store network configuration on flash-disk

This commit is contained in:
folkert van heusden 2023-03-26 21:20:52 +02:00
parent c624d8c4e5
commit ee7e934df9
Signed by untrusted user who does not match committer: folkert
GPG key ID: 6B6455EDFEED3BD1
4 changed files with 459 additions and 328 deletions

0
ESP32/data/.place_holder Normal file
View file

View file

@ -1,8 +1,10 @@
// (C) 2018-2023 by Folkert van Heusden // (C) 2018-2023 by Folkert van Heusden
// Released under Apache License v2.0 // Released under Apache License v2.0
#include <Arduino.h> #include <Arduino.h>
#include <ArduinoJson.h>
#include <atomic> #include <atomic>
#include <HardwareSerial.h> #include <HardwareSerial.h>
#include <LittleFS.h>
#include <stdio.h> #include <stdio.h>
#include <string.h> #include <string.h>
#include <unistd.h> #include <unistd.h>
@ -27,6 +29,12 @@
#include "utils.h" #include "utils.h"
constexpr const char CFG_FILE[] = "/net-disk.json";
#define MAX_CFG_SIZE 1024
StaticJsonDocument<MAX_CFG_SIZE> json_doc;
bus *b = nullptr; bus *b = nullptr;
cpu *c = nullptr; cpu *c = nullptr;
tty *tty_ = nullptr; tty *tty_ = nullptr;
@ -60,6 +68,83 @@ void console_thread_wrapper_io(void *const c)
cnsl->operator()(); cnsl->operator()();
} }
typedef enum { DT_RK05, DT_RL02 } disk_type_t;
std::optional<std::pair<std::vector<disk_backend *>, std::vector<disk_backend *> > > load_disk_configuration(console *const c)
{
File dataFile = LittleFS.open(CFG_FILE, "r");
if (!dataFile)
return { };
size_t size = dataFile.size();
char buffer[MAX_CFG_SIZE];
if (size > sizeof buffer) { // this should not happen
dataFile.close();
return { };
}
dataFile.read(reinterpret_cast<uint8_t *>(buffer), size);
buffer[(sizeof buffer) - 1] = 0x00;
dataFile.close();
auto error = deserializeJson(json_doc, buffer);
if (error) // this should not happen
return { };
String nbd_host = json_doc["NBD-host"];
int nbd_port = json_doc["NBD-port"];
String disk_type_temp = json_doc["disk-type"];
disk_type_t disk_type = DT_RK05;
if (disk_type_temp == "rl02")
disk_type = DT_RL02;
disk_backend *d = new disk_backend_nbd(nbd_host.c_str(), nbd_port);
if (d->begin() == false) {
c->put_string_lf("Cannot initialize NBD client from configuration file");
delete d;
return { };
}
c->put_string_lf(format("Connection to NBD server at %s:%d success", nbd_host.c_str(), nbd_port));
if (disk_type == DT_RK05)
return { { { d }, { } } };
if (disk_type == DT_RL02)
return { { { }, { d } } };
return { };
}
bool save_disk_configuration(const std::string & nbd_host, const int nbd_port, const disk_type_t dt)
{
json_doc["NBD-host"] = nbd_host;
json_doc["NBD-port"] = nbd_port;
json_doc["disk-type"] = dt == DT_RK05 ? "rk05" : "rl02";
File dataFile = LittleFS.open(CFG_FILE, "w");
if (!dataFile)
return false;
serializeJson(json_doc, dataFile);
dataFile.close();
return true;
}
typedef enum { BE_NETWORK, BE_SD } disk_backend_t; typedef enum { BE_NETWORK, BE_SD } disk_backend_t;
std::optional<disk_backend_t> select_disk_backend(console *const c) std::optional<disk_backend_t> select_disk_backend(console *const c)
{ {
@ -81,8 +166,6 @@ std::optional<disk_backend_t> select_disk_backend(console *const c)
return BE_SD; return BE_SD;
} }
typedef enum { DT_RK05, DT_RL02 } disk_type_t;
std::optional<disk_type_t> select_disk_type(console *const c) std::optional<disk_type_t> select_disk_type(console *const c)
{ {
c->put_string("1. RK05, 2. RL02, 9. abort"); c->put_string("1. RK05, 2. RL02, 9. abort");
@ -130,6 +213,11 @@ std::optional<std::pair<std::vector<disk_backend *>, std::vector<disk_backend *>
return { }; return { };
} }
if (save_disk_configuration(hostname, atoi(port_str.c_str()), disk_type.value()))
c->put_string_lf("NBD disk configuration saved");
else
c->put_string_lf("NBD disk configuration NOT saved");
if (disk_type.value() == DT_RK05) if (disk_type.value() == DT_RK05)
return { { { d }, { } } }; return { { { d }, { } } };
@ -197,6 +285,21 @@ std::optional<std::pair<std::vector<disk_backend *>, std::vector<disk_backend *>
} }
} }
void set_disk_configuration(std::pair<std::vector<disk_backend *>, std::vector<disk_backend *> > & disk_files)
{
if (disk_files.first.empty() == false)
b->add_rk05(new rk05(disk_files.first, b, cnsl->get_disk_read_activity_flag(), cnsl->get_disk_write_activity_flag()));
if (disk_files.second.empty() == false)
b->add_rl02(new rl02(disk_files.second, b, cnsl->get_disk_read_activity_flag(), cnsl->get_disk_write_activity_flag()));
// TODO: allow bootloader to be selected
if (disk_files.first.empty() == false)
setBootLoader(b, BL_RK05);
else
setBootLoader(b, BL_RL02);
}
void configure_disk(console *const c) void configure_disk(console *const c)
{ {
for(;;) { for(;;) {
@ -217,17 +320,7 @@ void configure_disk(console *const c)
if (files.has_value() == false) if (files.has_value() == false)
break; break;
if (files.value().first.empty() == false) set_disk_configuration(files.value());
b->add_rk05(new rk05(files.value().first, b, cnsl->get_disk_read_activity_flag(), cnsl->get_disk_write_activity_flag()));
if (files.value().second.empty() == false)
b->add_rl02(new rl02(files.value().second, b, cnsl->get_disk_read_activity_flag(), cnsl->get_disk_write_activity_flag()));
// TODO: allow bootloader to be selected
if (files.value().first.empty() == false)
setBootLoader(b, BL_RK05);
else
setBootLoader(b, BL_RL02);
break; break;
} }
@ -266,15 +359,14 @@ void configure_network(console *const c)
return; return;
} }
set_hostname();
if (parts.size() == 1) if (parts.size() == 1)
WiFi.begin(parts.at(0).c_str()); WiFi.begin(parts.at(0).c_str());
else else
WiFi.begin(parts.at(0).c_str(), parts.at(1).c_str()); WiFi.begin(parts.at(0).c_str(), parts.at(1).c_str());
} }
void wait_network(console *const c) { void wait_network(console *const c)
{
constexpr const int timeout = 10 * 3; constexpr const int timeout = 10 * 3;
int i = 0; int i = 0;
@ -291,18 +383,20 @@ void wait_network(console *const c) {
c->put_string_lf("Time out connecting"); c->put_string_lf("Time out connecting");
} }
void check_network(console *const c) { void check_network(console *const c)
{
wait_network(c); wait_network(c);
c->put_string_lf(""); c->put_string_lf("");
c->put_string_lf(format("Local IP address: %s", WiFi.localIP().toString().c_str())); c->put_string_lf(format("Local IP address: %s", WiFi.localIP().toString().c_str()));
} }
void start_network(console *const c) { void start_network(console *const c)
WiFi.mode(WIFI_STA); {
set_hostname(); set_hostname();
WiFi.mode(WIFI_STA);
WiFi.begin(); WiFi.begin();
wait_network(c); wait_network(c);
@ -311,11 +405,26 @@ void start_network(console *const c) {
c->put_string_lf(format("Local IP address: %s", WiFi.localIP().toString().c_str())); c->put_string_lf(format("Local IP address: %s", WiFi.localIP().toString().c_str()));
} }
void set_tty_serial_speed(const int bps) { void set_tty_serial_speed(const int bps)
{
Serial_RS232.begin(bps); Serial_RS232.begin(bps);
} }
void setup() { void recall_configuration(console *const c)
{
c->put_string_lf("Starting network...");
start_network(cnsl);
auto disk_configuration = load_disk_configuration(c);
if (disk_configuration.has_value()) {
c->put_string_lf("Starting disk...");
set_disk_configuration(disk_configuration.value());
}
}
void setup()
{
Serial.begin(115200); Serial.begin(115200);
Serial.println(F("This PDP-11 emulator is called \"kek\" (reason for that is forgotten) and was written by Folkert van Heusden.")); Serial.println(F("This PDP-11 emulator is called \"kek\" (reason for that is forgotten) and was written by Folkert van Heusden."));
@ -328,6 +437,9 @@ void setup() {
Serial.print(F("CPU clock frequency (MHz): ")); Serial.print(F("CPU clock frequency (MHz): "));
Serial.println(getCpuFrequencyMhz()); Serial.println(getCpuFrequencyMhz());
if (!LittleFS.begin(true))
Serial.println(F("LittleFS.begin() failed"));
Serial.print(F("Free RAM before init (decimal bytes): ")); Serial.print(F("Free RAM before init (decimal bytes): "));
Serial.println(ESP.getFreeHeap()); Serial.println(ESP.getFreeHeap());
@ -380,7 +492,8 @@ void setup() {
cnsl->start_thread(); cnsl->start_thread();
} }
void loop() { void loop()
{
debugger(cnsl, b, &stop_event, false); debugger(cnsl, b, &stop_event, false);
c->reset(); c->reset();

View file

@ -10,8 +10,10 @@ board = wemos_d1_mini32
framework = arduino framework = arduino
monitor_speed = 115200 monitor_speed = 115200
upload_speed = 1000000 upload_speed = 1000000
board_build.filesystem = littlefs
lib_deps = greiman/SdFat@^2.1.2 lib_deps = greiman/SdFat@^2.1.2
adafruit/Adafruit NeoPixel adafruit/Adafruit NeoPixel
bblanchon/ArduinoJson@^6.19.4
build_flags = -std=gnu++17 -Ofast -DESP32=1 -ggdb3 -D_GLIBCXX_USE_C99 build_flags = -std=gnu++17 -Ofast -DESP32=1 -ggdb3 -D_GLIBCXX_USE_C99
build_unflags = -std=gnu++11 -Os build_unflags = -std=gnu++11 -Os

View file

@ -19,6 +19,8 @@ void check_network(console *const c);
void start_network(console *const c); void start_network(console *const c);
void set_tty_serial_speed(const int bps); void set_tty_serial_speed(const int bps);
void recall_configuration(console *const c);
#endif #endif
// returns size of instruction (in bytes) // returns size of instruction (in bytes)
@ -162,6 +164,7 @@ void debugger(console *const cnsl, bus *const b, std::atomic_uint32_t *const sto
bool single_step = false; bool single_step = false;
while(*stop_event != EVENT_TERMINATE) { while(*stop_event != EVENT_TERMINATE) {
try {
std::string cmd = cnsl->read_line(format("%d", stop_event->load())); std::string cmd = cnsl->read_line(format("%d", stop_event->load()));
auto parts = split(cmd, " "); auto parts = split(cmd, " ");
auto kv = split(parts, "="); auto kv = split(parts, "=");
@ -397,7 +400,7 @@ void debugger(console *const cnsl, bus *const b, std::atomic_uint32_t *const sto
} }
#endif #endif
else if (cmd == "cls") { else if (cmd == "cls") {
const char cls[] = { 12, 0 }; const char cls[] = { 27, '[', '2', 'J', 12, 0 };
cnsl->put_string_lf(cls); cnsl->put_string_lf(cls);
@ -410,6 +413,11 @@ void debugger(console *const cnsl, bus *const b, std::atomic_uint32_t *const sto
continue; continue;
} }
else if (cmd == "init") {
recall_configuration(cnsl);
continue;
}
else if (cmd == "quit" || cmd == "q") { else if (cmd == "quit" || cmd == "q") {
#if defined(ESP32) #if defined(ESP32)
ESP.restart(); ESP.restart();
@ -440,6 +448,7 @@ void debugger(console *const cnsl, bus *const b, std::atomic_uint32_t *const sto
cnsl->put_string_lf("chknet - check network status"); cnsl->put_string_lf("chknet - check network status");
cnsl->put_string_lf("cfgdisk - configure disk"); cnsl->put_string_lf("cfgdisk - configure disk");
cnsl->put_string_lf("serspd - set serial speed in bps (8N1 are default)"); cnsl->put_string_lf("serspd - set serial speed in bps (8N1 are default)");
cnsl->put_string_lf("init - reload (disk-)configuration from flash");
#endif #endif
continue; continue;
@ -497,6 +506,13 @@ void debugger(console *const cnsl, bus *const b, std::atomic_uint32_t *const sto
*stop_event = EVENT_NONE; *stop_event = EVENT_NONE;
} }
} }
catch(const std::exception & e) {
cnsl->put_string_lf(format("Exception caught: %s", e.what()));
}
catch(...) {
cnsl->put_string_lf("Unspecified exception caught");
}
}
} }
void run_bic(console *const cnsl, bus *const b, std::atomic_uint32_t *const stop_event, const bool tracing, const uint16_t start_addr) void run_bic(console *const cnsl, bus *const b, std::atomic_uint32_t *const stop_event, const bool tracing, const uint16_t start_addr)