From f0c5a993d56ce9cfd679e10840b9051c94b6e4b0 Mon Sep 17 00:00:00 2001 From: Marcelo Aquino Date: Mon, 22 Aug 2016 21:26:44 -0300 Subject: [PATCH] Fix Rpi config file corruption * Add uninstall to Makefile --- Makefile | 24 +++++++++++++++++++++++- MyConfig.h | 2 +- configure | 4 ++-- core/MyHwLinuxGeneric.cpp | 5 +++-- examples_linux/mysGateway.cpp | 2 +- 5 files changed, 30 insertions(+), 7 deletions(-) diff --git a/Makefile b/Makefile index 05067d1..1596919 100644 --- a/Makefile +++ b/Makefile @@ -11,7 +11,8 @@ CONFIG_FILE=Makefile.inc include $(CONFIG_FILE) -GATEWAY=examples_linux/mysGateway +GATEWAY_BIN=mysGateway +GATEWAY=examples_linux/$(GATEWAY_BIN) GATEWAY_SOURCES=$(wildcard drivers/Linux/*.cpp) examples_linux/mysGateway.cpp GATEWAY_OBJECTS=$(patsubst %.cpp,%.o,$(GATEWAY_SOURCES)) DEPS+=$(patsubst %.cpp,%.d,$(GATEWAY_SOURCES)) @@ -61,7 +62,28 @@ ifeq ($(INIT_SYSTEM), systemd) install -m0644 initscripts/mysgateway.systemd /etc/systemd/system/mysgateway.service @sed -i -e "s|%gateway_dir%|${GATEWAY_DIR}|g" /etc/systemd/system/mysgateway.service systemctl daemon-reload + @echo "MySensors gateway has been installed, to add to the boot run:" + @echo " sudo systemctl enable mysgateway.service" + @echo "To start the gateway run:" + @echo " sudo systemctl start mysgateway.service" else ifeq ($(INIT_SYSTEM), sysvinit) install -m0755 initscripts/mysgateway.sysvinit /etc/init.d/mysgateway @sed -i -e "s|%gateway_dir%|${GATEWAY_DIR}|g" /etc/init.d/mysgateway + @echo "MySensors gateway has been installed, to add to the boot run:" + @echo " sudo update-rc.d mysgateway defaults" + @echo "To start the gateway run:" + @echo " sudo service mysgateway start" +endif + +uninstall: +ifeq ($(INIT_SYSTEM), systemd) + @echo "Stopping daemon mysgateway (ignore errors)" + -@systemctl stop mysgateway.service + @echo "removing files" + rm /etc/systemd/system/mysgateway.service $(GATEWAY_DIR)/$(GATEWAY_BIN) +else ifeq ($(INIT_SYSTEM), sysvinit) + @echo "Stopping daemon mysgateway (ignore errors)" + -@service mysgateway stop + @echo "removing files" + rm /etc/init.d/mysgateway $(GATEWAY_DIR)/$(GATEWAY_BIN) endif diff --git a/MyConfig.h b/MyConfig.h index b8e0517..785beda 100644 --- a/MyConfig.h +++ b/MyConfig.h @@ -732,7 +732,7 @@ * For now the configuration file is only used to store the emulated eeprom state */ #ifndef MY_LINUX_CONFIG_FILE - #define MY_LINUX_CONFIG_FILE "/etc/MySensorGateway.cfg" + #define MY_LINUX_CONFIG_FILE "/etc/mysensors.dat" #endif // Doxygen specific constructs, not included when built normally diff --git a/configure b/configure index 8df6e97..242b236 100755 --- a/configure +++ b/configure @@ -35,7 +35,7 @@ MySensors options: --my-debug=[enable|disable] Enables or disables debugging. Default to enable. - --my-config-file= Config file path. [/etc/MySensorGateway.cfg] + --my-config-file= Config file path. [/etc/mysensors.dat] --my-gateway=[none|ethernet|serial|mqtt] Gateway type, set to none to disable gateway feature. Default to ethernet. @@ -248,7 +248,7 @@ for opt do gateway_type=${optarg} ;; --my-config-file=*) - CXXFLAGS="-DMY_CONFIG_FILE=${optarg} $CXXFLAGS" + CXXFLAGS="-DMY_LINUX_CONFIG_FILE=\\\"${optarg}\\\" $CXXFLAGS" ;; --my-radio=*) radio_type=${optarg} diff --git a/core/MyHwLinuxGeneric.cpp b/core/MyHwLinuxGeneric.cpp index bf48457..b116555 100644 --- a/core/MyHwLinuxGeneric.cpp +++ b/core/MyHwLinuxGeneric.cpp @@ -90,12 +90,13 @@ void hwWriteConfigBlock(void* buf, void* addr, size_t length) if (length && offs + length <= _length) { memcpy(_config+offs, buf, length); - std::ofstream myFile(CONFIG_FILE, std::ios::out | std::ios::binary); + std::ofstream myFile(CONFIG_FILE, std::ios::out | std::ios::in | std::ios::binary); if (!myFile) { debug("Unable to write config to file %s.\n", CONFIG_FILE); return; } - myFile.write((const char*)buf+offs, _length); + myFile.seekp(offs); + myFile.write((const char*)buf, length); myFile.close(); } } diff --git a/examples_linux/mysGateway.cpp b/examples_linux/mysGateway.cpp index b53ffcc..baec194 100644 --- a/examples_linux/mysGateway.cpp +++ b/examples_linux/mysGateway.cpp @@ -24,7 +24,7 @@ // For more options run ./configure --help // Config file -//#define MY_LINUX_CONFIG_FILE "/etc/MySensorGateway.cfg" +//#define MY_LINUX_CONFIG_FILE "/etc/mysensors.dat" // How many clients should be able to connect to this gateway (default 1) #define MY_GATEWAY_MAX_CLIENTS 10