Example usage for io.netty.buffer ByteBuf readUnsignedMedium

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

Introduction

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

Prototype

public abstract int readUnsignedMedium();

Source Link

Document

Gets an unsigned 24-bit medium integer at the current readerIndex and increases the readerIndex by 3 in this buffer.

Usage

From source file:org.opendaylight.protocol.bgp.evpn.impl.esi.types.MacParser.java

License:Open Source License

@Override
public Esi parseEsi(final ByteBuf buffer) {
    final MacAutoGenerated t3 = new MacAutoGeneratedBuilder()
            .setSystemMacAddress(/*from w w  w.j av a  2  s.c o  m*/
                    IetfYangUtil.INSTANCE.macAddressFor(ByteArray.readBytes(buffer, MAC_ADDRESS_LENGTH)))
            .setLocalDiscriminator(new Uint24(Long.valueOf(buffer.readUnsignedMedium()))).build();
    return new MacAutoGeneratedCaseBuilder().setMacAutoGenerated(t3).build();
}

From source file:org.opendaylight.protocol.bgp.labeled.unicast.OriginatorSrgbTlvParser.java

License:Open Source License

private static List<SrgbValue> parseSrgbs(final ByteBuf buffer) {
    Preconditions.checkState(buffer.readableBytes() % SRGB_LENGTH == 0,
            "Number of SRGBs doesn't fit available bytes.");
    final List<SrgbValue> ret = new ArrayList<SrgbValue>();
    while (buffer.isReadable()) {
        ret.add(new SrgbValueBuilder().setBase(new Srgb((long) buffer.readUnsignedMedium()))
                .setRange(new Srgb((long) buffer.readUnsignedMedium())).build());
    }/* ww  w.  j a  v a 2s .com*/
    return ret;
}

From source file:org.opendaylight.protocol.bgp.linkstate.attribute.sr.SidLabelIndexParser.java

License:Open Source License

static SidLabelIndex parseSidLabelIndex(final Size length, final ByteBuf buffer) {
    switch (length) {
    case LABEL://from   w  w  w .j  ava  2s. c om
        return new LocalLabelCaseBuilder()
                .setLocalLabel(new MplsLabel(new Long(buffer.readUnsignedMedium() & LABEL_MASK))).build();
    case SID:
        return new SidCaseBuilder().setSid(buffer.readUnsignedInt()).build();
    case IPV6_ADD:
        return new Ipv6AddressCaseBuilder().setIpv6Address(Ipv6Util.addressForByteBuf(buffer)).build();
    default:
        return null;
    }
}

From source file:org.opendaylight.protocol.bgp.linkstate.attribute.sr.SrNodeAttributesParser.java

License:Open Source License

public static SrCapabilities parseSrCapabilities(final ByteBuf buffer, final ProtocolId protocol) {
    final SrCapabilitiesBuilder builder = new SrCapabilitiesBuilder();
    final BitArray flags = BitArray.valueOf(buffer, FLAGS_SIZE);
    setFlags(flags, protocol, builder);//w w w .  j  a  v  a  2  s  .  c o  m
    buffer.skipBytes(RESERVERED);
    builder.setRangeSize((long) buffer.readUnsignedMedium());
    builder.setSidLabelIndex(SidLabelIndexParser.parseSidSubTlv(buffer));
    return builder.build();
}

From source file:org.opendaylight.protocol.bgp.linkstate.impl.attribute.sr.SidLabelIndexParser.java

License:Open Source License

public static SidLabelIndex parseSidLabelIndex(final Size length, final ByteBuf buffer) {
    switch (length) {
    case LABEL:/*from  w  w  w.  ja v  a 2  s . co  m*/
        return new LocalLabelCaseBuilder()
                .setLocalLabel(new MplsLabel(new Long(buffer.readUnsignedMedium() & LABEL_MASK))).build();
    case SID:
        return new SidCaseBuilder().setSid(buffer.readUnsignedInt()).build();
    case IPV6_ADD:
        return new Ipv6AddressCaseBuilder().setIpv6Address(Ipv6Util.addressForByteBuf(buffer)).build();
    default:
        return null;
    }
}

From source file:org.opendaylight.protocol.util.MplsLabelUtil.java

License:Open Source License

/**
 * Creates and returns MplsLabel object from 20 leftmost bits in the incoming buffer.
 * Moves reader index by 3.//from   w ww . j av  a 2s  .c  om
 * @param buffer expecting 3 bytes with 20 leftmost bits as label
 * @return MplsLabel object
 */
public static MplsLabel mplsLabelForByteBuf(final ByteBuf buffer) {
    return new MplsLabel(new Long(buffer.readUnsignedMedium() >> LABEL_OFFSET));
}

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

License:Apache License

private void decodeEventData(Position position, ByteBuf buf, int event) {
    switch (event) {
    case 2://  www  .j  ava2  s  . c om
    case 40:
        buf.readUnsignedByte();
        break;
    case 9:
        buf.readUnsignedMedium();
        break;
    case 31:
    case 32:
        buf.readUnsignedShort();
        break;
    case 38:
        buf.skipBytes(4 * 9);
        break;
    case 113:
        buf.readUnsignedInt();
        buf.readUnsignedByte();
        break;
    case 119:
        position.set("eventData",
                ByteBufUtil.hexDump(buf, buf.readerIndex(), Math.min(buf.readableBytes(), 1024)));
        break;
    case 121:
    case 142:
        buf.readLong();
        break;
    case 130:
        buf.readUnsignedInt(); // incorrect
        break;
    case 188:
        decodeEB(position, buf);
        break;
    default:
        break;
    }
}

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

License:Apache License

private void decodeCanData(ByteBuf buf, Position position) {

    buf.readUnsignedMedium(); // packet identifier
    position.set(Position.KEY_VERSION_FW, buf.readUnsignedByte());
    int count = buf.readUnsignedByte();
    buf.readUnsignedByte(); // batch count
    buf.readUnsignedShort(); // selector bit
    buf.readUnsignedInt(); // timestamp

    buf.skipBytes(8);/*from  w w  w .  java  2s. co m*/

    ArrayList<ByteBuf> values = new ArrayList<>(count);

    for (int i = 0; i < count; i++) {
        values.add(buf.readSlice(8));
    }

    for (int i = 0; i < count; i++) {
        ByteBuf value = values.get(i);
        switch (buf.readInt()) {
        case 0x20D:
            position.set(Position.KEY_RPM, value.readShortLE());
            position.set("dieselTemperature", value.readShortLE() * 0.1);
            position.set("batteryVoltage", value.readShortLE() * 0.01);
            position.set("supplyAirTempDep1", value.readShortLE() * 0.1);
            break;
        case 0x30D:
            position.set("activeAlarm", ByteBufUtil.hexDump(value));
            break;
        case 0x40C:
            position.set("airTempDep1", value.readShortLE() * 0.1);
            position.set("airTempDep2", value.readShortLE() * 0.1);
            break;
        case 0x40D:
            position.set("coldUnitState", ByteBufUtil.hexDump(value));
            break;
        case 0x50C:
            position.set("defrostTempDep1", value.readShortLE() * 0.1);
            position.set("defrostTempDep2", value.readShortLE() * 0.1);
            break;
        case 0x50D:
            position.set("condenserPressure", value.readShortLE() * 0.1);
            position.set("suctionPressure", value.readShortLE() * 0.1);
            break;
        case 0x58C:
            value.readByte();
            value.readShort(); // index
            switch (value.readByte()) {
            case 0x01:
                position.set("setpointZone1", value.readIntLE() * 0.1);
                break;
            case 0x02:
                position.set("setpointZone2", value.readIntLE() * 0.1);
                break;
            case 0x05:
                position.set("unitType", value.readIntLE());
                break;
            case 0x13:
                position.set("dieselHours", value.readIntLE() / 60 / 60);
                break;
            case 0x14:
                position.set("electricHours", value.readIntLE() / 60 / 60);
                break;
            case 0x17:
                position.set("serviceIndicator", value.readIntLE());
                break;
            case 0x18:
                position.set("softwareVersion", value.readIntLE() * 0.01);
                break;
            default:
                break;
            }
            break;
        default:
            LOGGER.warn("Aplicom CAN decoding error", new UnsupportedOperationException());
            break;
        }
    }
}

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

License:Apache License

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

    ByteBuf buf = (ByteBuf) msg;

    char protocol = (char) buf.readByte();
    int version = buf.readUnsignedByte();

    String imei;/*from w  w w.jav  a 2s .c  o  m*/
    if ((version & 0x80) != 0) {
        imei = String.valueOf((buf.readUnsignedInt() << (3 * 8)) | buf.readUnsignedMedium());
    } else {
        imei = String.valueOf(imeiFromUnitId(buf.readUnsignedMedium()));
    }

    buf.readUnsignedShort(); // length

    int selector = DEFAULT_SELECTOR_D;
    if (protocol == 'E') {
        selector = DEFAULT_SELECTOR_E;
    } else if (protocol == 'F') {
        selector = DEFAULT_SELECTOR_F;
    }
    if ((version & 0x40) != 0) {
        selector = buf.readUnsignedMedium();
    }

    Position position = new Position(getProtocolName());
    DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, imei);
    if (deviceSession == null) {
        return null;
    }
    position.setDeviceId(deviceSession.getDeviceId());

    int event = buf.readUnsignedByte();
    position.set(Position.KEY_EVENT, event);
    position.set("eventInfo", buf.readUnsignedByte());

    if (protocol == 'D') {
        decodeD(position, buf, selector, event);
    } else if (protocol == 'E') {
        decodeE(position, buf, selector);
    } else if (protocol == 'H') {
        decodeH(position, buf, selector);
    } else if (protocol == 'F') {
        decodeF(position, buf, selector);
    } else {
        return null;
    }

    return position;
}

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

License:Apache License

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

    ByteBuf buf = (ByteBuf) msg;

    if (channel != null) {
        channel.writeAndFlush(new NetworkMessage(Unpooled.wrappedBuffer(new byte[] { 0x06 }), remoteAddress));
    }//w  w w  . j  ava2  s . c o  m

    buf.readUnsignedByte(); // protocol
    buf.readUnsignedShort(); // length

    String imei = String.format("%08d", buf.readUnsignedInt())
            + String.format("%07d", buf.readUnsignedMedium());
    DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, imei);
    if (deviceSession == null) {
        return null;
    }

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

    while (buf.readableBytes() > 2) {

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

        buf.readUnsignedByte(); // index

        position.setValid(true);
        position.setLatitude(buf.readInt() * 0.000001);
        position.setLongitude(buf.readInt() * 0.000001);

        DateBuilder dateBuilder = new DateBuilder().setDate(1980, 1, 6)
                .addMillis(buf.readUnsignedInt() * 1000L);
        position.setTime(dateBuilder.getDate());

        position.setSpeed(UnitsConverter.knotsFromKph(buf.readUnsignedByte() * 2));
        position.setCourse(buf.readUnsignedByte() * 2);

        int reason = buf.readUnsignedMedium();
        position.set(Position.KEY_EVENT, reason);

        int status = buf.readUnsignedShort();
        position.set(Position.KEY_STATUS, status);

        position.set(Position.PREFIX_IO + 1, buf.readUnsignedByte());
        position.set(Position.PREFIX_ADC + 1, buf.readUnsignedByte());
        position.set(Position.KEY_BATTERY, buf.readUnsignedByte());
        position.set(Position.KEY_POWER, buf.readUnsignedByte());

        buf.readUnsignedByte(); // max journey speed
        buf.skipBytes(6); // accelerometer
        position.set(Position.KEY_ODOMETER_TRIP, buf.readUnsignedShort());
        buf.readUnsignedShort(); // journey idle time

        position.setAltitude(buf.readUnsignedByte() * 20);

        int quality = buf.readUnsignedByte();
        position.set(Position.KEY_SATELLITES, quality & 0xf);
        position.set(Position.KEY_RSSI, quality >> 4);

        buf.readUnsignedByte(); // geofence events

        if (BitUtil.check(status, 8)) {
            position.set(Position.KEY_DRIVER_UNIQUE_ID, buf.readSlice(7).toString(StandardCharsets.US_ASCII));
            position.set(Position.KEY_ODOMETER, buf.readUnsignedMedium() * 1000);
            position.set(Position.KEY_HOURS, UnitsConverter.msFromHours(buf.readUnsignedShort()));
        }

        if (BitUtil.check(status, 6)) {
            LOGGER.warn("Extension data is not supported");
            return position;
        }

        positions.add(position);

    }

    return positions;
}