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 #ifndef MMS_HEADER_H
17 #define MMS_HEADER_H
18 
19 #include <map>
20 #include <vector>
21 
22 #include "mms_address.h"
23 #include "mms_content_type.h"
24 #include "mms_decode_buffer.h"
25 #include "mms_encode_string.h"
26 #include "mms_header_categ.h"
27 
28 namespace OHOS {
29 namespace Telephony {
30 class MmsHeader {
31 public:
32     MmsHeader();
33     ~MmsHeader() = default;
34     void DumpMmsHeader();
35     bool DecodeMmsHeader(MmsDecodeBuffer &decodeBuffer);
36     bool EncodeMmsHeader(MmsEncodeBuffer &encodeBuffer);
37     bool GetOctetValue(uint8_t fieldId, uint8_t &value) const;
38     bool SetOctetValue(uint8_t fieldId, uint8_t value);
39     bool GetLongValue(uint8_t fieldId, int64_t &value) const;
40     bool SetLongValue(uint8_t fieldId, int64_t value);
41     bool GetTextValue(uint8_t fieldId, std::string &value) const;
42     bool SetTextValue(uint8_t fieldId, std::string value);
43     bool GetEncodeStringValue(uint8_t fieldId, MmsEncodeString &value) const;
44     bool SetEncodeStringValue(uint8_t fieldId, uint32_t charset, const std::string &value);
45     bool AddHeaderAddressValue(uint8_t fieldId, MmsAddress &address);
46     bool GetHeaderAllAddressValue(uint8_t fieldId, std::vector<MmsAddress> &addressValue);
47     bool GetStringValue(uint8_t fieldId, std::string &value) const;
48     MmsContentType &GetHeaderContentType();
49     bool GetSmilFileName(std::string &smileFileName);
50     bool IsHaveBody();
51 private:
52     void InitOctetHandleFun();
53     void InitLongHandleFun();
54     void InitStringHandleFun();
55     void InitTextStringHandleFun();
56     std::string MakeTransactionId(uint32_t len);
57     bool FindHeaderFieldName(const uint8_t fieldId, std::string &fieldName);
58 
59     bool DecodeMmsMsgType(uint8_t fieldId, MmsDecodeBuffer &buff, int32_t &len);
60     bool DecodeFieldAddressModelValue(uint8_t fieldId, MmsDecodeBuffer &buff, int32_t &len);
61     bool DecodeFieldOctetValue(uint8_t fieldId, MmsDecodeBuffer &buff, int32_t &len);
62     bool DecodeFieldLongValue(uint8_t fieldId, MmsDecodeBuffer &buff, int32_t &len);
63     bool DecodeFieldIntegerValue(uint8_t fieldId, MmsDecodeBuffer &buff, int32_t &len);
64     bool DecodeFieldTextStringValue(uint8_t fieldId, MmsDecodeBuffer &buff, int32_t &len);
65     bool DecodeFieldEncodedStringValue(uint8_t fieldId, MmsDecodeBuffer &buff, int32_t &len);
66     bool DecodeFromValue(uint8_t fieldId, MmsDecodeBuffer &buff, int32_t &len);
67     bool DecodeMmsMsgUnKnownField(MmsDecodeBuffer &decodeBuffer);
68     bool DecodeMmsContentType(uint8_t fieldId, MmsDecodeBuffer &buff, int32_t &len);
69     bool DecodeFieldDate(uint8_t fieldId, MmsDecodeBuffer &buff, int32_t &len);
70     bool DecodeFieldPreviouslySentDate(uint8_t fieldId, MmsDecodeBuffer &buff, int32_t &len);
71     bool DecodeFieldMBox(uint8_t fieldId, MmsDecodeBuffer &buff, int32_t &len);
72     bool DecodeFieldMMFlag(uint8_t fieldId, MmsDecodeBuffer &buff, int32_t &len);
73     bool SetOctetValuePartData(uint8_t fieldId, uint8_t value, bool &match);
74     bool SetOctetValuePortionData(uint8_t fieldId, uint8_t value, bool &match);
75 
76     // encode value length feild
77     bool EncodeFieldExpriyValue(MmsEncodeBuffer &buff, int64_t value);
78     bool EncodeFieldFromValue(MmsEncodeBuffer &buff, std::vector<MmsAddress> &addr);
79     bool EcondeFieldMessageClassValue(MmsEncodeBuffer &buff);
80     // end
81 
82     bool EncodeCommontFieldValue(MmsEncodeBuffer &buff);
83     bool EncodeMultipleAddressValue(MmsEncodeBuffer &buff, uint8_t fieldId, std::vector<MmsAddress> &addrs);
84 
85     bool EncodeMmsSendReq(MmsEncodeBuffer &buff);
86     bool EncodeMmsSendConf(MmsEncodeBuffer &buff);
87     bool EncodeMmsNotificationInd(MmsEncodeBuffer &buff);
88     bool EnocdeMmsNotifyRespInd(MmsEncodeBuffer &buff);
89     bool EnocdeMmsRetrieveConf(MmsEncodeBuffer &buff);
90     bool EnocdeMmsAcknowledgeInd(MmsEncodeBuffer &buff);
91     bool EnocdeMmsDeliveryInd(MmsEncodeBuffer &buff);
92     bool EncodeMmsReadRecInd(MmsEncodeBuffer &buff);
93     bool EncodeMmsReadOrigInd(MmsEncodeBuffer &buff);
94 
95     void TrimString(std::string &str);  // trim string two side <>
96     bool EncodeOctetValue(MmsEncodeBuffer &buff, uint8_t fieldId, uint8_t value);
97     bool EncodeTextStringValue(MmsEncodeBuffer &buff, uint8_t fieldId, std::string value);
98     bool EncodeEncodeStringValue(MmsEncodeBuffer &buff, uint8_t fieldId, MmsEncodeString value);
99     bool EncodeShortIntegerValue(MmsEncodeBuffer &buff, uint8_t fieldId, int64_t value);
100     bool EncodeLongIntergerValue(MmsEncodeBuffer &buff, uint8_t fieldId, int64_t value);
101 
102     bool EncodeOctetValueFromMap(MmsEncodeBuffer &buff, uint8_t fieldId);
103     bool EncodeTextStringValueFromMap(MmsEncodeBuffer &buff, uint8_t fieldId);
104     bool EnocdeEncodeStringValueFromMap(MmsEncodeBuffer &buff, uint8_t fieldId);
105     bool EnocdeShortIntegerValueFromMap(MmsEncodeBuffer &buff, uint8_t fieldId);
106     bool EncodeLongIntergerValueFromMap(MmsEncodeBuffer &buff, uint8_t fieldId);
107 
108     bool IsHaveTransactionId(uint8_t messageType);
109     bool CheckResponseStatus(uint8_t value);
110     bool CheckRetrieveStatus(uint8_t value);
111     bool CheckStoreStatus(uint8_t value);
112     bool CheckBooleanValue(uint8_t fieldId, uint8_t value);
113 
114     using Func = std::function<bool(uint8_t fieldId, MmsDecodeBuffer &buff, int32_t &len)>;
115     std::map<uint8_t, Func> memberFuncMap_;
116 
117 private:
118     std::map<uint8_t, uint8_t> octetValueMap_;
119     std::map<uint8_t, int64_t> longValueMap_;
120     std::map<uint8_t, std::string> textValueMap_;
121     std::map<uint8_t, MmsEncodeString> encodeStringsMap_;
122     std::map<uint8_t, std::vector<MmsAddress>> addressMap_;
123     MmsContentType mmsContentType_;
124     MmsHeaderCateg mmsHaderCateg_;
125 };
126 } // namespace Telephony
127 } // namespace OHOS
128 #endif
129