1 /*
2  * Copyright (c) 2020 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 "nativeapi_timer_task.h"
17 #include <stdio.h>
18 #include "kal.h"
19 #include "ohos_errno.h"
20 
InitTimerTask()21 int InitTimerTask()
22 {
23     return EC_SUCCESS;
24 }
25 
StartTimerTask(bool isPeriodic,const unsigned int delay,void * userCallback,void * userContext,timerHandle_t * timerHandle)26 int StartTimerTask(bool isPeriodic, const unsigned int delay, void* userCallback,
27     void* userContext, timerHandle_t* timerHandle)
28 {
29     if (userCallback == NULL || timerHandle == NULL) {
30         return EC_FAILURE;
31     }
32 
33     KalTimerType timerType = isPeriodic ? KAL_TIMER_PERIODIC : KAL_TIMER_ONCE;
34     KalTimerId timerId = KalTimerCreate((KalTimerProc)userCallback, timerType, userContext, delay);
35     if (timerId == NULL) {
36         return EC_FAILURE;
37     }
38 
39     if (KalTimerStart(timerId) != KAL_OK) {
40         StopTimerTask(timerId);
41         return EC_FAILURE;
42     }
43     *timerHandle = timerId;
44 
45     return EC_SUCCESS;
46 }
47 
StopTimerTask(const timerHandle_t timerHandle)48 int StopTimerTask(const timerHandle_t timerHandle)
49 {
50     if (timerHandle == NULL) {
51         return EC_FAILURE;
52     }
53     return KalTimerDelete((KalTimerId)timerHandle);
54 }