xref: /btstack/example/gatt_device_information_query.c (revision 087a95b88599c99c210f7df5b431a291b36c5192)
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__ "gatt_device_information_query.c"
39 
40 // *****************************************************************************
41 /* EXAMPLE_START(gatt_device_information_query): GATT Device Information Client
42  *
43  */
44 
45 #include <stdint.h>
46 #include <stdio.h>
47 #include <stdlib.h>
48 #include <string.h>
49 
50 #include "btstack.h"
51 
52 // gatt_device_information_query.gatt contains the declaration of the provided GATT Services + Characteristics
53 // gatt_device_information_query.h    contains the binary representation of gatt_device_information_query.gatt
54 // it is generated by the build system by calling: $BTSTACK_ROOT/tool/compile_gatt.py gatt_device_information_query.gatt gatt_device_information_query.h
55 // it needs to be regenerated when the GATT Database declared in gatt_device_information_query.gatt file is modified
56 #include "gatt_device_information_query.h"
57 
58 typedef struct advertising_report {
59     uint8_t   type;
60     uint8_t   event_type;
61     uint8_t   address_type;
62     bd_addr_t address;
63     uint8_t   rssi;
64     uint8_t   length;
65     const uint8_t * data;
66 } advertising_report_t;
67 
68 static enum {
69     APP_STATE_IDLE,
70     APP_STATE_W4_SCAN_RESULT,
71     APP_STATE_W4_CONNECT,
72     APP_STATE_CONNECTED
73 } app_state;
74 
75 static int blacklist_index = 0;
76 static bd_addr_t blacklist[20];
77 static advertising_report_t report;
78 
79 static hci_con_handle_t connection_handle;
80 
81 static bd_addr_t cmdline_addr;
82 static int cmdline_addr_found = 0;
83 
84 static btstack_packet_callback_registration_t hci_event_callback_registration;
85 
86 static int blacklist_size(void){
87     return sizeof(blacklist) / sizeof(bd_addr_t);
88 }
89 
90 static int blacklist_contains(bd_addr_t addr){
91     int i;
92     for (i=0; i<blacklist_size(); i++){
93         if (bd_addr_cmp(addr, blacklist[i]) == 0) return 1;
94     }
95     return 0;
96 }
97 
98 static void add_to_blacklist(bd_addr_t addr){
99     printf("%s added to blacklist (no device information service found).\n", bd_addr_to_str(addr));
100     bd_addr_copy(blacklist[blacklist_index], addr);
101     blacklist_index = (blacklist_index + 1) % blacklist_size();
102 }
103 
104 static void dump_advertising_report(uint8_t *packet){
105     bd_addr_t address;
106     gap_event_advertising_report_get_address(packet, address);
107 
108     printf("    * adv. event: evt-type %u, addr-type %u, addr %s, rssi %u, length adv %u, data: ",
109         gap_event_advertising_report_get_advertising_event_type(packet),
110         gap_event_advertising_report_get_address_type(packet),
111         bd_addr_to_str(address),
112         gap_event_advertising_report_get_rssi(packet),
113         gap_event_advertising_report_get_data_length(packet));
114     printf_hexdump(gap_event_advertising_report_get_data(packet), gap_event_advertising_report_get_data_length(packet));
115 
116 }
117 
118 static void handle_gatt_client_event(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
119     UNUSED(packet_type);
120     UNUSED(channel);
121     UNUSED(size);
122 
123     uint8_t att_status = 0;
124 
125     if (hci_event_packet_get_type(packet) != HCI_EVENT_GATTSERVICE_META){
126         return;
127     }
128 
129     switch (hci_event_gattservice_meta_get_subevent_code(packet)){
130         case GATTSERVICE_SUBEVENT_DEVICE_INFORMATION_MANUFACTURER_NAME:
131             att_status = gattservice_subevent_device_information_manufacturer_name_get_att_status(packet);
132             if (att_status != ATT_ERROR_SUCCESS){
133                 printf("Manufacturer Name read failed, ATT Error 0x%02x\n", att_status);
134             } else {
135                 printf("Manufacturer Name: %s\n", gattservice_subevent_device_information_manufacturer_name_get_value(packet));
136             }
137             break;
138 
139         case GATTSERVICE_SUBEVENT_DEVICE_INFORMATION_MODEL_NUMBER:
140             att_status = gattservice_subevent_device_information_model_number_get_att_status(packet);
141             if (att_status != ATT_ERROR_SUCCESS){
142                 printf("Model Number read failed, ATT Error 0x%02x\n", att_status);
143             } else {
144                 printf("Model Number:     %s\n", gattservice_subevent_device_information_model_number_get_value(packet));
145             }
146             break;
147 
148         case GATTSERVICE_SUBEVENT_DEVICE_INFORMATION_SERIAL_NUMBER:
149             att_status = gattservice_subevent_device_information_serial_number_get_att_status(packet);
150             if (att_status != ATT_ERROR_SUCCESS){
151                 printf("Serial Number read failed, ATT Error 0x%02x\n", att_status);
152             } else {
153                 printf("Serial Number:    %s\n", gattservice_subevent_device_information_serial_number_get_value(packet));
154             }
155             break;
156 
157         case GATTSERVICE_SUBEVENT_DEVICE_INFORMATION_HARDWARE_REVISION:
158             att_status = gattservice_subevent_device_information_hardware_revision_get_att_status(packet);
159             if (att_status != ATT_ERROR_SUCCESS){
160                 printf("Hardware Revision read failed, ATT Error 0x%02x\n", att_status);
161             } else {
162                 printf("Hardware Revision: %s\n", gattservice_subevent_device_information_hardware_revision_get_value(packet));
163             }
164             break;
165 
166         case GATTSERVICE_SUBEVENT_DEVICE_INFORMATION_FIRMWARE_REVISION:
167             att_status = gattservice_subevent_device_information_firmware_revision_get_att_status(packet);
168             if (att_status != ATT_ERROR_SUCCESS){
169                 printf("Firmware Revision read failed, ATT Error 0x%02x\n", att_status);
170             } else {
171                 printf("Firmware Revision: %s\n", gattservice_subevent_device_information_firmware_revision_get_value(packet));
172             }
173             break;
174 
175         case GATTSERVICE_SUBEVENT_DEVICE_INFORMATION_SOFTWARE_REVISION:
176             att_status = gattservice_subevent_device_information_software_revision_get_att_status(packet);
177             if (att_status != ATT_ERROR_SUCCESS){
178                 printf("Software Revision read failed, ATT Error 0x%02x\n", att_status);
179             } else {
180                 printf("Software Revision: %s\n", gattservice_subevent_device_information_software_revision_get_value(packet));
181             }
182             break;
183 
184         case GATTSERVICE_SUBEVENT_DEVICE_INFORMATION_SYSTEM_ID:
185             att_status = gattservice_subevent_device_information_system_id_get_att_status(packet);
186             if (att_status != ATT_ERROR_SUCCESS){
187                 printf("System ID read failed, ATT Error 0x%02x\n", att_status);
188             } else {
189                 uint32_t manufacturer_identifier_low  = gattservice_subevent_device_information_system_id_get_manufacturer_id_low(packet);
190                 uint8_t  manufacturer_identifier_high = gattservice_subevent_device_information_system_id_get_manufacturer_id_high(packet);
191 
192                 printf("Manufacturer ID:  0x%02x%08x\n",  manufacturer_identifier_high, manufacturer_identifier_low);
193                 printf("Organizationally Unique ID:  0x%06x\n", gattservice_subevent_device_information_system_id_get_organizationally_unique_id(packet));
194             }
195             break;
196 
197         case GATTSERVICE_SUBEVENT_DEVICE_INFORMATION_IEEE_REGULATORY_CERTIFICATION:
198             att_status = gattservice_subevent_device_information_ieee_regulatory_certification_get_att_status(packet);
199             if (att_status != ATT_ERROR_SUCCESS){
200                 printf("IEEE Regulatory Certification read failed, ATT Error 0x%02x\n", att_status);
201             } else {
202                 printf("value_a:          0x%04x\n", gattservice_subevent_device_information_ieee_regulatory_certification_get_value_a(packet));
203                 printf("value_b:          0x%04x\n", gattservice_subevent_device_information_ieee_regulatory_certification_get_value_b(packet));
204             }
205             break;
206 
207         case GATTSERVICE_SUBEVENT_DEVICE_INFORMATION_PNP_ID:
208             att_status = gattservice_subevent_device_information_pnp_id_get_att_status(packet);
209             if (att_status != ATT_ERROR_SUCCESS){
210                 printf("PNP ID read failed, ATT Error 0x%02x\n", att_status);
211             } else {
212                 printf("Vendor Source ID: 0x%02x\n", gattservice_subevent_device_information_pnp_id_get_vendor_source_id(packet));
213                 printf("Vendor  ID:       0x%04x\n", gattservice_subevent_device_information_pnp_id_get_vendor_id(packet));
214                 printf("Product ID:       0x%04x\n", gattservice_subevent_device_information_pnp_id_get_product_id(packet));
215                 printf("Product Version:  0x%04x\n", gattservice_subevent_device_information_pnp_id_get_product_version(packet));
216             }
217             break;
218 
219         case GATTSERVICE_SUBEVENT_DEVICE_INFORMATION_DONE:
220             att_status = gattservice_subevent_device_information_serial_number_get_att_status(packet);
221             if (att_status != ATT_ERROR_SUCCESS){
222                 printf("Query failed, ATT Error 0x%02x\n", att_status);
223             } else {
224                 printf("Query done\n");
225             }
226 
227         default:
228             break;
229     }
230 }
231 
232 static void hci_event_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
233     UNUSED(channel);
234     UNUSED(size);
235 
236     uint8_t status;
237     bd_addr_t address;
238 
239     if (packet_type != HCI_EVENT_PACKET){
240         return;
241     }
242 
243     switch (hci_event_packet_get_type(packet)) {
244         case BTSTACK_EVENT_STATE:
245             // BTstack activated, get started
246             if (btstack_event_state_get_state(packet) != HCI_STATE_WORKING) break;
247             if (cmdline_addr_found){
248                 printf("Connect to %s\n", bd_addr_to_str(cmdline_addr));
249                 app_state = APP_STATE_W4_CONNECT;
250                 gap_connect(cmdline_addr, 0);
251                 break;
252             }
253             printf("Start scanning!\n");
254             app_state = APP_STATE_W4_SCAN_RESULT;
255             gap_set_scan_parameters(0,0x0030, 0x0030);
256             gap_start_scan();
257             break;
258 
259         case GAP_EVENT_ADVERTISING_REPORT:
260             if (app_state != APP_STATE_W4_SCAN_RESULT) return;
261 
262             gap_event_advertising_report_get_address(packet, address);
263             if (blacklist_contains(address)) {
264                 break;
265             }
266             dump_advertising_report(packet);
267 
268             // stop scanning, and connect to the device
269             app_state = APP_STATE_W4_CONNECT;
270             gap_stop_scan();
271             printf("Stop scan. Connect to device with addr %s.\n", bd_addr_to_str(report.address));
272             gap_connect(report.address,report.address_type);
273             break;
274 
275         case HCI_EVENT_LE_META:
276             // wait for connection complete
277             if (hci_event_le_meta_get_subevent_code(packet) !=  HCI_SUBEVENT_LE_CONNECTION_COMPLETE) break;
278             if (app_state != APP_STATE_W4_CONNECT) return;
279             connection_handle = hci_subevent_le_connection_complete_get_connection_handle(packet);
280 
281             status = device_information_service_client_query(connection_handle, handle_gatt_client_event);
282             btstack_assert(status == ERROR_CODE_SUCCESS);
283 
284             app_state = APP_STATE_CONNECTED;
285             printf("Device Information connected.\n");
286             break;
287 
288         case HCI_EVENT_DISCONNECTION_COMPLETE:
289             connection_handle = HCI_CON_HANDLE_INVALID;
290 
291             if (cmdline_addr_found){
292                 printf("Disconnected %s\n", bd_addr_to_str(cmdline_addr));
293                 return;
294             }
295 
296             printf("Disconnected %s\n", bd_addr_to_str(report.address));
297             printf("Restart scan.\n");
298             app_state = APP_STATE_W4_SCAN_RESULT;
299             gap_start_scan();
300             break;
301         default:
302             break;
303     }
304 }
305 
306 int btstack_main(int argc, const char * argv[]);
307 int btstack_main(int argc, const char * argv[]){
308 
309     // parse address if command line arguments are provided
310     int arg = 1;
311     cmdline_addr_found = 0;
312 
313     while (arg < argc) {
314         if(!strcmp(argv[arg], "-a") || !strcmp(argv[arg], "--address")){
315             arg++;
316             cmdline_addr_found = sscanf_bd_addr(argv[arg], cmdline_addr);
317             arg++;
318             if (!cmdline_addr_found) exit(1);
319             continue;
320         }
321         fprintf(stderr, "\nUsage: %s [-a|--address aa:bb:cc:dd:ee:ff]\n", argv[0]);
322         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");
323         return 0;
324     }
325     (void)argv;
326 
327     l2cap_init();
328 
329     // setup ATT server - only needed if LE Peripheral does ATT queries on its own, e.g. Android phones
330     att_server_init(profile_data, NULL, NULL);
331 
332     // GATT Client setup
333     gatt_client_init();
334     device_information_service_client_init();
335 
336     sm_init();
337     sm_set_io_capabilities(IO_CAPABILITY_NO_INPUT_NO_OUTPUT);
338 
339     hci_event_callback_registration.callback = &hci_event_handler;
340     hci_add_event_handler(&hci_event_callback_registration);
341 
342     app_state = APP_STATE_IDLE;
343 
344     // turn on!
345     hci_power_control(HCI_POWER_ON);
346 
347     return 0;
348 }
349 
350 /* EXAMPLE_END */
351 
352 
353