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