2016-01-11 20:27:50 +08:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2016 Intel Corporation
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <errno.h>
|
|
|
|
|
2016-01-15 21:45:13 +08:00
|
|
|
#include <nanokernel.h>
|
|
|
|
#include <device.h>
|
|
|
|
#include <gpio.h>
|
|
|
|
|
2016-01-11 20:27:50 +08:00
|
|
|
#include <bluetooth/bluetooth.h>
|
2016-01-14 21:02:54 +08:00
|
|
|
#include <bluetooth/conn.h>
|
2016-01-15 21:45:13 +08:00
|
|
|
#include <bluetooth/log.h>
|
|
|
|
|
2016-03-01 19:52:49 +08:00
|
|
|
#include "version.h"
|
2016-01-21 18:08:38 +08:00
|
|
|
#include "gap_internal.h"
|
2016-01-15 21:45:13 +08:00
|
|
|
#include "uart.h"
|
2016-01-18 20:04:08 +08:00
|
|
|
#include "rpc.h"
|
2016-01-15 21:45:13 +08:00
|
|
|
|
2016-02-16 18:23:29 +08:00
|
|
|
#if !defined(CONFIG_NBLE_DEBUG_GAP)
|
|
|
|
#undef BT_DBG
|
|
|
|
#define BT_DBG(fmt, ...)
|
|
|
|
#endif
|
|
|
|
|
2016-01-15 21:45:13 +08:00
|
|
|
#define NBLE_SWDIO_PIN 6
|
|
|
|
#define NBLE_RESET_PIN NBLE_SWDIO_PIN
|
2016-01-21 18:07:13 +08:00
|
|
|
#define NBLE_BTWAKE_PIN 5
|
2016-01-15 21:45:13 +08:00
|
|
|
|
2016-01-21 18:08:38 +08:00
|
|
|
static bt_ready_cb_t bt_ready_cb;
|
2016-02-16 17:59:16 +08:00
|
|
|
static bt_le_scan_cb_t *scan_dev_found_cb;
|
2016-01-21 18:08:38 +08:00
|
|
|
|
2016-02-10 21:02:00 +08:00
|
|
|
/* Local Bluetooth LE Device Address */
|
2016-02-15 18:53:25 +08:00
|
|
|
bt_addr_le_t nble_bdaddr;
|
2016-02-10 21:02:00 +08:00
|
|
|
|
2016-02-16 18:23:29 +08:00
|
|
|
#if defined(CONFIG_NBLE_DEBUG_GAP)
|
2016-02-10 22:00:30 +08:00
|
|
|
static const char *bt_addr_le_str(const bt_addr_le_t *addr)
|
|
|
|
{
|
|
|
|
static char str[BT_ADDR_LE_STR_LEN];
|
|
|
|
|
|
|
|
bt_addr_le_to_str(addr, str, sizeof(str));
|
|
|
|
|
|
|
|
return str;
|
|
|
|
}
|
|
|
|
#endif /* CONFIG_BLUETOOTH_DEBUG */
|
|
|
|
|
2016-02-10 15:50:07 +08:00
|
|
|
static void send_dm_config(void)
|
|
|
|
{
|
2016-02-12 21:47:05 +08:00
|
|
|
struct nble_gap_sm_config_params config = {
|
|
|
|
.options = 1, /* bonding */
|
|
|
|
.io_caps = 3, /* no input no output */
|
|
|
|
.key_size = 16, /* or 7 */
|
|
|
|
.oob_present = 0, /* no oob */
|
|
|
|
};
|
2016-02-10 15:50:07 +08:00
|
|
|
|
2016-02-12 21:47:05 +08:00
|
|
|
nble_gap_sm_config_req(&config);
|
2016-02-10 15:50:07 +08:00
|
|
|
}
|
|
|
|
|
2016-01-21 18:08:38 +08:00
|
|
|
void on_nble_up(void)
|
|
|
|
{
|
|
|
|
BT_DBG("");
|
2016-01-26 19:24:52 +08:00
|
|
|
|
2016-02-10 15:50:07 +08:00
|
|
|
send_dm_config();
|
2016-01-21 18:08:38 +08:00
|
|
|
}
|
|
|
|
|
2016-02-12 21:47:05 +08:00
|
|
|
void on_nble_get_version_rsp(const struct nble_version_response *rsp)
|
2016-01-21 18:08:38 +08:00
|
|
|
{
|
2016-01-28 23:26:00 +08:00
|
|
|
BT_DBG("VERSION: %d.%d.%d %.20s", rsp->version.major,
|
|
|
|
rsp->version.minor, rsp->version.patch,
|
|
|
|
rsp->version.version_string);
|
2016-02-02 20:59:55 +08:00
|
|
|
|
|
|
|
if (bt_ready_cb) {
|
|
|
|
bt_ready_cb(0);
|
|
|
|
bt_ready_cb = NULL;
|
|
|
|
}
|
2016-01-21 18:08:38 +08:00
|
|
|
}
|
|
|
|
|
2016-01-11 20:27:50 +08:00
|
|
|
int bt_enable(bt_ready_cb_t cb)
|
|
|
|
{
|
2016-01-15 21:45:13 +08:00
|
|
|
struct device *gpio;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
BT_DBG("");
|
|
|
|
|
|
|
|
gpio = device_get_binding(CONFIG_GPIO_DW_0_NAME);
|
|
|
|
if (!gpio) {
|
2016-01-27 17:09:19 +08:00
|
|
|
BT_ERR("Cannot find %s", CONFIG_GPIO_DW_0_NAME);
|
2016-01-15 21:45:13 +08:00
|
|
|
return -ENODEV;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = gpio_pin_configure(gpio, NBLE_RESET_PIN, GPIO_DIR_OUT);
|
|
|
|
if (ret) {
|
|
|
|
BT_ERR("Error configuring pin %d", NBLE_RESET_PIN);
|
|
|
|
return -ENODEV;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Reset hold time is 0.2us (normal) or 100us (SWD debug) */
|
|
|
|
ret = gpio_pin_write(gpio, NBLE_RESET_PIN, 0);
|
|
|
|
if (ret) {
|
|
|
|
BT_ERR("Error pin write %d", NBLE_RESET_PIN);
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
2016-01-21 18:07:13 +08:00
|
|
|
ret = gpio_pin_configure(gpio, NBLE_BTWAKE_PIN, GPIO_DIR_OUT);
|
|
|
|
if (ret) {
|
|
|
|
BT_ERR("Error configuring pin %d", NBLE_BTWAKE_PIN);
|
|
|
|
return -ENODEV;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = gpio_pin_write(gpio, NBLE_BTWAKE_PIN, 1);
|
|
|
|
if (ret) {
|
|
|
|
BT_ERR("Error pin write %d", NBLE_BTWAKE_PIN);
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
2016-01-15 21:45:13 +08:00
|
|
|
/**
|
|
|
|
* NBLE reset is achieved by asserting low the SWDIO pin.
|
|
|
|
* However, the BLE Core chip can be in SWD debug mode,
|
|
|
|
* and NRF_POWER->RESET = 0 due to, other constraints: therefore,
|
|
|
|
* this reset might not work everytime, especially after
|
|
|
|
* flashing or debugging.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* sleep 1ms depending on context */
|
|
|
|
switch (sys_execution_context_type_get()) {
|
|
|
|
case NANO_CTX_FIBER:
|
|
|
|
fiber_sleep(MSEC(1));
|
|
|
|
break;
|
|
|
|
case NANO_CTX_TASK:
|
|
|
|
task_sleep(MSEC(1));
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
BT_ERR("ISR context is not supported");
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = nble_open();
|
|
|
|
if (ret) {
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = gpio_pin_write(gpio, NBLE_RESET_PIN, 1);
|
|
|
|
if (ret) {
|
|
|
|
BT_ERR("Error pin write %d", NBLE_RESET_PIN);
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Set back GPIO to input to avoid interfering with external debugger */
|
|
|
|
ret = gpio_pin_configure(gpio, NBLE_RESET_PIN, GPIO_DIR_IN);
|
|
|
|
if (ret) {
|
|
|
|
BT_ERR("Error configuring pin %d", NBLE_RESET_PIN);
|
|
|
|
return -ENODEV;
|
|
|
|
}
|
|
|
|
|
2016-01-21 18:08:38 +08:00
|
|
|
bt_ready_cb = cb;
|
|
|
|
|
2016-01-15 21:45:13 +08:00
|
|
|
return 0;
|
2016-01-11 20:27:50 +08:00
|
|
|
}
|
|
|
|
|
2016-01-26 08:48:35 +08:00
|
|
|
static bool valid_adv_param(const struct bt_le_adv_param *param)
|
|
|
|
{
|
|
|
|
switch (param->type) {
|
|
|
|
case BT_LE_ADV_IND:
|
|
|
|
case BT_LE_ADV_SCAN_IND:
|
|
|
|
case BT_LE_ADV_NONCONN_IND:
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (param->addr_type) {
|
2016-02-02 00:15:52 +08:00
|
|
|
case BT_LE_ADV_ADDR_IDENTITY:
|
2016-01-26 08:48:35 +08:00
|
|
|
break;
|
2016-02-02 00:17:35 +08:00
|
|
|
case BT_LE_ADV_ADDR_NRPA: /* nble doesn't support NRPAs (for now) */
|
2016-01-26 08:48:35 +08:00
|
|
|
default:
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (param->interval_min > param->interval_max ||
|
|
|
|
param->interval_min < 0x0020 || param->interval_max > 0x4000) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-01-11 20:27:50 +08:00
|
|
|
int bt_le_adv_start(const struct bt_le_adv_param *param,
|
2016-01-13 18:53:54 +08:00
|
|
|
const struct bt_data *ad, size_t ad_len,
|
|
|
|
const struct bt_data *sd, size_t sd_len)
|
2016-01-11 20:27:50 +08:00
|
|
|
{
|
2016-02-12 21:47:05 +08:00
|
|
|
struct nble_gap_adv_params params = { 0 };
|
2016-01-26 08:48:35 +08:00
|
|
|
int i;
|
|
|
|
|
|
|
|
if (!valid_adv_param(param)) {
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 0; i < ad_len; i++) {
|
|
|
|
uint8_t *p;
|
|
|
|
|
|
|
|
/* Check if ad fit in the remaining buffer */
|
|
|
|
if (params.ad.len + ad[i].data_len + 2 > 31) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
p = ¶ms.ad.data[params.ad.len];
|
|
|
|
*p++ = ad[i].data_len + 1;
|
|
|
|
*p++ = ad[i].type;
|
|
|
|
memcpy(p, ad[i].data, ad[i].data_len);
|
|
|
|
params.ad.len += ad[i].data_len + 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Don't bother with scan response if the advertising type isn't
|
|
|
|
* a scannable one.
|
|
|
|
*/
|
|
|
|
if (param->type != BT_LE_ADV_IND && param->type != BT_LE_ADV_SCAN_IND) {
|
|
|
|
goto send_set_param;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 0; i < sd_len; i++) {
|
|
|
|
uint8_t *p;
|
|
|
|
|
|
|
|
/* Check if sd fit in the remaining buffer */
|
|
|
|
if (params.sd.len + sd[i].data_len + 2 > 31) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
p = ¶ms.sd.data[params.sd.len];
|
|
|
|
*p++ = sd[i].data_len + 1;
|
|
|
|
*p++ = sd[i].type;
|
|
|
|
memcpy(p, sd[i].data, sd[i].data_len);
|
|
|
|
params.sd.len += sd[i].data_len + 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
send_set_param:
|
|
|
|
/* Timeout is handled by application timer */
|
|
|
|
params.timeout = 0;
|
|
|
|
/* forced to none currently (no whitelist support) */
|
|
|
|
params.filter_policy = 0;
|
|
|
|
params.interval_max = param->interval_max;
|
|
|
|
params.interval_min = param->interval_min;
|
|
|
|
params.type = param->type;
|
|
|
|
|
2016-02-12 21:47:05 +08:00
|
|
|
nble_gap_start_advertise_req(¶ms);
|
2016-01-26 08:48:35 +08:00
|
|
|
|
|
|
|
return 0;
|
2016-01-11 20:27:50 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
int bt_le_adv_stop(void)
|
|
|
|
{
|
|
|
|
return -ENOSYS;
|
|
|
|
}
|
|
|
|
|
2016-02-16 17:59:16 +08:00
|
|
|
static bool valid_le_scan_param(const struct bt_le_scan_param *param)
|
|
|
|
{
|
|
|
|
if (param->type != BT_HCI_LE_SCAN_PASSIVE &&
|
|
|
|
param->type != BT_HCI_LE_SCAN_ACTIVE) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (param->filter_dup != BT_HCI_LE_SCAN_FILTER_DUP_DISABLE &&
|
|
|
|
param->filter_dup != BT_HCI_LE_SCAN_FILTER_DUP_ENABLE) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (param->interval < 0x0004 || param->interval > 0x4000) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (param->window < 0x0004 || param->window > 0x4000) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (param->window > param->interval) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-01-11 20:27:50 +08:00
|
|
|
int bt_le_scan_start(const struct bt_le_scan_param *param, bt_le_scan_cb_t cb)
|
|
|
|
{
|
2016-02-16 17:59:16 +08:00
|
|
|
struct nble_gap_scan_params nble_params;
|
|
|
|
|
|
|
|
BT_DBG("");
|
|
|
|
|
|
|
|
/* Check that the parameters have valid values */
|
|
|
|
if (!valid_le_scan_param(param)) {
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
nble_params.interval = param->interval;
|
|
|
|
nble_params.window = param->window;
|
|
|
|
nble_params.scan_type = param->type;
|
|
|
|
nble_params.use_whitelist = 0;
|
|
|
|
|
|
|
|
/* Check is scan already enabled */
|
|
|
|
|
|
|
|
scan_dev_found_cb = cb;
|
|
|
|
|
|
|
|
nble_gap_start_scan_req(&nble_params);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void on_nble_gap_adv_report_evt(const struct nble_gap_adv_report_evt *evt,
|
|
|
|
const uint8_t *buf, uint8_t len)
|
|
|
|
{
|
|
|
|
BT_DBG("");
|
|
|
|
|
|
|
|
if (scan_dev_found_cb) {
|
|
|
|
scan_dev_found_cb(&evt->addr, evt->rssi, evt->adv_type,
|
|
|
|
buf, len);
|
|
|
|
}
|
2016-01-11 20:27:50 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
int bt_le_scan_stop(void)
|
|
|
|
{
|
2016-02-16 17:59:16 +08:00
|
|
|
BT_DBG("");
|
|
|
|
|
|
|
|
scan_dev_found_cb = NULL;
|
|
|
|
|
|
|
|
nble_gap_stop_scan_req();
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void on_nble_gap_scan_start_stop_rsp(const struct nble_response *rsp)
|
|
|
|
{
|
|
|
|
if (rsp->status) {
|
|
|
|
BT_ERR("Unable to stop scan, status %d", rsp->status);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
BT_DBG("");
|
2016-01-11 20:27:50 +08:00
|
|
|
}
|
2016-01-25 17:34:24 +08:00
|
|
|
|
2016-02-12 21:47:05 +08:00
|
|
|
void nble_log(const struct nble_log_s *param, char *format, uint8_t len)
|
2016-01-25 17:34:24 +08:00
|
|
|
{
|
2016-01-25 22:31:16 +08:00
|
|
|
#if defined(CONFIG_BLUETOOTH_DEBUG)
|
|
|
|
/* Build meaningful output */
|
|
|
|
printf("nble: ");
|
|
|
|
printf(format, param->param0, param->param1, param->param2,
|
|
|
|
param->param3);
|
|
|
|
printf("\n");
|
|
|
|
#endif
|
2016-01-25 17:34:24 +08:00
|
|
|
}
|
|
|
|
|
2016-02-12 21:47:05 +08:00
|
|
|
void on_nble_gap_read_bda_rsp(const struct nble_service_read_bda_response *rsp)
|
2016-01-25 17:34:24 +08:00
|
|
|
{
|
2016-02-10 21:02:00 +08:00
|
|
|
if (rsp->status) {
|
|
|
|
BT_ERR("Read bdaddr failed, status %d", rsp->status);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-02-15 18:53:25 +08:00
|
|
|
bt_addr_le_copy(&nble_bdaddr, &rsp->bd);
|
2016-02-10 21:02:00 +08:00
|
|
|
|
2016-02-15 18:53:25 +08:00
|
|
|
BT_DBG("Local bdaddr: %s", bt_addr_le_str(&nble_bdaddr));
|
2016-02-10 22:00:30 +08:00
|
|
|
|
2016-02-12 21:47:05 +08:00
|
|
|
nble_get_version_req(NULL);
|
2016-01-25 17:34:24 +08:00
|
|
|
}
|
|
|
|
|
2016-02-12 21:47:05 +08:00
|
|
|
void on_nble_gap_sm_config_rsp(struct nble_gap_sm_config_rsp *rsp)
|
2016-01-25 17:34:24 +08:00
|
|
|
{
|
2016-02-10 17:53:36 +08:00
|
|
|
if (rsp->status) {
|
|
|
|
BT_ERR("SM config failed, status %d", rsp->status);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-02-19 21:52:05 +08:00
|
|
|
BT_DBG("status %u", rsp->status);
|
2016-02-10 21:02:00 +08:00
|
|
|
|
|
|
|
/* Get bdaddr queued after SM setup */
|
2016-02-12 21:47:05 +08:00
|
|
|
nble_gap_read_bda_req(NULL);
|
2016-01-25 17:34:24 +08:00
|
|
|
}
|