File: main.c

package info (click to toggle)
gpstrans 0.34-6
  • links: PTS
  • area: main
  • in suites: potato
  • size: 476 kB
  • ctags: 348
  • sloc: ansic: 3,463; makefile: 97
file content (366 lines) | stat: -rw-r--r-- 12,407 bytes parent folder | download | duplicates (2)
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
/****************************************************************************/
/*                                                                          */
/* ./ascii/main.c   -   Main program and small utility procedures           */
/*                                                                          */
/* This file is part of gpstrans - a program to communicate with garmin gps */
/*                                                                          */
/*                                                                          */
/*    Copyright (c) 1995 by Carsten Tschach (tschach@zedat.fu-berlin.de)    */
/*                                                                          */
/* Permission  to use, copy,  modify, and distribute  this software and its */
/* documentation for non-commercial purpose, is hereby granted without fee, */
/* providing that the  copyright notice  appear in all copies and that both */
/* the  copyright notice and  this permission  notice appear in  supporting */
/* documentation.  I make no representations about  the suitability of this */
/* software  for any  purpose.  It is  provides "as is" without  express or */
/* implied warranty.                                                        */
/*                                                                          */
/****************************************************************************/
#include "defs.h"
#include "Garmin.h"
#include "Prefs.h"
#include <signal.h>
#include <sys/time.h>


/* define external variables */
extern struct PREFS gPrefs;                     /* Preferences from prefs.c */
extern int mayko_xmap_format;

/* define global variables */
char  cmd = ' ', what = ' ';
char  *prog, *progname;
char  *dowhat;
char  FileName[255];
FILE  *refNum;
enum  PROTOCOL mode = GARMIN; 



/****************************************************************************/
/* Print message if GPS is not responding and exit.                         */
/****************************************************************************/
void NotResponding()
{
  fprintf(stderr, "GPS [%s] is not responding - ", gPrefs.Device);
  fprintf(stderr, "make sure it is on and set to GRMN/GRMN protocol.\n");
  exit(1);
}


/****************************************************************************/
/* Print headline with programname, version and copyright notice.           */
/****************************************************************************/
void PrintHeadLine()
{
  fprintf(stderr, "%s (ASCII) - Version %s\nCopyright (c) %s by Carsten ",
	  ProgramName, ProgramVersion, ProgramYear);
  fprintf(stderr, "Tschach (tschach@zedat.fu-berlin.de)\n");
  fprintf(stderr, "Linux/KKJ mods by     Janne Sinkkonen <janne@iki.fi> (1996)\n");
  fprintf(stderr, "Copyright (c) 1998 Mayko-mXmap mods by Matthias Kattanek <mattes@ugraf.com>\n\n");
}


/****************************************************************************/
/* Print usage information.                                                 */
/****************************************************************************/
void usage()
{
  fprintf(stderr, "Usage: %s flag [filename]\n\n", progname);
  
  fprintf(stderr, "Flags are:  -v   version:   information about program\n");
  fprintf(stderr, "            -p   port:      set serial I/O-Device\n");
  fprintf(stderr, "            -s   setup:     set datum, format, offset ");
  fprintf(stderr, "and device\n\n");
  fprintf(stderr, "            -i   ident:     identify connected GPS\n");
  fprintf(stderr, "            -o   off:       Turn off GPS Device\n");
  fprintf(stderr, "            -t   time:      Get time from GPS (-ts will ");
  fprintf(stderr, "set time on host)\n\n");
  fprintf(stderr, "            -d?  download:  r = route, t = track, w = ");
  fprintf(stderr, "waypoint, a = almanac\n");
  fprintf(stderr, "            -u?  upload:    r = route, t = track, w = ");
  fprintf(stderr, "waypoint\n\n");
  fprintf(stderr, "            -m   format:    data in Mayko-mXmap format\n\n");
  fprintf(stderr, "If no filename is entered, data will be written to %s",
	  "stdout and read from stdin.\n");
  fprintf(stderr, "Serial I/O-Device can also be set within the %s",
	  "environment variable $GPSDEV.\n");
  exit(1);
}


/****************************************************************************/
/* Print error messages.                                                    */
/****************************************************************************/
void ParseError(err)
int err;
{
  switch (err) {
  case 1:
    fprintf(stderr, "You have to specify at least one parameter.\n\n");
    fprintf(stderr, "Start %s without any parameters to get usage ",
	    progname);
    fprintf(stderr, "information.\n");
    break;
  case 2:
    fprintf(stderr, "You have to specify one of '-da -dr -dt -dw'\n\n");
    fprintf(stderr, "Start %s without any parameters to get usage ",
	    progname);
    fprintf(stderr, "information.\n");
    break;
  case 3:
    fprintf(stderr, "You have to specify one of '-ur -ut -uw'\n\n");
    fprintf(stderr, "Start %s without any parameters to get usage ",
	    progname);
    fprintf(stderr, "information.\n");
    break;
  case 4:
    fprintf(stderr, "You have to specify a device name when using '-p'\n\n");
    fprintf(stderr, "Start %s without any parameters to get usage ",
	    progname);
    fprintf(stderr, "information.\n");
    break;
  case 5:
    fprintf(stderr, "Can't get time from GPS Receiver\n");
    break;
  }
  fflush(stderr);
  exit(1);
}


/****************************************************************************/
/* Print verbose version information and copyright notice.                  */
/****************************************************************************/
void HandleAbout()
{
  fprintf(stderr, "Parts of this program were taken from MacGPS by %s",
	  "John F. Waers\n\n");
  
  fprintf(stderr, "If you've any questions or bugs please send email to:\n\n");
  fprintf(stderr, "%53s", "tschach@zedat.fu-berlin.de");
  fprintf(stderr, "\n\n");
  fprintf(stderr, "The newest version can always be found at:\n\n");
  fprintf(stderr, "%57s", "ftp.fu-berlin.de:/pub/unix/misc/gps");
  fprintf(stderr, "\n\n");

  fprintf(stderr, "*******************************************************************************\n");
  fprintf(stderr, "*   Permission to use, copy, modify, and distribute this software and its     *\n");
  fprintf(stderr, "*   documentation for non-commercial purpose, is hereby granted without fee,  *\n");
  fprintf(stderr, "*   providing that the copyright notice appears in all copies and that both   *\n");
  fprintf(stderr, "*   the copyright notice and this permission notice appear in supporting      *\n");
  fprintf(stderr, "*   documentation. I make no representations about the suitability of this    *\n");
  fprintf(stderr, "*   software for any purpose. It is provides \"as is\" without express or       *\n");
  fprintf(stderr, "*   implied warranty.                                                          *\n");
  fprintf(stderr, "*******************************************************************************\n");
  exit(0);
}


/****************************************************************************/
/* Query GPS for device type and software version.                          */
/****************************************************************************/
void InfoAboutGPS()
{
  printf("Connected GPS [%s] is: %s\n", gPrefs.Device, getGPSVersion());
  exit(0);
}


/****************************************************************************/
/* Turn off GPS device.                                                     */
/****************************************************************************/
void TurnOffGPS()
{
  sendGPSOff();
  exit(0);
}


/****************************************************************************/
/* Get time from GPS - compare with local time and adjust if requested.     */
/****************************************************************************/
void AdjustTime(char dowhat)
{
  char            c;
  long            diffis;
  time_t          clock, clockgps;
  struct timeval  tp;

  time(&clock);
  clockgps = getGPSTime();
  if (clockgps == -1) ParseError(5);

  printf("Local time determine from GPS-Receiver is:  %s", ctime(&clockgps));
  printf("Local time on machine is:                   %s\n", ctime(&clock));
  
  diffis = (clockgps - clock);
  if (diffis < 0) diffis = diffis * (-1);
  printf("Time difference is %ld seconds.\n", diffis);

  /* if set-option and uid=root then adjust local time */
  if (dowhat == 's') {
    if (getuid() == 0) {
      if (diffis <= 7200) {
	clockgps   = getGPSTime();
	tp.tv_sec  = clockgps;
	tp.tv_usec = 5000;                /* to adjust lost time in process */
	settimeofday(&tp, (struct timezone *)NULL);
	fprintf(stderr, "\nLocal time set to:  %s", ctime(&clockgps));
      } else {
	fprintf(stderr, "\nTime difference is > 2 hours - time not set\n");
      }
    } else 
      fprintf(stderr, "\nSorry, only root can set the time.\n");
  }

  exit(0);
}


/*****************************************************************************/
/* Catch signals to get grateful death.                                      */
/*****************************************************************************/
void TerminateHandler(sig)
int   sig;
{
  if (sig == SIGINT) {
    fprintf(stderr, "\nDon't touch me...but you've pressed CTRL-C\n");
    fprintf(stderr, "It was your choice....exiting\n");
  } else {
    fprintf(stderr, "\nGotcha...somebody try to shoot me...but he missed !\n");
    fprintf(stderr, "\nBut okay, committing suicide....<argh!>\n");
  }
  exit(0);
}



/*****************************************************************************/
/*                        H a u p t p r o g r a m m                          */
/*****************************************************************************/
main(argc, argv)
int  argc;
char *argv[];
{
  /* Initialize Signal handler */
  signal(SIGTERM, TerminateHandler);
  signal(SIGINT,  TerminateHandler);

  /* initialize mayko-xmap flag */
  mayko_xmap_format=0;

  /* Get program name and print headline */
  progname = argv[0];
  PrintHeadLine();

  /* Print usage information if no parameters where given */
  if (argc <= 1) usage();
 
  /* Init preferences - overwrite serial device if $GPSDEV is set */ 
  sprintf(FileName, "");
  InitPrefs();
  if (getenv("GPSDEV") != NULL) sprintf(gPrefs.Device, "%s", getenv("GPSDEV"));

  /* Parse arguements */
  for (prog = *argv++; --argc; argv++) {
    if (argv[0][0] == '-') {
      switch (argv[0][1]) {
      case 'd':
        dowhat = *argv+2;
	cmd = 'd';
        break;
      case 'u':
        dowhat = *argv+2;
	cmd = 'u';
        break;
      case 'p':
	sprintf(gPrefs.Device, "%s", *argv+2);
	break;
      case 'v':
	HandleAbout();
        break;
      case 'i':
	InfoAboutGPS();
	break;
      case 'o':
	TurnOffGPS();
	break;
      case 's':
	SetupProgram();
	break;
      case 't':
	dowhat = *argv+2;
	AdjustTime(*dowhat);
	break;
      case 'm':
        fprintf(stderr, "\n... Mayko-mXmap format is enabled !\n");
	mayko_xmap_format++;
	break;
      }
    } else {
      if (strlen(FileName) == 0) sprintf(FileName, "%s", argv[0]);
    }
  }

  /* Exit if no serial device */
  if (strlen(gPrefs.Device) == 0) ParseError(4);

  /* Parse up- and download parameters */
  if (cmd != ' ') what = dowhat[0];

  switch (cmd) {
  case 'd': {
    if (strlen(FileName) == 0) 
      refNum = stdout;
    else
      refNum = fopen(FileName, "wt");

    switch (what) {
    case 'a':
      getGPSInfo(refNum, ALMANAC);
      break;
    case 'r':
      getGPSInfo(refNum, ROUTE);
      break;
    case 't':
      getGPSInfo(refNum, TRACK);
      break;
    case 'w':
      getGPSInfo(refNum, WAYPOINT);
      break;
    default:
      ParseError(2);
      break;
    }
    break;
  }
  case 'u': {
    if (strlen(FileName) == 0) 
      refNum = stdin;
    else
      refNum = fopen(FileName, "rt");

    switch (what) {
    case 'r':
      sendGPSInfo(refNum, ROUTE);
      break;
    case 't':
      sendGPSInfo(refNum, TRACK);
      break;
    case 'w':
      sendGPSInfo(refNum, WAYPOINT);
      break;
    default:
      ParseError(3);
      break;
    }
    break;
  }
  default:
    ParseError(1);
    break;
  }
  exit(0);
}