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