Go to the documentation of this file.00001
00048 #ifndef CLEARPATH_TRANSPORT_H
00049 #define CLEARPATH_TRANSPORT_H
00050
00051 #include <list>
00052 #include <iostream>
00053
00054 #include "husky_base/horizon_legacy/Message.h"
00055 #include "husky_base/horizon_legacy/Exception.h"
00056
00057 namespace clearpath
00058 {
00059
00060 class TransportException : public Exception
00061 {
00062 public:
00063 enum errors
00064 {
00065 ERROR_BASE,
00066 NOT_CONFIGURED,
00067 CONFIGURE_FAIL,
00068 UNACKNOWLEDGED_SEND,
00069 BAD_ACK_RESULT
00070 };
00071 public:
00072 enum errors type;
00073
00074 TransportException(const char *msg, enum errors ex_type = ERROR_BASE);
00075 };
00076
00077 class BadAckException : public TransportException
00078 {
00079 public:
00080 enum ackFlags
00081 {
00082 BAD_CHECKSUM = 0x01,
00083 BAD_TYPE = 0x02,
00084 BAD_FORMAT = 0x04,
00085 RANGE = 0x08,
00086 NO_BANDWIDTH = 0x10,
00087 OVER_FREQ = 0x20,
00088 OVER_SUBSCRIBE = 0x40
00089 } ack_flag;
00090
00091 BadAckException(unsigned int flag);
00092 };
00093
00094
00095
00096
00097 class Transport
00098 {
00099 public:
00100 enum counterTypes
00101 {
00102 GARBLE_BYTES,
00103 INVALID_MSG,
00104 IGNORED_ACK,
00105 QUEUE_FULL,
00106 NUM_COUNTERS
00107 };
00108 static const char *counter_names[NUM_COUNTERS];
00109
00110
00111 private:
00112 bool configured;
00113 void *serial;
00114 int retries;
00115
00116 static const int RETRY_DELAY_MS = 200;
00117
00118 std::list<Message *> rx_queue;
00119 static const size_t MAX_QUEUE_LEN = 10000;
00120
00121 unsigned long counters[NUM_COUNTERS];
00122
00123 private:
00124 Message *rxMessage();
00125
00126 Message *getAck();
00127
00128 void enqueueMessage(Message *msg);
00129
00130 int openComm(const char *device);
00131
00132 int closeComm();
00133
00134 void resetCounters();
00135
00136 protected:
00137 Transport();
00138
00139 ~Transport();
00140
00141 public:
00142 static Transport &instance();
00143
00144 void configure(const char *device, int retries);
00145
00146 bool isConfigured()
00147 {
00148 return configured;
00149 }
00150
00151 int close();
00152
00153 void poll();
00154
00155 void send(Message *m);
00156
00157 Message *popNext();
00158
00159 Message *popNext(enum MessageTypes type);
00160
00161 Message *waitNext(double timeout = 0.0);
00162
00163 Message *waitNext(enum MessageTypes type, double timeout = 0.0);
00164
00165 void flush(std::list<Message *> *queue = 0);
00166
00167 void flush(enum MessageTypes type, std::list<Message *> *queue = 0);
00168
00169 unsigned long getCounter(enum counterTypes counter)
00170 {
00171 return counters[counter];
00172 }
00173
00174 void printCounters(std::ostream &stream = std::cout);
00175 };
00176
00177 };
00178
00179 #endif // CLEARPATH_TRANSPORT_H
00180