Libbarrett
1.2.4
|
00001 00033 #ifndef BARRETT_BUS_BUS_MANAGER_H_ 00034 #define BARRETT_BUS_BUS_MANAGER_H_ 00035 00036 00037 #include <map> 00038 #include <cstring> 00039 00040 #include <boost/circular_buffer.hpp> 00041 00042 #include <barrett/detail/ca_macro.h> 00043 #include <barrett/thread/abstract/mutex.h> 00044 #include <barrett/bus/abstract/communications_bus.h> 00045 00046 00047 namespace barrett { 00048 namespace bus { 00049 00050 00051 class BusManager : public CommunicationsBus { 00052 public: 00055 BusManager(CommunicationsBus* bus = NULL); 00056 BusManager(int port); 00057 virtual ~BusManager(); 00060 const CommunicationsBus& getUnderlyingBus() const { return *bus; } 00063 virtual thread::Mutex& getMutex() const { return bus->getMutex(); } 00066 virtual void open(int port) { bus->open(port); } 00069 virtual void close() { bus->close(); } 00072 virtual bool isOpen() const { return bus->isOpen(); } 00075 virtual int send(int busId, const unsigned char* data, size_t len) const 00076 { return bus->send(busId, data, len); } 00079 virtual int receive(int expectedBusId, unsigned char* data, size_t& len, 00080 bool blocking = true, bool realtime = false) const; 00083 virtual int receiveRaw(int& busId, unsigned char* data, size_t& len, 00084 bool blocking = true) const 00085 { return bus->receiveRaw(busId, data, len, blocking); } 00086 00087 protected: 00088 int updateBuffers() const; 00089 void storeMessage(int busId, const unsigned char* data, size_t len) const; 00090 bool retrieveMessage(int busId, unsigned char* data, size_t& len) const; 00091 00092 CommunicationsBus* bus; 00093 bool deleteBus; 00094 00095 private: 00096 struct Message { 00097 Message(const unsigned char* d, size_t l) : 00098 len(l) 00099 { 00100 memcpy(data, d, len); 00101 } 00102 00103 void copyTo(unsigned char* d, size_t& l) { 00104 l = len; 00105 memcpy(d, data, len); 00106 } 00107 00108 unsigned char data[CommunicationsBus::MAX_MESSAGE_LEN]; 00109 size_t len; 00110 }; 00111 00112 static const size_t MESSAGE_BUFFER_SIZE = 10; 00113 class MessageBuffer : public boost::circular_buffer<Message> { 00114 public: 00115 MessageBuffer() : 00116 boost::circular_buffer<Message>(MESSAGE_BUFFER_SIZE) {} 00117 }; 00118 00119 mutable std::map<int, MessageBuffer> messageBuffers; 00120 00121 DISALLOW_COPY_AND_ASSIGN(BusManager); 00122 }; 00123 00124 00125 } 00126 } 00127 00128 00129 #endif /* BARRETT_BUS_BUS_MANAGER_H_ */