xref: /btstack/example/gatt_streamer_server.c (revision 6058cb0da77e05ebe2be82d7f052d1d5a49d0546)
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
23bdc352b1SMatthias Ringwald  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL MATTHIAS
24bdc352b1SMatthias Ringwald  * RINGWALD 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 
38ec8ae085SMilanka Ringwald #define BTSTACK_FILE__ "gatt_streamer_server.c"
39bdc352b1SMatthias Ringwald 
40bdc352b1SMatthias Ringwald // *****************************************************************************
41ec8ae085SMilanka Ringwald /* EXAMPLE_START(gatt_streamer_server): Performance - Stream Data over GATT (Server)
42bdc352b1SMatthias Ringwald  *
43bdc352b1SMatthias Ringwald  * @text All newer operating systems provide GATT Client functionality.
44bdc352b1SMatthias Ringwald  * This example shows how to get a maximal throughput via BLE:
45bdc352b1SMatthias Ringwald  * - send whenever possible,
46bdc352b1SMatthias Ringwald  * - use the max ATT MTU.
47bdc352b1SMatthias Ringwald  *
48bdc352b1SMatthias Ringwald  * @text In theory, we should also update the connection parameters, but we already get
49bdc352b1SMatthias Ringwald  * a connection interval of 30 ms and there's no public way to use a shorter
50bdc352b1SMatthias Ringwald  * interval with iOS (if we're not implementing an HID device).
51bdc352b1SMatthias Ringwald  *
52bdc352b1SMatthias Ringwald  * @text Note: To start the streaming, run the example.
53bdc352b1SMatthias Ringwald  * On remote device use some GATT Explorer, e.g. LightBlue, BLExplr to enable notifications.
54bdc352b1SMatthias Ringwald  */
55bdc352b1SMatthias Ringwald  // *****************************************************************************
56bdc352b1SMatthias Ringwald 
57bdc352b1SMatthias Ringwald #include <inttypes.h>
58bdc352b1SMatthias Ringwald #include <stdint.h>
59bdc352b1SMatthias Ringwald #include <stdio.h>
60bdc352b1SMatthias Ringwald #include <stdlib.h>
61bdc352b1SMatthias Ringwald #include <string.h>
62bdc352b1SMatthias Ringwald 
63bdc352b1SMatthias Ringwald #include "btstack.h"
64bdc352b1SMatthias Ringwald 
65bdc352b1SMatthias Ringwald // le_streamer.gatt contains the declaration of the provided GATT Services + Characteristics
66bdc352b1SMatthias Ringwald // le_streamer.h    contains the binary representation of le_streamer.gatt
67bdc352b1SMatthias Ringwald // it is generated by the build system by calling: $BTSTACK_ROOT/tool/compile_gatt.py le_streamer.gatt le_streamer.h
68bdc352b1SMatthias Ringwald // it needs to be regenerated when the GATT Database declared in le_streamer.gatt file is modified
69bdc352b1SMatthias Ringwald #include "gatt_streamer_server.h"
70bdc352b1SMatthias Ringwald 
71bdc352b1SMatthias Ringwald #define REPORT_INTERVAL_MS 3000
72bdc352b1SMatthias Ringwald #define MAX_NR_CONNECTIONS 3
73bdc352b1SMatthias Ringwald 
74bdc352b1SMatthias Ringwald 
75bdc352b1SMatthias Ringwald static void  hci_packet_handler (uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size);
76bdc352b1SMatthias Ringwald static void  att_packet_handler (uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size);
77bdc352b1SMatthias 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);
78bdc352b1SMatthias Ringwald static void  streamer(void);
79bdc352b1SMatthias Ringwald 
80bdc352b1SMatthias Ringwald const uint8_t adv_data[] = {
81bdc352b1SMatthias Ringwald     // Flags general discoverable, BR/EDR not supported
82bdc352b1SMatthias Ringwald     0x02, BLUETOOTH_DATA_TYPE_FLAGS, 0x06,
83bdc352b1SMatthias Ringwald     // Name
84bdc352b1SMatthias Ringwald     0x0c, BLUETOOTH_DATA_TYPE_COMPLETE_LOCAL_NAME, 'L', 'E', ' ', 'S', 't', 'r', 'e', 'a', 'm', 'e', 'r',
85bdc352b1SMatthias Ringwald     // Incomplete List of 16-bit Service Class UUIDs -- FF10 - only valid for testing!
86bdc352b1SMatthias Ringwald     0x03, BLUETOOTH_DATA_TYPE_INCOMPLETE_LIST_OF_16_BIT_SERVICE_CLASS_UUIDS, 0x10, 0xff,
87bdc352b1SMatthias Ringwald };
88bdc352b1SMatthias Ringwald const uint8_t adv_data_len = sizeof(adv_data);
89bdc352b1SMatthias Ringwald 
90bdc352b1SMatthias Ringwald static btstack_packet_callback_registration_t hci_event_callback_registration;
91bdc352b1SMatthias Ringwald 
92bdc352b1SMatthias Ringwald // support for multiple clients
93bdc352b1SMatthias Ringwald typedef struct {
94bdc352b1SMatthias Ringwald     char name;
95bdc352b1SMatthias Ringwald     int le_notification_enabled;
96bdc352b1SMatthias Ringwald     uint16_t value_handle;
97bdc352b1SMatthias Ringwald     hci_con_handle_t connection_handle;
98bdc352b1SMatthias Ringwald     int  counter;
99bdc352b1SMatthias Ringwald     char test_data[200];
100bdc352b1SMatthias Ringwald     int  test_data_len;
101bdc352b1SMatthias Ringwald     uint32_t test_data_sent;
102bdc352b1SMatthias Ringwald     uint32_t test_data_start;
103bdc352b1SMatthias Ringwald } le_streamer_connection_t;
104bdc352b1SMatthias Ringwald static le_streamer_connection_t le_streamer_connections[MAX_NR_CONNECTIONS];
105bdc352b1SMatthias Ringwald 
106bdc352b1SMatthias Ringwald // round robin sending
107bdc352b1SMatthias Ringwald static int connection_index;
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 static void init_connections(void){
114bdc352b1SMatthias Ringwald     // track connections
115bdc352b1SMatthias Ringwald     int i;
116bdc352b1SMatthias Ringwald     for (i=0;i<MAX_NR_CONNECTIONS;i++){
117bdc352b1SMatthias Ringwald         le_streamer_connections[i].connection_handle = HCI_CON_HANDLE_INVALID;
118bdc352b1SMatthias Ringwald         le_streamer_connections[i].name = 'A' + i;
119bdc352b1SMatthias Ringwald     }
120bdc352b1SMatthias Ringwald }
121bdc352b1SMatthias Ringwald 
122bdc352b1SMatthias Ringwald static le_streamer_connection_t * connection_for_conn_handle(hci_con_handle_t conn_handle){
123bdc352b1SMatthias Ringwald     int i;
124bdc352b1SMatthias Ringwald     for (i=0;i<MAX_NR_CONNECTIONS;i++){
125bdc352b1SMatthias Ringwald         if (le_streamer_connections[i].connection_handle == conn_handle) return &le_streamer_connections[i];
126bdc352b1SMatthias Ringwald     }
127bdc352b1SMatthias Ringwald     return NULL;
128bdc352b1SMatthias Ringwald }
129bdc352b1SMatthias Ringwald 
130bdc352b1SMatthias Ringwald static void next_connection_index(void){
131bdc352b1SMatthias Ringwald     connection_index++;
132bdc352b1SMatthias Ringwald     if (connection_index == MAX_NR_CONNECTIONS){
133bdc352b1SMatthias Ringwald         connection_index = 0;
134bdc352b1SMatthias Ringwald     }
135bdc352b1SMatthias Ringwald }
136bdc352b1SMatthias Ringwald 
137bdc352b1SMatthias Ringwald /* @section Main Application Setup
138bdc352b1SMatthias Ringwald  *
139bdc352b1SMatthias Ringwald  * @text Listing MainConfiguration shows main application code.
140bdc352b1SMatthias Ringwald  * It initializes L2CAP, the Security Manager, and configures the ATT Server with the pre-compiled
141bdc352b1SMatthias Ringwald  * ATT Database generated from $le_streamer.gatt$. Finally, it configures the advertisements
142bdc352b1SMatthias Ringwald  * and boots the Bluetooth stack.
143bdc352b1SMatthias Ringwald  */
144bdc352b1SMatthias Ringwald 
145bdc352b1SMatthias Ringwald /* LISTING_START(MainConfiguration): Init L2CAP, SM, ATT Server, and enable advertisements */
146bdc352b1SMatthias Ringwald 
147bdc352b1SMatthias Ringwald static void le_streamer_setup(void){
148bdc352b1SMatthias Ringwald 
149bdc352b1SMatthias Ringwald     l2cap_init();
150bdc352b1SMatthias Ringwald 
151bdc352b1SMatthias Ringwald     // setup le device db
152bdc352b1SMatthias Ringwald     le_device_db_init();
153bdc352b1SMatthias Ringwald 
154bdc352b1SMatthias Ringwald     // setup SM: Display only
155bdc352b1SMatthias Ringwald     sm_init();
156bdc352b1SMatthias Ringwald 
157bdc352b1SMatthias Ringwald #ifdef ENABLE_GATT_OVER_CLASSIC
158bdc352b1SMatthias Ringwald     // init SDP, create record for GATT and register with SDP
159bdc352b1SMatthias Ringwald     sdp_init();
160bdc352b1SMatthias Ringwald     memset(gatt_service_buffer, 0, sizeof(gatt_service_buffer));
161bdc352b1SMatthias Ringwald     gatt_create_sdp_record(gatt_service_buffer, 0x10001, ATT_SERVICE_GATT_SERVICE_START_HANDLE, ATT_SERVICE_GATT_SERVICE_END_HANDLE);
162bdc352b1SMatthias Ringwald     sdp_register_service(gatt_service_buffer);
163bdc352b1SMatthias Ringwald     printf("SDP service record size: %u\n", de_get_len(gatt_service_buffer));
164bdc352b1SMatthias Ringwald 
165bdc352b1SMatthias Ringwald     // configure Classic GAP
166bdc352b1SMatthias Ringwald     gap_set_local_name("GATT Streamer BR/EDR 00:00:00:00:00:00");
167bdc352b1SMatthias Ringwald     gap_ssp_set_io_capability(SSP_IO_CAPABILITY_DISPLAY_YES_NO);
168bdc352b1SMatthias Ringwald     gap_discoverable_control(1);
169bdc352b1SMatthias Ringwald #endif
170bdc352b1SMatthias Ringwald 
171bdc352b1SMatthias Ringwald     // setup ATT server
172bdc352b1SMatthias Ringwald     att_server_init(profile_data, NULL, att_write_callback);
173bdc352b1SMatthias Ringwald 
174bdc352b1SMatthias Ringwald     // register for HCI events
175bdc352b1SMatthias Ringwald     hci_event_callback_registration.callback = &hci_packet_handler;
176bdc352b1SMatthias Ringwald     hci_add_event_handler(&hci_event_callback_registration);
177bdc352b1SMatthias Ringwald 
178bdc352b1SMatthias Ringwald     // register for ATT events
179bdc352b1SMatthias Ringwald     att_server_register_packet_handler(att_packet_handler);
180bdc352b1SMatthias Ringwald 
181bdc352b1SMatthias Ringwald     // setup advertisements
182bdc352b1SMatthias Ringwald     uint16_t adv_int_min = 0x0030;
183bdc352b1SMatthias Ringwald     uint16_t adv_int_max = 0x0030;
184bdc352b1SMatthias Ringwald     uint8_t adv_type = 0;
185bdc352b1SMatthias Ringwald     bd_addr_t null_addr;
186bdc352b1SMatthias Ringwald     memset(null_addr, 0, 6);
187bdc352b1SMatthias Ringwald     gap_advertisements_set_params(adv_int_min, adv_int_max, adv_type, 0, null_addr, 0x07, 0x00);
188bdc352b1SMatthias Ringwald     gap_advertisements_set_data(adv_data_len, (uint8_t*) adv_data);
189bdc352b1SMatthias Ringwald     gap_advertisements_enable(1);
190bdc352b1SMatthias Ringwald 
191bdc352b1SMatthias Ringwald     // init client state
192bdc352b1SMatthias Ringwald     init_connections();
193bdc352b1SMatthias Ringwald }
194bdc352b1SMatthias Ringwald /* LISTING_END */
195bdc352b1SMatthias Ringwald 
196bdc352b1SMatthias Ringwald /*
197bdc352b1SMatthias Ringwald  * @section Track throughput
198bdc352b1SMatthias Ringwald  * @text We calculate the throughput by setting a start time and measuring the amount of
199bdc352b1SMatthias Ringwald  * data sent. After a configurable REPORT_INTERVAL_MS, we print the throughput in kB/s
200bdc352b1SMatthias Ringwald  * and reset the counter and start time.
201bdc352b1SMatthias Ringwald  */
202bdc352b1SMatthias Ringwald 
203bdc352b1SMatthias Ringwald /* LISTING_START(tracking): Tracking throughput */
204bdc352b1SMatthias Ringwald 
205bdc352b1SMatthias Ringwald static void test_reset(le_streamer_connection_t * context){
206bdc352b1SMatthias Ringwald     context->test_data_start = btstack_run_loop_get_time_ms();
207bdc352b1SMatthias Ringwald     context->test_data_sent = 0;
208bdc352b1SMatthias Ringwald }
209bdc352b1SMatthias Ringwald 
210bdc352b1SMatthias Ringwald static void test_track_sent(le_streamer_connection_t * context, int bytes_sent){
211bdc352b1SMatthias Ringwald     context->test_data_sent += bytes_sent;
212bdc352b1SMatthias Ringwald     // evaluate
213bdc352b1SMatthias Ringwald     uint32_t now = btstack_run_loop_get_time_ms();
214bdc352b1SMatthias Ringwald     uint32_t time_passed = now - context->test_data_start;
215bdc352b1SMatthias Ringwald     if (time_passed < REPORT_INTERVAL_MS) return;
216bdc352b1SMatthias Ringwald     // print speed
217bdc352b1SMatthias Ringwald     int bytes_per_second = context->test_data_sent * 1000 / time_passed;
218bdc352b1SMatthias Ringwald     printf("%c: %"PRIu32" bytes sent-> %u.%03u kB/s\n", context->name, context->test_data_sent, bytes_per_second / 1000, bytes_per_second % 1000);
219bdc352b1SMatthias Ringwald 
220bdc352b1SMatthias Ringwald     // restart
221bdc352b1SMatthias Ringwald     context->test_data_start = now;
222bdc352b1SMatthias Ringwald     context->test_data_sent  = 0;
223bdc352b1SMatthias Ringwald }
224bdc352b1SMatthias Ringwald /* LISTING_END(tracking): Tracking throughput */
225bdc352b1SMatthias Ringwald 
226bdc352b1SMatthias Ringwald /*
227bdc352b1SMatthias Ringwald  * @section HCI Packet Handler
228bdc352b1SMatthias Ringwald  *
229bdc352b1SMatthias Ringwald  * @text The packet handler is used track incoming connections and to stop notifications on disconnect
230bdc352b1SMatthias Ringwald  * It is also a good place to request the connection parameter update as indicated
231bdc352b1SMatthias Ringwald  * in the commented code block.
232bdc352b1SMatthias Ringwald  */
233bdc352b1SMatthias Ringwald 
234bdc352b1SMatthias Ringwald /* LISTING_START(hciPacketHandler): Packet Handler */
235bdc352b1SMatthias Ringwald static void hci_packet_handler (uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
236bdc352b1SMatthias Ringwald     UNUSED(channel);
237bdc352b1SMatthias Ringwald     UNUSED(size);
238bdc352b1SMatthias Ringwald 
239bdc352b1SMatthias Ringwald     uint16_t conn_interval;
240bdc352b1SMatthias Ringwald     hci_con_handle_t con_handle;
2417bbeb3adSMilanka Ringwald 
2427bbeb3adSMilanka Ringwald     if (packet_type != HCI_EVENT_PACKET) return;
2437bbeb3adSMilanka Ringwald 
244bdc352b1SMatthias Ringwald     switch (hci_event_packet_get_type(packet)) {
245bdc352b1SMatthias Ringwald         case BTSTACK_EVENT_STATE:
246bdc352b1SMatthias Ringwald             // BTstack activated, get started
247bdc352b1SMatthias Ringwald             if (btstack_event_state_get_state(packet) == HCI_STATE_WORKING) {
248bdc352b1SMatthias Ringwald                 printf("To start the streaming, please run the le_streamer_client example on other device, or use some GATT Explorer, e.g. LightBlue, BLExplr.\n");
249bdc352b1SMatthias Ringwald             }
250bdc352b1SMatthias Ringwald             break;
251bdc352b1SMatthias Ringwald         case HCI_EVENT_DISCONNECTION_COMPLETE:
252bdc352b1SMatthias Ringwald             con_handle = hci_event_disconnection_complete_get_connection_handle(packet);
253bdc352b1SMatthias Ringwald             printf("- LE Connection %04x: disconnect, reason %02x\n", con_handle, hci_event_disconnection_complete_get_reason(packet));
254bdc352b1SMatthias Ringwald             break;
255bdc352b1SMatthias Ringwald         case HCI_EVENT_LE_META:
256bdc352b1SMatthias Ringwald             switch (hci_event_le_meta_get_subevent_code(packet)) {
257bdc352b1SMatthias Ringwald                 case HCI_SUBEVENT_LE_CONNECTION_COMPLETE:
258bdc352b1SMatthias Ringwald                     // print connection parameters (without using float operations)
259bdc352b1SMatthias Ringwald                     con_handle    = hci_subevent_le_connection_complete_get_connection_handle(packet);
260bdc352b1SMatthias Ringwald                     conn_interval = hci_subevent_le_connection_complete_get_conn_interval(packet);
261bdc352b1SMatthias Ringwald                     printf("- LE Connection %04x: connected - connection interval %u.%02u ms, latency %u\n", con_handle, conn_interval * 125 / 100,
262bdc352b1SMatthias Ringwald                         25 * (conn_interval & 3), hci_subevent_le_connection_complete_get_conn_latency(packet));
263bdc352b1SMatthias Ringwald 
264bdc352b1SMatthias Ringwald                     // request min con interval 15 ms for iOS 11+
265bdc352b1SMatthias Ringwald                     printf("- LE Connection %04x: request 15 ms connection interval\n", con_handle);
266bdc352b1SMatthias Ringwald                     gap_request_connection_parameter_update(con_handle, 12, 12, 0, 0x0048);
267bdc352b1SMatthias Ringwald                     break;
268bdc352b1SMatthias Ringwald                 case HCI_SUBEVENT_LE_CONNECTION_UPDATE_COMPLETE:
269bdc352b1SMatthias Ringwald                     // print connection parameters (without using float operations)
270bdc352b1SMatthias Ringwald                     con_handle    = hci_subevent_le_connection_update_complete_get_connection_handle(packet);
271bdc352b1SMatthias Ringwald                     conn_interval = hci_subevent_le_connection_update_complete_get_conn_interval(packet);
272bdc352b1SMatthias Ringwald                     printf("- LE Connection %04x: connection update - connection interval %u.%02u ms, latency %u\n", con_handle, conn_interval * 125 / 100,
273bdc352b1SMatthias Ringwald                         25 * (conn_interval & 3), hci_subevent_le_connection_update_complete_get_conn_latency(packet));
274bdc352b1SMatthias Ringwald                     break;
275bdc352b1SMatthias Ringwald                 default:
276bdc352b1SMatthias Ringwald                     break;
277bdc352b1SMatthias Ringwald             }
278bdc352b1SMatthias Ringwald             break;
2797bbeb3adSMilanka Ringwald 
280bdc352b1SMatthias Ringwald         default:
281bdc352b1SMatthias Ringwald             break;
282bdc352b1SMatthias Ringwald     }
283bdc352b1SMatthias Ringwald }
284bdc352b1SMatthias Ringwald 
285bdc352b1SMatthias Ringwald /* LISTING_END */
286bdc352b1SMatthias Ringwald 
287bdc352b1SMatthias Ringwald /*
288bdc352b1SMatthias Ringwald  * @section ATT Packet Handler
289bdc352b1SMatthias Ringwald  *
290bdc352b1SMatthias Ringwald  * @text The packet handler is used to track the ATT MTU Exchange and trigger ATT send
291bdc352b1SMatthias Ringwald  */
292bdc352b1SMatthias Ringwald 
293bdc352b1SMatthias Ringwald /* LISTING_START(attPacketHandler): Packet Handler */
294bdc352b1SMatthias Ringwald static void att_packet_handler (uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
295bdc352b1SMatthias Ringwald     UNUSED(channel);
296bdc352b1SMatthias Ringwald     UNUSED(size);
297bdc352b1SMatthias Ringwald 
298bdc352b1SMatthias Ringwald     int mtu;
299bdc352b1SMatthias Ringwald     le_streamer_connection_t * context;
300bdc352b1SMatthias Ringwald     switch (packet_type) {
301bdc352b1SMatthias Ringwald         case HCI_EVENT_PACKET:
302bdc352b1SMatthias Ringwald             switch (hci_event_packet_get_type(packet)) {
303bdc352b1SMatthias Ringwald                 case ATT_EVENT_CONNECTED:
304bdc352b1SMatthias Ringwald                     // setup new
305bdc352b1SMatthias Ringwald                     context = connection_for_conn_handle(HCI_CON_HANDLE_INVALID);
306bdc352b1SMatthias Ringwald                     if (!context) break;
307bdc352b1SMatthias Ringwald                     context->counter = 'A';
308bdc352b1SMatthias Ringwald                     context->connection_handle = att_event_connected_get_handle(packet);
309bdc352b1SMatthias Ringwald                     context->test_data_len = btstack_min(att_server_get_mtu(context->connection_handle) - 3, sizeof(context->test_data));
310bdc352b1SMatthias Ringwald                     printf("%c: ATT connected, handle %04x, test data len %u\n", context->name, context->connection_handle, context->test_data_len);
311bdc352b1SMatthias Ringwald                     break;
312bdc352b1SMatthias Ringwald                 case ATT_EVENT_MTU_EXCHANGE_COMPLETE:
313bdc352b1SMatthias Ringwald                     mtu = att_event_mtu_exchange_complete_get_MTU(packet) - 3;
314bdc352b1SMatthias Ringwald                     context = connection_for_conn_handle(att_event_mtu_exchange_complete_get_handle(packet));
315bdc352b1SMatthias Ringwald                     if (!context) break;
316bdc352b1SMatthias Ringwald                     context->test_data_len = btstack_min(mtu - 3, sizeof(context->test_data));
317bdc352b1SMatthias Ringwald                     printf("%c: ATT MTU = %u => use test data of len %u\n", context->name, mtu, context->test_data_len);
318bdc352b1SMatthias Ringwald                     break;
319bdc352b1SMatthias Ringwald                 case ATT_EVENT_CAN_SEND_NOW:
320bdc352b1SMatthias Ringwald                     streamer();
321bdc352b1SMatthias Ringwald                     break;
322bdc352b1SMatthias Ringwald                 case ATT_EVENT_DISCONNECTED:
323bdc352b1SMatthias Ringwald                     context = connection_for_conn_handle(att_event_disconnected_get_handle(packet));
324bdc352b1SMatthias Ringwald                     if (!context) break;
325bdc352b1SMatthias Ringwald                     // free connection
326bdc352b1SMatthias Ringwald                     printf("%c: ATT disconnected, handle %04x\n", context->name, context->connection_handle);
327bdc352b1SMatthias Ringwald                     context->le_notification_enabled = 0;
328bdc352b1SMatthias Ringwald                     context->connection_handle = HCI_CON_HANDLE_INVALID;
329bdc352b1SMatthias Ringwald                     break;
330bdc352b1SMatthias Ringwald                 default:
331bdc352b1SMatthias Ringwald                     break;
332bdc352b1SMatthias Ringwald             }
333bdc352b1SMatthias Ringwald             break;
334bdc352b1SMatthias Ringwald         default:
335bdc352b1SMatthias Ringwald             break;
336bdc352b1SMatthias Ringwald     }
337bdc352b1SMatthias Ringwald }
338bdc352b1SMatthias Ringwald /* LISTING_END */
339bdc352b1SMatthias Ringwald 
340bdc352b1SMatthias Ringwald /*
341bdc352b1SMatthias Ringwald  * @section Streamer
342bdc352b1SMatthias Ringwald  *
343bdc352b1SMatthias Ringwald  * @text The streamer function checks if notifications are enabled and if a notification can be sent now.
344bdc352b1SMatthias Ringwald  * It creates some test data - a single letter that gets increased every time - and tracks the data sent.
345bdc352b1SMatthias Ringwald  */
346bdc352b1SMatthias Ringwald 
347bdc352b1SMatthias Ringwald  /* LISTING_START(streamer): Streaming code */
348bdc352b1SMatthias Ringwald static void streamer(void){
349bdc352b1SMatthias Ringwald 
350bdc352b1SMatthias Ringwald     // find next active streaming connection
351bdc352b1SMatthias Ringwald     int old_connection_index = connection_index;
352bdc352b1SMatthias Ringwald     while (1){
353bdc352b1SMatthias Ringwald         // active found?
354bdc352b1SMatthias Ringwald         if ((le_streamer_connections[connection_index].connection_handle != HCI_CON_HANDLE_INVALID) &&
355bdc352b1SMatthias Ringwald             (le_streamer_connections[connection_index].le_notification_enabled)) break;
356bdc352b1SMatthias Ringwald 
357bdc352b1SMatthias Ringwald         // check next
358bdc352b1SMatthias Ringwald         next_connection_index();
359bdc352b1SMatthias Ringwald 
360bdc352b1SMatthias Ringwald         // none found
361bdc352b1SMatthias Ringwald         if (connection_index == old_connection_index) return;
362bdc352b1SMatthias Ringwald     }
363bdc352b1SMatthias Ringwald 
364bdc352b1SMatthias Ringwald     le_streamer_connection_t * context = &le_streamer_connections[connection_index];
365bdc352b1SMatthias Ringwald 
366bdc352b1SMatthias Ringwald     // create test data
367bdc352b1SMatthias Ringwald     context->counter++;
368bdc352b1SMatthias Ringwald     if (context->counter > 'Z') context->counter = 'A';
369bdc352b1SMatthias Ringwald     memset(context->test_data, context->counter, context->test_data_len);
370bdc352b1SMatthias Ringwald 
371bdc352b1SMatthias Ringwald     // send
372bdc352b1SMatthias Ringwald     att_server_notify(context->connection_handle, context->value_handle, (uint8_t*) context->test_data, context->test_data_len);
373bdc352b1SMatthias Ringwald 
374bdc352b1SMatthias Ringwald     // track
375bdc352b1SMatthias Ringwald     test_track_sent(context, context->test_data_len);
376bdc352b1SMatthias Ringwald 
377bdc352b1SMatthias Ringwald     // request next send event
378bdc352b1SMatthias Ringwald     att_server_request_can_send_now_event(context->connection_handle);
379bdc352b1SMatthias Ringwald 
380bdc352b1SMatthias Ringwald     // check next
381bdc352b1SMatthias Ringwald     next_connection_index();
382bdc352b1SMatthias Ringwald }
383bdc352b1SMatthias Ringwald /* LISTING_END */
384bdc352b1SMatthias Ringwald 
385bdc352b1SMatthias Ringwald /*
386bdc352b1SMatthias Ringwald  * @section ATT Write
387bdc352b1SMatthias Ringwald  *
388bdc352b1SMatthias Ringwald  * @text The only valid ATT write in this example is to the Client Characteristic Configuration, which configures notification
389bdc352b1SMatthias Ringwald  * and indication. If the ATT handle matches the client configuration handle, the new configuration value is stored.
390bdc352b1SMatthias Ringwald  * If notifications get enabled, an ATT_EVENT_CAN_SEND_NOW is requested. See Listing attWrite.
391bdc352b1SMatthias Ringwald  */
392bdc352b1SMatthias Ringwald 
393bdc352b1SMatthias Ringwald /* LISTING_START(attWrite): ATT Write */
394bdc352b1SMatthias 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){
395bdc352b1SMatthias Ringwald     UNUSED(offset);
396bdc352b1SMatthias Ringwald 
397bdc352b1SMatthias Ringwald     // printf("att_write_callback att_handle %04x, transaction mode %u\n", att_handle, transaction_mode);
398bdc352b1SMatthias Ringwald     if (transaction_mode != ATT_TRANSACTION_MODE_NONE) return 0;
399bdc352b1SMatthias Ringwald     le_streamer_connection_t * context = connection_for_conn_handle(con_handle);
400bdc352b1SMatthias Ringwald     switch(att_handle){
401bdc352b1SMatthias Ringwald         case ATT_CHARACTERISTIC_0000FF11_0000_1000_8000_00805F9B34FB_01_CLIENT_CONFIGURATION_HANDLE:
402bdc352b1SMatthias Ringwald         case ATT_CHARACTERISTIC_0000FF12_0000_1000_8000_00805F9B34FB_01_CLIENT_CONFIGURATION_HANDLE:
403bdc352b1SMatthias Ringwald             context->le_notification_enabled = little_endian_read_16(buffer, 0) == GATT_CLIENT_CHARACTERISTICS_CONFIGURATION_NOTIFICATION;
404bdc352b1SMatthias Ringwald             printf("%c: Notifications enabled %u\n", context->name, context->le_notification_enabled);
405bdc352b1SMatthias Ringwald             if (context->le_notification_enabled){
406bdc352b1SMatthias Ringwald                 switch (att_handle){
407bdc352b1SMatthias Ringwald                     case ATT_CHARACTERISTIC_0000FF11_0000_1000_8000_00805F9B34FB_01_CLIENT_CONFIGURATION_HANDLE:
408bdc352b1SMatthias Ringwald                         context->value_handle = ATT_CHARACTERISTIC_0000FF11_0000_1000_8000_00805F9B34FB_01_VALUE_HANDLE;
409bdc352b1SMatthias Ringwald                         break;
410bdc352b1SMatthias Ringwald                     case ATT_CHARACTERISTIC_0000FF12_0000_1000_8000_00805F9B34FB_01_CLIENT_CONFIGURATION_HANDLE:
411bdc352b1SMatthias Ringwald                         context->value_handle = ATT_CHARACTERISTIC_0000FF12_0000_1000_8000_00805F9B34FB_01_VALUE_HANDLE;
412bdc352b1SMatthias Ringwald                         break;
413bdc352b1SMatthias Ringwald                     default:
414bdc352b1SMatthias Ringwald                         break;
415bdc352b1SMatthias Ringwald                 }
416bdc352b1SMatthias Ringwald                 att_server_request_can_send_now_event(context->connection_handle);
417bdc352b1SMatthias Ringwald             }
418bdc352b1SMatthias Ringwald             test_reset(context);
419bdc352b1SMatthias Ringwald             break;
420bdc352b1SMatthias Ringwald         case ATT_CHARACTERISTIC_0000FF11_0000_1000_8000_00805F9B34FB_01_VALUE_HANDLE:
421bdc352b1SMatthias Ringwald         case ATT_CHARACTERISTIC_0000FF12_0000_1000_8000_00805F9B34FB_01_VALUE_HANDLE:
422bdc352b1SMatthias Ringwald             test_track_sent(context, buffer_size);
423bdc352b1SMatthias Ringwald             break;
424bdc352b1SMatthias Ringwald         default:
425bdc352b1SMatthias Ringwald             printf("Write to 0x%04x, len %u\n", att_handle, buffer_size);
426*6058cb0dSMatthias Ringwald             break;
427bdc352b1SMatthias Ringwald     }
428bdc352b1SMatthias Ringwald     return 0;
429bdc352b1SMatthias Ringwald }
430bdc352b1SMatthias Ringwald /* LISTING_END */
431bdc352b1SMatthias Ringwald 
432bdc352b1SMatthias Ringwald int btstack_main(void);
433bdc352b1SMatthias Ringwald int btstack_main(void)
434bdc352b1SMatthias Ringwald {
435bdc352b1SMatthias Ringwald     le_streamer_setup();
436bdc352b1SMatthias Ringwald 
437bdc352b1SMatthias Ringwald     // turn on!
438bdc352b1SMatthias Ringwald 	hci_power_control(HCI_POWER_ON);
439bdc352b1SMatthias Ringwald 
440bdc352b1SMatthias Ringwald     return 0;
441bdc352b1SMatthias Ringwald }
442bdc352b1SMatthias Ringwald /* EXAMPLE_END */
443