-
Notifications
You must be signed in to change notification settings - Fork 5
/
libubus-obj.c
242 lines (189 loc) · 5.51 KB
/
libubus-obj.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
/*
* Copyright (C) 2011-2012 Felix Fietkau <[email protected]>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License version 2.1
* as published by the Free Software Foundation
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*/
#include "libubus.h"
#include "libubus-internal.h"
static void
ubus_process_unsubscribe(struct ubus_context *ctx, struct ubus_msghdr *hdr,
struct ubus_object *obj, struct blob_attr **attrbuf)
{
struct ubus_subscriber *s;
if (!obj || !attrbuf[UBUS_ATTR_TARGET])
return;
if (obj->methods != &watch_method)
return;
s = container_of(obj, struct ubus_subscriber, obj);
if (s->remove_cb)
s->remove_cb(ctx, s, blob_get_u32(attrbuf[UBUS_ATTR_TARGET]));
}
static void
ubus_process_notify(struct ubus_context *ctx, struct ubus_msghdr *hdr,
struct ubus_object *obj, struct blob_attr **attrbuf)
{
if (!obj || !attrbuf[UBUS_ATTR_ACTIVE])
return;
obj->has_subscribers = blob_get_u8(attrbuf[UBUS_ATTR_ACTIVE]);
if (obj->subscribe_cb)
obj->subscribe_cb(ctx, obj);
}
static void
ubus_process_invoke(struct ubus_context *ctx, struct ubus_msghdr *hdr,
struct ubus_object *obj, struct blob_attr **attrbuf)
{
struct ubus_request_data req = {
.fd = -1,
};
int method;
int ret;
bool no_reply = false;
if (!obj) {
ret = UBUS_STATUS_NOT_FOUND;
goto send;
}
if (!attrbuf[UBUS_ATTR_METHOD]) {
ret = UBUS_STATUS_INVALID_ARGUMENT;
goto send;
}
if (attrbuf[UBUS_ATTR_NO_REPLY])
no_reply = blob_get_int8(attrbuf[UBUS_ATTR_NO_REPLY]);
req.peer = hdr->peer;
req.seq = hdr->seq;
req.object = obj->id;
for (method = 0; method < obj->n_methods; method++)
if (!obj->methods[method].name ||
!strcmp(obj->methods[method].name,
blob_data(attrbuf[UBUS_ATTR_METHOD])))
goto found;
/* not found */
ret = UBUS_STATUS_METHOD_NOT_FOUND;
goto send;
found:
ret = obj->methods[method].handler(ctx, obj, &req,
blob_data(attrbuf[UBUS_ATTR_METHOD]),
attrbuf[UBUS_ATTR_DATA]);
if (req.deferred || no_reply)
return;
send:
ubus_complete_deferred_request(ctx, &req, ret);
}
void __hidden ubus_process_obj_msg(struct ubus_context *ctx, struct ubus_msghdr *hdr)
{
void (*cb)(struct ubus_context *, struct ubus_msghdr *,
struct ubus_object *, struct blob_attr **);
struct blob_attr **attrbuf;
struct ubus_object *obj;
uint32_t objid;
attrbuf = ubus_parse_msg(ubus_msghdr_data(hdr));
if (!attrbuf[UBUS_ATTR_OBJID])
return;
objid = blob_get_u32(attrbuf[UBUS_ATTR_OBJID]);
obj = avl_find_element(&ctx->objects, &objid, obj, avl);
switch (hdr->type) {
case UBUS_MSG_INVOKE:
cb = ubus_process_invoke;
break;
case UBUS_MSG_UNSUBSCRIBE:
cb = ubus_process_unsubscribe;
break;
case UBUS_MSG_NOTIFY:
cb = ubus_process_notify;
break;
default:
return;
}
cb(ctx, hdr, obj, attrbuf);
}
static void ubus_add_object_cb(struct ubus_request *req, int type, struct blob_attr *msg)
{
struct ubus_object *obj = req->priv;
struct blob_attr **attrbuf = ubus_parse_msg(msg);
if (!attrbuf[UBUS_ATTR_OBJID])
return;
obj->id = blob_get_u32(attrbuf[UBUS_ATTR_OBJID]);
if (attrbuf[UBUS_ATTR_OBJTYPE])
obj->type->id = blob_get_u32(attrbuf[UBUS_ATTR_OBJTYPE]);
obj->avl.key = &obj->id;
avl_insert(&req->ctx->objects, &obj->avl);
}
static void ubus_push_method_data(const struct ubus_method *m)
{
void *mtbl;
int i;
mtbl = blobmsg_open_table(&b, m->name);
for (i = 0; i < m->n_policy; i++) {
if (m->mask && !(m->mask & (1 << i)))
continue;
blobmsg_add_u32(&b, m->policy[i].name, m->policy[i].type);
}
blobmsg_close_table(&b, mtbl);
}
static bool ubus_push_object_type(const struct ubus_object_type *type)
{
void *s;
int i;
s = blob_nest_start(&b, UBUS_ATTR_SIGNATURE);
for (i = 0; i < type->n_methods; i++)
ubus_push_method_data(&type->methods[i]);
blob_nest_end(&b, s);
return true;
}
int ubus_add_object(struct ubus_context *ctx, struct ubus_object *obj)
{
struct ubus_request req;
int ret;
blob_buf_init(&b, 0);
if (obj->name && obj->type) {
blob_put_string(&b, UBUS_ATTR_OBJPATH, obj->name);
if (obj->type->id)
blob_put_int32(&b, UBUS_ATTR_OBJTYPE, obj->type->id);
else if (!ubus_push_object_type(obj->type))
return UBUS_STATUS_INVALID_ARGUMENT;
}
if (ubus_start_request(ctx, &req, b.head, UBUS_MSG_ADD_OBJECT, 0) < 0)
return UBUS_STATUS_INVALID_ARGUMENT;
req.raw_data_cb = ubus_add_object_cb;
req.priv = obj;
ret = ubus_complete_request(ctx, &req, 0);
if (ret)
return ret;
if (!obj->id)
return UBUS_STATUS_NO_DATA;
return 0;
}
static void ubus_remove_object_cb(struct ubus_request *req, int type, struct blob_attr *msg)
{
struct ubus_object *obj = req->priv;
struct blob_attr **attrbuf = ubus_parse_msg(msg);
if (!attrbuf[UBUS_ATTR_OBJID])
return;
obj->id = 0;
if (attrbuf[UBUS_ATTR_OBJTYPE] && obj->type)
obj->type->id = 0;
avl_delete(&req->ctx->objects, &obj->avl);
}
int ubus_remove_object(struct ubus_context *ctx, struct ubus_object *obj)
{
struct ubus_request req;
int ret;
blob_buf_init(&b, 0);
blob_put_int32(&b, UBUS_ATTR_OBJID, obj->id);
if (ubus_start_request(ctx, &req, b.head, UBUS_MSG_REMOVE_OBJECT, 0) < 0)
return UBUS_STATUS_INVALID_ARGUMENT;
req.raw_data_cb = ubus_remove_object_cb;
req.priv = obj;
ret = ubus_complete_request(ctx, &req, 0);
if (ret)
return ret;
if (obj->id)
return UBUS_STATUS_NO_DATA;
return 0;
}