Example usage for io.netty.buffer ByteBufUtil hexDump

List of usage examples for io.netty.buffer ByteBufUtil hexDump

Introduction

In this page you can find the example usage for io.netty.buffer ByteBufUtil hexDump.

Prototype

public static String hexDump(byte[] array) 

Source Link

Document

Returns a <a href="http://en.wikipedia.org/wiki/Hex_dump">hex dump</a> of the specified byte array.

Usage

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 .com*/
    }

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

License:Apache License

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

    ByteBuf buf = (ByteBuf) msg;/*from  w  w w.  java  2s  .c o  m*/

    int type = buf.readUnsignedByte();

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

        if (type == MSG_LOGIN) {
            buf.readUnsignedByte(); // hardware version
            buf.readUnsignedByte(); // software version
        }

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

License:Apache License

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

    ByteBuf buf = (ByteBuf) msg;/*from   w  w w .  ja  v a 2  s . co  m*/

    buf.skipBytes(4); // sync
    int type = buf.readUnsignedByte();
    buf.readUnsignedShortLE(); // length

    switch (type) {
    case MSG_LOGIN_REQUEST:
        String imei = ByteBufUtil.hexDump(buf.readBytes(8));
        DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, imei);
        if (deviceSession == null) {
            return null;
        }
        int fuelConst = buf.readUnsignedShortLE();
        int tripConst = buf.readUnsignedShortLE();
        if (channel != null) {
            ByteBuf response = Unpooled.buffer();
            response.writeInt(0xF1F1F1F1); // sync
            response.writeByte(MSG_LOGIN_CONFIRM);
            response.writeShortLE(12); // length
            response.writeBytes(ByteBufUtil.decodeHexDump(imei));
            response.writeShortLE(fuelConst);
            response.writeShortLE(tripConst);
            response.writeShort(Checksum.crc16(Checksum.CRC16_XMODEM, response.nioBuffer()));
            channel.writeAndFlush(new NetworkMessage(response, remoteAddress));
        }
        return null;
    case MSG_TELEMETRY_1:
    case MSG_TELEMETRY_2:
    case MSG_TELEMETRY_3:
        deviceSession = getDeviceSession(channel, remoteAddress);
        if (deviceSession == null) {
            return null;
        }
        return decodeTelemetry(channel, remoteAddress, deviceSession, buf);
    default:
        return null;
    }
}

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;/*from   w  w w . j ava 2  s.c  o m*/

    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);
        }

        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);/*from   w w  w . ja  v  a2s .c o 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.CityeasyProtocolDecoder.java

License:Apache License

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

    ByteBuf buf = (ByteBuf) msg;//  w  ww .j  a va  2  s.  c o  m

    buf.skipBytes(2); // header
    buf.readUnsignedShort(); // length

    String imei = ByteBufUtil.hexDump(buf.readSlice(7));
    DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, imei,
            imei + Checksum.luhn(Long.parseLong(imei)));
    if (deviceSession == null) {
        return null;
    }

    int type = buf.readUnsignedShort();

    if (type == MSG_LOCATION_REPORT || type == MSG_LOCATION_REQUEST) {

        String sentence = buf.toString(buf.readerIndex(), buf.readableBytes() - 8, StandardCharsets.US_ASCII);
        Parser parser = new Parser(PATTERN, sentence);
        if (!parser.matches()) {
            return null;
        }

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

        if (parser.hasNext(15)) {

            position.setTime(parser.nextDateTime());

            position.setValid(parser.next().equals("A"));
            position.set(Position.KEY_SATELLITES, parser.nextInt());

            position.setLatitude(parser.nextCoordinate(Parser.CoordinateFormat.HEM_DEG));
            position.setLongitude(parser.nextCoordinate(Parser.CoordinateFormat.HEM_DEG));

            position.setSpeed(parser.nextDouble(0));
            position.set(Position.KEY_HDOP, parser.nextDouble(0));
            position.setAltitude(parser.nextDouble(0));

        } else {

            getLastLocation(position, null);

        }

        position.setNetwork(new Network(
                CellTower.from(parser.nextInt(0), parser.nextInt(0), parser.nextInt(0), parser.nextInt(0))));

        return position;
    }

    return null;
}

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

License:Apache License

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

    ByteBuf buf = (ByteBuf) msg;//w  w  w  . j a v a 2 s .c  om

    String uniqueId = null;
    DeviceSession deviceSession;

    if (buf.getByte(0) == 'E' && buf.getByte(1) == 'L') {
        buf.skipBytes(2 + 2 + 2); // udp header
        uniqueId = ByteBufUtil.hexDump(buf.readSlice(8)).substring(1);
        deviceSession = getDeviceSession(channel, remoteAddress, uniqueId);
    } else {
        deviceSession = getDeviceSession(channel, remoteAddress);
    }

    buf.skipBytes(2); // header
    int type = buf.readUnsignedByte();
    buf.readShort(); // length
    int index = buf.readUnsignedShort();

    if (type != MSG_GPS && type != MSG_DATA) {
        ByteBuf content = Unpooled.buffer();
        if (type == MSG_LOGIN) {
            content.writeInt((int) (System.currentTimeMillis() / 1000));
            content.writeByte(1); // protocol version
            content.writeByte(0); // action mask
        }
        ByteBuf response = EelinkProtocolEncoder.encodeContent(channel instanceof DatagramChannel, uniqueId,
                type, index, content);
        content.release();
        if (channel != null) {
            channel.writeAndFlush(new NetworkMessage(response, remoteAddress));
        }
    }

    if (type == MSG_LOGIN) {

        if (deviceSession == null) {
            getDeviceSession(channel, remoteAddress, ByteBufUtil.hexDump(buf.readSlice(8)).substring(1));
        }

    } else {

        if (deviceSession == null) {
            return null;
        }

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

            return decodeOld(deviceSession, buf, type, index);

        } else if (type >= MSG_NORMAL && type <= MSG_OBD_CODE) {

            return decodeNew(deviceSession, buf, type, index);

        } else if (type == MSG_HEARTBEAT && buf.readableBytes() >= 2) {

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

            getLastLocation(position, null);

            decodeStatus(position, buf.readUnsignedShort());

            return position;

        } else if (type == MSG_OBD) {

            return decodeObd(deviceSession, buf, index);

        } else if (type == MSG_DOWNLINK) {

            return decodeResult(deviceSession, buf, index);

        }

    }

    return null;
}

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

License:Apache License

private void decodeTagOther(Position position, ByteBuf buf, int tag) {
    switch (tag) {
    case 0x01://www .jav a  2s. co m
        position.set(Position.KEY_VERSION_HW, buf.readUnsignedByte());
        break;
    case 0x02:
        position.set(Position.KEY_VERSION_FW, buf.readUnsignedByte());
        break;
    case 0x04:
        position.set("deviceId", buf.readUnsignedShortLE());
        break;
    case 0x10:
        position.set(Position.KEY_INDEX, buf.readUnsignedShortLE());
        break;
    case 0x20:
        position.setTime(new Date(buf.readUnsignedIntLE() * 1000));
        break;
    case 0x33:
        position.setSpeed(UnitsConverter.knotsFromKph(buf.readUnsignedShortLE() * 0.1));
        position.setCourse(buf.readUnsignedShortLE() * 0.1);
        break;
    case 0x34:
        position.setAltitude(buf.readShortLE());
        break;
    case 0x35:
        position.set(Position.KEY_HDOP, buf.readUnsignedByte() * 0.1);
        break;
    case 0x40:
        position.set(Position.KEY_STATUS, buf.readUnsignedShortLE());
        break;
    case 0x41:
        position.set(Position.KEY_POWER, buf.readUnsignedShortLE() / 1000.0);
        break;
    case 0x42:
        position.set(Position.KEY_BATTERY, buf.readUnsignedShortLE() / 1000.0);
        break;
    case 0x43:
        position.set(Position.KEY_DEVICE_TEMP, buf.readByte());
        break;
    case 0x44:
        position.set(Position.KEY_ACCELERATION, buf.readUnsignedIntLE());
        break;
    case 0x45:
        position.set(Position.KEY_OUTPUT, buf.readUnsignedShortLE());
        break;
    case 0x46:
        position.set(Position.KEY_INPUT, buf.readUnsignedShortLE());
        break;
    case 0x48:
        position.set("statusExtended", buf.readUnsignedShortLE());
        break;
    case 0x58:
        position.set("rs2320", buf.readUnsignedShortLE());
        break;
    case 0x59:
        position.set("rs2321", buf.readUnsignedShortLE());
        break;
    case 0x90:
        position.set(Position.KEY_DRIVER_UNIQUE_ID, String.valueOf(buf.readUnsignedIntLE()));
        break;
    case 0xc0:
        position.set("fuelTotal", buf.readUnsignedIntLE() * 0.5);
        break;
    case 0xc1:
        position.set(Position.KEY_FUEL_LEVEL, buf.readUnsignedByte() * 0.4);
        position.set(Position.PREFIX_TEMP + 1, buf.readUnsignedByte() - 40);
        position.set(Position.KEY_RPM, buf.readUnsignedShortLE() * 0.125);
        break;
    case 0xc2:
        position.set("canB0", buf.readUnsignedIntLE());
        break;
    case 0xc3:
        position.set("canB1", buf.readUnsignedIntLE());
        break;
    case 0xd4:
        position.set(Position.KEY_ODOMETER, buf.readUnsignedIntLE());
        break;
    case 0xe0:
        position.set(Position.KEY_INDEX, buf.readUnsignedIntLE());
        break;
    case 0xe1:
        position.set(Position.KEY_RESULT,
                buf.readSlice(buf.readUnsignedByte()).toString(StandardCharsets.US_ASCII));
        break;
    case 0xea:
        position.set("userDataArray", ByteBufUtil.hexDump(buf.readSlice(buf.readUnsignedByte())));
        position.set("userDataArray", ByteBufUtil.hexDump(buf.readSlice(buf.readUnsignedByte())));
        break;
    default:
        buf.skipBytes(getTagLength(tag));
        break;
    }
}

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

License:Apache License

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

    ByteBuf buf = (ByteBuf) msg;//  www  .  j  a  v a  2 s  . c o  m

    buf.skipBytes(2); // header
    buf.readByte(); // size

    Position position = new Position(getProtocolName());

    // Zero for location messages
    int power = buf.readUnsignedByte();
    int gsm = buf.readUnsignedByte();

    String imei = ByteBufUtil.hexDump(buf.readSlice(8)).substring(1);
    DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, imei);
    if (deviceSession == null) {
        return null;
    }
    position.setDeviceId(deviceSession.getDeviceId());

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

    int type = buf.readUnsignedByte();

    if (type == MSG_HEARTBEAT) {

        getLastLocation(position, null);

        position.set(Position.KEY_POWER, power);
        position.set(Position.KEY_RSSI, gsm);

        if (channel != null) {
            byte[] response = { 0x54, 0x68, 0x1A, 0x0D, 0x0A };
            channel.writeAndFlush(new NetworkMessage(Unpooled.wrappedBuffer(response), remoteAddress));
        }

    } else if (type == MSG_DATA) {

        DateBuilder dateBuilder = new DateBuilder()
                .setDate(buf.readUnsignedByte(), buf.readUnsignedByte(), buf.readUnsignedByte())
                .setTime(buf.readUnsignedByte(), buf.readUnsignedByte(), buf.readUnsignedByte());
        position.setTime(dateBuilder.getDate());

        double latitude = buf.readUnsignedInt() / (60.0 * 30000.0);
        double longitude = buf.readUnsignedInt() / (60.0 * 30000.0);

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

        buf.skipBytes(3); // reserved

        long flags = buf.readUnsignedInt();
        position.setValid(BitUtil.check(flags, 0));
        if (!BitUtil.check(flags, 1)) {
            latitude = -latitude;
        }
        if (!BitUtil.check(flags, 2)) {
            longitude = -longitude;
        }

        position.setLatitude(latitude);
        position.setLongitude(longitude);

    } else if (type == MSG_RESPONSE) {

        getLastLocation(position, null);

        position.set(Position.KEY_RESULT,
                buf.readSlice(buf.readUnsignedByte()).toString(StandardCharsets.US_ASCII));

    } else {

        return null;

    }

    return position;
}

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

License:Apache License

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

    int length = buf.readUnsignedByte();
    int dataLength = length - 5;
    int type = buf.readUnsignedByte();

    DeviceSession deviceSession = null;//from  w  ww.j  av a  2s . c  om
    if (type != MSG_LOGIN) {
        deviceSession = getDeviceSession(channel, remoteAddress);
        if (deviceSession == null) {
            return null;
        }
        if (deviceSession.getTimeZone() == null) {
            deviceSession.setTimeZone(getTimeZone(deviceSession.getDeviceId()));
        }
    }

    if (type == MSG_LOGIN) {

        String imei = ByteBufUtil.hexDump(buf.readSlice(8)).substring(1);
        buf.readUnsignedShort(); // type

        deviceSession = getDeviceSession(channel, remoteAddress, imei);
        if (deviceSession != null && deviceSession.getTimeZone() == null) {
            deviceSession.setTimeZone(getTimeZone(deviceSession.getDeviceId()));
        }

        if (dataLength > 10) {
            int extensionBits = buf.readUnsignedShort();
            int hours = (extensionBits >> 4) / 100;
            int minutes = (extensionBits >> 4) % 100;
            int offset = (hours * 60 + minutes) * 60;
            if ((extensionBits & 0x8) != 0) {
                offset = -offset;
            }
            if (deviceSession != null) {
                TimeZone timeZone = deviceSession.getTimeZone();
                if (timeZone.getRawOffset() == 0) {
                    timeZone.setRawOffset(offset * 1000);
                    deviceSession.setTimeZone(timeZone);
                }
            }

        }

        if (deviceSession != null) {
            sendResponse(channel, false, type, buf.getShort(buf.writerIndex() - 6), null);
        }

    } else if (type == MSG_HEARTBEAT) {

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

        getLastLocation(position, null);

        int status = buf.readUnsignedByte();
        position.set(Position.KEY_ARMED, BitUtil.check(status, 0));
        position.set(Position.KEY_IGNITION, BitUtil.check(status, 1));
        position.set(Position.KEY_CHARGE, BitUtil.check(status, 2));

        if (buf.readableBytes() >= 2 + 6) {
            position.set(Position.KEY_BATTERY, buf.readUnsignedShort() * 0.01);
        }
        if (buf.readableBytes() >= 1 + 6) {
            position.set(Position.KEY_RSSI, buf.readUnsignedByte());
        }

        sendResponse(channel, false, type, buf.getShort(buf.writerIndex() - 6), null);

        return position;

    } else if (type == MSG_ADDRESS_REQUEST) {

        String response = "NA&&NA&&0##";
        ByteBuf content = Unpooled.buffer();
        content.writeByte(response.length());
        content.writeInt(0);
        content.writeBytes(response.getBytes(StandardCharsets.US_ASCII));
        sendResponse(channel, true, MSG_ADDRESS_RESPONSE, 0, content);

    } else if (type == MSG_TIME_REQUEST) {

        Calendar calendar = Calendar.getInstance(TimeZone.getTimeZone("UTC"));
        ByteBuf content = Unpooled.buffer();
        content.writeByte(calendar.get(Calendar.YEAR) - 2000);
        content.writeByte(calendar.get(Calendar.MONTH) + 1);
        content.writeByte(calendar.get(Calendar.DAY_OF_MONTH));
        content.writeByte(calendar.get(Calendar.HOUR_OF_DAY));
        content.writeByte(calendar.get(Calendar.MINUTE));
        content.writeByte(calendar.get(Calendar.SECOND));
        sendResponse(channel, false, MSG_TIME_REQUEST, 0, content);

    } else if (type == MSG_X1_GPS || type == MSG_X1_PHOTO_INFO) {

        return decodeX1(channel, buf, deviceSession, type);

    } else if (type == MSG_WIFI || type == MSG_WIFI_2) {

        return decodeWifi(channel, buf, deviceSession, type);

    } else if (type == MSG_INFO) {

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

        getLastLocation(position, null);

        position.set(Position.KEY_POWER, buf.readShort() * 0.01);

        return position;

    } else {

        return decodeBasicOther(channel, buf, deviceSession, type, dataLength);

    }

    return null;
}