package art.servers.pvvserver.protocols.dgt.clv;
|
|
import art.library.utils.common.ArrayUtils;
|
import java.util.ArrayList;
|
import java.util.List;
|
|
|
public class ClvDgtPDU
|
{
|
public int stx;
|
public int etx;
|
public int crc1;
|
public int crc2;
|
public int function;
|
public int address;
|
public boolean ack = false;
|
public int[] information = new int[0];
|
|
private int index = 0;
|
|
public void checkOrder(ClvDgtPDU request) throws Exception
|
{
|
}
|
|
public int readByte() throws Exception
|
{
|
return information[index++];
|
}
|
|
public int readShort() throws Exception
|
{
|
return (readByte() << 8) + readByte();
|
}
|
|
public int readInt() throws Exception
|
{
|
return (readByte() << 24) + (readByte() << 16) + (readByte() << 8) + readByte();
|
}
|
|
public long readLong() throws Exception
|
{
|
long res = ((long) readByte() << 56);
|
res = res + ((long) readByte() << 48);
|
res = res + ((long) readByte() << 40);
|
res = res + ((long) readByte() << 32);
|
res = res + ((long) readByte() << 24);
|
res = res + ((long) readByte() << 16);
|
res = res + ((long) readByte() << 8);
|
res = res + (long) readByte();
|
return (res);
|
}
|
|
public int[] readFully() throws Exception
|
{
|
int[] resultado = new int[information.length - index];
|
System.arraycopy(information, index, resultado, 0, resultado.length);
|
return resultado;
|
}
|
|
public int[] readIntArray(int count) throws Exception
|
{
|
int[] resultado = new int[count];
|
System.arraycopy(information, index, resultado, 0, resultado.length);
|
index = index + count;
|
return resultado;
|
}
|
|
public int[] readIntArrayUntilValue(int limit) throws Exception
|
{
|
List<Integer> list = new ArrayList<Integer>();
|
int value = readByte();
|
while (value != limit)
|
{
|
list.add(value);
|
value = readByte();
|
}
|
|
int[] result = new int[list.size()];
|
for (int i=0; i<list.size(); i++)
|
{
|
result[i] = list.get(i).intValue();
|
}
|
|
return result;
|
}
|
|
public boolean eof()
|
{
|
return (index == information.length);
|
}
|
|
public byte[] createMessage() throws Exception
|
{
|
int[] result = new int[6 + information.length];
|
int index = 0;
|
result[index++] = stx;
|
result[index++] = address;
|
result[index++] = function;
|
for (int i = 0; i < information.length; i++)
|
{
|
result[index++] = information[i];
|
}
|
|
byte[] crc = checksum(result, 1, index - 1);
|
result[index++] = crc[1];
|
result[index++] = crc[0];
|
result[index++] = etx;
|
|
return (ArrayUtils.toByteArray(addStuffing(result)));
|
}
|
|
|
public byte obtenerByte(int valor)
|
{
|
try
|
{
|
switch (valor)
|
{
|
case 0 : return((byte)'0');
|
case 1 : return((byte)'1');
|
case 2 : return((byte)'2');
|
case 3 : return((byte)'3');
|
case 4 : return((byte)'4');
|
case 5 : return((byte)'5');
|
case 6 : return((byte)'6');
|
case 7 : return((byte)'7');
|
case 8 : return((byte)'8');
|
case 9 : return((byte)'9');
|
case 10 : return((byte)'A');
|
case 11 : return((byte)'B');
|
case 12 : return((byte)'C');
|
case 13 : return((byte)'D');
|
case 14 : return((byte)'E');
|
case 15 : return((byte)'F');
|
}
|
}
|
catch (Exception e)
|
{
|
}
|
|
return((byte)'0');
|
}
|
|
|
public byte obtenerByteAscii(byte b)
|
{
|
try
|
{
|
switch (b)
|
{
|
case 0 : return((byte)'0');
|
case 1 : return((byte)'1');
|
case 2 : return((byte)'2');
|
case 3 : return((byte)'3');
|
case 4 : return((byte)'4');
|
case 5 : return((byte)'5');
|
case 6 : return((byte)'6');
|
case 7 : return((byte)'7');
|
case 8 : return((byte)'8');
|
case 9 : return((byte)'9');
|
case 10 : return((byte)'A');
|
case 11 : return((byte)'B');
|
case 12 : return((byte)'C');
|
case 13 : return((byte)'D');
|
case 14 : return((byte)'E');
|
case 15 : return((byte)'F');
|
}
|
|
}
|
catch (Exception e)
|
{
|
}
|
|
return((byte)'0');
|
}
|
|
|
public byte[] checksum(int[] trama, int inicio, int fin) throws Exception
|
{
|
try
|
{
|
short acum,dato;
|
long crc;
|
int i,j;
|
|
crc=0;
|
acum=(short)crc;
|
for(i=inicio;i<=fin;i++)
|
{
|
int valor = trama[i];
|
|
dato = (short)valor;
|
acum=(short)crc;
|
|
dato<<=8;
|
for(j=8;j>0;j--)
|
{
|
if(((dato^acum) & 0x8000) != 0)
|
acum =(short)((acum<<1)^0x1021);
|
else
|
acum<<=1;
|
|
dato<<=1;
|
}
|
|
crc=acum;
|
}
|
|
long aux = acum;
|
|
if (aux < 0)
|
aux += (Short.MAX_VALUE*2 + 2);
|
crc=aux;
|
|
byte[] res = new byte[2];
|
|
res[0]=(byte) crc;
|
crc=(long)(crc/256);
|
res[1]=(byte) crc;
|
|
return(res);
|
}
|
catch (Exception e)
|
{
|
throw e;
|
}
|
}
|
|
|
public int[] addStuffing(int[] trama) throws Exception
|
{
|
try
|
{
|
int[] aux = new int[100096];
|
int apuntador = 0;
|
|
for (int i=0; i<trama.length; i++)
|
{
|
if ((i>=1) && (i<trama.length-1))
|
{
|
if ((trama[i] == 2) || (trama[i] == 3) ||
|
(trama[i] == 5) || (trama[i] == 6) ||
|
(trama[i] == 16))
|
{
|
aux[apuntador++] = 0x10;
|
aux[apuntador++] = (trama[i]+128);
|
}
|
else
|
aux[apuntador++] = trama[i];
|
}
|
else
|
aux[apuntador++] = trama[i];
|
}
|
|
int[] resultado = new int[apuntador];
|
System.arraycopy(aux, 0, resultado, 0, apuntador);
|
|
return(resultado);
|
}
|
catch (Exception e)
|
{
|
throw e;
|
}
|
}
|
|
|
public int[] removeStuffing(int[] trama) throws Exception
|
{
|
try
|
{
|
int[] aux = new int[100096];
|
int apuntador = 0;
|
|
for (int i=0; i<trama.length; i++)
|
{
|
if (trama[i] == 16)
|
{
|
aux[apuntador++] = (trama[i+1]-128);
|
i++;
|
}
|
else
|
aux[apuntador++] = trama[i];
|
}
|
|
int[] resultado = new int[apuntador];
|
System.arraycopy(aux, 0, resultado, 0, apuntador);
|
|
return(resultado);
|
}
|
catch (Exception e)
|
{
|
throw e;
|
}
|
}
|
|
}
|