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 SMS_BASE_MESSAGE__H
17  #define SMS_BASE_MESSAGE__H
18  
19  #include <cmath>
20  #include <memory>
21  
22  #include "cdma_sms_transport_message.h"
23  #include "gsm_sms_tpdu_codec.h"
24  #include "msg_text_convert_common.h"
25  #include "sms_common_utils.h"
26  #include "string_utils.h"
27  
28  namespace OHOS {
29  namespace Telephony {
30  typedef struct {
31      uint16_t msgRef;
32      uint16_t seqNum;
33      uint16_t totalSeg;
34      bool is8Bits;
35  } SmsConcat;
36  
37  typedef struct {
38      uint16_t destPort;
39      uint16_t originPort;
40      bool is8Bits;
41  } SmsAppPortAddr;
42  
43  typedef struct {
44      bool bStore;
45      uint16_t msgInd;
46      uint16_t waitMsgNum;
47  } SpecialSmsIndication;
48  
49  struct SplitInfo {
50      std::string text = "";
51      std::vector<uint8_t> encodeData {};
52      DataCodingScheme encodeType = DataCodingScheme::DATA_CODING_7BIT;
53      MSG_LANGUAGE_ID_T langId = 0;
54  };
55  
56  struct LengthInfo {
57      uint8_t dcs = 0;
58      uint8_t msgSegCount = 0;
59      uint16_t msgEncodeCount = 0;
60      uint8_t msgRemainCount = 0;
61  };
62  
63  class SmsBaseMessage {
64  public:
65      SmsBaseMessage() = default;
66      virtual ~SmsBaseMessage() = default;
67      virtual void SetSmscAddr(const std::string &scAddress);
68      virtual std::string GetSmscAddr() const;
69      virtual std::string GetOriginatingAddress() const;
70      virtual std::string GetVisibleOriginatingAddress() const;
71      virtual std::string GetVisibleMessageBody() const;
72      virtual enum SmsMessageClass GetMessageClass() const;
73      std::vector<uint8_t> GetRawPdu() const;
74      std::string GetRawUserData() const;
75      std::string GetRawWapPushUserData() const;
76      virtual int64_t GetScTimestamp() const;
77      virtual int GetStatus() const;
78      virtual int GetProtocolId() const;
79      virtual bool IsReplaceMessage();
80      virtual bool IsCphsMwi() const;
81      virtual bool IsMwiClear() const;
82      virtual bool IsMwiSet() const;
83      virtual bool IsMwiNotStore() const;
84      virtual bool IsSmsStatusReportMessage() const;
85      virtual bool HasReplyPath() const;
86      virtual std::shared_ptr<SmsConcat> GetConcatMsg();
87      virtual std::shared_ptr<SmsAppPortAddr> GetPortAddress();
88      virtual std::shared_ptr<SpecialSmsIndication> GetSpecialSmsInd();
89      virtual bool IsConcatMsg();
90      virtual bool IsWapPushMsg();
91      virtual void ConvertMessageClass(enum SmsMessageClass msgClass);
92      virtual int GetMsgRef();
93      virtual int GetSegmentSize(
94          DataCodingScheme &codingScheme, int dataLen, bool bPortNum, MSG_LANGUAGE_ID_T &langId) const;
95      virtual void SplitMessage(std::vector<struct SplitInfo> &splitResult, const std::string &text, bool force7BitCode,
96          DataCodingScheme &codingType, bool bPortNum, const std::string &desAddr);
97      virtual int32_t GetIndexOnSim() const;
98      virtual void SetIndexOnSim(int32_t index);
99      virtual int32_t GetSmsSegmentsInfo(const std::string &message, bool force7BitCode, LengthInfo &lenInfo);
100      virtual int GetMaxSegmentSize(
101          DataCodingScheme &codingScheme, int dataLen, bool bPortNum, MSG_LANGUAGE_ID_T &langId, int replyAddrLen) const;
102      virtual void SplitMessageUcs2(std::vector<struct SplitInfo> &splitResult, const uint8_t* decodeData,
103          int32_t encodeLen, int32_t segSize, DataCodingScheme &codingType);
104  
105  protected:
106      constexpr static int16_t MAX_MSG_TEXT_LEN = 1530;
107      constexpr static int16_t MAX_REPLY_PID = 8;
108      std::string scAddress_;
109      std::string originatingAddress_;
110      std::string visibleMessageBody_;
111      enum SmsMessageClass msgClass_ = SMS_CLASS_UNKNOWN;
112      int64_t scTimestamp_;
113      int status_;
114      int protocolId_;
115      bool bReplaceMessage_;
116      bool bStatusReportMessage_;
117      bool bMwi_;
118      bool bMwiSense_;
119      bool bCphsMwi_;
120      bool bMwiClear_;
121      bool bMwiSet_;
122      bool bMwiNotStore_;
123      bool hasReplyPath_;
124      bool bMoreMsg_;
125      bool bHeaderInd_;
126      int headerCnt_;
127      int headerDataLen_;
128      int msgRef_;
129      bool bCompressed_;
130      bool bIndActive_;
131      int codingScheme_;
132      int codingGroup_;
133      std::vector<uint8_t> rawPdu_;
134      std::string rawUserData_;
135      std::string rawWapPushUserData_;
136      struct SmsUDPackage smsUserData_;
137      struct SmsTpud smsWapPushUserData_;
138      std::shared_ptr<SmsConcat> smsConcat_;
139      std::shared_ptr<SmsAppPortAddr> portAddress_;
140      std::shared_ptr<SpecialSmsIndication> specialSmsInd_;
141      int32_t indexOnSim_ = -1;
142  
143  private:
144      virtual int DecodeMessage(uint8_t *decodeData, unsigned int length, DataCodingScheme &codingType,
145          const std::string &msgText, bool &bAbnormal, MSG_LANGUAGE_ID_T &langId) = 0;
146      void ConvertSpiltToUtf8(SplitInfo &split, const DataCodingScheme &codingType);
147  };
148  } // namespace Telephony
149  } // namespace OHOS
150  #endif
151