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