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.RecodaProtocolDecoder.java

License:Apache License

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

    ByteBuf buf = (ByteBuf) msg;

    int type = buf.readIntLE();
    buf.readUnsignedIntLE(); // length

    if (type != MSG_HEARTBEAT) {
        buf.readUnsignedShortLE(); // version
        buf.readUnsignedShortLE(); // index
    }//from w w  w  . java  2  s  . c o  m

    if (type == MSG_SIGNAL_LINK_REGISTRATION) {

        getDeviceSession(channel, remoteAddress, buf.readSlice(12).toString(StandardCharsets.US_ASCII));

    } else if (type == MSG_GPS_DATA) {

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

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

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

        int flags = buf.readUnsignedByte();

        if (BitUtil.check(flags, 0)) {

            buf.readUnsignedShortLE(); // declination

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

            position.setLongitude(buf.readUnsignedByte() + buf.readUnsignedByte() / 60.0);
            position.setLatitude(buf.readUnsignedByte() + buf.readUnsignedByte() / 60.0);

            position.setLongitude(position.getLongitude() + buf.readUnsignedIntLE() / 3600.0);
            position.setLatitude(position.getLatitude() + buf.readUnsignedIntLE() / 3600.0);

            int status = buf.readUnsignedByte();

            position.setValid(BitUtil.check(status, 0));
            if (BitUtil.check(status, 1)) {
                position.setLongitude(-position.getLongitude());
            }
            if (!BitUtil.check(status, 2)) {
                position.setLatitude(-position.getLatitude());
            }

        } else {

            getLastLocation(position, position.getDeviceTime());

        }

        return position;

    }

    return null;
}

From source file:org.traccar.protocol.RoboTrackProtocolDecoder.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_ID) {

        buf.skipBytes(16); // name

        String imei = buf.readSlice(15).toString(StandardCharsets.US_ASCII);

        if (getDeviceSession(channel, remoteAddress, imei) != null && channel != null) {
            ByteBuf response = Unpooled.buffer();
            response.writeByte(MSG_ACK);
            response.writeByte(0x01); // success
            response.writeByte(0x66); // checksum
            channel.writeAndFlush(new NetworkMessage(response, remoteAddress));
        }/*from  ww  w .  j a v a 2s  .  co m*/

    } else if (type == MSG_GPS || type == MSG_GSM) {

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

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

        position.setDeviceTime(new Date(buf.readUnsignedIntLE() * 1000));

        if (type == MSG_GPS) {

            position.setValid(true);
            position.setFixTime(position.getDeviceTime());
            position.setLatitude(buf.readIntLE() * 0.000001);
            position.setLongitude(buf.readIntLE() * 0.000001);
            position.setSpeed(UnitsConverter.knotsFromKph(buf.readByte()));

        } else {

            getLastLocation(position, position.getDeviceTime());

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

            buf.readUnsignedByte(); // reserved

        }

        int value = buf.readUnsignedByte();

        position.set(Position.KEY_SATELLITES, BitUtil.to(value, 4));
        position.set(Position.KEY_RSSI, BitUtil.between(value, 4, 7));
        position.set(Position.KEY_MOTION, BitUtil.check(value, 7));

        value = buf.readUnsignedByte();

        position.set(Position.KEY_CHARGE, BitUtil.check(value, 0));

        for (int i = 1; i <= 4; i++) {
            position.set(Position.PREFIX_IN + i, BitUtil.check(value, i));
        }

        position.set(Position.KEY_BATTERY_LEVEL, BitUtil.from(value, 5) * 100 / 7);
        position.set(Position.KEY_DEVICE_TEMP, buf.readByte());

        for (int i = 1; i <= 3; i++) {
            position.set(Position.PREFIX_ADC + i, buf.readUnsignedShortLE());
        }

        return position;

    }

    return null;
}

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

License:Apache License

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

    ByteBuf buf = (ByteBuf) msg;

    buf.readUnsignedShort(); // data length

    String imei = String.format("%015d", buf.readLong());
    DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, imei);
    if (deviceSession == null) {
        return null;
    }//from  ww  w  .ja v  a2 s  . c  o m

    int type = buf.readUnsignedByte();

    if (type == MSG_RECORDS || type == MSG_EXTENDED_RECORDS) {

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

        buf.readUnsignedByte(); // records left
        int count = buf.readUnsignedByte();

        for (int i = 0; i < count; i++) {
            Position position = new Position(getProtocolName());
            position.setDeviceId(deviceSession.getDeviceId());

            position.setTime(new Date(buf.readUnsignedInt() * 1000));
            buf.readUnsignedByte(); // timestamp extension

            if (type == MSG_EXTENDED_RECORDS) {
                buf.readUnsignedByte(); // record extension
            }

            buf.readUnsignedByte(); // priority (reserved)

            position.setValid(true);
            position.setLongitude(buf.readInt() / 10000000.0);
            position.setLatitude(buf.readInt() / 10000000.0);
            position.setAltitude(buf.readUnsignedShort() / 10.0);
            position.setCourse(buf.readUnsignedShort() / 100.0);

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

            position.setSpeed(UnitsConverter.knotsFromKph(buf.readUnsignedShort()));

            position.set(Position.KEY_HDOP, buf.readUnsignedByte() / 10.0);

            if (type == MSG_EXTENDED_RECORDS) {
                position.set(Position.KEY_EVENT, buf.readUnsignedShort());
            } else {
                position.set(Position.KEY_EVENT, buf.readUnsignedByte());
            }

            // Read 1 byte data
            int cnt = buf.readUnsignedByte();
            for (int j = 0; j < cnt; j++) {
                int id = type == MSG_EXTENDED_RECORDS ? buf.readUnsignedShort() : buf.readUnsignedByte();
                decodeParameter(position, id, buf, 1);
            }

            // Read 2 byte data
            cnt = buf.readUnsignedByte();
            for (int j = 0; j < cnt; j++) {
                int id = type == MSG_EXTENDED_RECORDS ? buf.readUnsignedShort() : buf.readUnsignedByte();
                decodeParameter(position, id, buf, 2);
            }

            // Read 4 byte data
            cnt = buf.readUnsignedByte();
            for (int j = 0; j < cnt; j++) {
                int id = type == MSG_EXTENDED_RECORDS ? buf.readUnsignedShort() : buf.readUnsignedByte();
                decodeParameter(position, id, buf, 4);
            }

            // Read 8 byte data
            cnt = buf.readUnsignedByte();
            for (int j = 0; j < cnt; j++) {
                int id = type == MSG_EXTENDED_RECORDS ? buf.readUnsignedShort() : buf.readUnsignedByte();
                decodeParameter(position, id, buf, 8);
            }

            Long driverIdPart1 = (Long) position.getAttributes().remove(Position.PREFIX_IO + 126);
            Long driverIdPart2 = (Long) position.getAttributes().remove(Position.PREFIX_IO + 127);
            if (driverIdPart1 != null && driverIdPart2 != null) {
                ByteBuf driverId = Unpooled.copyLong(driverIdPart1, driverIdPart2);
                position.set(Position.KEY_DRIVER_UNIQUE_ID, driverId.toString(StandardCharsets.US_ASCII));
                driverId.release();
            }

            positions.add(position);
        }

        if (channel != null) {
            channel.writeAndFlush(new NetworkMessage(
                    Unpooled.wrappedBuffer(DataConverter.parseHex("0002640113bc")), remoteAddress));
        }

        return positions;

    } else if (type == MSG_DTCS) {

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

        int count = buf.readUnsignedByte();

        for (int i = 0; i < count; i++) {
            Position position = new Position(getProtocolName());
            position.setDeviceId(deviceSession.getDeviceId());

            buf.readUnsignedByte(); // reserved

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

            position.setValid(true);
            position.setLongitude(buf.readInt() / 10000000.0);
            position.setLatitude(buf.readInt() / 10000000.0);

            if (buf.readUnsignedByte() == 2) {
                position.set(Position.KEY_ARCHIVE, true);
            }

            position.set(Position.KEY_DTCS, buf.readSlice(5).toString(StandardCharsets.US_ASCII));

            positions.add(position);
        }

        if (channel != null) {
            channel.writeAndFlush(new NetworkMessage(
                    Unpooled.wrappedBuffer(DataConverter.parseHex("00026d01c4a4")), remoteAddress));
        }

        return positions;

    } else {

        return decodeCommandResponse(deviceSession, type, buf);

    }
}

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

License:Apache License

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

    ByteBuf buf = (ByteBuf) msg;

    buf.skipBytes(2); // header
    buf.readUnsignedByte(); // protocol version

    int type = buf.readUnsignedByte();

    ByteBuf id = buf.readSlice(8);
    DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, ByteBufUtil.hexDump(id));
    if (deviceSession == null) {
        return null;
    }/*ww  w.  jav  a 2s  .c  o  m*/

    if (type == MSG_DATE_RECORD) {

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

        position.set(Position.KEY_VERSION_FW, buf.readUnsignedShort());

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

        DateBuilder dateBuilder = new DateBuilder().setDate(2000, 1, 1).addSeconds(buf.readUnsignedInt());

        getLastLocation(position, dateBuilder.getDate());

        int index = buf.readUnsignedByte();
        position.set(Position.KEY_INDEX, index);

        int report = buf.readUnsignedShort();

        buf.readUnsignedShort(); // length

        position.set(Position.KEY_BATTERY, buf.readUnsignedShort());

        Network network = new Network();

        if (report != 0x0203) {

            int count = 1;
            if (report != 0x0200) {
                count = buf.readUnsignedByte();
            }

            for (int i = 0; i < count; i++) {
                int mcc = buf.readUnsignedShort();
                int mnc = buf.readUnsignedShort();
                int lac = buf.readUnsignedShort();
                int cid = buf.readUnsignedShort();
                if (i == 0) {
                    buf.readByte(); // timing advance
                }
                int rssi = buf.readByte();
                network.addCellTower(CellTower.from(mcc, mnc, lac, cid, rssi));
            }

        }

        if (report == 0x0202 || report == 0x0203) {

            int count = buf.readUnsignedByte();

            for (int i = 0; i < count; i++) {
                buf.readerIndex(buf.indexOf(buf.readerIndex(), buf.writerIndex(), (byte) 0) + 1); // ssid

                String mac = String.format("%02x:%02x:%02x:%02x:%02x:%02x", buf.readUnsignedByte(),
                        buf.readUnsignedByte(), buf.readUnsignedByte(), buf.readUnsignedByte(),
                        buf.readUnsignedByte(), buf.readUnsignedByte());

                network.addWifiAccessPoint(WifiAccessPoint.from(mac, buf.readByte()));
            }

        }

        position.setNetwork(network);

        sendResponse(channel, remoteAddress, id, index, report);

        return position;

    }

    return null;
}

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

License:Apache License

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

    ByteBuf buf = (ByteBuf) msg;

    short header = buf.readShort();
    int type = buf.readUnsignedByte();
    buf.readUnsignedShort(); // length
    int index = buf.readUnsignedShort();
    ByteBuf imei = buf.readSlice(8);

    DeviceSession deviceSession = getDeviceSession(channel, remoteAddress,
            ByteBufUtil.hexDump(imei).substring(1));
    if (deviceSession == null) {
        return null;
    }//  w  ww.  j  a v a 2s.co m

    if (type == MSG_GPS || type == MSG_ALARM) {

        return decodePosition(channel, deviceSession, buf, header, type, index, imei);

    } else if (type == MSG_COMMAND) {

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

        getLastLocation(position, null);

        buf.readUnsignedByte(); // protocol number

        position.set(Position.KEY_RESULT, buf.toString(StandardCharsets.UTF_16LE));

        sendResponse(channel, header, type, index, imei, 0);

        return position;

    }

    sendResponse(channel, header, type, index, imei, 0);

    return null;
}

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

License:Apache License

private void decodeSerial(Channel channel, SocketAddress remoteAddress, Position position, ByteBuf buf) {

    getLastLocation(position, null);//from   w  w  w.  j  a  v  a2s. c om

    int type = buf.readUnsignedByte();
    if (type == 0x0D) {

        buf.readInt(); // length
        int subtype = buf.readUnsignedByte();
        if (subtype == 0x01) {

            long photoId = buf.readUnsignedInt();
            ByteBuf photo = Unpooled.buffer(buf.readInt());
            photos.put(photoId, photo);
            sendImageRequest(channel, remoteAddress, photoId, 0, Math.min(IMAGE_PACKET_MAX, photo.capacity()));

        } else if (subtype == 0x02) {

            long photoId = buf.readUnsignedInt();
            buf.readInt(); // offset
            ByteBuf photo = photos.get(photoId);
            photo.writeBytes(buf, buf.readUnsignedShort());
            if (photo.writableBytes() > 0) {
                sendImageRequest(channel, remoteAddress, photoId, photo.writerIndex(),
                        Math.min(IMAGE_PACKET_MAX, photo.writableBytes()));
            } else {
                String uniqueId = Context.getIdentityManager().getById(position.getDeviceId()).getUniqueId();
                photos.remove(photoId);
                try {
                    position.set(Position.KEY_IMAGE,
                            Context.getMediaManager().writeFile(uniqueId, photo, "jpg"));
                } finally {
                    photo.release();
                }
            }

        }

    } else {

        position.set(Position.KEY_TYPE, type);

        int length = buf.readInt();
        boolean readable = true;
        for (int i = 0; i < length; i++) {
            byte b = buf.getByte(buf.readerIndex() + i);
            if (b < 32 && b != '\r' && b != '\n') {
                readable = false;
                break;
            }
        }

        if (readable) {
            position.set(Position.KEY_RESULT, buf.readSlice(length).toString(StandardCharsets.US_ASCII));
        } else {
            position.set(Position.KEY_RESULT, ByteBufUtil.hexDump(buf.readSlice(length)));
        }
    }
}

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

License:Apache License

private void decodeOtherParameter(Position position, int id, ByteBuf buf, int length) {
    switch (id) {
    case 1:/* ww  w  . j  av a 2 s. com*/
    case 2:
    case 3:
    case 4:
        position.set("di" + id, readValue(buf, length, false));
        break;
    case 9:
        position.set(Position.PREFIX_ADC + 1, readValue(buf, length, false));
        break;
    case 17:
        position.set("axisX", readValue(buf, length, true));
        break;
    case 18:
        position.set("axisY", readValue(buf, length, true));
        break;
    case 19:
        position.set("axisZ", readValue(buf, length, true));
        break;
    case 21:
        position.set(Position.KEY_RSSI, readValue(buf, length, false));
        break;
    case 25:
    case 26:
    case 27:
    case 28:
        position.set(Position.PREFIX_TEMP + (id - 24), readValue(buf, length, true) * 0.1);
        break;
    case 66:
        position.set(Position.KEY_POWER, readValue(buf, length, false) * 0.001);
        break;
    case 67:
        position.set(Position.KEY_BATTERY, readValue(buf, length, false) * 0.001);
        break;
    case 69:
        position.set("gpsStatus", readValue(buf, length, false));
        break;
    case 72:
    case 73:
    case 74:
        position.set(Position.PREFIX_TEMP + (id - 71), readValue(buf, length, true) * 0.1);
        break;
    case 78:
        long driverUniqueId = readValue(buf, length, false);
        if (driverUniqueId != 0) {
            position.set(Position.KEY_DRIVER_UNIQUE_ID, String.format("%016X", driverUniqueId));
        }
        break;
    case 80:
        position.set("workMode", readValue(buf, length, false));
        break;
    case 129:
    case 130:
    case 131:
    case 132:
    case 133:
    case 134:
        String driver = id == 129 || id == 132 ? "" : position.getString("driver1");
        position.set("driver" + (id >= 132 ? 2 : 1),
                driver + buf.readSlice(length).toString(StandardCharsets.US_ASCII).trim());
        break;
    case 179:
        position.set(Position.PREFIX_OUT + 1, readValue(buf, length, false) == 1);
        break;
    case 180:
        position.set(Position.PREFIX_OUT + 2, readValue(buf, length, false) == 1);
        break;
    case 181:
        position.set(Position.KEY_PDOP, readValue(buf, length, false) * 0.1);
        break;
    case 182:
        position.set(Position.KEY_HDOP, readValue(buf, length, false) * 0.1);
        break;
    case 236:
        if (readValue(buf, length, false) == 1) {
            position.set(Position.KEY_ALARM, Position.ALARM_OVERSPEED);
        }
        break;
    case 237:
        position.set(Position.KEY_MOTION, readValue(buf, length, false) == 0);
        break;
    case 238:
        switch ((int) readValue(buf, length, false)) {
        case 1:
            position.set(Position.KEY_ALARM, Position.ALARM_ACCELERATION);
            break;
        case 2:
            position.set(Position.KEY_ALARM, Position.ALARM_BRAKING);
            break;
        case 3:
            position.set(Position.KEY_ALARM, Position.ALARM_CORNERING);
            break;
        default:
            break;
        }
        break;
    case 239:
        position.set(Position.KEY_IGNITION, readValue(buf, length, false) == 1);
        break;
    case 240:
        position.set(Position.KEY_MOTION, readValue(buf, length, false) == 1);
        break;
    case 241:
        position.set(Position.KEY_OPERATOR, readValue(buf, length, false));
        break;
    default:
        position.set(Position.PREFIX_IO + id, readValue(buf, length, false));
        break;
    }
}

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

License:Apache License

private void decodeLocation(Position position, ByteBuf buf, int codec) {

    int globalMask = 0x0f;

    if (codec == CODEC_GH3000) {

        long time = buf.readUnsignedInt() & 0x3fffffff;
        time += 1167609600; // 2007-01-01 00:00:00

        globalMask = buf.readUnsignedByte();
        if (BitUtil.check(globalMask, 0)) {

            position.setTime(new Date(time * 1000));

            int locationMask = buf.readUnsignedByte();

            if (BitUtil.check(locationMask, 0)) {
                position.setLatitude(buf.readFloat());
                position.setLongitude(buf.readFloat());
            }//from  w ww .j  a va2 s .  c o  m

            if (BitUtil.check(locationMask, 1)) {
                position.setAltitude(buf.readUnsignedShort());
            }

            if (BitUtil.check(locationMask, 2)) {
                position.setCourse(buf.readUnsignedByte() * 360.0 / 256);
            }

            if (BitUtil.check(locationMask, 3)) {
                position.setSpeed(UnitsConverter.knotsFromKph(buf.readUnsignedByte()));
            }

            if (BitUtil.check(locationMask, 4)) {
                position.set(Position.KEY_SATELLITES, buf.readUnsignedByte());
            }

            if (BitUtil.check(locationMask, 5)) {
                CellTower cellTower = CellTower.fromLacCid(buf.readUnsignedShort(), buf.readUnsignedShort());

                if (BitUtil.check(locationMask, 6)) {
                    cellTower.setSignalStrength((int) buf.readUnsignedByte());
                }

                if (BitUtil.check(locationMask, 7)) {
                    cellTower.setOperator(buf.readUnsignedInt());
                }

                position.setNetwork(new Network(cellTower));

            } else {
                if (BitUtil.check(locationMask, 6)) {
                    position.set(Position.KEY_RSSI, buf.readUnsignedByte());
                }
                if (BitUtil.check(locationMask, 7)) {
                    position.set(Position.KEY_OPERATOR, buf.readUnsignedInt());
                }
            }

        } else {

            getLastLocation(position, new Date(time * 1000));

        }

    } else {

        position.setTime(new Date(buf.readLong()));

        position.set("priority", buf.readUnsignedByte());

        position.setLongitude(buf.readInt() / 10000000.0);
        position.setLatitude(buf.readInt() / 10000000.0);
        position.setAltitude(buf.readShort());
        position.setCourse(buf.readUnsignedShort());

        int satellites = buf.readUnsignedByte();
        position.set(Position.KEY_SATELLITES, satellites);

        position.setValid(satellites != 0);

        position.setSpeed(UnitsConverter.knotsFromKph(buf.readUnsignedShort()));

        position.set(Position.KEY_EVENT, readExtByte(buf, codec, CODEC_8_EXT, CODEC_16));
        if (codec == CODEC_16) {
            buf.readUnsignedByte(); // generation type
        }

        readExtByte(buf, codec, CODEC_8_EXT); // total IO data records

    }

    // Read 1 byte data
    if (BitUtil.check(globalMask, 1)) {
        int cnt = readExtByte(buf, codec, CODEC_8_EXT);
        for (int j = 0; j < cnt; j++) {
            decodeParameter(position, readExtByte(buf, codec, CODEC_8_EXT, CODEC_16), buf, 1, codec);
        }
    }

    // Read 2 byte data
    if (BitUtil.check(globalMask, 2)) {
        int cnt = readExtByte(buf, codec, CODEC_8_EXT);
        for (int j = 0; j < cnt; j++) {
            decodeParameter(position, readExtByte(buf, codec, CODEC_8_EXT, CODEC_16), buf, 2, codec);
        }
    }

    // Read 4 byte data
    if (BitUtil.check(globalMask, 3)) {
        int cnt = readExtByte(buf, codec, CODEC_8_EXT);
        for (int j = 0; j < cnt; j++) {
            decodeParameter(position, readExtByte(buf, codec, CODEC_8_EXT, CODEC_16), buf, 4, codec);
        }
    }

    // Read 8 byte data
    if (codec == CODEC_8 || codec == CODEC_8_EXT || codec == CODEC_16) {
        int cnt = readExtByte(buf, codec, CODEC_8_EXT);
        for (int j = 0; j < cnt; j++) {
            decodeOtherParameter(position, readExtByte(buf, codec, CODEC_8_EXT, CODEC_16), buf, 8);
        }
    }

    // Read 16 byte data
    if (extended) {
        int cnt = readExtByte(buf, codec, CODEC_8_EXT);
        for (int j = 0; j < cnt; j++) {
            int id = readExtByte(buf, codec, CODEC_8_EXT, CODEC_16);
            position.set(Position.PREFIX_IO + id, ByteBufUtil.hexDump(buf.readSlice(16)));
        }
    }

    // Read X byte data
    if (codec == CODEC_8_EXT) {
        int cnt = buf.readUnsignedShort();
        for (int j = 0; j < cnt; j++) {
            int id = buf.readUnsignedShort();
            int length = buf.readUnsignedShort();
            if (id == 256) {
                position.set(Position.KEY_VIN, buf.readSlice(length).toString(StandardCharsets.US_ASCII));
            } else {
                position.set(Position.PREFIX_IO + id, ByteBufUtil.hexDump(buf.readSlice(length)));
            }
        }
    }

    decodeNetwork(position);

}

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

License:Apache License

private Object decodeUdp(Channel channel, SocketAddress remoteAddress, ByteBuf buf) throws Exception {

    buf.readUnsignedShort(); // length
    buf.readUnsignedShort(); // packet id
    buf.readUnsignedByte(); // packet type
    int locationPacketId = buf.readUnsignedByte();
    String imei = buf.readSlice(buf.readUnsignedShort()).toString(StandardCharsets.US_ASCII);

    return parseData(channel, remoteAddress, buf, locationPacketId, imei);

}

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

License:Apache License

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

    ByteBuf buf = (ByteBuf) msg;

    buf.skipBytes(2); // header
    ByteBuf id = buf.readSlice(12);
    buf.readUnsignedByte(); // separator
    int type = buf.readUnsignedByte();
    buf.readUnsignedShort(); // length

    if (type == MSG_LOGIN) {

        int command = buf.readUnsignedByte(); // 0x00 - heartbeat

        if (command == 0x01) {
            String imei = buf.toString(buf.readerIndex(), 15, StandardCharsets.US_ASCII);
            DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, imei);
            if (deviceSession != null && channel != null) {
                ByteBuf response = Unpooled.buffer();
                response.writeByte(0x48);
                response.writeByte(0x52); // header
                response.writeBytes(id);
                response.writeByte(0x2c); // separator
                response.writeByte(type);
                response.writeShort(0x0002); // length
                response.writeShort(0x8000);
                response.writeShort(0x0000); // checksum
                channel.writeAndFlush(new NetworkMessage(response, remoteAddress));
            }/*w w  w .j  ava 2s . com*/
        }

    } else if (type == MSG_GPS) {

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

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

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

        int flags = buf.readUnsignedByte();

        position.setValid(true);
        position.setLatitude(convertCoordinate(buf.readUnsignedInt(), !BitUtil.check(flags, 0)));
        position.setLongitude(convertCoordinate(buf.readUnsignedInt(), !BitUtil.check(flags, 1)));

        position.setSpeed(buf.readUnsignedByte());
        position.setCourse(buf.readUnsignedByte());

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

        return position;

    }

    return null;
}