Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion main/actuatorSomfy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ void setupSomfy() {
void XtoSomfy(const char* topicOri, JsonObject& jsonData) {
if (cmpToMainTopic(topicOri, subjectMQTTtoSomfy)) {
THEENGS_LOG_TRACE(F("MQTTtoSomfy json data analysis" CR));
float txFrequency = jsonData["frequency"] | RFConfig.frequency;
float txFrequency = jsonData["frequency"] | iRFConfig.getFrequency();
# ifdef ZradioCC1101 // set Receive off and Transmitt on
disableCurrentReceiver();
ELECHOUSE_cc1101.SetTx(txFrequency);
Expand Down
190 changes: 39 additions & 151 deletions main/commonRF.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@
# ifdef ZradioCC1101
# include <ELECHOUSE_CC1101_SRC_DRV.h>
# endif
# include <rf/RFConfiguration.h>

# include "TheengsCommon.h"
# include "config_RF.h"

Expand All @@ -37,10 +39,28 @@
extern rtl_433_ESP rtl_433;
# endif

RFConfig_s RFConfig;
void RFConfig_init();
void RFConfig_load();

int currentReceiver = ACTIVE_NONE;
extern void enableActiveReceiver();
extern void disableCurrentReceiver();

// Note: this is currently just a simple wrapper used to make everything work.
// It prevents introducing external dependencies on newly added C++ structures,
// and acts as a first approach to mask the concrete implementations (rf, rf2,
// pilight, etc.). Later this can be extended or replaced by more complete driver
// abstractions without changing the rest of the system.
class ZCommonRFWrapper : public RFReceiver {
public:
ZCommonRFWrapper() : RFReceiver() {}
void enable() override { enableActiveReceiver(); }
void disable() override { disableCurrentReceiver(); }

int getReceiverID() const override { return currentReceiver; }
};

ZCommonRFWrapper iRFReceiver;
RFConfiguration iRFConfig(iRFReceiver);

//TODO review
void initCC1101() {
# ifdef ZradioCC1101 //receiving with CC1101
// Loop on getCC1101() until it returns true and break after 10 attempts
Expand All @@ -54,7 +74,7 @@ void initCC1101() {
if (ELECHOUSE_cc1101.getCC1101()) {
THEENGS_LOG_NOTICE(F("C1101 spi Connection OK" CR));
ELECHOUSE_cc1101.Init();
ELECHOUSE_cc1101.SetRx(RFConfig.frequency);
ELECHOUSE_cc1101.SetRx(iRFConfig.getFrequency());
break;
} else {
THEENGS_LOG_ERROR(F("C1101 spi Connection Error" CR));
Expand All @@ -68,23 +88,10 @@ void initCC1101() {
}

void setupCommonRF() {
RFConfig_init();
RFConfig_load();
}

bool validFrequency(float mhz) {
// CC1101 valid frequencies 300-348 MHZ, 387-464MHZ and 779-928MHZ.
if (mhz >= 300 && mhz <= 348)
return true;
if (mhz >= 387 && mhz <= 464)
return true;
if (mhz >= 779 && mhz <= 928)
return true;
return false;
iRFConfig.reInit();
iRFConfig.loadFromStorage();
}

int currentReceiver = ACTIVE_NONE;

# if !defined(ZgatewayRFM69) && !defined(ZactuatorSomfy)
// Check if a receiver is available
bool validReceiver(int receiver) {
Expand Down Expand Up @@ -138,13 +145,13 @@ void disableCurrentReceiver() {
break;
# endif
default:
THEENGS_LOG_ERROR(F("ERROR: unsupported receiver %d" CR), RFConfig.activeReceiver);
THEENGS_LOG_ERROR(F("ERROR: unsupported receiver %d" CR), iRFConfig.getActiveReceiver());
}
}

void enableActiveReceiver() {
THEENGS_LOG_TRACE(F("enableActiveReceiver: %d" CR), RFConfig.activeReceiver);
switch (RFConfig.activeReceiver) {
THEENGS_LOG_TRACE(F("enableActiveReceiver: %d" CR), iRFConfig.getActiveReceiver());
switch (iRFConfig.getActiveReceiver()) {
# ifdef ZgatewayPilight
case ACTIVE_PILIGHT:
initCC1101();
Expand All @@ -155,7 +162,7 @@ void enableActiveReceiver() {
# ifdef ZgatewayRF
case ACTIVE_RF:
initCC1101();
enableRFReceive(RFConfig.frequency, RF_RECEIVER_GPIO, RF_EMITTER_GPIO);
enableRFReceive(iRFConfig.getFrequency(), RF_RECEIVER_GPIO, RF_EMITTER_GPIO);
currentReceiver = ACTIVE_RF;
break;
# endif
Expand All @@ -177,18 +184,21 @@ void enableActiveReceiver() {
THEENGS_LOG_ERROR(F("ERROR: no receiver selected" CR));
break;
default:
THEENGS_LOG_ERROR(F("ERROR: unsupported receiver %d" CR), RFConfig.activeReceiver);
THEENGS_LOG_ERROR(F("ERROR: unsupported receiver %d" CR), iRFConfig.getActiveReceiver());
}
}

String stateRFMeasures() {
//Publish RTL_433 state
StaticJsonDocument<JSON_MSG_BUFFER> jsonBuffer;
JsonObject RFdata = jsonBuffer.to<JsonObject>();
RFdata["active"] = RFConfig.activeReceiver;

// load the configuration
iRFConfig.toJson(RFdata);

// load the current state
# if defined(ZradioCC1101) || defined(ZradioSX127x)
RFdata["frequency"] = RFConfig.frequency;
if (RFConfig.activeReceiver == ACTIVE_RTL) {
if (iRFConfig.getActiveReceiver() == ACTIVE_RTL) {
# ifdef ZgatewayRTL_433
RFdata["rssithreshold"] = (int)getRTLrssiThreshold();
RFdata["rssi"] = (int)getRTLCurrentRSSI();
Expand All @@ -211,134 +221,12 @@ String stateRFMeasures() {
return output;
}

void RFConfig_fromJson(JsonObject& RFdata) {
bool success = false;
if (RFdata.containsKey("frequency") && validFrequency(RFdata["frequency"])) {
Config_update(RFdata, "frequency", RFConfig.frequency);
THEENGS_LOG_NOTICE(F("RF Receive mhz: %F" CR), RFConfig.frequency);
success = true;
}
if (RFdata.containsKey("active")) {
THEENGS_LOG_NOTICE(F("RF receiver active: %d" CR), RFConfig.activeReceiver);
Config_update(RFdata, "active", RFConfig.activeReceiver);
success = true;
}
# ifdef ZgatewayRTL_433
if (RFdata.containsKey("rssithreshold")) {
THEENGS_LOG_NOTICE(F("RTL_433 RSSI Threshold : %d " CR), RFConfig.rssiThreshold);
Config_update(RFdata, "rssithreshold", RFConfig.rssiThreshold);
rtl_433.setRSSIThreshold(RFConfig.rssiThreshold);
success = true;
}
# if defined(RF_SX1276) || defined(RF_SX1278)
if (RFdata.containsKey("ookthreshold")) {
Config_update(RFdata, "ookthreshold", RFConfig.newOokThreshold);
THEENGS_LOG_NOTICE(F("RTL_433 ookThreshold %d" CR), RFConfig.newOokThreshold);
rtl_433.setOOKThreshold(RFConfig.newOokThreshold);
success = true;
}
# endif
if (RFdata.containsKey("status")) {
THEENGS_LOG_NOTICE(F("RF get status:" CR));
rtl_433.getStatus();
success = true;
}
if (!success) {
THEENGS_LOG_ERROR(F("MQTTtoRF Fail json" CR));
}
# endif
disableCurrentReceiver();
enableActiveReceiver();
# ifdef ESP32
if (RFdata.containsKey("erase") && RFdata["erase"].as<bool>()) {
// Erase config from NVS (non-volatile storage)
preferences.begin(Gateway_Short_Name, false);
if (preferences.isKey("RFConfig")) {
int result = preferences.remove("RFConfig");
THEENGS_LOG_NOTICE(F("RF config erase result: %d" CR), result);
preferences.end();
return; // Erase prevails on save, so skipping save
} else {
THEENGS_LOG_NOTICE(F("RF config not found" CR));
preferences.end();
}
}
if (RFdata.containsKey("save") && RFdata["save"].as<bool>()) {
StaticJsonDocument<JSON_MSG_BUFFER> jsonBuffer;
JsonObject jo = jsonBuffer.to<JsonObject>();
jo["frequency"] = RFConfig.frequency;
jo["active"] = RFConfig.activeReceiver;
// Don't save those for now, need to be tested
# ifdef ZgatewayRTL_433
//jo["rssithreshold"] = RFConfig.rssiThreshold;
//jo["ookthreshold"] = RFConfig.newOokThreshold;
# endif
// Save config into NVS (non-volatile storage)
String conf = "";
serializeJson(jsonBuffer, conf);
preferences.begin(Gateway_Short_Name, false);
int result = preferences.putString("RFConfig", conf);
preferences.end();
THEENGS_LOG_NOTICE(F("RF Config_save: %s, result: %d" CR), conf.c_str(), result);
}
# endif
}

void RFConfig_init() {
RFConfig.frequency = RF_FREQUENCY;
RFConfig.activeReceiver = ACTIVE_RECEIVER;
RFConfig.rssiThreshold = 0;
RFConfig.newOokThreshold = 0;
}

void RFConfig_load() {
# ifdef ESP32
StaticJsonDocument<JSON_MSG_BUFFER> jsonBuffer;
preferences.begin(Gateway_Short_Name, true);
if (preferences.isKey("RFConfig")) {
auto error = deserializeJson(jsonBuffer, preferences.getString("RFConfig", "{}"));
preferences.end();
if (error) {
THEENGS_LOG_ERROR(F("RF Config deserialization failed: %s, buffer capacity: %u" CR), error.c_str(), jsonBuffer.capacity());
return;
}
if (jsonBuffer.isNull()) {
THEENGS_LOG_WARNING(F("RF Config is null" CR));
return;
}
JsonObject jo = jsonBuffer.as<JsonObject>();
RFConfig_fromJson(jo);
THEENGS_LOG_NOTICE(F("RF Config loaded" CR));
} else {
preferences.end();
THEENGS_LOG_NOTICE(F("RF Config not found using default" CR));
enableActiveReceiver();
}
# else
enableActiveReceiver();
# endif
}

void XtoRFset(const char* topicOri, JsonObject& RFdata) {
if (cmpToMainTopic(topicOri, subjectMQTTtoRFset)) {
THEENGS_LOG_TRACE(F("MQTTtoRF json set" CR));

/*
* Configuration modifications priorities:
* First `init=true` and `load=true` commands are executed (if both are present, INIT prevails on LOAD)
* Then parameters included in json are taken in account
* Finally `erase=true` and `save=true` commands are executed (if both are present, ERASE prevails on SAVE)
*/
if (RFdata.containsKey("init") && RFdata["init"].as<bool>()) {
// Restore the default (initial) configuration
RFConfig_init();
} else if (RFdata.containsKey("load") && RFdata["load"].as<bool>()) {
// Load the saved configuration, if not initialised
RFConfig_load();
}
iRFConfig.loadFromMessage(RFdata);

// Load config from json if available
RFConfig_fromJson(RFdata);
stateRFMeasures();
}
}
Expand Down
16 changes: 5 additions & 11 deletions main/config_RF.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,11 @@
*/
#ifndef config_RF_h
#define config_RF_h
#pragma once

#include "TheengsCommon.h"
#include <rf/RFConfiguration.h>

#ifdef ZgatewayRF
extern void setupRF();
extern void RFtoX();
extern void XtoRF(const char* topicOri, const char* datacallback);
extern void XtoRF(const char* topicOri, JsonObject& RFdata);
Expand Down Expand Up @@ -137,6 +137,9 @@ const char parameters[51][4][24] = {
# define DISCOVERY_TRACE_LOG(...)
# endif
#endif

extern RFConfiguration iRFConfig;

/*-------------------RF topics & parameters----------------------*/
//433Mhz MQTT Subjects and keys
#define subjectMQTTtoRF "/commands/MQTTto433"
Expand Down Expand Up @@ -198,13 +201,6 @@ const char parameters[51][4][24] = {
* 4 = ZgatewayRF2
*/

struct RFConfig_s {
float frequency;
int rssiThreshold;
int newOokThreshold;
int activeReceiver;
};

#define ACTIVE_NONE -1
#define ACTIVE_RECERROR 0
#define ACTIVE_PILIGHT 1
Expand All @@ -224,8 +220,6 @@ struct RFConfig_s {
# define ACTIVE_RECEIVER ACTIVE_NONE
#endif

extern RFConfig_s RFConfig;

/*-------------------CC1101 DefaultTXPower----------------------*/
//Adjust the default TX-Power for sending radio if ZradioCC1101 is used.
//The following settings are possible depending on the frequency band. (-30 -20 -15 -10 -6 0 5 7 10 11 12) Default is max!
Expand Down
4 changes: 2 additions & 2 deletions main/gatewayPilight.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ void XtoPilight(const char* topicOri, JsonObject& Pilightdata) {
const char* protocol = Pilightdata["protocol"];
THEENGS_LOG_NOTICE(F("MQTTtoPilight protocol: %s" CR), protocol);
const char* raw = Pilightdata["raw"];
float txFrequency = Pilightdata["frequency"] | RFConfig.frequency;
float txFrequency = Pilightdata["frequency"] | iRFConfig.getFrequency();
bool success = false;
disableCurrentReceiver();
initCC1101();
Expand Down Expand Up @@ -307,7 +307,7 @@ extern void disablePilightReceive() {
};

extern void enablePilightReceive() {
THEENGS_LOG_NOTICE(F("Switching to Pilight Receiver: %F" CR), RFConfig.frequency);
THEENGS_LOG_NOTICE(F("Switching to Pilight Receiver: %F" CR), iRFConfig.getFrequency());
THEENGS_LOG_NOTICE(F("RF_EMITTER_GPIO: %d " CR), RF_EMITTER_GPIO);
THEENGS_LOG_NOTICE(F("RF_RECEIVER_GPIO: %d " CR), RF_RECEIVER_GPIO);
THEENGS_LOG_TRACE(F("gatewayPilight command topic: %s%s%s" CR), mqtt_topic, gateway_name, subjectMQTTtoPilight);
Expand Down
Loading