Example usage for io.netty.buffer ByteBuf readUnsignedShortLE

List of usage examples for io.netty.buffer ByteBuf readUnsignedShortLE

Introduction

In this page you can find the example usage for io.netty.buffer ByteBuf readUnsignedShortLE.

Prototype

public abstract int readUnsignedShortLE();

Source Link

Document

Gets an unsigned 16-bit short integer at the current readerIndex in the Little Endian Byte Order and increases the readerIndex by 2 in this buffer.

Usage

From source file:org.traccar.protocol.NavigilProtocolDecoder.java

License:Apache License

private Position parseUnitReport(DeviceSession deviceSession, ByteBuf buf, int sequenceNumber) {
    Position position = new Position(getProtocolName());

    position.setValid(true);/*from   w w  w . j a  v  a 2  s.  co m*/
    position.set(Position.KEY_INDEX, sequenceNumber);
    position.setDeviceId(deviceSession.getDeviceId());

    buf.readUnsignedShortLE(); // report trigger
    position.set(Position.KEY_FLAGS, buf.readUnsignedShortLE());

    position.setLatitude(buf.readIntLE() * 0.0000001);
    position.setLongitude(buf.readIntLE() * 0.0000001);
    position.setAltitude(buf.readUnsignedShortLE());

    position.set(Position.KEY_SATELLITES, buf.readUnsignedShortLE());
    position.set(Position.KEY_SATELLITES_VISIBLE, buf.readUnsignedShortLE());
    position.set("gpsAntennaState", buf.readUnsignedShortLE());

    position.setSpeed(buf.readUnsignedShortLE() * 0.194384);
    position.setCourse(buf.readUnsignedShortLE());

    position.set(Position.KEY_ODOMETER, buf.readUnsignedIntLE());
    position.set(Position.KEY_DISTANCE, buf.readUnsignedIntLE());

    position.set(Position.KEY_BATTERY, buf.readUnsignedShortLE() * 0.001);

    position.set(Position.KEY_CHARGE, buf.readUnsignedShortLE());

    position.setTime(convertTimestamp(buf.readUnsignedIntLE()));

    return position;
}

From source file:org.traccar.protocol.NavigilProtocolDecoder.java

License:Apache License

private Position parseTg2Report(DeviceSession deviceSession, ByteBuf buf, int sequenceNumber) {
    Position position = new Position(getProtocolName());

    position.setValid(true);//w ww  . ja v a 2  s .co m
    position.set(Position.KEY_INDEX, sequenceNumber);
    position.setDeviceId(deviceSession.getDeviceId());

    buf.readUnsignedShortLE(); // report trigger
    buf.readUnsignedByte(); // reserved
    buf.readUnsignedByte(); // assisted GPS age

    position.setTime(convertTimestamp(buf.readUnsignedIntLE()));

    position.setLatitude(buf.readIntLE() * 0.0000001);
    position.setLongitude(buf.readIntLE() * 0.0000001);
    position.setAltitude(buf.readUnsignedShortLE());

    position.set(Position.KEY_SATELLITES, buf.readUnsignedByte());
    position.set(Position.KEY_SATELLITES_VISIBLE, buf.readUnsignedByte());

    position.setSpeed(buf.readUnsignedShortLE() * 0.194384);
    position.setCourse(buf.readUnsignedShortLE());

    position.set(Position.KEY_ODOMETER, buf.readUnsignedIntLE());
    position.set("maximumSpeed", buf.readUnsignedShortLE());
    position.set("minimumSpeed", buf.readUnsignedShortLE());

    position.set(Position.PREFIX_IO + 1, buf.readUnsignedShortLE()); // VSAUT1 voltage
    position.set(Position.PREFIX_IO + 2, buf.readUnsignedShortLE()); // VSAUT2 voltage
    position.set(Position.PREFIX_IO + 3, buf.readUnsignedShortLE()); // solar voltage

    position.set(Position.KEY_BATTERY, buf.readUnsignedShortLE() * 0.001);

    return position;
}

From source file:org.traccar.protocol.NavigilProtocolDecoder.java

License:Apache License

private Position parseSnapshot4(DeviceSession deviceSession, ByteBuf buf, int sequenceNumber) {
    Position position = new Position(getProtocolName());

    position.set(Position.KEY_INDEX, sequenceNumber);
    position.setDeviceId(deviceSession.getDeviceId());

    buf.readUnsignedByte(); // report trigger
    buf.readUnsignedByte(); // position fix source
    buf.readUnsignedByte(); // GNSS fix quality
    buf.readUnsignedByte(); // GNSS assistance age

    long flags = buf.readUnsignedIntLE();
    position.setValid((flags & 0x0400) == 0x0400);

    position.setTime(convertTimestamp(buf.readUnsignedIntLE()));

    position.setLatitude(buf.readIntLE() * 0.0000001);
    position.setLongitude(buf.readIntLE() * 0.0000001);
    position.setAltitude(buf.readUnsignedShortLE());

    position.set(Position.KEY_SATELLITES, buf.readUnsignedByte());
    position.set(Position.KEY_SATELLITES_VISIBLE, buf.readUnsignedByte());

    position.setSpeed(buf.readUnsignedShortLE() * 0.194384);
    position.setCourse(buf.readUnsignedShortLE() * 0.1);

    position.set("maximumSpeed", buf.readUnsignedByte());
    position.set("minimumSpeed", buf.readUnsignedByte());
    position.set(Position.KEY_ODOMETER, buf.readUnsignedIntLE());

    position.set(Position.PREFIX_IO + 1, buf.readUnsignedByte()); // supply voltage 1
    position.set(Position.PREFIX_IO + 2, buf.readUnsignedByte()); // supply voltage 2
    position.set(Position.KEY_BATTERY, buf.readUnsignedShortLE() * 0.001);

    return position;
}

From source file:org.traccar.protocol.NavigilProtocolDecoder.java

License:Apache License

private Position parseTrackingData(DeviceSession deviceSession, ByteBuf buf, int sequenceNumber,
        long timestamp) {
    Position position = new Position(getProtocolName());

    position.set(Position.KEY_INDEX, sequenceNumber);
    position.setDeviceId(deviceSession.getDeviceId());
    position.setTime(convertTimestamp(timestamp));

    buf.readUnsignedByte(); // tracking mode

    short flags = buf.readUnsignedByte();
    position.setValid((flags & 0x01) == 0x01);

    buf.readUnsignedShortLE(); // duration

    position.setLatitude(buf.readIntLE() * 0.0000001);
    position.setLongitude(buf.readIntLE() * 0.0000001);

    position.setSpeed(UnitsConverter.knotsFromKph(buf.readUnsignedByte()));
    position.setCourse(buf.readUnsignedByte() * 2.0);

    position.set(Position.KEY_SATELLITES, buf.readUnsignedByte());
    position.set(Position.KEY_BATTERY, buf.readUnsignedShortLE() * 0.001);
    position.set(Position.KEY_ODOMETER, buf.readUnsignedIntLE());

    return position;
}

From source file:org.traccar.protocol.NavigilProtocolDecoder.java

License:Apache License

@Override
protected Object decode(Channel channel, SocketAddress remoteAddress, Object msg) throws Exception {

    ByteBuf buf = (ByteBuf) msg;

    buf.readUnsignedByte(); // protocol version
    buf.readUnsignedByte(); // version id
    int sequenceNumber = buf.readUnsignedShortLE();
    int messageId = buf.readUnsignedShortLE();
    buf.readUnsignedShortLE(); // length
    int flags = buf.readUnsignedShortLE();
    buf.readUnsignedShortLE(); // checksum

    DeviceSession deviceSession = getDeviceSession(channel, remoteAddress,
            String.valueOf(buf.readUnsignedIntLE()));
    if (deviceSession == null) {
        return null;
    }//from  w w  w . ja v a  2s  .  c om

    long timestamp = buf.readUnsignedIntLE();

    if ((flags & 0x1) == 0x0) {
        sendAcknowledgment(channel, sequenceNumber);
    }

    switch (messageId) {
    case MSG_UNIT_REPORT:
        return parseUnitReport(deviceSession, buf, sequenceNumber);
    case MSG_TG2_REPORT:
        return parseTg2Report(deviceSession, buf, sequenceNumber);
    case MSG_POSITION_REPORT:
        return parsePositionReport(deviceSession, buf, sequenceNumber, timestamp);
    case MSG_POSITION_REPORT_2:
        return parsePositionReport2(deviceSession, buf, sequenceNumber, timestamp);
    case MSG_SNAPSHOT4:
        return parseSnapshot4(deviceSession, buf, sequenceNumber);
    case MSG_TRACKING_DATA:
        return parseTrackingData(deviceSession, buf, sequenceNumber, timestamp);
    default:
        return null;
    }
}

From source file:org.traccar.protocol.NavisProtocolDecoder.java

License:Apache License

private Position parseNtcbPosition(DeviceSession deviceSession, ByteBuf buf) {
    Position position = new Position(getProtocolName());

    position.setDeviceId(deviceSession.getDeviceId());

    int format;/* ww  w. j ava 2s  . c om*/
    if (buf.getUnsignedByte(buf.readerIndex()) == 0) {
        format = buf.readUnsignedShortLE();
    } else {
        format = buf.readUnsignedByte();
    }
    position.set("format", format);

    position.set(Position.KEY_INDEX, buf.readUnsignedIntLE());
    position.set(Position.KEY_EVENT, buf.readUnsignedShortLE());

    buf.skipBytes(6); // event time

    short armedStatus = buf.readUnsignedByte();
    if (isFormat(format, F10, F20, F30, F40, F50, F51, F52)) {
        position.set(Position.KEY_ARMED, BitUtil.to(armedStatus, 7));
        if (BitUtil.check(armedStatus, 7)) {
            position.set(Position.KEY_ALARM, Position.ALARM_GENERAL);
        }
    } else if (isFormat(format, F60)) {
        position.set(Position.KEY_ARMED, BitUtil.check(armedStatus, 0));
        if (BitUtil.check(armedStatus, 1)) {
            position.set(Position.KEY_ALARM, Position.ALARM_GENERAL);
        }
    }

    position.set(Position.KEY_STATUS, buf.readUnsignedByte());
    position.set(Position.KEY_RSSI, buf.readUnsignedByte());

    if (isFormat(format, F10, F20, F30)) {
        int output = buf.readUnsignedShortLE();
        position.set(Position.KEY_OUTPUT, output);
        for (int i = 0; i < 16; i++) {
            position.set(Position.PREFIX_OUT + (i + 1), BitUtil.check(output, i));
        }
    } else if (isFormat(format, F50, F51, F52)) {
        short extField = buf.readUnsignedByte();
        position.set(Position.KEY_OUTPUT, BitUtil.to(extField, 2));
        position.set(Position.PREFIX_OUT + 1, BitUtil.check(extField, 0));
        position.set(Position.PREFIX_OUT + 2, BitUtil.check(extField, 1));
        position.set(Position.KEY_SATELLITES, BitUtil.from(extField, 2));
    } else if (isFormat(format, F40, F60)) {
        short output = buf.readUnsignedByte();
        position.set(Position.KEY_OUTPUT, BitUtil.to(output, 4));
        for (int i = 0; i < 4; i++) {
            position.set(Position.PREFIX_OUT + (i + 1), BitUtil.check(output, i));
        }
    }

    if (isFormat(format, F10, F20, F30, F40)) {
        int input = buf.readUnsignedShortLE();
        position.set(Position.KEY_INPUT, input);
        if (!isFormat(format, F40)) {
            for (int i = 0; i < 16; i++) {
                position.set(Position.PREFIX_IN + (i + 1), BitUtil.check(input, i));
            }
        } else {
            position.set(Position.PREFIX_IN + 1, BitUtil.check(input, 0));
            position.set(Position.PREFIX_IN + 2, BitUtil.check(input, 1));
            position.set(Position.PREFIX_IN + 3, BitUtil.check(input, 2));
            position.set(Position.PREFIX_IN + 4, BitUtil.check(input, 3));
            position.set(Position.PREFIX_IN + 5, BitUtil.between(input, 4, 7));
            position.set(Position.PREFIX_IN + 6, BitUtil.between(input, 7, 10));
            position.set(Position.PREFIX_IN + 7, BitUtil.between(input, 10, 12));
            position.set(Position.PREFIX_IN + 8, BitUtil.between(input, 12, 14));
        }
    } else if (isFormat(format, F50, F51, F52, F60)) {
        short input = buf.readUnsignedByte();
        position.set(Position.KEY_INPUT, input);
        for (int i = 0; i < 8; i++) {
            position.set(Position.PREFIX_IN + (i + 1), BitUtil.check(input, i));
        }
    }

    position.set(Position.KEY_POWER, buf.readUnsignedShortLE() * 0.001);
    position.set(Position.KEY_BATTERY, buf.readUnsignedShortLE() * 0.001);

    if (isFormat(format, F10, F20, F30)) {
        position.set(Position.PREFIX_TEMP + 1, buf.readShortLE());
    }

    if (isFormat(format, F10, F20, F50, F51, F52, F60)) {
        position.set(Position.PREFIX_ADC + 1, buf.readUnsignedShortLE());
        position.set(Position.PREFIX_ADC + 2, buf.readUnsignedShortLE());
    }
    if (isFormat(format, F60)) {
        position.set(Position.PREFIX_ADC + 3, buf.readUnsignedShortLE());
    }

    // Impulse counters
    if (isFormat(format, F20, F50, F51, F52, F60)) {
        buf.readUnsignedIntLE();
        buf.readUnsignedIntLE();
    }

    if (isFormat(format, F60)) {
        // Fuel
        buf.readUnsignedShortLE();
        buf.readUnsignedShortLE();
        buf.readByte();
        buf.readShortLE();
        buf.readByte();
        buf.readUnsignedShortLE();
        buf.readByte();
        buf.readUnsignedShortLE();
        buf.readByte();
        buf.readUnsignedShortLE();
        buf.readByte();
        buf.readUnsignedShortLE();
        buf.readByte();
        buf.readUnsignedShortLE();
        buf.readByte();
        buf.readUnsignedShortLE();
        buf.readByte();
        buf.readUnsignedShortLE();

        position.set(Position.PREFIX_TEMP + 1, buf.readByte());
        position.set(Position.PREFIX_TEMP + 2, buf.readByte());
        position.set(Position.PREFIX_TEMP + 3, buf.readByte());
        position.set(Position.PREFIX_TEMP + 4, buf.readByte());
        position.set(Position.KEY_AXLE_WEIGHT, buf.readIntLE());
        position.set(Position.KEY_RPM, buf.readUnsignedShortLE());
    }

    if (isFormat(format, F20, F50, F51, F52, F60)) {
        int navSensorState = buf.readUnsignedByte();
        position.setValid(BitUtil.check(navSensorState, 1));
        if (isFormat(format, F60)) {
            position.set(Position.KEY_SATELLITES, BitUtil.from(navSensorState, 2));
        }

        DateBuilder dateBuilder = new DateBuilder()
                .setTime(buf.readUnsignedByte(), buf.readUnsignedByte(), buf.readUnsignedByte())
                .setDateReverse(buf.readUnsignedByte(), buf.readUnsignedByte() + 1, buf.readUnsignedByte());
        position.setTime(dateBuilder.getDate());

        if (isFormat(format, F60)) {
            position.setLatitude(buf.readIntLE() / 600000.0);
            position.setLongitude(buf.readIntLE() / 600000.0);
            position.setAltitude(buf.readIntLE() * 0.1);
        } else {
            position.setLatitude(buf.readFloatLE() / Math.PI * 180);
            position.setLongitude(buf.readFloatLE() / Math.PI * 180);
        }

        position.setSpeed(UnitsConverter.knotsFromKph(buf.readFloatLE()));
        position.setCourse(buf.readUnsignedShortLE());

        position.set(Position.KEY_ODOMETER, buf.readFloatLE() * 1000);
        position.set(Position.KEY_DISTANCE, buf.readFloatLE() * 1000);

        // Segment times
        buf.readUnsignedShortLE();
        buf.readUnsignedShortLE();
    }

    // Other
    if (isFormat(format, F51, F52)) {
        buf.readUnsignedShortLE();
        buf.readByte();
        buf.readUnsignedShortLE();
        buf.readUnsignedShortLE();
        buf.readByte();
        buf.readUnsignedShortLE();
        buf.readUnsignedShortLE();
        buf.readByte();
        buf.readUnsignedShortLE();
    }

    // Four temperature sensors
    if (isFormat(format, F40, F52)) {
        position.set(Position.PREFIX_TEMP + 1, buf.readByte());
        position.set(Position.PREFIX_TEMP + 2, buf.readByte());
        position.set(Position.PREFIX_TEMP + 3, buf.readByte());
        position.set(Position.PREFIX_TEMP + 4, buf.readByte());
    }

    return position;
}

From source file:org.traccar.protocol.NavisProtocolDecoder.java

License:Apache License

private Position parseFlexPosition(DeviceSession deviceSession, ByteBuf buf) {

    Position position = new Position(getProtocolName());

    position.setDeviceId(deviceSession.getDeviceId());

    int status = 0;
    short input = 0;
    short output = 0;

    for (int i = 0; i < flexBitFieldSize; i++) {
        if (!checkFlexBitfield(i)) {
            continue;
        }//  w ww  . java  2 s.c  om

        switch (i) {
        case 0:
            position.set(Position.KEY_INDEX, buf.readUnsignedIntLE());
            break;
        case 1:
            position.set(Position.KEY_EVENT, buf.readUnsignedShortLE());
            break;
        case 3:
            short armedStatus = buf.readUnsignedByte();
            position.set(Position.KEY_ARMED, BitUtil.check(armedStatus, 0));
            if (BitUtil.check(armedStatus, 1)) {
                position.set(Position.KEY_ALARM, Position.ALARM_GENERAL);
            }
            break;
        case 4:
            status = buf.readUnsignedByte();
            position.set(Position.KEY_STATUS, status);
            break;
        case 5:
            int status2 = buf.readUnsignedByte();
            position.set(Position.KEY_STATUS, (short) (BitUtil.to(status, 8) | (status2 << 8)));
            break;
        case 6:
            position.set(Position.KEY_RSSI, buf.readUnsignedByte());
            break;
        case 7:
            int navSensorState = buf.readUnsignedByte();
            position.setValid(BitUtil.check(navSensorState, 1));
            position.set(Position.KEY_SATELLITES, BitUtil.from(navSensorState, 2));
            break;
        case 8:
            position.setTime(new DateBuilder(new Date(buf.readUnsignedIntLE() * 1000)).getDate());
            break;
        case 9:
            position.setLatitude(buf.readIntLE() / 600000.0);
            break;
        case 10:
            position.setLongitude(buf.readIntLE() / 600000.0);
            break;
        case 11:
            position.setAltitude(buf.readIntLE() * 0.1);
            break;
        case 12:
            position.setSpeed(UnitsConverter.knotsFromKph(buf.readFloatLE()));
            break;
        case 13:
            position.setCourse(buf.readUnsignedShortLE());
            break;
        case 14:
            position.set(Position.KEY_ODOMETER, buf.readFloatLE() * 1000);
            break;
        case 15:
            position.set(Position.KEY_DISTANCE, buf.readFloatLE() * 1000);
            break;
        case 18:
            position.set(Position.KEY_POWER, buf.readUnsignedShortLE() * 0.001);
            break;
        case 19:
            position.set(Position.KEY_BATTERY, buf.readUnsignedShortLE() * 0.001);
            break;
        case 20:
        case 21:
        case 22:
        case 23:
        case 24:
        case 25:
        case 26:
        case 27:
            position.set(Position.PREFIX_ADC + (i - 19), buf.readUnsignedShortLE());
            break;
        case 28:
            input = buf.readUnsignedByte();
            position.set(Position.KEY_INPUT, input);
            for (int k = 0; k < 8; k++) {
                position.set(Position.PREFIX_IN + (k + 1), BitUtil.check(input, k));
            }
            break;
        case 29:
            short input2 = buf.readUnsignedByte();
            position.set(Position.KEY_INPUT, (short) (BitUtil.to(input, 8) | (input2 << 8)));
            for (int k = 0; k < 8; k++) {
                position.set(Position.PREFIX_IN + (k + 9), BitUtil.check(input2, k));
            }
            break;
        case 30:
            output = buf.readUnsignedByte();
            position.set(Position.KEY_OUTPUT, output);
            for (int k = 0; k < 8; k++) {
                position.set(Position.PREFIX_OUT + (k + 1), BitUtil.check(output, k));
            }
            break;
        case 31:
            short output2 = buf.readUnsignedByte();
            position.set(Position.KEY_OUTPUT, (short) (BitUtil.to(output, 8) | (output2 << 8)));
            for (int k = 0; k < 8; k++) {
                position.set(Position.PREFIX_OUT + (k + 9), BitUtil.check(output2, k));
            }
            break;
        case 36:
            position.set(Position.KEY_HOURS, buf.readUnsignedIntLE() * 1000);
            break;
        case 44:
        case 45:
        case 46:
        case 47:
        case 48:
        case 49:
        case 50:
        case 51:
            position.set(Position.PREFIX_TEMP + (i - 43), buf.readByte());
            break;
        case 68:
            position.set("can-speed", buf.readUnsignedByte());
            break;
        // FLEX 2.0
        case 69:
            int satVisible = 0;
            for (int k = 0; k < 8; k++) {
                satVisible += buf.readUnsignedByte();
            }
            position.set(Position.KEY_SATELLITES_VISIBLE, satVisible);
            break;
        case 70:
            position.set(Position.KEY_HDOP, buf.readUnsignedByte() * 0.1);
            position.set(Position.KEY_PDOP, buf.readUnsignedByte() * 0.1);
            break;
        default:
            if (i < FLEX_FIELDS_SIZES.length) {
                buf.skipBytes(FLEX_FIELDS_SIZES[i]);
            }
            break;
        }
    }

    return position;
}

From source file:org.traccar.protocol.NavisProtocolDecoder.java

License:Apache License

private Position parseFlex20Position(DeviceSession deviceSession, ByteBuf buf) {

    Position position = new Position(getProtocolName());
    position.setDeviceId(deviceSession.getDeviceId());

    int length = buf.readUnsignedShort();
    if (length <= buf.readableBytes() && buf.readUnsignedByte() == 0x0A) {

        buf.readUnsignedByte(); // length of static part

        position.set(Position.KEY_INDEX, buf.readUnsignedIntLE());

        position.set(Position.KEY_EVENT, buf.readUnsignedShortLE());
        buf.readUnsignedInt(); // event time

        int navSensorState = buf.readUnsignedByte();
        position.setValid(BitUtil.check(navSensorState, 1));
        position.set(Position.KEY_SATELLITES, BitUtil.from(navSensorState, 2));

        position.setTime(new DateBuilder(new Date(buf.readUnsignedIntLE() * 1000)).getDate());
        position.setLatitude(buf.readIntLE() / 600000.0);
        position.setLongitude(buf.readIntLE() / 600000.0);
        position.setAltitude(buf.readIntLE() * 0.1);
        position.setSpeed(UnitsConverter.knotsFromKph(buf.readFloatLE()));
        position.setCourse(buf.readUnsignedShortLE());
        position.set(Position.KEY_ODOMETER, buf.readFloatLE() * 1000);

        buf.skipBytes(length - buf.readerIndex() - 1); // skip unused part
    }//ww w  . j  a  v  a2s . com

    return position;
}

From source file:org.traccar.protocol.NavisProtocolDecoder.java

License:Apache License

private Object decodeNtcb(Channel channel, SocketAddress remoteAddress, ByteBuf buf) {

    prefix = buf.toString(buf.readerIndex(), 4, StandardCharsets.US_ASCII);
    buf.skipBytes(prefix.length()); // prefix @NTC by default
    serverId = buf.readUnsignedIntLE();//  w  w  w.  j  a va 2s  .  c om
    deviceUniqueId = buf.readUnsignedIntLE();
    int length = buf.readUnsignedShortLE();
    buf.skipBytes(2); // header and data XOR checksum

    if (length == 0) {
        return null; // keep alive message
    }

    String type = buf.toString(buf.readerIndex(), 3, StandardCharsets.US_ASCII);
    buf.skipBytes(type.length());

    if (type.equals("*>S")) {
        return processHandshake(channel, remoteAddress, buf);
    } else {
        DeviceSession deviceSession = getDeviceSession(channel, remoteAddress);
        if (deviceSession != null) {
            switch (type) {
            case "*>A":
                return processNtcbArray(deviceSession, channel, buf);
            case "*>T":
                return processNtcbSingle(deviceSession, channel, buf);
            case "*>F":
                buf.skipBytes(3);
                return processFlexNegotiation(channel, buf);
            default:
                break;
            }
        }
    }

    return null;
}

From source file:org.traccar.protocol.NoranProtocolDecoder.java

License:Apache License

@Override
protected Object decode(Channel channel, SocketAddress remoteAddress, Object msg) throws Exception {

    ByteBuf buf = (ByteBuf) msg;

    buf.readUnsignedShortLE(); // length
    int type = buf.readUnsignedShortLE();

    if (type == MSG_SHAKE_HAND && channel != null) {

        ByteBuf response = Unpooled.buffer(13);
        response.writeCharSequence("\r\n*KW", StandardCharsets.US_ASCII);
        response.writeByte(0);//w w w.  j  a  va 2 s  .  c  o  m
        response.writeShortLE(response.capacity());
        response.writeShortLE(MSG_SHAKE_HAND_RESPONSE);
        response.writeByte(1); // status
        response.writeCharSequence("\r\n", StandardCharsets.US_ASCII);

        channel.writeAndFlush(new NetworkMessage(response, remoteAddress));

    } else if (type == MSG_UPLOAD_POSITION || type == MSG_UPLOAD_POSITION_NEW || type == MSG_CONTROL_RESPONSE
            || type == MSG_ALARM) {

        boolean newFormat = false;
        if (type == MSG_UPLOAD_POSITION && buf.readableBytes() == 48
                || type == MSG_ALARM && buf.readableBytes() == 48
                || type == MSG_CONTROL_RESPONSE && buf.readableBytes() == 57) {
            newFormat = true;
        }

        Position position = new Position(getProtocolName());

        if (type == MSG_CONTROL_RESPONSE) {
            buf.readUnsignedIntLE(); // GIS ip
            buf.readUnsignedIntLE(); // GIS port
        }

        position.setValid(BitUtil.check(buf.readUnsignedByte(), 0));

        short alarm = buf.readUnsignedByte();
        switch (alarm) {
        case 1:
            position.set(Position.KEY_ALARM, Position.ALARM_SOS);
            break;
        case 2:
            position.set(Position.KEY_ALARM, Position.ALARM_OVERSPEED);
            break;
        case 3:
            position.set(Position.KEY_ALARM, Position.ALARM_GEOFENCE_EXIT);
            break;
        case 9:
            position.set(Position.KEY_ALARM, Position.ALARM_POWER_OFF);
            break;
        default:
            break;
        }

        if (newFormat) {
            position.setSpeed(UnitsConverter.knotsFromKph(buf.readUnsignedIntLE()));
            position.setCourse(buf.readFloatLE());
        } else {
            position.setSpeed(UnitsConverter.knotsFromKph(buf.readUnsignedByte()));
            position.setCourse(buf.readUnsignedShortLE());
        }
        position.setLongitude(buf.readFloatLE());
        position.setLatitude(buf.readFloatLE());

        if (!newFormat) {
            long timeValue = buf.readUnsignedIntLE();
            DateBuilder dateBuilder = new DateBuilder().setYear((int) BitUtil.from(timeValue, 26))
                    .setMonth((int) BitUtil.between(timeValue, 22, 26))
                    .setDay((int) BitUtil.between(timeValue, 17, 22))
                    .setHour((int) BitUtil.between(timeValue, 12, 17))
                    .setMinute((int) BitUtil.between(timeValue, 6, 12))
                    .setSecond((int) BitUtil.to(timeValue, 6));
            position.setTime(dateBuilder.getDate());
        }

        ByteBuf rawId;
        if (newFormat) {
            rawId = buf.readSlice(12);
        } else {
            rawId = buf.readSlice(11);
        }
        String id = rawId.toString(StandardCharsets.US_ASCII).replaceAll("[^\\p{Print}]", "");
        DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, id);
        if (deviceSession == null) {
            return null;
        }
        position.setDeviceId(deviceSession.getDeviceId());

        if (newFormat) {
            DateFormat dateFormat = new SimpleDateFormat("yy-MM-dd HH:mm:ss");
            position.setTime(dateFormat.parse(buf.readSlice(17).toString(StandardCharsets.US_ASCII)));
            buf.readByte();
        }

        if (!newFormat) {
            position.set(Position.PREFIX_IO + 1, buf.readUnsignedByte());
            position.set(Position.KEY_FUEL_LEVEL, buf.readUnsignedByte());
        } else if (type == MSG_UPLOAD_POSITION_NEW) {
            position.set(Position.PREFIX_TEMP + 1, buf.readShortLE());
            position.set(Position.KEY_ODOMETER, buf.readFloatLE());
        }

        return position;
    }

    return null;
}