Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rpi servicename #1421

Draft
wants to merge 4 commits into
base: development
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 17 additions & 15 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ include $(CONFIG_FILE)
CPPFLAGS+=-Ofast -g -Wall -Wextra
DEPFLAGS=-MT $@ -MMD -MP

GATEWAY_BIN=mysgw
GATEWAY_BIN=$(SERVICE_NAME)
GATEWAY=$(BINDIR)/$(GATEWAY_BIN)
GATEWAY_C_SOURCES=$(wildcard hal/architecture/Linux/drivers/core/*.c)
GATEWAY_CPP_SOURCES=$(wildcard hal/architecture/Linux/drivers/core/*.cpp) examples_linux/mysgw.cpp
Expand Down Expand Up @@ -124,31 +124,33 @@ install-gateway:

install-initscripts:
ifeq ($(INIT_SYSTEM), systemd)
install -m0644 initscripts/mysgw.systemd ${DESTDIR}/etc/systemd/system/mysgw.service
@sed -i -e "s|%gateway_dir%|${GATEWAY_DIR}|g" ${DESTDIR}/etc/systemd/system/mysgw.service
install -m0644 initscripts/mysgw.systemd ${DESTDIR}/etc/systemd/system/${SERVICE_NAME}.service
@sed -i -e "s|%gateway_dir%|${GATEWAY_DIR}|g" ${DESTDIR}/etc/systemd/system/${SERVICE_NAME}.service
@sed -i -e "s|%mysgw%|${SERVICE_NAME}|g" ${DESTDIR}/etc/systemd/system/${SERVICE_NAME}.service
systemctl daemon-reload
@echo "MySensors gateway has been installed, to add to the boot run:"
@echo " sudo systemctl enable mysgw.service"
@echo " sudo systemctl enable ${SERVICE_NAME}.service"
@echo "To start the gateway run:"
@echo " sudo systemctl start mysgw.service"
@echo " sudo systemctl start ${SERVICE_NAME}.service"
else ifeq ($(INIT_SYSTEM), sysvinit)
install -m0755 initscripts/mysgw.sysvinit ${DESTDIR}/etc/init.d/mysgw
@sed -i -e "s|%gateway_dir%|${GATEWAY_DIR}|g" ${DESTDIR}/etc/init.d/mysgw
install -m0755 initscripts/mysgw.sysvinit ${DESTDIR}/etc/init.d/${SERVICE_NAME}
@sed -i -e "s|%gateway_dir%|${GATEWAY_DIR}|g" ${DESTDIR}/etc/init.d/${SERVICE_NAME}
@sed -i -e "s|%mysgw%|${SERVICE_NAME}|g" ${DESTDIR}/etc/init.d/${SERVICE_NAME}
@echo "MySensors gateway has been installed, to add to the boot run:"
@echo " sudo update-rc.d mysgw defaults"
@echo " sudo update-rc.d ${SERVICE_NAME} defaults"
@echo "To start the gateway run:"
@echo " sudo service mysgw start"
@echo " sudo service ${SERVICE_NAME} start"
endif

uninstall:
ifeq ($(INIT_SYSTEM), systemd)
@echo "Stopping daemon mysgw (ignore errors)"
-@systemctl stop mysgw.service
@echo "Stopping daemon ${SERVICE_NAME} (ignore errors)"
-@systemctl stop ${SERVICE_NAME}.service
@echo "removing files"
rm /etc/systemd/system/mysgw.service $(GATEWAY_DIR)/$(GATEWAY_BIN)
rm /etc/systemd/system/${SERVICE_NAME}.service $(GATEWAY_DIR)/$(GATEWAY_BIN)
else ifeq ($(INIT_SYSTEM), sysvinit)
@echo "Stopping daemon mysgw (ignore errors)"
-@service mysgw stop
@echo "Stopping daemon ${SERVICE_NAME} (ignore errors)"
-@service ${SERVICE_NAME} stop
@echo "removing files"
rm /etc/init.d/mysgw $(GATEWAY_DIR)/$(GATEWAY_BIN)
rm /etc/init.d/${SERVICE_NAME} $(GATEWAY_DIR)/$(GATEWAY_BIN)
endif
10 changes: 8 additions & 2 deletions configure
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
# Original work: https://github.com/TMRh20/RF24/blob/master/configure

function help {
cat <<EOF
cat <<EOF
configure script for MySensors gateway.
Options:

Expand Down Expand Up @@ -34,6 +34,7 @@ Building options:
Installation options:
--prefix=<PREFIX> Installation prefix path. [/usr/local]
--gateway-dir=<DIR> Gateway files installation directory. [PREFIX/bin]
--service-name=<NAME> Name for the binary and systemd service. [mysgw]

MySensors options:
--my-debug=[enable|disable] Enables or disables MySensors core debugging. [enable]
Expand Down Expand Up @@ -297,7 +298,7 @@ signing=none
signing_request_signatures=false
encryption=false

params="SOC CFLAGS CXXFLAGS CPPFLAGS LDFLAGS PREFIX CC CXX ARDUINO_LIB_DIR BUILDDIR BINDIR GATEWAY_DIR INIT_SYSTEM SPI_DRIVER"
params="SOC CFLAGS CXXFLAGS CPPFLAGS LDFLAGS PREFIX CC CXX ARDUINO_LIB_DIR BUILDDIR BINDIR GATEWAY_DIR INIT_SYSTEM SPI_DRIVER SERVICE_NAME"

for opt do
if [ "$opt" = "-h" ] || [ "$opt" = "--help" ]; then
Expand Down Expand Up @@ -345,6 +346,10 @@ for opt do
--bin-dir=*)
BINDIR="$optarg"
;;
--service-name=*)
SERVICE_NAME="$optarg"
CPPFLAGS="-DSERVICENAME=\"${optarg}\" $CPPFLAGS"
;;
--no-clean*)
NO_CLEAN="1"
;;
Expand Down Expand Up @@ -547,6 +552,7 @@ PREFIX=${PREFIX:-/usr/local}
BUILDDIR=${BUILDDIR:-build}
BINDIR=${BINDIR:-bin}
GATEWAY_DIR=${GATEWAY_DIR:-${PREFIX}/bin}
SERVICE_NAME=${SERVICE_NAME:-mysgw}
CC=${CC:-gcc}
CXX=${CXX:-g++}
CXXFLAGS="$CXXFLAGS -std=c++11"
Expand Down
2 changes: 2 additions & 0 deletions hal/architecture/Linux/MyHwLinuxGeneric.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ bool hwInit(void)
exit(1);
}

logInfo("Using eeprom file %s\n", conf.eeprom_file);

return true;
}

Expand Down
2 changes: 1 addition & 1 deletion hal/architecture/Linux/drivers/core/EthernetServer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ void EthernetServer::begin(IPAddress address)
}

if (p == NULL) {
logError("Failed to bind!\n");
logError("Failed to bind to port %d! Is another instance running?\n", port);
freeaddrinfo(servinfo);
return;
}
Expand Down
7 changes: 5 additions & 2 deletions hal/architecture/Linux/drivers/core/config.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@
#include <string.h>
#include <unistd.h>
#include <sys/stat.h>
#include <sys/file.h>
#include "log.h"
#include <errno.h>

static int _config_create(const char *config_file);
static int _config_parse_int(char *token, const char *name, int *value);
Expand All @@ -36,6 +38,7 @@ int config_parse(const char *config_file)
FILE *fptr;
char buf[1024];
struct stat fileInfo;
logInfo("Using config file %s\n", config_file);

if (stat(config_file, &fileInfo) != 0) {
//File does not exist. Create it.
Expand All @@ -45,7 +48,7 @@ int config_parse(const char *config_file)

fptr = fopen(config_file, "rt");
if (!fptr) {
logError("Error opening config file \"%s\".\n", config_file);
logError("Error opening config file \"%s\": %s\n", config_file, strerror(errno));
return -1;
}

Expand Down Expand Up @@ -260,7 +263,7 @@ int _config_create(const char *config_file)

myFile = fopen(config_file, "w");
if (!myFile) {
logError("Unable to create config file %s.\n", config_file);
logError("Unable to create config file %s: %s\n", config_file, strerror(errno));
return -1;
}
ret = fputs(default_conf, myFile);
Expand Down
16 changes: 13 additions & 3 deletions hal/architecture/Linux/drivers/core/log.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,13 @@
#include <time.h>
#include <errno.h>

#ifndef SERVICENAME
#define SERVICENAME "mysgw"
#endif

#define STR_HELPER(x) #x //!< Helper macro, STR_HELPER()
#define STR(x) STR_HELPER(x) //!< Helper macro, STR()

static const char *_log_level_colors[] = {
"\x1b[1;5;91m", "\x1b[1;91m", "\x1b[91m", "\x1b[31m", "\x1b[33m", "\x1b[34m", "\x1b[32m", "\x1b[36m"
};
Expand Down Expand Up @@ -80,6 +87,7 @@ int logSetPipe(char *pipe_file)
_log_pipe = 1;
}

logInfo("Using log pipe %s\n", _log_pipe_file);
return ret;
}

Expand All @@ -94,6 +102,7 @@ int logSetFile(char *file)
return errno;
}

logInfo("Using log file %s\n", file);
return 0;
}

Expand Down Expand Up @@ -138,18 +147,19 @@ void vlog(int level, const char *fmt, va_list args)
date[strftime(date, sizeof(date), "%b %d %H:%M:%S", lt)] = '\0';

if (_log_file_fp != NULL) {
fprintf(_log_file_fp, "%s %-5s ", date, _log_level_names[level]);
fprintf(_log_file_fp, "%s %s %-5s ", date, STR(SERVICENAME), _log_level_names[level]);
vfprintf(_log_file_fp, fmt, args);
fflush(_log_file_fp);
}

if (!_log_quiet) {
#ifdef LOG_DISABLE_COLOR
(void)_log_level_colors;
fprintf(stderr, "%s %-5s ", date, _log_level_names[level]);
fprintf(stderr, "%s %s %-5s ", date, STR(SERVICENAME) _log_level_names[level]);
vfprintf(stderr, fmt, args);
#else
fprintf(stderr, "%s %s%-5s\x1b[0m ", date, _log_level_colors[level], _log_level_names[level]);
fprintf(stderr, "%s %s %s%-5s\x1b[0m ", date, STR(SERVICENAME), _log_level_colors[level],
_log_level_names[level]);
vfprintf(stderr, fmt, args);
#endif
}
Expand Down
2 changes: 1 addition & 1 deletion initscripts/mysgw.systemd
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ Description=MySensors Gateway daemon
Requires=network.target

[Service]
ExecStart=%gateway_dir%/mysgw -q
ExecStart=%gateway_dir%/%mysgw% -q

[Install]
WantedBy=multi-user.target
2 changes: 1 addition & 1 deletion initscripts/mysgw.sysvinit
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
# PATH should only include /usr/* if it runs after the mountnfs.sh script
PATH=/sbin:/usr/sbin:/bin:/usr/bin:/usr/local/sbin:/usr/local/bin
DESC="MySensors Gateway"
NAME=mysgw
NAME=%mysgw%
DAEMON=%gateway_dir%/$NAME
DAEMON_ARGS="--daemon -q"
PIDFILE=/var/run/$NAME.pid
Expand Down