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