Lely core libraries  2.3.4
val.h
Go to the documentation of this file.
1 
22 #ifndef LELY_CO_VAL_H_
23 #define LELY_CO_VAL_H_
24 
25 #include <lely/co/type.h>
26 #include <lely/util/util.h>
27 
28 #include <float.h>
29 #include <stddef.h>
30 
31 #if !LELY_NO_CO_DCF || !LELY_NO_CO_OBJ_FILE
32 // The read file buffer from <lely/util/frbuf.h>
33 struct __frbuf;
34 // The write file buffer from <lely/util/fwbuf.h>
35 struct __fwbuf;
36 #endif
37 
39 #define CO_BOOLEAN_INIT 0
40 
42 #define CO_BOOLEAN_MIN 0
43 
45 #define CO_BOOLEAN_MAX 1
46 
48 #define CO_INTEGER8_INIT 0
49 
51 #define CO_INTEGER8_MIN (-INT8_C(0x7f) - 1)
52 
54 #define CO_INTEGER8_MAX INT8_C(0x7f)
55 
57 #define CO_INTEGER16_INIT 0
58 
60 #define CO_INTEGER16_MIN (-INT16_C(0x7fff) - 1)
61 
63 #define CO_INTEGER16_MAX INT16_C(0x7fff)
64 
66 #define CO_INTEGER32_INIT 0
67 
69 #define CO_INTEGER32_MIN (-INT32_C(0x7fffffff) - 1)
70 
72 #define CO_INTEGER32_MAX INT32_C(0x7fffffff)
73 
75 #define CO_UNSIGNED8_INIT 0
76 
78 #define CO_UNSIGNED8_MIN 0
79 
81 #define CO_UNSIGNED8_MAX UINT8_C(0xff)
82 
84 #define CO_UNSIGNED16_INIT 0
85 
87 #define CO_UNSIGNED16_MIN 0
88 
90 #define CO_UNSIGNED16_MAX UINT16_C(0xffff)
91 
93 #define CO_UNSIGNED32_INIT 0
94 
96 #define CO_UNSIGNED32_MIN 0
97 
99 #define CO_UNSIGNED32_MAX UINT32_C(0xffffffff)
100 
102 #define CO_REAL32_INIT 0
103 
105 #define CO_REAL32_MIN (-FLT_MAX)
106 
108 #define CO_REAL32_MAX FLT_MAX
109 
111 #define CO_VISIBLE_STRING_INIT NULL
112 
113 // The "minimum value" of an array of visible characters.
114 #define CO_VISIBLE_STRING_MIN CO_VISIBLE_STRING_INIT
115 
116 // The "maximum value" of an array of visible characters.
117 #define CO_VISIBLE_STRING_MAX CO_VISIBLE_STRING_INIT
118 
120 #define CO_OCTET_STRING_INIT NULL
121 
122 // The "minimum value" of an array of octets.
123 #define CO_OCTET_STRING_MIN CO_OCTET_STRING_INIT
124 
125 // The "maximum value" of an array of octets.
126 #define CO_OCTET_STRING_MAX CO_OCTET_STRING_INIT
127 
129 #define CO_UNICODE_STRING_INIT NULL
130 
131 // The "minimum value" of an array of (16-bit) Unicode characters.
132 #define CO_UNICODE_STRING_MIN CO_UNICODE_STRING_INIT
133 
134 // The "maximum value" of an array of (16-bit) Unicode characters.
135 #define CO_UNICODE_STRING_MAX CO_UNICODE_STRING_INIT
136 
138 #define CO_TIME_OF_DAY_INIT \
139  { \
140  0, 0 \
141  }
142 
144 #define CO_TIME_OF_DAY_MIN \
145  { \
146  0, 0 \
147  }
148 
150 #define CO_TIME_OF_DAY_MAX \
151  { \
152  UINT32_C(0x0fffffff), UINT16_C(0xffff) \
153  }
154 
156 #define CO_TIME_DIFF_INIT CO_TIME_OF_DAY_INIT
157 
159 #define CO_TIME_DIFF_MIN CO_TIME_OF_DAY_MIN
160 
162 #define CO_TIME_DIFF_MAX CO_TIME_OF_DAY_MAX
163 
165 #define CO_DOMAIN_INIT NULL
166 
167 // The "minimum value" of an arbitrary large block of data..
168 #define CO_DOMAIN_MIN CO_DOMAIN_INIT
169 
170 // The "maximum value" of an arbitrary large block of data..
171 #define CO_DOMAIN_MAX CO_DOMAIN_INIT
172 
174 #define CO_INTEGER24_INIT 0
175 
177 #define CO_INTEGER24_MIN (-INT32_C(0x007fffff) - 1)
178 
180 #define CO_INTEGER24_MAX INT32_C(0x007fffff)
181 
183 #define CO_REAL64_INIT 0
184 
186 #define CO_REAL64_MIN (-DBL_MAX)
187 
189 #define CO_REAL64_MAX DBL_MAX
190 
192 #define CO_INTEGER40_INIT 0
193 
195 #define CO_INTEGER40_MIN (-INT64_C(0x0000007fffffffff) - 1)
196 
198 #define CO_INTEGER40_MAX INT64_C(0x0000007fffffffff)
199 
201 #define CO_INTEGER48_INIT 0
202 
204 #define CO_INTEGER48_MIN (-INT64_C(0x00007fffffffffff) - 1)
205 
207 #define CO_INTEGER48_MAX INT64_C(0x00007fffffffffff)
208 
210 #define CO_INTEGER56_INIT 0
211 
213 #define CO_INTEGER56_MIN (-INT64_C(0x007fffffffffffff) - 1)
214 
216 #define CO_INTEGER56_MAX INT64_C(0x007fffffffffffff)
217 
219 #define CO_INTEGER64_INIT 0
220 
222 #define CO_INTEGER64_MIN (-INT64_C(0x7fffffffffffffff) - 1)
223 
225 #define CO_INTEGER64_MAX INT64_C(0x7fffffffffffffff)
226 
228 #define CO_UNSIGNED24_INIT 0
229 
231 #define CO_UNSIGNED24_MIN 0
232 
234 #define CO_UNSIGNED24_MAX UINT32_C(0x00ffffff)
235 
237 #define CO_UNSIGNED40_INIT 0
238 
240 #define CO_UNSIGNED40_MIN 0
241 
243 #define CO_UNSIGNED40_MAX UINT64_C(0x000000ffffffffff)
244 
246 #define CO_UNSIGNED48_INIT 0
247 
249 #define CO_UNSIGNED48_MIN 0
250 
252 #define CO_UNSIGNED48_MAX UINT64_C(0x0000ffffffffffff)
253 
255 #define CO_UNSIGNED56_INIT 0
256 
258 #define CO_UNSIGNED56_MIN 0
259 
261 #define CO_UNSIGNED56_MAX UINT64_C(0x00ffffffffffffff)
262 
264 #define CO_UNSIGNED64_INIT 0
265 
267 #define CO_UNSIGNED64_MIN 0
268 
270 #define CO_UNSIGNED64_MAX UINT64_C(0xffffffffffffffff)
271 
273 union co_val {
274 #define LELY_CO_DEFINE_TYPE(a, b, c, d) co_##b##_t c;
275 #include <lely/co/def/type.def>
276 #undef LELY_CO_DEFINE_TYPE
277 };
278 
280 struct co_array_hdr {
282  size_t capacity;
284  size_t size;
285 };
286 
287 #ifndef CO_ARRAY_CAPACITY
288 #if LELY_NO_MALLOC
290 #define CO_ARRAY_CAPACITY 256
291 #else
292 #define CO_ARRAY_CAPACITY 0
293 #endif
294 #endif
295 
296 #if LELY_NO_MALLOC
297 
299 struct co_array {
301  struct co_array_hdr hdr;
302  union {
304  char data[CO_ARRAY_CAPACITY];
305  // Ensure the correct alignment.
306  union co_val _val;
307  } u;
308 };
309 
311 #define CO_ARRAY_INIT \
312  { \
313  { CO_ARRAY_CAPACITY, 0 }, \
314  { \
315  { \
316  0 \
317  } \
318  } \
319  }
320 
321 #endif // LELY_NO_MALLOC
322 
323 #if __STDC_VERSION__ >= 199901L
324 
325 #if LELY_NO_MALLOC
326 
330 #define CO_ARRAY_C ((void *)((struct co_array)CO_ARRAY_INIT).u.data)
331 #endif
332 
333 #define _CO_ARRAY(...) __VA_ARGS__
334 
339 // clang-format off
340 #define CO_VISIBLE_STRING_NC(n, c) \
341  (((struct { \
342  struct co_array_hdr hdr; \
343  union { \
344  char vs[MAX(n, sizeof(c))]; \
345  union co_val val; \
346  } u; \
347  }){ \
348  .hdr = { \
349  .capacity = MAX(n, sizeof(c)), \
350  .size = sizeof(c) - 1 \
351  }, \
352  .u = { .vs = c } \
353  }).u.vs)
354 // clang-format on
355 
357 #define CO_VISIBLE_STRING_C(c) CO_VISIBLE_STRING_NC(CO_ARRAY_CAPACITY, c)
358 
363 // clang-format off
364 #define CO_OCTET_STRING_NC(n, c) \
365  (((struct { \
366  struct co_array_hdr hdr; \
367  union { \
368  uint_least8_t os[MAX(n, sizeof(c))]; \
369  union co_val val; \
370  } u; \
371  }){ \
372  .hdr = { \
373  .capacity = MAX(n, sizeof(c)), \
374  .size = sizeof(c) - 1 \
375  }, \
376  .u = { .os = c } \
377  }).u.os)
378 // clang-format on
379 
381 #define CO_OCTET_STRING_C(c) CO_OCTET_STRING_NC(CO_ARRAY_CAPACITY, c)
382 
387 #define CO_UNICODE_STRING_NC(n, ...) \
388  _CO_UNICODE_STRING_NC(n, _CO_ARRAY(__VA_ARGS__))
389 
390 // clang-format off
391 #define _CO_UNICODE_STRING_NC(n, c) \
392  (((struct { \
393  struct co_array_hdr hdr; \
394  union { \
395  char16_t us[MAX(ALIGN(n, sizeof(char16_t)), \
396  sizeof((char16_t[])c)) \
397  / sizeof(char16_t)]; \
398  union co_val val; \
399  } u; \
400  }){ \
401  .hdr = { \
402  .capacity = MAX(ALIGN(n, sizeof(char16_t)), \
403  sizeof((char16_t[])c)), \
404  .size = sizeof((char16_t[])c) - sizeof(char16_t) \
405  }, \
406  .u = { .us = c } \
407  }).u.us)
408 // clang-format on
409 
411 #define CO_UNICODE_STRING_C(...) \
412  _CO_UNICODE_STRING_NC(CO_ARRAY_CAPACITY, _CO_ARRAY(__VA_ARGS__))
413 
418 #define CO_DOMAIN_NC(type, n, ...) \
419  _CO_DOMAIN_NC(type, n, _CO_ARRAY(__VA_ARGS__))
420 
421 // clang-format off
422 #define _CO_DOMAIN_NC(type, n, c) \
423  (((struct { \
424  struct co_array_hdr hdr; \
425  union { \
426  type dom[MAX(ALIGN(n, sizeof(type)), sizeof((type[])c)) \
427  / sizeof(type)]; \
428  union co_val val; \
429  } u; \
430  }){ \
431  .hdr = { \
432  .capacity = MAX(ALIGN(n, sizeof(type)), \
433  sizeof((type[])c)), \
434  .size = sizeof((type[])c) \
435  }, \
436  .u = { .dom = c } \
437  }).u.dom)
438 // clang-format on
439 
444 #define CO_DOMAIN_C(type, ...) \
445  _CO_DOMAIN_NC(type, CO_ARRAY_CAPACITY, _CO_ARRAY(__VA_ARGS__))
446 
447 #endif // __STDC_VERSION__ >= 199901L
448 
449 // The file location struct from <lely/util/diag.h>.
450 struct floc;
451 
452 #ifdef __cplusplus
453 extern "C" {
454 #endif
455 
469 int co_val_init(co_unsigned16_t type, void *val);
470 
485 int co_val_init_min(co_unsigned16_t type, void *val);
486 
501 int co_val_init_max(co_unsigned16_t type, void *val);
502 
516 int co_val_init_vs(char **val, const char *vs);
517 
534 int co_val_init_vs_n(char **val, const char *vs, size_t n);
535 
551 int co_val_init_os(uint_least8_t **val, const uint_least8_t *os, size_t n);
552 
567 int co_val_init_us(char16_t **val, const char16_t *us);
568 
587 int co_val_init_us_n(char16_t **val, const char16_t *us, size_t n);
588 
604 int co_val_init_dom(void **val, const void *dom, size_t n);
605 
606 #if LELY_NO_MALLOC
607 static inline void co_val_init_array(void *val, struct co_array *array);
609 #endif
610 
622 void co_val_fini(co_unsigned16_t type, void *val);
623 
636 const void *co_val_addressof(co_unsigned16_t type, const void *val);
637 
649 size_t co_val_sizeof(co_unsigned16_t type, const void *val);
650 
667 size_t co_val_make(co_unsigned16_t type, void *val, const void *ptr, size_t n);
668 
687 size_t co_val_copy(co_unsigned16_t type, void *dst, const void *src);
688 
708 size_t co_val_move(co_unsigned16_t type, void *dst, void *src);
709 
723 int co_val_cmp(co_unsigned16_t type, const void *v1, const void *v2);
724 
745 size_t co_val_read(co_unsigned16_t type, void *val, const uint_least8_t *begin,
746  const uint_least8_t *end);
747 
748 #if !LELY_NO_STDIO
749 
767 size_t co_val_read_file(co_unsigned16_t type, void *val, const char *filename);
768 
787 size_t co_val_read_frbuf(co_unsigned16_t type, void *val, struct __frbuf *buf);
788 
789 #endif // !LELY_NO_STDIO
790 
808 co_unsigned32_t co_val_read_sdo(
809  co_unsigned16_t type, void *val, const void *ptr, size_t n);
810 
831 size_t co_val_write(co_unsigned16_t type, const void *val, uint_least8_t *begin,
832  uint_least8_t *end);
833 
834 #if !LELY_NO_STDIO
835 
850 size_t co_val_write_file(
851  co_unsigned16_t type, const void *val, const char *filename);
852 
868 size_t co_val_write_fwbuf(
869  co_unsigned16_t type, const void *val, struct __fwbuf *buf);
870 
891 size_t co_val_lex(co_unsigned16_t type, void *val, const char *begin,
892  const char *end, struct floc *at);
893 
912 size_t co_val_print(co_unsigned16_t type, const void *val, char **pbegin,
913  char *end);
914 
915 #endif // !LELY_NO_STDIO
916 
917 #if LELY_NO_MALLOC
918 static inline void
919 co_val_init_array(void *val, struct co_array *array)
920 {
921  if (val)
922  *(char **)val = array ? array->u.data : NULL;
923 }
924 #endif
925 
926 #ifdef __cplusplus
927 }
928 #endif
929 
930 #endif // !LELY_CO_VAL_H_
co_val_read
size_t co_val_read(co_unsigned16_t type, void *val, const uint_least8_t *begin, const uint_least8_t *end)
Reads a value of the specified data type from a memory buffer.
Definition: val.c:451
co_val_copy
size_t co_val_copy(co_unsigned16_t type, void *dst, const void *src)
Copies one value to another.
Definition: val.c:318
co_val_init_max
int co_val_init_max(co_unsigned16_t type, void *val)
Initializes a value of the specified data type with its upper limit.
Definition: val.c:118
co_val_read_file
size_t co_val_read_file(co_unsigned16_t type, void *val, const char *filename)
Reads a value of the specified data type from a file.
Definition: val.c:671
co_val_fini
void co_val_fini(co_unsigned16_t type, void *val)
Finalizes a value of the specified data type.
Definition: val.c:249
co_val_init_vs_n
int co_val_init_vs_n(char **val, const char *vs, size_t n)
Initializes an array of visible characters (CO_DEFTYPE_VISIBLE_STRING).
Definition: val.c:157
float.h
co_val_init_min
int co_val_init_min(co_unsigned16_t type, void *val)
Initializes a value of the specified data type with its lower limit.
Definition: val.c:90
util.h
co_val_addressof
const void * co_val_addressof(co_unsigned16_t type, const void *val)
Returns the address of the first byte in a value of the specified data type.
Definition: val.c:260
co_val_move
size_t co_val_move(co_unsigned16_t type, void *dst, void *src)
Moves one value to another.
Definition: val.c:354
co_val_write_fwbuf
size_t co_val_write_fwbuf(co_unsigned16_t type, const void *val, struct __fwbuf *buf)
Writes a value of the specified data type to the current position in a write file buffer.
Definition: val.c:964
co_array_hdr::size
size_t size
The current size (in bytes).
Definition: val.h:284
type.h
floc
A location in a text file.
Definition: diag.h:39
co_val_init_vs
int co_val_init_vs(char **val, const char *vs)
Initializes an array of visible characters (CO_DEFTYPE_VISIBLE_STRING).
Definition: val.c:146
co_val_print
size_t co_val_print(co_unsigned16_t type, const void *val, char **pbegin, char *end)
Prints a value of the specified data type to a memory buffer.
Definition: val.c:1490
co_val_read_frbuf
size_t co_val_read_frbuf(co_unsigned16_t type, void *val, struct __frbuf *buf)
Reads a value of the specified data type from the current position in a read file buffer.
Definition: val.c:692
co_val_read_sdo
co_unsigned32_t co_val_read_sdo(co_unsigned16_t type, void *val, const void *ptr, size_t n)
Reads a value of the specified data type from an SDO buffer.
Definition: val.c:767
co_val_sizeof
size_t co_val_sizeof(co_unsigned16_t type, const void *val)
Returns the size (in bytes) of a value of the specified data type.
Definition: val.c:269
CO_ARRAY_CAPACITY
#define CO_ARRAY_CAPACITY
The default capacity (in bytes) of a statically allocated CANopen array.
Definition: val.h:292
__frbuf
An read file buffer struct.
Definition: frbuf.c:52
co_val_make
size_t co_val_make(co_unsigned16_t type, void *val, const void *ptr, size_t n)
Constructs a value of the specified data type.
Definition: val.c:282
co_array_hdr::capacity
size_t capacity
The total capacity (in bytes).
Definition: val.h:282
co_val_init_us
int co_val_init_us(char16_t **val, const char16_t *us)
Initializes an array of (16-bit) Unicode characters (CO_DEFTYPE_UNICODE_STRING).
Definition: val.c:198
__fwbuf
An (atomic) write file buffer struct.
Definition: fwbuf.c:59
co_val_init_os
int co_val_init_os(uint_least8_t **val, const uint_least8_t *os, size_t n)
Initializes an array of octets (CO_DEFTYPE_OCTET_STRING).
Definition: val.c:179
co_val_cmp
int co_val_cmp(co_unsigned16_t type, const void *v1, const void *v2)
Compares two values of the specified data type.
Definition: val.c:369
co_val
A union of the CANopen static data types.
Definition: val.h:273
co_val_lex
size_t co_val_lex(co_unsigned16_t type, void *val, const char *begin, const char *end, struct floc *at)
Lexes a value of the specified data type from a memory buffer.
Definition: val.c:1010
co_val_init_dom
int co_val_init_dom(void **val, const void *dom, size_t n)
Initializes an arbitrary large block of data (CO_DEFTYPE_DOMAIN).
Definition: val.c:230
co_val_init
int co_val_init(co_unsigned16_t type, void *val)
Initializes a value of the specified data type to zero.
Definition: val.c:62
floc::filename
const char * filename
The name of the file.
Definition: diag.h:41
co_val_write_file
size_t co_val_write_file(co_unsigned16_t type, const void *val, const char *filename)
Writes a value of the specified data type to a file.
Definition: val.c:943
stddef.h
co_val_init_us_n
int co_val_init_us_n(char16_t **val, const char16_t *us, size_t n)
Initializes an array of (16-bit) Unicode characters (CO_DEFTYPE_UNICODE_STRING).
Definition: val.c:211
co_val_write
size_t co_val_write(co_unsigned16_t type, const void *val, uint_least8_t *begin, uint_least8_t *end)
Writes a value of the specified data type to a memory buffer.
Definition: val.c:791
co_array_hdr
The header directly preceding the bytes in a CANopen array.
Definition: val.h:280