1 /****************************************************************************** 2 * 3 * Copyright (C) 2003-2012 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. 22 * 23 ******************************************************************************/ 24 25 #include "bt_target.h" 26 #include "gki.h" 27 #include "ptim.h" 28 #include "bta_sys.h" 29 30 /******************************************************************************* 31 ** 32 ** Function ptim_init 33 ** 34 ** Description Initialize a protocol timer control block. Parameter 35 ** period is the GKI timer period in milliseconds. Parameter 36 ** timer_id is the GKI timer id. 37 ** 38 ** Returns void 39 ** 40 *******************************************************************************/ 41 void ptim_init(tPTIM_CB *p_cb, UINT16 period, UINT8 timer_id) 42 { 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 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 ptim_timer_update(tPTIM_CB *p_cb) 60 { 61 TIMER_LIST_ENT *p_tle; 62 BT_HDR *p_msg; 63 UINT32 new_ticks_count; 64 INT32 period_in_ticks; 65 66 /* To handle the case when the function is called less frequently than the 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 { 74 period_in_ticks = (INT32)(new_ticks_count - p_cb->last_gki_ticks); 75 } 76 else 77 { 78 period_in_ticks = (INT32)(((UINT32)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) && (p_cb->timer_queue.p_first->ticks <= 0)) 89 { 90 /* removed expired timer from list */ 91 p_tle = p_cb->timer_queue.p_first; 92 GKI_remove_from_timer_list(&p_cb->timer_queue, p_tle); 93 94 /* call timer callback */ 95 if(p_tle->p_cback) 96 { 97 (*p_tle->p_cback)(p_tle); 98 } 99 else if(p_tle->event) 100 { 101 if ((p_msg = (BT_HDR *) GKI_getbuf(sizeof(BT_HDR))) != NULL) 102 { 103 p_msg->event = p_tle->event; 104 p_msg->layer_specific = 0; 105 bta_sys_sendmsg(p_msg); 106 } 107 } 108 } 109 110 /* if timer list is empty stop periodic GKI timer */ 111 if (p_cb->timer_queue.p_first == NULL) 112 { 113 GKI_stop_timer(p_cb->timer_id); 114 } 115 } 116 117 /******************************************************************************* 118 ** 119 ** Function 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 *******************************************************************************/ 127 void ptim_start_timer(tPTIM_CB *p_cb, TIMER_LIST_ENT *p_tle, UINT16 type, INT32 timeout) 128 { 129 /* if timer list is currently empty, start periodic GKI timer */ 130 if (p_cb->timer_queue.p_first == NULL) 131 { 132 p_cb->last_gki_ticks = GKI_get_tick_count(); 133 GKI_start_timer(p_cb->timer_id, GKI_MS_TO_TICKS(p_cb->period), TRUE); 134 } 135 136 GKI_remove_from_timer_list(&p_cb->timer_queue, p_tle); 137 138 p_tle->event = type; 139 p_tle->ticks = timeout; 140 141 GKI_add_to_timer_list(&p_cb->timer_queue, p_tle); 142 } 143 144 /******************************************************************************* 145 ** 146 ** Function ptim_stop_timer 147 ** 148 ** Description Stop a protocol timer. 149 ** 150 ** Returns void 151 ** 152 *******************************************************************************/ 153 void ptim_stop_timer(tPTIM_CB *p_cb, TIMER_LIST_ENT *p_tle) 154 { 155 GKI_remove_from_timer_list (&p_cb->timer_queue, p_tle); 156 157 /* if timer list is empty stop periodic GKI timer */ 158 if (p_cb->timer_queue.p_first == NULL) 159 { 160 GKI_stop_timer(p_cb->timer_id); 161 } 162 } 163