From 836772de81baf0074c2ab386fc22b454d9c56d1a Mon Sep 17 00:00:00 2001 From: "Deomid \"rojer\" Ryabkov" Date: Thu, 11 Nov 2021 17:30:50 +0000 Subject: [PATCH] scratch --- include/esp32/esp32_bt.h | 6 +- include/esp32/esp32_bt_gap.h | 8 - include/esp32/esp32_bt_internal.h | 8 +- include/mgos_bt_gap.h | 6 + mos.yml | 21 ++ mos_esp32.yml | 37 --- src/esp32/esp32_bt.c | 114 +++++--- src/esp32/esp32_bt_gap.c | 246 ++++++++++++++++-- ...{esp32_bt_gattc.c => esp32_bt_gattc.c.bak} | 0 src/esp32/esp32_bt_gatts.c | 2 + 10 files changed, 339 insertions(+), 109 deletions(-) delete mode 100644 mos_esp32.yml rename src/esp32/{esp32_bt_gattc.c => esp32_bt_gattc.c.bak} (100%) diff --git a/include/esp32/esp32_bt.h b/include/esp32/esp32_bt.h index c55cfc9..c8e40c1 100644 --- a/include/esp32/esp32_bt.h +++ b/include/esp32/esp32_bt.h @@ -19,8 +19,8 @@ #include -#include "esp_bt_defs.h" -#include "esp_gatt_defs.h" +//#include "esp_bt_defs.h" +//#include "esp_gatt_defs.h" #include "common/mg_str.h" @@ -35,9 +35,11 @@ extern "C" { #define MGOS_BT_ADDR_LEN 6 +/* const char *esp32_bt_addr_to_str(const esp_bd_addr_t addr, char *out); int esp32_bt_addr_cmp(const esp_bd_addr_t a, const esp_bd_addr_t b); const char *esp32_bt_uuid_to_str(const esp_bt_uuid_t *uuid, char *out); +*/ void mgos_bt_uuid_to_esp32(const struct mgos_bt_uuid *in, esp_bt_uuid_t *out); void esp32_bt_uuid_to_mgos(const esp_bt_uuid_t *in, struct mgos_bt_uuid *out); diff --git a/include/esp32/esp32_bt_gap.h b/include/esp32/esp32_bt_gap.h index e75c6be..ede0946 100644 --- a/include/esp32/esp32_bt_gap.h +++ b/include/esp32/esp32_bt_gap.h @@ -34,14 +34,6 @@ extern "C" { bool mgos_bt_gap_get_pairing_enable(void); bool mgos_bt_gap_set_pairing_enable(bool pairing_enable); -int mgos_bt_gap_get_num_paired_devices(void); -/* - * These are actually async. TODO(rojer): Add callbacks to the API. - * For now, just allow some time for the calls to complete. - */ -void mgos_bt_gap_remove_paired_device(const esp_bd_addr_t addr); -void mgos_bt_gap_remove_all_paired_devices(void); - #ifdef __cplusplus } #endif diff --git a/include/esp32/esp32_bt_internal.h b/include/esp32/esp32_bt_internal.h index 3bd0931..40eac06 100644 --- a/include/esp32/esp32_bt_internal.h +++ b/include/esp32/esp32_bt_internal.h @@ -27,21 +27,21 @@ extern "C" { #endif -enum cs_log_level ll_from_status(esp_bt_status_t status); +// enum cs_log_level ll_from_status(esp_bt_status_t status); +const char *esp32_bt_addr_to_str(const uint8_t *addr, char *out); bool esp32_bt_is_scanning(void); bool esp32_bt_gattc_init(void); bool esp32_bt_gap_init(void); bool esp32_bt_gatts_init(void); -void esp32_bt_gatts_auth_cmpl(const esp_bd_addr_t addr, bool success); +//void esp32_bt_gatts_auth_cmpl(const esp_bd_addr_t addr, bool success); void esp32_bt_set_is_advertising(bool is_advertising); -/* Workaround for https://github.com/espressif/esp-idf/issues/1406 */ bool esp32_bt_wipe_config(void); -esp_gatt_status_t esp32_bt_gatt_get_status(enum mgos_bt_gatt_status st); +//esp_gatt_status_t esp32_bt_gatt_get_status(enum mgos_bt_gatt_status st); #ifdef __cplusplus } diff --git a/include/mgos_bt_gap.h b/include/mgos_bt_gap.h index 357fefb..2a83ee8 100644 --- a/include/mgos_bt_gap.h +++ b/include/mgos_bt_gap.h @@ -94,6 +94,12 @@ bool mgos_bt_gap_set_scan_rsp_data(struct mg_str scan_rsp_data); bool mgos_bt_gap_get_adv_enable(void); bool mgos_bt_gap_set_adv_enable(bool adv_enable); +int mgos_bt_gap_get_num_paired_devices(void); + +void mgos_bt_gap_remove_paired_device(const struct mgos_bt_addr addr); + +void mgos_bt_gap_remove_all_paired_devices(void); + #ifdef __cplusplus } #endif diff --git a/mos.yml b/mos.yml index 2291c3d..915094d 100644 --- a/mos.yml +++ b/mos.yml @@ -28,6 +28,27 @@ config_schema: - ["bt.gatts.min_sec_level", "i", 0, {title: "0 - no auth required, 1 - encryption reqd, 2 - encryption + MITM reqd"}] - ["bt.gatts.require_pairing", "b", false, {title: "Require device to be paired before accessing services"}] +conds: + - when: mos.platform == "esp32" + apply: + build_vars: + ESP_IDF_EXTRA_COMPONENTS: "${build_vars.ESP_IDF_EXTRA_COMPONENTS} bt esp_wifi wpa_supplicant" + ESP_IDF_SDKCONFIG_OPTS: > + ${build_vars.ESP_IDF_SDKCONFIG_OPTS} + CONFIG_BT_ENABLED=y + CONFIG_BT_NIMBLE_ENABLED=y + CONFIG_BT_NIMBLE_ROLE_BROADCASTER=n + CONFIG_BT_NIMBLE_ROLE_OBSERVER=n + CONFIG_BT_NIMBLE_ROLE_CENTRAL=n + CONFIG_BT_NIMBLE_ROLE_PERIPHERAL=y + CONFIG_BT_NIMBLE_MAX_CONNECTIONS=7 + CONFIG_BT_NIMBLE_DEBUG=n + CONFIG_BT_NIMBLE_TASK_STACK_SIZE=6144 + CONFIG_BT_NIMBLE_L2CAP_COC_MAX_NUM=0 + CONFIG_BT_NIMBLE_GAP_DEVICE_NAME_MAX_LEN=50 + +# CONFIG_BT_NIMBLE_CRYPTO_STACK_MBEDTLS=y + tags: - bt - bluetooth diff --git a/mos_esp32.yml b/mos_esp32.yml deleted file mode 100644 index d824d49..0000000 --- a/mos_esp32.yml +++ /dev/null @@ -1,37 +0,0 @@ -build_vars: - ESP_IDF_EXTRA_COMPONENTS: "${build_vars.ESP_IDF_EXTRA_COMPONENTS} bt esp_wifi wpa_supplicant" - ESP_IDF_SDKCONFIG_OPTS: > - ${build_vars.ESP_IDF_SDKCONFIG_OPTS} - CONFIG_BT_ACL_CONNECTIONS=4 - CONFIG_BT_BLE_DYNAMIC_ENV_MEMORY=y - CONFIG_BT_BLE_SMP_ENABLE=y - CONFIG_BT_BLUEDROID_ENABLED=y - CONFIG_BT_BTC_TASK_STACK_SIZE=6144 - CONFIG_BT_CLASSIC_ENABLED=n - CONFIG_BT_DRAM_RELEASE=y - CONFIG_BT_ENABLED=y - CONFIG_BT_GATTS_ENABLE=y - CONFIG_BT_GATTC_ENABLE=y - CONFIG_BTDM_CONTROLLER_RUN_CPU=0 - CONFIG_BT_SMP_ENABLE=y - CONFIG_BT_RESERVE_DRAM=0x10000 - -# Control verbose debugging for various BT modules. -cdefs: - # A2D_INITIAL_TRACE_LEVEL: BT_TRACE_LEVEL_DEBUG - # APPL_INITIAL_TRACE_LEVEL: BT_TRACE_LEVEL_DEBUG - # AVCT_INITIAL_TRACE_LEVEL: BT_TRACE_LEVEL_DEBUG - # AVDT_INITIAL_TRACE_LEVEL: BT_TRACE_LEVEL_DEBUG - # AVRC_INITIAL_TRACE_LEVEL: BT_TRACE_LEVEL_DEBUG - # BNEP_INITIAL_TRACE_LEVEL: BT_TRACE_LEVEL_DEBUG - # BTM_INITIAL_TRACE_LEVEL: BT_TRACE_LEVEL_DEBUG - # GAP_INITIAL_TRACE_LEVEL: BT_TRACE_LEVEL_DEBUG - # GATT_INITIAL_TRACE_LEVEL: BT_TRACE_LEVEL_DEBUG - # HCI_INITIAL_TRACE_LEVEL: BT_TRACE_LEVEL_DEBUG - # HID_INITIAL_TRACE_LEVEL: BT_TRACE_LEVEL_DEBUG - # L2CAP_INITIAL_TRACE_LEVEL: BT_TRACE_LEVEL_DEBUG - # MCA_INITIAL_TRACE_LEVEL: BT_TRACE_LEVEL_DEBUG - # PAN_INITIAL_TRACE_LEVEL: BT_TRACE_LEVEL_DEBUG - # RFCOMM_INITIAL_TRACE_LEVEL: BT_TRACE_LEVEL_DEBUG - # SDP_INITIAL_TRACE_LEVEL: BT_TRACE_LEVEL_DEBUG - # SMP_INITIAL_TRACE_LEVEL: BT_TRACE_LEVEL_DEBUG diff --git a/src/esp32/esp32_bt.c b/src/esp32/esp32_bt.c index de8b718..60f4f67 100644 --- a/src/esp32/esp32_bt.c +++ b/src/esp32/esp32_bt.c @@ -22,19 +22,20 @@ #include #include -#include "esp_bt.h" -#include "esp_bt_defs.h" -#include "esp_bt_main.h" -#include "esp_gap_ble_api.h" -#include "esp_gatt_common_api.h" -#include "nvs.h" - #include "common/mg_str.h" #include "mgos_hal.h" #include "mgos_net.h" #include "mgos_sys_config.h" +#include "esp_nimble_hci.h" +#include "host/ble_hs.h" +#include "host/util/util.h" +#include "nimble/nimble_port.h" +#include "nimble/nimble_port_freertos.h" +#include "services/gap/ble_svc_gap.h" + + const char *esp32_bt_addr_to_str(const esp_bd_addr_t addr, char *out) { return mgos_bt_addr_to_str((const struct mgos_bt_addr *) &addr[0], 0, out); } @@ -75,38 +76,67 @@ static void mgos_bt_net_ev(int ev, void *evd, void *arg) { if (ev != MGOS_NET_EV_IP_ACQUIRED) return; LOG(LL_INFO, ("Network is up, disabling Bluetooth")); mgos_sys_config_set_bt_enable(false); +#if 0 // TODO char *msg = NULL; if (save_cfg(&mgos_sys_config, &msg)) { esp_bt_controller_disable(); esp_bt_controller_deinit(); esp_bt_controller_mem_release(ESP_BT_MODE_BLE); } +#endif (void) arg; } int mgos_bt_gap_get_num_paired_devices(void) { - return esp_ble_get_bond_device_num(); + int count = 0; + ble_store_util_count(BLE_STORE_OBJ_TYPE_OUR_SEC, &count); + return count; } -/* Workaround for https://github.com/espressif/esp-idf/issues/1406 */ bool esp32_bt_wipe_config(void) { - bool result = false; - nvs_handle h = 0; - /* CONFIG_FILE_PATH form btc_config.c */ - if (nvs_open("bt_config.conf", NVS_READWRITE, &h) != ESP_OK) goto clean; - if (nvs_erase_key(h, "bt_cfg_key") != ESP_OK) goto clean; - result = true; - -clean: - if (h != 0) nvs_close(h); - return result; + // TODO + return false; +} + +static void _on_reset(int reason) { + LOG(LL_ERROR, ("Resetting state; reason=%d", reason)); } +static void _on_sync(void) { + int rc; + + rc = ble_hs_util_ensure_addr(mgos_sys_config_get_bt_random_address()); + if (rc != 0) { + LOG(LL_ERROR, ("ble_hs_util_ensure_addr rc=%d", rc)); + return; + } + + uint8_t own_addr_type; + rc = ble_hs_id_infer_auto(0, &own_addr_type); + if (rc != 0) { + LOG(LL_ERROR, ("error determining address type; rc=%d", rc)); + return; + } + + uint8_t addr_val[6] = {0}; + rc = ble_hs_id_copy_addr(own_addr_type, addr_val, NULL); + char addr[18] = {0}; + LOG(LL_INFO, ("BLE Device Address: %s", esp32_bt_addr_to_str(addr_val, addr))); + + mgos_bt_gap_set_adv_enable(mgos_sys_config_get_bt_adv_enable()); +} + +static void ble_host_task(void *param) { + nimble_port_run(); + nimble_port_freertos_deinit(); +} + +extern void ble_store_config_init(void); + bool mgos_bt_common_init(void) { bool ret = false; if (!mgos_sys_config_get_bt_enable()) { LOG(LL_INFO, ("Bluetooth is disabled")); - esp_bt_controller_mem_release(ESP_BT_MODE_BTDM); return true; } @@ -117,38 +147,32 @@ bool mgos_bt_common_init(void) { free(dev_name); } - esp_bt_controller_mem_release(ESP_BT_MODE_CLASSIC_BT); - - esp_bt_controller_config_t bt_cfg = BT_CONTROLLER_INIT_CONFIG_DEFAULT(); - esp_err_t err = esp_bt_controller_init(&bt_cfg); + esp_err_t err = esp_nimble_hci_and_controller_init(); if (err) { LOG(LL_ERROR, ("BT init failed: %d", err)); goto out; } - err = esp_bt_controller_enable(ESP_BT_MODE_BLE); - if (err) { - LOG(LL_ERROR, ("BT enable failed: %d", err)); - goto out; - } - err = esp_bluedroid_init(); - if (err != ESP_OK) { - LOG(LL_ERROR, ("bluedroid init failed: %d", err)); - goto out; - } - err = esp_bluedroid_enable(); - if (err != ESP_OK) { - LOG(LL_ERROR, ("bluedroid enable failed: %d", err)); - goto out; - } + + nimble_port_init(); + + ble_hs_cfg.reset_cb = _on_reset; + ble_hs_cfg.sync_cb = _on_sync; + //ble_hs_cfg.gatts_register_cb = gatt_svr_register_cb; + ble_hs_cfg.store_status_cb = ble_store_util_status_rr; + + ble_hs_cfg.sm_sc = true; + ble_hs_cfg.sm_io_cap = BLE_SM_IO_CAP_NO_IO; + ble_hs_cfg.sm_bonding = mgos_sys_config_get_bt_allow_pairing(); + ble_hs_cfg.sm_mitm = true; + + ble_att_set_preferred_mtu(mgos_sys_config_get_bt_gatt_mtu()); if (!esp32_bt_gap_init()) { LOG(LL_ERROR, ("GAP init failed")); ret = false; goto out; } - - esp_ble_gatt_set_local_mtu(mgos_sys_config_get_bt_gatt_mtu()); - +#if 0 if (!esp32_bt_gattc_init()) { LOG(LL_ERROR, ("GATTC init failed")); ret = false; @@ -160,11 +184,15 @@ bool mgos_bt_common_init(void) { ret = false; goto out; } - +#endif if (!mgos_sys_config_get_bt_keep_enabled()) { mgos_event_add_group_handler(MGOS_EVENT_GRP_NET, mgos_bt_net_ev, NULL); } + ble_store_config_init(); + + nimble_port_freertos_init(ble_host_task); + LOG(LL_INFO, ("Bluetooth init ok, MTU %d, pairing %s, %d paired devices", mgos_sys_config_get_bt_gatt_mtu(), (mgos_bt_gap_get_pairing_enable() ? "enabled" : "disabled"), diff --git a/src/esp32/esp32_bt_gap.c b/src/esp32/esp32_bt_gap.c index df03e62..c01e395 100644 --- a/src/esp32/esp32_bt_gap.c +++ b/src/esp32/esp32_bt_gap.c @@ -17,14 +17,16 @@ #include "esp32_bt_gap.h" +#include "mgos.h" + +#include "host/ble_gap.h" +#include "services/gap/ble_svc_gap.h" + +#if 0 #include #include #include -#include "esp_bt.h" -#include "esp_bt_defs.h" -#include "esp_bt_device.h" -#include "esp_gap_ble_api.h" #include "nvs.h" #include "common/str_util.h" @@ -38,9 +40,13 @@ #include "esp32_bt_internal.h" +<<<<<<< HEAD static struct mg_str s_name = MG_NULL_STR; static bool s_adv_enable = false; static bool s_advertising = false; + +======= +>>>>>>> scratch static bool s_pairing_enable = false; static bool s_scanning = false; static int s_scan_duration_sec = 3; @@ -74,6 +80,7 @@ bool esp32_bt_is_scanning(void) { return s_scanning; } +<<<<<<< HEAD bool mgos_bt_gap_set_name(struct mg_str name) { mg_strfree(&s_name); s_name = mg_strdup_nul(name); @@ -118,15 +125,12 @@ bool mgos_bt_gap_set_scan_rsp_data(struct mg_str scan_rsp_data) { scan_rsp_data.len) == ESP_OK); } +======= +>>>>>>> scratch bool mgos_bt_gap_get_adv_enable(void) { return s_adv_enable; } -bool mgos_bt_gap_set_adv_enable(bool adv_enable) { - s_adv_enable = adv_enable; - return (s_adv_enable ? start_advertising() : stop_advertising()); -} - void esp32_bt_set_is_advertising(bool is_advertising) { s_advertising = is_advertising; } @@ -443,14 +447,184 @@ static void esp32_gap_ev_handler(esp_gap_ble_cb_event_t ev, } } } +#endif + +static int mgos_bt_gap_event(struct ble_gap_event *event, void *arg) { + struct ble_gap_conn_desc desc; + int rc; + + switch (event->type) { + case BLE_GAP_EVENT_CONNECT: + LOG(LL_INFO, ("CONNECT %d %u", event->connect.status, event->connect.conn_handle)); + if (event->connect.status == 0) { + rc = ble_gap_conn_find(event->connect.conn_handle, &desc); + assert(rc == 0); + bleprph_print_conn_desc(&desc); + } + MODLOG_DFLT(INFO, "\n"); -static void adv_enable_cb(void *arg) { - mgos_bt_gap_set_adv_enable(s_adv_enable); - (void) arg; + if (event->connect.status != 0) { + /* Connection failed; resume advertising. */ + bleprph_advertise(); + } + return 0; + + case BLE_GAP_EVENT_DISCONNECT: + MODLOG_DFLT(INFO, "disconnect; reason=%d ", event->disconnect.reason); + bleprph_print_conn_desc(&event->disconnect.conn); + MODLOG_DFLT(INFO, "\n"); + + /* Connection terminated; resume advertising. */ + bleprph_advertise(); + 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->conn_update.conn_handle, &desc); + assert(rc == 0); + bleprph_print_conn_desc(&desc); + MODLOG_DFLT(INFO, "\n"); + return 0; + + case BLE_GAP_EVENT_ADV_COMPLETE: + MODLOG_DFLT(INFO, "advertise complete; reason=%d", + event->adv_complete.reason); + bleprph_advertise(); + 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->enc_change.conn_handle, &desc); + assert(rc == 0); + bleprph_print_conn_desc(&desc); + 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); + 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: + ESP_LOGI(tag, "PASSKEY_ACTION_EVENT started \n"); + struct ble_sm_io pkey = {0}; + int key = 0; + + if (event->passkey.params.action == BLE_SM_IOACT_DISP) { + pkey.action = event->passkey.params.action; + pkey.passkey = 123456; // This is the passkey to be entered on peer + ESP_LOGI(tag, "Enter passkey %d on the peer side", pkey.passkey); + rc = ble_sm_inject_io(event->passkey.conn_handle, &pkey); + ESP_LOGI(tag, "ble_sm_inject_io result: %d\n", rc); + } else if (event->passkey.params.action == BLE_SM_IOACT_NUMCMP) { + ESP_LOGI(tag, "Passkey on device's display: %d", + event->passkey.params.numcmp); + ESP_LOGI(tag, + "Accept or reject the passkey through console in this format " + "-> key Y or key N"); + pkey.action = event->passkey.params.action; + if (scli_receive_key(&key)) { + pkey.numcmp_accept = key; + } else { + pkey.numcmp_accept = 0; + ESP_LOGE(tag, "Timeout! Rejecting the key"); + } + rc = ble_sm_inject_io(event->passkey.conn_handle, &pkey); + ESP_LOGI(tag, "ble_sm_inject_io result: %d\n", rc); + } else if (event->passkey.params.action == BLE_SM_IOACT_OOB) { + static uint8_t tem_oob[16] = {0}; + pkey.action = event->passkey.params.action; + for (int i = 0; i < 16; i++) { + pkey.oob[i] = tem_oob[i]; + } + rc = ble_sm_inject_io(event->passkey.conn_handle, &pkey); + ESP_LOGI(tag, "ble_sm_inject_io result: %d\n", rc); + } else if (event->passkey.params.action == BLE_SM_IOACT_INPUT) { + ESP_LOGI( + tag, + "Enter the passkey through console in this format-> key 123456"); + pkey.action = event->passkey.params.action; + if (scli_receive_key(&key)) { + pkey.passkey = key; + } else { + pkey.passkey = 0; + ESP_LOGE(tag, "Timeout! Passing 0 as the key"); + } + rc = ble_sm_inject_io(event->passkey.conn_handle, &pkey); + ESP_LOGI(tag, "ble_sm_inject_io result: %d\n", rc); + } + return 0; + } + + return 0; } -bool esp32_bt_gap_init(void) { - if (esp_ble_gap_register_callback(esp32_gap_ev_handler) != ESP_OK) { +void mgos_bt_gap_set_scan_rsp_data(const struct mg_str scan_rsp_data) { + ble_gap_adv_rsp_set_data((const uint8_t *) scan_rsp_data.p, + scan_rsp_data.len); +} + +static bool s_adv_enable = false; +static bool s_advertising = false; + +static bool start_advertising(void) { + int rc; + + if (s_advertising) return true; + if (!s_adv_enable) return false; + const char *dev_name = mgos_sys_config_get_bt_dev_name(); + if (dev_name == NULL) dev_name = mgos_sys_config_get_device_id(); + if (dev_name == NULL) { + LOG(LL_ERROR, ("bt.dev_name or device.id must be set")); + return false; + } + if (ble_svc_gap_device_name_set(dev_name) != 0) { + return false; + } + + struct ble_hs_adv_fields fields = { + .flags = 0, + + .name = (uint8_t *) dev_name, + .name_len = strlen(dev_name), + .name_is_complete = true, + + .tx_pwr_lvl = BLE_HS_ADV_TX_PWR_LVL_AUTO, + .tx_pwr_lvl_is_present = true, + }; + if ((rc = ble_gap_adv_set_fields(&fields)) != 0) { + LOG(LL_ERROR, ("ble_gap_adv_set_fields: %d", rc)); return false; } @@ -486,6 +660,48 @@ bool esp32_bt_gap_init(void) { return false; } + struct ble_gap_adv_params adv_params = { + .conn_mode = BLE_GAP_CONN_MODE_UND, + .disc_mode = BLE_GAP_DISC_MODE_GEN, + .itvl_min = BLE_GAP_ADV_FAST_INTERVAL2_MIN, + .itvl_max = BLE_GAP_ADV_FAST_INTERVAL2_MAX, + .channel_map = BLE_GAP_ADV_DFLT_CHANNEL_MAP, + }; + uint8_t own_addr_type; + if ((rc = ble_hs_id_infer_auto(0, &own_addr_type)) != 0) { + LOG(LL_ERROR, ("ble_hs_id_infer_auto: %d", rc)); + return false; + } + + if ((rc = ble_gap_adv_start(own_addr_type, NULL, BLE_HS_FOREVER, &adv_params, + mgos_bt_gap_event, NULL)) != 0) { + LOG(LL_ERROR, ("ble_hs_id_infer_auto: %d", rc)); + return false; + } + + char addr[BT_ADDR_STR_LEN] = {0}; + LOG(LL_INFO, ("BT device name %s, addr %s", dev_name, addr)); + // mgos_bt_addr_to_str(&la, MGOS_BT_ADDR_STRINGIFY_TYPE, addr))); + return true; +} + +static bool stop_advertising(void) { + if (!s_advertising) return true; + return (ble_gap_adv_stop() == 0); +} + +bool mgos_bt_gap_set_adv_enable(bool adv_enable) { + s_adv_enable = adv_enable; + return (s_adv_enable ? start_advertising() : stop_advertising()); +} + +bool esp32_bt_gap_init(void) { +#if 0 + if (esp_ble_gap_register_callback(esp32_gap_ev_handler) != ESP_OK) { + return false; + } + + mgos_bt_gap_set_pairing_enable(mgos_sys_config_get_bt_allow_pairing()); esp_ble_io_cap_t io_cap = ESP_IO_CAP_NONE; @@ -494,7 +710,6 @@ bool esp32_bt_gap_init(void) { uint8_t key_size = 16; esp_ble_gap_set_security_param(ESP_BLE_SM_MAX_KEY_SIZE, &key_size, sizeof(key_size)); - if (mgos_sys_config_get_bt_random_address()) { esp_ble_gap_config_local_privacy(true); s_adv_params.own_addr_type = BLE_ADDR_TYPE_RANDOM; @@ -507,6 +722,7 @@ bool esp32_bt_gap_init(void) { /* Delay until later, we've only just started the BT system and * sometimes this throws a "No random address yet" error. */ mgos_set_timer(100, 0, adv_enable_cb, NULL); +#endif return true; } diff --git a/src/esp32/esp32_bt_gattc.c b/src/esp32/esp32_bt_gattc.c.bak similarity index 100% rename from src/esp32/esp32_bt_gattc.c rename to src/esp32/esp32_bt_gattc.c.bak diff --git a/src/esp32/esp32_bt_gatts.c b/src/esp32/esp32_bt_gatts.c index a43b990..d2ce733 100644 --- a/src/esp32/esp32_bt_gatts.c +++ b/src/esp32/esp32_bt_gatts.c @@ -1,3 +1,4 @@ +#if 0 /* * Copyright (c) 2014-2018 Cesanta Software Limited * All rights reserved @@ -1315,3 +1316,4 @@ bool esp32_bt_gatts_init(void) { return (esp_ble_gatts_register_callback(esp32_bt_gatts_ev) == ESP_OK && esp_ble_gatts_app_register(0) == ESP_OK); } +#endif