3085d008bb949f0737dc9a2534e02d7b90503ff8
2 * blobmsg - library for generating/parsing structured blob messages
4 * Copyright (C) 2010 Felix Fietkau <nbd@openwrt.org>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU Lesser General Public License version 2.1
8 * as published by the Free Software Foundation
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
21 #define BLOBMSG_ALIGN 2
22 #define BLOBMSG_PADDING(len) (((len) + (1 << BLOBMSG_ALIGN) - 1) & ~((1 << BLOBMSG_ALIGN) - 1))
34 BLOBMSG_TYPE_LAST
= __BLOBMSG_TYPE_LAST
- 1
42 struct blobmsg_policy
{
44 enum blobmsg_type type
;
47 static inline int blobmsg_hdrlen(int namelen
)
49 return BLOBMSG_PADDING(sizeof(struct blobmsg_hdr
) + namelen
+ 1);
52 static inline void *blobmsg_data(struct blob_attr
*attr
)
54 struct blobmsg_hdr
*hdr
= blob_data(attr
);
55 return &hdr
->name
[blobmsg_hdrlen(hdr
->namelen
) - 1];
58 static inline int blobmsg_data_len(struct blob_attr
*attr
)
62 start
= blob_data(attr
);
63 end
= blobmsg_data(attr
);
65 return blob_len(attr
) - (end
- start
);
68 bool blobmsg_check_attr(const struct blob_attr
*attr
, bool name
);
69 int blobmsg_parse(const struct blobmsg_policy
*policy
, int policy_len
,
70 struct blob_attr
**tb
, void *data
, int len
);
72 int blobmsg_add_field(struct blob_buf
*buf
, int type
, const char *name
,
73 const void *data
, int len
);
76 blobmsg_add_u8(struct blob_buf
*buf
, const char *name
, uint8_t val
)
78 return blobmsg_add_field(buf
, BLOBMSG_TYPE_INT8
, name
, &val
, 1);
82 blobmsg_add_u16(struct blob_buf
*buf
, const char *name
, uint16_t val
)
84 return blobmsg_add_field(buf
, BLOBMSG_TYPE_INT16
, name
, &val
, 2);
88 blobmsg_add_u32(struct blob_buf
*buf
, const char *name
, uint32_t val
)
90 return blobmsg_add_field(buf
, BLOBMSG_TYPE_INT32
, name
, &val
, 4);
94 blobmsg_add_u64(struct blob_buf
*buf
, const char *name
, uint64_t val
)
96 return blobmsg_add_field(buf
, BLOBMSG_TYPE_INT64
, name
, &val
, 8);
100 blobmsg_add_string(struct blob_buf
*buf
, const char *name
, const char *string
)
102 return blobmsg_add_field(buf
, BLOBMSG_TYPE_STRING
, name
, string
, strlen(string
) + 1);
105 void *blobmsg_open_nested(struct blob_buf
*buf
, const char *name
, bool array
);
108 blobmsg_open_array(struct blob_buf
*buf
, const char *name
)
110 return blobmsg_open_nested(buf
, name
, true);
114 blobmsg_open_table(struct blob_buf
*buf
, const char *name
)
116 return blobmsg_open_nested(buf
, name
, false);
120 blobmsg_close_array(struct blob_buf
*buf
, void *cookie
)
122 blob_nest_end(buf
, cookie
);
126 blobmsg_close_table(struct blob_buf
*buf
, void *cookie
)
128 blob_nest_end(buf
, cookie
);
131 static inline int blobmsg_buf_init(struct blob_buf
*buf
)
133 return blob_buf_init(buf
, BLOBMSG_TYPE_TABLE
);