Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add BLE GAP/GATT support to the dev-esp32 branch #3473

Open
wants to merge 32 commits into
base: dev-esp32
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 4 commits
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
2cf72b4
It compiles!!
pjsg Oct 16, 2021
4a55397
Seems to sort of work.
pjsg Oct 16, 2021
f8709b9
Fixed the docs and add support for advertising data
pjsg Oct 17, 2021
0a4253a
Ensure that BT and STRUCT are enabled. Can't figure out how to enforc…
pjsg Oct 17, 2021
06becd1
Try and get shutdown to work
pjsg Oct 18, 2021
69405ce
Paritally working
pjsg Oct 18, 2021
e803610
Now actually gets started
pjsg Oct 19, 2021
28cb898
Making progress on ble
pjsg Oct 19, 2021
96993ef
FIx the advertising start
pjsg Oct 21, 2021
d204d33
Use the hardware random number generator
pjsg Oct 21, 2021
6b55c39
Merge remote-tracking branch 'origin/dev-esp32-idf4' into ble
pjsg Oct 21, 2021
52562a6
Got rid of the random printfs
pjsg Oct 24, 2021
7b6a85b
Remove trailing spaces
pjsg Oct 24, 2021
3f33027
See if this fixes the cross compiles
pjsg Oct 24, 2021
b5c4082
Allow dynamic update of advertisements
pjsg Oct 26, 2021
4109e30
Prevent ble.shutdown() as it corrupts something...
pjsg Nov 5, 2021
7e123b2
Apply suggestions from code review
pjsg Nov 9, 2021
7aa2233
Add support for notify (untested)
pjsg Jan 6, 2022
13e1dad
Update the docs to match the code
pjsg Jan 6, 2022
adc188b
Merge remote-tracking branch 'origin/dev-esp32-idf4' into ble
pjsg Jan 7, 2022
4d24232
Notify seems to work now.
pjsg Jan 8, 2022
225217c
Updated the Kconfig to note that you have to enable the Nimble module…
pjsg Feb 20, 2022
f18e9d5
Merge remote-tracking branch 'origin/dev-esp32-idf4' into ble
pjsg Mar 5, 2022
891cf01
Review comments
pjsg Mar 5, 2022
3e5ba28
Moved the flash init logic
pjsg Mar 6, 2022
1c26ba3
FIx missing } in an example
pjsg Mar 6, 2022
9483312
Merge remote-tracking branch 'origin/dev-esp32' into ble
pjsg Jan 14, 2024
aaaa440
First attempt to add name support to characteristics
pjsg Jan 15, 2024
55ab2fa
Now correctly supports adding User Description desriptors
pjsg Jan 15, 2024
8cafd55
Merge remote-tracking branch 'origin/dev-esp32' into ble
pjsg Jan 31, 2024
3e4dd23
Now runs on idf5 and you can start and stop the stack!
pjsg Jan 31, 2024
0b6205b
Fix memory leak now that we can shutdown the stack
pjsg Feb 1, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion components/base_nodemcu/user_main.c
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,13 @@ void __attribute__((noreturn)) app_main(void)

nodemcu_init ();

nvs_flash_init ();
// This is the standard flash init sequence
int rc = nvs_flash_init();
if (rc == ESP_ERR_NVS_NO_FREE_PAGES || rc == ESP_ERR_NVS_NEW_VERSION_FOUND) {
nvs_flash_erase();
nvs_flash_init();
}

esp_netif_init ();

start_lua ();
Expand Down
35 changes: 12 additions & 23 deletions components/modules/ble.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@

#include "sdkconfig.h"
#ifdef CONFIG_NODEMCU_CMODULE_BLE
pjsg marked this conversation as resolved.
Show resolved Hide resolved
#include "nvs_flash.h"
#include <assert.h>
#include <errno.h>

Expand All @@ -34,6 +33,10 @@
#include <esp_log.h>
#define TAG "ble"

#ifndef CONFIG_BT_NIMBLE_ENABLED
#error You must enable NIMBLE if you want the Lua ble module. Hopefully this can be made automatic some day.
#endif

/* BLE */
#include "esp_nimble_hci.h"
#include "nimble/nimble_port.h"
Expand Down Expand Up @@ -196,7 +199,8 @@ free_gatt_svcs(lua_State *L, const struct ble_gatt_svc_def * svcs) {

static int
lble_access_cb(uint16_t conn_handle, uint16_t attr_handle, struct ble_gatt_access_ctxt *ctxt, void *arg) {
// Actually the only thing we care about is the arg and the ctxt
UNUSED(conn_handle);
UNUSED(attr_handle);

size_t task_block_size = sizeof(task_block_t);

Expand All @@ -220,6 +224,7 @@ lble_access_cb(uint16_t conn_handle, uint16_t attr_handle, struct ble_gatt_acces
task_block->length = OS_MBUF_PKTLEN(ctxt->om);
uint16_t outlen;
if (ble_hs_mbuf_to_flat(ctxt->om, task_block->buffer, task_block->length, &outlen)) {
free(task_block);
return BLE_ATT_ERR_UNLIKELY;
pjsg marked this conversation as resolved.
Show resolved Hide resolved
}
}
Expand All @@ -232,7 +237,7 @@ lble_access_cb(uint16_t conn_handle, uint16_t attr_handle, struct ble_gatt_acces
response_message_t message;

while (1) {
if (xQueueReceive(response_queue, &message, (TickType_t) (10000/portTICK_PERIOD_MS) ) != pdPASS) {
if (xQueueReceive(response_queue, &message, (TickType_t) (2000/portTICK_PERIOD_MS) ) != pdPASS) {
free(task_block);
return BLE_ATT_ERR_UNLIKELY;
}
Expand Down Expand Up @@ -270,6 +275,8 @@ lble_task_cb(task_param_t param, task_prio_t prio) {
message.errcode = BLE_ATT_ERR_UNLIKELY;

lua_State *L = lua_getstate();
int top = lua_gettop(L);

lua_rawgeti(L, LUA_REGISTRYINDEX, (int) task_block->arg);
// Now we have the characteristic table in -1
lua_getfield(L, -1, "type");
Expand Down Expand Up @@ -391,12 +398,12 @@ lble_task_cb(task_param_t param, task_prio_t prio) {
} else {
lua_pop(L, 1); // Throw away the null write pointer
}
lua_pop(L, 1); // THrow away the value
lua_pop(L, 1); // Throw away the value
message.errcode = 0;
}

cleanup:
lua_pop(L, 2);
lua_settop(L, top);
message.seqno = task_block->seqno;

xQueueSend(response_queue, &message, (TickType_t) 0);
Expand Down Expand Up @@ -563,7 +570,6 @@ gatt_svr_init(lua_State *L) {

struct ble_gatt_svc_def *svcs = NULL;
lble_build_gatt_svcs(L, &svcs, &notify_handles);
//free_gatt_svcs(L, gatt_svr_svcs);
gatt_svr_svcs = svcs;

rc = ble_gatts_count_cfg(gatt_svr_svcs);
Expand Down Expand Up @@ -608,18 +614,6 @@ lble_print_conn_desc(struct ble_gap_conn_desc *desc)

static int
lble_sys_init(lua_State *L) {
int rc = nvs_flash_init();
if (rc == ESP_ERR_NVS_NO_FREE_PAGES || rc == ESP_ERR_NVS_NEW_VERSION_FOUND) {
rc = nvs_flash_erase();
if (rc) {
return luaL_error(L, "Failed to erase flash: %d", rc);
}
rc = nvs_flash_init();
}
if (rc) {
return luaL_error(L, "Failed to init flash: %d", rc);
}

task_handle = task_get_id(lble_task_cb);
response_queue = xQueueCreate(2, sizeof(response_message_t));

Expand Down Expand Up @@ -944,11 +938,6 @@ static int lble_notify(lua_State *L) {

ble_gatts_chr_updated(notify_handles[handle]);

/*
if (rc) {
return luaL_error(L, "Must supply a valid handle");
}
*/
return 0;
}

Expand Down
2 changes: 0 additions & 2 deletions components/modules/ledc.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,6 @@ static int lledc_new_channel( lua_State *L )

ledc_timer.timer_num = opt_checkint_range(L, "timer", -1, 0, LEDC_TIMER_MAX-1);

ledc_timer.clk_cfg = LEDC_AUTO_CLK;
pjsg marked this conversation as resolved.
Show resolved Hide resolved

/* Setup channel */
ledc_channel_config_t channel_config = {
.speed_mode = ledc_timer.speed_mode,
Expand Down
10 changes: 6 additions & 4 deletions docs/modules/ble.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@ function read_battery_level()
-- This ought to do something better!
return 50
end
local battery = { uuid="180f", characteristics={ {uuid="2a19", type='B', read=read_battery_level} } }
local myservice = {uuid="0123456789abcdef0123456789abcdef", characteristics={{uuid="1234", value=0, type='c'}}}
local config = {name="MyGadget=", services={ myservice, battery }
battery = { uuid="180f", characteristics={ {uuid="2a19", type='B', read=read_battery_level} } }
myservice = {uuid="0123456789abcdef0123456789abcdef", characteristics={{uuid="1234", value=0, type='c'}}}
config = {name="MyGadget", services={ myservice, battery } }
ble.init(config)
```

Expand Down Expand Up @@ -120,11 +120,13 @@ The characteristic table contains the following keys:

- `uuid` The UUID of the characteristics. This can be either a 16 byte string or a 2 byte string that identifies the particular characteristic. Typically, 2 byte strings are used for well-known characteristics.
- `type` This is the optional type of the value. It has the same value as a unpack code in the `struct` module.
- `value` This is the actual value of the characteristic. This will be a string of bytes unless a `type` value is set.
- `value` This is the actual value of the characteristic. This will be a string of bytes (unless `type` is set).
- `read` This is a function that will be invoked to read the value (and so does not need the `value` entry). It should return a string of bytes (unless `type` is set).
- `write` This is a function that will be invoked to write the value (and so does not need the `value` entry). It is given a string of bytes (unless `type` is set)
pjsg marked this conversation as resolved.
Show resolved Hide resolved
- `notify` If this attribute is present then notifications are supported on this characteristic. The value of the `notify` attribute is updated to be an integer which is the value to be passed into `ble.notify()`

In the above functions, the value is that passed to/from the write/read functions is of the type specified by the `type` key. If this key is missing, then the default type is a string of bytes. For example, if `type` is `'B'` then the value is an integer (in the range 0 - 255) and the bluetooth client will see a single byte containing that value.

If the `value` key is present, then the characteristic is read/write. However, if one or `read` or `write` is set to `true`, then it restricts access to that mode.

The characteristics are treated as read/write unless only one of the `read` or `write` keys is present and the `value` key is not specified.
Expand Down
2 changes: 2 additions & 0 deletions install.sh
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#!/bin/bash

set -e

echo "Installing IDF prerequisites..."

IDF_DIR=./sdk/esp32-esp-idf
Expand Down