mirror of
https://github.com/UtilitechAS/amsreader-firmware.git
synced 2026-01-26 12:13:10 +00:00
Improved ethernet connection
This commit is contained in:
45
lib/ConnectionHandler/include/ConnectionHandler.h
Normal file
45
lib/ConnectionHandler/include/ConnectionHandler.h
Normal 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
|
||||
48
lib/ConnectionHandler/include/EthernetConnectionHandler.h
Normal file
48
lib/ConnectionHandler/include/EthernetConnectionHandler.h
Normal 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
|
||||
@@ -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
|
||||
46
lib/ConnectionHandler/include/WiFiClientConnectionHandler.h
Normal file
46
lib/ConnectionHandler/include/WiFiClientConnectionHandler.h
Normal 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
|
||||
Reference in New Issue
Block a user