1 /*
2  * Copyright (C) 2021 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_sender.h"
17 
18 #include <cinttypes>
19 
20 #include "core_manager_inner.h"
21 #include "ims_sms_client.h"
22 #include "radio_event.h"
23 #include "securec.h"
24 #include "sms_hisysevent.h"
25 #include "string_utils.h"
26 #include "telephony_log_wrapper.h"
27 
28 namespace OHOS {
29 namespace Telephony {
30 using namespace std;
31 using namespace std::chrono;
32 int64_t SmsSender::msgRef64bit_ = 0;
33 std::unordered_map<int64_t, std::shared_ptr<SmsSendIndexer>> SmsSender::sendCacheMap_;
34 
SmsSender(int32_t slotId,function<void (shared_ptr<SmsSendIndexer>)> & sendRetryFun)35 SmsSender::SmsSender(int32_t slotId, function<void(shared_ptr<SmsSendIndexer>)> &sendRetryFun)
36     : TelEventHandler("SmsSender"), slotId_(slotId), sendRetryFun_(sendRetryFun)
37 {}
38 
~SmsSender()39 SmsSender::~SmsSender() {}
40 
ProcessEvent(const AppExecFwk::InnerEvent::Pointer & event)41 void SmsSender::ProcessEvent(const AppExecFwk::InnerEvent::Pointer &event)
42 {
43     if (event == nullptr) {
44         TELEPHONY_LOGE("event is nullptr");
45         return;
46     }
47     shared_ptr<SmsSendIndexer> smsIndexer = nullptr;
48     uint32_t eventId = event->GetInnerEventId();
49     TELEPHONY_LOGI("SmsSender::ProcessEvent eventId %{public}d", eventId);
50     switch (eventId) {
51         case RadioEvent::RADIO_SEND_SMS:
52         case RadioEvent::RADIO_SEND_CDMA_SMS:
53         case RadioEvent::RADIO_SEND_IMS_GSM_SMS:
54         case RadioEvent::RADIO_SEND_SMS_EXPECT_MORE: {
55             smsIndexer = FindCacheMapAndTransform(event);
56             HandleMessageResponse(smsIndexer);
57             break;
58         }
59         case MSG_SMS_RETRY_DELIVERY: {
60             smsIndexer = event->GetSharedObject<SmsSendIndexer>();
61             if (sendRetryFun_ != nullptr) {
62                 sendRetryFun_(smsIndexer);
63             }
64             break;
65         }
66         case RadioEvent::RADIO_SMS_STATUS: {
67             StatusReportAnalysis(event);
68             break;
69         }
70         case RadioEvent::RADIO_SET_IMS_SMS: {
71             StatusReportSetImsSms(event);
72             SyncSwitchISmsResponse();
73             break;
74         }
75         case RadioEvent::RADIO_GET_IMS_SMS: {
76             StatusReportGetImsSms(event);
77             SyncSwitchISmsResponse();
78             break;
79         }
80         default:
81             TELEPHONY_LOGE("SmsSender::ProcessEvent Unknown %{public}d", eventId);
82             break;
83     }
84 }
85 
SyncSwitchISmsResponse()86 void SmsSender::SyncSwitchISmsResponse()
87 {
88     std::unique_lock<std::mutex> lck(ctx_);
89     resIsSmsReady_ = true;
90     TELEPHONY_LOGI("resIsSmsReady_ = %{public}d", resIsSmsReady_);
91     cv_.notify_one();
92 }
93 
HandleMessageResponse(const shared_ptr<SmsSendIndexer> & smsIndexer)94 void SmsSender::HandleMessageResponse(const shared_ptr<SmsSendIndexer> &smsIndexer)
95 {
96     if (smsIndexer == nullptr) {
97         TELEPHONY_LOGE("smsIndexer is nullptr");
98         return;
99     }
100     if (!SendCacheMapEraseItem(smsIndexer->GetMsgRefId64Bit())) {
101         TELEPHONY_LOGE("SendCacheMapEraseItem fail !!!!!");
102     }
103     SendCacheMapTimeoutCheck();
104     if (!smsIndexer->GetIsFailure()) {
105         if (smsIndexer->GetDeliveryCallback() != nullptr) {
106             // Expecting a status report.  Add it to the list.
107             if (reportList_.size() > MAX_REPORT_LIST_LIMIT) {
108                 reportList_.pop_front();
109             }
110             reportList_.push_back(smsIndexer);
111         }
112         SendMessageSucceed(smsIndexer);
113     } else {
114         HandleResend(smsIndexer);
115     }
116 }
117 
SendMessageSucceed(const shared_ptr<SmsSendIndexer> & smsIndexer)118 void SmsSender::SendMessageSucceed(const shared_ptr<SmsSendIndexer> &smsIndexer)
119 {
120     if (smsIndexer == nullptr) {
121         TELEPHONY_LOGE("SendMessageSucceed but smsIndexer drop!");
122         return;
123     }
124 
125     bool isLastPart = false;
126     uint8_t unSentCellCount = smsIndexer->GetUnSentCellCount();
127     if (unSentCellCount == 0) {
128         isLastPart = true;
129     }
130     TELEPHONY_LOGI("isLastPart:%{public}d", isLastPart);
131     if (isLastPart) {
132         smsIndexer->SetPsResendCount(INITIAL_COUNT);
133         smsIndexer->SetCsResendCount(INITIAL_COUNT);
134         ISendShortMessageCallback::SmsSendResult messageType = ISendShortMessageCallback::SEND_SMS_SUCCESS;
135         if (smsIndexer->GetHasCellFailed() != nullptr) {
136             if (*smsIndexer->GetHasCellFailed()) {
137                 messageType = ISendShortMessageCallback::SEND_SMS_FAILURE_UNKNOWN;
138             }
139         }
140         SendResultCallBack(smsIndexer->GetSendCallback(), messageType);
141         if (messageType == ISendShortMessageCallback::SEND_SMS_SUCCESS) {
142             SmsHiSysEvent::WriteSmsSendBehaviorEvent(slotId_, SmsMmsMessageType::SMS_SHORT_MESSAGE);
143         }
144     }
145 }
146 
SendMessageFailed(const shared_ptr<SmsSendIndexer> & smsIndexer)147 void SmsSender::SendMessageFailed(const shared_ptr<SmsSendIndexer> &smsIndexer)
148 {
149     if (smsIndexer == nullptr) {
150         TELEPHONY_LOGE("smsIndexer is nullptr");
151         return;
152     }
153     shared_ptr<bool> hasCellFailed = smsIndexer->GetHasCellFailed();
154     if (hasCellFailed != nullptr) {
155         *hasCellFailed = true;
156     }
157 
158     bool isLastPart = false;
159     uint8_t unSentCellCount = smsIndexer->GetUnSentCellCount();
160     if (unSentCellCount == 0) {
161         isLastPart = true;
162     }
163     TELEPHONY_LOGI("isLastPart:%{public}d", isLastPart);
164     if (isLastPart) {
165         smsIndexer->SetPsResendCount(INITIAL_COUNT);
166         smsIndexer->SetCsResendCount(INITIAL_COUNT);
167         // save to db and update state
168         sptr<ISendShortMessageCallback> sendCallback = smsIndexer->GetSendCallback();
169         SendResultCallBack(sendCallback, ISendShortMessageCallback::SEND_SMS_FAILURE_UNKNOWN);
170         TELEPHONY_LOGE("send sms result fail from ril response");
171         SmsHiSysEvent::WriteSmsSendFaultEvent(slotId_, SmsMmsMessageType::SMS_SHORT_MESSAGE,
172             SmsMmsErrorCode::SMS_ERROR_SEND_RESULT_FAIL, "send sms result fail from ril response");
173     }
174 }
175 
SendResultCallBack(const std::shared_ptr<SmsSendIndexer> & indexer,ISendShortMessageCallback::SmsSendResult result)176 void SmsSender::SendResultCallBack(
177     const std::shared_ptr<SmsSendIndexer> &indexer, ISendShortMessageCallback::SmsSendResult result)
178 {
179     if (indexer != nullptr && indexer->GetSendCallback() != nullptr) {
180         indexer->GetSendCallback()->OnSmsSendResult(result);
181     }
182 }
183 
SendResultCallBack(const sptr<ISendShortMessageCallback> & sendCallback,ISendShortMessageCallback::SmsSendResult result)184 void SmsSender::SendResultCallBack(
185     const sptr<ISendShortMessageCallback> &sendCallback, ISendShortMessageCallback::SmsSendResult result)
186 {
187     if (sendCallback != nullptr) {
188         sendCallback->OnSmsSendResult(result);
189     }
190 }
191 
SendCacheMapTimeoutCheck()192 void SmsSender::SendCacheMapTimeoutCheck()
193 {
194     std::lock_guard<std::mutex> guard(sendCacheMapMutex_);
195     system_clock::duration timePoint = system_clock::now().time_since_epoch();
196     seconds sec = duration_cast<seconds>(timePoint);
197     int64_t timeStamp = sec.count();
198     auto item = sendCacheMap_.begin();
199     while (item != sendCacheMap_.end()) {
200         auto iter = item++;
201         shared_ptr<SmsSendIndexer> &indexer = iter->second;
202         if (indexer == nullptr || (timeStamp - indexer->GetTimeStamp()) > EXPIRED_TIME) {
203             sendCacheMap_.erase(iter);
204         }
205     }
206 }
207 
SendCacheMapLimitCheck(const sptr<ISendShortMessageCallback> & sendCallback)208 bool SmsSender::SendCacheMapLimitCheck(const sptr<ISendShortMessageCallback> &sendCallback)
209 {
210     std::lock_guard<std::mutex> guard(sendCacheMapMutex_);
211     if (sendCacheMap_.size() > MSG_QUEUE_LIMIT) {
212         SendResultCallBack(sendCallback, ISendShortMessageCallback::SEND_SMS_FAILURE_UNKNOWN);
213         return true;
214     }
215     return false;
216 }
217 
SendCacheMapAddItem(int64_t id,const std::shared_ptr<SmsSendIndexer> & smsIndexer)218 bool SmsSender::SendCacheMapAddItem(int64_t id, const std::shared_ptr<SmsSendIndexer> &smsIndexer)
219 {
220     std::lock_guard<std::mutex> guard(sendCacheMapMutex_);
221     if (smsIndexer != nullptr) {
222         auto result = sendCacheMap_.emplace(id, smsIndexer);
223         return result.second;
224     }
225     return false;
226 }
227 
SendCacheMapEraseItem(int64_t id)228 bool SmsSender::SendCacheMapEraseItem(int64_t id)
229 {
230     std::lock_guard<std::mutex> guard(sendCacheMapMutex_);
231     return (sendCacheMap_.erase(id) != 0);
232 }
233 
FindCacheMapAndTransform(const AppExecFwk::InnerEvent::Pointer & event)234 std::shared_ptr<SmsSendIndexer> SmsSender::FindCacheMapAndTransform(const AppExecFwk::InnerEvent::Pointer &event)
235 {
236     if (event == nullptr) {
237         TELEPHONY_LOGE("event is nullptr");
238         return nullptr;
239     }
240     for (auto const &pair : sendCacheMap_) {
241         TELEPHONY_LOGI("Key = %{public}" PRId64 "", pair.first);
242     }
243     std::shared_ptr<SmsSendIndexer> smsIndexer = nullptr;
244     std::lock_guard<std::mutex> guard(sendCacheMapMutex_);
245     std::shared_ptr<RadioResponseInfo> res = event->GetSharedObject<RadioResponseInfo>();
246     if (res != nullptr) {
247         TELEPHONY_LOGI("flag = %{public}d", res->flag);
248         auto iter = sendCacheMap_.find(res->flag);
249         if (iter != sendCacheMap_.end()) {
250             smsIndexer = iter->second;
251             if (smsIndexer == nullptr) {
252                 TELEPHONY_LOGE("smsIndexer is nullptr");
253                 return nullptr;
254             }
255             smsIndexer->SetErrorCode(ISendShortMessageCallback::SEND_SMS_FAILURE_UNKNOWN);
256             smsIndexer->SetMsgRefId64Bit(res->flag);
257             smsIndexer->SetIsFailure(true);
258             UpdateUnSentCellCount(smsIndexer->GetMsgRefId());
259         }
260         return smsIndexer;
261     }
262     std::shared_ptr<SendSmsResultInfo> info = event->GetSharedObject<SendSmsResultInfo>();
263     if (info != nullptr) {
264         TELEPHONY_LOGI("flag = %{public}" PRId64 "", info->flag);
265         auto iter = sendCacheMap_.find(info->flag);
266         if (iter != sendCacheMap_.end()) {
267             TELEPHONY_LOGI("msgRef = %{public}d", info->msgRef);
268             smsIndexer = iter->second;
269             if (smsIndexer == nullptr) {
270                 TELEPHONY_LOGE("smsIndexer is nullptr");
271                 return nullptr;
272             }
273             smsIndexer->SetAckPdu(std::move(StringUtils::HexToByteVector(info->pdu)));
274             info->errCode != 0? smsIndexer->SetIsFailure(true) : smsIndexer->SetIsFailure(false);
275             smsIndexer->SetErrorCode(info->errCode);
276             smsIndexer->SetMsgRefId64Bit(info->flag);
277             UpdateUnSentCellCount(smsIndexer->GetMsgRefId());
278         }
279     }
280     return smsIndexer;
281 }
282 
UpdateUnSentCellCount(uint8_t refId)283 void SmsSender::UpdateUnSentCellCount(uint8_t refId)
284 {
285     std::shared_ptr<SmsSendIndexer> smsIndexer = nullptr;
286     for (auto it = sendCacheMap_.begin(); it != sendCacheMap_.end(); ++it) {
287         smsIndexer = it->second;
288         if (smsIndexer == nullptr) {
289             continue;
290         }
291         uint8_t unSentCount = smsIndexer->GetUnSentCellCount();
292         if (smsIndexer->GetMsgRefId() == refId && unSentCount > 0) {
293             smsIndexer->SetUnSentCellCount(unSentCount - 1);
294         }
295     }
296 }
297 
SetImsSmsConfig(int32_t slotId,int32_t enable)298 bool SmsSender::SetImsSmsConfig(int32_t slotId, int32_t enable)
299 {
300     auto smsClient = DelayedSingleton<ImsSmsClient>::GetInstance();
301     if (smsClient == nullptr) {
302         TELEPHONY_LOGE("SetImsSmsConfig return, ImsSmsClient is nullptr.");
303         return false;
304     }
305     imsSmsCfg_ = enable;
306     std::unique_lock<std::mutex> lck(ctx_);
307     resIsSmsReady_ = false;
308 
309     int32_t reply = smsClient->ImsSetSmsConfig(slotId, enable);
310     TELEPHONY_LOGI("SetImsSmsConfig reply = %{public}d", reply);
311     while (resIsSmsReady_) {
312         TELEPHONY_LOGI("SetImsSmsConfig::wait(), resIsSmsReady_ = false");
313         if (cv_.wait_for(lck, std::chrono::seconds(WAIT_TIME_SECOND)) == std::cv_status::timeout) {
314             break;
315         }
316     }
317     TELEPHONY_LOGI("SmsSender::SetImsSmsConfig(), %{public}d:", imsSmsCfg_);
318     return true;
319 }
320 
HandleResend(const std::shared_ptr<SmsSendIndexer> & smsIndexer)321 void SmsSender::HandleResend(const std::shared_ptr<SmsSendIndexer> &smsIndexer)
322 {
323     if (smsIndexer == nullptr) {
324         TELEPHONY_LOGE("smsIndexer is nullptr");
325         return;
326     }
327     // resending mechanism
328     bool errorCode = false;
329     if ((smsIndexer->GetErrorCode() == static_cast<int32_t>(ErrType::ERR_GENERIC_FAILURE)) ||
330         (smsIndexer->GetErrorCode() == static_cast<int32_t>(ErrType::ERR_CMD_SEND_FAILURE))) {
331         errorCode = true;
332     }
333     bool csResend = false;
334     if (!lastSmsDomain_ && smsIndexer->GetCsResendCount() < MAX_SEND_RETRIES) {
335         csResend = true;
336     }
337     bool psResend = false;
338     if (lastSmsDomain_ && smsIndexer->GetPsResendCount() <= MAX_SEND_RETRIES) {
339         psResend = true;
340     }
341     if (errorCode && (csResend || psResend)) {
342         if (lastSmsDomain_ && psResend) {
343             smsIndexer->SetPsResendCount(smsIndexer->GetPsResendCount() + 1);
344             SendEvent(MSG_SMS_RETRY_DELIVERY, smsIndexer, DELAY_MAX_TIME_MSCE);
345         } else if (csResend) {
346             smsIndexer->SetCsResendCount(smsIndexer->GetCsResendCount() + 1);
347             SendEvent(MSG_SMS_RETRY_DELIVERY, smsIndexer, DELAY_MAX_TIME_MSCE);
348         }
349     } else {
350         SendMessageFailed(smsIndexer);
351     }
352 }
353 
GetMsgRef8Bit()354 uint8_t SmsSender::GetMsgRef8Bit()
355 {
356     return ++msgRef8bit_;
357 }
358 
GetMsgRef64Bit()359 int64_t SmsSender::GetMsgRef64Bit()
360 {
361     return ++msgRef64bit_;
362 }
363 
SetNetworkState(bool isImsNetDomain,int32_t voiceServiceState)364 void SmsSender::SetNetworkState(bool isImsNetDomain, int32_t voiceServiceState)
365 {
366     isImsNetDomain_ = isImsNetDomain;
367     voiceServiceState_ = voiceServiceState;
368     TELEPHONY_LOGD("isImsNetDomain = %{public}s voiceServiceState = %{public}d",
369         isImsNetDomain_ ? "true" : "false", voiceServiceState_);
370 }
371 
CheckForce7BitEncodeType()372 bool SmsSender::CheckForce7BitEncodeType()
373 {
374     auto helperPtr = DelayedSingleton<SmsPersistHelper>::GetInstance();
375     if (helperPtr == nullptr) {
376         TELEPHONY_LOGE("Check User Force 7Bit Encode Type helperPtr nullptr error.");
377         return false;
378     }
379     return helperPtr->QueryParamBoolean(SmsPersistHelper::SMS_ENCODING_PARAM_KEY, false);
380 }
381 
GetSmsCodingNationalType(int slotId)382 SmsCodingNationalType SmsSender::GetSmsCodingNationalType(int slotId)
383 {
384     SmsCodingNationalType smsCodingNational = SMS_CODING_NATIONAL_TYPE_DEFAULT;
385     OperatorConfig operatorConfig;
386     CoreManagerInner::GetInstance().GetOperatorConfigs(slotId, operatorConfig);
387     if (operatorConfig.intValue.find(KEY_SMS_CODING_NATIONAL_INT) != operatorConfig.intValue.end()) {
388         smsCodingNational = (SmsCodingNationalType)operatorConfig.intValue[KEY_SMS_CODING_NATIONAL_INT];
389     }
390     return smsCodingNational;
391 }
392 
GetNetworkId()393 std::optional<int32_t> SmsSender::GetNetworkId()
394 {
395     return networkId_;
396 }
397 
SetNetworkId(std::optional<int32_t> & id)398 void SmsSender::SetNetworkId(std::optional<int32_t> &id)
399 {
400     networkId_ = id;
401 }
402 
OnRilAdapterHostDied()403 void SmsSender::OnRilAdapterHostDied()
404 {
405     std::shared_ptr<SmsSendIndexer> smsIndexer = nullptr;
406     for (auto it = sendCacheMap_.begin(); it != sendCacheMap_.end(); ++it) {
407         smsIndexer = it->second;
408         if (smsIndexer == nullptr || smsIndexer->GetIsFailure()) {
409             TELEPHONY_LOGE("smsIndexer is nullptr");
410             continue;
411         }
412         if (!SendCacheMapEraseItem(smsIndexer->GetMsgRefId64Bit())) {
413             TELEPHONY_LOGE("SendCacheMapEraseItem fail !!!!!");
414         }
415         smsIndexer->SetIsFailure(true);
416         smsIndexer->SetPsResendCount(INITIAL_COUNT);
417         smsIndexer->SetCsResendCount(INITIAL_COUNT);
418         sptr<ISendShortMessageCallback> sendCallback = smsIndexer->GetSendCallback();
419         SendResultCallBack(sendCallback, ISendShortMessageCallback::SEND_SMS_FAILURE_UNKNOWN);
420         TELEPHONY_LOGI("Message(s) failed to send due to RIL died.");
421     }
422 }
423 
CharArrayToString(const uint8_t * data,uint32_t dataLen,std::string & dataStr)424 void SmsSender::CharArrayToString(const uint8_t *data, uint32_t dataLen, std::string &dataStr)
425 {
426     uint32_t indexData = 0;
427     while (indexData < dataLen) {
428         dataStr += data[indexData];
429         indexData++;
430     }
431 }
432 
DataBasedSmsDeliverySplitPage(GsmSmsMessage & gsmSmsMessage,std::vector<struct SplitInfo> cellsInfos,std::shared_ptr<struct SmsTpdu> tpdu,uint8_t msgRef8bit,const std::string & desAddr,const std::string & scAddr,int32_t port,const sptr<ISendShortMessageCallback> & sendCallback,const sptr<IDeliveryShortMessageCallback> & deliveryCallback)433 void SmsSender::DataBasedSmsDeliverySplitPage(GsmSmsMessage &gsmSmsMessage, std::vector<struct SplitInfo> cellsInfos,
434     std::shared_ptr<struct SmsTpdu> tpdu, uint8_t msgRef8bit, const std::string &desAddr, const std::string &scAddr,
435     int32_t port, const sptr<ISendShortMessageCallback> &sendCallback,
436     const sptr<IDeliveryShortMessageCallback> &deliveryCallback)
437 {
438     uint32_t cellsInfosSize = static_cast<uint32_t>(cellsInfos.size());
439     for (uint32_t indexData = 0; indexData < cellsInfosSize; indexData++) {
440         const uint8_t *dataItem = reinterpret_cast<uint8_t *>(cellsInfos[indexData].text.data());
441         uint32_t dataItemLen = static_cast<uint32_t>(cellsInfos[indexData].text.size());
442 
443         std::shared_ptr<SmsSendIndexer> indexer =
444             make_shared<SmsSendIndexer>(desAddr, scAddr, port, dataItem, dataItemLen, sendCallback, deliveryCallback);
445         if (indexer == nullptr) {
446             SendResultCallBack(indexer, ISendShortMessageCallback::SEND_SMS_FAILURE_UNKNOWN);
447             TELEPHONY_LOGE("create SmsSendIndexer nullptr");
448             return;
449         }
450 
451         (void)memset_s(tpdu->data.submit.userData.data, MAX_USER_DATA_LEN + 1, 0x00, MAX_USER_DATA_LEN + 1);
452         if (cellsInfos[indexData].encodeData.size() > MAX_USER_DATA_LEN + 1) {
453             TELEPHONY_LOGE("data length invalid.");
454             return;
455         }
456         int ret = memcpy_s(tpdu->data.submit.userData.data, MAX_USER_DATA_LEN + 1, &cellsInfos[indexData].encodeData[0],
457             cellsInfos[indexData].encodeData.size());
458         if (ret != EOK) {
459             SendResultCallBack(indexer, ISendShortMessageCallback::SEND_SMS_FAILURE_UNKNOWN);
460             TELEPHONY_LOGE("ret:%{public}d", ret);
461             return;
462         }
463         DataBasedSmsDeliveryPacketSplitPage(gsmSmsMessage, tpdu, msgRef8bit, indexData, port, scAddr, sendCallback,
464             deliveryCallback, indexer, cellsInfos);
465     }
466 }
467 
DataBasedSmsDeliveryPacketSplitPage(GsmSmsMessage & gsmSmsMessage,std::shared_ptr<struct SmsTpdu> tpdu,uint8_t msgRef8bit,uint32_t indexData,int32_t port,const std::string & scAddr,const sptr<ISendShortMessageCallback> & sendCallback,const sptr<IDeliveryShortMessageCallback> & deliveryCallback,std::shared_ptr<SmsSendIndexer> indexer,std::vector<struct SplitInfo> cellsInfos)468 void SmsSender::DataBasedSmsDeliveryPacketSplitPage(GsmSmsMessage &gsmSmsMessage, std::shared_ptr<struct SmsTpdu> tpdu,
469     uint8_t msgRef8bit, uint32_t indexData, int32_t port, const std::string &scAddr,
470     const sptr<ISendShortMessageCallback> &sendCallback, const sptr<IDeliveryShortMessageCallback> &deliveryCallback,
471     std::shared_ptr<SmsSendIndexer> indexer, std::vector<struct SplitInfo> cellsInfos)
472 {
473     tpdu->data.submit.userData.length = static_cast<int>(cellsInfos[indexData].encodeData.size());
474     tpdu->data.submit.userData.data[cellsInfos[indexData].encodeData.size()] = 0;
475     tpdu->data.submit.msgRef = msgRef8bit;
476     int headerCnt = 0;
477     uint32_t cellsInfosSize = static_cast<uint32_t>(cellsInfos.size());
478     if (cellsInfosSize > 1) {
479         indexer->SetIsConcat(true);
480         SmsConcat concat;
481         concat.is8Bits = true;
482         concat.msgRef = msgRef8bit;
483         concat.totalSeg = static_cast<uint16_t>(cellsInfosSize);
484         concat.seqNum = static_cast<uint16_t>(indexData + 1);
485         indexer->SetSmsConcat(concat);
486         headerCnt += gsmSmsMessage.SetHeaderConcat(headerCnt, concat);
487     }
488     if (headerCnt >= MAX_UD_HEADER_NUM) {
489         TELEPHONY_LOGE("PDU header Array out of bounds");
490         return;
491     }
492 
493     tpdu->data.submit.userData.header[headerCnt].udhType = UDH_APP_PORT_16BIT;
494     tpdu->data.submit.userData.header[headerCnt].udh.appPort16bit.destPort = ((unsigned short)port & 0xFFFF);
495     tpdu->data.submit.userData.header[headerCnt].udh.appPort16bit.originPort = 0;
496     headerCnt++;
497     tpdu->data.submit.bHeaderInd = (headerCnt > 0) ? true : false;
498     /* Set User Data Header for Alternate Reply Address */
499     headerCnt += gsmSmsMessage.SetHeaderReply(headerCnt);
500     /* Set User Data Header for National Language Single Shift */
501     DataCodingScheme pCodingType = DATA_CODING_7BIT;
502     MSG_LANGUAGE_ID_T langId = MSG_ID_RESERVED_LANG;
503     headerCnt += gsmSmsMessage.SetHeaderLang(headerCnt, pCodingType, langId);
504     tpdu->data.submit.userData.headerCnt = headerCnt;
505 
506     std::shared_ptr<struct EncodeInfo> encodeInfo = gsmSmsMessage.GetSubmitEncodeInfo(scAddr, false);
507     if (encodeInfo == nullptr) {
508         SendResultCallBack(sendCallback, ISendShortMessageCallback::SEND_SMS_FAILURE_UNKNOWN);
509         TELEPHONY_LOGE("encodeInfo nullptr error.");
510         SmsHiSysEvent::WriteSmsSendFaultEvent(slotId_, SmsMmsMessageType::SMS_SHORT_MESSAGE,
511             SmsMmsErrorCode::SMS_ERROR_PDU_ENCODEING_FAIL, "data sms gsm encodeInfo nullptr error");
512         return;
513     }
514 
515     if (cellsInfosSize > 1 && indexData < (cellsInfosSize - 1)) {
516         tpdu->data.submit.bStatusReport = false;
517     } else {
518         tpdu->data.submit.bStatusReport = (deliveryCallback == nullptr) ? false : true;
519     }
520     encodeInfo->isMore_ = (cellsInfosSize > 1) ? true : false;
521     DataBasedSmsDeliverySendSplitPage(encodeInfo, sendCallback, indexer, msgRef8bit, cellsInfosSize);
522 }
523 
SendCallbackExceptionCase(const sptr<ISendShortMessageCallback> & sendCallback,std::string str)524 void SmsSender::SendCallbackExceptionCase(const sptr<ISendShortMessageCallback> &sendCallback, std::string str)
525 {
526     SendResultCallBack(sendCallback, ISendShortMessageCallback::SEND_SMS_FAILURE_UNKNOWN);
527     TELEPHONY_LOGE("%{public}s tpdu nullptr error.", str.c_str());
528     SmsHiSysEvent::WriteSmsSendFaultEvent(slotId_, SmsMmsMessageType::SMS_SHORT_MESSAGE,
529         SmsMmsErrorCode::SMS_ERROR_PDU_ENCODEING_FAIL, "data sms gsm encodeInfo nullptr error");
530 }
531 
DataBasedSmsDeliverySendSplitPage(std::shared_ptr<struct EncodeInfo> encodeInfo,const sptr<ISendShortMessageCallback> & sendCallback,shared_ptr<SmsSendIndexer> indexer,uint8_t msgRef8bit,uint32_t cellsInfosSize)532 void SmsSender::DataBasedSmsDeliverySendSplitPage(std::shared_ptr<struct EncodeInfo> encodeInfo,
533     const sptr<ISendShortMessageCallback> &sendCallback, shared_ptr<SmsSendIndexer> indexer, uint8_t msgRef8bit,
534     uint32_t cellsInfosSize)
535 {
536     std::vector<uint8_t> smca(encodeInfo->smcaData_, encodeInfo->smcaData_ + encodeInfo->smcaLen);
537     std::vector<uint8_t> pdu(encodeInfo->tpduData_, encodeInfo->tpduData_ + encodeInfo->tpduLen);
538     TELEPHONY_LOGE("cellsInfosSize:%{public}d", cellsInfosSize);
539     std::shared_ptr<uint8_t> unSentCellCount = make_shared<uint8_t>(cellsInfosSize);
540     std::shared_ptr<bool> hasCellFailed = make_shared<bool>(false);
541     if (unSentCellCount == nullptr || hasCellFailed == nullptr) {
542         SendResultCallBack(sendCallback, ISendShortMessageCallback::SEND_SMS_FAILURE_UNKNOWN);
543         return;
544     }
545 
546     chrono::system_clock::duration timePoint = chrono::system_clock::now().time_since_epoch();
547     int64_t timeStamp = chrono::duration_cast<chrono::seconds>(timePoint).count();
548     indexer->SetUnSentCellCount(*unSentCellCount);
549     indexer->SetHasCellFailed(hasCellFailed);
550     indexer->SetEncodeSmca(std::move(smca));
551     indexer->SetEncodePdu(std::move(pdu));
552     indexer->SetHasMore(encodeInfo->isMore_);
553     indexer->SetMsgRefId(msgRef8bit);
554     indexer->SetNetWorkType(NET_TYPE_GSM);
555     indexer->SetTimeStamp(timeStamp);
556     SendSmsToRil(indexer);
557 }
558 } // namespace Telephony
559 } // namespace OHOS
560