forked from placidorevilla/rtkgps
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rtkgps.c
1331 lines (1179 loc) · 39.8 KB
/
rtkgps.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
/******************************************************************************
Copyright © 2008-2010 Brendt Wohlberg
This program is free software; you can redistribute it and/or modify
it under the terms of version 2 of the GNU General Public License at
http://www.gnu.org/licenses/gpl-2.0.txt.
This program is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
Most recent modification: 4 February 2012
******************************************************************************/
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/ioctl.h>
#include <unistd.h>
#include <string.h>
#include <ctype.h>
#include <getopt.h>
#include <errno.h>
#include <assert.h>
#include "serial.h"
#include "rtkcom.h"
#include "gpsfmt.h"
typedef struct {
unsigned char vflg;
unsigned char yflg;
unsigned char nflg;
unsigned char pflg;
unsigned char eflg;
unsigned char uflg;
char *devs;
char *spds;
char *btas;
char *lgts;
char *mfos;
char *cfls;
char *snts;
char *dsts;
char *flns;
char *cmds;
short int sint;
short int fnmn;
short int fnmx;
unsigned int sspd; /* serial line speed */
char usgs[1500];
} cmdlnopts_t;
int prgbrfp = 0;
void scan_cmdline(int argc, char* argv[], cmdlnopts_t *cmdopt);
void cmd_status(cmdlnopts_t *cmdopt);
void cmd_date(cmdlnopts_t *cmdopt);
void cmd_list(cmdlnopts_t *cmdopt);
void cmd_set(cmdlnopts_t *cmdopt);
void cmd_read(cmdlnopts_t *cmdopt);
void cmd_erase(cmdlnopts_t *cmdopt);
void get_data_progress(unsigned short nfxt, unsigned short nfxc);
void text_progress_bar(float frac, const char *prfs);
void warning(const char *wrn, int line, const char *file);
int is_directory(const char *path);
int file_backup(const char *path);
int coms_open(cmdlnopts_t *cmdopt);
void coms_close(int fd, const cmdlnopts_t *cmdopt);
void gpsmouse_disable(int fd, int md, const cmdlnopts_t *cmdopt);
void gpsmouse_enable(int fd, int md, const cmdlnopts_t *cmdopt);
void outlog_disable(int fd, const cmdlnopts_t *cmdopt);
void outlog_enable(int fd, int md, const cmdlnopts_t *cmdopt);
void status_read(int fd, status_t *status, const cmdlnopts_t *cmdopt);
void file_read(int fd, short int flnm, char *fnam, FILE *strm,
const geoid_height_t *gdhtp, const status_t* status,
const cmdlnopts_t *cmdopt);
/*****************************************************************************
Main program.
*****************************************************************************/
int main (int argc, char* argv[]) {
const char* usage0 =
"usage: rtkgps [-h] [-v] [-d <dev> [-r <rate>] | -b <addr>]\n"
" ([-e] status | date | list | [-y] erase |\n"
" [-c <flg>] [-l <lgtp>] [-m <mfo>] [-s <int>] set |\n"
" [-n] [-p] [-o <dest> [-u]] [-f <nstr>] read)\n\n"
" -h display usage\n"
" -v verbose mode\n"
" -d <dev> specify serial device\n"
" -r <rate> specify baud rate for serial device\n"
" -b <addr> specify bluetooth address\n"
" -e display extended status information\n";
const char* usage1 =
" -c <flg> set real-time output (GPS mouse) mode "
"(0=disable, 1=enable)\n"
" -l <lgtp> set log record type (tl, tla, or tlav)\n"
" -m <mfo> set memory overwrite behaviour (o=overwrite, s=stop)\n"
" -s <int> set sampling interval in seconds\n"
" -n output data in simple native text form\n"
" -p display text progress bar\n"
" -o <dest> specify destination file or directory\n"
" -u skip downloading date for existing files\n";
const char* usage2 =
" -f <nstr> string specifying index number(s) of log file(s) \n"
" to retrieve as a single file number, or range of \n"
" file numbers in the format -n, n-, or n-m\n"
" -y don't ask for confirmation\n";
/* most Royalteks operate on 57600 baud, use that as the default */
cmdlnopts_t cmdopt = {0,0,0,0,0,0,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,
NULL,NULL,-1,-1,-1,57600,""};
/* Initialise usage string */
strcpy(cmdopt.usgs, usage0);
strcat(cmdopt.usgs, usage1);
strcat(cmdopt.usgs, usage2);
/* Scan command line options */
scan_cmdline(argc, argv, &cmdopt);
/* Set up warning callback function */
gcwrnfp = warning;
/* Perform requested task */
if (strcmp(cmdopt.cmds,"status") == 0) {
cmd_status(&cmdopt);
} else if (strcmp(cmdopt.cmds,"date") == 0) {
cmd_date(&cmdopt);
} else if (strcmp(cmdopt.cmds,"list") == 0) {
cmd_list(&cmdopt);
} else if (strcmp(cmdopt.cmds,"set") == 0) {
cmd_set(&cmdopt);
} else if (strcmp(cmdopt.cmds,"read") == 0) {
cmd_read(&cmdopt);
} else if (strcmp(cmdopt.cmds,"erase") == 0) {
cmd_erase(&cmdopt);
}
exit(0);
}
/*****************************************************************************
Scan command line arguments and check for valid choices.
*****************************************************************************/
void scan_cmdline(int argc, char* argv[], cmdlnopts_t *cmdopt) {
int n;
/* Scan command line options */
opterr = 0;
while ((n = getopt (argc, argv, "hved:r:b:l:m:c:s:npo:uf:y")) != -1)
switch (n) {
case 'h': fprintf(stderr, "%s", cmdopt->usgs);
exit(0);
case '?': fprintf(stderr, "rtkgps: Unknown command line flag\n");
fprintf(stderr, "%s", cmdopt->usgs);
exit(1);
case 'v': cmdopt->vflg = 1;
break;
case 'e': cmdopt->eflg = 1;
break;
case 'd': cmdopt->devs = optarg;
break;
case 'r': cmdopt->spds = optarg;
break;
case 'b': cmdopt->btas = optarg;
break;
case 'l': cmdopt->lgts = optarg;
break;
case 'm': cmdopt->mfos = optarg;
break;
case 'c': cmdopt->cfls = optarg;
break;
case 's': cmdopt->snts = optarg;
break;
case 'n': cmdopt->nflg = 1;
break;
case 'p': cmdopt->pflg = 1;
break;
case 'o': cmdopt->dsts = optarg;
break;
case 'u': cmdopt->uflg = 1;
break;
case 'f': cmdopt->flns = optarg;
break;
case 'y': cmdopt->yflg = 1;
break;
default:
exit(1);
}
/* Check command line options */
if (cmdopt->devs != NULL && cmdopt->btas != NULL) {
fprintf(stderr, "%s", cmdopt->usgs);
exit(1);
}
#if !ENABLE_LINUX_BT-0
if (cmdopt->btas != NULL) {
fprintf(stderr, "rtkgps: Bluetooth support disabled\n");
exit(1);
}
/* bluetooth not enabled and Serial device name not provided */
if (cmdopt->devs == NULL) {
fprintf(stderr, "%s", cmdopt->usgs);
exit(1);
}
#endif /* ENABLE_LINUX_BT */
/* baud rate is illegal for bluetooth device */
if (cmdopt->spds != NULL && cmdopt->btas != NULL) {
fprintf(stderr, "%s", cmdopt->usgs);
exit(1);
}
if (cmdopt->spds != NULL) {
int baudi;
if(sscanf(cmdopt->spds, "%d", &baudi) != 1 || baudi < 0) {
fprintf(stderr, "rtkgps: Flag -r may only take unsigned "
"integer values\n");
exit(1);
}
if( !dev_speed_valid(baudi)){
fprintf(stderr, "rtkgps: Unsupported baud rate: %d\n", baudi);
exit(1);
}
cmdopt->sspd = baudi;
}
if (optind < argc)
cmdopt->cmds = argv[optind++];
else {
fprintf(stderr, "%s", cmdopt->usgs);
exit(1);
}
if ((strcmp(cmdopt->cmds,"status") != 0 &&
strcmp(cmdopt->cmds,"date") != 0 &&
strcmp(cmdopt->cmds,"list") != 0 &&
strcmp(cmdopt->cmds,"read") != 0 &&
strcmp(cmdopt->cmds,"set") != 0 &&
strcmp(cmdopt->cmds,"erase") != 0) ||
(strcmp(cmdopt->cmds,"status") != 0 && cmdopt->eflg) ||
(strcmp(cmdopt->cmds,"set") != 0 && cmdopt->cfls) ||
(strcmp(cmdopt->cmds,"set") != 0 && cmdopt->lgts) ||
(strcmp(cmdopt->cmds,"set") != 0 && cmdopt->mfos) ||
(strcmp(cmdopt->cmds,"set") != 0 && cmdopt->snts) ||
(strcmp(cmdopt->cmds,"read") != 0 && cmdopt->dsts != NULL) ||
(strcmp(cmdopt->cmds,"read") != 0 && cmdopt->nflg) ||
(strcmp(cmdopt->cmds,"read") != 0 && cmdopt->pflg) ||
(strcmp(cmdopt->cmds,"read") != 0 && cmdopt->uflg) ||
(strcmp(cmdopt->cmds,"read") != 0 && cmdopt->dsts) ||
(strcmp(cmdopt->cmds,"read") != 0 && cmdopt->flns) ||
(strcmp(cmdopt->cmds,"erase") != 0 && cmdopt->yflg)) {
fprintf(stderr, "%s", cmdopt->usgs);
exit(1);
}
if (strcmp(cmdopt->cmds,"set") == 0) {
if (cmdopt->lgts == NULL && cmdopt->mfos == NULL &&
cmdopt->cfls == NULL && cmdopt->snts == NULL) {
fprintf(stderr, "rtkgps: Must specify at least one parameter flag "
" for set command\n");
exit(1);
}
if (cmdopt->lgts != NULL && strcmp(cmdopt->lgts,"tl") != 0 &&
strcmp(cmdopt->lgts,"tla") != 0 &&
strcmp(cmdopt->lgts,"tlav") != 0) {
fprintf(stderr, "rtkgps: Flag -l for set command may only take values"
"\"tl\", \"tla\", or \"tlav\"\n");
exit(1);
}
if (cmdopt->mfos != NULL && strcmp(cmdopt->mfos,"o") != 0 &&
strcmp(cmdopt->mfos,"s") != 0) {
fprintf(stderr, "rtkgps: Flag -m for set command may only take values"
"\"o\" or \"s\"\n");
exit(1);
}
if (cmdopt->cfls != NULL && strcmp(cmdopt->cfls,"0") != 0 &&
strcmp(cmdopt->cfls,"1") != 0) {
fprintf(stderr, "rtkgps: Flag -c for set command may only take values"
"\"0\" or \"1\"\n");
exit(1);
}
if (cmdopt->snts != NULL &&
(sscanf(cmdopt->snts, "%hd", &cmdopt->sint) != 1 ||
cmdopt->sint < 1 || cmdopt->sint > 60)) {
fprintf(stderr, "rtkgps: Flag -c for set command may only take "
"integer values between 1 and 60\n");
exit(1);
}
}
if (cmdopt->flns != NULL) {
unsigned char strvld = 0;
if (strchr(cmdopt->flns, '-')) {
if (cmdopt->flns[0] == '-') {
if (sscanf(cmdopt->flns, "-%hd", &cmdopt->fnmx) == 1)
strvld = 1;
} else if (cmdopt->flns[strlen(cmdopt->flns)-1] == '-') {
if (sscanf(cmdopt->flns, "%hd-", &cmdopt->fnmn) == 1)
strvld = 1;
} else {
if (sscanf(cmdopt->flns, "%hd-%hd", &cmdopt->fnmn, &cmdopt->fnmx) == 2)
strvld = 1;
}
} else {
if (sscanf(cmdopt->flns, "%hd", &cmdopt->fnmn) == 1) {
cmdopt->fnmx = cmdopt->fnmn;
strvld = 1;
}
}
if (cmdopt->fnmn != -1 && cmdopt->fnmn < 0)
strvld = 0;
if (cmdopt->fnmn != -1 && cmdopt->fnmx != -1 &&
cmdopt->fnmn > cmdopt->fnmx)
strvld = 0;
if (!strvld) {
fprintf(stderr, "rtkgps: Flag -f for read command has invalid "
"file number specification string\n");
exit(1);
}
}
if (strcmp(cmdopt->cmds,"erase") == 0 &&
cmdopt->devs == NULL && cmdopt->btas == NULL) {
fprintf(stderr, "rtkgps: Must specify explicit device or address for "
" erase command\n");
exit(1);
}
}
/*****************************************************************************
Perform rtkgps status command.
*****************************************************************************/
void cmd_status(cmdlnopts_t *cmdopt) {
int fd;
unsigned int mu = 0;
status_t status;
logfile_t lgfl;
log_bndry_t lgbd;
memory_t mem;
firmware_t frm;
fd = coms_open(cmdopt);
if (cmdopt->vflg)
printf("Requesting logger status information\n");
status_read(fd, &status, cmdopt);
if (cmdopt->eflg) {
if (cmdopt->vflg)
printf("Requesting extended logger information\n");
gpsmouse_disable(fd, status.gpsms, cmdopt);
if (get_log_bndry(fd, &lgbd) < 0) {
fprintf(stderr,"rtkgps: Failed to read log start/end details [%s]\n",
gcstrerror(rcerrno));
gpsmouse_enable(fd, status.gpsms, cmdopt);
coms_close(fd, cmdopt);
exit(5);
}
if (get_memory_info(fd, &mem) < 0) {
fprintf(stderr,"rtkgps: Failed to read logger memory details [%s]\n",
gcstrerror(rcerrno));
gpsmouse_enable(fd, status.gpsms, cmdopt);
coms_close(fd, cmdopt);
exit(5);
}
if (get_firmware_info(fd, &frm) < 0) {
fprintf(stderr,"rtkgps: Failed to read logger firmware details [%s]\n",
gcstrerror(rcerrno));
gpsmouse_enable(fd, status.gpsms, cmdopt);
coms_close(fd, cmdopt);
exit(5);
}
/* Get info for first logfile */
if (get_file_info(fd, 0, &lgfl) < 0) {
fprintf(stderr,"rtkgps: Error reading information for file %d [%s]\n",
0, gcstrerror(rcerrno));
gpsmouse_enable(fd, status.gpsms, cmdopt);
coms_close(fd, cmdopt);
exit(5);
}
if (lgfl.memp == 0) { /* Memory has not wrapped around in overwrite mode */
/* Get info for last logfile */
if (get_file_info(fd, status.nfile-1, &lgfl) < 0) {
fprintf(stderr,"rtkgps: Error reading information for file %d [%s]\n",
status.nfile-1, gcstrerror(rcerrno));
gpsmouse_enable(fd, status.gpsms, cmdopt);
coms_close(fd, cmdopt);
exit(5);
}
/* Memory used computed from last logfile pointer plus number of
fixes in active logfile */
mu = lgfl.memp + status.nfix*fix_size(status.fxtyp);
} else { /* Memory has wrapped around in overwrite mode */
int n;
/* Need to compute memory used by adding up number of fixes in each
log file */
mu = lgfl.nfix*fix_size(lgfl.fxtyp);
for (n = 1; n < status.nfile; n++) {
if (get_file_info(fd, n, &lgfl) < 0) {
fprintf(stderr,"rtkgps: Error reading information for file "
"%d [%s]\n", n, gcstrerror(rcerrno));
gpsmouse_enable(fd, status.gpsms, cmdopt);
coms_close(fd, cmdopt);
exit(5);
}
mu += lgfl.nfix*fix_size(lgfl.fxtyp);
}
}
gpsmouse_enable(fd, status.gpsms, cmdopt);
}
printf("GPS Fix: %s\nGPS mouse mode: %s\n"
"Record type: %s\nMemory full: %s\n"
"Sampling interval: %hds\nNumber of files: %hd\n"
"Fixes in last file: %d\n", gpsrx_string(status.gpsrx),
gpsms_string(status.gpsms), fxtyp_string(status.fxtyp),
mfowm_string(status.mfowm), status.sntvl, status.nfile,
status.nfix
);
if (cmdopt->eflg) {
printf("First log fix: %.4s-%.2s-%.2s %.2s:%.2s:%.2s\n"
"Last log fix: %.4s-%.2s-%.2s %.2s:%.2s:%.2s\n",
lgbd.first.date,lgbd.first.date+4,lgbd.first.date+6,
lgbd.first.time, lgbd.first.time+2, lgbd.first.time+4,
lgbd.last.date,lgbd.last.date+4,lgbd.last.date+6,
lgbd.last.time, lgbd.last.time+2, lgbd.last.time+4);
printf("Device memory: %7.2fkb\n"
"Memory used: %7.2fkb (%.2f%%)\n",
mem.nbytes / 1024.0, mu / 1024.4, ((float)mu / mem.nbytes)*100.0);
printf("Version: %s\n"
"Firmware: %s\n", frm.vrsnr, frm.frmwr);
}
coms_close(fd, cmdopt);
}
/*****************************************************************************
Perform rtkgps date command.
*****************************************************************************/
void cmd_date(cmdlnopts_t *cmdopt) {
int fd;
date_time_t dttm;
fd = coms_open(cmdopt);
if (cmdopt->vflg)
printf("Determining current date/time information\n");
if (get_current_utc(fd, &dttm) < 0) {
fprintf(stderr,"rtkgps: Failed to determine current date/time [%s]\n",
gcstrerror(rcerrno));
coms_close(fd, cmdopt);
exit(5);
}
printf("%.4s-%.2s-%.2s %.2s:%.2s:%.2s\n", dttm.date,dttm.date+4,
dttm.date+6,dttm.time, dttm.time+2, dttm.time+4);
coms_close(fd, cmdopt);
}
/*****************************************************************************
Perform rtkgps list command.
*****************************************************************************/
void cmd_list(cmdlnopts_t *cmdopt) {
int fd;
status_t status;
logfile_t *lgflp;
/*unsigned int mem = 0;*/
int n;
fd = coms_open(cmdopt);
if (cmdopt->vflg)
printf("Requesting logger status information\n");
status_read(fd, &status, cmdopt);
gpsmouse_disable(fd, status.gpsms, cmdopt);
if ((lgflp = malloc(status.nfile*sizeof(logfile_t))) == NULL) {
fprintf(stderr,"rtkgps: Error allocating memory\n");
gpsmouse_enable(fd, status.gpsms, cmdopt);
coms_close(fd, cmdopt);
exit(2);
}
for (n = 0; n < status.nfile; n++) {
if (cmdopt->vflg) {
printf("Requesting metadata for file %4d\n", n);
}
if (get_file_info(fd, n, lgflp+n) < 0) {
fprintf(stderr,"rtkgps: Error reading information for file %d [%s]\n",
n, gcstrerror(rcerrno));
free(lgflp);
gpsmouse_enable(fd, status.gpsms, cmdopt);
coms_close(fd, cmdopt);
exit(5);
}
}
printf("File num Date Fix type Num fix Mem ptr\n");
for (n = 0; n < status.nfile; n++)
printf("%8d %8s %8hd %7d %7d\n", n, lgflp[n].date, lgflp[n].fxtyp,
lgflp[n].nfix, lgflp[n].memp);
free(lgflp);
gpsmouse_enable(fd, status.gpsms, cmdopt);
coms_close(fd, cmdopt);
}
/*****************************************************************************
Perform rtkgps set command.
*****************************************************************************/
void cmd_set(cmdlnopts_t *cmdopt) {
int fd;
status_t status;
unsigned char cflg = 0, fxtp = 0, mfow = 0, gpsm;
fd = coms_open(cmdopt);
if (cmdopt->vflg) {
printf("Requesting logger status information\n");
}
status_read(fd, &status, cmdopt);
gpsm = status.gpsms;
if (cmdopt->cfls != NULL) {
if (strcmp(cmdopt->cfls,"0") == 0)
gpsm = 0;
else if (strcmp(cmdopt->cfls,"1") == 0)
gpsm = 1;
}
if (cmdopt->lgts != NULL) {
if (strcmp(cmdopt->lgts,"tl") == 0)
fxtp = 0;
else if (strcmp(cmdopt->lgts,"tla") == 0)
fxtp = 1;
else if (strcmp(cmdopt->lgts,"tlav") == 0)
fxtp = 2;
if (fxtp != status.fxtyp) {
status.fxtyp = fxtp;
cflg = 1;
}
}
if (cmdopt->mfos != NULL) {
if (strcmp(cmdopt->mfos,"o") == 0)
mfow = 0;
else if (strcmp(cmdopt->mfos,"s") == 0)
mfow = 1;
if (mfow != status.mfowm) {
status.mfowm = mfow;
cflg = 1;
}
}
if (cmdopt->snts != NULL && status.sntvl != cmdopt->sint) {
status.sntvl = cmdopt->sint;
cflg = 1;
}
if (cflg == 1) {
gpsmouse_disable(fd, status.gpsms, cmdopt);
if (cmdopt->vflg) {
printf("Setting new logger parameters\n");
}
if (set_status(fd, &status) < 0) {
fprintf(stderr,"rtkgps: Failed to set device status [%s]\n",
gcstrerror(rcerrno));
gpsmouse_enable(fd, gpsm, cmdopt);
coms_close(fd, cmdopt);
exit(5);
}
gpsmouse_enable(fd, gpsm, cmdopt);
} else {
if (gpsm && !status.gpsms)
gpsmouse_enable(fd, gpsm, cmdopt);
else if (!gpsm && status.gpsms)
gpsmouse_disable(fd, status.gpsms, cmdopt);
}
coms_close(fd, cmdopt);
}
/*****************************************************************************
Perform rtkgps read command.
*****************************************************************************/
void cmd_read(cmdlnopts_t *cmdopt) {
int fd;
status_t status;
geoid_height_t gdht = {0,0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,NULL,NULL};
FILE *strm = NULL;
char *fnam = NULL;
short int n;
#ifdef GEOIDCOR
/* Set up geoid correction data structure */
if (geoid_calc_open(GGRDPATH, &gdht) == -1) {
fprintf(stderr, "rtkgps: Warning: could not access geoid correction "
"data\n");
}
#endif
/* Open communication with logger */
fd = coms_open(cmdopt);
/* Set up progress bar if requested */
if (cmdopt->pflg) {
#ifdef HAVE_TIOCGWINSZ
struct winsize ws;
if (ioctl(1, TIOCGWINSZ, (void *)&ws) == 0)
prgbrfp = 1;
else if (ioctl(2, TIOCGWINSZ, (void *)&ws) == 0)
prgbrfp = 2;
#else
prgbrfp = 1;
#endif
if (prgbrfp)
gdpfp = get_data_progress;
}
if (cmdopt->vflg)
printf("Requesting logger status information\n");
/* Read logger status */
status_read(fd, &status, cmdopt);
/* Handle unspecified ends of file number range */
if (cmdopt->fnmn == -1)
cmdopt->fnmn = 0;
if (cmdopt->fnmx == -1)
cmdopt->fnmx = status.nfile-1;
/* Check for minimum file number out of range */
if (cmdopt->fnmn >= status.nfile) {
fprintf(stderr,"rtkgps: Requested file number(s) all invalid\n");
coms_close(fd, cmdopt);
exit(1);
}
/* Check for maximum file number out of range */
if (cmdopt->fnmx >= status.nfile) {
cmdopt->fnmx = status.nfile-1;
fprintf(stderr, "rtkgps: Warning: reduced maximum requested file "
"number to valid range\n");
}
outlog_disable(fd, cmdopt);
/* The complicated handling of output files, split between this function
and file_read, is due to the following output policy:
• If no output file is specified, selected logfiles are written
to stdout
• If the specified output file is a regular file, all selected logfiles
are written to that file
• If the specified output file is a directory, each selected logfile is
written to a unique file within that directory
*/
if (cmdopt->dsts == NULL)
strm = stdout; /* If output flag not specified, output stream is stdout */
else {
/* If output flag is specified, determine whether it is a directory */
if (is_directory(cmdopt->dsts)) {
/* If specified output path is a directory, allocate memory for
constructing the full output filename */
if ((fnam = malloc(strlen(cmdopt->dsts) + 32)) == NULL) {
fprintf(stderr,"rtkgps: Error allocating memory\n");
outlog_enable(fd, status.gpsms, cmdopt);
coms_close(fd, cmdopt);
exit(2);
}
} else {
/* If specified output path is not a directory, open the file
for writing after creating a backup if the file already exists. */
if (file_backup(cmdopt->dsts) != 0) {
fprintf(stderr,"rtkgps: Error creating backup of file %s\n",
cmdopt->dsts);
outlog_enable(fd, status.gpsms, cmdopt);
coms_close(fd, cmdopt);
exit(3);
}
if ((strm = fopen(cmdopt->dsts, "w")) == NULL) {
fprintf(stderr,"rtkgps: Error opening output file %s [%s]\n",
cmdopt->dsts, gcstrerror(rcerrno));
outlog_enable(fd, status.gpsms, cmdopt);
coms_close(fd, cmdopt);
exit(3);
}
}
}
if (strm != NULL) {
/* Write output file header */
if (cmdopt->nflg)
print_hdr_native(strm);
else
print_hdr_nmea(strm, cmdopt->btas);
}
/* Reset warning function message records */
warning(NULL, 0, NULL);
/* Read requested range of log files */
for (n = cmdopt->fnmn; n <= cmdopt->fnmx; n++) {
char nstr[8];
/* If progress bar requested and verbose output not enabled, set
up prefix displaying file number */
if (cmdopt->pflg && !cmdopt->vflg) {
sprintf(nstr, "%4d ", n);
text_progress_bar(0.0, nstr);
}
file_read(fd, n, fnam, strm, &gdht, &status, cmdopt);
/* Reset warning function message records */
warning(NULL, 0, NULL);
}
/* Free memory allocated for file name */
free(fnam);
outlog_enable(fd, status.gpsms, cmdopt);
/* Close ommunication with logger */
coms_close(fd, cmdopt);
#ifdef GEOIDCOR
/* Destroy geoid correction data structure */
geoid_calc_close(&gdht);
#endif
}
/*****************************************************************************
Perform rtkgps erase command.
*****************************************************************************/
void cmd_erase(cmdlnopts_t *cmdopt) {
int fd;
int ce;
ce = cmdopt->yflg;
if (!ce) {
printf("Confirm complete memory erase [y/N]\n");
if (tolower(getchar()) == 'y')
ce = 1;
}
if (ce) {
fd = coms_open(cmdopt);
if (cmdopt->vflg) {
printf("Erasing memory\n");
}
if (set_memory_erase(fd) < 0) {
fprintf(stderr,"rtkgps: Memory erase command not confirmed [%s]\n",
gcstrerror(rcerrno));
coms_close(fd, cmdopt);
exit(5);
}
coms_close(fd, cmdopt);
} else if (cmdopt->vflg)
printf("Erase operation aborted\n");
}
/*****************************************************************************
Get data progress callback function.
*****************************************************************************/
void get_data_progress(unsigned short nfxt, unsigned short nfxc) {
text_progress_bar((float)nfxc/(float)nfxt, NULL);
if (nfxc == nfxt)
fprintf(stderr,"\n");
}
/*****************************************************************************
Display text progress bar.
*****************************************************************************/
void text_progress_bar(float frac, const char *prfs) {
static const char *prfx = "";
static unsigned short nbpv = 0;
static unsigned short ncol = 0;
static unsigned short bmax = 0;
static char bar[] = "================================================="
"==================================================================="
"===================================================================";
static char spc[] = " "
" "
" "
" ";
if (prfs == NULL && prgbrfp) {
FILE *fp = NULL;
unsigned short nbar;
/* Set output file pointer based on file descriptor in prgbrfp */
if (prgbrfp == 1)
fp = stdout;
else if (prgbrfp == 2)
fp = stderr;
else
return;
#ifdef HAVE_TIOCGWINSZ
{
struct winsize ws;
if (ioctl(prgbrfp, TIOCGWINSZ, (void *)&ws) == 0)
ncol = ws.ws_col;
else
return;
}
#else
ncol = 40;
#endif
if (frac < 0) {
/* Print spaces to clear current progress bar line */
fprintf(fp, "%.*s\r", (int)((ncol < strlen(spc))?ncol:strlen(spc)), spc);
fflush(fp);
} else {
/* Compute length of current progress bar */
bmax = ncol - strlen(prfx) - 7;
if (bmax > strlen(bar))
bmax = strlen(bar);
nbar = (unsigned short)floor(frac*bmax);
if (nbar > bmax)
nbar = bmax;
/* If progress bar length has changed, print new line and update
previous bar length */
if (nbar != nbpv) {
nbpv = nbar;
fprintf(fp, "%s|%.*s%.*s| %3.0f%%\r", prfx, nbar, bar, bmax-nbar,
spc, 100.0*frac);
fflush(fp);
}
}
} else
prfx = prfs;
}
/*****************************************************************************
Warning display function.
*****************************************************************************/
#if defined(__GNUC__) && !defined(DEBUG)
void warning(const char *wrn, int line __attribute__((unused)),
const char *file __attribute__((unused))) {
#else
void warning(const char *wrn, int line, const char *file) {
#endif
static char wmsg[20][256];
static uint8_t nmsg = 0;
if (wrn == NULL) {
/* Reset warning message records if function called with null string */
nmsg = 0;
} else {
/* Record and print non-null string */
uint8_t msn = 0;
uint8_t n = 0;
#ifdef DEBUG
/* When debugging, print every message */
msn = 1;
#else
/* Determine whether error message has already been encountered */
for (n = 0; n < nmsg; n++) {
if (strncmp(wrn, wmsg[n], 255) == 0) {
msn = 1;
break;
}
}
#endif
if (!msn) {
/* Current error message has not already been encountered */
#ifndef DEBUG
/* When debugging, no need to record messages */
if (nmsg < 20) {
strncpy(wmsg[n], wrn, 255);
wmsg[n][255] = '\0';
nmsg++;
}
#endif
/* If progress bar file descriptor is not zero, call progress bar
function with request to clear current progress bar line. */
if (prgbrfp)
text_progress_bar(-1.0, NULL);
/* Print warning */
fprintf(stderr, "rtkgps: Warning: ");
fprintf(stderr, "%s ", wrn);
#ifdef DEBUG
fprintf(stderr, "at line %d in file %s", line, file);
#endif
fprintf(stderr, "\n");
}
}
}
/*****************************************************************************
Determine whether the file path is a dictionary.
*****************************************************************************/
int is_directory(const char *path) {
struct stat pstat;
if (stat(path, &pstat) < 0)
return 0; /* If file doesn't exist, it isn't a directory */
return S_ISDIR(pstat.st_mode);
}
/*****************************************************************************
Determine whether the file path is a non-zero size regular file
*****************************************************************************/
int is_nzsregfile(const char *path) {
struct stat pstat;
if (stat(path, &pstat) < 0)
return 0; /* If file doesn't exist, it isn't a regular file */
return S_ISREG(pstat.st_mode) && (pstat.st_size > 0);
}
/*****************************************************************************
If the specified file exists, rename it to have a .bak extension.
*****************************************************************************/
int file_backup(const char *path) {
char bpath[512];
if (strlen(path) > 500)
return -3;
if (is_directory(path))
return -2;
if (access(path, F_OK) == 0) {
sprintf(bpath, "%s.bak", path);
return rename(path, bpath);
} else
return 0;
}
/*****************************************************************************
Open communications with GPS device.
*****************************************************************************/
int coms_open(cmdlnopts_t *cmdopt) {
int fd = -1;
/* Open communication with device */
if (cmdopt->devs != NULL) {
/* Serial device name is provided */
fd = dev_open(cmdopt->devs);
if (fd < 0) {
fprintf(stderr, "rtkgps: Error opening device %s [%s]\n", cmdopt->devs,
strerror(errno));
exit(4);
}
if (dev_config_serial(fd, cmdopt->sspd) < 0) {
fprintf(stderr, "rtkgps: Error setting device speed to %u [%s]\n",
cmdopt->sspd, strerror(errno));
exit(4);
}
/* Display connection message in verbose mode */
if (cmdopt->vflg) {
printf("Opened device %s at %u baud\n", cmdopt->devs, cmdopt->sspd);
}
} else {
#if ENABLE_LINUX_BT-0
static char btscs[24];
/* Serial device name not provide: scan or use provide bluetooth address */
if (cmdopt->btas == NULL) {
/* Bluetooth address not specified: do a scan for a matching device */
bt_device_t btd[32];
int nbtd, nbgp = -1, n;
/* Perform scan */
nbtd = bt_scan(btd, 32);
if (nbtd < 0) {