From 3e4dd23f90f9214b0faca6e1071eafd08ca46d0b Mon Sep 17 00:00:00 2001 From: Philip Gladstone Date: Wed, 31 Jan 2024 13:14:42 -0500 Subject: [PATCH] Now runs on idf5 and you can start and stop the stack! --- components/modules/ble.c | 31 +++++-------------------------- docs/modules/ble.md | 11 +++-------- 2 files changed, 8 insertions(+), 34 deletions(-) diff --git a/components/modules/ble.c b/components/modules/ble.c index f165974b..64f628f1 100644 --- a/components/modules/ble.c +++ b/components/modules/ble.c @@ -77,14 +77,14 @@ static const char *gadget_name; static uint8_t *gadget_mfg; static size_t gadget_mfg_len; +static ble_uuid_t *cud_uuid = BLE_UUID16_DECLARE(0x2901); + static const struct ble_gatt_svc_def *gatt_svr_svcs; static const uint16_t *notify_handles; static task_handle_t task_handle; static QueueHandle_t response_queue; -static bool already_inited; - static int struct_pack_index; static int struct_unpack_index; @@ -588,9 +588,7 @@ lble_build_gatt_svcs(lua_State *L, struct ble_gatt_svc_def **resultp, const uint chr->descriptors = dsc; - dsc->uuid = (const ble_uuid_t*)(ble_uuid16_t[]) { - BLE_UUID16_INIT(0x2901) - }; + dsc->uuid = cud_uuid; dsc->att_flags = BLE_ATT_F_READ; dsc->access_cb = lble_access_cb; dsc->arg = chr->arg; @@ -666,6 +664,8 @@ lble_sys_init(lua_State *L) { task_handle = task_get_id(lble_task_cb); response_queue = xQueueCreate(2, sizeof(response_message_t)); + esp_log_level_set("NimBLE", ESP_LOG_WARN); + return 0; } @@ -678,15 +678,6 @@ lble_host_task(void *param) static void lble_init_stack(lua_State *L) { - static char stack_inited; - if (!stack_inited) { - stack_inited = 1; - int ret = esp_nimble_hci_and_controller_init(); - if (ret != ESP_OK) { - luaL_error(L, "esp_nimble_hci_and_controller_init() failed with error: %d", ret); - return; - } - } nimble_port_init(); @@ -994,10 +985,6 @@ static int lble_init(lua_State *L) { if (inited != STOPPED) { return luaL_error(L, "ble is already running"); } - if (already_inited) { - return luaL_error(L, "Can only call ble.init once. Internal stack problem."); - } - already_inited = true; if (!struct_pack_index) { lua_getglobal(L, "struct"); lua_getfield(L, -1, "pack"); @@ -1066,10 +1053,6 @@ static int lble_init(lua_State *L) { } static int lble_shutdown(lua_State *L) { - // It seems that shutting down the stack corrupts some critical data structures - // so, for now, don't allow it. - luaL_error(L, "Shutting down the BLE stack is currently not possible"); - inited = SHUTTING; ble_gap_adv_stop(); @@ -1080,10 +1063,6 @@ static int lble_shutdown(lua_State *L) { nimble_port_deinit(); - if (ESP_OK != esp_nimble_hci_and_controller_deinit()) { - return luaL_error(L, "Failed to shutdown the BLE controller"); - } - inited = STOPPED; return 0; diff --git a/docs/modules/ble.md b/docs/modules/ble.md index 8fab2bd9..d6cd31c8 100644 --- a/docs/modules/ble.md +++ b/docs/modules/ble.md @@ -11,10 +11,6 @@ This allows you to build simple gadgets that can be interrogated and controlled This initializes the Bluetooth stack and starts advertising according to the data in the configuration table. See below for a detailed description of this table. -At the present time, you can only call the `init` function once. There is some problem -in the underlying implementation of the BLE stack that prevents a `init`, `shutdown`, `init` -sequence from working. - #### Syntax `ble.init(ble_config)` @@ -65,7 +61,7 @@ Updates the advertising data field for future advertising frames. #### Parameters -- `advertisement` This string will be placed in future advertising frames as the manufacturer data field. This overrides the a`advertisement` value from the config block. +- `advertisement` This string will be placed in future advertising frames as the manufacturer data field. This overrides the `advertisement` value from the config block. #### Returns `nil` @@ -77,8 +73,7 @@ ble.advertise("foo") ## ble.shutdown() -Shuts down the Bluetooth controller and returns it to the state where another `init` ought to work (but currently doesn't). And, at the moment, shutting -it down doesn't work either -- it appears to corrupt some deep data structures. +Shuts down the Bluetooth controller and returns it to the state where another `init` ought to work. #### Syntax `ble.shutdown()` @@ -153,4 +148,4 @@ end ### Type conversions -If the `type` value converts a single item, then that will be the value that is placed into the `value` element. If it converts multiple elements, then the elements will be placed into an array that that will be plaed into the `value` element. +If the `type` value converts a single item, then that will be the value that is placed into the `value` element. If it converts multiple elements, then the elements will be placed into an array that that will be placed into the `value` element.