xref: /btstack/example/spp_and_gatt_streamer.c (revision 762141afa59a4094329f828b2e84ec60cf9c372c)
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 
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 
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 
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 
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 
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 
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 
224bdc352b1SMatthias Ringwald                 case HCI_EVENT_LE_META:
225bdc352b1SMatthias Ringwald                     switch (hci_event_le_meta_get_subevent_code(packet)) {
226bdc352b1SMatthias Ringwald                         case HCI_SUBEVENT_LE_CONNECTION_COMPLETE:
227bdc352b1SMatthias Ringwald                             // print connection parameters (without using float operations)
228bdc352b1SMatthias Ringwald                             con_handle    = hci_subevent_le_connection_complete_get_connection_handle(packet);
229bdc352b1SMatthias Ringwald                             conn_interval = hci_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));
231bdc352b1SMatthias Ringwald                             printf("LE Connection - Connection Latency: %u\n", hci_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;
237bdc352b1SMatthias Ringwald 
238bdc352b1SMatthias Ringwald                         case HCI_SUBEVENT_LE_CONNECTION_UPDATE_COMPLETE:
239bdc352b1SMatthias Ringwald                             // print connection parameters (without using float operations)
240bdc352b1SMatthias Ringwald                             con_handle    = hci_subevent_le_connection_update_complete_get_connection_handle(packet);
241bdc352b1SMatthias Ringwald                             conn_interval = hci_subevent_le_connection_update_complete_get_conn_interval(packet);
242bdc352b1SMatthias Ringwald                             printf("LE Connection - Connection Param update - connection interval %u.%02u ms, latency %u\n", conn_interval * 125 / 100,
243bdc352b1SMatthias Ringwald                                 25 * (conn_interval & 3), hci_subevent_le_connection_update_complete_get_conn_latency(packet));
244bdc352b1SMatthias Ringwald                             break;
245bdc352b1SMatthias Ringwald 
246bdc352b1SMatthias Ringwald                         default:
247bdc352b1SMatthias Ringwald                             break;
248bdc352b1SMatthias Ringwald                     }
249bdc352b1SMatthias Ringwald                     break;
250bdc352b1SMatthias Ringwald 
251bdc352b1SMatthias Ringwald                 case HCI_EVENT_DISCONNECTION_COMPLETE:
252bdc352b1SMatthias Ringwald                     // re-enable page/inquiry scan again
253bdc352b1SMatthias Ringwald                     gap_discoverable_control(1);
254bdc352b1SMatthias Ringwald                     gap_connectable_control(1);
255bdc352b1SMatthias Ringwald                     // re-enable advertisements
256bdc352b1SMatthias Ringwald                     gap_advertisements_enable(1);
257bdc352b1SMatthias Ringwald                     le_notification_enabled = 0;
258bdc352b1SMatthias Ringwald                     break;
259bdc352b1SMatthias Ringwald 
260bdc352b1SMatthias Ringwald                 default:
261bdc352b1SMatthias Ringwald                     break;
262bdc352b1SMatthias Ringwald             }
263bdc352b1SMatthias Ringwald             break;
264bdc352b1SMatthias Ringwald 
265bdc352b1SMatthias Ringwald         default:
266bdc352b1SMatthias Ringwald             break;
267bdc352b1SMatthias Ringwald     }
268bdc352b1SMatthias Ringwald }
269bdc352b1SMatthias Ringwald 
270bdc352b1SMatthias Ringwald /*
271bdc352b1SMatthias Ringwald  * @section RFCOMM Packet Handler
272bdc352b1SMatthias Ringwald  *
273bdc352b1SMatthias Ringwald  * @text The RFCOMM packet handler accepts incoming connection and triggers sending of RFCOMM data on RFCOMM_EVENT_CAN_SEND_NOW
274bdc352b1SMatthias Ringwald  */
275bdc352b1SMatthias Ringwald 
276bdc352b1SMatthias Ringwald static void rfcomm_packet_handler (uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
277bdc352b1SMatthias Ringwald     UNUSED(channel);
278bdc352b1SMatthias Ringwald 
279bdc352b1SMatthias Ringwald     bd_addr_t event_addr;
280bdc352b1SMatthias Ringwald     uint8_t   rfcomm_channel_nr;
281bdc352b1SMatthias Ringwald 
282bdc352b1SMatthias Ringwald     switch (packet_type) {
283bdc352b1SMatthias Ringwald         case HCI_EVENT_PACKET:
284bdc352b1SMatthias Ringwald             switch (hci_event_packet_get_type(packet)) {
285bdc352b1SMatthias Ringwald 
286bdc352b1SMatthias Ringwald                 case RFCOMM_EVENT_INCOMING_CONNECTION:
287bdc352b1SMatthias Ringwald                     // data: event (8), len(8), address(48), channel (8), rfcomm_cid (16)
288bdc352b1SMatthias Ringwald                     rfcomm_event_incoming_connection_get_bd_addr(packet, event_addr);
289bdc352b1SMatthias Ringwald                     rfcomm_channel_nr = rfcomm_event_incoming_connection_get_server_channel(packet);
290bdc352b1SMatthias Ringwald                     rfcomm_cid = rfcomm_event_incoming_connection_get_rfcomm_cid(packet);
291bdc352b1SMatthias Ringwald                     printf("RFCOMM channel %u requested for %s\n", rfcomm_channel_nr, bd_addr_to_str(event_addr));
292bdc352b1SMatthias Ringwald                     rfcomm_accept_connection(rfcomm_cid);
293bdc352b1SMatthias Ringwald                     break;
294bdc352b1SMatthias Ringwald 
295bdc352b1SMatthias Ringwald                 case RFCOMM_EVENT_CHANNEL_OPENED:
296bdc352b1SMatthias Ringwald                     // data: event(8), len(8), status (8), address (48), server channel(8), rfcomm_cid(16), max frame size(16)
297bdc352b1SMatthias Ringwald                     if (rfcomm_event_channel_opened_get_status(packet)) {
298fcd55a0bSMilanka Ringwald                         printf("RFCOMM channel open failed, status 0x%02x\n", rfcomm_event_channel_opened_get_status(packet));
299bdc352b1SMatthias Ringwald                     } else {
300bdc352b1SMatthias Ringwald                         rfcomm_cid = rfcomm_event_channel_opened_get_rfcomm_cid(packet);
301bdc352b1SMatthias Ringwald                         rfcomm_mtu = rfcomm_event_channel_opened_get_max_frame_size(packet);
302bdc352b1SMatthias Ringwald                         printf("RFCOMM channel open succeeded. New RFCOMM Channel ID %u, max frame size %u\n", rfcomm_cid, rfcomm_mtu);
303bdc352b1SMatthias Ringwald 
304bdc352b1SMatthias Ringwald                         spp_test_data_len = rfcomm_mtu;
305bdc352b1SMatthias Ringwald                         if (spp_test_data_len > sizeof(test_data)){
306bdc352b1SMatthias Ringwald                             spp_test_data_len = sizeof(test_data);
307bdc352b1SMatthias Ringwald                         }
308bdc352b1SMatthias Ringwald 
309bdc352b1SMatthias Ringwald                         // disable page/inquiry scan to get max performance
310bdc352b1SMatthias Ringwald                         gap_discoverable_control(0);
311bdc352b1SMatthias Ringwald                         gap_connectable_control(0);
312bdc352b1SMatthias Ringwald                         // disable advertisements
313bdc352b1SMatthias Ringwald                         gap_advertisements_enable(0);
314bdc352b1SMatthias Ringwald 
315bdc352b1SMatthias Ringwald                         test_reset();
316bdc352b1SMatthias Ringwald                         rfcomm_request_can_send_now_event(rfcomm_cid);
317bdc352b1SMatthias Ringwald                     }
318bdc352b1SMatthias Ringwald                     break;
319bdc352b1SMatthias Ringwald 
320bdc352b1SMatthias Ringwald                 case RFCOMM_EVENT_CAN_SEND_NOW:
321bdc352b1SMatthias Ringwald                     spp_send_packet();
322bdc352b1SMatthias Ringwald                     break;
323bdc352b1SMatthias Ringwald 
324bdc352b1SMatthias Ringwald                 case RFCOMM_EVENT_CHANNEL_CLOSED:
325bdc352b1SMatthias Ringwald                     printf("RFCOMM channel closed\n");
326bdc352b1SMatthias Ringwald                     rfcomm_cid = 0;
327bdc352b1SMatthias Ringwald                     break;
328bdc352b1SMatthias Ringwald 
329bdc352b1SMatthias Ringwald                 default:
330bdc352b1SMatthias Ringwald                     break;
331bdc352b1SMatthias Ringwald             }
332bdc352b1SMatthias Ringwald             break;
333bdc352b1SMatthias Ringwald 
334bdc352b1SMatthias Ringwald         case RFCOMM_DATA_PACKET:
335bdc352b1SMatthias Ringwald             test_track_transferred(size);
336bdc352b1SMatthias Ringwald #if 0
337bdc352b1SMatthias Ringwald             printf("RCV: '");
338bdc352b1SMatthias Ringwald             for (i=0;i<size;i++){
339bdc352b1SMatthias Ringwald                 putchar(packet[i]);
340bdc352b1SMatthias Ringwald             }
341bdc352b1SMatthias Ringwald             printf("'\n");
342bdc352b1SMatthias Ringwald #endif
343bdc352b1SMatthias Ringwald             break;
344bdc352b1SMatthias Ringwald 
345bdc352b1SMatthias Ringwald         default:
346bdc352b1SMatthias Ringwald             break;
347bdc352b1SMatthias Ringwald     }
348bdc352b1SMatthias Ringwald }
349bdc352b1SMatthias Ringwald 
350bdc352b1SMatthias Ringwald /*
351bdc352b1SMatthias Ringwald  * @section ATT Packet Handler
352bdc352b1SMatthias Ringwald  *
353bdc352b1SMatthias Ringwald  * @text The packet handler is used to track the ATT MTU Exchange and trigger ATT send
354bdc352b1SMatthias Ringwald  */
355bdc352b1SMatthias Ringwald 
356bdc352b1SMatthias Ringwald /* LISTING_START(attPacketHandler): Packet Handler */
357bdc352b1SMatthias Ringwald static void att_packet_handler (uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
358bdc352b1SMatthias Ringwald     UNUSED(channel);
359bdc352b1SMatthias Ringwald     UNUSED(size);
360bdc352b1SMatthias Ringwald 
3617bbeb3adSMilanka Ringwald     if (packet_type != HCI_EVENT_PACKET) return;
3627bbeb3adSMilanka Ringwald 
363bdc352b1SMatthias Ringwald     switch (hci_event_packet_get_type(packet)) {
364bdc352b1SMatthias Ringwald         case ATT_EVENT_CONNECTED:
365bdc352b1SMatthias Ringwald             le_connection_handle = att_event_connected_get_handle(packet);
366bdc352b1SMatthias Ringwald             att_mtu = att_server_get_mtu(le_connection_handle);
367bdc352b1SMatthias Ringwald             le_test_data_len = btstack_min(att_server_get_mtu(le_connection_handle) - 3, sizeof(test_data));
368bdc352b1SMatthias Ringwald             printf("ATT MTU = %u\n", att_mtu);
369bdc352b1SMatthias Ringwald             break;
370bdc352b1SMatthias Ringwald 
371bdc352b1SMatthias Ringwald         case ATT_EVENT_MTU_EXCHANGE_COMPLETE:
372bdc352b1SMatthias Ringwald             att_mtu = att_event_mtu_exchange_complete_get_MTU(packet);
373bdc352b1SMatthias Ringwald             le_test_data_len = btstack_min(att_mtu - 3, sizeof(test_data));
374bdc352b1SMatthias Ringwald             printf("ATT MTU = %u\n", att_mtu);
375bdc352b1SMatthias Ringwald             break;
376bdc352b1SMatthias Ringwald 
377bdc352b1SMatthias Ringwald         case ATT_EVENT_CAN_SEND_NOW:
378bdc352b1SMatthias Ringwald             le_streamer();
379bdc352b1SMatthias Ringwald             break;
380bdc352b1SMatthias Ringwald 
381bdc352b1SMatthias Ringwald         case ATT_EVENT_DISCONNECTED:
382bdc352b1SMatthias Ringwald             le_notification_enabled = 0;
383bdc352b1SMatthias Ringwald             le_connection_handle = HCI_CON_HANDLE_INVALID;
384bdc352b1SMatthias Ringwald             break;
3857bbeb3adSMilanka Ringwald 
3867bbeb3adSMilanka Ringwald         default:
3877bbeb3adSMilanka Ringwald             break;
388bdc352b1SMatthias Ringwald     }
389bdc352b1SMatthias Ringwald }
390bdc352b1SMatthias Ringwald 
391bdc352b1SMatthias Ringwald // ATT Client Read Callback for Dynamic Data
392bdc352b1SMatthias Ringwald // - if buffer == NULL, don't copy data, just return size of value
393bdc352b1SMatthias Ringwald // - if buffer != NULL, copy data and return number bytes copied
394bdc352b1SMatthias Ringwald // @param offset defines start of attribute value
395bdc352b1SMatthias 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){
396bdc352b1SMatthias Ringwald     UNUSED(con_handle);
397bdc352b1SMatthias Ringwald     UNUSED(att_handle);
398bdc352b1SMatthias Ringwald     UNUSED(offset);
399bdc352b1SMatthias Ringwald     UNUSED(buffer);
400bdc352b1SMatthias Ringwald     UNUSED(buffer_size);
401bdc352b1SMatthias Ringwald     return 0;
402bdc352b1SMatthias Ringwald }
403bdc352b1SMatthias Ringwald 
404bdc352b1SMatthias Ringwald // write requests
405bdc352b1SMatthias 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){
406bdc352b1SMatthias Ringwald     UNUSED(con_handle);
407bdc352b1SMatthias Ringwald     UNUSED(offset);
408bdc352b1SMatthias Ringwald     UNUSED(buffer_size);
409bdc352b1SMatthias Ringwald 
410bdc352b1SMatthias Ringwald     // printf("att_write_callback att_handle %04x, transaction mode %u\n", att_handle, transaction_mode);
411bdc352b1SMatthias Ringwald     if (transaction_mode != ATT_TRANSACTION_MODE_NONE) return 0;
412bdc352b1SMatthias Ringwald     switch(att_handle){
413bdc352b1SMatthias Ringwald         case ATT_CHARACTERISTIC_0000FF11_0000_1000_8000_00805F9B34FB_01_CLIENT_CONFIGURATION_HANDLE:
414bdc352b1SMatthias Ringwald             le_notification_enabled = little_endian_read_16(buffer, 0) == GATT_CLIENT_CHARACTERISTICS_CONFIGURATION_NOTIFICATION;
415bdc352b1SMatthias Ringwald             printf("Notifications enabled %u\n", le_notification_enabled);
416bdc352b1SMatthias Ringwald             if (le_notification_enabled){
417bdc352b1SMatthias Ringwald                 att_server_request_can_send_now_event(le_connection_handle);
418bdc352b1SMatthias Ringwald             }
419bdc352b1SMatthias Ringwald 
420bdc352b1SMatthias Ringwald             // disable page/inquiry scan to get max performance
421bdc352b1SMatthias Ringwald             gap_discoverable_control(0);
422bdc352b1SMatthias Ringwald             gap_connectable_control(0);
423bdc352b1SMatthias Ringwald 
424bdc352b1SMatthias Ringwald             test_reset();
425bdc352b1SMatthias Ringwald             break;
426bdc352b1SMatthias Ringwald         default:
427bdc352b1SMatthias Ringwald             break;
428bdc352b1SMatthias Ringwald     }
429bdc352b1SMatthias Ringwald     return 0;
430bdc352b1SMatthias Ringwald }
431bdc352b1SMatthias Ringwald 
432bdc352b1SMatthias Ringwald /*
433bdc352b1SMatthias Ringwald  * @section Main Application Setup
434bdc352b1SMatthias Ringwald  *
435bdc352b1SMatthias Ringwald  * @text As with the packet and the heartbeat handlers, the combined app setup contains the code from the individual example setups.
436bdc352b1SMatthias Ringwald  */
437bdc352b1SMatthias Ringwald 
438bdc352b1SMatthias Ringwald 
439bdc352b1SMatthias Ringwald /* LISTING_START(MainConfiguration): Init L2CAP RFCOMM SDO SM ATT Server and start heartbeat timer */
440bdc352b1SMatthias Ringwald int btstack_main(int argc, const char * argv[])
441bdc352b1SMatthias Ringwald {
442bdc352b1SMatthias Ringwald     UNUSED(argc);
443bdc352b1SMatthias Ringwald     (void)argv;
444bdc352b1SMatthias Ringwald 
445bdc352b1SMatthias Ringwald     l2cap_init();
446bdc352b1SMatthias Ringwald 
447bdc352b1SMatthias Ringwald     rfcomm_init();
448bdc352b1SMatthias Ringwald     rfcomm_register_service(rfcomm_packet_handler, RFCOMM_SERVER_CHANNEL, 0xffff);
449bdc352b1SMatthias Ringwald 
450bdc352b1SMatthias Ringwald     // init SDP, create record for SPP and register with SDP
451bdc352b1SMatthias Ringwald     sdp_init();
452bdc352b1SMatthias Ringwald     memset(spp_service_buffer, 0, sizeof(spp_service_buffer));
453*762141afSMatthias Ringwald     spp_create_sdp_record(spp_service_buffer, sdp_create_service_record_handle(), RFCOMM_SERVER_CHANNEL, "SPP Streamer");
454*762141afSMatthias Ringwald     btstack_assert(de_get_len( spp_service_buffer) <= sizeof(spp_service_buffer));
455bdc352b1SMatthias Ringwald     sdp_register_service(spp_service_buffer);
456bdc352b1SMatthias Ringwald 
457bdc352b1SMatthias Ringwald #ifdef ENABLE_GATT_OVER_CLASSIC
458bdc352b1SMatthias Ringwald     // init SDP, create record for GATT and register with SDP
459bdc352b1SMatthias Ringwald     memset(gatt_service_buffer, 0, sizeof(gatt_service_buffer));
460*762141afSMatthias 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);
461*762141afSMatthias Ringwald     btstack_assert(de_get_len( gatt_service_buffer) <= sizeof(gatt_service_buffer));
462bdc352b1SMatthias Ringwald     sdp_register_service(gatt_service_buffer);
463bdc352b1SMatthias Ringwald #endif
464bdc352b1SMatthias Ringwald 
465bdc352b1SMatthias Ringwald     gap_set_local_name("SPP and LE Streamer 00:00:00:00:00:00");
466bdc352b1SMatthias Ringwald     gap_ssp_set_io_capability(SSP_IO_CAPABILITY_DISPLAY_YES_NO);
467bdc352b1SMatthias Ringwald 
468bdc352b1SMatthias Ringwald     // short-cut to find other SPP Streamer
469bdc352b1SMatthias Ringwald     gap_set_class_of_device(TEST_COD);
470bdc352b1SMatthias Ringwald 
471bdc352b1SMatthias Ringwald     gap_discoverable_control(1);
472bdc352b1SMatthias Ringwald 
473bdc352b1SMatthias Ringwald     // setup SM: Display only
474bdc352b1SMatthias Ringwald     sm_init();
475bdc352b1SMatthias Ringwald 
476bdc352b1SMatthias Ringwald     // setup ATT server
477bdc352b1SMatthias Ringwald     att_server_init(profile_data, att_read_callback, att_write_callback);
478bdc352b1SMatthias Ringwald 
479bdc352b1SMatthias Ringwald     // register for HCI events
480bdc352b1SMatthias Ringwald     hci_event_callback_registration.callback = &hci_packet_handler;
481bdc352b1SMatthias Ringwald     hci_add_event_handler(&hci_event_callback_registration);
482bdc352b1SMatthias Ringwald 
483bdc352b1SMatthias Ringwald     // register for ATT events
484bdc352b1SMatthias Ringwald     att_server_register_packet_handler(att_packet_handler);
485bdc352b1SMatthias Ringwald 
486bdc352b1SMatthias Ringwald     // setup advertisements
487bdc352b1SMatthias Ringwald     uint16_t adv_int_min = 0x0030;
488bdc352b1SMatthias Ringwald     uint16_t adv_int_max = 0x0030;
489bdc352b1SMatthias Ringwald     uint8_t adv_type = 0;
490bdc352b1SMatthias Ringwald     bd_addr_t null_addr;
491bdc352b1SMatthias Ringwald     memset(null_addr, 0, 6);
492bdc352b1SMatthias Ringwald     gap_advertisements_set_params(adv_int_min, adv_int_max, adv_type, 0, null_addr, 0x07, 0x00);
493bdc352b1SMatthias Ringwald     gap_advertisements_set_data(adv_data_len, (uint8_t*) adv_data);
494bdc352b1SMatthias Ringwald     gap_advertisements_enable(1);
495bdc352b1SMatthias Ringwald 
496bdc352b1SMatthias Ringwald     spp_create_test_data();
497bdc352b1SMatthias Ringwald 
498bdc352b1SMatthias Ringwald     // turn on!
499bdc352b1SMatthias Ringwald     hci_power_control(HCI_POWER_ON);
500bdc352b1SMatthias Ringwald 
501bdc352b1SMatthias Ringwald     return 0;
502bdc352b1SMatthias Ringwald }
503bdc352b1SMatthias Ringwald /* LISTING_END */
504bdc352b1SMatthias Ringwald /* EXAMPLE_END */
505