Skip to content

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

convert shell actions and models vector to const array for reducing heap usage

Geo672abf60a305
diff --git a/src/Shells/GuideShell.cpp b/src/Shells/GuideShell.cpp
index fd298936..6c20eba9 100644
--- a/src/Shells/GuideShell.cpp
+++ b/src/Shells/GuideShell.cpp
@@ -13,16 +13,10 @@ GuideShell::GuideShell(ITerminalView& tv,
void GuideShell::run() {
bool loop = true;
- std::vector<std::string> actionsVector(actions, actions + actionsCount);
while (loop) {
terminalView.println("\n=== Guide Shell ===");
- int choice = userInputManager.readValidatedChoiceIndex(
- "Select a topic:",
- actionsVector,
- static_cast<int>(actionsCount) - 1
- );
-
+ int choice = userInputManager.readValidatedChoiceIndex("Select a topic", actions, actionsCount, actionsCount - 1);
switch (choice) {
case 0: cmdQuickStart(); break;
case 1: cmdExamples(); break;
diff --git a/src/Shells/I2cEepromShell.cpp b/src/Shells/I2cEepromShell.cpp
index cb446afc..0df60e58 100644
--- a/src/Shells/I2cEepromShell.cpp
+++ b/src/Shells/I2cEepromShell.cpp
@@ -17,7 +17,7 @@ I2cEepromShell::I2cEepromShell(
void I2cEepromShell::run(uint8_t addr) {
// Select EEPROM model
- int selectedModelIndex = userInputManager.readValidatedChoiceIndex("\nSelect EEPROM type", models, 0);
+ int selectedModelIndex = userInputManager.readValidatedChoiceIndex("\nSelect EEPROM type", kModels, kModelsCount, 0);
// Initialize EEPROM
uint16_t selectedType = memoryLengths[selectedModelIndex];
@@ -27,8 +27,10 @@ void I2cEepromShell::run(uint8_t addr) {
}
// Set variables
- terminalView.println("\n✅ EEPROM initialized:" + models[selectedModelIndex]);
- selectedModel = models[selectedModelIndex];
+ terminalView.println(
+ std::string("\n✅ EEPROM initialized: ") + kModels[selectedModelIndex]
+ );
+ selectedModel = kModels[selectedModelIndex];
selectedLength = memoryLengths[selectedModelIndex];
selectedI2cAddress = addr;
initialized = true;
@@ -36,8 +38,8 @@ void I2cEepromShell::run(uint8_t addr) {
while (true) {
// Select action
terminalView.println("\n=== I2C EEPROM Shell ===");
- int index = userInputManager.readValidatedChoiceIndex("Select an EEPROM action", actions, 0);
- if (index == -1 || actions[index] == " 🚪 Exit Shell") {
+ int index = userInputManager.readValidatedChoiceIndex("Select an EEPROM action", kActions, kActionsCount, kActionsCount - 1);
+ if (index == -1 || kActions[index] == " 🚪 Exit Shell") {
terminalView.println("Exiting EEPROM shell...\n");
break;
}
diff --git a/src/Shells/I2cEepromShell.h b/src/Shells/I2cEepromShell.h
index 27370b9c..2c819247 100644
--- a/src/Shells/I2cEepromShell.h
+++ b/src/Shells/I2cEepromShell.h
@@ -22,7 +22,7 @@ public:
private:
// Available actions
- const std::vector<std::string> actions = {
+ inline static constexpr const char* kActions[] = {
" 🔍 Probe",
" 📊 Analyze",
" 📖 Read bytes",
@@ -34,22 +34,25 @@ private:
};
// Available EEPROM models
- std::vector<std::string> models = {
- " 24x01 (1 Kbit)",
- " 24x02 (2 Kbit)",
- " 24x04 (4 Kbit)",
- " 24x08 (8 Kbit)",
- " 24x16 (16 Kbit)",
- " 24x32 (32 Kbit)",
- " 24x64 (64 Kbit)",
- " 24x128 (128 Kbit)",
- " 24x256 (256 Kbit)",
- "24x512 (512 Kbit)",
- "24x1025 (1 Mbit)",
- "24x1026 (1 Mbit)",
- "24xM01 (1 Mbit)",
- "24xM02 (2 Mbit)"
- };
+ inline static constexpr const char* kModels[] = {
+ " 24x01 (1 Kbit)",
+ " 24x02 (2 Kbit)",
+ " 24x04 (4 Kbit)",
+ " 24x08 (8 Kbit)",
+ " 24x16 (16 Kbit)",
+ " 24x32 (32 Kbit)",
+ " 24x64 (64 Kbit)",
+ " 24x128 (128 Kbit)",
+ " 24x256 (256 Kbit)",
+ " 24x512 (512 Kbit)",
+ " 24x1025 (1 Mbit)",
+ " 24x1026 (1 Mbit)",
+ " 24xM01 (1 Mbit)",
+ " 24xM02 (2 Mbit)"
+ };
+
+ static constexpr size_t kActionsCount = sizeof(kActions) / sizeof(kActions[0]);
+ static constexpr size_t kModelsCount = sizeof(kModels) / sizeof(kModels[0]);
std::vector<uint16_t> memoryLengths = {
1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1025, 1025, 1025, 2048
diff --git a/src/Shells/ModbusShell.cpp b/src/Shells/ModbusShell.cpp
index 4dded3cb..90a5ab5d 100644
--- a/src/Shells/ModbusShell.cpp
+++ b/src/Shells/ModbusShell.cpp
@@ -32,7 +32,7 @@ void ModbusShell::run(const std::string& host, uint16_t port) {
bool start = true;
while (start) {
printHeader();
- auto choice = userInputManager.readValidatedChoiceIndex("Select a modbus action", actions, 0);
+ auto choice = userInputManager.readValidatedChoiceIndex("Select a modbus action", actions, actionsCount, 0);
switch (choice) {
case 0: cmdReadHolding(); break;
diff --git a/src/Shells/ModbusShell.h b/src/Shells/ModbusShell.h
index f7ff6f48..7d215c4b 100644
--- a/src/Shells/ModbusShell.h
+++ b/src/Shells/ModbusShell.h
@@ -58,7 +58,7 @@ private:
ModbusService::Reply _reply;
- const std::vector<std::string> actions = {
+ inline static const char* actions[] = {
" 📖 Read Holding (FC03)",
" ✏️ Write Holding (FC06/FC16)",
" 📘 Read Input (FC04)",
@@ -70,4 +70,6 @@ private:
" 🔌 Change Target",
"🚪 Exit Shell"
};
+
+ static constexpr size_t actionsCount = sizeof(actions) / sizeof(actions[0]);
};
diff --git a/src/Shells/SpiEepromShell.cpp b/src/Shells/SpiEepromShell.cpp
index 9123873d..965cd529 100644
--- a/src/Shells/SpiEepromShell.cpp
+++ b/src/Shells/SpiEepromShell.cpp
@@ -19,7 +19,7 @@ SpiEepromShell::SpiEepromShell(
void SpiEepromShell::run() {
// Select EEPROM model
- int selectedModelIndex = userInputManager.readValidatedChoiceIndex("\nSelect EEPROM type", models, 0);
+ int selectedModelIndex = userInputManager.readValidatedChoiceIndex("\nSelect EEPROM type", models, modelsCount, 0);
if (selectedModelIndex < 0) {
terminalView.println("Invalid selection. Aborting.\n");
return;
@@ -51,7 +51,7 @@ void SpiEepromShell::run() {
terminalView.println("\n=== SPI EEPROM Shell ===");
// Select action
- int index = userInputManager.readValidatedChoiceIndex("Select EEPROM action", actions, 0);
+ int index = userInputManager.readValidatedChoiceIndex("Select EEPROM action", actions, actionsCount, 0);
// Quit
if (index == -1 || actions[index] == " 🚪 Exit Shell") {
diff --git a/src/Shells/SpiEepromShell.h b/src/Shells/SpiEepromShell.h
index 5e8a818c..0a8a643d 100644
--- a/src/Shells/SpiEepromShell.h
+++ b/src/Shells/SpiEepromShell.h
@@ -33,7 +33,7 @@ private:
uint16_t pageSize = 64; // default
std::string eepromModel;
- const std::vector<std::string> actions = {
+ inline static const char* actions[] = {
" 🔍 Probe EEPROM",
" 📊 Analyze EEPROM",
" 📖 Read bytes",
@@ -44,7 +44,9 @@ private:
" 🚪 Exit Shell"
};
- std::vector<std::string> models = {
+ static constexpr size_t actionsCount = sizeof(actions) / sizeof(actions[0]);
+
+ inline static const char* models[] = {
" 25X010 | 128 B | 8 B page | 1 B addr | blk: 0b | max 10 MHz",
" 25X020 | 256 B | 8 B page | 1 B addr | blk: 0b | max 10 MHz",
" 25X040 | 512 B | 8 B page | 1 B addr | blk: 1b | max 10 MHz",
@@ -60,7 +62,8 @@ private:
"25XM02 | 256 KB | 256 B page | 3 B addr | blk: 0b | max 5 MHz",
"25XM04 | 512 KB | 256 B page | 3 B addr | blk: 0b | max 8 MHz"
};
-
+ static constexpr size_t modelsCount = sizeof(models) / sizeof(models[0]);
+
std::vector<uint32_t> memoryLengths = {
128, // 25X010
256, // 25X020
diff --git a/src/Shells/SysInfoShell.cpp b/src/Shells/SysInfoShell.cpp
index c35404ac..a0905e03 100644
--- a/src/Shells/SysInfoShell.cpp
+++ b/src/Shells/SysInfoShell.cpp
@@ -20,7 +20,7 @@ void SysInfoShell::run() {
while (loop) {
terminalView.println("\n=== System Shell ===");
- int choice = userInputManager.readValidatedChoiceIndex("Select action", actions);
+ int choice = userInputManager.readValidatedChoiceIndex("Select action", actions, actions.size() - 1);
switch (choice) {
case 0: cmdSummary(); break;