package org.josast.AX25;

public class AX25Frame {
	

	public static final byte AX25 = 0;
	public static final byte TXDELAY = 1;
	public static final byte P = 2;
	public static final byte SlotTime = 3;
	public static final byte TXtail = 4;
	public static final byte Fullduplex = 5;
	public static final byte SetHardware = 6;
	public static final byte Exit1 = (byte) 255;
	public static final byte unknow = -1;
	
	byte[] rawData=null;
	
	

	Callsign from;
	Callsign to;
	Callsign [] repeater = new Callsign[8];
	char[] data;
	byte controlID;
	byte protocolID;
	byte type;

public void decodeAX25Frame (byte[] frame , int lenght)
{
	int offset = 0;
	rawData=frame;
	to = decodeCalsign(  frame,offset);
	offset=offset+7;
	from = decodeCalsign(  frame,offset);
	offset=offset+7;
	int i=0;
	boolean valid=true;
	
	while (valid)
	{
		if (frame[offset] !=0x03)
		{
		repeater[i]=decodeCalsign(  frame,offset);
		offset=offset+7;
		i++;
		}
		else 
		{
			valid=false;
		}
		if (i==8)
		{
			valid=false;
			System.out.println("erreur de decodage");
		}
	}
	
	  controlID= frame[offset++];
	  protocolID= frame[offset++];
	  int datalengt = lenght-offset;
	  data = new char[datalengt];
	  int j=0;
	  for (  j =0; j<datalengt;j++)
	  {
		  data[j]=(char) frame[offset++];
	  }

}
	
	
 public  Callsign decodeCalsign(byte[] arg0,int offset) {
		
		Callsign calsign = new Callsign();
		char [] val =new char[6];
		
		for (int i=0;i<6;i++)
		{	

		char c=  (char) ((arg0[i+offset] & 0xFF) >>1 & 0x7F);
		
		val[i] = c;
		}
		calsign.setCallsign(val);
		calsign.setSsid((char)((arg0[6+offset]>>1) & 0xFF));
		return calsign;
	}
 public Callsign getFromClassign ()
 {
	 return from;
 }
 
 public String getData()
 {
	 return String.valueOf(data);
 }

 public String  toString()
 {
 return "from : "+ from.toString()+" to : "+to.toString() +" data :"+ String.valueOf(data);

 }


public String toStringHexa() {
	// TODO Auto-generated method stub
	return "string Hexa";
}


public String toStringHeader() {
	// TODO Auto-generated method stub
	return "string Header";
}

public byte[] getRawData() {
	return rawData;
}


public void setRawData(byte[] rawData) {
	this.rawData = rawData;
}

}
