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 "sms_network_policy_manager.h"
17
18 #include "core_manager_inner.h"
19 #include "radio_event.h"
20 #include "telephony_log_wrapper.h"
21
22 namespace OHOS {
23 namespace Telephony {
SmsNetworkPolicyManager(const std::shared_ptr<AppExecFwk::EventRunner> & runner,int32_t slotId)24 SmsNetworkPolicyManager::SmsNetworkPolicyManager(
25 const std::shared_ptr<AppExecFwk::EventRunner> &runner, int32_t slotId)
26 : AppExecFwk::EventHandler(runner), slotId_(slotId)
27 {}
28
~SmsNetworkPolicyManager()29 SmsNetworkPolicyManager::~SmsNetworkPolicyManager()
30 {
31 UnRegisterHandler();
32 }
33
Init()34 void SmsNetworkPolicyManager::Init()
35 {
36 RegisterHandler();
37 }
38
RegisterHandler()39 void SmsNetworkPolicyManager::RegisterHandler()
40 {
41 TELEPHONY_LOGI("SmsNetworkPolicyManager::RegisterHandler Ok.");
42 CoreManagerInner::GetInstance().RegisterCoreNotify(
43 slotId_, shared_from_this(), RadioEvent::RADIO_PS_CONNECTION_ATTACHED, nullptr);
44 CoreManagerInner::GetInstance().RegisterCoreNotify(
45 slotId_, shared_from_this(), RadioEvent::RADIO_PS_CONNECTION_DETACHED, nullptr);
46 CoreManagerInner::GetInstance().RegisterCoreNotify(
47 slotId_, shared_from_this(), RadioEvent::RADIO_ON, nullptr);
48 CoreManagerInner::GetInstance().RegisterCoreNotify(
49 slotId_, shared_from_this(), RadioEvent::RADIO_OFF, nullptr);
50 CoreManagerInner::GetInstance().RegisterCoreNotify(
51 slotId_, shared_from_this(), RadioEvent::RADIO_STATE_CHANGED, nullptr);
52 CoreManagerInner::GetInstance().RegisterCoreNotify(
53 slotId_, shared_from_this(), RadioEvent::RADIO_PS_RAT_CHANGED, nullptr);
54 CoreManagerInner::GetInstance().RegisterCoreNotify(
55 slotId_, shared_from_this(), RadioEvent::RADIO_NETWORK_STATE, nullptr);
56 CoreManagerInner::GetInstance().RegisterCoreNotify(
57 slotId_, shared_from_this(), RadioEvent::RADIO_IMS_NETWORK_STATE_CHANGED, nullptr);
58 GetRadioState();
59 }
60
UnRegisterHandler()61 void SmsNetworkPolicyManager::UnRegisterHandler()
62 {
63 CoreManagerInner::GetInstance().UnRegisterCoreNotify(
64 slotId_, shared_from_this(), RadioEvent::RADIO_PS_CONNECTION_ATTACHED);
65 CoreManagerInner::GetInstance().UnRegisterCoreNotify(
66 slotId_, shared_from_this(), RadioEvent::RADIO_PS_CONNECTION_DETACHED);
67 CoreManagerInner::GetInstance().UnRegisterCoreNotify(slotId_, shared_from_this(), RadioEvent::RADIO_ON);
68 CoreManagerInner::GetInstance().UnRegisterCoreNotify(slotId_, shared_from_this(), RadioEvent::RADIO_OFF);
69 CoreManagerInner::GetInstance().UnRegisterCoreNotify(
70 slotId_, shared_from_this(), RadioEvent::RADIO_STATE_CHANGED);
71 CoreManagerInner::GetInstance().UnRegisterCoreNotify(
72 slotId_, shared_from_this(), RadioEvent::RADIO_PS_RAT_CHANGED);
73 CoreManagerInner::GetInstance().UnRegisterCoreNotify(
74 slotId_, shared_from_this(), RadioEvent::RADIO_NETWORK_STATE);
75 CoreManagerInner::GetInstance().UnRegisterCoreNotify(
76 slotId_, shared_from_this(), RadioEvent::RADIO_IMS_NETWORK_STATE_CHANGED);
77 callbackMap_.clear();
78 }
79
ProcessEvent(const AppExecFwk::InnerEvent::Pointer & event)80 void SmsNetworkPolicyManager::ProcessEvent(const AppExecFwk::InnerEvent::Pointer &event)
81 {
82 if (event == nullptr) {
83 TELEPHONY_LOGE("SmsNetworkPolicyManager::ProcessEvent event == nullptr");
84 return;
85 }
86
87 int eventId = 0;
88 eventId = event->GetInnerEventId();
89 TELEPHONY_LOGI("SmsNetworkPolicyManager::ProcessEvent Handler Rec%{public}d", eventId);
90 switch (eventId) {
91 case RadioEvent::RADIO_ON:
92 case RadioEvent::RADIO_OFF:
93 case RadioEvent::RADIO_STATE_CHANGED:
94 case RadioEvent::RADIO_PS_RAT_CHANGED:
95 case RadioEvent::RADIO_NETWORK_STATE:
96 case RadioEvent::RADIO_IMS_NETWORK_STATE_CHANGED:
97 case RadioEvent::RADIO_PS_CONNECTION_DETACHED:
98 case RadioEvent::RADIO_PS_CONNECTION_ATTACHED:
99 GetRadioState();
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 netWorkType_ = static_cast<NetWorkType>(CoreManagerInner::GetInstance().GetPhoneType(slotId_));
114 isImsNetDomain_ = CoreManagerInner::GetInstance().GetImsRegStatus(slotId_);
115 voiceServiceState_ = CoreManagerInner::GetInstance().GetCsRegState(slotId_);
116 TELEPHONY_LOGI("netWorkType_ = %{public}d isImsNetDomain_ = %{public}s GetCsRegStatus = %{public}d",
117 netWorkType_, isImsNetDomain_ ? "true" : "false", voiceServiceState_);
118 for (const auto &item : callbackMap_) {
119 if (item.second == nullptr) {
120 TELEPHONY_LOGE("callbackList's item is nullptr");
121 continue;
122 }
123 TELEPHONY_LOGI("update network info.");
124 item.second(isImsNetDomain_, voiceServiceState_);
125 }
126 }
127
IsImsNetDomain() const128 bool SmsNetworkPolicyManager::IsImsNetDomain() const
129 {
130 return isImsNetDomain_;
131 }
132
GetVoiceServiceState() const133 int32_t SmsNetworkPolicyManager::GetVoiceServiceState() const
134 {
135 return voiceServiceState_;
136 }
137
NetworkRegister(const std::function<void (bool isImsNetDomain,int32_t voiceServiceState)> & callback)138 std::optional<int32_t> SmsNetworkPolicyManager::NetworkRegister(
139 const std::function<void(bool isImsNetDomain, int32_t voiceServiceState)> &callback)
140 {
141 if (callback == nullptr) {
142 TELEPHONY_LOGE("NetworkRegister is failed");
143 return std::nullopt;
144 }
145 callback(isImsNetDomain_, voiceServiceState_);
146 callbackMap_.emplace(GetId(), callback);
147 return id_;
148 }
149
NetworkRegister(const std::function<void (bool isImsNetDomain,int32_t voiceServiceState)> && callback)150 std::optional<int32_t> SmsNetworkPolicyManager::NetworkRegister(
151 const std::function<void(bool isImsNetDomain, int32_t voiceServiceState)> &&callback)
152 {
153 if (callback == nullptr) {
154 TELEPHONY_LOGE("NetworkRegister is failed");
155 return std::nullopt;
156 }
157 callback(isImsNetDomain_, voiceServiceState_);
158 callbackMap_.emplace(GetId(), std::move(callback));
159 return id_;
160 }
161
GetId()162 int32_t SmsNetworkPolicyManager::GetId()
163 {
164 return ++id_;
165 }
166
NetworkUnregister(int32_t id)167 void SmsNetworkPolicyManager::NetworkUnregister(int32_t id)
168 {
169 auto iter = callbackMap_.find(id);
170 if (iter != callbackMap_.end()) {
171 callbackMap_.erase(iter);
172 } else {
173 TELEPHONY_LOGE("NetworkUnregister id[%{public}d] is failed", id);
174 }
175 }
176 } // namespace Telephony
177 } // namespace OHOS