Generating and encoding messages with dynamic allocaiton
[apps/agl-service-can-low-level.git] / tests / alltypes_pointer / encode_alltypes_pointer.c
1 /* Attempts to test all the datatypes supported by ProtoBuf.
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 "test_helpers.h"
10
11 int main(int argc, char **argv)
12 {
13     int mode = (argc > 1) ? atoi(argv[1]) : 0;
14     
15     /* Initialize values to encode */
16     int32_t value_int32 = -1000;
17     int64_t value_int64 = -10000000000;
18
19     uint32_t value_uint32 = 1000;
20     uint64_t value_uint64 = 10000000000;
21
22     bool value_bool = true;
23     float value_float = 1000.0f;
24     double value_double = 1000.0f;
25
26     char *value_string = "1000";
27     AllTypes_req_bytes_t value_req_bytes;
28     AllTypes_rep_bytes_t value_rep_bytes;
29     AllTypes_opt_bytes_t value_opt_bytes;
30
31     SubMessage value_submessage = {0};
32     MyEnum value_enum = MyEnum_Truth;
33     EmptyMessage value_empty_message = {0};
34
35     /* Initialize the structure with constants */
36     AllTypes alltypes = {0};
37
38     alltypes.req_int32         = &value_int32;
39     alltypes.req_int64         = &value_int64;
40     alltypes.req_uint32        = &value_uint32;
41     alltypes.req_uint64        = &value_uint64;
42     alltypes.req_sint32        = &value_int32;
43     alltypes.req_sint64        = &value_int64;
44     alltypes.req_bool          = &value_bool;
45     
46     alltypes.req_fixed32       = &value_uint32;
47     alltypes.req_sfixed32      = &value_int32;
48     alltypes.req_float         = &value_float;
49     
50     alltypes.req_fixed64       = &value_uint64;
51     alltypes.req_sfixed64      = &value_int64;
52     alltypes.req_double        = &value_double;
53
54     value_req_bytes.bytes = (uint8_t*)"1000";
55     value_req_bytes.size  = 4;
56     
57     alltypes.req_string        = value_string;
58     alltypes.req_bytes         = &value_req_bytes;
59
60     value_submessage.substuff1 = value_string;
61     value_submessage.substuff2 = &value_int32;
62
63     alltypes.req_submsg        = &value_submessage;
64     alltypes.req_enum          = &value_enum;
65     alltypes.req_emptymsg      = &value_empty_message;
66     
67     alltypes.rep_int32_count = 1; alltypes.rep_int32 = &value_int32;
68     alltypes.rep_int64_count = 1; alltypes.rep_int64 = &value_int64;
69     alltypes.rep_uint32_count = 1; alltypes.rep_uint32 = &value_uint32;
70     alltypes.rep_uint64_count = 1; alltypes.rep_uint64 = &value_uint64;
71     alltypes.rep_sint32_count = 1; alltypes.rep_sint32 = &value_int32;
72     alltypes.rep_sint64_count = 1; alltypes.rep_sint64 = &value_int64;
73     alltypes.rep_bool_count = 1; alltypes.rep_bool = &value_bool;
74     
75     alltypes.rep_fixed32_count = 1; alltypes.rep_fixed32 = &value_uint32;
76     alltypes.rep_sfixed32_count = 1; alltypes.rep_sfixed32 = &value_int32;
77     alltypes.rep_float_count = 1; alltypes.rep_float = &value_float;
78     
79     alltypes.rep_fixed64_count = 1; alltypes.rep_fixed64 = &value_uint64;
80     alltypes.rep_sfixed64_count = 1; alltypes.rep_sfixed64 = &value_int64;
81     alltypes.rep_double_count = 1; alltypes.rep_double = &value_double;
82
83     value_rep_bytes.bytes = (uint8_t*)"1000";
84     value_rep_bytes.size  = 4;
85
86     alltypes.rep_string_count = 1; alltypes.rep_string = (char **)&value_string;
87     alltypes.rep_bytes_count = 0; alltypes.rep_bytes = &value_rep_bytes;
88
89     alltypes.rep_submsg_count = 1; alltypes.rep_submsg = &value_submessage;
90     alltypes.rep_enum_count = 1; alltypes.rep_enum = &value_enum;
91     alltypes.rep_emptymsg_count = 1; alltypes.rep_emptymsg = &value_empty_message;
92     
93     if (mode != 0)
94     {
95         /* Fill in values for optional fields */
96       alltypes.opt_int32         = &value_int32;
97       alltypes.opt_int64         = &value_int64;
98       alltypes.opt_uint32        = &value_uint32;
99       alltypes.opt_uint64        = &value_uint64;
100       alltypes.opt_sint32        = &value_int32;
101       alltypes.opt_sint64        = &value_int64;
102       alltypes.opt_bool          = &value_bool;
103     
104       alltypes.opt_fixed32       = &value_uint32;
105       alltypes.opt_sfixed32      = &value_int32;
106       alltypes.opt_float         = &value_float;
107     
108       alltypes.opt_fixed64       = &value_uint64;
109       alltypes.opt_sfixed64      = &value_int64;
110       alltypes.opt_double        = &value_double;
111     
112       value_opt_bytes.bytes = (uint8_t*)"1000";
113       value_opt_bytes.size  = 4;
114     
115       alltypes.opt_string        = value_string;
116       alltypes.opt_bytes         = &value_opt_bytes;
117
118       alltypes.opt_submsg        = &value_submessage;
119       alltypes.opt_enum          = &value_enum;
120       alltypes.opt_emptymsg      = &value_empty_message;
121     }
122     
123     alltypes.end = &value_int32;
124     
125     {
126         uint8_t buffer[4096];
127         pb_ostream_t stream = pb_ostream_from_buffer(buffer, sizeof(buffer));
128         
129         /* Now encode it and check if we succeeded. */
130         if (pb_encode(&stream, AllTypes_fields, &alltypes))
131         {
132             /*SET_BINARY_MODE(stdout);
133             fwrite(buffer, 1, stream.bytes_written, stdout);*/     /* TODO: use this to validate decoding, when implemented */
134             return 0; /* Success */
135         }
136         else
137         {
138             fprintf(stderr, "Encoding failed: %s\n", PB_GET_ERROR(&stream));
139             return 1; /* Failure */
140         }
141     }
142 }