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 #include "telephony_napi_common_error.h"
25
26 namespace OHOS {
27 namespace Telephony {
SmsServiceManagerClient()28 SmsServiceManagerClient::SmsServiceManagerClient() {}
29
~SmsServiceManagerClient()30 SmsServiceManagerClient::~SmsServiceManagerClient() {}
31
InitSmsServiceProxy()32 bool SmsServiceManagerClient::InitSmsServiceProxy()
33 {
34 if (smsServiceInterface_ == nullptr) {
35 std::lock_guard<std::mutex> lock(mutex_);
36 sptr<ISystemAbilityManager> systemAbilityManager =
37 SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
38 if (!systemAbilityManager) {
39 TELEPHONY_LOGE(" Get system ability mgr failed.");
40 return false;
41 }
42 sptr<IRemoteObject> remoteObject = systemAbilityManager->GetSystemAbility(TELEPHONY_SMS_MMS_SYS_ABILITY_ID);
43 if (!remoteObject) {
44 TELEPHONY_LOGE("Get SMS Service Failed.");
45 return false;
46 }
47 smsServiceInterface_ = iface_cast<ISmsServiceInterface>(remoteObject);
48 if ((!smsServiceInterface_) || (!smsServiceInterface_->AsObject())) {
49 TELEPHONY_LOGE("Get SMS Service Proxy Failed.");
50 return false;
51 }
52 recipient_ = new SmsServiceInterfaceDeathRecipient();
53 if (!recipient_) {
54 TELEPHONY_LOGE("Failed to create death Recipient ptr SmsServiceInterfaceDeathRecipient!");
55 return false;
56 }
57 smsServiceInterface_->AsObject()->AddDeathRecipient(recipient_);
58 }
59 return true;
60 }
61
ResetSmsServiceProxy()62 void SmsServiceManagerClient::ResetSmsServiceProxy()
63 {
64 std::lock_guard<std::mutex> lock(mutex_);
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 return smsServiceInterface_->SetDefaultSmsSlotId(slotId);
75 }
76 return TELEPHONY_ERR_IPC_CONNECT_STUB_FAIL;
77 }
78
GetDefaultSmsSlotId()79 int32_t SmsServiceManagerClient::GetDefaultSmsSlotId()
80 {
81 if (InitSmsServiceProxy()) {
82 return smsServiceInterface_->GetDefaultSmsSlotId();
83 }
84 return TELEPHONY_ERR_IPC_CONNECT_STUB_FAIL;
85 }
86
SendMessage(int32_t slotId,const std::u16string desAddr,const std::u16string scAddr,const std::u16string text,const sptr<ISendShortMessageCallback> & callback,const sptr<IDeliveryShortMessageCallback> & deliveryCallback)87 int32_t SmsServiceManagerClient::SendMessage(int32_t slotId, const std::u16string desAddr,
88 const std::u16string scAddr, const std::u16string text, const sptr<ISendShortMessageCallback> &callback,
89 const sptr<IDeliveryShortMessageCallback> &deliveryCallback)
90 {
91 if (InitSmsServiceProxy()) {
92 return smsServiceInterface_->SendMessage(slotId, desAddr, scAddr, text, callback, deliveryCallback);
93 }
94 return TELEPHONY_ERR_IPC_CONNECT_STUB_FAIL;
95 }
96
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)97 int32_t SmsServiceManagerClient::SendMessage(int32_t slotId, const std::u16string desAddr,
98 const std::u16string scAddr, uint16_t port, const uint8_t *data, uint16_t dataLen,
99 const sptr<ISendShortMessageCallback> &callback, const sptr<IDeliveryShortMessageCallback> &deliveryCallback)
100 {
101 if (InitSmsServiceProxy()) {
102 return smsServiceInterface_->SendMessage(
103 slotId, desAddr, scAddr, port, data, dataLen, callback, deliveryCallback);
104 }
105 return TELEPHONY_ERR_IPC_CONNECT_STUB_FAIL;
106 }
107
SetScAddress(int32_t slotId,const std::u16string & scAddr)108 int32_t SmsServiceManagerClient::SetScAddress(int32_t slotId, const std::u16string &scAddr)
109 {
110 if (InitSmsServiceProxy()) {
111 return smsServiceInterface_->SetSmscAddr(slotId, scAddr);
112 }
113 return TELEPHONY_ERR_IPC_CONNECT_STUB_FAIL;
114 }
115
GetScAddress(int32_t slotId,std::u16string & smscAddress)116 int32_t SmsServiceManagerClient::GetScAddress(int32_t slotId, std::u16string &smscAddress)
117 {
118 if (InitSmsServiceProxy()) {
119 return smsServiceInterface_->GetSmscAddr(slotId, smscAddress);
120 }
121 return TELEPHONY_ERR_IPC_CONNECT_STUB_FAIL;
122 }
123
AddSimMessage(int32_t slotId,const std::u16string & smsc,const std::u16string & pdu,ISmsServiceInterface::SimMessageStatus status)124 int32_t SmsServiceManagerClient::AddSimMessage(int32_t slotId, const std::u16string &smsc, const std::u16string &pdu,
125 ISmsServiceInterface::SimMessageStatus status)
126 {
127 if (InitSmsServiceProxy()) {
128 return smsServiceInterface_->AddSimMessage(slotId, smsc, pdu, status);
129 }
130 return TELEPHONY_ERR_IPC_CONNECT_STUB_FAIL;
131 }
132
DelSimMessage(int32_t slotId,uint32_t msgIndex)133 int32_t SmsServiceManagerClient::DelSimMessage(int32_t slotId, uint32_t msgIndex)
134 {
135 if (InitSmsServiceProxy()) {
136 return smsServiceInterface_->DelSimMessage(slotId, msgIndex);
137 }
138 return TELEPHONY_ERR_IPC_CONNECT_STUB_FAIL;
139 }
140
UpdateSimMessage(int32_t slotId,uint32_t msgIndex,ISmsServiceInterface::SimMessageStatus newStatus,const std::u16string & pdu,const std::u16string & smsc)141 int32_t SmsServiceManagerClient::UpdateSimMessage(int32_t slotId, uint32_t msgIndex,
142 ISmsServiceInterface::SimMessageStatus newStatus, const std::u16string &pdu, const std::u16string &smsc)
143 {
144 if (InitSmsServiceProxy()) {
145 return smsServiceInterface_->UpdateSimMessage(slotId, msgIndex, newStatus, pdu, smsc);
146 }
147 return TELEPHONY_ERR_IPC_CONNECT_STUB_FAIL;
148 }
149
GetAllSimMessages(int32_t slotId,std::vector<ShortMessage> & message)150 int32_t SmsServiceManagerClient::GetAllSimMessages(int32_t slotId, std::vector<ShortMessage> &message)
151 {
152 if (InitSmsServiceProxy()) {
153 return smsServiceInterface_->GetAllSimMessages(slotId, message);
154 }
155 return TELEPHONY_ERR_IPC_CONNECT_STUB_FAIL;
156 }
157
SetCBConfig(int32_t slotId,bool enable,uint32_t startMessageId,uint32_t endMessageId,uint8_t ranType)158 int32_t SmsServiceManagerClient::SetCBConfig(
159 int32_t slotId, bool enable, uint32_t startMessageId, uint32_t endMessageId, uint8_t ranType)
160 {
161 if (InitSmsServiceProxy()) {
162 return smsServiceInterface_->SetCBConfig(slotId, enable, startMessageId, endMessageId, ranType);
163 }
164 return TELEPHONY_ERR_IPC_CONNECT_STUB_FAIL;
165 }
166
SetImsSmsConfig(int32_t slotId,int32_t enable)167 bool SmsServiceManagerClient::SetImsSmsConfig(int32_t slotId, int32_t enable)
168 {
169 if (InitSmsServiceProxy()) {
170 return smsServiceInterface_->SetImsSmsConfig(slotId, enable);
171 }
172 return false;
173 }
174
SplitMessage(const std::u16string & message,std::vector<std::u16string> & splitMessage)175 int32_t SmsServiceManagerClient::SplitMessage(const std::u16string &message, std::vector<std::u16string> &splitMessage)
176 {
177 if (InitSmsServiceProxy()) {
178 return smsServiceInterface_->SplitMessage(message, splitMessage);
179 }
180 return TELEPHONY_ERR_IPC_CONNECT_STUB_FAIL;
181 }
182
GetSmsSegmentsInfo(int32_t slotId,const std::u16string & message,bool force7BitCode,ISmsServiceInterface::SmsSegmentsInfo & segInfo)183 int32_t SmsServiceManagerClient::GetSmsSegmentsInfo(
184 int32_t slotId, const std::u16string &message, bool force7BitCode, ISmsServiceInterface::SmsSegmentsInfo &segInfo)
185 {
186 if (InitSmsServiceProxy()) {
187 return smsServiceInterface_->GetSmsSegmentsInfo(slotId, message, force7BitCode, segInfo);
188 }
189 return TELEPHONY_ERR_IPC_CONNECT_STUB_FAIL;
190 }
191
IsImsSmsSupported(int32_t slotId,bool & isSupported)192 int32_t SmsServiceManagerClient::IsImsSmsSupported(int32_t slotId, bool &isSupported)
193 {
194 if (InitSmsServiceProxy()) {
195 return smsServiceInterface_->IsImsSmsSupported(slotId, isSupported);
196 }
197 return TELEPHONY_ERR_IPC_CONNECT_STUB_FAIL;
198 }
199
GetImsShortMessageFormat(std::u16string & format)200 int32_t SmsServiceManagerClient::GetImsShortMessageFormat(std::u16string &format)
201 {
202 if (InitSmsServiceProxy()) {
203 return smsServiceInterface_->GetImsShortMessageFormat(format);
204 }
205 return TELEPHONY_ERR_IPC_CONNECT_STUB_FAIL;
206 }
207
HasSmsCapability()208 bool SmsServiceManagerClient::HasSmsCapability()
209 {
210 if (InitSmsServiceProxy()) {
211 return smsServiceInterface_->HasSmsCapability();
212 }
213 return false;
214 }
215
CreateMessage(std::string pdu,std::string specification,ShortMessage & message)216 int32_t SmsServiceManagerClient::CreateMessage(std::string pdu, std::string specification, ShortMessage &message)
217 {
218 if (InitSmsServiceProxy()) {
219 return smsServiceInterface_->CreateMessage(pdu, specification, message);
220 }
221 return TELEPHONY_ERR_IPC_CONNECT_STUB_FAIL;
222 }
223
GetBase64Encode(std::string src,std::string & dest)224 bool SmsServiceManagerClient::GetBase64Encode(std::string src, std::string &dest)
225 {
226 if (InitSmsServiceProxy()) {
227 return smsServiceInterface_->GetBase64Encode(src, dest);
228 }
229 return false;
230 }
231
GetBase64Decode(std::string src,std::string & dest)232 bool SmsServiceManagerClient::GetBase64Decode(std::string src, std::string &dest)
233 {
234 if (InitSmsServiceProxy()) {
235 return smsServiceInterface_->GetBase64Decode(src, dest);
236 }
237 return false;
238 }
239
GetEncodeStringFunc(std::string & encodeString,uint32_t charset,uint32_t valLength,std::string strEncodeString)240 bool SmsServiceManagerClient::GetEncodeStringFunc(
241 std::string &encodeString, uint32_t charset, uint32_t valLength, std::string strEncodeString)
242 {
243 if (InitSmsServiceProxy()) {
244 return smsServiceInterface_->GetEncodeStringFunc(encodeString, charset, valLength, strEncodeString);
245 }
246 return false;
247 }
248 } // namespace Telephony
249 } // namespace OHOS