• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 the main Bluetooth Upper Layer processing loop.
22  *  The Broadcom implementations of L2CAP RFCOMM, SDP and the BTIf run as one
23  *  GKI task. This btu_task switches between them.
24  *
25  *  Note that there will always be an L2CAP, but there may or may not be an
26  *  RFCOMM or SDP. Whether these layers are present or not is determined by
27  *  compile switches.
28  *
29  ******************************************************************************/
30 
31 #include <stdlib.h>
32 #include <string.h>
33 #include <stdio.h>
34 
35 #include "bt_target.h"
36 #include "gki.h"
37 #include "bt_types.h"
38 #include "hcimsgs.h"
39 #include "l2c_int.h"
40 #include "btu.h"
41 #include "bt_utils.h"
42 
43 #include "sdpint.h"
44 
45 #if ( defined(RFCOMM_INCLUDED) && RFCOMM_INCLUDED == TRUE )
46 #include "port_api.h"
47 #include "port_ext.h"
48 #endif
49 
50 #include "btm_api.h"
51 #include "btm_int.h"
52 
53 #if (defined(EVAL) && EVAL == TRUE)
54 #include "btu_eval.h"
55 #endif
56 
57 #if GAP_INCLUDED == TRUE
58 #include "gap_int.h"
59 #endif
60 
61 #if (defined(OBX_INCLUDED) && OBX_INCLUDED == TRUE)
62 #include "obx_int.h"
63 
64 #if (defined(BIP_INCLUDED) && BIP_INCLUDED == TRUE)
65 #include "bip_int.h"
66 #endif /* BIP */
67 
68 #if (BPP_SND_INCLUDED == TRUE ||  BPP_INCLUDED == TRUE)
69 #include "bpp_int.h"
70 #endif /* BPP */
71 
72 #endif /* OBX */
73 
74 #include "bt_trace.h"
75 
76 /* BTE application task */
77 #if APPL_INCLUDED == TRUE
78 #include "bte_appl.h"
79 #endif
80 
81 #if (defined(RPC_INCLUDED) && RPC_INCLUDED == TRUE)
82 #include "rpct_main.h"
83 #endif
84 
85 #if (defined(BNEP_INCLUDED) && BNEP_INCLUDED == TRUE)
86 #include "bnep_int.h"
87 #endif
88 
89 #if (defined(PAN_INCLUDED) && PAN_INCLUDED == TRUE)
90 #include "pan_int.h"
91 #endif
92 
93 #if (defined(SAP_SERVER_INCLUDED) && SAP_SERVER_INCLUDED == TRUE)
94 #include "sap_int.h"
95 #endif
96 
97 #if (defined(HID_DEV_INCLUDED) && HID_DEV_INCLUDED == TRUE )
98 #include "hidd_int.h"
99 #endif
100 
101 #if (defined(HID_HOST_INCLUDED) && HID_HOST_INCLUDED == TRUE )
102 #include "hidh_int.h"
103 #endif
104 
105 #if (defined(AVDT_INCLUDED) && AVDT_INCLUDED == TRUE)
106 #include "avdt_int.h"
107 #else
108 extern void avdt_rcv_sync_info (BT_HDR *p_buf); /* this is for hci_test */
109 #endif
110 
111 #if (defined(MCA_INCLUDED) && MCA_INCLUDED == TRUE)
112 #include "mca_api.h"
113 #include "mca_defs.h"
114 #include "mca_int.h"
115 #endif
116 
117 
118 #if (defined(BTU_BTA_INCLUDED) && BTU_BTA_INCLUDED == TRUE)
119 #include "bta_sys.h"
120 #endif
121 
122 #if (BLE_INCLUDED == TRUE)
123 #include "gatt_int.h"
124 #if (SMP_INCLUDED == TRUE)
125 #include "smp_int.h"
126 #endif
127 #include "btm_ble_int.h"
128 #endif
129 
130 #ifdef __cplusplus
131 extern "C"
132 {
133 #endif
134 
135 BT_API extern void BTE_InitStack(void);
136 
137 #ifdef __cplusplus
138 }
139 #endif
140 
141 /* Define BTU storage area
142 */
143 #if BTU_DYNAMIC_MEMORY == FALSE
144 tBTU_CB  btu_cb;
145 #endif
146 
147 
148 /* Define a function prototype to allow a generic timeout handler */
149 typedef void (tUSER_TIMEOUT_FUNC) (TIMER_LIST_ENT *p_tle);
150 
151 /*******************************************************************************
152 **
153 ** Function         btu_task
154 **
155 ** Description      This is the main task of the Bluetooth Upper Layers unit.
156 **                  It sits in a loop waiting for messages, and dispatches them
157 **                  to the appropiate handlers.
158 **
159 ** Returns          should never return
160 **
161 *******************************************************************************/
btu_task(UINT32 param)162 BTU_API UINT32 btu_task (UINT32 param)
163 {
164     UINT16           event;
165     BT_HDR          *p_msg;
166     UINT8            i;
167     UINT16           mask;
168     BOOLEAN          handled;
169 
170 #if (defined(HCISU_H4_INCLUDED) && HCISU_H4_INCLUDED == TRUE)
171     /* wait an event that HCISU is ready */
172     GKI_wait(0xFFFF, 0);
173 #endif
174     /* Initialize the mandatory core stack control blocks
175        (BTU, BTM, L2CAP, and SDP)
176      */
177     btu_init_core();
178 
179     /* Initialize any optional stack components */
180     BTE_InitStack();
181 
182 #if (defined(BTU_BTA_INCLUDED) && BTU_BTA_INCLUDED == TRUE)
183     bta_sys_init();
184 #endif
185 
186     /* Initialise platform trace levels at this point as BTE_InitStack() and bta_sys_init()
187      * reset the control blocks and preset the trace level with XXX_INITIAL_TRACE_LEVEL
188      */
189 #if ( BT_USE_TRACES==TRUE )
190     BTE_InitTraceLevels();
191 #endif
192 
193     /* Send a startup evt message to BTIF_TASK to kickstart the init procedure */
194     GKI_send_event(BTIF_TASK, BT_EVT_TRIGGER_STACK_INIT);
195 
196     raise_priority_a2dp(TASK_HIGH_BTU);
197 
198     /* Wait for, and process, events */
199     for (;;)
200     {
201         event = GKI_wait (0xFFFF, 0);
202 
203         if (event & TASK_MBOX_0_EVT_MASK)
204         {
205             /* Process all messages in the queue */
206             while ((p_msg = (BT_HDR *) GKI_read_mbox (BTU_HCI_RCV_MBOX)) != NULL)
207             {
208                 /* Determine the input message type. */
209                 switch (p_msg->event & BT_EVT_MASK)
210                 {
211                     case BT_EVT_TO_BTU_HCI_ACL:
212                         /* All Acl Data goes to L2CAP */
213                         l2c_rcv_acl_data (p_msg);
214                         break;
215 
216                     case BT_EVT_TO_BTU_L2C_SEG_XMIT:
217                         /* L2CAP segment transmit complete */
218                         l2c_link_segments_xmitted (p_msg);
219                         break;
220 
221                     case BT_EVT_TO_BTU_HCI_SCO:
222 #if BTM_SCO_INCLUDED == TRUE
223                         btm_route_sco_data (p_msg);
224                         break;
225 #endif
226 
227                     case BT_EVT_TO_BTU_HCI_EVT:
228                         btu_hcif_process_event ((UINT8)(p_msg->event & BT_SUB_EVT_MASK), p_msg);
229                         GKI_freebuf(p_msg);
230 
231 #if (defined(HCILP_INCLUDED) && HCILP_INCLUDED == TRUE)
232                         /* If host receives events which it doesn't response to, */
233                         /* host should start idle timer to enter sleep mode.     */
234                         btu_check_bt_sleep ();
235 #endif
236                         break;
237 
238                     case BT_EVT_TO_BTU_HCI_CMD:
239                         btu_hcif_send_cmd ((UINT8)(p_msg->event & BT_SUB_EVT_MASK), p_msg);
240                         break;
241 
242 #if (defined(OBX_INCLUDED) && OBX_INCLUDED == TRUE)
243 #if (defined(OBX_SERVER_INCLUDED) && OBX_SERVER_INCLUDED == TRUE)
244                     case BT_EVT_TO_OBX_SR_MSG:
245                         obx_sr_proc_evt((tOBX_PORT_EVT *)(p_msg + 1));
246                         GKI_freebuf (p_msg);
247                         break;
248 
249                     case BT_EVT_TO_OBX_SR_L2C_MSG:
250                         obx_sr_proc_l2c_evt((tOBX_L2C_EVT_MSG *)(p_msg + 1));
251                         GKI_freebuf (p_msg);
252                         break;
253 #endif
254 
255 #if (defined(OBX_CLIENT_INCLUDED) && OBX_CLIENT_INCLUDED == TRUE)
256                     case BT_EVT_TO_OBX_CL_MSG:
257                         obx_cl_proc_evt((tOBX_PORT_EVT *)(p_msg + 1));
258                         GKI_freebuf (p_msg);
259                         break;
260 
261                     case BT_EVT_TO_OBX_CL_L2C_MSG:
262                         obx_cl_proc_l2c_evt((tOBX_L2C_EVT_MSG *)(p_msg + 1));
263                         GKI_freebuf (p_msg);
264                         break;
265 #endif
266 
267 #if (defined(BIP_INCLUDED) && BIP_INCLUDED == TRUE)
268                     case BT_EVT_TO_BIP_CMDS :
269                         bip_proc_btu_event(p_msg);
270                         GKI_freebuf (p_msg);
271                         break;
272 #endif /* BIP */
273 #if (BPP_SND_INCLUDED == TRUE || BPP_INCLUDED == TRUE)
274                     case BT_EVT_TO_BPP_PR_CMDS:
275                         bpp_pr_proc_event(p_msg);
276                         GKI_freebuf (p_msg);
277                         break;
278                     case BT_EVT_TO_BPP_SND_CMDS:
279                         bpp_snd_proc_event(p_msg);
280                         GKI_freebuf (p_msg);
281                         break;
282 
283 #endif /* BPP */
284 
285 #endif /* OBX */
286 
287 #if (defined(SAP_SERVER_INCLUDED) && SAP_SERVER_INCLUDED == TRUE)
288                     case BT_EVT_TO_BTU_SAP :
289                         sap_proc_btu_event(p_msg);
290                         GKI_freebuf (p_msg);
291                         break;
292 #endif /* SAP */
293 #if (defined(GAP_CONN_INCLUDED) && GAP_CONN_INCLUDED == TRUE && GAP_CONN_POST_EVT_INCLUDED == TRUE)
294                     case BT_EVT_TO_GAP_MSG :
295                         gap_proc_btu_event(p_msg);
296                         GKI_freebuf (p_msg);
297                         break;
298 #endif
299                     case BT_EVT_TO_START_TIMER :
300                         /* Start free running 1 second timer for list management */
301                         GKI_start_timer (TIMER_0, GKI_SECS_TO_TICKS (1), TRUE);
302                         GKI_freebuf (p_msg);
303                         break;
304 
305 #if defined(QUICK_TIMER_TICKS_PER_SEC) && (QUICK_TIMER_TICKS_PER_SEC > 0)
306                     case BT_EVT_TO_START_QUICK_TIMER :
307                         GKI_start_timer (TIMER_2, QUICK_TIMER_TICKS, TRUE);
308                         GKI_freebuf (p_msg);
309                         break;
310 #endif
311 
312                     default:
313                         i = 0;
314                         mask = (UINT16) (p_msg->event & BT_EVT_MASK);
315                         handled = FALSE;
316 
317                         for (; !handled && i < BTU_MAX_REG_EVENT; i++)
318                         {
319                             if (btu_cb.event_reg[i].event_cb == NULL)
320                                 continue;
321 
322                             if (mask == btu_cb.event_reg[i].event_range)
323                             {
324                                 if (btu_cb.event_reg[i].event_cb)
325                                 {
326                                     btu_cb.event_reg[i].event_cb(p_msg);
327                                     handled = TRUE;
328                                 }
329                             }
330                         }
331 
332                         if (handled == FALSE)
333                             GKI_freebuf (p_msg);
334 
335                         break;
336                 }
337             }
338         }
339 
340 
341         if (event & TIMER_0_EVT_MASK)
342         {
343             TIMER_LIST_ENT  *p_tle;
344 
345             GKI_update_timer_list (&btu_cb.timer_queue, 1);
346 
347             while ((btu_cb.timer_queue.p_first) && (!btu_cb.timer_queue.p_first->ticks))
348             {
349                 p_tle = btu_cb.timer_queue.p_first;
350                 GKI_remove_from_timer_list (&btu_cb.timer_queue, p_tle);
351 
352                 switch (p_tle->event)
353                 {
354                     case BTU_TTYPE_BTM_DEV_CTL:
355                         btm_dev_timeout(p_tle);
356                         break;
357 
358                     case BTU_TTYPE_BTM_ACL:
359                         btm_acl_timeout(p_tle);
360                         break;
361 
362                     case BTU_TTYPE_L2CAP_LINK:
363                     case BTU_TTYPE_L2CAP_CHNL:
364                     case BTU_TTYPE_L2CAP_HOLD:
365                     case BTU_TTYPE_L2CAP_INFO:
366                     case BTU_TTYPE_L2CAP_FCR_ACK:
367 
368                         l2c_process_timeout (p_tle);
369                         break;
370 
371                     case BTU_TTYPE_SDP:
372                         sdp_conn_timeout ((tCONN_CB *)p_tle->param);
373                         break;
374 
375                     case BTU_TTYPE_BTM_RMT_NAME:
376                         btm_inq_rmt_name_failed();
377                         break;
378 
379 #if (defined(RFCOMM_INCLUDED) && RFCOMM_INCLUDED == TRUE)
380                     case BTU_TTYPE_RFCOMM_MFC:
381                     case BTU_TTYPE_RFCOMM_PORT:
382                         rfcomm_process_timeout (p_tle);
383                         break;
384 
385 #endif /* If defined(RFCOMM_INCLUDED) && RFCOMM_INCLUDED == TRUE */
386 
387 #if ((defined(BNEP_INCLUDED) && BNEP_INCLUDED == TRUE))
388                     case BTU_TTYPE_BNEP:
389                         bnep_process_timeout(p_tle);
390                         break;
391 #endif
392 
393 
394 #if (defined(AVDT_INCLUDED) && AVDT_INCLUDED == TRUE)
395                     case BTU_TTYPE_AVDT_CCB_RET:
396                     case BTU_TTYPE_AVDT_CCB_RSP:
397                     case BTU_TTYPE_AVDT_CCB_IDLE:
398                     case BTU_TTYPE_AVDT_SCB_TC:
399                         avdt_process_timeout(p_tle);
400                         break;
401 #endif
402 
403 #if (defined(OBX_INCLUDED) && OBX_INCLUDED == TRUE)
404 #if (defined(OBX_CLIENT_INCLUDED) && OBX_CLIENT_INCLUDED == TRUE)
405                     case BTU_TTYPE_OBX_CLIENT_TO:
406                         obx_cl_timeout(p_tle);
407                         break;
408 #endif
409 #if (defined(OBX_SERVER_INCLUDED) && OBX_SERVER_INCLUDED == TRUE)
410                     case BTU_TTYPE_OBX_SERVER_TO:
411                         obx_sr_timeout(p_tle);
412                         break;
413 
414                     case BTU_TTYPE_OBX_SVR_SESS_TO:
415                         obx_sr_sess_timeout(p_tle);
416                         break;
417 #endif
418 #endif
419 
420 #if (defined(SAP_SERVER_INCLUDED) && SAP_SERVER_INCLUDED == TRUE)
421                     case BTU_TTYPE_SAP_TO:
422                         sap_process_timeout(p_tle);
423                         break;
424 #endif
425 
426                     case BTU_TTYPE_BTU_CMD_CMPL:
427                         btu_hcif_cmd_timeout((UINT8)(p_tle->event - BTU_TTYPE_BTU_CMD_CMPL));
428                         break;
429 
430 #if (defined(HID_HOST_INCLUDED) && HID_HOST_INCLUDED == TRUE)
431                     case BTU_TTYPE_HID_HOST_REPAGE_TO :
432                         hidh_proc_repage_timeout(p_tle);
433                         break;
434 #endif
435 
436 #if (defined(BLE_INCLUDED) && BLE_INCLUDED == TRUE)
437                     case BTU_TTYPE_BLE_INQUIRY:
438                     case BTU_TTYPE_BLE_GAP_LIM_DISC:
439                     case BTU_TTYPE_BLE_RANDOM_ADDR:
440                         btm_ble_timeout(p_tle);
441                         break;
442 
443                     case BTU_TTYPE_BLE_SCAN_PARAM_IDLE:
444                         btm_ble_scan_param_idle();
445                         break;
446 
447                     case BTU_TTYPE_ATT_WAIT_FOR_RSP:
448                         gatt_rsp_timeout(p_tle);
449                         break;
450 
451                     case BTU_TTYPE_ATT_WAIT_FOR_IND_ACK:
452                         gatt_ind_ack_timeout(p_tle);
453                         break;
454 #if (defined(SMP_INCLUDED) && SMP_INCLUDED == TRUE)
455                     case BTU_TTYPE_SMP_PAIRING_CMD:
456                         smp_rsp_timeout(p_tle);
457                         break;
458 #endif
459 
460 #endif
461 
462 #if (MCA_INCLUDED == TRUE)
463                     case BTU_TTYPE_MCA_CCB_RSP:
464                         mca_process_timeout(p_tle);
465                         break;
466 #endif
467                     case BTU_TTYPE_USER_FUNC:
468                         {
469                             tUSER_TIMEOUT_FUNC  *p_uf = (tUSER_TIMEOUT_FUNC *)p_tle->param;
470                             (*p_uf)(p_tle);
471                         }
472                         break;
473 
474                     default:
475                         i = 0;
476                         handled = FALSE;
477 
478                         for (; !handled && i < BTU_MAX_REG_TIMER; i++)
479                         {
480                             if (btu_cb.timer_reg[i].timer_cb == NULL)
481                                 continue;
482                             if (btu_cb.timer_reg[i].p_tle == p_tle)
483                             {
484                                 btu_cb.timer_reg[i].timer_cb(p_tle);
485                                 handled = TRUE;
486                             }
487                         }
488                         break;
489                 }
490             }
491 
492             /* if timer list is empty stop periodic GKI timer */
493             if (btu_cb.timer_queue.p_first == NULL)
494             {
495                 GKI_stop_timer(TIMER_0);
496             }
497         }
498 
499 #if defined(QUICK_TIMER_TICKS_PER_SEC) && (QUICK_TIMER_TICKS_PER_SEC > 0)
500         if (event & TIMER_2_EVT_MASK)
501         {
502             btu_process_quick_timer_evt();
503         }
504 #endif
505 
506 
507 #if (RPC_INCLUDED == TRUE)
508         /* if RPC message queue event */
509         if (event & RPCGEN_MSG_EVT)
510         {
511             if ((p_msg = (BT_HDR *) GKI_read_mbox(RPCGEN_MSG_MBOX)) != NULL)
512                 RPCT_RpcgenMsg(p_msg);  /* handle RPC message queue */
513         }
514 #endif
515 
516 #if (defined(BTU_BTA_INCLUDED) && BTU_BTA_INCLUDED == TRUE)
517         if (event & TASK_MBOX_2_EVT_MASK)
518         {
519             while ((p_msg = (BT_HDR *) GKI_read_mbox(TASK_MBOX_2)) != NULL)
520             {
521                 bta_sys_event(p_msg);
522             }
523         }
524 
525         if (event & TIMER_1_EVT_MASK)
526         {
527             bta_sys_timer_update();
528         }
529 #endif
530 
531         if (event & EVENT_MASK(APPL_EVT_7))
532             break;
533     }
534 
535     return(0);
536 }
537 
538 /*******************************************************************************
539 **
540 ** Function         btu_start_timer
541 **
542 ** Description      Start a timer for the specified amount of time.
543 **                  NOTE: The timeout resolution is in SECONDS! (Even
544 **                          though the timer structure field is ticks)
545 **
546 ** Returns          void
547 **
548 *******************************************************************************/
btu_start_timer(TIMER_LIST_ENT * p_tle,UINT16 type,UINT32 timeout)549 void btu_start_timer (TIMER_LIST_ENT *p_tle, UINT16 type, UINT32 timeout)
550 {
551     BT_HDR *p_msg;
552     /* if timer list is currently empty, start periodic GKI timer */
553     if (btu_cb.timer_queue.p_first == NULL)
554     {
555         /* if timer starts on other than BTU task */
556         if (GKI_get_taskid() != BTU_TASK)
557         {
558             /* post event to start timer in BTU task */
559             if ((p_msg = (BT_HDR *)GKI_getbuf(BT_HDR_SIZE)) != NULL)
560             {
561                 p_msg->event = BT_EVT_TO_START_TIMER;
562                 GKI_send_msg (BTU_TASK, TASK_MBOX_0, p_msg);
563             }
564         }
565         else
566         {
567             /* Start free running 1 second timer for list management */
568             GKI_start_timer (TIMER_0, GKI_SECS_TO_TICKS (1), TRUE);
569         }
570     }
571 
572     GKI_remove_from_timer_list (&btu_cb.timer_queue, p_tle);
573 
574     p_tle->event = type;
575     p_tle->ticks = timeout;         /* Save the number of seconds for the timer */
576 
577     GKI_add_to_timer_list (&btu_cb.timer_queue, p_tle);
578 }
579 
580 /*******************************************************************************
581 **
582 ** Function         btu_remaining_time
583 **
584 ** Description      Return amount of time to expire
585 **
586 ** Returns          time in second
587 **
588 *******************************************************************************/
btu_remaining_time(TIMER_LIST_ENT * p_tle)589 UINT32 btu_remaining_time (TIMER_LIST_ENT *p_tle)
590 {
591     return(GKI_get_remaining_ticks (&btu_cb.timer_queue, p_tle));
592 }
593 
594 /*******************************************************************************
595 **
596 ** Function         btu_stop_timer
597 **
598 ** Description      Stop a timer.
599 **
600 ** Returns          void
601 **
602 *******************************************************************************/
btu_stop_timer(TIMER_LIST_ENT * p_tle)603 void btu_stop_timer (TIMER_LIST_ENT *p_tle)
604 {
605     GKI_remove_from_timer_list (&btu_cb.timer_queue, p_tle);
606 
607     /* if timer list is empty stop periodic GKI timer */
608     if (btu_cb.timer_queue.p_first == NULL)
609     {
610         GKI_stop_timer(TIMER_0);
611     }
612 
613 }
614 
615 #if defined(QUICK_TIMER_TICKS_PER_SEC) && (QUICK_TIMER_TICKS_PER_SEC > 0)
616 /*******************************************************************************
617 **
618 ** Function         btu_start_quick_timer
619 **
620 ** Description      Start a timer for the specified amount of time.
621 **                  NOTE: The timeout resolution depends on including modules.
622 **                  QUICK_TIMER_TICKS_PER_SEC should be used to convert from
623 **                  time to ticks.
624 **
625 **
626 ** Returns          void
627 **
628 *******************************************************************************/
btu_start_quick_timer(TIMER_LIST_ENT * p_tle,UINT16 type,UINT32 timeout)629 void btu_start_quick_timer (TIMER_LIST_ENT *p_tle, UINT16 type, UINT32 timeout)
630 {
631     BT_HDR *p_msg;
632 
633     /* if timer list is currently empty, start periodic GKI timer */
634     if (btu_cb.quick_timer_queue.p_first == NULL)
635     {
636         /* script test calls stack API without posting event */
637         if (GKI_get_taskid() != BTU_TASK)
638         {
639             /* post event to start timer in BTU task */
640             if ((p_msg = (BT_HDR *)GKI_getbuf(BT_HDR_SIZE)) != NULL)
641             {
642                 p_msg->event = BT_EVT_TO_START_QUICK_TIMER;
643                 GKI_send_msg (BTU_TASK, TASK_MBOX_0, p_msg);
644             }
645         }
646         else
647             GKI_start_timer(TIMER_2, QUICK_TIMER_TICKS, TRUE);
648     }
649 
650     GKI_remove_from_timer_list (&btu_cb.quick_timer_queue, p_tle);
651 
652     p_tle->event = type;
653     p_tle->ticks = timeout; /* Save the number of ticks for the timer */
654 
655     GKI_add_to_timer_list (&btu_cb.quick_timer_queue, p_tle);
656 }
657 
658 
659 /*******************************************************************************
660 **
661 ** Function         btu_stop_quick_timer
662 **
663 ** Description      Stop a timer.
664 **
665 ** Returns          void
666 **
667 *******************************************************************************/
btu_stop_quick_timer(TIMER_LIST_ENT * p_tle)668 void btu_stop_quick_timer (TIMER_LIST_ENT *p_tle)
669 {
670     GKI_remove_from_timer_list (&btu_cb.quick_timer_queue, p_tle);
671 
672     /* if timer list is empty stop periodic GKI timer */
673     if (btu_cb.quick_timer_queue.p_first == NULL)
674     {
675         GKI_stop_timer(TIMER_2);
676     }
677 }
678 
679 /*******************************************************************************
680 **
681 ** Function         btu_process_quick_timer_evt
682 **
683 ** Description      Process quick timer event
684 **
685 ** Returns          void
686 **
687 *******************************************************************************/
btu_process_quick_timer_evt(void)688 void btu_process_quick_timer_evt(void)
689 {
690     process_quick_timer_evt(&btu_cb.quick_timer_queue);
691 
692     /* if timer list is empty stop periodic GKI timer */
693     if (btu_cb.quick_timer_queue.p_first == NULL)
694     {
695         GKI_stop_timer(TIMER_2);
696     }
697 }
698 
699 /*******************************************************************************
700 **
701 ** Function         process_quick_timer_evt
702 **
703 ** Description      Process quick timer event
704 **
705 ** Returns          void
706 **
707 *******************************************************************************/
process_quick_timer_evt(TIMER_LIST_Q * p_tlq)708 void process_quick_timer_evt(TIMER_LIST_Q *p_tlq)
709 {
710     TIMER_LIST_ENT  *p_tle;
711 
712     GKI_update_timer_list (p_tlq, 1);
713 
714     while ((p_tlq->p_first) && (!p_tlq->p_first->ticks))
715     {
716         p_tle = p_tlq->p_first;
717         GKI_remove_from_timer_list (p_tlq, p_tle);
718 
719         switch (p_tle->event)
720         {
721             case BTU_TTYPE_L2CAP_CHNL:      /* monitor or retransmission timer */
722             case BTU_TTYPE_L2CAP_FCR_ACK:   /* ack timer */
723                 l2c_process_timeout (p_tle);
724                 break;
725 
726             default:
727                 break;
728         }
729     }
730 }
731 #endif /* defined(QUICK_TIMER_TICKS_PER_SEC) && (QUICK_TIMER_TICKS_PER_SEC > 0) */
732 
733 
734 
btu_register_timer(TIMER_LIST_ENT * p_tle,UINT16 type,UINT32 timeout,tBTU_TIMER_CALLBACK timer_cb)735 void btu_register_timer (TIMER_LIST_ENT *p_tle, UINT16 type, UINT32 timeout, tBTU_TIMER_CALLBACK timer_cb)
736 {
737     UINT8 i = 0;
738     INT8  first = -1;
739     for (; i < BTU_MAX_REG_TIMER; i++)
740     {
741         if (btu_cb.timer_reg[i].p_tle == NULL && first < 0)
742             first = i;
743         if (btu_cb.timer_reg[i].p_tle == p_tle)
744         {
745             btu_cb.timer_reg[i].timer_cb = timer_cb;
746             btu_start_timer(p_tle, type, timeout);
747             first = -1;
748             break;
749         }
750     }
751 
752     if (first >= 0 && first < BTU_MAX_REG_TIMER)
753     {
754         btu_cb.timer_reg[first].timer_cb = timer_cb;
755         btu_cb.timer_reg[first].p_tle = p_tle;
756         btu_start_timer(p_tle, type, timeout);
757     }
758 
759 }
760 
761 
btu_deregister_timer(TIMER_LIST_ENT * p_tle)762 void btu_deregister_timer(TIMER_LIST_ENT *p_tle)
763 {
764     UINT8 i = 0;
765 
766     for (; i < BTU_MAX_REG_TIMER; i++)
767     {
768         if (btu_cb.timer_reg[i].p_tle == p_tle)
769         {
770             btu_stop_timer(p_tle);
771             btu_cb.timer_reg[i].timer_cb = NULL;
772             btu_cb.timer_reg[i].p_tle = NULL;
773             break;
774         }
775     }
776 }
777 
btu_register_event_range(UINT16 start,tBTU_EVENT_CALLBACK event_cb)778 void btu_register_event_range (UINT16 start, tBTU_EVENT_CALLBACK event_cb)
779 {
780     UINT8 i = 0;
781     INT8  first = -1;
782 
783     for (; i < BTU_MAX_REG_EVENT; i++)
784     {
785         if (btu_cb.event_reg[i].event_cb == NULL && first < 0)
786             first = i;
787 
788         if (btu_cb.event_reg[i].event_range == start)
789         {
790             btu_cb.event_reg[i].event_cb = event_cb;
791 
792             if (!event_cb)
793                 btu_cb.event_reg[i].event_range = 0;
794 
795             first = -1;
796         }
797     }
798 
799     /* if not deregistering && an empty index was found in range, register */
800     if (event_cb && first >= 0 && first < BTU_MAX_REG_EVENT)
801     {
802         btu_cb.event_reg[first].event_range = start;
803         btu_cb.event_reg[first].event_cb = event_cb;
804     }
805 }
806 
807 
btu_deregister_event_range(UINT16 range)808 void btu_deregister_event_range (UINT16 range)
809 {
810     btu_register_event_range(range, NULL);
811 }
812 
813 #if (defined(HCILP_INCLUDED) && HCILP_INCLUDED == TRUE)
814 /*******************************************************************************
815 **
816 ** Function         btu_check_bt_sleep
817 **
818 ** Description      This function is called to check if controller can go to sleep.
819 **
820 ** Returns          void
821 **
822 *******************************************************************************/
btu_check_bt_sleep(void)823 void btu_check_bt_sleep (void)
824 {
825     if ((btu_cb.hci_cmd_cb[LOCAL_BR_EDR_CONTROLLER_ID].cmd_cmpl_q.count == 0)
826         &&(btu_cb.hci_cmd_cb[LOCAL_BR_EDR_CONTROLLER_ID].cmd_xmit_q.count == 0))
827     {
828         if (l2cb.controller_xmit_window == l2cb.num_lm_acl_bufs)
829         {
830             /* enable dev to sleep  in the cmd cplt and cmd status only and num cplt packet */
831             HCI_LP_ALLOW_BT_DEVICE_SLEEP();
832         }
833     }
834 }
835 #endif
836