00001
00038 #ifndef SAWYER_MESSAGE_H
00039 #define SAWYER_MESSAGE_H
00040
00041 #include <iostream>
00042 #include <cstdlib>
00043 #include <stdint.h>
00044
00045 #include "roch_base/core/Exception.h"
00046 #include <stdio.h>
00047
00048
00049 namespace sawyer
00050 {
00051
00052 class MessageException : public Exception
00053 {
00054 public:
00055 enum errors
00056 {
00057 ERROR_BASE = 0,
00058 INVALID_LENGTH
00059 };
00060
00061 public:
00062 enum errors type;
00063
00064 MessageException(const char *msg, enum errors ex_type = ERROR_BASE);
00065 };
00066
00067 class Message
00068 {
00069 public:
00070 static const size_t MAX_MSG_LENGTH = 256;
00071
00072 uint8_t data[MAX_MSG_LENGTH];
00073
00074 size_t total_len;
00075
00076 protected:
00077 static const size_t CRC_LENGTH = 2;
00078 static const uint16_t CRC_INIT_VAL = 0xFFFF;
00079
00080 static const size_t HEADER_LENGTH = 8;
00081
00082
00083 enum dataOffsets
00084 {
00085 SOH_OFST = 0,
00086 LENGTH_OFST,
00087 LENGTH_COMP_OFST,
00088 VERSION_OFST,
00089 FLAGS_OFST = 4,
00090 STX_OFST = 5,
00091 TYPE_OFST,
00092 PAYLOAD_OFST=8
00093 };
00094
00095
00096
00097 bool is_sent;
00098
00099 friend class Transport;
00100
00101 public:
00102 static const size_t MIN_MSG_LENGTH = HEADER_LENGTH + CRC_LENGTH;
00103 static const uint8_t SOH = 0xAA;
00104 static const uint8_t STX = 0x55;
00105
00106 protected:
00107 size_t crcOffset()
00108 {
00109 return total_len - CRC_LENGTH;
00110 };
00111
00112 void setLength(uint8_t len);
00113
00114 void setVersion(uint8_t version);
00115
00116
00117 void setFlags(uint8_t flags);
00118
00119 void setType(uint16_t type);
00120
00121 uint8_t *getPayloadPointer(size_t offset = 0);
00122
00123 void setPayload(void *buf, size_t buf_size);
00124
00125 void setPayloadLength(uint8_t len);
00126
00127 void makeValid();
00128
00129 public:
00130 Message();
00131
00132 Message(void *input, size_t msg_len);
00133
00134 Message(const Message &other);
00135
00136 Message(uint16_t type, uint8_t *payload, size_t payload_len,
00137 uint32_t timestamp = 0, uint8_t flags = 0, uint8_t version = 0);
00138
00139
00140 virtual ~Message();
00141
00142 void send();
00143 Message * sendRequest();
00144 uint8_t getLength();
00145 uint8_t getLengthComp();
00146
00147 uint8_t getVersion();
00148
00149 uint8_t getFlags();
00150
00151 uint16_t getType();
00152 size_t getType(void *buf, size_t max_size);
00153
00154 uint16_t getChecksum();
00155
00156 size_t getTypeLength()
00157 {
00158 return ((total_len - HEADER_LENGTH - CRC_LENGTH));
00159 }
00160
00161 size_t getPayloadLength()
00162 {
00163 return ((total_len - HEADER_LENGTH - CRC_LENGTH));
00164 }
00165
00166 size_t getPayload(void *buf, size_t max_size);
00167
00168 size_t getTotalLength()
00169 {
00170 return total_len;
00171 }
00172
00173 size_t toBytes(void *buf, size_t buf_size);
00174
00175 bool isValid(char *whyNot = NULL, size_t strLen = 0);
00176
00177 bool isCommand()
00178 {
00179 return getType() < 0x4000;
00180 }
00181
00182 bool isRequest()
00183 {
00184 return (getType() >= 0x4000) && (getType() < 0x8000);
00185 }
00186
00187 bool isData()
00188 {
00189 return (getType() >= 0x8000) && (getType() < 0xC000);
00190 }
00191
00192 virtual std::ostream &printMessage(std::ostream &stream = std::cout);
00193
00194
00195
00196 void printRaw(std::ostream &stream = std::cout);
00197
00198
00199 static Message *factory(void *input, size_t msg_len);
00200
00201 static Message *popNext();
00202
00203 static Message *waitNext(double timeout = 0.0);
00204
00205 };
00206
00207 enum MessageTypes
00208 {
00209
00210
00211
00212 SET_PLATFORM_NAME = 0x0002,
00213 SET_PLATFORM_TIME = 0x0005,
00214 SET_SAFETY_SYSTEM = 0x0010,
00215 SET_DIFF_WHEEL_SPEEDS = 0x0200,
00216 SET_DIFF_CTRL_CONSTS = 0x0201,
00217 SET_DIFF_WHEEL_SETPTS = 0x0202,
00218 SET_ACKERMANN_SETPT = 0x0203,
00219 SET_VELOCITY_SETPT = 0x0204,
00220 SET_TURN_SETPT = 0x0205,
00221 SET_WHEEL_INFO = 0x0206,
00222 SET_MAX_SPEED = 0x0210,
00223 SET_MAX_ACCEL = 0x0211,
00224 SET_GEAR_SETPOINT = 0x0212,
00225 SET_GPADC_OUTPUT = 0x0300,
00226 SET_GPIO_DIRECTION = 0x0301,
00227 SET_GPIO_OUTPUT = 0x0302,
00228 SET_PTZ_POSITION = 0x0400,
00229 SET_XYZ_DATA = 0x2607,
00230
00231
00232
00233
00234 CMD_PROCESSOR_RESET = 0x2000,
00235 CMD_RESTORE_SETTINGS = 0x2001,
00236 CMD_STORE_SETTINGS = 0x2002,
00237
00238
00239
00240
00241 REQUEST_ECHO = 0x4000,
00242 REQUEST_PLATFORM_INFO = 0x4001,
00243 REQUEST_PLATFORM_NAME = 0x4002,
00244 REQUEST_FIRMWARE_INFO = 0x4003,
00245 REQUEST_SYSTEM_STATUS = 0x4004,
00246 REQUEST_POWER_SYSTEM = 0x4005,
00247 REQUEST_SAFETY_SYSTEM = 0x4010,
00248 REQUEST_DIFF_WHEEL_SPEEDS = 0x4200,
00249 REQUEST_DIFF_CTRL_CONSTS = 0x4201,
00250 REQUEST_DIFF_WHEEL_SETPTS = 0x4202,
00251 REQUEST_ACKERMANN_SETPTS = 0x4203,
00252 REQUEST_VELOCITY_SETPT = 0x4204,
00253 REQUEST_TURN_SETPT = 0x4205,
00254 REQUEST_WHEEL_INFO = 0x4206,
00255 REQUEST_MAX_SPEED = 0x4210,
00256 REQUEST_MAX_ACCEL = 0x4211,
00257 REQUEST_GEAR_SETPT = 0x4212,
00258 REQUEST_GPADC_OUTPUT = 0x4300,
00259 REQUEST_GPIO_STATUS = 0x4301,
00260 REQUEST_GPADC_INPUT = 0x4303,
00261 REQUEST_PTZ_POSITION = 0x4400,
00262 REQUEST_DISTANCE_DATA = 0x4500,
00263 REQUEST_DISTANCE_TIMING = 0x4501,
00264 REQUEST_ORIENT = 0x4600,
00265 REQUEST_ROT_RATE = 0x4601,
00266 REQUEST_ACCEL = 0x4602,
00267 REQUEST_6AXIS = 0x4603,
00268 REQUEST_6AXIS_ORIENT = 0x4604,
00269 REQUEST_6AXIS_YAW = 0x4605,
00270 REQUEST_XYZ_DATA = 0x4607,
00271 REQUEST_ENCODER = 0x4800,
00272 REQUEST_ENCODER_RAW = 0x4801,
00273
00274
00275
00276
00277 DATA_ECHO = 0x8000,
00278 DATA_PLATFORM_INFO = 0x8001,
00279 DATA_PLATFORM_NAME = 0x8002,
00280 DATA_FIRMWARE_INFO = 0x8003,
00281 DATA_SYSTEM_STATUS = 0x8004,
00282 DATA_POWER_SYSTEM = 0x8005,
00283 DATA_PROC_STATUS = 0x8006,
00284 DATA_SAFETY_SYSTEM = 0x8010,
00285 DATA_DIFF_WHEEL_SPEEDS = 0x8200,
00286 DATA_DIFF_CTRL_CONSTS = 0x8201,
00287 DATA_DIFF_WHEEL_SETPTS = 0x8202,
00288 DATA_ACKERMANN_SETPTS = 0x8203,
00289 DATA_VELOCITY_SETPT = 0x8204,
00290 DATA_TURN_SETPT = 0x8205,
00291 DATA_WHEEL_INFO = 0x8206,
00292 DATA_MAX_SPEED = 0x8210,
00293 DATA_MAX_ACCEL = 0x8211,
00294 DATA_GEAR_SETPT = 0x8212,
00295 DATA_GPADC_OUTPUT = 0x8300,
00296 DATA_GPIO_STATUS = 0x8301,
00297 DATA_GPADC_INPUT = 0x8303,
00298 DATA_PTZ_POSITION = 0x8400,
00299 DATA_DISTANCE_DATA = 0x8500,
00300 DATA_DISTANCE_TIMING = 0x8501,
00301 DATA_ORIENT = 0x8600,
00302 DATA_ROT_RATE = 0x8601,
00303 DATA_ACCEL = 0x8602,
00304 DATA_6AXIS = 0x8603,
00305 DATA_6AXIS_ORIENT = 0x8604,
00306 DATA_6AXIS_YAW = 0x8605,
00307 DATA_MAGNETOMETER = 0x8606,
00308 DATA_XYZ_DATA = 0x8607,
00309 DATA_ENCODER = 0x8800,
00310 DATA_ENCODER_RAW = 0x8801,
00311 DATA_CURRENT_RAW = 0xA110,
00312 DATA_VOLTAGE_RAW = 0xA111,
00313 DATA_TEMPERATURE_RAW = 0xA112,
00314 DATA_ORIENT_RAW = 0xA113,
00315 DATA_GYRO_RAW = 0xA114,
00316 DATA_ACCEL_RAW = 0xA115,
00317 DATA_MAGNETOMETER_RAW = 0xA116
00318 };
00319
00320 };
00321
00322 std::ostream &operator<<(std::ostream &stream, sawyer::Message &msg);
00323
00324 #endif // sawyer_MESSAGE_H