• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2022 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 <stdio.h>
17 #include <stdlib.h>
18 #include <stdint.h>
19 #include "attest_utils.h"
20 #include "attest_utils_log.h"
21 #include "attest_utils_timer.h"
22 
23 static timer_t g_timerId = 0;
24 
TimerFunction(union sigval sigv)25 static void TimerFunction(union sigval sigv)
26 {
27     TimerInfo *timerInfo = (TimerInfo *)(sigv.sival_ptr);
28     timerInfo->func();
29 }
30 
Ms2TimeSpec(struct timespec * tp,uint32_t ms)31 static void Ms2TimeSpec(struct timespec *tp, uint32_t ms)
32 {
33     tp->tv_sec = ms / LOSCFG_BASE_CORE_MS_PRE_SECOND;
34     ms -= tp->tv_sec * LOSCFG_BASE_CORE_MS_PRE_SECOND;
35     tp->tv_nsec = (long)(((unsigned long long)ms * OS_SYS_NS_PER_SECOND) / LOSCFG_BASE_CORE_MS_PRE_SECOND);
36 }
37 
TimerCreate(TimerCallbackFunc userCallBack,TimerInfo * timerInfo)38 static int32_t TimerCreate(TimerCallbackFunc userCallBack, TimerInfo* timerInfo)
39 {
40     struct sigevent evp = {0};
41     timer_t timerId;
42     timerInfo->func = userCallBack;
43     evp.sigev_value.sival_ptr = timerInfo;
44     evp.sigev_notify = SIGEV_THREAD;
45     evp.sigev_notify_function = TimerFunction;
46     int32_t ret = timer_create(CLOCK_REALTIME, &evp, &timerId);
47     if (ret != ATTEST_OK) {
48         ATTEST_LOG_ERROR("[TimerCreate] TimerCreate failed");
49         return ATTEST_ERR;
50     }
51     timerInfo->timerId = timerId;
52     return ret;
53 }
54 
TimerStart(TimerInfo * timerInfo,AttestTimerType type,uint32_t milliseconds)55 static int32_t TimerStart(TimerInfo* timerInfo, AttestTimerType type, uint32_t milliseconds)
56 {
57     struct itimerspec ts;
58     (void)memset_s(&ts, sizeof(ts), 0, sizeof(ts));
59     Ms2TimeSpec(&ts.it_value, milliseconds);
60     if (type == ATTEST_TIMER_TYPE_PERIOD) {
61         Ms2TimeSpec(&ts.it_interval, milliseconds);
62     }
63     return timer_settime(timerInfo->timerId, 0, &ts, NULL);
64 }
65 
CreateTimerTask(uint32_t milliseconds,void * userCallBack,AttestTimerType type)66 int32_t CreateTimerTask(uint32_t milliseconds, void* userCallBack, AttestTimerType type)
67 {
68     if (g_timerId != 0) {
69         ATTEST_LOG_ERROR("[CreateTimerTask] TimerTask exists");
70         return ATTEST_ERR;
71     }
72     TimerInfo* timerInfo = (TimerInfo *)ATTEST_MEM_MALLOC(sizeof(TimerInfo));
73     if (timerInfo == NULL) {
74         return ATTEST_ERR;
75     }
76     int32_t ret = TimerCreate((TimerCallbackFunc)userCallBack, timerInfo);
77     if (ret != ATTEST_OK) {
78         ATTEST_LOG_ERROR("[CreateTimerTask] TimerCreate failed");
79         ATTEST_MEM_FREE(timerInfo);
80         return ATTEST_ERR;
81     }
82 
83     ret = TimerStart(timerInfo, type, milliseconds);
84     if (ret != ATTEST_OK) {
85         ATTEST_LOG_ERROR("[CreateTimerTask] TimerStart failed");
86         timer_delete(timerInfo->timerId);
87         ATTEST_MEM_FREE(timerInfo);
88         return ATTEST_ERR;
89     } else {
90         g_timerId = timerInfo->timerId;
91         ATTEST_LOG_INFO("[CreateTimerTask] TimerStart success");
92     }
93     return ATTEST_OK;
94 }