Files
OpenMQTTGateway/main/commonRF.cpp
Alessandro Staniscia 98481c5145 [SITE] Renew the web board presentation and the ESP32 web upload + [SYS] Security checks (#2277)
* Refactor GitHub Actions workflows for build, documentation, and linting

- Consolidated build logic into reusable workflows (`task-build.yml` and `task-docs.yml`) to reduce duplication across multiple workflows.
- Introduced `environments.json` to centralize the list of PlatformIO build environments, improving maintainability and clarity.
- Updated `build.yml` and `build_and_docs_to_dev.yml` to utilize the new reusable workflows and environment definitions.
- Enhanced `release.yml` to streamline the release process and integrate documentation generation.
- Created reusable linting workflow (`task-lint.yml`) to standardize code formatting checks across the repository.
- Simplified manual documentation workflow by leveraging the new reusable documentation workflow.
- Improved artifact management and retention policies across workflows.
- Updated dependencies and versions in workflows to ensure compatibility and performance.

CI/CD pipeline agnostic of Workflow Engine and integrated on github actions

- Implemented ci.sh for orchestrating the complete build pipeline.
- Created ci_00_config.sh for centralized configuration of build scripts.
- Created ci_build_firmware.sh for building firmware for specified PlatformIO environments.
- Created ci_prepare_artifacts.sh for preparing firmware artifacts for upload or deployment.
- Created ci_set_version.sh for updating version tags in firmware configuration files.
- Created ci_build.sh to orchestrate the complete build pipeline.
- Created ci_qa.sh for code linting and formatting checks using clang-format.
- Created ci_site.sh for building and deploying VuePress documentation with version management.
- Implemented checks for required tools and dependencies in the new scripts.
- Improved internal scripts for better error handling and logging.

UPDATE the web installer manifest generation and update documentation structure
- Enhanced ci_list-env.sh to list environments from a JSON file.
- Replaced  common_wu.py and gen_wu.py scripts with new npm scripts for site generation and previewing on docsgen/gen_wu.js
- Replaced  generate_board_docs.py with docsgen/generated_board_docs.js
- Added new npm scripts for integration of site generation on build phase.
- Created preview_site.js to serve locally generated site over HTTPS with improved error handling.
- Added new CI environments for CI builds in environments.json.
- Deleted lint.yml as part of workflow cleanup.
- Enhanced task-build.yml to include linting as a job and added support for specifying PlatformIO version.
- Improved task-docs.yml to handle versioning more effectively and added clean option.

Enhance documentation
- ADD CLEAR Mark of development version of site
- Updated README.md to include detailed workflow dependencies and relationships using mermaid diagrams.
- Improved development.md with a quick checklist for contributors and clarified the code style guide.
- Enhanced quick_start.md with tips for contributors and streamlined the workflow explanation.

LINT FIX
- Refined User_config.h for better formatting consistency.
- Adjusted blufi.cpp and gatewayBT.cpp for improved code readability and consistency in formatting.
- Updated gatewaySERIAL.cpp and mqttDiscovery.cpp to enhance logging error messages.
- Improved sensorDS1820.cpp for better logging of device information.

Add security scan workflows for vulnerability detection

Add SBOM generation and upload to release workflow; update security scan summary handling

Add shellcheck suppor + FIX shellcheck warning

Enhance documentation for CI/CD scripts and workflows, adding details for security scanning and SBOM generation processes

Fix formatting and alignment in BLE connection handling

Reviewed the full web board presentation and the ESP32 web upload. The project uses a modern pattern where data is divided from the presentation layer.

- Removed the `generate_board_docs` script.
- Updated the `gen_wu` script in order to generate `boards-info.json`: the fail that containe all information about the configuration
- Created and isolate the file `boards-info.js` to streamline the parsing of PlatformIO dependencies, modules, environments and improve the handling of library information.
- Introduced vuepress component `BoardEnvironmentTable.vue` that render `boards-info.json` as UI card component
- Introduced vuepress component `FlashEnvironmentSelector.vue` that render a selectred environment from  `boards-info.json` and provide esp-web-upload feature on it
- Introduced a new board page `board-selector.md` for improved firmware selection.
- Updated `web-install.md` to enhance the firmware upload process, including a new board environment table.
- Enhanced custom descriptions in `environments.ini` to include HTML links for better user guidance and board image link

Add CC1101 initialization improvements and logging enhancements
Add installation step for PlatformIO dependencies in documentation workflow

Remove ci_set_version.sh script and associated versioning functionality

* Fix comment provisined

Fix PlatformIO version input reference in documentation workflow

Remove outdated Squeezelite-ESP32 installer documentation
2026-03-09 07:47:30 -05:00

272 lines
8.0 KiB
C++

/*
Theengs OpenMQTTGateway - We Unite Sensors in One Open-Source Interface
Act as a wifi or ethernet gateway between your BLE/433mhz/infrared IR signal and an MQTT broker
Send and receiving command by MQTT
Copyright: (c)Florian ROBERT
This file is part of OpenMQTTGateway.
OpenMQTTGateway 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.
OpenMQTTGateway 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/>.
*/
#include "User_config.h"
#if defined(ZgatewayRF) || defined(ZgatewayPilight) || defined(ZgatewayRTL_433) || defined(ZgatewayRF2) || defined(ZactuatorSomfy)
# ifdef ZradioCC1101
# include <ELECHOUSE_CC1101_SRC_DRV.h>
# endif
# include <rf/RFConfiguration.h>
# include "TheengsCommon.h"
# include "config_RF.h"
# ifdef ZgatewayRTL_433
# include <rtl_433_ESP.h>
extern rtl_433_ESP rtl_433;
# endif
int currentReceiver = ACTIVE_NONE;
boolean isDriverEnabled = false;
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);
// Initialize the CC1101 and tune the RX frequency.
// - Uses truncated exponential backoff to avoid tight retry loops
// - Validates the configured frequency before touching the radio
// - Emits explicit logs for success/failure so watchdog resets are traceable
void initCC1101() {
# ifdef ZradioCC1101 // receiving with CC1101
const float freqMhz = iRFConfig.getFrequency();
if (!iRFConfig.validFrequency(freqMhz)) {
THEENGS_LOG_ERROR(F("C1101 invalid frequency: %F MHz" CR), freqMhz);
return;
}
int delayMS = 16;
int delayMaxMS = 500;
bool connected = false;
for (int attempt = 1; attempt <= 10; attempt++) {
# if defined(RF_CC1101_SCK) && defined(RF_CC1101_MISO) && \
defined(RF_CC1101_MOSI) && defined(RF_CC1101_CS)
THEENGS_LOG_TRACE(F("initCC1101 with custom SPI pins, SCK=%d, MISO=%d, MOSI=%d, CS=%d" CR), RF_CC1101_SCK, RF_CC1101_MISO, RF_CC1101_MOSI, RF_CC1101_CS);
ELECHOUSE_cc1101.setSpiPin(RF_CC1101_SCK, RF_CC1101_MISO, RF_CC1101_MOSI, RF_CC1101_CS);
# endif
if (ELECHOUSE_cc1101.getCC1101()) {
connected = true;
THEENGS_LOG_NOTICE(F("C1101 SPI connection OK on attempt %d" CR), attempt);
ELECHOUSE_cc1101.Init();
ELECHOUSE_cc1101.SetRx(freqMhz);
THEENGS_LOG_NOTICE(F("C1101 tuned RX to %F MHz" CR), freqMhz);
break;
}
THEENGS_LOG_ERROR(F("C1101 SPI connection error (attempt %d), retrying" CR), attempt);
delay(delayMS);
// truncated exponential backoff
delayMS = delayMS * 2;
if (delayMS > delayMaxMS) delayMS = delayMaxMS;
}
if (!connected) {
THEENGS_LOG_ERROR(F("C1101 init failed after retries, radio left disabled" CR));
}
# else
THEENGS_LOG_TRACE(F("initCC1101 skipped: ZradioCC1101 not enabled" CR));
# endif
}
void setupCommonRF() {
iRFConfig.reInit();
iRFConfig.loadFromStorage(true);
}
# if !defined(ZgatewayRFM69) && !defined(ZactuatorSomfy)
// Check if a receiver is available
bool validReceiver(int receiver) {
switch (receiver) {
# ifdef ZgatewayPilight
case ACTIVE_PILIGHT:
return true;
# endif
# ifdef ZgatewayRF
case ACTIVE_RF:
return true;
# endif
# ifdef ZgatewayRTL_433
case ACTIVE_RTL:
return true;
# endif
# ifdef ZgatewayRF2
case ACTIVE_RF2:
return true;
# endif
default:
THEENGS_LOG_ERROR(F("ERROR: stored receiver %d not available" CR), receiver);
}
return false;
}
# endif
void disableCurrentReceiver() {
if (!isDriverEnabled) {
THEENGS_LOG_TRACE(F("Receiver %d is already disabled" CR), currentReceiver);
return;
}
THEENGS_LOG_TRACE(F("disableCurrentReceiver: %d" CR), currentReceiver);
switch (currentReceiver) {
case ACTIVE_NONE:
isDriverEnabled = false;
break;
# ifdef ZgatewayPilight
case ACTIVE_PILIGHT:
disablePilightReceive();
isDriverEnabled = false;
break;
# endif
# ifdef ZgatewayRF
case ACTIVE_RF:
disableRFReceive();
isDriverEnabled = false;
break;
# endif
# ifdef ZgatewayRTL_433
case ACTIVE_RTL:
disableRTLreceive();
isDriverEnabled = false;
break;
# endif
# ifdef ZgatewayRF2
case ACTIVE_RF2:
disableRF2Receive();
isDriverEnabled = false;
break;
# endif
default:
THEENGS_LOG_ERROR(F("ERROR: unsupported receiver %d" CR), iRFConfig.getActiveReceiver());
}
}
void enableActiveReceiver() {
if (isDriverEnabled) {
THEENGS_LOG_TRACE(F("Receiver %d is already enabled" CR), currentReceiver);
return;
}
THEENGS_LOG_TRACE(F("enableActiveReceiver: %d" CR), iRFConfig.getActiveReceiver());
switch (iRFConfig.getActiveReceiver()) {
# ifdef ZgatewayPilight
case ACTIVE_PILIGHT:
initCC1101();
enablePilightReceive();
currentReceiver = ACTIVE_PILIGHT;
isDriverEnabled = true;
break;
# endif
# ifdef ZgatewayRF
case ACTIVE_RF:
initCC1101();
enableRFReceive(iRFConfig.getFrequency(), RF_RECEIVER_GPIO, RF_EMITTER_GPIO);
currentReceiver = ACTIVE_RF;
isDriverEnabled = true;
break;
# endif
# ifdef ZgatewayRTL_433
case ACTIVE_RTL:
initCC1101();
enableRTLreceive();
currentReceiver = ACTIVE_RTL;
isDriverEnabled = true;
break;
# endif
# ifdef ZgatewayRF2
case ACTIVE_RF2:
initCC1101();
enableRF2Receive();
currentReceiver = ACTIVE_RF2;
isDriverEnabled = true;
break;
# endif
case ACTIVE_RECERROR:
THEENGS_LOG_ERROR(F("ERROR: no receiver selected" CR));
break;
default:
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>();
// load the configuration
iRFConfig.toJson(RFdata);
// load the current state
# if defined(ZradioCC1101) || defined(ZradioSX127x)
if (iRFConfig.getActiveReceiver() == ACTIVE_RTL) {
# ifdef ZgatewayRTL_433
RFdata["rssithreshold"] = (int)getRTLrssiThreshold();
RFdata["rssi"] = (int)getRTLCurrentRSSI();
RFdata["avgrssi"] = (int)getRTLAverageRSSI();
RFdata["count"] = (int)getRTLMessageCount();
// Capture high water mark of rtl_433_Decoder stack since it can run out and trigger reboot
extern TaskHandle_t rtl_433_DecoderHandle;
RFdata["rtl433_stack"] = (int)uxTaskGetStackHighWaterMark(rtl_433_DecoderHandle);
# endif
# ifdef ZradioSX127x
RFdata["ookthreshold"] = (int)getOOKThresh();
# endif
}
# endif
RFdata["origin"] = subjectcommonRFtoMQTT;
enqueueJsonObject(RFdata);
String output;
serializeJson(RFdata, output);
return output;
}
void XtoRFset(const char* topicOri, JsonObject& RFdata) {
if (cmpToMainTopic(topicOri, subjectMQTTtoRFset)) {
THEENGS_LOG_TRACE(F("MQTTtoRF json set" CR));
iRFConfig.loadFromMessage(RFdata);
stateRFMeasures();
}
}
#endif