• 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_type.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 {
NetworkType(const std::weak_ptr<NetworkSearchManager> & networkSearchManager,int32_t slotId)25 NetworkType::NetworkType(const std::weak_ptr<NetworkSearchManager> &networkSearchManager, int32_t slotId)
26     : networkSearchManager_(networkSearchManager), slotId_(slotId)
27 {}
28 
ProcessGetPreferredNetwork(const AppExecFwk::InnerEvent::Pointer & event) const29 void NetworkType::ProcessGetPreferredNetwork(const AppExecFwk::InnerEvent::Pointer &event) const
30 {
31     if (event == nullptr) {
32         TELEPHONY_LOGE("NetworkType::ProcessGetPreferredNetwork event is nullptr");
33         return;
34     }
35     std::shared_ptr<PreferredNetworkTypeInfo> preferredNetworkInfo =
36         event->GetSharedObject<PreferredNetworkTypeInfo>();
37     std::shared_ptr<RadioResponseInfo> responseInfo = event->GetSharedObject<RadioResponseInfo>();
38     if (preferredNetworkInfo == nullptr && responseInfo == nullptr) {
39         TELEPHONY_LOGE("NetworkType::ProcessGetPreferredNetwork object is nullptr\n");
40         return;
41     }
42     std::shared_ptr<NetworkSearchManager> networkSearchManager = networkSearchManager_.lock();
43     if (networkSearchManager != nullptr && preferredNetworkInfo != nullptr) {
44         networkSearchManager->SetCachePreferredNetworkValue(slotId_, preferredNetworkInfo->preferredNetworkType);
45     }
46     MessageParcel data;
47     int64_t index = -1;
48     if (!WriteGetPreferredNetworkInfo(preferredNetworkInfo, responseInfo, data, index)) {
49         return;
50     }
51     std::shared_ptr<NetworkSearchCallbackInfo> callbackInfo = NetworkUtils::FindNetworkSearchCallback(index);
52     if (callbackInfo != nullptr) {
53         sptr<INetworkSearchCallback> callback = callbackInfo->networkSearchItem_;
54         if (callback != nullptr) {
55             callback->OnNetworkSearchCallback(
56                 INetworkSearchCallback::NetworkSearchCallback::GET_PREFERRED_NETWORK_MODE_RESULT, data);
57             TELEPHONY_LOGI("NetworkType::ProcessGetPreferredNetwork callback success");
58         }
59         NetworkUtils::RemoveCallbackFromMap(index);
60     } else {
61         TELEPHONY_LOGI("NetworkType::ProcessGetPreferredNetwork has no callbackInfo");
62     }
63 }
64 
ProcessSetPreferredNetwork(const AppExecFwk::InnerEvent::Pointer & event) const65 void NetworkType::ProcessSetPreferredNetwork(const AppExecFwk::InnerEvent::Pointer &event) const
66 {
67     if (event == nullptr) {
68         TELEPHONY_LOGE("NetworkType::ProcessSetPreferredNetwork event is nullptr");
69         return;
70     }
71     std::shared_ptr<NetworkSearchManager> networkSearchManager = networkSearchManager_.lock();
72     if (networkSearchManager == nullptr) {
73         TELEPHONY_LOGE("NetworkType::ProcessSetPreferredNetwork networkSearchManager is nullptr");
74         return;
75     }
76     std::shared_ptr<RadioResponseInfo> responseInfo = event->GetSharedObject<RadioResponseInfo>();
77     if (responseInfo == nullptr) {
78         TELEPHONY_LOGE("NetworkType::ProcessSetPreferredNetwork responseInfo is nullptr");
79         return;
80     }
81 
82     bool success = responseInfo->error == ErrType::NONE;
83     int32_t networkMode = 0;
84     if (success) {
85         networkSearchManager->GetCachePreferredNetworkValue(slotId_, networkMode);
86         if (networkMode >= static_cast<int32_t>(PreferredNetworkMode::CORE_NETWORK_MODE_AUTO) &&
87             networkMode < static_cast<int32_t>(PreferredNetworkMode::CORE_NETWORK_MODE_MAX_VALUE)) {
88             networkSearchManager->SavePreferredNetworkValue(slotId_, networkMode);
89         }
90     } else {
91         networkMode = networkSearchManager->GetPreferredNetworkValue(slotId_);
92         networkSearchManager->SetCachePreferredNetworkValue(slotId_, networkMode);
93     }
94     int64_t index = responseInfo->flag;
95     std::shared_ptr<NetworkSearchCallbackInfo> callbackInfo = NetworkUtils::FindNetworkSearchCallback(index);
96     if (callbackInfo == nullptr) {
97         TELEPHONY_LOGE("NetworkType::ProcessSetPreferredNetwork callbackInfo is nullptr slotId:%{public}d", slotId_);
98         return;
99     }
100     sptr<INetworkSearchCallback> callback = callbackInfo->networkSearchItem_;
101     if (callback == nullptr) {
102         TELEPHONY_LOGE("NetworkType::ProcessSetPreferredNetwork callback is nullptr slotId:%{public}d", slotId_);
103         return;
104     }
105     MessageParcel data;
106     data.WriteInterfaceToken(INetworkSearchCallback::GetDescriptor());
107     if (!data.WriteBool(success) ||
108         !data.WriteInt32(success ? TELEPHONY_SUCCESS : (int32_t)responseInfo->error)) {
109         TELEPHONY_LOGE("NetworkType::ProcessSetPreferredNetwork write date fail slotId:%{public}d", slotId_);
110         return;
111     }
112     callback->OnNetworkSearchCallback(
113         INetworkSearchCallback::NetworkSearchCallback::SET_PREFERRED_NETWORK_MODE_RESULT, data);
114     NetworkUtils::RemoveCallbackFromMap(index);
115 }
116 
WriteGetPreferredNetworkInfo(std::shared_ptr<PreferredNetworkTypeInfo> & preferredNetworkInfo,std::shared_ptr<RadioResponseInfo> & responseInfo,MessageParcel & data,int64_t & index) const117 bool NetworkType::WriteGetPreferredNetworkInfo(std::shared_ptr<PreferredNetworkTypeInfo> &preferredNetworkInfo,
118     std::shared_ptr<RadioResponseInfo> &responseInfo, MessageParcel &data, int64_t &index) const
119 {
120     std::shared_ptr<NetworkSearchManager> networkSearchManager = networkSearchManager_.lock();
121     if (networkSearchManager == nullptr) {
122         TELEPHONY_LOGE("NetworkType::ProcessGetPreferredNetwork networkSearchManager is nullptr\n");
123         return false;
124     }
125     int32_t networkMode = -1;
126     if (!data.WriteInterfaceToken(INetworkSearchCallback::GetDescriptor())) {
127         TELEPHONY_LOGE("NetworkType::ProcessGetPreferredNetwork WriteInterfaceToken failed");
128         return false;
129     }
130     if (preferredNetworkInfo != nullptr) {
131         networkMode = preferredNetworkInfo->preferredNetworkType;
132         index = preferredNetworkInfo->flag;
133         networkSearchManager->SavePreferredNetworkValue(slotId_, networkMode);
134         std::shared_ptr<NetworkSearchCallbackInfo> callbackInfo = NetworkUtils::FindNetworkSearchCallback(index);
135         if (callbackInfo != nullptr) {
136             bool isChipsetNetworkExtSupported = static_cast<bool>(callbackInfo->param_);
137             if (!isChipsetNetworkExtSupported && TELEPHONY_EXT_WRAPPER.getPreferredNetworkExt_ != nullptr) {
138                 TELEPHONY_EXT_WRAPPER.getPreferredNetworkExt_(networkMode);
139             }
140         }
141         if (!data.WriteInt32(networkMode) || !data.WriteInt32(TELEPHONY_SUCCESS)) {
142             TELEPHONY_LOGE("NetworkType::ProcessGetPreferredNetwork WriteInt32 networkMode is false");
143             return false;
144         }
145     } else if (responseInfo != nullptr) {
146         TELEPHONY_LOGE("NetworkType::ProcessGetPreferredNetwork error code is %{public}d", responseInfo->error);
147         index = responseInfo->flag;
148         if (!data.WriteInt32(networkMode) && !data.WriteInt32(static_cast<int32_t>(responseInfo->error))) {
149             TELEPHONY_LOGE("NetworkType::ProcessGetPreferredNetwork WriteInt32 networkMode is false");
150             return false;
151         }
152     }
153     return true;
154 }
155 } // namespace Telephony
156 } // namespace OHOS
157