iCub-main
SocketCan.h
Go to the documentation of this file.
1 // -*- mode:C++; tab-width:4; c-basic-offset:4; indent-tabs-mode:nil -*-
2 
25 
26 #ifndef __SOCKETCANH__
27 #define __SOCKETCANH__
28 
29 #include <yarp/dev/DeviceDriver.h>
30 #include <yarp/dev/CanBusInterface.h>
31 
32 #include "memory.h"
33 #include <sys/types.h>
34 #include <sys/socket.h>
35 #include <linux/can.h>
36 #include <linux/can/raw.h>
37 
38 namespace yarp{
39  namespace dev{
40  class SocketCan;
41  class SocketCanMessage;
42  }
43 }
44 
45 class yarp::dev::SocketCanMessage:public yarp::dev::CanMessage
46 {
47 public:
48  struct can_frame *msg;
49 
50 public:
52  {
53  msg=0;
54  }
55 
57  {
58  }
59 
60  virtual CanMessage &operator=(const CanMessage &l)
61  {
62  const SocketCanMessage &tmp=dynamic_cast<const SocketCanMessage &>(l);
63  memcpy(msg, tmp.msg, sizeof(struct can_frame));
64  return *this;
65  }
66 
67  virtual unsigned int getId() const
68  { return msg->can_id;}
69 
70  virtual unsigned char getLen() const
71  { return msg->can_dlc;}
72 
73  virtual void setLen(unsigned char len)
74  { msg->can_dlc=len;}
75 
76  virtual void setId(unsigned int id)
77  { msg->can_id=id;}
78 
79  virtual const unsigned char *getData() const
80  { return msg->data; }
81 
82  virtual unsigned char *getData()
83  { return msg->data; }
84 
85  virtual unsigned char *getPointer()
86  { return (unsigned char *) msg; }
87 
88  virtual const unsigned char *getPointer() const
89  { return (const unsigned char *) msg; }
90 
91  virtual void setBuffer(unsigned char *b)
92  {
93  if (b!=0)
94  msg=(can_frame *)(b);
95  }
96 };
97 
106 class yarp::dev::SocketCan: public ImplementCanBufferFactory<SocketCanMessage, can_frame>,
107  public ICanBus,
108  public DeviceDriver
109 {
110 private:
111  int skt;
112 public:
113  SocketCan();
114  ~SocketCan();
115 
116  /* ICanBus */
117  virtual bool canSetBaudRate(unsigned int rate);
118  virtual bool canGetBaudRate(unsigned int *rate);
119  virtual bool canIdAdd(unsigned int id);
120  virtual bool canIdDelete(unsigned int id);
121 
122  virtual bool canRead(CanBuffer &msgs,
123  unsigned int size,
124  unsigned int *read,
125  bool wait=false);
126 
127  virtual bool canWrite(const CanBuffer &msgs,
128  unsigned int size,
129  unsigned int *sent,
130  bool wait=false);
131 
132  /*Device Driver*/
133  virtual bool open(yarp::os::Searchable &par);
134  virtual bool close();
135 
136 };
137 
138 #endif
virtual CanMessage & operator=(const CanMessage &l)
Definition: SocketCan.h:60
virtual const unsigned char * getPointer() const
Definition: SocketCan.h:88
virtual unsigned char getLen() const
Definition: SocketCan.h:70
virtual const unsigned char * getData() const
Definition: SocketCan.h:79
virtual void setLen(unsigned char len)
Definition: SocketCan.h:73
virtual unsigned char * getData()
Definition: SocketCan.h:82
virtual void setId(unsigned int id)
Definition: SocketCan.h:76
struct can_frame * msg
Definition: SocketCan.h:48
virtual unsigned int getId() const
Definition: SocketCan.h:67
virtual void setBuffer(unsigned char *b)
Definition: SocketCan.h:91
virtual unsigned char * getPointer()
Definition: SocketCan.h:85
socketcan : implements yarp::dev::ICanBus for a linux socketcan.
Definition: SocketCan.h:109
virtual bool close()
Definition: SocketCan.cpp:284
virtual bool canIdAdd(unsigned int id)
Definition: SocketCan.cpp:83
virtual bool canRead(CanBuffer &msgs, unsigned int size, unsigned int *read, bool wait=false)
Definition: SocketCan.cpp:95
virtual bool canWrite(const CanBuffer &msgs, unsigned int size, unsigned int *sent, bool wait=false)
Definition: SocketCan.cpp:175
virtual bool open(yarp::os::Searchable &par)
Definition: SocketCan.cpp:238
virtual bool canIdDelete(unsigned int id)
Definition: SocketCan.cpp:89
virtual bool canSetBaudRate(unsigned int rate)
Definition: SocketCan.cpp:71
virtual bool canGetBaudRate(unsigned int *rate)
Definition: SocketCan.cpp:77
bool read(yarp::os::Searchable &cfgtotal, pc104Data &pc104data)
Definition: ethParser.cpp:92
Copyright (C) 2008 RobotCub Consortium.