nordic_spp_service: use events instead of callback

This commit is contained in:
Matthias Ringwald 2021-03-21 21:38:36 +01:00
parent f8aace927f
commit 257f2b0068
4 changed files with 80 additions and 35 deletions

View File

@ -124,13 +124,28 @@ static void heartbeat_handler(struct btstack_timer_source *ts){
}
/* LISTING_END */
static void nordic_data_received(hci_con_handle_t tx_con_handle, const uint8_t * data, uint16_t size){
if (size == 0 && con_handle == HCI_CON_HANDLE_INVALID ){
con_handle = tx_con_handle;
printf("Connected with handle 0x%04x\n", con_handle);
} else {
printf("RECV: ");
printf_hexdump(data, size);
static void nordic_spp_packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
switch (packet_type){
case HCI_EVENT_PACKET:
if (hci_event_packet_get_type(packet) != HCI_EVENT_GATTSERVICE_META) break;
switch (hci_event_gattservice_meta_get_subevent_code(packet)){
case GATTSERVICE_SUBEVENT_SPP_SERVICE_CONNECTED:
con_handle = gattservice_subevent_spp_service_connected_get_con_handle(packet);
printf("Connected with handle 0x%04x\n", con_handle);
break;
case GATTSERVICE_SUBEVENT_SPP_SERVICE_DISCONNECTED:
con_handle = HCI_CON_HANDLE_INVALID;
break;
default:
break;
}
break;
case RFCOMM_DATA_PACKET:
printf("RECV: ");
printf_hexdump(packet, size);
break;
default:
break;
}
}
@ -142,7 +157,7 @@ static void nordic_data_received(hci_con_handle_t tx_con_handle, const uint8_t *
*/
/* LISTING_START(packetHandler): Packet Handler */
static void packet_handler (uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
static void hci_packet_handler (uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
UNUSED(channel);
UNUSED(size);
@ -163,7 +178,7 @@ int btstack_main(void);
int btstack_main(void)
{
// register for HCI events
hci_event_callback_registration.callback = &packet_handler;
hci_event_callback_registration.callback = &hci_packet_handler;
hci_add_event_handler(&hci_event_callback_registration);
l2cap_init();
@ -178,7 +193,7 @@ int btstack_main(void)
att_server_init(profile_data, NULL, NULL);
// setup Nordic SPP service
nordic_spp_service_server_init(&nordic_data_received);
nordic_spp_service_server_init(&nordic_spp_packet_handler);
// setup advertisements
uint16_t adv_int_min = 0x0030;

View File

@ -303,21 +303,40 @@ static void nordic_can_send(void * some_context){
}
/* LISTING_END */
static void nordic_data_received(hci_con_handle_t tx_con_handle, const uint8_t * data, uint16_t size){
nordic_spp_le_streamer_connection_t * context = connection_for_conn_handle(tx_con_handle);
if (!context) return;
if (size == 0 && context->le_notification_enabled == 0){
context->le_notification_enabled = 1;
test_reset(context);
context->send_request.callback = &nordic_can_send;
nordic_spp_service_server_request_can_send_now(&context->send_request, context->connection_handle);
} else {
printf("RECV: ");
printf_hexdump(data, size);
test_track_sent(context, size);
}
static void nordic_spp_packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
hci_con_handle_t con_handle;
nordic_spp_le_streamer_connection_t * context;
switch (packet_type){
case HCI_EVENT_PACKET:
if (hci_event_packet_get_type(packet) != HCI_EVENT_GATTSERVICE_META) break;
switch (hci_event_gattservice_meta_get_subevent_code(packet)){
case GATTSERVICE_SUBEVENT_SPP_SERVICE_CONNECTED:
con_handle = gattservice_subevent_spp_service_connected_get_con_handle(packet);
context = connection_for_conn_handle(con_handle);
if (!context) break;
context->le_notification_enabled = 1;
test_reset(context);
context->send_request.callback = &nordic_can_send;
nordic_spp_service_server_request_can_send_now(&context->send_request, context->connection_handle);
break;
case GATTSERVICE_SUBEVENT_SPP_SERVICE_DISCONNECTED:
con_handle = HCI_CON_HANDLE_INVALID;
context = connection_for_conn_handle(con_handle);
if (!context) break;
context->le_notification_enabled = 0;
break;
default:
break;
}
break;
case RFCOMM_DATA_PACKET:
printf("RECV: ");
printf_hexdump(packet, size);
test_track_sent(context, size);
break;
default:
break;
}
}
int btstack_main(void);
@ -338,7 +357,7 @@ int btstack_main(void){
att_server_init(profile_data, NULL, NULL);
// setup Nordic SPP service
nordic_spp_service_server_init(&nordic_data_received);
nordic_spp_service_server_init(&nordic_spp_packet_handler);
// register for ATT events
att_server_register_packet_handler(att_packet_handler);

View File

@ -58,13 +58,24 @@
//
static att_service_handler_t nordic_spp_service;
static void (*client_callback)(hci_con_handle_t con_handle, const uint8_t * data, uint16_t size);
static btstack_packet_handler_t client_packet_handler;
static uint16_t nordic_spp_rx_value_handle;
static uint16_t nordic_spp_tx_value_handle;
static uint16_t nordic_spp_tx_client_configuration_handle;
static uint16_t nordic_spp_tx_client_configuration_value;
static void nordic_spp_service_emit_state(hci_con_handle_t con_handle, bool enabled){
uint8_t event[5];
uint8_t pos = 0;
event[pos++] = HCI_EVENT_GATTSERVICE_META;
event[pos++] = sizeof(event) - 2;
event[pos++] = enabled ? GATTSERVICE_SUBEVENT_SPP_SERVICE_CONNECTED : GATTSERVICE_SUBEVENT_SPP_SERVICE_DISCONNECTED;
little_endian_store_16(event,pos, (uint16_t) con_handle);
pos += 2;
(*client_packet_handler)(HCI_EVENT_PACKET, 0, event, pos);
}
static uint16_t nordic_spp_service_read_callback(hci_con_handle_t con_handle, uint16_t attribute_handle, uint16_t offset, uint8_t * buffer, uint16_t buffer_size){
UNUSED(con_handle);
UNUSED(offset);
@ -85,13 +96,13 @@ static int nordic_spp_service_write_callback(hci_con_handle_t con_handle, uint16
UNUSED(buffer_size);
if (attribute_handle == nordic_spp_rx_value_handle){
client_callback(con_handle, &buffer[0], buffer_size);
(*client_packet_handler)(RFCOMM_DATA_PACKET, (uint16_t) con_handle, buffer, buffer_size);
}
if (attribute_handle == nordic_spp_tx_client_configuration_handle){
nordic_spp_tx_client_configuration_value = little_endian_read_16(buffer, 0);
if (nordic_spp_tx_client_configuration_value != 0){
client_callback(con_handle, NULL, 0);
}
bool enabled = (nordic_spp_tx_client_configuration_value != 0);
nordic_spp_service_emit_state(con_handle, enabled);
}
return 0;
@ -101,13 +112,13 @@ static int nordic_spp_service_write_callback(hci_con_handle_t con_handle, uint16
* @brief Init Nordic SPP Service Server with ATT DB
* @param callback for tx data from peer
*/
void nordic_spp_service_server_init(void (*callback)(hci_con_handle_t con_handle, const uint8_t * data, uint16_t size)){
void nordic_spp_service_server_init(btstack_packet_handler_t packet_handler){
static const uint8_t nordic_spp_profile_uuid128[] = { 0x6E, 0x40, 0x00, 0x01, 0xB5, 0xA3, 0xF3, 0x93, 0xE0, 0xA9, 0xE5, 0x0E, 0x24, 0xDC, 0xCA, 0x9E };
static const uint8_t nordic_spp_rx_uuid128[] = { 0x6E, 0x40, 0x00, 0x02, 0xB5, 0xA3, 0xF3, 0x93, 0xE0, 0xA9, 0xE5, 0x0E, 0x24, 0xDC, 0xCA, 0x9E };
static const uint8_t nordic_spp_tx_uuid128[] = { 0x6E, 0x40, 0x00, 0x03, 0xB5, 0xA3, 0xF3, 0x93, 0xE0, 0xA9, 0xE5, 0x0E, 0x24, 0xDC, 0xCA, 0x9E };
client_callback = callback;
client_packet_handler = packet_handler;
// get service handle range
uint16_t start_handle = 0;

View File

@ -56,9 +56,9 @@ extern "C" {
/**
* @brief Init Nordic SPP Service Server with ATT DB
* @param callback for tx data from peer. Will be called with empty data after service gets activated
* @param packet_handler for events and tx data from peer as RFCOMM_DATA_PACKET
*/
void nordic_spp_service_server_init(void (*callback)(hci_con_handle_t con_handle, const uint8_t * data, uint16_t size));
void nordic_spp_service_server_init(btstack_packet_handler_t packet_handler);
/**
* @brief Queue send request. When called, one packet can be send via nordic_spp_service_send below