Now correctly supports adding User Description desriptors

This commit is contained in:
Philip Gladstone 2024-01-15 11:44:00 -05:00
parent aaaa440d16
commit 55ab2fa7e9
2 changed files with 9 additions and 8 deletions

View File

@ -260,7 +260,7 @@ lble_access_cb(uint16_t conn_handle, uint16_t attr_handle, struct ble_gatt_acces
int rc = BLE_ATT_ERR_UNLIKELY;
if (ctxt->op == BLE_GATT_ACCESS_OP_READ_CHR ||
ctxt->op == BLE_GATT_ACCESS_OP_READ_CHR) {
ctxt->op == BLE_GATT_ACCESS_OP_READ_DSC) {
rc = message.errcode;
if (rc == 0) {
if (os_mbuf_append(ctxt->om, message.buffer, message.length)) {
@ -343,7 +343,7 @@ lble_task_cb(task_param_t param, task_prio_t prio) {
}
if (task_block->ctxt->op == BLE_GATT_ACCESS_OP_READ_DSC) {
lua_getfield(L, -2, "name");
lua_getfield(L, -2, "description");
// Now we have the name (-1), struct (-2), table (-3)
data = lua_tolstring(L, -1, &datalen);
}
@ -460,7 +460,7 @@ lble_build_gatt_svcs(lua_State *L, struct ble_gatt_svc_def **resultp, const uint
// Now count the number of descriptors that we need
for (int j = 1; j <= sccnt; j++) {
lua_geti(L, -1, j);
if (lua_getfield(L, -1, "name") != LUA_TNIL) {
if (lua_getfield(L, -1, "description") != LUA_TNIL) {
nd++;
}
lua_pop(L, 2);
@ -575,11 +575,12 @@ lble_build_gatt_svcs(lua_State *L, struct ble_gatt_svc_def **resultp, const uint
}
lua_pop(L, 1);
lua_pushvalue(L, -1); // duplicate the characteristic
// -1 is now the characteristic again
chr->arg = (void *) luaL_ref(L, LUA_REGISTRYINDEX);
chr->access_cb = lble_access_cb;
if (lua_getfield(L, -1, "name") != LUA_TNIL) {
if (lua_getfield(L, -1, "description") != LUA_TNIL) {
if ((void *) (dsc + 2) > eom) {
free_gatt_svcs(L, result);
return luaL_error(L, "Miscalculated memory requirements");
@ -596,7 +597,7 @@ lble_build_gatt_svcs(lua_State *L, struct ble_gatt_svc_def **resultp, const uint
dsc += 2;
}
lua_pop(L, 1);
lua_pop(L, 2);
}
lua_pop(L, 2);
chrs++; // terminate the list of characteristics for this service

View File

@ -30,8 +30,8 @@ function read_battery_level()
-- This ought to do something better!
return 50
end
battery = { uuid="180f", characteristics={ {uuid="2a19", type='B', read=read_battery_level, name="Battery percentage"} } }
myservice = {uuid="0123456789abcdef0123456789abcdef", characteristics={{uuid="1234", value=0, type='c'}}}
battery = { uuid="180f", characteristics={ {uuid="2a19", type='B', read=read_battery_level, description="Battery percentage"} } }
myservice = {uuid="0123456789abcdef0123456789abcdef", characteristics={{uuid="1234", value=0, type='c', description='Testing123'}}}
config = {name="MyGadget", services={ myservice, battery } }
ble.init(config)
```
@ -124,7 +124,7 @@ The characteristic table contains the following keys:
- `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)
- `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()`
- `name` If this attribute is present, then an additional descriptor is added containing the name of the characteristic.
- `description` If this attribute is present, then an additional descriptor is added containing the description of the characteristic.
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.