17c804472Sopenharmony_ci/* 27c804472Sopenharmony_ci * Copyright (c) 2023 Huawei Device Co., Ltd. 37c804472Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 47c804472Sopenharmony_ci * you may not use this file except in compliance with the License. 57c804472Sopenharmony_ci * You may obtain a copy of the License at 67c804472Sopenharmony_ci * 77c804472Sopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 87c804472Sopenharmony_ci * 97c804472Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software 107c804472Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 117c804472Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 127c804472Sopenharmony_ci * See the License for the specific language governing permissions and 137c804472Sopenharmony_ci * limitations under the License. 147c804472Sopenharmony_ci */ 157c804472Sopenharmony_ci 167c804472Sopenharmony_ci#include "nativeapi_timer_task.h" 177c804472Sopenharmony_ci 187c804472Sopenharmony_ci#include "CppTimer.h" 197c804472Sopenharmony_ci#include "CppTimerManager.h" 207c804472Sopenharmony_ci#include "PreviewerEngineLog.h" 217c804472Sopenharmony_ci 227c804472Sopenharmony_ciconst static int RES_OK = 0; 237c804472Sopenharmony_ciconst static int RES_ERROR = -1; 247c804472Sopenharmony_ci 257c804472Sopenharmony_ci#ifdef __cplusplus 267c804472Sopenharmony_ci#if __cplusplus 277c804472Sopenharmony_ciextern "C" { 287c804472Sopenharmony_ci#endif 297c804472Sopenharmony_ci#endif /* __cplusplus */ 307c804472Sopenharmony_ci 317c804472Sopenharmony_ciint InitTimerTask() 327c804472Sopenharmony_ci{ 337c804472Sopenharmony_ci return RES_OK; 347c804472Sopenharmony_ci} 357c804472Sopenharmony_ci 367c804472Sopenharmony_ciusing TimerCallBack = void(*)(void*); 377c804472Sopenharmony_ci 387c804472Sopenharmony_ciint StartTimerTask(bool isPeriodic, const unsigned int delay, void* userCallback, 397c804472Sopenharmony_ci void* userContext, timerHandle_t *timerHandle) 407c804472Sopenharmony_ci{ 417c804472Sopenharmony_ci if (timerHandle == nullptr) { 427c804472Sopenharmony_ci ELOG("timerHandle is nullptr"); 437c804472Sopenharmony_ci return RES_ERROR; 447c804472Sopenharmony_ci } 457c804472Sopenharmony_ci 467c804472Sopenharmony_ci CppTimer* timer = new(std::nothrow) CppTimer(reinterpret_cast<TimerCallBack>(userCallback), userContext); 477c804472Sopenharmony_ci if (timer == nullptr) { 487c804472Sopenharmony_ci ELOG("Memory allocation failed: timer."); 497c804472Sopenharmony_ci *timerHandle = timer; 507c804472Sopenharmony_ci return RES_ERROR; 517c804472Sopenharmony_ci } 527c804472Sopenharmony_ci 537c804472Sopenharmony_ci if (!isPeriodic) { 547c804472Sopenharmony_ci timer->SetShotTimes(1); 557c804472Sopenharmony_ci } 567c804472Sopenharmony_ci timer->Start(delay); 577c804472Sopenharmony_ci CppTimerManager::GetTimerManager().AddCppTimer(*timer); 587c804472Sopenharmony_ci *timerHandle = timer; 597c804472Sopenharmony_ci return RES_OK; 607c804472Sopenharmony_ci} 617c804472Sopenharmony_ci 627c804472Sopenharmony_ciint StopTimerTask(const timerHandle_t timerHandle) 637c804472Sopenharmony_ci{ 647c804472Sopenharmony_ci if (timerHandle == nullptr) { 657c804472Sopenharmony_ci ELOG("TimerTaskHandler::StartTimer timerHandle is null."); 667c804472Sopenharmony_ci return RES_ERROR; 677c804472Sopenharmony_ci } 687c804472Sopenharmony_ci 697c804472Sopenharmony_ci CppTimer* timer = static_cast<CppTimer*>(timerHandle); 707c804472Sopenharmony_ci if (timer == nullptr) { 717c804472Sopenharmony_ci ELOG("TimerTaskHandler::StartTimer timerHandle is invalud."); 727c804472Sopenharmony_ci return RES_ERROR; 737c804472Sopenharmony_ci } 747c804472Sopenharmony_ci timer->Stop(); 757c804472Sopenharmony_ci CppTimerManager::GetTimerManager().RemoveCppTimer(*timer); 767c804472Sopenharmony_ci delete timer; 777c804472Sopenharmony_ci timer = nullptr; 787c804472Sopenharmony_ci return RES_OK; 797c804472Sopenharmony_ci} 807c804472Sopenharmony_ci 817c804472Sopenharmony_ci#ifdef __cplusplus 827c804472Sopenharmony_ci#if __cplusplus 837c804472Sopenharmony_ci} 847c804472Sopenharmony_ci#endif 857c804472Sopenharmony_ci#endif /* __cplusplus */ 86