gap_inquiry.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  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. * contact@bluekitchen-gmbh.com
  35. *
  36. */
  37. #define BTSTACK_FILE__ "gap_inquiry.c"
  38. // *****************************************************************************
  39. /* EXAMPLE_START(gap_inquiry): GAP Inquiry Example
  40. *
  41. * @text The Generic Access Profile (GAP) defines how Bluetooth devices discover
  42. * and establish a connection with each other. In this example, the application
  43. * discovers surrounding Bluetooth devices and collects their Class of Device
  44. * (CoD), page scan mode, clock offset, and RSSI. After that, the remote name of
  45. * each device is requested. In the following section we outline the Bluetooth
  46. * logic part, i.e., how the packet handler handles the asynchronous events and
  47. * data packets.
  48. */
  49. // *****************************************************************************
  50. #include <stdint.h>
  51. #include <stdio.h>
  52. #include <stdlib.h>
  53. #include <string.h>
  54. #include "btstack.h"
  55. #define MAX_DEVICES 20
  56. enum DEVICE_STATE { REMOTE_NAME_REQUEST, REMOTE_NAME_INQUIRED, REMOTE_NAME_FETCHED };
  57. struct device {
  58. bd_addr_t address;
  59. uint8_t pageScanRepetitionMode;
  60. uint16_t clockOffset;
  61. enum DEVICE_STATE state;
  62. };
  63. #define INQUIRY_INTERVAL 5
  64. struct device devices[MAX_DEVICES];
  65. int deviceCount = 0;
  66. enum STATE {INIT, ACTIVE} ;
  67. enum STATE state = INIT;
  68. static btstack_packet_callback_registration_t hci_event_callback_registration;
  69. static int getDeviceIndexForAddress( bd_addr_t addr){
  70. int j;
  71. for (j=0; j< deviceCount; j++){
  72. if (bd_addr_cmp(addr, devices[j].address) == 0){
  73. return j;
  74. }
  75. }
  76. return -1;
  77. }
  78. static void start_scan(void){
  79. printf("Starting inquiry scan..\n");
  80. gap_inquiry_start(INQUIRY_INTERVAL);
  81. }
  82. static int has_more_remote_name_requests(void){
  83. int i;
  84. for (i=0;i<deviceCount;i++) {
  85. if (devices[i].state == REMOTE_NAME_REQUEST) return 1;
  86. }
  87. return 0;
  88. }
  89. static void do_next_remote_name_request(void){
  90. int i;
  91. for (i=0;i<deviceCount;i++) {
  92. // remote name request
  93. if (devices[i].state == REMOTE_NAME_REQUEST){
  94. devices[i].state = REMOTE_NAME_INQUIRED;
  95. printf("Get remote name of %s...\n", bd_addr_to_str(devices[i].address));
  96. gap_remote_name_request( devices[i].address, devices[i].pageScanRepetitionMode, devices[i].clockOffset | 0x8000);
  97. return;
  98. }
  99. }
  100. }
  101. static void continue_remote_names(void){
  102. if (has_more_remote_name_requests()){
  103. do_next_remote_name_request();
  104. return;
  105. }
  106. start_scan();
  107. }
  108. /* @section Bluetooth Logic
  109. *
  110. * @text The Bluetooth logic is implemented as a state machine within the packet
  111. * handler. In this example, the following states are passed sequentially:
  112. * INIT, and ACTIVE.
  113. */
  114. static void packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
  115. UNUSED(channel);
  116. UNUSED(size);
  117. bd_addr_t addr;
  118. int i;
  119. int index;
  120. if (packet_type != HCI_EVENT_PACKET) return;
  121. uint8_t event = hci_event_packet_get_type(packet);
  122. switch(state){
  123. /* @text In INIT, an inquiry scan is started, and the application transits to
  124. * ACTIVE state.
  125. */
  126. case INIT:
  127. switch(event){
  128. case BTSTACK_EVENT_STATE:
  129. if (btstack_event_state_get_state(packet) == HCI_STATE_WORKING){
  130. start_scan();
  131. state = ACTIVE;
  132. }
  133. break;
  134. default:
  135. break;
  136. }
  137. break;
  138. /* @text In ACTIVE, the following events are processed:
  139. * - GAP Inquiry result event: BTstack provides a unified inquiry result that contain
  140. * Class of Device (CoD), page scan mode, clock offset. RSSI and name (from EIR) are optional.
  141. * - Inquiry complete event: the remote name is requested for devices without a fetched
  142. * name. The state of a remote name can be one of the following:
  143. * REMOTE_NAME_REQUEST, REMOTE_NAME_INQUIRED, or REMOTE_NAME_FETCHED.
  144. * - Remote name request complete event: the remote name is stored in the table and the
  145. * state is updated to REMOTE_NAME_FETCHED. The query of remote names is continued.
  146. */
  147. case ACTIVE:
  148. switch(event){
  149. case GAP_EVENT_INQUIRY_RESULT:
  150. if (deviceCount >= MAX_DEVICES) break; // already full
  151. gap_event_inquiry_result_get_bd_addr(packet, addr);
  152. index = getDeviceIndexForAddress(addr);
  153. if (index >= 0) break; // already in our list
  154. memcpy(devices[deviceCount].address, addr, 6);
  155. devices[deviceCount].pageScanRepetitionMode = gap_event_inquiry_result_get_page_scan_repetition_mode(packet);
  156. devices[deviceCount].clockOffset = gap_event_inquiry_result_get_clock_offset(packet);
  157. // print info
  158. printf("Device found: %s ", bd_addr_to_str(addr));
  159. printf("with COD: 0x%06x, ", (unsigned int) gap_event_inquiry_result_get_class_of_device(packet));
  160. printf("pageScan %d, ", devices[deviceCount].pageScanRepetitionMode);
  161. printf("clock offset 0x%04x",devices[deviceCount].clockOffset);
  162. if (gap_event_inquiry_result_get_rssi_available(packet)){
  163. printf(", rssi %d dBm", (int8_t) gap_event_inquiry_result_get_rssi(packet));
  164. }
  165. if (gap_event_inquiry_result_get_name_available(packet)){
  166. char name_buffer[240];
  167. int name_len = gap_event_inquiry_result_get_name_len(packet);
  168. memcpy(name_buffer, gap_event_inquiry_result_get_name(packet), name_len);
  169. name_buffer[name_len] = 0;
  170. printf(", name '%s'", name_buffer);
  171. devices[deviceCount].state = REMOTE_NAME_FETCHED;;
  172. } else {
  173. devices[deviceCount].state = REMOTE_NAME_REQUEST;
  174. }
  175. printf("\n");
  176. deviceCount++;
  177. break;
  178. case GAP_EVENT_INQUIRY_COMPLETE:
  179. for (i=0;i<deviceCount;i++) {
  180. // retry remote name request
  181. if (devices[i].state == REMOTE_NAME_INQUIRED)
  182. devices[i].state = REMOTE_NAME_REQUEST;
  183. }
  184. continue_remote_names();
  185. break;
  186. case HCI_EVENT_REMOTE_NAME_REQUEST_COMPLETE:
  187. reverse_bd_addr(&packet[3], addr);
  188. index = getDeviceIndexForAddress(addr);
  189. if (index >= 0) {
  190. if (packet[2] == 0) {
  191. printf("Name: '%s'\n", &packet[9]);
  192. devices[index].state = REMOTE_NAME_FETCHED;
  193. } else {
  194. printf("Failed to get name: page timeout\n");
  195. }
  196. }
  197. continue_remote_names();
  198. break;
  199. default:
  200. break;
  201. }
  202. break;
  203. default:
  204. break;
  205. }
  206. }
  207. /* @text For more details on discovering remote devices, please see
  208. * Section on [GAP](../profiles/#sec:GAPdiscoverRemoteDevices).
  209. */
  210. /* @section Main Application Setup
  211. *
  212. * @text Listing MainConfiguration shows main application code.
  213. * It registers the HCI packet handler and starts the Bluetooth stack.
  214. */
  215. /* LISTING_START(MainConfiguration): Setup packet handler for GAP inquiry */
  216. int btstack_main(int argc, const char * argv[]);
  217. int btstack_main(int argc, const char * argv[]) {
  218. (void)argc;
  219. (void)argv;
  220. // enabled EIR
  221. hci_set_inquiry_mode(INQUIRY_MODE_RSSI_AND_EIR);
  222. hci_event_callback_registration.callback = &packet_handler;
  223. hci_add_event_handler(&hci_event_callback_registration);
  224. // turn on!
  225. hci_power_control(HCI_POWER_ON);
  226. return 0;
  227. }
  228. /* LISTING_END */
  229. /* EXAMPLE_END */