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

License:Apache License

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

    if (buf.readableBytes() >= 2) {

        if (buf.getUnsignedShort(buf.readerIndex()) == 0xfe02) {

            if (buf.readableBytes() >= KEEPALIVE_LENGTH) {
                return buf.readRetainedSlice(KEEPALIVE_LENGTH);
            }/* ww  w .  j a va  2 s . c  o m*/

        } else if (buf.getUnsignedShort(buf.readerIndex()) == 0x4050
                && buf.getByte(buf.readerIndex() + 2) != ',') {

            if (buf.readableBytes() > 6) {
                int length = buf.getUnsignedShort(buf.readerIndex() + 4) + 4 + 2;
                if (buf.readableBytes() >= length) {
                    return buf.readRetainedSlice(length);
                }
            }

        } else {

            int lengthStart = buf.indexOf(buf.readerIndex() + 3, buf.writerIndex(), (byte) ',') + 1;
            if (lengthStart > 0) {
                int lengthEnd = buf.indexOf(lengthStart, buf.writerIndex(), (byte) ',');
                if (lengthEnd > 0) {
                    int length = lengthEnd + Integer.parseInt(
                            buf.toString(lengthStart, lengthEnd - lengthStart, StandardCharsets.US_ASCII));
                    if (buf.readableBytes() > length && buf.getByte(buf.readerIndex() + length) == '\n') {
                        length += 1;
                    }
                    if (buf.readableBytes() >= length) {
                        return buf.readRetainedSlice(length);
                    }
                }
            } else {
                int endIndex = BufferUtil.indexOf("\r\n", buf);
                if (endIndex > 0) {
                    return buf.readRetainedSlice(endIndex - buf.readerIndex() + 2);
                }
            }

        }

    }

    return null;
}

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

License:Apache License

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

    ByteBuf buf = (ByteBuf) msg;

    if (buf.getUnsignedShort(buf.readerIndex()) == 0xfe02) {
        if (channel != null) {
            channel.writeAndFlush(new NetworkMessage(buf.retain(), remoteAddress)); // keep-alive message
        }// w  w  w  .jav  a  2 s . c o  m
        return null;
    } else if (buf.getByte(buf.readerIndex()) == '$') {
        return decodeInfo(channel, remoteAddress, buf.toString(StandardCharsets.US_ASCII).trim());
    } else if (buf.getByte(buf.readerIndex() + 2) == ',') {
        return decodeText(channel, remoteAddress, buf.toString(StandardCharsets.US_ASCII).trim());
    } else {
        return decodeBinary(channel, remoteAddress, buf);
    }
}

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

License:Apache License

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

    ByteBuf buf = (ByteBuf) msg;

    int type = buf.readUnsignedByte();

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

        if (type == MSG_LOGIN) {
            buf.readUnsignedByte(); // hardware version
            buf.readUnsignedByte(); // software version
        }/*  w w w .ja  v a  2 s.  c om*/

        String imei = ByteBufUtil.hexDump(buf.readSlice(8)).substring(1);
        DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, imei);

        if (deviceSession != null && channel != null) {
            ByteBuf response = Unpooled.buffer();
            response.writeBytes("resp_crc=".getBytes(StandardCharsets.US_ASCII));
            response.writeByte(buf.getByte(buf.writerIndex() - 1));
            channel.writeAndFlush(new NetworkMessage(response, remoteAddress));
        }

        return null;

    }

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

    if (type == MSG_LOCATION) {

        return decodePosition(deviceSession, buf, false);

    } else if (type == MSG_HISTORY) {

        int count = buf.readUnsignedByte() & 0x0f;
        buf.readUnsignedShort(); // total count
        List<Position> positions = new LinkedList<>();

        for (int i = 0; i < count; i++) {
            positions.add(decodePosition(deviceSession, buf, true));
        }

        return positions;

    } else if (type == MSG_45_LOCATION) {

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

        short status = buf.readUnsignedByte();
        if (BitUtil.check(status, 7)) {
            position.set(Position.KEY_ALARM, Position.ALARM_GENERAL);
        }
        position.set(Position.KEY_BATTERY, BitUtil.to(status, 7));

        buf.skipBytes(2); // remaining time

        position.set(Position.PREFIX_TEMP + 1, buf.readByte());

        buf.skipBytes(2); // timer (interval and units)
        buf.readByte(); // mode
        buf.readByte(); // gprs sending interval

        buf.skipBytes(6); // mcc, mnc, lac, cid

        int valid = buf.readUnsignedByte();
        position.setValid(BitUtil.from(valid, 6) != 0);
        position.set(Position.KEY_SATELLITES, BitUtil.from(valid, 6));

        int time = buf.readUnsignedMedium();
        int date = buf.readUnsignedMedium();

        DateBuilder dateBuilder = new DateBuilder().setTime(time / 10000, time / 100 % 100, time % 100)
                .setDateReverse(date / 10000, date / 100 % 100, date % 100);
        position.setTime(dateBuilder.getDate());

        position.setLatitude(convertCoordinate(buf.readUnsignedByte(), buf.readUnsignedMedium()));
        position.setLongitude(convertCoordinate(buf.readUnsignedByte(), buf.readUnsignedMedium()));
        position.setSpeed(buf.readUnsignedByte());
        position.setCourse(buf.readUnsignedShort());

        return position;

    }

    return null;
}

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

License:Apache License

private static byte checksum(ByteBuf buf, int end) {
    byte result = 0;
    for (int i = 0; i < end; i++) {
        result += buf.getByte(buf.readerIndex() + i);
    }/*from   www  .  jav a2  s.c o m*/
    return result;
}

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

License:Apache License

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

    if (header && buf.readableBytes() >= HANDSHAKE_LENGTH) {
        buf.skipBytes(HANDSHAKE_LENGTH);
        header = false;//  www . ja  v  a2 s.c om
    }

    int end = 8; // IMEI

    while (buf.readableBytes() >= end + 2 + 1 + 1 + 1) {
        end += buf.getUnsignedShortLE(buf.readerIndex() + end) + 2;

        if (buf.readableBytes() > end && checksum(buf, end) == buf.getByte(buf.readerIndex() + end)) {
            return buf.readRetainedSlice(end + 1);
        }
    }

    return null;
}

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

License:Apache License

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

    ByteBuf buf = (ByteBuf) msg;

    if (BitUtil.check(buf.getByte(buf.readerIndex()), 7)) {

        int content = buf.readUnsignedByte();

        if (BitUtil.check(content, 0)) {
            String id = ByteBufUtil.hexDump(buf.readSlice(buf.readUnsignedByte()));
            getDeviceSession(channel, remoteAddress, id);
        }//from ww w  .j  a v a  2s . c om

        if (BitUtil.check(content, 1)) {
            buf.skipBytes(buf.readUnsignedByte()); // identifier type
        }

        if (BitUtil.check(content, 2)) {
            buf.skipBytes(buf.readUnsignedByte()); // authentication
        }

        if (BitUtil.check(content, 3)) {
            buf.skipBytes(buf.readUnsignedByte()); // routing
        }

        if (BitUtil.check(content, 4)) {
            buf.skipBytes(buf.readUnsignedByte()); // forwarding
        }

        if (BitUtil.check(content, 5)) {
            buf.skipBytes(buf.readUnsignedByte()); // response redirection
        }

    }

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

    int service = buf.readUnsignedByte();
    int type = buf.readUnsignedByte();
    int index = buf.readUnsignedShort();

    if (service == SERVICE_ACKNOWLEDGED) {
        sendResponse(channel, remoteAddress, type, index, 0);
    }

    if (type == MSG_EVENT_REPORT || type == MSG_LOCATE_REPORT || type == MSG_MINI_EVENT_REPORT) {
        return decodePosition(deviceSession, type, buf);
    }

    return null;
}

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

License:Apache License

private void sendReply(Channel channel, SocketAddress remoteAddress, long deviceId, byte packetNumber) {
    if (channel != null) {
        ByteBuf reply = Unpooled.buffer(28);
        reply.writeByte('M');
        reply.writeByte('C');
        reply.writeByte('G');
        reply.writeByte('P');
        reply.writeByte(MSG_SERVER_ACKNOWLEDGE);
        reply.writeIntLE((int) deviceId);
        reply.writeByte(commandCount++);
        reply.writeIntLE(0); // authentication code
        reply.writeByte(0);/*from www  .j a v a  2 s  .co  m*/
        reply.writeByte(packetNumber);
        reply.writeZero(11);

        byte checksum = 0;
        for (int i = 4; i < 27; i++) {
            checksum += reply.getByte(i);
        }
        reply.writeByte(checksum);

        channel.writeAndFlush(new NetworkMessage(reply, remoteAddress));
    }
}

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

License:Apache License

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

    ByteBuf buf = (ByteBuf) msg;

    boolean alternative = buf.getByte(buf.readerIndex() + 3) != 'P';

    buf.skipBytes(4); // system code
    int type = buf.readUnsignedByte();
    long deviceUniqueId = buf.readUnsignedIntLE();

    if (type != MSG_CLIENT_SERIAL) {
        buf.readUnsignedShortLE(); // communication control
    }/*  w  ww . j a  va 2 s  .  c  o m*/
    byte packetNumber = buf.readByte();

    sendReply(channel, remoteAddress, deviceUniqueId, packetNumber);

    if (type == MSG_CLIENT_STATUS) {

        Position position = new Position(getProtocolName());

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

        position.set(Position.KEY_VERSION_HW, buf.readUnsignedByte());
        position.set(Position.KEY_VERSION_FW, buf.readUnsignedByte());
        buf.readUnsignedByte(); // protocol version

        position.set(Position.KEY_STATUS, buf.readUnsignedByte() & 0x0f);

        buf.readUnsignedByte(); // operator / configuration flags
        buf.readUnsignedByte(); // reason data
        position.set(Position.KEY_ALARM, decodeAlarm(buf.readUnsignedByte()));

        position.set("mode", buf.readUnsignedByte());
        position.set(Position.KEY_INPUT, buf.readUnsignedIntLE());

        if (alternative) {
            buf.readUnsignedByte(); // input
            position.set(Position.PREFIX_ADC + 1, buf.readUnsignedShortLE());
            position.set(Position.PREFIX_ADC + 2, buf.readUnsignedShortLE());
        } else {
            buf.readUnsignedByte(); // operator
            position.set(Position.PREFIX_ADC + 1, buf.readUnsignedIntLE());
        }

        position.set(Position.KEY_ODOMETER, buf.readUnsignedMediumLE());

        buf.skipBytes(6); // multi-purpose data
        buf.readUnsignedShortLE(); // fix time
        buf.readUnsignedByte(); // location status
        buf.readUnsignedByte(); // mode 1
        buf.readUnsignedByte(); // mode 2

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

        position.setValid(true);

        if (alternative) {
            position.setLongitude(buf.readIntLE() / 10000000.0);
            position.setLatitude(buf.readIntLE() / 10000000.0);
        } else {
            position.setLongitude(buf.readIntLE() / Math.PI * 180 / 100000000);
            position.setLatitude(buf.readIntLE() / Math.PI * 180 / 100000000);
        }

        position.setAltitude(buf.readIntLE() * 0.01);

        if (alternative) {
            position.setSpeed(UnitsConverter.knotsFromKph(buf.readUnsignedIntLE()));
            position.setCourse(buf.readUnsignedShortLE() / 1000.0);
        } else {
            position.setSpeed(UnitsConverter.knotsFromMps(buf.readUnsignedIntLE() * 0.01));
            position.setCourse(buf.readUnsignedShortLE() / Math.PI * 180.0 / 1000.0);
        }

        DateBuilder dateBuilder = new DateBuilder()
                .setTimeReverse(buf.readUnsignedByte(), buf.readUnsignedByte(), buf.readUnsignedByte())
                .setDateReverse(buf.readUnsignedByte(), buf.readUnsignedByte(), buf.readUnsignedShortLE());
        position.setTime(dateBuilder.getDate());

        return position;
    }

    return null;
}

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

License:Apache License

private ByteBuf encodeContent(long deviceId, int command, int data1, int data2) {

    ByteBuf buf = Unpooled.buffer(0);
    buf.writeByte('M');
    buf.writeByte('C');
    buf.writeByte('G');
    buf.writeByte('P');
    buf.writeByte(0);//from  w ww .  j  a v a 2  s.  c  o m
    buf.writeIntLE(Integer.parseInt(getUniqueId(deviceId)));
    buf.writeByte(0); // command numerator
    buf.writeIntLE(0); // authentication code
    buf.writeByte(command);
    buf.writeByte(command);
    buf.writeByte(data1);
    buf.writeByte(data1);
    buf.writeByte(data2);
    buf.writeByte(data2);
    buf.writeIntLE(0); // command specific data

    byte checksum = 0;
    for (int i = 4; i < buf.writerIndex(); i++) {
        checksum += buf.getByte(i);
    }
    buf.writeByte(checksum);

    return buf;
}

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

License:Apache License

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

    ByteBuf buf = (ByteBuf) msg;

    String uniqueId = null;//w  w  w .  j  a  v  a  2  s .co  m
    DeviceSession deviceSession;

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

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

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

    if (type == MSG_LOGIN) {

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

    } else {

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

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

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

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

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

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

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

            getLastLocation(position, null);

            decodeStatus(position, buf.readUnsignedShort());

            return position;

        } else if (type == MSG_OBD) {

            return decodeObd(deviceSession, buf, index);

        } else if (type == MSG_DOWNLINK) {

            return decodeResult(deviceSession, buf, index);

        }

    }

    return null;
}