• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 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 "nativeapi_timer_task.h"
17 
18 #include "CppTimer.h"
19 #include "CppTimerManager.h"
20 #include "PreviewerEngineLog.h"
21 
22 using namespace std;
23 
24 const static int RES_OK = 0;
25 const static int RES_ERROR = -1;
26 
27 #ifdef __cplusplus
28 #if __cplusplus
29 extern "C" {
30 #endif
31 #endif /* __cplusplus */
32 
InitTimerTask()33 int InitTimerTask()
34 {
35     return RES_OK;
36 }
37 
38 using TimerCallBack = void(*)(void*);
39 
StartTimerTask(bool isPeriodic,const unsigned int delay,void * userCallback,void * userContext,timerHandle_t * timerHandle)40 int StartTimerTask(bool isPeriodic, const unsigned int delay, void* userCallback,
41                    void* userContext, timerHandle_t *timerHandle)
42 {
43     if (timerHandle == nullptr) {
44         ELOG("timerHandle is nullptr");
45         return RES_ERROR;
46     }
47 
48     CppTimer* timer = new CppTimer(reinterpret_cast<TimerCallBack>(userCallback), userContext);
49     if (timer == nullptr) {
50         ELOG("TimerNew timer memory allocation failed");
51         *timerHandle = timer;
52         return RES_ERROR;
53     }
54 
55     if (!isPeriodic) {
56         timer->SetShotTimes(1);
57     }
58     timer->Start(delay);
59     CppTimerManager::GetTimerManager().AddCppTimer(*timer);
60     *timerHandle = timer;
61     return RES_OK;
62 }
63 
StopTimerTask(const timerHandle_t timerHandle)64 int StopTimerTask(const timerHandle_t timerHandle)
65 {
66     if (timerHandle == nullptr) {
67         ELOG("TimerTaskHandler::StartTimer timerHandle is null.");
68         return RES_ERROR;
69     }
70 
71     CppTimer* timer = static_cast<CppTimer*>(timerHandle);
72     if (timer == nullptr) {
73         ELOG("TimerTaskHandler::StartTimer timerHandle is invalud.");
74         return RES_ERROR;
75     }
76     timer->Stop();
77     CppTimerManager::GetTimerManager().RemoveCppTimer(*timer);
78     delete timer;
79     timer = nullptr;
80     return RES_OK;
81 }
82 
83 #ifdef __cplusplus
84 #if __cplusplus
85 }
86 #endif
87 #endif  /* __cplusplus */
88