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_service_manager_client.h"
17
18 #include "if_system_ability_manager.h"
19 #include "iservice_registry.h"
20 #include "sms_service_interface_death_recipient.h"
21 #include "system_ability_definition.h"
22 #include "telephony_errors.h"
23 #include "telephony_log_wrapper.h"
24
25 namespace OHOS {
26 namespace Telephony {
SmsServiceManagerClient()27 SmsServiceManagerClient::SmsServiceManagerClient() {}
28
~SmsServiceManagerClient()29 SmsServiceManagerClient::~SmsServiceManagerClient() {}
30
InitSmsServiceProxy()31 bool SmsServiceManagerClient::InitSmsServiceProxy()
32 {
33 std::lock_guard<std::mutex> lock(mutex_);
34 if (smsServiceInterface_ == nullptr) {
35 sptr<ISystemAbilityManager> systemAbilityManager =
36 SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
37 if (!systemAbilityManager) {
38 TELEPHONY_LOGE(" Get system ability mgr failed.");
39 return false;
40 }
41 sptr<IRemoteObject> remoteObject = systemAbilityManager->GetSystemAbility(TELEPHONY_SMS_MMS_SYS_ABILITY_ID);
42 if (!remoteObject) {
43 TELEPHONY_LOGE("Get SMS Service Failed.");
44 return false;
45 }
46 smsServiceInterface_ = iface_cast<ISmsServiceInterface>(remoteObject);
47 if ((!smsServiceInterface_) || (!smsServiceInterface_->AsObject())) {
48 TELEPHONY_LOGE("Get SMS Service Proxy Failed.");
49 return false;
50 }
51 recipient_ = new SmsServiceInterfaceDeathRecipient(*this);
52 if (!recipient_) {
53 TELEPHONY_LOGE("Failed to create death Recipient ptr SmsServiceInterfaceDeathRecipient!");
54 return false;
55 }
56 smsServiceInterface_->AsObject()->AddDeathRecipient(recipient_);
57 }
58 return true;
59 }
60
ResetSmsServiceProxy()61 void SmsServiceManagerClient::ResetSmsServiceProxy()
62 {
63 std::lock_guard<std::mutex> lock(mutex_);
64 std::lock_guard<std::mutex> mmsLock(mmsMutex_);
65 if ((smsServiceInterface_ != nullptr) && (smsServiceInterface_->AsObject() != nullptr)) {
66 smsServiceInterface_->AsObject()->RemoveDeathRecipient(recipient_);
67 }
68 smsServiceInterface_ = nullptr;
69 }
70
SetDefaultSmsSlotId(int32_t slotId)71 int32_t SmsServiceManagerClient::SetDefaultSmsSlotId(int32_t slotId)
72 {
73 if (InitSmsServiceProxy()) {
74 std::lock_guard<std::mutex> lock(mutex_);
75 return smsServiceInterface_->SetDefaultSmsSlotId(slotId);
76 }
77 return TELEPHONY_ERR_IPC_CONNECT_STUB_FAIL;
78 }
79
GetDefaultSmsSimId(int32_t & simId)80 int32_t SmsServiceManagerClient::GetDefaultSmsSimId(int32_t &simId)
81 {
82 if (InitSmsServiceProxy()) {
83 std::lock_guard<std::mutex> lock(mutex_);
84 return smsServiceInterface_->GetDefaultSmsSimId(simId);
85 }
86 return TELEPHONY_ERR_IPC_CONNECT_STUB_FAIL;
87 }
88
GetDefaultSmsSlotId()89 int32_t SmsServiceManagerClient::GetDefaultSmsSlotId()
90 {
91 if (InitSmsServiceProxy()) {
92 std::lock_guard<std::mutex> lock(mutex_);
93 return smsServiceInterface_->GetDefaultSmsSlotId();
94 }
95 return TELEPHONY_ERR_IPC_CONNECT_STUB_FAIL;
96 }
97
SendMessage(int32_t slotId,const std::u16string desAddr,const std::u16string scAddr,const std::u16string text,const sptr<ISendShortMessageCallback> & callback,const sptr<IDeliveryShortMessageCallback> & deliveryCallback)98 int32_t SmsServiceManagerClient::SendMessage(int32_t slotId, const std::u16string desAddr, const std::u16string scAddr,
99 const std::u16string text, const sptr<ISendShortMessageCallback> &callback,
100 const sptr<IDeliveryShortMessageCallback> &deliveryCallback)
101 {
102 if (InitSmsServiceProxy()) {
103 std::lock_guard<std::mutex> lock(mutex_);
104 return smsServiceInterface_->SendMessage(slotId, desAddr, scAddr, text, callback, deliveryCallback);
105 }
106 return TELEPHONY_ERR_IPC_CONNECT_STUB_FAIL;
107 }
108
SendMessage(int32_t slotId,const std::u16string desAddr,const std::u16string scAddr,uint16_t port,const uint8_t * data,uint16_t dataLen,const sptr<ISendShortMessageCallback> & callback,const sptr<IDeliveryShortMessageCallback> & deliveryCallback)109 int32_t SmsServiceManagerClient::SendMessage(int32_t slotId, const std::u16string desAddr, const std::u16string scAddr,
110 uint16_t port, const uint8_t *data, uint16_t dataLen, const sptr<ISendShortMessageCallback> &callback,
111 const sptr<IDeliveryShortMessageCallback> &deliveryCallback)
112 {
113 if (InitSmsServiceProxy()) {
114 std::lock_guard<std::mutex> lock(mutex_);
115 return smsServiceInterface_->SendMessage(
116 slotId, desAddr, scAddr, port, data, dataLen, callback, deliveryCallback);
117 }
118 return TELEPHONY_ERR_IPC_CONNECT_STUB_FAIL;
119 }
120
SendMessageWithoutSave(int32_t slotId,const std::u16string desAddr,const std::u16string scAddr,const std::u16string text,const sptr<ISendShortMessageCallback> & callback,const sptr<IDeliveryShortMessageCallback> & deliveryCallback)121 int32_t SmsServiceManagerClient::SendMessageWithoutSave(int32_t slotId, const std::u16string desAddr,
122 const std::u16string scAddr, const std::u16string text, const sptr<ISendShortMessageCallback> &callback,
123 const sptr<IDeliveryShortMessageCallback> &deliveryCallback)
124 {
125 if (InitSmsServiceProxy()) {
126 std::lock_guard<std::mutex> lock(mutex_);
127 return smsServiceInterface_->SendMessageWithoutSave(slotId, desAddr, scAddr, text, callback, deliveryCallback);
128 }
129 return TELEPHONY_ERR_IPC_CONNECT_STUB_FAIL;
130 }
131
SetScAddress(int32_t slotId,const std::u16string & scAddr)132 int32_t SmsServiceManagerClient::SetScAddress(int32_t slotId, const std::u16string &scAddr)
133 {
134 if (InitSmsServiceProxy()) {
135 std::lock_guard<std::mutex> lock(mutex_);
136 return smsServiceInterface_->SetSmscAddr(slotId, scAddr);
137 }
138 return TELEPHONY_ERR_IPC_CONNECT_STUB_FAIL;
139 }
140
GetScAddress(int32_t slotId,std::u16string & smscAddress)141 int32_t SmsServiceManagerClient::GetScAddress(int32_t slotId, std::u16string &smscAddress)
142 {
143 if (InitSmsServiceProxy()) {
144 std::lock_guard<std::mutex> lock(mutex_);
145 return smsServiceInterface_->GetSmscAddr(slotId, smscAddress);
146 }
147 return TELEPHONY_ERR_IPC_CONNECT_STUB_FAIL;
148 }
149
AddSimMessage(int32_t slotId,const std::u16string & smsc,const std::u16string & pdu,ISmsServiceInterface::SimMessageStatus status)150 int32_t SmsServiceManagerClient::AddSimMessage(int32_t slotId, const std::u16string &smsc, const std::u16string &pdu,
151 ISmsServiceInterface::SimMessageStatus status)
152 {
153 if (InitSmsServiceProxy()) {
154 std::lock_guard<std::mutex> lock(mutex_);
155 return smsServiceInterface_->AddSimMessage(slotId, smsc, pdu, status);
156 }
157 return TELEPHONY_ERR_IPC_CONNECT_STUB_FAIL;
158 }
159
DelSimMessage(int32_t slotId,uint32_t msgIndex)160 int32_t SmsServiceManagerClient::DelSimMessage(int32_t slotId, uint32_t msgIndex)
161 {
162 if (InitSmsServiceProxy()) {
163 std::lock_guard<std::mutex> lock(mutex_);
164 return smsServiceInterface_->DelSimMessage(slotId, msgIndex);
165 }
166 return TELEPHONY_ERR_IPC_CONNECT_STUB_FAIL;
167 }
168
UpdateSimMessage(int32_t slotId,uint32_t msgIndex,ISmsServiceInterface::SimMessageStatus newStatus,const std::u16string & pdu,const std::u16string & smsc)169 int32_t SmsServiceManagerClient::UpdateSimMessage(int32_t slotId, uint32_t msgIndex,
170 ISmsServiceInterface::SimMessageStatus newStatus, const std::u16string &pdu, const std::u16string &smsc)
171 {
172 if (InitSmsServiceProxy()) {
173 std::lock_guard<std::mutex> lock(mutex_);
174 return smsServiceInterface_->UpdateSimMessage(slotId, msgIndex, newStatus, pdu, smsc);
175 }
176 return TELEPHONY_ERR_IPC_CONNECT_STUB_FAIL;
177 }
178
GetAllSimMessages(int32_t slotId,std::vector<ShortMessage> & message)179 int32_t SmsServiceManagerClient::GetAllSimMessages(int32_t slotId, std::vector<ShortMessage> &message)
180 {
181 if (InitSmsServiceProxy()) {
182 std::lock_guard<std::mutex> lock(mutex_);
183 return smsServiceInterface_->GetAllSimMessages(slotId, message);
184 }
185 return TELEPHONY_ERR_IPC_CONNECT_STUB_FAIL;
186 }
187
SetCBConfig(int32_t slotId,bool enable,uint32_t startMessageId,uint32_t endMessageId,uint8_t ranType)188 int32_t SmsServiceManagerClient::SetCBConfig(
189 int32_t slotId, bool enable, uint32_t startMessageId, uint32_t endMessageId, uint8_t ranType)
190 {
191 if (InitSmsServiceProxy()) {
192 std::lock_guard<std::mutex> lock(mutex_);
193 return smsServiceInterface_->SetCBConfig(slotId, enable, startMessageId, endMessageId, ranType);
194 }
195 return TELEPHONY_ERR_IPC_CONNECT_STUB_FAIL;
196 }
197
SetImsSmsConfig(int32_t slotId,int32_t enable)198 bool SmsServiceManagerClient::SetImsSmsConfig(int32_t slotId, int32_t enable)
199 {
200 if (InitSmsServiceProxy()) {
201 std::lock_guard<std::mutex> lock(mutex_);
202 return smsServiceInterface_->SetImsSmsConfig(slotId, enable);
203 }
204 return false;
205 }
206
SendMms(int32_t slotId,const std::u16string & mmsc,const std::u16string & data,const std::u16string & ua,const std::u16string & uaprof,int64_t & time)207 int32_t SmsServiceManagerClient::SendMms(int32_t slotId, const std::u16string &mmsc, const std::u16string &data,
208 const std::u16string &ua, const std::u16string &uaprof, int64_t &time)
209 {
210 if (InitSmsServiceProxy()) {
211 std::lock_guard<std::mutex> lock(mmsMutex_);
212 return smsServiceInterface_->SendMms(slotId, mmsc, data, ua, uaprof, time);
213 }
214 return TELEPHONY_ERR_IPC_CONNECT_STUB_FAIL;
215 }
216
DownloadMms(int32_t slotId,const std::u16string & mmsc,std::u16string & data,const std::u16string & ua,const std::u16string & uaprof)217 int32_t SmsServiceManagerClient::DownloadMms(int32_t slotId, const std::u16string &mmsc, std::u16string &data,
218 const std::u16string &ua, const std::u16string &uaprof)
219 {
220 if (InitSmsServiceProxy()) {
221 std::lock_guard<std::mutex> lock(mmsMutex_);
222 return smsServiceInterface_->DownloadMms(slotId, mmsc, data, ua, uaprof);
223 }
224 return TELEPHONY_ERR_IPC_CONNECT_STUB_FAIL;
225 }
226
SplitMessage(const std::u16string & message,std::vector<std::u16string> & splitMessage)227 int32_t SmsServiceManagerClient::SplitMessage(const std::u16string &message, std::vector<std::u16string> &splitMessage)
228 {
229 if (InitSmsServiceProxy()) {
230 std::lock_guard<std::mutex> lock(mutex_);
231 return smsServiceInterface_->SplitMessage(message, splitMessage);
232 }
233 return TELEPHONY_ERR_IPC_CONNECT_STUB_FAIL;
234 }
235
GetSmsSegmentsInfo(int32_t slotId,const std::u16string & message,bool force7BitCode,ISmsServiceInterface::SmsSegmentsInfo & segInfo)236 int32_t SmsServiceManagerClient::GetSmsSegmentsInfo(
237 int32_t slotId, const std::u16string &message, bool force7BitCode, ISmsServiceInterface::SmsSegmentsInfo &segInfo)
238 {
239 if (InitSmsServiceProxy()) {
240 std::lock_guard<std::mutex> lock(mutex_);
241 return smsServiceInterface_->GetSmsSegmentsInfo(slotId, message, force7BitCode, segInfo);
242 }
243 return TELEPHONY_ERR_IPC_CONNECT_STUB_FAIL;
244 }
245
IsImsSmsSupported(int32_t slotId,bool & isSupported)246 int32_t SmsServiceManagerClient::IsImsSmsSupported(int32_t slotId, bool &isSupported)
247 {
248 if (InitSmsServiceProxy()) {
249 std::lock_guard<std::mutex> lock(mutex_);
250 return smsServiceInterface_->IsImsSmsSupported(slotId, isSupported);
251 }
252 return TELEPHONY_ERR_IPC_CONNECT_STUB_FAIL;
253 }
254
GetImsShortMessageFormat(std::u16string & format)255 int32_t SmsServiceManagerClient::GetImsShortMessageFormat(std::u16string &format)
256 {
257 if (InitSmsServiceProxy()) {
258 std::lock_guard<std::mutex> lock(mutex_);
259 return smsServiceInterface_->GetImsShortMessageFormat(format);
260 }
261 return TELEPHONY_ERR_IPC_CONNECT_STUB_FAIL;
262 }
263
HasSmsCapability()264 bool SmsServiceManagerClient::HasSmsCapability()
265 {
266 if (InitSmsServiceProxy()) {
267 std::lock_guard<std::mutex> lock(mutex_);
268 return smsServiceInterface_->HasSmsCapability();
269 }
270 return false;
271 }
272
CreateMessage(std::string pdu,std::string specification,ShortMessage & message)273 int32_t SmsServiceManagerClient::CreateMessage(std::string pdu, std::string specification, ShortMessage &message)
274 {
275 if (InitSmsServiceProxy()) {
276 std::lock_guard<std::mutex> lock(mutex_);
277 return smsServiceInterface_->CreateMessage(pdu, specification, message);
278 }
279 return TELEPHONY_ERR_IPC_CONNECT_STUB_FAIL;
280 }
281
GetBase64Encode(std::string src,std::string & dest)282 bool SmsServiceManagerClient::GetBase64Encode(std::string src, std::string &dest)
283 {
284 if (InitSmsServiceProxy()) {
285 std::lock_guard<std::mutex> lock(mutex_);
286 return smsServiceInterface_->GetBase64Encode(src, dest);
287 }
288 return false;
289 }
290
GetBase64Decode(std::string src,std::string & dest)291 bool SmsServiceManagerClient::GetBase64Decode(std::string src, std::string &dest)
292 {
293 if (InitSmsServiceProxy()) {
294 std::lock_guard<std::mutex> lock(mutex_);
295 return smsServiceInterface_->GetBase64Decode(src, dest);
296 }
297 return false;
298 }
299
GetEncodeStringFunc(std::string & encodeString,uint32_t charset,uint32_t valLength,std::string strEncodeString)300 bool SmsServiceManagerClient::GetEncodeStringFunc(
301 std::string &encodeString, uint32_t charset, uint32_t valLength, std::string strEncodeString)
302 {
303 if (InitSmsServiceProxy()) {
304 std::lock_guard<std::mutex> lock(mutex_);
305 return smsServiceInterface_->GetEncodeStringFunc(encodeString, charset, valLength, strEncodeString);
306 }
307 return false;
308 }
309 } // namespace Telephony
310 } // namespace OHOS