Add run time options to RPi gateway
This commit is contained in:
@@ -28,7 +28,7 @@ Installation options:
|
|||||||
|
|
||||||
MySensors options:
|
MySensors options:
|
||||||
--my-debug=[enable|disable]
|
--my-debug=[enable|disable]
|
||||||
Enables or disables debugging. [enable]
|
Enables or disables MySensors core debugging. [enable]
|
||||||
--my-config-file=<FILE> Config file path. [/etc/mysensors.dat]
|
--my-config-file=<FILE> Config file path. [/etc/mysensors.dat]
|
||||||
--my-gateway=[none|ethernet|serial|mqtt]
|
--my-gateway=[none|ethernet|serial|mqtt]
|
||||||
Gateway type, set to none to disable gateway feature. [ethernet]
|
Gateway type, set to none to disable gateway feature. [ethernet]
|
||||||
|
|||||||
@@ -17,7 +17,6 @@
|
|||||||
* version 2 as published by the Free Software Foundation.
|
* version 2 as published by the Free Software Foundation.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
|
||||||
#include "MyConfig.h"
|
#include "MyConfig.h"
|
||||||
#include "MyProtocol.h"
|
#include "MyProtocol.h"
|
||||||
#include "MyGatewayTransport.h"
|
#include "MyGatewayTransport.h"
|
||||||
@@ -31,7 +30,6 @@ char _serialInputString[MY_GATEWAY_MAX_RECEIVE_LENGTH]; // A buffer for incom
|
|||||||
int _serialInputPos;
|
int _serialInputPos;
|
||||||
MyMessage _serialMsg;
|
MyMessage _serialMsg;
|
||||||
|
|
||||||
|
|
||||||
bool gatewayTransportSend(MyMessage &message) {
|
bool gatewayTransportSend(MyMessage &message) {
|
||||||
setIndication(INDICATION_GW_TX);
|
setIndication(INDICATION_GW_TX);
|
||||||
MY_SERIALDEVICE.print(protocolFormat(message));
|
MY_SERIALDEVICE.print(protocolFormat(message));
|
||||||
@@ -47,7 +45,6 @@ bool gatewayTransportInit() {
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
bool gatewayTransportAvailable() {
|
bool gatewayTransportAvailable() {
|
||||||
while (MY_SERIALDEVICE.available()) {
|
while (MY_SERIALDEVICE.available()) {
|
||||||
// get the new byte:
|
// get the new byte:
|
||||||
|
|||||||
@@ -24,12 +24,14 @@
|
|||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <fstream>
|
#include <fstream>
|
||||||
#include <sys/stat.h>
|
#include <sys/stat.h>
|
||||||
|
#include "log.h"
|
||||||
|
|
||||||
static const char* CONFIG_FILE = MY_LINUX_CONFIG_FILE;
|
static const char* CONFIG_FILE = MY_LINUX_CONFIG_FILE;
|
||||||
static const size_t _length = 1024; // ATMega328 has 1024 bytes
|
static const size_t _length = 1024; // ATMega328 has 1024 bytes
|
||||||
static uint8_t _config[_length];
|
static uint8_t _config[_length];
|
||||||
|
|
||||||
bool CheckConfigFile() {
|
bool CheckConfigFile()
|
||||||
|
{
|
||||||
struct stat fileInfo;
|
struct stat fileInfo;
|
||||||
|
|
||||||
if (stat(CONFIG_FILE, &fileInfo) != 0) {
|
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;
|
return -2;
|
||||||
}
|
}
|
||||||
|
|
||||||
uint16_t hwCPUVoltage() {
|
uint16_t hwCPUVoltage()
|
||||||
|
{
|
||||||
// TODO: Not supported!
|
// TODO: Not supported!
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
uint16_t hwCPUFrequency() {
|
uint16_t hwCPUFrequency()
|
||||||
|
{
|
||||||
// TODO: Not supported!
|
// TODO: Not supported!
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
uint16_t hwFreeMem() {
|
uint16_t hwFreeMem()
|
||||||
|
{
|
||||||
// TODO: Not supported!
|
// TODO: Not supported!
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
#ifdef MY_DEBUG
|
#ifdef MY_DEBUG
|
||||||
void hwDebugPrint(const char *fmt, ... )
|
void hwDebugPrint(const char *fmt, ...)
|
||||||
{
|
{
|
||||||
va_list arglist;
|
va_list args;
|
||||||
va_start(arglist, fmt);
|
|
||||||
vprintf(fmt, arglist);
|
va_start(args, fmt);
|
||||||
va_end(arglist);
|
mys_log_v(LOG_DEBUG, fmt, args);
|
||||||
|
va_end(args);
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
+91
-7
@@ -1,19 +1,23 @@
|
|||||||
// Initialize library and handle sketch functions like we want to
|
// Initialize library and handle sketch functions like we want to
|
||||||
|
|
||||||
#include <iostream>
|
#include <stdio.h>
|
||||||
#include <csignal>
|
#include <csignal>
|
||||||
#include <cstdlib>
|
#include <cstdlib>
|
||||||
|
#include <unistd.h>
|
||||||
|
#include <fcntl.h>
|
||||||
|
#include <syslog.h>
|
||||||
|
#include <errno.h>
|
||||||
|
#include "log.h"
|
||||||
#include "MySensorsCore.h"
|
#include "MySensorsCore.h"
|
||||||
|
|
||||||
/*
|
|
||||||
* handler for SIGINT signal
|
|
||||||
*/
|
|
||||||
void handle_sigint(int sig)
|
void handle_sigint(int sig)
|
||||||
{
|
{
|
||||||
if (sig == SIGINT) {
|
if (sig == SIGINT) {
|
||||||
std::cout << "Received SIGINT\n" << std::endl;
|
mys_log(LOG_NOTICE, "Received SIGINT\n\n");
|
||||||
} else if (sig == SIGTERM) {
|
} 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
|
#ifdef MY_RF24_IRQ_PIN
|
||||||
@@ -27,11 +31,91 @@ void handle_sigint(int sig)
|
|||||||
exit(0);
|
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 */
|
/* register the signal handler */
|
||||||
signal(SIGINT, handle_sigint);
|
signal(SIGINT, handle_sigint);
|
||||||
signal(SIGTERM, 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
|
_begin(); // Startup MySensors library
|
||||||
|
|
||||||
for (;;) {
|
for (;;) {
|
||||||
|
|||||||
@@ -29,6 +29,7 @@
|
|||||||
#include <sys/time.h>
|
#include <sys/time.h>
|
||||||
#include <netinet/tcp.h>
|
#include <netinet/tcp.h>
|
||||||
#include <errno.h>
|
#include <errno.h>
|
||||||
|
#include "log.h"
|
||||||
#include "EthernetClient.h"
|
#include "EthernetClient.h"
|
||||||
|
|
||||||
EthernetClient::EthernetClient() : _sock(-1) {
|
EthernetClient::EthernetClient() : _sock(-1) {
|
||||||
@@ -50,7 +51,7 @@ int EthernetClient::connect(const char* host, uint16_t port) {
|
|||||||
|
|
||||||
sprintf(port_str, "%hu", port);
|
sprintf(port_str, "%hu", port);
|
||||||
if ((rv = getaddrinfo(host, port_str, &hints, &servinfo)) != 0) {
|
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;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -58,13 +59,13 @@ int EthernetClient::connect(const char* host, uint16_t port) {
|
|||||||
for (p = servinfo; p != NULL; p = p->ai_next) {
|
for (p = servinfo; p != NULL; p = p->ai_next) {
|
||||||
if ((sockfd = socket(p->ai_family, p->ai_socktype,
|
if ((sockfd = socket(p->ai_family, p->ai_socktype,
|
||||||
p->ai_protocol)) == -1) {
|
p->ai_protocol)) == -1) {
|
||||||
perror("socket");
|
mys_log(LOG_ERR, "socket: %s\n", strerror(errno));
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (::connect(sockfd, p->ai_addr, p->ai_addrlen) == -1) {
|
if (::connect(sockfd, p->ai_addr, p->ai_addrlen) == -1) {
|
||||||
close(sockfd);
|
close(sockfd);
|
||||||
perror("connect");
|
mys_log(LOG_ERR, "connect: %s\n", strerror(errno));
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -72,7 +73,7 @@ int EthernetClient::connect(const char* host, uint16_t port) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (p == NULL) {
|
if (p == NULL) {
|
||||||
fprintf(stderr, "failed to connect\n");
|
mys_log(LOG_ERR, "failed to connect\n");
|
||||||
return -1;
|
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);
|
void *addr = &(((struct sockaddr_in*)p->ai_addr)->sin_addr);
|
||||||
inet_ntop(p->ai_family, addr, s, sizeof s);
|
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
|
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) {
|
while (size > 0) {
|
||||||
rc = send(_sock, buf + bytes, size, MSG_NOSIGNAL | MSG_DONTWAIT);
|
rc = send(_sock, buf + bytes, size, MSG_NOSIGNAL | MSG_DONTWAIT);
|
||||||
if (rc == -1) {
|
if (rc == -1) {
|
||||||
perror("send");
|
mys_log(LOG_ERR, "send: %s\n", strerror(errno));
|
||||||
close(_sock);
|
close(_sock);
|
||||||
_sock = -1;
|
_sock = -1;
|
||||||
break;
|
break;
|
||||||
|
|||||||
@@ -37,13 +37,6 @@
|
|||||||
#define ETHERNETCLIENT_W5100_CLOSE_WAIT 0x1C
|
#define ETHERNETCLIENT_W5100_CLOSE_WAIT 0x1C
|
||||||
#define ETHERNETCLIENT_W5100_LAST_ACK 0x1D
|
#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
|
* EthernetClient class
|
||||||
*/
|
*/
|
||||||
|
|||||||
@@ -28,6 +28,7 @@
|
|||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
#include <errno.h>
|
#include <errno.h>
|
||||||
#include <fcntl.h>
|
#include <fcntl.h>
|
||||||
|
#include "log.h"
|
||||||
#include "EthernetClient.h"
|
#include "EthernetClient.h"
|
||||||
#include "EthernetServer.h"
|
#include "EthernetServer.h"
|
||||||
|
|
||||||
@@ -56,26 +57,26 @@ void EthernetServer::begin(IPAddress address)
|
|||||||
|
|
||||||
sprintf(portstr, "%d", port);
|
sprintf(portstr, "%d", port);
|
||||||
if ((rv = getaddrinfo(address.toString().c_str(), portstr, &hints, &servinfo)) != 0) {
|
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;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// loop through all the results and bind to the first we can
|
// loop through all the results and bind to the first we can
|
||||||
for (p = servinfo; p != NULL; p = p->ai_next) {
|
for (p = servinfo; p != NULL; p = p->ai_next) {
|
||||||
if ((sockfd = socket(p->ai_family, p->ai_socktype, p->ai_protocol)) == -1) {
|
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;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(int)) == -1) {
|
if (setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(int)) == -1) {
|
||||||
perror("setsockopt");
|
mys_log(LOG_ERR, "setsockopt: %s\n", strerror(errno));
|
||||||
freeaddrinfo(servinfo);
|
freeaddrinfo(servinfo);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (bind(sockfd, p->ai_addr, p->ai_addrlen) == -1) {
|
if (bind(sockfd, p->ai_addr, p->ai_addrlen) == -1) {
|
||||||
close(sockfd);
|
close(sockfd);
|
||||||
perror("bind");
|
mys_log(LOG_ERR, "bind: %s\n", strerror(errno));
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -83,13 +84,13 @@ void EthernetServer::begin(IPAddress address)
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (p == NULL) {
|
if (p == NULL) {
|
||||||
fprintf(stderr, "failed to bind\n");
|
mys_log(LOG_ERR, "failed to bind\n");
|
||||||
freeaddrinfo(servinfo);
|
freeaddrinfo(servinfo);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (listen(sockfd, ETHERNETSERVER_BACKLOG) == -1) {
|
if (listen(sockfd, ETHERNETSERVER_BACKLOG) == -1) {
|
||||||
perror("listen");
|
mys_log(LOG_ERR, "listen: %s\n", strerror(errno));
|
||||||
freeaddrinfo(servinfo);
|
freeaddrinfo(servinfo);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@@ -101,7 +102,7 @@ void EthernetServer::begin(IPAddress address)
|
|||||||
struct sockaddr_in *ipv4 = (struct sockaddr_in *)p->ai_addr;
|
struct sockaddr_in *ipv4 = (struct sockaddr_in *)p->ai_addr;
|
||||||
void *addr = &(ipv4->sin_addr);
|
void *addr = &(ipv4->sin_addr);
|
||||||
inet_ntop(p->ai_family, addr, ipstr, sizeof ipstr);
|
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()
|
bool EthernetServer::hasClient()
|
||||||
@@ -141,7 +142,7 @@ size_t EthernetServer::write(const uint8_t *buffer, size_t size)
|
|||||||
client.stop();
|
client.stop();
|
||||||
clients[i] = clients.back();
|
clients[i] = clients.back();
|
||||||
clients.pop_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) {
|
if (no_free_slots) {
|
||||||
ETHERNETSERVER_DEBUG("Max number of ethernet clients reached.");
|
mys_log(LOG_DEBUG, "Max number of ethernet clients reached.");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -200,7 +201,7 @@ void EthernetServer::_accept()
|
|||||||
new_fd = accept(sockfd, (struct sockaddr *)&client_addr, &sin_size);
|
new_fd = accept(sockfd, (struct sockaddr *)&client_addr, &sin_size);
|
||||||
if (new_fd == -1) {
|
if (new_fd == -1) {
|
||||||
if (errno != EAGAIN && errno != EWOULDBLOCK) {
|
if (errno != EAGAIN && errno != EWOULDBLOCK) {
|
||||||
perror("accept");
|
mys_log(LOG_ERR, "accept: %s\n", strerror(errno));
|
||||||
}
|
}
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@@ -210,5 +211,5 @@ void EthernetServer::_accept()
|
|||||||
|
|
||||||
void *addr = &(((struct sockaddr_in*)&client_addr)->sin_addr);
|
void *addr = &(((struct sockaddr_in*)&client_addr)->sin_addr);
|
||||||
inet_ntop(client_addr.ss_family, addr, ipstr, sizeof ipstr);
|
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);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -34,13 +34,6 @@
|
|||||||
#define ETHERNETSERVER_BACKLOG 10 //!< Maximum length to which the queue of pending connections may grow.
|
#define ETHERNETSERVER_BACKLOG 10 //!< Maximum length to which the queue of pending connections may grow.
|
||||||
#endif
|
#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;
|
class EthernetClient;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -38,7 +38,11 @@ size_t Print::write(const uint8_t *buffer, size_t size) {
|
|||||||
return n;
|
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_list arg;
|
||||||
va_start(arg, format);
|
va_start(arg, format);
|
||||||
char temp[64];
|
char temp[64];
|
||||||
|
|||||||
@@ -28,6 +28,7 @@
|
|||||||
#include <grp.h>
|
#include <grp.h>
|
||||||
#include <errno.h>
|
#include <errno.h>
|
||||||
#include <sys/stat.h>
|
#include <sys/stat.h>
|
||||||
|
#include "log.h"
|
||||||
#include "SerialPort.h"
|
#include "SerialPort.h"
|
||||||
|
|
||||||
SerialPort::SerialPort(const char *port, bool isPty) : serialPort(std::string(port)), isPty(isPty)
|
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)
|
void SerialPort::begin(int bauds)
|
||||||
{
|
{
|
||||||
if (!open(bauds)) {
|
if (!open(bauds)) {
|
||||||
fprintf(stderr, "Failed to open serial port.\n");
|
mys_log(LOG_ERR, "Failed to open serial port.\n");
|
||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -51,35 +52,29 @@ bool SerialPort::open(int bauds)
|
|||||||
if (isPty) {
|
if (isPty) {
|
||||||
sd = posix_openpt(O_RDWR | O_NOCTTY | O_NDELAY);
|
sd = posix_openpt(O_RDWR | O_NOCTTY | O_NDELAY);
|
||||||
if (sd < 0) {
|
if (sd < 0) {
|
||||||
perror("Couldn't open a PTY");
|
mys_log(LOG_ERR, "Couldn't open a PTY: %s\n", strerror(errno));
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (grantpt(sd) != 0) {
|
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;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (unlockpt(sd) != 0) {
|
if (unlockpt(sd) != 0) {
|
||||||
perror("Couldn't unlock the PTY");
|
mys_log(LOG_ERR, "Couldn't unlock the PTY: %s\n", strerror(errno));
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Get the current options of the port
|
|
||||||
if (tcgetattr(sd, &options) < 0) {
|
|
||||||
perror("Couldn't get term attributes");
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* create a symlink with predictable name to the PTY device */
|
/* create a symlink with predictable name to the PTY device */
|
||||||
unlink(serialPort.c_str()); // remove the symlink if it already exists
|
unlink(serialPort.c_str()); // remove the symlink if it already exists
|
||||||
if (symlink(ptsname(sd), serialPort.c_str()) != 0) {
|
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;
|
return false;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
if ((sd = ::open(serialPort.c_str(), O_RDWR | O_NOCTTY | O_NDELAY)) == -1) {
|
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;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -109,7 +104,7 @@ bool SerialPort::open(int bauds)
|
|||||||
|
|
||||||
// Get the current options of the port
|
// Get the current options of the port
|
||||||
if (tcgetattr(sd, &options) < 0) {
|
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;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -132,13 +127,13 @@ bool SerialPort::open(int bauds)
|
|||||||
|
|
||||||
// Set parameters
|
// Set parameters
|
||||||
if (tcsetattr(sd, TCSANOW, &options) < 0) {
|
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;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
// flush
|
// flush
|
||||||
if (tcflush(sd, TCIOFLUSH) < 0) {
|
if (tcflush(sd, TCIOFLUSH) < 0) {
|
||||||
perror("Couldn't flush serial");
|
mys_log(LOG_ERR, "Couldn't flush serial: %s\n", strerror(errno));
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -157,7 +152,7 @@ bool SerialPort::setGroupPerm(const char *groupName)
|
|||||||
if (sd != -1 && groupName != NULL) {
|
if (sd != -1 && groupName != NULL) {
|
||||||
devGrp = getgrnam(groupName);
|
devGrp = getgrnam(groupName);
|
||||||
if (devGrp == NULL) {
|
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;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -169,13 +164,13 @@ bool SerialPort::setGroupPerm(const char *groupName)
|
|||||||
|
|
||||||
ret = chown(dev, -1, devGrp->gr_gid);
|
ret = chown(dev, -1, devGrp->gr_gid);
|
||||||
if (ret == -1) {
|
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;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
ret = chmod(dev, ttyPermissions);
|
ret = chmod(dev, ttyPermissions);
|
||||||
if (ret != 0) {
|
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;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -189,7 +184,7 @@ int SerialPort::available()
|
|||||||
int nbytes = 0;
|
int nbytes = 0;
|
||||||
|
|
||||||
if (ioctl(sd, FIONREAD, &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);
|
exit(-1);
|
||||||
}
|
}
|
||||||
return nbytes;
|
return nbytes;
|
||||||
|
|||||||
@@ -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);
|
||||||
|
}
|
||||||
@@ -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
|
||||||
@@ -10,11 +10,6 @@
|
|||||||
# placed in /etc/init.d.
|
# placed in /etc/init.d.
|
||||||
### END INIT INFO
|
### END INIT INFO
|
||||||
|
|
||||||
# Author: Foo Bar <foobar@baz.org>
|
|
||||||
#
|
|
||||||
# Please remove the "Author" lines above and replace them
|
|
||||||
# with your own name if you copy and modify this script.
|
|
||||||
|
|
||||||
# Do NOT "set -e"
|
# Do NOT "set -e"
|
||||||
|
|
||||||
# PATH should only include /usr/* if it runs after the mountnfs.sh script
|
# 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"
|
DESC="MySensors Gateway"
|
||||||
NAME=mysGateway
|
NAME=mysGateway
|
||||||
DAEMON=%gateway_dir%/$NAME
|
DAEMON=%gateway_dir%/$NAME
|
||||||
DAEMON_ARGS="-d"
|
DAEMON_ARGS="-b"
|
||||||
PIDFILE=/var/run/$NAME.pid
|
PIDFILE=/var/run/$NAME.pid
|
||||||
SCRIPTNAME=/etc/init.d/$NAME
|
SCRIPTNAME=/etc/init.d/$NAME
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user