b50cca96d6a7e9aa2902412e5e367ec0f0a5f3bb
[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_data(struct blob_attr *attr)
53 {
54         struct blobmsg_hdr *hdr = blob_data(attr);
55         return &hdr->name[blobmsg_hdrlen(hdr->namelen) - 1];
56 }
57
58 static inline int blobmsg_data_len(struct blob_attr *attr)
59 {
60         uint8_t *start, *end;
61
62         start = blob_data(attr);
63         end = blobmsg_data(attr);
64
65         return blob_len(attr) - (end - start);
66 }
67
68 int blobmsg_parse(const struct blobmsg_policy *policy, int policy_len,
69                   struct blob_attr **tb, void *data, int len);
70
71 int blobmsg_add_field(struct blob_buf *buf, int type, const char *name,
72                       const void *data, int len);
73
74 static inline int
75 blobmsg_add_u8(struct blob_buf *buf, const char *name, uint8_t val)
76 {
77         return blobmsg_add_field(buf, BLOBMSG_TYPE_INT8, name, &val, 1);
78 }
79
80 static inline int
81 blobmsg_add_u16(struct blob_buf *buf, const char *name, uint16_t val)
82 {
83         return blobmsg_add_field(buf, BLOBMSG_TYPE_INT16, name, &val, 2);
84 }
85
86 static inline int
87 blobmsg_add_u32(struct blob_buf *buf, const char *name, uint32_t val)
88 {
89         return blobmsg_add_field(buf, BLOBMSG_TYPE_INT32, name, &val, 4);
90 }
91
92 static inline int
93 blobmsg_add_u64(struct blob_buf *buf, const char *name, uint64_t val)
94 {
95         return blobmsg_add_field(buf, BLOBMSG_TYPE_INT64, name, &val, 8);
96 }
97
98 static inline int
99 blobmsg_add_string(struct blob_buf *buf, const char *name, const char *string)
100 {
101         return blobmsg_add_field(buf, BLOBMSG_TYPE_STRING, name, string, strlen(string) + 1);
102 }
103
104 void *blobmsg_open_nested(struct blob_buf *buf, const char *name, bool array);
105
106 static inline void *
107 blobmsg_open_array(struct blob_buf *buf, const char *name)
108 {
109         return blobmsg_open_nested(buf, name, true);
110 }
111
112 static inline void *
113 blobmsg_open_table(struct blob_buf *buf, const char *name)
114 {
115         return blobmsg_open_nested(buf, name, false);
116 }
117
118 static inline void
119 blobmsg_close_array(struct blob_buf *buf, void *cookie)
120 {
121         blob_nest_end(buf, cookie);
122 }
123
124 static inline void
125 blobmsg_close_table(struct blob_buf *buf, void *cookie)
126 {
127         blob_nest_end(buf, cookie);
128 }
129
130 static inline int blobmsg_buf_init(struct blob_buf *buf)
131 {
132         return blob_buf_init(buf, BLOBMSG_TYPE_TABLE);
133 }
134
135 #endif