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

License:Apache License

private Object decodeBasicOther(Channel channel, ByteBuf buf, DeviceSession deviceSession, int type,
        int dataLength) {

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

    if (type == MSG_LBS_MULTIPLE || type == MSG_LBS_EXTEND || type == MSG_LBS_WIFI || type == MSG_LBS_2
            || type == MSG_WIFI_3) {/*from  www.ja  va  2 s. c o  m*/

        boolean longFormat = type == MSG_LBS_2 || type == MSG_WIFI_3;

        DateBuilder dateBuilder = new DateBuilder(deviceSession.getTimeZone())
                .setDate(buf.readUnsignedByte(), buf.readUnsignedByte(), buf.readUnsignedByte())
                .setTime(buf.readUnsignedByte(), buf.readUnsignedByte(), buf.readUnsignedByte());

        getLastLocation(position, dateBuilder.getDate());

        int mcc = buf.readUnsignedShort();
        int mnc = BitUtil.check(mcc, 15) ? buf.readUnsignedShort() : buf.readUnsignedByte();
        Network network = new Network();
        for (int i = 0; i < 7; i++) {
            int lac = longFormat ? buf.readInt() : buf.readUnsignedShort();
            int cid = longFormat ? (int) buf.readLong() : buf.readUnsignedMedium();
            int rssi = -buf.readUnsignedByte();
            if (lac > 0) {
                network.addCellTower(CellTower.from(BitUtil.to(mcc, 15), mnc, lac, cid, rssi));
            }
        }

        buf.readUnsignedByte(); // time leads

        if (type != MSG_LBS_MULTIPLE && type != MSG_LBS_2) {
            int wifiCount = buf.readUnsignedByte();
            for (int i = 0; i < wifiCount; i++) {
                String mac = ByteBufUtil.hexDump(buf.readSlice(6)).replaceAll("(..)", "$1:");
                network.addWifiAccessPoint(
                        WifiAccessPoint.from(mac.substring(0, mac.length() - 1), buf.readUnsignedByte()));
            }
        }

        position.setNetwork(network);

    } else if (type == MSG_STRING) {

        getLastLocation(position, null);

        int commandLength = buf.readUnsignedByte();

        if (commandLength > 0) {
            buf.readUnsignedByte(); // server flag (reserved)
            position.set(Position.KEY_RESULT,
                    buf.readSlice(commandLength - 1).toString(StandardCharsets.US_ASCII));
        }

    } else if (isSupported(type)) {

        if (hasGps(type)) {
            decodeGps(position, buf, false, deviceSession.getTimeZone());
        } else {
            getLastLocation(position, null);
        }

        if (hasLbs(type)) {
            decodeLbs(position, buf, hasStatus(type));
        }

        if (hasStatus(type)) {
            decodeStatus(position, buf);
        }

        if (type == MSG_GPS_LBS_1 && buf.readableBytes() == 2 + 6) {
            int mask = buf.readUnsignedShort();
            position.set(Position.KEY_IGNITION, BitUtil.check(mask, 8 + 7));
            position.set(Position.PREFIX_IN + 2, BitUtil.check(mask, 8 + 6));
            if (BitUtil.check(mask, 8 + 4)) {
                int value = BitUtil.to(mask, 8 + 1);
                if (BitUtil.check(mask, 8 + 1)) {
                    value = -value;
                }
                position.set(Position.PREFIX_TEMP + 1, value);
            } else {
                int value = BitUtil.to(mask, 8 + 2);
                if (BitUtil.check(mask, 8 + 5)) {
                    position.set(Position.PREFIX_ADC + 1, value);
                } else {
                    position.set(Position.PREFIX_ADC + 1, value * 0.1);
                }
            }
        }

        if (type == MSG_GPS_LBS_1 && buf.readableBytes() == 4 + 6) {
            position.set(Position.KEY_ODOMETER, buf.readUnsignedInt());
        }

        if (type == MSG_GPS_LBS_2 && buf.readableBytes() == 3 + 6) {
            position.set(Position.KEY_IGNITION, buf.readUnsignedByte() > 0);
            position.set(Position.KEY_EVENT, buf.readUnsignedByte()); // reason
            position.set(Position.KEY_ARCHIVE, buf.readUnsignedByte() > 0);
        }

    } else {

        if (dataLength > 0) {
            buf.skipBytes(dataLength);
        }
        if (type != MSG_COMMAND_0 && type != MSG_COMMAND_1 && type != MSG_COMMAND_2) {
            sendResponse(channel, false, type, buf.getShort(buf.writerIndex() - 6), null);
        }
        return null;

    }

    if (hasLanguage(type)) {
        buf.readUnsignedShort();
    }

    if (type == MSG_GPS_LBS_STATUS_3 || type == MSG_FENCE_MULTI) {
        position.set(Position.KEY_GEOFENCE, buf.readUnsignedByte());
    }

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

    return position;
}

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

License:Apache License

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

    DeviceSession deviceSession = getDeviceSession(channel, remoteAddress);
    if (deviceSession == null) {
        return null;
    }/* w  w w  .j  a  v  a2s  .c  o m*/

    if (deviceSession.getTimeZone() == null) {
        deviceSession.setTimeZone(getTimeZone(deviceSession.getDeviceId()));
    }

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

    buf.readUnsignedShort(); // length
    int type = buf.readUnsignedByte();

    if (type == MSG_STRING_INFO) {

        buf.readUnsignedInt(); // server flag
        String data;
        if (buf.readUnsignedByte() == 1) {
            data = buf.readSlice(buf.readableBytes() - 6).toString(StandardCharsets.US_ASCII);
        } else {
            data = buf.readSlice(buf.readableBytes() - 6).toString(StandardCharsets.UTF_16BE);
        }

        if (decodeLocationString(position, data) == null) {
            getLastLocation(position, null);
            position.set(Position.KEY_RESULT, data);
        }

        return position;

    } else if (type == MSG_INFO) {

        int subType = buf.readUnsignedByte();

        getLastLocation(position, null);

        if (subType == 0x00) {
            position.set(Position.KEY_POWER, buf.readUnsignedShort() * 0.01);
            return position;
        } else if (subType == 0x05) {
            int flags = buf.readUnsignedByte();
            position.set(Position.KEY_DOOR, BitUtil.check(flags, 0));
            position.set(Position.PREFIX_IO + 1, BitUtil.check(flags, 2));
            return position;
        } else if (subType == 0x0a) {
            buf.skipBytes(8); // imei
            buf.skipBytes(8); // imsi
            position.set("iccid", ByteBufUtil.hexDump(buf.readSlice(8)));
            return position;
        } else if (subType == 0x0d) {
            if (buf.getByte(buf.readerIndex()) != '!') {
                buf.skipBytes(6);
            }
            return decodeFuelData(position,
                    buf.toString(buf.readerIndex(), buf.readableBytes() - 4 - 2, StandardCharsets.US_ASCII));
        }

    } else if (type == MSG_X1_PHOTO_DATA) {

        int pictureId = buf.readInt();

        ByteBuf photo = photos.get(pictureId);

        buf.readUnsignedInt(); // offset
        buf.readBytes(photo, buf.readUnsignedShort());

        if (photo.writableBytes() > 0) {
            sendPhotoRequest(channel, pictureId);
        } else {
            Device device = Context.getDeviceManager().getById(deviceSession.getDeviceId());
            position.set(Position.KEY_IMAGE,
                    Context.getMediaManager().writeFile(device.getUniqueId(), photo, "jpg"));
            photos.remove(pictureId).release();
        }

    } else if (type == MSG_AZ735_GPS || type == MSG_AZ735_ALARM) {

        if (!decodeGps(position, buf, true, deviceSession.getTimeZone())) {
            getLastLocation(position, position.getDeviceTime());
        }

        if (decodeLbs(position, buf, true)) {
            position.set(Position.KEY_RSSI, buf.readUnsignedByte());
        }

        buf.skipBytes(buf.readUnsignedByte()); // additional cell towers
        buf.skipBytes(buf.readUnsignedByte()); // wifi access point

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

        if (type == MSG_AZ735_ALARM) {
            switch (status) {
            case 0xA0:
                position.set(Position.KEY_ARMED, true);
                break;
            case 0xA1:
                position.set(Position.KEY_ARMED, false);
                break;
            case 0xA2:
            case 0xA3:
                position.set(Position.KEY_ALARM, Position.ALARM_LOW_BATTERY);
                break;
            case 0xA4:
                position.set(Position.KEY_ALARM, Position.ALARM_GENERAL);
                break;
            case 0xA5:
                position.set(Position.KEY_ALARM, Position.ALARM_DOOR);
                break;
            default:
                break;
            }
        }

        buf.skipBytes(buf.readUnsignedByte()); // reserved extension

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

        return position;

    } else if (type == MSG_OBD) {

        DateBuilder dateBuilder = new DateBuilder(deviceSession.getTimeZone())
                .setDate(buf.readUnsignedByte(), buf.readUnsignedByte(), buf.readUnsignedByte())
                .setTime(buf.readUnsignedByte(), buf.readUnsignedByte(), buf.readUnsignedByte());

        getLastLocation(position, dateBuilder.getDate());

        position.set(Position.KEY_IGNITION, buf.readUnsignedByte() > 0);

        String data = buf.readCharSequence(buf.readableBytes() - 18, StandardCharsets.US_ASCII).toString();
        for (String pair : data.split(",")) {
            String[] values = pair.split("=");
            switch (Integer.parseInt(values[0].substring(0, 2), 16)) {
            case 40:
                position.set(Position.KEY_ODOMETER, Integer.parseInt(values[1], 16) * 0.01);
                break;
            case 43:
                position.set(Position.KEY_FUEL_LEVEL, Integer.parseInt(values[1], 16) * 0.01);
                break;
            case 45:
                position.set(Position.KEY_COOLANT_TEMP, Integer.parseInt(values[1], 16) * 0.01);
                break;
            case 53:
                position.set(Position.KEY_OBD_SPEED, Integer.parseInt(values[1], 16) * 0.01);
                break;
            case 54:
                position.set(Position.KEY_RPM, Integer.parseInt(values[1], 16) * 0.01);
                break;
            case 71:
                position.set(Position.KEY_FUEL_USED, Integer.parseInt(values[1], 16) * 0.01);
                break;
            case 73:
                position.set(Position.KEY_HOURS, Integer.parseInt(values[1], 16) * 0.01);
                break;
            case 74:
                position.set(Position.KEY_VIN, values[1]);
                break;
            default:
                break;
            }
        }

        return position;

    }

    return null;
}

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

License:Apache License

private Position decodeBinary(ByteBuf buf, Channel channel, SocketAddress remoteAddress) {

    Position position = new Position(getProtocolName());

    boolean longId = buf.readableBytes() == 42;

    buf.readByte(); // marker

    String id;//from  w  w w .  ja v a 2 s.co m
    if (longId) {
        id = ByteBufUtil.hexDump(buf.readSlice(8)).substring(0, 15);
    } else {
        id = ByteBufUtil.hexDump(buf.readSlice(5));
    }

    DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, id);
    if (deviceSession == null) {
        return null;
    }
    position.setDeviceId(deviceSession.getDeviceId());

    DateBuilder dateBuilder = new DateBuilder().setHour(BcdUtil.readInteger(buf, 2))
            .setMinute(BcdUtil.readInteger(buf, 2)).setSecond(BcdUtil.readInteger(buf, 2))
            .setDay(BcdUtil.readInteger(buf, 2)).setMonth(BcdUtil.readInteger(buf, 2))
            .setYear(BcdUtil.readInteger(buf, 2));
    position.setTime(dateBuilder.getDate());

    double latitude = readCoordinate(buf, false);
    position.set(Position.KEY_BATTERY_LEVEL, decodeBattery(buf.readUnsignedByte()));
    double longitude = readCoordinate(buf, true);

    int flags = buf.readUnsignedByte() & 0x0f;
    position.setValid((flags & 0x02) != 0);
    if ((flags & 0x04) == 0) {
        latitude = -latitude;
    }
    if ((flags & 0x08) == 0) {
        longitude = -longitude;
    }

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

    position.setSpeed(BcdUtil.readInteger(buf, 3));
    position.setCourse((buf.readUnsignedByte() & 0x0f) * 100.0 + BcdUtil.readInteger(buf, 2));

    processStatus(position, buf.readUnsignedInt());

    return position;
}

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

License:Apache License

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

    ByteBuf buf = (ByteBuf) msg;/*w  w w .ja  va2s . c  om*/

    buf.readUnsignedByte(); // start marker
    int type = buf.readUnsignedShort();
    buf.readUnsignedShort(); // body length
    ByteBuf id = buf.readSlice(6); // phone number
    int index = buf.readUnsignedShort();

    DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, ByteBufUtil.hexDump(id));
    if (deviceSession == null) {
        return null;
    }

    if (deviceSession.getTimeZone() == null) {
        deviceSession.setTimeZone(getTimeZone(deviceSession.getDeviceId(), "GMT+8"));
    }

    if (type == MSG_TERMINAL_REGISTER) {

        if (channel != null) {
            ByteBuf response = Unpooled.buffer();
            response.writeShort(index);
            response.writeByte(RESULT_SUCCESS);
            response.writeBytes("authentication".getBytes(StandardCharsets.US_ASCII));
            channel.writeAndFlush(new NetworkMessage(
                    formatMessage(MSG_TERMINAL_REGISTER_RESPONSE, id, response), remoteAddress));
        }

    } else if (type == MSG_TERMINAL_AUTH) {

        sendGeneralResponse(channel, remoteAddress, id, type, index);

    } else if (type == MSG_LOCATION_REPORT) {

        return decodeLocation(deviceSession, buf);

    } else if (type == MSG_LOCATION_BATCH) {

        return decodeLocationBatch(deviceSession, buf);

    }

    return null;
}

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

License:Apache License

private List<Position> decodeBinary(ByteBuf buf, Channel channel, SocketAddress remoteAddress) {

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

    buf.readByte(); // header

    boolean longFormat = buf.getUnsignedByte(buf.readerIndex()) == 0x75;

    String id = String.valueOf(Long.parseLong(ByteBufUtil.hexDump(buf.readSlice(5))));
    DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, id);
    if (deviceSession == null) {
        return null;
    }/* w  w w  .  j  a  v a  2  s  . com*/

    int protocolVersion = 0;
    if (longFormat) {
        protocolVersion = buf.readUnsignedByte();
    }

    int version = BitUtil.from(buf.readUnsignedByte(), 4);
    buf.readUnsignedShort(); // length

    while (buf.readableBytes() > 1) {

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

        DateBuilder dateBuilder = new DateBuilder().setDay(BcdUtil.readInteger(buf, 2))
                .setMonth(BcdUtil.readInteger(buf, 2)).setYear(BcdUtil.readInteger(buf, 2))
                .setHour(BcdUtil.readInteger(buf, 2)).setMinute(BcdUtil.readInteger(buf, 2))
                .setSecond(BcdUtil.readInteger(buf, 2));
        position.setTime(dateBuilder.getDate());

        double latitude = convertCoordinate(BcdUtil.readInteger(buf, 8));
        double longitude = convertCoordinate(BcdUtil.readInteger(buf, 9));

        byte flags = buf.readByte();
        position.setValid((flags & 0x1) == 0x1);
        if ((flags & 0x2) == 0) {
            latitude = -latitude;
        }
        position.setLatitude(latitude);
        if ((flags & 0x4) == 0) {
            longitude = -longitude;
        }
        position.setLongitude(longitude);

        position.setSpeed(BcdUtil.readInteger(buf, 2));
        position.setCourse(buf.readUnsignedByte() * 2.0);

        if (longFormat) {

            position.set(Position.KEY_ODOMETER, buf.readUnsignedInt() * 1000);
            position.set(Position.KEY_SATELLITES, buf.readUnsignedByte());

            buf.readUnsignedInt(); // vehicle id combined

            int status = buf.readUnsignedShort();
            position.set(Position.KEY_ALARM, BitUtil.check(status, 1) ? Position.ALARM_GEOFENCE_ENTER : null);
            position.set(Position.KEY_ALARM, BitUtil.check(status, 2) ? Position.ALARM_GEOFENCE_EXIT : null);
            position.set(Position.KEY_ALARM, BitUtil.check(status, 3) ? Position.ALARM_POWER_CUT : null);
            position.set(Position.KEY_ALARM, BitUtil.check(status, 4) ? Position.ALARM_VIBRATION : null);
            position.set(Position.KEY_BLOCKED, BitUtil.check(status, 7));
            position.set(Position.KEY_ALARM, BitUtil.check(status, 8 + 3) ? Position.ALARM_LOW_BATTERY : null);
            position.set(Position.KEY_ALARM, BitUtil.check(status, 8 + 6) ? Position.ALARM_FAULT : null);
            position.set(Position.KEY_STATUS, status);

            int battery = buf.readUnsignedByte();
            if (battery == 0xff) {
                position.set(Position.KEY_CHARGE, true);
            } else {
                position.set(Position.KEY_BATTERY_LEVEL, battery);
            }

            CellTower cellTower = CellTower.fromCidLac(buf.readUnsignedShort(), buf.readUnsignedShort());
            cellTower.setSignalStrength((int) buf.readUnsignedByte());
            position.setNetwork(new Network(cellTower));

            if (protocolVersion == 0x17) {
                buf.readUnsignedByte(); // geofence id
                buf.skipBytes(3); // reserved
            }

        } else if (version == 1) {

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

            buf.readByte(); // other flags and sensors

            position.setAltitude(buf.readUnsignedShort());

            int cid = buf.readUnsignedShort();
            int lac = buf.readUnsignedShort();
            int rssi = buf.readUnsignedByte();

            if (cid != 0 && lac != 0) {
                CellTower cellTower = CellTower.fromCidLac(cid, lac);
                cellTower.setSignalStrength(rssi);
                position.setNetwork(new Network(cellTower));
            } else {
                position.set(Position.KEY_RSSI, rssi);
            }

        } else if (version == 2) {

            int fuel = buf.readUnsignedByte() << 8;

            decodeStatus(position, buf);
            position.set(Position.KEY_ODOMETER, buf.readUnsignedInt() * 1000);

            fuel += buf.readUnsignedByte();
            position.set(Position.KEY_FUEL_LEVEL, fuel);

        } else if (version == 3) {

            BitBuffer bitBuffer = new BitBuffer(buf);

            position.set("fuel1", bitBuffer.readUnsigned(12));
            position.set("fuel2", bitBuffer.readUnsigned(12));
            position.set("fuel3", bitBuffer.readUnsigned(12));
            position.set(Position.KEY_ODOMETER, bitBuffer.readUnsigned(20) * 1000);

            int status = bitBuffer.readUnsigned(24);
            position.set(Position.KEY_IGNITION, BitUtil.check(status, 0));
            position.set(Position.KEY_STATUS, status);

        }

        positions.add(position);

    }

    buf.readUnsignedByte(); // index

    return positions;
}

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

License:Apache License

private Position decodeArMessage(Channel channel, SocketAddress remoteAddress, ByteBuf buf) {

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

    int type = buf.readUnsignedByte();

    int tag = buf.readUnsignedByte();

    DeviceSession deviceSession;//from   w w w  .j  av a 2 s  .c  o  m
    switch (BitUtil.to(tag, 3)) {
    case 0:
        String imei = ByteBufUtil.hexDump(buf.readSlice(8)).substring(1);
        deviceSession = getDeviceSession(channel, remoteAddress, imei);
        break;
    case 1:
        buf.skipBytes(1);
        String meid = buf.readSlice(14).toString(StandardCharsets.US_ASCII);
        deviceSession = getDeviceSession(channel, remoteAddress, meid);
        break;
    default:
        deviceSession = getDeviceSession(channel, remoteAddress);
        break;
    }

    if (deviceSession == null || type != MSG_AR_LOCATION) {
        return null;
    }

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

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

    int mask = buf.readInt();

    if (BitUtil.check(mask, 0)) {
        position.set(Position.KEY_INDEX, buf.readUnsignedShort());
    }

    if (BitUtil.check(mask, 1)) {
        int date = buf.readUnsignedByte();
        DateBuilder dateBuilder = new DateBuilder()
                .setDate(BitUtil.between(date, 4, 8) + 2010, BitUtil.to(date, 4), buf.readUnsignedByte())
                .setTime(buf.readUnsignedByte(), buf.readUnsignedByte(), buf.readUnsignedByte());
        position.setTime(dateBuilder.getDate());
    }

    if (BitUtil.check(mask, 2)) {
        buf.skipBytes(5); // device time
    }

    if (BitUtil.check(mask, 3)) {
        position.setLatitude(buf.readUnsignedInt() * 0.000001 - 90);
        position.setLongitude(buf.readUnsignedInt() * 0.000001 - 180.0);
    }

    if (BitUtil.check(mask, 4)) {
        int status = buf.readUnsignedByte();
        position.setValid(BitUtil.between(status, 4, 8) != 0);
        position.set(Position.KEY_SATELLITES, BitUtil.to(status, 4));
        position.set(Position.KEY_HDOP, buf.readUnsignedByte() * 0.1);
    }

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

    if (BitUtil.check(mask, 6)) {
        position.setCourse(buf.readUnsignedShort());
    }

    if (BitUtil.check(mask, 7)) {
        position.setAltitude(buf.readShort());
    }

    if (BitUtil.check(mask, 8)) {
        position.set(Position.KEY_RSSI, buf.readUnsignedByte());
    }

    if (BitUtil.check(mask, 9)) {
        position.set(Position.KEY_POWER, buf.readUnsignedShort() * 0.001);
    }

    if (BitUtil.check(mask, 10)) {
        position.set(Position.KEY_BATTERY, buf.readUnsignedShort() * 0.001);
    }

    if (BitUtil.check(mask, 11)) {
        buf.skipBytes(2); // gpio
    }

    if (BitUtil.check(mask, 12)) {
        position.set(Position.KEY_ODOMETER, buf.readUnsignedInt() * 1000);
    }

    if (BitUtil.check(mask, 13)) {
        buf.skipBytes(6); // software version
    }

    if (BitUtil.check(mask, 14)) {
        buf.skipBytes(5); // hardware version
    }

    if (BitUtil.check(mask, 15)) {
        buf.readUnsignedShort(); // device config
    }

    return position;
}

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

License:Apache License

private Position decodeMgMessage(Channel channel, SocketAddress remoteAddress, ByteBuf buf) {

    buf.readUnsignedByte(); // tag
    int flags = buf.getUnsignedByte(buf.readerIndex());

    DeviceSession deviceSession;//from  ww  w  . j  a  v  a  2 s .  c om
    if (BitUtil.check(flags, 6)) {
        buf.readUnsignedByte(); // flags
        deviceSession = getDeviceSession(channel, remoteAddress);
    } else {
        String imei = ByteBufUtil.hexDump(buf.readSlice(8)).substring(1);
        deviceSession = getDeviceSession(channel, remoteAddress, imei);
    }

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

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

    buf.skipBytes(8); // imsi

    int date = buf.readUnsignedShort();

    DateBuilder dateBuilder = new DateBuilder()
            .setDate(2010 + BitUtil.from(date, 12), BitUtil.between(date, 8, 12), BitUtil.to(date, 8))
            .setTime(buf.readUnsignedByte(), buf.readUnsignedByte(), 0);

    position.setValid(true);
    position.setLatitude(convertCoordinate(buf.readInt()));
    position.setLongitude(convertCoordinate(buf.readInt()));

    position.setAltitude(UnitsConverter.metersFromFeet(buf.readShort()));
    position.setCourse(buf.readUnsignedShort());
    position.setSpeed(UnitsConverter.knotsFromMph(buf.readUnsignedByte()));

    position.set(Position.KEY_POWER, buf.readUnsignedByte() * 0.1);
    position.set(Position.PREFIX_IO + 1, buf.readUnsignedByte());

    dateBuilder.setSecond(buf.readUnsignedByte());
    position.setTime(dateBuilder.getDate());

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

    int index = buf.readUnsignedByte();

    position.set(Position.KEY_VERSION_FW, buf.readUnsignedByte());
    position.set(Position.KEY_SATELLITES, buf.readUnsignedByte());
    position.set(Position.KEY_ODOMETER, (long) (buf.readUnsignedInt() * 1609.34));

    if (channel != null && BitUtil.check(flags, 7)) {
        ByteBuf response = Unpooled.buffer();
        response.writeByte(MSG_ACKNOWLEDGEMENT);
        response.writeByte(index);
        response.writeByte(0x00);
        channel.writeAndFlush(new NetworkMessage(response, remoteAddress));
    }

    return position;
}

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

License:Apache License

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

    ByteBuf buf = (ByteBuf) msg;/*from   w  ww  .j a v  a 2 s. com*/
    int type = buf.readUnsignedShortLE();
    buf.readUnsignedShortLE(); // length

    if (type == MSG_IDENT || type == MSG_IDENT_FULL) {

        buf.readUnsignedIntLE(); // id
        int length = buf.readUnsignedShortLE();
        buf.skipBytes(length);
        length = buf.readUnsignedShortLE();
        buf.skipBytes(length);
        length = buf.readUnsignedShortLE();
        String imei = buf.readSlice(length).toString(StandardCharsets.US_ASCII);
        getDeviceSession(channel, remoteAddress, imei);

    } else if (type == MSG_POINT || type == MSG_ALARM || type == MSG_LOGMSG) {

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

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

        int recordCount = 1;
        if (type == MSG_LOGMSG) {
            recordCount = buf.readUnsignedShortLE();
        }

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

            if (type == MSG_LOGMSG) {
                position.set(Position.KEY_ARCHIVE, true);
                int subtype = buf.readUnsignedShortLE();
                if (subtype == MSG_ALARM) {
                    position.set(Position.KEY_ALARM, Position.ALARM_GENERAL);
                }
                if (buf.readUnsignedShortLE() > buf.readableBytes()) {
                    lastIndex += 1;
                    break; // workaround for device bug
                }
                lastIndex = buf.readUnsignedIntLE();
                position.set(Position.KEY_INDEX, lastIndex);
            } else {
                newIndex = buf.readUnsignedIntLE();
            }

            position.setTime(new Date(buf.readUnsignedIntLE() * 1000));
            position.setLatitude(buf.readIntLE() * 180.0 / 0x7FFFFFFF);
            position.setLongitude(buf.readIntLE() * 180.0 / 0x7FFFFFFF);
            position.setSpeed(buf.readUnsignedIntLE() * 0.01);
            position.setCourse(buf.readUnsignedShortLE() * 0.01);
            position.setAltitude(buf.readUnsignedShortLE() * 0.01);

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

            position.set(Position.KEY_RSSI, buf.readUnsignedByte());
            position.set(Position.KEY_ODOMETER, buf.readUnsignedIntLE());

            long extraFlags = buf.readLongLE();

            if (BitUtil.check(extraFlags, 0)) {
                int count = buf.readUnsignedShortLE();
                for (int i = 1; i <= count; i++) {
                    position.set(Position.PREFIX_ADC + i, buf.readUnsignedShortLE());
                }
            }

            if (BitUtil.check(extraFlags, 1)) {
                int size = buf.readUnsignedShortLE();
                position.set("can", buf.toString(buf.readerIndex(), size, StandardCharsets.US_ASCII));
                buf.skipBytes(size);
            }

            if (BitUtil.check(extraFlags, 2)) {
                position.set("passenger", ByteBufUtil.hexDump(buf.readSlice(buf.readUnsignedShortLE())));
            }

            if (type == MSG_ALARM) {
                position.set(Position.KEY_ALARM, true);
                byte[] response = { (byte) 0xC9, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
                channel.writeAndFlush(new NetworkMessage(Unpooled.wrappedBuffer(response), remoteAddress));
            }

            buf.readUnsignedIntLE(); // crc

            positions.add(position);
        }

        requestArchive(channel);

        return positions;
    }

    return null;
}

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;//from  w ww.  ja v a  2  s.com

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

    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;/*  w  w  w.ja  v a 2  s  .  c o m*/

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

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