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 "gsm_sms_cb_handler.h"
17
18 #include "common_event_support.h"
19 #include "core_manager_inner.h"
20 #include "radio_event.h"
21 #include "securec.h"
22 #include "singleton.h"
23 #include "sms_hisysevent.h"
24 #include "string_utils.h"
25 #include "telephony_log_wrapper.h"
26 #include "telephony_permission.h"
27
28 namespace OHOS {
29 namespace Telephony {
30 using namespace OHOS::EventFwk;
GsmSmsCbHandler(const std::shared_ptr<AppExecFwk::EventRunner> & runner,int32_t slotId)31 GsmSmsCbHandler::GsmSmsCbHandler(const std::shared_ptr<AppExecFwk::EventRunner> &runner, int32_t slotId)
32 : AppExecFwk::EventHandler(runner), slotId_(slotId)
33 {
34 TELEPHONY_LOGI("GsmSmsCbHandler Create slotId (%{public}d)", slotId_);
35 }
36
Init()37 void GsmSmsCbHandler::Init()
38 {
39 cbMsgList_.clear();
40 CoreManagerInner::GetInstance().RegisterCoreNotify(
41 slotId_, shared_from_this(), RadioEvent::RADIO_CELL_BROADCAST, nullptr);
42 TELEPHONY_LOGI("GsmSmsCbHandler::RegisterHandler::slotId= %{public}d", slotId_);
43 }
44
UnRegisterHandler()45 void GsmSmsCbHandler::UnRegisterHandler()
46 {
47 CoreManagerInner::GetInstance().UnRegisterCoreNotify(slotId_, shared_from_this(), RadioEvent::RADIO_CELL_BROADCAST);
48 }
49
CheckCbActive(const std::shared_ptr<GsmCbCodec> & cbMessage)50 bool GsmSmsCbHandler::CheckCbActive(const std::shared_ptr<GsmCbCodec> &cbMessage)
51 {
52 if (cbMessage == nullptr) {
53 TELEPHONY_LOGE("CbMessage is null!");
54 return false;
55 }
56 return true;
57 }
58
CheckCbMessage(const std::shared_ptr<GsmCbCodec> & cbMessage)59 uint8_t GsmSmsCbHandler::CheckCbMessage(const std::shared_ptr<GsmCbCodec> &cbMessage)
60 {
61 uint8_t currPageCnt = 0;
62 bool bFind = false;
63 if (cbMessage == nullptr) {
64 TELEPHONY_LOGE("CheckCbMessage cbMessage nullptr err.");
65 return currPageCnt;
66 }
67
68 std::shared_ptr<GsmCbCodec::GsmCbMessageHeader> cbHeader = cbMessage->GetCbHeader();
69 if (cbHeader == nullptr || cbHeader->totalPages == 0) {
70 TELEPHONY_LOGE("CheckCbMessage GetCbHeader err.");
71 return currPageCnt;
72 }
73
74 for (std::size_t i = 0; i < cbMsgList_.size(); i++) {
75 SmsCbInfo &cbInfo = cbMsgList_[i];
76 if (*cbInfo.header.get() == *cbHeader.get()) {
77 int updateNum = cbHeader->serialNum.updateNum - cbInfo.header->serialNum.updateNum;
78 if (updateNum != 0) {
79 break;
80 }
81
82 if (cbInfo.cbMsgs.count(cbHeader->page)) {
83 currPageCnt = 0x01;
84 return currPageCnt;
85 }
86 cbInfo.cbMsgs.insert(std::make_pair(cbHeader->page, cbMessage));
87 currPageCnt = cbInfo.cbMsgs.size();
88 bFind = true;
89 break;
90 }
91 }
92
93 if (bFind == false || cbHeader->totalPages == 0x01) {
94 AddCbMessageToList(cbMessage);
95 currPageCnt = 0x01;
96 }
97
98 return currPageCnt;
99 }
100
FindCbMessage(const std::shared_ptr<GsmCbCodec> & cbMessage)101 std::unique_ptr<SmsCbInfo> GsmSmsCbHandler::FindCbMessage(const std::shared_ptr<GsmCbCodec> &cbMessage)
102 {
103 std::unique_ptr<SmsCbInfo> cbInfo = nullptr;
104 if (cbMessage == nullptr) {
105 TELEPHONY_LOGE("FindCbMessage cbMessage nullptr err.");
106 return cbInfo;
107 }
108
109 std::shared_ptr<GsmCbCodec::GsmCbMessageHeader> cbHeader = cbMessage->GetCbHeader();
110 if (cbHeader == nullptr) {
111 TELEPHONY_LOGE("FindCbMessage header err.");
112 return cbInfo;
113 }
114
115 for (std::size_t i = 0; i < cbMsgList_.size(); i++) {
116 SmsCbInfo &info = cbMsgList_[i];
117 if (*info.header.get() == *cbHeader.get()) {
118 cbInfo = std::make_unique<SmsCbInfo>(info.header, info.cbMsgs);
119 if (cbInfo == nullptr) {
120 TELEPHONY_LOGE("Make SmsCbInfo err.");
121 return cbInfo;
122 }
123 }
124 }
125 return cbInfo;
126 }
127
AddCbMessageToList(const std::shared_ptr<GsmCbCodec> & cbMessage)128 bool GsmSmsCbHandler::AddCbMessageToList(const std::shared_ptr<GsmCbCodec> &cbMessage)
129 {
130 if (cbMessage == nullptr) {
131 TELEPHONY_LOGE("AddCbMessageToList cbMessage nullptr err.");
132 return false;
133 }
134 std::shared_ptr<GsmCbCodec::GsmCbMessageHeader> cbHeader = cbMessage->GetCbHeader();
135 if (cbHeader == nullptr) {
136 TELEPHONY_LOGE("AddCbMessageToList header err.");
137 return false;
138 }
139 std::unique_ptr<SmsCbInfo> cbInfo = FindCbMessage(cbMessage);
140 if (cbInfo == nullptr) {
141 SmsCbInfo info;
142 info.header = cbHeader;
143 info.cbMsgs.insert(std::make_pair(cbHeader->page, cbMessage));
144 InitLocation(info);
145 cbMsgList_.push_back(info);
146 return true;
147 }
148 return false;
149 }
150
InitLocation(SmsCbInfo & info)151 bool GsmSmsCbHandler::InitLocation(SmsCbInfo &info)
152 {
153 const uint8_t cellWideImmediate = 0;
154 const uint8_t plmnWide = 1;
155 const uint8_t LaWide = 2;
156 const uint8_t cellWide = 3;
157 const int32_t defaultValue = -1;
158 info.plmn_ = CoreManagerInner::GetInstance().GetOperatorNumeric(slotId_);
159 sptr<CellLocation> location = CoreManagerInner::GetInstance().GetCellLocation(slotId_);
160 if (location == nullptr) {
161 TELEPHONY_LOGE("location is nullptr.");
162 return false;
163 }
164 if (location->GetCellLocationType() != CellLocation::CellType::CELL_TYPE_GSM) {
165 TELEPHONY_LOGE("location type isn't GSM.");
166 return false;
167 }
168 sptr<GsmCellLocation> gsmLocation = sptr<GsmCellLocation>(static_cast<GsmCellLocation *>(location.GetRefPtr()));
169 info.lac_ = gsmLocation->GetLac();
170 info.cid_ = gsmLocation->GetCellId();
171 TELEPHONY_LOGI("plmn = %{private}s lac = %{private}s cid = %{private}s", StringUtils::ToUtf8(info.plmn_).c_str(),
172 std::to_string(info.lac_).c_str(), std::to_string(info.cid_).c_str());
173 switch (info.header->serialNum.geoScope) {
174 case LaWide:
175 info.cid_ = defaultValue;
176 break;
177 case cellWide:
178 case cellWideImmediate:
179 break;
180 case plmnWide:
181 default:
182 info.cid_ = defaultValue;
183 info.lac_ = defaultValue;
184 break;
185 }
186 plmn_ = info.plmn_;
187 cid_ = info.cid_;
188 lac_ = info.lac_;
189 return true;
190 }
191
RemoveCbMessageFromList(const std::shared_ptr<GsmCbCodec> & cbMessage)192 bool GsmSmsCbHandler::RemoveCbMessageFromList(const std::shared_ptr<GsmCbCodec> &cbMessage)
193 {
194 bool result = false;
195 if (cbMessage == nullptr) {
196 TELEPHONY_LOGE("RemoveCbMessageFromList cbMessage nullptr err.");
197 return false;
198 }
199
200 std::shared_ptr<GsmCbCodec::GsmCbMessageHeader> header = cbMessage->GetCbHeader();
201 if (header == nullptr) {
202 TELEPHONY_LOGE("RemoveCbMessageFromList header err.");
203 return false;
204 }
205 auto it = cbMsgList_.begin();
206 while (it != cbMsgList_.end()) {
207 SmsCbInfo &info = *it;
208 if (*info.header.get() == *header.get() || !info.MatchLocation(plmn_, lac_, cid_)) {
209 it = cbMsgList_.erase(it);
210 result = true;
211 } else {
212 ++it;
213 }
214 }
215 return result;
216 }
217
HandleCbMessage(std::shared_ptr<CBConfigReportInfo> & message)218 void GsmSmsCbHandler::HandleCbMessage(std::shared_ptr<CBConfigReportInfo> &message)
219 {
220 if (message == nullptr) {
221 TELEPHONY_LOGE("GsmSmsCbHandler HandleCbMessage message == nullptr");
222 return;
223 }
224
225 std::string pdu(message->pdu);
226 std::shared_ptr<GsmCbCodec> cbMessage = GsmCbCodec::CreateCbMessage(pdu);
227 if (cbMessage == nullptr) {
228 TELEPHONY_LOGE("create Sms CbMessage fail, pdu %{private}s", pdu.c_str());
229 SmsHiSysEvent::WriteSmsReceiveFaultEvent(slotId_, SmsMmsMessageType::CELL_BROAD_CAST,
230 SmsMmsErrorCode::SMS_ERROR_CELL_BROADCAST_PUD_ANALYSIS_FAIL, "publish cell broadcast event fail");
231 return;
232 }
233 std::shared_ptr<GsmCbCodec::GsmCbMessageHeader> header = cbMessage->GetCbHeader();
234 if (header == nullptr) {
235 TELEPHONY_LOGE("HandleCbMessage header is null.");
236 return;
237 }
238
239 if (!CheckCbActive(cbMessage)) {
240 TELEPHONY_LOGE("The Cell Broadcast msg is not active");
241 return;
242 }
243
244 uint8_t pageCnt = CheckCbMessage(cbMessage);
245 if (header->totalPages == pageCnt) {
246 SendCbMessageBroadcast(cbMessage);
247 RemoveCbMessageFromList(cbMessage);
248 }
249 }
250
SendCbMessageBroadcast(const std::shared_ptr<GsmCbCodec> & cbMessage)251 bool GsmSmsCbHandler::SendCbMessageBroadcast(const std::shared_ptr<GsmCbCodec> &cbMessage)
252 {
253 if (cbMessage == nullptr) {
254 TELEPHONY_LOGE("SendCbMessageBroadcast cbMessage nullptr err.");
255 return false;
256 }
257 EventFwk::Want want;
258 EventFwk::CommonEventData data;
259 if (!SetWantData(want, cbMessage)) {
260 TELEPHONY_LOGE("SendCbMessageBroadcast SetWantData fail.");
261 return false;
262 }
263 data.SetWant(want);
264 EventFwk::CommonEventPublishInfo publishInfo;
265 publishInfo.SetOrdered(true);
266 std::vector<std::string> gsmCbPermissions;
267 gsmCbPermissions.emplace_back(Permission::RECEIVE_MESSAGES);
268 publishInfo.SetSubscriberPermissions(gsmCbPermissions);
269 bool publishResult = EventFwk::CommonEventManager::PublishCommonEvent(data, publishInfo, nullptr);
270 if (!publishResult) {
271 TELEPHONY_LOGE("SendBroadcast PublishBroadcastEvent result fail");
272 SmsHiSysEvent::WriteSmsReceiveFaultEvent(slotId_, SmsMmsMessageType::CELL_BROAD_CAST,
273 SmsMmsErrorCode::SMS_ERROR_PUBLISH_COMMON_EVENT_FAIL, "publish cell broadcast event fail");
274 return false;
275 }
276 DelayedSingleton<SmsHiSysEvent>::GetInstance()->SetCbBroadcastStartTime();
277 return true;
278 }
279
ProcessEvent(const AppExecFwk::InnerEvent::Pointer & event)280 void GsmSmsCbHandler::ProcessEvent(const AppExecFwk::InnerEvent::Pointer &event)
281 {
282 if (event == nullptr) {
283 TELEPHONY_LOGE("GsmSmsCbHandler event nullptr error.");
284 return;
285 }
286
287 uint32_t eventId = 0;
288 eventId = event->GetInnerEventId();
289 TELEPHONY_LOGI("ProcessEvent eventId = %{public}d", eventId);
290 switch (eventId) {
291 case RadioEvent::RADIO_CELL_BROADCAST: {
292 std::shared_ptr<CBConfigReportInfo> cbMessage = nullptr;
293 cbMessage = event->GetSharedObject<CBConfigReportInfo>();
294 HandleCbMessage(cbMessage);
295 break;
296 }
297 default:
298 TELEPHONY_LOGE("GsmSmsCbHandler eventId unknown error.");
299 break;
300 }
301 }
302
SetWantData(EventFwk::Want & want,const std::shared_ptr<GsmCbCodec> & cbMessage)303 bool GsmSmsCbHandler::SetWantData(EventFwk::Want &want, const std::shared_ptr<GsmCbCodec> &cbMessage)
304 {
305 if (cbMessage == nullptr) {
306 TELEPHONY_LOGE("cbMessage is nullptr.");
307 return false;
308 }
309 SmsCbData::CbData sendData;
310 std::unique_ptr<SmsCbInfo> info = FindCbMessage(cbMessage);
311 if (info == nullptr) {
312 TELEPHONY_LOGE("find cb message fail.");
313 return false;
314 }
315 std::string rawMsgBody;
316 for (auto it = info->cbMsgs.begin(); it != info->cbMsgs.end(); it++) {
317 rawMsgBody.append(it->second->GetCbMessageRaw());
318 }
319 GetCbData(cbMessage, sendData);
320 cbMessage->ConvertToUTF8(rawMsgBody, sendData.msgBody);
321 want.SetParam(SmsCbData::GEO_SCOPE, static_cast<char>(sendData.geoScope));
322 want.SetParam(SmsCbData::CMAS_RESPONSE, static_cast<char>(sendData.cmasRes));
323 want.SetParam(SmsCbData::SLOT_ID, static_cast<int>(slotId_));
324 want.SetParam(SmsCbData::FORMAT, static_cast<char>(sendData.format));
325 want.SetParam(SmsCbData::CB_MSG_TYPE, static_cast<char>(sendData.msgType));
326 want.SetParam(SmsCbData::MSG_ID, static_cast<int>(sendData.msgId));
327 want.SetParam(SmsCbData::SERVICE_CATEGORY, static_cast<int>(sendData.category));
328 want.SetParam(SmsCbData::LANG_TYPE, static_cast<char>(sendData.langType));
329 want.SetParam(SmsCbData::PRIORITY, static_cast<char>(sendData.priority));
330 want.SetParam(SmsCbData::MSG_BODY, sendData.msgBody);
331 want.SetParam(SmsCbData::CMAS_CLASS, static_cast<char>(sendData.cmasClass));
332 want.SetParam(SmsCbData::CMAS_CATEGORY, static_cast<char>(sendData.cmasCate));
333 want.SetParam(SmsCbData::SEVERITY, static_cast<char>(sendData.severity));
334 want.SetParam(SmsCbData::URGENCY, static_cast<char>(sendData.urgency));
335 want.SetParam(SmsCbData::CERTAINTY, static_cast<char>(sendData.certainty));
336 want.SetParam(SmsCbData::IS_CMAS_MESSAGE, sendData.isCmas);
337 want.SetParam(SmsCbData::SERIAL_NUM, static_cast<int>(sendData.serial));
338 want.SetParam(SmsCbData::RECV_TIME, std::to_string(sendData.recvTime));
339 want.SetParam(SmsCbData::DCS, static_cast<char>(sendData.dcs));
340
341 want.SetParam(SmsCbData::IS_ETWS_PRIMARY, sendData.isPrimary);
342 want.SetParam(SmsCbData::IS_ETWS_MESSAGE, sendData.isEtws);
343 want.SetParam(SmsCbData::PLMN, StringUtils::ToUtf8(plmn_));
344 want.SetParam(SmsCbData::LAC, static_cast<int>(lac_));
345 want.SetParam(SmsCbData::CID, static_cast<int>(cid_));
346 want.SetParam(SmsCbData::WARNING_TYPE, static_cast<int>(sendData.warnType));
347 if (sendData.isPrimary) {
348 want.SetAction(CommonEventSupport::COMMON_EVENT_SMS_EMERGENCY_CB_RECEIVE_COMPLETED);
349 } else {
350 want.SetAction(CommonEventSupport::COMMON_EVENT_SMS_CB_RECEIVE_COMPLETED);
351 }
352 return true;
353 }
354
GetCbData(const std::shared_ptr<GsmCbCodec> & cbMessage,SmsCbData::CbData & sendData)355 void GsmSmsCbHandler::GetCbData(const std::shared_ptr<GsmCbCodec> &cbMessage, SmsCbData::CbData &sendData)
356 {
357 if (cbMessage == nullptr) {
358 TELEPHONY_LOGE("Get Cb Data error.");
359 return;
360 }
361 cbMessage->GetMsgType(sendData.msgType);
362 cbMessage->GetLangType(sendData.langType);
363 cbMessage->GetDcs(sendData.dcs);
364 cbMessage->GetPriority(sendData.priority);
365 cbMessage->GetCmasMessageClass(sendData.cmasClass);
366 cbMessage->GetCmasResponseType(sendData.cmasRes);
367 cbMessage->GetCmasSeverity(sendData.severity);
368 cbMessage->GetCmasUrgency(sendData.urgency);
369 cbMessage->GetCmasCertainty(sendData.certainty);
370 cbMessage->IsEtwsMessage(sendData.isEtws);
371 cbMessage->GetWarningType(sendData.warnType);
372 cbMessage->IsCmasMessage(sendData.isCmas);
373 cbMessage->GetSerialNum(sendData.serial);
374 cbMessage->GetReceiveTime(sendData.recvTime);
375 cbMessage->GetMessageId(sendData.msgId);
376 cbMessage->GetServiceCategory(sendData.category);
377 cbMessage->GetFormat(sendData.format);
378 cbMessage->IsEtwsPrimary(sendData.isPrimary);
379 cbMessage->GetGeoScope(sendData.geoScope);
380 cbMessage->GetCmasCategory(sendData.cmasCate);
381 }
382 } // namespace Telephony
383 } // namespace OHOS