Example usage for io.netty.buffer ByteBuf getUnsignedByte

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

Introduction

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

Prototype

public abstract short getUnsignedByte(int index);

Source Link

Document

Gets an unsigned byte at the specified absolute index in this buffer.

Usage

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

License:Apache License

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

    if (buf.readableBytes() < 20) {
        return null;
    }//from   ww  w  .ja v a 2s.  c o m

    int length;
    if (buf.getUnsignedByte(buf.readerIndex()) == 0x80) {
        length = buf.getUnsignedShortLE(buf.readerIndex() + 6);
    } else {
        length = buf.getUnsignedShort(buf.readerIndex() + 6);
    }

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

    return null;
}

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

License:Apache License

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

    if (buf.readableBytes() < 2) {
        return null;
    }// w w  w  .j  a v  a  2  s. co  m

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

        int index = buf.indexOf(buf.readerIndex() + 1, buf.writerIndex(), (byte) 0xF8);
        if (index != -1) {
            ByteBuf result = Unpooled.buffer(index + 1 - buf.readerIndex());

            while (buf.readerIndex() <= index) {
                int b = buf.readUnsignedByte();
                if (b == 0xF7) {
                    int ext = buf.readUnsignedByte();
                    if (ext == 0x00) {
                        result.writeByte(0xF7);
                    } else if (ext == 0x0F) {
                        result.writeByte(0xF8);
                    }
                } else {
                    result.writeByte(b);
                }
            }

            return result;
        }

    } else {

        int index = buf.indexOf(buf.readerIndex(), buf.writerIndex(), (byte) '#');
        if (index != -1) {
            return buf.readRetainedSlice(index + 1 - buf.readerIndex());
        }

    }

    return null;
}

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

License:Apache License

private void decodeObd(Position position, ByteBuf buf, int length) {

    int end = buf.readerIndex() + length;

    while (buf.readerIndex() < end) {
        int parameterLength = buf.getUnsignedByte(buf.readerIndex()) >> 4;
        int mode = buf.readUnsignedByte() & 0x0F;
        position.add(ObdDecoder.decode(mode, ByteBufUtil.hexDump(buf.readSlice(parameterLength - 1))));
    }// w  w  w  .j a va 2 s.  c  om
}

From source file:org.traccar.protocol.UlbotechProtocolDecoder.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()) == 0xF8) {

        if (channel != null) {
            ByteBuf response = Unpooled.buffer();
            response.writeByte(0xF8);/*  w  w w  .j a v a2s.  c om*/
            response.writeByte(DATA_GPS);
            response.writeByte(0xFE);
            response.writeShort(buf.getShort(response.writerIndex() - 1 - 2));
            response.writeShort(Checksum.crc16(Checksum.CRC16_XMODEM, response.nioBuffer(1, 4)));
            response.writeByte(0xF8);
            channel.writeAndFlush(new NetworkMessage(response, remoteAddress));
        }

        return decodeBinary(channel, remoteAddress, buf);
    } else {

        if (channel != null) {
            channel.writeAndFlush(
                    new NetworkMessage(Unpooled.copiedBuffer(
                            String.format("*TS01,ACK:%04X#",
                                    Checksum.crc16(Checksum.CRC16_XMODEM,
                                            buf.nioBuffer(1, buf.writerIndex() - 2))),
                            StandardCharsets.US_ASCII), remoteAddress));
        }

        return decodeText(channel, remoteAddress, buf.toString(StandardCharsets.US_ASCII));
    }
}

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

License:Apache License

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

    if (buf.readableBytes() < KEEP_ALIVE_LENGTH) {
        return null;
    }//from   ww  w .  ja  va2  s.  c  o m

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

        // Send response
        ByteBuf frame = buf.readRetainedSlice(KEEP_ALIVE_LENGTH);
        if (channel != null) {
            frame.retain();
            channel.writeAndFlush(new NetworkMessage(frame, channel.remoteAddress()));
        }
        return frame;

    } else {

        int index = BufferUtil.indexOf("\r\n", buf);
        if (index != -1) {
            ByteBuf frame = buf.readRetainedSlice(index - buf.readerIndex());
            buf.skipBytes(2);
            return frame;
        }

    }

    return null;
}

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

License:Apache License

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

    ByteBuf buf = (ByteBuf) msg;

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

        long deviceId = ((Long.reverseBytes(buf.getLong(0))) >> 32) & 0xFFFFFFFFL;
        getDeviceSession(channel, remoteAddress, String.valueOf(deviceId));

        return null;
    } else if (buf.toString(StandardCharsets.US_ASCII).startsWith("$OK:")
            || buf.toString(StandardCharsets.US_ASCII).startsWith("$ERR:")
            || buf.toString(StandardCharsets.US_ASCII).startsWith("$MSG:")) {

        DeviceSession deviceSession = getDeviceSession(channel, remoteAddress);

        Position position = new Position(getProtocolName());
        position.setDeviceId(deviceSession.getDeviceId());
        getLastLocation(position, new Date());
        position.set(Position.KEY_RESULT, buf.toString(StandardCharsets.US_ASCII));

        return position;

    } else {/*from  w ww  .  j a v a  2s .  c  o  m*/

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

        Position position = new Position(getProtocolName());

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

        position.setTime(parser.nextDateTime());

        position.setLongitude(parser.nextDouble(0));
        position.setLatitude(parser.nextDouble(0));
        position.setSpeed(UnitsConverter.knotsFromKph(parser.nextDouble(0)));
        position.setCourse(parser.nextDouble(0));
        position.setAltitude(parser.nextDouble(0));

        int satellites = parser.nextInt(0);
        position.setValid(satellites != 0);
        position.set(Position.KEY_SATELLITES, satellites);

        position.set(Position.KEY_EVENT, parser.next());
        position.set(Position.KEY_BATTERY, parser.nextDouble());
        if (parser.hasNext()) {
            position.set(Position.KEY_ODOMETER, parser.nextDouble(0) * 1000);
        }
        position.set(Position.KEY_INPUT, parser.next());
        position.set(Position.PREFIX_ADC + 1, parser.next());
        position.set(Position.PREFIX_ADC + 2, parser.next());
        position.set(Position.KEY_OUTPUT, parser.next());

        return position;

    }

}

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

License:Apache License

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

    ByteBuf buf = (ByteBuf) msg;

    byte[] format = null;
    if (formats.size() > 1) {
        format = formats.get(buf.getUnsignedByte(buf.readerIndex()));
    } else if (!formats.isEmpty()) {
        format = formats.values().iterator().next();
    }//w w w  .  j  av  a 2  s . c om

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

    Position position = new Position(getProtocolName());

    for (byte tag : format) {
        switch (tag) {
        case 0x03:
            DeviceSession deviceSession = getDeviceSession(channel, remoteAddress,
                    String.valueOf(buf.readUnsignedInt()));
            if (deviceSession == null) {
                return null;
            }
            position.setDeviceId(deviceSession.getDeviceId());
            break;
        case 0x04:
            position.set(Position.KEY_EVENT, buf.readUnsignedByte());
            break;
        case 0x05:
            position.set(Position.KEY_INDEX, buf.readUnsignedShort());
            break;
        case 0x06:
            position.setTime(new Date(buf.readUnsignedInt() * 1000));
            break;
        case 0x07:
            position.setLatitude(buf.readInt() * 0.000001);
            break;
        case 0x08:
            position.setLongitude(buf.readInt() * 0.000001);
            break;
        case 0x09:
            position.setAltitude(buf.readShort() * 0.1);
            break;
        case 0x0a:
            position.setCourse(buf.readShort() * 0.1);
            break;
        case 0x0b:
            position.setSpeed(UnitsConverter.knotsFromKph(buf.readUnsignedByte()));
            break;
        case 0x10:
            position.set(Position.KEY_ODOMETER_TRIP, buf.readUnsignedInt());
            break;
        case 0x12:
            position.set(Position.KEY_HDOP, buf.readUnsignedByte() * 0.1);
            break;
        case 0x13:
            position.set(Position.KEY_SATELLITES, buf.readUnsignedByte());
            break;
        case 0x14:
            position.set(Position.KEY_RSSI, buf.readShort());
            break;
        case 0x16:
            position.set(Position.KEY_BATTERY, buf.readUnsignedByte() * 0.1);
            break;
        case 0x17:
            position.set(Position.KEY_POWER, buf.readUnsignedByte() * 0.1);
            break;
        case 0x57:
            position.set(Position.KEY_OBD_SPEED, UnitsConverter.knotsFromKph(buf.readUnsignedShort()));
            break;
        case 0x65:
            position.set(Position.KEY_VIN, buf.readSlice(17).toString(StandardCharsets.US_ASCII));
            break;
        case 0x73:
            position.set(Position.KEY_VERSION_FW, buf.readSlice(16).toString(StandardCharsets.US_ASCII).trim());
            break;
        default:
            buf.skipBytes(getTagLength(tag));
            break;
        }
    }

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

    return position;
}

From source file:ru.jts_dev.gameserver.util.Encoder.java

License:Open Source License

@Transformer
public ByteBuf decrypt(ByteBuf data, @Header(CONNECTION_ID) String connectionId) {
    GameSession gameSession = sessionService.getSessionBy(connectionId);

    assert gameSession != null : "GameSession for " + connectionId + " does not exist";

    ByteBuf key = gameSession.getDecryptKey();

    int temp = 0;
    for (int i = 0; i < data.readableBytes(); i++) {
        final int temp2 = data.getUnsignedByte(i);
        data.setByte(i, (byte) (temp2 ^ key.getByte(i & 15) ^ temp));
        temp = temp2;//from   w  w  w  .  ja va  2s . co  m
    }

    int old = key.getInt(8);
    old += data.readableBytes();

    key.setInt(8, old);

    return data;
}

From source file:ru.jts_dev.gameserver.util.Encoder.java

License:Open Source License

@Transformer
public ByteBuf encrypt(ByteBuf data, @Header(CONNECTION_ID) String connectionId) {
    GameSession gameSession = sessionService.getSessionBy(connectionId);

    assert gameSession != null : "GameSession for " + connectionId + " does not exist";

    ByteBuf key = gameSession.getEncryptKey();

    int temp = 0;
    for (int i = 0; i < data.readableBytes(); i++) {
        int temp2 = data.getUnsignedByte(data.readerIndex() + i);
        temp = temp2 ^ key.getByte(i & 15) ^ temp;
        data.setByte(data.readerIndex() + i, (byte) temp);
    }/*from w  ww .  ja  v a2 s.c o  m*/

    int old = key.getInt(8);
    old += data.readableBytes();

    key.setInt(8, old);

    return data;
}

From source file:sas.systems.imflux.packet.DataPacket.java

License:Apache License

/**
 * Decodes a {@code DataPacket}.//w  w w.j ava  2s. c o  m
 * 
 * @param buffer as a ByteBuf
 * @return the DataPacket object
 * @throws IndexOutOfBoundsException
 */
public static DataPacket decode(ByteBuf buffer) throws IndexOutOfBoundsException {
    if (buffer.readableBytes() < 12) {
        throw new IllegalArgumentException("A RTP packet must be at least 12 octets long");
    }

    // Version, Padding, eXtension, CSRC Count
    DataPacket packet = new DataPacket();
    byte b = buffer.readByte();
    packet.version = RtpVersion.fromByte(b);
    boolean padding = (b & 0x20) > 0; // mask 0010 0000
    boolean extension = (b & 0x10) > 0; // mask 0001 0000
    int contributingSourcesCount = b & 0x0f; // mask 0000 1111

    // Marker, Payload Type
    b = buffer.readByte();
    packet.marker = (b & 0x80) > 0; // mask 1000 0000
    packet.payloadType = (b & 0x7f); // mask 0111 1111

    packet.sequenceNumber = buffer.readUnsignedShort();
    packet.timestamp = buffer.readUnsignedInt();
    packet.ssrc = buffer.readUnsignedInt();

    // Read extension headers & data
    if (extension) {
        packet.extensionHeaderData = buffer.readShort();
        packet.extensionData = new byte[buffer.readUnsignedShort() * 4];
        buffer.readBytes(packet.extensionData);
    }

    // Read CCRC's
    if (contributingSourcesCount > 0) {
        packet.contributingSourceIds = new ArrayList<Long>(contributingSourcesCount);
        for (int i = 0; i < contributingSourcesCount; i++) {
            long contributingSource = buffer.readUnsignedInt();
            packet.contributingSourceIds.add(contributingSource);
        }
    }

    if (!padding) {
        // No padding used, assume remaining data is the packet
        byte[] remainingBytes = new byte[buffer.readableBytes()];
        buffer.readBytes(remainingBytes);
        packet.setData(remainingBytes);
    } else {
        // Padding bit was set, so last byte contains the number of padding octets that should be discarded.
        short lastByte = buffer.getUnsignedByte(buffer.readerIndex() + buffer.readableBytes() - 1);
        byte[] dataBytes = new byte[buffer.readableBytes() - lastByte];
        buffer.readBytes(dataBytes);
        packet.setData(dataBytes);
        // Discard rest of buffer.
        buffer.skipBytes(buffer.readableBytes());
    }

    return packet;
}