From 70eee9942e74e05335a68d0d281b992e766fe367 Mon Sep 17 00:00:00 2001 From: Dee Newcum Date: Fri, 11 Feb 2022 10:20:28 -0600 Subject: [PATCH 1/2] improve error handling --- pi400.c | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/pi400.c b/pi400.c index cb5f013..4f822d3 100644 --- a/pi400.c +++ b/pi400.c @@ -14,6 +14,7 @@ #include #include #include +#include #include #include @@ -36,6 +37,24 @@ void signal_handler(int dummy) { running = 0; } +void prechecks(char *argv0) { + DIR *dir; + + if (geteuid() != 0) { + printf("Error: %s must be run as root.\n", argv0); + exit(1); + } + + // is the 'dwc2' module properly loaded? + dir = opendir("/sys/module/dwc2"); + if (!dir || errno == ENOENT) { + printf("Error: This must be added to /boot/config.txt and the system rebooted:\n"); + printf(" dtoverlay=dwc2\n"); + exit(1); + } + closedir(dir); +} + bool modprobe_libcomposite() { pid_t pid; @@ -154,7 +173,9 @@ void send_empty_hid_reports_both() { } } -int main() { +int main(int argc, char *argv[]) { + prechecks(argv[0]); + modprobe_libcomposite(); keyboard_buf.report_id = 1; From fa285bfd45aa3d14a89c0c5c118618d47627cfc9 Mon Sep 17 00:00:00 2001 From: Dee Newcum Date: Fri, 11 Feb 2022 15:23:21 -0600 Subject: [PATCH 2/2] wrap prechecks() in #ifdef NO_OUTPUT --- pi400.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/pi400.c b/pi400.c index 4f822d3..5b69be9 100644 --- a/pi400.c +++ b/pi400.c @@ -174,7 +174,9 @@ void send_empty_hid_reports_both() { } int main(int argc, char *argv[]) { +#ifndef NO_OUTPUT prechecks(argv[0]); +#endif modprobe_libcomposite();