blob: a556aa5370d763db97965cea3d9fbb0ec9213fe8 [file] [log] [blame]
Chris Mantona6cee152014-10-09 16:04:05 -07001/******************************************************************************
2 *
3 * Copyright (C) 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
Myles Watson911d1ae2016-11-28 16:44:40 -080019#include "support/adapter.h"
Chris Mantona6cee152014-10-09 16:04:05 -070020#include "base.h"
21#include "btcore/include/bdaddr.h"
22#include "btcore/include/property.h"
Chris Mantona6cee152014-10-09 16:04:05 -070023#include "support/callbacks.h"
24
25static bt_state_t state;
26static int property_count = 0;
Myles Watson911d1ae2016-11-28 16:44:40 -080027static bt_property_t* properties = NULL;
Chris Mantona6cee152014-10-09 16:04:05 -070028static bt_discovery_state_t discovery_state;
29static bt_acl_state_t acl_state;
30static bt_bond_state_t bond_state;
31
Myles Watson911d1ae2016-11-28 16:44:40 -080032static void parse_properties(int num_properties, bt_property_t* property);
Chris Mantona6cee152014-10-09 16:04:05 -070033
34// Returns the current adapter state.
Myles Watson911d1ae2016-11-28 16:44:40 -080035bt_state_t adapter_get_state() { return state; }
Chris Mantona6cee152014-10-09 16:04:05 -070036
37// Returns the number of adapter properties.
Myles Watson911d1ae2016-11-28 16:44:40 -080038int adapter_get_property_count() { return property_count; }
Chris Mantona6cee152014-10-09 16:04:05 -070039
40// Returns the specified property.
Myles Watson911d1ae2016-11-28 16:44:40 -080041bt_property_t* adapter_get_property(bt_property_type_t type) {
Chris Mantona6cee152014-10-09 16:04:05 -070042 for (int i = 0; i < property_count; ++i) {
43 if (properties[i].type == type) {
44 return &properties[i];
45 }
46 }
47
48 return NULL;
49}
50
51// Returns the device discovery state.
Myles Watson911d1ae2016-11-28 16:44:40 -080052bt_discovery_state_t adapter_get_discovery_state() { return discovery_state; }
Chris Mantona6cee152014-10-09 16:04:05 -070053
54// Returns the device acl state.
Myles Watson911d1ae2016-11-28 16:44:40 -080055bt_acl_state_t adapter_get_acl_state() { return acl_state; }
Chris Mantona6cee152014-10-09 16:04:05 -070056
57// Returns the device bond state.
Myles Watson911d1ae2016-11-28 16:44:40 -080058bt_bond_state_t adapter_get_bond_state() { return bond_state; }
Chris Mantona6cee152014-10-09 16:04:05 -070059
60// callback
Jakub Pawlowskia484a882017-06-24 17:30:18 -070061void acl_state_changed(bt_status_t status, RawAddress* remote_bd_addr,
Myles Watson911d1ae2016-11-28 16:44:40 -080062 bt_acl_state_t state) {
Chris Mantona6cee152014-10-09 16:04:05 -070063 acl_state = state;
64 CALLBACK_RET();
65}
66
67// callback
Myles Watson911d1ae2016-11-28 16:44:40 -080068void adapter_properties(bt_status_t status, int num_properties,
69 bt_property_t* new_properties) {
Chris Mantona6cee152014-10-09 16:04:05 -070070 property_free_array(properties, property_count);
71 properties = property_copy_array(new_properties, num_properties);
72 property_count = num_properties;
73
74 CALLBACK_RET();
75}
76
77// callback
78void adapter_state_changed(bt_state_t new_state) {
79 state = new_state;
80 CALLBACK_RET();
81}
82
83// callback
Jakub Pawlowskia484a882017-06-24 17:30:18 -070084void bond_state_changed(bt_status_t status, RawAddress* bdaddr,
Myles Watson911d1ae2016-11-28 16:44:40 -080085 bt_bond_state_t state) {
Chris Mantona6cee152014-10-09 16:04:05 -070086 char buf[18];
87 bond_state = state;
88
Myles Watson911d1ae2016-11-28 16:44:40 -080089 const char* state_name = "Bond state unknown";
Chris Mantona6cee152014-10-09 16:04:05 -070090 switch (bond_state) {
91 case BT_BOND_STATE_NONE:
92 state_name = "Bond state none";
93 break;
94
95 case BT_BOND_STATE_BONDING:
96 state_name = "Bond state bonding";
97 break;
98
99 case BT_BOND_STATE_BONDED:
100 state_name = "Bond state bonded";
101 break;
102
103 // default none
104 }
Myles Watson911d1ae2016-11-28 16:44:40 -0800105 fprintf(stdout, "Bond state changed callback addr:%s state:%s\n",
106 bdaddr_to_string(bdaddr, buf, sizeof(buf)), state_name);
Chris Mantona6cee152014-10-09 16:04:05 -0700107
108 CALLBACK_RET();
109}
110
111// callback
Myles Watson911d1ae2016-11-28 16:44:40 -0800112void device_found(int num_properties, bt_property_t* property) {
Chris Mantona6cee152014-10-09 16:04:05 -0700113 fprintf(stdout, "Device found num_properties:%d\n", num_properties);
114 parse_properties(num_properties, property);
115
116 CALLBACK_RET();
117}
118
119// callback
120void discovery_state_changed(bt_discovery_state_t state) {
Myles Watson911d1ae2016-11-28 16:44:40 -0800121 const char* state_name = "Unknown";
Chris Mantona6cee152014-10-09 16:04:05 -0700122 discovery_state = state;
123
124 switch (discovery_state) {
125 case BT_DISCOVERY_STOPPED:
126 state_name = "Discovery stopped";
127 break;
128
129 case BT_DISCOVERY_STARTED:
130 state_name = "Discovery started";
131 break;
132
133 // default omitted
134 }
135 fprintf(stdout, "Discover state %s\n", state_name);
136
137 CALLBACK_RET();
138}
139
140// callback
Jakub Pawlowskia484a882017-06-24 17:30:18 -0700141void remote_device_properties(bt_status_t status, RawAddress* bdaddr,
Myles Watson911d1ae2016-11-28 16:44:40 -0800142 int num_properties, bt_property_t* properties) {
Chris Mantona6cee152014-10-09 16:04:05 -0700143 char buf[18];
144 fprintf(stdout, "Device found bdaddr:%s num_properties:%d\n",
Myles Watson911d1ae2016-11-28 16:44:40 -0800145 bdaddr_to_string(bdaddr, buf, sizeof(buf)), num_properties);
Chris Mantona6cee152014-10-09 16:04:05 -0700146
147 parse_properties(num_properties, properties);
148
149 CALLBACK_RET();
150}
151
152// callback
Jakub Pawlowskia484a882017-06-24 17:30:18 -0700153void ssp_request(RawAddress* remote_bd_addr, bt_bdname_t* bd_name, uint32_t cod,
154 bt_ssp_variant_t pairing_variant, uint32_t pass_key) {
Myles Watson911d1ae2016-11-28 16:44:40 -0800155 char* pairing_variant_name = "Unknown";
Chris Mantona6cee152014-10-09 16:04:05 -0700156
157 switch (pairing_variant) {
158 case BT_SSP_VARIANT_PASSKEY_CONFIRMATION:
159 pairing_variant_name = "Passkey confirmation";
160 break;
161 case BT_SSP_VARIANT_PASSKEY_ENTRY:
162 pairing_variant_name = "Passkey entry";
163 break;
164
165 case BT_SSP_VARIANT_CONSENT:
166 pairing_variant_name = "Passkey consent";
167 break;
168
169 case BT_SSP_VARIANT_PASSKEY_NOTIFICATION:
170 pairing_variant_name = "Passkey notification";
171 break;
172 }
173
Myles Watson911d1ae2016-11-28 16:44:40 -0800174 fprintf(stdout,
175 "Got ssp request device_class:%u passkey:%x pairing_variant:%s\n",
176 cod, pass_key, pairing_variant_name);
Chris Mantona6cee152014-10-09 16:04:05 -0700177 char buf[18];
Myles Watson911d1ae2016-11-28 16:44:40 -0800178 fprintf(stdout, "Device found:%s %s\n",
179 bdaddr_to_string(remote_bd_addr, buf, sizeof(buf)), bd_name->name);
Chris Mantona6cee152014-10-09 16:04:05 -0700180
181 fprintf(stdout, "auto-accepting bond\n");
182 bool accept = true;
183 int rc = bt_interface->ssp_reply(remote_bd_addr, pairing_variant,
Myles Watson911d1ae2016-11-28 16:44:40 -0800184 (uint8_t)accept, pass_key);
Chris Mantona6cee152014-10-09 16:04:05 -0700185 CALLBACK_RET();
186}
187
188// callback
Myles Watson911d1ae2016-11-28 16:44:40 -0800189void thread_evt(bt_cb_thread_evt evt) { CALLBACK_RET(); }
Chris Mantona6cee152014-10-09 16:04:05 -0700190
Myles Watson911d1ae2016-11-28 16:44:40 -0800191static void parse_properties(int num_properties, bt_property_t* property) {
Chris Mantona6cee152014-10-09 16:04:05 -0700192 while (num_properties-- > 0) {
Myles Watson911d1ae2016-11-28 16:44:40 -0800193 switch (property->type) {
194 case BT_PROPERTY_BDNAME: {
195 const bt_bdname_t* name = property_as_name(property);
196 if (name) fprintf(stdout, " name:%s\n", name->name);
197 } break;
Chris Mantona6cee152014-10-09 16:04:05 -0700198
Myles Watson911d1ae2016-11-28 16:44:40 -0800199 case BT_PROPERTY_BDADDR: {
200 char buf[18];
Jakub Pawlowskia484a882017-06-24 17:30:18 -0700201 const RawAddress* addr = property_as_addr(property);
Myles Watson911d1ae2016-11-28 16:44:40 -0800202 if (addr)
203 fprintf(stdout, " addr:%s\n",
204 bdaddr_to_string(addr, buf, sizeof(buf)));
205 } break;
Chris Mantona6cee152014-10-09 16:04:05 -0700206
Myles Watson911d1ae2016-11-28 16:44:40 -0800207 case BT_PROPERTY_UUIDS: {
208 size_t num_uuid;
209 const bt_uuid_t* uuid = property_as_uuids(property, &num_uuid);
210 if (uuid) {
211 for (size_t i = 0; i < num_uuid; i++) {
212 fprintf(stdout, " uuid:%zd: ", i);
213 for (size_t j = 0; j < sizeof(uuid); j++) {
214 fprintf(stdout, "%02x", uuid->uu[j]);
Chris Mantona6cee152014-10-09 16:04:05 -0700215 }
Myles Watson911d1ae2016-11-28 16:44:40 -0800216 fprintf(stdout, "\n");
Chris Mantona6cee152014-10-09 16:04:05 -0700217 }
218 }
Myles Watson911d1ae2016-11-28 16:44:40 -0800219 } break;
Chris Mantona6cee152014-10-09 16:04:05 -0700220
Myles Watson911d1ae2016-11-28 16:44:40 -0800221 case BT_PROPERTY_TYPE_OF_DEVICE: {
222 bt_device_type_t device_type = property_as_device_type(property);
223 if (device_type) {
224 const struct {
225 const char* device_type;
226 } device_type_lookup[] = {
227 {"Unknown"},
228 {"Classic Only"},
229 {"BLE Only"},
230 {"Both Classic and BLE"},
231 };
232 int idx = (int)device_type;
233 if (idx > BT_DEVICE_DEVTYPE_DUAL) idx = 0;
234 fprintf(stdout, " device_type:%s\n",
235 device_type_lookup[idx].device_type);
Chris Mantona6cee152014-10-09 16:04:05 -0700236 }
Myles Watson911d1ae2016-11-28 16:44:40 -0800237 } break;
Chris Mantona6cee152014-10-09 16:04:05 -0700238
Myles Watson911d1ae2016-11-28 16:44:40 -0800239 case BT_PROPERTY_CLASS_OF_DEVICE: {
240 const bt_device_class_t* dc = property_as_device_class(property);
241 int dc_int = device_class_to_int(dc);
242 fprintf(stdout, " device_class:0x%x\n", dc_int);
243 } break;
Chris Mantona6cee152014-10-09 16:04:05 -0700244
Myles Watson911d1ae2016-11-28 16:44:40 -0800245 case BT_PROPERTY_REMOTE_RSSI: {
246 int8_t rssi = property_as_rssi(property);
247 fprintf(stdout, " rssi:%d\n", rssi);
248 } break;
Chris Mantona6cee152014-10-09 16:04:05 -0700249
Myles Watson911d1ae2016-11-28 16:44:40 -0800250 case BT_PROPERTY_REMOTE_FRIENDLY_NAME: {
251 const bt_bdname_t* name = property_as_name(property);
252 if (name) fprintf(stdout, " remote_name:%s\n", name->name);
253 } break;
Chris Mantona6cee152014-10-09 16:04:05 -0700254
255 case BT_PROPERTY_SERVICE_RECORD:
256 case BT_PROPERTY_ADAPTER_SCAN_MODE:
257 case BT_PROPERTY_ADAPTER_BONDED_DEVICES:
258 case BT_PROPERTY_ADAPTER_DISCOVERY_TIMEOUT:
259 case BT_PROPERTY_REMOTE_VERSION_INFO:
260 case BT_PROPERTY_LOCAL_LE_FEATURES:
261 case BT_PROPERTY_REMOTE_DEVICE_TIMESTAMP:
Myles Watson911d1ae2016-11-28 16:44:40 -0800262 default: {
263 fprintf(stderr, "Unhandled property type:%d len:%d\n", property->type,
264 property->len);
265 uint8_t* p = (uint8_t*)property->val;
266 for (int i = 0; i < property->len; ++i, p++) {
267 fprintf(stderr, " %02x", *p);
Chris Mantona6cee152014-10-09 16:04:05 -0700268 }
Myles Watson911d1ae2016-11-28 16:44:40 -0800269 if (property->len != 0) fprintf(stderr, "\n");
270 }
Chris Mantona6cee152014-10-09 16:04:05 -0700271 }
272 property++;
273 }
274}