INET Framework for OMNeT++/OMNEST
inet::BMacHeaderSerializer Class Reference

Converts between BMacHeader and binary (network byte order) BMAC headers. More...

#include <BMacHeaderSerializer.h>

Inheritance diagram for inet::BMacHeaderSerializer:
inet::FieldsChunkSerializer inet::ChunkSerializer

Public Member Functions

 BMacHeaderSerializer ()
 
- Public Member Functions inherited from inet::FieldsChunkSerializer
virtual void serialize (MemoryOutputStream &stream, const Ptr< const Chunk > &chunk, b offset, b length) const override
 Serializes a chunk into a stream by writing the bytes representing the chunk at the end of the stream. More...
 
virtual const Ptr< Chunkdeserialize (MemoryInputStream &stream, const std::type_info &typeInfo) const override
 Deserializes a chunk from a stream by reading the bytes at the current position of the stream. More...
 

Protected Member Functions

virtual void serialize (MemoryOutputStream &stream, const Ptr< const Chunk > &chunk) const override
 Serializes a chunk into a stream by writing all bytes representing the chunk at the end of the stream. More...
 
virtual const Ptr< Chunkdeserialize (MemoryInputStream &stream) const override
 Deserializes a chunk from a stream by reading the bytes at the current position of the stream. More...
 

Additional Inherited Members

- Static Public Attributes inherited from inet::ChunkSerializer
static b totalSerializedLength = b(0)
 
static b totalDeserializedLength = b(0)
 

Detailed Description

Converts between BMacHeader and binary (network byte order) BMAC headers.

Constructor & Destructor Documentation

◆ BMacHeaderSerializer()

inet::BMacHeaderSerializer::BMacHeaderSerializer ( )
inline
25 : FieldsChunkSerializer() {}

Member Function Documentation

◆ deserialize()

const Ptr< Chunk > inet::BMacHeaderSerializer::deserialize ( MemoryInputStream stream) const
overrideprotectedvirtual

Deserializes a chunk from a stream by reading the bytes at the current position of the stream.

The current stream position is updated according to the length of the returned chunk.

Implements inet::FieldsChunkSerializer.

51 {
52  b startPos = stream.getPosition();
53  BMacType type = static_cast<BMacType>(stream.readByte());
54  b length = b(stream.readUint16Be());
55  MacAddress srcAddr = stream.readMacAddress();
56  MacAddress destAddr = stream.readMacAddress();
57  switch (type) {
58  case BMAC_PREAMBLE:
59  case BMAC_ACK: {
60  auto ctrlFrame = makeShared<BMacControlFrame>();
61  ctrlFrame->setType(type);
62  ctrlFrame->setChunkLength(length);
63  ctrlFrame->setSrcAddr(srcAddr);
64  ctrlFrame->setDestAddr(destAddr);
65  auto remainderBits = b(length - (stream.getPosition() - startPos)).get();
66  if (remainderBits >= 8)
67  stream.readByteRepeatedly('?', remainderBits >> 3);
68  if (remainderBits & 7)
69  stream.readBitRepeatedly(0, remainderBits & 7);
70  return ctrlFrame;
71  }
72  case BMAC_DATA: {
73  auto dataFrame = makeShared<BMacDataFrameHeader>();
74  dataFrame->setType(type);
75  dataFrame->setChunkLength(length);
76  dataFrame->setSrcAddr(srcAddr);
77  dataFrame->setDestAddr(destAddr);
78  dataFrame->setSequenceId(stream.readUint64Be());
79  dataFrame->setNetworkProtocol(stream.readUint16Be());
80  auto remainderBits = b(length - (stream.getPosition() - startPos)).get();
81  if (remainderBits >= 8)
82  stream.readByteRepeatedly('?', remainderBits >> 3);
83  if (remainderBits & 7)
84  stream.readBitRepeatedly(0, remainderBits & 7);
85  return dataFrame;
86  }
87  default: {
88  auto unknownFrame = makeShared<BMacHeaderBase>();
89  unknownFrame->setType(type);
90  unknownFrame->setChunkLength(length);
91  auto remainderBits = b(length - (stream.getPosition() - startPos)).get();
92  if (remainderBits >= 8)
93  stream.readByteRepeatedly('?', remainderBits >> 3);
94  if (remainderBits & 7)
95  stream.readBitRepeatedly(0, remainderBits & 7);
96  unknownFrame->markIncorrect();
97  return unknownFrame;
98  }
99  }
100 }

◆ serialize()

void inet::BMacHeaderSerializer::serialize ( MemoryOutputStream stream,
const Ptr< const Chunk > &  chunk 
) const
overrideprotectedvirtual

Serializes a chunk into a stream by writing all bytes representing the chunk at the end of the stream.

Implements inet::FieldsChunkSerializer.

20 {
21  b startPos = stream.getLength();
22  const auto& header = staticPtrCast<const BMacHeaderBase>(chunk);
23  stream.writeByte(header->getType());
24  stream.writeUint16Be(b(header->getChunkLength()).get());
25  stream.writeMacAddress(header->getSrcAddr());
26  stream.writeMacAddress(header->getDestAddr());
27  switch (header->getType()) {
28  case BMAC_PREAMBLE:
29  case BMAC_ACK: {
30  break;
31  }
32  case BMAC_DATA: {
33  const auto& dataFrame = staticPtrCast<const BMacDataFrameHeader>(chunk);
34  stream.writeUint64Be(dataFrame->getSequenceId());
35  stream.writeUint16Be(dataFrame->getNetworkProtocol());
36  break;
37  }
38  default:
39  throw cRuntimeError("Unknown header type: %d", header->getType());
40  }
41  auto remainderBits = b(header->getChunkLength() - (stream.getLength() - startPos)).get();
42  if (remainderBits < 0)
43  throw cRuntimeError("BMacHeader length = %s smaller than required %s, try to increase the 'headerLength' parameter", header->getChunkLength().str().c_str(), (stream.getLength() - startPos).str().c_str());
44  if (remainderBits >= 8)
45  stream.writeByteRepeatedly('?', remainderBits >> 3);
46  if (remainderBits & 7)
47  stream.writeBitRepeatedly(0, remainderBits & 7);
48 }

The documentation for this class was generated from the following files:
inet::BMAC_DATA
@ BMAC_DATA
Definition: BMacHeader_m.h:76
inet::BMAC_PREAMBLE
@ BMAC_PREAMBLE
Definition: BMacHeader_m.h:75
type
removed type
Definition: IUdp-gates.txt:7
inet::units::values::b
value< int64_t, units::b > b
Definition: Units.h:1241
inet::BMacType
BMacType
Enum generated from inet/linklayer/bmac/BMacHeader.msg:15 by opp_msgtool.
Definition: BMacHeader_m.h:74
inet::BMAC_ACK
@ BMAC_ACK
Definition: BMacHeader_m.h:77