nxpandroid | c761165 | 2015-09-23 16:42:05 +0530 | [diff] [blame] | 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 | |
nxpandroid | c761165 | 2015-09-23 16:42:05 +0530 | [diff] [blame] | 19 | /****************************************************************************** |
| 20 | * |
| 21 | * Protocol timer services (taken from bta ptim) |
| 22 | * |
| 23 | ******************************************************************************/ |
nxf24591 | c1cbeab | 2018-02-21 17:32:26 +0530 | [diff] [blame] | 24 | #include <android-base/stringprintf.h> |
| 25 | #include <base/logging.h> |
nxpandroid | c761165 | 2015-09-23 16:42:05 +0530 | [diff] [blame] | 26 | |
nxpandroid | c761165 | 2015-09-23 16:42:05 +0530 | [diff] [blame] | 27 | #include "nfa_sys_ptim.h" |
| 28 | #include "nfa_sys.h" |
nxf24591 | c1cbeab | 2018-02-21 17:32:26 +0530 | [diff] [blame] | 29 | |
| 30 | using android::base::StringPrintf; |
| 31 | |
| 32 | extern bool nfc_debug_enabled; |
nxpandroid | c761165 | 2015-09-23 16:42:05 +0530 | [diff] [blame] | 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 | *******************************************************************************/ |
nxpandroid | 8f6d053 | 2017-07-12 18:25:30 +0530 | [diff] [blame] | 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; |
nxpandroid | c761165 | 2015-09-23 16:42:05 +0530 | [diff] [blame] | 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 | *******************************************************************************/ |
nxpandroid | 8f6d053 | 2017-07-12 18:25:30 +0530 | [diff] [blame] | 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; |
nxpandroid | c761165 | 2015-09-23 16:42:05 +0530 | [diff] [blame] | 67 | |
nxpandroid | 8f6d053 | 2017-07-12 18:25:30 +0530 | [diff] [blame] | 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(); |
nxpandroid | c761165 | 2015-09-23 16:42:05 +0530 | [diff] [blame] | 73 | |
nxpandroid | 8f6d053 | 2017-07-12 18:25:30 +0530 | [diff] [blame] | 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; |
nxf24591 | c1cbeab | 2018-02-21 17:32:26 +0530 | [diff] [blame] | 92 | DLOG_IF(INFO, nfc_debug_enabled) |
| 93 | << StringPrintf("nfa_sys_ptim_timer_update expired: %p", p_tle); |
nxpandroid | 8f6d053 | 2017-07-12 18:25:30 +0530 | [diff] [blame] | 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 != NULL) { |
| 102 | p_msg->event = p_tle->event; |
| 103 | p_msg->layer_specific = 0; |
| 104 | nfa_sys_sendmsg(p_msg); |
| 105 | } |
nxpandroid | c761165 | 2015-09-23 16:42:05 +0530 | [diff] [blame] | 106 | } |
nxpandroid | 8f6d053 | 2017-07-12 18:25:30 +0530 | [diff] [blame] | 107 | } |
nxpandroid | c761165 | 2015-09-23 16:42:05 +0530 | [diff] [blame] | 108 | |
nxpandroid | 8f6d053 | 2017-07-12 18:25:30 +0530 | [diff] [blame] | 109 | /* if timer list is empty stop periodic GKI timer */ |
| 110 | if (p_cb->timer_queue.p_first == NULL) { |
nxf24591 | c1cbeab | 2018-02-21 17:32:26 +0530 | [diff] [blame] | 111 | DLOG_IF(INFO, nfc_debug_enabled) << StringPrintf("ptim timer stop"); |
nxpandroid | 8f6d053 | 2017-07-12 18:25:30 +0530 | [diff] [blame] | 112 | GKI_stop_timer(p_cb->timer_id); |
| 113 | } |
nxpandroid | c761165 | 2015-09-23 16:42:05 +0530 | [diff] [blame] | 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 | *******************************************************************************/ |
nxpandroid | 8f6d053 | 2017-07-12 18:25:30 +0530 | [diff] [blame] | 126 | void nfa_sys_ptim_start_timer(tPTIM_CB* p_cb, TIMER_LIST_ENT* p_tle, |
| 127 | uint16_t type, int32_t timeout) { |
nxf24591 | c1cbeab | 2018-02-21 17:32:26 +0530 | [diff] [blame] | 128 | DLOG_IF(INFO, nfc_debug_enabled) |
| 129 | << StringPrintf("nfa_sys_ptim_start_timer %p", p_tle); |
nxpandroid | c761165 | 2015-09-23 16:42:05 +0530 | [diff] [blame] | 130 | |
nxpandroid | 8f6d053 | 2017-07-12 18:25:30 +0530 | [diff] [blame] | 131 | /* if timer list is currently empty, start periodic GKI timer */ |
| 132 | if (p_cb->timer_queue.p_first == NULL) { |
nxf24591 | c1cbeab | 2018-02-21 17:32:26 +0530 | [diff] [blame] | 133 | DLOG_IF(INFO, nfc_debug_enabled) << StringPrintf("ptim timer start"); |
nxpandroid | 8f6d053 | 2017-07-12 18:25:30 +0530 | [diff] [blame] | 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 | } |
nxpandroid | c761165 | 2015-09-23 16:42:05 +0530 | [diff] [blame] | 137 | |
nxpandroid | 8f6d053 | 2017-07-12 18:25:30 +0530 | [diff] [blame] | 138 | GKI_remove_from_timer_list(&p_cb->timer_queue, p_tle); |
nxpandroid | c761165 | 2015-09-23 16:42:05 +0530 | [diff] [blame] | 139 | |
nxpandroid | 8f6d053 | 2017-07-12 18:25:30 +0530 | [diff] [blame] | 140 | p_tle->event = type; |
| 141 | p_tle->ticks = timeout; |
nxpandroid | c761165 | 2015-09-23 16:42:05 +0530 | [diff] [blame] | 142 | |
nxpandroid | 8f6d053 | 2017-07-12 18:25:30 +0530 | [diff] [blame] | 143 | GKI_add_to_timer_list(&p_cb->timer_queue, p_tle); |
nxpandroid | c761165 | 2015-09-23 16:42:05 +0530 | [diff] [blame] | 144 | } |
| 145 | |
| 146 | /******************************************************************************* |
| 147 | ** |
| 148 | ** Function nfa_sys_ptim_stop_timer |
| 149 | ** |
| 150 | ** Description Stop a protocol timer. |
| 151 | ** |
| 152 | ** Returns void |
| 153 | ** |
| 154 | *******************************************************************************/ |
nxpandroid | 8f6d053 | 2017-07-12 18:25:30 +0530 | [diff] [blame] | 155 | void nfa_sys_ptim_stop_timer(tPTIM_CB* p_cb, TIMER_LIST_ENT* p_tle) { |
nxf24591 | c1cbeab | 2018-02-21 17:32:26 +0530 | [diff] [blame] | 156 | DLOG_IF(INFO, nfc_debug_enabled) |
| 157 | << StringPrintf("nfa_sys_ptim_stop_timer %p", p_tle); |
nxpandroid | c761165 | 2015-09-23 16:42:05 +0530 | [diff] [blame] | 158 | |
nxpandroid | 8f6d053 | 2017-07-12 18:25:30 +0530 | [diff] [blame] | 159 | GKI_remove_from_timer_list(&p_cb->timer_queue, p_tle); |
nxpandroid | c761165 | 2015-09-23 16:42:05 +0530 | [diff] [blame] | 160 | |
nxpandroid | 8f6d053 | 2017-07-12 18:25:30 +0530 | [diff] [blame] | 161 | /* if timer list is empty stop periodic GKI timer */ |
| 162 | if (p_cb->timer_queue.p_first == NULL) { |
nxf24591 | c1cbeab | 2018-02-21 17:32:26 +0530 | [diff] [blame] | 163 | DLOG_IF(INFO, nfc_debug_enabled) << StringPrintf("ptim timer stop"); |
nxpandroid | 8f6d053 | 2017-07-12 18:25:30 +0530 | [diff] [blame] | 164 | GKI_stop_timer(p_cb->timer_id); |
| 165 | } |
nxpandroid | c761165 | 2015-09-23 16:42:05 +0530 | [diff] [blame] | 166 | } |