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