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_sms_client.h"
17 
18 #include "if_system_ability_manager.h"
19 #include "iservice_registry.h"
20 #include "satellite_sms_proxy.h"
21 #include "telephony_errors.h"
22 #include "telephony_log_wrapper.h"
23 
24 namespace OHOS {
25 namespace Telephony {
SatelliteSmsClient()26 SatelliteSmsClient::SatelliteSmsClient()
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     }
42 }
43 
~SatelliteSmsClient()44 SatelliteSmsClient::~SatelliteSmsClient()
45 {
46     RemoveDeathRecipient(nullptr, false);
47 }
48 
GetServiceProxy()49 sptr<ISatelliteService> SatelliteSmsClient::GetServiceProxy()
50 {
51     std::lock_guard<std::mutex> lock(mutexProxy_);
52     if (satelliteServiceProxy_ != nullptr) {
53         return satelliteServiceProxy_;
54     }
55 
56     sptr<ISystemAbilityManager> sam = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
57     if (sam == nullptr) {
58         TELEPHONY_LOGE("Failed to get system ability manager");
59         return nullptr;
60     }
61     sptr<IRemoteObject> obj = sam->CheckSystemAbility(TELEPHONY_SATELLITE_SERVICE_ABILITY_ID);
62     if (obj == nullptr) {
63         return nullptr;
64     }
65     std::unique_ptr<SatelliteServiceDeathRecipient> recipient = std::make_unique<SatelliteServiceDeathRecipient>(*this);
66     if (recipient == nullptr) {
67         TELEPHONY_LOGE("recipient is null");
68         return nullptr;
69     }
70     sptr<IRemoteObject::DeathRecipient> dr(recipient.release());
71     if ((obj->IsProxyObject()) && (!obj->AddDeathRecipient(dr))) {
72         TELEPHONY_LOGE("Failed to add death recipient");
73         return nullptr;
74     }
75     satelliteServiceProxy_ = iface_cast<ISatelliteService>(obj);
76 
77     deathRecipient_ = dr;
78     TELEPHONY_LOGD("Succeed to connect satellite service %{public}d", satelliteServiceProxy_ == nullptr);
79     return satelliteServiceProxy_;
80 }
81 
GetProxy()82 sptr<ISatelliteSmsService> SatelliteSmsClient::GetProxy()
83 {
84     auto satelliteServiceProxy = GetServiceProxy();
85 
86     std::lock_guard<std::mutex> lock(mutexProxy_);
87     if (proxy_ != nullptr) {
88         return proxy_;
89     }
90 
91     if (satelliteServiceProxy == nullptr) {
92         TELEPHONY_LOGE("GetProxy: satellite service is null or destroyed");
93         return nullptr;
94     }
95 
96     sptr<IRemoteObject> smsObj = satelliteServiceProxy->GetProxyObjectPtr(PROXY_SATELLITE_SMS);
97     if (smsObj == nullptr) {
98         TELEPHONY_LOGE("satellite sms service is null");
99         return nullptr;
100     }
101     proxy_ = iface_cast<ISatelliteSmsService>(smsObj);
102 
103     TELEPHONY_LOGD("Succeed to get satellite sms service %{public}d", proxy_ == nullptr);
104     return proxy_;
105 }
106 
OnRemoteDied(const wptr<IRemoteObject> & remote)107 void SatelliteSmsClient::OnRemoteDied(const wptr<IRemoteObject> &remote)
108 {
109     RemoveDeathRecipient(remote, true);
110 }
111 
RemoveDeathRecipient(const wptr<IRemoteObject> & remote,bool isRemoteDied)112 void SatelliteSmsClient::RemoveDeathRecipient(const wptr<IRemoteObject> &remote, bool isRemoteDied)
113 {
114     if (isRemoteDied && remote == nullptr) {
115         TELEPHONY_LOGE("Remote died, remote is nullptr");
116         return;
117     }
118     std::lock_guard<std::mutex> lock(mutexProxy_);
119     if (satelliteServiceProxy_ == nullptr) {
120         TELEPHONY_LOGE("satelliteServiceProxy_ is nullptr");
121         return;
122     }
123     auto serviceRemote = satelliteServiceProxy_->AsObject();
124     if (serviceRemote == nullptr) {
125         TELEPHONY_LOGE("serviceRemote is nullptr");
126         return;
127     }
128     if (isRemoteDied && serviceRemote != remote.promote()) {
129         TELEPHONY_LOGE("Remote died serviceRemote is not same");
130         return;
131     }
132     serviceRemote->RemoveDeathRecipient(deathRecipient_);
133     satelliteServiceProxy_ = nullptr;
134     proxy_ = nullptr;
135     TELEPHONY_LOGI("SatelliteSmsClient:RemoveDeathRecipient success");
136 }
137 
OnAddSystemAbility(int32_t systemAbilityId,const std::string & deviceId)138 void SatelliteSmsClient::SystemAbilityListener::OnAddSystemAbility(int32_t systemAbilityId, const std::string &deviceId)
139 {
140     TELEPHONY_LOGI("SA:%{public}d is added!", systemAbilityId);
141     if (!CheckInputSysAbilityId(systemAbilityId)) {
142         TELEPHONY_LOGE("add SA:%{public}d is invalid!", systemAbilityId);
143         return;
144     }
145 
146     auto &satelliteSmsClient = SatelliteSmsClient::GetInstance();
147     satelliteSmsClient.ServiceOn();
148     TELEPHONY_LOGI("SA:%{public}d reconnect service successfully!", systemAbilityId);
149 }
150 
OnRemoveSystemAbility(int32_t systemAbilityId,const std::string & deviceId)151 void SatelliteSmsClient::SystemAbilityListener::OnRemoveSystemAbility(
152     int32_t systemAbilityId, const std::string &deviceId)
153 {
154     TELEPHONY_LOGI("SA:%{public}d is removed!", systemAbilityId);
155     auto &satelliteSmsClient = SatelliteSmsClient::GetInstance();
156     satelliteSmsClient.ServiceOff();
157 }
158 
AddSendHandler(int32_t slotId,const std::shared_ptr<TelEventHandler> sender)159 int32_t SatelliteSmsClient::AddSendHandler(int32_t slotId, const std::shared_ptr<TelEventHandler> sender)
160 {
161     if (sender == nullptr) {
162         TELEPHONY_LOGE("AddSendHandler return, sender is null.");
163         return TELEPHONY_ERR_LOCAL_PTR_NULL;
164     }
165     std::lock_guard<std::mutex> lock(mutexMap_);
166     senderMap_.insert(std::make_pair(slotId, sender));
167     TELEPHONY_LOGI("AddSendHandler success: %{public}d", slotId);
168     return TELEPHONY_SUCCESS;
169 }
170 
AddReceiveHandler(int32_t slotId,const std::shared_ptr<TelEventHandler> receiver)171 int32_t SatelliteSmsClient::AddReceiveHandler(int32_t slotId, const std::shared_ptr<TelEventHandler> receiver)
172 {
173     if (receiver == nullptr) {
174         TELEPHONY_LOGE("AddReceiveHandler return, receiver is null.");
175         return TELEPHONY_ERR_LOCAL_PTR_NULL;
176     }
177     std::lock_guard<std::mutex> lock(mutexMap_);
178     receiverMap_.insert(std::make_pair(slotId, receiver));
179     TELEPHONY_LOGI("AddReceiveHandler success: %{public}d", slotId);
180     return TELEPHONY_SUCCESS;
181 }
182 
ServiceOn()183 void SatelliteSmsClient::ServiceOn()
184 {
185     std::lock_guard<std::mutex> lock(mutexMap_);
186     for (auto pair : senderMap_) {
187         auto handler = static_cast<GsmSmsSender *>(pair.second.get());
188         if (handler == nullptr) {
189             TELEPHONY_LOGE("SenderHandler is null: %{public}d", pair.first);
190             continue;
191         }
192         handler->RegisterSatelliteCallback();
193     }
194     for (auto pair : receiverMap_) {
195         auto handler = static_cast<GsmSmsReceiveHandler *>(pair.second.get());
196         if (handler == nullptr) {
197             TELEPHONY_LOGE("ReceiveHandler is null: %{public}d", pair.first);
198             continue;
199         }
200         handler->RegisterSatelliteCallback();
201     }
202 }
203 
ServiceOff()204 void SatelliteSmsClient::ServiceOff()
205 {
206     std::lock(mutexProxy_, mutexMap_);
207     satelliteServiceProxy_ = nullptr;
208     proxy_ = nullptr;
209     mutexProxy_.unlock();
210     for (auto pair : senderMap_) {
211         auto handler = static_cast<GsmSmsSender *>(pair.second.get());
212         if (handler == nullptr) {
213             TELEPHONY_LOGE("SenderHandler is null: %{public}d", pair.first);
214             continue;
215         }
216         handler->UnregisterSatelliteCallback();
217     }
218     for (auto pair : receiverMap_) {
219         auto handler = static_cast<GsmSmsReceiveHandler *>(pair.second.get());
220         if (handler == nullptr) {
221             TELEPHONY_LOGE("ReceiveHandler is null: %{public}d", pair.first);
222             continue;
223         }
224         handler->UnregisterSatelliteCallback();
225     }
226     mutexMap_.unlock();
227 }
228 
GetSatelliteSupported()229 bool SatelliteSmsClient::GetSatelliteSupported()
230 {
231     char satelliteSupported[SYSPARA_SIZE] = { 0 };
232     GetParameter(TEL_SATELLITE_SUPPORTED, SATELLITE_DEFAULT_VALUE, satelliteSupported, SYSPARA_SIZE);
233     return std::atoi(satelliteSupported);
234 }
235 
IsSatelliteEnabled()236 bool SatelliteSmsClient::IsSatelliteEnabled()
237 {
238     auto proxy = GetServiceProxy();
239     if (proxy == nullptr) {
240         return false;
241     }
242     return proxy->IsSatelliteEnabled();
243 }
244 
GetSatelliteCapability()245 int32_t SatelliteSmsClient::GetSatelliteCapability()
246 {
247     auto proxy = GetServiceProxy();
248     if (proxy == nullptr) {
249         TELEPHONY_LOGE("service proxy is null!");
250         return static_cast<int32_t>(SatelliteCapability::NONE);
251     }
252     return proxy->GetSatelliteCapability();
253 }
254 
RegisterSmsNotify(int32_t slotId,int32_t what,const sptr<ISatelliteSmsCallback> & callback)255 int32_t SatelliteSmsClient::RegisterSmsNotify(int32_t slotId, int32_t what, const sptr<ISatelliteSmsCallback> &callback)
256 {
257     auto proxy = GetProxy();
258     if (proxy == nullptr) {
259         TELEPHONY_LOGE("proxy is null!");
260         return TELEPHONY_ERR_IPC_CONNECT_STUB_FAIL;
261     }
262     return proxy->RegisterSmsNotify(slotId, what, callback);
263 }
264 
UnRegisterSmsNotify(int32_t slotId,int32_t what)265 int32_t SatelliteSmsClient::UnRegisterSmsNotify(int32_t slotId, int32_t what)
266 {
267     auto proxy = GetProxy();
268     if (proxy == nullptr) {
269         TELEPHONY_LOGE("proxy is null!");
270         return TELEPHONY_ERR_IPC_CONNECT_STUB_FAIL;
271     }
272     return proxy->UnRegisterSmsNotify(slotId, what);
273 }
274 
SendSms(int32_t slotId,int32_t eventId,SatelliteMessage & message)275 int32_t SatelliteSmsClient::SendSms(int32_t slotId, int32_t eventId, SatelliteMessage &message)
276 {
277     auto proxy = GetProxy();
278     if (proxy == nullptr) {
279         TELEPHONY_LOGE("proxy is null!");
280         return TELEPHONY_ERR_IPC_CONNECT_STUB_FAIL;
281     }
282     TELEPHONY_LOGI("SatelliteSms SendSms");
283     return proxy->SendSms(slotId, eventId, message);
284 }
285 
SendSmsMoreMode(int32_t slotId,int32_t eventId,SatelliteMessage & message)286 int32_t SatelliteSmsClient::SendSmsMoreMode(int32_t slotId, int32_t eventId, SatelliteMessage &message)
287 {
288     auto proxy = GetProxy();
289     if (proxy == nullptr) {
290         TELEPHONY_LOGE("proxy is null!");
291         return TELEPHONY_ERR_IPC_CONNECT_STUB_FAIL;
292     }
293     TELEPHONY_LOGI("SatelliteSms SendSmsMore");
294     return proxy->SendSmsMoreMode(slotId, eventId, message);
295 }
296 
SendSmsAck(int32_t slotId,int32_t eventId,bool success,int32_t cause)297 int32_t SatelliteSmsClient::SendSmsAck(int32_t slotId, int32_t eventId, bool success, int32_t cause)
298 {
299     auto proxy = GetProxy();
300     if (proxy == nullptr) {
301         TELEPHONY_LOGE("proxy is null!");
302         return TELEPHONY_ERR_IPC_CONNECT_STUB_FAIL;
303     }
304     return proxy->SendSmsAck(slotId, eventId, success, cause);
305 }
306 } // namespace Telephony
307 } // namespace OHOS
308