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 CANREQUEST_HPP
00029 #define CANREQUEST_HPP
00030
00031 #include "CANMessage.hpp"
00032 #include "CANBusInterface.hpp"
00033
00034 #include <rtt/TimeService.hpp>
00035 #include <rtt/Time.hpp>
00036 #include <rtt/os/rtstreams.hpp>
00037
00038 namespace RTT
00039 {namespace CAN
00040 {
00041
00042
00043
00052 class CANRequest
00053 :public CANDeviceInterface
00054 {
00055 public:
00060 CANRequest(CANMessage* _write, CANMessage* _expect, Seconds _timeout = 0 )
00061 : bus(0), write(_write), expected(_expect), result(0), timestamp(0), timeout(_timeout)
00062 {
00063 }
00064
00068 bool sendTo(CANBusInterface* _bus)
00069 {
00070 if ( bus != 0 )
00071 return false;
00072
00073 _bus->addDevice(this);
00074 bus = _bus;
00075 result = 0;
00076 bus->write(write);
00077 timestamp = TimeService::Instance()->getTicks();
00078 return true;
00079 }
00080
00081 void process(const CANMessage* msg)
00082 {
00083 if (result != 0)
00084 return;
00085
00086 if ( (msg->isStandard() == expected->isStandard() && msg->getStdId() == expected->getStdId() )
00087 || ( msg->isExtended() == expected->isExtended() && msg->getExtId() == expected->getExtId() ) )
00088 {
00089 result_cache = *msg;
00090 result = &result_cache;
00091 abort();
00092 } else if ( isExpired() )
00093 {
00094 abort();
00095 }
00096 }
00097
00098 unsigned int nodeId() const
00099 {
00100 return write->origin->nodeId();
00101 }
00102
00106 void abort()
00107 {
00108 if (bus != 0)
00109 {
00110 bus->removeDevice(this);
00111 bus = 0;
00112 }
00113 }
00114
00118 bool isReceived() const
00119 {
00120 return result != 0;
00121 }
00122
00126 bool isExpired() const
00127 {
00128 if ( timeout == 0 )
00129 return false;
00130 else
00131 return TimeService::Instance()->secondsSince(timestamp) > timeout;
00132 }
00133
00141 bool isExactMatch() const
00142 {
00143 if (result != 0)
00144 return *expected == *result;
00145 else
00146 return false;
00147 }
00148
00152 bool matchDataByte(unsigned int pos) const
00153 {
00154 if (result != 0 && pos < expected->getDLC() && pos < result->getDLC() )
00155 return expected->getData(pos) == result->getData(pos);
00156 return false;
00157 }
00158
00164 bool matchDataByte(unsigned int start, unsigned int end) const
00165 {
00166 if (result != 0 && start <= end && end < expected->getDLC() && end < result->getDLC() )
00167 {
00168 for (unsigned int i=start; i <= end; ++i)
00169 if ( expected->getData(i) != result->getData(i) )
00170 return false;
00171 return true;
00172 }
00173 return false;
00174 }
00175
00179 CANMessage* getReceivedMessage() { return result; }
00180
00181 protected:
00182 CANBusInterface* bus;
00183 CANMessage* write;
00184 CANMessage* expected;
00185 CANMessage* result;
00186 CANMessage result_cache;
00187 TimeService::ticks timestamp;
00188 Seconds timeout;
00189 };
00190
00191 }}
00192
00193 #endif