• 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/time.h>
23 #include <sys/types.h>
24 #include <unistd.h>
25 
26 #include "securec.h"
27 #include "softbus_adapter_log.h"
28 #include "softbus_adapter_socket.h"
29 #include "softbus_def.h"
30 #include "softbus_errcode.h"
31 
32 #define MS_PER_SECOND 1000
33 #define US_PER_MSECOND 1000
34 #define NS_PER_USECOND 1000
35 
36 static unsigned int g_timerType;
37 
38 static TimerFunc g_timerfunc = NULL;
39 
HandleTimeoutAdapterFun(union sigval para)40 static void HandleTimeoutAdapterFun(union sigval para)
41 {
42     (void)para;
43     if (g_timerfunc != NULL) {
44         g_timerfunc();
45     }
46 }
47 
SetTimerFunc(TimerFunc func)48 void SetTimerFunc(TimerFunc func)
49 {
50     g_timerfunc = func;
51 }
52 
SoftBusCreateTimer(void ** timerId,unsigned int type)53 void *SoftBusCreateTimer(void **timerId, unsigned int type)
54 {
55     if (timerId == NULL) {
56         HILOG_ERROR(SOFTBUS_HILOG_ID, "timerId is null");
57         return NULL;
58     }
59     struct sigevent envent;
60     (void)memset_s(&envent, sizeof(envent), 0, sizeof(envent));
61     envent.sigev_notify = SIGEV_THREAD;
62     envent.sigev_notify_function = HandleTimeoutAdapterFun;
63     envent.sigev_notify_attributes = NULL;
64 
65     g_timerType = type;
66     if (timer_create(CLOCK_REALTIME, &envent, timerId) != 0) {
67         HILOG_ERROR(SOFTBUS_HILOG_ID, "timer create error, errno code: [%{public}d]", errno);
68         return NULL;
69     }
70 
71     return *timerId;
72 }
73 
SoftBusStartTimer(void * timerId,unsigned int tickets)74 int SoftBusStartTimer(void *timerId, unsigned int tickets)
75 {
76     if (timerId == NULL) {
77         HILOG_ERROR(SOFTBUS_HILOG_ID, "timerId is null");
78         return SOFTBUS_ERR;
79     }
80     struct itimerspec value;
81     (void)memset_s(&value, sizeof(value), 0, sizeof(value));
82     value.it_value.tv_sec = tickets / MS_PER_SECOND;
83     value.it_value.tv_nsec = 0;
84     if (g_timerType == TIMER_TYPE_ONCE) {
85         value.it_interval.tv_sec = 0;
86         value.it_interval.tv_nsec = 0;
87     } else {
88         value.it_interval.tv_sec = tickets / MS_PER_SECOND;
89         value.it_interval.tv_nsec = 0;
90     }
91 
92     if (timer_settime(timerId, 0, &value, NULL) != 0) {
93         HILOG_ERROR(SOFTBUS_HILOG_ID, "timer start error, errno code: [%{public}d]", errno);
94         return SOFTBUS_ERR;
95     }
96 
97     return SOFTBUS_OK;
98 }
99 
SoftBusDeleteTimer(void * timerId)100 int SoftBusDeleteTimer(void *timerId)
101 {
102     if (timerId == NULL) {
103         HILOG_ERROR(SOFTBUS_HILOG_ID, "timerId is null");
104         return SOFTBUS_ERR;
105     }
106 
107     if (timer_delete(timerId) != 0) {
108         HILOG_ERROR(SOFTBUS_HILOG_ID, "timer delete err, errno code: [%{public}d]", errno);
109         return SOFTBUS_ERR;
110     }
111 
112     return SOFTBUS_OK;
113 }
114 
SoftBusSleepMs(unsigned int ms)115 int SoftBusSleepMs(unsigned int ms)
116 {
117     int ret;
118     struct timeval tm;
119     tm.tv_sec = ms / MS_PER_SECOND;
120     tm.tv_usec = (ms % MS_PER_SECOND) * US_PER_MSECOND;
121 
122     do {
123         ret = select(0, NULL, NULL, NULL, &tm);
124     } while ((ret == -1) && (errno == EINTR));
125 
126     return SOFTBUS_ERR;
127 }
128 
SoftBusGetTime(SoftBusSysTime * sysTime)129 int32_t SoftBusGetTime(SoftBusSysTime *sysTime)
130 {
131     if (sysTime == NULL) {
132         HILOG_INFO(SOFTBUS_HILOG_ID, "sysTime is null");
133         return SOFTBUS_INVALID_PARAM;
134     }
135     struct timespec time = {0};
136     (void)clock_gettime(CLOCK_MONOTONIC, &time);
137 
138     sysTime->sec = time.tv_sec;
139     sysTime->usec = time.tv_nsec / NS_PER_USECOND;
140     return SOFTBUS_OK;
141 }
142