• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "common_event.h"
19 #include "common_event_manager.h"
20 #include "common_event_support.h"
21 #include "gsm_sms_message.h"
22 #include "radio_event.h"
23 #include "singleton.h"
24 #include "sms_hisysevent.h"
25 #include "string_utils.h"
26 #include "telephony_log_wrapper.h"
27 #include "telephony_permission.h"
28 #include "want.h"
29 
30 namespace OHOS {
31 namespace Telephony {
32 using namespace std;
33 using namespace EventFwk;
SmsReceiveHandler(const std::shared_ptr<AppExecFwk::EventRunner> & runner,int32_t slotId)34 SmsReceiveHandler::SmsReceiveHandler(const std::shared_ptr<AppExecFwk::EventRunner> &runner, int32_t slotId)
35     : AppExecFwk::EventHandler(runner), slotId_(slotId)
36 {
37     smsWapPushHandler_ = std::make_unique<SmsWapPushHandler>(slotId);
38     if (smsWapPushHandler_ == nullptr) {
39         TELEPHONY_LOGE("make sms wapPush Hander error.");
40     }
41 }
42 
~SmsReceiveHandler()43 SmsReceiveHandler::~SmsReceiveHandler() {}
44 
ProcessEvent(const AppExecFwk::InnerEvent::Pointer & event)45 void SmsReceiveHandler::ProcessEvent(const AppExecFwk::InnerEvent::Pointer &event)
46 {
47     if (event == nullptr) {
48         TELEPHONY_LOGE("SmsReceiveHandler::ProcessEvent event == nullptr");
49         return;
50     }
51 
52     uint32_t eventId = 0;
53     eventId = event->GetInnerEventId();
54     TELEPHONY_LOGI("SmsReceiveHandler::ProcessEvent eventId = %{public}d", eventId);
55     switch (eventId) {
56         case RadioEvent::RADIO_GSM_SMS:
57         case RadioEvent::RADIO_CDMA_SMS: {
58             std::shared_ptr<SmsBaseMessage> message = nullptr;
59             message = TransformMessageInfo(event->GetSharedObject<SmsMessageInfo>());
60             if (message != nullptr) {
61                 TELEPHONY_LOGI("[raw pdu] =%{private}s", StringUtils::StringToHex(message->GetRawPdu()).c_str());
62             }
63             HandleReceivedSms(message);
64             break;
65         }
66         default:
67             TELEPHONY_LOGE("SmsReceiveHandler::ProcessEvent Unknown eventId %{public}d", eventId);
68             break;
69     }
70 }
71 
HandleReceivedSms(const std::shared_ptr<SmsBaseMessage> & smsBaseMessage)72 void SmsReceiveHandler::HandleReceivedSms(const std::shared_ptr<SmsBaseMessage> &smsBaseMessage)
73 {
74     if (smsBaseMessage == nullptr) {
75         TELEPHONY_LOGE("smsBaseMessage is nullptr");
76         return;
77     }
78     ReplySmsToSmsc(HandleSmsByType(smsBaseMessage), nullptr);
79 }
80 
CombineMessagePart(const std::shared_ptr<SmsReceiveIndexer> & indexer)81 void SmsReceiveHandler::CombineMessagePart(const std::shared_ptr<SmsReceiveIndexer> &indexer)
82 {
83     std::shared_ptr<vector<string>> pdus = make_shared<vector<string>>();
84     if ((indexer == nullptr) || (pdus == nullptr)) {
85         TELEPHONY_LOGE("indexer or pdus is nullptr");
86         return;
87     }
88     std::string messagBody;
89     std::string userDataRaw;
90     if (indexer->IsSingleMsg()) {
91         string pdu = StringUtils::StringToHex(indexer->GetPdu());
92         messagBody.append(indexer->GetVisibleMessageBody());
93         userDataRaw.append(indexer->GetRawUserData());
94         pdus->push_back(pdu);
95     } else {
96         pdus->assign(MAX_SEGMENT_NUM, "");
97         int msgSeg = static_cast<int>(indexer->GetMsgCount());
98         int8_t count = 0;
99         int8_t notNullPart = msgSeg;
100 
101         std::vector<SmsReceiveIndexer> dbIndexers;
102         NativeRdb::DataAbilityPredicates predicates;
103         predicates.EqualTo(SmsMmsData::SENDER_NUMBER, indexer->GetOriginatingAddress())
104             ->And()
105             ->EqualTo(SmsMmsData::SMS_SUBSECTION_ID, std::to_string(indexer->GetMsgRefId()))
106             ->And()
107             ->EqualTo(SmsMmsData::SIZE, std::to_string(indexer->GetMsgCount()));
108         DelayedSingleton<SmsPersistHelper>::GetInstance()->Query(predicates, dbIndexers);
109 
110         for (const auto &v : dbIndexers) {
111             ++count;
112             string pdu = StringUtils::StringToHex(v.GetPdu());
113             if ((v.GetMsgSeqId() - PDU_POS_OFFSET >= MAX_SEGMENT_NUM) || (v.GetMsgSeqId() - PDU_POS_OFFSET < 0)) {
114                 DeleteMessageFormDb(indexer);
115                 return;
116             }
117             pdus->at(v.GetMsgSeqId() - PDU_POS_OFFSET) = pdu;
118             if (v.GetPdu().size() == 0) {
119                 --notNullPart;
120             }
121             std::shared_ptr<SmsBaseMessage> baseMessage = GsmSmsMessage::CreateMessage(pdu);
122             if (baseMessage != nullptr) {
123                 userDataRaw.append(baseMessage->GetRawUserData());
124                 messagBody.append(baseMessage->GetVisibleMessageBody());
125             }
126         }
127         if ((count != msgSeg) || (pdus->empty()) || (notNullPart != msgSeg)) {
128             return;
129         }
130     }
131 
132     indexer->SetVisibleMessageBody(messagBody);
133     indexer->SetRawUserData(userDataRaw);
134     DeleteMessageFormDb(indexer);
135     if (CheckBlockPhone(indexer)) {
136         TELEPHONY_LOGI("indexer display address is block");
137         return;
138     }
139     if (indexer->GetIsWapPushMsg()) {
140         if (smsWapPushHandler_ != nullptr) {
141             if (!smsWapPushHandler_->DecodeWapPushPdu(userDataRaw)) {
142                 SmsHiSysEvent::WriteSmsReceiveFaultEvent(slotId_, SmsMmsMessageType::WAP_PUSH,
143                     SmsMmsErrorCode::SMS_ERROR_PDU_DECODE_FAIL, "Wap push decode wap push fail");
144             }
145         }
146         return;
147     }
148     SendBroadcast(indexer, pdus);
149 }
150 
DeleteMessageFormDb(const std::shared_ptr<SmsReceiveIndexer> & smsIndexer)151 void SmsReceiveHandler::DeleteMessageFormDb(const std::shared_ptr<SmsReceiveIndexer> &smsIndexer)
152 {
153     if (smsIndexer == nullptr) {
154         TELEPHONY_LOGE("smsIndexer is nullptr");
155         return;
156     }
157 
158     NativeRdb::DataAbilityPredicates predicates;
159     predicates.EqualTo(SmsMmsData::SMS_SUBSECTION_ID, std::to_string(smsIndexer->GetMsgRefId()));
160     DelayedSingleton<SmsPersistHelper>::GetInstance()->Delete(predicates);
161 }
162 
IsRepeatedMessagePart(const shared_ptr<SmsReceiveIndexer> & smsIndexer)163 bool SmsReceiveHandler::IsRepeatedMessagePart(const shared_ptr<SmsReceiveIndexer> &smsIndexer)
164 {
165     if (smsIndexer != nullptr) {
166         std::vector<SmsReceiveIndexer> dbIndexers;
167         NativeRdb::DataAbilityPredicates predicates;
168         predicates.EqualTo(SmsMmsData::SENDER_NUMBER, smsIndexer->GetOriginatingAddress())
169             ->And()
170             ->EqualTo(SmsMmsData::SMS_SUBSECTION_ID, std::to_string(smsIndexer->GetMsgRefId()))
171             ->And()
172             ->EqualTo(SmsMmsData::SIZE, std::to_string(smsIndexer->GetMsgCount()));
173         DelayedSingleton<SmsPersistHelper>::GetInstance()->Query(predicates, dbIndexers);
174 
175         for (const auto &it : dbIndexers) {
176             if (it.GetMsgSeqId() == smsIndexer->GetMsgSeqId()) {
177                 return true;
178             }
179         }
180     }
181     return false;
182 }
183 
SendBroadcast(const std::shared_ptr<SmsReceiveIndexer> & indexer,const shared_ptr<vector<string>> & pdus)184 void SmsReceiveHandler::SendBroadcast(
185     const std::shared_ptr<SmsReceiveIndexer> &indexer, const shared_ptr<vector<string>> &pdus)
186 {
187     if (pdus == nullptr || indexer == nullptr) {
188         TELEPHONY_LOGE("pdus is nullptr");
189         return;
190     }
191     std::vector<std::string> newPdus;
192     for (const auto &it : *pdus) {
193         if (!it.empty()) {
194             newPdus.emplace_back(it);
195         }
196     }
197     Want want;
198     want.SetAction(CommonEventSupport::COMMON_EVENT_SMS_RECEIVE_COMPLETED);
199     want.SetParam("slotId", static_cast<int>(slotId_));
200     want.SetParam("pdus", newPdus);
201     want.SetParam("isCdma", indexer->GetIsCdma());
202     CommonEventData data;
203     data.SetWant(want);
204     if (indexer->GetIsText()) {
205         data.SetData("TEXT_SMS_RECEIVE");
206         data.SetCode(TEXT_MSG_RECEIVE_CODE);
207     } else {
208         data.SetData("DATA_SMS_RECEIVE");
209         data.SetCode(DATA_MSG_RECEIVE_CODE);
210         want.SetParam("port", static_cast<short>(indexer->GetDestPort()));
211     }
212     CommonEventPublishInfo publishInfo;
213     publishInfo.SetOrdered(true);
214     std::vector<std::string> smsPermissions;
215     smsPermissions.emplace_back(Permission::RECEIVE_MESSAGES);
216     publishInfo.SetSubscriberPermissions(smsPermissions);
217     bool publishResult = CommonEventManager::PublishCommonEvent(data, publishInfo, nullptr);
218     if (!publishResult) {
219         TELEPHONY_LOGE("SendBroadcast PublishBroadcastEvent result fail");
220         SmsHiSysEvent::WriteSmsReceiveFaultEvent(slotId_, SmsMmsMessageType::SMS_SHORT_MESSAGE,
221             SmsMmsErrorCode::SMS_ERROR_PUBLISH_COMMON_EVENT_FAIL, "publish short message broadcast event fail");
222     }
223     DelayedSingleton<SmsHiSysEvent>::GetInstance()->SetSmsBroadcastStartTime();
224 }
225 
AddMsgToDB(const std::shared_ptr<SmsReceiveIndexer> & indexer)226 bool SmsReceiveHandler::AddMsgToDB(const std::shared_ptr<SmsReceiveIndexer> &indexer)
227 {
228     if (indexer == nullptr) {
229         TELEPHONY_LOGE("indexer is nullptr.");
230         return false;
231     }
232     const uint8_t gsm = 1;
233     const uint8_t cdma = 2;
234     NativeRdb::ValuesBucket bucket;
235     bucket.PutString(SmsMmsData::SLOT_ID, std::to_string(slotId_));
236     bucket.PutString(SmsMmsData::RECEIVER_NUMBER, indexer->GetOriginatingAddress());
237     bucket.PutString(SmsMmsData::SENDER_NUMBER, indexer->GetOriginatingAddress());
238     bucket.PutString(SmsMmsData::START_TIME, std::to_string(indexer->GetTimestamp()));
239     bucket.PutString(SmsMmsData::END_TIME, std::to_string(indexer->GetTimestamp()));
240     bucket.PutString(SmsMmsData::RAW_PUD, StringUtils::StringToHex(indexer->GetPdu()));
241 
242     bucket.PutInt(SmsMmsData::FORMAT, indexer->GetIsCdma() ? cdma : gsm);
243     bucket.PutInt(SmsMmsData::DEST_PORT, indexer->GetDestPort());
244     bucket.PutInt(SmsMmsData::SMS_SUBSECTION_ID, indexer->GetMsgRefId());
245     bucket.PutInt(SmsMmsData::SIZE, indexer->GetMsgCount());
246     bucket.PutInt(SmsMmsData::SUBSECTION_INDEX, indexer->GetMsgSeqId());
247     bool ret = DelayedSingleton<SmsPersistHelper>::GetInstance()->Insert(bucket);
248     if (!ret) {
249         SmsHiSysEvent::WriteSmsReceiveFaultEvent(slotId_, SmsMmsMessageType::SMS_SHORT_MESSAGE,
250             SmsMmsErrorCode::SMS_ERROR_ADD_TO_DATABASE_FAIL, "add msg to database error");
251     }
252     return ret;
253 }
254 
CheckBlockPhone(const std::shared_ptr<SmsReceiveIndexer> & indexer)255 bool SmsReceiveHandler::CheckBlockPhone(const std::shared_ptr<SmsReceiveIndexer> &indexer)
256 {
257     if (indexer == nullptr) {
258         TELEPHONY_LOGE("CheckBlockPhone sms indexer nullptr error.");
259         return false;
260     }
261     TELEPHONY_LOGD("indexer originating =%{private}s", indexer->GetOriginatingAddress().c_str());
262     return DelayedSingleton<SmsPersistHelper>::GetInstance()->QueryBlockPhoneNumber(
263         indexer->GetOriginatingAddress());
264 }
265 
CheckSmsCapable()266 bool SmsReceiveHandler::CheckSmsCapable()
267 {
268     auto helperPtr = DelayedSingleton<SmsPersistHelper>::GetInstance();
269     if (helperPtr == nullptr) {
270         return true;
271     }
272     return helperPtr->QueryParamBoolean(SmsPersistHelper::SMS_CAPABLE_PARAM_KEY, true);
273 }
274 } // namespace Telephony
275 } // namespace OHOS