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
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
init_connections(void)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
connection_for_conn_handle(hci_con_handle_t conn_handle)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
next_connection_index(void)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
le_streamer_setup(void)154bdc352b1SMatthias Ringwald static void le_streamer_setup(void){
155bdc352b1SMatthias Ringwald
156bdc352b1SMatthias Ringwald l2cap_init();
157bdc352b1SMatthias Ringwald
158bdc352b1SMatthias Ringwald // setup SM: Display only
159bdc352b1SMatthias Ringwald sm_init();
160bdc352b1SMatthias Ringwald
161bdc352b1SMatthias Ringwald #ifdef ENABLE_GATT_OVER_CLASSIC
162bdc352b1SMatthias Ringwald // init SDP, create record for GATT and register with SDP
163bdc352b1SMatthias Ringwald sdp_init();
164bdc352b1SMatthias Ringwald memset(gatt_service_buffer, 0, sizeof(gatt_service_buffer));
165bdc352b1SMatthias Ringwald gatt_create_sdp_record(gatt_service_buffer, 0x10001, ATT_SERVICE_GATT_SERVICE_START_HANDLE, ATT_SERVICE_GATT_SERVICE_END_HANDLE);
166bdc352b1SMatthias Ringwald sdp_register_service(gatt_service_buffer);
167bdc352b1SMatthias Ringwald printf("SDP service record size: %u\n", de_get_len(gatt_service_buffer));
168bdc352b1SMatthias Ringwald
169bdc352b1SMatthias Ringwald // configure Classic GAP
170bdc352b1SMatthias Ringwald gap_set_local_name("GATT Streamer BR/EDR 00:00:00:00:00:00");
171bdc352b1SMatthias Ringwald gap_ssp_set_io_capability(SSP_IO_CAPABILITY_DISPLAY_YES_NO);
172bdc352b1SMatthias Ringwald gap_discoverable_control(1);
173bdc352b1SMatthias Ringwald #endif
174bdc352b1SMatthias Ringwald
175bdc352b1SMatthias Ringwald // setup ATT server
176bdc352b1SMatthias Ringwald att_server_init(profile_data, NULL, att_write_callback);
177bdc352b1SMatthias Ringwald
178bdc352b1SMatthias Ringwald // register for HCI events
179bdc352b1SMatthias Ringwald hci_event_callback_registration.callback = &hci_packet_handler;
180bdc352b1SMatthias Ringwald hci_add_event_handler(&hci_event_callback_registration);
181bdc352b1SMatthias Ringwald
182bdc352b1SMatthias Ringwald // register for ATT events
183bdc352b1SMatthias Ringwald att_server_register_packet_handler(att_packet_handler);
184bdc352b1SMatthias Ringwald
185bdc352b1SMatthias Ringwald // setup advertisements
186bdc352b1SMatthias Ringwald uint16_t adv_int_min = 0x0030;
187bdc352b1SMatthias Ringwald uint16_t adv_int_max = 0x0030;
188bdc352b1SMatthias Ringwald uint8_t adv_type = 0;
189bdc352b1SMatthias Ringwald bd_addr_t null_addr;
190bdc352b1SMatthias Ringwald memset(null_addr, 0, 6);
191bdc352b1SMatthias Ringwald gap_advertisements_set_params(adv_int_min, adv_int_max, adv_type, 0, null_addr, 0x07, 0x00);
192bdc352b1SMatthias Ringwald gap_advertisements_set_data(adv_data_len, (uint8_t*) adv_data);
193bdc352b1SMatthias Ringwald gap_advertisements_enable(1);
194bdc352b1SMatthias Ringwald
195bdc352b1SMatthias Ringwald // init client state
196bdc352b1SMatthias Ringwald init_connections();
197bdc352b1SMatthias Ringwald }
198bdc352b1SMatthias Ringwald /* LISTING_END */
199bdc352b1SMatthias Ringwald
200bdc352b1SMatthias Ringwald /*
201bdc352b1SMatthias Ringwald * @section Track throughput
202bdc352b1SMatthias Ringwald * @text We calculate the throughput by setting a start time and measuring the amount of
203bdc352b1SMatthias Ringwald * data sent. After a configurable REPORT_INTERVAL_MS, we print the throughput in kB/s
204bdc352b1SMatthias Ringwald * and reset the counter and start time.
205bdc352b1SMatthias Ringwald */
206bdc352b1SMatthias Ringwald
207bdc352b1SMatthias Ringwald /* LISTING_START(tracking): Tracking throughput */
208bdc352b1SMatthias Ringwald
test_reset(le_streamer_connection_t * context)209bdc352b1SMatthias Ringwald static void test_reset(le_streamer_connection_t * context){
210bdc352b1SMatthias Ringwald context->test_data_start = btstack_run_loop_get_time_ms();
211bdc352b1SMatthias Ringwald context->test_data_sent = 0;
212bdc352b1SMatthias Ringwald }
213bdc352b1SMatthias Ringwald
test_track_sent(le_streamer_connection_t * context,int bytes_sent)214bdc352b1SMatthias Ringwald static void test_track_sent(le_streamer_connection_t * context, int bytes_sent){
215bdc352b1SMatthias Ringwald context->test_data_sent += bytes_sent;
216bdc352b1SMatthias Ringwald // evaluate
217bdc352b1SMatthias Ringwald uint32_t now = btstack_run_loop_get_time_ms();
218bdc352b1SMatthias Ringwald uint32_t time_passed = now - context->test_data_start;
219bdc352b1SMatthias Ringwald if (time_passed < REPORT_INTERVAL_MS) return;
220bdc352b1SMatthias Ringwald // print speed
221bdc352b1SMatthias Ringwald int bytes_per_second = context->test_data_sent * 1000 / time_passed;
222bdc352b1SMatthias 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);
223bdc352b1SMatthias Ringwald
224bdc352b1SMatthias Ringwald // restart
225bdc352b1SMatthias Ringwald context->test_data_start = now;
226bdc352b1SMatthias Ringwald context->test_data_sent = 0;
227bdc352b1SMatthias Ringwald }
228bdc352b1SMatthias Ringwald /* LISTING_END(tracking): Tracking throughput */
229bdc352b1SMatthias Ringwald
230bdc352b1SMatthias Ringwald /*
231bdc352b1SMatthias Ringwald * @section HCI Packet Handler
232bdc352b1SMatthias Ringwald *
233bdc352b1SMatthias Ringwald * @text The packet handler is used track incoming connections and to stop notifications on disconnect
234bdc352b1SMatthias Ringwald * It is also a good place to request the connection parameter update as indicated
235bdc352b1SMatthias Ringwald * in the commented code block.
236bdc352b1SMatthias Ringwald */
237bdc352b1SMatthias Ringwald
238bdc352b1SMatthias Ringwald /* LISTING_START(hciPacketHandler): Packet Handler */
hci_packet_handler(uint8_t packet_type,uint16_t channel,uint8_t * packet,uint16_t size)239bdc352b1SMatthias Ringwald static void hci_packet_handler (uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
240bdc352b1SMatthias Ringwald UNUSED(channel);
241bdc352b1SMatthias Ringwald UNUSED(size);
242bdc352b1SMatthias Ringwald
243f4fc15d4SMatthias Ringwald if (packet_type != HCI_EVENT_PACKET) return;
244f4fc15d4SMatthias Ringwald
245bdc352b1SMatthias Ringwald uint16_t conn_interval;
246bdc352b1SMatthias Ringwald hci_con_handle_t con_handle;
247f4fc15d4SMatthias Ringwald static const char * const phy_names[] = {
248*d142f146SMatthias Ringwald "Reserved", "1 M", "2 M", "Codec"
249f4fc15d4SMatthias Ringwald };
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);
260fcd55a0bSMilanka Ringwald printf("- LE Connection 0x%04x: disconnect, reason %02x\n", con_handle, hci_event_disconnection_complete_get_reason(packet));
261bdc352b1SMatthias Ringwald break;
262bba48196SMatthias Ringwald case HCI_EVENT_META_GAP:
263bba48196SMatthias Ringwald switch (hci_event_gap_meta_get_subevent_code(packet)) {
264bba48196SMatthias Ringwald case GAP_SUBEVENT_LE_CONNECTION_COMPLETE:
265bdc352b1SMatthias Ringwald // print connection parameters (without using float operations)
266bba48196SMatthias Ringwald con_handle = gap_subevent_le_connection_complete_get_connection_handle(packet);
267bba48196SMatthias Ringwald conn_interval = gap_subevent_le_connection_complete_get_conn_interval(packet);
268fcd55a0bSMilanka Ringwald printf("- LE Connection 0x%04x: connected - connection interval %u.%02u ms, latency %u\n", con_handle, conn_interval * 125 / 100,
269bba48196SMatthias Ringwald 25 * (conn_interval & 3), gap_subevent_le_connection_complete_get_conn_latency(packet));
270bdc352b1SMatthias Ringwald
271bdc352b1SMatthias Ringwald // request min con interval 15 ms for iOS 11+
272fcd55a0bSMilanka Ringwald printf("- LE Connection 0x%04x: request 15 ms connection interval\n", con_handle);
273a6622c57SMatthias Ringwald gap_request_connection_parameter_update(con_handle, 12, 12, 4, 0x0048);
274bdc352b1SMatthias Ringwald break;
275bba48196SMatthias Ringwald default:
276bba48196SMatthias Ringwald break;
277bba48196SMatthias Ringwald }
278bba48196SMatthias Ringwald break;
279bba48196SMatthias Ringwald case HCI_EVENT_LE_META:
280bba48196SMatthias Ringwald switch (hci_event_le_meta_get_subevent_code(packet)) {
281bdc352b1SMatthias Ringwald case HCI_SUBEVENT_LE_CONNECTION_UPDATE_COMPLETE:
282bdc352b1SMatthias Ringwald // print connection parameters (without using float operations)
283bdc352b1SMatthias Ringwald con_handle = hci_subevent_le_connection_update_complete_get_connection_handle(packet);
284bdc352b1SMatthias Ringwald conn_interval = hci_subevent_le_connection_update_complete_get_conn_interval(packet);
285fcd55a0bSMilanka Ringwald printf("- LE Connection 0x%04x: connection update - connection interval %u.%02u ms, latency %u\n", con_handle, conn_interval * 125 / 100,
286bdc352b1SMatthias Ringwald 25 * (conn_interval & 3), hci_subevent_le_connection_update_complete_get_conn_latency(packet));
287bdc352b1SMatthias Ringwald break;
288f4fc15d4SMatthias Ringwald case HCI_SUBEVENT_LE_DATA_LENGTH_CHANGE:
289f4fc15d4SMatthias Ringwald con_handle = hci_subevent_le_data_length_change_get_connection_handle(packet);
290f4fc15d4SMatthias Ringwald printf("- LE Connection 0x%04x: data length change - max %u bytes per packet\n", con_handle,
291f4fc15d4SMatthias Ringwald hci_subevent_le_data_length_change_get_max_tx_octets(packet));
292f4fc15d4SMatthias Ringwald break;
293f4fc15d4SMatthias Ringwald case HCI_SUBEVENT_LE_PHY_UPDATE_COMPLETE:
294f4fc15d4SMatthias Ringwald con_handle = hci_subevent_le_phy_update_complete_get_connection_handle(packet);
295f4fc15d4SMatthias Ringwald printf("- LE Connection 0x%04x: PHY update - using LE %s PHY now\n", con_handle,
296f4fc15d4SMatthias Ringwald phy_names[hci_subevent_le_phy_update_complete_get_tx_phy(packet)]);
297f4fc15d4SMatthias Ringwald break;
298bdc352b1SMatthias Ringwald default:
299bdc352b1SMatthias Ringwald break;
300bdc352b1SMatthias Ringwald }
301bdc352b1SMatthias Ringwald break;
3027bbeb3adSMilanka Ringwald
303bdc352b1SMatthias Ringwald default:
304bdc352b1SMatthias Ringwald break;
305bdc352b1SMatthias Ringwald }
306bdc352b1SMatthias Ringwald }
307bdc352b1SMatthias Ringwald
308bdc352b1SMatthias Ringwald /* LISTING_END */
309bdc352b1SMatthias Ringwald
310bdc352b1SMatthias Ringwald /*
311bdc352b1SMatthias Ringwald * @section ATT Packet Handler
312bdc352b1SMatthias Ringwald *
313bdc352b1SMatthias Ringwald * @text The packet handler is used to track the ATT MTU Exchange and trigger ATT send
314bdc352b1SMatthias Ringwald */
315bdc352b1SMatthias Ringwald
316bdc352b1SMatthias Ringwald /* LISTING_START(attPacketHandler): Packet Handler */
att_packet_handler(uint8_t packet_type,uint16_t channel,uint8_t * packet,uint16_t size)317bdc352b1SMatthias Ringwald static void att_packet_handler (uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
318bdc352b1SMatthias Ringwald UNUSED(channel);
319bdc352b1SMatthias Ringwald UNUSED(size);
320bdc352b1SMatthias Ringwald
321bdc352b1SMatthias Ringwald int mtu;
322bdc352b1SMatthias Ringwald le_streamer_connection_t * context;
323bdc352b1SMatthias Ringwald switch (packet_type) {
324bdc352b1SMatthias Ringwald case HCI_EVENT_PACKET:
325bdc352b1SMatthias Ringwald switch (hci_event_packet_get_type(packet)) {
326bdc352b1SMatthias Ringwald case ATT_EVENT_CONNECTED:
327bdc352b1SMatthias Ringwald // setup new
328bdc352b1SMatthias Ringwald context = connection_for_conn_handle(HCI_CON_HANDLE_INVALID);
329bdc352b1SMatthias Ringwald if (!context) break;
330bdc352b1SMatthias Ringwald context->counter = 'A';
331bdc352b1SMatthias Ringwald context->connection_handle = att_event_connected_get_handle(packet);
332bdc352b1SMatthias Ringwald context->test_data_len = btstack_min(att_server_get_mtu(context->connection_handle) - 3, sizeof(context->test_data));
333fcd55a0bSMilanka Ringwald printf("%c: ATT connected, handle 0x%04x, test data len %u\n", context->name, context->connection_handle, context->test_data_len);
334bdc352b1SMatthias Ringwald break;
335bdc352b1SMatthias Ringwald case ATT_EVENT_MTU_EXCHANGE_COMPLETE:
336bdc352b1SMatthias Ringwald mtu = att_event_mtu_exchange_complete_get_MTU(packet) - 3;
337bdc352b1SMatthias Ringwald context = connection_for_conn_handle(att_event_mtu_exchange_complete_get_handle(packet));
338bdc352b1SMatthias Ringwald if (!context) break;
339bdc352b1SMatthias Ringwald context->test_data_len = btstack_min(mtu - 3, sizeof(context->test_data));
340bdc352b1SMatthias Ringwald printf("%c: ATT MTU = %u => use test data of len %u\n", context->name, mtu, context->test_data_len);
341bdc352b1SMatthias Ringwald break;
342bdc352b1SMatthias Ringwald case ATT_EVENT_CAN_SEND_NOW:
343bdc352b1SMatthias Ringwald streamer();
344bdc352b1SMatthias Ringwald break;
345bdc352b1SMatthias Ringwald case ATT_EVENT_DISCONNECTED:
346bdc352b1SMatthias Ringwald context = connection_for_conn_handle(att_event_disconnected_get_handle(packet));
347bdc352b1SMatthias Ringwald if (!context) break;
348bdc352b1SMatthias Ringwald // free connection
349fcd55a0bSMilanka Ringwald printf("%c: ATT disconnected, handle 0x%04x\n", context->name, context->connection_handle);
350bdc352b1SMatthias Ringwald context->le_notification_enabled = 0;
351bdc352b1SMatthias Ringwald context->connection_handle = HCI_CON_HANDLE_INVALID;
352bdc352b1SMatthias Ringwald break;
353bdc352b1SMatthias Ringwald default:
354bdc352b1SMatthias Ringwald break;
355bdc352b1SMatthias Ringwald }
356bdc352b1SMatthias Ringwald break;
357bdc352b1SMatthias Ringwald default:
358bdc352b1SMatthias Ringwald break;
359bdc352b1SMatthias Ringwald }
360bdc352b1SMatthias Ringwald }
361bdc352b1SMatthias Ringwald /* LISTING_END */
362bdc352b1SMatthias Ringwald
363bdc352b1SMatthias Ringwald /*
364bdc352b1SMatthias Ringwald * @section Streamer
365bdc352b1SMatthias Ringwald *
366bdc352b1SMatthias Ringwald * @text The streamer function checks if notifications are enabled and if a notification can be sent now.
367bdc352b1SMatthias Ringwald * It creates some test data - a single letter that gets increased every time - and tracks the data sent.
368bdc352b1SMatthias Ringwald */
369bdc352b1SMatthias Ringwald
370bdc352b1SMatthias Ringwald /* LISTING_START(streamer): Streaming code */
streamer(void)371bdc352b1SMatthias Ringwald static void streamer(void){
372bdc352b1SMatthias Ringwald
373bdc352b1SMatthias Ringwald // find next active streaming connection
374bdc352b1SMatthias Ringwald int old_connection_index = connection_index;
375bdc352b1SMatthias Ringwald while (1){
376bdc352b1SMatthias Ringwald // active found?
377bdc352b1SMatthias Ringwald if ((le_streamer_connections[connection_index].connection_handle != HCI_CON_HANDLE_INVALID) &&
378bdc352b1SMatthias Ringwald (le_streamer_connections[connection_index].le_notification_enabled)) break;
379bdc352b1SMatthias Ringwald
380bdc352b1SMatthias Ringwald // check next
381bdc352b1SMatthias Ringwald next_connection_index();
382bdc352b1SMatthias Ringwald
383bdc352b1SMatthias Ringwald // none found
384bdc352b1SMatthias Ringwald if (connection_index == old_connection_index) return;
385bdc352b1SMatthias Ringwald }
386bdc352b1SMatthias Ringwald
387bdc352b1SMatthias Ringwald le_streamer_connection_t * context = &le_streamer_connections[connection_index];
388bdc352b1SMatthias Ringwald
389bdc352b1SMatthias Ringwald // create test data
390bdc352b1SMatthias Ringwald context->counter++;
391bdc352b1SMatthias Ringwald if (context->counter > 'Z') context->counter = 'A';
392bdc352b1SMatthias Ringwald memset(context->test_data, context->counter, context->test_data_len);
393bdc352b1SMatthias Ringwald
394bdc352b1SMatthias Ringwald // send
395bdc352b1SMatthias Ringwald att_server_notify(context->connection_handle, context->value_handle, (uint8_t*) context->test_data, context->test_data_len);
396bdc352b1SMatthias Ringwald
397bdc352b1SMatthias Ringwald // track
398bdc352b1SMatthias Ringwald test_track_sent(context, context->test_data_len);
399bdc352b1SMatthias Ringwald
400bdc352b1SMatthias Ringwald // request next send event
401bdc352b1SMatthias Ringwald att_server_request_can_send_now_event(context->connection_handle);
402bdc352b1SMatthias Ringwald
403bdc352b1SMatthias Ringwald // check next
404bdc352b1SMatthias Ringwald next_connection_index();
405bdc352b1SMatthias Ringwald }
406bdc352b1SMatthias Ringwald /* LISTING_END */
407bdc352b1SMatthias Ringwald
408bdc352b1SMatthias Ringwald /*
409bdc352b1SMatthias Ringwald * @section ATT Write
410bdc352b1SMatthias Ringwald *
411bdc352b1SMatthias Ringwald * @text The only valid ATT write in this example is to the Client Characteristic Configuration, which configures notification
412bdc352b1SMatthias Ringwald * and indication. If the ATT handle matches the client configuration handle, the new configuration value is stored.
413bdc352b1SMatthias Ringwald * If notifications get enabled, an ATT_EVENT_CAN_SEND_NOW is requested. See Listing attWrite.
414bdc352b1SMatthias Ringwald */
415bdc352b1SMatthias Ringwald
416bdc352b1SMatthias Ringwald /* LISTING_START(attWrite): ATT Write */
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)417bdc352b1SMatthias 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){
418bdc352b1SMatthias Ringwald UNUSED(offset);
419bdc352b1SMatthias Ringwald
420fcd55a0bSMilanka Ringwald // printf("att_write_callback att_handle 0x%04x, transaction mode %u\n", att_handle, transaction_mode);
421bdc352b1SMatthias Ringwald if (transaction_mode != ATT_TRANSACTION_MODE_NONE) return 0;
422bdc352b1SMatthias Ringwald le_streamer_connection_t * context = connection_for_conn_handle(con_handle);
423bdc352b1SMatthias Ringwald switch(att_handle){
424bdc352b1SMatthias Ringwald case ATT_CHARACTERISTIC_0000FF11_0000_1000_8000_00805F9B34FB_01_CLIENT_CONFIGURATION_HANDLE:
425bdc352b1SMatthias Ringwald case ATT_CHARACTERISTIC_0000FF12_0000_1000_8000_00805F9B34FB_01_CLIENT_CONFIGURATION_HANDLE:
426bdc352b1SMatthias Ringwald context->le_notification_enabled = little_endian_read_16(buffer, 0) == GATT_CLIENT_CHARACTERISTICS_CONFIGURATION_NOTIFICATION;
427bdc352b1SMatthias Ringwald printf("%c: Notifications enabled %u\n", context->name, context->le_notification_enabled);
428bdc352b1SMatthias Ringwald if (context->le_notification_enabled){
429bdc352b1SMatthias Ringwald switch (att_handle){
430bdc352b1SMatthias Ringwald case ATT_CHARACTERISTIC_0000FF11_0000_1000_8000_00805F9B34FB_01_CLIENT_CONFIGURATION_HANDLE:
431bdc352b1SMatthias Ringwald context->value_handle = ATT_CHARACTERISTIC_0000FF11_0000_1000_8000_00805F9B34FB_01_VALUE_HANDLE;
432bdc352b1SMatthias Ringwald break;
433bdc352b1SMatthias Ringwald case ATT_CHARACTERISTIC_0000FF12_0000_1000_8000_00805F9B34FB_01_CLIENT_CONFIGURATION_HANDLE:
434bdc352b1SMatthias Ringwald context->value_handle = ATT_CHARACTERISTIC_0000FF12_0000_1000_8000_00805F9B34FB_01_VALUE_HANDLE;
435bdc352b1SMatthias Ringwald break;
436bdc352b1SMatthias Ringwald default:
437bdc352b1SMatthias Ringwald break;
438bdc352b1SMatthias Ringwald }
439bdc352b1SMatthias Ringwald att_server_request_can_send_now_event(context->connection_handle);
440bdc352b1SMatthias Ringwald }
441bdc352b1SMatthias Ringwald test_reset(context);
442bdc352b1SMatthias Ringwald break;
443bdc352b1SMatthias Ringwald case ATT_CHARACTERISTIC_0000FF11_0000_1000_8000_00805F9B34FB_01_VALUE_HANDLE:
444bdc352b1SMatthias Ringwald case ATT_CHARACTERISTIC_0000FF12_0000_1000_8000_00805F9B34FB_01_VALUE_HANDLE:
445bdc352b1SMatthias Ringwald test_track_sent(context, buffer_size);
446bdc352b1SMatthias Ringwald break;
447bdc352b1SMatthias Ringwald default:
448bdc352b1SMatthias Ringwald printf("Write to 0x%04x, len %u\n", att_handle, buffer_size);
4496058cb0dSMatthias Ringwald break;
450bdc352b1SMatthias Ringwald }
451bdc352b1SMatthias Ringwald return 0;
452bdc352b1SMatthias Ringwald }
453bdc352b1SMatthias Ringwald /* LISTING_END */
454bdc352b1SMatthias Ringwald
455bdc352b1SMatthias Ringwald int btstack_main(void);
btstack_main(void)456bdc352b1SMatthias Ringwald int btstack_main(void)
457bdc352b1SMatthias Ringwald {
458bdc352b1SMatthias Ringwald le_streamer_setup();
459bdc352b1SMatthias Ringwald
460bdc352b1SMatthias Ringwald // turn on!
461bdc352b1SMatthias Ringwald hci_power_control(HCI_POWER_ON);
462bdc352b1SMatthias Ringwald
463bdc352b1SMatthias Ringwald return 0;
464bdc352b1SMatthias Ringwald }
465bdc352b1SMatthias Ringwald /* EXAMPLE_END */
466