mgmt/mcumgr: zcbor_bulk: Allow spaces in map keys

The CBOR encoding allows strings to have white spaces, and as string
may be used as key it should also be allowed to use space in key.
The commit provides ZCBOR_MAP_DECODE_KEY_DECODER macro, which is
intended to replace ZCBOR_MAP_DECODE_KEY_VAL macro, that allows
to use string keys with spaces in it.
Both macros are available for now.

Signed-off-by: Dominik Ermel <dominik.ermel@nordicsemi.no>
This commit is contained in:
Dominik Ermel 2022-12-06 16:32:56 +00:00 committed by Carles Cufí
commit 550b6d518d

View file

@ -21,7 +21,32 @@ struct zcbor_map_decode_key_val {
bool found; bool found;
}; };
/** @brief Define single key-decoder mapping
*
* The macro creates a single zcbor_map_decode_key_val type object.
*
* @param k key is "" enclosed string representing key;
* @param dec decoder function; this should be zcbor_decoder_t
* type function from zcbor or a user provided implementation
* compatible with the type.
* @param vp non-NULL pointer for result of decoding; should correspond
* to type served by decoder function for the mapping.
*/
#define ZCBOR_MAP_DECODE_KEY_DECODER(k, dec, vp) \
{ \
{ \
.value = k, \
.len = sizeof(k) - 1, \
}, \
.decoder = (zcbor_decoder_t *)dec, \
.value_ptr = vp, \
}
/** @brief Define single key-value decode mapping /** @brief Define single key-value decode mapping
*
* ZCBOR_MAP_DECODE_KEY_DECODER should be used instead of this macro as,
* this macro does not allow keys with whitespaces embeeded, which CBOR
* does allow.
* *
* The macro creates a single zcbor_map_decode_key_val type object. * The macro creates a single zcbor_map_decode_key_val type object.
* *
@ -34,28 +59,19 @@ struct zcbor_map_decode_key_val {
* to type served by decoder function for the mapping. * to type served by decoder function for the mapping.
*/ */
#define ZCBOR_MAP_DECODE_KEY_VAL(k, dec, vp) \ #define ZCBOR_MAP_DECODE_KEY_VAL(k, dec, vp) \
{ \ ZCBOR_MAP_DECODE_KEY_DECODER(STRINGIFY(k), dec, vp)
{ \
.value = STRINGIFY(k), \
.len = sizeof(STRINGIFY(k)) - 1, \
}, \
.decoder = (zcbor_decoder_t *)dec, \
.value_ptr = vp, \
}
/** @brief Decodes single level map according to a provided key-decode map. /** @brief Decodes single level map according to a provided key-decode map.
* *
* The function takes @p map of key to decoder array defined as: * The function takes @p map of key to decoder array defined as:
* *
* struct zcbor_map_decode_key_val map[] = { * struct zcbor_map_decode_key_val map[] = {
* ZCBOR_MAP_DECODE_KEY_VAL(key0, decode_fun0, val_ptr0), * ZCBOR_MAP_DECODE_KEY_DECODER("key0", decode_fun0, val_ptr0),
* ZCBOR_MAP_DECODE_KEY_VAL(key1, decode_fun1, val_ptr1), * ZCBOR_MAP_DECODE_KEY_DECODER("key1", decode_fun1, val_ptr1),
* ... * ...
* }; * };
* *
* where key? is string representing key without "", for example when key * where "key?" is string representing key; the decode_fun? is
* is "abcd" , then the abcd should be provided; the decode_fun? is
* zcbor_decoder_t compatible function, either from zcbor or defined by * zcbor_decoder_t compatible function, either from zcbor or defined by
* user; val_ptr? are pointers to variables where decoder function for * user; val_ptr? are pointers to variables where decoder function for
* a given key will place a decoded value - they have to agree in type * a given key will place a decoded value - they have to agree in type