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