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