dunno, changes and stuff

This commit is contained in:
2022-07-17 14:47:21 +02:00
parent 331f9f3e6c
commit 07917fe5ba
19 changed files with 1449 additions and 192 deletions

View File

@@ -5,7 +5,8 @@
#define Port uint16_t
#if ESP8266
#if OLD_IS_ESP8266 // RAW
struct IP4 {
@@ -13,17 +14,17 @@
ip_addr_t addr;
/** empty ctor */
explicit IP4() {
explicit IP4() {
addr.addr = 0;
}
/** ctor with IP-string */
explicit IP4(const char* ipStr) {
explicit IP4(const char* ipStr) {
set(ipStr);
}
/** ctor with ip_addr_t */
explicit IP4(ip_addr addr) : addr(addr) {
explicit IP4(ip_addr addr) : addr(addr) {
;
}
@@ -47,7 +48,47 @@
};
#elif ESP32
#elif IS_ESP8266 // freeRTOS
#include <lwip/ip.h>
struct IP4 {
ip4_addr addr;
/** empty ctor */
explicit IP4() {
addr.addr = 0;
}
/** ctor with IP-string */
explicit IP4(const char* ipStr) {
set(ipStr);
}
/** ctor with ip4_addr */
explicit IP4(ip4_addr _addr) {
addr =_addr;
}
/** set the IP by string: x.x.x.x */
void set(const char* ipStr) {
ip4addr_aton(ipStr, &addr);
}
/** convert to ip_addr/ip_addr_t */
const ip_addr_t* getPtr() const {
return &addr;
}
/** convert to string */
const char* toString() const {
return ipaddr_ntoa(&addr);
}
};
#elif IS_ESP32
#include <lwip/ip.h>

107
net/UDP.h
View File

@@ -2,11 +2,16 @@
#define UDP_H
extern "C" {
#include "mem.h"
#include "espconn.h"
//#include "mem.h"
//#include "espconn.h"
#include "lwip/sockets.h"
#include "lwip/sys.h"
#include "lwip/err.h"
#include "lwip/udp.h"
}
typedef void (*UDPCallback)(void* arg, char* data, unsigned short len);
//typedef void (*UDPCallback)(void* arg, char* data, unsigned short len);
typedef void (*UDPCallback)(void* arg, struct udp_pcb* pcb, struct pbuf* p, const ip_addr_t* addr, u16_t port);
#include "../Debug.h"
#include "IP.h"
@@ -17,7 +22,8 @@ private:
static constexpr const char* NAME = "UDP";
espconn* con;
//espconn* con;
struct udp_pcb* udp;
public:
@@ -35,78 +41,91 @@ public:
/** bind the socket to the given local port */
void bind(const Port localPort) {
debugMod1(NAME, "binding to local port %d", localPort);
Log::addInfo(NAME, "binding to local port %d", localPort);
// set the local port to listen on
con->proto.udp->local_port = localPort;
// // set the local port to listen on
// con->proto.udp->local_port = localPort;
//
// // todo: check? 0=OK
// const int res = espconn_create(con);
// os_printf("create: %d\r\n", res);
// todo: check? 0=OK
const int res = espconn_create(con);
os_printf("create: %d\r\n", res);
ip_addr_t bindIP;
IP4_ADDR(&bindIP, 0,0,0,0);
udp_bind(udp, &bindIP, localPort);
}
bool send(const IP4 ip, const Port port, const void* data, const uint16_t dataLen) {
debugMod1(NAME, "sending packet to remote port %d", port);
Log::addInfo(NAME, "sending packet to remote port %d", port);
// set remote port and IP
con->proto.udp->remote_port = port;
os_memcpy(con->proto.udp->remote_ip, ip.getPtr(), 4);
// // set remote port and IP
// con->proto.udp->remote_port = port;
// os_memcpy(con->proto.udp->remote_ip, ip.getPtr(), 4);
//
// // send. TODO: check. 0=OK
// const int res = espconn_sent(con, (unsigned char*)data, dataLen);
// return (res == 0);
//os_printf("send %d bytes\r\n", dataLen);
//os_printf("IP: %d.%d.%d.%d\n\r", con->proto.udp->remote_ip[0], con->proto.udp->remote_ip[1], con->proto.udp->remote_ip[2], con->proto.udp->remote_ip[3]);
// allocate and fill transmit buffer
pbuf* buf = pbuf_alloc(PBUF_TRANSPORT, dataLen, PBUF_RAM);
memcpy(buf->payload, data, dataLen);
// send. TODO: check. 0=OK
const int res = espconn_sent(con, (unsigned char*)data, dataLen);
return (res == 0);
// transmit
udp_sendto(udp, buf, ip.getPtr(), port);
pbuf_free(buf);
return true;
}
/** set the callback to call whenever a packet is received */
void setRecvCallback(UDPCallback callback) {
debugMod(NAME, "setRecvCallback()");
espconn_regist_recvcb(con, callback);
Log::addInfo(NAME, "setRecvCallback()");
// espconn_regist_recvcb(con, callback);
udp_recv(udp, callback, this);
}
/** get the IP address of the most recent packet's sender */
IP4 getSenderIP() {
remot_info* rem;
espconn_get_connection_info(con, &rem, 0);
ip_addr_t* _ip = (ip_addr_t*) rem->remote_ip;
return IP4(*_ip);
//const uint16_t port = rem->remote_port;
}
// IP4 getSenderIP() {
// remot_info* rem;
// espconn_get_connection_info(con, &rem, 0);
// ip_addr_t* _ip = (ip_addr_t*) rem->remote_ip;
// return IP4(*_ip);
// }
private:
/** initialize the UDP "connection" */
void init() {
debugMod(NAME, "init()");
Log::addInfo(NAME, "init()");
// allocate connection-objects
con = (espconn*) os_zalloc(sizeof(espconn));
ets_memset( con, 0, sizeof( espconn ) );
// configure
con->type = ESPCONN_UDP;
//con->state = ESPCONN_NONE;
con->proto.udp = (esp_udp*) os_zalloc(sizeof(esp_udp));
// // allocate connection-objects
// con = (espconn*) os_zalloc(sizeof(espconn));
// ets_memset( con, 0, sizeof( espconn ) );
//
// // configure
// con->type = ESPCONN_UDP;
//
// con->proto.udp = (esp_udp*) os_zalloc(sizeof(esp_udp));
udp = udp_new();
}
/** cleanup everything */
void cleanup() {
debugMod(NAME, "cleanup()");
Log::addInfo(NAME, "cleanup()");
espconn_delete(con);
os_free(con->proto.udp);
os_free(con);
// espconn_delete(con);
// os_free(con->proto.udp);
// os_free(con);
udp_remove(udp);
}

View File

@@ -3,6 +3,12 @@
#include "MAC.h"
#if IS_ESP8266
extern "C" {
#include "esp_wifi.h"
}
#endif
// ifconfig wlp0s26u1u2u1 down && iw dev wlp0s26u1u2u1 set monitor none && ifconfig wlp0s26u1u2u1 up && iw dev wlp0s26u1u2u1 set channel 3
// ifconfig wlp0s26u1u2u1 down && iw dev wlp0s26u1u2u1 set monitor none && ifconfig wlp0s26u1u2u1 up iwconfig wlp0s26u1u2u1 channel 3
@@ -183,15 +189,15 @@ namespace WiFiRaw {
};
#ifdef ESP8266
#if IS_ESP8266
WiFiRaw::MACAddress getMyMAC() {
WiFiRaw::MACAddress mine;
wifi_get_macaddr(0x00, (uint8_t*)&mine);
esp_wifi_get_mac(ESP_IF_WIFI_STA, (uint8_t*)&mine);
return mine;
}
#endif
#ifdef ESP32
#if IS_ESP32
WiFiRaw::MACAddress getMyMAC() {
WiFiRaw::MACAddress mine;
esp_wifi_get_mac(ESP_IF_WIFI_STA, (uint8_t*)&mine);