2929# ifdef ZradioCC1101
3030# include < ELECHOUSE_CC1101_SRC_DRV.h>
3131# endif
32+ # include < rf/RFConfiguration.h>
33+
3234# include " TheengsCommon.h"
3335# include " config_RF.h"
3436
3739extern rtl_433_ESP rtl_433;
3840# endif
3941
40- RFConfig_s RFConfig;
41- void RFConfig_init ();
42- void RFConfig_load ();
43-
42+ int currentReceiver = ACTIVE_NONE;
43+ extern void enableActiveReceiver ();
44+ extern void disableCurrentReceiver ();
45+
46+ // Note: this is currently just a simple wrapper used to make everything work.
47+ // It prevents introducing external dependencies on newly added C++ structures,
48+ // and acts as a first approach to mask the concrete implementations (rf, rf2,
49+ // pilight, etc.). Later this can be extended or replaced by more complete driver
50+ // abstractions without changing the rest of the system.
51+ class ZCommonRFWrapper : public RFReceiver {
52+ public:
53+ ZCommonRFWrapper () : RFReceiver() {}
54+ void enable () override { enableActiveReceiver (); }
55+ void disable () override { disableCurrentReceiver (); }
56+
57+ int getReceiverID () const override { return currentReceiver; }
58+ };
59+
60+ ZCommonRFWrapper iRFReceiver;
61+ RFConfiguration iRFConfig (iRFReceiver);
62+
63+ // TODO review
4464void initCC1101 () {
4565# ifdef ZradioCC1101 // receiving with CC1101
4666 // Loop on getCC1101() until it returns true and break after 10 attempts
@@ -54,7 +74,7 @@ void initCC1101() {
5474 if (ELECHOUSE_cc1101.getCC1101 ()) {
5575 THEENGS_LOG_NOTICE (F (" C1101 spi Connection OK" CR));
5676 ELECHOUSE_cc1101.Init ();
57- ELECHOUSE_cc1101.SetRx (RFConfig. frequency );
77+ ELECHOUSE_cc1101.SetRx (iRFConfig. getFrequency () );
5878 break ;
5979 } else {
6080 THEENGS_LOG_ERROR (F (" C1101 spi Connection Error" CR));
@@ -68,23 +88,10 @@ void initCC1101() {
6888}
6989
7090void setupCommonRF () {
71- RFConfig_init ();
72- RFConfig_load ();
73- }
74-
75- bool validFrequency (float mhz) {
76- // CC1101 valid frequencies 300-348 MHZ, 387-464MHZ and 779-928MHZ.
77- if (mhz >= 300 && mhz <= 348 )
78- return true ;
79- if (mhz >= 387 && mhz <= 464 )
80- return true ;
81- if (mhz >= 779 && mhz <= 928 )
82- return true ;
83- return false ;
91+ iRFConfig.reInit ();
92+ iRFConfig.loadFromStorage ();
8493}
8594
86- int currentReceiver = ACTIVE_NONE;
87-
8895# if !defined(ZgatewayRFM69) && !defined(ZactuatorSomfy)
8996// Check if a receiver is available
9097bool validReceiver (int receiver) {
@@ -138,13 +145,13 @@ void disableCurrentReceiver() {
138145 break ;
139146# endif
140147 default :
141- THEENGS_LOG_ERROR (F (" ERROR: unsupported receiver %d" CR), RFConfig. activeReceiver );
148+ THEENGS_LOG_ERROR (F (" ERROR: unsupported receiver %d" CR), iRFConfig. getActiveReceiver () );
142149 }
143150}
144151
145152void enableActiveReceiver () {
146- THEENGS_LOG_TRACE (F (" enableActiveReceiver: %d" CR), RFConfig. activeReceiver );
147- switch (RFConfig. activeReceiver ) {
153+ THEENGS_LOG_TRACE (F (" enableActiveReceiver: %d" CR), iRFConfig. getActiveReceiver () );
154+ switch (iRFConfig. getActiveReceiver () ) {
148155# ifdef ZgatewayPilight
149156 case ACTIVE_PILIGHT:
150157 initCC1101 ();
@@ -155,7 +162,7 @@ void enableActiveReceiver() {
155162# ifdef ZgatewayRF
156163 case ACTIVE_RF:
157164 initCC1101 ();
158- enableRFReceive (RFConfig. frequency , RF_RECEIVER_GPIO, RF_EMITTER_GPIO);
165+ enableRFReceive (iRFConfig. getFrequency () , RF_RECEIVER_GPIO, RF_EMITTER_GPIO);
159166 currentReceiver = ACTIVE_RF;
160167 break ;
161168# endif
@@ -177,18 +184,21 @@ void enableActiveReceiver() {
177184 THEENGS_LOG_ERROR (F (" ERROR: no receiver selected" CR));
178185 break ;
179186 default :
180- THEENGS_LOG_ERROR (F (" ERROR: unsupported receiver %d" CR), RFConfig. activeReceiver );
187+ THEENGS_LOG_ERROR (F (" ERROR: unsupported receiver %d" CR), iRFConfig. getActiveReceiver () );
181188 }
182189}
183190
184191String stateRFMeasures () {
185192 // Publish RTL_433 state
186193 StaticJsonDocument<JSON_MSG_BUFFER> jsonBuffer;
187194 JsonObject RFdata = jsonBuffer.to <JsonObject>();
188- RFdata[" active" ] = RFConfig.activeReceiver ;
195+
196+ // load the configuration
197+ iRFConfig.toJson (RFdata);
198+
199+ // load the current state
189200# if defined(ZradioCC1101) || defined(ZradioSX127x)
190- RFdata[" frequency" ] = RFConfig.frequency ;
191- if (RFConfig.activeReceiver == ACTIVE_RTL) {
201+ if (iRFConfig.getActiveReceiver () == ACTIVE_RTL) {
192202# ifdef ZgatewayRTL_433
193203 RFdata[" rssithreshold" ] = (int )getRTLrssiThreshold ();
194204 RFdata[" rssi" ] = (int )getRTLCurrentRSSI ();
@@ -211,134 +221,12 @@ String stateRFMeasures() {
211221 return output;
212222}
213223
214- void RFConfig_fromJson (JsonObject& RFdata) {
215- bool success = false ;
216- if (RFdata.containsKey (" frequency" ) && validFrequency (RFdata[" frequency" ])) {
217- Config_update (RFdata, " frequency" , RFConfig.frequency );
218- THEENGS_LOG_NOTICE (F (" RF Receive mhz: %F" CR), RFConfig.frequency );
219- success = true ;
220- }
221- if (RFdata.containsKey (" active" )) {
222- THEENGS_LOG_NOTICE (F (" RF receiver active: %d" CR), RFConfig.activeReceiver );
223- Config_update (RFdata, " active" , RFConfig.activeReceiver );
224- success = true ;
225- }
226- # ifdef ZgatewayRTL_433
227- if (RFdata.containsKey (" rssithreshold" )) {
228- THEENGS_LOG_NOTICE (F (" RTL_433 RSSI Threshold : %d " CR), RFConfig.rssiThreshold );
229- Config_update (RFdata, " rssithreshold" , RFConfig.rssiThreshold );
230- rtl_433.setRSSIThreshold (RFConfig.rssiThreshold );
231- success = true ;
232- }
233- # if defined(RF_SX1276) || defined(RF_SX1278)
234- if (RFdata.containsKey (" ookthreshold" )) {
235- Config_update (RFdata, " ookthreshold" , RFConfig.newOokThreshold );
236- THEENGS_LOG_NOTICE (F (" RTL_433 ookThreshold %d" CR), RFConfig.newOokThreshold );
237- rtl_433.setOOKThreshold (RFConfig.newOokThreshold );
238- success = true ;
239- }
240- # endif
241- if (RFdata.containsKey (" status" )) {
242- THEENGS_LOG_NOTICE (F (" RF get status:" CR));
243- rtl_433.getStatus ();
244- success = true ;
245- }
246- if (!success) {
247- THEENGS_LOG_ERROR (F (" MQTTtoRF Fail json" CR));
248- }
249- # endif
250- disableCurrentReceiver ();
251- enableActiveReceiver ();
252- # ifdef ESP32
253- if (RFdata.containsKey (" erase" ) && RFdata[" erase" ].as <bool >()) {
254- // Erase config from NVS (non-volatile storage)
255- preferences.begin (Gateway_Short_Name, false );
256- if (preferences.isKey (" RFConfig" )) {
257- int result = preferences.remove (" RFConfig" );
258- THEENGS_LOG_NOTICE (F (" RF config erase result: %d" CR), result);
259- preferences.end ();
260- return ; // Erase prevails on save, so skipping save
261- } else {
262- THEENGS_LOG_NOTICE (F (" RF config not found" CR));
263- preferences.end ();
264- }
265- }
266- if (RFdata.containsKey (" save" ) && RFdata[" save" ].as <bool >()) {
267- StaticJsonDocument<JSON_MSG_BUFFER> jsonBuffer;
268- JsonObject jo = jsonBuffer.to <JsonObject>();
269- jo[" frequency" ] = RFConfig.frequency ;
270- jo[" active" ] = RFConfig.activeReceiver ;
271- // Don't save those for now, need to be tested
272- # ifdef ZgatewayRTL_433
273- // jo["rssithreshold"] = RFConfig.rssiThreshold;
274- // jo["ookthreshold"] = RFConfig.newOokThreshold;
275- # endif
276- // Save config into NVS (non-volatile storage)
277- String conf = " " ;
278- serializeJson (jsonBuffer, conf);
279- preferences.begin (Gateway_Short_Name, false );
280- int result = preferences.putString (" RFConfig" , conf);
281- preferences.end ();
282- THEENGS_LOG_NOTICE (F (" RF Config_save: %s, result: %d" CR), conf.c_str (), result);
283- }
284- # endif
285- }
286-
287- void RFConfig_init () {
288- RFConfig.frequency = RF_FREQUENCY;
289- RFConfig.activeReceiver = ACTIVE_RECEIVER;
290- RFConfig.rssiThreshold = 0 ;
291- RFConfig.newOokThreshold = 0 ;
292- }
293-
294- void RFConfig_load () {
295- # ifdef ESP32
296- StaticJsonDocument<JSON_MSG_BUFFER> jsonBuffer;
297- preferences.begin (Gateway_Short_Name, true );
298- if (preferences.isKey (" RFConfig" )) {
299- auto error = deserializeJson (jsonBuffer, preferences.getString (" RFConfig" , " {}" ));
300- preferences.end ();
301- if (error) {
302- THEENGS_LOG_ERROR (F (" RF Config deserialization failed: %s, buffer capacity: %u" CR), error.c_str (), jsonBuffer.capacity ());
303- return ;
304- }
305- if (jsonBuffer.isNull ()) {
306- THEENGS_LOG_WARNING (F (" RF Config is null" CR));
307- return ;
308- }
309- JsonObject jo = jsonBuffer.as <JsonObject>();
310- RFConfig_fromJson (jo);
311- THEENGS_LOG_NOTICE (F (" RF Config loaded" CR));
312- } else {
313- preferences.end ();
314- THEENGS_LOG_NOTICE (F (" RF Config not found using default" CR));
315- enableActiveReceiver ();
316- }
317- # else
318- enableActiveReceiver ();
319- # endif
320- }
321-
322224void XtoRFset (const char * topicOri, JsonObject& RFdata) {
323225 if (cmpToMainTopic (topicOri, subjectMQTTtoRFset)) {
324226 THEENGS_LOG_TRACE (F (" MQTTtoRF json set" CR));
325227
326- /*
327- * Configuration modifications priorities:
328- * First `init=true` and `load=true` commands are executed (if both are present, INIT prevails on LOAD)
329- * Then parameters included in json are taken in account
330- * Finally `erase=true` and `save=true` commands are executed (if both are present, ERASE prevails on SAVE)
331- */
332- if (RFdata.containsKey (" init" ) && RFdata[" init" ].as <bool >()) {
333- // Restore the default (initial) configuration
334- RFConfig_init ();
335- } else if (RFdata.containsKey (" load" ) && RFdata[" load" ].as <bool >()) {
336- // Load the saved configuration, if not initialised
337- RFConfig_load ();
338- }
228+ iRFConfig.loadFromMessage (RFdata);
339229
340- // Load config from json if available
341- RFConfig_fromJson (RFdata);
342230 stateRFMeasures ();
343231 }
344232}
0 commit comments