Jump to content

Analysing maritime AIVDM payloads


iamtheky
 Share

Recommended Posts

I am participating in a DoD sponsored event this weekend to glean anomalous events from standard maritime radio traffic.  In preparation I have been tearing open the payload to make sure I understand what parts should go where.  In the next few hours I will be putting in all the If statements to make sure each field is within the expected range of values (like the radio channel being something other than A,B,1 or 2). And then later I expect I will be checking those values against relative data (like if a ship stays on  the same 12 digit latitude for any length of time without vary, or the vice and a  heading changes drastically).  Also I need to do the lat/long math, from Bin to Seconds.

Heres a link all about AIS messages:      http://www.it-digin.com/blog/?p=20

Here's about the encoded AIVDM sentences:  http://catb.org/gpsd/AIVDM.html

Here is the parsing of the AIS report.  I used a JSON with known good values that also contained the payload so I could unpack that and check my work.  If you see anything that can be improved upon do it, I will post more data as I get it.

#include<array.au3>


$sPositionReport = '{"mmsi":565835000,"heading":42,"latitude":39.1834333333,"nmea":"!AIVDM,1,1,0,B,18KWlv001qrRko0FJt;1WiDN0H8b,0*2A","course":41.5,"type":1,"speed":12.1,"timestamp":"2017-01-01T11:10:044+00:00","longitude":-76.2816533333}'

msgbox(0, '' , $sPositionReport)

$aSplit = stringsplit(stringtrimleft(StringTrimRight($sPositionReport , 1) , 2) , ',"' , 3)

local $aFrmtValue[ubound($aSplit)]
local $aFrmtKey[ubound($aSplit)]
local $aOut[ubound($aSplit)][2]

For $i = 0 to ubound($aSplit) - 1
    $aFrmtValue[$i] = stringtrimleft($aSplit[$i] , stringinstr($aSplit[$i] , ":"))
    $aFrmtValue[$i] = stringreplace($aFrmtValue[$i] , '"' , "")

;~ msgbox(0, '' , $aSplit[$i])
    $aFrmtKey[$i] = stringleft($aSplit[$i] , stringinstr($aSplit[$i] , ":") - 2)

    $aOut[$i][0] = $aFrmtKey[$i]
    $aOut[$i][1] = $aFrmtValue[$i]
Next

;~ _ArrayDisplay($aFrmtKey , "Key")
;~ _ArrayDisplay($aFrmtValue , "Value")
_ArrayDisplay($aOut , "OUT")

;course [4] = 0-3599 with 3600 as a default, 3601-4095 should not be used
;heading [1] = 0-359, 511 is the default
;speed [6] = 0 - 102.2 , 1023 speed is unavailable , 1022 speed is 102.2 or higher

;AIVDM - bang is not technically required to lead off
;~ [1] = !AIVDM
;~ [2] = Number of fragments - (82 character max in the AIVDM packet)
;~ [3] = fragment number of this sentence - (shouldnt be larger than field 2)
;~ [4] = sequential message ID  for multi sentence msgs
;~ [5] = radio channel code (A,B or 1,2)  - IF A or B exists the msg should be AIVDM
;~ [6] = payload (88-95 are not used)
;~ [7] = number of fill bits required. data integrity checksum is the * separated suffix.

;PAYLOAD_TEST

$aPayload = stringsplit($aOut[3][1] , "," , 2)

_ArrayDisplay($aPayload , "Payload")

;~ $sData = "18KWlv001qrRko0FJt;1WiDN0H8b"

$sData = $aPayload[5]

$aAsc = StringToASCIIArray($sData)  ; any ASCII over 119 is invalid , ascii 64-87 and 88-95 are not used

for $i = 0 to ubound($aAsc) - 1
    If $aAsc[$i] < 48 Then msgbox(0, '' , "asc less than 48")
    If $aAsc[$i] > 88 And $aAsc[$i] < 95 Then msgbox(0, '' , "asc in invalid range 88-95")
    If $aAsc[$i] > 119 Then msgbox(0, '' , "asc above 119")
next

_ArrayDisplay($aAsc)

;subtract 48 from each

for $i = 0 to ubound($aAsc) - 1
    $aAsc[$i] = number($aAsc[$i] - 48)
    $aAsc[$i] = $aAsc[$i] > 40 ? $aAsc[$i] - 8 : $aAsc[$i] ; if still over 40 subtract 8
Next

_ArrayDisplay($aAsc) ; armored payload

for $i = 0 to UBound($aAsc) - 1
    If $aAsc[$i] = "0" Then $aAsc[$i] = "000000"
    If $aAsc[$i] = "1" Then $aAsc[$i] = "000001"
    If $aAsc[$i] = "8" Then $aAsc[$i] = "001000"
    If $aAsc[$i] = "27" Then $aAsc[$i] = "011011"
    If $aAsc[$i] = "39" Then $aAsc[$i] = "100111"
    If $aAsc[$i] = "62" Then $aAsc[$i] = "111110"
    If $aAsc[$i] = "57" Then $aAsc[$i] = "111001"
    If $aAsc[$i] = "58" Then $aAsc[$i] = "111010"
    If $aAsc[$i] = "51" Then $aAsc[$i] = "110011"
    If $aAsc[$i] = "55" Then $aAsc[$i] = "110111"
    If $aAsc[$i] = "34" Then $aAsc[$i] = "100010"
    If $aAsc[$i] = "22" Then $aAsc[$i] = "010110"
    If $aAsc[$i] = "26" Then $aAsc[$i] = "011010"
    If $aAsc[$i] = "60" Then $aAsc[$i] = "111100"
    If $aAsc[$i] = "11" Then $aAsc[$i] = "001011"
    If $aAsc[$i] = "49" Then $aAsc[$i] = "110001"
    If $aAsc[$i] = "20" Then $aAsc[$i] = "010100"
    If $aAsc[$i] = "30" Then $aAsc[$i] = "011110"
    If $aAsc[$i] = "24" Then $aAsc[$i] = "011000"
    If $aAsc[$i] = "42" Then $aAsc[$i] = "101010"
    If $aAsc[$i] = "52" Then $aAsc[$i] = "110100"


next

_ArrayDisplay($aAsc) ;bit stream


$sBitStream = _ArrayToString($aAsc , "")

$sMsgType = stringleft($sBitStream , 6)
$sRptIndicator = stringmid($sBitStream , 7 , 2)
$sMMSI = StringMid($sBitStream , 9 , 30)
$sNavStatus   = StringMid($sBitStream , 39 , 4)
$sRateOfTurn = StringMid($sBitStream , 43 , 8)
$sSpeedOverGrnd = StringMid($sBitStream , 51 , 10)
$sPositionAccuracy = StringMid($sBitStream , 61 , 1)
$slongitude = StringMid($sBitStream , 62 , 28)
$slatitude = StringMid($sBitStream , 90 , 27)
$sCourseOverGround = StringMid($sBitStream , 117 , 12)
$sHeading = StringMid($sBitStream , 129 , 9)
$sTimeStamp = StringMid($sBitStream , 138 , 6)
$sManeuverIndicator = StringMid($sBitStream , 144 , 2)
$sSpare = StringMid($sBitStream , 146 , 3) ;unused
$sRAIMflag = StringMid($sBitStream , 149 , 1)
$sRadioStatus = StringMid($sBitStream , 150 , 19) ;ending at 168 (or 28 blocks of 6)


local $aBitStream[16] = [$sMsgType , $sRptIndicator , $sMMSI , $sNavStatus , $sRateOfTurn , $sSpeedOverGrnd , $sPositionAccuracy , $slongitude , _
$slatitude, $sCourseOverGround , $sHeading , $sTimeStamp , $sManeuverIndicator , $sSpare , $sRAIMflag , $sRadioStatus]

_ArrayDisplay($aBitStream)


;checking my work

msgbox(0, 'MMSI_CHECK' , "Payload: " & _BinaryToDec($sMMSI) & @LF & "JSON     : " & $aOut[0][1])

msgbox(0, 'course_check' ,"Payload: " & _BinaryToDec($sCourseOverGround) / 10 & @LF & "JSON     : " & $aOut[4][1])

;------------------------------



Func _BinaryToDec($strBin)
Local $Return
Local $lngResult
Local $intIndex

If StringRegExp($strBin,'[0-1]') then
$lngResult = 0
For $intIndex = StringLen($strBin) to 1 step -1
$strDigit = StringMid($strBin, $intIndex, 1)
Select
case $strDigit="0"
; do nothing
case $strDigit="1"
$lngResult = $lngResult + (2 ^ (StringLen($strBin)-$intIndex))
case else
; invalid binary digit, so the whole thing is invalid
$lngResult = 0
$intIndex = 0 ; stop the loop
EndSelect
Next

$Return = $lngResult
    Return $Return
Else
    MsgBox(0,"Error","Wrong input, try again ...")
    Return
EndIf
EndFunc

 

 

Edited by iamtheky

,-. .--. ________ .-. .-. ,---. ,-. .-. .-. .-.
|(| / /\ \ |\ /| |__ __||| | | || .-' | |/ / \ \_/ )/
(_) / /__\ \ |(\ / | )| | | `-' | | `-. | | / __ \ (_)
| | | __ | (_)\/ | (_) | | .-. | | .-' | | \ |__| ) (
| | | | |)| | \ / | | | | | |)| | `--. | |) \ | |
`-' |_| (_) | |\/| | `-' /( (_)/( __.' |((_)-' /(_|
'-' '-' (__) (__) (_) (__)

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...