From 25dd46bb3041bcbcd499194617f56f16971e1157 Mon Sep 17 00:00:00 2001 From: Arnaud Ferraris Date: Wed, 6 Oct 2021 22:25:39 +0200 Subject: [PATCH] manager: make modem_reset more reliable `modem_reset()` could previously either fail silently, or fall back to using AT commands without indicating what happened. This commit adds informative messages and makes sure we fall back to resetting using AT commands whenever we encounter an error. --- src/manager.c | 35 ++++++++++++++++++++++++++++------- 1 file changed, 28 insertions(+), 7 deletions(-) diff --git a/src/manager.c b/src/manager.c index f725eb7..d0c527d 100644 --- a/src/manager.c +++ b/src/manager.c @@ -149,16 +149,20 @@ void modem_reset(struct EG25Manager *manager) { int fd, ret, len; - if (manager->reset_timer) + if (manager->reset_timer) { + g_message("modem_reset: timer already setup, skipping..."); return; + } /* * If we are managing the modem through lets say ofono, we should not * reset the modem based on the availability of USB ID * TODO: Improve ofono plugin and add support for fetching USB ID */ - if (manager->modem_iface != MODEM_IFACE_MODEMMANAGER) + if (manager->modem_iface != MODEM_IFACE_MODEMMANAGER) { + g_message("modem_reset: not using ModemManager, bail out!"); return; + } if (manager->modem_recovery_timer) { g_source_remove(manager->modem_recovery_timer); @@ -166,30 +170,42 @@ void modem_reset(struct EG25Manager *manager) } if (!manager->modem_usb_id) { - g_warning("Unknown modem USB ID"); + g_warning("Empty modem USB ID"); goto error; } + g_message("Trying to reset modem with USB ID '%s'", manager->modem_usb_id); + len = strlen(manager->modem_usb_id); manager->modem_state = EG25_STATE_RESETTING; fd = open("/sys/bus/usb/drivers/usb/unbind", O_WRONLY); - if (fd < 0) + if (fd < 0) { + g_warning("Unable to open /sys/bus/usb/drivers/usb/unbind"); goto error; + } ret = write(fd, manager->modem_usb_id, len); - if (ret < len) + if (ret < len) { g_warning("Couldn't unbind modem: wrote %d/%d bytes", ret, len); + goto error; + } close(fd); fd = open("/sys/bus/usb/drivers/usb/bind", O_WRONLY); - if (fd < 0) + if (fd < 0) { + g_warning("Unable to open /sys/bus/usb/drivers/usb/unbind"); goto error; + } ret = write(fd, manager->modem_usb_id, len); - if (ret < len) + if (ret < len) { g_warning("Couldn't bind modem: wrote %d/%d bytes", ret, len); + goto error; + } close(fd); + g_message("Successfully reset modem's USB connection"); + /* * 3s is long enough to make sure the modem has been bound back and * short enough to ensure it hasn't been acquired by ModemManager @@ -206,8 +222,13 @@ error: g_source_remove(manager->modem_boot_timer); manager->modem_boot_timer = 0; } + // Everything else failed, reboot the modem + g_message("USB reset failed, falling back to AT command"); at_sequence_reset(manager); + + // Setup timer for making sure we don't queue other reset commands + manager->reset_timer = g_timeout_add_seconds(30, G_SOURCE_FUNC(modem_reset_done), manager); } void modem_suspend_pre(struct EG25Manager *manager)