• 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 "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 {
26 const std::set<std::string> NetworkSelection::radioTechStrings_ = {" 2G", " 3G", " 4G", " 5G"};
NetworkSelection(std::weak_ptr<NetworkSearchManager> networkSearchManager,int32_t slotId)27 NetworkSelection::NetworkSelection(std::weak_ptr<NetworkSearchManager> networkSearchManager, int32_t slotId)
28     : networkSearchManager_(networkSearchManager), slotId_(slotId)
29 {}
30 
ProcessNetworkSearchResult(const AppExecFwk::InnerEvent::Pointer & event) const31 void NetworkSelection::ProcessNetworkSearchResult(const AppExecFwk::InnerEvent::Pointer &event) const
32 {
33     TELEPHONY_LOGI("NetworkSelection::ProcessNetworkSearchResult slotId:%{public}d", slotId_);
34     if (event == nullptr) {
35         TELEPHONY_LOGE("NetworkSelection::ProcessNetworkSearchResult event is nullptr slotId:%{public}d", slotId_);
36         return;
37     }
38     std::shared_ptr<NetworkSearchManager> nsm = networkSearchManager_.lock();
39     if (nsm == nullptr) {
40         TELEPHONY_LOGE("NetworkSelection::ProcessNetworkSearchResult nsm is nullptr slotId:%{public}d", slotId_);
41         return;
42     }
43     std::shared_ptr<AvailableNetworkList> availNetworkResult = event->GetSharedObject<AvailableNetworkList>();
44     std::shared_ptr<RadioResponseInfo> responseInfo = event->GetSharedObject<RadioResponseInfo>();
45     if (availNetworkResult == nullptr && responseInfo == nullptr) {
46         TELEPHONY_LOGE("NetworkSelection::ProcessNetworkSearchResult object is nullptr slotId:%{public}d", slotId_);
47         return;
48     }
49 
50     MessageParcel data;
51     int64_t index = -1;
52     data.WriteInterfaceToken(INetworkSearchCallback::GetDescriptor());
53     if (availNetworkResult != nullptr) {
54         if (!AvailNetworkResult(availNetworkResult, data, index)) {
55             return;
56         }
57     } else if (responseInfo != nullptr) {
58         if (!ResponseInfoOfResult(responseInfo, data, index)) {
59             return;
60         }
61     }
62     std::shared_ptr<NetworkSearchCallbackInfo> callbackInfo = NetworkUtils::FindNetworkSearchCallback(index);
63     if (callbackInfo != nullptr) {
64         sptr<INetworkSearchCallback> callback = callbackInfo->networkSearchItem_;
65         if (callback != nullptr) {
66             callback->OnNetworkSearchCallback(
67                 INetworkSearchCallback::NetworkSearchCallback::GET_AVAILABLE_RESULT, data);
68             TELEPHONY_LOGI("NetworkSelection::ProcessNetworkSearchResult callback success slotId:%{public}d", slotId_);
69         }
70         NetworkUtils::RemoveCallbackFromMap(index);
71     } else {
72         TELEPHONY_LOGE("NetworkSelection::ProcessNetworkSearchResult callbackInfo is null slotId:%{public}d", slotId_);
73     }
74 }
75 
ProcessGetNetworkSelectionMode(const AppExecFwk::InnerEvent::Pointer & event) const76 void NetworkSelection::ProcessGetNetworkSelectionMode(const AppExecFwk::InnerEvent::Pointer &event) const
77 {
78     TELEPHONY_LOGI("NetworkSelection::ProcessGetNetworkSelectionMode slotId:%{public}d", slotId_);
79     if (event == nullptr) {
80         TELEPHONY_LOGE("NetworkSelection::ProcessGetNetworkSelectionMode event is nullptr slotId:%{public}d", slotId_);
81         return;
82     }
83 
84     std::shared_ptr<NetworkSearchManager> nsm = networkSearchManager_.lock();
85     if (nsm == nullptr) {
86         TELEPHONY_LOGE("NetworkSelection::ProcessNetworkSearchResult nsm is nullptr slotId:%{public}d", slotId_);
87         return;
88     }
89     std::shared_ptr<SetNetworkModeInfo> selectModeResult = event->GetSharedObject<SetNetworkModeInfo>();
90     std::shared_ptr<RadioResponseInfo> responseInfo = event->GetSharedObject<RadioResponseInfo>();
91     if (selectModeResult == nullptr && responseInfo == nullptr) {
92         TELEPHONY_LOGE(
93             "NetworkSelection::ProcessGetNetworkSelectionMode SelectModeResultInfo, NetworkSearchManager"
94             "or RadioResponseInfo is nullptr slotId:%{public}d",
95             slotId_);
96         return;
97     }
98 
99     MessageParcel data;
100     int64_t index = -1;
101     data.WriteInterfaceToken(INetworkSearchCallback::GetDescriptor());
102     if (selectModeResult != nullptr) {
103         if (!SelectModeResult(selectModeResult, data, index)) {
104             return;
105         }
106     } else if (responseInfo != nullptr) {
107         if (!ResponseInfoOfGet(responseInfo, data, index)) {
108             return;
109         }
110     }
111 
112     std::shared_ptr<NetworkSearchCallbackInfo> callbackInfo = NetworkUtils::FindNetworkSearchCallback(index);
113     if (callbackInfo != nullptr) {
114         sptr<INetworkSearchCallback> callback = callbackInfo->networkSearchItem_;
115         if (callback != nullptr) {
116             callback->OnNetworkSearchCallback(
117                 INetworkSearchCallback::NetworkSearchCallback::GET_NETWORK_MODE_RESULT, data);
118             TELEPHONY_LOGI(
119                 "NetworkSelection::ProcessGetNetworkSelectionMode callback success slotId:%{public}d", slotId_);
120         } else {
121             TELEPHONY_LOGE(
122                 "NetworkSelection::ProcessGetNetworkSelectionMode callback is null slotId:%{public}d", slotId_);
123         }
124         NetworkUtils::RemoveCallbackFromMap(index);
125     } else {
126         TELEPHONY_LOGE(
127             "NetworkSelection::ProcessGetNetworkSelectionMode callbackInfo is null slotId:%{public}d", slotId_);
128     }
129 }
130 
ProcessSetNetworkSelectionMode(const AppExecFwk::InnerEvent::Pointer & event) const131 void NetworkSelection::ProcessSetNetworkSelectionMode(const AppExecFwk::InnerEvent::Pointer &event) const
132 {
133     TELEPHONY_LOGI("NetworkSelection::ProcessSetNetworkSelectionMode ok slotId:%{public}d", slotId_);
134     if (event == nullptr) {
135         TELEPHONY_LOGE("NetworkSelection::ProcessSetNetworkSelectionMode event is nullptr slotId:%{public}d", slotId_);
136         return;
137     }
138     std::shared_ptr<NetworkSearchManager> nsm = networkSearchManager_.lock();
139     if (nsm == nullptr) {
140         TELEPHONY_LOGE("NetworkSelection::ProcessSetNetworkSelectionMode nsm is nullptr slotId:%{public}d", slotId_);
141         return;
142     }
143     std::shared_ptr<RadioResponseInfo> responseInfo = event->GetSharedObject<RadioResponseInfo>();
144     if (responseInfo == nullptr) {
145         TELEPHONY_LOGE("NetworkSelection::ProcessSetNetworkSelectionMode responseInfo is nullptr");
146         return;
147     }
148 
149     MessageParcel data;
150     data.WriteInterfaceToken(INetworkSearchCallback::GetDescriptor());
151     int64_t index = responseInfo->flag;
152     if (!ResponseInfoOfSet(responseInfo, data, index)) {
153         return;
154     }
155 
156     std::shared_ptr<NetworkSearchCallbackInfo> callbackInfo = NetworkUtils::FindNetworkSearchCallback(index);
157     if (callbackInfo == nullptr) {
158         TELEPHONY_LOGE(
159             "NetworkSelection::ProcessSetNetworkSelectionMode callbackInfo is nullptr slotId:%{public}d", slotId_);
160         return;
161     }
162     sptr<INetworkSearchCallback> callback = callbackInfo->networkSearchItem_;
163     nsm->SetNetworkSelectionValue(slotId_, static_cast<SelectionMode>(callbackInfo->param_));
164     TELEPHONY_LOGI("NetworkSelection::ProcessSetNetworkSelectionMode selectionMode:%{public}d slotId:%{public}d",
165         callbackInfo->param_, slotId_);
166     if (callback != nullptr) {
167         callback->OnNetworkSearchCallback(INetworkSearchCallback::NetworkSearchCallback::SET_NETWORK_MODE_RESULT, data);
168         TELEPHONY_LOGI("NetworkSelection::ProcessSetNetworkSelectionMode callback success slotId:%{public}d", slotId_);
169     } else {
170         TELEPHONY_LOGE("NetworkSelection::ProcessSetNetworkSelectionMode callback fail slotId:%{public}d", slotId_);
171     }
172     NetworkUtils::RemoveCallbackFromMap(index);
173 }
174 
AvailNetworkResult(std::shared_ptr<AvailableNetworkList> availNetworkResult,MessageParcel & data,int64_t & index) const175 bool NetworkSelection::AvailNetworkResult(
176     std::shared_ptr<AvailableNetworkList> availNetworkResult, MessageParcel &data, int64_t &index) const
177 {
178     std::shared_ptr<NetworkSearchManager> nsm = networkSearchManager_.lock();
179     if (nsm == nullptr) {
180         TELEPHONY_LOGE("NetworkSelection::ProcessNetworkSearchResult nsm is nullptr slotId:%{public}d", slotId_);
181         return false;
182     }
183     index = -1;
184     if (availNetworkResult != nullptr) {
185         int32_t availableSize = availNetworkResult->itemNum;
186         index = availNetworkResult->flag;
187         const std::vector<AvailableNetworkInfo> &availableNetworkInfo = availNetworkResult->availableNetworkInfo;
188         std::vector<NetworkInformation> networkInformation;
189         if (availableSize > 0) {
190             for (auto &availableNetworkInfoItem : availableNetworkInfo) {
191                 std::string numeric = availableNetworkInfoItem.numeric;
192                 std::string longName = GetCustomName(availableNetworkInfoItem);
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 
GetCustomName(const AvailableNetworkInfo & info)220 std::string NetworkSelection::GetCustomName(const AvailableNetworkInfo &info)
221 {
222     std::string rawName = info.longName;
223     std::string plmn = OperatorNameUtils::GetInstance().GetCustomName(info.numeric);
224     if (plmn.empty()) {
225         return rawName;
226     }
227     auto indexOfBlank = rawName.find_last_of(' ');
228     if (indexOfBlank == std::string::npos) {
229         return plmn;
230     }
231     std::string radioTechStr = rawName.substr(indexOfBlank);
232     if (radioTechStrings_.find(radioTechStr) != radioTechStrings_.end()) {
233         return plmn + radioTechStr;
234     }
235     return plmn;
236 }
237 
ResponseInfoOfResult(std::shared_ptr<RadioResponseInfo> responseInfo,MessageParcel & data,int64_t & index) const238 bool NetworkSelection::ResponseInfoOfResult(
239     std::shared_ptr<RadioResponseInfo> responseInfo, MessageParcel &data, int64_t &index) const
240 {
241     if (responseInfo != nullptr) {
242         TELEPHONY_LOGE("NetworkSelection::RilRadioResponseInfoOfResult error code is %{public}d slotId:%{public}d",
243             responseInfo->error, slotId_);
244         index = responseInfo->flag;
245         sptr<NetworkSearchResult> networkSearchResult = new (std::nothrow) NetworkSearchResult;
246         if (networkSearchResult == nullptr) {
247             TELEPHONY_LOGE("NetworkSelection::RilRadioResponseInfoOfResult networkSearchResult is nullptr "
248                 "slotId:%{public}d", slotId_);
249             return false;
250         }
251         networkSearchResult->Marshalling(data);
252         if (!data.WriteInt32((int32_t)responseInfo->error)) {
253             TELEPHONY_LOGE(
254                 "NetworkSelection::RilRadioResponseInfoOfResult WriteInt32 errorCode is false slotId:%{public}d",
255                 slotId_);
256             return false;
257         }
258     }
259     return true;
260 }
261 
ResponseInfoOfGet(std::shared_ptr<RadioResponseInfo> responseInfo,MessageParcel & data,int64_t & index) const262 bool NetworkSelection::ResponseInfoOfGet(
263     std::shared_ptr<RadioResponseInfo> responseInfo, MessageParcel &data, int64_t &index) const
264 {
265     if (responseInfo != nullptr) {
266         TELEPHONY_LOGE(
267             "NetworkSelection::RilRadioResponseInfoOfGet RadioResponseInfo error is %{public}d "
268             "slotId:%{public}d",
269             responseInfo->error, slotId_);
270         index = responseInfo->flag;
271         if (!data.WriteInt32(static_cast<int32_t>(SelectionMode::MODE_TYPE_UNKNOWN))) {
272             TELEPHONY_LOGE(
273                 "NetworkSelection::RilRadioResponseInfoOfGet WriteInt32 slotId is false slotId:%{public}d", slotId_);
274             return false;
275         }
276         if (!data.WriteInt32((int32_t)responseInfo->error)) {
277             TELEPHONY_LOGE(
278                 "NetworkSelection::RilRadioResponseInfoOfGet WriteInt32 errorCode is false slotId:%{public}d", slotId_);
279             return false;
280         }
281     }
282     return true;
283 }
284 
ResponseInfoOfSet(std::shared_ptr<RadioResponseInfo> responseInfo,MessageParcel & data,int64_t & index) const285 bool NetworkSelection::ResponseInfoOfSet(
286     std::shared_ptr<RadioResponseInfo> responseInfo, MessageParcel &data, int64_t &index) const
287 {
288     if (responseInfo->error == ErrType::NONE) {
289         if (!data.WriteBool(true) || !data.WriteInt32(TELEPHONY_SUCCESS)) {
290             TELEPHONY_LOGE("NetworkSelection::ResponseInfoOfSet write data fail slotId:%{public}d", slotId_);
291             return false;
292         }
293     } else {
294         if (!data.WriteBool(false) || !data.WriteInt32((int32_t)responseInfo->error)) {
295             TELEPHONY_LOGE("NetworkSelection::ResponseInfoOfSet write data fail slotId:%{public}d", slotId_);
296             return false;
297         }
298     }
299     return true;
300 }
301 
SelectModeResult(std::shared_ptr<SetNetworkModeInfo> selectModeResult,MessageParcel & data,int64_t & index) const302 bool NetworkSelection::SelectModeResult(
303     std::shared_ptr<SetNetworkModeInfo> selectModeResult, MessageParcel &data, int64_t &index) const
304 {
305     std::shared_ptr<NetworkSearchManager> nsm = networkSearchManager_.lock();
306     if (nsm == nullptr) {
307         TELEPHONY_LOGE("NetworkSelection::SelectModeResult nsm is nullptr slotId:%{public}d", slotId_);
308         return false;
309     }
310 
311     if (selectModeResult == nullptr) {
312         TELEPHONY_LOGE("NetworkSelection::SelectModeResult selectModeResult is nullptr slotId:%{public}d", slotId_);
313         return false;
314     }
315     int32_t selectMode = selectModeResult->selectMode;
316     TELEPHONY_LOGI("NetworkSelection::ProcessGetNetworkSelectionMode selectMode:%{public}d slotId:%{public}d",
317         selectMode, slotId_);
318     nsm->SetNetworkSelectionValue(slotId_, static_cast<SelectionMode>(selectMode));
319     if (!data.WriteInt32(selectMode)) {
320         TELEPHONY_LOGE(
321             "NetworkSelection::ProcessGetNetworkSelectionMode WriteInt32 slotId is false slotId:%{public}d", slotId_);
322         return false;
323     }
324     if (!data.WriteInt32(TELEPHONY_SUCCESS)) {
325         TELEPHONY_LOGE(
326             "NetworkSelection::ProcessGetNetworkSelectionMode WriteInt32 errorCode is false slotId:%{public}d",
327             slotId_);
328         return false;
329     }
330     index = selectModeResult->flag;
331     return true;
332 }
333 } // namespace Telephony
334 } // namespace OHOS
335