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