X-Git-Url: https://gerrit.automotivelinux.org/gerrit/gitweb?a=blobdiff_plain;f=pb_encode.c;h=d6ba7e34ef03a6cfee09506218c9984dd2d9b0a5;hb=262c62676cf740ec3ce14a22bde47b7968fec8f0;hp=0e048ac6e9d3de51be02d7f6c4f0090f5dd9742c;hpb=d2063ff0b63b3b4f2f2081e61874a3c2487c4994;p=apps%2Fagl-service-can-low-level.git diff --git a/pb_encode.c b/pb_encode.c index 0e048ac6..d6ba7e34 100644 --- a/pb_encode.c +++ b/pb_encode.c @@ -28,7 +28,8 @@ static const pb_encoder_t PB_ENCODERS[PB_LTYPES_COUNT] = { &pb_enc_bytes, &pb_enc_string, - &pb_enc_submessage + &pb_enc_submessage, + NULL /* extensions */ }; /* pb_ostream_t implementation */ @@ -83,10 +84,7 @@ bool checkreturn pb_write(pb_ostream_t *stream, const uint8_t *buf, size_t count /* Main encoding stuff */ -/* Callbacks don't need this function because they usually know the data type - * without examining the field structure. - * Therefore it is static for now. - */ +/* Encode a static array. Handles the size calculations and possible packing. */ static bool checkreturn encode_array(pb_ostream_t *stream, const pb_field_t *field, const void *pData, size_t count, pb_encoder_t func) { @@ -97,6 +95,7 @@ static bool checkreturn encode_array(pb_ostream_t *stream, const pb_field_t *fie if (count == 0) return true; + /* We always pack arrays if the datatype allows it. */ if (PB_LTYPE(field->type) <= PB_LTYPE_LAST_PACKABLE) { if (!pb_encode_tag(stream, PB_WT_STRING, field->tag)) @@ -112,7 +111,7 @@ static bool checkreturn encode_array(pb_ostream_t *stream, const pb_field_t *fie size = 8 * count; } else - { + { pb_ostream_t sizestream = PB_OSTREAM_SIZING; p = pData; for (i = 0; i < count; i++) @@ -155,13 +154,21 @@ static bool checkreturn encode_array(pb_ostream_t *stream, const pb_field_t *fie return true; } -bool checkreturn encode_static_field(pb_ostream_t *stream, const pb_field_t *field, const void *pData) +/* Encode a field with static allocation, i.e. one whose data is stored + * in the structure itself. */ +static bool checkreturn encode_static_field(pb_ostream_t *stream, + const pb_field_t *field, const void *pData) { pb_encoder_t func; const void *pSize; + bool dummy = true; func = PB_ENCODERS[PB_LTYPE(field->type)]; - pSize = (const char*)pData + field->size_offset; + + if (field->size_offset) + pSize = (const char*)pData + field->size_offset; + else + pSize = &dummy; switch (PB_HTYPE(field->type)) { @@ -195,7 +202,10 @@ bool checkreturn encode_static_field(pb_ostream_t *stream, const pb_field_t *fie return true; } -bool checkreturn encode_callback_field(pb_ostream_t *stream, const pb_field_t *field, const void *pData) +/* Encode a field with callback semantics. This means that a user function is + * called to provide and encode the actual data. */ +static bool checkreturn encode_callback_field(pb_ostream_t *stream, + const pb_field_t *field, const void *pData) { const pb_callback_t *callback = (const pb_callback_t*)pData; @@ -213,6 +223,57 @@ bool checkreturn encode_callback_field(pb_ostream_t *stream, const pb_field_t *f return true; } +/* Encode a single field of any callback or static type. */ +static bool checkreturn encode_field(pb_ostream_t *stream, + const pb_field_t *field, const void *pData) +{ + switch (PB_ATYPE(field->type)) + { + case PB_ATYPE_STATIC: + return encode_static_field(stream, field, pData); + + case PB_ATYPE_CALLBACK: + return encode_callback_field(stream, field, pData); + + default: + PB_RETURN_ERROR(stream, "invalid field type"); + } +} + +/* Default handler for extension fields. Expects to have a pb_field_t + * pointer in the extension->type->arg field. */ +static bool checkreturn default_extension_handler(pb_ostream_t *stream, + const pb_extension_t *extension) +{ + const pb_field_t *field = (const pb_field_t*)extension->type->arg; + return encode_field(stream, field, extension->dest); +} + +/* Walk through all the registered extensions and give them a chance + * to encode themselves. */ +static bool checkreturn encode_extension_field(pb_ostream_t *stream, + const pb_field_t *field, const void *pData) +{ + const pb_extension_t *extension = *(const pb_extension_t* const *)pData; + UNUSED(field); + + while (extension) + { + bool status; + if (extension->type->encode) + status = extension->type->encode(stream, extension); + else + status = default_extension_handler(stream, extension); + + if (!status) + return false; + + extension = extension->next; + } + + return true; +} + bool checkreturn pb_encode(pb_ostream_t *stream, const pb_field_t fields[], const void *src_struct) { const pb_field_t *field = fields; @@ -230,21 +291,18 @@ bool checkreturn pb_encode(pb_ostream_t *stream, const pb_field_t fields[], cons { prev_size *= field->array_size; } - - switch (PB_ATYPE(field->type)) + + if (PB_LTYPE(field->type) == PB_LTYPE_EXTENSION) { - case PB_ATYPE_STATIC: - if (!encode_static_field(stream, field, pData)) - return false; - break; - - case PB_ATYPE_CALLBACK: - if (!encode_callback_field(stream, field, pData)) - return false; - break; - - default: - PB_RETURN_ERROR(stream, "invalid field type"); + /* Special case for the extension field placeholder */ + if (!encode_extension_field(stream, field, pData)) + return false; + } + else + { + /* Regular field */ + if (!encode_field(stream, field, pData)) + return false; } field++; @@ -253,6 +311,11 @@ bool checkreturn pb_encode(pb_ostream_t *stream, const pb_field_t fields[], cons return true; } +bool pb_encode_delimited(pb_ostream_t *stream, const pb_field_t fields[], const void *src_struct) +{ + return pb_encode_submessage(stream, fields, src_struct); +} + /* Helper functions */ bool checkreturn pb_encode_varint(pb_ostream_t *stream, uint64_t value) {