Skip to content

Commit 2175c99

Browse files
marceloaqnomfalkvidd
authored andcommitted
Linux: Use config file for gateway settings (#1061)
- The following settings can be use on the config file: - verbose=[debug,info,notice,warn,err] - Logging verbosity. - log_file[0|1] - Enable logging to a file. - log_filepath=(FILE) - Log file path. - log_pipe=[0|1] - Enable logging to a named pipe(aka fifo). Use this option to view your gateway's log messages from the log_pipe_file (defined below). To do so, run the following command on another terminal: - $ cat "log_pipe_file" - log_pipe_file=(FILE) - syslog=[0|1] - Enable logging to syslog. - eeprom_file=[/etc/mysensors.eeprom] - eeprom_size=[1024] - Change some mysgw parameters: - Added: - -q, --quiet: for quiet mode, disable log messages written to the terminal. - Removed: - -d, --debug: removed, log messages are now enabled by default. - Replaced: - -b, --background: replaced by --daemon - isatty() is no longer used, log messages by default are printed to stderr unless the gateway is started with --quiet (#1022) - MY_LINUX_CONFIG_FILE: no longer holds the path to the eeprom file, but to the configuration file
1 parent 06f8d2b commit 2175c99

File tree

14 files changed

+635
-130
lines changed

14 files changed

+635
-130
lines changed

MyConfig.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1873,7 +1873,7 @@
18731873
* @note For now the configuration file is only used to store the emulated eeprom state.
18741874
*/
18751875
#ifndef MY_LINUX_CONFIG_FILE
1876-
#define MY_LINUX_CONFIG_FILE "/etc/mysensors.dat"
1876+
#define MY_LINUX_CONFIG_FILE "/etc/mysensors.conf"
18771877
#endif
18781878
/** @}*/ // End of LinuxSettingGrpPub group
18791879
/** @}*/ // End of PlatformSettingGrpPub group

configure

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ Installation options:
3737
3838
MySensors options:
3939
--my-debug=[enable|disable] Enables or disables MySensors core debugging. [enable]
40-
--my-config-file=<FILE> Config file path. [/etc/mysensors.dat]
40+
--my-config-file=<FILE> Config file path. [/etc/mysensors.conf]
4141
--my-gateway=[none|ethernet|serial|mqtt]
4242
Set the protocol used to communicate with the controller. [ethernet]
4343
--my-node-id=<ID> Disable gateway feature and run as a node with the specified id.

drivers/Linux/SoftEeprom.cpp

Lines changed: 43 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
* network topology allowing messages to be routed to nodes.
77
*
88
* Created by Henrik Ekblad <[email protected]>
9-
* Copyright (C) 2013-2017 Sensnology AB
9+
* Copyright (C) 2013-2018 Sensnology AB
1010
* Full contributor list: https://github.com/mysensors/Arduino/graphs/contributors
1111
*
1212
* Documentation: http://www.mysensors.org
@@ -26,14 +26,36 @@
2626
#include "log.h"
2727
#include "SoftEeprom.h"
2828

29-
SoftEeprom::SoftEeprom(const char *fileName, size_t length)
29+
SoftEeprom::SoftEeprom() : _length(0), _fileName(NULL), _values(NULL)
30+
{
31+
}
32+
33+
SoftEeprom::SoftEeprom(const SoftEeprom& other)
34+
{
35+
_fileName = strdup(other._fileName);
36+
37+
_length = other._length;
38+
_values = new uint8_t[_length];
39+
for (size_t i = 0; i < _length; ++i) {
40+
_values[i] = other._values[i];
41+
}
42+
}
43+
44+
SoftEeprom::~SoftEeprom()
45+
{
46+
destroy();
47+
}
48+
49+
int SoftEeprom::init(const char *fileName, size_t length)
3050
{
3151
struct stat fileInfo;
3252

53+
destroy();
54+
3355
_fileName = strdup(fileName);
3456
if (_fileName == NULL) {
3557
logError("Error: %s\n", strerror(errno));
36-
exit(1);
58+
return -1;
3759
}
3860

3961
_length = length;
@@ -44,53 +66,48 @@ SoftEeprom::SoftEeprom(const char *fileName, size_t length)
4466

4567
if (stat(_fileName, &fileInfo) != 0) {
4668
//File does not exist. Create it.
47-
logInfo("Config file %s does not exist, creating new config file.\n", _fileName);
69+
logInfo("EEPROM file %s does not exist, creating new file.\n", _fileName);
4870
std::ofstream myFile(_fileName, std::ios::out | std::ios::binary);
4971
if (!myFile) {
5072
logError("Unable to create config file %s.\n", _fileName);
51-
exit(1);
73+
return -1;
5274
}
5375
myFile.write((const char*)_values, _length);
5476
myFile.close();
5577
} else if (fileInfo.st_size < 0 || (size_t)fileInfo.st_size != _length) {
56-
logError("Config file %s is not the correct size of %zu. Please remove the file and a new one will be created.\n",
78+
logError("EEPROM file %s is not the correct size of %zu. Please remove the file and a new one will be created.\n",
5779
_fileName, _length);
58-
exit(1);
80+
return -1;
5981
} else {
6082
//Read config into local memory.
6183
std::ifstream myFile(_fileName, std::ios::in | std::ios::binary);
6284
if (!myFile) {
63-
logError("Unable to open config to file %s for reading.\n", _fileName);
64-
exit(1);
85+
logError("Unable to open EEPROM file %s for reading.\n", _fileName);
86+
return -1;
6587
}
6688
myFile.read((char*)_values, _length);
6789
myFile.close();
6890
}
69-
}
70-
71-
SoftEeprom::SoftEeprom(const SoftEeprom& other)
72-
{
73-
_fileName = strdup(other._fileName);
7491

75-
_length = other._length;
76-
_values = new uint8_t[_length];
77-
for (size_t i = 0; i < _length; ++i) {
78-
_values[i] = other._values[i];
79-
}
92+
return 0;
8093
}
8194

82-
SoftEeprom::~SoftEeprom()
95+
void SoftEeprom::destroy()
8396
{
84-
delete[] _values;
85-
free(_fileName);
97+
if (_values) {
98+
delete[] _values;
99+
}
100+
if (_fileName) {
101+
free(_fileName);
102+
}
86103
}
87104

88105
void SoftEeprom::readBlock(void* buf, void* addr, size_t length)
89106
{
90107
static bool config_to_mem = false;
91108
unsigned long int offs = reinterpret_cast<unsigned long int>(addr);
92109

93-
if (!config_to_mem) {
110+
if (!config_to_mem && length) {
94111
//Read config into local memory.
95112
std::ifstream myFile(_fileName, std::ios::in | std::ios::binary);
96113
if (!myFile) {
@@ -144,6 +161,9 @@ void SoftEeprom::writeByte(int addr, uint8_t value)
144161
SoftEeprom& SoftEeprom::operator=(const SoftEeprom& other)
145162
{
146163
if (this != &other) {
164+
delete[] _values;
165+
free(_fileName);
166+
147167
_fileName = strdup(other._fileName);
148168

149169
_length = other._length;

drivers/Linux/SoftEeprom.h

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
* network topology allowing messages to be routed to nodes.
77
*
88
* Created by Henrik Ekblad <[email protected]>
9-
* Copyright (C) 2013-2017 Sensnology AB
9+
* Copyright (C) 2013-2018 Sensnology AB
1010
* Full contributor list: https://github.com/mysensors/Arduino/graphs/contributors
1111
*
1212
* Documentation: http://www.mysensors.org
@@ -37,7 +37,7 @@ class SoftEeprom
3737
/**
3838
* @brief SoftEeprom constructor.
3939
*/
40-
SoftEeprom(const char *fileName, size_t length);
40+
SoftEeprom();
4141
/**
4242
* @brief SoftEeprom copy constructor.
4343
*/
@@ -46,6 +46,19 @@ class SoftEeprom
4646
* @brief SoftEeprom destructor.
4747
*/
4848
~SoftEeprom();
49+
/**
50+
* @brief Initializes the eeprom class.
51+
*
52+
* @param fileName filepath where the data is saved.
53+
* @param length eeprom size in bytes.
54+
* @return 0 if SUCCESS or -1 if FAILURE.
55+
*/
56+
int init(const char *fileName, size_t length);
57+
/**
58+
* @brief Clear all allocated memory variables.
59+
*
60+
*/
61+
void destroy();
4962
/**
5063
* @brief Read a block of bytes from eeprom.
5164
*

0 commit comments

Comments
 (0)