1
0
mirror of https://github.com/DCC-EX/CommandStation-EX.git synced 2024-11-27 10:06:13 +01:00
CommandStation-EX/NetworkInterface.cpp

125 lines
3.7 KiB
C++
Raw Normal View History

/*
* © 2020, Gregor Baues. All rights reserved.
*
* This is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* It is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with CommandStation. If not, see <https://www.gnu.org/licenses/>.
*/
#include <Arduino.h>
#include "DIAG.h"
#include "NetworkInterface.h"
2020-10-26 10:29:40 +01:00
#include "Transport.h"
#include "EthernetSetup.h"
#include "WifiSetup.h"
2020-10-26 10:29:40 +01:00
Transport<WiFiServer, WiFiClient, WiFiUDP>* wifiTransport;
Transport<EthernetServer, EthernetClient, EthernetUDP>* ethernetTransport;
void NetworkInterface::setup(transportType transport, protocolType protocol, uint16_t port)
{
bool ok = false;
DIAG(F("\n[%s] Transport Setup In Progress ...\n"), transport ? "Ethernet" : "Wifi");
// configure the Transport and get Ethernet/Wifi server up and running
t = transport;
switch (transport)
{
case WIFI:
{
WifiSetup wSetup(port, protocol);
ok = wSetup.setup();
if (ok)
{
2020-10-26 10:29:40 +01:00
wifiTransport = new Transport<WiFiServer, WiFiClient, WiFiUDP>;
wifiTransport->server = wSetup.getServer();
wifiTransport->port = port;
wifiTransport->protocol = protocol;
wifiTransport->transport = transport;
wifiTransport->maxConnections = wSetup.maxConnections;
2020-10-26 10:29:40 +01:00
ok = wifiTransport->setup(this);
}
break;
};
case ETHERNET:
{
EthernetSetup eSetup(port, protocol);
2020-10-26 10:29:40 +01:00
ethernetTransport = new Transport<EthernetServer, EthernetClient, EthernetUDP>;
ethernetTransport->server = eSetup.setup(); // returns (NULL) 0 if we run over UDP; todo: error handling if something goes wrong in the init
ethernetTransport->port = port;
ethernetTransport->protocol = protocol;
ethernetTransport->transport = transport;
ethernetTransport->maxConnections = eSetup.maxConnections; // that has been determined during the ethernet/wifi setup
2020-10-26 10:29:40 +01:00
ok = ethernetTransport->setup(this); // start the transport i.e. setup all the client connections; We don't need the setup object anymore from here on
break;
};
default:
{
DIAG(F("\nERROR: Unknown Transport")); // Something went wrong
break;
}
}
DIAG(F("\n\n[%s] Transport %s ..."), transport ? "Ethernet" : "Wifi", ok ? "OK" : "Failed");
}
void NetworkInterface::setup(transportType tt, protocolType pt)
{
NetworkInterface::setup(tt, pt, LISTEN_PORT);
}
void NetworkInterface::setup(transportType tt)
{
NetworkInterface::setup(tt, TCP, LISTEN_PORT);
}
void NetworkInterface::setup()
{
NetworkInterface::setup(ETHERNET, TCP, LISTEN_PORT);
}
void NetworkInterface::loop()
{
switch (t)
{
case WIFI:
{
if (wifiTransport->isConnected()){
wifiTransport->loop();
}
break;
}
case ETHERNET:
{
if (ethernetTransport->isConnected()) {
ethernetTransport->loop();
}
break;
}
}
}
void NetworkInterface::setHttpCallback(HttpCallback callback)
{
2020-10-26 10:29:40 +01:00
this->httpCallback = callback;
}
2020-10-26 10:29:40 +01:00
HttpCallback NetworkInterface::getHttpCallback()
{
2020-10-26 10:29:40 +01:00
return this->httpCallback;
}
2020-10-26 10:29:40 +01:00
NetworkInterface::NetworkInterface(){}
NetworkInterface::~NetworkInterface(){}