#include <sys/capability.h>
#endif
+#define STARTUP_GRACE_SECONDS 5
+#define SHUTDOWN_GRACE_SECONDS 10
+
static GMainLoop *event_loop;
static struct mgmt *mgmt_if = NULL;
return FALSE;
}
+static void shutdown_complete(uint8_t status, uint16_t length,
+ const void *param, void *user_data)
+{
+ if (status != MGMT_STATUS_SUCCESS)
+ error("Clean controller shutdown failed");
+
+ g_main_loop_quit(event_loop);
+}
+
+static void shutdown_controller(void)
+{
+ struct mgmt_mode cp;
+
+ info("Switching controller off");
+
+ memset(&cp, 0, sizeof(cp));
+ cp.val = 0x00;
+
+ if (mgmt_send(mgmt_if, MGMT_OP_SET_POWERED, adapter_index,
+ sizeof(cp), &cp, shutdown_complete, NULL, NULL) > 0)
+ return;
+
+ g_main_loop_quit(event_loop);
+}
+
+static gboolean quit_eventloop(gpointer user_data)
+{
+ g_main_loop_quit(event_loop);
+ return FALSE;
+}
+
static gboolean signal_handler(GIOChannel *channel, GIOCondition cond,
gpointer user_data)
{
case SIGTERM:
if (!__terminated) {
info("Terminating");
- g_main_loop_quit(event_loop);
+ if (adapter_index != MGMT_INDEX_NONE) {
+ g_timeout_add_seconds(SHUTDOWN_GRACE_SECONDS,
+ quit_eventloop, NULL);
+ shutdown_controller();
+ } else
+ g_main_loop_quit(event_loop);
}
__terminated = true;
return;
if (num < 1) {
- adapter_timeout = g_timeout_add_seconds(5,
- adapter_timeout_handler, NULL);
+ adapter_timeout = g_timeout_add_seconds(STARTUP_GRACE_SECONDS,
+ adapter_timeout_handler, NULL);
if (adapter_timeout == 0) {
error("%s: Failed init timeout", __func__);
g_main_loop_quit(event_loop);