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_interface_stub.h"
17
18#include "sms_interface_manager.h"
19#include "sms_receive_reliability_handler.h"
20#include "sms_service.h"
21#include "string_utils.h"
22#include "telephony_errors.h"
23#include "telephony_log_wrapper.h"
24#include "telephony_types.h"
25
26namespace OHOS {
27namespace Telephony {
28using namespace std;
29
30static inline bool IsValidSlotId(int32_t slotId)
31{
32    return ((slotId >= DEFAULT_SIM_SLOT_ID) && (slotId < SIM_SLOT_COUNT));
33}
34
35SmsInterfaceStub::SmsInterfaceStub()
36{
37    memberFuncMap_[SmsServiceInterfaceCode::TEXT_BASED_SMS_DELIVERY] = [this](MessageParcel &data,
38        MessageParcel &reply, MessageOption &option) { OnSendSmsTextRequest(data, reply, option); };
39    memberFuncMap_[SmsServiceInterfaceCode::SEND_SMS_TEXT_WITHOUT_SAVE] = [this](MessageParcel &data,
40        MessageParcel &reply, MessageOption &option) { OnSendSmsTextWithoutSaveRequest(data, reply, option); };
41    memberFuncMap_[SmsServiceInterfaceCode::DATA_BASED_SMS_DELIVERY] = [this](MessageParcel &data,
42        MessageParcel &reply, MessageOption &option) { OnSendSmsDataRequest(data, reply, option); };
43    memberFuncMap_[SmsServiceInterfaceCode::SET_SMSC_ADDRESS] = [this](MessageParcel &data,
44        MessageParcel &reply, MessageOption &option) { OnSetSmscAddr(data, reply, option); };
45    memberFuncMap_[SmsServiceInterfaceCode::GET_SMSC_ADDRESS] = [this](MessageParcel &data,
46        MessageParcel &reply, MessageOption &option) { OnGetSmscAddr(data, reply, option); };
47    memberFuncMap_[SmsServiceInterfaceCode::ADD_SIM_MESSAGE] = [this](MessageParcel &data,
48        MessageParcel &reply, MessageOption &option) { OnAddSimMessage(data, reply, option); };
49    memberFuncMap_[SmsServiceInterfaceCode::DEL_SIM_MESSAGE] = [this](MessageParcel &data,
50        MessageParcel &reply, MessageOption &option) { OnDelSimMessage(data, reply, option); };
51    memberFuncMap_[SmsServiceInterfaceCode::UPDATE_SIM_MESSAGE] = [this](MessageParcel &data,
52        MessageParcel &reply, MessageOption &option) { OnUpdateSimMessage(data, reply, option); };
53    memberFuncMap_[SmsServiceInterfaceCode::GET_ALL_SIM_MESSAGE] = [this](MessageParcel &data,
54        MessageParcel &reply, MessageOption &option) { OnGetAllSimMessages(data, reply, option); };
55    memberFuncMap_[SmsServiceInterfaceCode::SET_CB_CONFIG] = [this](MessageParcel &data,
56        MessageParcel &reply, MessageOption &option) { OnSetCBConfig(data, reply, option); };
57    memberFuncMap_[SmsServiceInterfaceCode::SET_IMS_SMS_CONFIG] = [this](MessageParcel &data,
58        MessageParcel &reply, MessageOption &option) { OnSetImsSmsConfig(data, reply, option); };
59    memberFuncMap_[SmsServiceInterfaceCode::SET_DEFAULT_SMS_SLOT_ID] = [this](MessageParcel &data,
60        MessageParcel &reply, MessageOption &option) { OnSetDefaultSmsSlotId(data, reply, option); };
61    memberFuncMap_[SmsServiceInterfaceCode::GET_DEFAULT_SMS_SLOT_ID] = [this](MessageParcel &data,
62        MessageParcel &reply, MessageOption &option) { OnGetDefaultSmsSlotId(data, reply, option); };
63    memberFuncMap_[SmsServiceInterfaceCode::GET_DEFAULT_SMS_SIM_ID] = [this](MessageParcel &data,
64        MessageParcel &reply, MessageOption &option) { OnGetDefaultSmsSimId(data, reply, option); };
65    memberFuncMap_[SmsServiceInterfaceCode::SPLIT_MESSAGE] = [this](MessageParcel &data,
66        MessageParcel &reply, MessageOption &option) { OnSplitMessage(data, reply, option); };
67    memberFuncMap_[SmsServiceInterfaceCode::GET_SMS_SEGMENTS_INFO] = [this](MessageParcel &data,
68        MessageParcel &reply, MessageOption &option) { OnGetSmsSegmentsInfo(data, reply, option); };
69    memberFuncMap_[SmsServiceInterfaceCode::GET_IMS_SHORT_MESSAGE_FORMAT] = [this](MessageParcel &data,
70        MessageParcel &reply, MessageOption &option) { OnGetImsShortMessageFormat(data, reply, option); };
71    memberFuncMap_[SmsServiceInterfaceCode::IS_IMS_SMS_SUPPORTED] = [this](MessageParcel &data,
72        MessageParcel &reply, MessageOption &option) { OnIsImsSmsSupported(data, reply, option); };
73    memberFuncMap_[SmsServiceInterfaceCode::HAS_SMS_CAPABILITY] = [this](MessageParcel &data,
74        MessageParcel &reply, MessageOption &option) { OnHasSmsCapability(data, reply, option); };
75    RegisterServiceCode();
76}
77
78void SmsInterfaceStub::RegisterServiceCode()
79{
80    memberFuncMap_[SmsServiceInterfaceCode::CREATE_MESSAGE] = [this](MessageParcel &data,
81        MessageParcel &reply, MessageOption &option) { OnCreateMessage(data, reply, option); };
82    memberFuncMap_[SmsServiceInterfaceCode::MMS_BASE64_ENCODE] = [this](MessageParcel &data,
83        MessageParcel &reply, MessageOption &option) { OnGetBase64Encode(data, reply, option); };
84    memberFuncMap_[SmsServiceInterfaceCode::MMS_BASE64_DECODE] = [this](MessageParcel &data,
85        MessageParcel &reply, MessageOption &option) { OnGetBase64Decode(data, reply, option); };
86    memberFuncMap_[SmsServiceInterfaceCode::GET_ENCODE_STRING] = [this](MessageParcel &data,
87        MessageParcel &reply, MessageOption &option) { OnGetEncodeStringFunc(data, reply, option); };
88    memberFuncMap_[SmsServiceInterfaceCode::SEND_MMS] = [this](MessageParcel &data,
89        MessageParcel &reply, MessageOption &option) { OnSendMms(data, reply, option); };
90    memberFuncMap_[SmsServiceInterfaceCode::DOWNLOAD_MMS] = [this](MessageParcel &data,
91        MessageParcel &reply, MessageOption &option) { OnDownloadMms(data, reply, option); };
92}
93
94SmsInterfaceStub::~SmsInterfaceStub()
95{
96    slotSmsInterfaceManagerMap_.clear();
97    memberFuncMap_.clear();
98}
99
100void SmsInterfaceStub::InitModule()
101{
102    static bool bInitModule = false;
103    if (bInitModule) {
104        return;
105    }
106    bInitModule = true;
107    std::lock_guard<std::mutex> lock(mutex_);
108    for (int32_t slotId = 0; slotId < SIM_SLOT_COUNT; ++slotId) {
109        slotSmsInterfaceManagerMap_[slotId] = std::make_shared<SmsInterfaceManager>(slotId);
110        if (slotSmsInterfaceManagerMap_[slotId] == nullptr) {
111            TELEPHONY_LOGE("SmsInterfaceStub InitModule slotSmsInterfaceManagerMap_[%{public}d] is nullptr", slotId);
112            return;
113        }
114        slotSmsInterfaceManagerMap_[slotId]->InitInterfaceManager();
115
116        TelFFRTUtils::Submit([slotId]() {
117            auto reliabilityHandler = std::make_shared<SmsReceiveReliabilityHandler>(slotId);
118            if (reliabilityHandler == nullptr) {
119                TELEPHONY_LOGE("reliabilityHandler nullptr");
120                return;
121            }
122            if (!reliabilityHandler->DeleteExpireSmsFromDB()) {
123                return;
124            }
125            if (!reliabilityHandler->CheckSmsCapable()) {
126                TELEPHONY_LOGE("sms receive capable unSupport");
127                return;
128            }
129            reliabilityHandler->SmsReceiveReliabilityProcessing();
130        });
131    }
132}
133
134std::shared_ptr<SmsInterfaceManager> SmsInterfaceStub::GetSmsInterfaceManager(int32_t slotId)
135{
136    std::lock_guard<std::mutex> lock(mutex_);
137    std::map<uint32_t, std::shared_ptr<SmsInterfaceManager>>::iterator iter =
138        slotSmsInterfaceManagerMap_.find(slotId);
139    if (iter != slotSmsInterfaceManagerMap_.end()) {
140        return iter->second;
141    }
142    return nullptr;
143}
144
145std::shared_ptr<SmsInterfaceManager> SmsInterfaceStub::GetSmsInterfaceManager()
146{
147    std::lock_guard<std::mutex> lock(mutex_);
148    for (const auto &iter : slotSmsInterfaceManagerMap_) {
149        if (iter.second != nullptr) {
150            return iter.second;
151        }
152    }
153    return nullptr;
154}
155
156void SmsInterfaceStub::OnSendSmsTextRequest(MessageParcel &data, MessageParcel &reply, MessageOption &option)
157{
158    sptr<ISendShortMessageCallback> sendCallback = nullptr;
159    sptr<IDeliveryShortMessageCallback> deliveryCallback = nullptr;
160    int32_t slotId = data.ReadInt32();
161    u16string desAddr = data.ReadString16();
162    u16string scAddr = data.ReadString16();
163    u16string text = data.ReadString16();
164    if (!IsValidSlotId(slotId)) {
165        TELEPHONY_LOGE("invalid slotId:%{public}d", slotId);
166        return;
167    }
168
169    sptr<IRemoteObject> remoteSendCallback = data.ReadRemoteObject();
170    sptr<IRemoteObject> remoteDeliveryCallback = data.ReadRemoteObject();
171    if (remoteSendCallback != nullptr) {
172        sendCallback = iface_cast<ISendShortMessageCallback>(remoteSendCallback);
173    }
174    if (remoteDeliveryCallback != nullptr) {
175        deliveryCallback = iface_cast<IDeliveryShortMessageCallback>(remoteDeliveryCallback);
176    }
177    std::string bundleName = data.ReadString();
178    bool isMmsApp = (bundleName == MMS_APP);
179    TELEPHONY_LOGI("MessageID::TEXT_BASED_SMS_DELIVERY %{public}d;isMmsApp:%{public}d;bundleName = %{public}s",
180        slotId, isMmsApp, bundleName.c_str());
181    RemoveSpacesInDesAddr(desAddr);
182    int32_t result = SendMessage(slotId, desAddr, scAddr, text, sendCallback, deliveryCallback, isMmsApp);
183    reply.WriteInt32(result);
184}
185
186void SmsInterfaceStub::OnSendSmsTextWithoutSaveRequest(MessageParcel &data, MessageParcel &reply,
187    MessageOption &option)
188{
189    sptr<ISendShortMessageCallback> sendCallback = nullptr;
190    sptr<IDeliveryShortMessageCallback> deliveryCallback = nullptr;
191    int32_t slotId = data.ReadInt32();
192    u16string desAddr = data.ReadString16();
193    u16string scAddr = data.ReadString16();
194    u16string text = data.ReadString16();
195    if (!IsValidSlotId(slotId)) {
196        TELEPHONY_LOGE("invalid slotId: %{public}d", slotId);
197        return;
198    }
199
200    sptr<IRemoteObject> remoteSendCallback = data.ReadRemoteObject();
201    sptr<IRemoteObject> remoteDeliveryCallback = data.ReadRemoteObject();
202    if (remoteSendCallback != nullptr) {
203        sendCallback = iface_cast<ISendShortMessageCallback>(remoteSendCallback);
204    }
205    if (remoteDeliveryCallback != nullptr) {
206        deliveryCallback = iface_cast<IDeliveryShortMessageCallback>(remoteDeliveryCallback);
207    }
208    TELEPHONY_LOGI("MessageID::SEND_SMS_TEXT_WITHOUT_SAVE %{public}d", slotId);
209    RemoveSpacesInDesAddr(desAddr);
210    std::string bundleName = data.ReadString();
211    TELEPHONY_LOGI("bundleName = %{public}s", bundleName.c_str());
212    int32_t result = SendMessageWithoutSave(slotId, desAddr, scAddr, text, sendCallback, deliveryCallback);
213    reply.WriteInt32(result);
214}
215
216void SmsInterfaceStub::OnSendSmsDataRequest(MessageParcel &data, MessageParcel &reply, MessageOption &option)
217{
218    sptr<ISendShortMessageCallback> sendCallback = nullptr;
219    sptr<IDeliveryShortMessageCallback> deliveryCallback = nullptr;
220    int32_t slotId = data.ReadInt32();
221    u16string desAddr = data.ReadString16();
222    u16string scAddr = data.ReadString16();
223    int16_t port = data.ReadInt16();
224    if (!IsValidSlotId(slotId)) {
225        TELEPHONY_LOGE("invalid slotId:%{public}d", slotId);
226        return;
227    }
228
229    sptr<IRemoteObject> remoteSendCallback = data.ReadRemoteObject();
230    sptr<IRemoteObject> remoteDeliveryCallback = data.ReadRemoteObject();
231    if (remoteSendCallback != nullptr) {
232        sendCallback = iface_cast<ISendShortMessageCallback>(remoteSendCallback);
233    }
234    if (remoteDeliveryCallback != nullptr) {
235        deliveryCallback = iface_cast<IDeliveryShortMessageCallback>(remoteDeliveryCallback);
236    }
237    int16_t dataLen = data.ReadInt16();
238    const uint8_t *buffer = reinterpret_cast<const uint8_t *>(data.ReadRawData(dataLen));
239    if (buffer == nullptr) {
240        return;
241    }
242    RemoveSpacesInDesAddr(desAddr);
243    int32_t result = SendMessage(slotId, desAddr, scAddr, port, buffer, dataLen, sendCallback, deliveryCallback);
244    reply.WriteInt32(result);
245}
246
247void SmsInterfaceStub::RemoveSpacesInDesAddr(std::u16string &desAddr)
248{
249    // Remove spaces in desAddr
250    if (desAddr.empty() || desAddr.size() >= MAX_ADDRESS_LEN) {
251        TELEPHONY_LOGE("RemoveSpacesInDesAddr desAddr is invalid");
252        return;
253    }
254
255    std::u16string storeAddr = desAddr;
256    int32_t count = static_cast<int32_t>(desAddr.size());
257    int32_t indexDes = 0;
258    int32_t indexResult = 0;
259    while (indexDes < count) {
260        if (desAddr[indexDes] != ' ') {
261            storeAddr[indexResult] = desAddr[indexDes];
262            indexResult++;
263        }
264        indexDes++;
265    }
266    desAddr = storeAddr.substr(0, indexResult);
267}
268
269void SmsInterfaceStub::OnSetSmscAddr(MessageParcel &data, MessageParcel &reply, MessageOption &option)
270{
271    int32_t slotId = data.ReadInt32();
272    std::u16string scAddr = data.ReadString16();
273    int32_t result = SetSmscAddr(slotId, scAddr);
274    TELEPHONY_LOGI("set smsc result:%{public}d", result == TELEPHONY_ERR_SUCCESS);
275    reply.WriteInt32(result);
276}
277
278void SmsInterfaceStub::OnGetSmscAddr(MessageParcel &data, MessageParcel &reply, MessageOption &option)
279{
280    std::u16string smscAddress;
281    int32_t slotId = data.ReadInt32();
282    int32_t result = GetSmscAddr(slotId, smscAddress);
283    if (!reply.WriteInt32(result)) {
284        TELEPHONY_LOGE("SmsInterfaceStub::OnGetSmscAddr write reply failed.");
285        return;
286    }
287    if (result != TELEPHONY_ERR_SUCCESS) {
288        TELEPHONY_LOGE("SmsInterfaceStub::OnGetSmscAddr result is not TELEPHONY_ERR_SUCCESS.");
289        return;
290    }
291
292    if (!reply.WriteString16(smscAddress)) {
293        TELEPHONY_LOGE("SmsInterfaceStub::OnGetSmscAddr write reply failed.");
294        return;
295    }
296}
297
298void SmsInterfaceStub::OnAddSimMessage(MessageParcel &data, MessageParcel &reply, MessageOption &option)
299{
300    int32_t slotId = data.ReadInt32();
301    std::u16string smsc = data.ReadString16();
302    std::u16string pdu = data.ReadString16();
303    uint32_t status = data.ReadUint32();
304    if (status > SIM_MESSAGE_STATUS_SENT || status < SIM_MESSAGE_STATUS_UNREAD) {
305        return;
306    }
307    int32_t result = AddSimMessage(slotId, smsc, pdu, static_cast<SimMessageStatus>(status));
308    TELEPHONY_LOGI("AddSimMessage result %{public}d", result);
309    reply.WriteInt32(result);
310}
311
312void SmsInterfaceStub::OnDelSimMessage(MessageParcel &data, MessageParcel &reply, MessageOption &option)
313{
314    int32_t slotId = data.ReadInt32();
315    uint32_t msgIndex = data.ReadUint32();
316    int32_t result = DelSimMessage(slotId, msgIndex);
317    TELEPHONY_LOGI("DelSimMessage result %{public}d", result);
318    reply.WriteInt32(result);
319}
320
321void SmsInterfaceStub::OnUpdateSimMessage(MessageParcel &data, MessageParcel &reply, MessageOption &option)
322{
323    int32_t slotId = data.ReadInt32();
324    uint32_t msgIndex = data.ReadUint32();
325    uint32_t newStatus = data.ReadUint32();
326    std::u16string pdu = data.ReadString16();
327    std::u16string smsc = data.ReadString16();
328    if (newStatus > SIM_MESSAGE_STATUS_SENT || newStatus < SIM_MESSAGE_STATUS_UNREAD) {
329        return;
330    }
331    int32_t result = UpdateSimMessage(slotId, msgIndex, static_cast<SimMessageStatus>(newStatus), pdu, smsc);
332    TELEPHONY_LOGI("UpdateSimMessage result %{public}d", result);
333    reply.WriteInt32(result);
334}
335
336void SmsInterfaceStub::OnGetAllSimMessages(MessageParcel &data, MessageParcel &reply, MessageOption &option)
337{
338    std::vector<ShortMessage> message;
339    int32_t slotId = data.ReadInt32();
340    int32_t result = GetAllSimMessages(slotId, message);
341    TELEPHONY_LOGI("GetAllSimMessages result %{public}d size %{public}zu", result, message.size());
342    reply.WriteInt32(result);
343    if (result != TELEPHONY_ERR_SUCCESS) {
344        TELEPHONY_LOGE("SmsInterfaceStub::OnGetSmscAddr result is not TELEPHONY_ERR_SUCCESS.");
345        return;
346    }
347    int32_t resultLen = static_cast<int32_t>(message.size());
348    reply.WriteInt32(resultLen);
349    for (const auto &v : message) {
350        v.Marshalling(reply);
351    }
352}
353
354void SmsInterfaceStub::OnSetCBConfig(MessageParcel &data, MessageParcel &reply, MessageOption &option)
355{
356    int32_t slotId = data.ReadInt32();
357    TELEPHONY_LOGD("set cb config slotId:%{public}d", slotId);
358    bool enable = data.ReadBool();
359    uint32_t fromMsgId = data.ReadUint32();
360    uint32_t toMsgId = data.ReadUint32();
361    uint8_t ranType = data.ReadUint8();
362    int32_t result = SetCBConfig(slotId, enable, fromMsgId, toMsgId, ranType);
363    if (result != TELEPHONY_ERR_SUCCESS) {
364        TELEPHONY_LOGE("OnSetCBConfig fail, result:%{public}d, slotId:%{public}d", result, slotId);
365    }
366    reply.WriteInt32(result);
367}
368
369void SmsInterfaceStub::OnSetImsSmsConfig(MessageParcel &data, MessageParcel &reply, MessageOption &option)
370{
371    bool result = false;
372    int32_t slotId = data.ReadInt32();
373    int32_t enable = data.ReadInt32();
374    result = SetImsSmsConfig(slotId, enable);
375    TELEPHONY_LOGI("SetImsSmsConfig result %{public}d", result);
376    reply.WriteBool(result);
377}
378
379void SmsInterfaceStub::OnSetDefaultSmsSlotId(MessageParcel &data, MessageParcel &reply, MessageOption &option)
380{
381    int32_t slotId = data.ReadInt32();
382    int32_t result = SetDefaultSmsSlotId(slotId);
383    TELEPHONY_LOGI("SetDefaultSmsSlotId result %{public}d", result);
384    reply.WriteInt32(result);
385}
386
387void SmsInterfaceStub::OnGetDefaultSmsSlotId(MessageParcel &data, MessageParcel &reply, MessageOption &option)
388{
389    int32_t result = 0;
390    result = GetDefaultSmsSlotId();
391    TELEPHONY_LOGI("SetDefaultSmsSlotId result %{public}d", result);
392    reply.WriteInt32(result);
393}
394
395void SmsInterfaceStub::OnGetDefaultSmsSimId(MessageParcel &data, MessageParcel &reply, MessageOption &option)
396{
397    int32_t result = 0;
398    int32_t simId = 0;
399    result = GetDefaultSmsSimId(simId);
400    if (!reply.WriteInt32(result)) {
401        TELEPHONY_LOGE("write int32 reply failed.");
402        return;
403    }
404    if (result != TELEPHONY_ERR_SUCCESS) {
405        TELEPHONY_LOGE("result %{public}d", result);
406        return;
407    }
408    if (!reply.WriteInt32(simId)) {
409        TELEPHONY_LOGE("write int32 reply failed.");
410        return;
411    }
412}
413
414void SmsInterfaceStub::OnSplitMessage(MessageParcel &data, MessageParcel &reply, MessageOption &option)
415{
416    std::vector<std::u16string> splitMessage;
417    std::u16string message = data.ReadString16();
418    int32_t result = SplitMessage(message, splitMessage);
419    reply.WriteInt32(result);
420    if (result != TELEPHONY_ERR_SUCCESS) {
421        TELEPHONY_LOGE("SmsInterfaceStub::OnSplitMessage result is not TELEPHONY_ERR_SUCCESS.");
422        return;
423    }
424    int32_t resultLen = static_cast<int32_t>(splitMessage.size());
425    TELEPHONY_LOGI("SplitMessage size %{public}d", resultLen);
426    reply.WriteInt32(resultLen);
427    for (const auto &item : splitMessage) {
428        reply.WriteString16(item);
429    }
430}
431
432void SmsInterfaceStub::OnGetSmsSegmentsInfo(MessageParcel &data, MessageParcel &reply, MessageOption &option)
433{
434    int32_t slotId = data.ReadInt32();
435    std::u16string message = data.ReadString16();
436    bool force7BitCode = data.ReadBool();
437
438    SmsSegmentsInfo segInfo;
439    int32_t result = GetSmsSegmentsInfo(slotId, message, force7BitCode, segInfo);
440    reply.WriteInt32(result);
441
442    if (result == TELEPHONY_ERR_SUCCESS) {
443        reply.WriteInt32(segInfo.msgSegCount);
444        reply.WriteInt32(segInfo.msgEncodingCount);
445        reply.WriteInt32(segInfo.msgRemainCount);
446        reply.WriteInt32(static_cast<int32_t>(segInfo.msgCodeScheme));
447    }
448}
449
450void SmsInterfaceStub::OnIsImsSmsSupported(MessageParcel &data, MessageParcel &reply, MessageOption &option)
451{
452    int32_t slotId = data.ReadInt32();
453    bool isSupported = false;
454    int32_t result = IsImsSmsSupported(slotId, isSupported);
455    if (!reply.WriteInt32(result)) {
456        TELEPHONY_LOGE("SmsInterfaceStub::OnIsImsSmsSupported write reply failed.");
457        return;
458    }
459    if (result != TELEPHONY_ERR_SUCCESS) {
460        TELEPHONY_LOGE("SmsInterfaceStub::OnIsImsSmsSupported result is not TELEPHONY_ERR_SUCCESS.");
461        return;
462    }
463    if (!reply.WriteBool(isSupported)) {
464        TELEPHONY_LOGE("SmsInterfaceStub::OnIsImsSmsSupported write reply failed.");
465        return;
466    }
467}
468
469void SmsInterfaceStub::OnGetImsShortMessageFormat(MessageParcel &data, MessageParcel &reply, MessageOption &option)
470{
471    std::u16string format;
472    int32_t result = GetImsShortMessageFormat(format);
473    reply.WriteInt32(result);
474    if (result != TELEPHONY_ERR_SUCCESS) {
475        TELEPHONY_LOGE("SmsInterfaceStub::OnGetImsShortMessageFormat result is not TELEPHONY_ERR_SUCCESS.");
476        return;
477    }
478    reply.WriteString16(format);
479}
480
481void SmsInterfaceStub::OnHasSmsCapability(MessageParcel &data, MessageParcel &reply, MessageOption &option)
482{
483    reply.WriteBool(HasSmsCapability());
484}
485
486void SmsInterfaceStub::OnCreateMessage(MessageParcel &data, MessageParcel &reply, MessageOption &option)
487{
488    std::string pdu = data.ReadString();
489    std::string specification = data.ReadString();
490    ShortMessage message;
491    int32_t result = CreateMessage(pdu, specification, message);
492
493    reply.WriteInt32(result);
494    if (result != TELEPHONY_ERR_SUCCESS) {
495        return;
496    }
497    if (!message.Marshalling(reply)) {
498        TELEPHONY_LOGE("SmsInterfaceStub::OnCreateMessage fail");
499    }
500}
501
502void SmsInterfaceStub::OnGetBase64Encode(MessageParcel &data, MessageParcel &reply, MessageOption &option)
503{
504    bool result = false;
505
506    u16string src = data.ReadString16();
507    std::string dest;
508    result = GetBase64Encode(StringUtils::ToUtf8(src), dest);
509    reply.WriteBool(result);
510    if (!result) {
511        return;
512    }
513    reply.WriteString16(StringUtils::ToUtf16(dest));
514}
515
516void SmsInterfaceStub::OnGetBase64Decode(MessageParcel &data, MessageParcel &reply, MessageOption &option)
517{
518    bool result = false;
519    u16string src = data.ReadString16();
520    std::string dest;
521    result = GetBase64Decode(StringUtils::ToUtf8(src), dest);
522    reply.WriteBool(result);
523    if (!result) {
524        return;
525    }
526    reply.WriteString16(StringUtils::ToUtf16(dest));
527}
528
529void SmsInterfaceStub::OnGetEncodeStringFunc(MessageParcel &data, MessageParcel &reply, MessageOption &option)
530{
531    bool result = false;
532    uint32_t charset = data.ReadUint32();
533    uint32_t valLength = data.ReadUint32();
534    u16string strEncodeString = data.ReadString16();
535    std::string str = StringUtils::ToUtf8(strEncodeString);
536    std::string encodeString;
537
538    if (valLength != str.length()) {
539        TELEPHONY_LOGE("invalid valLength!");
540        return;
541    }
542    result = GetEncodeStringFunc(encodeString, charset, valLength, str);
543    reply.WriteBool(result);
544    if (!result) {
545        return;
546    }
547    reply.WriteString16(StringUtils::ToUtf16(encodeString));
548}
549
550void SmsInterfaceStub::OnSendMms(MessageParcel &data, MessageParcel &reply, MessageOption &option)
551{
552    int32_t slotId = data.ReadInt32();
553    TELEPHONY_LOGI("send mms slotId:%{public}d", slotId);
554    u16string mmsc = data.ReadString16();
555    u16string mmsData = data.ReadString16();
556    u16string ua = data.ReadString16();
557    u16string uaprof = data.ReadString16();
558    int64_t time = data.ReadInt64();
559    std::string bundleName = data.ReadString();
560    TELEPHONY_LOGI("SmsInterfaceStub::OnSendMms read time stamp :%{public}s;bundleName:%{public}s",
561        std::to_string(time).c_str(), bundleName.c_str());
562    bool isMmsApp = (bundleName == MMS_APP);
563    int32_t result = SendMms(slotId, mmsc, mmsData, ua, uaprof, time, isMmsApp);
564    if (!reply.WriteInt32(result)) {
565        TELEPHONY_LOGE("SmsInterfaceStub::OnSendMms write reply failed");
566        return;
567    }
568}
569
570void SmsInterfaceStub::OnDownloadMms(MessageParcel &data, MessageParcel &reply, MessageOption &option)
571{
572    int32_t slotId = data.ReadInt32();
573    TELEPHONY_LOGI("download mms slotId:%{public}d", slotId);
574    u16string mmsc = data.ReadString16();
575    u16string mmsData = data.ReadString16();
576    u16string ua = data.ReadString16();
577    u16string uaprof = data.ReadString16();
578    int32_t result = DownloadMms(slotId, mmsc, mmsData, ua, uaprof);
579    if (!reply.WriteInt32(result)) {
580        TELEPHONY_LOGE("SmsInterfaceStub::OnDownloadMms write reply failed");
581        return;
582    }
583    reply.WriteString16(mmsData);
584    TELEPHONY_LOGI("SmsInterfaceStub::OnDownloadMms dbUrls:%{public}s", StringUtils::ToUtf8(mmsData).c_str());
585}
586
587int SmsInterfaceStub::OnRemoteRequest(
588    uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option)
589{
590    TELEPHONY_LOGD("SmsInterfaceStub::OnRemoteRequest code:%{public}d", code);
591    std::u16string myDescripter = SmsInterfaceStub::GetDescriptor();
592    std::u16string remoteDescripter = data.ReadInterfaceToken();
593    if (myDescripter != remoteDescripter) {
594        TELEPHONY_LOGE("descriptor checked fail");
595        return TELEPHONY_ERR_DESCRIPTOR_MISMATCH;
596    }
597
598    auto itFunc = memberFuncMap_.find(static_cast<SmsServiceInterfaceCode>(code));
599    if (itFunc != memberFuncMap_.end()) {
600        auto memberFunc = itFunc->second;
601        if (memberFunc != nullptr) {
602            memberFunc(data, reply, option);
603            return TELEPHONY_ERR_SUCCESS;
604        }
605    }
606    return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
607}
608} // namespace Telephony
609} // namespace OHOS
610