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