8000 extmod/modbluetooth: Allow NimBLE to use Zephyr static address. · larsks/micropython@4005138 · GitHub
[go: up one dir, main page]

Skip to content

Commit 4005138

Browse files
jimmodpgeorge
authored andcommitted
extmod/modbluetooth: Allow NimBLE to use Zephyr static address.
Zephyr controllers can be queried for a static address (computed from the device ID). BlueKitchen already supports this, but make them both use the same macro to enable the feature.
1 parent 236274f commit 4005138

File tree

2 files changed

+19
-9
lines changed

2 files changed

+19
-9
lines changed

extmod/btstack/modbluetooth_btstack.c

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -282,7 +282,7 @@ STATIC void btstack_packet_handler_att_server(uint8_t packet_type, uint16_t chan
282282
}
283283
}
284284

285-
#if MICROPY_BLUETOOTH_BTSTACK_ZEPHYR_STATIC_ADDRESS
285+
#if MICROPY_BLUETOOTH_USE_ZEPHYR_STATIC_ADDRESS
286286
// During startup, the controller (e.g. Zephyr) might give us a static address that we can use.
287287
STATIC uint8_t controller_static_addr[6] = {0};
288288
STATIC bool controller_static_addr_available = false;
@@ -349,13 +349,13 @@ STATIC void btstack_packet_handler(uint8_t packet_type, uint8_t *packet, uint8_t
349349
DEBUG_printf(" --> hci transport packet sent\n");
350350
} else if (event_type == HCI_EVENT_COMMAND_COMPLETE) {
351351
DEBUG_printf(" --> hci command complete\n");
352-
#if MICROPY_BLUETOOTH_BTSTACK_ZEPHYR_STATIC_ADDRESS
352+
#if MICROPY_BLUETOOTH_USE_ZEPHYR_STATIC_ADDRESS
353353
if (memcmp(packet, read_static_address_command_complete_prefix, sizeof(read_static_address_command_complete_prefix)) == 0) {
354354
DEBUG_printf(" --> static address available\n");
355355
reverse_48(&packet[7], controller_static_addr);
356356
controller_static_addr_available = true;
357357
}
358-
#endif // MICROPY_BLUETOOTH_BTSTACK_ZEPHYR_STATIC_ADDRESS
358+
#endif // MICROPY_BLUETOOTH_USE_ZEPHYR_STATIC_ADDRESS
359359
} else if (event_type == HCI_EVENT_COMMAND_STATUS) {
360360
DEBUG_printf(" --> hci command status\n");
361361
} else if (event_type == HCI_EVENT_NUMBER_OF_COMPLETED_PACKETS) {
@@ -575,12 +575,12 @@ STATIC bool set_public_address(void) {
575575
}
576576

577577
STATIC void set_random_address(void) {
578-
#if MICROPY_BLUETOOTH_BTSTACK_ZEPHYR_STATIC_ADDRESS
578+
#if MICROPY_BLUETOOTH_USE_ZEPHYR_STATIC_ADDRESS
579579
if (controller_static_addr_available) {
580580
DEBUG_printf("set_random_address: Using static address supplied by controller.\n");
581581
gap_random_address_set(controller_static_addr);
582582
} else
583-
#endif // MICROPY_BLUETOOTH_BTSTACK_ZEPHYR_STATIC_ADDRESS
583+
#endif // MICROPY_BLUETOOTH_USE_ZEPHYR_STATIC_ADDRESS
584584
{
585585
bd_addr_t static_addr;
586586

@@ -635,7 +635,7 @@ int mp_bluetooth_init(void) {
635635

636636
btstack_memory_init();
637637

638-
#if MICROPY_BLUETOOTH_BTSTACK_ZEPHYR_STATIC_ADDRESS
638+
#if MICROPY_BLUETOOTH_USE_ZEPHYR_STATIC_ADDRESS
639639
controller_static_addr_available = false;
640640
#endif
641641

extmod/nimble/modbluetooth_nimble.c

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,11 @@
4848
#include "nimble/host/src/ble_l2cap_priv.h"
4949
#endif
5050

51+
#if MICROPY_PY_BLUETOOTH_ENABLE_HCI_CMD || MICROPY_BLUETOOTH_USE_ZEPHYR_STATIC_ADDRESS
52+
// For ble_hs_hci_cmd_tx
53+
#include "nimble/host/src/ble_hs_hci_priv.h"
54+
#endif
55+
5156
#ifndef MICROPY_PY_BLUETOOTH_DEFAULT_GAP_NAME
5257
#define MICROPY_PY_BLUETOOTH_DEFAULT_GAP_NAME "MPY NIMBLE"
5358
#endif
@@ -179,6 +184,14 @@ STATIC void set_random_address(bool nrpa) {
179184
// Mark it as STATIC (not RPA or NRPA).
180185
addr.val[5] |= 0xc0;
181186
} else
187+
#elif MICROPY_BLUETOOTH_USE_ZEPHYR_STATIC_ADDRESS
188+
if (!nrpa) {
189+
DEBUG_printf("set_random_address: Generating static address from Zephyr controller\n");
190+
uint8_t buf[23];
191+
rc = ble_hs_hci_cmd_tx(BLE_HCI_OP(BLE_HCI_OGF_VENDOR, 0x09), NULL, 0, buf, sizeof(buf));
192+
assert(rc == 0);
193+
memcpy(addr.val, buf + 1, 6);
194+
} else
182195
#endif
183196
{
184197
DEBUG_printf("set_random_address: Generating random static address\n");
@@ -1688,9 +1701,6 @@ int mp_bluetooth_l2cap_recvinto(uint16_t conn_handle, uint16_t cid, uint8_t *buf
16881701

16891702
#if MICROPY_PY_BLUETOOTH_ENABLE_HCI_CMD
16901703

1691-
// For ble_hs_hci_cmd_tx
1692-
#include "nimble/host/src/ble_hs_hci_priv.h"
1693-
16941704
int mp_bluetooth_hci_cmd(uint16_t ogf, uint16_t ocf, const uint8_t *req, size_t req_len, uint8_t *resp, size_t resp_len, uint8_t *status) {
16951705
int rc = ble_hs_hci_cmd_tx(BLE_HCI_OP(ogf, ocf), req, req_len, resp, resp_len);
16961706
if (rc < BLE_HS_ERR_HCI_BASE || rc >= BLE_HS_ERR_HCI_BASE + 0x100) {

0 commit comments

Comments
 (0)
0