Home | History | Annotate | Download | only in sys
      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  *
     22  *  Protocol timer services (taken from bta ptim)
     23  *
     24  ******************************************************************************/
     25 
     26 #include "nfc_target.h"
     27 #include "gki.h"
     28 #include "nfa_sys_ptim.h"
     29 #include "nfa_sys.h"
     30 #include "nfa_sys_int.h"
     31 
     32 /*******************************************************************************
     33 **
     34 ** Function         nfa_sys_ptim_init
     35 **
     36 ** Description      Initialize a protocol timer control block.  Parameter
     37 **                  period is the GKI timer period in milliseconds.  Parameter
     38 **                  timer_id is the GKI timer id.
     39 **
     40 ** Returns          void
     41 **
     42 *******************************************************************************/
     43 void nfa_sys_ptim_init (tPTIM_CB *p_cb, UINT16 period, UINT8 timer_id)
     44 {
     45     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         nfa_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 *******************************************************************************/
     61 void nfa_sys_ptim_timer_update (tPTIM_CB *p_cb)
     62 {
     63     TIMER_LIST_ENT *p_tle;
     64     BT_HDR *p_msg;
     65     UINT32 new_ticks_count;
     66     INT32  period_in_ticks;
     67 
     68     /* To handle the case when the function is called less frequently than the 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 = GKI_get_tick_count ();
     72 
     73     /* Check for wrapped condition */
     74     if (new_ticks_count >= p_cb->last_gki_ticks)
     75     {
     76         period_in_ticks = (INT32) (new_ticks_count - p_cb->last_gki_ticks);
     77     }
     78     else
     79     {
     80         period_in_ticks = (INT32) (((UINT32) 0xffffffff - p_cb->last_gki_ticks)
     81                             + new_ticks_count + 1);
     82     }
     83 
     84     /* update timer list */
     85     GKI_update_timer_list (&p_cb->timer_queue, GKI_TICKS_TO_MS (period_in_ticks));
     86 
     87     p_cb->last_gki_ticks = new_ticks_count;
     88 
     89     /* while there are expired timers */
     90     while ((p_cb->timer_queue.p_first) && (p_cb->timer_queue.p_first->ticks <= 0))
     91     {
     92         /* removed expired timer from list */
     93         p_tle = p_cb->timer_queue.p_first;
     94         NFA_TRACE_DEBUG1 ("nfa_sys_ptim_timer_update expired: %08x", p_tle);
     95         GKI_remove_from_timer_list (&p_cb->timer_queue, p_tle);
     96 
     97         /* call timer callback */
     98         if (p_tle->p_cback)
     99         {
    100             (*p_tle->p_cback) (p_tle);
    101         }
    102         else if (p_tle->event)
    103         {
    104             if ((p_msg = (BT_HDR *) GKI_getbuf (sizeof (BT_HDR))) != NULL)
    105             {
    106                 p_msg->event = p_tle->event;
    107                 p_msg->layer_specific = 0;
    108                 nfa_sys_sendmsg (p_msg);
    109             }
    110         }
    111     }
    112 
    113     /* if timer list is empty stop periodic GKI timer */
    114     if (p_cb->timer_queue.p_first == NULL)
    115     {
    116         NFA_TRACE_DEBUG0 ("ptim timer stop");
    117         GKI_stop_timer (p_cb->timer_id);
    118     }
    119 }
    120 
    121 /*******************************************************************************
    122 **
    123 ** Function         nfa_sys_ptim_start_timer
    124 **
    125 ** Description      Start a protocol timer for the specified amount
    126 **                  of time in seconds.
    127 **
    128 ** Returns          void
    129 **
    130 *******************************************************************************/
    131 void nfa_sys_ptim_start_timer (tPTIM_CB *p_cb, TIMER_LIST_ENT *p_tle, UINT16 type, INT32 timeout)
    132 {
    133     NFA_TRACE_DEBUG1 ("nfa_sys_ptim_start_timer %08x", p_tle);
    134 
    135     /* if timer list is currently empty, start periodic GKI timer */
    136     if (p_cb->timer_queue.p_first == NULL)
    137     {
    138         NFA_TRACE_DEBUG0 ("ptim timer start");
    139         p_cb->last_gki_ticks = GKI_get_tick_count ();
    140         GKI_start_timer (p_cb->timer_id, GKI_MS_TO_TICKS (p_cb->period), TRUE);
    141     }
    142 
    143     GKI_remove_from_timer_list (&p_cb->timer_queue, p_tle);
    144 
    145     p_tle->event = type;
    146     p_tle->ticks = timeout;
    147 
    148     GKI_add_to_timer_list (&p_cb->timer_queue, p_tle);
    149 }
    150 
    151 /*******************************************************************************
    152 **
    153 ** Function         nfa_sys_ptim_stop_timer
    154 **
    155 ** Description      Stop a protocol timer.
    156 **
    157 ** Returns          void
    158 **
    159 *******************************************************************************/
    160 void nfa_sys_ptim_stop_timer (tPTIM_CB *p_cb, TIMER_LIST_ENT *p_tle)
    161 {
    162     NFA_TRACE_DEBUG1 ("nfa_sys_ptim_stop_timer %08x", p_tle);
    163 
    164     GKI_remove_from_timer_list (&p_cb->timer_queue, p_tle);
    165 
    166     /* if timer list is empty stop periodic GKI timer */
    167     if (p_cb->timer_queue.p_first == NULL)
    168     {
    169         NFA_TRACE_DEBUG0 ("ptim timer stop");
    170         GKI_stop_timer (p_cb->timer_id);
    171     }
    172 }
    173