Skip to content

Commit

Permalink
Merge pull request #33 from CapibaraZero/develop/0.3.0
Browse files Browse the repository at this point in the history
Add SubGHZ
  • Loading branch information
andreock authored Jul 1, 2024
2 parents ba0384a + f8996fa commit 64521ba
Show file tree
Hide file tree
Showing 34 changed files with 1,256 additions and 45 deletions.
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,7 @@ build

# Seeed-Studio libs
lib/PN532
lib/PN532_I2C
lib/PN532_I2C

# MacOS useless file
.DS_Store
5 changes: 2 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,13 @@ Working Features:
- Wi-Fi support
- BLE support
- BadUSB support(Limited to 4-5 files in file browser)
- NFC support(Expect bugs)
- NFC support
- Some network attacks
- SubGHZ(Beta support, need more testing)

WIP features:

- SubGHZ
- IR
- Add more network attacks

More details in the project view: https://github.com/orgs/CapibaraZero/projects/2/views/1

Expand Down
11 changes: 9 additions & 2 deletions include/pins.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@
#define SD_CARD_SCK 36
#define SD_CARD_MISO 37

#define SX1276_NSS 1
#define SX1276_DIO2 15
#define SX1276_DIO1 16

#define PN532_SDA 8
#define PN532_SCL 9

Expand All @@ -62,9 +66,12 @@
#define SD_CARD_MISO D12
#define SD_CARD_SCK D13

#define CC1101_GDO0 A0
#define CC1101_CSN D7
// SX1276(SubGHZ)
#define SX1276_DIO1 D3
#define SX1276_NSS D7
#define SX1276_DIO2 D8

// PN532(NFC)
#define PN532_SCL A6
#define PN532_SDA A7
#endif
161 changes: 161 additions & 0 deletions lib/SubGHZ/SubGHZ.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,161 @@
#include "SubGHZ.hpp"
#include <Arduino.h>
#include <vector>
#include "../../include/pins.h"

SX1276 radio = new Module(SX1276_NSS, RADIOLIB_NC, RADIOLIB_NC, SX1276_DIO1);

// this function is called when a new bit is received
void IRAM_ATTR readBit(void) {
// read the data bit
radio.readBit(SX1276_DIO2);
}

SubGHZ::SubGHZ(uint8_t sck, uint8_t miso, uint8_t mosi, uint8_t csn,
uint8_t gdo0, uint8_t gdo2) {
if (initialized) return;
int state = radio.beginFSK();
if (state == RADIOLIB_ERR_NONE) {
Serial.println(F("success!"));
} else {
Serial.print(F("failed, code "));
Serial.println(state);
}
_gdo0 = gdo0;
_gdo2 = gdo2;
initialized = true;
}

SubGHZ::~SubGHZ() {}

void SubGHZ::error_check(const char *operation, int16_t status_code) {
if (status_code != RADIOLIB_ERR_NONE) {
Serial.print(operation);
Serial.print(" : ");
Serial.print(F("failed, code "));
Serial.println(status_code);
}
}

void SubGHZ::init_receive() {
if (_modulation == 2) // LoRa doesn't require anything
return;
radio.setCRC(false);
radio.setAFC(true);
radio.setDirectSyncWord(0x555512AD, 0);
radio.setDirectAction(readBit);
radio.receiveDirect();
}

void SubGHZ::stop_receive() { radio.clearDio1Action(); }

void SubGHZ::set_freq_mod(float freq, float bw, float deviation) {
int state = radio.setFrequency(freq);
error_check("Set frequency", state);
if (_modulation != 2) {
state = radio.setRxBandwidth(bw);
error_check("Set bandwidth", state);
state = radio.setFrequencyDeviation(deviation);
error_check("Set deviation", state);
_deviation = deviation;
}
_frequency = freq;
_bw = bw;
}

SignalStrength SubGHZ::scan_frequency(float freq, float bw, float deviation) {
set_freq_mod(freq, bw, deviation);
return SignalStrength{.rssi = radio.getRSSI(false, false)};
}

SignalStrength SubGHZ::scan_frequency() {
return SignalStrength{.rssi = radio.getRSSI(false, false)};
}

#define DEFAULT_SAMPLING_RATE 50

#define READ_N_BYTE() for (int j = 7; j > -1; j--)

void read_lora_packet(std::vector<byte> *orig_signal) {
// TODO: Add DIO0 and use it as interrupt

// you can also receive data as byte array
size_t len = radio.getPacketLength();
byte byteArr[len];
int state = radio.receive(byteArr, len);

if (state == RADIOLIB_ERR_NONE) {
// packet was successfully received
Serial.println(F("Packet arrived!"));
memcpy(orig_signal->data(), byteArr, len);
} else if (state == RADIOLIB_ERR_RX_TIMEOUT) {
// timeout occurred while waiting for a packet
Serial.println(F("timeout!"));

} else if (state == RADIOLIB_ERR_CRC_MISMATCH) {
// packet was received, but is malformed
Serial.println(F("CRC error!"));

} else {
// some other error occurred
Serial.print(F("failed, code "));
Serial.println(state);
}
}

void SubGHZ::rec_signal(std::vector<byte> *orig_signal) {
if (_modulation == 2) { // LoRa modulation
while (true) {
read_lora_packet(orig_signal);
}
}
if (radio.available()) {
Serial.println("[SX1276] Received packet in direct mode!");
while (radio.available()) {
// read a byte
byte signal = radio.read();
orig_signal->push_back(signal);
}
}
}

void SubGHZ::send_signal(float freq, Modulation modulation, byte *payload,
size_t length) {
set_modulation(modulation.mode);
set_freq_mod(freq, modulation.bandwidth, modulation.deviation);
int state = radio.transmit(payload, length);
if (state == RADIOLIB_ERR_NONE) {
Serial.println(F("Transimt successfully!"));

} else if (state == RADIOLIB_ERR_PACKET_TOO_LONG) {
// the supplied packet was longer than 256 bytes
Serial.println(F("too long!"));
}
else {
// some other error occurred
Serial.print(F("failed, code "));
Serial.println(state);
}
};

void SubGHZ::set_modulation(int mod) {
int state = RADIOLIB_ERR_NONE;
/* Module was on LoRA*/
if (_modulation == 2 && mod != 2) {
state = radio.beginFSK();
error_check("Begin FSK", state);
}

_modulation = mod;

if (!mod)
state = radio.setOOK(true);
else if (mod == 1)
state = radio.setOOK(false);
else {
state = radio.begin();
error_check("Begin LoRa", state);
return;
}
error_check("Set modulation", state);
}
59 changes: 59 additions & 0 deletions lib/SubGHZ/SubGHZ.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
#ifndef SUBGHZ_LIBRARY_H
#define SUBGHZ_LIBRARY_H

#include <Arduino.h>
#include <vector>
#include <RadioLib.h>

// enum ModulationMode { ASK, FSK };

typedef struct Modulation {
int mode;
float deviation;
float bandwidth;
} Modulation;

typedef struct Signal {
int modulation;
float frequency;
uint8_t *payload;
} Signal;

typedef struct SignalStrength {
float rssi;
}SignalStrength;

class SubGHZ {
private:
byte jammer_payload[8] = {0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF};
int _gdo0 = -1;
int _gdo2 = -1;
float _frequency;
float _bw;
float _deviation;
int _modulation;
bool initialized = false;
void error_check(const char *operation, int16_t status_code);
public:
SubGHZ(uint8_t sck, uint8_t miso, uint8_t mosi, uint8_t csn, uint8_t gdo0,
uint8_t gdo2);
~SubGHZ();
// Getter functions
float get_frequency() { return _frequency; };
float get_bw() { return _bw; };
float get_deviation() { return _deviation; };
int get_modulation() { return _modulation; };
void init_receive();
void stop_receive();
void set_freq_mod(float freq, float bw, float deviation);
SignalStrength scan_frequency(float freq, float bw = 200.0F,
float deviation = 47.0F);
SignalStrength scan_frequency();
void rec_signal(std::vector<byte> *orig_signal);
void send_signal(float freq, Modulation modulation, byte *payload, size_t length);
/// @brief Set modulation to ASK or FSK
/// @param mod 0 for ASK, 1 for FSK and 2 for LoRA
void set_modulation(int mod);
};

#endif
50 changes: 44 additions & 6 deletions lib/UI/gui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,9 @@ void Gui::set_selected_widget(int pos, bool selected) {
else if (nfc_felica_polling_result_page_visible())
nfc_felica_polling_result_page->set_selected(pos, selected);
else if (badusb_browser_visible())
badusb_payload_browser_page->set_selected(pos, selected);
file_browser_page->set_selected(pos, selected);
else if(subghz_page_visible())
subghz_page->set_selected(pos, selected);
}

/******************** NFC GUI FUNCTIONS ************************/
Expand Down Expand Up @@ -330,15 +332,51 @@ void Gui::nfc_cleanup() {
}

/******************** BadUSB FUNCTIONS ************************/
void Gui::init_badusb_browser_gui(std::list<std::string> *files) {
void Gui::init_file_browser_gui(const char *name, std::list<std::string> *files, bool _badusb) {
grid_visible = false;
if(_badusb)
badusb_visible = true;
else
subghz_browser_visible = true;
lower_limit = 1;
position_limit = 2; // TODO: To adjust dynamically
position_limit = files->size() - 1;
position_increment = 1;
badusb_payload_browser_page = new BadUSBPayloadBrowserPage(screen);
badusb_payload_browser_page->display(files);
file_browser_page = new FileBrowserPage(screen);
file_browser_page->display(name, files);
};

void Gui::init_subghz_gui(){
grid_visible = false;
position_limit = 3;
position_increment = 1;
subghz_page = new SubGHZPage(screen);
subghz_page->display();
};

void Gui::init_subghz_frequency_analyzer() {
grid_visible = false;
position_limit = 1;
position_increment = 0;
subghz_frequency_analyzer_page = new SubGHZFrequencyAnalyzerPage(screen);
subghz_frequency_analyzer_page->display();
}

void Gui::init_subghz_raw_record_ui() {
grid_visible = false;
position_limit = 1;
position_increment = 0;
subghz_raw_record_page = new SubGHZRAWRecordPage(screen);
subghz_raw_record_page->display();
}

void Gui::init_subghz_sender() {
grid_visible = false;
position_limit = 0;
position_increment = 0;
subghz_sender_page = new SubGHZSender(screen);
subghz_sender_page->display();
}

void Gui::click_element(int pos, void callback()) {
if (grid_visible)
grid->click(pos, callback);
Expand All @@ -357,7 +395,7 @@ void Gui::click_element(int pos, void callback()) {
else if (nfc_felica_polling_result_page_visible())
nfc_felica_polling_result_page->click(pos, callback);
else if (badusb_browser_visible())
badusb_payload_browser_page->click(pos, callback);
file_browser_page->click(pos, callback);
}

void Gui::up() {
Expand Down
Loading

0 comments on commit 64521ba

Please sign in to comment.