-
Notifications
You must be signed in to change notification settings - Fork 0
/
soarIO.c
2720 lines (2444 loc) · 76.4 KB
/
soarIO.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
#include <PalmOS.h> // all the system toolbox headers
#include <SerialMgr.h>
#include <SerialMgrOld.h>
#include <MemoryMgr.h>
#include <SystemMgr.h>
#include <VFSMgr.h>
#include <DLServer.h>
#include <SmsLib.h>
#include "soaring.h"
#include "soarIO.h"
#include "soarUtil.h"
#include "soarForm.h"
#include "soarDB.h"
#include "soarMem.h"
#include "soarGar.h"
#include "soarMath.h"
#include "soarUMap.h"
#include "soarBT.h" // AGM: for prefered BT GPS connection feature
#ifdef TREOSUPPORT
#include <HsExt.h>
#include <HsPhone.h>
#endif
#define myCustomSerQueueSize 4*1024
#define VFSBufferSize 16383
UInt16 serial_ref = 0;
UInt16 serial_alt = 0;
UInt16 smslib_ref = sysInvalidRefNum;
UInt32 SerportID35 = 0x8000;
UInt32 SerportID40 = 0x8003;
UInt32 USBportID = 0x8004;
UInt32 IRportID = sysFileCVirtIrComm;
UInt32 BTportID = sysFileCVirtRfComm;
UInt32 CFportID = 'u550'; //Handera Compact Flash Serial Port
UInt32 EXportID = 0xFFFF; // connection "ExtGPS" port
FileRef vfsfileRef;
Err fileerror;
Char *customSerQP;
Char *buffer;
Char *customSerQP2;
Char *buffer2;
Char *vfsbuffer;
Char *docbuffer;
UInt16 buffermax;
Int32 waitTime = 0;
Int32 rxrecs = 0;
Char rxtype[15] = "Records";
Int16 currentFilePage = 0;
Int16 selectedFileIndex = -1;
Int16 Filenumperpage = 7;
Char transfer_filename[30];
Int8 io_file_type = NO_FILE_TYPE;
Int8 io_type = IO_NONE;
Int8 xfrdialog = XFRXFR;
Char *filelist = NULL;
Int16 numfilesfound = 0;
extern Boolean menuopen;
extern Boolean PrgCancel;
extern Boolean sim_full_speed;
void ClearSerial()
{
// HostTraceOutputTL(appErrorClass, "Clearing Serial Port");
if (device.NewSerialPresent || device.CliePDA) {
SrmReceiveFlush(serial_ref, 0);
SrmSendFlush(serial_ref);
SrmClearErr(serial_ref);
} else {
SerReceiveFlush(serial_ref, 0);
SerSendFlush(serial_ref);
SerClearErr(serial_ref);
}
}
Boolean RxData(Int8 xfertype)
{
if (xfertype == USEVFS) {
// HostTraceOutputTL(appErrorClass, "RxDataVFS");
return(RxDataVFS());
} else if (xfertype == USEMEMO) {
// HostTraceOutputTL(appErrorClass, "RxDataMemo");
return(RxDataMemo());
} else if (xfertype == USEDOC) {
// HostTraceOutputTL(appErrorClass, "RxDataDOC");
return(RxDataDOC());
} else if (device.NewSerialPresent || device.CliePDA || xfertype == USECF) {
// HostTraceOutputTL(appErrorClass, "RxDataNew");
if (serial_ref) {
return(RxDataNew());
} else {
return(false);
}
} else {
// HostTraceOutputTL(appErrorClass, "RxDataOld");
if (serial_ref) {
return(RxDataOld());
} else {
return(false);
}
}
}
Boolean RxDataOld(void)
{
UInt32 numBytes = 0;
Err err;
// Set waitTime to 1/5 of a second
waitTime = SysTicksPerSecond() / 5;
err = SerReceiveCheck(serial_ref, &numBytes);
if (err) {
SerClearErr(serial_ref);
// clean out errors and check if there is still data left
SerReceiveFlush(serial_ref, 0);
return(false);
}
// HostTraceOutputTL(appErrorClass, "serial:numBytes |%lu|", numBytes);
if (numBytes <= 0) {
return(false);
}
if (numBytes > buffermax) {
numBytes = buffermax;
}
numBytes=SerReceive(serial_ref, &buffer[0], numBytes, waitTime, &err);
if (err) {
SerClearErr(serial_ref);
SerReceiveFlush(serial_ref, 0);
return(false);
}
if ((rxrecs % 10)==0) HandleWaitDialogUpdate(UPDATEDIALOG, rxrecs, -1, rxtype);
// HostTraceOutputTL(appErrorClass, "serial:numBytes2 |%lu|", numBytes);
buffer[numBytes] = '\0';
// HostTraceOutputTL(appErrorClass, "serial:buffer |%s|", buffer);
// Using this for retransmission
// Too slow for PalmOS 4.X and below
// TxDataOld(buffer);
#ifdef NMEALOG
if (device.VFSCapable) HandleVFSData(IOWRITE, buffer, &numBytes);
#endif
data.parser.parser_func(&buffer[0], numBytes, false);
return(true);
}
Boolean RxDataNew(void)
{
Err err;
UInt32 numBytesPending=0;
UInt32 nb, numBytes = 0;
// Set waitTime to 1/5 of a second
waitTime = SysTicksPerSecond() / 5;
// HostTraceOutputTL(appErrorClass, "RxDataNew: Inside RxDataNew");
err = SrmReceiveCheck(serial_ref, &numBytesPending);
// HostTraceOutputTL(appErrorClass, "RxDataNew: numBytesPending: |%lu|", numBytesPending);
if (err == serErrLineErr) {
// HostTraceOutputTL(appErrorClass, "RxDataNew: ShowSerialError1");
// ShowSerialError();
SrmClearErr(serial_ref);
SrmReceiveFlush(serial_ref, 0);
return(false);
}
if (numBytesPending <= 0) {
// HostTraceOutputTL(appErrorClass, "RxDataNew: numBytesPending <= 0");
return(false);
}
nb = (numBytesPending > buffermax) ? buffermax : numBytesPending;
// HostTraceOutputTL(appErrorClass, "RxDataNew: nb: |%lu|", nb);
if (nb > 0) {
numBytes = SrmReceive(serial_ref, buffer, nb, waitTime, &err);
if(numBytes > 0) {
if ((rxrecs % 10)==0) HandleWaitDialogUpdate(UPDATEDIALOG, rxrecs, -1, rxtype);
// HostTraceOutputTL(appErrorClass, "RxDataNew:buffer before|%s|", buffer);
buffer[numBytes] = '\0';
// HostTraceOutputTL(appErrorClass, "RxDataNew:buffer after|%s|", buffer);
// Using this for retransmission
// Too slow for PalmOS 4.X and below
// TxDataNew(buffer);
#ifdef NMEALOG
if (device.VFSCapable) HandleVFSData(IOWRITE, buffer, &numBytes);
#endif
data.parser.parser_func(&buffer[0], numBytes, false);
nb -= numBytes;
}
if (err == serErrLineErr) {
// HostTraceOutputTL(appErrorClass, "RxDataNew: ShowSerialError2");
// ShowSerialError();
SrmClearErr(serial_ref);
SrmReceiveFlush(serial_ref, 0); // will clear the error
return(false);
}
}
return(true);
}
Boolean RxDataMemo(void)
{
UInt32 numBytesRead = buffermax;
while(HandleMemoData(IOREAD, buffer, &numBytesRead)) {
if ((rxrecs % 10)==0) HandleWaitDialogUpdate(UPDATEDIALOG, rxrecs, -1, rxtype);
// buffer[numBytesRead] = '\0';
// HostTraceOutputTL(appErrorClass, "RxDataMemo: About to parse data");
data.parser.parser_func(buffer, numBytesRead, false);
MemSet(buffer, buffermax+1, 0);
}
return(false);
}
Boolean RxDataDOC(void)
{
UInt32 numBytesRead = buffermax;
while(HandleDOCData(IOREAD, docbuffer, &numBytesRead)) {
if ((rxrecs % 10)==0) HandleWaitDialogUpdate(UPDATEDIALOG, rxrecs, -1, rxtype);
// HostTraceOutputTL(appErrorClass, "RxDataMemo: About to parse data");
// HostTraceOutputTL(appErrorClass, "numbytesread %s", DblToStr(numBytesRead,0));
data.parser.parser_func(docbuffer, numBytesRead, false);
MemSet(docbuffer, buffermax+1, 0);
numBytesRead = buffermax;
}
return(false);
}
Boolean RxDataVFS(void)
{
UInt32 numBytesRead = buffermax;
while(HandleVFSData(IOREAD, vfsbuffer, &numBytesRead)) { // && !PrgCancel) {
if ((rxrecs % 10)==0) HandleWaitDialogUpdate(UPDATEDIALOG, rxrecs, -1, rxtype);
// HandleWaitDialogUpdate(CHECKCANCEL, 0, 0, NULL);
// HostTraceOutputTL(appErrorClass, "RxDataVFS: About to parse data");
data.parser.parser_func(vfsbuffer, numBytesRead, false);
// HostTraceOutputTL(appErrorClass, "RxDataVFS: Parsed data");
}
// HostTraceOutputTL(appErrorClass, "RxDataVFS: Returning false");
return(false);
}
void ShowSerialError(void)
{
UInt16 res;
UInt32 sts;
// Char msg[132];
res = SrmGetStatus (serial_ref, &sts, &res);
SrmClearErr(serial_ref);
SrmReceiveFlush(serial_ref, 0); // will clear the error
/*
StrCopy(msg, "Serial Error");
if(res & serLineErrorParity) {
StrCat(msg,"\nParity");
}
if(res & serLineErrorHWOverrun) {
StrCat(msg,"\nHWOverrun");
}
if(res & serLineErrorFraming) {
StrCat(msg,"\nFraming");
}
if(res & serLineErrorBreak) {
StrCat(msg,"\nBreak");
}
if(res & serLineErrorHShake) {
StrCat(msg,"\nHandshake");
}
if(res & serLineErrorSWOverrun) {
StrCat(msg,"\nSW Overrun");
}
Report(msg);
*/
}
Boolean XferInit(Char *baudrate, UInt16 flowctrl, Int8 xfertype)
{
Boolean retval;
switch(xfertype) {
case USEMEMO:
// Overloading baudrate to be the file to open
// Overloading flowctrol to show whether to truncate the file on opening
return(XferInitMemo(baudrate, (UInt32)flowctrl));
break;
case USEVFS:
// Overloading baudrate to be the file to open
// Overloading flowctrol to show whether to truncate the file on opening
return(XferInitVFS(baudrate, (UInt32)flowctrl));
break;
case USEDOC:
// Overloading baudrate to be the file to open
// Overloading flowctrol to show whether to truncate the file on opening
return(XferInitDOC(baudrate, (UInt32)flowctrl));
break;
case USEIQUE:
return(XferInitiQue());
break;
case USEIQUESER:
retval = XferInitiQue();
// If the iQue failed to initialize, don't go any further
if (!retval) {
return(retval);
}
if (device.NewSerialPresent || device.CliePDA) {
// HostTraceOutputTL(appErrorClass, "XferInitNew");
retval = XferInitNew(StrAToI(baudrate), flowctrl, xfertype);
} else {
// HostTraceOutputTL(appErrorClass, "XferInitOld");
retval = XferInitOld(StrAToI(baudrate), flowctrl);
}
return(retval);
break;
case USECF:
return(XferInitNew(StrAToI(baudrate), flowctrl, xfertype));
break;
default:
if (device.NewSerialPresent || device.CliePDA) {
// HostTraceOutputTL(appErrorClass, "XferInitNew");
return(XferInitNew(StrAToI(baudrate), flowctrl, xfertype));
} else {
// HostTraceOutputTL(appErrorClass, "XferInitOld");
return(XferInitOld(StrAToI(baudrate), flowctrl));
}
break;
}
}
Boolean XferInitOld(UInt32 baudrate, UInt16 flowctrl)
{
SerSettingsType serial_settings;
Err err;
if (SysLibFind("Serial Library", &serial_ref)) {
ErrDisplay("Error trying to load the serial library");
return(false);
}
if (SerOpen(serial_ref, 0, baudrate)) {
// ErrDisplay("Could not open serial port");
FrmCustomAlert(WarningAlert, "Could not open serial port"," "," ");
serial_ref = 0;
return(false);
}
SerClearErr(serial_ref);
SerReceiveFlush(serial_ref, 0);
// Allocate a dynamic memory chunk for our custom receive queue
customSerQP = MemPtrNew(myCustomSerQueueSize);
// buffermax = 16+max(baudrate/20, 128);
if (baudrate/20 > 128) {
buffermax = 16+(baudrate/20);
} else {
buffermax = 16+128;
}
buffer = MemPtrNew (buffermax+1);
// Replace the default receive queue
if (customSerQP)
err = SerSetReceiveBuffer(serial_ref, customSerQP, myCustomSerQueueSize);
if (flowctrl) {
serial_settings.flags = serSettingsFlagStopBits1 | serSettingsFlagBitsPerChar8 |
serSettingsFlagRTSAutoM | serSettingsFlagCTSAutoM;
serial_settings.ctsTimeout = SERIAL_TIMEOUT_DEFAULT;
} else {
serial_settings.flags = serSettingsFlagStopBits1 | serSettingsFlagBitsPerChar8 ;
serial_settings.ctsTimeout=0;
}
serial_settings.baudRate=baudrate;
// HostTraceOutputTL(appErrorClass, "XferInit: baudrate - |%lu|", baudrate);
if (SerSetSettings(serial_ref,&serial_settings)) {
ErrDisplay("Err when setting serial port values");
return(false);
}
return(true);
}
Boolean XferInitNew (UInt32 baudrate, UInt16 flowctrl, Int8 xfertype)
{
Err error = 0;
UInt32 flgs = 0;
DeviceInfoType dt;
Char msg[128];
Int16 n = 0;
UInt16 sz;
UInt32 value;
Int32 ctsTimeout = SERIAL_TIMEOUT_DEFAULT;
*msg = 0;
error = FtrGet(sysFileCSerialMgr, sysFtrNewSerialPresent, &value);
if (error != 0) {
ErrDisplay("Error trying to load the New Serial Library");
return(false);
}
if (OpenPortNew(baudrate, xfertype)) {
// HostTraceOutputTL(appErrorClass, "XferInitNew: OpenPortNew returned true-error");
return(false);
}
SrmClearErr(serial_ref);
SrmReceiveFlush(serial_ref, 0);
SrmGetDeviceInfo(serial_ref, &dt);
if(baudrate > dt.serDevMaxBaudRate) {
baudrate = dt.serDevMaxBaudRate;
n = StrPrintF(msg, "Rate capped at %ld baud", baudrate);
}
if(flowctrl == NOFLOW && baudrate > dt.serDevHandshakeBaud) {
// flowctrl = RTS;
// if(n) {
// *(msg+n++) = '\n';
// }
// n += StrPrintF(msg,"\nForcing RTS for %ld baud", baudrate);
}
if(n) {
Report(msg);
}
// HostTraceOutputTL(appErrorClass, "XferInitNew: Setting baudrate-|%lu|", baudrate);
// Sets the baudrate
sz = sizeof((Int32)baudrate);
error = SrmControl(serial_ref, srmCtlSetBaudRate, &baudrate, &sz);
if (flowctrl == RTS || flowctrl == BOTH) {
// Turns on RTS/CST Flow control
flgs |= (srmSettingsFlagCTSAutoM |
srmSettingsFlagRTSAutoM |
srmSettingsFlagFlowControlIn);
// Supposedly needed to get Tungsten serial port into proper state
flgs = flgs & ~srmSettingsFlagRTSInactive;
} else {
// Turns off all hardware flow control
flgs &= ~(srmSettingsFlagCTSAutoM |
srmSettingsFlagRTSAutoM |
srmSettingsFlagFlowControlIn);
}
// Turns off Stop Bits Settings
// flgs &= ~srmSettingsFlagStopBitsM;
// Sets Stop Bits to 1
flgs |= srmSettingsFlagStopBits1;
// Turns off BitsPerChar Setting
// flgs &= ~srmSettingsFlagBitsPerCharM;
// Sets to 8 bits per character
flgs |= srmSettingsFlagBitsPerChar8;
// switch(prefs.bits) {
// case 5:
// flgs |= srmSettingsFlagBitsPerChar5;
// break;
// case 6:
// flgs |= srmSettingsFlagBitsPerChar6;
// break;
// case 7:
// flgs |= srmSettingsFlagBitsPerChar7;
// break;
// default:
// flgs |= srmSettingsFlagBitsPerChar8;
// break;
// }
// if (flowctrl == XON || flowctrl == FLOWBOTH) {
// Turns Xon/Xoff Flow Control On
// flgs |= srmSettingsFlagXonXoffM ;
// } else {
// Turns Xon/Xoff Flow Control Off
// flgs &= ~srmSettingsFlagXonXoffM ;
// }
// Turns Parity Off
// flgs &= ~(srmSettingsFlagParityOnM | srmSettingsFlagParityEvenM);
// if (prefs.parity == 1) {
// Sets Odd Parity
// flgs |= srmSettingsFlagParityOnM;
// } else if (prefs.parity == 2) {
// Sets Even Parity
// flgs |= srmSettingsFlagParityOnM | srmSettingsFlagParityEvenM;
// }
sz = sizeof(flgs);
if(error == 0) {
error = SrmControl(serial_ref, srmCtlSetFlags, &flgs, &sz);
}
if (flowctrl == NOFLOW) {
ctsTimeout = 0;
}
sz = sizeof(ctsTimeout);
if(error == 0) {
error = SrmControl(serial_ref, srmCtlSetCtsTimeout, &ctsTimeout, &sz);
}
if(xfertype == USEIR) {
// error = SrmControl(serial_ref, srmCtlIrDAEnable,NULL,NULL);
// error = SrmControl(serial_ref, srmCtlRxEnable,NULL,NULL);
}
if (error) {
SrmClearErr(serial_ref);
// ErrNonFatalDisplayIf(error, errSettingSerial);
ErrDisplay(errSettingSerial);
return(false);
} else {
customSerQP = MemPtrNew(myCustomSerQueueSize);
if (baudrate/20 > 128) {
buffermax = 16+(baudrate/20);
} else {
buffermax = 16+128;
}
// For Testing
// buffermax = 1024 * 6;
// buffer = MemPtrNew(buffermax+1);
buffer = MemPtrNew(2*buffermax+1);
error = SrmSetReceiveBuffer (serial_ref, customSerQP, myCustomSerQueueSize);
if (error) {
ErrNonFatalDisplayIf ((error == serErrBadPort), "Port Doesn't Exist");
ErrNonFatalDisplayIf ((error == memErrNotEnoughSpace), "Out Of Memory");
}
}
SrmReceiveFlush(serial_ref, 0); // will clear the error
// HostTraceOutputTL(appErrorClass, "XferInitNew: Succeeded-about to return false");
return(true);
}
Boolean XferInitAlt (UInt32 baudrate, UInt16 flowctrl, Int8 xfertype)
{
Err error = 0;
UInt32 flgs = 0;
DeviceInfoType dt;
Char msg[128];
Int16 n = 0;
UInt16 sz;
UInt32 value;
Int32 ctsTimeout = SERIAL_TIMEOUT_DEFAULT;
*msg = 0;
error = FtrGet(sysFileCSerialMgr, sysFtrNewSerialPresent, &value);
if (error != 0) {
ErrDisplay("Error trying to load the New Serial Library");
return(false);
}
// if (OpenPortNew(baudrate, xfertype)) {
// HostTraceOutputTL(appErrorClass, "XferInitNew: OpenPortNew returned true-error");
// return(false);
// }
if (device.romVersion >= SYS_VER_40) {
error = SrmOpen(SerportID40, baudrate, &serial_alt);
} else {
error = SrmOpen(SerportID35, baudrate, &serial_alt);
}
if (error) {
return(false);
// HostTraceOutputTL(appErrorClass, "XferInitNew: OpenPortNew returned true-error");
}
SrmClearErr(serial_alt);
SrmReceiveFlush(serial_alt, 0);
SrmGetDeviceInfo(serial_alt, &dt);
if(baudrate > dt.serDevMaxBaudRate) {
baudrate = dt.serDevMaxBaudRate;
n = StrPrintF(msg, "Rate capped at %ld baud", baudrate);
}
if(flowctrl == NOFLOW && baudrate > dt.serDevHandshakeBaud) {
// flowctrl = RTS;
// if(n) {
// *(msg+n++) = '\n';
// }
// n += StrPrintF(msg,"\nForcing RTS for %ld baud", baudrate);
}
if(n) {
Report(msg);
}
// HostTraceOutputTL(appErrorClass, "XferInitNew: Setting baudrate-|%lu|", baudrate);
// Sets the baudrate
sz = sizeof((Int32)baudrate);
error = SrmControl(serial_alt, srmCtlSetBaudRate, &baudrate, &sz);
if (flowctrl == RTS || flowctrl == BOTH) {
// Turns on RTS/CST Flow control
flgs |= (srmSettingsFlagCTSAutoM |
srmSettingsFlagRTSAutoM |
srmSettingsFlagFlowControlIn);
// Supposedly needed to get Tungsten serial port into proper state
flgs = flgs & ~srmSettingsFlagRTSInactive;
} else {
// Turns off all hardware flow control
flgs &= ~(srmSettingsFlagCTSAutoM |
srmSettingsFlagRTSAutoM |
srmSettingsFlagFlowControlIn);
}
// Turns off Stop Bits Settings
// flgs &= ~srmSettingsFlagStopBitsM;
// Sets Stop Bits to 1
flgs |= srmSettingsFlagStopBits1;
// Turns off BitsPerChar Setting
// flgs &= ~srmSettingsFlagBitsPerCharM;
// Sets to 8 bits per character
flgs |= srmSettingsFlagBitsPerChar8;
// switch(prefs.bits) {
// case 5:
// flgs |= srmSettingsFlagBitsPerChar5;
// break;
// case 6:
// flgs |= srmSettingsFlagBitsPerChar6;
// break;
// case 7:
// flgs |= srmSettingsFlagBitsPerChar7;
// break;
// default:
// flgs |= srmSettingsFlagBitsPerChar8;
// break;
// }
// if (flowctrl == XON || flowctrl == FLOWBOTH) {
// Turns Xon/Xoff Flow Control On
// flgs |= srmSettingsFlagXonXoffM ;
// } else {
// Turns Xon/Xoff Flow Control Off
// flgs &= ~srmSettingsFlagXonXoffM ;
// }
// Turns Parity Off
// flgs &= ~(srmSettingsFlagParityOnM | srmSettingsFlagParityEvenM);
// if (prefs.parity == 1) {
// Sets Odd Parity
// flgs |= srmSettingsFlagParityOnM;
// } else if (prefs.parity == 2) {
// Sets Even Parity
// flgs |= srmSettingsFlagParityOnM | srmSettingsFlagParityEvenM;
// }
sz = sizeof(flgs);
if(error == 0) {
error = SrmControl(serial_alt, srmCtlSetFlags, &flgs, &sz);
}
if (flowctrl == NOFLOW) {
ctsTimeout = 0;
}
sz = sizeof(ctsTimeout);
if(error == 0) {
error = SrmControl(serial_alt, srmCtlSetCtsTimeout, &ctsTimeout, &sz);
}
if(xfertype == USEIR) {
// error = SrmControl(serial_alt, srmCtlIrDAEnable,NULL,NULL);
// error = SrmControl(serial_alt, srmCtlRxEnable,NULL,NULL);
}
if (error) {
SrmClearErr(serial_alt);
// ErrNonFatalDisplayIf(error, errSettingSerial);
ErrDisplay(errSettingSerial);
return(false);
} else {
customSerQP2 = MemPtrNew(myCustomSerQueueSize);
if (baudrate/20 > 128) {
buffermax = 16+(baudrate/20);
} else {
buffermax = 16+128;
}
// For Testing
// buffermax = 1024 * 6;
// buffer = MemPtrNew(buffermax+1);
buffer2 = MemPtrNew(2*buffermax+1);
error = SrmSetReceiveBuffer (serial_alt, customSerQP2, myCustomSerQueueSize);
if (error) {
ErrNonFatalDisplayIf ((error == serErrBadPort), "Port Doesn't Exist");
ErrNonFatalDisplayIf ((error == memErrNotEnoughSpace), "Out Of Memory");
}
}
SrmReceiveFlush(serial_alt, 0); // will clear the error
// HostTraceOutputTL(appErrorClass, "XferInitNew: Succeeded-about to return false");
return(true);
}
Boolean XferInitMemo(Char *fileName, UInt32 inittype)
{
buffermax = 82;
buffer = MemPtrNew (buffermax+1);
// Initialize the memopad output
return(HandleMemoData(IOINIT, fileName, &inittype));
}
Boolean XferInitDOC(Char *fileName, UInt32 inittype)
{
buffermax = 82;
docbuffer = MemPtrNew (buffermax+1);
MemSet(docbuffer, buffermax+1, 0);
// Initialize the DOC output
return(HandleDOCData(IOINIT, fileName, &inittype));
}
Boolean XferInitVFS(Char *fileName, UInt32 inittype)
{
buffermax = 82;
vfsbuffer = MemPtrNew (buffermax+1);
// Initialize the VFS output
return(HandleVFSData(IOINIT, fileName, &inittype));
}
Boolean VFSFileCopy(Char *oldpath, Char *newpath)
{
FileRef ofref, nfref;
UInt32 numBytes = 0;
Err err;
static Char *filebuffer = NULL;
UInt16 volRefNum = vfsInvalidVolRef;
UInt32 volIterator = vfsIteratorStart;
// Open the card
while (volIterator != vfsIteratorStop) {
err = VFSVolumeEnumerate(&volRefNum, &volIterator);
if (err == errNone) {
// Stopping on the first found
// Should be OK for now
break;
}
}
if (volRefNum == vfsInvalidVolRef) return(false);
// initialise buffer
filebuffer = MemPtrNew(VFSBufferSize+1);
if (!filebuffer) return(false);
MemSet(filebuffer, VFSBufferSize+1, 0);
// open files
err = VFSFileOpen(volRefNum, oldpath, vfsModeRead, &ofref);
if (err != errNone) return(false);
err = VFSFileOpen(volRefNum, newpath, (vfsModeWrite|vfsModeCreate|vfsModeTruncate), &nfref);
if (err != errNone) return(false);
// Copy the old file to the new one
while (!VFSFileEOF(ofref)) {
err = VFSFileRead(ofref, VFSBufferSize, filebuffer, &numBytes);
if (numBytes >= 1) err = VFSFileWrite(nfref, numBytes, filebuffer, &numBytes);
}
if (err != errNone) return(false);
// Close the files
VFSFileClose(ofref);
VFSFileClose(nfref);
// free buffer memory
if (filebuffer) {
MemPtrFree(filebuffer);
filebuffer = NULL;
}
return(true);
}
Boolean HandleVFSData(Int8 cmd, Char *data, UInt32 *numBytes)
{
Err err = errNone;
static UInt16 volRefNum=vfsInvalidVolRef;
UInt32 volIterator = vfsIteratorStart;
Char basedirName1[16];
Char basedirName2[16];
Char dirName[256];
Char fileName[256];
static FileRef vfsdirRef;
static FileRef vfsfileRef;
static UInt32 filepos = 0;
static Char *filebuffer = NULL;
Int16 prev_numfilesfound = 0;
FileInfoType FileInfo;
Char nameP[FILENAMESIZE];
Char *prev_filelist = NULL;
Char extension[5];
StrCopy (basedirName1, "/PALM");
StrCopy (basedirName2, "/PALM/Programs");
StrCopy (dirName, "/PALM/Programs/SoarPilot");
if ((cmd == IODELETE) || (cmd == IOEXIST) || (cmd == IODIR) || (cmd == IOINIT)) {
// common code to check for a card present and the /PALM/Programs/SoarPilot directory
volRefNum = vfsInvalidVolRef;
if (device.CardPresent) {
while (volIterator != vfsIteratorStop)
{
err = VFSVolumeEnumerate(&volRefNum, &volIterator);
if (err == errNone) {
// Stopping on the first found
// Should be OK for now
break;
}
}
if (volRefNum == vfsInvalidVolRef) {
return(false);
}
// try to open the /PALM directory to ensure that it is present
err = VFSFileOpen(volRefNum, basedirName1, (vfsModeReadWrite), &vfsdirRef);
if (err == vfsErrFileNotFound) {
err = VFSDirCreate(volRefNum, basedirName1);
if (err != errNone) {
return(false);
}
} else if (err != errNone) {
return(false);
}
VFSFileClose(vfsdirRef);
// try to open the /PALM/Programs directory to ensure that it is present
err = VFSFileOpen(volRefNum, basedirName2, (vfsModeReadWrite), &vfsdirRef);
if (err == vfsErrFileNotFound) {
err = VFSDirCreate(volRefNum, basedirName2);
if (err != errNone) {
return(false);
}
} else if (err != errNone) {
return(false);
}
VFSFileClose(vfsdirRef);
// try to open the /Palm/Programs/SoarPilot directory & get a valid dirref
err = VFSFileOpen(volRefNum, dirName, (vfsModeReadWrite), &vfsdirRef);
if (err == vfsErrFileNotFound) {
err = VFSDirCreate(volRefNum, dirName);
if (err != errNone) {
return(false);
}
} else if (err != errNone) {
return(false);
}
VFSFileClose(vfsdirRef);
} else {
FrmCustomAlert(WarningAlert, "No Memory Card In Slot"," "," ");
return(false);
}
}
switch(cmd) {
case IODELETE:
// delete a file
StrCopy(fileName, dirName);
StrCat(fileName, "/");
// The filename is passed in data
StrCat(fileName, data);
err = VFSFileDelete(volRefNum, fileName);
if (err == errNone) {
return(true);
} else {
fileerror = err;
return(false);
}
break;
case IOEXIST:
// test if a filename is valid and exists
StrCopy(fileName, dirName);
StrCat(fileName, "/");
// The filename is passed in data
StrCat(fileName, data);
err = VFSFileCreate(volRefNum, fileName);
if (err == errNone) {
err = VFSFileDelete(volRefNum, fileName);
return(true);
} else {
fileerror = err;
return(false);
}
break;
case IODIR:
// list files in /Palm/Programs/SoarPilot directory that have the required extension (data)
numfilesfound = 0;
prev_numfilesfound = 0;
err = VFSFileOpen(volRefNum, dirName, vfsModeRead, &vfsdirRef);
if(err == errNone) {
FileInfo.nameP = nameP; // point to local buffer
FileInfo.nameBufLen = FILENAMESIZE;
volIterator = vfsIteratorStart;
while (volIterator != vfsIteratorStop) {
MemSet(nameP, FILENAMESIZE, 0);
// Get the next file
err = VFSDirEntryEnumerate(vfsdirRef, &volIterator, &FileInfo);
if (err == errNone) {
// HostTraceOutputTL(appErrorClass, "File =|%s|", FileInfo.nameP);
// check file extension (ignore case)
StrToLower(extension , Right(nameP, 4));
if (StrCompare(extension, data) == 0) {
// HostTraceOutputTL(appErrorClass, "Add File to List");
// increment file count
numfilesfound++;
// add item to the list
if (prev_numfilesfound > 0) {
AllocMem((void *)&prev_filelist, prev_numfilesfound * FILENAMESIZE);
MemMove(prev_filelist, filelist, prev_numfilesfound * FILENAMESIZE);
}
if (filelist) FreeMem((void *)&filelist);
AllocMem((void *)&filelist, numfilesfound * FILENAMESIZE);
if ((prev_numfilesfound > 0) && prev_filelist) MemMove(filelist, prev_filelist, prev_numfilesfound * FILENAMESIZE);
// filename without extension
StrCopy(&filelist[(numfilesfound-1)*FILENAMESIZE], Left(nameP, StrLen(nameP)-4));
if ((prev_numfilesfound > 0) && prev_filelist) FreeMem((void *)&prev_filelist);
prev_numfilesfound = numfilesfound;
}
} else {
// handle error, possibly by breaking out of the loop
break;
}
}
} else {
// handle directory open error here
return(false);
}
VFSFileClose(vfsdirRef);
quicksort(filelist, numfilesfound, FILENAMESIZE, 1);
*numBytes = numfilesfound;
return(true);
break;
case IOINIT:
if (StrLen(data) < 5) return(false);
StrCopy(fileName, dirName);
StrCat(fileName, "/");
// The filename is passed in data
StrCat(fileName, data);
// initialise buffer
filepos = 0;
filebuffer = MemPtrNew(VFSBufferSize+1);
if (!filebuffer) return(false);
MemSet(filebuffer, VFSBufferSize+1, 0);
// Overloading numBytes to pass whether to clean the file upon opening
if (*numBytes == IOOPENTRUNC) {
// try to open the file and get a valid fileref for the file
err = VFSFileOpen(volRefNum, fileName, (vfsModeReadWrite|vfsModeCreate|vfsModeTruncate), &vfsfileRef);
} else {
err = VFSFileOpen(volRefNum, fileName, (vfsModeRead), &vfsfileRef);
}
if (err != errNone) {
return(false);
}
return(true);
break;
case IOWRITE:
// new buffered write code from soarCol.c COLWriteString function
// HostTraceOutputTL(appErrorClass, "file pos-|%lu|", filepos);
if (*numBytes) {
if ((filepos + *numBytes + 1) > VFSBufferSize) {
// HostTraceOutputTL(appErrorClass, "WRITE Data1-|%lu|", filepos);
err = VFSFileWrite(vfsfileRef, filepos, filebuffer, NULL); // write the buffer to the end of the output file
if (err == errNone) {
MemSet(filebuffer, VFSBufferSize, 0); // erase the whole buffer
MemMove(filebuffer, data, *numBytes); // copy the new string to the beginning of the empty buffer
filepos = *numBytes;
}
} else {
MemMove(&filebuffer[filepos], data, *numBytes); // copy the new string to the end of the buffer
filepos += *numBytes;
}
} else {
// HostTraceOutputTL(appErrorClass, "WRITE Data2-|%lu|", filepos);