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