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