Format fixes - ubus.c

This commit is contained in:
Simon Kelley
2018-07-29 22:15:36 +01:00
parent 9d6fd1727e
commit 6f835ed6c8

View File

@@ -24,81 +24,82 @@ static struct ubus_context *ubus;
static struct blob_buf b; static struct blob_buf b;
static int ubus_handle_metrics(struct ubus_context *ctx, struct ubus_object *obj, static int ubus_handle_metrics(struct ubus_context *ctx, struct ubus_object *obj,
struct ubus_request_data *req, const char *method, struct ubus_request_data *req, const char *method,
struct blob_attr *msg); struct blob_attr *msg);
static struct ubus_method ubus_object_methods[] = { static struct ubus_method ubus_object_methods[] = {
{.name = "metrics", .handler = ubus_handle_metrics}, {.name = "metrics", .handler = ubus_handle_metrics},
}; };
static struct ubus_object_type ubus_object_type = UBUS_OBJECT_TYPE("dnsmasq", ubus_object_methods); static struct ubus_object_type ubus_object_type = UBUS_OBJECT_TYPE("dnsmasq", ubus_object_methods);
static struct ubus_object ubus_object = { static struct ubus_object ubus_object = {
.name = "dnsmasq", .name = "dnsmasq",
.type = &ubus_object_type, .type = &ubus_object_type,
.methods = ubus_object_methods, .methods = ubus_object_methods,
.n_methods = ARRAY_SIZE(ubus_object_methods), .n_methods = ARRAY_SIZE(ubus_object_methods),
}; };
void set_ubus_listeners() void set_ubus_listeners()
{ {
if (!ubus) if (!ubus)
return; return;
poll_listen(ubus->sock.fd, POLLIN); poll_listen(ubus->sock.fd, POLLIN);
poll_listen(ubus->sock.fd, POLLERR); poll_listen(ubus->sock.fd, POLLERR);
poll_listen(ubus->sock.fd, POLLHUP); poll_listen(ubus->sock.fd, POLLHUP);
} }
void check_ubus_listeners() void check_ubus_listeners()
{ {
if (!ubus) { if (!ubus)
ubus = ubus_connect(NULL); {
if (!ubus) ubus = ubus_connect(NULL);
return; if (!ubus)
ubus_add_object(ubus, &ubus_object); return;
} ubus_add_object(ubus, &ubus_object);
}
if (poll_check(ubus->sock.fd, POLLIN)) if (poll_check(ubus->sock.fd, POLLIN))
ubus_handle_event(ubus); ubus_handle_event(ubus);
if (poll_check(ubus->sock.fd, POLLHUP)) { if (poll_check(ubus->sock.fd, POLLHUP))
ubus_free(ubus); {
ubus = NULL; ubus_free(ubus);
} ubus = NULL;
}
} }
static int ubus_handle_metrics(struct ubus_context *ctx, struct ubus_object *obj, static int ubus_handle_metrics(struct ubus_context *ctx, struct ubus_object *obj,
struct ubus_request_data *req, const char *method, struct ubus_request_data *req, const char *method,
struct blob_attr *msg) struct blob_attr *msg)
{ {
blob_buf_init(&b, 0); blob_buf_init(&b, 0);
for(int i=0; i < __METRIC_MAX; i++) { for(int i=0; i < __METRIC_MAX; i++)
blobmsg_add_u32(&b, get_metric_name(i), daemon->metrics[i]); blobmsg_add_u32(&b, get_metric_name(i), daemon->metrics[i]);
}
ubus_send_reply(ctx, req, b.head); ubus_send_reply(ctx, req, b.head);
return 0; return 0;
} }
void ubus_event_bcast(const char *type, const char *mac, const char *ip, const char *name, const char *interface) void ubus_event_bcast(const char *type, const char *mac, const char *ip, const char *name, const char *interface)
{ {
if (!ubus || !ubus_object.has_subscribers) if (!ubus || !ubus_object.has_subscribers)
return; return;
blob_buf_init(&b, 0); blob_buf_init(&b, 0);
if (mac) if (mac)
blobmsg_add_string(&b, "mac", mac); blobmsg_add_string(&b, "mac", mac);
if (ip) if (ip)
blobmsg_add_string(&b, "ip", ip); blobmsg_add_string(&b, "ip", ip);
if (name) if (name)
blobmsg_add_string(&b, "name", name); blobmsg_add_string(&b, "name", name);
if (interface) if (interface)
blobmsg_add_string(&b, "interface", interface); blobmsg_add_string(&b, "interface", interface);
ubus_notify(ubus, &ubus_object, type, b.head, -1); ubus_notify(ubus, &ubus_object, type, b.head, -1);
} }