1 /*
2 * Copyright (C) 2023 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 "satellite_service_client.h"
17
18 #include "if_system_ability_manager.h"
19 #include "iservice_registry.h"
20 #include "satellite_service_proxy.h"
21 #include "telephony_errors.h"
22 #include "telephony_log_wrapper.h"
23
24 namespace OHOS {
25 namespace Telephony {
SatelliteServiceClient()26 SatelliteServiceClient::SatelliteServiceClient()
27 {
28 statusChangeListener_ = new (std::nothrow) SystemAbilityListener();
29 if (statusChangeListener_ == nullptr) {
30 TELEPHONY_LOGE("Init, failed to create statusChangeListener.");
31 return;
32 }
33 auto managerPtr = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
34 if (managerPtr == nullptr) {
35 TELEPHONY_LOGE("Init, get system ability manager error.");
36 return;
37 }
38 int32_t ret = managerPtr->SubscribeSystemAbility(TELEPHONY_SATELLITE_SERVICE_ABILITY_ID, statusChangeListener_);
39 if (ret) {
40 TELEPHONY_LOGE("Init, failed to subscribe sa:%{public}d", TELEPHONY_SATELLITE_SERVICE_ABILITY_ID);
41 return;
42 }
43 }
44
~SatelliteServiceClient()45 SatelliteServiceClient::~SatelliteServiceClient()
46 {
47 RemoveDeathRecipient(nullptr, false);
48 }
49
GetProxy()50 sptr<ISatelliteService> SatelliteServiceClient::GetProxy()
51 {
52 std::lock_guard<std::mutex> lock(mutexProxy_);
53 if (proxy_ != nullptr) {
54 return proxy_;
55 }
56
57 sptr<ISystemAbilityManager> sam = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
58 if (sam == nullptr) {
59 TELEPHONY_LOGE("Failed to get system ability manager");
60 return nullptr;
61 }
62 sptr<IRemoteObject> obj = sam->CheckSystemAbility(TELEPHONY_SATELLITE_SERVICE_ABILITY_ID);
63 if (obj == nullptr) {
64 TELEPHONY_LOGE("Failed to get satellite service");
65 return nullptr;
66 }
67 std::unique_ptr<SatelliteServiceDeathRecipient> recipient = std::make_unique<SatelliteServiceDeathRecipient>(*this);
68 if (recipient == nullptr) {
69 TELEPHONY_LOGE("recipient is null");
70 return nullptr;
71 }
72 sptr<IRemoteObject::DeathRecipient> dr(recipient.release());
73 if ((obj->IsProxyObject()) && (!obj->AddDeathRecipient(dr))) {
74 TELEPHONY_LOGE("Failed to add death recipient");
75 return nullptr;
76 }
77 proxy_ = iface_cast<ISatelliteService>(obj);
78 deathRecipient_ = dr;
79 TELEPHONY_LOGD("Succeed to connect satellite service %{public}d", proxy_ == nullptr);
80 return proxy_;
81 }
82
OnRemoteDied(const wptr<IRemoteObject> & remote)83 void SatelliteServiceClient::OnRemoteDied(const wptr<IRemoteObject> &remote)
84 {
85 RemoveDeathRecipient(remote, true);
86 }
87
RemoveDeathRecipient(const wptr<IRemoteObject> & remote,bool isRemoteDied)88 void SatelliteServiceClient::RemoveDeathRecipient(const wptr<IRemoteObject> &remote, bool isRemoteDied)
89 {
90 if (isRemoteDied && remote == nullptr) {
91 TELEPHONY_LOGE("Remote died, remote is nullptr");
92 return;
93 }
94 std::lock_guard<std::mutex> lock(mutexProxy_);
95 if (proxy_ == nullptr) {
96 TELEPHONY_LOGE("proxy_ is nullptr");
97 return;
98 }
99 auto serviceRemote = proxy_->AsObject();
100 if (serviceRemote == nullptr) {
101 TELEPHONY_LOGE("serviceRemote is nullptr");
102 return;
103 }
104 if (isRemoteDied && serviceRemote != remote.promote()) {
105 TELEPHONY_LOGE("Remote died serviceRemote is not same");
106 return;
107 }
108 serviceRemote->RemoveDeathRecipient(deathRecipient_);
109 proxy_ = nullptr;
110 TELEPHONY_LOGI("RemoveDeathRecipient success");
111 }
112
OnAddSystemAbility(int32_t systemAbilityId,const std::string & deviceId)113 void SatelliteServiceClient::SystemAbilityListener::OnAddSystemAbility(
114 int32_t systemAbilityId, const std::string &deviceId)
115 {
116 TELEPHONY_LOGI("SA:%{public}d is added!", systemAbilityId);
117 if (!CheckInputSysAbilityId(systemAbilityId)) {
118 TELEPHONY_LOGE("add SA:%{public}d is invalid!", systemAbilityId);
119 return;
120 }
121
122 std::shared_ptr<SatelliteServiceClient> satelliteClient = DelayedSingleton<SatelliteServiceClient>::GetInstance();
123 satelliteClient->ServiceOn();
124 TELEPHONY_LOGI("SA:%{public}d reconnect service successfully!", systemAbilityId);
125 }
126
OnRemoveSystemAbility(int32_t systemAbilityId,const std::string & deviceId)127 void SatelliteServiceClient::SystemAbilityListener::OnRemoveSystemAbility(
128 int32_t systemAbilityId, const std::string &deviceId)
129 {
130 TELEPHONY_LOGI("SA:%{public}d is removed!", systemAbilityId);
131 std::shared_ptr<SatelliteServiceClient> satelliteClient = DelayedSingleton<SatelliteServiceClient>::GetInstance();
132 satelliteClient->ServiceOff();
133 }
134
AddSimHandler(int32_t slotId,const std::shared_ptr<TelEventHandler> & handler)135 int32_t SatelliteServiceClient::AddSimHandler(int32_t slotId, const std::shared_ptr<TelEventHandler> &handler)
136 {
137 if (handler == nullptr) {
138 TELEPHONY_LOGE("AddSimHandler return, handler is null.");
139 return TELEPHONY_ERR_LOCAL_PTR_NULL;
140 }
141
142 simHandlerMap_.insert(std::make_pair(slotId, handler));
143 TELEPHONY_LOGI("AddSimHandler success: %{public}d", slotId);
144 return TELEPHONY_SUCCESS;
145 }
146
AddNetworkHandler(int32_t slotId,const std::shared_ptr<TelEventHandler> & handler)147 int32_t SatelliteServiceClient::AddNetworkHandler(int32_t slotId, const std::shared_ptr<TelEventHandler> &handler)
148 {
149 if (handler == nullptr) {
150 TELEPHONY_LOGE("AddNetworkHandler return, handler is null.");
151 return TELEPHONY_ERR_LOCAL_PTR_NULL;
152 }
153
154 networkHandlerMap_.insert(std::make_pair(slotId, handler));
155 TELEPHONY_LOGI("AddNetworkHandler success: %{public}d", slotId);
156 return TELEPHONY_SUCCESS;
157 }
158
ServiceOn()159 void SatelliteServiceClient::ServiceOn()
160 {
161 for (auto pair : simHandlerMap_) {
162 auto handler = static_cast<SimStateHandle *>(pair.second.get());
163 if (handler == nullptr) {
164 TELEPHONY_LOGE("SimStateHandle is null: %{public}d", pair.first);
165 continue;
166 }
167 handler->RegisterSatelliteCallback();
168 }
169 for (auto pair : networkHandlerMap_) {
170 auto handler = static_cast<NetworkSearchHandler *>(pair.second.get());
171 if (handler == nullptr) {
172 TELEPHONY_LOGE("NetworkSearchHandler is null: %{public}d", pair.first);
173 continue;
174 }
175 handler->RegisterSatelliteCallback();
176 }
177 }
178
ServiceOff()179 void SatelliteServiceClient::ServiceOff()
180 {
181 std::lock_guard<std::mutex> lock(mutexProxy_);
182 proxy_ = nullptr;
183
184 for (auto pair : simHandlerMap_) {
185 auto handler = static_cast<SimStateHandle *>(pair.second.get());
186 if (handler == nullptr) {
187 TELEPHONY_LOGE("SimStateHandle is null: %{public}d", pair.first);
188 continue;
189 }
190 handler->UnregisterSatelliteCallback();
191 }
192 for (auto pair : networkHandlerMap_) {
193 auto handler = static_cast<NetworkSearchHandler *>(pair.second.get());
194 if (handler == nullptr) {
195 TELEPHONY_LOGE("NetworkSearchHandler is null: %{public}d", pair.first);
196 continue;
197 }
198 handler->UnregisterSatelliteCallback();
199 }
200 }
201
RegisterCoreNotify(int32_t slotId,int32_t what,const sptr<ISatelliteCoreCallback> & callback)202 int32_t SatelliteServiceClient::RegisterCoreNotify(
203 int32_t slotId, int32_t what, const sptr<ISatelliteCoreCallback> &callback)
204 {
205 auto proxy = GetProxy();
206 if (proxy == nullptr) {
207 TELEPHONY_LOGE("proxy is null!");
208 return TELEPHONY_ERR_IPC_CONNECT_STUB_FAIL;
209 }
210 return proxy->RegisterCoreNotify(slotId, what, callback);
211 }
212
UnRegisterCoreNotify(int32_t slotId,int32_t what)213 int32_t SatelliteServiceClient::UnRegisterCoreNotify(int32_t slotId, int32_t what)
214 {
215 auto proxy = GetProxy();
216 if (proxy == nullptr) {
217 TELEPHONY_LOGE("proxy is null!");
218 return TELEPHONY_ERR_IPC_CONNECT_STUB_FAIL;
219 }
220 return proxy->UnRegisterCoreNotify(slotId, what);
221 }
222
IsSatelliteEnabled()223 bool SatelliteServiceClient::IsSatelliteEnabled()
224 {
225 auto proxy = GetProxy();
226 if (proxy == nullptr) {
227 TELEPHONY_LOGE("proxy is null!");
228 return false;
229 }
230 return proxy->IsSatelliteEnabled();
231 }
232
GetSatelliteCapability()233 int32_t SatelliteServiceClient::GetSatelliteCapability()
234 {
235 auto proxy = GetProxy();
236 if (proxy == nullptr) {
237 TELEPHONY_LOGE("proxy is null!");
238 return static_cast<int32_t>(SatelliteCapability::NONE);
239 }
240 return proxy->GetSatelliteCapability();
241 }
242
GetProxyObjectPtr(SatelliteServiceProxyType type)243 sptr<IRemoteObject> SatelliteServiceClient::GetProxyObjectPtr(SatelliteServiceProxyType type)
244 {
245 auto proxy = GetProxy();
246 if (proxy == nullptr) {
247 TELEPHONY_LOGE("proxy is null!");
248 return nullptr;
249 }
250 return proxy->GetProxyObjectPtr(type);
251 }
252
SetRadioState(int32_t slotId,int32_t isRadioOn,int32_t rst)253 int32_t SatelliteServiceClient::SetRadioState(int32_t slotId, int32_t isRadioOn, int32_t rst)
254 {
255 auto proxy = GetProxy();
256 if (proxy == nullptr) {
257 TELEPHONY_LOGE("proxy is null!");
258 return TELEPHONY_ERR_IPC_CONNECT_STUB_FAIL;
259 }
260 return proxy->SetRadioState(slotId, isRadioOn, rst);
261 }
262
GetImei()263 std::string SatelliteServiceClient::GetImei()
264 {
265 auto proxy = GetProxy();
266 if (proxy == nullptr) {
267 TELEPHONY_LOGE("proxy is null!");
268 return "";
269 }
270 return proxy->GetImei();
271 }
272 } // namespace Telephony
273 } // namespace OHOS
274