8 #include "blobmsg_json.h"
25 static const struct blobmsg_policy pol
[] = {
28 .type
= BLOBMSG_TYPE_STRING
,
32 .type
= BLOBMSG_TYPE_INT64
,
36 .type
= BLOBMSG_TYPE_INT64
,
40 .type
= BLOBMSG_TYPE_INT32
,
44 .type
= BLOBMSG_TYPE_INT32
,
48 .type
= BLOBMSG_TYPE_INT16
,
52 .type
= BLOBMSG_TYPE_INT16
,
56 .type
= BLOBMSG_TYPE_INT8
,
60 .type
= BLOBMSG_TYPE_INT8
,
64 .type
= BLOBMSG_TYPE_DOUBLE
,
68 .type
= BLOBMSG_TYPE_DOUBLE
,
72 static const struct blobmsg_policy pol_json
[] = {
75 .type
= BLOBMSG_TYPE_STRING
,
79 .type
= BLOBMSG_TYPE_INT64
,
83 .type
= BLOBMSG_TYPE_INT64
,
87 .type
= BLOBMSG_TYPE_INT32
,
91 .type
= BLOBMSG_TYPE_INT32
,
95 .type
= BLOBMSG_TYPE_INT32
,
99 .type
= BLOBMSG_TYPE_INT32
,
103 .type
= BLOBMSG_TYPE_INT8
,
107 .type
= BLOBMSG_TYPE_INT8
,
110 .name
= "double_max",
111 .type
= BLOBMSG_TYPE_DOUBLE
,
114 .name
= "double_min",
115 .type
= BLOBMSG_TYPE_DOUBLE
,
120 #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
123 static void dump_message(struct blob_buf
*buf
)
125 struct blob_attr
*tb
[ARRAY_SIZE(pol
)];
127 if (blobmsg_parse(pol
, ARRAY_SIZE(pol
), tb
, blob_data(buf
->head
), blob_len(buf
->head
)) != 0) {
128 fprintf(stderr
, "Parse failed\n");
132 fprintf(stderr
, "string: %s\n", blobmsg_get_string(tb
[FOO_STRING
]));
133 if (tb
[FOO_INT64_MAX
])
134 fprintf(stderr
, "int64_max: %" PRId64
"\n", (int64_t)blobmsg_get_u64(tb
[FOO_INT64_MAX
]));
135 if (tb
[FOO_INT64_MIN
])
136 fprintf(stderr
, "int64_min: %" PRId64
"\n", (int64_t)blobmsg_get_u64(tb
[FOO_INT64_MIN
]));
137 if (tb
[FOO_INT32_MAX
])
138 fprintf(stderr
, "int32_max: %" PRId32
"\n", (int32_t)blobmsg_get_u32(tb
[FOO_INT32_MAX
]));
139 if (tb
[FOO_INT32_MIN
])
140 fprintf(stderr
, "int32_min: %" PRId32
"\n", (int32_t)blobmsg_get_u32(tb
[FOO_INT32_MIN
]));
141 if (tb
[FOO_INT16_MAX
])
142 fprintf(stderr
, "int16_max: %" PRId16
"\n", (int16_t)blobmsg_get_u16(tb
[FOO_INT16_MAX
]));
143 if (tb
[FOO_INT16_MIN
])
144 fprintf(stderr
, "int16_min: %" PRId16
"\n", (int16_t)blobmsg_get_u16(tb
[FOO_INT16_MIN
]));
145 if (tb
[FOO_INT8_MAX
])
146 fprintf(stderr
, "int8_max: %" PRId8
"\n", (int8_t)blobmsg_get_u8(tb
[FOO_INT8_MAX
]));
147 if (tb
[FOO_INT8_MIN
])
148 fprintf(stderr
, "int8_min: %" PRId8
"\n", (int8_t)blobmsg_get_u8(tb
[FOO_INT8_MIN
]));
149 if (tb
[FOO_DOUBLE_MAX
])
150 fprintf(stderr
, "double_max: %e\n", blobmsg_get_double(tb
[FOO_DOUBLE_MAX
]));
151 if (tb
[FOO_DOUBLE_MIN
])
152 fprintf(stderr
, "double_min: %e\n", blobmsg_get_double(tb
[FOO_DOUBLE_MIN
]));
155 static void dump_message_cast_u64(struct blob_buf
*buf
)
157 struct blob_attr
*tb
[ARRAY_SIZE(pol
)];
159 if (blobmsg_parse(pol
, ARRAY_SIZE(pol
), tb
, blob_data(buf
->head
), blob_len(buf
->head
)) != 0) {
160 fprintf(stderr
, "Parse failed\n");
164 fprintf(stderr
, "string: %s\n", blobmsg_get_string(tb
[FOO_STRING
]));
165 if (tb
[FOO_INT64_MAX
])
166 fprintf(stderr
, "int64_max: %" PRIu64
"\n", blobmsg_cast_u64(tb
[FOO_INT64_MAX
]));
167 if (tb
[FOO_INT64_MIN
])
168 fprintf(stderr
, "int64_min: %" PRIu64
"\n", blobmsg_cast_u64(tb
[FOO_INT64_MIN
]));
169 if (tb
[FOO_INT32_MAX
])
170 fprintf(stderr
, "int32_max: %" PRIu64
"\n", blobmsg_cast_u64(tb
[FOO_INT32_MAX
]));
171 if (tb
[FOO_INT32_MIN
])
172 fprintf(stderr
, "int32_min: %" PRIu64
"\n", blobmsg_cast_u64(tb
[FOO_INT32_MIN
]));
173 if (tb
[FOO_INT16_MAX
])
174 fprintf(stderr
, "int16_max: %" PRIu64
"\n", blobmsg_cast_u64(tb
[FOO_INT16_MAX
]));
175 if (tb
[FOO_INT16_MIN
])
176 fprintf(stderr
, "int16_min: %" PRIu64
"\n", blobmsg_cast_u64(tb
[FOO_INT16_MIN
]));
177 if (tb
[FOO_INT8_MAX
])
178 fprintf(stderr
, "int8_max: %" PRIu64
"\n", blobmsg_cast_u64(tb
[FOO_INT8_MAX
]));
179 if (tb
[FOO_INT8_MIN
])
180 fprintf(stderr
, "int8_min: %" PRIu64
"\n", blobmsg_cast_u64(tb
[FOO_INT8_MIN
]));
181 if (tb
[FOO_DOUBLE_MAX
])
182 fprintf(stderr
, "double_max: %e\n", blobmsg_get_double(tb
[FOO_DOUBLE_MAX
]));
183 if (tb
[FOO_DOUBLE_MIN
])
184 fprintf(stderr
, "double_min: %e\n", blobmsg_get_double(tb
[FOO_DOUBLE_MIN
]));
187 static void dump_message_cast_s64(struct blob_buf
*buf
)
189 struct blob_attr
*tb
[ARRAY_SIZE(pol
)];
191 if (blobmsg_parse(pol
, ARRAY_SIZE(pol
), tb
, blob_data(buf
->head
), blob_len(buf
->head
)) != 0) {
192 fprintf(stderr
, "Parse failed\n");
196 fprintf(stderr
, "string: %s\n", blobmsg_get_string(tb
[FOO_STRING
]));
197 if (tb
[FOO_INT64_MAX
])
198 fprintf(stderr
, "int64_max: %" PRId64
"\n", blobmsg_cast_s64(tb
[FOO_INT64_MAX
]));
199 if (tb
[FOO_INT64_MIN
])
200 fprintf(stderr
, "int64_min: %" PRId64
"\n", blobmsg_cast_s64(tb
[FOO_INT64_MIN
]));
201 if (tb
[FOO_INT32_MAX
])
202 fprintf(stderr
, "int32_max: %" PRId64
"\n", blobmsg_cast_s64(tb
[FOO_INT32_MAX
]));
203 if (tb
[FOO_INT32_MIN
])
204 fprintf(stderr
, "int32_min: %" PRId64
"\n", blobmsg_cast_s64(tb
[FOO_INT32_MIN
]));
205 if (tb
[FOO_INT16_MAX
])
206 fprintf(stderr
, "int16_max: %" PRId64
"\n", blobmsg_cast_s64(tb
[FOO_INT16_MAX
]));
207 if (tb
[FOO_INT16_MIN
])
208 fprintf(stderr
, "int16_min: %" PRId64
"\n", blobmsg_cast_s64(tb
[FOO_INT16_MIN
]));
209 if (tb
[FOO_INT8_MAX
])
210 fprintf(stderr
, "int8_max: %" PRId64
"\n", blobmsg_cast_s64(tb
[FOO_INT8_MAX
]));
211 if (tb
[FOO_INT8_MIN
])
212 fprintf(stderr
, "int8_min: %" PRId64
"\n", blobmsg_cast_s64(tb
[FOO_INT8_MIN
]));
213 if (tb
[FOO_DOUBLE_MAX
])
214 fprintf(stderr
, "double_max: %e\n", blobmsg_get_double(tb
[FOO_DOUBLE_MAX
]));
215 if (tb
[FOO_DOUBLE_MIN
])
216 fprintf(stderr
, "double_min: %e\n", blobmsg_get_double(tb
[FOO_DOUBLE_MIN
]));
219 static void dump_message_json(struct blob_buf
*buf
)
221 struct blob_attr
*tb
[ARRAY_SIZE(pol
)];
223 if (blobmsg_parse(pol_json
, ARRAY_SIZE(pol_json
), tb
, blob_data(buf
->head
), blob_len(buf
->head
)) != 0) {
224 fprintf(stderr
, "Parse failed\n");
228 fprintf(stderr
, "string: %s\n", blobmsg_get_string(tb
[FOO_STRING
]));
229 if (tb
[FOO_INT64_MAX
])
230 fprintf(stderr
, "int64_max: %" PRId64
"\n", blobmsg_get_u64(tb
[FOO_INT64_MAX
]));
231 if (tb
[FOO_INT64_MIN
])
232 fprintf(stderr
, "int64_min: %" PRId64
"\n", blobmsg_get_u64(tb
[FOO_INT64_MIN
]));
233 if (tb
[FOO_INT32_MAX
])
234 fprintf(stderr
, "int32_max: %" PRId32
"\n", blobmsg_get_u32(tb
[FOO_INT32_MAX
]));
235 if (tb
[FOO_INT32_MIN
])
236 fprintf(stderr
, "int32_min: %" PRId32
"\n", blobmsg_get_u32(tb
[FOO_INT32_MIN
]));
237 /* u16 is unknown to json, retrieve as u32 */
238 if (tb
[FOO_INT16_MAX
])
239 fprintf(stderr
, "int16_max: %" PRId32
"\n", blobmsg_get_u32(tb
[FOO_INT16_MAX
]));
240 /* u16 is unknown to json, retrieve as u32 */
241 if (tb
[FOO_INT16_MIN
])
242 fprintf(stderr
, "int16_min: %" PRId32
"\n", blobmsg_get_u32(tb
[FOO_INT16_MIN
]));
243 /* u8 is converted to boolean (true: all values != 0/false: value 0) in json */
244 if (tb
[FOO_INT8_MAX
])
245 fprintf(stderr
, "int8_max: %" PRId8
"\n", blobmsg_get_u8(tb
[FOO_INT8_MAX
]));
246 /* u8 is converted to boolean (true: all values != 0/false: value 0) in json */
247 if (tb
[FOO_INT8_MIN
])
248 fprintf(stderr
, "int8_min: %" PRId8
"\n", blobmsg_get_u8(tb
[FOO_INT8_MIN
]));
249 if (tb
[FOO_DOUBLE_MAX
])
250 fprintf(stderr
, "double_max: %e\n", blobmsg_get_double(tb
[FOO_DOUBLE_MAX
]));
251 if (tb
[FOO_DOUBLE_MIN
])
252 fprintf(stderr
, "double_min: %e\n", blobmsg_get_double(tb
[FOO_DOUBLE_MIN
]));
255 static void dump_message_cast_u64_json(struct blob_buf
*buf
)
257 struct blob_attr
*tb
[ARRAY_SIZE(pol
)];
259 if (blobmsg_parse(pol_json
, ARRAY_SIZE(pol_json
), tb
, blob_data(buf
->head
), blob_len(buf
->head
)) != 0) {
260 fprintf(stderr
, "Parse failed\n");
264 fprintf(stderr
, "string: %s\n", blobmsg_get_string(tb
[FOO_STRING
]));
265 if (tb
[FOO_INT64_MAX
])
266 fprintf(stderr
, "int64_max: %" PRIu64
"\n", blobmsg_cast_u64(tb
[FOO_INT64_MAX
]));
267 if (tb
[FOO_INT64_MIN
])
268 fprintf(stderr
, "int64_min: %" PRIu64
"\n", blobmsg_cast_u64(tb
[FOO_INT64_MIN
]));
269 if (tb
[FOO_INT32_MAX
])
270 fprintf(stderr
, "int32_max: %" PRIu64
"\n", blobmsg_cast_u64(tb
[FOO_INT32_MAX
]));
271 if (tb
[FOO_INT32_MIN
])
272 fprintf(stderr
, "int32_min: %" PRIu64
"\n", blobmsg_cast_u64(tb
[FOO_INT32_MIN
]));
273 if (tb
[FOO_INT16_MAX
])
274 fprintf(stderr
, "int16_max: %" PRIu64
"\n", blobmsg_cast_u64(tb
[FOO_INT16_MAX
]));
275 if (tb
[FOO_INT16_MIN
])
276 fprintf(stderr
, "int16_min: %" PRIu64
"\n", blobmsg_cast_u64(tb
[FOO_INT16_MIN
]));
277 if (tb
[FOO_INT8_MAX
])
278 fprintf(stderr
, "int8_max: %" PRIu64
"\n", blobmsg_cast_u64(tb
[FOO_INT8_MAX
]));
279 if (tb
[FOO_INT8_MIN
])
280 fprintf(stderr
, "int8_min: %" PRIu64
"\n", blobmsg_cast_u64(tb
[FOO_INT8_MIN
]));
281 if (tb
[FOO_DOUBLE_MAX
])
282 fprintf(stderr
, "double_max: %e\n", blobmsg_get_double(tb
[FOO_DOUBLE_MAX
]));
283 if (tb
[FOO_DOUBLE_MIN
])
284 fprintf(stderr
, "double_min: %e\n", blobmsg_get_double(tb
[FOO_DOUBLE_MIN
]));
287 static void dump_message_cast_s64_json(struct blob_buf
*buf
)
289 struct blob_attr
*tb
[ARRAY_SIZE(pol
)];
291 if (blobmsg_parse(pol_json
, ARRAY_SIZE(pol_json
), tb
, blob_data(buf
->head
), blob_len(buf
->head
)) != 0) {
292 fprintf(stderr
, "Parse failed\n");
296 fprintf(stderr
, "string: %s\n", blobmsg_get_string(tb
[FOO_STRING
]));
297 if (tb
[FOO_INT64_MAX
])
298 fprintf(stderr
, "int64_max: %" PRId64
"\n", blobmsg_cast_s64(tb
[FOO_INT64_MAX
]));
299 if (tb
[FOO_INT64_MIN
])
300 fprintf(stderr
, "int64_min: %" PRId64
"\n", blobmsg_cast_s64(tb
[FOO_INT64_MIN
]));
301 if (tb
[FOO_INT32_MAX
])
302 fprintf(stderr
, "int32_max: %" PRId64
"\n", blobmsg_cast_s64(tb
[FOO_INT32_MAX
]));
303 if (tb
[FOO_INT32_MIN
])
304 fprintf(stderr
, "int32_min: %" PRId64
"\n", blobmsg_cast_s64(tb
[FOO_INT32_MIN
]));
305 if (tb
[FOO_INT16_MAX
])
306 fprintf(stderr
, "int16_max: %" PRId64
"\n", blobmsg_cast_s64(tb
[FOO_INT16_MAX
]));
307 if (tb
[FOO_INT16_MIN
])
308 fprintf(stderr
, "int16_min: %" PRId64
"\n", blobmsg_cast_s64(tb
[FOO_INT16_MIN
]));
309 if (tb
[FOO_INT8_MAX
])
310 fprintf(stderr
, "int8_max: %" PRId64
"\n", blobmsg_cast_s64(tb
[FOO_INT8_MAX
]));
311 if (tb
[FOO_INT8_MIN
])
312 fprintf(stderr
, "int8_min: %" PRId64
"\n", blobmsg_cast_s64(tb
[FOO_INT8_MIN
]));
313 if (tb
[FOO_DOUBLE_MAX
])
314 fprintf(stderr
, "double_max: %e\n", blobmsg_get_double(tb
[FOO_DOUBLE_MAX
]));
315 if (tb
[FOO_DOUBLE_MIN
])
316 fprintf(stderr
, "double_min: %e\n", blobmsg_get_double(tb
[FOO_DOUBLE_MIN
]));
320 fill_message(struct blob_buf
*buf
)
322 blobmsg_add_string(buf
, "string", "Hello, world!");
323 blobmsg_add_u64(buf
, "int64_max", INT64_MAX
);
324 blobmsg_add_u64(buf
, "int64_min", INT64_MIN
);
325 blobmsg_add_u32(buf
, "int32_max", INT32_MAX
);
326 blobmsg_add_u32(buf
, "int32_min", INT32_MIN
);
327 blobmsg_add_u16(buf
, "int16_max", INT16_MAX
);
328 blobmsg_add_u16(buf
, "int16_min", INT16_MIN
);
329 blobmsg_add_u8(buf
, "int8_max", INT8_MAX
);
330 blobmsg_add_u8(buf
, "int8_min", INT8_MIN
);
331 blobmsg_add_double(buf
, "double_max", DBL_MAX
);
332 blobmsg_add_double(buf
, "double_min", DBL_MIN
);
335 int main(int argc
, char **argv
)
338 static struct blob_buf buf
;
340 blobmsg_buf_init(&buf
);
342 fprintf(stderr
, "[*] blobmsg dump:\n");
344 fprintf(stderr
, "[*] blobmsg dump cast_u64:\n");
345 dump_message_cast_u64(&buf
);
346 fprintf(stderr
, "[*] blobmsg dump cast_s64:\n");
347 dump_message_cast_s64(&buf
);
349 json
= blobmsg_format_json(buf
.head
, true);
353 fprintf(stderr
, "\n[*] blobmsg to json: %s\n", json
);
355 blobmsg_buf_init(&buf
);
356 if (!blobmsg_add_json_from_string(&buf
, json
))
359 fprintf(stderr
, "\n[*] blobmsg from json:\n");
360 dump_message_json(&buf
);
361 fprintf(stderr
, "\n[*] blobmsg from json/cast_u64:\n");
362 dump_message_cast_u64_json(&buf
);
363 fprintf(stderr
, "\n[*] blobmsg from json/cast_s64:\n");
364 dump_message_cast_s64_json(&buf
);