Improved ethernet connection

This commit is contained in:
Gunnar Skjold
2023-12-10 16:56:13 +01:00
parent ee462ec468
commit 0f22fd561e
11 changed files with 112 additions and 38 deletions

View File

@@ -0,0 +1,45 @@
/**
* @copyright Utilitech AS 2023
* License: Fair Source
*
*/
#ifndef _CONNECTIONHANDLER_H
#define _CONNECTIONHANDLER_H
#include "AmsConfiguration.h"
#if defined(ESP8266)
#include <ESP8266WiFi.h>
#elif defined(ESP32)
#include <WiFi.h>
#endif
#define NETWORK_MODE_WIFI_CLIENT 1
#define NETWORK_MODE_WIFI_AP 2
#define NETWORK_MODE_ETH_CLIENT 3
class ConnectionHandler {
public:
virtual ~ConnectionHandler() {};
virtual bool connect(NetworkConfig config, SystemConfig sys);
virtual void disconnect(unsigned long reconnectDelay);
virtual bool isConnected();
virtual bool isConfigChanged();
virtual void getCurrentConfig(NetworkConfig& networkConfig);
virtual IPAddress getIP();
virtual IPAddress getSubnetMask();
virtual IPAddress getGateway();
virtual IPAddress getDns(uint8_t idx);
#if defined(ESP32)
virtual void eventHandler(WiFiEvent_t event, WiFiEventInfo_t info);
#endif
uint8_t getMode() {
return this->mode;
}
protected:
uint8_t mode;
};
#endif

View File

@@ -0,0 +1,48 @@
/**
* @copyright Utilitech AS 2023
* License: Fair Source
*
*/
#ifndef _ETHERNETCONNECTIONHANDLER_H
#define _ETHERNETCONNECTIONHANDLER_H
#include "ConnectionHandler.h"
#include <Arduino.h>
#include "RemoteDebug.h"
#define CONNECTION_TIMEOUT 30000
class EthernetConnectionHandler : public ConnectionHandler {
public:
EthernetConnectionHandler(RemoteDebug* debugger);
bool connect(NetworkConfig config, SystemConfig sys);
void disconnect(unsigned long reconnectDelay);
bool isConnected();
bool isConfigChanged();
void getCurrentConfig(NetworkConfig& networkConfig);
IPAddress getIP();
IPAddress getSubnetMask();
IPAddress getGateway();
IPAddress getDns(uint8_t idx);
#if defined(ESP32)
void eventHandler(WiFiEvent_t event, WiFiEventInfo_t info);
#endif
private:
RemoteDebug* debugger;
NetworkConfig config;
bool connected = false;
bool configChanged = false;
unsigned long timeout = CONNECTION_TIMEOUT;
unsigned long lastRetry = 0;
int8_t ethPowerPin = -1;
uint8_t ethEnablePin = 0;
};
#endif

View File

@@ -0,0 +1,43 @@
/**
* @copyright Utilitech AS 2023
* License: Fair Source
*
*/
#ifndef _WIFIACCESSPOINTCONNECTIONHANDLER_H
#define _WIFIACCESSPOINTCONNECTIONHANDLER_H
#include "ConnectionHandler.h"
#include <Arduino.h>
#include "RemoteDebug.h"
#include <DNSServer.h>
class WiFiAccessPointConnectionHandler : public ConnectionHandler {
public:
WiFiAccessPointConnectionHandler(RemoteDebug* debugger);
bool connect(NetworkConfig config, SystemConfig sys);
void disconnect(unsigned long reconnectDelay);
bool isConnected();
bool isConfigChanged();
void getCurrentConfig(NetworkConfig& networkConfig);
IPAddress getIP();
IPAddress getSubnetMask();
IPAddress getGateway();
IPAddress getDns(uint8_t idx);
#if defined(ESP32)
void eventHandler(WiFiEvent_t event, WiFiEventInfo_t info);
#endif
private:
RemoteDebug* debugger;
NetworkConfig config;
DNSServer dnsServer;
bool connected = false;
bool configChanged = false;
};
#endif

View File

@@ -0,0 +1,46 @@
/**
* @copyright Utilitech AS 2023
* License: Fair Source
*
*/
#ifndef _WIFICLIENTCONNECTIONHANDLER_H
#define _WIFICLIENTCONNECTIONHANDLER_H
#include "ConnectionHandler.h"
#include <Arduino.h>
#include "RemoteDebug.h"
#define CONNECTION_TIMEOUT 30000
#define RECONNECT_TIMEOUT 5000
class WiFiClientConnectionHandler : public ConnectionHandler {
public:
WiFiClientConnectionHandler(RemoteDebug* debugger);
bool connect(NetworkConfig config, SystemConfig sys);
void disconnect(unsigned long reconnectDelay);
bool isConnected();
bool isConfigChanged();
void getCurrentConfig(NetworkConfig& networkConfig);
IPAddress getIP();
IPAddress getSubnetMask();
IPAddress getGateway();
IPAddress getDns(uint8_t idx);
#if defined(ESP32)
void eventHandler(WiFiEvent_t event, WiFiEventInfo_t info);
#endif
private:
RemoteDebug* debugger;
NetworkConfig config;
bool busPowered = false;
bool firstConnect = true;
bool configChanged = false;
unsigned long timeout = CONNECTION_TIMEOUT;
unsigned long lastRetry = 0;
};
#endif