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 #ifndef HDC_SERIAL_STRUCT_H
16 #define HDC_SERIAL_STRUCT_H
17 
18 #include "serial_struct_define.h"
19 
20 #include <string>
21 #include <sstream>
22 
23 namespace Hdc {
24 using std::string;
25 namespace BaseStruct {
26     struct SessionHandShake {
27         string banner; // must first index
28         // auth none
29         uint8_t authType;
30         uint32_t sessionId;
31         string connectKey;
32         string buf;
33         string version;
ToDebugStringHdc::BaseStruct::SessionHandShake34         std::string ToDebugString() const
35         {
36             std::ostringstream oss;
37             oss << "SessionHandShake [";
38             oss << " banner:" << banner;
39             oss << " sessionId:" << sessionId;
40             oss << " authType:" << unsigned(authType);
41             oss << " connectKey:" << connectKey;
42             oss << " buf:" << buf;
43             oss << " version:" << version;
44             oss << " ]";
45             return oss.str();
46         }
47     };
48 
49     struct PayloadProtect {  // reserve for encrypt and decrypt
50         uint32_t channelId;
51         uint32_t commandFlag;
52         uint8_t checkSum;  // enable it will be lose about 20% speed
53         uint8_t vCode;
54     };
55 
56     struct TransferConfig {
57         uint64_t fileSize;
58         uint64_t atime;  // ns
59         uint64_t mtime;  // ns
60         string options;
61         string path;
62         string optionalName;
63         bool updateIfNew;
64         uint8_t compressType;
65         bool holdTimestamp;
66         string functionName;
67         string clientCwd;
68         string reserve1;
69         string reserve2;
70     };
71 
72     struct FileMode {
73         uint64_t perm;
74         uint64_t u_id;
75         uint64_t g_id;
76         string context;
77         string fullName;
78     };
79 
80     // used for HdcTransferBase. just base class use, not public
81     struct TransferPayload {
82         uint64_t index;
83         uint8_t compressType;
84         uint32_t compressSize;
85         uint32_t uncompressSize;
86     };
87 } // BaseStruct
88 
89 namespace RustStruct {
90     struct SessionHandShake {
91         const char* banner; // must first index
92         // auth none
93         uint8_t authType;
94         uint32_t sessionId;
95         const char* connectKey;
96         const char* buf;
97         const char* version;
98     };
99 
100     struct PayloadProtect {  // reserve for encrypt and decrypt
101         uint32_t channelId;
102         uint32_t commandFlag;
103         uint8_t checkSum;  // enable it will be lose about 20% speed
104         uint8_t vCode;
105     };
106 
107     struct TransferConfig {
108         uint64_t fileSize;
109         uint64_t atime;  // ns
110         uint64_t mtime;  // ns
111         const char* options;
112         const char* path;
113         const char* optionalName;
114         uint8_t updateIfNew;
115         uint8_t compressType;
116         uint8_t holdTimestamp;
117         const char* functionName;
118         const char* clientCwd;
119         const char* reserve1;
120         const char* reserve2;
121     };
122 
123     struct FileMode {
124         uint64_t perm;
125         uint64_t u_id;
126         uint64_t g_id;
127         const char* context;
128         const char* fullName;
129     };
130 
131     struct TransferPayload {
132         uint64_t index;
133         uint8_t compressType;
134         uint32_t compressSize;
135         uint32_t uncompressSize;
136     };
137 
138     struct PayloadHead {
139         uint8_t flag[2];
140         uint8_t reserve[2];  // encrypt'flag or others options
141         uint8_t protocolVer;
142         uint16_t headSize;
143         uint32_t data_size;
144     } __attribute__((packed));
145 
146     struct USBHead {
147         uint8_t flag[2];
148         uint8_t option;
149         uint32_t sessionId;
150         uint32_t data_size;
151     } __attribute__((packed));
152 
153     struct UartHead {
154         uint8_t flag[2];
155         uint16_t option;
156         uint32_t sessionId;
157         uint32_t data_size;
158         uint32_t package_index;
159         uint32_t data_checksum;
160         uint32_t head_checksum;
161     } __attribute__((packed));
162 } // RustStruct
163 
164 namespace SerialStruct {
165     constexpr int fieldOne = 1;
166     constexpr int fieldTwo = 2;
167     constexpr int fieldThree = 3;
168     constexpr int fieldFour = 4;
169     constexpr int fieldFive = 5;
170     constexpr int fieldSix = 6;
171     constexpr int fieldSeven = 7;
172     constexpr int fieldEight = 8;
173     constexpr int fieldNine = 9;
174     constexpr int fieldTen = 10;
175     constexpr int field11 = 11;
176     constexpr int field12 = 12;
177     constexpr int field13 = 13;
178 
179     template<> struct Descriptor<BaseStruct::TransferConfig> {
typeHdc::SerialStruct::Descriptor180         static auto type()
181         {
182             return Message(Field<fieldOne, &BaseStruct::TransferConfig::fileSize>("fileSize"),
183                            Field<fieldTwo, &BaseStruct::TransferConfig::atime>("atime"),
184                            Field<fieldThree, &BaseStruct::TransferConfig::mtime>("mtime"),
185                            Field<fieldFour, &BaseStruct::TransferConfig::options>("options"),
186                            Field<fieldFive, &BaseStruct::TransferConfig::path>("path"),
187                            Field<fieldSix, &BaseStruct::TransferConfig::optionalName>("optionalName"),
188                            Field<fieldSeven, &BaseStruct::TransferConfig::updateIfNew>("updateIfNew"),
189                            Field<fieldEight, &BaseStruct::TransferConfig::compressType>("compressType"),
190                            Field<fieldNine, &BaseStruct::TransferConfig::holdTimestamp>("holdTimestamp"),
191                            Field<fieldTen, &BaseStruct::TransferConfig::functionName>("functionName"),
192                            Field<field11, &BaseStruct::TransferConfig::clientCwd>("clientCwd"),
193                            Field<field12, &BaseStruct::TransferConfig::reserve1>("reserve1"),
194                            Field<field13, &BaseStruct::TransferConfig::reserve2>("reserve2"));
195         }
196     };
197 
198     template<> struct Descriptor<BaseStruct::FileMode> {
typeHdc::SerialStruct::Descriptor199         static auto type()
200         {
201             return Message(Field<fieldOne, &BaseStruct::FileMode::perm>("perm"),
202                            Field<fieldTwo, &BaseStruct::FileMode::u_id>("u_id"),
203                            Field<fieldThree, &BaseStruct::FileMode::g_id>("g_id"),
204                            Field<fieldFour, &BaseStruct::FileMode::context>("context"),
205                            Field<fieldFive, &BaseStruct::FileMode::fullName>("fullName"));
206         }
207     };
208 
209     template<> struct Descriptor<BaseStruct::TransferPayload> {
typeHdc::SerialStruct::Descriptor210         static auto type()
211         {
212             return Message(Field<fieldOne, &BaseStruct::TransferPayload::index>("index"),
213                            Field<fieldTwo, &BaseStruct::TransferPayload::compressType>("compressType"),
214                            Field<fieldThree, &BaseStruct::TransferPayload::compressSize>("compressSize"),
215                            Field<fieldFour, &BaseStruct::TransferPayload::uncompressSize>("uncompressSize"));
216         }
217     };
218 
219     template<> struct Descriptor<BaseStruct::SessionHandShake> {
typeHdc::SerialStruct::Descriptor220         static auto type()
221         {
222             return Message(Field<fieldOne, &BaseStruct::SessionHandShake::banner>("banner"),
223                            Field<fieldTwo, &BaseStruct::SessionHandShake::authType>("authType"),
224                            Field<fieldThree, &BaseStruct::SessionHandShake::sessionId>("sessionId"),
225                            Field<fieldFour, &BaseStruct::SessionHandShake::connectKey>("connectKey"),
226                            Field<fieldFive, &BaseStruct::SessionHandShake::buf>("buf"),
227                            Field<fieldSix, &BaseStruct::SessionHandShake::version>("version"));
228         }
229     };
230 
231     template<> struct Descriptor<BaseStruct::PayloadProtect> {
typeHdc::SerialStruct::Descriptor232         static auto type()
233         {
234             return Message(Field<fieldOne, &BaseStruct::PayloadProtect::channelId>("channelId"),
235                            Field<fieldTwo, &BaseStruct::PayloadProtect::commandFlag>("commandFlag"),
236                            Field<fieldThree, &BaseStruct::PayloadProtect::checkSum>("checkSum"),
237                            Field<fieldFour, &BaseStruct::PayloadProtect::vCode>("vCode"));
238         }
239     };
240 }  // SerialStruct
241 }  // Hdc
242 #endif  // HDC_SERIAL_STRUCT_H
243