1 /*
2 * Copyright (c) 2024 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 "battery_log.h"
17 #include "battery_xcollie.h"
18 #ifdef HICOLLIE_ENABLE
19 #include "xcollie/xcollie.h"
20 #endif
21
22 namespace OHOS {
23 namespace HDI {
24 namespace Battery {
BatteryXCollie(const std::string & logTag,uint32_t timeoutSeconds)25 BatteryXCollie::BatteryXCollie(const std::string &logTag, uint32_t timeoutSeconds)
26 {
27 logTag_ = logTag;
28 isCanceled_ = false;
29 #ifdef HICOLLIE_ENABLE
30 id_ = HiviewDFX::XCollie::GetInstance().SetTimer(
31 logTag_, timeoutSeconds, nullptr, nullptr, HiviewDFX::XCOLLIE_FLAG_LOG | HiviewDFX::XCOLLIE_FLAG_RECOVERY);
32 #else
33 id_ = -1;
34 #endif
35 BATTERY_HILOGD(FEATURE_BATT_INFO, "Start BatteryXCollie, id:%{public}d, tag:%{public}s, timeout(s):%{public}u", id_,
36 logTag_.c_str(), timeoutSeconds);
37 }
38
~BatteryXCollie()39 BatteryXCollie::~BatteryXCollie()
40 {
41 CancelBatteryXCollie();
42 }
43
CancelBatteryXCollie()44 void BatteryXCollie::CancelBatteryXCollie()
45 {
46 if (!isCanceled_) {
47 #ifdef HICOLLIE_ENABLE
48 HiviewDFX::XCollie::GetInstance().CancelTimer(id_);
49 #endif
50 isCanceled_ = true;
51 BATTERY_HILOGD(FEATURE_BATT_INFO, "Cancel BatteryXCollie, id:%{public}d, tag:%{public}s", id_, logTag_.c_str());
52 }
53 }
54
55 } // namespace Battery
56 } // namespace HDI
57 } // namespace OHOS