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