Skip to content

Commit

Permalink
daemon: set beacon elements using ubus
Browse files Browse the repository at this point in the history
  • Loading branch information
blocktrron committed Feb 5, 2024
1 parent 6fe5495 commit f1264c1
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 7 deletions.
8 changes: 8 additions & 0 deletions src/daemon.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#pragma once

#include "gluon-diagnostic.h"

struct gluon_diagnostic {
struct ubus_context *ubus_ctx;
struct uloop_timeout update_timeout;
};
14 changes: 14 additions & 0 deletions src/interface.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include "gluon-diagnostic.h"

static LIST_HEAD(gluon_diagnostic_interfaces);
static struct blob_buf b;

static int gluon_diagnostic_interface_handle_event(struct ubus_context *ctx,
struct ubus_object *obj,
Expand All @@ -22,6 +23,19 @@ static void gluon_diagnostic_interface_handle_remove(struct ubus_context *ctx,
gluon_diagnostic_interface_remove(ctx, iface);
}

int gluon_diagnostic_interface_update(struct ubus_context *ctx, char *vendor_elements)
{
struct gluon_diagnostic_interface *iface;

blob_buf_init(&b, 0);
blobmsg_add_string(&b, "vendor_elements", vendor_elements);

list_for_each_entry(iface, &gluon_diagnostic_interfaces, list) {
log_debug("Sending vendor elements to id=%d name=%s", iface->ubus.id, iface->ubus.name);
ubus_invoke(ctx, iface->ubus.id, "set_vendor_elements", b.head, NULL, NULL, 1000);
}
}

int gluon_diagnostic_interface_remove(struct ubus_context *ctx,
struct gluon_diagnostic_interface *iface)
{
Expand Down
2 changes: 2 additions & 0 deletions src/interface.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ struct gluon_diagnostic_interface {
} ubus;
};

int gluon_diagnostic_interface_update(struct ubus_context *ctx, char *vendor_elements);

int gluon_diagnostic_interface_remove(struct ubus_context *ctx,
struct gluon_diagnostic_interface *iface);

Expand Down
23 changes: 16 additions & 7 deletions src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
#include <stddef.h>

#include "gluon-diagnostic.h"
#include "daemon.h"

#define UPDATE_INTERVAL (30 * 1000)

#define HOSTNAME_PATH "/home/dbauer/test"
#define BEACON_BUFFER_SIZE 512
Expand Down Expand Up @@ -100,35 +103,41 @@ int buffer_to_hexstring(uint8_t *buf, size_t len, uint8_t *hexstring) {
}

static void collect_information(struct uloop_timeout *timeout) {
struct gluon_diagnostic *instance = container_of(timeout, struct gluon_diagnostic, update_timeout);
create_vendor_element_buf();

/* Allocate output buffer */
size_t buf_len = gbi.output.len * 2 + 1;
uint8_t *buf_hex = malloc(buf_len);

buffer_to_hexstring(gbi.output.buf, gbi.output.len, buf_hex);

/* Update nodes */
log_debug("Update %s\n", buf_hex);
gluon_diagnostic_interface_update(instance->ubus_ctx, (char *)buf_hex);

free(buf_hex);
uloop_timeout_set(timeout, UPDATE_INTERVAL);
}

static int start_daemon() {
struct uloop_timeout information_update_timeout;
struct gluon_diagnostic instance;

uloop_init();

ubus_ctx = ubus_connect(NULL);
if (!ubus_ctx) {
instance.ubus_ctx = ubus_connect(NULL);
if (!instance.ubus_ctx) {
fprintf(stderr, "Failed to connect to ubus\n");
return -1;
}

/* Init ubus */
ubus_add_uloop(ubus_ctx);
gluon_diagnostic_ubus_init(ubus_ctx);
ubus_add_uloop(instance.ubus_ctx);
gluon_diagnostic_ubus_init(instance.ubus_ctx);

/* Add Information gathering timer */
information_update_timeout.cb = collect_information;
uloop_timeout_set(&information_update_timeout, 1000);
instance.update_timeout.cb = collect_information;
uloop_timeout_set(&instance.update_timeout, 5 * 1000);

uloop_run();

Expand Down

0 comments on commit f1264c1

Please sign in to comment.