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 {
00040 namespace CAN
00041 {
00042
00051 class CANRequest: public CANDeviceInterface
00052 {
00053 public:
00058 CANRequest(CANMessage* _write, CANMessage* _expect,
00059 Seconds _timeout = 0) :
00060 bus(0), write(_write), expected(_expect), result(0), timestamp(
00061 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()
00087 && msg->getStdId() == expected->getStdId())
00088 || (msg->isExtended() == expected->isExtended()
00089 && msg->getExtId() == expected->getExtId()))
00090 {
00091 result_cache = *msg;
00092 result = &result_cache;
00093 abort();
00094 }
00095 else if (isExpired())
00096 {
00097 abort();
00098 }
00099 }
00100
00104 void abort()
00105 {
00106 if (bus != 0)
00107 {
00108 bus->removeDevice(this);
00109 bus = 0;
00110 }
00111 }
00112
00116 bool isReceived() const
00117 {
00118 return result != 0;
00119 }
00120
00124 bool isExpired() const
00125 {
00126 if (timeout == 0)
00127 return false;
00128 else
00129 return TimeService::Instance()->secondsSince(timestamp)
00130 > timeout;
00131 }
00132
00140 bool isExactMatch() const
00141 {
00142 if (result != 0)
00143 return *expected == *result;
00144 else
00145 return false;
00146 }
00147
00151 bool matchDataByte(unsigned int pos) const
00152 {
00153 if (result != 0 && pos < expected->getDLC() && pos
00154 < 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()
00167 && end < result->getDLC())
00168 {
00169 for (unsigned int i = start; i <= end; ++i)
00170 if (expected->getData(i) != result->getData(i))
00171 return false;
00172 return true;
00173 }
00174 return false;
00175 }
00176
00180 CANMessage* getReceivedMessage()
00181 {
00182 return result;
00183 }
00184
00185 protected:
00186 CANBusInterface* bus;
00187 CANMessage* write;
00188 CANMessage* expected;
00189 CANMessage* result;
00190 CANMessage result_cache;
00191 TimeService::ticks timestamp;
00192 Seconds timeout;
00193 };
00194
00195 }
00196 }
00197
00198 #endif