Skip to content

Commit

Permalink
Code maintenance (#1264)
Browse files Browse the repository at this point in the history
  • Loading branch information
tekka007 committed Feb 3, 2019
1 parent 1aa20cd commit 767a447
Show file tree
Hide file tree
Showing 19 changed files with 314 additions and 313 deletions.
2 changes: 1 addition & 1 deletion .ci/arduino.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ def buildSTM32F1(config, sketches, String key) {
}

def buildESP8266(config, sketches, String key) {
def fqbn = '-fqbn=esp8266:esp8266:generic:CpuFrequency=80,VTable=flash,ResetMethod=ck,CrystalFreq=26,FlashFreq=40,FlashMode=qio,FlashSize=512K0,led=2,LwIPVariant=v2mss536,Debug=Disabled,DebugLevel=None____,FlashErase=none,UploadSpeed=115200'
def fqbn = '-fqbn=esp8266:esp8266:generic:xtal=80,vt=flash,exception=disabled,ResetMethod=ck,CrystalFreq=26,FlashFreq=40,FlashMode=dout,eesz=512K,led=2,ip=lm2f,dbg=Disabled,lvl=None____,wipe=none,baud=115200'
config.pr.setBuildStatus(config, 'PENDING', 'Toll gate (ESP8266 - '+key+')', 'Building...', '${BUILD_URL}flowGraphTable/')
try {
for (sketch = 0; sketch < sketches.size(); sketch++) {
Expand Down
6 changes: 6 additions & 0 deletions MyConfig.h
Original file line number Diff line number Diff line change
Expand Up @@ -2144,6 +2144,12 @@
*/
#define ARDUINO_ARCH_ESP8266

/**
* @def ARDUINO_ARCH_ESP32
* @brief Automatically set when building for ESP32 targets
*/
#define ARDUINO_ARCH_ESP32

/**
* @def ARDUINO_ARCH_AVR
* @brief Automatically set when building for AVR targets
Expand Down
36 changes: 20 additions & 16 deletions MySensors.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@
#endif

#include "MyConfig.h"
#include "core/MyHelperFunctions.cpp"

#include "core/MySplashScreen.h"
#include "core/MySensorsCore.h"

Expand Down Expand Up @@ -68,8 +70,6 @@
#include "hal/architecture/STM32F1/MyHwSTM32F1.cpp"
#include "hal/crypto/generic/MyCryptoGeneric.cpp"
#elif defined(ARDUINO_ARCH_NRF5) || defined(ARDUINO_ARCH_NRF52)
#include "drivers/NVM/VirtualPage.cpp"
#include "drivers/NVM/NVRAM.cpp"
#include "hal/architecture/NRF5/MyHwNRF5.cpp"
#include "hal/crypto/generic/MyCryptoGeneric.cpp"
#elif defined(__arm__) && defined(TEENSYDUINO)
Expand Down Expand Up @@ -171,6 +171,22 @@ MY_DEFAULT_RX_LED_PIN in your sketch instead to enable LEDs
#error You must specify MY_CONTROLLER_IP_ADDRESS or MY_CONTROLLER_URL_ADDRESS for UDP
#endif



// Set MQTT defaults if not set

#if !defined(MY_MQTT_PUBLISH_TOPIC_PREFIX)
#define MY_MQTT_PUBLISH_TOPIC_PREFIX "mygateway1-out"
#endif

#if !defined(MY_MQTT_SUBSCRIBE_TOPIC_PREFIX)
#define MY_MQTT_SUBSCRIBE_TOPIC_PREFIX "mygateway1-in"
#endif

#if !defined(MY_MQTT_CLIENT_ID)
#define MY_MQTT_CLIENT_ID "mysensors-1"
#endif

#if defined(MY_GATEWAY_MQTT_CLIENT)
#if defined(MY_SENSOR_NETWORK)
// We assume that a gateway having a radio also should act as repeater
Expand All @@ -188,20 +204,8 @@ MY_DEFAULT_RX_LED_PIN in your sketch instead to enable LEDs
#error MY_GATEWAY_TINYGSM only works with MY_GATEWAY_MQTT_CLIENT
#endif

#if !defined(MY_MQTT_PUBLISH_TOPIC_PREFIX)
#error You must specify a topic publish prefix MY_MQTT_PUBLISH_TOPIC_PREFIX for this MQTT client
#endif

#if !defined(MY_MQTT_SUBSCRIBE_TOPIC_PREFIX)
#error You must specify a topic subscribe prefix MY_MQTT_SUBSCRIBE_TOPIC_PREFIX for this MQTT client
#endif

#if !defined(MY_MQTT_CLIENT_ID)
#error You must define a unique MY_MQTT_CLIENT_ID for this MQTT client
#endif

#include "core/MyGatewayTransport.cpp"
#include "core/MyProtocolMySensors.cpp"
#include "core/MyProtocol.cpp"

#if defined(MY_GATEWAY_TINYGSM)
#include "drivers/TinyGSM/TinyGsmClient.h"
Expand All @@ -217,7 +221,7 @@ MY_DEFAULT_RX_LED_PIN in your sketch instead to enable LEDs
#elif defined(MY_GATEWAY_FEATURE)
// GATEWAY - COMMON FUNCTIONS
#include "core/MyGatewayTransport.cpp"
#include "core/MyProtocolMySensors.cpp"
#include "core/MyProtocol.cpp"

// GATEWAY - CONFIGURATION
#if defined(MY_SENSOR_NETWORK)
Expand Down
9 changes: 4 additions & 5 deletions core/MyGatewayTransportEthernet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,7 @@ bool gatewayTransportInit(void)
bool gatewayTransportSend(MyMessage &message)
{
int nbytes = 0;
char *_ethernetMsg = protocolFormat(message);
char *_ethernetMsg = protocolMyMessage2Serial(message);

setIndication(INDICATION_GW_TX);

Expand Down Expand Up @@ -316,10 +316,9 @@ bool _readFromClient(uint8_t i)
inputString[i].string[inputString[i].idx] = 0;
GATEWAY_DEBUG(PSTR("GWT:RFC:C=%" PRIu8 ",MSG=%s\n"), i, inputString[i].string);
inputString[i].idx = 0;
if (protocolParse(_ethernetMsg, inputString[i].string)) {
if (protocolSerial2MyMessage(_ethernetMsg, inputString[i].string)) {
return true;
}

} else {
// add it to the inputString:
inputString[i].string[inputString[i].idx++] = inChar;
Expand All @@ -346,7 +345,7 @@ bool _readFromClient(void)
inputString.string[inputString.idx] = 0;
GATEWAY_DEBUG(PSTR("GWT:RFC:MSG=%s\n"), inputString.string);
inputString.idx = 0;
if (protocolParse(_ethernetMsg, inputString.string)) {
if (protocolSerial2MyMessage(_ethernetMsg, inputString.string)) {
return true;
}

Expand Down Expand Up @@ -384,7 +383,7 @@ bool gatewayTransportAvailable(void)
inputString.string[packet_size] = 0;
GATEWAY_DEBUG(PSTR("GWT:TSA:UDP MSG=%s\n"), inputString.string);
_w5100_spi_en(false);
const bool ok = protocolParse(_ethernetMsg, inputString.string);
const bool ok = protocolSerial2MyMessage(_ethernetMsg, inputString.string);
if (ok) {
setIndication(INDICATION_GW_RX);
}
Expand Down
4 changes: 2 additions & 2 deletions core/MyGatewayTransportMQTTClient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ bool gatewayTransportSend(MyMessage &message)
return false;
}
setIndication(INDICATION_GW_TX);
char *topic = protocolFormatMQTTTopic(MY_MQTT_PUBLISH_TOPIC_PREFIX, message);
char *topic = protocolMyMessage2MQTT(MY_MQTT_PUBLISH_TOPIC_PREFIX, message);
GATEWAY_DEBUG(PSTR("GWT:TPS:TOPIC=%s,MSG SENT\n"), topic);
#if defined(MY_MQTT_CLIENT_PUBLISH_RETAIN)
const bool retain = mGetCommand(message) == C_SET ||
Expand All @@ -130,7 +130,7 @@ bool gatewayTransportSend(MyMessage &message)
void incomingMQTT(char *topic, uint8_t *payload, unsigned int length)
{
GATEWAY_DEBUG(PSTR("GWT:IMQ:TOPIC=%s, MSG RECEIVED\n"), topic);
_MQTT_available = protocolMQTTParse(_MQTT_msg, topic, payload, length);
_MQTT_available = protocolMQTT2MyMessage(_MQTT_msg, topic, payload, length);
setIndication(INDICATION_GW_RX);
}

Expand Down
4 changes: 2 additions & 2 deletions core/MyGatewayTransportSerial.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ MyMessage _serialMsg;
bool gatewayTransportSend(MyMessage &message)
{
setIndication(INDICATION_GW_TX);
MY_SERIALDEVICE.print(protocolFormat(message));
MY_SERIALDEVICE.print(protocolMyMessage2Serial(message));
// Serial print is always successful
return true;
}
Expand All @@ -56,7 +56,7 @@ bool gatewayTransportAvailable(void)
if (_serialInputPos < MY_GATEWAY_MAX_RECEIVE_LENGTH - 1) {
if (inChar == '\n') {
_serialInputString[_serialInputPos] = 0;
const bool ok = protocolParse(_serialMsg, _serialInputString);
const bool ok = protocolSerial2MyMessage(_serialMsg, _serialInputString);
if (ok) {
setIndication(INDICATION_GW_RX);
}
Expand Down
41 changes: 41 additions & 0 deletions core/MyHelperFunctions.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
* The MySensors Arduino library handles the wireless radio link and protocol
* between your home built sensors/actuators and HA controller of choice.
* The sensors forms a self healing radio network with optional repeaters. Each
* repeater and gateway builds a routing tables in EEPROM which keeps track of the
* network topology allowing messages to be routed to nodes.
*
* Created by Henrik Ekblad <henrik.ekblad@mysensors.org>
* Copyright (C) 2013-2019 Sensnology AB
* Full contributor list: https://github.com/mysensors/MySensors/graphs/contributors
*
* Documentation: http://www.mysensors.org
* Support Forum: http://forum.mysensors.org
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* version 2 as published by the Free Software Foundation.
*/

#include "MyHelperFunctions.h"

static uint8_t convertH2I(const char c)
{
if (c <= '9') {
return c - '0';
} else if (c >= 'a') {
return c - 'a' + 10;
} else {
return c - 'A' + 10;
}
}

static char convertI2H(const uint8_t i)
{
const uint8_t k = i & 0x0F;
if (k <= 9) {
return '0' + k;
} else {
return 'A' + k - 10;
}
}
38 changes: 38 additions & 0 deletions core/MyHelperFunctions.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
* The MySensors Arduino library handles the wireless radio link and protocol
* between your home built sensors/actuators and HA controller of choice.
* The sensors forms a self healing radio network with optional repeaters. Each
* repeater and gateway builds a routing tables in EEPROM which keeps track of the
* network topology allowing messages to be routed to nodes.
*
* Created by Henrik Ekblad <henrik.ekblad@mysensors.org>
* Copyright (C) 2013-2019 Sensnology AB
* Full contributor list: https://github.com/mysensors/MySensors/graphs/contributors
*
* Documentation: http://www.mysensors.org
* Support Forum: http://forum.mysensors.org
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* version 2 as published by the Free Software Foundation.
*/

#ifndef MyHelperFunctions_h
#define MyHelperFunctions_h

/**
* Single character hex conversion
* @param c hex char
* @return byte representation of the paramter
*/
static uint8_t convertH2I(const char c) __attribute__((unused));

/**
* Lower nibble byte to hex conversion
* @param i byte
* @return hex char representation of the parameter
*/
static char convertI2H(const uint8_t i) __attribute__((unused));


#endif
48 changes: 19 additions & 29 deletions core/MyMessage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@


#include "MyMessage.h"
#include "MyHelperFunctions.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
Expand All @@ -39,12 +40,12 @@ void MyMessage::clear(void)
{
last = 0u;
sender = 0u;
destination = 0u; // Gateway is default destination
destination = 0u; // Gateway is default destination
version_length = 0u;
command_ack_payload = 0u;
type = 0u;
sensor = 0u;
(void)memset(data, 0u, sizeof(data));
(void)memset((void *)data, 0u, sizeof(data));

// set message protocol version
miSetVersion(PROTOCOL_VERSION);
Expand Down Expand Up @@ -76,21 +77,11 @@ const char* MyMessage::getString(void) const
}
}

char MyMessage::i2h(const uint8_t i) const
{
uint8_t k = i & 0x0F;
if (k <= 9) {
return '0' + k;
} else {
return 'A' + k - 10;
}
}

char* MyMessage::getCustomString(char *buffer) const
{
for (uint8_t i = 0; i < miGetLength(); i++) {
buffer[i * 2] = i2h(data[i] >> 4);
buffer[(i * 2) + 1] = i2h(data[i]);
buffer[i * 2] = convertI2H(data[i] >> 4);
buffer[(i * 2) + 1] = convertI2H(data[i]);
}
buffer[miGetLength() * 2] = '\0';
return buffer;
Expand Down Expand Up @@ -204,7 +195,6 @@ uint16_t MyMessage::getUInt(void) const
} else {
return 0;
}

}

MyMessage& MyMessage::setType(const uint8_t _type)
Expand All @@ -226,40 +216,40 @@ MyMessage& MyMessage::setDestination(const uint8_t _destination)
}

// Set payload
MyMessage& MyMessage::set(const void* value, const uint8_t length)
MyMessage& MyMessage::set(const void* value, const size_t length)
{
uint8_t payloadLength = value == NULL ? 0 : min(length, (uint8_t)MAX_PAYLOAD);
const size_t payloadLength = value == NULL ? 0 : min(length, (size_t)MAX_PAYLOAD);
miSetLength(payloadLength);
miSetPayloadType(P_CUSTOM);
memcpy(data, value, payloadLength);
(void)memcpy(data, value, payloadLength);
return *this;
}

MyMessage& MyMessage::set(const char* value)
{
uint8_t length = value == NULL ? 0 : min(strlen(value), (size_t)MAX_PAYLOAD);
miSetLength(length);
const size_t payloadLength = value == NULL ? 0 : min(strlen(value), (size_t)MAX_PAYLOAD);
miSetLength(payloadLength);
miSetPayloadType(P_STRING);
if (length) {
strncpy(data, value, length);
if (payloadLength) {
(void)strncpy(data, value, payloadLength);
}
// null terminate string
data[length] = 0;
data[payloadLength] = 0;
return *this;
}

#if !defined(__linux__)
MyMessage& MyMessage::set(const __FlashStringHelper* value)
{
uint8_t length = value == NULL ? 0
: min(strlen_P(reinterpret_cast<const char *>(value)), (size_t)MAX_PAYLOAD);
miSetLength(length);
const size_t payloadLength = value == NULL ? 0
: min(strlen_P(reinterpret_cast<const char *>(value)), (size_t)MAX_PAYLOAD);
miSetLength(payloadLength);
miSetPayloadType(P_STRING);
if (length) {
strncpy_P(data, reinterpret_cast<const char *>(value), length);
if (payloadLength) {
(void)strncpy_P(data, reinterpret_cast<const char *>(value), payloadLength);
}
// null terminate string
data[length] = 0;
data[payloadLength] = 0;
return *this;
}
#endif
Expand Down
9 changes: 1 addition & 8 deletions core/MyMessage.h
Original file line number Diff line number Diff line change
Expand Up @@ -301,13 +301,6 @@ class MyMessage
*/
MyMessage(const uint8_t sensor, const uint8_t type);

/**
* Single character hex (0 - 15) conversion
* @param i byte (only lower 4 bits will be considered)
* @return single char with the hex representation (0 to F) of the parameter
*/
char i2h(const uint8_t i) const;

/**
* @brief Clear message contents.
*/
Expand Down Expand Up @@ -415,7 +408,7 @@ class MyMessage
* @param payload pointer to the buffer where the payload is stored
* @param length of the payload
*/
MyMessage& set(const void* payload, const uint8_t length);
MyMessage& set(const void* payload, const size_t length);

/**
* @brief Set payload to character array
Expand Down
Loading

0 comments on commit 767a447

Please sign in to comment.