test-blobmsg.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. #include <stdio.h>
  2. #include <float.h>
  3. #include <limits.h>
  4. #include <stdint.h>
  5. #include <inttypes.h>
  6. #include "blobmsg.h"
  7. #include "blobmsg_json.h"
  8. static const char *indent_str = "\t\t\t\t\t\t\t\t\t\t\t\t\t";
  9. #define indent_printf(indent, ...) do { \
  10. if (indent > 0) \
  11. fwrite(indent_str, indent, 1, stderr); \
  12. fprintf(stderr, __VA_ARGS__); \
  13. } while(0)
  14. static void dump_attr_data(struct blob_attr *data, int indent, int next_indent);
  15. static void
  16. dump_table(struct blob_attr *head, size_t len, int indent, bool array)
  17. {
  18. struct blob_attr *attr;
  19. struct blobmsg_hdr *hdr;
  20. indent_printf(indent, "{\n");
  21. __blob_for_each_attr(attr, head, len) {
  22. hdr = blob_data(attr);
  23. if (!array)
  24. indent_printf(indent + 1, "%s : ", hdr->name);
  25. dump_attr_data(attr, 0, indent + 1);
  26. }
  27. indent_printf(indent, "}\n");
  28. }
  29. static void dump_attr_data(struct blob_attr *data, int indent, int next_indent)
  30. {
  31. int type = blobmsg_type(data);
  32. switch(type) {
  33. case BLOBMSG_TYPE_STRING:
  34. indent_printf(indent, "%s (str)\n", blobmsg_get_string(data));
  35. break;
  36. case BLOBMSG_TYPE_INT8:
  37. indent_printf(indent, "%d (i8)\n", (int8_t) blobmsg_get_u8(data));
  38. break;
  39. case BLOBMSG_TYPE_INT16:
  40. indent_printf(indent, "%d (i16)\n", (int16_t) blobmsg_get_u16(data));
  41. break;
  42. case BLOBMSG_TYPE_INT32:
  43. indent_printf(indent, "%d (i32)\n", (int32_t) blobmsg_get_u32(data));
  44. break;
  45. case BLOBMSG_TYPE_INT64:
  46. indent_printf(indent, "%"PRId64" (i64)\n", (int64_t) blobmsg_get_u64(data));
  47. break;
  48. case BLOBMSG_TYPE_DOUBLE:
  49. indent_printf(indent, "%lf (dbl)\n", blobmsg_get_double(data));
  50. break;
  51. case BLOBMSG_TYPE_TABLE:
  52. case BLOBMSG_TYPE_ARRAY:
  53. if (!indent)
  54. indent_printf(indent, "\n");
  55. dump_table(blobmsg_data(data), blobmsg_data_len(data),
  56. next_indent, type == BLOBMSG_TYPE_ARRAY);
  57. break;
  58. }
  59. }
  60. enum {
  61. FOO_MESSAGE,
  62. FOO_LIST,
  63. FOO_TESTDATA
  64. };
  65. static const struct blobmsg_policy pol[] = {
  66. [FOO_MESSAGE] = {
  67. .name = "message",
  68. .type = BLOBMSG_TYPE_STRING,
  69. },
  70. [FOO_LIST] = {
  71. .name = "list",
  72. .type = BLOBMSG_TYPE_ARRAY,
  73. },
  74. [FOO_TESTDATA] = {
  75. .name = "testdata",
  76. .type = BLOBMSG_TYPE_TABLE,
  77. },
  78. };
  79. #ifndef ARRAY_SIZE
  80. #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
  81. #endif
  82. static void dump_message(struct blob_buf *buf)
  83. {
  84. struct blob_attr *tb[ARRAY_SIZE(pol)];
  85. if (blobmsg_parse(pol, ARRAY_SIZE(pol), tb, blob_data(buf->head), blob_len(buf->head)) != 0) {
  86. fprintf(stderr, "Parse failed\n");
  87. return;
  88. }
  89. if (tb[FOO_MESSAGE])
  90. fprintf(stderr, "Message: %s\n", (char *) blobmsg_data(tb[FOO_MESSAGE]));
  91. if (tb[FOO_LIST]) {
  92. fprintf(stderr, "List: ");
  93. dump_table(blobmsg_data(tb[FOO_LIST]), blobmsg_data_len(tb[FOO_LIST]), 0, true);
  94. }
  95. if (tb[FOO_TESTDATA]) {
  96. fprintf(stderr, "Testdata: ");
  97. dump_table(blobmsg_data(tb[FOO_TESTDATA]), blobmsg_data_len(tb[FOO_TESTDATA]), 0, false);
  98. }
  99. }
  100. static void
  101. fill_message(struct blob_buf *buf)
  102. {
  103. void *tbl;
  104. blobmsg_add_string(buf, "message", "Hello, world!");
  105. tbl = blobmsg_open_table(buf, "testdata");
  106. blobmsg_add_double(buf, "dbl-min", DBL_MIN);
  107. blobmsg_add_double(buf, "dbl-max", DBL_MAX);
  108. blobmsg_add_u8(buf, "foo", 0);
  109. blobmsg_add_u8(buf, "poo", 100);
  110. blobmsg_add_u8(buf, "moo-min", INT8_MIN);
  111. blobmsg_add_u8(buf, "moo-max", INT8_MAX);
  112. blobmsg_add_u16(buf, "bar-min", INT16_MIN);
  113. blobmsg_add_u16(buf, "bar-max", INT16_MAX);
  114. blobmsg_add_u32(buf, "baz-min", INT32_MIN);
  115. blobmsg_add_u32(buf, "baz-max", INT32_MAX);
  116. blobmsg_add_u64(buf, "taz-min", INT64_MIN);
  117. blobmsg_add_u64(buf, "taz-max", INT64_MAX);
  118. blobmsg_add_string(buf, "world", "2");
  119. blobmsg_close_table(buf, tbl);
  120. tbl = blobmsg_open_array(buf, "list");
  121. blobmsg_add_u8(buf, NULL, 0);
  122. blobmsg_add_u8(buf, NULL, 100);
  123. blobmsg_add_u8(buf, NULL, INT8_MIN);
  124. blobmsg_add_u8(buf, NULL, INT8_MAX);
  125. blobmsg_add_u16(buf, NULL, INT16_MIN);
  126. blobmsg_add_u16(buf, NULL, INT16_MAX);
  127. blobmsg_add_u32(buf, NULL, INT32_MIN);
  128. blobmsg_add_u32(buf, NULL, INT32_MAX);
  129. blobmsg_add_u64(buf, NULL, INT64_MIN);
  130. blobmsg_add_u64(buf, NULL, INT64_MAX);
  131. blobmsg_add_double(buf, NULL, DBL_MIN);
  132. blobmsg_add_double(buf, NULL, DBL_MAX);
  133. blobmsg_close_table(buf, tbl);
  134. }
  135. int main(int argc, char **argv)
  136. {
  137. char *json = NULL;
  138. static struct blob_buf buf;
  139. blobmsg_buf_init(&buf);
  140. fill_message(&buf);
  141. fprintf(stderr, "[*] blobmsg dump:\n");
  142. dump_message(&buf);
  143. json = blobmsg_format_json(buf.head, true);
  144. if (!json)
  145. exit(EXIT_FAILURE);
  146. fprintf(stderr, "\n[*] blobmsg to json: %s\n", json);
  147. blobmsg_buf_init(&buf);
  148. if (!blobmsg_add_json_from_string(&buf, json))
  149. exit(EXIT_FAILURE);
  150. fprintf(stderr, "\n[*] blobmsg from json:\n");
  151. dump_message(&buf);
  152. if (buf.buf)
  153. free(buf.buf);
  154. free(json);
  155. return 0;
  156. }