• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "active.h"
17 
18 #include "cellular_data_hisysevent.h"
19 #include "cellular_data_utils.h"
20 #include "core_manager_inner.h"
21 #include "inactive.h"
22 #include "telephony_log_wrapper.h"
23 #include "telephony_ext_wrapper.h"
24 #include "apn_manager.h"
25 
26 namespace OHOS {
27 namespace Telephony {
StateBegin()28 void Active::StateBegin()
29 {
30     TELEPHONY_LOGI("Enter active state");
31     std::shared_ptr<CellularDataStateMachine> stateMachine = stateMachine_.lock();
32     if (stateMachine == nullptr) {
33         TELEPHONY_LOGE("stateMachine is null");
34         return;
35     }
36     isActive_ = true;
37     RefreshTcpBufferSizes();
38     RefreshConnectionBandwidths();
39     stateMachine->SetCurrentState(sptr<State>(this));
40 }
41 
StateEnd()42 void Active::StateEnd()
43 {
44     TELEPHONY_LOGI("Exit active state");
45 }
46 
StateProcess(const AppExecFwk::InnerEvent::Pointer & event)47 bool Active::StateProcess(const AppExecFwk::InnerEvent::Pointer &event)
48 {
49     if (event == nullptr) {
50         TELEPHONY_LOGE("event is null");
51         return false;
52     }
53     bool retVal = false;
54     uint32_t eventCode = event->GetInnerEventId();
55     std::map<uint32_t, Fun>::iterator it = eventIdFunMap_.find(eventCode);
56     if (it != eventIdFunMap_.end()) {
57         if (it->second != nullptr) {
58             return it->second(event);
59         }
60     }
61     return retVal;
62 }
63 
ProcessConnectDone(const AppExecFwk::InnerEvent::Pointer & event)64 bool Active::ProcessConnectDone(const AppExecFwk::InnerEvent::Pointer &event)
65 {
66     TELEPHONY_LOGI("Active::MSG_SM_CONNECT");
67     return PROCESSED;
68 }
69 
ProcessDisconnectDone(const AppExecFwk::InnerEvent::Pointer & event)70 bool Active::ProcessDisconnectDone(const AppExecFwk::InnerEvent::Pointer &event)
71 {
72     TELEPHONY_LOGI("Active::MSG_SM_DISCONNECT");
73     std::shared_ptr<CellularDataStateMachine> stateMachine = stateMachine_.lock();
74     if (stateMachine == nullptr) {
75         TELEPHONY_LOGE("stateMachine is null");
76         return false;
77     }
78 
79     if (event == nullptr) {
80         TELEPHONY_LOGE("event is null");
81         return false;
82     }
83     std::unique_ptr<DataDisconnectParams> object = event->GetUniqueObject<DataDisconnectParams>();
84     if (object == nullptr) {
85         TELEPHONY_LOGE("object is null");
86         return false;
87     }
88     if (stateMachine->GetReuseApnCap() != NetManagerStandard::NetCap::NET_CAPABILITY_END) {
89         stateMachine->SetIfReuseSupplierId(false);
90         stateMachine->SetReuseApnCap(NetManagerStandard::NetCap::NET_CAPABILITY_END);
91     }
92     DisConnectionReason reason = object->GetReason();
93     Inactive *inActive = static_cast<Inactive *>(stateMachine->inActiveState_.GetRefPtr());
94     if (inActive == nullptr) {
95         TELEPHONY_LOGE("inActive is null");
96         return false;
97     }
98     inActive->SetPdpErrorReason(disconnReasonPdpErrorMap_[reason]);
99     stateMachine->FreeConnection(*object);
100     stateMachine->TransitionTo(stateMachine->disconnectingState_);
101     return PROCESSED;
102 }
103 
ProcessDisconnectAllDone(const AppExecFwk::InnerEvent::Pointer & event)104 bool Active::ProcessDisconnectAllDone(const AppExecFwk::InnerEvent::Pointer &event)
105 {
106     TELEPHONY_LOGI("Active::MSG_SM_DISCONNECT_ALL");
107     std::shared_ptr<CellularDataStateMachine> stateMachine = stateMachine_.lock();
108     if (stateMachine == nullptr) {
109         TELEPHONY_LOGE("stateMachine is null");
110         return false;
111     }
112     if (event == nullptr) {
113         TELEPHONY_LOGE("event is null");
114         return false;
115     }
116     std::unique_ptr<DataDisconnectParams> object = event->GetUniqueObject<DataDisconnectParams>();
117     if (object == nullptr) {
118         TELEPHONY_LOGE("object is null");
119         return false;
120     }
121     if (stateMachine->GetReuseApnCap() != NetManagerStandard::NetCap::NET_CAPABILITY_END) {
122         stateMachine->SetIfReuseSupplierId(false);
123         stateMachine->SetReuseApnCap(NetManagerStandard::NetCap::NET_CAPABILITY_END);
124     }
125     DisConnectionReason reason = object->GetReason();
126     Inactive *inActive = static_cast<Inactive *>(stateMachine->inActiveState_.GetRefPtr());
127     if (inActive == nullptr) {
128         TELEPHONY_LOGE("inActive is null");
129         return false;
130     }
131     inActive->SetPdpErrorReason(disconnReasonPdpErrorMap_[reason]);
132     stateMachine->FreeConnection(*object);
133     stateMachine->TransitionTo(stateMachine->disconnectingState_);
134     return PROCESSED;
135 }
136 
ProcessLostConnection(const AppExecFwk::InnerEvent::Pointer & event)137 bool Active::ProcessLostConnection(const AppExecFwk::InnerEvent::Pointer &event)
138 {
139     TELEPHONY_LOGI("Active::EVENT_LOST_CONNECTION");
140     if (event == nullptr) {
141         TELEPHONY_LOGE("event is null");
142         return false;
143     }
144     auto resultInfo = event->GetSharedObject<SetupDataCallResultInfo>();
145     if (resultInfo == nullptr) {
146         TELEPHONY_LOGE("resultInfo null");
147         return false;
148     }
149     std::shared_ptr<CellularDataStateMachine> stateMachine = stateMachine_.lock();
150     if (stateMachine == nullptr) {
151         TELEPHONY_LOGE("stateMachine is null");
152         return false;
153     }
154     if (stateMachine->GetReuseApnCap() != NetManagerStandard::NetCap::NET_CAPABILITY_END) {
155         stateMachine->SetIfReuseSupplierId(false);
156         stateMachine->SetReuseApnCap(NetManagerStandard::NetCap::NET_CAPABILITY_END);
157     }
158     CellularDataHiSysEvent::WriteDataDeactiveBehaviorEvent(stateMachine->GetSlotId(),
159         DataDisconnectCause::LOST_CONNECTION, ApnManager::FindApnNameByApnId(stateMachine->apnId_));
160     Inactive *inActive = static_cast<Inactive *>(stateMachine->inActiveState_.GetRefPtr());
161     if (inActive == nullptr) {
162         TELEPHONY_LOGE("inActive is null");
163         return false;
164     }
165     inActive->SetDeActiveApnTypeId(stateMachine->apnId_);
166     inActive->SetDataCallResultInfo(resultInfo);
167 
168 #ifdef OHOS_BUILD_ENABLE_TELEPHONY_EXT
169     if (TELEPHONY_EXT_WRAPPER.dataEndSelfCure_) {
170         int32_t cause = static_cast<int32_t>(stateMachine->cause_);
171         int32_t slotId = stateMachine->GetSlotId();
172         TELEPHONY_LOGI("cause%{private}d:, slotId%{public}d", cause, slotId);
173         TELEPHONY_EXT_WRAPPER.dataEndSelfCure_(cause, slotId);
174     }
175 #endif
176     stateMachine->TransitionTo(stateMachine->inActiveState_);
177     return PROCESSED;
178 }
179 
ProcessRilAdapterHostDied(const AppExecFwk::InnerEvent::Pointer & event)180 bool Active::ProcessRilAdapterHostDied(const AppExecFwk::InnerEvent::Pointer &event)
181 {
182     TELEPHONY_LOGI("Active::EVENT_RIL_ADAPTER_HOST_DIED");
183     return ProcessLostConnection(event);
184 }
185 
ProcessLinkCapabilityChanged(const AppExecFwk::InnerEvent::Pointer & event)186 bool Active::ProcessLinkCapabilityChanged(const AppExecFwk::InnerEvent::Pointer &event)
187 {
188     TELEPHONY_LOGI("Active::MSG_SM_LINK_CAPABILITY_CHANGED");
189     if (event == nullptr) {
190         TELEPHONY_LOGE("event is null");
191         return false;
192     }
193     std::shared_ptr<DataLinkCapability> dataLinkCapability = event->GetSharedObject<DataLinkCapability>();
194     if (dataLinkCapability == nullptr) {
195         TELEPHONY_LOGE("result info is null");
196         return false;
197     }
198     uint32_t upBandwidth = static_cast<uint32_t>(dataLinkCapability->primaryUplinkKbps);
199     uint32_t downBandwidth = static_cast<uint32_t>(dataLinkCapability->primaryDownlinkKbps);
200     std::shared_ptr<CellularDataStateMachine> shareStateMachine = stateMachine_.lock();
201     if (shareStateMachine == nullptr) {
202         TELEPHONY_LOGE("shareStateMachine is null");
203         return false;
204     }
205     if (upBandwidth == 0 || downBandwidth == 0) {
206         RefreshConnectionBandwidths();
207     } else {
208         shareStateMachine->SetConnectionBandwidth(upBandwidth, downBandwidth);
209     }
210     shareStateMachine->UpdateNetworkInfo();
211     return true;
212 }
213 
ProcessDataConnectionRoamOn(const AppExecFwk::InnerEvent::Pointer & event)214 bool Active::ProcessDataConnectionRoamOn(const AppExecFwk::InnerEvent::Pointer &event)
215 {
216     TELEPHONY_LOGI("Active::EVENT_DATA_CONNECTION_ROAM_ON");
217     std::shared_ptr<CellularDataStateMachine> stateMachine = stateMachine_.lock();
218     if (stateMachine == nullptr) {
219         TELEPHONY_LOGE("stateMachine is null");
220         return false;
221     }
222     CellularDataNetAgent &netAgent = CellularDataNetAgent::GetInstance();
223     int32_t supplierId = netAgent.GetSupplierId(stateMachine->GetSlotId(), stateMachine->GetCapability());
224     netAgent.UpdateNetSupplierInfo(supplierId, stateMachine->netSupplierInfo_);
225     return PROCESSED;
226 }
227 
ProcessDataConnectionRoamOff(const AppExecFwk::InnerEvent::Pointer & event)228 bool Active::ProcessDataConnectionRoamOff(const AppExecFwk::InnerEvent::Pointer &event)
229 {
230     TELEPHONY_LOGI("Active::EVENT_DATA_CONNECTION_ROAM_OFF");
231     std::shared_ptr<CellularDataStateMachine> stateMachine = stateMachine_.lock();
232     if (stateMachine == nullptr) {
233         TELEPHONY_LOGE("stateMachine is null");
234         return false;
235     }
236     CellularDataNetAgent &netAgent = CellularDataNetAgent::GetInstance();
237     int32_t supplierId = netAgent.GetSupplierId(stateMachine->GetSlotId(), stateMachine->GetCapability());
238     netAgent.UpdateNetSupplierInfo(supplierId, stateMachine->netSupplierInfo_);
239     return PROCESSED;
240 }
241 
ProcessDataConnectionVoiceCallStartedOrEnded(const AppExecFwk::InnerEvent::Pointer & event)242 bool Active::ProcessDataConnectionVoiceCallStartedOrEnded(const AppExecFwk::InnerEvent::Pointer &event)
243 {
244     std::shared_ptr<CellularDataStateMachine> stateMachine = stateMachine_.lock();
245     if (stateMachine == nullptr) {
246         TELEPHONY_LOGE("stateMachine is null");
247         return false;
248     }
249     CellularDataNetAgent &netAgent = CellularDataNetAgent::GetInstance();
250     int32_t supplierId = netAgent.GetSupplierId(stateMachine->GetSlotId(), stateMachine->GetCapability());
251     netAgent.UpdateNetSupplierInfo(supplierId, stateMachine->netSupplierInfo_);
252     return PROCESSED;
253 }
254 
ProcessDataConnectionComplete(const AppExecFwk::InnerEvent::Pointer & event)255 bool Active::ProcessDataConnectionComplete(const AppExecFwk::InnerEvent::Pointer &event)
256 {
257     if (event == nullptr) {
258         TELEPHONY_LOGE("event is null");
259         return false;
260     }
261     std::shared_ptr<SetupDataCallResultInfo> resultInfo = event->GetSharedObject<SetupDataCallResultInfo>();
262     if (resultInfo == nullptr) {
263         TELEPHONY_LOGE("result info is null");
264         return false;
265     }
266     std::shared_ptr<CellularDataStateMachine> shareStateMachine = stateMachine_.lock();
267     if (shareStateMachine == nullptr) {
268         TELEPHONY_LOGE("shareStateMachine is null");
269         return false;
270     }
271     resultInfo->flag = shareStateMachine->apnId_;
272 
273     if (shareStateMachine->stateMachineEventHandler_ == nullptr) {
274         TELEPHONY_LOGE("stateMachineEventHandler_ is null");
275         return false;
276     }
277     shareStateMachine->stateMachineEventHandler_->RemoveEvent(CellularDataEventCode::MSG_CONNECT_TIMEOUT_CHECK);
278     if (shareStateMachine->cellularDataHandler_ != nullptr) {
279         shareStateMachine->cellularDataHandler_->SendEvent(
280             CellularDataEventCode::MSG_ESTABLISH_DATA_CONNECTION_COMPLETE, resultInfo);
281     } else {
282         TELEPHONY_LOGE("cellularDataHandler is null");
283         return false;
284     }
285     return true;
286 }
287 
ProcessNrStateChanged(const AppExecFwk::InnerEvent::Pointer & event)288 bool Active::ProcessNrStateChanged(const AppExecFwk::InnerEvent::Pointer &event)
289 {
290     std::shared_ptr<CellularDataStateMachine> shareStateMachine = stateMachine_.lock();
291     if (shareStateMachine == nullptr) {
292         TELEPHONY_LOGE("shareStateMachine is null");
293         return false;
294     }
295     TELEPHONY_LOGI("ProcessNrStateChanged event");
296     RefreshTcpBufferSizes();
297     RefreshConnectionBandwidths();
298     shareStateMachine->UpdateNetworkInfo();
299     return true;
300 }
301 
ProcessNrFrequencyChanged(const AppExecFwk::InnerEvent::Pointer & event)302 bool Active::ProcessNrFrequencyChanged(const AppExecFwk::InnerEvent::Pointer &event)
303 {
304     std::shared_ptr<CellularDataStateMachine> shareStateMachine = stateMachine_.lock();
305     if (shareStateMachine == nullptr) {
306         TELEPHONY_LOGE("shareStateMachine is null");
307         return false;
308     }
309     TELEPHONY_LOGI("ProcessNrFrequencyChanged event");
310     RefreshConnectionBandwidths();
311     shareStateMachine->UpdateNetworkInfo();
312     return true;
313 }
314 
RefreshTcpBufferSizes()315 void Active::RefreshTcpBufferSizes()
316 {
317     std::shared_ptr<CellularDataStateMachine> shareStateMachine = stateMachine_.lock();
318     if (shareStateMachine == nullptr) {
319         TELEPHONY_LOGE("shareStateMachine is null");
320         return;
321     }
322     int32_t slotId = shareStateMachine->GetSlotId();
323     int32_t radioTech = static_cast<int32_t>(RadioTech::RADIO_TECHNOLOGY_INVALID);
324     CoreManagerInner::GetInstance().GetPsRadioTech(slotId, radioTech);
325     if (shareStateMachine->cdConnectionManager_ == nullptr) {
326         TELEPHONY_LOGE("cdConnectionManager_ is null");
327         return;
328     }
329     std::string tcpBuffer = shareStateMachine->cdConnectionManager_->GetTcpBufferByRadioTech(radioTech);
330     TELEPHONY_LOGI("tcpBuffer is %{public}s", tcpBuffer.c_str());
331     shareStateMachine->SetConnectionTcpBuffer(tcpBuffer);
332 }
333 
RefreshConnectionBandwidths()334 void Active::RefreshConnectionBandwidths()
335 {
336     std::shared_ptr<CellularDataStateMachine> shareStateMachine = stateMachine_.lock();
337     if (shareStateMachine == nullptr) {
338         TELEPHONY_LOGE("shareStateMachine is null");
339         return;
340     }
341     int32_t slotId = shareStateMachine->GetSlotId();
342     int32_t radioTech = static_cast<int32_t>(RadioTech::RADIO_TECHNOLOGY_INVALID);
343     CoreManagerInner::GetInstance().GetPsRadioTech(slotId, radioTech);
344     if (shareStateMachine->cdConnectionManager_ == nullptr) {
345         TELEPHONY_LOGE("cdConnectionManager_ is null");
346         return;
347     }
348     LinkBandwidthInfo linkBandwidthInfo = shareStateMachine->cdConnectionManager_->GetBandwidthsByRadioTech(radioTech);
349     TELEPHONY_LOGI("upBandwidth is %{public}u, downBandwidth is %{public}u", linkBandwidthInfo.upBandwidth,
350         linkBandwidthInfo.downBandwidth);
351     shareStateMachine->SetConnectionBandwidth(linkBandwidthInfo.upBandwidth, linkBandwidthInfo.downBandwidth);
352 }
353 } // namespace Telephony
354 } // namespace OHOS
355