1 /******************************************************************************
3 * Copyright (C) 2014 Google, Inc.
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:
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 ******************************************************************************/
20 #include "btcore/include/bdaddr.h"
21 #include "btcore/include/property.h"
22 #include "support/adapter.h"
23 #include "support/callbacks.h"
25 static bt_state_t state;
26 static int property_count = 0;
27 static bt_property_t *properties = NULL;
28 static bt_discovery_state_t discovery_state;
29 static bt_acl_state_t acl_state;
30 static bt_bond_state_t bond_state;
32 static void parse_properties(int num_properties, bt_property_t *property);
34 // Returns the current adapter state.
35 bt_state_t adapter_get_state() {
39 // Returns the number of adapter properties.
40 int adapter_get_property_count() {
41 return property_count;
44 // Returns the specified property.
45 bt_property_t *adapter_get_property(bt_property_type_t type) {
46 for (int i = 0; i < property_count; ++i) {
47 if (properties[i].type == type) {
48 return &properties[i];
55 // Returns the device discovery state.
56 bt_discovery_state_t adapter_get_discovery_state() {
57 return discovery_state;
60 // Returns the device acl state.
61 bt_acl_state_t adapter_get_acl_state() {
65 // Returns the device bond state.
66 bt_bond_state_t adapter_get_bond_state() {
71 void acl_state_changed(bt_status_t status, bt_bdaddr_t *remote_bd_addr, bt_acl_state_t state) {
77 void adapter_properties(bt_status_t status,
79 bt_property_t *new_properties) {
80 property_free_array(properties, property_count);
81 properties = property_copy_array(new_properties, num_properties);
82 property_count = num_properties;
88 void adapter_state_changed(bt_state_t new_state) {
94 void bond_state_changed(bt_status_t status,
96 bt_bond_state_t state) {
100 const char *state_name = "Bond state unknown";
101 switch (bond_state) {
102 case BT_BOND_STATE_NONE:
103 state_name = "Bond state none";
106 case BT_BOND_STATE_BONDING:
107 state_name = "Bond state bonding";
110 case BT_BOND_STATE_BONDED:
111 state_name = "Bond state bonded";
116 fprintf(stdout, "Bond state changed callback addr:%s state:%s\n", bdaddr_to_string(bdaddr, buf, sizeof(buf)), state_name);
122 void device_found(int num_properties, bt_property_t *property) {
123 fprintf(stdout, "Device found num_properties:%d\n", num_properties);
124 parse_properties(num_properties, property);
130 void discovery_state_changed(bt_discovery_state_t state) {
131 const char *state_name = "Unknown";
132 discovery_state = state;
134 switch (discovery_state) {
135 case BT_DISCOVERY_STOPPED:
136 state_name = "Discovery stopped";
139 case BT_DISCOVERY_STARTED:
140 state_name = "Discovery started";
145 fprintf(stdout, "Discover state %s\n", state_name);
151 void remote_device_properties(bt_status_t status,
154 bt_property_t *properties) {
156 fprintf(stdout, "Device found bdaddr:%s num_properties:%d\n",
157 bdaddr_to_string(bdaddr, buf, sizeof(buf)), num_properties);
159 parse_properties(num_properties, properties);
166 bt_bdaddr_t *remote_bd_addr,
167 bt_bdname_t *bd_name,
169 bt_ssp_variant_t pairing_variant,
171 char *pairing_variant_name = "Unknown";
173 switch (pairing_variant) {
174 case BT_SSP_VARIANT_PASSKEY_CONFIRMATION:
175 pairing_variant_name = "Passkey confirmation";
177 case BT_SSP_VARIANT_PASSKEY_ENTRY:
178 pairing_variant_name = "Passkey entry";
181 case BT_SSP_VARIANT_CONSENT:
182 pairing_variant_name = "Passkey consent";
185 case BT_SSP_VARIANT_PASSKEY_NOTIFICATION:
186 pairing_variant_name = "Passkey notification";
190 fprintf(stdout, "Got ssp request device_class:%u passkey:%x pairing_variant:%s\n", cod, pass_key, pairing_variant_name);
192 fprintf(stdout, "Device found:%s %s\n", bdaddr_to_string(remote_bd_addr, buf, sizeof(buf)), bd_name->name);
195 fprintf(stdout, "auto-accepting bond\n");
197 int rc = bt_interface->ssp_reply(remote_bd_addr, pairing_variant,
198 (uint8_t)accept, pass_key);
203 void thread_evt(bt_cb_thread_evt evt) {
207 static void parse_properties(int num_properties, bt_property_t *property) {
208 while (num_properties-- > 0) {
209 switch(property->type) {
210 case BT_PROPERTY_BDNAME:
212 const bt_bdname_t *name = property_as_name(property);
214 fprintf(stdout, " name:%s\n", name->name);
218 case BT_PROPERTY_BDADDR:
221 const bt_bdaddr_t *addr = property_as_addr(property);
223 fprintf(stdout, " addr:%s\n", bdaddr_to_string(addr, buf, sizeof(buf)));
227 case BT_PROPERTY_UUIDS:
230 const bt_uuid_t *uuid = property_as_uuids(property, &num_uuid);
232 for (size_t i = 0; i < num_uuid; i++) {
233 fprintf(stdout, " uuid:%zd: ", i);
234 for (size_t j = 0; j < sizeof(uuid); j++) {
235 fprintf(stdout, "%02x", uuid->uu[j]);
237 fprintf(stdout, "\n");
243 case BT_PROPERTY_TYPE_OF_DEVICE:
245 bt_device_type_t device_type = property_as_device_type(property);
248 const char * device_type;
249 } device_type_lookup[] = {
253 { "Both Classic and BLE" },
255 int idx = (int)device_type;
256 if (idx > BT_DEVICE_DEVTYPE_DUAL)
258 fprintf(stdout, " device_type:%s\n", device_type_lookup[idx].device_type);
263 case BT_PROPERTY_CLASS_OF_DEVICE:
265 const bt_device_class_t *dc = property_as_device_class(property);
266 int dc_int = device_class_to_int(dc);
267 fprintf(stdout, " device_class:0x%x\n", dc_int);
271 case BT_PROPERTY_REMOTE_RSSI:
273 int8_t rssi = property_as_rssi(property);
274 fprintf(stdout, " rssi:%d\n", rssi);
278 case BT_PROPERTY_REMOTE_FRIENDLY_NAME:
280 const bt_bdname_t *name = property_as_name(property);
282 fprintf(stdout, " remote_name:%s\n", name->name);
286 case BT_PROPERTY_SERVICE_RECORD:
287 case BT_PROPERTY_ADAPTER_SCAN_MODE:
288 case BT_PROPERTY_ADAPTER_BONDED_DEVICES:
289 case BT_PROPERTY_ADAPTER_DISCOVERY_TIMEOUT:
290 case BT_PROPERTY_REMOTE_VERSION_INFO:
291 case BT_PROPERTY_LOCAL_LE_FEATURES:
292 case BT_PROPERTY_REMOTE_DEVICE_TIMESTAMP:
295 fprintf(stderr, "Unhandled property type:%d len:%d\n", property->type, property->len);
296 uint8_t *p = (uint8_t *)property->val;
297 for (int i = 0; i < property->len; ++i, p++) {
298 fprintf(stderr, " %02x", *p);
300 if (property->len != 0)
301 fprintf(stderr, "\n");