diff options
author | Reinhard Arlt <reinhard.arlt@t-online.de> | 2013-07-21 23:53:44 +0200 |
---|---|---|
committer | Reinhard Arlt <reinhard.arlt@t-online.de> | 2013-07-21 23:53:44 +0200 |
commit | b8fffbe3871316d1cf892d1288fc6ddce017c038 (patch) | |
tree | 680179ab0a8f289e89a384ba58d0a31a448c1e0c /pseudoais.c | |
parent | 393adafe5abb4959d5575aa52b670096adc09134 (diff) | |
download | gpsd-b8fffbe3871316d1cf892d1288fc6ddce017c038.tar.gz |
Groundbreaking for AIVDM messages in nmea mode.
Diffstat (limited to 'pseudoais.c')
-rw-r--r-- | pseudoais.c | 119 |
1 files changed, 119 insertions, 0 deletions
diff --git a/pseudoais.c b/pseudoais.c new file mode 100644 index 00000000..ff2c5a6d --- /dev/null +++ b/pseudoais.c @@ -0,0 +1,119 @@ +/* + * Encode AIS messages. + * + * See the file AIVDM.txt on the GPSD website for documentation and references. + * + * This file is build from driver_ais.c + * + * This file is Copyright (c) 2013 by the GPSD project + * BSD terms apply: see the file COPYING in the distribution root for details. + */ +#include <stdlib.h> +#include <string.h> +#include <ctype.h> + +#include "gpsd.h" +#include "bits.h" +#include <stdint.h> + +static unsigned char convtab[] = {"0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVW`abcdefghijklmnopqrstuvw"}; + +static unsigned int ais_addbits(unsigned char *bits, + unsigned int start, + unsigned int len, + uint64_t data) +{ + unsigned int l; + unsigned int pos; + uint64_t mask; + unsigned char mask1; + + mask = 0x1; + pos = (start+len-1) / 6; + mask1 = 0x20; + mask1 = mask1 >> ((start+len-1) % 6); + + if (len == 0) { + return 0; + } + + for(l=0;l<len;l++) { + if (data & mask) { + bits[pos] |= mask1; + } + mask <<= 1; + mask1 <<= 1; + if (mask1 == 0x40) { + pos -= 1; + mask1 = 0x1; + } + } + return 0; +} + + +static unsigned int ais_binary_to_ascii(unsigned char *bits, unsigned int len) +{ + unsigned int l; + + if (len == 0) { + bits[0] = 0; + return 0; + } + + for (l=0;l<len;l+=6) { + bits[l/6] = convtab[bits[l/6] & 0x3f]; + } +} + + +unsigned int ais_binary_encode(struct ais_t *ais, + unsigned char *bits) +{ + unsigned int len; + + len = 0; + + ais_addbits(bits, 0, 6, ais->type); + ais_addbits(bits, 6, 2, ais->repeat); + ais_addbits(bits, 8, 30, ais->mmsi); + switch (ais->type) { + case 1: /* Position Report */ + case 2: + case 3: + ais_addbits(bits, 38, 4, ais->type1.status); + ais_addbits(bits, 42, 8, ais->type1.turn); + ais_addbits(bits, 50, 10, ais->type1.speed); + ais_addbits(bits, 60, 1, ais->type1.accuracy); + ais_addbits(bits, 61, 28, ais->type1.lon); + ais_addbits(bits, 89, 27, ais->type1.lat); + ais_addbits(bits, 116, 12, ais->type1.course); + ais_addbits(bits, 128, 9, ais->type1.heading); + ais_addbits(bits, 137, 6, ais->type1.second); + ais_addbits(bits, 143, 2, ais->type1.maneuver); + /* ais->type1.spare = UBITS(145, 3); */ + ais_addbits(bits, 148, 1, ais->type1.raim); + ais_addbits(bits, 149, 19, ais->type1.radio); + len = 149 + 19; + break; + case 4: /* Base Station Report */ + case 11: /* UTC/Date Response */ + break; + case 5: /* Ship static and voyage related data */ + break; + case 9: /* Standard SAR Aircraft Position Report */ + break; + case 18: /* Standard Class B CS Position Report */ + break; + case 19: /* Extended Class B CS Position Report */ + break; + case 21: /* Aid-to-Navigation Report */ + break; + case 24: /* Class B CS Static Data Report */ + break; + case 27: /* Long Range AIS Broadcast message */ + break; + } + ais_binary_to_ascii(bits, len); + return len; +} |