libubox
C utility functions for OpenWrt.
test-blobmsg.c File Reference
#include <stdio.h>
#include <float.h>
#include <limits.h>
#include <stdint.h>
#include <inttypes.h>
#include "blobmsg.h"
#include "blobmsg_json.h"

Go to the source code of this file.

Macros

#define indent_printf(indent, ...)
 
#define ARRAY_SIZE(x)   (sizeof(x) / sizeof((x)[0]))
 

Enumerations

enum  { FOO_MESSAGE , FOO_LIST , FOO_TESTDATA }
 

Functions

static void dump_attr_data (struct blob_attr *data, int indent, int next_indent)
 
static void dump_table (struct blob_attr *head, size_t len, int indent, bool array)
 
static void dump_message (struct blob_buf *buf)
 
static void fill_message (struct blob_buf *buf)
 
int main (int argc, char **argv)
 

Variables

static const char * indent_str = "\t\t\t\t\t\t\t\t\t\t\t\t\t"
 
static const struct blobmsg_policy pol []
 

Macro Definition Documentation

◆ ARRAY_SIZE

#define ARRAY_SIZE (   x)    (sizeof(x) / sizeof((x)[0]))

Definition at line 90 of file test-blobmsg.c.

◆ indent_printf

#define indent_printf (   indent,
  ... 
)
Value:
do { \
if (indent > 0) \
fwrite(indent_str, indent, 1, stderr); \
fprintf(stderr, __VA_ARGS__); \
} while(0)
static const char * indent_str
Definition: test-blobmsg.c:10

Definition at line 12 of file test-blobmsg.c.

Enumeration Type Documentation

◆ anonymous enum

anonymous enum
Enumerator
FOO_MESSAGE 
FOO_LIST 
FOO_TESTDATA 

Definition at line 68 of file test-blobmsg.c.

68  {
70  FOO_LIST,
72 };
@ FOO_TESTDATA
Definition: test-blobmsg.c:71
@ FOO_MESSAGE
Definition: test-blobmsg.c:69
@ FOO_LIST
Definition: test-blobmsg.c:70

Function Documentation

◆ dump_attr_data()

static void dump_attr_data ( struct blob_attr data,
int  indent,
int  next_indent 
)
static

Definition at line 36 of file test-blobmsg.c.

37 {
38  int type = blobmsg_type(data);
39  switch(type) {
41  indent_printf(indent, "%s (str)\n", blobmsg_get_string(data));
42  break;
43  case BLOBMSG_TYPE_INT8:
44  indent_printf(indent, "%d (i8)\n", (int8_t) blobmsg_get_u8(data));
45  break;
46  case BLOBMSG_TYPE_INT16:
47  indent_printf(indent, "%d (i16)\n", (int16_t) blobmsg_get_u16(data));
48  break;
49  case BLOBMSG_TYPE_INT32:
50  indent_printf(indent, "%d (i32)\n", (int32_t) blobmsg_get_u32(data));
51  break;
52  case BLOBMSG_TYPE_INT64:
53  indent_printf(indent, "%"PRId64" (i64)\n", (int64_t) blobmsg_get_u64(data));
54  break;
56  indent_printf(indent, "%lf (dbl)\n", blobmsg_get_double(data));
57  break;
58  case BLOBMSG_TYPE_TABLE:
59  case BLOBMSG_TYPE_ARRAY:
60  if (!indent)
61  indent_printf(indent, "\n");
63  next_indent, type == BLOBMSG_TYPE_ARRAY);
64  break;
65  }
66 }
char data[]
Definition: blob.h:1
static size_t blobmsg_data_len(const struct blob_attr *attr)
Definition: blobmsg.h:95
static uint16_t blobmsg_get_u16(struct blob_attr *attr)
Definition: blobmsg.h:288
static char * blobmsg_get_string(struct blob_attr *attr)
Definition: blobmsg.h:348
static uint32_t blobmsg_get_u32(struct blob_attr *attr)
Definition: blobmsg.h:293
blobmsg_type
Definition: blobmsg.h:25
@ BLOBMSG_TYPE_TABLE
Definition: blobmsg.h:28
@ BLOBMSG_TYPE_STRING
Definition: blobmsg.h:29
@ BLOBMSG_TYPE_INT16
Definition: blobmsg.h:32
@ BLOBMSG_TYPE_ARRAY
Definition: blobmsg.h:27
@ BLOBMSG_TYPE_INT8
Definition: blobmsg.h:33
@ BLOBMSG_TYPE_INT32
Definition: blobmsg.h:31
@ BLOBMSG_TYPE_INT64
Definition: blobmsg.h:30
@ BLOBMSG_TYPE_DOUBLE
Definition: blobmsg.h:35
static uint64_t blobmsg_get_u64(struct blob_attr *attr)
Definition: blobmsg.h:298
static double blobmsg_get_double(struct blob_attr *attr)
Definition: blobmsg.h:338
static void * blobmsg_data(const struct blob_attr *attr)
Definition: blobmsg.h:78
static uint8_t blobmsg_get_u8(struct blob_attr *attr)
Definition: blobmsg.h:278
#define indent_printf(indent,...)
Definition: test-blobmsg.c:12
static void dump_table(struct blob_attr *head, size_t len, int indent, bool array)
Definition: test-blobmsg.c:21
uint8_t type
Definition: udebug-proto.h:0
Here is the call graph for this function:
Here is the caller graph for this function:

◆ dump_message()

static void dump_message ( struct blob_buf buf)
static

Definition at line 93 of file test-blobmsg.c.

94 {
95  struct blob_attr *tb[ARRAY_SIZE(pol)];
96 
97  if (blobmsg_parse(pol, ARRAY_SIZE(pol), tb, blob_data(buf->head), blob_len(buf->head)) != 0) {
98  fprintf(stderr, "Parse failed\n");
99  return;
100  }
101  if (tb[FOO_MESSAGE])
102  fprintf(stderr, "Message: %s\n", (char *) blobmsg_data(tb[FOO_MESSAGE]));
103 
104  if (tb[FOO_LIST]) {
105  fprintf(stderr, "List: ");
107  }
108  if (tb[FOO_TESTDATA]) {
109  fprintf(stderr, "Testdata: ");
111  }
112 }
static size_t blob_len(const struct blob_attr *attr)
Definition: blob.h:100
static void * blob_data(const struct blob_attr *attr)
Definition: blob.h:75
int blobmsg_parse(const struct blobmsg_policy *policy, int policy_len, struct blob_attr **tb, void *data, unsigned int len)
Definition: blobmsg.c:166
Definition: blob.h:52
struct blob_attr * head
Definition: blob.h:65
#define ARRAY_SIZE(x)
Definition: test-blobmsg.c:90
static const struct blobmsg_policy pol[]
Definition: test-blobmsg.c:74
Here is the call graph for this function:
Here is the caller graph for this function:

◆ dump_table()

static void dump_table ( struct blob_attr head,
size_t  len,
int  indent,
bool  array 
)
static

Definition at line 21 of file test-blobmsg.c.

22 {
23  struct blob_attr *attr;
24  struct blobmsg_hdr *hdr;
25 
26  indent_printf(indent, "{\n");
27  __blob_for_each_attr(attr, head, len) {
28  hdr = blob_data(attr);
29  if (!array)
30  indent_printf(indent + 1, "%s : ", hdr->name);
31  dump_attr_data(attr, 0, indent + 1);
32  }
33  indent_printf(indent, "}\n");
34 }
#define __blob_for_each_attr(pos, attr, rem)
Definition: blob.h:244
uint8_t name[]
Definition: blobmsg.h:43
static void dump_attr_data(struct blob_attr *data, int indent, int next_indent)
Definition: test-blobmsg.c:36
Here is the call graph for this function:
Here is the caller graph for this function:

◆ fill_message()

static void fill_message ( struct blob_buf buf)
static

Definition at line 115 of file test-blobmsg.c.

116 {
117  void *tbl;
118 
119  blobmsg_add_string(buf, "message", "Hello, world!");
120 
121  tbl = blobmsg_open_table(buf, "testdata");
122  blobmsg_add_double(buf, "dbl-min", DBL_MIN);
123  blobmsg_add_double(buf, "dbl-max", DBL_MAX);
124  blobmsg_add_u8(buf, "foo", 0);
125  blobmsg_add_u8(buf, "poo", 100);
126  blobmsg_add_u8(buf, "moo-min", INT8_MIN);
127  blobmsg_add_u8(buf, "moo-max", INT8_MAX);
128  blobmsg_add_u16(buf, "bar-min", INT16_MIN);
129  blobmsg_add_u16(buf, "bar-max", INT16_MAX);
130  blobmsg_add_u32(buf, "baz-min", INT32_MIN);
131  blobmsg_add_u32(buf, "baz-max", INT32_MAX);
132  blobmsg_add_u64(buf, "taz-min", INT64_MIN);
133  blobmsg_add_u64(buf, "taz-max", INT64_MAX);
134  blobmsg_add_string(buf, "world", "2");
135  blobmsg_close_table(buf, tbl);
136 
137  tbl = blobmsg_open_array(buf, "list");
138  blobmsg_add_u8(buf, NULL, 0);
139  blobmsg_add_u8(buf, NULL, 100);
140  blobmsg_add_u8(buf, NULL, INT8_MIN);
141  blobmsg_add_u8(buf, NULL, INT8_MAX);
142  blobmsg_add_u16(buf, NULL, INT16_MIN);
143  blobmsg_add_u16(buf, NULL, INT16_MAX);
144  blobmsg_add_u32(buf, NULL, INT32_MIN);
145  blobmsg_add_u32(buf, NULL, INT32_MAX);
146  blobmsg_add_u64(buf, NULL, INT64_MIN);
147  blobmsg_add_u64(buf, NULL, INT64_MAX);
148  blobmsg_add_double(buf, NULL, DBL_MIN);
149  blobmsg_add_double(buf, NULL, DBL_MAX);
150  blobmsg_close_table(buf, tbl);
151 }
static void * blobmsg_open_table(struct blob_buf *buf, const char *name)
Definition: blobmsg.h:256
static void blobmsg_close_table(struct blob_buf *buf, void *cookie)
Definition: blobmsg.h:268
static int blobmsg_add_u8(struct blob_buf *buf, const char *name, uint8_t val)
Definition: blobmsg.h:208
static int blobmsg_add_u64(struct blob_buf *buf, const char *name, uint64_t val)
Definition: blobmsg.h:228
static int blobmsg_add_string(struct blob_buf *buf, const char *name, const char *string)
Definition: blobmsg.h:235
static int blobmsg_add_u16(struct blob_buf *buf, const char *name, uint16_t val)
Definition: blobmsg.h:214
static void * blobmsg_open_array(struct blob_buf *buf, const char *name)
Definition: blobmsg.h:250
static int blobmsg_add_double(struct blob_buf *buf, const char *name, double val)
Definition: blobmsg.h:196
static int blobmsg_add_u32(struct blob_buf *buf, const char *name, uint32_t val)
Definition: blobmsg.h:221
Here is the call graph for this function:
Here is the caller graph for this function:

◆ main()

int main ( int  argc,
char **  argv 
)

Definition at line 153 of file test-blobmsg.c.

154 {
155  char *json = NULL;
156  static struct blob_buf buf;
157 
159  fill_message(&buf);
160  fprintf(stderr, "[*] blobmsg dump:\n");
161  dump_message(&buf);
162 
163  json = blobmsg_format_json(buf.head, true);
164  if (!json)
165  exit(EXIT_FAILURE);
166 
167  fprintf(stderr, "\n[*] blobmsg to json: %s\n", json);
168 
170  if (!blobmsg_add_json_from_string(&buf, json))
171  exit(EXIT_FAILURE);
172 
173  fprintf(stderr, "\n[*] blobmsg from json:\n");
174  dump_message(&buf);
175 
176  if (buf.buf)
177  free(buf.buf);
178  free(json);
179 
180  return 0;
181 }
static int blobmsg_buf_init(struct blob_buf *buf)
Definition: blobmsg.h:273
bool blobmsg_add_json_from_string(struct blob_buf *b, const char *str)
Definition: blobmsg_json.c:112
static char * blobmsg_format_json(struct blob_attr *attr, bool list)
Definition: blobmsg_json.h:35
Definition: blob.h:64
void * buf
Definition: blob.h:68
static void fill_message(struct blob_buf *buf)
Definition: test-blobmsg.c:115
static void dump_message(struct blob_buf *buf)
Definition: test-blobmsg.c:93
Here is the call graph for this function:

Variable Documentation

◆ indent_str

const char* indent_str = "\t\t\t\t\t\t\t\t\t\t\t\t\t"
static

Definition at line 10 of file test-blobmsg.c.

◆ pol

const struct blobmsg_policy pol[]
static
Initial value:
= {
.name = "message",
},
[FOO_LIST] = {
.name = "list",
},
.name = "testdata",
},
}

Definition at line 36 of file test-blobmsg.c.