test-blobmsg-parse.c 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. #include <stdio.h>
  2. #include <stdint.h>
  3. #include <stddef.h>
  4. #include <libgen.h>
  5. #include "blobmsg.h"
  6. enum {
  7. FOO_MESSAGE,
  8. FOO_LIST,
  9. FOO_TESTDATA,
  10. __FOO_MAX
  11. };
  12. static const struct blobmsg_policy foo_policy[] = {
  13. [FOO_MESSAGE] = {
  14. .name = "message",
  15. .type = BLOBMSG_TYPE_STRING,
  16. },
  17. [FOO_LIST] = {
  18. .name = "list",
  19. .type = BLOBMSG_TYPE_ARRAY,
  20. },
  21. [FOO_TESTDATA] = {
  22. .name = "testdata",
  23. .type = BLOBMSG_TYPE_TABLE,
  24. },
  25. };
  26. static void dump_result(const char *fn, int r, const char *filename, struct blob_attr **tb)
  27. {
  28. fprintf(stdout, "%s: %s: %c%c%c (%d)\n", basename((char *) filename), fn,
  29. tb[FOO_MESSAGE] ? 'M' : '.',
  30. tb[FOO_LIST] ? 'L' : '.',
  31. tb[FOO_TESTDATA] ? 'T' : '.',
  32. r);
  33. }
  34. static void test_blobmsg(const char *filename)
  35. {
  36. #define BUF_LEN 256
  37. int r = 0;
  38. size_t len = 0;
  39. FILE *fd = NULL;
  40. char *buf = NULL;
  41. struct blob_attr *tb[__FOO_MAX];
  42. fd = fopen(filename, "r");
  43. if (!fd) {
  44. fprintf(stderr, "unable to open %s\n", filename);
  45. return;
  46. }
  47. buf = malloc(BUF_LEN+1);
  48. if (!buf)
  49. return;
  50. len = fread(buf, 1, BUF_LEN, fd);
  51. fclose(fd);
  52. r = blobmsg_parse(foo_policy, ARRAY_SIZE(foo_policy), tb, buf, len);
  53. dump_result("blobmsg_parse", r, filename, tb);
  54. r = blobmsg_parse_array(foo_policy, ARRAY_SIZE(foo_policy), tb, buf, len);
  55. dump_result("blobmsg_parse_array", r, filename, tb);
  56. free(buf);
  57. }
  58. int main(int argc, char *argv[])
  59. {
  60. if (argc != 2) {
  61. fprintf(stderr, "Usage: %s <blobmsg.bin>\n", argv[0]);
  62. return 3;
  63. }
  64. test_blobmsg(argv[1]);
  65. return 0;
  66. }