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
-3
View File
@@ -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:
+15 -9
View File
@@ -24,12 +24,14 @@
#include <iostream>
#include <fstream>
#include <sys/stat.h>
#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
+91 -7
View File
@@ -1,19 +1,23 @@
// Initialize library and handle sketch functions like we want to
#include <iostream>
#include <stdio.h>
#include <csignal>
#include <cstdlib>
#include <unistd.h>
#include <fcntl.h>
#include <syslog.h>
#include <errno.h>
#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 (;;) {