1 /* 2 * Copyright (C) 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 #ifndef INCALL_DATA_STATE_MACHINE_H 17 #define INCALL_DATA_STATE_MACHINE_H 18 19 #include <map> 20 #include <memory> 21 22 #include "apn_manager.h" 23 #include "cellular_data_constant.h" 24 #include "inner_event.h" 25 #include "refbase.h" 26 #include "state_machine.h" 27 #include "tel_event_handler.h" 28 29 namespace OHOS { 30 namespace Telephony { 31 class IncallDataStateMachine : public StateMachine, public std::enable_shared_from_this<IncallDataStateMachine> { 32 public: IncallDataStateMachine(int32_t slotId,std::weak_ptr<TelEventHandler> && cellularDataHandler,sptr<ApnManager> & apnManager)33 IncallDataStateMachine( 34 int32_t slotId, std::weak_ptr<TelEventHandler> &&cellularDataHandler, sptr<ApnManager> &apnManager) 35 : StateMachine("IncallDataStateMachine"), cellularDataHandler_(std::move(cellularDataHandler)), 36 apnManager_(apnManager), slotId_(slotId) 37 {} 38 ~IncallDataStateMachine() = default; 39 sptr<State> GetCurrentState() const; 40 int32_t GetSlotId() const; 41 int32_t GetCallState() const; 42 bool HasAnyConnectedState() const; 43 void UpdateCallState(int32_t state); 44 void Init(int32_t callState); 45 bool IsSecondaryActiveState() const; 46 47 protected: 48 sptr<State> idleState_; 49 sptr<State> secondaryActiveState_; 50 sptr<State> activatingSecondaryState_; 51 sptr<State> activatedSecondaryState_; 52 sptr<State> deactivatingSecondaryState_; 53 sptr<State> currentState_; 54 std::weak_ptr<TelEventHandler> cellularDataHandler_; 55 sptr<ApnManager> apnManager_; 56 57 private: 58 void SetCurrentState(const sptr<State> &&state); 59 bool IsIncallDataSwitchOn(); 60 bool IsSecondaryCanActiveData(); 61 bool CanActiveDataByRadioTech(); 62 63 private: 64 friend class IdleState; 65 friend class SecondaryActiveState; 66 friend class ActivatingSecondaryState; 67 friend class ActivatedSecondaryState; 68 friend class DeactivatingSecondaryState; 69 int32_t slotId_ = INVALID_SLOT_ID; 70 int32_t callState_ = static_cast<int32_t>(TelCallStatus::CALL_STATUS_IDLE); 71 }; 72 73 class IdleState : public State { 74 public: IdleState(std::weak_ptr<IncallDataStateMachine> && incallData,std::string && name)75 IdleState(std::weak_ptr<IncallDataStateMachine> &&incallData, std::string &&name) 76 : State(std::move(name)), stateMachine_(std::move(incallData)) 77 {} 78 virtual ~IdleState() = default; 79 virtual void StateBegin(); 80 virtual void StateEnd(); 81 virtual bool StateProcess(const AppExecFwk::InnerEvent::Pointer &event); 82 83 private: 84 bool ProcessCallStarted(const AppExecFwk::InnerEvent::Pointer &event); 85 bool ProcessCallEnded(const AppExecFwk::InnerEvent::Pointer &event); 86 bool ProcessSettingsOn(const AppExecFwk::InnerEvent::Pointer &event); 87 bool ProcessDsdsChanged(const AppExecFwk::InnerEvent::Pointer &event); 88 89 private: 90 using Fun = std::function<bool(const AppExecFwk::InnerEvent::Pointer &data)>; 91 std::map<uint32_t, Fun> eventIdFunMap_ { 92 { CellularDataEventCode::MSG_SM_INCALL_DATA_CALL_STARTED, 93 [this](const AppExecFwk::InnerEvent::Pointer &data) { return ProcessCallStarted(data); } }, 94 { CellularDataEventCode::MSG_SM_INCALL_DATA_CALL_ENDED, 95 [this](const AppExecFwk::InnerEvent::Pointer &data) { return ProcessCallEnded(data); } }, 96 { CellularDataEventCode::MSG_SM_INCALL_DATA_SETTINGS_ON, 97 [this](const AppExecFwk::InnerEvent::Pointer &data) { return ProcessSettingsOn(data); } }, 98 { CellularDataEventCode::MSG_SM_INCALL_DATA_DSDS_CHANGED, 99 [this](const AppExecFwk::InnerEvent::Pointer &data) { return ProcessDsdsChanged(data); } }, 100 }; 101 std::weak_ptr<IncallDataStateMachine> stateMachine_; 102 }; 103 104 class SecondaryActiveState : public State { 105 public: SecondaryActiveState(std::weak_ptr<IncallDataStateMachine> && incallData,std::string && name)106 SecondaryActiveState(std::weak_ptr<IncallDataStateMachine> &&incallData, std::string &&name) 107 : State(std::move(name)), stateMachine_(std::move(incallData)) 108 {} 109 virtual ~SecondaryActiveState() = default; 110 virtual void StateBegin(); 111 virtual void StateEnd(); 112 virtual bool StateProcess(const AppExecFwk::InnerEvent::Pointer &event); 113 114 private: 115 bool ProcessSettingsOn(const AppExecFwk::InnerEvent::Pointer &event); 116 bool ProcessCallEnded(const AppExecFwk::InnerEvent::Pointer &event); 117 bool ProcessSettingsOff(const AppExecFwk::InnerEvent::Pointer &event); 118 bool ProcessDsdsChanged(const AppExecFwk::InnerEvent::Pointer &event); 119 120 private: 121 using Fun = std::function<bool(const AppExecFwk::InnerEvent::Pointer &data)>; 122 std::map<uint32_t, Fun> eventIdFunMap_ { 123 { CellularDataEventCode::MSG_SM_INCALL_DATA_SETTINGS_ON, 124 [this](const AppExecFwk::InnerEvent::Pointer &data) { return ProcessSettingsOn(data); } }, 125 { CellularDataEventCode::MSG_SM_INCALL_DATA_CALL_ENDED, 126 [this](const AppExecFwk::InnerEvent::Pointer &data) { return ProcessCallEnded(data); } }, 127 { CellularDataEventCode::MSG_SM_INCALL_DATA_SETTINGS_OFF, 128 [this](const AppExecFwk::InnerEvent::Pointer &data) { return ProcessSettingsOff(data); } }, 129 { CellularDataEventCode::MSG_SM_INCALL_DATA_DSDS_CHANGED, 130 [this](const AppExecFwk::InnerEvent::Pointer &data) { return ProcessDsdsChanged(data); } }, 131 }; 132 std::weak_ptr<IncallDataStateMachine> stateMachine_; 133 }; 134 135 class ActivatingSecondaryState : public State { 136 public: ActivatingSecondaryState(std::weak_ptr<IncallDataStateMachine> && incallData,std::string && name)137 ActivatingSecondaryState(std::weak_ptr<IncallDataStateMachine> &&incallData, std::string &&name) 138 : State(std::move(name)), stateMachine_(std::move(incallData)) 139 {} 140 virtual ~ActivatingSecondaryState() = default; 141 virtual void StateBegin(); 142 virtual void StateEnd(); 143 virtual bool StateProcess(const AppExecFwk::InnerEvent::Pointer &event); 144 145 private: 146 std::weak_ptr<IncallDataStateMachine> stateMachine_; 147 }; 148 149 class ActivatedSecondaryState : public State { 150 public: ActivatedSecondaryState(std::weak_ptr<IncallDataStateMachine> && incallData,std::string && name)151 ActivatedSecondaryState(std::weak_ptr<IncallDataStateMachine> &&incallData, std::string &&name) 152 : State(std::move(name)), stateMachine_(std::move(incallData)) 153 {} 154 virtual ~ActivatedSecondaryState() = default; 155 virtual void StateBegin(); 156 virtual void StateEnd(); 157 virtual bool StateProcess(const AppExecFwk::InnerEvent::Pointer &event); 158 159 private: 160 std::weak_ptr<IncallDataStateMachine> stateMachine_; 161 }; 162 163 class DeactivatingSecondaryState : public State { 164 public: DeactivatingSecondaryState(std::weak_ptr<IncallDataStateMachine> && incallData,std::string && name)165 DeactivatingSecondaryState(std::weak_ptr<IncallDataStateMachine> &&incallData, std::string &&name) 166 : State(std::move(name)), stateMachine_(std::move(incallData)) 167 {} 168 virtual ~DeactivatingSecondaryState() = default; 169 virtual void StateBegin(); 170 virtual void StateEnd(); 171 virtual bool StateProcess(const AppExecFwk::InnerEvent::Pointer &event); 172 173 private: 174 std::weak_ptr<IncallDataStateMachine> stateMachine_; 175 }; 176 } // namespace Telephony 177 } // namespace OHOS 178 #endif // INCALL_DATA_STATE_MACHINE_H