• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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         TELEPHONY_LOGE("Failed to get satellite service");
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     satelliteServiceProxy_ = iface_cast<ISatelliteService>(obj);
77 
78     deathRecipient_ = dr;
79     TELEPHONY_LOGD("Succeed to connect satellite service %{public}d", satelliteServiceProxy_ == nullptr);
80     return satelliteServiceProxy_;
81 }
82 
GetProxy()83 sptr<ISatelliteSmsService> SatelliteSmsClient::GetProxy()
84 {
85     auto satelliteServiceProxy = GetServiceProxy();
86 
87     std::lock_guard<std::mutex> lock(mutexProxy_);
88     if (proxy_ != nullptr) {
89         return proxy_;
90     }
91 
92     if (satelliteServiceProxy == nullptr) {
93         TELEPHONY_LOGE("GetProxy: satellite service is null or destroyed");
94         return nullptr;
95     }
96 
97     sptr<IRemoteObject> smsObj = satelliteServiceProxy->GetProxyObjectPtr(PROXY_SATELLITE_SMS);
98     if (smsObj == nullptr) {
99         TELEPHONY_LOGE("satellite sms service is null");
100         return nullptr;
101     }
102     proxy_ = iface_cast<ISatelliteSmsService>(smsObj);
103 
104     TELEPHONY_LOGD("Succeed to get satellite sms service %{public}d", proxy_ == nullptr);
105     return proxy_;
106 }
107 
OnRemoteDied(const wptr<IRemoteObject> & remote)108 void SatelliteSmsClient::OnRemoteDied(const wptr<IRemoteObject> &remote)
109 {
110     RemoveDeathRecipient(remote, true);
111 }
112 
RemoveDeathRecipient(const wptr<IRemoteObject> & remote,bool isRemoteDied)113 void SatelliteSmsClient::RemoveDeathRecipient(const wptr<IRemoteObject> &remote, bool isRemoteDied)
114 {
115     if (isRemoteDied && remote == nullptr) {
116         TELEPHONY_LOGE("Remote died, remote is nullptr");
117         return;
118     }
119     std::lock_guard<std::mutex> lock(mutexProxy_);
120     if (satelliteServiceProxy_ == nullptr) {
121         TELEPHONY_LOGE("satelliteServiceProxy_ is nullptr");
122         return;
123     }
124     auto serviceRemote = satelliteServiceProxy_->AsObject();
125     if (serviceRemote == nullptr) {
126         TELEPHONY_LOGE("serviceRemote is nullptr");
127         return;
128     }
129     if (isRemoteDied && serviceRemote != remote.promote()) {
130         TELEPHONY_LOGE("Remote died serviceRemote is not same");
131         return;
132     }
133     serviceRemote->RemoveDeathRecipient(deathRecipient_);
134     satelliteServiceProxy_ = nullptr;
135     proxy_ = nullptr;
136     TELEPHONY_LOGI("SatelliteSmsClient:RemoveDeathRecipient success");
137 }
138 
OnAddSystemAbility(int32_t systemAbilityId,const std::string & deviceId)139 void SatelliteSmsClient::SystemAbilityListener::OnAddSystemAbility(int32_t systemAbilityId, const std::string &deviceId)
140 {
141     TELEPHONY_LOGI("SA:%{public}d is added!", systemAbilityId);
142     if (!CheckInputSysAbilityId(systemAbilityId)) {
143         TELEPHONY_LOGE("add SA:%{public}d is invalid!", systemAbilityId);
144         return;
145     }
146 
147     auto &satelliteSmsClient = SatelliteSmsClient::GetInstance();
148     satelliteSmsClient.ServiceOn();
149     TELEPHONY_LOGI("SA:%{public}d reconnect service successfully!", systemAbilityId);
150 }
151 
OnRemoveSystemAbility(int32_t systemAbilityId,const std::string & deviceId)152 void SatelliteSmsClient::SystemAbilityListener::OnRemoveSystemAbility(
153     int32_t systemAbilityId, const std::string &deviceId)
154 {
155     TELEPHONY_LOGI("SA:%{public}d is removed!", systemAbilityId);
156     auto &satelliteSmsClient = SatelliteSmsClient::GetInstance();
157     satelliteSmsClient.ServiceOff();
158 }
159 
AddSendHandler(int32_t slotId,const std::shared_ptr<TelEventHandler> sender)160 int32_t SatelliteSmsClient::AddSendHandler(int32_t slotId, const std::shared_ptr<TelEventHandler> sender)
161 {
162     if (sender == nullptr) {
163         TELEPHONY_LOGE("AddSendHandler return, sender is null.");
164         return TELEPHONY_ERR_LOCAL_PTR_NULL;
165     }
166 
167     senderMap_.insert(std::make_pair(slotId, sender));
168     TELEPHONY_LOGI("AddSendHandler success: %{public}d", slotId);
169     return TELEPHONY_SUCCESS;
170 }
171 
AddReceiveHandler(int32_t slotId,const std::shared_ptr<TelEventHandler> receiver)172 int32_t SatelliteSmsClient::AddReceiveHandler(int32_t slotId, const std::shared_ptr<TelEventHandler> receiver)
173 {
174     if (receiver == nullptr) {
175         TELEPHONY_LOGE("AddReceiveHandler return, receiver is null.");
176         return TELEPHONY_ERR_LOCAL_PTR_NULL;
177     }
178 
179     receiverMap_.insert(std::make_pair(slotId, receiver));
180     TELEPHONY_LOGI("AddReceiveHandler success: %{public}d", slotId);
181     return TELEPHONY_SUCCESS;
182 }
183 
ServiceOn()184 void SatelliteSmsClient::ServiceOn()
185 {
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_guard<std::mutex> lock(mutexProxy_);
207     satelliteServiceProxy_ = nullptr;
208     proxy_ = nullptr;
209 
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 }
227 
GetSatelliteSupported()228 bool SatelliteSmsClient::GetSatelliteSupported()
229 {
230     char satelliteSupported[SYSPARA_SIZE] = { 0 };
231     GetParameter(TEL_SATELLITE_SUPPORTED, SATELLITE_DEFAULT_VALUE, satelliteSupported, SYSPARA_SIZE);
232     TELEPHONY_LOGI("SatelliteSms satelliteSupported is %{public}s", satelliteSupported);
233     return std::atoi(satelliteSupported);
234 }
235 
IsSatelliteEnabled()236 bool SatelliteSmsClient::IsSatelliteEnabled()
237 {
238     auto proxy = GetServiceProxy();
239     if (proxy == nullptr) {
240         TELEPHONY_LOGE("service proxy is null!");
241         return false;
242     }
243     return proxy->IsSatelliteEnabled();
244 }
245 
GetSatelliteCapability()246 int32_t SatelliteSmsClient::GetSatelliteCapability()
247 {
248     auto proxy = GetServiceProxy();
249     if (proxy == nullptr) {
250         TELEPHONY_LOGE("service proxy is null!");
251         return static_cast<int32_t>(SatelliteCapability::NONE);
252     }
253     return proxy->GetSatelliteCapability();
254 }
255 
RegisterSmsNotify(int32_t slotId,int32_t what,const sptr<ISatelliteSmsCallback> & callback)256 int32_t SatelliteSmsClient::RegisterSmsNotify(int32_t slotId, int32_t what, const sptr<ISatelliteSmsCallback> &callback)
257 {
258     auto proxy = GetProxy();
259     if (proxy == nullptr) {
260         TELEPHONY_LOGE("proxy is null!");
261         return TELEPHONY_ERR_IPC_CONNECT_STUB_FAIL;
262     }
263     return proxy->RegisterSmsNotify(slotId, what, callback);
264 }
265 
UnRegisterSmsNotify(int32_t slotId,int32_t what)266 int32_t SatelliteSmsClient::UnRegisterSmsNotify(int32_t slotId, int32_t what)
267 {
268     auto proxy = GetProxy();
269     if (proxy == nullptr) {
270         TELEPHONY_LOGE("proxy is null!");
271         return TELEPHONY_ERR_IPC_CONNECT_STUB_FAIL;
272     }
273     return proxy->UnRegisterSmsNotify(slotId, what);
274 }
275 
SendSms(int32_t slotId,int32_t eventId,SatelliteMessage & message)276 int32_t SatelliteSmsClient::SendSms(int32_t slotId, int32_t eventId, SatelliteMessage &message)
277 {
278     auto proxy = GetProxy();
279     if (proxy == nullptr) {
280         TELEPHONY_LOGE("proxy is null!");
281         return TELEPHONY_ERR_IPC_CONNECT_STUB_FAIL;
282     }
283     TELEPHONY_LOGI("SatelliteSms SendSms");
284     return proxy->SendSms(slotId, eventId, message);
285 }
286 
SendSmsMoreMode(int32_t slotId,int32_t eventId,SatelliteMessage & message)287 int32_t SatelliteSmsClient::SendSmsMoreMode(int32_t slotId, int32_t eventId, SatelliteMessage &message)
288 {
289     auto proxy = GetProxy();
290     if (proxy == nullptr) {
291         TELEPHONY_LOGE("proxy is null!");
292         return TELEPHONY_ERR_IPC_CONNECT_STUB_FAIL;
293     }
294     TELEPHONY_LOGI("SatelliteSms SendSmsMore");
295     return proxy->SendSmsMoreMode(slotId, eventId, message);
296 }
297 
SendSmsAck(int32_t slotId,int32_t eventId,bool success,int32_t cause)298 int32_t SatelliteSmsClient::SendSmsAck(int32_t slotId, int32_t eventId, bool success, int32_t cause)
299 {
300     auto proxy = GetProxy();
301     if (proxy == nullptr) {
302         TELEPHONY_LOGE("proxy is null!");
303         return TELEPHONY_ERR_IPC_CONNECT_STUB_FAIL;
304     }
305     return proxy->SendSmsAck(slotId, eventId, success, cause);
306 }
307 } // namespace Telephony
308 } // namespace OHOS
309