If flash init failed, format it automatically & save preferences.json

This commit is contained in:
Spacehuhn 2022-07-14 00:17:22 +02:00
parent 69a0e36bd8
commit 5f4543ca0e
6 changed files with 74 additions and 30 deletions

View File

@ -24,15 +24,29 @@ Mode mode;
void setup() {
// Initialize all the things
debug_init();
msc::init();
selector::init();
led::init();
preferences::load();
// Initialize memory and ceck for problems
if (!msc::init()) {
format::start(); // Format the drive
// If it still fails, blink red LED
if (!msc::init()) {
while (true) {
led::setColor(255, 0, 0);
delay(500);
led::setColor(0, 0, 0);
delay(500);
}
}
}
// Read the mode from the toggle switch position
mode = selector::read() ? SETUP : ATTACK;
// Preferences
preferences::load();
led::setEnable(preferences::ledEnabled());
keyboard::setLocale(locale::get(preferences::getDefaultLayout().c_str()));
keyboard::setID(preferences::getHidVid(), preferences::getHidPid(), preferences::getHidRev());
@ -46,9 +60,13 @@ void setup() {
if (preferences::mscEnabled() || (mode == SETUP)) msc::enableDrive();
// Format Flash
if(mode == SETUP && preferences::getFormat()) {
led::setColor(255,255,255);
if ((mode == SETUP) && preferences::getFormat()) {
led::setColor(255, 255, 255);
format::start(preferences::getDriveName().c_str());
}
// Create preferences file if it doesn't exist yet
if (!msc::exists(PREFERENCES_PATH)) {
preferences::save();
}

View File

@ -20,4 +20,7 @@
// ===== Parser Settings ===== //
#define CASE_SENSETIVE false
#define DEFAULT_SLEEP 5
#define DEFAULT_SLEEP 5
// ===== Other Stuff ====== //
#define PREFERENCES_PATH "preferences.json"

View File

@ -4,5 +4,5 @@
namespace format {
bool start(const char* drive_name);
bool start(const char* drive_name = "USB Nova");
}

View File

@ -66,9 +66,18 @@ namespace msc {
}
// ===== PUBLIC ===== //
void init() {
flash.begin();
fatfs.begin(&flash);
bool init() {
if(!flash.begin()) {
debugln("Couldn't find flash chip!");
return false;
}
if(!fatfs.begin(&flash)) {
debugln("Couldn't mount flash!");
return false;
}
return true;
}
void setID(const char* vid, const char* pid, const char* rev) {
@ -88,8 +97,12 @@ namespace msc {
fs_changed = false;
return tmp;
}
bool exists(const char* filename) {
return fatfs.exists(filename);
}
bool open(const char* path, bool write) {
bool open(const char* path) {
debug("Open new file: ");
debugln(path);
@ -111,7 +124,7 @@ namespace msc {
file_stack.push(file_element);
// Open file and return whether it was successful
return file.open(path, write ? (O_RDWR | O_CREAT) : O_RDONLY);
return file.open(path);
}
bool openNextFile() {
@ -210,9 +223,17 @@ namespace msc {
return in_line;
}
size_t write(const char* buffer, size_t len) {
if (!file.isOpen()) return 0;
size_t write(const char* path, const char* buffer, size_t len) {
FatFile wfile;
wfile.open(path, (O_RDWR | O_CREAT));
if (!wfile.isOpen()) return 0;
return file.write(buffer, len);
size_t written = wfile.write(buffer, len);
wfile.close();
debug("Wrote ");
debugln(written);
return written;
}
}

View File

@ -5,14 +5,15 @@
#include "SdFat.h"
namespace msc {
void init();
bool init();
void setID(const char* vid, const char* pid, const char* rev);
void enableDrive();
bool changed();
bool exists(const char* filename);
bool open(const char* path, bool write = false);
bool open(const char* path);
bool openNextFile();
void close();
@ -24,5 +25,5 @@ namespace msc {
size_t readLine(char* buffer, size_t len);
bool getInLine();
size_t write(const char* buffer, size_t len);
size_t write(const char* path, const char* buffer, size_t len);
}

View File

@ -8,6 +8,8 @@
#include <ArduinoJson.h>
#include "../msc/msc.h"
#define JSON_SIZE 1536
namespace preferences {
// ========== PRIVATE ========= //
bool enable_msc { false };
@ -69,15 +71,16 @@ namespace preferences {
// ======== PUBLIC ======== //
void load() {
// Read config file
char buffer[1024];
StaticJsonDocument<1024> config_doc;
char* buffer = (char*)malloc(JSON_SIZE);
DynamicJsonDocument config_doc(JSON_SIZE);
// Open the file and read it into a buffer
if (!msc::open("preferences.json")) return;
msc::read(buffer, sizeof(buffer));
if (!msc::open(PREFERENCES_PATH)) return;
msc::read(buffer, JSON_SIZE);
// Deserialize the JSON document
DeserializationError error = deserializeJson(config_doc, buffer);
free(buffer);
// Test if parsing succeeds.
if (error) {
@ -171,7 +174,7 @@ namespace preferences {
void save() {
// Create a new JSON document (and string buffer)
StaticJsonDocument<1024> json_doc;
DynamicJsonDocument json_doc(JSON_SIZE);
// JsonObject json_obj = json_doc.as<JsonObject>();
std::string json_str = { "" };
@ -180,16 +183,14 @@ namespace preferences {
// Serialize JSON to buffer
serializeJsonPretty(json_doc, json_str);
json_doc.clear();
// Write the buffer to file
msc::open("preferences.json", true);
debugln(json_str.length());
debugln(msc::write(json_str.c_str(), json_str.length()));
msc::close();
// Write the buffer to file (and print results)
debugln(json_str.c_str());
msc::write(PREFERENCES_PATH, json_str.c_str(), json_str.length());
//debugln(json_str.length());
//debugln(json_str.c_str());
debugln("Saved preferences.json");
debug("Saved ");
debugln(PREFERENCES_PATH);
}
bool mscEnabled() {