Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unified serial PCAPs #452

Merged
merged 12 commits into from
Jan 18, 2024
41 changes: 0 additions & 41 deletions .github/workflows/build_push.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ name: Build and Push
on:
workflow_dispatch:
pull_request:
types: [opened, reopened]

jobs:
# This workflow contains a single job called "build"
Expand Down Expand Up @@ -205,27 +204,6 @@ jobs:
run: |
mv ./esp32_marauder/build/esp32.esp32.esp32s2/esp32_marauder.ino.bin ./esp32_marauder/build/esp32.esp32.esp32s2/esp32_marauder.flipper.bin

- name: Configure SD Serial for Flipper Zero SD Serial
run: |
pwd
sed -i 's/^ \/\/#define WRITE_PACKETS_SERIAL/ #define WRITE_PACKETS_SERIAL/' esp32_marauder/configs.h

- name: Build Marauder SD Serial for Flipper Zero WiFi Dev Board
uses: ArminJo/[email protected]
with:
sketch-names: esp32_marauder.ino
arduino-board-fqbn: esp32:esp32:esp32s2:PartitionScheme=min_spiffs,FlashSize=4M,PSRAM=enabled
extra-arduino-cli-args: "--warnings none"

- name: Rename Marauder Flipper SD Serial bin
run: |
mv ./esp32_marauder/build/esp32.esp32.esp32s2/esp32_marauder.ino.bin ./esp32_marauder/build/esp32.esp32.esp32s2/esp32_marauder.flipper_sd_serial.bin

- name: Remove SD Serial
run: |
pwd
sed -i 's/^ #define WRITE_PACKETS_SERIAL/ \/\/#define WRITE_PACKETS_SERIAL/' esp32_marauder/configs.h

- name: Build Marauder for Flipper Zero Multi Board
uses: ArminJo/[email protected]
with:
Expand Down Expand Up @@ -464,13 +442,6 @@ jobs:
path: ./esp32_marauder/build/esp32.esp32.esp32s2/esp32_marauder.flipper.bin
retention-days: 5

- name: 'Upload Flipper SD Serial Artifact'
uses: actions/upload-artifact@v3
with:
name: esp32_marauder.flipper_sd_serial.bin
path: ./esp32_marauder/build/esp32.esp32.esp32s2/esp32_marauder.flipper_sd_serial.bin
retention-days: 5

- name: 'Upload MultiboardS3 Artifact'
uses: actions/upload-artifact@v3
with:
Expand Down Expand Up @@ -558,18 +529,6 @@ jobs:
asset_content_type: application/bin
if: github.event_name != 'pull_request'

- name: Upload Flipper SD Serial Asset
id: upload-flipper-sd-serial-release-asset
uses: actions/upload-release-asset@v1
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
with:
upload_url: ${{ steps.create_release.outputs.upload_url }}
asset_path: ./esp32_marauder/build/esp32.esp32.esp32s2/esp32_marauder.flipper_sd_serial.bin
asset_name: esp32_marauder.flipper_sd_serial.bin
asset_content_type: application/bin
if: github.event_name != 'pull_request'

- name: Upload MultiboardS3 Asset
id: upload-multiboardS3-release-asset
uses: actions/upload-release-asset@v1
Expand Down
191 changes: 93 additions & 98 deletions esp32_marauder/Buffer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,17 @@ Buffer::Buffer(){
bufB = (uint8_t*)malloc(BUF_SIZE);
}

void Buffer::createPcapFile(fs::FS* fs, String fn, bool log){
void Buffer::createFile(String name, bool is_pcap){
int i=0;
if (!log) {
if (is_pcap) {
do{
fileName = "/"+fn+"_"+(String)i+".pcap";
fileName = "/"+name+"_"+(String)i+".pcap";
i++;
} while(fs->exists(fileName));
}
else {
do{
fileName = "/"+fn+"_"+(String)i+".log";
fileName = "/"+name+"_"+(String)i+".log";
i++;
} while(fs->exists(fileName));
}
Expand All @@ -27,15 +27,15 @@ void Buffer::createPcapFile(fs::FS* fs, String fn, bool log){
file.close();
}

void Buffer::open(bool log){
void Buffer::open(bool is_pcap){
bufSizeA = 0;
bufSizeB = 0;

bufSizeB = 0;

writing = true;

if (!log) {
if (is_pcap) {
write(uint32_t(0xa1b2c3d4)); // magic number
write(uint16_t(2)); // major version number
write(uint16_t(4)); // minor version number
Expand All @@ -46,14 +46,35 @@ void Buffer::open(bool log){
}
}

void Buffer::close(fs::FS* fs){
if(!writing) return;
forceSave(fs);
writing = false;
Serial.println(text01);
void Buffer::openFile(String file_name, fs::FS* fs, bool serial, bool is_pcap) {
bool save_pcap = settings_obj.loadSetting<bool>("SavePCAP");
if (!save_pcap) {
this->fs = NULL;
this->serial = false;
writing = false;
return;
}
this->fs = fs;
this->serial = serial;
if (this->fs) {
createFile(file_name, is_pcap);
}
if (this->fs || this->serial) {
open(is_pcap);
} else {
writing = false;
}
}

void Buffer::pcapOpen(String file_name, fs::FS* fs, bool serial) {
openFile(file_name, fs, serial, true);
}

void Buffer::addPacket(uint8_t* buf, uint32_t len, bool log){
void Buffer::logOpen(String file_name, fs::FS* fs, bool serial) {
openFile(file_name, fs, serial, false);
}

void Buffer::add(const uint8_t* buf, uint32_t len, bool is_pcap){
// buffer is full -> drop packet
if((useA && bufSizeA + len >= BUF_SIZE && bufSizeB > 0) || (!useA && bufSizeB + len >= BUF_SIZE && bufSizeA > 0)){
//Serial.print(";");
Expand All @@ -74,7 +95,7 @@ void Buffer::addPacket(uint8_t* buf, uint32_t len, bool log){

microSeconds -= seconds*1000*1000; // e.g. 45200400 - 45*1000*1000 = 45200400 - 45000000 = 400us (because we only need the offset)

if (!log) {
if (is_pcap) {
write(seconds); // ts_sec
write(microSeconds); // ts_usec
write(len); // incl_len
Expand All @@ -84,6 +105,20 @@ void Buffer::addPacket(uint8_t* buf, uint32_t len, bool log){
write(buf, len); // packet payload
}

void Buffer::append(wifi_promiscuous_pkt_t *packet, int len) {
bool save_packet = settings_obj.loadSetting<bool>(text_table4[7]);
if (save_packet) {
add(packet->payload, len, true);
}
}

void Buffer::append(String log) {
bool save_packet = settings_obj.loadSetting<bool>(text_table4[7]);
if (save_packet) {
add((const uint8_t*)log.c_str(), log.length(), false);
}
}

void Buffer::write(int32_t n){
uint8_t buf[4];
buf[0] = n;
Expand All @@ -109,8 +144,9 @@ void Buffer::write(uint16_t n){
write(buf,2);
}

void Buffer::write(uint8_t* buf, uint32_t len){
void Buffer::write(const uint8_t* buf, uint32_t len){
if(!writing) return;
while(saving) delay(10);

if(useA){
memcpy(&bufA[bufSizeA], buf, len);
Expand All @@ -121,127 +157,86 @@ void Buffer::write(uint8_t* buf, uint32_t len){
}
}

void Buffer::save(fs::FS* fs){
if(saving) return; // makes sure the function isn't called simultaneously on different cores

// buffers are already emptied, therefor saving is unecessary
if((useA && bufSizeB == 0) || (!useA && bufSizeA == 0)){
//Serial.printf("useA: %s, bufA %u, bufB %u\n",useA ? "true" : "false",bufSizeA,bufSizeB); // for debug porpuses
return;
}

//Serial.println("saving file");

uint32_t startTime = millis();
uint32_t finishTime;

file = fs->open(fileName, FILE_APPEND);
if (!file) {
Serial.println(text02 + fileName+"'");
//useSD = false;
return;
}

saving = true;

uint32_t len;

if(useA){
file.write(bufB, bufSizeB);
len = bufSizeB;
bufSizeB = 0;
}
else{
file.write(bufA, bufSizeA);
len = bufSizeA;
bufSizeA = 0;
}

file.close();

finishTime = millis() - startTime;

//Serial.printf("\n%u bytes written for %u ms\n", len, finishTime);

saving = false;

}

void Buffer::forceSave(fs::FS* fs){
uint32_t len = bufSizeA + bufSizeB;
if(len == 0) return;

void Buffer::saveFs(){
file = fs->open(fileName, FILE_APPEND);
if (!file) {
Serial.println(text02+fileName+"'");
//useSD = false;
return;
}

saving = true;
writing = false;

if(useA){

if(bufSizeB > 0){
file.write(bufB, bufSizeB);
bufSizeB = 0;
}

if(bufSizeA > 0){
file.write(bufA, bufSizeA);
bufSizeA = 0;
}

} else {

if(bufSizeA > 0){
file.write(bufA, bufSizeA);
bufSizeA = 0;
}

if(bufSizeB > 0){
file.write(bufB, bufSizeB);
bufSizeB = 0;
}

}

file.close();

//Serial.printf("saved %u bytes\n",len);

saving = false;
writing = true;
}

void Buffer::forceSaveSerial() {
uint32_t len = bufSizeA + bufSizeB;
if(len == 0) return;

saving = true;
writing = false;
void Buffer::saveSerial() {
// Saves to main console UART, user-facing app will ignore these markers
// Uses / and ] in markers as they are illegal characters for SSIDs
const char* mark_begin = "[BUF/BEGIN]";
const size_t mark_begin_len = strlen(mark_begin);
const char* mark_close = "[BUF/CLOSE]";
const size_t mark_close_len = strlen(mark_close);

// Additional buffer and memcpy's so that a single Serial.write() is called
// This is necessary so that other console output isn't mixed into buffer stream
uint8_t* buf = (uint8_t*)malloc(mark_begin_len + bufSizeA + bufSizeB + mark_close_len);
uint8_t* it = buf;
memcpy(it, mark_begin, mark_begin_len);
it += mark_begin_len;

if(useA){
if(bufSizeB > 0){
Serial1.write(bufB, bufSizeB);
bufSizeB = 0;
memcpy(it, bufB, bufSizeB);
it += bufSizeB;
}
if(bufSizeA > 0){
Serial1.write(bufA, bufSizeA);
bufSizeA = 0;
memcpy(it, bufA, bufSizeA);
it += bufSizeA;
}
} else {
if(bufSizeA > 0){
Serial1.write(bufA, bufSizeA);
bufSizeA = 0;
memcpy(it, bufA, bufSizeA);
it += bufSizeA;
}
if(bufSizeB > 0){
Serial1.write(bufB, bufSizeB);
bufSizeB = 0;
memcpy(it, bufB, bufSizeB);
it += bufSizeB;
}
}

memcpy(it, mark_close, mark_close_len);
it += mark_close_len;
Serial.write(buf, it - buf);
free(buf);
}

void Buffer::save() {
saving = true;

if((bufSizeA + bufSizeB) == 0){
saving = false;
return;
}

if(this->fs) saveFs();
if(this->serial) saveSerial();

bufSizeA = 0;
bufSizeB = 0;

saving = false;
writing = true;
}
Loading
Loading