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