OSDN Git Service

Pass unknown run_unit_tests long arguments to tests am: 6babef0456 am: bc2b435ab4
[android-x86/system-bt.git] / tools / bdtool / adapter.c
1 /******************************************************************************
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
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;
31
32 static void parse_properties(int num_properties, bt_property_t *property);
33
34 // Returns the current adapter state.
35 bt_state_t adapter_get_state() {
36   return state;
37 }
38
39 // Returns the number of adapter properties.
40 int adapter_get_property_count() {
41   return property_count;
42 }
43
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];
49     }
50   }
51
52   return NULL;
53 }
54
55 // Returns the device discovery state.
56 bt_discovery_state_t adapter_get_discovery_state() {
57   return discovery_state;
58 }
59
60 // Returns the device acl state.
61 bt_acl_state_t adapter_get_acl_state() {
62   return acl_state;
63 }
64
65 // Returns the device bond state.
66 bt_bond_state_t adapter_get_bond_state() {
67   return bond_state;
68 }
69
70 // callback
71 void 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
77 void 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
88 void adapter_state_changed(bt_state_t new_state) {
89   state = new_state;
90   CALLBACK_RET();
91 }
92
93 // callback
94 void 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
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);
125
126   CALLBACK_RET();
127 }
128
129 // callback
130 void 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
151 void 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
165 void 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
203 void thread_evt(bt_cb_thread_evt evt) {
204   CALLBACK_RET();
205 }
206
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:
211         {
212           const bt_bdname_t *name = property_as_name(property);
213           if (name)
214             fprintf(stdout, " name:%s\n", name->name);
215         }
216         break;
217
218       case BT_PROPERTY_BDADDR:
219         {
220           char buf[18];
221           const bt_bdaddr_t *addr = property_as_addr(property);
222           if (addr)
223             fprintf(stdout, " addr:%s\n", bdaddr_to_string(addr, buf, sizeof(buf)));
224         }
225         break;
226
227       case BT_PROPERTY_UUIDS:
228         {
229           size_t num_uuid;
230           const bt_uuid_t *uuid = property_as_uuids(property, &num_uuid);
231           if (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]);
236               }
237               fprintf(stdout, "\n");
238             }
239           }
240         }
241         break;
242
243       case BT_PROPERTY_TYPE_OF_DEVICE:
244         {
245           bt_device_type_t device_type = property_as_device_type(property);
246           if (device_type) {
247             const struct {
248               const char * device_type;
249             } device_type_lookup[] = {
250               { "Unknown" },
251               { "Classic Only" },
252               { "BLE Only" },
253               { "Both Classic and BLE" },
254             };
255             int idx = (int)device_type;
256             if (idx > BT_DEVICE_DEVTYPE_DUAL)
257               idx = 0;
258             fprintf(stdout, " device_type:%s\n", device_type_lookup[idx].device_type);
259           }
260         }
261         break;
262
263       case BT_PROPERTY_CLASS_OF_DEVICE:
264         {
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);
268         }
269         break;
270
271       case BT_PROPERTY_REMOTE_RSSI:
272         {
273           int8_t rssi = property_as_rssi(property);
274           fprintf(stdout, " rssi:%d\n", rssi);
275         }
276         break;
277
278       case BT_PROPERTY_REMOTE_FRIENDLY_NAME:
279         {
280           const bt_bdname_t *name = property_as_name(property);
281           if (name)
282             fprintf(stdout, " remote_name:%s\n", name->name);
283         }
284         break;
285
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:
293       default:
294         {
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);
299           }
300           if (property->len != 0)
301             fprintf(stderr, "\n");
302         }
303     }
304     property++;
305   }
306 }