btif_util.c revision 247c68fe6eee3660cbdcf4509195fe735ae25573
1/******************************************************************************
2 *
3 *  Copyright (C) 2009-2012 Broadcom Corporation
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/************************************************************************************
20 *
21 *  Filename:      btif_util.c
22 *
23 *  Description:   Miscellaneous helper functions
24 *
25 *
26 ***********************************************************************************/
27
28#include <hardware/bluetooth.h>
29#include <hardware/bt_hf.h>
30#include <hardware/bt_av.h>
31#include <netinet/in.h>
32#include <stdio.h>
33#include <stdlib.h>
34#include <string.h>
35#include <ctype.h>
36
37
38#define LOG_TAG "BTIF_UTIL"
39#include "btif_common.h"
40#include "bta_api.h"
41#include "gki.h"
42#include "btu.h"
43#include "bte.h"
44#include "bd.h"
45#include "btif_dm.h"
46#include "btif_util.h"
47#include "bta_ag_api.h"
48#include "bta_av_api.h"
49#include "bta_hh_api.h"
50#include "avrc_defs.h"
51
52
53
54/************************************************************************************
55**  Constants & Macros
56************************************************************************************/
57#define ISDIGIT(a)  ((a>='0') && (a<='9'))
58#define ISXDIGIT(a) (((a>='0') && (a<='9'))||((a>='A') && (a<='F'))||((a>='a') && (a<='f')))
59
60/************************************************************************************
61**  Local type definitions
62************************************************************************************/
63
64/************************************************************************************
65**  Static variables
66************************************************************************************/
67
68/************************************************************************************
69**  Static functions
70************************************************************************************/
71
72/************************************************************************************
73**  Externs
74************************************************************************************/
75
76/************************************************************************************
77**  Functions
78************************************************************************************/
79
80/*****************************************************************************
81**   Logging helper functions
82*****************************************************************************/
83
84int str2bd(char *str, bt_bdaddr_t *addr)
85{
86    int32_t i = 0;
87    for (i = 0; i < 6; i++) {
88       addr->address[i] = (uint8_t) strtoul(str, (char **)&str, 16);
89       str++;
90    }
91    return 0;
92}
93
94char *bd2str(const bt_bdaddr_t *bdaddr, bdstr_t *bdstr)
95{
96    char *addr = (char *) bdaddr->address;
97
98    sprintf((char*)bdstr, "%02x:%02x:%02x:%02x:%02x:%02x",
99                       (int)addr[0],(int)addr[1],(int)addr[2],
100                       (int)addr[3],(int)addr[4],(int)addr[5]);
101    return (char *)bdstr;
102}
103
104UINT32 devclass2uint(DEV_CLASS dev_class)
105{
106    UINT32 cod = 0;
107
108    if(dev_class != NULL)
109    {
110        /* if COD is 0, irrespective of the device type set it to Unclassified device */
111        cod = (dev_class[2]) | (dev_class[1] << 8) | (dev_class[0] << 16);
112    }
113    return cod;
114}
115void uint2devclass(UINT32 cod, DEV_CLASS dev_class)
116{
117    dev_class[2] = (UINT8)cod;
118    dev_class[1] = (UINT8)(cod >> 8);
119    dev_class[0] = (UINT8)(cod >> 16);
120}
121
122static const UINT8  sdp_base_uuid[] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10, 0x00,
123                                       0x80, 0x00, 0x00, 0x80, 0x5F, 0x9B, 0x34, 0xFB};
124
125void uuid16_to_uuid128(uint16_t uuid16, bt_uuid_t* uuid128)
126{
127    uint16_t uuid16_bo;
128    memset(uuid128, 0, sizeof(bt_uuid_t));
129
130    memcpy(uuid128->uu, sdp_base_uuid, MAX_UUID_SIZE);
131    uuid16_bo = ntohs(uuid16);
132    memcpy(uuid128->uu + 2, &uuid16_bo, sizeof(uint16_t));
133}
134
135void string_to_uuid(char *str, bt_uuid_t *p_uuid)
136{
137    uint32_t uuid0, uuid4;
138    uint16_t uuid1, uuid2, uuid3, uuid5;
139
140    sscanf(str, "%08x-%04hx-%04hx-%04hx-%08x%04hx",
141                &uuid0, &uuid1, &uuid2, &uuid3, &uuid4, &uuid5);
142
143    uuid0 = htonl(uuid0);
144    uuid1 = htons(uuid1);
145    uuid2 = htons(uuid2);
146    uuid3 = htons(uuid3);
147    uuid4 = htonl(uuid4);
148    uuid5 = htons(uuid5);
149
150    memcpy(&(p_uuid->uu[0]), &uuid0, 4);
151    memcpy(&(p_uuid->uu[4]), &uuid1, 2);
152    memcpy(&(p_uuid->uu[6]), &uuid2, 2);
153    memcpy(&(p_uuid->uu[8]), &uuid3, 2);
154    memcpy(&(p_uuid->uu[10]), &uuid4, 4);
155    memcpy(&(p_uuid->uu[14]), &uuid5, 2);
156
157    return;
158
159}
160
161void uuid_to_string(bt_uuid_t *p_uuid, char *str)
162{
163    uint32_t uuid0, uuid4;
164    uint16_t uuid1, uuid2, uuid3, uuid5;
165
166    memcpy(&uuid0, &(p_uuid->uu[0]), 4);
167    memcpy(&uuid1, &(p_uuid->uu[4]), 2);
168    memcpy(&uuid2, &(p_uuid->uu[6]), 2);
169    memcpy(&uuid3, &(p_uuid->uu[8]), 2);
170    memcpy(&uuid4, &(p_uuid->uu[10]), 4);
171    memcpy(&uuid5, &(p_uuid->uu[14]), 2);
172
173    sprintf((char *)str, "%.8x-%.4x-%.4x-%.4x-%.8x%.4x",
174            ntohl(uuid0), ntohs(uuid1),
175            ntohs(uuid2), ntohs(uuid3),
176            ntohl(uuid4), ntohs(uuid5));
177    return;
178}
179
180/*****************************************************************************
181**  Function        ascii_2_hex
182**
183**  Description     This function converts an ASCII string into HEX
184**
185**  Returns         the number of hex bytes filled.
186*/
187int ascii_2_hex (char *p_ascii, int len, UINT8 *p_hex)
188{
189    int     x;
190    UINT8   c;
191
192    for (x = 0; (x < len) && (*p_ascii); x++)
193    {
194        if (ISDIGIT (*p_ascii))
195            c = (*p_ascii - '0') << 4;
196        else
197            c = (toupper(*p_ascii) - 'A' + 10) << 4;
198
199        p_ascii++;
200        if (*p_ascii)
201        {
202            if (ISDIGIT (*p_ascii))
203                c |= (*p_ascii - '0');
204            else
205                c |= (toupper(*p_ascii) - 'A' + 10);
206
207            p_ascii++;
208        }
209        *p_hex++ = c;
210    }
211
212    return (x);
213}
214
215
216const char* dump_dm_search_event(UINT16 event)
217{
218    switch(event)
219    {
220        CASE_RETURN_STR(BTA_DM_INQ_RES_EVT)
221        CASE_RETURN_STR(BTA_DM_INQ_CMPL_EVT)
222        CASE_RETURN_STR(BTA_DM_DISC_RES_EVT)
223        CASE_RETURN_STR(BTA_DM_DISC_BLE_RES_EVT)
224        CASE_RETURN_STR(BTA_DM_DISC_CMPL_EVT)
225        CASE_RETURN_STR(BTA_DM_DI_DISC_CMPL_EVT)
226        CASE_RETURN_STR(BTA_DM_SEARCH_CANCEL_CMPL_EVT)
227
228        default:
229            return "UNKNOWN MSG ID";
230     }
231}
232
233
234const char* dump_property_type(bt_property_type_t type)
235{
236    switch(type)
237    {
238        CASE_RETURN_STR(BT_PROPERTY_BDNAME)
239        CASE_RETURN_STR(BT_PROPERTY_BDADDR)
240        CASE_RETURN_STR(BT_PROPERTY_UUIDS)
241        CASE_RETURN_STR(BT_PROPERTY_CLASS_OF_DEVICE)
242        CASE_RETURN_STR(BT_PROPERTY_TYPE_OF_DEVICE)
243        CASE_RETURN_STR(BT_PROPERTY_REMOTE_RSSI)
244        CASE_RETURN_STR(BT_PROPERTY_ADAPTER_DISCOVERY_TIMEOUT)
245        CASE_RETURN_STR(BT_PROPERTY_ADAPTER_BONDED_DEVICES)
246        CASE_RETURN_STR(BT_PROPERTY_ADAPTER_SCAN_MODE)
247        CASE_RETURN_STR(BT_PROPERTY_REMOTE_FRIENDLY_NAME)
248
249        default:
250            return "UNKNOWN PROPERTY ID";
251    }
252}
253
254const char* dump_dm_event(UINT16 event)
255{
256    switch(event)
257    {
258        CASE_RETURN_STR(BTA_DM_ENABLE_EVT)
259        CASE_RETURN_STR(BTA_DM_DISABLE_EVT)
260        CASE_RETURN_STR(BTA_DM_PIN_REQ_EVT)
261        CASE_RETURN_STR(BTA_DM_AUTH_CMPL_EVT)
262        CASE_RETURN_STR(BTA_DM_AUTHORIZE_EVT)
263        CASE_RETURN_STR(BTA_DM_LINK_UP_EVT)
264        CASE_RETURN_STR(BTA_DM_LINK_DOWN_EVT)
265        CASE_RETURN_STR(BTA_DM_SIG_STRENGTH_EVT)
266        CASE_RETURN_STR(BTA_DM_BUSY_LEVEL_EVT)
267        CASE_RETURN_STR(BTA_DM_BOND_CANCEL_CMPL_EVT)
268        CASE_RETURN_STR(BTA_DM_SP_CFM_REQ_EVT)
269        CASE_RETURN_STR(BTA_DM_SP_KEY_NOTIF_EVT)
270        CASE_RETURN_STR(BTA_DM_SP_RMT_OOB_EVT)
271        CASE_RETURN_STR(BTA_DM_SP_KEYPRESS_EVT)
272        CASE_RETURN_STR(BTA_DM_ROLE_CHG_EVT)
273        CASE_RETURN_STR(BTA_DM_BLE_KEY_EVT)
274        CASE_RETURN_STR(BTA_DM_BLE_SEC_REQ_EVT)
275        CASE_RETURN_STR(BTA_DM_BLE_PASSKEY_NOTIF_EVT)
276        CASE_RETURN_STR(BTA_DM_BLE_PASSKEY_REQ_EVT)
277        CASE_RETURN_STR(BTA_DM_BLE_OOB_REQ_EVT)
278        CASE_RETURN_STR(BTA_DM_BLE_LOCAL_IR_EVT)
279        CASE_RETURN_STR(BTA_DM_BLE_LOCAL_ER_EVT)
280        CASE_RETURN_STR(BTA_DM_BLE_AUTH_CMPL_EVT)
281        CASE_RETURN_STR(BTA_DM_DEV_UNPAIRED_EVT)
282        CASE_RETURN_STR(BTA_DM_HW_ERROR_EVT)
283
284        default:
285            return "UNKNOWN DM EVENT";
286    }
287}
288
289const char* dump_hf_event(UINT16 event)
290{
291    switch(event)
292    {
293        CASE_RETURN_STR(BTA_AG_ENABLE_EVT)
294        CASE_RETURN_STR(BTA_AG_REGISTER_EVT)
295        CASE_RETURN_STR(BTA_AG_OPEN_EVT)
296        CASE_RETURN_STR(BTA_AG_CLOSE_EVT)
297        CASE_RETURN_STR(BTA_AG_CONN_EVT)
298        CASE_RETURN_STR(BTA_AG_AUDIO_OPEN_EVT)
299        CASE_RETURN_STR(BTA_AG_AUDIO_CLOSE_EVT)
300        CASE_RETURN_STR(BTA_AG_SPK_EVT)
301        CASE_RETURN_STR(BTA_AG_MIC_EVT)
302        CASE_RETURN_STR(BTA_AG_AT_CKPD_EVT)
303        CASE_RETURN_STR(BTA_AG_DISABLE_EVT)
304
305        CASE_RETURN_STR(BTA_AG_AT_A_EVT)
306        CASE_RETURN_STR(BTA_AG_AT_D_EVT)
307        CASE_RETURN_STR(BTA_AG_AT_CHLD_EVT)
308        CASE_RETURN_STR(BTA_AG_AT_CHUP_EVT)
309        CASE_RETURN_STR(BTA_AG_AT_CIND_EVT)
310        CASE_RETURN_STR(BTA_AG_AT_VTS_EVT)
311        CASE_RETURN_STR(BTA_AG_AT_BINP_EVT)
312        CASE_RETURN_STR(BTA_AG_AT_BLDN_EVT)
313        CASE_RETURN_STR(BTA_AG_AT_BVRA_EVT)
314        CASE_RETURN_STR(BTA_AG_AT_NREC_EVT)
315        CASE_RETURN_STR(BTA_AG_AT_CNUM_EVT)
316        CASE_RETURN_STR(BTA_AG_AT_BTRH_EVT)
317        CASE_RETURN_STR(BTA_AG_AT_CLCC_EVT)
318        CASE_RETURN_STR(BTA_AG_AT_COPS_EVT)
319        CASE_RETURN_STR(BTA_AG_AT_UNAT_EVT)
320        CASE_RETURN_STR(BTA_AG_AT_CBC_EVT)
321        CASE_RETURN_STR(BTA_AG_AT_BAC_EVT)
322        CASE_RETURN_STR(BTA_AG_AT_BCS_EVT)
323
324        default:
325            return "UNKNOWN MSG ID";
326     }
327}
328
329const char* dump_hh_event(UINT16 event)
330{
331    switch(event)
332    {
333        CASE_RETURN_STR(BTA_HH_ENABLE_EVT)
334        CASE_RETURN_STR(BTA_HH_DISABLE_EVT)
335        CASE_RETURN_STR(BTA_HH_OPEN_EVT)
336        CASE_RETURN_STR(BTA_HH_CLOSE_EVT)
337        CASE_RETURN_STR(BTA_HH_GET_DSCP_EVT)
338        CASE_RETURN_STR(BTA_HH_GET_PROTO_EVT)
339        CASE_RETURN_STR(BTA_HH_GET_RPT_EVT)
340        CASE_RETURN_STR(BTA_HH_GET_IDLE_EVT)
341        CASE_RETURN_STR(BTA_HH_SET_PROTO_EVT)
342        CASE_RETURN_STR(BTA_HH_SET_RPT_EVT)
343        CASE_RETURN_STR(BTA_HH_SET_IDLE_EVT)
344        CASE_RETURN_STR(BTA_HH_VC_UNPLUG_EVT)
345        CASE_RETURN_STR(BTA_HH_ADD_DEV_EVT)
346        CASE_RETURN_STR(BTA_HH_RMV_DEV_EVT)
347        CASE_RETURN_STR(BTA_HH_API_ERR_EVT)
348        default:
349            return "UNKNOWN MSG ID";
350     }
351}
352
353
354const char* dump_hf_conn_state(UINT16 event)
355{
356    switch(event)
357    {
358        CASE_RETURN_STR(BTHF_CONNECTION_STATE_DISCONNECTED)
359        CASE_RETURN_STR(BTHF_CONNECTION_STATE_CONNECTING)
360        CASE_RETURN_STR(BTHF_CONNECTION_STATE_CONNECTED)
361        CASE_RETURN_STR(BTHF_CONNECTION_STATE_SLC_CONNECTED)
362        CASE_RETURN_STR(BTHF_CONNECTION_STATE_DISCONNECTING)
363        default:
364            return "UNKNOWN MSG ID";
365    }
366}
367
368const char* dump_hf_call_state(bthf_call_state_t call_state)
369{
370    switch(call_state)
371    {
372        CASE_RETURN_STR(BTHF_CALL_STATE_IDLE)
373        CASE_RETURN_STR(BTHF_CALL_STATE_HELD)
374        CASE_RETURN_STR(BTHF_CALL_STATE_DIALING)
375        CASE_RETURN_STR(BTHF_CALL_STATE_ALERTING)
376        CASE_RETURN_STR(BTHF_CALL_STATE_INCOMING)
377        CASE_RETURN_STR(BTHF_CALL_STATE_WAITING)
378        CASE_RETURN_STR(BTHF_CALL_STATE_ACTIVE)
379        default:
380            return "UNKNOWN CALL STATE";
381    }
382}
383
384const char* dump_thread_evt(bt_cb_thread_evt evt)
385{
386    switch(evt)
387    {
388        CASE_RETURN_STR(ASSOCIATE_JVM)
389        CASE_RETURN_STR(DISASSOCIATE_JVM)
390
391        default:
392            return "unknown thread evt";
393    }
394}
395
396
397const char* dump_hf_audio_state(UINT16 event)
398{
399    switch(event)
400    {
401        CASE_RETURN_STR(BTHF_AUDIO_STATE_DISCONNECTED)
402        CASE_RETURN_STR(BTHF_AUDIO_STATE_CONNECTING)
403        CASE_RETURN_STR(BTHF_AUDIO_STATE_CONNECTED)
404        CASE_RETURN_STR(BTHF_AUDIO_STATE_DISCONNECTING)
405        default:
406           return "UNKNOWN MSG ID";
407
408    }
409}
410
411const char* dump_av_conn_state(UINT16 event)
412{
413    switch(event)
414    {
415        CASE_RETURN_STR(BTAV_CONNECTION_STATE_DISCONNECTED)
416        CASE_RETURN_STR(BTAV_CONNECTION_STATE_CONNECTING)
417        CASE_RETURN_STR(BTAV_CONNECTION_STATE_CONNECTED)
418        CASE_RETURN_STR(BTAV_CONNECTION_STATE_DISCONNECTING)
419        default:
420            return "UNKNOWN MSG ID";
421    }
422}
423
424const char* dump_av_audio_state(UINT16 event)
425{
426    switch(event)
427    {
428        CASE_RETURN_STR(BTAV_AUDIO_STATE_REMOTE_SUSPEND)
429        CASE_RETURN_STR(BTAV_AUDIO_STATE_STOPPED)
430        CASE_RETURN_STR(BTAV_AUDIO_STATE_STARTED)
431        default:
432            return "UNKNOWN MSG ID";
433    }
434}
435
436const char* dump_adapter_scan_mode(bt_scan_mode_t mode)
437{
438    switch(mode)
439    {
440        CASE_RETURN_STR(BT_SCAN_MODE_NONE)
441        CASE_RETURN_STR(BT_SCAN_MODE_CONNECTABLE)
442        CASE_RETURN_STR(BT_SCAN_MODE_CONNECTABLE_DISCOVERABLE)
443
444        default:
445            return "unknown scan mode";
446    }
447}
448
449const char* dump_bt_status(bt_status_t status)
450{
451    switch(status)
452    {
453        CASE_RETURN_STR(BT_STATUS_SUCCESS)
454        CASE_RETURN_STR(BT_STATUS_FAIL)
455        CASE_RETURN_STR(BT_STATUS_NOT_READY)
456        CASE_RETURN_STR(BT_STATUS_NOMEM)
457        CASE_RETURN_STR(BT_STATUS_BUSY)
458        CASE_RETURN_STR(BT_STATUS_UNSUPPORTED)
459
460        default:
461            return "unknown scan mode";
462    }
463}
464
465const char *dump_rc_event(UINT8 event)
466{
467   switch(event) {
468        CASE_RETURN_STR(BTA_AV_RC_OPEN_EVT)
469        CASE_RETURN_STR(BTA_AV_RC_CLOSE_EVT)
470        CASE_RETURN_STR(BTA_AV_REMOTE_CMD_EVT)
471        CASE_RETURN_STR(BTA_AV_REMOTE_RSP_EVT)
472        CASE_RETURN_STR(BTA_AV_VENDOR_CMD_EVT)
473        CASE_RETURN_STR(BTA_AV_VENDOR_RSP_EVT)
474        CASE_RETURN_STR(BTA_AV_META_MSG_EVT)
475        CASE_RETURN_STR(BTA_AV_RC_FEAT_EVT)
476        default:
477            return "UNKNOWN_EVENT";
478   }
479}
480
481const char * dump_rc_notification_event_id(UINT8 event_id)
482{
483    switch(event_id)
484    {
485        CASE_RETURN_STR(AVRC_EVT_PLAY_STATUS_CHANGE)
486        CASE_RETURN_STR(AVRC_EVT_TRACK_CHANGE)
487        CASE_RETURN_STR(AVRC_EVT_TRACK_REACHED_END)
488        CASE_RETURN_STR(AVRC_EVT_TRACK_REACHED_START)
489        CASE_RETURN_STR(AVRC_EVT_PLAY_POS_CHANGED)
490        CASE_RETURN_STR(AVRC_EVT_BATTERY_STATUS_CHANGE)
491        CASE_RETURN_STR(AVRC_EVT_SYSTEM_STATUS_CHANGE)
492        CASE_RETURN_STR(AVRC_EVT_APP_SETTING_CHANGE)
493        CASE_RETURN_STR(AVRC_EVT_VOLUME_CHANGE)
494
495        default:
496            return "Unhandled Event ID";
497    }
498}
499
500const char*  dump_rc_pdu(UINT8 pdu)
501{
502    switch(pdu)
503    {
504        CASE_RETURN_STR(AVRC_PDU_LIST_PLAYER_APP_ATTR)
505        CASE_RETURN_STR(AVRC_PDU_LIST_PLAYER_APP_VALUES)
506        CASE_RETURN_STR(AVRC_PDU_GET_CUR_PLAYER_APP_VALUE)
507        CASE_RETURN_STR(AVRC_PDU_SET_PLAYER_APP_VALUE)
508        CASE_RETURN_STR(AVRC_PDU_GET_PLAYER_APP_ATTR_TEXT)
509        CASE_RETURN_STR(AVRC_PDU_GET_PLAYER_APP_VALUE_TEXT)
510        CASE_RETURN_STR(AVRC_PDU_INFORM_DISPLAY_CHARSET)
511        CASE_RETURN_STR(AVRC_PDU_INFORM_BATTERY_STAT_OF_CT)
512        CASE_RETURN_STR(AVRC_PDU_GET_ELEMENT_ATTR)
513        CASE_RETURN_STR(AVRC_PDU_GET_PLAY_STATUS)
514        CASE_RETURN_STR(AVRC_PDU_REGISTER_NOTIFICATION)
515        CASE_RETURN_STR(AVRC_PDU_REQUEST_CONTINUATION_RSP)
516        CASE_RETURN_STR(AVRC_PDU_ABORT_CONTINUATION_RSP)
517        CASE_RETURN_STR(AVRC_PDU_SET_ABSOLUTE_VOLUME)
518        default:
519            return "Unknown PDU";
520    }
521}
522
523
524