extmod/modbluetooth: Separate enabling of "client" from "central".
Previously, the MICROPY_PY_BLUETOOTH_ENABLE_CENTRAL_MODE macro controlled enabling both the central mode and the GATT client functionality (because usually the two go together). This commits adds a new MICROPY_PY_BLUETOOTH_ENABLE_GATT_CLIENT macro that separately enables the GATT client functionality. This defaults to MICROPY_PY_BLUETOOTH_ENABLE_CENTRAL_MODE. This also fixes a bug in the NimBLE bindings where a notification or indication would not be received by a peripheral (acting as client) as gap_event_cb wasn't handling it. Now both central_gap_event_cb and peripheral_gap_event_cb share the same common handler for these events. Signed-off-by: Jim Mussared <jim.mussared@gmail.com>
This commit is contained in:
@@ -91,7 +91,7 @@ STATIC mp_obj_bluetooth_uuid_t create_mp_uuid(uint16_t uuid16, const uint8_t *uu
|
||||
}
|
||||
return result;
|
||||
}
|
||||
#endif
|
||||
#endif // MICROPY_PY_BLUETOOTH_ENABLE_CENTRAL_MODE
|
||||
|
||||
// Notes on supporting background ops (e.g. an attempt to gatts_notify while
|
||||
// an existing notification is in progress):
|
||||
@@ -218,7 +218,7 @@ STATIC mp_btstack_pending_op_t *btstack_enqueue_pending_operation(uint16_t op_ty
|
||||
return pending_op;
|
||||
}
|
||||
|
||||
#if MICROPY_PY_BLUETOOTH_ENABLE_CENTRAL_MODE
|
||||
#if MICROPY_PY_BLUETOOTH_ENABLE_GATT_CLIENT
|
||||
|
||||
// Cleans up a pending op of the specified type for this conn_handle (and if specified, value_handle).
|
||||
// Used by MP_BLUETOOTH_BTSTACK_PENDING_WRITE and MP_BLUETOOTH_BTSTACK_PENDING_WRITE_NO_RESPONSE.
|
||||
@@ -418,6 +418,8 @@ STATIC void btstack_packet_handler(uint8_t packet_type, uint8_t *packet, uint8_t
|
||||
uint8_t length = gap_event_advertising_report_get_data_length(packet);
|
||||
const uint8_t *data = gap_event_advertising_report_get_data(packet);
|
||||
mp_bluetooth_gap_on_scan_result(address_type, address, adv_event_type, rssi, data, length);
|
||||
#endif // MICROPY_PY_BLUETOOTH_ENABLE_CENTRAL_MODE
|
||||
#if MICROPY_PY_BLUETOOTH_ENABLE_GATT_CLIENT
|
||||
} else if (event_type == GATT_EVENT_QUERY_COMPLETE) {
|
||||
uint16_t conn_handle = gatt_event_query_complete_get_handle(packet);
|
||||
uint16_t status = gatt_event_query_complete_get_att_status(packet);
|
||||
@@ -487,7 +489,7 @@ STATIC void btstack_packet_handler(uint8_t packet_type, uint8_t *packet, uint8_t
|
||||
// Note: Can't "del" the pending_op from IRQ context. Leave it for the GC.
|
||||
}
|
||||
|
||||
#endif // MICROPY_PY_BLUETOOTH_ENABLE_CENTRAL_MODE
|
||||
#endif // MICROPY_PY_BLUETOOTH_ENABLE_GATT_CLIENT
|
||||
} else {
|
||||
DEBUG_printf(" --> hci event type: unknown (0x%02x)\n", event_type);
|
||||
}
|
||||
@@ -506,7 +508,7 @@ STATIC btstack_packet_callback_registration_t hci_event_callback_registration =
|
||||
.callback = &btstack_packet_handler_generic
|
||||
};
|
||||
|
||||
#if MICROPY_PY_BLUETOOTH_ENABLE_CENTRAL_MODE
|
||||
#if MICROPY_PY_BLUETOOTH_ENABLE_GATT_CLIENT
|
||||
// For when the handler is being used for service discovery.
|
||||
STATIC void btstack_packet_handler_discover_services(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size) {
|
||||
(void)channel;
|
||||
@@ -541,7 +543,7 @@ STATIC void btstack_packet_handler_write_with_response(uint8_t packet_type, uint
|
||||
(void)size;
|
||||
btstack_packet_handler(packet_type, packet, MP_BLUETOOTH_IRQ_GATTC_WRITE_DONE);
|
||||
}
|
||||
#endif // MICROPY_PY_BLUETOOTH_ENABLE_CENTRAL_MODE
|
||||
#endif // MICROPY_PY_BLUETOOTH_ENABLE_GATT_CLIENT
|
||||
|
||||
STATIC btstack_timer_source_t btstack_init_deinit_timeout;
|
||||
|
||||
@@ -662,12 +664,12 @@ int mp_bluetooth_init(void) {
|
||||
sm_set_er(dummy_key);
|
||||
sm_set_ir(dummy_key);
|
||||
|
||||
#if MICROPY_PY_BLUETOOTH_ENABLE_CENTRAL_MODE
|
||||
#if MICROPY_PY_BLUETOOTH_ENABLE_GATT_CLIENT
|
||||
gatt_client_init();
|
||||
|
||||
// We always require explicitly exchanging MTU with ble.gattc_exchange_mtu().
|
||||
gatt_client_mtu_enable_auto_negotiation(false);
|
||||
#endif
|
||||
#endif // MICROPY_PY_BLUETOOTH_ENABLE_GATT_CLIENT
|
||||
|
||||
// Register for HCI events.
|
||||
hci_add_event_handler(&hci_event_callback_registration);
|
||||
@@ -719,10 +721,10 @@ int mp_bluetooth_init(void) {
|
||||
set_random_address();
|
||||
}
|
||||
|
||||
#if MICROPY_PY_BLUETOOTH_ENABLE_CENTRAL_MODE
|
||||
#if MICROPY_PY_BLUETOOTH_ENABLE_GATT_CLIENT
|
||||
// Enable GATT_EVENT_NOTIFICATION/GATT_EVENT_INDICATION for all connections and handles.
|
||||
gatt_client_listen_for_characteristic_value_updates(&MP_STATE_PORT(bluetooth_btstack_root_pointers)->notification, &btstack_packet_handler_generic, GATT_CLIENT_ANY_CONNECTION, NULL);
|
||||
#endif
|
||||
#endif // MICROPY_PY_BLUETOOTH_ENABLE_GATT_CLIENT
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -737,10 +739,10 @@ void mp_bluetooth_deinit(void) {
|
||||
|
||||
mp_bluetooth_gap_advertise_stop();
|
||||
|
||||
#if MICROPY_PY_BLUETOOTH_ENABLE_CENTRAL_MODE
|
||||
#if MICROPY_PY_BLUETOOTH_ENABLE_GATT_CLIENT
|
||||
// Remove our registration for notify/indicate.
|
||||
gatt_client_stop_listening_for_characteristic_value_updates(&MP_STATE_PORT(bluetooth_btstack_root_pointers)->notification);
|
||||
#endif
|
||||
#endif // MICROPY_PY_BLUETOOTH_ENABLE_GATT_CLIENT
|
||||
|
||||
// Set a timer that will forcibly set the state to TIMEOUT, which will stop the loop below.
|
||||
btstack_run_loop_set_timer(&btstack_init_deinit_timeout, BTSTACK_INIT_DEINIT_TIMEOUT_MS);
|
||||
@@ -1232,6 +1234,10 @@ int mp_bluetooth_gap_peripheral_connect(uint8_t addr_type, const uint8_t *addr,
|
||||
return btstack_error_to_errno(gap_connect(btstack_addr, addr_type));
|
||||
}
|
||||
|
||||
#endif // MICROPY_PY_BLUETOOTH_ENABLE_CENTRAL_MODE
|
||||
|
||||
#if MICROPY_PY_BLUETOOTH_ENABLE_GATT_CLIENT
|
||||
|
||||
int mp_bluetooth_gattc_discover_primary_services(uint16_t conn_handle, const mp_obj_bluetooth_uuid_t *uuid) {
|
||||
DEBUG_printf("mp_bluetooth_gattc_discover_primary_services\n");
|
||||
uint8_t err;
|
||||
@@ -1346,7 +1352,7 @@ int mp_bluetooth_gattc_exchange_mtu(uint16_t conn_handle) {
|
||||
|
||||
return 0;
|
||||
}
|
||||
#endif // MICROPY_PY_BLUETOOTH_ENABLE_CENTRAL_MODE
|
||||
#endif // MICROPY_PY_BLUETOOTH_ENABLE_GATT_CLIENT
|
||||
|
||||
#if MICROPY_PY_BLUETOOTH_ENABLE_L2CAP_CHANNELS
|
||||
|
||||
|
||||
Reference in New Issue
Block a user