remove blob_buf::priv
[project/libubox.git] / blobmsg.h
1 /*
2  * blobmsg - library for generating/parsing structured blob messages
3  *
4  * Copyright (C) 2010 Felix Fietkau <nbd@openwrt.org>
5  *
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
9  *
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.
14  */
15
16 #ifndef __BLOBMSG_H
17 #define __BLOBMSG_H
18
19 #include "blob.h"
20
21 #define BLOBMSG_ALIGN   2
22 #define BLOBMSG_PADDING(len) (((len) + (1 << BLOBMSG_ALIGN) - 1) & ~((1 << BLOBMSG_ALIGN) - 1))
23
24 enum blobmsg_type {
25         BLOBMSG_TYPE_UNSPEC,
26         BLOBMSG_TYPE_ARRAY,
27         BLOBMSG_TYPE_TABLE,
28         BLOBMSG_TYPE_STRING,
29         BLOBMSG_TYPE_INT64,
30         BLOBMSG_TYPE_INT32,
31         BLOBMSG_TYPE_INT16,
32         BLOBMSG_TYPE_INT8,
33         __BLOBMSG_TYPE_LAST,
34         BLOBMSG_TYPE_LAST = __BLOBMSG_TYPE_LAST - 1
35 };
36
37 struct blobmsg_hdr {
38         uint8_t namelen;
39         uint8_t name[];
40 } __packed;
41
42 struct blobmsg_policy {
43         const char *name;
44         enum blobmsg_type type;
45 };
46
47 static inline int blobmsg_hdrlen(int namelen)
48 {
49         return BLOBMSG_PADDING(sizeof(struct blobmsg_hdr) + namelen + 1);
50 }
51
52 static inline void *blobmsg_name(struct blob_attr *attr)
53 {
54         struct blobmsg_hdr *hdr = blob_data(attr);
55         return &hdr->name;
56 }
57
58 static inline void *blobmsg_data(struct blob_attr *attr)
59 {
60         struct blobmsg_hdr *hdr = blob_data(attr);
61         return &hdr->name[blobmsg_hdrlen(hdr->namelen) - 1];
62 }
63
64 static inline int blobmsg_data_len(struct blob_attr *attr)
65 {
66         uint8_t *start, *end;
67
68         start = blob_data(attr);
69         end = blobmsg_data(attr);
70
71         return blob_len(attr) - (end - start);
72 }
73
74 bool blobmsg_check_attr(const struct blob_attr *attr, bool name);
75 int blobmsg_parse(const struct blobmsg_policy *policy, int policy_len,
76                   struct blob_attr **tb, void *data, int len);
77
78 int blobmsg_add_field(struct blob_buf *buf, int type, const char *name,
79                       const void *data, int len);
80
81 static inline int
82 blobmsg_add_u8(struct blob_buf *buf, const char *name, uint8_t val)
83 {
84         return blobmsg_add_field(buf, BLOBMSG_TYPE_INT8, name, &val, 1);
85 }
86
87 static inline int
88 blobmsg_add_u16(struct blob_buf *buf, const char *name, uint16_t val)
89 {
90         return blobmsg_add_field(buf, BLOBMSG_TYPE_INT16, name, &val, 2);
91 }
92
93 static inline int
94 blobmsg_add_u32(struct blob_buf *buf, const char *name, uint32_t val)
95 {
96         return blobmsg_add_field(buf, BLOBMSG_TYPE_INT32, name, &val, 4);
97 }
98
99 static inline int
100 blobmsg_add_u64(struct blob_buf *buf, const char *name, uint64_t val)
101 {
102         return blobmsg_add_field(buf, BLOBMSG_TYPE_INT64, name, &val, 8);
103 }
104
105 static inline int
106 blobmsg_add_string(struct blob_buf *buf, const char *name, const char *string)
107 {
108         return blobmsg_add_field(buf, BLOBMSG_TYPE_STRING, name, string, strlen(string) + 1);
109 }
110
111 void *blobmsg_open_nested(struct blob_buf *buf, const char *name, bool array);
112
113 static inline void *
114 blobmsg_open_array(struct blob_buf *buf, const char *name)
115 {
116         return blobmsg_open_nested(buf, name, true);
117 }
118
119 static inline void *
120 blobmsg_open_table(struct blob_buf *buf, const char *name)
121 {
122         return blobmsg_open_nested(buf, name, false);
123 }
124
125 static inline void
126 blobmsg_close_array(struct blob_buf *buf, void *cookie)
127 {
128         blob_nest_end(buf, cookie);
129 }
130
131 static inline void
132 blobmsg_close_table(struct blob_buf *buf, void *cookie)
133 {
134         blob_nest_end(buf, cookie);
135 }
136
137 static inline int blobmsg_buf_init(struct blob_buf *buf)
138 {
139         return blob_buf_init(buf, BLOBMSG_TYPE_TABLE);
140 }
141
142 char *blobmsg_format_json(struct blob_attr *attr);
143
144 #endif