Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor network devices #289

Merged
merged 4 commits into from
Feb 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ jobs:
cd ~/arduino*
./install.sh
./arduino --pref "boardsmanager.additional.urls=https://raw.githubusercontent.com/espressif/arduino-esp32/gh-pages/package_esp32_index.json" --save-prefs
./arduino --install-boards esp32:esp32
./arduino --install-boards esp32:esp32:2.0.9
- name: Install Arduino CMake Toolchain
uses: actions/checkout@v2
with:
Expand Down
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ set(SRCFILES
NetworkLock.cpp
NetworkOpener.cpp
networkDevices/NetworkDevice.h
networkDevices/NetworkDevice.cpp
networkDevices/WifiDevice.cpp
networkDevices/W5500Device.cpp
networkDevices/EthLan8720Device.cpp
Expand Down
2 changes: 1 addition & 1 deletion lib/MqttLogger/library.properties
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@ category=Communication
url=https://github.com/androbi-com/MqttLogger
architectures=*
includes=MqttLogger.h
depends=PubSubClient
depends=espMqttClient
10 changes: 5 additions & 5 deletions lib/MqttLogger/src/MqttLogger.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ MqttLogger::MqttLogger(MqttLoggerMode mode)
this->setBufferSize(MQTT_MAX_PACKET_SIZE);
}

MqttLogger::MqttLogger(NetworkDevice* client, const char* topic, MqttLoggerMode mode)
MqttLogger::MqttLogger(MqttClient& client, const char* topic, MqttLoggerMode mode)
{
this->setClient(client);
this->setTopic(topic);
Expand All @@ -19,9 +19,9 @@ MqttLogger::~MqttLogger()
{
}

void MqttLogger::setClient(NetworkDevice* client)
void MqttLogger::setClient(MqttClient& client)
{
this->client = client;
this->client = &client;
}

void MqttLogger::setTopic(const char* topic)
Expand Down Expand Up @@ -74,9 +74,9 @@ void MqttLogger::sendBuffer()
if (this->bufferCnt > 0)
{
bool doSerial = this->mode==MqttLoggerMode::SerialOnly || this->mode==MqttLoggerMode::MqttAndSerial;
if (this->mode!=MqttLoggerMode::SerialOnly && this->client != NULL && this->client->mqttConnected())
if (this->mode!=MqttLoggerMode::SerialOnly && this->client != NULL && this->client->connected())
{
this->client->mqttPublish(topic, 0, true, (uint8_t*)this->buffer, this->bufferCnt);
this->client->publish(topic, 0, true, this->buffer, this->bufferCnt);
} else if (this->mode == MqttLoggerMode::MqttAndSerialFallback)
{
doSerial = true;
Expand Down
8 changes: 4 additions & 4 deletions lib/MqttLogger/src/MqttLogger.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

#include <Arduino.h>
#include <Print.h>
#include "../../../networkDevices/NetworkDevice.h"
#include <espMqttClient.h>

#define MQTT_MAX_PACKET_SIZE 1024

Expand All @@ -29,16 +29,16 @@ class MqttLogger : public Print
uint8_t* buffer;
uint8_t* bufferEnd;
uint16_t bufferCnt = 0, bufferSize = 0;
NetworkDevice* client;
MqttClient* client;
MqttLoggerMode mode;
void sendBuffer();

public:
MqttLogger(MqttLoggerMode mode=MqttLoggerMode::MqttAndSerialFallback);
MqttLogger(NetworkDevice* client, const char* topic, MqttLoggerMode mode=MqttLoggerMode::MqttAndSerialFallback);
MqttLogger(MqttClient& client, const char* topic, MqttLoggerMode mode=MqttLoggerMode::MqttAndSerialFallback);
~MqttLogger();

void setClient(NetworkDevice* client);
void setClient(MqttClient& client);
void setTopic(const char* topic);
void setMode(MqttLoggerMode mode);
void setRetained(boolean retained);
Expand Down
206 changes: 1 addition & 205 deletions networkDevices/EthLan8720Device.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ EthLan8720Device::EthLan8720Device(const String& hostname, Preferences* preferen
String pathStr = preferences->getString(preference_mqtt_lock_path);
pathStr.concat(mqtt_topic_log);
strcpy(_path, pathStr.c_str());
Log = new MqttLogger(this, _path, MqttLoggerMode::MqttAndSerial);
Log = new MqttLogger(*getMqttClient(), _path, MqttLoggerMode::MqttAndSerial);
}
}

Expand Down Expand Up @@ -96,12 +96,6 @@ void EthLan8720Device::reconfigure()
restartEsp(RestartReason::ReconfigureLAN8720);
}

void EthLan8720Device::printError()
{
Log->print(F("Free Heap: "));
Log->println(ESP.getFreeHeap());
}

bool EthLan8720Device::supportsEncryption()
{
return true;
Expand Down Expand Up @@ -132,20 +126,6 @@ ReconnectStatus EthLan8720Device::reconnect()
return isConnected() ? ReconnectStatus::Success : ReconnectStatus::Failure;
}

void EthLan8720Device::update()
{
if(_mqttEnabled)
{
if (_useEncryption)
{
_mqttClientSecure->loop();
} else
{
_mqttClient->loop();
}
}
}

void EthLan8720Device::onDisconnected()
{
if(millis() > 60000)
Expand All @@ -163,187 +143,3 @@ String EthLan8720Device::localIP()
{
return ETH.localIP().toString();
}

void EthLan8720Device::mqttSetClientId(const char *clientId)
{
if(_useEncryption)
{
_mqttClientSecure->setClientId(clientId);
}
else
{
_mqttClient->setClientId(clientId);
}
}

void EthLan8720Device::mqttSetCleanSession(bool cleanSession)
{
if(_useEncryption)
{
_mqttClientSecure->setCleanSession(cleanSession);
}
else
{
_mqttClient->setCleanSession(cleanSession);
}
}

uint16_t EthLan8720Device::mqttPublish(const char *topic, uint8_t qos, bool retain, const char *payload)
{
if(_useEncryption)
{
return _mqttClientSecure->publish(topic, qos, retain, payload);
}
else
{
return _mqttClient->publish(topic, qos, retain, payload);
}
}

uint16_t EthLan8720Device::mqttPublish(const char *topic, uint8_t qos, bool retain, const uint8_t *payload, size_t length)
{
if(_useEncryption)
{
return _mqttClientSecure->publish(topic, qos, retain, payload, length);
}
else
{
return _mqttClient->publish(topic, qos, retain, payload, length);
}
}

bool EthLan8720Device::mqttConnected() const
{
if(_useEncryption)
{
return _mqttClientSecure->connected();
}
else
{
return _mqttClient->connected();
}
}

void EthLan8720Device::mqttSetServer(const char *host, uint16_t port)
{
if(_useEncryption)
{
_mqttClientSecure->setServer(host, port);
}
else
{
_mqttClient->setServer(host, port);
}
}

bool EthLan8720Device::mqttConnect()
{
if(_useEncryption)
{
return _mqttClientSecure->connect();
}
else
{
return _mqttClient->connect();
}
}

bool EthLan8720Device::mqttDisconnect(bool force)
{
if(_useEncryption)
{
return _mqttClientSecure->disconnect(force);
}
else
{
return _mqttClient->disconnect(force);
}
}

void EthLan8720Device::setWill(const char *topic, uint8_t qos, bool retain, const char *payload)
{
if(_useEncryption)
{
_mqttClientSecure->setWill(topic, qos, retain, payload);
}
else
{
_mqttClient->setWill(topic, qos, retain, payload);
}
}

void EthLan8720Device::mqttSetCredentials(const char *username, const char *password)
{
if(_useEncryption)
{
_mqttClientSecure->setCredentials(username, password);
}
else
{
_mqttClient->setCredentials(username, password);
}
}

void EthLan8720Device::mqttOnMessage(espMqttClientTypes::OnMessageCallback callback)
{
if(_useEncryption)
{
_mqttClientSecure->onMessage(callback);
}
else
{
_mqttClient->onMessage(callback);
}
}


void EthLan8720Device::mqttOnConnect(espMqttClientTypes::OnConnectCallback callback)
{
if(_useEncryption)
{
_mqttClientSecure->onConnect(callback);
}
else
{
_mqttClient->onConnect(callback);
}
}

void EthLan8720Device::mqttOnDisconnect(espMqttClientTypes::OnDisconnectCallback callback)
{
if(_useEncryption)
{
_mqttClientSecure->onDisconnect(callback);
}
else
{
_mqttClient->onDisconnect(callback);
}
}


uint16_t EthLan8720Device::mqttSubscribe(const char *topic, uint8_t qos)
{
if(_useEncryption)
{
return _mqttClientSecure->subscribe(topic, qos);
}
else
{
return _mqttClient->subscribe(topic, qos);
}
}

void EthLan8720Device::disableMqtt()
{
if (_useEncryption)
{
_mqttClientSecure->disconnect();
} else
{
_mqttClient->disconnect();
}

_mqttEnabled = false;
}

38 changes: 0 additions & 38 deletions networkDevices/EthLan8720Device.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,57 +28,20 @@ class EthLan8720Device : public NetworkDevice
virtual void initialize();
virtual void reconfigure();
virtual ReconnectStatus reconnect();
virtual void printError();
bool supportsEncryption() override;

virtual void update();

virtual bool isConnected();

int8_t signalStrength() override;

String localIP() override;

void mqttSetClientId(const char *clientId) override;

void mqttSetCleanSession(bool cleanSession) override;

uint16_t mqttPublish(const char *topic, uint8_t qos, bool retain, const char *payload) override;

uint16_t mqttPublish(const char *topic, uint8_t qos, bool retain, const uint8_t *payload, size_t length) override;

bool mqttConnected() const override;

void mqttSetServer(const char *host, uint16_t port) override;

bool mqttConnect() override;

bool mqttDisconnect(bool force) override;

void setWill(const char *topic, uint8_t qos, bool retain, const char *payload) override;

void mqttSetCredentials(const char *username, const char *password) override;

void mqttOnMessage(espMqttClientTypes::OnMessageCallback callback) override;

void mqttOnConnect(espMqttClientTypes::OnConnectCallback callback) override;

void mqttOnDisconnect(espMqttClientTypes::OnDisconnectCallback callback) override;

uint16_t mqttSubscribe(const char *topic, uint8_t qos) override;

void disableMqtt() override;

private:
void onDisconnected();

espMqttClient* _mqttClient = nullptr;
espMqttClientSecure* _mqttClientSecure = nullptr;

bool _restartOnDisconnect = false;
bool _startAp = false;
char* _path;
bool _useEncryption = false;
bool _hardwareInitialized = false;
bool _lastConnected = false;

Expand All @@ -90,7 +53,6 @@ class EthLan8720Device : public NetworkDevice
eth_phy_type_t _type;
eth_clock_mode_t _clock_mode;
bool _use_mac_from_efuse;
bool _mqttEnabled = true;

char _ca[TLS_CA_MAX_SIZE] = {0};
char _cert[TLS_CERT_MAX_SIZE] = {0};
Expand Down
Loading
Loading