use of sugar.free.sightparser.pipeline.ByteBuf in project SightRemote by TebbeUbben.
the class AppLayerMessage method deserialize.
public static AppLayerMessage deserialize(ByteBuf byteBuf) throws Exception {
byte version = byteBuf.readByte();
byte service = byteBuf.readByte();
short command = byteBuf.readShort();
short error = byteBuf.readShort();
byte[] data = byteBuf.readBytes();
if (version != VERSION)
throw new InvalidAppVersionError(version, VERSION);
if (!MESSAGES.containsKey(service))
throw new UnknownServiceError(service);
Class<? extends AppLayerMessage> clazz = MESSAGES.get(service).get(command);
if (clazz == null)
throw new UnknownAppMessageError(service, command);
if (error != 0x0000 && error != 0xF0CC) {
Class<? extends AppErrorCodeError> errorClass = Errors.ERRORS.get(error);
if (errorClass != null)
throw errorClass.getConstructor(Class.class, short.class).newInstance(clazz, error);
else
throw new UnknownAppErrorCodeError(clazz, error);
}
AppLayerMessage message = clazz.newInstance();
ByteBuf dataBuf = new ByteBuf(data.length);
dataBuf.putBytes(data);
if (message.inCRC()) {
int crc = dataBuf.getUInt16LE(data.length - 2);
byte[] bytes = dataBuf.getBytes(data.length - 2);
int calculatedCRC = Cryptograph.calculateCRC(bytes);
if (crc != calculatedCRC)
throw new InvalidAppCRCError(crc, calculatedCRC);
dataBuf = new ByteBuf(bytes.length);
dataBuf.putBytes(bytes);
}
message.parse(dataBuf);
return message;
}
use of sugar.free.sightparser.pipeline.ByteBuf in project SightRemote by TebbeUbben.
the class MaxBRAmountBlock method getData.
@Override
public byte[] getData() {
ByteBuf byteBuf = new ByteBuf(2);
byteBuf.putUInt16LE(Helpers.roundDoubleToInt(maximumAmount * 100D));
return byteBuf.getBytes();
}
use of sugar.free.sightparser.pipeline.ByteBuf in project SightRemote by TebbeUbben.
the class MaxBolusAmountBlock method getData.
@Override
public byte[] getData() {
ByteBuf byteBuf = new ByteBuf(2);
byteBuf.putUInt16LE(Helpers.roundDoubleToInt(maximumAmount * 100D));
return byteBuf.getBytes();
}
use of sugar.free.sightparser.pipeline.ByteBuf in project SightRemote by TebbeUbben.
the class NameBlock method getData.
@Override
public byte[] getData() {
ByteBuf byteBuf = new ByteBuf(42);
byteBuf.putUTF16LE(name, 42);
return byteBuf.getBytes();
}
use of sugar.free.sightparser.pipeline.ByteBuf in project SightRemote by TebbeUbben.
the class KeyRequest method getData.
@Override
protected byte[] getData() {
ByteBuf byteBuf = new ByteBuf(288);
byteBuf.putBytes(randomBytes);
byteBuf.putUInt32LE(translateDate());
byteBuf.putBytes(preMasterKey);
return byteBuf.getBytes();
}
Aggregations