15ccb8f90Sopenharmony_ci/*
25ccb8f90Sopenharmony_ci * Copyright (c) 2022-2023 Huawei Device Co., Ltd.
35ccb8f90Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
45ccb8f90Sopenharmony_ci * you may not use this file except in compliance with the License.
55ccb8f90Sopenharmony_ci * You may obtain a copy of the License at
65ccb8f90Sopenharmony_ci *
75ccb8f90Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
85ccb8f90Sopenharmony_ci *
95ccb8f90Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
105ccb8f90Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
115ccb8f90Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
125ccb8f90Sopenharmony_ci * See the License for the specific language governing permissions and
135ccb8f90Sopenharmony_ci * limitations under the License.
145ccb8f90Sopenharmony_ci */
155ccb8f90Sopenharmony_ci
165ccb8f90Sopenharmony_ci#include "sysparam.h"
175ccb8f90Sopenharmony_ci
185ccb8f90Sopenharmony_ci#include "power_log.h"
195ccb8f90Sopenharmony_ci#include "string_ex.h"
205ccb8f90Sopenharmony_ci#include "syspara/parameter.h"
215ccb8f90Sopenharmony_ci
225ccb8f90Sopenharmony_cinamespace OHOS {
235ccb8f90Sopenharmony_cinamespace PowerMgr {
245ccb8f90Sopenharmony_civoid SysParam::RegisterBootCompletedCallback(BootCompletedCallback& callback)
255ccb8f90Sopenharmony_ci{
265ccb8f90Sopenharmony_ci    int32_t ret = WatchParameter(
275ccb8f90Sopenharmony_ci        KEY_BOOT_COMPLETED,
285ccb8f90Sopenharmony_ci        [](const char* key, const char* value, void* context) {
295ccb8f90Sopenharmony_ci            if (strcmp(value, "true") == 0) {
305ccb8f90Sopenharmony_ci                ((BootCompletedCallback)context)();
315ccb8f90Sopenharmony_ci            }
325ccb8f90Sopenharmony_ci        },
335ccb8f90Sopenharmony_ci        reinterpret_cast<void*>(callback));
345ccb8f90Sopenharmony_ci    if (ret < 0) {
355ccb8f90Sopenharmony_ci        POWER_HILOGW(COMP_UTILS, "RegisterBootCompletedCallback failed, ret=%{public}d", ret);
365ccb8f90Sopenharmony_ci    }
375ccb8f90Sopenharmony_ci}
385ccb8f90Sopenharmony_ci
395ccb8f90Sopenharmony_ciint32_t SysParam::GetIntValue(const std::string& key, int32_t def)
405ccb8f90Sopenharmony_ci{
415ccb8f90Sopenharmony_ci    char value[VALUE_MAX_LEN] = {0};
425ccb8f90Sopenharmony_ci    int32_t ret = GetParameter(key.c_str(), std::to_string(def).c_str(), value, VALUE_MAX_LEN);
435ccb8f90Sopenharmony_ci    if (ret < 0) {
445ccb8f90Sopenharmony_ci        POWER_HILOGW(COMP_UTILS, "GetParameter failed, return default value, ret=%{public}d, def=%{public}d", ret, def);
455ccb8f90Sopenharmony_ci        return def;
465ccb8f90Sopenharmony_ci    }
475ccb8f90Sopenharmony_ci    int32_t intValue = def;
485ccb8f90Sopenharmony_ci    if (!StrToInt(TrimStr(value), intValue)) {
495ccb8f90Sopenharmony_ci        POWER_HILOGW(COMP_UTILS, "StrToInt failed, return default def, value=%{public}s, def=%{public}d", value, def);
505ccb8f90Sopenharmony_ci        return def;
515ccb8f90Sopenharmony_ci    }
525ccb8f90Sopenharmony_ci    StrToInt(TrimStr(value), intValue);
535ccb8f90Sopenharmony_ci    return intValue;
545ccb8f90Sopenharmony_ci}
555ccb8f90Sopenharmony_ci} // namespace PowerMgr
565ccb8f90Sopenharmony_ci} // namespace OHOS