From: Felix Fietkau Date: Sun, 19 Nov 2023 12:55:27 +0000 (+0100) Subject: ubus: add config support X-Git-Url: http://git.lede-project.org./?a=commitdiff_plain;h=cb6239b3fe865cd84d7af8e3cc5161abbb7107c1;p=project%2Fudebug.git ubus: add config support issue notifications for config changes Signed-off-by: Felix Fietkau --- diff --git a/ubus.c b/ubus.c index a58ccb3..ca82d0b 100644 --- a/ubus.c +++ b/ubus.c @@ -5,6 +5,7 @@ struct ubus_auto_conn conn; struct blob_buf b; static struct ubus_object udebug_object; +static struct blob_attr *service_config; enum { LIST_ATTR_PROCNAME, @@ -121,8 +122,56 @@ udebug_list(struct ubus_context *ctx, struct ubus_object *obj, return 0; } +enum { + CFG_ATTR_SERVICE, + __CFG_ATTR_MAX +}; +static const struct blobmsg_policy config_policy[__CFG_ATTR_MAX] = { + [CFG_ATTR_SERVICE] = { "service", BLOBMSG_TYPE_TABLE }, +}; + +static struct blob_attr * +udebug_fill_config(void) +{ + blob_buf_init(&b, 0); + if (service_config) + blobmsg_add_blob(&b, service_config); + else + blobmsg_close_table(&b, blobmsg_open_table(&b, "service")); + + return b.head; +} + +static int +udebug_set_config(struct ubus_context *ctx, struct ubus_object *obj, + struct ubus_request_data *req, const char *method, + struct blob_attr *msg) +{ + struct blob_attr *tb[__CFG_ATTR_MAX], *cur; + + blobmsg_parse_attr(config_policy, __CFG_ATTR_MAX, tb, msg); + if ((cur = tb[CFG_ATTR_SERVICE]) != NULL) { + free(service_config); + service_config = blob_memdup(cur); + ubus_notify(ctx, obj, "config", udebug_fill_config(), -1); + } + + return 0; +} + +static int +udebug_get_config(struct ubus_context *ctx, struct ubus_object *obj, + struct ubus_request_data *req, const char *method, + struct blob_attr *msg) +{ + ubus_send_reply(ctx, req, udebug_fill_config()); + return 0; +} + static const struct ubus_method udebug_methods[] = { UBUS_METHOD("list", udebug_list, list_policy), + UBUS_METHOD("set_config", udebug_set_config, config_policy), + UBUS_METHOD_NOARG("get_config", udebug_get_config), }; static struct ubus_object_type udebug_object_type =