• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include "softbus_adapter_timer.h"
17 
18 #include <errno.h>
19 #include <fcntl.h>
20 #include <signal.h>
21 #include <sys/stat.h>
22 #include <sys/types.h>
23 #include <sys/time.h>
24 #include <time.h>
25 #include <unistd.h>
26 
27 #include "securec.h"
28 #include "softbus_adapter_log.h"
29 #include "softbus_adapter_socket.h"
30 #include "softbus_def.h"
31 #include "softbus_errcode.h"
32 
33 #define MS_PER_SECOND 1000
34 #define US_PER_MSECOND 1000
35 #define NS_PER_USECOND 1000
36 
37 static unsigned int g_timerType;
38 
39 static TimerFunc g_timerfunc = NULL;
40 
HandleTimeoutAdapterFun(union sigval para)41 NO_SANITIZE("cfi") static void HandleTimeoutAdapterFun(union sigval para)
42 {
43     (void)para;
44     if (g_timerfunc != NULL) {
45         g_timerfunc();
46     }
47 }
48 
SetTimerFunc(TimerFunc func)49 void SetTimerFunc(TimerFunc func)
50 {
51     g_timerfunc = func;
52 }
53 
SoftBusCreateTimer(void ** timerId,unsigned int type)54 void *SoftBusCreateTimer(void **timerId, unsigned int type)
55 {
56     if (timerId == NULL) {
57         HILOG_ERROR(SOFTBUS_HILOG_ID, "timerId is null");
58         return NULL;
59     }
60     struct sigevent envent;
61     (void)memset_s(&envent, sizeof(envent), 0, sizeof(envent));
62     envent.sigev_notify = SIGEV_THREAD;
63     envent.sigev_notify_function = HandleTimeoutAdapterFun;
64     envent.sigev_notify_attributes = NULL;
65 
66     g_timerType = type;
67     if (timer_create(CLOCK_REALTIME, &envent, timerId) != 0) {
68         HILOG_ERROR(SOFTBUS_HILOG_ID, "timer create error, errno code: [%{public}d]", errno);
69         return NULL;
70     }
71 
72     return *timerId;
73 }
74 
SoftBusStartTimer(void * timerId,unsigned int tickets)75 int SoftBusStartTimer(void *timerId, unsigned int tickets)
76 {
77     if (timerId == NULL) {
78         HILOG_ERROR(SOFTBUS_HILOG_ID, "timerId is null");
79         return SOFTBUS_ERR;
80     }
81     struct itimerspec value;
82     (void)memset_s(&value, sizeof(value), 0, sizeof(value));
83     value.it_value.tv_sec = tickets / MS_PER_SECOND;
84     value.it_value.tv_nsec = 0;
85     if (g_timerType == TIMER_TYPE_ONCE) {
86         value.it_interval.tv_sec = 0;
87         value.it_interval.tv_nsec = 0;
88     } else {
89         value.it_interval.tv_sec = tickets / MS_PER_SECOND;
90         value.it_interval.tv_nsec = 0;
91     }
92 
93     if (timer_settime(timerId, 0, &value, NULL) != 0) {
94         HILOG_ERROR(SOFTBUS_HILOG_ID, "timer start error, errno code: [%{public}d]", errno);
95         return SOFTBUS_ERR;
96     }
97 
98     return SOFTBUS_OK;
99 }
100 
SoftBusDeleteTimer(void * timerId)101 int SoftBusDeleteTimer(void *timerId)
102 {
103     if (timerId == NULL) {
104         HILOG_ERROR(SOFTBUS_HILOG_ID, "timerId is null");
105         return SOFTBUS_ERR;
106     }
107 
108     if (timer_delete(timerId) != 0) {
109         HILOG_ERROR(SOFTBUS_HILOG_ID, "timer delete err, errno code: [%{public}d]", errno);
110         return SOFTBUS_ERR;
111     }
112 
113     return SOFTBUS_OK;
114 }
115 
SoftBusSleepMs(unsigned int ms)116 int SoftBusSleepMs(unsigned int ms)
117 {
118     int ret;
119     struct timeval tm;
120     tm.tv_sec = ms / MS_PER_SECOND;
121     tm.tv_usec = (ms % MS_PER_SECOND) * US_PER_MSECOND;
122 
123     do {
124         ret = select(0, NULL, NULL, NULL, &tm);
125     } while ((ret == -1) && (errno == EINTR));
126 
127     return SOFTBUS_ERR;
128 }
129 
SoftBusGetTime(SoftBusSysTime * sysTime)130 int32_t SoftBusGetTime(SoftBusSysTime *sysTime)
131 {
132     if (sysTime == NULL) {
133         HILOG_INFO(SOFTBUS_HILOG_ID, "sysTime is null");
134         return SOFTBUS_INVALID_PARAM;
135     }
136     struct timespec time = {0};
137     (void)clock_gettime(CLOCK_MONOTONIC, &time);
138 
139     sysTime->sec = time.tv_sec;
140     sysTime->usec = time.tv_nsec / NS_PER_USECOND;
141     return SOFTBUS_OK;
142 }
143 
SoftBusGetSysTimeMs(void)144 uint64_t SoftBusGetSysTimeMs(void)
145 {
146     struct timeval time;
147     time.tv_sec = 0;
148     time.tv_usec = 0;
149     if (gettimeofday(&time, NULL) != 0) {
150         HILOG_INFO(SOFTBUS_HILOG_ID, "get sys time fail");
151         return 0;
152     }
153     uint64_t ms = (uint64_t)time.tv_sec * MS_PER_SECOND + (uint64_t)time.tv_usec / US_PER_MSECOND;
154     return ms;
155 }