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