xref: /btstack/example/gatt_battery_query.c (revision 0f09bd9698afb66ceb5189da0275ad8994d5cf47)
1bcf00d8fSMatthias Ringwald /*
2bcf00d8fSMatthias Ringwald  * Copyright (C) 2014 BlueKitchen GmbH
3bcf00d8fSMatthias Ringwald  *
4bcf00d8fSMatthias Ringwald  * Redistribution and use in source and binary forms, with or without
5bcf00d8fSMatthias Ringwald  * modification, are permitted provided that the following conditions
6bcf00d8fSMatthias Ringwald  * are met:
7bcf00d8fSMatthias Ringwald  *
8bcf00d8fSMatthias Ringwald  * 1. Redistributions of source code must retain the above copyright
9bcf00d8fSMatthias Ringwald  *    notice, this list of conditions and the following disclaimer.
10bcf00d8fSMatthias Ringwald  * 2. Redistributions in binary form must reproduce the above copyright
11bcf00d8fSMatthias Ringwald  *    notice, this list of conditions and the following disclaimer in the
12bcf00d8fSMatthias Ringwald  *    documentation and/or other materials provided with the distribution.
13bcf00d8fSMatthias Ringwald  * 3. Neither the name of the copyright holders nor the names of
14bcf00d8fSMatthias Ringwald  *    contributors may be used to endorse or promote products derived
15bcf00d8fSMatthias Ringwald  *    from this software without specific prior written permission.
16bcf00d8fSMatthias Ringwald  * 4. Any redistribution, use, or modification is done solely for
17bcf00d8fSMatthias Ringwald  *    personal benefit and not for any commercial purpose or for
18bcf00d8fSMatthias Ringwald  *    monetary gain.
19bcf00d8fSMatthias Ringwald  *
20bcf00d8fSMatthias Ringwald  * THIS SOFTWARE IS PROVIDED BY BLUEKITCHEN GMBH AND CONTRIBUTORS
21bcf00d8fSMatthias Ringwald  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22bcf00d8fSMatthias Ringwald  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23bcf00d8fSMatthias Ringwald  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL MATTHIAS
24bcf00d8fSMatthias Ringwald  * RINGWALD OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
25bcf00d8fSMatthias Ringwald  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
26bcf00d8fSMatthias Ringwald  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
27bcf00d8fSMatthias Ringwald  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
28bcf00d8fSMatthias Ringwald  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
29bcf00d8fSMatthias Ringwald  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
30bcf00d8fSMatthias Ringwald  * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31bcf00d8fSMatthias Ringwald  * SUCH DAMAGE.
32bcf00d8fSMatthias Ringwald  *
33bcf00d8fSMatthias Ringwald  * Please inquire about commercial licensing options at
34bcf00d8fSMatthias Ringwald  * [email protected]
35bcf00d8fSMatthias Ringwald  *
36bcf00d8fSMatthias Ringwald  */
37bcf00d8fSMatthias Ringwald 
38bcf00d8fSMatthias Ringwald // *****************************************************************************
39bcf00d8fSMatthias Ringwald //
40bcf00d8fSMatthias Ringwald // BLE Client
41bcf00d8fSMatthias Ringwald //
42bcf00d8fSMatthias Ringwald // *****************************************************************************
43bcf00d8fSMatthias Ringwald 
44bcf00d8fSMatthias Ringwald #include <stdint.h>
45bcf00d8fSMatthias Ringwald #include <stdio.h>
46bcf00d8fSMatthias Ringwald #include <stdlib.h>
47bcf00d8fSMatthias Ringwald #include <string.h>
48bcf00d8fSMatthias Ringwald 
49bcf00d8fSMatthias Ringwald #include "btstack.h"
50bcf00d8fSMatthias Ringwald 
51bcf00d8fSMatthias Ringwald typedef struct advertising_report {
52bcf00d8fSMatthias Ringwald     uint8_t   type;
53bcf00d8fSMatthias Ringwald     uint8_t   event_type;
54bcf00d8fSMatthias Ringwald     uint8_t   address_type;
55bcf00d8fSMatthias Ringwald     bd_addr_t address;
56bcf00d8fSMatthias Ringwald     uint8_t   rssi;
57bcf00d8fSMatthias Ringwald     uint8_t   length;
583ee82ab1SMilanka Ringwald     const uint8_t * data;
59bcf00d8fSMatthias Ringwald } advertising_report_t;
60bcf00d8fSMatthias Ringwald 
61bcf00d8fSMatthias Ringwald 
62bcf00d8fSMatthias Ringwald typedef enum {
63bcf00d8fSMatthias Ringwald     TC_IDLE,
64bcf00d8fSMatthias Ringwald     TC_W4_SCAN_RESULT,
65bcf00d8fSMatthias Ringwald     TC_W4_CONNECT,
66bcf00d8fSMatthias Ringwald     TC_W4_SERVICE_RESULT,
67bcf00d8fSMatthias Ringwald     TC_W4_CHARACTERISTIC_RESULT,
68bcf00d8fSMatthias Ringwald     TC_W4_BATTERY_DATA
69bcf00d8fSMatthias Ringwald } gc_state_t;
70bcf00d8fSMatthias Ringwald 
71*0f09bd96SMilanka Ringwald static int blacklist_index = 0;
72*0f09bd96SMilanka Ringwald static bd_addr_t blacklist[20];
73*0f09bd96SMilanka Ringwald static advertising_report_t report;
74*0f09bd96SMilanka Ringwald 
75bcf00d8fSMatthias Ringwald static void handle_gatt_client_event(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size);
76bcf00d8fSMatthias Ringwald 
77bcf00d8fSMatthias Ringwald static bd_addr_t cmdline_addr = { };
78bcf00d8fSMatthias Ringwald static int cmdline_addr_found = 0;
79bcf00d8fSMatthias Ringwald 
80a59bfbf7SMatthias Ringwald static hci_con_handle_t connection_handle;
81bcf00d8fSMatthias Ringwald static uint16_t battery_service_uuid = 0x180F;
82bcf00d8fSMatthias Ringwald static uint16_t battery_level_characteristic_uuid = 0x2a19;
83bcf00d8fSMatthias Ringwald static gatt_client_service_t battery_service;
84bcf00d8fSMatthias Ringwald static gatt_client_characteristic_t config_characteristic;
85bcf00d8fSMatthias Ringwald 
86bcf00d8fSMatthias Ringwald static gc_state_t state = TC_IDLE;
87bcf00d8fSMatthias Ringwald static btstack_packet_callback_registration_t hci_event_callback_registration;
88bcf00d8fSMatthias Ringwald 
89bcf00d8fSMatthias Ringwald static void printUUID(uint8_t * uuid128, uint16_t uuid16){
90bcf00d8fSMatthias Ringwald     if (uuid16){
91bcf00d8fSMatthias Ringwald         printf("%04x",uuid16);
92bcf00d8fSMatthias Ringwald     } else {
93bcf00d8fSMatthias Ringwald         printf("%s", uuid128_to_str(uuid128));
94bcf00d8fSMatthias Ringwald     }
95bcf00d8fSMatthias Ringwald }
96bcf00d8fSMatthias Ringwald 
97*0f09bd96SMilanka Ringwald static int blacklist_size(){
98*0f09bd96SMilanka Ringwald     return sizeof(blacklist) / sizeof(bd_addr_t);
99*0f09bd96SMilanka Ringwald }
100*0f09bd96SMilanka Ringwald 
101*0f09bd96SMilanka Ringwald static int blacklist_contains(bd_addr_t addr){
102*0f09bd96SMilanka Ringwald     int i;
103*0f09bd96SMilanka Ringwald     for (i=0; i<blacklist_size(); i++){
104*0f09bd96SMilanka Ringwald         if (bd_addr_cmp(addr, blacklist[i]) == 0) return 1;
105*0f09bd96SMilanka Ringwald     }
106*0f09bd96SMilanka Ringwald     return 0;
107*0f09bd96SMilanka Ringwald }
108*0f09bd96SMilanka Ringwald 
109*0f09bd96SMilanka Ringwald static void add_to_blacklist(bd_addr_t addr){
110*0f09bd96SMilanka Ringwald     printf("%s added to blacklist (no battery service found\n", bd_addr_to_str(addr));
111*0f09bd96SMilanka Ringwald     bd_addr_copy(blacklist[blacklist_index], addr);
112*0f09bd96SMilanka Ringwald     blacklist_index = (blacklist_index + 1) % blacklist_size();
113*0f09bd96SMilanka Ringwald }
114*0f09bd96SMilanka Ringwald 
115bcf00d8fSMatthias Ringwald static void dump_advertising_report(advertising_report_t * e){
116bcf00d8fSMatthias Ringwald     printf("    * adv. event: evt-type %u, addr-type %u, addr %s, rssi %u, length adv %u, data: ", e->event_type,
117bcf00d8fSMatthias Ringwald            e->address_type, bd_addr_to_str(e->address), e->rssi, e->length);
118bcf00d8fSMatthias Ringwald     printf_hexdump(e->data, e->length);
119bcf00d8fSMatthias Ringwald 
120bcf00d8fSMatthias Ringwald }
121bcf00d8fSMatthias Ringwald 
122bcf00d8fSMatthias Ringwald static void dump_characteristic_value(uint8_t * blob, uint16_t blob_length){
123bcf00d8fSMatthias Ringwald     printf("    * characteristic value of length %d *** ", blob_length );
124bcf00d8fSMatthias Ringwald     printf_hexdump(blob , blob_length);
125bcf00d8fSMatthias Ringwald     printf("\n");
126bcf00d8fSMatthias Ringwald }
127bcf00d8fSMatthias Ringwald 
128bcf00d8fSMatthias Ringwald static void dump_characteristic(gatt_client_characteristic_t * characteristic){
129bcf00d8fSMatthias Ringwald     printf("    * characteristic: [0x%04x-0x%04x-0x%04x], properties 0x%02x, uuid ",
130bcf00d8fSMatthias Ringwald                             characteristic->start_handle, characteristic->value_handle, characteristic->end_handle, characteristic->properties);
131bcf00d8fSMatthias Ringwald     printUUID(characteristic->uuid128, characteristic->uuid16);
132bcf00d8fSMatthias Ringwald     printf("\n");
133bcf00d8fSMatthias Ringwald }
134bcf00d8fSMatthias Ringwald 
135bcf00d8fSMatthias Ringwald static void dump_service(gatt_client_service_t * service){
136bcf00d8fSMatthias Ringwald     printf("    * service: [0x%04x-0x%04x], uuid ", service->start_group_handle, service->end_group_handle);
137bcf00d8fSMatthias Ringwald     printUUID(service->uuid128, service->uuid16);
138bcf00d8fSMatthias Ringwald     printf("\n");
139bcf00d8fSMatthias Ringwald }
140bcf00d8fSMatthias Ringwald 
141bcf00d8fSMatthias Ringwald 
142bcf00d8fSMatthias Ringwald static void handle_gatt_client_event(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
143*0f09bd96SMilanka Ringwald     int status;
144*0f09bd96SMilanka Ringwald     uint8_t battery_level;
145bcf00d8fSMatthias Ringwald 
146bcf00d8fSMatthias Ringwald     switch(state){
147bcf00d8fSMatthias Ringwald         case TC_W4_SERVICE_RESULT:
1480e2df43fSMatthias Ringwald             switch(hci_event_packet_get_type(packet)){
149bcf00d8fSMatthias Ringwald                 case GATT_EVENT_SERVICE_QUERY_RESULT:
150bcf00d8fSMatthias Ringwald                     gatt_event_service_query_result_get_service(packet, &battery_service);
151bcf00d8fSMatthias Ringwald                     dump_service(&battery_service);
152bcf00d8fSMatthias Ringwald                     break;
153bcf00d8fSMatthias Ringwald                 case GATT_EVENT_QUERY_COMPLETE:
154*0f09bd96SMilanka Ringwald                     if (packet[4] != 0){
155*0f09bd96SMilanka Ringwald                         printf("SERVICE_QUERY_RESULT - Error status %x.\n", packet[4]);
156*0f09bd96SMilanka Ringwald                         add_to_blacklist(report.address);
157*0f09bd96SMilanka Ringwald                         gap_disconnect(connection_handle);
158bcf00d8fSMatthias Ringwald                         break;
159bcf00d8fSMatthias Ringwald                     }
160bcf00d8fSMatthias Ringwald                     state = TC_W4_CHARACTERISTIC_RESULT;
161*0f09bd96SMilanka Ringwald                     printf("\nSearch for battery level characteristic.\n");
162a59bfbf7SMatthias Ringwald                     gatt_client_discover_characteristics_for_service_by_uuid16(handle_gatt_client_event, connection_handle, &battery_service, battery_level_characteristic_uuid);
163bcf00d8fSMatthias Ringwald                     break;
164bcf00d8fSMatthias Ringwald                 default:
165bcf00d8fSMatthias Ringwald                     break;
166bcf00d8fSMatthias Ringwald             }
167bcf00d8fSMatthias Ringwald             break;
168bcf00d8fSMatthias Ringwald 
169bcf00d8fSMatthias Ringwald         case TC_W4_CHARACTERISTIC_RESULT:
1700e2df43fSMatthias Ringwald             switch(hci_event_packet_get_type(packet)){
171bcf00d8fSMatthias Ringwald                 case GATT_EVENT_CHARACTERISTIC_QUERY_RESULT:
172bcf00d8fSMatthias Ringwald                     gatt_event_characteristic_query_result_get_characteristic(packet, &config_characteristic);
173bcf00d8fSMatthias Ringwald                     dump_characteristic(&config_characteristic);
174bcf00d8fSMatthias Ringwald                     break;
175bcf00d8fSMatthias Ringwald                 case GATT_EVENT_QUERY_COMPLETE:
176*0f09bd96SMilanka Ringwald                     if (packet[4] != 0){
177*0f09bd96SMilanka Ringwald                         printf("CHARACTERISTIC_QUERY_RESULT - Error status %x.\n", packet[4]);
178*0f09bd96SMilanka Ringwald                         add_to_blacklist(report.address);
179*0f09bd96SMilanka Ringwald                         gap_disconnect(connection_handle);
180*0f09bd96SMilanka Ringwald                         break;
181*0f09bd96SMilanka Ringwald                     }
182bcf00d8fSMatthias Ringwald                     state = TC_W4_BATTERY_DATA;
183*0f09bd96SMilanka Ringwald                     printf("\nConfigure battery level characteristic for notify.\n");
184*0f09bd96SMilanka Ringwald                     status = gatt_client_write_client_characteristic_configuration(handle_gatt_client_event, connection_handle, &config_characteristic, GATT_CLIENT_CHARACTERISTICS_CONFIGURATION_NOTIFICATION);
185*0f09bd96SMilanka Ringwald                     if (status != 0){
186*0f09bd96SMilanka Ringwald                         printf("\nNotification not supported. Query value of characteristic.\n");
187*0f09bd96SMilanka Ringwald                         gatt_client_read_value_of_characteristic(handle_gatt_client_event, connection_handle, &config_characteristic);
188*0f09bd96SMilanka Ringwald                     }
189*0f09bd96SMilanka Ringwald 
190bcf00d8fSMatthias Ringwald                     break;
191bcf00d8fSMatthias Ringwald                 default:
192bcf00d8fSMatthias Ringwald                     break;
193bcf00d8fSMatthias Ringwald             }
194bcf00d8fSMatthias Ringwald             break;
195bcf00d8fSMatthias Ringwald         case TC_W4_BATTERY_DATA:
196*0f09bd96SMilanka Ringwald             switch(hci_event_packet_get_type(packet)){
197*0f09bd96SMilanka Ringwald                 case GATT_EVENT_NOTIFICATION:
198bcf00d8fSMatthias Ringwald                     printf("\nBattery Data:\n");
199bcf00d8fSMatthias Ringwald                     dump_characteristic_value(&packet[8], little_endian_read_16(packet, 6));
200bcf00d8fSMatthias Ringwald                     break;
201*0f09bd96SMilanka Ringwald                 case GATT_EVENT_CHARACTERISTIC_VALUE_QUERY_RESULT:
202*0f09bd96SMilanka Ringwald 
203*0f09bd96SMilanka Ringwald                         if (gatt_event_characteristic_value_query_result_get_value_length(packet) < 1) break;
204*0f09bd96SMilanka Ringwald                         battery_level = gatt_event_characteristic_value_query_result_get_value(packet)[0];
205*0f09bd96SMilanka Ringwald                         printf("Battry level %d \n", battery_level);
206*0f09bd96SMilanka Ringwald                     break;
207*0f09bd96SMilanka Ringwald 
208*0f09bd96SMilanka Ringwald                 case GATT_EVENT_QUERY_COMPLETE:
209*0f09bd96SMilanka Ringwald                     if (packet[4] != 0){
210*0f09bd96SMilanka Ringwald                         printf("CHARACTERISTIC_VALUE_QUERY_RESULT - Error status %x.\n", packet[4]);
211*0f09bd96SMilanka Ringwald                         break;
212*0f09bd96SMilanka Ringwald                     }
213*0f09bd96SMilanka Ringwald                     // Use timer if repeated request is needed and notification is not supperted
214*0f09bd96SMilanka Ringwald                     gap_disconnect(connection_handle);
215*0f09bd96SMilanka Ringwald                     break;
216*0f09bd96SMilanka Ringwald                 default:
217*0f09bd96SMilanka Ringwald                     printf("Unknown packet type %x\n", hci_event_packet_get_type(packet));
218*0f09bd96SMilanka Ringwald                     break;
219*0f09bd96SMilanka Ringwald             }
220*0f09bd96SMilanka Ringwald             break;
221bcf00d8fSMatthias Ringwald 
222bcf00d8fSMatthias Ringwald         default:
223bcf00d8fSMatthias Ringwald             printf("error\n");
224bcf00d8fSMatthias Ringwald             break;
225bcf00d8fSMatthias Ringwald     }
226bcf00d8fSMatthias Ringwald 
227bcf00d8fSMatthias Ringwald }
228bcf00d8fSMatthias Ringwald 
229*0f09bd96SMilanka Ringwald static void fill_advertising_report_from_packet(advertising_report_t * adv_report, uint8_t *packet){
230*0f09bd96SMilanka Ringwald     gap_event_advertising_report_get_address(packet, adv_report->address);
231*0f09bd96SMilanka Ringwald     adv_report->event_type = gap_event_advertising_report_get_advertising_event_type(packet);
232*0f09bd96SMilanka Ringwald     adv_report->address_type = gap_event_advertising_report_get_address_type(packet);
233*0f09bd96SMilanka Ringwald     adv_report->rssi = gap_event_advertising_report_get_rssi(packet);
234*0f09bd96SMilanka Ringwald     adv_report->length = gap_event_advertising_report_get_data_length(packet);
235*0f09bd96SMilanka Ringwald     adv_report->data = gap_event_advertising_report_get_data(packet);
236bcf00d8fSMatthias Ringwald }
237bcf00d8fSMatthias Ringwald 
238bcf00d8fSMatthias Ringwald static void hci_event_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
239bcf00d8fSMatthias Ringwald     if (packet_type != HCI_EVENT_PACKET) return;
240*0f09bd96SMilanka Ringwald 
2410e2df43fSMatthias Ringwald     uint8_t event = hci_event_packet_get_type(packet);
242bcf00d8fSMatthias Ringwald     switch (event) {
243bcf00d8fSMatthias Ringwald         case BTSTACK_EVENT_STATE:
244bcf00d8fSMatthias Ringwald             // BTstack activated, get started
245cdc7d1abSMilanka Ringwald             if (btstack_event_state_get_state(packet) != HCI_STATE_WORKING) break;
246bcf00d8fSMatthias Ringwald             if (cmdline_addr_found){
247*0f09bd96SMilanka Ringwald                 printf("Connect to %s\n", bd_addr_to_str(cmdline_addr));
248bcf00d8fSMatthias Ringwald                 state = TC_W4_CONNECT;
249bcf00d8fSMatthias Ringwald                 gap_connect(cmdline_addr, 0);
250bcf00d8fSMatthias Ringwald                 break;
251bcf00d8fSMatthias Ringwald             }
252*0f09bd96SMilanka Ringwald             printf("Start scanning!\n");
253bcf00d8fSMatthias Ringwald             state = TC_W4_SCAN_RESULT;
254bcf00d8fSMatthias Ringwald             gap_set_scan_parameters(0,0x0030, 0x0030);
255bcf00d8fSMatthias Ringwald             gap_start_scan();
256bcf00d8fSMatthias Ringwald             break;
257045013feSMatthias Ringwald         case GAP_EVENT_ADVERTISING_REPORT:
258bcf00d8fSMatthias Ringwald             if (state != TC_W4_SCAN_RESULT) return;
259bcf00d8fSMatthias Ringwald             fill_advertising_report_from_packet(&report, packet);
2603ee82ab1SMilanka Ringwald 
261*0f09bd96SMilanka Ringwald             if (blacklist_contains(report.address)) {
262*0f09bd96SMilanka Ringwald                 break;
263*0f09bd96SMilanka Ringwald             }
264*0f09bd96SMilanka Ringwald             dump_advertising_report(&report);
265bcf00d8fSMatthias Ringwald             // stop scanning, and connect to the device
266bcf00d8fSMatthias Ringwald             state = TC_W4_CONNECT;
267bcf00d8fSMatthias Ringwald             gap_stop_scan();
268*0f09bd96SMilanka Ringwald             printf("Stop scan. Connect to device with addr %s.\n", bd_addr_to_str(report.address));
269bcf00d8fSMatthias Ringwald             gap_connect(report.address,report.address_type);
270*0f09bd96SMilanka Ringwald 
271bcf00d8fSMatthias Ringwald             break;
272bcf00d8fSMatthias Ringwald         case HCI_EVENT_LE_META:
273bcf00d8fSMatthias Ringwald             // wait for connection complete
27410cad102SMilanka Ringwald             if (hci_event_le_meta_get_subevent_code(packet) !=  HCI_SUBEVENT_LE_CONNECTION_COMPLETE) break;
275bcf00d8fSMatthias Ringwald             if (state != TC_W4_CONNECT) return;
276a59bfbf7SMatthias Ringwald             connection_handle = hci_subevent_le_connection_complete_get_connection_handle(packet);
277bcf00d8fSMatthias Ringwald             // initialize gatt client context with handle, and add it to the list of active clients
278bcf00d8fSMatthias Ringwald             // query primary services
2798e14b3b1SMatthias Ringwald             printf("\nSearch for battery service.\n");
280bcf00d8fSMatthias Ringwald             state = TC_W4_SERVICE_RESULT;
281a59bfbf7SMatthias Ringwald             gatt_client_discover_primary_services_by_uuid16(handle_gatt_client_event, connection_handle, battery_service_uuid);
282bcf00d8fSMatthias Ringwald             break;
283bcf00d8fSMatthias Ringwald         case HCI_EVENT_DISCONNECTION_COMPLETE:
284*0f09bd96SMilanka Ringwald 
285*0f09bd96SMilanka Ringwald             if (cmdline_addr_found){
286*0f09bd96SMilanka Ringwald                 printf("\nDisconnected %s\n", bd_addr_to_str(cmdline_addr));
287bcf00d8fSMatthias Ringwald                 exit(0);
288*0f09bd96SMilanka Ringwald             }
289*0f09bd96SMilanka Ringwald 
290*0f09bd96SMilanka Ringwald             printf("\nDisconnected %s\n", bd_addr_to_str(report.address));
291*0f09bd96SMilanka Ringwald             printf("Restart scan.\n");
292*0f09bd96SMilanka Ringwald             state = TC_W4_SCAN_RESULT;
293*0f09bd96SMilanka Ringwald             gap_start_scan();
294bcf00d8fSMatthias Ringwald             break;
295bcf00d8fSMatthias Ringwald         default:
296bcf00d8fSMatthias Ringwald             break;
297bcf00d8fSMatthias Ringwald     }
298bcf00d8fSMatthias Ringwald }
299bcf00d8fSMatthias Ringwald 
300bcf00d8fSMatthias Ringwald static void usage(const char *name){
301bcf00d8fSMatthias Ringwald     fprintf(stderr, "\nUsage: %s [-a|--address aa:bb:cc:dd:ee:ff]\n", name);
302bcf00d8fSMatthias Ringwald     fprintf(stderr, "If no argument is provided, GATT browser will start scanning and connect to the first found device.\nTo connect to a specific device use argument [-a].\n\n");
303bcf00d8fSMatthias Ringwald }
304bcf00d8fSMatthias Ringwald 
305bcf00d8fSMatthias Ringwald int btstack_main(int argc, const char * argv[]);
306bcf00d8fSMatthias Ringwald int btstack_main(int argc, const char * argv[]){
307bcf00d8fSMatthias Ringwald 
308bcf00d8fSMatthias Ringwald     int arg = 1;
309bcf00d8fSMatthias Ringwald     cmdline_addr_found = 0;
310bcf00d8fSMatthias Ringwald 
311bcf00d8fSMatthias Ringwald     while (arg < argc) {
312bcf00d8fSMatthias Ringwald         if(!strcmp(argv[arg], "-a") || !strcmp(argv[arg], "--address")){
313bcf00d8fSMatthias Ringwald             arg++;
314a6efb919SMatthias Ringwald             cmdline_addr_found = sscanf_bd_addr(argv[arg], cmdline_addr);
315bcf00d8fSMatthias Ringwald             arg++;
316*0f09bd96SMilanka Ringwald             if (!cmdline_addr_found) exit(1);
317bcf00d8fSMatthias Ringwald             continue;
318bcf00d8fSMatthias Ringwald         }
319bcf00d8fSMatthias Ringwald         usage(argv[0]);
320bcf00d8fSMatthias Ringwald         return 0;
321bcf00d8fSMatthias Ringwald     }
322bcf00d8fSMatthias Ringwald 
323bcf00d8fSMatthias Ringwald     hci_event_callback_registration.callback = &hci_event_handler;
324bcf00d8fSMatthias Ringwald     hci_add_event_handler(&hci_event_callback_registration);
325bcf00d8fSMatthias Ringwald 
326bcf00d8fSMatthias Ringwald     l2cap_init();
327bcf00d8fSMatthias Ringwald 
328bcf00d8fSMatthias Ringwald     gatt_client_init();
329bcf00d8fSMatthias Ringwald 
330bcf00d8fSMatthias Ringwald     sm_init();
331bcf00d8fSMatthias Ringwald     sm_set_io_capabilities(IO_CAPABILITY_NO_INPUT_NO_OUTPUT);
332bcf00d8fSMatthias Ringwald 
333bcf00d8fSMatthias Ringwald     // turn on!
334bcf00d8fSMatthias Ringwald     hci_power_control(HCI_POWER_ON);
335bcf00d8fSMatthias Ringwald 
336bcf00d8fSMatthias Ringwald     return 0;
337bcf00d8fSMatthias Ringwald }
338bcf00d8fSMatthias Ringwald 
339bcf00d8fSMatthias Ringwald 
340bcf00d8fSMatthias Ringwald 
341bcf00d8fSMatthias Ringwald 
342