@ -35,7 +35,7 @@
# 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
# 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 ;
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 ) {
return 0 ;
} 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 ) {
MICROPY_PY_BLUETOOTH_ENTER
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=%l u \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=%z u \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 ) {
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 ) ;
( void ) err ;
} else {
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 ) ;
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 ) ;
( 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.
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=%l u \n " , op_type , conn_handle , value_handle , len ) ;
DEBUG_printf ( " btstack_enqueue_pending_operation op_type=%d conn_handle=%d value_handle=%d len=%z u \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 ) ;
pending_op - > op_type = op_type ;
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.
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
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_init ( & it , & MP_STATE_PORT ( bluetooth_btstack_root_pointers ) - > pending_ops ) ;
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 ) ;
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=%l u \n " , pending_op - > value_handle , pending_op - > len ) ;
DEBUG_printf ( " btstack_finish_pending_operation: found value_handle=%d len=%z u \n " , pending_op - > value_handle , pending_op - > len ) ;
btstack_remove_pending_operation ( pending_op , del ) ;
MICROPY_PY_BLUETOOTH_EXIT
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
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 ) {
( void ) channel ;
( 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 ) {
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 ) ;
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.
// So we use the HCI_EVENT_LE_META event directly in the main packet handler.
} 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 ) {
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 value_handle = att_event_handle_value_indication_complete_get_attribute_handle ( 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 ) {
// Ignore, duplicated by att_server.c.
} 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 ) {
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 ) {
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 ) ;
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 ) {
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 ) ;
@ -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 ) {
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 ) {
// Signal that initialisation has completed.
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 ;
}
} 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 ) {
DEBUG_EVENT_ printf ( " --> hci command complete \n " ) ;
DEBUG_printf ( " --> hci command complete \n " ) ;
} 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 ) {
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 ) {
DEBUG_EVENT_ printf ( " --> btstack # conns changed \n " ) ;
DEBUG_printf ( " --> btstack # conns changed \n " ) ;
} 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 ) {
DEBUG_EVENT_ printf ( " --> hci MTU \n " ) ;
DEBUG_printf ( " --> hci MTU \n " ) ;
} 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 ) ;
const hci_connection_t * conn = hci_connection_for_handle ( conn_handle ) ;
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 ) ;
# if MICROPY_PY_BLUETOOTH_ENABLE_CENTRAL_MODE
} 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 ;
gap_event_advertising_report_get_address ( packet , address ) ;
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 ) {
uint16_t conn_handle = gatt_event_query_complete_get_handle ( 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 ) {
// TODO there is no value_handle available to pass here.
// 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 ) ;
}
} 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 ) ;
gatt_client_service_t 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_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 ) {
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 ) ;
gatt_client_characteristic_t 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_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 ) {
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 ) ;
gatt_client_characteristic_descriptor_t 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_bluetooth_gattc_on_descriptor_result ( conn_handle , descriptor . handle , & descriptor_uuid ) ;
} 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 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 ) ;
@ -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_end ( atomic_state ) ;
} 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 value_handle = gatt_event_notification_get_value_handle ( 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_end ( atomic_state ) ;
} 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 value_handle = gatt_event_indication_get_value_handle ( 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 ) ;
} else if ( event_type = = GATT_EVENT_CAN_WRITE_WITHOUT_RESPONSE ) {
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 */ ) ;
if ( pending_op ) {
DEBUG_EVENT_ printf ( " --> ready for value_handle=%d len=%l u \n " , pending_op - > value_handle , pending_op - > len ) ;
DEBUG_printf ( " --> ready for value_handle=%d len=%z u \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 ) ;
// Note: Can't "del" the pending_op from IRQ context. Leave it for the GC.
}
# endif // MICROPY_PY_BLUETOOTH_ENABLE_CENTRAL_MODE
} 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 ) {
DEBUG_EVENT_ printf ( " mp_bluetooth_init \n " ) ;
DEBUG_printf ( " mp_bluetooth_init \n " ) ;
if ( mp_bluetooth_btstack_state = = MP_BLUETOOTH_BTSTACK_STATE_ACTIVE ) {
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_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.
mp_bluetooth_btstack_port_start ( ) ;
while ( mp_bluetooth_btstack_state = = MP_BLUETOOTH_BTSTACK_STATE_STARTING ) {
@ -547,6 +549,8 @@ int mp_bluetooth_init(void) {
// Check for timeout.
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.
mp_bluetooth_btstack_state = MP_BLUETOOTH_BTSTACK_STATE_OFF ;
// Attempt a shutdown (may not do anything).
@ -566,7 +570,7 @@ int mp_bluetooth_init(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.
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_STATE_PORT ( bluetooth_btstack_root_pointers ) = NULL ;
DEBUG_printf ( " mp_bluetooth_deinit: complete \n " ) ;
}
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 ) {
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_max = interval_us / 625 ;
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 ) {
DEBUG_EVENT_ printf ( " mp_bluetooth_gap_advertise_stop \n " ) ;
DEBUG_printf ( " mp_bluetooth_gap_advertise_stop \n " ) ;
gap_advertisements_enable ( false ) ;
MP_STATE_PORT ( bluetooth_btstack_root_pointers ) - > adv_data_alloc = 0 ;
MP_STATE_PORT ( bluetooth_btstack_root_pointers ) - > adv_data = NULL ;
}
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 ) {
// This will reset the DB.
// 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 ) {
( 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 ) ;
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.
}
@ -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 ) {
( void ) offset ;
( 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 ) ;
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.
}
@ -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 ) {
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).
// 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 ;
}
}
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 ;
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 ;
}
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 ;
+ + 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 ) {
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 ) ;
return 0 ;
}
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 ) ;
}
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 ) ;
}
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.
uint8_t * data = NULL ;
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 ) {
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.
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
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.
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 ) {
DEBUG_EVENT_ printf ( " mp_bluetooth_gatts_indicate \n " ) ;
DEBUG_printf ( " mp_bluetooth_gatts_indicate \n " ) ;
uint8_t * data = NULL ;
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
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.
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 ) {
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 ) ;
}
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 ) ;
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 ) {
DEBUG_EVENT_ printf ( " mp_bluetooth_gap_scan_start \n " ) ;
DEBUG_printf ( " mp_bluetooth_gap_scan_start \n " ) ;
if ( duration_ms > 0 ) {
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 ) {
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 ) ;
gap_stop_scan ( ) ;
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 ) {
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_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 ) {
DEBUG_EVENT_ printf ( " mp_bluetooth_gattc_discover_primary_services \n " ) ;
DEBUG_printf ( " mp_bluetooth_gattc_discover_primary_services \n " ) ;
uint8_t err ;
if ( uuid ) {
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 ) ;
err = gatt_client_discover_primary_services_by_uuid128 ( & btstack_packet_handler_discover_services , conn_handle , buffer ) ;
} else {
DEBUG_EVENT_ printf ( " --> unknown UUID size \n " ) ;
DEBUG_printf ( " --> unknown UUID size \n " ) ;
return MP_EINVAL ;
}
} 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 ) {
DEBUG_EVENT_ printf ( " mp_bluetooth_gattc_discover_characteristics \n " ) ;
DEBUG_printf ( " mp_bluetooth_gattc_discover_characteristics \n " ) ;
gatt_client_service_t service = {
// Only start/end handles needed for gatt_client_discover_characteristics_for_service.
. 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 ) ;
err = gatt_client_discover_characteristics_for_service_by_uuid128 ( & btstack_packet_handler_discover_characteristics , conn_handle , & service , buffer ) ;
} else {
DEBUG_EVENT_ printf ( " --> unknown UUID size \n " ) ;
DEBUG_printf ( " --> unknown UUID size \n " ) ;
return MP_EINVAL ;
}
} 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 ) {
DEBUG_EVENT_ printf ( " mp_bluetooth_gattc_discover_descriptors \n " ) ;
DEBUG_printf ( " mp_bluetooth_gattc_discover_descriptors \n " ) ;
gatt_client_characteristic_t characteristic = {
// Only start/end handles needed for gatt_client_discover_characteristic_descriptors.
. 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 ) {
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 ) ) ;
}
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
// 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.
err = gatt_client_write_value_of_characteristic_without_response ( conn_handle , value_handle , * value_len , ( uint8_t * ) value ) ;
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.
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.
err = gatt_client_request_can_write_without_response_event ( & btstack_packet_handler_generic , conn_handle ) ;
} 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 ) {
// Pending operation copies the value buffer and keeps a GC reference