src: minor cleanups and cosmetic fixes

This commit is contained in:
Arnaud Ferraris
2021-02-20 17:01:17 +01:00
parent 4962fcd13e
commit b929c6a380
5 changed files with 67 additions and 52 deletions

View File

@@ -126,9 +126,9 @@ void modem_reset(struct EG25Manager *manager)
if (manager->reset_timer) if (manager->reset_timer)
return; return;
if (manager->suspend_timer) { if (manager->modem_recovery_timer) {
g_source_remove(manager->suspend_timer); g_source_remove(manager->modem_recovery_timer);
manager->suspend_timer = 0; manager->modem_recovery_timer = 0;
} }
if (!manager->modem_usb_id) { if (!manager->modem_usb_id) {
@@ -205,8 +205,8 @@ int main(int argc, char *argv[])
memset(&manager, 0, sizeof(manager)); memset(&manager, 0, sizeof(manager));
manager.at_fd = -1; manager.at_fd = -1;
manager.delay_inhibit_fd = -1; manager.suspend_delay_fd = -1;
manager.block_inhibit_fd = -1; manager.suspend_block_fd = -1;
opt_context = g_option_context_new ("- Power management for the Quectel EG25 modem"); opt_context = g_option_context_new ("- Power management for the Quectel EG25 modem");
g_option_context_add_main_entries (opt_context, options, NULL); g_option_context_add_main_entries (opt_context, options, NULL);

View File

@@ -43,10 +43,11 @@ struct EG25Manager {
MMModem *mm_modem; MMModem *mm_modem;
GDBusProxy *suspend_proxy; GDBusProxy *suspend_proxy;
int delay_inhibit_fd; int suspend_delay_fd;
int block_inhibit_fd; int suspend_block_fd;
guint suspend_timer;
guint boot_timer; guint modem_recovery_timer;
guint modem_boot_timer;
GUdevClient *udev; GUdevClient *udev;

View File

@@ -35,9 +35,9 @@ static void add_modem(struct EG25Manager *manager, GDBusObject *object)
g_assert(manager->mm_modem != NULL); g_assert(manager->mm_modem != NULL);
if (manager->modem_state == EG25_STATE_RESUMING) { if (manager->modem_state == EG25_STATE_RESUMING) {
if (manager->suspend_timer) { if (manager->modem_recovery_timer) {
g_source_remove(manager->suspend_timer); g_source_remove(manager->modem_recovery_timer);
manager->suspend_timer = 0; manager->modem_recovery_timer = 0;
} }
modem_resume_post(manager); modem_resume_post(manager);
manager->modem_state = EG25_STATE_CONFIGURED; manager->modem_state = EG25_STATE_CONFIGURED;

View File

@@ -21,7 +21,7 @@
static gboolean check_modem_resume(struct EG25Manager *manager) static gboolean check_modem_resume(struct EG25Manager *manager)
{ {
g_message("Modem wasn't probed in time, restart it!"); g_message("Modem wasn't probed in time, restart it!");
manager->suspend_timer = 0; manager->modem_recovery_timer = 0;
modem_reset(manager); modem_reset(manager);
return FALSE; return FALSE;
@@ -30,18 +30,18 @@ static gboolean check_modem_resume(struct EG25Manager *manager)
static gboolean drop_inhibitor(struct EG25Manager *manager, gboolean block) static gboolean drop_inhibitor(struct EG25Manager *manager, gboolean block)
{ {
if (block) { if (block) {
if (manager->block_inhibit_fd >= 0) { if (manager->suspend_block_fd >= 0) {
g_message("dropping systemd sleep block inhibitor"); g_message("dropping systemd sleep block inhibitor");
close(manager->block_inhibit_fd); close(manager->suspend_block_fd);
manager->block_inhibit_fd = -1; manager->suspend_block_fd = -1;
return TRUE; return TRUE;
} }
} }
else { else {
if (manager->delay_inhibit_fd >= 0) { if (manager->suspend_delay_fd >= 0) {
g_message("dropping systemd sleep delay inhibitor"); g_message("dropping systemd sleep delay inhibitor");
close(manager->delay_inhibit_fd); close(manager->suspend_delay_fd);
manager->delay_inhibit_fd = -1; manager->suspend_delay_fd = -1;
return TRUE; return TRUE;
} }
} }
@@ -49,8 +49,8 @@ static gboolean drop_inhibitor(struct EG25Manager *manager, gboolean block)
} }
static void inhibit_done_delay(GObject *source, static void inhibit_done_delay(GObject *source,
GAsyncResult *result, GAsyncResult *result,
gpointer user_data) gpointer user_data)
{ {
GDBusProxy *suspend_proxy = G_DBUS_PROXY(source); GDBusProxy *suspend_proxy = G_DBUS_PROXY(source);
struct EG25Manager *manager = user_data; struct EG25Manager *manager = user_data;
@@ -58,24 +58,25 @@ static void inhibit_done_delay(GObject *source,
GVariant *res; GVariant *res;
GUnixFDList *fd_list; GUnixFDList *fd_list;
res = g_dbus_proxy_call_with_unix_fd_list_finish(suspend_proxy, &fd_list, result, &error); res = g_dbus_proxy_call_with_unix_fd_list_finish(suspend_proxy, &fd_list,
result, &error);
if (!res) { if (!res) {
g_warning("inhibit failed: %s", error->message); g_warning("inhibit failed: %s", error->message);
} else { } else {
if (!fd_list || g_unix_fd_list_get_length(fd_list) != 1) if (!fd_list || g_unix_fd_list_get_length(fd_list) != 1)
g_warning("didn't get a single fd back"); g_warning("didn't get a single fd back");
manager->delay_inhibit_fd = g_unix_fd_list_get(fd_list, 0, NULL); manager->suspend_delay_fd = g_unix_fd_list_get(fd_list, 0, NULL);
g_message("inhibitor sleep fd is %d", manager->delay_inhibit_fd); g_message("inhibitor sleep fd is %d", manager->suspend_delay_fd);
g_object_unref(fd_list); g_object_unref(fd_list);
g_variant_unref(res); g_variant_unref(res);
} }
} }
static void inhibit_done_block(GObject *source, static void inhibit_done_block(GObject *source,
GAsyncResult *result, GAsyncResult *result,
gpointer user_data) gpointer user_data)
{ {
GDBusProxy *suspend_proxy = G_DBUS_PROXY(source); GDBusProxy *suspend_proxy = G_DBUS_PROXY(source);
struct EG25Manager *manager = user_data; struct EG25Manager *manager = user_data;
@@ -83,16 +84,17 @@ static void inhibit_done_block(GObject *source,
GVariant *res; GVariant *res;
GUnixFDList *fd_list; GUnixFDList *fd_list;
res = g_dbus_proxy_call_with_unix_fd_list_finish(suspend_proxy, &fd_list, result, &error); res = g_dbus_proxy_call_with_unix_fd_list_finish(suspend_proxy, &fd_list,
result, &error);
if (!res) { if (!res) {
g_warning("inhibit failed: %s", error->message); g_warning("inhibit failed: %s", error->message);
} else { } else {
if (!fd_list || g_unix_fd_list_get_length(fd_list) != 1) if (!fd_list || g_unix_fd_list_get_length(fd_list) != 1)
g_warning("didn't get a single fd back"); g_warning("didn't get a single fd back");
manager->block_inhibit_fd = g_unix_fd_list_get(fd_list, 0, NULL); manager->suspend_block_fd = g_unix_fd_list_get(fd_list, 0, NULL);
g_message("inhibitor block fd is %d", manager->block_inhibit_fd); g_message("inhibitor block fd is %d", manager->suspend_block_fd);
g_object_unref(fd_list); g_object_unref(fd_list);
g_variant_unref(res); g_variant_unref(res);
} }
@@ -106,7 +108,7 @@ static void inhibit_done_block(GObject *source,
static gboolean modem_fully_booted(struct EG25Manager *manager) static gboolean modem_fully_booted(struct EG25Manager *manager)
{ {
g_message("Modem is up for %d seconds and fully ready", FULL_BOOT_DELAY); g_message("Modem is up for %d seconds and fully ready", FULL_BOOT_DELAY);
manager->boot_timer = 0; manager->modem_boot_timer = 0;
drop_inhibitor(manager, TRUE); drop_inhibitor(manager, TRUE);
return FALSE; return FALSE;
@@ -117,28 +119,33 @@ static void take_inhibitor(struct EG25Manager *manager, gboolean block)
GVariant *variant_arg; GVariant *variant_arg;
if (block) { if (block) {
if(manager->block_inhibit_fd != -1) if(manager->suspend_block_fd != -1)
drop_inhibitor(manager, TRUE); drop_inhibitor(manager, TRUE);
variant_arg = g_variant_new ("(ssss)", "sleep", "eg25manager", variant_arg = g_variant_new ("(ssss)", "sleep", "eg25manager",
"eg25manager needs to wait for modem to be fully booted", "block"); "eg25manager needs to wait for modem to be fully booted",
"block");
g_message("taking systemd sleep block inhibitor"); g_message("taking systemd sleep inhibitor (blocking)");
g_dbus_proxy_call_with_unix_fd_list(manager->suspend_proxy, "Inhibit", variant_arg, g_dbus_proxy_call_with_unix_fd_list(manager->suspend_proxy, "Inhibit",
0, G_MAXINT, NULL, NULL, inhibit_done_block, manager); variant_arg, 0, G_MAXINT, NULL, NULL,
g_message("scheduling block inhibitor release"); inhibit_done_block, manager);
manager->boot_timer = g_timeout_add_seconds(FULL_BOOT_DELAY, G_SOURCE_FUNC(modem_fully_booted), manager); manager->modem_boot_timer = g_timeout_add_seconds(FULL_BOOT_DELAY,
G_SOURCE_FUNC(modem_fully_booted),
manager);
} }
else { else {
if(manager->delay_inhibit_fd != -1) if(manager->suspend_delay_fd != -1)
drop_inhibitor(manager, FALSE); drop_inhibitor(manager, FALSE);
variant_arg = g_variant_new ("(ssss)", "sleep", "eg25manager", variant_arg = g_variant_new ("(ssss)", "sleep", "eg25manager",
"eg25manager needs to prepare modem for sleep", "delay"); "eg25manager needs to prepare modem for sleep",
"delay");
g_message("taking systemd sleep delay inhibitor"); g_message("taking systemd sleep inhibitor");
g_dbus_proxy_call_with_unix_fd_list(manager->suspend_proxy, "Inhibit", variant_arg, g_dbus_proxy_call_with_unix_fd_list(manager->suspend_proxy, "Inhibit",
0, G_MAXINT, NULL, NULL, inhibit_done_delay, manager); variant_arg, 0, G_MAXINT, NULL, NULL,
inhibit_done_delay, manager);
} }
} }
@@ -174,7 +181,9 @@ static void signal_cb(GDBusProxy *proxy,
modem_resume_post(manager); modem_resume_post(manager);
} else { } else {
manager->modem_state = EG25_STATE_RESUMING; manager->modem_state = EG25_STATE_RESUMING;
manager->suspend_timer = g_timeout_add_seconds(9, G_SOURCE_FUNC(check_modem_resume), manager); manager->modem_recovery_timer = g_timeout_add_seconds(9,
G_SOURCE_FUNC(check_modem_resume),
manager);
} }
} }
} }
@@ -211,8 +220,10 @@ static void on_proxy_acquired(GObject *object,
return; return;
} }
g_signal_connect(manager->suspend_proxy, "notify::g-name-owner", G_CALLBACK(name_owner_cb), manager); g_signal_connect(manager->suspend_proxy, "notify::g-name-owner",
g_signal_connect(manager->suspend_proxy, "g-signal", G_CALLBACK(signal_cb), manager); G_CALLBACK(name_owner_cb), manager);
g_signal_connect(manager->suspend_proxy, "g-signal",
G_CALLBACK(signal_cb), manager);
owner = g_dbus_proxy_get_name_owner(manager->suspend_proxy); owner = g_dbus_proxy_get_name_owner(manager->suspend_proxy);
if (owner) { if (owner) {
@@ -234,13 +245,13 @@ void suspend_destroy(struct EG25Manager *manager)
{ {
drop_inhibitor(manager, FALSE); drop_inhibitor(manager, FALSE);
drop_inhibitor(manager, TRUE); drop_inhibitor(manager, TRUE);
if (manager->suspend_timer) { if (manager->modem_recovery_timer) {
g_source_remove(manager->suspend_timer); g_source_remove(manager->modem_recovery_timer);
manager->suspend_timer = 0; manager->modem_recovery_timer = 0;
} }
if (manager->boot_timer) { if (manager->modem_boot_timer) {
g_source_remove(manager->boot_timer); g_source_remove(manager->modem_boot_timer);
manager->boot_timer = 0; manager->modem_boot_timer = 0;
} }
if (manager->suspend_proxy) { if (manager->suspend_proxy) {
g_object_unref(manager->suspend_proxy); g_object_unref(manager->suspend_proxy);

View File

@@ -12,8 +12,11 @@ static void udev_event_cb(GUdevClient *client, gchar *action, GUdevDevice *devic
{ {
struct EG25Manager *manager = data; struct EG25Manager *manager = data;
if (strcmp(action, "unbind") != 0 || manager->modem_state == EG25_STATE_RESETTING || !manager->modem_usb_id) if (strcmp(action, "unbind") != 0 ||
manager->modem_state == EG25_STATE_RESETTING ||
!manager->modem_usb_id) {
return; return;
}
if (strncmp(g_udev_device_get_name(device), manager->modem_usb_id, strlen(manager->modem_usb_id)) == 0 && if (strncmp(g_udev_device_get_name(device), manager->modem_usb_id, strlen(manager->modem_usb_id)) == 0 &&
manager->reset_timer == 0) { manager->reset_timer == 0) {