1 /* 2 * Copyright (C) 2014 BlueKitchen GmbH 3 * 4 * Redistribution and use in source and binary forms, with or without 5 * modification, are permitted provided that the following conditions 6 * are met: 7 * 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 3. Neither the name of the copyright holders nor the names of 14 * contributors may be used to endorse or promote products derived 15 * from this software without specific prior written permission. 16 * 4. Any redistribution, use, or modification is done solely for 17 * personal benefit and not for any commercial purpose or for 18 * monetary gain. 19 * 20 * THIS SOFTWARE IS PROVIDED BY BLUEKITCHEN GMBH AND CONTRIBUTORS 21 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 23 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL MATTHIAS 24 * RINGWALD OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 25 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 26 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS 27 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 28 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 29 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF 30 * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31 * SUCH DAMAGE. 32 * 33 * Please inquire about commercial licensing options at 34 * [email protected] 35 * 36 */ 37 38 #define __BTSTACK_FILE__ "nordic_spp_le_streamer.c" 39 40 // ***************************************************************************** 41 /* EXAMPLE_START(nordic_spp_le_streamer): LE Streamer - Stream data over GATT. 42 * 43 * @text All newer operating systems provide GATT Client functionality. 44 * This example shows how to get a maximal throughput via BLE: 45 * - send whenever possible, 46 * - use the max ATT MTU. 47 * 48 * @text In theory, we should also update the connection parameters, but we already get 49 * a connection interval of 30 ms and there's no public way to use a shorter 50 * interval with iOS (if we're not implementing an HID device). 51 * 52 * @text Note: To start the streaming, run the example. 53 * On remote device use some GATT Explorer, e.g. LightBlue, BLExplr to enable notifications. 54 */ 55 // ***************************************************************************** 56 57 #include <inttypes.h> 58 #include <stdint.h> 59 #include <stdio.h> 60 #include <stdlib.h> 61 #include <string.h> 62 63 #include "btstack.h" 64 #include "ble/gatt-service/nordic_spp_service_server.h" 65 66 // nordic_spp_le_streamer.gatt contains the declaration of the provided GATT Services + Characteristics 67 // nordic_spp_le_streamer.h contains the binary representation of nordic_spp_le_streamer.gatt 68 // it is generated by the build system by calling: $BTSTACK_ROOT/tool/compile_gatt.py nordic_spp_le_streamer.gatt nordic_spp_le_streamer.h 69 // it needs to be regenerated when the GATT Database declared in nordic_spp_le_streamer.gatt file is modified 70 #include "nordic_spp_le_streamer.h" 71 72 #define REPORT_INTERVAL_MS 3000 73 #define MAX_NR_CONNECTIONS 3 74 75 static void packet_handler (uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size); 76 77 const uint8_t adv_data[] = { 78 // Flags general discoverable, BR/EDR not supported 79 2, BLUETOOTH_DATA_TYPE_FLAGS, 0x06, 80 // Name 81 8, BLUETOOTH_DATA_TYPE_COMPLETE_LOCAL_NAME, 'n', 'R', 'F',' ', 'S', 'P', 'P', 82 // UUID ... 83 17, BLUETOOTH_DATA_TYPE_COMPLETE_LIST_OF_128_BIT_SERVICE_CLASS_UUIDS, 0x9e, 0xca, 0xdc, 0x24, 0xe, 0xe5, 0xa9, 0xe0, 0x93, 0xf3, 0xa3, 0xb5, 0x1, 0x0, 0x40, 0x6e, 84 }; 85 const uint8_t adv_data_len = sizeof(adv_data); 86 87 static btstack_packet_callback_registration_t hci_event_callback_registration; 88 89 // support for multiple clients 90 typedef struct { 91 char name; 92 int le_notification_enabled; 93 hci_con_handle_t connection_handle; 94 int counter; 95 char test_data[200]; 96 int test_data_len; 97 uint32_t test_data_sent; 98 uint32_t test_data_start; 99 btstack_context_callback_registration_t send_request; 100 } nordic_spp_le_streamer_connection_t; 101 102 static nordic_spp_le_streamer_connection_t nordic_spp_le_streamer_connections[MAX_NR_CONNECTIONS]; 103 104 // round robin sending 105 static int connection_index; 106 107 static void init_connections(void){ 108 // track connections 109 int i; 110 for (i=0;i<MAX_NR_CONNECTIONS;i++){ 111 nordic_spp_le_streamer_connections[i].connection_handle = HCI_CON_HANDLE_INVALID; 112 nordic_spp_le_streamer_connections[i].name = 'A' + i; 113 } 114 } 115 116 static nordic_spp_le_streamer_connection_t * connection_for_conn_handle(hci_con_handle_t conn_handle){ 117 int i; 118 for (i=0;i<MAX_NR_CONNECTIONS;i++){ 119 if (nordic_spp_le_streamer_connections[i].connection_handle == conn_handle) return &nordic_spp_le_streamer_connections[i]; 120 } 121 return NULL; 122 } 123 124 static void next_connection_index(void){ 125 connection_index++; 126 if (connection_index == MAX_NR_CONNECTIONS){ 127 connection_index = 0; 128 } 129 } 130 131 /* 132 * @section Track throughput 133 * @text We calculate the throughput by setting a start time and measuring the amount of 134 * data sent. After a configurable REPORT_INTERVAL_MS, we print the throughput in kB/s 135 * and reset the counter and start time. 136 */ 137 138 /* LISTING_START(tracking): Tracking throughput */ 139 140 static void test_reset(nordic_spp_le_streamer_connection_t * context){ 141 context->test_data_start = btstack_run_loop_get_time_ms(); 142 context->test_data_sent = 0; 143 } 144 145 static void test_track_sent(nordic_spp_le_streamer_connection_t * context, int bytes_sent){ 146 context->test_data_sent += bytes_sent; 147 // evaluate 148 uint32_t now = btstack_run_loop_get_time_ms(); 149 uint32_t time_passed = now - context->test_data_start; 150 if (time_passed < REPORT_INTERVAL_MS) return; 151 // print speed 152 int bytes_per_second = context->test_data_sent * 1000 / time_passed; 153 printf("%c: %"PRIu32" bytes sent-> %u.%03u kB/s\n", context->name, context->test_data_sent, bytes_per_second / 1000, bytes_per_second % 1000); 154 155 // restart 156 context->test_data_start = now; 157 context->test_data_sent = 0; 158 } 159 /* LISTING_END(tracking): Tracking throughput */ 160 161 /* 162 * @section Packet Handler 163 * 164 * @text The packet handler is used to stop the notifications and reset the MTU on connect 165 * It would also be a good place to request the connection parameter update as indicated 166 * in the commented code block. 167 */ 168 169 /* LISTING_START(packetHandler): Packet Handler */ 170 static void packet_handler (uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){ 171 UNUSED(channel); 172 UNUSED(size); 173 174 int mtu; 175 uint16_t conn_interval; 176 nordic_spp_le_streamer_connection_t * context; 177 switch (packet_type) { 178 case HCI_EVENT_PACKET: 179 switch (hci_event_packet_get_type(packet)) { 180 case BTSTACK_EVENT_STATE: 181 // BTstack activated, get started 182 if (btstack_event_state_get_state(packet) == HCI_STATE_WORKING) { 183 printf("To start the streaming, please run the AMB2621 Toolbox to connect.\n"); 184 } 185 break; 186 case HCI_EVENT_DISCONNECTION_COMPLETE: 187 context = connection_for_conn_handle(hci_event_disconnection_complete_get_connection_handle(packet)); 188 if (!context) break; 189 // free connection 190 printf("%c: Disconnect, reason %02x\n", context->name, hci_event_disconnection_complete_get_reason(packet)); 191 context->le_notification_enabled = 0; 192 context->connection_handle = HCI_CON_HANDLE_INVALID; 193 break; 194 case HCI_EVENT_LE_META: 195 switch (hci_event_le_meta_get_subevent_code(packet)) { 196 case HCI_SUBEVENT_LE_CONNECTION_COMPLETE: 197 // setup new 198 context = connection_for_conn_handle(HCI_CON_HANDLE_INVALID); 199 if (!context) break; 200 context->counter = 'A'; 201 context->test_data_len = ATT_DEFAULT_MTU - 4; // -1 for nordic 0x01 packet type 202 context->connection_handle = hci_subevent_le_connection_complete_get_connection_handle(packet); 203 // print connection parameters (without using float operations) 204 conn_interval = hci_subevent_le_connection_complete_get_conn_interval(packet); 205 printf("%c: Connection Interval: %u.%02u ms\n", context->name, conn_interval * 125 / 100, 25 * (conn_interval & 3)); 206 printf("%c: Connection Latency: %u\n", context->name, hci_subevent_le_connection_complete_get_conn_latency(packet)); 207 break; 208 } 209 break; 210 case ATT_EVENT_MTU_EXCHANGE_COMPLETE: 211 mtu = att_event_mtu_exchange_complete_get_MTU(packet) - 3; 212 context = connection_for_conn_handle(att_event_mtu_exchange_complete_get_handle(packet)); 213 if (!context) break; 214 context->test_data_len = btstack_min(mtu - 3, sizeof(context->test_data)); 215 printf("%c: ATT MTU = %u => use test data of len %u\n", context->name, mtu, context->test_data_len); 216 break; 217 default: 218 break; 219 } 220 } 221 } 222 223 /* LISTING_END */ 224 /* 225 * @section Streamer 226 * 227 * @text The streamer function checks if notifications are enabled and if a notification can be sent now. 228 * It creates some test data - a single letter that gets increased every time - and tracks the data sent. 229 */ 230 231 /* LISTING_START(streamer): Streaming code */ 232 static void nordic_can_send(void * some_context){ 233 UNUSED(some_context); 234 235 // find next active streaming connection 236 int old_connection_index = connection_index; 237 while (1){ 238 // active found? 239 if ((nordic_spp_le_streamer_connections[connection_index].connection_handle != HCI_CON_HANDLE_INVALID) && 240 (nordic_spp_le_streamer_connections[connection_index].le_notification_enabled)) break; 241 242 // check next 243 next_connection_index(); 244 245 // none found 246 if (connection_index == old_connection_index) return; 247 } 248 249 nordic_spp_le_streamer_connection_t * context = &nordic_spp_le_streamer_connections[connection_index]; 250 251 // create test data 252 context->counter++; 253 if (context->counter > 'Z') context->counter = 'A'; 254 memset(context->test_data, context->counter, context->test_data_len); 255 256 // send 257 nordic_spp_service_server_send(context->connection_handle, (uint8_t*) context->test_data, context->test_data_len); 258 259 // track 260 test_track_sent(context, context->test_data_len); 261 262 // request next send event 263 nordic_spp_service_server_request_can_send_now(&context->send_request, context->connection_handle); 264 265 // check next 266 next_connection_index(); 267 } 268 /* LISTING_END */ 269 270 static void nordic_data_received(hci_con_handle_t tx_con_handle, const uint8_t * data, uint16_t size){ 271 nordic_spp_le_streamer_connection_t * context = connection_for_conn_handle(tx_con_handle); 272 273 if (!context) return; 274 275 if (size == 0 && context->le_notification_enabled == 0){ 276 context->le_notification_enabled = 1; 277 test_reset(context); 278 context->send_request.callback = &nordic_can_send; 279 nordic_spp_service_server_request_can_send_now(&context->send_request, context->connection_handle); 280 } else { 281 printf("RECV: "); 282 printf_hexdump(data, size); 283 test_track_sent(context, size); 284 } 285 } 286 287 int btstack_main(void); 288 int btstack_main(void){ 289 // register for HCI events 290 hci_event_callback_registration.callback = &packet_handler; 291 hci_add_event_handler(&hci_event_callback_registration); 292 293 l2cap_init(); 294 295 // setup LE device DB 296 le_device_db_init(); 297 298 // setup SM: Display only 299 sm_init(); 300 301 // setup ATT server 302 att_server_init(profile_data, NULL, NULL); 303 304 // setup Nordic SPP service 305 nordic_spp_service_server_init(&nordic_data_received); 306 307 // setup advertisements 308 uint16_t adv_int_min = 0x0030; 309 uint16_t adv_int_max = 0x0030; 310 uint8_t adv_type = 0; 311 bd_addr_t null_addr; 312 memset(null_addr, 0, 6); 313 gap_advertisements_set_params(adv_int_min, adv_int_max, adv_type, 0, null_addr, 0x07, 0x00); 314 gap_advertisements_set_data(adv_data_len, (uint8_t*) adv_data); 315 gap_advertisements_enable(1); 316 317 // init client state 318 init_connections(); 319 320 // turn on! 321 hci_power_control(HCI_POWER_ON); 322 323 return 0; 324 } 325 /* EXAMPLE_END */ 326