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