aboutsummaryrefslogtreecommitdiff
path: root/pn54x/tml/phOsalNfc_Timer.c
blob: 3f5269dc064f7861301af0f23cea4b4f6a044118 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
/*
 * Copyright (C) 2010-2014 NXP Semiconductors
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/*
 * OSAL Implementation for Timers.
 */

#include <phNfcCommon.h>
#include <phNfcTypes.h>
#include <phNxpLog.h>
#include <phNxpNciHal.h>
#include <phOsalNfc_Timer.h>
#include <signal.h>

#define PH_NFC_MAX_TIMER (5U)
static phOsalNfc_TimerHandle_t apTimerInfo[PH_NFC_MAX_TIMER];

extern phNxpNciHal_Control_t nxpncihal_ctrl;

/*
 * Defines the base address for generating timerid.
 */
#define PH_NFC_TIMER_BASE_ADDRESS (100U)

/*
 *  Defines the value for invalid timerid returned during timeSetEvent
 */
#define PH_NFC_TIMER_ID_ZERO (0x00)

/*
 * Invalid timer ID type. This ID used indicate timer creation is failed */
#define PH_NFC_TIMER_ID_INVALID (0xFFFF)

/* Forward declarations */
static void phOsalNfc_PostTimerMsg(phLibNfc_Message_t* pMsg);
static void phOsalNfc_DeferredCall(void* pParams);
static void phOsalNfc_Timer_Expired(union sigval sv);

/*
 *************************** Function Definitions ******************************
 */

/*******************************************************************************
**
** Function         phOsalNfc_Timer_Create
**
** Description      Creates a timer which shall call back the specified function
**                  when the timer expires. Fails if OSAL module is not
**                  initialized or timers are already occupied
**
** Parameters       None
**
** Returns          TimerId
**                  TimerId value of PH_OSALNFC_TIMER_ID_INVALID indicates that
**                  timer is not created
**
*******************************************************************************/
uint32_t phOsalNfc_Timer_Create(void) {
  /* dwTimerId is also used as an index at which timer object can be stored */
  uint32_t dwTimerId = PH_OSALNFC_TIMER_ID_INVALID;
  static struct sigevent se;
  phOsalNfc_TimerHandle_t* pTimerHandle;
  /* Timer needs to be initialized for timer usage */

  se.sigev_notify = SIGEV_THREAD;
  se.sigev_notify_function = phOsalNfc_Timer_Expired;
  se.sigev_notify_attributes = NULL;
  dwTimerId = phUtilNfc_CheckForAvailableTimer();

  /* Check whether timers are available, if yes create a timer handle structure
   */
  if ((PH_NFC_TIMER_ID_ZERO != dwTimerId) && (dwTimerId <= PH_NFC_MAX_TIMER)) {
    pTimerHandle = (phOsalNfc_TimerHandle_t*)&apTimerInfo[dwTimerId - 1];
    /* Build the Timer Id to be returned to Caller Function */
    dwTimerId += PH_NFC_TIMER_BASE_ADDRESS;
    se.sigev_value.sival_int = (int)dwTimerId;
    /* Create POSIX timer */
    if (timer_create(CLOCK_REALTIME, &se, &(pTimerHandle->hTimerHandle)) ==
        -1) {
      dwTimerId = PH_NFC_TIMER_ID_INVALID;
    } else {
      /* Set the state to indicate timer is ready */
      pTimerHandle->eState = eTimerIdle;
      /* Store the Timer Id which shall act as flag during check for timer
       * availability */
      pTimerHandle->TimerId = dwTimerId;
    }
  } else {
    dwTimerId = PH_NFC_TIMER_ID_INVALID;
  }

  /* Timer ID invalid can be due to Uninitialized state,Non availability of
   * Timer */
  return dwTimerId;
}

/*******************************************************************************
**
** Function         phOsalNfc_Timer_Start
**
** Description      Starts the requested, already created, timer.
**                  If the timer is already running, timer stops and restarts
**                  with the new timeout value and new callback function in case
**                  any ??????
**                  Creates a timer which shall call back the specified function
**                  when the timer expires
**
** Parameters       dwTimerId - valid timer ID obtained during timer creation
**                  dwRegTimeCnt - requested timeout in milliseconds
**                  pApplication_callback - application callback interface to be
**                                          called when timer expires
**                  pContext - caller context, to be passed to the application
**                             callback function
**
** Returns          NFC status:
**                  NFCSTATUS_SUCCESS - the operation was successful
**                  NFCSTATUS_NOT_INITIALISED - OSAL Module is not initialized
**                  NFCSTATUS_INVALID_PARAMETER - invalid parameter passed to
**                                                the function
**                  PH_OSALNFC_TIMER_START_ERROR - timer could not be created
**                                                 due to system error
**
*******************************************************************************/
NFCSTATUS phOsalNfc_Timer_Start(uint32_t dwTimerId, uint32_t dwRegTimeCnt,
                                pphOsalNfc_TimerCallbck_t pApplication_callback,
                                void* pContext) {
  NFCSTATUS wStartStatus = NFCSTATUS_SUCCESS;

  struct itimerspec its;
  uint32_t dwIndex;
  phOsalNfc_TimerHandle_t* pTimerHandle;
  /* Retrieve the index at which the timer handle structure is stored */
  dwIndex = dwTimerId - PH_NFC_TIMER_BASE_ADDRESS - 0x01;
  pTimerHandle = (phOsalNfc_TimerHandle_t*)&apTimerInfo[dwIndex];
  /* OSAL Module needs to be initialized for timer usage */
  /* Check whether the handle provided by user is valid */
  if ((dwIndex < PH_NFC_MAX_TIMER) && (0x00 != pTimerHandle->TimerId) &&
      (NULL != pApplication_callback)) {
    its.it_interval.tv_sec = 0;
    its.it_interval.tv_nsec = 0;
    its.it_value.tv_sec = dwRegTimeCnt / 1000;
    its.it_value.tv_nsec = 1000000 * (dwRegTimeCnt % 1000);
    if (its.it_value.tv_sec == 0 && its.it_value.tv_nsec == 0) {
      /* This would inadvertently stop the timer*/
      its.it_value.tv_nsec = 1;
    }
    pTimerHandle->Application_callback = pApplication_callback;
    pTimerHandle->pContext = pContext;
    pTimerHandle->eState = eTimerRunning;
    /* Arm the timer */
    if ((timer_settime(pTimerHandle->hTimerHandle, 0, &its, NULL)) == -1) {
      wStartStatus = PHNFCSTVAL(CID_NFC_OSAL, PH_OSALNFC_TIMER_START_ERROR);
    }
  } else {
    wStartStatus = PHNFCSTVAL(CID_NFC_OSAL, NFCSTATUS_INVALID_PARAMETER);
  }

  return wStartStatus;
}

/*******************************************************************************
**
** Function         phOsalNfc_Timer_Stop
**
** Description      Stops already started timer
**                  Allows to stop running timer. In case timer is stopped,
**                  timer callback will not be notified any more
**
** Parameters       dwTimerId - valid timer ID obtained during timer creation
**
** Returns          NFC status:
**                  NFCSTATUS_SUCCESS - the operation was successful
**                  NFCSTATUS_NOT_INITIALISED - OSAL Module is not initialized
**                  NFCSTATUS_INVALID_PARAMETER - invalid parameter passed to
**                                                the function
**                  PH_OSALNFC_TIMER_STOP_ERROR - timer could not be stopped due
**                                                to system error
**
*******************************************************************************/
NFCSTATUS phOsalNfc_Timer_Stop(uint32_t dwTimerId) {
  NFCSTATUS wStopStatus = NFCSTATUS_SUCCESS;
  static struct itimerspec its = {{0, 0}, {0, 0}};

  uint32_t dwIndex;
  phOsalNfc_TimerHandle_t* pTimerHandle;
  dwIndex = dwTimerId - PH_NFC_TIMER_BASE_ADDRESS - 0x01;
  pTimerHandle = (phOsalNfc_TimerHandle_t*)&apTimerInfo[dwIndex];
  /* OSAL Module and Timer needs to be initialized for timer usage */
  /* Check whether the TimerId provided by user is valid */
  if ((dwIndex < PH_NFC_MAX_TIMER) && (0x00 != pTimerHandle->TimerId) &&
      (pTimerHandle->eState != eTimerIdle)) {
    /* Stop the timer only if the callback has not been invoked */
    if (pTimerHandle->eState == eTimerRunning) {
      if ((timer_settime(pTimerHandle->hTimerHandle, 0, &its, NULL)) == -1) {
        wStopStatus = PHNFCSTVAL(CID_NFC_OSAL, PH_OSALNFC_TIMER_STOP_ERROR);
      } else {
        /* Change the state of timer to Stopped */
        pTimerHandle->eState = eTimerStopped;
      }
    }
  } else {
    wStopStatus = PHNFCSTVAL(CID_NFC_OSAL, NFCSTATUS_INVALID_PARAMETER);
  }

  return wStopStatus;
}

/*******************************************************************************
**
** Function         phOsalNfc_Timer_Delete
**
** Description      Deletes previously created timer
**                  Allows to delete previously created timer. In case timer is
**                  running, it is first stopped and then deleted
**
** Parameters       dwTimerId - valid timer ID obtained during timer creation
**
** Returns          NFC status:
**                  NFCSTATUS_SUCCESS - the operation was successful
**                  NFCSTATUS_NOT_INITIALISED - OSAL Module is not initialized
**                  NFCSTATUS_INVALID_PARAMETER - invalid parameter passed to
**                                                the function
**                  PH_OSALNFC_TIMER_DELETE_ERROR - timer could not be stopped
**                                                  due to system error
**
*******************************************************************************/
NFCSTATUS phOsalNfc_Timer_Delete(uint32_t dwTimerId) {
  NFCSTATUS wDeleteStatus = NFCSTATUS_SUCCESS;

  uint32_t dwIndex;
  phOsalNfc_TimerHandle_t* pTimerHandle;
  dwIndex = dwTimerId - PH_NFC_TIMER_BASE_ADDRESS - 0x01;
  pTimerHandle = (phOsalNfc_TimerHandle_t*)&apTimerInfo[dwIndex];
  /* OSAL Module and Timer needs to be initialized for timer usage */

  /* Check whether the TimerId passed by user is valid and Deregistering of
   * timer is successful */
  if ((dwIndex < PH_NFC_MAX_TIMER) && (0x00 != pTimerHandle->TimerId) &&
      (NFCSTATUS_SUCCESS == phOsalNfc_CheckTimerPresence(pTimerHandle))) {
    /* Cancel the timer before deleting */
    if (timer_delete(pTimerHandle->hTimerHandle) == -1) {
      wDeleteStatus = PHNFCSTVAL(CID_NFC_OSAL, PH_OSALNFC_TIMER_DELETE_ERROR);
    }
    /* Clear Timer structure used to store timer related data */
    memset(pTimerHandle, (uint8_t)0x00, sizeof(phOsalNfc_TimerHandle_t));
  } else {
    wDeleteStatus = PHNFCSTVAL(CID_NFC_OSAL, NFCSTATUS_INVALID_PARAMETER);
  }
  return wDeleteStatus;
}

/*******************************************************************************
**
** Function         phOsalNfc_Timer_Cleanup
**
** Description      Deletes all previously created timers
**                  Allows to delete previously created timers. In case timer is
**                  running, it is first stopped and then deleted
**
** Parameters       None
**
** Returns          None
**
*******************************************************************************/
void phOsalNfc_Timer_Cleanup(void) {
  /* Delete all timers */
  uint32_t dwIndex;
  phOsalNfc_TimerHandle_t* pTimerHandle;
  for (dwIndex = 0; dwIndex < PH_NFC_MAX_TIMER; dwIndex++) {
    pTimerHandle = (phOsalNfc_TimerHandle_t*)&apTimerInfo[dwIndex];
    /* OSAL Module and Timer needs to be initialized for timer usage */

    /* Check whether the TimerId passed by user is valid and Deregistering of
     * timer is successful */
    if ((0x00 != pTimerHandle->TimerId) &&
        (NFCSTATUS_SUCCESS == phOsalNfc_CheckTimerPresence(pTimerHandle))) {
      /* Cancel the timer before deleting */
      if (timer_delete(pTimerHandle->hTimerHandle) == -1) {
        NXPLOG_TML_E("timer %d delete error!", dwIndex);
      }
      /* Clear Timer structure used to store timer related data */
      memset(pTimerHandle, (uint8_t)0x00, sizeof(phOsalNfc_TimerHandle_t));
    }
  }

  return;
}

/*******************************************************************************
**
** Function         phOsalNfc_DeferredCall
**
** Description      Invokes the timer callback function after timer expiration.
**                  Shall invoke the callback function registered by the timer
**                  caller function
**
** Parameters       pParams - parameters indicating the ID of the timer
**
** Returns          None                -
**
*******************************************************************************/
static void phOsalNfc_DeferredCall(void* pParams) {
  /* Retrieve the timer id from the parameter */
  uint32_t dwIndex;
  phOsalNfc_TimerHandle_t* pTimerHandle;
  if (NULL != pParams) {
    /* Retrieve the index at which the timer handle structure is stored */
    dwIndex = (uintptr_t)pParams - PH_NFC_TIMER_BASE_ADDRESS - 0x01;
    pTimerHandle = (phOsalNfc_TimerHandle_t*)&apTimerInfo[dwIndex];
    if (pTimerHandle->Application_callback != NULL) {
      /* Invoke the callback function with osal Timer ID */
      pTimerHandle->Application_callback((uintptr_t)pParams,
                                         pTimerHandle->pContext);
    }
  }

  return;
}

/*******************************************************************************
**
** Function         phOsalNfc_PostTimerMsg
**
** Description      Posts message on the user thread
**                  Shall be invoked upon expiration of a timer
**                  Shall post message on user thread through which timer
**                  callback function shall be invoked
**
** Parameters       pMsg - pointer to the message structure posted on user
**                         thread
**
** Returns          None
**
*******************************************************************************/
static void phOsalNfc_PostTimerMsg(phLibNfc_Message_t* pMsg) {
  (void)phDal4Nfc_msgsnd(
      nxpncihal_ctrl.gDrvCfg
          .nClientId /*gpphOsalNfc_Context->dwCallbackThreadID*/,
      pMsg, 0);

  return;
}

/*******************************************************************************
**
** Function         phOsalNfc_Timer_Expired
**
** Description      posts message upon expiration of timer
**                  Shall be invoked when any one timer is expired
**                  Shall post message on user thread to invoke respective
**                  callback function provided by the caller of Timer function
**
** Returns          None
**
*******************************************************************************/
static void phOsalNfc_Timer_Expired(union sigval sv) {
  uint32_t dwIndex;
  phOsalNfc_TimerHandle_t* pTimerHandle;

  dwIndex = ((uint32_t)(sv.sival_int)) - PH_NFC_TIMER_BASE_ADDRESS - 0x01;
  pTimerHandle = (phOsalNfc_TimerHandle_t*)&apTimerInfo[dwIndex];
  /* Timer is stopped when callback function is invoked */
  pTimerHandle->eState = eTimerStopped;

  pTimerHandle->tDeferedCallInfo.pDeferedCall = &phOsalNfc_DeferredCall;
  pTimerHandle->tDeferedCallInfo.pParam = (void*)((intptr_t)(sv.sival_int));

  pTimerHandle->tOsalMessage.eMsgType = PH_LIBNFC_DEFERREDCALL_MSG;
  pTimerHandle->tOsalMessage.pMsgData = (void*)&pTimerHandle->tDeferedCallInfo;

  /* Post a message on the queue to invoke the function */
  phOsalNfc_PostTimerMsg((phLibNfc_Message_t*)&pTimerHandle->tOsalMessage);

  return;
}

/*******************************************************************************
**
** Function         phUtilNfc_CheckForAvailableTimer
**
** Description      Find an available timer id
**
** Parameters       void
**
** Returns          Available timer id
**
*******************************************************************************/
uint32_t phUtilNfc_CheckForAvailableTimer(void) {
  /* Variable used to store the index at which the object structure details
     can be stored. Initialize it as not available. */
  uint32_t dwIndex = 0x00;
  uint32_t dwRetval = 0x00;

  /* Check whether Timer object can be created */
  for (dwIndex = 0x00; ((dwIndex < PH_NFC_MAX_TIMER) && (0x00 == dwRetval));
       dwIndex++) {
    if (!(apTimerInfo[dwIndex].TimerId)) {
      dwRetval = (dwIndex + 0x01);
    }
  }

  return (dwRetval);
}

/*******************************************************************************
**
** Function         phOsalNfc_CheckTimerPresence
**
** Description      Checks the requested timer is present or not
**
** Parameters       pObjectHandle - timer context
**
** Returns          NFCSTATUS_SUCCESS if found
**                  Other value if not found
**
*******************************************************************************/
NFCSTATUS phOsalNfc_CheckTimerPresence(void* pObjectHandle) {
  uint32_t dwIndex;
  NFCSTATUS wRegisterStatus = NFCSTATUS_INVALID_PARAMETER;

  for (dwIndex = 0x00;
       ((dwIndex < PH_NFC_MAX_TIMER) && (wRegisterStatus != NFCSTATUS_SUCCESS));
       dwIndex++) {
    /* For Timer, check whether the requested handle is present or not */
    if (((&apTimerInfo[dwIndex]) == (phOsalNfc_TimerHandle_t*)pObjectHandle) &&
        (apTimerInfo[dwIndex].TimerId)) {
      wRegisterStatus = NFCSTATUS_SUCCESS;
    }
  }
  return wRegisterStatus;
}