xref: /btstack/platform/daemon/example/inquiry.c (revision db09c08d745c978bc51e1abcf0215f8727d1e684)
12531c97eSMatthias Ringwald /*
22531c97eSMatthias Ringwald  * Copyright (C) 2014 BlueKitchen GmbH
32531c97eSMatthias Ringwald  *
42531c97eSMatthias Ringwald  * Redistribution and use in source and binary forms, with or without
52531c97eSMatthias Ringwald  * modification, are permitted provided that the following conditions
62531c97eSMatthias Ringwald  * are met:
72531c97eSMatthias Ringwald  *
82531c97eSMatthias Ringwald  * 1. Redistributions of source code must retain the above copyright
92531c97eSMatthias Ringwald  *    notice, this list of conditions and the following disclaimer.
102531c97eSMatthias Ringwald  * 2. Redistributions in binary form must reproduce the above copyright
112531c97eSMatthias Ringwald  *    notice, this list of conditions and the following disclaimer in the
122531c97eSMatthias Ringwald  *    documentation and/or other materials provided with the distribution.
132531c97eSMatthias Ringwald  * 3. Neither the name of the copyright holders nor the names of
142531c97eSMatthias Ringwald  *    contributors may be used to endorse or promote products derived
152531c97eSMatthias Ringwald  *    from this software without specific prior written permission.
162531c97eSMatthias Ringwald  * 4. Any redistribution, use, or modification is done solely for
172531c97eSMatthias Ringwald  *    personal benefit and not for any commercial purpose or for
182531c97eSMatthias Ringwald  *    monetary gain.
192531c97eSMatthias Ringwald  *
202531c97eSMatthias Ringwald  * THIS SOFTWARE IS PROVIDED BY BLUEKITCHEN GMBH AND CONTRIBUTORS
212531c97eSMatthias Ringwald  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
222531c97eSMatthias 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,
252531c97eSMatthias Ringwald  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
262531c97eSMatthias Ringwald  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
272531c97eSMatthias Ringwald  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
282531c97eSMatthias Ringwald  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
292531c97eSMatthias Ringwald  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
302531c97eSMatthias Ringwald  * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
312531c97eSMatthias Ringwald  * SUCH DAMAGE.
322531c97eSMatthias Ringwald  *
332531c97eSMatthias Ringwald  * Please inquire about commercial licensing options at
342531c97eSMatthias Ringwald  * contact@bluekitchen-gmbh.com
352531c97eSMatthias Ringwald  *
362531c97eSMatthias Ringwald  */
372531c97eSMatthias Ringwald 
38e501bae0SMatthias Ringwald #define BTSTACK_FILE__ "inquiry.c"
39ab2c6ae4SMatthias Ringwald 
402531c97eSMatthias Ringwald /*
412531c97eSMatthias Ringwald  *  inquiry.c
422531c97eSMatthias Ringwald  *
432531c97eSMatthias Ringwald  *  basic inquiry scan with remote name lookup
442531c97eSMatthias Ringwald  */
452531c97eSMatthias Ringwald 
462531c97eSMatthias Ringwald #include <unistd.h>
472531c97eSMatthias Ringwald #include <stdio.h>
482531c97eSMatthias Ringwald #include <stdlib.h>
492531c97eSMatthias Ringwald #include <string.h>
502531c97eSMatthias Ringwald 
512531c97eSMatthias Ringwald #include "btstack_client.h"
52fa968b99SMatthias Ringwald 
53fa968b99SMatthias Ringwald #ifdef _WIN32
542ca78d18SMatthias Ringwald #include "btstack_run_loop_windows.h"
55fa968b99SMatthias Ringwald #else
56fa968b99SMatthias Ringwald #include "btstack_run_loop_posix.h"
57fa968b99SMatthias Ringwald #endif
582531c97eSMatthias Ringwald 
592531c97eSMatthias Ringwald #define MAX_DEVICES 10
602531c97eSMatthias Ringwald struct device {
612531c97eSMatthias Ringwald 	bd_addr_t  address;
622531c97eSMatthias Ringwald 	uint16_t   clockOffset;
632531c97eSMatthias Ringwald 	uint32_t   classOfDevice;
642531c97eSMatthias Ringwald 	uint8_t	   pageScanRepetitionMode;
652531c97eSMatthias Ringwald 	uint8_t    rssi;
662531c97eSMatthias Ringwald 	uint8_t    state; // 0 empty, 1 found, 2 remote name tried, 3 remote name found
672531c97eSMatthias Ringwald };
682531c97eSMatthias Ringwald 
692531c97eSMatthias Ringwald #define INQUIRY_INTERVAL 5
702531c97eSMatthias Ringwald struct device devices[MAX_DEVICES];
712531c97eSMatthias Ringwald int deviceCount = 0;
722531c97eSMatthias Ringwald 
732531c97eSMatthias Ringwald enum STATE {INIT, W4_INQUIRY_MODE_COMPLETE, ACTIVE, DEVICE_NAME, REMOTE_NAME_REQUEST, REMOTE_NAME_INQUIRED, REMOTE_NAME_FETCHED} ;
742531c97eSMatthias Ringwald enum STATE state = INIT;
752531c97eSMatthias Ringwald 
getDeviceIndexForAddress(bd_addr_t addr)762531c97eSMatthias Ringwald int getDeviceIndexForAddress( bd_addr_t addr){
772531c97eSMatthias Ringwald 	int j;
782531c97eSMatthias Ringwald 	for (j=0; j< deviceCount; j++){
792531c97eSMatthias Ringwald 		if (bd_addr_cmp(addr, devices[j].address) == 0){
802531c97eSMatthias Ringwald 			return j;
812531c97eSMatthias Ringwald 		}
822531c97eSMatthias Ringwald 	}
832531c97eSMatthias Ringwald 	return -1;
842531c97eSMatthias Ringwald }
852531c97eSMatthias Ringwald 
start_scan(void)862531c97eSMatthias Ringwald void start_scan(void){
872531c97eSMatthias Ringwald 	printf("Starting inquiry scan..\n");
888aee7be2SMilanka Ringwald 	bt_send_cmd(&hci_inquiry, GAP_IAC_GENERAL_INQUIRY, INQUIRY_INTERVAL, 0);
892531c97eSMatthias Ringwald }
902531c97eSMatthias Ringwald 
has_more_remote_name_requests(void)912531c97eSMatthias Ringwald int has_more_remote_name_requests(void){
922531c97eSMatthias Ringwald 	int i;
932531c97eSMatthias Ringwald 	for (i=0;i<deviceCount;i++) {
942531c97eSMatthias Ringwald 		if (devices[i].state == REMOTE_NAME_REQUEST) return 1;
952531c97eSMatthias Ringwald 	}
962531c97eSMatthias Ringwald 	return 0;
972531c97eSMatthias Ringwald }
982531c97eSMatthias Ringwald 
do_next_remote_name_request(void)992531c97eSMatthias Ringwald void do_next_remote_name_request(void){
1002531c97eSMatthias Ringwald 	int i;
1012531c97eSMatthias Ringwald 	for (i=0;i<deviceCount;i++) {
1022531c97eSMatthias Ringwald 		// remote name request
1032531c97eSMatthias Ringwald 		if (devices[i].state == REMOTE_NAME_REQUEST){
1042531c97eSMatthias Ringwald 			devices[i].state = REMOTE_NAME_INQUIRED;
1052531c97eSMatthias Ringwald 			printf("Get remote name of %s...\n", bd_addr_to_str(devices[i].address));
1062531c97eSMatthias Ringwald 			bt_send_cmd(&hci_remote_name_request, devices[i].address,
1072531c97eSMatthias Ringwald 								devices[i].pageScanRepetitionMode, 0, devices[i].clockOffset | 0x8000);
1082531c97eSMatthias Ringwald 			return;
1092531c97eSMatthias Ringwald 		}
1102531c97eSMatthias Ringwald 	}
1112531c97eSMatthias Ringwald }
1122531c97eSMatthias Ringwald 
1132531c97eSMatthias Ringwald 
packet_handler(uint8_t packet_type,uint16_t channel,uint8_t * packet,uint16_t size)1142531c97eSMatthias Ringwald static void packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
1152531c97eSMatthias Ringwald //static void packet_handler (uint8_t packet_type, uint8_t *packet, uint16_t size){
1162531c97eSMatthias Ringwald 	bd_addr_t addr;
1172531c97eSMatthias Ringwald 	int i;
1182531c97eSMatthias Ringwald 	int index;
1192531c97eSMatthias Ringwald 	int numResponses;
1202531c97eSMatthias Ringwald 
1212531c97eSMatthias Ringwald 	// printf("packet_handler: pt: 0x%02x, packet[0]: 0x%02x\n", packet_type, packet[0]);
1222531c97eSMatthias Ringwald 	if (packet_type != HCI_EVENT_PACKET) return;
1232531c97eSMatthias Ringwald 
1240e2df43fSMatthias Ringwald 	uint8_t event = hci_event_packet_get_type(packet);
1252531c97eSMatthias Ringwald 
1262531c97eSMatthias Ringwald 	switch(state){
1272531c97eSMatthias Ringwald 
1282531c97eSMatthias Ringwald 		case INIT:
129be7cc9a0SMilanka Ringwald 			if (btstack_event_state_get_state(packet) == HCI_STATE_WORKING){
1302531c97eSMatthias Ringwald 				bt_send_cmd(&hci_write_inquiry_mode, 0x01); // with RSSI
1312531c97eSMatthias Ringwald 				state = W4_INQUIRY_MODE_COMPLETE;
1322531c97eSMatthias Ringwald 			}
1332531c97eSMatthias Ringwald 			break;
1342531c97eSMatthias Ringwald 
1352531c97eSMatthias Ringwald 		case W4_INQUIRY_MODE_COMPLETE:
1362531c97eSMatthias Ringwald 			switch(event){
1372531c97eSMatthias Ringwald 				case HCI_EVENT_COMMAND_COMPLETE:
138*db09c08dSMatthias Ringwald 					if (hci_event_command_complete_get_command_opcode(packet) == HCI_OPCODE_HCI_WRITE_INQUIRY_MODE){
1392531c97eSMatthias Ringwald 						start_scan();
1402531c97eSMatthias Ringwald 						state = ACTIVE;
1412531c97eSMatthias Ringwald 					}
1422531c97eSMatthias Ringwald 					break;
1432531c97eSMatthias Ringwald 				case HCI_EVENT_COMMAND_STATUS:
144*db09c08dSMatthias Ringwald 					if (hci_event_command_complete_get_command_opcode(packet) == HCI_OPCODE_HCI_WRITE_INQUIRY_MODE){
1452531c97eSMatthias Ringwald 						printf("Ignoring error (0x%x) from hci_write_inquiry_mode.\n", packet[2]);
1462531c97eSMatthias Ringwald 						start_scan();
1472531c97eSMatthias Ringwald 						state = ACTIVE;
1482531c97eSMatthias Ringwald 					}
1492531c97eSMatthias Ringwald 					break;
1502531c97eSMatthias Ringwald 				default:
1512531c97eSMatthias Ringwald 					break;
1522531c97eSMatthias Ringwald 			}
1532531c97eSMatthias Ringwald 
1542531c97eSMatthias Ringwald 			break;
1552531c97eSMatthias Ringwald 
1562531c97eSMatthias Ringwald 		case ACTIVE:
1572531c97eSMatthias Ringwald 			switch(event){
1582531c97eSMatthias Ringwald 				case HCI_EVENT_INQUIRY_RESULT:
1592531c97eSMatthias Ringwald 				case HCI_EVENT_INQUIRY_RESULT_WITH_RSSI:{
160f8744deaSMilanka Ringwald                     numResponses = hci_event_inquiry_result_get_num_responses(packet);
1612531c97eSMatthias Ringwald                     int offset = 3;
1622531c97eSMatthias Ringwald                     for (i=0; i<numResponses && deviceCount < MAX_DEVICES;i++){
1632531c97eSMatthias Ringwald                         reverse_bd_addr(&packet[offset], addr);
1642531c97eSMatthias Ringwald                         offset += 6;
1652531c97eSMatthias Ringwald                         index = getDeviceIndexForAddress(addr);
1662531c97eSMatthias Ringwald                         if (index >= 0) continue;   // already in our list
1672531c97eSMatthias Ringwald                         memcpy(devices[deviceCount].address, addr, 6);
1682531c97eSMatthias Ringwald 
1692531c97eSMatthias Ringwald                         devices[deviceCount].pageScanRepetitionMode = packet[offset];
1702531c97eSMatthias Ringwald                         offset += 1;
1712531c97eSMatthias Ringwald 
1722531c97eSMatthias Ringwald                         if (event == HCI_EVENT_INQUIRY_RESULT){
1732531c97eSMatthias Ringwald                             offset += 2; // Reserved + Reserved
1742531c97eSMatthias Ringwald                             devices[deviceCount].classOfDevice = little_endian_read_24(packet, offset);
1752531c97eSMatthias Ringwald                             offset += 3;
1762531c97eSMatthias Ringwald                             devices[deviceCount].clockOffset =   little_endian_read_16(packet, offset) & 0x7fff;
1772531c97eSMatthias Ringwald                             offset += 2;
1782531c97eSMatthias Ringwald                             devices[deviceCount].rssi  = 0;
1792531c97eSMatthias Ringwald                         } else {
1802531c97eSMatthias Ringwald                             offset += 1; // Reserved
1812531c97eSMatthias Ringwald                             devices[deviceCount].classOfDevice = little_endian_read_24(packet, offset);
1822531c97eSMatthias Ringwald                             offset += 3;
1832531c97eSMatthias Ringwald                             devices[deviceCount].clockOffset =   little_endian_read_16(packet, offset) & 0x7fff;
1842531c97eSMatthias Ringwald                             offset += 2;
1852531c97eSMatthias Ringwald                             devices[deviceCount].rssi  = packet[offset];
1862531c97eSMatthias Ringwald                             offset += 1;
1872531c97eSMatthias Ringwald                         }
1882531c97eSMatthias Ringwald                         devices[deviceCount].state = REMOTE_NAME_REQUEST;
1892531c97eSMatthias Ringwald                         printf("Device found: %s with COD: 0x%06x, pageScan %d, clock offset 0x%04x, rssi 0x%02x\n", bd_addr_to_str(addr),
1902531c97eSMatthias Ringwald                                 devices[deviceCount].classOfDevice, devices[deviceCount].pageScanRepetitionMode,
1912531c97eSMatthias Ringwald                                 devices[deviceCount].clockOffset, devices[deviceCount].rssi);
1922531c97eSMatthias Ringwald                         deviceCount++;
1932531c97eSMatthias Ringwald                     }
1942531c97eSMatthias Ringwald 
1952531c97eSMatthias Ringwald                     break;
1962531c97eSMatthias Ringwald                 }
1972531c97eSMatthias Ringwald 
1982531c97eSMatthias Ringwald 				case HCI_EVENT_INQUIRY_COMPLETE:
1992531c97eSMatthias Ringwald 					for (i=0;i<deviceCount;i++) {
2002531c97eSMatthias Ringwald 						// retry remote name request
2012531c97eSMatthias Ringwald 						if (devices[i].state == REMOTE_NAME_INQUIRED)
2022531c97eSMatthias Ringwald 							devices[i].state = REMOTE_NAME_REQUEST;
2032531c97eSMatthias Ringwald 					}
2042531c97eSMatthias Ringwald 					if (has_more_remote_name_requests()){
2052531c97eSMatthias Ringwald 						do_next_remote_name_request();
2062531c97eSMatthias Ringwald 						break;
2072531c97eSMatthias Ringwald 					}
2082531c97eSMatthias Ringwald 					start_scan();
2092531c97eSMatthias Ringwald 					break;
2102531c97eSMatthias Ringwald 
2112531c97eSMatthias Ringwald 				case DAEMON_EVENT_REMOTE_NAME_CACHED:
2122531c97eSMatthias Ringwald 					reverse_bd_addr(&packet[3], addr);
2132531c97eSMatthias Ringwald 					printf("Cached remote name for %s: '%s'\n", bd_addr_to_str(addr), &packet[9]);
2142531c97eSMatthias Ringwald 					break;
2152531c97eSMatthias Ringwald 
2162531c97eSMatthias Ringwald 				case HCI_EVENT_REMOTE_NAME_REQUEST_COMPLETE:
2172531c97eSMatthias Ringwald 					reverse_bd_addr(&packet[3], addr);
2182531c97eSMatthias Ringwald 					index = getDeviceIndexForAddress(addr);
2192531c97eSMatthias Ringwald 					if (index >= 0) {
2202531c97eSMatthias Ringwald 						if (packet[2] == 0) {
2212531c97eSMatthias Ringwald 							printf("Name: '%s'\n", &packet[9]);
2222531c97eSMatthias Ringwald 							devices[index].state = REMOTE_NAME_FETCHED;
2232531c97eSMatthias Ringwald 						} else {
2242531c97eSMatthias Ringwald 							printf("Failed to get name: page timeout\n");
2252531c97eSMatthias Ringwald 						}
2262531c97eSMatthias Ringwald 					}
2272531c97eSMatthias Ringwald 					if (has_more_remote_name_requests()){
2282531c97eSMatthias Ringwald 						do_next_remote_name_request();
2292531c97eSMatthias Ringwald 						break;
2302531c97eSMatthias Ringwald 					}
2312531c97eSMatthias Ringwald 					start_scan();
2322531c97eSMatthias Ringwald 					break;
2332531c97eSMatthias Ringwald 
2342531c97eSMatthias Ringwald 				default:
2352531c97eSMatthias Ringwald 					break;
2362531c97eSMatthias Ringwald 			}
2372531c97eSMatthias Ringwald 			break;
2382531c97eSMatthias Ringwald 
2392531c97eSMatthias Ringwald 		default:
2402531c97eSMatthias Ringwald 			break;
2412531c97eSMatthias Ringwald 	}
2422531c97eSMatthias Ringwald }
2432531c97eSMatthias Ringwald 
main(int argc,const char * argv[])2442531c97eSMatthias Ringwald int main (int argc, const char * argv[]){
2452531c97eSMatthias Ringwald 	// start stack
246b9dcd1ccSMatthias Ringwald #ifdef _WIN32
247b9dcd1ccSMatthias Ringwald 	btstack_run_loop_init(btstack_run_loop_windows_get_instance());
248fa968b99SMatthias Ringwald #else
249fa968b99SMatthias Ringwald 	btstack_run_loop_init(btstack_run_loop_posix_get_instance());
250b9dcd1ccSMatthias Ringwald #endif
2512531c97eSMatthias Ringwald 	int err = bt_open();
2522531c97eSMatthias Ringwald 	if (err) {
2532531c97eSMatthias Ringwald 		printf("Failed to open connection to BTdaemon\n");
2542531c97eSMatthias Ringwald 		return err;
2552531c97eSMatthias Ringwald 	}
2562531c97eSMatthias Ringwald 	// init table
2572531c97eSMatthias Ringwald 	int i; for (i=0;i<MAX_DEVICES;i++) devices[i].state = 0;
2582531c97eSMatthias Ringwald 
2592531c97eSMatthias Ringwald 	bt_register_packet_handler(packet_handler);
2602531c97eSMatthias Ringwald 	bt_send_cmd(&btstack_set_power_mode, HCI_POWER_ON );
2612531c97eSMatthias Ringwald 	btstack_run_loop_execute();
2622531c97eSMatthias Ringwald 	bt_close();
2632531c97eSMatthias Ringwald 	return 0;
2642531c97eSMatthias Ringwald }
265