[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[gpsd-dev] Space removal problems in AIS messages
From: |
Stefan Roels |
Subject: |
[gpsd-dev] Space removal problems in AIS messages |
Date: |
Fri, 02 Oct 2015 07:22:08 +0200 |
User-agent: |
Mozilla/5.0 (X11; Linux x86_64; rv:31.0) Gecko/20100101 Thunderbird/31.3.0 |
Dear All,
I am currently working on a AIS message decoder (AIVDM/AIVDO) for a
project at work. Rather than using the existing gpsd library, I am
writing my own decoder. I am using the http://catb.org/gpsd/AIVDM.html
documentation as guideling for my decoder and I use the gpsdecoder
output for validation purposes, confirming that my decoder provides the
same results. Obviously, most of the problems I am running into are due
to my own mistakes, but I am also running into some problems that seem
to be on the gpsd side.
One of these problems has to do with the removal of spaces at the end of
the six bits ascii strings. According to the documentation, the
following steps should be taken:
- Remove everything from the first @ sign (including the @ sign itself)
- Strip trailing spaces from the result
The existing code however fails to do so properly in certain circumstances.
Consider the following scenarios (all assuming count = 20, e.g. type 5
field shipname):
1) The input string: "ALPHA @@@@@@@@@@@@@@" (single space before first @
character)
The from_sixbit function will take i from 0 up to and including 6, where
it will see the @ and break. to[6] will then be set to \0. Now the
second part of the algorithm kicks in and starts at i=18. I could not
find where the field was initialised, so I am not sure whether the field
would be zero-filled, @ or space filled or random junk, but in any case
either we find spaces or @ signs until we reach i=6, or we break at i>6.
This would leave us with:
"ALPHA \0..." The space at to[5] will not be cleared.
2) The input string: "ALPHA BRAVO CHARLIE " (single space at the end)
The from_sixbit function will take i from 0 up to and including 20, the
break condition of the for loop will be met and we set to[20] to \0. At
that moment we get to the second part of the algorithm which would look
at to[18] which is 'E' and break without removing the trailing space.
3) The input string: "ALPHA BRAVO CHARLI E" (space as character before
the last one)
The from_sixbit function will take i from 0 up to and including 20, the
break condition of the for loop will be met and we set to[20] to \0. At
that moment we get to the second part of the algorithm which would look
at to[18] which is ' ' and will replace it with a \0, dropping the E at
the end.
Another spacing problem lies within the type 21 name/name extension
field. If the name field would be trimmed, but there is data in the name
extension field, the name extension would not be taken into account.
Take for instance the following examples:
name:"ALPHA BRAVO CHARLI E" name extension:"CHO" (assuming the incorrect
space removal) result: "ALPHA BRAVO CHARLI" instead of "ALPHA BRAVO
CHARLI ECHO"
name:"ALPHA BRAVO CHARLIE " name extension:"DELTA" (assuming removal of
the trailing space of name) result "ALPHA BRAVO CHARLIE" instead of
"ALPHA BRAVO CHARLIE DELTA"
I have created a patch to solve the problems mentioned above. You can
find the patch and some examples of real world messages going wrong below.
Best regards,
Stefan Roels
!AIVDM,2,1,7,B,53aDpaT000010;CKKB0h4Q8TpLDr222222222216<P:656rd07Tai0CKk5hD,0*74
!AIVDM,2,2,7,B,Q1C`8888880,2*63
-"callsign":"PB4664 "
+"callsign":"PB4664"
@@ -61,12 +61,12 @@
!AIVDM,2,1,2,B,address@hidden>1=Dq8U<F222220V1pR847<:0<hCDm1DQ0CH8888,0*0B
!AIVDM,2,2,2,B,8888882,2*27
-"callsign":"PE6851 "
+"callsign":"PE6851"
!AIVDM,2,1,2,B,address@hidden;address@hidden<P4m0Ttr0O10C,0*5D
!AIVDM,2,2,2,B,2640Ht043lU30CQ8888888888880,2*06
-"shipname":"WILLAMETTE CHAMPION "
+"shipname":"WILLAMETTE CHAMPION"
!AIVDM,2,1,9,A,53<@V;@220A8E8qF2204hU`F2222222222222216000004I1093SmSlSlljF,0*68
!AIVDM,2,2,9,A,address@hidden,2*06
-"destination":"NOVOROSSIYSK;RUSSIA "
+"destination":"NOVOROSSIYSK;RUSSIA"
!AIVDM,2,1,5,B,ENk`sBG37a:@address@hidden,0*1A
!AIVDM,2,2,5,B,000003vP08=P,0*50
-"name":"FORT PIERCE INLET LB"
+"name":"FORT PIERCE INLET LB 6"
!AIVDM,2,1,0,1,53aI9sl000010WCC;@1LTh`t000000000000001S0h;41vqa00PCDm1DQ0CO,0*55
!AIVDM,2,2,0,1,`8800000000,2*04
-"destination":"AMSTERDAM> "
+"destination":"AMSTERDAM>"
!AIVDM,1,1,,A,address@hidden@address@hidden<Mh0,2*4A
-"name":"HILLSBRGH CUT C CH"
+"name":"HILLSBRGH CUT C CH LB 17"
!AIVDM,2,1,5,B,53aC2TT000010GK;address@hidden@T62220000001?78825w?os0K`88888888,0*4A
!AIVDM,2,2,5,B,88888888800,2*2A
-callsign":"PE6235 ","shipname":"DILIGENTIA "
+callsign":"PE6235","shipname":"DILIGENTIA"
!AIVDM,1,1,,B,ENk`sBG37a:@address@hidden,0*6C
-"name":"FORT PIERCE INLET LB"
+"name":"FORT PIERCE INLET LB 6"
!AIVDM,1,1,,B,C6:W26h00:address@hidden,0*71
-"shipname":"YUXINHUO10388 "
+"shipname":"YUXINHUO10388"
!AIVDM,1,1,,B,C6:kuiP0021mUQSCHwAvKwk2>bB>2L?0>2L>`2C1iik0S38351P7,0*06
-"shipname":"GUIGANG GANGTAI 889 "
+"shipname":"GUIGANG GANGTAI 889"
!AIVDM,1,1,,A,H7lca<DlPPPPPPPI4koloP1h>550,0*0E
-"callsign":"YD3747 "
+"callsign":"YD3747"
diff --git a/driver_ais.c b/driver_ais.c
index 953d352..4f488ce 100644
--- a/driver_ais.c
+++ b/driver_ais.c
@@ -26,7 +26,7 @@
* Parse the data from the device
*/
-static void from_sixbit(unsigned char *bitvec, uint start, int count,
char *to)
+static void from_sixbit_untrimmed(unsigned char *bitvec, uint start,
int count, char *to)
/* beginning at bitvec bit start, unpack count sixbit characters */
{
const char sixchr[64] =
@@ -43,14 +43,29 @@ static void from_sixbit(unsigned char *bitvec, uint
start, int count, char *to)
to[i] = newchar;
}
to[i] = '\0';
- /* trim spaces on right end */
- for (i = count - 2; i >= 0; i--)
- if (to[i] == ' ' || to[i] == '@')
- to[i] = '\0';
- else
- break;
}
+static void trim_spaces_on_right_end(char* to)
+/* trim spaces on right end */
+{
+ int i;
+ for (i = strlen(to) - 1; i >= 0; i--)
+ if (to[i] == ' ' || to[i] == '@')
+ to[i] = '\0';
+ else
+ break;
+}
+
+static void from_sixbit(unsigned char *bitvec, uint start, int count,
char *to)
+/* beginning at bitvec bit start, unpack count sixbit characters and
remove trailing
+ * spaces */
+{
+ from_sixbit_untrimmed(bitvec, start, count, to);
+ trim_spaces_on_right_end(to);
+}
+
+
+
bool ais_binary_decode(const struct gpsd_errout_t *errout,
struct ais_t *ais,
const unsigned char *bits, size_t bitlen,
@@ -870,7 +885,7 @@ bool ais_binary_decode(const struct gpsd_errout_t
*errout,
case 21: /* Aid-to-Navigation Report */
RANGE_CHECK(272, 360);
ais->type21.aid_type = UBITS(38, 5);
- from_sixbit((unsigned char *)bits,
+ from_sixbit_untrimmed((unsigned char *)bits,
43, 20, ais->type21.name);
ais->type21.accuracy = UBITS(163, 1);
ais->type21.lon = SBITS(164, 28);
@@ -889,6 +904,7 @@ bool ais_binary_decode(const struct gpsd_errout_t
*errout,
//ais->type21.spare = UBITS(271, 1);
if (strlen(ais->type21.name) == 20 && bitlen > 272)
ENDCHARS(272, ais->type21.name+20);
+ trim_spaces_on_right_end(ais->type21.name);
break;
case 22: /* Channel Management */
PERMISSIVE_LENGTH_CHECK(168)