xref: /btstack/example/le_streamer_client.c (revision a4fe6467953bdb173fdf96a604f6527ed88f81c3)
1c8f97be6SMatthias Ringwald /*
2c8f97be6SMatthias Ringwald  * Copyright (C) 2014 BlueKitchen GmbH
3c8f97be6SMatthias Ringwald  *
4c8f97be6SMatthias Ringwald  * Redistribution and use in source and binary forms, with or without
5c8f97be6SMatthias Ringwald  * modification, are permitted provided that the following conditions
6c8f97be6SMatthias Ringwald  * are met:
7c8f97be6SMatthias Ringwald  *
8c8f97be6SMatthias Ringwald  * 1. Redistributions of source code must retain the above copyright
9c8f97be6SMatthias Ringwald  *    notice, this list of conditions and the following disclaimer.
10c8f97be6SMatthias Ringwald  * 2. Redistributions in binary form must reproduce the above copyright
11c8f97be6SMatthias Ringwald  *    notice, this list of conditions and the following disclaimer in the
12c8f97be6SMatthias Ringwald  *    documentation and/or other materials provided with the distribution.
13c8f97be6SMatthias Ringwald  * 3. Neither the name of the copyright holders nor the names of
14c8f97be6SMatthias Ringwald  *    contributors may be used to endorse or promote products derived
15c8f97be6SMatthias Ringwald  *    from this software without specific prior written permission.
16c8f97be6SMatthias Ringwald  * 4. Any redistribution, use, or modification is done solely for
17c8f97be6SMatthias Ringwald  *    personal benefit and not for any commercial purpose or for
18c8f97be6SMatthias Ringwald  *    monetary gain.
19c8f97be6SMatthias Ringwald  *
20c8f97be6SMatthias Ringwald  * THIS SOFTWARE IS PROVIDED BY BLUEKITCHEN GMBH AND CONTRIBUTORS
21c8f97be6SMatthias Ringwald  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22c8f97be6SMatthias Ringwald  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23c8f97be6SMatthias Ringwald  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL MATTHIAS
24c8f97be6SMatthias Ringwald  * RINGWALD OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
25c8f97be6SMatthias Ringwald  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
26c8f97be6SMatthias Ringwald  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
27c8f97be6SMatthias Ringwald  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
28c8f97be6SMatthias Ringwald  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
29c8f97be6SMatthias Ringwald  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
30c8f97be6SMatthias Ringwald  * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31c8f97be6SMatthias Ringwald  * SUCH DAMAGE.
32c8f97be6SMatthias Ringwald  *
33c8f97be6SMatthias Ringwald  * Please inquire about commercial licensing options at
34c8f97be6SMatthias Ringwald  * [email protected]
35c8f97be6SMatthias Ringwald  *
36c8f97be6SMatthias Ringwald  */
37c8f97be6SMatthias Ringwald 
38ab2c6ae4SMatthias Ringwald #define __BTSTACK_FILE__ "le_streamer_client.c"
39ab2c6ae4SMatthias Ringwald 
4015de8206SMilanka Ringwald /*
4115de8206SMilanka Ringwald  * le_streamer_client.c
4215de8206SMilanka Ringwald  */
4315de8206SMilanka Ringwald 
44c8f97be6SMatthias Ringwald // *****************************************************************************
4515de8206SMilanka Ringwald /* EXAMPLE_START(le_streamer_client): Connects to 'LE Streamer' and subscribes to test characteristic
4615de8206SMilanka Ringwald  */
47c8f97be6SMatthias Ringwald // *****************************************************************************
48c8f97be6SMatthias Ringwald 
49173fff9bSMatthias Ringwald #include <inttypes.h>
50c8f97be6SMatthias Ringwald #include <stdint.h>
51c8f97be6SMatthias Ringwald #include <stdio.h>
52c8f97be6SMatthias Ringwald #include <stdlib.h>
53c8f97be6SMatthias Ringwald #include <string.h>
54c8f97be6SMatthias Ringwald 
55c8f97be6SMatthias Ringwald #include "btstack.h"
56c8f97be6SMatthias Ringwald 
577fa79385SMatthias Ringwald // prototypes
587fa79385SMatthias Ringwald static void handle_gatt_client_event(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size);
597fa79385SMatthias Ringwald 
60c8f97be6SMatthias Ringwald typedef enum {
61beeed1f3SMatthias Ringwald     TC_OFF,
62c8f97be6SMatthias Ringwald     TC_IDLE,
63c8f97be6SMatthias Ringwald     TC_W4_SCAN_RESULT,
64c8f97be6SMatthias Ringwald     TC_W4_CONNECT,
65c8f97be6SMatthias Ringwald     TC_W4_SERVICE_RESULT,
665c4f3138SMatthias Ringwald     TC_W4_CHARACTERISTIC_RX_RESULT,
675c4f3138SMatthias Ringwald     TC_W4_CHARACTERISTIC_TX_RESULT,
68dc673ce3SMatthias Ringwald     TC_W4_ENABLE_NOTIFICATIONS_COMPLETE,
69c8f97be6SMatthias Ringwald     TC_W4_TEST_DATA
70c8f97be6SMatthias Ringwald } gc_state_t;
71c8f97be6SMatthias Ringwald 
72c30af2ffSMatthias Ringwald static bd_addr_t cmdline_addr;
73c8f97be6SMatthias Ringwald static int cmdline_addr_found = 0;
74c8f97be6SMatthias Ringwald 
75c8f97be6SMatthias Ringwald // addr and type of device with correct name
76c8f97be6SMatthias Ringwald static bd_addr_t      le_streamer_addr;
77c8f97be6SMatthias Ringwald static bd_addr_type_t le_streamer_addr_type;
78c8f97be6SMatthias Ringwald 
79c8f97be6SMatthias Ringwald static hci_con_handle_t connection_handle;
805c4f3138SMatthias Ringwald 
815c4f3138SMatthias Ringwald // On the GATT Server, RX Characteristic is used for receive data via Write, and TX Characteristic is used to send data via Notifications
82c8f97be6SMatthias Ringwald static uint8_t le_streamer_service_uuid[16]           = { 0x00, 0x00, 0xFF, 0x10, 0x00, 0x00, 0x10, 0x00, 0x80, 0x00, 0x00, 0x80, 0x5F, 0x9B, 0x34, 0xFB};
835c4f3138SMatthias Ringwald static uint8_t le_streamer_characteristic_rx_uuid[16] = { 0x00, 0x00, 0xFF, 0x11, 0x00, 0x00, 0x10, 0x00, 0x80, 0x00, 0x00, 0x80, 0x5F, 0x9B, 0x34, 0xFB};
845c4f3138SMatthias Ringwald static uint8_t le_streamer_characteristic_tx_uuid[16] = { 0x00, 0x00, 0xFF, 0x12, 0x00, 0x00, 0x10, 0x00, 0x80, 0x00, 0x00, 0x80, 0x5F, 0x9B, 0x34, 0xFB};
85c8f97be6SMatthias Ringwald 
86c8f97be6SMatthias Ringwald static gatt_client_service_t le_streamer_service;
875c4f3138SMatthias Ringwald static gatt_client_characteristic_t le_streamer_characteristic_rx;
885c4f3138SMatthias Ringwald static gatt_client_characteristic_t le_streamer_characteristic_tx;
89c8f97be6SMatthias Ringwald 
9044a1ebc0SMatthias Ringwald static gatt_client_notification_t notification_listener;
9144a1ebc0SMatthias Ringwald static int listener_registered;
92c8f97be6SMatthias Ringwald 
93beeed1f3SMatthias Ringwald static gc_state_t state = TC_OFF;
94c8f97be6SMatthias Ringwald static btstack_packet_callback_registration_t hci_event_callback_registration;
95c8f97be6SMatthias Ringwald 
9615304b2dSMatthias Ringwald /*
9715304b2dSMatthias Ringwald  * @section Track throughput
9815304b2dSMatthias Ringwald  * @text We calculate the throughput by setting a start time and measuring the amount of
9915304b2dSMatthias Ringwald  * data sent. After a configurable REPORT_INTERVAL_MS, we print the throughput in kB/s
10015304b2dSMatthias Ringwald  * and reset the counter and start time.
10115304b2dSMatthias Ringwald  */
10215304b2dSMatthias Ringwald 
10315304b2dSMatthias Ringwald /* LISTING_START(tracking): Tracking throughput */
10415304b2dSMatthias Ringwald 
105dc673ce3SMatthias Ringwald #define TEST_MODE_WRITE_WITHOUT_RESPONSE 1
106dc673ce3SMatthias Ringwald #define TEST_MODE_ENABLE_NOTIFICATIONS   2
107dc673ce3SMatthias Ringwald #define TEST_MODE_DUPLEX                 3
108dc673ce3SMatthias Ringwald 
109dc673ce3SMatthias Ringwald // configure test mode: send only, receive only, full duplex
110dc673ce3SMatthias Ringwald #define TEST_MODE TEST_MODE_DUPLEX
111dc673ce3SMatthias Ringwald 
11215304b2dSMatthias Ringwald #define REPORT_INTERVAL_MS 3000
11315304b2dSMatthias Ringwald 
11415304b2dSMatthias Ringwald // support for multiple clients
11515304b2dSMatthias Ringwald typedef struct {
11615304b2dSMatthias Ringwald     char name;
11715304b2dSMatthias Ringwald     int le_notification_enabled;
11815304b2dSMatthias Ringwald     int  counter;
11915304b2dSMatthias Ringwald     char test_data[200];
12015304b2dSMatthias Ringwald     int  test_data_len;
12115304b2dSMatthias Ringwald     uint32_t test_data_sent;
12215304b2dSMatthias Ringwald     uint32_t test_data_start;
12315304b2dSMatthias Ringwald } le_streamer_connection_t;
12415304b2dSMatthias Ringwald 
12515304b2dSMatthias Ringwald static le_streamer_connection_t le_streamer_connection;
12615304b2dSMatthias Ringwald 
12715304b2dSMatthias Ringwald static void test_reset(le_streamer_connection_t * context){
12815304b2dSMatthias Ringwald     context->test_data_start = btstack_run_loop_get_time_ms();
12915304b2dSMatthias Ringwald     context->test_data_sent = 0;
13015304b2dSMatthias Ringwald }
13115304b2dSMatthias Ringwald 
13215304b2dSMatthias Ringwald static void test_track_data(le_streamer_connection_t * context, int bytes_sent){
13315304b2dSMatthias Ringwald     context->test_data_sent += bytes_sent;
13415304b2dSMatthias Ringwald     // evaluate
13515304b2dSMatthias Ringwald     uint32_t now = btstack_run_loop_get_time_ms();
13615304b2dSMatthias Ringwald     uint32_t time_passed = now - context->test_data_start;
13715304b2dSMatthias Ringwald     if (time_passed < REPORT_INTERVAL_MS) return;
13815304b2dSMatthias Ringwald     // print speed
13915304b2dSMatthias Ringwald     int bytes_per_second = context->test_data_sent * 1000 / time_passed;
140173fff9bSMatthias Ringwald     printf("%c: %"PRIu32" bytes -> %u.%03u kB/s\n", context->name, context->test_data_sent, bytes_per_second / 1000, bytes_per_second % 1000);
14115304b2dSMatthias Ringwald 
14215304b2dSMatthias Ringwald     // restart
14315304b2dSMatthias Ringwald     context->test_data_start = now;
14415304b2dSMatthias Ringwald     context->test_data_sent  = 0;
14515304b2dSMatthias Ringwald }
14615304b2dSMatthias Ringwald /* LISTING_END(tracking): Tracking throughput */
14715304b2dSMatthias Ringwald 
14815304b2dSMatthias Ringwald 
1497fa79385SMatthias Ringwald // stramer
1507fa79385SMatthias Ringwald static void streamer(le_streamer_connection_t * context){
1517fa79385SMatthias Ringwald     if (connection_handle == HCI_CON_HANDLE_INVALID) return;
1527fa79385SMatthias Ringwald 
1537fa79385SMatthias Ringwald     // create test data
1547fa79385SMatthias Ringwald     context->counter++;
1557fa79385SMatthias Ringwald     if (context->counter > 'Z') context->counter = 'A';
1567fa79385SMatthias Ringwald     memset(context->test_data, context->counter, context->test_data_len);
1577fa79385SMatthias Ringwald 
1587fa79385SMatthias Ringwald     // send
1595c4f3138SMatthias Ringwald     uint8_t status = gatt_client_write_value_of_characteristic_without_response(connection_handle, le_streamer_characteristic_rx.value_handle, context->test_data_len, (uint8_t*) context->test_data);
1607fa79385SMatthias Ringwald     if (status){
1617fa79385SMatthias Ringwald         printf("error %02x for write without response!\n", status);
1627fa79385SMatthias Ringwald         return;
1637fa79385SMatthias Ringwald     } else {
1647fa79385SMatthias Ringwald         test_track_data(&le_streamer_connection, context->test_data_len);
1657fa79385SMatthias Ringwald     }
1667fa79385SMatthias Ringwald 
1677fa79385SMatthias Ringwald     // request again
1687fa79385SMatthias Ringwald     gatt_client_request_can_write_without_response_event(handle_gatt_client_event, connection_handle);
1697fa79385SMatthias Ringwald }
1707fa79385SMatthias Ringwald 
1717fa79385SMatthias Ringwald 
172c8f97be6SMatthias Ringwald // returns 1 if name is found in advertisement
173c8f97be6SMatthias Ringwald static int advertisement_report_contains_name(const char * name, uint8_t * advertisement_report){
174c8f97be6SMatthias Ringwald     // get advertisement from report event
175c8f97be6SMatthias Ringwald     const uint8_t * adv_data = gap_event_advertising_report_get_data(advertisement_report);
176c8f97be6SMatthias Ringwald     uint16_t        adv_len  = gap_event_advertising_report_get_data_length(advertisement_report);
177c8f97be6SMatthias Ringwald     int             name_len = strlen(name);
178c8f97be6SMatthias Ringwald 
179c8f97be6SMatthias Ringwald     // iterate over advertisement data
180c8f97be6SMatthias Ringwald     ad_context_t context;
181c8f97be6SMatthias Ringwald     for (ad_iterator_init(&context, adv_len, adv_data) ; ad_iterator_has_more(&context) ; ad_iterator_next(&context)){
182c8f97be6SMatthias Ringwald         uint8_t data_type    = ad_iterator_get_data_type(&context);
183c8f97be6SMatthias Ringwald         uint8_t data_size    = ad_iterator_get_data_len(&context);
184c8f97be6SMatthias Ringwald         const uint8_t * data = ad_iterator_get_data(&context);
185c8f97be6SMatthias Ringwald         int i;
186c8f97be6SMatthias Ringwald         switch (data_type){
187c8f97be6SMatthias Ringwald             case BLUETOOTH_DATA_TYPE_SHORTENED_LOCAL_NAME:
188c8f97be6SMatthias Ringwald             case BLUETOOTH_DATA_TYPE_COMPLETE_LOCAL_NAME:
189c8f97be6SMatthias Ringwald                 // compare common prefix
190c8f97be6SMatthias Ringwald                 for (i=0; i<data_size && i<name_len;i++){
191c8f97be6SMatthias Ringwald                     if (data[i] != name[i]) break;
192c8f97be6SMatthias Ringwald                 }
193c8f97be6SMatthias Ringwald                 // prefix match
194c8f97be6SMatthias Ringwald                 return 1;
195c8f97be6SMatthias Ringwald             default:
196c8f97be6SMatthias Ringwald                 break;
197c8f97be6SMatthias Ringwald         }
198c8f97be6SMatthias Ringwald     }
199c8f97be6SMatthias Ringwald     return 0;
200c8f97be6SMatthias Ringwald }
201c8f97be6SMatthias Ringwald 
202c8f97be6SMatthias Ringwald static void handle_gatt_client_event(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
203c8f97be6SMatthias Ringwald     UNUSED(packet_type);
204c8f97be6SMatthias Ringwald     UNUSED(channel);
205c8f97be6SMatthias Ringwald     UNUSED(size);
206c8f97be6SMatthias Ringwald 
207dc673ce3SMatthias Ringwald     uint16_t mtu;
208c8f97be6SMatthias Ringwald     switch(state){
209c8f97be6SMatthias Ringwald         case TC_W4_SERVICE_RESULT:
210c8f97be6SMatthias Ringwald             switch(hci_event_packet_get_type(packet)){
211c8f97be6SMatthias Ringwald                 case GATT_EVENT_SERVICE_QUERY_RESULT:
212c8f97be6SMatthias Ringwald                     // store service (we expect only one)
213c8f97be6SMatthias Ringwald                     gatt_event_service_query_result_get_service(packet, &le_streamer_service);
214c8f97be6SMatthias Ringwald                     break;
215c8f97be6SMatthias Ringwald                 case GATT_EVENT_QUERY_COMPLETE:
216c8f97be6SMatthias Ringwald                     if (packet[4] != 0){
217c8f97be6SMatthias Ringwald                         printf("SERVICE_QUERY_RESULT - Error status %x.\n", packet[4]);
218c8f97be6SMatthias Ringwald                         gap_disconnect(connection_handle);
219c8f97be6SMatthias Ringwald                         break;
220c8f97be6SMatthias Ringwald                     }
221c8f97be6SMatthias Ringwald                     // service query complete, look for characteristic
2225c4f3138SMatthias Ringwald                     state = TC_W4_CHARACTERISTIC_RX_RESULT;
2235c4f3138SMatthias Ringwald                     printf("Search for LE Streamer RX characteristic.\n");
2245c4f3138SMatthias Ringwald                     gatt_client_discover_characteristics_for_service_by_uuid128(handle_gatt_client_event, connection_handle, &le_streamer_service, le_streamer_characteristic_rx_uuid);
225c8f97be6SMatthias Ringwald                     break;
226c8f97be6SMatthias Ringwald                 default:
227c8f97be6SMatthias Ringwald                     break;
228c8f97be6SMatthias Ringwald             }
229c8f97be6SMatthias Ringwald             break;
230c8f97be6SMatthias Ringwald 
2315c4f3138SMatthias Ringwald         case TC_W4_CHARACTERISTIC_RX_RESULT:
232c8f97be6SMatthias Ringwald             switch(hci_event_packet_get_type(packet)){
233c8f97be6SMatthias Ringwald                 case GATT_EVENT_CHARACTERISTIC_QUERY_RESULT:
2345c4f3138SMatthias Ringwald                     gatt_event_characteristic_query_result_get_characteristic(packet, &le_streamer_characteristic_rx);
2355c4f3138SMatthias Ringwald                     break;
2365c4f3138SMatthias Ringwald                 case GATT_EVENT_QUERY_COMPLETE:
2375c4f3138SMatthias Ringwald                     if (packet[4] != 0){
2385c4f3138SMatthias Ringwald                         printf("CHARACTERISTIC_QUERY_RESULT - Error status %x.\n", packet[4]);
2395c4f3138SMatthias Ringwald                         gap_disconnect(connection_handle);
2405c4f3138SMatthias Ringwald                         break;
2415c4f3138SMatthias Ringwald                     }
2425c4f3138SMatthias Ringwald                     // rx characteristiic found, look for tx characteristic
2435c4f3138SMatthias Ringwald                     state = TC_W4_CHARACTERISTIC_TX_RESULT;
2445c4f3138SMatthias Ringwald                     printf("Search for LE Streamer TX characteristic.\n");
2455c4f3138SMatthias Ringwald                     gatt_client_discover_characteristics_for_service_by_uuid128(handle_gatt_client_event, connection_handle, &le_streamer_service, le_streamer_characteristic_tx_uuid);
2465c4f3138SMatthias Ringwald                     break;
2475c4f3138SMatthias Ringwald                 default:
2485c4f3138SMatthias Ringwald                     break;
2495c4f3138SMatthias Ringwald             }
2505c4f3138SMatthias Ringwald             break;
2515c4f3138SMatthias Ringwald 
2525c4f3138SMatthias Ringwald         case TC_W4_CHARACTERISTIC_TX_RESULT:
2535c4f3138SMatthias Ringwald             switch(hci_event_packet_get_type(packet)){
2545c4f3138SMatthias Ringwald                 case GATT_EVENT_CHARACTERISTIC_QUERY_RESULT:
2555c4f3138SMatthias Ringwald                     gatt_event_characteristic_query_result_get_characteristic(packet, &le_streamer_characteristic_tx);
256c8f97be6SMatthias Ringwald                     break;
257c8f97be6SMatthias Ringwald                 case GATT_EVENT_QUERY_COMPLETE:
258c8f97be6SMatthias Ringwald                     if (packet[4] != 0){
259c8f97be6SMatthias Ringwald                         printf("CHARACTERISTIC_QUERY_RESULT - Error status %x.\n", packet[4]);
260c8f97be6SMatthias Ringwald                         gap_disconnect(connection_handle);
261c8f97be6SMatthias Ringwald                         break;
262c8f97be6SMatthias Ringwald                     }
263c8f97be6SMatthias Ringwald                     // register handler for notifications
26444a1ebc0SMatthias Ringwald                     listener_registered = 1;
2655c4f3138SMatthias Ringwald                     gatt_client_listen_for_characteristic_value_updates(&notification_listener, handle_gatt_client_event, connection_handle, &le_streamer_characteristic_tx);
26615304b2dSMatthias Ringwald                     // setup tracking
26715304b2dSMatthias Ringwald                     le_streamer_connection.name = 'A';
268dc673ce3SMatthias Ringwald                     le_streamer_connection.test_data_len = ATT_DEFAULT_MTU - 3;
26915304b2dSMatthias Ringwald                     test_reset(&le_streamer_connection);
270dc673ce3SMatthias Ringwald                     gatt_client_get_mtu(connection_handle, &mtu);
271dc673ce3SMatthias Ringwald                     le_streamer_connection.test_data_len = btstack_min(mtu - 3, sizeof(le_streamer_connection.test_data));
272dc673ce3SMatthias Ringwald                     printf("%c: ATT MTU = %u => use test data of len %u\n", le_streamer_connection.name, mtu, le_streamer_connection.test_data_len);
273dc673ce3SMatthias Ringwald                     // enable notifications
274dc673ce3SMatthias Ringwald #if (TEST_MODE & TEST_MODE_ENABLE_NOTIFICATIONS)
275dc673ce3SMatthias Ringwald                     printf("Start streaming - enable notify on test characteristic.\n");
276dc673ce3SMatthias Ringwald                     state = TC_W4_ENABLE_NOTIFICATIONS_COMPLETE;
2777fa79385SMatthias Ringwald                     gatt_client_write_client_characteristic_configuration(handle_gatt_client_event, connection_handle,
2785c4f3138SMatthias Ringwald                         &le_streamer_characteristic_tx, GATT_CLIENT_CHARACTERISTICS_CONFIGURATION_NOTIFICATION);
2797fa79385SMatthias Ringwald                     break;
280dc673ce3SMatthias Ringwald #endif
2817fa79385SMatthias Ringwald                     state = TC_W4_TEST_DATA;
282dc673ce3SMatthias Ringwald #if (TEST_MODE & TEST_MODE_WRITE_WITHOUT_RESPONSE)
283dc673ce3SMatthias Ringwald                     printf("Start streaming - request can send now.\n");
2847fa79385SMatthias Ringwald                     gatt_client_request_can_write_without_response_event(handle_gatt_client_event, connection_handle);
285dc673ce3SMatthias Ringwald #endif
286dc673ce3SMatthias Ringwald                     break;
287dc673ce3SMatthias Ringwald                 default:
288dc673ce3SMatthias Ringwald                     break;
289dc673ce3SMatthias Ringwald             }
290dc673ce3SMatthias Ringwald             break;
291dc673ce3SMatthias Ringwald 
292dc673ce3SMatthias Ringwald         case TC_W4_ENABLE_NOTIFICATIONS_COMPLETE:
293dc673ce3SMatthias Ringwald             switch(hci_event_packet_get_type(packet)){
294dc673ce3SMatthias Ringwald                 case GATT_EVENT_QUERY_COMPLETE:
295dc673ce3SMatthias Ringwald                     printf("Notifications enabled, status %02x\n", gatt_event_query_complete_get_status(packet));
2966da5c9d5SMatthias Ringwald                     if ( gatt_event_query_complete_get_status(packet)) break;
297dc673ce3SMatthias Ringwald                     state = TC_W4_TEST_DATA;
298dc673ce3SMatthias Ringwald #if (TEST_MODE & TEST_MODE_WRITE_WITHOUT_RESPONSE)
2997fa79385SMatthias Ringwald                     printf("Start streaming - request can send now.\n");
3007fa79385SMatthias Ringwald                     gatt_client_request_can_write_without_response_event(handle_gatt_client_event, connection_handle);
301dc673ce3SMatthias Ringwald #endif
302c8f97be6SMatthias Ringwald                     break;
303c8f97be6SMatthias Ringwald                 default:
304c8f97be6SMatthias Ringwald                     break;
305c8f97be6SMatthias Ringwald             }
306c8f97be6SMatthias Ringwald             break;
307c8f97be6SMatthias Ringwald 
308c8f97be6SMatthias Ringwald         case TC_W4_TEST_DATA:
309c8f97be6SMatthias Ringwald             switch(hci_event_packet_get_type(packet)){
310c8f97be6SMatthias Ringwald                 case GATT_EVENT_NOTIFICATION:
31115304b2dSMatthias Ringwald                     test_track_data(&le_streamer_connection, gatt_event_notification_get_value_length(packet));
31215304b2dSMatthias Ringwald                     break;
313c8f97be6SMatthias Ringwald                 case GATT_EVENT_QUERY_COMPLETE:
314c8f97be6SMatthias Ringwald                     break;
3157fa79385SMatthias Ringwald                 case GATT_EVENT_CAN_WRITE_WITHOUT_RESPONSE:
3167fa79385SMatthias Ringwald                     streamer(&le_streamer_connection);
3177fa79385SMatthias Ringwald                     break;
318c8f97be6SMatthias Ringwald                 default:
319c8f97be6SMatthias Ringwald                     printf("Unknown packet type %x\n", hci_event_packet_get_type(packet));
320c8f97be6SMatthias Ringwald                     break;
321c8f97be6SMatthias Ringwald             }
322c8f97be6SMatthias Ringwald             break;
323c8f97be6SMatthias Ringwald 
324c8f97be6SMatthias Ringwald         default:
325c8f97be6SMatthias Ringwald             printf("error\n");
326c8f97be6SMatthias Ringwald             break;
327c8f97be6SMatthias Ringwald     }
328c8f97be6SMatthias Ringwald 
329c8f97be6SMatthias Ringwald }
330c8f97be6SMatthias Ringwald 
33144a1ebc0SMatthias Ringwald // Either connect to remote specified on command line or start scan for device with "LE Streamer" in advertisement
33244a1ebc0SMatthias Ringwald static void le_streamer_client_start(void){
33344a1ebc0SMatthias Ringwald     if (cmdline_addr_found){
33444a1ebc0SMatthias Ringwald         printf("Connect to %s\n", bd_addr_to_str(cmdline_addr));
33544a1ebc0SMatthias Ringwald         state = TC_W4_CONNECT;
33644a1ebc0SMatthias Ringwald         gap_connect(cmdline_addr, 0);
33744a1ebc0SMatthias Ringwald     } else {
33844a1ebc0SMatthias Ringwald         printf("Start scanning!\n");
33944a1ebc0SMatthias Ringwald         state = TC_W4_SCAN_RESULT;
34044a1ebc0SMatthias Ringwald         gap_set_scan_parameters(0,0x0030, 0x0030);
34144a1ebc0SMatthias Ringwald         gap_start_scan();
34244a1ebc0SMatthias Ringwald     }
34344a1ebc0SMatthias Ringwald }
34444a1ebc0SMatthias Ringwald 
345c8f97be6SMatthias Ringwald static void hci_event_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
346c8f97be6SMatthias Ringwald     UNUSED(channel);
347c8f97be6SMatthias Ringwald     UNUSED(size);
348c8f97be6SMatthias Ringwald 
349c8f97be6SMatthias Ringwald     if (packet_type != HCI_EVENT_PACKET) return;
350c8f97be6SMatthias Ringwald 
3518e46c847SMatthias Ringwald     uint16_t conn_interval;
352c8f97be6SMatthias Ringwald     uint8_t event = hci_event_packet_get_type(packet);
353c8f97be6SMatthias Ringwald     switch (event) {
354c8f97be6SMatthias Ringwald         case BTSTACK_EVENT_STATE:
355c8f97be6SMatthias Ringwald             // BTstack activated, get started
356beeed1f3SMatthias Ringwald             if (btstack_event_state_get_state(packet) == HCI_STATE_WORKING) {
35744a1ebc0SMatthias Ringwald                 le_streamer_client_start();
358beeed1f3SMatthias Ringwald             } else {
359beeed1f3SMatthias Ringwald                 state = TC_OFF;
360beeed1f3SMatthias Ringwald             }
361c8f97be6SMatthias Ringwald             break;
362c8f97be6SMatthias Ringwald         case GAP_EVENT_ADVERTISING_REPORT:
363c8f97be6SMatthias Ringwald             if (state != TC_W4_SCAN_RESULT) return;
364c8f97be6SMatthias Ringwald             // check name in advertisement
365c8f97be6SMatthias Ringwald             if (!advertisement_report_contains_name("LE Streamer", packet)) return;
366c8f97be6SMatthias Ringwald             // store address and type
367c8f97be6SMatthias Ringwald             gap_event_advertising_report_get_address(packet, le_streamer_addr);
368c8f97be6SMatthias Ringwald             le_streamer_addr_type = gap_event_advertising_report_get_address_type(packet);
369c8f97be6SMatthias Ringwald             // stop scanning, and connect to the device
370c8f97be6SMatthias Ringwald             state = TC_W4_CONNECT;
371c8f97be6SMatthias Ringwald             gap_stop_scan();
372c8f97be6SMatthias Ringwald             printf("Stop scan. Connect to device with addr %s.\n", bd_addr_to_str(le_streamer_addr));
373c8f97be6SMatthias Ringwald             gap_connect(le_streamer_addr,le_streamer_addr_type);
374c8f97be6SMatthias Ringwald             break;
375c8f97be6SMatthias Ringwald         case HCI_EVENT_LE_META:
376c8f97be6SMatthias Ringwald             // wait for connection complete
377c8f97be6SMatthias Ringwald             if (hci_event_le_meta_get_subevent_code(packet) !=  HCI_SUBEVENT_LE_CONNECTION_COMPLETE) break;
378c8f97be6SMatthias Ringwald             if (state != TC_W4_CONNECT) return;
379c8f97be6SMatthias Ringwald             connection_handle = hci_subevent_le_connection_complete_get_connection_handle(packet);
3808e46c847SMatthias Ringwald             // print connection parameters (without using float operations)
3818e46c847SMatthias Ringwald             conn_interval = hci_subevent_le_connection_complete_get_conn_interval(packet);
3828e46c847SMatthias Ringwald             printf("Connection Interval: %u.%02u ms\n", conn_interval * 125 / 100, 25 * (conn_interval & 3));
3839163ed0fSMatthias Ringwald             printf("Connection Latency: %u\n", hci_subevent_le_connection_complete_get_conn_latency(packet));
384c8f97be6SMatthias Ringwald             // initialize gatt client context with handle, and add it to the list of active clients
385c8f97be6SMatthias Ringwald             // query primary services
386c8f97be6SMatthias Ringwald             printf("Search for LE Streamer service.\n");
387c8f97be6SMatthias Ringwald             state = TC_W4_SERVICE_RESULT;
388c8f97be6SMatthias Ringwald             gatt_client_discover_primary_services_by_uuid128(handle_gatt_client_event, connection_handle, le_streamer_service_uuid);
389c8f97be6SMatthias Ringwald             break;
390c8f97be6SMatthias Ringwald         case HCI_EVENT_DISCONNECTION_COMPLETE:
39144a1ebc0SMatthias Ringwald             // unregister listener
392dc673ce3SMatthias Ringwald             connection_handle = HCI_CON_HANDLE_INVALID;
39344a1ebc0SMatthias Ringwald             if (listener_registered){
39444a1ebc0SMatthias Ringwald                 listener_registered = 0;
39544a1ebc0SMatthias Ringwald                 gatt_client_stop_listening_for_characteristic_value_updates(&notification_listener);
39644a1ebc0SMatthias Ringwald             }
397c8f97be6SMatthias Ringwald             if (cmdline_addr_found){
398c8f97be6SMatthias Ringwald                 printf("Disconnected %s\n", bd_addr_to_str(cmdline_addr));
399c8f97be6SMatthias Ringwald                 return;
400c8f97be6SMatthias Ringwald             }
401c8f97be6SMatthias Ringwald             printf("Disconnected %s\n", bd_addr_to_str(le_streamer_addr));
402beeed1f3SMatthias Ringwald             if (state == TC_OFF) break;
40344a1ebc0SMatthias Ringwald             le_streamer_client_start();
404c8f97be6SMatthias Ringwald             break;
405c8f97be6SMatthias Ringwald         default:
406c8f97be6SMatthias Ringwald             break;
407c8f97be6SMatthias Ringwald     }
408c8f97be6SMatthias Ringwald }
409c8f97be6SMatthias Ringwald 
4107ea7688aSMatthias Ringwald #ifdef HAVE_BTSTACK_STDIN
411c8f97be6SMatthias Ringwald static void usage(const char *name){
412c8f97be6SMatthias Ringwald     fprintf(stderr, "Usage: %s [-a|--address aa:bb:cc:dd:ee:ff]\n", name);
413c8f97be6SMatthias Ringwald     fprintf(stderr, "If no argument is provided, LE Streamer Client will start scanning and connect to the first device named 'LE Streamer'.\n");
414c8f97be6SMatthias Ringwald     fprintf(stderr, "To connect to a specific device use argument [-a].\n\n");
415c8f97be6SMatthias Ringwald }
416c8f97be6SMatthias Ringwald #endif
417c8f97be6SMatthias Ringwald 
418c8f97be6SMatthias Ringwald int btstack_main(int argc, const char * argv[]);
419c8f97be6SMatthias Ringwald int btstack_main(int argc, const char * argv[]){
420c8f97be6SMatthias Ringwald 
4217ea7688aSMatthias Ringwald #ifdef HAVE_BTSTACK_STDIN
422c8f97be6SMatthias Ringwald     int arg = 1;
423c8f97be6SMatthias Ringwald     cmdline_addr_found = 0;
424c8f97be6SMatthias Ringwald 
425c8f97be6SMatthias Ringwald     while (arg < argc) {
426c8f97be6SMatthias Ringwald         if(!strcmp(argv[arg], "-a") || !strcmp(argv[arg], "--address")){
427c8f97be6SMatthias Ringwald             arg++;
428c8f97be6SMatthias Ringwald             cmdline_addr_found = sscanf_bd_addr(argv[arg], cmdline_addr);
429c8f97be6SMatthias Ringwald             arg++;
430c8f97be6SMatthias Ringwald             if (!cmdline_addr_found) exit(1);
431c8f97be6SMatthias Ringwald             continue;
432c8f97be6SMatthias Ringwald         }
433c8f97be6SMatthias Ringwald         usage(argv[0]);
434c8f97be6SMatthias Ringwald         return 0;
435c8f97be6SMatthias Ringwald     }
436c8f97be6SMatthias Ringwald #else
4376316c8edSMatthias Ringwald     (void)argc;
4386316c8edSMatthias Ringwald     (void)argv;
439c8f97be6SMatthias Ringwald #endif
440c8f97be6SMatthias Ringwald     l2cap_init();
441c8f97be6SMatthias Ringwald 
442c8f97be6SMatthias Ringwald     sm_init();
443c8f97be6SMatthias Ringwald     sm_set_io_capabilities(IO_CAPABILITY_NO_INPUT_NO_OUTPUT);
444c8f97be6SMatthias Ringwald 
445bdd0a871SMatthias Ringwald     // sm_init needed before gatt_client_init
446bdd0a871SMatthias Ringwald     gatt_client_init();
447bdd0a871SMatthias Ringwald 
448*a4fe6467SMatthias Ringwald     hci_event_callback_registration.callback = &hci_event_handler;
449*a4fe6467SMatthias Ringwald     hci_add_event_handler(&hci_event_callback_registration);
450*a4fe6467SMatthias Ringwald 
4515ddac272SMatthias Ringwald     // use different connection parameters: conn interval min/max (* 1.25 ms), slave latency, supervision timeout, CE len min/max (* 0.6125 ms)
4525ddac272SMatthias Ringwald     // gap_set_connection_parameters(0x06, 0x06, 4, 1000, 0x01, 0x06 * 2);
4535ddac272SMatthias Ringwald 
454c8f97be6SMatthias Ringwald     // turn on!
455c8f97be6SMatthias Ringwald     hci_power_control(HCI_POWER_ON);
456c8f97be6SMatthias Ringwald 
457c8f97be6SMatthias Ringwald     return 0;
458c8f97be6SMatthias Ringwald }
45915de8206SMilanka Ringwald /* EXAMPLE_END */
460