00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028 #ifndef CANOPENBUS_HPP
00029 #define CANOPENBUS_HPP
00030
00031 #include "CANBusInterface.hpp"
00032 #include "CANControllerInterface.hpp"
00033 #include "CANMessage.hpp"
00034
00035 #include <list>
00036 #include <algorithm>
00037 #include <rtt/os/rtstreams.hpp>
00038
00039 namespace RTT
00040 {namespace CAN
00041 {
00042 using std::list;
00043 using std::find;
00044
00050 class CANOpenBus
00051 :public CANBusInterface
00052 {
00053 CANMessage syncMsg;
00054
00055 public:
00063 CANOpenBus()
00064 : controller( 0 )
00065 {
00066 syncMsg.setStdId( 0x80 );
00067 }
00068
00073 void sync()
00074 {
00075 this->write( &syncMsg );
00076 }
00077
00078 void setController(CANControllerInterface* contr)
00079 {
00080 controller = contr;
00081 }
00082
00083 virtual bool addDevice(CANDeviceInterface* dev)
00084 {
00085 if ( devices.size() < MAX_DEVICES)
00086 devices.push_back(dev);
00087 else
00088 return false;
00089 return true;
00090 }
00091
00092 virtual bool addListener(CANListenerInterface* dev)
00093 {
00094 if ( listeners.size() < MAX_DEVICES)
00095 listeners.push_back(dev);
00096 else
00097 return false;
00098 return true;
00099 }
00100
00101 virtual void removeDevice(CANDeviceInterface* dev)
00102 {
00103 list<CANDeviceInterface*>::iterator itl;
00104 itl = find(devices.begin(), devices.end(), dev);
00105 if ( itl != devices.end() )
00106 devices.erase(itl);
00107 }
00108
00109 virtual void removeListener(CANListenerInterface* dev)
00110 {
00111 list<CANListenerInterface*>::iterator itl;
00112 itl = find(listeners.begin(), listeners.end(), dev);
00113 if ( itl != listeners.end() )
00114 listeners.erase(itl);
00115 }
00116
00117 virtual void write(const CANMessage *msg)
00118 {
00119
00120
00121 list<CANListenerInterface*>::iterator itl = listeners.begin();
00122 while ( itl != listeners.end() )
00123 {
00124 list<CANListenerInterface*>::iterator next = (++itl)--;
00125 (*itl)->process(msg);
00126 itl = next;
00127 }
00128
00129 if ( controller == 0 )
00130 return;
00131 if ( msg->origin != controller )
00132 {
00133
00134 controller->process(msg);
00135 }
00136 else
00137 {
00138
00139 list<CANDeviceInterface*>::iterator itd = devices.begin();
00140 while ( itd != devices.end() )
00141 {
00142 list<CANDeviceInterface*>::iterator _next = (++itd)--;
00143 (*itd)->process(msg);
00144 itd = _next;
00145
00146 }
00147 }
00148 }
00149
00150 static const unsigned int MAX_DEVICES = 127;
00151 protected:
00152 list<CANDeviceInterface*> devices;
00153 list<CANListenerInterface*> listeners;
00154
00155 CANControllerInterface* controller;
00156 };
00157
00158 }}
00159
00160
00161 #endif
00162
00163