Home | History | Annotate | Download | only in src
      1 /*
      2  * Copyright (C) 2010 NXP Semiconductors
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 /*!
     18 * \file  phLlcNfc_Timer.c
     19 * \brief To create, start, stop and destroy timer.
     20 *
     21 * Project: NFC-FRI-1.1
     22 *
     23 * $Date: Mon Jun 14 11:47:54 2010 $
     24 * $Author: ing02260 $
     25 * $Revision: 1.55 $
     26 * $Aliases: NFC_FRI1.1_WK1023_R35_2,NFC_FRI1.1_WK1023_R35_1 $
     27 *
     28 */
     29 
     30 /*************************** Includes *******************************/
     31 #include <phNfcTypes.h>
     32 #include <phNfcStatus.h>
     33 #include <phOsalNfc.h>
     34 #include <phOsalNfc_Timer.h>
     35 #include <phNfcInterface.h>
     36 #include <phLlcNfc.h>
     37 #include <phLlcNfc_DataTypes.h>
     38 #include <phLlcNfc_Interface.h>
     39 #include <phLlcNfc_Frame.h>
     40 #include <phLlcNfc_Timer.h>
     41 
     42 /*********************** End of includes ****************************/
     43 
     44 /***************************** Macros *******************************/
     45 /**< Timer for connection timer index */
     46 #define PH_LLCNFC_CONNECTION_TO_INDEX       (0x00)
     47 /**< 0x0A Timer for ack time out value */
     48 #define PH_LLCNFC_ACK_TO_VALUE              (1000)
     49 /**< Maximum guard timer can be present */
     50 #define PH_LLCNFC_MAX_GUARD_TIMER           (0x04)
     51 /** Connection time out bit to set */
     52 #define PH_LLCNFC_CON_TO_BIT                (0)
     53 /** Guard time out bit to set */
     54 #define PH_LLCNFC_GUARD_TO_BIT              (1)
     55 /** Ack time out bit to set */
     56 #define PH_LLCNFC_ACK_TO_BIT                (2)
     57 /** No of bits to set */
     58 #define PH_LLCNFC_TO_NOOFBITS               (1)
     59 /** Connection time out bit value */
     60 #define PH_LLCNFC_CON_TO_BIT_VAL            (0x01)
     61 /** Guard time out bit to set */
     62 #define PH_LLCNFC_GUARD_TO_BIT_VAL          (0x02)
     63 
     64 #define GUARD_TO_URSET
     65 
     66 
     67 /************************ End of macros *****************************/
     68 
     69 /*********************** Local functions ****************************/
     70 /* This callback is for guard time out */
     71 #ifdef LLC_TIMER_ENABLE
     72 static
     73 void
     74 phLlcNfc_GuardTimeoutCb (
     75     uint32_t TimerId,
     76     void *pContext
     77 );
     78 
     79 
     80 #ifdef PIGGY_BACK
     81 /* This callback is for acknowledge time out */
     82 static
     83 void
     84 phLlcNfc_AckTimeoutCb (
     85     uint32_t TimerId
     86 );
     87 #endif
     88 
     89 /* This callback is for connection time out */
     90 static
     91 void
     92 phLlcNfc_ConnectionTimeoutCb (
     93     uint32_t TimerId,
     94     void *pContext
     95 );
     96 #endif /* #ifdef LLC_TIMER_ENABLE */
     97 
     98 /******************** End of Local functions ************************/
     99 
    100 /********************** Global variables ****************************/
    101 static phLlcNfc_Context_t   *gpphLlcNfc_Ctxt = NULL;
    102 
    103 /******************** End of Global Variables ***********************/
    104 
    105 NFCSTATUS
    106 phLlcNfc_TimerInit(
    107     phLlcNfc_Context_t  *psLlcCtxt
    108 )
    109 {
    110     NFCSTATUS   result = PHNFCSTVAL(CID_NFC_LLC,
    111                                     NFCSTATUS_INVALID_PARAMETER);
    112     uint8_t     index = 0;
    113     if (NULL != psLlcCtxt)
    114     {
    115         result = NFCSTATUS_SUCCESS;
    116         gpphLlcNfc_Ctxt = psLlcCtxt;
    117         while (index < PH_LLCNFC_MAX_TIMER_USED)
    118         {
    119 #ifdef LLC_TIMER_ENABLE
    120             gpphLlcNfc_Ctxt->s_timerinfo.timer_id[index] =
    121                                     PH_OSALNFC_INVALID_TIMER_ID;
    122 #endif /* #ifdef LLC_TIMER_ENABLE */
    123             index++;
    124         }
    125     }
    126     return result;
    127 }
    128 
    129 void
    130 phLlcNfc_TimerUnInit(
    131     phLlcNfc_Context_t  *psLlcCtxt
    132 )
    133 {
    134     uint8_t     index = 0;
    135     if ((NULL != gpphLlcNfc_Ctxt) &&
    136         (gpphLlcNfc_Ctxt == psLlcCtxt))
    137     {
    138         while (index <= PH_LLCNFC_ACKTIMER)
    139         {
    140             if (PH_LLCNFC_GUARDTIMER == index)
    141             {
    142                 phLlcNfc_StopTimers (index,
    143                         gpphLlcNfc_Ctxt->s_timerinfo.guard_to_count);
    144             }
    145             else
    146             {
    147                 phLlcNfc_StopTimers (index, 0);
    148             }
    149             index++;
    150         }
    151         phLlcNfc_DeleteTimer();
    152     }
    153 }
    154 
    155 void
    156 phLlcNfc_CreateTimers(void)
    157 {
    158     uint8_t     index = 0;
    159 
    160     while (index < PH_LLCNFC_MAX_TIMER_USED)
    161     {
    162 #ifdef LLC_TIMER_ENABLE
    163         gpphLlcNfc_Ctxt->s_timerinfo.timer_id[index] =
    164                             phOsalNfc_Timer_Create();
    165 #endif /* #ifdef LLC_TIMER_ENABLE */
    166         index++;
    167     }
    168     return;
    169 }
    170 
    171 NFCSTATUS
    172 phLlcNfc_StartTimers (
    173     uint8_t             TimerType,
    174     uint8_t             ns_value
    175 )
    176 {
    177     NFCSTATUS               result = NFCSTATUS_SUCCESS;
    178 #ifdef LLC_TIMER_ENABLE
    179 
    180     uint32_t                timerid = 0;
    181     uint8_t                 timerstarted = 0;
    182     uint8_t                 timer_count = 0;
    183     uint16_t                timer_resolution = 0;
    184     ppCallBck_t             Callback = NULL;
    185     phLlcNfc_Timerinfo_t    *ps_timer_info = NULL;
    186 
    187     ps_timer_info = &(gpphLlcNfc_Ctxt->s_timerinfo);
    188     PHNFC_UNUSED_VARIABLE(result);
    189 
    190     PH_LLCNFC_PRINT("\n\nLLC : START TIMER CALLED\n\n");
    191     /* Depending on the timer type, use the Osal callback */
    192     switch(TimerType)
    193     {
    194         case PH_LLCNFC_CONNECTIONTIMER:
    195         {
    196             /* Get the connection timer flag */
    197             timerstarted = (uint8_t)
    198                 GET_BITS8(ps_timer_info->timer_flag,
    199                         PH_LLCNFC_CON_TO_BIT,
    200                         PH_LLCNFC_TO_NOOFBITS);
    201             if (0 == timerstarted)
    202             {
    203                 /* Timer not started, so start the timer */
    204                 gpphLlcNfc_Ctxt->s_timerinfo.timer_flag = (uint8_t)
    205                                 SET_BITS8 (ps_timer_info->timer_flag,
    206                                         PH_LLCNFC_CON_TO_BIT,
    207                                         PH_LLCNFC_TO_NOOFBITS,
    208                                         (PH_LLCNFC_CON_TO_BIT + 1));
    209             }
    210 
    211             timerid = ps_timer_info->timer_id[PH_LLCNFC_CONNECTION_TO_INDEX];
    212             Callback = (ppCallBck_t)&phLlcNfc_ConnectionTimeoutCb;
    213             timer_resolution = ps_timer_info->con_to_value = (uint16_t)
    214                                             PH_LLCNFC_CONNECTION_TO_VALUE;
    215             break;
    216         }
    217 
    218         case PH_LLCNFC_GUARDTIMER:
    219         {
    220             if (ps_timer_info->guard_to_count < PH_LLCNFC_MAX_GUARD_TIMER)
    221             {
    222                 timer_count = ps_timer_info->guard_to_count;
    223                 timer_resolution = (uint16_t)PH_LLCNFC_RESOLUTION;
    224 
    225                 PH_LLCNFC_DEBUG("RESOLUTION VALUE : 0x%02X\n", PH_LLCNFC_RESOLUTION);
    226                 PH_LLCNFC_DEBUG("TIME-OUT VALUE : 0x%02X\n", PH_LLCNFC_GUARD_TO_VALUE);
    227 
    228                 /* Get the guard timer flag */
    229                 timerstarted = (uint8_t)
    230                     GET_BITS8 (ps_timer_info->timer_flag,
    231                             PH_LLCNFC_GUARD_TO_BIT,
    232                             PH_LLCNFC_TO_NOOFBITS);
    233 
    234                 PH_LLCNFC_DEBUG("GUARD TIMER NS INDEX : 0x%02X\n", ns_value);
    235                 PH_LLCNFC_DEBUG("GUARD TIMER COUNT : 0x%02X\n", timer_count);
    236                 PH_LLCNFC_DEBUG("GUARD TIMER STARTED : 0x%02X\n", timerstarted);
    237 
    238                 if (0 == timerstarted)
    239                 {
    240                     /* Timer not started, so start the timer */
    241                     ps_timer_info->timer_flag = (uint8_t)
    242                         SET_BITS8 (ps_timer_info->timer_flag,
    243                                     PH_LLCNFC_GUARD_TO_BIT,
    244                                     PH_LLCNFC_TO_NOOFBITS,
    245                                     PH_LLCNFC_GUARD_TO_BIT);
    246                 }
    247 
    248                 timerid = ps_timer_info->timer_id[PH_LLCNFC_GUARDTIMER];
    249                 Callback = (ppCallBck_t)&phLlcNfc_GuardTimeoutCb;
    250 
    251                 /* Guard time out value */
    252                 ps_timer_info->guard_to_value[timer_count] = (uint16_t)
    253                                         PH_LLCNFC_GUARD_TO_VALUE;
    254 
    255                 ps_timer_info->timer_ns_value[timer_count] = ns_value;
    256                 ps_timer_info->frame_type[timer_count] = (uint8_t)invalid_frame;
    257                 ps_timer_info->iframe_send_count[timer_count] = 0;
    258 
    259                 if ((timer_count > 0) &&
    260                     (ps_timer_info->guard_to_value[(timer_count - 1)] >=
    261                     PH_LLCNFC_GUARD_TO_VALUE))
    262                 {
    263                     /* If the timer has been started already and the
    264                         value is same as the previous means that timer has still
    265                         not expired, so the time out value is increased by
    266                         a resolution */
    267                     ps_timer_info->guard_to_value[timer_count] = (uint16_t)
    268                             (ps_timer_info->guard_to_value[(timer_count - 1)] +
    269                             PH_LLCNFC_RESOLUTION);
    270                 }
    271 
    272                 PH_LLCNFC_DEBUG("GUARD TIMER VALUE : 0x%04X\n", ps_timer_info->guard_to_value[timer_count]);
    273 
    274 
    275                 ps_timer_info->guard_to_count = (uint8_t)(
    276                                         ps_timer_info->guard_to_count + 1);
    277             }
    278             else
    279             {
    280                 /* TIMER should not start, because the time out count has readched the limit */
    281                 timerstarted = TRUE;
    282             }
    283             break;
    284         }
    285 
    286 #ifdef PIGGY_BACK
    287 
    288         case PH_LLCNFC_ACKTIMER:
    289         {
    290             /* Get the ack timer flag */
    291             timerstarted = (uint8_t)GET_BITS8 (
    292                                     ps_timer_info->timer_flag,
    293                                     PH_LLCNFC_GUARD_TO_BIT,
    294                                     PH_LLCNFC_TO_NOOFBITS);
    295             if (0 == timerstarted)
    296             {
    297                 /* Timer not started, so start the timer */
    298                 ps_timer_info->timer_flag = (uint8_t)
    299                                 SET_BITS8 (ps_timer_info->timer_flag,
    300                                         PH_LLCNFC_GUARD_TO_BIT
    301                                         PH_LLCNFC_TO_NOOFBITS
    302                                         (PH_LLCNFC_GUARD_TO_BIT - 1));
    303             }
    304             timerid = ps_timer_info->timer_id[PH_LLCNFC_ACKTIMER];
    305             Callback = (ppCallBck_t)&phLlcNfc_AckTimeoutCb;
    306             break;
    307         }
    308 
    309 #endif /* #ifdef PIGGY_BACK */
    310 
    311         default:
    312         {
    313             result = PHNFCSTVAL(CID_NFC_LLC,
    314                                 NFCSTATUS_INVALID_PARAMETER);
    315             break;
    316         }
    317     }
    318     if ((NFCSTATUS_SUCCESS == result) &&
    319         (FALSE == timerstarted))
    320     {
    321         PH_LLCNFC_DEBUG("OSAL START TIMER CALLED TIMER ID : 0x%02X\n", timerid);
    322         phOsalNfc_Timer_Start (timerid, timer_resolution, Callback, NULL);
    323     }
    324 
    325     PH_LLCNFC_PRINT("\n\nLLC : START TIMER END\n\n");
    326 
    327 #else /* #ifdef LLC_TIMER_ENABLE */
    328 
    329     PHNFC_UNUSED_VARIABLE(result);
    330     PHNFC_UNUSED_VARIABLE(TimerType);
    331     PHNFC_UNUSED_VARIABLE(ns_value);
    332 
    333 #endif /* #ifdef LLC_TIMER_ENABLE */
    334     return result;
    335 }
    336 
    337 void
    338 phLlcNfc_StopTimers (
    339     uint8_t             TimerType,
    340     uint8_t             no_of_guard_to_del
    341 )
    342 {
    343     NFCSTATUS               result = NFCSTATUS_SUCCESS;
    344 #ifdef LLC_TIMER_ENABLE
    345 
    346     uint32_t                timerid = 0,
    347                             timerflag = FALSE;
    348     uint8_t                 timer_count = 0;
    349     phLlcNfc_Timerinfo_t    *ps_timer_info = NULL;
    350 
    351 
    352     ps_timer_info = &(gpphLlcNfc_Ctxt->s_timerinfo);
    353     timerflag = ps_timer_info->timer_flag;
    354 
    355     PHNFC_UNUSED_VARIABLE (result);
    356     PH_LLCNFC_PRINT("\n\nLLC : STOP TIMER CALLED\n\n");
    357     switch(TimerType)
    358     {
    359         case PH_LLCNFC_CONNECTIONTIMER:
    360         {
    361             ps_timer_info->timer_flag = (uint8_t)
    362                         SET_BITS8(ps_timer_info->timer_flag,
    363                                 PH_LLCNFC_CON_TO_BIT,
    364                                 PH_LLCNFC_TO_NOOFBITS, 0);
    365             timerid = ps_timer_info->timer_id
    366                                 [PH_LLCNFC_CONNECTION_TO_INDEX];
    367             break;
    368         }
    369 
    370         case PH_LLCNFC_GUARDTIMER:
    371         {
    372             uint8_t             start_index = 0;
    373 
    374             timer_count = ps_timer_info->guard_to_count;
    375 
    376             PH_LLCNFC_DEBUG("GUARD TIMER COUNT BEFORE DELETE: 0x%02X\n", timer_count);
    377             PH_LLCNFC_DEBUG("GUARD TIMER TO DELETE: 0x%02X\n", no_of_guard_to_del);
    378 
    379             if (timer_count > no_of_guard_to_del)
    380             {
    381                 /* The number of guard timer count is more than the
    382                     guard timer to delete  */
    383                 while (start_index < no_of_guard_to_del)
    384                 {
    385                     /* Copy the previous stored timer values to the present */
    386                     ps_timer_info->guard_to_value[start_index] = (uint16_t)
    387                                 (ps_timer_info->guard_to_value[
    388                                 (no_of_guard_to_del + start_index)]);
    389 
    390                     ps_timer_info->iframe_send_count[start_index] = (uint8_t)
    391                                 (ps_timer_info->iframe_send_count[
    392                                 (no_of_guard_to_del + start_index)]);
    393 
    394                     PH_LLCNFC_DEBUG("GUARD TIMER NS INDEX DELETED : 0x%02X\n", ps_timer_info->timer_ns_value[start_index]);
    395 
    396                     ps_timer_info->timer_ns_value[start_index] = (uint8_t)
    397                                 (ps_timer_info->timer_ns_value[
    398                                 (no_of_guard_to_del + start_index)]);
    399 
    400                     ps_timer_info->frame_type[start_index] = (uint8_t)
    401                                 (ps_timer_info->frame_type[
    402                                 (no_of_guard_to_del + start_index)]);
    403 
    404                     start_index = (uint8_t)(start_index + 1);
    405                 }
    406             }
    407             else
    408             {
    409                 while (start_index < no_of_guard_to_del)
    410                 {
    411                     ps_timer_info->guard_to_value[start_index] = 0;
    412 
    413                     ps_timer_info->iframe_send_count[start_index] = 0;
    414 
    415                     PH_LLCNFC_DEBUG("GUARD TIMER NS INDEX DELETED ELSE : 0x%02X\n", ps_timer_info->timer_ns_value[start_index]);
    416 
    417                     ps_timer_info->timer_ns_value[start_index] = 0;
    418 
    419                     ps_timer_info->frame_type[start_index] = 0;
    420 
    421                     start_index = (uint8_t)(start_index + 1);
    422                 }
    423             }
    424 
    425             if (timer_count >= no_of_guard_to_del)
    426             {
    427                 timer_count = (uint8_t)(timer_count - no_of_guard_to_del);
    428             }
    429             else
    430             {
    431                 if (0 != no_of_guard_to_del)
    432                 {
    433                     timer_count = 0;
    434                 }
    435             }
    436 
    437             timerid = ps_timer_info->timer_id[PH_LLCNFC_GUARDTIMER];
    438             ps_timer_info->guard_to_count = timer_count;
    439             PH_LLCNFC_DEBUG("GUARD TIMER COUNT AFTER DELETE: 0x%02X\n", timer_count);
    440 
    441             if (0 == ps_timer_info->guard_to_count)
    442             {
    443                 /* This means that there are no frames to run guard
    444                     timer, so set the timer flag to 0 */
    445                 ps_timer_info->timer_flag = (uint8_t)
    446                         SET_BITS8 (ps_timer_info->timer_flag,
    447                                     PH_LLCNFC_GUARD_TO_BIT,
    448                                     PH_LLCNFC_TO_NOOFBITS, 0);
    449             }
    450             else
    451             {
    452                 timerflag = 0;
    453             }
    454             break;
    455         }
    456 
    457 #ifdef PIGGY_BACK
    458         case PH_LLCNFC_ACKTIMER:
    459         {
    460             ps_timer_info->timer_flag = (uint8_t)
    461                                 SET_BITS8 (ps_timer_info->timer_flag,
    462                                         PH_LLCNFC_GUARD_TO_BIT,
    463                                         PH_LLCNFC_TO_NOOFBITS, 0);
    464             timerid = ps_timer_info->timer_id[PH_LLCNFC_ACKTIMER];
    465             break;
    466         }
    467 #endif /* #ifdef PIGGY_BACK */
    468 
    469         default:
    470         {
    471             result = PHNFCSTVAL(CID_NFC_LLC,
    472                                 NFCSTATUS_INVALID_PARAMETER);
    473             break;
    474         }
    475     }
    476 
    477     if ((NFCSTATUS_SUCCESS == result) && (timerflag > 0))
    478     {
    479         PH_LLCNFC_DEBUG("OSAL STOP TIMER CALLED TIMER ID : 0x%02X\n", timerid);
    480         phOsalNfc_Timer_Stop (timerid);
    481     }
    482 
    483     PH_LLCNFC_PRINT("\n\nLLC : STOP TIMER END\n\n");
    484 
    485 #else /* #ifdef LLC_TIMER_ENABLE */
    486 
    487     PHNFC_UNUSED_VARIABLE (result);
    488     PHNFC_UNUSED_VARIABLE (TimerType);
    489     PHNFC_UNUSED_VARIABLE (no_of_gaurd_to_del);
    490 
    491 #endif /* #ifdef LLC_TIMER_ENABLE */
    492 }
    493 
    494 void
    495 phLlcNfc_StopAllTimers (void)
    496 {
    497 
    498 #ifdef LLC_TIMER_ENABLE
    499 
    500     phLlcNfc_Timerinfo_t    *ps_timer_info = NULL;
    501     uint8_t                 timer_started = 0;
    502     uint32_t                timerid = 0;
    503     uint8_t                 timer_index = 0;
    504 
    505 
    506     ps_timer_info = &(gpphLlcNfc_Ctxt->s_timerinfo);
    507 
    508     PH_LLCNFC_PRINT("\n\nLLC : STOP ALL TIMERS CALLED \n\n");
    509 
    510     timerid = ps_timer_info->timer_id[timer_index];
    511     timer_started = (uint8_t)
    512                 GET_BITS8(ps_timer_info->timer_flag,
    513                         PH_LLCNFC_CON_TO_BIT,
    514                         PH_LLCNFC_TO_NOOFBITS);
    515 
    516     PH_LLCNFC_DEBUG("CONNECTION TIMER ID: 0x%02X\n", timerid);
    517 
    518     if (0 != timer_started)
    519     {
    520         /* Connection timer is started, so now stop it */
    521         ps_timer_info->timer_flag = (uint8_t)
    522                         SET_BITS8 (ps_timer_info->timer_flag,
    523                                     PH_LLCNFC_CON_TO_BIT,
    524                                     PH_LLCNFC_TO_NOOFBITS, 0);
    525 #if 0
    526 
    527         ps_timer_info->con_to_value = 0;
    528 
    529 #endif /* #if 0 */
    530 
    531         phOsalNfc_Timer_Stop (timerid);
    532     }
    533 
    534     timer_index = (uint8_t)(timer_index + 1);
    535     timerid = ps_timer_info->timer_id[timer_index];
    536     timer_started = (uint8_t)GET_BITS8 (ps_timer_info->timer_flag,
    537                                         PH_LLCNFC_GUARD_TO_BIT,
    538                                         PH_LLCNFC_TO_NOOFBITS);
    539 
    540     if (0 != timer_started)
    541     {
    542         /* Guard timer is already started */
    543         ps_timer_info->timer_flag = (uint8_t)
    544                         SET_BITS8 (ps_timer_info->timer_flag,
    545                                     PH_LLCNFC_GUARD_TO_BIT,
    546                                     PH_LLCNFC_TO_NOOFBITS, 0);
    547 
    548         timer_index = 0;
    549         ps_timer_info->guard_to_count = 0;
    550 
    551 #if 0
    552 
    553         /* Reset all the guard timer related variables */
    554         while (timer_index < ps_timer_info->guard_to_count)
    555         {
    556             ps_timer_info->guard_to_value[timer_index] = 0;
    557             ps_timer_info->iframe_send_count[timer_index] = 0;
    558 
    559             timer_index = (uint8_t)(timer_index + 1);
    560         }
    561 
    562 #endif /* #if 0 */
    563 
    564         PH_LLCNFC_DEBUG("GUARD TIMER ID: 0x%02X\n", timerid);
    565 
    566         /* Stop the timer */
    567         phOsalNfc_Timer_Stop (timerid);
    568 
    569         PH_LLCNFC_PRINT("\n\nLLC : STOP ALL TIMERS END \n\n");
    570     }
    571 
    572 #endif /* #ifdef LLC_TIMER_ENABLE */
    573 }
    574 
    575 void
    576 phLlcNfc_DeleteTimer (void)
    577 {
    578     uint8_t     index = 0;
    579     while (index < PH_LLCNFC_MAX_TIMER_USED)
    580     {
    581 #ifdef LLC_TIMER_ENABLE
    582         phOsalNfc_Timer_Delete(
    583             gpphLlcNfc_Ctxt->s_timerinfo.timer_id[index]);
    584         gpphLlcNfc_Ctxt->s_timerinfo.timer_id[index] =
    585                             PH_OSALNFC_INVALID_TIMER_ID;
    586 #endif /* #ifdef LLC_TIMER_ENABLE */
    587         index++;
    588     }
    589 }
    590 
    591 #ifdef LLC_TIMER_ENABLE
    592 
    593 #define LLC_GUARD_TIMER_RETRIES                         (0x03U)
    594 
    595 static
    596 void
    597 phLlcNfc_GuardTimeoutCb (
    598     uint32_t TimerId,
    599     void *pContext
    600 )
    601 {
    602     NFCSTATUS                   result = NFCSTATUS_SUCCESS;
    603     phLlcNfc_Timerinfo_t        *ps_timer_info = NULL;
    604     phLlcNfc_Frame_t            *ps_frame_info = NULL;
    605     phLlcNfc_LlcPacket_t        *ps_packet_info = NULL;
    606     uint8_t                     index = 0;
    607     uint8_t                     zero_to_index = 0;
    608 #if defined (GUARD_TO_ERROR)
    609     phNfc_sCompletionInfo_t     notifyinfo = {0};
    610 #endif /* #if defined (GUARD_TO_ERROR) */
    611     PHNFC_UNUSED_VARIABLE(pContext);
    612 
    613     PH_LLCNFC_PRINT("\n\nLLC : GUARD TIMEOUT CB CALLED \n\n");
    614 
    615     if ((NULL != gpphLlcNfc_Ctxt) && (TimerId ==
    616         gpphLlcNfc_Ctxt->s_timerinfo.timer_id[PH_LLCNFC_GUARDTIMER]) &&
    617         (PH_LLCNFC_GUARD_TO_BIT_VAL ==
    618         (gpphLlcNfc_Ctxt->s_timerinfo.timer_flag &
    619         PH_LLCNFC_GUARD_TO_BIT_VAL)))
    620     {
    621         uint8_t                 timer_expired = FALSE;
    622         ps_frame_info = &(gpphLlcNfc_Ctxt->s_frameinfo);
    623         ps_timer_info = &(gpphLlcNfc_Ctxt->s_timerinfo);
    624 
    625 #if !defined (CYCLIC_TIMER)
    626         phOsalNfc_Timer_Stop(
    627                     ps_timer_info->timer_id[PH_LLCNFC_GUARDTIMER]);
    628 #endif
    629 
    630         PH_LLCNFC_DEBUG("NO OF TIMEOUT COUNT : 0x%02X\n", ps_timer_info->guard_to_count);
    631         /* Loop is completely depending on the number of different LLC
    632            send called */
    633         while (index < ps_timer_info->guard_to_count)
    634         {
    635             if (0 != ps_timer_info->guard_to_value[index])
    636             {
    637                 if (ps_timer_info->guard_to_value[index] > 0)
    638                 {
    639                     if (ps_timer_info->guard_to_value[index] >=
    640                         PH_LLCNFC_RESOLUTION)
    641                     {
    642                         ps_timer_info->guard_to_value[index] = (uint16_t)
    643                             (ps_timer_info->guard_to_value[index] -
    644                             PH_LLCNFC_RESOLUTION);
    645                     }
    646                     else
    647                     {
    648                         ps_timer_info->guard_to_value[index] = 0;
    649                     }
    650                 }
    651 
    652                 if (0 == ps_timer_info->guard_to_value[index])
    653                 {
    654                     zero_to_index = index;
    655                     timer_expired = TRUE;
    656                 }
    657             }
    658             index = (uint8_t)(index + 1);
    659         }
    660 
    661 #if !defined (CYCLIC_TIMER)
    662         /* Start the timer again */
    663         phOsalNfc_Timer_Start(
    664                     ps_timer_info->timer_id[PH_LLCNFC_GUARDTIMER],
    665                     PH_LLCNFC_RESOLUTION, phLlcNfc_GuardTimeoutCb, NULL);
    666 #endif
    667         PH_LLCNFC_DEBUG("TIMER EXPIRED : 0x%02X\n", timer_expired);
    668 
    669         if (TRUE == timer_expired)
    670         {
    671             PH_LLCNFC_DEBUG("TIMER EXPIRED INDEX: 0x%02X\n", zero_to_index);
    672             PH_LLCNFC_DEBUG("TIMER EXPIRED NS INDEX: 0x%02X\n", ps_timer_info->timer_ns_value[zero_to_index]);
    673             PH_LLCNFC_DEBUG("TIMER EXPIRED RETRIES : 0x%02X\n", ps_timer_info->iframe_send_count[zero_to_index]);
    674 
    675             PH_LLCNFC_DEBUG("TIMER EXPIRED GUARD TIME-OUT COUNT: 0x%02X\n", ps_timer_info->guard_to_value[zero_to_index]);
    676 
    677             if ((0 == ps_timer_info->guard_to_value[zero_to_index]) &&
    678                 (ps_timer_info->iframe_send_count[zero_to_index] <
    679                 LLC_GUARD_TIMER_RETRIES))
    680             {
    681                 if (ps_frame_info->s_send_store.winsize_cnt > 0)
    682                 {
    683                     uint8_t             start_index = 0;
    684                     uint8_t             timer_count = 0;
    685                     uint8_t             while_exit = FALSE;
    686 
    687                     timer_count = ps_timer_info->guard_to_count;
    688 
    689 
    690                     /* Check before changing the index to resend, if index
    691                         already exist then dont set the index */
    692                     while ((FALSE == while_exit) && (start_index < timer_count))
    693                     {
    694                         if (resend_i_frame ==
    695                             ps_timer_info->frame_type[start_index])
    696                         {
    697                             while_exit = TRUE;
    698                         }
    699                         else
    700                         {
    701                             start_index = (uint8_t)(start_index + 1);
    702                         }
    703                     }
    704 
    705                     if (TRUE == while_exit)
    706                     {
    707                         ps_timer_info->index_to_send = zero_to_index;
    708                     }
    709 
    710                     ps_timer_info->frame_type[zero_to_index] = (uint8_t)
    711                                                             resend_i_frame;
    712                     /* Now resend the frame stored */
    713                     result = phLlcNfc_H_SendTimedOutIFrame (gpphLlcNfc_Ctxt,
    714                                             &(ps_frame_info->s_send_store),
    715                                             0);
    716                 }
    717             }
    718             else
    719             {
    720                 if ((LLC_GUARD_TIMER_RETRIES ==
    721                     ps_timer_info->iframe_send_count[zero_to_index]) &&
    722                     (NULL != gpphLlcNfc_Ctxt->cb_for_if.notify))
    723                 {
    724                     phLlcNfc_StopAllTimers ();
    725 #if defined (GUARD_TO_ERROR)
    726 
    727                     notifyinfo.status = PHNFCSTVAL(CID_NFC_LLC,
    728                                         NFCSTATUS_BOARD_COMMUNICATION_ERROR);
    729 #if 0
    730                     phOsalNfc_RaiseException(phOsalNfc_e_UnrecovFirmwareErr,1);
    731 #endif /* #if 0 */
    732                     /* Resend done, no answer from the device */
    733                     gpphLlcNfc_Ctxt->cb_for_if.notify (
    734                                     gpphLlcNfc_Ctxt->cb_for_if.pif_ctxt,
    735                                     gpphLlcNfc_Ctxt->phwinfo,
    736                                     NFC_NOTIFY_DEVICE_ERROR,
    737                                     &notifyinfo);
    738 
    739 #endif /* #if defined (GUARD_TO_ERROR) */
    740 
    741 #if (!defined (GUARD_TO_ERROR) && defined (GUARD_TO_URSET))
    742 
    743                     PH_LLCNFC_PRINT("U-RSET IS SENT \n");
    744                     ps_packet_info = &(gpphLlcNfc_Ctxt->s_frameinfo.s_llcpacket);
    745 
    746                     result = phLlcNfc_H_CreateUFramePayload(gpphLlcNfc_Ctxt,
    747                                         ps_packet_info,
    748                                         &(ps_packet_info->llcbuf_len),
    749                                         phLlcNfc_e_rset);
    750 
    751                     result = phLlcNfc_Interface_Write(gpphLlcNfc_Ctxt,
    752                                     (uint8_t*)&(ps_packet_info->s_llcbuf),
    753                                     (uint32_t)ps_packet_info->llcbuf_len);
    754 
    755                     ps_frame_info->write_status = result;
    756                     if (NFCSTATUS_PENDING == result)
    757                     {
    758                         /* Start the timer */
    759                         result = phLlcNfc_StartTimers (PH_LLCNFC_CONNECTIONTIMER, 0);
    760                         if (NFCSTATUS_SUCCESS == result)
    761                         {
    762                             ps_frame_info->retry_cnt = 0;
    763                             gpphLlcNfc_Ctxt->s_frameinfo.sent_frame_type =
    764                                                                     u_rset_frame;
    765                             result = NFCSTATUS_PENDING;
    766                         }
    767                     }
    768                     else
    769                     {
    770                         if (NFCSTATUS_BUSY == PHNFCSTATUS (result))
    771                         {
    772                             ps_frame_info->write_wait_call = u_rset_frame;
    773                         }
    774                     }
    775 
    776 #endif /* #if defined (GUARD_TO_ERROR) */
    777                 }
    778             }
    779         }
    780     }
    781     PH_LLCNFC_PRINT("\n\nLLC : GUARD TIMEOUT CB END\n\n");
    782 }
    783 
    784 #ifdef PIGGY_BACK
    785 static
    786 void
    787 phLlcNfc_AckTimeoutCb (
    788     uint32_t TimerId
    789 )
    790 {
    791 
    792 }
    793 #endif
    794 
    795 static
    796 void
    797 phLlcNfc_ConnectionTimeoutCb (
    798     uint32_t TimerId,
    799     void *pContext
    800 )
    801 {
    802     NFCSTATUS                   result = NFCSTATUS_SUCCESS;
    803     phNfc_sCompletionInfo_t     notifyinfo = {0};
    804     pphNfcIF_Notification_CB_t  notifyul = NULL;
    805     void                        *p_upperctxt = NULL;
    806     phLlcNfc_Frame_t            *ps_frame_info = NULL;
    807     phLlcNfc_Timerinfo_t        *ps_timer_info = NULL;
    808     PHNFC_UNUSED_VARIABLE(pContext);
    809 
    810     PH_LLCNFC_PRINT("\n\nLLC : CONNECTION TIMEOUT CB CALLED\n\n");
    811     if ((NULL != gpphLlcNfc_Ctxt) && (TimerId ==
    812         gpphLlcNfc_Ctxt->s_timerinfo.timer_id[PH_LLCNFC_CONNECTIONTIMER])
    813         && (PH_LLCNFC_CON_TO_BIT_VAL ==
    814         (gpphLlcNfc_Ctxt->s_timerinfo.timer_flag &
    815         PH_LLCNFC_CON_TO_BIT_VAL)))
    816     {
    817         ps_frame_info = &(gpphLlcNfc_Ctxt->s_frameinfo);
    818         ps_timer_info = &(gpphLlcNfc_Ctxt->s_timerinfo);
    819         if (ps_timer_info->con_to_value > 0)
    820         {
    821 #if !defined (CYCLIC_TIMER)
    822             phOsalNfc_Timer_Stop(
    823                     ps_timer_info->timer_id[PH_LLCNFC_CONNECTIONTIMER]);
    824             /* phLlcNfc_StopTimers(PH_LLCNFC_CONNECTIONTIMER, 0); */
    825 #endif
    826             ps_timer_info->con_to_value = 0;
    827 
    828             if (0 == ps_timer_info->con_to_value)
    829             {
    830                 PH_LLCNFC_DEBUG("TIMER EXPIRED RETRY COUNT : %02X\n", ps_frame_info->retry_cnt);
    831                 phLlcNfc_StopTimers (PH_LLCNFC_CONNECTIONTIMER, 0);
    832 
    833                 if (ps_frame_info->retry_cnt < PH_LLCNFC_MAX_RETRY_COUNT)
    834                 {
    835                     /* Create a U frame */
    836                     result = phLlcNfc_H_CreateUFramePayload(gpphLlcNfc_Ctxt,
    837                                         &(ps_frame_info->s_llcpacket),
    838                                         &(ps_frame_info->s_llcpacket.llcbuf_len),
    839                                         phLlcNfc_e_rset);
    840 
    841                     if (NFCSTATUS_SUCCESS == result)
    842                     {
    843                         /* Call DAL write */
    844                         result = phLlcNfc_Interface_Write (gpphLlcNfc_Ctxt,
    845                                 (uint8_t*)&(ps_frame_info->s_llcpacket.s_llcbuf),
    846                                 (uint32_t)(ps_frame_info->s_llcpacket.llcbuf_len));
    847                     }
    848                     if (NFCSTATUS_PENDING == result)
    849                     {
    850                         /* Start the timer */
    851                         result = phLlcNfc_StartTimers(PH_LLCNFC_CONNECTIONTIMER, 0);
    852                         if (NFCSTATUS_SUCCESS == result)
    853                         {
    854                             ps_frame_info->retry_cnt++;
    855                             result = NFCSTATUS_PENDING;
    856                         }
    857                     }
    858                     else
    859                     {
    860                         if (NFCSTATUS_BUSY == PHNFCSTATUS(result))
    861                         {
    862                             result = NFCSTATUS_PENDING;
    863                         }
    864                     }
    865                 }
    866                 else
    867                 {
    868                     PH_LLCNFC_PRINT("RETRY COUNT LIMIT REACHED \n");
    869                     if ((ps_frame_info->retry_cnt == PH_LLCNFC_MAX_RETRY_COUNT)
    870                         && (NULL != gpphLlcNfc_Ctxt->cb_for_if.notify))
    871                     {
    872                         void            *p_hw_info = NULL;
    873                         uint8_t         type = 0;
    874 
    875                         p_hw_info = gpphLlcNfc_Ctxt->phwinfo;
    876                         notifyinfo.status = PHNFCSTVAL(CID_NFC_LLC,
    877                                             NFCSTATUS_BOARD_COMMUNICATION_ERROR);
    878 
    879                         notifyul = gpphLlcNfc_Ctxt->cb_for_if.notify;
    880                         p_upperctxt = gpphLlcNfc_Ctxt->cb_for_if.pif_ctxt;
    881                         type = NFC_NOTIFY_ERROR;
    882                         if (init_u_rset_frame == ps_frame_info->sent_frame_type)
    883                         {
    884                             type = NFC_NOTIFY_INIT_FAILED;
    885                             /* Release if, the initialisation is not complete */
    886                             result = phLlcNfc_Release(gpphLlcNfc_Ctxt, p_hw_info);
    887                             gpphLlcNfc_Ctxt = NULL;
    888                         }
    889                         else
    890                         {
    891                             type = NFC_NOTIFY_DEVICE_ERROR;
    892                             notifyinfo.status = PHNFCSTVAL(CID_NFC_LLC,
    893                                             NFCSTATUS_BOARD_COMMUNICATION_ERROR);
    894 #if 0
    895                             phOsalNfc_RaiseException(phOsalNfc_e_UnrecovFirmwareErr,1);
    896 #endif /* #if 0 */
    897                         }
    898                         /* Notify the upper layer */
    899                         notifyul(p_upperctxt, p_hw_info, type, &notifyinfo);
    900                     }
    901                 }
    902             }
    903 #if !defined (CYCLIC_TIMER)
    904             else
    905             {
    906                 /* Start the timer again */
    907                 phOsalNfc_Timer_Start(
    908                             ps_timer_info->timer_id[PH_LLCNFC_CONNECTIONTIMER],
    909                             ps_timer_info->con_to_value, phLlcNfc_ConnectionTimeoutCb, NULL);
    910             }
    911 #endif
    912         }
    913     }
    914     PH_LLCNFC_PRINT("\n\nLLC : CONNECTION TIMEOUT CB END\n\n");
    915 }
    916 #endif /* #ifdef LLC_TIMER_ENABLE */
    917 
    918 #ifdef LLC_URSET_NO_DELAY
    919 
    920     /* NO definition required */
    921 
    922 #else /* #ifdef LLC_URSET_NO_DELAY */
    923 
    924 void
    925 phLlcNfc_URSET_Delay_Notify (
    926     uint32_t            delay_id,
    927     void                *pContext)
    928 {
    929     phLlcNfc_Frame_t            *ps_frame_info = NULL;
    930     phNfc_sCompletionInfo_t     notifyinfo = {0};
    931 
    932     if (NULL != gpphLlcNfc_Ctxt)
    933     {
    934         ps_frame_info = &(gpphLlcNfc_Ctxt->s_frameinfo);
    935 
    936         phOsalNfc_Timer_Stop (delay_id);
    937         phOsalNfc_Timer_Delete (delay_id);
    938         if (ps_frame_info->s_send_store.winsize_cnt > 0)
    939         {
    940 #if 0
    941 
    942             /* Resend I frame */
    943             (void)phLlcNfc_H_SendTimedOutIFrame (gpphLlcNfc_Ctxt,
    944                                         &(ps_frame_info->s_send_store), 0);
    945 
    946 #else
    947 
    948             (void)phLlcNfc_H_SendUserIFrame (gpphLlcNfc_Ctxt,
    949                                         &(ps_frame_info->s_send_store));
    950 
    951 #endif /* #if 0 */
    952             gpphLlcNfc_Ctxt->state = phLlcNfc_Resend_State;
    953         }
    954         else
    955         {
    956             if ((init_u_rset_frame == ps_frame_info->sent_frame_type) &&
    957                 (NULL != gpphLlcNfc_Ctxt->cb_for_if.notify))
    958             {
    959                 ps_frame_info->sent_frame_type = write_resp_received;
    960                 notifyinfo.status = NFCSTATUS_SUCCESS;
    961                 /* Send the notification to the upper layer */
    962                 gpphLlcNfc_Ctxt->cb_for_if.notify (
    963                             gpphLlcNfc_Ctxt->cb_for_if.pif_ctxt,
    964                             gpphLlcNfc_Ctxt->phwinfo,
    965                             NFC_NOTIFY_INIT_COMPLETED,
    966                             &notifyinfo);
    967             }
    968         }
    969     }
    970 }
    971 
    972 #endif /* #ifdef LLC_URSET_NO_DELAY */
    973 
    974