YARP
Yet Another Robot Platform
CanBusInterface.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2006-2020 Istituto Italiano di Tecnologia (IIT)
3  * Copyright (C) 2006-2010 RobotCub Consortium
4  * All rights reserved.
5  *
6  * This software may be modified and distributed under the terms of the
7  * BSD-3-Clause license. See the accompanying LICENSE file for details.
8  */
9 
10 #ifndef YARP_DEV_CANBUSINTERFACE_H
11 #define YARP_DEV_CANBUSINTERFACE_H
12 
13 #include <cstring> // for std::memset
14 
15 #include <yarp/dev/api.h>
16 #include <yarp/os/Log.h>
17 
20 namespace yarp {
21  namespace dev {
22  class ICanBus;
23  class ICanBufferFactory;
24  class ICanBusErrors;
25  class CanMessage;
26  class CanBuffer;
27  class CanErrors;
28  template <class M, class T> class ImplementCanBufferFactory;
29  }
30 }
31 
33 {
34 public:
36  {
37  txCanErrors=0; //can device tx errors
38  rxCanErrors=0; //can device rx errors
39  busoff=false; //bus off
40  rxCanFifoOvr=0; // can device rx overflow
41  txCanFifoOvr=0; // can device tx overflow
42  txBufferOvr=0; // tx buffer overflow
43  rxBufferOvr=0; // rx buffer overflow
44  }
45 
46  int txCanErrors; //can device tx errors
47  int rxCanErrors; //can device rx errors
48  bool busoff; //bus off
49  unsigned int rxCanFifoOvr; // can device rx overflow
50  unsigned int txCanFifoOvr; // can device tx overflow
51  unsigned int txBufferOvr; // tx buffer overflow
52  unsigned int rxBufferOvr; // rx buffer overflow
53 };
54 
56 {
57  public:
58  virtual ~CanMessage(){}
59  virtual CanMessage &operator=(const CanMessage &l)=0;
60  virtual unsigned int getId() const =0;
61  virtual unsigned char getLen() const =0;
62  virtual void setLen(unsigned char len) =0;
63  virtual void setId(unsigned int id) =0;
64  virtual const unsigned char *getData() const =0;
65  virtual unsigned char *getData() =0;
66  virtual unsigned char *getPointer()=0;
67  virtual const unsigned char *getPointer() const =0;
68  virtual void setBuffer(unsigned char *)=0;
69 };
70 
72 {
73  yarp::dev::CanMessage **data;
74  int size;
75 
76 public:
77  CanBuffer();
78 
79  void resize(CanMessage **d, int s)
80  {
81  size=s;
82  data=d;
83  }
84 
86  {
87  return data;
88  }
89 
90  const CanMessage *const *getPointer() const
91  {
92  return data;
93  }
94 
96  {
97  return *data[k];
98  }
99  const CanMessage &operator[](int k) const
100  {
101  return *data[k];
102  }
103 };
104 
106 {
107 public:
108  virtual ~ICanBufferFactory(){}
109  virtual CanBuffer createBuffer(int nmessage)=0;
110  virtual void destroyBuffer(CanBuffer &msgs)=0;
111 };
112 
118 template<class M, class IMPL>
120 {
121 public:
123 
124  virtual CanBuffer createBuffer(int elem)
125  {
126  CanBuffer ret;
127  IMPL *storage=new IMPL[elem];
128  CanMessage **messages=new CanMessage *[elem];
129  M *tmp=new M[elem];
130 
131  std::memset(storage, 0, sizeof(IMPL)*elem);
132 
133  for(int k=0;k<elem;k++)
134  {
135  messages[k]=&tmp[k];
136  messages[k]->setBuffer((unsigned char *)(&storage[k]));
137  }
138 
139  ret.resize(messages, elem);
140  return ret;
141  }
142 
144  {
145  CanMessage **m=buffer.getPointer();
146  IMPL *storage=0;
147  M *msgs=0;
148 
149  if (m==0)
150  {
151  yError("Warning trying to destroy non valid buffer\n");
152  return;
153  }
154 
155  storage=reinterpret_cast<IMPL *>(m[0]->getPointer());
156  msgs=dynamic_cast<M *>(m[0]);
157 
158  if ((msgs==0)||(storage==0))
159  {
160  yError("Warning, troubles destroying memory\n");
161  return;
162  }
163 
164  delete [] storage;
165  delete [] msgs;
166  delete [] m;
167  }
168 };
169 
174 {
175  public:
176  virtual ~ICanBus(){}
177 
178 
179  virtual bool canSetBaudRate(unsigned int rate)=0;
180  virtual bool canGetBaudRate(unsigned int *rate)=0;
181  virtual bool canIdAdd(unsigned int id)=0;
182  virtual bool canIdDelete(unsigned int id)=0;
183 
184  virtual bool canRead(CanBuffer &msgs,
185  unsigned int size,
186  unsigned int *read,
187  bool wait=false)=0;
188 
189  virtual bool canWrite(const CanBuffer &msgs,
190  unsigned int size,
191  unsigned int *sent,
192  bool wait=false)=0;
193 };
194 
196 {
197 public:
198  virtual bool canGetErrors(yarp::dev::CanErrors &err)=0;
199 
200  virtual ~ICanBusErrors(){}
201 };
202 
203 #endif // YARP_DEV_CANBUSINTERFACE_H
yarp::dev::CanMessage::setBuffer
virtual void setBuffer(unsigned char *)=0
yarp::dev::ICanBusErrors::~ICanBusErrors
virtual ~ICanBusErrors()
Definition: CanBusInterface.h:200
yarp::sig::file::read
bool read(ImageOf< PixelRgb > &dest, const std::string &src, image_fileformat format=FORMAT_ANY)
Definition: ImageFile.cpp:827
yarp::dev::ICanBus::canWrite
virtual bool canWrite(const CanBuffer &msgs, unsigned int size, unsigned int *sent, bool wait=false)=0
yarp::dev::CanErrors
Definition: CanBusInterface.h:33
yarp::dev::CanMessage::getId
virtual unsigned int getId() const =0
yarp::dev::CanErrors::txCanFifoOvr
unsigned int txCanFifoOvr
Definition: CanBusInterface.h:50
yarp::dev::CanBuffer::resize
void resize(CanMessage **d, int s)
Definition: CanBusInterface.h:79
yarp::dev::ImplementCanBufferFactory::destroyBuffer
virtual void destroyBuffer(CanBuffer &buffer)
Definition: CanBusInterface.h:143
yarp::dev::ICanBus::~ICanBus
virtual ~ICanBus()
Definition: CanBusInterface.h:176
yarp::dev::ICanBus
Interface for a can bus device.
Definition: CanBusInterface.h:174
yarp::dev::CanBuffer::operator[]
CanMessage & operator[](int k)
Definition: CanBusInterface.h:95
yarp::dev::CanErrors::txCanErrors
int txCanErrors
Definition: CanBusInterface.h:46
yarp::dev::CanBuffer::getPointer
CanMessage ** getPointer()
Definition: CanBusInterface.h:85
ret
bool ret
Definition: ImplementAxisInfo.cpp:72
yarp::dev::CanMessage::getPointer
virtual const unsigned char * getPointer() const =0
yarp::dev::ICanBusErrors
Definition: CanBusInterface.h:196
yError
#define yError(...)
Definition: Log.h:282
yarp::dev::ImplementCanBufferFactory
Implementation of a ICanBufferFactory.
Definition: CanBusInterface.h:120
yarp::dev::CanErrors::rxCanErrors
int rxCanErrors
Definition: CanBusInterface.h:47
Log.h
yarp::dev::CanErrors::busoff
bool busoff
Definition: CanBusInterface.h:48
yarp::dev::ImplementCanBufferFactory::createBuffer
virtual CanBuffer createBuffer(int elem)
Definition: CanBusInterface.h:124
yarp::dev::CanErrors::rxCanFifoOvr
unsigned int rxCanFifoOvr
Definition: CanBusInterface.h:49
yarp::dev::CanBuffer
Definition: CanBusInterface.h:72
yarp::dev::CanBuffer::operator[]
const CanMessage & operator[](int k) const
Definition: CanBusInterface.h:99
yarp::dev::CanBuffer::CanBuffer
CanBuffer()
Definition: CanBusInterface.cpp:11
yarp::dev::ICanBus::canRead
virtual bool canRead(CanBuffer &msgs, unsigned int size, unsigned int *read, bool wait=false)=0
yarp::dev::CanMessage::getData
virtual const unsigned char * getData() const =0
buffer
Definition: V4L_camera.h:75
yarp::dev::CanBuffer::getPointer
const CanMessage *const * getPointer() const
Definition: CanBusInterface.h:90
yarp::dev::CanMessage::setLen
virtual void setLen(unsigned char len)=0
yarp::dev::CanMessage::setId
virtual void setId(unsigned int id)=0
yarp::dev::CanErrors::txBufferOvr
unsigned int txBufferOvr
Definition: CanBusInterface.h:51
YARP_dev_API
#define YARP_dev_API
Definition: api.h:19
yarp::dev::ICanBufferFactory::~ICanBufferFactory
virtual ~ICanBufferFactory()
Definition: CanBusInterface.h:108
yarp::dev::ICanBufferFactory::createBuffer
virtual CanBuffer createBuffer(int nmessage)=0
yarp::dev::ICanBufferFactory::destroyBuffer
virtual void destroyBuffer(CanBuffer &msgs)=0
IMPL
#define IMPL()
Definition: Port.cpp:46
yarp::dev::CanMessage::getLen
virtual unsigned char getLen() const =0
yarp
The main, catch-all namespace for YARP.
Definition: environment.h:18
yarp::dev::CanMessage
Definition: CanBusInterface.h:56
yarp::dev::ImplementCanBufferFactory::~ImplementCanBufferFactory
virtual ~ImplementCanBufferFactory()
Definition: CanBusInterface.h:122
yarp::dev::CanMessage::getData
virtual unsigned char * getData()=0
yarp::dev::CanMessage::~CanMessage
virtual ~CanMessage()
Definition: CanBusInterface.h:58
yarp::dev::ICanBus::canGetBaudRate
virtual bool canGetBaudRate(unsigned int *rate)=0
yarp::dev::CanErrors::CanErrors
CanErrors()
Definition: CanBusInterface.h:35
yarp::dev::ICanBus::canSetBaudRate
virtual bool canSetBaudRate(unsigned int rate)=0
yarp::dev::ICanBus::canIdAdd
virtual bool canIdAdd(unsigned int id)=0
yarp::dev::ICanBus::canIdDelete
virtual bool canIdDelete(unsigned int id)=0
yarp::dev::ICanBusErrors::canGetErrors
virtual bool canGetErrors(yarp::dev::CanErrors &err)=0
yarp::dev::CanErrors::rxBufferOvr
unsigned int rxBufferOvr
Definition: CanBusInterface.h:52
api.h
yarp::dev::ICanBufferFactory
Definition: CanBusInterface.h:106
yarp::dev::CanMessage::getPointer
virtual unsigned char * getPointer()=0
yarp::dev::CanMessage::operator=
virtual CanMessage & operator=(const CanMessage &l)=0