yarp-devices
CanBusSocket.hpp
1 // -*- mode:C++; tab-width:4; c-basic-offset:4; indent-tabs-mode:nil -*-
2 
3 #ifndef __CAN_BUS_SOCKET_HPP__
4 #define __CAN_BUS_SOCKET_HPP__
5 
6 #include <linux/can.h>
7 #include <linux/can/raw.h>
8 
9 #include <string>
10 #include <vector>
11 
12 #include <yarp/dev/DeviceDriver.h>
13 #include <yarp/dev/CanBusInterface.h>
14 
15 #include "SocketCanMessage.hpp"
16 
17 namespace roboticslab
18 {
19 
27 class CanBusSocket : public yarp::dev::DeviceDriver,
28  public yarp::dev::ICanBus,
29  public yarp::dev::ImplementCanBufferFactory<SocketCanMessage, struct can_frame>
30 {
31 public:
32  ~CanBusSocket() override
33  { close(); }
34 
35  // --------- DeviceDriver declarations. Implementation in DeviceDriverImpl.cpp ---------
36  bool open(yarp::os::Searchable& config) override;
37  bool close() override;
38 
39  // --------- ICanBus declarations. Implementation in ICanBusImpl.cpp ---------
40  bool canSetBaudRate(unsigned int rate) override;
41  bool canGetBaudRate(unsigned int * rate) override;
42  bool canIdAdd(unsigned int id) override;
43  bool canIdDelete(unsigned int id) override;
44  bool canRead(yarp::dev::CanBuffer & msgs, unsigned int size, unsigned int * read, bool wait = false) override;
45  bool canWrite(const yarp::dev::CanBuffer & msgs, unsigned int size, unsigned int * sent, bool wait = false) override;
46 
47 private:
48  enum io_operation { READ, WRITE };
49 
50  bool waitUntilTimeout(io_operation op, bool * bufferReady);
51  void interpretErrorFrame(const struct can_frame * msg);
52 
53  std::string iface;
54  bool blockingMode;
55  bool allowPermissive;
56  bool filterFunctionCodes;
57  unsigned int bitrate {0};
58  int rxTimeoutMs {0};
59  int txTimeoutMs {0};
60  int s {0};
61  std::vector<struct can_filter> filters;
62 };
63 
64 } // namespace roboticslab
65 
66 #endif // __CAN_BUS_SOCKET_HPP__
Definition: CanBusSocket.hpp:30
The main, catch-all namespace for Robotics Lab UC3M.
Definition: groups.dox:6