OSDN Git Service

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