Nanopb files
Dependents: nanopb_V2 Message_generator LEX_Threaded_Programming_V3
pb_encode.h@0:c7beea49fc91, 2013-02-19 (annotated)
- Committer:
- intrinseca
- Date:
- Tue Feb 19 20:03:29 2013 +0000
- Revision:
- 0:c7beea49fc91
- Child:
- 2:d2c61a9be078
Initial Uploaded Version 0.1.9
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
intrinseca | 0:c7beea49fc91 | 1 | #ifndef _PB_ENCODE_H_ |
intrinseca | 0:c7beea49fc91 | 2 | #define _PB_ENCODE_H_ |
intrinseca | 0:c7beea49fc91 | 3 | |
intrinseca | 0:c7beea49fc91 | 4 | /* pb_encode.h: Functions to encode protocol buffers. Depends on pb_encode.c. |
intrinseca | 0:c7beea49fc91 | 5 | * The main function is pb_encode. You also need an output stream, structures |
intrinseca | 0:c7beea49fc91 | 6 | * and their field descriptions (just like with pb_decode). |
intrinseca | 0:c7beea49fc91 | 7 | */ |
intrinseca | 0:c7beea49fc91 | 8 | |
intrinseca | 0:c7beea49fc91 | 9 | #include <stdbool.h> |
intrinseca | 0:c7beea49fc91 | 10 | #include "pb.h" |
intrinseca | 0:c7beea49fc91 | 11 | |
intrinseca | 0:c7beea49fc91 | 12 | #ifdef __cplusplus |
intrinseca | 0:c7beea49fc91 | 13 | extern "C" { |
intrinseca | 0:c7beea49fc91 | 14 | #endif |
intrinseca | 0:c7beea49fc91 | 15 | |
intrinseca | 0:c7beea49fc91 | 16 | /* Lightweight output stream. |
intrinseca | 0:c7beea49fc91 | 17 | * You can provide callback for writing or use pb_ostream_from_buffer. |
intrinseca | 0:c7beea49fc91 | 18 | * |
intrinseca | 0:c7beea49fc91 | 19 | * Alternatively, callback can be NULL in which case the stream will just |
intrinseca | 0:c7beea49fc91 | 20 | * count the number of bytes that would have been written. In this case |
intrinseca | 0:c7beea49fc91 | 21 | * max_size is not checked. |
intrinseca | 0:c7beea49fc91 | 22 | * |
intrinseca | 0:c7beea49fc91 | 23 | * Rules for callback: |
intrinseca | 0:c7beea49fc91 | 24 | * 1) Return false on IO errors. This will cause encoding to abort. |
intrinseca | 0:c7beea49fc91 | 25 | * |
intrinseca | 0:c7beea49fc91 | 26 | * 2) You can use state to store your own data (e.g. buffer pointer). |
intrinseca | 0:c7beea49fc91 | 27 | * |
intrinseca | 0:c7beea49fc91 | 28 | * 3) pb_write will update bytes_written after your callback runs. |
intrinseca | 0:c7beea49fc91 | 29 | * |
intrinseca | 0:c7beea49fc91 | 30 | * 4) Substreams will modify max_size and bytes_written. Don't use them to |
intrinseca | 0:c7beea49fc91 | 31 | * calculate any pointers. |
intrinseca | 0:c7beea49fc91 | 32 | */ |
intrinseca | 0:c7beea49fc91 | 33 | struct _pb_ostream_t |
intrinseca | 0:c7beea49fc91 | 34 | { |
intrinseca | 0:c7beea49fc91 | 35 | #ifdef PB_BUFFER_ONLY |
intrinseca | 0:c7beea49fc91 | 36 | /* Callback pointer is not used in buffer-only configuration. |
intrinseca | 0:c7beea49fc91 | 37 | * Having an int pointer here allows binary compatibility but |
intrinseca | 0:c7beea49fc91 | 38 | * gives an error if someone tries to assign callback function. |
intrinseca | 0:c7beea49fc91 | 39 | * Also, NULL pointer marks a 'sizing stream' that does not |
intrinseca | 0:c7beea49fc91 | 40 | * write anything. |
intrinseca | 0:c7beea49fc91 | 41 | */ |
intrinseca | 0:c7beea49fc91 | 42 | int *callback; |
intrinseca | 0:c7beea49fc91 | 43 | #else |
intrinseca | 0:c7beea49fc91 | 44 | bool (*callback)(pb_ostream_t *stream, const uint8_t *buf, size_t count); |
intrinseca | 0:c7beea49fc91 | 45 | #endif |
intrinseca | 0:c7beea49fc91 | 46 | void *state; /* Free field for use by callback implementation */ |
intrinseca | 0:c7beea49fc91 | 47 | size_t max_size; /* Limit number of output bytes written (or use SIZE_MAX). */ |
intrinseca | 0:c7beea49fc91 | 48 | size_t bytes_written; |
intrinseca | 0:c7beea49fc91 | 49 | }; |
intrinseca | 0:c7beea49fc91 | 50 | |
intrinseca | 0:c7beea49fc91 | 51 | pb_ostream_t pb_ostream_from_buffer(uint8_t *buf, size_t bufsize); |
intrinseca | 0:c7beea49fc91 | 52 | bool pb_write(pb_ostream_t *stream, const uint8_t *buf, size_t count); |
intrinseca | 0:c7beea49fc91 | 53 | |
intrinseca | 0:c7beea49fc91 | 54 | /* Encode struct to given output stream. |
intrinseca | 0:c7beea49fc91 | 55 | * Returns true on success, false on any failure. |
intrinseca | 0:c7beea49fc91 | 56 | * The actual struct pointed to by src_struct must match the description in fields. |
intrinseca | 0:c7beea49fc91 | 57 | * All required fields in the struct are assumed to have been filled in. |
intrinseca | 0:c7beea49fc91 | 58 | */ |
intrinseca | 0:c7beea49fc91 | 59 | bool pb_encode(pb_ostream_t *stream, const pb_field_t fields[], const void *src_struct); |
intrinseca | 0:c7beea49fc91 | 60 | |
intrinseca | 0:c7beea49fc91 | 61 | /* --- Helper functions --- |
intrinseca | 0:c7beea49fc91 | 62 | * You may want to use these from your caller or callbacks. |
intrinseca | 0:c7beea49fc91 | 63 | */ |
intrinseca | 0:c7beea49fc91 | 64 | |
intrinseca | 0:c7beea49fc91 | 65 | /* Encode field header based on LTYPE and field number defined in the field structure. |
intrinseca | 0:c7beea49fc91 | 66 | * Call this from the callback before writing out field contents. */ |
intrinseca | 0:c7beea49fc91 | 67 | bool pb_encode_tag_for_field(pb_ostream_t *stream, const pb_field_t *field); |
intrinseca | 0:c7beea49fc91 | 68 | |
intrinseca | 0:c7beea49fc91 | 69 | /* Encode field header by manually specifing wire type. You need to use this if |
intrinseca | 0:c7beea49fc91 | 70 | * you want to write out packed arrays from a callback field. */ |
intrinseca | 0:c7beea49fc91 | 71 | bool pb_encode_tag(pb_ostream_t *stream, pb_wire_type_t wiretype, uint32_t field_number); |
intrinseca | 0:c7beea49fc91 | 72 | |
intrinseca | 0:c7beea49fc91 | 73 | /* Encode an integer in the varint format. |
intrinseca | 0:c7beea49fc91 | 74 | * This works for bool, enum, int32, int64, uint32 and uint64 field types. */ |
intrinseca | 0:c7beea49fc91 | 75 | bool pb_encode_varint(pb_ostream_t *stream, uint64_t value); |
intrinseca | 0:c7beea49fc91 | 76 | |
intrinseca | 0:c7beea49fc91 | 77 | /* Encode an integer in the zig-zagged svarint format. |
intrinseca | 0:c7beea49fc91 | 78 | * This works for sint32 and sint64. */ |
intrinseca | 0:c7beea49fc91 | 79 | bool pb_encode_svarint(pb_ostream_t *stream, int64_t value); |
intrinseca | 0:c7beea49fc91 | 80 | |
intrinseca | 0:c7beea49fc91 | 81 | /* Encode a string or bytes type field. For strings, pass strlen(s) as size. */ |
intrinseca | 0:c7beea49fc91 | 82 | bool pb_encode_string(pb_ostream_t *stream, const uint8_t *buffer, size_t size); |
intrinseca | 0:c7beea49fc91 | 83 | |
intrinseca | 0:c7beea49fc91 | 84 | /* Encode a fixed32, sfixed32 or float value. |
intrinseca | 0:c7beea49fc91 | 85 | * You need to pass a pointer to a 4-byte wide C variable. */ |
intrinseca | 0:c7beea49fc91 | 86 | bool pb_encode_fixed32(pb_ostream_t *stream, const void *value); |
intrinseca | 0:c7beea49fc91 | 87 | |
intrinseca | 0:c7beea49fc91 | 88 | /* Encode a fixed64, sfixed64 or double value. |
intrinseca | 0:c7beea49fc91 | 89 | * You need to pass a pointer to a 8-byte wide C variable. */ |
intrinseca | 0:c7beea49fc91 | 90 | bool pb_encode_fixed64(pb_ostream_t *stream, const void *value); |
intrinseca | 0:c7beea49fc91 | 91 | |
intrinseca | 0:c7beea49fc91 | 92 | /* Encode a submessage field. |
intrinseca | 0:c7beea49fc91 | 93 | * You need to pass the pb_field_t array and pointer to struct, just like with pb_encode(). |
intrinseca | 0:c7beea49fc91 | 94 | * This internally encodes the submessage twice, first to calculate message size and then to actually write it out. |
intrinseca | 0:c7beea49fc91 | 95 | */ |
intrinseca | 0:c7beea49fc91 | 96 | bool pb_encode_submessage(pb_ostream_t *stream, const pb_field_t fields[], const void *src_struct); |
intrinseca | 0:c7beea49fc91 | 97 | |
intrinseca | 0:c7beea49fc91 | 98 | /* --- Internal functions --- |
intrinseca | 0:c7beea49fc91 | 99 | * These functions are not terribly useful for the average library user, but |
intrinseca | 0:c7beea49fc91 | 100 | * are exported to make the unit testing and extending nanopb easier. |
intrinseca | 0:c7beea49fc91 | 101 | */ |
intrinseca | 0:c7beea49fc91 | 102 | |
intrinseca | 0:c7beea49fc91 | 103 | #ifdef NANOPB_INTERNALS |
intrinseca | 0:c7beea49fc91 | 104 | bool pb_enc_varint(pb_ostream_t *stream, const pb_field_t *field, const void *src); |
intrinseca | 0:c7beea49fc91 | 105 | bool pb_enc_svarint(pb_ostream_t *stream, const pb_field_t *field, const void *src); |
intrinseca | 0:c7beea49fc91 | 106 | bool pb_enc_fixed32(pb_ostream_t *stream, const pb_field_t *field, const void *src); |
intrinseca | 0:c7beea49fc91 | 107 | bool pb_enc_fixed64(pb_ostream_t *stream, const pb_field_t *field, const void *src); |
intrinseca | 0:c7beea49fc91 | 108 | bool pb_enc_bytes(pb_ostream_t *stream, const pb_field_t *field, const void *src); |
intrinseca | 0:c7beea49fc91 | 109 | bool pb_enc_string(pb_ostream_t *stream, const pb_field_t *field, const void *src); |
intrinseca | 0:c7beea49fc91 | 110 | #endif |
intrinseca | 0:c7beea49fc91 | 111 | |
intrinseca | 0:c7beea49fc91 | 112 | /* This function is not recommended for new programs. Use pb_encode_submessage() |
intrinseca | 0:c7beea49fc91 | 113 | * instead, it has the same functionality with a less confusing interface. */ |
intrinseca | 0:c7beea49fc91 | 114 | bool pb_enc_submessage(pb_ostream_t *stream, const pb_field_t *field, const void *src); |
intrinseca | 0:c7beea49fc91 | 115 | |
intrinseca | 0:c7beea49fc91 | 116 | #ifdef __cplusplus |
intrinseca | 0:c7beea49fc91 | 117 | } /* extern "C" */ |
intrinseca | 0:c7beea49fc91 | 118 | #endif |
intrinseca | 0:c7beea49fc91 | 119 | |
intrinseca | 0:c7beea49fc91 | 120 | #endif |