Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Fork of LinkNode-Test by
pb_decode.h
00001 /* pb_decode.h: Functions to decode protocol buffers. Depends on pb_decode.c. 00002 * The main function is pb_decode. You also need an input stream, and the 00003 * field descriptions created by nanopb_generator.py. 00004 */ 00005 00006 #ifndef PB_DECODE_H_INCLUDED 00007 #define PB_DECODE_H_INCLUDED 00008 00009 #include "pb.h" 00010 00011 #ifdef __cplusplus 00012 extern "C" { 00013 #endif 00014 00015 /* Structure for defining custom input streams. You will need to provide 00016 * a callback function to read the bytes from your storage, which can be 00017 * for example a file or a network socket. 00018 * 00019 * The callback must conform to these rules: 00020 * 00021 * 1) Return false on IO errors. This will cause decoding to abort. 00022 * 2) You can use state to store your own data (e.g. buffer pointer), 00023 * and rely on pb_read to verify that no-body reads past bytes_left. 00024 * 3) Your callback may be used with substreams, in which case bytes_left 00025 * is different than from the main stream. Don't use bytes_left to compute 00026 * any pointers. 00027 */ 00028 struct pb_istream_s 00029 { 00030 #ifdef PB_BUFFER_ONLY 00031 /* Callback pointer is not used in buffer-only configuration. 00032 * Having an int pointer here allows binary compatibility but 00033 * gives an error if someone tries to assign callback function. 00034 */ 00035 int *callback; 00036 #else 00037 bool (*callback)(pb_istream_t *stream, pb_byte_t *buf, size_t count); 00038 #endif 00039 00040 void *state; /* Free field for use by callback implementation */ 00041 size_t bytes_left; 00042 00043 #ifndef PB_NO_ERRMSG 00044 const char *errmsg; 00045 #endif 00046 }; 00047 00048 /*************************** 00049 * Main decoding functions * 00050 ***************************/ 00051 00052 /* Decode a single protocol buffers message from input stream into a C structure. 00053 * Returns true on success, false on any failure. 00054 * The actual struct pointed to by dest must match the description in fields. 00055 * Callback fields of the destination structure must be initialized by caller. 00056 * All other fields will be initialized by this function. 00057 * 00058 * Example usage: 00059 * MyMessage msg = {}; 00060 * uint8_t buffer[64]; 00061 * pb_istream_t stream; 00062 * 00063 * // ... read some data into buffer ... 00064 * 00065 * stream = pb_istream_from_buffer(buffer, count); 00066 * pb_decode(&stream, MyMessage_fields, &msg); 00067 */ 00068 bool pb_decode(pb_istream_t *stream, const pb_field_t fields[], void *dest_struct); 00069 00070 /* Same as pb_decode, except does not initialize the destination structure 00071 * to default values. This is slightly faster if you need no default values 00072 * and just do memset(struct, 0, sizeof(struct)) yourself. 00073 * 00074 * This can also be used for 'merging' two messages, i.e. update only the 00075 * fields that exist in the new message. 00076 * 00077 * Note: If this function returns with an error, it will not release any 00078 * dynamically allocated fields. You will need to call pb_release() yourself. 00079 */ 00080 bool pb_decode_noinit(pb_istream_t *stream, const pb_field_t fields[], void *dest_struct); 00081 00082 /* Same as pb_decode, except expects the stream to start with the message size 00083 * encoded as varint. Corresponds to parseDelimitedFrom() in Google's 00084 * protobuf API. 00085 */ 00086 bool pb_decode_delimited(pb_istream_t *stream, const pb_field_t fields[], void *dest_struct); 00087 00088 #ifdef PB_ENABLE_MALLOC 00089 /* Release any allocated pointer fields. If you use dynamic allocation, you should 00090 * call this for any successfully decoded message when you are done with it. If 00091 * pb_decode() returns with an error, the message is already released. 00092 */ 00093 void pb_release(const pb_field_t fields[], void *dest_struct); 00094 #endif 00095 00096 00097 /************************************** 00098 * Functions for manipulating streams * 00099 **************************************/ 00100 00101 /* Create an input stream for reading from a memory buffer. 00102 * 00103 * Alternatively, you can use a custom stream that reads directly from e.g. 00104 * a file or a network socket. 00105 */ 00106 pb_istream_t pb_istream_from_buffer(const pb_byte_t *buf, size_t bufsize); 00107 00108 /* Function to read from a pb_istream_t. You can use this if you need to 00109 * read some custom header data, or to read data in field callbacks. 00110 */ 00111 bool pb_read(pb_istream_t *stream, pb_byte_t *buf, size_t count); 00112 00113 00114 /************************************************ 00115 * Helper functions for writing field callbacks * 00116 ************************************************/ 00117 00118 /* Decode the tag for the next field in the stream. Gives the wire type and 00119 * field tag. At end of the message, returns false and sets eof to true. */ 00120 bool pb_decode_tag(pb_istream_t *stream, pb_wire_type_t *wire_type, uint32_t *tag, bool *eof); 00121 00122 /* Skip the field payload data, given the wire type. */ 00123 bool pb_skip_field(pb_istream_t *stream, pb_wire_type_t wire_type); 00124 00125 /* Decode an integer in the varint format. This works for bool, enum, int32, 00126 * int64, uint32 and uint64 field types. */ 00127 bool pb_decode_varint(pb_istream_t *stream, uint64_t *dest); 00128 00129 /* Decode an integer in the zig-zagged svarint format. This works for sint32 00130 * and sint64. */ 00131 bool pb_decode_svarint(pb_istream_t *stream, int64_t *dest); 00132 00133 /* Decode a fixed32, sfixed32 or float value. You need to pass a pointer to 00134 * a 4-byte wide C variable. */ 00135 bool pb_decode_fixed32(pb_istream_t *stream, void *dest); 00136 00137 /* Decode a fixed64, sfixed64 or double value. You need to pass a pointer to 00138 * a 8-byte wide C variable. */ 00139 bool pb_decode_fixed64(pb_istream_t *stream, void *dest); 00140 00141 /* Make a limited-length substream for reading a PB_WT_STRING field. */ 00142 bool pb_make_string_substream(pb_istream_t *stream, pb_istream_t *substream); 00143 void pb_close_string_substream(pb_istream_t *stream, pb_istream_t *substream); 00144 00145 #ifdef __cplusplus 00146 } /* extern "C" */ 00147 #endif 00148 00149 #endif
Generated on Tue Jul 12 2022 16:00:22 by
