adapter.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. /******************************************************************************
  2. *
  3. * Copyright 2014 Google, Inc.
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at:
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. *
  17. ******************************************************************************/
  18. #include "support/adapter.h"
  19. #include "base.h"
  20. #include "btcore/include/property.h"
  21. #include "support/callbacks.h"
  22. static bt_state_t state;
  23. static int property_count = 0;
  24. static bt_property_t* properties = NULL;
  25. static bt_discovery_state_t discovery_state;
  26. static bt_acl_state_t acl_state;
  27. static bt_bond_state_t bond_state;
  28. static void parse_properties(int num_properties, bt_property_t* property);
  29. // Returns the current adapter state.
  30. bt_state_t adapter_get_state() { return state; }
  31. // Returns the number of adapter properties.
  32. int adapter_get_property_count() { return property_count; }
  33. // Returns the specified property.
  34. bt_property_t* adapter_get_property(bt_property_type_t type) {
  35. for (int i = 0; i < property_count; ++i) {
  36. if (properties[i].type == type) {
  37. return &properties[i];
  38. }
  39. }
  40. return NULL;
  41. }
  42. // Returns the device discovery state.
  43. bt_discovery_state_t adapter_get_discovery_state() { return discovery_state; }
  44. // Returns the device acl state.
  45. bt_acl_state_t adapter_get_acl_state() { return acl_state; }
  46. // Returns the device bond state.
  47. bt_bond_state_t adapter_get_bond_state() { return bond_state; }
  48. // callback
  49. void acl_state_changed(bt_status_t status, RawAddress* remote_bd_addr,
  50. bt_acl_state_t state) {
  51. acl_state = state;
  52. CALLBACK_RET();
  53. }
  54. // callback
  55. void adapter_properties(bt_status_t status, int num_properties,
  56. bt_property_t* new_properties) {
  57. property_free_array(properties, property_count);
  58. properties = property_copy_array(new_properties, num_properties);
  59. property_count = num_properties;
  60. CALLBACK_RET();
  61. }
  62. // callback
  63. void adapter_state_changed(bt_state_t new_state) {
  64. state = new_state;
  65. CALLBACK_RET();
  66. }
  67. // callback
  68. void bond_state_changed(bt_status_t status, RawAddress* bdaddr,
  69. bt_bond_state_t state) {
  70. char buf[18];
  71. bond_state = state;
  72. const char* state_name = "Bond state unknown";
  73. switch (bond_state) {
  74. case BT_BOND_STATE_NONE:
  75. state_name = "Bond state none";
  76. break;
  77. case BT_BOND_STATE_BONDING:
  78. state_name = "Bond state bonding";
  79. break;
  80. case BT_BOND_STATE_BONDED:
  81. state_name = "Bond state bonded";
  82. break;
  83. // default none
  84. }
  85. fprintf(stdout, "Bond state changed callback addr:%s state:%s\n",
  86. bdaddr_to_string(bdaddr, buf, sizeof(buf)), state_name);
  87. CALLBACK_RET();
  88. }
  89. // callback
  90. void device_found(int num_properties, bt_property_t* property) {
  91. fprintf(stdout, "Device found num_properties:%d\n", num_properties);
  92. parse_properties(num_properties, property);
  93. CALLBACK_RET();
  94. }
  95. // callback
  96. void discovery_state_changed(bt_discovery_state_t state) {
  97. const char* state_name = "Unknown";
  98. discovery_state = state;
  99. switch (discovery_state) {
  100. case BT_DISCOVERY_STOPPED:
  101. state_name = "Discovery stopped";
  102. break;
  103. case BT_DISCOVERY_STARTED:
  104. state_name = "Discovery started";
  105. break;
  106. // default omitted
  107. }
  108. fprintf(stdout, "Discover state %s\n", state_name);
  109. CALLBACK_RET();
  110. }
  111. // callback
  112. void remote_device_properties(bt_status_t status, RawAddress* bdaddr,
  113. int num_properties, bt_property_t* properties) {
  114. char buf[18];
  115. fprintf(stdout, "Device found bdaddr:%s num_properties:%d\n",
  116. bdaddr_to_string(bdaddr, buf, sizeof(buf)), num_properties);
  117. parse_properties(num_properties, properties);
  118. CALLBACK_RET();
  119. }
  120. // callback
  121. void ssp_request(RawAddress* remote_bd_addr, bt_bdname_t* bd_name, uint32_t cod,
  122. bt_ssp_variant_t pairing_variant, uint32_t pass_key) {
  123. char* pairing_variant_name = "Unknown";
  124. switch (pairing_variant) {
  125. case BT_SSP_VARIANT_PASSKEY_CONFIRMATION:
  126. pairing_variant_name = "Passkey confirmation";
  127. break;
  128. case BT_SSP_VARIANT_PASSKEY_ENTRY:
  129. pairing_variant_name = "Passkey entry";
  130. break;
  131. case BT_SSP_VARIANT_CONSENT:
  132. pairing_variant_name = "Passkey consent";
  133. break;
  134. case BT_SSP_VARIANT_PASSKEY_NOTIFICATION:
  135. pairing_variant_name = "Passkey notification";
  136. break;
  137. }
  138. fprintf(stdout,
  139. "Got ssp request device_class:%u passkey:%x pairing_variant:%s\n",
  140. cod, pass_key, pairing_variant_name);
  141. char buf[18];
  142. fprintf(stdout, "Device found:%s %s\n",
  143. bdaddr_to_string(remote_bd_addr, buf, sizeof(buf)), bd_name->name);
  144. fprintf(stdout, "auto-accepting bond\n");
  145. bool accept = true;
  146. int rc = bt_interface->ssp_reply(remote_bd_addr, pairing_variant,
  147. (uint8_t)accept, pass_key);
  148. CALLBACK_RET();
  149. }
  150. // callback
  151. void thread_evt(bt_cb_thread_evt evt) { CALLBACK_RET(); }
  152. static void parse_properties(int num_properties, bt_property_t* property) {
  153. while (num_properties-- > 0) {
  154. switch (property->type) {
  155. case BT_PROPERTY_BDNAME: {
  156. const bt_bdname_t* name = property_as_name(property);
  157. if (name) fprintf(stdout, " name:%s\n", name->name);
  158. } break;
  159. case BT_PROPERTY_BDADDR: {
  160. char buf[18];
  161. const RawAddress* addr = property_as_addr(property);
  162. if (addr)
  163. fprintf(stdout, " addr:%s\n",
  164. bdaddr_to_string(addr, buf, sizeof(buf)));
  165. } break;
  166. case BT_PROPERTY_UUIDS: {
  167. size_t num_uuid;
  168. const Uuid* uuid = property_as_uuids(property, &num_uuid);
  169. if (uuid) {
  170. for (size_t i = 0; i < num_uuid; i++) {
  171. fprintf(stdout, " uuid:%zd: ", i);
  172. for (size_t j = 0; j < sizeof(uuid); j++) {
  173. fprintf(stdout, "%02x", uuid->uu[j]);
  174. }
  175. fprintf(stdout, "\n");
  176. }
  177. }
  178. } break;
  179. case BT_PROPERTY_TYPE_OF_DEVICE: {
  180. bt_device_type_t device_type = property_as_device_type(property);
  181. if (device_type) {
  182. const struct {
  183. const char* device_type;
  184. } device_type_lookup[] = {
  185. {"Unknown"},
  186. {"Classic Only"},
  187. {"BLE Only"},
  188. {"Both Classic and BLE"},
  189. };
  190. int idx = (int)device_type;
  191. if (idx > BT_DEVICE_DEVTYPE_DUAL) idx = 0;
  192. fprintf(stdout, " device_type:%s\n",
  193. device_type_lookup[idx].device_type);
  194. }
  195. } break;
  196. case BT_PROPERTY_CLASS_OF_DEVICE: {
  197. const bt_device_class_t* dc = property_as_device_class(property);
  198. int dc_int = device_class_to_int(dc);
  199. fprintf(stdout, " device_class:0x%x\n", dc_int);
  200. } break;
  201. case BT_PROPERTY_REMOTE_RSSI: {
  202. int8_t rssi = property_as_rssi(property);
  203. fprintf(stdout, " rssi:%d\n", rssi);
  204. } break;
  205. case BT_PROPERTY_REMOTE_FRIENDLY_NAME: {
  206. const bt_bdname_t* name = property_as_name(property);
  207. if (name) fprintf(stdout, " remote_name:%s\n", name->name);
  208. } break;
  209. case BT_PROPERTY_SERVICE_RECORD:
  210. case BT_PROPERTY_ADAPTER_SCAN_MODE:
  211. case BT_PROPERTY_ADAPTER_BONDED_DEVICES:
  212. case BT_PROPERTY_ADAPTER_DISCOVERY_TIMEOUT:
  213. case BT_PROPERTY_REMOTE_VERSION_INFO:
  214. case BT_PROPERTY_LOCAL_LE_FEATURES:
  215. case BT_PROPERTY_REMOTE_DEVICE_TIMESTAMP:
  216. default: {
  217. fprintf(stderr, "Unhandled property type:%d len:%d\n", property->type,
  218. property->len);
  219. uint8_t* p = (uint8_t*)property->val;
  220. for (int i = 0; i < property->len; ++i, p++) {
  221. fprintf(stderr, " %02x", *p);
  222. }
  223. if (property->len != 0) fprintf(stderr, "\n");
  224. }
  225. }
  226. property++;
  227. }
  228. }