Add run time options to RPi gateway

This commit is contained in:
Marcelo Aquino
2016-09-08 15:56:37 -03:00
parent b4f4e28545
commit 09103e9c04
13 changed files with 178 additions and 77 deletions
+7 -6
View File
@@ -29,6 +29,7 @@
#include <sys/time.h>
#include <netinet/tcp.h>
#include <errno.h>
#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;
-7
View File
@@ -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
*/
+12 -11
View File
@@ -28,6 +28,7 @@
#include <unistd.h>
#include <errno.h>
#include <fcntl.h>
#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);
}
-7
View File
@@ -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;
/**
+5 -1
View File
@@ -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];
+14 -19
View File
@@ -28,6 +28,7 @@
#include <grp.h>
#include <errno.h>
#include <sys/stat.h>
#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;
+22
View File
@@ -0,0 +1,22 @@
#include <stdio.h>
#include <syslog.h>
#include <stdarg.h>
#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);
}
+10
View File
@@ -0,0 +1,10 @@
#ifndef LOG_H
#define LOG_H
#include <stdarg.h>
#include <syslog.h>
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