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_receive_handler.h"
17
18 #include "gsm_sms_message.h"
19 #include "radio_event.h"
20 #include "sms_hisysevent.h"
21 #include "sms_persist_helper.h"
22 #include "sms_receive_reliability_handler.h"
23 #include "telephony_log_wrapper.h"
24
25 namespace OHOS {
26 namespace Telephony {
27 using namespace std;
28 constexpr static uint16_t PDU_POS_OFFSET = 1;
29 constexpr static uint8_t SMS_TYPE_GSM = 1;
30 constexpr static uint8_t SMS_TYPE_CDMA = 2;
31
SmsReceiveHandler(const std::shared_ptr<AppExecFwk::EventRunner> & runner,int32_t slotId)32 SmsReceiveHandler::SmsReceiveHandler(const std::shared_ptr<AppExecFwk::EventRunner> &runner, int32_t slotId)
33 : AppExecFwk::EventHandler(runner), slotId_(slotId)
34 {
35 smsWapPushHandler_ = std::make_unique<SmsWapPushHandler>(slotId);
36 if (smsWapPushHandler_ == nullptr) {
37 TELEPHONY_LOGE("make sms wapPush Hander error.");
38 }
39 }
40
~SmsReceiveHandler()41 SmsReceiveHandler::~SmsReceiveHandler() {}
42
ProcessEvent(const AppExecFwk::InnerEvent::Pointer & event)43 void SmsReceiveHandler::ProcessEvent(const AppExecFwk::InnerEvent::Pointer &event)
44 {
45 if (event == nullptr) {
46 TELEPHONY_LOGE("SmsReceiveHandler::ProcessEvent event == nullptr");
47 return;
48 }
49
50 uint32_t eventId = 0;
51 eventId = event->GetInnerEventId();
52 TELEPHONY_LOGI("SmsReceiveHandler::ProcessEvent eventId = %{public}d", eventId);
53 switch (eventId) {
54 case RadioEvent::RADIO_GSM_SMS:
55 case RadioEvent::RADIO_CDMA_SMS: {
56 std::shared_ptr<SmsBaseMessage> message = nullptr;
57 message = TransformMessageInfo(event->GetSharedObject<SmsMessageInfo>());
58 if (message != nullptr) {
59 TELEPHONY_LOGI("[raw pdu] =%{private}s", StringUtils::StringToHex(message->GetRawPdu()).c_str());
60 }
61 HandleReceivedSms(message);
62 break;
63 }
64 default:
65 TELEPHONY_LOGE("SmsReceiveHandler::ProcessEvent Unknown eventId %{public}d", eventId);
66 break;
67 }
68 }
69
HandleReceivedSms(const std::shared_ptr<SmsBaseMessage> smsBaseMessage)70 void SmsReceiveHandler::HandleReceivedSms(const std::shared_ptr<SmsBaseMessage> smsBaseMessage)
71 {
72 if (smsBaseMessage == nullptr) {
73 TELEPHONY_LOGE("smsBaseMessage is nullptr");
74 return;
75 }
76 ReplySmsToSmsc(HandleSmsByType(smsBaseMessage), nullptr);
77 }
78
CombineMessagePart(const std::shared_ptr<SmsReceiveIndexer> & indexer)79 void SmsReceiveHandler::CombineMessagePart(const std::shared_ptr<SmsReceiveIndexer> &indexer)
80 {
81 std::shared_ptr<vector<string>> pdus = make_shared<vector<string>>();
82 if ((indexer == nullptr) || (pdus == nullptr)) {
83 TELEPHONY_LOGE("indexer or pdus is nullptr");
84 return;
85 }
86 auto reliabilityHandler = std::make_shared<SmsReceiveReliabilityHandler>(slotId_);
87 if ((reliabilityHandler == nullptr)) {
88 TELEPHONY_LOGE("reliabilityHandler is nullptr");
89 return;
90 }
91 std::string messagBody;
92 std::string userDataRaw;
93 if (indexer->IsSingleMsg()) {
94 string pdu = StringUtils::StringToHex(indexer->GetPdu());
95 messagBody.append(indexer->GetVisibleMessageBody());
96 userDataRaw.append(indexer->GetRawUserData());
97 pdus->push_back(pdu);
98 } else {
99 pdus->assign(MAX_SEGMENT_NUM, "");
100 int msgSeg = static_cast<int>(indexer->GetMsgCount());
101 int8_t count = 0;
102 int8_t notNullPart = msgSeg;
103
104 std::vector<SmsReceiveIndexer> dbIndexers;
105 DataShare::DataSharePredicates predicates;
106 predicates.EqualTo(SmsSubsection::SENDER_NUMBER, indexer->GetOriginatingAddress())
107 ->And()
108 ->EqualTo(SmsSubsection::SMS_SUBSECTION_ID, std::to_string(indexer->GetMsgRefId()))
109 ->And()
110 ->EqualTo(SmsSubsection::SIZE, std::to_string(indexer->GetMsgCount()));
111 DelayedSingleton<SmsPersistHelper>::GetInstance()->Query(predicates, dbIndexers);
112
113 for (const auto &v : dbIndexers) {
114 ++count;
115 string pdu = StringUtils::StringToHex(v.GetPdu());
116 if ((v.GetMsgSeqId() - PDU_POS_OFFSET >= MAX_SEGMENT_NUM) || (v.GetMsgSeqId() - PDU_POS_OFFSET < 0)) {
117 reliabilityHandler->DeleteMessageFormDb(indexer->GetMsgRefId());
118 return;
119 }
120 pdus->at(v.GetMsgSeqId() - PDU_POS_OFFSET) = pdu;
121 if (v.GetPdu().size() == 0) {
122 --notNullPart;
123 }
124 std::shared_ptr<SmsBaseMessage> baseMessage = GsmSmsMessage::CreateMessage(pdu);
125 if (baseMessage != nullptr) {
126 userDataRaw.append(baseMessage->GetRawUserData());
127 messagBody.append(baseMessage->GetVisibleMessageBody());
128 }
129 }
130 if ((count != msgSeg) || (pdus->empty()) || (notNullPart != msgSeg)) {
131 return;
132 }
133 }
134
135 indexer->SetVisibleMessageBody(messagBody);
136 indexer->SetRawUserData(userDataRaw);
137
138 if (reliabilityHandler->CheckBlockedPhoneNumber(indexer->GetOriginatingAddress())) {
139 TELEPHONY_LOGI("indexer display address is block");
140 return;
141 }
142 if (indexer->GetIsWapPushMsg()) {
143 if (smsWapPushHandler_ != nullptr) {
144 userDataRaw = indexer->GetWapPusRawUserData();
145 if (!smsWapPushHandler_->DecodeWapPushPdu(indexer, userDataRaw)) {
146 SmsHiSysEvent::WriteSmsReceiveFaultEvent(slotId_, SmsMmsMessageType::WAP_PUSH,
147 SmsMmsErrorCode::SMS_ERROR_PDU_DECODE_FAIL, "Wap push decode wap push fail");
148 }
149 }
150 return;
151 }
152 reliabilityHandler->SendBroadcast(indexer, pdus);
153 }
154
IsRepeatedMessagePart(const shared_ptr<SmsReceiveIndexer> & smsIndexer)155 bool SmsReceiveHandler::IsRepeatedMessagePart(const shared_ptr<SmsReceiveIndexer> &smsIndexer)
156 {
157 if (smsIndexer != nullptr) {
158 std::vector<SmsReceiveIndexer> dbIndexers;
159 DataShare::DataSharePredicates predicates;
160 predicates.EqualTo(SmsSubsection::SENDER_NUMBER, smsIndexer->GetOriginatingAddress())
161 ->And()
162 ->EqualTo(SmsSubsection::SMS_SUBSECTION_ID, std::to_string(smsIndexer->GetMsgRefId()))
163 ->And()
164 ->EqualTo(SmsSubsection::SIZE, std::to_string(smsIndexer->GetMsgCount()));
165 DelayedSingleton<SmsPersistHelper>::GetInstance()->Query(predicates, dbIndexers);
166
167 for (const auto &it : dbIndexers) {
168 if (it.GetMsgSeqId() == smsIndexer->GetMsgSeqId()) {
169 return true;
170 }
171 }
172 }
173 return false;
174 }
175
AddMsgToDB(const std::shared_ptr<SmsReceiveIndexer> indexer)176 bool SmsReceiveHandler::AddMsgToDB(const std::shared_ptr<SmsReceiveIndexer> indexer)
177 {
178 if (indexer == nullptr) {
179 TELEPHONY_LOGE("indexer is nullptr.");
180 return false;
181 }
182
183 DataShare::DataShareValuesBucket bucket;
184 bucket.Put(SmsSubsection::SLOT_ID, std::to_string(slotId_));
185 bucket.Put(SmsSubsection::RECEIVER_NUMBER, indexer->GetOriginatingAddress());
186 bucket.Put(SmsSubsection::SENDER_NUMBER, indexer->GetOriginatingAddress());
187 bucket.Put(SmsSubsection::START_TIME, std::to_string(indexer->GetTimestamp()));
188 bucket.Put(SmsSubsection::END_TIME, std::to_string(indexer->GetTimestamp()));
189 bucket.Put(SmsSubsection::REW_PUD, StringUtils::StringToHex(indexer->GetPdu()));
190
191 bucket.Put(SmsSubsection::FORMAT, indexer->GetIsCdma() ? SMS_TYPE_CDMA : SMS_TYPE_GSM);
192 bucket.Put(SmsSubsection::DEST_PORT, indexer->GetDestPort());
193 bucket.Put(SmsSubsection::SMS_SUBSECTION_ID, indexer->GetMsgRefId());
194 bucket.Put(SmsSubsection::SIZE, indexer->GetMsgCount());
195 bucket.Put(SmsSubsection::SUBSECTION_INDEX, indexer->GetMsgSeqId());
196 uint16_t dataBaseId = 0;
197 bool ret = DelayedSingleton<SmsPersistHelper>::GetInstance()->Insert(bucket, dataBaseId);
198 indexer->SetDataBaseId(dataBaseId);
199 if (!ret) {
200 SmsHiSysEvent::WriteSmsReceiveFaultEvent(slotId_, SmsMmsMessageType::SMS_SHORT_MESSAGE,
201 SmsMmsErrorCode::SMS_ERROR_ADD_TO_DATABASE_FAIL, "add msg to database error");
202 }
203 return ret;
204 }
205 } // namespace Telephony
206 } // namespace OHOS