• 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_MESSAGE_DATA_HEADER_H
17 #define OHOS_DSCHED_COLLAB_MESSAGE_DATA_HEADER_H
18 
19 #include "av_trans_data_buffer.h"
20 
21 #include <map>
22 #include <memory>
23 
24 namespace OHOS {
25 namespace DistributedCollab {
26     enum class MESSAGE_TLV_TYPE : uint16_t {
27         TLV_TYPE_NULL = 0,
28         TLV_TYPE_VERSION = 1001,
29         TLV_TYPE_DATA_TYPE = 1002,
30         TLV_TYPE_TOTAL_LEN = 1003,
31     };
32 
33     struct MessageTlvItem {
34         MESSAGE_TLV_TYPE type = MESSAGE_TLV_TYPE::TLV_TYPE_NULL;
35         uint16_t len = 0;
36         uint32_t value = 0;
37 
38         static constexpr uint16_t HEADER_TYPE_BYTES = sizeof(type);
39         static constexpr uint16_t HEADER_LEN_BYTES = sizeof(len);
40     };
41 
42     class MessageDataHeader {
43     public:
44         static std::optional<MessageDataHeader> Deserialize(const uint8_t* buffer, const size_t bufLen);
45 
46     public:
47         explicit MessageDataHeader() = default;
MessageDataHeader(uint16_t version,uint32_t dataType,uint32_t totalLen)48         explicit MessageDataHeader(
49             uint16_t version,
50             uint32_t dataType,
51             uint32_t totalLen)
52             : version_(version),
53             dataType_(dataType),
54             totalLen_(totalLen) {};
55         ~MessageDataHeader() = default;
56         std::unique_ptr<AVTransDataBuffer> Serialize();
57 
58     public:
59         static constexpr uint16_t PROTOCOL_VERSION = 1;
60         static constexpr uint16_t HEADER_TLV_NUM = 3;
61         static constexpr uint16_t HEADER_UINT16_NUM = 1;
62         static constexpr uint16_t HEADER_UINT32_NUM = 2;
63         static constexpr uint32_t HEADER_LEN = 2 * sizeof(uint16_t) * HEADER_TLV_NUM +
64             sizeof(uint16_t) * HEADER_UINT16_NUM + sizeof(uint32_t) * HEADER_UINT32_NUM;
65         static constexpr uint32_t BINARY_DATA_MAX_TOTAL_LEN = 100 * 1024 * 1024;
66         static constexpr uint32_t BINARY_PAYLOAD_MAX_LEN = 4 * 1024 * 1024;
67 
68     public:
69         uint16_t version_ = PROTOCOL_VERSION;
70         uint32_t dataType_;
71         uint32_t totalLen_;
72 
73     private:
74         static constexpr uint32_t BASE_HEADER_LEN = 2 * sizeof(uint16_t) * HEADER_TLV_NUM +
75             sizeof(uint16_t) * HEADER_UINT16_NUM + sizeof(uint32_t) * HEADER_UINT32_NUM;
76 
77     private:
78         int32_t WriteTlvItemToBuffer(const MessageTlvItem& tlvItem, uint8_t* header, const uint32_t bufLen);
79         uint32_t WriteVersion(uint8_t* header, const uint32_t bufLen);
80         uint32_t WriteDataType(uint8_t* header, const uint32_t bufLen);
81         uint32_t WriteTotalLen(uint8_t* header, const uint32_t bufLen);
82 
83         int32_t ReadTlvItemFromBuffer(MessageTlvItem& tlvItem, MessageDataHeader& sessionHeader,
84             const uint8_t* header, const uint8_t* end);
85         uint16_t ReadUint16(const uint8_t* header);
86         int32_t ReadVersionFromBuffer(MessageDataHeader&, const uint8_t* header, const uint32_t len);
87         int32_t ReadDataTypeFromBuffer(MessageDataHeader&, const uint8_t* header, const uint32_t len);
88         int32_t ReadTotalLenFromBuffer(MessageDataHeader&, const uint8_t* header, const uint32_t len);
89     };
90 } // namespace DistributedCollab
91 } // namespace OHOS
92 #endif