options.h
Go to the documentation of this file.
1 /*
2  * SPDX-FileCopyrightText: 2024-2025 Carl Seifert
3  * SPDX-FileCopyrightText: 2024-2025 TU Dresden
4  * SPDX-License-Identifier: LGPL-2.1-only
5  */
6 
7 #pragma once
8 
9 #include <stdint.h>
10 #include <stdbool.h>
11 #include <string.h>
12 #include <assert.h>
13 #include <errno.h>
14 #include <sys/types.h>
15 
16 #include "byteorder.h"
17 
18 #include "net/unicoap/constants.h"
20 
59 #ifdef __cplusplus
60 extern "C" {
61 #endif
62 
63 /* MARK: - Essentials */
71 typedef struct {
76  uint8_t* data;
77 
81  uint16_t size;
82 
88 
97 typedef struct {
98  /* TODO: OSCORE: read-only due to options stored in two different buffers */
99 
103  size_t storage_size;
104 
111 
119 
125  size_t option_count;
127 
136 static inline void unicoap_options_init(unicoap_options_t* options, uint8_t* storage,
137  size_t capacity)
138 {
139  options->entries->data = storage;
140  options->option_count = 0;
141  options->storage_size = 0;
142  options->storage_capacity = capacity;
143 }
144 
157 
164 static inline void unicoap_options_clear(unicoap_options_t* options)
165 {
166  options->option_count = 0;
167  options->storage_size = 0;
168 }
171 /* MARK: - Allocating option buffers */
176 #ifndef DOXYGEN
177 # define _UNICOAP_OPTIONS_ALLOC(_buf, _name, capacity, _static) \
178  _static uint8_t _buf[capacity]; \
179  _static unicoap_options_t _name = { \
180  .entries = { { .data = _buf } }, \
181  .storage_capacity = capacity, \
182  };
183 #endif
184 
197 #define UNICOAP_OPTIONS_ALLOC(name, capacity) \
198  _UNICOAP_OPTIONS_ALLOC(_CONCAT3(name, _storage, __LINE__), name, capacity,)
199 
212 #define UNICOAP_OPTIONS_ALLOC_STATIC(name, capacity) \
213  _UNICOAP_OPTIONS_ALLOC(_CONCAT3(name, _storage, __LINE__), name, capacity, static)
214 
226 #define UNICOAP_OPTIONS_ALLOC_DEFAULT(name) \
227  UNICOAP_OPTIONS_ALLOC(name, CONFIG_UNICOAP_OPTIONS_BUFFER_DEFAULT_CAPACITY)
228 
240 #define UNICOAP_OPTIONS_ALLOC_STATIC_DEFAULT(name) \
241  UNICOAP_OPTIONS_ALLOC_STATIC(name, CONFIG_UNICOAP_OPTIONS_BUFFER_DEFAULT_CAPACITY)
244 /* MARK: - Option characteristics */
259 static inline bool unicoap_option_is_critical(unicoap_option_number_t option_number)
260 {
261  return option_number & 1;
262 }
263 
273 {
274  return (option_number & 2) == 0;
275 }
276 
288 {
289  return ((option_number & 0x1e) == 0x1c);
290 }
291 
302 static inline bool unicoap_option_is_cache_key(unicoap_option_number_t option_number)
303 {
304  return ((option_number & 0x1e) != 0x1c);
305 }
308 /* MARK: - Tools */
319 
332 ssize_t unicoap_options_swap_storage(unicoap_options_t* options, uint8_t* destination,
333  size_t capacity);
334 
342 static inline uint8_t* unicoap_options_data(const unicoap_options_t* options)
343 {
344  return options->entries->data;
345 }
346 
354 static inline size_t unicoap_options_size(const unicoap_options_t* options)
355 {
356  return options->storage_size;
357 }
360 /* MARK: - Auxiliary constants */
366 #if !defined(EBADOPT) || defined(DOXYGEN)
367 # define EBADOPT 151
368 #endif
369 
371 #if !defined(EPAYLD) || defined(DOXYGEN)
372 # define EPAYLD 152
373 #endif
374 
376 #define UNICOAP_UINT24_MAX (0xffffff)
377 
379 #define UNICOAP_UINT24_SIZE (3)
380 
387 #define UNICOAP_UINT_MAX (14 + 255 + 0xffff)
390 /* MARK: - Generic Option Methods - */
397 /* MARK: - Repeatable options */
420  const uint8_t* value, size_t value_size);
421 
443  const uint8_t* buffer, size_t size, uint8_t separator);
444 
465  unicoap_option_number_t number, uint8_t* buffer,
466  size_t capacity, uint8_t separator);
467 
484 /* MARK: - Non-repeatable options */
504  const uint8_t** value);
505 
521  uint8_t* dest, size_t capacity);
522 
536  const uint8_t* value, size_t value_size);
537 
550 {
551  /* reuse unicoap_options_remove_all, helps decrease binary size */
552  return unicoap_options_remove_all(options, number);
553 }
556 /* MARK: - Enumerating options */
579 typedef struct {
584 
588  size_t index;
590 
599  unicoap_options_t* options)
600 {
601  assert(iterator);
602  assert(options);
603  assert(options->entries->data);
604  iterator->options = options;
605  iterator->index = 0;
606 }
607 
623  unicoap_option_number_t* number, const uint8_t** value);
624 
641  unicoap_option_number_t number, const uint8_t** value);
642 
669  unicoap_option_number_t number, const char* name,
670  const char** value);
671 
681 /* MARK: - Strings */
702  unicoap_option_number_t number, const char* string,
703  size_t count)
704 {
705  return unicoap_options_set(options, number, (uint8_t*)string,
706  ((count > 0) && string) ? count : strlen(string));
707 }
708 
728  unicoap_option_number_t number, const char* string,
729  size_t count)
730 {
731  return unicoap_options_add(options, number, (uint8_t*)string,
732  ((count > 0) && string) ? count : strlen(string));
733 }
736 /* MARK: - Unsigned integers */
741 #ifndef DOXYGEN
758 ssize_t _unicoap_options_get_variable_uint(const unicoap_options_t* options,
759  unicoap_option_number_t number, uint32_t* uint,
760  size_t max_size);
761 #endif
762 
780 static inline ssize_t unicoap_options_get_uint32(const unicoap_options_t* options,
781  unicoap_option_number_t number, uint32_t* uint)
782 {
783  return _unicoap_options_get_variable_uint(options, number, uint, sizeof(uint32_t));
784 }
785 
803 static inline ssize_t unicoap_options_get_uint24(const unicoap_options_t* options,
804  unicoap_option_number_t number, uint32_t* uint)
805 {
806  return _unicoap_options_get_variable_uint(options, number, uint, UNICOAP_UINT24_SIZE);
807 }
808 
826 static inline ssize_t unicoap_options_get_uint16(const unicoap_options_t* options,
827  unicoap_option_number_t number, uint16_t* uint)
828 {
829  uint32_t _uint = 0;
830  int res = _unicoap_options_get_variable_uint(options, number, &_uint, sizeof(uint16_t));
831  *uint = (uint16_t)_uint;
832  return res;
833 }
834 
853 static inline ssize_t unicoap_options_get_uint8(const unicoap_options_t* options,
854  unicoap_option_number_t number, uint8_t* uint)
855 {
856  return unicoap_options_copy_value(options, number, uint, sizeof(uint8_t));
857 }
858 
874  uint32_t value);
875 
894  uint32_t value);
898 /* MARK: - Predefined Options - */
899 
907 /* MARK: - Uri-Host */
924 static inline ssize_t unicoap_options_get_uri_host(const unicoap_options_t* options,
925  const char** host)
926 {
927  return unicoap_options_get(options, UNICOAP_OPTION_URI_HOST, (const uint8_t**)host);
928 }
929 
945 static inline ssize_t unicoap_options_set_uri_host(unicoap_options_t* options, char* host,
946  size_t length)
947 {
948  assert(length > 0 && length <= 255);
949  return unicoap_options_set(options, UNICOAP_OPTION_URI_HOST, (uint8_t*)host, length);
950 }
951 
968 static inline ssize_t unicoap_options_set_uri_host_string(unicoap_options_t* options, char* host)
969 {
970  return unicoap_options_set_uri_host(options, host, strlen(host));
971 }
972 
982 {
984 } /* Uri-Host */
986 
987 /* MARK: - If-None-Match */
1003 static inline bool unicoap_options_get_if_none_match(const unicoap_options_t* options)
1004 {
1006 }
1007 
1018 static inline ssize_t unicoap_options_set_if_none_match(unicoap_options_t* options, bool value)
1019 {
1020  if (value) {
1022  }
1023  else {
1024  return unicoap_options_set(options, UNICOAP_OPTION_IF_NONE_MATCH, NULL, 0);
1025  }
1026 } /* If-None-Match */
1028 
1029 /* MARK: - Uri-Port */
1046 static inline ssize_t unicoap_options_get_uri_port(const unicoap_options_t* options, uint16_t* port)
1047 {
1048  return unicoap_options_get_uint16(options, UNICOAP_OPTION_URI_PORT, port);
1049 }
1050 
1061 static inline ssize_t unicoap_options_set_uri_port(unicoap_options_t* options, uint16_t port)
1062 {
1063  return unicoap_options_set_uint(options, UNICOAP_OPTION_URI_PORT, (uint32_t)port);
1064 }
1065 
1075 {
1077 } /* Uri-Port */
1079 
1080 /* MARK: - Content-Format */
1097 static inline ssize_t unicoap_options_get_content_format(const unicoap_options_t* options,
1098  unicoap_content_format_t* format)
1099 {
1100  return unicoap_options_get_uint16(options, UNICOAP_OPTION_CONTENT_FORMAT, (uint16_t*)format);
1101 }
1102 
1114  unicoap_content_format_t format)
1115 {
1116  return unicoap_options_set_uint(options, UNICOAP_OPTION_CONTENT_FORMAT, (uint32_t)format);
1117 }
1118 
1128 {
1130 } /* Content-Format */
1132 
1133 /* MARK: - Max-Age */
1150 static inline ssize_t unicoap_options_get_max_age(const unicoap_options_t* options, uint32_t* age)
1151 {
1152  return unicoap_options_get_uint32(options, UNICOAP_OPTION_MAX_AGE, age);
1153 }
1154 
1165 static inline ssize_t unicoap_options_set_max_age(unicoap_options_t* options, uint32_t age)
1166 {
1167  return unicoap_options_set_uint(options, UNICOAP_OPTION_MAX_AGE, age);
1168 }
1169 
1179 {
1181 } /* Max-Age */
1183 
1184 /* MARK: - Accept */
1201 static inline ssize_t unicoap_options_get_accept(const unicoap_options_t* options,
1202  unicoap_content_format_t* format)
1203 {
1204  return unicoap_options_get_uint16(options, UNICOAP_OPTION_ACCEPT, (uint16_t*)format);
1205 }
1206 
1217 static inline ssize_t unicoap_options_set_accept(unicoap_options_t* options,
1218  unicoap_content_format_t format)
1219 {
1220  return unicoap_options_set_uint(options, UNICOAP_OPTION_ACCEPT, (uint32_t)format);
1221 }
1222 
1232 {
1234 } /* Accept */
1236 
1237 /* MARK: - Proxy-Scheme */
1254 static inline ssize_t unicoap_options_get_proxy_scheme(const unicoap_options_t* options,
1255  const char** scheme)
1256 {
1257  return unicoap_options_get(options, UNICOAP_OPTION_PROXY_SCHEME, (const uint8_t**)scheme);
1258 }
1259 
1273 static inline ssize_t unicoap_options_set_proxy_scheme(unicoap_options_t* options, char* scheme,
1274  size_t length)
1275 {
1276  return unicoap_options_set(options, UNICOAP_OPTION_PROXY_SCHEME, (uint8_t*)scheme, length);
1277 }
1278 
1294  char* scheme)
1295 {
1296  return unicoap_options_set_proxy_scheme(options, scheme, strlen(scheme));
1297 }
1298 
1308 {
1310 } /* Proxy-Scheme */
1312 
1313 /* MARK: - Proxy-Uri */
1330 static inline ssize_t unicoap_options_get_proxy_uri(const unicoap_options_t* options,
1331  const char** uri)
1332 {
1333  return unicoap_options_get(options, UNICOAP_OPTION_PROXY_URI, (const uint8_t**)uri);
1334 }
1335 
1351 static inline ssize_t unicoap_options_set_proxy_uri(unicoap_options_t* options, char* uri,
1352  size_t length)
1353 {
1354  assert(length > 0 && length <= 1034);
1355  return unicoap_options_set(options, UNICOAP_OPTION_PROXY_URI, (uint8_t*)uri, length);
1356 }
1357 
1374 static inline ssize_t unicoap_options_set_proxy_uri_string(unicoap_options_t* options, char* uri)
1375 {
1376  return unicoap_options_set_proxy_uri(options, uri, strlen(uri));
1377 }
1378 
1388 {
1390 } /* Proxy-Uri */
1392 
1393 /* MARK: - No-Response */
1410 static inline ssize_t unicoap_options_get_no_response(const unicoap_options_t* options,
1411  uint8_t* value)
1412 {
1413  return unicoap_options_get_uint8(options, UNICOAP_OPTION_NO_RESPONSE, value);
1414 }
1415 
1426 static inline ssize_t unicoap_options_set_no_response(unicoap_options_t* options, uint8_t value)
1427 {
1428  return unicoap_options_set_uint(options, UNICOAP_OPTION_NO_RESPONSE, (uint32_t)value);
1429 }
1430 
1440 {
1442 } /* No-Response */
1444 
1445 /* MARK: - If-Match */
1462 static inline ssize_t unicoap_options_get_first_if_match(const unicoap_options_t* options,
1463  const uint8_t** value)
1464 {
1465  return unicoap_options_get(options, UNICOAP_OPTION_IF_MATCH, value);
1466 }
1467 
1482  const uint8_t** value)
1483 {
1485 }
1486 
1500 static inline ssize_t unicoap_options_add_if_match(unicoap_options_t* options, uint8_t* value,
1501  size_t size)
1502 {
1503  assert(size <= 8);
1504  return unicoap_options_add(options, UNICOAP_OPTION_IF_MATCH, value, size);
1505 }
1506 
1516 {
1518 } /* If-Match */
1520 
1521 /* MARK: - ETag */
1538 static inline ssize_t unicoap_options_get_first_etag(const unicoap_options_t* options,
1539  const uint8_t** etag)
1540 {
1541  return unicoap_options_get(options, UNICOAP_OPTION_ETAG, etag);
1542 }
1543 
1558  const uint8_t** etag)
1559 {
1561 }
1562 
1577 static inline ssize_t unicoap_options_add_etag(unicoap_options_t* options, uint8_t* etag,
1578  size_t size)
1579 {
1580  assert(size > 0 && size <= 8);
1581  return unicoap_options_add(options, UNICOAP_OPTION_ETAG, etag, size);
1582 }
1583 
1593 {
1595 } /* ETag */
1597 
1598 /* MARK: - Location-Path */
1615 static inline ssize_t
1617  const char** component)
1618 {
1619  return unicoap_options_get(options, UNICOAP_OPTION_LOCATION_PATH, (const uint8_t**)component);
1620 }
1621 
1635 static inline ssize_t
1637  const char** component)
1638 {
1640  (const uint8_t**)component);
1641 }
1642 
1662 static inline ssize_t unicoap_options_copy_location_path(const unicoap_options_t* options,
1663  char* path, size_t capacity)
1664 {
1665  assert(capacity > 0);
1666  *path = '/';
1667  path += 1;
1668  capacity -= 1;
1670  (uint8_t*)path, capacity, '/');
1671  return res < 0 ? res : res + 1;
1672 }
1673 
1688  char* component, size_t length)
1689 {
1690  return unicoap_options_add(options, UNICOAP_OPTION_LOCATION_PATH, (uint8_t*)component, length);
1691 }
1692 
1708  char* component)
1709 {
1710  return unicoap_options_add_location_path_component(options, component, strlen(component));
1711 }
1712 
1728 static inline ssize_t unicoap_options_add_location_path(unicoap_options_t* options, char* path,
1729  size_t length)
1730 {
1731  return unicoap_options_add_values_joined(options, UNICOAP_OPTION_LOCATION_PATH, (uint8_t*)path,
1732  length, '/');
1733 }
1734 
1752  char* path)
1753 {
1754  return unicoap_options_add_location_path(options, path, strlen(path));
1755 }
1756 
1766 {
1768 } /* Location-Path */
1770 
1771 /* MARK: - Uri-Path */
1789  const char** component)
1790 {
1791  return unicoap_options_get(options, UNICOAP_OPTION_URI_PATH, (const uint8_t**)component);
1792 }
1793 
1807 static inline ssize_t
1809  const char** component)
1810 {
1812  (const uint8_t**)component);
1813 }
1814 
1834 static inline ssize_t unicoap_options_copy_uri_path(const unicoap_options_t* options, char* path,
1835  size_t capacity)
1836 {
1837  assert(capacity > 0);
1838  *path = '/';
1839  path += 1;
1840  capacity -= 1;
1841  int res = unicoap_options_copy_values_joined(options, UNICOAP_OPTION_URI_PATH, (uint8_t*)path,
1842  capacity, '/');
1843  return res < 0 ? res : res + 1;
1844 }
1845 
1861  char* component, size_t length)
1862 {
1863  assert(length <= 255);
1864  return unicoap_options_add(options, UNICOAP_OPTION_URI_PATH, (uint8_t*)component, length);
1865 }
1866 
1883  char* component)
1884 {
1885  return unicoap_options_add_uri_path_component(options, component, strlen(component));
1886 }
1887 
1904 static inline ssize_t unicoap_options_add_uri_path(unicoap_options_t* options, char* path,
1905  size_t length)
1906 {
1907  return unicoap_options_add_values_joined(options, UNICOAP_OPTION_URI_PATH, (uint8_t*)path,
1908  length, '/');
1909 }
1910 
1928 static inline ssize_t unicoap_options_add_uri_path_string(unicoap_options_t* options, char* path)
1929 {
1930  return unicoap_options_add_uri_path(options, path, strlen(path));
1931 }
1932 
1942 {
1944 } /* Uri-Path */
1946 
1947 /* MARK: - Uri-Query */
1964 static inline ssize_t unicoap_options_get_first_uri_query(const unicoap_options_t* options,
1965  const char** query)
1966 {
1967  return unicoap_options_get(options, UNICOAP_OPTION_URI_QUERY, (const uint8_t**)query);
1968 }
1969 
1984  const char** query)
1985 {
1987  (const uint8_t**)query);
1988 }
1989 
2007 static inline ssize_t
2009  const char** value)
2010 {
2012 }
2013 
2033  const char* name,
2034  const char** value)
2035 {
2036  unicoap_options_iterator_t iterator;
2037  unicoap_options_iterator_init(&iterator, options);
2038  return unicoap_options_get_next_uri_query_by_name(&iterator, name, value);
2039 }
2040 
2057 static inline ssize_t unicoap_options_copy_uri_queries(const unicoap_options_t* options,
2058  char* queries, size_t capacity)
2059 {
2060  return unicoap_options_copy_values_joined(options, UNICOAP_OPTION_URI_QUERY, (uint8_t*)queries,
2061  capacity, '&');
2062 }
2063 
2078 static inline ssize_t unicoap_options_add_uri_query(unicoap_options_t* options, char* query,
2079  size_t length)
2080 {
2081  assert(length <= 255);
2082  return unicoap_options_add(options, UNICOAP_OPTION_URI_QUERY, (uint8_t*)query, length);
2083 }
2084 
2100 static inline ssize_t unicoap_options_add_uri_query_string(unicoap_options_t* options, char* query)
2101 {
2102  return unicoap_options_add_uri_query(options, query, strlen(query));
2103 }
2104 
2121 static inline ssize_t unicoap_options_add_uri_queries(unicoap_options_t* options, char* queries,
2122  size_t length)
2123 {
2124  return unicoap_options_add_values_joined(options, UNICOAP_OPTION_URI_QUERY, (uint8_t*)queries,
2125  length, '&');
2126 }
2127 
2146  char* queries)
2147 {
2148  return unicoap_options_add_uri_queries(options, queries, strlen(queries));
2149 }
2150 
2160 {
2162 } /* Uri-Query */
2164 
2165 /* MARK: - Location-Query */
2183  const char** query)
2184 {
2185  return unicoap_options_get(options, UNICOAP_OPTION_LOCATION_QUERY, (const uint8_t**)query);
2186 }
2187 
2202  const char** query)
2203 {
2205  (const uint8_t**)query);
2206 }
2207 
2225 static inline ssize_t
2227  const char* name, const char** value)
2228 {
2230  value);
2231 }
2232 
2252  const char* name,
2253  const char** value)
2254 {
2255  unicoap_options_iterator_t iterator;
2256  unicoap_options_iterator_init(&iterator, options);
2257  return unicoap_options_get_next_location_query_by_name(&iterator, name, value);
2258 }
2259 
2276 static inline ssize_t unicoap_options_copy_location_queries(const unicoap_options_t* options,
2277  char* queries, size_t capacity)
2278 {
2280  (uint8_t*)queries, capacity, '&');
2281 }
2282 
2297 static inline ssize_t unicoap_options_add_location_query(unicoap_options_t* options, char* query,
2298  size_t length)
2299 {
2300  assert(length <= 255);
2301  return unicoap_options_add(options, UNICOAP_OPTION_LOCATION_QUERY, (uint8_t*)query, length);
2302 }
2303 
2320  char* query)
2321 {
2322  return unicoap_options_add_location_query(options, query, strlen(query));
2323 }
2324 
2342  char* queries, size_t length)
2343 {
2345  (uint8_t*)queries, length, '&');
2346 }
2347 
2366  char* queries)
2367 {
2368  return unicoap_options_add_location_queries(options, queries, strlen(queries));
2369 }
2370 
2380 {
2382 } /* Location-Query */
2384 
2385 /* MARK: - Observe */
2392 #define UNICOAP_OBSERVE_OPTION_REGISTER (0)
2393 
2395 #define UNICOAP_OBSERVE_OPTION_DEREGISTER (1)
2396 
2408 static inline ssize_t unicoap_options_get_observe(const unicoap_options_t* options,
2409  uint32_t* observe)
2410 {
2411  return unicoap_options_get_uint24(options, UNICOAP_OPTION_OBSERVE, observe);
2412 }
2413 
2424 static inline ssize_t unicoap_options_set_observe(unicoap_options_t* options, uint32_t observe)
2425 {
2426  return unicoap_options_set_uint(options, UNICOAP_OPTION_OBSERVE, observe);
2427 }
2428 
2438 {
2440 }
2441 
2452  /* Observe */
2454 
2455 /* MARK: - Block-wise Transfers */
2466 #define UNICOAP_BLOCK_OPTION_NONE (0x0f000000)
2467 
2471 typedef uint32_t unicoap_block_option_t;
2472 
2485 static inline ssize_t unicoap_options_get_block(const unicoap_options_t* options,
2486  unicoap_option_number_t number,
2487  unicoap_block_option_t* block)
2488 {
2489  return unicoap_options_get_uint24(options, number, block);
2490 }
2491 
2504  unicoap_option_number_t number,
2505  unicoap_block_option_t block)
2506 {
2507  assert(block <= UNICOAP_UINT24_MAX);
2508  return unicoap_options_set_uint(options, number, block);
2509 }
2510 
2522 static inline ssize_t unicoap_options_get_block1(const unicoap_options_t* options,
2523  unicoap_block_option_t* block)
2524 {
2525  return unicoap_options_get_block(options, UNICOAP_OPTION_BLOCK1, block);
2526 }
2527 
2538 static inline ssize_t unicoap_options_set_block1(unicoap_options_t* options,
2539  unicoap_block_option_t block)
2540 {
2541  return unicoap_options_set_block(options, UNICOAP_OPTION_BLOCK1, block);
2542 }
2543 
2553 {
2555 }
2556 
2568 static inline ssize_t unicoap_options_get_block2(const unicoap_options_t* options,
2569  unicoap_block_option_t* block)
2570 {
2571  return unicoap_options_get_block(options, UNICOAP_OPTION_BLOCK2, block);
2572 }
2573 
2584 static inline ssize_t unicoap_options_set_block2(unicoap_options_t* options,
2585  unicoap_block_option_t block)
2586 {
2587  return unicoap_options_set_block(options, UNICOAP_OPTION_BLOCK2, block);
2588 }
2589 
2599 {
2601 }
2602 
2614 static inline ssize_t unicoap_options_get_size1(const unicoap_options_t* options, uint32_t* size)
2615 {
2616  return unicoap_options_get_uint32(options, UNICOAP_OPTION_SIZE1, size);
2617 }
2618 
2629 static inline ssize_t unicoap_options_set_size1(unicoap_options_t* options, uint32_t size)
2630 {
2631  return unicoap_options_set_uint(options, UNICOAP_OPTION_SIZE1, size);
2632 }
2633 
2643 {
2645 }
2646 
2658 static inline ssize_t unicoap_options_get_size2(const unicoap_options_t* options, uint32_t* size)
2659 {
2660  return unicoap_options_get_uint32(options, UNICOAP_OPTION_SIZE2, size);
2661 }
2662 
2673 static inline ssize_t unicoap_options_set_size2(unicoap_options_t* options, uint32_t size)
2674 {
2675  return unicoap_options_set_uint(options, UNICOAP_OPTION_SIZE2, size);
2676 }
2677 
2687 {
2689 } /* Block-wise Transfers */
2691 
2694 #ifdef __cplusplus
2695 }
2696 #endif
2697 
POSIX.1-2008 compliant version of the assert macro.
#define assert(cond)
abort the program if assertion is false
Definition: assert.h:143
Functions to work with different byte orders.
Constants used in CoAP such as option numbers and message codes.
#define CONFIG_UNICOAP_OPTIONS_MAX
Maximum number of options that can be present in a request or response.
Definition: config.h:53
static ssize_t unicoap_options_get_uint8(const unicoap_options_t *options, unicoap_option_number_t number, uint8_t *uint)
Retrieves an unsigned option value that takes up at most one bytes.
Definition: options.h:853
static ssize_t unicoap_options_get_uint16(const unicoap_options_t *options, unicoap_option_number_t number, uint16_t *uint)
Retrieves an unsigned option value that takes up at most 2 bytes.
Definition: options.h:826
ssize_t unicoap_options_copy_values_joined(const unicoap_options_t *options, unicoap_option_number_t number, uint8_t *buffer, size_t capacity, uint8_t separator)
Copies the values of all options with the given number joined by the given separator.
ssize_t unicoap_options_get(const unicoap_options_t *options, unicoap_option_number_t number, const uint8_t **value)
Retrieves the value of the option with given value, if present.
void unicoap_options_dump_all(const unicoap_options_t *options)
Iterates and dumps all options using printf
int unicoap_options_remove_all(unicoap_options_t *options, unicoap_option_number_t number)
Removes all options with the given number, if any.
static void unicoap_options_iterator_init(unicoap_options_iterator_t *iterator, unicoap_options_t *options)
Initializes the given iterator structure.
Definition: options.h:598
static ssize_t unicoap_options_get_uint24(const unicoap_options_t *options, unicoap_option_number_t number, uint32_t *uint)
Retrieves an unsigned option value that takes up at most 3 bytes.
Definition: options.h:803
ssize_t unicoap_options_copy_value(const unicoap_options_t *options, unicoap_option_number_t number, uint8_t *dest, size_t capacity)
Copies the value of the option with given value, if present, into a buffer.
int unicoap_options_add(unicoap_options_t *options, unicoap_option_number_t number, const uint8_t *value, size_t value_size)
Adds a repeatable option with the given value.
ssize_t unicoap_options_get_next(unicoap_options_iterator_t *iterator, unicoap_option_number_t *number, const uint8_t **value)
Gets the next option provided by the given iterator.
int unicoap_options_set(unicoap_options_t *options, unicoap_option_number_t number, const uint8_t *value, size_t value_size)
Sets the option with the given number.
int unicoap_options_set_uint(unicoap_options_t *options, unicoap_option_number_t number, uint32_t value)
Sets the option with the given number to the unsigned integer value passed.
ssize_t unicoap_options_get_next_by_number(unicoap_options_iterator_t *iterator, unicoap_option_number_t number, const uint8_t **value)
Gets the next option with the given number, potentially skipping any options in between.
static int unicoap_options_remove(unicoap_options_t *options, unicoap_option_number_t number)
Removes option with the given number, if present.
Definition: options.h:549
static int unicoap_options_add_string(unicoap_options_t *options, unicoap_option_number_t number, const char *string, size_t count)
Adds a repeatable option with the given string value.
Definition: options.h:727
ssize_t unicoap_options_get_next_query_by_name(unicoap_options_iterator_t *iterator, unicoap_option_number_t number, const char *name, const char **value)
Gets the next query option matching the given name, potentially skipping any options in between.
int unicoap_options_add_uint(unicoap_options_t *options, unicoap_option_number_t number, uint32_t value)
Adds a repeatable option with the given unsigned integer value.
static int unicoap_options_set_string(unicoap_options_t *options, unicoap_option_number_t number, const char *string, size_t count)
Sets a non-repeatable option to the given string value.
Definition: options.h:701
static ssize_t unicoap_options_get_uint32(const unicoap_options_t *options, unicoap_option_number_t number, uint32_t *uint)
Retrieves an unsigned option value that takes up at most 4 bytes.
Definition: options.h:780
int unicoap_options_add_values_joined(unicoap_options_t *options, unicoap_option_number_t number, const uint8_t *buffer, size_t size, uint8_t separator)
Splits the given value into separate values and adds them as option values.
static ssize_t unicoap_options_add_uri_path_string(unicoap_options_t *options, char *path)
Adds multiple Uri-Path options from null-terminated string.
Definition: options.h:1928
uint32_t unicoap_block_option_t
Block1 and Block2 option value
Definition: options.h:2471
static ssize_t unicoap_options_get_proxy_scheme(const unicoap_options_t *options, const char **scheme)
Retrieves the Proxy-Scheme option, if present.
Definition: options.h:1254
static ssize_t unicoap_options_set_size2(unicoap_options_t *options, uint32_t size)
Sets the Size2 option.
Definition: options.h:2673
static ssize_t unicoap_options_get_first_uri_query(const unicoap_options_t *options, const char **query)
Retrieves the first Uri-Query option, if present.
Definition: options.h:1964
static ssize_t unicoap_options_set_proxy_scheme_string(unicoap_options_t *options, char *scheme)
Sets the Proxy-Scheme option from null-terminated string.
Definition: options.h:1293
static int unicoap_options_remove_observe(unicoap_options_t *options)
Removes the Observe option, if present.
Definition: options.h:2437
static ssize_t unicoap_options_copy_location_path(const unicoap_options_t *options, char *path, size_t capacity)
Copies absolute location path into the given buffer.
Definition: options.h:1662
static ssize_t unicoap_options_set_if_none_match(unicoap_options_t *options, bool value)
Sets the If-None-Match option.
Definition: options.h:1018
static int unicoap_options_remove_block1(unicoap_options_t *options)
Removes the Block1 option, if present.
Definition: options.h:2552
static int unicoap_options_remove_location_queries(unicoap_options_t *options)
Removes all Location-Query options, if any.
Definition: options.h:2379
static ssize_t unicoap_options_add_uri_query_string(unicoap_options_t *options, char *query)
Adds Uri-Query option from null-terminated string.
Definition: options.h:2100
static ssize_t unicoap_options_set_uri_port(unicoap_options_t *options, uint16_t port)
Sets the Uri-Port option.
Definition: options.h:1061
static ssize_t unicoap_options_set_proxy_uri(unicoap_options_t *options, char *uri, size_t length)
Sets the Proxy-Uri option.
Definition: options.h:1351
static ssize_t unicoap_options_set_proxy_uri_string(unicoap_options_t *options, char *uri)
Sets the Proxy-Uri option from null-terminated string.
Definition: options.h:1374
static ssize_t unicoap_options_set_proxy_scheme(unicoap_options_t *options, char *scheme, size_t length)
Sets the Proxy-Scheme option.
Definition: options.h:1273
static int unicoap_options_remove_accept(unicoap_options_t *options)
Removes the Accept option, if present.
Definition: options.h:1231
static ssize_t unicoap_options_set_accept(unicoap_options_t *options, unicoap_content_format_t format)
Sets the Accept option.
Definition: options.h:1217
static ssize_t unicoap_options_add_location_queries(unicoap_options_t *options, char *queries, size_t length)
Adds multiple Location-Query options from string.
Definition: options.h:2341
static int unicoap_options_set_block(unicoap_options_t *options, unicoap_option_number_t number, unicoap_block_option_t block)
Sets the Block1 or Block2 option.
Definition: options.h:2503
static ssize_t unicoap_options_add_location_path_component_string(unicoap_options_t *options, char *component)
Adds Location-Path option from null-terminated string.
Definition: options.h:1707
static ssize_t unicoap_options_add_uri_queries(unicoap_options_t *options, char *queries, size_t length)
Adds multiple Uri-Query options from string.
Definition: options.h:2121
static ssize_t unicoap_options_add_if_match(unicoap_options_t *options, uint8_t *value, size_t size)
Adds If-Match option.
Definition: options.h:1500
static ssize_t unicoap_options_add_uri_query(unicoap_options_t *options, char *query, size_t length)
Adds Uri-Query option.
Definition: options.h:2078
static ssize_t unicoap_options_set_no_response(unicoap_options_t *options, uint8_t value)
Sets the No-Response option.
Definition: options.h:1426
static ssize_t unicoap_options_set_block1(unicoap_options_t *options, unicoap_block_option_t block)
Sets the Block1 option.
Definition: options.h:2538
static ssize_t unicoap_options_add_location_path(unicoap_options_t *options, char *path, size_t length)
Adds multiple Location-Path options from string.
Definition: options.h:1728
static ssize_t unicoap_options_set_observe(unicoap_options_t *options, uint32_t observe)
Sets the Observe option.
Definition: options.h:2424
static ssize_t unicoap_options_get_block(const unicoap_options_t *options, unicoap_option_number_t number, unicoap_block_option_t *block)
Retrieves a Block1 or Block2 option.
Definition: options.h:2485
static ssize_t unicoap_options_get_max_age(const unicoap_options_t *options, uint32_t *age)
Retrieves the Max-Age option, if present.
Definition: options.h:1150
static int unicoap_options_remove_max_age(unicoap_options_t *options)
Removes the Max-Age option, if present.
Definition: options.h:1178
static ssize_t unicoap_options_add_location_path_component(unicoap_options_t *options, char *component, size_t length)
Adds Location-Path option.
Definition: options.h:1687
static ssize_t unicoap_options_get_first_etag(const unicoap_options_t *options, const uint8_t **etag)
Retrieves the first ETag option, if present.
Definition: options.h:1538
static ssize_t unicoap_options_get_block2(const unicoap_options_t *options, unicoap_block_option_t *block)
Retrieves the Block2 option, if present.
Definition: options.h:2568
static ssize_t unicoap_options_copy_location_queries(const unicoap_options_t *options, char *queries, size_t capacity)
Copies location query string into the given buffer.
Definition: options.h:2276
static ssize_t unicoap_options_add_uri_path(unicoap_options_t *options, char *path, size_t length)
Adds multiple Uri-Path options from string.
Definition: options.h:1904
static ssize_t unicoap_options_get_first_location_query(const unicoap_options_t *options, const char **query)
Retrieves the first Location-Query option, if present.
Definition: options.h:2182
unicoap_content_format_t
Content-Format option values
Definition: constants.h:710
static ssize_t unicoap_options_set_uri_host_string(unicoap_options_t *options, char *host)
Sets the Uri-Host option from null-terminated string.
Definition: options.h:968
static ssize_t unicoap_options_get_first_if_match(const unicoap_options_t *options, const uint8_t **value)
Retrieves the first If-Match option, if present.
Definition: options.h:1462
static int unicoap_options_remove_no_response(unicoap_options_t *options)
Removes the No-Response option, if present.
Definition: options.h:1439
static ssize_t unicoap_options_add_location_query_string(unicoap_options_t *options, char *query)
Adds Location-Query option from null-terminated string.
Definition: options.h:2319
static ssize_t unicoap_options_get_next_uri_query(unicoap_options_iterator_t *iterator, const char **query)
Gets the next Uri-Query option provided by the specified iterator.
Definition: options.h:1983
static int unicoap_options_remove_all_if_match(unicoap_options_t *options)
Removes all If-Match options, if any.
Definition: options.h:1515
static int unicoap_options_remove_uri_host(unicoap_options_t *options)
Removes the Uri-Host option, if present.
Definition: options.h:981
static ssize_t unicoap_options_get_first_uri_path_component(const unicoap_options_t *options, const char **component)
Retrieves the first Uri-Path option, if present.
Definition: options.h:1788
static int unicoap_options_remove_content_format(unicoap_options_t *options)
Removes the Content-Format option, if present.
Definition: options.h:1127
static ssize_t unicoap_options_set_max_age(unicoap_options_t *options, uint32_t age)
Sets the Max-Age option.
Definition: options.h:1165
static ssize_t unicoap_options_get_no_response(const unicoap_options_t *options, uint8_t *value)
Retrieves the No-Response option, if present.
Definition: options.h:1410
static ssize_t unicoap_options_set_block2(unicoap_options_t *options, unicoap_block_option_t block)
Sets the Block2 option.
Definition: options.h:2584
static ssize_t unicoap_options_get_next_location_path_component(unicoap_options_iterator_t *iterator, const char **component)
Gets the next Location-Path option provided by the specified iterator.
Definition: options.h:1636
static ssize_t unicoap_options_get_next_etag(unicoap_options_iterator_t *iterator, const uint8_t **etag)
Gets the next ETag option provided by the specified iterator.
Definition: options.h:1557
static ssize_t unicoap_options_get_next_if_match(unicoap_options_iterator_t *iterator, const uint8_t **value)
Gets the next If-Match option provided by the specified iterator.
Definition: options.h:1481
static ssize_t unicoap_options_get_uri_port(const unicoap_options_t *options, uint16_t *port)
Retrieves the Uri-Port option, if present.
Definition: options.h:1046
static ssize_t unicoap_options_get_next_location_query_by_name(unicoap_options_iterator_t *iterator, const char *name, const char **value)
Gets the next Location-Query option matching the given name, potentially skipping any options in betw...
Definition: options.h:2226
static int unicoap_options_remove_size2(unicoap_options_t *options)
Removes the Size2 option, if present.
Definition: options.h:2686
int unicoap_options_set_observe_generated(unicoap_options_t *options)
Sets the Observe option to a randomly generated value.
static int unicoap_options_remove_uri_path(unicoap_options_t *options)
Removes all Uri-Path options, if any.
Definition: options.h:1941
static ssize_t unicoap_options_get_size1(const unicoap_options_t *options, uint32_t *size)
Retrieves the Size1 option, if present.
Definition: options.h:2614
static bool unicoap_options_get_if_none_match(const unicoap_options_t *options)
Determines whether the If-None-Match option is present.
Definition: options.h:1003
static ssize_t unicoap_options_copy_uri_queries(const unicoap_options_t *options, char *queries, size_t capacity)
Copies URI query string into the given buffer.
Definition: options.h:2057
static ssize_t unicoap_options_add_location_query(unicoap_options_t *options, char *query, size_t length)
Adds Location-Query option.
Definition: options.h:2297
static ssize_t unicoap_options_get_size2(const unicoap_options_t *options, uint32_t *size)
Retrieves the Size2 option, if present.
Definition: options.h:2658
static ssize_t unicoap_options_get_accept(const unicoap_options_t *options, unicoap_content_format_t *format)
Retrieves the Accept option, if present.
Definition: options.h:1201
static ssize_t unicoap_options_add_uri_path_component(unicoap_options_t *options, char *component, size_t length)
Adds Uri-Path option.
Definition: options.h:1860
static ssize_t unicoap_options_get_next_uri_query_by_name(unicoap_options_iterator_t *iterator, const char *name, const char **value)
Gets the next Uri-Query option matching the given name, potentially skipping any options in between.
Definition: options.h:2008
static ssize_t unicoap_options_get_next_location_query(unicoap_options_iterator_t *iterator, const char **query)
Gets the next Location-Query option provided by the specified iterator.
Definition: options.h:2201
static int unicoap_options_remove_etags(unicoap_options_t *options)
Removes all ETag options, if any.
Definition: options.h:1592
static ssize_t unicoap_options_copy_uri_path(const unicoap_options_t *options, char *path, size_t capacity)
Copies absolute URI path into the given buffer.
Definition: options.h:1834
static ssize_t unicoap_options_add_etag(unicoap_options_t *options, uint8_t *etag, size_t size)
Adds ETag option.
Definition: options.h:1577
static ssize_t unicoap_options_get_content_format(const unicoap_options_t *options, unicoap_content_format_t *format)
Retrieves the Content-Format option, if present.
Definition: options.h:1097
static ssize_t unicoap_options_get_first_uri_query_by_name(unicoap_options_t *options, const char *name, const char **value)
Retrieves the first Uri-Query option matching the given name, if present.
Definition: options.h:2032
static ssize_t unicoap_options_get_first_location_query_by_name(unicoap_options_t *options, const char *name, const char **value)
Retrieves the first Location-Query option matching the given name, if present.
Definition: options.h:2251
static int unicoap_options_remove_proxy_scheme(unicoap_options_t *options)
Removes the Proxy-Scheme option, if present.
Definition: options.h:1307
static ssize_t unicoap_options_add_location_queries_string(unicoap_options_t *options, char *queries)
Adds multiple Location-Query options from null-terminated string.
Definition: options.h:2365
static int unicoap_options_remove_uri_queries(unicoap_options_t *options)
Removes all Uri-Query options, if any.
Definition: options.h:2159
static ssize_t unicoap_options_add_uri_queries_string(unicoap_options_t *options, char *queries)
Adds multiple Uri-Query options from null-terminated string.
Definition: options.h:2145
static ssize_t unicoap_options_add_uri_path_component_string(unicoap_options_t *options, char *component)
Adds Uri-Path option from null-terminated string.
Definition: options.h:1882
static ssize_t unicoap_options_get_block1(const unicoap_options_t *options, unicoap_block_option_t *block)
Retrieves the Block1 option, if present.
Definition: options.h:2522
static ssize_t unicoap_options_get_proxy_uri(const unicoap_options_t *options, const char **uri)
Retrieves the Proxy-Uri option, if present.
Definition: options.h:1330
static ssize_t unicoap_options_get_uri_host(const unicoap_options_t *options, const char **host)
Retrieves the Uri-Host option, if present.
Definition: options.h:924
static int unicoap_options_remove_location_path(unicoap_options_t *options)
Removes all Location-Path options, if any.
Definition: options.h:1765
static ssize_t unicoap_options_add_location_path_string(unicoap_options_t *options, char *path)
Adds multiple Location-Path options from null-terminated string.
Definition: options.h:1751
static ssize_t unicoap_options_set_content_format(unicoap_options_t *options, unicoap_content_format_t format)
Sets the Content-Format option.
Definition: options.h:1113
static int unicoap_options_remove_block2(unicoap_options_t *options)
Removes the Block2 option, if present.
Definition: options.h:2598
static ssize_t unicoap_options_get_next_uri_path_component(unicoap_options_iterator_t *iterator, const char **component)
Gets the next Uri-Path option provided by the specified iterator.
Definition: options.h:1808
static int unicoap_options_remove_size1(unicoap_options_t *options)
Removes the Size1 option, if present.
Definition: options.h:2642
static ssize_t unicoap_options_set_uri_host(unicoap_options_t *options, char *host, size_t length)
Sets the Uri-Host option.
Definition: options.h:945
static ssize_t unicoap_options_get_observe(const unicoap_options_t *options, uint32_t *observe)
Retrieves the Observe option, if present.
Definition: options.h:2408
static ssize_t unicoap_options_get_first_location_path_component(const unicoap_options_t *options, const char **component)
Retrieves the first Location-Path option, if present.
Definition: options.h:1616
static ssize_t unicoap_options_set_size1(unicoap_options_t *options, uint32_t size)
Sets the Size1 option.
Definition: options.h:2629
static int unicoap_options_remove_uri_port(unicoap_options_t *options)
Removes the Uri-Port option, if present.
Definition: options.h:1074
static int unicoap_options_remove_proxy_uri(unicoap_options_t *options)
Removes the Proxy-Uri option, if present.
Definition: options.h:1387
unicoap_option_number_t
CoAP option number.
Definition: constants.h:416
#define UNICOAP_UINT24_SIZE
Size in bytes of number representable with 24 bits (3 bytes)
Definition: options.h:379
static uint8_t * unicoap_options_data(const unicoap_options_t *options)
Retrieves storage buffer.
Definition: options.h:342
static void unicoap_options_clear(unicoap_options_t *options)
Removes all options.
Definition: options.h:164
static size_t unicoap_options_size(const unicoap_options_t *options)
Retrieves total size of options in buffer.
Definition: options.h:354
static bool unicoap_option_is_no_cache_key(unicoap_option_number_t option_number)
Determines whether the given option is not intended to be part of the cache key.
Definition: options.h:287
static bool unicoap_option_is_critical(unicoap_option_number_t option_number)
Determines whether the given option is considered critical.
Definition: options.h:259
static void unicoap_options_init(unicoap_options_t *options, uint8_t *storage, size_t capacity)
Initializes the given options structure.
Definition: options.h:136
ssize_t unicoap_options_swap_storage(unicoap_options_t *options, uint8_t *destination, size_t capacity)
Copies storage and adjusts options struct.
static bool unicoap_option_is_safe_to_forward(unicoap_option_number_t option_number)
Determines whether the given option is safe to forward.
Definition: options.h:272
const char * unicoap_string_from_option_number(unicoap_option_number_t number)
Returns label of option corresponding to the given number.
bool unicoap_options_contains(const unicoap_options_t *options, unicoap_option_number_t number)
Determines whether the given options container has one or more options with the given number.
#define UNICOAP_UINT24_MAX
Largest number representable with 24 bits (3 bytes)
Definition: options.h:376
static bool unicoap_option_is_cache_key(unicoap_option_number_t option_number)
Determines whether the given option is intended to be part of the cache key.
Definition: options.h:302
@ UNICOAP_OPTION_URI_PORT
Uri-Port
Definition: constants.h:457
@ UNICOAP_OPTION_MAX_AGE
Max-Age option
Definition: constants.h:495
@ UNICOAP_OPTION_URI_HOST
Uri-Host option
Definition: constants.h:429
@ UNICOAP_OPTION_CONTENT_FORMAT
Content-Format option
Definition: constants.h:488
@ UNICOAP_OPTION_OBSERVE
Observe option
Definition: constants.h:450
@ UNICOAP_OPTION_IF_MATCH
If-Match option
Definition: constants.h:422
@ UNICOAP_OPTION_NO_RESPONSE
suppress CoAP response
Definition: constants.h:629
@ UNICOAP_OPTION_PROXY_URI
Proxy-Uri option
Definition: constants.h:592
@ UNICOAP_OPTION_BLOCK2
Block2 option
Definition: constants.h:555
@ UNICOAP_OPTION_SIZE2
Size2 option.
Definition: constants.h:576
@ UNICOAP_OPTION_URI_PATH
Uri-Path option
Definition: constants.h:481
@ UNICOAP_OPTION_BLOCK1
Block1 option
Definition: constants.h:564
@ UNICOAP_OPTION_ETAG
ETag option
Definition: constants.h:436
@ UNICOAP_OPTION_LOCATION_QUERY
Location-Query option
Definition: constants.h:536
@ UNICOAP_OPTION_IF_NONE_MATCH
If-None-Match option
Definition: constants.h:443
@ UNICOAP_OPTION_LOCATION_PATH
Location-Path option
Definition: constants.h:464
@ UNICOAP_OPTION_SIZE1
Size1 option.
Definition: constants.h:613
@ UNICOAP_OPTION_PROXY_SCHEME
Proxy-Scheme option
Definition: constants.h:599
@ UNICOAP_OPTION_URI_QUERY
Uri-Query option
Definition: constants.h:504
@ UNICOAP_OPTION_ACCEPT
Accept option
Definition: constants.h:520
Helper struct for options parser.
Definition: options.h:71
uint16_t size
Number of bytes this option is comprised of.
Definition: options.h:81
uint8_t * data
Pointer into options storage where this option starts.
Definition: options.h:76
unicoap_option_number_t number
Option number.
Definition: options.h:86
The iterator you use to retrieve option values in-order.
Definition: options.h:579
size_t index
Current option's index.
Definition: options.h:588
unicoap_options_t * options
Options.
Definition: options.h:583
CoAP options container.
Definition: options.h:97
size_t storage_size
Current size of encoded options.
Definition: options.h:103
size_t option_count
Number of options present.
Definition: options.h:125
unicoap_option_entry_t entries[CONFIG_UNICOAP_OPTIONS_MAX]
Helper array used to encode and decode options into options storage.
Definition: options.h:118
size_t storage_capacity
Available capacity in options storage buffer.
Definition: options.h:110
Utility macros for unicoap