-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathSeanet.h
2167 lines (1875 loc) · 74.3 KB
/
Seanet.h
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
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Prevent Visual Studio Intellisense from defining _WIN32 and _MSC_VER when we use
// Visual Studio to edit Linux or Borland C++ code.
#ifdef __linux__
# undef _WIN32
#endif // __linux__
#if defined(__GNUC__) || defined(__BORLANDC__)
# undef _MSC_VER
#endif // defined(__GNUC__) || defined(__BORLANDC__)
#ifndef SEANET_H
#define SEANET_H
#include "OSMisc.h"
#include "RS232Port.h"
#ifndef DISABLE_SEANETTHREAD
#include "OSThread.h"
#endif // !DISABLE_SEANETTHREAD
#define TIMEOUT_MESSAGE_SEANET 4.0 // In s.
// Should be at least 2 * number of bytes to be sure to contain entirely the biggest desired message (or group of messages) + 1.
#define MAX_NB_BYTES_SEANET 2048
#pragma region SEANET-SPECIFIC DEFINITIONS
#define MAX_NUMBER_OF_STEPS_SEANET 6400
#define MIN_MESSAGE_LEN_SEANET 11
#define TIMEOUT_REBOOT_SEANET 30.0 // In s.
#define TIMEOUT_BBUSER_SEANET 10.0 // In s.
#define TIMEOUT_HEADCOMMAND_SEANET 10.0 // In s.
#define MESSAGE_HEADER_SEANET 0x40 // '@'.
#define MESSAGE_TERMINATOR_SEANET 0x0a // LF.
#define SERIAL_PORT_SONAR_NODE_NUMBER_SEANET 2
#define SERIAL_PORT_PROGRAM_NODE_NUMBER_SEANET 255
#define mtAlive 4
#define mtReboot 16
#define mtSendVersion 23
#define mtVersionData 1
#define mtSendBBUser 24
#define mtBBUserData 6
#define mtFpgaCalibrationData 63
#define mtFpgaVersionData 57
#define mtHeadCommand 19
#define mtSendData 25
#define mtHeadData 2
#define mtAuxData 8
#define RESOLUTION2STEP_ANGLE_SIZE_IN_DEGREES(res) ((res)*0.05625)
#define STEP_ANGLE_SIZE_IN_DEGREES2RESOLUTION(StepAngleSize) ((int)((StepAngleSize)/0.05625))
#define RESOLUTION2NUMBER_OF_STEPS(res) ((int)(360.0/RESOLUTION2STEP_ANGLE_SIZE_IN_DEGREES(res)))
#define NUMBER_OF_STEPS2RESOLUTION(NSteps) ((int)(STEP_ANGLE_SIZE_IN_DEGREES2RESOLUTION(360.0/(NSteps))))
#ifndef STEP_ANGLE_SIZE_IN_DEGREES2NUMBER_OF_STEPS
#define STEP_ANGLE_SIZE_IN_DEGREES2NUMBER_OF_STEPS(StepAngleSize) ((int)(360.0/(StepAngleSize)))
#endif // !STEP_ANGLE_SIZE_IN_DEGREES2NUMBER_OF_STEPS
#ifndef NUMBER_OF_STEPS2STEP_ANGLE_SIZE_IN_DEGREES
#define NUMBER_OF_STEPS2STEP_ANGLE_SIZE_IN_DEGREES(NSteps) (360.0/(NSteps))
#endif // !NUMBER_OF_STEPS2STEP_ANGLE_SIZE_IN_DEGREES
// Transducer Bearing is the position of the transducer for the current scanline (0..6399 in 1/16 Gradian units),
// ahead corresponds to 3200. 10 gradians = 9 degrees. 0.05625 = (1/16)*(9/10).
// Angle of the transducer in degrees (in [0;360[ deg).
#define BEARING2ANGLE_IN_DEGREES(bearing) (((((int)(bearing))%6400-3200+6400)%6400)*0.05625)
#define ANGLE_IN_DEGREES2BEARING(angle) ((((int)((angle)/0.05625))%6400+3200+6400)%6400)
union usShort_SEANET
{
short v;
unsigned char c[2];
};
typedef union usShort_SEANET usShort_SEANET;
union uShort_SEANET
{
unsigned short v;
unsigned char c[2];
};
typedef union uShort_SEANET uShort_SEANET;
union usInt_SEANET
{
int v;
unsigned char c[4];
};
typedef union usInt_SEANET usInt_SEANET;
union uInt_SEANET
{
unsigned int v;
unsigned char c[4];
};
typedef union uInt_SEANET uInt_SEANET;
union uFloat_SEANET
{
float v;
unsigned char c[4];
};
typedef union uFloat_SEANET uFloat_SEANET;
struct HDCTRLStruct
{
unsigned int adc8on : 1;
unsigned int cont : 1;
unsigned int scanright : 1;
unsigned int invert : 1;
unsigned int motoff : 1;
unsigned int txoff : 1;
unsigned int spare : 1;
unsigned int chan2 : 1;
unsigned int raw : 1;
unsigned int hasmot : 1;
unsigned int applyoffset : 1;
unsigned int pingpong : 1;
unsigned int stareLLim : 1;
unsigned int ReplyASL : 1;
unsigned int ReplyThr : 1;
unsigned int IgnoreSensor : 1;
unsigned int padding : 16;
};
union HDCTRL
{
struct HDCTRLStruct bits;
unsigned char uc[4];
char c[4];
unsigned int u;
int i;
};
typedef union HDCTRL HDCTRL;
//struct SEANETDATA
//{
//
// unsigned char HeadInf;
//};
//typedef struct SEANETDATA SEANETDATA;
#pragma endregion
struct SEANET
{
RS232PORT RS232Port;
BOOL bDST;
BOOL bHalfDuplex;
//BOOL bSingleChannel;
HDCTRL HdCtrl;
int LeftAngleLimit; // In [0..359] deg, computed from ScanDirection and ScanWidth.
int RightAngleLimit; // In [0..359] deg, computed from ScanDirection and ScanWidth.
int LeftLim; // In 1/16 Grad, computed from LeftAngleLimit.
int RightLim; // In 1/16 Grad, computed from RightAngleLimit.
int ADLow; // Computed from Sensitivity.
int ADSpan; // Computed from Contrast.
int IGain; // Initial Gain of the receiver (in units 0..210 = 0..+80dB = 0..100%), computed from Gain.
int Slope; // Slope setting for each channel in 1/255 units.
int Resolution; // Scanning motor step angle between pings in units of 1/16 Grad, computed from StepAngleSize.
int NSteps; // Computed from StepAngleSize.
int HeadStatus;
HDCTRL HeadHdCtrl; // Current value returned by the sonar.
int HeadRangescale; // Current value returned by the sonar.
int HeadIGain; // Current value returned by the sonar.
int HeadSlope; // Current value returned by the sonar.
int HeadADLow; // Current value returned by the sonar.
int HeadADSpan; // Current value returned by the sonar.
int HeadADInterval; // Current value returned by the sonar.
int HeadLeftLim; // Current value returned by the sonar.
int HeadRightLim; // Current value returned by the sonar.
int HeadSteps; // Current value returned by the sonar.
int HeadNBins; // Current value returned by the sonar.
int Dbytes;
int HeadInf;
FILE* pfSaveFile; // Used to save raw data, should be handled specifically...
//SEANETDATA LastSeanetData;
//LastScanLine, LastAngle
char szCfgFilePath[256];
// Parameters.
char szDevPath[256];
int BaudRate;
int timeout;
int threadperiod;
BOOL bSaveRawData;
int RangeScale; // In m.
int Gain; // In %.
int Sensitivity; // In [0..80] dB.
int Contrast; // In [0..80] dB.
int ScanDirection; // In [0..359] deg.
int ScanWidth; // In [2..360] deg.
double StepAngleSize; // In deg, angle between 2 pings.
int NBins; // Corresponds to the resolution of a ping, max 800.
int adc8on; // BOOL.
int scanright; // BOOL.
int invert; // BOOL.
int stareLLim; // BOOL.
int VelocityOfSound; // In m/s.
double alpha_max_err;
double d_max_err;
int HorizontalBeam;
int VerticalBeam;
};
typedef struct SEANET SEANET;
// msg must contain a valid message of msglen bytes.
inline void SetHexBinLengthSeanet(unsigned char* msg, int msglen)
{
uShort_SEANET BinLength;
char szHexLength[5];
BinLength.v = (unsigned short)(msglen-6);
msg[5] = BinLength.c[0];
msg[6] = BinLength.c[1];
memset(szHexLength, 0, sizeof(szHexLength));
sprintf(szHexLength, "%.04x", (int)BinLength.v);
memcpy(msg+1, szHexLength, 4);
}
// buf must contain a valid message without invalid data in the beginning.
inline int GetHexLengthSeanet(unsigned char* buf)
{
char szHexLength[5];
int HexLength = 0;
memset(szHexLength, 0, sizeof(szHexLength));
memcpy(szHexLength, buf+1, 4);
if (sscanf(szHexLength, "%x", &HexLength) == 1) return HexLength; else return 0;
}
// buf must contain a valid message without invalid data in the beginning.
inline int GetBinLengthSeanet(unsigned char* buf)
{
uShort_SEANET BinLength;
BinLength.c[0] = buf[5];
BinLength.c[1] = buf[6];
return (int)BinLength.v;
}
// If this function succeeds, the beginning of buf contains a valid message
// header, hex and bin length but the message might be incomplete or there
// might be other data at the end.
inline int AnalyseBufHdrHexBinLenSeanet(unsigned char* buf, int buflen, int* pExpectedMsgLen)
{
int HexLength = 0;
if (buflen < 7)
{
//PRINT_DEBUG_MESSAGE_SEANETCORE(("Invalid number of bytes\n"));
return EXIT_FAILURE;
}
if (buf[0] != MESSAGE_HEADER_SEANET)
{
//PRINT_DEBUG_MESSAGE_SEANETCORE(("Invalid message header\n"));
return EXIT_FAILURE;
}
HexLength = GetHexLengthSeanet(buf);
if (HexLength != GetBinLengthSeanet(buf))
{
//PRINT_DEBUG_MESSAGE_SEANETCORE(("Invalid Hex or Bin Length\n"));
return EXIT_FAILURE;
}
*pExpectedMsgLen = HexLength+6;
return EXIT_SUCCESS;
}
// If this function succeeds, the beginning of buf contains a valid message
// header, hex length, bin length and MID but the message might be incomplete
// or there might be other data at the end.
inline int AnalyseBufHdrHexBinLenMIDSeanet(unsigned char* buf, int buflen, int mid, int* pExpectedMsgLen)
{
int HexLength = 0;
if (buflen < MIN_MESSAGE_LEN_SEANET)
{
//PRINT_DEBUG_MESSAGE_SEANETCORE(("Invalid number of bytes\n"));
return EXIT_FAILURE;
}
if (buf[0] != MESSAGE_HEADER_SEANET)
{
//PRINT_DEBUG_MESSAGE_SEANETCORE(("Invalid message header\n"));
return EXIT_FAILURE;
}
HexLength = GetHexLengthSeanet(buf);
if (HexLength != GetBinLengthSeanet(buf))
{
//PRINT_DEBUG_MESSAGE_SEANETCORE(("Invalid Hex or Bin Length\n"));
return EXIT_FAILURE;
}
if (buf[10] != (unsigned char)mid)
{
//PRINT_DEBUG_MESSAGE_SEANETCORE(("Invalid message ID\n"));
return EXIT_FAILURE;
}
*pExpectedMsgLen = HexLength+6;
return EXIT_SUCCESS;
}
// If this function succeeds, the beginning of buf contains a valid message
// but there might be other data at the end.
inline int AnalyseBufSeanet(unsigned char* buf, int buflen)
{
int HexLength = 0;
if (buflen < MIN_MESSAGE_LEN_SEANET)
{
//PRINT_DEBUG_MESSAGE_SEANETCORE(("Invalid number of bytes\n"));
return EXIT_FAILURE;
}
if (buf[0] != MESSAGE_HEADER_SEANET)
{
//PRINT_DEBUG_MESSAGE_SEANETCORE(("Invalid message header\n"));
return EXIT_FAILURE;
}
HexLength = GetHexLengthSeanet(buf);
if (HexLength != GetBinLengthSeanet(buf))
{
//PRINT_DEBUG_MESSAGE_SEANETCORE(("Invalid Hex or Bin Length\n"));
return EXIT_FAILURE;
}
if (buflen < HexLength+6)
{
//PRINT_DEBUG_MESSAGE_SEANETCORE(("Incomplete message\n"));
return EXIT_FAILURE;
}
if (buf[HexLength+5] != MESSAGE_TERMINATOR_SEANET)
{
//PRINT_DEBUG_MESSAGE_SEANETCORE(("Invalid message terminator\n"));
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}
// If this function succeeds, the beginning of buf contains a valid message
// but there might be other data at the end.
inline int AnalyseBufWithMIDSeanet(unsigned char* buf, int buflen, int mid)
{
int HexLength = 0;
if (buflen < MIN_MESSAGE_LEN_SEANET)
{
//PRINT_DEBUG_MESSAGE_SEANETCORE(("Invalid number of bytes\n"));
return EXIT_FAILURE;
}
if (buf[0] != MESSAGE_HEADER_SEANET)
{
//PRINT_DEBUG_MESSAGE_SEANETCORE(("Invalid message header\n"));
return EXIT_FAILURE;
}
HexLength = GetHexLengthSeanet(buf);
if (HexLength != GetBinLengthSeanet(buf))
{
//PRINT_DEBUG_MESSAGE_SEANETCORE(("Invalid Hex or Bin Length\n"));
return EXIT_FAILURE;
}
if (buf[10] != (unsigned char)mid)
{
//PRINT_DEBUG_MESSAGE_SEANETCORE(("Invalid message ID\n"));
return EXIT_FAILURE;
}
if (buflen < HexLength+6)
{
//PRINT_DEBUG_MESSAGE_SEANETCORE(("Incomplete message\n"));
return EXIT_FAILURE;
}
if (buf[HexLength+5] != MESSAGE_TERMINATOR_SEANET)
{
//PRINT_DEBUG_MESSAGE_SEANETCORE(("Invalid message terminator\n"));
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}
// If this function succeeds, the beginning of *pFoundMsg should contain a valid message
// but there might be other data at the end. Data in the beginning of buf might have been discarded.
inline int FindMsgSeanet(unsigned char* buf, int buflen, unsigned char** pFoundMsg, int* pFoundMsgTmpLen)
{
*pFoundMsg = buf;
*pFoundMsgTmpLen = buflen;
while (AnalyseBufSeanet(*pFoundMsg, *pFoundMsgTmpLen) != EXIT_SUCCESS)
{
(*pFoundMsg)++;
(*pFoundMsgTmpLen)--;
if (*pFoundMsgTmpLen < MIN_MESSAGE_LEN_SEANET)
{
*pFoundMsg = NULL;
*pFoundMsgTmpLen = 0;
return EXIT_FAILURE;
}
}
return EXIT_SUCCESS;
}
// If this function succeeds, the beginning of *pFoundMsg should contain a valid message
// but there might be other data at the end. Data in the beginning of buf might have been discarded.
inline int FindMsgWithMIDSeanet(unsigned char* buf, int buflen, int mid, unsigned char** pFoundMsg, int* pFoundMsgTmpLen)
{
*pFoundMsg = buf;
*pFoundMsgTmpLen = buflen;
while (AnalyseBufWithMIDSeanet(*pFoundMsg, *pFoundMsgTmpLen, mid) != EXIT_SUCCESS)
{
(*pFoundMsg)++;
(*pFoundMsgTmpLen)--;
if (*pFoundMsgTmpLen < MIN_MESSAGE_LEN_SEANET)
{
*pFoundMsg = NULL;
*pFoundMsgTmpLen = 0;
return EXIT_FAILURE;
}
}
return EXIT_SUCCESS;
}
// If this function succeeds, the beginning of *pFoundMsg should contain the latest valid message
// but there might be other data at the end. Data in the beginning of buf might have been discarded,
// including valid messages.
inline int FindLatestMsg0Seanet(unsigned char* buf, int buflen, unsigned char** pFoundMsg, int* pFoundMsgTmpLen)
{
unsigned char* ptr = NULL;
int len = 0;
int msglen = 0;
if (FindMsgSeanet(buf, buflen, &ptr, &len) != EXIT_SUCCESS)
{
*pFoundMsg = NULL;
*pFoundMsgTmpLen = 0;
return EXIT_FAILURE;
}
for (;;)
{
// Save the position of the beginning of the message.
*pFoundMsg = ptr;
*pFoundMsgTmpLen = len;
// Expected total message length.
msglen = GetBinLengthSeanet(*pFoundMsg)+6;
// Search just after the message.
if (FindMsgSeanet(*pFoundMsg+msglen, *pFoundMsgTmpLen-msglen,
&ptr, &len) != EXIT_SUCCESS)
{
break;
}
}
return EXIT_SUCCESS;
}
// If this function succeeds, the beginning of *pFoundMsg should contain the latest valid message
// but there might be other data at the end. Data in the beginning of buf might have been discarded,
// including valid messages.
inline int FindLatestMsgWithMID0Seanet(unsigned char* buf, int buflen, int mid, unsigned char** pFoundMsg, int* pFoundMsgTmpLen)
{
unsigned char* ptr = NULL;
int len = 0;
int msglen = 0;
if (FindMsgWithMIDSeanet(buf, buflen, mid, &ptr, &len) != EXIT_SUCCESS)
{
*pFoundMsg = NULL;
*pFoundMsgTmpLen = 0;
return EXIT_FAILURE;
}
for (;;)
{
// Save the position of the beginning of the message.
*pFoundMsg = ptr;
*pFoundMsgTmpLen = len;
// Expected total message length.
msglen = GetBinLengthSeanet(*pFoundMsg)+6;
// Search just after the message.
if (FindMsgWithMIDSeanet(*pFoundMsg+msglen, *pFoundMsgTmpLen-msglen,
mid, &ptr, &len) != EXIT_SUCCESS)
{
break;
}
}
return EXIT_SUCCESS;
}
// If this function succeeds, *pFoundMsg should contain the latest valid message
// (of *pFoundMsgLen bytes).
// Data in the beginning of buf (*pFoundMsg-buf bytes starting at buf address), including valid
// messages might have been discarded.
// Other data at the end of buf (*pRemainingDataLen bytes, that should not contain any valid message)
// might be available in *pRemainingData.
inline int FindLatestMsgSeanet(unsigned char* buf, int buflen,
unsigned char** pFoundMsg, int* pFoundMsgLen,
unsigned char** pRemainingData, int* pRemainingDataLen)
{
unsigned char* ptr = NULL;
int len = 0;
if (FindMsgSeanet(buf, buflen, &ptr, &len) != EXIT_SUCCESS)
{
*pFoundMsg = NULL;
*pFoundMsgLen = 0;
*pRemainingData = buf;
*pRemainingDataLen = buflen;
return EXIT_FAILURE;
}
for (;;)
{
// Save the position of the beginning of the message.
*pFoundMsg = ptr;
// Expected total message length.
*pFoundMsgLen = GetBinLengthSeanet(*pFoundMsg)+6;
*pRemainingData = *pFoundMsg+*pFoundMsgLen;
*pRemainingDataLen = len-*pFoundMsgLen;
// Search just after the message.
if (FindMsgSeanet(*pRemainingData, *pRemainingDataLen,
&ptr, &len) != EXIT_SUCCESS)
{
break;
}
}
return EXIT_SUCCESS;
}
// If this function succeeds, *pFoundMsg should contain the latest valid message
// (of *pFoundMsgLen bytes).
// Data in the beginning of buf (*pFoundMsg-buf bytes starting at buf address), including valid
// messages might have been discarded.
// Other data at the end of buf (*pRemainingDataLen bytes, that should not contain any valid message)
// might be available in *pRemainingData.
inline int FindLatestMsgWithMIDSeanet(unsigned char* buf, int buflen, int mid,
unsigned char** pFoundMsg, int* pFoundMsgLen,
unsigned char** pRemainingData, int* pRemainingDataLen)
{
unsigned char* ptr = NULL;
int len = 0;
if (FindMsgWithMIDSeanet(buf, buflen, mid, &ptr, &len) != EXIT_SUCCESS)
{
*pFoundMsg = NULL;
*pFoundMsgLen = 0;
*pRemainingData = buf;
*pRemainingDataLen = buflen;
return EXIT_FAILURE;
}
for (;;)
{
// Save the position of the beginning of the message.
*pFoundMsg = ptr;
// Expected total message length.
*pFoundMsgLen = GetBinLengthSeanet(*pFoundMsg)+6;
*pRemainingData = *pFoundMsg+*pFoundMsgLen;
*pRemainingDataLen = len-*pFoundMsgLen;
// Search just after the message.
if (FindMsgWithMIDSeanet(*pRemainingData, *pRemainingDataLen,
mid, &ptr, &len) != EXIT_SUCCESS)
{
break;
}
}
return EXIT_SUCCESS;
}
//// Should be able to use previously remaining data and output any new remaining data...
//inline int GetLatestMsgWithRemainingSeanet(SEANET* pSeanet, int mid,
// unsigned char* databuf, int databuflen, int* pNbdatabytes,
// unsigned char** pRemainingData, int* pRemainingDataLen);
// We suppose that read operations return when a message has just been completely sent, and not randomly.
inline int GetLatestMsgSeanet(SEANET* pSeanet, int mid, unsigned char* databuf, int databuflen, int* pNbdatabytes)
{
unsigned char recvbuf[2*MAX_NB_BYTES_SEANET];
unsigned char savebuf[MAX_NB_BYTES_SEANET];
int BytesReceived = 0, Bytes = 0, recvbuflen = 0;
unsigned char* ptr = NULL;
int len = 0;
unsigned char* remainingdata = NULL;
int remainingdatalen = 0;
CHRONO chrono;
StartChrono(&chrono);
// Prepare the buffers.
memset(recvbuf, 0, sizeof(recvbuf));
memset(savebuf, 0, sizeof(savebuf));
recvbuflen = MAX_NB_BYTES_SEANET-1; // The last character must be a 0 to be a valid string for sscanf.
BytesReceived = 0;
if (ReadRS232Port(&pSeanet->RS232Port, (unsigned char*)recvbuf, recvbuflen, &Bytes) != EXIT_SUCCESS)
{
printf("Error reading data from a Seanet. \n");
return EXIT_FAILURE;
}
BytesReceived += Bytes;
if (BytesReceived >= recvbuflen)
{
// If the buffer is full and if the device always sends data, there might be old data to discard...
while (Bytes == recvbuflen)
{
if (GetTimeElapsedChronoQuick(&chrono) > TIMEOUT_MESSAGE_SEANET)
{
printf("Error reading data from a Seanet : Message timeout. \n");
return EXIT_TIMEOUT;
}
memcpy(savebuf, recvbuf, Bytes);
if (ReadRS232Port(&pSeanet->RS232Port, (unsigned char*)recvbuf, recvbuflen, &Bytes) != EXIT_SUCCESS)
{
printf("Error reading data from a Seanet. \n");
return EXIT_FAILURE;
}
BytesReceived += Bytes;
}
// The desired message should be among all the data gathered, unless there was
// so many other messages sent after that the desired message was in the
// discarded data, or we did not wait enough...
memmove(recvbuf+recvbuflen-Bytes, recvbuf, Bytes);
memcpy(recvbuf, savebuf+Bytes, recvbuflen-Bytes);
// Only the last recvbuflen bytes received should be taken into account in what follows.
BytesReceived = recvbuflen;
}
// The data need to be analyzed and we must check if we need to get more data from
// the device to get the desired message.
// But normally we should not have to get more data unless we did not wait enough
// for the desired message...
while (FindLatestMsgWithMIDSeanet(recvbuf, BytesReceived, mid, &ptr, &len, &remainingdata, &remainingdatalen) != EXIT_SUCCESS)
{
if (GetTimeElapsedChronoQuick(&chrono) > TIMEOUT_MESSAGE_SEANET)
{
printf("Error reading data from a Seanet : Message timeout. \n");
return EXIT_TIMEOUT;
}
// The last character must be a 0 to be a valid string for sscanf.
if (BytesReceived >= 2*MAX_NB_BYTES_SEANET-1)
{
printf("Error reading data from a Seanet : Invalid data. \n");
return EXIT_INVALID_DATA;
}
if (ReadRS232Port(&pSeanet->RS232Port, (unsigned char*)recvbuf+BytesReceived, 2*MAX_NB_BYTES_SEANET-1-BytesReceived, &Bytes) != EXIT_SUCCESS)
{
printf("Error reading data from a Seanet. \n");
return EXIT_FAILURE;
}
BytesReceived += Bytes;
}
if (remainingdatalen > 0)
{
printf("Error getting data from a Seanet : Unexpected data after a message. \n");
}
// Get data bytes.
memset(databuf, 0, databuflen);
*pNbdatabytes = len;
// Check the number of data bytes before copy.
if (databuflen < *pNbdatabytes)
{
printf("Error getting data from a Seanet : Too small data buffer. \n");
return EXIT_FAILURE;
}
// Copy the data bytes of the message.
if (*pNbdatabytes > 0)
{
memcpy(databuf, ptr, *pNbdatabytes);
}
return EXIT_SUCCESS;
}
// Note : flush does not exist in TCP mode...
// Possible improvement : mix this function with the other version that discards a lot of data...
// But is it really possible?
//
// GetMsgSeanet() partout sauf pour alive (surtout le 1er)? Pas forcément, seulement quand il y a risque
// qu'un autre message important suive celui qu'on est en train de recevoir...
//
inline int GetMsgSeanet(SEANET* pSeanet, int mid, unsigned char* databuf, int databuflen, int* pNbdatabytes)
{
unsigned char recvbuf[MAX_NB_BYTES_SEANET];
int i = 0;
int BytesReceived = 0, recvbuflen = 0;
unsigned char* ptr = NULL;
int len = 0;
unsigned char* remainingdata = NULL;
int remainingdatalen = 0;
CHRONO chrono;
StartChrono(&chrono);
// Prepare the buffers.
memset(recvbuf, 0, sizeof(recvbuf));
recvbuflen = MAX_NB_BYTES_SEANET-1; // The last character must be a 0 to be a valid string for sscanf.
BytesReceived = 0;
// Suppose that there are not so many data to discard.
// First try to get directly the desired message...
if (ReadAllRS232Port(&pSeanet->RS232Port, recvbuf, MIN_MESSAGE_LEN_SEANET) != EXIT_SUCCESS)
{
printf("Error reading data from a Seanet. \n");
return EXIT_FAILURE;
}
BytesReceived += MIN_MESSAGE_LEN_SEANET;
i = 0;
while (AnalyseBufHdrHexBinLenMIDSeanet(recvbuf+i, BytesReceived-i, mid, &len) != EXIT_SUCCESS)
{
if (GetTimeElapsedChronoQuick(&chrono) > TIMEOUT_MESSAGE_SEANET)
{
printf("Error reading data from a Seanet : Message timeout. \n");
return EXIT_TIMEOUT;
}
if (BytesReceived+1 > recvbuflen)
{
printf("Error reading data from a Seanet : Invalid data. \n");
return EXIT_INVALID_DATA;
}
if (ReadAllRS232Port(&pSeanet->RS232Port, recvbuf+BytesReceived, 1) != EXIT_SUCCESS)
{
printf("Error reading data from a Seanet. \n");
return EXIT_FAILURE;
}
BytesReceived += 1;
i++;
}
// Notes :
// BytesReceived+len-(BytesReceived-i) = len+i
// BytesReceived = MIN_MESSAGE_LEN_SEANET+i
// len-MIN_MESSAGE_LEN_SEANET = len-(BytesReceived-i)
if (len+i > recvbuflen)
{
printf("Error reading data from a Seanet : Invalid data. \n");
return EXIT_INVALID_DATA;
}
// Read the remaining bytes of the message according to the length found.
if (len-(BytesReceived-i) > 0)
{
if (ReadAllRS232Port(&pSeanet->RS232Port, recvbuf+BytesReceived, len-(BytesReceived-i)) != EXIT_SUCCESS)
{
printf("Error reading data from a Seanet. \n");
return EXIT_FAILURE;
}
BytesReceived += (len-(BytesReceived-i));
}
if (FindLatestMsgWithMIDSeanet(recvbuf, BytesReceived, mid,
&ptr, &len, &remainingdata, &remainingdatalen) != EXIT_SUCCESS)
{
printf("Error reading data from a Seanet : Invalid data. \n");
return EXIT_INVALID_DATA;
}
if (remainingdatalen > 0)
{
printf("Error getting data from a Seanet : Unexpected data after a message. \n");
}
// Get data bytes.
memset(databuf, 0, databuflen);
*pNbdatabytes = len;
// Check the number of data bytes before copy.
if (databuflen < *pNbdatabytes)
{
printf("Error getting data from a Seanet : Too small data buffer. \n");
return EXIT_FAILURE;
}
// Copy the data bytes of the message.
if (*pNbdatabytes > 0)
{
memcpy(databuf, ptr, *pNbdatabytes);
}
return EXIT_SUCCESS;
}
// Same as GetLatestMsgSeanet() but do not look for a specific MID and return the mid found.
inline int GetLatestMsgWithoutMIDSeanet(SEANET* pSeanet, int* pMid, unsigned char* databuf, int databuflen, int* pNbdatabytes)
{
unsigned char recvbuf[2*MAX_NB_BYTES_SEANET];
unsigned char savebuf[MAX_NB_BYTES_SEANET];
int BytesReceived = 0, Bytes = 0, recvbuflen = 0;
unsigned char* ptr = NULL;
int len = 0;
unsigned char* remainingdata = NULL;
int remainingdatalen = 0;
CHRONO chrono;
StartChrono(&chrono);
// Prepare the buffers.
memset(recvbuf, 0, sizeof(recvbuf));
memset(savebuf, 0, sizeof(savebuf));
recvbuflen = MAX_NB_BYTES_SEANET-1; // The last character must be a 0 to be a valid string for sscanf.
BytesReceived = 0;
if (ReadRS232Port(&pSeanet->RS232Port, (unsigned char*)recvbuf, recvbuflen, &Bytes) != EXIT_SUCCESS)
{
printf("Error reading data from a Seanet. \n");
return EXIT_FAILURE;
}
BytesReceived += Bytes;
if (BytesReceived >= recvbuflen)
{
// If the buffer is full and if the device always sends data, there might be old data to discard...
while (Bytes == recvbuflen)
{
if (GetTimeElapsedChronoQuick(&chrono) > TIMEOUT_MESSAGE_SEANET)
{
printf("Error reading data from a Seanet : Message timeout. \n");
return EXIT_TIMEOUT;
}
memcpy(savebuf, recvbuf, Bytes);
if (ReadRS232Port(&pSeanet->RS232Port, (unsigned char*)recvbuf, recvbuflen, &Bytes) != EXIT_SUCCESS)
{
printf("Error reading data from a Seanet. \n");
return EXIT_FAILURE;
}
BytesReceived += Bytes;
}
// The desired message should be among all the data gathered, unless there was
// so many other messages sent after that the desired message was in the
// discarded data, or we did not wait enough...
memmove(recvbuf+recvbuflen-Bytes, recvbuf, Bytes);
memcpy(recvbuf, savebuf+Bytes, recvbuflen-Bytes);
// Only the last recvbuflen bytes received should be taken into account in what follows.
BytesReceived = recvbuflen;
}
// The data need to be analyzed and we must check if we need to get more data from
// the device to get the desired message.
// But normally we should not have to get more data unless we did not wait enough
// for the desired message...
while (FindLatestMsgSeanet(recvbuf, BytesReceived, &ptr, &len, &remainingdata, &remainingdatalen) != EXIT_SUCCESS)
{
if (GetTimeElapsedChronoQuick(&chrono) > TIMEOUT_MESSAGE_SEANET)
{
printf("Error reading data from a Seanet : Message timeout. \n");
return EXIT_TIMEOUT;
}
// The last character must be a 0 to be a valid string for sscanf.
if (BytesReceived >= 2*MAX_NB_BYTES_SEANET-1)
{
printf("Error reading data from a Seanet : Invalid data. \n");
return EXIT_INVALID_DATA;
}
if (ReadRS232Port(&pSeanet->RS232Port, (unsigned char*)recvbuf+BytesReceived, 2*MAX_NB_BYTES_SEANET-1-BytesReceived, &Bytes) != EXIT_SUCCESS)
{
printf("Error reading data from a Seanet. \n");
return EXIT_FAILURE;
}
BytesReceived += Bytes;
}
if (remainingdatalen > 0)
{
printf("Error getting data from a Seanet : Unexpected data after a message. \n");
}
// Get data bytes.
memset(databuf, 0, databuflen);
*pNbdatabytes = len;
// Check the number of data bytes before copy.
if (databuflen < *pNbdatabytes)
{
printf("Error getting data from a Seanet : Too small data buffer. \n");
return EXIT_FAILURE;
}
// Copy the data bytes of the message.
if (*pNbdatabytes > 0)
{
memcpy(databuf, ptr, *pNbdatabytes);
}
*pMid = databuf[10];
return EXIT_SUCCESS;
}
// Same as GetMsgSeanet() but do not look for a specific MID and return the mid found.
inline int GetMsgWithoutMIDSeanet(SEANET* pSeanet, int* pMid, unsigned char* databuf, int databuflen, int* pNbdatabytes)
{
unsigned char recvbuf[MAX_NB_BYTES_SEANET];
int i = 0;
int BytesReceived = 0, recvbuflen = 0;
unsigned char* ptr = NULL;
int len = 0;
unsigned char* remainingdata = NULL;
int remainingdatalen = 0;
CHRONO chrono;
StartChrono(&chrono);
// Prepare the buffers.
memset(recvbuf, 0, sizeof(recvbuf));
recvbuflen = MAX_NB_BYTES_SEANET-1; // The last character must be a 0 to be a valid string for sscanf.
BytesReceived = 0;
// Suppose that there are not so many data to discard.
// First try to get directly the desired message...
if (ReadAllRS232Port(&pSeanet->RS232Port, recvbuf, MIN_MESSAGE_LEN_SEANET) != EXIT_SUCCESS)
{
printf("Error reading data from a Seanet. \n");
return EXIT_FAILURE;
}
BytesReceived += MIN_MESSAGE_LEN_SEANET;
i = 0;
while (AnalyseBufHdrHexBinLenSeanet(recvbuf+i, BytesReceived-i, &len) != EXIT_SUCCESS)
{
if (GetTimeElapsedChronoQuick(&chrono) > TIMEOUT_MESSAGE_SEANET)
{
printf("Error reading data from a Seanet : Message timeout. \n");
return EXIT_TIMEOUT;
}
if (BytesReceived+1 > recvbuflen)
{
printf("Error reading data from a Seanet : Invalid data. \n");
return EXIT_INVALID_DATA;
}
if (ReadAllRS232Port(&pSeanet->RS232Port, recvbuf+BytesReceived, 1) != EXIT_SUCCESS)
{
printf("Error reading data from a Seanet. \n");
return EXIT_FAILURE;
}
BytesReceived += 1;
i++;
}
// Notes :
// BytesReceived+len-(BytesReceived-i) = len+i
// BytesReceived = MIN_MESSAGE_LEN_SEANET+i
// len-MIN_MESSAGE_LEN_SEANET = len-(BytesReceived-i)
if (len+i > recvbuflen)
{
printf("Error reading data from a Seanet : Invalid data. \n");
return EXIT_INVALID_DATA;
}
// Read the remaining bytes of the message according to the length found.
if (len-(BytesReceived-i) > 0)
{
if (ReadAllRS232Port(&pSeanet->RS232Port, recvbuf+BytesReceived, len-(BytesReceived-i)) != EXIT_SUCCESS)
{
printf("Error reading data from a Seanet. \n");
return EXIT_FAILURE;
}
BytesReceived += (len-(BytesReceived-i));
}
if (FindLatestMsgSeanet(recvbuf, BytesReceived,
&ptr, &len, &remainingdata, &remainingdatalen) != EXIT_SUCCESS)
{
printf("Error reading data from a Seanet : Invalid data. \n");
return EXIT_INVALID_DATA;
}
if (remainingdatalen > 0)
{
printf("Error getting data from a Seanet : Unexpected data after a message. \n");
}