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 #ifndef STATE_MACHINE_H
17 #define STATE_MACHINE_H
18 
19 #include <string>
20 #include <vector>
21 #include <memory>
22 #include <map>
23 
24 #include "charger_state_collection.h"
25 #include "constants.h"
26 #include "istate_collection.h"
27 #include "thermal_common_event_receiver.h"
28 
29 namespace OHOS {
30 namespace PowerMgr {
31 struct StateItem {
32     std::string name;
33     std::string params;
34     bool isExistParam = false;
35 };
36 
37 class StateMachine {
38 public:
39     using StateMachineMap = std::map<std::string, std::shared_ptr<IStateCollection>>;
40     bool Init();
41     void UpdateState(std::string stateName, std::string stateValue);
42     void DumpState(std::string& result);
43     void DumpIdle(std::string& result);
SetStateItem(std::vector<StateItem> & vstateItem)44     void SetStateItem(std::vector<StateItem>& vstateItem)
45     {
46         vState_ = vstateItem;
47     }
48 
GetStateCollectionMap()49     StateMachineMap GetStateCollectionMap()
50     {
51         return stateCollectionMap_;
52     }
53 
SetIdleStateConfig(const IdleState & idleState)54     void SetIdleStateConfig(const IdleState& idleState)
55     {
56         idleStateConfig_ = idleState;
57     }
58 
GetIdleStateConfig()59     IdleState& GetIdleStateConfig()
60     {
61         return idleStateConfig_;
62     }
63 
GetCommonEventReceiver()64     std::shared_ptr<ThermalCommonEventReceiver> GetCommonEventReceiver()
65     {
66         return receiver_;
67     }
68 private:
69     std::mutex stateMutex_;
70     bool initFlag_ {false};
71     StateMachineMap stateCollectionMap_;
72     std::vector<StateItem> vState_;
73     std::shared_ptr<ThermalCommonEventReceiver> receiver_;
74     IdleState idleStateConfig_;
75 };
76 } // namespace PowerMgr
77 } // namespace OHOS
78 #endif // STATE_MACHINE_H
79