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