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()25 void 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         CellularDataNetAgent &netAgent = CellularDataNetAgent::GetInstance();
38         int32_t supplierId = netAgent.GetSupplierId(stateMachine->GetSlotId(), stateMachine->GetCapability());
39         if (stateMachine->netSupplierInfo_ != nullptr) {
40             stateMachine->netSupplierInfo_->isAvailable_ = false;
41             netAgent.UpdateNetSupplierInfo(supplierId, stateMachine->netSupplierInfo_);
42         }
43         // send MSG_DISCONNECT_DATA_COMPLETE to CellularDataHandler
44         std::string apnType = ApnManager::FindApnNameByApnId(deActiveApnTypeId_);
45         std::unique_ptr<DataDisconnectParams> object = std::make_unique<DataDisconnectParams>(apnType, reason_);
46         if (object == nullptr) {
47             TELEPHONY_LOGE("Create data disconnect params failed");
48             return;
49         }
50         AppExecFwk::InnerEvent::Pointer event =
51             AppExecFwk::InnerEvent::Get(CellularDataEventCode::MSG_DISCONNECT_DATA_COMPLETE, object);
52         if (event == nullptr) {
53             TELEPHONY_LOGE("Create event failed");
54             return;
55         }
56         if (stateMachine->cellularDataHandler_ != nullptr) {
57             stateMachine->cellularDataHandler_->SendEvent(event);
58         }
59         deActiveApnTypeId_ = ERROR_APN_ID;
60         reason_ = DisConnectionReason::REASON_RETRY_CONNECTION;
61     }
62     stateMachine->SetCurrentState(sptr<State>(this));
63     if (stateMachine->cdConnectionManager_ != nullptr) {
64         stateMachine->cdConnectionManager_->RemoveActiveConnectionByCid(stateMachine->GetCid());
65     }
66 }
67 
StateEnd()68 void Inactive::StateEnd()
69 {
70     TELEPHONY_LOGI("Exit inactive state");
71     isActive_ = false;
72 }
73 
StateProcess(const AppExecFwk::InnerEvent::Pointer & event)74 bool Inactive::StateProcess(const AppExecFwk::InnerEvent::Pointer &event)
75 {
76     if (event == nullptr) {
77         TELEPHONY_LOGE("event is null");
78         return false;
79     }
80     std::shared_ptr<CellularDataStateMachine> stateMachine = stateMachine_.lock();
81     if (stateMachine == nullptr) {
82         TELEPHONY_LOGE("stateMachine is null");
83         return false;
84     }
85     bool retVal = false;
86     uint32_t eventCode = event->GetInnerEventId();
87     switch (eventCode) {
88         case CellularDataEventCode::MSG_SM_CONNECT: {
89             TELEPHONY_LOGD("Inactive::MSG_SM_CONNECT");
90             stateMachine->DoConnect(*(event->GetUniqueObject<DataConnectionParams>()));
91             stateMachine->TransitionTo(stateMachine->activatingState_);
92             retVal = PROCESSED;
93             break;
94         }
95         case CellularDataEventCode::MSG_SM_DISCONNECT: {
96             TELEPHONY_LOGI("Inactive::MSG_SM_DISCONNECT");
97             retVal = PROCESSED;
98             break;
99         }
100         case CellularDataEventCode::MSG_SM_DISCONNECT_ALL: {
101             TELEPHONY_LOGI("Inactive::MSG_SM_DISCONNECT_ALL");
102             retVal = PROCESSED;
103             break;
104         }
105         default:
106             break;
107     }
108     return retVal;
109 }
110 
SetStateMachine(const std::weak_ptr<CellularDataStateMachine> & stateMachine)111 void Inactive::SetStateMachine(const std::weak_ptr<CellularDataStateMachine> &stateMachine)
112 {
113     stateMachine_ = stateMachine;
114 }
115 
SetDeActiveApnTypeId(int32_t deActiveApnTypeId)116 void Inactive::SetDeActiveApnTypeId(int32_t deActiveApnTypeId)
117 {
118     deActiveApnTypeId_ = deActiveApnTypeId;
119 }
120 
SetReason(DisConnectionReason reason)121 void Inactive::SetReason(DisConnectionReason reason)
122 {
123     reason_ = reason;
124 }
125 } // namespace Telephony
126 } // namespace OHOS
127