From 7b6395ea5894399fcd0796038fd033a75749a791 Mon Sep 17 00:00:00 2001 From: Matthias Ringwald Date: Tue, 10 May 2016 10:06:29 +0200 Subject: [PATCH] examples: fix chunk len calculation in att_read_callback for le_couner and spp_and_le_couner --- example/le_counter.c | 14 +++++++++++--- example/spp_and_le_counter.c | 28 +++++++++++++++++++++------- 2 files changed, 32 insertions(+), 10 deletions(-) diff --git a/example/le_counter.c b/example/le_counter.c index f2912c051..b918b632a 100644 --- a/example/le_counter.c +++ b/example/le_counter.c @@ -117,6 +117,9 @@ static void le_counter_setup(void){ heartbeat.process = &heartbeat_handler; btstack_run_loop_set_timer(&heartbeat, HEARTBEAT_PERIOD_MS); btstack_run_loop_add_timer(&heartbeat); + + // beat once + beat(); } /* LISTING_END */ @@ -189,13 +192,18 @@ static void packet_handler (uint8_t packet_type, uint16_t channel, uint8_t *pack // - if buffer != NULL, copy data and return number bytes copied // @param offset defines start of attribute value 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){ + uint16_t size = 0; if (att_handle == ATT_CHARACTERISTIC_0000FF11_0000_1000_8000_00805F9B34FB_01_VALUE_HANDLE){ if (buffer){ - memcpy(buffer, &counter_string[offset], counter_string_len - offset); + // size is bound by actual value chunk and provided buffer + size = btstack_min(counter_string_len - offset, buffer_size); + memcpy(buffer, &counter_string[offset], size); + } else { + // size is only bound by actual value chunk + size = counter_string_len - offset; } - return counter_string_len - offset; } - return 0; + return size; } /* LISTING_END */ diff --git a/example/spp_and_le_counter.c b/example/spp_and_le_counter.c index 1bdd6c8c6..8f51727c9 100644 --- a/example/spp_and_le_counter.c +++ b/example/spp_and_le_counter.c @@ -176,14 +176,18 @@ static void packet_handler (uint8_t packet_type, uint16_t channel, uint8_t *pack // - if buffer != NULL, copy data and return number bytes copied // @param offset defines start of attribute value static uint16_t att_read_callback(hci_con_handle_t con_handle, uint16_t att_handle, uint16_t offset, uint8_t * buffer, uint16_t buffer_size){ + uint16_t size = 0; if (att_handle == ATT_CHARACTERISTIC_0000FF11_0000_1000_8000_00805F9B34FB_01_VALUE_HANDLE){ if (buffer){ - log_info("att_read_callback for Characteristic *FF11*, value %s", counter_string); - memcpy(buffer, &counter_string[offset], counter_string_len - offset); + // size is bound by actual value chunk and provided buffer + size = btstack_min(counter_string_len - offset, buffer_size); + memcpy(buffer, &counter_string[offset], size); + } else { + // size is only bound by actual value chunk + size = counter_string_len - offset; } - return counter_string_len - offset; } - return 0; + return size; } // write requests @@ -207,6 +211,12 @@ static int att_write_callback(hci_con_handle_t con_handle, uint16_t att_handle, } } +static void beat(void){ + counter++; + counter_string_len = sprintf(counter_string, "BTstack counter %04u", counter); + puts(counter_string); +} + /* * @section Heartbeat Handler * @@ -217,9 +227,9 @@ static int att_write_callback(hci_con_handle_t con_handle, uint16_t att_handle, /* LISTING_START(heartbeat): Combined Heartbeat handler */ static void heartbeat_handler(struct btstack_timer_source *ts){ - counter++; - counter_string_len = sprintf(counter_string, "BTstack counter %04u\n", counter); - // log_info("%s", counter_string); + if (rfcomm_channel_id || le_notification_enabled) { + beat(); + } if (rfcomm_channel_id){ rfcomm_request_can_send_now_event(rfcomm_channel_id); @@ -228,6 +238,7 @@ static void heartbeat_handler(struct btstack_timer_source *ts){ if (le_notification_enabled) { att_server_request_can_send_now_event(att_con_handle); } + btstack_run_loop_set_timer(ts, HEARTBEAT_PERIOD_MS); btstack_run_loop_add_timer(ts); } @@ -288,6 +299,9 @@ int btstack_main(void) gap_advertisements_set_data(adv_data_len, (uint8_t*) adv_data); gap_advertisements_enable(1); + // beat once + beat(); + // turn on! hci_power_control(HCI_POWER_ON);