Skip to content

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

rename sdCardManager to sdCardShell and move it, add shells to provider

Geo7d058efa9623
diff --git a/src/Providers/DependencyProvider.cpp b/src/Providers/DependencyProvider.cpp
index 318a8958..7c0cb47e 100644
--- a/src/Providers/DependencyProvider.cpp
+++ b/src/Providers/DependencyProvider.cpp
@@ -33,16 +33,17 @@ DependencyProvider::DependencyProvider(ITerminalView &terminalView, IDeviceView
commandHistoryManager(),
binaryAnalyzeManager(spiService, terminalView, terminalInput),
userInputManager(terminalView, terminalInput, argTransformer),
- sdCardManager(sdService, terminalView, terminalInput, argTransformer),
+ sdCardShell(sdService, terminalView, terminalInput, argTransformer),
+ universalRemoteShell(terminalView, terminalInput, infraredService, argTransformer, userInputManager),
horizontalSelector(deviceView, deviceInput),
terminalTypeConfigurator(horizontalSelector),
uartController(terminalView, terminalInput, deviceInput, uartService, sdService, hdUartService, argTransformer, userInputManager),
i2cController(terminalView, terminalInput, i2cService, argTransformer, userInputManager),
oneWireController(terminalView, terminalInput, oneWireService, argTransformer, userInputManager),
- infraredController(terminalView, terminalInput, infraredService, argTransformer, userInputManager),
+ infraredController(terminalView, terminalInput, infraredService, argTransformer, userInputManager, universalRemoteShell),
utilityController(terminalView, deviceView, terminalInput, pinService, userInputManager, argTransformer),
hdUartController(terminalView, terminalInput, deviceInput, hdUartService, uartService, argTransformer, userInputManager),
- spiController(terminalView, terminalInput, spiService, sdService, argTransformer, userInputManager, binaryAnalyzeManager, sdCardManager),
+ spiController(terminalView, terminalInput, spiService, sdService, argTransformer, userInputManager, binaryAnalyzeManager, sdCardShell),
jtagController(terminalView, terminalInput, jtagService, userInputManager),
twoWireController(terminalView, terminalInput, userInputManager, twoWireService),
threeWireController(terminalView, terminalInput, userInputManager, threeWireService, argTransformer),
@@ -112,7 +113,10 @@ WebRequestTransformer &DependencyProvider::getWebRequestTransformer() { return w
CommandHistoryManager &DependencyProvider::getCommandHistoryManager() { return commandHistoryManager; }
UserInputManager &DependencyProvider::getUserInputManager() { return userInputManager; }
BinaryAnalyzeManager &DependencyProvider::getBinaryAnalyzeManager() { return binaryAnalyzeManager; }
-SdCardManager &DependencyProvider::getSdCardManager() { return sdCardManager; }
+
+// Shells
+SdCardShell &DependencyProvider::getSdCardShell() { return sdCardShell; }
+UniversalRemoteShell &DependencyProvider::getUniversalRemoteShell() { return universalRemoteShell; }
// Selectors
HorizontalSelector &DependencyProvider::getHorizontalSelector() { return horizontalSelector; }
diff --git a/src/Providers/DependencyProvider.h b/src/Providers/DependencyProvider.h
index 3f72a6f8..bf5dd870 100644
--- a/src/Providers/DependencyProvider.h
+++ b/src/Providers/DependencyProvider.h
@@ -53,7 +53,8 @@ and injecting shared instances of core components
#include "Managers/CommandHistoryManager.h"
#include "Managers/BinaryAnalyzeManager.h"
#include "Managers/UserInputManager.h"
-#include "Managers/SdCardManager.h"
+#include "Shells/SdCardShell.h"
+#include "Shells/UniversalRemoteShell.h"
#include "Config/TerminalTypeConfigurator.h"
class DependencyProvider
@@ -122,7 +123,10 @@ public:
CommandHistoryManager &getCommandHistoryManager();
UserInputManager &getUserInputManager();
BinaryAnalyzeManager &getBinaryAnalyzeManager();
- SdCardManager &getSdCardManager();
+
+ // Shells
+ SdCardShell &getSdCardShell();
+ UniversalRemoteShell &getUniversalRemoteShell();
// Selectors
HorizontalSelector &getHorizontalSelector();
@@ -191,7 +195,10 @@ private:
CommandHistoryManager commandHistoryManager;
UserInputManager userInputManager;
BinaryAnalyzeManager binaryAnalyzeManager;
- SdCardManager sdCardManager;
+
+ // Shells
+ SdCardShell sdCardShell;
+ UniversalRemoteShell universalRemoteShell;
// Selectors
HorizontalSelector horizontalSelector;
diff --git a/src/Shells/SdCardShell.cpp b/src/Shells/SdCardShell.cpp
new file mode 100644
index 00000000..20dbad4c
--- /dev/null
+++ b/src/Shells/SdCardShell.cpp
@@ -0,0 +1,352 @@
+#include "SdCardShell.h"
+
+SdCardShell::SdCardShell(SdService& sdService, ITerminalView& view, IInput& input, ArgTransformer& argTransformer)
+ : sd(sdService), terminalView(view), terminalInput(input), currentDir("/"), argTransformer(argTransformer) {}
+
+void SdCardShell::run() {
+ terminalView.println("- SD Shell: Type 'help' for commands. Type 'exit' to quit.");
+
+ while (true) {
+ terminalView.print(currentDir + " $ ");
+ std::string input = readLine();
+
+ if (input.empty()) continue;
+ if (input == "exit") break;
+
+ executeCommand(input);
+ }
+
+ terminalView.println("- Exiting SD shell.\n");
+}
+
+void SdCardShell::executeCommand(const std::string& input) {
+ std::istringstream iss(input);
+ std::string cmd;
+ iss >> cmd;
+
+ if (cmd == "ls") cmdLs();
+ else if (cmd == "cd") cmdCd(iss);
+ else if (cmd == "mkdir") cmdMkdir(iss);
+ else if (cmd == "touch") cmdTouch(iss);
+ else if (cmd == "rm") cmdRm(iss);
+ else if (cmd == "cat") cmdCat(iss);
+ else if (cmd == "echo") cmdEcho(iss);
+ else if (cmd == "help") cmdHelp();
+ else terminalView.println("Unknown command: " + cmd);
+}
+
+void SdCardShell::cmdLs() {
+ auto files = sd.listElementsCached(currentDir);
+
+ for (const auto& f : files) {
+ std::string fullPath = currentDir;
+ if (fullPath.back() != '/') fullPath += '/';
+ fullPath += f;
+
+ if (sd.isDirectory(fullPath)) {
+ terminalView.println(" πŸ“ " + f);
+ } else {
+ std::string ext = sd.getFileExt(f);
+ std::string icon;
+
+ std::transform(ext.begin(), ext.end(), ext.begin(), ::tolower);
+
+ if (ext == "txt" || ext == "md" || ext == "log" || ext == "csv" || ext == "pdf") {
+ icon = " πŸ“";
+ } else if (ext == "png" || ext == "jpg" || ext == "jpeg" || ext == "bmp" || ext == "gif" || ext == "webp") {
+ icon = " πŸ–ΌοΈ ";
+ } else if (ext == "mp3" || ext == "wav" || ext == "ogg" || ext == "flac" || ext == "m4a") {
+ icon = " 🎡";
+ } else if (ext == "mp4" || ext == "avi" || ext == "mov" || ext == "mkv" || ext == "webm") {
+ icon = " 🎞️ ";
+ } else if (ext == "zip" || ext == "rar" || ext == "7z" || ext == "tar" || ext == "gz") {
+ icon = " πŸ“¦";
+ } else if (ext == "ino" || ext == "cpp" || ext == "c" || ext == "h" ||
+ ext == "py" || ext == "js" || ext == "ts" || ext == "html" ||
+ ext == "css" || ext == "json" || ext == "xml" || ext == "sh") {
+ icon = " πŸ’»";
+ } else if (ext == "bin") {
+ icon = " 🧾";
+ } else {
+ icon = " πŸ“„"; // Default
+ }
+
+ terminalView.println(icon + " " + f);
+ }
+ }
+}
+
+void SdCardShell::cmdCd(std::istringstream& iss) {
+ std::string arg;
+ iss >> arg;
+
+ if (arg.empty()) {
+ currentDir = "/";
+ return;
+ }
+
+ std::string newPath;
+
+ if (arg[0] == '/') {
+ newPath = normalizePath(arg);
+ } else {
+ newPath = resolveRelativePath(currentDir, arg); // ../.., images/ etc.
+ }
+
+ if (sd.isDirectory(newPath)) {
+ currentDir = newPath;
+ } else {
+ terminalView.println("Directory not found: " + newPath);
+ }
+}
+
+void SdCardShell::cmdMkdir(std::istringstream& iss) {
+ std::string name;
+ iss >> name;
+ if (name.empty()) {
+ terminalView.println("Usage: mkdir <dirname>");
+ return;
+ }
+ std::string fullPath = currentDir + "/" + name;
+ if (sd.ensureDirectory(fullPath)) {
+ terminalView.println("Directory created: " + name);
+ sd.removeCachedPath(currentDir); // changed, remove to reload
+ }
+ else terminalView.println("Failed to create directory.");
+}
+
+void SdCardShell::cmdTouch(std::istringstream& iss) {
+ std::string name;
+ iss >> name;
+ if (name.empty()) {
+ terminalView.println("Usage: touch <filename>");
+ return;
+ }
+ std::string fullPath = currentDir + "/" + name;
+ if (sd.writeFile(fullPath, "")) {
+ terminalView.println("File created: " + name);
+ sd.removeCachedPath(currentDir); // changed, remove to reload
+ }
+ else terminalView.println("Failed to create file.");
+}
+
+void SdCardShell::cmdRm(std::istringstream& iss) {
+ std::string name;
+ iss >> name;
+ if (name.empty()) {
+ terminalView.println("Usage: rm <file_or_dir>");
+ return;
+ }
+ std::string fullPath = currentDir + "/" + name;
+ if (sd.isFile(fullPath)) {
+ if (sd.deleteFile(fullPath)) {
+ terminalView.println("File deleted.");
+ sd.removeCachedPath(currentDir); // changed, remove to reload
+ }
+ else terminalView.println("Failed to delete file.");
+
+ } else if (sd.isDirectory(fullPath)) {
+ if (sd.deleteDirectory(fullPath)) {
+ terminalView.println("Folder deleted.");
+ sd.removeCachedPath(currentDir); // changed, remove to reload
+ }
+ } else {
+ terminalView.println("Path not found.");
+ }
+}
+
+void SdCardShell::cmdHelp() {
+ terminalView.println(" Available commands:");
+ terminalView.println(" ls : List files in the directory");
+ terminalView.println(" cd <dir> : Change directory");
+ terminalView.println(" cat <file> : Show content of a text file");
+ terminalView.println(" echo TEXT > file : Overwrite a file with TEXT");
+ terminalView.println(" echo TEXT >> file : Append TEXT to the file");
+ terminalView.println(" mkdir <dir> : Create a new directory");
+ terminalView.println(" touch <file> : Create an empty file");
+ terminalView.println(" rm <file/dir> : Delete a file or directory");
+ terminalView.println(" help : Show this help message");
+ terminalView.println(" exit : Exit SD shell");
+}
+
+void SdCardShell::cmdCat(std::istringstream& iss) {
+ constexpr size_t MAX_DISPLAY_CHARS = 4096;
+
+ std::string filename;
+ iss >> filename;
+ if (filename.empty()) {
+ terminalView.println("Usage: cat <filename>");
+ return;
+ }
+
+ std::string fullPath = currentDir;
+ if (fullPath.back() != '/') fullPath += '/';
+ fullPath += filename;
+
+ if (!sd.isFile(fullPath)) {
+ terminalView.println("File not found: " + filename);
+ return;
+ }
+
+ std::string content = sd.readFileChunk(fullPath, 0, MAX_DISPLAY_CHARS);
+ terminalView.println(content);
+ if (content.length() == MAX_DISPLAY_CHARS) {
+ terminalView.println("\n... (file too long)");
+ }
+}
+
+void SdCardShell::cmdEcho(std::istringstream& iss) {
+ std::vector<std::string> tokens;
+ std::string word;
+
+ while (iss >> word) {
+ tokens.push_back(word);
+ }
+
+ if (tokens.size() < 3) {
+ terminalView.println("Usage: echo <text> > <filename> or >> <filename>");
+ return;
+ }
+
+ // Find > or >> position
+ size_t redirPos = tokens.size();
+ std::string redir;
+ for (size_t i = 0; i < tokens.size(); ++i) {
+ if (tokens[i] == ">" || tokens[i] == ">>") {
+ redir = tokens[i];
+ redirPos = i;
+ }
+ }
+
+ if (redir.empty() || redirPos == tokens.size() - 1) {
+ terminalView.println("Usage: echo <text> > <filename> or >> <filename>");
+ return;
+ }
+
+ // Text
+ std::string text;
+ for (size_t i = 0; i < redirPos; ++i) {
+ if (!text.empty()) text += " ";
+ text += tokens[i];
+ }
+
+ // Path
+ std::string filename = tokens.back();
+ std::string fullPath = currentDir;
+ if (fullPath.back() != '/') fullPath += '/';
+ fullPath += filename;
+
+ // Decode escape, eg \\n
+ auto decodedText = argTransformer.decodeEscapes(text);
+
+ // Write or append to file
+ bool append = (redir == ">>");
+ if (sd.writeFile(fullPath, decodedText, append)) {
+ terminalView.println((append ? "Appended to " : "Wrote to ") + filename);
+ sd.removeCachedPath(currentDir);
+ } else {
+ terminalView.println("Failed to write to: " + filename);
+ }
+}
+
+std::string SdCardShell::readLine() {
+ std::string line;
+ size_t cursorIndex = 0;
+
+ while (true) {
+ char c = terminalInput.readChar();
+ if (c == KEY_NONE) continue;
+
+ // ENTER
+ if (c == '\n' || c == '\r') {
+ terminalView.println("");
+ commandHistoryManager.add(line);
+ return line;
+ }
+
+ // BACKSPACE
+ if (c == '\b' || c == 127) {
+ if (cursorIndex == 0) continue;
+ cursorIndex--;
+ line.erase(cursorIndex, 1);
+
+ terminalView.print("\r" + currentDir + " $ " + line + " \033[K");
+ size_t moveBack = line.length() - cursorIndex;
+ for (size_t i = 0; i <= moveBack; ++i) {
+ terminalView.print("\x1B[D");
+ }
+ continue;
+ }
+
+ // ESCAPE SEQUENCE
+ if (c == '\x1B') {
+ if (terminalInput.readChar() == '[') {
+ char next = terminalInput.readChar();
+ if (next == 'A') {
+ line = commandHistoryManager.up();
+ cursorIndex = line.length();
+ terminalView.print("\r" + currentDir + " $ " + line + "\033[K");
+ } else if (next == 'B') {
+ line = commandHistoryManager.down();
+ cursorIndex = line.length();
+ terminalView.print("\r" + currentDir + " $ " + line + "\033[K");
+ } else if (next == 'C') { // RIGHT
+ if (cursorIndex < line.length()) {
+ cursorIndex++;
+ terminalView.print("\x1B[C");
+ }
+ } else if (next == 'D') { // LEFT
+ if (cursorIndex > 0) {
+ cursorIndex--;
+ terminalView.print("\x1B[D");
+ }
+ }
+ }
+ continue;
+ }
+
+ // Printable
+ if (isprint(c)) {
+ line.insert(cursorIndex, 1, c);
+ cursorIndex++;
+
+ terminalView.print("\r" + currentDir + " $ " + line + "\033[K");
+ size_t moveBack = line.length() - cursorIndex;
+ for (size_t i = 0; i < moveBack; ++i) {
+ terminalView.print("\x1B[D");
+ }
+ }
+ }
+}
+
+std::string SdCardShell::normalizePath(const std::string& path) {
+ std::vector<std::string> parts;
+ std::istringstream ss(path);
+ std::string token;
+
+ while (std::getline(ss, token, '/')) {
+ if (token.empty() || token == ".") continue;
+ if (token == "..") {
+ if (!parts.empty()) parts.pop_back();
+ } else {
+ parts.push_back(token);
+ }
+ }
+
+ std::string result = "/";
+ for (size_t i = 0; i < parts.size(); ++i) {
+ result += parts[i];
+ if (i < parts.size() - 1) result += '/';
+ }
+
+ return result;
+}
+
+std::string SdCardShell::resolveRelativePath(const std::string& base, const std::string& arg) {
+ std::string combined = base;
+ if (!combined.empty() && combined.back() != '/') {
+ combined += '/';
+ }
+ combined += arg;
+ return normalizePath(combined);
+}
diff --git a/src/Shells/SdCardShell.h b/src/Shells/SdCardShell.h
new file mode 100644
index 00000000..04798d1d
--- /dev/null
+++ b/src/Shells/SdCardShell.h
@@ -0,0 +1,42 @@
+#pragma once
+
+#include <string>
+#include <sstream>
+#include "Services/SdService.h"
+#include "Interfaces/ITerminalView.h"
+#include "Interfaces/IInput.h"
+#include "Transformers/ArgTransformer.h"
+#include "Managers/CommandHistoryManager.h"
+
+class SdCardShell {
+public:
+ SdCardShell(SdService& sdService, ITerminalView& view, IInput& input, ArgTransformer& argTransformer);
+ void run();
+
+private:
+ SdService& sd;
+ ITerminalView& terminalView;
+ IInput& terminalInput;
+ ArgTransformer& argTransformer;
+ std::string currentDir;
+ CommandHistoryManager commandHistoryManager;
+
+ void executeCommand(const std::string& input);
+
+ // Command handlers
+ void cmdLs();
+ void cmdCd(std::istringstream& iss);
+ void cmdMkdir(std::istringstream& iss);
+ void cmdTouch(std::istringstream& iss);
+ void cmdRm(std::istringstream& iss);
+ void cmdCat(std::istringstream& iss);
+ void cmdEcho(std::istringstream& iss);
+ void cmdHelp();
+
+ // Input reader
+ std::string readLine();
+
+ // Path utils
+ std::string normalizePath(const std::string& path);
+ std::string resolveRelativePath(const std::string& base, const std::string& arg);
+};