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