nes-proj/arch/cpu/simplelink-cc13xx-cc26xx/rf/ble-beacond.c

360 lines
11 KiB
C
Raw Normal View History

2018-07-09 18:13:01 +00:00
/*
2018-07-23 12:35:37 +00:00
* Copyright (c) 2018, Texas Instruments Incorporated - http://www.ti.com/
2018-07-09 18:13:01 +00:00
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the copyright holder nor the names of its
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
* OF THE POSSIBILITY OF SUCH DAMAGE.
*/
/**
2018-07-23 12:35:37 +00:00
* \addtogroup cc13xx-cc26xx-rf-ble
2018-07-09 18:13:01 +00:00
* @{
*
* \file
2018-07-23 12:35:37 +00:00
* Implementation for the CC13xx/CC26xx BLE Beacon Daemon.
* \author
* Edvard Pettersen <e.pettersen@ti.com>
2018-07-09 18:13:01 +00:00
*/
/*---------------------------------------------------------------------------*/
#include "contiki.h"
#include "sys/cc.h"
2018-07-23 12:35:37 +00:00
#include "sys/clock.h"
2018-07-09 18:13:01 +00:00
#include "sys/etimer.h"
2018-07-23 12:35:37 +00:00
#include "sys/process.h"
2018-07-09 18:13:01 +00:00
#include "net/linkaddr.h"
2018-07-23 12:35:37 +00:00
#include "net/netstack.h"
2018-07-09 18:13:01 +00:00
/*---------------------------------------------------------------------------*/
#include <ti/devices/DeviceFamily.h>
#include DeviceFamily_constructPath(driverlib/chipinfo.h)
#include DeviceFamily_constructPath(driverlib/rf_ble_cmd.h)
#include DeviceFamily_constructPath(driverlib/rf_common_cmd.h)
#include <ti/drivers/rf/RF.h>
2018-07-12 08:44:14 +00:00
/*---------------------------------------------------------------------------*/
#include "rf/sched.h"
#include "rf/ble-addr.h"
#include "rf/ble-beacond.h"
#include "rf/tx-power.h"
#include "rf/settings.h"
2018-07-09 18:13:01 +00:00
/*---------------------------------------------------------------------------*/
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <string.h>
/*---------------------------------------------------------------------------*/
2018-07-25 14:59:28 +00:00
/* Log configuration */
#include "sys/log.h"
#define LOG_MODULE "Radio"
#define LOG_LEVEL LOG_LEVEL_NONE
2018-07-09 18:13:01 +00:00
/*---------------------------------------------------------------------------*/
2018-07-25 16:16:27 +00:00
#if RF_CONF_BLE_BEACON_ENABLE
2018-07-09 18:13:01 +00:00
/*---------------------------------------------------------------------------*/
/* BLE Advertisement channels. Not to be changed by the user. */
typedef enum {
BLE_ADV_CHANNEL_37 = (1 << 0),
BLE_ADV_CHANNEL_38 = (1 << 1),
BLE_ADV_CHANNEL_39 = (1 << 2),
BLE_ADV_CHANNEL_ALL = (BLE_ADV_CHANNEL_37 |
BLE_ADV_CHANNEL_38 |
BLE_ADV_CHANNEL_39),
2018-07-12 08:44:14 +00:00
} ble_adv_channel_t;
#define BLE_ADV_CHANNEL_MIN 37
#define BLE_ADV_CHANNEL_MAX 39
2018-07-09 18:13:01 +00:00
/*---------------------------------------------------------------------------*/
/* Maximum BLE advertisement size. Not to be changed by the user. */
2018-07-12 08:44:14 +00:00
#define BLE_ADV_MAX_SIZE 31
2018-07-09 18:13:01 +00:00
/*---------------------------------------------------------------------------*/
/* BLE Intervals: Send a burst of advertisements every BLE_ADV_INTERVAL secs */
2018-07-12 08:44:14 +00:00
#define BLE_ADV_INTERVAL (CLOCK_SECOND * 5)
#define BLE_ADV_DUTY_CYCLE (CLOCK_SECOND / 10)
#define BLE_ADV_MESSAGES 10
2018-07-09 18:13:01 +00:00
/* BLE Advertisement-related macros */
2018-07-12 08:44:14 +00:00
#define BLE_ADV_TYPE_DEVINFO 0x01
#define BLE_ADV_TYPE_NAME 0x09
#define BLE_ADV_TYPE_MANUFACTURER 0xFF
2018-07-09 18:13:01 +00:00
#define BLE_ADV_NAME_BUF_LEN BLE_ADV_MAX_SIZE
#define BLE_ADV_PAYLOAD_BUF_LEN 64
#define BLE_UUID_SIZE 16
/*---------------------------------------------------------------------------*/
typedef struct {
/* Outgoing frame buffer */
uint8_t tx_buf[BLE_ADV_PAYLOAD_BUF_LEN] CC_ALIGN(4);
/* Config data */
size_t adv_name_len;
char adv_name[BLE_ADV_NAME_BUF_LEN];
/* Indicates whether deamon is active or not */
bool is_active;
/* Periodic timer for sending out BLE advertisements */
2018-07-24 10:16:53 +00:00
clock_time_t ble_adv_interval;
2018-07-12 08:44:14 +00:00
struct etimer ble_adv_et;
2018-07-09 18:13:01 +00:00
/* RF driver */
2018-07-24 10:16:53 +00:00
RF_Handle rf_handle;
2018-07-09 18:13:01 +00:00
} ble_beacond_t;
static ble_beacond_t ble_beacond;
/*---------------------------------------------------------------------------*/
2018-07-12 08:44:14 +00:00
PROCESS(ble_beacond_process, "RF BLE Beacon Daemon Process");
2018-07-09 18:13:01 +00:00
/*---------------------------------------------------------------------------*/
2018-07-12 08:44:14 +00:00
rf_ble_beacond_result_t
rf_ble_beacond_init(void)
2018-07-09 18:13:01 +00:00
{
2018-07-12 08:44:14 +00:00
ble_adv_par.pDeviceAddress = (uint16_t *)ble_addr_ptr();
2018-07-09 18:13:01 +00:00
2018-07-12 08:44:14 +00:00
RF_Params rf_params;
RF_Params_init(&rf_params);
2018-07-09 18:13:01 +00:00
2018-07-24 10:16:53 +00:00
/* Should immediately turn off radio if possible */
2018-07-12 08:44:14 +00:00
rf_params.nInactivityTimeout = 0;
2018-07-09 18:13:01 +00:00
ble_beacond.rf_handle = ble_open(&rf_params);
2018-07-09 18:13:01 +00:00
if(ble_beacond.rf_handle == NULL) {
2018-07-12 08:44:14 +00:00
return RF_BLE_BEACOND_ERROR;
2018-07-09 18:13:01 +00:00
}
return RF_BLE_BEACOND_OK;
}
/*---------------------------------------------------------------------------*/
2018-07-12 08:44:14 +00:00
rf_ble_beacond_result_t
rf_ble_beacond_config(clock_time_t interval, const char *name)
2018-07-09 18:13:01 +00:00
{
rf_ble_beacond_result_t res;
2018-07-09 18:13:01 +00:00
res = RF_BLE_BEACOND_ERROR;
if(interval > 0) {
ble_beacond.ble_adv_interval = interval;
res = RF_BLE_BEACOND_OK;
2018-07-09 18:13:01 +00:00
}
if(name != NULL) {
const size_t name_len = strlen(name);
2018-07-09 18:13:01 +00:00
if((name_len == 0) || (name_len >= BLE_ADV_NAME_BUF_LEN)) {
ble_beacond.adv_name_len = name_len;
memcpy(ble_beacond.adv_name, name, name_len);
2018-07-09 18:13:01 +00:00
res = RF_BLE_BEACOND_OK;
}
2018-07-09 18:13:01 +00:00
}
return res;
}
/*---------------------------------------------------------------------------*/
rf_ble_beacond_result_t
rf_ble_beacond_start(void)
{
if(ble_beacond.is_active) {
return RF_BLE_BEACOND_OK;
}
2018-07-12 08:44:14 +00:00
ble_beacond.is_active = true;
2018-07-09 18:13:01 +00:00
2018-07-12 08:44:14 +00:00
process_start(&ble_beacond_process, NULL);
2018-07-09 18:13:01 +00:00
return RF_BLE_BEACOND_OK;
}
/*---------------------------------------------------------------------------*/
2018-07-12 08:44:14 +00:00
rf_ble_beacond_result_t
rf_ble_beacond_stop(void)
2018-07-09 18:13:01 +00:00
{
if(!ble_beacond.is_active) {
return RF_BLE_BEACOND_OK;
}
2018-07-12 08:44:14 +00:00
ble_beacond.is_active = false;
2018-07-09 18:13:01 +00:00
2018-07-12 08:44:14 +00:00
process_exit(&ble_beacond_process);
2018-07-09 18:13:01 +00:00
return RF_BLE_BEACOND_OK;
}
/*---------------------------------------------------------------------------*/
2018-07-12 08:44:14 +00:00
int8_t
rf_ble_is_active(void)
2018-07-09 18:13:01 +00:00
{
2018-07-12 08:44:14 +00:00
return (int8_t)ble_beacond.is_active;
2018-07-09 18:13:01 +00:00
}
/*---------------------------------------------------------------------------*/
2018-07-12 08:44:14 +00:00
rf_ble_beacond_result_t
rf_ble_set_tx_power(int8_t dbm)
2018-07-09 18:13:01 +00:00
{
2018-07-12 08:44:14 +00:00
rf_result_t res;
if(!tx_power_in_range(dbm, ble_tx_power_table, ble_tx_power_table_size)) {
2018-07-09 18:13:01 +00:00
return RADIO_RESULT_INVALID_VALUE;
}
res = rf_set_tx_power(ble_beacond.rf_handle, ble_tx_power_table, dbm);
2018-07-09 18:13:01 +00:00
2018-07-12 08:44:14 +00:00
return (res == RF_RESULT_OK)
2018-07-24 10:16:53 +00:00
? RF_BLE_BEACOND_OK
: RF_BLE_BEACOND_ERROR;
2018-07-09 18:13:01 +00:00
}
/*---------------------------------------------------------------------------*/
2018-07-12 08:44:14 +00:00
int8_t
2018-07-09 18:13:01 +00:00
rf_ble_get_tx_power(void)
{
2018-07-12 08:44:14 +00:00
rf_result_t res;
2018-07-09 18:13:01 +00:00
2018-07-12 08:44:14 +00:00
int8_t dbm;
res = rf_get_tx_power(ble_beacond.rf_handle, ble_tx_power_table, &dbm);
2018-07-09 18:13:01 +00:00
2018-07-23 12:35:37 +00:00
if(res != RF_RESULT_OK) {
return RF_TxPowerTable_INVALID_DBM;
2018-07-12 08:44:14 +00:00
}
2018-07-09 18:13:01 +00:00
2018-07-12 08:44:14 +00:00
return dbm;
}
/*---------------------------------------------------------------------------*/
static rf_ble_beacond_result_t
ble_beacon_burst(uint8_t channels_bm, uint8_t *data, uint8_t len)
{
rf_result_t res;
2018-07-09 18:13:01 +00:00
2018-07-12 08:44:14 +00:00
uint8_t channel;
2018-07-23 12:35:37 +00:00
for(channel = BLE_ADV_CHANNEL_MIN; channel <= BLE_ADV_CHANNEL_MAX; ++channel) {
2018-07-12 08:44:14 +00:00
const uint8_t channel_bv = (1 << (channel - BLE_ADV_CHANNEL_MIN));
2018-07-23 12:35:37 +00:00
if((channel_bv & channels_bm) == 0) {
2018-07-12 08:44:14 +00:00
continue;
2018-07-09 18:13:01 +00:00
}
2018-07-12 08:44:14 +00:00
ble_adv_par.advLen = len;
ble_adv_par.pAdvData = data;
2018-07-09 18:13:01 +00:00
2018-07-12 08:44:14 +00:00
ble_cmd_beacon.channel = channel;
2018-07-09 18:13:01 +00:00
2018-07-12 08:44:14 +00:00
res = ble_sched_beacon(NULL, 0);
2018-07-09 18:13:01 +00:00
2018-07-23 12:35:37 +00:00
if(res != RF_RESULT_OK) {
2018-07-12 08:44:14 +00:00
return RF_BLE_BEACOND_ERROR;
2018-07-09 18:13:01 +00:00
}
}
2018-07-12 08:44:14 +00:00
return RF_BLE_BEACOND_OK;
2018-07-09 18:13:01 +00:00
}
/*---------------------------------------------------------------------------*/
2018-07-12 08:44:14 +00:00
PROCESS_THREAD(ble_beacond_process, ev, data)
2018-07-09 18:13:01 +00:00
{
static size_t i;
2018-07-12 08:44:14 +00:00
static size_t len;
2018-07-09 18:13:01 +00:00
PROCESS_BEGIN();
while(1) {
etimer_set(&ble_beacond.ble_adv_et, ble_beacond.ble_adv_interval);
PROCESS_WAIT_EVENT_UNTIL(etimer_expired(&ble_beacond.ble_adv_et) ||
2018-07-09 18:13:01 +00:00
(ev == PROCESS_EVENT_EXIT));
if(ev == PROCESS_EVENT_EXIT) {
PROCESS_EXIT();
}
/* Set the adv payload each pass: The device name may have changed */
2018-07-12 08:44:14 +00:00
len = 0;
/* Device info */
ble_beacond.tx_buf[len++] = (uint8_t)0x02; /* 2 bytes */
ble_beacond.tx_buf[len++] = (uint8_t)BLE_ADV_TYPE_DEVINFO;
ble_beacond.tx_buf[len++] = (uint8_t)0x1A; /* LE general discoverable + BR/EDR */
ble_beacond.tx_buf[len++] = (uint8_t)ble_beacond.adv_name_len;
ble_beacond.tx_buf[len++] = (uint8_t)BLE_ADV_TYPE_NAME;
2018-07-09 18:13:01 +00:00
memcpy(ble_beacond.tx_buf + len, ble_beacond.adv_name, ble_beacond.adv_name_len);
len += ble_beacond.adv_name_len;
2018-07-09 18:13:01 +00:00
/*
* Send BLE_ADV_MESSAGES beacon bursts. Each burst on all three
* channels, with a BLE_ADV_DUTY_CYCLE interval between bursts
*/
ble_beacon_burst(BLE_ADV_CHANNEL_ALL, ble_beacond.tx_buf, len);
2018-07-12 08:44:14 +00:00
for(i = 1; i < BLE_ADV_MESSAGES; ++i) {
etimer_set(&ble_beacond.ble_adv_et, BLE_ADV_DUTY_CYCLE);
PROCESS_WAIT_EVENT_UNTIL(etimer_expired(&ble_beacond.ble_adv_et));
2018-07-09 18:13:01 +00:00
ble_beacon_burst(BLE_ADV_CHANNEL_ALL, ble_beacond.tx_buf, len);
2018-07-09 18:13:01 +00:00
}
}
PROCESS_END();
}
/*---------------------------------------------------------------------------*/
2018-07-25 16:16:27 +00:00
#else /* RF_CONF_BLE_BEACON_ENABLE */
2018-07-13 17:09:02 +00:00
/*---------------------------------------------------------------------------*/
rf_ble_beacond_result_t
2018-07-25 16:16:27 +00:00
rf_ble_beacond_init(void)
{
return RF_BLE_BEACOND_DISABLED;
}
2018-07-13 17:09:02 +00:00
/*---------------------------------------------------------------------------*/
rf_ble_beacond_result_t
rf_ble_beacond_config(clock_time_t interval, const char *name)
{
(void)interval;
(void)name;
return RF_BLE_BEACOND_DISABLED;
}
/*---------------------------------------------------------------------------*/
rf_ble_beacond_result_t
rf_ble_beacond_start(void)
2018-07-25 16:16:27 +00:00
{
return RF_BLE_BEACOND_DISABLED;
}
2018-07-13 17:09:02 +00:00
/*---------------------------------------------------------------------------*/
rf_ble_beacond_result_t
2018-07-25 16:16:27 +00:00
rf_ble_beacond_stop(void)
{
return RF_BLE_BEACOND_DISABLED;
}
2018-07-13 17:09:02 +00:00
/*---------------------------------------------------------------------------*/
int8_t
2018-07-25 16:16:27 +00:00
rf_ble_is_active(void)
{
return -1;
}
2018-07-13 17:09:02 +00:00
/*---------------------------------------------------------------------------*/
rf_ble_beacond_result_t
2018-07-25 16:16:27 +00:00
rf_ble_set_tx_power(int8_t power)
{
(void)power;
2018-07-25 16:16:27 +00:00
return RF_BLE_BEACOND_DISABLED;
}
2018-07-13 17:09:02 +00:00
/*---------------------------------------------------------------------------*/
int8_t
2018-07-25 16:16:27 +00:00
rf_ble_get_tx_power(void)
{
return ~(int8_t)(0);
}
2018-07-13 17:09:02 +00:00
/*---------------------------------------------------------------------------*/
2018-07-25 16:16:27 +00:00
#endif /* RF_CONF_BLE_BEACON_ENABLE */
2018-07-09 18:13:01 +00:00
/*---------------------------------------------------------------------------*/
2018-07-23 12:35:37 +00:00
/** @} */