1 /*
2  * Copyright (c) 2021-2023 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 <sys/time.h>
19 #include <time.h>
20 
21 #include "cmsis_os2.h"
22 #include "comm_log.h"
23 #include "softbus_errcode.h"
24 
25 #define MS_PER_SECOND  1000
26 #define US_PER_MSECOND 1000
27 #define NS_PER_USECOND 1000
28 
29 static TimerFunc g_timerFunc = NULL;
30 
HandleTimeoutAdapterFun(void)31 static void HandleTimeoutAdapterFun(void)
32 {
33     if (g_timerFunc != NULL) {
34         g_timerFunc();
35     }
36 }
37 
SetTimerFunc(TimerFunc func)38 void SetTimerFunc(TimerFunc func)
39 {
40     g_timerFunc = func;
41 }
42 
SoftBusCreateTimer(void ** timerId,unsigned int type)43 void *SoftBusCreateTimer(void **timerId, unsigned int type)
44 {
45     (void)timerId;
46 
47     void *id = osTimerNew((osTimerFunc_t)HandleTimeoutAdapterFun, (osTimerType_t)type, NULL, NULL);
48     if (id != NULL) {
49         COMM_LOGI(COMM_ADAPTER, "create timer success");
50         return id;
51     }
52     COMM_LOGE(COMM_ADAPTER, "create timer failed");
53     return NULL;
54 }
55 
SoftBusStartTimer(void * timerId,unsigned int ms)56 int SoftBusStartTimer(void *timerId, unsigned int ms)
57 {
58     if (timerId == NULL) {
59         COMM_LOGE(COMM_ADAPTER, "timerId is NULL");
60         return SOFTBUS_ERR;
61     }
62     if (osTimerStart(timerId, ms * osKernelGetTickFreq() / MS_PER_SECOND) != osOK) {
63         COMM_LOGE(COMM_ADAPTER, "start timer failed");
64         (void)osTimerDelete(timerId);
65         return SOFTBUS_ERR;
66     }
67     COMM_LOGI(COMM_ADAPTER, "start timer success");
68     return SOFTBUS_OK;
69 }
70 
SoftBusDeleteTimer(void * timerId)71 int SoftBusDeleteTimer(void *timerId)
72 {
73     if (timerId == NULL) {
74         COMM_LOGE(COMM_ADAPTER, "timerId is NULL");
75         return SOFTBUS_ERR;
76     }
77     if (osTimerDelete(timerId) != osOK) {
78         COMM_LOGE(COMM_ADAPTER, "delete timer failed");
79         return SOFTBUS_ERR;
80     }
81     COMM_LOGI(COMM_ADAPTER, "delete timer success");
82     return SOFTBUS_OK;
83 }
84 
SoftBusSleepMs(unsigned int ms)85 int SoftBusSleepMs(unsigned int ms)
86 {
87     osDelay(ms * osKernelGetTickFreq() / MS_PER_SECOND);
88     return SOFTBUS_OK;
89 }
90 
SoftBusGetTime(SoftBusSysTime * sysTime)91 int32_t SoftBusGetTime(SoftBusSysTime *sysTime)
92 {
93     if (sysTime == NULL) {
94         COMM_LOGW(COMM_ADAPTER, "sysTime is null");
95         return SOFTBUS_INVALID_PARAM;
96     }
97     struct timeval time = {0};
98     gettimeofday(&time, NULL);
99     sysTime->sec = time.tv_sec;
100     sysTime->usec = time.tv_usec;
101     return SOFTBUS_OK;
102 }
103 
SoftBusGetRealTime(SoftBusSysTime * sysTime)104 int32_t SoftBusGetRealTime(SoftBusSysTime *sysTime)
105 {
106     if (sysTime == NULL) {
107         COMM_LOGW(COMM_ADAPTER, "sysTime is null");
108         return SOFTBUS_INVALID_PARAM;
109     }
110     struct timespec time = {0};
111     (void)clock_gettime(CLOCK_BOOTTIME, &time);
112     sysTime->sec = time.tv_sec;
113     sysTime->usec = time.tv_nsec / NS_PER_USECOND;
114     return SOFTBUS_OK;
115 }
116 
SoftBusGetSysTimeMs(void)117 uint64_t SoftBusGetSysTimeMs(void)
118 {
119     struct timeval time;
120     time.tv_sec = 0;
121     time.tv_usec = 0;
122     if (gettimeofday(&time, NULL) != 0) {
123         COMM_LOGI(COMM_ADAPTER, "get sys time fail");
124         return 0;
125     }
126     uint64_t ms = (uint64_t)time.tv_sec * MS_PER_SECOND + (uint64_t)time.tv_usec / US_PER_MSECOND;
127     return ms;
128 }
129 
SoftBusFormatTimestamp(uint64_t timestamp)130 const char *SoftBusFormatTimestamp(uint64_t timestamp)
131 {
132     return "0000-00-00 00:00:00.000";
133 }
134