/**
 *  DENM port implementation
 * 
 *  @author     ETSI / STF424
 *  @version    $URL$
 *              $Id$
 *
 */
package org.etsi.its.adapter.ports;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import org.etsi.common.ByteHelper;
import org.etsi.its.adapter.layers.BtpLayer;
import org.etsi.its.adapter.layers.GnLayer;
import org.etsi.its.adapter.layers.Layer;
import org.etsi.its.codec.TestAdapterMessageTypeEnum;

/**
 *  DENM port implementation
 */
public class DenmPort 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)
     */
	public DenmPort(String portName, String componentName, String lowerStackDesc, String linkLayerAddress) {
		super(portName, componentName, lowerStackDesc, linkLayerAddress);
	}

	/* (non-Javadoc)
	 * @see org.etsi.its.adapter.ports.ProtocolPort#receive(byte[])
	 */
	@Override
	public void receive(byte[] message, Map<String, Object> lowerInfo) {
		
		byte[] msgInd;

		try {
			msgInd = getTaCodec().Encode(TestAdapterMessageTypeEnum.DenmInd, message, (Long)lowerInfo.get(Layer.RECEPTION_TIMESTAMP));
			super.receive(msgInd, lowerInfo);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	/* (non-Javadoc)
     * @see org.etsi.its.adapter.ports.IPort#send(byte[])
     */
    @Override
    public boolean send(byte[] message) {
    
        //TODO: clean this + extract params from MsgReq    (params.put)    
        // Extract MsgReq/Ind + msgOut/msgIn field (one byte) + length field (2 bytes)
        message = ByteHelper.extract(message, 3, message.length - 3);
        
        HashMap<String, Object> params = new HashMap<String, Object>();
        params.put(BtpLayer.BTP_TYPE, BtpLayer.TYPE_A);
        params.put(BtpLayer.BTP_DSTPORT, 500);
        params.put(BtpLayer.BTP_SRCPORT, 500);
        params.put(GnLayer.GN_TYPE, GnLayer.HT_TSB);
        params.put(GnLayer.GN_SUBTYPE, GnLayer.HST_MULTIHOP);
        params.put(GnLayer.GN_NEXTHEADER, "BTP-A");
        return send(message, params);
    }
}
