• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2021 Huawei Device Co., Ltd.
3  * Copyright (C) 2014 Samsung Electronics Co., Ltd. All rights reserved
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #ifndef SMS_CB_MESSAGE_H
18 #define SMS_CB_MESSAGE_H
19 
20 #include <ctime>
21 #include <memory>
22 #include <string>
23 
24 #include "gsm_pdu_code_type.h"
25 #include "gsm_sms_tpdu_codec.h"
26 
27 namespace OHOS {
28 namespace Telephony {
29 class SmsCbMessage {
30 public:
31     static constexpr uint8_t SMS_CB_IOS639LANG_SIZE = 3;
32     /**
33      * @brief SmsCbMsgNetType
34      * from 3GPP TS 23.041 V4.1.0 (2001-06) 5 section Network Architecture
35      * GSM Cell broadcast message 0x01
36      * UMTSCell broadcast message 0x02
37      * CDMA broadcast message 0x03
38      */
39     enum SmsCbMsgNetType { SMS_NETTEXT_CB_MSG_GSM = 1, SMS_NETTEXT_CB_MSG_UMTS, SMS_NETTEXT_CB_MSG_CDMA };
40 
41     /**
42      * @brief SmsEtwsMsgType
43      * Primary ETWS message
44      * GSM Secondary ETWS message
45      * UMTS Secondary ETWS message
46      * CDMA Seconday ETWS message
47      */
48     enum SmsEtwsMsgType {
49         SMS_NETTEXT_ETWS_PRIMARY = 0,
50         SMS_NETTEXT_ETWS_SECONDARY_GSM,
51         SMS_NETTEXT_ETWS_SECONDARY_UMTS,
52         SMS_NETTEXT_ETWS_SECONDARY_CDMA
53     };
54 
55     /**
56      * @brief SmsCbMsgType
57      * CBS Message
58      * Schedule Message
59      * CBS41 Message
60      * JAVA-CB Message
61      */
62     enum SmsCbMsgType {
63         SMS_CBMSG_TYPE_UNKNOWN = 0,
64         SMS_CBMSG_TYPE_CBS,
65         SMS_CBMSG_TYPE_SCHEDULE,
66         SMS_CBMSG_TYPE_CBS41,
67         SMS_CBMSG_TYPE_JAVACBS,
68         SMS_CBMSG_TYPE_ETWS,
69     };
70 
71     /**
72      * @brief SmsCbMessageLangType
73      * from 3GPP TS 23.038 V4.3.0 (2001-09) 5 section
74      * CBS Data Coding Scheme
75      */
76     enum SmsCbMessageLangType {
77         SMS_CBMSG_LANG_GERMAN = 0x00,
78         SMS_CBMSG_LANG_ENGLISH = 0x01,
79         SMS_CBMSG_LANG_ITALIAN = 0x02,
80         SMS_CBMSG_LANG_FRENCH = 0x03,
81         SMS_CBMSG_LANG_SPANISH = 0x04,
82         SMS_CBMSG_LANG_DUTCH = 0x05,
83         SMS_CBMSG_LANG_SWEDISH = 0x06,
84         SMS_CBMSG_LANG_DANISH = 0x07,
85         SMS_CBMSG_LANG_PORTUGUESE = 0x08,
86         SMS_CBMSG_LANG_FINNISH = 0x09,
87         SMS_CBMSG_LANG_NORWEGIAN = 0x0a,
88         SMS_CBMSG_LANG_GREEK = 0x0b,
89         SMS_CBMSG_LANG_TURKISH = 0x0c,
90         SMS_CBMSG_LANG_HUNGARIAN = 0x0d,
91         SMS_CBMSG_LANG_POLISH = 0x0e,
92         SMS_CBMSG_LANG_UNSPECIFIED = 0x0f,
93 
94         SMS_CBMSG_LANG_ISO639 = 0x10,
95 
96         SMS_CBMSG_LANG_CZECH = 0x20,
97         SMS_CBMSG_LANG_HEBREW = 0x21,
98         SMS_CBMSG_LANG_ARABIC = 0x22,
99         SMS_CBMSG_LANG_RUSSIAN = 0x23,
100         SMS_CBMSG_LANG_ICELANDIC = 0x24,
101         SMS_CBMSG_LANG_RESERVED_25 = 0x25,
102         SMS_CBMSG_LANG_RESERVED_26 = 0x26,
103         SMS_CBMSG_LANG_RESERVED_27 = 0x27,
104         SMS_CBMSG_LANG_RESERVED_28 = 0x28,
105         SMS_CBMSG_LANG_RESERVED_29 = 0x29,
106         SMS_CBMSG_LANG_RESERVED_2A = 0x2a,
107         SMS_CBMSG_LANG_RESERVED_2B = 0x2b,
108         SMS_CBMSG_LANG_RESERVED_2C = 0x2c,
109         SMS_CBMSG_LANG_RESERVED_2D = 0x2d,
110         SMS_CBMSG_LANG_RESERVED_2E = 0x2e,
111         SMS_CBMSG_LANG_RESERVED_2F = 0x2f,
112 
113         SMS_CBMSG_LANG_DUMMY = 0xFF
114     };
115 
116     enum SmsMessageSubType {
117         /* SMS Specific Message Type */
118         MSG_NORMAL_SMS = 0, /** Text SMS message */
119         MSG_CB_SMS, /** Cell Broadcasting  message */
120         MSG_JAVACB_SMS, /** JAVA Cell Broadcasting  message */
121 
122         MSG_CMAS_PRESIDENTIAL, /** CMAS CLASS */
123         MSG_CMAS_EXTREME,
124         MSG_CMAS_SEVERE,
125         MSG_CMAS_AMBER,
126         MSG_CMAS_TEST,
127         MSG_CMAS_EXERCISE,
128         MSG_CMAS_OPERATOR_DEFINED,
129     };
130 
131     /**
132      * @brief
133      * CMAS(Commercial Mobile Alert System) 4370-4399 (0x1112-0x112F)
134      */
135     enum SmsCmasMessageType : unsigned short {
136         CMAS_PRESIDENTIAL_DEFUALT = 4370,
137         CMAS_PRESIDENTIAL_SPANISH = 4383,
138 
139         CMAS_EXTREME_OBSERVED_DEFUALT = 4371,
140         CMAS_EXTREME_OBSERVED_SPANISH = 4384,
141         CMAS_EXTREME_LIKELY_DEFUALT = 4372,
142         CMAS_EXTREME_LIKELY_SPANISH = 4385,
143 
144         CMAS_SERVER_EXTERME_OBSERVED_DEFUALT = 4373,
145         CMAS_SERVER_EXTREME_OBSERVED_SPANISH = 4386,
146         CMAS_SERVER_EXTERME_LIKELY_DEFUALT = 4374,
147         CMAS_SERVER_EXTERME_LIKELY_SPANISH = 4387,
148         CMAS_SERVER_SERVER_OBSERVED_DEFUALT = 4375,
149         CMAS_SERVER_SERVER_OBSERVED_SPANISH = 4388,
150         CMAS_SERVER_SERVER_LIKELY_DEFUALT = 4376,
151         CMAS_SERVER_SERVER_LIKELY_SPANISH = 4389,
152         CMAS_SERVER_SERVER_EXPECTED_OBSERVED_DEFUALT = 4377,
153         CMAS_SERVER_SERVER_EXPECTED_OBSERVED_SPANISH = 4390,
154         CMAS_SERVER_SERVER_EXPECTED_LIKELY_DEFUALT = 4378,
155         CMAS_SERVER_SERVER_EXPECTED_LIKELY_SPANISH = 4391,
156 
157         CMAS_AMBER_ALERT_DEFUALT = 4379,
158         CMAS_AMBER_ALERT_SPANISH = 4392,
159         CMAS_RMT_ALERT_DEFUALT = 4380,
160         CMAS_RMT_ALERT_SPANISH = 4393,
161         CMAS_EXERCISE_ALERT_DEFUALT = 4381,
162         CMAS_EXERCISE_ALERT_SPANISH = 4394,
163 
164         CMAS_OPERATOR_ALERT_DEFUALT = 4382,
165         CMAS_OPERATOR_ALERT_SPANISH = 4395,
166     };
167 
168     enum SmsCbCodingGroupType {
169         SMS_CBMSG_CODGRP_GENERAL_DCS, /** Bits 7..4 00xx */
170         SMS_CBMSG_CODGRP_WAP, /** 1110 Cell Broadcast */
171         SMS_CBMSG_CODGRP_CLASS_CODING, /** 1111 Cell Broadcast */
172     };
173 
174     enum CbLanguageType : unsigned short {
175         SMS_LANGUAGE_UNKNOWN = 0x00,
176         SMS_LANGUAGE_ENGLISH = 0x01,
177         SMS_LANGUAGE_FRENCH = 0x02,
178         SMS_LANGUAGE_SPANISH = 0x03,
179         SMS_LANGUAGE_JAPANESE = 0x04,
180         SMS_LANGUAGE_KOREAN = 0x05,
181         SMS_LANGUAGE_CHINESE = 0x06,
182         SMS_LANGUAGE_HEBREW = 0x07,
183     };
184 
185     /**
186      * @brief SmsCBMessageSerialNum
187      * from 3GPP TS 23.041 V4.1.0 (2001-06) 9.4.1.2.1 section
188      * Serial Number
189      */
190     struct SmsCBMessageSerialNum {
191         unsigned char geoScope;
192         unsigned char updateNum;
193         unsigned short msgCode;
194     };
195 
196     /**
197      * @brief SmsCbMessageDcs
198      * from 3GPP TS 23.038 V4.3.0 (2001-09) 5 section
199      * CBS Data Coding Scheme
200      */
201     struct SmsCbMessageDcs {
202         unsigned char codingGroup; /**  Coding group, GSM 03.38 */
203         unsigned char classType; /** The message class */
204         bool bCompressed; /** if text is compressed this is TRUE */
205         unsigned char codingScheme; /** How to encode a message. */
206         unsigned char langType;
207         unsigned char iso639Lang[SmsCbMessage::SMS_CB_IOS639LANG_SIZE]; /* 2 GSM chars and a CR char */
208         bool bUDH;
209         unsigned char rawData;
210     };
211 
212     /**
213      * @brief SmsCbMessageHeader
214      * from 3GPP TS 23.041 V4.1.0 (2001-06) 9.4.1.2 section
215      * Message Parameter
216      */
217     struct SmsCbMessageHeader {
218         bool bEtwsMessage;
219         SmsEtwsMsgType cbEtwsType;
220         SmsCbMsgNetType cbNetType;
221         unsigned char cbMsgType;
222         unsigned short warningType;
223         SmsCBMessageSerialNum serialNum;
224         unsigned short msgId;
225         unsigned char langType;
226         SmsCbMessageDcs dcs;
227         unsigned char page;
228         unsigned char totalPages;
229         time_t recvTime;
230 
231         bool operator==(const SmsCbMessageHeader &other) const
232         {
233             return serialNum.geoScope == other.serialNum.geoScope &&
234                 serialNum.msgCode == other.serialNum.msgCode && msgId == other.msgId;
235         }
236     };
237     SmsCbMessage() = default;
238     ~SmsCbMessage() = default;
239     bool operator==(const SmsCbMessage &other) const;
240     static std::shared_ptr<SmsCbMessage> CreateCbMessage(const std::string &pdu);
241     static std::shared_ptr<SmsCbMessage> CreateCbMessage(const std::vector<unsigned char> &pdu);
242     std::shared_ptr<SmsCbMessageHeader> GetCbHeader() const;
243     void ConvertToUTF8(const std::string &raw, std::string &message) const;
244     std::string GetCbMessageRaw() const;
245     bool IsSinglePageMsg() const;
246     std::string ToString() const;
247     unsigned short EncodeCbSerialNum(const SmsCBMessageSerialNum &snFields);
248     bool GetFormat(int8_t &format) const;
249     bool GetPriority(int8_t &priority) const;
250     bool GetGeoScope(uint8_t &gs) const;
251     bool GetSerialNum(uint16_t &serial) const;
252     bool GetServiceCategory(uint16_t &categoty) const;
253     bool IsEtwsPrimary(bool &isPriority) const;
254     bool IsEtwsMessage(bool &isEtws) const;
255     bool IsCmasMessage(bool &isCmas) const;
256     bool IsEtwsEmergencyUserAlert(bool &isUserAlert) const;
257     bool IsEtwsPopupAlert(bool &isPopupAlert) const;
258     bool GetWarningType(uint16_t &warnType) const;
259     bool GetCmasSeverity(int8_t &severity) const;
260     bool GetCmasUrgency(int8_t &urgency) const;
261     bool GetCmasCertainty(int8_t &certainty) const;
262     bool GetCmasCategory(int8_t &cmasCategory) const;
263     bool GetCmasResponseType(int8_t &cmasRes) const;
264     bool GetMessageId(uint16_t &msgId) const;
265     bool GetCmasMessageClass(int8_t &cmasClass) const;
266     bool GetMsgType(uint8_t &msgType) const;
267     bool GetLangType(uint8_t &lan) const;
268     bool GetDcs(uint8_t &dcs) const;
269     bool GetReceiveTime(long &recvTime) const;
270 
271 private:
272     static constexpr uint8_t HEX_BYTE_STEP = 2;
273     static constexpr uint8_t BYTE_BIT = 8;
274     static constexpr uint8_t ENCODE_GSM_BIT = 7;
275     static constexpr uint8_t SMS_CB_HEADER_SIZE = 6;
276     static constexpr uint8_t SMS_CBMI_LIST_SIZE_MAX = 50; // Maximum SMS CBMI list size
277     static constexpr uint16_t MAX_CB_MSG_TEXT_LEN = 4200;
278     static constexpr uint8_t MAX_CBMSG_PAGE_SIZE = 82;
279     static constexpr uint16_t MAX_CBMSG_TOTAL_PAGE_SIZE = 1252;
280     static constexpr uint16_t MAX_CBMSG_PAGE_BEAR_SIZE = MAX_CBMSG_TOTAL_PAGE_SIZE * 8 / 7;
281     static constexpr uint8_t MAX_ETWS_SIZE = 56;
282     static constexpr uint8_t MAX_CBMSG_PAGE_NUM = 15;
283 
284     static constexpr uint8_t SMS_DCS_ALPHABET_GROUP = 0x01;
285     static constexpr uint16_t SMS_ETWS_BASE_MASK = 0x1100;
286 
287     bool PduAnalysis(const std::vector<unsigned char> &pdu);
288     int Decode2gHeader(const std::vector<unsigned char> &pdu);
289     int Decode3gHeader(const std::vector<unsigned char> &pdu);
290     void Decode2gCbMsg(const std::vector<unsigned char> &pdu);
291     void Decode3gCbMsg(const std::vector<unsigned char> &pdu);
292     void DecodeEtwsMsg(const std::vector<unsigned char> &pdu);
293     void DecodeCbMsgDCS(const unsigned char dcsData, const unsigned short iosData, SmsCbMessageDcs &pDcs) const;
294     void DecodeIos639Dcs(const unsigned char dcsData, const unsigned short iosData, SmsCbMessageDcs &pDcs) const;
295     void DecodeGeneralDcs(const unsigned char dcsData, SmsCbMessageDcs &pDcs) const;
296     unsigned long GetRecvTime() const;
297     int CMASClass(const unsigned short messageId) const;
298     void Decode3g7Bit(const std::vector<unsigned char> &pdu);
299     void Decode3gUCS2(const std::vector<unsigned char> &pdu);
300 
301     std::shared_ptr<SmsCbMessageHeader> cbHeader_;
302     std::string messageRaw_;
303 };
304 } // namespace Telephony
305 } // namespace OHOS
306 #endif // SMS_CB_MESSAGE_H
307