16 #include <yarp/dev/PolyDriver.h>
17 #include <yarp/dev/CanBusInterface.h>
18 #include <yarp/os/Searchable.h>
19 #include <yarp/os/Time.h>
20 #include <yarp/os/Log.h>
21 #include <yarp/os/LogStream.h>
24 #include <ace/SOCK_Dgram_Bcast.h>
25 #include <ace/Time_Value.h>
26 #include <ace/OS_NS_sys_socket.h>
37 #define DRIVER_KEEP_LEGACY_IDRIVER
40 #define MAX_READ_MSG 64
41 #define MAX_WRITE_MSG 8
51 enum { everyCANbus = 255 };
61 unsigned char data[8];
67 CanPacket(){ canpkt.canbus = 0; canpkt.id = 0; canpkt.len = 0; memset(canpkt.data, 0,
sizeof(canpkt.data)); }
71 CanPacket operator = (
const yarp::dev::CanMessage &canmsg)
74 pkt.canpkt.id = canmsg.getId();
75 pkt.canpkt.len = canmsg.getLen();
76 memcpy(pkt.canpkt.data, canmsg.getData(),
sizeof(pkt.canpkt.data));
81 unsigned int getId()
const {
return canpkt.id; }
82 unsigned char getLen()
const {
return canpkt.len; }
83 void setLen(
unsigned char len) { canpkt.len=len; }
84 void setId(
unsigned int id){ canpkt.id=id; }
85 const unsigned char *
getData()
const {
return canpkt.data; }
86 unsigned char *
getData(){
return canpkt.data; }
88 void setCanBus(
unsigned int bus){ canpkt.canbus=bus; }
97 typedef enum { can_driver2 = 0, eth_driver2 = 1 } iDriver2Type;
100 virtual int init(yarp::os::Searchable &config,
bool verbose =
true)=0;
119 int init(yarp::os::Searchable &config,
bool verbose =
true);
121 int receive_message(vector<CanPacket> &canpackets,
int howMany =
MAX_READ_MSG,
double TIMEOUT = 1);
122 int send_message(vector<CanPacket> &canpackets,
int n);
126 yarp::dev::PolyDriver dd;
127 yarp::dev::ICanBus *iCanBus;
128 yarp::dev::ICanBufferFactory *iFactory;
130 yarp::dev::CanBuffer canTXbuffer;
131 yarp::dev::CanBuffer canRXbuffer;
137 yarp::dev::CanBuffer createCanBuffer(
int m);
138 void destroyCanBuffer(yarp::dev::CanBuffer &buff);
152 int init(yarp::os::Searchable &config,
bool verbose =
true);
154 int receive_message(vector<CanPacket> &canpackets,
int howMany =
MAX_READ_MSG,
double TIMEOUT = 1);
155 int send_message(vector<CanPacket> &canpackets,
int n);
161 ACE_UINT32 mBoardAddr;
192 unsigned char dummy[4];
211 ACE_OS::socket_init(2,2);
218 ACE_OS::socket_fini();
221 bool create(ACE_UINT16 port,ACE_UINT32 address)
223 mSocket=
new ACE_SOCK_Dgram_Bcast(ACE_INET_Addr(port,address));
224 return mSocket!=NULL;
227 void sendTo(
void*
data,
size_t len, ACE_UINT16 port, ACE_UINT32 address)
233 mSocket->send(
data,len,ACE_INET_Addr(port,address));
236 ssize_t
receiveFrom(
void*
data,
size_t len, ACE_UINT32 &address, ACE_UINT16 &port,
int wait_msec)
238 ACE_Time_Value tv(wait_msec/1000,(wait_msec%1000)*1000);
239 ACE_INET_Addr ace_addr;
240 ssize_t nrec=mSocket->recv(
data,len,ace_addr,0,&tv);
247 address=ace_addr.get_ip_address();
248 port=ace_addr.get_port_number();
278 #if defined(DRIVER_KEEP_LEGACY_IDRIVER)
284 virtual int init(yarp::os::Searchable &config)=0;
297 yarp::dev::PolyDriver dd;
298 yarp::dev::ICanBus *iCanBus;
299 yarp::dev::ICanBufferFactory *iFactory;
303 int init(yarp::os::Searchable &config);
305 int receive_message(yarp::dev::CanBuffer &messages,
int howMany =
MAX_READ_MSG,
double TIMEOUT = 1);
306 int send_message(yarp::dev::CanBuffer &message,
int n);
308 yarp::dev::CanBuffer createBuffer(
int m);
309 void destroyBuffer(yarp::dev::CanBuffer &buff);
333 memcpy(msg,
tmp.msg,
sizeof(
ECMSG));
337 virtual unsigned int getId()
const {
return msg->
id; }
338 virtual unsigned char getLen()
const {
return msg->
len; }
339 virtual void setLen(
unsigned char len) { msg->
len=len; }
340 virtual void setId(
unsigned int id){ msg->
id=id; }
341 virtual const unsigned char *
getData()
const {
return msg->
data; }
343 virtual unsigned char *
getPointer(){
return (
unsigned char *) msg; }
344 virtual const unsigned char *
getPointer()
const {
return (
const unsigned char *) msg; }
352 class eDriver :
public yarp::dev::ImplementCanBufferFactory<EthCanMessage,ECMSG>,
public iDriver
358 ACE_UINT32 mBoardAddr;
362 yarp::dev::ICanBufferFactory *iFactory;
367 int init(yarp::os::Searchable &config);
369 int receive_message(yarp::dev::CanBuffer &messages,
int howMany =
MAX_READ_MSG,
double TIMEOUT = 1.0);
370 int send_message(yarp::dev::CanBuffer &message,
int n);
371 yarp::dev::CanBuffer createBuffer(
int m);
372 void destroyBuffer(yarp::dev::CanBuffer &buff);
unsigned int getId() const
void setCanBus(unsigned int bus)
const unsigned char * getData() const
void setId(unsigned int id)
unsigned char getLen() const
unsigned char * getData()
void setLen(unsigned char len)
bool create(ACE_UINT16 port, ACE_UINT32 address)
void sendTo(void *data, size_t len, ACE_UINT16 port, ACE_UINT32 address)
ssize_t receiveFrom(void *data, size_t len, ACE_UINT32 &address, ACE_UINT16 &port, int wait_msec)
ACE_SOCK_Dgram_Bcast * mSocket
unsigned int getCanBus() const
virtual const unsigned char * getData() const
virtual unsigned char getLen() const
virtual void setId(unsigned int id)
virtual unsigned char * getData()
void setCanBus(unsigned int bus)
virtual unsigned int getId() const
virtual unsigned char * getPointer()
virtual CanMessage & operator=(const CanMessage &l)
virtual void setLen(unsigned char len)
virtual void setBuffer(unsigned char *b)
virtual const unsigned char * getPointer() const
virtual int send_message(vector< CanPacket > &canpackets, int n)=0
virtual iDriver2Type type()=0
virtual int init(yarp::os::Searchable &config, bool verbose=true)=0
virtual int receive_message(vector< CanPacket > &canpackets, int howMany=MAX_READ_MSG, double TIMEOUT=1)=0
virtual void destroyBuffer(yarp::dev::CanBuffer &buff)=0
virtual int send_message(yarp::dev::CanBuffer &message, int n)=0
virtual yarp::dev::CanBuffer createBuffer(int m)=0
virtual int init(yarp::os::Searchable &config)=0
virtual int receive_message(yarp::dev::CanBuffer &messages, int howMany=MAX_READ_MSG, double TIMEOUT=1)=0