• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2021-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 "sms_service_proxy.h"
17 
18 #include "parcel.h"
19 #include "sms_mms_errors.h"
20 #include "string_utils.h"
21 #include "telephony_common_utils.h"
22 #include "telephony_errors.h"
23 #include "telephony_log_wrapper.h"
24 
25 namespace OHOS {
26 namespace Telephony {
27 const int32_t MAX_LEN = 10000;
SmsServiceProxy(const sptr<IRemoteObject> & impl)28 SmsServiceProxy::SmsServiceProxy(const sptr<IRemoteObject> &impl) : IRemoteProxy<ISmsServiceInterface>(impl) {}
29 
SendMessage(int32_t slotId,const std::u16string desAddr,const std::u16string scAddr,const std::u16string text,const sptr<ISendShortMessageCallback> & sendCallback,const sptr<IDeliveryShortMessageCallback> & deliverCallback)30 int32_t SmsServiceProxy::SendMessage(int32_t slotId, const std::u16string desAddr, const std::u16string scAddr,
31     const std::u16string text, const sptr<ISendShortMessageCallback> &sendCallback,
32     const sptr<IDeliveryShortMessageCallback> &deliverCallback)
33 {
34     TELEPHONY_LOGI("SmsServiceProxy::SendMessage with text slotId : %{public}d", slotId);
35     MessageParcel dataParcel;
36     MessageParcel replyParcel;
37     MessageOption option(MessageOption::TF_SYNC);
38     if (!dataParcel.WriteInterfaceToken(SmsServiceProxy::GetDescriptor())) {
39         TELEPHONY_LOGE("SendMessage with text WriteInterfaceToken is false");
40         return TELEPHONY_ERR_WRITE_DESCRIPTOR_TOKEN_FAIL;
41     }
42 
43     dataParcel.WriteInt32(slotId);
44     dataParcel.WriteString16(desAddr);
45     dataParcel.WriteString16(scAddr);
46     dataParcel.WriteString16(text);
47     if (sendCallback != nullptr) {
48         dataParcel.WriteRemoteObject(sendCallback->AsObject().GetRefPtr());
49     }
50 
51     if (deliverCallback != nullptr) {
52         dataParcel.WriteRemoteObject(deliverCallback->AsObject().GetRefPtr());
53     }
54 
55     sptr<IRemoteObject> remote = Remote();
56     if (remote == nullptr) {
57         TELEPHONY_LOGE("SendMessage with text Remote is null");
58         return TELEPHONY_ERR_IPC_CONNECT_STUB_FAIL;
59     }
60     std::string bundleName = GetBundleName();
61     dataParcel.WriteString(bundleName);
62     remote->SendRequest(static_cast<int32_t>(SmsServiceInterfaceCode::TEXT_BASED_SMS_DELIVERY), dataParcel,
63         replyParcel, option);
64     return replyParcel.ReadInt32();
65 };
66 
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> & sendCallback,const sptr<IDeliveryShortMessageCallback> & deliverCallback)67 int32_t SmsServiceProxy::SendMessage(int32_t slotId, const std::u16string desAddr, const std::u16string scAddr,
68     uint16_t port, const uint8_t *data, uint16_t dataLen, const sptr<ISendShortMessageCallback> &sendCallback,
69     const sptr<IDeliveryShortMessageCallback> &deliverCallback)
70 {
71     TELEPHONY_LOGI("SmsServiceProxy::SendMessage with data slotId : %{public}d", slotId);
72     MessageParcel dataParcel;
73     MessageParcel replyParcel;
74     MessageOption option(MessageOption::TF_SYNC);
75     if (!dataParcel.WriteInterfaceToken(SmsServiceProxy::GetDescriptor())) {
76         TELEPHONY_LOGE("SendMessage with data WriteInterfaceToken is false");
77         return TELEPHONY_ERR_WRITE_DESCRIPTOR_TOKEN_FAIL;
78     }
79 
80     dataParcel.WriteInt32(slotId);
81     dataParcel.WriteString16(desAddr);
82     dataParcel.WriteString16(scAddr);
83     dataParcel.WriteInt16(port);
84     if (sendCallback == nullptr) {
85         TELEPHONY_LOGE("SendMessage with data sendCallback is nullptr");
86         return TELEPHONY_ERR_LOCAL_PTR_NULL;
87     }
88     dataParcel.WriteRemoteObject(sendCallback->AsObject().GetRefPtr());
89     if (deliverCallback == nullptr) {
90         TELEPHONY_LOGE("SendMessage with data deliverCallback is nullptr");
91         return TELEPHONY_ERR_LOCAL_PTR_NULL;
92     }
93     dataParcel.WriteRemoteObject(deliverCallback->AsObject().GetRefPtr());
94     dataParcel.WriteInt16(dataLen);
95     dataParcel.WriteRawData(data, dataLen);
96 
97     sptr<IRemoteObject> remote = Remote();
98     if (remote == nullptr) {
99         TELEPHONY_LOGE("SendMessage with data Remote is null");
100         return TELEPHONY_ERR_IPC_CONNECT_STUB_FAIL;
101     }
102     remote->SendRequest(static_cast<int32_t>(SmsServiceInterfaceCode::DATA_BASED_SMS_DELIVERY), dataParcel,
103         replyParcel, option);
104     return replyParcel.ReadInt32();
105 };
106 
SetSmscAddr(int32_t slotId,const std::u16string & scAddr)107 int32_t SmsServiceProxy::SetSmscAddr(int32_t slotId, const std::u16string &scAddr)
108 {
109     TELEPHONY_LOGI("SmsServiceProxy::SetSmscAddr slotId : %{public}d", slotId);
110     MessageParcel dataParcel;
111     MessageParcel replyParcel;
112     MessageOption option(MessageOption::TF_SYNC);
113     if (!dataParcel.WriteInterfaceToken(SmsServiceProxy::GetDescriptor())) {
114         TELEPHONY_LOGE("SetSmscAddr WriteInterfaceToken is false");
115         return TELEPHONY_ERR_WRITE_DESCRIPTOR_TOKEN_FAIL;
116     }
117     dataParcel.WriteInt32(slotId);
118     dataParcel.WriteString16(scAddr);
119     sptr<IRemoteObject> remote = Remote();
120     if (remote == nullptr) {
121         TELEPHONY_LOGE("SetSmscAddr Remote is null");
122         return TELEPHONY_ERR_IPC_CONNECT_STUB_FAIL;
123     }
124     remote->SendRequest(static_cast<int32_t>(SmsServiceInterfaceCode::SET_SMSC_ADDRESS), dataParcel,
125         replyParcel, option);
126     return replyParcel.ReadInt32();
127 }
128 
GetSmscAddr(int32_t slotId,std::u16string & smscAddress)129 int32_t SmsServiceProxy::GetSmscAddr(int32_t slotId, std::u16string &smscAddress)
130 {
131     TELEPHONY_LOGI("SmsServiceProxy::GetSmscAddr slotId : %{public}d", slotId);
132     MessageParcel dataParcel;
133     MessageParcel replyParcel;
134     MessageOption option(MessageOption::TF_SYNC);
135     if (!dataParcel.WriteInterfaceToken(SmsServiceProxy::GetDescriptor())) {
136         TELEPHONY_LOGE("GetSmscAddr WriteInterfaceToken is false");
137         return TELEPHONY_ERR_WRITE_DESCRIPTOR_TOKEN_FAIL;
138     }
139     dataParcel.WriteInt32(slotId);
140     sptr<IRemoteObject> remote = Remote();
141     if (remote == nullptr) {
142         TELEPHONY_LOGE("GetSmscAddr Remote is null");
143         return TELEPHONY_ERR_IPC_CONNECT_STUB_FAIL;
144     }
145     remote->SendRequest(static_cast<int32_t>(SmsServiceInterfaceCode::GET_SMSC_ADDRESS), dataParcel,
146         replyParcel, option);
147     int32_t result = replyParcel.ReadInt32();
148     if (result == TELEPHONY_ERR_SUCCESS) {
149         smscAddress = replyParcel.ReadString16();
150     }
151     return result;
152 }
153 
AddSimMessage(int32_t slotId,const std::u16string & smsc,const std::u16string & pdu,SimMessageStatus status)154 int32_t SmsServiceProxy::AddSimMessage(
155     int32_t slotId, const std::u16string &smsc, const std::u16string &pdu, SimMessageStatus status)
156 {
157     TELEPHONY_LOGI("SmsServiceProxy::AddSimMessage slotId : %{public}d", slotId);
158     MessageParcel dataParcel;
159     MessageParcel replyParcel;
160     MessageOption option(MessageOption::TF_SYNC);
161     if (!dataParcel.WriteInterfaceToken(SmsServiceProxy::GetDescriptor())) {
162         TELEPHONY_LOGE("AddSimMessage WriteInterfaceToken is false");
163         return TELEPHONY_ERR_WRITE_DESCRIPTOR_TOKEN_FAIL;
164     }
165     dataParcel.WriteInt32(slotId);
166     dataParcel.WriteString16(smsc);
167     dataParcel.WriteString16(pdu);
168     dataParcel.WriteUint32(status);
169     sptr<IRemoteObject> remote = Remote();
170     if (remote == nullptr) {
171         TELEPHONY_LOGE("AddSimMessage Remote is null");
172         return TELEPHONY_ERR_IPC_CONNECT_STUB_FAIL;
173     }
174     remote->SendRequest(static_cast<int32_t>(SmsServiceInterfaceCode::ADD_SIM_MESSAGE), dataParcel,
175         replyParcel, option);
176     return replyParcel.ReadInt32();
177 }
178 
DelSimMessage(int32_t slotId,uint32_t msgIndex)179 int32_t SmsServiceProxy::DelSimMessage(int32_t slotId, uint32_t msgIndex)
180 {
181     TELEPHONY_LOGI("SmsServiceProxy::DelSimMessage slotId : %{public}d", slotId);
182     MessageParcel dataParcel;
183     MessageParcel replyParcel;
184     MessageOption option(MessageOption::TF_SYNC);
185     if (!dataParcel.WriteInterfaceToken(SmsServiceProxy::GetDescriptor())) {
186         TELEPHONY_LOGE("DelSimMessage WriteInterfaceToken is false");
187         return TELEPHONY_ERR_WRITE_DESCRIPTOR_TOKEN_FAIL;
188     }
189     dataParcel.WriteInt32(slotId);
190     dataParcel.WriteUint32(msgIndex);
191     sptr<IRemoteObject> remote = Remote();
192     if (remote == nullptr) {
193         TELEPHONY_LOGE("DelSimMessage Remote is null");
194         return TELEPHONY_ERR_IPC_CONNECT_STUB_FAIL;
195     }
196     remote->SendRequest(static_cast<int32_t>(SmsServiceInterfaceCode::DEL_SIM_MESSAGE), dataParcel,
197         replyParcel, option);
198     return replyParcel.ReadInt32();
199 }
200 
UpdateSimMessage(int32_t slotId,uint32_t msgIndex,SimMessageStatus newStatus,const std::u16string & pdu,const std::u16string & smsc)201 int32_t SmsServiceProxy::UpdateSimMessage(int32_t slotId, uint32_t msgIndex, SimMessageStatus newStatus,
202     const std::u16string &pdu, const std::u16string &smsc)
203 {
204     TELEPHONY_LOGI("SmsServiceProxy::UpdateSimMessage slotId : %{public}d", slotId);
205     MessageParcel dataParcel;
206     MessageParcel replyParcel;
207     MessageOption option(MessageOption::TF_SYNC);
208     if (!dataParcel.WriteInterfaceToken(SmsServiceProxy::GetDescriptor())) {
209         TELEPHONY_LOGE("UpdateSimMessage WriteInterfaceToken is false");
210         return TELEPHONY_ERR_WRITE_DESCRIPTOR_TOKEN_FAIL;
211     }
212     dataParcel.WriteInt32(slotId);
213     dataParcel.WriteUint32(msgIndex);
214     dataParcel.WriteUint32(newStatus);
215     dataParcel.WriteString16(pdu);
216     dataParcel.WriteString16(smsc);
217     sptr<IRemoteObject> remote = Remote();
218     if (remote == nullptr) {
219         TELEPHONY_LOGE("UpdateSimMessage Remote is null");
220         return TELEPHONY_ERR_IPC_CONNECT_STUB_FAIL;
221     }
222     remote->SendRequest(static_cast<int32_t>(SmsServiceInterfaceCode::UPDATE_SIM_MESSAGE), dataParcel,
223         replyParcel, option);
224     return replyParcel.ReadInt32();
225 }
226 
GetAllSimMessages(int32_t slotId,std::vector<ShortMessage> & message)227 int32_t SmsServiceProxy::GetAllSimMessages(int32_t slotId, std::vector<ShortMessage> &message)
228 {
229     TELEPHONY_LOGI("SmsServiceProxy::GetAllSimMessages slotId : %{public}d", slotId);
230     MessageParcel dataParcel;
231     MessageParcel replyParcel;
232     MessageOption option(MessageOption::TF_SYNC);
233     if (!dataParcel.WriteInterfaceToken(SmsServiceProxy::GetDescriptor())) {
234         TELEPHONY_LOGE("GetAllSimMessages WriteInterfaceToken is false");
235         return TELEPHONY_ERR_WRITE_DESCRIPTOR_TOKEN_FAIL;
236     }
237     dataParcel.WriteInt32(slotId);
238     sptr<IRemoteObject> remote = Remote();
239     if (remote == nullptr) {
240         TELEPHONY_LOGE("GetAllSimMessages Remote is null");
241         return TELEPHONY_ERR_IPC_CONNECT_STUB_FAIL;
242     }
243     remote->SendRequest(static_cast<int32_t>(SmsServiceInterfaceCode::GET_ALL_SIM_MESSAGE), dataParcel,
244         replyParcel, option);
245     int32_t result = replyParcel.ReadInt32();
246     if (result != TELEPHONY_ERR_SUCCESS) {
247         TELEPHONY_LOGE("GetAllSimMessages result is not TELEPHONY_ERR_SUCCESS");
248         return result;
249     }
250     int32_t resultLen = replyParcel.ReadInt32();
251     if (resultLen >= MAX_LEN) {
252         TELEPHONY_LOGE("GetAllSimMessages resultLen over max");
253         return SMS_MMS_MESSAGE_LENGTH_OUT_OF_RANGE;
254     }
255     for (int32_t i = 0; i < resultLen; i++) {
256         message.emplace_back(ShortMessage::UnMarshalling(replyParcel));
257     }
258     return result;
259 }
260 
SetCBConfig(int32_t slotId,bool enable,uint32_t fromMsgId,uint32_t toMsgId,uint8_t netType)261 int32_t SmsServiceProxy::SetCBConfig(
262     int32_t slotId, bool enable, uint32_t fromMsgId, uint32_t toMsgId, uint8_t netType)
263 {
264     TELEPHONY_LOGI("SmsServiceProxy::SetCBConfig slotId : %{public}d", slotId);
265     MessageParcel dataParcel;
266     MessageParcel replyParcel;
267     MessageOption option(MessageOption::TF_SYNC);
268     if (!dataParcel.WriteInterfaceToken(SmsServiceProxy::GetDescriptor())) {
269         TELEPHONY_LOGE("SetCBConfig WriteInterfaceToken is false");
270         return TELEPHONY_ERR_WRITE_DESCRIPTOR_TOKEN_FAIL;
271     }
272     dataParcel.WriteInt32(slotId);
273     dataParcel.WriteBool(enable);
274     dataParcel.WriteUint32(fromMsgId);
275     dataParcel.WriteUint32(toMsgId);
276     dataParcel.WriteUint8(netType);
277     sptr<IRemoteObject> remote = Remote();
278     if (remote == nullptr) {
279         TELEPHONY_LOGE("SetCBConfig Remote is null");
280         return TELEPHONY_ERR_IPC_CONNECT_STUB_FAIL;
281     }
282     remote->SendRequest(static_cast<int32_t>(SmsServiceInterfaceCode::SET_CB_CONFIG), dataParcel, replyParcel, option);
283     return replyParcel.ReadInt32();
284 }
285 
SetImsSmsConfig(int32_t slotId,int32_t enable)286 bool SmsServiceProxy::SetImsSmsConfig(
287     int32_t slotId, int32_t enable)
288 {
289     TELEPHONY_LOGI("SmsServiceProxy::SetImsSmsConfig slotId : %{public}d", slotId);
290     bool result = false;
291     MessageParcel dataParcel;
292     MessageParcel replyParcel;
293     MessageOption option(MessageOption::TF_SYNC);
294     if (!dataParcel.WriteInterfaceToken(SmsServiceProxy::GetDescriptor())) {
295         TELEPHONY_LOGE("SetImsSmsConfig WriteInterfaceToken is false");
296         return result;
297     }
298     dataParcel.WriteInt32(slotId);
299     dataParcel.WriteInt32(enable);
300     sptr<IRemoteObject> remote = Remote();
301     if (remote == nullptr) {
302         TELEPHONY_LOGE("SetImsSmsConfig Remote is null");
303         return result;
304     }
305     remote->SendRequest(static_cast<int32_t>(SmsServiceInterfaceCode::SET_IMS_SMS_CONFIG), dataParcel,
306         replyParcel, option);
307     return replyParcel.ReadBool();
308 }
309 
SetDefaultSmsSlotId(int32_t slotId)310 int32_t SmsServiceProxy::SetDefaultSmsSlotId(int32_t slotId)
311 {
312     TELEPHONY_LOGI("SmsServiceProxy::SetDefaultSmsSlotId slotId : %{public}d", slotId);
313     MessageParcel dataParcel;
314     MessageParcel replyParcel;
315     MessageOption option(MessageOption::TF_SYNC);
316     if (!dataParcel.WriteInterfaceToken(SmsServiceProxy::GetDescriptor())) {
317         TELEPHONY_LOGE("SetDefaultSmsSlotId WriteInterfaceToken is false");
318         return TELEPHONY_ERR_WRITE_DESCRIPTOR_TOKEN_FAIL;
319     }
320     dataParcel.WriteInt32(slotId);
321     sptr<IRemoteObject> remote = Remote();
322     if (remote == nullptr) {
323         TELEPHONY_LOGE("SetDefaultSmsSlotId Remote is null");
324         return TELEPHONY_ERR_IPC_CONNECT_STUB_FAIL;
325     }
326     remote->SendRequest(static_cast<int32_t>(SmsServiceInterfaceCode::SET_DEFAULT_SMS_SLOT_ID), dataParcel,
327         replyParcel, option);
328     return replyParcel.ReadInt32();
329 }
330 
GetDefaultSmsSlotId()331 int32_t SmsServiceProxy::GetDefaultSmsSlotId()
332 {
333     TELEPHONY_LOGI("SmsServiceProxy::GetDefaultSmsSlotId");
334     int32_t result = -1;
335     MessageParcel dataParcel;
336     MessageParcel replyParcel;
337     MessageOption option(MessageOption::TF_SYNC);
338     if (!dataParcel.WriteInterfaceToken(SmsServiceProxy::GetDescriptor())) {
339         TELEPHONY_LOGE("GetDefaultSmsSlotId WriteInterfaceToken is false");
340         return result;
341     }
342     sptr<IRemoteObject> remote = Remote();
343     if (remote == nullptr) {
344         TELEPHONY_LOGE("GetDefaultSmsSlotId Remote is null");
345         return result;
346     }
347     remote->SendRequest(static_cast<int32_t>(SmsServiceInterfaceCode::GET_DEFAULT_SMS_SLOT_ID), dataParcel,
348         replyParcel, option);
349     return replyParcel.ReadInt32();
350 }
351 
GetDefaultSmsSimId(int32_t & simId)352 int32_t SmsServiceProxy::GetDefaultSmsSimId(int32_t &simId)
353 {
354     MessageParcel dataParcel;
355     if (!dataParcel.WriteInterfaceToken(SmsServiceProxy::GetDescriptor())) {
356         TELEPHONY_LOGE("WriteInterfaceToken is false");
357         return TELEPHONY_ERR_WRITE_DESCRIPTOR_TOKEN_FAIL;
358     }
359     sptr<IRemoteObject> remote = Remote();
360     if (remote == nullptr) {
361         TELEPHONY_LOGE("Remote is null");
362         return TELEPHONY_ERR_IPC_CONNECT_STUB_FAIL;
363     }
364     MessageParcel replyParcel;
365     MessageOption option(MessageOption::TF_SYNC);
366     remote->SendRequest(static_cast<int32_t>(SmsServiceInterfaceCode::GET_DEFAULT_SMS_SIM_ID), dataParcel,
367         replyParcel, option);
368     int32_t result = replyParcel.ReadInt32();
369     TELEPHONY_LOGI("end: result=%{public}d", result);
370     if (result == TELEPHONY_ERR_SUCCESS) {
371         simId = replyParcel.ReadInt32();
372     }
373     return result;
374 }
375 
SplitMessage(const std::u16string & message,std::vector<std::u16string> & splitMessage)376 int32_t SmsServiceProxy::SplitMessage(const std::u16string &message, std::vector<std::u16string> &splitMessage)
377 {
378     TELEPHONY_LOGI("SmsServiceProxy::SplitMessage");
379     MessageParcel dataParcel;
380     MessageParcel replyParcel;
381     MessageOption option(MessageOption::TF_SYNC);
382     if (!dataParcel.WriteInterfaceToken(SmsServiceProxy::GetDescriptor())) {
383         TELEPHONY_LOGE("SplitMessage WriteInterfaceToken is false");
384         return TELEPHONY_ERR_WRITE_DESCRIPTOR_TOKEN_FAIL;
385     }
386     dataParcel.WriteString16(message);
387     sptr<IRemoteObject> remote = Remote();
388     if (remote == nullptr) {
389         TELEPHONY_LOGE("SplitMessage Remote is null");
390         return TELEPHONY_ERR_IPC_CONNECT_STUB_FAIL;
391     }
392     remote->SendRequest(static_cast<int32_t>(SmsServiceInterfaceCode::SPLIT_MESSAGE), dataParcel, replyParcel, option);
393     int32_t result = replyParcel.ReadInt32();
394     if (result != TELEPHONY_ERR_SUCCESS) {
395         TELEPHONY_LOGE("SplitMessage result is not TELEPHONY_ERR_SUCCESS");
396         return result;
397     }
398     int32_t resultLen = replyParcel.ReadInt32();
399     if (resultLen >= MAX_LEN) {
400         TELEPHONY_LOGE("SplitMessage resultLen over max");
401         return SMS_MMS_MESSAGE_LENGTH_OUT_OF_RANGE;
402     }
403     for (int32_t i = 0; i < resultLen; ++i) {
404         splitMessage.emplace_back(replyParcel.ReadString16());
405     }
406     return result;
407 }
408 
GetSmsSegmentsInfo(int32_t slotId,const std::u16string & message,bool force7BitCode,ISmsServiceInterface::SmsSegmentsInfo & segInfo)409 int32_t SmsServiceProxy::GetSmsSegmentsInfo(
410     int32_t slotId, const std::u16string &message, bool force7BitCode, ISmsServiceInterface::SmsSegmentsInfo &segInfo)
411 {
412     TELEPHONY_LOGI("SmsServiceProxy::GetSmsSegmentsInfo slotId : %{public}d", slotId);
413     MessageParcel dataParcel;
414     MessageParcel replyParcel;
415     MessageOption option(MessageOption::TF_SYNC);
416     if (!dataParcel.WriteInterfaceToken(SmsServiceProxy::GetDescriptor())) {
417         TELEPHONY_LOGE("GetSmsSegmentsInfo WriteInterfaceToken is false");
418         return TELEPHONY_ERR_WRITE_DESCRIPTOR_TOKEN_FAIL;
419     }
420     dataParcel.WriteInt32(slotId);
421     dataParcel.WriteString16(message);
422     dataParcel.WriteBool(force7BitCode);
423     sptr<IRemoteObject> remote = Remote();
424     if (remote == nullptr) {
425         TELEPHONY_LOGE("GetSmsSegmentsInfo Remote is null");
426         return TELEPHONY_ERR_IPC_CONNECT_STUB_FAIL;
427     }
428     remote->SendRequest(static_cast<int32_t>(SmsServiceInterfaceCode::GET_SMS_SEGMENTS_INFO), dataParcel,
429         replyParcel, option);
430     int32_t result = replyParcel.ReadInt32();
431     if (result != TELEPHONY_ERR_SUCCESS) {
432         TELEPHONY_LOGE("GetSmsSegmentsInfo ReadBool is null");
433         return result;
434     }
435 
436     segInfo.msgSegCount = replyParcel.ReadInt32();
437     segInfo.msgEncodingCount = replyParcel.ReadInt32();
438     segInfo.msgRemainCount = replyParcel.ReadInt32();
439     int32_t cds = replyParcel.ReadInt32();
440     segInfo.msgCodeScheme = static_cast<ISmsServiceInterface::SmsSegmentsInfo::SmsSegmentCodeScheme>(cds);
441     return TELEPHONY_ERR_SUCCESS;
442 }
443 
IsImsSmsSupported(int32_t slotId,bool & isSupported)444 int32_t SmsServiceProxy::IsImsSmsSupported(int32_t slotId, bool &isSupported)
445 {
446     TELEPHONY_LOGI("SmsServiceProxy::IsImsSmsSupported slotId : %{public}d", slotId);
447     MessageParcel dataParcel;
448     MessageParcel replyParcel;
449     MessageOption option(MessageOption::TF_SYNC);
450     if (!dataParcel.WriteInterfaceToken(SmsServiceProxy::GetDescriptor())) {
451         TELEPHONY_LOGE("IsImsSmsSupported WriteInterfaceToken is false");
452         return TELEPHONY_ERR_WRITE_DESCRIPTOR_TOKEN_FAIL;
453     }
454     dataParcel.WriteInt32(slotId);
455     sptr<IRemoteObject> remote = Remote();
456     if (remote == nullptr) {
457         TELEPHONY_LOGE("IsImsSmsSupported Remote is null");
458         return TELEPHONY_ERR_IPC_CONNECT_STUB_FAIL;
459     }
460     remote->SendRequest(static_cast<int32_t>(SmsServiceInterfaceCode::IS_IMS_SMS_SUPPORTED), dataParcel,
461         replyParcel, option);
462     int32_t result = replyParcel.ReadInt32();
463     if (result != TELEPHONY_ERR_SUCCESS) {
464         TELEPHONY_LOGE("GetSmsSegmentsInfo ReadBool is null");
465         return result;
466     }
467     isSupported = replyParcel.ReadBool();
468     return result;
469 }
470 
GetImsShortMessageFormat(std::u16string & format)471 int32_t SmsServiceProxy::GetImsShortMessageFormat(std::u16string &format)
472 {
473     TELEPHONY_LOGI("SmsServiceProxy::GetImsShortMessageFormat");
474     MessageParcel dataParcel;
475     MessageParcel replyParcel;
476     MessageOption option(MessageOption::TF_SYNC);
477     if (!dataParcel.WriteInterfaceToken(SmsServiceProxy::GetDescriptor())) {
478         TELEPHONY_LOGE("GetImsShortMessageFormat WriteInterfaceToken is false");
479         return TELEPHONY_ERR_WRITE_DESCRIPTOR_TOKEN_FAIL;
480     }
481     sptr<IRemoteObject> remote = Remote();
482     if (remote == nullptr) {
483         TELEPHONY_LOGE("GetImsShortMessageFormat Remote is null");
484         return TELEPHONY_ERR_IPC_CONNECT_STUB_FAIL;
485     }
486     remote->SendRequest(static_cast<int32_t>(SmsServiceInterfaceCode::GET_IMS_SHORT_MESSAGE_FORMAT), dataParcel,
487         replyParcel, option);
488     int32_t result = replyParcel.ReadInt32();
489     if (result == TELEPHONY_ERR_SUCCESS) {
490         format = replyParcel.ReadString16();
491     }
492     return result;
493 }
494 
HasSmsCapability()495 bool SmsServiceProxy::HasSmsCapability()
496 {
497     TELEPHONY_LOGI("SmsServiceProxy::HasSmsCapability");
498     bool result = false;
499     MessageParcel dataParcel;
500     MessageParcel replyParcel;
501     MessageOption option(MessageOption::TF_SYNC);
502     if (!dataParcel.WriteInterfaceToken(SmsServiceProxy::GetDescriptor())) {
503         TELEPHONY_LOGE("HasSmsCapability WriteInterfaceToken is false");
504         return result;
505     }
506     sptr<IRemoteObject> remote = Remote();
507     if (remote == nullptr) {
508         TELEPHONY_LOGE("HasSmsCapability Remote is null");
509         return result;
510     }
511     remote->SendRequest(static_cast<int32_t>(SmsServiceInterfaceCode::HAS_SMS_CAPABILITY), dataParcel,
512         replyParcel, option);
513     return replyParcel.ReadBool();
514 }
515 
SendMms(int32_t slotId,const std::u16string & mmsc,const std::u16string & data,const std::u16string & ua,const std::u16string & uaprof)516 int32_t SmsServiceProxy::SendMms(int32_t slotId, const std::u16string &mmsc, const std::u16string &data,
517     const std::u16string &ua, const std::u16string &uaprof)
518 {
519     MessageParcel dataParcel;
520     MessageParcel replyParcel;
521     MessageOption option(MessageOption::TF_SYNC);
522     if (!dataParcel.WriteInterfaceToken(SmsServiceProxy::GetDescriptor())) {
523         TELEPHONY_LOGE("SendMms WriteInterfaceToken is false");
524         return TELEPHONY_ERR_WRITE_DESCRIPTOR_TOKEN_FAIL;
525     }
526     dataParcel.WriteInt32(slotId);
527     dataParcel.WriteString16(mmsc);
528     dataParcel.WriteString16(data);
529     dataParcel.WriteString16(ua);
530     dataParcel.WriteString16(uaprof);
531     sptr<IRemoteObject> remote = Remote();
532     if (remote == nullptr) {
533         TELEPHONY_LOGE("SendMms Remote is null");
534         return TELEPHONY_ERR_IPC_CONNECT_STUB_FAIL;
535     }
536     remote->SendRequest(static_cast<int32_t>(SmsServiceInterfaceCode::SEND_MMS), dataParcel, replyParcel, option);
537     int32_t result = replyParcel.ReadInt32();
538     if (result != TELEPHONY_ERR_SUCCESS) {
539         TELEPHONY_LOGE("SendMms result fail");
540     }
541     return result;
542 }
543 
DownloadMms(int32_t slotId,const std::u16string & mmsc,const std::u16string & data,const std::u16string & ua,const std::u16string & uaprof)544 int32_t SmsServiceProxy::DownloadMms(int32_t slotId, const std::u16string &mmsc, const std::u16string &data,
545     const std::u16string &ua, const std::u16string &uaprof)
546 {
547     MessageParcel dataParcel;
548     MessageParcel replyParcel;
549     MessageOption option(MessageOption::TF_SYNC);
550     if (!dataParcel.WriteInterfaceToken(SmsServiceProxy::GetDescriptor())) {
551         TELEPHONY_LOGE("DownloadMms WriteInterfaceToken is false");
552         return TELEPHONY_ERR_WRITE_DESCRIPTOR_TOKEN_FAIL;
553     }
554     dataParcel.WriteInt32(slotId);
555     dataParcel.WriteString16(mmsc);
556     dataParcel.WriteString16(data);
557     dataParcel.WriteString16(ua);
558     dataParcel.WriteString16(uaprof);
559     sptr<IRemoteObject> remote = Remote();
560     if (remote == nullptr) {
561         TELEPHONY_LOGE("DownloadMms Remote is null");
562         return TELEPHONY_ERR_IPC_CONNECT_STUB_FAIL;
563     }
564     remote->SendRequest(static_cast<int32_t>(SmsServiceInterfaceCode::DOWNLOAD_MMS), dataParcel, replyParcel, option);
565     int32_t result = replyParcel.ReadInt32();
566     if (result != TELEPHONY_ERR_SUCCESS) {
567         TELEPHONY_LOGE("DownloadMms result fail");
568     }
569     return result;
570 }
571 
CreateMessage(std::string pdu,std::string specification,ShortMessage & message)572 int32_t SmsServiceProxy::CreateMessage(std::string pdu, std::string specification, ShortMessage &message)
573 {
574     if (pdu.empty() || specification.empty()) {
575         return TELEPHONY_ERR_ARGUMENT_INVALID;
576     }
577 
578     MessageParcel dataParcel;
579     MessageParcel replyParcel;
580     MessageOption option(MessageOption::TF_SYNC);
581     if (!dataParcel.WriteInterfaceToken(SmsServiceProxy::GetDescriptor())) {
582         TELEPHONY_LOGE("CreateMessage WriteInterfaceToken is false");
583         return TELEPHONY_ERR_WRITE_DESCRIPTOR_TOKEN_FAIL;
584     }
585 
586     dataParcel.WriteString(pdu);
587     dataParcel.WriteString(specification);
588 
589     sptr<IRemoteObject> remote = Remote();
590     if (remote == nullptr) {
591         return TELEPHONY_ERR_IPC_CONNECT_STUB_FAIL;
592     }
593     remote->SendRequest(static_cast<int32_t>(SmsServiceInterfaceCode::CREATE_MESSAGE), dataParcel, replyParcel, option);
594 
595     int32_t result = replyParcel.ReadInt32();
596     TELEPHONY_LOGI("SmsServiceProxy::CreateMessage result:%{public}d", result);
597     if (result != TELEPHONY_ERR_SUCCESS) {
598         TELEPHONY_LOGE("CreateMessage result fail");
599         return result;
600     }
601 
602     if (!message.ReadFromParcel(replyParcel)) {
603         TELEPHONY_LOGE("SmsServiceProxy::CreateMessage fail");
604         return TELEPHONY_ERR_LOCAL_PTR_NULL;
605     }
606     return result;
607 }
608 
GetBase64Encode(std::string src,std::string & dest)609 bool SmsServiceProxy::GetBase64Encode(std::string src, std::string &dest)
610 {
611     bool result = false;
612     if (src.empty()) {
613         return result;
614     }
615 
616     MessageParcel dataParcel;
617     MessageParcel replyParcel;
618     MessageOption option(MessageOption::TF_SYNC);
619     if (!dataParcel.WriteInterfaceToken(SmsServiceProxy::GetDescriptor())) {
620         TELEPHONY_LOGE("GetBase64Encode WriteInterfaceToken is false");
621         return result;
622     }
623 
624     dataParcel.WriteString16(StringUtils::ToUtf16(src));
625 
626     sptr<IRemoteObject> remote = Remote();
627     if (remote == nullptr) {
628         return result;
629     }
630     remote->SendRequest(static_cast<int32_t>(SmsServiceInterfaceCode::MMS_BASE64_ENCODE), dataParcel,
631         replyParcel, option);
632     result = replyParcel.ReadBool();
633     TELEPHONY_LOGI("SmsServiceProxy::GetBase64Encode result:%{public}d", result);
634     if (!result) {
635         return result;
636     }
637     dest = StringUtils::ToUtf8(dataParcel.ReadString16());
638     return result;
639 }
640 
GetBase64Decode(std::string src,std::string & dest)641 bool SmsServiceProxy::GetBase64Decode(std::string src, std::string &dest)
642 {
643     bool result = false;
644     if (src.empty()) {
645         return result;
646     }
647 
648     MessageParcel dataParcel;
649     MessageParcel replyParcel;
650     MessageOption option(MessageOption::TF_SYNC);
651     if (!dataParcel.WriteInterfaceToken(SmsServiceProxy::GetDescriptor())) {
652         TELEPHONY_LOGE("GetBase64Decode WriteInterfaceToken is false");
653         return result;
654     }
655 
656     dataParcel.WriteString16(StringUtils::ToUtf16(src));
657 
658     sptr<IRemoteObject> remote = Remote();
659     if (remote == nullptr) {
660         return result;
661     }
662     remote->SendRequest(static_cast<int32_t>(SmsServiceInterfaceCode::MMS_BASE64_DECODE), dataParcel,
663         replyParcel, option);
664     result = replyParcel.ReadBool();
665     TELEPHONY_LOGI("SmsServiceProxy::GetBase64Decode result:%{public}d", result);
666     if (!result) {
667         return result;
668     }
669     dest = StringUtils::ToUtf8(dataParcel.ReadString16());
670     return result;
671 }
672 
GetEncodeStringFunc(std::string & encodeString,uint32_t charset,uint32_t valLength,std::string strEncodeString)673 bool SmsServiceProxy::GetEncodeStringFunc(
674     std::string &encodeString, uint32_t charset, uint32_t valLength, std::string strEncodeString)
675 {
676     bool result = false;
677     if (strEncodeString.empty()) {
678         return result;
679     }
680 
681     MessageParcel dataParcel;
682     MessageParcel replyParcel;
683     MessageOption option(MessageOption::TF_SYNC);
684     if (!dataParcel.WriteInterfaceToken(SmsServiceProxy::GetDescriptor())) {
685         TELEPHONY_LOGE("GetEncodeStringFunc WriteInterfaceToken is false");
686         return result;
687     }
688 
689     dataParcel.WriteUint32(charset);
690     dataParcel.WriteUint32(valLength);
691     dataParcel.WriteString16(StringUtils::ToUtf16(strEncodeString));
692 
693     sptr<IRemoteObject> remote = Remote();
694     if (remote == nullptr) {
695         return result;
696     }
697     remote->SendRequest(static_cast<int32_t>(SmsServiceInterfaceCode::GET_ENCODE_STRING), dataParcel,
698         replyParcel, option);
699     result = replyParcel.ReadBool();
700     TELEPHONY_LOGI("SmsServiceProxy::GetEncodeStringFunc result:%{public}d", result);
701     if (!result) {
702         return result;
703     }
704     encodeString = StringUtils::ToUtf8(dataParcel.ReadString16());
705     return result;
706 }
707 
708 bool SmsServiceDeathRecipient::gotDeathRecipient_ = false;
709 
GotDeathRecipient()710 bool SmsServiceDeathRecipient::GotDeathRecipient()
711 {
712     return gotDeathRecipient_;
713 }
714 
OnRemoteDied(const wptr<IRemoteObject> & remote)715 void SmsServiceDeathRecipient::OnRemoteDied(const wptr<IRemoteObject> &remote)
716 {
717     gotDeathRecipient_ = true;
718 }
719 
SmsServiceDeathRecipient()720 SmsServiceDeathRecipient::SmsServiceDeathRecipient() {}
721 
~SmsServiceDeathRecipient()722 SmsServiceDeathRecipient::~SmsServiceDeathRecipient() {}
723 } // namespace Telephony
724 } // namespace OHOS
725