1 /* 2 * Copyright (c) 2022 Nordic Semiconductor ASA 3 * 4 * SPDX-License-Identifier: Apache-2.0 5 */ 6 #ifndef H_ZCBOR_BULK_PRIV_ 7 #define H_ZCBOR_BULK_PRIV_ 8 9 #ifdef __cplusplus 10 extern "C" { 11 #endif 12 13 #ifdef __ZEPHYR__ 14 #include <zcbor_common.h> 15 #include <zcbor_decode.h> 16 #else 17 #include "zcbor_common.h" 18 #include "zcbor_decode.h" 19 #endif 20 21 /** @cond INTERNAL_HIDDEN */ 22 23 struct zcbor_map_decode_key_val { 24 struct zcbor_string key; /* Map key string */ 25 zcbor_decoder_t *decoder; /* Key corresponding decoder */ 26 void *value_ptr; 27 bool found; 28 }; 29 30 /** @brief Define single key-decoder mapping 31 * 32 * The macro creates a single zcbor_map_decode_key_val type object. 33 * 34 * @param k key is "" enclosed string representing key; 35 * @param dec decoder function; this should be zcbor_decoder_t 36 * type function from zcbor or a user provided implementation 37 * compatible with the type. 38 * @param vp non-NULL pointer for result of decoding; should correspond 39 * to type served by decoder function for the mapping. 40 */ 41 #define ZCBOR_MAP_DECODE_KEY_DECODER(k, dec, vp) \ 42 { \ 43 { \ 44 .value = (uint8_t *)k, \ 45 .len = sizeof(k) - 1, \ 46 }, \ 47 .decoder = (zcbor_decoder_t *)dec, \ 48 .value_ptr = vp, \ 49 } 50 51 /** @brief Define single key-value decode mapping 52 * 53 * ZCBOR_MAP_DECODE_KEY_DECODER should be used instead of this macro as, 54 * this macro does not allow keys with whitespaces embeeded, which CBOR 55 * does allow. 56 * 57 * The macro creates a single zcbor_map_decode_key_val type object. 58 * 59 * @param k key; the @p k will be stringified so should be given 60 * without ""; 61 * @param dec decoder function; this should be zcbor_decoder_t 62 * type function from zcbor or a user provided implementation 63 * compatible with the type. 64 * @param vp non-NULL pointer for result of decoding; should correspond 65 * to type served by decoder function for the mapping. 66 */ 67 #define ZCBOR_MAP_DECODE_KEY_VAL(k, dec, vp) \ 68 ZCBOR_MAP_DECODE_KEY_DECODER(STRINGIFY(k), dec, vp) 69 70 /** @brief Decodes single level map according to a provided key-decode map. 71 * 72 * The function takes @p map of key to decoder array defined as: 73 * 74 * struct zcbor_map_decode_key_val map[] = { 75 * ZCBOR_MAP_DECODE_KEY_DECODER("key0", decode_fun0, val_ptr0), 76 * ZCBOR_MAP_DECODE_KEY_DECODER("key1", decode_fun1, val_ptr1), 77 * ... 78 * }; 79 * 80 * where "key?" is string representing key; the decode_fun? is 81 * zcbor_decoder_t compatible function, either from zcbor or defined by 82 * user; val_ptr? are pointers to variables where decoder function for 83 * a given key will place a decoded value - they have to agree in type 84 * with decoder function. 85 * 86 * Failure to decode any of values will cause the function to return 87 * negative error, and leave the map open: map is broken anyway or key-decoder 88 * mapping is broken, and we can not really decode the map. 89 * 90 * Note that the function opens map by itself and will fail if map 91 * is already opened. 92 * 93 * @param zsd zcbor decoder state; 94 * @param map key-decoder mapping list; 95 * @param map_size size of maps, both maps have to have the same size; 96 * @param matched pointer to the counter of matched keys, zeroed upon 97 * successful map entry and incremented only for successful 98 * decoded fields. 99 * @return 0 when the whole map has been parsed, there have been 100 * no decoding errors, and map has been closed successfully; 101 * -ENOMSG when given decoder function failed to decode 102 * value; 103 * -EADDRINUSE when key appears twice within map, map is then 104 * parsed up to they key that has appeared twice; 105 * -EBADMSG when failed to close map. 106 */ 107 int zcbor_map_decode_bulk(zcbor_state_t *zsd, struct zcbor_map_decode_key_val *map, 108 size_t map_size, size_t *matched); 109 110 /** @endcond */ 111 112 #ifdef __cplusplus 113 } 114 #endif 115 116 #endif /* H_ZCBOR_BULK_PRIV_ */ 117