yarp-devices
CanBusPeak.hpp
1 // -*- mode:C++; tab-width:4; c-basic-offset:4; indent-tabs-mode:nil -*-
2 
3 #ifndef __CAN_BUS_PEAK_HPP__
4 #define __CAN_BUS_PEAK_HPP__
5 
6 #include <cstdint>
7 
8 #include <mutex>
9 #include <set>
10 
11 #include <yarp/dev/DeviceDriver.h>
12 #include <yarp/dev/CanBusInterface.h>
13 
14 #include <libpcanfd.h>
15 
16 #include "PeakCanMessage.hpp"
17 
18 namespace roboticslab
19 {
20 
31 class ImplementPeakCanBufferFactory : public yarp::dev::ImplementCanBufferFactory<PeakCanMessage, struct pcanfd_msg>
32 {
33 public:
34  yarp::dev::CanBuffer createBuffer(int elem) override
35  {
36  yarp::dev::CanBuffer ret;
37  struct pcanfd_msg * storage = new pcanfd_msg[elem];
38  yarp::dev::CanMessage ** messages = new yarp::dev::CanMessage *[elem];
39  PeakCanMessage * tmp = new PeakCanMessage[elem];
40 
41  std::memset(storage, 0, sizeof(struct pcanfd_msg) * elem);
42 
43  for (int k = 0; k < elem; k++)
44  {
45  messages[k] = &tmp[k];
46  messages[k]->setBuffer(reinterpret_cast<unsigned char *>(&storage[k]));
47 
48  // Changes wrt default implementation:
49  storage[k].type = PCANFD_TYPE_CAN20_MSG;
50  storage[k].flags = PCANFD_MSG_STD;
51  }
52 
53  ret.resize(messages, elem);
54  return ret;
55  }
56 };
57 
62 class CanBusPeak : public yarp::dev::DeviceDriver,
63  public yarp::dev::ICanBus,
64  public yarp::dev::ICanBusErrors,
66 {
67 public:
68 
69  ~CanBusPeak() override
70  { close(); }
71 
72  // --------- DeviceDriver declarations. Implementation in DeviceDriverImpl.cpp ---------
73 
74  bool open(yarp::os::Searchable& config) override;
75 
76  bool close() override;
77 
78  // --------- ICanBus declarations. Implementation in ICanBusImpl.cpp ---------
79 
80  bool canSetBaudRate(unsigned int rate) override;
81 
82  bool canGetBaudRate(unsigned int * rate) override;
83 
84  bool canIdAdd(unsigned int id) override;
85 
86  bool canIdDelete(unsigned int id) override;
87 
88  bool canRead(yarp::dev::CanBuffer & msgs, unsigned int size, unsigned int * read, bool wait = false) override;
89 
90  bool canWrite(const yarp::dev::CanBuffer & msgs, unsigned int size, unsigned int * sent, bool wait = false) override;
91 
92  // --------- ICanBusErrors declarations. Implementation in ICanBusErrorsImpl.cpp ---------
93 
94  bool canGetErrors(yarp::dev::CanErrors & err) override;
95 
96 private:
97 
98  enum io_operation { READ, WRITE };
99 
100  bool waitUntilTimeout(io_operation op, bool * bufferReady);
101 
102  std::uint64_t computeAcceptanceCodeAndMask();
103 
104  int fileDescriptor {0};
105  int rxTimeoutMs {0};
106  int txTimeoutMs {0};
107 
108  bool blockingMode;
109  bool allowPermissive;
110 
111  mutable std::mutex canBusReady;
112 
113  std::set<unsigned int> activeFilters;
114 };
115 
116 } // namespace roboticslab
117 
118 #endif // __CAN_BUS_PEAK_HPP__
Specifies the PeakCan behaviour and specifications.
Definition: CanBusPeak.hpp:66
Custom buffer of PeakCanMessage instances.
Definition: CanBusPeak.hpp:32
YARP wrapper for PeakCAN messages.
Definition: PeakCanMessage.hpp:21
The main, catch-all namespace for Robotics Lab UC3M.
Definition: groups.dox:6