1 /******************************************************************************
2 *
3 * Copyright (c) 2014 The Android Open Source Project
4 * Copyright 2009-2016 Broadcom Corporation
5 *
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at:
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 *
18 ******************************************************************************/
19
20 /*******************************************************************************
21 *
22 * Filename: btif_util.c
23 *
24 * Description: Miscellaneous helper functions
25 *
26 *
27 ******************************************************************************/
28
29 #define LOG_TAG "bt_btif_util"
30
31 #include "btif_util.h"
32
33 #include <base/logging.h>
34 #include <ctype.h>
35 #include <netinet/in.h>
36 #include <stdio.h>
37 #include <stdlib.h>
38 #include <string.h>
39
40 #include <hardware/bt_av.h>
41
42 #include "avrc_defs.h"
43 #include "bt_common.h"
44 #include "bta_ag_api.h"
45 #include "bta_api.h"
46 #include "bta_av_api.h"
47 #include "bta_hd_api.h"
48 #include "bta_hf_client_api.h"
49 #include "bta_hh_api.h"
50 #include "bte.h"
51 #include "btif_common.h"
52 #include "btif_dm.h"
53 #include "btu.h"
54
55 /*******************************************************************************
56 * Constants & Macros
57 ******************************************************************************/
58 #define ISDIGIT(a) (((a) >= '0') && ((a) <= '9'))
59
60 /*****************************************************************************
61 * Logging helper functions
62 ****************************************************************************/
63
devclass2uint(DEV_CLASS dev_class)64 uint32_t devclass2uint(DEV_CLASS dev_class) {
65 uint32_t cod = 0;
66
67 if (dev_class != NULL) {
68 /* if COD is 0, irrespective of the device type set it to Unclassified
69 * device */
70 cod = (dev_class[2]) | (dev_class[1] << 8) | (dev_class[0] << 16);
71 }
72 return cod;
73 }
uint2devclass(uint32_t cod,DEV_CLASS dev_class)74 void uint2devclass(uint32_t cod, DEV_CLASS dev_class) {
75 dev_class[2] = (uint8_t)cod;
76 dev_class[1] = (uint8_t)(cod >> 8);
77 dev_class[0] = (uint8_t)(cod >> 16);
78 }
79
80 /*****************************************************************************
81 * Function ascii_2_hex
82 *
83 * Description This function converts an ASCII string into HEX
84 *
85 * Returns the number of hex bytes filled.
86 */
ascii_2_hex(const char * p_ascii,int len,uint8_t * p_hex)87 int ascii_2_hex(const char* p_ascii, int len, uint8_t* p_hex) {
88 int x;
89 uint8_t c;
90
91 for (x = 0; (x < len) && (*p_ascii); x++) {
92 if (ISDIGIT(*p_ascii))
93 c = (*p_ascii - '0') << 4;
94 else
95 c = (toupper(*p_ascii) - 'A' + 10) << 4;
96
97 p_ascii++;
98 if (*p_ascii) {
99 if (ISDIGIT(*p_ascii))
100 c |= (*p_ascii - '0');
101 else
102 c |= (toupper(*p_ascii) - 'A' + 10);
103
104 p_ascii++;
105 }
106 *p_hex++ = c;
107 }
108
109 return (x);
110 }
111
dump_dm_search_event(uint16_t event)112 const char* dump_dm_search_event(uint16_t event) {
113 switch (event) {
114 CASE_RETURN_STR(BTA_DM_INQ_RES_EVT)
115 CASE_RETURN_STR(BTA_DM_INQ_CMPL_EVT)
116 CASE_RETURN_STR(BTA_DM_DISC_RES_EVT)
117 CASE_RETURN_STR(BTA_DM_DISC_BLE_RES_EVT)
118 CASE_RETURN_STR(BTA_DM_DISC_CMPL_EVT)
119 CASE_RETURN_STR(BTA_DM_SEARCH_CANCEL_CMPL_EVT)
120
121 default:
122 return "UNKNOWN MSG ID";
123 }
124 }
125
dump_property_type(bt_property_type_t type)126 const char* dump_property_type(bt_property_type_t type) {
127 switch (type) {
128 CASE_RETURN_STR(BT_PROPERTY_BDNAME)
129 CASE_RETURN_STR(BT_PROPERTY_BDADDR)
130 CASE_RETURN_STR(BT_PROPERTY_UUIDS)
131 CASE_RETURN_STR(BT_PROPERTY_CLASS_OF_DEVICE)
132 CASE_RETURN_STR(BT_PROPERTY_TYPE_OF_DEVICE)
133 CASE_RETURN_STR(BT_PROPERTY_REMOTE_RSSI)
134 CASE_RETURN_STR(BT_PROPERTY_ADAPTER_DISCOVERY_TIMEOUT)
135 CASE_RETURN_STR(BT_PROPERTY_ADAPTER_BONDED_DEVICES)
136 CASE_RETURN_STR(BT_PROPERTY_ADAPTER_SCAN_MODE)
137 CASE_RETURN_STR(BT_PROPERTY_REMOTE_FRIENDLY_NAME)
138
139 default:
140 return "UNKNOWN PROPERTY ID";
141 }
142 }
143
dump_dm_event(uint16_t event)144 const char* dump_dm_event(uint16_t event) {
145 switch (event) {
146 CASE_RETURN_STR(BTA_DM_PIN_REQ_EVT)
147 CASE_RETURN_STR(BTA_DM_AUTH_CMPL_EVT)
148 CASE_RETURN_STR(BTA_DM_LINK_UP_EVT)
149 CASE_RETURN_STR(BTA_DM_LINK_DOWN_EVT)
150 CASE_RETURN_STR(BTA_DM_BOND_CANCEL_CMPL_EVT)
151 CASE_RETURN_STR(BTA_DM_SP_CFM_REQ_EVT)
152 CASE_RETURN_STR(BTA_DM_SP_KEY_NOTIF_EVT)
153 CASE_RETURN_STR(BTA_DM_BLE_KEY_EVT)
154 CASE_RETURN_STR(BTA_DM_BLE_SEC_REQ_EVT)
155 CASE_RETURN_STR(BTA_DM_BLE_PASSKEY_NOTIF_EVT)
156 CASE_RETURN_STR(BTA_DM_BLE_PASSKEY_REQ_EVT)
157 CASE_RETURN_STR(BTA_DM_BLE_OOB_REQ_EVT)
158 CASE_RETURN_STR(BTA_DM_BLE_LOCAL_IR_EVT)
159 CASE_RETURN_STR(BTA_DM_BLE_LOCAL_ER_EVT)
160 CASE_RETURN_STR(BTA_DM_BLE_AUTH_CMPL_EVT)
161 CASE_RETURN_STR(BTA_DM_DEV_UNPAIRED_EVT)
162 CASE_RETURN_STR(BTA_DM_ENER_INFO_READ)
163
164 default:
165 return "UNKNOWN DM EVENT";
166 }
167 }
168
dump_hf_event(uint16_t event)169 const char* dump_hf_event(uint16_t event) {
170 switch (event) {
171 CASE_RETURN_STR(BTA_AG_ENABLE_EVT)
172 CASE_RETURN_STR(BTA_AG_REGISTER_EVT)
173 CASE_RETURN_STR(BTA_AG_OPEN_EVT)
174 CASE_RETURN_STR(BTA_AG_CLOSE_EVT)
175 CASE_RETURN_STR(BTA_AG_CONN_EVT)
176 CASE_RETURN_STR(BTA_AG_AUDIO_OPEN_EVT)
177 CASE_RETURN_STR(BTA_AG_AUDIO_CLOSE_EVT)
178 CASE_RETURN_STR(BTA_AG_SPK_EVT)
179 CASE_RETURN_STR(BTA_AG_MIC_EVT)
180 CASE_RETURN_STR(BTA_AG_AT_CKPD_EVT)
181 CASE_RETURN_STR(BTA_AG_DISABLE_EVT)
182 CASE_RETURN_STR(BTA_AG_WBS_EVT)
183 CASE_RETURN_STR(BTA_AG_AT_A_EVT)
184 CASE_RETURN_STR(BTA_AG_AT_D_EVT)
185 CASE_RETURN_STR(BTA_AG_AT_CHLD_EVT)
186 CASE_RETURN_STR(BTA_AG_AT_CHUP_EVT)
187 CASE_RETURN_STR(BTA_AG_AT_CIND_EVT)
188 CASE_RETURN_STR(BTA_AG_AT_VTS_EVT)
189 CASE_RETURN_STR(BTA_AG_AT_BINP_EVT)
190 CASE_RETURN_STR(BTA_AG_AT_BLDN_EVT)
191 CASE_RETURN_STR(BTA_AG_AT_BVRA_EVT)
192 CASE_RETURN_STR(BTA_AG_AT_NREC_EVT)
193 CASE_RETURN_STR(BTA_AG_AT_CNUM_EVT)
194 CASE_RETURN_STR(BTA_AG_AT_BTRH_EVT)
195 CASE_RETURN_STR(BTA_AG_AT_CLCC_EVT)
196 CASE_RETURN_STR(BTA_AG_AT_COPS_EVT)
197 CASE_RETURN_STR(BTA_AG_AT_UNAT_EVT)
198 CASE_RETURN_STR(BTA_AG_AT_CBC_EVT)
199 CASE_RETURN_STR(BTA_AG_AT_BAC_EVT)
200 CASE_RETURN_STR(BTA_AG_AT_BCS_EVT)
201 CASE_RETURN_STR(BTA_AG_AT_BIND_EVT)
202 CASE_RETURN_STR(BTA_AG_AT_BIEV_EVT)
203 CASE_RETURN_STR(BTA_AG_AT_BIA_EVT)
204
205 default:
206 return "UNKNOWN MSG ID";
207 }
208 }
209
dump_hf_client_event(uint16_t event)210 const char* dump_hf_client_event(uint16_t event) {
211 switch (event) {
212 CASE_RETURN_STR(BTA_HF_CLIENT_ENABLE_EVT)
213 CASE_RETURN_STR(BTA_HF_CLIENT_REGISTER_EVT)
214 CASE_RETURN_STR(BTA_HF_CLIENT_OPEN_EVT)
215 CASE_RETURN_STR(BTA_HF_CLIENT_CLOSE_EVT)
216 CASE_RETURN_STR(BTA_HF_CLIENT_CONN_EVT)
217 CASE_RETURN_STR(BTA_HF_CLIENT_AUDIO_OPEN_EVT)
218 CASE_RETURN_STR(BTA_HF_CLIENT_AUDIO_MSBC_OPEN_EVT)
219 CASE_RETURN_STR(BTA_HF_CLIENT_AUDIO_CLOSE_EVT)
220 CASE_RETURN_STR(BTA_HF_CLIENT_SPK_EVT)
221 CASE_RETURN_STR(BTA_HF_CLIENT_MIC_EVT)
222 CASE_RETURN_STR(BTA_HF_CLIENT_DISABLE_EVT)
223 CASE_RETURN_STR(BTA_HF_CLIENT_IND_EVT)
224 CASE_RETURN_STR(BTA_HF_CLIENT_VOICE_REC_EVT)
225 CASE_RETURN_STR(BTA_HF_CLIENT_OPERATOR_NAME_EVT)
226 CASE_RETURN_STR(BTA_HF_CLIENT_CLIP_EVT)
227 CASE_RETURN_STR(BTA_HF_CLIENT_CCWA_EVT)
228 CASE_RETURN_STR(BTA_HF_CLIENT_AT_RESULT_EVT)
229 CASE_RETURN_STR(BTA_HF_CLIENT_CLCC_EVT)
230 CASE_RETURN_STR(BTA_HF_CLIENT_CNUM_EVT)
231 CASE_RETURN_STR(BTA_HF_CLIENT_BTRH_EVT)
232 CASE_RETURN_STR(BTA_HF_CLIENT_BSIR_EVT)
233 CASE_RETURN_STR(BTA_HF_CLIENT_BINP_EVT)
234 CASE_RETURN_STR(BTA_HF_CLIENT_RING_INDICATION)
235 default:
236 return "UNKNOWN MSG ID";
237 }
238 }
239
dump_hh_event(uint16_t event)240 const char* dump_hh_event(uint16_t event) {
241 switch (event) {
242 CASE_RETURN_STR(BTA_HH_ENABLE_EVT)
243 CASE_RETURN_STR(BTA_HH_DISABLE_EVT)
244 CASE_RETURN_STR(BTA_HH_OPEN_EVT)
245 CASE_RETURN_STR(BTA_HH_CLOSE_EVT)
246 CASE_RETURN_STR(BTA_HH_GET_DSCP_EVT)
247 CASE_RETURN_STR(BTA_HH_GET_PROTO_EVT)
248 CASE_RETURN_STR(BTA_HH_GET_RPT_EVT)
249 CASE_RETURN_STR(BTA_HH_GET_IDLE_EVT)
250 CASE_RETURN_STR(BTA_HH_SET_PROTO_EVT)
251 CASE_RETURN_STR(BTA_HH_SET_RPT_EVT)
252 CASE_RETURN_STR(BTA_HH_SET_IDLE_EVT)
253 CASE_RETURN_STR(BTA_HH_VC_UNPLUG_EVT)
254 CASE_RETURN_STR(BTA_HH_ADD_DEV_EVT)
255 CASE_RETURN_STR(BTA_HH_RMV_DEV_EVT)
256 CASE_RETURN_STR(BTA_HH_API_ERR_EVT)
257 default:
258 return "UNKNOWN MSG ID";
259 }
260 }
261
dump_hd_event(uint16_t event)262 const char* dump_hd_event(uint16_t event) {
263 switch (event) {
264 CASE_RETURN_STR(BTA_HD_ENABLE_EVT)
265 CASE_RETURN_STR(BTA_HD_DISABLE_EVT)
266 CASE_RETURN_STR(BTA_HD_REGISTER_APP_EVT)
267 CASE_RETURN_STR(BTA_HD_UNREGISTER_APP_EVT)
268 CASE_RETURN_STR(BTA_HD_OPEN_EVT)
269 CASE_RETURN_STR(BTA_HD_CLOSE_EVT)
270 CASE_RETURN_STR(BTA_HD_GET_REPORT_EVT)
271 CASE_RETURN_STR(BTA_HD_SET_REPORT_EVT)
272 CASE_RETURN_STR(BTA_HD_SET_PROTOCOL_EVT)
273 CASE_RETURN_STR(BTA_HD_INTR_DATA_EVT)
274 CASE_RETURN_STR(BTA_HD_VC_UNPLUG_EVT)
275 CASE_RETURN_STR(BTA_HD_CONN_STATE_EVT)
276 CASE_RETURN_STR(BTA_HD_API_ERR_EVT)
277 default:
278 return "UNKNOWN MSG ID";
279 }
280 }
281
dump_thread_evt(bt_cb_thread_evt evt)282 const char* dump_thread_evt(bt_cb_thread_evt evt) {
283 switch (evt) {
284 CASE_RETURN_STR(ASSOCIATE_JVM)
285 CASE_RETURN_STR(DISASSOCIATE_JVM)
286
287 default:
288 return "unknown thread evt";
289 }
290 }
291
dump_av_conn_state(uint16_t event)292 const char* dump_av_conn_state(uint16_t event) {
293 switch (event) {
294 CASE_RETURN_STR(BTAV_CONNECTION_STATE_DISCONNECTED)
295 CASE_RETURN_STR(BTAV_CONNECTION_STATE_CONNECTING)
296 CASE_RETURN_STR(BTAV_CONNECTION_STATE_CONNECTED)
297 CASE_RETURN_STR(BTAV_CONNECTION_STATE_DISCONNECTING)
298 default:
299 return "UNKNOWN MSG ID";
300 }
301 }
302
dump_av_audio_state(uint16_t event)303 const char* dump_av_audio_state(uint16_t event) {
304 switch (event) {
305 CASE_RETURN_STR(BTAV_AUDIO_STATE_REMOTE_SUSPEND)
306 CASE_RETURN_STR(BTAV_AUDIO_STATE_STOPPED)
307 CASE_RETURN_STR(BTAV_AUDIO_STATE_STARTED)
308 default:
309 return "UNKNOWN MSG ID";
310 }
311 }
312
dump_adapter_scan_mode(bt_scan_mode_t mode)313 const char* dump_adapter_scan_mode(bt_scan_mode_t mode) {
314 switch (mode) {
315 CASE_RETURN_STR(BT_SCAN_MODE_NONE)
316 CASE_RETURN_STR(BT_SCAN_MODE_CONNECTABLE)
317 CASE_RETURN_STR(BT_SCAN_MODE_CONNECTABLE_DISCOVERABLE)
318
319 default:
320 return "unknown scan mode";
321 }
322 }
323
dump_bt_status(bt_status_t status)324 const char* dump_bt_status(bt_status_t status) {
325 switch (status) {
326 CASE_RETURN_STR(BT_STATUS_SUCCESS)
327 CASE_RETURN_STR(BT_STATUS_FAIL)
328 CASE_RETURN_STR(BT_STATUS_NOT_READY)
329 CASE_RETURN_STR(BT_STATUS_NOMEM)
330 CASE_RETURN_STR(BT_STATUS_BUSY)
331 CASE_RETURN_STR(BT_STATUS_UNSUPPORTED)
332
333 default:
334 return "unknown scan mode";
335 }
336 }
337
dump_rc_event(uint8_t event)338 const char* dump_rc_event(uint8_t event) {
339 switch (event) {
340 CASE_RETURN_STR(BTA_AV_RC_OPEN_EVT)
341 CASE_RETURN_STR(BTA_AV_RC_CLOSE_EVT)
342 CASE_RETURN_STR(BTA_AV_RC_BROWSE_OPEN_EVT)
343 CASE_RETURN_STR(BTA_AV_RC_BROWSE_CLOSE_EVT)
344 CASE_RETURN_STR(BTA_AV_REMOTE_CMD_EVT)
345 CASE_RETURN_STR(BTA_AV_REMOTE_RSP_EVT)
346 CASE_RETURN_STR(BTA_AV_VENDOR_CMD_EVT)
347 CASE_RETURN_STR(BTA_AV_VENDOR_RSP_EVT)
348 CASE_RETURN_STR(BTA_AV_META_MSG_EVT)
349 CASE_RETURN_STR(BTA_AV_RC_FEAT_EVT)
350 CASE_RETURN_STR(BTA_AV_RC_PSM_EVT)
351 default:
352 return "UNKNOWN_EVENT";
353 }
354 }
355
dump_rc_notification_event_id(uint8_t event_id)356 const char* dump_rc_notification_event_id(uint8_t event_id) {
357 switch (event_id) {
358 CASE_RETURN_STR(AVRC_EVT_PLAY_STATUS_CHANGE)
359 CASE_RETURN_STR(AVRC_EVT_TRACK_CHANGE)
360 CASE_RETURN_STR(AVRC_EVT_TRACK_REACHED_END)
361 CASE_RETURN_STR(AVRC_EVT_TRACK_REACHED_START)
362 CASE_RETURN_STR(AVRC_EVT_PLAY_POS_CHANGED)
363 CASE_RETURN_STR(AVRC_EVT_BATTERY_STATUS_CHANGE)
364 CASE_RETURN_STR(AVRC_EVT_SYSTEM_STATUS_CHANGE)
365 CASE_RETURN_STR(AVRC_EVT_APP_SETTING_CHANGE)
366 CASE_RETURN_STR(AVRC_EVT_VOLUME_CHANGE)
367 CASE_RETURN_STR(AVRC_EVT_ADDR_PLAYER_CHANGE)
368 CASE_RETURN_STR(AVRC_EVT_AVAL_PLAYERS_CHANGE)
369 CASE_RETURN_STR(AVRC_EVT_NOW_PLAYING_CHANGE)
370 CASE_RETURN_STR(AVRC_EVT_UIDS_CHANGE)
371
372 default:
373 return "Unhandled Event ID";
374 }
375 }
376
dump_rc_pdu(uint8_t pdu)377 const char* dump_rc_pdu(uint8_t pdu) {
378 switch (pdu) {
379 CASE_RETURN_STR(AVRC_PDU_LIST_PLAYER_APP_ATTR)
380 CASE_RETURN_STR(AVRC_PDU_LIST_PLAYER_APP_VALUES)
381 CASE_RETURN_STR(AVRC_PDU_GET_CUR_PLAYER_APP_VALUE)
382 CASE_RETURN_STR(AVRC_PDU_SET_PLAYER_APP_VALUE)
383 CASE_RETURN_STR(AVRC_PDU_GET_PLAYER_APP_ATTR_TEXT)
384 CASE_RETURN_STR(AVRC_PDU_GET_PLAYER_APP_VALUE_TEXT)
385 CASE_RETURN_STR(AVRC_PDU_INFORM_DISPLAY_CHARSET)
386 CASE_RETURN_STR(AVRC_PDU_INFORM_BATTERY_STAT_OF_CT)
387 CASE_RETURN_STR(AVRC_PDU_GET_ELEMENT_ATTR)
388 CASE_RETURN_STR(AVRC_PDU_GET_PLAY_STATUS)
389 CASE_RETURN_STR(AVRC_PDU_REGISTER_NOTIFICATION)
390 CASE_RETURN_STR(AVRC_PDU_REQUEST_CONTINUATION_RSP)
391 CASE_RETURN_STR(AVRC_PDU_ABORT_CONTINUATION_RSP)
392 CASE_RETURN_STR(AVRC_PDU_SET_ABSOLUTE_VOLUME)
393 CASE_RETURN_STR(AVRC_PDU_SET_ADDRESSED_PLAYER)
394 CASE_RETURN_STR(AVRC_PDU_CHANGE_PATH)
395 CASE_RETURN_STR(AVRC_PDU_GET_CAPABILITIES)
396 CASE_RETURN_STR(AVRC_PDU_SET_BROWSED_PLAYER)
397 CASE_RETURN_STR(AVRC_PDU_GET_FOLDER_ITEMS)
398 CASE_RETURN_STR(AVRC_PDU_GET_ITEM_ATTRIBUTES)
399 CASE_RETURN_STR(AVRC_PDU_PLAY_ITEM)
400 CASE_RETURN_STR(AVRC_PDU_SEARCH)
401 CASE_RETURN_STR(AVRC_PDU_ADD_TO_NOW_PLAYING)
402 CASE_RETURN_STR(AVRC_PDU_GET_TOTAL_NUM_OF_ITEMS)
403 CASE_RETURN_STR(AVRC_PDU_GENERAL_REJECT)
404
405 default:
406 return "Unknown PDU";
407 }
408 }
409