Java Code Examples for org.jboss.netty.buffer.ChannelBuffer.getUnsignedByte()

The following are Jave code examples for showing how to use getUnsignedByte() of the org.jboss.netty.buffer.ChannelBuffer class. You can vote up the examples you like. Your votes will be used in our system to get more good examples.
Example 1
Project: traccar-service   File: MeiligaoFrameDecoder.java   Source Code and License Vote up 6 votes
@Override
protected Object decode(
        ChannelHandlerContext ctx, Channel channel, ChannelBuffer buf) throws Exception {

    // Strip not '$' (0x24) bytes from the beginning
    while (buf.readable() && buf.getUnsignedByte(buf.readerIndex()) != 0x24) {
        buf.readByte();
    }

    // Check length and return buffer
    if (buf.readableBytes() >= MESSAGE_HEADER) {
        int length = buf.getUnsignedShort(buf.readerIndex() + 2);
        if (buf.readableBytes() >= length) {
            return buf.readBytes(length);
        }
    }

    return null;
}
 
Example 2
Project: traccar-service   File: NvsFrameDecoder.java   Source Code and License Vote up 6 votes
@Override
protected Object decode(
        ChannelHandlerContext ctx,
        Channel channel,
        ChannelBuffer buf) throws Exception {

    if (buf.readableBytes() < 4 + 2) {
        return null;
    }

    int length;
    if (buf.getUnsignedByte(buf.readerIndex()) == 0) {
        length = 2 + buf.getUnsignedShort(buf.readerIndex());
    } else {
        length = 4 + 2 + buf.getUnsignedShort(buf.readerIndex() + 4) + 2;
    }

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

    return null;
}
 
Example 3
Project: traccar-service   File: H02ProtocolDecoder.java   Source Code and License Vote up 6 votes
private static double readCoordinate(ChannelBuffer buf, boolean lon) {

        int degrees = BcdUtil.readInteger(buf, 2);
        if (lon) {
            degrees = degrees * 10 + (buf.getUnsignedByte(buf.readerIndex()) >> 4);
        }

        double result = 0;
        if (lon) {
            result = buf.readUnsignedByte() & 0x0f;
        }

        int length = 6;
        if (lon) {
            length = 5;
        }

        result = result * 10 + BcdUtil.readInteger(buf, length) * 0.0001;

        result /= 60;
        result += degrees;

        return result;
    }
 
Example 4
Project: traccar-service   File: Pt502FrameDecoder.java   Source Code and License Vote up 6 votes
@Override
protected Object decode(
        ChannelHandlerContext ctx, Channel channel, ChannelBuffer buf) throws Exception {

    if (buf.readableBytes() < BINARY_HEADER) {
        return null;
    }

    if (buf.getUnsignedByte(buf.readerIndex()) == 0xbf) {
        buf.skipBytes(BINARY_HEADER);
    }

    int index = buf.indexOf(buf.readerIndex(), buf.writerIndex(), (byte) '\r');
    if (index != -1 && index + 1 < buf.writerIndex()) {
        ChannelBuffer result = buf.readBytes(index - buf.readerIndex());
        buf.skipBytes(2);
        return result;
    }

    return null;
}
 
Example 5
Project: traccar-service   File: Gps056FrameDecoder.java   Source Code and License Vote up 6 votes
@Override
protected Object decode(
        ChannelHandlerContext ctx, Channel channel, ChannelBuffer buf) throws Exception {

    if (buf.readableBytes() >= MESSAGE_HEADER) {
        int length = Integer.parseInt(buf.toString(2, 2, StandardCharsets.US_ASCII)) + 5;
        if (buf.readableBytes() >= length) {
            ChannelBuffer frame = buf.readBytes(length);
            while (buf.readable() && buf.getUnsignedByte(buf.readerIndex()) != '$') {
                buf.readByte();
            }
            return frame;
        }
    }

    return null;
}
 
Example 6
Project: traccar-service   File: TramigoFrameDecoder.java   Source Code and License Vote up 6 votes
@Override
protected Object decode(
        ChannelHandlerContext ctx,
        Channel channel,
        ChannelBuffer buf) throws Exception {

    if (buf.readableBytes() < 20) {
        return null;
    }

    // Swap byte order for legacy protocol
    if (buf.getUnsignedByte(buf.readerIndex()) == 0x80) {
        int length = buf.readableBytes();
        byte[] bytes = new byte[length];
        buf.getBytes(buf.readerIndex(), bytes);

        ChannelBuffer result = (ChannelBuffer) super.decode(
                ctx, channel, ChannelBuffers.wrappedBuffer(ByteOrder.LITTLE_ENDIAN, bytes));
        if (result != null) {
            buf.skipBytes(result.readableBytes());
        }
        return result;
    }

    return super.decode(ctx, channel, buf);
}
 
Example 7
Project: traccar-service   File: Mta6ProtocolDecoder.java   Source Code and License Vote up 6 votes
public float readFloat(ChannelBuffer buf) {
    switch (buf.getUnsignedByte(buf.readerIndex()) >> 6) {
        case 0:
            previousFloat = buf.readInt() << 2;
            break;
        case 1:
            previousFloat = (previousFloat & 0xffffff00) + ((buf.readUnsignedByte() & 0x3f) << 2);
            break;
        case 2:
            previousFloat = (previousFloat & 0xffff0000) + ((buf.readUnsignedShort() & 0x3fff) << 2);
            break;
        case 3:
            previousFloat = (previousFloat & 0xff000000) + ((buf.readUnsignedMedium() & 0x3fffff) << 2);
            break;
        default:
            Log.warning(new IllegalArgumentException());
            break;
    }
    return Float.intBitsToFloat(previousFloat);
}
 
Example 8
Project: traccar-service   File: AplicomFrameDecoder.java   Source Code and License Vote up 5 votes
@Override
protected Object decode(
        ChannelHandlerContext ctx, Channel channel, ChannelBuffer buf) throws Exception {

    // Skip Alive message
    while (buf.readable() && Character.isDigit(buf.getByte(buf.readerIndex()))) {
        buf.readByte();
    }

    // Check minimum length
    if (buf.readableBytes() < 11) {
        return null;
    }

    // Read flags
    int version = buf.getUnsignedByte(buf.readerIndex() + 1);
    int offset = 1 + 1 + 3;
    if ((version & 0x80) != 0) {
        offset += 4;
    }

    // Get data length
    int length = buf.getUnsignedShort(buf.readerIndex() + offset);
    offset += 2;
    if ((version & 0x40) != 0) {
        offset += 3;
    }
    length += offset; // add header

    // Return buffer
    if (buf.readableBytes() >= length) {
        return buf.readBytes(length);
    }

    return null;
}
 
Example 9
Project: traccar-service   File: Jt600FrameDecoder.java   Source Code and License Vote up 5 votes
@Override
protected Object decode(
        ChannelHandlerContext ctx, Channel channel, ChannelBuffer buf) throws Exception {

    if (buf.readableBytes() < 10) {
        return null;
    }

    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.readBytes(length);
        }
    } else if (type == '(') {
        int endIndex = buf.indexOf(buf.readerIndex(), buf.writerIndex(), (byte) ')');
        if (endIndex != -1) {
            return buf.readBytes(endIndex + 1);
        }
    } else {
        throw new ParseException(null, 0); // unknown message
    }

    return null;
}
 
Example 10
Project: traccar-service   File: AutoFonFrameDecoder.java   Source Code and License Vote up 5 votes
@Override
protected Object decode(
        ChannelHandlerContext ctx,
        Channel channel,
        ChannelBuffer buf) throws Exception {

    // Check minimum length
    if (buf.readableBytes() < 12) {
        return null;
    }

    int length;
    switch (buf.getUnsignedByte(buf.readerIndex())) {
        case AutoFonProtocolDecoder.MSG_LOGIN:
            length = 12;
            break;
        case AutoFonProtocolDecoder.MSG_LOCATION:
            length = 78;
            break;
        case AutoFonProtocolDecoder.MSG_HISTORY:
            length = 257;
            break;
        case AutoFonProtocolDecoder.MSG_45_LOGIN:
            length = 19;
            break;
        case AutoFonProtocolDecoder.MSG_45_LOCATION:
            length = 34;
            break;
        default:
            length = 0;
            break;
    }

    // Check length and return buffer
    if (length != 0 && buf.readableBytes() >= length) {
        return buf.readBytes(length);
    }

    return null;
}
 
Example 11
Project: traccar-service   File: Gt06FrameDecoder.java   Source Code and License Vote up 5 votes
@Override
protected Object decode(
        ChannelHandlerContext ctx, Channel channel, ChannelBuffer buf) throws Exception {

    if (buf.readableBytes() < 5) {
        return null;
    }

    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.readBytes(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.readBytes(endIndex + 2 - buf.readerIndex());
        }
    } while (endIndex > 0);

    return null;
}
 
Example 12
Project: traccar-service   File: UlbotechProtocolDecoder.java   Source Code and License Vote up 5 votes
private void decodeObd(Position position, ChannelBuffer 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, ChannelBuffers.hexDump(buf.readBytes(parameterLength - 1))));
        }
    }
 
Example 13
Project: traccar-service   File: UlbotechProtocolDecoder.java   Source Code and License Vote up 5 votes
@Override
protected Object decode(
        Channel channel, SocketAddress remoteAddress, Object msg) throws Exception {

    ChannelBuffer buf = (ChannelBuffer) msg;

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

        if (channel != null) {
            ChannelBuffer response = ChannelBuffers.dynamicBuffer();
            response.writeByte(0xF8);
            response.writeByte(DATA_GPS);
            response.writeByte(0xFE);
            response.writeShort(buf.getShort(response.writerIndex() - 1 - 2));
            response.writeShort(Checksum.crc16(Checksum.CRC16_XMODEM, response.toByteBuffer(1, 4)));
            response.writeByte(0xF8);
            channel.write(response);
        }

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

        if (channel != null) {
            channel.write(ChannelBuffers.copiedBuffer(String.format("*TS01,ACK:%04X#",
                    Checksum.crc16(Checksum.CRC16_XMODEM, buf.toByteBuffer(1, buf.writerIndex() - 2))),
                    StandardCharsets.US_ASCII));
        }

        return decodeText(channel, remoteAddress, buf.toString(StandardCharsets.US_ASCII));
    }
}
 
Example 14
Project: traccar-service   File: OigoProtocolDecoder.java   Source Code and License Vote up 5 votes
@Override
protected Object decode(
        Channel channel, SocketAddress remoteAddress, Object msg) throws Exception {

    ChannelBuffer buf = (ChannelBuffer) msg;

    if (buf.getUnsignedByte(buf.readerIndex()) == 0x7e) {
        return decodeArMessage(channel, remoteAddress, buf);
    } else {
        return decodeMgMessage(channel, remoteAddress, buf);
    }
}
 
Example 15
Project: traccar-service   File: NavisProtocolDecoder.java   Source Code and License Vote up 5 votes
private static short checksum(ChannelBuffer buf) {
    short sum = 0;
    for (int i = 0; i < buf.readableBytes(); i++) {
        sum ^= buf.getUnsignedByte(i);
    }
    return sum;
}
 
Example 16
Project: traccar-service   File: OrionFrameDecoder.java   Source Code and License Vote up 4 votes
@Override
protected Object decode(
        ChannelHandlerContext ctx,
        Channel channel,
        ChannelBuffer buf) throws Exception {

    int length = 6;

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

        int type = buf.getUnsignedByte(buf.readerIndex() + 2) & 0x0f;

        if (type == OrionProtocolDecoder.MSG_USERLOG && buf.readableBytes() >= length + 5) {

            int index = buf.readerIndex() + 3;
            int count = buf.getUnsignedByte(index) & 0x0f;
            index += 5;
            length += 5;

            for (int i = 0; i < count; i++) {
                if (buf.readableBytes() < length) {
                    return null;
                }
                int logLength = buf.getUnsignedByte(index + 1);
                index += logLength;
                length += logLength;
            }

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

        } else if (type == OrionProtocolDecoder.MSG_SYSLOG && buf.readableBytes() >= length + 12) {

            length += buf.getUnsignedShort(buf.readerIndex() + 8);
            if (buf.readableBytes() >= length) {
                return buf.readBytes(length);
            }

        }
    }

    return null;
}
 
Example 17
Project: traccar-service   File: OigoProtocolDecoder.java   Source Code and License Vote up 4 votes
private Position decodeMgMessage(Channel channel, SocketAddress remoteAddress, ChannelBuffer buf) {

        buf.readUnsignedByte(); // tag
        int flags = buf.getUnsignedByte(buf.readerIndex());

        DeviceSession deviceSession;
        if (BitUtil.check(flags, 6)) {
            buf.readUnsignedByte(); // flags
            deviceSession = getDeviceSession(channel, remoteAddress);
        } else {
            String imei = ChannelBuffers.hexDump(buf.readBytes(8)).substring(1);
            deviceSession = getDeviceSession(channel, remoteAddress, imei);
        }

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

        Position position = new Position();
        position.setProtocol(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)) {
            ChannelBuffer response = ChannelBuffers.dynamicBuffer();
            response.writeByte(MSG_ACKNOWLEDGEMENT);
            response.writeByte(index);
            response.writeByte(0x00);
            channel.write(response, remoteAddress);
        }

        return position;
    }
 
Example 18
Project: traccar-service   File: CellocatorFrameDecoder.java   Source Code and License Vote up 4 votes
@Override
protected Object decode(
        ChannelHandlerContext ctx,
        Channel channel,
        ChannelBuffer buf) throws Exception {

    // Check minimum length
    int available = buf.readableBytes();
    if (available < MESSAGE_MINIMUM_LENGTH) {
        return null;
    }

    // Size depending on message type
    int length = 0;
    int type = buf.getUnsignedByte(4);
    switch (type) {
        case CellocatorProtocolDecoder.MSG_CLIENT_STATUS:
            length = 70;
            break;
        case CellocatorProtocolDecoder.MSG_CLIENT_PROGRAMMING:
            length = 31;
            break;
        case CellocatorProtocolDecoder.MSG_CLIENT_SERIAL_LOG:
            length = 70;
            break;
        case CellocatorProtocolDecoder.MSG_CLIENT_SERIAL:
            if (available >= 19) {
                length = 19 + buf.getUnsignedShort(16);
            }
            break;
        case CellocatorProtocolDecoder.MSG_CLIENT_MODULAR:
            length = 15 + buf.getUnsignedByte(13);
            break;
        default:
            Log.warning(new UnsupportedOperationException(String.valueOf(type)));
            break;
    }

    // Read packet
    if (length > 0 && available >= length) {
        return buf.readBytes(length);
    }

    return null;
}
 
Example 19
Project: traccar-service   File: Mta6ProtocolDecoder.java   Source Code and License Vote up 4 votes
private Position parseFormatA1(DeviceSession deviceSession, ChannelBuffer buf) {
    Position position = new Position();
    position.setDeviceId(deviceSession.getDeviceId());
    position.setProtocol(getProtocolName());

    short flags = buf.readUnsignedByte();

    // Skip events
    short event = buf.readUnsignedByte();
    if (BitUtil.check(event, 7)) {
        if (BitUtil.check(event, 6)) {
            buf.skipBytes(8);
        } else {
            while (BitUtil.check(event, 7)) {
                event = buf.readUnsignedByte();
            }
        }
    }

    position.setLatitude(new FloatReader().readFloat(buf) / Math.PI * 180);
    position.setLongitude(new FloatReader().readFloat(buf) / Math.PI * 180);
    position.setTime(new TimeReader().readTime(buf));

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

    if (BitUtil.check(flags, 0)) {
        position.setAltitude(buf.readUnsignedShort());
        position.setSpeed(buf.readUnsignedByte());
        position.setCourse(buf.readByte());
        position.set(Position.KEY_ODOMETER, new FloatReader().readFloat(buf));
    }

    if (BitUtil.check(flags, 1)) {
        position.set(Position.KEY_FUEL_CONSUMPTION, new FloatReader().readFloat(buf));
        position.set(Position.KEY_HOURS, new FloatReader().readFloat(buf));
        position.set("tank", buf.readUnsignedByte() * 0.4);
    }

    if (BitUtil.check(flags, 2)) {
        position.set("engine", buf.readUnsignedShort() * 0.125);
        position.set("pedals", buf.readUnsignedByte());
        position.set(Position.PREFIX_TEMP + 1, buf.readUnsignedByte() - 40);
        position.set(Position.KEY_ODOMETER_SERVICE, buf.readUnsignedShort());
    }

    if (BitUtil.check(flags, 3)) {
        position.set(Position.KEY_FUEL_LEVEL, buf.readUnsignedShort());
        position.set(Position.PREFIX_ADC + 2, buf.readUnsignedShort());
        position.set(Position.PREFIX_ADC + 3, buf.readUnsignedShort());
        position.set(Position.PREFIX_ADC + 4, buf.readUnsignedShort());
    }

    if (BitUtil.check(flags, 4)) {
        position.set(Position.PREFIX_TEMP + 1, buf.readByte());
        buf.getUnsignedByte(buf.readerIndex()); // control (>> 4)
        position.set(Position.KEY_INPUT, buf.readUnsignedShort() & 0x0fff);
        buf.readUnsignedShort(); // old sensor state (& 0x0fff)
    }

    if (BitUtil.check(flags, 5)) {
        position.set(Position.KEY_BATTERY, buf.getUnsignedByte(buf.readerIndex()) >> 2);
        position.set(Position.KEY_POWER, buf.readUnsignedShort() & 0x03ff);
        position.set(Position.KEY_DEVICE_TEMP, buf.readByte());

        position.set(Position.KEY_RSSI, buf.getUnsignedByte(buf.readerIndex()) >> 5);

        int satellites = buf.readUnsignedByte() & 0x1f;
        position.setValid(satellites >= 3);
        position.set(Position.KEY_SATELLITES, satellites);
    }

    // other data

    return position;
}
 
Example 20
Project: traccar-service   File: Gl200FrameDecoder.java   Source Code and License Vote up 4 votes
@Override
protected Object decode(
        ChannelHandlerContext ctx, Channel channel, ChannelBuffer buf) throws Exception {

    if (buf.readableBytes() < MINIMUM_LENGTH) {
        return null;
    }

    if (isBinary(buf)) {

        int length;
        switch (buf.toString(buf.readerIndex(), 4, StandardCharsets.US_ASCII)) {
            case "+ACK":
                length = buf.getUnsignedByte(buf.readerIndex() + 6);
                break;
            case "+INF":
            case "+BNF":
                length = buf.getUnsignedShort(buf.readerIndex() + 7);
                break;
            case "+HBD":
                length = buf.getUnsignedByte(buf.readerIndex() + 5);
                break;
            case "+CRD":
            case "+BRD":
                length = buf.getUnsignedShort(buf.readerIndex() + 6);
                break;
            default:
                length = buf.getUnsignedShort(buf.readerIndex() + 9);
                break;
        }

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

    } else {

        int endIndex = buf.indexOf(buf.readerIndex(), buf.writerIndex(), (byte) '$');
        if (endIndex < 0) {
            endIndex = buf.indexOf(buf.readerIndex(), buf.writerIndex(), (byte) 0);
        }
        if (endIndex > 0) {
            ChannelBuffer frame = buf.readBytes(endIndex - buf.readerIndex());
            buf.readByte(); // delimiter
            return frame;
        }

    }

    return null;
}