• 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 
SetCBConfigList(int32_t slotId,const std::vector<int32_t> & messageIds,int32_t ranType)343 int32_t SmsServiceProxy::SetCBConfigList(
344     int32_t slotId, const std::vector<int32_t>& messageIds, int32_t ranType)
345 {
346     MessageParcel dataParcel;
347     MessageParcel replyParcel;
348     MessageOption option(MessageOption::TF_SYNC);
349     if (!dataParcel.WriteInterfaceToken(SmsServiceProxy::GetDescriptor())) {
350         TELEPHONY_LOGE("SetCBConfigList WriteInterfaceToken is false");
351         return TELEPHONY_ERR_WRITE_DESCRIPTOR_TOKEN_FAIL;
352     }
353     dataParcel.WriteInt32(slotId);
354     dataParcel.WriteInt32(static_cast<int32_t>(messageIds.size()));
355     for (int32_t messageId : messageIds) {
356         dataParcel.WriteInt32(messageId);
357     }
358     dataParcel.WriteInt32(ranType);
359     sptr<IRemoteObject> remote = Remote();
360     if (remote == nullptr) {
361         TELEPHONY_LOGE("SetCBConfigList Remote is null");
362         return TELEPHONY_ERR_IPC_CONNECT_STUB_FAIL;
363     }
364     remote->SendRequest(static_cast<int32_t>(SmsServiceInterfaceCode::SET_CB_CONFIG_LIST), dataParcel,
365         replyParcel, option);
366     return replyParcel.ReadInt32();
367 }
368 
SetImsSmsConfig(int32_t slotId,int32_t enable)369 bool SmsServiceProxy::SetImsSmsConfig(
370     int32_t slotId, int32_t enable)
371 {
372     TELEPHONY_LOGI("SmsServiceProxy::SetImsSmsConfig slotId : %{public}d", slotId);
373     bool result = false;
374     MessageParcel dataParcel;
375     MessageParcel replyParcel;
376     MessageOption option(MessageOption::TF_SYNC);
377     if (!dataParcel.WriteInterfaceToken(SmsServiceProxy::GetDescriptor())) {
378         TELEPHONY_LOGE("SetImsSmsConfig WriteInterfaceToken is false");
379         return result;
380     }
381     dataParcel.WriteInt32(slotId);
382     dataParcel.WriteInt32(enable);
383     sptr<IRemoteObject> remote = Remote();
384     if (remote == nullptr) {
385         TELEPHONY_LOGE("SetImsSmsConfig Remote is null");
386         return result;
387     }
388     remote->SendRequest(
389         static_cast<int32_t>(SmsServiceInterfaceCode::SET_IMS_SMS_CONFIG), dataParcel, replyParcel, option);
390     return replyParcel.ReadBool();
391 }
392 
SetDefaultSmsSlotId(int32_t slotId)393 int32_t SmsServiceProxy::SetDefaultSmsSlotId(int32_t slotId)
394 {
395     TELEPHONY_LOGI("SmsServiceProxy::SetDefaultSmsSlotId slotId : %{public}d", slotId);
396     MessageParcel dataParcel;
397     MessageParcel replyParcel;
398     MessageOption option(MessageOption::TF_SYNC);
399     if (!dataParcel.WriteInterfaceToken(SmsServiceProxy::GetDescriptor())) {
400         TELEPHONY_LOGE("SetDefaultSmsSlotId WriteInterfaceToken is false");
401         return TELEPHONY_ERR_WRITE_DESCRIPTOR_TOKEN_FAIL;
402     }
403     dataParcel.WriteInt32(slotId);
404     sptr<IRemoteObject> remote = Remote();
405     if (remote == nullptr) {
406         TELEPHONY_LOGE("SetDefaultSmsSlotId Remote is null");
407         return TELEPHONY_ERR_IPC_CONNECT_STUB_FAIL;
408     }
409     remote->SendRequest(static_cast<int32_t>(SmsServiceInterfaceCode::SET_DEFAULT_SMS_SLOT_ID), dataParcel,
410         replyParcel, option);
411     return replyParcel.ReadInt32();
412 }
413 
GetDefaultSmsSlotId()414 int32_t SmsServiceProxy::GetDefaultSmsSlotId()
415 {
416     TELEPHONY_LOGI("SmsServiceProxy::GetDefaultSmsSlotId");
417     int32_t result = -1;
418     MessageParcel dataParcel;
419     MessageParcel replyParcel;
420     MessageOption option(MessageOption::TF_SYNC);
421     if (!dataParcel.WriteInterfaceToken(SmsServiceProxy::GetDescriptor())) {
422         TELEPHONY_LOGE("GetDefaultSmsSlotId WriteInterfaceToken is false");
423         return result;
424     }
425     sptr<IRemoteObject> remote = Remote();
426     if (remote == nullptr) {
427         TELEPHONY_LOGE("GetDefaultSmsSlotId Remote is null");
428         return result;
429     }
430     remote->SendRequest(static_cast<int32_t>(SmsServiceInterfaceCode::GET_DEFAULT_SMS_SLOT_ID), dataParcel,
431         replyParcel, option);
432     return replyParcel.ReadInt32();
433 }
434 
GetDefaultSmsSimId(int32_t & simId)435 int32_t SmsServiceProxy::GetDefaultSmsSimId(int32_t &simId)
436 {
437     MessageParcel dataParcel;
438     if (!dataParcel.WriteInterfaceToken(SmsServiceProxy::GetDescriptor())) {
439         TELEPHONY_LOGE("WriteInterfaceToken is false");
440         return TELEPHONY_ERR_WRITE_DESCRIPTOR_TOKEN_FAIL;
441     }
442     sptr<IRemoteObject> remote = Remote();
443     if (remote == nullptr) {
444         TELEPHONY_LOGE("Remote is null");
445         return TELEPHONY_ERR_IPC_CONNECT_STUB_FAIL;
446     }
447     MessageParcel replyParcel;
448     MessageOption option(MessageOption::TF_SYNC);
449     remote->SendRequest(static_cast<int32_t>(SmsServiceInterfaceCode::GET_DEFAULT_SMS_SIM_ID), dataParcel,
450         replyParcel, option);
451     int32_t result = replyParcel.ReadInt32();
452     TELEPHONY_LOGI("end: result=%{public}d", result);
453     if (result == TELEPHONY_ERR_SUCCESS) {
454         simId = replyParcel.ReadInt32();
455     }
456     return result;
457 }
458 
SplitMessage(const std::u16string & message,std::vector<std::u16string> & splitMessage)459 int32_t SmsServiceProxy::SplitMessage(const std::u16string &message, std::vector<std::u16string> &splitMessage)
460 {
461     TELEPHONY_LOGI("SmsServiceProxy::SplitMessage");
462     MessageParcel dataParcel;
463     MessageParcel replyParcel;
464     MessageOption option(MessageOption::TF_SYNC);
465     if (!dataParcel.WriteInterfaceToken(SmsServiceProxy::GetDescriptor())) {
466         TELEPHONY_LOGE("SplitMessage WriteInterfaceToken is false");
467         return TELEPHONY_ERR_WRITE_DESCRIPTOR_TOKEN_FAIL;
468     }
469     dataParcel.WriteString16(message);
470     sptr<IRemoteObject> remote = Remote();
471     if (remote == nullptr) {
472         TELEPHONY_LOGE("SplitMessage Remote is null");
473         return TELEPHONY_ERR_IPC_CONNECT_STUB_FAIL;
474     }
475     remote->SendRequest(static_cast<int32_t>(SmsServiceInterfaceCode::SPLIT_MESSAGE), dataParcel, replyParcel, option);
476     int32_t result = replyParcel.ReadInt32();
477     if (result != TELEPHONY_ERR_SUCCESS) {
478         TELEPHONY_LOGE("SplitMessage result is not TELEPHONY_ERR_SUCCESS");
479         return result;
480     }
481     int32_t resultLen = replyParcel.ReadInt32();
482     if (resultLen >= MAX_LEN) {
483         TELEPHONY_LOGE("SplitMessage resultLen over max");
484         return SMS_MMS_MESSAGE_LENGTH_OUT_OF_RANGE;
485     }
486     for (int32_t i = 0; i < resultLen; ++i) {
487         splitMessage.emplace_back(replyParcel.ReadString16());
488     }
489     return result;
490 }
491 
GetSmsSegmentsInfo(int32_t slotId,const std::u16string & message,bool force7BitCode,ISmsServiceInterface::SmsSegmentsInfo & segInfo)492 int32_t SmsServiceProxy::GetSmsSegmentsInfo(
493     int32_t slotId, const std::u16string &message, bool force7BitCode, ISmsServiceInterface::SmsSegmentsInfo &segInfo)
494 {
495     TELEPHONY_LOGI("SmsServiceProxy::GetSmsSegmentsInfo slotId : %{public}d", slotId);
496     MessageParcel dataParcel;
497     MessageParcel replyParcel;
498     MessageOption option(MessageOption::TF_SYNC);
499     if (!dataParcel.WriteInterfaceToken(SmsServiceProxy::GetDescriptor())) {
500         TELEPHONY_LOGE("GetSmsSegmentsInfo WriteInterfaceToken is false");
501         return TELEPHONY_ERR_WRITE_DESCRIPTOR_TOKEN_FAIL;
502     }
503     dataParcel.WriteInt32(slotId);
504     dataParcel.WriteString16(message);
505     dataParcel.WriteBool(force7BitCode);
506     sptr<IRemoteObject> remote = Remote();
507     if (remote == nullptr) {
508         TELEPHONY_LOGE("GetSmsSegmentsInfo Remote is null");
509         return TELEPHONY_ERR_IPC_CONNECT_STUB_FAIL;
510     }
511     remote->SendRequest(static_cast<int32_t>(SmsServiceInterfaceCode::GET_SMS_SEGMENTS_INFO), dataParcel,
512         replyParcel, option);
513     int32_t result = replyParcel.ReadInt32();
514     if (result != TELEPHONY_ERR_SUCCESS) {
515         TELEPHONY_LOGE("GetSmsSegmentsInfo ReadBool is null");
516         return result;
517     }
518 
519     segInfo.msgSegCount = replyParcel.ReadInt32();
520     segInfo.msgEncodingCount = replyParcel.ReadInt32();
521     segInfo.msgRemainCount = replyParcel.ReadInt32();
522     int32_t cds = replyParcel.ReadInt32();
523     segInfo.msgCodeScheme = static_cast<ISmsServiceInterface::SmsSegmentsInfo::SmsSegmentCodeScheme>(cds);
524     return TELEPHONY_ERR_SUCCESS;
525 }
526 
IsImsSmsSupported(int32_t slotId,bool & isSupported)527 int32_t SmsServiceProxy::IsImsSmsSupported(int32_t slotId, bool &isSupported)
528 {
529     TELEPHONY_LOGI("SmsServiceProxy::IsImsSmsSupported slotId : %{public}d", slotId);
530     MessageParcel dataParcel;
531     MessageParcel replyParcel;
532     MessageOption option(MessageOption::TF_SYNC);
533     if (!dataParcel.WriteInterfaceToken(SmsServiceProxy::GetDescriptor())) {
534         TELEPHONY_LOGE("IsImsSmsSupported WriteInterfaceToken is false");
535         return TELEPHONY_ERR_WRITE_DESCRIPTOR_TOKEN_FAIL;
536     }
537     dataParcel.WriteInt32(slotId);
538     sptr<IRemoteObject> remote = Remote();
539     if (remote == nullptr) {
540         TELEPHONY_LOGE("IsImsSmsSupported Remote is null");
541         return TELEPHONY_ERR_IPC_CONNECT_STUB_FAIL;
542     }
543     remote->SendRequest(static_cast<int32_t>(SmsServiceInterfaceCode::IS_IMS_SMS_SUPPORTED), dataParcel,
544         replyParcel, option);
545     int32_t result = replyParcel.ReadInt32();
546     if (result != TELEPHONY_ERR_SUCCESS) {
547         TELEPHONY_LOGE("GetSmsSegmentsInfo ReadBool is null");
548         return result;
549     }
550     isSupported = replyParcel.ReadBool();
551     return result;
552 }
553 
GetImsShortMessageFormat(std::u16string & format)554 int32_t SmsServiceProxy::GetImsShortMessageFormat(std::u16string &format)
555 {
556     TELEPHONY_LOGI("SmsServiceProxy::GetImsShortMessageFormat");
557     MessageParcel dataParcel;
558     MessageParcel replyParcel;
559     MessageOption option(MessageOption::TF_SYNC);
560     if (!dataParcel.WriteInterfaceToken(SmsServiceProxy::GetDescriptor())) {
561         TELEPHONY_LOGE("GetImsShortMessageFormat WriteInterfaceToken is false");
562         return TELEPHONY_ERR_WRITE_DESCRIPTOR_TOKEN_FAIL;
563     }
564     sptr<IRemoteObject> remote = Remote();
565     if (remote == nullptr) {
566         TELEPHONY_LOGE("GetImsShortMessageFormat Remote is null");
567         return TELEPHONY_ERR_IPC_CONNECT_STUB_FAIL;
568     }
569     remote->SendRequest(static_cast<int32_t>(SmsServiceInterfaceCode::GET_IMS_SHORT_MESSAGE_FORMAT), dataParcel,
570         replyParcel, option);
571     int32_t result = replyParcel.ReadInt32();
572     if (result == TELEPHONY_ERR_SUCCESS) {
573         format = replyParcel.ReadString16();
574     }
575     return result;
576 }
577 
HasSmsCapability()578 bool SmsServiceProxy::HasSmsCapability()
579 {
580     TELEPHONY_LOGI("SmsServiceProxy::HasSmsCapability");
581     bool result = false;
582     MessageParcel dataParcel;
583     MessageParcel replyParcel;
584     MessageOption option(MessageOption::TF_SYNC);
585     if (!dataParcel.WriteInterfaceToken(SmsServiceProxy::GetDescriptor())) {
586         TELEPHONY_LOGE("HasSmsCapability WriteInterfaceToken is false");
587         return result;
588     }
589     sptr<IRemoteObject> remote = Remote();
590     if (remote == nullptr) {
591         TELEPHONY_LOGE("HasSmsCapability Remote is null");
592         return result;
593     }
594     remote->SendRequest(static_cast<int32_t>(SmsServiceInterfaceCode::HAS_SMS_CAPABILITY), dataParcel,
595         replyParcel, option);
596     return replyParcel.ReadBool();
597 }
598 
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)599 int32_t SmsServiceProxy::SendMms(int32_t slotId, const std::u16string &mmsc, const std::u16string &data,
600     const std::u16string &ua, const std::u16string &uaprof, int64_t &time, bool isMmsApp)
601 {
602     MessageParcel dataParcel;
603     MessageParcel replyParcel;
604     MessageOption option(MessageOption::TF_SYNC);
605     if (!dataParcel.WriteInterfaceToken(SmsServiceProxy::GetDescriptor())) {
606         TELEPHONY_LOGE("SendMms WriteInterfaceToken is false");
607         return TELEPHONY_ERR_WRITE_DESCRIPTOR_TOKEN_FAIL;
608     }
609     dataParcel.WriteInt32(slotId);
610     dataParcel.WriteString16(mmsc);
611     dataParcel.WriteString16(data);
612     dataParcel.WriteString16(ua);
613     dataParcel.WriteString16(uaprof);
614     dataParcel.WriteInt64(time);
615     std::string bundleName = GetBundleName();
616     dataParcel.WriteString(bundleName);
617     sptr<IRemoteObject> remote = Remote();
618     if (remote == nullptr) {
619         TELEPHONY_LOGE("SendMms Remote is null");
620         return TELEPHONY_ERR_IPC_CONNECT_STUB_FAIL;
621     }
622     int32_t errCode =
623         remote->SendRequest(static_cast<int32_t>(SmsServiceInterfaceCode::SEND_MMS), dataParcel, replyParcel, option);
624     if (errCode != TELEPHONY_SUCCESS) {
625         TELEPHONY_LOGE("SendMms failed, errcode:%{public}d", errCode);
626         return errCode;
627     }
628     return replyParcel.ReadInt32();
629 }
630 
DownloadMms(int32_t slotId,const std::u16string & mmsc,std::u16string & data,const std::u16string & ua,const std::u16string & uaprof)631 int32_t SmsServiceProxy::DownloadMms(int32_t slotId, const std::u16string &mmsc, std::u16string &data,
632     const std::u16string &ua, const std::u16string &uaprof)
633 {
634     MessageParcel dataParcel;
635     MessageParcel replyParcel;
636     MessageOption option(MessageOption::TF_SYNC);
637     if (!dataParcel.WriteInterfaceToken(SmsServiceProxy::GetDescriptor())) {
638         TELEPHONY_LOGE("DownloadMms WriteInterfaceToken is false");
639         return TELEPHONY_ERR_WRITE_DESCRIPTOR_TOKEN_FAIL;
640     }
641     dataParcel.WriteInt32(slotId);
642     dataParcel.WriteString16(mmsc);
643     dataParcel.WriteString16(data);
644     dataParcel.WriteString16(ua);
645     dataParcel.WriteString16(uaprof);
646     sptr<IRemoteObject> remote = Remote();
647     if (remote == nullptr) {
648         TELEPHONY_LOGE("DownloadMms Remote is null");
649         return TELEPHONY_ERR_IPC_CONNECT_STUB_FAIL;
650     }
651     remote->SendRequest(static_cast<int32_t>(SmsServiceInterfaceCode::DOWNLOAD_MMS), dataParcel, replyParcel, option);
652     int32_t result = replyParcel.ReadInt32();
653     if (result != TELEPHONY_ERR_SUCCESS) {
654         TELEPHONY_LOGE("DownloadMms result fail");
655         return result;
656     }
657     data = replyParcel.ReadString16();
658     return result;
659 }
660 
CreateMessage(std::string pdu,std::string specification,ShortMessage & message)661 int32_t SmsServiceProxy::CreateMessage(std::string pdu, std::string specification, ShortMessage &message)
662 {
663     if (pdu.empty() || specification.empty()) {
664         return TELEPHONY_ERR_ARGUMENT_INVALID;
665     }
666 
667     MessageParcel dataParcel;
668     MessageParcel replyParcel;
669     MessageOption option(MessageOption::TF_SYNC);
670     if (!dataParcel.WriteInterfaceToken(SmsServiceProxy::GetDescriptor())) {
671         TELEPHONY_LOGE("CreateMessage WriteInterfaceToken is false");
672         return TELEPHONY_ERR_WRITE_DESCRIPTOR_TOKEN_FAIL;
673     }
674 
675     dataParcel.WriteString(pdu);
676     dataParcel.WriteString(specification);
677 
678     if (localObject_ == nullptr) {
679         TELEPHONY_LOGE("localObject_ nullptr");
680         return TELEPHONY_ERR_IPC_CONNECT_STUB_FAIL;
681     }
682     localObject_->SendRequest(
683         static_cast<int32_t>(SmsServiceInterfaceCode::CREATE_MESSAGE), dataParcel, replyParcel, option);
684 
685     int32_t result = replyParcel.ReadInt32();
686     if (result != TELEPHONY_ERR_SUCCESS) {
687         TELEPHONY_LOGE("CreateMessage result fail");
688         return result;
689     }
690 
691     if (!message.ReadFromParcel(replyParcel)) {
692         TELEPHONY_LOGE("SmsServiceProxy::CreateMessage fail");
693         return TELEPHONY_ERR_LOCAL_PTR_NULL;
694     }
695     return result;
696 }
697 
GetBase64Encode(std::string src,std::string & dest)698 bool SmsServiceProxy::GetBase64Encode(std::string src, std::string &dest)
699 {
700     bool result = false;
701     if (src.empty()) {
702         return result;
703     }
704 
705     MessageParcel dataParcel;
706     MessageParcel replyParcel;
707     MessageOption option(MessageOption::TF_SYNC);
708     if (!dataParcel.WriteInterfaceToken(SmsServiceProxy::GetDescriptor())) {
709         TELEPHONY_LOGE("GetBase64Encode WriteInterfaceToken is false");
710         return result;
711     }
712 
713     dataParcel.WriteString16(StringUtils::ToUtf16(src));
714 
715     sptr<IRemoteObject> remote = Remote();
716     if (remote == nullptr) {
717         return result;
718     }
719     remote->SendRequest(static_cast<int32_t>(SmsServiceInterfaceCode::MMS_BASE64_ENCODE), dataParcel,
720         replyParcel, option);
721     result = replyParcel.ReadBool();
722     TELEPHONY_LOGI("SmsServiceProxy::GetBase64Encode result:%{public}d", result);
723     if (!result) {
724         return result;
725     }
726     dest = StringUtils::ToUtf8(replyParcel.ReadString16());
727     return result;
728 }
729 
GetBase64Decode(std::string src,std::string & dest)730 bool SmsServiceProxy::GetBase64Decode(std::string src, std::string &dest)
731 {
732     bool result = false;
733     if (src.empty()) {
734         return result;
735     }
736 
737     MessageParcel dataParcel;
738     MessageParcel replyParcel;
739     MessageOption option(MessageOption::TF_SYNC);
740     if (!dataParcel.WriteInterfaceToken(SmsServiceProxy::GetDescriptor())) {
741         TELEPHONY_LOGE("GetBase64Decode WriteInterfaceToken is false");
742         return result;
743     }
744 
745     dataParcel.WriteString16(StringUtils::ToUtf16(src));
746 
747     sptr<IRemoteObject> remote = Remote();
748     if (remote == nullptr) {
749         return result;
750     }
751     remote->SendRequest(static_cast<int32_t>(SmsServiceInterfaceCode::MMS_BASE64_DECODE), dataParcel,
752         replyParcel, option);
753     result = replyParcel.ReadBool();
754     TELEPHONY_LOGI("SmsServiceProxy::GetBase64Decode result:%{public}d", result);
755     if (!result) {
756         return result;
757     }
758     dest = StringUtils::ToUtf8(replyParcel.ReadString16());
759     return result;
760 }
761 
GetEncodeStringFunc(std::string & encodeString,uint32_t charset,uint32_t valLength,std::string strEncodeString)762 bool SmsServiceProxy::GetEncodeStringFunc(
763     std::string &encodeString, uint32_t charset, uint32_t valLength, std::string strEncodeString)
764 {
765     bool result = false;
766     if (strEncodeString.empty()) {
767         return result;
768     }
769 
770     MessageParcel dataParcel;
771     MessageParcel replyParcel;
772     MessageOption option(MessageOption::TF_SYNC);
773     if (!dataParcel.WriteInterfaceToken(SmsServiceProxy::GetDescriptor())) {
774         TELEPHONY_LOGE("GetEncodeStringFunc WriteInterfaceToken is false");
775         return result;
776     }
777 
778     dataParcel.WriteUint32(charset);
779     dataParcel.WriteUint32(valLength);
780     dataParcel.WriteString16(StringUtils::ToUtf16(strEncodeString));
781 
782     sptr<IRemoteObject> remote = Remote();
783     if (remote == nullptr) {
784         return result;
785     }
786     remote->SendRequest(static_cast<int32_t>(SmsServiceInterfaceCode::GET_ENCODE_STRING), dataParcel,
787         replyParcel, option);
788     result = replyParcel.ReadBool();
789     TELEPHONY_LOGI("SmsServiceProxy::GetEncodeStringFunc result:%{public}d", result);
790     if (!result) {
791         return result;
792     }
793     encodeString = StringUtils::ToUtf8(replyParcel.ReadString16());
794     return result;
795 }
796 
797 bool SmsServiceDeathRecipient::gotDeathRecipient_ = false;
798 
GotDeathRecipient()799 bool SmsServiceDeathRecipient::GotDeathRecipient()
800 {
801     return gotDeathRecipient_;
802 }
803 
OnRemoteDied(const wptr<IRemoteObject> & remote)804 void SmsServiceDeathRecipient::OnRemoteDied(const wptr<IRemoteObject> &remote)
805 {
806     gotDeathRecipient_ = true;
807 }
808 
SmsServiceDeathRecipient()809 SmsServiceDeathRecipient::SmsServiceDeathRecipient() {}
810 
~SmsServiceDeathRecipient()811 SmsServiceDeathRecipient::~SmsServiceDeathRecipient() {}
812 } // namespace Telephony
813 } // namespace OHOS
814