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 "network_selection.h"
17
18 #include "network_search_manager.h"
19 #include "telephony_errors.h"
20 #include "telephony_log_wrapper.h"
21 #include "telephony_ext_wrapper.h"
22 #include "operator_name_utils.h"
23
24 namespace OHOS {
25 namespace Telephony {
NetworkSelection(std::weak_ptr<NetworkSearchManager> networkSearchManager,int32_t slotId)26 NetworkSelection::NetworkSelection(std::weak_ptr<NetworkSearchManager> networkSearchManager, int32_t slotId)
27 : networkSearchManager_(networkSearchManager), slotId_(slotId)
28 {}
29
ProcessNetworkSearchResult(const AppExecFwk::InnerEvent::Pointer & event) const30 void NetworkSelection::ProcessNetworkSearchResult(const AppExecFwk::InnerEvent::Pointer &event) const
31 {
32 TELEPHONY_LOGI("NetworkSelection::ProcessNetworkSearchResult slotId:%{public}d", slotId_);
33 if (event == nullptr) {
34 TELEPHONY_LOGE("NetworkSelection::ProcessNetworkSearchResult event is nullptr slotId:%{public}d", slotId_);
35 return;
36 }
37 std::shared_ptr<NetworkSearchManager> nsm = networkSearchManager_.lock();
38 if (nsm == nullptr) {
39 TELEPHONY_LOGE("NetworkSelection::ProcessNetworkSearchResult nsm is nullptr slotId:%{public}d", slotId_);
40 return;
41 }
42 std::shared_ptr<AvailableNetworkList> availNetworkResult = event->GetSharedObject<AvailableNetworkList>();
43 std::shared_ptr<RadioResponseInfo> responseInfo = event->GetSharedObject<RadioResponseInfo>();
44 if (availNetworkResult == nullptr && responseInfo == nullptr) {
45 TELEPHONY_LOGE("NetworkSelection::ProcessNetworkSearchResult object is nullptr slotId:%{public}d", slotId_);
46 return;
47 }
48
49 MessageParcel data;
50 int64_t index = -1;
51 data.WriteInterfaceToken(INetworkSearchCallback::GetDescriptor());
52 if (availNetworkResult != nullptr) {
53 if (!AvailNetworkResult(availNetworkResult, data, index)) {
54 return;
55 }
56 } else if (responseInfo != nullptr) {
57 if (!ResponseInfoOfResult(responseInfo, data, index)) {
58 return;
59 }
60 }
61 std::shared_ptr<NetworkSearchCallbackInfo> callbackInfo = NetworkUtils::FindNetworkSearchCallback(index);
62 if (callbackInfo != nullptr) {
63 sptr<INetworkSearchCallback> callback = callbackInfo->networkSearchItem_;
64 if (callback != nullptr) {
65 callback->OnNetworkSearchCallback(
66 INetworkSearchCallback::NetworkSearchCallback::GET_AVAILABLE_RESULT, data);
67 TELEPHONY_LOGI("NetworkSelection::ProcessNetworkSearchResult callback success slotId:%{public}d", slotId_);
68 }
69 NetworkUtils::RemoveCallbackFromMap(index);
70 } else {
71 TELEPHONY_LOGE("NetworkSelection::ProcessNetworkSearchResult callbackInfo is null slotId:%{public}d", slotId_);
72 }
73 }
74
ProcessGetNetworkSelectionMode(const AppExecFwk::InnerEvent::Pointer & event) const75 void NetworkSelection::ProcessGetNetworkSelectionMode(const AppExecFwk::InnerEvent::Pointer &event) const
76 {
77 TELEPHONY_LOGI("NetworkSelection::ProcessGetNetworkSelectionMode slotId:%{public}d", slotId_);
78 if (event == nullptr) {
79 TELEPHONY_LOGE("NetworkSelection::ProcessGetNetworkSelectionMode event is nullptr slotId:%{public}d", slotId_);
80 return;
81 }
82
83 std::shared_ptr<NetworkSearchManager> nsm = networkSearchManager_.lock();
84 if (nsm == nullptr) {
85 TELEPHONY_LOGE("NetworkSelection::ProcessNetworkSearchResult nsm is nullptr slotId:%{public}d", slotId_);
86 return;
87 }
88 std::shared_ptr<SetNetworkModeInfo> selectModeResult = event->GetSharedObject<SetNetworkModeInfo>();
89 std::shared_ptr<RadioResponseInfo> responseInfo = event->GetSharedObject<RadioResponseInfo>();
90 if (selectModeResult == nullptr && responseInfo == nullptr) {
91 TELEPHONY_LOGE(
92 "NetworkSelection::ProcessGetNetworkSelectionMode SelectModeResultInfo, NetworkSearchManager"
93 "or RadioResponseInfo is nullptr slotId:%{public}d",
94 slotId_);
95 return;
96 }
97
98 MessageParcel data;
99 int64_t index = -1;
100 data.WriteInterfaceToken(INetworkSearchCallback::GetDescriptor());
101 if (selectModeResult != nullptr) {
102 if (!SelectModeResult(selectModeResult, data, index)) {
103 return;
104 }
105 } else if (responseInfo != nullptr) {
106 if (!ResponseInfoOfGet(responseInfo, data, index)) {
107 return;
108 }
109 }
110
111 std::shared_ptr<NetworkSearchCallbackInfo> callbackInfo = NetworkUtils::FindNetworkSearchCallback(index);
112 if (callbackInfo != nullptr) {
113 sptr<INetworkSearchCallback> callback = callbackInfo->networkSearchItem_;
114 if (callback != nullptr) {
115 callback->OnNetworkSearchCallback(
116 INetworkSearchCallback::NetworkSearchCallback::GET_NETWORK_MODE_RESULT, data);
117 TELEPHONY_LOGI(
118 "NetworkSelection::ProcessGetNetworkSelectionMode callback success slotId:%{public}d", slotId_);
119 } else {
120 TELEPHONY_LOGE(
121 "NetworkSelection::ProcessGetNetworkSelectionMode callback is null slotId:%{public}d", slotId_);
122 }
123 NetworkUtils::RemoveCallbackFromMap(index);
124 } else {
125 TELEPHONY_LOGE(
126 "NetworkSelection::ProcessGetNetworkSelectionMode callbackInfo is null slotId:%{public}d", slotId_);
127 }
128 }
129
ProcessSetNetworkSelectionMode(const AppExecFwk::InnerEvent::Pointer & event) const130 void NetworkSelection::ProcessSetNetworkSelectionMode(const AppExecFwk::InnerEvent::Pointer &event) const
131 {
132 TELEPHONY_LOGI("NetworkSelection::ProcessSetNetworkSelectionMode ok slotId:%{public}d", slotId_);
133 if (event == nullptr) {
134 TELEPHONY_LOGE("NetworkSelection::ProcessSetNetworkSelectionMode event is nullptr slotId:%{public}d", slotId_);
135 return;
136 }
137 std::shared_ptr<NetworkSearchManager> nsm = networkSearchManager_.lock();
138 if (nsm == nullptr) {
139 TELEPHONY_LOGE("NetworkSelection::ProcessSetNetworkSelectionMode nsm is nullptr slotId:%{public}d", slotId_);
140 return;
141 }
142 std::shared_ptr<RadioResponseInfo> responseInfo = event->GetSharedObject<RadioResponseInfo>();
143 if (responseInfo == nullptr) {
144 TELEPHONY_LOGE("NetworkSelection::ProcessSetNetworkSelectionMode responseInfo is nullptr");
145 return;
146 }
147
148 MessageParcel data;
149 data.WriteInterfaceToken(INetworkSearchCallback::GetDescriptor());
150 int64_t index = responseInfo->flag;
151 if (!ResponseInfoOfSet(responseInfo, data, index)) {
152 return;
153 }
154
155 std::shared_ptr<NetworkSearchCallbackInfo> callbackInfo = NetworkUtils::FindNetworkSearchCallback(index);
156 if (callbackInfo == nullptr) {
157 TELEPHONY_LOGE(
158 "NetworkSelection::ProcessSetNetworkSelectionMode callbackInfo is nullptr slotId:%{public}d", slotId_);
159 return;
160 }
161 sptr<INetworkSearchCallback> callback = callbackInfo->networkSearchItem_;
162 nsm->SetNetworkSelectionValue(slotId_, static_cast<SelectionMode>(callbackInfo->param_));
163 TELEPHONY_LOGI("NetworkSelection::ProcessSetNetworkSelectionMode selectionMode:%{public}d slotId:%{public}d",
164 callbackInfo->param_, slotId_);
165 if (callback != nullptr) {
166 callback->OnNetworkSearchCallback(INetworkSearchCallback::NetworkSearchCallback::SET_NETWORK_MODE_RESULT, data);
167 TELEPHONY_LOGI("NetworkSelection::ProcessSetNetworkSelectionMode callback success slotId:%{public}d", slotId_);
168 } else {
169 TELEPHONY_LOGE("NetworkSelection::ProcessSetNetworkSelectionMode callback fail slotId:%{public}d", slotId_);
170 }
171 NetworkUtils::RemoveCallbackFromMap(index);
172 }
173
AvailNetworkResult(std::shared_ptr<AvailableNetworkList> availNetworkResult,MessageParcel & data,int64_t & index) const174 bool NetworkSelection::AvailNetworkResult(
175 std::shared_ptr<AvailableNetworkList> availNetworkResult, MessageParcel &data, int64_t &index) const
176 {
177 std::shared_ptr<NetworkSearchManager> nsm = networkSearchManager_.lock();
178 if (nsm == nullptr) {
179 TELEPHONY_LOGE("NetworkSelection::ProcessNetworkSearchResult nsm is nullptr slotId:%{public}d", slotId_);
180 return false;
181 }
182 index = -1;
183 if (availNetworkResult != nullptr) {
184 int32_t availableSize = availNetworkResult->itemNum;
185 index = availNetworkResult->flag;
186 const std::vector<AvailableNetworkInfo> &availableNetworkInfo = availNetworkResult->availableNetworkInfo;
187 std::vector<NetworkInformation> networkInformation;
188 if (availableSize > 0) {
189 for (auto &availableNetworkInfoItem : availableNetworkInfo) {
190 std::string numeric = availableNetworkInfoItem.numeric;
191 std::string customName = OperatorNameUtils::GetInstance().GetCustomName(numeric);
192 std::string longName = customName.empty()? availableNetworkInfoItem.longName : customName;
193 std::string shortName = availableNetworkInfoItem.shortName;
194 int32_t status = availableNetworkInfoItem.status;
195 int32_t rat = availableNetworkInfoItem.rat;
196 NetworkInformation networkStateItem;
197 networkStateItem.SetOperateInformation(longName, shortName, numeric, status, rat);
198 networkInformation.push_back(networkStateItem);
199 }
200 }
201 if (TELEPHONY_EXT_WRAPPER.onGetNetworkSearchInformationExt_ != nullptr) {
202 TELEPHONY_EXT_WRAPPER.onGetNetworkSearchInformationExt_(availableSize, networkInformation);
203 }
204
205 nsm->SetNetworkSearchResultValue(slotId_, availableSize, networkInformation);
206 sptr<NetworkSearchResult> networkSearchResult = nsm->GetNetworkSearchInformationValue(slotId_);
207 if (networkSearchResult != nullptr) {
208 networkSearchResult->Marshalling(data);
209 }
210 if (!data.WriteInt32(TELEPHONY_SUCCESS)) {
211 TELEPHONY_LOGE(
212 "NetworkSelection::ProcessNetworkSearchResult WriteInt32 errorCode is false slotId:%{public}d",
213 slotId_);
214 return false;
215 }
216 }
217 return true;
218 }
219
ResponseInfoOfResult(std::shared_ptr<RadioResponseInfo> responseInfo,MessageParcel & data,int64_t & index) const220 bool NetworkSelection::ResponseInfoOfResult(
221 std::shared_ptr<RadioResponseInfo> responseInfo, MessageParcel &data, int64_t &index) const
222 {
223 if (responseInfo != nullptr) {
224 TELEPHONY_LOGE("NetworkSelection::RilRadioResponseInfoOfResult error code is %{public}d slotId:%{public}d",
225 responseInfo->error, slotId_);
226 index = responseInfo->flag;
227 sptr<NetworkSearchResult> networkSearchResult = new (std::nothrow) NetworkSearchResult;
228 networkSearchResult->Marshalling(data);
229 if (!data.WriteInt32((int32_t)responseInfo->error)) {
230 TELEPHONY_LOGE(
231 "NetworkSelection::RilRadioResponseInfoOfResult WriteInt32 errorCode is false slotId:%{public}d",
232 slotId_);
233 return false;
234 }
235 }
236 return true;
237 }
238
ResponseInfoOfGet(std::shared_ptr<RadioResponseInfo> responseInfo,MessageParcel & data,int64_t & index) const239 bool NetworkSelection::ResponseInfoOfGet(
240 std::shared_ptr<RadioResponseInfo> responseInfo, MessageParcel &data, int64_t &index) const
241 {
242 if (responseInfo != nullptr) {
243 TELEPHONY_LOGE(
244 "NetworkSelection::RilRadioResponseInfoOfGet RadioResponseInfo error is %{public}d "
245 "slotId:%{public}d",
246 responseInfo->error, slotId_);
247 index = responseInfo->flag;
248 if (!data.WriteInt32(static_cast<int32_t>(SelectionMode::MODE_TYPE_UNKNOWN))) {
249 TELEPHONY_LOGE(
250 "NetworkSelection::RilRadioResponseInfoOfGet WriteInt32 slotId is false slotId:%{public}d", slotId_);
251 return false;
252 }
253 if (!data.WriteInt32((int32_t)responseInfo->error)) {
254 TELEPHONY_LOGE(
255 "NetworkSelection::RilRadioResponseInfoOfGet WriteInt32 errorCode is false slotId:%{public}d", slotId_);
256 return false;
257 }
258 }
259 return true;
260 }
261
ResponseInfoOfSet(std::shared_ptr<RadioResponseInfo> responseInfo,MessageParcel & data,int64_t & index) const262 bool NetworkSelection::ResponseInfoOfSet(
263 std::shared_ptr<RadioResponseInfo> responseInfo, MessageParcel &data, int64_t &index) const
264 {
265 if (responseInfo->error == ErrType::NONE) {
266 if (!data.WriteBool(true) || !data.WriteInt32(TELEPHONY_SUCCESS)) {
267 TELEPHONY_LOGE("NetworkSelection::ResponseInfoOfSet write data fail slotId:%{public}d", slotId_);
268 return false;
269 }
270 } else {
271 if (!data.WriteBool(false) || !data.WriteInt32((int32_t)responseInfo->error)) {
272 TELEPHONY_LOGE("NetworkSelection::ResponseInfoOfSet write data fail slotId:%{public}d", slotId_);
273 return false;
274 }
275 }
276 return true;
277 }
278
SelectModeResult(std::shared_ptr<SetNetworkModeInfo> selectModeResult,MessageParcel & data,int64_t & index) const279 bool NetworkSelection::SelectModeResult(
280 std::shared_ptr<SetNetworkModeInfo> selectModeResult, MessageParcel &data, int64_t &index) const
281 {
282 std::shared_ptr<NetworkSearchManager> nsm = networkSearchManager_.lock();
283 if (nsm == nullptr) {
284 TELEPHONY_LOGE("NetworkSelection::SelectModeResult nsm is nullptr slotId:%{public}d", slotId_);
285 return false;
286 }
287
288 if (selectModeResult == nullptr) {
289 TELEPHONY_LOGE("NetworkSelection::SelectModeResult selectModeResult is nullptr slotId:%{public}d", slotId_);
290 return false;
291 }
292 int32_t selectMode = selectModeResult->selectMode;
293 TELEPHONY_LOGI("NetworkSelection::ProcessGetNetworkSelectionMode selectMode:%{public}d slotId:%{public}d",
294 selectMode, slotId_);
295 nsm->SetNetworkSelectionValue(slotId_, static_cast<SelectionMode>(selectMode));
296 if (!data.WriteInt32(selectMode)) {
297 TELEPHONY_LOGE(
298 "NetworkSelection::ProcessGetNetworkSelectionMode WriteInt32 slotId is false slotId:%{public}d", slotId_);
299 return false;
300 }
301 if (!data.WriteInt32(TELEPHONY_SUCCESS)) {
302 TELEPHONY_LOGE(
303 "NetworkSelection::ProcessGetNetworkSelectionMode WriteInt32 errorCode is false slotId:%{public}d",
304 slotId_);
305 return false;
306 }
307 index = selectModeResult->flag;
308 return true;
309 }
310 } // namespace Telephony
311 } // namespace OHOS
312