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 <ctype.h>
34 #include <hardware/bt_av.h>
35 #include <netinet/in.h>
36 #include <stdlib.h>
37
38 #include <string>
39
40 #include "avrc_defs.h"
41 #include "bta_ag_api.h"
42 #include "bta_api.h"
43 #include "bta_av_api.h"
44 #include "bta_hd_api.h"
45 #include "bta_hf_client_api.h"
46 #include "bta_hh_api.h"
47 #include "btif_common.h"
48 #include "btif_dm.h"
49 #include "include/macros.h"
50
51 /*******************************************************************************
52 * Constants & Macros
53 ******************************************************************************/
54 #define ISDIGIT(a) (((a) >= '0') && ((a) <= '9'))
55
56 /*****************************************************************************
57 * Logging helper functions
58 ****************************************************************************/
59
devclass2uint(const DEV_CLASS dev_class)60 uint32_t devclass2uint(const DEV_CLASS dev_class) {
61 uint32_t cod = 0;
62
63 if (dev_class != kDevClassEmpty) {
64 /* if COD is 0, irrespective of the device type set it to Unclassified
65 * device */
66 cod = (dev_class[2]) | (dev_class[1] << 8) | (dev_class[0] << 16);
67 }
68 return cod;
69 }
uint2devclass(uint32_t cod)70 DEV_CLASS uint2devclass(uint32_t cod) {
71 DEV_CLASS dev_class;
72 dev_class[2] = (uint8_t)cod;
73 dev_class[1] = (uint8_t)(cod >> 8);
74 dev_class[0] = (uint8_t)(cod >> 16);
75 return dev_class;
76 }
77
78 /*****************************************************************************
79 * Function ascii_2_hex
80 *
81 * Description This function converts an ASCII string into HEX
82 *
83 * Returns the number of hex bytes filled.
84 */
ascii_2_hex(const char * p_ascii,int len,uint8_t * p_hex)85 int ascii_2_hex(const char* p_ascii, int len, uint8_t* p_hex) {
86 int x;
87 uint8_t c;
88
89 for (x = 0; (x < len) && (*p_ascii); x++) {
90 if (ISDIGIT(*p_ascii))
91 c = (*p_ascii - '0') << 4;
92 else
93 c = (toupper(*p_ascii) - 'A' + 10) << 4;
94
95 p_ascii++;
96 if (*p_ascii) {
97 if (ISDIGIT(*p_ascii))
98 c |= (*p_ascii - '0');
99 else
100 c |= (toupper(*p_ascii) - 'A' + 10);
101
102 p_ascii++;
103 }
104 *p_hex++ = c;
105 }
106
107 return (x);
108 }
109
dump_dm_search_event(uint16_t event)110 std::string dump_dm_search_event(uint16_t event) {
111 switch (event) {
112 CASE_RETURN_STRING(BTA_DM_INQ_RES_EVT);
113 CASE_RETURN_STRING(BTA_DM_INQ_CMPL_EVT);
114 CASE_RETURN_STRING(BTA_DM_DISC_CMPL_EVT);
115 CASE_RETURN_STRING(BTA_DM_SEARCH_CANCEL_CMPL_EVT);
116 CASE_RETURN_STRING(BTA_DM_NAME_READ_EVT);
117 }
118 RETURN_UNKNOWN_TYPE_STRING(dm_search_event, event);
119 }
120
dump_property_type(bt_property_type_t type)121 std::string dump_property_type(bt_property_type_t type) {
122 switch (type) {
123 CASE_RETURN_STRING(BT_PROPERTY_BDNAME);
124 CASE_RETURN_STRING(BT_PROPERTY_BDADDR);
125 CASE_RETURN_STRING(BT_PROPERTY_UUIDS);
126 CASE_RETURN_STRING(BT_PROPERTY_CLASS_OF_DEVICE);
127 CASE_RETURN_STRING(BT_PROPERTY_TYPE_OF_DEVICE);
128 CASE_RETURN_STRING(BT_PROPERTY_REMOTE_RSSI);
129 CASE_RETURN_STRING(BT_PROPERTY_ADAPTER_DISCOVERABLE_TIMEOUT);
130 CASE_RETURN_STRING(BT_PROPERTY_ADAPTER_BONDED_DEVICES);
131 CASE_RETURN_STRING(BT_PROPERTY_ADAPTER_SCAN_MODE);
132 CASE_RETURN_STRING(BT_PROPERTY_REMOTE_FRIENDLY_NAME);
133 default:
134 RETURN_UNKNOWN_TYPE_STRING(bt_property_type_t, type);
135 }
136 }
137
dump_dm_event(uint16_t event)138 std::string dump_dm_event(uint16_t event) {
139 switch (event) {
140 CASE_RETURN_STRING(BTA_DM_PIN_REQ_EVT);
141 CASE_RETURN_STRING(BTA_DM_AUTH_CMPL_EVT);
142 CASE_RETURN_STRING(BTA_DM_LINK_UP_EVT);
143 CASE_RETURN_STRING(BTA_DM_LINK_DOWN_EVT);
144 CASE_RETURN_STRING(BTA_DM_BOND_CANCEL_CMPL_EVT);
145 CASE_RETURN_STRING(BTA_DM_SP_CFM_REQ_EVT);
146 CASE_RETURN_STRING(BTA_DM_SP_KEY_NOTIF_EVT);
147 CASE_RETURN_STRING(BTA_DM_BLE_KEY_EVT);
148 CASE_RETURN_STRING(BTA_DM_BLE_SEC_REQ_EVT);
149 CASE_RETURN_STRING(BTA_DM_BLE_PASSKEY_NOTIF_EVT);
150 CASE_RETURN_STRING(BTA_DM_BLE_PASSKEY_REQ_EVT);
151 CASE_RETURN_STRING(BTA_DM_BLE_OOB_REQ_EVT);
152 CASE_RETURN_STRING(BTA_DM_BLE_SC_OOB_REQ_EVT);
153 CASE_RETURN_STRING(BTA_DM_BLE_LOCAL_IR_EVT);
154 CASE_RETURN_STRING(BTA_DM_BLE_LOCAL_ER_EVT);
155 CASE_RETURN_STRING(BTA_DM_BLE_AUTH_CMPL_EVT);
156 CASE_RETURN_STRING(BTA_DM_DEV_UNPAIRED_EVT);
157 CASE_RETURN_STRING(BTA_DM_ENER_INFO_READ);
158 CASE_RETURN_STRING(BTA_DM_SIRK_VERIFICATION_REQ_EVT);
159 CASE_RETURN_STRING(BTA_DM_KEY_MISSING_EVT);
160 }
161 RETURN_UNKNOWN_TYPE_STRING(dm_event, event);
162 }
163
dump_hf_event(uint16_t event)164 std::string dump_hf_event(uint16_t event) {
165 switch (event) {
166 CASE_RETURN_STRING(BTA_AG_ENABLE_EVT);
167 CASE_RETURN_STRING(BTA_AG_REGISTER_EVT);
168 CASE_RETURN_STRING(BTA_AG_OPEN_EVT);
169 CASE_RETURN_STRING(BTA_AG_CLOSE_EVT);
170 CASE_RETURN_STRING(BTA_AG_CONN_EVT);
171 CASE_RETURN_STRING(BTA_AG_AUDIO_OPEN_EVT);
172 CASE_RETURN_STRING(BTA_AG_AUDIO_CLOSE_EVT);
173 CASE_RETURN_STRING(BTA_AG_SPK_EVT);
174 CASE_RETURN_STRING(BTA_AG_MIC_EVT);
175 CASE_RETURN_STRING(BTA_AG_AT_CKPD_EVT);
176 CASE_RETURN_STRING(BTA_AG_DISABLE_EVT);
177 CASE_RETURN_STRING(BTA_AG_CODEC_EVT);
178 CASE_RETURN_STRING(BTA_AG_AT_A_EVT);
179 CASE_RETURN_STRING(BTA_AG_AT_D_EVT);
180 CASE_RETURN_STRING(BTA_AG_AT_CHLD_EVT);
181 CASE_RETURN_STRING(BTA_AG_AT_CHUP_EVT);
182 CASE_RETURN_STRING(BTA_AG_AT_CIND_EVT);
183 CASE_RETURN_STRING(BTA_AG_AT_VTS_EVT);
184 CASE_RETURN_STRING(BTA_AG_AT_BINP_EVT);
185 CASE_RETURN_STRING(BTA_AG_AT_BLDN_EVT);
186 CASE_RETURN_STRING(BTA_AG_AT_BVRA_EVT);
187 CASE_RETURN_STRING(BTA_AG_AT_NREC_EVT);
188 CASE_RETURN_STRING(BTA_AG_AT_CNUM_EVT);
189 CASE_RETURN_STRING(BTA_AG_AT_BTRH_EVT);
190 CASE_RETURN_STRING(BTA_AG_AT_CLCC_EVT);
191 CASE_RETURN_STRING(BTA_AG_AT_COPS_EVT);
192 CASE_RETURN_STRING(BTA_AG_AT_UNAT_EVT);
193 CASE_RETURN_STRING(BTA_AG_AT_CBC_EVT);
194 CASE_RETURN_STRING(BTA_AG_AT_BAC_EVT);
195 CASE_RETURN_STRING(BTA_AG_AT_BCS_EVT);
196 CASE_RETURN_STRING(BTA_AG_AT_BIND_EVT);
197 CASE_RETURN_STRING(BTA_AG_AT_BIEV_EVT);
198 CASE_RETURN_STRING(BTA_AG_AT_BIA_EVT);
199 }
200 RETURN_UNKNOWN_TYPE_STRING(hf_event, event);
201 }
202
dump_hf_client_event(uint16_t event)203 std::string dump_hf_client_event(uint16_t event) {
204 switch (event) {
205 CASE_RETURN_STRING(BTA_HF_CLIENT_ENABLE_EVT);
206 CASE_RETURN_STRING(BTA_HF_CLIENT_REGISTER_EVT);
207 CASE_RETURN_STRING(BTA_HF_CLIENT_OPEN_EVT);
208 CASE_RETURN_STRING(BTA_HF_CLIENT_CLOSE_EVT);
209 CASE_RETURN_STRING(BTA_HF_CLIENT_CONN_EVT);
210 CASE_RETURN_STRING(BTA_HF_CLIENT_AUDIO_OPEN_EVT);
211 CASE_RETURN_STRING(BTA_HF_CLIENT_AUDIO_MSBC_OPEN_EVT);
212 CASE_RETURN_STRING(BTA_HF_CLIENT_AUDIO_LC3_OPEN_EVT);
213 CASE_RETURN_STRING(BTA_HF_CLIENT_AUDIO_CLOSE_EVT);
214 CASE_RETURN_STRING(BTA_HF_CLIENT_SPK_EVT);
215 CASE_RETURN_STRING(BTA_HF_CLIENT_MIC_EVT);
216 CASE_RETURN_STRING(BTA_HF_CLIENT_DISABLE_EVT);
217 CASE_RETURN_STRING(BTA_HF_CLIENT_IND_EVT);
218 CASE_RETURN_STRING(BTA_HF_CLIENT_VOICE_REC_EVT);
219 CASE_RETURN_STRING(BTA_HF_CLIENT_OPERATOR_NAME_EVT);
220 CASE_RETURN_STRING(BTA_HF_CLIENT_CLIP_EVT);
221 CASE_RETURN_STRING(BTA_HF_CLIENT_CCWA_EVT);
222 CASE_RETURN_STRING(BTA_HF_CLIENT_AT_RESULT_EVT);
223 CASE_RETURN_STRING(BTA_HF_CLIENT_CLCC_EVT);
224 CASE_RETURN_STRING(BTA_HF_CLIENT_CNUM_EVT);
225 CASE_RETURN_STRING(BTA_HF_CLIENT_BTRH_EVT);
226 CASE_RETURN_STRING(BTA_HF_CLIENT_BSIR_EVT);
227 CASE_RETURN_STRING(BTA_HF_CLIENT_BINP_EVT);
228 CASE_RETURN_STRING(BTA_HF_CLIENT_RING_INDICATION);
229 }
230 RETURN_UNKNOWN_TYPE_STRING(hf_client_event, event);
231 }
232
dump_hd_event(uint16_t event)233 std::string dump_hd_event(uint16_t event) {
234 switch (event) {
235 CASE_RETURN_STRING(BTA_HD_ENABLE_EVT);
236 CASE_RETURN_STRING(BTA_HD_DISABLE_EVT);
237 CASE_RETURN_STRING(BTA_HD_REGISTER_APP_EVT);
238 CASE_RETURN_STRING(BTA_HD_UNREGISTER_APP_EVT);
239 CASE_RETURN_STRING(BTA_HD_OPEN_EVT);
240 CASE_RETURN_STRING(BTA_HD_CLOSE_EVT);
241 CASE_RETURN_STRING(BTA_HD_GET_REPORT_EVT);
242 CASE_RETURN_STRING(BTA_HD_SET_REPORT_EVT);
243 CASE_RETURN_STRING(BTA_HD_SET_PROTOCOL_EVT);
244 CASE_RETURN_STRING(BTA_HD_INTR_DATA_EVT);
245 CASE_RETURN_STRING(BTA_HD_VC_UNPLUG_EVT);
246 CASE_RETURN_STRING(BTA_HD_CONN_STATE_EVT);
247 CASE_RETURN_STRING(BTA_HD_API_ERR_EVT);
248 }
249 RETURN_UNKNOWN_TYPE_STRING(hd_event, event);
250 }
251
dump_thread_evt(bt_cb_thread_evt evt)252 std::string dump_thread_evt(bt_cb_thread_evt evt) {
253 switch (evt) {
254 CASE_RETURN_STRING(ASSOCIATE_JVM);
255 CASE_RETURN_STRING(DISASSOCIATE_JVM);
256 }
257 RETURN_UNKNOWN_TYPE_STRING(bt_cb_thread_evt, evt);
258 }
259
dump_av_conn_state(uint16_t event)260 std::string dump_av_conn_state(uint16_t event) {
261 switch (event) {
262 CASE_RETURN_STRING(BTAV_CONNECTION_STATE_DISCONNECTED);
263 CASE_RETURN_STRING(BTAV_CONNECTION_STATE_CONNECTING);
264 CASE_RETURN_STRING(BTAV_CONNECTION_STATE_CONNECTED);
265 CASE_RETURN_STRING(BTAV_CONNECTION_STATE_DISCONNECTING);
266 }
267 RETURN_UNKNOWN_TYPE_STRING(av_conn_state, event);
268 }
269
dump_av_audio_state(uint16_t event)270 std::string dump_av_audio_state(uint16_t event) {
271 switch (event) {
272 CASE_RETURN_STRING(BTAV_AUDIO_STATE_REMOTE_SUSPEND);
273 CASE_RETURN_STRING(BTAV_AUDIO_STATE_STOPPED);
274 CASE_RETURN_STRING(BTAV_AUDIO_STATE_STARTED);
275 }
276 RETURN_UNKNOWN_TYPE_STRING(av_audio_state, event);
277 }
278
dump_adapter_scan_mode(bt_scan_mode_t mode)279 std::string dump_adapter_scan_mode(bt_scan_mode_t mode) {
280 switch (mode) {
281 CASE_RETURN_STRING(BT_SCAN_MODE_NONE);
282 CASE_RETURN_STRING(BT_SCAN_MODE_CONNECTABLE);
283 CASE_RETURN_STRING(BT_SCAN_MODE_CONNECTABLE_DISCOVERABLE);
284 default:
285 RETURN_UNKNOWN_TYPE_STRING(bt_scan_mode_t, mode);
286 }
287 }
288
dump_bt_status(bt_status_t status)289 std::string dump_bt_status(bt_status_t status) {
290 switch (status) {
291 CASE_RETURN_STRING(BT_STATUS_SUCCESS);
292 CASE_RETURN_STRING(BT_STATUS_FAIL);
293 CASE_RETURN_STRING(BT_STATUS_NOT_READY);
294 CASE_RETURN_STRING(BT_STATUS_NOMEM);
295 CASE_RETURN_STRING(BT_STATUS_BUSY);
296 CASE_RETURN_STRING(BT_STATUS_UNSUPPORTED);
297 default:
298 RETURN_UNKNOWN_TYPE_STRING(bt_status_t, status);
299 }
300 }
301
dump_rc_opcode(uint8_t opcode)302 std::string dump_rc_opcode(uint8_t opcode) {
303 switch (opcode) {
304 CASE_RETURN_STRING(AVRC_OP_UNIT_INFO);
305 CASE_RETURN_STRING(AVRC_OP_SUB_INFO);
306 CASE_RETURN_STRING(AVRC_OP_VENDOR);
307 CASE_RETURN_STRING(AVRC_OP_PASS_THRU);
308 CASE_RETURN_STRING(AVRC_OP_BROWSE);
309 CASE_RETURN_STRING(AVRC_OP_INVALID);
310 }
311 RETURN_UNKNOWN_TYPE_STRING(rc_opcode, opcode);
312 }
313
dump_rc_event(uint8_t event)314 std::string dump_rc_event(uint8_t event) {
315 switch (event) {
316 CASE_RETURN_STRING(BTA_AV_RC_OPEN_EVT);
317 CASE_RETURN_STRING(BTA_AV_RC_CLOSE_EVT);
318 CASE_RETURN_STRING(BTA_AV_RC_BROWSE_OPEN_EVT);
319 CASE_RETURN_STRING(BTA_AV_RC_BROWSE_CLOSE_EVT);
320 CASE_RETURN_STRING(BTA_AV_REMOTE_CMD_EVT);
321 CASE_RETURN_STRING(BTA_AV_REMOTE_RSP_EVT);
322 CASE_RETURN_STRING(BTA_AV_VENDOR_CMD_EVT);
323 CASE_RETURN_STRING(BTA_AV_VENDOR_RSP_EVT);
324 CASE_RETURN_STRING(BTA_AV_META_MSG_EVT);
325 CASE_RETURN_STRING(BTA_AV_RC_FEAT_EVT);
326 CASE_RETURN_STRING(BTA_AV_RC_PSM_EVT);
327 }
328 RETURN_UNKNOWN_TYPE_STRING(rc_event, event);
329 }
330
dump_rc_notification_event_id(uint8_t event_id)331 std::string dump_rc_notification_event_id(uint8_t event_id) {
332 switch (event_id) {
333 CASE_RETURN_STRING(AVRC_EVT_INVALID);
334 CASE_RETURN_STRING(AVRC_EVT_PLAY_STATUS_CHANGE);
335 CASE_RETURN_STRING(AVRC_EVT_TRACK_CHANGE);
336 CASE_RETURN_STRING(AVRC_EVT_TRACK_REACHED_END);
337 CASE_RETURN_STRING(AVRC_EVT_TRACK_REACHED_START);
338 CASE_RETURN_STRING(AVRC_EVT_PLAY_POS_CHANGED);
339 CASE_RETURN_STRING(AVRC_EVT_BATTERY_STATUS_CHANGE);
340 CASE_RETURN_STRING(AVRC_EVT_SYSTEM_STATUS_CHANGE);
341 CASE_RETURN_STRING(AVRC_EVT_APP_SETTING_CHANGE);
342 CASE_RETURN_STRING(AVRC_EVT_VOLUME_CHANGE);
343 CASE_RETURN_STRING(AVRC_EVT_ADDR_PLAYER_CHANGE);
344 CASE_RETURN_STRING(AVRC_EVT_AVAL_PLAYERS_CHANGE);
345 CASE_RETURN_STRING(AVRC_EVT_NOW_PLAYING_CHANGE);
346 CASE_RETURN_STRING(AVRC_EVT_UIDS_CHANGE);
347 }
348 RETURN_UNKNOWN_TYPE_STRING(rc_notification_event_id, event_id);
349 }
350
dump_rc_pdu(uint8_t pdu)351 std::string dump_rc_pdu(uint8_t pdu) {
352 switch (pdu) {
353 CASE_RETURN_STRING(AVRC_PDU_LIST_PLAYER_APP_ATTR);
354 CASE_RETURN_STRING(AVRC_PDU_LIST_PLAYER_APP_VALUES);
355 CASE_RETURN_STRING(AVRC_PDU_GET_CUR_PLAYER_APP_VALUE);
356 CASE_RETURN_STRING(AVRC_PDU_SET_PLAYER_APP_VALUE);
357 CASE_RETURN_STRING(AVRC_PDU_GET_PLAYER_APP_ATTR_TEXT);
358 CASE_RETURN_STRING(AVRC_PDU_GET_PLAYER_APP_VALUE_TEXT);
359 CASE_RETURN_STRING(AVRC_PDU_INFORM_DISPLAY_CHARSET);
360 CASE_RETURN_STRING(AVRC_PDU_INFORM_BATTERY_STAT_OF_CT);
361 CASE_RETURN_STRING(AVRC_PDU_GET_ELEMENT_ATTR);
362 CASE_RETURN_STRING(AVRC_PDU_GET_PLAY_STATUS);
363 CASE_RETURN_STRING(AVRC_PDU_REGISTER_NOTIFICATION);
364 CASE_RETURN_STRING(AVRC_PDU_REQUEST_CONTINUATION_RSP);
365 CASE_RETURN_STRING(AVRC_PDU_ABORT_CONTINUATION_RSP);
366 CASE_RETURN_STRING(AVRC_PDU_SET_ABSOLUTE_VOLUME);
367 CASE_RETURN_STRING(AVRC_PDU_SET_ADDRESSED_PLAYER);
368 CASE_RETURN_STRING(AVRC_PDU_CHANGE_PATH);
369 CASE_RETURN_STRING(AVRC_PDU_GET_CAPABILITIES);
370 CASE_RETURN_STRING(AVRC_PDU_SET_BROWSED_PLAYER);
371 CASE_RETURN_STRING(AVRC_PDU_GET_FOLDER_ITEMS);
372 CASE_RETURN_STRING(AVRC_PDU_GET_ITEM_ATTRIBUTES);
373 CASE_RETURN_STRING(AVRC_PDU_PLAY_ITEM);
374 CASE_RETURN_STRING(AVRC_PDU_SEARCH);
375 CASE_RETURN_STRING(AVRC_PDU_ADD_TO_NOW_PLAYING);
376 CASE_RETURN_STRING(AVRC_PDU_GET_TOTAL_NUM_OF_ITEMS);
377 CASE_RETURN_STRING(AVRC_PDU_GENERAL_REJECT);
378 }
379 RETURN_UNKNOWN_TYPE_STRING(rc_pdu, pdu);
380 }
381