Message.h
Go to the documentation of this file.
1 
38 #ifndef SAWYER_MESSAGE_H
39 #define SAWYER_MESSAGE_H
40 
41 #include <iostream>
42 #include <cstdlib>
43 #include <stdint.h>
44 
46 #include <stdio.h>
47 
48 
49 namespace sawyer
50 {
51 
52  class MessageException : public Exception
53  {
54  public:
55  enum errors
56  {
59  };
60 
61  public:
62  enum errors type;
63 
64  MessageException(const char *msg, enum errors ex_type = ERROR_BASE);
65  };
66 
67  class Message
68  {
69  public:
70  static const size_t MAX_MSG_LENGTH = 256;
71 
72  uint8_t data[MAX_MSG_LENGTH];
73 
74  size_t total_len;
75 
76  protected:
77  static const size_t CRC_LENGTH = 2;
78  static const uint16_t CRC_INIT_VAL = 0xFFFF;
79 
80  static const size_t HEADER_LENGTH = 8;
81 
82  // Offsets of fields within data
84  {
85  SOH_OFST = 0,
89  FLAGS_OFST = 4,
90  STX_OFST = 5,
92  PAYLOAD_OFST=8
93  };
94 
95  // Whether this Message has ever been sent by the Transport()
96  // (Updated by Transport::send())
97  bool is_sent;
98 
99  friend class Transport; // Allow Transport to read data and total_len directly
100 
101  public:
102  static const size_t MIN_MSG_LENGTH = HEADER_LENGTH + CRC_LENGTH;
103  static const uint8_t SOH = 0xAA;
104  static const uint8_t STX = 0x55;
105 
106  protected:
107  size_t crcOffset()
108  {
109  return total_len - CRC_LENGTH;
110  };
111 
112  void setLength(uint8_t len);
113 
114  void setVersion(uint8_t version);
115 
116 
117  void setFlags(uint8_t flags);
118 
119  void setType(uint16_t type);
120 
121  uint8_t *getPayloadPointer(size_t offset = 0);
122 
123  void setPayload(void *buf, size_t buf_size);
124 
125  void setPayloadLength(uint8_t len);
126 
127  void makeValid();
128 
129  public:
130  Message();
131 
132  Message(void *input, size_t msg_len);
133 
134  Message(const Message &other);
135 
136  Message(uint16_t type, uint8_t *payload, size_t payload_len,
137  uint32_t timestamp = 0, uint8_t flags = 0, uint8_t version = 0);
138 
139 
140  virtual ~Message();
141 
142  void send();
143  Message * sendRequest();
144  uint8_t getLength(); // as reported by packet length field.
145  uint8_t getLengthComp();
146 
147  uint8_t getVersion();
148 
149  uint8_t getFlags();
150 
151  uint16_t getType();
152  size_t getType(void *buf, size_t max_size);
153 
154  uint16_t getChecksum();
155 
156  size_t getTypeLength()
157  {
158  return ((total_len - HEADER_LENGTH - CRC_LENGTH));
159  }
160 
162  {
163  return ((total_len - HEADER_LENGTH - CRC_LENGTH));
164  }
165 
166  size_t getPayload(void *buf, size_t max_size);
167 
168  size_t getTotalLength()
169  {
170  return total_len;
171  }
172 
173  size_t toBytes(void *buf, size_t buf_size);
174 
175  bool isValid(char *whyNot = NULL, size_t strLen = 0);
176 
177  bool isCommand()
178  {
179  return getType() < 0x4000;
180  }
181 
182  bool isRequest()
183  {
184  return (getType() >= 0x4000) && (getType() < 0x8000);
185  }
186 
187  bool isData()
188  {
189  return (getType() >= 0x8000) && (getType() < 0xC000);
190  }
191 
192  virtual std::ostream &printMessage(std::ostream &stream = std::cout);
193 
194 
195 
196  void printRaw(std::ostream &stream = std::cout);
197 
198 
199  static Message *factory(void *input, size_t msg_len);
200 
201  static Message *popNext();
202 
203  static Message *waitNext(double timeout = 0.0);
204 
205  }; // class Message
206 
208  {
209  /*
210  * Set commands
211  */
220  SET_TURN_SETPT = 0x0205,
221  SET_WHEEL_INFO = 0x0206,
222  SET_MAX_SPEED = 0x0210,
223  SET_MAX_ACCEL = 0x0211,
227  SET_GPIO_OUTPUT = 0x0302,
229  SET_XYZ_DATA = 0x2607,
230 
231  /*
232  * Command commands
233  */
237 
238  /*
239  * Request commands
240  */
241  REQUEST_ECHO = 0x4000,
264  REQUEST_ORIENT = 0x4600,
266  REQUEST_ACCEL = 0x4602,
267  REQUEST_6AXIS = 0x4603,
271  REQUEST_ENCODER = 0x4800,
273 
274  /*
275  * Data commands
276  */
277  DATA_ECHO = 0x8000,
290  DATA_TURN_SETPT = 0x8205,
291  DATA_WHEEL_INFO = 0x8206,
292  DATA_MAX_SPEED = 0x8210,
293  DATA_MAX_ACCEL = 0x8211,
294  DATA_GEAR_SETPT = 0x8212,
301  DATA_ORIENT = 0x8600,
302  DATA_ROT_RATE = 0x8601,
303  DATA_ACCEL = 0x8602,
304  DATA_6AXIS = 0x8603,
306  DATA_6AXIS_YAW = 0x8605,
308  DATA_XYZ_DATA = 0x8607,
309  DATA_ENCODER = 0x8800,
314  DATA_ORIENT_RAW = 0xA113,
315  DATA_GYRO_RAW = 0xA114,
316  DATA_ACCEL_RAW = 0xA115,
318  }; // enum MessageTypes
319 
320 }; // namespace sawyer
321 
322 std::ostream &operator<<(std::ostream &stream, sawyer::Message &msg);
323 
324 #endif // sawyer_MESSAGE_H
MessageException(const char *msg, enum errors ex_type=ERROR_BASE)
Definition: Message.cpp:69
bool isCommand()
Definition: Message.h:177
std::ostream & operator<<(std::ostream &stream, sawyer::Message &msg)
Definition: Message.cpp:544
bool isRequest()
Definition: Message.h:182
size_t total_len
Definition: Message.h:74
MessageTypes
Definition: Message.h:207
size_t crcOffset()
Definition: Message.h:107
data
size_t getPayloadLength()
Definition: Message.h:161
bool is_sent
Definition: Message.h:97
bool isData()
Definition: Message.h:187
size_t getTypeLength()
Definition: Message.h:156
enum errors type
Definition: Message.h:62
bool isValid(const trajectory_msgs::JointTrajectoryPoint &point, const unsigned int joint_dim)
size_t getTotalLength()
Definition: Message.h:168


roch_base
Author(s): Mike Purvis , Paul Bovbel , Chen
autogenerated on Mon Jun 10 2019 14:41:13