Lely core libraries  2.3.4
device.hpp
Go to the documentation of this file.
1 
22 #ifndef LELY_COAPP_DEVICE_HPP_
23 #define LELY_COAPP_DEVICE_HPP_
24 
25 #include <lely/coapp/sdo_error.hpp>
27 #include <lely/util/mutex.hpp>
28 
29 #include <functional>
30 #include <memory>
31 #include <string>
32 #include <typeinfo>
33 
34 // The CANopen device from <lely/co/dev.h>.
35 struct __co_dev;
36 
37 namespace lely {
38 
39 namespace canopen {
40 
45 class Device {
46  public:
61  Device(const ::std::string& dcf_txt, const ::std::string& dcf_bin = "",
62  uint8_t id = 0xff, util::BasicLockable* mutex = nullptr);
63 
64  Device(const Device&) = delete;
65  Device& operator=(const Device&) = delete;
66 
68  uint8_t netid() const noexcept;
69 
71  uint8_t id() const noexcept;
72 
85  template <class T>
86  typename ::std::enable_if<is_canopen<T>::value, T>::type Read(
87  uint16_t idx, uint8_t subidx) const;
88 
100  template <class T>
101  typename ::std::enable_if<is_canopen<T>::value, T>::type Read(
102  uint16_t idx, uint8_t subidx, ::std::error_code& ec) const;
103 
115  template <class T>
116  typename ::std::enable_if<is_canopen<T>::value>::type Write(uint16_t idx,
117  uint8_t subidx,
118  const T& value);
119 
130  template <class T>
131  typename ::std::enable_if<is_canopen<T>::value>::type Write(
132  uint16_t idx, uint8_t subidx, const T& value, ::std::error_code& ec);
133 
145  void Write(uint16_t idx, uint8_t subidx, const char* value);
146 
157  void Write(uint16_t idx, uint8_t subidx, const char* value,
158  ::std::error_code& ec);
159 
172  void Write(uint16_t idx, uint8_t subidx, const char16_t* value);
173 
185  void Write(uint16_t idx, uint8_t subidx, const char16_t* value,
186  ::std::error_code& ec);
187 
200  void Write(uint16_t idx, uint8_t subidx, const void* p, ::std::size_t n);
201 
213  void Write(uint16_t idx, uint8_t subidx, const void* p, ::std::size_t n,
214  ::std::error_code& ec);
215 
229  void WriteDcf(const uint8_t* begin, const uint8_t* end);
230 
243  void WriteDcf(const uint8_t* begin, const uint8_t* end,
244  ::std::error_code& ec);
245 
256  void WriteDcf(const char* path);
257 
267  void WriteDcf(const char* path, ::std::error_code& ec);
268 
280  void WriteEvent(uint16_t idx, uint8_t subidx);
281 
293  void WriteEvent(uint16_t idx, uint8_t subidx, ::std::error_code& ec) noexcept;
294 
312  template <class T>
313  typename ::std::enable_if<is_canopen_basic<T>::value, T>::type RpdoRead(
314  uint8_t id, uint16_t idx, uint8_t subidx) const;
315 
332  template <class T>
333  typename ::std::enable_if<is_canopen_basic<T>::value, T>::type RpdoRead(
334  uint8_t id, uint16_t idx, uint8_t subidx, ::std::error_code& ec) const;
335 
354  template <class T>
355  typename ::std::enable_if<is_canopen_basic<T>::value, T>::type TpdoRead(
356  uint8_t id, uint16_t idx, uint8_t subidx) const;
357 
375  template <class T>
376  typename ::std::enable_if<is_canopen_basic<T>::value, T>::type TpdoRead(
377  uint8_t id, uint16_t idx, uint8_t subidx, ::std::error_code& ec) const;
378 
395  template <class T>
396  typename ::std::enable_if<is_canopen_basic<T>::value>::type TpdoWrite(
397  uint8_t id, uint16_t idx, uint8_t subidx, T value);
398 
414  template <class T>
415  typename ::std::enable_if<is_canopen_basic<T>::value>::type TpdoWrite(
416  uint8_t id, uint16_t idx, uint8_t subidx, T value, ::std::error_code& ec);
417 
432  void TpdoWriteEvent(uint8_t id, uint16_t idx, uint8_t subidx);
433 
448  void TpdoWriteEvent(uint8_t id, uint16_t idx, uint8_t subidx,
449  ::std::error_code& ec) noexcept;
450 
451  /*
452  * Registers the function to be invoked when a value is successfully written
453  * to the local object dictionary by an SDO download (or Receive-PDO) request.
454  * Only a single function can be registered at any one time. If
455  * <b>on_write</b> contains a callable function target, a copy of the target
456  * is invoked _after_ OnWrite(uint16_t, uint8_t) completes.
457  */
458  void OnWrite(::std::function<void(uint16_t, uint8_t)> on_write);
459 
460  /*
461  * Registers the function to be invoked when a value is successfully written
462  * to an RPDO-mapped object in the local object dictionary by a Receive-PDO
463  * (or SDO download). Only a single function can be registered at any one
464  * time. If <b>on_tpdo_write</b> contains a callable function target, a copy
465  * of the target is invoked _after_ OnRpdoWrite(uint8_t, uint16_t, uint8_t)
466  * completes.
467  */
468  void OnRpdoWrite(
469  ::std::function<void(uint8_t, uint16_t, uint8_t)> on_rpdo_write);
470 
471  protected:
472  ~Device();
473 
475  __co_dev* dev() const noexcept;
476 
488  const ::std::type_info& Type(uint16_t idx, uint8_t subidx) const;
489 
501  const ::std::type_info& Type(uint16_t idx, uint8_t subidx,
502  ::std::error_code& ec) const noexcept;
503 
517  template <class T>
518  typename ::std::enable_if<is_canopen<T>::value, T>::type Get(
519  uint16_t idx, uint8_t subidx) const;
520 
533  template <class T>
534  typename ::std::enable_if<is_canopen<T>::value, T>::type Get(
535  uint16_t idx, uint8_t subidx, ::std::error_code& ec) const noexcept;
536 
549  template <class T>
550  typename ::std::enable_if<is_canopen<T>::value>::type Set(uint16_t idx,
551  uint8_t subidx,
552  const T& value);
553 
565  template <class T>
566  typename ::std::enable_if<is_canopen<T>::value>::type Set(
567  uint16_t idx, uint8_t subidx, const T& value,
568  ::std::error_code& ec) noexcept;
569 
582  void Set(uint16_t idx, uint8_t subidx, const char* value);
583 
595  void Set(uint16_t idx, uint8_t subidx, const char* value,
596  ::std::error_code& ec) noexcept;
597 
611  void Set(uint16_t idx, uint8_t subidx, const char16_t* value);
612 
625  void Set(uint16_t idx, uint8_t subidx, const char16_t* value,
626  ::std::error_code& ec) noexcept;
627 
641  void Set(uint16_t idx, uint8_t subidx, const void* p, ::std::size_t n);
642 
655  void Set(uint16_t idx, uint8_t subidx, const void* p, ::std::size_t n,
656  ::std::error_code& ec) noexcept;
657 
671  const char* GetUploadFile(uint16_t idx, uint8_t subidx) const;
672 
686  const char* GetUploadFile(uint16_t idx, uint8_t subidx,
687  ::std::error_code& ec) const noexcept;
688 
702  void SetUploadFile(uint16_t idx, uint8_t subidx, const char* filename);
703 
717  void SetUploadFile(uint16_t idx, uint8_t subidx, const char* filename,
718  ::std::error_code& ec) noexcept;
719 
733  const char* GetDownloadFile(uint16_t idx, uint8_t subidx) const;
734 
748  const char* GetDownloadFile(uint16_t idx, uint8_t subidx,
749  ::std::error_code& ec) const noexcept;
750 
764  void SetDownloadFile(uint16_t idx, uint8_t subidx, const char* filename);
765 
779  void SetDownloadFile(uint16_t idx, uint8_t subidx, const char* filename,
780  ::std::error_code& ec) noexcept;
792  void SetEvent(uint16_t idx, uint8_t subidx);
793 
807  void SetEvent(uint16_t idx, uint8_t subidx, ::std::error_code& ec) noexcept;
808 
827  template <class T>
828  typename ::std::enable_if<is_canopen_basic<T>::value, T>::type RpdoGet(
829  uint8_t id, uint16_t idx, uint8_t subidx) const;
830 
849  template <class T>
850  typename ::std::enable_if<is_canopen_basic<T>::value, T>::type RpdoGet(
851  uint8_t id, uint16_t idx, uint8_t subidx,
852  ::std::error_code& ec) const noexcept;
853 
873  template <class T>
874  typename ::std::enable_if<is_canopen_basic<T>::value, T>::type TpdoGet(
875  uint8_t id, uint16_t idx, uint8_t subidx) const;
876 
896  template <class T>
897  typename ::std::enable_if<is_canopen_basic<T>::value, T>::type TpdoGet(
898  uint8_t id, uint16_t idx, uint8_t subidx,
899  ::std::error_code& ec) const noexcept;
900 
918  template <class T>
919  typename ::std::enable_if<is_canopen_basic<T>::value>::type TpdoSet(
920  uint8_t id, uint16_t idx, uint8_t subidx, T value);
921 
938  template <class T>
939  typename ::std::enable_if<is_canopen_basic<T>::value>::type TpdoSet(
940  uint8_t id, uint16_t idx, uint8_t subidx, T value,
941  ::std::error_code& ec) noexcept;
942 
958  void TpdoSetEvent(uint8_t id, uint16_t idx, uint8_t subidx);
959 
974  void TpdoSetEvent(uint8_t id, uint16_t idx, uint8_t subidx,
975  ::std::error_code& ec) noexcept;
976 
984  void UpdateRpdoMapping();
985 
993  void UpdateTpdoMapping();
994 
995  /*
996  * The function invoked when a value is successfully written to the local
997  * object dictionary by an SDO download (or Receive-PDO) request.
998  *
999  * @param idx the object index (in the range [0x2000..0xBFFF]).
1000  * @param subidx the object sub-index.
1001  */
1002  virtual void
1003  OnWrite(uint16_t /*idx*/, uint8_t /*subidx*/) noexcept {}
1004 
1005  /*
1006  * The function invoked when a value is successfully written to an RPDO-mapped
1007  * object in the local object dictionary by a Receive-PDO (or SDO download)
1008  * request. In the case of a PDO, this function is invoked for each sub-object
1009  * in the order of the RPDO mapping.
1010  *
1011  * @param id the node-ID.
1012  * @param idx the remote object index.
1013  * @param subidx the remote object sub-index.
1014  *
1015  * @pre a valid mapping from remote TPDO-mapped sub-objects to local
1016  * RPDO-mapped sub-objects has been generated with UpdateRpdoMapping().
1017  */
1018  virtual void
1019  OnRpdoWrite(uint8_t /*id*/, uint16_t /*idx*/, uint8_t /*subidx*/) noexcept {}
1020 
1029  void RpdoWrite(uint8_t id, uint16_t idx, uint8_t subidx);
1030 
1031  private:
1032  struct Impl_;
1033  ::std::unique_ptr<Impl_> impl_;
1034 };
1035 
1036 } // namespace canopen
1037 
1038 } // namespace lely
1039 
1040 #endif // LELY_COAPP_DEVICE_HPP_
lely::util::BasicLockable
An abstract interface conforming to the BasicLockable concept.
Definition: mutex.hpp:34
lely::canopen::Device::RpdoRead
typename ::std::enable_if< is_canopen_basic< T >::value, T >::type RpdoRead(uint8_t id, uint16_t idx, uint8_t subidx) const
Reads the value of a sub-object in a remote object dictionary by submitting an SDO upload request to ...
lely::canopen::Device::WriteDcf
void WriteDcf(const uint8_t *begin, const uint8_t *end)
Submits a series of SDO download requests to the local object dictionary.
Definition: device.cpp:522
lely::canopen::Device::UpdateTpdoMapping
void UpdateTpdoMapping()
Updates the mapping from remote RPDO-mapped sub-objects to local TPDO-mapped sub-objects.
Definition: device.cpp:1714
lely::canopen::Device::TpdoSet
typename ::std::enable_if< is_canopen_basic< T >::value >::type TpdoSet(uint8_t id, uint16_t idx, uint8_t subidx, T value)
Writes a value to a sub-object in a remote object dictionary by writing to the corresponding PDO-mapp...
lely::canopen::Device::TpdoSetEvent
void TpdoSetEvent(uint8_t id, uint16_t idx, uint8_t subidx)
Triggers the transmission of every event-driven, asynchronous Transmit-PDO which is mapped into the s...
Definition: device.cpp:1579
lely::canopen::Device::GetUploadFile
const char * GetUploadFile(uint16_t idx, uint8_t subidx) const
Returns the value of the UploadFile attribute of a sub-object, if present.
Definition: device.cpp:1235
sdo_error.hpp
lely::canopen::Device::Get
typename ::std::enable_if< is_canopen< T >::value, T >::type Get(uint16_t idx, uint8_t subidx) const
Reads the value of a sub-object.
lely::canopen::Device::Device
Device(const ::std::string &dcf_txt, const ::std::string &dcf_bin="", uint8_t id=0xff, util::BasicLockable *mutex=nullptr)
Creates a new CANopen device description.
Definition: device.cpp:178
lely::canopen::Device::TpdoWrite
typename ::std::enable_if< is_canopen_basic< T >::value >::type TpdoWrite(uint8_t id, uint16_t idx, uint8_t subidx, T value)
Writes a value to a sub-object in a remote object dictionary by submitting an SDO download request to...
lely::canopen::Device::Impl_
The internal implementation of the CANopen device description.
Definition: device.cpp:56
lely::canopen::Device::netid
uint8_t netid() const noexcept
Returns the network-ID.
Definition: device.cpp:186
lely::canopen::Device::SetDownloadFile
void SetDownloadFile(uint16_t idx, uint8_t subidx, const char *filename)
Sets the value of the DownloadFile attribute of a sub-object, if present.
Definition: device.cpp:1320
lely::canopen::Device::dev
__co_dev * dev() const noexcept
Returns a pointer to the internal CANopen device from <lely/co/dev.hpp>.
Definition: device.cpp:828
lely::canopen::Device::id
uint8_t id() const noexcept
Returns the node-ID.
Definition: device.cpp:193
type_traits.hpp
lely::canopen::Device::SetUploadFile
void SetUploadFile(uint16_t idx, uint8_t subidx, const char *filename)
Sets the value of the UploadFile attribute of a sub-object, if present.
Definition: device.cpp:1262
lely::canopen::Device::SetEvent
void SetEvent(uint16_t idx, uint8_t subidx)
Checks if the specified sub-object in the local object dictionary can be mapped into a PDO and,...
Definition: device.cpp:1353
lely::canopen::Device
The CANopen device description.
Definition: device.hpp:45
lely::canopen::Device::RpdoWrite
void RpdoWrite(uint8_t id, uint16_t idx, uint8_t subidx)
Invokes OnRpdoWrite() as if a value was written to an RPDO-mapped object in the local object dictiona...
Definition: device.cpp:1776
lely::canopen::Device::TpdoWriteEvent
void TpdoWriteEvent(uint8_t id, uint16_t idx, uint8_t subidx)
Triggers the transmission of every event-driven, asynchronous Transmit-PDO which is mapped into the s...
Definition: device.cpp:796
lely::canopen::Device::Set
typename ::std::enable_if< is_canopen< T >::value >::type Set(uint16_t idx, uint8_t subidx, const T &value)
Writes a CANopen value to a sub-object.
lely::canopen::Device::RpdoGet
typename ::std::enable_if< is_canopen_basic< T >::value, T >::type RpdoGet(uint8_t id, uint16_t idx, uint8_t subidx) const
Reads the value of a sub-object in a remote object dictionary by reading the corresponding PDO-mapped...
lely::canopen::Device::TpdoRead
typename ::std::enable_if< is_canopen_basic< T >::value, T >::type TpdoRead(uint8_t id, uint16_t idx, uint8_t subidx) const
Submits an SDO upload request to a TPDO-mapped sub-object in the local object dictionary,...
__co_dev
A CANopen device.
Definition: dev.h:30
lely::canopen::Device::TpdoGet
typename ::std::enable_if< is_canopen_basic< T >::value, T >::type TpdoGet(uint8_t id, uint16_t idx, uint8_t subidx) const
Reads the value of a TPDO-mapped sub-object in the local object dictionary that will be written to an...
lely::canopen::Device::GetDownloadFile
const char * GetDownloadFile(uint16_t idx, uint8_t subidx) const
Returns the value of the DownloadFile attribute of a sub-object, if present.
Definition: device.cpp:1293
lely::canopen::Device::Write
typename ::std::enable_if< is_canopen< T >::value >::type Write(uint16_t idx, uint8_t subidx, const T &value)
Submits an SDO download request to the local object dictionary.
lely::canopen::Device::Type
const ::std::type_info & Type(uint16_t idx, uint8_t subidx) const
Returns the type of a sub-object.
Definition: device.cpp:833
lely::canopen::Device::Read
typename ::std::enable_if< is_canopen< T >::value, T >::type Read(uint16_t idx, uint8_t subidx) const
Submits an SDO upload request to the local object dictionary.
lely::canopen::Device::WriteEvent
void WriteEvent(uint16_t idx, uint8_t subidx)
Checks if the specified sub-object in the local object dictionary can be mapped into a PDO and,...
Definition: device.cpp:575
lely::canopen::Device::UpdateRpdoMapping
void UpdateRpdoMapping()
Updates the mapping from remote TPDO-mapped sub-objects to local RPDO-mapped sub-objects.
Definition: device.cpp:1594
mutex.hpp