clangtidy corrections part 2
This commit is contained in:
+79
-45
@@ -1,33 +1,38 @@
|
||||
/**
|
||||
* @file network.cpp
|
||||
* @author Alexander Klein (alex@kleiax.de)
|
||||
* @brief
|
||||
* @brief
|
||||
* @version 0.1
|
||||
* @date 2023-09-18
|
||||
*
|
||||
*
|
||||
* @copyright Copyright (c) 2023
|
||||
*
|
||||
*
|
||||
*/
|
||||
|
||||
#include "network.h"
|
||||
|
||||
Network::Network(const char *ssid, const char *passphrase) {
|
||||
if (!WiFi.mode(WIFI_AP_STA))
|
||||
Network::Network(const char *ssid, const char *passphrase)
|
||||
{
|
||||
if (!WiFiGenericClass::mode(WIFI_AP_STA))
|
||||
{
|
||||
std::cout << "Network::connectWiFi failed WiFi.mode" << std::endl;
|
||||
}
|
||||
|
||||
this->init(ssid, passphrase);
|
||||
}
|
||||
|
||||
Network::Network(const char *ssid, const char *passphrase, NetworkAdresses adresses) {
|
||||
this->adresses = adresses;
|
||||
|
||||
if (!WiFi.mode(WIFI_AP_STA))
|
||||
Network::Network(const char *ssid, const char *passphrase, NetworkAdresses adresses)
|
||||
: adresses{adresses}
|
||||
{
|
||||
if (!WiFiGenericClass::mode(WIFI_AP_STA))
|
||||
{
|
||||
std::cout << "Network::connectWiFi failed WiFi.mode" << std::endl;
|
||||
}
|
||||
|
||||
if (!WiFi.config(this->adresses.localIP,
|
||||
this->adresses.gateway,
|
||||
this->adresses.subnet,
|
||||
this->adresses.dnsServer))
|
||||
this->adresses.dnsServer))
|
||||
{
|
||||
std::cout << "STA Failed to configure" << std::endl;
|
||||
}
|
||||
@@ -35,13 +40,15 @@ Network::Network(const char *ssid, const char *passphrase, NetworkAdresses adres
|
||||
this->init(ssid, passphrase);
|
||||
}
|
||||
|
||||
Network::~Network() {
|
||||
if (this->mqttClient)
|
||||
delete mqttClient;
|
||||
Network::~Network()
|
||||
{
|
||||
delete mqttClient;
|
||||
}
|
||||
|
||||
bool Network::activateEspNow(recieveCallbackPtr reci, sendCallbackPtr send) {
|
||||
if (esp_now_init() != ESP_OK) {
|
||||
bool Network::activateEspNow(recieveCallbackPtr reci, sendCallbackPtr send)
|
||||
{
|
||||
if (esp_now_init() != ESP_OK)
|
||||
{
|
||||
std::cout << "Network::activateEspNow - Error initializing ESP-NOW" << std::endl;
|
||||
return false;
|
||||
}
|
||||
@@ -49,11 +56,12 @@ bool Network::activateEspNow(recieveCallbackPtr reci, sendCallbackPtr send) {
|
||||
esp_now_register_send_cb(send);
|
||||
|
||||
esp_now_peer_info_t peerInfo = {};
|
||||
memcpy(peerInfo.peer_addr, this->broadcastAddress, 6);
|
||||
peerInfo.channel = 0;
|
||||
memcpy(static_cast<void *>(peerInfo.peer_addr), static_cast<const void *>(this->broadcastAddress), 6);
|
||||
peerInfo.channel = 0;
|
||||
peerInfo.encrypt = false;
|
||||
|
||||
if (esp_now_add_peer(&peerInfo) != ESP_OK) {
|
||||
if (esp_now_add_peer(&peerInfo) != ESP_OK)
|
||||
{
|
||||
std::cout << "Network::connectEspNow - Failed to add peer" << std::endl;
|
||||
return false;
|
||||
}
|
||||
@@ -62,7 +70,8 @@ bool Network::activateEspNow(recieveCallbackPtr reci, sendCallbackPtr send) {
|
||||
return true;
|
||||
}
|
||||
|
||||
bool Network::activateMqtt(const char *user, const char *passphrase) {
|
||||
bool Network::activateMqtt(const char *user, const char *passphrase)
|
||||
{
|
||||
this->mqttUser = user;
|
||||
this->mqttPassphrase = passphrase;
|
||||
|
||||
@@ -71,53 +80,68 @@ bool Network::activateMqtt(const char *user, const char *passphrase) {
|
||||
this->mqttClient->setSocketTimeout(1);
|
||||
|
||||
if (this->wifiConnected)
|
||||
{
|
||||
return this->connectMqtt();
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
void Network::printIPs() {
|
||||
const void Network::printIPs()
|
||||
{
|
||||
std::cout << std::endl;
|
||||
if (!this->wifiConnected) {
|
||||
if (!this->wifiConnected)
|
||||
{
|
||||
std::cout << "WiFi is not connected." << std::endl;
|
||||
return;
|
||||
}
|
||||
std::cout << "WiFi is connected to" << std::endl;
|
||||
std::cout << "IP address: " << std::endl;
|
||||
std::cout << WiFi.localIP().toString().c_str() << std::endl;
|
||||
std::cout << "WiFi MAC Address: " << WiFi.macAddress().c_str() << std::endl << std::endl;
|
||||
std::cout << "WiFi MAC Address: " << WiFi.macAddress().c_str() << std::endl
|
||||
<< std::endl;
|
||||
}
|
||||
|
||||
uint8_t Network::getCurrentChannel() {
|
||||
uint8_t channel;
|
||||
wifi_second_chan_t secondChannel;
|
||||
if (esp_wifi_get_channel(&channel, &secondChannel) != ESP_OK) {
|
||||
uint8_t Network::getCurrentChannel()
|
||||
{
|
||||
uint8_t channel = 0;
|
||||
wifi_second_chan_t secondChannel = WIFI_SECOND_CHAN_NONE;
|
||||
if (esp_wifi_get_channel(&channel, &secondChannel) != ESP_OK)
|
||||
{
|
||||
std::cout << "Network::getCurrentChannel - Error!" << std::endl;
|
||||
return -1;
|
||||
}
|
||||
|
||||
|
||||
return channel;
|
||||
}
|
||||
|
||||
void Network::runAsChild() {
|
||||
void Network::runAsChild()
|
||||
{
|
||||
if (!this->initSucessful)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
this->checkWifi();
|
||||
|
||||
if (this->wifiConnected && this->mqttClient)
|
||||
if (this->wifiConnected && static_cast<bool>(this->mqttClient))
|
||||
{
|
||||
this->checkMqtt();
|
||||
}
|
||||
}
|
||||
|
||||
void Network::init(const char *ssid, const char *passphrase) {
|
||||
void Network::init(const char *ssid, const char *passphrase)
|
||||
{
|
||||
// Connect to Wi-Fi network with SSID and password
|
||||
std::cout << "Connecting to " << ssid << std::endl;
|
||||
WiFi.begin(ssid, passphrase);
|
||||
uint8_t timeout = Network::wifiConnectTimeout;
|
||||
while (WiFi.status() != WL_CONNECTED) {
|
||||
while (WiFiSTAClass::status() != WL_CONNECTED)
|
||||
{
|
||||
delay(Network::wifiConnectLoopTime);
|
||||
std::cout << "." << std::flush;
|
||||
timeout--;
|
||||
if (timeout == 0) {
|
||||
if (timeout == 0)
|
||||
{
|
||||
std::cout << std::endl;
|
||||
std::cout << "WiFi NOT connected." << std::endl;
|
||||
return;
|
||||
@@ -128,9 +152,9 @@ void Network::init(const char *ssid, const char *passphrase) {
|
||||
this->printIPs();
|
||||
}
|
||||
|
||||
void Network::checkWifi() {
|
||||
if ((WiFi.status() != WL_CONNECTED)
|
||||
&& (millis() - this->lastWifiRecoonectAttemp >= Network::wifiReconnectDelay))
|
||||
void Network::checkWifi()
|
||||
{
|
||||
if ((WiFiSTAClass::status() != WL_CONNECTED) && (millis() - this->lastWifiRecoonectAttemp >= Network::wifiReconnectDelay))
|
||||
{
|
||||
std::cout << "Reconnecting to WiFi..." << std::endl;
|
||||
WiFi.disconnect();
|
||||
@@ -139,29 +163,39 @@ void Network::checkWifi() {
|
||||
}
|
||||
}
|
||||
|
||||
void Network::checkMqtt() {
|
||||
if (!this->mqttClient->connected()
|
||||
&& millis() - this->lastMqttReconnectAttemp > Network::mqttReconnectDelay)
|
||||
void Network::checkMqtt()
|
||||
{
|
||||
if (!this->mqttClient->connected() && millis() - this->lastMqttReconnectAttemp > Network::mqttReconnectDelay)
|
||||
{
|
||||
this->mqttConnected = this->connectMqtt();
|
||||
this->lastMqttReconnectAttemp = millis();
|
||||
}
|
||||
|
||||
if (this->mqttConnected)
|
||||
{
|
||||
this->mqttClient->loop();
|
||||
}
|
||||
}
|
||||
|
||||
bool Network::connectMqtt() {
|
||||
bool Network::connectMqtt()
|
||||
{
|
||||
String clientId = "ESP32Rover-";
|
||||
clientId += String(random(0xffff), HEX);
|
||||
clientId += String(random(), HEX);
|
||||
|
||||
if (this->mqttUser) {
|
||||
if (static_cast<bool>(this->mqttUser))
|
||||
{
|
||||
if (this->mqttClient->connect(clientId.c_str(), this->mqttUser, this->mqttPassphrase))
|
||||
{
|
||||
this->mqttClient->publish("Rover/Info", "Connected to Mqtt-Broker");
|
||||
} else {
|
||||
if (this->mqttClient->connect(clientId.c_str()))
|
||||
this->mqttClient->publish("Rover/Info", "Connected to Mqtt-Broker");
|
||||
}
|
||||
}
|
||||
|
||||
else
|
||||
{
|
||||
if (this->mqttClient->connect(clientId.c_str()))
|
||||
{
|
||||
this->mqttClient->publish("Rover/Info", "Connected to Mqtt-Broker");
|
||||
}
|
||||
}
|
||||
|
||||
return this->mqttClient->connected();
|
||||
}
|
||||
|
||||
+44
-43
@@ -1,12 +1,12 @@
|
||||
/**
|
||||
* @file network.h
|
||||
* @author Alexander Klein (alex@kleiax.de)
|
||||
* @brief
|
||||
* @brief
|
||||
* @version 0.1
|
||||
* @date 2023-09-18
|
||||
*
|
||||
*
|
||||
* @copyright Copyright (c) 2023
|
||||
*
|
||||
*
|
||||
*/
|
||||
|
||||
#ifndef NETWORK_H
|
||||
@@ -20,10 +20,11 @@
|
||||
#include <esp_now.h>
|
||||
#include <esp_wifi.h>
|
||||
|
||||
typedef void (*recieveCallbackPtr) (const uint8_t * mac, const uint8_t *incomingData, int len);
|
||||
typedef void (*sendCallbackPtr) (const uint8_t *mac_addr, esp_now_send_status_t status);
|
||||
typedef void (*recieveCallbackPtr)(const uint8_t *mac, const uint8_t *incomingData, int len);
|
||||
typedef void (*sendCallbackPtr)(const uint8_t *mac_addr, esp_now_send_status_t status);
|
||||
|
||||
struct NetworkAdresses {
|
||||
struct NetworkAdresses
|
||||
{
|
||||
IPAddress localIP;
|
||||
IPAddress gateway;
|
||||
IPAddress subnet;
|
||||
@@ -32,53 +33,53 @@ struct NetworkAdresses {
|
||||
uint16_t mqttPort = 1883;
|
||||
};
|
||||
|
||||
class Network : public Component {
|
||||
public:
|
||||
Network(const char *ssid, const char *passphrase);
|
||||
Network(const char *ssid, const char *passphrase, NetworkAdresses adresses);
|
||||
class Network : public Component
|
||||
{
|
||||
public:
|
||||
Network(const char *ssid, const char *passphrase);
|
||||
Network(const char *ssid, const char *passphrase, NetworkAdresses adresses);
|
||||
|
||||
~Network();
|
||||
~Network();
|
||||
|
||||
bool activateEspNow(recieveCallbackPtr reci, sendCallbackPtr send);
|
||||
bool activateMqtt(const char *user = nullptr, const char *passphrase = nullptr);
|
||||
bool activateEspNow(recieveCallbackPtr reci, sendCallbackPtr send);
|
||||
bool activateMqtt(const char *user = nullptr, const char *passphrase = nullptr);
|
||||
|
||||
void printIPs();
|
||||
const void printIPs();
|
||||
|
||||
bool isWifiConnected() const { return this->wifiConnected; }
|
||||
bool isMqttConnected() const { return this->mqttConnected; }
|
||||
const uint8_t* getBroadcastAddress() const { return this->broadcastAddress; }
|
||||
PubSubClient* getMqttClient() const { return this->mqttClient; }
|
||||
bool isWifiConnected() const { return this->wifiConnected; }
|
||||
bool isMqttConnected() const { return this->mqttConnected; }
|
||||
const uint8_t *getBroadcastAddress() const { return this->broadcastAddress; }
|
||||
PubSubClient *getMqttClient() const { return this->mqttClient; }
|
||||
|
||||
static uint8_t getCurrentChannel();
|
||||
|
||||
private:
|
||||
void run() override {};
|
||||
void runAsChild() override;
|
||||
void init(const char *ssid, const char *passphrase);
|
||||
void checkWifi();
|
||||
void checkMqtt();
|
||||
bool connectMqtt();
|
||||
static uint8_t getCurrentChannel();
|
||||
|
||||
NetworkAdresses adresses;
|
||||
WiFiClient wifiClient;
|
||||
PubSubClient* mqttClient = nullptr;
|
||||
private:
|
||||
void run() override{};
|
||||
void runAsChild() override;
|
||||
void init(const char *ssid, const char *passphrase);
|
||||
void checkWifi();
|
||||
void checkMqtt();
|
||||
bool connectMqtt();
|
||||
|
||||
bool initSucessful = false;
|
||||
bool wifiConnected = false;
|
||||
bool mqttConnected = false;
|
||||
NetworkAdresses adresses;
|
||||
WiFiClient wifiClient;
|
||||
PubSubClient *mqttClient = nullptr;
|
||||
|
||||
const char *mqttUser;
|
||||
const char *mqttPassphrase;
|
||||
bool initSucessful = false;
|
||||
bool wifiConnected = false;
|
||||
bool mqttConnected = false;
|
||||
|
||||
uint8_t broadcastAddress[6] = {0xC8, 0xC9, 0xA3, 0xC8, 0x57, 0x10};
|
||||
uint32_t lastWifiRecoonectAttemp = 0;
|
||||
uint32_t lastMqttReconnectAttemp = 0;
|
||||
const char *mqttUser = nullptr;
|
||||
const char *mqttPassphrase = nullptr;
|
||||
|
||||
static constexpr uint8_t wifiConnectTimeout = 20;
|
||||
static constexpr uint16_t wifiConnectLoopTime = 500;
|
||||
static constexpr uint16_t wifiReconnectDelay = 5000;
|
||||
static constexpr uint16_t mqttReconnectDelay = 2500;
|
||||
uint8_t broadcastAddress[6] = {0xC8, 0xC9, 0xA3, 0xC8, 0x57, 0x10};
|
||||
uint32_t lastWifiRecoonectAttemp = 0;
|
||||
uint32_t lastMqttReconnectAttemp = 0;
|
||||
|
||||
static constexpr uint8_t wifiConnectTimeout = 20;
|
||||
static constexpr uint16_t wifiConnectLoopTime = 500;
|
||||
static constexpr uint16_t wifiReconnectDelay = 5000;
|
||||
static constexpr uint16_t mqttReconnectDelay = 2500;
|
||||
};
|
||||
|
||||
#endif //NETWORK_H
|
||||
#endif // NETWORK_H
|
||||
|
||||
Reference in New Issue
Block a user