From e4e2e572183a1c82b849f77a7fbde2c31e0e250b Mon Sep 17 00:00:00 2001 From: Olaf Bergmann Date: Sun, 7 Jun 2015 11:42:44 +0200 Subject: [PATCH] option.[hc]: new option filter implementation The implementation of option filters based on coap_opt_filter_t has changed to improve support for long option numbers. An option filter is now a sparse field of a fixed size that can hold up to COAP_OPT_FILTER_LONG options with a number larger than 255, and up to COAP_OPT_FILTER_SHORT options with a number of 255 or less. The space taken by coap_opt_filter_t is 2 * ((((COAP_OPT_FILTER_SHORT + 1) >> 1) + COAP_OPT_FILTER_LONG) + 1), or 12 bytes with the default settings for COAP_OPT_FILTER_SHORT == 6 and COAP_OPT_FILTER_LONG == 2. Three new functions have been added to the option filter API: * coap_option_filter_set() to store an option number in a filter, * coap_option_filter_get() to check if a filter contains a given option number, and * coap_option_filter_unset() to remove an option number from a filter. The previously existing functions coap_option_setb(), coap_option_getb(), and coap_option_clrb() continue to work on the new option filter unchanged but now are deprecated. --- include/coap/option.h | 99 +++++++++++++++++++++++++++++++++--- src/option.c | 115 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 208 insertions(+), 6 deletions(-) diff --git a/include/coap/option.h b/include/coap/option.h index 8bd024e4..8fc4e9b8 100644 --- a/include/coap/option.h +++ b/include/coap/option.h @@ -84,10 +84,55 @@ coap_opt_t *options_start(coap_pdu_t *pdu); */ /** - * Fixed-size bit-vector we use for option filtering. It is large enough to hold - * the highest option number known at build time (20 in the core spec). + * The number of option types below 256 that can be stored in an + * option filter. COAP_OPT_FILTER_SHORT + COAP_OPT_FILTER_LONG must be + * at most 16. Each coap_option_filter_t object reserves + * ((COAP_OPT_FILTER_SHORT + 1) / 2) * 2 bytes for short options. */ -typedef unsigned char coap_opt_filter_t[(COAP_MAX_OPT >> 3) + 1]; +#define COAP_OPT_FILTER_SHORT 6 + +/** + * The number of option types above 255 that can be stored in an + * option filter. COAP_OPT_FILTER_SHORT + COAP_OPT_FILTER_LONG must be + * at most 16. Each coap_option_filter_t object reserves + * COAP_OPT_FILTER_LONG * 2 bytes for short options. + */ +#define COAP_OPT_FILTER_LONG 2 + +/* Ensure that COAP_OPT_FILTER_SHORT and COAP_OPT_FILTER_LONG are set + * correctly. */ +#if (COAP_OPT_FILTER_SHORT + COAP_OPT_FILTER_LONG > 16) +#error COAP_OPT_FILTER_SHORT + COAP_OPT_FILTER_LONG must be less or equal 16 +#endif /* (COAP_OPT_FILTER_SHORT + COAP_OPT_FILTER_LONG > 16) */ + +/** The number of elements in coap_opt_filter_t. */ +#define COAP_OPT_FILTER_SIZE \ + (((COAP_OPT_FILTER_SHORT + 1) >> 1) + COAP_OPT_FILTER_LONG) +1 + +/** + * Fixed-size vector we use for option filtering. It is large enough + * to hold COAP_OPT_FILTER_SHORT entries with an option number between + * 0 and 255, and COAP_OPT_FILTER_LONG entries with an option number + * between 256 and 65535. Its internal structure is + * + * @code +struct { + uint16_t mask; + uint16_t long_opts[COAP_OPT_FILTER_LONG]; + uint8_t short_opts[COAP_OPT_FILTER_SHORT]; +} + * @endcode + * + * The first element contains a bit vector that indicates which fields + * in the remaining array are used. The first COAP_OPT_FILTER_LONG + * bits correspond to the long option types that are stored in the + * elements from index 1 to COAP_OPT_FILTER_LONG. The next + * COAP_OPT_FILTER_SHORT bits correspond to the short option types + * that are stored in the elements from index COAP_OPT_FILTER_LONG + 1 + * to COAP_OPT_FILTER_LONG + COAP_OPT_FILTER_SHORT. The latter + * elements are treated as bytes. + */ +typedef uint16_t coap_opt_filter_t[COAP_OPT_FILTER_SIZE]; /** Pre-defined filter that includes all options. */ #define COAP_OPT_ALL NULL @@ -102,11 +147,49 @@ coap_option_filter_clear(coap_opt_filter_t f) { memset(f, 0, sizeof(coap_opt_filter_t)); } +/** + * Sets the corresponding entry for @p type in @p filter. This + * function returns @c 1 if bit was set or @c 0 on error (i.e. when + * the given type does not fit in the filter). + * + * @param filter The filter object to change. + * @param type The type for which the bit should be set. + * + * @return @c 1 if bit was set, @c 0 otherwise. + */ +int coap_option_filter_set(coap_opt_filter_t filter, unsigned short type); + +/** + * Clears the corresponding entry for @p type in @p filter. This + * function returns @c 1 if bit was set or @c 0 on error (i.e. when + * the given type does not fit in the filter). + * + * @param filter The filter object to change. + * @param type The type that should be cleared from the filter. + * + * @return @c 1 if bit was set, @c 0 otherwise. + */ +int coap_option_filter_unset(coap_opt_filter_t filter, unsigned short type); + +/** + * Checks if @p type is contained in @p filter. This function returns + * @c 1 if found, @c 0 if not, or @c -1 on error (i.e. when the given + * type does not fit in the filter). + * + * @param filter The filter object to search. + * @param type The type to search for. + * + * @return @c 1 if @p type was found, @c 0 otherwise, or @c -1 on error. + */ +int coap_option_filter_get(const coap_opt_filter_t filter, unsigned short type); + /** * Sets the corresponding bit for @p type in @p filter. This function returns @c * 1 if bit was set or @c -1 on error (i.e. when the given type does not fit in * the filter). * + * @deprecated Use coap_option_filter_set() instead. + * * @param filter The filter object to change. * @param type The type for which the bit should be set. * @@ -114,7 +197,7 @@ coap_option_filter_clear(coap_opt_filter_t f) { */ inline static int coap_option_setb(coap_opt_filter_t filter, unsigned short type) { - return bits_setb((uint8_t *)filter, sizeof(coap_opt_filter_t), type); + return coap_option_filter_set(filter, type) ? 1 : -1; } /** @@ -122,6 +205,8 @@ coap_option_setb(coap_opt_filter_t filter, unsigned short type) { * @c 1 if bit was cleared or @c -1 on error (i.e. when the given type does not * fit in the filter). * + * @deprecated Use coap_option_filter_unset() instead. + * * @param filter The filter object to change. * @param type The type for which the bit should be cleared. * @@ -129,7 +214,7 @@ coap_option_setb(coap_opt_filter_t filter, unsigned short type) { */ inline static int coap_option_clrb(coap_opt_filter_t filter, unsigned short type) { - return bits_clrb((uint8_t *)filter, sizeof(coap_opt_filter_t), type); + return coap_option_filter_unset(filter, type) ? 1 : -1; } /** @@ -137,6 +222,8 @@ coap_option_clrb(coap_opt_filter_t filter, unsigned short type) { * 1 if the bit is set @c 0 if not, or @c -1 on error (i.e. when the given type * does not fit in the filter). * + * @deprecated Use coap_option_filter_get() instead. + * * @param filter The filter object to read bit from. * @param type The type for which the bit should be read. * @@ -144,7 +231,7 @@ coap_option_clrb(coap_opt_filter_t filter, unsigned short type) { */ inline static int coap_option_getb(const coap_opt_filter_t filter, unsigned short type) { - return bits_getb((uint8_t *)filter, sizeof(coap_opt_filter_t), type); + return coap_option_filter_get(filter, type); } /** diff --git a/src/option.c b/src/option.c index 949ef8bd..dd9d0cdb 100644 --- a/src/option.c +++ b/src/option.c @@ -18,6 +18,7 @@ #include #include "option.h" +#include "encode.h" /* for coap_fls() */ #include "debug.h" coap_opt_t * @@ -406,3 +407,117 @@ coap_opt_encode(coap_opt_t *opt, size_t maxlen, unsigned short delta, return l + length; } +/* coap_opt_filter_t has the following internal structure: */ +typedef struct { + uint16_t mask; + +#define LONG_MASK ((1 << COAP_OPT_FILTER_LONG) - 1) +#define SHORT_MASK \ + (~LONG_MASK & ((1 << (COAP_OPT_FILTER_LONG + COAP_OPT_FILTER_SHORT)) - 1)) + + uint16_t long_opts[COAP_OPT_FILTER_LONG]; + uint8_t short_opts[COAP_OPT_FILTER_SHORT]; +} opt_filter; + +/** Returns true iff @p type denotes an option type larger than 255. */ +static inline int +is_long_option(unsigned short type) { return type > 255; } + +/** Operation specifiers for coap_filter_op(). */ +enum filter_op_t { FILTER_SET, FILTER_CLEAR, FILTER_GET }; + +/** + * Applies @p op on @p filter with respect to @p type. The following + * operations are defined: + * + * FILTER_SET: Store @p type into an empty slot in @p filter. Returns + * @c 1 on success, or @c 0 if no spare slot was available. + * + * FILTER_CLEAR: Remove @p type from filter if it exists. + * + * FILTER_GET: Search for @p type in @p filter. Returns @c 1 if found, + * or @c 0 if not found. + * + * @param filter The filter object. + * @param type The option type to set, get or clear in @p filter. + * @param op The operation to apply to @p filter and @p type. + * + * @return 1 on success, and 0 when FILTER_GET yields no + * hit or no free slot is available to store @p type with FILTER_SET. + */ +static int +coap_option_filter_op(coap_opt_filter_t filter, + unsigned short type, + enum filter_op_t op) { + size_t index = 0; + opt_filter *of = (opt_filter *)filter; + uint16_t nr, mask = 0; + + if (is_long_option(type)) { + mask = LONG_MASK; + + for (nr = 1; index < COAP_OPT_FILTER_LONG; nr <<= 1, index++) { + + if (((of->mask & nr) > 0) && (of->long_opts[index] == type)) { + if (op == FILTER_CLEAR) { + of->mask &= ~nr; + } + + return 1; + } + } + } else { + mask = SHORT_MASK; + + for (nr = 1 << COAP_OPT_FILTER_LONG; index < COAP_OPT_FILTER_SHORT; + nr <<= 1, index++) { + + if (((of->mask & nr) > 0) && (of->short_opts[index] == (type & 0xff))) { + if (op == FILTER_CLEAR) { + of->mask &= ~nr; + } + + return 1; + } + } + } + + /* type was not found, so there is nothing to do if op is CLEAR or GET */ + if ((op == FILTER_CLEAR) || (op == FILTER_GET)) { + return 0; + } + + /* handle FILTER_SET: */ + + index = coap_fls(~of->mask & mask); + if (!index) { + return 0; + } + + if (is_long_option(type)) { + of->long_opts[index - 1] = type; + } else { + of->short_opts[index - COAP_OPT_FILTER_LONG - 1] = type; + } + + of->mask |= 1 << (index - 1); + + return 1; +} + +int +coap_option_filter_set(coap_opt_filter_t filter, unsigned short type) { + return coap_option_filter_op(filter, type, FILTER_SET); +} + +int +coap_option_filter_unset(coap_opt_filter_t filter, unsigned short type) { + return coap_option_filter_op(filter, type, FILTER_CLEAR); +} + +int +coap_option_filter_get(const coap_opt_filter_t filter, unsigned short type) { + /* Ugly cast to make the const go away (FILTER_GET wont change filter + * but as _set and _unset do, the function does not take a const). */ + return coap_option_filter_op((uint16_t *)filter, type, FILTER_GET); +}