xref: /btstack/example/le_streamer_client.c (revision 8e46c84798e030b354253c956ea8a6776bbbe96c)
1 /*
2  * Copyright (C) 2014 BlueKitchen GmbH
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  *
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. Neither the name of the copyright holders nor the names of
14  *    contributors may be used to endorse or promote products derived
15  *    from this software without specific prior written permission.
16  * 4. Any redistribution, use, or modification is done solely for
17  *    personal benefit and not for any commercial purpose or for
18  *    monetary gain.
19  *
20  * THIS SOFTWARE IS PROVIDED BY BLUEKITCHEN GMBH AND CONTRIBUTORS
21  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL MATTHIAS
24  * RINGWALD OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
25  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
26  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
27  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
28  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
29  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
30  * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  *
33  * Please inquire about commercial licensing options at
34  * [email protected]
35  *
36  */
37 
38 #define __BTSTACK_FILE__ "le_streamer_client.c"
39 
40 // *****************************************************************************
41 //
42 // LE Streamer Client - connects to 'LE Streamer' and subscribes to test characteristic
43 //
44 // *****************************************************************************
45 
46 #include <stdint.h>
47 #include <stdio.h>
48 #include <stdlib.h>
49 #include <string.h>
50 
51 #include "btstack.h"
52 
53 typedef enum {
54     TC_IDLE,
55     TC_W4_SCAN_RESULT,
56     TC_W4_CONNECT,
57     TC_W4_SERVICE_RESULT,
58     TC_W4_CHARACTERISTIC_RESULT,
59     TC_W4_TEST_DATA
60 } gc_state_t;
61 
62 static bd_addr_t cmdline_addr = { };
63 static int cmdline_addr_found = 0;
64 
65 // addr and type of device with correct name
66 static bd_addr_t      le_streamer_addr;
67 static bd_addr_type_t le_streamer_addr_type;
68 
69 static hci_con_handle_t connection_handle;
70 static uint8_t le_streamer_service_uuid[16]        = { 0x00, 0x00, 0xFF, 0x10, 0x00, 0x00, 0x10, 0x00, 0x80, 0x00, 0x00, 0x80, 0x5F, 0x9B, 0x34, 0xFB};
71 static uint8_t le_streamer_characteristic_uuid[16] = { 0x00, 0x00, 0xFF, 0x11, 0x00, 0x00, 0x10, 0x00, 0x80, 0x00, 0x00, 0x80, 0x5F, 0x9B, 0x34, 0xFB};
72 
73 static gatt_client_service_t le_streamer_service;
74 static gatt_client_characteristic_t le_streamer_characteristic;
75 
76 static gatt_client_notification_t notification_registration;
77 
78 static gc_state_t state = TC_IDLE;
79 static btstack_packet_callback_registration_t hci_event_callback_registration;
80 
81 // returns 1 if name is found in advertisement
82 static int advertisement_report_contains_name(const char * name, uint8_t * advertisement_report){
83     // get advertisement from report event
84     const uint8_t * adv_data = gap_event_advertising_report_get_data(advertisement_report);
85     uint16_t        adv_len  = gap_event_advertising_report_get_data_length(advertisement_report);
86     int             name_len = strlen(name);
87 
88     // iterate over advertisement data
89     ad_context_t context;
90     for (ad_iterator_init(&context, adv_len, adv_data) ; ad_iterator_has_more(&context) ; ad_iterator_next(&context)){
91         uint8_t data_type    = ad_iterator_get_data_type(&context);
92         uint8_t data_size    = ad_iterator_get_data_len(&context);
93         const uint8_t * data = ad_iterator_get_data(&context);
94         int i;
95         switch (data_type){
96             case BLUETOOTH_DATA_TYPE_SHORTENED_LOCAL_NAME:
97             case BLUETOOTH_DATA_TYPE_COMPLETE_LOCAL_NAME:
98                 // compare common prefix
99                 for (i=0; i<data_size && i<name_len;i++){
100                     if (data[i] != name[i]) break;
101                 }
102                 // prefix match
103                 return 1;
104             default:
105                 break;
106         }
107     }
108     return 0;
109 }
110 
111 static void handle_gatt_client_event(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
112     UNUSED(packet_type);
113     UNUSED(channel);
114     UNUSED(size);
115 
116     switch(state){
117         case TC_W4_SERVICE_RESULT:
118             switch(hci_event_packet_get_type(packet)){
119                 case GATT_EVENT_SERVICE_QUERY_RESULT:
120                     // store service (we expect only one)
121                     gatt_event_service_query_result_get_service(packet, &le_streamer_service);
122                     break;
123                 case GATT_EVENT_QUERY_COMPLETE:
124                     if (packet[4] != 0){
125                         printf("SERVICE_QUERY_RESULT - Error status %x.\n", packet[4]);
126                         gap_disconnect(connection_handle);
127                         break;
128                     }
129                     // service query complete, look for characteristic
130                     state = TC_W4_CHARACTERISTIC_RESULT;
131                     printf("Search for LE Streamer test characteristic.\n");
132                     gatt_client_discover_characteristics_for_service_by_uuid128(handle_gatt_client_event, connection_handle, &le_streamer_service, le_streamer_characteristic_uuid);
133                     break;
134                 default:
135                     break;
136             }
137             break;
138 
139         case TC_W4_CHARACTERISTIC_RESULT:
140             switch(hci_event_packet_get_type(packet)){
141                 case GATT_EVENT_CHARACTERISTIC_QUERY_RESULT:
142                     gatt_event_characteristic_query_result_get_characteristic(packet, &le_streamer_characteristic);
143                     break;
144                 case GATT_EVENT_QUERY_COMPLETE:
145                     if (packet[4] != 0){
146                         printf("CHARACTERISTIC_QUERY_RESULT - Error status %x.\n", packet[4]);
147                         gap_disconnect(connection_handle);
148                         break;
149                     }
150                     // register handler for notifications
151                     gatt_client_listen_for_characteristic_value_updates(&notification_registration, handle_gatt_client_event, connection_handle, &le_streamer_characteristic);
152                     // enable notifications
153                     state = TC_W4_TEST_DATA;
154                     printf("Start streaming - enable notify on test characteristic.\n");
155                     gatt_client_write_client_characteristic_configuration(handle_gatt_client_event, connection_handle, &le_streamer_characteristic, GATT_CLIENT_CHARACTERISTICS_CONFIGURATION_NOTIFICATION);
156                     break;
157                 default:
158                     break;
159             }
160             break;
161 
162         case TC_W4_TEST_DATA:
163             switch(hci_event_packet_get_type(packet)){
164                 case GATT_EVENT_NOTIFICATION:
165                     printf("Data: ");
166                     printf_hexdump( gatt_event_notification_get_value(packet), gatt_event_notification_get_value_length(packet));
167                 case GATT_EVENT_QUERY_COMPLETE:
168                     break;
169                 default:
170                     printf("Unknown packet type %x\n", hci_event_packet_get_type(packet));
171                     break;
172             }
173             break;
174 
175         default:
176             printf("error\n");
177             break;
178     }
179 
180 }
181 
182 static void hci_event_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
183     UNUSED(channel);
184     UNUSED(size);
185 
186     if (packet_type != HCI_EVENT_PACKET) return;
187 
188     uint16_t conn_interval;
189     uint8_t event = hci_event_packet_get_type(packet);
190     switch (event) {
191         case BTSTACK_EVENT_STATE:
192             // BTstack activated, get started
193             if (btstack_event_state_get_state(packet) != HCI_STATE_WORKING) break;
194             if (cmdline_addr_found){
195                 printf("Connect to %s\n", bd_addr_to_str(cmdline_addr));
196                 state = TC_W4_CONNECT;
197                 gap_connect(cmdline_addr, 0);
198                 break;
199             }
200             printf("Start scanning!\n");
201             state = TC_W4_SCAN_RESULT;
202             gap_set_scan_parameters(0,0x0030, 0x0030);
203             gap_start_scan();
204             break;
205         case GAP_EVENT_ADVERTISING_REPORT:
206             if (state != TC_W4_SCAN_RESULT) return;
207             // check name in advertisement
208             if (!advertisement_report_contains_name("LE Streamer", packet)) return;
209             // store address and type
210             gap_event_advertising_report_get_address(packet, le_streamer_addr);
211             le_streamer_addr_type = gap_event_advertising_report_get_address_type(packet);
212             // stop scanning, and connect to the device
213             state = TC_W4_CONNECT;
214             gap_stop_scan();
215             printf("Stop scan. Connect to device with addr %s.\n", bd_addr_to_str(le_streamer_addr));
216             gap_connect(le_streamer_addr,le_streamer_addr_type);
217             break;
218         case HCI_EVENT_LE_META:
219             // wait for connection complete
220             if (hci_event_le_meta_get_subevent_code(packet) !=  HCI_SUBEVENT_LE_CONNECTION_COMPLETE) break;
221             if (state != TC_W4_CONNECT) return;
222             connection_handle = hci_subevent_le_connection_complete_get_connection_handle(packet);
223             // print connection parameters (without using float operations)
224             conn_interval = hci_subevent_le_connection_complete_get_conn_interval(packet);
225             printf("Connection Interval: %u.%02u ms\n", conn_interval * 125 / 100, 25 * (conn_interval & 3));
226             printf("Connection Latency: %u\n", hci_subevent_le_connection_complete_get_conn_latency(packet));                            break;
227             // initialize gatt client context with handle, and add it to the list of active clients
228             // query primary services
229             printf("Search for LE Streamer service.\n");
230             state = TC_W4_SERVICE_RESULT;
231             gatt_client_discover_primary_services_by_uuid128(handle_gatt_client_event, connection_handle, le_streamer_service_uuid);
232             break;
233         case HCI_EVENT_DISCONNECTION_COMPLETE:
234             if (cmdline_addr_found){
235                 printf("Disconnected %s\n", bd_addr_to_str(cmdline_addr));
236                 return;
237             }
238             printf("Disconnected %s\n", bd_addr_to_str(le_streamer_addr));
239             break;
240         default:
241             break;
242     }
243 }
244 
245 #ifdef HAVE_BTSTACK_STDIN
246 static void usage(const char *name){
247     fprintf(stderr, "Usage: %s [-a|--address aa:bb:cc:dd:ee:ff]\n", name);
248     fprintf(stderr, "If no argument is provided, LE Streamer Client will start scanning and connect to the first device named 'LE Streamer'.\n");
249     fprintf(stderr, "To connect to a specific device use argument [-a].\n\n");
250 }
251 #endif
252 
253 int btstack_main(int argc, const char * argv[]);
254 int btstack_main(int argc, const char * argv[]){
255 
256 #ifdef HAVE_BTSTACK_STDIN
257     int arg = 1;
258     cmdline_addr_found = 0;
259 
260     while (arg < argc) {
261         if(!strcmp(argv[arg], "-a") || !strcmp(argv[arg], "--address")){
262             arg++;
263             cmdline_addr_found = sscanf_bd_addr(argv[arg], cmdline_addr);
264             arg++;
265             if (!cmdline_addr_found) exit(1);
266             continue;
267         }
268         usage(argv[0]);
269         return 0;
270     }
271 #else
272     (void)argc;
273     (void)argv;
274 #endif
275 
276     hci_event_callback_registration.callback = &hci_event_handler;
277     hci_add_event_handler(&hci_event_callback_registration);
278 
279     l2cap_init();
280 
281     gatt_client_init();
282 
283     sm_init();
284     sm_set_io_capabilities(IO_CAPABILITY_NO_INPUT_NO_OUTPUT);
285 
286     // turn on!
287     hci_power_control(HCI_POWER_ON);
288 
289     return 0;
290 }
291