1 /*
2 * Copyright (c) 2021 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 "state_machine/state_machine.h"
17 #include "state_collection_factory.h"
18 #include "thermal_common.h"
19
20 namespace OHOS {
21 namespace PowerMgr {
Init()22 bool StateMachine::Init()
23 {
24 THERMAL_HILOGD(COMP_SVC, "Enter");
25
26 std::lock_guard<std::mutex> lock(stateMutex_);
27 if (initFlag_) {
28 THERMAL_HILOGI(COMP_SVC, "state machine already init");
29 return true;
30 }
31 initFlag_ = true;
32
33 if (receiver_ == nullptr) {
34 receiver_ = std::make_shared<ThermalCommonEventReceiver>();
35 }
36
37 for (auto state = vState_.begin(); state != vState_.end(); state++) {
38 THERMAL_HILOGI(COMP_SVC, "StateMachine name = %{public}s", state->name.c_str());
39 std::shared_ptr<IStateCollection> stateCollection = StateCollectionFactory::Create(state->name);
40 if (state->isExistParam) {
41 stateCollection->InitParam(state->params);
42 }
43 stateCollection->Init();
44 stateCollectionMap_.emplace(std::pair(state->name, stateCollection));
45 }
46 if (!receiver_->Register()) {
47 THERMAL_HILOGE(COMP_SVC, "ThermalCommonEventReceiver register failed!");
48 }
49 return true;
50 }
51
UpdateState(std::string stateName,std::string stateValue)52 void StateMachine::UpdateState(std::string stateName, std::string stateValue)
53 {
54 std::lock_guard<std::mutex> lock(stateMutex_);
55 auto iter = stateCollectionMap_.find(stateName);
56 if (iter != stateCollectionMap_.end()) {
57 iter->second->SetState(stateValue);
58 return;
59 }
60 vState_.push_back({stateName, "", false});
61 std::shared_ptr<IStateCollection> stateCollection = StateCollectionFactory::Create(stateName);
62 stateCollection->SetState(stateValue);
63 stateCollectionMap_.emplace(std::pair(stateName, stateCollection));
64 THERMAL_HILOGI(COMP_SVC, "StateMachine add state success");
65 }
66
DumpState(std::string & result)67 void StateMachine::DumpState(std::string& result)
68 {
69 std::lock_guard<std::mutex> lock(stateMutex_);
70 for (auto iter = vState_.begin(); iter != vState_.end(); ++iter) {
71 result.append("name: ");
72 result.append(iter->name);
73 if (!iter->params.empty()) {
74 result.append("\t");
75 result.append("params: ");
76 result.append(iter->params);
77 }
78 result.append("\n");
79 }
80 }
81
DumpIdle(std::string & result)82 void StateMachine::DumpIdle(std::string& result)
83 {
84 result.append("thermallevel: ");
85 result.append(std::to_string(idleStateConfig_.level));
86 result.append("\n");
87 result.append("soc: ");
88 result.append(std::to_string(idleStateConfig_.soc));
89 result.append("\n");
90 result.append("charging: ");
91 result.append(std::to_string(idleStateConfig_.charging));
92 result.append("\n");
93 result.append("current: ");
94 result.append(std::to_string(idleStateConfig_.current));
95 result.append("\n");
96 }
97 } // namespace PowerMgr
98 } // namespace OHOS
99