yarp-devices
CanBusHico.hpp
1 // -*- mode:C++; tab-width:4; c-basic-offset:4; indent-tabs-mode:nil -*-
2 
3 #ifndef __CAN_BUS_HICO_HPP__
4 #define __CAN_BUS_HICO_HPP__
5 
6 #include <set>
7 #include <mutex>
8 #include <string>
9 #include <utility>
10 
11 #include <yarp/os/Bottle.h>
12 
13 #include <yarp/dev/DeviceDriver.h>
14 #include <yarp/dev/CanBusInterface.h>
15 
16 #include "hico_api.h"
17 #include "HicoCanMessage.hpp"
18 
19 namespace roboticslab
20 {
21 
32 class CanBusHico : public yarp::dev::DeviceDriver,
33  public yarp::dev::ICanBus,
34  public yarp::dev::ICanBusErrors,
35  public yarp::dev::ImplementCanBufferFactory<HicoCanMessage, struct can_msg>
36 {
37 public:
38 
39  ~CanBusHico() override
40  { close(); }
41 
42  // --------- DeviceDriver declarations. Implementation in DeviceDriverImpl.cpp ---------
43 
44  bool open(yarp::os::Searchable& config) override;
45 
46  bool close() override;
47 
48  // --------- ICanBus declarations. Implementation in ICanBusImpl.cpp ---------
49 
50  bool canSetBaudRate(unsigned int rate) override;
51 
52  bool canGetBaudRate(unsigned int * rate) override;
53 
54  bool canIdAdd(unsigned int id) override;
55 
56  bool canIdDelete(unsigned int id) override;
57 
58  bool canRead(yarp::dev::CanBuffer & msgs, unsigned int size, unsigned int * read, bool wait = false) override;
59 
60  bool canWrite(const yarp::dev::CanBuffer & msgs, unsigned int size, unsigned int * sent, bool wait = false) override;
61 
62  // --------- ICanBusErrors declarations. Implementation in ICanBusErrorsImpl.cpp ---------
63 
64  bool canGetErrors(yarp::dev::CanErrors & err) override;
65 
66 private:
67 
69  {
70  public:
71  enum filter_config { DISABLED, NO_RANGE, MASK_AND_RANGE };
72 
73  explicit FilterManager(const CanBusHico & owner, int fileDescriptor, bool enableRanges);
74 
75  bool parseIds(const yarp::os::Bottle & b);
76  bool hasId(unsigned int id) const;
77  bool isValid() const;
78  bool insertId(unsigned int id);
79  bool eraseId(unsigned int id);
80  bool clearFilters(bool clearStage = true);
81 
82  static const int MAX_FILTERS;
83 
84  private:
85  bool setMaskedFilter(unsigned int id);
86  bool setRangedFilter(unsigned int lower, unsigned int upper);
87  bool bulkUpdate();
88 
89  const CanBusHico & owner;
90  int fd;
91  bool valid;
92  bool enableRanges;
93  std::set<unsigned int> stage, currentlyActive;
94  };
95 
96  FilterManager::filter_config parseFilterConfiguration(const std::string & str);
97 
98  enum io_operation { READ, WRITE };
99 
100  bool waitUntilTimeout(io_operation op, bool * bufferReady);
101  bool bitrateToId(unsigned int bitrate, unsigned int * id);
102  bool idToBitrate(unsigned int id, unsigned int * bitrate);
103 
105  int fileDescriptor {0};
106  int rxTimeoutMs {0}, txTimeoutMs {0};
107 
108  bool blockingMode;
109  bool allowPermissive;
110 
111  mutable std::mutex canBusReady;
112 
113  std::pair<bool, unsigned int> bitrateState;
114 
115  FilterManager * filterManager {nullptr};
116 
117  FilterManager::filter_config filterConfig;
118 };
119 
120 } // namespace roboticslab
121 
122 #endif // __CAN_BUS_HICO_HPP__
Definition: CanBusHico.hpp:69
Specifies the HicoCan (hcanpci) behaviour and specifications.
Definition: CanBusHico.hpp:36
int fileDescriptor
Definition: CanBusHico.hpp:105
The main, catch-all namespace for Robotics Lab UC3M.
Definition: groups.dox:6