• 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 
ProcessLinkCapabilityChanged(const AppExecFwk::InnerEvent::Pointer & event)128 bool Active::ProcessLinkCapabilityChanged(const AppExecFwk::InnerEvent::Pointer &event)
129 {
130     TELEPHONY_LOGI("Active::MSG_SM_LINK_CAPABILITY_CHANGED");
131     if (event == nullptr) {
132         TELEPHONY_LOGE("event is null");
133         return false;
134     }
135     std::shared_ptr<DataLinkCapability> dataLinkCapability = event->GetSharedObject<DataLinkCapability>();
136     if (dataLinkCapability == nullptr) {
137         TELEPHONY_LOGE("result info is null");
138         return false;
139     }
140     uint32_t upBandwidth = static_cast<uint32_t>(dataLinkCapability->primaryUplinkKbps);
141     uint32_t downBandwidth = static_cast<uint32_t>(dataLinkCapability->primaryDownlinkKbps);
142     std::shared_ptr<CellularDataStateMachine> shareStateMachine = stateMachine_.lock();
143     if (shareStateMachine == nullptr) {
144         TELEPHONY_LOGE("shareStateMachine is null");
145         return false;
146     }
147     if (upBandwidth == 0 || downBandwidth == 0) {
148         RefreshConnectionBandwidths();
149     } else {
150         shareStateMachine->SetConnectionBandwidth(upBandwidth, downBandwidth);
151     }
152     shareStateMachine->UpdateNetworkInfo();
153     return true;
154 }
155 
ProcessDataConnectionRoamOn(const AppExecFwk::InnerEvent::Pointer & event)156 bool Active::ProcessDataConnectionRoamOn(const AppExecFwk::InnerEvent::Pointer &event)
157 {
158     TELEPHONY_LOGI("Active::EVENT_DATA_CONNECTION_ROAM_ON");
159     std::shared_ptr<CellularDataStateMachine> stateMachine = stateMachine_.lock();
160     if (stateMachine == nullptr) {
161         TELEPHONY_LOGE("stateMachine is null");
162         return false;
163     }
164     CellularDataNetAgent &netAgent = CellularDataNetAgent::GetInstance();
165     int32_t supplierId = netAgent.GetSupplierId(stateMachine->GetSlotId(), stateMachine->GetCapability());
166     netAgent.UpdateNetSupplierInfo(supplierId, stateMachine->netSupplierInfo_);
167     return PROCESSED;
168 }
169 
ProcessDataConnectionRoamOff(const AppExecFwk::InnerEvent::Pointer & event)170 bool Active::ProcessDataConnectionRoamOff(const AppExecFwk::InnerEvent::Pointer &event)
171 {
172     TELEPHONY_LOGI("Active::EVENT_DATA_CONNECTION_ROAM_OFF");
173     std::shared_ptr<CellularDataStateMachine> stateMachine = stateMachine_.lock();
174     if (stateMachine == nullptr) {
175         TELEPHONY_LOGE("stateMachine is null");
176         return false;
177     }
178     CellularDataNetAgent &netAgent = CellularDataNetAgent::GetInstance();
179     int32_t supplierId = netAgent.GetSupplierId(stateMachine->GetSlotId(), stateMachine->GetCapability());
180     netAgent.UpdateNetSupplierInfo(supplierId, stateMachine->netSupplierInfo_);
181     return PROCESSED;
182 }
183 
ProcessDataConnectionVoiceCallStartedOrEnded(const AppExecFwk::InnerEvent::Pointer & event)184 bool Active::ProcessDataConnectionVoiceCallStartedOrEnded(const AppExecFwk::InnerEvent::Pointer &event)
185 {
186     std::shared_ptr<CellularDataStateMachine> stateMachine = stateMachine_.lock();
187     if (stateMachine == nullptr) {
188         TELEPHONY_LOGE("stateMachine is null");
189         return false;
190     }
191     CellularDataNetAgent &netAgent = CellularDataNetAgent::GetInstance();
192     int32_t supplierId = netAgent.GetSupplierId(stateMachine->GetSlotId(), stateMachine->GetCapability());
193     netAgent.UpdateNetSupplierInfo(supplierId, stateMachine->netSupplierInfo_);
194     return PROCESSED;
195 }
196 
ProcessDataConnectionComplete(const AppExecFwk::InnerEvent::Pointer & event)197 bool Active::ProcessDataConnectionComplete(const AppExecFwk::InnerEvent::Pointer &event)
198 {
199     if (event == nullptr) {
200         TELEPHONY_LOGE("event is null");
201         return false;
202     }
203     std::shared_ptr<SetupDataCallResultInfo> resultInfo = event->GetSharedObject<SetupDataCallResultInfo>();
204     if (resultInfo == nullptr) {
205         TELEPHONY_LOGE("result info is null");
206         return false;
207     }
208     std::shared_ptr<CellularDataStateMachine> shareStateMachine = stateMachine_.lock();
209     if (shareStateMachine == nullptr) {
210         TELEPHONY_LOGE("shareStateMachine is null");
211         return false;
212     }
213     resultInfo->flag = shareStateMachine->apnId_;
214 
215     if (shareStateMachine->stateMachineEventHandler_ == nullptr) {
216         TELEPHONY_LOGE("stateMachineEventHandler_ is null");
217         return false;
218     }
219     shareStateMachine->stateMachineEventHandler_->RemoveEvent(CellularDataEventCode::MSG_CONNECT_TIMEOUT_CHECK);
220     if (shareStateMachine->cellularDataHandler_ != nullptr) {
221         shareStateMachine->cellularDataHandler_->SendEvent(
222             CellularDataEventCode::MSG_ESTABLISH_DATA_CONNECTION_COMPLETE, resultInfo);
223     } else {
224         TELEPHONY_LOGE("cellularDataHandler is null");
225         return false;
226     }
227     return true;
228 }
229 
ProcessNrStateChanged(const AppExecFwk::InnerEvent::Pointer & event)230 bool Active::ProcessNrStateChanged(const AppExecFwk::InnerEvent::Pointer &event)
231 {
232     std::shared_ptr<CellularDataStateMachine> shareStateMachine = stateMachine_.lock();
233     if (shareStateMachine == nullptr) {
234         TELEPHONY_LOGE("shareStateMachine is null");
235         return false;
236     }
237     TELEPHONY_LOGI("ProcessNrStateChanged event");
238     RefreshTcpBufferSizes();
239     RefreshConnectionBandwidths();
240     shareStateMachine->UpdateNetworkInfo();
241     return true;
242 }
243 
ProcessNrFrequencyChanged(const AppExecFwk::InnerEvent::Pointer & event)244 bool Active::ProcessNrFrequencyChanged(const AppExecFwk::InnerEvent::Pointer &event)
245 {
246     std::shared_ptr<CellularDataStateMachine> shareStateMachine = stateMachine_.lock();
247     if (shareStateMachine == nullptr) {
248         TELEPHONY_LOGE("shareStateMachine is null");
249         return false;
250     }
251     TELEPHONY_LOGI("ProcessNrFrequencyChanged event");
252     RefreshConnectionBandwidths();
253     shareStateMachine->UpdateNetworkInfo();
254     return true;
255 }
256 
RefreshTcpBufferSizes()257 void Active::RefreshTcpBufferSizes()
258 {
259     std::shared_ptr<CellularDataStateMachine> shareStateMachine = stateMachine_.lock();
260     if (shareStateMachine == nullptr) {
261         TELEPHONY_LOGE("shareStateMachine is null");
262         return;
263     }
264     int32_t slotId = shareStateMachine->GetSlotId();
265     int32_t radioTech = static_cast<int32_t>(RadioTech::RADIO_TECHNOLOGY_INVALID);
266     CoreManagerInner::GetInstance().GetPsRadioTech(slotId, radioTech);
267     if (shareStateMachine->cdConnectionManager_ == nullptr) {
268         TELEPHONY_LOGE("cdConnectionManager_ is null");
269         return;
270     }
271     std::string tcpBuffer = shareStateMachine->cdConnectionManager_->GetTcpBufferByRadioTech(radioTech);
272     TELEPHONY_LOGI("tcpBuffer is %{public}s", tcpBuffer.c_str());
273     shareStateMachine->SetConnectionTcpBuffer(tcpBuffer);
274 }
275 
RefreshConnectionBandwidths()276 void Active::RefreshConnectionBandwidths()
277 {
278     std::shared_ptr<CellularDataStateMachine> shareStateMachine = stateMachine_.lock();
279     if (shareStateMachine == nullptr) {
280         TELEPHONY_LOGE("shareStateMachine is null");
281         return;
282     }
283     int32_t slotId = shareStateMachine->GetSlotId();
284     int32_t radioTech = static_cast<int32_t>(RadioTech::RADIO_TECHNOLOGY_INVALID);
285     CoreManagerInner::GetInstance().GetPsRadioTech(slotId, radioTech);
286     if (shareStateMachine->cdConnectionManager_ == nullptr) {
287         TELEPHONY_LOGE("cdConnectionManager_ is null");
288         return;
289     }
290     LinkBandwidthInfo linkBandwidthInfo = shareStateMachine->cdConnectionManager_->GetBandwidthsByRadioTech(radioTech);
291     TELEPHONY_LOGI("upBandwidth is %{public}u, downBandwidth is %{public}u", linkBandwidthInfo.upBandwidth,
292         linkBandwidthInfo.downBandwidth);
293     shareStateMachine->SetConnectionBandwidth(linkBandwidthInfo.upBandwidth, linkBandwidthInfo.downBandwidth);
294 }
295 } // namespace Telephony
296 } // namespace OHOS
297