examples: le_counter, le_streamer - use att_server_request_can_send_now_event

This commit is contained in:
Matthias Ringwald 2016-04-02 22:22:36 +02:00
parent 3c97b2428c
commit c4792ed925
2 changed files with 45 additions and 37 deletions

View File

@ -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

View File

@ -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<sizeof(test_data);i++){
test_data[i] = counter;
}
memset(test_data, counter, sizeof(test_data));
// send
att_server_notify(ATT_CHARACTERISTIC_0000FF11_0000_1000_8000_00805F9B34FB_01_VALUE_HANDLE, (uint8_t*) test_data, test_data_len);
// track
test_track_sent(test_data_len);
// request next send event
att_server_request_can_send_now_event();
}
/* LISTING_END */
@ -223,8 +223,8 @@ static void streamer(void){
* @section ATT Write
*
* @text The only valid ATT write in this example is to the Client Characteristic Configuration, which configures notification
* and indication. If the ATT handle matches the client configuration handle, the new configuration value is stored and used
* in the heartbeat handler to decide if a new value should be sent. See Listing attWrite.
* and indication. If the ATT handle matches the client configuration handle, the new configuration value is stored.
* If notifications get enabled, an ATT_EVENT_CAN_SEND_NOW is requested. See Listing attWrite.
*/
/* LISTING_START(attWrite): ATT Write */
@ -235,6 +235,9 @@ static int att_write_callback(hci_con_handle_t con_handle, uint16_t att_handle,
case ATT_CHARACTERISTIC_0000FF11_0000_1000_8000_00805F9B34FB_01_CLIENT_CONFIGURATION_HANDLE:
le_notification_enabled = little_endian_read_16(buffer, 0) == GATT_CLIENT_CHARACTERISTICS_CONFIGURATION_NOTIFICATION;
printf("Notifications enabled %u\n", le_notification_enabled);
if (le_notification_enabled){
att_server_request_can_send_now_event();
}
test_reset();
break;
case ATT_CHARACTERISTIC_0000FF12_0000_1000_8000_00805F9B34FB_01_VALUE_HANDLE: