1
0
mirror of https://github.com/jscrane/r65emu.git synced 2024-10-05 19:54:36 +00:00
r65emu/socket_filer.cpp

80 lines
1.3 KiB
C++
Raw Normal View History

2019-03-25 16:12:38 +00:00
#include <Arduino.h>
#include <stdint.h>
#include "hardware.h"
#include "serialio.h"
#include "filer.h"
#include "socket_filer.h"
#if !defined(ESP32)
#pragma message "no socket filer"
2019-03-26 19:27:25 +00:00
#elif defined(WIFI_SSID)
2019-03-25 16:12:38 +00:00
#include <WiFi.h>
static WiFiServer server(23);
static WiFiClient client;
static bool connected() {
if (client.connected())
return true;
client = server.available();
return client.connected();
}
const char *socket_filer::advance() {
if (connected())
return "connected";
return 0;
}
2019-03-26 19:27:25 +00:00
bool socket_filer::start() {
2019-03-25 16:12:38 +00:00
#if defined(WIFI_SSID)
WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
2019-03-26 19:27:25 +00:00
WiFi.setHostname(_hostname);
2019-03-25 16:12:38 +00:00
for (int i=0; i < 60 && WiFi.status() != WL_CONNECTED; i++)
delay(1000);
server.begin();
return WiFi.status() == WL_CONNECTED;
#else
return false;
#endif
}
void socket_filer::write(uint8_t b) {
if (connected())
client.write(b);
}
uint8_t socket_filer::read() {
if (connected())
return client.read();
return 0;
}
bool socket_filer::more() {
return connected() && client.available() > 0;
}
#if !defined(NO_CHECKPOINT)
const char *socket_filer::checkpoint() {
if (connected()) {
hardware_checkpoint(client);
client.flush();
client.stop();
return "checkpointed";
}
return "not connected";
2019-03-25 16:12:38 +00:00
}
void socket_filer::restore(const char *) {
if (connected()) {
hardware_restore(client);
client.stop();
}
2019-03-25 16:12:38 +00:00
}
#endif
#endif