All Downloads are FREE. Search and download functionalities are using the official Maven repository.

nl.esi.metis.aisparser.impl.AISMessage25Impl Maven / Gradle / Ivy

Go to download

This package supports the parsing of AIS messages in Java. AIS, the Automatic Identification System, is a system aiming at improving maritime safety by exchanging messages between ships, other vehicles in particular aircraft involved in search-and-rescue (SAR), and (fixed) base stations. To be precise, this package support the ITU-R M.1371-4 AIS standard. See our extensive javadoc and in particular the class AISParser for more information on how to use this package. The parser was used in the Poseidon project, and is improved in the Metis project to better handle uncertain information. Both projects were led by the Embedded Systems Institute. In both projects Thales Nederlands was the carrying industrial partner, and multiple Dutch universities participated.

The newest version!
package nl.esi.metis.aisparser.impl;

import nl.esi.metis.aisparser.AISMessage25;
import nl.esi.metis.aisparser.Sixbit;
import nl.esi.metis.aisparser.provenance.VDMMessageProvenance;
import cern.colt.bitvector.BitVector;

/** ESI AIS Parser
 * 
 * Copyright 2011/2012 by Pierre van de Laar (Embedded Systems Institute)
 * Copyright 2008 by Brian C. Lane 
 * All Rights Reserved
 * 
 * @author Pierre van de Laar
 * @author Brian C. Lane
 */


/** Single Slot Binary Message.
 * 
 * Field Nr     Field Name                          NrOf Bits   (from, to )
 * ------------------------------------------------------------------------
 *  1	messageID                               	   6	(   1,   6)
 *  2	repeatIndicator                         	   2	(   7,   8)
 *  3	userID                                  	  30	(   9,  38)
 *  4	destinationIndicator                    	   1	(  39,  39)
 *  5	binaryDataFlag                          	   1	(  40,  40)
 *  6	conditionalIDsAndData                   	 128	(  41, 168)
 *    		destinationID							  30    (  41,  70)					//conditional on destionIndicator
 *  		applicationID							  16    (  41,  56) or (  71,  86)  //conditional on binaryDataFlag
 *      	applicationBinaryData		maximally    128    (  41, 168) or (  71, 168) or (  57, 168) or (  87, 168)
 *                                                  ---- +
 *                       (maximum) number of bits    168
 */
class AISMessage25Impl extends AISMessageImpl implements AISMessage25 {
	public static final int MINLENGTH = 40;
	public static final int MAXLENGTH = 168;

	public static boolean valid(Sixbit content)
	{
		if ( content.length() < MINLENGTH || content.length() > MAXLENGTH)
		{
			return false;
		}
		else
		{
			int minLengthContent = MINLENGTH;
			if (content.getBoolean(DESTINATIONINDICATOR_BITINDEX)) //destinationIndicator
			{
				minLengthContent += 30;
			}
			if (content.getBoolean(BINARYDATAFLAG_BITINDEX)) //	binaryDataFlag 
			{
				minLengthContent += 16;
			}
			return minLengthContent <= content.length();
		}
	}

	/**
	 * Return the difference in available and needed bits to parse this sixbit as an AISMessage
	 * A positive difference indicates that there are more bits available than needed by the standard.
	 * @param sb
	 * @return
	 */
	public static int differenceInBits(Sixbit sb)
	{
		final int available = sb.available();
		if (available > MAXLENGTH)
		{
			return (available - MAXLENGTH);
		}
		else if (available < MINLENGTH)
		{
			return available - MINLENGTH;
		}
		else
		{
			int minLengthContent = MINLENGTH;
			if (sb.getBoolean(DESTINATIONINDICATOR_BITINDEX)) //destinationIndicator
			{
				minLengthContent += 30;
			}
			if (sb.getBoolean(BINARYDATAFLAG_BITINDEX)) //	binaryDataFlag 
			{
				minLengthContent += 16;
			}
			if (available < minLengthContent)
			{
				//too few bits
				return available - minLengthContent;
			}
			else
			{
				//enough bits
				return 0;
			}
		}
	}

	private static final int DESTINATIONINDICATOR_BITINDEX = 39;

	private boolean destinationIndicator;
	/** destinationIndicator
	 * @return boolean value of destinationIndicator (bit 39)
	 */
	public boolean getDestinationIndicator() { return destinationIndicator; }

	private static final int DESTINATIONID_FROM = 41;
	private static final int DESTINATIONID_TO = 70;

	private int destinationID;
	/** destinationID
	 * @return int value of destinationID (30 bits [41,70])
	 * @precondition getDestinationIndicator() == true
	 */
	public int getDestinationID() { return destinationID; }

	private static final int BINARYDATAFLAG_BITINDEX = 40;

	private boolean binaryDataFlag;
	/** binaryDataFlag
	 * @return boolean value of binaryDataFlag (bit 40)
	 */
	public boolean getBinaryDataFlag() { return binaryDataFlag; }

	private int applicationID;
	/** applicationID
	 * @return int value of applicationID (16 bits)
	 * @precondition getBinaryDataFlag() == true
	 */
	public int getApplicationID() { return applicationID; }

	private BitVector applicationBinaryData;
	/** applicationBinaryData
	 * @return BitVector value of applicationBinaryData (maximally 129 bits [41,168])
	 */
	public BitVector getApplicationBinaryData() { return applicationBinaryData; }

	/** AISMessage 25 constructor
	 * @param content AIS content
	 * @param prov the provenance of the message
	 * @precondition valid(content) && AISMessageBase.getMessageId(content)==25
	 */
	public AISMessage25Impl(Sixbit content, VDMMessageProvenance prov)
	{
		super(content, prov);
		
		assert(valid(content));
		assert(getMessageID() == 25);

		destinationIndicator = content.getBoolean(DESTINATIONINDICATOR_BITINDEX);
		binaryDataFlag = content.getBoolean(BINARYDATAFLAG_BITINDEX);
		int startPos = DESTINATIONID_FROM;
		if(destinationIndicator)
		{
			destinationID = content.getIntFromTo(DESTINATIONID_FROM, DESTINATIONID_TO);
			startPos = DESTINATIONID_TO+1;
		}
		if(binaryDataFlag)
		{
			applicationID = content.getIntFromTo(startPos, startPos+15);
			startPos += 16;
		}

		applicationBinaryData = content.getBitVectorFromTo(startPos,content.length());
	}	
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy