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