From: Felix Fietkau Date: Wed, 29 Nov 2023 18:10:16 +0000 (+0100) Subject: lib: add helper function for applying ring config X-Git-Url: http://git.openwrt.org/?a=commitdiff_plain;h=e02306af7c5063655757e67cb2feaf8ad05b4ed4;p=project%2Fudebug.git lib: add helper function for applying ring config Signed-off-by: Felix Fietkau --- diff --git a/lib.c b/lib.c index f3e62f9..5c5cd7e 100644 --- a/lib.c +++ b/lib.c @@ -72,6 +72,84 @@ static void udebug_ubus_get_config(struct uloop_timeout *t) ubus_invoke(ctx->ubus, id, "get_config", NULL, udebug_ubus_req_cb, ctx, 1000); } +void udebug_ubus_ring_init(struct udebug *ud, struct udebug_ubus_ring *ring) +{ + if (!ring->size) + ring->size = ring->default_size; + if (!ring->entries) + ring->entries = ring->default_entries; + udebug_buf_init(ring->buf, ring->entries, ring->size); + udebug_buf_add(ud, ring->buf, ring->meta); +} + +void udebug_ubus_apply_config(struct udebug *ud, struct udebug_ubus_ring *rings, int n, + struct blob_attr *data, bool enabled) +{ + enum { + CFG_ATTR_ENABLE, + CFG_ATTR_SIZE, + CFG_ATTR_ENTRIES, + __CFG_ATTR_MAX, + }; + static struct blobmsg_policy policy[] = { + [CFG_ATTR_ENABLE] = { NULL, BLOBMSG_TYPE_STRING }, + [CFG_ATTR_SIZE] = { NULL, BLOBMSG_TYPE_STRING }, + [CFG_ATTR_ENTRIES] = { NULL, BLOBMSG_TYPE_STRING }, + }; + + for (size_t i = 0; i < n; i++) { + struct blob_attr *tb[__CFG_ATTR_MAX], *cur; + struct udebug_buf *buf = rings[i].buf; + const char *name = rings[i].meta->name; + int name_len = strlen(name); + unsigned int size, entries; + bool cur_enabled = enabled; + char *str; + + policy[CFG_ATTR_ENABLE].name = name; + +#define SIZE_FMT "%s_size" + str = alloca(sizeof(SIZE_FMT) + name_len); + sprintf(str, SIZE_FMT, name); + policy[CFG_ATTR_SIZE].name = str; + +#define ENTRIES_FMT "%s_entries" + str = alloca(sizeof(ENTRIES_FMT) + name_len); + sprintf(str, ENTRIES_FMT, name); + policy[CFG_ATTR_ENTRIES].name = str; + + blobmsg_parse_attr(policy, __CFG_ATTR_MAX, tb, data); + + if ((cur = tb[CFG_ATTR_ENABLE]) != NULL) + cur_enabled = !!atoi(blobmsg_get_string(cur)); + + if ((cur = tb[CFG_ATTR_SIZE]) != NULL) + size = atoi(blobmsg_get_string(cur)); + else + size = rings[i].default_size; + + if ((cur = tb[CFG_ATTR_ENTRIES]) != NULL) + entries = atoi(blobmsg_get_string(cur)); + else + entries = rings[i].default_entries; + + if (udebug_buf_valid(buf) == cur_enabled && + size == rings[i].size && + entries == rings[i].entries) + continue; + + if (udebug_buf_valid(buf)) + udebug_buf_free(buf); + + rings[i].size = size; + rings[i].entries = entries; + if (!cur_enabled) + continue; + + udebug_ubus_ring_init(ud, &rings[i]); + } +} + void udebug_ubus_init(struct udebug_ubus *ctx, struct ubus_context *ubus, const char *service, udebug_config_cb cb) { diff --git a/udebug.h b/udebug.h index 0d7ea30..ae48837 100644 --- a/udebug.h +++ b/udebug.h @@ -4,6 +4,13 @@ struct udebug_ubus; typedef void (*udebug_config_cb)(struct udebug_ubus *ctx, struct blob_attr *data, bool enabled); +struct udebug_ubus_ring { + struct udebug_buf *buf; + const struct udebug_buf_meta *meta; + unsigned int size, default_size; + unsigned int entries, default_entries; +}; + struct udebug_ubus { struct ubus_context *ubus; struct uloop_timeout t; @@ -14,4 +21,7 @@ struct udebug_ubus { void udebug_ubus_init(struct udebug_ubus *ctx, struct ubus_context *ubus, const char *service, udebug_config_cb cb); +void udebug_ubus_ring_init(struct udebug *ud, struct udebug_ubus_ring *ring); +void udebug_ubus_apply_config(struct udebug *ud, struct udebug_ubus_ring *rings, int n, + struct blob_attr *data, bool enabled); void udebug_ubus_free(struct udebug_ubus *ctx);