• 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 <sys/time.h>
19 #include "cmsis_os2.h"
20 #include "softbus_adapter_log.h"
21 #include "softbus_def.h"
22 #include "softbus_errcode.h"
23 
24 #define MS_PER_SECOND 1000
25 
26 static TimerFunc g_timerfunc = NULL;
27 
HandleTimeoutAdapterFun(void)28 NO_SANITIZE("cfi") 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         HILOG_INFO(SOFTBUS_HILOG_ID, "create timer success");
47         return id;
48     }
49     HILOG_ERROR(SOFTBUS_HILOG_ID, "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         HILOG_ERROR(SOFTBUS_HILOG_ID, "timerId is NULL");
57         return SOFTBUS_ERR;
58     }
59     if (osTimerStart(timerId, ms * osKernelGetTickFreq() / MS_PER_SECOND) != osOK) {
60         HILOG_ERROR(SOFTBUS_HILOG_ID, "start timer failed");
61         (void)osTimerDelete(timerId);
62         return SOFTBUS_ERR;
63     }
64     HILOG_INFO(SOFTBUS_HILOG_ID, "start timer success");
65     return SOFTBUS_OK;
66 }
67 
SoftBusDeleteTimer(void * timerId)68 int SoftBusDeleteTimer(void *timerId)
69 {
70     if (timerId == NULL) {
71         HILOG_ERROR(SOFTBUS_HILOG_ID, "timerId is NULL");
72         return SOFTBUS_ERR;
73     }
74     if (osTimerDelete(timerId) != osOK) {
75         HILOG_ERROR(SOFTBUS_HILOG_ID, "delete timer failed");
76         return SOFTBUS_ERR;
77     }
78     HILOG_INFO(SOFTBUS_HILOG_ID, "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         HILOG_INFO(SOFTBUS_HILOG_ID, "sysTime is null");
92         return SOFTBUS_INVALID_PARAM;
93     }
94     struct timeval time = {0};
95     gettimeofday(&time, NULL);
96 
97     sysTime->sec = time.tv_sec;
98     sysTime->usec = time.tv_usec;
99     return SOFTBUS_OK;
100 }
101 
102