List of usage examples for io.netty.buffer ByteBuf readUnsignedInt
public abstract long readUnsignedInt();
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 .ja v a 2 s .c o m 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:pl.tcs.btppllib.telegrams.OcitGetTimeResponse.java
@Override public OcitResponseTelegram fromByteBuf(ByteBuf buf) { OcitResponseTelegram resp = super.fromByteBuf(buf); long devTime = buf.readUnsignedInt() * 1000; setDevTime(new Date(devTime)); setTimeZone(buf.readInt());//from w w w . java2 s. c o m setSource(buf.readUnsignedByte()); log.debug("Device time: " + getDevTime().toString()); return this; }
From source file:pl.tcs.btppllib.telegrams.OcitZIntersectionOnOffResponse.java
@Override public OcitResponseTelegram fromByteBuf(ByteBuf buf) { OcitResponseTelegram resp = super.fromByteBuf(buf); if (method == ZIntersectionOnOff_Get.getVal()) { actProcess = buf.readUnsignedInt(); actStartTime = buf.readUnsignedInt(); actEndTime = buf.readUnsignedInt(); actOnOffState = OnOffState.fromInt(buf.readUnsignedByte()); nextProcess = buf.readUnsignedInt(); nextStartTime = buf.readUnsignedInt(); nextEndTime = buf.readUnsignedInt(); nextOnOffState = OnOffState.fromInt(buf.readUnsignedByte()); }/*w w w.j av a2s. co m*/ log.info(String.format("actOnOffState=%s nextOnOffState=%s", actOnOffState.toString(), nextOnOffState.toString())); return this; }
From source file:sas.systems.imflux.packet.DataPacket.java
License:Apache License
/** * Decodes a {@code DataPacket}.// w w w. j a va 2 s . com * * @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; }
From source file:sas.systems.imflux.packet.rtcp.ByePacket.java
License:Apache License
/** * Decodes a receiver report from a {@code ByteBuf}. This method is called by {@code ControlPacket.decode()}. * //ww w. j av a 2 s.c o m * @param buffer bytes, which still have to be decoded * @param innerBlocks number of reports in this packet * @param length remaining 32bit words * @return a new {@code ByePacket} containing all information from the {@code buffer} */ public static ByePacket decode(ByteBuf buffer, byte innerBlocks, int length) { ByePacket packet = new ByePacket(); int read = 0; for (int i = 0; i < innerBlocks; i++) { packet.addSsrc(buffer.readUnsignedInt()); read += 4; } // Length is written in 32bit words, not octet count. int lengthInOctets = length * 4; if (read < lengthInOctets) { byte[] reasonBytes = new byte[buffer.readUnsignedByte()]; buffer.readBytes(reasonBytes); packet.reasonForLeaving = new String(reasonBytes, CharsetUtil.UTF_8); read += (1 + reasonBytes.length); if (read < lengthInOctets) { // Skip remaining bytes (used for padding). This takes care of both the null termination bytes (padding // of the 'reason for leaving' string) and the packet padding bytes. buffer.skipBytes(lengthInOctets - read); } } return packet; }
From source file:sas.systems.imflux.packet.rtcp.ReceiverReportPacket.java
License:Apache License
/** * Decodes a receiver report from a {@code ByteBuf}. This method is called by {@code ControlPacket.decode()}. * //from ww w.ja v a 2 s .c o m * @param buffer bytes, which still have to be decoded * @param innerBlocks number of reports in this packet * @param length remaining 32bit words * @return a new {@code RecieverReportPacket} containing all information from the {@code buffer} */ public static ReceiverReportPacket decode(ByteBuf buffer, byte innerBlocks, int length) { ReceiverReportPacket packet = new ReceiverReportPacket(); packet.setSenderSsrc(buffer.readUnsignedInt()); int read = 4; for (int i = 0; i < innerBlocks; i++) { packet.addReportBlock(ReceptionReport.decode(buffer)); read += 24; // Each SR/RR block has 24 bytes (6 32bit words) } // Length is written in 32bit words, not octet count. int lengthInOctets = length * 4; // (hasPadding == true) check is not done here. RFC respecting implementations will set the padding bit to 1 // if length of packet is bigger than the necessary to convey the data; therefore it's a redundant check. if (read < lengthInOctets) { // Skip remaining bytes (used for padding). buffer.skipBytes(lengthInOctets - read); } return packet; }
From source file:sas.systems.imflux.packet.rtcp.ReceptionReport.java
License:Apache License
public static ReceptionReport decode(ByteBuf buffer) { ReceptionReport block = new ReceptionReport(); block.setSsrc(buffer.readUnsignedInt()); block.setFractionLost(buffer.readUnsignedByte()); block.setCumulativeNumberOfPacketsLost(buffer.readUnsignedMedium()); block.setExtendedHighestSequenceNumberReceived(buffer.readUnsignedInt()); block.setInterArrivalJitter(buffer.readUnsignedInt()); block.setLastSenderReport(buffer.readUnsignedInt()); block.setDelaySinceLastSenderReport(buffer.readUnsignedInt()); return block; }
From source file:sas.systems.imflux.packet.rtcp.SdesChunk.java
License:Apache License
/** * Decodes a chunk of the SourceDescriptionPacket. * // w w w . j ava 2s. c o m * @param buffer containing the bytes * @return a new object of type {@code SdesChunk} */ public static SdesChunk decode(ByteBuf buffer) { SdesChunk chunk = new SdesChunk(); chunk.ssrc = buffer.readUnsignedInt(); // Because some genius thought that 32bit alignment would be cool, we must count the amount of bytes remaining // after decoding each SdesChunkItem so that when we read the end/null item, we know how many more bytes we // must read to discard the padding bytes (hit the 32bit alignment barrier). int read = 0; for (;;) { if (buffer.readableBytes() == 0) { // Some implementations don't write the mandatory last item (end/null). return chunk; } int remaining = buffer.readableBytes(); SdesChunkItem item = SdesChunkItems.decode(buffer); read += remaining - buffer.readableBytes(); if (item.getType().equals(SdesChunkItem.Type.NULL)) { int paddingBytes = 4 - (read % 4); if (paddingBytes != 4) { buffer.skipBytes(paddingBytes); } return chunk; } chunk.addItem(item); } }
From source file:sas.systems.imflux.packet.rtcp.SenderReportPacket.java
License:Apache License
/** * Decodes a control packet from a {@code ByteBuf}. This method is called by {@code ControlPacket.decode()}. * //from www . j av a 2s. c o m * @param buffer bytes, which still have to be decoded * @param innerBlocks number of reports in this packet * @param length remaining 32bit words * @return a new {@code SenderReportPacket} containing all information from the {@code buffer} */ public static SenderReportPacket decode(ByteBuf buffer, byte innerBlocks, int length) { SenderReportPacket packet = new SenderReportPacket(); packet.setSenderSsrc(buffer.readUnsignedInt()); // reads 4 bytes (one 32bit word) from the buffer packet.setNtpTimestamp( // reads 2x 4bytes (two 32bit words) from the buffer BigInteger.valueOf(buffer.readUnsignedInt()) // read high word .multiply(BigInteger.valueOf(4294967296l)) // multiply by 0x0001 0000 0000 to shift value to // the right position .add(BigInteger.valueOf(buffer.readUnsignedInt())) // read low word ); packet.setRtpTimestamp(buffer.readUnsignedInt()); // reads 4 bytes (one 32bit word) from the buffer packet.setSenderPacketCount(buffer.readUnsignedInt()); // reads 4 bytes (one 32bit word) from the buffer packet.setSenderOctetCount(buffer.readUnsignedInt()); // reads 4 bytes (one 32bit word) from the buffer // = 24 bytes int read = 24; for (int i = 0; i < innerBlocks; i++) { packet.addReportBlock(ReceptionReport.decode(buffer)); read += 24; // Each SR/RR block has 24 bytes (6 32bit words) } // Length is written in 32bit words, not octet count. int lengthInOctets = length * 4; // (hasPadding == true) check is not done here. RFC respecting implementations will set the padding bit to 1 // if length of packet is bigger than the necessary to convey the data; therefore it's a redundant check. if (read < lengthInOctets) { // Skip remaining bytes (used for padding). buffer.skipBytes(lengthInOctets - read); } return packet; }
From source file:uk.ac.lancs.stopcock.netty.OpenFlowDecoder.java
License:Apache License
@Override protected void decode(ChannelHandlerContext channelHandlerContext, ByteBuf byteBuf, List<Object> objects) throws Exception { /* Sanity check. */ if (byteBuf.readableBytes() < 8) { throw new IllegalStateException(); }/*from w ww .j a v a 2s . c om*/ /* Read OpenFlow Header. */ short version = byteBuf.readUnsignedByte(); short typeId = byteBuf.readUnsignedByte(); int length = byteBuf.readUnsignedShort(); long transactionId = byteBuf.readUnsignedInt(); /* Sanity check that we have enough data. */ if (byteBuf.readableBytes() < (length - 8)) { throw new IllegalStateException(); } /* Copy the data portion of the OpenFlow packet and store it. */ byteBuf.resetReaderIndex(); byte[] originalData = byteBuf.readBytes(length).array(); /* Construct the OpenFlow header and container for both it and data. */ Header header = new Header(version, typeId, length, transactionId); Type type = Type.getById(typeId); /* Get full openflow packet for processing with openflowj */ byteBuf.resetReaderIndex(); /* Call openflowj using our Netty 3.9.X -> Netty 4.0.0 proxy object. */ OFMessage message = OFFactories.getGenericReader().readFrom(new NettyCompatibilityChannelBuffer(byteBuf)); /* Container object for header, raw data and openflowj message. */ Container container = new Container(header, originalData, type, message); /* Add to the Netty pipeline. */ objects.add(container); }