Skip to content

A Hardware Hacking Tool with Web-Based CLI That Speaks Every Protocol

create rf24 controller

Geoa93607badcfe
diff --git a/src/Controllers/Rf24Controller.cpp b/src/Controllers/Rf24Controller.cpp
new file mode 100644
index 00000000..a65717d0
--- /dev/null
+++ b/src/Controllers/Rf24Controller.cpp
@@ -0,0 +1,194 @@
+#include "Controllers/Rf24Controller.h"
+
+/*
+Entry point for rf24 commands
+*/
+void Rf24Controller::handleCommand(const TerminalCommand& cmd) {
+ const std::string& root = cmd.getRoot();
+
+ if (root == "help") handleHelp();
+ else if (root == "config") handleConfig();
+ else if (root == "sniff") handleSniff();
+ else if (root == "scan") handleScan();
+ else if (root == "setchannel") handleSetChannel();
+ else handleHelp();
+}
+
+/*
+Ensure NRF24 is configured before use
+*/
+void Rf24Controller::ensureConfigured() {
+ if (!configured) {
+ handleConfig();
+ configured = true;
+ return;
+ }
+
+ uint8_t ce = state.getRf24CePin();
+ uint8_t csn = state.getRf24CsnPin();
+ uint8_t sck = state.getRf24SckPin();
+ uint8_t miso = state.getRf24MisoPin();
+ uint8_t mosi = state.getRf24MosiPin();
+ rf24Service.configure(csn, ce, sck, miso, mosi);
+
+}
+
+/*
+Sniff
+*/
+void Rf24Controller::handleSniff() {
+ terminalView.println("RF24: Sniffing Channel " + std::to_string(rf24Service.getChannel()) + "... Press [ENTER] to stop.\n");
+
+ rf24Service.initRx();
+ rf24Service.startListening();
+ while (true) {
+ auto c = terminalInput.readChar();
+ if (c == '\n' || c == '\r') break;
+
+ if (rf24Service.available()) {
+ uint8_t tmp[32] = {0};
+ if (rf24Service.receive(tmp, sizeof(tmp))) {
+
+ // Display hex and ASCII
+ for (int row = 0; row < 32; row += 16) {
+ // Hex
+ for (int i = 0; i < 16; i++) {
+ terminalView.print(argTransformer.toHex(tmp[row + i], 2) + " ");
+ }
+
+ terminalView.print(" | ");
+
+ // ASCII
+ for (int i = 0; i < 16; i++) {
+ char c = (tmp[row + i] >= 32 && tmp[row + i] <= 126) ? tmp[row + i] : '.';
+ terminalView.print(std::string(1, c));
+ }
+
+ terminalView.println("");
+ }
+ }
+ }
+ }
+ rf24Service.stopListening();
+ rf24Service.flushRx();
+ terminalView.println("\nRF24: Sniff stopped by user.\n");
+}
+
+/*
+Scan
+*/
+void Rf24Controller::handleScan() {
+ uint32_t dwell = 128; // µs
+ uint8_t levelHold[126 + 1] = {0}; // 0..200
+ uint8_t threshold = userInputManager.readValidatedUint8("High threshold (10..200)?", 20, 10, 200);
+ int bestCh = -1;
+ uint8_t bestVal = 0;
+ const uint8_t DECAY = 6; // decay per sweep
+
+ terminalView.println("RF24: Scanning ch 0..125… Press [ENTER] to stop.\n");
+
+ rf24Service.initRx();
+ while (true) {
+ // Cancel
+ char c = terminalInput.readChar();
+ if (c == '\n' || c == '\r') break;
+
+ // Sweep
+ for (uint8_t ch = 0; ch <= 125; ++ch) {
+ rf24Service.setChannel(ch);
+ rf24Service.startListening();
+ delayMicroseconds(dwell);
+ rf24Service.stopListening();
+
+ // Measure
+ uint8_t instant = 0;
+ if (rf24Service.testRpd()) instant = 200;
+ else if (rf24Service.testCarrier()) instant = 120;
+
+ // Decrease hold level
+ if (levelHold[ch] > DECAY) levelHold[ch] -= DECAY;
+ else levelHold[ch] = 0;
+
+ // Get max
+ if (instant > levelHold[ch]) levelHold[ch] = instant;
+
+ if (levelHold[ch] >= threshold) {
+ // Save best
+ if (levelHold[ch] > bestVal) {
+ bestVal = levelHold[ch];
+ bestCh = ch;
+ }
+
+ // Log
+ terminalView.println(
+ " Detect: Channel=" + std::to_string(ch) +
+ " Freq=" + std::to_string(2400 + ch) + " MHz" +
+ " Level=" + std::to_string(levelHold[ch])
+ );
+ }
+ }
+ }
+
+ // Result
+ terminalView.println("");
+ if (bestCh >= 0) {
+ // Log best
+ terminalView.println(
+ "Best channel: ch=" + std::to_string(bestCh) +
+ " f=" + std::to_string(2400 + bestCh) + " MHz" +
+ " peakLevel=" + std::to_string(bestVal)
+ );
+
+ // Ask to apply to config
+ if (userInputManager.readYesNo("Save best channel to config?", true)) {
+ rf24Service.setChannel((uint8_t)bestCh);
+ terminalView.println("RF24: Channel set to " + std::to_string(bestCh) + ".\n");
+ } else {
+ terminalView.println("RF24: Channel not changed.\n");
+ }
+ } else {
+ terminalView.println("\nRF24: No activity detected.\n");
+ }
+}
+
+
+/*
+Set Channel
+*/
+void Rf24Controller::handleSetChannel() {
+ uint8_t ch = userInputManager.readValidatedUint8("Channel (0..125)?", 76, 0, 125);
+ rf24Service.setChannel(ch);
+ terminalView.println("RF24: Channel set to " + std::to_string(ch) + ".");
+}
+
+/*
+NRF24 Configuration
+*/
+void Rf24Controller::handleConfig() {
+ uint8_t csn = userInputManager.readValidatedInt("NRF24 CSN pin?", state.getRf24CsnPin());
+ uint8_t sck = userInputManager.readValidatedInt("NRF24 SCK pin?", state.getRf24SckPin());
+ uint8_t miso = userInputManager.readValidatedInt("NRF24 MISO pin?", state.getRf24MisoPin());
+ uint8_t mosi = userInputManager.readValidatedInt("NRF24 MOSI pin?", state.getRf24MosiPin());
+ uint8_t ce = userInputManager.readValidatedInt("NRF24 CE pin?", state.getRf24CePin());
+ state.setRf24CsnPin(csn);
+ state.setRf24SckPin(sck);
+ state.setRf24MisoPin(miso);
+ state.setRf24MosiPin(mosi);
+ state.setRf24CePin(ce);
+
+ bool ok = rf24Service.configure(csn, ce, sck, miso, mosi);
+
+ configured = true; // consider configured even if not detected to avoid re-asking
+ terminalView.println(ok ? "\n ✅ NRF24 detected and configured.\n" : "\n ❌ NRF24 not detected. Check wiring.\n");
+}
+
+/*
+Help
+*/
+void Rf24Controller::handleHelp() {
+ terminalView.println("RF24 commands:");
+ terminalView.println(" scan");
+ terminalView.println(" sniff");
+ terminalView.println(" setchannel");
+ terminalView.println(" config");
+}
\ No newline at end of file
diff --git a/src/Controllers/Rf24Controller.h b/src/Controllers/Rf24Controller.h
new file mode 100644
index 00000000..782ec30e
--- /dev/null
+++ b/src/Controllers/Rf24Controller.h
@@ -0,0 +1,58 @@
+#pragma once
+
+#include <vector>
+#include <string>
+#include <cstdint>
+#include "Interfaces/ITerminalView.h"
+#include "Interfaces/IInput.h"
+#include "Interfaces/IDeviceView.h"
+#include "Models/TerminalCommand.h"
+#include "Transformers/ArgTransformer.h"
+#include "Managers/UserInputManager.h"
+#include "States/GlobalState.h"
+#include "Services/Rf24Service.h"
+#include "Services/PinService.h"
+
+class Rf24Controller {
+public:
+ Rf24Controller(ITerminalView& terminalView,
+ IInput& terminalInput,
+ IDeviceView& deviceView,
+ Rf24Service& rf24Service,
+ PinService& pinService,
+ ArgTransformer& argTransformer,
+ UserInputManager& userInputManager)
+ : terminalView(terminalView),
+ terminalInput(terminalInput),
+ deviceView(deviceView),
+ rf24Service(rf24Service),
+ pinService(pinService),
+ argTransformer(argTransformer),
+ userInputManager(userInputManager) {}
+
+ // Entry point for rf24 commands
+ void handleCommand(const TerminalCommand& cmd);
+
+ // Ensure RF24 is configured before use
+ void ensureConfigured();
+
+private:
+ // Command handlers
+ void handleConfig();
+ void handleSniff();
+ void handleScan();
+ void handleSetChannel();
+ void handleHelp();
+
+private:
+ ITerminalView& terminalView;
+ IInput& terminalInput;
+ IDeviceView& deviceView;
+ Rf24Service& rf24Service;
+ PinService& pinService;
+ ArgTransformer& argTransformer;
+ UserInputManager& userInputManager;
+ GlobalState& state = GlobalState::getInstance();
+
+ bool configured = false;
+};