Sergei G / Mbed OS ProtocBufTest

Dependencies:   nanopb protocol

Committer:
sgnezdov
Date:
Wed Jul 12 22:40:29 2017 +0000
Revision:
0:fbdd0d307c19
initial import demonstrates how to decode Sample protocol buffers message.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
sgnezdov 0:fbdd0d307c19 1 /* pb_encode.h: Functions to encode protocol buffers. Depends on pb_encode.c.
sgnezdov 0:fbdd0d307c19 2 * The main function is pb_encode. You also need an output stream, and the
sgnezdov 0:fbdd0d307c19 3 * field descriptions created by nanopb_generator.py.
sgnezdov 0:fbdd0d307c19 4 */
sgnezdov 0:fbdd0d307c19 5
sgnezdov 0:fbdd0d307c19 6 #ifndef PB_ENCODE_H_INCLUDED
sgnezdov 0:fbdd0d307c19 7 #define PB_ENCODE_H_INCLUDED
sgnezdov 0:fbdd0d307c19 8
sgnezdov 0:fbdd0d307c19 9 #include "pb.h"
sgnezdov 0:fbdd0d307c19 10
sgnezdov 0:fbdd0d307c19 11 #ifdef __cplusplus
sgnezdov 0:fbdd0d307c19 12 extern "C" {
sgnezdov 0:fbdd0d307c19 13 #endif
sgnezdov 0:fbdd0d307c19 14
sgnezdov 0:fbdd0d307c19 15 /* Structure for defining custom output streams. You will need to provide
sgnezdov 0:fbdd0d307c19 16 * a callback function to write the bytes to your storage, which can be
sgnezdov 0:fbdd0d307c19 17 * for example a file or a network socket.
sgnezdov 0:fbdd0d307c19 18 *
sgnezdov 0:fbdd0d307c19 19 * The callback must conform to these rules:
sgnezdov 0:fbdd0d307c19 20 *
sgnezdov 0:fbdd0d307c19 21 * 1) Return false on IO errors. This will cause encoding to abort.
sgnezdov 0:fbdd0d307c19 22 * 2) You can use state to store your own data (e.g. buffer pointer).
sgnezdov 0:fbdd0d307c19 23 * 3) pb_write will update bytes_written after your callback runs.
sgnezdov 0:fbdd0d307c19 24 * 4) Substreams will modify max_size and bytes_written. Don't use them
sgnezdov 0:fbdd0d307c19 25 * to calculate any pointers.
sgnezdov 0:fbdd0d307c19 26 */
sgnezdov 0:fbdd0d307c19 27 struct pb_ostream_s
sgnezdov 0:fbdd0d307c19 28 {
sgnezdov 0:fbdd0d307c19 29 #ifdef PB_BUFFER_ONLY
sgnezdov 0:fbdd0d307c19 30 /* Callback pointer is not used in buffer-only configuration.
sgnezdov 0:fbdd0d307c19 31 * Having an int pointer here allows binary compatibility but
sgnezdov 0:fbdd0d307c19 32 * gives an error if someone tries to assign callback function.
sgnezdov 0:fbdd0d307c19 33 * Also, NULL pointer marks a 'sizing stream' that does not
sgnezdov 0:fbdd0d307c19 34 * write anything.
sgnezdov 0:fbdd0d307c19 35 */
sgnezdov 0:fbdd0d307c19 36 int *callback;
sgnezdov 0:fbdd0d307c19 37 #else
sgnezdov 0:fbdd0d307c19 38 bool (*callback)(pb_ostream_t *stream, const pb_byte_t *buf, size_t count);
sgnezdov 0:fbdd0d307c19 39 #endif
sgnezdov 0:fbdd0d307c19 40 void *state; /* Free field for use by callback implementation. */
sgnezdov 0:fbdd0d307c19 41 size_t max_size; /* Limit number of output bytes written (or use SIZE_MAX). */
sgnezdov 0:fbdd0d307c19 42 size_t bytes_written; /* Number of bytes written so far. */
sgnezdov 0:fbdd0d307c19 43
sgnezdov 0:fbdd0d307c19 44 #ifndef PB_NO_ERRMSG
sgnezdov 0:fbdd0d307c19 45 const char *errmsg;
sgnezdov 0:fbdd0d307c19 46 #endif
sgnezdov 0:fbdd0d307c19 47 };
sgnezdov 0:fbdd0d307c19 48
sgnezdov 0:fbdd0d307c19 49 /***************************
sgnezdov 0:fbdd0d307c19 50 * Main encoding functions *
sgnezdov 0:fbdd0d307c19 51 ***************************/
sgnezdov 0:fbdd0d307c19 52
sgnezdov 0:fbdd0d307c19 53 /* Encode a single protocol buffers message from C structure into a stream.
sgnezdov 0:fbdd0d307c19 54 * Returns true on success, false on any failure.
sgnezdov 0:fbdd0d307c19 55 * The actual struct pointed to by src_struct must match the description in fields.
sgnezdov 0:fbdd0d307c19 56 * All required fields in the struct are assumed to have been filled in.
sgnezdov 0:fbdd0d307c19 57 *
sgnezdov 0:fbdd0d307c19 58 * Example usage:
sgnezdov 0:fbdd0d307c19 59 * MyMessage msg = {};
sgnezdov 0:fbdd0d307c19 60 * uint8_t buffer[64];
sgnezdov 0:fbdd0d307c19 61 * pb_ostream_t stream;
sgnezdov 0:fbdd0d307c19 62 *
sgnezdov 0:fbdd0d307c19 63 * msg.field1 = 42;
sgnezdov 0:fbdd0d307c19 64 * stream = pb_ostream_from_buffer(buffer, sizeof(buffer));
sgnezdov 0:fbdd0d307c19 65 * pb_encode(&stream, MyMessage_fields, &msg);
sgnezdov 0:fbdd0d307c19 66 */
sgnezdov 0:fbdd0d307c19 67 bool pb_encode(pb_ostream_t *stream, const pb_field_t fields[], const void *src_struct);
sgnezdov 0:fbdd0d307c19 68
sgnezdov 0:fbdd0d307c19 69 /* Same as pb_encode, but prepends the length of the message as a varint.
sgnezdov 0:fbdd0d307c19 70 * Corresponds to writeDelimitedTo() in Google's protobuf API.
sgnezdov 0:fbdd0d307c19 71 */
sgnezdov 0:fbdd0d307c19 72 bool pb_encode_delimited(pb_ostream_t *stream, const pb_field_t fields[], const void *src_struct);
sgnezdov 0:fbdd0d307c19 73
sgnezdov 0:fbdd0d307c19 74 /* Encode the message to get the size of the encoded data, but do not store
sgnezdov 0:fbdd0d307c19 75 * the data. */
sgnezdov 0:fbdd0d307c19 76 bool pb_get_encoded_size(size_t *size, const pb_field_t fields[], const void *src_struct);
sgnezdov 0:fbdd0d307c19 77
sgnezdov 0:fbdd0d307c19 78 /**************************************
sgnezdov 0:fbdd0d307c19 79 * Functions for manipulating streams *
sgnezdov 0:fbdd0d307c19 80 **************************************/
sgnezdov 0:fbdd0d307c19 81
sgnezdov 0:fbdd0d307c19 82 /* Create an output stream for writing into a memory buffer.
sgnezdov 0:fbdd0d307c19 83 * The number of bytes written can be found in stream.bytes_written after
sgnezdov 0:fbdd0d307c19 84 * encoding the message.
sgnezdov 0:fbdd0d307c19 85 *
sgnezdov 0:fbdd0d307c19 86 * Alternatively, you can use a custom stream that writes directly to e.g.
sgnezdov 0:fbdd0d307c19 87 * a file or a network socket.
sgnezdov 0:fbdd0d307c19 88 */
sgnezdov 0:fbdd0d307c19 89 pb_ostream_t pb_ostream_from_buffer(pb_byte_t *buf, size_t bufsize);
sgnezdov 0:fbdd0d307c19 90
sgnezdov 0:fbdd0d307c19 91 /* Pseudo-stream for measuring the size of a message without actually storing
sgnezdov 0:fbdd0d307c19 92 * the encoded data.
sgnezdov 0:fbdd0d307c19 93 *
sgnezdov 0:fbdd0d307c19 94 * Example usage:
sgnezdov 0:fbdd0d307c19 95 * MyMessage msg = {};
sgnezdov 0:fbdd0d307c19 96 * pb_ostream_t stream = PB_OSTREAM_SIZING;
sgnezdov 0:fbdd0d307c19 97 * pb_encode(&stream, MyMessage_fields, &msg);
sgnezdov 0:fbdd0d307c19 98 * printf("Message size is %d\n", stream.bytes_written);
sgnezdov 0:fbdd0d307c19 99 */
sgnezdov 0:fbdd0d307c19 100 #ifndef PB_NO_ERRMSG
sgnezdov 0:fbdd0d307c19 101 #define PB_OSTREAM_SIZING {0,0,0,0,0}
sgnezdov 0:fbdd0d307c19 102 #else
sgnezdov 0:fbdd0d307c19 103 #define PB_OSTREAM_SIZING {0,0,0,0}
sgnezdov 0:fbdd0d307c19 104 #endif
sgnezdov 0:fbdd0d307c19 105
sgnezdov 0:fbdd0d307c19 106 /* Function to write into a pb_ostream_t stream. You can use this if you need
sgnezdov 0:fbdd0d307c19 107 * to append or prepend some custom headers to the message.
sgnezdov 0:fbdd0d307c19 108 */
sgnezdov 0:fbdd0d307c19 109 bool pb_write(pb_ostream_t *stream, const pb_byte_t *buf, size_t count);
sgnezdov 0:fbdd0d307c19 110
sgnezdov 0:fbdd0d307c19 111
sgnezdov 0:fbdd0d307c19 112 /************************************************
sgnezdov 0:fbdd0d307c19 113 * Helper functions for writing field callbacks *
sgnezdov 0:fbdd0d307c19 114 ************************************************/
sgnezdov 0:fbdd0d307c19 115
sgnezdov 0:fbdd0d307c19 116 /* Encode field header based on type and field number defined in the field
sgnezdov 0:fbdd0d307c19 117 * structure. Call this from the callback before writing out field contents. */
sgnezdov 0:fbdd0d307c19 118 bool pb_encode_tag_for_field(pb_ostream_t *stream, const pb_field_t *field);
sgnezdov 0:fbdd0d307c19 119
sgnezdov 0:fbdd0d307c19 120 /* Encode field header by manually specifing wire type. You need to use this
sgnezdov 0:fbdd0d307c19 121 * if you want to write out packed arrays from a callback field. */
sgnezdov 0:fbdd0d307c19 122 bool pb_encode_tag(pb_ostream_t *stream, pb_wire_type_t wiretype, uint32_t field_number);
sgnezdov 0:fbdd0d307c19 123
sgnezdov 0:fbdd0d307c19 124 /* Encode an integer in the varint format.
sgnezdov 0:fbdd0d307c19 125 * This works for bool, enum, int32, int64, uint32 and uint64 field types. */
sgnezdov 0:fbdd0d307c19 126 bool pb_encode_varint(pb_ostream_t *stream, uint64_t value);
sgnezdov 0:fbdd0d307c19 127
sgnezdov 0:fbdd0d307c19 128 /* Encode an integer in the zig-zagged svarint format.
sgnezdov 0:fbdd0d307c19 129 * This works for sint32 and sint64. */
sgnezdov 0:fbdd0d307c19 130 bool pb_encode_svarint(pb_ostream_t *stream, int64_t value);
sgnezdov 0:fbdd0d307c19 131
sgnezdov 0:fbdd0d307c19 132 /* Encode a string or bytes type field. For strings, pass strlen(s) as size. */
sgnezdov 0:fbdd0d307c19 133 bool pb_encode_string(pb_ostream_t *stream, const pb_byte_t *buffer, size_t size);
sgnezdov 0:fbdd0d307c19 134
sgnezdov 0:fbdd0d307c19 135 /* Encode a fixed32, sfixed32 or float value.
sgnezdov 0:fbdd0d307c19 136 * You need to pass a pointer to a 4-byte wide C variable. */
sgnezdov 0:fbdd0d307c19 137 bool pb_encode_fixed32(pb_ostream_t *stream, const void *value);
sgnezdov 0:fbdd0d307c19 138
sgnezdov 0:fbdd0d307c19 139 /* Encode a fixed64, sfixed64 or double value.
sgnezdov 0:fbdd0d307c19 140 * You need to pass a pointer to a 8-byte wide C variable. */
sgnezdov 0:fbdd0d307c19 141 bool pb_encode_fixed64(pb_ostream_t *stream, const void *value);
sgnezdov 0:fbdd0d307c19 142
sgnezdov 0:fbdd0d307c19 143 /* Encode a submessage field.
sgnezdov 0:fbdd0d307c19 144 * You need to pass the pb_field_t array and pointer to struct, just like
sgnezdov 0:fbdd0d307c19 145 * with pb_encode(). This internally encodes the submessage twice, first to
sgnezdov 0:fbdd0d307c19 146 * calculate message size and then to actually write it out.
sgnezdov 0:fbdd0d307c19 147 */
sgnezdov 0:fbdd0d307c19 148 bool pb_encode_submessage(pb_ostream_t *stream, const pb_field_t fields[], const void *src_struct);
sgnezdov 0:fbdd0d307c19 149
sgnezdov 0:fbdd0d307c19 150 #ifdef __cplusplus
sgnezdov 0:fbdd0d307c19 151 } /* extern "C" */
sgnezdov 0:fbdd0d307c19 152 #endif
sgnezdov 0:fbdd0d307c19 153
sgnezdov 0:fbdd0d307c19 154 #endif