diff --git a/MyConfig.h b/MyConfig.h index 3834b5f..84a5b1c 100644 --- a/MyConfig.h +++ b/MyConfig.h @@ -616,7 +616,11 @@ * @brief The Ethernet TCP/UDP port to open on controller or gateway. */ #ifndef MY_PORT -#define MY_PORT 5003 + #ifdef MY_GATEWAY_MQTT_CLIENT + #define MY_PORT 1883 + #else + #define MY_PORT 5003 + #endif #endif // Static ip address of gateway (if this is disabled, DHCP will be used) @@ -718,15 +722,7 @@ * @brief Set the name of predictable tty */ #ifndef MY_LINUX_TTY_NAME -#define MY_LINUX_TTY_NAME "/dev/ttyMySensorsGateway" -#endif - -/** - * @def MY_LINUX_TTY_GROUPNAME - * @brief Set the group name for the raw tty - */ -#ifndef MY_LINUX_TTY_GROUPNAME -#define MY_LINUX_TTY_GROUPNAME tty +#define MY_LINUX_TTY_NAME "/dev/ttyAMA0" #endif /** diff --git a/configure b/configure index 7ad5629..dc3012c 100755 --- a/configure +++ b/configure @@ -267,13 +267,13 @@ for opt do CXXFLAGS="-DMY_PORT=${optarg} $CXXFLAGS" ;; --my-mqtt-client-id=*) - CXXFLAGS="-DMY_MQTT_CLIENT_ID=${optarg} $CXXFLAGS" + CXXFLAGS="-DMY_MQTT_CLIENT_ID=\\\"${optarg}\\\" $CXXFLAGS" ;; --my-mqtt-publish-topic-prefix=*) - CXXFLAGS="-DMY_MQTT_PUBLISH_TOPIC_PREFIX=${optarg} $CXXFLAGS" + CXXFLAGS="-DMY_MQTT_PUBLISH_TOPIC_PREFIX=\\\"${optarg}\\\" $CXXFLAGS" ;; --my-mqtt-subscribe-topic-prefix=*) - CXXFLAGS="-DMY_MQTT_SUBSCRIBE_TOPIC_PREFIX=${optarg} $CXXFLAGS" + CXXFLAGS="-DMY_MQTT_SUBSCRIBE_TOPIC_PREFIX=\\\"${optarg}\\\" $CXXFLAGS" ;; --my-rf24-irq-pin=*) CXXFLAGS="-DMY_RX_MESSAGE_BUFFER_FEATURE -DMY_RF24_IRQ_PIN=${optarg} $CXXFLAGS" @@ -319,7 +319,7 @@ if [[ ${gateway_type} == "ethernet" ]]; then CXXFLAGS="-DMY_GATEWAY_LINUX $CXXFLAGS" elif [[ ${gateway_type} == "mqtt" ]]; then LDFLAGS="-lmosquitto $LDFLAGS" - CXXFLAGS="-DMY_GATEWAY_MQTT_CLIENT $CXXFLAGS" + CXXFLAGS="-DMY_GATEWAY_LINUX -DMY_GATEWAY_MQTT_CLIENT $CXXFLAGS" fi if [[ ${radio_type} == "nrf24" ]]; then diff --git a/core/MyHwLinuxGeneric.cpp b/core/MyHwLinuxGeneric.cpp index e288bd6..ee74731 100644 --- a/core/MyHwLinuxGeneric.cpp +++ b/core/MyHwLinuxGeneric.cpp @@ -19,7 +19,6 @@ #include "MyHwLinuxGeneric.h" -#include #include #include #include @@ -28,60 +27,47 @@ static const char* CONFIG_FILE = MY_LINUX_CONFIG_FILE; static const size_t _length = 1024; // ATMega328 has 1024 bytes -static uint8_t _config[_length]; -static unsigned long millis_at_start; +static uint8_t _config[_length]; bool CheckConfigFile() { struct stat fileInfo; - - if(stat(CONFIG_FILE, &fileInfo) != 0) - { + + if (stat(CONFIG_FILE, &fileInfo) != 0) { //File does not exist. Create it. debug("Config file %s does not exist, creating new config file.\n", CONFIG_FILE); ofstream myFile(CONFIG_FILE, ios::out | ios::binary); - if(!myFile) - { + if (!myFile) { debug("Unable to create config file %s.\n", CONFIG_FILE); return false; } myFile.write((const char*)_config, _length); myFile.close(); - } - else if(fileInfo.st_size < 0 || (size_t)fileInfo.st_size != _length) - { + } else if (fileInfo.st_size < 0 || (size_t)fileInfo.st_size != _length) { debug("Config file %s is not the correct size of %i. Please remove the file and a new one will be created.\n", CONFIG_FILE, _length); return false; - } - else - { + } else { //Read config into local memory. ifstream myFile(CONFIG_FILE, ios::in | ios::binary); - if(!myFile) - { + if (!myFile) { debug("Unable to open config to file %s for reading.\n", CONFIG_FILE); return false; } myFile.read((char*)_config, _length); myFile.close(); } - + return true; } void hwInit() { - timeval curTime; - - for (size_t i = 0; i < _length; i++) + for (size_t i = 0; i < _length; i++) { _config[i] = 0xFF; + } - if (!CheckConfigFile()) - { + if (!CheckConfigFile()) { exit(1); } - - gettimeofday(&curTime, NULL); - millis_at_start = curTime.tv_sec; } void hwReadConfigBlock(void* buf, void* addr, size_t length) @@ -101,8 +87,7 @@ void hwWriteConfigBlock(void* buf, void* addr, size_t length) memcpy(_config+offs, buf, length); ofstream myFile(CONFIG_FILE, ios::out | ios::binary); - if(!myFile) - { + if (!myFile) { debug("Unable to write config to file %s.\n", CONFIG_FILE); return; } @@ -121,21 +106,12 @@ uint8_t hwReadConfig(int adr) void hwWriteConfig(int adr, uint8_t value) { uint8_t curr = hwReadConfig(adr); - if (curr != value) - { + if (curr != value) { hwWriteConfigBlock(&value, reinterpret_cast(adr), 1); } } -unsigned long hwMillis() -{ - timeval curTime; - - gettimeofday(&curTime, NULL); - return ((curTime.tv_sec - millis_at_start) * 1000) + (curTime.tv_usec / 1000); -} - -// TODO: Not supported! +// Not supported! int8_t hwSleep(unsigned long ms) { (void)ms; @@ -143,7 +119,7 @@ int8_t hwSleep(unsigned long ms) return -2; } -// TODO: Not supported! +// Not supported! int8_t hwSleep(uint8_t interrupt, uint8_t mode, unsigned long ms) { (void)interrupt; @@ -153,7 +129,7 @@ int8_t hwSleep(uint8_t interrupt, uint8_t mode, unsigned long ms) return -2; } -// TODO: Not supported! +// Not supported! int8_t hwSleep(uint8_t interrupt1, uint8_t mode1, uint8_t interrupt2, uint8_t mode2, unsigned long ms) { (void)interrupt1; diff --git a/core/MyHwLinuxGeneric.h b/core/MyHwLinuxGeneric.h index c44d912..1d763ce 100644 --- a/core/MyHwLinuxGeneric.h +++ b/core/MyHwLinuxGeneric.h @@ -30,25 +30,25 @@ #define hwDigitalWrite(__pin, __value) #define hwWatchdogReset() #define hwReboot() +#define hwMillis() millis() void hwInit(); void hwReadConfigBlock(void* buf, void* adr, size_t length); void hwWriteConfigBlock(void* buf, void* adr, size_t length); void hwWriteConfig(int adr, uint8_t value); uint8_t hwReadConfig(int adr); -unsigned long hwMillis(); #ifdef MY_RF24_IRQ_PIN -static pthread_mutex_t hw_mutex = PTHREAD_MUTEX_INITIALIZER; + static pthread_mutex_t hw_mutex = PTHREAD_MUTEX_INITIALIZER; -static __inline__ void __hwUnlock(const uint8_t *__s) { - pthread_mutex_unlock(&hw_mutex); - (void)__s; -} + static __inline__ void __hwUnlock(const uint8_t *__s) { + pthread_mutex_unlock(&hw_mutex); + (void)__s; + } -static __inline__ void __hwLock() { - pthread_mutex_lock(&hw_mutex); -} + static __inline__ void __hwLock() { + pthread_mutex_lock(&hw_mutex); + } #endif #if defined(DOXYGEN) diff --git a/drivers/Linux/Arduino.h b/drivers/Linux/Arduino.h index e928b04..08e9701 100644 --- a/drivers/Linux/Arduino.h +++ b/drivers/Linux/Arduino.h @@ -51,6 +51,7 @@ char *ltoa(long value, char* result, int base); char *ultoa(long num, char *str, int radix); char *utoa(int num, char *str, int radix); char *dtostrf(float f, int width, int decimals, char *result); +unsigned long millis(void); void delay(unsigned int millis); #ifdef __cplusplus diff --git a/drivers/Linux/compatibility.cpp b/drivers/Linux/compatibility.cpp index 5c1e5fe..ff76cc5 100644 --- a/drivers/Linux/compatibility.cpp +++ b/drivers/Linux/compatibility.cpp @@ -1,7 +1,11 @@ #include #include +#include #include "Arduino.h" +// For millis() +static unsigned long millis_at_start = 0; + /** * C++ version 0.4 char* style "itoa": * Written by Lukás Chmela @@ -177,6 +181,19 @@ char *dtostrf(float f, int width, int decimals, char *result) return result; } +unsigned long millis(void) +{ + timeval curTime; + + if (millis_at_start == 0) { + gettimeofday(&curTime, NULL); + millis_at_start = curTime.tv_sec; + } + + gettimeofday(&curTime, NULL); + return ((curTime.tv_sec - millis_at_start) * 1000) + (curTime.tv_usec / 1000); +} + void delay(unsigned int millis) { struct timespec sleeper;