MOS Source Code
Loading...
Searching...
No Matches
encode_extensions.c
Go to the documentation of this file.
1/* Tests extension fields.
2 */
3
4#include <stdio.h>
5#include <stdlib.h>
6#include <string.h>
7#include <pb_encode.h>
8#include "alltypes.pb.h"
9#include "extensions.pb.h"
10#include "test_helpers.h"
11
12int main(int argc, char **argv)
13{
14 uint8_t buffer[1024];
15 pb_ostream_t stream;
16
17 AllTypes alltypes = {0};
18 int32_t extensionfield1 = 12345;
19 pb_extension_t ext1 = pb_extension_init_zero;
20 ExtensionMessage extensionfield2 = {"test", 54321};
21 pb_extension_t ext2 = pb_extension_init_zero;
22
23 /* Set up the extensions */
24 alltypes.extensions = &ext1;
25
26 ext1.type = &AllTypes_extensionfield1;
27 ext1.dest = &extensionfield1;
28 ext1.next = &ext2;
29
30 ext2.type = &ExtensionMessage_AllTypes_extensionfield2;
31 ext2.dest = &extensionfield2;
32 ext2.next = NULL;
33
34 /* Set up the output stream */
35 stream = pb_ostream_from_buffer(buffer, sizeof(buffer));
36
37 /* Now encode the message and check if we succeeded. */
38 if (pb_encode(&stream, AllTypes_fields, &alltypes))
39 {
41 fwrite(buffer, 1, stream.bytes_written, stdout);
42 return 0; /* Success */
43 }
44 else
45 {
46 fprintf(stderr, "Encoding failed: %s\n", PB_GET_ERROR(&stream));
47 return 1; /* Failure */
48 }
49
50 /* Check that the field tags are properly generated */
51 (void)AllTypes_extensionfield1_tag;
52 (void)ExtensionMessage_AllTypes_extensionfield2_tag;
53}
54
#define AllTypes_fields
#define stdout
Definition mos_stdio.h:31
#define stderr
Definition mos_stdio.h:32
size_t fwrite(const void *__restrict ptr, size_t size, size_t nmemb, FILE *__restrict stream)
MOSAPI void(1, 2) fatal_abort(const char *fmt
const char ** argv
Definition kmain.c:44
size_t argc
Definition kmain.c:43
static pb_extension_t ext2
Definition mem_release.c:17
static pb_extension_t ext1
Definition mem_release.c:17
#define PB_GET_ERROR(stream)
Definition pb.h:891
#define pb_extension_init_zero
Definition pb.h:477
pb_ostream_t pb_ostream_from_buffer(pb_byte_t *buf, size_t bufsize)
Definition pb_encode.c:63
bool pb_encode(pb_ostream_t *stream, const pb_msgdesc_t *fields, const void *src_struct)
Definition pb_encode.c:512
#define NULL
Definition pb_syshdr.h:46
signed int int32_t
Definition pb_syshdr.h:23
unsigned char uint8_t
Definition pb_syshdr.h:20
int main()
Definition simple.cpp:6
pb_extension_t * extensions
#define SET_BINARY_MODE(file)
static char buffer[2048]
Definition test_printf.c:7