xref: /btstack/example/le_streamer_client.c (revision 143eca7a03dd8ea5eaa89502a5f42bf9d5d9e350)
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
232fca4dadSMilanka Ringwald  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL BLUEKITCHEN
242fca4dadSMilanka Ringwald  * GMBH 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 
38e501bae0SMatthias Ringwald #define BTSTACK_FILE__ "le_streamer_client.c"
39ab2c6ae4SMatthias Ringwald 
4015de8206SMilanka Ringwald /*
4115de8206SMilanka Ringwald  * le_streamer_client.c
4215de8206SMilanka Ringwald  */
4315de8206SMilanka Ringwald 
44c8f97be6SMatthias Ringwald // *****************************************************************************
45ec8ae085SMilanka Ringwald /* EXAMPLE_START(le_streamer_client): Performance - Stream Data over GATT (Client)
4613820c00SMilanka Ringwald  *
4713820c00SMilanka Ringwald  * @text Connects to 'LE Streamer' and subscribes to test characteristic
4813820c00SMilanka Ringwald  *
4915de8206SMilanka Ringwald  */
50c8f97be6SMatthias Ringwald // *****************************************************************************
51c8f97be6SMatthias Ringwald 
52173fff9bSMatthias Ringwald #include <inttypes.h>
53c8f97be6SMatthias Ringwald #include <stdint.h>
54c8f97be6SMatthias Ringwald #include <stdio.h>
55c8f97be6SMatthias Ringwald #include <stdlib.h>
56c8f97be6SMatthias Ringwald #include <string.h>
57c8f97be6SMatthias Ringwald 
58c8f97be6SMatthias Ringwald #include "btstack.h"
59c8f97be6SMatthias Ringwald 
604d638bb7SMatthias Ringwald typedef struct {
614d638bb7SMatthias Ringwald     char name;
624d638bb7SMatthias Ringwald     int le_notification_enabled;
634d638bb7SMatthias Ringwald     int  counter;
644d638bb7SMatthias Ringwald     char test_data[200];
654d638bb7SMatthias Ringwald     int  test_data_len;
664d638bb7SMatthias Ringwald     uint32_t test_data_sent;
674d638bb7SMatthias Ringwald     uint32_t test_data_start;
684d638bb7SMatthias Ringwald     btstack_context_callback_registration_t write_without_response_request;
694d638bb7SMatthias Ringwald } le_streamer_connection_t;
707fa79385SMatthias Ringwald 
71c8f97be6SMatthias Ringwald typedef enum {
72beeed1f3SMatthias Ringwald     TC_OFF,
73c8f97be6SMatthias Ringwald     TC_IDLE,
74c8f97be6SMatthias Ringwald     TC_W4_SCAN_RESULT,
75c8f97be6SMatthias Ringwald     TC_W4_CONNECT,
76c8f97be6SMatthias Ringwald     TC_W4_SERVICE_RESULT,
775c4f3138SMatthias Ringwald     TC_W4_CHARACTERISTIC_RX_RESULT,
785c4f3138SMatthias Ringwald     TC_W4_CHARACTERISTIC_TX_RESULT,
79dc673ce3SMatthias Ringwald     TC_W4_ENABLE_NOTIFICATIONS_COMPLETE,
80c8f97be6SMatthias Ringwald     TC_W4_TEST_DATA
81c8f97be6SMatthias Ringwald } gc_state_t;
82c8f97be6SMatthias Ringwald 
8398928f8eSMatthias Ringwald static char *const le_streamer_server_name = "LE Streamer";
84c30af2ffSMatthias Ringwald static bd_addr_t cmdline_addr;
85c8f97be6SMatthias Ringwald static int cmdline_addr_found = 0;
86c8f97be6SMatthias Ringwald 
87c8f97be6SMatthias Ringwald // addr and type of device with correct name
88c8f97be6SMatthias Ringwald static bd_addr_t      le_streamer_addr;
89c8f97be6SMatthias Ringwald static bd_addr_type_t le_streamer_addr_type;
90c8f97be6SMatthias Ringwald 
91c8f97be6SMatthias Ringwald static hci_con_handle_t connection_handle;
925c4f3138SMatthias Ringwald 
935c4f3138SMatthias Ringwald // On the GATT Server, RX Characteristic is used for receive data via Write, and TX Characteristic is used to send data via Notifications
94c8f97be6SMatthias 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};
955c4f3138SMatthias 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};
965c4f3138SMatthias 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};
97c8f97be6SMatthias Ringwald 
98c8f97be6SMatthias Ringwald static gatt_client_service_t le_streamer_service;
995c4f3138SMatthias Ringwald static gatt_client_characteristic_t le_streamer_characteristic_rx;
1005c4f3138SMatthias Ringwald static gatt_client_characteristic_t le_streamer_characteristic_tx;
101c8f97be6SMatthias Ringwald 
10244a1ebc0SMatthias Ringwald static gatt_client_notification_t notification_listener;
10344a1ebc0SMatthias Ringwald static int listener_registered;
104c8f97be6SMatthias Ringwald 
105beeed1f3SMatthias Ringwald static gc_state_t state = TC_OFF;
106c8f97be6SMatthias Ringwald static btstack_packet_callback_registration_t hci_event_callback_registration;
107c8f97be6SMatthias Ringwald 
1084d638bb7SMatthias Ringwald // prototypes
1094d638bb7SMatthias Ringwald static void handle_gatt_client_event(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size);
1104d638bb7SMatthias Ringwald static void le_streamer_handle_can_write_without_response(void * context);
1114d638bb7SMatthias Ringwald static void le_streamer_client_request_to_send(le_streamer_connection_t * connection);
1124d638bb7SMatthias Ringwald 
11315304b2dSMatthias Ringwald /*
11415304b2dSMatthias Ringwald  * @section Track throughput
11515304b2dSMatthias Ringwald  * @text We calculate the throughput by setting a start time and measuring the amount of
11615304b2dSMatthias Ringwald  * data sent. After a configurable REPORT_INTERVAL_MS, we print the throughput in kB/s
11715304b2dSMatthias Ringwald  * and reset the counter and start time.
11815304b2dSMatthias Ringwald  */
11915304b2dSMatthias Ringwald 
12015304b2dSMatthias Ringwald /* LISTING_START(tracking): Tracking throughput */
12115304b2dSMatthias Ringwald 
122dc673ce3SMatthias Ringwald #define TEST_MODE_WRITE_WITHOUT_RESPONSE 1
123dc673ce3SMatthias Ringwald #define TEST_MODE_ENABLE_NOTIFICATIONS   2
124dc673ce3SMatthias Ringwald #define TEST_MODE_DUPLEX                 3
125dc673ce3SMatthias Ringwald 
126dc673ce3SMatthias Ringwald // configure test mode: send only, receive only, full duplex
127dc673ce3SMatthias Ringwald #define TEST_MODE TEST_MODE_DUPLEX
128dc673ce3SMatthias Ringwald 
12915304b2dSMatthias Ringwald #define REPORT_INTERVAL_MS 3000
13015304b2dSMatthias Ringwald 
13115304b2dSMatthias Ringwald // support for multiple clients
13215304b2dSMatthias Ringwald static le_streamer_connection_t le_streamer_connection;
13315304b2dSMatthias Ringwald 
test_reset(le_streamer_connection_t * context)13415304b2dSMatthias Ringwald static void test_reset(le_streamer_connection_t * context){
13515304b2dSMatthias Ringwald     context->test_data_start = btstack_run_loop_get_time_ms();
13615304b2dSMatthias Ringwald     context->test_data_sent = 0;
13715304b2dSMatthias Ringwald }
13815304b2dSMatthias Ringwald 
test_track_data(le_streamer_connection_t * context,int bytes_sent)13915304b2dSMatthias Ringwald static void test_track_data(le_streamer_connection_t * context, int bytes_sent){
14015304b2dSMatthias Ringwald     context->test_data_sent += bytes_sent;
14115304b2dSMatthias Ringwald     // evaluate
14215304b2dSMatthias Ringwald     uint32_t now = btstack_run_loop_get_time_ms();
14315304b2dSMatthias Ringwald     uint32_t time_passed = now - context->test_data_start;
14415304b2dSMatthias Ringwald     if (time_passed < REPORT_INTERVAL_MS) return;
14515304b2dSMatthias Ringwald     // print speed
14615304b2dSMatthias Ringwald     int bytes_per_second = context->test_data_sent * 1000 / time_passed;
147173fff9bSMatthias Ringwald     printf("%c: %"PRIu32" bytes -> %u.%03u kB/s\n", context->name, context->test_data_sent, bytes_per_second / 1000, bytes_per_second % 1000);
14815304b2dSMatthias Ringwald 
14915304b2dSMatthias Ringwald     // restart
15015304b2dSMatthias Ringwald     context->test_data_start = now;
15115304b2dSMatthias Ringwald     context->test_data_sent  = 0;
15215304b2dSMatthias Ringwald }
15315304b2dSMatthias Ringwald /* LISTING_END(tracking): Tracking throughput */
15415304b2dSMatthias Ringwald 
15515304b2dSMatthias Ringwald 
1564d638bb7SMatthias Ringwald // streamer
le_streamer_handle_can_write_without_response(void * context)1574d638bb7SMatthias Ringwald static void le_streamer_handle_can_write_without_response(void * context){
1584d638bb7SMatthias Ringwald     le_streamer_connection_t * connection = (le_streamer_connection_t *) context;
1597fa79385SMatthias Ringwald 
1607fa79385SMatthias Ringwald     // create test data
1614d638bb7SMatthias Ringwald     connection->counter++;
1624d638bb7SMatthias Ringwald     if (connection->counter > 'Z') connection->counter = 'A';
1634d638bb7SMatthias Ringwald     memset(connection->test_data, connection->counter, connection->test_data_len);
1647fa79385SMatthias Ringwald 
1657fa79385SMatthias Ringwald     // send
1664d638bb7SMatthias Ringwald     uint8_t status = gatt_client_write_value_of_characteristic_without_response(connection_handle, le_streamer_characteristic_rx.value_handle, connection->test_data_len, (uint8_t*) connection->test_data);
1677fa79385SMatthias Ringwald     if (status){
168fcd55a0bSMilanka Ringwald         printf("Write without response failed, status 0x%02x.\n", status);
1697fa79385SMatthias Ringwald         return;
1707fa79385SMatthias Ringwald     } else {
1714d638bb7SMatthias Ringwald         test_track_data(connection, connection->test_data_len);
1727fa79385SMatthias Ringwald     }
1737fa79385SMatthias Ringwald 
1747fa79385SMatthias Ringwald     // request again
1754d638bb7SMatthias Ringwald     le_streamer_client_request_to_send(connection);
1767fa79385SMatthias Ringwald }
1777fa79385SMatthias Ringwald 
le_streamer_client_request_to_send(le_streamer_connection_t * connection)1784d638bb7SMatthias Ringwald static void le_streamer_client_request_to_send(le_streamer_connection_t * connection){
1794d638bb7SMatthias Ringwald     connection->write_without_response_request.callback = &le_streamer_handle_can_write_without_response;
1804d638bb7SMatthias Ringwald     connection->write_without_response_request.context = connection;
1814d638bb7SMatthias Ringwald     gatt_client_request_to_write_without_response(&connection->write_without_response_request, connection_handle);
1824d638bb7SMatthias Ringwald }
1837fa79385SMatthias Ringwald 
1845d81be33SMatthias Ringwald // returns true if name is found in advertisement
advertisement_contains_name(const char * name,uint8_t adv_len,const uint8_t * adv_data)1855d81be33SMatthias Ringwald static bool advertisement_contains_name(const char * name, uint8_t adv_len, const uint8_t * adv_data){
186c8f97be6SMatthias Ringwald     // get advertisement from report event
1870801ef92SMatthias Ringwald     uint16_t        name_len = (uint8_t) strlen(name);
188c8f97be6SMatthias Ringwald 
189c8f97be6SMatthias Ringwald     // iterate over advertisement data
190c8f97be6SMatthias Ringwald     ad_context_t context;
191c8f97be6SMatthias Ringwald     for (ad_iterator_init(&context, adv_len, adv_data) ; ad_iterator_has_more(&context) ; ad_iterator_next(&context)){
192c8f97be6SMatthias Ringwald         uint8_t data_type    = ad_iterator_get_data_type(&context);
193c8f97be6SMatthias Ringwald         uint8_t data_size    = ad_iterator_get_data_len(&context);
194c8f97be6SMatthias Ringwald         const uint8_t * data = ad_iterator_get_data(&context);
195c8f97be6SMatthias Ringwald         switch (data_type){
196c8f97be6SMatthias Ringwald             case BLUETOOTH_DATA_TYPE_SHORTENED_LOCAL_NAME:
197c8f97be6SMatthias Ringwald             case BLUETOOTH_DATA_TYPE_COMPLETE_LOCAL_NAME:
1988580489cSMatthias Ringwald                 // compare prefix
1998580489cSMatthias Ringwald                 if (data_size < name_len) break;
20057069ed9SMatthias Ringwald                 if (memcmp(data, name, name_len) == 0) return true;
2015d81be33SMatthias Ringwald                 break;
202c8f97be6SMatthias Ringwald             default:
203c8f97be6SMatthias Ringwald                 break;
204c8f97be6SMatthias Ringwald         }
205c8f97be6SMatthias Ringwald     }
2065d81be33SMatthias Ringwald     return false;
207c8f97be6SMatthias Ringwald }
208c8f97be6SMatthias Ringwald 
handle_gatt_client_event(uint8_t packet_type,uint16_t channel,uint8_t * packet,uint16_t size)209c8f97be6SMatthias Ringwald static void handle_gatt_client_event(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
210c8f97be6SMatthias Ringwald     UNUSED(packet_type);
211c8f97be6SMatthias Ringwald     UNUSED(channel);
212c8f97be6SMatthias Ringwald     UNUSED(size);
213c8f97be6SMatthias Ringwald 
214dc673ce3SMatthias Ringwald     uint16_t mtu;
2156058cb0dSMatthias Ringwald     uint8_t att_status;
216c8f97be6SMatthias Ringwald     switch(state){
217c8f97be6SMatthias Ringwald         case TC_W4_SERVICE_RESULT:
218c8f97be6SMatthias Ringwald             switch(hci_event_packet_get_type(packet)){
219c8f97be6SMatthias Ringwald                 case GATT_EVENT_SERVICE_QUERY_RESULT:
220c8f97be6SMatthias Ringwald                     // store service (we expect only one)
221c8f97be6SMatthias Ringwald                     gatt_event_service_query_result_get_service(packet, &le_streamer_service);
222c8f97be6SMatthias Ringwald                     break;
223c8f97be6SMatthias Ringwald                 case GATT_EVENT_QUERY_COMPLETE:
2246058cb0dSMatthias Ringwald                     att_status = gatt_event_query_complete_get_att_status(packet);
2256058cb0dSMatthias Ringwald                     if (att_status != ATT_ERROR_SUCCESS){
226fcd55a0bSMilanka Ringwald                         printf("SERVICE_QUERY_RESULT, ATT Error 0x%02x.\n", att_status);
227c8f97be6SMatthias Ringwald                         gap_disconnect(connection_handle);
228c8f97be6SMatthias Ringwald                         break;
229c8f97be6SMatthias Ringwald                     }
230c8f97be6SMatthias Ringwald                     // service query complete, look for characteristic
2315c4f3138SMatthias Ringwald                     state = TC_W4_CHARACTERISTIC_RX_RESULT;
2325c4f3138SMatthias Ringwald                     printf("Search for LE Streamer RX characteristic.\n");
2335dcb6ca7SMatthias Ringwald                     gatt_client_discover_characteristics_for_service_by_uuid128(handle_gatt_client_event, connection_handle, &le_streamer_service, le_streamer_characteristic_rx_uuid);
234c8f97be6SMatthias Ringwald                     break;
235c8f97be6SMatthias Ringwald                 default:
236c8f97be6SMatthias Ringwald                     break;
237c8f97be6SMatthias Ringwald             }
238c8f97be6SMatthias Ringwald             break;
239c8f97be6SMatthias Ringwald 
2405c4f3138SMatthias Ringwald         case TC_W4_CHARACTERISTIC_RX_RESULT:
241c8f97be6SMatthias Ringwald             switch(hci_event_packet_get_type(packet)){
242c8f97be6SMatthias Ringwald                 case GATT_EVENT_CHARACTERISTIC_QUERY_RESULT:
2435c4f3138SMatthias Ringwald                     gatt_event_characteristic_query_result_get_characteristic(packet, &le_streamer_characteristic_rx);
2445c4f3138SMatthias Ringwald                     break;
2455c4f3138SMatthias Ringwald                 case GATT_EVENT_QUERY_COMPLETE:
2466058cb0dSMatthias Ringwald                     att_status = gatt_event_query_complete_get_att_status(packet);
2476058cb0dSMatthias Ringwald                     if (att_status != ATT_ERROR_SUCCESS){
248fcd55a0bSMilanka Ringwald                         printf("CHARACTERISTIC_QUERY_RESULT, ATT Error 0x%02x.\n", att_status);
2495c4f3138SMatthias Ringwald                         gap_disconnect(connection_handle);
2505c4f3138SMatthias Ringwald                         break;
2515c4f3138SMatthias Ringwald                     }
2525c4f3138SMatthias Ringwald                     // rx characteristiic found, look for tx characteristic
2535dcb6ca7SMatthias Ringwald                     state = TC_W4_CHARACTERISTIC_TX_RESULT;
2545c4f3138SMatthias Ringwald                     printf("Search for LE Streamer TX characteristic.\n");
2555c4f3138SMatthias Ringwald                     gatt_client_discover_characteristics_for_service_by_uuid128(handle_gatt_client_event, connection_handle, &le_streamer_service, le_streamer_characteristic_tx_uuid);
2565c4f3138SMatthias Ringwald                     break;
2575c4f3138SMatthias Ringwald                 default:
2585c4f3138SMatthias Ringwald                     break;
2595c4f3138SMatthias Ringwald             }
2605c4f3138SMatthias Ringwald             break;
2615c4f3138SMatthias Ringwald 
2625c4f3138SMatthias Ringwald         case TC_W4_CHARACTERISTIC_TX_RESULT:
2635c4f3138SMatthias Ringwald             switch(hci_event_packet_get_type(packet)){
2645c4f3138SMatthias Ringwald                 case GATT_EVENT_CHARACTERISTIC_QUERY_RESULT:
2655c4f3138SMatthias Ringwald                     gatt_event_characteristic_query_result_get_characteristic(packet, &le_streamer_characteristic_tx);
266c8f97be6SMatthias Ringwald                     break;
267c8f97be6SMatthias Ringwald                 case GATT_EVENT_QUERY_COMPLETE:
2686058cb0dSMatthias Ringwald                     att_status = gatt_event_query_complete_get_att_status(packet);
2696058cb0dSMatthias Ringwald                     if (att_status != ATT_ERROR_SUCCESS){
270fcd55a0bSMilanka Ringwald                         printf("CHARACTERISTIC_QUERY_RESULT, ATT Error 0x%02x.\n", att_status);
271c8f97be6SMatthias Ringwald                         gap_disconnect(connection_handle);
272c8f97be6SMatthias Ringwald                         break;
273c8f97be6SMatthias Ringwald                     }
274c8f97be6SMatthias Ringwald                     // register handler for notifications
27544a1ebc0SMatthias Ringwald                     listener_registered = 1;
2765c4f3138SMatthias Ringwald                     gatt_client_listen_for_characteristic_value_updates(&notification_listener, handle_gatt_client_event, connection_handle, &le_streamer_characteristic_tx);
27715304b2dSMatthias Ringwald                     // setup tracking
27815304b2dSMatthias Ringwald                     le_streamer_connection.name = 'A';
279dc673ce3SMatthias Ringwald                     le_streamer_connection.test_data_len = ATT_DEFAULT_MTU - 3;
28015304b2dSMatthias Ringwald                     test_reset(&le_streamer_connection);
281dc673ce3SMatthias Ringwald                     gatt_client_get_mtu(connection_handle, &mtu);
282dc673ce3SMatthias Ringwald                     le_streamer_connection.test_data_len = btstack_min(mtu - 3, sizeof(le_streamer_connection.test_data));
283dc673ce3SMatthias Ringwald                     printf("%c: ATT MTU = %u => use test data of len %u\n", le_streamer_connection.name, mtu, le_streamer_connection.test_data_len);
284dc673ce3SMatthias Ringwald                     // enable notifications
285dc673ce3SMatthias Ringwald #if (TEST_MODE & TEST_MODE_ENABLE_NOTIFICATIONS)
286dc673ce3SMatthias Ringwald                     printf("Start streaming - enable notify on test characteristic.\n");
287dc673ce3SMatthias Ringwald                     state = TC_W4_ENABLE_NOTIFICATIONS_COMPLETE;
2887fa79385SMatthias Ringwald                     gatt_client_write_client_characteristic_configuration(handle_gatt_client_event, connection_handle,
2895c4f3138SMatthias Ringwald                         &le_streamer_characteristic_tx, GATT_CLIENT_CHARACTERISTICS_CONFIGURATION_NOTIFICATION);
2907fa79385SMatthias Ringwald                     break;
291dc673ce3SMatthias Ringwald #endif
2927fa79385SMatthias Ringwald                     state = TC_W4_TEST_DATA;
293dc673ce3SMatthias Ringwald #if (TEST_MODE & TEST_MODE_WRITE_WITHOUT_RESPONSE)
294dc673ce3SMatthias Ringwald                     printf("Start streaming - request can send now.\n");
2954d638bb7SMatthias Ringwald                     le_streamer_client_request_to_send(&le_streamer_connection);
296dc673ce3SMatthias Ringwald #endif
297dc673ce3SMatthias Ringwald                     break;
298dc673ce3SMatthias Ringwald                 default:
299dc673ce3SMatthias Ringwald                     break;
300dc673ce3SMatthias Ringwald             }
301dc673ce3SMatthias Ringwald             break;
302dc673ce3SMatthias Ringwald 
303dc673ce3SMatthias Ringwald         case TC_W4_ENABLE_NOTIFICATIONS_COMPLETE:
304dc673ce3SMatthias Ringwald             switch(hci_event_packet_get_type(packet)){
305dc673ce3SMatthias Ringwald                 case GATT_EVENT_QUERY_COMPLETE:
306fcd55a0bSMilanka Ringwald                     printf("Notifications enabled, ATT status 0x%02x\n", gatt_event_query_complete_get_att_status(packet));
3079cb80b17SMilanka Ringwald                     if (gatt_event_query_complete_get_att_status(packet) != ATT_ERROR_SUCCESS) break;
308dc673ce3SMatthias Ringwald                     state = TC_W4_TEST_DATA;
309dc673ce3SMatthias Ringwald #if (TEST_MODE & TEST_MODE_WRITE_WITHOUT_RESPONSE)
3107fa79385SMatthias Ringwald                     printf("Start streaming - request can send now.\n");
3114d638bb7SMatthias Ringwald                     le_streamer_client_request_to_send(&le_streamer_connection);
312dc673ce3SMatthias Ringwald #endif
313c8f97be6SMatthias Ringwald                     break;
314c8f97be6SMatthias Ringwald                 default:
315c8f97be6SMatthias Ringwald                     break;
316c8f97be6SMatthias Ringwald             }
317c8f97be6SMatthias Ringwald             break;
318c8f97be6SMatthias Ringwald 
319c8f97be6SMatthias Ringwald         case TC_W4_TEST_DATA:
320c8f97be6SMatthias Ringwald             switch(hci_event_packet_get_type(packet)){
321c8f97be6SMatthias Ringwald                 case GATT_EVENT_NOTIFICATION:
32215304b2dSMatthias Ringwald                     test_track_data(&le_streamer_connection, gatt_event_notification_get_value_length(packet));
32315304b2dSMatthias Ringwald                     break;
324c8f97be6SMatthias Ringwald                 case GATT_EVENT_QUERY_COMPLETE:
325c8f97be6SMatthias Ringwald                     break;
3267fa79385SMatthias Ringwald                 case GATT_EVENT_CAN_WRITE_WITHOUT_RESPONSE:
3274d638bb7SMatthias Ringwald                     le_streamer_handle_can_write_without_response(&le_streamer_connection);
3287fa79385SMatthias Ringwald                     break;
329c8f97be6SMatthias Ringwald                 default:
330fcd55a0bSMilanka Ringwald                     printf("Unknown packet type 0x%02x\n", hci_event_packet_get_type(packet));
331c8f97be6SMatthias Ringwald                     break;
332c8f97be6SMatthias Ringwald             }
333c8f97be6SMatthias Ringwald             break;
334c8f97be6SMatthias Ringwald 
335c8f97be6SMatthias Ringwald         default:
336c8f97be6SMatthias Ringwald             printf("error\n");
337c8f97be6SMatthias Ringwald             break;
338c8f97be6SMatthias Ringwald     }
339c8f97be6SMatthias Ringwald 
340c8f97be6SMatthias Ringwald }
341c8f97be6SMatthias Ringwald 
34244a1ebc0SMatthias Ringwald // Either connect to remote specified on command line or start scan for device with "LE Streamer" in advertisement
le_streamer_client_start(void)34344a1ebc0SMatthias Ringwald static void le_streamer_client_start(void){
34444a1ebc0SMatthias Ringwald     if (cmdline_addr_found){
34544a1ebc0SMatthias Ringwald         printf("Connect to %s\n", bd_addr_to_str(cmdline_addr));
34644a1ebc0SMatthias Ringwald         state = TC_W4_CONNECT;
34744a1ebc0SMatthias Ringwald         gap_connect(cmdline_addr, 0);
34844a1ebc0SMatthias Ringwald     } else {
34944a1ebc0SMatthias Ringwald         printf("Start scanning!\n");
35044a1ebc0SMatthias Ringwald         state = TC_W4_SCAN_RESULT;
35144a1ebc0SMatthias Ringwald         gap_set_scan_parameters(0,0x0030, 0x0030);
35244a1ebc0SMatthias Ringwald         gap_start_scan();
35344a1ebc0SMatthias Ringwald     }
35444a1ebc0SMatthias Ringwald }
35544a1ebc0SMatthias Ringwald 
le_stream_server_found(void)35698928f8eSMatthias Ringwald static void le_stream_server_found(void) {
35798928f8eSMatthias Ringwald     // stop scanning, and connect to the device
35898928f8eSMatthias Ringwald     state = TC_W4_CONNECT;
35998928f8eSMatthias Ringwald     gap_stop_scan();
36098928f8eSMatthias Ringwald     printf("Stop scan. Connect to device with addr %s.\n", bd_addr_to_str(le_streamer_addr));
36198928f8eSMatthias Ringwald     gap_connect(le_streamer_addr,le_streamer_addr_type);
36298928f8eSMatthias Ringwald }
36398928f8eSMatthias Ringwald 
hci_event_handler(uint8_t packet_type,uint16_t channel,uint8_t * packet,uint16_t size)364c8f97be6SMatthias Ringwald static void hci_event_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
365c8f97be6SMatthias Ringwald     UNUSED(channel);
366c8f97be6SMatthias Ringwald     UNUSED(size);
367c8f97be6SMatthias Ringwald 
368c8f97be6SMatthias Ringwald     if (packet_type != HCI_EVENT_PACKET) return;
369c8f97be6SMatthias Ringwald 
370f4fc15d4SMatthias Ringwald     static const char * const phy_names[] = {
371f4fc15d4SMatthias Ringwald             "1 M", "2 M", "Codec"
372f4fc15d4SMatthias Ringwald     };
373f4fc15d4SMatthias Ringwald 
3748e46c847SMatthias Ringwald     uint16_t conn_interval;
375f4fc15d4SMatthias Ringwald     hci_con_handle_t con_handle;
37698928f8eSMatthias Ringwald     const uint8_t * adv_data;
37798928f8eSMatthias Ringwald     uint8_t         adv_len;
378f4fc15d4SMatthias Ringwald 
379f4fc15d4SMatthias Ringwald     switch (hci_event_packet_get_type(packet)) {
380c8f97be6SMatthias Ringwald         case BTSTACK_EVENT_STATE:
381c8f97be6SMatthias Ringwald             // BTstack activated, get started
382beeed1f3SMatthias Ringwald             if (btstack_event_state_get_state(packet) == HCI_STATE_WORKING) {
38344a1ebc0SMatthias Ringwald                 le_streamer_client_start();
384beeed1f3SMatthias Ringwald             } else {
385beeed1f3SMatthias Ringwald                 state = TC_OFF;
386beeed1f3SMatthias Ringwald             }
387c8f97be6SMatthias Ringwald             break;
388c8f97be6SMatthias Ringwald         case GAP_EVENT_ADVERTISING_REPORT:
389c8f97be6SMatthias Ringwald             if (state != TC_W4_SCAN_RESULT) return;
390c8f97be6SMatthias Ringwald             // check name in advertisement
39198928f8eSMatthias Ringwald             adv_data = gap_event_advertising_report_get_data(packet);
39298928f8eSMatthias Ringwald             adv_len  = gap_event_advertising_report_get_data_length(packet);
39398928f8eSMatthias Ringwald             if (!advertisement_contains_name(le_streamer_server_name, adv_len, adv_data)) return;
39498928f8eSMatthias Ringwald             // match connecting phy
39598928f8eSMatthias Ringwald             gap_set_connection_phys(1);
396c8f97be6SMatthias Ringwald             // store address and type
397c8f97be6SMatthias Ringwald             gap_event_advertising_report_get_address(packet, le_streamer_addr);
398c8f97be6SMatthias Ringwald             le_streamer_addr_type = gap_event_advertising_report_get_address_type(packet);
39998928f8eSMatthias Ringwald             le_stream_server_found();
400c8f97be6SMatthias Ringwald             break;
40198928f8eSMatthias Ringwald #ifdef ENABLE_LE_EXTENDED_ADVERTISING
40298928f8eSMatthias Ringwald         case GAP_EVENT_EXTENDED_ADVERTISING_REPORT:
40398928f8eSMatthias Ringwald             if (state != TC_W4_SCAN_RESULT) return;
40498928f8eSMatthias Ringwald             // check name in advertisement
40598928f8eSMatthias Ringwald             adv_data = gap_event_extended_advertising_report_get_data(packet);
40698928f8eSMatthias Ringwald             adv_len  = gap_event_extended_advertising_report_get_data_length(packet);
40798928f8eSMatthias Ringwald             if (!advertisement_contains_name(le_streamer_server_name, adv_len, adv_data)) return;
40898928f8eSMatthias Ringwald             // match connecting phy
40998928f8eSMatthias Ringwald             if (gap_event_extended_advertising_report_get_primary_phy(packet) == 1){
41098928f8eSMatthias Ringwald                 // LE 1M PHY => use 1M or 2M PHY
41198928f8eSMatthias Ringwald                 gap_set_connection_phys(3);
41298928f8eSMatthias Ringwald             } else {
41398928f8eSMatthias Ringwald                 // Coded PHY => use Coded PHY
41498928f8eSMatthias Ringwald                 gap_set_connection_phys(4);
41598928f8eSMatthias Ringwald             }
41698928f8eSMatthias Ringwald             // store address and type
41798928f8eSMatthias Ringwald             gap_event_extended_advertising_report_get_address(packet, le_streamer_addr);
41898928f8eSMatthias Ringwald             le_streamer_addr_type = gap_event_extended_advertising_report_get_address_type(packet);
41998928f8eSMatthias Ringwald             le_stream_server_found();
42098928f8eSMatthias Ringwald             break;
42198928f8eSMatthias Ringwald #endif
422bba48196SMatthias Ringwald         case HCI_EVENT_META_GAP:
423bba48196SMatthias Ringwald             switch (hci_event_gap_meta_get_subevent_code(packet)) {
424bba48196SMatthias Ringwald                 case GAP_SUBEVENT_LE_CONNECTION_COMPLETE:
425bba48196SMatthias Ringwald                     switch (hci_event_gap_meta_get_subevent_code(packet)) {
426bba48196SMatthias Ringwald                         case GAP_SUBEVENT_LE_CONNECTION_COMPLETE:
427c8f97be6SMatthias Ringwald                             if (state != TC_W4_CONNECT) return;
428bba48196SMatthias Ringwald                             connection_handle = gap_subevent_le_connection_complete_get_connection_handle(packet);
4298e46c847SMatthias Ringwald                             // print connection parameters (without using float operations)
430bba48196SMatthias Ringwald                             conn_interval = gap_subevent_le_connection_complete_get_conn_interval(packet);
4318e46c847SMatthias Ringwald                             printf("Connection Interval: %u.%02u ms\n", conn_interval * 125 / 100, 25 * (conn_interval & 3));
432bba48196SMatthias Ringwald                             printf("Connection Latency: %u\n", gap_subevent_le_connection_complete_get_conn_latency(packet));
433c8f97be6SMatthias Ringwald                             // initialize gatt client context with handle, and add it to the list of active clients
434c8f97be6SMatthias Ringwald                             // query primary services
435c8f97be6SMatthias Ringwald                             printf("Search for LE Streamer service.\n");
436c8f97be6SMatthias Ringwald                             state = TC_W4_SERVICE_RESULT;
4375dcb6ca7SMatthias Ringwald                             gatt_client_discover_primary_services_by_uuid128(handle_gatt_client_event, connection_handle, le_streamer_service_uuid);
438c8f97be6SMatthias Ringwald                             break;
439bba48196SMatthias Ringwald                         default:
440bba48196SMatthias Ringwald                             break;
441bba48196SMatthias Ringwald                     }
442bba48196SMatthias Ringwald                     break;
443bba48196SMatthias Ringwald                 default:
444bba48196SMatthias Ringwald                     break;
445bba48196SMatthias Ringwald             }
446bba48196SMatthias Ringwald             break;
447bba48196SMatthias Ringwald         case HCI_EVENT_LE_META:
448bba48196SMatthias Ringwald             switch (hci_event_le_meta_get_subevent_code(packet)){
449f4fc15d4SMatthias Ringwald                 case HCI_SUBEVENT_LE_DATA_LENGTH_CHANGE:
450f4fc15d4SMatthias Ringwald                     con_handle = hci_subevent_le_data_length_change_get_connection_handle(packet);
451f4fc15d4SMatthias Ringwald                     printf("- LE Connection 0x%04x: data length change - max %u bytes per packet\n", con_handle,
452f4fc15d4SMatthias Ringwald                            hci_subevent_le_data_length_change_get_max_tx_octets(packet));
453f4fc15d4SMatthias Ringwald                     break;
454f4fc15d4SMatthias Ringwald                 case HCI_SUBEVENT_LE_PHY_UPDATE_COMPLETE:
455f4fc15d4SMatthias Ringwald                     con_handle = hci_subevent_le_phy_update_complete_get_connection_handle(packet);
456f4fc15d4SMatthias Ringwald                     printf("- LE Connection 0x%04x: PHY update - using LE %s PHY now\n", con_handle,
457*143eca7aSTobias Müller                            phy_names[hci_subevent_le_phy_update_complete_get_tx_phy(packet) - 1]);
458f4fc15d4SMatthias Ringwald                     break;
459f4fc15d4SMatthias Ringwald                 default:
460f4fc15d4SMatthias Ringwald                     break;
461f4fc15d4SMatthias Ringwald             }
462f4fc15d4SMatthias Ringwald             break;
463c8f97be6SMatthias Ringwald         case HCI_EVENT_DISCONNECTION_COMPLETE:
46444a1ebc0SMatthias Ringwald             // unregister listener
465dc673ce3SMatthias Ringwald             connection_handle = HCI_CON_HANDLE_INVALID;
46644a1ebc0SMatthias Ringwald             if (listener_registered){
46744a1ebc0SMatthias Ringwald                 listener_registered = 0;
46844a1ebc0SMatthias Ringwald                 gatt_client_stop_listening_for_characteristic_value_updates(&notification_listener);
46944a1ebc0SMatthias Ringwald             }
470c8f97be6SMatthias Ringwald             if (cmdline_addr_found){
471c8f97be6SMatthias Ringwald                 printf("Disconnected %s\n", bd_addr_to_str(cmdline_addr));
472c8f97be6SMatthias Ringwald                 return;
473c8f97be6SMatthias Ringwald             }
474c8f97be6SMatthias Ringwald             printf("Disconnected %s\n", bd_addr_to_str(le_streamer_addr));
475beeed1f3SMatthias Ringwald             if (state == TC_OFF) break;
47644a1ebc0SMatthias Ringwald             le_streamer_client_start();
477c8f97be6SMatthias Ringwald             break;
478c8f97be6SMatthias Ringwald         default:
479c8f97be6SMatthias Ringwald             break;
480c8f97be6SMatthias Ringwald     }
481c8f97be6SMatthias Ringwald }
482c8f97be6SMatthias Ringwald 
4837ea7688aSMatthias Ringwald #ifdef HAVE_BTSTACK_STDIN
usage(const char * name)484c8f97be6SMatthias Ringwald static void usage(const char *name){
485c8f97be6SMatthias Ringwald     fprintf(stderr, "Usage: %s [-a|--address aa:bb:cc:dd:ee:ff]\n", name);
486c8f97be6SMatthias Ringwald     fprintf(stderr, "If no argument is provided, LE Streamer Client will start scanning and connect to the first device named 'LE Streamer'.\n");
487c8f97be6SMatthias Ringwald     fprintf(stderr, "To connect to a specific device use argument [-a].\n\n");
488c8f97be6SMatthias Ringwald }
489c8f97be6SMatthias Ringwald #endif
490c8f97be6SMatthias Ringwald 
491c8f97be6SMatthias Ringwald int btstack_main(int argc, const char * argv[]);
btstack_main(int argc,const char * argv[])492c8f97be6SMatthias Ringwald int btstack_main(int argc, const char * argv[]){
493c8f97be6SMatthias Ringwald 
4942e80d8ecSSteven Buytaert 
4957ea7688aSMatthias Ringwald #ifdef HAVE_BTSTACK_STDIN
4962e80d8ecSSteven Buytaert     int arg;
497c8f97be6SMatthias Ringwald     cmdline_addr_found = 0;
498c8f97be6SMatthias Ringwald 
4992e80d8ecSSteven Buytaert     for (arg = 1; arg < argc; arg++) {
500c8f97be6SMatthias Ringwald         if(!strcmp(argv[arg], "-a") || !strcmp(argv[arg], "--address")){
5012e80d8ecSSteven Buytaert             if (arg + 1 < argc) {
502c8f97be6SMatthias Ringwald                 arg++;
503c8f97be6SMatthias Ringwald                 cmdline_addr_found = sscanf_bd_addr(argv[arg], cmdline_addr);
504c8f97be6SMatthias Ringwald             }
5052e80d8ecSSteven Buytaert             if (!cmdline_addr_found) {
506c8f97be6SMatthias Ringwald                 usage(argv[0]);
5072e80d8ecSSteven Buytaert                 return 1;
5082e80d8ecSSteven Buytaert             }
5092e80d8ecSSteven Buytaert         }
5102e80d8ecSSteven Buytaert     }
5112e80d8ecSSteven Buytaert     if (!cmdline_addr_found) {
5122e80d8ecSSteven Buytaert         fprintf(stderr, "No specific address specified or found; start scanning for 'LE Streamer' advertisement.\n");
513c8f97be6SMatthias Ringwald     }
514c8f97be6SMatthias Ringwald #else
5156316c8edSMatthias Ringwald     (void)argc;
5166316c8edSMatthias Ringwald     (void)argv;
517c8f97be6SMatthias Ringwald #endif
518c8f97be6SMatthias Ringwald     l2cap_init();
519c8f97be6SMatthias Ringwald 
520c8f97be6SMatthias Ringwald     sm_init();
521c8f97be6SMatthias Ringwald     sm_set_io_capabilities(IO_CAPABILITY_NO_INPUT_NO_OUTPUT);
522c8f97be6SMatthias Ringwald 
523bdd0a871SMatthias Ringwald     // sm_init needed before gatt_client_init
524bdd0a871SMatthias Ringwald     gatt_client_init();
525bdd0a871SMatthias Ringwald 
526a4fe6467SMatthias Ringwald     hci_event_callback_registration.callback = &hci_event_handler;
527a4fe6467SMatthias Ringwald     hci_add_event_handler(&hci_event_callback_registration);
528a4fe6467SMatthias Ringwald 
5295ddac272SMatthias Ringwald     // use different connection parameters: conn interval min/max (* 1.25 ms), slave latency, supervision timeout, CE len min/max (* 0.6125 ms)
5305ddac272SMatthias Ringwald     // gap_set_connection_parameters(0x06, 0x06, 4, 1000, 0x01, 0x06 * 2);
5315ddac272SMatthias Ringwald 
53298928f8eSMatthias Ringwald #ifdef ENABLE_LE_EXTENDED_ADVERTISING
53398928f8eSMatthias Ringwald     // scan on Coded and 1M PHYs
5345f5e5437SMatthias Ringwald     gap_set_scan_phys(5);
53598928f8eSMatthias Ringwald #endif
53698928f8eSMatthias Ringwald 
537c8f97be6SMatthias Ringwald     // turn on!
538c8f97be6SMatthias Ringwald     hci_power_control(HCI_POWER_ON);
539c8f97be6SMatthias Ringwald 
540c8f97be6SMatthias Ringwald     return 0;
541c8f97be6SMatthias Ringwald }
54215de8206SMilanka Ringwald /* EXAMPLE_END */
543