-
Notifications
You must be signed in to change notification settings - Fork 0
/
soarCOL.c
1983 lines (1776 loc) · 55.4 KB
/
soarCOL.c
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
// tab size: 4
/**
* \file soarCOL.c
* \brief SoarPilot Colibri support
*/
#include <PalmOS.h> // all the system toolbox headers
#include "soarCOL.h"
#include "soarComp.h"
#include "soaring.h"
#include "soarIO.h"
#include "soarUtil.h"
#include "soarDB.h"
#include "soarPLst.h"
#include "soarForm.h"
#include "soarUMap.h"
#include "soarMath.h"
#include "soarCAI.h"
#include "soarWay.h"
// global variables from the main SoarPilot program
extern Char c36[37]; ///< FAI base36 format - from soarVolk.c
extern Boolean BinaryFileXfr; ///< the format of the requested log download, false - IGC-format, true - LXN-format
extern Int8 CompCmdRes; ///< from soarComp.c to pass back results
// global variables from CAI routines
extern CAILogData *cailogdata; ///< CAI logger data
extern Int16 cainumLogs; ///< CAI number of logs
extern Int16 selectedCAIFltIndex;///< CAI selected flight index
// Global variables
Char CRC; ///< CRC checksum
// Global variables for the flight-log download
UInt32 StartPos = 0, ///< start postion of one flight log in the memory of the logger
EndPos = 0; ///< end postion of one flight log in the memory of the logger
UInt32 OrigStartPos = 0; ///< original starting position of the log, because StartPos gets incremented
UInt32 CurrentPos = 0; ///< current position in the binary data when generating IGC
UInt16 RemainInBlock; ///< remaining bytes in the current block
UInt16 PosInBlock; ///< current position in the downloaded block
MemInfo *MeminfoP; ///< memory info downloaded from the logger
UInt8 *LogBuffer; ///< Logger buffer
//Boolean LXold = false; // flag to show if filser sentences to be used for older LX devide (LX5000)
/// Competition class strings
Char *Competition_Class[] =
{
"STANDARD",
"15-METER",
"OPEN",
"18-METER",
"WORLD",
"DOUBLE",
"MOTOR_GL",""
};
/// GPS datum strings
Char *Table_GPSdatum[] =
{
"ADINDAN ",
"AFGOOYE ",
"AIN EL ABD 1970",
"COCOS ISLAND ",
"ARC 1950 ",
"ARC 1960 ",
"ASCENSION 1958 ",
"ASTRO BEACON E ",
"AUSTRALIAN 1966",
"AUSTRALIAN 1984",
"ASTRO DOS 7/14 ",
"MARCUS ISLAND ",
"TERN ISLAND ",
"BELLEVUE (IGN) ",
"BERMUDA 1957 ",
"COLOMBIA ",
"CAMPO INCHAUSPE",
"CANTON ASTRO ",
"CAPE CANAVERAL ",
"CAPE (AFRICA) ",
"CARTHAGE ",
"CHATHAM 1971 ",
"CHUA ASTRO ",
"CORREGO ALEGRE ",
"DJAKARTA ",
"DOS 1968 ",
"EASTER ISLAND ",
"EUROPEAN 1950 ",
"EUROPEAN 1979 ",
"FINLAND 1910 ",
"GANDAJIKA BASE ",
"NEW ZEALAND '49",
"OSGB 1936 ",
"GUAM 1963 ",
"GUX 1 ASTRO ",
"HJOESEY 1955 ",
"HONG KONG 1962 ",
"INDIAN/NEPAL ",
"INDIAN/VIETNAM ",
"IRELAND 1965 ",
"DIEGO GARCIA ",
"JOHNSTON 1961 ",
"KANDAWALA ",
"KERGUELEN ISL. ",
"KERTAU 1948 ",
"CAYMAN BRAC ",
"LIBERIA 1964 ",
"LUZON/MINDANAO ",
"LUZON PHILIPPI.",
"MAHE 1971 ",
"MARCO ASTRO ",
"MASSAWA ",
"MERCHICH ",
"MIDWAY ASTRO'61",
"MINNA (NIGERIA)",
"NAD-1927 ALASKA",
"NAD-1927 BAHAM.",
"NAD-1927 CENTR.",
"NAD-1927 CANAL ",
"NAD-1927 CANADA",
"NAD-1927 CARIB.",
"NAD-1927 CONUS ",
"NAD-1927 CUBA ",
"NAD-1927 GREEN.",
"NAD-1927 MEXICO",
"NAD-1927 SALVA.",
"NAD-1983 ",
"NAPARIMA ",
"MASIRAH ISLAND ",
"SAUDI ARABIA ",
"ARAB EMIRATES ",
"OBSERVATORIO'66",
"OLD EGYIPTIAN ",
"OLD HAWAIIAN ",
"OMAN ",
"CANARY ISLAND ",
"PICAIRN 1967 ",
"PUERTO RICO ",
"QATAR NATIONAL ",
"QORNOQ ",
"REUNION ",
"ROME 1940 ",
"RT-90 SWEDEN ",
"S.AMERICA 1956",
"S.AMERICA 1956",
"SOUTH ASIA ",
"CHILEAN 1963 ",
"SANTO(DOS) ",
"SAO BRAZ ",
"SAPPER HILL ",
"SCHWARZECK ",
"SOUTHEAST BASE ",
"FAIAL ",
"TIMBALI 1948 ",
"TOKYO ",
"TRISTAN ASTRO ",
"RESERVED ",
"VITI LEVU 1916 ",
"WAKE-ENIWETOK ",
"WGS-1972 ",
"WGS-1984 ",
"ZANDERIJ ",
"CH-1903 "
};
//**************************************************************************************
//**************************************************************************************
//**
//** Exported functions used by the main SoarPilot program
//**
//**************************************************************************************
//**************************************************************************************
/**
* \return true - successful declaration, false - unsuccessful declaration
* \brief Function for sending active task + IGC info to the Colibri, uses global variable 'data'
*/
Boolean COLDeclareTask()
{
DateTimeType date;
UInt32 time;
UInt16 numWaypts, turnPoints, i;
UInt8 from, to, numCtrl;
Boolean copyStart = false,
copyFinish = false,
result = false;
UInt8 *sendBuffer;
numWaypts = data.task.numwaypts;
turnPoints = numWaypts - 2; // start and finish points are not counted as turnpoints
if (data.task.hastakeoff)
turnPoints--; // takeoff point is not turnpoint
else
{
copyStart = true; // copy the start point as the takeoff point
numWaypts++;
}
if (data.task.haslanding)
turnPoints--; // landing point is not turnpoint
else
{
copyFinish = true; // copy the finish point as the landing point
numWaypts++;
}
sendBuffer = MemPtrNew(349);
if (!sendBuffer)
{
CompCmdRes = MEMORYERR;
return false;
}
turnPoints = turnPoints - data.task.numctlpts;
MemSet(sendBuffer, 349, 0); // initialize block to send
HandleWaitDialog(true);
// Start assembling of flight info block
// Add pilot name, max 18 chars, padded by spaces
StrNCopy(&sendBuffer[3], data.igchinfo.name, 18);
if (StrLen(data.igchinfo.name) < 18)
for (i = (3 + StrLen(data.igchinfo.name)); i < (3 + 18); i++) sendBuffer[i] = 0x20;
// Add glider type, max 11 chars, padded by spaces
StrNCopy(&sendBuffer[22], data.igchinfo.type, 11);
if (StrLen(data.igchinfo.type) < 11)
for (i = (22 + StrLen(data.igchinfo.type)); i < (22 + 11); i++) sendBuffer[i] = 0x20;
// Add registration number, max 7 chars, padded by spaces
StrNCopy(&sendBuffer[34], data.igchinfo.gid, 7);
if (StrLen(data.igchinfo.gid) < 7)
for (i = (34 + StrLen(data.igchinfo.gid)); i < (34 + 7); i++) sendBuffer[i] = 0x20;
// Add competition ID, max 3 chars, padded by spaces
StrNCopy(&sendBuffer[42], data.igchinfo.cid, 3);
if (StrLen(data.igchinfo.cid) < 3)
for (i = (42 + StrLen(data.igchinfo.cid)); i < (42 + 3); i++) sendBuffer[i] = 0x20;
// 48 razred ???
// Add observer name, max 9 chars, padded by spaces
StrNCopy(&sendBuffer[47], data.igchinfo.ooid, 9);
if (StrLen(data.igchinfo.ooid) < 9)
for (i = (47 + StrLen(data.igchinfo.ooid)); i < (47 + 9); i++) sendBuffer[i] = 0x20;
TimSecondsToDateTime(TimGetSeconds(), &date);
time = (UInt32) date.hour * 3600;
time += date.minute * 60;
time += date.second;
sendBuffer[121] = (time & 0x00FF0000) >> 16;
sendBuffer[122] = (time & 0x0000FF00) >> 8;
sendBuffer[123] = time & 0x000000FF;
sendBuffer[124] = date.day;
sendBuffer[125] = date.month;
sendBuffer[126] = date.year % 100; // last 2 digits
// 127-129 user defined year/month/day
// 130-131 taskID
sendBuffer[132] = turnPoints; // number of turning points
// 133-144 type of points
sendBuffer[133] = 3; // take-off point
sendBuffer[133 + numWaypts - 1 - data.task.numctlpts] = 2; // landing point
for (i = 134; i < (133 + numWaypts - 1 - data.task.numctlpts); i++) sendBuffer[i] = 1;
numCtrl = 0;
// 145-192 longitudes (4 bytes x 12)
// 193-240 latitudes (4 bytes x 12)
// 241-348 names (9 chars x 12)
for (i = 0; i < data.task.numwaypts; i++)
{
if (copyStart)
{
if (i == 0)
{
COLCopyWaypoint(sendBuffer, 0, 0); // copy start point into the take-off position as well
from = 0;
to = 1;
}
else
{
from = i;
to = i + 1 - numCtrl;
}
}
else
{
from = i;
to = i - numCtrl;
}
if (copyFinish)
{
if (i == (data.task.numwaypts - 1))
{
COLCopyWaypoint(sendBuffer, i, to); // copy last point into the landing position as well
from = i;
to++;
}
}
if ((data.task.waypttypes[from] & CONTROL) == 0)
{
COLCopyWaypoint(sendBuffer, from, to);
}
else
{
numCtrl++;
}
}
if (!COLSync())
{
HandleWaitDialog(false);
CompCmdRes = CONNECTERR;
MemPtrFree(sendBuffer);
return false;
}
serial_out(LX_STX); // initialization sequence for sending of task declaration
serial_out(LX_PCWRITE);
result = COLSendBlock(sendBuffer, 349); // send the declaration block to the Colibri
if (!result) // error during sending of block
{
HandleWaitDialog(false);
MemPtrFree(sendBuffer);
return false;
}
// send competition class
MemSet(sendBuffer, 10, 0); // initialize block to send
// Add competition class, max 8 chars
StrNCopy(sendBuffer, data.igchinfo.cls, 8); // needs to be from the list, not freetext, ethzsa
serial_out(LX_STX); // initialization sequence for sending of competition class
serial_out(LX_CCWRITE);
result = COLSendBlock(sendBuffer, 9); // send the competition class block to the Colibri
HandleWaitDialog(false);
MemPtrFree(sendBuffer);
return(result);
}
/**
* \param cmd command code, what to do
* \return true - successful flight headers download, false - unsuccessful download
* \brief Function for acquire Colibri flight headers. Uses global variables cailogdata and cainumLogs
*/
Boolean COLGetFlightList(Int16 cmd)
{
UInt16 numberOfFlights = 0;
UInt8 *rcvBuffer;
CAILogData *caitemp;
if (cmd == CAIFIFREE)
{
if (cailogdata) // free the previously allocated memory
{
MemPtrFree(cailogdata);
cailogdata = NULL;
}
cainumLogs = 0;
return true;
}
if (cmd == CAIFISTART) // download the list of flights stored in the logger
{
HandleWaitDialogUpdate(SHOWDIALOG, 0, 0, NULL);
if (COLSync())
{
rcvBuffer = MemPtrNew(95);
if (!rcvBuffer) // no free memory
{
HandleWaitDialogUpdate(STOPDIALOG, 0, 0, NULL);
cainumLogs = 0;
return false;
}
serial_out(LX_STX);
serial_out(LX_FLLIST); // order the logger to send flight headers
do
{
if (!COLReadBlock(rcvBuffer, 95, 30)) // wait for the flight header for 30s
{
MemPtrFree(rcvBuffer); // timeout or error occured, the flight headers cannot be used
if (cailogdata)
{
MemPtrFree(cailogdata);
cailogdata = NULL;
}
cainumLogs = 0;
HandleWaitDialogUpdate(STOPDIALOG, 0, 0, NULL);
return false;
}
if (rcvBuffer[0]) // flight header valid
{
if (!cailogdata)
{ // first flight header
cailogdata = MemPtrNew(sizeof(CAILogData));
if (!cailogdata) // no free memory to store the header
{
MemPtrFree(rcvBuffer);
cainumLogs = 0;
HandleWaitDialogUpdate(STOPDIALOG, 0, 0, NULL);
return false;
}
MemSet(cailogdata, sizeof(CAILogData), 0); // clear the storage space
}
else
{
caitemp = MemPtrNew(sizeof(CAILogData) * (numberOfFlights + 1)); // allocate memory for the new header
if (!caitemp) // no free memory to store the header
{
MemPtrFree(rcvBuffer);
MemPtrFree(cailogdata);
cailogdata = NULL;
cainumLogs = 0;
HandleWaitDialogUpdate(STOPDIALOG, 0, 0, NULL);
return false;
}
MemSet(caitemp, sizeof(CAILogData) * (numberOfFlights + 1), 0); // clear the storage space
MemMove(caitemp, cailogdata, sizeof(CAILogData) * numberOfFlights); // copy the collected headers to the new array
MemPtrFree(cailogdata); // free the old array
cailogdata = caitemp;
}
COLStoreFlightHeader(rcvBuffer, numberOfFlights); // store the flight data into the cailogdata array
numberOfFlights++;
HandleWaitDialogUpdate(UPDATEDIALOG, numberOfFlights, -1, "header");
}
} while (rcvBuffer[0]);
MemPtrFree(rcvBuffer);
cainumLogs = numberOfFlights;
HandleWaitDialogUpdate(STOPDIALOG, 0, 0, NULL);
return true;
}
else
{
// synching failed
HandleWaitDialogUpdate(STOPDIALOG, 0, 0, NULL);
cainumLogs = 0;
cailogdata = NULL;
return false;
}
}
return false;
}
/**
* \return true - successful flight download, false - unsuccessful download
* \brief Function for downloading one flight log.
* Uses external variable BinaryFileXfr to decide if download in LXN or IGC format
* Uses external variables cailogdata, cainumLogs, selectedCAIFltIndex
*/
Boolean COLDownloadFlight()
{
Boolean result;
Char fileName[13];
HandleWaitDialogUpdate(SHOWDIALOG, 0, 0, "%");
if (COLSync())
{
if (selectedCAIFltIndex < cainumLogs)
{
MemSet(fileName, 13, 0);
fileName[0] = c36[cailogdata[selectedCAIFltIndex].startDate.year % 10]; // assemble IGC-file name
fileName[1] = c36[cailogdata[selectedCAIFltIndex].startDate.month];
fileName[2] = c36[cailogdata[selectedCAIFltIndex].startDate.day];
if ((data.config.flightcomp == LXCOMP) || (data.config.flightcomp == B50LXCOMP))
// if (!LXold && ((data.config.flightcomp == LXCOMP) || (data.config.flightcomp == B50LXCOMP)))
fileName[3] = 'L'; // LX navigation ID
else
fileName[3] = 'F'; // Filser
// can be other manufacturer: Posigraph
// http://www.spsys.demon.co.uk/download.htm
fileName[4] = c36[cailogdata[selectedCAIFltIndex].SerialNo / 1296];
fileName[5] = c36[(cailogdata[selectedCAIFltIndex].SerialNo % 1296) / 36];
fileName[6] = c36[cailogdata[selectedCAIFltIndex].SerialNo % 36];
fileName[7] = (cailogdata[selectedCAIFltIndex].FlightOfDay < 36 ? c36[cailogdata[selectedCAIFltIndex].FlightOfDay] : '_');
if (BinaryFileXfr)
{
if ((data.config.flightcomp == LXCOMP) || (data.config.flightcomp == B50LXCOMP))
// if (!LXold && ((data.config.flightcomp == LXCOMP) || (data.config.flightcomp == B50LXCOMP)))
StrCat(fileName, ".LXN"); // LX navigation binary format
else
StrCat(fileName, ".FIL"); // Filser binary format
}
else
StrCat(fileName, ".IGC");
MeminfoP = MemPtrNew(sizeof(MemInfo));
if (COLGetMemInfo())
{
StartPos = cailogdata[selectedCAIFltIndex].StartTape + (32768 * cailogdata[selectedCAIFltIndex].StartPage);
EndPos = cailogdata[selectedCAIFltIndex].EndTape + (32768 * cailogdata[selectedCAIFltIndex].EndPage);
OrigStartPos = StartPos;
if (EndPos < StartPos)
EndPos += 32768 * (MeminfoP->endpage - MeminfoP->startpage + 1);
if (XferInit(fileName, IOOPENTRUNC, data.config.xfertype)) // open output file
{
if (BinaryFileXfr)
result = COLDownloadFlightLXN(); // make the binary download
else
result = COLDownloadFlightIGC(); // convert the downloaded data to IGC-format
XferClose(data.config.xfertype); // close output file
}
else result = false; // file open was unsuccessful
}
else result = false; // meminfo reception failed
MemPtrFree(MeminfoP);
}
else result = false; // invalid flight index (bigger than cainumLogs-1)
}
else result = false; // syncing with colibri unsuccessful
HandleWaitDialogUpdate(STOPDIALOG, 0, 0, "%");
return result;
}
//***********************************************************************************************
//***********************************************************************************************
//**
//** Internal functions
//**
//***********************************************************************************************
//***********************************************************************************************
/**
* \brief Function for copying waypoint into sendBuffer as it is required by the Colibri
* Uses global variable struct 'data'
* \param sendBuffer - declaration block under construction (will be sent to Colibri)
* \param from - index of waypoint in data.task struct, this should be copied into the declaration block to send
* \param to - index of waypoint in the declaration block
*/
void COLCopyWaypoint(Char *sendBuffer, UInt8 from, UInt8 to)
{
Int32 longitude, latitude;
StrNCopy(&sendBuffer[241 + (to * 9)], data.task.wayptnames[from], 8);
longitude = (Int32) Fabs(data.task.wayptlons[from] * 60000.0);
latitude = (Int32) Fabs(data.task.wayptlats[from] * 60000.0);
if (data.task.wayptlons[from] < 0) longitude = 1 - longitude; // west long.
if (data.task.wayptlats[from] < 0) latitude = 1 - latitude; // south lat.
sendBuffer[145 + (to * 4)] = (UInt8) ((longitude & 0xFF000000) >> 24);
sendBuffer[146 + (to * 4)] = (UInt8) ((longitude & 0x00FF0000) >> 16);
sendBuffer[147 + (to * 4)] = (UInt8) ((longitude & 0x0000FF00) >> 8);
sendBuffer[148 + (to * 4)] = (UInt8) (longitude & 0x000000FF);
sendBuffer[193 + (to * 4)] = (UInt8) ((latitude & 0xFF000000) >> 24);
sendBuffer[194 + (to * 4)] = (UInt8) ((latitude & 0x00FF0000) >> 16);
sendBuffer[195 + (to * 4)] = (UInt8) ((latitude & 0x0000FF00) >> 8);
sendBuffer[196 + (to * 4)] = (UInt8) (latitude & 0x000000FF);
}
//***********************************************************************************************
/**
* \brief Function for calculating CRC-checksum continuously, uses global CRC variable
* \param c - character for including in the CRC value
* \return c (the same character the function was called with)
*/
Char Calc_CRC(Char c)
{
Int8 tmp, count, tmpc = c;
for(count = 8; --count >= 0; c <<= 1)
{
tmp = CRC ^ c;
CRC <<= 1;
if(tmp < 0) CRC ^= CRC_POLY;
}
return tmpc;
}
//***********************************************************************************************
/**
* \brief Function for syncing the Colibri
* \return true - 3 consecutive correct answer from Colibri,
* false - 10 consecutive incorrect (or missing) answer from Colibri
*/
Boolean COLSync()
{
UInt16 cnt = 0,
failedcnt = 0,
timeoutcnt;
UInt8 c;
serial_out(LX_SYN);
ClearSerial();
SysTaskDelay(SysTicksPerSecond()/2);
serial_out(LX_SYN);
ClearSerial();
SysTaskDelay(SysTicksPerSecond()/2);
do
{
serial_out(LX_SYN);
c = 0;
timeoutcnt = 0;
while(!serial_in(&c) && (timeoutcnt < 5)) // wait 1 sec. for each LX_ACK
{
timeoutcnt++;
SysTaskDelay(SysTicksPerSecond()/5); // 0.2 sec delay
}
if (c != LX_ACK)
{
cnt = 0; // Colibri did not send LX_ACK as answer
failedcnt++;
}
else
{
cnt++; // Colibri connected and answers correctly
failedcnt = 0;
}
SysTaskDelay(SysTicksPerSecond()*3/4);
ClearSerial(); // clear buffer
} while ((cnt < 3) && (failedcnt < 10));
if (cnt == 3)
return true;
else
return false;
}
//***********************************************************************************************
/**
* \brief Function for keeping the Colibri active
* \return true - correct answer from Colibri,
* false - incorrect (or missing) answer from Colibri
*/
Boolean COLPing()
{
UInt16 timeoutcnt;
UInt8 c;
serial_out(LX_SYN);
ClearSerial();
SysTaskDelay(SysTicksPerSecond()/2);
serial_out(LX_SYN);
c = 0;
timeoutcnt = 0;
while(!serial_in(&c) && (timeoutcnt < 5)) // wait 1 sec. for each LX_ACK
{
timeoutcnt++;
SysTaskDelay(SysTicksPerSecond()/5); // 0.2 sec delay
}
if (c != LX_ACK)
return(false);
else
return(true);
}
//***********************************************************************************************
/**
* \brief Function to send a data block to the Colibri
* after the block is sent, a CRC-byte is appended, too
* \param block - pointer to the block to send
* \param size - number of bytes to send out
* \return true - successful sending (LX_ACK received after checksum sent)
* false - unsuccessful sending (NAK received, timeout, etc...)
*/
Boolean COLSendBlock(UInt8 *block, UInt16 size)
{
UInt32 i;
UInt16 timeoutcnt = 0;
Char c;
Boolean rcvd = false;
CRC = 0xFF;
for(i = 0; i < size; i++)
{
serial_out(Calc_CRC(block[i]));
}
serial_out(CRC); // send the calculated checksum
do
{
if (serial_in(&c))
{
rcvd = true;
}
else
{
timeoutcnt++;
SysTaskDelay(SysTicksPerSecond()/5);
}
} while ((timeoutcnt < 100) && !rcvd); // wait for byte from the serial port for 20s
if (rcvd && (c == LX_ACK))
return true; // LX_ACK is received after the block was sent
else
return false; // error occured (timeout, CRC, etc...)
}
//***********************************************************************************************
/**
* \brief Function to receive a data block from the Colibri
* after the block is received, CRC-check is done
* \param block - pointer to the block where the data can be stored after reception
* \param size - number of bytes to receive, the CRC-byte is not included
* \param timeout - time to wait for the next byte, in seconds
* \return true - successful (received checksum correct)
* false - unsuccessful reception (checksum incorrect, timeout, etc...)
*/
Boolean COLReadBlock(UInt8 *block, UInt16 size, UInt16 timeout)
{
UInt16 cnt = 0,
timeoutcnt = 0,
timeoutThreshold = timeout * 5;
Char c;
CRC = 0xFF;
MemSet(block, size, 0); //clear the buffer
do
{
if (serial_in(&c))
{
timeoutcnt = 0;
if (cnt < size)
block[cnt] = Calc_CRC(c);
cnt++;
}
else
{
timeoutcnt++;
SysTaskDelay(SysTicksPerSecond()/5);
}
} while ((timeoutcnt < timeoutThreshold) && (cnt < (size + 1))); // wait for data, or timeout after the predefined time
if (timeoutcnt == timeoutThreshold)
return false;
else
{
if (c == CRC)
return true;
else
return false;
}
}
//***********************************************************************************************
/**
* \brief Function for downloading one block of a flight log
* \param blockStart - starting position of the block in the Colibri's memory
* \param blockEnd - end position of the block in the Colibri's memory
* \param buffer - buffer for the received block
* \return 0 - unsuccessful flight block download
* other - successful download, returns the size of the block
*/
UInt16 COLGetFlightBlock(UInt32 blockStart, UInt32 blockEnd, UInt8 *buffer)
{
Boolean result, done;
UInt16 i, offset, blockSize = 0, currentPercent;
UInt16 sizes[16];
UInt8 blockLimits[6];
UInt32 percent = (EndPos - OrigStartPos + 256)/100;
blockLimits[0] = (UInt8) ((blockStart % 32768) & 0xFF);
blockLimits[1] = (UInt8) ((blockStart % 32768) >> 8);
blockLimits[3] = (UInt8) ((blockEnd % 32768) & 0xFF);
blockLimits[4] = (UInt8) ((blockEnd % 32768) >> 8);
blockLimits[2] = (UInt8) (blockStart / 32768);
if (blockLimits[2] > MeminfoP->endpage)
blockLimits[2] -= MeminfoP->endpage - MeminfoP->startpage + 1;
blockLimits[5] = (UInt8) (blockEnd / 32768);
if (blockLimits[5] > MeminfoP->endpage)
blockLimits[5] -= MeminfoP->endpage - MeminfoP->startpage + 1;
serial_out(LX_STX);
serial_out(LX_FLTPOS);
result = COLSendBlock(blockLimits, 6); // select block to download
if (!result) return 0; // problem occured
serial_out(LX_STX);
serial_out(LX_BSIZES);
result = COLReadBlock(buffer, 32, 5); // read 32-byte block from colibri, 5sec timeout
if (!result) return 0; // problem occured
for (i = 0; i < 16; i++)
{
sizes[i] = (256 * buffer[2*i]) + buffer[(2*i) + 1];
blockSize += sizes[i];
}
done = false;
i = 0;
offset = 0;
currentPercent = blockStart - OrigStartPos;
do
{
if (BinaryFileXfr)
{ // we need to update the progress dialog only
currentPercent += sizes[i]; // in case of LXN-file download, IGC download
HandleWaitDialogUpdate(UPDATEDIALOG, 100, currentPercent/percent, "%"); // has its own progress calculation
}
serial_out(LX_STX);
serial_out(LX_FLB0 + i);
result = COLReadBlock(&buffer[offset], sizes[i], 30); // receive flight blocks from colibri, 30sec timeout
if (!result) return 0;
offset += sizes[i];
i++;
if (i < 16)
done = (sizes[i] == 0);
} while(!done && (i < 16));
return blockSize;
}
//***********************************************************************************************
/**
* \brief Function for downloading the decrypt block at the end of the flight-log download
* \param buffer - pointer to the buffer where the data can be stored after reception
* \return true - successful decrypt block download
* false - unsuccessful download
*/
Boolean COLGetDecryptBlock(UInt8 *buffer)
{
Boolean result;
serial_out(LX_STX);
serial_out(LX_DECRYPT);
result = COLReadBlock(buffer, 256, 30); // download decrypt block from colibri, 30sec timeout
return result;
}
//***********************************************************************************************
/**
* \brief Function for storing the received flight header data into the given index of the cailogdata array
* Uses global variable array 'cailogdata'
* \param buffer - pointer to the buffer where the received flight header is stored
* \param ind - index of the 'cailogdata' array, where the flight details should be extracted
*/
void COLStoreFlightHeader(UInt8* buffer, UInt16 ind)
{
cailogdata[ind].index = ind;
cailogdata[ind].startDate.day = ((buffer[9] - 48) * 10) + (buffer[10] - 48); // convert ASCII to integers
cailogdata[ind].startDate.month = ((buffer[12] - 48) * 10) + (buffer[13] - 48);
cailogdata[ind].startDate.year = ((buffer[15] - 48) * 10) + (buffer[16] - 48);
if (buffer[18] == 0x20) // hour starts with space
cailogdata[ind].startTime.hour = (Int16) (buffer[19] - 48);
else
cailogdata[ind].startTime.hour = (Int16) (((buffer[18] - 48) * 10) + (buffer[19] - 48));
cailogdata[ind].startTime.min = (Int16) (((buffer[21] - 48) * 10) + (buffer[22] - 48)); // convert ASCII to integers
cailogdata[ind].startTime.sec = (Int16) (((buffer[24] - 48) * 10) + (buffer[25] - 48));
// cailogdata[ind].endDate;
if (buffer[27] == 0x20) // hour starts with space
cailogdata[ind].endTime.hour = (Int16) (buffer[28] - 48);
else
cailogdata[ind].endTime.hour = (Int16) (((buffer[27] - 48) * 10) + (buffer[28] - 48));
cailogdata[ind].endTime.min = (Int16) (((buffer[30] - 48) * 10) + (buffer[31] - 48)); // convert ASCII to integers
cailogdata[ind].endTime.sec = (Int16) (((buffer[33] - 48) * 10) + (buffer[34] - 48));
StrNCopy(cailogdata[ind].pilotName, &buffer[40], 24);
cailogdata[ind].FlightOfDay = (Int16) buffer[94];
cailogdata[ind].StartTape = (((UInt16) buffer[1]) * 256) + buffer[2];
cailogdata[ind].StartPage = buffer[4];
cailogdata[ind].EndTape = (((UInt16) buffer[5]) * 256) + buffer[6];
cailogdata[ind].EndPage = buffer[8];
cailogdata[ind].SerialNo = (((UInt16) buffer[91]) * 256) + buffer[92];
}
//***********************************************************************************************
/**
* \brief Function for downloading the memory information of the Colibri (or other LX-device)
* \return true - successful memory info download
* false - unsuccessful download
*/
Boolean COLGetMemInfo()
{
UInt8 *tempBuffer;
Boolean valid = false;
tempBuffer = MemPtrNew(6);
serial_out(LX_STX);
serial_out(LX_MEMINFO);
valid = COLReadBlock(tempBuffer, 6, 4); // read 6 bytes from the serial port
if (valid)
{
MeminfoP->startpos = (tempBuffer[0] * 256) + tempBuffer[1];
MeminfoP->startpage = tempBuffer[2];
MeminfoP->endpos = (tempBuffer[3] * 256) + tempBuffer[4];
MeminfoP->endpage = tempBuffer[5];
}
else
{
MeminfoP->startpos = 0;
MeminfoP->startpage = 8;
MeminfoP->endpos = (UInt16) 32768;
MeminfoP->endpage = 15;
}
MemPtrFree(tempBuffer);
return valid;
}
/**
* \brief Function for downloading one flight log in binary-format
* \return true - successful flight download
* false - unsuccessful flight download
*/
Boolean COLDownloadFlightLXN()
{
Boolean result = false;
UInt8 *buffer;
UInt32 rcvBlockSize;
buffer = MemPtrNew(FLIGHTBLOCKSIZE);
if (buffer)
{
do
{
if (COLSync())
{
StartPos += FLIGHTBLOCKSIZE;
rcvBlockSize = COLGetFlightBlock(StartPos - FLIGHTBLOCKSIZE, (StartPos<EndPos?StartPos:EndPos), buffer);
if (rcvBlockSize)
{
result = HandleVFSData(IOWRITE, buffer, &rcvBlockSize);
}
else {
result = false; // problem during downloading the block
}
}
else {
// didn't sync
result = false;
}
} while ((StartPos < EndPos) && result);
MemPtrFree(buffer);
}
// succesful so far?
if (result)
{
buffer = MemPtrNew(256);