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 "inactive.h" 17 18 #include "telephony_log_wrapper.h" 19 20 #include "apn_manager.h" 21 #include "cellular_data_event_code.h" 22 23 namespace OHOS { 24 namespace Telephony { StateBegin()25void Inactive::StateBegin() 26 { 27 TELEPHONY_LOGI("Enter inactive state"); 28 std::shared_ptr<CellularDataStateMachine> stateMachine = stateMachine_.lock(); 29 if (stateMachine == nullptr) { 30 TELEPHONY_LOGE("stateMachine is null"); 31 return; 32 } 33 stateMachine->connectId_++; 34 isActive_ = true; 35 if (deActiveApnTypeId_ != ERROR_APN_ID) { 36 // set net manager connection false 37 if (stateMachine->netSupplierInfo_ != nullptr) { 38 stateMachine->netSupplierInfo_->isAvailable_ = false; 39 } 40 // send MSG_DISCONNECT_DATA_COMPLETE to CellularDataHandler 41 auto netInfo = std::make_shared<SetupDataCallResultInfo>(); 42 if (netInfo == nullptr) { 43 TELEPHONY_LOGE("Create data disconnect params failed"); 44 return; 45 } 46 netInfo->cid = stateMachine->cid_; 47 netInfo->flag = deActiveApnTypeId_; 48 netInfo->active = 0; 49 if (resultInfo_ != nullptr) { 50 netInfo->reason = resultInfo_->reason; 51 netInfo->retryTime = resultInfo_->retryTime; 52 netInfo->retryScene = resultInfo_->retryScene; 53 } 54 AppExecFwk::InnerEvent::Pointer event = 55 AppExecFwk::InnerEvent::Get(CellularDataEventCode::MSG_DISCONNECT_DATA_COMPLETE, netInfo); 56 if (event == nullptr) { 57 TELEPHONY_LOGE("Create event failed"); 58 return; 59 } 60 if (stateMachine->cellularDataHandler_ != nullptr) { 61 stateMachine->cellularDataHandler_->SendEvent(event); 62 } 63 deActiveApnTypeId_ = ERROR_APN_ID; 64 resultInfo_ = nullptr; 65 } 66 stateMachine->SetCurrentState(sptr<State>(this)); 67 } 68 StateEnd()69void Inactive::StateEnd() 70 { 71 TELEPHONY_LOGI("Exit inactive state"); 72 isActive_ = false; 73 } 74 StateProcess(const AppExecFwk::InnerEvent::Pointer & event)75bool Inactive::StateProcess(const AppExecFwk::InnerEvent::Pointer &event) 76 { 77 if (event == nullptr) { 78 TELEPHONY_LOGE("event is null"); 79 return false; 80 } 81 std::shared_ptr<CellularDataStateMachine> stateMachine = stateMachine_.lock(); 82 if (stateMachine == nullptr) { 83 TELEPHONY_LOGE("stateMachine is null"); 84 return false; 85 } 86 bool retVal = false; 87 uint32_t eventCode = event->GetInnerEventId(); 88 switch (eventCode) { 89 case CellularDataEventCode::MSG_SM_CONNECT: { 90 TELEPHONY_LOGD("Inactive::MSG_SM_CONNECT"); 91 std::unique_ptr<DataConnectionParams> params = event->GetUniqueObject<DataConnectionParams>(); 92 if (params == nullptr) { 93 TELEPHONY_LOGE("Failed to get DataConnectionParams"); 94 return false; 95 } 96 stateMachine->DoConnect(*params); 97 stateMachine->TransitionTo(stateMachine->activatingState_); 98 retVal = PROCESSED; 99 break; 100 } 101 case CellularDataEventCode::MSG_SM_DISCONNECT: { 102 TELEPHONY_LOGI("Inactive::MSG_SM_DISCONNECT"); 103 retVal = PROCESSED; 104 break; 105 } 106 case CellularDataEventCode::MSG_SM_DISCONNECT_ALL: { 107 TELEPHONY_LOGI("Inactive::MSG_SM_DISCONNECT_ALL"); 108 retVal = PROCESSED; 109 break; 110 } 111 default: 112 break; 113 } 114 return retVal; 115 } 116 SetStateMachine(const std::weak_ptr<CellularDataStateMachine> & stateMachine)117void Inactive::SetStateMachine(const std::weak_ptr<CellularDataStateMachine> &stateMachine) 118 { 119 stateMachine_ = stateMachine; 120 } 121 SetDeActiveApnTypeId(int32_t deActiveApnTypeId)122void Inactive::SetDeActiveApnTypeId(int32_t deActiveApnTypeId) 123 { 124 deActiveApnTypeId_ = deActiveApnTypeId; 125 } 126 SetDataCallResultInfo(std::shared_ptr<SetupDataCallResultInfo> resultInfo)127void Inactive::SetDataCallResultInfo(std::shared_ptr<SetupDataCallResultInfo> resultInfo) 128 { 129 resultInfo_ = resultInfo; 130 } 131 SetPdpErrorReason(PdpErrorReason reason)132void Inactive::SetPdpErrorReason(PdpErrorReason reason) 133 { 134 resultInfo_ = std::make_shared<SetupDataCallResultInfo>(); 135 resultInfo_->reason = static_cast<int32_t>(reason); 136 } 137 } // namespace Telephony 138 } // namespace OHOS 139