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.Gt02ProtocolDecoder.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.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;
    }//from  www.ja va2s .c o m
    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;/* w ww . ja  v a 2s  . c  o  m*/
    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;
}

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

License:Apache License

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

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

    ByteBuf time = buf.readSlice(6);
    DateBuilder dateBuilder = new DateBuilder().setYear(BcdUtil.readInteger(time, 2))
            .setMonth(BcdUtil.readInteger(time, 2)).setDay(BcdUtil.readInteger(time, 2))
            .setHour(BcdUtil.readInteger(time, 2)).setMinute(BcdUtil.readInteger(time, 2))
            .setSecond(BcdUtil.readInteger(time, 2));
    getLastLocation(position, dateBuilder.getDate());

    Network network = new Network();

    int wifiCount = buf.getByte(2);
    for (int i = 0; i < wifiCount; i++) {
        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.readUnsignedByte()));
    }/*w w w.  j a va  2s . com*/

    int cellCount = buf.readUnsignedByte();
    int mcc = buf.readUnsignedShort();
    int mnc = buf.readUnsignedByte();
    for (int i = 0; i < cellCount; i++) {
        network.addCellTower(CellTower.from(mcc, mnc, buf.readUnsignedShort(), buf.readUnsignedShort(),
                buf.readUnsignedByte()));
    }

    position.setNetwork(network);

    if (channel != null) {
        ByteBuf response = Unpooled.buffer();
        response.writeShort(0x7878);
        response.writeByte(0);
        response.writeByte(type);
        response.writeBytes(time.resetReaderIndex());
        response.writeByte('\r');
        response.writeByte('\n');
        channel.writeAndFlush(new NetworkMessage(response, channel.remoteAddress()));
    }

    return position;
}

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   w ww.  j  av  a  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;
    }//from   ww w .j  a  va 2 s .  co 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 ww.  j  a va  2 s .  c  o 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;

    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;
    }//  www  . j  a va2 s . c o m

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

License:Apache License

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

    ByteBuf buf = (ByteBuf) msg;

    buf.skipBytes(1); // start marker
    buf.readUnsignedByte(); // flag
    buf.readUnsignedByte(); // reserved
    buf.readUnsignedShort(); // length

    int type = buf.readUnsignedShort();

    buf.readUnsignedShort(); // checksum
    int index = buf.readInt();

    if (type == MSG_LOGIN) {

        while (buf.readableBytes() > 4) {
            int subtype = buf.readUnsignedShort();
            int length = buf.readUnsignedShort() - 4;
            if (subtype == 0x0003) {
                String imei = buf.readSlice(length).toString(StandardCharsets.US_ASCII);
                DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, imei);
                if (deviceSession != null && channel != null) {
                    ByteBuf content = Unpooled.buffer();
                    content.writeByte(0); // success
                    sendResponse(channel, MSG_LOGIN_RSP, index, content);
                }/*from  w  w w .  j a  v  a 2s  .  c o m*/
            } else {
                buf.skipBytes(length);
            }
        }

    } else if (type == MSG_HSO_REQ) {

        sendResponse(channel, MSG_HSO_RSP, index, null);

    } else if (type == MSG_POSITION) {

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

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

        int status = buf.readUnsignedShort();

        position.setValid(BitUtil.check(status, 15));

        position.set(Position.KEY_STATUS, status);
        position.set(Position.KEY_IGNITION, BitUtil.check(status, 14));
        position.set(Position.KEY_EVENT, buf.readUnsignedShort());

        String time = buf.readSlice(12).toString(StandardCharsets.US_ASCII);

        DateBuilder dateBuilder = new DateBuilder().setYear(Integer.parseInt(time.substring(0, 2)))
                .setMonth(Integer.parseInt(time.substring(2, 4))).setDay(Integer.parseInt(time.substring(4, 6)))
                .setHour(Integer.parseInt(time.substring(6, 8)))
                .setMinute(Integer.parseInt(time.substring(8, 10)))
                .setSecond(Integer.parseInt(time.substring(10, 12)));
        position.setTime(dateBuilder.getDate());

        position.setLongitude(buf.readInt() * 0.00001);
        position.setLatitude(buf.readInt() * 0.00001);

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

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

        while (buf.readableBytes() > 4) {
            buf.readUnsignedShort(); // subtype
            int length = buf.readUnsignedShort() - 4;
            buf.skipBytes(length);
        }

        sendResponse(channel, MSG_POSITION_RSP, index, null);

        return position;

    }

    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;
    }/*ww  w.j av a  2 s .c  o m*/

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

License:Apache License

private List<Position> decodeRetransmission(ByteBuf buf, DeviceSession deviceSession) {
    List<Position> positions = new LinkedList<>();

    int count = buf.readUnsignedByte();
    for (int i = 0; i < count; i++) {

        buf.readUnsignedByte(); // alarm

        int endIndex = buf.indexOf(buf.readerIndex(), buf.writerIndex(), (byte) '\\');
        if (endIndex < 0) {
            endIndex = buf.writerIndex() - 4;
        }/* w  w w .  ja v  a 2s .  c o  m*/

        String sentence = buf.readSlice(endIndex - buf.readerIndex()).toString(StandardCharsets.US_ASCII);

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

        position = decodeRegular(position, sentence);

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

        if (buf.readableBytes() > 4) {
            buf.readUnsignedByte(); // delimiter
        }

    }

    return positions;
}