ESP32: configure network & NBD disk backend

This commit is contained in:
folkert van heusden 2023-03-22 11:26:13 +01:00
parent 89a9fbead0
commit 9040a2b075
Signed by untrusted user who does not match committer: folkert
GPG key ID: 6B6455EDFEED3BD1
2 changed files with 202 additions and 67 deletions

View file

@ -1,4 +1,4 @@
// (C) 2018-2022 by Folkert van Heusden // (C) 2018-2023 by Folkert van Heusden
// Released under Apache License v2.0 // Released under Apache License v2.0
#include <atomic> #include <atomic>
#include <stdio.h> #include <stdio.h>
@ -14,6 +14,7 @@
#include "debugger.h" #include "debugger.h"
#include "disk_backend.h" #include "disk_backend.h"
#include "disk_backend_esp32.h" #include "disk_backend_esp32.h"
#include "disk_backend_nbd.h"
#include "error.h" #include "error.h"
#include "esp32.h" #include "esp32.h"
#include "gen.h" #include "gen.h"
@ -55,58 +56,87 @@ void console_thread_wrapper_io(void *const c)
cnsl->operator()(); cnsl->operator()();
} }
void setup_wifi_stations() typedef enum { BE_NETWORK, BE_SD } disk_backend_t;
std::optional<disk_backend_t> select_disk_backend(console *const c)
{ {
#if 0 c->put_string("1. network (NBD), 2. local SD card, 9. abort");
WiFi.mode(WIFI_STA);
WiFi.softAP("PDP-11 KEK", nullptr, 5, 0, 4); int ch = -1;
while(ch == -1 && ch != '1' && ch != '2' && ch != '9')
ch = c->wait_char(500);
#if 0 c->put_string_lf(format("%c", ch));
Serial.println(F("Scanning for WiFi access points..."));
int n = WiFi.scanNetworks(); if (ch == '9')
return { };
Serial.println(F("scan done")); if (ch == '1')
return BE_NETWORK;
if (n == 0) // if (ch == '2')
Serial.println(F("no networks found")); return BE_SD;
else {
for (int i = 0; i < n; ++i) {
// Print SSID and RSSI for each network found
Serial.print(i + 1);
Serial.print(F(": "));
Serial.print(WiFi.SSID(i));
Serial.print(F(" ("));
Serial.print(WiFi.RSSI(i));
Serial.print(F(")"));
Serial.println(WiFi.encryptionType(i) == WIFI_AUTH_OPEN ? " " : "*");
delay(10);
}
} }
std::string ssid = read_terminal_line("SSID: "); typedef enum { DT_RK05, DT_RL02 } disk_type_t;
std::string password = read_terminal_line("password: ");
WiFi.begin(ssid.c_str(), password.c_str());
#else
WiFi.begin("www.vanheusden.com", "Ditiseentest31415926");
//WiFi.begin("NURDspace-guest", "harkharkhark");
#endif
while (WiFi.status() != WL_CONNECTED) { std::optional<disk_type_t> select_disk_type(console *const c)
Serial.print('.'); {
c->put_string("1. RK05, 2. RL02, 9. abort");
delay(250); int ch = -1;
while(ch == -1 && ch != '1' && ch != '2' && ch != '9')
ch = c->wait_char(500);
c->put_string_lf(format("%c", ch));
if (ch == '9')
return { };
if (ch == '1')
return DT_RK05;
// if (ch == '2')
return DT_RL02;
} }
on_wifi = true; std::optional<std::pair<std::vector<disk_backend *>, std::vector<disk_backend *> > > select_nbd_server(console *const c)
{
c->flush_input();
Serial.println(WiFi.localIP()); std::string hostname = c->read_line("Enter hostname (or empty to abort): ");
#endif
if (hostname.empty())
return { };
std::string port_str = c->read_line("Enter port number (or empty to abort): ");
if (port_str.empty())
return { };
auto disk_type = select_disk_type(c);
if (disk_type.has_value() == false)
return { };
disk_backend *d = new disk_backend_nbd(hostname, atoi(port_str.c_str()));
if (d->begin() == false) {
c->put_string_lf("Cannot initialize NBD client");
delete d;
return { };
}
if (disk_type.value() == DT_RK05)
return { { { d }, { } } };
if (disk_type.value() == DT_RL02)
return { { { }, { d } } };
return { };
} }
// RK05, RL02 files // RK05, RL02 files
std::pair<std::vector<disk_backend *>, std::vector<disk_backend *> > select_disk_files(console *const c) std::optional<std::pair<std::vector<disk_backend *>, std::vector<disk_backend *> > > select_disk_files(console *const c)
{ {
c->debug("MISO: %d", int(MISO)); c->debug("MISO: %d", int(MISO));
c->debug("MOSI: %d", int(MOSI)); c->debug("MOSI: %d", int(MOSI));
@ -128,16 +158,10 @@ std::pair<std::vector<disk_backend *>, std::vector<disk_backend *> > select_disk
if (selected_file.empty()) if (selected_file.empty())
continue; continue;
c->put_string("1. RK05, 2. RL02, 3. re-select file"); auto disk_type = select_disk_type(c);
int ch = -1; if (disk_type.has_value() == false)
while(ch == -1 && ch != '1' && ch != '2' && ch != '3') return { };
ch = c->wait_char(500);
c->put_string_lf(format("%c", ch));
if (ch == '3')
continue;
c->put_string("Opening file: "); c->put_string("Opening file: ");
c->put_string_lf(selected_file.c_str()); c->put_string_lf(selected_file.c_str());
@ -158,17 +182,115 @@ std::pair<std::vector<disk_backend *>, std::vector<disk_backend *> > select_disk
continue; continue;
} }
if (ch == '1') if (disk_type.value() == DT_RK05)
return { { temp }, { } }; return { { { temp }, { } } };
if (ch == '2') if (disk_type.value() == DT_RL02)
return { { }, { temp } }; return { { { }, { temp } } };
} }
c->put_string_lf("open failed"); c->put_string_lf("open failed");
} }
} }
void configure_disk(console *const c)
{
for(;;) {
Serial.println(F("Load disk"));
auto backend = select_disk_backend(cnsl);
if (backend.has_value() == false)
break;
std::optional<std::pair<std::vector<disk_backend *>, std::vector<disk_backend *> > > files;
if (backend == BE_NETWORK)
files = select_nbd_server(cnsl);
else // if (backend == BE_SD)
files = select_disk_files(cnsl);
if (files.has_value() == false)
break;
if (files.value().first.empty() == false)
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;
}
}
void set_hostname()
{
WiFi.setHostname("PDP-11");
}
void configure_network(console *const c)
{
WiFi.persistent(true);
WiFi.setAutoReconnect(true);
WiFi.mode(WIFI_STA);
c->put_string_lf("Scanning for wireless networks...");
int n_ssids = WiFi.scanNetworks();
c->put_string_lf("Wireless networks:");
for(int i=0; i<n_ssids; i++)
c->put_string_lf(format("\t%s", WiFi.SSID(i).c_str()));
c->flush_input();
std::string wifi_ap = c->read_line("Enter SSID[|PSK]: ");
auto parts = split(wifi_ap, "|");
if (parts.size() > 2) {
c->put_string_lf("Invalid SSID/PSK: should not contain '|'");
return;
}
set_hostname();
if (parts.size() == 1)
WiFi.begin(parts.at(0).c_str());
else
WiFi.begin(parts.at(0).c_str(), parts.at(1).c_str());
}
void start_network(console *const c)
{
WiFi.mode(WIFI_STA);
set_hostname();
WiFi.begin();
int i = 0;
while (WiFi.waitForConnectResult() != WL_CONNECTED && i < 10 * 3) {
c->put_string(".");
delay(1000 / 3);
i++;
}
c->put_string_lf("");
c->put_string_lf(format("Local IP address: %s", WiFi.localIP().toString().c_str()));
}
void setup() { void setup() {
Serial.begin(115200); Serial.begin(115200);
@ -216,20 +338,6 @@ void setup() {
// setup_wifi_stations(); // setup_wifi_stations();
Serial.println(F("Load RK05"));
auto disk_files = select_disk_files(cnsl);
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()));
if (disk_files.first.empty() == false)
setBootLoader(b, BL_RK05);
else
setBootLoader(b, BL_RL02);
Serial.print(F("Free RAM after init: ")); Serial.print(F("Free RAM after init: "));
Serial.println(ESP.getFreeHeap()); Serial.println(ESP.getFreeHeap());

View file

@ -10,6 +10,11 @@
#include "esp32.h" #include "esp32.h"
void setBootLoader(bus *const b); void setBootLoader(bus *const b);
void configure_disk(console *const c);
void configure_network(console *const c);
void start_network(console *const c);
#endif #endif
// returns size of instruction (in bytes) // returns size of instruction (in bytes)
@ -343,6 +348,23 @@ void debugger(console *const cnsl, bus *const b, std::atomic_uint32_t *const sto
#endif #endif
continue; continue;
} }
#if defined(ESP32)
else if (cmd == "cfgdisk") {
configure_disk(cnsl);
continue;
}
else if (cmd == "cfgnet") {
configure_network(cnsl);
continue;
}
else if (cmd == "startnet") {
start_network(cnsl);
continue;
}
#endif
else if (cmd == "quit" || cmd == "q") { else if (cmd == "quit" || cmd == "q") {
#if defined(ESP32) #if defined(ESP32)
ESP.restart(); ESP.restart();
@ -365,6 +387,11 @@ void debugger(console *const cnsl, bus *const b, std::atomic_uint32_t *const sto
cnsl->put_string_lf("setpc - set PC to value"); cnsl->put_string_lf("setpc - set PC to value");
cnsl->put_string_lf("setmem - set memory (a=) to value (v=), both in octal, one byte"); cnsl->put_string_lf("setmem - set memory (a=) to value (v=), both in octal, one byte");
cnsl->put_string_lf("toggle - set switch (s=, 0...15 (decimal)) of the front panel to state (t=, 0 or 1)"); cnsl->put_string_lf("toggle - set switch (s=, 0...15 (decimal)) of the front panel to state (t=, 0 or 1)");
#if defined(ESP32)
cnsl->put_string_lf("cfgnet - configure network (e.g. WiFi)");
cnsl->put_string_lf("startnet - start network");
cnsl->put_string_lf("cfgdisk - configure disk");
#endif
continue; continue;
} }