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