From d2c8b63849c47bdadc4f1e030ec0cbe03a0b7baa Mon Sep 17 00:00:00 2001 From: Manfred Wallner Date: Tue, 2 Apr 2024 21:45:25 +0200 Subject: [PATCH 1/4] [MAINT] typos --- wiringPi/wiringPi.c | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/wiringPi/wiringPi.c b/wiringPi/wiringPi.c index 5c6a0f6..6a0981a 100644 --- a/wiringPi/wiringPi.c +++ b/wiringPi/wiringPi.c @@ -857,12 +857,12 @@ static void usingGpioMemCheck (const char *what) void PrintSystemStdErr () { struct utsname sys_info; if (uname(&sys_info) == 0) { - fprintf (stderr, " wiringpi = %d.%d\n", VERSION_MAJOR, VERSION_MINOR); - fprintf (stderr, " system name = %s\n", sys_info.sysname); - //fprintf (stderr, " node name = %s\n", sys_info.nodename); - fprintf (stderr, " release = %s\n", sys_info.release); - fprintf (stderr, " version = %s\n", sys_info.version); - fprintf (stderr, " machine = %s\n", sys_info.machine); + fprintf (stderr, " wiringpi : %d.%d\n", VERSION_MAJOR, VERSION_MINOR); + fprintf (stderr, " system name : %s\n", sys_info.sysname); + //fprintf (stderr, " node name : %s\n", sys_info.nodename); + fprintf (stderr, " release : %s\n", sys_info.release); + fprintf (stderr, " version : %s\n", sys_info.version); + fprintf (stderr, " machine : %s\n", sys_info.machine); if (strstr(sys_info.machine, "arm") == NULL && strstr(sys_info.machine, "aarch")==NULL) { fprintf (stderr, " -> This is not an ARM architecture; it cannot be a Raspberry Pi.\n") ; } @@ -2813,7 +2813,7 @@ int wiringPiSetupPhys (void) int wiringPiSetupSys (void) { piFunctionOops("wiringPiSetupSys", - "use wringpi 3.1 (last version with GPIO Sysfs interface)", + "use WiringPi 3.1 (last version with GPIO Sysfs interface)", "https://www.kernel.org/doc/html/v5.5/admin-guide/gpio/sysfs.html"); return 0 ; From 370f6112f77670c2da07c7b8ad18e58e94f1f2bc Mon Sep 17 00:00:00 2001 From: Manfred Wallner Date: Tue, 2 Apr 2024 21:55:47 +0200 Subject: [PATCH 2/4] [MAINT] typo --- wiringPi/wiringPi.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/wiringPi/wiringPi.c b/wiringPi/wiringPi.c index 6a0981a..fd06922 100644 --- a/wiringPi/wiringPi.c +++ b/wiringPi/wiringPi.c @@ -857,7 +857,7 @@ static void usingGpioMemCheck (const char *what) void PrintSystemStdErr () { struct utsname sys_info; if (uname(&sys_info) == 0) { - fprintf (stderr, " wiringpi : %d.%d\n", VERSION_MAJOR, VERSION_MINOR); + fprintf (stderr, " WiringPi : %d.%d\n", VERSION_MAJOR, VERSION_MINOR); fprintf (stderr, " system name : %s\n", sys_info.sysname); //fprintf (stderr, " node name : %s\n", sys_info.nodename); fprintf (stderr, " release : %s\n", sys_info.release); From 2b383b167e7ea12f7eab012c9f789c14f126ccef Mon Sep 17 00:00:00 2001 From: Benjamin Braunfels Date: Fri, 26 Apr 2024 14:35:50 +0200 Subject: [PATCH 3/4] Fixed warnings and removed unused function --- gpio/gpio.c | 57 ++++++++++++++++++++++++++++++++----- wiringPi/mcp3422.c | 21 ++++++++------ wiringPi/pcf8591.c | 6 +++- wiringPi/pseudoPins.c | 76 +++++++++++++++++++++++++++++-------------------- wiringPi/wiringPi.c | 25 ---------------- wiringPi/wiringSerial.c | 15 +++++++--- 6 files changed, 124 insertions(+), 76 deletions(-) diff --git a/gpio/gpio.c b/gpio/gpio.c index 36d1fc1..35a4e62 100644 --- a/gpio/gpio.c +++ b/gpio/gpio.c @@ -290,13 +290,33 @@ static void doLoad (int argc, char *argv []) if (!moduleLoaded (module1)) { sprintf (cmd, "%s %s%s", findExecutable (MODPROBE), module1, args1) ; - system (cmd) ; + int ret = system(cmd); + if (ret == -1) { + perror("Error executing command"); + } else if (WIFEXITED(ret)) { + int exit_status = WEXITSTATUS(ret); + if (exit_status != 0) { + fprintf(stderr, "Command failed with exit status %d\n", exit_status); + } + } else { + fprintf(stderr, "Command terminated by signal\n"); + } } if (!moduleLoaded (module2)) { sprintf (cmd, "%s %s%s", findExecutable (MODPROBE), module2, args2) ; - system (cmd) ; + int ret = system(cmd); + if (ret == -1) { + perror("Error executing command"); + } else if (WIFEXITED(ret)) { + int exit_status = WEXITSTATUS(ret); + if (exit_status != 0) { + fprintf(stderr, "Command failed with exit status %d\n", exit_status); + } + } else { + fprintf(stderr, "Command terminated by signal\n"); + } } if (!moduleLoaded (module2)) @@ -350,13 +370,33 @@ static void doUnLoad (int argc, char *argv []) if (moduleLoaded (module1)) { sprintf (cmd, "%s %s", findExecutable (RMMOD), module1) ; - system (cmd) ; + int ret = system(cmd); + if (ret == -1) { + perror("Error executing command"); + } else if (WIFEXITED(ret)) { + int exit_status = WEXITSTATUS(ret); + if (exit_status != 0) { + fprintf(stderr, "Command failed with exit status %d\n", exit_status); + } + } else { + fprintf(stderr, "Command terminated by signal\n"); + } } if (moduleLoaded (module2)) { sprintf (cmd, "%s %s", findExecutable (RMMOD), module2) ; - system (cmd) ; + int ret = system(cmd); + if (ret == -1) { + perror("Error executing command"); + } else if (WIFEXITED(ret)) { + int exit_status = WEXITSTATUS(ret); + if (exit_status != 0) { + fprintf(stderr, "Command failed with exit status %d\n", exit_status); + } + } else { + fprintf(stderr, "Command terminated by signal\n"); + } } } @@ -562,14 +602,14 @@ static volatile int globalCounter ; void printgpioflush(const char* text) { if (gpioDebug) { - printf(text); + printf("%s", text); fflush(stdout); } } void printgpio(const char* text) { if (gpioDebug) { - printf(text); + printf("%s", text); fflush(stdout); } } @@ -1371,7 +1411,10 @@ static void doVersion (char *argv []) { if ((fd = fopen ("/proc/device-tree/model", "r")) != NULL) { - fgets (name, 80, fd) ; + if (fgets(name, sizeof(name), fd) == NULL) { + // Handle error or end of file condition + perror("Error reading /proc/device-tree/model"); + } fclose (fd) ; printf (" *--> %s\n", name) ; } diff --git a/wiringPi/mcp3422.c b/wiringPi/mcp3422.c index 15934cb..fd441a5 100644 --- a/wiringPi/mcp3422.c +++ b/wiringPi/mcp3422.c @@ -43,15 +43,20 @@ ********************************************************************************* */ -void waitForConversion (int fd, unsigned char *buffer, int n) +void waitForConversion(int fd, unsigned char *buffer, int n) { - for (;;) - { - read (fd, buffer, n) ; - if ((buffer [n-1] & 0x80) == 0) - break ; - delay (1) ; - } + for (;;) { + ssize_t bytes_read = read(fd, buffer, n); + if (bytes_read != n) { + perror("Error reading from file descriptor"); + return; + } + + if ((buffer[n - 1] & 0x80) == 0) + break; + + delay(1); + } } /* diff --git a/wiringPi/pcf8591.c b/wiringPi/pcf8591.c index 902d8a8..10beb6b 100644 --- a/wiringPi/pcf8591.c +++ b/wiringPi/pcf8591.c @@ -24,6 +24,7 @@ */ #include +#include #include "wiringPi.h" #include "wiringPiI2C.h" @@ -41,7 +42,10 @@ static void myAnalogWrite (struct wiringPiNodeStruct *node, UNU int pin, int val unsigned char b [2] ; b [0] = 0x40 ; b [1] = value & 0xFF ; - write (node->fd, b, 2) ; + ssize_t bytes_written = write(node->fd, b, 2); + if (bytes_written != 2) { + perror("Error writing to file descriptor"); + } } diff --git a/wiringPi/pseudoPins.c b/wiringPi/pseudoPins.c index 36afe71..5a03d5e 100644 --- a/wiringPi/pseudoPins.c +++ b/wiringPi/pseudoPins.c @@ -35,9 +35,12 @@ #define SHARED_NAME "wiringPiPseudoPins" #define PSEUDO_PINS 64 +#include #include #include #include +#include +#include #include #include @@ -45,21 +48,20 @@ #include "pseudoPins.h" -static int myAnalogRead (struct wiringPiNodeStruct *node, int pin) +static int myAnalogRead(struct wiringPiNodeStruct *node, int pin) { - int *ptr = (int *)node->data0 ; - int myPin = pin - node->pinBase ; + int *ptr = (int *)(intptr_t)node->data0; // Cast to intptr_t to handle pointer-to-integer conversion + int myPin = pin - node->pinBase; - return *(ptr + myPin) ; + return *(ptr + myPin); } - -static void myAnalogWrite (struct wiringPiNodeStruct *node, int pin, int value) +static void myAnalogWrite(struct wiringPiNodeStruct *node, int pin, int value) { - int *ptr = (int *)node->data0 ; - int myPin = pin - node->pinBase ; + int *ptr = (int *)(intptr_t)node->data0; + int myPin = pin - node->pinBase; - *(ptr + myPin) = value ; + *(ptr + myPin) = value; } @@ -69,27 +71,39 @@ static void myAnalogWrite (struct wiringPiNodeStruct *node, int pin, int value) ********************************************************************************* */ -int pseudoPinsSetup (const int pinBase) -{ - struct wiringPiNodeStruct *node ; - void *ptr ; - - node = wiringPiNewNode (pinBase, PSEUDO_PINS) ; - - node->fd = shm_open (SHARED_NAME, O_CREAT | O_RDWR, 0666) ; - - if (node->fd < 0) - return FALSE ; - if (ftruncate (node->fd, PSEUDO_PINS * sizeof (int)) < 0) - return FALSE ; - - ptr = mmap (NULL, PSEUDO_PINS * sizeof (int), PROT_READ | PROT_WRITE, MAP_SHARED, node->fd, 0) ; - - node->data0 = (unsigned int)ptr ; - - node->analogRead = myAnalogRead ; - node->analogWrite = myAnalogWrite ; - - return TRUE ; +int pseudoPinsSetup(const int pinBase) +{ + struct wiringPiNodeStruct *node; + void *ptr; + + node = wiringPiNewNode(pinBase, PSEUDO_PINS); + if (node == NULL) { + perror("Error creating new wiringPi node"); + return FALSE; + } + + node->fd = shm_open(SHARED_NAME, O_CREAT | O_RDWR, 0666); + if (node->fd < 0) { + perror("Error opening shared memory"); + return FALSE; + } + + if (ftruncate(node->fd, PSEUDO_PINS * sizeof(int)) < 0) { + perror("Error resizing shared memory"); + return FALSE; + } + + ptr = mmap(NULL, PSEUDO_PINS * sizeof(int), PROT_READ | PROT_WRITE, MAP_SHARED, node->fd, 0); + if (ptr == MAP_FAILED) { + perror("Error mapping shared memory"); + return FALSE; + } + + node->data0 = (unsigned int)(uintptr_t)ptr; + + node->analogRead = myAnalogRead; + node->analogWrite = myAnalogWrite; + + return TRUE; } diff --git a/wiringPi/wiringPi.c b/wiringPi/wiringPi.c index 5c6a0f6..5bb9ade 100644 --- a/wiringPi/wiringPi.c +++ b/wiringPi/wiringPi.c @@ -536,31 +536,6 @@ const int _0v=-1; const int _3v=-1; -static int physToSysGPIOPi5 [41] = -{ - -1, // 0 - _3v, _5v, // 1, 2 - 401, _5v, - 402, _0v, - 403, 413, - _0v, 414, - 416, 417, - 426, _0v, - 421, 422, - _3v, 423, - 409, _0v, - 408, 424, - 410, 407, - _0v, 406, - 399, 400, - 404, _0v, - 405, 411, - 412, _0v, - 418, 415, - 425, 419, - _0v, 420, //39, 40 -} ; - int GPIOToSysFS(const int pin) { int sysfspin = pin; if (RaspberryPiModel<0) { //need to detect pi model diff --git a/wiringPi/wiringSerial.c b/wiringPi/wiringSerial.c index 9d59702..c57acb7 100644 --- a/wiringPi/wiringSerial.c +++ b/wiringPi/wiringSerial.c @@ -153,9 +153,12 @@ void serialClose (const int fd) ********************************************************************************* */ -void serialPutchar (const int fd, const unsigned char c) +void serialPutchar(const int fd, const unsigned char c) { - write (fd, &c, 1) ; + ssize_t bytes_written = write(fd, &c, 1); + if (bytes_written != 1) { + perror("Error writing to file descriptor"); + } } @@ -165,9 +168,13 @@ void serialPutchar (const int fd, const unsigned char c) ********************************************************************************* */ -void serialPuts (const int fd, const char *s) +void serialPuts(const int fd, const char *s) { - write (fd, s, strlen (s)) ; + size_t len = strlen(s); + ssize_t bytes_written = write(fd, s, len); + if (bytes_written != (ssize_t)len) { + perror("Error writing to file descriptor"); + } } /* From 783d95962ff31d0e243b4df5ba4aa1c766c063b7 Mon Sep 17 00:00:00 2001 From: mstroh76 Date: Fri, 3 May 2024 15:15:20 +0200 Subject: [PATCH 4/4] #224 --- gpio/gpio.c | 3 +-- wiringPi/pseudoPins.c | 22 +++++++++++----------- wiringPi/wiringSerial.c | 4 ++-- 3 files changed, 14 insertions(+), 15 deletions(-) diff --git a/gpio/gpio.c b/gpio/gpio.c index 35a4e62..86b1a42 100644 --- a/gpio/gpio.c +++ b/gpio/gpio.c @@ -610,7 +610,6 @@ void printgpioflush(const char* text) { void printgpio(const char* text) { if (gpioDebug) { printf("%s", text); - fflush(stdout); } } @@ -660,7 +659,7 @@ void doWfi (int argc, char *argv []) exit (1) ; } - printgpio("wait for interrupt function call \n"); + printgpio("wait for interrupt function call\n"); for (int Sec=0; Secdata0; - int myPin = pin - node->pinBase; + int *ptr = (int *)(intptr_t)node->data0; + int myPin = pin - node->pinBase; - *(ptr + myPin) = value; + *(ptr + myPin) = value; } @@ -79,25 +79,25 @@ int pseudoPinsSetup(const int pinBase) node = wiringPiNewNode(pinBase, PSEUDO_PINS); if (node == NULL) { - perror("Error creating new wiringPi node"); - return FALSE; + fprintf(stderr, "Error creating new wiringPi node"); + return FALSE; } node->fd = shm_open(SHARED_NAME, O_CREAT | O_RDWR, 0666); if (node->fd < 0) { - perror("Error opening shared memory"); - return FALSE; + perror("Error opening shared memory"); + return FALSE; } if (ftruncate(node->fd, PSEUDO_PINS * sizeof(int)) < 0) { - perror("Error resizing shared memory"); - return FALSE; + perror("Error resizing shared memory"); + return FALSE; } ptr = mmap(NULL, PSEUDO_PINS * sizeof(int), PROT_READ | PROT_WRITE, MAP_SHARED, node->fd, 0); if (ptr == MAP_FAILED) { - perror("Error mapping shared memory"); - return FALSE; + perror("Error mapping shared memory"); + return FALSE; } node->data0 = (unsigned int)(uintptr_t)ptr; diff --git a/wiringPi/wiringSerial.c b/wiringPi/wiringSerial.c index c57acb7..40fbad8 100644 --- a/wiringPi/wiringSerial.c +++ b/wiringPi/wiringSerial.c @@ -157,7 +157,7 @@ void serialPutchar(const int fd, const unsigned char c) { ssize_t bytes_written = write(fd, &c, 1); if (bytes_written != 1) { - perror("Error writing to file descriptor"); + perror("Error writing to file descriptor"); } } @@ -173,7 +173,7 @@ void serialPuts(const int fd, const char *s) size_t len = strlen(s); ssize_t bytes_written = write(fd, s, len); if (bytes_written != (ssize_t)len) { - perror("Error writing to file descriptor"); + perror("Error writing to file descriptor"); } }