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