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 "satellite_sms_client.h"
23 #include "sms_service.h"
24 #include "sms_persist_helper.h"
25 #include "telephony_log_wrapper.h"
26 
27 namespace OHOS {
28 namespace Telephony {
SmsNetworkPolicyManager(int32_t slotId)29 SmsNetworkPolicyManager::SmsNetworkPolicyManager(int32_t slotId)
30     : TelEventHandler("SmsNetworkPolicyManager"), slotId_(slotId)
31 {}
32 
Init()33 void SmsNetworkPolicyManager::Init()
34 {
35     RegisterHandler();
36 }
37 
RegisterHandler()38 void SmsNetworkPolicyManager::RegisterHandler()
39 {
40     TELEPHONY_LOGI("SmsNetworkPolicyManager::RegisterHandler Ok.");
41     CoreManagerInner &coreInner = CoreManagerInner::GetInstance();
42     coreInner.RegisterCoreNotify(slotId_, shared_from_this(), RadioEvent::RADIO_PS_CONNECTION_ATTACHED, nullptr);
43     coreInner.RegisterCoreNotify(slotId_, shared_from_this(), RadioEvent::RADIO_PS_CONNECTION_DETACHED, nullptr);
44     coreInner.RegisterCoreNotify(slotId_, shared_from_this(), RadioEvent::RADIO_ON, nullptr);
45     coreInner.RegisterCoreNotify(slotId_, shared_from_this(), RadioEvent::RADIO_OFF, nullptr);
46     coreInner.RegisterCoreNotify(slotId_, shared_from_this(), RadioEvent::RADIO_STATE_CHANGED, nullptr);
47     coreInner.RegisterCoreNotify(slotId_, shared_from_this(), RadioEvent::RADIO_PS_RAT_CHANGED, nullptr);
48     coreInner.RegisterCoreNotify(slotId_, shared_from_this(), RadioEvent::RADIO_NETWORK_STATE, nullptr);
49     coreInner.RegisterCoreNotify(slotId_, shared_from_this(), RadioEvent::RADIO_IMS_NETWORK_STATE_CHANGED, nullptr);
50     coreInner.RegisterCoreNotify(slotId_, shared_from_this(), RadioEvent::RADIO_RIL_ADAPTER_HOST_DIED, nullptr);
51     coreInner.RegisterCoreNotify(slotId_, shared_from_this(), RadioEvent::RADIO_FACTORY_RESET, nullptr);
52     GetRadioState();
53     GetImsRegState();
54 }
55 
UnRegisterHandler()56 void SmsNetworkPolicyManager::UnRegisterHandler()
57 {
58     CoreManagerInner &coreInner = CoreManagerInner::GetInstance();
59     coreInner.UnRegisterCoreNotify(slotId_, shared_from_this(), RadioEvent::RADIO_PS_CONNECTION_ATTACHED);
60     coreInner.UnRegisterCoreNotify(slotId_, shared_from_this(), RadioEvent::RADIO_PS_CONNECTION_DETACHED);
61     coreInner.UnRegisterCoreNotify(slotId_, shared_from_this(), RadioEvent::RADIO_ON);
62     coreInner.UnRegisterCoreNotify(slotId_, shared_from_this(), RadioEvent::RADIO_OFF);
63     coreInner.UnRegisterCoreNotify(slotId_, shared_from_this(), RadioEvent::RADIO_STATE_CHANGED);
64     coreInner.UnRegisterCoreNotify(slotId_, shared_from_this(), RadioEvent::RADIO_PS_RAT_CHANGED);
65     coreInner.UnRegisterCoreNotify(slotId_, shared_from_this(), RadioEvent::RADIO_NETWORK_STATE);
66     coreInner.UnRegisterCoreNotify(slotId_, shared_from_this(), RadioEvent::RADIO_IMS_NETWORK_STATE_CHANGED);
67     coreInner.UnRegisterCoreNotify(slotId_, shared_from_this(), RadioEvent::RADIO_RIL_ADAPTER_HOST_DIED);
68     coreInner.UnRegisterCoreNotify(slotId_, shared_from_this(), RadioEvent::RADIO_FACTORY_RESET);
69     std::lock_guard<std::mutex> lock(callbackMapMutex_);
70     callbackMap_.clear();
71 }
72 
ProcessEvent(const AppExecFwk::InnerEvent::Pointer & event)73 void SmsNetworkPolicyManager::ProcessEvent(const AppExecFwk::InnerEvent::Pointer &event)
74 {
75     if (event == nullptr) {
76         TELEPHONY_LOGE("SmsNetworkPolicyManager::ProcessEvent event == nullptr");
77         return;
78     }
79 
80     uint32_t eventId = 0;
81     eventId = event->GetInnerEventId();
82     TELEPHONY_LOGD("SmsNetworkPolicyManager::ProcessEvent Handler Rec%{public}d", eventId);
83     switch (eventId) {
84         case RadioEvent::RADIO_ON:
85         case RadioEvent::RADIO_OFF:
86         case RadioEvent::RADIO_STATE_CHANGED:
87         case RadioEvent::RADIO_PS_RAT_CHANGED:
88         case RadioEvent::RADIO_NETWORK_STATE:
89         case RadioEvent::RADIO_IMS_NETWORK_STATE_CHANGED:
90         case RadioEvent::RADIO_PS_CONNECTION_DETACHED:
91         case RadioEvent::RADIO_PS_CONNECTION_ATTACHED:
92         case NotificationType::NOTIFICATION_TYPE_IMS:
93             GetRadioState();
94             break;
95         case RadioEvent::RADIO_RIL_ADAPTER_HOST_DIED:
96             DelayedSingleton<SmsService>::GetInstance()->OnRilAdapterHostDied(slotId_);
97             break;
98         case RadioEvent::RADIO_FACTORY_RESET:
99             HandleFactoryReset();
100             break;
101         default:
102             break;
103     }
104 }
105 
GetNetWorkType()106 NetWorkType SmsNetworkPolicyManager::GetNetWorkType()
107 {
108     return netWorkType_;
109 }
110 
GetRadioState()111 void SmsNetworkPolicyManager::GetRadioState()
112 {
113     bool isCTSimCard = false;
114     bool isRoaming = false;
115     CoreManagerInner::GetInstance().IsCTSimCard(slotId_, isCTSimCard);
116     sptr<NetworkState> networkState = nullptr;
117     CoreManagerInner::GetInstance().GetNetworkStatus(slotId_, networkState);
118     if (networkState != nullptr) {
119         isRoaming = networkState->IsRoaming();
120     } else {
121         TELEPHONY_LOGE("networkState get failed, slotId_: %{public}d", slotId_);
122     }
123     if (isCTSimCard && !isRoaming) {
124         netWorkType_ = NET_TYPE_CDMA;
125     } else {
126         netWorkType_ = NET_TYPE_GSM;
127     }
128 
129     auto &satelliteSmsClient = SatelliteSmsClient::GetInstance();
130     if (satelliteSmsClient.IsSatelliteEnabled()) {
131         TELEPHONY_LOGI("Satellite mode on");
132         netWorkType_ = NET_TYPE_GSM;
133     }
134     ImsRegInfo info;
135     CoreManagerInner::GetInstance().GetImsRegStatus(slotId_, ImsServiceType::TYPE_SMS, info);
136     isImsNetDomain_ = info.imsRegState == ImsRegState::IMS_REGISTERED;
137     voiceServiceState_ = CoreManagerInner::GetInstance().GetCsRegState(slotId_);
138     TELEPHONY_LOGD("netWorkType_ = %{public}d isImsNetDomain_ = %{public}s GetCsRegStatus = %{public}d", netWorkType_,
139         isImsNetDomain_ ? "true" : "false", voiceServiceState_);
140     std::lock_guard<std::mutex> lock(callbackMapMutex_);
141     for (const auto &item : callbackMap_) {
142         if (item.second == nullptr) {
143             TELEPHONY_LOGE("callbackList's item is nullptr");
144             continue;
145         }
146         TELEPHONY_LOGD("update network info.");
147         item.second(isImsNetDomain_, voiceServiceState_);
148     }
149 }
150 
GetImsRegState()151 void SmsNetworkPolicyManager::GetImsRegState()
152 {
153     ImsServiceType imsSrvType = TYPE_VOICE;
154     sptr<ImsRegInfoCallback> callback = new ImsRegStateCallbackStub(shared_from_this());
155     int32_t ret = CoreServiceClient::GetInstance().RegisterImsRegInfoCallback(slotId_, imsSrvType, callback);
156     TELEPHONY_LOGI("SmsNetworkPolicyManager::GetImsRegState ret:%{public}d", ret);
157 }
158 
IsImsNetDomain() const159 bool SmsNetworkPolicyManager::IsImsNetDomain() const
160 {
161     return isImsNetDomain_;
162 }
163 
GetVoiceServiceState() const164 int32_t SmsNetworkPolicyManager::GetVoiceServiceState() const
165 {
166     return voiceServiceState_;
167 }
168 
NetworkRegister(const std::function<void (bool isImsNetDomain,int32_t voiceServiceState)> & callback)169 std::optional<int32_t> SmsNetworkPolicyManager::NetworkRegister(
170     const std::function<void(bool isImsNetDomain, int32_t voiceServiceState)> &callback)
171 {
172     if (callback == nullptr) {
173         TELEPHONY_LOGE("NetworkRegister is failed");
174         return std::nullopt;
175     }
176     callback(isImsNetDomain_, voiceServiceState_);
177     std::lock_guard<std::mutex> lock(callbackMapMutex_);
178     callbackMap_.emplace(GetId(), callback);
179     return id_;
180 }
181 
NetworkRegister(const std::function<void (bool isImsNetDomain,int32_t voiceServiceState)> && callback)182 std::optional<int32_t>  SmsNetworkPolicyManager::NetworkRegister(
183     const std::function<void(bool isImsNetDomain, int32_t voiceServiceState)> &&callback)
184 {
185     if (callback == nullptr) {
186         TELEPHONY_LOGE("NetworkRegister is failed");
187         return std::nullopt;
188     }
189     callback(isImsNetDomain_, voiceServiceState_);
190     std::lock_guard<std::mutex> lock(callbackMapMutex_);
191     callbackMap_.emplace(GetId(), std::move(callback));
192     return id_;
193 }
194 
GetId()195 int32_t SmsNetworkPolicyManager::GetId()
196 {
197     return ++id_;
198 }
199 
NetworkUnregister(int32_t id)200 void SmsNetworkPolicyManager::NetworkUnregister(int32_t id)
201 {
202     auto iter = callbackMap_.find(id);
203     if (iter != callbackMap_.end()) {
204         std::lock_guard<std::mutex> lock(callbackMapMutex_);
205         callbackMap_.erase(iter);
206     } else {
207         TELEPHONY_LOGE("NetworkUnregister id[%{public}d] is failed", id);
208     }
209 }
210 
HandleFactoryReset()211 void SmsNetworkPolicyManager::HandleFactoryReset()
212 {
213     DataShare::DataSharePredicates predicates;
214     predicates.EqualTo(SmsSubsection::SLOT_ID, std::to_string(slotId_));
215     bool ret = DelayedSingleton<SmsPersistHelper>::GetInstance()->Delete(predicates);
216     TELEPHONY_LOGI("sms factory reset ret:%{public}d", ret);
217 }
218 } // namespace Telephony
219 } // namespace OHOS