Example usage for io.netty.buffer ByteBuf readSlice

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

Introduction

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

Prototype

public abstract ByteBuf readSlice(int length);

Source Link

Document

Returns a new slice of this buffer's sub-region starting at the current readerIndex and increases the readerIndex by the size of the new slice (= length ).

Usage

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

License:Apache License

private void decodeE(Position position, ByteBuf buf, int selector) {

    if ((selector & 0x0008) != 0) {
        position.set("tachographEvent", buf.readUnsignedShort());
    }//  w  w w.  j av  a2s . c  om

    if ((selector & 0x0004) != 0) {
        getLastLocation(position, new Date(buf.readUnsignedInt() * 1000));
    } else {
        getLastLocation(position, null);
    }

    if ((selector & 0x0010) != 0) {
        String time = buf.readUnsignedByte() + "s " + buf.readUnsignedByte() + "m " + buf.readUnsignedByte()
                + "h " + buf.readUnsignedByte() + "M " + buf.readUnsignedByte() + "D " + buf.readUnsignedByte()
                + "Y " + buf.readByte() + "m " + buf.readByte() + "h";
        position.set("tachographTime", time);
    }

    position.set("workState", buf.readUnsignedByte());
    position.set("driver1State", buf.readUnsignedByte());
    position.set("driver2State", buf.readUnsignedByte());

    if ((selector & 0x0020) != 0) {
        position.set("tachographStatus", buf.readUnsignedByte());
    }

    if ((selector & 0x0040) != 0) {
        position.set(Position.KEY_OBD_SPEED, buf.readUnsignedShort() / 256.0);
    }

    if ((selector & 0x0080) != 0) {
        position.set(Position.KEY_OBD_ODOMETER, buf.readUnsignedInt() * 5);
    }

    if ((selector & 0x0100) != 0) {
        position.set(Position.KEY_ODOMETER_TRIP, buf.readUnsignedInt() * 5);
    }

    if ((selector & 0x8000) != 0) {
        position.set("kFactor", buf.readUnsignedShort() * 0.001 + " pulses/m");
    }

    if ((selector & 0x0200) != 0) {
        position.set(Position.KEY_RPM, buf.readUnsignedShort() * 0.125);
    }

    if ((selector & 0x0400) != 0) {
        position.set("extraInfo", buf.readUnsignedShort());
    }

    if ((selector & 0x0800) != 0) {
        position.set(Position.KEY_VIN, buf.readSlice(18).toString(StandardCharsets.US_ASCII).trim());
    }

    if ((selector & 0x2000) != 0) {
        buf.readUnsignedByte(); // card 1 type
        buf.readUnsignedByte(); // card 1 country code
        String card = buf.readSlice(20).toString(StandardCharsets.US_ASCII).trim();
        if (!card.isEmpty()) {
            position.set("card1", card);
        }
    }

    if ((selector & 0x4000) != 0) {
        buf.readUnsignedByte(); // card 2 type
        buf.readUnsignedByte(); // card 2 country code
        String card = buf.readSlice(20).toString(StandardCharsets.US_ASCII).trim();
        if (!card.isEmpty()) {
            position.set("card2", card);
        }
    }

    if ((selector & 0x10000) != 0) {
        int count = buf.readUnsignedByte();
        for (int i = 1; i <= count; i++) {
            position.set("driver" + i, buf.readSlice(22).toString(StandardCharsets.US_ASCII).trim());
            position.set("driverTime" + i, buf.readUnsignedInt());
        }
    }
}

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

License:Apache License

private void decodeEB(Position position, ByteBuf buf) {

    if (buf.readByte() != (byte) 'E' || buf.readByte() != (byte) 'B') {
        return;// ww  w. ja  v a  2 s.c  o  m
    }

    position.set(Position.KEY_VERSION_FW, buf.readUnsignedByte());
    position.set(Position.KEY_EVENT, buf.readUnsignedShort());
    position.set("dataValidity", buf.readUnsignedByte());
    position.set("towed", buf.readUnsignedByte());
    buf.readUnsignedShort(); // length

    while (buf.readableBytes() > 0) {
        buf.readUnsignedByte(); // towed position
        int type = buf.readUnsignedByte();
        int length = buf.readUnsignedByte();
        int end = buf.readerIndex() + length;

        switch (type) {
        case 0x01:
            position.set("brakeFlags", ByteBufUtil.hexDump(buf.readSlice(length)));
            break;
        case 0x02:
            position.set("wheelSpeed", buf.readUnsignedShort() / 256.0);
            position.set("wheelSpeedDifference", buf.readUnsignedShort() / 256.0 - 125.0);
            position.set("lateralAcceleration", buf.readUnsignedByte() / 10.0 - 12.5);
            position.set("vehicleSpeed", buf.readUnsignedShort() / 256.0);
            break;
        case 0x03:
            position.set(Position.KEY_AXLE_WEIGHT, buf.readUnsignedShort() * 2);
            break;
        case 0x04:
            position.set("tyrePressure", buf.readUnsignedByte() * 10);
            position.set("pneumaticPressure", buf.readUnsignedByte() * 5);
            break;
        case 0x05:
            position.set("brakeLining", buf.readUnsignedByte() * 0.4);
            position.set("brakeTemperature", buf.readUnsignedByte() * 10);
            break;
        case 0x06:
            position.set(Position.KEY_ODOMETER, buf.readUnsignedInt() * 5L);
            position.set(Position.KEY_ODOMETER_TRIP, buf.readUnsignedInt() * 5L);
            position.set(Position.KEY_ODOMETER_SERVICE, (buf.readUnsignedInt() - 2105540607) * 5L);
            break;
        case 0x0A:
            position.set("absStatusCounter", buf.readUnsignedShort());
            position.set("atvbStatusCounter", buf.readUnsignedShort());
            position.set("vdcActiveCounter", buf.readUnsignedShort());
            break;
        case 0x0B:
            position.set("brakeMinMaxData", ByteBufUtil.hexDump(buf.readSlice(length)));
            break;
        case 0x0C:
            position.set("missingPgn", ByteBufUtil.hexDump(buf.readSlice(length)));
            break;
        case 0x0D:
            buf.readUnsignedByte();
            position.set("towedDetectionStatus", buf.readUnsignedInt());
            buf.skipBytes(17); // vin
            break;
        case 0x0E:
        default:
            break;
        }

        buf.readerIndex(end);
    }
}

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

License:Apache License

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

    ByteBuf buf = (ByteBuf) msg;

    if (channel != null) {
        channel.writeAndFlush(new NetworkMessage(Unpooled.wrappedBuffer(new byte[] { 0x06 }), remoteAddress));
    }/*ww w  .  j  a v a2 s . com*/

    buf.readUnsignedByte(); // protocol
    buf.readUnsignedShort(); // length

    String imei = String.format("%08d", buf.readUnsignedInt())
            + String.format("%07d", buf.readUnsignedMedium());
    DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, imei);
    if (deviceSession == null) {
        return null;
    }

    List<Position> positions = new LinkedList<>();

    while (buf.readableBytes() > 2) {

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

        buf.readUnsignedByte(); // index

        position.setValid(true);
        position.setLatitude(buf.readInt() * 0.000001);
        position.setLongitude(buf.readInt() * 0.000001);

        DateBuilder dateBuilder = new DateBuilder().setDate(1980, 1, 6)
                .addMillis(buf.readUnsignedInt() * 1000L);
        position.setTime(dateBuilder.getDate());

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

        int reason = buf.readUnsignedMedium();
        position.set(Position.KEY_EVENT, reason);

        int status = buf.readUnsignedShort();
        position.set(Position.KEY_STATUS, status);

        position.set(Position.PREFIX_IO + 1, buf.readUnsignedByte());
        position.set(Position.PREFIX_ADC + 1, buf.readUnsignedByte());
        position.set(Position.KEY_BATTERY, buf.readUnsignedByte());
        position.set(Position.KEY_POWER, buf.readUnsignedByte());

        buf.readUnsignedByte(); // max journey speed
        buf.skipBytes(6); // accelerometer
        position.set(Position.KEY_ODOMETER_TRIP, buf.readUnsignedShort());
        buf.readUnsignedShort(); // journey idle time

        position.setAltitude(buf.readUnsignedByte() * 20);

        int quality = buf.readUnsignedByte();
        position.set(Position.KEY_SATELLITES, quality & 0xf);
        position.set(Position.KEY_RSSI, quality >> 4);

        buf.readUnsignedByte(); // geofence events

        if (BitUtil.check(status, 8)) {
            position.set(Position.KEY_DRIVER_UNIQUE_ID, buf.readSlice(7).toString(StandardCharsets.US_ASCII));
            position.set(Position.KEY_ODOMETER, buf.readUnsignedMedium() * 1000);
            position.set(Position.KEY_HOURS, UnitsConverter.msFromHours(buf.readUnsignedShort()));
        }

        if (BitUtil.check(status, 6)) {
            LOGGER.warn("Extension data is not supported");
            return position;
        }

        positions.add(position);

    }

    return positions;
}

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

License:Apache License

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

    ByteBuf buf = (ByteBuf) msg;

    if (buf.getUnsignedByte(buf.readerIndex()) == 0x01) {
        buf.readUnsignedByte(); // codec id
    }//w  ww.j  a  v  a 2 s .  c o  m

    int type = buf.readUnsignedByte();
    buf.readUnsignedMediumLE(); // length
    buf.skipBytes(BLOCK_LENGTH - 1 - 3);

    if (type == MSG_DEVICE_ID) {

        String imei = buf.readSlice(15).toString(StandardCharsets.US_ASCII);
        if (getDeviceSession(channel, remoteAddress, imei) != null) {

            byte[] iv = new byte[BLOCK_LENGTH];
            buf.readBytes(iv);
            IvParameterSpec ivSpec = new IvParameterSpec(iv);

            SecretKeySpec keySpec = new SecretKeySpec(
                    DataConverter.parseHex("000102030405060708090a0b0c0d0e0f"), "AES");

            cipher = Cipher.getInstance("AES/CBC/NoPadding");
            cipher.init(Cipher.DECRYPT_MODE, keySpec, ivSpec);

            byte[] data = new byte[BLOCK_LENGTH];
            buf.readBytes(data);
            cipher.update(data);

        }

    } else if (type == MSG_TRACK_RESPONSE) {

        DeviceSession deviceSession = getDeviceSession(channel, remoteAddress);
        if (deviceSession == null) {
            return null;
        }

        if (buf.capacity() <= BLOCK_LENGTH) {
            return null; // empty message
        }

        List<Position> positions = new LinkedList<>();

        byte[] data = new byte[buf.capacity() - BLOCK_LENGTH];
        buf.readBytes(data);
        buf = Unpooled.wrappedBuffer(cipher.update(data));
        try {
            while (buf.readableBytes() >= 63) {

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

                buf.readUnsignedShortLE(); // index
                buf.readUnsignedShortLE(); // reserved

                position.setValid(true);

                position.setTime(new Date(buf.readLongLE() * 1000));

                position.setLatitude(buf.readFloatLE());
                position.setLongitude(buf.readFloatLE());
                position.setAltitude(buf.readFloatLE());
                position.setSpeed(UnitsConverter.knotsFromKph(buf.readFloatLE()));
                position.setCourse(buf.readFloatLE());

                buf.readUnsignedIntLE(); // geozone event
                buf.readUnsignedIntLE(); // io events
                buf.readUnsignedIntLE(); // geozone value
                buf.readUnsignedIntLE(); // io values
                buf.readUnsignedShortLE(); // operator

                position.set(Position.PREFIX_ADC + 1, buf.readUnsignedShortLE());
                position.set(Position.PREFIX_ADC + 1, buf.readUnsignedShortLE());

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

                buf.readUnsignedShortLE(); // cid
                position.set(Position.KEY_RSSI, buf.readUnsignedByte());
                buf.readUnsignedByte(); // current profile

                position.set(Position.KEY_BATTERY, buf.readUnsignedByte());
                position.set(Position.PREFIX_TEMP + 1, buf.readUnsignedByte());
                position.set(Position.KEY_SATELLITES, buf.readUnsignedByte());

                positions.add(position);

            }
        } finally {
            buf.release();
        }

        return positions;

    }

    if (type == MSG_DEVICE_ID) {
        sendRequest(channel);
    }

    return null;
}

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

License:Apache License

private static String readString(ByteBuf buf) {
    String result = null;/*from w  ww .j ava 2  s .  c  o m*/
    int index = buf.indexOf(buf.readerIndex(), buf.writerIndex(), (byte) 0);
    if (index > buf.readerIndex()) {
        result = buf.readSlice(index - buf.readerIndex()).toString(StandardCharsets.US_ASCII);
    }
    buf.readByte();
    return result;
}

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

License:Apache License

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

    ByteBuf buf = (ByteBuf) msg;

    int type = buf.readUnsignedByte();

    if (type == MSG_LOGIN || type == MSG_45_LOGIN) {

        if (type == MSG_LOGIN) {
            buf.readUnsignedByte(); // hardware version
            buf.readUnsignedByte(); // software version
        }// w w w.j av  a  2s .  com

        String imei = ByteBufUtil.hexDump(buf.readSlice(8)).substring(1);
        DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, imei);

        if (deviceSession != null && channel != null) {
            ByteBuf response = Unpooled.buffer();
            response.writeBytes("resp_crc=".getBytes(StandardCharsets.US_ASCII));
            response.writeByte(buf.getByte(buf.writerIndex() - 1));
            channel.writeAndFlush(new NetworkMessage(response, remoteAddress));
        }

        return null;

    }

    DeviceSession deviceSession = getDeviceSession(channel, remoteAddress);
    if (deviceSession == null) {
        return null;
    }

    if (type == MSG_LOCATION) {

        return decodePosition(deviceSession, buf, false);

    } else if (type == MSG_HISTORY) {

        int count = buf.readUnsignedByte() & 0x0f;
        buf.readUnsignedShort(); // total count
        List<Position> positions = new LinkedList<>();

        for (int i = 0; i < count; i++) {
            positions.add(decodePosition(deviceSession, buf, true));
        }

        return positions;

    } else if (type == MSG_45_LOCATION) {

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

        short status = buf.readUnsignedByte();
        if (BitUtil.check(status, 7)) {
            position.set(Position.KEY_ALARM, Position.ALARM_GENERAL);
        }
        position.set(Position.KEY_BATTERY, BitUtil.to(status, 7));

        buf.skipBytes(2); // remaining time

        position.set(Position.PREFIX_TEMP + 1, buf.readByte());

        buf.skipBytes(2); // timer (interval and units)
        buf.readByte(); // mode
        buf.readByte(); // gprs sending interval

        buf.skipBytes(6); // mcc, mnc, lac, cid

        int valid = buf.readUnsignedByte();
        position.setValid(BitUtil.from(valid, 6) != 0);
        position.set(Position.KEY_SATELLITES, BitUtil.from(valid, 6));

        int time = buf.readUnsignedMedium();
        int date = buf.readUnsignedMedium();

        DateBuilder dateBuilder = new DateBuilder().setTime(time / 10000, time / 100 % 100, time % 100)
                .setDateReverse(date / 10000, date / 100 % 100, date % 100);
        position.setTime(dateBuilder.getDate());

        position.setLatitude(convertCoordinate(buf.readUnsignedByte(), buf.readUnsignedMedium()));
        position.setLongitude(convertCoordinate(buf.readUnsignedByte(), buf.readUnsignedMedium()));
        position.setSpeed(buf.readUnsignedByte());
        position.setCourse(buf.readUnsignedShort());

        return position;

    }

    return null;
}

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

License:Apache License

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

    ByteBuf buf = (ByteBuf) msg;

    buf.readUnsignedByte(); // header
    int length = (buf.readUnsignedShortLE() & 0x7fff) + 3;

    List<Position> positions = new LinkedList<>();
    Set<Integer> tags = new HashSet<>();
    boolean hasLocation = false;
    Position position = new Position(getProtocolName());

    while (buf.readerIndex() < length) {

        // Check if new message started
        int tag = buf.readUnsignedByte();
        if (tags.contains(tag)) {
            if (hasLocation && position.getFixTime() != null) {
                positions.add(position);
            }/*  w  w  w .ja v  a2 s. c  om*/
            tags.clear();
            hasLocation = false;
            position = new Position(getProtocolName());
        }
        tags.add(tag);

        switch (tag) {

        case TAG_IMEI:
            getDeviceSession(channel, remoteAddress, buf.readSlice(15).toString(StandardCharsets.US_ASCII));
            break;

        case TAG_DATE:
            position.setTime(new Date(buf.readUnsignedIntLE() * 1000));
            break;

        case TAG_COORDINATES:
            hasLocation = true;
            position.setValid((buf.readUnsignedByte() & 0xf0) == 0x00);
            position.setLatitude(buf.readIntLE() / 1000000.0);
            position.setLongitude(buf.readIntLE() / 1000000.0);
            break;

        case TAG_SPEED_COURSE:
            position.setSpeed(buf.readUnsignedShortLE() * 0.0539957);
            position.setCourse(buf.readUnsignedShortLE() * 0.1);
            break;

        case TAG_ALTITUDE:
            position.setAltitude(buf.readShortLE());
            break;

        case TAG_STATUS:
            int status = buf.readUnsignedShortLE();
            position.set(Position.KEY_IGNITION, BitUtil.check(status, 9));
            if (BitUtil.check(status, 15)) {
                position.set(Position.KEY_ALARM, Position.ALARM_GENERAL);
            }
            position.set(Position.KEY_CHARGE, BitUtil.check(status, 2));
            break;

        case TAG_DIGITAL_INPUTS:
            int input = buf.readUnsignedShortLE();
            for (int i = 0; i < 16; i++) {
                position.set(Position.PREFIX_IO + (i + 1), BitUtil.check(input, i));
            }
            break;

        case TAG_DIGITAL_OUTPUTS:
            int output = buf.readUnsignedShortLE();
            for (int i = 0; i < 16; i++) {
                position.set(Position.PREFIX_IO + (i + 17), BitUtil.check(output, i));
            }
            break;

        case TAG_INPUT_VOLTAGE1:
            position.set(Position.PREFIX_ADC + 1, buf.readUnsignedShortLE() / 1000.0);
            break;

        case TAG_INPUT_VOLTAGE2:
            position.set(Position.PREFIX_ADC + 2, buf.readUnsignedShortLE() / 1000.0);
            break;

        case TAG_INPUT_VOLTAGE3:
            position.set(Position.PREFIX_ADC + 3, buf.readUnsignedShortLE() / 1000.0);
            break;

        case TAG_INPUT_VOLTAGE4:
            position.set(Position.PREFIX_ADC + 4, buf.readUnsignedShortLE() / 1000.0);
            break;

        case TAG_XT1:
        case TAG_XT2:
        case TAG_XT3:
            buf.skipBytes(16);
            break;

        default:
            break;

        }
    }

    if (hasLocation && position.getFixTime() != null) {
        positions.add(position);
    }

    DeviceSession deviceSession = getDeviceSession(channel, remoteAddress);
    if (deviceSession == null) {
        return null;
    }

    sendReply(channel, buf.readUnsignedShortLE());

    for (Position p : positions) {
        p.setDeviceId(deviceSession.getDeviceId());
    }

    if (positions.isEmpty()) {
        return null;
    }

    return positions;
}

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

License:Apache License

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

    ByteBuf buf = (ByteBuf) msg;

    if (BitUtil.check(buf.getByte(buf.readerIndex()), 7)) {

        int content = buf.readUnsignedByte();

        if (BitUtil.check(content, 0)) {
            String id = ByteBufUtil.hexDump(buf.readSlice(buf.readUnsignedByte()));
            getDeviceSession(channel, remoteAddress, id);
        }// www .j  a  v a  2s. c  o m

        if (BitUtil.check(content, 1)) {
            buf.skipBytes(buf.readUnsignedByte()); // identifier type
        }

        if (BitUtil.check(content, 2)) {
            buf.skipBytes(buf.readUnsignedByte()); // authentication
        }

        if (BitUtil.check(content, 3)) {
            buf.skipBytes(buf.readUnsignedByte()); // routing
        }

        if (BitUtil.check(content, 4)) {
            buf.skipBytes(buf.readUnsignedByte()); // forwarding
        }

        if (BitUtil.check(content, 5)) {
            buf.skipBytes(buf.readUnsignedByte()); // response redirection
        }

    }

    DeviceSession deviceSession = getDeviceSession(channel, remoteAddress);
    if (deviceSession == null) {
        return null;
    }

    int service = buf.readUnsignedByte();
    int type = buf.readUnsignedByte();
    int index = buf.readUnsignedShort();

    if (service == SERVICE_ACKNOWLEDGED) {
        sendResponse(channel, remoteAddress, type, index, 0);
    }

    if (type == MSG_EVENT_REPORT || type == MSG_LOCATE_REPORT || type == MSG_MINI_EVENT_REPORT) {
        return decodePosition(deviceSession, type, buf);
    }

    return null;
}

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

License:Apache License

private Object decodeSc(Channel channel, SocketAddress remoteAddress, ByteBuf buf, int version, ByteBuf id,
        short type, DeviceSession deviceSession) {

    if (type == MSG_SC_HEARTBEAT) {

        sendResponse(channel, remoteAddress, version, id, MSG_SC_HEARTBEAT_RESPONSE, null);

    } else if (type == MSG_SC_LOGIN || type == MSG_SC_LOGOUT || type == MSG_SC_GPS || type == MSG_SC_ALARM
            || type == MSG_SC_CURRENT_LOCATION || type == MSG_SC_FUEL) {

        if (type == MSG_SC_LOGIN) {
            ByteBuf response = Unpooled.buffer(10);
            response.writeIntLE(0xFFFFFFFF);
            response.writeShortLE(0);/*  ww w . j  a  v a 2  s .co  m*/
            response.writeIntLE((int) (System.currentTimeMillis() / 1000));
            sendResponse(channel, remoteAddress, version, id, MSG_SC_LOGIN_RESPONSE, response);
        }

        if (type == MSG_SC_GPS) {
            buf.readUnsignedByte(); // historical
        } else if (type == MSG_SC_ALARM) {
            buf.readUnsignedIntLE(); // alarm
        } else if (type == MSG_SC_CURRENT_LOCATION) {
            buf.readUnsignedShortLE();
        }

        buf.readUnsignedIntLE(); // ACC ON time
        buf.readUnsignedIntLE(); // UTC time
        long odometer = buf.readUnsignedIntLE();
        long tripOdometer = buf.readUnsignedIntLE();
        long fuelConsumption = buf.readUnsignedIntLE();
        buf.readUnsignedShortLE(); // current fuel consumption
        long status = buf.readUnsignedIntLE();
        buf.skipBytes(8);

        int count = buf.readUnsignedByte();

        List<Position> positions = new LinkedList<>();

        for (int i = 0; i < count; i++) {
            Position position = readPosition(deviceSession, buf);
            position.set(Position.KEY_ODOMETER, odometer);
            position.set(Position.KEY_ODOMETER_TRIP, tripOdometer);
            position.set(Position.KEY_FUEL_CONSUMPTION, fuelConsumption);
            position.set(Position.KEY_STATUS, status);
            positions.add(position);
        }

        if (type == MSG_SC_ALARM) {
            int alarmCount = buf.readUnsignedByte();
            for (int i = 0; i < alarmCount; i++) {
                if (buf.readUnsignedByte() != 0) {
                    int alarm = buf.readUnsignedByte();
                    for (Position position : positions) {
                        decodeAlarm(position, alarm);
                    }
                    buf.readUnsignedShortLE(); // description
                    buf.readUnsignedShortLE(); // threshold
                }
            }
        } else if (type == MSG_SC_FUEL) {
            for (Position position : positions) {
                position.set(Position.PREFIX_ADC + 1, buf.readUnsignedShortLE());
            }
        }

        if (!positions.isEmpty()) {
            return positions;
        }

    } else if (type == MSG_SC_GPS_SLEEP) {

        buf.readUnsignedIntLE(); // device time

        return readPosition(deviceSession, buf);

    } else if (type == MSG_SC_AGPS_REQUEST) {

        return readPosition(deviceSession, buf);

    } else if (type == MSG_SC_PID_DATA) {

        Position position = createPosition(deviceSession);

        decodeStat(position, buf);

        buf.readUnsignedShortLE(); // sample rate
        decodeObd(position, buf, true);

        return position;

    } else if (type == MSG_SC_DTCS_PASSENGER) {

        Position position = createPosition(deviceSession);

        decodeStat(position, buf);

        buf.readUnsignedByte(); // flag
        position.add(ObdDecoder.decodeCodes(ByteBufUtil.hexDump(buf.readSlice(buf.readUnsignedByte()))));

        return position;

    } else if (type == MSG_SC_OBD_DATA) {

        Position position = createPosition(deviceSession);

        decodeStat(position, buf);

        buf.readUnsignedByte(); // flag
        decodeObd(position, buf, false);

        return position;

    } else if (type == MSG_SC_CELL) {

        Position position = createPosition(deviceSession);

        decodeStat(position, buf);

        position.setNetwork(
                new Network(CellTower.fromLacCid(buf.readUnsignedShortLE(), buf.readUnsignedShortLE())));

        return position;

    } else if (type == MSG_SC_QUERY_RESPONSE) {

        Position position = createPosition(deviceSession);

        buf.readUnsignedShortLE(); // index
        buf.readUnsignedByte(); // response count
        buf.readUnsignedByte(); // response index

        int failureCount = buf.readUnsignedByte();
        for (int i = 0; i < failureCount; i++) {
            buf.readUnsignedShortLE(); // tag
        }

        int successCount = buf.readUnsignedByte();
        for (int i = 0; i < successCount; i++) {
            buf.readUnsignedShortLE(); // tag
            position.set(Position.KEY_RESULT,
                    buf.readSlice(buf.readUnsignedShortLE()).toString(StandardCharsets.US_ASCII));
        }

        return position;

    }

    return null;
}

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

License:Apache License

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

    ByteBuf buf = (ByteBuf) msg;

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

    int version = -1;
    if (header == 0x4040) {
        version = buf.readUnsignedByte();
    }/*from  w w  w .  j av  a  2  s.  c o m*/

    ByteBuf id = buf.readSlice(20);
    short type = buf.readShort();

    DeviceSession deviceSession = getDeviceSession(channel, remoteAddress,
            id.toString(StandardCharsets.US_ASCII).trim());
    if (deviceSession == null) {
        return null;
    }

    switch (version) {
    case -1:
        return decodeMpip(channel, remoteAddress, buf, version, id, type, deviceSession);
    case 3:
    case 4:
        return decodeSc(channel, remoteAddress, buf, version, id, type, deviceSession);
    default:
        return decodeCc(channel, remoteAddress, buf, version, id, type, deviceSession);
    }
}