Bitstream Interpretation Library (BIL)  0.1
Type2PacketRawData.cpp
Go to the documentation of this file.
1 
9 
10 using namespace bil;
11 
12 const size_t HEADERTYPE_BITSHIFT = 29;
13 const boost::uint32_t HEADERTYPE_VALUE = 0x2;
14 
15 const size_t OPCODE_BITSHIFT = 27;
16 const boost::uint32_t OPCODE_BITMASK = 0x3;
17 
18 const boost::uint32_t WORDCOUNT_BITMASK = 0x7ffffff;
19 
20 
21 size_t bil::isType2Packet(const boost::uint32_t* words, size_t wordCount)
22 {
23  // packet header is one word
24  if (1 > wordCount) return 0;
25 
26  // extract packet header
27  boost::uint32_t header = *words;
28  // check header type
29  if (HEADERTYPE_VALUE != (header >> HEADERTYPE_BITSHIFT)) return 0;
30  // if packet opcode is a write op, then the packet header is followed by the
31  // data to be written
33  return 1 + (header & WORDCOUNT_BITMASK);
34  else return 1;
35 }
36 
37 
38 size_t bil::type2PacketWordCount(const Type2Packet& type2Packet)
39 {
40  // if packet opcode is REGISTER_WRITE, then the one word big packet header
41  // is followed by the packet data. Otherwise packet is header only.
42  if (PacketOpcode::REGISTER_WRITE == (type2Packet.opcode()))
43  return 1 + type2Packet.wordCount();
44  else return 1;
45 }
46 
47 
48 size_t bil::writeType2Packet(const Type2Packet& type2Packet, boost::uint32_t* words, size_t wordCount)
49 {
50  // packet header is one word
51  if (1 > wordCount) throw Exception();
52 
53  // check packet
54  if (type2Packet.opcode() > OPCODE_BITMASK) throw Exception();
55  if (type2Packet.wordCount() > WORDCOUNT_BITMASK) throw Exception();
56 
57  // write packet header
58  *words++ =
60  (type2Packet.opcode() << OPCODE_BITSHIFT) |
61  (type2Packet.wordCount());
62 
63  // if packet opcode is REGISTER_WRITE, then packet data follows
64  if (PacketOpcode::REGISTER_WRITE != (type2Packet.opcode())) return 1;
65  // check data length
66  if ((1 + type2Packet.wordCount()) > wordCount) throw Exception();
67  // write packet data
68  memcpy(words, type2Packet.dataWords(), type2Packet.wordCount() << 2);
69 
70  // return written size
71  return 1 + type2Packet.wordCount();
72 }
73 
74 
75 size_t bil::readType2Packet(Type2Packet& type2Packet, const boost::uint32_t* words, size_t wordCount)
76 {
77  // packet header is one word
78  if (1 > wordCount) throw Exception();
79 
80  // extract packet header and read its data
81  boost::uint32_t header = *words++;
82  // check packet header type
83  if (HEADERTYPE_VALUE != (header >> HEADERTYPE_BITSHIFT)) throw Exception();
84  // extract opcode
85  type2Packet.opcode((header >> OPCODE_BITSHIFT) & OPCODE_BITMASK);
86  // extract word count
87  type2Packet.wordCount(header & WORDCOUNT_BITMASK);
88 
89  // if packet opcode is REGISTER_WRITE, then packet data follows
90  if (PacketOpcode::REGISTER_WRITE != (type2Packet.opcode())) return 1;
91  // check packet length
92  if ((1 + type2Packet.wordCount()) > wordCount) throw Exception();
93  // load data
94  memcpy(type2Packet.dataWords(), words, type2Packet.wordCount() << 2);
95  return 1 + type2Packet.wordCount();
96 }