1 /******************************************************************************
2 *
3 * Copyright (C) 1999-2012 Broadcom Corporation
4 * Copyright 2018-2019 NXP
5 *
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at:
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 *
18 ******************************************************************************/
19 /******************************************************************************
20 *
21 * Protocol timer services (taken from bta ptim)
22 *
23 ******************************************************************************/
24 #include "uwa_sys_ptim.h"
25
26 #include "uci_log.h"
27 #include "uwa_sys.h"
28 #include "uwa_sys_int.h"
29 #include "uwb_gki.h"
30 #include "uwb_osal_common.h"
31 #include "uwb_target.h"
32
33 /*******************************************************************************
34 **
35 ** Function uwa_sys_ptim_init
36 **
37 ** Description Initialize a protocol timer control block. Parameter
38 ** period is the GKI timer period in milliseconds. Parameter
39 ** timer_id is the GKI timer id.
40 **
41 ** Returns void
42 **
43 *******************************************************************************/
uwa_sys_ptim_init(tPTIM_CB * p_cb,uint16_t period,uint8_t timer_id)44 void uwa_sys_ptim_init(tPTIM_CB* p_cb, uint16_t period, uint8_t timer_id) {
45 phUwb_GKI_init_timer_list(&p_cb->timer_queue);
46 p_cb->period = period;
47 p_cb->timer_id = timer_id;
48 }
49
50 /*******************************************************************************
51 **
52 ** Function uwa_sys_ptim_timer_update
53 **
54 ** Description Update the protocol timer list and handle expired timers.
55 ** This function is called from the task running the protocol
56 ** timers when the periodic GKI timer expires.
57 **
58 ** Returns void
59 **
60 *******************************************************************************/
uwa_sys_ptim_timer_update(tPTIM_CB * p_cb)61 void uwa_sys_ptim_timer_update(tPTIM_CB* p_cb) {
62 TIMER_LIST_ENT* p_tle;
63 UWB_HDR* p_msg;
64 uint32_t new_ticks_count;
65 uint32_t period_in_ticks;
66
67 /* To handle the case when the function is called less frequently than the
68 period
69 we must convert determine the number of ticks since the last update, then
70 convert back to milliseconds before updating timer list */
71 new_ticks_count = phUwb_GKI_get_tick_count();
72
73 /* Check for wrapped condition */
74 if (new_ticks_count >= p_cb->last_gki_ticks) {
75 period_in_ticks = (uint32_t)(new_ticks_count - p_cb->last_gki_ticks);
76 } else {
77 period_in_ticks = (uint32_t)(((uint32_t)0xffffffff - p_cb->last_gki_ticks) +
78 new_ticks_count + 1);
79 }
80
81 /* update timer list */
82 phUwb_GKI_update_timer_list(&p_cb->timer_queue,
83 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 UCI_TRACE_I("uwa_sys_ptim_timer_update expired: %p", p_tle);
93 phUwb_GKI_remove_from_timer_list(&p_cb->timer_queue, p_tle);
94
95 /* call timer callback */
96 if (p_tle->p_cback) {
97 (*p_tle->p_cback)(p_tle);
98 } else if (p_tle->event) {
99 p_msg = (UWB_HDR*)phUwb_GKI_getbuf(sizeof(UWB_HDR));
100 if (p_msg != NULL) {
101 p_msg->event = p_tle->event;
102 p_msg->layer_specific = 0;
103 uwa_sys_sendmsg(p_msg);
104 }
105 }
106 }
107
108 /* if timer list is empty stop periodic GKI timer */
109 if (p_cb->timer_queue.p_first == NULL) {
110 UCI_TRACE_I("ptim timer stop");
111 phUwb_GKI_stop_timer(p_cb->timer_id, 0);
112 }
113 }
114
115 /*******************************************************************************
116 **
117 ** Function uwa_sys_ptim_start_timer
118 **
119 ** Description Start a protocol timer for the specified amount
120 ** of time in seconds.
121 **
122 ** Returns void
123 **
124 *******************************************************************************/
uwa_sys_ptim_start_timer(tPTIM_CB * p_cb,TIMER_LIST_ENT * p_tle,uint16_t type,uint32_t timeout)125 void uwa_sys_ptim_start_timer(tPTIM_CB* p_cb, TIMER_LIST_ENT* p_tle,
126 uint16_t type, uint32_t timeout) {
127 UCI_TRACE_I("uwa_sys_ptim_start_timer %p", p_tle);
128
129 /* if timer list is currently empty, start periodic GKI timer */
130 if (p_cb->timer_queue.p_first == NULL) {
131 UCI_TRACE_I("ptim timer start");
132 p_cb->last_gki_ticks = phUwb_GKI_get_tick_count();
133 phUwb_GKI_start_timer(p_cb->timer_id, GKI_MS_TO_TICKS(p_cb->period), true);
134 }
135
136 phUwb_GKI_remove_from_timer_list(&p_cb->timer_queue, p_tle);
137
138 p_tle->event = type;
139 p_tle->ticks = timeout;
140
141 phUwb_GKI_add_to_timer_list(&p_cb->timer_queue, p_tle);
142 }
143
144 /*******************************************************************************
145 **
146 ** Function uwa_sys_ptim_stop_timer
147 **
148 ** Description Stop a protocol timer.
149 **
150 ** Returns void
151 **
152 *******************************************************************************/
uwa_sys_ptim_stop_timer(tPTIM_CB * p_cb,TIMER_LIST_ENT * p_tle)153 void uwa_sys_ptim_stop_timer(tPTIM_CB* p_cb, TIMER_LIST_ENT* p_tle) {
154 UCI_TRACE_I("uwa_sys_ptim_stop_timer %p", p_tle);
155
156 phUwb_GKI_remove_from_timer_list(&p_cb->timer_queue, p_tle);
157
158 /* if timer list is empty stop periodic GKI timer */
159 if (p_cb->timer_queue.p_first == NULL) {
160 UCI_TRACE_I("ptim timer stop");
161 phUwb_GKI_stop_timer(p_cb->timer_id, 0);
162 }
163 }
164