MOS Source Code
Loading...
Searching...
No Matches
encode_oneof.c
Go to the documentation of this file.
1/* Encode a message using callbacks inside oneof fields.
2 * For encoding, callbacks inside oneofs require nothing special
3 * so this is just normal callback usage.
4 */
5
6#include <stdio.h>
7#include <stdlib.h>
8#include <pb_encode.h>
9#include "oneof.pb.h"
10#include "test_helpers.h"
11
12/* This is a nanopb-0.4 style global callback, that is referred by function name
13 * and does not have to be bound separately to the message. It also allows defining
14 * a custom data type for the field in the structure.
15 */
16bool SubMsg3_callback(pb_istream_t *istream, pb_ostream_t *ostream, const pb_field_t *field)
17{
18 if (ostream && field->tag == SubMsg3_strvalue_tag)
19 {
20 /* Our custom data type is char* */
21 const char *str = *(const char**)field->pData;
22
23 if (!pb_encode_tag_for_field(ostream, field))
24 return false;
25
26 return pb_encode_string(ostream, (const uint8_t*)str, strlen(str));
27 }
28
29 return true;
30}
31
32/* The two callbacks below are traditional callbacks that use function pointers
33 * defined in pb_callback_t.
34 */
35bool encode_int32_array(pb_ostream_t *stream, const pb_field_t *field, void * const *arg)
36{
37 int i;
38 for (i = 0; i < 15; i++)
39 {
40 if (!pb_encode_tag_for_field(stream, field))
41 return false;
42
43 if (!pb_encode_varint(stream, i))
44 return false;
45 }
46 return true;
47}
48
49bool encode_string(pb_ostream_t *stream, const pb_field_t *field, void * const *arg)
50{
51 const char *str = "mystring";
52
53 if (!pb_encode_tag_for_field(stream, field))
54 return false;
55
56 return pb_encode_string(stream, (const uint8_t*)str, strlen(str));
57}
58
59int main(int argc, char **argv)
60{
61 uint8_t buffer[256];
62 OneOfMessage msg = OneOfMessage_init_zero;
63 pb_ostream_t stream;
64 int option;
65
66 if (argc != 2)
67 {
68 fprintf(stderr, "Usage: encode_oneof [number]\n");
69 return 1;
70 }
71 option = atoi(argv[1]);
72
73 /* Prefix and suffix are used to test that the union does not disturb
74 * other fields in the same message. */
75 msg.prefix = 123;
76
77 /* We encode one of the 'values' fields based on command line argument */
78 if (option == 1)
79 {
80 msg.which_values = OneOfMessage_intvalue_tag;
81 msg.values.intvalue = 999;
82 }
83 else if (option == 2)
84 {
85 msg.which_values = OneOfMessage_strvalue_tag;
86 strcpy(msg.values.strvalue, "abcd");
87 }
88 else if (option == 3)
89 {
90 msg.which_values = OneOfMessage_submsg1_tag;
91 msg.values.submsg1.array.funcs.encode = encode_int32_array;
92 }
93 else if (option == 4)
94 {
95 msg.which_values = OneOfMessage_submsg2_tag;
96 msg.values.submsg2.strvalue.funcs.encode = encode_string;
97 }
98 else if (option == 5)
99 {
100 msg.which_values = OneOfMessage_submsg3_tag;
101 msg.values.submsg3.which_values = SubMsg3_intvalue_tag;
102 msg.values.submsg3.values.intvalue = 1234;
103 }
104 else if (option == 6)
105 {
106 msg.which_values = OneOfMessage_submsg3_tag;
107 msg.values.submsg3.which_values = SubMsg3_strvalue_tag;
108 msg.values.submsg3.values.strvalue = "efgh";
109 }
110
111 msg.suffix = 321;
112
113 stream = pb_ostream_from_buffer(buffer, sizeof(buffer));
114
115 if (pb_encode(&stream, OneOfMessage_fields, &msg))
116 {
118 fwrite(buffer, 1, stream.bytes_written, stdout);
119 return 0;
120 }
121 else
122 {
123 fprintf(stderr, "Encoding failed: %s\n", PB_GET_ERROR(&stream));
124 return 1;
125 }
126}
MOSAPI char * strcpy(char *__restrict dest, const char *__restrict src)
Definition mos_string.c:184
#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 s32 atoi(const char *nptr)
Definition mos_stdlib.c:36
const char ** argv
Definition kmain.c:44
size_t argc
Definition kmain.c:43
bool encode_int32_array(pb_ostream_t *stream, const pb_field_t *field, void *const *arg)
bool encode_string(pb_ostream_t *stream, const pb_field_t *field, void *const *arg)
bool SubMsg3_callback(pb_istream_t *istream, pb_ostream_t *ostream, const pb_field_t *field)
#define PB_GET_ERROR(stream)
Definition pb.h:891
pb_field_iter_t pb_field_t
Definition pb.h:359
bool pb_encode_tag_for_field(pb_ostream_t *stream, const pb_field_iter_t *field)
Definition pb_encode.c:681
bool pb_encode_varint(pb_ostream_t *stream, uint64_t value)
Definition pb_encode.c:607
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
bool pb_encode_string(pb_ostream_t *stream, const pb_byte_t *buffer, size_t size)
Definition pb_encode.c:716
static size_t strlen(const char *s)
Definition pb_syshdr.h:80
unsigned char uint8_t
Definition pb_syshdr.h:20
int main()
Definition simple.cpp:6
#define SET_BINARY_MODE(file)
static char buffer[2048]
Definition test_printf.c:7