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 BLUEKITCHEN
24 * GMBH 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__ "spp_and_gatt_streamer.c"
39
40 // *****************************************************************************
41 /* EXAMPLE_START(spp_and_le_streamer): Dual mode example
42 *
43 * @text The SPP and LE Streamer example combines the Bluetooth Classic SPP Streamer
44 * and the Bluetooth LE Streamer into a single application.
45 *
46 * @text In this Section, we only point out the differences to the individual examples
47 * and how how the stack is configured.
48 *
49 * @text Note: To test, please run the example, and then:
50 * - for SPP pair from a remote device, and open the Virtual Serial Port,
51 * - for LE use some GATT Explorer, e.g. LightBlue, BLExplr, to enable notifications.
52 *
53 */
54 // *****************************************************************************
55
56 #include <stdint.h>
57 #include <stdio.h>
58 #include <stdlib.h>
59 #include <string.h>
60 #include <inttypes.h>
61
62 #include "btstack.h"
63 #include "spp_and_gatt_streamer.h"
64
65 int btstack_main(int argc, const char * argv[]);
66
67 #define RFCOMM_SERVER_CHANNEL 1
68 #define HEARTBEAT_PERIOD_MS 1000
69
70 #define TEST_COD 0x1234
71 #define NUM_ROWS 25
72 #define NUM_COLS 40
73 #define DATA_VOLUME (10 * 1000 * 1000)
74
75 /*
76 * @section Advertisements
77 *
78 * @text The Flags attribute in the Advertisement Data indicates if a device is dual-mode or le-only.
79 */
80 /* LISTING_START(advertisements): Advertisement data: Flag 0x02 indicates dual-mode device */
81 const uint8_t adv_data[] = {
82 // Flags general discoverable
83 0x02, BLUETOOTH_DATA_TYPE_FLAGS, 0x02,
84 // Name
85 0x0c, BLUETOOTH_DATA_TYPE_COMPLETE_LOCAL_NAME, 'L', 'E', ' ', 'S', 't', 'r', 'e', 'a', 'm', 'e', 'r',
86 };
87
88 static btstack_packet_callback_registration_t hci_event_callback_registration;
89
90 uint8_t adv_data_len = sizeof(adv_data);
91
92 static uint8_t test_data[NUM_ROWS * NUM_COLS];
93
94 // SPP
95 static uint8_t spp_service_buffer[150];
96
97 static uint16_t spp_test_data_len;
98 static uint16_t rfcomm_mtu;
99 static uint16_t rfcomm_cid = 0;
100 // static uint32_t data_to_send = DATA_VOLUME;
101
102 // LE
103 static uint16_t att_mtu;
104 static int counter = 'A';
105 static int le_notification_enabled;
106 static uint16_t le_test_data_len;
107 static hci_con_handle_t le_connection_handle;
108
109 #ifdef ENABLE_GATT_OVER_CLASSIC
110 static uint8_t gatt_service_buffer[70];
111 #endif
112
113 /*
114 * @section Track throughput
115 * @text We calculate the throughput by setting a start time and measuring the amount of
116 * data sent. After a configurable REPORT_INTERVAL_MS, we print the throughput in kB/s
117 * and reset the counter and start time.
118 */
119
120 /* LISTING_START(tracking): Tracking throughput */
121 #define REPORT_INTERVAL_MS 3000
122 static uint32_t test_data_transferred;
123 static uint32_t test_data_start;
124
test_reset(void)125 static void test_reset(void){
126 test_data_start = btstack_run_loop_get_time_ms();
127 test_data_transferred = 0;
128 }
129
test_track_transferred(int bytes_sent)130 static void test_track_transferred(int bytes_sent){
131 test_data_transferred += bytes_sent;
132 // evaluate
133 uint32_t now = btstack_run_loop_get_time_ms();
134 uint32_t time_passed = now - test_data_start;
135 if (time_passed < REPORT_INTERVAL_MS) return;
136 // print speed
137 int bytes_per_second = test_data_transferred * 1000 / time_passed;
138 printf("%u bytes -> %u.%03u kB/s\n", (int) test_data_transferred, (int) bytes_per_second / 1000, bytes_per_second % 1000);
139
140 // restart
141 test_data_start = now;
142 test_data_transferred = 0;
143 }
144 /* LISTING_END(tracking): Tracking throughput */
145
146
spp_create_test_data(void)147 static void spp_create_test_data(void){
148 int x,y;
149 for (y=0;y<NUM_ROWS;y++){
150 for (x=0;x<NUM_COLS-2;x++){
151 test_data[y*NUM_COLS+x] = '0' + (x % 10);
152 }
153 test_data[y*NUM_COLS+NUM_COLS-2] = '\n';
154 test_data[y*NUM_COLS+NUM_COLS-1] = '\r';
155 }
156 }
157
spp_send_packet(void)158 static void spp_send_packet(void){
159 rfcomm_send(rfcomm_cid, (uint8_t*) test_data, spp_test_data_len);
160
161 test_track_transferred(spp_test_data_len);
162 #if 0
163 if (data_to_send <= spp_test_data_len){
164 printf("SPP Streamer: enough data send, closing channel\n");
165 rfcomm_disconnect(rfcomm_cid);
166 rfcomm_cid = 0;
167 return;
168 }
169 data_to_send -= spp_test_data_len;
170 #endif
171 rfcomm_request_can_send_now_event(rfcomm_cid);
172 }
173
le_streamer(void)174 static void le_streamer(void){
175 // check if we can send
176 if (!le_notification_enabled) return;
177
178 // create test data
179 counter++;
180 if (counter > 'Z') counter = 'A';
181 memset(test_data, counter, sizeof(test_data));
182
183 // send
184 att_server_notify(le_connection_handle, ATT_CHARACTERISTIC_0000FF11_0000_1000_8000_00805F9B34FB_01_VALUE_HANDLE, (uint8_t*) test_data, le_test_data_len);
185
186 // track
187 test_track_transferred(le_test_data_len);
188
189 // request next send event
190 att_server_request_can_send_now_event(le_connection_handle);
191 }
192
193 /*
194 * @section HCI Packet Handler
195 *
196 * @text The packet handler of the combined example is just the combination of the individual packet handlers.
197 */
198
hci_packet_handler(uint8_t packet_type,uint16_t channel,uint8_t * packet,uint16_t size)199 static void hci_packet_handler (uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
200 UNUSED(channel);
201 UNUSED(size);
202
203 bd_addr_t event_addr;
204 uint16_t conn_interval;
205 hci_con_handle_t con_handle;
206
207 switch (packet_type) {
208 case HCI_EVENT_PACKET:
209 switch (hci_event_packet_get_type(packet)) {
210
211 case HCI_EVENT_PIN_CODE_REQUEST:
212 // inform about pin code request
213 printf("Pin code request - using '0000'\n");
214 hci_event_pin_code_request_get_bd_addr(packet, event_addr);
215 gap_pin_code_response(event_addr, "0000");
216 break;
217
218 case HCI_EVENT_USER_CONFIRMATION_REQUEST:
219 // inform about user confirmation request
220 printf("SSP User Confirmation Request with numeric value '%06"PRIu32"'\n", little_endian_read_32(packet, 8));
221 printf("SSP User Confirmation Auto accept\n");
222 break;
223
224 case HCI_EVENT_META_GAP:
225 switch (hci_event_gap_meta_get_subevent_code(packet)) {
226 case GAP_SUBEVENT_LE_CONNECTION_COMPLETE:
227 // print connection parameters (without using float operations)
228 con_handle = gap_subevent_le_connection_complete_get_connection_handle(packet);
229 conn_interval = gap_subevent_le_connection_complete_get_conn_interval(packet);
230 printf("LE Connection - Connection Interval: %u.%02u ms\n", conn_interval * 125 / 100, 25 * (conn_interval & 3));
231 printf("LE Connection - Connection Latency: %u\n", gap_subevent_le_connection_complete_get_conn_latency(packet));
232
233 // request min con interval 15 ms for iOS 11+
234 printf("LE Connection - Request 15 ms connection interval\n");
235 gap_request_connection_parameter_update(con_handle, 12, 12, 4, 0x0048);
236 break;
237 default:
238 break;
239 }
240 break;
241
242 case HCI_EVENT_LE_META:
243 switch (hci_event_le_meta_get_subevent_code(packet)) {
244 case HCI_SUBEVENT_LE_CONNECTION_UPDATE_COMPLETE:
245 // print connection parameters (without using float operations)
246 con_handle = hci_subevent_le_connection_update_complete_get_connection_handle(packet);
247 conn_interval = hci_subevent_le_connection_update_complete_get_conn_interval(packet);
248 printf("LE Connection - Connection Param update - connection interval %u.%02u ms, latency %u\n", conn_interval * 125 / 100,
249 25 * (conn_interval & 3), hci_subevent_le_connection_update_complete_get_conn_latency(packet));
250 break;
251
252 default:
253 break;
254 }
255 break;
256
257 case HCI_EVENT_DISCONNECTION_COMPLETE:
258 // re-enable page/inquiry scan again
259 gap_discoverable_control(1);
260 gap_connectable_control(1);
261 // re-enable advertisements
262 gap_advertisements_enable(1);
263 le_notification_enabled = 0;
264 break;
265
266 default:
267 break;
268 }
269 break;
270
271 default:
272 break;
273 }
274 }
275
276 /*
277 * @section RFCOMM Packet Handler
278 *
279 * @text The RFCOMM packet handler accepts incoming connection and triggers sending of RFCOMM data on RFCOMM_EVENT_CAN_SEND_NOW
280 */
281
rfcomm_packet_handler(uint8_t packet_type,uint16_t channel,uint8_t * packet,uint16_t size)282 static void rfcomm_packet_handler (uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
283 UNUSED(channel);
284
285 bd_addr_t event_addr;
286 uint8_t rfcomm_channel_nr;
287
288 switch (packet_type) {
289 case HCI_EVENT_PACKET:
290 switch (hci_event_packet_get_type(packet)) {
291
292 case RFCOMM_EVENT_INCOMING_CONNECTION:
293 // data: event (8), len(8), address(48), channel (8), rfcomm_cid (16)
294 rfcomm_event_incoming_connection_get_bd_addr(packet, event_addr);
295 rfcomm_channel_nr = rfcomm_event_incoming_connection_get_server_channel(packet);
296 rfcomm_cid = rfcomm_event_incoming_connection_get_rfcomm_cid(packet);
297 printf("RFCOMM channel %u requested for %s\n", rfcomm_channel_nr, bd_addr_to_str(event_addr));
298 rfcomm_accept_connection(rfcomm_cid);
299 break;
300
301 case RFCOMM_EVENT_CHANNEL_OPENED:
302 // data: event(8), len(8), status (8), address (48), server channel(8), rfcomm_cid(16), max frame size(16)
303 if (rfcomm_event_channel_opened_get_status(packet)) {
304 printf("RFCOMM channel open failed, status 0x%02x\n", rfcomm_event_channel_opened_get_status(packet));
305 } else {
306 rfcomm_cid = rfcomm_event_channel_opened_get_rfcomm_cid(packet);
307 rfcomm_mtu = rfcomm_event_channel_opened_get_max_frame_size(packet);
308 printf("RFCOMM channel open succeeded. New RFCOMM Channel ID %u, max frame size %u\n", rfcomm_cid, rfcomm_mtu);
309
310 spp_test_data_len = rfcomm_mtu;
311 if (spp_test_data_len > sizeof(test_data)){
312 spp_test_data_len = sizeof(test_data);
313 }
314
315 // disable page/inquiry scan to get max performance
316 gap_discoverable_control(0);
317 gap_connectable_control(0);
318 // disable advertisements
319 gap_advertisements_enable(0);
320
321 test_reset();
322 rfcomm_request_can_send_now_event(rfcomm_cid);
323 }
324 break;
325
326 case RFCOMM_EVENT_CAN_SEND_NOW:
327 spp_send_packet();
328 break;
329
330 case RFCOMM_EVENT_CHANNEL_CLOSED:
331 printf("RFCOMM channel closed\n");
332 rfcomm_cid = 0;
333 break;
334
335 default:
336 break;
337 }
338 break;
339
340 case RFCOMM_DATA_PACKET:
341 test_track_transferred(size);
342 #if 0
343 printf("RCV: '");
344 for (i=0;i<size;i++){
345 putchar(packet[i]);
346 }
347 printf("'\n");
348 #endif
349 break;
350
351 default:
352 break;
353 }
354 }
355
356 /*
357 * @section ATT Packet Handler
358 *
359 * @text The packet handler is used to track the ATT MTU Exchange and trigger ATT send
360 */
361
362 /* LISTING_START(attPacketHandler): Packet Handler */
att_packet_handler(uint8_t packet_type,uint16_t channel,uint8_t * packet,uint16_t size)363 static void att_packet_handler (uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
364 UNUSED(channel);
365 UNUSED(size);
366
367 if (packet_type != HCI_EVENT_PACKET) return;
368
369 switch (hci_event_packet_get_type(packet)) {
370 case ATT_EVENT_CONNECTED:
371 le_connection_handle = att_event_connected_get_handle(packet);
372 att_mtu = att_server_get_mtu(le_connection_handle);
373 le_test_data_len = btstack_min(att_server_get_mtu(le_connection_handle) - 3, sizeof(test_data));
374 printf("ATT MTU = %u\n", att_mtu);
375 break;
376
377 case ATT_EVENT_MTU_EXCHANGE_COMPLETE:
378 att_mtu = att_event_mtu_exchange_complete_get_MTU(packet);
379 le_test_data_len = btstack_min(att_mtu - 3, sizeof(test_data));
380 printf("ATT MTU = %u\n", att_mtu);
381 break;
382
383 case ATT_EVENT_CAN_SEND_NOW:
384 le_streamer();
385 break;
386
387 case ATT_EVENT_DISCONNECTED:
388 le_notification_enabled = 0;
389 le_connection_handle = HCI_CON_HANDLE_INVALID;
390 break;
391
392 default:
393 break;
394 }
395 }
396
397 // ATT Client Read Callback for Dynamic Data
398 // - if buffer == NULL, don't copy data, just return size of value
399 // - if buffer != NULL, copy data and return number bytes copied
400 // @param offset defines start of attribute value
att_read_callback(hci_con_handle_t con_handle,uint16_t att_handle,uint16_t offset,uint8_t * buffer,uint16_t buffer_size)401 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){
402 UNUSED(con_handle);
403 UNUSED(att_handle);
404 UNUSED(offset);
405 UNUSED(buffer);
406 UNUSED(buffer_size);
407 return 0;
408 }
409
410 // write requests
att_write_callback(hci_con_handle_t con_handle,uint16_t att_handle,uint16_t transaction_mode,uint16_t offset,uint8_t * buffer,uint16_t buffer_size)411 static int att_write_callback(hci_con_handle_t con_handle, uint16_t att_handle, uint16_t transaction_mode, uint16_t offset, uint8_t *buffer, uint16_t buffer_size){
412 UNUSED(con_handle);
413 UNUSED(offset);
414 UNUSED(buffer_size);
415
416 // printf("att_write_callback att_handle %04x, transaction mode %u\n", att_handle, transaction_mode);
417 if (transaction_mode != ATT_TRANSACTION_MODE_NONE) return 0;
418 switch(att_handle){
419 case ATT_CHARACTERISTIC_0000FF11_0000_1000_8000_00805F9B34FB_01_CLIENT_CONFIGURATION_HANDLE:
420 le_notification_enabled = little_endian_read_16(buffer, 0) == GATT_CLIENT_CHARACTERISTICS_CONFIGURATION_NOTIFICATION;
421 printf("Notifications enabled %u\n", le_notification_enabled);
422 if (le_notification_enabled){
423 att_server_request_can_send_now_event(le_connection_handle);
424 }
425
426 // disable page/inquiry scan to get max performance
427 gap_discoverable_control(0);
428 gap_connectable_control(0);
429
430 test_reset();
431 break;
432 default:
433 break;
434 }
435 return 0;
436 }
437
438 /*
439 * @section Main Application Setup
440 *
441 * @text As with the packet and the heartbeat handlers, the combined app setup contains the code from the individual example setups.
442 */
443
444
445 /* LISTING_START(MainConfiguration): Init L2CAP RFCOMM SDO SM ATT Server and start heartbeat timer */
btstack_main(int argc,const char * argv[])446 int btstack_main(int argc, const char * argv[])
447 {
448 UNUSED(argc);
449 (void)argv;
450
451 l2cap_init();
452
453 rfcomm_init();
454 rfcomm_register_service(rfcomm_packet_handler, RFCOMM_SERVER_CHANNEL, 0xffff);
455
456 // init SDP, create record for SPP and register with SDP
457 sdp_init();
458 memset(spp_service_buffer, 0, sizeof(spp_service_buffer));
459 spp_create_sdp_record(spp_service_buffer, sdp_create_service_record_handle(), RFCOMM_SERVER_CHANNEL, "SPP Streamer");
460 btstack_assert(de_get_len( spp_service_buffer) <= sizeof(spp_service_buffer));
461 sdp_register_service(spp_service_buffer);
462
463 #ifdef ENABLE_GATT_OVER_CLASSIC
464 // init SDP, create record for GATT and register with SDP
465 memset(gatt_service_buffer, 0, sizeof(gatt_service_buffer));
466 gatt_create_sdp_record(gatt_service_buffer, sdp_create_service_record_handle(), ATT_SERVICE_GATT_SERVICE_START_HANDLE, ATT_SERVICE_GATT_SERVICE_END_HANDLE);
467 btstack_assert(de_get_len( gatt_service_buffer) <= sizeof(gatt_service_buffer));
468 sdp_register_service(gatt_service_buffer);
469 #endif
470
471 gap_set_local_name("SPP and LE Streamer 00:00:00:00:00:00");
472 gap_ssp_set_io_capability(SSP_IO_CAPABILITY_DISPLAY_YES_NO);
473
474 // short-cut to find other SPP Streamer
475 gap_set_class_of_device(TEST_COD);
476
477 gap_discoverable_control(1);
478
479 // setup SM: Display only
480 sm_init();
481
482 // setup ATT server
483 att_server_init(profile_data, att_read_callback, att_write_callback);
484
485 // register for HCI events
486 hci_event_callback_registration.callback = &hci_packet_handler;
487 hci_add_event_handler(&hci_event_callback_registration);
488
489 // register for ATT events
490 att_server_register_packet_handler(att_packet_handler);
491
492 // setup advertisements
493 uint16_t adv_int_min = 0x0030;
494 uint16_t adv_int_max = 0x0030;
495 uint8_t adv_type = 0;
496 bd_addr_t null_addr;
497 memset(null_addr, 0, 6);
498 gap_advertisements_set_params(adv_int_min, adv_int_max, adv_type, 0, null_addr, 0x07, 0x00);
499 gap_advertisements_set_data(adv_data_len, (uint8_t*) adv_data);
500 gap_advertisements_enable(1);
501
502 spp_create_test_data();
503
504 // turn on!
505 hci_power_control(HCI_POWER_ON);
506
507 return 0;
508 }
509 /* LISTING_END */
510 /* EXAMPLE_END */
511