• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_DI_DISC_CMPL_EVT)
120     CASE_RETURN_STR(BTA_DM_SEARCH_CANCEL_CMPL_EVT)
121 
122     default:
123       return "UNKNOWN MSG ID";
124   }
125 }
126 
dump_property_type(bt_property_type_t type)127 const char* dump_property_type(bt_property_type_t type) {
128   switch (type) {
129     CASE_RETURN_STR(BT_PROPERTY_BDNAME)
130     CASE_RETURN_STR(BT_PROPERTY_BDADDR)
131     CASE_RETURN_STR(BT_PROPERTY_UUIDS)
132     CASE_RETURN_STR(BT_PROPERTY_CLASS_OF_DEVICE)
133     CASE_RETURN_STR(BT_PROPERTY_TYPE_OF_DEVICE)
134     CASE_RETURN_STR(BT_PROPERTY_REMOTE_RSSI)
135     CASE_RETURN_STR(BT_PROPERTY_ADAPTER_DISCOVERY_TIMEOUT)
136     CASE_RETURN_STR(BT_PROPERTY_ADAPTER_BONDED_DEVICES)
137     CASE_RETURN_STR(BT_PROPERTY_ADAPTER_SCAN_MODE)
138     CASE_RETURN_STR(BT_PROPERTY_REMOTE_FRIENDLY_NAME)
139 
140     default:
141       return "UNKNOWN PROPERTY ID";
142   }
143 }
144 
dump_dm_event(uint16_t event)145 const char* dump_dm_event(uint16_t event) {
146   switch (event) {
147     CASE_RETURN_STR(BTA_DM_ENABLE_EVT)
148     CASE_RETURN_STR(BTA_DM_DISABLE_EVT)
149     CASE_RETURN_STR(BTA_DM_PIN_REQ_EVT)
150     CASE_RETURN_STR(BTA_DM_AUTH_CMPL_EVT)
151     CASE_RETURN_STR(BTA_DM_AUTHORIZE_EVT)
152     CASE_RETURN_STR(BTA_DM_LINK_UP_EVT)
153     CASE_RETURN_STR(BTA_DM_LINK_DOWN_EVT)
154     CASE_RETURN_STR(BTA_DM_SIG_STRENGTH_EVT)
155     CASE_RETURN_STR(BTA_DM_BUSY_LEVEL_EVT)
156     CASE_RETURN_STR(BTA_DM_BOND_CANCEL_CMPL_EVT)
157     CASE_RETURN_STR(BTA_DM_SP_CFM_REQ_EVT)
158     CASE_RETURN_STR(BTA_DM_SP_KEY_NOTIF_EVT)
159     CASE_RETURN_STR(BTA_DM_SP_RMT_OOB_EVT)
160     CASE_RETURN_STR(BTA_DM_SP_KEYPRESS_EVT)
161     CASE_RETURN_STR(BTA_DM_ROLE_CHG_EVT)
162     CASE_RETURN_STR(BTA_DM_BLE_KEY_EVT)
163     CASE_RETURN_STR(BTA_DM_BLE_SEC_REQ_EVT)
164     CASE_RETURN_STR(BTA_DM_BLE_PASSKEY_NOTIF_EVT)
165     CASE_RETURN_STR(BTA_DM_BLE_PASSKEY_REQ_EVT)
166     CASE_RETURN_STR(BTA_DM_BLE_OOB_REQ_EVT)
167     CASE_RETURN_STR(BTA_DM_BLE_LOCAL_IR_EVT)
168     CASE_RETURN_STR(BTA_DM_BLE_LOCAL_ER_EVT)
169     CASE_RETURN_STR(BTA_DM_BLE_AUTH_CMPL_EVT)
170     CASE_RETURN_STR(BTA_DM_DEV_UNPAIRED_EVT)
171     CASE_RETURN_STR(BTA_DM_HW_ERROR_EVT)
172     CASE_RETURN_STR(BTA_DM_ENER_INFO_READ)
173 
174     default:
175       return "UNKNOWN DM EVENT";
176   }
177 }
178 
dump_hf_event(uint16_t event)179 const char* dump_hf_event(uint16_t event) {
180   switch (event) {
181     CASE_RETURN_STR(BTA_AG_ENABLE_EVT)
182     CASE_RETURN_STR(BTA_AG_REGISTER_EVT)
183     CASE_RETURN_STR(BTA_AG_OPEN_EVT)
184     CASE_RETURN_STR(BTA_AG_CLOSE_EVT)
185     CASE_RETURN_STR(BTA_AG_CONN_EVT)
186     CASE_RETURN_STR(BTA_AG_AUDIO_OPEN_EVT)
187     CASE_RETURN_STR(BTA_AG_AUDIO_CLOSE_EVT)
188     CASE_RETURN_STR(BTA_AG_SPK_EVT)
189     CASE_RETURN_STR(BTA_AG_MIC_EVT)
190     CASE_RETURN_STR(BTA_AG_AT_CKPD_EVT)
191     CASE_RETURN_STR(BTA_AG_DISABLE_EVT)
192     CASE_RETURN_STR(BTA_AG_WBS_EVT)
193     CASE_RETURN_STR(BTA_AG_AT_A_EVT)
194     CASE_RETURN_STR(BTA_AG_AT_D_EVT)
195     CASE_RETURN_STR(BTA_AG_AT_CHLD_EVT)
196     CASE_RETURN_STR(BTA_AG_AT_CHUP_EVT)
197     CASE_RETURN_STR(BTA_AG_AT_CIND_EVT)
198     CASE_RETURN_STR(BTA_AG_AT_VTS_EVT)
199     CASE_RETURN_STR(BTA_AG_AT_BINP_EVT)
200     CASE_RETURN_STR(BTA_AG_AT_BLDN_EVT)
201     CASE_RETURN_STR(BTA_AG_AT_BVRA_EVT)
202     CASE_RETURN_STR(BTA_AG_AT_NREC_EVT)
203     CASE_RETURN_STR(BTA_AG_AT_CNUM_EVT)
204     CASE_RETURN_STR(BTA_AG_AT_BTRH_EVT)
205     CASE_RETURN_STR(BTA_AG_AT_CLCC_EVT)
206     CASE_RETURN_STR(BTA_AG_AT_COPS_EVT)
207     CASE_RETURN_STR(BTA_AG_AT_UNAT_EVT)
208     CASE_RETURN_STR(BTA_AG_AT_CBC_EVT)
209     CASE_RETURN_STR(BTA_AG_AT_BAC_EVT)
210     CASE_RETURN_STR(BTA_AG_AT_BCS_EVT)
211     CASE_RETURN_STR(BTA_AG_AT_BIND_EVT)
212     CASE_RETURN_STR(BTA_AG_AT_BIEV_EVT)
213     CASE_RETURN_STR(BTA_AG_AT_BIA_EVT)
214 
215     default:
216       return "UNKNOWN MSG ID";
217   }
218 }
219 
dump_hf_client_event(uint16_t event)220 const char* dump_hf_client_event(uint16_t event) {
221   switch (event) {
222     CASE_RETURN_STR(BTA_HF_CLIENT_ENABLE_EVT)
223     CASE_RETURN_STR(BTA_HF_CLIENT_REGISTER_EVT)
224     CASE_RETURN_STR(BTA_HF_CLIENT_OPEN_EVT)
225     CASE_RETURN_STR(BTA_HF_CLIENT_CLOSE_EVT)
226     CASE_RETURN_STR(BTA_HF_CLIENT_CONN_EVT)
227     CASE_RETURN_STR(BTA_HF_CLIENT_AUDIO_OPEN_EVT)
228     CASE_RETURN_STR(BTA_HF_CLIENT_AUDIO_MSBC_OPEN_EVT)
229     CASE_RETURN_STR(BTA_HF_CLIENT_AUDIO_CLOSE_EVT)
230     CASE_RETURN_STR(BTA_HF_CLIENT_SPK_EVT)
231     CASE_RETURN_STR(BTA_HF_CLIENT_MIC_EVT)
232     CASE_RETURN_STR(BTA_HF_CLIENT_DISABLE_EVT)
233     CASE_RETURN_STR(BTA_HF_CLIENT_IND_EVT)
234     CASE_RETURN_STR(BTA_HF_CLIENT_VOICE_REC_EVT)
235     CASE_RETURN_STR(BTA_HF_CLIENT_OPERATOR_NAME_EVT)
236     CASE_RETURN_STR(BTA_HF_CLIENT_CLIP_EVT)
237     CASE_RETURN_STR(BTA_HF_CLIENT_CCWA_EVT)
238     CASE_RETURN_STR(BTA_HF_CLIENT_AT_RESULT_EVT)
239     CASE_RETURN_STR(BTA_HF_CLIENT_CLCC_EVT)
240     CASE_RETURN_STR(BTA_HF_CLIENT_CNUM_EVT)
241     CASE_RETURN_STR(BTA_HF_CLIENT_BTRH_EVT)
242     CASE_RETURN_STR(BTA_HF_CLIENT_BSIR_EVT)
243     CASE_RETURN_STR(BTA_HF_CLIENT_BINP_EVT)
244     CASE_RETURN_STR(BTA_HF_CLIENT_RING_INDICATION)
245     default:
246       return "UNKNOWN MSG ID";
247   }
248 }
249 
dump_hh_event(uint16_t event)250 const char* dump_hh_event(uint16_t event) {
251   switch (event) {
252     CASE_RETURN_STR(BTA_HH_ENABLE_EVT)
253     CASE_RETURN_STR(BTA_HH_DISABLE_EVT)
254     CASE_RETURN_STR(BTA_HH_OPEN_EVT)
255     CASE_RETURN_STR(BTA_HH_CLOSE_EVT)
256     CASE_RETURN_STR(BTA_HH_GET_DSCP_EVT)
257     CASE_RETURN_STR(BTA_HH_GET_PROTO_EVT)
258     CASE_RETURN_STR(BTA_HH_GET_RPT_EVT)
259     CASE_RETURN_STR(BTA_HH_GET_IDLE_EVT)
260     CASE_RETURN_STR(BTA_HH_SET_PROTO_EVT)
261     CASE_RETURN_STR(BTA_HH_SET_RPT_EVT)
262     CASE_RETURN_STR(BTA_HH_SET_IDLE_EVT)
263     CASE_RETURN_STR(BTA_HH_VC_UNPLUG_EVT)
264     CASE_RETURN_STR(BTA_HH_ADD_DEV_EVT)
265     CASE_RETURN_STR(BTA_HH_RMV_DEV_EVT)
266     CASE_RETURN_STR(BTA_HH_API_ERR_EVT)
267     default:
268       return "UNKNOWN MSG ID";
269   }
270 }
271 
dump_hd_event(uint16_t event)272 const char* dump_hd_event(uint16_t event) {
273   switch (event) {
274     CASE_RETURN_STR(BTA_HD_ENABLE_EVT)
275     CASE_RETURN_STR(BTA_HD_DISABLE_EVT)
276     CASE_RETURN_STR(BTA_HD_REGISTER_APP_EVT)
277     CASE_RETURN_STR(BTA_HD_UNREGISTER_APP_EVT)
278     CASE_RETURN_STR(BTA_HD_OPEN_EVT)
279     CASE_RETURN_STR(BTA_HD_CLOSE_EVT)
280     CASE_RETURN_STR(BTA_HD_GET_REPORT_EVT)
281     CASE_RETURN_STR(BTA_HD_SET_REPORT_EVT)
282     CASE_RETURN_STR(BTA_HD_SET_PROTOCOL_EVT)
283     CASE_RETURN_STR(BTA_HD_INTR_DATA_EVT)
284     CASE_RETURN_STR(BTA_HD_VC_UNPLUG_EVT)
285     CASE_RETURN_STR(BTA_HD_CONN_STATE_EVT)
286     CASE_RETURN_STR(BTA_HD_API_ERR_EVT)
287     default:
288       return "UNKNOWN MSG ID";
289   }
290 }
291 
dump_thread_evt(bt_cb_thread_evt evt)292 const char* dump_thread_evt(bt_cb_thread_evt evt) {
293   switch (evt) {
294     CASE_RETURN_STR(ASSOCIATE_JVM)
295     CASE_RETURN_STR(DISASSOCIATE_JVM)
296 
297     default:
298       return "unknown thread evt";
299   }
300 }
301 
dump_av_conn_state(uint16_t event)302 const char* dump_av_conn_state(uint16_t event) {
303   switch (event) {
304     CASE_RETURN_STR(BTAV_CONNECTION_STATE_DISCONNECTED)
305     CASE_RETURN_STR(BTAV_CONNECTION_STATE_CONNECTING)
306     CASE_RETURN_STR(BTAV_CONNECTION_STATE_CONNECTED)
307     CASE_RETURN_STR(BTAV_CONNECTION_STATE_DISCONNECTING)
308     default:
309       return "UNKNOWN MSG ID";
310   }
311 }
312 
dump_av_audio_state(uint16_t event)313 const char* dump_av_audio_state(uint16_t event) {
314   switch (event) {
315     CASE_RETURN_STR(BTAV_AUDIO_STATE_REMOTE_SUSPEND)
316     CASE_RETURN_STR(BTAV_AUDIO_STATE_STOPPED)
317     CASE_RETURN_STR(BTAV_AUDIO_STATE_STARTED)
318     default:
319       return "UNKNOWN MSG ID";
320   }
321 }
322 
dump_adapter_scan_mode(bt_scan_mode_t mode)323 const char* dump_adapter_scan_mode(bt_scan_mode_t mode) {
324   switch (mode) {
325     CASE_RETURN_STR(BT_SCAN_MODE_NONE)
326     CASE_RETURN_STR(BT_SCAN_MODE_CONNECTABLE)
327     CASE_RETURN_STR(BT_SCAN_MODE_CONNECTABLE_DISCOVERABLE)
328 
329     default:
330       return "unknown scan mode";
331   }
332 }
333 
dump_bt_status(bt_status_t status)334 const char* dump_bt_status(bt_status_t status) {
335   switch (status) {
336     CASE_RETURN_STR(BT_STATUS_SUCCESS)
337     CASE_RETURN_STR(BT_STATUS_FAIL)
338     CASE_RETURN_STR(BT_STATUS_NOT_READY)
339     CASE_RETURN_STR(BT_STATUS_NOMEM)
340     CASE_RETURN_STR(BT_STATUS_BUSY)
341     CASE_RETURN_STR(BT_STATUS_UNSUPPORTED)
342 
343     default:
344       return "unknown scan mode";
345   }
346 }
347 
dump_rc_event(uint8_t event)348 const char* dump_rc_event(uint8_t event) {
349   switch (event) {
350     CASE_RETURN_STR(BTA_AV_RC_OPEN_EVT)
351     CASE_RETURN_STR(BTA_AV_RC_CLOSE_EVT)
352     CASE_RETURN_STR(BTA_AV_RC_BROWSE_OPEN_EVT)
353     CASE_RETURN_STR(BTA_AV_RC_BROWSE_CLOSE_EVT)
354     CASE_RETURN_STR(BTA_AV_REMOTE_CMD_EVT)
355     CASE_RETURN_STR(BTA_AV_REMOTE_RSP_EVT)
356     CASE_RETURN_STR(BTA_AV_VENDOR_CMD_EVT)
357     CASE_RETURN_STR(BTA_AV_VENDOR_RSP_EVT)
358     CASE_RETURN_STR(BTA_AV_META_MSG_EVT)
359     CASE_RETURN_STR(BTA_AV_RC_FEAT_EVT)
360     default:
361       return "UNKNOWN_EVENT";
362   }
363 }
364 
dump_rc_notification_event_id(uint8_t event_id)365 const char* dump_rc_notification_event_id(uint8_t event_id) {
366   switch (event_id) {
367     CASE_RETURN_STR(AVRC_EVT_PLAY_STATUS_CHANGE)
368     CASE_RETURN_STR(AVRC_EVT_TRACK_CHANGE)
369     CASE_RETURN_STR(AVRC_EVT_TRACK_REACHED_END)
370     CASE_RETURN_STR(AVRC_EVT_TRACK_REACHED_START)
371     CASE_RETURN_STR(AVRC_EVT_PLAY_POS_CHANGED)
372     CASE_RETURN_STR(AVRC_EVT_BATTERY_STATUS_CHANGE)
373     CASE_RETURN_STR(AVRC_EVT_SYSTEM_STATUS_CHANGE)
374     CASE_RETURN_STR(AVRC_EVT_APP_SETTING_CHANGE)
375     CASE_RETURN_STR(AVRC_EVT_VOLUME_CHANGE)
376     CASE_RETURN_STR(AVRC_EVT_ADDR_PLAYER_CHANGE)
377     CASE_RETURN_STR(AVRC_EVT_AVAL_PLAYERS_CHANGE)
378     CASE_RETURN_STR(AVRC_EVT_NOW_PLAYING_CHANGE)
379     CASE_RETURN_STR(AVRC_EVT_UIDS_CHANGE)
380 
381     default:
382       return "Unhandled Event ID";
383   }
384 }
385 
dump_rc_pdu(uint8_t pdu)386 const char* dump_rc_pdu(uint8_t pdu) {
387   switch (pdu) {
388     CASE_RETURN_STR(AVRC_PDU_LIST_PLAYER_APP_ATTR)
389     CASE_RETURN_STR(AVRC_PDU_LIST_PLAYER_APP_VALUES)
390     CASE_RETURN_STR(AVRC_PDU_GET_CUR_PLAYER_APP_VALUE)
391     CASE_RETURN_STR(AVRC_PDU_SET_PLAYER_APP_VALUE)
392     CASE_RETURN_STR(AVRC_PDU_GET_PLAYER_APP_ATTR_TEXT)
393     CASE_RETURN_STR(AVRC_PDU_GET_PLAYER_APP_VALUE_TEXT)
394     CASE_RETURN_STR(AVRC_PDU_INFORM_DISPLAY_CHARSET)
395     CASE_RETURN_STR(AVRC_PDU_INFORM_BATTERY_STAT_OF_CT)
396     CASE_RETURN_STR(AVRC_PDU_GET_ELEMENT_ATTR)
397     CASE_RETURN_STR(AVRC_PDU_GET_PLAY_STATUS)
398     CASE_RETURN_STR(AVRC_PDU_REGISTER_NOTIFICATION)
399     CASE_RETURN_STR(AVRC_PDU_REQUEST_CONTINUATION_RSP)
400     CASE_RETURN_STR(AVRC_PDU_ABORT_CONTINUATION_RSP)
401     CASE_RETURN_STR(AVRC_PDU_SET_ABSOLUTE_VOLUME)
402     CASE_RETURN_STR(AVRC_PDU_SET_ADDRESSED_PLAYER)
403     CASE_RETURN_STR(AVRC_PDU_CHANGE_PATH)
404     CASE_RETURN_STR(AVRC_PDU_GET_CAPABILITIES)
405     CASE_RETURN_STR(AVRC_PDU_SET_BROWSED_PLAYER)
406     CASE_RETURN_STR(AVRC_PDU_GET_FOLDER_ITEMS)
407     CASE_RETURN_STR(AVRC_PDU_GET_ITEM_ATTRIBUTES)
408     CASE_RETURN_STR(AVRC_PDU_PLAY_ITEM)
409     CASE_RETURN_STR(AVRC_PDU_SEARCH)
410     CASE_RETURN_STR(AVRC_PDU_ADD_TO_NOW_PLAYING)
411     CASE_RETURN_STR(AVRC_PDU_GET_TOTAL_NUM_OF_ITEMS)
412     CASE_RETURN_STR(AVRC_PDU_GENERAL_REJECT)
413 
414     default:
415       return "Unknown PDU";
416   }
417 }
418