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 "ffrt_utils.h"
19 #include "power_log.h"
20 #include "string_ex.h"
21 #include "syspara/parameter.h"
22 #include "syspara/parameters.h"
23
24 namespace OHOS {
25 namespace PowerMgr {
26
LoopReadBootCompletedParameter(BootCompletedCallback & callback)27 void SysParam::LoopReadBootCompletedParameter(BootCompletedCallback& callback)
28 {
29 ffrt::submit([callback](void) -> void {
30 constexpr int32_t delayTimeMs = 50;
31 constexpr int32_t logInterval = 100;
32 int32_t count = 0;
33 while (!system::GetBoolParameter(KEY_BOOT_COMPLETED, false)) {
34 count++;
35 if (count >= logInterval) {
36 POWER_HILOGW(COMP_UTILS, "bootevent not fired!");
37 count = 0;
38 }
39 ffrt::this_task::sleep_for(std::chrono::milliseconds(delayTimeMs));
40 }
41 POWER_HILOGI(COMP_UTILS, "Get booteventCompleted true success!");
42 callback();
43 });
44 }
45
RegisterBootCompletedCallback(BootCompletedCallback & callback)46 void SysParam::RegisterBootCompletedCallback(BootCompletedCallback& callback)
47 {
48 POWER_HILOGI(COMP_UTILS, "start to RegisterBootCompletedCallback");
49 int32_t ret = WatchParameter(
50 KEY_BOOT_COMPLETED,
51 [](const char* key, const char* value, void* context) {
52 if (strcmp(value, "true") == 0) {
53 ((BootCompletedCallback)context)();
54 }
55 },
56 reinterpret_cast<void*>(callback));
57 if (ret != 0) {
58 POWER_HILOGW(COMP_UTILS, "RegisterBootCompletedCallback failed, ret=%{public}d", ret);
59 }
60 }
61
RegisterBootCompletedCallbackForPowerSa(BootCompletedCallback & callback)62 void SysParam::RegisterBootCompletedCallbackForPowerSa(BootCompletedCallback& callback)
63 {
64 POWER_HILOGI(COMP_UTILS, "start to RegisterBootCompletedCallback for power SA");
65 int32_t ret = WatchParameter(
66 KEY_BOOT_COMPLETED,
67 [](const char* key, const char* value, void* context) {
68 if (strcmp(value, "true") == 0) {
69 ((BootCompletedCallback)context)();
70 }
71 },
72 reinterpret_cast<void*>(callback));
73 if (ret != 0) {
74 POWER_HILOGW(COMP_UTILS, "RegisterBootCompletedCallback for power SA failed, ret=%{public}d", ret);
75 }
76 LoopReadBootCompletedParameter(callback);
77 }
78
GetIntValue(const std::string & key,int32_t def)79 int32_t SysParam::GetIntValue(const std::string& key, int32_t def)
80 {
81 char value[VALUE_MAX_LEN] = {0};
82 int32_t ret = GetParameter(key.c_str(), std::to_string(def).c_str(), value, VALUE_MAX_LEN);
83 if (ret < 0) {
84 POWER_HILOGW(COMP_UTILS, "GetParameter failed, return default value, ret=%{public}d, def=%{public}d", ret, def);
85 return def;
86 }
87 int32_t intValue = def;
88 if (!StrToInt(TrimStr(value), intValue)) {
89 POWER_HILOGW(COMP_UTILS, "StrToInt failed, return default def, value=%{public}s, def=%{public}d", value, def);
90 return def;
91 }
92 StrToInt(TrimStr(value), intValue);
93 return intValue;
94 }
95 } // namespace PowerMgr
96 } // namespace OHOS