nordic_spp_le_streamer.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  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. * contact@bluekitchen-gmbh.com
  35. *
  36. */
  37. #define BTSTACK_FILE__ "nordic_spp_le_streamer.c"
  38. // *****************************************************************************
  39. /* EXAMPLE_START(nordic_spp_le_streamer): LE Streamer - Stream data over GATT.
  40. *
  41. * @text All newer operating systems provide GATT Client functionality.
  42. * This example shows how to get a maximal throughput via BLE:
  43. * - send whenever possible,
  44. * - use the max ATT MTU.
  45. *
  46. * @text In theory, we should also update the connection parameters, but we already get
  47. * a connection interval of 30 ms and there's no public way to use a shorter
  48. * interval with iOS (if we're not implementing an HID device).
  49. *
  50. * @text Note: To start the streaming, run the example.
  51. * On remote device use some GATT Explorer, e.g. LightBlue, BLExplr to enable notifications.
  52. */
  53. // *****************************************************************************
  54. #include <inttypes.h>
  55. #include <stdint.h>
  56. #include <stdio.h>
  57. #include <stdlib.h>
  58. #include <string.h>
  59. #include "btstack.h"
  60. #include "ble/gatt-service/nordic_spp_service_server.h"
  61. // nordic_spp_le_streamer.gatt contains the declaration of the provided GATT Services + Characteristics
  62. // nordic_spp_le_streamer.h contains the binary representation of nordic_spp_le_streamer.gatt
  63. // 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
  64. // it needs to be regenerated when the GATT Database declared in nordic_spp_le_streamer.gatt file is modified
  65. #include "nordic_spp_le_streamer.h"
  66. #define REPORT_INTERVAL_MS 3000
  67. #define MAX_NR_CONNECTIONS 3
  68. const uint8_t adv_data[] = {
  69. // Flags general discoverable, BR/EDR not supported
  70. 2, BLUETOOTH_DATA_TYPE_FLAGS, 0x06,
  71. // Name
  72. 8, BLUETOOTH_DATA_TYPE_COMPLETE_LOCAL_NAME, 'n', 'R', 'F',' ', 'S', 'P', 'P',
  73. // UUID ...
  74. 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,
  75. };
  76. const uint8_t adv_data_len = sizeof(adv_data);
  77. static btstack_packet_callback_registration_t hci_event_callback_registration;
  78. // support for multiple clients
  79. typedef struct {
  80. char name;
  81. int le_notification_enabled;
  82. hci_con_handle_t connection_handle;
  83. int counter;
  84. char test_data[200];
  85. int test_data_len;
  86. uint32_t test_data_sent;
  87. uint32_t test_data_start;
  88. btstack_context_callback_registration_t send_request;
  89. } nordic_spp_le_streamer_connection_t;
  90. static nordic_spp_le_streamer_connection_t nordic_spp_le_streamer_connections[MAX_NR_CONNECTIONS];
  91. // round robin sending
  92. static int connection_index;
  93. static void init_connections(void){
  94. // track connections
  95. int i;
  96. for (i=0;i<MAX_NR_CONNECTIONS;i++){
  97. nordic_spp_le_streamer_connections[i].connection_handle = HCI_CON_HANDLE_INVALID;
  98. nordic_spp_le_streamer_connections[i].name = 'A' + i;
  99. }
  100. }
  101. static nordic_spp_le_streamer_connection_t * connection_for_conn_handle(hci_con_handle_t conn_handle){
  102. int i;
  103. for (i=0;i<MAX_NR_CONNECTIONS;i++){
  104. if (nordic_spp_le_streamer_connections[i].connection_handle == conn_handle) return &nordic_spp_le_streamer_connections[i];
  105. }
  106. return NULL;
  107. }
  108. static void next_connection_index(void){
  109. connection_index++;
  110. if (connection_index == MAX_NR_CONNECTIONS){
  111. connection_index = 0;
  112. }
  113. }
  114. /*
  115. * @section Track throughput
  116. * @text We calculate the throughput by setting a start time and measuring the amount of
  117. * data sent. After a configurable REPORT_INTERVAL_MS, we print the throughput in kB/s
  118. * and reset the counter and start time.
  119. */
  120. /* LISTING_START(tracking): Tracking throughput */
  121. static void test_reset(nordic_spp_le_streamer_connection_t * context){
  122. context->test_data_start = btstack_run_loop_get_time_ms();
  123. context->test_data_sent = 0;
  124. }
  125. static void test_track_sent(nordic_spp_le_streamer_connection_t * context, int bytes_sent){
  126. context->test_data_sent += bytes_sent;
  127. // evaluate
  128. uint32_t now = btstack_run_loop_get_time_ms();
  129. uint32_t time_passed = now - context->test_data_start;
  130. if (time_passed < REPORT_INTERVAL_MS) return;
  131. // print speed
  132. int bytes_per_second = context->test_data_sent * 1000 / time_passed;
  133. printf("%c: %"PRIu32" bytes sent-> %u.%03u kB/s\n", context->name, context->test_data_sent, bytes_per_second / 1000, bytes_per_second % 1000);
  134. // restart
  135. context->test_data_start = now;
  136. context->test_data_sent = 0;
  137. }
  138. /* LISTING_END(tracking): Tracking throughput */
  139. /*
  140. * @section HCI Packet Handler
  141. *
  142. * @text The packet handler prints the welcome message and requests a connection paramter update for LE Connections
  143. */
  144. /* LISTING_START(packetHandler): Packet Handler */
  145. static void hci_packet_handler (uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
  146. UNUSED(channel);
  147. UNUSED(size);
  148. uint16_t conn_interval;
  149. hci_con_handle_t con_handle;
  150. switch (packet_type) {
  151. case HCI_EVENT_PACKET:
  152. switch (hci_event_packet_get_type(packet)) {
  153. case BTSTACK_EVENT_STATE:
  154. // BTstack activated, get started
  155. if (btstack_event_state_get_state(packet) == HCI_STATE_WORKING) {
  156. printf("To start the streaming, please run nRF Toolbox -> UART to connect.\n");
  157. }
  158. break;
  159. case HCI_EVENT_LE_META:
  160. switch (hci_event_le_meta_get_subevent_code(packet)) {
  161. case HCI_SUBEVENT_LE_CONNECTION_COMPLETE:
  162. // print connection parameters (without using float operations)
  163. con_handle = hci_subevent_le_connection_complete_get_connection_handle(packet);
  164. conn_interval = hci_subevent_le_connection_complete_get_conn_interval(packet);
  165. printf("LE Connection - Connection Interval: %u.%02u ms\n", conn_interval * 125 / 100, 25 * (conn_interval & 3));
  166. printf("LE Connection - Connection Latency: %u\n", hci_subevent_le_connection_complete_get_conn_latency(packet));
  167. // request min con interval 15 ms for iOS 11+
  168. printf("LE Connection - Request 15 ms connection interval\n");
  169. gap_request_connection_parameter_update(con_handle, 12, 12, 0, 0x0048);
  170. break;
  171. case HCI_SUBEVENT_LE_CONNECTION_UPDATE_COMPLETE:
  172. // print connection parameters (without using float operations)
  173. con_handle = hci_subevent_le_connection_update_complete_get_connection_handle(packet);
  174. conn_interval = hci_subevent_le_connection_update_complete_get_conn_interval(packet);
  175. printf("LE Connection - Connection Param update - connection interval %u.%02u ms, latency %u\n", conn_interval * 125 / 100,
  176. 25 * (conn_interval & 3), hci_subevent_le_connection_update_complete_get_conn_latency(packet));
  177. break;
  178. default:
  179. break;
  180. }
  181. break;
  182. default:
  183. break;
  184. }
  185. }
  186. }
  187. /* LISTING_END */
  188. /*
  189. * @section ATT Packet Handler
  190. *
  191. * @text The packet handler is used to setup and tear down the spp-over-gatt connection and its MTU
  192. */
  193. /* LISTING_START(packetHandler): Packet Handler */
  194. static void att_packet_handler (uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
  195. UNUSED(channel);
  196. UNUSED(size);
  197. int mtu;
  198. nordic_spp_le_streamer_connection_t * context;
  199. switch (packet_type) {
  200. case HCI_EVENT_PACKET:
  201. switch (hci_event_packet_get_type(packet)) {
  202. case ATT_EVENT_CONNECTED:
  203. // setup new
  204. context = connection_for_conn_handle(HCI_CON_HANDLE_INVALID);
  205. if (!context) break;
  206. context->counter = 'A';
  207. context->test_data_len = ATT_DEFAULT_MTU - 4; // -1 for nordic 0x01 packet type
  208. context->connection_handle = att_event_connected_get_handle(packet);
  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. case ATT_EVENT_DISCONNECTED:
  218. context = connection_for_conn_handle(att_event_disconnected_get_handle(packet));
  219. if (!context) break;
  220. // free connection
  221. printf("%c: Disconnect\n", context->name);
  222. context->le_notification_enabled = 0;
  223. context->connection_handle = HCI_CON_HANDLE_INVALID;
  224. break;
  225. default:
  226. break;
  227. }
  228. }
  229. }
  230. /* LISTING_END */
  231. /*
  232. * @section Streamer
  233. *
  234. * @text The streamer function checks if notifications are enabled and if a notification can be sent now.
  235. * It creates some test data - a single letter that gets increased every time - and tracks the data sent.
  236. */
  237. /* LISTING_START(streamer): Streaming code */
  238. static void nordic_can_send(void * some_context){
  239. UNUSED(some_context);
  240. // find next active streaming connection
  241. int old_connection_index = connection_index;
  242. while (1){
  243. // active found?
  244. if ((nordic_spp_le_streamer_connections[connection_index].connection_handle != HCI_CON_HANDLE_INVALID) &&
  245. (nordic_spp_le_streamer_connections[connection_index].le_notification_enabled)) break;
  246. // check next
  247. next_connection_index();
  248. // none found
  249. if (connection_index == old_connection_index) return;
  250. }
  251. nordic_spp_le_streamer_connection_t * context = &nordic_spp_le_streamer_connections[connection_index];
  252. // create test data
  253. context->counter++;
  254. if (context->counter > 'Z') context->counter = 'A';
  255. memset(context->test_data, context->counter, context->test_data_len);
  256. // send
  257. nordic_spp_service_server_send(context->connection_handle, (uint8_t*) context->test_data, context->test_data_len);
  258. // track
  259. test_track_sent(context, context->test_data_len);
  260. // request next send event
  261. nordic_spp_service_server_request_can_send_now(&context->send_request, context->connection_handle);
  262. // check next
  263. next_connection_index();
  264. }
  265. /* LISTING_END */
  266. static void nordic_data_received(hci_con_handle_t tx_con_handle, const uint8_t * data, uint16_t size){
  267. nordic_spp_le_streamer_connection_t * context = connection_for_conn_handle(tx_con_handle);
  268. if (!context) return;
  269. if (size == 0 && context->le_notification_enabled == 0){
  270. context->le_notification_enabled = 1;
  271. test_reset(context);
  272. context->send_request.callback = &nordic_can_send;
  273. nordic_spp_service_server_request_can_send_now(&context->send_request, context->connection_handle);
  274. } else {
  275. printf("RECV: ");
  276. printf_hexdump(data, size);
  277. test_track_sent(context, size);
  278. }
  279. }
  280. int btstack_main(void);
  281. int btstack_main(void){
  282. // register for HCI events
  283. hci_event_callback_registration.callback = &hci_packet_handler;
  284. hci_add_event_handler(&hci_event_callback_registration);
  285. l2cap_init();
  286. // setup LE device DB
  287. le_device_db_init();
  288. // setup SM: Display only
  289. sm_init();
  290. // setup ATT server
  291. att_server_init(profile_data, NULL, NULL);
  292. // setup Nordic SPP service
  293. nordic_spp_service_server_init(&nordic_data_received);
  294. // register for ATT events
  295. att_server_register_packet_handler(att_packet_handler);
  296. // setup advertisements
  297. uint16_t adv_int_min = 0x0030;
  298. uint16_t adv_int_max = 0x0030;
  299. uint8_t adv_type = 0;
  300. bd_addr_t null_addr;
  301. memset(null_addr, 0, 6);
  302. gap_advertisements_set_params(adv_int_min, adv_int_max, adv_type, 0, null_addr, 0x07, 0x00);
  303. gap_advertisements_set_data(adv_data_len, (uint8_t*) adv_data);
  304. gap_advertisements_enable(1);
  305. // init client state
  306. init_connections();
  307. // turn on!
  308. hci_power_control(HCI_POWER_ON);
  309. return 0;
  310. }
  311. /* EXAMPLE_END */