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))
40 struct blobmsg_policy {
42 enum blobmsg_type type;
45 static inline int blobmsg_hdrlen(int namelen)
47 return BLOBMSG_PADDING(sizeof(struct blobmsg_hdr) + namelen + 1);
50 static inline void *blobmsg_data(struct blob_attr *attr)
52 struct blobmsg_hdr *hdr = blob_data(attr);
53 return &hdr->name[blobmsg_hdrlen(hdr->namelen) - 1];
56 static inline int blobmsg_data_len(struct blob_attr *attr)
60 start = blob_data(attr);
61 end = blobmsg_data(attr);
63 return blob_len(attr) - (end - start);
66 int blobmsg_parse(const struct blobmsg_policy *policy, int policy_len,
67 struct blob_attr **tb, void *data, int len);
69 int blobmsg_add_field(struct blob_buf *buf, int type, const char *name,
70 const void *data, int len);
73 blobmsg_add_u8(struct blob_buf *buf, const char *name, uint8_t val)
75 return blobmsg_add_field(buf, BLOBMSG_TYPE_INT8, name, &val, 1);
79 blobmsg_add_u16(struct blob_buf *buf, const char *name, uint16_t val)
81 return blobmsg_add_field(buf, BLOBMSG_TYPE_INT16, name, &val, 2);
85 blobmsg_add_u32(struct blob_buf *buf, const char *name, uint32_t val)
87 return blobmsg_add_field(buf, BLOBMSG_TYPE_INT32, name, &val, 4);
91 blobmsg_add_u64(struct blob_buf *buf, const char *name, uint64_t val)
93 return blobmsg_add_field(buf, BLOBMSG_TYPE_INT64, name, &val, 8);
97 blobmsg_add_string(struct blob_buf *buf, const char *name, const char *string)
99 return blobmsg_add_field(buf, BLOBMSG_TYPE_STRING, name, string, strlen(string) + 1);
102 void *blobmsg_open_nested(struct blob_buf *buf, const char *name, bool array);
105 blobmsg_open_array(struct blob_buf *buf, const char *name)
107 return blobmsg_open_nested(buf, name, true);
111 blobmsg_open_table(struct blob_buf *buf, const char *name)
113 return blobmsg_open_nested(buf, name, false);
117 blobmsg_close_array(struct blob_buf *buf, void *cookie)
119 blob_nest_end(buf, cookie);
123 blobmsg_close_table(struct blob_buf *buf, void *cookie)
125 blob_nest_end(buf, cookie);
128 static inline int blobmsg_buf_init(struct blob_buf *buf)
130 return blob_buf_init(buf, BLOBMSG_TYPE_TABLE);