2018-03-20 13:31:11 +00:00
|
|
|
/*
|
|
|
|
xdrv_08_serial_bridge.ino - serial bridge support for Sonoff-Tasmota
|
|
|
|
|
2019-01-01 12:55:01 +00:00
|
|
|
Copyright (C) 2019 Theo Arends and Dániel Zoltán Tolnai
|
2018-03-20 13:31:11 +00:00
|
|
|
|
|
|
|
This program is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifdef USE_SERIAL_BRIDGE
|
|
|
|
/*********************************************************************************************\
|
|
|
|
* Serial Bridge using Software Serial library (TasmotaSerial)
|
|
|
|
\*********************************************************************************************/
|
2018-11-07 09:30:03 +00:00
|
|
|
|
|
|
|
#define XDRV_08 8
|
|
|
|
|
2018-03-20 13:31:11 +00:00
|
|
|
#define SERIAL_BRIDGE_BUFFER_SIZE 130
|
|
|
|
|
|
|
|
#include <TasmotaSerial.h>
|
|
|
|
|
|
|
|
enum SerialBridgeCommands { CMND_SSERIALSEND, CMND_SBAUDRATE };
|
|
|
|
const char kSerialBridgeCommands[] PROGMEM = D_CMND_SSERIALSEND "|" D_CMND_SBAUDRATE;
|
|
|
|
|
|
|
|
TasmotaSerial *SerialBridgeSerial;
|
|
|
|
|
|
|
|
uint8_t serial_bridge_active = 1;
|
|
|
|
uint8_t serial_bridge_in_byte_counter = 0;
|
|
|
|
unsigned long serial_bridge_polling_window = 0;
|
2018-11-27 11:09:36 +00:00
|
|
|
char *serial_bridge_buffer = NULL;
|
2018-03-20 13:31:11 +00:00
|
|
|
|
2018-11-14 13:32:09 +00:00
|
|
|
void SerialBridgeInput(void)
|
2018-03-20 13:31:11 +00:00
|
|
|
{
|
|
|
|
while (SerialBridgeSerial->available()) {
|
|
|
|
yield();
|
|
|
|
uint8_t serial_in_byte = SerialBridgeSerial->read();
|
|
|
|
|
|
|
|
if (serial_in_byte > 127) { // binary data...
|
|
|
|
serial_bridge_in_byte_counter = 0;
|
|
|
|
SerialBridgeSerial->flush();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (serial_in_byte) {
|
2018-11-27 11:18:27 +00:00
|
|
|
if ((serial_in_byte_counter < SERIAL_BRIDGE_BUFFER_SIZE -1) && (serial_in_byte != Settings.serial_delimiter)) { // add char to string if it still fits
|
2018-03-20 13:31:11 +00:00
|
|
|
serial_bridge_buffer[serial_bridge_in_byte_counter++] = serial_in_byte;
|
|
|
|
serial_bridge_polling_window = millis(); // Wait for more data
|
|
|
|
} else {
|
|
|
|
serial_bridge_polling_window = 0; // Publish now
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (serial_bridge_in_byte_counter && (millis() > (serial_bridge_polling_window + SERIAL_POLLING))) {
|
|
|
|
serial_bridge_buffer[serial_bridge_in_byte_counter] = 0; // serial data completed
|
|
|
|
snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("{\"" D_JSON_SSERIALRECEIVED "\":\"%s\"}"), serial_bridge_buffer);
|
|
|
|
MqttPublishPrefixTopic_P(RESULT_OR_TELE, PSTR(D_JSON_SSERIALRECEIVED));
|
2018-05-24 15:23:20 +01:00
|
|
|
// XdrvRulesProcess();
|
2018-03-20 13:31:11 +00:00
|
|
|
serial_bridge_in_byte_counter = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/********************************************************************************************/
|
|
|
|
|
|
|
|
void SerialBridgeInit(void)
|
|
|
|
{
|
|
|
|
serial_bridge_active = 0;
|
|
|
|
if ((pin[GPIO_SBR_RX] < 99) && (pin[GPIO_SBR_TX] < 99)) {
|
2018-11-27 11:09:36 +00:00
|
|
|
serial_bridge_buffer = (char*)(malloc(SERIAL_BRIDGE_BUFFER_SIZE));
|
|
|
|
if (serial_bridge_buffer != NULL) {
|
|
|
|
SerialBridgeSerial = new TasmotaSerial(pin[GPIO_SBR_RX], pin[GPIO_SBR_TX]);
|
|
|
|
if (SerialBridgeSerial->begin(Settings.sbaudrate * 1200)) { // Baud rate is stored div 1200 so it fits into one byte
|
|
|
|
serial_bridge_active = 1;
|
|
|
|
SerialBridgeSerial->flush();
|
|
|
|
}
|
2018-03-20 13:31:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*********************************************************************************************\
|
|
|
|
* Commands
|
|
|
|
\*********************************************************************************************/
|
|
|
|
|
2018-11-14 13:32:09 +00:00
|
|
|
boolean SerialBridgeCommand(void)
|
2018-03-20 13:31:11 +00:00
|
|
|
{
|
|
|
|
char command [CMDSZ];
|
|
|
|
boolean serviced = true;
|
|
|
|
|
|
|
|
int command_code = GetCommandCode(command, sizeof(command), XdrvMailbox.topic, kSerialBridgeCommands);
|
2018-05-09 09:49:43 +01:00
|
|
|
if (-1 == command_code) {
|
|
|
|
serviced = false; // Unknown command
|
|
|
|
}
|
|
|
|
else if ((CMND_SSERIALSEND == command_code) && (XdrvMailbox.index > 0) && (XdrvMailbox.index <= 3)) {
|
2018-03-20 13:31:11 +00:00
|
|
|
if (XdrvMailbox.data_len > 0) {
|
|
|
|
if (1 == XdrvMailbox.index) {
|
|
|
|
SerialBridgeSerial->write(XdrvMailbox.data, XdrvMailbox.data_len);
|
|
|
|
SerialBridgeSerial->write("\n");
|
|
|
|
}
|
|
|
|
else if (2 == XdrvMailbox.index) {
|
|
|
|
SerialBridgeSerial->write(XdrvMailbox.data, XdrvMailbox.data_len);
|
|
|
|
}
|
|
|
|
else if (3 == XdrvMailbox.index) {
|
|
|
|
SerialBridgeSerial->write(Unescape(XdrvMailbox.data, &XdrvMailbox.data_len), XdrvMailbox.data_len);
|
|
|
|
}
|
|
|
|
snprintf_P(mqtt_data, sizeof(mqtt_data), S_JSON_COMMAND_SVALUE, command, D_JSON_DONE);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (CMND_SBAUDRATE == command_code) {
|
|
|
|
char *p;
|
|
|
|
int baud = strtol(XdrvMailbox.data, &p, 10);
|
|
|
|
if (baud > 0) {
|
|
|
|
baud /= 1200; // Make it a valid baudrate
|
|
|
|
Settings.sbaudrate = (1 == XdrvMailbox.payload) ? SOFT_BAUDRATE / 1200 : baud;
|
|
|
|
SerialBridgeSerial->begin(Settings.sbaudrate * 1200); // Reinitialize serial port with new baud rate
|
|
|
|
}
|
2018-12-24 17:14:25 +00:00
|
|
|
snprintf_P(mqtt_data, sizeof(mqtt_data), S_JSON_COMMAND_NVALUE, command, Settings.sbaudrate * 1200);
|
2018-03-20 13:31:11 +00:00
|
|
|
}
|
2018-05-09 09:49:43 +01:00
|
|
|
else serviced = false; // Unknown command
|
2018-11-27 11:09:36 +00:00
|
|
|
|
2018-03-20 13:31:11 +00:00
|
|
|
return serviced;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*********************************************************************************************\
|
|
|
|
* Interface
|
|
|
|
\*********************************************************************************************/
|
|
|
|
|
|
|
|
boolean Xdrv08(byte function)
|
|
|
|
{
|
|
|
|
boolean result = false;
|
|
|
|
|
|
|
|
if (serial_bridge_active) {
|
|
|
|
switch (function) {
|
2018-06-04 17:10:38 +01:00
|
|
|
case FUNC_PRE_INIT:
|
2018-03-20 13:31:11 +00:00
|
|
|
SerialBridgeInit();
|
|
|
|
break;
|
|
|
|
case FUNC_LOOP:
|
|
|
|
SerialBridgeInput();
|
|
|
|
break;
|
|
|
|
case FUNC_COMMAND:
|
|
|
|
result = SerialBridgeCommand();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif // USE_SERIAL_BRIDGE
|