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 "gsm_sms_receive_handler.h"
17
18 #include "core_manager_inner.h"
19 #include "gsm_sms_message.h"
20 #include "radio_event.h"
21 #include "runner_pool.h"
22 #include "sms_common.h"
23 #include "sms_hisysevent.h"
24 #include "sms_receive_reliability_handler.h"
25 #include "telephony_log_wrapper.h"
26
27 namespace OHOS {
28 namespace Telephony {
29 using namespace std;
GsmSmsReceiveHandler(const shared_ptr<AppExecFwk::EventRunner> & runner,int32_t slotId)30 GsmSmsReceiveHandler::GsmSmsReceiveHandler(const shared_ptr<AppExecFwk::EventRunner> &runner, int32_t slotId)
31 : SmsReceiveHandler(runner, slotId)
32 {}
33
~GsmSmsReceiveHandler()34 GsmSmsReceiveHandler::~GsmSmsReceiveHandler()
35 {
36 if (smsCbHandler_ != nullptr) {
37 smsCbHandler_->UnRegisterHandler();
38 }
39 }
40
Init()41 void GsmSmsReceiveHandler::Init()
42 {
43 if (!RegisterHandler()) {
44 TELEPHONY_LOGI("GsmSmsSender::Init Register RADIO_SMS_STATUS fail.");
45 }
46 smsCbRunner_ = RunnerPool::GetInstance().GetSmsCommonRunner();
47 if (smsCbRunner_ == nullptr) {
48 TELEPHONY_LOGE("failed to create GsmSmsCbHandler");
49 return;
50 }
51 smsCbHandler_ = std::make_shared<GsmSmsCbHandler>(smsCbRunner_, slotId_);
52 if (smsCbHandler_ == nullptr) {
53 TELEPHONY_LOGE("failed to create GsmSmsCbHandler");
54 return;
55 }
56 smsCbHandler_->Init();
57 TELEPHONY_LOGI("smsCbHandler_->Run().");
58 }
59
RegisterHandler()60 bool GsmSmsReceiveHandler::RegisterHandler()
61 {
62 TELEPHONY_LOGI("GsmSmsReceiveHandler::RegisteHandler Register RADIO_GSM_SMS ok.");
63 CoreManagerInner::GetInstance().RegisterCoreNotify(
64 slotId_, shared_from_this(), RadioEvent::RADIO_GSM_SMS, nullptr);
65 return true;
66 }
67
UnRegisterHandler()68 void GsmSmsReceiveHandler::UnRegisterHandler()
69 {
70 TELEPHONY_LOGI("SmsReceiveHandler::UnRegisterHandler::slotId= %{public}d", slotId_);
71 CoreManagerInner::GetInstance().UnRegisterCoreNotify(
72 slotId_, shared_from_this(), RadioEvent::RADIO_GSM_SMS);
73 if (smsCbHandler_ != nullptr) {
74 smsCbRunner_->Stop();
75 }
76 }
77
HandleSmsByType(const shared_ptr<SmsBaseMessage> smsBaseMessage)78 int32_t GsmSmsReceiveHandler::HandleSmsByType(const shared_ptr<SmsBaseMessage> smsBaseMessage)
79 {
80 if (smsBaseMessage == nullptr) {
81 TELEPHONY_LOGE("BaseMessage is null.");
82 return AckIncomeCause::SMS_ACK_UNKNOWN_ERROR;
83 }
84 GsmSmsMessage *message = (GsmSmsMessage *)smsBaseMessage.get();
85 if (message->IsSpecialMessage()) {
86 TELEPHONY_LOGI("GsmSmsReceiveHandler:: IsSpecialMessage");
87 return AckIncomeCause::SMS_ACK_RESULT_OK;
88 }
89 int ret = CheckSmsSupport();
90 if (ret != AckIncomeCause::SMS_ACK_RESULT_OK) {
91 return ret;
92 }
93 return HandleNormalSmsByType(smsBaseMessage);
94 }
95
CheckSmsSupport()96 int32_t GsmSmsReceiveHandler::CheckSmsSupport()
97 {
98 auto reliabilityHandler = std::make_shared<SmsReceiveReliabilityHandler>(slotId_);
99 if (reliabilityHandler == nullptr) {
100 TELEPHONY_LOGE("reliabilityHandler nullptr");
101 return AckIncomeCause::SMS_ACK_UNKNOWN_ERROR;
102 }
103 if (!(reliabilityHandler->CheckSmsCapable())) {
104 TELEPHONY_LOGI("sms receive capable unSupport");
105 SmsHiSysEvent::WriteSmsReceiveFaultEvent(slotId_, SmsMmsMessageType::SMS_SHORT_MESSAGE,
106 SmsMmsErrorCode::SMS_ERROR_EMPTY_INPUT_PARAMETER, "sms receive capable unsupported");
107 return AckIncomeCause::SMS_ACK_PROCESSED;
108 }
109 if (!reliabilityHandler->DeleteExpireSmsFromDB()) {
110 TELEPHONY_LOGE("DeleteExpireSmsFromDB fail");
111 return AckIncomeCause::SMS_ACK_PROCESSED;
112 }
113 return AckIncomeCause::SMS_ACK_RESULT_OK;
114 }
115
HandleNormalSmsByType(const shared_ptr<SmsBaseMessage> smsBaseMessage)116 int32_t GsmSmsReceiveHandler::HandleNormalSmsByType(const shared_ptr<SmsBaseMessage> smsBaseMessage)
117 {
118 if (smsBaseMessage == nullptr) {
119 TELEPHONY_LOGE("BaseMessage is null.");
120 return AckIncomeCause::SMS_ACK_UNKNOWN_ERROR;
121 }
122 GsmSmsMessage *message = (GsmSmsMessage *)smsBaseMessage.get();
123
124 shared_ptr<SmsReceiveIndexer> indexer;
125 if (!message->IsConcatMsg()) {
126 indexer = make_shared<SmsReceiveIndexer>(message->GetRawPdu(), message->GetScTimestamp(),
127 message->GetDestPort(), !message->GetGsm(), false, message->GetOriginatingAddress(),
128 message->GetVisibleOriginatingAddress(), message->GetVisibleMessageBody());
129 } else {
130 std::shared_ptr<SmsConcat> smsConcat = message->GetConcatMsg();
131 if (smsConcat == nullptr) {
132 TELEPHONY_LOGE("Concat is null.");
133 return AckIncomeCause::SMS_ACK_UNKNOWN_ERROR;
134 }
135 indexer = make_shared<SmsReceiveIndexer>(message->GetRawPdu(), message->GetScTimestamp(),
136 message->GetDestPort(), !message->GetGsm(), message->GetOriginatingAddress(),
137 message->GetVisibleOriginatingAddress(), smsConcat->msgRef, smsConcat->seqNum, smsConcat->totalSeg,
138 false, message->GetVisibleMessageBody());
139 }
140 if (indexer == nullptr) {
141 TELEPHONY_LOGE("indexer is null.");
142 return AckIncomeCause::SMS_ACK_UNKNOWN_ERROR;
143 }
144 indexer->SetRawUserData(message->GetRawUserData());
145 indexer->SetRawWapPushUserData(message->GetRawWapPushUserData());
146
147 TELEPHONY_LOGI("received a gsm sms, this is %{public}d, a total of %{public}d", indexer->GetMsgSeqId(),
148 indexer->GetMsgCount());
149 if (indexer->GetIsText() && message->IsConcatMsg() && IsRepeatedMessagePart(indexer)) {
150 TELEPHONY_LOGE("Ack repeated error.");
151 SmsHiSysEvent::WriteSmsReceiveFaultEvent(slotId_, SmsMmsMessageType::SMS_SHORT_MESSAGE,
152 SmsMmsErrorCode::SMS_ERROR_REPEATED_ERROR, "gsm message repeated error");
153 return AckIncomeCause::SMS_ACK_REPEATED_ERROR;
154 }
155 if (!AddMsgToDB(indexer)) {
156 return AckIncomeCause::SMS_ACK_UNKNOWN_ERROR;
157 }
158 CombineMessagePart(indexer);
159 return AckIncomeCause::SMS_ACK_RESULT_OK;
160 }
161
ReplySmsToSmsc(int result,const shared_ptr<SmsBaseMessage> response)162 void GsmSmsReceiveHandler::ReplySmsToSmsc(int result, const shared_ptr<SmsBaseMessage> response)
163 {
164 TELEPHONY_LOGI("GsmSmsReceiveHandler::ReplySmsToSmsc ackResult %{public}d", result);
165 CoreManagerInner::GetInstance().SendSmsAck(
166 slotId_, SMS_EVENT_NEW_SMS_REPLY, result == AckIncomeCause::SMS_ACK_RESULT_OK, result, shared_from_this());
167 }
168
TransformMessageInfo(const shared_ptr<SmsMessageInfo> info)169 shared_ptr<SmsBaseMessage> GsmSmsReceiveHandler::TransformMessageInfo(const shared_ptr<SmsMessageInfo> info)
170 {
171 std::shared_ptr<SmsBaseMessage> baseMessage = nullptr;
172 if (info == nullptr) {
173 TELEPHONY_LOGE("MessageInfo is null.");
174 return baseMessage;
175 }
176 std::string pdu = StringUtils::StringToHex(info->pdu);
177 baseMessage = GsmSmsMessage::CreateMessage(pdu);
178 return baseMessage;
179 }
180 } // namespace Telephony
181 } // namespace OHOS
182