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 #ifndef GSM_SMS_CB_HANDLER_H
17 #define GSM_SMS_CB_HANDLER_H
18 
19 #include <map>
20 
21 #include "common_event.h"
22 #include "common_event_manager.h"
23 #include "gsm_cb_codec.h"
24 #include "tel_ril_sms_parcel.h"
25 #include "sms_cb_data.h"
26 #include "tel_event_handler.h"
27 #include "want.h"
28 
29 enum SmsCbType {
30     SMS_CB_TYPE = 0,
31     SMS_ETWS_TYPE,
32 };
33 
34 namespace OHOS {
35 namespace Telephony {
36 using SmsCbInfo = struct CbInfo {
CbInfoCbInfo37     CbInfo(const std::shared_ptr<GsmCbCodec::GsmCbMessageHeader> &headPtr,
38         const std::map<uint8_t, std::shared_ptr<GsmCbCodec>> &cbPtr)
39         : header(headPtr), cbMsgs(cbPtr)
40     {}
CbInfoCbInfo41     CbInfo() {}
42     std::shared_ptr<GsmCbCodec::GsmCbMessageHeader> header = nullptr;
43     std::map<uint8_t, std::shared_ptr<GsmCbCodec>> cbMsgs {};
44     std::u16string plmn_ = u"";
45     int32_t lac_ = -1;
46     int32_t cid_ = -1;
47 
MatchLocationCbInfo48     bool MatchLocation(std::u16string plmn, int32_t lac, int32_t cid)
49     {
50         const int32_t defaultValue = -1;
51         if (plmn != plmn_) {
52             return false;
53         }
54         if (lac_ != defaultValue && lac_ != lac) {
55             return false;
56         }
57         if (cid_ != defaultValue && cid_ != cid) {
58             return false;
59         }
60         return true;
61     }
62 };
63 
64 class GsmSmsCbHandler : public TelEventHandler {
65 public:
66     explicit GsmSmsCbHandler(int32_t slotId);
67     ~GsmSmsCbHandler() = default;
68     void Init();
69     void UnRegisterHandler();
70     virtual void ProcessEvent(const AppExecFwk::InnerEvent::Pointer &event) override;
71 
72     static constexpr uint16_t MAX_CB_MSG_LEN = 4200;
73     static constexpr uint8_t MAX_CB_MSG_LANGUAGE_TYPE_LEN = 3;
74     static constexpr uint8_t MAX_ETWS_WARNING_DATA_INFO_LEN = 50;
75 
76 private:
77     bool CheckCbActive(const std::shared_ptr<GsmCbCodec> &cbMessage);
78     uint8_t CheckCbMessage(const std::shared_ptr<GsmCbCodec> &cbMessage);
79     std::unique_ptr<SmsCbInfo> FindCbMessage(const std::shared_ptr<GsmCbCodec> &cbMessage);
80     bool AddCbMessageToList(const std::shared_ptr<GsmCbCodec> &cbMessage);
81     bool RemoveCbMessageFromList(const std::shared_ptr<GsmCbCodec> &cbMessage);
82     bool SendCbMessageBroadcast(const std::shared_ptr<GsmCbCodec> &cbMessage);
83     void HandleCbMessage(std::shared_ptr<CBConfigReportInfo> &message);
84     bool SetWantData(EventFwk::Want &want, const std::shared_ptr<GsmCbCodec> &cbMessage);
85     bool InitLocation(SmsCbInfo &info);
86     void GetCbData(const std::shared_ptr<GsmCbCodec> &cbMessage, SmsCbData::CbData &SendData);
87     void PackageWantData(SmsCbData::CbData &sendData, EventFwk::Want &want);
88     void ClearExpiredMessage();
89 
90 private:
91     static constexpr uint16_t MAX_CB_MSG_LIST_SIZE = 10000;
92     static constexpr uint64_t DEFAULT_EXPIRED_TIME = 3 * 60 * 60;
93     int32_t slotId_;
94     std::vector<SmsCbInfo> cbMsgList_;
95     int32_t cid_ = -1;
96     int32_t lac_ = -1;
97     std::u16string plmn_;
98 };
99 } // namespace Telephony
100 } // namespace OHOS
101 #endif