Example usage for io.netty.buffer ByteBuf getByte

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

Introduction

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

Prototype

public abstract byte getByte(int index);

Source Link

Document

Gets a byte at the specified absolute index in this buffer.

Usage

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

License:Apache License

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

    ByteBuf buf = (ByteBuf) msg;

    // Find IMEI number
    int index = -1;
    for (int i = buf.readerIndex(); i < buf.writerIndex() - IMEI_LENGTH; i++) {
        index = i;//w ww .  jav a  2 s .  c  o  m
        for (int j = i; j < i + IMEI_LENGTH; j++) {
            if (!Character.isDigit((char) buf.getByte(j))) {
                index = -1;
                break;
            }
        }
        if (index > 0) {
            break;
        }
    }
    if (index == -1) {
        return null;
    }

    String imei = buf.toString(index, IMEI_LENGTH, StandardCharsets.US_ASCII);
    DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, imei);
    if (deviceSession == null) {
        return null;
    }

    // Find NMEA sentence
    int start = BufferUtil.indexOf("GPRMC", buf);
    if (start == -1) {
        return null;
    }

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

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

    DateBuilder dateBuilder = new DateBuilder().setTime(parser.nextInt(0), parser.nextInt(0),
            parser.nextInt(0));

    position.setValid(parser.next().equals("A"));
    position.setLatitude(parser.nextCoordinate());
    position.setLongitude(parser.nextCoordinate());
    position.setSpeed(parser.nextDouble(0));
    position.setCourse(parser.nextDouble(0));

    dateBuilder.setDateReverse(parser.nextInt(0), parser.nextInt(0), parser.nextInt(0));
    position.setTime(dateBuilder.getDate());

    return position;
}

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

License:Apache License

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

    ByteBuf buf = (ByteBuf) msg;

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

    String id = decodeId(buf.readUnsignedByte(), buf.readUnsignedByte(), buf.readUnsignedByte(),
            buf.readUnsignedByte());//w w  w. j a  v  a  2s .c  o  m

    sendResponse(channel, remoteAddress, buf.getByte(buf.writerIndex() - 2));

    if (type == MSG_POSITION_DATA || type == MSG_ROLLCALL_RESPONSE || type == MSG_ALARM_DATA
            || type == MSG_BLIND_AREA) {

        Position position = new Position(getProtocolName());

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

        DateBuilder dateBuilder = new DateBuilder().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());

        position.setLatitude(BcdUtil.readCoordinate(buf));
        position.setLongitude(BcdUtil.readCoordinate(buf));
        position.setSpeed(UnitsConverter.knotsFromKph(BcdUtil.readInteger(buf, 4)));
        position.setCourse(BcdUtil.readInteger(buf, 4));

        int flags = buf.readUnsignedByte();
        position.setValid((flags & 0x80) != 0);
        position.set(Position.KEY_SATELLITES, flags & 0x0f);

        position.set(Position.KEY_STATUS, buf.readUnsignedByte());
        position.set("key", buf.readUnsignedByte());
        position.set("oil", buf.readUnsignedShort() / 10.0);
        position.set(Position.KEY_POWER, buf.readUnsignedByte() + buf.readUnsignedByte() * 0.01);
        position.set(Position.KEY_ODOMETER, buf.readUnsignedInt());

        return position;
    }

    return null;
}

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

License:Apache License

public static boolean isBinary(ByteBuf buf) {
    String header = buf.toString(buf.readerIndex(), 4, StandardCharsets.US_ASCII);
    if (header.equals("+ACK")) {
        return buf.getByte(buf.readerIndex() + header.length()) != (byte) ':';
    } else {/*w ww . j av a2  s  .  c o m*/
        return BINARY_HEADERS.contains(header);
    }
}

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

License:Apache License

@Override
protected Object decode(ChannelHandlerContext ctx, Channel channel, ByteBuf buf) throws Exception {

    if (buf.readableBytes() < 5) {
        return null;
    }// w ww . j a v a2 s.  c  om

    int length = 2 + 2; // head and tail

    if (buf.getByte(buf.readerIndex()) == 0x78) {
        length += 1 + buf.getUnsignedByte(buf.readerIndex() + 2);
    } else {
        length += 2 + buf.getUnsignedShort(buf.readerIndex() + 2);
    }

    if (buf.readableBytes() >= length && buf.getUnsignedShort(buf.readerIndex() + length - 2) == 0x0d0a) {
        return buf.readRetainedSlice(length);
    }

    int endIndex = buf.readerIndex() - 1;
    do {
        endIndex = buf.indexOf(endIndex + 1, buf.writerIndex(), (byte) 0x0d);
        if (endIndex > 0 && buf.writerIndex() > endIndex + 1 && buf.getByte(endIndex + 1) == 0x0a) {
            return buf.readRetainedSlice(endIndex + 2 - buf.readerIndex());
        }
    } while (endIndex > 0);

    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);//from w w w  .  ja  va  2  s. c  om
    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()));
    }

    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 decodeExtended(Channel channel, SocketAddress remoteAddress, ByteBuf buf) {

    DeviceSession deviceSession = getDeviceSession(channel, remoteAddress);
    if (deviceSession == null) {
        return null;
    }//  w  ww.j a v a  2s .  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.H02FrameDecoder.java

License:Apache License

@Override
protected Object decode(ChannelHandlerContext ctx, Channel channel, ByteBuf buf) throws Exception {

    char marker = (char) buf.getByte(buf.readerIndex());

    while (marker != '*' && marker != '$' && marker != 'X' && buf.readableBytes() > 0) {
        buf.skipBytes(1);//www  .j  av a 2 s  . co  m
        if (buf.readableBytes() > 0) {
            marker = (char) buf.getByte(buf.readerIndex());
        }
    }

    switch (marker) {
    case '*':

        // Return text message
        int index = buf.indexOf(buf.readerIndex(), buf.writerIndex(), (byte) '#');
        if (index != -1) {
            ByteBuf result = buf.readRetainedSlice(index + 1 - buf.readerIndex());
            while (buf.isReadable()
                    && (buf.getByte(buf.readerIndex()) == '\r' || buf.getByte(buf.readerIndex()) == '\n')) {
                buf.readByte(); // skip new line
            }
            return result;
        }

        break;

    case '$':

        if (messageLength == 0) {
            if (buf.readableBytes() == MESSAGE_LONG) {
                messageLength = MESSAGE_LONG;
            } else {
                messageLength = MESSAGE_SHORT;
            }
        }

        if (buf.readableBytes() >= messageLength) {
            return buf.readRetainedSlice(messageLength);
        }

        break;

    case 'X':

        if (buf.readableBytes() >= MESSAGE_SHORT) {
            return buf.readRetainedSlice(MESSAGE_SHORT);
        }

        break;

    default:

        return null;
    }

    return null;
}

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

License:Apache License

@Override
protected Object decode(ChannelHandlerContext ctx, Channel channel, ByteBuf buf) throws Exception {

    while (buf.isReadable() && buf.getByte(buf.readerIndex()) != '$') {
        buf.skipBytes(1);/*from  www .  jav a  2s  .c om*/
    }

    int delimiterIndex = BufferUtil.indexOf("\r\n", buf);
    if (delimiterIndex > MINIMUM_LENGTH) {
        return readFrame(buf, delimiterIndex, 2);
    } else {
        delimiterIndex = buf.indexOf(buf.readerIndex(), buf.writerIndex(), (byte) '*');
        if (delimiterIndex > MINIMUM_LENGTH) {
            if (buf.writerIndex() > delimiterIndex + 1 && buf.getByte(delimiterIndex + 1) == '*') {
                delimiterIndex += 1;
            }
            if (buf.getByte(delimiterIndex - 2) == ',') {
                return readFrame(buf, delimiterIndex - 1, 2); // skip binary checksum
            } else {
                return readFrame(buf, delimiterIndex, 1);
            }
        }
    }

    return null;
}

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

License:Apache License

@Override
protected Object decode(ChannelHandlerContext ctx, Channel channel, ByteBuf buf) throws Exception {

    if (buf.readableBytes() < 10) {
        return null;
    }//w w w.ja va2  s.c om

    char type = (char) buf.getByte(buf.readerIndex());

    if (type == '$') {
        boolean longFormat = buf.getUnsignedByte(buf.readerIndex() + 1) == 0x75;
        int length = buf.getUnsignedShort(buf.readerIndex() + (longFormat ? 8 : 7)) + 10;
        if (length <= buf.readableBytes()) {
            return buf.readRetainedSlice(length);
        }
    } else if (type == '(') {
        int endIndex = buf.indexOf(buf.readerIndex(), buf.writerIndex(), (byte) ')');
        if (endIndex != -1) {
            return buf.readRetainedSlice(endIndex + 1);
        }
    } else {
        throw new ParseException(null, 0); // unknown message
    }

    return null;
}

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

License:Apache License

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

    ByteBuf buf = (ByteBuf) msg;
    char first = (char) buf.getByte(0);

    if (first == '$') {
        return decodeBinary(buf, channel, remoteAddress);
    } else if (first == '(') {
        String sentence = buf.toString(StandardCharsets.US_ASCII);
        if (sentence.contains("W01")) {
            return decodeW01(sentence, channel, remoteAddress);
        } else {/*from w  w  w.j a  v  a2s.  c o m*/
            return decodeU01(sentence, channel, remoteAddress);
        }
    }

    return null;
}