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