• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /******************************************************************************
2  *
3  *  Copyright (C) 2010-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  *  Protocol timer services (taken from bta ptim)
22  *
23  ******************************************************************************/
24 
25 #include "nfc_target.h"
26 #include "gki.h"
27 #include "nfa_sys_ptim.h"
28 #include "nfa_sys.h"
29 #include "nfa_sys_int.h"
30 
31 /*******************************************************************************
32 **
33 ** Function         nfa_sys_ptim_init
34 **
35 ** Description      Initialize a protocol timer control block.  Parameter
36 **                  period is the GKI timer period in milliseconds.  Parameter
37 **                  timer_id is the GKI timer id.
38 **
39 ** Returns          void
40 **
41 *******************************************************************************/
nfa_sys_ptim_init(tPTIM_CB * p_cb,UINT16 period,UINT8 timer_id)42 void nfa_sys_ptim_init (tPTIM_CB *p_cb, UINT16 period, UINT8 timer_id)
43 {
44     GKI_init_timer_list (&p_cb->timer_queue);
45     p_cb->period = period;
46     p_cb->timer_id = timer_id;
47 }
48 
49 /*******************************************************************************
50 **
51 ** Function         nfa_sys_ptim_timer_update
52 **
53 ** Description      Update the protocol timer list and handle expired timers.
54 **                  This function is called from the task running the protocol
55 **                  timers when the periodic GKI timer expires.
56 **
57 ** Returns          void
58 **
59 *******************************************************************************/
nfa_sys_ptim_timer_update(tPTIM_CB * p_cb)60 void nfa_sys_ptim_timer_update (tPTIM_CB *p_cb)
61 {
62     TIMER_LIST_ENT *p_tle;
63     BT_HDR *p_msg;
64     UINT32 new_ticks_count;
65     INT32  period_in_ticks;
66 
67     /* To handle the case when the function is called less frequently than the period
68        we must convert determine the number of ticks since the last update, then
69        convert back to milliseconds before updating timer list */
70     new_ticks_count = GKI_get_tick_count ();
71 
72     /* Check for wrapped condition */
73     if (new_ticks_count >= p_cb->last_gki_ticks)
74     {
75         period_in_ticks = (INT32) (new_ticks_count - p_cb->last_gki_ticks);
76     }
77     else
78     {
79         period_in_ticks = (INT32) (((UINT32) 0xffffffff - p_cb->last_gki_ticks)
80                             + new_ticks_count + 1);
81     }
82 
83     /* update timer list */
84     GKI_update_timer_list (&p_cb->timer_queue, GKI_TICKS_TO_MS (period_in_ticks));
85 
86     p_cb->last_gki_ticks = new_ticks_count;
87 
88     /* while there are expired timers */
89     while ((p_cb->timer_queue.p_first) && (p_cb->timer_queue.p_first->ticks <= 0))
90     {
91         /* removed expired timer from list */
92         p_tle = p_cb->timer_queue.p_first;
93         NFA_TRACE_DEBUG1 ("nfa_sys_ptim_timer_update expired: %08x", p_tle);
94         GKI_remove_from_timer_list (&p_cb->timer_queue, p_tle);
95 
96         /* call timer callback */
97         if (p_tle->p_cback)
98         {
99             (*p_tle->p_cback) (p_tle);
100         }
101         else if (p_tle->event)
102         {
103             if ((p_msg = (BT_HDR *) GKI_getbuf (sizeof (BT_HDR))) != NULL)
104             {
105                 p_msg->event = p_tle->event;
106                 p_msg->layer_specific = 0;
107                 nfa_sys_sendmsg (p_msg);
108             }
109         }
110     }
111 
112     /* if timer list is empty stop periodic GKI timer */
113     if (p_cb->timer_queue.p_first == NULL)
114     {
115         NFA_TRACE_DEBUG0 ("ptim timer stop");
116         GKI_stop_timer (p_cb->timer_id);
117     }
118 }
119 
120 /*******************************************************************************
121 **
122 ** Function         nfa_sys_ptim_start_timer
123 **
124 ** Description      Start a protocol timer for the specified amount
125 **                  of time in seconds.
126 **
127 ** Returns          void
128 **
129 *******************************************************************************/
nfa_sys_ptim_start_timer(tPTIM_CB * p_cb,TIMER_LIST_ENT * p_tle,UINT16 type,INT32 timeout)130 void nfa_sys_ptim_start_timer (tPTIM_CB *p_cb, TIMER_LIST_ENT *p_tle, UINT16 type, INT32 timeout)
131 {
132     NFA_TRACE_DEBUG1 ("nfa_sys_ptim_start_timer %08x", p_tle);
133 
134     /* if timer list is currently empty, start periodic GKI timer */
135     if (p_cb->timer_queue.p_first == NULL)
136     {
137         NFA_TRACE_DEBUG0 ("ptim timer start");
138         p_cb->last_gki_ticks = GKI_get_tick_count ();
139         GKI_start_timer (p_cb->timer_id, GKI_MS_TO_TICKS (p_cb->period), TRUE);
140     }
141 
142     GKI_remove_from_timer_list (&p_cb->timer_queue, p_tle);
143 
144     p_tle->event = type;
145     p_tle->ticks = timeout;
146 
147     GKI_add_to_timer_list (&p_cb->timer_queue, p_tle);
148 }
149 
150 /*******************************************************************************
151 **
152 ** Function         nfa_sys_ptim_stop_timer
153 **
154 ** Description      Stop a protocol timer.
155 **
156 ** Returns          void
157 **
158 *******************************************************************************/
nfa_sys_ptim_stop_timer(tPTIM_CB * p_cb,TIMER_LIST_ENT * p_tle)159 void nfa_sys_ptim_stop_timer (tPTIM_CB *p_cb, TIMER_LIST_ENT *p_tle)
160 {
161     NFA_TRACE_DEBUG1 ("nfa_sys_ptim_stop_timer %08x", p_tle);
162 
163     GKI_remove_from_timer_list (&p_cb->timer_queue, p_tle);
164 
165     /* if timer list is empty stop periodic GKI timer */
166     if (p_cb->timer_queue.p_first == NULL)
167     {
168         NFA_TRACE_DEBUG0 ("ptim timer stop");
169         GKI_stop_timer (p_cb->timer_id);
170     }
171 }
172