1 /*
2 * Copyright (C) 2021-2022 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 "sms_network_policy_manager.h"
17
18 #include "core_manager_inner.h"
19 #include "core_service_client.h"
20 #include "ims_reg_info_callback_stub.h"
21 #include "radio_event.h"
22 #include "telephony_log_wrapper.h"
23
24 namespace OHOS {
25 namespace Telephony {
26 const std::vector<std::string> CT_ICCID_ARRAY = { "898603", "898606", "898611", "8985302", "8985307" };
27 const std::vector<std::string> CT_CPLMNS = { "46003", "46005", "46011", "46012", "47008", "45002", "45007" };
28 constexpr int32_t ICCID_LEN_MINIMUM = 7;
29 constexpr int32_t PREFIX_LOCAL_ICCID_LEN = 4;
30 constexpr const char *PREFIX_LOCAL_ICCID = "8986";
31 constexpr int32_t ICCID_LEN_SIX = 6;
32
SmsNetworkPolicyManager(const std::shared_ptr<AppExecFwk::EventRunner> & runner,int32_t slotId)33 SmsNetworkPolicyManager::SmsNetworkPolicyManager(
34 const std::shared_ptr<AppExecFwk::EventRunner> &runner, int32_t slotId)
35 : AppExecFwk::EventHandler(runner), slotId_(slotId)
36 {}
37
Init()38 void SmsNetworkPolicyManager::Init()
39 {
40 RegisterHandler();
41 }
42
RegisterHandler()43 void SmsNetworkPolicyManager::RegisterHandler()
44 {
45 TELEPHONY_LOGI("SmsNetworkPolicyManager::RegisterHandler Ok.");
46 CoreManagerInner::GetInstance().RegisterCoreNotify(
47 slotId_, shared_from_this(), RadioEvent::RADIO_PS_CONNECTION_ATTACHED, nullptr);
48 CoreManagerInner::GetInstance().RegisterCoreNotify(
49 slotId_, shared_from_this(), RadioEvent::RADIO_PS_CONNECTION_DETACHED, nullptr);
50 CoreManagerInner::GetInstance().RegisterCoreNotify(
51 slotId_, shared_from_this(), RadioEvent::RADIO_ON, nullptr);
52 CoreManagerInner::GetInstance().RegisterCoreNotify(
53 slotId_, shared_from_this(), RadioEvent::RADIO_OFF, nullptr);
54 CoreManagerInner::GetInstance().RegisterCoreNotify(
55 slotId_, shared_from_this(), RadioEvent::RADIO_STATE_CHANGED, nullptr);
56 CoreManagerInner::GetInstance().RegisterCoreNotify(
57 slotId_, shared_from_this(), RadioEvent::RADIO_PS_RAT_CHANGED, nullptr);
58 CoreManagerInner::GetInstance().RegisterCoreNotify(
59 slotId_, shared_from_this(), RadioEvent::RADIO_NETWORK_STATE, nullptr);
60 CoreManagerInner::GetInstance().RegisterCoreNotify(
61 slotId_, shared_from_this(), RadioEvent::RADIO_IMS_NETWORK_STATE_CHANGED, nullptr);
62 GetRadioState();
63 GetImsRegState();
64 }
65
UnRegisterHandler()66 void SmsNetworkPolicyManager::UnRegisterHandler()
67 {
68 CoreManagerInner::GetInstance().UnRegisterCoreNotify(
69 slotId_, shared_from_this(), RadioEvent::RADIO_PS_CONNECTION_ATTACHED);
70 CoreManagerInner::GetInstance().UnRegisterCoreNotify(
71 slotId_, shared_from_this(), RadioEvent::RADIO_PS_CONNECTION_DETACHED);
72 CoreManagerInner::GetInstance().UnRegisterCoreNotify(slotId_, shared_from_this(), RadioEvent::RADIO_ON);
73 CoreManagerInner::GetInstance().UnRegisterCoreNotify(slotId_, shared_from_this(), RadioEvent::RADIO_OFF);
74 CoreManagerInner::GetInstance().UnRegisterCoreNotify(
75 slotId_, shared_from_this(), RadioEvent::RADIO_STATE_CHANGED);
76 CoreManagerInner::GetInstance().UnRegisterCoreNotify(
77 slotId_, shared_from_this(), RadioEvent::RADIO_PS_RAT_CHANGED);
78 CoreManagerInner::GetInstance().UnRegisterCoreNotify(
79 slotId_, shared_from_this(), RadioEvent::RADIO_NETWORK_STATE);
80 CoreManagerInner::GetInstance().UnRegisterCoreNotify(
81 slotId_, shared_from_this(), RadioEvent::RADIO_IMS_NETWORK_STATE_CHANGED);
82 callbackMap_.clear();
83 }
84
ProcessEvent(const AppExecFwk::InnerEvent::Pointer & event)85 void SmsNetworkPolicyManager::ProcessEvent(const AppExecFwk::InnerEvent::Pointer &event)
86 {
87 if (event == nullptr) {
88 TELEPHONY_LOGE("SmsNetworkPolicyManager::ProcessEvent event == nullptr");
89 return;
90 }
91
92 uint32_t eventId = 0;
93 eventId = event->GetInnerEventId();
94 TELEPHONY_LOGI("SmsNetworkPolicyManager::ProcessEvent Handler Rec%{public}d", eventId);
95 switch (eventId) {
96 case RadioEvent::RADIO_ON:
97 case RadioEvent::RADIO_OFF:
98 case RadioEvent::RADIO_STATE_CHANGED:
99 case RadioEvent::RADIO_PS_RAT_CHANGED:
100 case RadioEvent::RADIO_NETWORK_STATE:
101 case RadioEvent::RADIO_IMS_NETWORK_STATE_CHANGED:
102 case RadioEvent::RADIO_PS_CONNECTION_DETACHED:
103 case RadioEvent::RADIO_PS_CONNECTION_ATTACHED:
104 case NotificationType::NOTIFICATION_TYPE_IMS:
105 GetRadioState();
106 break;
107 default:
108 break;
109 }
110 }
111
GetNetWorkType()112 NetWorkType SmsNetworkPolicyManager::GetNetWorkType()
113 {
114 return netWorkType_;
115 }
116
IsCtSimCard()117 bool SmsNetworkPolicyManager::IsCtSimCard()
118 {
119 CardType cardType = CardType::UNKNOWN_CARD;
120 CoreManagerInner::GetInstance().GetCardType(slotId_, cardType);
121 bool isCTCardType = false;
122 bool result = false;
123 TELEPHONY_LOGD("[slot%{public}d] cardType = %{public}d", slotId_, cardType);
124 switch (cardType) {
125 case CardType::SINGLE_MODE_USIM_CARD:
126 case CardType::CT_NATIONAL_ROAMING_CARD:
127 case CardType::DUAL_MODE_TELECOM_LTE_CARD:
128 isCTCardType = true;
129 break;
130 default:
131 isCTCardType = false;
132 break;
133 }
134 if (isCTCardType) {
135 std::u16string tempIccId;
136 CoreManagerInner::GetInstance().GetSimIccId(slotId_, tempIccId);
137 std::string iccid = Str16ToStr8(tempIccId);
138 if (!iccid.empty() && iccid.length() >= ICCID_LEN_MINIMUM) {
139 std::string subIccId = iccid.substr(0, ICCID_LEN_MINIMUM);
140 if (!subIccId.empty() && (subIccId.compare(0, PREFIX_LOCAL_ICCID_LEN, PREFIX_LOCAL_ICCID) == 0) &&
141 subIccId.length() >= ICCID_LEN_MINIMUM) {
142 subIccId = subIccId.substr(0, ICCID_LEN_SIX);
143 }
144 auto iccIdRet = find(CT_ICCID_ARRAY.begin(), CT_ICCID_ARRAY.end(), subIccId);
145 result = iccIdRet != CT_ICCID_ARRAY.end();
146 } else {
147 result = false;
148 }
149 } else {
150 result = isCTCardType;
151 }
152 if (!result) {
153 std::u16string operatorNumeric;
154 CoreManagerInner::GetInstance().GetSimOperatorNumeric(slotId_, operatorNumeric);
155 std::string cplmn = Str16ToStr8(operatorNumeric);
156 if (!cplmn.empty()) {
157 auto cplmnRet = find(CT_CPLMNS.begin(), CT_CPLMNS.end(), cplmn);
158 result = cplmnRet != CT_CPLMNS.end();
159 }
160 }
161 TELEPHONY_LOGD("[slot%{public}d] result = %{public}d", slotId_, result);
162 return result;
163 }
164
GetRadioState()165 void SmsNetworkPolicyManager::GetRadioState()
166 {
167 if (IsCtSimCard()) {
168 netWorkType_ = NET_TYPE_CDMA;
169 } else {
170 netWorkType_ = NET_TYPE_GSM;
171 }
172
173 ImsRegInfo info;
174 CoreManagerInner::GetInstance().GetImsRegStatus(slotId_, ImsServiceType::TYPE_SMS, info);
175 isImsNetDomain_ = info.imsRegState == ImsRegState::IMS_REGISTERED;
176 voiceServiceState_ = CoreManagerInner::GetInstance().GetCsRegState(slotId_);
177 TELEPHONY_LOGD("netWorkType_ = %{public}d isImsNetDomain_ = %{public}s GetCsRegStatus = %{public}d", netWorkType_,
178 isImsNetDomain_ ? "true" : "false", voiceServiceState_);
179 for (const auto &item : callbackMap_) {
180 if (item.second == nullptr) {
181 TELEPHONY_LOGE("callbackList's item is nullptr");
182 continue;
183 }
184 TELEPHONY_LOGD("update network info.");
185 item.second(isImsNetDomain_, voiceServiceState_);
186 }
187 }
188
GetImsRegState()189 void SmsNetworkPolicyManager::GetImsRegState()
190 {
191 ImsServiceType imsSrvType = TYPE_VOICE;
192 callback_ = (std::make_unique<ImsRegStateCallbackStub>(shared_from_this())).release();
193 int32_t ret = CoreServiceClient::GetInstance().RegisterImsRegInfoCallback(slotId_, imsSrvType, callback_);
194 TELEPHONY_LOGI("SmsNetworkPolicyManager::GetImsRegState ret:%{public}d", ret);
195 }
196
IsImsNetDomain() const197 bool SmsNetworkPolicyManager::IsImsNetDomain() const
198 {
199 return isImsNetDomain_;
200 }
201
GetVoiceServiceState() const202 int32_t SmsNetworkPolicyManager::GetVoiceServiceState() const
203 {
204 return voiceServiceState_;
205 }
206
NetworkRegister(const std::function<void (bool isImsNetDomain,int32_t voiceServiceState)> & callback)207 std::optional<int32_t> SmsNetworkPolicyManager::NetworkRegister(
208 const std::function<void(bool isImsNetDomain, int32_t voiceServiceState)> &callback)
209 {
210 if (callback == nullptr) {
211 TELEPHONY_LOGE("NetworkRegister is failed");
212 return std::nullopt;
213 }
214 callback(isImsNetDomain_, voiceServiceState_);
215 callbackMap_.emplace(GetId(), callback);
216 return id_;
217 }
218
NetworkRegister(const std::function<void (bool isImsNetDomain,int32_t voiceServiceState)> && callback)219 std::optional<int32_t> SmsNetworkPolicyManager::NetworkRegister(
220 const std::function<void(bool isImsNetDomain, int32_t voiceServiceState)> &&callback)
221 {
222 if (callback == nullptr) {
223 TELEPHONY_LOGE("NetworkRegister is failed");
224 return std::nullopt;
225 }
226 callback(isImsNetDomain_, voiceServiceState_);
227 callbackMap_.emplace(GetId(), std::move(callback));
228 return id_;
229 }
230
GetId()231 int32_t SmsNetworkPolicyManager::GetId()
232 {
233 return ++id_;
234 }
235
NetworkUnregister(int32_t id)236 void SmsNetworkPolicyManager::NetworkUnregister(int32_t id)
237 {
238 auto iter = callbackMap_.find(id);
239 if (iter != callbackMap_.end()) {
240 callbackMap_.erase(iter);
241 } else {
242 TELEPHONY_LOGE("NetworkUnregister id[%{public}d] is failed", id);
243 }
244 }
245 } // namespace Telephony
246 } // namespace OHOS