Search in sources :

Example 1 with UnknownAppErrorCodeError

use of sugar.free.sightparser.error.UnknownAppErrorCodeError 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;
}
Also used : UnknownAppErrorCodeError(sugar.free.sightparser.error.UnknownAppErrorCodeError) ByteBuf(sugar.free.sightparser.pipeline.ByteBuf) SuppressLint(android.annotation.SuppressLint) InvalidAppCRCError(sugar.free.sightparser.error.InvalidAppCRCError) UnknownServiceError(sugar.free.sightparser.error.UnknownServiceError) UnknownAppMessageError(sugar.free.sightparser.error.UnknownAppMessageError) InvalidAppVersionError(sugar.free.sightparser.error.InvalidAppVersionError)

Aggregations

SuppressLint (android.annotation.SuppressLint)1 InvalidAppCRCError (sugar.free.sightparser.error.InvalidAppCRCError)1 InvalidAppVersionError (sugar.free.sightparser.error.InvalidAppVersionError)1 UnknownAppErrorCodeError (sugar.free.sightparser.error.UnknownAppErrorCodeError)1 UnknownAppMessageError (sugar.free.sightparser.error.UnknownAppMessageError)1 UnknownServiceError (sugar.free.sightparser.error.UnknownServiceError)1 ByteBuf (sugar.free.sightparser.pipeline.ByteBuf)1