summaryrefslogtreecommitdiffstats
path: root/nsframework/framework_unified/client/NS_Timer/src/ns_timer.c
blob: d24264b1e5b11da5789b62aa4aed1ac0bc9b6552 (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
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
/*
 * @copyright Copyright (c) 2016-2020 TOYOTA MOTOR CORPORATION.
 *
 * 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.
 */

///////////////////////////////////////////////////////////////////////////////
/// \ingroup  tag_NSTimer
/// \brief    Native Services Timer Interface code
///
///
///
///////////////////////////////////////////////////////////////////////////////

#include <unistd.h>
#include <string.h>
#include <malloc.h>
#include <errno.h>
#include <sys/epoll.h>
#include <sys/eventfd.h>
#include <sys/timerfd.h>
#include <sys/prctl.h>

#include <native_service/ns_timer_if.h>
#include <ns_timer_handle.h>
#include <native_service/ns_message_center_if.h>
#include <ns_timer_internal.h>
#include <native_service/ns_logger_if.h>
#include <native_service/ns_resource_controler.h>

#define INVALID_TIMERFD    -1

#define RUNNING_STATE       0
#define DELETING_STATE      1
#define DELETED_STATE       2

#define MAX_FD_EPOLL 10

int epollFd;    // To listen to multiple timerfd events.
int eventFd;    // To use not complete at epoll_wait
pthread_t timerTh_id = 0;  // Thread ID of the TimerMonitoringThread
pthread_mutex_t m_mtx = PTHREAD_MUTEX_INITIALIZER;

const UI_32 MS_IN_SEC = 1000;
const UI_64 NS_IN_MS  = 1000000;

BOOL DebugFlag = FALSE;

static PVOID TimerMonitoringThread(PVOID args) {
  struct epoll_event events[MAX_FD_EPOLL];
  int nfds;   // The number of events received
  int n;      // Loop counter
  PNSTimerHandle hTimer;
  char *p, name[32];
  uint64_t exp;

  // Thread naming
#define NSTIMER_APPEND_NAME "_T"
#ifndef NSTIMER_SIZE_PROCESSNAME
#define NSTIMER_SIZE_PROCESSNAME 15  // Profiler analysis tool name length limitations
#endif
  prctl(PR_GET_NAME, name);
  name[NSTIMER_SIZE_PROCESSNAME] = '\0';
  if (strlen(name) + strlen(NSTIMER_APPEND_NAME) > NSTIMER_SIZE_PROCESSNAME) {
    p = name + NSTIMER_SIZE_PROCESSNAME - strlen(NSTIMER_APPEND_NAME);
  } else {
    p = name + strlen(name);
  }
  strcpy(p, NSTIMER_APPEND_NAME);
  prctl(PR_SET_NAME, name);

  for (;;) {
    nfds = epoll_wait(epollFd, events, MAX_FD_EPOLL, -1);
    if (-1 != nfds) {
      for (n = 0; n < nfds; ++n) {
        hTimer = (PNSTimerHandle)events[n].data.ptr;
        if (NULL != hTimer) {
          if (-1 == read(hTimer->timerfd, &exp, sizeof(uint64_t))) {
            if (errno != EAGAIN) {
              // LCOV_EXCL_BR_START 15:marco defined in "native_service/ns_logger_if.h"
              FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __FUNCTION__, "Failed to read in timeout : fd=%d, errno=%d",
                                                                hTimer->timerfd, errno);
              // LCOV_EXCL_BR_STOP
            }
            continue;
          }

          if (RUNNING_STATE == hTimer->timerState) {
            if (NULL != hTimer->tTimerInfo) {
              EFrameworkunifiedStatus eStatus;
              HANDLE hReceiver = NULL;
              if (frameworkunifiedAcquireResouce(FRAMEWORKUNIFIED_RES_TIMER, hTimer->tTimerInfo->q_name, (long *)&hReceiver) < 0) {
                if ((hReceiver = McOpenSender(hTimer->tTimerInfo->q_name)) == NULL) {
                  // LCOV_EXCL_BR_START 15:marco defined in "native_service/ns_logger_if.h"
                  FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __FUNCTION__, "ERROR : McOpenSender is Failed");
                  // LCOV_EXCL_BR_STOP
                  continue;
                } else {
                  if (frameworkunifiedRegistResouce(FRAMEWORKUNIFIED_RES_TIMER, hTimer->tTimerInfo->q_name, (long)hReceiver, 1) < 0) {
                    // LCOV_EXCL_BR_START 15:marco defined in "native_service/ns_logger_if.h"
                    FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __FUNCTION__, "ERROR : frameworkunifiedRegistResouce is Failed : q_name=%s",
                                                                hTimer->tTimerInfo->q_name);
                    // LCOV_EXCL_BR_STOP
                  }
                }
              }

              eStatus = McSendWithPriority(hReceiver,
                                           TIMER_QUE,
                                           hTimer->tTimerInfo->iCmd,
                                           0,
                                           NULL,
                                           eFrameworkunifiedMsgPrioEmergency,
                                           0);
              if (eFrameworkunifiedStatusOK != eStatus) {
                // LCOV_EXCL_BR_START 15:marco defined in "native_service/ns_logger_if.h"
                FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __FUNCTION__, "ERROR : McSendWithPriority to %s is Failed, eStatus=%d", hTimer->tTimerInfo->q_name,
                       eStatus);
                // LCOV_EXCL_BR_STOP
              }
            } else {
              // LCOV_EXCL_BR_START 15:marco defined in "native_service/ns_logger_if.h"
              FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __FUNCTION__, "ERROR(RUNNING_STATE) : tTimerInfo is NULL");
              // LCOV_EXCL_BR_STOP
            }
          } else if (DELETED_STATE == hTimer->timerState) {
            if (-1 == epoll_ctl(epollFd, EPOLL_CTL_DEL, hTimer->timerfd, events)) {
              // LCOV_EXCL_BR_START 15:marco defined in "native_service/ns_logger_if.h"
              FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __FUNCTION__, "ERROR : epoll_ctl(DEL) Failed, fd=%d, errno=%d",
                     hTimer->timerfd, errno);
              // LCOV_EXCL_BR_STOP
            }

            if (-1 == close(hTimer->timerfd)) {
              // LCOV_EXCL_BR_START 15:marco defined in "native_service/ns_logger_if.h"
              FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __FUNCTION__, "ERROR : close(fd=%d) Failed, errno=%d", hTimer->timerfd, errno);
              // LCOV_EXCL_BR_STOP
            }

            hTimer->timerfd = INVALID_TIMERFD;

            if (NULL != hTimer->tTimerInfo) {
              HANDLE hReceiver;
              if (frameworkunifiedAcquireResouce(FRAMEWORKUNIFIED_RES_TIMER, hTimer->tTimerInfo->q_name, (long *)&hReceiver) >= 0) {
                if (frameworkunifiedReleaseResouce(FRAMEWORKUNIFIED_RES_TIMER, hTimer->tTimerInfo->q_name) <= 0) {
                  frameworkunifiedUnregistResouce(FRAMEWORKUNIFIED_RES_TIMER, hTimer->tTimerInfo->q_name);
                  McClose(hReceiver);
                }
              }
              free(hTimer->tTimerInfo->q_name);
              free(hTimer->tTimerInfo);   // delete the timer info
              hTimer->tTimerInfo = NULL;
            } else {
              // LCOV_EXCL_BR_START 15:marco defined in "native_service/ns_logger_if.h"
              FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __FUNCTION__, "ERROR(DELETED_STATE) : tTimerInfo is NULL");
              // LCOV_EXCL_BR_STOP
            }

            free(hTimer);   // delete the handle
            events[n].data.ptr = NULL;
          } else {
            // do nothing
          }
        } else {
          // LCOV_EXCL_BR_START 15:marco defined in "native_service/ns_logger_if.h"
          FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __FUNCTION__, "ERROR : hTimer is NULL");
          // LCOV_EXCL_BR_STOP
        }
      }
    } else {
      if (errno == EINTR) {
        // signal interrupt
      } else {
        // LCOV_EXCL_BR_START 15:marco defined in "native_service/ns_logger_if.h"
        FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __FUNCTION__, "ERROR : epoll_wait Failed, errno=%d", errno);
        // LCOV_EXCL_BR_STOP
      }
    }
  }
  return NULL;
}

static EFrameworkunifiedStatus CreateTimerMonitoringThread() {
  EFrameworkunifiedStatus eStatus = eFrameworkunifiedStatusOK;
  struct epoll_event ev;  // Info struct to associate with multiwaiting FD
  int ret;

  pthread_mutex_lock(&m_mtx);

  // Create the TimerMonitoringThread If the thread has not been generated.
  if (0 == timerTh_id) {
    epollFd = epoll_create1(EPOLL_CLOEXEC);
    if (-1 != epollFd) {
      eventFd = eventfd(0, EFD_CLOEXEC);
      if (-1 != eventFd) {
        ev.events = EPOLLIN;
        ev.data.fd = eventFd;
        if (-1 != epoll_ctl(epollFd, EPOLL_CTL_ADD, eventFd, &ev)) {
          if (0 != (ret = pthread_create(&timerTh_id, NULL, TimerMonitoringThread, NULL))) {
            eStatus = eFrameworkunifiedStatusFail;
            // LCOV_EXCL_BR_START 15:marco defined in "native_service/ns_logger_if.h"
            FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __FUNCTION__, "Failed to pthread_create : errno %d", ret);
            // LCOV_EXCL_BR_STOP
          }
        } else {
          eStatus = eFrameworkunifiedStatusFail;
          // LCOV_EXCL_BR_START 15:marco defined in "native_service/ns_logger_if.h"
          FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __FUNCTION__, "ERROR : epoll_ctl(eventFd=%d, ADD) Failed, status=%d, errno=%d", eventFd, eStatus,
                 errno);
          // LCOV_EXCL_BR_STOP
        }
      } else {
        eStatus = eFrameworkunifiedStatusFail;
        // LCOV_EXCL_BR_START 15:marco defined in "native_service/ns_logger_if.h"
        FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __FUNCTION__, "ERROR :eventfd Failed, status=%d, errno=%d", eStatus, errno);
        // LCOV_EXCL_BR_STOP
      }
    } else {
      eStatus = eFrameworkunifiedStatusFail;
      // LCOV_EXCL_BR_START 15:marco defined in "native_service/ns_logger_if.h"
      FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __FUNCTION__, "ERROR : epoll_create1 Failed, status=%d, errno=%d",
             eStatus, errno);
      // LCOV_EXCL_BR_STOP
    }

  }

  if (TRUE == DebugFlag) {  // LCOV_EXCL_BR_LINE 7: debug code
    // LCOV_EXCL_START 7: debug code
    AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
    FRAMEWORKUNIFIEDLOG(ZONE_NS_IMP_INFO, __FUNCTION__, "[DEBUG] sleep 3 Sec START.");
    sleep(3);
    FRAMEWORKUNIFIEDLOG(ZONE_NS_IMP_INFO, __FUNCTION__, "[DEBUG] sleep 3 Sec END.");
    // LCOV_EXCL_STOP
  }
  pthread_mutex_unlock(&m_mtx);

  return eStatus;
}

HANDLE NS_TimerCreate(NSTimerInfo timer_info, eNSTimerCallbackMechanism cbMech, HANDLE sndMqHndl) {
  PNSTimerHandle hTimer = NULL;

  if ((NULL != sndMqHndl) && (cbMech == CALLBACK_MESSAGE)) {
    EFrameworkunifiedStatus eStatus = eFrameworkunifiedStatusOK;

    PTimerInfo pTimerInfo = NULL;
    struct epoll_event ev;  // Info struct to associate with multiwaiting FD
    int timerfd = INVALID_TIMERFD;

    eStatus = CreateTimerMonitoringThread();
    if (eFrameworkunifiedStatusOK == eStatus) {
      hTimer = (PNSTimerHandle)malloc(sizeof(NSTimerHandle));
      if (NULL != hTimer) {  // LCOV_EXCL_BR_LINE 5: malloc's error case
        pTimerInfo = (PTimerInfo)malloc(sizeof(TTimerInfo));

        if (NULL != pTimerInfo) {  // LCOV_EXCL_BR_LINE 5: malloc's error case
          hTimer->timerState = RUNNING_STATE;

          // set the timer info structure
          PCSTR pName = McGetQueueName(sndMqHndl);
          if (NULL != pName) {
            pTimerInfo->q_name = strdup(pName);
          } else {
            pTimerInfo->q_name = NULL;
            eStatus = eFrameworkunifiedStatusNullPointer;
            // LCOV_EXCL_BR_START 15:marco defined in "native_service/ns_logger_if.h"
            FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __FUNCTION__, "ERROR : McGetQueueName(sndMqHndl) Failed, status=%d", eStatus);
            // LCOV_EXCL_BR_STOP
          }

          if (eFrameworkunifiedStatusOK == eStatus) {
            pTimerInfo->iCmd = timer_info.iCmd;

            hTimer->tTimerInfo = pTimerInfo;
            if (INVALID_TIMERFD != (timerfd = timerfd_create(CLOCK_MONOTONIC, TFD_CLOEXEC|TFD_NONBLOCK))) {
              ev.events = EPOLLIN;
              ev.data.ptr = hTimer;
              if (-1 != epoll_ctl(epollFd, EPOLL_CTL_ADD, timerfd, &ev)) {
                hTimer->timerfd = timerfd;
                if (eFrameworkunifiedStatusOK == NS_TimerSetTime(hTimer, timer_info)) {
                  // set the interval in timer handle
                  hTimer->itime.it_value.tv_sec = (__time_t)timer_info.t_sec;
                  hTimer->itime.it_value.tv_nsec = (__syscall_slong_t)timer_info.t_nsec;
                  hTimer->itime.it_interval.tv_sec = (__time_t)timer_info.rpt_sec;
                  hTimer->itime.it_interval.tv_nsec = (__syscall_slong_t)timer_info.rpt_nsec;

                } else {
                  eStatus = eFrameworkunifiedStatusErrOther;

                  // if timer interval is not set or memory is not allocated for hTimer
                  close(timerfd);
                  timerfd = INVALID_TIMERFD;
                }
              } else {
                eStatus = eFrameworkunifiedStatusFail;
                // LCOV_EXCL_BR_START 15:marco defined in "native_service/ns_logger_if.h"
                FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __FUNCTION__, "ERROR : epoll_ctl(timerfd=%d, ADD) Failed, status=%d, errno=%d", timerfd, eStatus,
                       errno);
                // LCOV_EXCL_BR_STOP
              }
            } else {
              eStatus = eFrameworkunifiedStatusErrOther;
            }
          }
        } else {
          // LCOV_EXCL_START 5: malloc's error case
          AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
          eStatus = eFrameworkunifiedStatusNullPointer;
          FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __FUNCTION__, "ERROR : (PTimerInfo)malloc(sizeof(TTimerInfo)) Failed, status=%d", eStatus);
          // LCOV_EXCL_STOP
        }

        if (eFrameworkunifiedStatusOK != eStatus) {
          if (NULL != pTimerInfo) {
            if (NULL != pTimerInfo->q_name) {
              free(pTimerInfo->q_name);
              pTimerInfo->q_name = NULL;
            }

            free(pTimerInfo);
            pTimerInfo = NULL;
          }

          if (NULL != hTimer) {
            free(hTimer);
            hTimer = NULL;
          }
        }
      } else {
        // LCOV_EXCL_START 5: malloc's error case
        AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
        FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __FUNCTION__, "ERROR : malloc(sizeof(NSTimerHandle)) Failed");
        // LCOV_EXCL_STOP
      }
    } else {
      // LCOV_EXCL_BR_START 15:marco defined in "native_service/ns_logger_if.h"
      FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __FUNCTION__, "ERROR : CreateTimerMonitoringThread Failed, status=%d", eStatus);
      // LCOV_EXCL_BR_STOP
    }
  } else {
    // LCOV_EXCL_BR_START 15:marco defined in "native_service/ns_logger_if.h"
    FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __FUNCTION__, "ERROR : Invalid param");
    // LCOV_EXCL_BR_STOP
  }

  return hTimer;
}

EFrameworkunifiedStatus NS_TimerDelete(HANDLE hTimer) {
  EFrameworkunifiedStatus eStatus = eFrameworkunifiedStatusOK;
  if (NULL != hTimer) {
    PNSTimerHandle tTimerHndl = (PNSTimerHandle)hTimer;
    /**
     * @todo
     *  Dropping by typing the service handle instead of the timer handle
     */
    if (DELETED_STATE != tTimerHndl->timerState) {
      if (NULL != tTimerHndl->tTimerInfo) {
        tTimerHndl->timerState = DELETING_STATE;
        NSTimerInfo timer_info = {1, 0, tTimerHndl->tTimerInfo->iCmd, 0, 0};

        if (eFrameworkunifiedStatusOK != (eStatus = NS_TimerSetTime(tTimerHndl, timer_info))) {
          // LCOV_EXCL_BR_START 15:marco defined in "native_service/ns_logger_if.h"
          FRAMEWORKUNIFIEDLOG(ZONE_NS_WAR, __FUNCTION__, "ERROR : NS_TimerSetTime for Timer delete Failed, status=%d", eStatus);
          // LCOV_EXCL_BR_STOP
        }

        tTimerHndl->timerState = DELETED_STATE;
      } else {
        eStatus = eFrameworkunifiedStatusInvldParam;
        // LCOV_EXCL_BR_START 15:marco defined in "native_service/ns_logger_if.h"
        FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __FUNCTION__, "ERROR : tTimerInfo is NULL, status=%d", eStatus);
        // LCOV_EXCL_BR_STOP
      }
    } else {
      eStatus = eFrameworkunifiedStatusFail;
      // LCOV_EXCL_BR_START 15:marco defined in "native_service/ns_logger_if.h"
      FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __FUNCTION__, "ERROR : Timer is not RUNNING State, timerStatus=%d, status=%d",
             tTimerHndl->timerState, eStatus);
      // LCOV_EXCL_BR_STOP
    }
  } else {
    eStatus = eFrameworkunifiedStatusInvldParam;
    // LCOV_EXCL_BR_START 15:marco defined in "native_service/ns_logger_if.h"
    FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __FUNCTION__, "ERROR : hTimer is NULL, status=%d", eStatus);
    // LCOV_EXCL_BR_STOP
  }

  return eStatus;
}

EFrameworkunifiedStatus NS_TimerSetTime(HANDLE hTimer, NSTimerInfo timer_info) {
  EFrameworkunifiedStatus eStatus = eFrameworkunifiedStatusOK;

  if (NULL != hTimer) {
    PNSTimerHandle tTimerHndl = (PNSTimerHandle)hTimer;
    struct itimerspec itime;

    if (DELETED_STATE != tTimerHndl->timerState) {
      // set periodic interval values
      itime.it_value.tv_sec = (__time_t)timer_info.t_sec;
      itime.it_value.tv_nsec = (__syscall_slong_t)timer_info.t_nsec;

      // set periodic interval values
      itime.it_interval.tv_sec = (__time_t)timer_info.rpt_sec;
      itime.it_interval.tv_nsec = (__syscall_slong_t)timer_info.rpt_nsec;

      if (-1 != timerfd_settime(tTimerHndl->timerfd, 0, &itime, NULL)) {
        // updated the value of the timer values in the handler
        tTimerHndl->itime = itime;
      } else {
        eStatus = eFrameworkunifiedStatusFail;
        // LCOV_EXCL_BR_START 15:marco defined in "native_service/ns_logger_if.h"
        FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __FUNCTION__, "ERROR : timerfd_settime is failed, status=%d, errno=%d", eStatus, errno);
        // LCOV_EXCL_BR_STOP
      }
    } else {
      eStatus = eFrameworkunifiedStatusFail;
      // LCOV_EXCL_BR_START 15:marco defined in "native_service/ns_logger_if.h"
      FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __FUNCTION__, "ERROR : Timer is not RUNNING State, status=%d", eStatus);
      // LCOV_EXCL_BR_STOP
    }
  } else {
    eStatus = eFrameworkunifiedStatusInvldHandle;
    // LCOV_EXCL_BR_START 15:marco defined in "native_service/ns_logger_if.h"
    FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __FUNCTION__, "ERROR : hTimer is NULL, status=%d", eStatus);
    // LCOV_EXCL_BR_STOP
  }

  return eStatus;
}

EFrameworkunifiedStatus NS_TimerGetTime(HANDLE hTimer, NSTimerInfo *timer_info) {
  EFrameworkunifiedStatus eStatus = eFrameworkunifiedStatusOK;

  if ((NULL != hTimer) && (NULL != timer_info)) {
    PNSTimerHandle tTimerHndl = (PNSTimerHandle)hTimer;
    struct itimerspec itime;

    if (DELETED_STATE != tTimerHndl->timerState) {
      if (-1 != timerfd_gettime(tTimerHndl->timerfd, &itime)) {
        // set periodic interval values

//        timer_info->t_sec = (UI_32)itime.it_value.tv_sec;
        timer_info->t_sec = itime.it_value.tv_sec;
        timer_info->t_nsec = (UI_64)itime.it_value.tv_nsec;

        // set periodic interval values

//        timer_info->rpt_sec = (UI_32)itime.it_interval.tv_sec;
        timer_info->rpt_sec = itime.it_interval.tv_sec;
        timer_info->rpt_nsec = (UI_64)itime.it_interval.tv_nsec;
      } else {
        eStatus = eFrameworkunifiedStatusFail;
        // LCOV_EXCL_BR_START 15:marco defined in "native_service/ns_logger_if.h"
        FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __FUNCTION__, "ERROR : timerfd_gettime is failed, status=%d, errno=%d", eStatus, errno);
        // LCOV_EXCL_BR_STOP
      }
    } else {
      eStatus = eFrameworkunifiedStatusFail;
      // LCOV_EXCL_BR_START 15:marco defined in "native_service/ns_logger_if.h"
      FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __FUNCTION__, "ERROR : Timer is not RUNNING State, status=%d", eStatus);
      // LCOV_EXCL_BR_STOP
    }
  } else {
    eStatus = eFrameworkunifiedStatusInvldHandle;
    // LCOV_EXCL_BR_START 15:marco defined in "native_service/ns_logger_if.h"
    FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __FUNCTION__, "ERROR :  Invalid param, status=%d", eStatus);
    // LCOV_EXCL_BR_STOP
  }

  return eStatus;
}


//UI_32 WholeSeconds(UI_32 ms) {
//  return ms / MS_IN_SEC;
//}
time_t WholeSeconds(UI_32 ms) {
  return (time_t)(ms / MS_IN_SEC);
}


UI_32 RemainderMs(UI_32 ms) {
  return ms % MS_IN_SEC;
}

UI_64 MSToNS(UI_32 ms) {
  return (UI_64)ms * NS_IN_MS;
}

void NS_TimerDebugOn(BOOL FlagState) {  // LCOV_EXCL_START 7: debug code
  AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
  if (DebugFlag != FlagState) {
    DebugFlag = FlagState;
    if (TRUE == DebugFlag) {
      FRAMEWORKUNIFIEDLOG(ZONE_NS_IMP_INFO, __FUNCTION__, "NS_Timer debugging is enabled.");
    } else {
      FRAMEWORKUNIFIEDLOG(ZONE_NS_IMP_INFO, __FUNCTION__, "NS_Timer debugging is disabled.");
    }
  }
}
// LCOV_EXCL_STOP