1 /*
2  * Copyright (c) 2023-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 "dp_timer.h"
17 
18 #include "common_timer_errors.h"
19 #include "dp_log.h"
20 
21 namespace OHOS {
22 namespace CameraStandard {
23 namespace DeferredProcessing {
DpsTimer()24 DpsTimer::DpsTimer() : timer_(std::make_unique<Utils::Timer>("DpsManagerTimer"))
25 {
26     timer_->Setup();
27 }
28 
~DpsTimer()29 DpsTimer::~DpsTimer()
30 {
31     if (timer_) {
32         timer_->Shutdown(true);
33         timer_ = nullptr;
34     }
35 }
36 
StartTimer(const TimerCallback & callback,uint32_t interval)37 uint32_t DpsTimer::StartTimer(const TimerCallback& callback, uint32_t interval)
38 {
39     DP_CHECK_ERROR_RETURN_RET_LOG(timer_ == nullptr, INVALID_TIMEID, "DpsTimer is nullptr");
40 
41     uint32_t ret = timer_->Register(callback, interval, true);
42     DP_CHECK_ERROR_RETURN_RET_LOG(ret == Utils::TIMER_ERR_DEAL_FAILED, INVALID_TIMEID, "Register timer failed");
43 
44     DP_DEBUG_LOG("timerId is %{public}u, interval is %{public}u", ret, interval);
45     return ret;
46 }
47 
StopTimer(uint32_t & timerId)48 void DpsTimer::StopTimer(uint32_t& timerId)
49 {
50     DP_DEBUG_LOG("timer shutting down, timerId = %{public}u", timerId);
51     DP_CHECK_ERROR_RETURN_LOG(timerId == INVALID_TIMEID, "UnRegister timer failed, timerId is invalid");
52     DP_CHECK_ERROR_RETURN_LOG(timer_ == nullptr, "DpsTimer is nullptr");
53 
54     timer_->Unregister(timerId);
55     timerId = INVALID_TIMEID;
56 }
57 } // namespace DeferredProcessing
58 } // namespace CameraStandard
59 } // namespace OHOS