8000 Merge pull request #1 from NiekIlmer/dynamic_gatt · NiekIlmer/micropython@81a19a8 · GitHub
[go: up one dir, main page]

Skip to content

Commit 81a19a8

Browse files
authored
Merge pull request #1 from NiekIlmer/dynamic_gatt
2 parents 47dad5f + b8ac7ac commit 81a19a8

File tree

7 files changed

+149
-16
lines changed

7 files changed

+149
-16
lines changed

ports/zephyr/boards/da14695_pico.conf

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
CONFIG_NETWORKING=n
2+
CONFIG_BT=y
3+
CONFIG_BT_DEVICE_NAME_DYNAMIC=y
4+
CONFIG_BT_PERIPHERAL=y
5+
CONFIG_BT_CENTRAL=y
6+
CONFIG_BT_ATT_ENFORCE_FLOW=n
7+
CONFIG_USB_DEVICE_STACK=y
8+
CONFIG_USB_DEVICE_PRODUCT="DA14695MOD Pico"
9+
CONFIG_USB_DEVICE_PID=0x0004
10+
CONFIG_USB_DEVICE_INITIALIZE_AT_BOOT=n
11+
CONFIG_CONSOLE_GETCHAR_BUFSIZE=2048
12+
CONFIG_CONSOLE_PUTCHAR_BUFSIZE=2048
13+
CONFIG_BT_GATT_DYNAMIC_DB=y
14+
15+
CONFIG_SERIAL=y
16+
CONFIG_CONSOLE=y
17+
CONFIG_UART_CONSOLE=y
18+
CONFIG_UART_LINE_CTRL=y
19+
20+
CONFIG_MICROPY_HEAP_SIZE=98304
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
2+
/ {
3+
chosen {
4+
zephyr,console = &cdc_acm_uart0;
5+
};
6+
};
7+
8+
&zephyr_udc0 {
9+
cdc_acm_uart0: cdc_acm_uart0 {
10+
compatible = "zephyr,cdc-acm-uart";
11+
};
12+
};

ports/zephyr/boards/da1469x_dk_pro.conf

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,7 @@ CONFIG_BT=y
33
CONFIG_BT_DEVICE_NAME_DYNAMIC=y
44
CONFIG_BT_PERIPHERAL=y
55
CONFIG_BT_CENTRAL=y
6+
CONFIG_BT_ATT_ENFORCE_FLOW=n
7+
CONFIG_BT_GATT_DYNAMIC_DB=y
68

79
CONFIG_MICROPY_HEAP_SIZE=98304

ports/zephyr/main.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ STATIC void vfs_init(void) {
106106
mp_obj_t args[] = { mp_obj_new_str(CONFIG_SDMMC_VOLUME_NAME, strlen(CONFIG_SDMMC_VOLUME_NAME)) };
107107
bdev = MP_OBJ_TYPE_GET_SLOT(&zephyr_disk_access_type, make_new)(&zephyr_disk_access_type, ARRAY_SIZE(args), 0, args);
108108
mount_point_str = "/sd";
109-
#elif defined(CONFIG_FLASH_MAP) && FIXED_PARTITION_EXISTS(storage_partition)
109+
#elif defined(CONFIG_FLASH_MAP) && DT_HAS_FIXED_PARTITION_LABEL(storage)
110110
mp_obj_t args[] = { MP_OBJ_NEW_SMALL_INT(FLASH_AREA_ID(storage)), MP_OBJ_NEW_SMALL_INT(4096) };
111111
bdev = MP_OBJ_TYPE_GET_SLOT(&zephyr_flash_area_type, make_new)(&zephyr_flash_area_type, ARRAY_SIZE(args), 0, args);
112112
mount_point_str = "/flash";

ports/zephyr/modbluetooth_zephyr.c

Lines changed: 103 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@
3333

3434
#include <zephyr/bluetooth/bluetooth.h>
3535
#include <zephyr/bluetooth/hci.h>
36+
#include <zephyr/bluetooth/gatt.h>
37+
#include <zephyr/bluetooth/uuid.h>
3638
#include "extmod/modbluetooth.h"
3739

3840
#define DEBUG_printf(...) // printk("BLE: " __VA_ARGS__)
@@ -279,19 +281,116 @@ int mp_bluetooth_gatts_register_service_begin(bool append) {
279281
// Don't support append yet (modbluetooth.c doesn't support it yet anyway).
280282
return MP_EOPNOTSUPP;
281283
}
282-
283284
// Reset the gatt characteristic value db.
284285
mp_bluetooth_gatts_db_reset(MP_STATE_PORT(bluetooth_zephyr_root_pointers)->gatts_db);
285286

286-
return MP_EOPNOTSUPP;
287+
return 0;
287288
}
288289

289290
int mp_bluetooth_gatts_register_service_end(void) {
290-
return MP_EOPNOTSUPP;
291+
return 0;
292+
}
293+
// struct bt_gatt_service {
294+
// /** Service Attributes */
295+
// struct bt_gatt_attr *attrs;
296+
// /** Service Attribute count */
297+
// size_t attr_count;
298+
299+
// sys_snode_t node;
300+
// };
301+
// struct bt_gatt_attr {
302+
// /** Attribute UUID */
303+
// const struct bt_uuid *uuid;
304+
// bt_gatt_attr_read_func_t read;
305+
// /** Attribute write callback */
306+
// bt_gatt_attr_write_func_t write;
307+
// /** Attribute user data */
308+
// void *user_data;
309+
// /** Attribute handle */
310+
// uint16_t handle;
311+
// /** @brief Attribute permissions.
312+
// *
313+
// * Will be 0 if returned from bt_gatt_discover().
314+
// */
315+
// uint16_t perm;
316+
// };
317+
318+
// Note: modbluetooth UUIDs store their data in LE.
319+
STATIC void *create_zephyr_uuid(const mp_obj_bluetooth_uuid_t *uuid) {
320+
if (uuid->type == MP_BLUETOOTH_UUID_TYPE_16) {
321+
struct bt_uuid_16 *result = m_new(struct bt_uuid_16, 1);
322+
result->uuid.type = BT_UUID_TYPE_16;
323+
result->val = (uuid->data[1] << 8) | uuid->data[0];
324+
return (void *)result;
325+
} else if (uuid->type == MP_BLUETOOTH_UUID_TYPE_32) {
326+
struct bt_uuid_32 *result = m_new(struct bt_uuid_32, 1);
327+
result->uuid.type = BT_UUID_TYPE_32;
328+
result->val = (uuid->data[1] << 24) | (uuid->data[1] << 16) | (uuid->data[1] << 8) | uuid->data[0];
329+
return (void *)result;
330+
} else if (uuid->type == MP_BLUETOOTH_UUID_TYPE_128) {
331+
struct bt_uuid_128 *result = m_new(struct bt_uuid_128, 1);
332+
result->uuid.type = BT_UUID_TYPE_128;
333+
memcpy(result->val, uuid->data, 16);
334+
return (void *)result;
335+
} else {
336+
return NULL;
337+
}
291338
}
292339

293340
int mp_bluetooth_gatts_register_service(mp_obj_bluetooth_uuid_t *service_uuid, mp_obj_bluetooth_uuid_t **characteristic_uuids, uint16_t *characteristic_flags, mp_obj_bluetooth_uuid_t **descriptor_uuids, uint16_t *descriptor_flags, uint8_t *num_descriptors, uint16_t *handles, size_t num_characteristics) {
294-
return MP_EOPNOTSUPP;
341+
struct bt_gatt_service svc = {0};
342+
svc.attr_count = num_characteristics;
343+
svc.attrs = m_new(struct bt_gatt_attr, num_characteristics + 1);
344+
size_t handle_index = 0;
345+
size_t descriptor_index = 0;
346+
347+
for (size_t i = 0; i < num_characteristics; ++i) {
348+
svc.attrs[i].uuid = create_zephyr_uuid(characteristic_uuids[i]);
349+
//svc.attrs[i].read = characteristic_access_cb;
350+
//svc.attrs[i].write = characteristic_access_cb;
351+
svc.attrs[i].user_data = NULL;
352+
// NimBLE flags match the MP_BLUETOOTH_CHARACTERISTIC_FLAG_ ones exactly (including the security/privacy options).
353+
svc.attrs[i].perm = characteristic_flags[i];
354+
//characteristics[i].min_key_size = 0;
355+
svc.attrs[i].handle = handles[handle_index];
356+
++handle_index;
357+
358+
// if (num_descriptors[i] == 0) {
359+
// characteristics[i].descriptors = NULL;
360+
// } else {
361+
// struct ble_gatt_dsc_def *descriptors = m_new(struct ble_gatt_dsc_def, num_descriptors[i] + 1);
362+
363+
// for (size_t j = 0; j < num_descriptors[i]; ++j) {
364+
// descriptors[j].uuid = create_nimble_uuid(descriptor_uuids[descriptor_index], NULL);
365+
// descriptors[j].access_cb = characteristic_access_cb;
366+
// // NimBLE doesn't support security/privacy options on descriptors.
367+
// descriptors[j].att_flags = (uint8_t)descriptor_flags[descriptor_index];
368+
// descriptors[j].min_key_size = 0;
369+
// // Unlike characteristic, Nimble doesn't provide an au 10000 tomatic way to remember the handle, so use the arg.
370+
// descriptors[j].arg = &handles[handle_index];
371+
// ++descriptor_index;
372+
// ++handle_index;
373+
// }
374+
// descriptors[num_descriptors[i]].uuid = NULL; // no more descriptors
375+
376+
// characteristics[i].descriptors = descriptors;
377+
// }
378+
}
379+
// characteristics[num_characteristics].uuid = NULL; // no more characteristics
380+
381+
// struct ble_gatt_svc_def *service = m_new(struct ble_gatt_svc_def, 2);
382+
// service[0].type = BLE_GATT_SVC_TYPE_PRIMARY;
383+
// service[0].uuid = create_nimble_uuid(service_uuid, NULL);
384+
// service[0].includes = NULL;
385+
// service[0].characteristics = characteristics;
386+
// service[1].type = 0; // no more services
387+
388+
int ret = bt_gatt_service_register(&svc);
389+
if (ret != 0) {
390+
return bt_err_to_errno(ret);
391+
}
392+
393+
return 0;
295394
}
296395

297396
int mp_bluetooth_gap_disconnect(uint16_t conn_handle) {

ports/zephyr/mpconfigport.h

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -46,22 +46,22 @@
4646
#define MICROPY_KBD_EXCEPTION (1)
4747
#define MICROPY_CPYTHON_COMPAT (0)
4848
#define MICROPY_PY_ASYNC_AWAIT (0)
49-
#define MICROPY_PY_ATTRTUPLE (0)
49+
#define MICROPY_PY_ATTRTUPLE (1)
5050
#define MICROPY_PY_BUILTINS_BYTES_HEX (1)
51-
#define MICROPY_PY_BUILTINS_ENUMERATE (0)
52-
#define MICROPY_PY_BUILTINS_FILTER (0)
53-
#define MICROPY_PY_BUILTINS_MIN_MAX (0)
51+
#define MICROPY_PY_BUILTINS_ENUMERATE (1)
52+
#define MICROPY_PY_BUILTINS_FILTER (1)
53+
#define MICROPY_PY_BUILTINS_MIN_MAX (1)
54 F62C 54
#define MICROPY_PY_BUILTINS_PROPERTY (1)
55-
#define MICROPY_PY_BUILTINS_RANGE_ATTRS (0)
56-
#define MICROPY_PY_BUILTINS_REVERSED (0)
57-
#define MICROPY_PY_BUILTINS_SET (0)
58-
#define MICROPY_PY_BUILTINS_STR_COUNT (0)
55+
#define MICROPY_PY_BUILTINS_RANGE_ATTRS (1)
56+
#define MICROPY_PY_BUILTINS_REVERSED (1)
57+
#define MICROPY_PY_BUILTINS_SET (1)
58+
#define MICROPY_PY_BUILTINS_STR_COUNT (1)
5959
#define MICROPY_PY_BUILTINS_MEMORYVIEW (1)
6060
#define MICROPY_PY_BUILTINS_HELP (1)
6161
#define MICROPY_PY_BUILTINS_HELP_TEXT zephyr_help_text
6262
#define MICROPY_PY_ARRAY (1)
63-
#define MICROPY_PY_COLLECTIONS (0)
64-
#define MICROPY_PY_CMATH (0)
63+
#define MICROPY_PY_COLLECTIONS (1)
64+
#define MICROPY_PY_CMATH (1)
6565
#define MICROPY_PY_IO (0)
6666
#define MICROPY_PY_MICROPYTHON_MEM_INFO (1)
6767
#define MICROPY_PY_MACHINE (1)

ports/zephyr/zephyr_storage.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -244,7 +244,7 @@ STATIC const mp_rom_map_elem_t zephyr_flash_area_locals_dict_table[] = {
244244
{ MP_ROM_QSTR(MP_QSTR_readblocks), MP_ROM_PTR(&zephyr_flash_area_readblocks_obj) },
245245
{ MP_ROM_QSTR(MP_QSTR_writeblocks), MP_ROM_PTR(&zephyr_flash_area_writeblocks_obj) },
246246
{ MP_ROM_QSTR(MP_QSTR_ioctl), MP_ROM_PTR(&zephyr_flash_area_ioctl_obj) },
247-
#if FIXED_PARTITION_EXISTS(storage_partition)
247+
#if DT_HAS_FIXED_PARTITION_LABEL(storage)
248248
{ MP_ROM_QSTR(MP_QSTR_STORAGE), MP_ROM_INT(FLASH_AREA_ID(storage)) },
249249
#endif
250250
};

0 commit comments

Comments
 (0)
0