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