Skip to content

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

create shell for at commands

Geo500594602846
diff --git a/src/Shells/UartAtShell.cpp b/src/Shells/UartAtShell.cpp
new file mode 100644
index 00000000..89920d20
--- /dev/null
+++ b/src/Shells/UartAtShell.cpp
@@ -0,0 +1,365 @@
+#include "UartAtShell.h"
+#include <sstream>
+#include <cstdio>
+#include <regex>
+
+UartAtShell::UartAtShell(ITerminalView& terminalView,
+ IInput& terminalInput,
+ UserInputManager& userInputManager,
+ ArgTransformer& argTransformer,
+ UartService& uartService)
+: terminalView(terminalView),
+ terminalInput(terminalInput),
+ userInputManager(userInputManager),
+ argTransformer(argTransformer),
+ uartService(uartService) {}
+
+void UartAtShell::run() {
+
+ terminalView.println("\n=== UART AT Shell ===");
+
+ while (true){
+ // Select the mode
+ AtMode mode{};
+ if (!selectMode(mode)) {
+ terminalView.println("Exiting UART AT Shell...\n");
+ return;
+ }
+
+ // Loop action on the mode
+ actionLoop(mode);
+ }
+}
+
+void UartAtShell::actionLoop(AtMode& mode) {
+ while (true) {
+ const auto& actions = getAtActionsFor(mode);
+ const AtActionItem* chosen = nullptr;
+
+ terminalView.println("\n=== UART AT Shell ===");
+
+ // Select an action
+ selectAction(actions, chosen);
+
+ // Back
+ if (!chosen) {
+ terminalView.println("Going back to mode selection...\n");
+
+ break;
+ }
+
+ // Ask for args if needed
+ std::string cmd;
+ if (!buildCommandFromArgs(*chosen, cmd)) {
+ terminalView.println("⚠️ Command canceled.\n");
+ continue;
+ }
+
+ // Confirm if destructive cmd
+ if (!confirmIfDestructive(*chosen)) {
+ terminalView.println("⚠️ Destructive command canceled.\n");
+ continue;
+ }
+
+ // Send the command
+ terminalView.println("Send: " + cmd + " ... Waiting for response");
+ auto response = sendAt(cmd);
+
+ // Response
+ terminalView.println("\n=== Response ===");
+ if (response.empty()) {
+ terminalView.println("\nNo response from the device.\n");
+ } else {
+ std::string responseFormatted = "\n" + response;
+ terminalView.println(responseFormatted.c_str());
+ }
+ }
+}
+
+bool UartAtShell::selectMode(AtMode& outMode) {
+ std::vector<std::string> items;
+ items.reserve(kModes.size() + 1);
+
+ // Each mode
+ uint8_t i = 0;
+ for (const auto& m : kModes) {
+ auto label = joinLabel(m.emoji, m.name);
+ if (i < 9) label = " " + label;
+ items.push_back(label);
+ i++;
+ }
+
+ // Add exit option
+ auto exit = "🚪 Exit Shell";
+ items.push_back(exit);
+
+ int index = userInputManager.readValidatedChoiceIndex("Select AT mode", items, 0);
+ if (index < 0 || index >= (int)items.size() || items[index] == exit) {
+ return false;
+ }
+
+ outMode = kModes[(size_t)index].mode;
+ return true;
+}
+
+bool UartAtShell::selectAction(const std::vector<AtActionItem>& actions, const AtActionItem*& outAction) {
+ std::vector<std::string> items;
+ items.reserve(actions.size() + 2);
+
+ // Each action
+ uint8_t i = 0;
+ for (const auto& a : actions) {
+ // Display label
+ auto label = joinLabel(a.emoji, a.label, a.command);
+ if (i < 9) label = " " + label;
+ items.push_back(label);
+ i++;
+ }
+
+ // Add back option with correct spacing
+ items.push_back(i > 9 ? "↩️ Back" : " ↩️ Back");
+
+ // Select action
+ int index = userInputManager.readValidatedChoiceIndex("Select command", items, 0);
+ if (index < 0) return false;
+
+ // Back option choice
+ if ((size_t)index == actions.size()) {
+ return false;
+ }
+
+ outAction = &actions[(size_t)index];
+ return true;
+}
+
+std::string UartAtShell::buildPromptText(const AtActionArg& a, size_t idx) const {
+ std::string label = a.name ? std::string(a.name) : ("arg#" + std::to_string(idx + 1));
+ std::string p = "Enter " + label;
+ if (a.hint && *a.hint) {
+ p += " (e.g. ";
+ p += a.hint;
+ p += ")";
+ }
+ if (!a.required && a.defaultValue) {
+ p += " [default: ";
+ p += a.defaultValue;
+ p += "]";
+ }
+ p += ": ";
+ return p;
+}
+
+std::string UartAtShell::readUserLine(const std::string& prompt) {
+ terminalView.print(prompt);
+ return userInputManager.getLine();
+}
+
+bool UartAtShell::isInChoices(const std::string& v, const char* choices) const {
+ if (!choices) return false;
+ std::string vv = argTransformer.toLower(v);
+ std::string cs = argTransformer.toLower(choices);
+
+ size_t start = 0;
+ while (true) {
+ size_t bar = cs.find('|', start);
+ std::string tok = cs.substr(start, (bar == std::string::npos) ? (cs.size() - start) : (bar - start));
+ if (vv == tok) return true;
+ if (bar == std::string::npos) return false;
+ start = bar + 1;
+ }
+}
+
+bool UartAtShell::validateAndFormat(const AtActionArg& a, const std::string& raw, std::string& out) const {
+ if (raw.empty()) { terminalView.println("❌ This field is required."); return false; }
+
+ switch (a.type) {
+ case AtArgType::Phone:
+ case AtArgType::String:
+ out = raw;
+ return true;
+
+ case AtArgType::Uint:
+ if (!argTransformer.isValidNumber(raw)) {
+ terminalView.println("❌ Unsigned integer expected.");
+ return false;
+ }
+ out = std::to_string(argTransformer.toUint32(raw));
+ return true;
+
+ case AtArgType::Int: {
+ int iv = 0;
+ if (!argTransformer.parseInt(raw, iv)) {
+ terminalView.println("❌ Signed integer expected.");
+ return false;
+ }
+ out = std::to_string(iv);
+ return true;
+ }
+
+ case AtArgType::Bool01:
+ if (raw == "0" || raw == "1") { out = raw; return true; }
+ terminalView.println("❌ Enter 0 or 1.");
+ return false;
+
+ case AtArgType::HexBytes: {
+ auto bytes = argTransformer.parseHexList(raw);
+ if (bytes.empty()) {
+ terminalView.println("❌ Hex bytes expected (e.g. \"01 AA 03\").");
+ return false;
+ }
+ out.clear();
+ for (size_t i = 0; i < bytes.size(); ++i) {
+ if (i) out += ' ';
+ out += argTransformer.toHex(bytes[i], 2);
+ }
+ return true;
+ }
+
+ case AtArgType::Choice:
+ if (!isInChoices(raw, a.choices)) {
+ terminalView.println("❌ Invalid choice.");
+ return false;
+ }
+ out = raw;
+ return true;
+
+ case AtArgType::Regex:
+ try {
+ if (!a.pattern) return false;
+ if (!std::regex_match(raw, std::regex(a.pattern))) {
+ terminalView.println("❌ Invalid format.");
+ return false;
+ }
+ out = raw;
+ return true;
+ } catch (...) {
+ terminalView.println("❌ Bad regex.");
+ return false;
+ }
+ }
+ return false;
+}
+
+bool UartAtShell::acquireArgValue(const AtActionArg& a, size_t idx, std::string& accepted, bool& hasValue) {
+ // Reset outputs
+ accepted.clear();
+ hasValue = false;
+
+ // Keep prompting until we get a valid value, or skip if optional with no default
+ while (true) {
+ const std::string prompt = buildPromptText(a, idx);
+ std::string raw = readUserLine(prompt);
+
+ // If empty input
+ if (raw.empty()) {
+ // Optional with default -> take default
+ if (!a.required && a.defaultValue) {
+ accepted = a.defaultValue;
+ hasValue = true;
+ return true;
+ }
+ // Optional without default -> skip value entirely
+ if (!a.required && !a.defaultValue) {
+ // keep hasValue = false, accepted = ""
+ return true; // skip
+ }
+ // Required -> inform and loop
+ terminalView.println("❌ This field is required.");
+ continue;
+ }
+
+ // Non-empty input: validate
+ if (validateAndFormat(a, raw, accepted)) {
+ hasValue = true;
+ return true;
+ }
+ // else: validation printed an error, loop again
+ }
+}
+
+std::string UartAtShell::placeholderFor(size_t idx) const {
+ return "%" + std::to_string(idx + 1);
+}
+
+void UartAtShell::applyArgToCommand(std::string& cmd, size_t idx, const std::string& accepted, bool hasValue) const {
+ const std::string ph = placeholderFor(idx);
+ size_t pos = cmd.find(ph);
+
+ if (pos != std::string::npos) {
+ cmd.replace(pos, ph.size(), accepted);
+ return;
+ }
+
+ // No placeholder in template: append only if we actually have a value
+ if (hasValue) {
+ cmd += (idx ? "," : " ");
+ cmd += accepted;
+ }
+}
+
+bool UartAtShell::buildCommandFromArgs(const AtActionItem& action, std::string& outCmd) {
+ if (action.args.empty()) {
+ outCmd = action.command;
+ return true;
+ }
+
+ std::string cmd = action.command;
+
+ for (size_t i = 0; i < action.args.size(); ++i) {
+ const auto& a = action.args[i];
+ std::string accepted;
+ bool hasValue = false;
+
+ if (!acquireArgValue(a, i, accepted, hasValue)) {
+ // for futur cancellation logic if needed
+ }
+
+ applyArgToCommand(cmd, i, accepted, hasValue);
+ }
+
+ outCmd = cmd;
+ return true;
+}
+
+bool UartAtShell::confirmIfDestructive(const AtActionItem& action) {
+ if (!action.destructive) return true;
+
+ terminalView.println("⚠️ This action can be destructive: " + std::string(action.label));
+ std::vector<std::string> choices = { "No, cancel", "Yes, proceed" };
+ int c = userInputManager.readValidatedChoiceIndex("Are you sure?", choices, 0);
+ return (c == 1);
+}
+
+std::string UartAtShell::sendAt(const std::string& cmd, uint32_t timeoutMs /*=500*/) {
+ // uartService.flush();
+
+ // Envoi
+ uartService.write(cmd);
+ uartService.write("\r\n");
+
+ const uint32_t start = millis();
+ std::string resp = "";
+ uint32_t lastByteTs = start;
+
+ // Read everything for timeoutMs
+ while (millis() - start < timeoutMs) {
+ while (uartService.available() > 0) {
+ char c = uartService.read();
+ resp.push_back(c);
+ }
+ delay(1);
+ }
+
+ return resp;
+}
+
+std::string UartAtShell::joinLabel(const char* emoji, const char* text, const char* rawCmd) {
+ std::string s;
+ if (emoji && *emoji) { s += emoji; s += " "; }
+ if (rawCmd && *rawCmd) {
+ s += rawCmd;
+ s+= " - ";
+ }
+ if (text && *text) { s += text; }
+ return s;
+}
\ No newline at end of file
diff --git a/src/Shells/UartAtShell.h b/src/Shells/UartAtShell.h
new file mode 100644
index 00000000..004d8322
--- /dev/null
+++ b/src/Shells/UartAtShell.h
@@ -0,0 +1,42 @@
+#pragma once
+#include <vector>
+#include <string>
+#include "Interfaces/ITerminalView.h"
+#include "Interfaces/IInput.h"
+#include "Managers/UserInputManager.h"
+#include "Transformers/ArgTransformer.h"
+#include "Services/UartService.h"
+#include "Data/UartAtCommands.h"
+
+class UartAtShell {
+public:
+ UartAtShell(ITerminalView& terminalView,
+ IInput& terminalInput,
+ UserInputManager& userInputManager,
+ ArgTransformer& argTransformer,
+ UartService& uartService);
+
+ void run();
+
+private:
+ bool selectMode(AtMode& outMode);
+ void actionLoop(AtMode& outMode);
+ bool selectAction(const std::vector<AtActionItem>& actions, const AtActionItem*& outAction);
+ bool buildCommandFromArgs(const AtActionItem& action, std::string& outCmd);
+ std::string readUserLine(const std::string& prompt);
+ bool isInChoices(const std::string& v, const char* choices) const;
+ bool validateAndFormat(const AtActionArg& a, const std::string& raw, std::string& out) const;
+ bool acquireArgValue(const AtActionArg& a, size_t idx, std::string& accepted, bool& hasValue);
+ void applyArgToCommand(std::string& cmd, size_t idx, const std::string& accepted, bool hasValue) const;
+ std::string placeholderFor(size_t idx) const;
+ bool confirmIfDestructive(const AtActionItem& action);
+ std::string sendAt(const std::string& cmd, uint32_t timeoutMs=2000);
+ std::string buildPromptText(const AtActionArg& a, size_t idx) const;
+ static std::string joinLabel(const char* emoji, const char* text, const char* rawCmd = nullptr);
+private:
+ ITerminalView& terminalView;
+ IInput& terminalInput;
+ UserInputManager& userInputManager;
+ ArgTransformer& argTransformer;
+ UartService& uartService;
+};