lib: move library code to libubox, add ubus config handling code
[project/udebug.git] / lib.c
1 #include "udebug.h"
2
3 static struct blob_attr *
4 find_attr(struct blob_attr *attr, const char *name, enum blobmsg_type type)
5 {
6 struct blobmsg_policy policy = { name, type };
7 struct blob_attr *ret;
8
9 if (!attr)
10 return NULL;
11
12 blobmsg_parse_attr(&policy, 1, &ret, attr);
13 return ret;
14 }
15
16 static void
17 udebug_ubus_msg_cb(struct udebug_ubus *ctx, struct blob_attr *data)
18 {
19 struct blob_attr *en_attr;
20 bool enabled;
21
22 data = find_attr(data, "service", BLOBMSG_TYPE_TABLE);
23 data = find_attr(data, ctx->service, BLOBMSG_TYPE_TABLE);
24 if (!data)
25 return;
26
27 en_attr = find_attr(data, "enabled", BLOBMSG_TYPE_STRING);
28 enabled = en_attr && !!atoi(blobmsg_get_string(en_attr));
29 ctx->cb(ctx, data, enabled);
30 }
31
32 static int
33 udebug_ubus_notify_cb(struct ubus_context *ubus, struct ubus_object *obj,
34 struct ubus_request_data *req, const char *method,
35 struct blob_attr *msg)
36 {
37 struct udebug_ubus *ctx = container_of(obj, struct udebug_ubus, sub.obj);
38
39 if (!strcmp(method, "config"))
40 udebug_ubus_msg_cb(ctx, msg);
41
42 return 0;
43 }
44
45 static void
46 udebug_ubus_req_cb(struct ubus_request *req, int type, struct blob_attr *msg)
47 {
48 udebug_ubus_msg_cb(req->priv, msg);
49 }
50
51 static bool
52 udebug_ubus_new_obj_cb(struct ubus_context *ubus, struct ubus_subscriber *sub,
53 const char *path)
54 {
55 return !strcmp(path, "udebug");
56 }
57
58 void udebug_ubus_init(struct udebug_ubus *ctx, struct ubus_context *ubus,
59 const char *service, udebug_config_cb cb)
60 {
61 uint32_t id;
62
63 ctx->ubus = ubus;
64 ctx->service = service;
65 ctx->cb = cb;
66 ctx->sub.new_obj_cb = udebug_ubus_new_obj_cb;
67 ctx->sub.cb = udebug_ubus_notify_cb;
68 ubus_register_subscriber(ubus, &ctx->sub);
69
70 if (ubus_lookup_id(ubus, "udebug", &id))
71 return;
72
73 ubus_subscribe(ubus, &ctx->sub, id);
74 ubus_invoke(ubus, id, "get_config", NULL, udebug_ubus_req_cb, ctx, 1000);
75 }
76
77 void udebug_ubus_free(struct udebug_ubus *ctx)
78 {
79 if (!ctx->ubus)
80 return;
81
82 ubus_unregister_subscriber(ctx->ubus, &ctx->sub);
83 }