• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
SetCBConfigList(int32_t slotId,const std::vector<int32_t> & messageIds,int32_t ranType)198 int32_t SmsServiceManagerClient::SetCBConfigList(
199     int32_t slotId, const std::vector<int32_t>& messageIds, int32_t ranType)
200 {
201     if (InitSmsServiceProxy()) {
202         std::lock_guard<std::mutex> lock(mutex_);
203         return smsServiceInterface_->SetCBConfigList(slotId, messageIds, ranType);
204     }
205     return TELEPHONY_ERR_IPC_CONNECT_STUB_FAIL;
206 }
207 
SetImsSmsConfig(int32_t slotId,int32_t enable)208 bool SmsServiceManagerClient::SetImsSmsConfig(int32_t slotId, int32_t enable)
209 {
210     if (InitSmsServiceProxy()) {
211         std::lock_guard<std::mutex> lock(mutex_);
212         return smsServiceInterface_->SetImsSmsConfig(slotId, enable);
213     }
214     return false;
215 }
216 
SendMms(int32_t slotId,const std::u16string & mmsc,const std::u16string & data,const std::u16string & ua,const std::u16string & uaprof,int64_t & time)217 int32_t SmsServiceManagerClient::SendMms(int32_t slotId, const std::u16string &mmsc, const std::u16string &data,
218     const std::u16string &ua, const std::u16string &uaprof, int64_t &time)
219 {
220     if (InitSmsServiceProxy()) {
221         std::lock_guard<std::mutex> lock(mmsMutex_);
222         return smsServiceInterface_->SendMms(slotId, mmsc, data, ua, uaprof, time);
223     }
224     return TELEPHONY_ERR_IPC_CONNECT_STUB_FAIL;
225 }
226 
DownloadMms(int32_t slotId,const std::u16string & mmsc,std::u16string & data,const std::u16string & ua,const std::u16string & uaprof)227 int32_t SmsServiceManagerClient::DownloadMms(int32_t slotId, const std::u16string &mmsc, std::u16string &data,
228     const std::u16string &ua, const std::u16string &uaprof)
229 {
230     if (InitSmsServiceProxy()) {
231         std::lock_guard<std::mutex> lock(mmsMutex_);
232         return smsServiceInterface_->DownloadMms(slotId, mmsc, data, ua, uaprof);
233     }
234     return TELEPHONY_ERR_IPC_CONNECT_STUB_FAIL;
235 }
236 
SplitMessage(const std::u16string & message,std::vector<std::u16string> & splitMessage)237 int32_t SmsServiceManagerClient::SplitMessage(const std::u16string &message, std::vector<std::u16string> &splitMessage)
238 {
239     if (InitSmsServiceProxy()) {
240         std::lock_guard<std::mutex> lock(mutex_);
241         return smsServiceInterface_->SplitMessage(message, splitMessage);
242     }
243     return TELEPHONY_ERR_IPC_CONNECT_STUB_FAIL;
244 }
245 
GetSmsSegmentsInfo(int32_t slotId,const std::u16string & message,bool force7BitCode,ISmsServiceInterface::SmsSegmentsInfo & segInfo)246 int32_t SmsServiceManagerClient::GetSmsSegmentsInfo(
247     int32_t slotId, const std::u16string &message, bool force7BitCode, ISmsServiceInterface::SmsSegmentsInfo &segInfo)
248 {
249     if (InitSmsServiceProxy()) {
250         std::lock_guard<std::mutex> lock(mutex_);
251         return smsServiceInterface_->GetSmsSegmentsInfo(slotId, message, force7BitCode, segInfo);
252     }
253     return TELEPHONY_ERR_IPC_CONNECT_STUB_FAIL;
254 }
255 
IsImsSmsSupported(int32_t slotId,bool & isSupported)256 int32_t SmsServiceManagerClient::IsImsSmsSupported(int32_t slotId, bool &isSupported)
257 {
258     if (InitSmsServiceProxy()) {
259         std::lock_guard<std::mutex> lock(mutex_);
260         return smsServiceInterface_->IsImsSmsSupported(slotId, isSupported);
261     }
262     return TELEPHONY_ERR_IPC_CONNECT_STUB_FAIL;
263 }
264 
GetImsShortMessageFormat(std::u16string & format)265 int32_t SmsServiceManagerClient::GetImsShortMessageFormat(std::u16string &format)
266 {
267     if (InitSmsServiceProxy()) {
268         std::lock_guard<std::mutex> lock(mutex_);
269         return smsServiceInterface_->GetImsShortMessageFormat(format);
270     }
271     return TELEPHONY_ERR_IPC_CONNECT_STUB_FAIL;
272 }
273 
HasSmsCapability()274 bool SmsServiceManagerClient::HasSmsCapability()
275 {
276     if (InitSmsServiceProxy()) {
277         std::lock_guard<std::mutex> lock(mutex_);
278         return smsServiceInterface_->HasSmsCapability();
279     }
280     return false;
281 }
282 
CreateMessage(std::string pdu,std::string specification,ShortMessage & message)283 int32_t SmsServiceManagerClient::CreateMessage(std::string pdu, std::string specification, ShortMessage &message)
284 {
285     if (InitSmsServiceProxy()) {
286         std::lock_guard<std::mutex> lock(mutex_);
287         return smsServiceInterface_->CreateMessage(pdu, specification, message);
288     }
289     return TELEPHONY_ERR_IPC_CONNECT_STUB_FAIL;
290 }
291 
GetBase64Encode(std::string src,std::string & dest)292 bool SmsServiceManagerClient::GetBase64Encode(std::string src, std::string &dest)
293 {
294     if (InitSmsServiceProxy()) {
295         std::lock_guard<std::mutex> lock(mutex_);
296         return smsServiceInterface_->GetBase64Encode(src, dest);
297     }
298     return false;
299 }
300 
GetBase64Decode(std::string src,std::string & dest)301 bool SmsServiceManagerClient::GetBase64Decode(std::string src, std::string &dest)
302 {
303     if (InitSmsServiceProxy()) {
304         std::lock_guard<std::mutex> lock(mutex_);
305         return smsServiceInterface_->GetBase64Decode(src, dest);
306     }
307     return false;
308 }
309 
GetEncodeStringFunc(std::string & encodeString,uint32_t charset,uint32_t valLength,std::string strEncodeString)310 bool SmsServiceManagerClient::GetEncodeStringFunc(
311     std::string &encodeString, uint32_t charset, uint32_t valLength, std::string strEncodeString)
312 {
313     if (InitSmsServiceProxy()) {
314         std::lock_guard<std::mutex> lock(mutex_);
315         return smsServiceInterface_->GetEncodeStringFunc(encodeString, charset, valLength, strEncodeString);
316     }
317     return false;
318 }
319 } // namespace Telephony
320 } // namespace OHOS