File: mapsend.c

package info (click to toggle)
gpsbabel 1.3.2-2
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 15,956 kB
  • ctags: 9,083
  • sloc: ansic: 87,514; xml: 13,640; pascal: 6,981; sh: 3,770; makefile: 811; perl: 754; tcl: 74; objc: 7
file content (673 lines) | stat: -rw-r--r-- 15,035 bytes parent folder | download
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
/*
    Access Magellan Mapsend files.

    Copyright (C) 2002-2006 Robert Lipe, robertlipe@usa.net

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    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.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111 USA
 */

#include <stdio.h>
#include <string.h>

#include "defs.h"
#include "mapsend.h"
#include "magellan.h"

static FILE *mapsend_file_in;
static FILE *mapsend_file_out;
static short_handle mkshort_handle;
static short_handle wpt_handle;

static int route_wp_count;
static int mapsend_infile_version;
static int trk_version = 30;

#define MYNAME "mapsend"

static char *mapsend_opt_trkver = NULL;
#define MAPSEND_TRKVER_MIN 3
#define MAPSEND_TRKVER_MAX 4

static
arglist_t mapsend_args[] = {
	{"trkver", &mapsend_opt_trkver, 
		"MapSend version TRK file to generate (3,4)",
		"4", ARGTYPE_INT, "3", "4" },
	ARG_TERMINATOR
};

static void
mapsend_init_opts(const char isReading) {	/* 1=read, 2=write */
	int opt_trkver;
	
	/* read & write options here */
	
	if (isReading) {
		/* reading-only options here */
	} else {
		/* writing-only options here */
		
		// TRK MapSend version
	    opt_trkver = atoi(mapsend_opt_trkver);
	    if ((opt_trkver < MAPSEND_TRKVER_MIN) || (opt_trkver > MAPSEND_TRKVER_MAX))
		fatal(MYNAME ": Unsupported MapSend TRK version \"%s\"!\n", mapsend_opt_trkver);
		trk_version = opt_trkver * 10;
	}
}

static void
mapsend_rd_init(const char *fname)
{
	mapsend_init_opts(1);
	mapsend_file_in = xfopen(fname, "rb", MYNAME);
}

static void
mapsend_rd_deinit(void)
{
	fclose(mapsend_file_in);
}

static 
void 
my_fread8(void *ptr, FILE *stream)
{
	unsigned char cbuf[8];
	size_t rv;

	rv = fread(cbuf, 8, 1, stream);
	le_read64(ptr, cbuf);
	
}

static 
void
my_fwrite8(void *ptr, FILE *stream)
{
	unsigned char cbuf[8];

	le_read64(cbuf, ptr);
	fwrite(cbuf, 8, 1, stream);
}

static 
void 
my_fread4(void *ptr, FILE *stream)
{
	unsigned int *iptr = ptr;
	unsigned char cbuf[4];
	size_t rv;

	rv = fread(cbuf, 4, 1, stream);
	*iptr = le_read32(cbuf);
}

static 
size_t
my_fwrite4(void *ptr, FILE *stream)
{
	int i = le_read32(ptr);
	return fwrite(&i, 4, 1, stream);
}

static void
mapsend_wr_init(const char *fname)
{
	mapsend_init_opts(0);
	mapsend_file_out = xfopen(fname, "wb", MYNAME);
	mkshort_handle = mkshort_new_handle();

	wpt_handle = mkshort_new_handle();
	setshort_whitespace_ok(wpt_handle, 1);
	setshort_length(wpt_handle, 8);

	route_wp_count = 0;
}

static void
mapsend_wr_deinit(void)
{
	fclose(mapsend_file_out);
	mkshort_del_handle(&mkshort_handle);
	mkshort_del_handle(&wpt_handle);
}

static void
mapsend_wpt_read(void)
{
	char tbuf[256];
	char name[257];
	char comment[257];
	char *p;
	int wpt_count, rte_count, rte_num;
	unsigned char scount;
	int wpt_number;
	char wpt_icon;
	char wpt_status;
	double wpt_alt;
	double wpt_long;
	double wpt_lat;
	waypoint *wpt_tmp;
	route_head *rte_head;

	my_fread4(&wpt_count, mapsend_file_in);
	
	while (wpt_count--) {
		wpt_tmp = waypt_new();

		if (fread(&scount, sizeof(scount), 1, mapsend_file_in) < 1) {
			fatal(MYNAME ": out of data reading %d waypoints\n",
					wpt_count);
		}
		fread(tbuf, scount, 1, mapsend_file_in);
		p = strncpy(name, tbuf, scount);
		p[scount] = '\0';

		fread(&scount, sizeof(scount), 1, mapsend_file_in);
		fread(tbuf, scount, 1, mapsend_file_in);
		p = strncpy(comment, tbuf, scount);
		p[scount] = '\0';

		my_fread4(&wpt_number, mapsend_file_in);
		fread(&wpt_icon, sizeof(wpt_icon), 1, mapsend_file_in);
		fread(&wpt_status, sizeof(wpt_status), 1, mapsend_file_in);
		my_fread8(&wpt_alt, mapsend_file_in);
		my_fread8(&wpt_long, mapsend_file_in);
		my_fread8(&wpt_lat, mapsend_file_in);

		wpt_tmp->shortname = xstrdup(name);
		wpt_tmp->description = xstrdup(comment);
		wpt_tmp->altitude = wpt_alt;
		wpt_tmp->latitude = -wpt_lat;
		wpt_tmp->longitude = wpt_long;

		if (wpt_icon < 26)
			sprintf(tbuf, "%c", wpt_icon + 'a');
		else
			sprintf(tbuf, "a%c", wpt_icon - 26 + 'a');
		wpt_tmp->icon_descr = mag_find_descr_from_token(tbuf);

		waypt_add(wpt_tmp);
	}
	
	/* now read the routes... */
	my_fread4(&rte_count, mapsend_file_in);
	
	while (rte_count--) {
		rte_head = route_head_alloc();
		route_add_head(rte_head);
		
		/* route name */
		fread(&scount, sizeof(scount), 1, mapsend_file_in);
		fread(tbuf, scount, 1, mapsend_file_in);
		tbuf[scount] = '\0';
		rte_head->rte_name = xstrdup(tbuf);
		
		/* route # */
		my_fread4(&rte_num, mapsend_file_in);
		rte_head->rte_num = rte_num;
		
		/* points this route */
		my_fread4(&wpt_count, mapsend_file_in);
		
		while (wpt_count--) {
			wpt_tmp = waypt_new();

			/* waypoint name */
			fread(&scount, sizeof(scount), 1, mapsend_file_in);
			fread(tbuf, scount, 1, mapsend_file_in);
			tbuf[scount] = '\0';

			wpt_tmp->shortname = xstrdup(tbuf);
			
			/* waypoint # */
			my_fread4(&wpt_number, mapsend_file_in);
			my_fread8(&wpt_long, mapsend_file_in);
			my_fread8(&wpt_lat, mapsend_file_in);
			fread(&wpt_icon, sizeof(wpt_icon), 1, mapsend_file_in);

			wpt_tmp->longitude = wpt_long;
			wpt_tmp->latitude = -wpt_lat;

			if (wpt_icon < 26)
				sprintf(tbuf, "%c", wpt_icon + 'a');
			else
				sprintf(tbuf, "a%c", wpt_icon - 26 + 'a');
			wpt_tmp->icon_descr = mag_find_descr_from_token(tbuf);

			route_add_wpt(rte_head, wpt_tmp);
		}
	}
}

static void
mapsend_track_read(void)
{
	char *trk_name;
	unsigned char scount;
	unsigned int trk_count;
	double wpt_long;
	double wpt_lat;
	double wpt_alt;
	int i = 0;
	float f = 0;
	int time;
	int valid;
	unsigned char centisecs;
	route_head *track_head;
	waypoint *wpt_tmp;

	fread(&scount, sizeof(scount), 1, mapsend_file_in);
	trk_name = xmalloc(scount + 1);
	fread(trk_name, scount, 1, mapsend_file_in);
	trk_name[scount] = '\0';
	my_fread4(&trk_count, mapsend_file_in);

	track_head = route_head_alloc();
	track_head->rte_name = trk_name;
	track_add_head(track_head);

	while (trk_count--) {
		my_fread8(&wpt_long, mapsend_file_in);
		my_fread8(&wpt_lat, mapsend_file_in);
		if (mapsend_infile_version < 36) { /* < version 4.0 */
			my_fread4(&i, mapsend_file_in);
			wpt_alt = i;
		} else {
			my_fread4(&f, mapsend_file_in);
			wpt_alt = f;
		}
		my_fread4(&time, mapsend_file_in);
		my_fread4(&valid, mapsend_file_in);

		/* centiseconds only in >= version 3.0 */
		if (mapsend_infile_version >= 34) {
			fread(&centisecs, 1, 1, mapsend_file_in);
		} else {
			centisecs = 0;
		}

		wpt_tmp = waypt_new();
		wpt_tmp->latitude = -wpt_lat;
		wpt_tmp->longitude = wpt_long;
		wpt_tmp->creation_time = time;
		wpt_tmp->centiseconds = centisecs;
		wpt_tmp->altitude = wpt_alt;
		track_add_wpt(track_head, wpt_tmp);
	}
}

static void
mapsend_read(void)
{
	mapsend_hdr hdr;
	int type;
	char buf[3];

	/*
	 * Because of the silly struct packing and the goofy variable-length
	 * strings, each member has to be read in one at a time.  Grrr.
	 */

	fread(&hdr, sizeof(hdr), 1, mapsend_file_in);
	type = le_read16(&hdr.ms_type);
	strncpy(buf, hdr.ms_version, 2);
	buf[2] = '\0';
	
	mapsend_infile_version = atoi(buf);

	switch(type) {
		case ms_type_wpt:
			mapsend_wpt_read();
			break;
		case ms_type_track:
			mapsend_track_read();
			break;
		case ms_type_log:
			fatal(MYNAME ", GPS logs not supported.\n");
		case ms_type_rgn:
			fatal(MYNAME ", GPS regions not supported.\n");
		default:
			fatal(MYNAME ", unknown file type %d\n", type);
	}
}


static void
mapsend_waypt_pr(const waypoint *waypointp)
{
	int n;
	unsigned char c;
	double falt;
	double flong;
	double flat;
	static int cnt = 0;
	const char *iconp = NULL;
	const char *sn = global_opts.synthesize_shortnames ? 
		mkshort_from_wpt(mkshort_handle, waypointp) :
	       	waypointp->shortname;
	char *tmp;

	/*
	 * The format spec doesn't call out the character set of waypoint
	 * name and description.  Empirically, we can see that it's 8859-1,
	 * but if we create mapsend files containing those, Mapsend becomes
	 * grumpy uploading the resulting waypoints and being unable to deal
	 * with the resulting comm errors.
 	 * 
	 * Ironically, our own Magellan serial module strips the "naughty"
	 * characters, keeping it more in definition with their own serial
	 * spec. :-)
	 * 
	 * So we just decompose the utf8 strings to ascii before stuffing
	 * them into the Mapsend file.
	 */

	
	tmp = mkshort(wpt_handle, sn);
	c = tmp ? strlen(tmp) : 0;
	fwrite(&c, 1, 1, mapsend_file_out);
	fwrite(tmp, c, 1, mapsend_file_out);
	if (tmp)
		xfree(tmp);

	tmp = waypointp->description;
	if (tmp)
		c = strlen(tmp);
	else
		c = 0;

	if (c > 30) c = 30;
	fwrite(&c, 1, 1, mapsend_file_out);
	fwrite(tmp, c, 1, mapsend_file_out);

	/* #, icon, status */
	n = ++cnt;
	my_fwrite4(&n, mapsend_file_out);

	if (waypointp->icon_descr) {
		iconp = mag_find_token_from_descr(waypointp->icon_descr);
		if (1 == strlen(iconp)) {
			c = iconp[0] - 'a';
		} else {
			c = iconp[1] - 'a' + 26;
		}
	} else  {
		c = 0;
	}
	if (get_cache_icon(waypointp)) {
		iconp = mag_find_token_from_descr(get_cache_icon(waypointp));
		if (1 == strlen(iconp)) {
			c = iconp[0] - 'a';
		} else {
			c = iconp[1] - 'a' + 26;
		}
	}

	fwrite(&c, 1, 1, mapsend_file_out);
	c = 1;
	fwrite(&c, 1, 1, mapsend_file_out);

	falt = waypointp->altitude;
	if (falt == unknown_alt)
		falt = 0;
	my_fwrite8(&falt, mapsend_file_out);

	flong = waypointp->longitude;
	my_fwrite8(&flong, mapsend_file_out);
	flat = -waypointp->latitude;
	my_fwrite8(&flat, mapsend_file_out);
}

static void 
mapsend_route_hdr(const route_head *rte)
{
	int i;
	unsigned char c;
	char * rname;
	
	/* route name -- mapsend really seems to want something here.. */
	if (!rte->rte_name)
		rname = xstrdup("Route");
	else
		rname = xstrdup(rte->rte_name);

	c = strlen(rname);
	
	fwrite(&c, 1, 1, mapsend_file_out);
	fwrite(rname, c, 1, mapsend_file_out);

	xfree(rname);
	
	/* route # */
	i = rte->rte_num;
 	my_fwrite4(&i, mapsend_file_out);
	
	i = rte->rte_waypt_ct;

	/* # of waypoints to follow... */
	my_fwrite4(&i, mapsend_file_out);
}

static void 
mapsend_noop(const route_head *wp)
{
	/* no-op */
}

static void 
mapsend_route_disp(const waypoint *waypointp)
{
	unsigned char c;
	const char *iconp;
	double dbl;

	route_wp_count++;
	
	/* waypoint name */
	c = waypointp->shortname ? strlen(waypointp->shortname) : 0;
	fwrite(&c, 1, 1, mapsend_file_out);
	fwrite(waypointp->shortname, c, 1, mapsend_file_out);
	
	/* waypoint number */
	my_fwrite4(&route_wp_count, mapsend_file_out);

	dbl = waypointp->longitude;
	my_fwrite8(&dbl, mapsend_file_out);

	dbl = -waypointp->latitude;
	my_fwrite8(&dbl, mapsend_file_out);

	if (waypointp->icon_descr) {
		iconp = mag_find_token_from_descr(waypointp->icon_descr);
		if (1 == strlen(iconp)) {
			c = iconp[0] - 'a';
		} else {
			c = iconp[1] - 'a' + 26;
		}
	} else  {
		c = 0;
	}
	fwrite(&c, 1, 1, mapsend_file_out);
}

void mapsend_track_hdr(const route_head * trk)
{
	/* 
	 * we write mapsend v3.0 tracks as mapsend v2.0 tracks get
	 * tremendously out of whack time/date wise.
	 */
	char *verstring = "30";
	queue *elem, *tmp;
	char *tname;
	unsigned char c;
	int i;
	mapsend_hdr hdr = {13, "4D533334 MS", "30", ms_type_track, {0, 0, 0}};
	
	switch (trk_version) {
		case 20: verstring = "30"; break;
		case 30: verstring = "34"; break;
		case 40: 
			/* the signature seems to change with the versions, even though it 
			 * shouldn't have according to the document. MapSend V4 doesn't 
			 * like the old version.
			 */
			hdr.ms_signature[7] = '6';
			verstring = "36"; 
			break;
		default: fatal("Unknown track version.\n"); break;
	}

	hdr.ms_version[0] = verstring[0];
	hdr.ms_version[1] = verstring[1];

	fwrite(&hdr, sizeof(hdr), 1, mapsend_file_out);

	/* track name */
	if (!trk->rte_name)
		tname = xstrdup("Track");
	else
		tname = xstrdup(trk->rte_name);

	c = strlen(tname);
	
	fwrite(&c, 1, 1, mapsend_file_out);
	fwrite(tname, c, 1, mapsend_file_out);
	xfree(tname);
	
	/* total nodes (waypoints) this track */
	i = 0;
	QUEUE_FOR_EACH(&trk->waypoint_list, elem, tmp) {
		i++;
	}		

	my_fwrite4(&i, mapsend_file_out);
	
}

void mapsend_track_disp(const waypoint * wpt)
{
	unsigned char c;
	double dbl;
	int i;
	int t;
	int f;
	int valid = 1;
	static int last_time;

	/*
	 * Firmware Ver 4.06 (at least) has a defect when it's set for .01km
	 * tracking that will sometimes result in timestamps in the track
	 * going BACKWARDS.   When mapsend sees this, it (stupidly) advances
	 * the date by one, ignoring the date on the TRK lines.   This looks 
	 * for time travel and just uses the previous time - it's better to
	 * be thought to be standing still than to be time-travelling!
	 *
	 * This is rumoured (but yet unconfirmed) to be fixed in f/w 5.12.
	 */
	t = wpt->creation_time;
	if (t < last_time)  {
		t = last_time;
	}
	
	/* x = longitude */
	dbl = wpt->longitude;
	my_fwrite8(&dbl, mapsend_file_out);

	/* x = latitude */
	dbl = -wpt->latitude;
	my_fwrite8(&dbl, mapsend_file_out);

	/* altitude 
	 * in V4.0+ this field is a float, it was previously an int
	 */
	if (trk_version < 40) {
		i = (int) wpt->altitude;
		my_fwrite4(&i, mapsend_file_out);
	} else {
		f = (float) wpt->altitude;
		my_fwrite4(&f, mapsend_file_out);
	}
	
	/* time */
	my_fwrite4(&t, mapsend_file_out);
	last_time = t;

	/* validity */
	my_fwrite4(&valid, mapsend_file_out);

	/* 0 centiseconds */
	if (trk_version >= 30) {
		c = wpt->centiseconds;
		fwrite(&c, 1, 1, mapsend_file_out);
	}
}

void 
mapsend_track_write(void)
{
	track_disp_all(mapsend_track_hdr, mapsend_noop, mapsend_track_disp);
}

static void
mapsend_wpt_write(void)
{
	mapsend_hdr hdr = {13, {"4D533330 MS"}, {"30"}, ms_type_wpt, {0, 0, 0}};
	int wpt_count = waypt_count();
	int n = 0;
	
	if (global_opts.objective == trkdata) {
		mapsend_track_write();
	} else {
		fwrite(&hdr, sizeof(hdr), 1, mapsend_file_out);

		if (global_opts.objective == wptdata) {
			my_fwrite4(&wpt_count, mapsend_file_out);
			waypt_disp_all(mapsend_waypt_pr);
		} else 
		if (global_opts.objective == rtedata) {

			/* # of points - all routes */
			n = route_waypt_count();
			my_fwrite4(&n, mapsend_file_out);

			/* write points - all routes */
			route_disp_all(mapsend_noop, mapsend_noop, mapsend_waypt_pr);
		}
		
		n = route_count();

		my_fwrite4(&n, mapsend_file_out);
	
		if (n)
			route_disp_all(mapsend_route_hdr, mapsend_noop, mapsend_route_disp);
	}
}



ff_vecs_t mapsend_vecs = {
	ff_type_file,
	FF_CAP_RW_ALL,
	mapsend_rd_init,
	mapsend_wr_init,
	mapsend_rd_deinit,
	mapsend_wr_deinit,
	mapsend_read,
	mapsend_wpt_write,
	NULL,
	mapsend_args,
	CET_CHARSET_ASCII, 0	/* CET-REVIEW */
};