Lely core libraries  2.2.5
dev.hpp
Go to the documentation of this file.
1 
22 #ifndef LELY_CO_DEV_HPP_
23 #define LELY_CO_DEV_HPP_
24 
25 #ifndef __cplusplus
26 #error "include <lely/co/dev.h> for the C interface"
27 #endif
28 
29 #include <lely/util/c_call.hpp>
30 #include <lely/util/c_type.hpp>
31 #include <lely/co/dev.h>
32 #include <lely/co/val.hpp>
33 
34 #include <vector>
35 
36 struct floc;
37 struct co_sdev;
38 
39 namespace lely {
40 
42 template <>
44  typedef __co_dev value_type;
45  typedef value_type& reference;
46  typedef const value_type& const_reference;
47  typedef value_type* pointer;
48  typedef const value_type* const_pointer;
49 
50  static void*
51  alloc() noexcept {
52  return __co_dev_alloc();
53  }
54 
55  static void
56  free(void* ptr) noexcept {
57  __co_dev_free(ptr);
58  }
59 
60  static pointer
61  init(pointer p, co_unsigned8_t id) noexcept {
62  return __co_dev_init(p, id);
63  }
64 
65  static pointer init(pointer p, const char* filename) noexcept;
66  static pointer init(pointer p, const char* begin, const char* end,
67  floc* at) noexcept;
68  static pointer init(pointer p, const co_sdev* sdev) noexcept;
69 
70  static void
71  fini(pointer p) noexcept {
72  __co_dev_fini(p);
73  }
74 };
75 
77 class CODev : public incomplete_c_type<__co_dev> {
79 
80  public:
81  explicit CODev(co_unsigned8_t id = 0xff) : c_base(id) {}
82 
83  explicit CODev(const char* filename) : c_base(filename) {}
84 
85  CODev(const char* begin, const char* end, floc* at = 0)
86  : c_base(begin, end, at) {}
87 
88  explicit CODev(const co_sdev* sdev) : c_base(sdev) {}
89 
90  co_unsigned8_t
91  getNetid() const noexcept {
92  return co_dev_get_netid(this);
93  }
94 
95  int
96  setNetid(co_unsigned8_t id) noexcept {
97  return co_dev_set_netid(this, id);
98  }
99 
100  co_unsigned8_t
101  getId() const noexcept {
102  return co_dev_get_id(this);
103  }
104 
105  int
106  setId(co_unsigned8_t id) noexcept {
107  return co_dev_set_id(this, id);
108  }
109 
110  co_unsigned16_t
111  getIdx(co_unsigned16_t maxidx, co_unsigned16_t* idx) const noexcept {
112  return co_dev_get_idx(this, maxidx, idx);
113  }
114 
115  ::std::vector<co_unsigned16_t>
116  getIdx() const {
117  std::vector<co_unsigned16_t> idx(getIdx(0, 0));
118  getIdx(idx.size(), idx.data());
119  return idx;
120  }
121 
122  int
123  insert(COObj* obj) noexcept {
124  return co_dev_insert_obj(this, obj);
125  }
126 
127  int
128  remove(COObj* obj) noexcept {
129  return co_dev_remove_obj(this, obj);
130  }
131 
132  COObj*
133  find(co_unsigned16_t idx) const noexcept {
134  return co_dev_find_obj(this, idx);
135  }
136 
137  COSub*
138  find(co_unsigned16_t idx, co_unsigned8_t subidx) const noexcept {
139  return co_dev_find_sub(this, idx, subidx);
140  }
141 
142  const char*
143  getName() const noexcept {
144  return co_dev_get_name(this);
145  }
146 
147  int
148  setName(const char* name) noexcept {
149  return co_dev_set_name(this, name);
150  }
151 
152  const char*
153  getVendorName() const noexcept {
154  return co_dev_get_vendor_name(this);
155  }
156 
157  int
158  setVendorName(const char* vendor_name) noexcept {
159  return co_dev_set_vendor_name(this, vendor_name);
160  }
161 
162  co_unsigned32_t
163  getVendorId() const noexcept {
164  return co_dev_get_vendor_id(this);
165  }
166 
167  void
168  setVendorId(co_unsigned32_t vendor_id) noexcept {
169  co_dev_set_vendor_id(this, vendor_id);
170  }
171 
172  const char*
173  getProductName() const noexcept {
174  return co_dev_get_product_name(this);
175  }
176 
177  int
178  setProductName(const char* product_name) noexcept {
179  return co_dev_set_product_name(this, product_name);
180  }
181 
182  co_unsigned32_t
183  getProductCode() const noexcept {
184  return co_dev_get_product_code(this);
185  }
186 
187  void
188  setProductCode(co_unsigned32_t product_code) noexcept {
189  co_dev_set_product_code(this, product_code);
190  }
191 
192  co_unsigned32_t
193  getRevision() const noexcept {
194  return co_dev_get_revision(this);
195  }
196 
197  void
198  setRevision(co_unsigned32_t revision) noexcept {
199  co_dev_set_revision(this, revision);
200  }
201 
202  const char*
203  getOrderCode() const noexcept {
204  return co_dev_get_order_code(this);
205  }
206 
207  int
208  setOrderCode(const char* order_code) noexcept {
209  return co_dev_set_order_code(this, order_code);
210  }
211 
212  unsigned int
213  getBaud() const noexcept {
214  return co_dev_get_baud(this);
215  }
216 
217  void
218  setBaud(unsigned int baud) noexcept {
219  co_dev_set_baud(this, baud);
220  }
221 
222  co_unsigned16_t
223  getRate() const noexcept {
224  return co_dev_get_rate(this);
225  }
226 
227  void
228  setRate(co_unsigned16_t rate) noexcept {
229  co_dev_set_rate(this, rate);
230  }
231 
232  bool
233  getLSS() const noexcept {
234  return !!co_dev_get_lss(this);
235  }
236 
237  void
238  setLSS(bool lss) noexcept {
239  co_dev_set_lss(this, lss);
240  }
241 
242  co_unsigned32_t
243  getDummy() const noexcept {
244  return co_dev_get_dummy(this);
245  }
246 
247  void
248  setDummy(co_unsigned32_t dummy) noexcept {
249  co_dev_set_dummy(this, dummy);
250  }
251 
252  template <co_unsigned16_t N>
253  const COVal<N>&
254  getVal(co_unsigned16_t idx, co_unsigned8_t subidx) const noexcept {
255  return *reinterpret_cast<const COVal<N>*>(
256  co_dev_get_val(this, idx, subidx));
257  }
258 
259  ::std::size_t
260  setVal(co_unsigned16_t idx, co_unsigned8_t subidx, const void* ptr,
261  ::std::size_t n) noexcept {
262  return co_dev_set_val(this, idx, subidx, ptr, n);
263  }
264 
265  template <co_unsigned16_t N>
266  ::std::size_t
267  setVal(co_unsigned16_t idx, co_unsigned8_t subidx,
268  const COVal<N>& val) noexcept {
269  return setVal(idx, subidx, val.address(), val.size());
270  }
271 
272  template <class T>
273  ::std::size_t
274  setVal(co_unsigned16_t idx, co_unsigned8_t subidx, const T& val) noexcept {
275  return setVal<co_type_traits_T<T>::index>(idx, subidx, val);
276  }
277 
278  ::std::size_t
279  readSub(co_unsigned16_t* pidx, co_unsigned8_t* psubidx,
280  const uint_least8_t* begin, const uint_least8_t* end) noexcept {
281  return co_dev_read_sub(this, pidx, psubidx, begin, end);
282  }
283 
284  ::std::size_t
285  writeSub(co_unsigned16_t idx, co_unsigned8_t subidx, uint_least8_t* begin,
286  uint_least8_t* end = 0) const noexcept {
287  return co_dev_write_sub(this, idx, subidx, begin, end);
288  }
289 
290  int
291  readDCF(co_unsigned16_t* pmin, co_unsigned16_t* pmax,
292  const COVal<CO_DEFTYPE_DOMAIN>& val) noexcept {
293  return co_dev_read_dcf(this, pmin, pmax,
294  reinterpret_cast<void* const*>(&val));
295  }
296 
297  int
298  readDCF(co_unsigned16_t* pmin, co_unsigned16_t* pmax,
299  const char* filename) noexcept {
300  return co_dev_read_dcf_file(this, pmin, pmax, filename);
301  }
302 
303  int
304  writeDCF(co_unsigned16_t min, co_unsigned16_t max,
305  COVal<CO_DEFTYPE_DOMAIN>& val) const noexcept {
306  return co_dev_write_dcf(this, min, max, reinterpret_cast<void**>(&val));
307  }
308 
309  int
310  writeDCF(co_unsigned16_t min, co_unsigned16_t max, const char* filename) const
311  noexcept {
312  return co_dev_write_dcf_file(this, min, max, filename);
313  }
314 
315  void
316  getTPDOEventInd(co_dev_tpdo_event_ind_t** pind, void** pdata) const noexcept {
317  co_dev_get_tpdo_event_ind(this, pind, pdata);
318  }
319 
320  void
321  setTPDOEventInd(co_dev_tpdo_event_ind_t* ind, void* data) noexcept {
322  co_dev_set_tpdo_event_ind(this, ind, data);
323  }
324 
325  template <class F>
326  void
327  setTPDOEventInd(F* f) noexcept {
329  static_cast<void*>(f));
330  }
331 
332  template <class C, typename c_mem_fn<co_dev_tpdo_event_ind_t*, C>::type M>
333  void
334  setTPDOEventInd(C* obj) noexcept {
336  static_cast<void*>(obj));
337  }
338 
339  void
340  TPDOEvent(co_unsigned16_t idx, co_unsigned8_t subidx) noexcept {
341  co_dev_tpdo_event(this, idx, subidx);
342  }
343 
344  protected:
345  ~CODev() = default;
346 };
347 
348 } // namespace lely
349 
350 #endif // !LELY_CO_DEV_HPP_
co_dev_remove_obj
int co_dev_remove_obj(co_dev_t *dev, co_obj_t *obj)
Removes an object from the object dictionary a CANopen device.
Definition: dev.c:273
lely::incomplete_c_type
The base class for a C++ interface to an incomplete C type.
Definition: c_type.hpp:249
lely::COObj
An opaque CANopen object type.
Definition: obj.hpp:74
dev.h
lely::COVal< CO_DEFTYPE_DOMAIN >
A CANopen value containing an arbitrary large block of data.
Definition: val.hpp:343
co_dev_get_vendor_id
co_unsigned32_t co_dev_get_vendor_id(const co_dev_t *dev)
Returns the vendor ID of a CANopen device.
Definition: dev.c:384
co_dev_get_rate
co_unsigned16_t co_dev_get_rate(const co_dev_t *dev)
Returns the (pending) baudrate of a CANopen device (in kbit/s).
Definition: dev.c:508
lely::c_obj_call
Definition: c_call.hpp:62
co_dev_set_val
size_t co_dev_set_val(co_dev_t *dev, co_unsigned16_t idx, co_unsigned8_t subidx, const void *ptr, size_t n)
Sets the current value of a CANopen sub-object.
Definition: dev.c:563
co_dev_set_rate
void co_dev_set_rate(co_dev_t *dev, co_unsigned16_t rate)
Sets the (pending) baudrate of a CANopen device.
Definition: dev.c:516
co_dev_read_sub
size_t co_dev_read_sub(co_dev_t *dev, co_unsigned16_t *pidx, co_unsigned8_t *psubidx, const uint_least8_t *begin, const uint_least8_t *end)
Reads a value from a memory buffer, in the concise DCF format, and stores it in a sub-object in the o...
Definition: dev.c:606
val.hpp
co_dev_get_name
const char * co_dev_get_name(const co_dev_t *dev)
Returns the name of a CANopen device.
Definition: dev.c:324
co_dev_get_id
co_unsigned8_t co_dev_get_id(const co_dev_t *dev)
Returns the node-ID of a CANopen device.
Definition: dev.c:207
co_dev_get_product_name
const char * co_dev_get_product_name(const co_dev_t *dev)
Returns a pointer to the product name of a CANopen device.
Definition: dev.c:400
co_dev_set_product_code
void co_dev_set_product_code(co_dev_t *dev, co_unsigned32_t product_code)
Sets the product code of a CANopen device.
Definition: dev.c:438
co_dev_get_val
const void * co_dev_get_val(const co_dev_t *dev, co_unsigned16_t idx, co_unsigned8_t subidx)
Returns a pointer to the current value of a CANopen sub-object.
Definition: dev.c:556
co_dev_write_dcf_file
int co_dev_write_dcf_file(const co_dev_t *dev, co_unsigned16_t min, co_unsigned16_t max, const char *filename)
Loads the values of a range of objects in the object dictionary of a CANopen device,...
Definition: dev.c:839
co_dev_set_lss
void co_dev_set_lss(co_dev_t *dev, int lss)
Sets the LSS support flag.
Definition: dev.c:532
co_dev_get_revision
co_unsigned32_t co_dev_get_revision(const co_dev_t *dev)
Returns the revision number of a CANopen device.
Definition: dev.c:446
floc
A location in a text file.
Definition: diag.h:31
c_type.hpp
co_dev_tpdo_event
void co_dev_tpdo_event(co_dev_t *dev, co_unsigned16_t idx, co_unsigned8_t subidx)
Checks if the specified sub-object in the object dictionary of a CANopen device can be mapped into a ...
Definition: dev.c:909
co_dev_get_dummy
co_unsigned32_t co_dev_get_dummy(const co_dev_t *dev)
Returns the data types supported by a CANopen device for mapping dummy entries in PDOs (one bit for e...
Definition: dev.c:540
co_dev_get_netid
co_unsigned8_t co_dev_get_netid(const co_dev_t *dev)
Returns the network-ID of a CANopen device.
Definition: dev.c:184
co_dev_get_idx
co_unsigned16_t co_dev_get_idx(const co_dev_t *dev, co_unsigned16_t maxidx, co_unsigned16_t *idx)
Retrieves a list of object indices in the object dictionary of a CANopen device.
Definition: dev.c:233
lely::c_type_traits
A class template supplying a uniform interface to certain attributes of C types.
Definition: c_type.hpp:120
co_dev_get_vendor_name
const char * co_dev_get_vendor_name(const co_dev_t *dev)
Returns a pointer to the vendor name of a CANopen device.
Definition: dev.c:354
co_dev_set_order_code
int co_dev_set_order_code(co_dev_t *dev, const char *order_code)
Sets the order code of a CANopen device.
Definition: dev.c:470
co_dev_set_tpdo_event_ind
void co_dev_set_tpdo_event_ind(co_dev_t *dev, co_dev_tpdo_event_ind_t *ind, void *data)
Sets the indication function invoked by co_dev_tpdo_event() when an event is indicated for (a sub-obj...
Definition: dev.c:899
co_dev_set_revision
void co_dev_set_revision(co_dev_t *dev, co_unsigned32_t revision)
Sets the revision number of a CANopen device.
Definition: dev.c:454
co_sdev
A static CANopen device.
Definition: sdev.h:30
co_dev_set_vendor_name
int co_dev_set_vendor_name(co_dev_t *dev, const char *vendor_name)
Sets the vendor name of a CANopen device.
Definition: dev.c:362
lely::COVal
A CANopen value.
Definition: val.hpp:42
co_dev_get_order_code
const char * co_dev_get_order_code(const co_dev_t *dev)
Returns a pointer to the order code of a CANopen device.
Definition: dev.c:462
lely::c_mem_call
Definition: c_call.hpp:67
co_dev_set_baud
void co_dev_set_baud(co_dev_t *dev, unsigned int baud)
Sets the supported bit rates of a CANopen device.
Definition: dev.c:500
co_dev_set_product_name
int co_dev_set_product_name(co_dev_t *dev, const char *product_name)
Sets the product name of a CANopen device.
Definition: dev.c:408
__co_dev
A CANopen device.
Definition: dev.c:41
co_dev_tpdo_event_ind_t
void co_dev_tpdo_event_ind_t(co_unsigned16_t num, void *data)
The type of a CANopen Transmit-PDO event indication function, invoked by co_dev_tpdo_event() when an ...
Definition: dev.h:97
co_dev_write_dcf
int co_dev_write_dcf(const co_dev_t *dev, co_unsigned16_t min, co_unsigned16_t max, void **ptr)
Loads the values of a range of objects in the object dictionary of a CANopen device,...
Definition: dev.c:783
co_dev_write_sub
size_t co_dev_write_sub(const co_dev_t *dev, co_unsigned16_t idx, co_unsigned8_t subidx, uint_least8_t *begin, uint_least8_t *end)
Loads the value of a sub-object from the object dictionary of a CANopen device, and writes it to a me...
Definition: dev.c:652
c_call.hpp
co_dev_get_lss
int co_dev_get_lss(const co_dev_t *dev)
Returns 1 if LSS is supported and 0 if not.
Definition: dev.c:524
co_dev_set_vendor_id
void co_dev_set_vendor_id(co_dev_t *dev, co_unsigned32_t vendor_id)
Sets the vendor ID of a CANopen device.
Definition: dev.c:392
co_dev_set_name
int co_dev_set_name(co_dev_t *dev, const char *name)
Sets the name of a CANopen device.
Definition: dev.c:332
co_dev_set_netid
int co_dev_set_netid(co_dev_t *dev, co_unsigned8_t id)
Sets the network-ID of a CANopen device.
Definition: dev.c:192
co_dev_set_dummy
void co_dev_set_dummy(co_dev_t *dev, co_unsigned32_t dummy)
Sets the data types supported by a CANopen device for mapping dummy entries in PDOs.
Definition: dev.c:548
co_dev_get_baud
unsigned int co_dev_get_baud(const co_dev_t *dev)
Returns the supported bit rates of a CANopen device (any combination of CO_BAUD_1000,...
Definition: dev.c:492
co_dev_read_dcf
int co_dev_read_dcf(co_dev_t *dev, co_unsigned16_t *pmin, co_unsigned16_t *pmax, void *const *ptr)
Reads the values of a range of objects from a memory buffer, in the concise DCF format,...
Definition: dev.c:688
co_dev_find_sub
co_sub_t * co_dev_find_sub(const co_dev_t *dev, co_unsigned16_t idx, co_unsigned8_t subidx)
Finds a sub-object in the object dictionary of a CANopen device.
Definition: dev.c:299
co_dev_get_tpdo_event_ind
void co_dev_get_tpdo_event_ind(const co_dev_t *dev, co_dev_tpdo_event_ind_t **pind, void **pdata)
Retrieves the indication function invoked by co_dev_tpdo_event() when an event is indicated for (a su...
Definition: dev.c:887
co_dev_set_id
int co_dev_set_id(co_dev_t *dev, co_unsigned8_t id)
Sets the node-ID of a CANopen device.
Definition: dev.c:215
co_dev_insert_obj
int co_dev_insert_obj(co_dev_t *dev, co_obj_t *obj)
Inserts an object into the object dictionary of a CANopen device.
Definition: dev.c:252
co_dev_read_dcf_file
int co_dev_read_dcf_file(co_dev_t *dev, co_unsigned16_t *pmin, co_unsigned16_t *pmax, const char *filename)
Reads the values of a range of objects from a file, in the concise DCF format, and stores them in the...
Definition: dev.c:731
co_dev_get_product_code
co_unsigned32_t co_dev_get_product_code(const co_dev_t *dev)
Returns the product code of a CANopen device.
Definition: dev.c:430
lely::CODev
An opaque CANopen device type.
Definition: dev.hpp:77
lely::COSub
An opaque CANopen sub-object type.
Definition: obj.hpp:229
co_dev_find_obj
co_obj_t * co_dev_find_obj(const co_dev_t *dev, co_unsigned16_t idx)
Finds an object in the object dictionary of a CANopen device.
Definition: dev.c:288