blob: a85e3d0cbf9d352688150cf93b67dc12a0669054 [file] [log] [blame]
Chris Mantona6cee152014-10-09 16:04:05 -07001/******************************************************************************
2 *
Jakub Pawlowski5b790fe2017-09-18 09:00:20 -07003 * Copyright 2014 Google, Inc.
Chris Mantona6cee152014-10-09 16:04:05 -07004 *
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"
Chris Mantona6cee152014-10-09 16:04:05 -070021#include "btcore/include/property.h"
Chris Mantona6cee152014-10-09 16:04:05 -070022#include "support/callbacks.h"
23
24static bt_state_t state;
25static int property_count = 0;
Myles Watson911d1ae2016-11-28 16:44:40 -080026static bt_property_t* properties = NULL;
Chris Mantona6cee152014-10-09 16:04:05 -070027static bt_discovery_state_t discovery_state;
28static bt_acl_state_t acl_state;
29static bt_bond_state_t bond_state;
30
Myles Watson911d1ae2016-11-28 16:44:40 -080031static void parse_properties(int num_properties, bt_property_t* property);
Chris Mantona6cee152014-10-09 16:04:05 -070032
33// Returns the current adapter state.
Myles Watson911d1ae2016-11-28 16:44:40 -080034bt_state_t adapter_get_state() { return state; }
Chris Mantona6cee152014-10-09 16:04:05 -070035
36// Returns the number of adapter properties.
Myles Watson911d1ae2016-11-28 16:44:40 -080037int adapter_get_property_count() { return property_count; }
Chris Mantona6cee152014-10-09 16:04:05 -070038
39// Returns the specified property.
Myles Watson911d1ae2016-11-28 16:44:40 -080040bt_property_t* adapter_get_property(bt_property_type_t type) {
Chris Mantona6cee152014-10-09 16:04:05 -070041 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.
Myles Watson911d1ae2016-11-28 16:44:40 -080051bt_discovery_state_t adapter_get_discovery_state() { return discovery_state; }
Chris Mantona6cee152014-10-09 16:04:05 -070052
53// Returns the device acl state.
Myles Watson911d1ae2016-11-28 16:44:40 -080054bt_acl_state_t adapter_get_acl_state() { return acl_state; }
Chris Mantona6cee152014-10-09 16:04:05 -070055
56// Returns the device bond state.
Myles Watson911d1ae2016-11-28 16:44:40 -080057bt_bond_state_t adapter_get_bond_state() { return bond_state; }
Chris Mantona6cee152014-10-09 16:04:05 -070058
59// callback
Jakub Pawlowskia484a882017-06-24 17:30:18 -070060void acl_state_changed(bt_status_t status, RawAddress* remote_bd_addr,
Myles Watson911d1ae2016-11-28 16:44:40 -080061 bt_acl_state_t state) {
Chris Mantona6cee152014-10-09 16:04:05 -070062 acl_state = state;
63 CALLBACK_RET();
64}
65
66// callback
Myles Watson911d1ae2016-11-28 16:44:40 -080067void adapter_properties(bt_status_t status, int num_properties,
68 bt_property_t* new_properties) {
Chris Mantona6cee152014-10-09 16:04:05 -070069 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
77void adapter_state_changed(bt_state_t new_state) {
78 state = new_state;
79 CALLBACK_RET();
80}
81
82// callback
Jakub Pawlowskia484a882017-06-24 17:30:18 -070083void bond_state_changed(bt_status_t status, RawAddress* bdaddr,
Myles Watson911d1ae2016-11-28 16:44:40 -080084 bt_bond_state_t state) {
Chris Mantona6cee152014-10-09 16:04:05 -070085 char buf[18];
86 bond_state = state;
87
Myles Watson911d1ae2016-11-28 16:44:40 -080088 const char* state_name = "Bond state unknown";
Chris Mantona6cee152014-10-09 16:04:05 -070089 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 }
Myles Watson911d1ae2016-11-28 16:44:40 -0800104 fprintf(stdout, "Bond state changed callback addr:%s state:%s\n",
105 bdaddr_to_string(bdaddr, buf, sizeof(buf)), state_name);
Chris Mantona6cee152014-10-09 16:04:05 -0700106
107 CALLBACK_RET();
108}
109
110// callback
Myles Watson911d1ae2016-11-28 16:44:40 -0800111void device_found(int num_properties, bt_property_t* property) {
Chris Mantona6cee152014-10-09 16:04:05 -0700112 fprintf(stdout, "Device found num_properties:%d\n", num_properties);
113 parse_properties(num_properties, property);
114
115 CALLBACK_RET();
116}
117
118// callback
119void discovery_state_changed(bt_discovery_state_t state) {
Myles Watson911d1ae2016-11-28 16:44:40 -0800120 const char* state_name = "Unknown";
Chris Mantona6cee152014-10-09 16:04:05 -0700121 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
Jakub Pawlowskia484a882017-06-24 17:30:18 -0700140void remote_device_properties(bt_status_t status, RawAddress* bdaddr,
Myles Watson911d1ae2016-11-28 16:44:40 -0800141 int num_properties, bt_property_t* properties) {
Chris Mantona6cee152014-10-09 16:04:05 -0700142 char buf[18];
143 fprintf(stdout, "Device found bdaddr:%s num_properties:%d\n",
Myles Watson911d1ae2016-11-28 16:44:40 -0800144 bdaddr_to_string(bdaddr, buf, sizeof(buf)), num_properties);
Chris Mantona6cee152014-10-09 16:04:05 -0700145
146 parse_properties(num_properties, properties);
147
148 CALLBACK_RET();
149}
150
151// callback
Jakub Pawlowskia484a882017-06-24 17:30:18 -0700152void 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) {
Myles Watson911d1ae2016-11-28 16:44:40 -0800154 char* pairing_variant_name = "Unknown";
Chris Mantona6cee152014-10-09 16:04:05 -0700155
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
Myles Watson911d1ae2016-11-28 16:44:40 -0800173 fprintf(stdout,
174 "Got ssp request device_class:%u passkey:%x pairing_variant:%s\n",
175 cod, pass_key, pairing_variant_name);
Chris Mantona6cee152014-10-09 16:04:05 -0700176 char buf[18];
Myles Watson911d1ae2016-11-28 16:44:40 -0800177 fprintf(stdout, "Device found:%s %s\n",
178 bdaddr_to_string(remote_bd_addr, buf, sizeof(buf)), bd_name->name);
Chris Mantona6cee152014-10-09 16:04:05 -0700179
180 fprintf(stdout, "auto-accepting bond\n");
181 bool accept = true;
182 int rc = bt_interface->ssp_reply(remote_bd_addr, pairing_variant,
Myles Watson911d1ae2016-11-28 16:44:40 -0800183 (uint8_t)accept, pass_key);
Chris Mantona6cee152014-10-09 16:04:05 -0700184 CALLBACK_RET();
185}
186
187// callback
Myles Watson911d1ae2016-11-28 16:44:40 -0800188void thread_evt(bt_cb_thread_evt evt) { CALLBACK_RET(); }
Chris Mantona6cee152014-10-09 16:04:05 -0700189
Myles Watson911d1ae2016-11-28 16:44:40 -0800190static void parse_properties(int num_properties, bt_property_t* property) {
Chris Mantona6cee152014-10-09 16:04:05 -0700191 while (num_properties-- > 0) {
Myles Watson911d1ae2016-11-28 16:44:40 -0800192 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;
Chris Mantona6cee152014-10-09 16:04:05 -0700197
Myles Watson911d1ae2016-11-28 16:44:40 -0800198 case BT_PROPERTY_BDADDR: {
199 char buf[18];
Jakub Pawlowskia484a882017-06-24 17:30:18 -0700200 const RawAddress* addr = property_as_addr(property);
Myles Watson911d1ae2016-11-28 16:44:40 -0800201 if (addr)
202 fprintf(stdout, " addr:%s\n",
203 bdaddr_to_string(addr, buf, sizeof(buf)));
204 } break;
Chris Mantona6cee152014-10-09 16:04:05 -0700205
Myles Watson911d1ae2016-11-28 16:44:40 -0800206 case BT_PROPERTY_UUIDS: {
207 size_t num_uuid;
Jakub Pawlowski819e2ec2017-07-10 09:56:09 -0700208 const Uuid* uuid = property_as_uuids(property, &num_uuid);
Myles Watson911d1ae2016-11-28 16:44:40 -0800209 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]);
Chris Mantona6cee152014-10-09 16:04:05 -0700214 }
Myles Watson911d1ae2016-11-28 16:44:40 -0800215 fprintf(stdout, "\n");
Chris Mantona6cee152014-10-09 16:04:05 -0700216 }
217 }
Myles Watson911d1ae2016-11-28 16:44:40 -0800218 } break;
Chris Mantona6cee152014-10-09 16:04:05 -0700219
Myles Watson911d1ae2016-11-28 16:44:40 -0800220 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);
Chris Mantona6cee152014-10-09 16:04:05 -0700235 }
Myles Watson911d1ae2016-11-28 16:44:40 -0800236 } break;
Chris Mantona6cee152014-10-09 16:04:05 -0700237
Myles Watson911d1ae2016-11-28 16:44:40 -0800238 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;
Chris Mantona6cee152014-10-09 16:04:05 -0700243
Myles Watson911d1ae2016-11-28 16:44:40 -0800244 case BT_PROPERTY_REMOTE_RSSI: {
245 int8_t rssi = property_as_rssi(property);
246 fprintf(stdout, " rssi:%d\n", rssi);
247 } break;
Chris Mantona6cee152014-10-09 16:04:05 -0700248
Myles Watson911d1ae2016-11-28 16:44:40 -0800249 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;
Chris Mantona6cee152014-10-09 16:04:05 -0700253
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:
Myles Watson911d1ae2016-11-28 16:44:40 -0800261 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);
Chris Mantona6cee152014-10-09 16:04:05 -0700267 }
Myles Watson911d1ae2016-11-28 16:44:40 -0800268 if (property->len != 0) fprintf(stderr, "\n");
269 }
Chris Mantona6cee152014-10-09 16:04:05 -0700270 }
271 property++;
272 }
273}