From 616e88f87affa8c07909baf460d011defddab8de Mon Sep 17 00:00:00 2001 From: John Crispin Date: Fri, 8 Sep 2023 12:23:45 +0200 Subject: [PATCH] move blink handler to ucentral-state Signed-off-by: John Crispin --- CMakeLists.txt | 2 +- blink.c | 42 ------------------------------------------ proto.c | 3 ++- ubus.c | 16 ++++++++++++++++ ucentral.h | 3 +-- 5 files changed, 20 insertions(+), 46 deletions(-) delete mode 100644 blink.c diff --git a/CMakeLists.txt b/CMakeLists.txt index 7eb075e..1d5066c 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -11,7 +11,7 @@ ADD_DEFINITIONS(-Os -std=gnu99 -g3 -Wmissing-declarations -Wno-unused-parameter) SET(CMAKE_SHARED_LIBRARY_LINK_C_FLAGS "") -SET(SOURCES main.c config.c proto.c ubus.c task.c cmd.c blink.c apply.c upload.c rebootlog.c event.c collide.c) +SET(SOURCES main.c config.c proto.c ubus.c task.c cmd.c apply.c upload.c rebootlog.c event.c collide.c) FIND_LIBRARY(ubus NAMES ubus) FIND_LIBRARY(blobmsg_json NAMES blobmsg_json) diff --git a/blink.c b/blink.c deleted file mode 100644 index ab7bcec..0000000 --- a/blink.c +++ /dev/null @@ -1,42 +0,0 @@ -/* SPDX-License-Identifier: BSD-3-Clause */ - -#include "ucentral.h" - -static uint32_t blink_running; - -static void -blink_run_cb(time_t uuid, uint32_t id) -{ - char duration[32]; - - ULOG_INFO("running blink task\n"); - - snprintf(duration, sizeof(duration), "%" PRIu64, uuid); - execlp("/usr/libexec/ucentral/ucentral_led_blink.sh", "/usr/libexec/ucentral/ucentral_led_blink.sh", duration, NULL); - exit(1); -} - -static void -blink_complete_cb(struct task *t, time_t uuid, uint32_t id, int ret) -{ - blink_running = 0; - result_send_error(ret ? 1 : 0, "led blinking completed", ret, id); -} - -struct task blink_task = { - .run = blink_run_cb, - .complete = blink_complete_cb, -}; - -void -blink_run(uint32_t duration, uint32_t id) -{ - if (blink_running) { - result_send_error(1, "command already running", 1, id); - return; - } - - blink_task.run_time = duration + 15; - blink_running = 1; - task_run(&blink_task, duration, id, 1); -} diff --git a/proto.c b/proto.c index 08d9cc7..357c2f7 100644 --- a/proto.c +++ b/proto.c @@ -767,7 +767,8 @@ leds_handle(struct blob_attr **rpc) duration = blobmsg_get_u32(tb[LED_DURATION]); if (!strcmp(blobmsg_get_string(tb[LED_PATTERN]), "blink")) { - blink_run(duration, id); + result_send_error(0, "success", 0, id); + ubus_blink_leds(duration); return; } action_handle(rpc, "leds", 1, 1, 1, 0); diff --git a/ubus.c b/ubus.c index 97680eb..255d9df 100644 --- a/ubus.c +++ b/ubus.c @@ -344,6 +344,22 @@ void ubus_set_client_status(char *status) ubus_abort_request(&conn.ctx, &async); } +void ubus_blink_leds(int duration) +{ + struct ubus_request async = { }; + + if (!state) { + ULOG_ERR("state is not running\n"); + return; + } + blob_buf_init(&u, 0); + blobmsg_add_string(&u, "state", "blink"); + blobmsg_add_u32(&u, "duration", duration); + + ubus_invoke_async(&conn.ctx, state, "set", u.head, &async); + ubus_abort_request(&conn.ctx, &async); +} + static const struct ubus_method ucentral_methods[] = { UBUS_METHOD("health", ubus_health_cb, health_policy), UBUS_METHOD("result", ubus_result_cb, result_policy), diff --git a/ucentral.h b/ucentral.h index bd15915..6d8d64a 100644 --- a/ucentral.h +++ b/ucentral.h @@ -96,8 +96,7 @@ void ubus_init(void); void ubus_deinit(void); void ubus_forward_radius(struct blob_buf *msg); void ubus_set_client_status(char *status); - -void blink_run(uint32_t duration, uint32_t id); +void ubus_blink_leds(int duration); void health_run(uint32_t id, uint32_t immediate); void health_update_interval(uint32_t periodic);