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