Nanopb files

Dependents:   nanopb_V2 Message_generator LEX_Threaded_Programming_V3

Committer:
omatthews
Date:
Mon Aug 19 15:23:39 2019 +0000
Revision:
3:67ee10c4ae98
Parent:
2:d2c61a9be078
It works

Who changed what in which revision?

UserRevisionLine numberNew contents of line
omatthews 2:d2c61a9be078 1 /* pb_common.c: Common support functions for pb_encode.c and pb_decode.c.
omatthews 2:d2c61a9be078 2 *
omatthews 2:d2c61a9be078 3 * 2014 Petteri Aimonen <jpa@kapsi.fi>
omatthews 2:d2c61a9be078 4 */
omatthews 2:d2c61a9be078 5
omatthews 2:d2c61a9be078 6 #include "pb_common.h"
omatthews 2:d2c61a9be078 7
omatthews 2:d2c61a9be078 8 bool pb_field_iter_begin(pb_field_iter_t *iter, const pb_field_t *fields, void *dest_struct)
omatthews 2:d2c61a9be078 9 {
omatthews 2:d2c61a9be078 10 iter->start = fields;
omatthews 2:d2c61a9be078 11 iter->pos = fields;
omatthews 2:d2c61a9be078 12 iter->required_field_index = 0;
omatthews 2:d2c61a9be078 13 iter->dest_struct = dest_struct;
omatthews 2:d2c61a9be078 14 iter->pData = (char*)dest_struct + iter->pos->data_offset;
omatthews 2:d2c61a9be078 15 iter->pSize = (char*)iter->pData + iter->pos->size_offset;
omatthews 2:d2c61a9be078 16
omatthews 2:d2c61a9be078 17 return (iter->pos->tag != 0);
omatthews 2:d2c61a9be078 18 }
omatthews 2:d2c61a9be078 19
omatthews 2:d2c61a9be078 20 bool pb_field_iter_next(pb_field_iter_t *iter)
omatthews 2:d2c61a9be078 21 {
omatthews 2:d2c61a9be078 22 const pb_field_t *prev_field = iter->pos;
omatthews 2:d2c61a9be078 23
omatthews 2:d2c61a9be078 24 if (prev_field->tag == 0)
omatthews 2:d2c61a9be078 25 {
omatthews 2:d2c61a9be078 26 /* Handle empty message types, where the first field is already the terminator.
omatthews 2:d2c61a9be078 27 * In other cases, the iter->pos never points to the terminator. */
omatthews 2:d2c61a9be078 28 return false;
omatthews 2:d2c61a9be078 29 }
omatthews 2:d2c61a9be078 30
omatthews 2:d2c61a9be078 31 iter->pos++;
omatthews 2:d2c61a9be078 32
omatthews 2:d2c61a9be078 33 if (iter->pos->tag == 0)
omatthews 2:d2c61a9be078 34 {
omatthews 2:d2c61a9be078 35 /* Wrapped back to beginning, reinitialize */
omatthews 2:d2c61a9be078 36 (void)pb_field_iter_begin(iter, iter->start, iter->dest_struct);
omatthews 2:d2c61a9be078 37 return false;
omatthews 2:d2c61a9be078 38 }
omatthews 2:d2c61a9be078 39 else
omatthews 2:d2c61a9be078 40 {
omatthews 2:d2c61a9be078 41 /* Increment the pointers based on previous field size */
omatthews 2:d2c61a9be078 42 size_t prev_size = prev_field->data_size;
omatthews 2:d2c61a9be078 43
omatthews 2:d2c61a9be078 44 if (PB_HTYPE(prev_field->type) == PB_HTYPE_ONEOF &&
omatthews 2:d2c61a9be078 45 PB_HTYPE(iter->pos->type) == PB_HTYPE_ONEOF &&
omatthews 2:d2c61a9be078 46 iter->pos->data_offset == PB_SIZE_MAX)
omatthews 2:d2c61a9be078 47 {
omatthews 2:d2c61a9be078 48 /* Don't advance pointers inside unions */
omatthews 2:d2c61a9be078 49 return true;
omatthews 2:d2c61a9be078 50 }
omatthews 2:d2c61a9be078 51 else if (PB_ATYPE(prev_field->type) == PB_ATYPE_STATIC &&
omatthews 2:d2c61a9be078 52 PB_HTYPE(prev_field->type) == PB_HTYPE_REPEATED)
omatthews 2:d2c61a9be078 53 {
omatthews 2:d2c61a9be078 54 /* In static arrays, the data_size tells the size of a single entry and
omatthews 2:d2c61a9be078 55 * array_size is the number of entries */
omatthews 2:d2c61a9be078 56 prev_size *= prev_field->array_size;
omatthews 2:d2c61a9be078 57 }
omatthews 2:d2c61a9be078 58 else if (PB_ATYPE(prev_field->type) == PB_ATYPE_POINTER)
omatthews 2:d2c61a9be078 59 {
omatthews 2:d2c61a9be078 60 /* Pointer fields always have a constant size in the main structure.
omatthews 2:d2c61a9be078 61 * The data_size only applies to the dynamically allocated area. */
omatthews 2:d2c61a9be078 62 prev_size = sizeof(void*);
omatthews 2:d2c61a9be078 63 }
omatthews 2:d2c61a9be078 64
omatthews 2:d2c61a9be078 65 if (PB_HTYPE(prev_field->type) == PB_HTYPE_REQUIRED)
omatthews 2:d2c61a9be078 66 {
omatthews 2:d2c61a9be078 67 /* Count the required fields, in order to check their presence in the
omatthews 2:d2c61a9be078 68 * decoder. */
omatthews 2:d2c61a9be078 69 iter->required_field_index++;
omatthews 2:d2c61a9be078 70 }
omatthews 2:d2c61a9be078 71
omatthews 2:d2c61a9be078 72 iter->pData = (char*)iter->pData + prev_size + iter->pos->data_offset;
omatthews 2:d2c61a9be078 73 iter->pSize = (char*)iter->pData + iter->pos->size_offset;
omatthews 2:d2c61a9be078 74 return true;
omatthews 2:d2c61a9be078 75 }
omatthews 2:d2c61a9be078 76 }
omatthews 2:d2c61a9be078 77
omatthews 2:d2c61a9be078 78 bool pb_field_iter_find(pb_field_iter_t *iter, uint32_t tag)
omatthews 2:d2c61a9be078 79 {
omatthews 2:d2c61a9be078 80 const pb_field_t *start = iter->pos;
omatthews 2:d2c61a9be078 81
omatthews 2:d2c61a9be078 82 do {
omatthews 2:d2c61a9be078 83 if (iter->pos->tag == tag &&
omatthews 2:d2c61a9be078 84 PB_LTYPE(iter->pos->type) != PB_LTYPE_EXTENSION)
omatthews 2:d2c61a9be078 85 {
omatthews 2:d2c61a9be078 86 /* Found the wanted field */
omatthews 2:d2c61a9be078 87 return true;
omatthews 2:d2c61a9be078 88 }
omatthews 2:d2c61a9be078 89
omatthews 2:d2c61a9be078 90 (void)pb_field_iter_next(iter);
omatthews 2:d2c61a9be078 91 } while (iter->pos != start);
omatthews 2:d2c61a9be078 92
omatthews 2:d2c61a9be078 93 /* Searched all the way back to start, and found nothing. */
omatthews 2:d2c61a9be078 94 return false;
omatthews 2:d2c61a9be078 95 }
omatthews 2:d2c61a9be078 96