diff --git a/configure b/configure index 86b73c3..13d69fd 100755 --- a/configure +++ b/configure @@ -28,7 +28,7 @@ Installation options: MySensors options: --my-debug=[enable|disable] - Enables or disables debugging. [enable] + Enables or disables MySensors core debugging. [enable] --my-config-file= Config file path. [/etc/mysensors.dat] --my-gateway=[none|ethernet|serial|mqtt] Gateway type, set to none to disable gateway feature. [ethernet] diff --git a/core/MyGatewayTransportSerial.cpp b/core/MyGatewayTransportSerial.cpp index bbad4b3..8b5120d 100644 --- a/core/MyGatewayTransportSerial.cpp +++ b/core/MyGatewayTransportSerial.cpp @@ -17,7 +17,6 @@ * version 2 as published by the Free Software Foundation. */ - #include "MyConfig.h" #include "MyProtocol.h" #include "MyGatewayTransport.h" @@ -31,7 +30,6 @@ char _serialInputString[MY_GATEWAY_MAX_RECEIVE_LENGTH]; // A buffer for incom int _serialInputPos; MyMessage _serialMsg; - bool gatewayTransportSend(MyMessage &message) { setIndication(INDICATION_GW_TX); MY_SERIALDEVICE.print(protocolFormat(message)); @@ -47,7 +45,6 @@ bool gatewayTransportInit() { return true; } - bool gatewayTransportAvailable() { while (MY_SERIALDEVICE.available()) { // get the new byte: diff --git a/core/MyHwLinuxGeneric.cpp b/core/MyHwLinuxGeneric.cpp index 2db3102..cc66f05 100644 --- a/core/MyHwLinuxGeneric.cpp +++ b/core/MyHwLinuxGeneric.cpp @@ -24,12 +24,14 @@ #include #include #include +#include "log.h" static const char* CONFIG_FILE = MY_LINUX_CONFIG_FILE; static const size_t _length = 1024; // ATMega328 has 1024 bytes static uint8_t _config[_length]; -bool CheckConfigFile() { +bool CheckConfigFile() +{ struct stat fileInfo; if (stat(CONFIG_FILE, &fileInfo) != 0) { @@ -152,27 +154,31 @@ int8_t hwSleep(uint8_t interrupt1, uint8_t mode1, uint8_t interrupt2, uint8_t mo return -2; } -uint16_t hwCPUVoltage() { +uint16_t hwCPUVoltage() +{ // TODO: Not supported! return 0; } -uint16_t hwCPUFrequency() { +uint16_t hwCPUFrequency() +{ // TODO: Not supported! return 0; } -uint16_t hwFreeMem() { +uint16_t hwFreeMem() +{ // TODO: Not supported! return 0; } #ifdef MY_DEBUG -void hwDebugPrint(const char *fmt, ... ) +void hwDebugPrint(const char *fmt, ...) { - va_list arglist; - va_start(arglist, fmt); - vprintf(fmt, arglist); - va_end(arglist); + va_list args; + + va_start(args, fmt); + mys_log_v(LOG_DEBUG, fmt, args); + va_end(args); } #endif diff --git a/core/MyMainLinux.cpp b/core/MyMainLinux.cpp index fd044e4..a461184 100644 --- a/core/MyMainLinux.cpp +++ b/core/MyMainLinux.cpp @@ -1,19 +1,23 @@ // Initialize library and handle sketch functions like we want to -#include +#include #include #include +#include +#include +#include +#include +#include "log.h" #include "MySensorsCore.h" -/* - * handler for SIGINT signal - */ void handle_sigint(int sig) { if (sig == SIGINT) { - std::cout << "Received SIGINT\n" << std::endl; + mys_log(LOG_NOTICE, "Received SIGINT\n\n"); } else if (sig == SIGTERM) { - std::cout << "Received SIGTERM\n" << std::endl; + mys_log(LOG_NOTICE, "Received SIGTERM\n\n"); + } else { + return; } #ifdef MY_RF24_IRQ_PIN @@ -27,11 +31,91 @@ void handle_sigint(int sig) exit(0); } -int main(void) { +static int daemonize(void) +{ + pid_t pid, sid; + + /* Fork off the parent process */ + pid = fork(); + if (pid < 0) { + mys_log(LOG_ERR, "fork: %s", strerror(errno)); + return -1; + } + /* If we got a good PID, then we can exit the parent process. */ + if (pid > 0) { + exit(EXIT_SUCCESS); + } + + /* At this point we are executing as the child process */ + + /* Change the file mode mask */ + umask(0); + + /* Create a new SID for the child process */ + sid = setsid(); + if (sid < 0) { + mys_log(LOG_ERR, "setsid: %s", strerror(errno)); + return -1; + } + + /* Change the current working directory. This prevents the current + directory from being locked; hence not being able to remove it. */ + if ((chdir("/")) < 0) { + mys_log(LOG_ERR, "chdir(\"/\"): %s", strerror(errno)); + return -1; + } + + freopen( "/dev/null", "r", stdin); + freopen( "/dev/null", "r", stdout); + freopen( "/dev/null", "r", stderr); + + return 0; +} + +int main(int argc, char *argv[]) +{ + int opt, log_opts, debug = 0, foreground = 1; + /* register the signal handler */ signal(SIGINT, handle_sigint); signal(SIGTERM, handle_sigint); + while ((opt = getopt(argc, argv, "hdb")) != -1) { + switch (opt) { + case 'h': + printf("Usage: mysGateway [options]\n\n" \ + "Options:\n" \ + "-h Display a short summary of all program options.\n" \ + "-d Enable debug.\n" \ + "-b Become a daemon.\n"); + exit(0); + case 'd': + debug = 1; + break; + case 'b': + foreground = 0; + break; + } + } + + log_opts = LOG_CONS; + if (foreground && isatty(STDIN_FILENO)) { + log_opts |= LOG_PERROR; + } + if (!debug) { + setlogmask(LOG_UPTO (LOG_INFO)); + } + openlog(NULL, log_opts, LOG_USER); + + if (!foreground && !debug) { + if (daemonize() != 0) { + exit(EXIT_FAILURE); + } + } + + mys_log(LOG_INFO, "Starting gateway...\n"); + mys_log(LOG_INFO, "Protocol version - %s\n", MYSENSORS_LIBRARY_VERSION); + _begin(); // Startup MySensors library for (;;) { diff --git a/drivers/Linux/EthernetClient.cpp b/drivers/Linux/EthernetClient.cpp index 387fcaa..9a9e65b 100644 --- a/drivers/Linux/EthernetClient.cpp +++ b/drivers/Linux/EthernetClient.cpp @@ -29,6 +29,7 @@ #include #include #include +#include "log.h" #include "EthernetClient.h" EthernetClient::EthernetClient() : _sock(-1) { @@ -50,7 +51,7 @@ int EthernetClient::connect(const char* host, uint16_t port) { sprintf(port_str, "%hu", port); if ((rv = getaddrinfo(host, port_str, &hints, &servinfo)) != 0) { - fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(rv)); + mys_log(LOG_ERR, "getaddrinfo: %s\n", gai_strerror(rv)); return -1; } @@ -58,13 +59,13 @@ int EthernetClient::connect(const char* host, uint16_t port) { for (p = servinfo; p != NULL; p = p->ai_next) { if ((sockfd = socket(p->ai_family, p->ai_socktype, p->ai_protocol)) == -1) { - perror("socket"); + mys_log(LOG_ERR, "socket: %s\n", strerror(errno)); continue; } if (::connect(sockfd, p->ai_addr, p->ai_addrlen) == -1) { close(sockfd); - perror("connect"); + mys_log(LOG_ERR, "connect: %s\n", strerror(errno)); continue; } @@ -72,7 +73,7 @@ int EthernetClient::connect(const char* host, uint16_t port) { } if (p == NULL) { - fprintf(stderr, "failed to connect\n"); + mys_log(LOG_ERR, "failed to connect\n"); return -1; } @@ -80,7 +81,7 @@ int EthernetClient::connect(const char* host, uint16_t port) { void *addr = &(((struct sockaddr_in*)p->ai_addr)->sin_addr); inet_ntop(p->ai_family, addr, s, sizeof s); - ETHERNETCLIENT_DEBUG("connected to %s\n", s); + mys_log(LOG_DEBUG, "connected to %s\n", s); freeaddrinfo(servinfo); // all done with this structure @@ -106,7 +107,7 @@ size_t EthernetClient::write(const uint8_t *buf, size_t size) { while (size > 0) { rc = send(_sock, buf + bytes, size, MSG_NOSIGNAL | MSG_DONTWAIT); if (rc == -1) { - perror("send"); + mys_log(LOG_ERR, "send: %s\n", strerror(errno)); close(_sock); _sock = -1; break; diff --git a/drivers/Linux/EthernetClient.h b/drivers/Linux/EthernetClient.h index 78dc143..2cbaca9 100644 --- a/drivers/Linux/EthernetClient.h +++ b/drivers/Linux/EthernetClient.h @@ -37,13 +37,6 @@ #define ETHERNETCLIENT_W5100_CLOSE_WAIT 0x1C #define ETHERNETCLIENT_W5100_LAST_ACK 0x1D -// debug -#if defined(ETHERNETCLIENT_VERBOSE) - #define ETHERNETCLIENT_DEBUG(x,...) debug(x, ##__VA_ARGS__) //!< debug -#else - #define ETHERNETCLIENT_DEBUG(x,...) //!< debug NULL -#endif - /** * EthernetClient class */ diff --git a/drivers/Linux/EthernetServer.cpp b/drivers/Linux/EthernetServer.cpp index 0537bb6..7ac89f4 100644 --- a/drivers/Linux/EthernetServer.cpp +++ b/drivers/Linux/EthernetServer.cpp @@ -28,6 +28,7 @@ #include #include #include +#include "log.h" #include "EthernetClient.h" #include "EthernetServer.h" @@ -56,26 +57,26 @@ void EthernetServer::begin(IPAddress address) sprintf(portstr, "%d", port); if ((rv = getaddrinfo(address.toString().c_str(), portstr, &hints, &servinfo)) != 0) { - fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(rv)); + mys_log(LOG_ERR, "getaddrinfo: %s\n", gai_strerror(rv)); return; } // loop through all the results and bind to the first we can for (p = servinfo; p != NULL; p = p->ai_next) { if ((sockfd = socket(p->ai_family, p->ai_socktype, p->ai_protocol)) == -1) { - perror("socket"); + mys_log(LOG_ERR, "socket: %s\n", strerror(errno)); continue; } if (setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(int)) == -1) { - perror("setsockopt"); + mys_log(LOG_ERR, "setsockopt: %s\n", strerror(errno)); freeaddrinfo(servinfo); return; } if (bind(sockfd, p->ai_addr, p->ai_addrlen) == -1) { close(sockfd); - perror("bind"); + mys_log(LOG_ERR, "bind: %s\n", strerror(errno)); continue; } @@ -83,13 +84,13 @@ void EthernetServer::begin(IPAddress address) } if (p == NULL) { - fprintf(stderr, "failed to bind\n"); + mys_log(LOG_ERR, "failed to bind\n"); freeaddrinfo(servinfo); return; } if (listen(sockfd, ETHERNETSERVER_BACKLOG) == -1) { - perror("listen"); + mys_log(LOG_ERR, "listen: %s\n", strerror(errno)); freeaddrinfo(servinfo); return; } @@ -101,7 +102,7 @@ void EthernetServer::begin(IPAddress address) struct sockaddr_in *ipv4 = (struct sockaddr_in *)p->ai_addr; void *addr = &(ipv4->sin_addr); inet_ntop(p->ai_family, addr, ipstr, sizeof ipstr); - ETHERNETSERVER_DEBUG("Listening for connections on %s:%s\n", ipstr, portstr); + mys_log(LOG_DEBUG, "Listening for connections on %s:%s\n", ipstr, portstr); } bool EthernetServer::hasClient() @@ -141,7 +142,7 @@ size_t EthernetServer::write(const uint8_t *buffer, size_t size) client.stop(); clients[i] = clients.back(); clients.pop_back(); - ETHERNETSERVER_DEBUG("Client disconnected."); + mys_log(LOG_DEBUG, "Client disconnected."); } } @@ -191,7 +192,7 @@ void EthernetServer::_accept() } } if (no_free_slots) { - ETHERNETSERVER_DEBUG("Max number of ethernet clients reached."); + mys_log(LOG_DEBUG, "Max number of ethernet clients reached."); return; } } @@ -200,7 +201,7 @@ void EthernetServer::_accept() new_fd = accept(sockfd, (struct sockaddr *)&client_addr, &sin_size); if (new_fd == -1) { if (errno != EAGAIN && errno != EWOULDBLOCK) { - perror("accept"); + mys_log(LOG_ERR, "accept: %s\n", strerror(errno)); } return; } @@ -210,5 +211,5 @@ void EthernetServer::_accept() void *addr = &(((struct sockaddr_in*)&client_addr)->sin_addr); inet_ntop(client_addr.ss_family, addr, ipstr, sizeof ipstr); - ETHERNETSERVER_DEBUG("New connection from %s\n", ipstr); + mys_log(LOG_DEBUG, "New connection from %s\n", ipstr); } diff --git a/drivers/Linux/EthernetServer.h b/drivers/Linux/EthernetServer.h index 8bb4c6d..4240e96 100644 --- a/drivers/Linux/EthernetServer.h +++ b/drivers/Linux/EthernetServer.h @@ -34,13 +34,6 @@ #define ETHERNETSERVER_BACKLOG 10 //!< Maximum length to which the queue of pending connections may grow. #endif -// debug -#if defined(ETHERNETSERVER_VERBOSE) - #define ETHERNETSERVER_DEBUG(x,...) printf(x, ##__VA_ARGS__) //!< debug -#else - #define ETHERNETSERVER_DEBUG(x,...) //!< debug NULL -#endif - class EthernetClient; /** diff --git a/drivers/Linux/Print.cpp b/drivers/Linux/Print.cpp index 306b82c..94eb814 100644 --- a/drivers/Linux/Print.cpp +++ b/drivers/Linux/Print.cpp @@ -38,7 +38,11 @@ size_t Print::write(const uint8_t *buffer, size_t size) { return n; } -size_t Print::printf(const char *format, ...) { +size_t +#ifdef __GNUC__ +__attribute__((format(printf, 2, 3))) +#endif +Print::printf(const char *format, ...) { va_list arg; va_start(arg, format); char temp[64]; diff --git a/drivers/Linux/SerialPort.cpp b/drivers/Linux/SerialPort.cpp index 048db46..79b8484 100644 --- a/drivers/Linux/SerialPort.cpp +++ b/drivers/Linux/SerialPort.cpp @@ -28,6 +28,7 @@ #include #include #include +#include "log.h" #include "SerialPort.h" SerialPort::SerialPort(const char *port, bool isPty) : serialPort(std::string(port)), isPty(isPty) @@ -38,7 +39,7 @@ SerialPort::SerialPort(const char *port, bool isPty) : serialPort(std::string(po void SerialPort::begin(int bauds) { if (!open(bauds)) { - fprintf(stderr, "Failed to open serial port.\n"); + mys_log(LOG_ERR, "Failed to open serial port.\n"); exit(1); } } @@ -51,35 +52,29 @@ bool SerialPort::open(int bauds) if (isPty) { sd = posix_openpt(O_RDWR | O_NOCTTY | O_NDELAY); if (sd < 0) { - perror("Couldn't open a PTY"); + mys_log(LOG_ERR, "Couldn't open a PTY: %s\n", strerror(errno)); return false; } if (grantpt(sd) != 0) { - perror("Couldn't grant permission to the PTY"); + mys_log(LOG_ERR, "Couldn't grant permission to the PTY: %s\n", strerror(errno)); return false; } if (unlockpt(sd) != 0) { - perror("Couldn't unlock the PTY"); - return false; - } - - // Get the current options of the port - if (tcgetattr(sd, &options) < 0) { - perror("Couldn't get term attributes"); + mys_log(LOG_ERR, "Couldn't unlock the PTY: %s\n", strerror(errno)); return false; } /* create a symlink with predictable name to the PTY device */ unlink(serialPort.c_str()); // remove the symlink if it already exists if (symlink(ptsname(sd), serialPort.c_str()) != 0) { - fprintf(stderr, "Couldn't create a symlink '%s' to PTY! (%d) %s\n", serialPort.c_str(), errno, strerror(errno)); + mys_log(LOG_ERR, "Couldn't create a symlink '%s' to PTY! (%d) %s\n", serialPort.c_str(), errno, strerror(errno)); return false; } } else { if ((sd = ::open(serialPort.c_str(), O_RDWR | O_NOCTTY | O_NDELAY)) == -1) { - fprintf(stderr, "Unable to open the serial port %s\n", serialPort.c_str()); + mys_log(LOG_ERR, "Unable to open the serial port %s\n", serialPort.c_str()); return false; } @@ -109,7 +104,7 @@ bool SerialPort::open(int bauds) // Get the current options of the port if (tcgetattr(sd, &options) < 0) { - perror("Couldn't get term attributes"); + mys_log(LOG_ERR, "Couldn't get term attributes: %s\n", strerror(errno)); return false; } @@ -132,13 +127,13 @@ bool SerialPort::open(int bauds) // Set parameters if (tcsetattr(sd, TCSANOW, &options) < 0) { - perror("Couldn't set term attributes"); + mys_log(LOG_ERR, "Couldn't set term attributes: %s\n", strerror(errno)); return false; } // flush if (tcflush(sd, TCIOFLUSH) < 0) { - perror("Couldn't flush serial"); + mys_log(LOG_ERR, "Couldn't flush serial: %s\n", strerror(errno)); return false; } @@ -157,7 +152,7 @@ bool SerialPort::setGroupPerm(const char *groupName) if (sd != -1 && groupName != NULL) { devGrp = getgrnam(groupName); if (devGrp == NULL) { - fprintf(stderr, "getgrnam: %s failed. (%d) %s\n", groupName, errno, strerror(errno)); + mys_log(LOG_ERR, "getgrnam: %s failed. (%d) %s\n", groupName, errno, strerror(errno)); return false; } @@ -169,13 +164,13 @@ bool SerialPort::setGroupPerm(const char *groupName) ret = chown(dev, -1, devGrp->gr_gid); if (ret == -1) { - fprintf(stderr, "Could not change PTY owner! (%d) %s\n", errno, strerror(errno)); + mys_log(LOG_ERR, "Could not change PTY owner! (%d) %s\n", errno, strerror(errno)); return false; } ret = chmod(dev, ttyPermissions); if (ret != 0) { - fprintf(stderr, "Could not change PTY permissions! (%d) %s\n", errno, strerror(errno)); + mys_log(LOG_ERR, "Could not change PTY permissions! (%d) %s\n", errno, strerror(errno)); return false; } @@ -189,7 +184,7 @@ int SerialPort::available() int nbytes = 0; if (ioctl(sd, FIONREAD, &nbytes) < 0) { - fprintf(stderr, "Failed to get byte count on serial.\n"); + mys_log(LOG_ERR, "Failed to get byte count on serial.\n"); exit(-1); } return nbytes; diff --git a/drivers/Linux/log.cpp b/drivers/Linux/log.cpp new file mode 100644 index 0000000..b45df4e --- /dev/null +++ b/drivers/Linux/log.cpp @@ -0,0 +1,22 @@ +#include +#include +#include +#include "log.h" + +void mys_log_v(int level, const char *fmt, va_list args) +{ + vsyslog(level, fmt, args); +} + +void +#ifdef __GNUC__ +__attribute__((format(printf, 2, 3))) +#endif +mys_log(int level, const char *fmt, ...) +{ + va_list args; + + va_start(args, fmt); + mys_log_v(level, fmt, args); + va_end(args); +} diff --git a/drivers/Linux/log.h b/drivers/Linux/log.h new file mode 100644 index 0000000..791e4b9 --- /dev/null +++ b/drivers/Linux/log.h @@ -0,0 +1,10 @@ +#ifndef LOG_H +#define LOG_H + +#include +#include + +extern void mys_log_v(int level, const char *fmt, va_list args); +extern void mys_log(int level, const char *fmt, ...) __attribute__((format(printf,2,3))); + +#endif diff --git a/initscripts/mysgateway.sysvinit b/initscripts/mysgateway.sysvinit index 34ee0c4..5ca7e12 100644 --- a/initscripts/mysgateway.sysvinit +++ b/initscripts/mysgateway.sysvinit @@ -10,11 +10,6 @@ # placed in /etc/init.d. ### END INIT INFO -# Author: Foo Bar -# -# Please remove the "Author" lines above and replace them -# with your own name if you copy and modify this script. - # Do NOT "set -e" # PATH should only include /usr/* if it runs after the mountnfs.sh script @@ -22,7 +17,7 @@ PATH=/sbin:/usr/sbin:/bin:/usr/bin:/usr/local/sbin:/usr/local/bin DESC="MySensors Gateway" NAME=mysGateway DAEMON=%gateway_dir%/$NAME -DAEMON_ARGS="-d" +DAEMON_ARGS="-b" PIDFILE=/var/run/$NAME.pid SCRIPTNAME=/etc/init.d/$NAME