1 /*
2  * Copyright (c) 2024 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 #ifndef STATE_MANAGER_H
16 #define STATE_MANAGER_H
17 
18 #include <iostream>
19 #include <map>
20 #include <vector>
21 #include <mutex>
22 #include <memory>
23 #include <string>
24 #include <functional>
25 #include "timer_mgr.h"
26 
27 namespace OHOS {
28 namespace IntellVoiceUtils {
29 constexpr int NO_PROCESS_RET = -2;
30 
31 struct StateMsg {
32 public:
33     int32_t msgId;
34     void *inMsg = nullptr;
35     int32_t inMsgLen;
36     void *outMsg = nullptr;
37 
38 public:
39     explicit StateMsg(int32_t id, void *in = nullptr, int32_t inLen = 0, void *out = nullptr)
40         : msgId(id), inMsg(in), inMsgLen(inLen), outMsg(out) {};
41 };
42 
43 struct State {
44     State() = default;
StateState45     explicit State(int s) : state(s) {};
46 
47     bool operator==(const State &right) const
48     {
49         return state == right.state;
50     };
51 
52     bool operator!=(const State &right) const
53     {
54         return !(*this == right);
55     };
56 
57     bool operator<(const State &right) const
58     {
59         return state < right.state;
60     };
61 
ToStrState62     std::string ToStr() const
63     {
64         return std::to_string(state);
65     };
66 
67     int32_t state = -1;
68 };
69 
70 struct StateInfo {
~StateInfoStateInfo71     virtual ~StateInfo(){};
72     TimerCfg cfg;
73     int timerId = INVALID_ID;
74 };
75 
76 using HandleMsg = std::function<int(const StateMsg &msg, State &nextState)>;
77 struct ModuleStates;
78 
79 struct StateActions : public StateInfo {
~StateActionsStateActions80     ~StateActions() override {};
81     StateActions() = default;
82     bool operator==(StateActions& right) const
83     {
84         return actions.size() == right.actions.size();
85     }
86 
AddStateActions87     StateActions& Add(int msgid, HandleMsg handler)
88     {
89         actions[msgid] = handler;
90         return *this;
91     };
92 
DelStateActions93     StateActions& Del(int msgid)
94     {
95         actions.erase(msgid);
96         return *this;
97     };
98 
99     StateActions& WaitUntil(int type, HandleMsg handler, int64_t delayUs, int cookie = 0)
100     {
101         cfg.type = type;
102         cfg.delayUs = delayUs;
103         cfg.cookie = cookie;
104 
105         Add(type, handler);
106 
107         return *this;
108     };
109 
110     int Handle(const StateMsg &msg, State &nextState);
111 
112 private:
113     std::map<int32_t, HandleMsg> actions;
114 };
115 
116 struct StateGroup {
117 public:
~StateGroupStateGroup118     virtual ~StateGroup()
119     {
120         ClearAction();
121     }
122 
AddActionStateGroup123     void AddAction(StateActions *action)
124     {
125         mActions.push_back(action);
126     }
127 
ClearActionStateGroup128     void ClearAction()
129     {
130         mActions.clear();
131     }
132 
AddStateGroup133     StateGroup& Add(int msgid, HandleMsg handler)
134     {
135         for (auto each : mActions) {
136             each->Add(msgid, handler);
137         }
138         return *this;
139     }
140 
DelStateGroup141     StateGroup& Del(int msgid)
142     {
143         for (auto each : mActions) {
144             each->Del(msgid);
145         }
146         return *this;
147     }
148 private:
149     std::vector<StateActions*> mActions;
150 };
151 
152 struct ModuleStates : public ITimerObserver, private TimerMgr, private StateGroup {
153     explicit ModuleStates(const State &defaultState = State(0), const std::string &name = "",
154         const std::string &threadName = "StateThread");
155     ~ModuleStates() override;
156 
157     StateActions& ForState(const State &s);
158     StateActions& ForState(int simpleState);
159     StateGroup& FromState(int simpleStateStart, int simpleStateEnd);
160 
161     int HandleMsg(const StateMsg &msg);
162     void ResetTimerDelay();
163     bool IsStatesInitSucc() const;
164     State CurrState() const;
165 
166 protected:
167     void ToState(std::map<State, StateActions*>::iterator &nextIt);
168     void OnTimerEvent(TimerEvent &info) override;
169 
170 protected:
171     std::map<State, StateActions*>::iterator currState_;
172 
173 private:
174     std::mutex msgHandleMutex_;
175     bool isInitSucc_ = false;
176     std::map<State, StateActions*> states_;
177     std::string name_;
178 };
179 
180 #define ADDR(func) ([this](const StateMsg &msg, State &nextState)->int { return this->func(msg, nextState); })
181 #define ACT(msgid, func) Add(msgid, ADDR(func))
182 }
183 }
184 
185 #endif /* STATE_MANAGER_H */
186