• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2020-2021 Huawei Device Co., Ltd.
3  *
4  * HDF is dual licensed: you can use it either under the terms of
5  * the GPL, or the BSD license, at your option.
6  * See the LICENSE file in the root of this repository for complete details.
7  */
8 
9 #include "osal_time.h"
10 #include <errno.h>
11 #include <sys/time.h>
12 #include <time.h>
13 #include <unistd.h>
14 #include "securec.h"
15 #include "hdf_log.h"
16 
17 #define HDF_LOG_TAG osal_time
18 
OsalGetTime(OsalTimespec * time)19 int32_t OsalGetTime(OsalTimespec *time)
20 {
21     struct timespec ts;
22 
23     if (time == NULL) {
24         HDF_LOGE("%s invalid para", __func__);
25         return HDF_ERR_INVALID_PARAM;
26     }
27 
28     (void)memset_s(&ts, sizeof(ts), 0, sizeof(ts));
29     clock_gettime(CLOCK_MONOTONIC, &ts);
30     time->sec = (uint64_t)ts.tv_sec;
31     time->usec = (uint64_t)ts.tv_nsec / HDF_KILO_UNIT;
32 
33     return HDF_SUCCESS;
34 }
35 
OsalDiffTime(const OsalTimespec * start,const OsalTimespec * end,OsalTimespec * diff)36 int32_t OsalDiffTime(const OsalTimespec *start, const OsalTimespec *end, OsalTimespec *diff)
37 {
38     uint32_t usec = 0;
39     uint32_t sec = 0;
40     if (start == NULL || end == NULL || diff == NULL) {
41         HDF_LOGE("%s invalid para", __func__);
42         return HDF_ERR_INVALID_PARAM;
43     }
44 
45     if (start->sec > end->sec) {
46         HDF_LOGE("%s start time later then end time", __func__);
47         return HDF_ERR_INVALID_PARAM;
48     }
49 
50     if (end->usec < start->usec) {
51         usec = (HDF_KILO_UNIT * HDF_KILO_UNIT);
52         sec = 1;
53     }
54     diff->usec = usec + end->usec - start->usec;
55     diff->sec = end->sec - start->sec - sec;
56 
57     return HDF_SUCCESS;
58 }
59 
OsalSleep(uint32_t sec)60 void OsalSleep(uint32_t sec)
61 {
62     sleep(sec);
63 }
64 
OsalMSleep(uint32_t ms)65 void OsalMSleep(uint32_t ms)
66 {
67     int result;
68     struct timespec ts;
69 
70     ts.tv_sec = (time_t)ms / HDF_KILO_UNIT;
71     ts.tv_nsec = (time_t)HDF_KILO_UNIT * HDF_KILO_UNIT * ((long)(ms % HDF_KILO_UNIT));
72     result = nanosleep(&ts, &ts);
73     if (result != 0) {
74         HDF_LOGE("%s OsalMSleep failed %d", __func__, errno);
75     }
76 }
77 
OsalUSleep(uint32_t us)78 void OsalUSleep(uint32_t us)
79 {
80     int result;
81     struct timespec ts;
82 
83     ts.tv_sec = (time_t)us / ((long)HDF_KILO_UNIT * HDF_KILO_UNIT);
84     ts.tv_nsec = (time_t)HDF_KILO_UNIT * ((long)(us % HDF_KILO_UNIT));
85     result = nanosleep(&ts, &ts);
86     if (result != 0) {
87         HDF_LOGE("%s OsalUSleep failed %d", __func__, errno);
88     }
89 }
90 
OsalUDelay(uint32_t us)91 void OsalUDelay(uint32_t us)
92 {
93     (void)us;
94 }
95 
OsalMDelay(uint32_t ms)96 void OsalMDelay(uint32_t ms)
97 {
98     (void)ms;
99 }
100 
OsalGetSysTimeMs()101 uint64_t OsalGetSysTimeMs()
102 {
103     OsalTimespec time;
104 
105     (void)memset_s(&time, sizeof(time), 0, sizeof(time));
106     (void)OsalGetTime(&time);
107 
108     return (time.sec * HDF_KILO_UNIT + time.usec / HDF_KILO_UNIT);
109 }
110