Example usage for io.netty.buffer ByteBuf readShort

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

Introduction

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

Prototype

public abstract short readShort();

Source Link

Document

Gets a 16-bit short integer at the current readerIndex and increases the readerIndex by 2 in this buffer.

Usage

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

License:Apache License

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

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

    int type = buf.readUnsignedByte();

    buf.readUnsignedInt(); // mask
    buf.readUnsignedShort(); // length
    buf.readUnsignedByte(); // device type
    buf.readUnsignedShort(); // protocol version
    buf.readUnsignedShort(); // firmware version

    DeviceSession deviceSession = getDeviceSession(channel, remoteAddress,
            String.format("%015d", buf.readLong()));
    if (deviceSession == null) {
        return null;
    }//w  w  w. j  ava2s .com

    int battery = buf.readUnsignedByte();
    int power = buf.readUnsignedShort();

    if (type == MSG_RSP_GEO) {
        buf.readUnsignedByte(); // reserved
        buf.readUnsignedByte(); // reserved
    }

    buf.readUnsignedByte(); // motion status
    int satellites = buf.readUnsignedByte();

    if (type != MSG_RSP_COMPRESSED) {
        buf.readUnsignedByte(); // index
    }

    if (type == MSG_RSP_LCB) {
        buf.readUnsignedByte(); // phone length
        for (int b = buf.readUnsignedByte();; b = buf.readUnsignedByte()) {
            if ((b & 0xf) == 0xf || (b & 0xf0) == 0xf0) {
                break;
            }
        }
    }

    if (type == MSG_RSP_COMPRESSED) {

        int count = buf.readUnsignedShort();

        BitBuffer bits;
        int speed = 0;
        int heading = 0;
        int latitude = 0;
        int longitude = 0;
        long time = 0;

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

            if (time > 0) {
                time += 1;
            }

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

            switch (BitUtil.from(buf.getUnsignedByte(buf.readerIndex()), 8 - 2)) {
            case 1:
                bits = new BitBuffer(buf.readSlice(3));
                bits.readUnsigned(2); // point attribute
                bits.readUnsigned(1); // fix type
                speed = bits.readUnsigned(12);
                heading = bits.readUnsigned(9);
                longitude = buf.readInt();
                latitude = buf.readInt();
                if (time == 0) {
                    time = buf.readUnsignedInt();
                }
                break;
            case 2:
                bits = new BitBuffer(buf.readSlice(5));
                bits.readUnsigned(2); // point attribute
                bits.readUnsigned(1); // fix type
                speed += bits.readSigned(7);
                heading += bits.readSigned(7);
                longitude += bits.readSigned(12);
                latitude += bits.readSigned(11);
                break;
            default:
                buf.readUnsignedByte(); // invalid or same
                continue;
            }

            position.setValid(true);
            position.setTime(new Date(time * 1000));
            position.setSpeed(UnitsConverter.knotsFromKph(speed * 0.1));
            position.setCourse(heading);
            position.setLongitude(longitude * 0.000001);
            position.setLatitude(latitude * 0.000001);

            positions.add(position);

        }

    } else {

        int count = buf.readUnsignedByte();

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

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

            position.set(Position.KEY_BATTERY_LEVEL, battery);
            position.set(Position.KEY_POWER, power);
            position.set(Position.KEY_SATELLITES, satellites);

            int hdop = buf.readUnsignedByte();
            position.setValid(hdop > 0);
            position.set(Position.KEY_HDOP, hdop);

            position.setSpeed(UnitsConverter.knotsFromKph(buf.readUnsignedMedium() * 0.1));
            position.setCourse(buf.readUnsignedShort());
            position.setAltitude(buf.readShort());
            position.setLongitude(buf.readInt() * 0.000001);
            position.setLatitude(buf.readInt() * 0.000001);

            position.setTime(decodeTime(buf));

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

            buf.readUnsignedByte(); // reserved

            positions.add(position);

        }

    }

    return positions;
}

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

License:Apache License

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

    Position position = new Position(getProtocolName());

    int type = buf.readUnsignedByte();

    buf.readUnsignedInt(); // mask
    buf.readUnsignedShort(); // length
    buf.readUnsignedByte(); // device type
    buf.readUnsignedShort(); // protocol version

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

    DeviceSession deviceSession = getDeviceSession(channel, remoteAddress,
            String.format("%015d", buf.readLong()));
    if (deviceSession == null) {
        return null;
    }//  w ww.  j a  va2  s .c om
    position.setDeviceId(deviceSession.getDeviceId());

    position.set(Position.KEY_BATTERY_LEVEL, buf.readUnsignedByte());
    position.set(Position.KEY_POWER, buf.readUnsignedShort());

    buf.readUnsignedByte(); // motion status

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

    switch (type) {
    case MSG_EVT_BPL:
        buf.readUnsignedShort(); // backup battery voltage
        break;
    case MSG_EVT_VGN:
    case MSG_EVT_VGF:
        buf.readUnsignedShort(); // reserved
        buf.readUnsignedByte(); // report type
        buf.readUnsignedInt(); // ignition duration
        break;
    case MSG_EVT_UPD:
        buf.readUnsignedShort(); // code
        buf.readUnsignedByte(); // retry
        break;
    case MSG_EVT_IDF:
        buf.readUnsignedInt(); // idling duration
        break;
    case MSG_EVT_GSS:
        buf.readUnsignedByte(); // gps signal status
        buf.readUnsignedInt(); // reserved
        break;
    case MSG_EVT_GES:
        buf.readUnsignedShort(); // trigger geo id
        buf.readUnsignedByte(); // trigger geo enable
        buf.readUnsignedByte(); // trigger mode
        buf.readUnsignedInt(); // radius
        buf.readUnsignedInt(); // check interval
        break;
    case MSG_EVT_GPJ:
        buf.readUnsignedByte(); // cw jamming value
        buf.readUnsignedByte(); // gps jamming state
        break;
    case MSG_EVT_RMD:
        buf.readUnsignedByte(); // roaming state
        break;
    case MSG_EVT_JDS:
        buf.readUnsignedByte(); // jamming state
        break;
    case MSG_EVT_CRA:
        buf.readUnsignedByte(); // crash counter
        break;
    case MSG_EVT_UPC:
        buf.readUnsignedByte(); // command id
        buf.readUnsignedShort(); // result
        break;
    default:
        break;
    }

    buf.readUnsignedByte(); // count

    int hdop = buf.readUnsignedByte();
    position.setValid(hdop > 0);
    position.set(Position.KEY_HDOP, hdop);

    position.setSpeed(UnitsConverter.knotsFromKph(buf.readUnsignedMedium() * 0.1));
    position.setCourse(buf.readUnsignedShort());
    position.setAltitude(buf.readShort());
    position.setLongitude(buf.readInt() * 0.000001);
    position.setLatitude(buf.readInt() * 0.000001);

    position.setTime(decodeTime(buf));

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

    buf.readUnsignedByte(); // reserved

    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  .  jav  a 2s . com*/
    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

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

    ByteBuf buf = (ByteBuf) msg;

    int header = buf.readShort();

    if (header == 0x7878) {
        return decodeBasic(channel, remoteAddress, buf);
    } else if (header == 0x7979) {
        return decodeExtended(channel, remoteAddress, buf);
    }//from   w w w. ja va  2s. c o  m

    return null;
}

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

License:Apache License

private Position decodeLocation(DeviceSession deviceSession, ByteBuf buf) {

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

    position.set(Position.KEY_ALARM, decodeAlarm(buf.readUnsignedInt()));

    int flags = buf.readInt();

    position.set(Position.KEY_IGNITION, BitUtil.check(flags, 0));

    position.setValid(BitUtil.check(flags, 1));

    double lat = buf.readUnsignedInt() * 0.000001;
    double lon = buf.readUnsignedInt() * 0.000001;

    if (BitUtil.check(flags, 2)) {
        position.setLatitude(-lat);//from  w w w  .  j av a2 s  .c o  m
    } else {
        position.setLatitude(lat);
    }

    if (BitUtil.check(flags, 3)) {
        position.setLongitude(-lon);
    } else {
        position.setLongitude(lon);
    }

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

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

    while (buf.readableBytes() > 2) {
        int subtype = buf.readUnsignedByte();
        int length = buf.readUnsignedByte();
        switch (subtype) {
        case 0x01:
            position.set(Position.KEY_ODOMETER, buf.readUnsignedInt() * 100);
            break;
        case 0x30:
            position.set(Position.KEY_RSSI, buf.readUnsignedByte());
            break;
        case 0x31:
            position.set(Position.KEY_SATELLITES, buf.readUnsignedByte());
            break;
        default:
            buf.skipBytes(length);
            break;
        }
    }

    return position;
}

From source file:org.traccar.protocol.MeiligaoProtocolDecoder.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.readShort(); // length
    ByteBuf id = buf.readSlice(7);/*from  ww  w.j a v  a2s .c  om*/
    int command = buf.readUnsignedShort();

    if (command == MSG_LOGIN) {
        ByteBuf response = Unpooled.wrappedBuffer(new byte[] { 0x01 });
        sendResponse(channel, remoteAddress, id, MSG_LOGIN_RESPONSE, response);
        return null;
    } else if (command == MSG_HEARTBEAT) {
        ByteBuf response = Unpooled.wrappedBuffer(new byte[] { 0x01 });
        sendResponse(channel, remoteAddress, id, MSG_HEARTBEAT, response);
        return null;
    } else if (command == MSG_SERVER) {
        ByteBuf response = Unpooled.copiedBuffer(getServer(channel, ':'), StandardCharsets.US_ASCII);
        sendResponse(channel, remoteAddress, id, MSG_SERVER, response);
        return null;
    } else if (command == MSG_UPLOAD_PHOTO) {
        byte imageIndex = buf.readByte();
        photos.put(imageIndex, Unpooled.buffer());
        ByteBuf response = Unpooled.copiedBuffer(new byte[] { imageIndex });
        sendResponse(channel, remoteAddress, id, MSG_UPLOAD_PHOTO_RESPONSE, response);
        return null;
    } else if (command == MSG_UPLOAD_COMPLETE) {
        byte imageIndex = buf.readByte();
        ByteBuf response = Unpooled.copiedBuffer(new byte[] { imageIndex, 0, 0 });
        sendResponse(channel, remoteAddress, id, MSG_RETRANSMISSION, response);
        return null;
    }

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

    if (command == MSG_DATA_PHOTO) {

        byte imageIndex = buf.readByte();
        buf.readUnsignedShort(); // image footage
        buf.readUnsignedByte(); // total packets
        buf.readUnsignedByte(); // packet index

        photos.get(imageIndex).writeBytes(buf, buf.readableBytes() - 2 - 2);

        return null;

    } else if (command == MSG_RETRANSMISSION) {

        return decodeRetransmission(buf, deviceSession);

    } else {

        Position position = new Position(getProtocolName());

        position.setDeviceId(deviceSession.getDeviceId());

        if (command == MSG_ALARM) {
            short alarmCode = buf.readUnsignedByte();
            position.set(Position.KEY_ALARM, decodeAlarm(alarmCode));
            if (alarmCode >= 0x02 && alarmCode <= 0x05) {
                position.set(Position.PREFIX_IN + alarmCode, 1);
            } else if (alarmCode >= 0x32 && alarmCode <= 0x35) {
                position.set(Position.PREFIX_IN + (alarmCode - 0x30), 0);
            }
        } else if (command == MSG_POSITION_LOGGED) {
            buf.skipBytes(6);
        } else if (command == MSG_RFID) {
            for (int i = 0; i < 15; i++) {
                long rfid = buf.readUnsignedInt();
                if (rfid != 0) {
                    String card = String.format("%010d", rfid);
                    position.set("card" + (i + 1), card);
                    position.set(Position.KEY_DRIVER_UNIQUE_ID, card);
                }
            }
        } else if (command == MSG_POSITION_IMAGE) {
            byte imageIndex = buf.readByte();
            buf.readUnsignedByte(); // image upload type
            String uniqueId = Context.getIdentityManager().getById(deviceSession.getDeviceId()).getUniqueId();
            ByteBuf photo = photos.remove(imageIndex);
            try {
                position.set(Position.KEY_IMAGE, Context.getMediaManager().writeFile(uniqueId, photo, "jpg"));
            } finally {
                photo.release();
            }
        }

        String sentence = buf.toString(buf.readerIndex(), buf.readableBytes() - 4, StandardCharsets.US_ASCII);

        switch (command) {
        case MSG_POSITION:
        case MSG_POSITION_LOGGED:
        case MSG_ALARM:
        case MSG_POSITION_IMAGE:
            return decodeRegular(position, sentence);
        case MSG_RFID:
            return decodeRfid(position, sentence);
        case MSG_OBD_RT:
            return decodeObd(position, sentence);
        case MSG_OBD_RTA:
            return decodeObdA(position, sentence);
        default:
            return null;
        }

    }
}

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

License:Apache License

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

    ByteBuf buf = (ByteBuf) msg;

    if (buf.getUnsignedByte(buf.readerIndex()) == 0) {

        buf.readUnsignedShort(); // length

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

        if (getDeviceSession(channel, remoteAddress, imei) != null) {
            sendResponse(channel, remoteAddress, "OK");
        } else {/*from w w w  .  j a  v a  2  s  . co  m*/
            sendResponse(channel, remoteAddress, "NO01");
        }

    } else {

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

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

        buf.skipBytes(4); // marker
        buf.readUnsignedShort(); // length
        buf.readLong(); // imei
        buf.readUnsignedByte(); // codec
        int count = buf.readUnsignedByte();

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

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

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

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

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

            position.setSpeed(UnitsConverter.knotsFromKph(buf.readUnsignedShort()));
            position.setValid(buf.readUnsignedByte() != 0);

            buf.readUnsignedByte(); // used systems

            buf.readUnsignedByte(); // cause element id

            // Read 1 byte data
            int cnt = buf.readUnsignedByte();
            for (int j = 0; j < cnt; j++) {
                position.set(Position.PREFIX_IO + buf.readUnsignedByte(), buf.readUnsignedByte());
            }

            // Read 2 byte data
            cnt = buf.readUnsignedByte();
            for (int j = 0; j < cnt; j++) {
                position.set(Position.PREFIX_IO + buf.readUnsignedByte(), buf.readUnsignedShort());
            }

            // Read 4 byte data
            cnt = buf.readUnsignedByte();
            for (int j = 0; j < cnt; j++) {
                position.set(Position.PREFIX_IO + buf.readUnsignedByte(), buf.readUnsignedInt());
            }

            // Read 8 byte data
            cnt = buf.readUnsignedByte();
            for (int j = 0; j < cnt; j++) {
                position.set(Position.PREFIX_IO + buf.readUnsignedByte(), buf.readLong());
            }

            positions.add(position);
        }

        return positions;

    }

    return null;
}

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   www .ja  v  a2s  .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  w ww  . j  a  v a  2s.co  m
    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.RetranslatorProtocolDecoder.java

License:Apache License

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

    if (channel != null) {
        channel.writeAndFlush(new NetworkMessage(Unpooled.wrappedBuffer(new byte[] { 0x11 }), remoteAddress));
    }//from   ww  w.ja  v a 2  s  .c o m

    ByteBuf buf = (ByteBuf) msg;

    buf.readUnsignedInt(); // length

    int idLength = buf.indexOf(buf.readerIndex(), buf.writerIndex(), (byte) 0x00) - buf.readerIndex();
    String id = buf.readBytes(idLength).toString(StandardCharsets.US_ASCII);
    buf.readByte();
    DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, id);
    if (deviceSession == null) {
        return null;
    }

    Position position = new Position(getProtocolName());
    position.setDeviceId(deviceSession.getDeviceId());
    position.setTime(new Date(buf.readUnsignedInt() * 1000));

    buf.readUnsignedInt(); // bit flags

    while (buf.isReadable()) {

        buf.readUnsignedShort(); // block type
        int blockEnd = buf.readInt() + buf.readerIndex();
        buf.readUnsignedByte(); // security attribute
        int dataType = buf.readUnsignedByte();

        int nameLength = buf.indexOf(buf.readerIndex(), buf.writerIndex(), (byte) 0x00) - buf.readerIndex();
        String name = buf.readBytes(nameLength).toString(StandardCharsets.US_ASCII);
        buf.readByte();

        if (name.equals("posinfo")) {
            position.setValid(true);
            position.setLongitude(buf.readDoubleLE());
            position.setLatitude(buf.readDoubleLE());
            position.setAltitude(buf.readDoubleLE());
            position.setSpeed(buf.readShort());
            position.setCourse(buf.readShort());
            position.set(Position.KEY_SATELLITES, buf.readByte());
        } else {
            switch (dataType) {
            case 1:
                int len = buf.indexOf(buf.readerIndex(), buf.writerIndex(), (byte) 0x00) - buf.readerIndex();
                position.set(name, buf.readBytes(len).toString(StandardCharsets.US_ASCII));
                buf.readByte();
                break;
            case 3:
                position.set(name, buf.readInt());
                break;
            case 4:
                position.set(name, buf.readDoubleLE());
                break;
            case 5:
                position.set(name, buf.readLong());
                break;
            default:
                break;
            }
        }

        buf.readerIndex(blockEnd);

    }

    if (position.getLatitude() == 0 && position.getLongitude() == 0) {
        getLastLocation(position, position.getDeviceTime());
    }

    return position;
}