Lely core libraries  2.2.5
device.hpp
Go to the documentation of this file.
1 
22 #ifndef LELY_COAPP_DEVICE_HPP_
23 #define LELY_COAPP_DEVICE_HPP_
24 
26 #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 namespace lely {
35 
36 // The CANopen device from <lely/co/dev.hpp>.
37 class CODev;
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<detail::is_canopen_type<T>::value, T>::type Read(
87  uint16_t idx, uint8_t subidx) const;
88 
100  template <class T>
101  typename ::std::enable_if<detail::is_canopen_type<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<detail::is_canopen_basic<T>::value>::type Write(
117  uint16_t idx, uint8_t subidx, T value);
118 
129  template <class T>
130  typename ::std::enable_if<detail::is_canopen_basic<T>::value>::type Write(
131  uint16_t idx, uint8_t subidx, T value, ::std::error_code& ec);
132 
144  template <class T>
145  typename ::std::enable_if<detail::is_canopen_array<T>::value>::type Write(
146  uint16_t idx, uint8_t subidx, const T& value);
147 
158  template <class T>
159  typename ::std::enable_if<detail::is_canopen_array<T>::value>::type Write(
160  uint16_t idx, uint8_t subidx, const T& value, ::std::error_code& ec);
161 
173  void Write(uint16_t idx, uint8_t subidx, const char* value);
174 
185  void Write(uint16_t idx, uint8_t subidx, const char* value,
186  ::std::error_code& ec);
187 
200  void Write(uint16_t idx, uint8_t subidx, const char16_t* value);
201 
213  void Write(uint16_t idx, uint8_t subidx, const char16_t* value,
214  ::std::error_code& ec);
215 
228  void Write(uint16_t idx, uint8_t subidx, const void* p, ::std::size_t n);
229 
241  void Write(uint16_t idx, uint8_t subidx, const void* p, ::std::size_t n,
242  ::std::error_code& ec);
243 
255  void WriteEvent(uint16_t idx, uint8_t subidx);
256 
268  void WriteEvent(uint16_t idx, uint8_t subidx, ::std::error_code& ec) noexcept;
269 
287  template <class T>
288  typename ::std::enable_if<detail::is_canopen_basic<T>::value, T>::type
289  RpdoRead(uint8_t id, uint16_t idx, uint8_t subidx) const;
290 
307  template <class T>
308  typename ::std::enable_if<detail::is_canopen_basic<T>::value, T>::type
309  RpdoRead(uint8_t id, uint16_t idx, uint8_t subidx,
310  ::std::error_code& ec) const;
311 
330  template <class T>
331  typename ::std::enable_if<detail::is_canopen_basic<T>::value, T>::type
332  TpdoRead(uint8_t id, uint16_t idx, uint8_t subidx) const;
333 
351  template <class T>
352  typename ::std::enable_if<detail::is_canopen_basic<T>::value, T>::type
353  TpdoRead(uint8_t id, uint16_t idx, uint8_t subidx,
354  ::std::error_code& ec) const;
355 
372  template <class T>
373  typename ::std::enable_if<detail::is_canopen_basic<T>::value>::type TpdoWrite(
374  uint8_t id, uint16_t idx, uint8_t subidx, T value);
375 
391  template <class T>
392  typename ::std::enable_if<detail::is_canopen_basic<T>::value>::type TpdoWrite(
393  uint8_t id, uint16_t idx, uint8_t subidx, T value, ::std::error_code& ec);
394 
409  void TpdoWriteEvent(uint8_t id, uint16_t idx, uint8_t subidx);
410 
425  void TpdoWriteEvent(uint8_t id, uint16_t idx, uint8_t subidx,
426  ::std::error_code& ec) noexcept;
427 
428  /*
429  * Registers the function to be invoked when a value is successfully written
430  * to the local object dictionary by an SDO download (or Receive-PDO) request.
431  * Only a single function can be registered at any one time. If
432  * <b>on_write</b> contains a callable function target, a copy of the target
433  * is invoked _after_ OnWrite(uint16_t, uint8_t) completes.
434  */
435  void OnWrite(::std::function<void(uint16_t, uint8_t)> on_write);
436 
437  /*
438  * Registers the function to be invoked when a value is successfully written
439  * to an RPDO-mapped object in the local object dictionary by a Receive-PDO
440  * (or SDO download). Only a single function can be registered at any one
441  * time. If <b>on_tpdo_write</b> contains a callable function target, a copy
442  * of the target is invoked _after_ OnRpdoWrite(uint8_t, uint16_t, uint8_t)
443  * completes.
444  */
445  void OnRpdoWrite(
446  ::std::function<void(uint8_t, uint16_t, uint8_t)> on_rpdo_write);
447 
448  protected:
449  ~Device();
450 
452  CODev* dev() const noexcept;
453 
465  const ::std::type_info& Type(uint16_t idx, uint8_t subidx) const;
466 
478  const ::std::type_info& Type(uint16_t idx, uint8_t subidx,
479  ::std::error_code& ec) const noexcept;
480 
494  template <class T>
495  typename ::std::enable_if<detail::is_canopen_type<T>::value, T>::type Get(
496  uint16_t idx, uint8_t subidx) const;
497 
510  template <class T>
511  typename ::std::enable_if<detail::is_canopen_type<T>::value, T>::type Get(
512  uint16_t idx, uint8_t subidx, ::std::error_code& ec) const noexcept;
513 
526  template <class T>
527  typename ::std::enable_if<detail::is_canopen_basic<T>::value>::type Set(
528  uint16_t idx, uint8_t subidx, T value);
529 
541  template <class T>
542  typename ::std::enable_if<detail::is_canopen_basic<T>::value>::type Set(
543  uint16_t idx, uint8_t subidx, T value, ::std::error_code& ec) noexcept;
544 
557  template <class T>
558  typename ::std::enable_if<detail::is_canopen_array<T>::value>::type Set(
559  uint16_t idx, uint8_t subidx, const T& value);
560 
572  template <class T>
573  typename ::std::enable_if<detail::is_canopen_array<T>::value>::type Set(
574  uint16_t idx, uint8_t subidx, const T& value,
575  ::std::error_code& ec) noexcept;
576 
589  void Set(uint16_t idx, uint8_t subidx, const char* value);
590 
602  void Set(uint16_t idx, uint8_t subidx, const char* value,
603  ::std::error_code& ec) noexcept;
604 
618  void Set(uint16_t idx, uint8_t subidx, const char16_t* value);
619 
632  void Set(uint16_t idx, uint8_t subidx, const char16_t* value,
633  ::std::error_code& ec) noexcept;
634 
648  void Set(uint16_t idx, uint8_t subidx, const void* p, ::std::size_t n);
649 
662  void Set(uint16_t idx, uint8_t subidx, const void* p, ::std::size_t n,
663  ::std::error_code& ec) noexcept;
664 
678  const char* GetUploadFile(uint16_t idx, uint8_t subidx) const;
679 
693  const char* GetUploadFile(uint16_t idx, uint8_t subidx,
694  ::std::error_code& ec) const noexcept;
695 
709  void SetUploadFile(uint16_t idx, uint8_t subidx, const char* filename);
710 
724  void SetUploadFile(uint16_t idx, uint8_t subidx, const char* filename,
725  ::std::error_code& ec) noexcept;
726 
740  const char* GetDownloadFile(uint16_t idx, uint8_t subidx) const;
741 
755  const char* GetDownloadFile(uint16_t idx, uint8_t subidx,
756  ::std::error_code& ec) const noexcept;
757 
771  void SetDownloadFile(uint16_t idx, uint8_t subidx, const char* filename);
772 
786  void SetDownloadFile(uint16_t idx, uint8_t subidx, const char* filename,
787  ::std::error_code& ec) noexcept;
799  void SetEvent(uint16_t idx, uint8_t subidx);
800 
814  void SetEvent(uint16_t idx, uint8_t subidx, ::std::error_code& ec) noexcept;
815 
834  template <class T>
835  typename ::std::enable_if<detail::is_canopen_basic<T>::value, T>::type
836  RpdoGet(uint8_t id, uint16_t idx, uint8_t subidx) const;
837 
856  template <class T>
857  typename ::std::enable_if<detail::is_canopen_basic<T>::value, T>::type
858  RpdoGet(uint8_t id, uint16_t idx, uint8_t subidx, ::std::error_code& ec) const
859  noexcept;
860 
880  template <class T>
881  typename ::std::enable_if<detail::is_canopen_basic<T>::value, T>::type
882  TpdoGet(uint8_t id, uint16_t idx, uint8_t subidx) const;
883 
903  template <class T>
904  typename ::std::enable_if<detail::is_canopen_basic<T>::value, T>::type
905  TpdoGet(uint8_t id, uint16_t idx, uint8_t subidx, ::std::error_code& ec) const
906  noexcept;
907 
925  template <class T>
926  typename ::std::enable_if<detail::is_canopen_basic<T>::value>::type TpdoSet(
927  uint8_t id, uint16_t idx, uint8_t subidx, T value);
928 
945  template <class T>
946  typename ::std::enable_if<detail::is_canopen_basic<T>::value>::type TpdoSet(
947  uint8_t id, uint16_t idx, uint8_t subidx, T value,
948  ::std::error_code& ec) noexcept;
949 
965  void TpdoSetEvent(uint8_t id, uint16_t idx, uint8_t subidx);
966 
981  void TpdoSetEvent(uint8_t id, uint16_t idx, uint8_t subidx,
982  ::std::error_code& ec) noexcept;
983 
991  void UpdateRpdoMapping();
992 
1000  void UpdateTpdoMapping();
1001 
1002  /*
1003  * The function invoked when a value is successfully written to the local
1004  * object dictionary by an SDO download (or Receive-PDO) request.
1005  *
1006  * @param idx the object index (in the range [0x2000..0xBFFF]).
1007  * @param subidx the object sub-index.
1008  */
1009  virtual void
1010  OnWrite(uint16_t /*idx*/, uint8_t /*subidx*/) noexcept {}
1011 
1012  /*
1013  * The function invoked when a value is successfully written to an RPDO-mapped
1014  * object in the local object dictionary by a Receive-PDO (or SDO download)
1015  * request.
1016  *
1017  * @param id the node-ID.
1018  * @param idx the remote object index.
1019  * @param subidx the remote object sub-index.
1020  *
1021  * @pre a valid mapping from remote TPDO-mapped sub-objects to local
1022  * RPDO-mapped sub-objects has been generated with UpdateRpdoMapping().
1023  */
1024  virtual void
1025  OnRpdoWrite(uint8_t /*id*/, uint16_t /*idx*/, uint8_t /*subidx*/) noexcept {}
1026 
1027  private:
1028  struct Impl_;
1029  ::std::unique_ptr<Impl_> impl_;
1030 };
1031 
1032 } // namespace canopen
1033 
1034 } // namespace lely
1035 
1036 #endif // LELY_COAPP_DEVICE_HPP_
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:695
void UpdateRpdoMapping()
Updates the mapping from remote TPDO-mapped sub-objects to local RPDO-mapped sub-objects.
Definition: device.cpp:1399
typename ::std::enable_if< detail::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...
This header file is part of the utilities library; it contains the C++ mutual exclusion helper classe...
void UpdateTpdoMapping()
Updates the mapping from remote RPDO-mapped sub-objects to local TPDO-mapped sub-objects.
Definition: device.cpp:1458
This header file is part of the C++ CANopen application library; it contains the SDO error declaratio...
typename ::std::enable_if< detail::is_canopen_basic< T >::value >::type Set(uint16_t idx, uint8_t subidx, T value)
Writes a CANopen basic value to a sub-object.
An opaque CANopen device type.
Definition: dev.hpp:77
typename ::std::enable_if< detail::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, which reads the value that will be written to an RPDO-mapped sub-object in a remote object dictionary by a Transmit-PDO.
typename ::std::enable_if< detail::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...
uint8_t netid() const noexcept
Returns the network-ID.
Definition: device.cpp:154
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:1074
typename ::std::enable_if< detail::is_canopen_type< T >::value, T >::type Read(uint16_t idx, uint8_t subidx) const
Submits an SDO upload request to the local object dictionary.
An abstract interface conforming to the BasicLockable concept.
Definition: mutex.hpp:34
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:1163
const ::std::type_info & Type(uint16_t idx, uint8_t subidx) const
Returns the type of a sub-object.
Definition: device.cpp:728
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:474
typename ::std::enable_if< detail::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...
This header file is part of the C++ CANopen application library; it contains the CANopen type traits...
typename ::std::enable_if< detail::is_canopen_type< T >::value, T >::type Get(uint16_t idx, uint8_t subidx) const
Reads the value of a sub-object.
typename ::std::enable_if< detail::is_canopen_basic< T >::value >::type Write(uint16_t idx, uint8_t subidx, T value)
Submits an SDO download request to the local object dictionary.
The internal implementation of the CANopen device description.
Definition: device.cpp:44
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:1047
CODev * dev() const noexcept
Returns a pointer to the internal CANopen device from <lely/co/dev.hpp>.
Definition: device.cpp:723
typename ::std::enable_if< detail::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 ...
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:1105
The CANopen device description.
Definition: device.hpp:45
Definition: buf.hpp:32
uint8_t id() const noexcept
Returns the node-ID.
Definition: device.cpp:161
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:1132
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:147
typename ::std::enable_if< detail::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...
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:1384