2020-02-19 22:40:55 -05:00
/*
* This file is part of the MicroPython project , http : //micropython.org/
*
* The MIT License ( MIT )
*
* Copyright ( c ) 2020 Damien P . George
*
* Permission is hereby granted , free of charge , to any person obtaining a copy
* of this software and associated documentation files ( the " Software " ) , to deal
* in the Software without restriction , including without limitation the rights
* to use , copy , modify , merge , publish , distribute , sublicense , and / or sell
* copies of the Software , and to permit persons to whom the Software is
* furnished to do so , subject to the following conditions :
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software .
*
* THE SOFTWARE IS PROVIDED " AS IS " , WITHOUT WARRANTY OF ANY KIND , EXPRESS OR
* IMPLIED , INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY ,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT . IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM , DAMAGES OR OTHER
* LIABILITY , WHETHER IN AN ACTION OF CONTRACT , TORT OR OTHERWISE , ARISING FROM ,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE .
*/
2020-02-19 22:43:01 -05:00
# include "py/runtime.h"
# include "py/mperrno.h"
# include "py/mphal.h"
2020-02-19 22:40:55 -05:00
# if MICROPY_PY_BLUETOOTH && MICROPY_BLUETOOTH_BTSTACK
# include "extmod/btstack/modbluetooth_btstack.h"
# include "extmod/modbluetooth.h"
2020-02-19 22:43:01 -05:00
# include "lib/btstack/src/btstack.h"
2020-06-18 08:20:20 -04:00
# define DEBUG_EVENT_printf(...) // printf(__VA_ARGS__)
2020-02-19 22:43:52 -05:00
2020-05-07 23:54:10 -04:00
# ifndef MICROPY_PY_BLUETOOTH_DEFAULT_GAP_NAME
# define MICROPY_PY_BLUETOOTH_DEFAULT_GAP_NAME "MPY BTSTACK"
# endif
2020-04-07 01:01:26 -04:00
// How long to wait for a controller to init/deinit.
// Some controllers can take up to 5-6 seconds in normal operation.
STATIC const uint32_t BTSTACK_INIT_DEINIT_TIMEOUT_MS = 15000 ;
2020-05-07 23:54:10 -04:00
// We need to know the attribute handle for the GAP device name (see GAP_DEVICE_NAME_UUID)
// so it can be put into the gatts_db before registering the services, and accessed
// efficiently when requesting an attribute in att_read_callback. Because this is the
// first characteristic of the first service, it always has a handle value of 3.
STATIC const uint16_t BTSTACK_GAP_DEVICE_NAME_HANDLE = 3 ;
2020-02-19 22:43:01 -05:00
volatile int mp_bluetooth_btstack_state = MP_BLUETOOTH_BTSTACK_STATE_OFF ;
2020-02-19 22:43:52 -05:00
STATIC int btstack_error_to_errno ( int err ) {
2020-05-11 07:46:56 -04:00
DEBUG_EVENT_printf ( " --> btstack error: %d \n " , err ) ;
2020-02-19 22:43:52 -05:00
if ( err = = ERROR_CODE_SUCCESS ) {
return 0 ;
2020-05-11 07:46:56 -04:00
} else if ( err = = BTSTACK_ACL_BUFFERS_FULL | | err = = BTSTACK_MEMORY_ALLOC_FAILED ) {
2020-02-19 22:43:52 -05:00
return MP_ENOMEM ;
2020-05-11 07:46:56 -04:00
} else if ( err = = GATT_CLIENT_IN_WRONG_STATE ) {
return MP_EALREADY ;
} else if ( err = = GATT_CLIENT_BUSY ) {
return MP_EBUSY ;
} else if ( err = = GATT_CLIENT_NOT_CONNECTED ) {
return MP_ENOTCONN ;
2020-02-19 22:43:52 -05:00
} else {
return MP_EINVAL ;
}
}
2020-02-19 22:44:13 -05:00
STATIC mp_obj_bluetooth_uuid_t create_mp_uuid ( uint16_t uuid16 , const uint8_t * uuid128 ) {
mp_obj_bluetooth_uuid_t result ;
if ( uuid16 ! = 0 ) {
result . data [ 0 ] = uuid16 & 0xff ;
result . data [ 1 ] = ( uuid16 > > 8 ) & 0xff ;
result . type = MP_BLUETOOTH_UUID_TYPE_16 ;
} else {
reverse_128 ( uuid128 , result . data ) ;
result . type = MP_BLUETOOTH_UUID_TYPE_128 ;
}
return result ;
}
2020-06-16 07:53:22 -04:00
// Notes on supporting background ops (e.g. an attempt to gatts_notify while
// an existing notification is in progress):
// GATTS Notify/Indicate (att_server_notify/indicate)
// * When available, copies buffer immediately.
// * Otherwise fails with BTSTACK_ACL_BUFFERS_FULL
// * Use att_server_request_to_send_notification/indication to get callback
// * Takes btstack_context_callback_registration_t (and takes ownership) and conn_handle.
// * Callback is invoked with just the context member of the btstack_context_callback_registration_t
// GATTC Write without response (gatt_client_write_value_of_characteristic_without_response)
// * When available, copies buffer immediately.
// * Otherwise, fails with GATT_CLIENT_BUSY.
// * Use gatt_client_request_can_write_without_response_event to get callback
// * Takes btstack_packet_handler_t (function pointer) and conn_handle
// * Callback is invoked, use gatt_event_can_write_without_response_get_handle to get the conn_handle (no other context)
// * There can only be one pending gatt_client_request_can_write_without_response_event (otherwise we fail with EALREADY).
// GATTC Write with response (gatt_client_write_value_of_characteristic)
// * When peripheral is available, takes ownership of buffer.
// * Otherwise, fails with GATT_CLIENT_IN_WRONG_STATE (we fail the operation).
// * Raises GATT_EVENT_QUERY_COMPLETE to the supplied packet handler.
// For notify/indicate/write-without-response that proceed immediately, nothing extra required.
// For all other cases, buffer needs to be copied and protected from GC.
// For notify/indicate:
// * btstack_context_callback_registration_t:
// * needs to be malloc'ed
// * needs to be protected from GC
// * context arg needs to point back to the callback registration so it can be freed and un-protected
// For write-without-response
// * only the conn_handle is available in the callback
// * so we need a queue of conn_handle->(value_handle, copied buffer)
// Pending operation types.
enum {
// Queued for sending when possible.
MP_BLUETOOTH_BTSTACK_PENDING_NOTIFY , // Waiting for context callback
MP_BLUETOOTH_BTSTACK_PENDING_INDICATE , // Waiting for context callback
MP_BLUETOOTH_BTSTACK_PENDING_WRITE_NO_RESPONSE , // Waiting for conn handle
// Hold buffer pointer until complete.
MP_BLUETOOTH_BTSTACK_PENDING_WRITE , // Waiting for write done event
} ;
// Pending operation:
// - Holds a GC reference to the copied outgoing buffer.
// - Provides enough information for the callback handler to execute the desired operation.
struct _mp_btstack_pending_op_t {
btstack_linked_item_t * next ; // Must be first field to match btstack_linked_item.
// See enum above.
uint16_t op_type ;
// For all op types.
uint16_t conn_handle ;
uint16_t value_handle ;
// For notify/indicate only.
// context_registration.context will point back to this struct.
btstack_context_callback_registration_t context_registration ;
// For notify/indicate/write-without-response, this is the actual buffer to send.
// For write-with-response, just holding onto the buffer for GC ref.
size_t len ;
uint8_t buf [ ] ;
} ;
// Must hold MICROPY_PY_BLUETOOTH_ENTER.
STATIC void btstack_remove_pending_operation ( mp_btstack_pending_op_t * pending_op , bool del ) {
bool removed = btstack_linked_list_remove ( & MP_STATE_PORT ( bluetooth_btstack_root_pointers ) - > pending_ops , ( btstack_linked_item_t * ) pending_op ) ;
assert ( removed ) ;
( void ) removed ;
if ( del ) {
m_del_var ( mp_btstack_pending_op_t , uint8_t , pending_op - > len , pending_op ) ;
}
}
// Called in response to a gatts_notify/indicate being unable to complete, which then calls
// att_server_request_to_send_notification.
// We now have an opportunity to re-try the operation with an empty ACL buffer.
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=%lu \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 ) ;
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 ) ;
assert ( err = = ERROR_CODE_SUCCESS ) ;
( void ) err ;
}
// Can't free the pending op as we're in IRQ context. Leave it for the GC.
btstack_remove_pending_operation ( pending_op , false /* del */ ) ;
MICROPY_PY_BLUETOOTH_EXIT
}
// 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=%lu \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 ;
pending_op - > value_handle = value_handle ;
pending_op - > len = len ;
memcpy ( pending_op - > buf , buf , len ) ;
if ( op_type = = MP_BLUETOOTH_BTSTACK_PENDING_NOTIFY | | op_type = = MP_BLUETOOTH_BTSTACK_PENDING_INDICATE ) {
pending_op - > context_registration . callback = & btstack_notify_indicate_ready_handler ;
pending_op - > context_registration . context = pending_op ;
}
MICROPY_PY_BLUETOOTH_ENTER
bool added = btstack_linked_list_add ( & MP_STATE_PORT ( bluetooth_btstack_root_pointers ) - > pending_ops , ( btstack_linked_item_t * ) pending_op ) ;
assert ( added ) ;
( void ) added ;
MICROPY_PY_BLUETOOTH_EXIT
return pending_op ;
}
# if MICROPY_PY_BLUETOOTH_ENABLE_CENTRAL_MODE
// Cleans up a pending op of the specified type for this conn_handle (and if specified, value_handle).
// Used by MP_BLUETOOTH_BTSTACK_PENDING_WRITE and MP_BLUETOOTH_BTSTACK_PENDING_WRITE_NO_RESPONSE.
// At the moment, both will set value_handle=0xffff as the events do not know their value_handle.
// TODO: Can we make btstack give us the value_handle for regular write (with response) so that we
// 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 ) ;
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=%lu \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 " ) ;
MICROPY_PY_BLUETOOTH_EXIT
return NULL ;
}
# endif
2020-05-11 07:46:56 -04:00
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 ) ;
2020-02-19 22:44:13 -05:00
if ( packet_type ! = HCI_EVENT_PACKET ) {
return ;
}
uint8_t event_type = hci_event_packet_get_type ( packet ) ;
if ( event_type = = ATT_EVENT_CONNECTED ) {
DEBUG_EVENT_printf ( " --> att connected \n " ) ;
} else if ( event_type = = ATT_EVENT_DISCONNECTED ) {
DEBUG_EVENT_printf ( " --> att disconnected \n " ) ;
} else if ( event_type = = HCI_EVENT_LE_META ) {
DEBUG_EVENT_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 ) ;
2020-02-19 22:43:52 -05:00
bd_addr_t addr ;
2020-02-19 22:44:13 -05:00
hci_subevent_le_connection_complete_get_peer_address ( packet , addr ) ;
uint16_t irq_event ;
if ( hci_subevent_le_connection_complete_get_role ( packet ) = = 0 ) {
// Master role.
irq_event = MP_BLUETOOTH_IRQ_PERIPHERAL_CONNECT ;
} else {
// Slave role.
irq_event = MP_BLUETOOTH_IRQ_CENTRAL_CONNECT ;
}
mp_bluetooth_gap_on_connected_disconnected ( irq_event , conn_handle , addr_type , addr ) ;
}
} else if ( event_type = = BTSTACK_EVENT_STATE ) {
2020-04-07 01:01:26 -04:00
uint8_t state = btstack_event_state_get_state ( packet ) ;
DEBUG_EVENT_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 ;
} else if ( state = = HCI_STATE_OFF ) {
// Signal that de-initialisation has completed.
mp_bluetooth_btstack_state = MP_BLUETOOTH_BTSTACK_STATE_OFF ;
}
2020-02-19 22:44:13 -05:00
} else if ( event_type = = HCI_EVENT_TRANSPORT_PACKET_SENT ) {
DEBUG_EVENT_printf ( " --> hci transport packet set \n " ) ;
} else if ( event_type = = HCI_EVENT_COMMAND_COMPLETE ) {
DEBUG_EVENT_printf ( " --> hci command complete \n " ) ;
} else if ( event_type = = HCI_EVENT_COMMAND_STATUS ) {
DEBUG_EVENT_printf ( " --> hci command status \n " ) ;
} else if ( event_type = = HCI_EVENT_NUMBER_OF_COMPLETED_PACKETS ) {
DEBUG_EVENT_printf ( " --> hci number of completed packets \n " ) ;
} else if ( event_type = = BTSTACK_EVENT_NR_CONNECTIONS_CHANGED ) {
DEBUG_EVENT_printf ( " --> btstack # conns changed \n " ) ;
} else if ( event_type = = HCI_EVENT_VENDOR_SPECIFIC ) {
DEBUG_EVENT_printf ( " --> hci vendor specific \n " ) ;
} else if ( event_type = = GAP_EVENT_ADVERTISING_REPORT ) {
DEBUG_EVENT_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 ) ;
uint8_t address_type = gap_event_advertising_report_get_address_type ( packet ) ;
int8_t rssi = gap_event_advertising_report_get_rssi ( packet ) ;
uint8_t length = gap_event_advertising_report_get_data_length ( packet ) ;
const uint8_t * data = gap_event_advertising_report_get_data ( packet ) ;
2020-04-05 01:31:53 -04:00
mp_bluetooth_gap_on_scan_result ( address_type , address , adv_event_type , rssi , data , length ) ;
2020-02-19 22:44:13 -05:00
} else if ( event_type = = HCI_EVENT_DISCONNECTION_COMPLETE ) {
DEBUG_EVENT_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 ;
if ( conn = = NULL | | conn - > role = = 0 ) {
// Master role.
irq_event = MP_BLUETOOTH_IRQ_PERIPHERAL_DISCONNECT ;
2020-02-19 22:43:28 -05:00
} else {
2020-02-19 22:44:13 -05:00
// Slave role.
irq_event = MP_BLUETOOTH_IRQ_CENTRAL_DISCONNECT ;
2020-02-19 22:43:28 -05:00
}
2020-02-19 22:44:13 -05:00
uint8_t addr [ 6 ] = { 0 } ;
mp_bluetooth_gap_on_connected_disconnected ( irq_event , conn_handle , 0xff , addr ) ;
2020-03-05 20:37:37 -05:00
# if MICROPY_PY_BLUETOOTH_ENABLE_CENTRAL_MODE
2020-02-19 22:44:13 -05:00
} else if ( event_type = = GATT_EVENT_QUERY_COMPLETE ) {
2020-05-11 07:46:56 -04:00
uint16_t conn_handle = gatt_event_query_complete_get_handle ( packet ) ;
uint16_t status = gatt_event_query_complete_get_att_status ( packet ) ;
2020-06-16 07:53:22 -04:00
DEBUG_EVENT_printf ( " --> gatt query complete irq=%d conn_handle=%d status=%d \n " , irq , conn_handle , status ) ;
2020-05-11 07:46:56 -04:00
if ( irq = = MP_BLUETOOTH_IRQ_GATTC_READ_DONE | | irq = = MP_BLUETOOTH_IRQ_GATTC_WRITE_DONE ) {
2020-06-16 07:53:22 -04:00
// TODO there is no value_handle available to pass here.
// TODO try and get this implemented in btstack.
mp_bluetooth_gattc_on_read_write_status ( irq , conn_handle , 0xffff , status ) ;
// Unref the saved buffer for the write operation on this conn_handle.
if ( irq = = MP_BLUETOOTH_IRQ_GATTC_WRITE_DONE ) {
btstack_finish_pending_operation ( MP_BLUETOOTH_BTSTACK_PENDING_WRITE , conn_handle , 0xffff , false /* del */ ) ;
}
2020-05-11 07:46:56 -04:00
} else if ( irq = = MP_BLUETOOTH_IRQ_GATTC_SERVICE_DONE | |
irq = = MP_BLUETOOTH_IRQ_GATTC_CHARACTERISTIC_DONE | |
irq = = MP_BLUETOOTH_IRQ_GATTC_DESCRIPTOR_DONE ) {
mp_bluetooth_gattc_on_discover_complete ( irq , conn_handle , status ) ;
}
2020-02-19 22:44:13 -05:00
} else if ( event_type = = GATT_EVENT_SERVICE_QUERY_RESULT ) {
DEBUG_EVENT_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 " ) ;
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 " ) ;
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 " ) ;
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 ) ;
const uint8_t * data = gatt_event_characteristic_value_query_result_get_value ( packet ) ;
2020-04-07 00:58:50 -04:00
mp_uint_t atomic_state ;
len = mp_bluetooth_gattc_on_data_available_start ( MP_BLUETOOTH_IRQ_GATTC_READ_RESULT , conn_handle , value_handle , len , & atomic_state ) ;
2020-02-19 22:44:13 -05:00
mp_bluetooth_gattc_on_data_available_chunk ( data , len ) ;
2020-04-07 00:58:50 -04:00
mp_bluetooth_gattc_on_data_available_end ( atomic_state ) ;
2020-03-05 20:37:37 -05:00
} else if ( event_type = = GATT_EVENT_NOTIFICATION ) {
DEBUG_EVENT_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 ) ;
const uint8_t * data = gatt_event_notification_get_value ( packet ) ;
2020-04-07 00:58:50 -04:00
mp_uint_t atomic_state ;
len = mp_bluetooth_gattc_on_data_available_start ( MP_BLUETOOTH_IRQ_GATTC_NOTIFY , conn_handle , value_handle , len , & atomic_state ) ;
2020-03-05 20:37:37 -05:00
mp_bluetooth_gattc_on_data_available_chunk ( data , len ) ;
2020-04-07 00:58:50 -04:00
mp_bluetooth_gattc_on_data_available_end ( atomic_state ) ;
2020-03-05 20:37:37 -05:00
} else if ( event_type = = GATT_EVENT_INDICATION ) {
DEBUG_EVENT_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 ) ;
const uint8_t * data = gatt_event_indication_get_value ( packet ) ;
2020-04-07 00:58:50 -04:00
mp_uint_t atomic_state ;
len = mp_bluetooth_gattc_on_data_available_start ( MP_BLUETOOTH_IRQ_GATTC_INDICATE , conn_handle , value_handle , len , & atomic_state ) ;
2020-03-05 20:37:37 -05:00
mp_bluetooth_gattc_on_data_available_chunk ( data , len ) ;
2020-04-07 00:58:50 -04:00
mp_bluetooth_gattc_on_data_available_end ( atomic_state ) ;
2020-06-16 07:53:22 -04:00
} 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 ) ;
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=%lu \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.
}
2020-05-11 07:46:56 -04:00
# endif // MICROPY_PY_BLUETOOTH_ENABLE_CENTRAL_MODE
2020-02-19 22:44:13 -05:00
} else {
DEBUG_EVENT_printf ( " --> hci event type: unknown (0x%02x) \n " , event_type ) ;
2020-02-19 22:43:28 -05:00
}
}
2020-05-11 07:46:56 -04:00
// Because the packet handler callbacks don't support an argument, we use a specific
// handler when we need to provide additional state to the handler (in the "irq" parameter).
// This is the generic handler for when you don't need extra state.
STATIC void btstack_packet_handler_generic ( uint8_t packet_type , uint16_t channel , uint8_t * packet , uint16_t size ) {
( void ) channel ;
( void ) size ;
btstack_packet_handler ( packet_type , packet , 0 ) ;
}
2020-06-16 07:53:22 -04:00
STATIC btstack_packet_callback_registration_t hci_event_callback_registration = {
. callback = & btstack_packet_handler_generic
} ;
2020-02-19 22:44:13 -05:00
# if MICROPY_PY_BLUETOOTH_ENABLE_CENTRAL_MODE
2020-05-11 07:46:56 -04:00
// For when the handler is being used for service discovery.
STATIC void btstack_packet_handler_discover_services ( uint8_t packet_type , uint16_t channel , uint8_t * packet , uint16_t size ) {
2020-04-16 20:00:36 -04:00
( void ) channel ;
( void ) size ;
2020-05-11 07:46:56 -04:00
btstack_packet_handler ( packet_type , packet , MP_BLUETOOTH_IRQ_GATTC_SERVICE_DONE ) ;
}
2020-02-19 22:44:13 -05:00
2020-05-11 07:46:56 -04:00
// For when the handler is being used for characteristic discovery.
STATIC void btstack_packet_handler_discover_characteristics ( uint8_t packet_type , uint16_t channel , uint8_t * packet , uint16_t size ) {
( void ) channel ;
( void ) size ;
btstack_packet_handler ( packet_type , packet , MP_BLUETOOTH_IRQ_GATTC_CHARACTERISTIC_DONE ) ;
2020-02-19 22:43:28 -05:00
}
2020-05-11 07:46:56 -04:00
// For when the handler is being used for descriptor discovery.
STATIC void btstack_packet_handler_discover_descriptors ( uint8_t packet_type , uint16_t channel , uint8_t * packet , uint16_t size ) {
( void ) channel ;
( void ) size ;
btstack_packet_handler ( packet_type , packet , MP_BLUETOOTH_IRQ_GATTC_DESCRIPTOR_DONE ) ;
}
2020-06-02 00:22:47 -04:00
// For when the handler is being used for a read query.
STATIC void btstack_packet_handler_read ( uint8_t packet_type , uint16_t channel , uint8_t * packet , uint16_t size ) {
( void ) channel ;
( void ) size ;
btstack_packet_handler ( packet_type , packet , MP_BLUETOOTH_IRQ_GATTC_READ_DONE ) ;
}
2020-05-11 07:46:56 -04:00
// For when the handler is being used for write-with-response.
STATIC void btstack_packet_handler_write_with_response ( uint8_t packet_type , uint16_t channel , uint8_t * packet , uint16_t size ) {
( void ) channel ;
( void ) size ;
btstack_packet_handler ( packet_type , packet , MP_BLUETOOTH_IRQ_GATTC_WRITE_DONE ) ;
}
# endif // MICROPY_PY_BLUETOOTH_ENABLE_CENTRAL_MODE
2020-02-19 22:43:28 -05:00
2020-04-07 01:01:26 -04:00
STATIC btstack_timer_source_t btstack_init_deinit_timeout ;
STATIC void btstack_init_deinit_timeout_handler ( btstack_timer_source_t * ds ) {
( void ) ds ;
// Stop waiting for initialisation.
// This signals both the loops in mp_bluetooth_init and mp_bluetooth_deinit,
// as well as ports that run a polling loop.
mp_bluetooth_btstack_state = MP_BLUETOOTH_BTSTACK_STATE_TIMEOUT ;
}
2020-02-19 22:40:55 -05:00
int mp_bluetooth_init ( void ) {
2020-02-19 22:43:52 -05:00
DEBUG_EVENT_printf ( " mp_bluetooth_init \n " ) ;
2020-04-07 01:01:26 -04:00
if ( mp_bluetooth_btstack_state = = MP_BLUETOOTH_BTSTACK_STATE_ACTIVE ) {
return 0 ;
}
// Clean up if necessary.
mp_bluetooth_deinit ( ) ;
2020-02-19 22:43:01 -05:00
btstack_memory_init ( ) ;
MP_STATE_PORT ( bluetooth_btstack_root_pointers ) = m_new0 ( mp_bluetooth_btstack_root_pointers_t , 1 ) ;
2020-02-19 22:43:52 -05:00
mp_bluetooth_gatts_db_create ( & MP_STATE_PORT ( bluetooth_btstack_root_pointers ) - > gatts_db ) ;
2020-02-19 22:43:01 -05:00
2020-05-07 23:54:10 -04:00
// Set the default GAP device name.
const char * gap_name = MICROPY_PY_BLUETOOTH_DEFAULT_GAP_NAME ;
size_t gap_len = strlen ( gap_name ) ;
mp_bluetooth_gatts_db_create_entry ( MP_STATE_PORT ( bluetooth_btstack_root_pointers ) - > gatts_db , BTSTACK_GAP_DEVICE_NAME_HANDLE , gap_len ) ;
mp_bluetooth_gap_set_device_name ( ( const uint8_t * ) gap_name , gap_len ) ;
2020-02-19 22:43:01 -05:00
mp_bluetooth_btstack_port_init ( ) ;
mp_bluetooth_btstack_state = MP_BLUETOOTH_BTSTACK_STATE_STARTING ;
l2cap_init ( ) ;
le_device_db_init ( ) ;
sm_init ( ) ;
// Set blank ER/IR keys to suppress BTstack warning.
// TODO handle this correctly.
sm_key_t dummy_key ;
memset ( dummy_key , 0 , sizeof ( dummy_key ) ) ;
sm_set_er ( dummy_key ) ;
sm_set_ir ( dummy_key ) ;
2020-02-19 22:44:13 -05:00
# if MICROPY_PY_BLUETOOTH_ENABLE_CENTRAL_MODE
gatt_client_init ( ) ;
# endif
2020-02-19 22:43:28 -05:00
// Register for HCI events.
hci_add_event_handler ( & hci_event_callback_registration ) ;
2020-04-07 01:01:26 -04:00
// Set a timeout for HCI initialisation.
btstack_run_loop_set_timer ( & btstack_init_deinit_timeout , BTSTACK_INIT_DEINIT_TIMEOUT_MS ) ;
btstack_run_loop_set_timer_handler ( & btstack_init_deinit_timeout , btstack_init_deinit_timeout_handler ) ;
btstack_run_loop_add_timer ( & btstack_init_deinit_timeout ) ;
// 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 ) {
MICROPY_EVENT_POLL_HOOK
}
btstack_run_loop_remove_timer ( & btstack_init_deinit_timeout ) ;
// Check for timeout.
if ( mp_bluetooth_btstack_state ! = MP_BLUETOOTH_BTSTACK_STATE_ACTIVE ) {
// Required to stop the polling loop.
mp_bluetooth_btstack_state = MP_BLUETOOTH_BTSTACK_STATE_OFF ;
// Attempt a shutdown (may not do anything).
mp_bluetooth_btstack_port_deinit ( ) ;
// Clean up.
MP_STATE_PORT ( bluetooth_btstack_root_pointers ) = NULL ;
return MP_ETIMEDOUT ;
}
2020-03-05 20:37:37 -05:00
# if MICROPY_PY_BLUETOOTH_ENABLE_CENTRAL_MODE
// Enable GATT_EVENT_NOTIFICATION/GATT_EVENT_INDICATION for all connections and handles.
2020-05-11 07:46:56 -04:00
gatt_client_listen_for_characteristic_value_updates ( & MP_STATE_PORT ( bluetooth_btstack_root_pointers ) - > notification , & btstack_packet_handler_generic , GATT_CLIENT_ANY_CONNECTION , NULL ) ;
2020-03-05 20:37:37 -05:00
# endif
2020-02-19 22:40:55 -05:00
return 0 ;
}
void mp_bluetooth_deinit ( void ) {
2020-02-19 22:43:52 -05:00
DEBUG_EVENT_printf ( " mp_bluetooth_deinit \n " ) ;
2020-03-05 20:37:37 -05:00
2020-04-07 01:01:26 -04:00
// Nothing to do if not initialised.
2020-03-05 20:37:37 -05:00
if ( ! MP_STATE_PORT ( bluetooth_btstack_root_pointers ) ) {
return ;
}
2020-04-07 01:01:26 -04:00
mp_bluetooth_gap_advertise_stop ( ) ;
2020-03-05 20:37:37 -05:00
# if MICROPY_PY_BLUETOOTH_ENABLE_CENTRAL_MODE
// Remove our registration for notify/indicate.
gatt_client_stop_listening_for_characteristic_value_updates ( & MP_STATE_PORT ( bluetooth_btstack_root_pointers ) - > notification ) ;
# endif
2020-04-07 01:01:26 -04:00
// Set a timer that will forcibly set the state to TIMEOUT, which will stop the loop below.
btstack_run_loop_set_timer ( & btstack_init_deinit_timeout , BTSTACK_INIT_DEINIT_TIMEOUT_MS ) ;
btstack_run_loop_add_timer ( & btstack_init_deinit_timeout ) ;
// This should result in a clean shutdown, which will set the state to OFF.
// On Unix this is blocking (it joins on the poll thread), on other ports the loop below will wait unil
// either timeout or clean shutdown.
2020-02-19 22:43:01 -05:00
mp_bluetooth_btstack_port_deinit ( ) ;
2020-04-07 01:01:26 -04:00
while ( mp_bluetooth_btstack_state = = MP_BLUETOOTH_BTSTACK_STATE_ACTIVE ) {
MICROPY_EVENT_POLL_HOOK
}
btstack_run_loop_remove_timer ( & btstack_init_deinit_timeout ) ;
2020-02-19 22:43:01 -05:00
2020-04-07 01:01:26 -04:00
mp_bluetooth_btstack_state = MP_BLUETOOTH_BTSTACK_STATE_OFF ;
2020-02-19 22:43:01 -05:00
MP_STATE_PORT ( bluetooth_btstack_root_pointers ) = NULL ;
2020-02-19 22:40:55 -05:00
}
2020-02-23 23:45:57 -05:00
bool mp_bluetooth_is_active ( void ) {
2020-02-19 22:43:01 -05:00
return mp_bluetooth_btstack_state = = MP_BLUETOOTH_BTSTACK_STATE_ACTIVE ;
2020-02-19 22:40:55 -05:00
}
void mp_bluetooth_get_device_addr ( uint8_t * addr ) {
mp_hal_get_mac ( MP_HAL_MAC_BDADDR , addr ) ;
}
2020-05-07 23:54:10 -04:00
size_t mp_bluetooth_gap_get_device_name ( const uint8_t * * buf ) {
uint8_t * value = NULL ;
size_t value_len = 0 ;
mp_bluetooth_gatts_db_read ( MP_STATE_PORT ( bluetooth_btstack_root_pointers ) - > gatts_db , BTSTACK_GAP_DEVICE_NAME_HANDLE , & value , & value_len ) ;
* buf = value ;
return value_len ;
}
int mp_bluetooth_gap_set_device_name ( const uint8_t * buf , size_t len ) {
2020-06-16 07:53:22 -04:00
return mp_bluetooth_gatts_db_write ( MP_STATE_PORT ( bluetooth_btstack_root_pointers ) - > gatts_db , BTSTACK_GAP_DEVICE_NAME_HANDLE , buf , len ) ;
2020-05-07 23:54:10 -04:00
}
2020-02-19 22:40:55 -05:00
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 ) {
2020-02-19 22:43:52 -05:00
DEBUG_EVENT_printf ( " mp_bluetooth_gap_advertise_start \n " ) ;
2020-02-19 22:43:01 -05:00
uint16_t adv_int_min = interval_us / 625 ;
uint16_t adv_int_max = interval_us / 625 ;
uint8_t adv_type = connectable ? 0 : 2 ;
bd_addr_t null_addr = { 0 } ;
uint8_t direct_address_type = 0 ;
uint8_t channel_map = 0x07 ; // Use all three broadcast channels.
uint8_t filter_policy = 0x00 ; // None.
gap_advertisements_set_params ( adv_int_min , adv_int_max , adv_type , direct_address_type , null_addr , channel_map , filter_policy ) ;
// Copy the adv_data and sr_data into a persistent buffer (which is findable via the btstack root pointers).
size_t total_bytes = adv_data_len + sr_data_len ;
if ( total_bytes > MP_STATE_PORT ( bluetooth_btstack_root_pointers ) - > adv_data_alloc ) {
// Resize if necessary.
MP_STATE_PORT ( bluetooth_btstack_root_pointers ) - > adv_data = m_new ( uint8_t , total_bytes ) ;
MP_STATE_PORT ( bluetooth_btstack_root_pointers ) - > adv_data_alloc = total_bytes ;
}
uint8_t * data = MP_STATE_PORT ( bluetooth_btstack_root_pointers ) - > adv_data ;
if ( adv_data ) {
memcpy ( data , ( uint8_t * ) adv_data , adv_data_len ) ;
gap_advertisements_set_data ( adv_data_len , data ) ;
data + = adv_data_len ;
}
if ( sr_data ) {
memcpy ( data , ( uint8_t * ) sr_data , sr_data_len ) ;
gap_scan_response_set_data ( sr_data_len , data ) ;
}
gap_advertisements_enable ( true ) ;
2020-02-19 22:40:55 -05:00
return 0 ;
}
void mp_bluetooth_gap_advertise_stop ( void ) {
2020-02-19 22:43:52 -05:00
DEBUG_EVENT_printf ( " mp_bluetooth_gap_advertise_stop \n " ) ;
2020-02-19 22:43:01 -05:00
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 ;
2020-02-19 22:40:55 -05:00
}
int mp_bluetooth_gatts_register_service_begin ( bool append ) {
2020-02-19 22:43:52 -05:00
DEBUG_EVENT_printf ( " mp_bluetooth_gatts_register_service_begin \n " ) ;
2020-02-19 22:43:28 -05:00
if ( ! append ) {
// This will reset the DB.
// Becase the DB is statically allocated, there's no problem with just re-initing it.
// Note this would be a memory leak if we enabled HAVE_MALLOC (there's no API to free the existing db).
att_db_util_init ( ) ;
att_db_util_add_service_uuid16 ( GAP_SERVICE_UUID ) ;
2020-05-07 23:54:10 -04:00
uint16_t handle = att_db_util_add_characteristic_uuid16 ( GAP_DEVICE_NAME_UUID , ATT_PROPERTY_READ | ATT_PROPERTY_DYNAMIC , ATT_SECURITY_NONE , ATT_SECURITY_NONE , NULL , 0 ) ;
assert ( handle = = BTSTACK_GAP_DEVICE_NAME_HANDLE ) ;
( void ) handle ;
2020-02-19 22:43:28 -05:00
att_db_util_add_service_uuid16 ( 0x1801 ) ;
att_db_util_add_characteristic_uuid16 ( 0x2a05 , ATT_PROPERTY_READ , ATT_SECURITY_NONE , ATT_SECURITY_NONE , NULL , 0 ) ;
}
return 0 ;
}
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 ) {
2020-04-16 20:00:36 -04:00
( void ) connection_handle ;
2020-02-19 22:43:52 -05:00
DEBUG_EVENT_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 " ) ;
return 0 ; // TODO: Find status code for not-found.
}
return att_read_callback_handle_blob ( entry - > data , entry - > data_len , offset , buffer , buffer_size ) ;
2020-02-19 22:43:28 -05:00
}
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 ) {
2020-04-16 20:00:36 -04:00
( void ) offset ;
( void ) transaction_mode ;
2020-02-19 22:43:52 -05:00
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 ) ;
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 " ) ;
return 0 ; // TODO: Find status code for not-found.
}
// TODO: Use `offset` arg.
size_t append_offset = 0 ;
if ( entry - > append ) {
append_offset = entry - > data_len ;
}
entry - > data_len = MIN ( entry - > data_alloc , buffer_size + append_offset ) ;
memcpy ( entry - > data + append_offset , buffer , entry - > data_len - append_offset ) ;
mp_bluetooth_gatts_on_write ( connection_handle , att_handle ) ;
2020-02-19 22:40:55 -05:00
return 0 ;
}
2020-02-19 22:43:28 -05:00
STATIC inline uint16_t get_uuid16 ( const mp_obj_bluetooth_uuid_t * uuid ) {
return ( uuid - > data [ 1 ] < < 8 ) | uuid - > data [ 0 ] ;
}
2020-02-19 22:40:55 -05:00
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 ) {
2020-02-19 22:43:52 -05:00
DEBUG_EVENT_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).
// TODO: btstack's att_db_util_add_* methods have no bounds checking or validation.
// Need some way to prevent additional services being added if we're out of space in the static buffer.
2020-02-19 22:43:28 -05:00
if ( service_uuid - > type = = MP_BLUETOOTH_UUID_TYPE_16 ) {
2020-02-19 22:43:52 -05:00
att_db_util_add_service_uuid16 ( get_uuid16 ( service_uuid ) ) ;
2020-02-19 22:43:28 -05:00
} else if ( service_uuid - > type = = MP_BLUETOOTH_UUID_TYPE_128 ) {
2020-02-19 22:43:52 -05:00
uint8_t buffer [ 16 ] ;
reverse_128 ( service_uuid - > data , buffer ) ;
att_db_util_add_service_uuid128 ( buffer ) ;
2020-02-19 22:43:28 -05:00
} else {
return MP_EINVAL ;
}
size_t handle_index = 0 ;
size_t descriptor_index = 0 ;
2020-02-19 22:43:52 -05:00
static uint8_t cccb_buf [ 2 ] = { 0 } ;
2020-02-19 22:43:28 -05:00
for ( size_t i = 0 ; i < num_characteristics ; + + i ) {
uint16_t props = characteristic_flags [ i ] | ATT_PROPERTY_DYNAMIC ;
uint16_t read_permission = ATT_SECURITY_NONE ;
uint16_t write_permission = ATT_SECURITY_NONE ;
if ( characteristic_uuids [ i ] - > type = = MP_BLUETOOTH_UUID_TYPE_16 ) {
handles [ handle_index ] = att_db_util_add_characteristic_uuid16 ( get_uuid16 ( characteristic_uuids [ i ] ) , props , read_permission , write_permission , NULL , 0 ) ;
} else if ( characteristic_uuids [ i ] - > type = = MP_BLUETOOTH_UUID_TYPE_128 ) {
2020-02-19 22:43:52 -05:00
uint8_t buffer [ 16 ] ;
reverse_128 ( characteristic_uuids [ i ] - > data , buffer ) ;
handles [ handle_index ] = att_db_util_add_characteristic_uuid128 ( buffer , props , read_permission , write_permission , NULL , 0 ) ;
2020-02-19 22:43:28 -05:00
} else {
return MP_EINVAL ;
}
2020-02-19 22:43:52 -05:00
mp_bluetooth_gatts_db_create_entry ( MP_STATE_PORT ( bluetooth_btstack_root_pointers ) - > gatts_db , handles [ handle_index ] , MP_BLUETOOTH_DEFAULT_ATTR_LEN ) ;
// If a NOTIFY or INDICATE characteristic is added, then we need to manage a value for the CCCB.
if ( props & ( ATT_PROPERTY_NOTIFY | ATT_PROPERTY_INDICATE ) ) {
// btstack creates the CCCB as the next handle.
mp_bluetooth_gatts_db_create_entry ( MP_STATE_PORT ( bluetooth_btstack_root_pointers ) - > gatts_db , handles [ handle_index ] + 1 , MP_BLUETOOTH_CCCB_LEN ) ;
2020-06-16 07:53:22 -04:00
int ret = mp_bluetooth_gatts_db_write ( MP_STATE_PORT ( bluetooth_btstack_root_pointers ) - > gatts_db , handles [ handle_index ] + 1 , cccb_buf , sizeof ( cccb_buf ) ) ;
if ( ret ) {
return ret ;
}
2020-02-19 22:43:52 -05:00
}
DEBUG_EVENT_printf ( " Registered char with handle %u \n " , handles [ handle_index ] ) ;
2020-02-19 22:43:28 -05:00
+ + handle_index ;
for ( size_t j = 0 ; j < num_descriptors [ i ] ; + + j ) {
2020-04-16 20:00:36 -04:00
props = descriptor_flags [ descriptor_index ] | ATT_PROPERTY_DYNAMIC ;
read_permission = ATT_SECURITY_NONE ;
write_permission = ATT_SECURITY_NONE ;
2020-02-19 22:43:28 -05:00
if ( descriptor_uuids [ descriptor_index ] - > type = = MP_BLUETOOTH_UUID_TYPE_16 ) {
handles [ handle_index ] = att_db_util_add_descriptor_uuid16 ( get_uuid16 ( descriptor_uuids [ descriptor_index ] ) , props , read_permission , write_permission , NULL , 0 ) ;
} else if ( descriptor_uuids [ descriptor_index ] - > type = = MP_BLUETOOTH_UUID_TYPE_128 ) {
2020-02-19 22:43:52 -05:00
uint8_t buffer [ 16 ] ;
reverse_128 ( descriptor_uuids [ descriptor_index ] - > data , buffer ) ;
handles [ handle_index ] = att_db_util_add_descriptor_uuid128 ( buffer , props , read_permission , write_permission , NULL , 0 ) ;
2020-02-19 22:43:28 -05:00
} else {
return MP_EINVAL ;
}
2020-02-19 22:43:52 -05:00
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 ] ) ;
2020-02-19 22:43:28 -05:00
+ + descriptor_index ;
+ + handle_index ;
}
}
2020-02-19 22:40:55 -05:00
return 0 ;
}
int mp_bluetooth_gatts_register_service_end ( void ) {
2020-02-19 22:43:52 -05:00
DEBUG_EVENT_printf ( " mp_bluetooth_gatts_register_service_end \n " ) ;
2020-02-19 22:43:28 -05:00
att_server_init ( att_db_util_get_address ( ) , & att_read_callback , & att_write_callback ) ;
2020-02-19 22:40:55 -05:00
return 0 ;
}
int mp_bluetooth_gatts_read ( uint16_t value_handle , uint8_t * * value , size_t * value_len ) {
2020-02-19 22:43:52 -05:00
DEBUG_EVENT_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 ) ;
2020-02-19 22:40:55 -05:00
}
int mp_bluetooth_gatts_write ( uint16_t value_handle , const uint8_t * value , size_t value_len ) {
2020-02-19 22:43:52 -05:00
DEBUG_EVENT_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 ) ;
2020-02-19 22:40:55 -05:00
}
int mp_bluetooth_gatts_notify ( uint16_t conn_handle , uint16_t value_handle ) {
2020-02-19 22:43:52 -05:00
DEBUG_EVENT_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 ;
mp_bluetooth_gatts_db_read ( MP_STATE_PORT ( bluetooth_btstack_root_pointers ) - > gatts_db , value_handle , & data , & len ) ;
2020-06-16 07:53:22 -04:00
return mp_bluetooth_gatts_notify_send ( conn_handle , value_handle , data , len ) ;
2020-02-19 22:40:55 -05:00
}
2020-06-16 07:53:22 -04:00
int mp_bluetooth_gatts_notify_send ( uint16_t conn_handle , uint16_t value_handle , const uint8_t * value , size_t value_len ) {
2020-02-19 22:43:52 -05:00
DEBUG_EVENT_printf ( " mp_bluetooth_gatts_notify_send \n " ) ;
2020-06-16 07:53:22 -04:00
// Attempt to send immediately. If it succeeds, btstack will copy the buffer.
MICROPY_PY_BLUETOOTH_ENTER
int err = att_server_notify ( conn_handle , value_handle , value , value_len ) ;
MICROPY_PY_BLUETOOTH_EXIT
if ( err = = BTSTACK_ACL_BUFFERS_FULL ) {
DEBUG_EVENT_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 ) ;
err = att_server_request_to_send_notification ( & pending_op - > context_registration , conn_handle ) ;
if ( err ! = ERROR_CODE_SUCCESS ) {
// Failure. Unref and free the pending operation.
btstack_remove_pending_operation ( pending_op , true /* del */ ) ;
}
return 0 ;
} else {
return btstack_error_to_errno ( err ) ;
}
2020-02-19 22:40:55 -05:00
}
int mp_bluetooth_gatts_indicate ( uint16_t conn_handle , uint16_t value_handle ) {
2020-02-19 22:43:52 -05:00
DEBUG_EVENT_printf ( " mp_bluetooth_gatts_indicate \n " ) ;
2020-06-16 07:53:22 -04:00
uint8_t * data = NULL ;
size_t len = 0 ;
mp_bluetooth_gatts_db_read ( MP_STATE_PORT ( bluetooth_btstack_root_pointers ) - > gatts_db , value_handle , & data , & len ) ;
2020-07-17 01:18:32 -04:00
// TODO: Handle ATT_EVENT_HANDLE_VALUE_INDICATION_COMPLETE to generate acknowledgment event.
2020-06-16 07:53:22 -04:00
// Attempt to send immediately, will copy buffer.
MICROPY_PY_BLUETOOTH_ENTER
int err = att_server_indicate ( conn_handle , value_handle , data , len ) ;
MICROPY_PY_BLUETOOTH_EXIT
if ( err = = BTSTACK_ACL_BUFFERS_FULL ) {
DEBUG_EVENT_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 ) ;
err = att_server_request_to_send_indication ( & pending_op - > context_registration , conn_handle ) ;
if ( err ! = ERROR_CODE_SUCCESS ) {
// Failure. Unref and free the pending operation.
btstack_remove_pending_operation ( pending_op , true /* del */ ) ;
}
return 0 ;
} else {
return btstack_error_to_errno ( err ) ;
}
2020-02-19 22:40:55 -05:00
}
int mp_bluetooth_gatts_set_buffer ( uint16_t value_handle , size_t len , bool append ) {
2020-02-19 22:43:52 -05:00
DEBUG_EVENT_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 ) ;
2020-02-19 22:40:55 -05:00
}
int mp_bluetooth_gap_disconnect ( uint16_t conn_handle ) {
2020-02-19 22:43:52 -05:00
DEBUG_EVENT_printf ( " mp_bluetooth_gap_disconnect \n " ) ;
gap_disconnect ( conn_handle ) ;
2020-02-19 22:40:55 -05:00
return 0 ;
}
# if MICROPY_PY_BLUETOOTH_ENABLE_CENTRAL_MODE
2020-02-19 22:44:13 -05:00
STATIC btstack_timer_source_t scan_duration_timeout ;
STATIC void hci_initialization_timeout_handler ( btstack_timer_source_t * ds ) {
2020-04-16 20:00:36 -04:00
( void ) ds ;
2020-02-19 22:44:13 -05:00
mp_bluetooth_gap_scan_stop ( ) ;
}
2020-02-19 22:40:55 -05:00
int mp_bluetooth_gap_scan_start ( int32_t duration_ms , int32_t interval_us , int32_t window_us ) {
2020-02-19 22:43:52 -05:00
DEBUG_EVENT_printf ( " mp_bluetooth_gap_scan_start \n " ) ;
2020-02-19 22:44:13 -05:00
btstack_run_loop_set_timer ( & scan_duration_timeout , duration_ms ) ;
btstack_run_loop_set_timer_handler ( & scan_duration_timeout , hci_initialization_timeout_handler ) ;
btstack_run_loop_add_timer ( & scan_duration_timeout ) ;
// 0 = passive scan (we don't handle scan response).
gap_set_scan_parameters ( 0 , interval_us / 625 , window_us / 625 ) ;
gap_start_scan ( ) ;
2020-02-19 22:40:55 -05:00
return 0 ;
}
int mp_bluetooth_gap_scan_stop ( void ) {
2020-02-19 22:43:52 -05:00
DEBUG_EVENT_printf ( " mp_bluetooth_gap_scan_stop \n " ) ;
2020-02-19 22:44:13 -05:00
btstack_run_loop_remove_timer ( & scan_duration_timeout ) ;
gap_stop_scan ( ) ;
mp_bluetooth_gap_on_scan_complete ( ) ;
2020-02-19 22:40:55 -05:00
return 0 ;
}
int mp_bluetooth_gap_peripheral_connect ( uint8_t addr_type , const uint8_t * addr , int32_t duration_ms ) {
2020-02-19 22:43:52 -05:00
DEBUG_EVENT_printf ( " mp_bluetooth_gap_peripheral_connect \n " ) ;
2020-02-19 22:44:13 -05:00
uint16_t conn_scan_interval = 60000 / 625 ;
uint16_t conn_scan_window = 30000 / 625 ;
uint16_t conn_interval_min = 10000 / 1250 ;
uint16_t conn_interval_max = 30000 / 1250 ;
uint16_t conn_latency = 4 ;
uint16_t supervision_timeout = duration_ms / 10 ; // default = 720
uint16_t min_ce_length = 10000 / 625 ;
uint16_t max_ce_length = 30000 / 625 ;
gap_set_connection_parameters ( conn_scan_interval , conn_scan_window , conn_interval_min , conn_interval_max , conn_latency , supervision_timeout , min_ce_length , max_ce_length ) ;
bd_addr_t btstack_addr ;
memcpy ( btstack_addr , addr , BD_ADDR_LEN ) ;
return btstack_error_to_errno ( gap_connect ( btstack_addr , addr_type ) ) ;
2020-02-19 22:40:55 -05:00
}
2020-05-12 09:55:49 -04:00
int mp_bluetooth_gattc_discover_primary_services ( uint16_t conn_handle , const mp_obj_bluetooth_uuid_t * uuid ) {
2020-02-19 22:43:52 -05:00
DEBUG_EVENT_printf ( " mp_bluetooth_gattc_discover_primary_services \n " ) ;
2020-05-12 09:55:49 -04:00
uint8_t err ;
if ( uuid ) {
if ( uuid - > type = = MP_BLUETOOTH_UUID_TYPE_16 ) {
err = gatt_client_discover_primary_services_by_uuid16 ( & btstack_packet_handler_discover_services , conn_handle , get_uuid16 ( uuid ) ) ;
} else if ( uuid - > type = = MP_BLUETOOTH_UUID_TYPE_128 ) {
uint8_t buffer [ 16 ] ;
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 " ) ;
return MP_EINVAL ;
}
} else {
err = gatt_client_discover_primary_services ( & btstack_packet_handler_discover_services , conn_handle ) ;
}
return btstack_error_to_errno ( err ) ;
2020-02-19 22:40:55 -05:00
}
2020-05-12 09:55:49 -04:00
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 ) {
2020-02-19 22:43:52 -05:00
DEBUG_EVENT_printf ( " mp_bluetooth_gattc_discover_characteristics \n " ) ;
2020-02-19 22:44:13 -05:00
gatt_client_service_t service = {
// Only start/end handles needed for gatt_client_discover_characteristics_for_service.
. start_group_handle = start_handle ,
. end_group_handle = end_handle ,
. uuid16 = 0 ,
. uuid128 = { 0 } ,
} ;
2020-05-12 09:55:49 -04:00
uint8_t err ;
if ( uuid ) {
if ( uuid - > type = = MP_BLUETOOTH_UUID_TYPE_16 ) {
err = gatt_client_discover_characteristics_for_service_by_uuid16 ( & btstack_packet_handler_discover_characteristics , conn_handle , & service , get_uuid16 ( uuid ) ) ;
} else if ( uuid - > type = = MP_BLUETOOTH_UUID_TYPE_128 ) {
uint8_t buffer [ 16 ] ;
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 " ) ;
return MP_EINVAL ;
}
} else {
err = gatt_client_discover_characteristics_for_service ( & btstack_packet_handler_discover_characteristics , conn_handle , & service ) ;
}
return btstack_error_to_errno ( err ) ;
2020-02-19 22:40:55 -05:00
}
int mp_bluetooth_gattc_discover_descriptors ( uint16_t conn_handle , uint16_t start_handle , uint16_t end_handle ) {
2020-02-19 22:43:52 -05:00
DEBUG_EVENT_printf ( " mp_bluetooth_gattc_discover_descriptors \n " ) ;
2020-02-19 22:44:13 -05:00
gatt_client_characteristic_t characteristic = {
// Only start/end handles needed for gatt_client_discover_characteristic_descriptors.
. start_handle = start_handle ,
. value_handle = 0 ,
. end_handle = end_handle ,
. properties = 0 ,
. uuid16 = 0 ,
. uuid128 = { 0 } ,
} ;
2020-05-11 07:46:56 -04:00
return btstack_error_to_errno ( gatt_client_discover_characteristic_descriptors ( & btstack_packet_handler_discover_descriptors , conn_handle , & characteristic ) ) ;
2020-02-19 22:40:55 -05:00
}
int mp_bluetooth_gattc_read ( uint16_t conn_handle , uint16_t value_handle ) {
2020-02-19 22:43:52 -05:00
DEBUG_EVENT_printf ( " mp_bluetooth_gattc_read \n " ) ;
2020-06-02 00:22:47 -04:00
return btstack_error_to_errno ( gatt_client_read_value_of_characteristic_using_value_handle ( & btstack_packet_handler_read , conn_handle , value_handle ) ) ;
2020-02-19 22:40:55 -05:00
}
int mp_bluetooth_gattc_write ( uint16_t conn_handle , uint16_t value_handle , const uint8_t * value , size_t * value_len , unsigned int mode ) {
2020-02-19 22:43:52 -05:00
DEBUG_EVENT_printf ( " mp_bluetooth_gattc_write \n " ) ;
2020-02-19 22:44:13 -05:00
2020-06-16 07:53:22 -04:00
// We should be distinguishing between gatt_client_write_value_of_characteristic vs
// gatt_client_write_characteristic_descriptor_using_descriptor_handle.
// However both are implemented using send_gatt_write_attribute_value_request under the hood,
// and we get the exact same event to the packet handler.
// Same story for the "without response" version.
int err ;
mp_btstack_pending_op_t * pending_op = NULL ;
2020-02-19 22:44:13 -05:00
if ( mode = = MP_BLUETOOTH_WRITE_MODE_NO_RESPONSE ) {
2020-06-16 07:53:22 -04:00
// 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 " ) ;
// 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 ) ;
}
} else if ( mode = = MP_BLUETOOTH_WRITE_MODE_WITH_RESPONSE ) {
// Pending operation copies the value buffer and keeps a GC reference
// until the response comes back (there is always a response).
pending_op = btstack_enqueue_pending_operation ( MP_BLUETOOTH_BTSTACK_PENDING_WRITE , conn_handle , value_handle , value , * value_len ) ;
err = gatt_client_write_value_of_characteristic ( & btstack_packet_handler_write_with_response , conn_handle , value_handle , pending_op - > len , pending_op - > buf ) ;
} else {
return MP_EINVAL ;
2020-02-19 22:44:13 -05:00
}
2020-06-16 07:53:22 -04:00
if ( pending_op & & err ! = ERROR_CODE_SUCCESS ) {
// Failure. Unref and free the pending operation.
btstack_remove_pending_operation ( pending_op , true /* del */ ) ;
2020-02-19 22:44:13 -05:00
}
2020-06-16 07:53:22 -04:00
return btstack_error_to_errno ( err ) ;
2020-02-19 22:40:55 -05:00
}
2020-03-05 20:37:37 -05:00
# endif // MICROPY_PY_BLUETOOTH_ENABLE_CENTRAL_MODE
2020-02-19 22:40:55 -05:00
# endif // MICROPY_PY_BLUETOOTH && MICROPY_BLUETOOTH_BTSTACK