• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 NSTACKX_DFILE_FRAME_H
17 #define NSTACKX_DFILE_FRAME_H
18 
19 #include <stdint.h>
20 #include <time.h>
21 #include "nstackx_list.h"
22 #include "nstackx_file_list.h"
23 #include "nstackx_congestion.h"
24 
25 #ifdef __cplusplus
26 extern "C" {
27 #endif
28 
29 /* RST_CODE */
30 #define NSTACKX_DFILE_NO_ERROR                200
31 #define NSTACKX_DFILE_WITHOUT_SETTING_ERROR   201
32 #define NSTACKX_DFILE_FILE_WRITE_ERROR        202
33 #define NSTACKX_DFILE_FILE_READ_ERROR         203
34 #define NSTACKX_DFILE_NO_ENOUGH_STORAGE_ERROR 204
35 #define NSTACKX_DFILE_INTERNAL_ERROR          209
36 #define NSTACKX_DFILE_CANCEL_ERROR            210
37 
38 #define NSTACKX_DFILE_HEADER_FRAME_USER_DATA_FLAG 0x1
39 #define NSTACKX_DFILE_HEADER_FRAME_PATH_TYPE_FLAG 0x2
40 #define NSTACKX_DFILE_HEADER_FRAME_NO_SYNC_FLAG 0x4
41 #define NSTACKX_DFILE_HEADER_FRAME_VTRANS_FLAG 0x8
42 
43 #define NSTACKX_DFILE_DATA_FRAME_START_FLAG 0x0
44 #define NSTACKX_DFILE_DATA_FRAME_CONTINUE_FLAG 0x1
45 #define NSTACKX_DFILE_DATA_FRAME_END_FLAG 0x3
46 #define NSTACKX_DFILE_DATA_FRAME_RETRAN_FLAG 0x4
47 
48 #define NSTACKX_DFILE_ACK_RETRAN_FILE_FLAG 0x1
49 
50 #define NSTACKX_RESERVED_FILE_ID 0
51 #define NSTACKX_FIRST_FILE_ID 1
52 
53 #define NSTACKX_MAX_FRAME_SIZE              14720
54 #define NSTACKX_DEFAULT_FRAME_SIZE          1472
55 #define NSTACKX_MAX_USER_DATA_SIZE          1024
56 #define NSTACKX_MAX_FILE_SIZE               (0x7FFFFFFFFF) /* Max file size 512GB */
57 #define NSTACKX_MIN_MTU_SIZE                64
58 
59 typedef enum {
60     NSTACKX_DFILE_FILE_HEADER_FRAME = 1,
61     NSTACKX_DFILE_FILE_HEADER_CONFIRM_FRAME,
62     NSTACKX_DFILE_FILE_TRANSFER_REQ_FRAME,
63     NSTACKX_DFILE_FILE_DATA_FRAME,
64     NSTACKX_DFILE_FILE_DATA_ACK_FRAME,
65     NSTACKX_DFILE_FILE_TRANSFER_DONE_FRAME,
66     NSTACKX_DFILE_FILE_TRANSFER_DONE_ACK_FRAME,
67     NSTACKX_DFILE_SETTING_FRAME,
68     NSTACKX_DFILE_RST_FRAME,
69     NSTACKX_DFILE_FLOW_CONTROL_FRAME,
70     NSTACKX_DFILE_CONGESTION_CONTROL_FRAME,
71     NSTACKX_DFILE_PEER_DOWN_FRAME,
72     NSTACKX_DFILE_FILE_BACK_PRESSURE_FRAME,
73     NSTACKX_DFILE_TYPE_MAX
74 } DFileFrameType;
75 
76 #pragma pack(push, 1)
77 typedef struct {
78     uint8_t type;
79     uint8_t flag;
80     uint16_t sessionId;
81     uint16_t transId;
82     uint16_t length;
83 } DFileFrameHeader;
84 
85 #define DFILE_FRAME_HEADER_LEN sizeof(DFileFrameHeader)
86 
87 typedef struct {
88     uint16_t fileId;
89     uint64_t fileSize;
90     uint16_t fileNameLength;
91     uint8_t fileName[0];
92 } FileInfoUnit;
93 
94 typedef struct {
95     uint16_t fileId;
96     uint64_t fileSize;
97     uint16_t fileNameLength;
98     uint64_t startOffset;
99     uint8_t fileName[0];
100 } FileInfoUnitMp;
101 
102 typedef struct {
103     uint16_t pathType;
104     uint8_t userData[0];
105 } UserDataUnit;
106 
107 typedef struct {
108     DFileFrameHeader header;
109     uint16_t nodeNumber;
110     uint8_t fileInfoUnit[0];
111 } FileHeaderFrame;
112 
113 typedef struct {
114     DFileFrameHeader header;
115     uint16_t fileId[0];
116 } FileHeaderConfirmFrame;
117 
118 typedef struct {
119     DFileFrameHeader header;
120     uint16_t fileId[0];
121 } FileTransferReqFrame;
122 
123 typedef struct {
124     DFileFrameHeader header;
125     uint16_t fileId;
126     uint32_t blockSequence;
127     uint8_t blockPayload[0];
128 } FileDataFrame;
129 
130 typedef struct {
131     DFileFrameHeader header;
132     uint16_t fileId;
133     uint32_t blockSequence;
134     uint32_t linkSequence;
135     uint8_t blockPayload[0];
136 } FileDataFrameZS;
137 
138 typedef struct {
139     uint8_t recvListOverIo;
140     uint8_t recvBufThreshold; /* for reserve */
141     uint32_t stopSendPeriod;
142 } DataBackPressure;
143 
144 typedef struct {
145     DFileFrameHeader header;
146     DataBackPressure backPressure;
147 } BackPressureFrame;
148 
149 typedef struct {
150     DFileFrameHeader header;
151     uint16_t fileId[0];
152 } FileTransferDoneFrame;
153 
154 typedef struct {
155     DFileFrameHeader header;
156     uint16_t fileId[0];
157 } FileTransferDoneAckFrame;
158 
159 typedef struct {
160     DFileFrameHeader header;
161     uint16_t mtu;
162     uint16_t connType;
163     uint32_t dFileVersion;
164     uint32_t abmCapability; /* keep the same format of SettingFrame with Hicomm branch */
165     uint32_t capability;
166     uint32_t dataFrameSize;
167     uint32_t capsCheck;
168 } SettingFrame;
169 
170 typedef struct {
171     DFileFrameHeader header;
172     uint16_t code;
173     uint16_t fileId[0];
174 } RstFrame;
175 
176 typedef struct {
177     DFileFrameHeader header;
178     WifiStationInfo wifiStationInfo;
179 } CongestionControlFrame;
180 #pragma pack(pop)
181 
182 typedef struct {
183     uint32_t flowCtrlType;
184     uint32_t kernelRecvQueueDropPacket;
185     uint32_t kernelRecvQueueRate;
186     uint32_t appProcessQueueRatio;
187     uint32_t appProcessQueueRate;
188     uint32_t sysTemperature;
189 } FlowCtrlInfo;
190 
191 #pragma pack(push, 1)
192 typedef struct {
193     DFileFrameHeader header;
194     FlowCtrlInfo flowCtrlInfo;
195 } FlowCtrlFrame;
196 
197 typedef struct {
198     DFileFrameHeader header;
199     uint8_t payload[0];
200 } DFileFrame;
201 #pragma pack(pop)
202 
SetDfileFrameTransID(DFileFrame * dFileFrame,uint16_t value)203 static inline void SetDfileFrameTransID(DFileFrame *dFileFrame, uint16_t value)
204 {
205     (dFileFrame)->header.transId = htons(value);
206     return;
207 }
208 
SetDfileFrameUserDataFlag(DFileFrameHeader * header)209 static inline void SetDfileFrameUserDataFlag(DFileFrameHeader *header)
210 {
211     header->flag |= NSTACKX_DFILE_HEADER_FRAME_USER_DATA_FLAG;
212     return;
213 }
214 
SetDfileFramePathTypeFlag(DFileFrameHeader * header)215 static inline void SetDfileFramePathTypeFlag(DFileFrameHeader *header)
216 {
217     header->flag |= NSTACKX_DFILE_HEADER_FRAME_PATH_TYPE_FLAG;
218     return;
219 }
220 
SetDfileFrameNoSyncFlag(DFileFrameHeader * header)221 static inline void SetDfileFrameNoSyncFlag(DFileFrameHeader *header)
222 {
223     header->flag |= NSTACKX_DFILE_HEADER_FRAME_NO_SYNC_FLAG;
224 }
225 
CheckDfileFrameEndFlag(FileDataFrame * fileDataFrame)226 static inline uint8_t CheckDfileFrameEndFlag(FileDataFrame *fileDataFrame)
227 {
228     return (((fileDataFrame)->header.flag & NSTACKX_DFILE_DATA_FRAME_END_FLAG) == NSTACKX_DFILE_DATA_FRAME_END_FLAG);
229 }
230 
231 const char *GetFrameName(DFileFrameType frameType);
232 void EncodeFileHeaderFrame(FileList *fileList, int32_t *fileId, uint8_t *buffer, size_t length, size_t *frameLength);
233 void EncodeFileHeaderConfirmFrame(FileList *fileList, uint16_t *fileId, uint8_t *buffer, size_t length,
234                                   size_t *frameLength);
235 int32_t EncodeFileDataFrame(FileList *fileList, uint16_t fileId, uint8_t *buffer, size_t length, size_t *frameLength);
236 void EncodeFileTransferDoneFrame(uint8_t *buffer, size_t length, uint16_t fileIdList[], uint32_t fileIdNum,
237                                  size_t *frameLength);
238 void EncodeFileTransferDoneAckFrame(uint8_t *buffer, size_t length, uint16_t transId, size_t *frameLength);
239 void EncodeSettingFrame(uint8_t *buffer, size_t length, size_t *frameLength, const SettingFrame *settingFramePara);
240 void EncodeRstFrame(uint8_t *buffer, size_t length, size_t *frameLength, uint16_t transId, uint16_t errCode);
241 void EncodeBackPressFrame(uint8_t *buffer, size_t length, size_t *frameLength, uint8_t recvListOverIo);
242 int32_t DecodeDFileFrame(const uint8_t *buffer, size_t bufferLength, DFileFrame **frame);
243 int32_t DecodeFileHeaderFrame(FileList *fileList, FileHeaderFrame *headerFrame);
244 int32_t DecodeFileHeaderConfirmFrame(FileList *fileList, FileHeaderConfirmFrame *confirmFrame);
245 int32_t DecodeFileDataFrame(FileList *fileList, FileDataFrame *dataFrame);
246 int16_t GetFileIdFromFileDataFrame(const FileList *fileList, const FileDataFrame *fileDataFrame);
247 int32_t DecodeFileTransferDoneFrame(FileList *fileList, FileTransferDoneFrame *transferDoneFrame);
248 int32_t DecodeSettingFrame(SettingFrame *netSettingFrame, SettingFrame *hostSettingFrame);
249 int32_t DecodeRstFrame(RstFrame *rstFrame, uint16_t *code, uint16_t **fileIdList, uint16_t *listCount);
250 int32_t DecodeBackPressFrame(const BackPressureFrame *backPressFrame, DataBackPressure *backPressInfo);
251 
252 #ifdef __cplusplus
253 }
254 #endif
255 
256 #endif  /* #ifndef NSTACKX_DFILE_FRAME_H */
257