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