-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsftardcal.c
4515 lines (3815 loc) · 110 KB
/
sftardcal.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
// sftardcal.c - Generic framework for calibrating or controlling a serial port
// Arduino-based device. For BSD, Linux, Cygwin, and Windows
//
// This is a framework application file. Each version must implement the
// 'calibrate_loop()' function (or build sftardcal.c with STAND_ALONE defined)
// '#define' values are typically used to determine which version to build,
// based on the Makefile, and any additional source files that implement the
// 'calibrate_loop()' function.
//
// Company web site: http://mrp3.com/ e-mail: [email protected]
//
// COPYRIGHT:
//
// Copyright (c) 2011-2013 S.F.T. Inc. - all rights reserved
//
// WARRANTY:
//
// There is NO WARRANTY, implied or otherwise. This software is being
// supplied "as-is" and updates may be made without notification to you
// at any time. You can expect to have to fix this for your platform
// and be pleasantly surprised if it works without any changes. Some
// features may be incomplete, marked with 'TODO' and/or skeleton code.
// Your mileage may vary. In other words, use it at your OWN risk.
//
// LICENSE TERMS:
//
// This code is available under a DUAL LICENSE, your choice of which one.
// You may either choose a BSD-like license, or GPLv2 (or later).
//
// BSD LICENSE
// You may copy, build, execute, reverse-engineer, derive work from,
// and/or re-distribute this source file (along with its corresponding
// header file) to any 3rd party you choose, provided that
//
// a) You include the original copyright, warrantee, and license terms
// in their entirety
// b) You do not re-license this source in any manner inconsistent with
// the copyright or license terms.
//
// You may ALSO produce a 'closed-source' (binary only) work that is
// either derived or entirely copied from this work, provided that you
// include a statement similar to:
// Derived from sftardcal, Copyright 2011-2013 S.F.T. Inc.
// in a prominent place within your software license or documentation.
// This may include 'help' or 'about' displays that are activated by user
// action, or on application startup.
//
//
// GPLv2 (or later)
//
// Please see included 'gpl-2.0.txt' for a copy of version 2.0 of this license.
// Also you can view it at https://www.gnu.org/licenses/gpl-2.0.txt
//
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <fcntl.h>
#ifdef WIN32
#include <Windows.h> // generic windows defines
#include <process.h>
#else // !WIN32
#include <sys/time.h>
#include <arpa/inet.h>
#include <termios.h>
#include <poll.h>
#include <unistd.h>
#include <signal.h>
//#ifdef __FreeBSD__
//#include <sys/ttycom.h> // stuff I need for IOCTLs etc.
//#else // __FreeBSD__ // assume Linux
#include <sys/ioctl.h> // linux needs this instead
//#endif // __FreeBSD__
#include <sys/socket.h>
#include <sys/stat.h>
#include <sys/wait.h>
#include <sys/types.h>
//#include <netinet6/in6.h> // sockaddr_in6 and ipv6-related stuff
#include <netinet/in.h> // sockaddr_in
#endif // WIN32
#include "sftardcal.h"
#ifdef WITH_XMODEM
#define SFTARDCAL
#include "xmodem.c"
#endif // WITH_XMODEM
#define DEFAULT_RESET_WAIT 5
//#define LINUX_SPECIAL_HANDLING
// DEFAULT SERIAL CONFIGURATION: 9600 baud, n, 8, 1 using argv[1] or /dev/ttyU0 as the input
// option vars
#ifdef WIN32
const char *pIn = "\\\\.\\COM1"; // winders needs something different, assume COM1 for now
#elif defined(__FreeBSD__)
const char *pIn = "/dev/ttyU0";
#elif defined(__gnu_linux__) && defined(__APCS_32__) && defined(__ARM_ARCH_6__) && defined(__ARMEL__)
// assume raspberry pi with raspbian using BCM2835 which is ARMv6 with VFP (uname shows as armv6l)
// for now I don't check for the presence of __VFP_FP__ nor __ARM_PCS_VFP
const char *pIn = "/dev/ttyAMA0"; // default serial on RPi with raspbian
#else // Linux assumed here
const char *pIn = "/dev/ttyACM0";
#endif // WIN32
const char *pApp;
int bRawFlag = 0;
HANDLE iStdOut; // note that for non-WIN32 I define 'HANDLE' as 'int'
#ifdef WIN32
#define pAltConsole FALSE
#define COMM_RW_EV EV_ERR | EV_RXCHAR | EV_TXEMPTY
#define COMM_RO_EV EV_ERR | EV_RXCHAR
#else // WIN32
char *pAltConsole = NULL;
#endif // WIN32
// default serial parameters
static char szBaud[256]="9600,n,8,1"; // baud rate expression, default is 9600,n,8,1
#ifdef WITH_XMODEM
static char szXModemFile[512]="";
#endif // WITH_XMODEM
static int iExperimental = 0;
static int iVerbosity = 0, bFactoryReset = 0, bLocalEcho = 0, bSerialDebug=0, bListenMode=0, bIsTCP=0,
iResetWait=0, iFlowControl=0, bQuietFlag = 0;
static int iTerminator = 0; // CRLF [default]
#ifdef WITH_XMODEM
static int bXModemFlag=0;
#endif // WITH_XMODEM
static int bQuitFlag = 0; // assign to non-zero to force app to exit (must test for it after input)
static int bCommandRepeatOnTimeoutFlag = 1; // default is to repeat a command every second until it 'takes'
static char *pszQuestion = NULL; // not null to ask a question, get reply, and exit
static int iQuestionWait=5000; // default question wait time
// other internal (semi-global) flags
static int bMyGetsEchoFlag = 1;
// SAMPLE COMMANDS
// to pipe /dev/ttyU0 to/from an EXISTING unix socket,
// sftardcal -c /var/run/unix_socket /dev/ttyU0
static void ttyconfigSTR(HANDLE iFile, char *szBaud);
#ifdef WITH_XMODEM
static void do_xmodem(HANDLE iFile, HANDLE iConsole); // internal XMODEM functionality
#endif // WITH_XMODEM
// console restore and 'alt console' - non-WIN32 only
#ifndef WIN32
static HANDLE hIOSConsoleRestoreHandle = -1;
static struct termios sIOSConsole;
void conrestore(void);
int configure_alt_console(HANDLE *piConsole); // NOTE: 'piConsole' must be a valid pointer
#endif // !WIN32
int do_main(int argc, char *argv[], char *envp[], HANDLE *piFile, HANDLE *piConsole);
#ifndef WIN32
HANDLE iGlobalFileHandle = -1; // global because FBSD will need to unlock it
#endif // WIN32
int main(int argc, char *argv[], char *envp[])
{
#ifdef WIN32
HANDLE iFile;
#endif // WIN32
HANDLE iConsole; // for non-WIN32 'HANDLE' is defined as 'int'
int iRval;
#ifdef WIN32
iFile = iConsole = INVALID_HANDLE_VALUE;
#else // !WIN32
iGlobalFileHandle = iConsole = -1;
#endif // WIN32
#ifdef WIN32
iRval = do_main(argc, argv, envp, &iFile, &iConsole);
if(iFile != INVALID_HANDLE_VALUE)
{
CloseHandle(iFile);
}
if(iConsole != INVALID_HANDLE_VALUE)
{
CloseHandle(iConsole);
}
#else // !WIN32
iRval = do_main(argc, argv, envp, &iGlobalFileHandle, &iConsole);
// TODO: restore default signal handlers? disable signal handlers?
if(iGlobalFileHandle >= 0)
{
#ifdef __FreeBSD__
flock(iGlobalFileHandle, LOCK_UN);
#endif // __FreeBSD__
close(iGlobalFileHandle);
iGlobalFileHandle = -1; // TODO: race condition?
}
conrestore();
if(iConsole >= 0)
{
close(iConsole);
}
#endif // WIN32
return iRval;
}
void usage()
{
fprintf(stderr,
"%s - Copyright (c) 2011-2013 S.F.T. Inc. - all rights reserved\n\n"
"usage:\t%s [-h]|[-[e][r[m|n]][R][v[v...]][B baud][N|W wait]",
pApp, pApp);
fputs(
#ifndef WIN32
"[c|l device|socket|[IP]:port]"
#endif // WIN32
"] device\n"
" where\t-r puts you in 'raw' terminal mode\n"
" and\t-m sets the terminator to [CR] in 'raw' mode\n"
" and\t-n sets the terminator to [LF] in 'raw' mode\n"
" and\t-B assigns the 'baud rate' configuration string\n"
"\t example: sftardcal -B 9600,n,8,1\n"
" and\t-N disables the serial port auto-reset\n"
" and\t-Q stifles output on stderr\n"
" and\t-F enables hardware flow control (implies -N)\n"
" and\t-W assigns the reset 'wait' period in seconds (default 5)\n"
#ifndef WIN32
" and\t-d dumps [serial port] debug information\n"
#endif // WIN32
" and\t-e enables local echo\n"
" and\t-R forces a factory reset during calibration\n"
" and\t-v increases verbosity of debug info sent to stderr\n"
"\t (can be specified multiple times to increase verbosity)\n"
#ifdef WITH_XMODEM
" and\t-X[S|R][filename] performs an xmodem transfer\n"
"\t the 'S' or 'R' must immediately precede the file name (no space)\n"
"\t The command 'XSfilename' or 'XRfilename' (followed by \\r) is sent\n"
"\t to the remote device, followed by the file transfer itself.\n"
"\t This option may not be used with '-q', '-r', or '-R'\n"
#endif // WITH_XMODEM
#ifndef WIN32
" and\t-c specifies an alternate console for stdin,stdout\n"
"\t (useful for providing a tunnel from a VM running this software)\n"
"\t specifying a port as ':port' assumes localhost:port\n"
" and\t-l is a special case version of '-c' that allows you to LISTEN for\n"
"\t a TCP connection on a specific TCP port, optionally specifying\n"
"\t the 'bind' address (default is all available IPs).\n"
" and\t-q specifies a 'question' to send. response returned on stdout\n"
"\t implies '-N' to disable serial port auto-reset.\n"
"\t This option may not be used with '-r', '-R', or '-X'\n"
" and\t-w specifies a wait time (for use with '-q'), default 5 seconds\n"
#endif // WIN32
"\n"
"-and-\t-h prints this message\n\n", stderr);
fprintf(stderr, "Default serial port device is \"%s\"\n\n", pIn);
}
#ifndef WIN32
void signalproc(int iSig)
{
static const char szMsg[]="\nError - exit on signal (console settings restored)\n";
if(iGlobalFileHandle != -1)
{
#ifdef __FreeBSD__
flock(iGlobalFileHandle, LOCK_UN);
#endif // __FreeBSD__
close(iGlobalFileHandle); // a hack for now
iGlobalFileHandle = -1;
}
conrestore();
write(2, szMsg, sizeof(szMsg) - 1);
_exit(iSig); // bye [must call THIS version of 'exit()']
}
#endif // WIN32
int do_main(int argc, char *argv[], char *envp[], HANDLE *piFile, HANDLE *piConsole)
{
int i1;
const char *p1;
#ifndef WIN32
// these signals all terminate the process, though I will
// want to make sure I restore the console settings before
// the program exits. As such I use a signal proc, then call
// '_exit()' to terminate the application.
signal(SIGINT, signalproc);
signal(SIGTSTP, signalproc);
signal(SIGTERM, signalproc);
signal(SIGUSR1, signalproc);
signal(SIGUSR2, signalproc);
#endif // WIN32
// stdout handle - for POSIX it's always '1', for Windows you need 'GetStdHandle()'
#ifdef WIN32
iStdOut = GetStdHandle(STD_OUTPUT_HANDLE); // windows supplies an API for this
#else // !WIN32
iStdOut = 1; // in POSIX OS's, stdout is always '1'
#endif // WIN32
pApp = argv[0];
p1 = pApp;
pApp += strlen(pApp) - 1;
while(pApp > p1 && *(pApp - 1) != '/')
{
pApp--;
}
i1 = do_options(argc, argv, envp);
if(i1 || argc <= 0)
{
usage();
return(i1);
}
#ifdef WIN32
if(!DuplicateHandle(GetCurrentProcess(), GetStdHandle(STD_INPUT_HANDLE),
GetCurrentProcess(), piConsole,
0, 0, DUPLICATE_SAME_ACCESS))
if(*piConsole == INVALID_HANDLE_VALUE)
#else // WIN32
// to allow testing windows in a 'virtualbox' VM I added the '-c' option, which
// re-directs console I/O to a pipe, device, or socket. device/pipe I/O is attempted
// first, followed by socket I/O. TODO: use 'stat' to determine which to use
if(pAltConsole) // alternate console, for testing via tunnel to VM's serial port
{
i1 = configure_alt_console(piConsole); // NOTE: this assigns 'iConsole'
if(i1)
{
return i1; // an error
}
}
else
{
*piConsole = dup(0); // copy of STDIN's handle
}
if(*piConsole < 0)
#endif // WIN32
{
fprintf(stderr, "Unable to dup console, %d\n", errno);
return -2;
}
#ifndef WIN32
if(pAltConsole) // if using alternate console, don't do 'conconfig'
{
altconconfig(*piConsole); // in case I must configure it like a console
}
else
#endif // WIN32
{
conconfig(*piConsole); // configure the console (for windows it starts a thread)
}
#ifdef WIN32
*piFile = CreateFile(pIn, GENERIC_READ | GENERIC_WRITE, 0,
NULL, OPEN_EXISTING, FILE_FLAG_OVERLAPPED, 0);
if(*piFile == INVALID_HANDLE_VALUE)
{
fprintf(stderr, "Unable to open %s\n", pIn);
CloseHandle(*piConsole);
*piConsole = INVALID_HANDLE_VALUE;
return -1;
}
#else // WIN32
#ifdef __FreeBSD__
*piFile = open(pIn, (O_RDWR | O_NONBLOCK /*| O_EXLOCK*/), 0);
if(*piFile >= 0)
{
if(flock(*piFile, LOCK_EX | LOCK_NB) < 0)
{
fprintf(stderr, "ERROR: unable to lock \"%s\", errno=%d\n",
pIn, errno);
close(*piFile);
*piFile = -1;
}
}
#elif defined(__linux__) && defined(LINUX_SPECIAL_HANDLING)
*piFile = open(pIn, (O_RDWR | O_NOCTTY | O_NONBLOCK | O_NDELAY), 0); // Linux does not support O_EXLOCK and needs 'O_NOCTTY' and 'O_NDELAY'
#else // !__FreeBSD__, !__linux__
*piFile = open(pIn, (O_RDWR | O_NONBLOCK), 0); // Linux does not support O_EXLOCK
#endif // __FreeBSD__
if(*piFile < 0)
{
fprintf(stderr, "Unable to open %s\n", pIn);
close(*piConsole);
*piConsole = -1;
return -1;
}
#endif // WIN32
// properly parse out the baud rate as BAUD,parity,bits,stop i.e. "9600,n,8,1"
// and assign it
ttyconfigSTR(*piFile, szBaud); // POOBAH
if(iExperimental)
{
// perform experiments here
goto exit_point;
}
if(iResetWait >= 0) // say something about it at this point (before I assign a default value)
{
fputs("Reset Arduino via Serial Port\n", stdout);
fflush(stdout);
}
if(!iResetWait) // i.e. 'use default'
{
iResetWait = DEFAULT_RESET_WAIT;
}
if(iResetWait > 0) // arduino reset using the RTS+DTR line
{
reset_arduino(*piFile);
fputs("Waiting for device reset to complete...", stdout);
fflush(stdout);
// wait several seconds for initialization
while(iResetWait > 1)
{
MySleep(1000);
fputs(".", stdout);
fflush(stdout);
iResetWait--;
}
MySleep(1000);
}
else if(iFlowControl > 0) // only if there's no 'reset wait' - they ARE mutually exclusive!
{
set_rts_dtr(*piFile, 1); // this simply sets the RTS+DTR line to a 'LOW' state [necessary for flow control]
}
if(!pszQuestion)
{
fputs("!\n", stdout);
fflush(stdout);
}
if(pszQuestion)
{
question_loop(*piFile, *piConsole);
}
else if(bRawFlag)
{
console_loop(*piFile, *piConsole);
}
#ifdef WITH_XMODEM
else if(bXModemFlag)
{
do_xmodem(*piFile, *piConsole);
}
#endif // WITH_XMODEM
else
{
// what I normally do
calibrate_loop(*piFile, *piConsole);
}
if(iResetWait >= 0 || iFlowControl > 0)
{
set_rts_dtr(*piFile, 0); // NOTE: avrdude does this, setting RTS and DTR _LOW_ at this point - should I?
// NOTE: this forces a reset next time someone opens the device, so...
}
exit_point:
#ifdef WIN32
CloseHandle(*piFile);
CloseHandle(*piConsole);
*piFile = *piConsole = INVALID_HANDLE_VALUE;
#else // WIN32
// TODO: restore default signal handlers? disable signal handlers?
signal(SIGINT, SIG_DFL); // SIG_IGN);
signal(SIGTSTP, SIG_DFL); // SIG_IGN);
signal(SIGTERM, SIG_DFL); // SIG_IGN);
signal(SIGUSR1, SIG_DFL); // SIG_IGN);
signal(SIGUSR2, SIG_DFL); // SIG_IGN);
#ifdef __FreeBSD__
flock(*piFile, LOCK_UN);
#endif // __FreeBSD__
close(*piFile); // I'm closing here [possible race condition?]
conrestore();
close(*piConsole);
*piFile = *piConsole = -1;
#endif // WIN32
return 0;
}
int do_options(int argc, char *argv[], char * envp[])
{
#ifndef HAVE_GETOPT /* basically, WIN32 */
int i1, optind;
for(optind=1; optind < argc; optind++)
{
if(argv[optind][0] != '-')
break;
if(argv[optind][1] == '-')
{
optind++;
break;
}
for(i1=1; argv[optind][i1]; i1++)
{
if(argv[optind][i1] == 'd')
{
bSerialDebug = 1;
}
else if(argv[optind][i1] == 'B')
{
if(argv[optind][i1 + 1])
{
#ifdef WIN32
strncpy_s(szBaud, sizeof(szBaud) - 1,
&(argv[optind][i1 + 1]), _TRUNCATE);
#else // WIN32
strncpy(szXBaud, &(argv[optind][i1 + 1]), sizeof(szBaud) - 1);
#endif // WIN32
}
else
{
optind++;
#ifdef WIN32
strncpy_s(szBaud, sizeof(szBaud) - 1,
argv[optind], _TRUNCATE);
#else // WIN32
strncpy(szBaud, argv[optind], sizeof(szBaud) - 1);
#endif // WIN32
}
szBaud[sizeof(szBaud) - 1] = 0;
break;
}
else if(argv[optind][i1] == 'N')
{
iResetWait = -1; // no reset wait
}
else if(argv[optind][i1] == 'F')
{
iFlowControl = 1;
iResetWait = -1; // implies no reset wait as well
}
else if(argv[optind][i1] == 'W')
{
// TODO: wait period, mutually exclusive with 'N'
}
else if(argv[optind][i1] == 'e')
{
bLocalEcho = 1;
}
else if(argv[optind][i1] == 'r')
{
if(
#ifdef WITH_XMODEM
bXModemFlag ||
#endif // WITH_XMODEM
bFactoryReset ||
pszQuestion != NULL)
{
usage();
return 1;
}
bRawFlag = 1;
}
else if(argv[optind][i1] == 'm')
{
if(
#ifdef WITH_XMODEM
bXModemFlag ||
#endif // WITH_XMODEM
bFactoryReset)
{
usage();
return 1;
}
bRawFlag = 1;
}
else if(argv[optind][i1] == 'c')
{
iTerminator = '\r';
}
else if(argv[optind][i1] == 'n')
{
iTerminator = '\n';
}
else if(argv[optind][i1] == 'R')
{
if(
#ifdef WITH_XMODEM
bXModemFlag ||
#endif // WITH_XMODEM
bRawFlag ||
pszQuestion != NULL)
{
usage();
return 1;
}
bFactoryReset = 1;
}
else if(argv[optind][i1] == 'h' ||
argv[optind][i1] == 'H' ||
argv[optind][i1] == '?')
{
usage();
return 1;
}
else if(argv[optind][i1] == 'v')
{
iVerbosity++;
}
#ifdef WITH_XMODEM
else if(argv[optind][i1] == 'X')
{
// next char must be S or R followed by the file name
// file name can ALSO be the next parameter
if(bRawFlag || bFactoryReset || pszQuestion != NULL ||
(argv[optind][i1 + 1] &&
argv[optind][i1 + 1] != 'R' &&
argv[optind][i1 + 1] != 'S') ||
(!argv[optind][i1 + 1] &&
((optind + 1) >= argc ||
(argv[optind+1][0] != 'R' &&
argv[optind+1][0] != 'S'))))
{
usage();
return 1;
}
if(argv[optind][i1 + 1])
{
#ifdef WIN32
strncpy_s(szXModemFile, sizeof(szXModemFile) - 1,
&(argv[optind][i1 + 1]), _TRUNCATE);
#else // WIN32
strncpy(szXModemFile, &(argv[optind][i1 + 1]), sizeof(szXModemFile) - 1);
#endif // WIN32
}
else
{
optind++;
#ifdef WIN32
strncpy_s(szXModemFile, sizeof(szXModemFile) - 1,
argv[optind], _TRUNCATE);
#else // WIN32
strncpy(szXModemFile, argv[optind], sizeof(szXModemFile) - 1);
#endif // WIN32
}
szXModemFile[sizeof(szXModemFile) - 1] = 0;
bXModemFlag = 1;
break;
}
#endif // WITH_XMODEM
else
{
fprintf(stderr, "Illegal or unrecognized option\n");
usage();
return 1;
}
}
}
#else // HAVE_GETOPT
int i1;
while((i1 = getopt(argc, argv,
"xhrmndeFRvNQW:l:B:c:q:w:"
#ifdef WITH_XMODEM
"X:"
#endif // WITH_XMODEM
)) != -1)
{
switch(i1)
{
case 'x':
// experimental option
iExperimental = 1;
break;
case 'd': // serial port debug
bSerialDebug = 1;
break;
case 'e': // local echo
bLocalEcho = 1;
break;
case 'r': // raw console access
if(
#ifdef WITH_XMODEM
bXModemFlag ||
#endif // WITH_XMODEM
bFactoryReset ||
pszQuestion != NULL)
{
usage();
return 1;
}
bRawFlag = 1;
break;
case 'm':
iTerminator = '\r';
break;
case 'n':
iTerminator = '\n';
break;
case 'R': // Factory Reset
if(
#ifdef WITH_XMODEM
bXModemFlag ||
#endif // WITH_XMODEM
pszQuestion != NULL ||
bRawFlag)
{
usage();
return 1;
}
bFactoryReset = 1;
break;
case 'v': // verbosity
iVerbosity++;
break;
case 'F': // flow control
iFlowControl = 1;
if(iResetWait > 0)
{
usage();
exit(1);
}
iResetWait = -1; // flow control implies reset wait also
break;
case 'N': // no 'reset wait'
if(iResetWait > 0)
{
usage();
exit(1);
}
iResetWait = -1;
break;
case 'W': // wait time
if(iResetWait < 0) // used '-N' option?
{
usage();
exit(1);
}
iResetWait = atoi(optarg);
if(iResetWait <= 0)
{
usage();
exit(1);
}
break;
case 'l': // 'listen' mode (TCP only, implies -c)
bListenMode = 1;
case 'c': // specify a console
// if(!optarg || *optarg == ':')
// {
// fputs("console not specified\n", stderr);
// usage();
// exit(1);
// }
pAltConsole = malloc(strlen(optarg) + 1);
strcpy(pAltConsole, optarg);
break;
case 'B': // specify a console
// if(!optarg || *optarg == ':')
// {
// fputs("BAUD string not specified\n", stderr);
// usage();
// exit(1);
// }
strncpy(szBaud, optarg, sizeof(szBaud));
break;
#ifdef WITH_XMODEM
case 'X': // xmodem transfer
// next char must be S or R followed by the file name
// file name can ALSO be the next parameter
if(bRawFlag || bFactoryReset || pszQuestion != NULL ||
!optarg || !*optarg ||
(optarg[0] != 'R' && optarg[0] != 'S'))
{
usage();
return 1;
}
strncpy(szXModemFile, optarg, sizeof(szXModemFile) - 1);
szXModemFile[sizeof(szXModemFile) - 1] = 0;
bXModemFlag = 1;
break;
#endif // WITH_XMODEM
case 'Q': // quiet mode
bQuietFlag = 1;
break;
case 'q':
if(bRawFlag || bFactoryReset
#ifdef WITH_XMODEM
|| bXModemFlag
#endif // WITH_XMODEM
|| !optarg || !*optarg)
{
usage();
return 1;
}
// will send 'optarg' using specified line endings
pszQuestion = malloc(strlen(optarg) + 2);
if(!pszQuestion)
{
fprintf(stderr, "Unable to allocate memory for arg!\n");
return 1;
}
memcpy(pszQuestion, optarg, strlen(optarg) + 1);
iResetWait = -1; // no reset wait implied
break;
case 'w': // wait time
iQuestionWait = atoi(optarg);
if(iQuestionWait <= 0)
{
usage();
exit(1);
}
break;
default:
fprintf(stderr, "Illegal or unrecognized option\n");
case 'h':
case '?':
return 1;
}
}
#endif // HAVE_GETOPT
argc -= optind;
argv += optind;
if(argc > 0) // only one extra option allowed at this time
{
pIn = argv[0];
argc--;
argv++;
}
if(argc < 0)
{
return -1;
}
return 0;
}
#define TTYCONFIG_PARSE(X,Y) if(!(Y = strchr(X, ','))) { Y = (X) + strlen(X) + 1; } else { *((Y)++) = 0; }
static void ttyconfigSTR(HANDLE iFile, char *szBaud)
{
int iBaud=9600, iParity=0, iBits=8, iStop=1;
char *p1, *p2;
p1 = szBaud;
if(*p1)
{
TTYCONFIG_PARSE(p1,p2);
if(*p1)
{
iBaud = atoi(p1);
if(iBaud <= 0)
{
iBaud = 9600;
}
}
p1 = p2;
}
if(*p1)
{
TTYCONFIG_PARSE(p1,p2);
if(*p1)
{
if(*p1 == 'n' || *p1 == 'N')
{
iParity = 0;
}
else if(*p1 == 'e' || *p1 == 'E')
{
iParity = -1;
}
else if(*p1 == 'o' || *p1 == 'O')
{
iParity = 1;
}
else
{
iParity = 0;
}
}
p1 = p2;
}
if(*p1)
{
TTYCONFIG_PARSE(p1,p2);
if(*p1)
{
iBits = atoi(p1);
if(iBits < 6 || iBits > 8)
{
iBits = 8;
}
}
p1 = p2;
}
if(*p1)
{
TTYCONFIG_PARSE(p1,p2);
if(*p1)
{
iStop = atoi(p1);
if(iStop < 1 || iStop > 2)
{
iStop = 1; // do NOT handle 1.5
}
}
p1 = p2;
}
if(!bQuietFlag)
{
fprintf(stderr, "Setting serial config to %d,%c,%d,%d\n",
iBaud, (char)(iParity ? (iParity > 0 ? 'o' : 'e') : 'n'), iBits, iStop);
}
ttyconfig(iFile, iBaud, iParity, iBits, iStop);
}