this is a sample for mbed(LPC1768)
serialization_lib/pb_decode.h@0:3163adfd2cf1, 2015-11-19 (annotated)
- Committer:
- 1
- Date:
- Thu Nov 19 10:17:55 2015 +0800
- Revision:
- 0:3163adfd2cf1
????????????????????????
1.callback
2.thread
3.auto-connect,time-up
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
1 | 0:3163adfd2cf1 | 1 | /* pb_decode.h: Functions to decode protocol buffers. Depends on pb_decode.c. |
1 | 0:3163adfd2cf1 | 2 | * The main function is pb_decode. You also need an input stream, and the |
1 | 0:3163adfd2cf1 | 3 | * field descriptions created by nanopb_generator.py. |
1 | 0:3163adfd2cf1 | 4 | */ |
1 | 0:3163adfd2cf1 | 5 | |
1 | 0:3163adfd2cf1 | 6 | #ifndef _PB_DECODE_H_ |
1 | 0:3163adfd2cf1 | 7 | #define _PB_DECODE_H_ |
1 | 0:3163adfd2cf1 | 8 | |
1 | 0:3163adfd2cf1 | 9 | #include "pb.h" |
1 | 0:3163adfd2cf1 | 10 | |
1 | 0:3163adfd2cf1 | 11 | #ifdef __cplusplus |
1 | 0:3163adfd2cf1 | 12 | extern "C" { |
1 | 0:3163adfd2cf1 | 13 | #endif |
1 | 0:3163adfd2cf1 | 14 | |
1 | 0:3163adfd2cf1 | 15 | /* Structure for defining custom input streams. You will need to provide |
1 | 0:3163adfd2cf1 | 16 | * a callback function to read the bytes from your storage, which can be |
1 | 0:3163adfd2cf1 | 17 | * for example a file or a network socket. |
1 | 0:3163adfd2cf1 | 18 | * |
1 | 0:3163adfd2cf1 | 19 | * The callback must conform to these rules: |
1 | 0:3163adfd2cf1 | 20 | * |
1 | 0:3163adfd2cf1 | 21 | * 1) Return false on IO errors. This will cause decoding to abort. |
1 | 0:3163adfd2cf1 | 22 | * 2) You can use state to store your own data (e.g. buffer pointer), |
1 | 0:3163adfd2cf1 | 23 | * and rely on pb_read to verify that no-body reads past bytes_left. |
1 | 0:3163adfd2cf1 | 24 | * 3) Your callback may be used with substreams, in which case bytes_left |
1 | 0:3163adfd2cf1 | 25 | * is different than from the main stream. Don't use bytes_left to compute |
1 | 0:3163adfd2cf1 | 26 | * any pointers. |
1 | 0:3163adfd2cf1 | 27 | */ |
1 | 0:3163adfd2cf1 | 28 | struct _pb_istream_t |
1 | 0:3163adfd2cf1 | 29 | { |
1 | 0:3163adfd2cf1 | 30 | #ifdef PB_BUFFER_ONLY |
1 | 0:3163adfd2cf1 | 31 | /* Callback pointer is not used in buffer-only configuration. |
1 | 0:3163adfd2cf1 | 32 | * Having an int pointer here allows binary compatibility but |
1 | 0:3163adfd2cf1 | 33 | * gives an error if someone tries to assign callback function. |
1 | 0:3163adfd2cf1 | 34 | */ |
1 | 0:3163adfd2cf1 | 35 | int *callback; |
1 | 0:3163adfd2cf1 | 36 | #else |
1 | 0:3163adfd2cf1 | 37 | bool (*callback)(pb_istream_t *stream, uint8_t *buf, size_t count); |
1 | 0:3163adfd2cf1 | 38 | #endif |
1 | 0:3163adfd2cf1 | 39 | |
1 | 0:3163adfd2cf1 | 40 | void *state; /* Free field for use by callback implementation */ |
1 | 0:3163adfd2cf1 | 41 | size_t bytes_left; |
1 | 0:3163adfd2cf1 | 42 | |
1 | 0:3163adfd2cf1 | 43 | #ifndef PB_NO_ERRMSG |
1 | 0:3163adfd2cf1 | 44 | const char *errmsg; |
1 | 0:3163adfd2cf1 | 45 | #endif |
1 | 0:3163adfd2cf1 | 46 | }; |
1 | 0:3163adfd2cf1 | 47 | |
1 | 0:3163adfd2cf1 | 48 | /*************************** |
1 | 0:3163adfd2cf1 | 49 | * Main decoding functions * |
1 | 0:3163adfd2cf1 | 50 | ***************************/ |
1 | 0:3163adfd2cf1 | 51 | |
1 | 0:3163adfd2cf1 | 52 | /* Decode a single protocol_lib buffers message from input stream into a C structure. |
1 | 0:3163adfd2cf1 | 53 | * Returns true on success, false on any failure. |
1 | 0:3163adfd2cf1 | 54 | * The actual struct pointed to by dest must match the description in fields. |
1 | 0:3163adfd2cf1 | 55 | * Callback fields of the destination structure must be initialized by caller. |
1 | 0:3163adfd2cf1 | 56 | * All other fields will be initialized by this function. |
1 | 0:3163adfd2cf1 | 57 | * |
1 | 0:3163adfd2cf1 | 58 | * Example usage: |
1 | 0:3163adfd2cf1 | 59 | * MyMessage msg = {}; |
1 | 0:3163adfd2cf1 | 60 | * uint8_t buffer[64]; |
1 | 0:3163adfd2cf1 | 61 | * pb_istream_t stream; |
1 | 0:3163adfd2cf1 | 62 | * |
1 | 0:3163adfd2cf1 | 63 | * // ... read some data into buffer ... |
1 | 0:3163adfd2cf1 | 64 | * |
1 | 0:3163adfd2cf1 | 65 | * stream = pb_istream_from_buffer(buffer, count); |
1 | 0:3163adfd2cf1 | 66 | * pb_decode(&stream, MyMessage_fields, &msg); |
1 | 0:3163adfd2cf1 | 67 | */ |
1 | 0:3163adfd2cf1 | 68 | bool pb_decode(pb_istream_t *stream, const pb_field_t fields[], void *dest_struct); |
1 | 0:3163adfd2cf1 | 69 | |
1 | 0:3163adfd2cf1 | 70 | /* Same as pb_decode, except does not initialize the destination structure |
1 | 0:3163adfd2cf1 | 71 | * to default values. This is slightly faster if you need no default values |
1 | 0:3163adfd2cf1 | 72 | * and just do memset(struct, 0, sizeof(struct)) yourself. |
1 | 0:3163adfd2cf1 | 73 | * |
1 | 0:3163adfd2cf1 | 74 | * This can also be used for 'merging' two messages, i.e. update only the |
1 | 0:3163adfd2cf1 | 75 | * fields that exist in the new message. |
1 | 0:3163adfd2cf1 | 76 | */ |
1 | 0:3163adfd2cf1 | 77 | bool pb_decode_noinit(pb_istream_t *stream, const pb_field_t fields[], void *dest_struct); |
1 | 0:3163adfd2cf1 | 78 | |
1 | 0:3163adfd2cf1 | 79 | /* Same as pb_decode, except expects the stream to start with the message size |
1 | 0:3163adfd2cf1 | 80 | * encoded as varint. Corresponds to parseDelimitedFrom() in Google's |
1 | 0:3163adfd2cf1 | 81 | * protobuf API. |
1 | 0:3163adfd2cf1 | 82 | */ |
1 | 0:3163adfd2cf1 | 83 | bool pb_decode_delimited(pb_istream_t *stream, const pb_field_t fields[], void *dest_struct); |
1 | 0:3163adfd2cf1 | 84 | |
1 | 0:3163adfd2cf1 | 85 | |
1 | 0:3163adfd2cf1 | 86 | /************************************** |
1 | 0:3163adfd2cf1 | 87 | * Functions for manipulating streams * |
1 | 0:3163adfd2cf1 | 88 | **************************************/ |
1 | 0:3163adfd2cf1 | 89 | |
1 | 0:3163adfd2cf1 | 90 | /* Create an input stream for reading from a memory buffer. |
1 | 0:3163adfd2cf1 | 91 | * |
1 | 0:3163adfd2cf1 | 92 | * Alternatively, you can use a custom stream that reads directly from e.g. |
1 | 0:3163adfd2cf1 | 93 | * a file or a network socket. |
1 | 0:3163adfd2cf1 | 94 | */ |
1 | 0:3163adfd2cf1 | 95 | pb_istream_t pb_istream_from_buffer(uint8_t *buf, size_t bufsize); |
1 | 0:3163adfd2cf1 | 96 | |
1 | 0:3163adfd2cf1 | 97 | /* Function to read from a pb_istream_t. You can use this if you need to |
1 | 0:3163adfd2cf1 | 98 | * read some custom header data, or to read data in field callbacks. |
1 | 0:3163adfd2cf1 | 99 | */ |
1 | 0:3163adfd2cf1 | 100 | bool pb_read(pb_istream_t *stream, uint8_t *buf, size_t count); |
1 | 0:3163adfd2cf1 | 101 | |
1 | 0:3163adfd2cf1 | 102 | |
1 | 0:3163adfd2cf1 | 103 | /************************************************ |
1 | 0:3163adfd2cf1 | 104 | * Helper functions for writing field callbacks * |
1 | 0:3163adfd2cf1 | 105 | ************************************************/ |
1 | 0:3163adfd2cf1 | 106 | |
1 | 0:3163adfd2cf1 | 107 | /* Decode the tag for the next field in the stream. Gives the wire type and |
1 | 0:3163adfd2cf1 | 108 | * field tag. At end of the message, returns false and sets eof to true. */ |
1 | 0:3163adfd2cf1 | 109 | bool pb_decode_tag(pb_istream_t *stream, pb_wire_type_t *wire_type, uint32_t *tag, bool *eof); |
1 | 0:3163adfd2cf1 | 110 | |
1 | 0:3163adfd2cf1 | 111 | /* Skip the field payload data, given the wire type. */ |
1 | 0:3163adfd2cf1 | 112 | bool pb_skip_field(pb_istream_t *stream, pb_wire_type_t wire_type); |
1 | 0:3163adfd2cf1 | 113 | |
1 | 0:3163adfd2cf1 | 114 | /* Decode an integer in the varint format. This works for bool, enum, int32, |
1 | 0:3163adfd2cf1 | 115 | * int64, uint32 and uint64 field types. */ |
1 | 0:3163adfd2cf1 | 116 | bool pb_decode_varint(pb_istream_t *stream, uint64_t *dest); |
1 | 0:3163adfd2cf1 | 117 | |
1 | 0:3163adfd2cf1 | 118 | /* Decode an integer in the zig-zagged svarint format. This works for sint32 |
1 | 0:3163adfd2cf1 | 119 | * and sint64. */ |
1 | 0:3163adfd2cf1 | 120 | bool pb_decode_svarint(pb_istream_t *stream, int64_t *dest); |
1 | 0:3163adfd2cf1 | 121 | |
1 | 0:3163adfd2cf1 | 122 | /* Decode a fixed32, sfixed32 or float value. You need to pass a pointer to |
1 | 0:3163adfd2cf1 | 123 | * a 4-byte wide C variable. */ |
1 | 0:3163adfd2cf1 | 124 | bool pb_decode_fixed32(pb_istream_t *stream, void *dest); |
1 | 0:3163adfd2cf1 | 125 | |
1 | 0:3163adfd2cf1 | 126 | /* Decode a fixed64, sfixed64 or double value. You need to pass a pointer to |
1 | 0:3163adfd2cf1 | 127 | * a 8-byte wide C variable. */ |
1 | 0:3163adfd2cf1 | 128 | bool pb_decode_fixed64(pb_istream_t *stream, void *dest); |
1 | 0:3163adfd2cf1 | 129 | |
1 | 0:3163adfd2cf1 | 130 | /* Make a limited-length substream for reading a PB_WT_STRING field. */ |
1 | 0:3163adfd2cf1 | 131 | bool pb_make_string_substream(pb_istream_t *stream, pb_istream_t *substream); |
1 | 0:3163adfd2cf1 | 132 | void pb_close_string_substream(pb_istream_t *stream, pb_istream_t *substream); |
1 | 0:3163adfd2cf1 | 133 | |
1 | 0:3163adfd2cf1 | 134 | #ifdef __cplusplus |
1 | 0:3163adfd2cf1 | 135 | } /* extern "C" */ |
1 | 0:3163adfd2cf1 | 136 | #endif |
1 | 0:3163adfd2cf1 | 137 | |
1 | 0:3163adfd2cf1 | 138 | #endif |