⚡ Full migration to esp-idf
This commit is contained in:
@@ -0,0 +1,144 @@
|
||||
#pragma once
|
||||
|
||||
#include <lwip/sockets.h>
|
||||
#include <lwip/netdb.h>
|
||||
#include <esp_log.h>
|
||||
#include <utils/ip_address.h>
|
||||
#include <freertos/FreeRTOS.h>
|
||||
#include <freertos/task.h>
|
||||
#include <string>
|
||||
#include <atomic>
|
||||
|
||||
#define DNS_PORT 53
|
||||
#define DNS_MAX_PACKET_SIZE 512
|
||||
|
||||
class DNSServer {
|
||||
public:
|
||||
DNSServer() : _socket(-1), _running(false), _task(nullptr) {}
|
||||
~DNSServer() { stop(); }
|
||||
|
||||
bool start(uint16_t port, const char* domainName, const IPAddress& resolvedIP) {
|
||||
if (_running) return true;
|
||||
|
||||
_port = port;
|
||||
_resolvedIP = resolvedIP;
|
||||
_domainName = domainName ? domainName : "*";
|
||||
|
||||
_socket = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
|
||||
if (_socket < 0) {
|
||||
ESP_LOGE("DNSServer", "Failed to create socket");
|
||||
return false;
|
||||
}
|
||||
|
||||
int opt = 1;
|
||||
setsockopt(_socket, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt));
|
||||
|
||||
struct timeval tv;
|
||||
tv.tv_sec = 1;
|
||||
tv.tv_usec = 0;
|
||||
setsockopt(_socket, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv));
|
||||
|
||||
struct sockaddr_in serverAddr = {};
|
||||
serverAddr.sin_family = AF_INET;
|
||||
serverAddr.sin_addr.s_addr = INADDR_ANY;
|
||||
serverAddr.sin_port = htons(_port);
|
||||
|
||||
if (bind(_socket, (struct sockaddr*)&serverAddr, sizeof(serverAddr)) < 0) {
|
||||
ESP_LOGE("DNSServer", "Failed to bind socket");
|
||||
close(_socket);
|
||||
_socket = -1;
|
||||
return false;
|
||||
}
|
||||
|
||||
_running = true;
|
||||
xTaskCreate(dnsTask, "dns_server", 4096, this, 3, &_task);
|
||||
|
||||
ESP_LOGI("DNSServer", "Started on port %d, resolving to %s", _port, _resolvedIP.toString().c_str());
|
||||
return true;
|
||||
}
|
||||
|
||||
void stop() {
|
||||
_running = false;
|
||||
if (_task) {
|
||||
vTaskDelay(100 / portTICK_PERIOD_MS);
|
||||
_task = nullptr;
|
||||
}
|
||||
if (_socket >= 0) {
|
||||
close(_socket);
|
||||
_socket = -1;
|
||||
}
|
||||
ESP_LOGI("DNSServer", "Stopped");
|
||||
}
|
||||
|
||||
void processNextRequest() {}
|
||||
|
||||
private:
|
||||
static void dnsTask(void* param) {
|
||||
DNSServer* self = static_cast<DNSServer*>(param);
|
||||
self->run();
|
||||
vTaskDelete(nullptr);
|
||||
}
|
||||
|
||||
void run() {
|
||||
uint8_t buffer[DNS_MAX_PACKET_SIZE];
|
||||
struct sockaddr_in clientAddr;
|
||||
socklen_t clientAddrLen = sizeof(clientAddr);
|
||||
|
||||
while (_running) {
|
||||
int len = recvfrom(_socket, buffer, DNS_MAX_PACKET_SIZE, 0, (struct sockaddr*)&clientAddr, &clientAddrLen);
|
||||
if (len > 0) {
|
||||
processRequest(buffer, len, &clientAddr);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void processRequest(uint8_t* buffer, int len, struct sockaddr_in* clientAddr) {
|
||||
if (len < 12) return;
|
||||
|
||||
uint16_t flags = (buffer[2] << 8) | buffer[3];
|
||||
if ((flags & 0x8000) != 0) return;
|
||||
|
||||
uint8_t response[DNS_MAX_PACKET_SIZE];
|
||||
memcpy(response, buffer, len);
|
||||
|
||||
response[2] = 0x81;
|
||||
response[3] = 0x80;
|
||||
|
||||
response[6] = 0x00;
|
||||
response[7] = 0x01;
|
||||
|
||||
int responseLen = len;
|
||||
|
||||
response[responseLen++] = 0xC0;
|
||||
response[responseLen++] = 0x0C;
|
||||
|
||||
response[responseLen++] = 0x00;
|
||||
response[responseLen++] = 0x01;
|
||||
|
||||
response[responseLen++] = 0x00;
|
||||
response[responseLen++] = 0x01;
|
||||
|
||||
response[responseLen++] = 0x00;
|
||||
response[responseLen++] = 0x00;
|
||||
response[responseLen++] = 0x00;
|
||||
response[responseLen++] = 0x3C;
|
||||
|
||||
response[responseLen++] = 0x00;
|
||||
response[responseLen++] = 0x04;
|
||||
|
||||
uint32_t ip = static_cast<uint32_t>(_resolvedIP);
|
||||
response[responseLen++] = ip & 0xFF;
|
||||
response[responseLen++] = (ip >> 8) & 0xFF;
|
||||
response[responseLen++] = (ip >> 16) & 0xFF;
|
||||
response[responseLen++] = (ip >> 24) & 0xFF;
|
||||
|
||||
sendto(_socket, response, responseLen, 0, (struct sockaddr*)clientAddr, sizeof(*clientAddr));
|
||||
}
|
||||
|
||||
int _socket;
|
||||
uint16_t _port;
|
||||
IPAddress _resolvedIP;
|
||||
std::string _domainName;
|
||||
std::atomic<bool> _running;
|
||||
TaskHandle_t _task;
|
||||
};
|
||||
@@ -0,0 +1,126 @@
|
||||
#pragma once
|
||||
|
||||
#include <esp_wifi.h>
|
||||
#include <esp_event.h>
|
||||
#include <esp_netif.h>
|
||||
#include <esp_log.h>
|
||||
#include <nvs_flash.h>
|
||||
#include <freertos/FreeRTOS.h>
|
||||
#include <freertos/event_groups.h>
|
||||
#include <utils/ip_address.h>
|
||||
#include <string>
|
||||
#include <cstring>
|
||||
#include <functional>
|
||||
#include <vector>
|
||||
|
||||
typedef enum {
|
||||
WL_NO_SHIELD = 255,
|
||||
WL_IDLE_STATUS = 0,
|
||||
WL_NO_SSID_AVAIL = 1,
|
||||
WL_SCAN_COMPLETED = 2,
|
||||
WL_CONNECTED = 3,
|
||||
WL_CONNECT_FAILED = 4,
|
||||
WL_CONNECTION_LOST = 5,
|
||||
WL_DISCONNECTED = 6
|
||||
} wl_status_t;
|
||||
|
||||
typedef enum {
|
||||
WIFI_AUTH_OPEN_IDF = 0,
|
||||
WIFI_AUTH_WEP_IDF,
|
||||
WIFI_AUTH_WPA_PSK_IDF,
|
||||
WIFI_AUTH_WPA2_PSK_IDF
|
||||
} wifi_enc_type_t;
|
||||
|
||||
using WiFiEventCb = std::function<void(int32_t event, void* event_data)>;
|
||||
|
||||
struct WiFiEventHandler {
|
||||
int32_t event_id;
|
||||
WiFiEventCb callback;
|
||||
};
|
||||
|
||||
class WiFiClass {
|
||||
public:
|
||||
WiFiClass();
|
||||
~WiFiClass();
|
||||
|
||||
bool init();
|
||||
|
||||
bool mode(wifi_mode_t mode);
|
||||
wifi_mode_t getMode();
|
||||
|
||||
bool begin(const char* ssid, const char* password = nullptr, int32_t channel = 0, const uint8_t* bssid = nullptr);
|
||||
bool disconnect(bool wifiOff = false);
|
||||
bool reconnect();
|
||||
|
||||
bool config(IPAddress local_ip, IPAddress gateway, IPAddress subnet, IPAddress dns1 = IPAddress(),
|
||||
IPAddress dns2 = IPAddress());
|
||||
bool setHostname(const char* hostname);
|
||||
const char* getHostname();
|
||||
|
||||
wl_status_t status();
|
||||
bool isConnected();
|
||||
|
||||
IPAddress localIP();
|
||||
IPAddress subnetMask();
|
||||
IPAddress gatewayIP();
|
||||
IPAddress dnsIP(uint8_t dns_no = 0);
|
||||
|
||||
std::string macAddress();
|
||||
std::string SSID();
|
||||
std::string BSSIDstr();
|
||||
int32_t RSSI();
|
||||
uint8_t channel();
|
||||
|
||||
int16_t scanNetworks(bool async = false);
|
||||
int16_t scanComplete();
|
||||
void scanDelete();
|
||||
|
||||
std::string SSID(uint8_t i);
|
||||
int32_t RSSI(uint8_t i);
|
||||
wifi_enc_type_t encryptionType(uint8_t i);
|
||||
std::string BSSIDstr(uint8_t i);
|
||||
int32_t channel(uint8_t i);
|
||||
void getNetworkInfo(uint8_t i, std::string& ssid, uint8_t& encType, int32_t& rssi, uint8_t*& bssid, int32_t& ch);
|
||||
|
||||
bool softAP(const char* ssid, const char* password = nullptr, int channel = 1, bool ssid_hidden = false,
|
||||
int max_connection = 4);
|
||||
bool softAPConfig(IPAddress local_ip, IPAddress gateway, IPAddress subnet);
|
||||
bool softAPdisconnect(bool wifiOff = false);
|
||||
IPAddress softAPIP();
|
||||
std::string softAPmacAddress();
|
||||
uint8_t softAPgetStationNum();
|
||||
|
||||
bool setAutoReconnect(bool autoReconnect);
|
||||
bool persistent(bool persistent);
|
||||
bool setTxPower(int8_t power);
|
||||
|
||||
void onEvent(WiFiEventCb callback, int32_t event_id);
|
||||
|
||||
private:
|
||||
static void eventHandler(void* arg, esp_event_base_t event_base, int32_t event_id, void* event_data);
|
||||
void dispatchEvent(int32_t event_id, void* event_data);
|
||||
|
||||
esp_netif_t* _sta_netif;
|
||||
esp_netif_t* _ap_netif;
|
||||
bool _initialized;
|
||||
bool _autoReconnect;
|
||||
bool _persistent;
|
||||
wl_status_t _status;
|
||||
wifi_mode_t _mode;
|
||||
|
||||
std::string _hostname;
|
||||
wifi_ap_record_t* _scanResult;
|
||||
uint16_t _scanCount;
|
||||
int16_t _scanStatus;
|
||||
|
||||
std::vector<WiFiEventHandler> _eventHandlers;
|
||||
|
||||
IPAddress _sta_static_ip;
|
||||
IPAddress _sta_static_gw;
|
||||
IPAddress _sta_static_sn;
|
||||
IPAddress _sta_static_dns1;
|
||||
IPAddress _sta_static_dns2;
|
||||
bool _useStaticIp;
|
||||
};
|
||||
|
||||
extern WiFiClass WiFi;
|
||||
Reference in New Issue
Block a user