diff --git a/MakeRemote b/MakeRemote index a3a235c..250d40e 100644 --- a/MakeRemote +++ b/MakeRemote @@ -96,6 +96,6 @@ command.o: command.c pigpio.h command.h pigpiod.o: pigpiod.c pigpio.h pigpiod_if.o: pigpiod_if.c pigpio.h pigpiod_if.h command.h pigpiod_if2.o: pigpiod_if2.c pigpio.h pigpiod_if2.h command.h -pigs.o: pigs.c pigpio.h command.h +pigs.o: pigs.c pigpio.h command.h pigs.h diff --git a/Makefile b/Makefile index ac68064..3d94f4d 100644 --- a/Makefile +++ b/Makefile @@ -135,7 +135,7 @@ $(LIB3): $(OBJ3) pig2vcd.o: pig2vcd.c pigpio.h pigpiod.o: pigpiod.c pigpio.h -pigs.o: pigs.c pigpio.h command.h +pigs.o: pigs.c pigpio.h command.h pigs.h x_pigpio.o: x_pigpio.c pigpio.h x_pigpiod_if.o: x_pigpiod_if.c pigpiod_if.h pigpio.h x_pigpiod_if2.o: x_pigpiod_if2.c pigpiod_if2.h pigpio.h diff --git a/README b/README index 35b4789..ed453a9 100644 --- a/README +++ b/README @@ -23,6 +23,7 @@ o the library (libpigpiod_if2.so) in /usr/local/lib o the header file (pigpio.h) in /usr/local/include o the header file (pigpiod_if.h) in /usr/local/include o the header file (pigpiod_if2.h) in /usr/local/include +o the header file (pigs.h) in /usr/local/include o the daemon (pigpiod) in /usr/local/bin o the socket interface (pigs) in /usr/local/bin o the utility pig2vcd in /usr/local/bin @@ -35,7 +36,7 @@ TEST (optional) * * * All the tests make extensive use of gpio 25 (pin 22). * * Ensure that either nothing or just a LED is connected to * -* gpio 4 before running any of the tests. * +* gpio 25 before running any of the tests. * * * * Some tests are statistical in nature and so may on * * occasion fail. Repeated failures on the same test or * diff --git a/pigpio.h b/pigpio.h index 65dc902..11ee4b9 100644 --- a/pigpio.h +++ b/pigpio.h @@ -31,7 +31,7 @@ For more information, please refer to #include #include -#define PIGPIO_VERSION 6701 +#define PIGPIO_VERSION 6702 /*TEXT diff --git a/pigs.1 b/pigs.1 index 51a9e08..df8fcb5 100644 --- a/pigs.1 +++ b/pigs.1 @@ -96,6 +96,26 @@ echo "{command}+" >/dev/pigpio .br pigs will show the result of the command on screen. +.br +The pigs process returns an exit status (which can be displayed with +the command echo $?). + +.br + +.EX +PIGS_OK 0 +.br +PIGS_CONNECT_ERR 255 +.br +PIGS_OPTION_ERR 254 +.br +PIGS_SCRIPT_ERR 253 +.br +.br +.br + +.EE + .br The results of /dev/pigpio commands need to be read from /dev/pigout, e.g. cat /dev/pigout (try cat /dev/pigout& so that all subsequent diff --git a/pigs.c b/pigs.c index 24f1fee..4ae97eb 100644 --- a/pigs.c +++ b/pigs.c @@ -26,7 +26,7 @@ For more information, please refer to */ /* -This version is for pigpio version 56+ +This version is for pigpio version 67+ */ #include @@ -42,6 +42,7 @@ This version is for pigpio version 56+ #include "pigpio.h" #include "command.h" +#include "pigs.h" /* This program provides a socket interface to some of @@ -53,16 +54,20 @@ char response_buf[CMD_MAX_EXTENSION]; int printFlags = 0; +int status = PIGS_OK; + #define SOCKET_OPEN_FAILED -1 #define PRINT_HEX 1 #define PRINT_ASCII 2 -void fatal(char *fmt, ...) +void report(int err, char *fmt, ...) { char buf[128]; va_list ap; + if (err > status) status = err; + va_start(ap, fmt); vsnprintf(buf, sizeof(buf), fmt, ap); va_end(ap); @@ -76,6 +81,8 @@ static int initOpts(int argc, char *argv[]) { int opt, args; + opterr = 0; + args = 1; while ((opt = getopt(argc, argv, "ax")) != -1) @@ -91,7 +98,11 @@ static int initOpts(int argc, char *argv[]) printFlags |= PRINT_HEX; args++; break; - } + + default: + args++; + report(PIGS_OPTION_ERR, "ERROR: bad option %c", optopt); + } } return args; } @@ -150,13 +161,13 @@ void print_result(int sock, int rv, cmdCmd_t cmd) if (r < 0) { printf("%d\n", r); - fatal("ERROR: %s", cmdErrStr(r)); + report(PIGS_SCRIPT_ERR, "ERROR: %s", cmdErrStr(r)); } break; case 2: printf("%d\n", r); - if (r < 0) fatal("ERROR: %s", cmdErrStr(r)); + if (r < 0) report(PIGS_SCRIPT_ERR, "ERROR: %s", cmdErrStr(r)); break; case 3: @@ -176,7 +187,7 @@ void print_result(int sock, int rv, cmdCmd_t cmd) I2CZ SERR SLR SPIX SPIR */ printf("%d", r); - if (r < 0) fatal("ERROR: %s", cmdErrStr(r)); + if (r < 0) report(PIGS_SCRIPT_ERR, "ERROR: %s", cmdErrStr(r)); if (r > 0) { if (printFlags == PRINT_ASCII) printf(" "); @@ -203,7 +214,7 @@ void print_result(int sock, int rv, cmdCmd_t cmd) if (r != (4 + (4*PI_MAX_SCRIPT_PARAMS))) { printf("%d", r); - fatal("ERROR: %s", cmdErrStr(r)); + report(PIGS_SCRIPT_ERR, "ERROR: %s", cmdErrStr(r)); } else { @@ -223,7 +234,7 @@ void print_result(int sock, int rv, cmdCmd_t cmd) if (r < 0) { printf("%d", r); - fatal("ERROR: %s", cmdErrStr(r)); + report(PIGS_SCRIPT_ERR, "ERROR: %s", cmdErrStr(r)); } p = (uint32_t *)response_buf; @@ -354,26 +365,29 @@ int main(int argc , char *argv[]) print_result(sock, cmdInfo[idx].rv, cmd); } - else fatal("socket receive failed"); + else report(PIGS_CONNECT_ERR, "socket receive failed"); } - else fatal("socket send failed"); + else report(PIGS_CONNECT_ERR, "socket send failed"); } - else fatal("socket connect failed"); + else report(PIGS_CONNECT_ERR, "socket connect failed"); } } - else fatal("%s only allowed within a script", cmdInfo[idx].name); + else report(PIGS_SCRIPT_ERR, + "%s only allowed within a script", cmdInfo[idx].name); } else { if (idx == CMD_UNKNOWN_CMD) - fatal("%s? unknown command, pigs h for help", cmdStr()); + report(PIGS_SCRIPT_ERR, + "%s? unknown command, pigs h for help", cmdStr()); else - fatal("%s: bad parameter, pigs h for help", cmdStr()); + report(PIGS_SCRIPT_ERR, + "%s: bad parameter, pigs h for help", cmdStr()); } } if (sock >= 0) close(sock); - return 0; + return status; } diff --git a/pigs.h b/pigs.h new file mode 100644 index 0000000..16cfcdd --- /dev/null +++ b/pigs.h @@ -0,0 +1,41 @@ +/* +This is free and unencumbered software released into the public domain. + +Anyone is free to copy, modify, publish, use, compile, sell, or +distribute this software, either in source code form or as a compiled +binary, for any purpose, commercial or non-commercial, and by any +means. + +In jurisdictions that recognize copyright laws, the author or authors +of this software dedicate any and all copyright interest in the +software to the public domain. We make this dedication for the benefit +of the public at large and to the detriment of our heirs and +successors. We intend this dedication to be an overt act of +relinquishment in perpetuity of all present and future rights to this +software under copyright law. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. +IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR +OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, +ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR +OTHER DEALINGS IN THE SOFTWARE. + +For more information, please refer to +*/ + +/* +This version is for pigpio version 67+ +*/ + +#ifndef PIGS_H +#define PIGS_H + +#define PIGS_OK 0 +#define PIGS_CONNECT_ERR 255 +#define PIGS_OPTION_ERR 254 +#define PIGS_SCRIPT_ERR 253 + +#endif +