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