Lely core libraries  1.9.2
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_type.hpp>
30 #include <lely/co/dev.h>
31 #include <lely/co/val.hpp>
32 
33 #include <vector>
34 
35 struct floc;
36 struct co_sdev;
37 
38 namespace lely {
39 
41 template <>
43  typedef __co_dev value_type;
44  typedef value_type& reference;
45  typedef const value_type& const_reference;
46  typedef value_type* pointer;
47  typedef const value_type* const_pointer;
48 
49  static void*
50  alloc() noexcept {
51  return __co_dev_alloc();
52  }
53  static void
54  free(void* ptr) noexcept {
55  __co_dev_free(ptr);
56  }
57 
58  static pointer
59  init(pointer p, co_unsigned8_t id) noexcept {
60  return __co_dev_init(p, id);
61  }
62 
63  static pointer init(pointer p, const char* filename) noexcept;
64  static pointer init(pointer p, const char* begin, const char* end,
65  floc* at) noexcept;
66 
67  static pointer init(pointer p, const co_sdev* sdev) noexcept;
68 
69  static void
70  fini(pointer p) noexcept {
71  __co_dev_fini(p);
72  }
73 };
74 
76 class CODev : public incomplete_c_type<__co_dev> {
78 
79  public:
80  explicit CODev(co_unsigned8_t id = 0xff) : c_base(id) {}
81 
82  explicit CODev(const char* filename) : c_base(filename) {}
83 
84  CODev(const char* begin, const char* end, floc* at = 0)
85  : c_base(begin, end, at) {}
86 
87  explicit CODev(const co_sdev* sdev) : c_base(sdev) {}
88 
89  co_unsigned8_t
90  getNetid() const noexcept {
91  return co_dev_get_netid(this);
92  }
93 
94  int
95  setNetid(co_unsigned8_t id) noexcept {
96  return co_dev_set_netid(this, id);
97  }
98 
99  co_unsigned8_t
100  getId() const noexcept {
101  return co_dev_get_id(this);
102  }
103 
104  int
105  setId(co_unsigned8_t id) noexcept {
106  return co_dev_set_id(this, id);
107  }
108 
109  co_unsigned16_t
110  getIdx(co_unsigned16_t maxidx, co_unsigned16_t* idx) const noexcept {
111  return co_dev_get_idx(this, maxidx, idx);
112  }
113 
114  ::std::vector<co_unsigned16_t>
115  getIdx() const {
116  std::vector<co_unsigned16_t> idx(getIdx(0, 0));
117  getIdx(idx.size(), idx.data());
118  return idx;
119  }
120 
121  int
122  insert(COObj* obj) noexcept {
123  return co_dev_insert_obj(this, obj);
124  }
125  int
126  remove(COObj* obj) noexcept {
127  return co_dev_remove_obj(this, obj);
128  }
129 
130  COObj*
131  find(co_unsigned16_t idx) const noexcept {
132  return co_dev_find_obj(this, idx);
133  }
134 
135  COSub*
136  find(co_unsigned16_t idx, co_unsigned8_t subidx) const noexcept {
137  return co_dev_find_sub(this, idx, subidx);
138  }
139 
140  const char*
141  getName() const noexcept {
142  return co_dev_get_name(this);
143  }
144 
145  int
146  setName(const char* name) noexcept {
147  return co_dev_set_name(this, name);
148  }
149 
150  const char*
151  getVendorName() const noexcept {
152  return co_dev_get_vendor_name(this);
153  }
154 
155  int
156  setVendorName(const char* vendor_name) noexcept {
157  return co_dev_set_vendor_name(this, vendor_name);
158  }
159 
160  co_unsigned32_t
161  getVendorId() const noexcept {
162  return co_dev_get_vendor_id(this);
163  }
164 
165  void
166  setVendorId(co_unsigned32_t vendor_id) noexcept {
167  co_dev_set_vendor_id(this, vendor_id);
168  }
169 
170  const char*
171  getProductName() const noexcept {
172  return co_dev_get_product_name(this);
173  }
174 
175  int
176  setProductName(const char* product_name) noexcept {
177  return co_dev_set_product_name(this, product_name);
178  }
179 
180  co_unsigned32_t
181  getProductCode() const noexcept {
182  return co_dev_get_product_code(this);
183  }
184 
185  void
186  setProductCode(co_unsigned32_t product_code) noexcept {
187  co_dev_set_product_code(this, product_code);
188  }
189 
190  co_unsigned32_t
191  getRevision() const noexcept {
192  return co_dev_get_revision(this);
193  }
194 
195  void
196  setRevision(co_unsigned32_t revision) noexcept {
197  co_dev_set_revision(this, revision);
198  }
199 
200  const char*
201  getOrderCode() const noexcept {
202  return co_dev_get_order_code(this);
203  }
204 
205  int
206  setOrderCode(const char* order_code) noexcept {
207  return co_dev_set_order_code(this, order_code);
208  }
209 
210  unsigned int
211  getBaud() const noexcept {
212  return co_dev_get_baud(this);
213  }
214 
215  void
216  setBaud(unsigned int baud) noexcept {
217  co_dev_set_baud(this, baud);
218  }
219 
220  co_unsigned16_t
221  getRate() const noexcept {
222  return co_dev_get_rate(this);
223  }
224 
225  void
226  setRate(co_unsigned16_t rate) noexcept {
227  co_dev_set_rate(this, rate);
228  }
229 
230  bool
231  getLSS() const noexcept {
232  return !!co_dev_get_lss(this);
233  }
234 
235  void
236  setLSS(bool lss) noexcept {
237  co_dev_set_lss(this, lss);
238  }
239 
240  co_unsigned32_t
241  getDummy() const noexcept {
242  return co_dev_get_dummy(this);
243  }
244 
245  void
246  setDummy(co_unsigned32_t dummy) noexcept {
247  co_dev_set_dummy(this, dummy);
248  }
249 
250  template <co_unsigned16_t N>
251  const COVal<N>&
252  getVal(co_unsigned16_t idx, co_unsigned8_t subidx) const noexcept {
253  return *reinterpret_cast<const COVal<N>*>(
254  co_dev_get_val(this, idx, subidx));
255  }
256 
257  ::std::size_t
258  setVal(co_unsigned16_t idx, co_unsigned8_t subidx, const void* ptr,
259  ::std::size_t n) noexcept {
260  return co_dev_set_val(this, idx, subidx, ptr, n);
261  }
262 
263  template <co_unsigned16_t N>
264  ::std::size_t
265  setVal(co_unsigned16_t idx, co_unsigned8_t subidx,
266  const COVal<N>& val) noexcept {
267  return setVal(idx, subidx, val.address(), val.size());
268  }
269 
270  template <class T>
271  ::std::size_t
272  setVal(co_unsigned16_t idx, co_unsigned8_t subidx, const T& val) noexcept {
273  return setVal<co_type_traits_T<T>::index>(idx, subidx, val);
274  }
275 
276  ::std::size_t
277  readSub(co_unsigned16_t* pidx, co_unsigned8_t* psubidx, const uint8_t* begin,
278  const uint8_t* end) noexcept {
279  return co_dev_read_sub(this, pidx, psubidx, begin, end);
280  }
281 
282  ::std::size_t
283  writeSub(co_unsigned16_t idx, co_unsigned8_t subidx, uint8_t* begin,
284  uint8_t* end = 0) const noexcept {
285  return co_dev_write_sub(this, idx, subidx, begin, end);
286  }
287 
288  int
289  readDCF(co_unsigned16_t* pmin, co_unsigned16_t* pmax,
290  const COVal<CO_DEFTYPE_DOMAIN>& val) noexcept {
291  return co_dev_read_dcf(this, pmin, pmax,
292  reinterpret_cast<void* const*>(&val));
293  }
294 
295  int
296  readDCF(co_unsigned16_t* pmin, co_unsigned16_t* pmax,
297  const char* filename) noexcept {
298  return co_dev_read_dcf_file(this, pmin, pmax, filename);
299  }
300 
301  int
302  writeDCF(co_unsigned16_t min, co_unsigned16_t max,
303  COVal<CO_DEFTYPE_DOMAIN>& val) const noexcept {
304  return co_dev_write_dcf(this, min, max, reinterpret_cast<void**>(&val));
305  }
306 
307  int
308  writeDCF(co_unsigned16_t min, co_unsigned16_t max, const char* filename) const
309  noexcept {
310  return co_dev_write_dcf_file(this, min, max, filename);
311  }
312 
313  protected:
314  ~CODev() {}
315 };
316 
317 } // namespace lely
318 
319 #endif // !LELY_CO_DEV_HPP_
void co_dev_set_revision(co_dev_t *dev, co_unsigned32_t revision)
Sets the revision number of a CANopen device.
Definition: dev.c:427
void co_dev_set_lss(co_dev_t *dev, int lss)
Sets the LSS support flag.
Definition: dev.c:505
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:521
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, and writes them to a memory buffer, in the concise DCF format.
Definition: dev.c:776
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:513
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, and stores them in the object dictionary of a CANopen device.
Definition: dev.c:682
A location in a text file.
Definition: diag.h:31
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:264
const char * co_dev_get_name(const co_dev_t *dev)
Returns the name of a CANopen device.
Definition: dev.c:297
co_unsigned8_t co_dev_get_id(const co_dev_t *dev)
Returns the node-ID of a CANopen device.
Definition: dev.c:198
The base class for a C++ interface to an incomplete C type.
Definition: c_type.hpp:245
An opaque CANopen device type.
Definition: dev.hpp:76
size_t co_dev_read_sub(co_dev_t *dev, co_unsigned16_t *pidx, co_unsigned8_t *psubidx, const uint8_t *begin, const uint8_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:580
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:411
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:335
co_unsigned32_t co_dev_get_vendor_id(const co_dev_t *dev)
Returns the vendor ID of a CANopen device.
Definition: dev.c:357
This header file is part of the CANopen library; it contains the C++ interface of the CANopen value d...
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:539
An opaque CANopen object type.
Definition: obj.hpp:74
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:529
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:373
int co_dev_set_netid(co_dev_t *dev, co_unsigned8_t id)
Sets the network-ID of a CANopen device.
Definition: dev.c:183
This header file is part of the utilities library; it contains the C to C++ interface declarations...
A static CANopen device.
Definition: sdev.h:30
An opaque CANopen sub-object type.
Definition: obj.hpp:229
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:243
co_unsigned8_t co_dev_get_netid(const co_dev_t *dev)
Returns the network-ID of a CANopen device.
Definition: dev.c:175
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:443
co_unsigned32_t co_dev_get_revision(const co_dev_t *dev)
Returns the revision number of a CANopen device.
Definition: dev.c:419
size_t co_dev_write_sub(const co_dev_t *dev, co_unsigned16_t idx, co_unsigned8_t subidx, uint8_t *begin, uint8_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:638
void co_dev_set_rate(co_dev_t *dev, co_unsigned16_t rate)
Sets the (pending) baudrate of a CANopen device.
Definition: dev.c:489
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, CO_BAUD_800, CO_BAUD_500, CO_BAUD_250, CO_BAUD_125, CO_BAUD_50, CO_BAUD_20, CO_BAUD_10 and CO_BAUD_AUTO).
Definition: dev.c:465
A CANopen device.
Definition: dev.c:38
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:224
This header file is part of the CANopen library; it contains the device description declarations...
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:435
A CANopen value containing an arbitrary large block of data.
Definition: val.hpp:333
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:327
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:381
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:481
int co_dev_set_id(co_dev_t *dev, co_unsigned8_t id)
Sets the node-ID of a CANopen device.
Definition: dev.c:206
void co_dev_set_baud(co_dev_t *dev, unsigned int baud)
Sets the supported bit rates of a CANopen device.
Definition: dev.c:473
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:279
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:725
Global namespace for the Lely Industries N.V. libraries.
Definition: buf.hpp:32
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, and writes them to a file, in the concise DCF format.
Definition: dev.c:851
int co_dev_set_name(co_dev_t *dev, const char *name)
Sets the name of a CANopen device.
Definition: dev.c:305
int co_dev_get_lss(const co_dev_t *dev)
Returns 1 if LSS is supported and 0 if not.
Definition: dev.c:497
A CANopen value.
Definition: val.hpp:42
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:365
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:290
co_unsigned32_t co_dev_get_product_code(const co_dev_t *dev)
Returns the product code of a CANopen device.
Definition: dev.c:403
A class template supplying a uniform interface to certain attributes of C types.
Definition: c_type.hpp:120