Skip to content

Commit

Permalink
ubus: add configuration for information sources
Browse files Browse the repository at this point in the history
  • Loading branch information
blocktrron committed Mar 30, 2024
1 parent 1202723 commit 5f50b1d
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 3 deletions.
1 change: 1 addition & 0 deletions src/information.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ struct nw_information_source {
char *name;
uint8_t type;
uint32_t fixed_size;
uint8_t enabled;
int (*collect)(uint8_t *buffer, size_t buffer_size);
int (*parse)(const uint8_t *ie_buf, size_t ie_len);
};
10 changes: 8 additions & 2 deletions src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,14 @@ int create_vendor_element_buf() {
/* Length now matches content. Make sure to update on each new information! */

/* Loop through all information-sources */
for (int i = 0; information_sources[i].name; i++) {
log_debug("Collecting information id=%d name=%s", information_sources[i].type, information_sources[i].name);
for (int i = 0; information_sources[i].name; i++) {
if (!information_sources[i].enabled) {
log_debug("Information source id=%d name=%s is disabled", information_sources[i].type, information_sources[i].name);
continue;
} else {
log_debug("Information source id=%d name=%s is enabled", information_sources[i].type, information_sources[i].name);
}

/* Check if we have space for T + L + {data} */
if (gbi.output.len + 3 > gbi.output.size) {
log_error("Buffer too small for id=%d name=%s", information_sources[i].type, information_sources[i].name);
Expand Down
71 changes: 70 additions & 1 deletion src/ubus.c
Original file line number Diff line number Diff line change
@@ -1,7 +1,76 @@
#include "node-whisperer.h"

extern struct nw_information_source information_sources[];

static struct blobmsg_policy source_toggle_arg[] = {
{ .name = "information_source", .type = BLOBMSG_TYPE_STRING, },
};

static struct nw_information_source *nw_information_source_get(const char *name)
{
struct nw_information_source *source;

for (source = information_sources; source->name; source++) {
if (!strcmp(source->name, name))
return source;
}

return NULL;
}

static int nw_ubus_information_source_set(const char *name, bool enabled)
{
struct nw_information_source *source;

source = nw_information_source_get(name);
if (!source)
return UBUS_STATUS_NOT_FOUND;

source->enabled = enabled;
return 0;
}

static int nw_ubus_enable_source(struct ubus_context *ctx, struct ubus_object *obj,
struct ubus_request_data *req, const char *method,
struct blob_attr *msg)
{
struct blob_attr *source_name_blob;
struct nw_information_source *source;
char *source_name;

blobmsg_parse(source_toggle_arg, 1, &source_name_blob, blob_data(msg), blob_len(msg));
if (!source_name_blob)
return UBUS_STATUS_INVALID_ARGUMENT;

source_name = blobmsg_get_string(source_name_blob);
if (!source_name)
return UBUS_STATUS_INVALID_ARGUMENT;

return nw_ubus_information_source_set(source_name, true);
}

static int nw_ubus_disable_source(struct ubus_context *ctx, struct ubus_object *obj,
struct ubus_request_data *req, const char *method,
struct blob_attr *msg)
{
struct blob_attr *source_name_blob;
struct nw_information_source *source;
char *source_name;

blobmsg_parse(source_toggle_arg, 1, &source_name_blob, blob_data(msg), blob_len(msg));
if (!source_name_blob)
return UBUS_STATUS_INVALID_ARGUMENT;

source_name = blobmsg_get_string(source_name_blob);
if (!source_name)
return UBUS_STATUS_INVALID_ARGUMENT;

return nw_ubus_information_source_set(source_name, false);
}

static const struct ubus_method nw_methods[] = {
/* Empty */
UBUS_METHOD("enable_source", nw_ubus_enable_source, source_toggle_arg),
UBUS_METHOD("disable_source", nw_ubus_disable_source, source_toggle_arg),
};

static struct ubus_object_type nw_obj_type =
Expand Down

0 comments on commit 5f50b1d

Please sign in to comment.