Skip to content

Commit

Permalink
added Panasonic ir protocol, added serial cmd prompt and more aliases,
Browse files Browse the repository at this point in the history
  • Loading branch information
eadmaster committed Sep 14, 2024
1 parent 515017e commit c541c45
Show file tree
Hide file tree
Showing 8 changed files with 81 additions and 26 deletions.
3 changes: 2 additions & 1 deletion src/core/menu_items/ScriptsMenu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ void ScriptsMenu::optionsMenu() {
String fileName = String(file2.name());
if( ! fileName.endsWith(".js") && ! fileName.endsWith(".bjs")) continue;
// else append to the choices
options.push_back({file2.name(), [=]() { run_bjs_script_headless(*fs, file2.path()); }});
String entry_title = String(file2.name()); entry_title = entry_title.substring(0, entry_title.lastIndexOf(".")); // remove the extension
options.push_back({entry_title.c_str(), [=]() { run_bjs_script_headless(*fs, file2.path()); }});
file2 = root.openNextFile();
}
file2.close();
Expand Down
29 changes: 17 additions & 12 deletions src/core/passwords.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,18 @@



String xorEncryptDecryptMD5(const String &input, const String &password) {
uint8_t md5Hash[16];
String xorEncryptDecryptMD5(const String &input, const String &password, const int MD5_PASSES) {

MD5Builder md5;
md5.begin();
md5.add(password);
md5.calculate();
String hash = password;

for (int i = 0; i < MD5_PASSES; i++) {
md5.begin();
md5.add(hash);
md5.calculate();
}

uint8_t md5Hash[16];
md5.getBytes(md5Hash); // Store MD5 hash in the output array

String output = input; // Copy input to output for modification
Expand Down Expand Up @@ -91,14 +96,14 @@ String readDecryptedFile(FS &fs, String filepath) {
if(line.startsWith("Filetype:") && !line.endsWith("Bruce Encrypted File")) unsupported_params = true;
if(line.startsWith("Algo:") && !line.endsWith("XOR")) unsupported_params = true;
if(line.startsWith("KeyDerivationAlgo:") && !line.endsWith("MD5")) unsupported_params = true;
if(line.startsWith("KeyDerivationPasses:") && !line.endsWith("1")) unsupported_params = true;
if(line.startsWith("KeyDerivationPasses:") && !line.endsWith("10")) unsupported_params = true; // TODO: parse
if(line.startsWith("Data:")) cypertextData = line.substring(strlen("Data:"));
}

cyphertextFile.close();

if(unsupported_params || cypertextData.length() == 0) {
Serial.println("err: invalid Encrypted file (altered?)");
displayError("err: invalid Encrypted file (altered?)");
return "";
}

Expand Down Expand Up @@ -139,12 +144,12 @@ String readDecryptedFile(FS &fs, String filepath) {
//Serial.println(cypertextData);
//Serial.println(cypertextDataDec);

plaintext = xorEncryptDecryptMD5(cypertextDataDec, cachedPassword);
plaintext = xorEncryptDecryptMD5(cypertextDataDec, cachedPassword, 10);

if(!isValidAscii(plaintext)) {
// invalidate cached password -> will ask again on the next try
cachedPassword = "";
Serial.println("err: decryption failed (invalid password?)");
displayError("decryption failed (invalid password?)");
//Serial.println(plaintext);
return "";
}
Expand All @@ -157,18 +162,18 @@ String readDecryptedFile(FS &fs, String filepath) {


String encryptString(String& plaintext, const String& password_str) {
String dataStr = xorEncryptDecryptMD5(plaintext, password_str);
String dataStr = xorEncryptDecryptMD5(plaintext, password_str, 10);
String dataStrHex = "";

for (size_t i = 0; i < dataStr.length(); i++)
dataStrHex += String(dataStr[i], HEX) + " ";
dataStrHex.toUpperCase();
dataStrHex.trim();

String out = "Filetype: Bruce Encrypted File\nVersion 1\n";
String out = "Filetype: Bruce Encrypted File\nVersion: 1\n";
out += "Algo: XOR\n"; // TODO: add AES
out += "KeyDerivationAlgo: MD5\n";
out += "KeyDerivationPasses: 1\n"; // TODO: allow more passes
out += "KeyDerivationPasses: 10\n";
out += "Data: " + dataStrHex + "\n";

return out;
Expand Down
2 changes: 2 additions & 0 deletions src/core/sd_functions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -635,6 +635,7 @@ String loopSD(FS &fs, bool filePicker, String allowed_ext) {
String plaintext = readDecryptedFile(fs, filepath);
if(plaintext.length()==0) return displayError("Decryption failed"); // file is too big or cannot read, or cancelled
// else
plaintext.trim(); // remove newlines
key_input_from_string(plaintext);
}});
}
Expand All @@ -644,6 +645,7 @@ String loopSD(FS &fs, bool filePicker, String allowed_ext) {
String plaintext = readDecryptedFile(fs, filepath);
delay(200);
if(plaintext.length()==0) return displayError("Decryption failed");
plaintext.trim(); // remove newlines
//if(plaintext.length()<..)
displaySuccess(plaintext);
while(!checkAnyKeyPress()) delay(100);
Expand Down
25 changes: 18 additions & 7 deletions src/core/serialcmds.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ bool is_free_gpio_pin(int pin_no ){
usable_pins.insert(usable_pins.end(), {
1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, // GPIO1 to GPIO25
33, // GPIO33
38, 39, 40, 41, 42, 43, 44, // GPIO38 to GPIO44
38, 39, 40, 41, 42, // GPIO38 to GPIO42
47, 48 // GPIO47 to GPIO48
});
#endif
Expand Down Expand Up @@ -163,6 +163,8 @@ void handleSerialCommands() {
if(r) setup_gpio(); // temp fix for menu inf. loop
else Serial.println("failed: " + cmd_str);

Serial.print("$ "); // prompt

returnToMenu = true; // forced menu redrawn
}

Expand All @@ -178,7 +180,8 @@ bool processSerialCommand(String cmd_str) {
}

// check cmd aliases
if(cmd_str == "ls") cmd_str.replace("ls", "storage list /");
if(cmd_str == "ls") cmd_str = "storage list ";
if(cmd_str == "dir") cmd_str = "storage list ";
if(cmd_str.startsWith("ls ")) cmd_str = "storage list " + cmd_str.substring(strlen("ls "));
if(cmd_str.startsWith("dir ")) cmd_str = "storage list " + cmd_str.substring(strlen("dir "));
if(cmd_str.startsWith("rm ")) cmd_str = "storage remove " + cmd_str.substring(strlen("rm "));
Expand All @@ -195,6 +198,8 @@ bool processSerialCommand(String cmd_str) {
if(cmd_str.startsWith("decrypt ")) cmd_str = "crypto decrypt_from_file " + cmd_str.substring(strlen("decrypt "));
if(cmd_str.startsWith("encrypt ")) cmd_str = "crypto encrypt_to_file " + cmd_str.substring(strlen("encrypt "));
if(cmd_str.startsWith("run ")) cmd_str = "js " + cmd_str.substring(strlen("run "));
if(cmd_str == "poweroff") cmd_str = "power off";
if(cmd_str == "reboot") cmd_str = "power reboot";

// case-insensitive matching only in some cases -- TODO: better solution for this
if(cmd_str.indexOf("from_file ") == -1 && cmd_str.indexOf("storage ") == -1 && cmd_str.indexOf("settings ") && cmd_str.indexOf("js "))
Expand Down Expand Up @@ -625,6 +630,7 @@ bool processSerialCommand(String cmd_str) {
startWebUi(true); // MEMO: will quit when checkEscPress
return true;
}
//TODO: if(cmd_str == "webui stop" ) {

/*
// WIP https://github.com/pr3y/Bruce/issues/162#issuecomment-2308788115
Expand Down Expand Up @@ -944,7 +950,7 @@ bool processSerialCommand(String cmd_str) {
// storage list BruceRF
String filepath = cmd_str.substring(strlen("storage list "));
filepath.trim();
if(filepath.length()==0) return false; // missing arg
//if(filepath.length()==0) return false; // missing arg
if(!filepath.startsWith("/")) filepath = "/" + filepath; // add "/" if missing
FS* fs = NULL;
if(SD.exists(filepath)) fs = &SD;
Expand Down Expand Up @@ -1167,12 +1173,17 @@ bool processSerialCommand(String cmd_str) {
}
}


/* WIP
if(cmd_str.startsWith("bt scan")) {
if(cmd_str == "wifi off") {
wifiDisconnect();
return true;
}

if(cmd_str.startsWith("wifi scan")) {

/* WIP
if(cmd_str.startsWith("wifi scan") || cmd_str.startsWith("scanap") {
}
if(cmd_str.startsWith("bt scan")) {
}
if(cmd_str.startsWith("rtl433")) {
Expand Down
24 changes: 24 additions & 0 deletions src/modules/ir/TV-B-Gone.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -366,6 +366,8 @@ bool txIrFile(FS *fs, String filepath) {
sendSamsungCommand(address, command);
} else if (protocol.startsWith("SIRC")) {
sendSonyCommand(address, command);
} else if (protocol.startsWith("Panasonic")) {
sendPanasonicCommand(address, command);
}
protocol = "";
address = "";
Expand Down Expand Up @@ -435,6 +437,7 @@ void otherIRcodes() {
else if(selected_code.protocol=="RC6") sendRC6Command(selected_code.address, selected_code.command);
else if(selected_code.protocol.startsWith("Samsung")) sendSamsungCommand(selected_code.address, selected_code.command);
else if(selected_code.protocol=="SIRC") sendSonyCommand(selected_code.address, selected_code.command);
else if(selected_code.protocol=="Panasonic") sendPanasonicCommand(selected_code.address, selected_code.command);
}
return;
// no need to proceed, go back
Expand Down Expand Up @@ -502,6 +505,7 @@ void otherIRcodes() {
if(codes[i].protocol.startsWith("RC6")) options.push_back({ codes[i].name.c_str(), [=](){ sendRC6Command(codes[i].address, codes[i].command); addToRecentCodes(codes[i]); }});
if(codes[i].protocol.startsWith("Samsung")) options.push_back({ codes[i].name.c_str(), [=](){ sendSamsungCommand(codes[i].address, codes[i].command); addToRecentCodes(codes[i]); }});
if(codes[i].protocol=="SIRC") options.push_back({ codes[i].name.c_str(), [=](){ sendSonyCommand(codes[i].address, codes[i].command); addToRecentCodes(codes[i]); }});
if(codes[i].protocol=="Panasonic") options.push_back({ codes[i].name.c_str(), [=](){ sendPanasonicCommand(codes[i].address, codes[i].command); addToRecentCodes(codes[i]); }});
}
options.push_back({ "Main Menu" , [&](){ exit=true; }});
databaseFile.close();
Expand Down Expand Up @@ -590,6 +594,26 @@ void sendSonyCommand(String address, String command) {
digitalWrite(IrTx, LED_OFF);
}

void sendPanasonicCommand(String address, String command) {
IRsend irsend(IrTx); // Set the GPIO to be used to sending the message.
irsend.begin();
displayRedStripe("Sending..",TFT_WHITE,FGCOLOR);
uint8_t first_zero_byte_pos = address.indexOf("00", 2);
if(first_zero_byte_pos!=-1) address = address.substring(0, first_zero_byte_pos);
address.replace(" ", "");
command.replace(" ", "");
// TODO: needs endianess fix?
uint16_t addressValue = strtoul(address.c_str(), nullptr, 16);
uint32_t commandValue = strtoul(command.c_str(), nullptr, 16);
//Serial.println(addressValue);
//Serial.println(commandValue);
irsend.sendPanasonic(addressValue, commandValue, 48, 10);
// sendPanasonic(const uint16_t address, const uint32_t data, const uint16_t nbits = kPanasonicBits, const uint16_t repeat = kNoRepeat);
delay(20);
Serial.println("Sent Panasonic Command");
digitalWrite(IrTx, LED_OFF);
}

void sendRawCommand(int frequency, String rawData) {
IRsend irsend(IrTx); // Set the GPIO to be used to sending the message.
irsend.begin();
Expand Down
1 change: 1 addition & 0 deletions src/modules/ir/TV-B-Gone.h
Original file line number Diff line number Diff line change
Expand Up @@ -71,5 +71,6 @@ void sendRC5Command(String address, String command);
void sendRC6Command(String address, String command);
void sendSamsungCommand(String address, String command);
void sendSonyCommand(String address, String command);
void sendPanasonicCommand(String address, String data);
void otherIRcodes();
bool txIrFile(FS *fs, String filepath);
21 changes: 16 additions & 5 deletions src/modules/ir/ir_read.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,14 @@ void IrRead::append_to_file_str(String btn_name) {
strDeviceContent += "protocol: SIRC\n";
break;
}
case decode_type_t::PANASONIC:
{
strDeviceContent += "protocol: Panasonic\n";
// not supported by flipper
//strDeviceContent += "value: " + uint32ToString( (uint32_t )results.value) + "\n";
// cast from uint64_t
break;
}
case decode_type_t::NEC:
{
// check address and command ranges to find the exact protocol
Expand All @@ -233,7 +241,8 @@ void IrRead::append_to_file_str(String btn_name) {
// TODO: more protocols?
default:
{
Serial.println("unsupported protocol, try raw mode");
Serial.print("unsupported protocol, try raw mode: ");
Serial.println(results.decode_type);
return;
}
}
Expand Down Expand Up @@ -308,9 +317,11 @@ String IrRead::loop_headless(int max_loops) {
Serial.println("# decoding failed, try raw mode");
return "";
}

// TODO: check results.overflow, results.repeat

String r = "Filetype: Bruce IR File\n";
r += "Version 1\n";
String r = "Filetype: IR signals file\n";
r += "Version: 1\n";
r += "#\n";
r += "#\n";

Expand All @@ -337,8 +348,8 @@ bool IrRead::write_file(String filename, FS* fs) {
return false;
}

file.println("Filetype: Bruce IR File");
file.println("Version 1");
file.println("Filetype: IR signals file");
file.println("Version: 1");
file.println("#");
file.println("# " + filename);
file.print(strDeviceContent);
Expand Down
2 changes: 1 addition & 1 deletion src/modules/ir/ir_read.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class IrRead {
bool _read_signal = false;
decode_results results;
uint16_t* rawcode;
int raw_data_len;
uint16_t raw_data_len;
int signals_read = 0;
String strDeviceContent = "";
bool headless = false;
Expand Down

0 comments on commit c541c45

Please sign in to comment.