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