From c4792ed925931d63419bd93996afdabfa0a79eac Mon Sep 17 00:00:00 2001 From: Matthias Ringwald Date: Sat, 2 Apr 2016 22:22:36 +0200 Subject: [PATCH] examples: le_counter, le_streamer - use att_server_request_can_send_now_event --- example/le_counter.c | 61 +++++++++++++++++++++++-------------------- example/le_streamer.c | 21 ++++++++------- 2 files changed, 45 insertions(+), 37 deletions(-) diff --git a/example/le_counter.c b/example/le_counter.c index dfe5f0016..c7e8fb282 100644 --- a/example/le_counter.c +++ b/example/le_counter.c @@ -100,6 +100,7 @@ static void le_counter_setup(void){ // setup ATT server att_server_init(profile_data, att_read_callback, att_write_callback); + att_server_register_packet_handler(packet_handler); // setup advertisements uint16_t adv_int_min = 0x0030; @@ -118,37 +119,11 @@ static void le_counter_setup(void){ } /* LISTING_END */ -/* - * @section Packet Handler - * - * @text The packet handler is only used to stop the counter after a disconnect - */ - -/* LISTING_START(packetHandler): Packet Handler */ -static void packet_handler (uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){ - switch (packet_type) { - case HCI_EVENT_PACKET: - switch (hci_event_packet_get_type(packet)) { - case BTSTACK_EVENT_STATE: - if (btstack_event_state_get_state(packet) == HCI_STATE_WORKING){ - printf("LE Counter Demo ready.\n"); - beat(); - } - break; - case HCI_EVENT_DISCONNECTION_COMPLETE: - le_notification_enabled = 0; - break; - } - break; - } -} -/* LISTING_END */ - /* * @section Heartbeat Handler * * @text The heartbeat handler updates the value of the single Characteristic provided in this example, - * and sends a notification for this characteristic if enabled, see Listing heartbeat. + * and request a ATT_EVENT_CAN_SEND_NOW to send a notification if enabled see Listing heartbeat. */ /* LISTING_START(heartbeat): Hearbeat Handler */ @@ -165,13 +140,43 @@ static void beat(void){ static void heartbeat_handler(struct btstack_timer_source *ts){ if (le_notification_enabled) { beat(); - att_server_notify(ATT_CHARACTERISTIC_0000FF11_0000_1000_8000_00805F9B34FB_01_VALUE_HANDLE, (uint8_t*) counter_string, counter_string_len); + att_server_request_can_send_now_event(); } btstack_run_loop_set_timer(ts, HEARTBEAT_PERIOD_MS); btstack_run_loop_add_timer(ts); } /* LISTING_END */ +/* + * @section Packet Handler + * + * @text The packet handler is used to: + * - stop the counter after a disconnect + * - send a notification when the requested ATT_EVENT_CAN_SEND_NOW is received + */ + +/* LISTING_START(packetHandler): Packet Handler */ +static void packet_handler (uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){ + switch (packet_type) { + case HCI_EVENT_PACKET: + switch (hci_event_packet_get_type(packet)) { + case BTSTACK_EVENT_STATE: + if (btstack_event_state_get_state(packet) == HCI_STATE_WORKING){ + printf("LE Counter Demo ready.\n"); + beat(); + } + break; + case HCI_EVENT_DISCONNECTION_COMPLETE: + le_notification_enabled = 0; + break; + case ATT_EVENT_CAN_SEND_NOW: + att_server_notify(ATT_CHARACTERISTIC_0000FF11_0000_1000_8000_00805F9B34FB_01_VALUE_HANDLE, (uint8_t*) counter_string, counter_string_len); + break; + } + break; + } +} +/* LISTING_END */ /* * @section ATT Read diff --git a/example/le_streamer.c b/example/le_streamer.c index cd13b4788..4ee1f49a7 100644 --- a/example/le_streamer.c +++ b/example/le_streamer.c @@ -183,10 +183,11 @@ static void packet_handler (uint8_t packet_type, uint16_t channel, uint8_t *pack printf("ATT MTU = %u\n", mtu); test_data_len = mtu - 3; break; + case ATT_EVENT_CAN_SEND_NOW: + streamer(); + break; } } - // try sending whenever something happens - streamer(); } /* LISTING_END */ @@ -201,21 +202,20 @@ static void packet_handler (uint8_t packet_type, uint16_t channel, uint8_t *pack static void streamer(void){ // check if we can send if (!le_notification_enabled) return; - if (!att_server_can_send_packet_now()) return; // create test data - int i; counter++; if (counter > 'Z') counter = 'A'; - for (i=0;i