15490a39dSopenharmony_ci/* 25490a39dSopenharmony_ci * Copyright (c) 2024 Huawei Device Co., Ltd. 35490a39dSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 45490a39dSopenharmony_ci * you may not use this file except in compliance with the License. 55490a39dSopenharmony_ci * You may obtain a copy of the License at 65490a39dSopenharmony_ci * 75490a39dSopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 85490a39dSopenharmony_ci * 95490a39dSopenharmony_ci * Unless required by applicable law or agreed to in writing, software 105490a39dSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 115490a39dSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 125490a39dSopenharmony_ci * See the License for the specific language governing permissions and 135490a39dSopenharmony_ci * limitations under the License. 145490a39dSopenharmony_ci */ 155490a39dSopenharmony_ci#ifndef STATE_MANAGER_H 165490a39dSopenharmony_ci#define STATE_MANAGER_H 175490a39dSopenharmony_ci 185490a39dSopenharmony_ci#include <iostream> 195490a39dSopenharmony_ci#include <map> 205490a39dSopenharmony_ci#include <vector> 215490a39dSopenharmony_ci#include <mutex> 225490a39dSopenharmony_ci#include <memory> 235490a39dSopenharmony_ci#include <string> 245490a39dSopenharmony_ci#include <functional> 255490a39dSopenharmony_ci#include "timer_mgr.h" 265490a39dSopenharmony_ci 275490a39dSopenharmony_cinamespace OHOS { 285490a39dSopenharmony_cinamespace IntellVoiceUtils { 295490a39dSopenharmony_ciconstexpr int NO_PROCESS_RET = -2; 305490a39dSopenharmony_ci 315490a39dSopenharmony_cistruct StateMsg { 325490a39dSopenharmony_cipublic: 335490a39dSopenharmony_ci int32_t msgId; 345490a39dSopenharmony_ci void *inMsg = nullptr; 355490a39dSopenharmony_ci int32_t inMsgLen; 365490a39dSopenharmony_ci void *outMsg = nullptr; 375490a39dSopenharmony_ci 385490a39dSopenharmony_cipublic: 395490a39dSopenharmony_ci explicit StateMsg(int32_t id, void *in = nullptr, int32_t inLen = 0, void *out = nullptr) 405490a39dSopenharmony_ci : msgId(id), inMsg(in), inMsgLen(inLen), outMsg(out) {}; 415490a39dSopenharmony_ci}; 425490a39dSopenharmony_ci 435490a39dSopenharmony_cistruct State { 445490a39dSopenharmony_ci State() = default; 455490a39dSopenharmony_ci explicit State(int s) : state(s) {}; 465490a39dSopenharmony_ci 475490a39dSopenharmony_ci bool operator==(const State &right) const 485490a39dSopenharmony_ci { 495490a39dSopenharmony_ci return state == right.state; 505490a39dSopenharmony_ci }; 515490a39dSopenharmony_ci 525490a39dSopenharmony_ci bool operator!=(const State &right) const 535490a39dSopenharmony_ci { 545490a39dSopenharmony_ci return !(*this == right); 555490a39dSopenharmony_ci }; 565490a39dSopenharmony_ci 575490a39dSopenharmony_ci bool operator<(const State &right) const 585490a39dSopenharmony_ci { 595490a39dSopenharmony_ci return state < right.state; 605490a39dSopenharmony_ci }; 615490a39dSopenharmony_ci 625490a39dSopenharmony_ci std::string ToStr() const 635490a39dSopenharmony_ci { 645490a39dSopenharmony_ci return std::to_string(state); 655490a39dSopenharmony_ci }; 665490a39dSopenharmony_ci 675490a39dSopenharmony_ci int32_t state = -1; 685490a39dSopenharmony_ci}; 695490a39dSopenharmony_ci 705490a39dSopenharmony_cistruct StateInfo { 715490a39dSopenharmony_ci virtual ~StateInfo(){}; 725490a39dSopenharmony_ci TimerCfg cfg; 735490a39dSopenharmony_ci int timerId = INVALID_ID; 745490a39dSopenharmony_ci}; 755490a39dSopenharmony_ci 765490a39dSopenharmony_ciusing HandleMsg = std::function<int(const StateMsg &msg, State &nextState)>; 775490a39dSopenharmony_cistruct ModuleStates; 785490a39dSopenharmony_ci 795490a39dSopenharmony_cistruct StateActions : public StateInfo { 805490a39dSopenharmony_ci ~StateActions() override {}; 815490a39dSopenharmony_ci StateActions() = default; 825490a39dSopenharmony_ci bool operator==(StateActions& right) const 835490a39dSopenharmony_ci { 845490a39dSopenharmony_ci return actions.size() == right.actions.size(); 855490a39dSopenharmony_ci } 865490a39dSopenharmony_ci 875490a39dSopenharmony_ci StateActions& Add(int msgid, HandleMsg handler) 885490a39dSopenharmony_ci { 895490a39dSopenharmony_ci actions[msgid] = handler; 905490a39dSopenharmony_ci return *this; 915490a39dSopenharmony_ci }; 925490a39dSopenharmony_ci 935490a39dSopenharmony_ci StateActions& Del(int msgid) 945490a39dSopenharmony_ci { 955490a39dSopenharmony_ci actions.erase(msgid); 965490a39dSopenharmony_ci return *this; 975490a39dSopenharmony_ci }; 985490a39dSopenharmony_ci 995490a39dSopenharmony_ci StateActions& WaitUntil(int type, HandleMsg handler, int64_t delayUs, int cookie = 0) 1005490a39dSopenharmony_ci { 1015490a39dSopenharmony_ci cfg.type = type; 1025490a39dSopenharmony_ci cfg.delayUs = delayUs; 1035490a39dSopenharmony_ci cfg.cookie = cookie; 1045490a39dSopenharmony_ci 1055490a39dSopenharmony_ci Add(type, handler); 1065490a39dSopenharmony_ci 1075490a39dSopenharmony_ci return *this; 1085490a39dSopenharmony_ci }; 1095490a39dSopenharmony_ci 1105490a39dSopenharmony_ci int Handle(const StateMsg &msg, State &nextState); 1115490a39dSopenharmony_ci 1125490a39dSopenharmony_ciprivate: 1135490a39dSopenharmony_ci std::map<int32_t, HandleMsg> actions; 1145490a39dSopenharmony_ci}; 1155490a39dSopenharmony_ci 1165490a39dSopenharmony_cistruct StateGroup { 1175490a39dSopenharmony_cipublic: 1185490a39dSopenharmony_ci virtual ~StateGroup() 1195490a39dSopenharmony_ci { 1205490a39dSopenharmony_ci ClearAction(); 1215490a39dSopenharmony_ci } 1225490a39dSopenharmony_ci 1235490a39dSopenharmony_ci void AddAction(StateActions *action) 1245490a39dSopenharmony_ci { 1255490a39dSopenharmony_ci mActions.push_back(action); 1265490a39dSopenharmony_ci } 1275490a39dSopenharmony_ci 1285490a39dSopenharmony_ci void ClearAction() 1295490a39dSopenharmony_ci { 1305490a39dSopenharmony_ci mActions.clear(); 1315490a39dSopenharmony_ci } 1325490a39dSopenharmony_ci 1335490a39dSopenharmony_ci StateGroup& Add(int msgid, HandleMsg handler) 1345490a39dSopenharmony_ci { 1355490a39dSopenharmony_ci for (auto each : mActions) { 1365490a39dSopenharmony_ci each->Add(msgid, handler); 1375490a39dSopenharmony_ci } 1385490a39dSopenharmony_ci return *this; 1395490a39dSopenharmony_ci } 1405490a39dSopenharmony_ci 1415490a39dSopenharmony_ci StateGroup& Del(int msgid) 1425490a39dSopenharmony_ci { 1435490a39dSopenharmony_ci for (auto each : mActions) { 1445490a39dSopenharmony_ci each->Del(msgid); 1455490a39dSopenharmony_ci } 1465490a39dSopenharmony_ci return *this; 1475490a39dSopenharmony_ci } 1485490a39dSopenharmony_ciprivate: 1495490a39dSopenharmony_ci std::vector<StateActions*> mActions; 1505490a39dSopenharmony_ci}; 1515490a39dSopenharmony_ci 1525490a39dSopenharmony_cistruct ModuleStates : public ITimerObserver, private TimerMgr, private StateGroup { 1535490a39dSopenharmony_ci explicit ModuleStates(const State &defaultState = State(0), const std::string &name = "", 1545490a39dSopenharmony_ci const std::string &threadName = "StateThread"); 1555490a39dSopenharmony_ci ~ModuleStates() override; 1565490a39dSopenharmony_ci 1575490a39dSopenharmony_ci StateActions& ForState(const State &s); 1585490a39dSopenharmony_ci StateActions& ForState(int simpleState); 1595490a39dSopenharmony_ci StateGroup& FromState(int simpleStateStart, int simpleStateEnd); 1605490a39dSopenharmony_ci 1615490a39dSopenharmony_ci int HandleMsg(const StateMsg &msg); 1625490a39dSopenharmony_ci void ResetTimerDelay(); 1635490a39dSopenharmony_ci bool IsStatesInitSucc() const; 1645490a39dSopenharmony_ci State CurrState() const; 1655490a39dSopenharmony_ci 1665490a39dSopenharmony_ciprotected: 1675490a39dSopenharmony_ci void ToState(std::map<State, StateActions*>::iterator &nextIt); 1685490a39dSopenharmony_ci void OnTimerEvent(TimerEvent &info) override; 1695490a39dSopenharmony_ci 1705490a39dSopenharmony_ciprotected: 1715490a39dSopenharmony_ci std::map<State, StateActions*>::iterator currState_; 1725490a39dSopenharmony_ci 1735490a39dSopenharmony_ciprivate: 1745490a39dSopenharmony_ci std::mutex msgHandleMutex_; 1755490a39dSopenharmony_ci bool isInitSucc_ = false; 1765490a39dSopenharmony_ci std::map<State, StateActions*> states_; 1775490a39dSopenharmony_ci std::string name_; 1785490a39dSopenharmony_ci}; 1795490a39dSopenharmony_ci 1805490a39dSopenharmony_ci#define ADDR(func) ([this](const StateMsg &msg, State &nextState)->int { return this->func(msg, nextState); }) 1815490a39dSopenharmony_ci#define ACT(msgid, func) Add(msgid, ADDR(func)) 1825490a39dSopenharmony_ci} 1835490a39dSopenharmony_ci} 1845490a39dSopenharmony_ci 1855490a39dSopenharmony_ci#endif /* STATE_MANAGER_H */ 186