From f34f84a3eecf6fb6c5a2a46ecc59c660505dadc6 Mon Sep 17 00:00:00 2001 From: nirenjan Date: Mon, 30 Aug 2021 12:33:52 -0700 Subject: [PATCH] Add routine to check device connectivity Prior to this change, if the clock thread is disabled, then disconnecting and reconnecting the X52 device would cause the daemon to not detect the transition. As a result, the daemon would stay in a state where it thinks the device is still connected, and therefore, not actually apply any of the saved configuration, until it received a SIGHUP to refresh the configuration. This change adds a routine that sends a dummy vendor command. This vendor command does nothing on my X52 Pro (VID 06a3, PID 0762), but serves as a check to see if the daemon can send vendor commands to the device. If the device is indeed disconnected, then that is a sufficient indicator to disable the update thread and re-enable the acquisition thread. --- daemon/x52d_device.c | 33 +++++++++++++++++++++++++++++++++ daemon/x52d_device.h | 1 + po/x52pro-linux.pot | 14 +++++++------- po/xx_PL.po | 14 +++++++------- 4 files changed, 48 insertions(+), 14 deletions(-) diff --git a/daemon/x52d_device.c b/daemon/x52d_device.c index d06e85e..00711a8 100644 --- a/daemon/x52d_device.c +++ b/daemon/x52d_device.c @@ -89,6 +89,8 @@ static void *x52_dev_upd(void *param) #define UPDATE_CHECK_DELAY 50000 // Wait for this many useconds if (!device_update_needed || !device_upd_thr_enable) { usleep(UPDATE_CHECK_DELAY); + /* Check if the device is still connected */ + x52d_dev_check(); continue; } @@ -230,3 +232,34 @@ int x52d_dev_update(void) return rc; } + +void x52d_dev_check(void) +{ + int rc; + + if (!libx52_is_connected(x52_dev)) { + return; + } + + pthread_mutex_lock(&device_mutex); + /* Use a dummy vendor command to check if the device is connected */ + rc = libx52_vendor_command(x52_dev, 0, 0); + pthread_mutex_unlock(&device_mutex); + + if (rc != LIBX52_SUCCESS) { + if (rc == LIBX52_ERROR_NO_DEVICE) { + // Detach and spawn thread to reconnect + PINELOG_TRACE("Disconnecting detached device"); + libx52_disconnect(x52_dev); + + PINELOG_TRACE("Disabling device update thread"); + device_upd_thr_enable = false; + + PINELOG_TRACE("Signaling device search thread"); + device_acq_thr_enable = true; + } else { + PINELOG_ERROR(_("Error %d when updating X52 device: %s"), + rc, libx52_strerror(rc)); + } + } +} diff --git a/daemon/x52d_device.h b/daemon/x52d_device.h index e7f024c..a4bebe1 100644 --- a/daemon/x52d_device.h +++ b/daemon/x52d_device.h @@ -27,5 +27,6 @@ int x52d_dev_set_brightness(uint8_t mfd, uint16_t brightness); int x52d_dev_set_shift(uint8_t state); int x52d_dev_set_blink(uint8_t state); int x52d_dev_update(void); +void x52d_dev_check(void); #endif // !defined X52D_DEVICE_H diff --git a/po/x52pro-linux.pot b/po/x52pro-linux.pot index 58ba776..64dd3fe 100644 --- a/po/x52pro-linux.pot +++ b/po/x52pro-linux.pot @@ -8,7 +8,7 @@ msgid "" msgstr "" "Project-Id-Version: x52pro-linux 0.2.2\n" "Report-Msgid-Bugs-To: https://github.com/nirenjan/x52pro-linux/issues\n" -"POT-Creation-Date: 2021-08-30 11:59-0700\n" +"POT-Creation-Date: 2021-08-30 12:33-0700\n" "PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n" "Last-Translator: FULL NAME \n" "Language-Team: LANGUAGE \n" @@ -688,29 +688,29 @@ msgstr "" msgid "Starting X52 device update thread" msgstr "" -#: daemon/x52d_device.c:105 +#: daemon/x52d_device.c:107 msgid "Initializing libx52" msgstr "" -#: daemon/x52d_device.c:109 +#: daemon/x52d_device.c:111 #, c-format msgid "Failure %d initializing libx52: %s" msgstr "" -#: daemon/x52d_device.c:128 +#: daemon/x52d_device.c:130 msgid "Shutting down X52 device acquisition thread" msgstr "" -#: daemon/x52d_device.c:131 +#: daemon/x52d_device.c:133 msgid "Shutting down X52 device update thread" msgstr "" -#: daemon/x52d_device.c:144 +#: daemon/x52d_device.c:146 #, c-format msgid "Error %d when updating X52 parameter: %s" msgstr "" -#: daemon/x52d_device.c:224 +#: daemon/x52d_device.c:226 daemon/x52d_device.c:261 #, c-format msgid "Error %d when updating X52 device: %s" msgstr "" diff --git a/po/xx_PL.po b/po/xx_PL.po index 804ded0..d0060a3 100644 --- a/po/xx_PL.po +++ b/po/xx_PL.po @@ -7,7 +7,7 @@ msgid "" msgstr "" "Project-Id-Version: x52pro-linux 0.2.1\n" "Report-Msgid-Bugs-To: https://github.com/nirenjan/x52pro-linux/issues\n" -"POT-Creation-Date: 2021-08-30 11:59-0700\n" +"POT-Creation-Date: 2021-08-30 12:33-0700\n" "PO-Revision-Date: 2021-08-30 10:41-0700\n" "Last-Translator: Nirenjan Krishnan \n" "Language-Team: Dummy Language for testing i18n\n" @@ -737,29 +737,29 @@ msgstr "Eviceday onnectedcay, itingwray onfigurationcay" msgid "Starting X52 device update thread" msgstr "Artingstay X52 eviceday updateay eadthray" -#: daemon/x52d_device.c:105 +#: daemon/x52d_device.c:107 msgid "Initializing libx52" msgstr "Initializingay libx52" -#: daemon/x52d_device.c:109 +#: daemon/x52d_device.c:111 #, c-format msgid "Failure %d initializing libx52: %s" msgstr "Ailurefay %d initializeay libx52: %s" -#: daemon/x52d_device.c:128 +#: daemon/x52d_device.c:130 msgid "Shutting down X52 device acquisition thread" msgstr "Uttingshay ownday X52 eviceday acquisitionay eadthray" -#: daemon/x52d_device.c:131 +#: daemon/x52d_device.c:133 msgid "Shutting down X52 device update thread" msgstr "Uttingshay ownday X52 eviceday updateay eadthray" -#: daemon/x52d_device.c:144 +#: daemon/x52d_device.c:146 #, c-format msgid "Error %d when updating X52 parameter: %s" msgstr "Erroray %d enwhay updatingay X52 arameterpay: %s" -#: daemon/x52d_device.c:224 +#: daemon/x52d_device.c:226 daemon/x52d_device.c:261 #, c-format msgid "Error %d when updating X52 device: %s" msgstr "Erroray %d enwhay updatingay X52 eviceday: %s"