Skip to content

Commit

Permalink
Now runs on idf5 and you can start and stop the stack!
Browse files Browse the repository at this point in the history
  • Loading branch information
pjsg committed Jan 31, 2024
1 parent 8cafd55 commit 3e4dd23
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 34 deletions.
31 changes: 5 additions & 26 deletions components/modules/ble.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}

Expand All @@ -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();

Expand Down Expand Up @@ -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");
Expand Down Expand Up @@ -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();
Expand All @@ -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;
Expand Down
11 changes: 3 additions & 8 deletions docs/modules/ble.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)`

Expand Down Expand Up @@ -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`
Expand All @@ -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()`
Expand Down Expand Up @@ -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.

0 comments on commit 3e4dd23

Please sign in to comment.