WPILibC++  unspecified
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Pages
WireDecoder.h
1 /*----------------------------------------------------------------------------*/
2 /* Copyright (c) FIRST 2015. All Rights Reserved. */
3 /* Open Source Software - may be modified and shared by FRC teams. The code */
4 /* must be accompanied by the FIRST BSD license file in the root directory of */
5 /* the project. */
6 /*----------------------------------------------------------------------------*/
7 
8 #ifndef NT_WIREDECODER_H_
9 #define NT_WIREDECODER_H_
10 
11 #include <cstddef>
12 
13 #include "nt_Value.h"
14 #include "support/leb128.h"
15 #include "support/raw_istream.h"
16 //#include "Log.h"
17 
18 namespace nt {
19 
20 /* Decodes network data into native representation.
21  * This class is designed to read from a raw_istream, which provides a blocking
22  * read interface. There are no provisions in this class for resuming a read
23  * that was interrupted partway. Read functions return false if
24  * raw_istream.read() returned false (indicating the end of the input data
25  * stream).
26  */
27 class WireDecoder {
28  public:
29  explicit WireDecoder(wpi::raw_istream& is, unsigned int proto_rev);
30  ~WireDecoder();
31 
32  void set_proto_rev(unsigned int proto_rev) { m_proto_rev = proto_rev; }
33 
34  /* Get the active protocol revision. */
35  unsigned int proto_rev() const { return m_proto_rev; }
36 
37  /* Clears error indicator. */
38  void Reset() { m_error = nullptr; }
39 
40  /* Returns error indicator (a string describing the error). Returns nullptr
41  * if no error has occurred.
42  */
43  const char* error() const { return m_error; }
44 
45  void set_error(const char* error) { m_error = error; }
46 
47  /* Reads the specified number of bytes.
48  * @param buf pointer to read data (output parameter)
49  * @param len number of bytes to read
50  * Caution: the buffer is only temporarily valid.
51  */
52  bool Read(const char** buf, std::size_t len) {
53  if (len > m_allocated) Realloc(len);
54  *buf = m_buf;
55  m_is.read(m_buf, len);
56 #if 0
57  nt::Logger& logger = nt::Logger::GetInstance();
58  if (logger.min_level() <= NT_LOG_DEBUG4 && logger.HasLogger()) {
59  std::ostringstream oss;
60  oss << "read " << len << " bytes:" << std::hex;
61  if (!rv)
62  oss << "error";
63  else {
64  for (std::size_t i=0; i < len; ++i)
65  oss << ' ' << (unsigned int)((*buf)[i]);
66  }
67  logger.Log(NT_LOG_DEBUG4, __FILE__, __LINE__, oss.str().c_str());
68  }
69 #endif
70  return !m_is.has_error();
71  }
72 
73  /* Reads a single byte. */
74  bool Read8(unsigned int* val) {
75  const char* buf;
76  if (!Read(&buf, 1)) return false;
77  *val = (*reinterpret_cast<const unsigned char*>(buf)) & 0xff;
78  return true;
79  }
80 
81  /* Reads a 16-bit word. */
82  bool Read16(unsigned int* val) {
83  const char* buf;
84  if (!Read(&buf, 2)) return false;
85  unsigned int v = (*reinterpret_cast<const unsigned char*>(buf)) & 0xff;
86  ++buf;
87  v <<= 8;
88  v |= (*reinterpret_cast<const unsigned char*>(buf)) & 0xff;
89  *val = v;
90  return true;
91  }
92 
93  /* Reads a 32-bit word. */
94  bool Read32(unsigned long* val) {
95  const char* buf;
96  if (!Read(&buf, 4)) return false;
97  unsigned int v = (*reinterpret_cast<const unsigned char*>(buf)) & 0xff;
98  ++buf;
99  v <<= 8;
100  v |= (*reinterpret_cast<const unsigned char*>(buf)) & 0xff;
101  ++buf;
102  v <<= 8;
103  v |= (*reinterpret_cast<const unsigned char*>(buf)) & 0xff;
104  ++buf;
105  v <<= 8;
106  v |= (*reinterpret_cast<const unsigned char*>(buf)) & 0xff;
107  *val = v;
108  return true;
109  }
110 
111  /* Reads a double. */
112  bool ReadDouble(double* val);
113 
114  /* Reads an ULEB128-encoded unsigned integer. */
115  bool ReadUleb128(unsigned long* val) { return wpi::ReadUleb128(m_is, val); }
116 
117  bool ReadType(NT_Type* type);
118  bool ReadString(std::string* str);
119  std::shared_ptr<Value> ReadValue(NT_Type type);
120 
121  WireDecoder(const WireDecoder&) = delete;
122  WireDecoder& operator=(const WireDecoder&) = delete;
123 
124  protected:
125  /* The protocol revision. E.g. 0x0200 for version 2.0. */
126  unsigned int m_proto_rev;
127 
128  /* Error indicator. */
129  const char* m_error;
130 
131  private:
132  /* Reallocate temporary buffer to specified length. */
133  void Realloc(std::size_t len);
134 
135  /* input stream */
136  wpi::raw_istream& m_is;
137 
138  /* temporary buffer */
139  char* m_buf;
140 
141  /* allocated size of temporary buffer */
142  std::size_t m_allocated;
143 };
144 
145 } // namespace nt
146 
147 #endif // NT_WIREDECODER_H_
Definition: Log.h:16
Definition: WireDecoder.h:27