doron2402 / ais-protocol-decoding

AIVDM/AIVDO protocol decoding
27 stars 13 forks source link

Bug in index.ts when parsing message 24 #16

Open neillj opened 2 years ago

neillj commented 2 years ago

Hi Doron

I think I've found a bug in the src/lib/index.ts when receiving a message 24A. You have

case 24:
    report = parseStaticDataReport(
    this.bitarray, aisType, repeat,
    (session.sequence_id === 1 ? MESSAGE_PART.A : MESSAGE_PART.B),
    mmsi)
    break

but the session object for a 24A with a message of e.g. "!AIVDO,1,1,,B,H3P<........,2*46" won't necessarily have a session object defined at this point as the code relies on messageCounter>1 to define it and here that is 1. This results in it decoding part B by mistake in this case as the identity to 1 fails for an empty object. The message I've quoted is from messages recorded live so, even it isn't following the standard does exist 'in the wild'.

The fix that works for me is

case 24:
    report = parseStaticDataReport(
    this.bitarray, aisType, repeat,
    ((Object.keys(session).length === 0 || session.sequence_id === 1) ? MESSAGE_PART.A : MESSAGE_PART.B),
    mmsi)
    break

where there's a check to make sure the session has been defined (multipart message) or hasn't been (single message).

Thanks for the plugin !

Neill

neillj commented 2 years ago

On further investigation I think actually you need to decode the part type in the parseStaticDataReport method so I've tried this change

export function parseStaticDataReport(
    bitArray: Array<number>,
    aisType: number,
    repeat: number,
    mmsi: string
): Static_Data_Report {
    const messageType = fetchIntByAttr(bitArray, aisType, 'partno') == 0 ? 'A' : 'B'
    if (messageType === 'A') {

which works with the messages I have. I removed the passing in of the part number and get it from the passed in message. This matches what https://www.navcen.uscg.gov/ais-class-b-reports shows for these.

Hope this helps