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 CANMESSAGE_HPP
00029 #define CANMESSAGE_HPP
00030
00031 #include "cpstruct.h"
00032 #include "cpmacro.h"
00033 #include "CANDeviceInterface.hpp"
00034 #include <string.h>
00035
00036 #define fillData(data, d0, d1, d2 ,d3, d4, d5, d6, d7) do { \
00037 data[0] = d0; data[1] = d1; data[2] = d2; data[3] = d3; \
00038 data[4] = d4; data[5] = d5; data[6] = d6; data[7] = d7; } while(0)
00039
00040 namespace RTT
00041 {
00042 namespace CAN
00043 {
00047 struct CANDummyDevice: public CANDeviceInterface
00048 {
00049 virtual void process(const CANMessage* msg)
00050 {
00051 return;
00052 }
00053 };
00054
00055 typedef ::CpStruct_CAN CANBase;
00056
00057 inline unsigned int lower_u32(const unsigned char* data)
00058 {
00059 return *(unsigned int*) (data);
00060 }
00061
00062 inline unsigned int higher_u32(const unsigned char* data)
00063 {
00064 return *(unsigned int*) (data + 4);
00065 }
00066
00070 struct CANMessage: public CANBase
00071 {
00075 typedef unsigned int ID;
00079 typedef unsigned int Flags;
00083 typedef unsigned char Data;
00087 typedef double Seconds;
00088
00089
00094 CANMessage() :
00095 origin(&candevice_dummy)
00096 {
00097 clear();
00098 }
00099
00103 CANMessage(CANDeviceInterface* _origin) :
00104 origin(_origin)
00105 {
00106 clear();
00107 }
00108
00118 CANMessage(CANDeviceInterface* _origin, ID _msgid, Data* _data,
00119 unsigned int _length) :
00120 origin(_origin)
00121 {
00122 clear();
00123 setDLC(_length);
00124 setStdId(_msgid);
00125 for (unsigned int i = 0; _data != 0 && i < _length; ++i)
00126 setData(i, _data[i]);
00127 }
00128
00136 static CANMessage* createExtended(CANDeviceInterface* _origin,
00137 ID _msgid, Data* _data, unsigned int _length)
00138 {
00139 CANMessage* cm =
00140 new CANMessage(_origin, _msgid, _data, _length);
00141 cm->setExtId(_msgid);
00142 return cm;
00143 }
00144
00152 static CANMessage* createStandard(CANDeviceInterface* _origin,
00153 ID _msgid, Data* _data, unsigned int _length)
00154 {
00155 return new CANMessage(_origin, _msgid, _data, _length);
00156 }
00157
00165 static CANMessage* createStdRemote(CANDeviceInterface* _origin,
00166 ID _msgid, Data* _data, unsigned int _length)
00167 {
00168 CANMessage* cm =
00169 new CANMessage(_origin, _msgid, _data, _length);
00170 cm->setRemote();
00171 return cm;
00172 }
00173
00181 static CANMessage* createExtRemote(CANDeviceInterface* _origin,
00182 ID _msgid, Data* _data, unsigned int _length)
00183 {
00184 CANMessage* cm =
00185 new CANMessage(_origin, _msgid, _data, _length);
00186 cm->setExtId(_msgid);
00187 cm->setRemote();
00188 return cm;
00189 }
00190
00194 void clear()
00195 {
00196 CpMacMsgClear(this);
00197 }
00198
00202 bool isRemote() const
00203 {
00204 return CpMacIsRemote(this);
00205 }
00206
00210 void setRemote()
00211 {
00212 CpMacSetRemote(this);
00213 }
00214
00218 Data getData(unsigned int pos) const
00219 {
00220 return CpMacGetData(this,pos);
00221 }
00222
00226 void setData(unsigned int pos, Data d)
00227 {
00228 CpMacSetData(this,pos,d);
00229 }
00230
00234 void setDataDLC(Data* _data, unsigned int _length)
00235 {
00236 CpMacSetDlc(this, _length);
00237 for (unsigned int i = 0; _data != 0 && i < _length; ++i)
00238 setData(i, _data[i]);
00239 }
00240
00244 unsigned int getDLC() const
00245 {
00246 return CpMacGetDlc(this);
00247 }
00248
00252 void setDLC(unsigned int length)
00253 {
00254 CpMacSetDlc(this,length);
00255 }
00256
00257 bool isExtended() const
00258 {
00259 return CpMacIsExtended(this);
00260 }
00261
00262 bool isStandard() const
00263 {
00264 return !isExtended();
00265 }
00266
00267 unsigned int getStdId() const
00268 {
00269 return CpMacGetStdId(this);
00270 }
00271
00272 unsigned int getExtId() const
00273 {
00274 return CpMacGetExtId(this);
00275 }
00276
00277 void setStdId(unsigned int id)
00278 {
00279 CpMacSetStdId(this,id);
00280 }
00281
00282 void setExtId(unsigned int id)
00283 {
00284 CpMacSetExtId(this,id);
00285 }
00286
00291 bool operator==(CANMessage& other) const
00292 {
00293 if (isStandard() == other.isStandard() && (getStdId()
00294 == other.getStdId()) && (getDLC() == other.getDLC()))
00295 {
00296 for (unsigned int i = 0; i < getDLC(); ++i)
00297 if (getData(i) != other.getData(i))
00298 return false;
00299 return true;
00300 }
00301
00302 if (isExtended() == other.isExtended() && (getExtId()
00303 == other.getExtId()) && (getDLC() == other.getDLC()))
00304 {
00305 for (unsigned int i = 0; i < getDLC(); ++i)
00306 if (getData(i) != other.getData(i))
00307 return false;
00308 return true;
00309 }
00310 return false;
00311 }
00312
00317 CANMessage& operator=(const CpStruct_CAN &msg)
00318 {
00319 v_MsgId = msg.v_MsgId;
00320 v_MsgFlags = msg.v_MsgFlags;
00321 memcpy((void*) (v_MsgData), (void*) (msg.v_MsgData), 8);
00322 return *this;
00323 }
00324
00328 CANDeviceInterface *origin;
00332 Seconds timestamp;
00333 private:
00337 static CANDummyDevice candevice_dummy;
00338 };
00339
00340 }
00341 }
00342
00343 #endif