Home | History | Annotate | Download | only in btm
      1 /******************************************************************************
      2  *
      3  *  Copyright (C) 1999-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  *  This file contains functions that handle BTM interface functions for the
     22  *  Bluetooth device including Rest, HCI buffer size and others
     23  *
     24  ******************************************************************************/
     25 
     26 #include <stdlib.h>
     27 #include <string.h>
     28 #include <stdio.h>
     29 #include <stddef.h>
     30 
     31 #include "bt_types.h"
     32 #include "hcimsgs.h"
     33 #include "btu.h"
     34 #include "btm_int.h"
     35 #include "l2c_int.h"
     36 
     37 #if BLE_INCLUDED == TRUE
     38 #include "gatt_int.h"
     39 
     40 #if GAP_INCLUDED == TRUE
     41 #include "gap_api.h"
     42 #include "gattdefs.h"
     43 #endif
     44 
     45 #endif /* BLE_INCLUDED */
     46 
     47 /* BTM_APP_DEV_INIT should be defined if additional controller initialization is
     48 **      needed by the application to be performed after the HCI reset
     49 */
     50 #ifdef BTM_APP_DEV_INIT
     51 extern void BTM_APP_DEV_INIT(void);
     52 #endif
     53 
     54 #ifdef BTA_PRM_CHECK_FW_VER
     55 extern BOOLEAN BTA_PRM_CHECK_FW_VER(UINT8 *p);
     56 #endif
     57 
     58 #ifndef TT_DEV_RESET_MASK
     59 #define TT_DEV_RESET_MASK 0xff
     60 #endif
     61 
     62 /********************************************************************************/
     63 /*                 L O C A L    D A T A    D E F I N I T I O N S                */
     64 /********************************************************************************/
     65 
     66 /* The default class of device. */
     67 #ifndef BTM_INIT_CLASS_OF_DEVICE
     68 #define BTM_INIT_CLASS_OF_DEVICE    "\x00\x1F\x00"
     69 #endif
     70 
     71 #ifndef BTM_DEV_RESET_TIMEOUT
     72 #define BTM_DEV_RESET_TIMEOUT   4
     73 #endif
     74 
     75 #define BTM_DEV_REPLY_TIMEOUT   2    /* 1 second expiration time is not good. Timer may start between 0 and 1 second. */
     76                                      /* if it starts at the very end of the 0 second, timer will expire really easily. */
     77 
     78 #define BTM_INFO_TIMEOUT        5   /* 5 seconds for info response */
     79 
     80 /* After Reset a timeout can be specified in the target.h for specific targets
     81  * that may require additional time to reset
     82  * otherwise no timeout is required
     83 */
     84 #ifndef BTM_AFTER_RESET_TIMEOUT
     85 #define BTM_AFTER_RESET_TIMEOUT 0
     86 #endif
     87 
     88 /* Internal baseband so the parameters such as local features, version etc. are known
     89 so there is no need to issue HCI commands and wait for responses at BTM initialization */
     90 #ifndef BTM_INTERNAL_BB
     91 #define BTM_INTERNAL_BB FALSE
     92 #endif
     93 
     94 /* The local version information in the format specified in the HCI read local version
     95 response message */
     96 #ifndef BTM_INTERNAL_LOCAL_VER
     97 #define BTM_INTERNAL_LOCAL_VER {0x00, 0x01, 0x05, 0x81, 0x01, 0x30, 0x00, 0x40, 0x8D}
     98 #endif
     99 
    100 /* The local features information in the format specified in the HCI read local features
    101 response message */
    102 #ifndef BTM_INTERNAL_LOCAL_FEA
    103 #define BTM_INTERNAL_LOCAL_FEA {0x00, 0xFF, 0xF9, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00}
    104 #endif
    105 
    106 #ifndef BTM_SET_DEV_NAME_UPON_RESET
    107 #define BTM_SET_DEV_NAME_UPON_RESET TRUE
    108 #endif
    109 
    110 /* host SCO buffer size */
    111 #ifndef BTM_SCO_HOST_BUF_SIZE
    112 #define BTM_SCO_HOST_BUF_SIZE       0xff
    113 #endif
    114 
    115 #ifndef BTM_GPS_UIPC_CH_NB
    116 #define BTM_GPS_UIPC_CH_NB UIPC_CH_ID_1
    117 #endif
    118 
    119 /********************************************************************************/
    120 /*              L O C A L    F U N C T I O N     P R O T O T Y P E S            */
    121 /********************************************************************************/
    122 static void btm_dev_reset (void);
    123 static void btm_after_reset_hold_complete (void);
    124 static void btm_continue_reset (void);
    125 
    126 /*******************************************************************************
    127 **
    128 ** Function         btm_dev_init
    129 **
    130 ** Description      This function is on the BTM startup
    131 **
    132 ** Returns          void
    133 **
    134 *******************************************************************************/
    135 void btm_dev_init (void)
    136 {
    137 #if 0  /* cleared in btm_init; put back in if called from anywhere else! */
    138     memset (&btm_cb.devcb, 0, sizeof (tBTM_DEVCB));
    139 #endif
    140 
    141     /* Initialize nonzero defaults */
    142 #if (BTM_MAX_LOC_BD_NAME_LEN > 0)
    143     memset(btm_cb.cfg.bd_name, 0, sizeof(tBTM_LOC_BD_NAME));
    144 #if (BTM_USE_DEF_LOCAL_NAME == TRUE)
    145     BCM_STRNCPY_S(btm_cb.cfg.bd_name, sizeof(btm_cb.cfg.bd_name), BTM_DEF_LOCAL_NAME, BTM_MAX_LOC_BD_NAME_LEN);
    146 #endif
    147 #endif
    148 
    149     btm_cb.devcb.reset_timer.param  = (TIMER_PARAM_TYPE)TT_DEV_RESET;
    150     btm_cb.devcb.rln_timer.param    = (TIMER_PARAM_TYPE)TT_DEV_RLN;
    151     btm_cb.devcb.rlinkp_timer.param = (TIMER_PARAM_TYPE)TT_DEV_RLNKP;
    152 
    153     btm_cb.btm_acl_pkt_types_supported = BTM_ACL_PKT_TYPES_MASK_DH1 + BTM_ACL_PKT_TYPES_MASK_DM1 +
    154                                          BTM_ACL_PKT_TYPES_MASK_DH3 + BTM_ACL_PKT_TYPES_MASK_DM3 +
    155                                          BTM_ACL_PKT_TYPES_MASK_DH5 + BTM_ACL_PKT_TYPES_MASK_DM5;
    156 
    157     btm_cb.btm_sco_pkt_types_supported = BTM_SCO_PKT_TYPES_MASK_HV1 +
    158                                          BTM_SCO_PKT_TYPES_MASK_HV2 +
    159                                          BTM_SCO_PKT_TYPES_MASK_HV3 +
    160                                          BTM_SCO_PKT_TYPES_MASK_EV3 +
    161                                          BTM_SCO_PKT_TYPES_MASK_EV4 +
    162                                          BTM_SCO_PKT_TYPES_MASK_EV5;
    163 
    164     btm_cb.first_disabled_channel = 0xff; /* To allow disabling 0th channel alone */
    165     btm_cb.last_disabled_channel = 0xff; /* To allow disabling 0th channel alone */
    166 
    167 #if (BTM_AUTOMATIC_HCI_RESET == TRUE)
    168 
    169 #if (BTM_FIRST_RESET_DELAY > 0)
    170     btm_cb.devcb.state = BTM_DEV_STATE_WAIT_RESET_CMPLT;
    171     btu_start_timer (&btm_cb.devcb.reset_timer, BTU_TTYPE_BTM_DEV_CTL, BTM_FIRST_RESET_DELAY);
    172 #else
    173     btm_dev_reset();
    174 #endif
    175 
    176 #else
    177    BTM_TRACE_EVENT0 ("BTM_AUTOMATIC_HCI_RESET is FALSE, so skip btm reset for now");
    178 #endif
    179 
    180 }
    181 
    182 
    183 /*******************************************************************************
    184 **
    185 ** Function         btm_db_reset
    186 **
    187 ** Description      This function is called by BTM_DeviceReset and clears out any
    188 **                  pending callbacks for inquiries, discoveries, other pending
    189 **                  functions that may be in progress.
    190 **
    191 ** Returns          void
    192 **
    193 *******************************************************************************/
    194 static void btm_db_reset (void)
    195 {
    196     tBTM_CMPL_CB    *p_cb;
    197     tBTM_STATUS      status = BTM_DEV_RESET;
    198 
    199     btm_inq_db_reset();
    200 
    201     if (btm_cb.devcb.p_rln_cmpl_cb)
    202     {
    203         p_cb = btm_cb.devcb.p_rln_cmpl_cb;
    204         btm_cb.devcb.p_rln_cmpl_cb = NULL;
    205 
    206         if (p_cb)
    207             (*p_cb)((void *) NULL);
    208     }
    209 
    210     if (btm_cb.devcb.p_rlinkp_cmpl_cb)
    211     {
    212         p_cb = btm_cb.devcb.p_rlinkp_cmpl_cb;
    213         btm_cb.devcb.p_rlinkp_cmpl_cb = NULL;
    214 
    215         if (p_cb)
    216             (*p_cb)((void *) &status);
    217     }
    218 
    219     if (btm_cb.devcb.p_rssi_cmpl_cb)
    220     {
    221         p_cb = btm_cb.devcb.p_rssi_cmpl_cb;
    222         btm_cb.devcb.p_rssi_cmpl_cb = NULL;
    223 
    224         if (p_cb)
    225             (*p_cb)((tBTM_RSSI_RESULTS *) &status);
    226     }
    227 }
    228 
    229 
    230 
    231 /*******************************************************************************
    232 **
    233 ** Function         btm_dev_absent
    234 **
    235 ** Description      This function is called by when it is detected that the
    236 **                  device is not connected any more.
    237 **
    238 ** Returns          void
    239 **
    240 *******************************************************************************/
    241 void btm_dev_absent (void)
    242 {
    243     btm_cb.devcb.state = BTM_DEV_STATE_WAIT_RESET_CMPLT;
    244 
    245     btm_db_reset ();
    246     btm_inq_db_reset();
    247 
    248     /* If anyone wants device status notifications, give him one */
    249     btm_report_device_status (BTM_DEV_STATUS_DOWN);
    250 
    251     btu_stop_timer (&btm_cb.devcb.reset_timer);
    252 }
    253 
    254 
    255 /*******************************************************************************
    256 **
    257 ** Function         BTM_DeviceReset
    258 **
    259 ** Description      This function is called to reset the HCI.  Callback function
    260 **                  if provided is called when startup of the device is
    261 **                  completed.
    262 **
    263 ** Returns          void
    264 **
    265 *******************************************************************************/
    266 void BTM_DeviceReset (tBTM_CMPL_CB *p_cb)
    267 {
    268     tBTM_STATUS status;
    269 
    270     /* If device is already resetting, do not allow another */
    271     if ((!btm_cb.devcb.p_reset_cmpl_cb) || (btm_cb.devcb.p_reset_cmpl_cb == p_cb))
    272     {
    273         /* Flush all ACL connections */
    274         btm_acl_device_down();
    275 
    276         /* Clear the callback, so application would not hang on reset */
    277         btm_db_reset();
    278 
    279         /* Save address of the completion routine, if provided */
    280         btm_cb.devcb.p_reset_cmpl_cb = p_cb;
    281 
    282         btm_dev_reset ();
    283     }
    284     else
    285     {
    286         /* pass an error to the bad callback, another one was already provided */
    287         if (p_cb)
    288         {
    289             status = BTM_ILLEGAL_VALUE;
    290             p_cb (&status);
    291         }
    292     }
    293 }
    294 
    295 
    296 /*******************************************************************************
    297 **
    298 ** Function         BTM_IsDeviceUp
    299 **
    300 ** Description      This function is called to check if the device is up.
    301 **
    302 ** Returns          TRUE if device is up, else FALSE
    303 **
    304 *******************************************************************************/
    305 BOOLEAN BTM_IsDeviceUp (void)
    306 {
    307     return ((BOOLEAN) (btm_cb.devcb.state == BTM_DEV_STATE_READY));
    308 }
    309 
    310 /*******************************************************************************
    311 **
    312 ** Function         BTM_SetAfhChannels
    313 **
    314 ** Description      This function is called disable channels
    315 **
    316 ** Returns          tBTM_STATUS
    317 **
    318 *******************************************************************************/
    319 tBTM_STATUS BTM_SetAfhChannels (UINT8 first, UINT8 last)
    320 {
    321     BTM_TRACE_API4 ("BTM_SetAfhChannels first: %d (%d) last: %d (%d)",
    322                        first, btm_cb.first_disabled_channel, last,
    323                        btm_cb.last_disabled_channel);
    324 
    325     /* Make sure the local device supports the feature before sending */
    326     if ((!HCI_LMP_AFH_CAP_MASTR_SUPPORTED(btm_cb.devcb.local_features))   &&
    327         (!HCI_LMP_AFH_CLASS_SLAVE_SUPPORTED(btm_cb.devcb.local_features)) &&
    328         (!HCI_LMP_AFH_CLASS_MASTR_SUPPORTED(btm_cb.devcb.local_features)))
    329         return (BTM_MODE_UNSUPPORTED);
    330 
    331     if (!BTM_IsDeviceUp())
    332         return (BTM_WRONG_MODE);
    333 
    334     if ((btm_cb.first_disabled_channel != first)
    335      || (btm_cb.last_disabled_channel  != last))
    336     {
    337         if (btsnd_hcic_set_afh_channels (first, last))
    338         {
    339             btm_cb.first_disabled_channel = first;
    340             btm_cb.last_disabled_channel  = last;
    341         }
    342         else
    343             return (BTM_NO_RESOURCES);
    344     }
    345     return (BTM_SUCCESS);
    346 }
    347 
    348 /*******************************************************************************
    349 **
    350 ** Function         BTM_SetAfhChannelAssessment
    351 **
    352 ** Description      This function is called to set the channel assessment mode on or off
    353 **
    354 ** Returns          none
    355 **
    356 *******************************************************************************/
    357 tBTM_STATUS BTM_SetAfhChannelAssessment (BOOLEAN enable_or_disable)
    358 {
    359     /* whatever app wants if device is not 1.2 scan type should be STANDARD */
    360     if (!HCI_LMP_AFH_CAP_SLAVE_SUPPORTED(btm_cb.devcb.local_features))
    361      return (BTM_MODE_UNSUPPORTED);
    362 
    363     if (!btsnd_hcic_write_afh_channel_assessment_mode (enable_or_disable))
    364         return (BTM_NO_RESOURCES);
    365 
    366     return (BTM_SUCCESS);
    367 }
    368 
    369 /*******************************************************************************
    370 **
    371 ** Function         BTM_ContinueReset
    372 **
    373 ** Description      This function is called by the application to continue
    374 **                  initialization after the application has completed its
    375 **                  vendor specific sequence.  It is only used when
    376 **                  BTM_APP_DEV_INIT is defined in target.h.
    377 **
    378 ** Returns          void
    379 **
    380 *******************************************************************************/
    381 void BTM_ContinueReset (void)
    382 {
    383 #ifdef BTM_APP_DEV_INIT
    384     btm_continue_reset();
    385 #endif
    386 }
    387 
    388 /*******************************************************************************
    389 **
    390 ** Function         btm_dev_reset
    391 **
    392 ** Description      Local function called to send a reset command
    393 **
    394 ** Returns          void
    395 **
    396 *******************************************************************************/
    397 static void btm_dev_reset (void)
    398 {
    399     btm_cb.devcb.state = BTM_DEV_STATE_WAIT_RESET_CMPLT;
    400 
    401     /* flush out the command complete queue and command transmit queue */
    402     btu_hcif_flush_cmd_queue();
    403 
    404     /* Start reset timer.  When timer expires we will send first command */
    405     /* from the setup sequence */
    406 
    407     btu_start_timer (&btm_cb.devcb.reset_timer, BTU_TTYPE_BTM_DEV_CTL,
    408                      BTM_DEV_RESET_TIMEOUT);
    409     btsnd_hcic_reset (LOCAL_BR_EDR_CONTROLLER_ID);
    410 }
    411 
    412 
    413 /*******************************************************************************
    414 **
    415 ** Function         btm_get_hci_buf_size
    416 **
    417 ** Description      Local function called to send a read buffer size command
    418 **
    419 ** Returns          void
    420 **
    421 *******************************************************************************/
    422 void btm_get_hci_buf_size (void)
    423 {
    424 
    425     btu_start_timer (&btm_cb.devcb.reset_timer, BTU_TTYPE_BTM_DEV_CTL, BTM_DEV_REPLY_TIMEOUT);
    426 
    427     /* Send a Read Buffer Size message to the Host Controller. */
    428     btsnd_hcic_read_buffer_size ();
    429 
    430 }
    431 #if BLE_INCLUDED == TRUE
    432 /*******************************************************************************
    433 **
    434 ** Function         btm_read_ble_wl_size
    435 **
    436 ** Description      Local function called to send a read BLE buffer size command
    437 **
    438 ** Returns          void
    439 **
    440 *******************************************************************************/
    441 void btm_read_ble_wl_size(void)
    442 {
    443     BTM_TRACE_DEBUG0("btm_read_ble_wl_size ");
    444     btu_start_timer (&btm_cb.devcb.reset_timer, BTU_TTYPE_BTM_DEV_CTL, BTM_DEV_REPLY_TIMEOUT);
    445 
    446     /* Send a Read Buffer Size message to the Host Controller. */
    447     btsnd_hcic_ble_read_white_list_size();
    448 }
    449 /*******************************************************************************
    450 **
    451 ** Function         btm_get_ble_buffer_size
    452 **
    453 ** Description      Local function called to send a read BLE buffer size command
    454 **
    455 ** Returns          void
    456 **
    457 *******************************************************************************/
    458 void btm_get_ble_buffer_size(void)
    459 {
    460     BTM_TRACE_DEBUG0("btm_get_ble_buffer_size ");
    461     btu_start_timer (&btm_cb.devcb.reset_timer, BTU_TTYPE_BTM_DEV_CTL, BTM_DEV_REPLY_TIMEOUT);
    462 
    463     /* Send a Read Buffer Size message to the Host Controller. */
    464     btsnd_hcic_ble_read_buffer_size ();
    465 }
    466 #endif
    467 /*******************************************************************************
    468 **
    469 ** Function         btm_get_local_version
    470 **
    471 ** Description      Local function called to send a read local version to controller
    472 **
    473 ** Returns          void
    474 **
    475 *******************************************************************************/
    476 void btm_get_local_version (void)
    477 {
    478 
    479     btu_start_timer (&btm_cb.devcb.reset_timer, BTU_TTYPE_BTM_DEV_CTL, BTM_DEV_REPLY_TIMEOUT);
    480 
    481     /* Send a Read Local Version message to the Host Controller. */
    482     btsnd_hcic_read_local_ver (LOCAL_BR_EDR_CONTROLLER_ID);
    483     btsnd_hcic_read_bd_addr ();
    484 
    485 #if BTM_PWR_MGR_INCLUDED == TRUE
    486         btm_pm_reset();
    487 #endif
    488 
    489 }
    490 
    491 /*******************************************************************************
    492 **
    493 ** Function         btm_get_local_features
    494 **
    495 ** Description      Local function called to send a read local features
    496 **
    497 ** Returns          void
    498 **
    499 *******************************************************************************/
    500 void btm_get_local_features (void)
    501 {
    502     btu_start_timer (&btm_cb.devcb.reset_timer, BTU_TTYPE_BTM_DEV_CTL, BTM_DEV_REPLY_TIMEOUT);
    503 
    504     btsnd_hcic_read_local_features ();
    505 }
    506 
    507 /*******************************************************************************
    508 **
    509 ** Function         btm_dev_timeout
    510 **
    511 ** Description      This function is called when a timer list entry expires.
    512 **
    513 ** Returns          void
    514 **
    515 *******************************************************************************/
    516 void btm_dev_timeout (TIMER_LIST_ENT  *p_tle)
    517 {
    518     TIMER_PARAM_TYPE timer_type = (TIMER_PARAM_TYPE)p_tle->param;
    519 
    520     if ((timer_type & TT_DEV_RESET_MASK) == TT_DEV_RESET)
    521     {
    522         /* Call device reset as long as there is timeout*/
    523         btm_dev_reset();
    524     }
    525     else if (timer_type == (TIMER_PARAM_TYPE)TT_DEV_RLN)
    526     {
    527         tBTM_CMPL_CB  *p_cb = btm_cb.devcb.p_rln_cmpl_cb;
    528 
    529         btm_cb.devcb.p_rln_cmpl_cb = NULL;
    530 
    531         if (p_cb)
    532             (*p_cb)((void *) NULL);
    533     }
    534 }
    535 
    536 /*******************************************************************************
    537 **
    538 ** Function         btm_reset_complete
    539 **
    540 ** Description      This function is called when command complete for HCI_Reset
    541 **                  is received.  It does not make sense to send next command
    542 **                  because device is resetting after command complete is
    543 **                  received.  Just start timer and set required state.
    544 **
    545 ** Returns          void
    546 **
    547 *******************************************************************************/
    548 void btm_reset_complete (void)
    549 {
    550     int devinx;
    551 
    552     BTM_TRACE_EVENT0 ("btm_reset_complete");
    553 
    554 #ifdef BRCM_VS
    555     btm_vs_reset_complete();
    556 #endif
    557 
    558 
    559     /* Handle if btm initiated the reset */
    560     if (btm_cb.devcb.state == BTM_DEV_STATE_WAIT_RESET_CMPLT)
    561     {
    562         /* Tell L2CAP that all connections are gone */
    563         l2cu_device_reset ();
    564 
    565         /* Clear current security state */
    566         for (devinx = 0; devinx < BTM_SEC_MAX_DEVICE_RECORDS; devinx++)
    567         {
    568             btm_cb.sec_dev_rec[devinx].sec_state = BTM_SEC_STATE_IDLE;
    569         }
    570 
    571         /* After the reset controller should restore all parameters to defaults. */
    572         btm_cb.btm_inq_vars.inq_counter       = 1;
    573         btm_cb.btm_inq_vars.inq_scan_window   = HCI_DEF_INQUIRYSCAN_WINDOW;
    574         btm_cb.btm_inq_vars.inq_scan_period   = HCI_DEF_INQUIRYSCAN_INTERVAL;
    575         btm_cb.btm_inq_vars.inq_scan_type     = HCI_DEF_SCAN_TYPE;
    576 
    577         btm_cb.btm_inq_vars.page_scan_window  = HCI_DEF_PAGESCAN_WINDOW;
    578         btm_cb.btm_inq_vars.page_scan_period  = HCI_DEF_PAGESCAN_INTERVAL;
    579         btm_cb.btm_inq_vars.page_scan_type    = HCI_DEF_SCAN_TYPE;
    580 
    581 #if (BTM_AFTER_RESET_TIMEOUT > 0)
    582         btu_start_timer (&btm_cb.devcb.reset_timer, BTU_TTYPE_BTM_DEV_CTL,
    583                          BTM_AFTER_RESET_TIMEOUT);
    584 #else
    585         btm_cb.devcb.state = BTM_DEV_STATE_WAIT_AFTER_RESET;
    586         btm_after_reset_hold_complete();
    587 #endif
    588 
    589 #if (BLE_INCLUDED == TRUE)
    590      btm_cb.ble_ctr_cb.bg_conn_state = BLE_BG_CONN_IDLE;
    591      btm_cb.ble_ctr_cb.bg_conn_dev_num = 0;
    592      btm_cb.ble_ctr_cb.bg_conn_type = BTM_BLE_CONN_NONE;
    593      btm_cb.ble_ctr_cb.p_select_cback = NULL;
    594      memset(&btm_cb.ble_ctr_cb.bg_conn_dev_list, 0, (sizeof(BD_ADDR)*BTM_BLE_MAX_BG_CONN_DEV_NUM));
    595      gatt_reset_bgdev_list();
    596 #endif
    597     }
    598 }
    599 
    600 /*******************************************************************************
    601 **
    602 ** Function         btm_continue_reset
    603 **
    604 ** Description      This function is called when wait period expired after
    605 **                  device reset or called by the application to continue
    606 **                  initialization after the application has completed its
    607 **                  vendor specific sequence.
    608 **
    609 ** Returns          void
    610 **
    611 *******************************************************************************/
    612 void btm_continue_reset (void)
    613 {
    614 
    615     /* Reinitialize the default class of device */
    616 #if BTM_INTERNAL_BB == TRUE
    617     btsnd_hcic_read_bd_addr ();
    618 #if BTM_PWR_MGR_INCLUDED == TRUE
    619     btm_pm_reset();
    620 #endif
    621 #endif
    622 
    623     btm_get_hci_buf_size ();
    624 
    625     /* default device class */
    626     BTM_SetDeviceClass((UINT8 *) BTM_INIT_CLASS_OF_DEVICE);
    627 
    628 #if (BTM_MAX_LOC_BD_NAME_LEN > 0) && (BTM_SET_DEV_NAME_UPON_RESET == TRUE)
    629     BTM_SetLocalDeviceName(btm_cb.cfg.bd_name);
    630 #elif BTM_USE_DEF_LOCAL_NAME == TRUE
    631     BTM_SetLocalDeviceName(BTM_DEF_LOCAL_NAME);
    632 #endif
    633 
    634     BTM_SetPinType (btm_cb.cfg.pin_type, btm_cb.cfg.pin_code, btm_cb.cfg.pin_code_len);
    635 }
    636 
    637 /*******************************************************************************
    638 **
    639 ** Function         btm_after_reset_hold_complete
    640 **
    641 ** Description      This function is called when wait period expired after
    642 **                  device reset.  Continue intitialization
    643 **
    644 ** Returns          void
    645 **
    646 *******************************************************************************/
    647 void btm_after_reset_hold_complete (void)
    648 {
    649 #ifdef BTM_APP_DEV_INIT
    650     btu_stop_timer(&btm_cb.devcb.reset_timer);
    651     BTM_APP_DEV_INIT();
    652 #else
    653     btm_continue_reset();
    654 #endif
    655 }
    656 
    657 
    658 /*******************************************************************************
    659 **
    660 ** Function         btm_read_hci_buf_size_complete
    661 **
    662 ** Description      This function is called when command complete for
    663 **                  get HCI buffer size is received.  Start timer and send
    664 **                  read local featues request
    665 **
    666 ** Returns          void
    667 **
    668 *******************************************************************************/
    669 void btm_read_hci_buf_size_complete (UINT8 *p, UINT16 evt_len)
    670 {
    671     UINT8       status;
    672     UINT8       lm_sco_buf_size;
    673     UINT16      lm_num_acl_bufs;
    674     UINT16      lm_num_sco_bufs;
    675     UINT16      acl_buf_size;
    676 
    677     STREAM_TO_UINT8  (status, p);
    678     if (status == HCI_SUCCESS)
    679     {
    680         STREAM_TO_UINT16 (btu_cb.hcit_acl_data_size, p);
    681         STREAM_TO_UINT8  (lm_sco_buf_size,   p);
    682         STREAM_TO_UINT16 (lm_num_acl_bufs,   p);
    683         STREAM_TO_UINT16 (lm_num_sco_bufs,   p);
    684 
    685         btu_cb.hcit_acl_pkt_size = btu_cb.hcit_acl_data_size + HCI_DATA_PREAMBLE_SIZE;
    686 
    687         l2c_link_processs_num_bufs (lm_num_acl_bufs);
    688 
    689 #if BTM_ACL_BUF_SIZE > 0
    690         acl_buf_size = (BTM_ACL_BUF_SIZE < L2CAP_MTU_SIZE) ? BTM_ACL_BUF_SIZE : L2CAP_MTU_SIZE;
    691 #else
    692         acl_buf_size = L2CAP_MTU_SIZE;
    693 #endif
    694         /* Tell the controller what our buffer sizes are. ?? Need SCO info */
    695         btsnd_hcic_set_host_buf_size (acl_buf_size, BTM_SCO_HOST_BUF_SIZE, L2CAP_HOST_FC_ACL_BUFS, 10);
    696 
    697 #if L2CAP_HOST_FLOW_CTRL == TRUE
    698         btsnd_hcic_set_host_flow_ctrl (HCI_HOST_FLOW_CTRL_ACL_ON);
    699 #endif
    700     }
    701 
    702     /* Set the device into connectable and/or discoverable mode (if configured to do so) */
    703 #if BTM_IS_CONNECTABLE == TRUE
    704     (void) BTM_SetConnectability (BTM_CONNECTABLE, BTM_DEFAULT_CONN_WINDOW, BTM_DEFAULT_CONN_INTERVAL);
    705 #endif
    706 
    707 #if BTM_IS_DISCOVERABLE == TRUE
    708     (void) BTM_SetDiscoverability (BTM_DEFAULT_DISC_MODE, BTM_DEFAULT_DISC_WINDOW, BTM_DEFAULT_DISC_INTERVAL);
    709 #endif
    710 
    711 #if BTM_INTERNAL_BB == TRUE
    712     {
    713         UINT8 buf[9] = BTM_INTERNAL_LOCAL_VER;
    714         btm_read_local_version_complete( buf, 9 );
    715     }
    716 #else
    717     btm_get_local_version ();
    718 #endif
    719 }
    720 
    721 #if (BLE_INCLUDED == TRUE)
    722 /*******************************************************************************
    723 **
    724 ** Function         btm_read_ble_buf_size_complete
    725 **
    726 ** Description      This function is called when command complete for
    727 **                  get HCI buffer size is received.  Start timer and send
    728 **                  read local featues request
    729 **
    730 ** Returns          void
    731 **
    732 *******************************************************************************/
    733 void btm_read_ble_buf_size_complete (UINT8 *p, UINT16 evt_len)
    734 {
    735     UINT8       status;
    736     UINT16      lm_num_le_bufs;
    737 
    738      BTM_TRACE_DEBUG0("btm_read_ble_buf_size_complete ");
    739     STREAM_TO_UINT8  (status, p);
    740     if (status == HCI_SUCCESS)
    741     {
    742         STREAM_TO_UINT16 (btu_cb.hcit_ble_acl_data_size, p);
    743         STREAM_TO_UINT8 (lm_num_le_bufs,   p);
    744 
    745         btu_cb.hcit_ble_acl_pkt_size = btu_cb.hcit_ble_acl_data_size + HCI_DATA_PREAMBLE_SIZE;
    746 
    747         l2c_link_processs_ble_num_bufs (lm_num_le_bufs);
    748     }
    749 
    750 #if BTM_INTERNAL_BB == TRUE
    751     {
    752         UINT8 buf[9] = BTM_INTERNAL_LOCAL_FEA;
    753         btm_read_local_features_complete( buf, 9 );
    754     }
    755 #else
    756 #ifdef BRCM_VS
    757     btm_brcm_feat_init();
    758 #else
    759     /* get local feature if BRCM specific feature is not included  */
    760     btm_get_local_features ();
    761 #endif
    762 #endif
    763 
    764 }
    765 
    766 /*******************************************************************************
    767 **
    768 ** Function         btm_read_white_list_size_complete
    769 **
    770 ** Description      This function read the current white list size.
    771 *******************************************************************************/
    772 void btm_read_white_list_size_complete(UINT8 *p, UINT16 evt_len)
    773 {
    774     UINT8       status;
    775 
    776      BTM_TRACE_DEBUG0("btm_read_white_list_size_complete ");
    777     STREAM_TO_UINT8  (status, p);
    778 
    779     if (status == HCI_SUCCESS)
    780     {
    781         STREAM_TO_UINT8(btm_cb.ble_ctr_cb.max_filter_entries, p);
    782         btm_cb.ble_ctr_cb.num_empty_filter = btm_cb.ble_ctr_cb.max_filter_entries;
    783     }
    784 
    785     btm_get_ble_buffer_size();
    786 }
    787 
    788 #endif
    789 /*******************************************************************************
    790 **
    791 ** Function         btm_read_local_version_complete
    792 **
    793 ** Description      This function is called when local BD Addr read complete
    794 **                  message is received from the HCI.
    795 **
    796 ** Returns          void
    797 **
    798 *******************************************************************************/
    799 void btm_read_local_version_complete (UINT8 *p, UINT16 evt_len)
    800 {
    801     tBTM_VERSION_INFO   *p_vi = &btm_cb.devcb.local_version;
    802     UINT8                status;
    803 
    804 #ifdef BTA_PRM_CHECK_FW_VER
    805     if(BTA_PRM_CHECK_FW_VER(p))
    806         return;
    807 #endif
    808 
    809     STREAM_TO_UINT8  (status, p);
    810     if (status == HCI_SUCCESS)
    811     {
    812 
    813         STREAM_TO_UINT8  (p_vi->hci_version, p);
    814         STREAM_TO_UINT16 (p_vi->hci_revision, p);
    815         STREAM_TO_UINT8  (p_vi->lmp_version, p);
    816         STREAM_TO_UINT16 (p_vi->manufacturer, p);
    817         STREAM_TO_UINT16 (p_vi->lmp_subversion, p);
    818     }
    819 
    820 #if BLE_INCLUDED == TRUE
    821     {
    822         btm_read_ble_wl_size();
    823     }
    824 #elif BTM_INTERNAL_BB == TRUE
    825     {
    826         UINT8 buf[9] = BTM_INTERNAL_LOCAL_FEA;
    827         btm_read_local_features_complete( buf, 9 );
    828     }
    829 #else
    830 #ifdef BRCM_VS
    831     btm_brcm_feat_init();
    832 #else
    833     /* get local feature if BRCM specific feature is not included  */
    834     btm_get_local_features ();
    835 #endif
    836 #endif
    837 }
    838 
    839 
    840 /*******************************************************************************
    841 **
    842 ** Function         btm_read_local_features_complete
    843 **
    844 ** Description      This function is called when local features read is complete.
    845 **                  This is the last step of the startup sequence.
    846 **
    847 ** Returns          void
    848 **
    849 *******************************************************************************/
    850 void btm_read_local_features_complete (UINT8 *p, UINT16 evt_len)
    851 {
    852     tBTM_DEVCB     *p_devcb = &btm_cb.devcb;
    853     tBTM_CMPL_CB   *p_cb = p_devcb->p_reset_cmpl_cb;
    854     UINT8           status;
    855     UINT16          xx;
    856     UINT8 last;
    857     UINT8 first;
    858 
    859     btu_stop_timer (&btm_cb.devcb.reset_timer);
    860     /* If there was a callback address for reset complete, call it */
    861     p_devcb->p_reset_cmpl_cb = NULL;
    862 
    863     STREAM_TO_UINT8  (status, p);
    864     if (status == HCI_SUCCESS)
    865     {
    866         /* stop guard timer to avoid accidental timeout */
    867         btu_stop_timer(&p_devcb->reset_timer);
    868 
    869         p_devcb->state = BTM_DEV_STATE_READY;
    870 
    871         /* Extract features and create "btm_acl_pkt_types_supported" flag
    872         */
    873         for (xx = 0; xx < HCI_NUM_FEATURE_BYTES; xx++)
    874             STREAM_TO_UINT8 (p_devcb->local_features[xx], p);
    875 
    876         /* Create ACL supported packet types mask
    877         */
    878         btm_cb.btm_acl_pkt_types_supported = (BTM_ACL_PKT_TYPES_MASK_DH1 +
    879                                               BTM_ACL_PKT_TYPES_MASK_DM1);
    880 
    881         if (HCI_3_SLOT_PACKETS_SUPPORTED(p_devcb->local_features))
    882             btm_cb.btm_acl_pkt_types_supported |= (BTM_ACL_PKT_TYPES_MASK_DH3 +
    883                                                    BTM_ACL_PKT_TYPES_MASK_DM3);
    884 
    885         if (HCI_5_SLOT_PACKETS_SUPPORTED(p_devcb->local_features))
    886             btm_cb.btm_acl_pkt_types_supported |= (BTM_ACL_PKT_TYPES_MASK_DH5 +
    887                                                    BTM_ACL_PKT_TYPES_MASK_DM5);
    888 
    889         /* _NO_X_DXX masks are reserved before ver 2.0.
    890            Set them only for later versions of controller */
    891         if (btm_cb.devcb.local_version.hci_version >= HCI_PROTO_VERSION_2_0)
    892         {
    893 
    894             /* Add in EDR related ACL types */
    895             if (!HCI_EDR_ACL_2MPS_SUPPORTED(p_devcb->local_features))
    896             {
    897                 btm_cb.btm_acl_pkt_types_supported |= (BTM_ACL_PKT_TYPES_MASK_NO_2_DH1 +
    898                                                        BTM_ACL_PKT_TYPES_MASK_NO_2_DH3 +
    899                                                        BTM_ACL_PKT_TYPES_MASK_NO_2_DH5);
    900             }
    901 
    902             if (!HCI_EDR_ACL_3MPS_SUPPORTED(p_devcb->local_features))
    903             {
    904                 btm_cb.btm_acl_pkt_types_supported |= (BTM_ACL_PKT_TYPES_MASK_NO_3_DH1 +
    905                                                        BTM_ACL_PKT_TYPES_MASK_NO_3_DH3 +
    906                                                        BTM_ACL_PKT_TYPES_MASK_NO_3_DH5);
    907             }
    908 
    909             /* Check to see if 3 and 5 slot packets are available */
    910             if (HCI_EDR_ACL_2MPS_SUPPORTED(p_devcb->local_features) ||
    911                 HCI_EDR_ACL_3MPS_SUPPORTED(p_devcb->local_features))
    912             {
    913                 if (!HCI_3_SLOT_EDR_ACL_SUPPORTED(p_devcb->local_features))
    914                     btm_cb.btm_acl_pkt_types_supported |= (BTM_ACL_PKT_TYPES_MASK_NO_2_DH3 +
    915                                                            BTM_ACL_PKT_TYPES_MASK_NO_3_DH3);
    916 
    917                 if (!HCI_5_SLOT_EDR_ACL_SUPPORTED(p_devcb->local_features))
    918                     btm_cb.btm_acl_pkt_types_supported |= (BTM_ACL_PKT_TYPES_MASK_NO_2_DH5 +
    919                                                            BTM_ACL_PKT_TYPES_MASK_NO_3_DH5);
    920             }
    921         }
    922 
    923         BTM_TRACE_DEBUG1("Local supported ACL packet types: 0x%04x",
    924                          btm_cb.btm_acl_pkt_types_supported);
    925 
    926         /* Create (e)SCO supported packet types mask
    927         */
    928         btm_cb.btm_sco_pkt_types_supported = 0;
    929 #if BTM_SCO_INCLUDED == TRUE
    930         btm_cb.sco_cb.esco_supported = FALSE;
    931 #endif
    932         if (HCI_SCO_LINK_SUPPORTED(p_devcb->local_features))
    933         {
    934             btm_cb.btm_sco_pkt_types_supported = BTM_SCO_PKT_TYPES_MASK_HV1;
    935 
    936             if (HCI_HV2_PACKETS_SUPPORTED(p_devcb->local_features))
    937                 btm_cb.btm_sco_pkt_types_supported |= BTM_SCO_PKT_TYPES_MASK_HV2;
    938 
    939             if (HCI_HV3_PACKETS_SUPPORTED(p_devcb->local_features))
    940                 btm_cb.btm_sco_pkt_types_supported |= BTM_SCO_PKT_TYPES_MASK_HV3;
    941         }
    942 
    943         if (HCI_ESCO_EV3_SUPPORTED(p_devcb->local_features))
    944             btm_cb.btm_sco_pkt_types_supported |= BTM_SCO_PKT_TYPES_MASK_EV3;
    945 
    946         if (HCI_ESCO_EV4_SUPPORTED(p_devcb->local_features))
    947             btm_cb.btm_sco_pkt_types_supported |= BTM_SCO_PKT_TYPES_MASK_EV4;
    948 
    949         if (HCI_ESCO_EV5_SUPPORTED(p_devcb->local_features))
    950             btm_cb.btm_sco_pkt_types_supported |= BTM_SCO_PKT_TYPES_MASK_EV5;
    951 #if BTM_SCO_INCLUDED == TRUE
    952         if (btm_cb.btm_sco_pkt_types_supported & BTM_ESCO_LINK_ONLY_MASK)
    953         {
    954             btm_cb.sco_cb.esco_supported = TRUE;
    955 
    956             /* Add in EDR related eSCO types */
    957             if (HCI_EDR_ESCO_2MPS_SUPPORTED(p_devcb->local_features))
    958             {
    959                 if (!HCI_3_SLOT_EDR_ESCO_SUPPORTED(p_devcb->local_features))
    960                     btm_cb.btm_sco_pkt_types_supported |= BTM_SCO_PKT_TYPES_MASK_NO_2_EV5;
    961             }
    962             else
    963             {
    964                 btm_cb.btm_sco_pkt_types_supported |= (BTM_SCO_PKT_TYPES_MASK_NO_2_EV3 +
    965                                                        BTM_SCO_PKT_TYPES_MASK_NO_2_EV5);
    966             }
    967 
    968             if (HCI_EDR_ESCO_3MPS_SUPPORTED(p_devcb->local_features))
    969             {
    970                 if (!HCI_3_SLOT_EDR_ESCO_SUPPORTED(p_devcb->local_features))
    971                     btm_cb.btm_sco_pkt_types_supported |= BTM_SCO_PKT_TYPES_MASK_NO_3_EV5;
    972             }
    973             else
    974             {
    975                 btm_cb.btm_sco_pkt_types_supported |= (BTM_SCO_PKT_TYPES_MASK_NO_3_EV3 +
    976                                                        BTM_SCO_PKT_TYPES_MASK_NO_3_EV5);
    977             }
    978         }
    979 #endif
    980 
    981         BTM_TRACE_DEBUG1("Local supported SCO packet types: 0x%04x",
    982                          btm_cb.btm_sco_pkt_types_supported);
    983 
    984         /* Create Default Policy Settings
    985         */
    986         if (HCI_SWITCH_SUPPORTED(p_devcb->local_features))
    987             btm_cb.btm_def_link_policy |= HCI_ENABLE_MASTER_SLAVE_SWITCH;
    988         else
    989             btm_cb.btm_def_link_policy &= ~HCI_ENABLE_MASTER_SLAVE_SWITCH;
    990 
    991         if (HCI_HOLD_MODE_SUPPORTED(p_devcb->local_features))
    992             btm_cb.btm_def_link_policy |= HCI_ENABLE_HOLD_MODE;
    993         else
    994             btm_cb.btm_def_link_policy &= ~HCI_ENABLE_HOLD_MODE;
    995 
    996         if (HCI_SNIFF_MODE_SUPPORTED(p_devcb->local_features))
    997             btm_cb.btm_def_link_policy |= HCI_ENABLE_SNIFF_MODE;
    998         else
    999             btm_cb.btm_def_link_policy &= ~HCI_ENABLE_SNIFF_MODE;
   1000 
   1001         if (HCI_PARK_MODE_SUPPORTED(p_devcb->local_features))
   1002             btm_cb.btm_def_link_policy |= HCI_ENABLE_PARK_MODE;
   1003         else
   1004             btm_cb.btm_def_link_policy &= ~HCI_ENABLE_PARK_MODE;
   1005 
   1006         btm_sec_dev_reset ();
   1007 
   1008         /* If 802.11 present might have to disable some channels */
   1009         if (btm_cb.last_disabled_channel != 0xff)
   1010         {
   1011             last  = btm_cb.last_disabled_channel;
   1012             first = btm_cb.first_disabled_channel;
   1013             btm_cb.last_disabled_channel  = 0xff;
   1014             btm_cb.first_disabled_channel = 0xff;
   1015             BTM_SetAfhChannels(first, last);
   1016         }
   1017 
   1018 #if ((BTM_EIR_SERVER_INCLUDED == TRUE)||(BTM_EIR_CLIENT_INCLUDED == TRUE))
   1019         if (HCI_LMP_INQ_RSSI_SUPPORTED(p_devcb->local_features))
   1020         {
   1021             if (HCI_EXT_INQ_RSP_SUPPORTED(p_devcb->local_features))
   1022                 BTM_SetInquiryMode (BTM_INQ_RESULT_EXTENDED);
   1023             else
   1024                 BTM_SetInquiryMode (BTM_INQ_RESULT_WITH_RSSI);
   1025         }
   1026 #else
   1027         if (HCI_LMP_INQ_RSSI_SUPPORTED(p_devcb->local_features))
   1028             BTM_SetInquiryMode (BTM_INQ_RESULT_WITH_RSSI);
   1029 #endif
   1030 #if L2CAP_NON_FLUSHABLE_PB_INCLUDED == TRUE
   1031         if( HCI_NON_FLUSHABLE_PB_SUPPORTED(p_devcb->local_features))
   1032             l2cu_set_non_flushable_pbf(TRUE);
   1033         else
   1034             l2cu_set_non_flushable_pbf(FALSE);
   1035 #endif
   1036         BTM_SetPageScanType (BTM_DEFAULT_SCAN_TYPE);
   1037         BTM_SetInquiryScanType (BTM_DEFAULT_SCAN_TYPE);
   1038 
   1039         /* If anyone wants device status notifications, give him one */
   1040         btm_report_device_status (BTM_DEV_STATUS_UP);
   1041 
   1042 #ifdef BRCM_VS
   1043     btm_brcm_arc_init();
   1044 #endif
   1045 
   1046         /* Reset sequence is complete. If this was an application originated */
   1047         /* reset, tell him its done.                                         */
   1048         if (p_cb)
   1049             (*p_cb)((void *) NULL);
   1050     }
   1051 }
   1052 
   1053 /*******************************************************************************
   1054 **
   1055 ** Function         btm_get_voice_coding_support
   1056 **
   1057 ** Description      This function is provides a way to get the voice coding schemes
   1058 **                  supported the device.
   1059 **
   1060 ** Returns          A bit mask - Bit 0 if set indicates CVSD support
   1061 **                               Bit 1 if set indicates PCM A-law support
   1062 **                               Bit 2 if set indicates PCM Mu-law support
   1063 **
   1064 *******************************************************************************/
   1065 
   1066 UINT8 btm_get_voice_coding_support( void )
   1067 {
   1068     UINT8 code = 0;
   1069 
   1070     if( HCI_LMP_CVSD_SUPPORTED(btm_cb.devcb.local_features) ) code |= 0x01 ;
   1071     if( HCI_LMP_A_LAW_SUPPORTED(btm_cb.devcb.local_features) ) code |= 0x02 ;
   1072     if( HCI_LMP_U_LAW_SUPPORTED(btm_cb.devcb.local_features) )  code |= 0x04 ;
   1073 
   1074     return code ;
   1075 }
   1076 
   1077 /*******************************************************************************
   1078 **
   1079 ** Function         BTM_SetLocalDeviceName
   1080 **
   1081 ** Description      This function is called to set the local device name.
   1082 **
   1083 ** Returns          status of the operation
   1084 **
   1085 *******************************************************************************/
   1086 tBTM_STATUS BTM_SetLocalDeviceName (char *p_name)
   1087 {
   1088     UINT8    *p;
   1089 #if BLE_INCLUDED == TRUE && GAP_INCLUDED == TRUE
   1090     tGAP_BLE_ATTR_VALUE     attr_value;
   1091 #endif
   1092 
   1093     if (!p_name || !p_name[0] || (strlen ((char *)p_name) > BD_NAME_LEN))
   1094         return (BTM_ILLEGAL_VALUE);
   1095 
   1096     if (btm_cb.devcb.state == BTM_DEV_STATE_WAIT_RESET_CMPLT ||
   1097         btm_cb.devcb.state == BTM_DEV_STATE_WAIT_AFTER_RESET)
   1098         return (BTM_DEV_RESET);
   1099 
   1100 #if BTM_MAX_LOC_BD_NAME_LEN > 0
   1101     /* Save the device name if local storage is enabled */
   1102     p = (UINT8 *)btm_cb.cfg.bd_name;
   1103     if (p != (UINT8 *)p_name)
   1104     {
   1105         BCM_STRNCPY_S(btm_cb.cfg.bd_name, sizeof(btm_cb.cfg.bd_name), p_name, BTM_MAX_LOC_BD_NAME_LEN);
   1106         btm_cb.cfg.bd_name[BTM_MAX_LOC_BD_NAME_LEN] = '\0';
   1107     }
   1108 #else
   1109     p = (UINT8 *)p_name;
   1110 #endif
   1111 
   1112 #if BLE_INCLUDED == TRUE && GAP_INCLUDED == TRUE
   1113     attr_value.p_dev_name = (UINT8 *)p_name;
   1114     GAP_BleAttrDBUpdate(GATT_UUID_GAP_DEVICE_NAME, &attr_value);
   1115 #endif
   1116 
   1117     if (btsnd_hcic_change_name(p))
   1118         return (BTM_CMD_STARTED);
   1119     else
   1120         return (BTM_NO_RESOURCES);
   1121 }
   1122 
   1123 
   1124 
   1125 /*******************************************************************************
   1126 **
   1127 ** Function         BTM_ReadLocalDeviceName
   1128 **
   1129 ** Description      This function is called to read the local device name.
   1130 **
   1131 ** Returns          status of the operation
   1132 **                  If success, BTM_SUCCESS is returned and p_name points stored
   1133 **                              local device name
   1134 **                  If BTM doesn't store local device name, BTM_NO_RESOURCES is
   1135 **                              is returned and p_name is set to NULL
   1136 **
   1137 *******************************************************************************/
   1138 tBTM_STATUS BTM_ReadLocalDeviceName (char **p_name)
   1139 {
   1140 #if BTM_MAX_LOC_BD_NAME_LEN > 0
   1141     *p_name = btm_cb.cfg.bd_name;
   1142     return(BTM_SUCCESS);
   1143 #else
   1144     *p_name = NULL;
   1145     return(BTM_NO_RESOURCES);
   1146 #endif
   1147 }
   1148 
   1149 
   1150 /*******************************************************************************
   1151 **
   1152 ** Function         BTM_ReadLocalDeviceNameFromController
   1153 **
   1154 ** Description      Get local device name from controller. Do not use cached
   1155 **                  name (used to get chip-id prior to btm reset complete).
   1156 **
   1157 ** Returns          BTM_CMD_STARTED if successful, otherwise an error
   1158 **
   1159 *******************************************************************************/
   1160 tBTM_STATUS BTM_ReadLocalDeviceNameFromController (tBTM_CMPL_CB *p_rln_cmpl_cback)
   1161 {
   1162     /* Check if rln already in progress */
   1163     if (btm_cb.devcb.p_rln_cmpl_cb)
   1164         return(BTM_NO_RESOURCES);
   1165 
   1166     /* Save callback */
   1167     btm_cb.devcb.p_rln_cmpl_cb = p_rln_cmpl_cback;
   1168 
   1169     btsnd_hcic_read_name();
   1170     btu_start_timer (&btm_cb.devcb.rln_timer, BTU_TTYPE_BTM_DEV_CTL, BTM_DEV_REPLY_TIMEOUT);
   1171 
   1172     return BTM_CMD_STARTED;
   1173 }
   1174 
   1175 /*******************************************************************************
   1176 **
   1177 ** Function         btm_read_local_name_complete
   1178 **
   1179 ** Description      This function is called when local name read complete.
   1180 **                  message is received from the HCI.
   1181 **
   1182 ** Returns          void
   1183 **
   1184 *******************************************************************************/
   1185 void btm_read_local_name_complete (UINT8 *p, UINT16 evt_len)
   1186 {
   1187     tBTM_CMPL_CB   *p_cb = btm_cb.devcb.p_rln_cmpl_cb;
   1188     UINT8           status;
   1189 
   1190     btu_stop_timer (&btm_cb.devcb.rln_timer);
   1191 
   1192     /* If there was a callback address for read local name, call it */
   1193     btm_cb.devcb.p_rln_cmpl_cb = NULL;
   1194 
   1195     if (p_cb)
   1196     {
   1197         STREAM_TO_UINT8  (status, p);
   1198 
   1199         if (status == HCI_SUCCESS)
   1200             (*p_cb)(p);
   1201         else
   1202             (*p_cb)(NULL);
   1203     }
   1204 }
   1205 
   1206 
   1207 /*******************************************************************************
   1208 **
   1209 ** Function         BTM_GetLocalDeviceAddr
   1210 **
   1211 ** Description      This function is called to read the local device address
   1212 **
   1213 ** Returns          void
   1214 **                  the local device address is copied into bd_addr
   1215 **
   1216 *******************************************************************************/
   1217 void BTM_GetLocalDeviceAddr (BD_ADDR bd_addr)
   1218 {
   1219     memcpy (bd_addr, btm_cb.devcb.local_addr, BD_ADDR_LEN);
   1220 }
   1221 
   1222 /*******************************************************************************
   1223 **
   1224 ** Function         BTM_ReadLocalDeviceAddr
   1225 **
   1226 ** Description      This function is called to read the local device address
   1227 **
   1228 ** Returns          status of the operation
   1229 **
   1230 *******************************************************************************/
   1231 tBTM_STATUS BTM_ReadLocalDeviceAddr (tBTM_CMPL_CB *p_cb)
   1232 {
   1233     if(p_cb)
   1234         (*p_cb)(btm_cb.devcb.local_addr);
   1235 
   1236     return (BTM_SUCCESS);
   1237 }
   1238 
   1239 
   1240 /*******************************************************************************
   1241 **
   1242 ** Function         btm_read_local_addr_complete
   1243 **
   1244 ** Description      This function is called when local BD Addr read complete
   1245 **                  message is received from the HCI.
   1246 **
   1247 ** Returns          void
   1248 **
   1249 *******************************************************************************/
   1250 void btm_read_local_addr_complete (UINT8 *p, UINT16 evt_len)
   1251 {
   1252     UINT8           status;
   1253 
   1254     STREAM_TO_UINT8  (status, p);
   1255 
   1256     if (status == HCI_SUCCESS)
   1257     {
   1258         STREAM_TO_BDADDR (btm_cb.devcb.local_addr, p);
   1259     }
   1260 }
   1261 
   1262 
   1263 /*******************************************************************************
   1264 **
   1265 ** Function         BTM_ReadLocalVersion
   1266 **
   1267 ** Description      This function is called to read the local device version
   1268 **
   1269 ** Returns          status of the operation
   1270 **
   1271 *******************************************************************************/
   1272 tBTM_STATUS BTM_ReadLocalVersion (tBTM_VERSION_INFO *p_vers)
   1273 {
   1274     /* Make sure the device has retrieved the info (not being reset) */
   1275     if (btm_cb.devcb.state < BTM_DEV_STATE_READY)
   1276         return (BTM_DEV_RESET);
   1277 
   1278     *p_vers = btm_cb.devcb.local_version;
   1279 
   1280     return (BTM_SUCCESS);
   1281 }
   1282 
   1283 
   1284 
   1285 
   1286 /*******************************************************************************
   1287 **
   1288 ** Function         BTM_SetDeviceClass
   1289 **
   1290 ** Description      This function is called to set the local device class
   1291 **
   1292 ** Returns          status of the operation
   1293 **
   1294 *******************************************************************************/
   1295 tBTM_STATUS BTM_SetDeviceClass (DEV_CLASS dev_class)
   1296 {
   1297     if(!memcmp (btm_cb.devcb.dev_class, dev_class, DEV_CLASS_LEN))
   1298         return(BTM_SUCCESS);
   1299 
   1300     memcpy (btm_cb.devcb.dev_class, dev_class, DEV_CLASS_LEN);
   1301 
   1302     if (btm_cb.devcb.state == BTM_DEV_STATE_WAIT_RESET_CMPLT ||
   1303         btm_cb.devcb.state == BTM_DEV_STATE_WAIT_AFTER_RESET)
   1304         return (BTM_DEV_RESET);
   1305 
   1306     if (!btsnd_hcic_write_dev_class (dev_class))
   1307         return (BTM_NO_RESOURCES);
   1308 
   1309     return (BTM_SUCCESS);
   1310 }
   1311 
   1312 
   1313 /*******************************************************************************
   1314 **
   1315 ** Function         BTM_ReadDeviceClass
   1316 **
   1317 ** Description      This function is called to read the local device class
   1318 **
   1319 ** Returns          pointer to the device class
   1320 **
   1321 *******************************************************************************/
   1322 UINT8 *BTM_ReadDeviceClass (void)
   1323 {
   1324     return ((UINT8 *)btm_cb.devcb.dev_class);
   1325 }
   1326 
   1327 
   1328 /*******************************************************************************
   1329 **
   1330 ** Function         BTM_ReadLocalFeatures
   1331 **
   1332 ** Description      This function is called to read the local features
   1333 **
   1334 ** Returns          pointer to the local features string
   1335 **
   1336 *******************************************************************************/
   1337 UINT8 *BTM_ReadLocalFeatures (void)
   1338 {
   1339     return (btm_cb.devcb.local_features);
   1340 }
   1341 
   1342 /*******************************************************************************
   1343 **
   1344 ** Function         BTM_ReadBrcmFeatures
   1345 **
   1346 ** Description      This function is called to read the Broadcom specific features
   1347 **
   1348 ** Returns          pointer to the Broadcom features string
   1349 **
   1350 *******************************************************************************/
   1351 UINT8 *BTM_ReadBrcmFeatures (void)
   1352 {
   1353     return (btm_cb.devcb.brcm_features);
   1354 }
   1355 
   1356 /*******************************************************************************
   1357 **
   1358 ** Function         BTM_RegisterForDeviceStatusNotif
   1359 **
   1360 ** Description      This function is called to register for device status
   1361 **                  change notifications.
   1362 **
   1363 **                  If one registration is already there calling function should
   1364 **                  save the pointer to the function that is return and
   1365 **                  call it when processing of the event is complete
   1366 **
   1367 ** Returns          status of the operation
   1368 **
   1369 *******************************************************************************/
   1370 tBTM_DEV_STATUS_CB *BTM_RegisterForDeviceStatusNotif (tBTM_DEV_STATUS_CB *p_cb)
   1371 {
   1372     tBTM_DEV_STATUS_CB *p_prev = btm_cb.devcb.p_dev_status_cb;
   1373 
   1374     btm_cb.devcb.p_dev_status_cb = p_cb;
   1375     return (p_prev);
   1376 }
   1377 
   1378 /*******************************************************************************
   1379 **
   1380 ** Function         BTM_VendorSpecificCommand
   1381 **
   1382 ** Description      Send a vendor specific HCI command to the controller.
   1383 **
   1384 ** Returns
   1385 **      BTM_SUCCESS         Command sent. Does not expect command complete
   1386 **                              event. (command cmpl callback param is NULL)
   1387 **      BTM_CMD_STARTED     Command sent. Waiting for command cmpl event.
   1388 **
   1389 ** Notes
   1390 **      Opcode will be OR'd with HCI_GRP_VENDOR_SPECIFIC.
   1391 **
   1392 *******************************************************************************/
   1393 tBTM_STATUS BTM_VendorSpecificCommand(UINT16 opcode, UINT8 param_len,
   1394                                       UINT8 *p_param_buf, tBTM_VSC_CMPL_CB *p_cb)
   1395 {
   1396     void *p_buf;
   1397 
   1398     BTM_TRACE_EVENT2 ("BTM: BTM_VendorSpecificCommand: Opcode: 0x%04X, ParamLen: %i.",
   1399                       opcode, param_len);
   1400 
   1401     /* Allocate a buffer to hold HCI command plus the callback function */
   1402     if ((p_buf = GKI_getbuf((UINT16)(sizeof(BT_HDR) + sizeof (tBTM_CMPL_CB *) +
   1403                             param_len + HCIC_PREAMBLE_SIZE))) != NULL)
   1404     {
   1405         /* Send the HCI command (opcode will be OR'd with HCI_GRP_VENDOR_SPECIFIC) */
   1406         btsnd_hcic_vendor_spec_cmd (p_buf, opcode, param_len, p_param_buf, (void *)p_cb);
   1407 
   1408         /* Return value */
   1409         if (p_cb != NULL)
   1410             return (BTM_CMD_STARTED);
   1411         else
   1412             return (BTM_SUCCESS);
   1413     }
   1414     else
   1415         return (BTM_NO_RESOURCES);
   1416 
   1417 }
   1418 
   1419 
   1420 /*******************************************************************************
   1421 **
   1422 ** Function         btm_vsc_complete
   1423 **
   1424 ** Description      This function is called when local HCI Vendor Specific
   1425 **                  Command complete message is received from the HCI.
   1426 **
   1427 ** Returns          void
   1428 **
   1429 *******************************************************************************/
   1430 void btm_vsc_complete (UINT8 *p, UINT16 opcode, UINT16 evt_len,
   1431                        tBTM_CMPL_CB *p_vsc_cplt_cback)
   1432 {
   1433     tBTM_VSC_CMPL   vcs_cplt_params;
   1434 
   1435     /* If there was a callback address for vcs complete, call it */
   1436     if (p_vsc_cplt_cback)
   1437     {
   1438         /* Pass paramters to the callback function */
   1439         vcs_cplt_params.opcode = opcode;        /* Number of bytes in return info */
   1440         vcs_cplt_params.param_len = evt_len;    /* Number of bytes in return info */
   1441         vcs_cplt_params.p_param_buf = p;
   1442         (*p_vsc_cplt_cback)(&vcs_cplt_params);  /* Call the VSC complete callback function */
   1443     }
   1444 }
   1445 
   1446 /*******************************************************************************
   1447 **
   1448 ** Function         BTM_RegisterForVSEvents
   1449 **
   1450 ** Description      This function is called to register/deregister for vendor
   1451 **                  specific HCI events.
   1452 **
   1453 **                  If is_register=TRUE, then the function will be registered;
   1454 **                  if is_register=FALSE, then the function will be deregistered.
   1455 **
   1456 ** Returns          BTM_SUCCESS if successful,
   1457 **                  BTM_BUSY if maximum number of callbacks have already been
   1458 **                           registered.
   1459 **
   1460 *******************************************************************************/
   1461 tBTM_STATUS BTM_RegisterForVSEvents (tBTM_VS_EVT_CB *p_cb, BOOLEAN is_register)
   1462 {
   1463     tBTM_STATUS retval = BTM_SUCCESS;
   1464     UINT8 i, free_idx = BTM_MAX_VSE_CALLBACKS;
   1465 
   1466     /* See if callback is already registered */
   1467     for (i=0; i<BTM_MAX_VSE_CALLBACKS; i++)
   1468     {
   1469         if (btm_cb.devcb.p_vend_spec_cb[i] == NULL)
   1470         {
   1471             /* Found a free slot. Store index */
   1472             free_idx = i;
   1473         }
   1474         else if (btm_cb.devcb.p_vend_spec_cb[i] == p_cb)
   1475         {
   1476             /* Found callback in lookup table. If deregistering, clear the entry. */
   1477             if (is_register == FALSE)
   1478             {
   1479                 btm_cb.devcb.p_vend_spec_cb[i] = NULL;
   1480                 BTM_TRACE_EVENT0("BTM Deregister For VSEvents is successfully");
   1481             }
   1482             return (BTM_SUCCESS);
   1483         }
   1484     }
   1485 
   1486     /* Didn't find callback. Add callback to free slot if registering */
   1487     if (is_register)
   1488     {
   1489         if (free_idx < BTM_MAX_VSE_CALLBACKS)
   1490         {
   1491             btm_cb.devcb.p_vend_spec_cb[free_idx] = p_cb;
   1492             BTM_TRACE_EVENT0("BTM Register For VSEvents is successfully");
   1493         }
   1494         else
   1495         {
   1496             /* No free entries available */
   1497             BTM_TRACE_ERROR0 ("BTM_RegisterForVSEvents: too many callbacks registered");
   1498 
   1499             retval = BTM_NO_RESOURCES;
   1500         }
   1501     }
   1502 
   1503     return (retval);
   1504 }
   1505 
   1506 /*******************************************************************************
   1507 **
   1508 ** Function         btm_vendor_specific_evt
   1509 **
   1510 ** Description      Process event HCI_VENDOR_SPECIFIC_EVT
   1511 **
   1512 **                  Note: Some controllers do not send command complete, so
   1513 **                  the callback and busy flag are cleared here also.
   1514 **
   1515 ** Returns          void
   1516 **
   1517 *******************************************************************************/
   1518 void btm_vendor_specific_evt (UINT8 *p, UINT8 evt_len)
   1519 {
   1520     UINT8 i;
   1521 
   1522     BTM_TRACE_DEBUG0 ("BTM Event: Vendor Specific event from controller");
   1523 
   1524     for (i=0; i<BTM_MAX_VSE_CALLBACKS; i++)
   1525     {
   1526         if (btm_cb.devcb.p_vend_spec_cb[i])
   1527             (*btm_cb.devcb.p_vend_spec_cb[i])(evt_len, p);
   1528     }
   1529 }
   1530 
   1531 
   1532 /*******************************************************************************
   1533 **
   1534 ** Function         BTM_WritePageTimeout
   1535 **
   1536 ** Description      Send HCI Write Page Timeout.
   1537 **
   1538 ** Returns
   1539 **      BTM_SUCCESS         Command sent.
   1540 **      BTM_NO_RESOURCES     If out of resources to send the command.
   1541 **
   1542 **
   1543 *******************************************************************************/
   1544 tBTM_STATUS BTM_WritePageTimeout(UINT16 timeout)
   1545 {
   1546     BTM_TRACE_EVENT1 ("BTM: BTM_WritePageTimeout: Timeout: %d.", timeout);
   1547 
   1548     /* Send the HCI command */
   1549     if (btsnd_hcic_write_page_tout (timeout))
   1550         return (BTM_SUCCESS);
   1551     else
   1552         return (BTM_NO_RESOURCES);
   1553 }
   1554 
   1555 /*******************************************************************************
   1556 **
   1557 ** Function         BTM_WriteVoiceSettings
   1558 **
   1559 ** Description      Send HCI Write Voice Settings command.
   1560 **                  See hcidefs.h for settings bitmask values.
   1561 **
   1562 ** Returns
   1563 **      BTM_SUCCESS         Command sent.
   1564 **      BTM_NO_RESOURCES     If out of resources to send the command.
   1565 **
   1566 **
   1567 *******************************************************************************/
   1568 tBTM_STATUS BTM_WriteVoiceSettings(UINT16 settings)
   1569 {
   1570     BTM_TRACE_EVENT1 ("BTM: BTM_WriteVoiceSettings: Settings: 0x%04x.", settings);
   1571 
   1572     /* Send the HCI command */
   1573     if (btsnd_hcic_write_voice_settings ((UINT16)(settings & 0x03ff)))
   1574         return (BTM_SUCCESS);
   1575 
   1576     return (BTM_NO_RESOURCES);
   1577 }
   1578 
   1579 /*******************************************************************************
   1580 **
   1581 ** Function         BTM_EnableTestMode
   1582 **
   1583 ** Description      Send HCI the enable device under test command.
   1584 **
   1585 **                  Note: Controller can only be taken out of this mode by
   1586 **                      resetting the controller.
   1587 **
   1588 ** Returns
   1589 **      BTM_SUCCESS         Command sent.
   1590 **      BTM_NO_RESOURCES    If out of resources to send the command.
   1591 **
   1592 **
   1593 *******************************************************************************/
   1594 tBTM_STATUS BTM_EnableTestMode(void)
   1595 {
   1596     UINT8   cond;
   1597 
   1598     BTM_TRACE_EVENT0 ("BTM: BTM_EnableTestMode");
   1599 
   1600     /* set auto accept connection as this is needed during test mode */
   1601     /* Allocate a buffer to hold HCI command */
   1602     cond = HCI_DO_AUTO_ACCEPT_CONNECT;
   1603     if (!btsnd_hcic_set_event_filter(HCI_FILTER_CONNECTION_SETUP,
   1604                                      HCI_FILTER_COND_NEW_DEVICE,
   1605                                      &cond, sizeof(cond)))
   1606     {
   1607         return (BTM_NO_RESOURCES);
   1608     }
   1609 
   1610     /* put device to connectable mode */
   1611     if (!BTM_SetConnectability(BTM_CONNECTABLE, BTM_DEFAULT_CONN_WINDOW,
   1612                                BTM_DEFAULT_CONN_INTERVAL) == BTM_SUCCESS)
   1613     {
   1614         return BTM_NO_RESOURCES;
   1615     }
   1616 
   1617     /* put device to discoverable mode */
   1618     if (!BTM_SetDiscoverability(BTM_GENERAL_DISCOVERABLE, BTM_DEFAULT_DISC_WINDOW,
   1619                                 BTM_DEFAULT_DISC_INTERVAL) == BTM_SUCCESS)
   1620     {
   1621         return BTM_NO_RESOURCES;
   1622     }
   1623 
   1624     /* mask off all of event from controller */
   1625     if (!btsnd_hcic_set_event_mask(LOCAL_BR_EDR_CONTROLLER_ID,
   1626                                    (UINT8 *)"\x00\x00\x00\x00\x00\x00\x00\x00"))
   1627     {
   1628         return BTM_NO_RESOURCES;
   1629     }
   1630 
   1631     /* Send the HCI command */
   1632     if (btsnd_hcic_enable_test_mode ())
   1633         return (BTM_SUCCESS);
   1634     else
   1635         return (BTM_NO_RESOURCES);
   1636 }
   1637 
   1638 /*******************************************************************************
   1639 **
   1640 ** Function         btm_get_hci_version
   1641 **
   1642 ** Description      Local function called to retrieve the current HCI version
   1643 **
   1644 ** Returns          Bluetooth HCI Version returned by the controller
   1645 **
   1646 *******************************************************************************/
   1647 UINT8 btm_get_hci_version (void)
   1648 {
   1649     return (btm_cb.devcb.local_version.hci_version);
   1650 }
   1651 
   1652 
   1653 
   1654 /*******************************************************************************
   1655 **
   1656 ** Function         BTM_ReadStoredLinkKey
   1657 **
   1658 ** Description      This function is called to obtain link key for the specified
   1659 **                  device from the NVRAM storage attached to the Bluetooth
   1660 **                  controller.
   1661 **
   1662 ** Parameters:      bd_addr      - Address of the device
   1663 **                  p_cb         - Call back function to be called to return
   1664 **                                 the results
   1665 **
   1666 *******************************************************************************/
   1667 tBTM_STATUS BTM_ReadStoredLinkKey (BD_ADDR bd_addr,	tBTM_CMPL_CB *p_cb)
   1668 {
   1669     BD_ADDR local_bd_addr;
   1670     BOOLEAN read_all_flag = FALSE;
   1671 
   1672     /* Check if the previous command is completed */
   1673     if (btm_cb.devcb.p_stored_link_key_cmpl_cb)
   1674         return (BTM_BUSY);
   1675 
   1676     if (!bd_addr)
   1677     {
   1678         /* This is to read all the link keys */
   1679         read_all_flag = TRUE;
   1680 
   1681         /* We don't care the BD address. Just pass a non zero pointer */
   1682         bd_addr = local_bd_addr;
   1683     }
   1684 
   1685     BTM_TRACE_EVENT1 ("BTM: BTM_ReadStoredLinkKey: Read_All: %s",
   1686                         read_all_flag ? "TRUE" : "FALSE");
   1687 
   1688     /* Send the HCI command */
   1689     btm_cb.devcb.p_stored_link_key_cmpl_cb = p_cb;
   1690     if (btsnd_hcic_read_stored_key (bd_addr, read_all_flag))
   1691         return (BTM_SUCCESS);
   1692     else
   1693         return (BTM_NO_RESOURCES);
   1694 
   1695 }
   1696 
   1697 
   1698 /*******************************************************************************
   1699 **
   1700 ** Function         BTM_WriteStoredLinkKey
   1701 **
   1702 ** Description      This function is called to write link keys for the specified
   1703 **                  device addresses to the NVRAM storage attached to the Bluetooth
   1704 **                  controller.
   1705 **
   1706 ** Parameters:      num_keys     - Number of link keys
   1707 **                  bd_addr      - Addresses of the devices
   1708 **                  link_key     - Link Keys to be stored
   1709 **                  p_cb         - Call back function to be called to return
   1710 **                                 the results
   1711 **
   1712 *******************************************************************************/
   1713 tBTM_STATUS BTM_WriteStoredLinkKey (UINT8 num_keys,
   1714                                     BD_ADDR *bd_addr,
   1715                                     LINK_KEY *link_key,
   1716                                     tBTM_CMPL_CB *p_cb)
   1717 {
   1718     /* Check if the previous command is completed */
   1719     if (btm_cb.devcb.p_stored_link_key_cmpl_cb)
   1720         return (BTM_BUSY);
   1721 
   1722     BTM_TRACE_EVENT1 ("BTM: BTM_WriteStoredLinkKey: num_keys: %d", num_keys);
   1723 
   1724     /* Check the maximum number of link keys */
   1725     if(num_keys > HCI_MAX_NUM_OF_LINK_KEYS_PER_CMMD)
   1726         num_keys = HCI_MAX_NUM_OF_LINK_KEYS_PER_CMMD;
   1727 
   1728     /* Send the HCI command */
   1729     btm_cb.devcb.p_stored_link_key_cmpl_cb = p_cb;
   1730     if (btsnd_hcic_write_stored_key (num_keys, bd_addr, link_key))
   1731         return (BTM_SUCCESS);
   1732     else
   1733         return (BTM_NO_RESOURCES);
   1734 
   1735 }
   1736 
   1737 
   1738 /*******************************************************************************
   1739 **
   1740 ** Function         BTM_DeleteStoredLinkKey
   1741 **
   1742 ** Description      This function is called to delete link key for the specified
   1743 **                  device addresses from the NVRAM storage attached to the Bluetooth
   1744 **                  controller.
   1745 **
   1746 ** Parameters:      bd_addr      - Addresses of the devices
   1747 **                  p_cb         - Call back function to be called to return
   1748 **                                 the results
   1749 **
   1750 *******************************************************************************/
   1751 tBTM_STATUS BTM_DeleteStoredLinkKey(BD_ADDR bd_addr, tBTM_CMPL_CB *p_cb)
   1752 {
   1753     BD_ADDR local_bd_addr;
   1754     BOOLEAN delete_all_flag = FALSE;
   1755 
   1756     /* Check if the previous command is completed */
   1757     if (btm_cb.devcb.p_stored_link_key_cmpl_cb)
   1758         return (BTM_BUSY);
   1759 
   1760     if (!bd_addr)
   1761     {
   1762         /* This is to delete all link keys */
   1763         delete_all_flag = TRUE;
   1764 
   1765         /* We don't care the BD address. Just pass a non zero pointer */
   1766         bd_addr = local_bd_addr;
   1767     }
   1768 
   1769     BTM_TRACE_EVENT1 ("BTM: BTM_DeleteStoredLinkKey: delete_all_flag: %s",
   1770                         delete_all_flag ? "TRUE" : "FALSE");
   1771 
   1772     /* Send the HCI command */
   1773     btm_cb.devcb.p_stored_link_key_cmpl_cb = p_cb;
   1774     if (!btsnd_hcic_delete_stored_key (bd_addr, delete_all_flag))
   1775     {
   1776         return (BTM_NO_RESOURCES);
   1777     }
   1778     else
   1779         return (BTM_SUCCESS);
   1780 }
   1781 
   1782 
   1783 /*******************************************************************************
   1784 **
   1785 ** Function         btm_read_stored_link_key_complete
   1786 **
   1787 ** Description      This function is called when the command complete message
   1788 **                  is received from the HCI for the read stored link key command.
   1789 **
   1790 ** Returns          void
   1791 **
   1792 *******************************************************************************/
   1793 void btm_read_stored_link_key_complete (UINT8 *p)
   1794 {
   1795     tBTM_CMPL_CB      *p_cb = btm_cb.devcb.p_stored_link_key_cmpl_cb;
   1796     tBTM_READ_STORED_LINK_KEY_COMPLETE  result;
   1797 
   1798     /* If there was a callback registered for read stored link key, call it */
   1799     btm_cb.devcb.p_stored_link_key_cmpl_cb = NULL;
   1800 
   1801     if (p_cb)
   1802     {
   1803         /* Set the call back event to indicate command complete */
   1804         result.event = BTM_CB_EVT_READ_STORED_LINK_KEYS;
   1805 
   1806         /* Extract the result fields from the HCI event if status is success */
   1807         STREAM_TO_UINT8  (result.status, p);
   1808         if (result.status == HCI_SUCCESS)
   1809         {
   1810             STREAM_TO_UINT16 (result.max_keys, p);
   1811             STREAM_TO_UINT16 (result.read_keys, p);
   1812         }
   1813         else
   1814         {
   1815             BTM_TRACE_WARNING1("Read stored link key status %d", result.status);
   1816             result.max_keys = 0;
   1817             result.read_keys = 0;
   1818         }
   1819         /* Call the call back and pass the result */
   1820         (*p_cb)(&result);
   1821     }
   1822 }
   1823 
   1824 
   1825 /*******************************************************************************
   1826 **
   1827 ** Function         btm_write_stored_link_key_complete
   1828 **
   1829 ** Description      This function is called when the command complete message
   1830 **                  is received from the HCI for the write stored link key command.
   1831 **
   1832 ** Returns          void
   1833 **
   1834 *******************************************************************************/
   1835 void btm_write_stored_link_key_complete (UINT8 *p)
   1836 {
   1837     tBTM_CMPL_CB       *p_cb = btm_cb.devcb.p_stored_link_key_cmpl_cb;
   1838     tBTM_WRITE_STORED_LINK_KEY_COMPLETE  result;
   1839 
   1840     /* If there was a callback registered for read stored link key, call it */
   1841     btm_cb.devcb.p_stored_link_key_cmpl_cb = NULL;
   1842 
   1843     if (p_cb)
   1844     {
   1845         /* Set the call back event to indicate command complete */
   1846         result.event = BTM_CB_EVT_WRITE_STORED_LINK_KEYS;
   1847 
   1848         /* Extract the result fields from the HCI event */
   1849         STREAM_TO_UINT8 (result.status, p);
   1850         STREAM_TO_UINT8 (result.num_keys, p);
   1851 
   1852         /* Call the call back and pass the result */
   1853         (*p_cb)(&result);
   1854     }
   1855 }
   1856 
   1857 
   1858 /*******************************************************************************
   1859 **
   1860 ** Function         btm_delete_stored_link_key_complete
   1861 **
   1862 ** Description      This function is called when the command complete message
   1863 **                  is received from the HCI for the delete stored link key command.
   1864 **
   1865 ** Returns          void
   1866 **
   1867 *******************************************************************************/
   1868 void btm_delete_stored_link_key_complete (UINT8 *p)
   1869 {
   1870     tBTM_CMPL_CB         *p_cb = btm_cb.devcb.p_stored_link_key_cmpl_cb;
   1871     tBTM_DELETE_STORED_LINK_KEY_COMPLETE  result;
   1872 
   1873     /* If there was a callback registered for read stored link key, call it */
   1874     btm_cb.devcb.p_stored_link_key_cmpl_cb = NULL;
   1875 
   1876     if (p_cb)
   1877     {
   1878         /* Set the call back event to indicate command complete */
   1879         result.event = BTM_CB_EVT_DELETE_STORED_LINK_KEYS;
   1880 
   1881         /* Extract the result fields from the HCI event */
   1882         STREAM_TO_UINT8  (result.status, p);
   1883         STREAM_TO_UINT16 (result.num_keys, p);
   1884 
   1885         /* Call the call back and pass the result */
   1886         (*p_cb)(&result);
   1887     }
   1888 }
   1889 
   1890 
   1891 /*******************************************************************************
   1892 **
   1893 ** Function         btm_return_link_keys_evt
   1894 **
   1895 ** Description      This function is called when the return link keys event
   1896 **                  is received from the HCI for the read stored link key command.
   1897 **
   1898 ** Returns          void
   1899 **
   1900 *******************************************************************************/
   1901 void btm_return_link_keys_evt (tBTM_RETURN_LINK_KEYS_EVT *result)
   1902 {
   1903     tBTM_CMPL_CB  *p_cb = btm_cb.devcb.p_stored_link_key_cmpl_cb;
   1904     UINT8          i, *p, *p1;
   1905     UINT8          bd_addr[BD_ADDR_LEN];
   1906     UINT8          link_key[LINK_KEY_LEN];
   1907 
   1908     /* Call the call back to pass the link keys to application */
   1909     if (p_cb)
   1910     {
   1911         /* Change the BD addr and Link key in to big endian order */
   1912         p = (UINT8 *)(result + 1);
   1913         for (i=0; i<result->num_keys; i++)
   1914         {
   1915             /* Initialize the backup pointer */
   1916             p1 = p;
   1917 
   1918             /* Extract the BD Addr and Link Key */
   1919             REVERSE_STREAM_TO_ARRAY(bd_addr, p1, BD_ADDR_LEN);
   1920             REVERSE_STREAM_TO_ARRAY(link_key, p1, LINK_KEY_LEN);
   1921 
   1922             /* Write the BD Addr and Link Key back in big endian format */
   1923             ARRAY_TO_STREAM(p, bd_addr, BD_ADDR_LEN);
   1924             ARRAY_TO_STREAM(p, link_key, LINK_KEY_LEN);
   1925         }
   1926 
   1927         (*p_cb)(result);
   1928     }
   1929 }
   1930 
   1931 
   1932 
   1933 /*******************************************************************************
   1934 **
   1935 ** Function         btm_report_device_status
   1936 **
   1937 ** Description      This function is called when there is a change in the device
   1938 **                  status. This function will report the new device status to
   1939 **                  the application
   1940 **
   1941 ** Returns          void
   1942 **
   1943 *******************************************************************************/
   1944 void btm_report_device_status (tBTM_DEV_STATUS status)
   1945 {
   1946     tBTM_DEV_STATUS_CB *p_cb = btm_cb.devcb.p_dev_status_cb;
   1947 
   1948     /* Call the call back to pass the device status to application */
   1949     if (p_cb)
   1950         (*p_cb)(status);
   1951 }
   1952 
   1953 
   1954