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