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 "telephony_log_wrapper.h"
19
20 #include "cellular_data_utils.h"
21 #include "inactive.h"
22 #include "core_manager_inner.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 return (this->*(it->second))(event);
56 }
57 return retVal;
58 }
59
ProcessConnectDone(const AppExecFwk::InnerEvent::Pointer & event)60 bool Active::ProcessConnectDone(const AppExecFwk::InnerEvent::Pointer &event)
61 {
62 TELEPHONY_LOGI("Active::MSG_SM_CONNECT");
63 return PROCESSED;
64 }
65
ProcessDisconnectDone(const AppExecFwk::InnerEvent::Pointer & event)66 bool Active::ProcessDisconnectDone(const AppExecFwk::InnerEvent::Pointer &event)
67 {
68 TELEPHONY_LOGI("Active::MSG_SM_DISCONNECT");
69 std::shared_ptr<CellularDataStateMachine> stateMachine = stateMachine_.lock();
70 if (stateMachine == nullptr) {
71 TELEPHONY_LOGE("stateMachine is null");
72 return false;
73 }
74 std::unique_ptr<DataDisconnectParams> object = event->GetUniqueObject<DataDisconnectParams>();
75 DisConnectionReason reason = object->GetReason();
76 Inactive *inActive = static_cast<Inactive *>(stateMachine->inActiveState_.GetRefPtr());
77 inActive->SetReason(reason);
78 stateMachine->FreeConnection(*object);
79 stateMachine->TransitionTo(stateMachine->disconnectingState_);
80 return PROCESSED;
81 }
82
ProcessDisconnectAllDone(const AppExecFwk::InnerEvent::Pointer & event)83 bool Active::ProcessDisconnectAllDone(const AppExecFwk::InnerEvent::Pointer &event)
84 {
85 TELEPHONY_LOGI("Active::MSG_SM_DISCONNECT_ALL");
86 std::shared_ptr<CellularDataStateMachine> stateMachine = stateMachine_.lock();
87 if (stateMachine == nullptr) {
88 TELEPHONY_LOGE("stateMachine is null");
89 return false;
90 }
91 std::unique_ptr<DataDisconnectParams> object = event->GetUniqueObject<DataDisconnectParams>();
92 DisConnectionReason reason = object->GetReason();
93 Inactive *inActive = static_cast<Inactive *>(stateMachine->inActiveState_.GetRefPtr());
94 inActive->SetReason(reason);
95 stateMachine->FreeConnection(*object);
96 stateMachine->TransitionTo(stateMachine->disconnectingState_);
97 return PROCESSED;
98 }
99
ProcessLostConnection(const AppExecFwk::InnerEvent::Pointer & event)100 bool Active::ProcessLostConnection(const AppExecFwk::InnerEvent::Pointer &event)
101 {
102 TELEPHONY_LOGI("Active::EVENT_LOST_CONNECTION");
103 std::shared_ptr<CellularDataStateMachine> stateMachine = stateMachine_.lock();
104 if (stateMachine == nullptr) {
105 TELEPHONY_LOGE("stateMachine is null");
106 return false;
107 }
108 Inactive *inActive = static_cast<Inactive *>(stateMachine->inActiveState_.GetRefPtr());
109 inActive->SetDeActiveApnTypeId(stateMachine->apnId_);
110 inActive->SetReason(DisConnectionReason::REASON_RETRY_CONNECTION);
111 stateMachine->TransitionTo(stateMachine->inActiveState_);
112 return PROCESSED;
113 }
114
ProcessDataConnectionRoamOn(const AppExecFwk::InnerEvent::Pointer & event)115 bool Active::ProcessDataConnectionRoamOn(const AppExecFwk::InnerEvent::Pointer &event)
116 {
117 TELEPHONY_LOGI("Active::EVENT_DATA_CONNECTION_ROAM_ON");
118 std::shared_ptr<CellularDataStateMachine> stateMachine = stateMachine_.lock();
119 if (stateMachine == nullptr) {
120 TELEPHONY_LOGE("stateMachine is null");
121 return false;
122 }
123 CellularDataNetAgent &netAgent = CellularDataNetAgent::GetInstance();
124 int32_t supplierId = netAgent.GetSupplierId(stateMachine->GetSlotId(), stateMachine->GetCapability());
125 netAgent.UpdateNetSupplierInfo(supplierId, stateMachine->netSupplierInfo_);
126 return PROCESSED;
127 }
128
ProcessDataConnectionRoamOff(const AppExecFwk::InnerEvent::Pointer & event)129 bool Active::ProcessDataConnectionRoamOff(const AppExecFwk::InnerEvent::Pointer &event)
130 {
131 TELEPHONY_LOGI("Active::EVENT_DATA_CONNECTION_ROAM_OFF");
132 std::shared_ptr<CellularDataStateMachine> stateMachine = stateMachine_.lock();
133 if (stateMachine == nullptr) {
134 TELEPHONY_LOGE("stateMachine is null");
135 return false;
136 }
137 CellularDataNetAgent &netAgent = CellularDataNetAgent::GetInstance();
138 int32_t supplierId = netAgent.GetSupplierId(stateMachine->GetSlotId(), stateMachine->GetCapability());
139 netAgent.UpdateNetSupplierInfo(supplierId, stateMachine->netSupplierInfo_);
140 return PROCESSED;
141 }
142
ProcessDataConnectionVoiceCallStartedOrEnded(const AppExecFwk::InnerEvent::Pointer & event)143 bool Active::ProcessDataConnectionVoiceCallStartedOrEnded(const AppExecFwk::InnerEvent::Pointer &event)
144 {
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
ProcessGetBandwidthsFromRil(const AppExecFwk::InnerEvent::Pointer & event)156 bool Active::ProcessGetBandwidthsFromRil(const AppExecFwk::InnerEvent::Pointer &event)
157 {
158 if (event == nullptr) {
159 TELEPHONY_LOGE("event is null");
160 return false;
161 }
162 std::shared_ptr<DataLinkBandwidthInfo> dataLinkBandwidthInfo = event->GetSharedObject<DataLinkBandwidthInfo>();
163 if (dataLinkBandwidthInfo == nullptr) {
164 TELEPHONY_LOGE("result info is null");
165 return false;
166 }
167 uint32_t upBandwidth = dataLinkBandwidthInfo->ulMfbr;
168 uint32_t downBandwidth = dataLinkBandwidthInfo->dlMfbr;
169 std::shared_ptr<CellularDataStateMachine> shareStateMachine = stateMachine_.lock();
170 if (shareStateMachine == nullptr) {
171 TELEPHONY_LOGE("shareStateMachine is null");
172 return false;
173 }
174 shareStateMachine->SetConnectionBandwidth(upBandwidth, downBandwidth);
175 shareStateMachine->UpdateNetworkInfo();
176 return true;
177 }
178
ProcessDataConnectionComplete(const AppExecFwk::InnerEvent::Pointer & event)179 bool Active::ProcessDataConnectionComplete(const AppExecFwk::InnerEvent::Pointer &event)
180 {
181 if (event == nullptr) {
182 TELEPHONY_LOGE("event is null");
183 return false;
184 }
185 std::shared_ptr<SetupDataCallResultInfo> resultInfo = event->GetSharedObject<SetupDataCallResultInfo>();
186 if (resultInfo == nullptr) {
187 TELEPHONY_LOGE("result info is null");
188 return false;
189 }
190 std::shared_ptr<CellularDataStateMachine> shareStateMachine = stateMachine_.lock();
191 if (shareStateMachine == nullptr) {
192 TELEPHONY_LOGE("shareStateMachine is null");
193 return false;
194 }
195 resultInfo->flag = shareStateMachine->apnId_;
196 shareStateMachine->stateMachineEventHandler_->RemoveEvent(CellularDataEventCode::MSG_CONNECT_TIMEOUT_CHECK);
197 if (shareStateMachine->cellularDataHandler_ != nullptr) {
198 shareStateMachine->cellularDataHandler_->SendEvent(
199 CellularDataEventCode::MSG_ESTABLISH_DATA_CONNECTION_COMPLETE, resultInfo);
200 } else {
201 TELEPHONY_LOGE("cellularDataHandler is null");
202 return false;
203 }
204 return true;
205 }
206
ProcessNrStateChanged(const AppExecFwk::InnerEvent::Pointer & event)207 bool Active::ProcessNrStateChanged(const AppExecFwk::InnerEvent::Pointer &event)
208 {
209 std::shared_ptr<CellularDataStateMachine> shareStateMachine = stateMachine_.lock();
210 if (shareStateMachine == nullptr) {
211 TELEPHONY_LOGE("shareStateMachine is null");
212 return false;
213 }
214 TELEPHONY_LOGI("ProcessNrStateChanged event");
215 RefreshTcpBufferSizes();
216 RefreshConnectionBandwidths();
217 shareStateMachine->UpdateNetworkInfo();
218 return true;
219 }
220
ProcessNrFrequencyChanged(const AppExecFwk::InnerEvent::Pointer & event)221 bool Active::ProcessNrFrequencyChanged(const AppExecFwk::InnerEvent::Pointer &event)
222 {
223 std::shared_ptr<CellularDataStateMachine> shareStateMachine = stateMachine_.lock();
224 if (shareStateMachine == nullptr) {
225 TELEPHONY_LOGE("shareStateMachine is null");
226 return false;
227 }
228 TELEPHONY_LOGI("ProcessNrFrequencyChanged event");
229 RefreshConnectionBandwidths();
230 shareStateMachine->UpdateNetworkInfo();
231 return true;
232 }
233
RefreshTcpBufferSizes()234 void Active::RefreshTcpBufferSizes()
235 {
236 std::shared_ptr<CellularDataStateMachine> shareStateMachine = stateMachine_.lock();
237 if (shareStateMachine == nullptr) {
238 TELEPHONY_LOGE("shareStateMachine is null");
239 return;
240 }
241 int32_t slotId = shareStateMachine->GetSlotId();
242 int32_t radioTech = CoreManagerInner::GetInstance().GetPsRadioTech(slotId);
243 std::string tcpBuffer = shareStateMachine->cdConnectionManager_->GetTcpBufferByRadioTech(radioTech);
244 TELEPHONY_LOGI("tcpBuffer is %{public}s", tcpBuffer.c_str());
245 shareStateMachine->SetConnectionTcpBuffer(tcpBuffer);
246 }
247
RefreshConnectionBandwidths()248 void Active::RefreshConnectionBandwidths()
249 {
250 std::shared_ptr<CellularDataStateMachine> shareStateMachine = stateMachine_.lock();
251 if (shareStateMachine == nullptr) {
252 TELEPHONY_LOGE("shareStateMachine is null");
253 return;
254 }
255 int32_t slotId = shareStateMachine->GetSlotId();
256 int32_t radioTech = CoreManagerInner::GetInstance().GetPsRadioTech(slotId);
257 LinkBandwidthInfo linkBandwidthInfo = shareStateMachine->cdConnectionManager_->GetBandwidthsByRadioTech(radioTech);
258 TELEPHONY_LOGI("upBandwidth is %{public}u, downBandwidth is %{public}u", linkBandwidthInfo.upBandwidth,
259 linkBandwidthInfo.downBandwidth);
260 shareStateMachine->SetConnectionBandwidth(linkBandwidthInfo.upBandwidth, linkBandwidthInfo.downBandwidth);
261 }
262 } // namespace Telephony
263 } // namespace OHOS
264