• 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     if (smsServiceInterface_ == nullptr) {
34         std::lock_guard<std::mutex> lock(mutex_);
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();
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     if ((smsServiceInterface_ != nullptr) && (smsServiceInterface_->AsObject() != nullptr)) {
65         smsServiceInterface_->AsObject()->RemoveDeathRecipient(recipient_);
66     }
67     smsServiceInterface_ = nullptr;
68 }
69 
SetDefaultSmsSlotId(int32_t slotId)70 int32_t SmsServiceManagerClient::SetDefaultSmsSlotId(int32_t slotId)
71 {
72     if (InitSmsServiceProxy()) {
73         return smsServiceInterface_->SetDefaultSmsSlotId(slotId);
74     }
75     return TELEPHONY_ERR_IPC_CONNECT_STUB_FAIL;
76 }
77 
GetDefaultSmsSimId(int32_t & simId)78 int32_t SmsServiceManagerClient::GetDefaultSmsSimId(int32_t &simId)
79 {
80     if (InitSmsServiceProxy()) {
81         return smsServiceInterface_->GetDefaultSmsSimId(simId);
82     }
83     return TELEPHONY_ERR_IPC_CONNECT_STUB_FAIL;
84 }
85 
GetDefaultSmsSlotId()86 int32_t SmsServiceManagerClient::GetDefaultSmsSlotId()
87 {
88     if (InitSmsServiceProxy()) {
89         return smsServiceInterface_->GetDefaultSmsSlotId();
90     }
91     return TELEPHONY_ERR_IPC_CONNECT_STUB_FAIL;
92 }
93 
SendMessage(int32_t slotId,const std::u16string desAddr,const std::u16string scAddr,const std::u16string text,const sptr<ISendShortMessageCallback> & callback,const sptr<IDeliveryShortMessageCallback> & deliveryCallback)94 int32_t SmsServiceManagerClient::SendMessage(int32_t slotId, const std::u16string desAddr, const std::u16string scAddr,
95     const std::u16string text, const sptr<ISendShortMessageCallback> &callback,
96     const sptr<IDeliveryShortMessageCallback> &deliveryCallback)
97 {
98     if (InitSmsServiceProxy()) {
99         return smsServiceInterface_->SendMessage(slotId, desAddr, scAddr, text, callback, deliveryCallback);
100     }
101     return TELEPHONY_ERR_IPC_CONNECT_STUB_FAIL;
102 }
103 
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)104 int32_t SmsServiceManagerClient::SendMessage(int32_t slotId, const std::u16string desAddr, const std::u16string scAddr,
105     uint16_t port, const uint8_t *data, uint16_t dataLen, const sptr<ISendShortMessageCallback> &callback,
106     const sptr<IDeliveryShortMessageCallback> &deliveryCallback)
107 {
108     if (InitSmsServiceProxy()) {
109         return smsServiceInterface_->SendMessage(
110             slotId, desAddr, scAddr, port, data, dataLen, callback, deliveryCallback);
111     }
112     return TELEPHONY_ERR_IPC_CONNECT_STUB_FAIL;
113 }
114 
SetScAddress(int32_t slotId,const std::u16string & scAddr)115 int32_t SmsServiceManagerClient::SetScAddress(int32_t slotId, const std::u16string &scAddr)
116 {
117     if (InitSmsServiceProxy()) {
118         return smsServiceInterface_->SetSmscAddr(slotId, scAddr);
119     }
120     return TELEPHONY_ERR_IPC_CONNECT_STUB_FAIL;
121 }
122 
GetScAddress(int32_t slotId,std::u16string & smscAddress)123 int32_t SmsServiceManagerClient::GetScAddress(int32_t slotId, std::u16string &smscAddress)
124 {
125     if (InitSmsServiceProxy()) {
126         return smsServiceInterface_->GetSmscAddr(slotId, smscAddress);
127     }
128     return TELEPHONY_ERR_IPC_CONNECT_STUB_FAIL;
129 }
130 
AddSimMessage(int32_t slotId,const std::u16string & smsc,const std::u16string & pdu,ISmsServiceInterface::SimMessageStatus status)131 int32_t SmsServiceManagerClient::AddSimMessage(int32_t slotId, const std::u16string &smsc, const std::u16string &pdu,
132     ISmsServiceInterface::SimMessageStatus status)
133 {
134     if (InitSmsServiceProxy()) {
135         return smsServiceInterface_->AddSimMessage(slotId, smsc, pdu, status);
136     }
137     return TELEPHONY_ERR_IPC_CONNECT_STUB_FAIL;
138 }
139 
DelSimMessage(int32_t slotId,uint32_t msgIndex)140 int32_t SmsServiceManagerClient::DelSimMessage(int32_t slotId, uint32_t msgIndex)
141 {
142     if (InitSmsServiceProxy()) {
143         return smsServiceInterface_->DelSimMessage(slotId, msgIndex);
144     }
145     return TELEPHONY_ERR_IPC_CONNECT_STUB_FAIL;
146 }
147 
UpdateSimMessage(int32_t slotId,uint32_t msgIndex,ISmsServiceInterface::SimMessageStatus newStatus,const std::u16string & pdu,const std::u16string & smsc)148 int32_t SmsServiceManagerClient::UpdateSimMessage(int32_t slotId, uint32_t msgIndex,
149     ISmsServiceInterface::SimMessageStatus newStatus, const std::u16string &pdu, const std::u16string &smsc)
150 {
151     if (InitSmsServiceProxy()) {
152         return smsServiceInterface_->UpdateSimMessage(slotId, msgIndex, newStatus, pdu, smsc);
153     }
154     return TELEPHONY_ERR_IPC_CONNECT_STUB_FAIL;
155 }
156 
GetAllSimMessages(int32_t slotId,std::vector<ShortMessage> & message)157 int32_t SmsServiceManagerClient::GetAllSimMessages(int32_t slotId, std::vector<ShortMessage> &message)
158 {
159     if (InitSmsServiceProxy()) {
160         return smsServiceInterface_->GetAllSimMessages(slotId, message);
161     }
162     return TELEPHONY_ERR_IPC_CONNECT_STUB_FAIL;
163 }
164 
SetCBConfig(int32_t slotId,bool enable,uint32_t startMessageId,uint32_t endMessageId,uint8_t ranType)165 int32_t SmsServiceManagerClient::SetCBConfig(
166     int32_t slotId, bool enable, uint32_t startMessageId, uint32_t endMessageId, uint8_t ranType)
167 {
168     if (InitSmsServiceProxy()) {
169         return smsServiceInterface_->SetCBConfig(slotId, enable, startMessageId, endMessageId, ranType);
170     }
171     return TELEPHONY_ERR_IPC_CONNECT_STUB_FAIL;
172 }
173 
SetImsSmsConfig(int32_t slotId,int32_t enable)174 bool SmsServiceManagerClient::SetImsSmsConfig(int32_t slotId, int32_t enable)
175 {
176     if (InitSmsServiceProxy()) {
177         return smsServiceInterface_->SetImsSmsConfig(slotId, enable);
178     }
179     return false;
180 }
181 
SendMms(int32_t slotId,const std::u16string & mmsc,const std::u16string & data,const std::u16string & ua,const std::u16string & uaprof)182 int32_t SmsServiceManagerClient::SendMms(int32_t slotId, const std::u16string &mmsc, const std::u16string &data,
183     const std::u16string &ua, const std::u16string &uaprof)
184 {
185     if (InitSmsServiceProxy()) {
186         return smsServiceInterface_->SendMms(slotId, mmsc, data, ua, uaprof);
187     }
188     return TELEPHONY_ERR_IPC_CONNECT_STUB_FAIL;
189 }
190 
DownloadMms(int32_t slotId,const std::u16string & mmsc,const std::u16string & data,const std::u16string & ua,const std::u16string & uaprof)191 int32_t SmsServiceManagerClient::DownloadMms(int32_t slotId, const std::u16string &mmsc, const std::u16string &data,
192     const std::u16string &ua, const std::u16string &uaprof)
193 {
194     if (InitSmsServiceProxy()) {
195         return smsServiceInterface_->DownloadMms(slotId, mmsc, data, ua, uaprof);
196     }
197     return TELEPHONY_ERR_IPC_CONNECT_STUB_FAIL;
198 }
199 
SplitMessage(const std::u16string & message,std::vector<std::u16string> & splitMessage)200 int32_t SmsServiceManagerClient::SplitMessage(const std::u16string &message, std::vector<std::u16string> &splitMessage)
201 {
202     if (InitSmsServiceProxy()) {
203         return smsServiceInterface_->SplitMessage(message, splitMessage);
204     }
205     return TELEPHONY_ERR_IPC_CONNECT_STUB_FAIL;
206 }
207 
GetSmsSegmentsInfo(int32_t slotId,const std::u16string & message,bool force7BitCode,ISmsServiceInterface::SmsSegmentsInfo & segInfo)208 int32_t SmsServiceManagerClient::GetSmsSegmentsInfo(
209     int32_t slotId, const std::u16string &message, bool force7BitCode, ISmsServiceInterface::SmsSegmentsInfo &segInfo)
210 {
211     if (InitSmsServiceProxy()) {
212         return smsServiceInterface_->GetSmsSegmentsInfo(slotId, message, force7BitCode, segInfo);
213     }
214     return TELEPHONY_ERR_IPC_CONNECT_STUB_FAIL;
215 }
216 
IsImsSmsSupported(int32_t slotId,bool & isSupported)217 int32_t SmsServiceManagerClient::IsImsSmsSupported(int32_t slotId, bool &isSupported)
218 {
219     if (InitSmsServiceProxy()) {
220         return smsServiceInterface_->IsImsSmsSupported(slotId, isSupported);
221     }
222     return TELEPHONY_ERR_IPC_CONNECT_STUB_FAIL;
223 }
224 
GetImsShortMessageFormat(std::u16string & format)225 int32_t SmsServiceManagerClient::GetImsShortMessageFormat(std::u16string &format)
226 {
227     if (InitSmsServiceProxy()) {
228         return smsServiceInterface_->GetImsShortMessageFormat(format);
229     }
230     return TELEPHONY_ERR_IPC_CONNECT_STUB_FAIL;
231 }
232 
HasSmsCapability()233 bool SmsServiceManagerClient::HasSmsCapability()
234 {
235     if (InitSmsServiceProxy()) {
236         return smsServiceInterface_->HasSmsCapability();
237     }
238     return false;
239 }
240 
CreateMessage(std::string pdu,std::string specification,ShortMessage & message)241 int32_t SmsServiceManagerClient::CreateMessage(std::string pdu, std::string specification, ShortMessage &message)
242 {
243     if (InitSmsServiceProxy()) {
244         return smsServiceInterface_->CreateMessage(pdu, specification, message);
245     }
246     return TELEPHONY_ERR_IPC_CONNECT_STUB_FAIL;
247 }
248 
GetBase64Encode(std::string src,std::string & dest)249 bool SmsServiceManagerClient::GetBase64Encode(std::string src, std::string &dest)
250 {
251     if (InitSmsServiceProxy()) {
252         return smsServiceInterface_->GetBase64Encode(src, dest);
253     }
254     return false;
255 }
256 
GetBase64Decode(std::string src,std::string & dest)257 bool SmsServiceManagerClient::GetBase64Decode(std::string src, std::string &dest)
258 {
259     if (InitSmsServiceProxy()) {
260         return smsServiceInterface_->GetBase64Decode(src, dest);
261     }
262     return false;
263 }
264 
GetEncodeStringFunc(std::string & encodeString,uint32_t charset,uint32_t valLength,std::string strEncodeString)265 bool SmsServiceManagerClient::GetEncodeStringFunc(
266     std::string &encodeString, uint32_t charset, uint32_t valLength, std::string strEncodeString)
267 {
268     if (InitSmsServiceProxy()) {
269         return smsServiceInterface_->GetEncodeStringFunc(encodeString, charset, valLength, strEncodeString);
270     }
271     return false;
272 }
273 } // namespace Telephony
274 } // namespace OHOS