From ab946b4a1ac394bdc8f2c0c6513ac3d093e33e84 Mon Sep 17 00:00:00 2001 From: nirenjan Date: Fri, 16 Jul 2021 00:28:30 -0700 Subject: [PATCH] Add device manager implementation --- .gitignore | 1 + daemon/Makefile.am | 11 +++- daemon/x52d_device.c | 151 +++++++++++++++++++++++++++++++++++++++++++ daemon/x52d_device.h | 33 ++++++++++ daemon/x52d_main.c | 3 +- po/POTFILES.in | 1 + po/x52pro-linux.pot | 28 ++++++-- po/xx_PL.po | 28 ++++++-- 8 files changed, 244 insertions(+), 12 deletions(-) create mode 100644 daemon/x52d_device.c create mode 100644 daemon/x52d_device.h diff --git a/.gitignore b/.gitignore index 7e72371..1c51b6b 100644 --- a/.gitignore +++ b/.gitignore @@ -13,6 +13,7 @@ util/x52charmapgen* lib/libusbx52/x52test* udev/*.rules daemon/x52d* +!daemon/x52d_*.* x52pro-linux-*.tar.gz # Module files diff --git a/daemon/Makefile.am b/daemon/Makefile.am index 3b1b846..8dc7987 100644 --- a/daemon/Makefile.am +++ b/daemon/Makefile.am @@ -10,7 +10,8 @@ bin_PROGRAMS = x52d # Service daemon that manages the X52 device x52d_SOURCES = \ - x52d_main.c x52d_config_parser.c x52d_config.c + x52d_main.c x52d_config_parser.c x52d_config.c \ + x52d_device.c x52d_CFLAGS = \ -I $(top_srcdir) \ @@ -22,13 +23,17 @@ x52d_CFLAGS = \ -DLOCALEDIR=\"$(localedir)\" \ -DLOGDIR=\"$(localstatedir)/log\" \ -DRUNDIR=\"$(runstatedir)\" \ - @INIH_CFLAGS@ $(WARN_CFLAGS) + @INIH_CFLAGS@ @X52_INCLUDE@ $(WARN_CFLAGS) x52d_LDFLAGS = @INIH_LIBS@ $(WARN_LDFLAGS) -x52d_LDADD = ../lib/pinelog/libpinelog.la @LTLIBINTL@ +x52d_LDADD = \ + ../lib/pinelog/libpinelog.la \ + ../lib/libx52/libx52.la \ + @LTLIBINTL@ EXTRA_DIST = \ x52d_config.def \ x52d_config.h \ x52d_const.h \ + x52d_device.h \ x52d.conf diff --git a/daemon/x52d_device.c b/daemon/x52d_device.c new file mode 100644 index 0000000..36cbf1a --- /dev/null +++ b/daemon/x52d_device.c @@ -0,0 +1,151 @@ +/* + * Saitek X52 Pro MFD & LED driver - Device manager + * + * Copyright (C) 2021 Nirenjan Krishnan (nirenjan@nirenjan.org) + * + * SPDX-License-Identifier: GPL-2.0-only WITH Classpath-exception-2.0 + */ + +#include "config.h" +#include +#include + +#include "x52d_const.h" +#include "x52d_device.h" +#include "libx52.h" +#include "pinelog.h" + +/* + * Functions + * + * Thread method to scan and reconnect to the joystick if it has been + * disconnected/never connected. + * + * API wrappers to libx52 functions + */ + +static libx52_device *x52_dev; + +static pthread_mutex_t device_mutex = PTHREAD_MUTEX_INITIALIZER; + +#if 0 +static void *x52_dev_thread(void *param) +{ + int rc; + + // Check if the device is connected in a loop + for (;;) { + #define RECONNECT_DELAY 5 + sleep(RECONNECT_DELAY); + if (!libx52_is_connected(x52_dev)) { + PINELOG_TRACE("Attempting to connect to X52 device"); + rc = libx52_connect(x52_dev); + if (rc != LIBX52_SUCCESS) { + if (rc != LIBX52_ERROR_NO_DEVICE) { + PINELOG_ERROR(_("Error %d connecting to device: %s"), + rc, libx52_strerror(rc)); + } + } + } + } + + return NULL; +} +#endif + +void x52d_dev_init(void) +{ + int rc; + PINELOG_TRACE("Initializing libx52"); + rc = libx52_init(&x52_dev); + + if (rc != LIBX52_SUCCESS) { + PINELOG_FATAL(_("Failure %d initializing libx52: %s"), + rc, libx52_strerror(rc)); + } + + // TODO: Create and initialize the threads +} + +void x52d_dev_exit(void) +{ + // TODO: Shutdown any threads + + libx52_exit(x52_dev); +} + +#define WRAP_LIBX52(func) \ + int rc; \ + pthread_mutex_lock(&device_mutex); \ + rc = func; \ + pthread_mutex_unlock(&device_mutex); \ + if (rc != LIBX52_SUCCESS) { \ + PINELOG_ERROR(_("Error %d when updating X52 parameter: %s"), \ + rc, libx52_strerror(rc)); \ + } \ + return rc + +int x52d_dev_set_text(uint8_t line, const char *text, uint8_t length) +{ + WRAP_LIBX52(libx52_set_text(x52_dev, line, text, length)); +} +int x52d_dev_set_led_state(libx52_led_id led, libx52_led_state state) +{ + WRAP_LIBX52(libx52_set_led_state(x52_dev, led, state)); +} +int x52d_dev_set_clock(time_t time, int local) +{ + WRAP_LIBX52(libx52_set_clock(x52_dev, time, local)); +} +int x52d_dev_set_clock_timezone(libx52_clock_id clock, int offset) +{ + WRAP_LIBX52(libx52_set_clock_timezone(x52_dev, clock, offset)); +} +int x52d_dev_set_clock_format(libx52_clock_id clock, libx52_clock_format format) +{ + WRAP_LIBX52(libx52_set_clock_format(x52_dev, clock, format)); +} +int x52d_dev_set_time(uint8_t hour, uint8_t minute) +{ + WRAP_LIBX52(libx52_set_time(x52_dev, hour, minute)); +} +int x52d_dev_set_date(uint8_t dd, uint8_t mm, uint8_t yy) +{ + WRAP_LIBX52(libx52_set_date(x52_dev, dd, mm, yy)); +} +int x52d_dev_set_date_format(libx52_date_format format) +{ + WRAP_LIBX52(libx52_set_date_format(x52_dev, format)); +} +int x52d_dev_set_brightness(uint8_t mfd, uint16_t brightness) +{ + WRAP_LIBX52(libx52_set_brightness(x52_dev, mfd, brightness)); +} +int x52d_dev_set_shift(uint8_t state) +{ + WRAP_LIBX52(libx52_set_shift(x52_dev, state)); +} +int x52d_dev_set_blink(uint8_t state) +{ + WRAP_LIBX52(libx52_set_blink(x52_dev, state)); +} +int x52d_dev_update(void) +{ + int rc; + + pthread_mutex_lock(&device_mutex); + rc = libx52_update(x52_dev); + pthread_mutex_unlock(&device_mutex); + + if (rc != LIBX52_SUCCESS) { + if (rc == LIBX52_ERROR_NO_DEVICE) { + // TODO: Detach and spawn thread to reconnect + libx52_disconnect(x52_dev); + } else { + PINELOG_ERROR(_("Error %d when updating X52 device: %s"), + rc, libx52_strerror(rc)); + } + } + + return rc; +} diff --git a/daemon/x52d_device.h b/daemon/x52d_device.h new file mode 100644 index 0000000..e826675 --- /dev/null +++ b/daemon/x52d_device.h @@ -0,0 +1,33 @@ +/* + * Saitek X52 Pro MFD & LED driver - Device manager header + * + * Copyright (C) 2021 Nirenjan Krishnan (nirenjan@nirenjan.org) + * + * SPDX-License-Identifier: GPL-2.0-only WITH Classpath-exception-2.0 + */ + +#ifndef X52D_DEVICE_H +#define X52D_DEVICE_H + +#include "libx52.h" + +void x52d_dev_init(void); +void x52d_dev_exit(void); + +void x52d_dev_start_thread(void); + +/* Wrapper methods for libx52 calls */ +int x52d_dev_set_text(uint8_t line, const char *text, uint8_t length); +int x52d_dev_set_led_state(libx52_led_id led, libx52_led_state state); +int x52d_dev_set_clock(time_t time, int local); +int x52d_dev_set_clock_timezone(libx52_clock_id clock, int offset); +int x52d_dev_set_clock_format(libx52_clock_id clock, libx52_clock_format format); +int x52d_dev_set_time(uint8_t hour, uint8_t minute); +int x52d_dev_set_date(uint8_t dd, uint8_t mm, uint8_t yy); +int x52d_dev_set_date_format(libx52_date_format format); +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); + +#endif // !defined X52D_DEVICE_H diff --git a/daemon/x52d_main.c b/daemon/x52d_main.c index 595de2c..ec4308a 100644 --- a/daemon/x52d_main.c +++ b/daemon/x52d_main.c @@ -1,6 +1,7 @@ /* * Saitek X52 Pro MFD & LED driver - Service daemon - * * Copyright (C) 2021 Nirenjan Krishnan (nirenjan@nirenjan.org) + * + * Copyright (C) 2021 Nirenjan Krishnan (nirenjan@nirenjan.org) * * SPDX-License-Identifier: GPL-2.0-only WITH Classpath-exception-2.0 */ diff --git a/po/POTFILES.in b/po/POTFILES.in index 987e9d8..5819676 100644 --- a/po/POTFILES.in +++ b/po/POTFILES.in @@ -14,3 +14,4 @@ utils/test/x52_test_mfd.c daemon/x52d_main.c daemon/x52d_config.c daemon/x52d_config_parser.c +daemon/x52d_device.c diff --git a/po/x52pro-linux.pot b/po/x52pro-linux.pot index 1231cb8..749d20e 100644 --- a/po/x52pro-linux.pot +++ b/po/x52pro-linux.pot @@ -8,7 +8,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-07-15 15:08-0700\n" +"POT-Creation-Date: 2021-07-16 00:26-0700\n" "PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n" "Last-Translator: FULL NAME \n" "Language-Team: LANGUAGE \n" @@ -370,17 +370,17 @@ msgstr "" msgid "OK" msgstr "" -#: daemon/x52d_main.c:32 +#: daemon/x52d_main.c:33 #, c-format msgid "Error %d setting log file: %s\n" msgstr "" -#: daemon/x52d_main.c:40 +#: daemon/x52d_main.c:44 #, c-format msgid "Usage: %s [-f] [-v] [-q] [-l log-file] [-o override] [-c config-file]\n" msgstr "" -#: daemon/x52d_main.c:101 +#: daemon/x52d_main.c:105 #, c-format msgid "Unable to parse configuration override '%s'\n" msgstr "" @@ -427,3 +427,23 @@ msgstr "" #, c-format msgid "Error processing override '%s.%s=%s'" msgstr "" + +#: daemon/x52d_device.c:45 +#, c-format +msgid "Error %d connecting to device: %s" +msgstr "" + +#: daemon/x52d_device.c:63 +#, c-format +msgid "Failure %d initializing libx52: %s" +msgstr "" + +#: daemon/x52d_device.c:83 +#, c-format +msgid "Error %d when updating X52 parameter: %s" +msgstr "" + +#: daemon/x52d_device.c:145 +#, c-format +msgid "Error %d when updating X52 device: %s" +msgstr "" diff --git a/po/xx_PL.po b/po/xx_PL.po index e3fcb6d..ee01b03 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-07-15 15:08-0700\n" +"POT-Creation-Date: 2021-07-16 00:26-0700\n" "PO-Revision-Date: 2021-07-15 15:09-0700\n" "Last-Translator: Nirenjan Krishnan \n" "Language-Team: Dummy Language for testing i18n\n" @@ -413,19 +413,19 @@ msgstr "Estingtay aracterchay 0x%02x..." msgid "OK" msgstr "OKay" -#: daemon/x52d_main.c:32 +#: daemon/x52d_main.c:33 #, c-format msgid "Error %d setting log file: %s\n" msgstr "Erroray %d ettingsay oglay ilefay: %s\n" -#: daemon/x52d_main.c:40 +#: daemon/x52d_main.c:44 #, c-format msgid "Usage: %s [-f] [-v] [-q] [-l log-file] [-o override] [-c config-file]\n" msgstr "" "Usageay: %s [-f] [-v] [-q] [-l oglay-ilefay] [-o overrideay] [-c onfigcay-" "ilefay]\n" -#: daemon/x52d_main.c:101 +#: daemon/x52d_main.c:105 #, c-format msgid "Unable to parse configuration override '%s'\n" msgstr "Unableay otay arsepay onfigurationcay overrideay '%s'\n" @@ -472,3 +472,23 @@ msgstr "Onay aluevay oundfay inay overrideay ingstray '%s'" #, c-format msgid "Error processing override '%s.%s=%s'" msgstr "Erroray ocessingpray overriday '%s.%s=%s'" + +#: daemon/x52d_device.c:45 +#, c-format +msgid "Error %d connecting to device: %s" +msgstr "Erroray %d onnectingcay otay eviceday: %s" + +#: daemon/x52d_device.c:63 +#, c-format +msgid "Failure %d initializing libx52: %s" +msgstr "Ailurefay %d initializeay libx52: %s" + +#: daemon/x52d_device.c:83 +#, c-format +msgid "Error %d when updating X52 parameter: %s" +msgstr "Erroray %d enwhay updatingay X52 arameterpay: %s" + +#: daemon/x52d_device.c:145 +#, c-format +msgid "Error %d when updating X52 device: %s" +msgstr "Erroray %d enwhay updatingay X52 eviceday: %s"