2 * Copyright (C) 2011-2014 Felix Fietkau <nbd@openwrt.org>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU Lesser General Public License version 2.1
6 * as published by the Free Software Foundation
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
14 #include <sys/types.h>
15 #include <sys/socket.h>
18 #include <libubox/blob.h>
19 #include <libubox/blobmsg.h>
22 #include "libubus-internal.h"
25 const char *__ubus_strerror
[__UBUS_STATUS_LAST
] = {
26 [UBUS_STATUS_OK
] = "Success",
27 [UBUS_STATUS_INVALID_COMMAND
] = "Invalid command",
28 [UBUS_STATUS_INVALID_ARGUMENT
] = "Invalid argument",
29 [UBUS_STATUS_METHOD_NOT_FOUND
] = "Method not found",
30 [UBUS_STATUS_NOT_FOUND
] = "Not found",
31 [UBUS_STATUS_NO_DATA
] = "No response",
32 [UBUS_STATUS_PERMISSION_DENIED
] = "Permission denied",
33 [UBUS_STATUS_TIMEOUT
] = "Request timed out",
34 [UBUS_STATUS_NOT_SUPPORTED
] = "Operation not supported",
35 [UBUS_STATUS_UNKNOWN_ERROR
] = "Unknown error",
36 [UBUS_STATUS_CONNECTION_FAILED
] = "Connection failed",
39 struct blob_buf b __hidden
= {};
41 struct ubus_pending_msg
{
42 struct list_head list
;
43 struct ubus_msghdr_buf hdr
;
46 static int ubus_cmp_id(const void *k1
, const void *k2
, void *ptr
)
48 const uint32_t *id1
= k1
, *id2
= k2
;
56 const char *ubus_strerror(int error
)
60 if (error
< 0 || error
>= __UBUS_STATUS_LAST
)
63 if (!__ubus_strerror
[error
])
66 return __ubus_strerror
[error
];
69 sprintf(err
, "Unknown error: %d", error
);
74 ubus_queue_msg(struct ubus_context
*ctx
, struct ubus_msghdr_buf
*buf
)
76 struct ubus_pending_msg
*pending
;
79 pending
= calloc_a(sizeof(*pending
), &data
, blob_raw_len(buf
->data
));
81 pending
->hdr
.data
= data
;
82 memcpy(&pending
->hdr
.hdr
, &buf
->hdr
, sizeof(buf
->hdr
));
83 memcpy(data
, buf
->data
, blob_raw_len(buf
->data
));
84 list_add_tail(&pending
->list
, &ctx
->pending
);
85 if (ctx
->sock
.registered
)
86 uloop_timeout_set(&ctx
->pending_timer
, 1);
90 ubus_process_msg(struct ubus_context
*ctx
, struct ubus_msghdr_buf
*buf
, int fd
)
92 switch(buf
->hdr
.type
) {
95 ubus_process_req_msg(ctx
, buf
, fd
);
99 case UBUS_MSG_UNSUBSCRIBE
:
100 case UBUS_MSG_NOTIFY
:
101 if (ctx
->stack_depth
) {
102 ubus_queue_msg(ctx
, buf
);
107 ubus_process_obj_msg(ctx
, buf
, fd
);
110 case UBUS_MSG_MONITOR
:
112 ctx
->monitor_cb(ctx
, buf
->hdr
.seq
, buf
->data
);
117 static void ubus_process_pending_msg(struct uloop_timeout
*timeout
)
119 struct ubus_context
*ctx
= container_of(timeout
, struct ubus_context
, pending_timer
);
120 struct ubus_pending_msg
*pending
;
122 while (!list_empty(&ctx
->pending
)) {
123 if (ctx
->stack_depth
)
126 pending
= list_first_entry(&ctx
->pending
, struct ubus_pending_msg
, list
);
127 list_del(&pending
->list
);
128 ubus_process_msg(ctx
, &pending
->hdr
, -1);
133 struct ubus_lookup_request
{
134 struct ubus_request req
;
135 ubus_lookup_handler_t cb
;
138 static void ubus_lookup_cb(struct ubus_request
*ureq
, int type
, struct blob_attr
*msg
)
140 struct ubus_lookup_request
*req
;
141 struct ubus_object_data obj
= {};
142 struct blob_attr
**attr
;
144 req
= container_of(ureq
, struct ubus_lookup_request
, req
);
145 attr
= ubus_parse_msg(msg
, blob_raw_len(msg
));
147 if (!attr
[UBUS_ATTR_OBJID
] || !attr
[UBUS_ATTR_OBJPATH
] ||
148 !attr
[UBUS_ATTR_OBJTYPE
])
151 obj
.id
= blob_get_u32(attr
[UBUS_ATTR_OBJID
]);
152 obj
.path
= blob_data(attr
[UBUS_ATTR_OBJPATH
]);
153 obj
.type_id
= blob_get_u32(attr
[UBUS_ATTR_OBJTYPE
]);
154 obj
.signature
= attr
[UBUS_ATTR_SIGNATURE
];
155 req
->cb(ureq
->ctx
, &obj
, ureq
->priv
);
158 int ubus_lookup(struct ubus_context
*ctx
, const char *path
,
159 ubus_lookup_handler_t cb
, void *priv
)
161 struct ubus_lookup_request lookup
;
163 blob_buf_init(&b
, 0);
165 blob_put_string(&b
, UBUS_ATTR_OBJPATH
, path
);
167 if (ubus_start_request(ctx
, &lookup
.req
, b
.head
, UBUS_MSG_LOOKUP
, 0) < 0)
168 return UBUS_STATUS_INVALID_ARGUMENT
;
170 lookup
.req
.raw_data_cb
= ubus_lookup_cb
;
171 lookup
.req
.priv
= priv
;
173 return ubus_complete_request(ctx
, &lookup
.req
, 0);
176 static void ubus_lookup_id_cb(struct ubus_request
*req
, int type
, struct blob_attr
*msg
)
178 struct blob_attr
**attr
;
179 uint32_t *id
= req
->priv
;
181 attr
= ubus_parse_msg(msg
, blob_raw_len(msg
));
183 if (!attr
[UBUS_ATTR_OBJID
])
186 *id
= blob_get_u32(attr
[UBUS_ATTR_OBJID
]);
189 int ubus_lookup_id(struct ubus_context
*ctx
, const char *path
, uint32_t *id
)
191 struct ubus_request req
;
193 blob_buf_init(&b
, 0);
195 blob_put_string(&b
, UBUS_ATTR_OBJPATH
, path
);
197 if (ubus_start_request(ctx
, &req
, b
.head
, UBUS_MSG_LOOKUP
, 0) < 0)
198 return UBUS_STATUS_INVALID_ARGUMENT
;
200 req
.raw_data_cb
= ubus_lookup_id_cb
;
203 return ubus_complete_request(ctx
, &req
, 0);
206 static int ubus_event_cb(struct ubus_context
*ctx
, struct ubus_object
*obj
,
207 struct ubus_request_data
*req
,
208 const char *method
, struct blob_attr
*msg
)
210 struct ubus_event_handler
*ev
;
212 ev
= container_of(obj
, struct ubus_event_handler
, obj
);
213 ev
->cb(ctx
, ev
, method
, msg
);
217 static const struct ubus_method event_method
= {
219 .handler
= ubus_event_cb
,
222 int ubus_register_event_handler(struct ubus_context
*ctx
,
223 struct ubus_event_handler
*ev
,
226 struct ubus_object
*obj
= &ev
->obj
;
227 struct blob_buf b2
= {};
231 obj
->methods
= &event_method
;
234 if (!!obj
->name
^ !!obj
->type
)
235 return UBUS_STATUS_INVALID_ARGUMENT
;
237 ret
= ubus_add_object(ctx
, obj
);
242 /* use a second buffer, ubus_invoke() overwrites the primary one */
243 blob_buf_init(&b2
, 0);
244 blobmsg_add_u32(&b2
, "object", obj
->id
);
246 blobmsg_add_string(&b2
, "pattern", pattern
);
248 ret
= ubus_invoke(ctx
, UBUS_SYSTEM_OBJECT_EVENT
, "register", b2
.head
,
255 int ubus_send_event(struct ubus_context
*ctx
, const char *id
,
256 struct blob_attr
*data
)
258 struct ubus_request req
;
261 blob_buf_init(&b
, 0);
262 blob_put_int32(&b
, UBUS_ATTR_OBJID
, UBUS_SYSTEM_OBJECT_EVENT
);
263 blob_put_string(&b
, UBUS_ATTR_METHOD
, "send");
264 s
= blob_nest_start(&b
, UBUS_ATTR_DATA
);
265 blobmsg_add_string(&b
, "id", id
);
266 blobmsg_add_field(&b
, BLOBMSG_TYPE_TABLE
, "data", blob_data(data
), blob_len(data
));
267 blob_nest_end(&b
, s
);
269 if (ubus_start_request(ctx
, &req
, b
.head
, UBUS_MSG_INVOKE
, UBUS_SYSTEM_OBJECT_EVENT
) < 0)
270 return UBUS_STATUS_INVALID_ARGUMENT
;
272 return ubus_complete_request(ctx
, &req
, 0);
275 static void ubus_default_connection_lost(struct ubus_context
*ctx
)
277 if (ctx
->sock
.registered
)
281 int ubus_connect_ctx(struct ubus_context
*ctx
, const char *path
)
284 memset(ctx
, 0, sizeof(*ctx
));
287 ctx
->sock
.cb
= ubus_handle_data
;
288 ctx
->connection_lost
= ubus_default_connection_lost
;
289 ctx
->pending_timer
.cb
= ubus_process_pending_msg
;
291 ctx
->msgbuf
.data
= calloc(1, UBUS_MSG_CHUNK_SIZE
);
292 if (!ctx
->msgbuf
.data
)
294 ctx
->msgbuf_data_len
= UBUS_MSG_CHUNK_SIZE
;
296 INIT_LIST_HEAD(&ctx
->requests
);
297 INIT_LIST_HEAD(&ctx
->pending
);
298 avl_init(&ctx
->objects
, ubus_cmp_id
, false, NULL
);
299 if (ubus_reconnect(ctx
, path
)) {
300 free(ctx
->msgbuf
.data
);
301 ctx
->msgbuf
.data
= NULL
;
308 static void ubus_auto_reconnect_cb(struct uloop_timeout
*timeout
)
310 struct ubus_auto_conn
*conn
= container_of(timeout
, struct ubus_auto_conn
, timer
);
312 if (!ubus_reconnect(&conn
->ctx
, conn
->path
))
313 ubus_add_uloop(&conn
->ctx
);
315 uloop_timeout_set(timeout
, 1000);
318 static void ubus_auto_disconnect_cb(struct ubus_context
*ctx
)
320 struct ubus_auto_conn
*conn
= container_of(ctx
, struct ubus_auto_conn
, ctx
);
322 conn
->timer
.cb
= ubus_auto_reconnect_cb
;
323 uloop_timeout_set(&conn
->timer
, 1000);
326 static void ubus_auto_connect_cb(struct uloop_timeout
*timeout
)
328 struct ubus_auto_conn
*conn
= container_of(timeout
, struct ubus_auto_conn
, timer
);
330 if (ubus_connect_ctx(&conn
->ctx
, conn
->path
)) {
331 uloop_timeout_set(timeout
, 1000);
332 fprintf(stderr
, "failed to connect to ubus\n");
335 conn
->ctx
.connection_lost
= ubus_auto_disconnect_cb
;
337 conn
->cb(&conn
->ctx
);
338 ubus_add_uloop(&conn
->ctx
);
341 void ubus_auto_connect(struct ubus_auto_conn
*conn
)
343 conn
->timer
.cb
= ubus_auto_connect_cb
;
344 ubus_auto_connect_cb(&conn
->timer
);
347 struct ubus_context
*ubus_connect(const char *path
)
349 struct ubus_context
*ctx
;
351 ctx
= calloc(1, sizeof(*ctx
));
355 if (ubus_connect_ctx(ctx
, path
)) {
363 void ubus_shutdown(struct ubus_context
*ctx
)
369 uloop_timeout_cancel(&ctx
->pending_timer
);
370 free(ctx
->msgbuf
.data
);
373 void ubus_free(struct ubus_context
*ctx
)