• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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     ret = CoreServiceClient::GetInstance().RegisterImsRegInfoCallback(slotId_, TYPE_SMS, callback);
158     TELEPHONY_LOGI("register TYPE_SMS callback ret:%{public}d", ret);
159 }
160 
IsImsNetDomain() const161 bool SmsNetworkPolicyManager::IsImsNetDomain() const
162 {
163     return isImsNetDomain_;
164 }
165 
GetVoiceServiceState() const166 int32_t SmsNetworkPolicyManager::GetVoiceServiceState() const
167 {
168     return voiceServiceState_;
169 }
170 
NetworkRegister(const std::function<void (bool isImsNetDomain,int32_t voiceServiceState)> & callback)171 std::optional<int32_t> SmsNetworkPolicyManager::NetworkRegister(
172     const std::function<void(bool isImsNetDomain, int32_t voiceServiceState)> &callback)
173 {
174     if (callback == nullptr) {
175         TELEPHONY_LOGE("NetworkRegister is failed");
176         return std::nullopt;
177     }
178     callback(isImsNetDomain_, voiceServiceState_);
179     std::lock_guard<std::mutex> lock(callbackMapMutex_);
180     callbackMap_.emplace(GetId(), callback);
181     return id_;
182 }
183 
NetworkRegister(const std::function<void (bool isImsNetDomain,int32_t voiceServiceState)> && callback)184 std::optional<int32_t>  SmsNetworkPolicyManager::NetworkRegister(
185     const std::function<void(bool isImsNetDomain, int32_t voiceServiceState)> &&callback)
186 {
187     if (callback == nullptr) {
188         TELEPHONY_LOGE("NetworkRegister is failed");
189         return std::nullopt;
190     }
191     callback(isImsNetDomain_, voiceServiceState_);
192     std::lock_guard<std::mutex> lock(callbackMapMutex_);
193     callbackMap_.emplace(GetId(), std::move(callback));
194     return id_;
195 }
196 
GetId()197 int32_t SmsNetworkPolicyManager::GetId()
198 {
199     return ++id_;
200 }
201 
NetworkUnregister(int32_t id)202 void SmsNetworkPolicyManager::NetworkUnregister(int32_t id)
203 {
204     auto iter = callbackMap_.find(id);
205     if (iter != callbackMap_.end()) {
206         std::lock_guard<std::mutex> lock(callbackMapMutex_);
207         callbackMap_.erase(iter);
208     } else {
209         TELEPHONY_LOGE("NetworkUnregister id[%{public}d] is failed", id);
210     }
211 }
212 
HandleFactoryReset()213 void SmsNetworkPolicyManager::HandleFactoryReset()
214 {
215     DataShare::DataSharePredicates predicates;
216     predicates.EqualTo(SmsSubsection::SLOT_ID, std::to_string(slotId_));
217     bool ret = DelayedSingleton<SmsPersistHelper>::GetInstance()->Delete(predicates);
218     TELEPHONY_LOGI("sms factory reset ret:%{public}d", ret);
219 }
220 } // namespace Telephony
221 } // namespace OHOS