MapemSpatemPort.java 4.01 KB
Newer Older
package org.etsi.its.adapter.ports;

import java.util.HashMap;
import java.util.Map;

garciay's avatar
garciay committed
import org.etsi.adapter.TERFactory;
import org.etsi.common.ByteHelper;
garciay's avatar
garciay committed
import org.etsi.its.adapter.SecurityHelper;
import org.etsi.its.adapter.layers.BtpLayer;
import org.etsi.its.adapter.layers.GnLayer;

garciay's avatar
garciay committed
public class MapemSpatemPort extends ProtocolPort {

    /**
     * Constructor
     * @param   portName        Name of the port
     * @param   componentName   Name of the component owning this port instance
     * @param   lowerStackDesc  Description of the port's lower stack in the form "Layer/Layer/Layer/..."
     * @param  linkLayerAddress    Link-layer address to be used by this port as source address (null if not applicable)
     */
garciay's avatar
garciay committed
    public MapemSpatemPort(String portName, String componentName, String lowerStackDesc, String linkLayerAddress) {
        super(portName, componentName, lowerStackDesc, linkLayerAddress);
    }
    
    /* (non-Javadoc)
     * @see org.etsi.its.adapter.ports.ProtocolPort#receive(byte[], java.util.Map)
     */
    @Override
    public void receive(byte[] message, Map<String, Object> lowerInfo) {
garciay's avatar
garciay committed
//      TERFactory.getInstance().logDebug(">>> MapemSpatemPort.receive: " + ByteHelper.byteArrayToString(message));
garciay's avatar
garciay committed
        if ((message[1] != 0x04) && (message[1] != 0x05)) { // Check that received packet has MAPEM/SPATEM message id - See ETSI TS 102 894
            TERFactory.getInstance().logDebug("MapemSpatemPort.receive: drop packet " + ByteHelper.byteArrayToString(message));
            return; // Drop it
        }
        
        // Encode with MAPEM/SPATEM indication header
        byte[] msgInd = ByteHelper.concat(
            message, 
garciay's avatar
garciay committed
            new byte[] { (Byte) lowerInfo.get(GnLayer.GN_NEXTHEADER) },
            ByteHelper.intToByteArray((Integer) lowerInfo.get(GnLayer.GN_TYPE), 1),
            ByteHelper.intToByteArray((Integer) lowerInfo.get(GnLayer.GN_SUBTYPE), 1),
            ByteHelper.intToByteArray((Integer) lowerInfo.get(GnLayer.GN_LIFETIME), Integer.SIZE / Byte.SIZE),
            ByteHelper.intToByteArray((Integer) lowerInfo.get(GnLayer.GN_TRAFFICCLASS), 1),
            (byte[]) lowerInfo.get(BtpLayer.BTP_DSTPORT),
            (byte[]) lowerInfo.get(BtpLayer.BTP_DSTPORTINFO)
        );
garciay's avatar
garciay committed
        // Add security info to pass to the ATS
        if (lowerInfo.get(SecurityHelper.SEC_SSP) == null) {
            byte[] buf = new byte[32];
garciay's avatar
garciay committed
            msgInd = ByteHelper.concat(msgInd, buf); 
        } else {
            msgInd = ByteHelper.concat(msgInd, (byte[])lowerInfo.get(SecurityHelper.SEC_SSP)); 
        }
        if (lowerInfo.get(SecurityHelper.SEC_ITS_AID) == null) { // It shall not be possible to have SSP absent and ATS_AID present but...
            msgInd = ByteHelper.concat(msgInd, new byte[] { (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00 }); 
            
        } else {
            msgInd = ByteHelper.concat(msgInd, (byte[])lowerInfo.get(SecurityHelper.SEC_ITS_AID)); 
        }
        super.receive(msgInd, lowerInfo);
    }
    
    /* (non-Javadoc)
     * @see org.etsi.its.adapter.ports.IPort#send(byte[])
     */
    @Override
    public boolean send(byte[] message) {
    
        HashMap<String, Object> params = new HashMap<String, Object>();
        params.put(BtpLayer.BTP_TYPE, BtpLayer.TYPE_B);
garciay's avatar
garciay committed
        if (message[1] == 0x5) { // MAPEM, see ITS-Container
            params.put(BtpLayer.BTP_DSTPORT, 2003);
garciay's avatar
garciay committed
        } else if (message[1] == 0x4) { // SPATEM, see ITS-Container
            params.put(BtpLayer.BTP_DSTPORT, 2004);
garciay's avatar
garciay committed
        } else {
            params.put(BtpLayer.BTP_DSTPORT, 0);
garciay's avatar
garciay committed
        params.put(BtpLayer.BTP_SRCPORT, 0);
        params.put(GnLayer.GN_TYPE, GnLayer.HT_TSB);
        params.put(GnLayer.GN_SUBTYPE, GnLayer.HST_MULTIHOP);
        params.put(GnLayer.GN_LATITUDE, ByteHelper.byteArrayToLong(management.getLatitude()));
        params.put(GnLayer.GN_LONGITUDE, ByteHelper.byteArrayToLong(management.getLongitude()));
        params.put(GnLayer.GN_NEXTHEADER, "BTP-B");
        return send(message, params);
    }
} // End of class MapSpatPort