• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2 * Copyright (c) 2024 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 OHOS_DSCHED_COLLAB_SESSSION_HEADER_H
17 #define OHOS_DSCHED_COLLAB_SESSSION_HEADER_H
18 
19 #include "av_trans_data_buffer.h"
20 #include <map>
21 #include <memory>
22 
23 namespace OHOS {
24 namespace DistributedCollab {
25 enum class FRAG_TYPE : uint8_t {
26     FRAG_NULL = 0,
27     FRAG_START,
28     FRAG_MID,
29     FRAG_END,
30     FRAG_START_END,
31 };
32 
33 enum class TLV_TYPE : uint16_t {
34     TLV_TYPE_NULL = 0,
35     TLV_TYPE_VERSION = 1001,
36     TLV_TYPE_FRAG_FLAG = 1002,
37     TLV_TYPE_DATA_TYPE = 1003,
38     TLV_TYPE_SEQ_NUM = 1004,
39     TLV_TYPE_TOTAL_LEN = 1005,
40     TLV_TYPE_SUB_SEQ = 1006,
41     TLV_TYPE_PAYLOAD_LEN = 1007,
42     TLV_TYPE_PACKET_LEN = 1008
43 };
44 
45 struct TlvItem {
46     TLV_TYPE type = TLV_TYPE::TLV_TYPE_NULL;
47     uint16_t len = 0;
48     uint32_t value = 0;
49 
50     static constexpr uint16_t HEADER_TYPE_BYTES = sizeof(type);
51     static constexpr uint16_t HEADER_LEN_BYTES = sizeof(len);
52 };
53 
54 class SessionDataHeader {
55 public:
56     static std::optional<SessionDataHeader> Deserialize(const uint8_t* buffer, const size_t bufLen);
57 
58 public:
59     explicit SessionDataHeader() = default;
SessionDataHeader(uint16_t version,FRAG_TYPE fragFlag,uint32_t dataType,uint32_t seqNum,uint32_t totalLen,uint32_t packetLen,uint32_t payloadLen,uint16_t subSeq)60     explicit SessionDataHeader(
61         uint16_t version,
62         FRAG_TYPE fragFlag,
63         uint32_t dataType,
64         uint32_t seqNum,
65         uint32_t totalLen,
66         uint32_t packetLen,
67         uint32_t payloadLen,
68         uint16_t subSeq)
69         : version_(version),
70         fragFlag_(fragFlag),
71         dataType_(dataType),
72         seqNum_(seqNum),
73         totalLen_(totalLen),
74         packetLen_(packetLen),
75         payloadLen_(payloadLen),
76         subSeq_(subSeq) {};
77     ~SessionDataHeader() = default;
78     std::unique_ptr<AVTransDataBuffer> Serialize();
79 
80 public:
81     static constexpr uint16_t PROTOCOL_VERSION = 1;
82     static constexpr uint16_t HEADER_TLV_NUM = 8;
83     static constexpr uint16_t HEADER_UINT8_NUM = 1;
84     static constexpr uint16_t HEADER_UINT16_NUM = 2;
85     static constexpr uint16_t HEADER_UINT32_NUM = 5;
86     static constexpr uint32_t HEADER_LEN = 2 * sizeof(uint16_t) * HEADER_TLV_NUM
87         + sizeof(uint16_t) * HEADER_UINT16_NUM
88         + sizeof(uint32_t) * HEADER_UINT32_NUM
89         + sizeof(uint8_t) * HEADER_UINT8_NUM;
90     static constexpr uint32_t BINARY_DATA_MAX_TOTAL_LEN = 100 * 1024 * 1024;
91     static constexpr uint32_t BINARY_PAYLOAD_MAX_LEN = 4 * 1024 * 1024;
92 
93 public:
94     uint16_t version_ = PROTOCOL_VERSION;
95     FRAG_TYPE fragFlag_;
96     uint32_t dataType_;
97     uint32_t seqNum_;
98     // assign whole data size
99     uint32_t totalLen_;
100     uint32_t packetLen_;
101     uint32_t payloadLen_;
102     // assign packet num
103     uint16_t subSeq_;
104 
105 private:
106     static constexpr uint32_t BASE_HEADER_LEN = 2 * sizeof(uint16_t) * HEADER_TLV_NUM
107         + sizeof(uint16_t) * HEADER_UINT16_NUM
108         + sizeof(uint32_t) * HEADER_UINT32_NUM
109         + sizeof(uint8_t) * HEADER_UINT8_NUM;
110 
111 private:
112     int32_t WriteTlvItemToBuffer(const TlvItem& tlvItem, uint8_t* header,
113         const uint32_t bufLen);
114     uint32_t WriteVersion(uint8_t* header, const uint32_t bufLen);
115     uint32_t WriteFragFlag(uint8_t* header, const uint32_t bufLen);
116     uint32_t WriteDataType(uint8_t* header, const uint32_t bufLen);
117     uint32_t WriteSeqNum(uint8_t* header, const uint32_t bufLen);
118     uint32_t WriteTotalLen(uint8_t* header, const uint32_t bufLen);
119     uint32_t WritePacketLen(uint8_t* header, const uint32_t bufLen);
120     uint32_t WritePayloadLen(uint8_t* header, const uint32_t bufLen);
121     uint32_t WriteSubSeq(uint8_t* header, const uint32_t bufLen);
122 
123     int32_t ReadTlvItemFromBuffer(TlvItem& tlvItem, SessionDataHeader& sessionHeader,
124         const uint8_t* header, const uint8_t* end);
125     uint16_t ReadUint16(const uint8_t* header);
126     int32_t ReadVersionFromBuffer(SessionDataHeader&, const uint8_t* header, const uint32_t len);
127     int32_t ReadFragFlagFromBuffer(SessionDataHeader&, const uint8_t* header, const uint32_t len);
128     int32_t ReadDataTypeFromBuffer(SessionDataHeader&, const uint8_t* header, const uint32_t len);
129     int32_t ReadSeqNumFromBuffer(SessionDataHeader&, const uint8_t* header, const uint32_t len);
130     int32_t ReadTotalLenFromBuffer(SessionDataHeader&, const uint8_t* header, const uint32_t len);
131     int32_t ReadPacketLenFromBuffer(SessionDataHeader&, const uint8_t* header, const uint32_t len);
132     int32_t ReadPayloadLenFromBuffer(SessionDataHeader&, const uint8_t* header, const uint32_t len);
133     int32_t ReadSubSeqFromBuffer(SessionDataHeader&, const uint8_t* header, const uint32_t len);
134 };
135 } // namespace DistributedCollab
136 } // namespace OHOS
137 #endif
138