From a03b9c74659e755ebb186a12b8b5597b9e1d9c8d Mon Sep 17 00:00:00 2001 From: Hrishikesh Dhayagude Date: Mon, 3 Feb 2020 12:19:38 +0530 Subject: [PATCH 1/2] Add NimBLE files in ESP-BLE-Mesh again Revert "ble_mesh: Remove NimBLE implementation for ESP-BLE-MESH" This reverts commit 925f93614ca0113223bca5821130b73997f0b5dc. --- components/bt/CMakeLists.txt | 2 +- .../mesh_core/include/mesh_bearer_adapt.h | 11 + .../mesh_core/nimble_host/mesh_bearer_adapt.c | 1824 +++++++++++++++++ examples/bluetooth/esp_ble_mesh/README.md | 2 +- .../main/ble_mesh_demo_init.c | 80 + .../main/ble_mesh_demo_init.c | 80 + .../onoff_client/main/ble_mesh_demo_init.c | 80 + .../onoff_server/main/ble_mesh_demo_init.c | 80 + .../main/ble_mesh_demo_init.c | 80 + .../main/ble_mesh_demo_init.c | 80 + 10 files changed, 2317 insertions(+), 2 deletions(-) create mode 100644 components/bt/esp_ble_mesh/mesh_core/nimble_host/mesh_bearer_adapt.c diff --git a/components/bt/CMakeLists.txt b/components/bt/CMakeLists.txt index aa94bdad5b..337fcfe977 100644 --- a/components/bt/CMakeLists.txt +++ b/components/bt/CMakeLists.txt @@ -282,6 +282,7 @@ if(CONFIG_BT_ENABLED) if(CONFIG_BLE_MESH) list(APPEND COMPONENT_ADD_INCLUDEDIRS bluedroid/osi/include) + list(APPEND COMPONENT_SRCS "esp_ble_mesh/mesh_core/bluedroid_host/mesh_bearer_adapt.c") endif() endif() @@ -326,7 +327,6 @@ if(CONFIG_BT_ENABLED) "esp_ble_mesh/mesh_common/mesh_common.c" "esp_ble_mesh/mesh_common/mesh_kernel.c" "esp_ble_mesh/mesh_common/mesh_util.c" - "esp_ble_mesh/mesh_core/bluedroid_host/mesh_bearer_adapt.c" "esp_ble_mesh/mesh_core/storage/settings_nvs.c" "esp_ble_mesh/mesh_core/access.c" "esp_ble_mesh/mesh_core/adv.c" diff --git a/components/bt/esp_ble_mesh/mesh_core/include/mesh_bearer_adapt.h b/components/bt/esp_ble_mesh/mesh_core/include/mesh_bearer_adapt.h index f022a2576d..7c3ead1d0a 100644 --- a/components/bt/esp_ble_mesh/mesh_core/include/mesh_bearer_adapt.h +++ b/components/bt/esp_ble_mesh/mesh_core/include/mesh_bearer_adapt.h @@ -23,6 +23,17 @@ #define BLE_MESH_ADV_TASK_CORE TASK_PINNED_TO_CORE #endif +#ifdef CONFIG_BT_NIMBLE_ENABLED +#define BLE_MESH_MAX_CONN CONFIG_BT_NIMBLE_MAX_CONNECTIONS + +#ifdef CONFIG_BT_NIMBLE_PINNED_TO_CORE +#define BLE_MESH_ADV_TASK_CORE (CONFIG_BT_NIMBLE_PINNED_TO_CORE < portNUM_PROCESSORS ? CONFIG_BT_NIMBLE_PINNED_TO_CORE : tskNO_AFFINITY) +#else +#define BLE_MESH_ADV_TASK_CORE (0) +#endif + +#endif + #define BLE_MESH_ADV_TASK_STACK_SIZE 3072 #define BLE_MESH_GAP_ADV_MAX_LEN 31 diff --git a/components/bt/esp_ble_mesh/mesh_core/nimble_host/mesh_bearer_adapt.c b/components/bt/esp_ble_mesh/mesh_core/nimble_host/mesh_bearer_adapt.c new file mode 100644 index 0000000000..e997be4db1 --- /dev/null +++ b/components/bt/esp_ble_mesh/mesh_core/nimble_host/mesh_bearer_adapt.c @@ -0,0 +1,1824 @@ +/* + * Copyright (c) 2017 Nordic Semiconductor ASA + * Copyright (c) 2015-2016 Intel Corporation + * Additional Copyright (c) 2018 Espressif Systems (Shanghai) PTE LTD + * + * SPDX-License-Identifier: Apache-2.0 + */ + +#include + +#include "btc/btc_task.h" +#include "osi/alarm.h" + +#include "mbedtls/aes.h" +#include "mbedtls/ecp.h" + +#include "host/ble_hs.h" +#include "host/ble_uuid.h" +#include "host/ble_att.h" +#include "host/ble_gatt.h" +#include "services/gap/ble_svc_gap.h" +#include "services/gatt/ble_svc_gatt.h" + +#include "mesh_hci.h" +#include "mesh_common.h" +#include "mesh_aes_encrypt.h" +#include "provisioner_prov.h" + +/** @def BT_UUID_MESH_PROV + * @brief Mesh Provisioning Service + */ +#define BT_UUID_MESH_PROV_VAL 0x1827 +/** @def BT_UUID_MESH_PROXY + * @brief Mesh Proxy Service + */ +#define BT_UUID_MESH_PROXY_VAL 0x1828 +/** @def BT_UUID_GATT_CCC + * @brief GATT Client Characteristic Configuration + */ +#define BT_UUID_GATT_CCC_VAL 0x2902 +/** @def BT_UUID_MESH_PROV_DATA_IN + * @brief Mesh Provisioning Data In + */ +#define BT_UUID_MESH_PROV_DATA_IN_VAL 0x2adb +/** @def BT_UUID_MESH_PROV_DATA_OUT + * @brief Mesh Provisioning Data Out + */ +#define BT_UUID_MESH_PROV_DATA_OUT_VAL 0x2adc +/** @def BT_UUID_MESH_PROXY_DATA_IN + * @brief Mesh Proxy Data In + */ +#define BT_UUID_MESH_PROXY_DATA_IN_VAL 0x2add +/** @def BT_UUID_MESH_PROXY_DATA_OUT + * @brief Mesh Proxy Data Out + */ +#define BT_UUID_MESH_PROXY_DATA_OUT_VAL 0x2ade + +#define BLE_MESH_GATT_GET_CONN_ID(conn_id) ((u16_t)(conn_id)) +#define BLE_MESH_GATT_CREATE_CONN_ID(conn_id) ((u16_t)(conn_id)) + +static uint16_t proxy_svc_start_handle, prov_svc_start_handle; +struct bt_mesh_dev bt_mesh_dev; + +/* P-256 Variables */ +static u8_t bt_mesh_public_key[64]; +static u8_t bt_mesh_private_key[32] = { + 0x3f, 0x49, 0xf6, 0xd4, 0xa3, 0xc5, 0x5f, 0x38, + 0x74, 0xc9, 0xb3, 0xe3, 0xd2, 0x10, 0x3f, 0x50, + 0x4a, 0xff, 0x60, 0x7b, 0xeb, 0x40, 0xb7, 0x99, + 0x58, 0x99, 0xb8, 0xa6, 0xcd, 0x3c, 0x1a, 0xbd +}; + +/* Scan related functions */ +static bt_mesh_scan_cb_t *bt_mesh_scan_dev_found_cb; + +#if defined(CONFIG_BLE_MESH_NODE) && CONFIG_BLE_MESH_NODE +/* the gatt database list to save the attribute table */ +static sys_slist_t bt_mesh_gatts_db; + +/* Static Variables */ +static struct bt_mesh_conn bt_mesh_gatts_conn[BLE_MESH_MAX_CONN]; +static struct bt_mesh_conn_cb *bt_mesh_gatts_conn_cb; + +static u8_t bt_mesh_gatts_addr[6]; + +#endif /* defined(CONFIG_BLE_MESH_NODE) && CONFIG_BLE_MESH_NODE */ + +int bt_mesh_host_init(void) +{ + int rc; + rc = btc_init(); + if (rc != 0) { + return -1; + } + + rc = osi_alarm_create_mux(); + if (rc != 0) { + return -1; + } + + osi_alarm_init(); + return 0; +} + +uint8_t ble_hs_hci_get_hci_version(void); + +void bt_mesh_hci_init(void) +{ + /** + * Currently 20ms non-connectable adv interval is supported, and we need to add + * a flag to indicate this support. + */ +#ifdef CONFIG_BLE_MESH_HCI_5_0 + bt_mesh_dev.hci_version = BLE_MESH_HCI_VERSION_5_0; +#else + bt_mesh_dev.hci_version = ble_hs_hci_get_hci_version(); +#endif + return; +} + +static struct ble_gap_disc_params scan_param; +#if (CONFIG_BLE_MESH_PROVISIONER && CONFIG_BLE_MESH_PB_GATT) || \ + CONFIG_BLE_MESH_GATT_PROXY_CLIENT +static struct gattc_prov_info { + /* Service to be found depends on the type of adv pkt received */ + struct bt_mesh_conn conn; + bt_mesh_addr_t addr; + u16_t service_uuid; + u16_t mtu; + bool wr_desc_done; /* Indicate if write char descriptor event is received */ + u16_t start_handle; /* Service attribute start handle */ + u16_t end_handle; /* Service attribute end handle */ + u16_t data_in_handle; /* Data In Characteristic attribute handle */ + u16_t data_out_handle; /* Data Out Characteristic attribute handle */ + u16_t ccc_handle; /* Data Out Characteristic CCC attribute handle */ +} bt_mesh_gattc_info[BLE_MESH_MAX_CONN]; + +static struct bt_mesh_prov_conn_cb *bt_mesh_gattc_conn_cb; + +static int ble_on_subscribe(uint16_t conn_handle, + const struct ble_gatt_error *error, + struct ble_gatt_attr *attr, + void *arg) +{ + struct bt_mesh_conn *conn = NULL; + uint8_t value[2] = {0x01, 0x00}; + int i = (int)arg, j, len; + MODLOG_DFLT(INFO, "Subscribe complete; status=%d conn_handle=%d " + "attr_handle=%d\n", + error->status, conn_handle, attr->handle); + + for (j = i + 1; j < ARRAY_SIZE(bt_mesh_gattc_info); j++) { + if ((bt_mesh_gattc_info[j].conn.handle == conn_handle) && bt_mesh_gattc_info[j].ccc_handle) { + break; + } + } + if (j == ARRAY_SIZE(bt_mesh_gattc_info)) { + + conn = &bt_mesh_gattc_info[i].conn; + + if (bt_mesh_gattc_info[i].ccc_handle != attr->handle) { + BT_WARN("%s, gattc ccc_handle is not matched", __func__); + bt_mesh_gattc_disconnect(conn); + return 0; + } + + if (bt_mesh_gattc_info[i].service_uuid == BLE_MESH_UUID_MESH_PROV_VAL) { + if (bt_mesh_gattc_conn_cb != NULL && bt_mesh_gattc_conn_cb->prov_write_descr != NULL) { + len = bt_mesh_gattc_conn_cb->prov_write_descr(&bt_mesh_gattc_info[i].addr, &bt_mesh_gattc_info[i].conn); + if (len < 0) { + BT_ERR("%s, prov_write_descr failed", __func__); + bt_mesh_gattc_disconnect(conn); + return 0; + } + bt_mesh_gattc_info[i].wr_desc_done = true; + } + } else if (bt_mesh_gattc_info[i].service_uuid == BLE_MESH_UUID_MESH_PROXY_VAL) { + if (bt_mesh_gattc_conn_cb != NULL && bt_mesh_gattc_conn_cb->proxy_write_descr != NULL) { + len = bt_mesh_gattc_conn_cb->proxy_write_descr(&bt_mesh_gattc_info[i].addr, &bt_mesh_gattc_info[i].conn); + if (len < 0) { + BT_ERR("%s, proxy_write_descr failed", __func__); + bt_mesh_gattc_disconnect(conn); + return 0; + } + bt_mesh_gattc_info[i].wr_desc_done = true; + } + } + + + return 0; + } + + ble_gattc_write_flat(conn_handle, bt_mesh_gattc_info[i].ccc_handle, value, sizeof(value), ble_on_subscribe, (void *)j); + return 0; +} + +static int dsc_disced(uint16_t conn_handle, const struct ble_gatt_error *error, + uint16_t chr_val_handle, const struct ble_gatt_dsc *dsc, + void *arg) +{ + int rc = 0, j, i = (int)arg; /* char index */ + uint8_t value[2] = {0x01, 0x00}; + + switch (error->status) { + case 0: + bt_mesh_gattc_info[i].ccc_handle = dsc->handle; + break; + + case BLE_HS_EDONE: + /* All descriptors in this characteristic discovered; start discovering + * descriptors in the next characteristic. + */ + for (j = i + 1; j < ARRAY_SIZE(bt_mesh_gattc_info); j++) { + if ((bt_mesh_gattc_info[j].conn.handle == conn_handle) && bt_mesh_gattc_info[j].data_out_handle) { + break; + } + } + if (j == ARRAY_SIZE(bt_mesh_gattc_info)) { + /* Register Notification for Mesh Provisioning/Proxy Data Out Characteristic */ + for (j = 0; j < ARRAY_SIZE(bt_mesh_gattc_info); j++) { + if ((bt_mesh_gattc_info[j].conn.handle == conn_handle) && bt_mesh_gattc_info[j].ccc_handle) { + break; + } + } + if (j == ARRAY_SIZE(bt_mesh_gattc_info)) { + return 0; + } + ble_gattc_write_flat(conn_handle, bt_mesh_gattc_info[i].ccc_handle, value, sizeof(value), ble_on_subscribe, (void *)j); + } else { + ble_gattc_disc_all_dscs(conn_handle, bt_mesh_gattc_info[j].data_out_handle, 0xffff, dsc_disced, (void *)j); + } + rc = 0; + break; + + default: + /* Error; abort discovery. */ + rc = error->status; + break; + } + + return rc; +} + + +static int chr_disced(uint16_t conn_handle, const struct ble_gatt_error *error, + const struct ble_gatt_chr *chr, void *arg) +{ + int rc = 0, j; + uint16_t uuid16 = 0; + int i = (int)arg; /* service index */ + struct bt_mesh_conn *conn = &bt_mesh_gattc_info[i].conn; + const ble_uuid_any_t *uuid = &chr->uuid; + if (chr) { + uuid16 = (uint16_t) BLE_UUID16(uuid)->value; + } + switch (error->status) { + case 0: + /* Get Mesh Provisioning/Proxy Data In/Out Characteristic */ + if ((uuid16 == BLE_MESH_UUID_MESH_PROV_DATA_IN_VAL) || (uuid16 == BLE_MESH_UUID_MESH_PROXY_DATA_IN_VAL)) { + if (!(chr->properties & BLE_MESH_GATT_CHRC_WRITE_WITHOUT_RESP)) { + bt_mesh_gattc_disconnect(conn); + BT_ERR("Write without response is not set for Data In characteristic"); + return 0; + } + bt_mesh_gattc_info[i].data_in_handle = chr->val_handle; + } else if ((uuid16 == BLE_MESH_UUID_MESH_PROV_DATA_OUT_VAL) || (uuid16 == BLE_MESH_UUID_MESH_PROXY_DATA_OUT_VAL)) { + if (!(chr->properties & BLE_MESH_GATT_CHRC_NOTIFY)) { + bt_mesh_gattc_disconnect(conn); + BT_ERR("Notify is not set for Data Out characteristic"); + return 0; + } + bt_mesh_gattc_info[i].data_out_handle = chr->val_handle; + } + break; + case BLE_HS_EDONE: + /* All characteristics in this service discovered; start discovering + * characteristics in the next service. + */ + for (j = i + 1; j < ARRAY_SIZE(bt_mesh_gattc_info); j++) { + if ((bt_mesh_gattc_info[j].conn.handle == conn_handle) && (bt_mesh_gattc_info[j].start_handle > bt_mesh_gattc_info[j].end_handle)) { + break; + } + } + if (j == ARRAY_SIZE(bt_mesh_gattc_info)) { + for (j = 0; j < ARRAY_SIZE(bt_mesh_gattc_info); j++) { + if ((bt_mesh_gattc_info[j].conn.handle == conn_handle) && bt_mesh_gattc_info[j].data_out_handle) { + break; + } + } + ble_gattc_disc_all_dscs(conn_handle, bt_mesh_gattc_info[j].data_out_handle, 0xffff, dsc_disced, (void *)j); + } else { + ble_gattc_disc_all_chrs(conn_handle, bt_mesh_gattc_info[j].start_handle, bt_mesh_gattc_info[j].end_handle, + chr_disced, (void *)j); + } + break; + + default: + rc = error->status; + break; + } + + return rc; +} + + +static int svc_disced(uint16_t conn_handle, const struct ble_gatt_error *error, + const struct ble_gatt_svc *service, void *arg) +{ + struct bt_mesh_conn *conn = NULL; + int rc = 0, i; + const ble_uuid_any_t *uuid; + uint8_t uuid_length; + switch (error->status) { + case 0: + if (!service) { + return 0; + } + uuid = &service->uuid; + uuid_length = (uint8_t) (uuid->u.type == BLE_UUID_TYPE_16 ? 2 : 16); + if (uuid_length != 2) { + return 0; + } + for (i = 0; i < ARRAY_SIZE(bt_mesh_gattc_info); i++) { + if (bt_mesh_gattc_info[i].service_uuid == (uint16_t)BLE_UUID16(uuid)->value) { + bt_mesh_gattc_info[i].start_handle = service->start_handle; + bt_mesh_gattc_info[i].end_handle = service->end_handle; + break; + } + } + + break; + case BLE_HS_EDONE: + for (i = 0; i < ARRAY_SIZE(bt_mesh_gattc_info); i++) { + if (bt_mesh_gattc_info[i].conn.handle == conn_handle) { + break; + } + } + + if (i == ARRAY_SIZE(bt_mesh_gattc_info)) { + BT_ERR("%s, Conn handle is not found", __func__); + return 0; + } + conn = &bt_mesh_gattc_info[i].conn; + if (bt_mesh_gattc_info[i].start_handle == 0x00 || + bt_mesh_gattc_info[i].end_handle == 0x00 || + (bt_mesh_gattc_info[i].start_handle > bt_mesh_gattc_info[i].end_handle)) { + bt_mesh_gattc_disconnect(conn); + return 0; + } + + /* Get the characteristic num within Mesh Provisioning/Proxy Service */ + ble_gattc_disc_all_chrs(conn_handle, bt_mesh_gattc_info[i].start_handle, bt_mesh_gattc_info[i].end_handle, + chr_disced, (void *)i); + break; + + default: + rc = error->status; + break; + } + + return rc; +} + + +#endif /* (CONFIG_BLE_MESH_PROVISIONER && CONFIG_BLE_MESH_PB_GATT) || CONFIG_BLE_MESH_GATT_PROXY_CLIENT */ + +static int disc_cb(struct ble_gap_event *event, void *arg) +{ + struct ble_gap_disc_desc *desc; + +#if (CONFIG_BLE_MESH_PROVISIONER && CONFIG_BLE_MESH_PB_GATT) || \ + CONFIG_BLE_MESH_GATT_PROXY_CLIENT + int rc, i; + uint8_t notif_data[100]; + uint16_t notif_len; + ssize_t len; + struct ble_gap_conn_desc conn_desc; + struct bt_mesh_conn *conn = NULL; +#endif + + switch (event->type) { + case BLE_GAP_EVENT_DISC: + desc = &event->disc; + + struct net_buf_simple *buf = bt_mesh_alloc_buf(desc->length_data); + if (!buf) { + BT_ERR("%s, Failed to allocate memory", __func__); + return 0; + } + net_buf_simple_add_mem(buf, desc->data, desc->length_data); + + if (bt_mesh_scan_dev_found_cb) { + bt_mesh_scan_dev_found_cb((bt_mesh_addr_t *)&desc->addr, desc->rssi, desc->event_type, buf); + } + bt_mesh_free(buf); + break; +#if (CONFIG_BLE_MESH_PROVISIONER && CONFIG_BLE_MESH_PB_GATT) || \ + CONFIG_BLE_MESH_GATT_PROXY_CLIENT + case BLE_GAP_EVENT_CONNECT: + if (event->connect.status == 0) { + /* Connection successfully established. */ + MODLOG_DFLT(INFO, "Connection established "); + + rc = ble_gap_conn_find(event->connect.conn_handle, &conn_desc); + assert(rc == 0); + + if (bt_mesh_gattc_conn_cb != NULL && bt_mesh_gattc_conn_cb->connected != NULL) { + for (i = 0; i < ARRAY_SIZE(bt_mesh_gattc_info); i++) { + if (!memcmp(bt_mesh_gattc_info[i].addr.val, conn_desc.peer_id_addr.val, BLE_MESH_ADDR_LEN)) { + bt_mesh_gattc_info[i].conn.handle = event->connect.conn_handle; + (bt_mesh_gattc_conn_cb->connected)(&bt_mesh_gattc_info[i].addr, &bt_mesh_gattc_info[i].conn, i); + break; + } + } + } + } +#if BLE_MESH_DEV + if (!bt_mesh_atomic_test_bit(bt_mesh_dev.flags, BLE_MESH_DEV_SCANNING)) { + rc = ble_gap_disc(BLE_OWN_ADDR_PUBLIC, BLE_HS_FOREVER, &scan_param, disc_cb, NULL); + if (rc != 0) { + BT_ERR("%s, Invalid status %d", __func__, rc); + break; + } + bt_mesh_atomic_set_bit(bt_mesh_dev.flags, BLE_MESH_DEV_SCANNING); + } +#else + rc = ble_gap_disc(BLE_OWN_ADDR_PUBLIC, BLE_HS_FOREVER, &scan_param, disc_cb, NULL); + if (rc != 0) { + BT_ERR("%s, Invalid status %d", __func__, rc); + break; + } +#endif /* BLE_MESH_DEV */ + break; + case BLE_GAP_EVENT_DISCONNECT: + if (bt_mesh_gattc_conn_cb != NULL && bt_mesh_gattc_conn_cb->disconnected != NULL) { + for (i = 0; i < ARRAY_SIZE(bt_mesh_gattc_info); i++) { + memcpy(&conn_desc, &event->disconnect.conn, sizeof(conn_desc)); + if (!memcmp(bt_mesh_gattc_info[i].addr.val, conn_desc.peer_ota_addr.val, BLE_MESH_ADDR_LEN)) { + if (bt_mesh_gattc_info[i].conn.handle == event->disconnect.conn.conn_handle) { + (bt_mesh_gattc_conn_cb->disconnected)(&bt_mesh_gattc_info[i].addr, &bt_mesh_gattc_info[i].conn, event->disconnect.reason); + if (!bt_mesh_gattc_info[i].wr_desc_done) { + /* Add this in case connection is established, connected event comes, but + * connection is terminated before server->filter_type is set to PROV. + */ +#if CONFIG_BLE_MESH_PROVISIONER && CONFIG_BLE_MESH_PB_GATT + if (bt_mesh_gattc_info[i].service_uuid == BLE_MESH_UUID_MESH_PROV_VAL) { + bt_mesh_provisioner_clear_link_info(bt_mesh_gattc_info[i].addr.val); + } +#endif + } + } else { + /* Add this in case connection is failed to be established, and here we + * need to clear some provision link info, like connecting flag, device + * uuid, address info, etc. + */ +#if CONFIG_BLE_MESH_PROVISIONER && CONFIG_BLE_MESH_PB_GATT + if (bt_mesh_gattc_info[i].service_uuid == BLE_MESH_UUID_MESH_PROV_VAL) { + bt_mesh_provisioner_clear_link_info(bt_mesh_gattc_info[i].addr.val); + } +#endif + } +#if CONFIG_BLE_MESH_PROVISIONER && CONFIG_BLE_MESH_PB_GATT + if (bt_mesh_gattc_info[i].service_uuid == BLE_MESH_UUID_MESH_PROV_VAL) { + /* Decrease prov pbg_count */ + bt_mesh_provisioner_pbg_count_dec(); + } +#endif + /* Reset corresponding gattc info */ + memset(&bt_mesh_gattc_info[i], 0, sizeof(bt_mesh_gattc_info[i])); + bt_mesh_gattc_info[i].conn.handle = 0xFFFF; + bt_mesh_gattc_info[i].mtu = BLE_ATT_MTU_DFLT; + bt_mesh_gattc_info[i].wr_desc_done = false; + break; + } + } + } + break; + case BLE_GAP_EVENT_MTU: + for (i = 0; i < ARRAY_SIZE(bt_mesh_gattc_info); i++) { + if (bt_mesh_gattc_info[i].conn.handle == event->mtu.conn_handle) { + bt_mesh_gattc_info[i].mtu = event->mtu.value; + break; + } + } + /** Once mtu exchanged accomplished, start to find services, and here + * need a flag to indicate which service to find(Mesh Prov Service or + * Mesh Proxy Service) + */ + ble_uuid_any_t bt_uuid; + if (i != ARRAY_SIZE(bt_mesh_gattc_info)) { + //service_uuid.len = sizeof(bt_mesh_gattc_info[i].service_uuid); + if (sizeof(bt_mesh_gattc_info[i].service_uuid) == 0x02) { + bt_uuid.u16.u.type = BLE_UUID_TYPE_16; + bt_uuid.u16.value = bt_mesh_gattc_info[i].service_uuid; + + } else if (sizeof(bt_mesh_gattc_info[i].service_uuid) == 0x10) { + bt_uuid.u128.u.type = BLE_UUID_TYPE_128; + memcpy(bt_uuid.u128.value, &bt_mesh_gattc_info[i].service_uuid, 16); + } + /* Search Mesh Provisioning Service or Mesh Proxy Service */ + ble_gattc_disc_all_svcs(bt_mesh_gattc_info[i].conn.handle, svc_disced, NULL); + } + break; + case BLE_GAP_EVENT_NOTIFY_RX: + for (i = 0; i < ARRAY_SIZE(bt_mesh_gattc_info); i++) { + if (bt_mesh_gattc_info[i].conn.handle == event->notify_rx.conn_handle) { + break; + } + } + + if (i == ARRAY_SIZE(bt_mesh_gattc_info)) { + BT_ERR("%s, Conn handle is not found", __func__); + return 0; + } + + conn = &bt_mesh_gattc_info[i].conn; + ble_gap_conn_find(event->notify_rx.conn_handle, &conn_desc); + + if (bt_mesh_gattc_info[i].data_out_handle != event->notify_rx.attr_handle) { + /* Data isn't populated yet */ + return 0; + } + + if (memcmp(bt_mesh_gattc_info[i].addr.val, conn_desc.peer_id_addr.val, BLE_MESH_ADDR_LEN) || + (bt_mesh_gattc_info[i].data_out_handle != event->notify_rx.attr_handle) || + (event->notify_rx.indication != 0)) { + BT_ERR("%s, Notification error", __func__); + bt_mesh_gattc_disconnect(conn); + return 0; + } + + notif_len = OS_MBUF_PKTLEN(event->notify_rx.om); + rc = os_mbuf_copydata(event->notify_rx.om, 0, notif_len, notif_data); + + if (bt_mesh_gattc_info[i].service_uuid == BLE_MESH_UUID_MESH_PROV_VAL) { + if (bt_mesh_gattc_conn_cb != NULL && bt_mesh_gattc_conn_cb->prov_notify != NULL) { + len = bt_mesh_gattc_conn_cb->prov_notify(&bt_mesh_gattc_info[i].conn, + notif_data, notif_len); + if (len < 0) { + BT_ERR("%s, prov_notify failed", __func__); + bt_mesh_gattc_disconnect(conn); + return 0; + } + } + } else if (bt_mesh_gattc_info[i].service_uuid == BLE_MESH_UUID_MESH_PROXY_VAL) { + if (bt_mesh_gattc_conn_cb != NULL && bt_mesh_gattc_conn_cb->proxy_notify != NULL) { + len = bt_mesh_gattc_conn_cb->proxy_notify(&bt_mesh_gattc_info[i].conn, + notif_data, notif_len); + if (len < 0) { + BT_ERR("%s, proxy_notify failed", __func__); + bt_mesh_gattc_disconnect(conn); + return 0; + } + } + } + break; +#endif + default: + break; + } + + return 0; +} + +static int start_le_scan(u8_t scan_type, u16_t interval, u16_t window, u8_t filter_dup) +{ + + scan_param.filter_duplicates = filter_dup; + scan_param.itvl = interval; + scan_param.window = window; + + if (scan_type == BLE_MESH_SCAN_PASSIVE) { + scan_param.passive = 1; + } else { + scan_param.passive = 0; + } + ble_gap_disc(BLE_OWN_ADDR_PUBLIC, BLE_HS_FOREVER, &scan_param, disc_cb, NULL); + +#if BLE_MESH_DEV + if (scan_type == BLE_MESH_SCAN_ACTIVE) { + bt_mesh_atomic_set_bit(bt_mesh_dev.flags, BLE_MESH_DEV_ACTIVE_SCAN); + } else { + bt_mesh_atomic_clear_bit(bt_mesh_dev.flags, BLE_MESH_DEV_ACTIVE_SCAN); + } +#endif + + return 0; +} + +static int set_ad(const struct bt_mesh_adv_data *ad, size_t ad_len, u8_t *buf, u8_t *buf_len) +{ + int i; + + for (i = 0; i < ad_len; i++) { + buf[(*buf_len)++] = ad[i].data_len + 1; + buf[(*buf_len)++] = ad[i].type; + + memcpy(&buf[*buf_len], ad[i].data, + ad[i].data_len); + *buf_len += ad[i].data_len; + } + + return 0; +} + +#if defined(CONFIG_BLE_MESH_NODE) && CONFIG_BLE_MESH_NODE +static struct bt_mesh_gatt_attr *bt_mesh_gatts_find_attr_by_handle(u16_t handle); + +static int gap_event_cb(struct ble_gap_event *event, void *arg) +{ + struct ble_gap_conn_desc desc; + int rc; + + switch (event->type) { + case BLE_GAP_EVENT_CONNECT: + /* A new connection was established or a connection attempt failed. */ + MODLOG_DFLT(INFO, "connection %s; status=%d ", + event->connect.status == 0 ? "established" : "failed", + event->connect.status); + if (event->connect.status == 0) { + rc = ble_gap_conn_find(event->connect.conn_handle, &desc); + assert(rc == 0); + } + MODLOG_DFLT(INFO, "\n"); +#if BLE_MESH_DEV + /* When connection is created, advertising will be stopped automatically. */ + bt_mesh_atomic_test_and_clear_bit(bt_mesh_dev.flags, BLE_MESH_DEV_ADVERTISING); +#endif + if (bt_mesh_gatts_conn_cb != NULL && bt_mesh_gatts_conn_cb->connected != NULL) { + u8_t index = BLE_MESH_GATT_GET_CONN_ID(event->connect.conn_handle); + if (index < BLE_MESH_MAX_CONN) { + bt_mesh_gatts_conn[index].handle = BLE_MESH_GATT_GET_CONN_ID(event->connect.conn_handle); + (bt_mesh_gatts_conn_cb->connected)(&bt_mesh_gatts_conn[index], 0); + } + memcpy(bt_mesh_gatts_addr, desc.peer_id_addr.val, BLE_MESH_ADDR_LEN); + /* This is for EspBleMesh Android app. When it tries to connect with the + * device at the first time and it fails due to some reason. And after + * the second connection, the device needs to send GATT service change + * indication to the phone manually to notify it dicovering service again. + */ + ble_svc_gatt_changed(prov_svc_start_handle, 0xffff); + + } + + return 0; + + case BLE_GAP_EVENT_DISCONNECT: + MODLOG_DFLT(INFO, "disconnect; reason=%d ", event->disconnect.reason); + MODLOG_DFLT(INFO, "\n"); +#if BLE_MESH_DEV + bt_mesh_atomic_test_and_clear_bit(bt_mesh_dev.flags, BLE_MESH_DEV_ADVERTISING); +#endif + if (bt_mesh_gatts_conn_cb != NULL && bt_mesh_gatts_conn_cb->disconnected != NULL) { + u8_t index = BLE_MESH_GATT_GET_CONN_ID(event->disconnect.conn.conn_handle); + if (index < BLE_MESH_MAX_CONN) { + bt_mesh_gatts_conn[index].handle = BLE_MESH_GATT_GET_CONN_ID(event->disconnect.conn.conn_handle); + (bt_mesh_gatts_conn_cb->disconnected)(&bt_mesh_gatts_conn[index], event->disconnect.reason); + } + memset(bt_mesh_gatts_addr, 0x0, BLE_MESH_ADDR_LEN); + } + + return 0; + + case BLE_GAP_EVENT_CONN_UPDATE: + /* The central has updated the connection parameters. */ + MODLOG_DFLT(INFO, "connection updated; status=%d ", + event->conn_update.status); + rc = ble_gap_conn_find(event->connect.conn_handle, &desc); + assert(rc == 0); + MODLOG_DFLT(INFO, "\n"); + return 0; + + case BLE_GAP_EVENT_ADV_COMPLETE: + MODLOG_DFLT(INFO, "advertise complete; reason=%d", + event->adv_complete.reason); + return 0; + + case BLE_GAP_EVENT_ENC_CHANGE: + /* Encryption has been enabled or disabled for this connection. */ + MODLOG_DFLT(INFO, "encryption change event; status=%d ", + event->enc_change.status); + rc = ble_gap_conn_find(event->connect.conn_handle, &desc); + assert(rc == 0); + MODLOG_DFLT(INFO, "\n"); + return 0; + + case BLE_GAP_EVENT_SUBSCRIBE: + MODLOG_DFLT(INFO, "subscribe event; conn_handle=%d attr_handle=%d " + "reason=%d prevn=%d curn=%d previ=%d curi=%d\n", + event->subscribe.conn_handle, + event->subscribe.attr_handle, + event->subscribe.reason, + event->subscribe.prev_notify, + event->subscribe.cur_notify, + event->subscribe.prev_indicate, + event->subscribe.cur_indicate); + struct bt_mesh_gatt_attr *attr = bt_mesh_gatts_find_attr_by_handle(event->subscribe.attr_handle + 1); + u8_t index = BLE_MESH_GATT_GET_CONN_ID(event->subscribe.conn_handle); + u16_t len = 0; + uint16_t ccc_val = 0; + + if (event->subscribe.prev_notify != event->subscribe.cur_notify) { + ccc_val = event->subscribe.cur_notify; + } else if (event->subscribe.prev_indicate != event->subscribe.cur_indicate) { + if (event->subscribe.cur_indicate) { + ccc_val = 2; + } else { + ccc_val = 0; + } + } + + if (attr != NULL && attr->write != NULL) { + if ((len = attr->write(&bt_mesh_gatts_conn[index], attr, + &ccc_val, + sizeof(ccc_val), + 0 /* offset */, 0)) > 0) { + } + } + + return 0; + + case BLE_GAP_EVENT_MTU: + MODLOG_DFLT(INFO, "mtu update event; conn_handle=%d cid=%d mtu=%d\n", + event->mtu.conn_handle, + event->mtu.channel_id, + event->mtu.value); + return 0; + + case BLE_GAP_EVENT_REPEAT_PAIRING: + /* We already have a bond with the peer, but it is attempting to + * establish a new secure link. This app sacrifices security for + * convenience: just throw away the old bond and accept the new link. + */ + + /* Delete the old bond. */ + rc = ble_gap_conn_find(event->repeat_pairing.conn_handle, &desc); + assert(rc == 0); + ble_store_util_delete_peer(&desc.peer_id_addr); + + /* Return BLE_GAP_REPEAT_PAIRING_RETRY to indicate that the host should + * continue with the pairing operation. + */ + return BLE_GAP_REPEAT_PAIRING_RETRY; + + case BLE_GAP_EVENT_PASSKEY_ACTION: + MODLOG_DFLT(INFO, "PASSKEY_ACTION_EVENT started \n"); + return 0; + } + + return 0; +} +#else + +static int gap_event_cb(struct ble_gap_event *event, void *arg) +{ + return 0; +} +#endif + +/* APIs functions */ +int bt_le_adv_start(const struct bt_mesh_adv_param *param, + const struct bt_mesh_adv_data *ad, size_t ad_len, + const struct bt_mesh_adv_data *sd, size_t sd_len) +{ +#if BLE_MESH_DEV + if (bt_mesh_atomic_test_bit(bt_mesh_dev.flags, BLE_MESH_DEV_ADVERTISING)) { + return -EALREADY; + } +#endif + uint8_t buf[BLE_HS_ADV_MAX_SZ]; + uint8_t buf_len = 0; + int err; + struct ble_gap_adv_params adv_params; + + err = set_ad(ad, ad_len, buf, &buf_len); + if (err) { + BT_ERR("set_ad failed: err %d", err); + return err; + } + + err = ble_gap_adv_set_data(buf, buf_len); + if (err != 0) { + BT_ERR("Advertising set failed: err %d", err); + return err; + } + + if (sd && (param->options & BLE_MESH_ADV_OPT_CONNECTABLE)) { + buf_len = 0; + + err = set_ad(sd, sd_len, buf, &buf_len); + if (err) { + BT_ERR("set_ad failed: err %d", err); + return err; + } + + err = ble_gap_adv_rsp_set_data(buf, buf_len); + if (err != 0) { + BT_ERR("Scan rsp failed: err %d", err); + return err; + } + } + + memset(&adv_params, 0, sizeof adv_params); + adv_params.itvl_min = param->interval_min; + adv_params.itvl_max = param->interval_max; + + if (param->options & BLE_MESH_ADV_OPT_CONNECTABLE) { + adv_params.conn_mode = BLE_GAP_CONN_MODE_UND; + adv_params.disc_mode = BLE_GAP_DISC_MODE_GEN; + } else if (sd != NULL) { + adv_params.conn_mode = BLE_GAP_CONN_MODE_NON; + adv_params.disc_mode = BLE_GAP_DISC_MODE_GEN; + } else { + adv_params.conn_mode = BLE_GAP_CONN_MODE_NON; + adv_params.disc_mode = BLE_GAP_DISC_MODE_NON; + } + + err = ble_gap_adv_start(BLE_OWN_ADDR_PUBLIC, NULL, BLE_HS_FOREVER, &adv_params, + gap_event_cb, NULL); + if (err) { + BT_ERR("Advertising start failed: err %d", err); + return err; + } + +#if BLE_MESH_DEV + bt_mesh_atomic_set_bit(bt_mesh_dev.flags, BLE_MESH_DEV_ADVERTISING); + + if (!(param->options & BLE_MESH_ADV_OPT_ONE_TIME)) { + bt_mesh_atomic_set_bit(bt_mesh_dev.flags, BLE_MESH_DEV_KEEP_ADVERTISING); + } +#endif + + return 0; +} + +int bt_le_adv_stop(void) +{ +#if BLE_MESH_DEV + bt_mesh_atomic_clear_bit(bt_mesh_dev.flags, BLE_MESH_DEV_KEEP_ADVERTISING); + if (!bt_mesh_atomic_test_bit(bt_mesh_dev.flags, BLE_MESH_DEV_ADVERTISING)) { + return 0; + } +#endif + ble_gap_adv_stop(); + +#if BLE_MESH_DEV + bt_mesh_atomic_clear_bit(bt_mesh_dev.flags, BLE_MESH_DEV_ADVERTISING); +#endif + + return 0; +} + +int bt_le_scan_start(const struct bt_mesh_scan_param *param, bt_mesh_scan_cb_t cb) +{ + int err; + +#if BLE_MESH_DEV + if (bt_mesh_atomic_test_bit(bt_mesh_dev.flags, BLE_MESH_DEV_SCANNING)) { + return -EALREADY; + } +#endif + +#if BLE_MESH_DEV + if (param->filter_dup) { + bt_mesh_atomic_set_bit(bt_mesh_dev.flags, BLE_MESH_DEV_SCAN_FILTER_DUP); + } else { + bt_mesh_atomic_clear_bit(bt_mesh_dev.flags, BLE_MESH_DEV_SCAN_FILTER_DUP); + } +#endif + + err = start_le_scan(param->type, param->interval, param->window, param->filter_dup); + if (err) { + return err; + } + +#if BLE_MESH_DEV + bt_mesh_atomic_set_bit(bt_mesh_dev.flags, BLE_MESH_DEV_SCANNING); +#endif + + bt_mesh_scan_dev_found_cb = cb; + return err; +} + +int bt_le_scan_stop(void) +{ +#if BLE_MESH_DEV + if (bt_mesh_atomic_test_bit(bt_mesh_dev.flags, BLE_MESH_DEV_SCANNING)) { + bt_mesh_atomic_clear_bit(bt_mesh_dev.flags, BLE_MESH_DEV_SCANNING); + ble_gap_disc_cancel(); + } +#else + ble_gap_disc_cancel(); +#endif + + bt_mesh_scan_dev_found_cb = NULL; + return 0; +} +#if defined(CONFIG_BLE_MESH_NODE) && CONFIG_BLE_MESH_NODE + +void bt_mesh_gatts_conn_cb_register(struct bt_mesh_conn_cb *cb) +{ + bt_mesh_gatts_conn_cb = cb; +} + +static struct bt_mesh_gatt_attr *bt_mesh_gatts_find_attr_by_handle(u16_t handle) +{ + struct bt_mesh_gatt_service *svc = NULL; + struct bt_mesh_gatt_attr *attr = NULL; + + SYS_SLIST_FOR_EACH_CONTAINER(&bt_mesh_gatts_db, svc, node) { + int i; + + for (i = 0; i < svc->attr_count; i++) { + attr = &svc->attrs[i]; + /* Check the attrs handle is equal to the handle or not */ + if (attr->handle == handle) { + return attr; + } + } + } + + return NULL; +} + +static void bt_mesh_gatts_foreach_attr(u16_t start_handle, u16_t end_handle, + bt_mesh_gatt_attr_func_t func, void *user_data) +{ + struct bt_mesh_gatt_service *svc = NULL; + + SYS_SLIST_FOR_EACH_CONTAINER(&bt_mesh_gatts_db, svc, node) { + int i; + + for (i = 0; i < svc->attr_count; i++) { + struct bt_mesh_gatt_attr *attr = &svc->attrs[i]; + + /* Check if attribute handle is within range */ + if (attr->handle < start_handle || + attr->handle > end_handle) { + continue; + } + + if (func(attr, user_data) == BLE_MESH_GATT_ITER_STOP) { + return; + } + } + } +} + +static u8_t find_next(const struct bt_mesh_gatt_attr *attr, void *user_data) +{ + struct bt_mesh_gatt_attr **next = user_data; + + *next = (struct bt_mesh_gatt_attr *)attr; + + return BLE_MESH_GATT_ITER_STOP; +} + +static struct bt_mesh_gatt_attr *bt_mesh_gatts_attr_next(const struct bt_mesh_gatt_attr *attr) +{ + struct bt_mesh_gatt_attr *next = NULL; + + bt_mesh_gatts_foreach_attr(attr->handle + 1, attr->handle + 1, find_next, &next); + + return next; +} + +ssize_t bt_mesh_gatts_attr_read(struct bt_mesh_conn *conn, const struct bt_mesh_gatt_attr *attr, + void *buf, u16_t buf_len, u16_t offset, + const void *value, u16_t value_len) +{ + u16_t len; + + if (offset > value_len) { + return BLE_MESH_GATT_ERR(BLE_MESH_ATT_ERR_INVALID_OFFSET); + } + + len = MIN(buf_len, value_len - offset); + + BT_DBG("handle 0x%04x offset %u length %u", attr->handle, offset, len); + + memcpy(buf, value + offset, len); + + return len; +} + +struct gatts_incl { + u16_t start_handle; + u16_t end_handle; + u16_t uuid16; +} __packed; + +ssize_t bt_mesh_gatts_attr_read_included(struct bt_mesh_conn *conn, + const struct bt_mesh_gatt_attr *attr, + void *buf, u16_t len, u16_t offset) +{ + struct bt_mesh_gatt_attr *incl = attr->user_data; + struct bt_mesh_uuid *uuid = incl->user_data; + struct gatts_incl pdu = {0}; + u8_t value_len; + + /* First attr points to the start handle */ + pdu.start_handle = sys_cpu_to_le16(incl->handle); + value_len = sizeof(pdu.start_handle) + sizeof(pdu.end_handle); + + /* + * Core 4.2, Vol 3, Part G, 3.2, + * The Service UUID shall only be present when the UUID is a 16-bit Bluetooth UUID. + */ + if (uuid->type == BLE_MESH_UUID_TYPE_16) { + pdu.uuid16 = sys_cpu_to_le16(BLE_MESH_UUID_16(uuid)->val); + value_len += sizeof(pdu.uuid16); + } + + return bt_mesh_gatts_attr_read(conn, attr, buf, len, offset, &pdu, value_len); +} + +ssize_t bt_mesh_gatts_attr_read_service(struct bt_mesh_conn *conn, + const struct bt_mesh_gatt_attr *attr, + void *buf, u16_t len, u16_t offset) +{ + struct bt_mesh_uuid *uuid = attr->user_data; + + if (uuid->type == BLE_MESH_UUID_TYPE_16) { + u16_t uuid16 = sys_cpu_to_le16(BLE_MESH_UUID_16(uuid)->val); + + return bt_mesh_gatts_attr_read(conn, attr, buf, len, offset, &uuid16, 2); + } + + return bt_mesh_gatts_attr_read(conn, attr, buf, len, offset, + BLE_MESH_UUID_128(uuid)->val, 16); +} + +struct gatts_chrc { + u8_t properties; + u16_t value_handle; + union { + u16_t uuid16; + u8_t uuid[16]; + }; +} __packed; + +ssize_t bt_mesh_gatts_attr_read_chrc(struct bt_mesh_conn *conn, + const struct bt_mesh_gatt_attr *attr, void *buf, + u16_t len, u16_t offset) +{ + struct bt_mesh_gatt_char *chrc = attr->user_data; + const struct bt_mesh_gatt_attr *next = NULL; + struct gatts_chrc pdu = {0}; + u8_t value_len; + + pdu.properties = chrc->properties; + /* BLUETOOTH SPECIFICATION Version 4.2 [Vol 3, Part G] page 534: + * 3.3.2 Characteristic Value Declaration + * The Characteristic Value declaration contains the value of the + * characteristic. It is the first Attribute after the characteristic + * declaration. All characteristic definitions shall have a + * Characteristic Value declaration. + */ + next = bt_mesh_gatts_attr_next(attr); + if (!next) { + BT_WARN("%s, No value for characteristic at 0x%04x", __func__, attr->handle); + pdu.value_handle = 0x0000; + } else { + pdu.value_handle = sys_cpu_to_le16(next->handle); + } + value_len = sizeof(pdu.properties) + sizeof(pdu.value_handle); + + if (chrc->uuid->type == BLE_MESH_UUID_TYPE_16) { + pdu.uuid16 = sys_cpu_to_le16(BLE_MESH_UUID_16(chrc->uuid)->val); + value_len += 2; + } else { + memcpy(pdu.uuid, BLE_MESH_UUID_128(chrc->uuid)->val, 16); + value_len += 16; + } + + return bt_mesh_gatts_attr_read(conn, attr, buf, len, offset, &pdu, value_len); +} + +static int gatts_register(struct bt_mesh_gatt_service *svc) +{ + struct bt_mesh_gatt_service *last; + u16_t handle; + + if (sys_slist_is_empty(&bt_mesh_gatts_db)) { + handle = 0; + goto populate; + } + + last = SYS_SLIST_PEEK_TAIL_CONTAINER(&bt_mesh_gatts_db, last, node); + handle = last->attrs[last->attr_count - 1].handle; + BT_DBG("%s, handle = %d", __func__, handle); + +populate: + sys_slist_append(&bt_mesh_gatts_db, &svc->node); + return 0; +} + +int bt_mesh_gatts_service_register(struct bt_mesh_gatt_service *svc) +{ + uint16_t offset = 0; + int i; + if (BLE_MESH_UUID_16(svc->attrs[0].user_data)->val == BT_UUID_MESH_PROXY_VAL) { + offset = proxy_svc_start_handle; + } else if (BLE_MESH_UUID_16(svc->attrs[0].user_data)->val == BT_UUID_MESH_PROV_VAL) { + offset = prov_svc_start_handle; + } + + for (i = 0; i < svc->attr_count; i++) { + svc->attrs[i].handle = offset + i; + } + gatts_register(svc); + return 0; +} + +int bt_mesh_gatts_disconnect(struct bt_mesh_conn *conn, u8_t reason) +{ + u16_t conn_id = BLE_MESH_GATT_CREATE_CONN_ID(conn->handle); + ble_gap_terminate(conn_id, reason); + return 0; +} + +int bt_mesh_gatts_service_unregister(struct bt_mesh_gatt_service *svc) +{ + assert(svc != NULL); + BT_ERR("%s, Unsupported for NimBLE host", __func__); + return 0; +} + +int bt_mesh_gatts_notify(struct bt_mesh_conn *conn, const struct bt_mesh_gatt_attr *attr, + const void *data, u16_t len) +{ + struct os_mbuf *om; + u16_t conn_id = BLE_MESH_GATT_CREATE_CONN_ID(conn->handle); + + om = ble_hs_mbuf_from_flat(data, len); + assert(om); + ble_gattc_notify_custom(conn_id, attr->handle, om); + + return 0; +} + +u16_t bt_mesh_gatt_get_mtu(struct bt_mesh_conn *conn) +{ + return ble_att_preferred_mtu(); +} + +/* APIs added by Espressif */ +int bt_mesh_gatts_service_stop(struct bt_mesh_gatt_service *svc) +{ + int rc; + uint16_t handle; + if (!svc) { + BT_ERR("%s, Invalid parameter", __func__); + return -EINVAL; + } + const ble_uuid_t *uuid; + if (BLE_MESH_UUID_16(svc->attrs[0].user_data)->val == BT_UUID_MESH_PROXY_VAL) { + uuid = BLE_UUID16_DECLARE(BT_UUID_MESH_PROXY_VAL); + } else { + uuid = BLE_UUID16_DECLARE(BT_UUID_MESH_PROV_VAL); + } + + rc = ble_gatts_find_svc(uuid, &handle); + assert(rc == 0); + ble_gatts_svc_set_visibility(handle, 0); + + /* FIXME: figure out end handle */ + ble_svc_gatt_changed(handle, 0xffff); + + return 0; +} + +int bt_mesh_gatts_service_start(struct bt_mesh_gatt_service *svc) +{ + int rc; + uint16_t handle; + const ble_uuid_t *uuid; + if (BLE_MESH_UUID_16(svc->attrs[0].user_data)->val == BT_UUID_MESH_PROXY_VAL) { + uuid = BLE_UUID16_DECLARE(BT_UUID_MESH_PROXY_VAL); + } else { + uuid = BLE_UUID16_DECLARE(BT_UUID_MESH_PROV_VAL); + } + + rc = ble_gatts_find_svc(uuid, &handle); + assert(rc == 0); + ble_gatts_svc_set_visibility(handle, 1); + + /* FIXME: figure out end handle */ + ble_svc_gatt_changed(handle, 0xffff); + + return 0; +} + +int bt_mesh_gatts_set_local_device_name(const char *name) +{ + return ble_svc_gap_device_name_set(name); +} +#endif /* defined(CONFIG_BLE_MESH_NODE) && CONFIG_BLE_MESH_NODE */ + +#if (CONFIG_BLE_MESH_PROVISIONER && CONFIG_BLE_MESH_PB_GATT) || \ + CONFIG_BLE_MESH_GATT_PROXY_CLIENT +void bt_mesh_gattc_conn_cb_register(struct bt_mesh_prov_conn_cb *cb) +{ + bt_mesh_gattc_conn_cb = cb; +} + +u8_t bt_mesh_gattc_get_free_conn_count(void) +{ + u8_t count = 0; + u8_t i; + + for (i = 0U; i < ARRAY_SIZE(bt_mesh_gattc_info); i++) { + if (bt_mesh_gattc_info[i].conn.handle == 0xFFFF && + bt_mesh_gattc_info[i].service_uuid == 0x0000) { + ++count; + } + } + + return count; +} + +u16_t bt_mesh_gattc_get_service_uuid(struct bt_mesh_conn *conn) +{ + int i; + + for (i = 0; i < ARRAY_SIZE(bt_mesh_gattc_info); i++) { + if (conn == &bt_mesh_gattc_info[i].conn) { + break; + } + } + + if (i == ARRAY_SIZE(bt_mesh_gattc_info)) { + return 0; + } + + return bt_mesh_gattc_info[i].service_uuid; +} + +/** For provisioner acting as a GATT client, it may follow the procedures + * listed below. + * 1. Create connection with the unprovisioned device + * 2. Exchange MTU size + * 3. Find Mesh Prov Service in the device's service database + * 4. Find Mesh Prov Data In/Out characteristic within the service + * 5. Get CCC of Mesh Prov Data Out Characteristic + * 6. Set the Notification bit of CCC + */ + +int bt_mesh_gattc_conn_create(const bt_mesh_addr_t *addr, u16_t service_uuid) +{ + u8_t zero[6] = {0}; + int i, rc; + + if (!addr || !memcmp(addr->val, zero, BLE_MESH_ADDR_LEN) || + (addr->type > BLE_ADDR_RANDOM)) { + BT_ERR("%s, Invalid remote address", __func__); + return -EINVAL; + } + + if (service_uuid != BLE_MESH_UUID_MESH_PROV_VAL && + service_uuid != BLE_MESH_UUID_MESH_PROXY_VAL) { + BT_ERR("%s, Invalid service uuid 0x%04x", __func__, service_uuid); + return -EINVAL; + } + + /* Check if already creating connection with the device */ + for (i = 0; i < ARRAY_SIZE(bt_mesh_gattc_info); i++) { + if (!memcmp(bt_mesh_gattc_info[i].addr.val, addr->val, BLE_MESH_ADDR_LEN)) { + BT_WARN("%s, Already create connection with %s", + __func__, bt_hex(addr->val, BLE_MESH_ADDR_LEN)); + return -EALREADY; + } + } + + /* Find empty element in queue to store device info */ + for (i = 0; i < ARRAY_SIZE(bt_mesh_gattc_info); i++) { + if ((bt_mesh_gattc_info[i].conn.handle == 0xFFFF) && + (bt_mesh_gattc_info[i].service_uuid == 0x0000)) { + memcpy(bt_mesh_gattc_info[i].addr.val, addr->val, BLE_MESH_ADDR_LEN); + bt_mesh_gattc_info[i].addr.type = addr->type; + /* Service to be found after exhanging mtu size */ + bt_mesh_gattc_info[i].service_uuid = service_uuid; + break; + } + } + + if (i == ARRAY_SIZE(bt_mesh_gattc_info)) { + BT_WARN("%s, gattc info is full", __func__); + return -ENOMEM; + } + +#if BLE_MESH_DEV + if (bt_mesh_atomic_test_and_clear_bit(bt_mesh_dev.flags, BLE_MESH_DEV_SCANNING)) { + rc = ble_gap_disc_cancel(); + if (rc != 0) { + return -1; + } + } +#else + rc = ble_gap_disc_cancel(); + if (rc != 0) { + return -1; + } +#endif /* BLE_MESH_DEV */ + + BT_DBG("%s, create conn with %s", __func__, bt_hex(addr->val, BLE_MESH_ADDR_LEN)); + + /* Min_interval: 250ms + * Max_interval: 250ms + * Slave_latency: 0x0 + * Supervision_timeout: 32 sec + */ + struct ble_gap_conn_params conn_params = {0}; + conn_params.itvl_min = 0xC8; /* (250 * 1000) / 1250 = 200 = 0xC8 */ + conn_params.itvl_max = 0xC8; /* (250 * 1000) / 1250 = 200 = 0xC8 */ + conn_params.latency = 0; + conn_params.supervision_timeout = 0xC80; + conn_params.scan_itvl = 0x0020; //0x0010 + conn_params.scan_window = 0x0020; //0x0010 + conn_params.min_ce_len = BLE_GAP_INITIAL_CONN_MIN_CE_LEN; + conn_params.max_ce_len = BLE_GAP_INITIAL_CONN_MAX_CE_LEN; + + + ble_addr_t peer_addr; + memcpy(peer_addr.val, addr->val, 6); + peer_addr.type = addr->type; + + rc = ble_gap_connect(BLE_OWN_ADDR_PUBLIC, &peer_addr, BLE_HS_FOREVER, &conn_params, + disc_cb, NULL); + + return i; +} + +static int mtu_cb(uint16_t conn_handle, + const struct ble_gatt_error *error, + uint16_t mtu, void *arg) +{ + int i; + if (error->status == 0) { + + for (i = 0; i < ARRAY_SIZE(bt_mesh_gattc_info); i++) { + if (bt_mesh_gattc_info[i].conn.handle == conn_handle) { + bt_mesh_gattc_info[i].mtu = mtu; + break; + } + } + } + return 0; +} + + + +void bt_mesh_gattc_exchange_mtu(u8_t index) +{ + /** Set local MTU and exchange with GATT server. + * ATT_MTU >= 69 for Mesh GATT Prov Service + * ATT_NTU >= 33 for Mesh GATT Proxy Service + */ + + ble_gattc_exchange_mtu(bt_mesh_gattc_info[index].conn.handle, mtu_cb, NULL); +} + +u16_t bt_mesh_gattc_get_mtu_info(struct bt_mesh_conn *conn) +{ + int i; + + for (i = 0; i < ARRAY_SIZE(bt_mesh_gattc_info); i++) { + if (conn == &bt_mesh_gattc_info[i].conn) { + return bt_mesh_gattc_info[i].mtu; + } + } + + return 0; +} + +int bt_mesh_gattc_write_no_rsp(struct bt_mesh_conn *conn, const struct bt_mesh_gatt_attr *attr, + const void *data, u16_t len) +{ + u16_t conn_id; + int i; + + for (i = 0; i < ARRAY_SIZE(bt_mesh_gattc_info); i++) { + if (conn == &bt_mesh_gattc_info[i].conn) { + break; + } + } + + if (i == ARRAY_SIZE(bt_mesh_gattc_info)) { + BT_ERR("%s, Conn is not found", __func__); + /** Here we return 0 for prov_send() return value check in provisioner.c + */ + return 0; + } + + conn_id = BLE_MESH_GATT_CREATE_CONN_ID(bt_mesh_gattc_info[i].conn.handle); + + struct os_mbuf *om; + int rc; + + om = ble_hs_mbuf_from_flat(data, len); + if (om == NULL) { + return -1; + } + + rc = ble_gattc_write_no_rsp(conn_id, bt_mesh_gattc_info[i].data_in_handle, om); + if (rc != 0) { + return -1; + } + + return 0; +} + +void bt_mesh_gattc_disconnect(struct bt_mesh_conn *conn) +{ + /** Disconnect + * Clear proper proxy server information + * Clear proper prov_link information + * Clear proper bt_mesh_gattc_info information + * Here in adapter, we just clear proper bt_mesh_gattc_info, and + * when proxy_disconnected callback comes, the proxy server + * information and prov_link information should be cleared. + */ + int i; + + for (i = 0; i < ARRAY_SIZE(bt_mesh_gattc_info); i++) { + if (conn == &bt_mesh_gattc_info[i].conn) { + break; + } + } + + if (i == ARRAY_SIZE(bt_mesh_gattc_info)) { + BT_ERR("%s, Conn is not found", __func__); + return; + } + ble_gap_terminate(bt_mesh_gattc_info[i].conn.handle, BLE_ERR_REM_USER_CONN_TERM); +} + +/** Mesh Provisioning Service: 0x1827 + * Mesh Provisioning Data In: 0x2ADB + * Mesh Provisioning Data Out: 0x2ADC + * Mesh Proxy Service: 0x1828 + * Mesh Proxy Data In: 0x2ADD + * Mesh PROXY Data Out: 0x2ADE + */ +#endif /* (CONFIG_BLE_MESH_PROVISIONER && CONFIG_BLE_MESH_PB_GATT) || CONFIG_BLE_MESH_GATT_PROXY_CLIENT */ + +struct bt_mesh_conn *bt_mesh_conn_ref(struct bt_mesh_conn *conn) +{ + bt_mesh_atomic_inc(&conn->ref); + + BT_DBG("handle %u ref %u", conn->handle, bt_mesh_atomic_get(&conn->ref)); + + return conn; +} + +void bt_mesh_conn_unref(struct bt_mesh_conn *conn) +{ + bt_mesh_atomic_dec(&conn->ref); + + BT_DBG("handle %u ref %u", conn->handle, bt_mesh_atomic_get(&conn->ref)); +} + +#if defined(CONFIG_BLE_MESH_NODE) && CONFIG_BLE_MESH_NODE +static int proxy_char_access_cb(uint16_t conn_handle, uint16_t attr_handle, + struct ble_gatt_access_ctxt *ctxt, void *arg) +{ + if (ctxt->op == BLE_GATT_ACCESS_OP_WRITE_CHR || ctxt->op == BLE_GATT_ACCESS_OP_WRITE_DSC) { + struct bt_mesh_gatt_attr *attr = bt_mesh_gatts_find_attr_by_handle(attr_handle); + u8_t index = BLE_MESH_GATT_GET_CONN_ID(conn_handle); + u16_t len = 0; + + BT_DBG("%s, write: handle = %d, len = %d, data = %s", __func__, attr_handle, + ctxt->om->om_len, + bt_hex(ctxt->om->om_data, ctxt->om->om_len)); + + if (attr != NULL && attr->write != NULL) { + if ((len = attr->write(&bt_mesh_gatts_conn[index], attr, + ctxt->om->om_data, + ctxt->om->om_len, + 0 /* offset */, 0)) > 0) { + } + } + } else if (ctxt->op == BLE_GATT_ACCESS_OP_READ_CHR || ctxt->op == BLE_GATT_ACCESS_OP_READ_DSC) { + BT_ERR("%s, Unhandled read request for chr and dsc: opcode - %d", __func__, ctxt->op); + } + return 0; +} + +static int dummy_access_cb(uint16_t conn_handle, uint16_t attr_handle, + struct ble_gatt_access_ctxt *ctxt, void *arg) +{ + /* + * We should never never enter this callback - it's attached to notify-only + * characteristic which are notified directly from mbuf. And we can't pass + * NULL as access_cb because gatts will assert on init... + */ + assert(0); + return 0; +} + +static const struct ble_gatt_svc_def svc_defs [] = { +#ifdef CONFIG_BLE_MESH_GATT_PROXY_SERVER + { + .type = BLE_GATT_SVC_TYPE_PRIMARY, + .uuid = BLE_UUID16_DECLARE(BT_UUID_MESH_PROXY_VAL), + .includes = NULL, + .characteristics = (struct ble_gatt_chr_def[]) + { { + .uuid = BLE_UUID16_DECLARE(BT_UUID_MESH_PROXY_DATA_IN_VAL), + .access_cb = proxy_char_access_cb, + .flags = BLE_GATT_CHR_F_WRITE_NO_RSP, + }, { + .uuid = BLE_UUID16_DECLARE(BT_UUID_MESH_PROXY_DATA_OUT_VAL), + .access_cb = dummy_access_cb, + .flags = BLE_GATT_CHR_F_NOTIFY, + }, { + 0, /* No more characteristics in this service. */ + } + }, + }, +#endif +#ifdef CONFIG_BLE_MESH_PB_GATT + { + .type = BLE_GATT_SVC_TYPE_PRIMARY, + .uuid = BLE_UUID16_DECLARE(BT_UUID_MESH_PROV_VAL), + .includes = NULL, + .characteristics = (struct ble_gatt_chr_def[]) + { { + .uuid = BLE_UUID16_DECLARE(BT_UUID_MESH_PROV_DATA_IN_VAL), + .access_cb = proxy_char_access_cb, + .flags = BLE_GATT_CHR_F_WRITE_NO_RSP, + }, { + .uuid = BLE_UUID16_DECLARE(BT_UUID_MESH_PROV_DATA_OUT_VAL), + .access_cb = dummy_access_cb, + .flags = BLE_GATT_CHR_F_NOTIFY, + }, { + 0, /* No more characteristics in this service. */ + } + }, + }, +#endif + { + 0, /* No more services. */ + }, +}; +#endif + +void gatt_register_cb(struct ble_gatt_register_ctxt *ctxt, + void *arg ) +{ + if (ctxt->op == BLE_GATT_REGISTER_OP_SVC) { + if (ble_uuid_cmp(ctxt->svc.svc_def->uuid, BLE_UUID16_DECLARE(BT_UUID_MESH_PROXY_VAL)) == 0) { + proxy_svc_start_handle = ctxt->svc.handle; + } else if (ble_uuid_cmp(ctxt->svc.svc_def->uuid, BLE_UUID16_DECLARE(BT_UUID_MESH_PROV_VAL)) == 0) { + prov_svc_start_handle = ctxt->svc.handle; + } + } +} + +void bt_mesh_gatt_init(void) +{ + ble_att_set_preferred_mtu(BLE_ATT_MTU_DFLT); + + ble_hs_cfg.gatts_register_cb = gatt_register_cb; + +#if defined(CONFIG_BLE_MESH_NODE) && CONFIG_BLE_MESH_NODE + int rc; + ble_svc_gap_init(); + ble_svc_gatt_init(); + + rc = ble_gatts_count_cfg(svc_defs); + assert(rc == 0); + + rc = ble_gatts_add_svcs(svc_defs); + assert(rc == 0); + + ble_gatts_start(); + + ble_gatts_svc_set_visibility(prov_svc_start_handle, 1); + ble_gatts_svc_set_visibility(proxy_svc_start_handle, 0); +#endif + +#if (CONFIG_BLE_MESH_PROVISIONER && CONFIG_BLE_MESH_PB_GATT) || \ + CONFIG_BLE_MESH_GATT_PROXY_CLIENT + for (int i = 0; i < ARRAY_SIZE(bt_mesh_gattc_info); i++) { + bt_mesh_gattc_info[i].conn.handle = 0xFFFF; + bt_mesh_gattc_info[i].mtu = BLE_ATT_MTU_DFLT; + bt_mesh_gattc_info[i].wr_desc_done = false; + } +#endif +} + +void ble_sm_alg_ecc_init(void); + +void bt_mesh_adapt_init(void) +{ + BT_DBG("%s", __func__); + /* initialization of P-256 parameters */ + ble_sm_alg_ecc_init(); +} + +int bt_mesh_rand(void *buf, size_t len) +{ + int i; + + if (buf == NULL || len == 0) { + BT_ERR("%s, Invalid parameter", __func__); + return -EAGAIN; + } + + for (i = 0; i < (int)(len / sizeof(u32_t)); i++) { + u32_t rand = esp_random(); + memcpy(buf + i * sizeof(u32_t), &rand, sizeof(u32_t)); + } + + BT_DBG("%s, rand: %s", __func__, bt_hex(buf, len)); + return 0; +} + +void bt_mesh_set_private_key(const u8_t pri_key[32]) +{ + memcpy(bt_mesh_private_key, pri_key, 32); +} + +int ble_sm_alg_gen_key_pair(uint8_t *pub, uint8_t *priv); + +const u8_t *bt_mesh_pub_key_get(void) +{ + uint8_t pri_key[32] = {0}; + +#if 1 + if (bt_mesh_atomic_test_bit(bt_mesh_dev.flags, BLE_MESH_DEV_HAS_PUB_KEY)) { + return bt_mesh_public_key; + } +#else + /* BLE Mesh BQB test case MESH/NODE/PROV/UPD/BV-12-C requires + * different public key for each provisioning procedure. + * Note: if enabled, when Provisioner provision multiple devices + * at the same time, this may cause invalid confirmation value. + */ + if (bt_mesh_rand(bt_mesh_private_key, 32)) { + BT_ERR("%s, Unable to generate bt_mesh_private_key", __func__); + return NULL; + } +#endif + + int rc = ble_sm_alg_gen_key_pair(bt_mesh_public_key, pri_key); + if (rc != 0) { + BT_ERR("%s, Failed to generate the key pair", __func__); + return NULL; + } + memcpy(bt_mesh_private_key, pri_key, 32); + + bt_mesh_atomic_set_bit(bt_mesh_dev.flags, BLE_MESH_DEV_HAS_PUB_KEY); + BT_DBG("gen the bt_mesh_public_key:%s", bt_hex(bt_mesh_public_key, sizeof(bt_mesh_public_key))); + + return bt_mesh_public_key; +} + +bool bt_mesh_check_public_key(const u8_t key[64]) +{ + struct mbedtls_ecp_point pt = {0}; + mbedtls_ecp_group grp = {0}; + bool rc = false; + + uint8_t pub[65] = {0}; + /* Hardcoded first byte of pub key for MBEDTLS_ECP_PF_UNCOMPRESSED */ + pub[0] = 0x04; + memcpy(&pub[1], key, 64); + + /* Initialize the required structures here */ + mbedtls_ecp_point_init(&pt); + mbedtls_ecp_group_init(&grp); + + /* Below 3 steps are to validate public key on curve secp256r1 */ + if (mbedtls_ecp_group_load(&grp, MBEDTLS_ECP_DP_SECP256R1) != 0) { + goto exit; + } + + if (mbedtls_ecp_point_read_binary(&grp, &pt, pub, 65) != 0) { + goto exit; + } + + if (mbedtls_ecp_check_pubkey(&grp, &pt) != 0) { + goto exit; + } + + rc = true; + +exit: + mbedtls_ecp_point_free(&pt); + mbedtls_ecp_group_free(&grp); + return rc; + +} + +int ble_sm_alg_gen_dhkey(uint8_t *peer_pub_key_x, uint8_t *peer_pub_key_y, + uint8_t *our_priv_key, uint8_t *out_dhkey); + +int bt_mesh_dh_key_gen(const u8_t remote_pk[64], bt_mesh_dh_key_cb_t cb, const u8_t idx) +{ + uint8_t dhkey[32]; + + BT_DBG("private key = %s", bt_hex(bt_mesh_private_key, 32)); + + ble_sm_alg_gen_dhkey((uint8_t *)&remote_pk[0], (uint8_t *)&remote_pk[32], bt_mesh_private_key, dhkey); + + if (cb != NULL) { + cb((const u8_t *)dhkey, idx); + } + return 0; +} + +#if CONFIG_MBEDTLS_HARDWARE_AES +static void ecb_encrypt(u8_t const *const key_le, u8_t const *const clear_text_le, + u8_t *const cipher_text_le, u8_t *const cipher_text_be) +{ + struct bt_mesh_ecb_param ecb; + mbedtls_aes_context aes_ctx = {0}; + + aes_ctx.key_bytes = 16; + mem_rcopy(&aes_ctx.key[0], key_le, 16); + mem_rcopy(&ecb.clear_text[0], clear_text_le, sizeof(ecb.clear_text)); + mbedtls_aes_crypt_ecb(&aes_ctx, MBEDTLS_AES_ENCRYPT, &ecb.clear_text[0], &ecb.cipher_text[0]); + + if (cipher_text_le) { + mem_rcopy(cipher_text_le, &ecb.cipher_text[0], + sizeof(ecb.cipher_text)); + } + + if (cipher_text_be) { + memcpy(cipher_text_be, &ecb.cipher_text[0], + sizeof(ecb.cipher_text)); + } +} + +static void ecb_encrypt_be(u8_t const *const key_be, u8_t const *const clear_text_be, + u8_t *const cipher_text_be) +{ + struct bt_mesh_ecb_param ecb; + mbedtls_aes_context aes_ctx = {0}; + + aes_ctx.key_bytes = 16; + memcpy(&aes_ctx.key[0], key_be, 16); + memcpy(&ecb.clear_text[0], clear_text_be, sizeof(ecb.clear_text)); + mbedtls_aes_crypt_ecb(&aes_ctx, MBEDTLS_AES_ENCRYPT, &ecb.clear_text[0], &ecb.cipher_text[0]); + + memcpy(cipher_text_be, &ecb.cipher_text[0], sizeof(ecb.cipher_text)); +} +#endif /* CONFIG_MBEDTLS_HARDWARE_AES */ + +int bt_mesh_encrypt_le(const u8_t key[16], const u8_t plaintext[16], + u8_t enc_data[16]) +{ +#if CONFIG_MBEDTLS_HARDWARE_AES + BT_DBG("key %s plaintext %s", bt_hex(key, 16), bt_hex(plaintext, 16)); + + ecb_encrypt(key, plaintext, enc_data, NULL); + + BT_DBG("enc_data %s", bt_hex(enc_data, 16)); + return 0; +#else /* CONFIG_MBEDTLS_HARDWARE_AES */ + struct tc_aes_key_sched_struct s; + u8_t tmp[16]; + + BT_DBG("key %s plaintext %s", bt_hex(key, 16), bt_hex(plaintext, 16)); + + sys_memcpy_swap(tmp, key, 16); + + if (tc_aes128_set_encrypt_key(&s, tmp) == TC_CRYPTO_FAIL) { + return -EINVAL; + } + + sys_memcpy_swap(tmp, plaintext, 16); + + if (tc_aes_encrypt(enc_data, tmp, &s) == TC_CRYPTO_FAIL) { + return -EINVAL; + } + + sys_mem_swap(enc_data, 16); + + BT_DBG("enc_data %s", bt_hex(enc_data, 16)); + + return 0; +#endif /* CONFIG_MBEDTLS_HARDWARE_AES */ +} + +int bt_mesh_encrypt_be(const u8_t key[16], const u8_t plaintext[16], + u8_t enc_data[16]) +{ +#if CONFIG_MBEDTLS_HARDWARE_AES + BT_DBG("key %s plaintext %s", bt_hex(key, 16), bt_hex(plaintext, 16)); + + ecb_encrypt_be(key, plaintext, enc_data); + + BT_DBG("enc_data %s", bt_hex(enc_data, 16)); + + return 0; +#else /* CONFIG_MBEDTLS_HARDWARE_AES */ + struct tc_aes_key_sched_struct s; + + BT_DBG("key %s plaintext %s", bt_hex(key, 16), bt_hex(plaintext, 16)); + + if (tc_aes128_set_encrypt_key(&s, key) == TC_CRYPTO_FAIL) { + return -EINVAL; + } + + if (tc_aes_encrypt(enc_data, plaintext, &s) == TC_CRYPTO_FAIL) { + return -EINVAL; + } + + BT_DBG("enc_data %s", bt_hex(enc_data, 16)); + + return 0; +#endif /* CONFIG_MBEDTLS_HARDWARE_AES */ +} + +#if defined(CONFIG_BLE_MESH_USE_DUPLICATE_SCAN) +int bt_mesh_update_exceptional_list(u8_t sub_code, u8_t type, void *info) +{ + BT_ERR("%s, Unsupported for NimBLE host", __func__); + return 0; +} +#endif diff --git a/examples/bluetooth/esp_ble_mesh/README.md b/examples/bluetooth/esp_ble_mesh/README.md index 744c1db985..aa69e954a0 100644 --- a/examples/bluetooth/esp_ble_mesh/README.md +++ b/examples/bluetooth/esp_ble_mesh/README.md @@ -4,7 +4,7 @@ Please help note that breaking changes may be introduced into ESP-BLE-MESH on [minor IDF versions](https://docs.espressif.com/projects/esp-idf/en/latest/versions.html). -Note: To use examples in this directory, you need to have Bluetooth enabled in configuration, and only Bluedroid can be selected as the host stack. +Note: To use examples in this directory, you need to have Bluetooth enabled in configuration, and either Bluedroid or NimBLE can be selected as the host stack. # Example Layout diff --git a/examples/bluetooth/esp_ble_mesh/ble_mesh_fast_provision/ble_mesh_fast_prov_client/main/ble_mesh_demo_init.c b/examples/bluetooth/esp_ble_mesh/ble_mesh_fast_provision/ble_mesh_fast_prov_client/main/ble_mesh_demo_init.c index 12c96d7a61..e0471fa185 100644 --- a/examples/bluetooth/esp_ble_mesh/ble_mesh_fast_provision/ble_mesh_fast_prov_client/main/ble_mesh_demo_init.c +++ b/examples/bluetooth/esp_ble_mesh/ble_mesh_fast_provision/ble_mesh_fast_prov_client/main/ble_mesh_demo_init.c @@ -16,6 +16,15 @@ #include "esp_bt_device.h" #endif +#ifdef CONFIG_BT_NIMBLE_ENABLED +#include "esp_nimble_hci.h" +#include "nimble/nimble_port.h" +#include "nimble/nimble_port_freertos.h" +#include "host/ble_hs.h" +#include "host/util/util.h" +#include "console/console.h" +#endif + #include "esp_ble_mesh_defs.h" #include "ble_mesh_demo_init.h" #include "esp_ble_mesh_common_api.h" @@ -61,3 +70,74 @@ esp_err_t bluetooth_init(void) } #endif + +#ifdef CONFIG_BT_NIMBLE_ENABLED +static SemaphoreHandle_t mesh_sem; +static uint8_t own_addr_type; +void ble_store_config_init(void); +static uint8_t addr_val[6] = {0}; + +void ble_mesh_get_dev_uuid(uint8_t *dev_uuid) +{ + memcpy(dev_uuid + 2, addr_val, BD_ADDR_LEN); +} + +static void mesh_on_reset(int reason) +{ + ESP_LOGI(TAG, "Resetting state; reason=%d", reason); +} + +static void mesh_on_sync(void) +{ + int rc; + + rc = ble_hs_util_ensure_addr(0); + assert(rc == 0); + + /* Figure out address to use while advertising (no privacy for now) */ + rc = ble_hs_id_infer_auto(0, &own_addr_type); + if (rc != 0) { + ESP_LOGI(TAG, "error determining address type; rc=%d", rc); + return; + } + + rc = ble_hs_id_copy_addr(own_addr_type, addr_val, NULL); + + xSemaphoreGive(mesh_sem); +} + +void mesh_host_task(void *param) +{ + ESP_LOGI(TAG, "BLE Host Task Started"); + /* This function will return only when nimble_port_stop() is executed */ + nimble_port_run(); + + nimble_port_freertos_deinit(); +} + +esp_err_t bluetooth_init(void) +{ + mesh_sem = xSemaphoreCreateBinary(); + if (mesh_sem == NULL) { + ESP_LOGE(TAG, "Failed to create mesh semaphore"); + return ESP_FAIL; + } + + ESP_ERROR_CHECK(esp_nimble_hci_and_controller_init()); + + nimble_port_init(); + /* Initialize the NimBLE host configuration. */ + ble_hs_cfg.reset_cb = mesh_on_reset; + ble_hs_cfg.sync_cb = mesh_on_sync; + ble_hs_cfg.store_status_cb = ble_store_util_status_rr; + + /* XXX Need to have template for store */ + ble_store_config_init(); + + nimble_port_freertos_init(mesh_host_task); + + xSemaphoreTake(mesh_sem, portMAX_DELAY); + + return ESP_OK; +} +#endif diff --git a/examples/bluetooth/esp_ble_mesh/ble_mesh_fast_provision/ble_mesh_fast_prov_server/main/ble_mesh_demo_init.c b/examples/bluetooth/esp_ble_mesh/ble_mesh_fast_provision/ble_mesh_fast_prov_server/main/ble_mesh_demo_init.c index 4381334dd9..fb145ad2ca 100644 --- a/examples/bluetooth/esp_ble_mesh/ble_mesh_fast_provision/ble_mesh_fast_prov_server/main/ble_mesh_demo_init.c +++ b/examples/bluetooth/esp_ble_mesh/ble_mesh_fast_provision/ble_mesh_fast_prov_server/main/ble_mesh_demo_init.c @@ -16,6 +16,15 @@ #include "esp_bt_device.h" #endif +#ifdef CONFIG_BT_NIMBLE_ENABLED +#include "esp_nimble_hci.h" +#include "nimble/nimble_port.h" +#include "nimble/nimble_port_freertos.h" +#include "host/ble_hs.h" +#include "host/util/util.h" +#include "console/console.h" +#endif + #include "esp_ble_mesh_defs.h" #include "ble_mesh_demo_init.h" #include "esp_ble_mesh_common_api.h" @@ -61,3 +70,74 @@ esp_err_t bluetooth_init(void) } #endif + +#ifdef CONFIG_BT_NIMBLE_ENABLED +static SemaphoreHandle_t mesh_sem; +static uint8_t own_addr_type; +void ble_store_config_init(void); +static uint8_t addr_val[6] = {0}; + +void ble_mesh_get_dev_uuid(uint8_t *dev_uuid) +{ + memcpy(dev_uuid + 2, addr_val, BD_ADDR_LEN); +} + +static void mesh_on_reset(int reason) +{ + ESP_LOGI(TAG, "Resetting state; reason=%d", reason); +} + +static void mesh_on_sync(void) +{ + int rc; + + rc = ble_hs_util_ensure_addr(0); + assert(rc == 0); + + /* Figure out address to use while advertising (no privacy for now) */ + rc = ble_hs_id_infer_auto(0, &own_addr_type); + if (rc != 0) { + ESP_LOGI(TAG, "error determining address type; rc=%d", rc); + return; + } + + rc = ble_hs_id_copy_addr(own_addr_type, addr_val, NULL); + + xSemaphoreGive(mesh_sem); +} + +void mesh_host_task(void *param) +{ + ESP_LOGI(TAG, "BLE Host Task Started"); + /* This function will return only when nimble_port_stop() is executed */ + nimble_port_run(); + + nimble_port_freertos_deinit(); +} + +esp_err_t bluetooth_init(void) +{ + mesh_sem = xSemaphoreCreateBinary(); + if (mesh_sem == NULL) { + ESP_LOGE(TAG, "Failed to create mesh semaphore"); + return ESP_FAIL; + } + + ESP_ERROR_CHECK(esp_nimble_hci_and_controller_init()); + + nimble_port_init(); + /* Initialize the NimBLE host configuration. */ + ble_hs_cfg.reset_cb = mesh_on_reset; + ble_hs_cfg.sync_cb = mesh_on_sync; + ble_hs_cfg.store_status_cb = ble_store_util_status_rr; + + /* XXX Need to have template for store */ + ble_store_config_init(); + + nimble_port_freertos_init(mesh_host_task); + + xSemaphoreTake(mesh_sem, portMAX_DELAY); + + return ESP_OK; +} +#endif diff --git a/examples/bluetooth/esp_ble_mesh/ble_mesh_node/onoff_client/main/ble_mesh_demo_init.c b/examples/bluetooth/esp_ble_mesh/ble_mesh_node/onoff_client/main/ble_mesh_demo_init.c index 4381334dd9..fb145ad2ca 100644 --- a/examples/bluetooth/esp_ble_mesh/ble_mesh_node/onoff_client/main/ble_mesh_demo_init.c +++ b/examples/bluetooth/esp_ble_mesh/ble_mesh_node/onoff_client/main/ble_mesh_demo_init.c @@ -16,6 +16,15 @@ #include "esp_bt_device.h" #endif +#ifdef CONFIG_BT_NIMBLE_ENABLED +#include "esp_nimble_hci.h" +#include "nimble/nimble_port.h" +#include "nimble/nimble_port_freertos.h" +#include "host/ble_hs.h" +#include "host/util/util.h" +#include "console/console.h" +#endif + #include "esp_ble_mesh_defs.h" #include "ble_mesh_demo_init.h" #include "esp_ble_mesh_common_api.h" @@ -61,3 +70,74 @@ esp_err_t bluetooth_init(void) } #endif + +#ifdef CONFIG_BT_NIMBLE_ENABLED +static SemaphoreHandle_t mesh_sem; +static uint8_t own_addr_type; +void ble_store_config_init(void); +static uint8_t addr_val[6] = {0}; + +void ble_mesh_get_dev_uuid(uint8_t *dev_uuid) +{ + memcpy(dev_uuid + 2, addr_val, BD_ADDR_LEN); +} + +static void mesh_on_reset(int reason) +{ + ESP_LOGI(TAG, "Resetting state; reason=%d", reason); +} + +static void mesh_on_sync(void) +{ + int rc; + + rc = ble_hs_util_ensure_addr(0); + assert(rc == 0); + + /* Figure out address to use while advertising (no privacy for now) */ + rc = ble_hs_id_infer_auto(0, &own_addr_type); + if (rc != 0) { + ESP_LOGI(TAG, "error determining address type; rc=%d", rc); + return; + } + + rc = ble_hs_id_copy_addr(own_addr_type, addr_val, NULL); + + xSemaphoreGive(mesh_sem); +} + +void mesh_host_task(void *param) +{ + ESP_LOGI(TAG, "BLE Host Task Started"); + /* This function will return only when nimble_port_stop() is executed */ + nimble_port_run(); + + nimble_port_freertos_deinit(); +} + +esp_err_t bluetooth_init(void) +{ + mesh_sem = xSemaphoreCreateBinary(); + if (mesh_sem == NULL) { + ESP_LOGE(TAG, "Failed to create mesh semaphore"); + return ESP_FAIL; + } + + ESP_ERROR_CHECK(esp_nimble_hci_and_controller_init()); + + nimble_port_init(); + /* Initialize the NimBLE host configuration. */ + ble_hs_cfg.reset_cb = mesh_on_reset; + ble_hs_cfg.sync_cb = mesh_on_sync; + ble_hs_cfg.store_status_cb = ble_store_util_status_rr; + + /* XXX Need to have template for store */ + ble_store_config_init(); + + nimble_port_freertos_init(mesh_host_task); + + xSemaphoreTake(mesh_sem, portMAX_DELAY); + + return ESP_OK; +} +#endif diff --git a/examples/bluetooth/esp_ble_mesh/ble_mesh_node/onoff_server/main/ble_mesh_demo_init.c b/examples/bluetooth/esp_ble_mesh/ble_mesh_node/onoff_server/main/ble_mesh_demo_init.c index 4381334dd9..fb145ad2ca 100644 --- a/examples/bluetooth/esp_ble_mesh/ble_mesh_node/onoff_server/main/ble_mesh_demo_init.c +++ b/examples/bluetooth/esp_ble_mesh/ble_mesh_node/onoff_server/main/ble_mesh_demo_init.c @@ -16,6 +16,15 @@ #include "esp_bt_device.h" #endif +#ifdef CONFIG_BT_NIMBLE_ENABLED +#include "esp_nimble_hci.h" +#include "nimble/nimble_port.h" +#include "nimble/nimble_port_freertos.h" +#include "host/ble_hs.h" +#include "host/util/util.h" +#include "console/console.h" +#endif + #include "esp_ble_mesh_defs.h" #include "ble_mesh_demo_init.h" #include "esp_ble_mesh_common_api.h" @@ -61,3 +70,74 @@ esp_err_t bluetooth_init(void) } #endif + +#ifdef CONFIG_BT_NIMBLE_ENABLED +static SemaphoreHandle_t mesh_sem; +static uint8_t own_addr_type; +void ble_store_config_init(void); +static uint8_t addr_val[6] = {0}; + +void ble_mesh_get_dev_uuid(uint8_t *dev_uuid) +{ + memcpy(dev_uuid + 2, addr_val, BD_ADDR_LEN); +} + +static void mesh_on_reset(int reason) +{ + ESP_LOGI(TAG, "Resetting state; reason=%d", reason); +} + +static void mesh_on_sync(void) +{ + int rc; + + rc = ble_hs_util_ensure_addr(0); + assert(rc == 0); + + /* Figure out address to use while advertising (no privacy for now) */ + rc = ble_hs_id_infer_auto(0, &own_addr_type); + if (rc != 0) { + ESP_LOGI(TAG, "error determining address type; rc=%d", rc); + return; + } + + rc = ble_hs_id_copy_addr(own_addr_type, addr_val, NULL); + + xSemaphoreGive(mesh_sem); +} + +void mesh_host_task(void *param) +{ + ESP_LOGI(TAG, "BLE Host Task Started"); + /* This function will return only when nimble_port_stop() is executed */ + nimble_port_run(); + + nimble_port_freertos_deinit(); +} + +esp_err_t bluetooth_init(void) +{ + mesh_sem = xSemaphoreCreateBinary(); + if (mesh_sem == NULL) { + ESP_LOGE(TAG, "Failed to create mesh semaphore"); + return ESP_FAIL; + } + + ESP_ERROR_CHECK(esp_nimble_hci_and_controller_init()); + + nimble_port_init(); + /* Initialize the NimBLE host configuration. */ + ble_hs_cfg.reset_cb = mesh_on_reset; + ble_hs_cfg.sync_cb = mesh_on_sync; + ble_hs_cfg.store_status_cb = ble_store_util_status_rr; + + /* XXX Need to have template for store */ + ble_store_config_init(); + + nimble_port_freertos_init(mesh_host_task); + + xSemaphoreTake(mesh_sem, portMAX_DELAY); + + return ESP_OK; +} +#endif diff --git a/examples/bluetooth/esp_ble_mesh/ble_mesh_provisioner/main/ble_mesh_demo_init.c b/examples/bluetooth/esp_ble_mesh/ble_mesh_provisioner/main/ble_mesh_demo_init.c index 4381334dd9..fb145ad2ca 100644 --- a/examples/bluetooth/esp_ble_mesh/ble_mesh_provisioner/main/ble_mesh_demo_init.c +++ b/examples/bluetooth/esp_ble_mesh/ble_mesh_provisioner/main/ble_mesh_demo_init.c @@ -16,6 +16,15 @@ #include "esp_bt_device.h" #endif +#ifdef CONFIG_BT_NIMBLE_ENABLED +#include "esp_nimble_hci.h" +#include "nimble/nimble_port.h" +#include "nimble/nimble_port_freertos.h" +#include "host/ble_hs.h" +#include "host/util/util.h" +#include "console/console.h" +#endif + #include "esp_ble_mesh_defs.h" #include "ble_mesh_demo_init.h" #include "esp_ble_mesh_common_api.h" @@ -61,3 +70,74 @@ esp_err_t bluetooth_init(void) } #endif + +#ifdef CONFIG_BT_NIMBLE_ENABLED +static SemaphoreHandle_t mesh_sem; +static uint8_t own_addr_type; +void ble_store_config_init(void); +static uint8_t addr_val[6] = {0}; + +void ble_mesh_get_dev_uuid(uint8_t *dev_uuid) +{ + memcpy(dev_uuid + 2, addr_val, BD_ADDR_LEN); +} + +static void mesh_on_reset(int reason) +{ + ESP_LOGI(TAG, "Resetting state; reason=%d", reason); +} + +static void mesh_on_sync(void) +{ + int rc; + + rc = ble_hs_util_ensure_addr(0); + assert(rc == 0); + + /* Figure out address to use while advertising (no privacy for now) */ + rc = ble_hs_id_infer_auto(0, &own_addr_type); + if (rc != 0) { + ESP_LOGI(TAG, "error determining address type; rc=%d", rc); + return; + } + + rc = ble_hs_id_copy_addr(own_addr_type, addr_val, NULL); + + xSemaphoreGive(mesh_sem); +} + +void mesh_host_task(void *param) +{ + ESP_LOGI(TAG, "BLE Host Task Started"); + /* This function will return only when nimble_port_stop() is executed */ + nimble_port_run(); + + nimble_port_freertos_deinit(); +} + +esp_err_t bluetooth_init(void) +{ + mesh_sem = xSemaphoreCreateBinary(); + if (mesh_sem == NULL) { + ESP_LOGE(TAG, "Failed to create mesh semaphore"); + return ESP_FAIL; + } + + ESP_ERROR_CHECK(esp_nimble_hci_and_controller_init()); + + nimble_port_init(); + /* Initialize the NimBLE host configuration. */ + ble_hs_cfg.reset_cb = mesh_on_reset; + ble_hs_cfg.sync_cb = mesh_on_sync; + ble_hs_cfg.store_status_cb = ble_store_util_status_rr; + + /* XXX Need to have template for store */ + ble_store_config_init(); + + nimble_port_freertos_init(mesh_host_task); + + xSemaphoreTake(mesh_sem, portMAX_DELAY); + + return ESP_OK; +} +#endif diff --git a/examples/bluetooth/esp_ble_mesh/ble_mesh_wifi_coexist/main/ble_mesh_demo_init.c b/examples/bluetooth/esp_ble_mesh/ble_mesh_wifi_coexist/main/ble_mesh_demo_init.c index 4381334dd9..fb145ad2ca 100644 --- a/examples/bluetooth/esp_ble_mesh/ble_mesh_wifi_coexist/main/ble_mesh_demo_init.c +++ b/examples/bluetooth/esp_ble_mesh/ble_mesh_wifi_coexist/main/ble_mesh_demo_init.c @@ -16,6 +16,15 @@ #include "esp_bt_device.h" #endif +#ifdef CONFIG_BT_NIMBLE_ENABLED +#include "esp_nimble_hci.h" +#include "nimble/nimble_port.h" +#include "nimble/nimble_port_freertos.h" +#include "host/ble_hs.h" +#include "host/util/util.h" +#include "console/console.h" +#endif + #include "esp_ble_mesh_defs.h" #include "ble_mesh_demo_init.h" #include "esp_ble_mesh_common_api.h" @@ -61,3 +70,74 @@ esp_err_t bluetooth_init(void) } #endif + +#ifdef CONFIG_BT_NIMBLE_ENABLED +static SemaphoreHandle_t mesh_sem; +static uint8_t own_addr_type; +void ble_store_config_init(void); +static uint8_t addr_val[6] = {0}; + +void ble_mesh_get_dev_uuid(uint8_t *dev_uuid) +{ + memcpy(dev_uuid + 2, addr_val, BD_ADDR_LEN); +} + +static void mesh_on_reset(int reason) +{ + ESP_LOGI(TAG, "Resetting state; reason=%d", reason); +} + +static void mesh_on_sync(void) +{ + int rc; + + rc = ble_hs_util_ensure_addr(0); + assert(rc == 0); + + /* Figure out address to use while advertising (no privacy for now) */ + rc = ble_hs_id_infer_auto(0, &own_addr_type); + if (rc != 0) { + ESP_LOGI(TAG, "error determining address type; rc=%d", rc); + return; + } + + rc = ble_hs_id_copy_addr(own_addr_type, addr_val, NULL); + + xSemaphoreGive(mesh_sem); +} + +void mesh_host_task(void *param) +{ + ESP_LOGI(TAG, "BLE Host Task Started"); + /* This function will return only when nimble_port_stop() is executed */ + nimble_port_run(); + + nimble_port_freertos_deinit(); +} + +esp_err_t bluetooth_init(void) +{ + mesh_sem = xSemaphoreCreateBinary(); + if (mesh_sem == NULL) { + ESP_LOGE(TAG, "Failed to create mesh semaphore"); + return ESP_FAIL; + } + + ESP_ERROR_CHECK(esp_nimble_hci_and_controller_init()); + + nimble_port_init(); + /* Initialize the NimBLE host configuration. */ + ble_hs_cfg.reset_cb = mesh_on_reset; + ble_hs_cfg.sync_cb = mesh_on_sync; + ble_hs_cfg.store_status_cb = ble_store_util_status_rr; + + /* XXX Need to have template for store */ + ble_store_config_init(); + + nimble_port_freertos_init(mesh_host_task); + + xSemaphoreTake(mesh_sem, portMAX_DELAY); + + return ESP_OK; +} +#endif From cf6f1e82462d665a6894d6a98f7987ae66715b92 Mon Sep 17 00:00:00 2001 From: Hrishikesh Dhayagude Date: Wed, 5 Feb 2020 13:38:29 +0530 Subject: [PATCH 2/2] Add support of NimBLE host in ESP-BLE-Mesh - v3.3 --- components/bt/CMakeLists.txt | 72 +++++--- components/bt/Kconfig | 3 +- components/bt/bluedroid/api/esp_gap_bt_api.c | 1 + .../btc/profile/std/include/btc_gap_bt.h | 1 + .../bluedroid/common/include/common/bt_defs.h | 27 +-- .../common/include/common/bt_target.h | 6 +- .../common/include/common/bt_trace.h | 77 +------- components/bt/bluedroid/stack/btu/btu_init.c | 2 +- .../bluedroid/stack/include/stack/bt_types.h | 22 +-- .../btc/core/btc_alarm.c | 1 + .../btc/core/btc_manage.c | 3 - .../{bluedroid => common}/btc/core/btc_task.c | 19 +- .../btc/include/btc/btc_alarm.h | 0 .../btc/include/btc/btc_manage.h | 2 - .../btc/include/btc/btc_task.h | 7 +- components/bt/common/include/bt_common.h | 169 ++++++++++++++++++ .../bt/{bluedroid => common}/osi/alarm.c | 7 +- .../bt/{bluedroid => common}/osi/allocator.c | 2 +- .../bt/{bluedroid => common}/osi/buffer.c | 4 +- .../bt/{bluedroid => common}/osi/config.c | 2 +- .../{bluedroid => common}/osi/fixed_queue.c | 2 - .../bt/{bluedroid => common}/osi/future.c | 3 +- .../osi/hash_functions.c | 0 .../bt/{bluedroid => common}/osi/hash_map.c | 3 +- .../osi/include/osi/alarm.h | 0 .../osi/include/osi/allocator.h | 0 .../osi/include/osi/buffer.h | 0 .../osi/include/osi/config.h | 0 .../osi/include/osi/fixed_queue.h | 0 .../osi/include/osi/future.h | 0 .../osi/include/osi/hash_functions.h | 0 .../osi/include/osi/hash_map.h | 0 .../osi/include/osi/list.h | 0 .../osi/include/osi/mutex.h | 0 .../osi/include/osi/osi.h | 0 .../osi/include/osi/semaphore.h | 0 .../osi/include/osi/thread.h | 10 +- .../bt/{bluedroid => common}/osi/list.c | 3 +- .../bt/{bluedroid => common}/osi/mutex.c | 0 components/bt/{bluedroid => common}/osi/osi.c | 0 .../bt/{bluedroid => common}/osi/semaphore.c | 0 components/bt/component.mk | 31 +++- .../bt/esp_ble_mesh/mesh_common/mesh_kernel.c | 1 + .../bluedroid_host/mesh_bearer_adapt.c | 1 + .../mesh_core/include/mesh_bearer_adapt.h | 8 +- components/nimble/nimble | 2 +- .../nimble/port/include/esp_nimble_cfg.h | 8 + .../main/ble_mesh_demo_init.c | 4 +- .../main/ble_mesh_demo_init.c | 4 +- .../onoff_client/main/ble_mesh_demo_init.c | 4 +- .../onoff_server/main/ble_mesh_demo_init.c | 4 +- .../main/ble_mesh_demo_init.c | 4 +- .../main/ble_mesh_demo_init.c | 4 +- 53 files changed, 312 insertions(+), 211 deletions(-) rename components/bt/{bluedroid => common}/btc/core/btc_alarm.c (97%) rename components/bt/{bluedroid => common}/btc/core/btc_manage.c (93%) rename components/bt/{bluedroid => common}/btc/core/btc_task.c (97%) rename components/bt/{bluedroid => common}/btc/include/btc/btc_alarm.h (100%) rename components/bt/{bluedroid => common}/btc/include/btc/btc_manage.h (94%) rename components/bt/{bluedroid => common}/btc/include/btc/btc_task.h (97%) create mode 100644 components/bt/common/include/bt_common.h rename components/bt/{bluedroid => common}/osi/alarm.c (98%) rename components/bt/{bluedroid => common}/osi/allocator.c (99%) rename components/bt/{bluedroid => common}/osi/buffer.c (96%) rename components/bt/{bluedroid => common}/osi/config.c (99%) rename components/bt/{bluedroid => common}/osi/fixed_queue.c (99%) rename components/bt/{bluedroid => common}/osi/future.c (98%) rename components/bt/{bluedroid => common}/osi/hash_functions.c (100%) rename components/bt/{bluedroid => common}/osi/hash_map.c (99%) rename components/bt/{bluedroid => common}/osi/include/osi/alarm.h (100%) rename components/bt/{bluedroid => common}/osi/include/osi/allocator.h (100%) rename components/bt/{bluedroid => common}/osi/include/osi/buffer.h (100%) rename components/bt/{bluedroid => common}/osi/include/osi/config.h (100%) rename components/bt/{bluedroid => common}/osi/include/osi/fixed_queue.h (100%) rename components/bt/{bluedroid => common}/osi/include/osi/future.h (100%) rename components/bt/{bluedroid => common}/osi/include/osi/hash_functions.h (100%) rename components/bt/{bluedroid => common}/osi/include/osi/hash_map.h (100%) rename components/bt/{bluedroid => common}/osi/include/osi/list.h (100%) rename components/bt/{bluedroid => common}/osi/include/osi/mutex.h (100%) rename components/bt/{bluedroid => common}/osi/include/osi/osi.h (100%) rename components/bt/{bluedroid => common}/osi/include/osi/semaphore.h (100%) rename components/bt/{bluedroid => common}/osi/include/osi/thread.h (88%) rename components/bt/{bluedroid => common}/osi/list.c (99%) rename components/bt/{bluedroid => common}/osi/mutex.c (100%) rename components/bt/{bluedroid => common}/osi/osi.c (100%) rename components/bt/{bluedroid => common}/osi/semaphore.c (100%) diff --git a/components/bt/CMakeLists.txt b/components/bt/CMakeLists.txt index 337fcfe977..7de6b2f907 100644 --- a/components/bt/CMakeLists.txt +++ b/components/bt/CMakeLists.txt @@ -6,6 +6,8 @@ if(CONFIG_BT_ENABLED) if(CONFIG_BLUEDROID_ENABLED) list(APPEND COMPONENT_PRIV_INCLUDEDIRS + common/btc/include + common/include bluedroid/bta/include bluedroid/bta/ar/include bluedroid/bta/av/include @@ -18,7 +20,6 @@ if(CONFIG_BT_ENABLED) bluedroid/bta/sys/include bluedroid/device/include bluedroid/hci/include - bluedroid/osi/include bluedroid/external/sbc/decoder/include bluedroid/external/sbc/encoder/include bluedroid/btc/profile/esp/blufi/include @@ -38,11 +39,29 @@ if(CONFIG_BT_ENABLED) bluedroid/stack/a2dp/include bluedroid/stack/rfcomm/include bluedroid/stack/include - bluedroid/common/include) + bluedroid/common/include + common/btc/include + common/include) - list(APPEND COMPONENT_ADD_INCLUDEDIRS bluedroid/api/include/api) + list(APPEND COMPONENT_ADD_INCLUDEDIRS bluedroid/api/include/api + common/osi/include) - list(APPEND COMPONENT_SRCS "bluedroid/api/esp_a2dp_api.c" + list(APPEND COMPONENT_SRCS "common/btc/core/btc_alarm.c" + "common/btc/core/btc_manage.c" + "common/btc/core/btc_task.c" + "common/osi/alarm.c" + "common/osi/allocator.c" + "common/osi/buffer.c" + "common/osi/config.c" + "common/osi/fixed_queue.c" + "common/osi/future.c" + "common/osi/hash_functions.c" + "common/osi/hash_map.c" + "common/osi/list.c" + "common/osi/mutex.c" + "common/osi/osi.c" + "common/osi/semaphore.c" + "bluedroid/api/esp_a2dp_api.c" "bluedroid/api/esp_avrc_api.c" "bluedroid/api/esp_blufi_api.c" "bluedroid/api/esp_bt_device.c" @@ -109,18 +128,15 @@ if(CONFIG_BT_ENABLED) "bluedroid/bta/sys/bta_sys_conn.c" "bluedroid/bta/sys/bta_sys_main.c" "bluedroid/bta/sys/utl.c" - "bluedroid/btc/core/btc_alarm.c" "bluedroid/btc/core/btc_ble_storage.c" "bluedroid/btc/core/btc_config.c" "bluedroid/btc/core/btc_dev.c" "bluedroid/btc/core/btc_dm.c" "bluedroid/btc/core/btc_main.c" - "bluedroid/btc/core/btc_manage.c" "bluedroid/btc/core/btc_profile_queue.c" "bluedroid/btc/core/btc_sec.c" "bluedroid/btc/core/btc_sm.c" "bluedroid/btc/core/btc_storage.c" - "bluedroid/btc/core/btc_task.c" "bluedroid/btc/core/btc_util.c" "bluedroid/btc/profile/esp/blufi/blufi_prf.c" "bluedroid/btc/profile/esp/blufi/blufi_protocol.c" @@ -173,18 +189,6 @@ if(CONFIG_BT_ENABLED) "bluedroid/hci/packet_fragmenter.c" "bluedroid/main/bte_init.c" "bluedroid/main/bte_main.c" - "bluedroid/osi/alarm.c" - "bluedroid/osi/allocator.c" - "bluedroid/osi/buffer.c" - "bluedroid/osi/config.c" - "bluedroid/osi/fixed_queue.c" - "bluedroid/osi/future.c" - "bluedroid/osi/hash_functions.c" - "bluedroid/osi/hash_map.c" - "bluedroid/osi/list.c" - "bluedroid/osi/mutex.c" - "bluedroid/osi/osi.c" - "bluedroid/osi/semaphore.c" "bluedroid/stack/a2dp/a2d_api.c" "bluedroid/stack/a2dp/a2d_sbc.c" "bluedroid/stack/avct/avct_api.c" @@ -281,7 +285,6 @@ if(CONFIG_BT_ENABLED) "bluedroid/stack/smp/smp_utils.c") if(CONFIG_BLE_MESH) - list(APPEND COMPONENT_ADD_INCLUDEDIRS bluedroid/osi/include) list(APPEND COMPONENT_SRCS "esp_ble_mesh/mesh_core/bluedroid_host/mesh_bearer_adapt.c") endif() @@ -362,6 +365,35 @@ if(CONFIG_BT_ENABLED) "esp_ble_mesh/mesh_models/server/state_transition.c" "esp_ble_mesh/mesh_models/server/time_scene_server.c") endif() + + if(CONFIG_NIMBLE_ENABLED) + + if (CONFIG_BLE_MESH) + list(APPEND COMPONENT_PRIV_INCLUDEDIRS + common/btc/include + common/include) + + list(APPEND COMPONENT_ADD_INCLUDEDIRS common/osi/include) + + list(APPEND COMPONENT_SRCS "esp_ble_mesh/mesh_core/nimble_host/mesh_bearer_adapt.c" + "common/btc/core/btc_alarm.c" + "common/btc/core/btc_manage.c" + "common/btc/core/btc_task.c" + "common/osi/alarm.c" + "common/osi/allocator.c" + "common/osi/buffer.c" + "common/osi/config.c" + "common/osi/fixed_queue.c" + "common/osi/future.c" + "common/osi/hash_functions.c" + "common/osi/hash_map.c" + "common/osi/list.c" + "common/osi/mutex.c" + "common/osi/osi.c" + "common/osi/semaphore.c") + endif() + + endif() endif() # requirements can't depend on config diff --git a/components/bt/Kconfig b/components/bt/Kconfig index ec8c20317b..9ee47b45e5 100644 --- a/components/bt/Kconfig +++ b/components/bt/Kconfig @@ -1341,7 +1341,7 @@ menu Bluetooth config NIMBLE_MAX_CONNECTIONS int "Maximum number of concurrent connections" range 1 9 - default 1 + default BTDM_CONTROLLER_BLE_MAX_CONN depends on NIMBLE_ENABLED help Defines maximum number of concurrent BLE connections @@ -1392,6 +1392,7 @@ menu Bluetooth config NIMBLE_TASK_STACK_SIZE int "NimBLE Host task stack size" depends on NIMBLE_ENABLED + default 5120 if BLE_MESH default 4096 help This configures stack size of NimBLE host task diff --git a/components/bt/bluedroid/api/esp_gap_bt_api.c b/components/bt/bluedroid/api/esp_gap_bt_api.c index ac0326e11f..89e1df33fa 100644 --- a/components/bt/bluedroid/api/esp_gap_bt_api.c +++ b/components/bt/bluedroid/api/esp_gap_bt_api.c @@ -17,6 +17,7 @@ #include "esp_bt_main.h" #include "esp_gap_bt_api.h" #include "common/bt_trace.h" +#include "bta/bta_api.h" #include "btc/btc_manage.h" #include "btc_gap_bt.h" #include "btc/btc_storage.h" diff --git a/components/bt/bluedroid/btc/profile/std/include/btc_gap_bt.h b/components/bt/bluedroid/btc/profile/std/include/btc_gap_bt.h index 67e03cb3f9..847b938194 100644 --- a/components/bt/bluedroid/btc/profile/std/include/btc_gap_bt.h +++ b/components/bt/bluedroid/btc/profile/std/include/btc_gap_bt.h @@ -16,6 +16,7 @@ #define __BTC_GAP_BT_H__ #include "common/bt_target.h" +#include "common/bt_defs.h" #include "esp_bt_defs.h" #include "esp_gap_bt_api.h" #include "btc/btc_task.h" diff --git a/components/bt/bluedroid/common/include/common/bt_defs.h b/components/bt/bluedroid/common/include/common/bt_defs.h index 77719bc847..7421e7b147 100644 --- a/components/bt/bluedroid/common/include/common/bt_defs.h +++ b/components/bt/bluedroid/common/include/common/bt_defs.h @@ -21,7 +21,7 @@ #include #include -#include "common/bt_trace.h" +#include "bt_common.h" #include "common/bt_target.h" #define UNUSED(x) (void)(x) @@ -65,31 +65,6 @@ typedef struct { uint8_t uu[16]; } bt_uuid_t; -/** Bluetooth Error Status */ -/** We need to build on this */ - -/* relate to ESP_BT_STATUS_xxx in esp_bt_defs.h */ -typedef enum { - BT_STATUS_SUCCESS = 0, - BT_STATUS_FAIL, - BT_STATUS_NOT_READY, - BT_STATUS_NOMEM, - BT_STATUS_BUSY, - BT_STATUS_DONE, /* request already completed */ - BT_STATUS_UNSUPPORTED, - BT_STATUS_PARM_INVALID, - BT_STATUS_UNHANDLED, - BT_STATUS_AUTH_FAILURE, - BT_STATUS_RMT_DEV_DOWN, - BT_STATUS_AUTH_REJECTED, - BT_STATUS_INVALID_STATIC_RAND_ADDR, - BT_STATUS_PENDING, - BT_STATUS_UNACCEPT_CONN_INTERVAL, - BT_STATUS_PARAM_OUT_OF_RANGE, - BT_STATUS_TIMEOUT, - BT_STATUS_MEMORY_FULL, -} bt_status_t; - #ifndef CPU_LITTLE_ENDIAN #define CPU_LITTLE_ENDIAN #endif diff --git a/components/bt/bluedroid/common/include/common/bt_target.h b/components/bt/bluedroid/common/include/common/bt_target.h index f2c0d3c211..4504e0cd86 100644 --- a/components/bt/bluedroid/common/include/common/bt_target.h +++ b/components/bt/bluedroid/common/include/common/bt_target.h @@ -20,6 +20,8 @@ #ifndef BT_TARGET_H #define BT_TARGET_H +#include "bt_common.h" + #ifndef BUILDCFG #define BUILDCFG #endif @@ -349,10 +351,6 @@ #define BTA_AV_CO_CP_SCMS_T FALSE//FALSE #endif -#ifndef QUEUE_CONGEST_SIZE -#define QUEUE_CONGEST_SIZE 40 -#endif - #ifndef CONFIG_BLE_HOST_QUEUE_CONGESTION_CHECK #define SCAN_QUEUE_CONGEST_CHECK FALSE #else diff --git a/components/bt/bluedroid/common/include/common/bt_trace.h b/components/bt/bluedroid/common/include/common/bt_trace.h index 7583479b2b..780ff2d08c 100644 --- a/components/bt/bluedroid/common/include/common/bt_trace.h +++ b/components/bt/bluedroid/common/include/common/bt_trace.h @@ -23,38 +23,7 @@ #include #include "stack/bt_types.h" - -#ifndef LOG_LOCAL_LEVEL -#ifndef BOOTLOADER_BUILD -#define LOG_LOCAL_LEVEL CONFIG_LOG_DEFAULT_LEVEL -#else -#define LOG_LOCAL_LEVEL CONFIG_LOG_BOOTLOADER_LEVEL -#endif -#endif - -#include "esp_log.h" - -// Mapping between ESP_LOG_LEVEL and BT_TRACE_LEVEL -#if (LOG_LOCAL_LEVEL >= 4) -#define LOG_LOCAL_LEVEL_MAPPING (LOG_LOCAL_LEVEL+1) -#else -#define LOG_LOCAL_LEVEL_MAPPING LOG_LOCAL_LEVEL -#endif - -#define MAX(a, b) ((a) > (b) ? (a) : (b)) -#define BT_LOG_LEVEL_CHECK(LAYER, LEVEL) (MAX(LAYER##_INITIAL_TRACE_LEVEL, LOG_LOCAL_LEVEL_MAPPING) >= BT_TRACE_LEVEL_##LEVEL) - -//#define TAG "BT" - -#define BT_PRINT_E(tag, format, ...) {esp_log_write(ESP_LOG_ERROR, tag, LOG_FORMAT(E, format), esp_log_timestamp(), tag, ##__VA_ARGS__); } -#define BT_PRINT_W(tag, format, ...) {esp_log_write(ESP_LOG_WARN, tag, LOG_FORMAT(W, format), esp_log_timestamp(), tag, ##__VA_ARGS__); } -#define BT_PRINT_I(tag, format, ...) {esp_log_write(ESP_LOG_INFO, tag, LOG_FORMAT(I, format), esp_log_timestamp(), tag, ##__VA_ARGS__); } -#define BT_PRINT_D(tag, format, ...) {esp_log_write(ESP_LOG_DEBUG, tag, LOG_FORMAT(D, format), esp_log_timestamp(), tag, ##__VA_ARGS__); } -#define BT_PRINT_V(tag, format, ...) {esp_log_write(ESP_LOG_VERBOSE, tag, LOG_FORMAT(V, format), esp_log_timestamp(), tag, ##__VA_ARGS__); } - -#ifndef assert -#define assert(x) do { if (!(x)) BT_PRINT_E("bt host error %s %u\n", __FILE__, __LINE__); } while (0) -#endif +#include "bt_common.h" inline void trc_dump_buffer(const char *prefix, uint8_t *data, uint16_t len) { @@ -324,18 +293,6 @@ inline void trc_dump_buffer(const char *prefix, uint8_t *data, uint16_t len) #define BTIF_INITIAL_TRACE_LEVEL BT_TRACE_LEVEL_WARNING #endif -#ifdef CONFIG_BTC_INITIAL_TRACE_LEVEL -#define BTC_INITIAL_TRACE_LEVEL CONFIG_BTC_INITIAL_TRACE_LEVEL -#else -#define BTC_INITIAL_TRACE_LEVEL BT_TRACE_LEVEL_WARNING -#endif - -#ifdef CONFIG_OSI_INITIAL_TRACE_LEVEL -#define OSI_INITIAL_TRACE_LEVEL CONFIG_OSI_INITIAL_TRACE_LEVEL -#else -#define OSI_INITIAL_TRACE_LEVEL BT_TRACE_LEVEL_WARNING -#endif - #ifdef CONFIG_BLUFI_INITIAL_TRACE_LEVEL #define BLUFI_INITIAL_TRACE_LEVEL CONFIG_BLUFI_INITIAL_TRACE_LEVEL #else @@ -497,22 +454,6 @@ extern UINT8 btif_trace_level; #define HCI_TRACE_EVENT(fmt, args...) {if (HCI_INITIAL_TRACE_LEVEL >= BT_TRACE_LEVEL_EVENT && BT_LOG_LEVEL_CHECK(HCI,EVENT)) BT_PRINT_D("BT_HCI", fmt,## args);} #define HCI_TRACE_DEBUG(fmt, args...) {if (HCI_INITIAL_TRACE_LEVEL >= BT_TRACE_LEVEL_DEBUG && BT_LOG_LEVEL_CHECK(HCI,DEBUG)) BT_PRINT_D("BT_HCI", fmt,## args);} -/* define traces for BTC */ -#define BTC_TRACE_ERROR(fmt, args...) {if (BTC_INITIAL_TRACE_LEVEL >= BT_TRACE_LEVEL_ERROR && BT_LOG_LEVEL_CHECK(BTC, ERROR)) BT_PRINT_E("BT_BTC", fmt, ## args);} -#define BTC_TRACE_WARNING(fmt, args...) {if (BTC_INITIAL_TRACE_LEVEL >= BT_TRACE_LEVEL_WARNING && BT_LOG_LEVEL_CHECK(BTC, WARNING)) BT_PRINT_W("BT_BTC", fmt, ## args);} -#define BTC_TRACE_API(fmt, args...) {if (BTC_INITIAL_TRACE_LEVEL >= BT_TRACE_LEVEL_API && BT_LOG_LEVEL_CHECK(BTC,API)) BT_PRINT_I("BT_BTC", fmt, ## args);} -#define BTC_TRACE_EVENT(fmt, args...) {if (BTC_INITIAL_TRACE_LEVEL >= BT_TRACE_LEVEL_EVENT && BT_LOG_LEVEL_CHECK(BTC,EVENT)) BT_PRINT_D("BT_BTC", fmt, ## args);} -#define BTC_TRACE_DEBUG(fmt, args...) {if (BTC_INITIAL_TRACE_LEVEL >= BT_TRACE_LEVEL_DEBUG && BT_LOG_LEVEL_CHECK(BTC,DEBUG)) BT_PRINT_D("BT_BTC", fmt, ## args);} -#define BTC_TRACE_VERBOSE(fmt, args...) {if (BTC_INITIAL_TRACE_LEVEL >= BT_TRACE_LEVEL_VERBOSE && BT_LOG_LEVEL_CHECK(BTC,VERBOSE)) BT_PRINT_V("BT_BTC", fmt, ## args);} - -/* define traces for OSI */ -#define OSI_TRACE_ERROR(fmt, args...) {if (OSI_INITIAL_TRACE_LEVEL >= BT_TRACE_LEVEL_ERROR && BT_LOG_LEVEL_CHECK(OSI, ERROR)) BT_PRINT_E("BT_OSI", fmt, ## args);} -#define OSI_TRACE_WARNING(fmt, args...) {if (OSI_INITIAL_TRACE_LEVEL >= BT_TRACE_LEVEL_WARNING && BT_LOG_LEVEL_CHECK(OSI, WARNING)) BT_PRINT_W("BT_OSI", fmt, ## args);} -#define OSI_TRACE_API(fmt, args...) {if (OSI_INITIAL_TRACE_LEVEL >= BT_TRACE_LEVEL_API && BT_LOG_LEVEL_CHECK(OSI,API)) BT_PRINT_I("BT_OSI", fmt, ## args);} -#define OSI_TRACE_EVENT(fmt, args...) {if (OSI_INITIAL_TRACE_LEVEL >= BT_TRACE_LEVEL_EVENT && BT_LOG_LEVEL_CHECK(OSI,EVENT)) BT_PRINT_D("BT_OSI", fmt, ## args);} -#define OSI_TRACE_DEBUG(fmt, args...) {if (OSI_INITIAL_TRACE_LEVEL >= BT_TRACE_LEVEL_DEBUG && BT_LOG_LEVEL_CHECK(OSI,DEBUG)) BT_PRINT_D("BT_OSI", fmt, ## args);} -#define OSI_TRACE_VERBOSE(fmt, args...) {if (OSI_INITIAL_TRACE_LEVEL >= BT_TRACE_LEVEL_VERBOSE && BT_LOG_LEVEL_CHECK(OSI,VERBOSE)) BT_PRINT_V("BT_OSI", fmt, ## args);} - /* define traces for BLUFI */ #define BLUFI_TRACE_ERROR(fmt, args...) {if (BLUFI_INITIAL_TRACE_LEVEL >= BT_TRACE_LEVEL_ERROR && BT_LOG_LEVEL_CHECK(BLUFI, ERROR)) BT_PRINT_E("BT_BLUFI", fmt, ## args);} #define BLUFI_TRACE_WARNING(fmt, args...) {if (BLUFI_INITIAL_TRACE_LEVEL >= BT_TRACE_LEVEL_WARNING && BT_LOG_LEVEL_CHECK(BLUFI, WARNING)) BT_PRINT_W("BT_BLUFI", fmt, ## args);} @@ -671,22 +612,6 @@ extern UINT8 btif_trace_level; #define APPL_TRACE_DEBUG(fmt, args...) #define APPL_TRACE_VERBOSE(fmt, args...) -/* define traces for BTC */ -#define BTC_TRACE_ERROR(fmt, args...) -#define BTC_TRACE_WARNING(fmt, args...) -#define BTC_TRACE_API(fmt, args...) -#define BTC_TRACE_EVENT(fmt, args...) -#define BTC_TRACE_DEBUG(fmt, args...) -#define BTC_TRACE_VERBOSE(fmt, args...) - -/* define traces for OSI */ -#define OSI_TRACE_ERROR(fmt, args...) -#define OSI_TRACE_WARNING(fmt, args...) -#define OSI_TRACE_API(fmt, args...) -#define OSI_TRACE_EVENT(fmt, args...) -#define OSI_TRACE_DEBUG(fmt, args...) -#define OSI_TRACE_VERBOSE(fmt, args...) - /* define traces for BLUFI */ #define BLUFI_TRACE_ERROR(fmt, args...) #define BLUFI_TRACE_WARNING(fmt, args...) diff --git a/components/bt/bluedroid/stack/btu/btu_init.c b/components/bt/bluedroid/stack/btu/btu_init.c index 7014cfde00..f9c3d12c3d 100644 --- a/components/bt/bluedroid/stack/btu/btu_init.c +++ b/components/bt/bluedroid/stack/btu/btu_init.c @@ -240,7 +240,7 @@ UINT16 BTU_BleAclPktSize(void) bool BTU_check_queue_is_congest(void) { UBaseType_t wait_size = uxQueueMessagesWaiting(xBtuQueue); - if(wait_size >= QUEUE_CONGEST_SIZE ) { + if(wait_size >= BT_QUEUE_CONGEST_SIZE ) { return true; } return false; diff --git a/components/bt/bluedroid/stack/include/stack/bt_types.h b/components/bt/bluedroid/stack/include/stack/bt_types.h index 52385cb513..b331125f35 100644 --- a/components/bt/bluedroid/stack/include/stack/bt_types.h +++ b/components/bt/bluedroid/stack/include/stack/bt_types.h @@ -21,14 +21,7 @@ #include #include - -#ifndef FALSE -# define FALSE false -#endif - -#ifndef TRUE -# define TRUE true -#endif +#include "bt_common.h" typedef uint8_t UINT8; typedef uint16_t UINT16; @@ -524,19 +517,6 @@ typedef struct { typedef UINT8 tBT_DEVICE_TYPE; /*****************************************************************************/ - -/* Define trace levels */ -#define BT_TRACE_LEVEL_NONE 0 /* No trace messages to be generated */ -#define BT_TRACE_LEVEL_ERROR 1 /* Error condition trace messages */ -#define BT_TRACE_LEVEL_WARNING 2 /* Warning condition trace messages */ -#define BT_TRACE_LEVEL_API 3 /* API traces */ -#define BT_TRACE_LEVEL_EVENT 4 /* Debug messages for events */ -#define BT_TRACE_LEVEL_DEBUG 5 /* Full debug messages */ -#define BT_TRACE_LEVEL_VERBOSE 6 /* Verbose debug messages */ - -#define MAX_TRACE_LEVEL 6 - - /* Define New Trace Type Definition */ /* TRACE_CTRL_TYPE 0x^^000000*/ #define TRACE_CTRL_MASK 0xff000000 diff --git a/components/bt/bluedroid/btc/core/btc_alarm.c b/components/bt/common/btc/core/btc_alarm.c similarity index 97% rename from components/bt/bluedroid/btc/core/btc_alarm.c rename to components/bt/common/btc/core/btc_alarm.c index ade9f093ac..653f9826d2 100644 --- a/components/bt/bluedroid/btc/core/btc_alarm.c +++ b/components/bt/common/btc/core/btc_alarm.c @@ -14,6 +14,7 @@ #include "btc/btc_task.h" #include "btc/btc_alarm.h" +#include "esp_log.h" void btc_alarm_handler(btc_msg_t *msg) { diff --git a/components/bt/bluedroid/btc/core/btc_manage.c b/components/bt/common/btc/core/btc_manage.c similarity index 93% rename from components/bt/bluedroid/btc/core/btc_manage.c rename to components/bt/common/btc/core/btc_manage.c index 81ecad4103..037deee3a8 100644 --- a/components/bt/bluedroid/btc/core/btc_manage.c +++ b/components/bt/common/btc/core/btc_manage.c @@ -14,10 +14,7 @@ #include "btc/btc_task.h" -#include "common/bt_trace.h" #include "osi/thread.h" -#include "esp_bt_defs.h" -#include "esp_gatt_defs.h" static void *btc_profile_cb_tab[BTC_PID_NUM] = {}; diff --git a/components/bt/bluedroid/btc/core/btc_task.c b/components/bt/common/btc/core/btc_task.c similarity index 97% rename from components/bt/bluedroid/btc/core/btc_task.c rename to components/bt/common/btc/core/btc_task.c index 21726cf898..53f77629a9 100644 --- a/components/bt/bluedroid/btc/core/btc_task.c +++ b/components/bt/common/btc/core/btc_task.c @@ -14,12 +14,14 @@ #include #include -#include "common/bt_target.h" #include "btc/btc_task.h" -#include "common/bt_trace.h" #include "osi/thread.h" -#include "common/bt_defs.h" +#include "esp_log.h" +#include "bt_common.h" #include "osi/allocator.h" +#include "btc/btc_alarm.h" +#ifdef CONFIG_BLUEDROID_ENABLED +#include "common/bt_target.h" #include "btc/btc_main.h" #include "btc/btc_dev.h" #include "btc_gatts.h" @@ -28,7 +30,6 @@ #include "btc_gap_ble.h" #include "btc_blufi_prf.h" #include "btc/btc_dm.h" -#include "btc/btc_alarm.h" #include "bta/bta_gatt_api.h" #if CONFIG_CLASSIC_BT_ENABLED #include "btc/btc_profile_queue.h" @@ -46,6 +47,7 @@ #include "btc_hf_client.h" #endif /* #if BTC_HF_CLIENT_INCLUDED */ #endif /* #if CONFIG_CLASSIC_BT_ENABLED */ +#endif #if CONFIG_BLE_MESH #include "btc_ble_mesh_prov.h" @@ -61,6 +63,7 @@ static xTaskHandle xBtcTaskHandle = NULL; static xQueueHandle xBtcQueue = 0; static btc_func_t profile_tab[BTC_PID_NUM] = { +#ifdef CONFIG_BLUEDROID_ENABLED [BTC_PID_MAIN_INIT] = {btc_main_call_handler, NULL }, [BTC_PID_DEV] = {btc_dev_call_handler, NULL }, #if (GATTS_INCLUDED == TRUE) @@ -79,7 +82,9 @@ static btc_func_t profile_tab[BTC_PID_NUM] = { [BTC_PID_BLUFI] = {btc_blufi_call_handler, btc_blufi_cb_handler }, #endif ///GATTS_INCLUDED == TRUE [BTC_PID_DM_SEC] = {NULL, btc_dm_sec_cb_handler }, +#endif [BTC_PID_ALARM] = {btc_alarm_handler, NULL }, +#ifdef CONFIG_BLUEDROID_ENABLED #if CONFIG_CLASSIC_BT_ENABLED #if (BTC_GAP_BT_INCLUDED == TRUE) [BTC_PID_GAP_BT] = {btc_gap_bt_call_handler, btc_gap_bt_cb_handler }, @@ -96,6 +101,7 @@ static btc_func_t profile_tab[BTC_PID_NUM] = { [BTC_PID_HF_CLIENT] = {btc_hf_client_call_handler, btc_hf_client_cb_handler}, #endif /* #if BTC_HF_CLIENT_INCLUDED */ #endif /* #if CONFIG_CLASSIC_BT_ENABLED */ +#endif #if CONFIG_BLE_MESH [BTC_PID_PROV] = {btc_ble_mesh_prov_call_handler, btc_ble_mesh_prov_cb_handler }, [BTC_PID_MODEL] = {btc_ble_mesh_model_call_handler, btc_ble_mesh_model_cb_handler }, @@ -194,7 +200,10 @@ int btc_init(void) if (xBtcTaskHandle == NULL || xBtcQueue == 0){ return BT_STATUS_NOMEM; } +#ifdef CONFIG_BLUEDROID_ENABLED btc_gap_callback_init(); +#endif + #if SCAN_QUEUE_CONGEST_CHECK btc_adv_list_init(); #endif @@ -216,7 +225,7 @@ void btc_deinit(void) bool btc_check_queue_is_congest(void) { UBaseType_t wait_size = uxQueueMessagesWaiting(xBtcQueue); - if(wait_size >= QUEUE_CONGEST_SIZE) { + if(wait_size >= BT_QUEUE_CONGEST_SIZE) { return true; } return false; diff --git a/components/bt/bluedroid/btc/include/btc/btc_alarm.h b/components/bt/common/btc/include/btc/btc_alarm.h similarity index 100% rename from components/bt/bluedroid/btc/include/btc/btc_alarm.h rename to components/bt/common/btc/include/btc/btc_alarm.h diff --git a/components/bt/bluedroid/btc/include/btc/btc_manage.h b/components/bt/common/btc/include/btc/btc_manage.h similarity index 94% rename from components/bt/bluedroid/btc/include/btc/btc_manage.h rename to components/bt/common/btc/include/btc/btc_manage.h index 46f746e8df..778786baaf 100644 --- a/components/bt/bluedroid/btc/include/btc/btc_manage.h +++ b/components/bt/common/btc/include/btc/btc_manage.h @@ -15,9 +15,7 @@ #ifndef __BTC_MANAGE_H__ #define __BTC_MANAGE_H__ -#include "bta/bta_api.h" #include "btc/btc_task.h" -#include "esp_bt_defs.h" /* reset gatt callback table */ void esp_profile_cb_reset(void); diff --git a/components/bt/bluedroid/btc/include/btc/btc_task.h b/components/bt/common/btc/include/btc/btc_task.h similarity index 97% rename from components/bt/bluedroid/btc/include/btc/btc_task.h rename to components/bt/common/btc/include/btc/btc_task.h index aa916d6f0e..00b63e55c0 100644 --- a/components/bt/bluedroid/btc/include/btc/btc_task.h +++ b/components/bt/common/btc/include/btc/btc_task.h @@ -16,10 +16,13 @@ #define __BTC_TASK_H__ #include -#include "common/bt_target.h" -#include "common/bt_defs.h" +#include "bt_common.h" #include "osi/thread.h" +#if CONFIG_BLUEDROID_ENABLED +#include "common/bt_target.h" +#endif + typedef struct btc_msg { uint8_t sig; //event signal uint8_t aid; //application id diff --git a/components/bt/common/include/bt_common.h b/components/bt/common/include/bt_common.h new file mode 100644 index 0000000000..32c7ce3485 --- /dev/null +++ b/components/bt/common/include/bt_common.h @@ -0,0 +1,169 @@ + +// Copyright 2019 Espressif Systems (Shanghai) PTE LTD +// +// 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. + +#ifndef _BT_COMMON_H_ +#define _BT_COMMON_H_ + +#include "esp_log.h" + +#ifndef FALSE +#define FALSE false +#endif + +#ifndef TRUE +#define TRUE true +#endif + +#ifndef BT_QUEUE_CONGEST_SIZE +#define BT_QUEUE_CONGEST_SIZE 40 +#endif + +#ifdef CONFIG_BTC_INITIAL_TRACE_LEVEL +#define BTC_INITIAL_TRACE_LEVEL CONFIG_BTC_INITIAL_TRACE_LEVEL +#else +#define BTC_INITIAL_TRACE_LEVEL BT_TRACE_LEVEL_WARNING +#endif + +#ifdef CONFIG_OSI_INITIAL_TRACE_LEVEL +#define OSI_INITIAL_TRACE_LEVEL CONFIG_OSI_INITIAL_TRACE_LEVEL +#else +#define OSI_INITIAL_TRACE_LEVEL BT_TRACE_LEVEL_WARNING +#endif + +#if CONFIG_BT_BLE_DYNAMIC_ENV_MEMORY +#define BT_BLE_DYNAMIC_ENV_MEMORY TRUE +#else +#define BT_BLE_DYNAMIC_ENV_MEMORY FALSE +#endif + +#ifdef CONFIG_BLUEDROID_PINNED_TO_CORE +#define TASK_PINNED_TO_CORE (CONFIG_BLUEDROID_PINNED_TO_CORE < portNUM_PROCESSORS ? CONFIG_BLUEDROID_PINNED_TO_CORE : tskNO_AFFINITY) +#define BTC_TASK_STACK_SIZE (CONFIG_BTC_TASK_STACK_SIZE + BT_TASK_EXTRA_STACK_SIZE) //by menuconfig +#endif + +#ifdef CONFIG_NIMBLE_ENABLED +#define TASK_PINNED_TO_CORE (CONFIG_NIMBLE_PINNED_TO_CORE < portNUM_PROCESSORS ? CONFIG_NIMBLE_PINNED_TO_CORE : tskNO_AFFINITY) +#define BTC_TASK_STACK_SIZE 4096 +#endif + +#define BTC_TASK_PINNED_TO_CORE (TASK_PINNED_TO_CORE) +#define BTC_TASK_PRIO (configMAX_PRIORITIES - 6) +#define BTC_TASK_QUEUE_LEN 60 + +/* Define trace levels */ +#define BT_TRACE_LEVEL_NONE 0 /* No trace messages to be generated */ +#define BT_TRACE_LEVEL_ERROR 1 /* Error condition trace messages */ +#define BT_TRACE_LEVEL_WARNING 2 /* Warning condition trace messages */ +#define BT_TRACE_LEVEL_API 3 /* API traces */ +#define BT_TRACE_LEVEL_EVENT 4 /* Debug messages for events */ +#define BT_TRACE_LEVEL_DEBUG 5 /* Full debug messages */ +#define BT_TRACE_LEVEL_VERBOSE 6 /* Verbose debug messages */ + +#define MAX_TRACE_LEVEL 6 + +#ifndef LOG_LOCAL_LEVEL +#ifndef BOOTLOADER_BUILD +#define LOG_LOCAL_LEVEL CONFIG_LOG_DEFAULT_LEVEL +#else +#define LOG_LOCAL_LEVEL CONFIG_LOG_BOOTLOADER_LEVEL +#endif +#endif + +// Mapping between ESP_LOG_LEVEL and BT_TRACE_LEVEL +#if (LOG_LOCAL_LEVEL >= 4) +#define LOG_LOCAL_LEVEL_MAPPING (LOG_LOCAL_LEVEL+1) +#else +#define LOG_LOCAL_LEVEL_MAPPING LOG_LOCAL_LEVEL +#endif + +#define MAX(a, b) ((a) > (b) ? (a) : (b)) + +#define BT_LOG_LEVEL_CHECK(LAYER, LEVEL) (MAX(LAYER##_INITIAL_TRACE_LEVEL, LOG_LOCAL_LEVEL_MAPPING) >= BT_TRACE_LEVEL_##LEVEL) + +#define BT_PRINT_E(tag, format, ...) {esp_log_write(ESP_LOG_ERROR, tag, LOG_FORMAT(E, format), esp_log_timestamp(), tag, ##__VA_ARGS__); } +#define BT_PRINT_W(tag, format, ...) {esp_log_write(ESP_LOG_WARN, tag, LOG_FORMAT(W, format), esp_log_timestamp(), tag, ##__VA_ARGS__); } +#define BT_PRINT_I(tag, format, ...) {esp_log_write(ESP_LOG_INFO, tag, LOG_FORMAT(I, format), esp_log_timestamp(), tag, ##__VA_ARGS__); } +#define BT_PRINT_D(tag, format, ...) {esp_log_write(ESP_LOG_DEBUG, tag, LOG_FORMAT(D, format), esp_log_timestamp(), tag, ##__VA_ARGS__); } +#define BT_PRINT_V(tag, format, ...) {esp_log_write(ESP_LOG_VERBOSE, tag, LOG_FORMAT(V, format), esp_log_timestamp(), tag, ##__VA_ARGS__); } + +#ifndef assert +#define assert(x) do { if (!(x)) BT_PRINT_E("BT", "bt host error %s %u\n", __FILE__, __LINE__); } while (0) +#endif + + +#if !CONFIG_BT_STACK_NO_LOG +/* define traces for BTC */ +#define BTC_TRACE_ERROR(fmt, args...) {if (BTC_INITIAL_TRACE_LEVEL >= BT_TRACE_LEVEL_ERROR && BT_LOG_LEVEL_CHECK(BTC, ERROR)) BT_PRINT_E("BT_BTC", fmt, ## args);} +#define BTC_TRACE_WARNING(fmt, args...) {if (BTC_INITIAL_TRACE_LEVEL >= BT_TRACE_LEVEL_WARNING && BT_LOG_LEVEL_CHECK(BTC, WARNING)) BT_PRINT_W("BT_BTC", fmt, ## args);} +#define BTC_TRACE_API(fmt, args...) {if (BTC_INITIAL_TRACE_LEVEL >= BT_TRACE_LEVEL_API && BT_LOG_LEVEL_CHECK(BTC,API)) BT_PRINT_I("BT_BTC", fmt, ## args);} +#define BTC_TRACE_EVENT(fmt, args...) {if (BTC_INITIAL_TRACE_LEVEL >= BT_TRACE_LEVEL_EVENT && BT_LOG_LEVEL_CHECK(BTC,EVENT)) BT_PRINT_D("BT_BTC", fmt, ## args);} +#define BTC_TRACE_DEBUG(fmt, args...) {if (BTC_INITIAL_TRACE_LEVEL >= BT_TRACE_LEVEL_DEBUG && BT_LOG_LEVEL_CHECK(BTC,DEBUG)) BT_PRINT_D("BT_BTC", fmt, ## args);} +#define BTC_TRACE_VERBOSE(fmt, args...) {if (BTC_INITIAL_TRACE_LEVEL >= BT_TRACE_LEVEL_VERBOSE && BT_LOG_LEVEL_CHECK(BTC,VERBOSE)) BT_PRINT_V("BT_BTC", fmt, ## args);} + +/* define traces for OSI */ +#define OSI_TRACE_ERROR(fmt, args...) {if (OSI_INITIAL_TRACE_LEVEL >= BT_TRACE_LEVEL_ERROR && BT_LOG_LEVEL_CHECK(OSI, ERROR)) BT_PRINT_E("BT_OSI", fmt, ## args);} +#define OSI_TRACE_WARNING(fmt, args...) {if (OSI_INITIAL_TRACE_LEVEL >= BT_TRACE_LEVEL_WARNING && BT_LOG_LEVEL_CHECK(OSI, WARNING)) BT_PRINT_W("BT_OSI", fmt, ## args);} +#define OSI_TRACE_API(fmt, args...) {if (OSI_INITIAL_TRACE_LEVEL >= BT_TRACE_LEVEL_API && BT_LOG_LEVEL_CHECK(OSI,API)) BT_PRINT_I("BT_OSI", fmt, ## args);} +#define OSI_TRACE_EVENT(fmt, args...) {if (OSI_INITIAL_TRACE_LEVEL >= BT_TRACE_LEVEL_EVENT && BT_LOG_LEVEL_CHECK(OSI,EVENT)) BT_PRINT_D("BT_OSI", fmt, ## args);} +#define OSI_TRACE_DEBUG(fmt, args...) {if (OSI_INITIAL_TRACE_LEVEL >= BT_TRACE_LEVEL_DEBUG && BT_LOG_LEVEL_CHECK(OSI,DEBUG)) BT_PRINT_D("BT_OSI", fmt, ## args);} +#define OSI_TRACE_VERBOSE(fmt, args...) {if (OSI_INITIAL_TRACE_LEVEL >= BT_TRACE_LEVEL_VERBOSE && BT_LOG_LEVEL_CHECK(OSI,VERBOSE)) BT_PRINT_V("BT_OSI", fmt, ## args);} + +#else + +/* define traces for BTC */ +#define BTC_TRACE_ERROR(fmt, args...) +#define BTC_TRACE_WARNING(fmt, args...) +#define BTC_TRACE_API(fmt, args...) +#define BTC_TRACE_EVENT(fmt, args...) +#define BTC_TRACE_DEBUG(fmt, args...) +#define BTC_TRACE_VERBOSE(fmt, args...) + +/* define traces for OSI */ +#define OSI_TRACE_ERROR(fmt, args...) +#define OSI_TRACE_WARNING(fmt, args...) +#define OSI_TRACE_API(fmt, args...) +#define OSI_TRACE_EVENT(fmt, args...) +#define OSI_TRACE_DEBUG(fmt, args...) +#define OSI_TRACE_VERBOSE(fmt, args...) + +#endif + +/** Bluetooth Error Status */ +/** We need to build on this */ + +/* relate to ESP_BT_STATUS_xxx in esp_bt_defs.h */ +typedef enum { + BT_STATUS_SUCCESS = 0, + BT_STATUS_FAIL, + BT_STATUS_NOT_READY, + BT_STATUS_NOMEM, + BT_STATUS_BUSY, + BT_STATUS_DONE, /* request already completed */ + BT_STATUS_UNSUPPORTED, + BT_STATUS_PARM_INVALID, + BT_STATUS_UNHANDLED, + BT_STATUS_AUTH_FAILURE, + BT_STATUS_RMT_DEV_DOWN, + BT_STATUS_AUTH_REJECTED, + BT_STATUS_INVALID_STATIC_RAND_ADDR, + BT_STATUS_PENDING, + BT_STATUS_UNACCEPT_CONN_INTERVAL, + BT_STATUS_PARAM_OUT_OF_RANGE, + BT_STATUS_TIMEOUT, + BT_STATUS_MEMORY_FULL, + BT_STATUS_EIR_TOO_LARGE, +} bt_status_t; + +#endif /* _BT_COMMON_H_ */ diff --git a/components/bt/bluedroid/osi/alarm.c b/components/bt/common/osi/alarm.c similarity index 98% rename from components/bt/bluedroid/osi/alarm.c rename to components/bt/common/osi/alarm.c index 5307010590..0a216a8dba 100644 --- a/components/bt/bluedroid/osi/alarm.c +++ b/components/bt/common/osi/alarm.c @@ -18,8 +18,6 @@ #include #include #include -#include "common/bt_defs.h" -#include "common/bt_trace.h" #include "osi/alarm.h" #include "osi/allocator.h" #include "osi/list.h" @@ -27,6 +25,7 @@ #include "btc/btc_task.h" #include "btc/btc_alarm.h" #include "osi/mutex.h" +#include "bt_common.h" typedef struct alarm_t { /* timer id point to here */ @@ -251,12 +250,12 @@ end: osi_alarm_err_t osi_alarm_set(osi_alarm_t *alarm, period_ms_t timeout) { - return alarm_set(alarm, timeout, false); + return alarm_set(alarm, timeout, FALSE); } osi_alarm_err_t osi_alarm_set_periodic(osi_alarm_t *alarm, period_ms_t period) { - return alarm_set(alarm, period, true); + return alarm_set(alarm, period, TRUE); } osi_alarm_err_t osi_alarm_cancel(osi_alarm_t *alarm) diff --git a/components/bt/bluedroid/osi/allocator.c b/components/bt/common/osi/allocator.c similarity index 99% rename from components/bt/bluedroid/osi/allocator.c rename to components/bt/common/osi/allocator.c index 7a06015e25..006f44209e 100644 --- a/components/bt/bluedroid/osi/allocator.c +++ b/components/bt/common/osi/allocator.c @@ -18,7 +18,7 @@ #include #include -#include "common/bt_defs.h" +#include "bt_common.h" #include "osi/allocator.h" extern void *pvPortZalloc(size_t size); diff --git a/components/bt/bluedroid/osi/buffer.c b/components/bt/common/osi/buffer.c similarity index 96% rename from components/bt/bluedroid/osi/buffer.c rename to components/bt/common/osi/buffer.c index 6b21ed8e28..5593675296 100644 --- a/components/bt/bluedroid/osi/buffer.c +++ b/components/bt/common/osi/buffer.c @@ -16,11 +16,9 @@ * ******************************************************************************/ #include -#include "common/bt_trace.h" +#include "bt_common.h" #include "osi/allocator.h" #include "osi/buffer.h" -#include "common/bt_defs.h" -#include "common/bt_trace.h" struct buffer_t { buffer_t *root; diff --git a/components/bt/bluedroid/osi/config.c b/components/bt/common/osi/config.c similarity index 99% rename from components/bt/bluedroid/osi/config.c rename to components/bt/common/osi/config.c index bd7307e8a3..75ec639679 100644 --- a/components/bt/bluedroid/osi/config.c +++ b/components/bt/common/osi/config.c @@ -23,10 +23,10 @@ #include #include +#include "bt_common.h" #include "osi/allocator.h" #include "osi/config.h" #include "osi/list.h" -#include "common/bt_trace.h" #define CONFIG_FILE_MAX_SIZE (1536)//1.5k #define CONFIG_FILE_DEFAULE_LENGTH (2048) diff --git a/components/bt/bluedroid/osi/fixed_queue.c b/components/bt/common/osi/fixed_queue.c similarity index 99% rename from components/bt/bluedroid/osi/fixed_queue.c rename to components/bt/common/osi/fixed_queue.c index 24442a0960..d0e68edb2c 100644 --- a/components/bt/bluedroid/osi/fixed_queue.c +++ b/components/bt/common/osi/fixed_queue.c @@ -16,12 +16,10 @@ * ******************************************************************************/ -#include "common/bt_defs.h" #include "osi/allocator.h" #include "osi/fixed_queue.h" #include "osi/list.h" #include "osi/osi.h" -#include "common/bt_trace.h" #include "osi/mutex.h" #include "osi/semaphore.h" diff --git a/components/bt/bluedroid/osi/future.c b/components/bt/common/osi/future.c similarity index 98% rename from components/bt/bluedroid/osi/future.c rename to components/bt/common/osi/future.c index 25eb5605e6..aee33b1c01 100644 --- a/components/bt/bluedroid/osi/future.c +++ b/components/bt/common/osi/future.c @@ -16,8 +16,7 @@ * ******************************************************************************/ -#include "common/bt_trace.h" - +#include "bt_common.h" #include "osi/allocator.h" #include "osi/future.h" #include "osi/osi.h" diff --git a/components/bt/bluedroid/osi/hash_functions.c b/components/bt/common/osi/hash_functions.c similarity index 100% rename from components/bt/bluedroid/osi/hash_functions.c rename to components/bt/common/osi/hash_functions.c diff --git a/components/bt/bluedroid/osi/hash_map.c b/components/bt/common/osi/hash_map.c similarity index 99% rename from components/bt/bluedroid/osi/hash_map.c rename to components/bt/common/osi/hash_map.c index 1487b07edc..bd7f67d00b 100644 --- a/components/bt/bluedroid/osi/hash_map.c +++ b/components/bt/common/osi/hash_map.c @@ -16,8 +16,7 @@ * ******************************************************************************/ -#include "common/bt_defs.h" -#include "common/bt_trace.h" +#include "bt_common.h" #include "osi/list.h" #include "osi/hash_map.h" #include "osi/allocator.h" diff --git a/components/bt/bluedroid/osi/include/osi/alarm.h b/components/bt/common/osi/include/osi/alarm.h similarity index 100% rename from components/bt/bluedroid/osi/include/osi/alarm.h rename to components/bt/common/osi/include/osi/alarm.h diff --git a/components/bt/bluedroid/osi/include/osi/allocator.h b/components/bt/common/osi/include/osi/allocator.h similarity index 100% rename from components/bt/bluedroid/osi/include/osi/allocator.h rename to components/bt/common/osi/include/osi/allocator.h diff --git a/components/bt/bluedroid/osi/include/osi/buffer.h b/components/bt/common/osi/include/osi/buffer.h similarity index 100% rename from components/bt/bluedroid/osi/include/osi/buffer.h rename to components/bt/common/osi/include/osi/buffer.h diff --git a/components/bt/bluedroid/osi/include/osi/config.h b/components/bt/common/osi/include/osi/config.h similarity index 100% rename from components/bt/bluedroid/osi/include/osi/config.h rename to components/bt/common/osi/include/osi/config.h diff --git a/components/bt/bluedroid/osi/include/osi/fixed_queue.h b/components/bt/common/osi/include/osi/fixed_queue.h similarity index 100% rename from components/bt/bluedroid/osi/include/osi/fixed_queue.h rename to components/bt/common/osi/include/osi/fixed_queue.h diff --git a/components/bt/bluedroid/osi/include/osi/future.h b/components/bt/common/osi/include/osi/future.h similarity index 100% rename from components/bt/bluedroid/osi/include/osi/future.h rename to components/bt/common/osi/include/osi/future.h diff --git a/components/bt/bluedroid/osi/include/osi/hash_functions.h b/components/bt/common/osi/include/osi/hash_functions.h similarity index 100% rename from components/bt/bluedroid/osi/include/osi/hash_functions.h rename to components/bt/common/osi/include/osi/hash_functions.h diff --git a/components/bt/bluedroid/osi/include/osi/hash_map.h b/components/bt/common/osi/include/osi/hash_map.h similarity index 100% rename from components/bt/bluedroid/osi/include/osi/hash_map.h rename to components/bt/common/osi/include/osi/hash_map.h diff --git a/components/bt/bluedroid/osi/include/osi/list.h b/components/bt/common/osi/include/osi/list.h similarity index 100% rename from components/bt/bluedroid/osi/include/osi/list.h rename to components/bt/common/osi/include/osi/list.h diff --git a/components/bt/bluedroid/osi/include/osi/mutex.h b/components/bt/common/osi/include/osi/mutex.h similarity index 100% rename from components/bt/bluedroid/osi/include/osi/mutex.h rename to components/bt/common/osi/include/osi/mutex.h diff --git a/components/bt/bluedroid/osi/include/osi/osi.h b/components/bt/common/osi/include/osi/osi.h similarity index 100% rename from components/bt/bluedroid/osi/include/osi/osi.h rename to components/bt/common/osi/include/osi/osi.h diff --git a/components/bt/bluedroid/osi/include/osi/semaphore.h b/components/bt/common/osi/include/osi/semaphore.h similarity index 100% rename from components/bt/bluedroid/osi/include/osi/semaphore.h rename to components/bt/common/osi/include/osi/semaphore.h diff --git a/components/bt/bluedroid/osi/include/osi/thread.h b/components/bt/common/osi/include/osi/thread.h similarity index 88% rename from components/bt/bluedroid/osi/include/osi/thread.h rename to components/bt/common/osi/include/osi/thread.h index a8e0773b90..cf7e7b01f9 100644 --- a/components/bt/bluedroid/osi/include/osi/thread.h +++ b/components/bt/common/osi/include/osi/thread.h @@ -21,7 +21,7 @@ #include "freertos/queue.h" #include "freertos/task.h" #include "esp_task.h" -#include "common/bt_defs.h" +#include "bt_common.h" #define portBASE_TYPE int @@ -57,8 +57,6 @@ typedef enum { SIG_BTU_NUM, } SIG_BTU_t; -#define TASK_PINNED_TO_CORE (CONFIG_BLUEDROID_PINNED_TO_CORE < portNUM_PROCESSORS ? CONFIG_BLUEDROID_PINNED_TO_CORE : tskNO_AFFINITY) - #define HCI_HOST_TASK_PINNED_TO_CORE (TASK_PINNED_TO_CORE) #define HCI_HOST_TASK_STACK_SIZE (2048 + BT_TASK_EXTRA_STACK_SIZE) #define HCI_HOST_TASK_PRIO (configMAX_PRIORITIES - 3) @@ -77,12 +75,6 @@ typedef enum { #define BTU_TASK_NAME "btuT" #define BTU_QUEUE_LEN 50 -#define BTC_TASK_PINNED_TO_CORE (TASK_PINNED_TO_CORE) -#define BTC_TASK_STACK_SIZE (CONFIG_BTC_TASK_STACK_SIZE + BT_TASK_EXTRA_STACK_SIZE) //by menuconfig -#define BTC_TASK_NAME "btcT" -#define BTC_TASK_PRIO (configMAX_PRIORITIES - 6) -#define BTC_TASK_QUEUE_LEN 60 - #define BTC_A2DP_SINK_TASK_PINNED_TO_CORE (TASK_PINNED_TO_CORE) #define BTC_A2DP_SINK_TASK_STACK_SIZE (CONFIG_A2DP_SINK_TASK_STACK_SIZE + BT_TASK_EXTRA_STACK_SIZE) // by menuconfig #define BTC_A2DP_SINK_TASK_NAME "BtA2dSinkT" diff --git a/components/bt/bluedroid/osi/list.c b/components/bt/common/osi/list.c similarity index 99% rename from components/bt/bluedroid/osi/list.c rename to components/bt/common/osi/list.c index 1a41873acf..730fe4e97a 100644 --- a/components/bt/bluedroid/osi/list.c +++ b/components/bt/common/osi/list.c @@ -1,6 +1,5 @@ -#include "common/bt_defs.h" - +#include "bt_common.h" #include "osi/allocator.h" #include "osi/list.h" #include "osi/osi.h" diff --git a/components/bt/bluedroid/osi/mutex.c b/components/bt/common/osi/mutex.c similarity index 100% rename from components/bt/bluedroid/osi/mutex.c rename to components/bt/common/osi/mutex.c diff --git a/components/bt/bluedroid/osi/osi.c b/components/bt/common/osi/osi.c similarity index 100% rename from components/bt/bluedroid/osi/osi.c rename to components/bt/common/osi/osi.c diff --git a/components/bt/bluedroid/osi/semaphore.c b/components/bt/common/osi/semaphore.c similarity index 100% rename from components/bt/bluedroid/osi/semaphore.c rename to components/bt/common/osi/semaphore.c diff --git a/components/bt/component.mk b/components/bt/component.mk index 73208b868c..04061b1605 100644 --- a/components/bt/component.mk +++ b/components/bt/component.mk @@ -22,11 +22,11 @@ ifeq ($(GCC_NOT_5_2_0), 1) CFLAGS += -Wno-implicit-fallthrough endif -endif - - ifdef CONFIG_BLUEDROID_ENABLED +COMPONENT_SRCDIRS += common/osi \ + common/btc/core + COMPONENT_PRIV_INCLUDEDIRS += bluedroid/bta/include \ bluedroid/bta/ar/include \ bluedroid/bta/av/include \ @@ -41,7 +41,6 @@ COMPONENT_PRIV_INCLUDEDIRS += bluedroid/bta/include \ bluedroid/device/include \ bluedroid/gki/include \ bluedroid/hci/include \ - bluedroid/osi/include \ bluedroid/utils/include \ bluedroid/external/sbc/decoder/include \ bluedroid/external/sbc/encoder/include \ @@ -69,7 +68,10 @@ COMPONENT_PRIV_INCLUDEDIRS += bluedroid/bta/include \ bluedroid/stack/rfcomm/include \ bluedroid/stack/include \ bluedroid/utils/include \ - bluedroid/common/include + bluedroid/common/include \ + common/btc/include \ + common/osi/include \ + common/include COMPONENT_ADD_INCLUDEDIRS += bluedroid/api/include/api @@ -128,11 +130,11 @@ bluedroid/stack/smp/smp_keys.o: CFLAGS += -Wno-unused-const-variable endif ifdef CONFIG_BLE_MESH -COMPONENT_ADD_INCLUDEDIRS += bluedroid/osi/include +COMPONENT_ADD_INCLUDEDIRS += common/osi/include COMPONENT_SRCDIRS += esp_ble_mesh/mesh_core/bluedroid_host -endif +endif endif ifdef CONFIG_BLE_MESH @@ -157,3 +159,18 @@ COMPONENT_SRCDIRS += esp_ble_mesh/mesh_common \ esp_ble_mesh/api/core \ esp_ble_mesh/api/models endif + +ifdef CONFIG_NIMBLE_ENABLED +ifdef CONFIG_BLE_MESH +COMPONENT_PRIV_INCLUDEDIRS += common/btc/include \ + common/include + +COMPONENT_SRCDIRS += common/osi \ + common/btc/core \ + esp_ble_mesh/mesh_core/nimble_host + +COMPONENT_ADD_INCLUDEDIRS += common/osi/include +endif +endif + +endif diff --git a/components/bt/esp_ble_mesh/mesh_common/mesh_kernel.c b/components/bt/esp_ble_mesh/mesh_common/mesh_kernel.c index d4597d8184..05607dcb94 100644 --- a/components/bt/esp_ble_mesh/mesh_common/mesh_kernel.c +++ b/components/bt/esp_ble_mesh/mesh_common/mesh_kernel.c @@ -14,6 +14,7 @@ #include "mesh_common.h" #include "provisioner_prov.h" +#include "bt_common.h" static bt_mesh_mutex_t bm_alarm_lock; static bt_mesh_mutex_t bm_list_lock; diff --git a/components/bt/esp_ble_mesh/mesh_core/bluedroid_host/mesh_bearer_adapt.c b/components/bt/esp_ble_mesh/mesh_core/bluedroid_host/mesh_bearer_adapt.c index 80998d2024..183c8d3fd4 100644 --- a/components/bt/esp_ble_mesh/mesh_core/bluedroid_host/mesh_bearer_adapt.c +++ b/components/bt/esp_ble_mesh/mesh_core/bluedroid_host/mesh_bearer_adapt.c @@ -19,6 +19,7 @@ #include "device/controller.h" #include "mbedtls/aes.h" +#include "bt_common.h" #include "mesh_hci.h" #include "mesh_aes_encrypt.h" diff --git a/components/bt/esp_ble_mesh/mesh_core/include/mesh_bearer_adapt.h b/components/bt/esp_ble_mesh/mesh_core/include/mesh_bearer_adapt.h index 7c3ead1d0a..0ac2350d16 100644 --- a/components/bt/esp_ble_mesh/mesh_core/include/mesh_bearer_adapt.h +++ b/components/bt/esp_ble_mesh/mesh_core/include/mesh_bearer_adapt.h @@ -23,11 +23,11 @@ #define BLE_MESH_ADV_TASK_CORE TASK_PINNED_TO_CORE #endif -#ifdef CONFIG_BT_NIMBLE_ENABLED -#define BLE_MESH_MAX_CONN CONFIG_BT_NIMBLE_MAX_CONNECTIONS +#ifdef CONFIG_NIMBLE_ENABLED +#define BLE_MESH_MAX_CONN CONFIG_NIMBLE_MAX_CONNECTIONS -#ifdef CONFIG_BT_NIMBLE_PINNED_TO_CORE -#define BLE_MESH_ADV_TASK_CORE (CONFIG_BT_NIMBLE_PINNED_TO_CORE < portNUM_PROCESSORS ? CONFIG_BT_NIMBLE_PINNED_TO_CORE : tskNO_AFFINITY) +#ifdef CONFIG_NIMBLE_PINNED_TO_CORE +#define BLE_MESH_ADV_TASK_CORE (CONFIG_NIMBLE_PINNED_TO_CORE < portNUM_PROCESSORS ? CONFIG_NIMBLE_PINNED_TO_CORE : tskNO_AFFINITY) #else #define BLE_MESH_ADV_TASK_CORE (0) #endif diff --git a/components/nimble/nimble b/components/nimble/nimble index 8326807c5e..5250d712a5 160000 --- a/components/nimble/nimble +++ b/components/nimble/nimble @@ -1 +1 @@ -Subproject commit 8326807c5e580c3bb8ad13d1e80819aa0029004e +Subproject commit 5250d712a5454b2623bf92343c027d77477ca804 diff --git a/components/nimble/port/include/esp_nimble_cfg.h b/components/nimble/port/include/esp_nimble_cfg.h index 86e483d93b..1298ec71b1 100644 --- a/components/nimble/port/include/esp_nimble_cfg.h +++ b/components/nimble/port/include/esp_nimble_cfg.h @@ -415,6 +415,14 @@ #define MYNEWT_VAL_BLE_HOST (1) #endif +#ifndef MYNEWT_VAL_ESP_BLE_MESH +#ifdef CONFIG_BLE_MESH_HCI_5_0 +#define MYNEWT_VAL_ESP_BLE_MESH (1) +#else +#define MYNEWT_VAL_ESP_BLE_MESH (0) +#endif +#endif + #ifndef MYNEWT_VAL_BLE_HS_DEBUG #ifdef CONFIG_NIMBLE_DEBUG #define MYNEWT_VAL_BLE_HS_DEBUG (1) diff --git a/examples/bluetooth/esp_ble_mesh/ble_mesh_fast_provision/ble_mesh_fast_prov_client/main/ble_mesh_demo_init.c b/examples/bluetooth/esp_ble_mesh/ble_mesh_fast_provision/ble_mesh_fast_prov_client/main/ble_mesh_demo_init.c index e0471fa185..2f6854eb61 100644 --- a/examples/bluetooth/esp_ble_mesh/ble_mesh_fast_provision/ble_mesh_fast_prov_client/main/ble_mesh_demo_init.c +++ b/examples/bluetooth/esp_ble_mesh/ble_mesh_fast_provision/ble_mesh_fast_prov_client/main/ble_mesh_demo_init.c @@ -16,7 +16,7 @@ #include "esp_bt_device.h" #endif -#ifdef CONFIG_BT_NIMBLE_ENABLED +#ifdef CONFIG_NIMBLE_ENABLED #include "esp_nimble_hci.h" #include "nimble/nimble_port.h" #include "nimble/nimble_port_freertos.h" @@ -71,7 +71,7 @@ esp_err_t bluetooth_init(void) #endif -#ifdef CONFIG_BT_NIMBLE_ENABLED +#ifdef CONFIG_NIMBLE_ENABLED static SemaphoreHandle_t mesh_sem; static uint8_t own_addr_type; void ble_store_config_init(void); diff --git a/examples/bluetooth/esp_ble_mesh/ble_mesh_fast_provision/ble_mesh_fast_prov_server/main/ble_mesh_demo_init.c b/examples/bluetooth/esp_ble_mesh/ble_mesh_fast_provision/ble_mesh_fast_prov_server/main/ble_mesh_demo_init.c index fb145ad2ca..a61f70e657 100644 --- a/examples/bluetooth/esp_ble_mesh/ble_mesh_fast_provision/ble_mesh_fast_prov_server/main/ble_mesh_demo_init.c +++ b/examples/bluetooth/esp_ble_mesh/ble_mesh_fast_provision/ble_mesh_fast_prov_server/main/ble_mesh_demo_init.c @@ -16,7 +16,7 @@ #include "esp_bt_device.h" #endif -#ifdef CONFIG_BT_NIMBLE_ENABLED +#ifdef CONFIG_NIMBLE_ENABLED #include "esp_nimble_hci.h" #include "nimble/nimble_port.h" #include "nimble/nimble_port_freertos.h" @@ -71,7 +71,7 @@ esp_err_t bluetooth_init(void) #endif -#ifdef CONFIG_BT_NIMBLE_ENABLED +#ifdef CONFIG_NIMBLE_ENABLED static SemaphoreHandle_t mesh_sem; static uint8_t own_addr_type; void ble_store_config_init(void); diff --git a/examples/bluetooth/esp_ble_mesh/ble_mesh_node/onoff_client/main/ble_mesh_demo_init.c b/examples/bluetooth/esp_ble_mesh/ble_mesh_node/onoff_client/main/ble_mesh_demo_init.c index fb145ad2ca..a61f70e657 100644 --- a/examples/bluetooth/esp_ble_mesh/ble_mesh_node/onoff_client/main/ble_mesh_demo_init.c +++ b/examples/bluetooth/esp_ble_mesh/ble_mesh_node/onoff_client/main/ble_mesh_demo_init.c @@ -16,7 +16,7 @@ #include "esp_bt_device.h" #endif -#ifdef CONFIG_BT_NIMBLE_ENABLED +#ifdef CONFIG_NIMBLE_ENABLED #include "esp_nimble_hci.h" #include "nimble/nimble_port.h" #include "nimble/nimble_port_freertos.h" @@ -71,7 +71,7 @@ esp_err_t bluetooth_init(void) #endif -#ifdef CONFIG_BT_NIMBLE_ENABLED +#ifdef CONFIG_NIMBLE_ENABLED static SemaphoreHandle_t mesh_sem; static uint8_t own_addr_type; void ble_store_config_init(void); diff --git a/examples/bluetooth/esp_ble_mesh/ble_mesh_node/onoff_server/main/ble_mesh_demo_init.c b/examples/bluetooth/esp_ble_mesh/ble_mesh_node/onoff_server/main/ble_mesh_demo_init.c index fb145ad2ca..a61f70e657 100644 --- a/examples/bluetooth/esp_ble_mesh/ble_mesh_node/onoff_server/main/ble_mesh_demo_init.c +++ b/examples/bluetooth/esp_ble_mesh/ble_mesh_node/onoff_server/main/ble_mesh_demo_init.c @@ -16,7 +16,7 @@ #include "esp_bt_device.h" #endif -#ifdef CONFIG_BT_NIMBLE_ENABLED +#ifdef CONFIG_NIMBLE_ENABLED #include "esp_nimble_hci.h" #include "nimble/nimble_port.h" #include "nimble/nimble_port_freertos.h" @@ -71,7 +71,7 @@ esp_err_t bluetooth_init(void) #endif -#ifdef CONFIG_BT_NIMBLE_ENABLED +#ifdef CONFIG_NIMBLE_ENABLED static SemaphoreHandle_t mesh_sem; static uint8_t own_addr_type; void ble_store_config_init(void); diff --git a/examples/bluetooth/esp_ble_mesh/ble_mesh_provisioner/main/ble_mesh_demo_init.c b/examples/bluetooth/esp_ble_mesh/ble_mesh_provisioner/main/ble_mesh_demo_init.c index fb145ad2ca..a61f70e657 100644 --- a/examples/bluetooth/esp_ble_mesh/ble_mesh_provisioner/main/ble_mesh_demo_init.c +++ b/examples/bluetooth/esp_ble_mesh/ble_mesh_provisioner/main/ble_mesh_demo_init.c @@ -16,7 +16,7 @@ #include "esp_bt_device.h" #endif -#ifdef CONFIG_BT_NIMBLE_ENABLED +#ifdef CONFIG_NIMBLE_ENABLED #include "esp_nimble_hci.h" #include "nimble/nimble_port.h" #include "nimble/nimble_port_freertos.h" @@ -71,7 +71,7 @@ esp_err_t bluetooth_init(void) #endif -#ifdef CONFIG_BT_NIMBLE_ENABLED +#ifdef CONFIG_NIMBLE_ENABLED static SemaphoreHandle_t mesh_sem; static uint8_t own_addr_type; void ble_store_config_init(void); diff --git a/examples/bluetooth/esp_ble_mesh/ble_mesh_wifi_coexist/main/ble_mesh_demo_init.c b/examples/bluetooth/esp_ble_mesh/ble_mesh_wifi_coexist/main/ble_mesh_demo_init.c index fb145ad2ca..a61f70e657 100644 --- a/examples/bluetooth/esp_ble_mesh/ble_mesh_wifi_coexist/main/ble_mesh_demo_init.c +++ b/examples/bluetooth/esp_ble_mesh/ble_mesh_wifi_coexist/main/ble_mesh_demo_init.c @@ -16,7 +16,7 @@ #include "esp_bt_device.h" #endif -#ifdef CONFIG_BT_NIMBLE_ENABLED +#ifdef CONFIG_NIMBLE_ENABLED #include "esp_nimble_hci.h" #include "nimble/nimble_port.h" #include "nimble/nimble_port_freertos.h" @@ -71,7 +71,7 @@ esp_err_t bluetooth_init(void) #endif -#ifdef CONFIG_BT_NIMBLE_ENABLED +#ifdef CONFIG_NIMBLE_ENABLED static SemaphoreHandle_t mesh_sem; static uint8_t own_addr_type; void ble_store_config_init(void);