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 19 #include "support/adapter.h" 20 #include "base.h" 21 #include "btcore/include/property.h" 22 #include "support/callbacks.h" 23 24 static bt_state_t state; 25 static int property_count = 0; 26 static bt_property_t* properties = NULL; 27 static bt_discovery_state_t discovery_state; 28 static bt_acl_state_t acl_state; 29 static bt_bond_state_t bond_state; 30 31 static void parse_properties(int num_properties, bt_property_t* property); 32 33 // Returns the current adapter state. 34 bt_state_t adapter_get_state() { return state; } 35 36 // Returns the number of adapter properties. 37 int adapter_get_property_count() { return property_count; } 38 39 // Returns the specified property. 40 bt_property_t* adapter_get_property(bt_property_type_t type) { 41 for (int i = 0; i < property_count; ++i) { 42 if (properties[i].type == type) { 43 return &properties[i]; 44 } 45 } 46 47 return NULL; 48 } 49 50 // Returns the device discovery state. 51 bt_discovery_state_t adapter_get_discovery_state() { return discovery_state; } 52 53 // Returns the device acl state. 54 bt_acl_state_t adapter_get_acl_state() { return acl_state; } 55 56 // Returns the device bond state. 57 bt_bond_state_t adapter_get_bond_state() { return bond_state; } 58 59 // callback 60 void acl_state_changed(bt_status_t status, RawAddress* remote_bd_addr, 61 bt_acl_state_t state) { 62 acl_state = state; 63 CALLBACK_RET(); 64 } 65 66 // callback 67 void adapter_properties(bt_status_t status, int num_properties, 68 bt_property_t* new_properties) { 69 property_free_array(properties, property_count); 70 properties = property_copy_array(new_properties, num_properties); 71 property_count = num_properties; 72 73 CALLBACK_RET(); 74 } 75 76 // callback 77 void adapter_state_changed(bt_state_t new_state) { 78 state = new_state; 79 CALLBACK_RET(); 80 } 81 82 // callback 83 void bond_state_changed(bt_status_t status, RawAddress* bdaddr, 84 bt_bond_state_t state) { 85 char buf[18]; 86 bond_state = state; 87 88 const char* state_name = "Bond state unknown"; 89 switch (bond_state) { 90 case BT_BOND_STATE_NONE: 91 state_name = "Bond state none"; 92 break; 93 94 case BT_BOND_STATE_BONDING: 95 state_name = "Bond state bonding"; 96 break; 97 98 case BT_BOND_STATE_BONDED: 99 state_name = "Bond state bonded"; 100 break; 101 102 // default none 103 } 104 fprintf(stdout, "Bond state changed callback addr:%s state:%s\n", 105 bdaddr_to_string(bdaddr, buf, sizeof(buf)), state_name); 106 107 CALLBACK_RET(); 108 } 109 110 // callback 111 void device_found(int num_properties, bt_property_t* property) { 112 fprintf(stdout, "Device found num_properties:%d\n", num_properties); 113 parse_properties(num_properties, property); 114 115 CALLBACK_RET(); 116 } 117 118 // callback 119 void discovery_state_changed(bt_discovery_state_t state) { 120 const char* state_name = "Unknown"; 121 discovery_state = state; 122 123 switch (discovery_state) { 124 case BT_DISCOVERY_STOPPED: 125 state_name = "Discovery stopped"; 126 break; 127 128 case BT_DISCOVERY_STARTED: 129 state_name = "Discovery started"; 130 break; 131 132 // default omitted 133 } 134 fprintf(stdout, "Discover state %s\n", state_name); 135 136 CALLBACK_RET(); 137 } 138 139 // callback 140 void remote_device_properties(bt_status_t status, RawAddress* bdaddr, 141 int num_properties, bt_property_t* properties) { 142 char buf[18]; 143 fprintf(stdout, "Device found bdaddr:%s num_properties:%d\n", 144 bdaddr_to_string(bdaddr, buf, sizeof(buf)), num_properties); 145 146 parse_properties(num_properties, properties); 147 148 CALLBACK_RET(); 149 } 150 151 // callback 152 void ssp_request(RawAddress* remote_bd_addr, bt_bdname_t* bd_name, uint32_t cod, 153 bt_ssp_variant_t pairing_variant, uint32_t pass_key) { 154 char* pairing_variant_name = "Unknown"; 155 156 switch (pairing_variant) { 157 case BT_SSP_VARIANT_PASSKEY_CONFIRMATION: 158 pairing_variant_name = "Passkey confirmation"; 159 break; 160 case BT_SSP_VARIANT_PASSKEY_ENTRY: 161 pairing_variant_name = "Passkey entry"; 162 break; 163 164 case BT_SSP_VARIANT_CONSENT: 165 pairing_variant_name = "Passkey consent"; 166 break; 167 168 case BT_SSP_VARIANT_PASSKEY_NOTIFICATION: 169 pairing_variant_name = "Passkey notification"; 170 break; 171 } 172 173 fprintf(stdout, 174 "Got ssp request device_class:%u passkey:%x pairing_variant:%s\n", 175 cod, pass_key, pairing_variant_name); 176 char buf[18]; 177 fprintf(stdout, "Device found:%s %s\n", 178 bdaddr_to_string(remote_bd_addr, buf, sizeof(buf)), bd_name->name); 179 180 fprintf(stdout, "auto-accepting bond\n"); 181 bool accept = true; 182 int rc = bt_interface->ssp_reply(remote_bd_addr, pairing_variant, 183 (uint8_t)accept, pass_key); 184 CALLBACK_RET(); 185 } 186 187 // callback 188 void thread_evt(bt_cb_thread_evt evt) { CALLBACK_RET(); } 189 190 static void parse_properties(int num_properties, bt_property_t* property) { 191 while (num_properties-- > 0) { 192 switch (property->type) { 193 case BT_PROPERTY_BDNAME: { 194 const bt_bdname_t* name = property_as_name(property); 195 if (name) fprintf(stdout, " name:%s\n", name->name); 196 } break; 197 198 case BT_PROPERTY_BDADDR: { 199 char buf[18]; 200 const RawAddress* addr = property_as_addr(property); 201 if (addr) 202 fprintf(stdout, " addr:%s\n", 203 bdaddr_to_string(addr, buf, sizeof(buf))); 204 } break; 205 206 case BT_PROPERTY_UUIDS: { 207 size_t num_uuid; 208 const Uuid* uuid = property_as_uuids(property, &num_uuid); 209 if (uuid) { 210 for (size_t i = 0; i < num_uuid; i++) { 211 fprintf(stdout, " uuid:%zd: ", i); 212 for (size_t j = 0; j < sizeof(uuid); j++) { 213 fprintf(stdout, "%02x", uuid->uu[j]); 214 } 215 fprintf(stdout, "\n"); 216 } 217 } 218 } break; 219 220 case BT_PROPERTY_TYPE_OF_DEVICE: { 221 bt_device_type_t device_type = property_as_device_type(property); 222 if (device_type) { 223 const struct { 224 const char* device_type; 225 } device_type_lookup[] = { 226 {"Unknown"}, 227 {"Classic Only"}, 228 {"BLE Only"}, 229 {"Both Classic and BLE"}, 230 }; 231 int idx = (int)device_type; 232 if (idx > BT_DEVICE_DEVTYPE_DUAL) idx = 0; 233 fprintf(stdout, " device_type:%s\n", 234 device_type_lookup[idx].device_type); 235 } 236 } break; 237 238 case BT_PROPERTY_CLASS_OF_DEVICE: { 239 const bt_device_class_t* dc = property_as_device_class(property); 240 int dc_int = device_class_to_int(dc); 241 fprintf(stdout, " device_class:0x%x\n", dc_int); 242 } break; 243 244 case BT_PROPERTY_REMOTE_RSSI: { 245 int8_t rssi = property_as_rssi(property); 246 fprintf(stdout, " rssi:%d\n", rssi); 247 } break; 248 249 case BT_PROPERTY_REMOTE_FRIENDLY_NAME: { 250 const bt_bdname_t* name = property_as_name(property); 251 if (name) fprintf(stdout, " remote_name:%s\n", name->name); 252 } break; 253 254 case BT_PROPERTY_SERVICE_RECORD: 255 case BT_PROPERTY_ADAPTER_SCAN_MODE: 256 case BT_PROPERTY_ADAPTER_BONDED_DEVICES: 257 case BT_PROPERTY_ADAPTER_DISCOVERY_TIMEOUT: 258 case BT_PROPERTY_REMOTE_VERSION_INFO: 259 case BT_PROPERTY_LOCAL_LE_FEATURES: 260 case BT_PROPERTY_REMOTE_DEVICE_TIMESTAMP: 261 default: { 262 fprintf(stderr, "Unhandled property type:%d len:%d\n", property->type, 263 property->len); 264 uint8_t* p = (uint8_t*)property->val; 265 for (int i = 0; i < property->len; ++i, p++) { 266 fprintf(stderr, " %02x", *p); 267 } 268 if (property->len != 0) fprintf(stderr, "\n"); 269 } 270 } 271 property++; 272 } 273 } 274