1/*
2 * Copyright (c) 2022-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 "sysparam.h"
17
18#include "power_log.h"
19#include "string_ex.h"
20#include "syspara/parameter.h"
21
22namespace OHOS {
23namespace PowerMgr {
24void SysParam::RegisterBootCompletedCallback(BootCompletedCallback& callback)
25{
26    int32_t ret = WatchParameter(
27        KEY_BOOT_COMPLETED,
28        [](const char* key, const char* value, void* context) {
29            if (strcmp(value, "true") == 0) {
30                ((BootCompletedCallback)context)();
31            }
32        },
33        reinterpret_cast<void*>(callback));
34    if (ret < 0) {
35        POWER_HILOGW(COMP_UTILS, "RegisterBootCompletedCallback failed, ret=%{public}d", ret);
36    }
37}
38
39int32_t SysParam::GetIntValue(const std::string& key, int32_t def)
40{
41    char value[VALUE_MAX_LEN] = {0};
42    int32_t ret = GetParameter(key.c_str(), std::to_string(def).c_str(), value, VALUE_MAX_LEN);
43    if (ret < 0) {
44        POWER_HILOGW(COMP_UTILS, "GetParameter failed, return default value, ret=%{public}d, def=%{public}d", ret, def);
45        return def;
46    }
47    int32_t intValue = def;
48    if (!StrToInt(TrimStr(value), intValue)) {
49        POWER_HILOGW(COMP_UTILS, "StrToInt failed, return default def, value=%{public}s, def=%{public}d", value, def);
50        return def;
51    }
52    StrToInt(TrimStr(value), intValue);
53    return intValue;
54}
55} // namespace PowerMgr
56} // namespace OHOS