-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
372 lines (297 loc) · 13.9 KB
/
main.cpp
File metadata and controls
372 lines (297 loc) · 13.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
#include <QCoreApplication>
#include <QRandomGenerator>
#include <QDebug>
#define USER_DATA_LENGTH 8
extern "C" {
#include "../../../Libs/porter/Source/porter.h"
}
// Обвертка данных пользователя и проверка структуры пакета
void testcase_wrap(); //Визуальная проверка 1
void testcase_wrap2(); //Визуальная проверка 2
void testcase_wrapAutoCheck(int iterationsCount); //Автоматическая проверка
//Проверка отправки и отправки по timeout
void testcase_sendInSpace(int testTime);
//Отправка пакета Porter`ом и получение им исскуственного подтверждения
void testcase_singleLoop();
//Замыкание двух Porter`ов друг на друга и отправка одним пакета данных, а другим подтверждения
void testcase_singleLoopReal();
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
qDebug() << "(System) Test begin";
testcase_wrap();
testcase_wrap2();
testcase_wrapAutoCheck(1'000'000);
testcase_sendInSpace(2'000);
testcase_singleLoop();
testcase_singleLoopReal();
qDebug() << "\n(System) Test end";
return a.exec();
}
void testcase_wrap()
{
qDebug() << "\n************************************ Wrap case ************************************";
porter_t porter;
uint8_t init_result = porter_init(&porter, 200);
if (init_result == PORTER_ERROR) {
qDebug() << "[ ERROR ] Init porter";
exit(-1);
}
uint8_t porter_tx_buff[USER_DATA_LENGTH + PORTER_SERVICE_BYTES_COUNT];
uint8_t userData[USER_DATA_LENGTH] = { 1, 2, 3, 4, 5, 6, 7, 8 };
porter_send(&porter, porter_tx_buff, userData, USER_DATA_LENGTH);
qDebug() << "User data: " << QByteArray((char*)userData, USER_DATA_LENGTH).toHex('.');
qDebug() << "Wrappered data: " << QByteArray((char*)porter_tx_buff, USER_DATA_LENGTH + PORTER_SERVICE_BYTES_COUNT).toHex('.');
qDebug() << "Test case result: [ OK ]";
}
void testcase_wrap2()
{
qDebug() << "\n************************************ Wrap case 2 ************************************";
porter_t porter;
uint8_t init_result = porter_init(&porter, 200);
if (init_result == PORTER_ERROR) {
qDebug() << "[ ERROR ] Init porter";
exit(-1);
}
uint8_t porter_tx_buff[USER_DATA_LENGTH + PORTER_SERVICE_BYTES_COUNT];
uint8_t userData[USER_DATA_LENGTH] = { 0x53, 0x12, 0x33, 0x34, 0x55, 0x26, 0x47, 0x08 };
porter_send(&porter, porter_tx_buff, userData, USER_DATA_LENGTH);
qDebug() << "User data: " << QByteArray((char*)userData, USER_DATA_LENGTH).toHex('.');
qDebug() << "Wrappered data: " << QByteArray((char*)porter_tx_buff, USER_DATA_LENGTH + PORTER_SERVICE_BYTES_COUNT).toHex('.');
qDebug() << "Test case result: [ OK ]";
}
void testcase_wrapAutoCheck(int iterationsCount)
{
qDebug() << "\n************************************ Auto check wrap ************************************";
porter_t porter;
uint8_t porter_tx_buff[USER_DATA_LENGTH + PORTER_SERVICE_BYTES_COUNT];
uint8_t userData[USER_DATA_LENGTH];
for (int i = 0; i < iterationsCount; ++i) {
int randomUserDataLength = QRandomGenerator::global()->bounded(1, 9);
for (int j = 0; j < randomUserDataLength; ++j) {
userData[j] = QRandomGenerator::global()->bounded(0, 256);
}
uint8_t init_result = porter_init(&porter, 200);
if (init_result == PORTER_ERROR) {
qDebug() << "[ ERROR ] Init porter";
exit(-1);
}
porter_send(&porter, porter_tx_buff, userData, USER_DATA_LENGTH);
if (porter_tx_buff[0] != PORTER_DATA_PACK_ID || porter_tx_buff[1] != 0) {
qDebug() << "[ ERROR ] Porter pack header error";
exit(-1);
}
for (int j = 0; j < randomUserDataLength; ++j) {
if (porter_tx_buff[j + 2] != userData[j]) {
qDebug() << "[ ERROR ] User data in Porter package is damaged";
exit(-1);
}
}
}
qDebug() << "Iterations: " << iterationsCount;
qDebug() << "Test case result: [ OK ]";
}
void testcase_sendInSpace(int testTime)
{
qDebug() << "\n************************************ Send in space case ************************************";
porter_t porter;
uint8_t init_result = porter_init(&porter, 100);
if (init_result == PORTER_ERROR) {
qDebug() << "[ ERROR ] Init porter";
exit(-1);
}
uint8_t porter_tx_buff[USER_DATA_LENGTH + PORTER_SERVICE_BYTES_COUNT];
uint8_t userData[USER_DATA_LENGTH];
int randomUserDataLength = QRandomGenerator::global()->bounded(1, 9);
for (int j = 0; j < randomUserDataLength; ++j) {
userData[j] = QRandomGenerator::global()->bounded(0, 256);
}
porter_send(&porter, porter_tx_buff, userData, USER_DATA_LENGTH);
clock_t getData = 0;
clock_t testStartTime = clock();
while(1) {
porter_frame_t frame = porter_process(&porter, NULL, 0, clock());
if (frame.data != NULL && frame.length != 0) {
getData = clock();
}
if ((clock() - getData) > 150) {
qDebug() << "[ ERROR ] Porter must give the data to send. Timeout.";
exit(-1);
}
if ((clock() - testStartTime) > testTime) {
break;
}
}
qDebug() << "Test case result: [ OK ]";
}
void testcase_singleLoop()
{
qDebug() << "\n************************************ Single loop case ************************************";
porter_t porter;
uint8_t init_result = porter_init(&porter, 200);
if (init_result == PORTER_ERROR) {
qDebug() << "[ ERROR ] Init porter";
exit(-1);
}
uint8_t porter_tx_buff[USER_DATA_LENGTH + PORTER_SERVICE_BYTES_COUNT];
uint8_t userData[USER_DATA_LENGTH];
int randomUserDataLength = QRandomGenerator::global()->bounded(1, 9);
for (int j = 0; j < randomUserDataLength; ++j) {
userData[j] = QRandomGenerator::global()->bounded(0, 256);
}
porter_send(&porter, porter_tx_buff, userData, USER_DATA_LENGTH);
porter_frame_t frame = porter_process(&porter, NULL, 0, clock());
if (frame.data != NULL && frame.length != 0) {
if (porter_tx_buff[0] != PORTER_DATA_PACK_ID || porter_tx_buff[1] != 0) {
qDebug() << "[ ERROR ] Porter pack header error";
exit(-1);
}
for (int j = 0; j < randomUserDataLength; ++j) {
if (porter_tx_buff[j + 2] != userData[j]) {
qDebug() << "[ ERROR ] User data in Porter package is damaged";
exit(-1);
}
}
} else {
qDebug() << "[ ERROR ] Porter must give the data to send. Timeout.";
exit(-1);
}
uint8_t ack[1] = {PORTER_ACK_PACK_ID};
frame = porter_process(&porter, ack, 1, clock());
if (frame.data != NULL || frame.length != 0) {
qDebug() << "[ ERROR ] Porter get data to send after receive the ack";
}
qDebug() << "Test case result: [ OK ]";
}
void testcase_singleLoopReal()
{
qDebug() << "\n************************************ Real single loop case ************************************";
//ccsa
qDebug() << "Test case result: [ OK ]";
}
/*
void unansweredSend()
{
qDebug() << "\n***Unanswered send case";
uint8_t porter_tx_buff[USER_DATA_LENGTH + PORTER_SERVICE_BYTES_COUNT];
uint8_t userData[] = { 0x56, 0x12, 0x34, 0x24, 0x95, 0x56, 0x37, 0x18 };
porter_send(&porter, porter_tx_buff, userData, USER_DATA_LENGTH);
qDebug() << "User data: " << QByteArray((char*)userData, USER_DATA_LENGTH).toHex('.');
qDebug() << "Wrappered data: " << QByteArray((char*)porter_tx_buff, USER_DATA_LENGTH + PORTER_SERVICE_BYTES_COUNT).toHex('.') << "\n";
while(1) {
porter_frame_t frame = porter_process(&porter, NULL, 0, clock());
if (frame.length > 0) {
qDebug() << "Want send: " << QByteArray((char*)frame.data, frame.length).toHex('.');
}
}
}
void answeredSend()
{
qDebug() << "\n***Unanswered send case";
uint8_t porter_tx_buff[USER_DATA_LENGTH + PORTER_SERVICE_BYTES_COUNT];
uint8_t userData[] = { 0x56, 0x12, 0x34, 0x24, 0x95, 0x56, 0x37, 0x18 };
porter_send(&porter, porter_tx_buff, userData, USER_DATA_LENGTH);
qDebug() << "User data: " << QByteArray((char*)userData, USER_DATA_LENGTH).toHex('.');
qDebug() << "Wrappered data: " << QByteArray((char*)porter_tx_buff, USER_DATA_LENGTH + PORTER_SERVICE_BYTES_COUNT).toHex('.') << "\n";
uint8_t test_ack = PORTER_ACK_PACK_ID;
//uint8_t test_ack = 65;
while(1) {
porter_frame_t frame = porter_process(&porter, &test_ack, 1, clock());
if (frame.length > 0) {
qDebug() << "Want send: " << QByteArray((char*)frame.data, frame.length).toHex('.');
}
}
}
void sendAndRecv()
{
qDebug() << "\n***sendAndRecv case";
uint8_t porter_tx_buff[USER_DATA_LENGTH + PORTER_SERVICE_BYTES_COUNT];
uint8_t userData[] = { 0x56, 0x12, 0x34, 0x24, 0x95, 0x56, 0x37, 0x18 };
porter_send(&porter, porter_tx_buff, userData, USER_DATA_LENGTH);
qDebug() << "User data: " << QByteArray((char*)userData, USER_DATA_LENGTH).toHex('.');
qDebug() << "Wrappered data: " << QByteArray((char*)porter_tx_buff, USER_DATA_LENGTH + PORTER_SERVICE_BYTES_COUNT).toHex('.') << "\n";
uint8_t porter_rx_buff[USER_DATA_LENGTH + PORTER_SERVICE_BYTES_COUNT];
uint8_t porter_rx_buff_length = 0;
uint8_t porter2_rx_buff[USER_DATA_LENGTH + PORTER_SERVICE_BYTES_COUNT];
uint8_t porter2_rx_buff_length = 0;
porter_frame_t frame, frame2;
for(uint16_t i = 0; i < 5000; ++i) {
frame = porter_process(&porter, porter_rx_buff, porter_rx_buff_length, clock());
if (frame.length > 0) {
memset(porter_rx_buff, 0, USER_DATA_LENGTH + PORTER_SERVICE_BYTES_COUNT);
qDebug() << "Want send: " << QByteArray((char*)frame.data, frame.length).toHex('.');
memcpy(porter2_rx_buff, frame.data, frame.length);
porter2_rx_buff_length = frame.length;
}
frame2 = porter_process(&porter2, porter2_rx_buff, porter2_rx_buff_length, clock());
if (frame2.length > 0) {
memset(porter2_rx_buff, 0, USER_DATA_LENGTH + PORTER_SERVICE_BYTES_COUNT);
qDebug() << "Want send2: " << QByteArray((char*)frame2.data, frame2.length).toHex('.');
memcpy(porter_rx_buff, frame2.data, frame2.length);
porter_rx_buff_length = frame2.length;
}
}
}
void sendAndRecvInfinite()
{
qDebug() << "\n***sendAndRecv case";
uint8_t porter_tx_buff[USER_DATA_LENGTH + PORTER_SERVICE_BYTES_COUNT];
uint8_t userData[] = { 0x56, 0x12, 0x34, 0x24, 0x95, 0x56, 0x37, 0x18 };
porter_send(&porter, porter_tx_buff, userData, USER_DATA_LENGTH);
qDebug() << "User data: " << QByteArray((char*)userData, USER_DATA_LENGTH).toHex('.');
qDebug() << "Wrappered data: " << QByteArray((char*)porter_tx_buff, USER_DATA_LENGTH + PORTER_SERVICE_BYTES_COUNT).toHex('.') << "\n";
uint8_t porter_rx_buff[USER_DATA_LENGTH + PORTER_SERVICE_BYTES_COUNT];
uint8_t porter_rx_buff_length = 0;
uint8_t porter2_rx_buff[USER_DATA_LENGTH + PORTER_SERVICE_BYTES_COUNT];
uint8_t porter2_rx_buff_length = 0;
porter_frame_t frame, frame2;
while(1) {
frame = porter_process(&porter, porter_rx_buff, porter_rx_buff_length, clock());
if (frame.length > 0) {
memset(porter_rx_buff, 0, USER_DATA_LENGTH + PORTER_SERVICE_BYTES_COUNT);
qDebug() << "Want send: " << QByteArray((char*)frame.data, frame.length).toHex('.');
memcpy(porter2_rx_buff, frame.data, frame.length);
porter2_rx_buff_length = frame.length;
}
frame2 = porter_process(&porter2, porter2_rx_buff, porter2_rx_buff_length, clock());
if (frame2.length > 0) {
memset(porter2_rx_buff, 0, USER_DATA_LENGTH + PORTER_SERVICE_BYTES_COUNT);
qDebug() << "Want send2: " << QByteArray((char*)frame2.data, frame2.length).toHex('.');
memcpy(porter_rx_buff, frame2.data, frame2.length);
porter_rx_buff_length = frame2.length;
}
}
}
void sync()
{
qDebug() << "\n***sync case";
uint8_t porter_tx_buff[USER_DATA_LENGTH + PORTER_SERVICE_BYTES_COUNT];
uint8_t userData[] = { 0x56, 0x12, 0x34, 0x24, 0x95, 0x56, 0x37, 0x18 };
porter_send(&porter, porter_tx_buff, userData, USER_DATA_LENGTH);
qDebug() << "User data: " << QByteArray((char*)userData, USER_DATA_LENGTH).toHex('.');
qDebug() << "Wrappered data: " << QByteArray((char*)porter_tx_buff, USER_DATA_LENGTH + PORTER_SERVICE_BYTES_COUNT).toHex('.') << "\n";
uint8_t porter_rx_buff[USER_DATA_LENGTH + PORTER_SERVICE_BYTES_COUNT];
uint8_t porter_rx_buff_length = 0;
uint8_t porter2_rx_buff[USER_DATA_LENGTH + PORTER_SERVICE_BYTES_COUNT];
uint8_t porter2_rx_buff_length = 0;
porter.tx_pack_id = 23;
porter2.rx_pack_id = 54;
porter_frame_t frame, frame2;
while(1) {
frame = porter_process(&porter, porter_rx_buff, porter_rx_buff_length, clock());
if (frame.length > 0) {
memset(porter_rx_buff, 0, USER_DATA_LENGTH + PORTER_SERVICE_BYTES_COUNT);
qDebug() << "Want send: " << QByteArray((char*)frame.data, frame.length).toHex('.');
memcpy(porter2_rx_buff, frame.data, frame.length);
porter2_rx_buff_length = frame.length;
}
frame2 = porter_process(&porter2, porter2_rx_buff, porter2_rx_buff_length, clock());
if (frame2.length > 0) {
memset(porter2_rx_buff, 0, USER_DATA_LENGTH + PORTER_SERVICE_BYTES_COUNT);
qDebug() << "Want send2: " << QByteArray((char*)frame2.data, frame2.length).toHex('.');
memcpy(porter_rx_buff, frame2.data, frame2.length);
porter_rx_buff_length = frame2.length;
}
}
}
*/