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#endif /* BLE_INCLUDED */
41
42/* BTM_APP_DEV_INIT should be defined if additional controller initialization is
43**      needed by the application to be performed after the HCI reset
44*/
45#ifdef BTM_APP_DEV_INIT
46extern void BTM_APP_DEV_INIT(void);
47#endif
48
49#ifdef BTA_PRM_CHECK_FW_VER
50extern BOOLEAN BTA_PRM_CHECK_FW_VER(UINT8 *p);
51#endif
52
53#ifndef TT_DEV_RESET_MASK
54#define TT_DEV_RESET_MASK 0xff
55#endif
56
57/********************************************************************************/
58/*                 L O C A L    D A T A    D E F I N I T I O N S                */
59/********************************************************************************/
60
61/* The default class of device. */
62#ifndef BTM_INIT_CLASS_OF_DEVICE
63#define BTM_INIT_CLASS_OF_DEVICE    "\x00\x1F\x00"
64#endif
65
66#ifndef BTM_DEV_RESET_TIMEOUT
67#define BTM_DEV_RESET_TIMEOUT   4
68#endif
69
70#define BTM_DEV_REPLY_TIMEOUT   2    /* 1 second expiration time is not good. Timer may start between 0 and 1 second. */
71                                     /* if it starts at the very end of the 0 second, timer will expire really easily. */
72
73#define BTM_INFO_TIMEOUT        5   /* 5 seconds for info response */
74
75/* After Reset a timeout can be specified in the target.h for specific targets
76 * that may require additional time to reset
77 * otherwise no timeout is required
78*/
79#ifndef BTM_AFTER_RESET_TIMEOUT
80#define BTM_AFTER_RESET_TIMEOUT 0
81#endif
82
83/* Internal baseband so the parameters such as local features, version etc. are known
84so there is no need to issue HCI commands and wait for responses at BTM initialization */
85#ifndef BTM_INTERNAL_BB
86#define BTM_INTERNAL_BB FALSE
87#endif
88
89/* The local version information in the format specified in the HCI read local version
90response message */
91#ifndef BTM_INTERNAL_LOCAL_VER
92#define BTM_INTERNAL_LOCAL_VER {0x00, 0x01, 0x05, 0x81, 0x01, 0x30, 0x00, 0x40, 0x8D}
93#endif
94
95/* The local features information in the format specified in the HCI read local features
96response message */
97#ifndef BTM_INTERNAL_LOCAL_FEA
98#define BTM_INTERNAL_LOCAL_FEA {0x00, 0xFF, 0xF9, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00}
99#endif
100
101#ifndef BTM_SET_DEV_NAME_UPON_RESET
102#define BTM_SET_DEV_NAME_UPON_RESET TRUE
103#endif
104
105/* host SCO buffer size */
106#ifndef BTM_SCO_HOST_BUF_SIZE
107#define BTM_SCO_HOST_BUF_SIZE       0xff
108#endif
109
110/********************************************************************************/
111/*              L O C A L    F U N C T I O N     P R O T O T Y P E S            */
112/********************************************************************************/
113static void btm_dev_reset (void);
114static void btm_after_reset_hold_complete (void);
115static void btm_continue_reset (void);
116
117static void btm_get_local_ext_features (UINT8 page_number);
118static void btm_decode_ext_features_page (UINT8 page_number, const BD_FEATURES p_features);
119static void btm_read_all_lmp_features_complete (UINT8 max_page_number);
120static void btm_set_lmp_features_host_may_support (UINT8 max_page_number);
121static void btm_get_local_features (void);
122static void btm_issue_host_support_for_lmp_features (void);
123static void btm_read_local_supported_cmds (UINT8 local_controller_id);
124
125#if BLE_INCLUDED == TRUE
126static void btm_read_ble_local_supported_features (void);
127#endif
128
129/*******************************************************************************
130**
131** Function         btm_dev_init
132**
133** Description      This function is on the BTM startup
134**
135** Returns          void
136**
137*******************************************************************************/
138void btm_dev_init (void)
139{
140#if 0  /* cleared in btm_init; put back in if called from anywhere else! */
141    memset (&btm_cb.devcb, 0, sizeof (tBTM_DEVCB));
142#endif
143
144    /* Initialize nonzero defaults */
145#if (BTM_MAX_LOC_BD_NAME_LEN > 0)
146    memset(btm_cb.cfg.bd_name, 0, sizeof(tBTM_LOC_BD_NAME));
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*******************************************************************************/
194static 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*******************************************************************************/
241void 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*******************************************************************************/
266void 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*******************************************************************************/
305BOOLEAN 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*******************************************************************************/
319tBTM_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_lmp_features[HCI_EXT_FEATURES_PAGE_0]))   &&
327        (!HCI_LMP_AFH_CLASS_SLAVE_SUPPORTED(btm_cb.devcb.local_lmp_features[HCI_EXT_FEATURES_PAGE_0])) &&
328        (!HCI_LMP_AFH_CLASS_MASTR_SUPPORTED(btm_cb.devcb.local_lmp_features[HCI_EXT_FEATURES_PAGE_0])))
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*******************************************************************************/
357tBTM_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_lmp_features[HCI_EXT_FEATURES_PAGE_0]))
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*******************************************************************************/
381void 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*******************************************************************************/
397static 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*******************************************************************************/
422void 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*******************************************************************************/
441void 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*******************************************************************************/
458void 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
467/*******************************************************************************
468**
469** Function         btm_read_ble_local_supported_features
470**
471** Description      Local function called to send a read BLE local supported
472**                  features command
473**
474** Returns          void
475**
476*******************************************************************************/
477static void btm_read_ble_local_supported_features(void)
478{
479    BTM_TRACE_DEBUG0("btm_read_ble_local_supported_features ");
480    btu_start_timer (&btm_cb.devcb.reset_timer, BTU_TTYPE_BTM_DEV_CTL, BTM_DEV_REPLY_TIMEOUT);
481
482    /* Send a Read Local Supported Features message to the Host Controller. */
483    btsnd_hcic_ble_read_local_spt_feat ();
484}
485#endif
486/*******************************************************************************
487**
488** Function         btm_get_local_version
489**
490** Description      Local function called to send a read local version to controller
491**
492** Returns          void
493**
494*******************************************************************************/
495void btm_get_local_version (void)
496{
497
498    btu_start_timer (&btm_cb.devcb.reset_timer, BTU_TTYPE_BTM_DEV_CTL, BTM_DEV_REPLY_TIMEOUT);
499
500    /* Send a Read Local Version message to the Host Controller. */
501    btsnd_hcic_read_local_ver (LOCAL_BR_EDR_CONTROLLER_ID);
502    btsnd_hcic_read_bd_addr ();
503
504#if BTM_PWR_MGR_INCLUDED == TRUE
505        btm_pm_reset();
506#endif
507
508}
509
510/*******************************************************************************
511**
512** Function         btm_read_local_supported_cmds
513**
514** Description      Local function called to send a read local supported commands
515**
516** Returns          void
517**
518*******************************************************************************/
519static void btm_read_local_supported_cmds (UINT8 local_controller_id)
520{
521    BTM_TRACE_DEBUG0("Start reading local supported commands");
522
523    btu_start_timer (&btm_cb.devcb.reset_timer, BTU_TTYPE_BTM_DEV_CTL, BTM_DEV_REPLY_TIMEOUT);
524
525    btsnd_hcic_read_local_supported_cmds(local_controller_id);
526}
527
528/*******************************************************************************
529**
530** Function         btm_get_local_features
531**
532** Description      Local function called to send a read local features
533**
534** Returns          void
535**
536*******************************************************************************/
537static void btm_get_local_features (void)
538{
539    /* If this BT controller supports Read Extended Feature */
540    if (btm_cb.devcb.local_version.hci_version >= HCI_PROTO_VERSION_2_0)
541    {
542        btm_get_local_ext_features(HCI_EXT_FEATURES_PAGE_0);
543    }
544    /* else, if this is a very old BT controller */
545    else
546{
547    btu_start_timer (&btm_cb.devcb.reset_timer, BTU_TTYPE_BTM_DEV_CTL, BTM_DEV_REPLY_TIMEOUT);
548
549        /* Just read the basic features (legacy HCI command) */
550    btsnd_hcic_read_local_features ();
551}
552}
553
554/*******************************************************************************
555**
556** Function         btm_get_local_ext_features
557**
558** Description      Local function called to send a read local extended features
559**
560** Returns          void
561**
562*******************************************************************************/
563static void btm_get_local_ext_features (UINT8 page_number)
564{
565    btu_start_timer (&btm_cb.devcb.reset_timer, BTU_TTYPE_BTM_DEV_CTL, BTM_DEV_REPLY_TIMEOUT);
566
567    btsnd_hcic_read_local_ext_features(page_number);
568}
569
570/*******************************************************************************
571**
572** Function         btm_dev_timeout
573**
574** Description      This function is called when a timer list entry expires.
575**
576** Returns          void
577**
578*******************************************************************************/
579void btm_dev_timeout (TIMER_LIST_ENT  *p_tle)
580{
581    TIMER_PARAM_TYPE timer_type = (TIMER_PARAM_TYPE)p_tle->param;
582
583    if ((timer_type & TT_DEV_RESET_MASK) == TT_DEV_RESET)
584    {
585        /* Call device reset as long as there is timeout*/
586        btm_dev_reset();
587    }
588    else if (timer_type == (TIMER_PARAM_TYPE)TT_DEV_RLN)
589    {
590        tBTM_CMPL_CB  *p_cb = btm_cb.devcb.p_rln_cmpl_cb;
591
592        btm_cb.devcb.p_rln_cmpl_cb = NULL;
593
594        if (p_cb)
595            (*p_cb)((void *) NULL);
596    }
597}
598
599/*******************************************************************************
600**
601** Function         btm_reset_complete
602**
603** Description      This function is called when command complete for HCI_Reset
604**                  is received.  It does not make sense to send next command
605**                  because device is resetting after command complete is
606**                  received.  Just start timer and set required state.
607**
608** Returns          void
609**
610*******************************************************************************/
611void btm_reset_complete (void)
612{
613    int devinx;
614
615    BTM_TRACE_EVENT0 ("btm_reset_complete");
616
617    /* Handle if btm initiated the reset */
618    if (btm_cb.devcb.state == BTM_DEV_STATE_WAIT_RESET_CMPLT)
619    {
620        /* Tell L2CAP that all connections are gone */
621        l2cu_device_reset ();
622
623        /* Clear current security state */
624        for (devinx = 0; devinx < BTM_SEC_MAX_DEVICE_RECORDS; devinx++)
625        {
626            btm_cb.sec_dev_rec[devinx].sec_state = BTM_SEC_STATE_IDLE;
627        }
628
629        /* After the reset controller should restore all parameters to defaults. */
630        btm_cb.btm_inq_vars.inq_counter       = 1;
631        btm_cb.btm_inq_vars.inq_scan_window   = HCI_DEF_INQUIRYSCAN_WINDOW;
632        btm_cb.btm_inq_vars.inq_scan_period   = HCI_DEF_INQUIRYSCAN_INTERVAL;
633        btm_cb.btm_inq_vars.inq_scan_type     = HCI_DEF_SCAN_TYPE;
634
635        btm_cb.btm_inq_vars.page_scan_window  = HCI_DEF_PAGESCAN_WINDOW;
636        btm_cb.btm_inq_vars.page_scan_period  = HCI_DEF_PAGESCAN_INTERVAL;
637        btm_cb.btm_inq_vars.page_scan_type    = HCI_DEF_SCAN_TYPE;
638
639#if (BTM_AFTER_RESET_TIMEOUT > 0)
640        btu_start_timer (&btm_cb.devcb.reset_timer, BTU_TTYPE_BTM_DEV_CTL,
641                         BTM_AFTER_RESET_TIMEOUT);
642#else
643        btm_cb.devcb.state = BTM_DEV_STATE_WAIT_AFTER_RESET;
644        btm_after_reset_hold_complete();
645#endif
646
647#if (BLE_INCLUDED == TRUE)
648     btm_cb.ble_ctr_cb.conn_state = BLE_CONN_IDLE;
649     btm_cb.ble_ctr_cb.bg_dev_num = 0;
650     btm_cb.ble_ctr_cb.bg_conn_type = BTM_BLE_CONN_NONE;
651     btm_cb.ble_ctr_cb.p_select_cback = NULL;
652     memset(&btm_cb.ble_ctr_cb.bg_dev_list, 0, (sizeof(tBTM_LE_BG_CONN_DEV)*BTM_BLE_MAX_BG_CONN_DEV_NUM));
653     gatt_reset_bgdev_list();
654#endif
655    }
656}
657
658/*******************************************************************************
659**
660** Function         btm_continue_reset
661**
662** Description      This function is called when wait period expired after
663**                  device reset or called by the application to continue
664**                  initialization after the application has completed its
665**                  vendor specific sequence.
666**
667** Returns          void
668**
669*******************************************************************************/
670void btm_continue_reset (void)
671{
672
673    /* Reinitialize the default class of device */
674#if BTM_INTERNAL_BB == TRUE
675    btsnd_hcic_read_bd_addr ();
676#if BTM_PWR_MGR_INCLUDED == TRUE
677    btm_pm_reset();
678#endif
679#endif
680
681    btm_get_hci_buf_size ();
682
683    /* default device class */
684    BTM_SetDeviceClass((UINT8 *) BTM_INIT_CLASS_OF_DEVICE);
685
686#if (BTM_MAX_LOC_BD_NAME_LEN > 0) && (BTM_SET_DEV_NAME_UPON_RESET == TRUE)
687    BTM_SetLocalDeviceName(btm_cb.cfg.bd_name);
688#endif
689
690    BTM_SetPinType (btm_cb.cfg.pin_type, btm_cb.cfg.pin_code, btm_cb.cfg.pin_code_len);
691}
692
693/*******************************************************************************
694**
695** Function         btm_after_reset_hold_complete
696**
697** Description      This function is called when wait period expired after
698**                  device reset.  Continue intitialization
699**
700** Returns          void
701**
702*******************************************************************************/
703void btm_after_reset_hold_complete (void)
704{
705#ifdef BTM_APP_DEV_INIT
706    btu_stop_timer(&btm_cb.devcb.reset_timer);
707    BTM_APP_DEV_INIT();
708#else
709    btm_continue_reset();
710#endif
711}
712
713
714/*******************************************************************************
715**
716** Function         btm_read_hci_buf_size_complete
717**
718** Description      This function is called when command complete for
719**                  get HCI buffer size is received.  Start timer and send
720**                  read local features request
721**
722** Returns          void
723**
724*******************************************************************************/
725void btm_read_hci_buf_size_complete (UINT8 *p, UINT16 evt_len)
726{
727    UINT8       status;
728    UINT8       lm_sco_buf_size;
729    UINT16      lm_num_acl_bufs;
730    UINT16      lm_num_sco_bufs;
731    UINT16      acl_buf_size;
732
733    STREAM_TO_UINT8  (status, p);
734    if (status == HCI_SUCCESS)
735    {
736        STREAM_TO_UINT16 (btu_cb.hcit_acl_data_size, p);
737        STREAM_TO_UINT8  (lm_sco_buf_size,   p);
738        STREAM_TO_UINT16 (lm_num_acl_bufs,   p);
739        STREAM_TO_UINT16 (lm_num_sco_bufs,   p);
740
741        btu_cb.hcit_acl_pkt_size = btu_cb.hcit_acl_data_size + HCI_DATA_PREAMBLE_SIZE;
742
743        l2c_link_processs_num_bufs (lm_num_acl_bufs);
744
745#if BTM_ACL_BUF_SIZE > 0
746        acl_buf_size = (BTM_ACL_BUF_SIZE < L2CAP_MTU_SIZE) ? BTM_ACL_BUF_SIZE : L2CAP_MTU_SIZE;
747#else
748        acl_buf_size = L2CAP_MTU_SIZE;
749#endif
750        /* Tell the controller what our buffer sizes are. ?? Need SCO info */
751        btsnd_hcic_set_host_buf_size (acl_buf_size, BTM_SCO_HOST_BUF_SIZE, L2CAP_HOST_FC_ACL_BUFS, 10);
752
753#if L2CAP_HOST_FLOW_CTRL == TRUE
754        btsnd_hcic_set_host_flow_ctrl (HCI_HOST_FLOW_CTRL_ACL_ON);
755#endif
756    }
757
758    /* Set the device into connectable and/or discoverable mode (if configured to do so) */
759#if BTM_IS_CONNECTABLE == TRUE
760    (void) BTM_SetConnectability (BTM_CONNECTABLE, BTM_DEFAULT_CONN_WINDOW, BTM_DEFAULT_CONN_INTERVAL);
761#endif
762
763#if BTM_IS_DISCOVERABLE == TRUE
764    (void) BTM_SetDiscoverability (BTM_DEFAULT_DISC_MODE, BTM_DEFAULT_DISC_WINDOW, BTM_DEFAULT_DISC_INTERVAL);
765#endif
766
767#if BTM_INTERNAL_BB == TRUE
768    {
769        UINT8 buf[9] = BTM_INTERNAL_LOCAL_VER;
770        btm_read_local_version_complete( buf, 9 );
771    }
772#else
773    btm_get_local_version ();
774#endif
775}
776
777#if (BLE_INCLUDED == TRUE)
778/*******************************************************************************
779**
780** Function         btm_read_ble_buf_size_complete
781**
782** Description      This function is called when command complete for
783**                  get HCI buffer size is received.  Start timer and send
784**                  read local supported features request
785**
786** Returns          void
787**
788*******************************************************************************/
789void btm_read_ble_buf_size_complete (UINT8 *p, UINT16 evt_len)
790{
791    UINT8       status;
792    UINT16      lm_num_le_bufs;
793
794     BTM_TRACE_DEBUG0("btm_read_ble_buf_size_complete ");
795    STREAM_TO_UINT8  (status, p);
796    if (status == HCI_SUCCESS)
797    {
798        STREAM_TO_UINT16 (btu_cb.hcit_ble_acl_data_size, p);
799        STREAM_TO_UINT8 (lm_num_le_bufs,   p);
800
801        if (btu_cb.hcit_ble_acl_data_size == 0)
802            btu_cb.hcit_ble_acl_data_size = btu_cb.hcit_acl_data_size;
803
804        btu_cb.hcit_ble_acl_pkt_size = btu_cb.hcit_ble_acl_data_size + HCI_DATA_PREAMBLE_SIZE;
805
806        l2c_link_processs_ble_num_bufs (lm_num_le_bufs);
807    }
808
809    btm_read_ble_local_supported_features();
810}
811
812/*******************************************************************************
813**
814** Function         btm_read_ble_local_supported_features_complete
815**
816** Description      This function is called when command complete for
817**                  Read LE Local Supported Features is received.  Start timer and send
818**                  read LMP local features request
819**
820** Returns          void
821**
822*******************************************************************************/
823void btm_read_ble_local_supported_features_complete (UINT8 *p, UINT16 evt_len)
824{
825    UINT8       status;
826
827    BTM_TRACE_DEBUG0("btm_read_ble_local_supported_features_complete ");
828
829    btu_stop_timer (&btm_cb.devcb.reset_timer);
830
831    STREAM_TO_UINT8  (status, p);
832    if (status == HCI_SUCCESS)
833    {
834        STREAM_TO_ARRAY(&btm_cb.devcb.local_le_features, p, HCI_FEATURE_BYTES_PER_PAGE);
835    }
836    else
837    {
838        BTM_TRACE_WARNING1 ("btm_read_ble_local_supported_features_complete status = %d", status);
839    }
840
841#if BTM_INTERNAL_BB == TRUE
842    {
843        UINT8 buf[9] = BTM_INTERNAL_LOCAL_FEA;
844        btm_read_local_features_complete( buf, 9 );
845    }
846#else
847
848    /* get local feature if BRCM specific feature is not included  */
849    btm_reset_ctrlr_complete();
850#endif
851
852}
853
854/*******************************************************************************
855**
856** Function         btm_read_white_list_size_complete
857**
858** Description      This function read the current white list size.
859*******************************************************************************/
860void btm_read_white_list_size_complete(UINT8 *p, UINT16 evt_len)
861{
862    UINT8       status;
863
864     BTM_TRACE_DEBUG0("btm_read_white_list_size_complete ");
865    STREAM_TO_UINT8  (status, p);
866
867    if (status == HCI_SUCCESS)
868    {
869        STREAM_TO_UINT8(btm_cb.ble_ctr_cb.max_filter_entries, p);
870        btm_cb.ble_ctr_cb.num_empty_filter = btm_cb.ble_ctr_cb.max_filter_entries;
871    }
872
873    btm_get_ble_buffer_size();
874}
875
876#endif
877/*******************************************************************************
878**
879** Function         btm_read_local_version_complete
880**
881** Description      This function is called when local BD Addr read complete
882**                  message is received from the HCI.
883**
884** Returns          void
885**
886*******************************************************************************/
887void btm_read_local_version_complete (UINT8 *p, UINT16 evt_len)
888{
889    tBTM_VERSION_INFO   *p_vi = &btm_cb.devcb.local_version;
890    UINT8                status;
891
892#ifdef BTA_PRM_CHECK_FW_VER
893    if(BTA_PRM_CHECK_FW_VER(p))
894        return;
895#endif
896
897    STREAM_TO_UINT8  (status, p);
898    if (status == HCI_SUCCESS)
899    {
900
901        STREAM_TO_UINT8  (p_vi->hci_version, p);
902        STREAM_TO_UINT16 (p_vi->hci_revision, p);
903        STREAM_TO_UINT8  (p_vi->lmp_version, p);
904        STREAM_TO_UINT16 (p_vi->manufacturer, p);
905        STREAM_TO_UINT16 (p_vi->lmp_subversion, p);
906    }
907
908    if (p_vi->hci_version >= HCI_PROTO_VERSION_1_2)
909    {
910        btm_read_local_supported_cmds(LOCAL_BR_EDR_CONTROLLER_ID);
911    }
912    else
913    {
914        btm_get_local_features ();
915    }
916}
917
918/*******************************************************************************
919**
920** Function         btm_decode_ext_features_page
921**
922** Description      This function is decodes a features page.
923**
924** Returns          void
925**
926*******************************************************************************/
927static void btm_decode_ext_features_page (UINT8 page_number, const UINT8 *p_features)
928{
929    UINT8 last;
930    UINT8 first;
931
932    BTM_TRACE_DEBUG1 ("btm_decode_ext_features_page page: %d", page_number);
933    switch (page_number)
934    {
935    /* Extended (Legacy) Page 0 */
936    case HCI_EXT_FEATURES_PAGE_0:
937
938        /* Create ACL supported packet types mask */
939        btm_cb.btm_acl_pkt_types_supported = (BTM_ACL_PKT_TYPES_MASK_DH1 +
940                                              BTM_ACL_PKT_TYPES_MASK_DM1);
941
942        if (HCI_3_SLOT_PACKETS_SUPPORTED(p_features))
943            btm_cb.btm_acl_pkt_types_supported |= (BTM_ACL_PKT_TYPES_MASK_DH3 +
944                                                   BTM_ACL_PKT_TYPES_MASK_DM3);
945
946        if (HCI_5_SLOT_PACKETS_SUPPORTED(p_features))
947            btm_cb.btm_acl_pkt_types_supported |= (BTM_ACL_PKT_TYPES_MASK_DH5 +
948                                                   BTM_ACL_PKT_TYPES_MASK_DM5);
949
950        /* _NO_X_DXX masks are reserved before ver 2.0.
951           Set them only for later versions of controller */
952        if (btm_cb.devcb.local_version.hci_version >= HCI_PROTO_VERSION_2_0)
953        {
954            /* Add in EDR related ACL types */
955            if (!HCI_EDR_ACL_2MPS_SUPPORTED(p_features))
956            {
957                btm_cb.btm_acl_pkt_types_supported |= (BTM_ACL_PKT_TYPES_MASK_NO_2_DH1 +
958                                                       BTM_ACL_PKT_TYPES_MASK_NO_2_DH3 +
959                                                       BTM_ACL_PKT_TYPES_MASK_NO_2_DH5);
960            }
961
962            if (!HCI_EDR_ACL_3MPS_SUPPORTED(p_features))
963            {
964                btm_cb.btm_acl_pkt_types_supported |= (BTM_ACL_PKT_TYPES_MASK_NO_3_DH1 +
965                                                       BTM_ACL_PKT_TYPES_MASK_NO_3_DH3 +
966                                                       BTM_ACL_PKT_TYPES_MASK_NO_3_DH5);
967            }
968
969            /* Check to see if 3 and 5 slot packets are available */
970            if (HCI_EDR_ACL_2MPS_SUPPORTED(p_features) ||
971                HCI_EDR_ACL_3MPS_SUPPORTED(p_features))
972            {
973                if (!HCI_3_SLOT_EDR_ACL_SUPPORTED(p_features))
974                    btm_cb.btm_acl_pkt_types_supported |= (BTM_ACL_PKT_TYPES_MASK_NO_2_DH3 +
975                                                           BTM_ACL_PKT_TYPES_MASK_NO_3_DH3);
976
977                if (!HCI_5_SLOT_EDR_ACL_SUPPORTED(p_features))
978                    btm_cb.btm_acl_pkt_types_supported |= (BTM_ACL_PKT_TYPES_MASK_NO_2_DH5 +
979                                                           BTM_ACL_PKT_TYPES_MASK_NO_3_DH5);
980            }
981        }
982
983        BTM_TRACE_DEBUG1("Local supported ACL packet types: 0x%04x",
984                         btm_cb.btm_acl_pkt_types_supported);
985
986        /* Create (e)SCO supported packet types mask */
987        btm_cb.btm_sco_pkt_types_supported = 0;
988#if BTM_SCO_INCLUDED == TRUE
989        btm_cb.sco_cb.esco_supported = FALSE;
990#endif
991        if (HCI_SCO_LINK_SUPPORTED(p_features))
992        {
993            btm_cb.btm_sco_pkt_types_supported = BTM_SCO_PKT_TYPES_MASK_HV1;
994
995            if (HCI_HV2_PACKETS_SUPPORTED(p_features))
996                btm_cb.btm_sco_pkt_types_supported |= BTM_SCO_PKT_TYPES_MASK_HV2;
997
998            if (HCI_HV3_PACKETS_SUPPORTED(p_features))
999                btm_cb.btm_sco_pkt_types_supported |= BTM_SCO_PKT_TYPES_MASK_HV3;
1000        }
1001
1002        if (HCI_ESCO_EV3_SUPPORTED(p_features))
1003            btm_cb.btm_sco_pkt_types_supported |= BTM_SCO_PKT_TYPES_MASK_EV3;
1004
1005        if (HCI_ESCO_EV4_SUPPORTED(p_features))
1006            btm_cb.btm_sco_pkt_types_supported |= BTM_SCO_PKT_TYPES_MASK_EV4;
1007
1008        if (HCI_ESCO_EV5_SUPPORTED(p_features))
1009            btm_cb.btm_sco_pkt_types_supported |= BTM_SCO_PKT_TYPES_MASK_EV5;
1010#if BTM_SCO_INCLUDED == TRUE
1011        if (btm_cb.btm_sco_pkt_types_supported & BTM_ESCO_LINK_ONLY_MASK)
1012        {
1013            btm_cb.sco_cb.esco_supported = TRUE;
1014
1015            /* Add in EDR related eSCO types */
1016            if (HCI_EDR_ESCO_2MPS_SUPPORTED(p_features))
1017            {
1018                if (!HCI_3_SLOT_EDR_ESCO_SUPPORTED(p_features))
1019                    btm_cb.btm_sco_pkt_types_supported |= BTM_SCO_PKT_TYPES_MASK_NO_2_EV5;
1020            }
1021            else
1022            {
1023                btm_cb.btm_sco_pkt_types_supported |= (BTM_SCO_PKT_TYPES_MASK_NO_2_EV3 +
1024                                                       BTM_SCO_PKT_TYPES_MASK_NO_2_EV5);
1025            }
1026
1027            if (HCI_EDR_ESCO_3MPS_SUPPORTED(p_features))
1028            {
1029                if (!HCI_3_SLOT_EDR_ESCO_SUPPORTED(p_features))
1030                    btm_cb.btm_sco_pkt_types_supported |= BTM_SCO_PKT_TYPES_MASK_NO_3_EV5;
1031            }
1032            else
1033            {
1034                btm_cb.btm_sco_pkt_types_supported |= (BTM_SCO_PKT_TYPES_MASK_NO_3_EV3 +
1035                                                       BTM_SCO_PKT_TYPES_MASK_NO_3_EV5);
1036            }
1037        }
1038#endif
1039
1040        BTM_TRACE_DEBUG1("Local supported SCO packet types: 0x%04x",
1041                         btm_cb.btm_sco_pkt_types_supported);
1042
1043        /* Create Default Policy Settings */
1044        if (HCI_SWITCH_SUPPORTED(p_features))
1045            btm_cb.btm_def_link_policy |= HCI_ENABLE_MASTER_SLAVE_SWITCH;
1046        else
1047            btm_cb.btm_def_link_policy &= ~HCI_ENABLE_MASTER_SLAVE_SWITCH;
1048
1049        if (HCI_HOLD_MODE_SUPPORTED(p_features))
1050            btm_cb.btm_def_link_policy |= HCI_ENABLE_HOLD_MODE;
1051        else
1052            btm_cb.btm_def_link_policy &= ~HCI_ENABLE_HOLD_MODE;
1053
1054        if (HCI_SNIFF_MODE_SUPPORTED(p_features))
1055            btm_cb.btm_def_link_policy |= HCI_ENABLE_SNIFF_MODE;
1056        else
1057            btm_cb.btm_def_link_policy &= ~HCI_ENABLE_SNIFF_MODE;
1058
1059        if (HCI_PARK_MODE_SUPPORTED(p_features))
1060            btm_cb.btm_def_link_policy |= HCI_ENABLE_PARK_MODE;
1061        else
1062            btm_cb.btm_def_link_policy &= ~HCI_ENABLE_PARK_MODE;
1063
1064        btm_sec_dev_reset ();
1065#if ((BTM_EIR_SERVER_INCLUDED == TRUE)||(BTM_EIR_CLIENT_INCLUDED == TRUE))
1066        if (HCI_LMP_INQ_RSSI_SUPPORTED(p_features))
1067        {
1068            if (HCI_EXT_INQ_RSP_SUPPORTED(p_features))
1069                BTM_SetInquiryMode (BTM_INQ_RESULT_EXTENDED);
1070            else
1071                BTM_SetInquiryMode (BTM_INQ_RESULT_WITH_RSSI);
1072        }
1073#else
1074        if (HCI_LMP_INQ_RSSI_SUPPORTED(p_features))
1075            BTM_SetInquiryMode (BTM_INQ_RESULT_WITH_RSSI);
1076#endif
1077#if L2CAP_NON_FLUSHABLE_PB_INCLUDED == TRUE
1078        if( HCI_NON_FLUSHABLE_PB_SUPPORTED(p_features))
1079            l2cu_set_non_flushable_pbf(TRUE);
1080        else
1081            l2cu_set_non_flushable_pbf(FALSE);
1082#endif
1083        BTM_SetPageScanType (BTM_DEFAULT_SCAN_TYPE);
1084        BTM_SetInquiryScanType (BTM_DEFAULT_SCAN_TYPE);
1085
1086        break;
1087
1088    /* Extended Page 1 */
1089    case HCI_EXT_FEATURES_PAGE_1:
1090        /* Nothing to do for page 1 */
1091        break;
1092
1093    /* Extended Page 2 */
1094    case HCI_EXT_FEATURES_PAGE_2:
1095        /* Nothing to do for page 2 */
1096        break;
1097
1098    default:
1099        BTM_TRACE_ERROR1("btm_decode_ext_features_page page=%d unknown", page_number);
1100        break;
1101    }
1102}
1103
1104/*******************************************************************************
1105**
1106** Function         btm_reset_ctrlr_complete
1107**
1108** Description      This is the last step of BR/EDR controller startup sequence.
1109**
1110** Returns          void
1111**
1112*******************************************************************************/
1113void btm_reset_ctrlr_complete ()
1114{
1115    tBTM_DEVCB     *p_devcb = &btm_cb.devcb;
1116    tBTM_CMPL_CB   *p_cb = p_devcb->p_reset_cmpl_cb;
1117    BOOLEAN         found = FALSE;
1118    UINT8           i, j, max_page_number;
1119
1120    btu_stop_timer (&btm_cb.devcb.reset_timer);
1121
1122    /* find the highest feature page number which contains non-zero bits */
1123    for (i = HCI_EXT_FEATURES_PAGE_MAX; i >= 0; i--)
1124    {
1125        for (j = 0; j < HCI_FEATURE_BYTES_PER_PAGE; j++)
1126        {
1127            if (p_devcb->local_lmp_features[i][j] != 0)
1128            {
1129                found = TRUE;
1130                break;
1131            }
1132        }
1133        if (found || !i)
1134        {
1135             break;
1136        }
1137    }
1138
1139    if (!found)
1140        BTM_TRACE_WARNING0 ("btm_reset_ctrlr_complete: NONE of local controller features is set");
1141
1142    max_page_number = i;
1143
1144    BTM_TRACE_DEBUG1 ("btm_reset_ctrlr_complete: max_page_number: %d", max_page_number);
1145
1146    /*
1147    * Set State to Ready (needs to be done before btm_decode_ext_features_page
1148    * to allow it to send some HCI configuration commands)
1149    */
1150    p_devcb->state = BTM_DEV_STATE_READY;
1151
1152    /* For every received/saved feature page */
1153    for (i = 0; i <= max_page_number; i++)
1154    {
1155        /* Decode the saved Feature Page */
1156        btm_decode_ext_features_page(i, p_devcb->local_lmp_features[i]);
1157    }
1158
1159    /* If there was a callback address for reset complete, reset it */
1160    p_devcb->p_reset_cmpl_cb = NULL;
1161
1162    /* If anyone wants device status notifications, give him one */
1163    btm_report_device_status(BTM_DEV_STATUS_UP);
1164
1165    /* Reset sequence is complete. If this was an application originated */
1166    /* reset, tell him its done.                                         */
1167    if (p_cb)
1168        (*p_cb)((void *) NULL);
1169}
1170
1171/*******************************************************************************
1172**
1173** Function         btm_issue_host_support_for_lmp_features
1174**
1175** Description      This function:
1176**                  - issues commands to set host supported LMP features (one at
1177**                    a time);
1178**                  - after this is done it issues command to re-read LMP features
1179**                    page 1;
1180**                  - after this is done it calls the last step of BR/EDR
1181**                    controller startup sequence.
1182**
1183** Returns          void
1184**
1185*******************************************************************************/
1186static void btm_issue_host_support_for_lmp_features (void)
1187{
1188    BTM_TRACE_DEBUG1("btm_issue_host_support_for_lmp_features lmp_features_host_may_support: 0x%02x", btm_cb.devcb.lmp_features_host_may_support);
1189
1190    if (btm_cb.devcb.lmp_features_host_may_support & BTM_HOST_MAY_SUPP_SSP)
1191    {
1192        btsnd_hcic_write_simple_pairing_mode(HCI_SP_MODE_ENABLED);
1193        return;
1194    }
1195
1196#if (BLE_INCLUDED == TRUE)
1197    if (btm_cb.devcb.lmp_features_host_may_support & BTM_HOST_MAY_SUPP_LE)
1198    {
1199        if (btm_cb.devcb.lmp_features_host_may_support & BTM_HOST_MAY_SUPP_SIMULT_BR_LE)
1200        {
1201            /* At the moment the host can't work simultaneously with BR/EDR and LE */
1202            btsnd_hcic_ble_write_host_supported(BTM_BLE_HOST_SUPPORT, 0);
1203        }
1204        else
1205        {
1206            btsnd_hcic_ble_write_host_supported(BTM_BLE_HOST_SUPPORT, 0);
1207        }
1208        return;
1209    }
1210#endif
1211
1212    if (btm_cb.devcb.lmp_features_host_may_support & BTM_RE_READ_1ST_PAGE)
1213    {
1214        btm_get_local_ext_features(HCI_EXT_FEATURES_PAGE_1);
1215        return;
1216    }
1217
1218    if (!btm_cb.devcb.lmp_features_host_may_support)
1219    {
1220#if BLE_INCLUDED == TRUE
1221        if (HCI_LE_HOST_SUPPORTED(btm_cb.devcb.local_lmp_features[HCI_EXT_FEATURES_PAGE_1]))
1222        {
1223            btm_read_ble_wl_size();
1224        }
1225        else
1226#elif BTM_INTERNAL_BB == TRUE
1227        {
1228            UINT8 buf[9] = BTM_INTERNAL_LOCAL_FEA;
1229            btm_read_local_features_complete( buf, 9 );
1230        }
1231#endif
1232        {
1233            btm_reset_ctrlr_complete();
1234        }
1235        return;
1236    }
1237
1238    BTM_TRACE_ERROR2("%s lmp_features_host_may_support: 0x%02x. This is unexpected.",__FUNCTION__,
1239                      btm_cb.devcb.lmp_features_host_may_support);
1240}
1241
1242/*******************************************************************************
1243**
1244** Function         btm_set_lmp_features_host_may_support
1245**
1246** Description      This function is called after all LMP features provided by
1247**                  controller are read. It sets the mask that indicates LMP
1248**                  features the host may support based on LMP features supported
1249**                  by controller.
1250**                  Example:
1251**                  Host may set SSP (host support) bit only if SSP (controller
1252**                  support) bit is set by controller.
1253**
1254** Returns          void
1255**
1256*******************************************************************************/
1257static void btm_set_lmp_features_host_may_support (UINT8 max_page_number)
1258{
1259    btm_cb.devcb.lmp_features_host_may_support = 0;
1260
1261    /* LMP page 0 is always read */
1262    if (HCI_SIMPLE_PAIRING_SUPPORTED(btm_cb.devcb.local_lmp_features[HCI_EXT_FEATURES_PAGE_0]))
1263    {
1264        /* host may support SSP */
1265        btm_cb.devcb.lmp_features_host_may_support |= BTM_HOST_MAY_SUPP_SSP;
1266    }
1267
1268#if (BLE_INCLUDED == TRUE)
1269    if (HCI_LE_SPT_SUPPORTED(btm_cb.devcb.local_lmp_features[HCI_EXT_FEATURES_PAGE_0]))
1270    {
1271        /* host may support LE */
1272        btm_cb.devcb.lmp_features_host_may_support |= BTM_HOST_MAY_SUPP_LE;
1273
1274        if (HCI_SIMUL_LE_BREDR_SUPPORTED(btm_cb.devcb.local_lmp_features[HCI_EXT_FEATURES_PAGE_0]))
1275        {
1276            /* host may support BR/EDR and LE simultaneously */
1277            btm_cb.devcb.lmp_features_host_may_support |= BTM_HOST_MAY_SUPP_SIMULT_BR_LE;
1278        }
1279    }
1280#endif
1281
1282    if (max_page_number >= HCI_EXT_FEATURES_PAGE_1)
1283    {
1284        /* nothing yet for HCI_EXT_FEATURES_PAGE_1 */
1285    }
1286
1287    if (max_page_number >= HCI_EXT_FEATURES_PAGE_1)
1288    {
1289        /* nothing yet for HCI_EXT_FEATURES_PAGE_2 */
1290    }
1291
1292    if (btm_cb.devcb.lmp_features_host_may_support)
1293        btm_cb.devcb.lmp_features_host_may_support |= BTM_RE_READ_1ST_PAGE;
1294}
1295
1296/*******************************************************************************
1297**
1298** Function         btm_read_all_lmp_features_complete
1299**
1300** Description      This function is called after all LMP features provided by
1301**                  controller are read.
1302**                  It works with controller supported LMP features which host
1303**                  may support too.
1304**
1305** Returns          void
1306**
1307*******************************************************************************/
1308static void btm_read_all_lmp_features_complete (UINT8 max_page_number)
1309{
1310    btm_set_lmp_features_host_may_support(max_page_number);
1311
1312    btm_issue_host_support_for_lmp_features();
1313}
1314
1315/*******************************************************************************
1316**
1317** Function         btm_read_local_features_complete
1318**
1319** Description      This function is called when local supported features read
1320**                  is complete.
1321**
1322** Returns          void
1323**
1324*******************************************************************************/
1325void btm_read_local_features_complete (UINT8 *p, UINT16 evt_len)
1326{
1327    tBTM_DEVCB     *p_devcb = &btm_cb.devcb;
1328    UINT8           status;
1329
1330    btu_stop_timer (&p_devcb->reset_timer);
1331
1332    STREAM_TO_UINT8  (status, p);
1333    if (status == HCI_SUCCESS)
1334    {
1335        /* Save the Feature Page 0 */
1336        STREAM_TO_ARRAY(p_devcb->local_lmp_features[0],
1337                p, HCI_FEATURE_BYTES_PER_PAGE);
1338
1339        if ((HCI_LMP_EXTENDED_SUPPORTED(p_devcb->local_lmp_features[HCI_EXT_FEATURES_PAGE_0])) &&
1340            (HCI_READ_LOCAL_EXT_FEATURES_SUPPORTED(p_devcb->supported_cmds)))
1341        {
1342            /* if local controller has extended features and supports
1343            **HCI_Read_Local_Extended_Features command,
1344            ** then start reading these feature starting with extended features page 1 */
1345            BTM_TRACE_DEBUG0 ("Start reading local extended features");
1346            btm_get_local_ext_features(HCI_EXT_FEATURES_PAGE_1);
1347        }
1348        else
1349        {
1350            btm_read_all_lmp_features_complete (HCI_EXT_FEATURES_PAGE_0);
1351        }
1352    }
1353}
1354
1355/*******************************************************************************
1356**
1357** Function         btm_read_local_ext_features_complete
1358**
1359** Description      This function is called when read local extended features
1360**                  command complete message is received from the HCI.
1361**
1362** Returns          void
1363**
1364*******************************************************************************/
1365void btm_read_local_ext_features_complete (UINT8 *p, UINT16 evt_len)
1366{
1367    tBTM_DEVCB     *p_devcb = &btm_cb.devcb;
1368    tBTM_CMPL_CB   *p_cb = p_devcb->p_reset_cmpl_cb;
1369    UINT8           status;
1370    UINT8           page_number;
1371    UINT8           page_number_max;
1372
1373    btu_stop_timer (&btm_cb.devcb.reset_timer);
1374
1375    STREAM_TO_UINT8 (status, p);
1376
1377    if (status != HCI_SUCCESS)
1378    {
1379        BTM_TRACE_WARNING1("btm_read_local_ext_features_complete status = 0x%02X", status);
1380        btm_read_all_lmp_features_complete (HCI_EXT_FEATURES_PAGE_0);
1381        return;
1382    }
1383
1384    /* Extract Page number */
1385    STREAM_TO_UINT8  (page_number, p);
1386
1387    /* Extract Page number Max */
1388    STREAM_TO_UINT8  (page_number_max, p);
1389
1390    if (page_number > HCI_EXT_FEATURES_PAGE_MAX)
1391    {
1392        BTM_TRACE_ERROR1("btm_read_local_ext_features_complete page=%d unknown",
1393                page_number);
1394        return;
1395    }
1396
1397    /* Save the extended features Page received */
1398    STREAM_TO_ARRAY(btm_cb.devcb.local_lmp_features[page_number],
1399            p, HCI_FEATURE_BYTES_PER_PAGE);
1400
1401    /* If this is re-read of the 1-st extended page after host supported LMP features are set */
1402    if ((page_number == HCI_EXT_FEATURES_PAGE_1) &&
1403        (btm_cb.devcb.lmp_features_host_may_support == BTM_RE_READ_1ST_PAGE))
1404    {
1405        btm_cb.devcb.lmp_features_host_may_support &= ~BTM_RE_READ_1ST_PAGE;
1406        btm_issue_host_support_for_lmp_features();
1407        return;
1408    }
1409
1410    /* If this is the last page supported by the local BT controller OR */
1411    /* if this is the last page supported by the Host */
1412    if ((page_number == page_number_max) ||
1413        (page_number == HCI_EXT_FEATURES_PAGE_MAX))
1414    {
1415        BTM_TRACE_DEBUG1("BTM reached last extended features page (%d)", page_number);
1416        btm_read_all_lmp_features_complete(page_number);
1417    }
1418    /* Else (another page must be read) */
1419    else
1420    {
1421        /* Read the next features page */
1422        page_number++;
1423        BTM_TRACE_DEBUG1("BTM reads next extended features page (%d)", page_number);
1424        btm_get_local_ext_features(page_number);
1425    }
1426}
1427
1428/*******************************************************************************
1429**
1430** Function         btm_read_local_supported_cmds_complete
1431**
1432** Description      This function is called when local supported commands read
1433**                  is complete.
1434**
1435** Returns          void
1436**
1437*******************************************************************************/
1438void btm_read_local_supported_cmds_complete (UINT8 *p)
1439{
1440    tBTM_DEVCB     *p_devcb = &btm_cb.devcb;
1441    UINT8           status;
1442
1443    btu_stop_timer (&(p_devcb->reset_timer));
1444
1445    STREAM_TO_UINT8  (status, p);
1446    BTM_TRACE_DEBUG1("btm_read_local_supported_cmds_complete status (0x%02x)", status);
1447
1448    if (status == HCI_SUCCESS)
1449    {
1450        /* Save the supported commands bit mask */
1451        STREAM_TO_ARRAY(p_devcb->supported_cmds, p, HCI_NUM_SUPP_COMMANDS_BYTES);
1452    }
1453
1454    btm_get_local_features();
1455}
1456
1457/*******************************************************************************
1458**
1459** Function         btm_write_simple_paring_mode_complete
1460**
1461** Description      This function is called when the command complete message
1462**                  is received from the HCI for the write simple pairing mode
1463**                  command.
1464**
1465** Returns          void
1466**
1467*******************************************************************************/
1468void btm_write_simple_paring_mode_complete (UINT8 *p)
1469{
1470    UINT8   status;
1471
1472    STREAM_TO_UINT8 (status, p);
1473
1474    if (status != HCI_SUCCESS)
1475    {
1476        BTM_TRACE_WARNING1("btm_write_simple_paring_mode_complete status: 0x%02x", status);
1477    }
1478
1479    if (btm_cb.devcb.lmp_features_host_may_support & BTM_HOST_MAY_SUPP_SSP)
1480    {
1481        btm_cb.devcb.lmp_features_host_may_support &= ~BTM_HOST_MAY_SUPP_SSP;
1482        btm_issue_host_support_for_lmp_features();
1483    }
1484}
1485
1486/*******************************************************************************
1487**
1488** Function         btm_write_le_host_supported_complete
1489**
1490** Description      This function is called when the command complete message
1491**                  is received from the HCI for the write LE host supported
1492**                  command.
1493**
1494** Returns          void
1495**
1496*******************************************************************************/
1497void btm_write_le_host_supported_complete (UINT8 *p)
1498{
1499    UINT8   status;
1500
1501    STREAM_TO_UINT8 (status, p);
1502
1503    if (status != HCI_SUCCESS)
1504    {
1505        BTM_TRACE_WARNING1("btm_write_le_host_supported_complete status: 0x%02x", status);
1506    }
1507
1508    if (btm_cb.devcb.lmp_features_host_may_support & BTM_HOST_MAY_SUPP_LE)
1509    {
1510        btm_cb.devcb.lmp_features_host_may_support &= ~BTM_HOST_MAY_SUPP_LE;
1511        if (btm_cb.devcb.lmp_features_host_may_support & BTM_HOST_MAY_SUPP_SIMULT_BR_LE)
1512        {
1513            btm_cb.devcb.lmp_features_host_may_support &= ~BTM_HOST_MAY_SUPP_SIMULT_BR_LE;
1514        }
1515        btm_issue_host_support_for_lmp_features();
1516    }
1517}
1518
1519/*******************************************************************************
1520**
1521** Function         btm_get_voice_coding_support
1522**
1523** Description      This function is provides a way to get the voice coding schemes
1524**                  supported the device.
1525**
1526** Returns          A bit mask - Bit 0 if set indicates CVSD support
1527**                               Bit 1 if set indicates PCM A-law support
1528**                               Bit 2 if set indicates PCM Mu-law support
1529**
1530*******************************************************************************/
1531
1532UINT8 btm_get_voice_coding_support( void )
1533{
1534    UINT8 code = 0;
1535
1536    if( HCI_LMP_CVSD_SUPPORTED(btm_cb.devcb.local_lmp_features[HCI_EXT_FEATURES_PAGE_0]) ) code |= 0x01 ;
1537    if( HCI_LMP_A_LAW_SUPPORTED(btm_cb.devcb.local_lmp_features[HCI_EXT_FEATURES_PAGE_0]) ) code |= 0x02 ;
1538    if( HCI_LMP_U_LAW_SUPPORTED(btm_cb.devcb.local_lmp_features[HCI_EXT_FEATURES_PAGE_0]) )  code |= 0x04 ;
1539
1540    return code ;
1541}
1542
1543/*******************************************************************************
1544**
1545** Function         BTM_SetLocalDeviceName
1546**
1547** Description      This function is called to set the local device name.
1548**
1549** Returns          status of the operation
1550**
1551*******************************************************************************/
1552tBTM_STATUS BTM_SetLocalDeviceName (char *p_name)
1553{
1554    UINT8    *p;
1555
1556    if (!p_name || !p_name[0] || (strlen ((char *)p_name) > BD_NAME_LEN))
1557        return (BTM_ILLEGAL_VALUE);
1558
1559    if (btm_cb.devcb.state == BTM_DEV_STATE_WAIT_RESET_CMPLT ||
1560        btm_cb.devcb.state == BTM_DEV_STATE_WAIT_AFTER_RESET)
1561        return (BTM_DEV_RESET);
1562
1563#if BTM_MAX_LOC_BD_NAME_LEN > 0
1564    /* Save the device name if local storage is enabled */
1565    p = (UINT8 *)btm_cb.cfg.bd_name;
1566    if (p != (UINT8 *)p_name)
1567    {
1568        BCM_STRNCPY_S(btm_cb.cfg.bd_name, sizeof(btm_cb.cfg.bd_name), p_name, BTM_MAX_LOC_BD_NAME_LEN);
1569        btm_cb.cfg.bd_name[BTM_MAX_LOC_BD_NAME_LEN] = '\0';
1570    }
1571#else
1572    p = (UINT8 *)p_name;
1573#endif
1574
1575    if (btsnd_hcic_change_name(p))
1576        return (BTM_CMD_STARTED);
1577    else
1578        return (BTM_NO_RESOURCES);
1579}
1580
1581
1582
1583/*******************************************************************************
1584**
1585** Function         BTM_ReadLocalDeviceName
1586**
1587** Description      This function is called to read the local device name.
1588**
1589** Returns          status of the operation
1590**                  If success, BTM_SUCCESS is returned and p_name points stored
1591**                              local device name
1592**                  If BTM doesn't store local device name, BTM_NO_RESOURCES is
1593**                              is returned and p_name is set to NULL
1594**
1595*******************************************************************************/
1596tBTM_STATUS BTM_ReadLocalDeviceName (char **p_name)
1597{
1598#if BTM_MAX_LOC_BD_NAME_LEN > 0
1599    *p_name = btm_cb.cfg.bd_name;
1600    return(BTM_SUCCESS);
1601#else
1602    *p_name = NULL;
1603    return(BTM_NO_RESOURCES);
1604#endif
1605}
1606
1607
1608/*******************************************************************************
1609**
1610** Function         BTM_ReadLocalDeviceNameFromController
1611**
1612** Description      Get local device name from controller. Do not use cached
1613**                  name (used to get chip-id prior to btm reset complete).
1614**
1615** Returns          BTM_CMD_STARTED if successful, otherwise an error
1616**
1617*******************************************************************************/
1618tBTM_STATUS BTM_ReadLocalDeviceNameFromController (tBTM_CMPL_CB *p_rln_cmpl_cback)
1619{
1620    /* Check if rln already in progress */
1621    if (btm_cb.devcb.p_rln_cmpl_cb)
1622        return(BTM_NO_RESOURCES);
1623
1624    /* Save callback */
1625    btm_cb.devcb.p_rln_cmpl_cb = p_rln_cmpl_cback;
1626
1627    btsnd_hcic_read_name();
1628    btu_start_timer (&btm_cb.devcb.rln_timer, BTU_TTYPE_BTM_DEV_CTL, BTM_DEV_REPLY_TIMEOUT);
1629
1630    return BTM_CMD_STARTED;
1631}
1632
1633/*******************************************************************************
1634**
1635** Function         btm_read_local_name_complete
1636**
1637** Description      This function is called when local name read complete.
1638**                  message is received from the HCI.
1639**
1640** Returns          void
1641**
1642*******************************************************************************/
1643void btm_read_local_name_complete (UINT8 *p, UINT16 evt_len)
1644{
1645    tBTM_CMPL_CB   *p_cb = btm_cb.devcb.p_rln_cmpl_cb;
1646    UINT8           status;
1647
1648    btu_stop_timer (&btm_cb.devcb.rln_timer);
1649
1650    /* If there was a callback address for read local name, call it */
1651    btm_cb.devcb.p_rln_cmpl_cb = NULL;
1652
1653    if (p_cb)
1654    {
1655        STREAM_TO_UINT8  (status, p);
1656
1657        if (status == HCI_SUCCESS)
1658            (*p_cb)(p);
1659        else
1660            (*p_cb)(NULL);
1661    }
1662}
1663
1664
1665/*******************************************************************************
1666**
1667** Function         BTM_GetLocalDeviceAddr
1668**
1669** Description      This function is called to read the local device address
1670**
1671** Returns          void
1672**                  the local device address is copied into bd_addr
1673**
1674*******************************************************************************/
1675void BTM_GetLocalDeviceAddr (BD_ADDR bd_addr)
1676{
1677    memcpy (bd_addr, btm_cb.devcb.local_addr, BD_ADDR_LEN);
1678}
1679
1680/*******************************************************************************
1681**
1682** Function         BTM_ReadLocalDeviceAddr
1683**
1684** Description      This function is called to read the local device address
1685**
1686** Returns          status of the operation
1687**
1688*******************************************************************************/
1689tBTM_STATUS BTM_ReadLocalDeviceAddr (tBTM_CMPL_CB *p_cb)
1690{
1691    if(p_cb)
1692        (*p_cb)(btm_cb.devcb.local_addr);
1693
1694    return (BTM_SUCCESS);
1695}
1696
1697
1698/*******************************************************************************
1699**
1700** Function         btm_read_local_addr_complete
1701**
1702** Description      This function is called when local BD Addr read complete
1703**                  message is received from the HCI.
1704**
1705** Returns          void
1706**
1707*******************************************************************************/
1708void btm_read_local_addr_complete (UINT8 *p, UINT16 evt_len)
1709{
1710    UINT8           status;
1711
1712    STREAM_TO_UINT8  (status, p);
1713
1714    if (status == HCI_SUCCESS)
1715    {
1716        STREAM_TO_BDADDR (btm_cb.devcb.local_addr, p);
1717    }
1718}
1719
1720
1721/*******************************************************************************
1722**
1723** Function         BTM_ReadLocalVersion
1724**
1725** Description      This function is called to read the local device version
1726**
1727** Returns          status of the operation
1728**
1729*******************************************************************************/
1730tBTM_STATUS BTM_ReadLocalVersion (tBTM_VERSION_INFO *p_vers)
1731{
1732    /* Make sure the device has retrieved the info (not being reset) */
1733    if (btm_cb.devcb.state < BTM_DEV_STATE_READY)
1734        return (BTM_DEV_RESET);
1735
1736    *p_vers = btm_cb.devcb.local_version;
1737
1738    return (BTM_SUCCESS);
1739}
1740
1741
1742
1743
1744/*******************************************************************************
1745**
1746** Function         BTM_SetDeviceClass
1747**
1748** Description      This function is called to set the local device class
1749**
1750** Returns          status of the operation
1751**
1752*******************************************************************************/
1753tBTM_STATUS BTM_SetDeviceClass (DEV_CLASS dev_class)
1754{
1755    if(!memcmp (btm_cb.devcb.dev_class, dev_class, DEV_CLASS_LEN))
1756        return(BTM_SUCCESS);
1757
1758    memcpy (btm_cb.devcb.dev_class, dev_class, DEV_CLASS_LEN);
1759
1760    if (btm_cb.devcb.state == BTM_DEV_STATE_WAIT_RESET_CMPLT ||
1761        btm_cb.devcb.state == BTM_DEV_STATE_WAIT_AFTER_RESET)
1762        return (BTM_DEV_RESET);
1763
1764    if (!btsnd_hcic_write_dev_class (dev_class))
1765        return (BTM_NO_RESOURCES);
1766
1767    return (BTM_SUCCESS);
1768}
1769
1770
1771/*******************************************************************************
1772**
1773** Function         BTM_ReadDeviceClass
1774**
1775** Description      This function is called to read the local device class
1776**
1777** Returns          pointer to the device class
1778**
1779*******************************************************************************/
1780UINT8 *BTM_ReadDeviceClass (void)
1781{
1782    return ((UINT8 *)btm_cb.devcb.dev_class);
1783}
1784
1785
1786/*******************************************************************************
1787**
1788** Function         BTM_ReadLocalFeatures
1789**
1790** Description      This function is called to read the local features
1791**
1792** Returns          pointer to the local features string
1793**
1794*******************************************************************************/
1795UINT8 *BTM_ReadLocalFeatures (void)
1796{
1797    return (btm_cb.devcb.local_lmp_features[HCI_EXT_FEATURES_PAGE_0]);
1798}
1799
1800/*******************************************************************************
1801**
1802** Function         BTM_ReadLocalExtendedFeatures
1803**
1804** Description      This function is called to read the local extended features
1805**
1806** Returns          pointer to the local extended features mask or NULL if bad
1807**                  page
1808**
1809*******************************************************************************/
1810UINT8 *BTM_ReadLocalExtendedFeatures (UINT8 page_number)
1811{
1812    if (page_number <= HCI_EXT_FEATURES_PAGE_MAX)
1813        return (btm_cb.devcb.local_lmp_features[page_number]);
1814
1815    BTM_TRACE_ERROR1("Warning: BTM_ReadLocalExtendedFeatures page %d unknown",
1816            page_number);
1817    return NULL;
1818}
1819
1820/*******************************************************************************
1821**
1822** Function         BTM_ReadBrcmFeatures
1823**
1824** Description      This function is called to read the Broadcom specific features
1825**
1826** Returns          pointer to the Broadcom features string
1827**
1828*******************************************************************************/
1829UINT8 *BTM_ReadBrcmFeatures (void)
1830{
1831    return (btm_cb.devcb.brcm_features);
1832}
1833
1834/*******************************************************************************
1835**
1836** Function         BTM_RegisterForDeviceStatusNotif
1837**
1838** Description      This function is called to register for device status
1839**                  change notifications.
1840**
1841**                  If one registration is already there calling function should
1842**                  save the pointer to the function that is return and
1843**                  call it when processing of the event is complete
1844**
1845** Returns          status of the operation
1846**
1847*******************************************************************************/
1848tBTM_DEV_STATUS_CB *BTM_RegisterForDeviceStatusNotif (tBTM_DEV_STATUS_CB *p_cb)
1849{
1850    tBTM_DEV_STATUS_CB *p_prev = btm_cb.devcb.p_dev_status_cb;
1851
1852    btm_cb.devcb.p_dev_status_cb = p_cb;
1853    return (p_prev);
1854}
1855
1856/*******************************************************************************
1857**
1858** Function         BTM_VendorSpecificCommand
1859**
1860** Description      Send a vendor specific HCI command to the controller.
1861**
1862** Returns
1863**      BTM_SUCCESS         Command sent. Does not expect command complete
1864**                              event. (command cmpl callback param is NULL)
1865**      BTM_CMD_STARTED     Command sent. Waiting for command cmpl event.
1866**
1867** Notes
1868**      Opcode will be OR'd with HCI_GRP_VENDOR_SPECIFIC.
1869**
1870*******************************************************************************/
1871tBTM_STATUS BTM_VendorSpecificCommand(UINT16 opcode, UINT8 param_len,
1872                                      UINT8 *p_param_buf, tBTM_VSC_CMPL_CB *p_cb)
1873{
1874    void *p_buf;
1875
1876    BTM_TRACE_EVENT2 ("BTM: BTM_VendorSpecificCommand: Opcode: 0x%04X, ParamLen: %i.",
1877                      opcode, param_len);
1878
1879    /* Allocate a buffer to hold HCI command plus the callback function */
1880    if ((p_buf = GKI_getbuf((UINT16)(sizeof(BT_HDR) + sizeof (tBTM_CMPL_CB *) +
1881                            param_len + HCIC_PREAMBLE_SIZE))) != NULL)
1882    {
1883        /* Send the HCI command (opcode will be OR'd with HCI_GRP_VENDOR_SPECIFIC) */
1884        btsnd_hcic_vendor_spec_cmd (p_buf, opcode, param_len, p_param_buf, (void *)p_cb);
1885
1886        /* Return value */
1887        if (p_cb != NULL)
1888            return (BTM_CMD_STARTED);
1889        else
1890            return (BTM_SUCCESS);
1891    }
1892    else
1893        return (BTM_NO_RESOURCES);
1894
1895}
1896
1897
1898/*******************************************************************************
1899**
1900** Function         btm_vsc_complete
1901**
1902** Description      This function is called when local HCI Vendor Specific
1903**                  Command complete message is received from the HCI.
1904**
1905** Returns          void
1906**
1907*******************************************************************************/
1908void btm_vsc_complete (UINT8 *p, UINT16 opcode, UINT16 evt_len,
1909                       tBTM_CMPL_CB *p_vsc_cplt_cback)
1910{
1911    tBTM_VSC_CMPL   vcs_cplt_params;
1912
1913    /* If there was a callback address for vcs complete, call it */
1914    if (p_vsc_cplt_cback)
1915    {
1916        /* Pass paramters to the callback function */
1917        vcs_cplt_params.opcode = opcode;        /* Number of bytes in return info */
1918        vcs_cplt_params.param_len = evt_len;    /* Number of bytes in return info */
1919        vcs_cplt_params.p_param_buf = p;
1920        (*p_vsc_cplt_cback)(&vcs_cplt_params);  /* Call the VSC complete callback function */
1921    }
1922}
1923
1924/*******************************************************************************
1925**
1926** Function         BTM_RegisterForVSEvents
1927**
1928** Description      This function is called to register/deregister for vendor
1929**                  specific HCI events.
1930**
1931**                  If is_register=TRUE, then the function will be registered;
1932**                  if is_register=FALSE, then the function will be deregistered.
1933**
1934** Returns          BTM_SUCCESS if successful,
1935**                  BTM_BUSY if maximum number of callbacks have already been
1936**                           registered.
1937**
1938*******************************************************************************/
1939tBTM_STATUS BTM_RegisterForVSEvents (tBTM_VS_EVT_CB *p_cb, BOOLEAN is_register)
1940{
1941    tBTM_STATUS retval = BTM_SUCCESS;
1942    UINT8 i, free_idx = BTM_MAX_VSE_CALLBACKS;
1943
1944    /* See if callback is already registered */
1945    for (i=0; i<BTM_MAX_VSE_CALLBACKS; i++)
1946    {
1947        if (btm_cb.devcb.p_vend_spec_cb[i] == NULL)
1948        {
1949            /* Found a free slot. Store index */
1950            free_idx = i;
1951        }
1952        else if (btm_cb.devcb.p_vend_spec_cb[i] == p_cb)
1953        {
1954            /* Found callback in lookup table. If deregistering, clear the entry. */
1955            if (is_register == FALSE)
1956            {
1957                btm_cb.devcb.p_vend_spec_cb[i] = NULL;
1958                BTM_TRACE_EVENT0("BTM Deregister For VSEvents is successfully");
1959            }
1960            return (BTM_SUCCESS);
1961        }
1962    }
1963
1964    /* Didn't find callback. Add callback to free slot if registering */
1965    if (is_register)
1966    {
1967        if (free_idx < BTM_MAX_VSE_CALLBACKS)
1968        {
1969            btm_cb.devcb.p_vend_spec_cb[free_idx] = p_cb;
1970            BTM_TRACE_EVENT0("BTM Register For VSEvents is successfully");
1971        }
1972        else
1973        {
1974            /* No free entries available */
1975            BTM_TRACE_ERROR0 ("BTM_RegisterForVSEvents: too many callbacks registered");
1976
1977            retval = BTM_NO_RESOURCES;
1978        }
1979    }
1980
1981    return (retval);
1982}
1983
1984/*******************************************************************************
1985**
1986** Function         btm_vendor_specific_evt
1987**
1988** Description      Process event HCI_VENDOR_SPECIFIC_EVT
1989**
1990**                  Note: Some controllers do not send command complete, so
1991**                  the callback and busy flag are cleared here also.
1992**
1993** Returns          void
1994**
1995*******************************************************************************/
1996void btm_vendor_specific_evt (UINT8 *p, UINT8 evt_len)
1997{
1998    UINT8 i;
1999
2000    BTM_TRACE_DEBUG0 ("BTM Event: Vendor Specific event from controller");
2001
2002    for (i=0; i<BTM_MAX_VSE_CALLBACKS; i++)
2003    {
2004        if (btm_cb.devcb.p_vend_spec_cb[i])
2005            (*btm_cb.devcb.p_vend_spec_cb[i])(evt_len, p);
2006    }
2007}
2008
2009
2010/*******************************************************************************
2011**
2012** Function         BTM_WritePageTimeout
2013**
2014** Description      Send HCI Write Page Timeout.
2015**
2016** Returns
2017**      BTM_SUCCESS         Command sent.
2018**      BTM_NO_RESOURCES     If out of resources to send the command.
2019**
2020**
2021*******************************************************************************/
2022tBTM_STATUS BTM_WritePageTimeout(UINT16 timeout)
2023{
2024    BTM_TRACE_EVENT1 ("BTM: BTM_WritePageTimeout: Timeout: %d.", timeout);
2025
2026    /* Send the HCI command */
2027    if (btsnd_hcic_write_page_tout (timeout))
2028        return (BTM_SUCCESS);
2029    else
2030        return (BTM_NO_RESOURCES);
2031}
2032
2033/*******************************************************************************
2034**
2035** Function         BTM_WriteVoiceSettings
2036**
2037** Description      Send HCI Write Voice Settings command.
2038**                  See hcidefs.h for settings bitmask values.
2039**
2040** Returns
2041**      BTM_SUCCESS         Command sent.
2042**      BTM_NO_RESOURCES     If out of resources to send the command.
2043**
2044**
2045*******************************************************************************/
2046tBTM_STATUS BTM_WriteVoiceSettings(UINT16 settings)
2047{
2048    BTM_TRACE_EVENT1 ("BTM: BTM_WriteVoiceSettings: Settings: 0x%04x.", settings);
2049
2050    /* Send the HCI command */
2051    if (btsnd_hcic_write_voice_settings ((UINT16)(settings & 0x03ff)))
2052        return (BTM_SUCCESS);
2053
2054    return (BTM_NO_RESOURCES);
2055}
2056
2057/*******************************************************************************
2058**
2059** Function         BTM_EnableTestMode
2060**
2061** Description      Send HCI the enable device under test command.
2062**
2063**                  Note: Controller can only be taken out of this mode by
2064**                      resetting the controller.
2065**
2066** Returns
2067**      BTM_SUCCESS         Command sent.
2068**      BTM_NO_RESOURCES    If out of resources to send the command.
2069**
2070**
2071*******************************************************************************/
2072tBTM_STATUS BTM_EnableTestMode(void)
2073{
2074    UINT8   cond;
2075
2076    BTM_TRACE_EVENT0 ("BTM: BTM_EnableTestMode");
2077
2078    /* set auto accept connection as this is needed during test mode */
2079    /* Allocate a buffer to hold HCI command */
2080    cond = HCI_DO_AUTO_ACCEPT_CONNECT;
2081    if (!btsnd_hcic_set_event_filter(HCI_FILTER_CONNECTION_SETUP,
2082                                     HCI_FILTER_COND_NEW_DEVICE,
2083                                     &cond, sizeof(cond)))
2084    {
2085        return (BTM_NO_RESOURCES);
2086    }
2087
2088    /* put device to connectable mode */
2089    if (!BTM_SetConnectability(BTM_CONNECTABLE, BTM_DEFAULT_CONN_WINDOW,
2090                               BTM_DEFAULT_CONN_INTERVAL) == BTM_SUCCESS)
2091    {
2092        return BTM_NO_RESOURCES;
2093    }
2094
2095    /* put device to discoverable mode */
2096    if (!BTM_SetDiscoverability(BTM_GENERAL_DISCOVERABLE, BTM_DEFAULT_DISC_WINDOW,
2097                                BTM_DEFAULT_DISC_INTERVAL) == BTM_SUCCESS)
2098    {
2099        return BTM_NO_RESOURCES;
2100    }
2101
2102    /* mask off all of event from controller */
2103    if (!btsnd_hcic_set_event_mask(LOCAL_BR_EDR_CONTROLLER_ID,
2104                                   (UINT8 *)"\x00\x00\x00\x00\x00\x00\x00\x00"))
2105    {
2106        return BTM_NO_RESOURCES;
2107    }
2108
2109    /* Send the HCI command */
2110    if (btsnd_hcic_enable_test_mode ())
2111        return (BTM_SUCCESS);
2112    else
2113        return (BTM_NO_RESOURCES);
2114}
2115
2116/*******************************************************************************
2117**
2118** Function         btm_get_hci_version
2119**
2120** Description      Local function called to retrieve the current HCI version
2121**
2122** Returns          Bluetooth HCI Version returned by the controller
2123**
2124*******************************************************************************/
2125UINT8 btm_get_hci_version (void)
2126{
2127    return (btm_cb.devcb.local_version.hci_version);
2128}
2129
2130
2131
2132/*******************************************************************************
2133**
2134** Function         BTM_ReadStoredLinkKey
2135**
2136** Description      This function is called to obtain link key for the specified
2137**                  device from the NVRAM storage attached to the Bluetooth
2138**                  controller.
2139**
2140** Parameters:      bd_addr      - Address of the device
2141**                  p_cb         - Call back function to be called to return
2142**                                 the results
2143**
2144*******************************************************************************/
2145tBTM_STATUS BTM_ReadStoredLinkKey (BD_ADDR bd_addr,	tBTM_CMPL_CB *p_cb)
2146{
2147    BD_ADDR local_bd_addr;
2148    BOOLEAN read_all_flag = FALSE;
2149
2150    /* Check if the previous command is completed */
2151    if (btm_cb.devcb.p_stored_link_key_cmpl_cb)
2152        return (BTM_BUSY);
2153
2154    if (!bd_addr)
2155    {
2156        /* This is to read all the link keys */
2157        read_all_flag = TRUE;
2158
2159        /* We don't care the BD address. Just pass a non zero pointer */
2160        bd_addr = local_bd_addr;
2161    }
2162
2163    BTM_TRACE_EVENT1 ("BTM: BTM_ReadStoredLinkKey: Read_All: %s",
2164                        read_all_flag ? "TRUE" : "FALSE");
2165
2166    /* Send the HCI command */
2167    btm_cb.devcb.p_stored_link_key_cmpl_cb = p_cb;
2168    if (btsnd_hcic_read_stored_key (bd_addr, read_all_flag))
2169        return (BTM_SUCCESS);
2170    else
2171        return (BTM_NO_RESOURCES);
2172
2173}
2174
2175
2176/*******************************************************************************
2177**
2178** Function         BTM_WriteStoredLinkKey
2179**
2180** Description      This function is called to write link keys for the specified
2181**                  device addresses to the NVRAM storage attached to the Bluetooth
2182**                  controller.
2183**
2184** Parameters:      num_keys     - Number of link keys
2185**                  bd_addr      - Addresses of the devices
2186**                  link_key     - Link Keys to be stored
2187**                  p_cb         - Call back function to be called to return
2188**                                 the results
2189**
2190*******************************************************************************/
2191tBTM_STATUS BTM_WriteStoredLinkKey (UINT8 num_keys,
2192                                    BD_ADDR *bd_addr,
2193                                    LINK_KEY *link_key,
2194                                    tBTM_CMPL_CB *p_cb)
2195{
2196    /* Check if the previous command is completed */
2197    if (btm_cb.devcb.p_stored_link_key_cmpl_cb)
2198        return (BTM_BUSY);
2199
2200    BTM_TRACE_EVENT1 ("BTM: BTM_WriteStoredLinkKey: num_keys: %d", num_keys);
2201
2202    /* Check the maximum number of link keys */
2203    if(num_keys > HCI_MAX_NUM_OF_LINK_KEYS_PER_CMMD)
2204        num_keys = HCI_MAX_NUM_OF_LINK_KEYS_PER_CMMD;
2205
2206    /* Send the HCI command */
2207    btm_cb.devcb.p_stored_link_key_cmpl_cb = p_cb;
2208    if (btsnd_hcic_write_stored_key (num_keys, bd_addr, link_key))
2209        return (BTM_SUCCESS);
2210    else
2211        return (BTM_NO_RESOURCES);
2212
2213}
2214
2215
2216/*******************************************************************************
2217**
2218** Function         BTM_DeleteStoredLinkKey
2219**
2220** Description      This function is called to delete link key for the specified
2221**                  device addresses from the NVRAM storage attached to the Bluetooth
2222**                  controller.
2223**
2224** Parameters:      bd_addr      - Addresses of the devices
2225**                  p_cb         - Call back function to be called to return
2226**                                 the results
2227**
2228*******************************************************************************/
2229tBTM_STATUS BTM_DeleteStoredLinkKey(BD_ADDR bd_addr, tBTM_CMPL_CB *p_cb)
2230{
2231    BD_ADDR local_bd_addr;
2232    BOOLEAN delete_all_flag = FALSE;
2233
2234    /* Check if the previous command is completed */
2235    if (btm_cb.devcb.p_stored_link_key_cmpl_cb)
2236        return (BTM_BUSY);
2237
2238    if (!bd_addr)
2239    {
2240        /* This is to delete all link keys */
2241        delete_all_flag = TRUE;
2242
2243        /* We don't care the BD address. Just pass a non zero pointer */
2244        bd_addr = local_bd_addr;
2245    }
2246
2247    BTM_TRACE_EVENT1 ("BTM: BTM_DeleteStoredLinkKey: delete_all_flag: %s",
2248                        delete_all_flag ? "TRUE" : "FALSE");
2249
2250    /* Send the HCI command */
2251    btm_cb.devcb.p_stored_link_key_cmpl_cb = p_cb;
2252    if (!btsnd_hcic_delete_stored_key (bd_addr, delete_all_flag))
2253    {
2254        return (BTM_NO_RESOURCES);
2255    }
2256    else
2257        return (BTM_SUCCESS);
2258}
2259
2260
2261/*******************************************************************************
2262**
2263** Function         btm_read_stored_link_key_complete
2264**
2265** Description      This function is called when the command complete message
2266**                  is received from the HCI for the read stored link key command.
2267**
2268** Returns          void
2269**
2270*******************************************************************************/
2271void btm_read_stored_link_key_complete (UINT8 *p)
2272{
2273    tBTM_CMPL_CB      *p_cb = btm_cb.devcb.p_stored_link_key_cmpl_cb;
2274    tBTM_READ_STORED_LINK_KEY_COMPLETE  result;
2275
2276    /* If there was a callback registered for read stored link key, call it */
2277    btm_cb.devcb.p_stored_link_key_cmpl_cb = NULL;
2278
2279    if (p_cb)
2280    {
2281        /* Set the call back event to indicate command complete */
2282        result.event = BTM_CB_EVT_READ_STORED_LINK_KEYS;
2283
2284        /* Extract the result fields from the HCI event if status is success */
2285        STREAM_TO_UINT8  (result.status, p);
2286        if (result.status == HCI_SUCCESS)
2287        {
2288            STREAM_TO_UINT16 (result.max_keys, p);
2289            STREAM_TO_UINT16 (result.read_keys, p);
2290        }
2291        else
2292        {
2293            BTM_TRACE_WARNING1("Read stored link key status %d", result.status);
2294            result.max_keys = 0;
2295            result.read_keys = 0;
2296        }
2297        /* Call the call back and pass the result */
2298        (*p_cb)(&result);
2299    }
2300}
2301
2302
2303/*******************************************************************************
2304**
2305** Function         btm_write_stored_link_key_complete
2306**
2307** Description      This function is called when the command complete message
2308**                  is received from the HCI for the write stored link key command.
2309**
2310** Returns          void
2311**
2312*******************************************************************************/
2313void btm_write_stored_link_key_complete (UINT8 *p)
2314{
2315    tBTM_CMPL_CB       *p_cb = btm_cb.devcb.p_stored_link_key_cmpl_cb;
2316    tBTM_WRITE_STORED_LINK_KEY_COMPLETE  result;
2317
2318    /* If there was a callback registered for read stored link key, call it */
2319    btm_cb.devcb.p_stored_link_key_cmpl_cb = NULL;
2320
2321    if (p_cb)
2322    {
2323        /* Set the call back event to indicate command complete */
2324        result.event = BTM_CB_EVT_WRITE_STORED_LINK_KEYS;
2325
2326        /* Extract the result fields from the HCI event */
2327        STREAM_TO_UINT8 (result.status, p);
2328        STREAM_TO_UINT8 (result.num_keys, p);
2329
2330        /* Call the call back and pass the result */
2331        (*p_cb)(&result);
2332    }
2333}
2334
2335
2336/*******************************************************************************
2337**
2338** Function         btm_delete_stored_link_key_complete
2339**
2340** Description      This function is called when the command complete message
2341**                  is received from the HCI for the delete stored link key command.
2342**
2343** Returns          void
2344**
2345*******************************************************************************/
2346void btm_delete_stored_link_key_complete (UINT8 *p)
2347{
2348    tBTM_CMPL_CB         *p_cb = btm_cb.devcb.p_stored_link_key_cmpl_cb;
2349    tBTM_DELETE_STORED_LINK_KEY_COMPLETE  result;
2350
2351    /* If there was a callback registered for read stored link key, call it */
2352    btm_cb.devcb.p_stored_link_key_cmpl_cb = NULL;
2353
2354    if (p_cb)
2355    {
2356        /* Set the call back event to indicate command complete */
2357        result.event = BTM_CB_EVT_DELETE_STORED_LINK_KEYS;
2358
2359        /* Extract the result fields from the HCI event */
2360        STREAM_TO_UINT8  (result.status, p);
2361        STREAM_TO_UINT16 (result.num_keys, p);
2362
2363        /* Call the call back and pass the result */
2364        (*p_cb)(&result);
2365    }
2366}
2367
2368
2369/*******************************************************************************
2370**
2371** Function         btm_return_link_keys_evt
2372**
2373** Description      This function is called when the return link keys event
2374**                  is received from the HCI for the read stored link key command.
2375**
2376** Returns          void
2377**
2378*******************************************************************************/
2379void btm_return_link_keys_evt (tBTM_RETURN_LINK_KEYS_EVT *result)
2380{
2381    tBTM_CMPL_CB  *p_cb = btm_cb.devcb.p_stored_link_key_cmpl_cb;
2382    UINT8          i, *p, *p1;
2383    UINT8          bd_addr[BD_ADDR_LEN];
2384    UINT8          link_key[LINK_KEY_LEN];
2385
2386    /* Call the call back to pass the link keys to application */
2387    if (p_cb)
2388    {
2389        /* Change the BD addr and Link key in to big endian order */
2390        p = (UINT8 *)(result + 1);
2391        for (i=0; i<result->num_keys; i++)
2392        {
2393            /* Initialize the backup pointer */
2394            p1 = p;
2395
2396            /* Extract the BD Addr and Link Key */
2397            REVERSE_STREAM_TO_ARRAY(bd_addr, p1, BD_ADDR_LEN);
2398            REVERSE_STREAM_TO_ARRAY(link_key, p1, LINK_KEY_LEN);
2399
2400            /* Write the BD Addr and Link Key back in big endian format */
2401            ARRAY_TO_STREAM(p, bd_addr, BD_ADDR_LEN);
2402            ARRAY_TO_STREAM(p, link_key, LINK_KEY_LEN);
2403        }
2404
2405        (*p_cb)(result);
2406    }
2407}
2408
2409
2410
2411/*******************************************************************************
2412**
2413** Function         btm_report_device_status
2414**
2415** Description      This function is called when there is a change in the device
2416**                  status. This function will report the new device status to
2417**                  the application
2418**
2419** Returns          void
2420**
2421*******************************************************************************/
2422void btm_report_device_status (tBTM_DEV_STATUS status)
2423{
2424    tBTM_DEV_STATUS_CB *p_cb = btm_cb.devcb.p_dev_status_cb;
2425
2426    /* Call the call back to pass the device status to application */
2427    if (p_cb)
2428        (*p_cb)(status);
2429}
2430
2431
2432