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 "radio_info.h"
17
18 #include "hril_types.h"
19 #include "hril_modem_parcel.h"
20 #include "network_search_manager.h"
21 #include "telephony_errors.h"
22 #include "telephony_log_wrapper.h"
23
24 namespace OHOS {
25 namespace Telephony {
RadioInfo(std::weak_ptr<NetworkSearchManager> networkSearchManager,int32_t slotId)26 RadioInfo::RadioInfo(std::weak_ptr<NetworkSearchManager> networkSearchManager, int32_t slotId)
27 : networkSearchManager_(networkSearchManager), slotId_(slotId)
28 {}
29
ProcessGetRadioState(const AppExecFwk::InnerEvent::Pointer & event) const30 void RadioInfo::ProcessGetRadioState(const AppExecFwk::InnerEvent::Pointer &event) const
31 {
32 if (event == nullptr) {
33 TELEPHONY_LOGE("RadioInfo::ProcessGetRadioState event is nullptr slotId:%{public}d", slotId_);
34 return;
35 }
36 std::unique_ptr<HRilRadioStateInfo> object = event->GetUniqueObject<HRilRadioStateInfo>();
37 std::shared_ptr<HRilRadioResponseInfo> responseInfo = event->GetSharedObject<HRilRadioResponseInfo>();
38 std::shared_ptr<NetworkSearchManager> nsm = networkSearchManager_.lock();
39 if ((responseInfo == nullptr && object == nullptr) || nsm == nullptr) {
40 TELEPHONY_LOGE("RadioInfo::ProcessGetRadioState object is nullptr slotId:%{public}d", slotId_);
41 return;
42 }
43 int64_t index = 0;
44 bool state = false;
45 MessageParcel data;
46 data.WriteInterfaceToken(INetworkSearchCallback::GetDescriptor());
47 if (responseInfo != nullptr) {
48 TELEPHONY_LOGE("RadioInfo::ProcessGetRadioState false slotId:%{public}d", slotId_);
49 index = responseInfo->flag;
50 if (!data.WriteBool(state) || !data.WriteInt32((int32_t)responseInfo->error)) {
51 NetworkUtils::RemoveCallbackFromMap(index);
52 return;
53 }
54 }
55 if (object != nullptr) {
56 index = object->flag;
57 int32_t radioState = object->state;
58 TELEPHONY_LOGI("ProcessGetRadioState RadioState is:%{public}d slotId:%{public}d", radioState, slotId_);
59 state = (radioState == ModemPowerState::CORE_SERVICE_POWER_ON) ? true : false;
60 nsm->SetRadioStateValue(slotId_, (ModemPowerState)radioState);
61 if (!data.WriteBool(state) || !data.WriteInt32(TELEPHONY_SUCCESS)) {
62 NetworkUtils::RemoveCallbackFromMap(index);
63 return;
64 }
65 }
66 std::shared_ptr<NetworkSearchCallbackInfo> callbackInfo = NetworkUtils::FindNetworkSearchCallback(index);
67 if (callbackInfo != nullptr) {
68 sptr<INetworkSearchCallback> callback = callbackInfo->networkSearchItem_;
69 if (callback != nullptr && callback->OnNetworkSearchCallback(
70 INetworkSearchCallback::NetworkSearchCallback::GET_RADIO_STATUS_RESULT, data)) {
71 TELEPHONY_LOGE("RadioInfo::ProcessGetRadioState callback fail slotId:%{public}d", slotId_);
72 }
73 NetworkUtils::RemoveCallbackFromMap(index);
74 } else {
75 if (nsm->GetRadioState(slotId_) != ModemPowerState::CORE_SERVICE_POWER_ON && !nsm->GetAirplaneMode()) {
76 nsm->SetRadioState(slotId_, static_cast<bool>(ModemPowerState::CORE_SERVICE_POWER_ON), 0);
77 }
78 if (nsm->GetRadioState(slotId_) == ModemPowerState::CORE_SERVICE_POWER_ON) {
79 nsm->TriggerSimRefresh(slotId_);
80 }
81 }
82 }
83
ProcessSetRadioState(const AppExecFwk::InnerEvent::Pointer & event) const84 void RadioInfo::ProcessSetRadioState(const AppExecFwk::InnerEvent::Pointer &event) const
85 {
86 if (event == nullptr) {
87 TELEPHONY_LOGE("RadioInfo::ProcessSetRadioState event is nullptr slotId:%{public}d", slotId_);
88 return;
89 }
90 std::unique_ptr<HRilRadioStateInfo> object = event->GetUniqueObject<HRilRadioStateInfo>();
91 std::shared_ptr<HRilRadioResponseInfo> responseInfo = event->GetSharedObject<HRilRadioResponseInfo>();
92 std::shared_ptr<NetworkSearchManager> nsm = networkSearchManager_.lock();
93 if ((responseInfo == nullptr && object == nullptr) || nsm == nullptr) {
94 TELEPHONY_LOGE("RadioInfo::ProcessSetRadioState object is nullptr slotId:%{public}d", slotId_);
95 return;
96 }
97 MessageParcel data;
98 int64_t index = 0;
99 ModemPowerState radioState = ModemPowerState::CORE_SERVICE_POWER_NOT_AVAILABLE;
100 bool result = true;
101 data.WriteInterfaceToken(INetworkSearchCallback::GetDescriptor());
102 if (responseInfo != nullptr) {
103 TELEPHONY_LOGE("RadioInfo::ProcessSetRadioState false slotId:%{public}d", slotId_);
104 index = responseInfo->flag;
105 int32_t error = static_cast<int32_t>(responseInfo->error);
106 int32_t status = static_cast<int32_t>(HRilErrNumber::HRIL_ERR_REPEAT_STATUS);
107 result = (error == status) ? true : false;
108 if (!data.WriteBool(result) || !data.WriteInt32((int32_t)responseInfo->error)) {
109 NetworkUtils::RemoveCallbackFromMap(index);
110 return;
111 }
112 }
113 if (object != nullptr) {
114 TELEPHONY_LOGI("RadioInfo::ProcessSetRadioState ok slotId:%{public}d", slotId_);
115 index = object->flag;
116 radioState = (ModemPowerState)object->flag;
117 result = true;
118 if (!data.WriteBool(result) || !data.WriteInt32(TELEPHONY_SUCCESS)) {
119 NetworkUtils::RemoveCallbackFromMap(index);
120 return;
121 }
122 }
123
124 std::shared_ptr<NetworkSearchCallbackInfo> callbackInfo =
125 NetworkUtils::FindNetworkSearchCallback(index);
126 if (callbackInfo != nullptr) {
127 if (result) {
128 nsm->SetRadioStateValue(slotId_, (ModemPowerState)(callbackInfo->param_));
129 radioState = (ModemPowerState)callbackInfo->param_;
130 }
131 sptr<INetworkSearchCallback> callback = callbackInfo->networkSearchItem_;
132 if (callback != nullptr && callback->OnNetworkSearchCallback(
133 INetworkSearchCallback::NetworkSearchCallback::SET_RADIO_STATUS_RESULT, data)) {
134 TELEPHONY_LOGE("RadioInfo::ProcessSetRadioState callback fail slotId:%{public}d", slotId_);
135 }
136 NetworkUtils::RemoveCallbackFromMap(index);
137 } else {
138 nsm->SetLocateUpdate(slotId_);
139 }
140 if (result) {
141 RadioFirstPowerOn(nsm, radioState);
142 }
143 }
144
RadioFirstPowerOn(std::shared_ptr<NetworkSearchManager> & nsm,ModemPowerState radioState) const145 void RadioInfo::RadioFirstPowerOn(std::shared_ptr<NetworkSearchManager> &nsm, ModemPowerState radioState) const
146 {
147 TELEPHONY_LOGI(
148 "RadioInfo::RadioFirstPowerOn radioState:%{public}d, slotId:%{public}d", static_cast<int>(radioState), slotId_);
149 if (radioState != ModemPowerState::CORE_SERVICE_POWER_ON) {
150 return;
151 }
152 if (!nsm->IsRadioFirstPowerOn(slotId_)) {
153 return;
154 }
155 nsm->SetRadioFirstPowerOn(slotId_, false);
156
157 UpdatePreferredNetwork(nsm, radioState);
158 }
159
UpdatePreferredNetwork(std::shared_ptr<NetworkSearchManager> & nsm,ModemPowerState radioState) const160 void RadioInfo::UpdatePreferredNetwork(std::shared_ptr<NetworkSearchManager> &nsm, ModemPowerState radioState) const
161 {
162 int32_t networkMode = nsm->GetPreferredNetworkValue(slotId_);
163 nsm->SetPreferredNetwork(slotId_, networkMode);
164 }
165
ProcessGetImei(const AppExecFwk::InnerEvent::Pointer & event) const166 void RadioInfo::ProcessGetImei(const AppExecFwk::InnerEvent::Pointer &event) const
167 {
168 std::shared_ptr<NetworkSearchManager> nsm = networkSearchManager_.lock();
169 TELEPHONY_LOGI("RadioInfo::ProcessGetImei slotId:%{public}d", slotId_);
170 if (event == nullptr) {
171 TELEPHONY_LOGE("RadioInfo::ProcessGetImei event is nullptr slotId:%{public}d", slotId_);
172 return;
173 }
174 if (nsm == nullptr) {
175 TELEPHONY_LOGE("NetworkSelection::ProcessGetImei nsm is nullptr slotId:%{public}d", slotId_);
176 return;
177 }
178
179 std::shared_ptr<HRilStringParcel> imeiID = event->GetSharedObject<HRilStringParcel>();
180 if (imeiID == nullptr) {
181 TELEPHONY_LOGE("RadioInfo::ProcessGetImei imei is nullptr slotId:%{public}d", slotId_);
182 nsm->SetImei(slotId_, u"");
183 return;
184 }
185 TELEPHONY_LOGI("RadioInfo::ProcessGetImei get imei success");
186 nsm->SetImei(slotId_, Str8ToStr16(imeiID->data));
187 }
188
ProcessGetMeid(const AppExecFwk::InnerEvent::Pointer & event) const189 void RadioInfo::ProcessGetMeid(const AppExecFwk::InnerEvent::Pointer &event) const
190 {
191 std::shared_ptr<NetworkSearchManager> nsm = networkSearchManager_.lock();
192 TELEPHONY_LOGI("RadioInfo::ProcessGetMeid slotId:%{public}d", slotId_);
193 if (event == nullptr) {
194 TELEPHONY_LOGE("RadioInfo::ProcessGetMeid event is nullptr slotId:%{public}d", slotId_);
195 return;
196 }
197 if (nsm == nullptr) {
198 TELEPHONY_LOGE("NetworkSelection::ProcessGetMeid nsm is nullptr slotId:%{public}d", slotId_);
199 return;
200 }
201
202 std::shared_ptr<HRilStringParcel> meid = event->GetSharedObject<HRilStringParcel>();
203 if (meid == nullptr) {
204 TELEPHONY_LOGE("RadioInfo::ProcessGetMeid meid is nullptr slotId:%{public}d", slotId_);
205 nsm->SetMeid(slotId_, u"");
206 return;
207 }
208 TELEPHONY_LOGI("RadioInfo::ProcessGetMeid success");
209 nsm->SetMeid(slotId_, Str8ToStr16(meid->data));
210 }
211
SetPhoneType(PhoneType phoneType)212 void RadioInfo::SetPhoneType(PhoneType phoneType)
213 {
214 phoneType_ = phoneType;
215 }
216
GetPhoneType() const217 PhoneType RadioInfo::GetPhoneType() const
218 {
219 return phoneType_;
220 }
221
UpdatePhone(RadioTech csRadioTech,const RadioTech & psRadioTech)222 void RadioInfo::UpdatePhone(RadioTech csRadioTech, const RadioTech &psRadioTech)
223 {
224 TELEPHONY_LOGI("RadioInfo::UpdatePhone");
225 std::shared_ptr<NetworkSearchManager> networkSearchManager = networkSearchManager_.lock();
226 if (networkSearchManager == nullptr) {
227 TELEPHONY_LOGE("RadioInfo::UpdatePhone networkSearchManager is nullptr");
228 return;
229 }
230 PhoneType phoneType = RadioTechToPhoneType(csRadioTech, psRadioTech);
231 if (phoneType_ == phoneType) {
232 TELEPHONY_LOGI("RadioInfo::UpdatePhone No Change");
233 return;
234 }
235 if (phoneType == PhoneType::PHONE_TYPE_IS_NONE) {
236 TELEPHONY_LOGE("RadioInfo::UpdatePhone phoneType is UNKNOWN");
237 return;
238 }
239 TELEPHONY_LOGI("RadioInfo::UpdatePhone SetPhoneType is success %{public}d", phoneType);
240 SetPhoneType(phoneType);
241
242 int radioState = networkSearchManager->GetRadioState(slotId_);
243 if (static_cast<ModemPowerState>(radioState) != CORE_SERVICE_POWER_NOT_AVAILABLE) {
244 networkSearchManager->InitSimRadioProtocol(slotId_);
245 std::u16string meid = u"";
246 std::u16string imei = u"";
247 networkSearchManager->GetImei(slotId_, imei);
248 networkSearchManager->GetMeid(slotId_, meid);
249 if (static_cast<ModemPowerState>(radioState) == CORE_SERVICE_POWER_ON) {
250 networkSearchManager->GetVoiceTech(slotId_);
251 }
252 }
253 }
254
ProcessVoiceTechChange(const AppExecFwk::InnerEvent::Pointer & event)255 void RadioInfo::ProcessVoiceTechChange(const AppExecFwk::InnerEvent::Pointer &event)
256 {
257 if (event == nullptr) {
258 TELEPHONY_LOGE("RadioInfo::ProcessVoiceTechChange event is nullptr");
259 return;
260 }
261 std::shared_ptr<VoiceRadioTechnology> csRadioTech = event->GetSharedObject<VoiceRadioTechnology>();
262 if (csRadioTech == nullptr) {
263 TELEPHONY_LOGE("RadioInfo::ProcessVoiceTechChange csRadioTech is nullptr");
264 return;
265 }
266 std::shared_ptr<NetworkSearchManager> networkSearchManager = networkSearchManager_.lock();
267 if (networkSearchManager == nullptr) {
268 TELEPHONY_LOGE("RadioInfo::ProcessVoiceTechChange networkSearchManager is nullptr");
269 return;
270 }
271 int32_t psRadioTech = 0;
272 networkSearchManager->GetPsRadioTech(slotId_, psRadioTech);
273 UpdatePhone(static_cast<RadioTech>(csRadioTech->actType), static_cast<RadioTech>(psRadioTech));
274 }
275
RadioTechToPhoneType(RadioTech csRadioTech,const RadioTech & psRadioTech) const276 PhoneType RadioInfo::RadioTechToPhoneType(RadioTech csRadioTech, const RadioTech &psRadioTech) const
277 {
278 PhoneType phoneType = PhoneType::PHONE_TYPE_IS_NONE;
279 switch (csRadioTech) {
280 case RadioTech::RADIO_TECHNOLOGY_GSM:
281 case RadioTech::RADIO_TECHNOLOGY_WCDMA:
282 case RadioTech::RADIO_TECHNOLOGY_HSPA:
283 case RadioTech::RADIO_TECHNOLOGY_HSPAP:
284 case RadioTech::RADIO_TECHNOLOGY_TD_SCDMA:
285 case RadioTech::RADIO_TECHNOLOGY_LTE:
286 case RadioTech::RADIO_TECHNOLOGY_LTE_CA:
287 case RadioTech::RADIO_TECHNOLOGY_NR:
288 phoneType = PhoneType::PHONE_TYPE_IS_GSM;
289 break;
290 case RadioTech::RADIO_TECHNOLOGY_1XRTT:
291 case RadioTech::RADIO_TECHNOLOGY_EVDO:
292 case RadioTech::RADIO_TECHNOLOGY_EHRPD:
293 phoneType = PhoneType::PHONE_TYPE_IS_CDMA;
294 break;
295 case RadioTech::RADIO_TECHNOLOGY_UNKNOWN:
296 default:
297 if (psRadioTech == RadioTech::RADIO_TECHNOLOGY_LTE || psRadioTech == RadioTech::RADIO_TECHNOLOGY_LTE_CA ||
298 psRadioTech == RadioTech::RADIO_TECHNOLOGY_NR) {
299 phoneType = PhoneType::PHONE_TYPE_IS_GSM;
300 }
301 break;
302 }
303 return phoneType;
304 }
305
AirplaneModeChange()306 void RadioInfo::AirplaneModeChange()
307 {
308 std::shared_ptr<NetworkSearchManager> nsm = networkSearchManager_.lock();
309 if (nsm == nullptr) {
310 TELEPHONY_LOGE("networkSearchManager_ is nullptr slotId:%{public}d", slotId_);
311 return;
312 }
313 bool airplaneMode = nsm->GetAirplaneMode();
314 if (nsm->GetRadioState(slotId_) == ModemPowerState::CORE_SERVICE_POWER_OFF && airplaneMode == false) {
315 nsm->SetRadioState(slotId_, static_cast<bool>(ModemPowerState::CORE_SERVICE_POWER_ON), 0);
316 }
317 if (nsm->GetRadioState(slotId_) == ModemPowerState::CORE_SERVICE_POWER_ON && airplaneMode == true) {
318 nsm->SetRadioState(slotId_, static_cast<bool>(ModemPowerState::CORE_SERVICE_POWER_OFF), 0);
319 }
320 }
321 } // namespace Telephony
322 } // namespace OHOS
323