extmod/modbluetooth: Rename logging macro to be just DEBUG_printf.
And prefix the debug message with "btstack:" or "nimble:", depending on the context. Also use correct format specifier for %zu.
This commit is contained in:
parent
5ff265a3db
commit
e46aac24ba
|
@ -35,7 +35,7 @@
|
||||||
|
|
||||||
#include "lib/btstack/src/btstack.h"
|
#include "lib/btstack/src/btstack.h"
|
||||||
|
|
||||||
#define DEBUG_EVENT_printf(...) // printf(__VA_ARGS__)
|
#define DEBUG_printf(...) // printf("btstack: " __VA_ARGS__)
|
||||||
|
|
||||||
#ifndef MICROPY_PY_BLUETOOTH_DEFAULT_GAP_NAME
|
#ifndef MICROPY_PY_BLUETOOTH_DEFAULT_GAP_NAME
|
||||||
#define MICROPY_PY_BLUETOOTH_DEFAULT_GAP_NAME "MPY BTSTACK"
|
#define MICROPY_PY_BLUETOOTH_DEFAULT_GAP_NAME "MPY BTSTACK"
|
||||||
|
@ -54,7 +54,7 @@ STATIC const uint16_t BTSTACK_GAP_DEVICE_NAME_HANDLE = 3;
|
||||||
volatile int mp_bluetooth_btstack_state = MP_BLUETOOTH_BTSTACK_STATE_OFF;
|
volatile int mp_bluetooth_btstack_state = MP_BLUETOOTH_BTSTACK_STATE_OFF;
|
||||||
|
|
||||||
STATIC int btstack_error_to_errno(int err) {
|
STATIC int btstack_error_to_errno(int err) {
|
||||||
DEBUG_EVENT_printf(" --> btstack error: %d\n", err);
|
DEBUG_printf(" --> btstack error: %d\n", err);
|
||||||
if (err == ERROR_CODE_SUCCESS) {
|
if (err == ERROR_CODE_SUCCESS) {
|
||||||
return 0;
|
return 0;
|
||||||
} else if (err == BTSTACK_ACL_BUFFERS_FULL || err == BTSTACK_MEMORY_ALLOC_FAILED) {
|
} else if (err == BTSTACK_ACL_BUFFERS_FULL || err == BTSTACK_MEMORY_ALLOC_FAILED) {
|
||||||
|
@ -168,16 +168,16 @@ STATIC void btstack_remove_pending_operation(mp_btstack_pending_op_t *pending_op
|
||||||
STATIC void btstack_notify_indicate_ready_handler(void *context) {
|
STATIC void btstack_notify_indicate_ready_handler(void *context) {
|
||||||
MICROPY_PY_BLUETOOTH_ENTER
|
MICROPY_PY_BLUETOOTH_ENTER
|
||||||
mp_btstack_pending_op_t *pending_op = (mp_btstack_pending_op_t *)context;
|
mp_btstack_pending_op_t *pending_op = (mp_btstack_pending_op_t *)context;
|
||||||
DEBUG_EVENT_printf("btstack_notify_indicate_ready_handler op_type=%d conn_handle=%d value_handle=%d len=%lu\n", pending_op->op_type, pending_op->conn_handle, pending_op->value_handle, pending_op->len);
|
DEBUG_printf("btstack_notify_indicate_ready_handler op_type=%d conn_handle=%d value_handle=%d len=%zu\n", pending_op->op_type, pending_op->conn_handle, pending_op->value_handle, pending_op->len);
|
||||||
if (pending_op->op_type == MP_BLUETOOTH_BTSTACK_PENDING_NOTIFY) {
|
if (pending_op->op_type == MP_BLUETOOTH_BTSTACK_PENDING_NOTIFY) {
|
||||||
int err = att_server_notify(pending_op->conn_handle, pending_op->value_handle, pending_op->buf, pending_op->len);
|
int err = att_server_notify(pending_op->conn_handle, pending_op->value_handle, pending_op->buf, pending_op->len);
|
||||||
DEBUG_EVENT_printf("btstack_notify_indicate_ready_handler: sending notification err=%d\n", err);
|
DEBUG_printf("btstack_notify_indicate_ready_handler: sending notification err=%d\n", err);
|
||||||
assert(err == ERROR_CODE_SUCCESS);
|
assert(err == ERROR_CODE_SUCCESS);
|
||||||
(void)err;
|
(void)err;
|
||||||
} else {
|
} else {
|
||||||
assert(pending_op->op_type == MP_BLUETOOTH_BTSTACK_PENDING_INDICATE);
|
assert(pending_op->op_type == MP_BLUETOOTH_BTSTACK_PENDING_INDICATE);
|
||||||
int err = att_server_indicate(pending_op->conn_handle, pending_op->value_handle, NULL, 0);
|
int err = att_server_indicate(pending_op->conn_handle, pending_op->value_handle, NULL, 0);
|
||||||
DEBUG_EVENT_printf("btstack_notify_indicate_ready_handler: sending indication err=%d\n", err);
|
DEBUG_printf("btstack_notify_indicate_ready_handler: sending indication err=%d\n", err);
|
||||||
assert(err == ERROR_CODE_SUCCESS);
|
assert(err == ERROR_CODE_SUCCESS);
|
||||||
(void)err;
|
(void)err;
|
||||||
}
|
}
|
||||||
|
@ -188,7 +188,7 @@ STATIC void btstack_notify_indicate_ready_handler(void *context) {
|
||||||
|
|
||||||
// Register a pending background operation -- copies the buffer, and makes it known to the GC.
|
// Register a pending background operation -- copies the buffer, and makes it known to the GC.
|
||||||
STATIC mp_btstack_pending_op_t *btstack_enqueue_pending_operation(uint16_t op_type, uint16_t conn_handle, uint16_t value_handle, const uint8_t *buf, size_t len) {
|
STATIC mp_btstack_pending_op_t *btstack_enqueue_pending_operation(uint16_t op_type, uint16_t conn_handle, uint16_t value_handle, const uint8_t *buf, size_t len) {
|
||||||
DEBUG_EVENT_printf("btstack_enqueue_pending_operation op_type=%d conn_handle=%d value_handle=%d len=%lu\n", op_type, conn_handle, value_handle, len);
|
DEBUG_printf("btstack_enqueue_pending_operation op_type=%d conn_handle=%d value_handle=%d len=%zu\n", op_type, conn_handle, value_handle, len);
|
||||||
mp_btstack_pending_op_t *pending_op = m_new_obj_var(mp_btstack_pending_op_t, uint8_t, len);
|
mp_btstack_pending_op_t *pending_op = m_new_obj_var(mp_btstack_pending_op_t, uint8_t, len);
|
||||||
pending_op->op_type = op_type;
|
pending_op->op_type = op_type;
|
||||||
pending_op->conn_handle = conn_handle;
|
pending_op->conn_handle = conn_handle;
|
||||||
|
@ -219,20 +219,20 @@ STATIC mp_btstack_pending_op_t *btstack_enqueue_pending_operation(uint16_t op_ty
|
||||||
// know for sure that we're using the correct entry.
|
// know for sure that we're using the correct entry.
|
||||||
STATIC mp_btstack_pending_op_t *btstack_finish_pending_operation(uint16_t op_type, uint16_t conn_handle, uint16_t value_handle, bool del) {
|
STATIC mp_btstack_pending_op_t *btstack_finish_pending_operation(uint16_t op_type, uint16_t conn_handle, uint16_t value_handle, bool del) {
|
||||||
MICROPY_PY_BLUETOOTH_ENTER
|
MICROPY_PY_BLUETOOTH_ENTER
|
||||||
DEBUG_EVENT_printf("btstack_finish_pending_operation op_type=%d conn_handle=%d value_handle=%d\n", op_type, conn_handle, value_handle);
|
DEBUG_printf("btstack_finish_pending_operation op_type=%d conn_handle=%d value_handle=%d\n", op_type, conn_handle, value_handle);
|
||||||
btstack_linked_list_iterator_t it;
|
btstack_linked_list_iterator_t it;
|
||||||
btstack_linked_list_iterator_init(&it, &MP_STATE_PORT(bluetooth_btstack_root_pointers)->pending_ops);
|
btstack_linked_list_iterator_init(&it, &MP_STATE_PORT(bluetooth_btstack_root_pointers)->pending_ops);
|
||||||
while (btstack_linked_list_iterator_has_next(&it)) {
|
while (btstack_linked_list_iterator_has_next(&it)) {
|
||||||
mp_btstack_pending_op_t *pending_op = (mp_btstack_pending_op_t *)btstack_linked_list_iterator_next(&it);
|
mp_btstack_pending_op_t *pending_op = (mp_btstack_pending_op_t *)btstack_linked_list_iterator_next(&it);
|
||||||
|
|
||||||
if (pending_op->op_type == op_type && pending_op->conn_handle == conn_handle && (value_handle == 0xffff || pending_op->value_handle == value_handle)) {
|
if (pending_op->op_type == op_type && pending_op->conn_handle == conn_handle && (value_handle == 0xffff || pending_op->value_handle == value_handle)) {
|
||||||
DEBUG_EVENT_printf("btstack_finish_pending_operation: found value_handle=%d len=%lu\n", pending_op->value_handle, pending_op->len);
|
DEBUG_printf("btstack_finish_pending_operation: found value_handle=%d len=%zu\n", pending_op->value_handle, pending_op->len);
|
||||||
btstack_remove_pending_operation(pending_op, del);
|
btstack_remove_pending_operation(pending_op, del);
|
||||||
MICROPY_PY_BLUETOOTH_EXIT
|
MICROPY_PY_BLUETOOTH_EXIT
|
||||||
return del ? NULL : pending_op;
|
return del ? NULL : pending_op;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
DEBUG_EVENT_printf("btstack_finish_pending_operation: not found\n");
|
DEBUG_printf("btstack_finish_pending_operation: not found\n");
|
||||||
MICROPY_PY_BLUETOOTH_EXIT
|
MICROPY_PY_BLUETOOTH_EXIT
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
@ -243,7 +243,7 @@ STATIC mp_btstack_pending_op_t *btstack_finish_pending_operation(uint16_t op_typ
|
||||||
STATIC void btstack_packet_handler_att_server(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size) {
|
STATIC void btstack_packet_handler_att_server(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size) {
|
||||||
(void)channel;
|
(void)channel;
|
||||||
(void)size;
|
(void)size;
|
||||||
DEBUG_EVENT_printf("btstack_packet_handler_att_server(packet_type=%u, packet=%p)\n", packet_type, packet);
|
DEBUG_printf("btstack_packet_handler_att_server(packet_type=%u, packet=%p)\n", packet_type, packet);
|
||||||
if (packet_type != HCI_EVENT_PACKET) {
|
if (packet_type != HCI_EVENT_PACKET) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
@ -251,13 +251,13 @@ STATIC void btstack_packet_handler_att_server(uint8_t packet_type, uint16_t chan
|
||||||
uint8_t event_type = hci_event_packet_get_type(packet);
|
uint8_t event_type = hci_event_packet_get_type(packet);
|
||||||
|
|
||||||
if (event_type == ATT_EVENT_CONNECTED) {
|
if (event_type == ATT_EVENT_CONNECTED) {
|
||||||
DEBUG_EVENT_printf(" --> att connected\n");
|
DEBUG_printf(" --> att connected\n");
|
||||||
// The ATT_EVENT_*CONNECTED events are fired for both peripheral and central role, with no way to tell which.
|
// The ATT_EVENT_*CONNECTED events are fired for both peripheral and central role, with no way to tell which.
|
||||||
// So we use the HCI_EVENT_LE_META event directly in the main packet handler.
|
// So we use the HCI_EVENT_LE_META event directly in the main packet handler.
|
||||||
} else if (event_type == ATT_EVENT_DISCONNECTED) {
|
} else if (event_type == ATT_EVENT_DISCONNECTED) {
|
||||||
DEBUG_EVENT_printf(" --> att disconnected\n");
|
DEBUG_printf(" --> att disconnected\n");
|
||||||
} else if (event_type == ATT_EVENT_HANDLE_VALUE_INDICATION_COMPLETE) {
|
} else if (event_type == ATT_EVENT_HANDLE_VALUE_INDICATION_COMPLETE) {
|
||||||
DEBUG_EVENT_printf(" --> att indication complete\n");
|
DEBUG_printf(" --> att indication complete\n");
|
||||||
uint16_t conn_handle = att_event_handle_value_indication_complete_get_conn_handle(packet);
|
uint16_t conn_handle = att_event_handle_value_indication_complete_get_conn_handle(packet);
|
||||||
uint16_t value_handle = att_event_handle_value_indication_complete_get_attribute_handle(packet);
|
uint16_t value_handle = att_event_handle_value_indication_complete_get_attribute_handle(packet);
|
||||||
uint8_t status = att_event_handle_value_indication_complete_get_status(packet);
|
uint8_t status = att_event_handle_value_indication_complete_get_status(packet);
|
||||||
|
@ -265,12 +265,12 @@ STATIC void btstack_packet_handler_att_server(uint8_t packet_type, uint16_t chan
|
||||||
} else if (event_type == HCI_EVENT_LE_META || event_type == HCI_EVENT_DISCONNECTION_COMPLETE) {
|
} else if (event_type == HCI_EVENT_LE_META || event_type == HCI_EVENT_DISCONNECTION_COMPLETE) {
|
||||||
// Ignore, duplicated by att_server.c.
|
// Ignore, duplicated by att_server.c.
|
||||||
} else {
|
} else {
|
||||||
DEBUG_EVENT_printf(" --> hci att server event type: unknown (0x%02x)\n", event_type);
|
DEBUG_printf(" --> hci att server event type: unknown (0x%02x)\n", event_type);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
STATIC void btstack_packet_handler(uint8_t packet_type, uint8_t *packet, uint8_t irq) {
|
STATIC void btstack_packet_handler(uint8_t packet_type, uint8_t *packet, uint8_t irq) {
|
||||||
DEBUG_EVENT_printf("btstack_packet_handler(packet_type=%u, packet=%p)\n", packet_type, packet);
|
DEBUG_printf("btstack_packet_handler(packet_type=%u, packet=%p)\n", packet_type, packet);
|
||||||
if (packet_type != HCI_EVENT_PACKET) {
|
if (packet_type != HCI_EVENT_PACKET) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
@ -278,7 +278,7 @@ STATIC void btstack_packet_handler(uint8_t packet_type, uint8_t *packet, uint8_t
|
||||||
uint8_t event_type = hci_event_packet_get_type(packet);
|
uint8_t event_type = hci_event_packet_get_type(packet);
|
||||||
|
|
||||||
if (event_type == HCI_EVENT_LE_META) {
|
if (event_type == HCI_EVENT_LE_META) {
|
||||||
DEBUG_EVENT_printf(" --> hci le meta\n");
|
DEBUG_printf(" --> hci le meta\n");
|
||||||
if (hci_event_le_meta_get_subevent_code(packet) == HCI_SUBEVENT_LE_CONNECTION_COMPLETE) {
|
if (hci_event_le_meta_get_subevent_code(packet) == HCI_SUBEVENT_LE_CONNECTION_COMPLETE) {
|
||||||
uint16_t conn_handle = hci_subevent_le_connection_complete_get_connection_handle(packet);
|
uint16_t conn_handle = hci_subevent_le_connection_complete_get_connection_handle(packet);
|
||||||
uint8_t addr_type = hci_subevent_le_connection_complete_get_peer_address_type(packet);
|
uint8_t addr_type = hci_subevent_le_connection_complete_get_peer_address_type(packet);
|
||||||
|
@ -296,7 +296,7 @@ STATIC void btstack_packet_handler(uint8_t packet_type, uint8_t *packet, uint8_t
|
||||||
}
|
}
|
||||||
} else if (event_type == BTSTACK_EVENT_STATE) {
|
} else if (event_type == BTSTACK_EVENT_STATE) {
|
||||||
uint8_t state = btstack_event_state_get_state(packet);
|
uint8_t state = btstack_event_state_get_state(packet);
|
||||||
DEBUG_EVENT_printf(" --> btstack event state 0x%02x\n", state);
|
DEBUG_printf(" --> btstack event state 0x%02x\n", state);
|
||||||
if (state == HCI_STATE_WORKING) {
|
if (state == HCI_STATE_WORKING) {
|
||||||
// Signal that initialisation has completed.
|
// Signal that initialisation has completed.
|
||||||
mp_bluetooth_btstack_state = MP_BLUETOOTH_BTSTACK_STATE_ACTIVE;
|
mp_bluetooth_btstack_state = MP_BLUETOOTH_BTSTACK_STATE_ACTIVE;
|
||||||
|
@ -305,21 +305,21 @@ STATIC void btstack_packet_handler(uint8_t packet_type, uint8_t *packet, uint8_t
|
||||||
mp_bluetooth_btstack_state = MP_BLUETOOTH_BTSTACK_STATE_OFF;
|
mp_bluetooth_btstack_state = MP_BLUETOOTH_BTSTACK_STATE_OFF;
|
||||||
}
|
}
|
||||||
} else if (event_type == HCI_EVENT_TRANSPORT_PACKET_SENT) {
|
} else if (event_type == HCI_EVENT_TRANSPORT_PACKET_SENT) {
|
||||||
DEBUG_EVENT_printf(" --> hci transport packet sent\n");
|
DEBUG_printf(" --> hci transport packet sent\n");
|
||||||
} else if (event_type == HCI_EVENT_COMMAND_COMPLETE) {
|
} else if (event_type == HCI_EVENT_COMMAND_COMPLETE) {
|
||||||
DEBUG_EVENT_printf(" --> hci command complete\n");
|
DEBUG_printf(" --> hci command complete\n");
|
||||||
} else if (event_type == HCI_EVENT_COMMAND_STATUS) {
|
} else if (event_type == HCI_EVENT_COMMAND_STATUS) {
|
||||||
DEBUG_EVENT_printf(" --> hci command status\n");
|
DEBUG_printf(" --> hci command status\n");
|
||||||
} else if (event_type == HCI_EVENT_NUMBER_OF_COMPLETED_PACKETS) {
|
} else if (event_type == HCI_EVENT_NUMBER_OF_COMPLETED_PACKETS) {
|
||||||
DEBUG_EVENT_printf(" --> hci number of completed packets\n");
|
DEBUG_printf(" --> hci number of completed packets\n");
|
||||||
} else if (event_type == BTSTACK_EVENT_NR_CONNECTIONS_CHANGED) {
|
} else if (event_type == BTSTACK_EVENT_NR_CONNECTIONS_CHANGED) {
|
||||||
DEBUG_EVENT_printf(" --> btstack # conns changed\n");
|
DEBUG_printf(" --> btstack # conns changed\n");
|
||||||
} else if (event_type == HCI_EVENT_VENDOR_SPECIFIC) {
|
} else if (event_type == HCI_EVENT_VENDOR_SPECIFIC) {
|
||||||
DEBUG_EVENT_printf(" --> hci vendor specific\n");
|
DEBUG_printf(" --> hci vendor specific\n");
|
||||||
} else if (event_type == GATT_EVENT_MTU) {
|
} else if (event_type == GATT_EVENT_MTU) {
|
||||||
DEBUG_EVENT_printf(" --> hci MTU\n");
|
DEBUG_printf(" --> hci MTU\n");
|
||||||
} else if (event_type == HCI_EVENT_DISCONNECTION_COMPLETE) {
|
} else if (event_type == HCI_EVENT_DISCONNECTION_COMPLETE) {
|
||||||
DEBUG_EVENT_printf(" --> hci disconnect complete\n");
|
DEBUG_printf(" --> hci disconnect complete\n");
|
||||||
uint16_t conn_handle = hci_event_disconnection_complete_get_connection_handle(packet);
|
uint16_t conn_handle = hci_event_disconnection_complete_get_connection_handle(packet);
|
||||||
const hci_connection_t *conn = hci_connection_for_handle(conn_handle);
|
const hci_connection_t *conn = hci_connection_for_handle(conn_handle);
|
||||||
uint16_t irq_event;
|
uint16_t irq_event;
|
||||||
|
@ -334,7 +334,7 @@ STATIC void btstack_packet_handler(uint8_t packet_type, uint8_t *packet, uint8_t
|
||||||
mp_bluetooth_gap_on_connected_disconnected(irq_event, conn_handle, 0xff, addr);
|
mp_bluetooth_gap_on_connected_disconnected(irq_event, conn_handle, 0xff, addr);
|
||||||
#if MICROPY_PY_BLUETOOTH_ENABLE_CENTRAL_MODE
|
#if MICROPY_PY_BLUETOOTH_ENABLE_CENTRAL_MODE
|
||||||
} else if (event_type == GAP_EVENT_ADVERTISING_REPORT) {
|
} else if (event_type == GAP_EVENT_ADVERTISING_REPORT) {
|
||||||
DEBUG_EVENT_printf(" --> gap advertising report\n");
|
DEBUG_printf(" --> gap advertising report\n");
|
||||||
bd_addr_t address;
|
bd_addr_t address;
|
||||||
gap_event_advertising_report_get_address(packet, address);
|
gap_event_advertising_report_get_address(packet, address);
|
||||||
uint8_t adv_event_type = gap_event_advertising_report_get_advertising_event_type(packet);
|
uint8_t adv_event_type = gap_event_advertising_report_get_advertising_event_type(packet);
|
||||||
|
@ -346,7 +346,7 @@ STATIC void btstack_packet_handler(uint8_t packet_type, uint8_t *packet, uint8_t
|
||||||
} else if (event_type == GATT_EVENT_QUERY_COMPLETE) {
|
} else if (event_type == GATT_EVENT_QUERY_COMPLETE) {
|
||||||
uint16_t conn_handle = gatt_event_query_complete_get_handle(packet);
|
uint16_t conn_handle = gatt_event_query_complete_get_handle(packet);
|
||||||
uint16_t status = gatt_event_query_complete_get_att_status(packet);
|
uint16_t status = gatt_event_query_complete_get_att_status(packet);
|
||||||
DEBUG_EVENT_printf(" --> gatt query complete irq=%d conn_handle=%d status=%d\n", irq, conn_handle, status);
|
DEBUG_printf(" --> gatt query complete irq=%d conn_handle=%d status=%d\n", irq, conn_handle, status);
|
||||||
if (irq == MP_BLUETOOTH_IRQ_GATTC_READ_DONE || irq == MP_BLUETOOTH_IRQ_GATTC_WRITE_DONE) {
|
if (irq == MP_BLUETOOTH_IRQ_GATTC_READ_DONE || irq == MP_BLUETOOTH_IRQ_GATTC_WRITE_DONE) {
|
||||||
// TODO there is no value_handle available to pass here.
|
// TODO there is no value_handle available to pass here.
|
||||||
// TODO try and get this implemented in btstack.
|
// TODO try and get this implemented in btstack.
|
||||||
|
@ -361,28 +361,28 @@ STATIC void btstack_packet_handler(uint8_t packet_type, uint8_t *packet, uint8_t
|
||||||
mp_bluetooth_gattc_on_discover_complete(irq, conn_handle, status);
|
mp_bluetooth_gattc_on_discover_complete(irq, conn_handle, status);
|
||||||
}
|
}
|
||||||
} else if (event_type == GATT_EVENT_SERVICE_QUERY_RESULT) {
|
} else if (event_type == GATT_EVENT_SERVICE_QUERY_RESULT) {
|
||||||
DEBUG_EVENT_printf(" --> gatt service query result\n");
|
DEBUG_printf(" --> gatt service query result\n");
|
||||||
uint16_t conn_handle = gatt_event_service_query_result_get_handle(packet);
|
uint16_t conn_handle = gatt_event_service_query_result_get_handle(packet);
|
||||||
gatt_client_service_t service;
|
gatt_client_service_t service;
|
||||||
gatt_event_service_query_result_get_service(packet, &service);
|
gatt_event_service_query_result_get_service(packet, &service);
|
||||||
mp_obj_bluetooth_uuid_t service_uuid = create_mp_uuid(service.uuid16, service.uuid128);
|
mp_obj_bluetooth_uuid_t service_uuid = create_mp_uuid(service.uuid16, service.uuid128);
|
||||||
mp_bluetooth_gattc_on_primary_service_result(conn_handle, service.start_group_handle, service.end_group_handle, &service_uuid);
|
mp_bluetooth_gattc_on_primary_service_result(conn_handle, service.start_group_handle, service.end_group_handle, &service_uuid);
|
||||||
} else if (event_type == GATT_EVENT_CHARACTERISTIC_QUERY_RESULT) {
|
} else if (event_type == GATT_EVENT_CHARACTERISTIC_QUERY_RESULT) {
|
||||||
DEBUG_EVENT_printf(" --> gatt characteristic query result\n");
|
DEBUG_printf(" --> gatt characteristic query result\n");
|
||||||
uint16_t conn_handle = gatt_event_characteristic_query_result_get_handle(packet);
|
uint16_t conn_handle = gatt_event_characteristic_query_result_get_handle(packet);
|
||||||
gatt_client_characteristic_t characteristic;
|
gatt_client_characteristic_t characteristic;
|
||||||
gatt_event_characteristic_query_result_get_characteristic(packet, &characteristic);
|
gatt_event_characteristic_query_result_get_characteristic(packet, &characteristic);
|
||||||
mp_obj_bluetooth_uuid_t characteristic_uuid = create_mp_uuid(characteristic.uuid16, characteristic.uuid128);
|
mp_obj_bluetooth_uuid_t characteristic_uuid = create_mp_uuid(characteristic.uuid16, characteristic.uuid128);
|
||||||
mp_bluetooth_gattc_on_characteristic_result(conn_handle, characteristic.start_handle, characteristic.value_handle, characteristic.properties, &characteristic_uuid);
|
mp_bluetooth_gattc_on_characteristic_result(conn_handle, characteristic.start_handle, characteristic.value_handle, characteristic.properties, &characteristic_uuid);
|
||||||
} else if (event_type == GATT_EVENT_CHARACTERISTIC_DESCRIPTOR_QUERY_RESULT) {
|
} else if (event_type == GATT_EVENT_CHARACTERISTIC_DESCRIPTOR_QUERY_RESULT) {
|
||||||
DEBUG_EVENT_printf(" --> gatt descriptor query result\n");
|
DEBUG_printf(" --> gatt descriptor query result\n");
|
||||||
uint16_t conn_handle = gatt_event_all_characteristic_descriptors_query_result_get_handle(packet);
|
uint16_t conn_handle = gatt_event_all_characteristic_descriptors_query_result_get_handle(packet);
|
||||||
gatt_client_characteristic_descriptor_t descriptor;
|
gatt_client_characteristic_descriptor_t descriptor;
|
||||||
gatt_event_all_characteristic_descriptors_query_result_get_characteristic_descriptor(packet, &descriptor);
|
gatt_event_all_characteristic_descriptors_query_result_get_characteristic_descriptor(packet, &descriptor);
|
||||||
mp_obj_bluetooth_uuid_t descriptor_uuid = create_mp_uuid(descriptor.uuid16, descriptor.uuid128);
|
mp_obj_bluetooth_uuid_t descriptor_uuid = create_mp_uuid(descriptor.uuid16, descriptor.uuid128);
|
||||||
mp_bluetooth_gattc_on_descriptor_result(conn_handle, descriptor.handle, &descriptor_uuid);
|
mp_bluetooth_gattc_on_descriptor_result(conn_handle, descriptor.handle, &descriptor_uuid);
|
||||||
} else if (event_type == GATT_EVENT_CHARACTERISTIC_VALUE_QUERY_RESULT) {
|
} else if (event_type == GATT_EVENT_CHARACTERISTIC_VALUE_QUERY_RESULT) {
|
||||||
DEBUG_EVENT_printf(" --> gatt characteristic value query result\n");
|
DEBUG_printf(" --> gatt characteristic value query result\n");
|
||||||
uint16_t conn_handle = gatt_event_characteristic_value_query_result_get_handle(packet);
|
uint16_t conn_handle = gatt_event_characteristic_value_query_result_get_handle(packet);
|
||||||
uint16_t value_handle = gatt_event_characteristic_value_query_result_get_value_handle(packet);
|
uint16_t value_handle = gatt_event_characteristic_value_query_result_get_value_handle(packet);
|
||||||
uint16_t len = gatt_event_characteristic_value_query_result_get_value_length(packet);
|
uint16_t len = gatt_event_characteristic_value_query_result_get_value_length(packet);
|
||||||
|
@ -392,7 +392,7 @@ STATIC void btstack_packet_handler(uint8_t packet_type, uint8_t *packet, uint8_t
|
||||||
mp_bluetooth_gattc_on_data_available_chunk(data, len);
|
mp_bluetooth_gattc_on_data_available_chunk(data, len);
|
||||||
mp_bluetooth_gattc_on_data_available_end(atomic_state);
|
mp_bluetooth_gattc_on_data_available_end(atomic_state);
|
||||||
} else if (event_type == GATT_EVENT_NOTIFICATION) {
|
} else if (event_type == GATT_EVENT_NOTIFICATION) {
|
||||||
DEBUG_EVENT_printf(" --> gatt notification\n");
|
DEBUG_printf(" --> gatt notification\n");
|
||||||
uint16_t conn_handle = gatt_event_notification_get_handle(packet);
|
uint16_t conn_handle = gatt_event_notification_get_handle(packet);
|
||||||
uint16_t value_handle = gatt_event_notification_get_value_handle(packet);
|
uint16_t value_handle = gatt_event_notification_get_value_handle(packet);
|
||||||
uint16_t len = gatt_event_notification_get_value_length(packet);
|
uint16_t len = gatt_event_notification_get_value_length(packet);
|
||||||
|
@ -402,7 +402,7 @@ STATIC void btstack_packet_handler(uint8_t packet_type, uint8_t *packet, uint8_t
|
||||||
mp_bluetooth_gattc_on_data_available_chunk(data, len);
|
mp_bluetooth_gattc_on_data_available_chunk(data, len);
|
||||||
mp_bluetooth_gattc_on_data_available_end(atomic_state);
|
mp_bluetooth_gattc_on_data_available_end(atomic_state);
|
||||||
} else if (event_type == GATT_EVENT_INDICATION) {
|
} else if (event_type == GATT_EVENT_INDICATION) {
|
||||||
DEBUG_EVENT_printf(" --> gatt indication\n");
|
DEBUG_printf(" --> gatt indication\n");
|
||||||
uint16_t conn_handle = gatt_event_indication_get_handle(packet);
|
uint16_t conn_handle = gatt_event_indication_get_handle(packet);
|
||||||
uint16_t value_handle = gatt_event_indication_get_value_handle(packet);
|
uint16_t value_handle = gatt_event_indication_get_value_handle(packet);
|
||||||
uint16_t len = gatt_event_indication_get_value_length(packet);
|
uint16_t len = gatt_event_indication_get_value_length(packet);
|
||||||
|
@ -413,17 +413,17 @@ STATIC void btstack_packet_handler(uint8_t packet_type, uint8_t *packet, uint8_t
|
||||||
mp_bluetooth_gattc_on_data_available_end(atomic_state);
|
mp_bluetooth_gattc_on_data_available_end(atomic_state);
|
||||||
} else if (event_type == GATT_EVENT_CAN_WRITE_WITHOUT_RESPONSE) {
|
} else if (event_type == GATT_EVENT_CAN_WRITE_WITHOUT_RESPONSE) {
|
||||||
uint16_t conn_handle = gatt_event_can_write_without_response_get_handle(packet);
|
uint16_t conn_handle = gatt_event_can_write_without_response_get_handle(packet);
|
||||||
DEBUG_EVENT_printf(" --> gatt can write without response %d\n", conn_handle);
|
DEBUG_printf(" --> gatt can write without response %d\n", conn_handle);
|
||||||
mp_btstack_pending_op_t *pending_op = btstack_finish_pending_operation(MP_BLUETOOTH_BTSTACK_PENDING_WRITE_NO_RESPONSE, conn_handle, 0xffff, false /* !del */);
|
mp_btstack_pending_op_t *pending_op = btstack_finish_pending_operation(MP_BLUETOOTH_BTSTACK_PENDING_WRITE_NO_RESPONSE, conn_handle, 0xffff, false /* !del */);
|
||||||
if (pending_op) {
|
if (pending_op) {
|
||||||
DEBUG_EVENT_printf(" --> ready for value_handle=%d len=%lu\n", pending_op->value_handle, pending_op->len);
|
DEBUG_printf(" --> ready for value_handle=%d len=%zu\n", pending_op->value_handle, pending_op->len);
|
||||||
gatt_client_write_value_of_characteristic_without_response(pending_op->conn_handle, pending_op->value_handle, pending_op->len, (uint8_t *)pending_op->buf);
|
gatt_client_write_value_of_characteristic_without_response(pending_op->conn_handle, pending_op->value_handle, pending_op->len, (uint8_t *)pending_op->buf);
|
||||||
// Note: Can't "del" the pending_op from IRQ context. Leave it for the GC.
|
// 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_CENTRAL_MODE
|
||||||
} else {
|
} else {
|
||||||
DEBUG_EVENT_printf(" --> hci event type: unknown (0x%02x)\n", event_type);
|
DEBUG_printf(" --> hci event type: unknown (0x%02x)\n", event_type);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -489,7 +489,7 @@ STATIC void btstack_init_deinit_timeout_handler(btstack_timer_source_t *ds) {
|
||||||
}
|
}
|
||||||
|
|
||||||
int mp_bluetooth_init(void) {
|
int mp_bluetooth_init(void) {
|
||||||
DEBUG_EVENT_printf("mp_bluetooth_init\n");
|
DEBUG_printf("mp_bluetooth_init\n");
|
||||||
|
|
||||||
if (mp_bluetooth_btstack_state == MP_BLUETOOTH_BTSTACK_STATE_ACTIVE) {
|
if (mp_bluetooth_btstack_state == MP_BLUETOOTH_BTSTACK_STATE_ACTIVE) {
|
||||||
return 0;
|
return 0;
|
||||||
|
@ -538,6 +538,8 @@ int mp_bluetooth_init(void) {
|
||||||
btstack_run_loop_set_timer_handler(&btstack_init_deinit_timeout, btstack_init_deinit_timeout_handler);
|
btstack_run_loop_set_timer_handler(&btstack_init_deinit_timeout, btstack_init_deinit_timeout_handler);
|
||||||
btstack_run_loop_add_timer(&btstack_init_deinit_timeout);
|
btstack_run_loop_add_timer(&btstack_init_deinit_timeout);
|
||||||
|
|
||||||
|
DEBUG_printf("mp_bluetooth_init: waiting for stack startup\n");
|
||||||
|
|
||||||
// Either the HCI event will set state to ACTIVE, or the timeout will set it to TIMEOUT.
|
// Either the HCI event will set state to ACTIVE, or the timeout will set it to TIMEOUT.
|
||||||
mp_bluetooth_btstack_port_start();
|
mp_bluetooth_btstack_port_start();
|
||||||
while (mp_bluetooth_btstack_state == MP_BLUETOOTH_BTSTACK_STATE_STARTING) {
|
while (mp_bluetooth_btstack_state == MP_BLUETOOTH_BTSTACK_STATE_STARTING) {
|
||||||
|
@ -547,6 +549,8 @@ int mp_bluetooth_init(void) {
|
||||||
|
|
||||||
// Check for timeout.
|
// Check for timeout.
|
||||||
if (mp_bluetooth_btstack_state != MP_BLUETOOTH_BTSTACK_STATE_ACTIVE) {
|
if (mp_bluetooth_btstack_state != MP_BLUETOOTH_BTSTACK_STATE_ACTIVE) {
|
||||||
|
DEBUG_printf("mp_bluetooth_init: stack startup timed out\n");
|
||||||
|
|
||||||
// Required to stop the polling loop.
|
// Required to stop the polling loop.
|
||||||
mp_bluetooth_btstack_state = MP_BLUETOOTH_BTSTACK_STATE_OFF;
|
mp_bluetooth_btstack_state = MP_BLUETOOTH_BTSTACK_STATE_OFF;
|
||||||
// Attempt a shutdown (may not do anything).
|
// Attempt a shutdown (may not do anything).
|
||||||
|
@ -566,7 +570,7 @@ int mp_bluetooth_init(void) {
|
||||||
}
|
}
|
||||||
|
|
||||||
void mp_bluetooth_deinit(void) {
|
void mp_bluetooth_deinit(void) {
|
||||||
DEBUG_EVENT_printf("mp_bluetooth_deinit\n");
|
DEBUG_printf("mp_bluetooth_deinit\n");
|
||||||
|
|
||||||
// Nothing to do if not initialised.
|
// Nothing to do if not initialised.
|
||||||
if (!MP_STATE_PORT(bluetooth_btstack_root_pointers)) {
|
if (!MP_STATE_PORT(bluetooth_btstack_root_pointers)) {
|
||||||
|
@ -595,6 +599,8 @@ void mp_bluetooth_deinit(void) {
|
||||||
|
|
||||||
mp_bluetooth_btstack_state = MP_BLUETOOTH_BTSTACK_STATE_OFF;
|
mp_bluetooth_btstack_state = MP_BLUETOOTH_BTSTACK_STATE_OFF;
|
||||||
MP_STATE_PORT(bluetooth_btstack_root_pointers) = NULL;
|
MP_STATE_PORT(bluetooth_btstack_root_pointers) = NULL;
|
||||||
|
|
||||||
|
DEBUG_printf("mp_bluetooth_deinit: complete\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
bool mp_bluetooth_is_active(void) {
|
bool mp_bluetooth_is_active(void) {
|
||||||
|
@ -618,7 +624,7 @@ int mp_bluetooth_gap_set_device_name(const uint8_t *buf, size_t len) {
|
||||||
}
|
}
|
||||||
|
|
||||||
int mp_bluetooth_gap_advertise_start(bool connectable, int32_t interval_us, const uint8_t *adv_data, size_t adv_data_len, const uint8_t *sr_data, size_t sr_data_len) {
|
int mp_bluetooth_gap_advertise_start(bool connectable, int32_t interval_us, const uint8_t *adv_data, size_t adv_data_len, const uint8_t *sr_data, size_t sr_data_len) {
|
||||||
DEBUG_EVENT_printf("mp_bluetooth_gap_advertise_start\n");
|
DEBUG_printf("mp_bluetooth_gap_advertise_start\n");
|
||||||
uint16_t adv_int_min = interval_us / 625;
|
uint16_t adv_int_min = interval_us / 625;
|
||||||
uint16_t adv_int_max = interval_us / 625;
|
uint16_t adv_int_max = interval_us / 625;
|
||||||
uint8_t adv_type = connectable ? 0 : 2;
|
uint8_t adv_type = connectable ? 0 : 2;
|
||||||
|
@ -654,14 +660,14 @@ int mp_bluetooth_gap_advertise_start(bool connectable, int32_t interval_us, cons
|
||||||
}
|
}
|
||||||
|
|
||||||
void mp_bluetooth_gap_advertise_stop(void) {
|
void mp_bluetooth_gap_advertise_stop(void) {
|
||||||
DEBUG_EVENT_printf("mp_bluetooth_gap_advertise_stop\n");
|
DEBUG_printf("mp_bluetooth_gap_advertise_stop\n");
|
||||||
gap_advertisements_enable(false);
|
gap_advertisements_enable(false);
|
||||||
MP_STATE_PORT(bluetooth_btstack_root_pointers)->adv_data_alloc = 0;
|
MP_STATE_PORT(bluetooth_btstack_root_pointers)->adv_data_alloc = 0;
|
||||||
MP_STATE_PORT(bluetooth_btstack_root_pointers)->adv_data = NULL;
|
MP_STATE_PORT(bluetooth_btstack_root_pointers)->adv_data = NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
int mp_bluetooth_gatts_register_service_begin(bool append) {
|
int mp_bluetooth_gatts_register_service_begin(bool append) {
|
||||||
DEBUG_EVENT_printf("mp_bluetooth_gatts_register_service_begin\n");
|
DEBUG_printf("mp_bluetooth_gatts_register_service_begin\n");
|
||||||
if (!append) {
|
if (!append) {
|
||||||
// This will reset the DB.
|
// This will reset the DB.
|
||||||
// Becase the DB is statically allocated, there's no problem with just re-initing it.
|
// Becase the DB is statically allocated, there's no problem with just re-initing it.
|
||||||
|
@ -682,10 +688,10 @@ int mp_bluetooth_gatts_register_service_begin(bool append) {
|
||||||
|
|
||||||
STATIC uint16_t att_read_callback(hci_con_handle_t connection_handle, uint16_t att_handle, uint16_t offset, uint8_t *buffer, uint16_t buffer_size) {
|
STATIC uint16_t att_read_callback(hci_con_handle_t connection_handle, uint16_t att_handle, uint16_t offset, uint8_t *buffer, uint16_t buffer_size) {
|
||||||
(void)connection_handle;
|
(void)connection_handle;
|
||||||
DEBUG_EVENT_printf("btstack: att_read_callback (handle: %u, offset: %u, buffer: %p, size: %u)\n", att_handle, offset, buffer, buffer_size);
|
DEBUG_printf("btstack: att_read_callback (handle: %u, offset: %u, buffer: %p, size: %u)\n", att_handle, offset, buffer, buffer_size);
|
||||||
mp_bluetooth_gatts_db_entry_t *entry = mp_bluetooth_gatts_db_lookup(MP_STATE_PORT(bluetooth_btstack_root_pointers)->gatts_db, att_handle);
|
mp_bluetooth_gatts_db_entry_t *entry = mp_bluetooth_gatts_db_lookup(MP_STATE_PORT(bluetooth_btstack_root_pointers)->gatts_db, att_handle);
|
||||||
if (!entry) {
|
if (!entry) {
|
||||||
DEBUG_EVENT_printf("btstack: att_read_callback handle not found\n");
|
DEBUG_printf("btstack: att_read_callback handle not found\n");
|
||||||
return 0; // TODO: Find status code for not-found.
|
return 0; // TODO: Find status code for not-found.
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -695,10 +701,10 @@ STATIC uint16_t att_read_callback(hci_con_handle_t connection_handle, uint16_t a
|
||||||
STATIC int att_write_callback(hci_con_handle_t connection_handle, uint16_t att_handle, uint16_t transaction_mode, uint16_t offset, uint8_t *buffer, uint16_t buffer_size) {
|
STATIC int att_write_callback(hci_con_handle_t connection_handle, uint16_t att_handle, uint16_t transaction_mode, uint16_t offset, uint8_t *buffer, uint16_t buffer_size) {
|
||||||
(void)offset;
|
(void)offset;
|
||||||
(void)transaction_mode;
|
(void)transaction_mode;
|
||||||
DEBUG_EVENT_printf("btstack: att_write_callback (handle: %u, mode: %u, offset: %u, buffer: %p, size: %u)\n", att_handle, transaction_mode, offset, buffer, buffer_size);
|
DEBUG_printf("btstack: att_write_callback (handle: %u, mode: %u, offset: %u, buffer: %p, size: %u)\n", att_handle, transaction_mode, offset, buffer, buffer_size);
|
||||||
mp_bluetooth_gatts_db_entry_t *entry = mp_bluetooth_gatts_db_lookup(MP_STATE_PORT(bluetooth_btstack_root_pointers)->gatts_db, att_handle);
|
mp_bluetooth_gatts_db_entry_t *entry = mp_bluetooth_gatts_db_lookup(MP_STATE_PORT(bluetooth_btstack_root_pointers)->gatts_db, att_handle);
|
||||||
if (!entry) {
|
if (!entry) {
|
||||||
DEBUG_EVENT_printf("btstack: att_write_callback handle not found\n");
|
DEBUG_printf("btstack: att_write_callback handle not found\n");
|
||||||
return 0; // TODO: Find status code for not-found.
|
return 0; // TODO: Find status code for not-found.
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -720,7 +726,7 @@ STATIC inline uint16_t get_uuid16(const mp_obj_bluetooth_uuid_t *uuid) {
|
||||||
}
|
}
|
||||||
|
|
||||||
int mp_bluetooth_gatts_register_service(mp_obj_bluetooth_uuid_t *service_uuid, mp_obj_bluetooth_uuid_t **characteristic_uuids, uint8_t *characteristic_flags, mp_obj_bluetooth_uuid_t **descriptor_uuids, uint8_t *descriptor_flags, uint8_t *num_descriptors, uint16_t *handles, size_t num_characteristics) {
|
int mp_bluetooth_gatts_register_service(mp_obj_bluetooth_uuid_t *service_uuid, mp_obj_bluetooth_uuid_t **characteristic_uuids, uint8_t *characteristic_flags, mp_obj_bluetooth_uuid_t **descriptor_uuids, uint8_t *descriptor_flags, uint8_t *num_descriptors, uint16_t *handles, size_t num_characteristics) {
|
||||||
DEBUG_EVENT_printf("mp_bluetooth_gatts_register_service\n");
|
DEBUG_printf("mp_bluetooth_gatts_register_service\n");
|
||||||
// Note: btstack expects BE UUIDs (which it immediately convertes to LE).
|
// Note: btstack expects BE UUIDs (which it immediately convertes to LE).
|
||||||
// So we have to convert all our modbluetooth LE UUIDs to BE just for the att_db_util_add_* methods (using get_uuid16 above, and reverse_128 from btstackutil.h).
|
// So we have to convert all our modbluetooth LE UUIDs to BE just for the att_db_util_add_* methods (using get_uuid16 above, and reverse_128 from btstackutil.h).
|
||||||
|
|
||||||
|
@ -764,7 +770,7 @@ int mp_bluetooth_gatts_register_service(mp_obj_bluetooth_uuid_t *service_uuid, m
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
DEBUG_EVENT_printf("Registered char with handle %u\n", handles[handle_index]);
|
DEBUG_printf("mp_bluetooth_gatts_register_service: Registered char with handle %u\n", handles[handle_index]);
|
||||||
++handle_index;
|
++handle_index;
|
||||||
|
|
||||||
for (size_t j = 0; j < num_descriptors[i]; ++j) {
|
for (size_t j = 0; j < num_descriptors[i]; ++j) {
|
||||||
|
@ -782,7 +788,7 @@ int mp_bluetooth_gatts_register_service(mp_obj_bluetooth_uuid_t *service_uuid, m
|
||||||
return MP_EINVAL;
|
return MP_EINVAL;
|
||||||
}
|
}
|
||||||
mp_bluetooth_gatts_db_create_entry(MP_STATE_PORT(bluetooth_btstack_root_pointers)->gatts_db, handles[handle_index], MP_BLUETOOTH_DEFAULT_ATTR_LEN);
|
mp_bluetooth_gatts_db_create_entry(MP_STATE_PORT(bluetooth_btstack_root_pointers)->gatts_db, handles[handle_index], MP_BLUETOOTH_DEFAULT_ATTR_LEN);
|
||||||
DEBUG_EVENT_printf("Registered desc with handle %u\n", handles[handle_index]);
|
DEBUG_printf("mp_bluetooth_gatts_register_service: Registered desc with handle %u\n", handles[handle_index]);
|
||||||
++descriptor_index;
|
++descriptor_index;
|
||||||
++handle_index;
|
++handle_index;
|
||||||
}
|
}
|
||||||
|
@ -792,23 +798,23 @@ int mp_bluetooth_gatts_register_service(mp_obj_bluetooth_uuid_t *service_uuid, m
|
||||||
}
|
}
|
||||||
|
|
||||||
int mp_bluetooth_gatts_register_service_end(void) {
|
int mp_bluetooth_gatts_register_service_end(void) {
|
||||||
DEBUG_EVENT_printf("mp_bluetooth_gatts_register_service_end\n");
|
DEBUG_printf("mp_bluetooth_gatts_register_service_end\n");
|
||||||
att_server_init(att_db_util_get_address(), &att_read_callback, &att_write_callback);
|
att_server_init(att_db_util_get_address(), &att_read_callback, &att_write_callback);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
int mp_bluetooth_gatts_read(uint16_t value_handle, uint8_t **value, size_t *value_len) {
|
int mp_bluetooth_gatts_read(uint16_t value_handle, uint8_t **value, size_t *value_len) {
|
||||||
DEBUG_EVENT_printf("mp_bluetooth_gatts_read\n");
|
DEBUG_printf("mp_bluetooth_gatts_read\n");
|
||||||
return mp_bluetooth_gatts_db_read(MP_STATE_PORT(bluetooth_btstack_root_pointers)->gatts_db, value_handle, value, value_len);
|
return mp_bluetooth_gatts_db_read(MP_STATE_PORT(bluetooth_btstack_root_pointers)->gatts_db, value_handle, value, value_len);
|
||||||
}
|
}
|
||||||
|
|
||||||
int mp_bluetooth_gatts_write(uint16_t value_handle, const uint8_t *value, size_t value_len) {
|
int mp_bluetooth_gatts_write(uint16_t value_handle, const uint8_t *value, size_t value_len) {
|
||||||
DEBUG_EVENT_printf("mp_bluetooth_gatts_write\n");
|
DEBUG_printf("mp_bluetooth_gatts_write\n");
|
||||||
return mp_bluetooth_gatts_db_write(MP_STATE_PORT(bluetooth_btstack_root_pointers)->gatts_db, value_handle, value, value_len);
|
return mp_bluetooth_gatts_db_write(MP_STATE_PORT(bluetooth_btstack_root_pointers)->gatts_db, value_handle, value, value_len);
|
||||||
}
|
}
|
||||||
|
|
||||||
int mp_bluetooth_gatts_notify(uint16_t conn_handle, uint16_t value_handle) {
|
int mp_bluetooth_gatts_notify(uint16_t conn_handle, uint16_t value_handle) {
|
||||||
DEBUG_EVENT_printf("mp_bluetooth_gatts_notify\n");
|
DEBUG_printf("mp_bluetooth_gatts_notify\n");
|
||||||
// Note: btstack doesn't appear to support sending a notification without a value, so include the stored value.
|
// Note: btstack doesn't appear to support sending a notification without a value, so include the stored value.
|
||||||
uint8_t *data = NULL;
|
uint8_t *data = NULL;
|
||||||
size_t len = 0;
|
size_t len = 0;
|
||||||
|
@ -817,7 +823,7 @@ int mp_bluetooth_gatts_notify(uint16_t conn_handle, uint16_t value_handle) {
|
||||||
}
|
}
|
||||||
|
|
||||||
int mp_bluetooth_gatts_notify_send(uint16_t conn_handle, uint16_t value_handle, const uint8_t *value, size_t value_len) {
|
int mp_bluetooth_gatts_notify_send(uint16_t conn_handle, uint16_t value_handle, const uint8_t *value, size_t value_len) {
|
||||||
DEBUG_EVENT_printf("mp_bluetooth_gatts_notify_send\n");
|
DEBUG_printf("mp_bluetooth_gatts_notify_send\n");
|
||||||
|
|
||||||
// Attempt to send immediately. If it succeeds, btstack will copy the buffer.
|
// Attempt to send immediately. If it succeeds, btstack will copy the buffer.
|
||||||
MICROPY_PY_BLUETOOTH_ENTER
|
MICROPY_PY_BLUETOOTH_ENTER
|
||||||
|
@ -825,7 +831,7 @@ int mp_bluetooth_gatts_notify_send(uint16_t conn_handle, uint16_t value_handle,
|
||||||
MICROPY_PY_BLUETOOTH_EXIT
|
MICROPY_PY_BLUETOOTH_EXIT
|
||||||
|
|
||||||
if (err == BTSTACK_ACL_BUFFERS_FULL) {
|
if (err == BTSTACK_ACL_BUFFERS_FULL) {
|
||||||
DEBUG_EVENT_printf("mp_bluetooth_gatts_notify_send: ACL buffer full, scheduling callback\n");
|
DEBUG_printf("mp_bluetooth_gatts_notify_send: ACL buffer full, scheduling callback\n");
|
||||||
// Schedule callback, making a copy of the buffer.
|
// Schedule callback, making a copy of the buffer.
|
||||||
mp_btstack_pending_op_t *pending_op = btstack_enqueue_pending_operation(MP_BLUETOOTH_BTSTACK_PENDING_NOTIFY, conn_handle, value_handle, value, value_len);
|
mp_btstack_pending_op_t *pending_op = btstack_enqueue_pending_operation(MP_BLUETOOTH_BTSTACK_PENDING_NOTIFY, conn_handle, value_handle, value, value_len);
|
||||||
|
|
||||||
|
@ -843,7 +849,7 @@ int mp_bluetooth_gatts_notify_send(uint16_t conn_handle, uint16_t value_handle,
|
||||||
}
|
}
|
||||||
|
|
||||||
int mp_bluetooth_gatts_indicate(uint16_t conn_handle, uint16_t value_handle) {
|
int mp_bluetooth_gatts_indicate(uint16_t conn_handle, uint16_t value_handle) {
|
||||||
DEBUG_EVENT_printf("mp_bluetooth_gatts_indicate\n");
|
DEBUG_printf("mp_bluetooth_gatts_indicate\n");
|
||||||
|
|
||||||
uint8_t *data = NULL;
|
uint8_t *data = NULL;
|
||||||
size_t len = 0;
|
size_t len = 0;
|
||||||
|
@ -858,7 +864,7 @@ int mp_bluetooth_gatts_indicate(uint16_t conn_handle, uint16_t value_handle) {
|
||||||
MICROPY_PY_BLUETOOTH_EXIT
|
MICROPY_PY_BLUETOOTH_EXIT
|
||||||
|
|
||||||
if (err == BTSTACK_ACL_BUFFERS_FULL) {
|
if (err == BTSTACK_ACL_BUFFERS_FULL) {
|
||||||
DEBUG_EVENT_printf("mp_bluetooth_gatts_indicate: ACL buffer full, scheduling callback\n");
|
DEBUG_printf("mp_bluetooth_gatts_indicate: ACL buffer full, scheduling callback\n");
|
||||||
// Schedule callback, making a copy of the buffer.
|
// Schedule callback, making a copy of the buffer.
|
||||||
mp_btstack_pending_op_t *pending_op = btstack_enqueue_pending_operation(MP_BLUETOOTH_BTSTACK_PENDING_INDICATE, conn_handle, value_handle, data, len);
|
mp_btstack_pending_op_t *pending_op = btstack_enqueue_pending_operation(MP_BLUETOOTH_BTSTACK_PENDING_INDICATE, conn_handle, value_handle, data, len);
|
||||||
|
|
||||||
|
@ -876,12 +882,12 @@ int mp_bluetooth_gatts_indicate(uint16_t conn_handle, uint16_t value_handle) {
|
||||||
}
|
}
|
||||||
|
|
||||||
int mp_bluetooth_gatts_set_buffer(uint16_t value_handle, size_t len, bool append) {
|
int mp_bluetooth_gatts_set_buffer(uint16_t value_handle, size_t len, bool append) {
|
||||||
DEBUG_EVENT_printf("mp_bluetooth_gatts_set_buffer\n");
|
DEBUG_printf("mp_bluetooth_gatts_set_buffer\n");
|
||||||
return mp_bluetooth_gatts_db_resize(MP_STATE_PORT(bluetooth_btstack_root_pointers)->gatts_db, value_handle, len, append);
|
return mp_bluetooth_gatts_db_resize(MP_STATE_PORT(bluetooth_btstack_root_pointers)->gatts_db, value_handle, len, append);
|
||||||
}
|
}
|
||||||
|
|
||||||
int mp_bluetooth_gap_disconnect(uint16_t conn_handle) {
|
int mp_bluetooth_gap_disconnect(uint16_t conn_handle) {
|
||||||
DEBUG_EVENT_printf("mp_bluetooth_gap_disconnect\n");
|
DEBUG_printf("mp_bluetooth_gap_disconnect\n");
|
||||||
gap_disconnect(conn_handle);
|
gap_disconnect(conn_handle);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
@ -895,7 +901,7 @@ STATIC void scan_duration_timeout_handler(btstack_timer_source_t *ds) {
|
||||||
}
|
}
|
||||||
|
|
||||||
int mp_bluetooth_gap_scan_start(int32_t duration_ms, int32_t interval_us, int32_t window_us, bool active_scan) {
|
int mp_bluetooth_gap_scan_start(int32_t duration_ms, int32_t interval_us, int32_t window_us, bool active_scan) {
|
||||||
DEBUG_EVENT_printf("mp_bluetooth_gap_scan_start\n");
|
DEBUG_printf("mp_bluetooth_gap_scan_start\n");
|
||||||
|
|
||||||
if (duration_ms > 0) {
|
if (duration_ms > 0) {
|
||||||
btstack_run_loop_set_timer(&scan_duration_timeout, duration_ms);
|
btstack_run_loop_set_timer(&scan_duration_timeout, duration_ms);
|
||||||
|
@ -910,7 +916,7 @@ int mp_bluetooth_gap_scan_start(int32_t duration_ms, int32_t interval_us, int32_
|
||||||
}
|
}
|
||||||
|
|
||||||
int mp_bluetooth_gap_scan_stop(void) {
|
int mp_bluetooth_gap_scan_stop(void) {
|
||||||
DEBUG_EVENT_printf("mp_bluetooth_gap_scan_stop\n");
|
DEBUG_printf("mp_bluetooth_gap_scan_stop\n");
|
||||||
btstack_run_loop_remove_timer(&scan_duration_timeout);
|
btstack_run_loop_remove_timer(&scan_duration_timeout);
|
||||||
gap_stop_scan();
|
gap_stop_scan();
|
||||||
mp_bluetooth_gap_on_scan_complete();
|
mp_bluetooth_gap_on_scan_complete();
|
||||||
|
@ -918,7 +924,7 @@ int mp_bluetooth_gap_scan_stop(void) {
|
||||||
}
|
}
|
||||||
|
|
||||||
int mp_bluetooth_gap_peripheral_connect(uint8_t addr_type, const uint8_t *addr, int32_t duration_ms) {
|
int mp_bluetooth_gap_peripheral_connect(uint8_t addr_type, const uint8_t *addr, int32_t duration_ms) {
|
||||||
DEBUG_EVENT_printf("mp_bluetooth_gap_peripheral_connect\n");
|
DEBUG_printf("mp_bluetooth_gap_peripheral_connect\n");
|
||||||
|
|
||||||
uint16_t conn_scan_interval = 60000 / 625;
|
uint16_t conn_scan_interval = 60000 / 625;
|
||||||
uint16_t conn_scan_window = 30000 / 625;
|
uint16_t conn_scan_window = 30000 / 625;
|
||||||
|
@ -937,7 +943,7 @@ int mp_bluetooth_gap_peripheral_connect(uint8_t addr_type, const uint8_t *addr,
|
||||||
}
|
}
|
||||||
|
|
||||||
int mp_bluetooth_gattc_discover_primary_services(uint16_t conn_handle, const mp_obj_bluetooth_uuid_t *uuid) {
|
int mp_bluetooth_gattc_discover_primary_services(uint16_t conn_handle, const mp_obj_bluetooth_uuid_t *uuid) {
|
||||||
DEBUG_EVENT_printf("mp_bluetooth_gattc_discover_primary_services\n");
|
DEBUG_printf("mp_bluetooth_gattc_discover_primary_services\n");
|
||||||
uint8_t err;
|
uint8_t err;
|
||||||
if (uuid) {
|
if (uuid) {
|
||||||
if (uuid->type == MP_BLUETOOTH_UUID_TYPE_16) {
|
if (uuid->type == MP_BLUETOOTH_UUID_TYPE_16) {
|
||||||
|
@ -947,7 +953,7 @@ int mp_bluetooth_gattc_discover_primary_services(uint16_t conn_handle, const mp_
|
||||||
reverse_128(uuid->data, buffer);
|
reverse_128(uuid->data, buffer);
|
||||||
err = gatt_client_discover_primary_services_by_uuid128(&btstack_packet_handler_discover_services, conn_handle, buffer);
|
err = gatt_client_discover_primary_services_by_uuid128(&btstack_packet_handler_discover_services, conn_handle, buffer);
|
||||||
} else {
|
} else {
|
||||||
DEBUG_EVENT_printf(" --> unknown UUID size\n");
|
DEBUG_printf(" --> unknown UUID size\n");
|
||||||
return MP_EINVAL;
|
return MP_EINVAL;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
|
@ -957,7 +963,7 @@ int mp_bluetooth_gattc_discover_primary_services(uint16_t conn_handle, const mp_
|
||||||
}
|
}
|
||||||
|
|
||||||
int mp_bluetooth_gattc_discover_characteristics(uint16_t conn_handle, uint16_t start_handle, uint16_t end_handle, const mp_obj_bluetooth_uuid_t *uuid) {
|
int mp_bluetooth_gattc_discover_characteristics(uint16_t conn_handle, uint16_t start_handle, uint16_t end_handle, const mp_obj_bluetooth_uuid_t *uuid) {
|
||||||
DEBUG_EVENT_printf("mp_bluetooth_gattc_discover_characteristics\n");
|
DEBUG_printf("mp_bluetooth_gattc_discover_characteristics\n");
|
||||||
gatt_client_service_t service = {
|
gatt_client_service_t service = {
|
||||||
// Only start/end handles needed for gatt_client_discover_characteristics_for_service.
|
// Only start/end handles needed for gatt_client_discover_characteristics_for_service.
|
||||||
.start_group_handle = start_handle,
|
.start_group_handle = start_handle,
|
||||||
|
@ -974,7 +980,7 @@ int mp_bluetooth_gattc_discover_characteristics(uint16_t conn_handle, uint16_t s
|
||||||
reverse_128(uuid->data, buffer);
|
reverse_128(uuid->data, buffer);
|
||||||
err = gatt_client_discover_characteristics_for_service_by_uuid128(&btstack_packet_handler_discover_characteristics, conn_handle, &service, buffer);
|
err = gatt_client_discover_characteristics_for_service_by_uuid128(&btstack_packet_handler_discover_characteristics, conn_handle, &service, buffer);
|
||||||
} else {
|
} else {
|
||||||
DEBUG_EVENT_printf(" --> unknown UUID size\n");
|
DEBUG_printf(" --> unknown UUID size\n");
|
||||||
return MP_EINVAL;
|
return MP_EINVAL;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
|
@ -984,7 +990,7 @@ int mp_bluetooth_gattc_discover_characteristics(uint16_t conn_handle, uint16_t s
|
||||||
}
|
}
|
||||||
|
|
||||||
int mp_bluetooth_gattc_discover_descriptors(uint16_t conn_handle, uint16_t start_handle, uint16_t end_handle) {
|
int mp_bluetooth_gattc_discover_descriptors(uint16_t conn_handle, uint16_t start_handle, uint16_t end_handle) {
|
||||||
DEBUG_EVENT_printf("mp_bluetooth_gattc_discover_descriptors\n");
|
DEBUG_printf("mp_bluetooth_gattc_discover_descriptors\n");
|
||||||
gatt_client_characteristic_t characteristic = {
|
gatt_client_characteristic_t characteristic = {
|
||||||
// Only start/end handles needed for gatt_client_discover_characteristic_descriptors.
|
// Only start/end handles needed for gatt_client_discover_characteristic_descriptors.
|
||||||
.start_handle = start_handle,
|
.start_handle = start_handle,
|
||||||
|
@ -998,12 +1004,12 @@ int mp_bluetooth_gattc_discover_descriptors(uint16_t conn_handle, uint16_t start
|
||||||
}
|
}
|
||||||
|
|
||||||
int mp_bluetooth_gattc_read(uint16_t conn_handle, uint16_t value_handle) {
|
int mp_bluetooth_gattc_read(uint16_t conn_handle, uint16_t value_handle) {
|
||||||
DEBUG_EVENT_printf("mp_bluetooth_gattc_read\n");
|
DEBUG_printf("mp_bluetooth_gattc_read\n");
|
||||||
return btstack_error_to_errno(gatt_client_read_value_of_characteristic_using_value_handle(&btstack_packet_handler_read, conn_handle, value_handle));
|
return btstack_error_to_errno(gatt_client_read_value_of_characteristic_using_value_handle(&btstack_packet_handler_read, conn_handle, value_handle));
|
||||||
}
|
}
|
||||||
|
|
||||||
int mp_bluetooth_gattc_write(uint16_t conn_handle, uint16_t value_handle, const uint8_t *value, size_t *value_len, unsigned int mode) {
|
int mp_bluetooth_gattc_write(uint16_t conn_handle, uint16_t value_handle, const uint8_t *value, size_t *value_len, unsigned int mode) {
|
||||||
DEBUG_EVENT_printf("mp_bluetooth_gattc_write\n");
|
DEBUG_printf("mp_bluetooth_gattc_write\n");
|
||||||
|
|
||||||
// We should be distinguishing between gatt_client_write_value_of_characteristic vs
|
// We should be distinguishing between gatt_client_write_value_of_characteristic vs
|
||||||
// gatt_client_write_characteristic_descriptor_using_descriptor_handle.
|
// gatt_client_write_characteristic_descriptor_using_descriptor_handle.
|
||||||
|
@ -1018,13 +1024,13 @@ int mp_bluetooth_gattc_write(uint16_t conn_handle, uint16_t value_handle, const
|
||||||
// If possible, this will send immediately, copying the buffer directly to the ACL buffer.
|
// If possible, this will send immediately, copying the buffer directly to the ACL buffer.
|
||||||
err = gatt_client_write_value_of_characteristic_without_response(conn_handle, value_handle, *value_len, (uint8_t *)value);
|
err = gatt_client_write_value_of_characteristic_without_response(conn_handle, value_handle, *value_len, (uint8_t *)value);
|
||||||
if (err == GATT_CLIENT_BUSY) {
|
if (err == GATT_CLIENT_BUSY) {
|
||||||
DEBUG_EVENT_printf("mp_bluetooth_gattc_write: client busy\n");
|
DEBUG_printf("mp_bluetooth_gattc_write: client busy\n");
|
||||||
// Can't send right now, need to take a copy of the buffer and add it to the queue.
|
// Can't send right now, need to take a copy of the buffer and add it to the queue.
|
||||||
pending_op = btstack_enqueue_pending_operation(MP_BLUETOOTH_BTSTACK_PENDING_WRITE_NO_RESPONSE, conn_handle, value_handle, value, *value_len);
|
pending_op = btstack_enqueue_pending_operation(MP_BLUETOOTH_BTSTACK_PENDING_WRITE_NO_RESPONSE, conn_handle, value_handle, value, *value_len);
|
||||||
// Notify when this conn_handle can write.
|
// Notify when this conn_handle can write.
|
||||||
err = gatt_client_request_can_write_without_response_event(&btstack_packet_handler_generic, conn_handle);
|
err = gatt_client_request_can_write_without_response_event(&btstack_packet_handler_generic, conn_handle);
|
||||||
} else {
|
} else {
|
||||||
DEBUG_EVENT_printf("mp_bluetooth_gattc_write: other failure: %d\n", err);
|
DEBUG_printf("mp_bluetooth_gattc_write: other failure: %d\n", err);
|
||||||
}
|
}
|
||||||
} else if (mode == MP_BLUETOOTH_WRITE_MODE_WITH_RESPONSE) {
|
} else if (mode == MP_BLUETOOTH_WRITE_MODE_WITH_RESPONSE) {
|
||||||
// Pending operation copies the value buffer and keeps a GC reference
|
// Pending operation copies the value buffer and keeps a GC reference
|
||||||
|
|
|
@ -45,7 +45,7 @@
|
||||||
#define MICROPY_PY_BLUETOOTH_DEFAULT_GAP_NAME "MPY NIMBLE"
|
#define MICROPY_PY_BLUETOOTH_DEFAULT_GAP_NAME "MPY NIMBLE"
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#define DEBUG_EVENT_printf(...) // printf(__VA_ARGS__)
|
#define DEBUG_printf(...) // printf("nimble: " __VA_ARGS__)
|
||||||
|
|
||||||
#define ERRNO_BLUETOOTH_NOT_ACTIVE MP_ENODEV
|
#define ERRNO_BLUETOOTH_NOT_ACTIVE MP_ENODEV
|
||||||
|
|
||||||
|
@ -172,10 +172,12 @@ STATIC void sync_cb(void) {
|
||||||
}
|
}
|
||||||
|
|
||||||
if (MP_BLUETOOTH_DEFAULT_ATTR_LEN > 20) {
|
if (MP_BLUETOOTH_DEFAULT_ATTR_LEN > 20) {
|
||||||
|
DEBUG_printf("sync_cb: Setting MTU\n");
|
||||||
rc = ble_att_set_preferred_mtu(MP_BLUETOOTH_DEFAULT_ATTR_LEN + 3);
|
rc = ble_att_set_preferred_mtu(MP_BLUETOOTH_DEFAULT_ATTR_LEN + 3);
|
||||||
assert(rc == 0);
|
assert(rc == 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
DEBUG_printf("sync_cb: Setting device name\n");
|
||||||
ble_svc_gap_device_name_set(MICROPY_PY_BLUETOOTH_DEFAULT_GAP_NAME);
|
ble_svc_gap_device_name_set(MICROPY_PY_BLUETOOTH_DEFAULT_GAP_NAME);
|
||||||
|
|
||||||
mp_bluetooth_nimble_ble_state = MP_BLUETOOTH_NIMBLE_BLE_STATE_ACTIVE;
|
mp_bluetooth_nimble_ble_state = MP_BLUETOOTH_NIMBLE_BLE_STATE_ACTIVE;
|
||||||
|
@ -188,12 +190,12 @@ STATIC void gatts_register_cb(struct ble_gatt_register_ctxt *ctxt, void *arg) {
|
||||||
switch (ctxt->op) {
|
switch (ctxt->op) {
|
||||||
case BLE_GATT_REGISTER_OP_SVC:
|
case BLE_GATT_REGISTER_OP_SVC:
|
||||||
// Called when a service is successfully registered.
|
// Called when a service is successfully registered.
|
||||||
DEBUG_EVENT_printf("gatts_register_cb: svc uuid=%p handle=%d\n", &ctxt->svc.svc_def->uuid, ctxt->svc.handle);
|
DEBUG_printf("gatts_register_cb: svc uuid=%p handle=%d\n", &ctxt->svc.svc_def->uuid, ctxt->svc.handle);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case BLE_GATT_REGISTER_OP_CHR:
|
case BLE_GATT_REGISTER_OP_CHR:
|
||||||
// Called when a characteristic is successfully registered.
|
// Called when a characteristic is successfully registered.
|
||||||
DEBUG_EVENT_printf("gatts_register_cb: chr uuid=%p def_handle=%d val_handle=%d\n", &ctxt->chr.chr_def->uuid, ctxt->chr.def_handle, ctxt->chr.val_handle);
|
DEBUG_printf("gatts_register_cb: chr uuid=%p def_handle=%d val_handle=%d\n", &ctxt->chr.chr_def->uuid, ctxt->chr.def_handle, ctxt->chr.val_handle);
|
||||||
|
|
||||||
// Note: We will get this event for the default GAP Service, meaning that we allocate storage for the
|
// Note: We will get this event for the default GAP Service, meaning that we allocate storage for the
|
||||||
// "device name" and "appearance" characteristics, even though we never see the reads for them.
|
// "device name" and "appearance" characteristics, even though we never see the reads for them.
|
||||||
|
@ -207,7 +209,7 @@ STATIC void gatts_register_cb(struct ble_gatt_register_ctxt *ctxt, void *arg) {
|
||||||
case BLE_GATT_REGISTER_OP_DSC:
|
case BLE_GATT_REGISTER_OP_DSC:
|
||||||
// Called when a descriptor is successfully registered.
|
// Called when a descriptor is successfully registered.
|
||||||
// Note: This is event is not called for the CCCD.
|
// Note: This is event is not called for the CCCD.
|
||||||
DEBUG_EVENT_printf("gatts_register_cb: dsc uuid=%p handle=%d\n", &ctxt->dsc.dsc_def->uuid, ctxt->dsc.handle);
|
DEBUG_printf("gatts_register_cb: dsc uuid=%p handle=%d\n", &ctxt->dsc.dsc_def->uuid, ctxt->dsc.handle);
|
||||||
|
|
||||||
// See above, safe to alloc.
|
// See above, safe to alloc.
|
||||||
mp_bluetooth_gatts_db_create_entry(MP_STATE_PORT(bluetooth_nimble_root_pointers)->gatts_db, ctxt->dsc.handle, MP_BLUETOOTH_DEFAULT_ATTR_LEN);
|
mp_bluetooth_gatts_db_create_entry(MP_STATE_PORT(bluetooth_nimble_root_pointers)->gatts_db, ctxt->dsc.handle, MP_BLUETOOTH_DEFAULT_ATTR_LEN);
|
||||||
|
@ -217,13 +219,13 @@ STATIC void gatts_register_cb(struct ble_gatt_register_ctxt *ctxt, void *arg) {
|
||||||
break;
|
break;
|
||||||
|
|
||||||
default:
|
default:
|
||||||
DEBUG_EVENT_printf("gatts_register_cb: unknown op %d\n", ctxt->op);
|
DEBUG_printf("gatts_register_cb: unknown op %d\n", ctxt->op);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
STATIC int gap_event_cb(struct ble_gap_event *event, void *arg) {
|
STATIC int gap_event_cb(struct ble_gap_event *event, void *arg) {
|
||||||
DEBUG_EVENT_printf("gap_event_cb: type=%d\n", event->type);
|
DEBUG_printf("gap_event_cb: type=%d\n", event->type);
|
||||||
if (!mp_bluetooth_is_active()) {
|
if (!mp_bluetooth_is_active()) {
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
@ -250,7 +252,7 @@ STATIC int gap_event_cb(struct ble_gap_event *event, void *arg) {
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case BLE_GAP_EVENT_NOTIFY_TX: {
|
case BLE_GAP_EVENT_NOTIFY_TX: {
|
||||||
DEBUG_EVENT_printf("gap_event_cb: notify_tx: %d %d\n", event->notify_tx.indication, event->notify_tx.status);
|
DEBUG_printf("gap_event_cb: notify_tx: %d %d\n", event->notify_tx.indication, event->notify_tx.status);
|
||||||
// This event corresponds to either a sent notify/indicate (status == 0), or an indication confirmation (status != 0).
|
// This event corresponds to either a sent notify/indicate (status == 0), or an indication confirmation (status != 0).
|
||||||
if (event->notify_tx.indication && event->notify_tx.status != 0) {
|
if (event->notify_tx.indication && event->notify_tx.status != 0) {
|
||||||
// Map "done/ack" to 0, otherwise pass the status directly.
|
// Map "done/ack" to 0, otherwise pass the status directly.
|
||||||
|
@ -263,7 +265,7 @@ STATIC int gap_event_cb(struct ble_gap_event *event, void *arg) {
|
||||||
}
|
}
|
||||||
|
|
||||||
int mp_bluetooth_init(void) {
|
int mp_bluetooth_init(void) {
|
||||||
DEBUG_EVENT_printf("mp_bluetooth_init\n");
|
DEBUG_printf("mp_bluetooth_init\n");
|
||||||
// Clean up if necessary.
|
// Clean up if necessary.
|
||||||
mp_bluetooth_deinit();
|
mp_bluetooth_deinit();
|
||||||
|
|
||||||
|
@ -291,7 +293,7 @@ int mp_bluetooth_init(void) {
|
||||||
while (mp_bluetooth_nimble_ble_state != MP_BLUETOOTH_NIMBLE_BLE_STATE_ACTIVE) {
|
while (mp_bluetooth_nimble_ble_state != MP_BLUETOOTH_NIMBLE_BLE_STATE_ACTIVE) {
|
||||||
MICROPY_EVENT_POLL_HOOK
|
MICROPY_EVENT_POLL_HOOK
|
||||||
}
|
}
|
||||||
DEBUG_EVENT_printf("mp_bluetooth_init: ready\n");
|
DEBUG_printf("mp_bluetooth_init: ready\n");
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
@ -304,7 +306,7 @@ STATIC void ble_hs_shutdown_stop_cb(int status, void *arg) {
|
||||||
STATIC struct ble_hs_stop_listener ble_hs_shutdown_stop_listener;
|
STATIC struct ble_hs_stop_listener ble_hs_shutdown_stop_listener;
|
||||||
|
|
||||||
void mp_bluetooth_deinit(void) {
|
void mp_bluetooth_deinit(void) {
|
||||||
DEBUG_EVENT_printf("mp_bluetooth_deinit\n");
|
DEBUG_printf("mp_bluetooth_deinit\n");
|
||||||
if (mp_bluetooth_nimble_ble_state == MP_BLUETOOTH_NIMBLE_BLE_STATE_OFF) {
|
if (mp_bluetooth_nimble_ble_state == MP_BLUETOOTH_NIMBLE_BLE_STATE_OFF) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
@ -325,7 +327,7 @@ void mp_bluetooth_deinit(void) {
|
||||||
mp_bluetooth_nimble_port_deinit();
|
mp_bluetooth_nimble_port_deinit();
|
||||||
|
|
||||||
MP_STATE_PORT(bluetooth_nimble_root_pointers) = NULL;
|
MP_STATE_PORT(bluetooth_nimble_root_pointers) = NULL;
|
||||||
DEBUG_EVENT_printf("mp_bluetooth_deinit: shut down\n");
|
DEBUG_printf("mp_bluetooth_deinit: shut down\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
bool mp_bluetooth_is_active(void) {
|
bool mp_bluetooth_is_active(void) {
|
||||||
|
@ -409,7 +411,7 @@ int mp_bluetooth_gap_advertise_start(bool connectable, int32_t interval_us, cons
|
||||||
if (ret == 0) {
|
if (ret == 0) {
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
DEBUG_EVENT_printf("ble_gap_adv_start: %d\n", ret);
|
DEBUG_printf("ble_gap_adv_start: %d\n", ret);
|
||||||
|
|
||||||
return ble_hs_err_to_errno(ret);
|
return ble_hs_err_to_errno(ret);
|
||||||
}
|
}
|
||||||
|
@ -421,7 +423,7 @@ void mp_bluetooth_gap_advertise_stop(void) {
|
||||||
}
|
}
|
||||||
|
|
||||||
static int characteristic_access_cb(uint16_t conn_handle, uint16_t value_handle, struct ble_gatt_access_ctxt *ctxt, void *arg) {
|
static int characteristic_access_cb(uint16_t conn_handle, uint16_t value_handle, struct ble_gatt_access_ctxt *ctxt, void *arg) {
|
||||||
DEBUG_EVENT_printf("characteristic_access_cb: conn_handle=%u value_handle=%u op=%u\n", conn_handle, value_handle, ctxt->op);
|
DEBUG_printf("characteristic_access_cb: conn_handle=%u value_handle=%u op=%u\n", conn_handle, value_handle, ctxt->op);
|
||||||
if (!mp_bluetooth_is_active()) {
|
if (!mp_bluetooth_is_active()) {
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
@ -640,7 +642,7 @@ STATIC void gattc_on_data_available(uint8_t event, uint16_t conn_handle, uint16_
|
||||||
}
|
}
|
||||||
|
|
||||||
STATIC int gap_scan_cb(struct ble_gap_event *event, void *arg) {
|
STATIC int gap_scan_cb(struct ble_gap_event *event, void *arg) {
|
||||||
DEBUG_EVENT_printf("gap_scan_cb: event=%d type=%d\n", event->type, event->type == BLE_GAP_EVENT_DISC ? event->disc.event_type : -1);
|
DEBUG_printf("gap_scan_cb: event=%d type=%d\n", event->type, event->type == BLE_GAP_EVENT_DISC ? event->disc.event_type : -1);
|
||||||
if (!mp_bluetooth_is_active()) {
|
if (!mp_bluetooth_is_active()) {
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
@ -681,6 +683,7 @@ int mp_bluetooth_gap_scan_start(int32_t duration_ms, int32_t interval_us, int32_
|
||||||
}
|
}
|
||||||
|
|
||||||
int mp_bluetooth_gap_scan_stop(void) {
|
int mp_bluetooth_gap_scan_stop(void) {
|
||||||
|
DEBUG_printf("mp_bluetooth_gap_scan_stop\n");
|
||||||
if (!mp_bluetooth_is_active()) {
|
if (!mp_bluetooth_is_active()) {
|
||||||
return ERRNO_BLUETOOTH_NOT_ACTIVE;
|
return ERRNO_BLUETOOTH_NOT_ACTIVE;
|
||||||
}
|
}
|
||||||
|
@ -697,7 +700,7 @@ int mp_bluetooth_gap_scan_stop(void) {
|
||||||
|
|
||||||
// Central role: GAP events for a connected peripheral.
|
// Central role: GAP events for a connected peripheral.
|
||||||
STATIC int peripheral_gap_event_cb(struct ble_gap_event *event, void *arg) {
|
STATIC int peripheral_gap_event_cb(struct ble_gap_event *event, void *arg) {
|
||||||
DEBUG_EVENT_printf("peripheral_gap_event_cb: event=%d\n", event->type);
|
DEBUG_printf("peripheral_gap_event_cb: event=%d\n", event->type);
|
||||||
if (!mp_bluetooth_is_active()) {
|
if (!mp_bluetooth_is_active()) {
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
@ -745,6 +748,7 @@ STATIC int peripheral_gap_event_cb(struct ble_gap_event *event, void *arg) {
|
||||||
}
|
}
|
||||||
|
|
||||||
int mp_bluetooth_gap_peripheral_connect(uint8_t addr_type, const uint8_t *addr, int32_t duration_ms) {
|
int mp_bluetooth_gap_peripheral_connect(uint8_t addr_type, const uint8_t *addr, int32_t duration_ms) {
|
||||||
|
DEBUG_printf("mp_bluetooth_gap_peripheral_connect\n");
|
||||||
if (!mp_bluetooth_is_active()) {
|
if (!mp_bluetooth_is_active()) {
|
||||||
return ERRNO_BLUETOOTH_NOT_ACTIVE;
|
return ERRNO_BLUETOOTH_NOT_ACTIVE;
|
||||||
}
|
}
|
||||||
|
@ -770,7 +774,7 @@ int mp_bluetooth_gap_peripheral_connect(uint8_t addr_type, const uint8_t *addr,
|
||||||
}
|
}
|
||||||
|
|
||||||
STATIC int peripheral_discover_service_cb(uint16_t conn_handle, const struct ble_gatt_error *error, const struct ble_gatt_svc *service, void *arg) {
|
STATIC int peripheral_discover_service_cb(uint16_t conn_handle, const struct ble_gatt_error *error, const struct ble_gatt_svc *service, void *arg) {
|
||||||
DEBUG_EVENT_printf("peripheral_discover_service_cb: conn_handle=%d status=%d start_handle=%d\n", conn_handle, error->status, service ? service->start_handle : -1);
|
DEBUG_printf("peripheral_discover_service_cb: conn_handle=%d status=%d start_handle=%d\n", conn_handle, error->status, service ? service->start_handle : -1);
|
||||||
if (!mp_bluetooth_is_active()) {
|
if (!mp_bluetooth_is_active()) {
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
@ -799,7 +803,7 @@ int mp_bluetooth_gattc_discover_primary_services(uint16_t conn_handle, const mp_
|
||||||
}
|
}
|
||||||
|
|
||||||
STATIC int ble_gatt_characteristic_cb(uint16_t conn_handle, const struct ble_gatt_error *error, const struct ble_gatt_chr *characteristic, void *arg) {
|
STATIC int ble_gatt_characteristic_cb(uint16_t conn_handle, const struct ble_gatt_error *error, const struct ble_gatt_chr *characteristic, void *arg) {
|
||||||
DEBUG_EVENT_printf("ble_gatt_characteristic_cb: conn_handle=%d status=%d def_handle=%d val_handle=%d\n", conn_handle, error->status, characteristic ? characteristic->def_handle : -1, characteristic ? characteristic->val_handle : -1);
|
DEBUG_printf("ble_gatt_characteristic_cb: conn_handle=%d status=%d def_handle=%d val_handle=%d\n", conn_handle, error->status, characteristic ? characteristic->def_handle : -1, characteristic ? characteristic->val_handle : -1);
|
||||||
if (!mp_bluetooth_is_active()) {
|
if (!mp_bluetooth_is_active()) {
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
@ -828,7 +832,7 @@ int mp_bluetooth_gattc_discover_characteristics(uint16_t conn_handle, uint16_t s
|
||||||
}
|
}
|
||||||
|
|
||||||
STATIC int ble_gatt_descriptor_cb(uint16_t conn_handle, const struct ble_gatt_error *error, uint16_t characteristic_val_handle, const struct ble_gatt_dsc *descriptor, void *arg) {
|
STATIC int ble_gatt_descriptor_cb(uint16_t conn_handle, const struct ble_gatt_error *error, uint16_t characteristic_val_handle, const struct ble_gatt_dsc *descriptor, void *arg) {
|
||||||
DEBUG_EVENT_printf("ble_gatt_descriptor_cb: conn_handle=%d status=%d chr_handle=%d dsc_handle=%d\n", conn_handle, error->status, characteristic_val_handle, descriptor ? descriptor->handle : -1);
|
DEBUG_printf("ble_gatt_descriptor_cb: conn_handle=%d status=%d chr_handle=%d dsc_handle=%d\n", conn_handle, error->status, characteristic_val_handle, descriptor ? descriptor->handle : -1);
|
||||||
if (!mp_bluetooth_is_active()) {
|
if (!mp_bluetooth_is_active()) {
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
@ -850,7 +854,7 @@ int mp_bluetooth_gattc_discover_descriptors(uint16_t conn_handle, uint16_t start
|
||||||
}
|
}
|
||||||
|
|
||||||
STATIC int ble_gatt_attr_read_cb(uint16_t conn_handle, const struct ble_gatt_error *error, struct ble_gatt_attr *attr, void *arg) {
|
STATIC int ble_gatt_attr_read_cb(uint16_t conn_handle, const struct ble_gatt_error *error, struct ble_gatt_attr *attr, void *arg) {
|
||||||
DEBUG_EVENT_printf("ble_gatt_attr_read_cb: conn_handle=%d status=%d handle=%d\n", conn_handle, error->status, attr ? attr->handle : -1);
|
DEBUG_printf("ble_gatt_attr_read_cb: conn_handle=%d status=%d handle=%d\n", conn_handle, error->status, attr ? attr->handle : -1);
|
||||||
if (!mp_bluetooth_is_active()) {
|
if (!mp_bluetooth_is_active()) {
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
@ -871,7 +875,7 @@ int mp_bluetooth_gattc_read(uint16_t conn_handle, uint16_t value_handle) {
|
||||||
}
|
}
|
||||||
|
|
||||||
STATIC int ble_gatt_attr_write_cb(uint16_t conn_handle, const struct ble_gatt_error *error, struct ble_gatt_attr *attr, void *arg) {
|
STATIC int ble_gatt_attr_write_cb(uint16_t conn_handle, const struct ble_gatt_error *error, struct ble_gatt_attr *attr, void *arg) {
|
||||||
DEBUG_EVENT_printf("ble_gatt_attr_write_cb: conn_handle=%d status=%d handle=%d\n", conn_handle, error->status, attr ? attr->handle : -1);
|
DEBUG_printf("ble_gatt_attr_write_cb: conn_handle=%d status=%d handle=%d\n", conn_handle, error->status, attr ? attr->handle : -1);
|
||||||
if (!mp_bluetooth_is_active()) {
|
if (!mp_bluetooth_is_active()) {
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue