blob: 3f0d48a0db45155fffabcae58c7b1778cc086900 [file] [log] [blame]
nxpandroidc7611652015-09-23 16:42:05 +05301/******************************************************************************
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
nxpandroidc7611652015-09-23 16:42:05 +053019/******************************************************************************
20 *
21 * Protocol timer services (taken from bta ptim)
22 *
23 ******************************************************************************/
nxf24591c1cbeab2018-02-21 17:32:26 +053024#include <android-base/stringprintf.h>
25#include <base/logging.h>
nxpandroidc7611652015-09-23 16:42:05 +053026
nxpandroidc7611652015-09-23 16:42:05 +053027#include "nfa_sys_ptim.h"
28#include "nfa_sys.h"
nxf24591c1cbeab2018-02-21 17:32:26 +053029
30using android::base::StringPrintf;
31
32extern bool nfc_debug_enabled;
nxpandroidc7611652015-09-23 16:42:05 +053033
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*******************************************************************************/
nxpandroid8f6d0532017-07-12 18:25:30 +053045void 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;
nxpandroidc7611652015-09-23 16:42:05 +053049}
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*******************************************************************************/
nxpandroid8f6d0532017-07-12 18:25:30 +053062void 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;
nxpandroidc7611652015-09-23 16:42:05 +053067
nxpandroid8f6d0532017-07-12 18:25:30 +053068 /* 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();
nxpandroidc7611652015-09-23 16:42:05 +053073
nxpandroid8f6d0532017-07-12 18:25:30 +053074 /* 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;
nxf24591c1cbeab2018-02-21 17:32:26 +053092 DLOG_IF(INFO, nfc_debug_enabled)
93 << StringPrintf("nfa_sys_ptim_timer_update expired: %p", p_tle);
nxpandroid8f6d0532017-07-12 18:25:30 +053094 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 != NULL) {
102 p_msg->event = p_tle->event;
103 p_msg->layer_specific = 0;
104 nfa_sys_sendmsg(p_msg);
105 }
nxpandroidc7611652015-09-23 16:42:05 +0530106 }
nxpandroid8f6d0532017-07-12 18:25:30 +0530107 }
nxpandroidc7611652015-09-23 16:42:05 +0530108
nxpandroid8f6d0532017-07-12 18:25:30 +0530109 /* if timer list is empty stop periodic GKI timer */
110 if (p_cb->timer_queue.p_first == NULL) {
nxf24591c1cbeab2018-02-21 17:32:26 +0530111 DLOG_IF(INFO, nfc_debug_enabled) << StringPrintf("ptim timer stop");
nxpandroid8f6d0532017-07-12 18:25:30 +0530112 GKI_stop_timer(p_cb->timer_id);
113 }
nxpandroidc7611652015-09-23 16:42:05 +0530114}
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*******************************************************************************/
nxpandroid8f6d0532017-07-12 18:25:30 +0530126void nfa_sys_ptim_start_timer(tPTIM_CB* p_cb, TIMER_LIST_ENT* p_tle,
127 uint16_t type, int32_t timeout) {
nxf24591c1cbeab2018-02-21 17:32:26 +0530128 DLOG_IF(INFO, nfc_debug_enabled)
129 << StringPrintf("nfa_sys_ptim_start_timer %p", p_tle);
nxpandroidc7611652015-09-23 16:42:05 +0530130
nxpandroid8f6d0532017-07-12 18:25:30 +0530131 /* if timer list is currently empty, start periodic GKI timer */
132 if (p_cb->timer_queue.p_first == NULL) {
nxf24591c1cbeab2018-02-21 17:32:26 +0530133 DLOG_IF(INFO, nfc_debug_enabled) << StringPrintf("ptim timer start");
nxpandroid8f6d0532017-07-12 18:25:30 +0530134 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 }
nxpandroidc7611652015-09-23 16:42:05 +0530137
nxpandroid8f6d0532017-07-12 18:25:30 +0530138 GKI_remove_from_timer_list(&p_cb->timer_queue, p_tle);
nxpandroidc7611652015-09-23 16:42:05 +0530139
nxpandroid8f6d0532017-07-12 18:25:30 +0530140 p_tle->event = type;
141 p_tle->ticks = timeout;
nxpandroidc7611652015-09-23 16:42:05 +0530142
nxpandroid8f6d0532017-07-12 18:25:30 +0530143 GKI_add_to_timer_list(&p_cb->timer_queue, p_tle);
nxpandroidc7611652015-09-23 16:42:05 +0530144}
145
146/*******************************************************************************
147**
148** Function nfa_sys_ptim_stop_timer
149**
150** Description Stop a protocol timer.
151**
152** Returns void
153**
154*******************************************************************************/
nxpandroid8f6d0532017-07-12 18:25:30 +0530155void nfa_sys_ptim_stop_timer(tPTIM_CB* p_cb, TIMER_LIST_ENT* p_tle) {
nxf24591c1cbeab2018-02-21 17:32:26 +0530156 DLOG_IF(INFO, nfc_debug_enabled)
157 << StringPrintf("nfa_sys_ptim_stop_timer %p", p_tle);
nxpandroidc7611652015-09-23 16:42:05 +0530158
nxpandroid8f6d0532017-07-12 18:25:30 +0530159 GKI_remove_from_timer_list(&p_cb->timer_queue, p_tle);
nxpandroidc7611652015-09-23 16:42:05 +0530160
nxpandroid8f6d0532017-07-12 18:25:30 +0530161 /* if timer list is empty stop periodic GKI timer */
162 if (p_cb->timer_queue.p_first == NULL) {
nxf24591c1cbeab2018-02-21 17:32:26 +0530163 DLOG_IF(INFO, nfc_debug_enabled) << StringPrintf("ptim timer stop");
nxpandroid8f6d0532017-07-12 18:25:30 +0530164 GKI_stop_timer(p_cb->timer_id);
165 }
nxpandroidc7611652015-09-23 16:42:05 +0530166}