many changes

This commit is contained in:
kazu
2018-09-22 15:44:26 +02:00
parent 528a00b0e9
commit 90e9fee101
11 changed files with 1581 additions and 476 deletions

69
net/DNS.h Normal file
View File

@@ -0,0 +1,69 @@
#ifndef DNS_H
#define DNS_H
#include "../Platforms.h"
#include "../Debug.h"
typedef void (*DNSResolveCallback)(const char* name, const ip_addr_t* ip, void* arg);
#if ESP8266
class DNS {
static constexpr const char* NAME = "DNS";
espconn dns;
ip_addr_t ip;
DNSResolveCallback callback;
void* arg;
public:
/** resolve hostname. creates callback */
void resolveHost(const char* c, DNSResolveCallback callback, void* arg) {
debugMod1(NAME, "dns lookup: %s", c);
this->callback = callback;
this->arg = arg;
dns.reverse = (void*) this;
espconn_gethostbyname(&dns, c, &ip, _onHostResolved);
}
static void _onHostResolved(const char* name, const ip_addr_t* ipaddr, void* arg) {
debugMod1(NAME, "dns resolved: %s", c);
DNS* dns = (DNS*) arg;
dns->callback(name, ipaddr, dns->arg);
}
};
#elif ESP32
#include <lwip/dns.h>
class DNS {
static constexpr const char* NAME = "DNS";
ip_addr_t ip;
DNSResolveCallback callback;
void* arg;
public:
/** resolve hostname. creates callback */
void resolveHost(const char* host, DNSResolveCallback callback, void* arg) {
debugMod1(NAME, "dns lookup: %s", host);
this->callback = callback;
this->arg = arg;
dns_gethostbyname(host, &ip, _onHostResolved, this);
}
static void _onHostResolved(const char* name, const ip_addr_t* ipaddr, void* arg) {
debugMod1(NAME, "dns resolved: %s", name);
DNS* dns = (DNS*) arg;
dns->callback(name, ipaddr, dns->arg);
}
};
#endif
#endif // DNS_H

View File

@@ -1,7 +1,11 @@
#ifndef HTTP_H
#define HTTP_H
#include "../Platforms.h"
#include "TCP.h"
#include "DNS.h"
#include "../Debug.h"
class HTTPClient;
@@ -10,13 +14,13 @@ typedef void (*HTTPCBodyDataCallback)(HTTPClient* httpc, const uint8_t* data, ui
typedef void (*HTTPCConnectCallback)(HTTPClient* httpc);
typedef void (*HTTPCDisconnectCallback)(HTTPClient* httpc);
class HTTPClient {
static constexpr const char* NAME = "HTTPC";
TCP tcp;
espconn dns;
DNS dns;
ip_addr_t ip;
char host[64];
char query[128];
@@ -70,7 +74,7 @@ public:
debugMod3(NAME, "connect(%s) -> host: '%s' query: '%s'", url, this->host, this->query);
resolveHost(this->host);
dns.resolveHost(this->host, _onHostResolved, (void*)this);
}
@@ -80,6 +84,10 @@ public:
numBreaks = 0;
}
void consumed(uint16_t len) {
tcp.consumed(len);
}
private:
/** from TCP: connected */
@@ -130,26 +138,23 @@ private:
private:
/** resolve hostname. creates callback */
void resolveHost(const char* c) {
debugMod1(NAME, "dns lookup: %s", c);
dns.reverse = (void*) this;
espconn_gethostbyname(&dns, c, &ip, _onHostResolved);
}
/** send the request header */
void sendRequest() {
char buf[128];
const int len = os_sprintf(buf, "GET %s HTTP/1.1\r\nicy-metadata:1\r\n\r\n", this->query);
debugMod(NAME, buf);
const int len = sprintf(buf, "GET %s HTTP/1.1\r\n\r\n", this->query);
//const int len = sprintf(buf, "GET %s HTTP/1.1\r\nicy-metadata:1\r\n\r\n", this->query);
printf(buf);
//debugMod(NAME, (const char*) buf);
tcp.send((const uint8_t*)buf, len);
}
static void _onHostResolved(const char* name, ip_addr_t* ipaddr, void* arg ) {
HTTPClient* http = (HTTPClient*) ((espconn*)arg)->reverse;
static void _onHostResolved(const char* name, const ip_addr_t* ipaddr, void* arg ) {
HTTPClient* http = (HTTPClient*) arg;
if (ipaddr) {
//debugMod1(NAME, "dns lookup returned: %s", tmp.toString());
http->tcp.connect(IP(*ipaddr), 80);
http->tcp.connect(IP4(*ipaddr), 80);
} else {
debugMod1(NAME, "dns lookup failed: %s", name);
}

121
net/IP.h
View File

@@ -1,46 +1,105 @@
#ifndef IP_H
#define IP_H
#include "../Platforms.h"
#define Port uint16_t
struct IP {
#if ESP8266
//uint32_t val;
ip_addr addr;
struct IP4 {
/** empty ctor */
explicit IP() {
addr.addr = 0;
}
//uint32_t val;
ip_addr_t addr;
/** ctor with IP-string */
explicit IP(const char* ipStr) {
set(ipStr);
}
/** empty ctor */
explicit IP() {
addr.addr = 0;
}
/** ctor with ip_addr_t */
explicit IP(ip_addr addr) : addr(addr) {
;
}
/** ctor with IP-string */
explicit IP(const char* ipStr) {
set(ipStr);
}
/** set the IP by string: x.x.x.x */
void set(const char* ipStr) {
addr.addr = ipaddr_addr(ipStr);
}
/** ctor with ip_addr_t */
explicit IP(ip_addr addr) : addr(addr) {
;
}
/** convert to ip_addr/ip_addr_t */
const ip_addr* getPtr() const {
return &addr;
}
/** set the IP by string: x.x.x.x */
void set(const char* ipStr) {
addr.addr = ipaddr_addr(ipStr);
}
/** convert to string */
const char* toString() const {
//static char str[16];
//ipaddr_aton(str, (ip_addr*)&addr);
//return str;
return ipaddr_ntoa(&addr);
}
/** convert to ip_addr/ip_addr_t */
const ip_addr* getPtr() const {
return &addr;
}
};
/** convert to string */
const char* toString() const {
//static char str[16];
//ipaddr_aton(str, (ip_addr*)&addr);
//return str;
return ipaddr_ntoa(&addr);
}
};
#elif ESP32
#include <lwip/ip.h>
struct IP4 {
ip_addr_t addr;
/** empty ctor */
explicit IP4() {
addr.type = IPADDR_TYPE_V4;
addr.u_addr.ip4.addr = 0;
}
/** ctor with IP-string */
explicit IP4(const char* ipStr) {
addr.type = IPADDR_TYPE_V4;
set(ipStr);
}
/** ctor with ip4_addr */
explicit IP4(ip4_addr _addr) {
addr.type = IPADDR_TYPE_V4;
addr.u_addr.ip4 = _addr;
}
/** ctor with ip_addr_t */
explicit IP4(ip_addr_t _addr) {
addr = _addr;
}
/** set the IP by string: x.x.x.x */
void set(const char* ipStr) {
//addr.u_addr.ip4 = ip4addr_aton(ipStr);
ip4addr_aton(ipStr, &addr.u_addr.ip4);
}
/** convert to ip_addr/ip_addr_t */
const ip_addr_t* getPtr() const {
return &addr;
}
/** convert to string */
const char* toString() const {
//static char str[16];
//ipaddr_aton(str, (ip_addr*)&addr);
//return str;
return ipaddr_ntoa(&addr);
}
};
#endif
#endif // IP_H

419
net/TCP.h
View File

@@ -1,10 +1,8 @@
#ifndef TCP_H
#define TCP_H
extern "C" {
#include "mem.h"
#include "espconn.h"
}
#include "../Platforms.h"
class TCP;
@@ -15,156 +13,337 @@ typedef void (*TCPDisconnectCallback)(TCP* tcp);
#include "../Debug.h"
#include "IP.h"
class TCP {
#if ESP8266
private:
static constexpr const char* NAME = "TCP";
espconn* con;
esp_tcp tcp;
bool connected = false;
TCPDataCallback onData = nullptr;
TCPConnectCallback onConnect = nullptr;
TCPDisconnectCallback onDisconnect = nullptr;
void* userData = nullptr;
public:
TCP() {
init();
extern "C" {
#include "mem.h"
#include "espconn.h"
}
/** dtor */
~TCP() {
cleanup();
}
class TCP {
private:
/** send data to the other side */
bool send(const uint8_t* data, const size_t dataLen) {
const int res = espconn_sent(con, (unsigned char*)data, dataLen);
return (res == 0);
}
static constexpr const char* NAME = "TCP";
/** connect to the given IP and port */
void connect(const IP ip, const Port port) {
disconnect();
debugMod2(NAME, "connect(%s, %d)", ip.toString(), port);
con->proto.tcp->remote_port = port;
con->proto.tcp->local_port = espconn_port();
os_memcpy(con->proto.tcp->remote_ip, ip.getPtr(), 4);
espconn_connect(con);
connected = true;
}
espconn* con;
esp_tcp tcp;
bool connected = false;
TCPDataCallback onData = nullptr;
TCPConnectCallback onConnect = nullptr;
TCPDisconnectCallback onDisconnect = nullptr;
void* userData = nullptr;
/** terminate connection */
void disconnect() {
if (!connected) {return;}
espconn_disconnect(con);
connected = false;
}
public:
void hold() {
if (connected) {
espconn_recv_hold(con);
TCP() {
init();
}
}
void unhold() {
if (connected) {
espconn_recv_unhold(con);
/** dtor */
~TCP() {
cleanup();
}
}
/** set the callback to call whenever data is received */
void setDataCallback(TCPDataCallback callback) {
this->onData = callback;
}
/** set the callback to call when connection is established */
void setConnectCallback(TCPConnectCallback callback) {
this->onConnect = callback;
}
/** send data to the other side */
bool send(const uint8_t* data, const size_t dataLen) {
const int res = espconn_sent(con, (unsigned char*)data, dataLen);
return (res == 0);
}
/** set the callback to call when connection is lost */
void setDisconnectCallback(TCPDisconnectCallback callback) {
this->onDisconnect = callback;
}
/** connect to the given IP and port */
void connect(const IP ip, const Port port) {
disconnect();
debugMod2(NAME, "connect(%s, %d)", ip.toString(), port);
con->proto.tcp->remote_port = port;
con->proto.tcp->local_port = espconn_port();
os_memcpy(con->proto.tcp->remote_ip, ip.getPtr(), 4);
espconn_connect(con);
connected = true;
}
/** attach user-data to this object. can bed used within callbacks */
void setUserData(void* ptr) {
this->userData = ptr;
}
/** get previously attached user data */
void* getUserData() const {
return this->userData;
}
/** terminate connection */
void disconnect() {
if (!connected) {return;}
espconn_disconnect(con);
connected = false;
}
private:
void hold() {
if (connected) {
espconn_recv_hold(con);
}
}
/** called for incoming data */
static void _onData(void* ptr, char* data, unsigned short len) {
TCP* tcp = (TCP*) ((espconn*)ptr)->reverse;
if (tcp->onData) {tcp->onData(tcp, (const uint8_t*)data, len);}
}
void unhold() {
if (connected) {
espconn_recv_unhold(con);
}
}
/** called when connection is established */
static void _onConnect(void* ptr) {
TCP* tcp = (TCP*) ((espconn*)ptr)->reverse;
if (tcp->onConnect) {tcp->onConnect(tcp);}
tcp->connected = true;
}
/** set the callback to call whenever data is received */
void setDataCallback(TCPDataCallback callback) {
this->onData = callback;
}
/** called when connection is lost */
static void _onDisconnect(void* ptr) {
TCP* tcp = (TCP*) ((espconn*)ptr)->reverse;
if (tcp->onDisconnect) {tcp->onDisconnect(tcp);}
tcp->connected = false;
}
/** set the callback to call when connection is established */
void setConnectCallback(TCPConnectCallback callback) {
this->onConnect = callback;
}
/** set the callback to call when connection is lost */
void setDisconnectCallback(TCPDisconnectCallback callback) {
this->onDisconnect = callback;
}
/** attach user-data to this object. can bed used within callbacks */
void setUserData(void* ptr) {
this->userData = ptr;
}
/** get previously attached user data */
void* getUserData() const {
return this->userData;
}
private:
/** called for incoming data */
static void _onData(void* ptr, char* data, unsigned short len) {
TCP* tcp = (TCP*) ((espconn*)ptr)->reverse;
if (tcp->onData) {tcp->onData(tcp, (const uint8_t*)data, len);}
}
/** called when connection is established */
static void _onConnect(void* ptr) {
TCP* tcp = (TCP*) ((espconn*)ptr)->reverse;
if (tcp->onConnect) {tcp->onConnect(tcp);}
tcp->connected = true;
}
/** called when connection is lost */
static void _onDisconnect(void* ptr) {
TCP* tcp = (TCP*) ((espconn*)ptr)->reverse;
if (tcp->onDisconnect) {tcp->onDisconnect(tcp);}
tcp->connected = false;
}
/** initialize the UDP "connection" */
void init() {
/** initialize the UDP "connection" */
void init() {
debugMod(NAME, "init()");
debugMod(NAME, "init()");
// allocate connection-objects
con = (espconn*) os_zalloc(sizeof(espconn));
ets_memset( con, 0, sizeof( espconn ) );
// allocate connection-objects
con = (espconn*) os_zalloc(sizeof(espconn));
ets_memset( con, 0, sizeof( espconn ) );
// configure
con->type = ESPCONN_TCP;
con->state = ESPCONN_NONE;
// configure
con->type = ESPCONN_TCP;
con->state = ESPCONN_NONE;
//con->proto.tcp = (esp_tcp*) os_zalloc(sizeof(esp_tcp));
con->proto.tcp = &tcp;
//con->proto.tcp = (esp_tcp*) os_zalloc(sizeof(esp_tcp));
con->proto.tcp = &tcp;
// attach ourselves for the callback
con->reverse = (void*) this; // user-data to refer to this class
espconn_regist_recvcb(con, _onData);
espconn_regist_connectcb(con, _onConnect);
espconn_regist_disconcb(con, _onDisconnect);
// espconn_regist_reconcb(con, _onDisconnect);
// attach ourselves for the callback
con->reverse = (void*) this; // user-data to refer to this class
espconn_regist_recvcb(con, _onData);
espconn_regist_connectcb(con, _onConnect);
espconn_regist_disconcb(con, _onDisconnect);
// espconn_regist_reconcb(con, _onDisconnect);
}
}
/** cleanup everything */
void cleanup() {
/** cleanup everything */
void cleanup() {
debugMod(NAME, "cleanup()");
debugMod(NAME, "cleanup()");
espconn_delete(con);
//os_free(con->proto.tcp);
os_free(con);
con = nullptr;
espconn_delete(con);
//os_free(con->proto.tcp);
os_free(con);
con = nullptr;
}
}
};
};
#elif ESP32
#include "lwip/err.h"
#include "lwip/tcp.h"
#include "lwip/ip_addr.h"
class TCP {
private:
static constexpr const char* NAME = "TCP";
struct tcp_pcb* pcb;
bool connected = false;
TCPDataCallback onData = nullptr;
TCPConnectCallback onConnect = nullptr;
TCPDisconnectCallback onDisconnect = nullptr;
void* userData = nullptr;
public:
TCP() {
init();
}
/** dtor */
~TCP() {
cleanup();
}
/** send data to the other side */
bool send(const uint8_t* data, const size_t dataLen) {
err_t res = tcp_write(pcb, data, dataLen, TCP_WRITE_FLAG_COPY);
return (res == ERR_OK);
}
/** connect to the given IP and port */
void connect(const IP4 ip, const Port port) {
debugMod2(NAME, "connect(%s, %d)", ip.toString(), port);
disconnect();
tcp_connect(pcb, ip.getPtr(), port, _onConnectCallback);
}
/** terminate connection */
void disconnect() {
if (!connected) {return;}
tcp_close(pcb);
connected = false;
}
void hold() {
// if (connected) {
// espconn_recv_hold(con);
// }
}
void unhold() {
// if (connected) {
// espconn_recv_unhold(con);
// }
}
/** set the callback to call whenever data is received */
void setDataCallback(TCPDataCallback callback) {
this->onData = callback;
}
/** set the callback to call when connection is established */
void setConnectCallback(TCPConnectCallback callback) {
this->onConnect = callback;
}
/** set the callback to call when connection is lost */
void setDisconnectCallback(TCPDisconnectCallback callback) {
this->onDisconnect = callback;
}
/** attach user-data to this object. can bed used within callbacks */
void setUserData(void* ptr) {
this->userData = ptr;
}
/** get previously attached user data */
void* getUserData() const {
return this->userData;
}
/** inform backend we consumed some data */
void consumed(uint16_t len) {
if (len) {
tcp_recved(pcb, len);
}
}
private:
/** called for incoming data */
static err_t _onData(void* arg, struct tcp_pcb* tpcb, struct pbuf* p, err_t err) {
TCP* tcp = (TCP*) arg;
// if p == nullptr -> disconnect -> inform listener
if (!p) {
if (tcp->onDisconnect) {tcp->onDisconnect(tcp);}
return ERR_OK;
}
// process pBuf (might be a chain of buffers)
do {
// pass data to listener
if (tcp->onData) {tcp->onData(tcp, (const uint8_t*)p->payload, p->len);}
// next pBuf in chain (if any)
pbuf* next = p->next;
// free current buffer (we consumed it completely)
pbuf_free(p);
// proceed?
p = next;
} while (p);
// everything fine
return ERR_OK;
}
/** called when connection failed */
static err_t _onConnectCallback(void* arg, struct tcp_pcb* tpcb, err_t err) {
TCP* tcp = (TCP*) arg;
if (err == ERR_OK) {
if (tcp->onConnect) {tcp->onConnect(tcp);}
tcp->connected = true;
} else {
if (tcp->onDisconnect) {tcp->onDisconnect(tcp);}
tcp->connected = false;
}
return ERR_OK;
}
/** initialize the UDP "connection" */
void init() {
debugMod(NAME, "init()");
pcb = tcp_new();
if (!pcb) {debugMod(NAME, "error creating TCP socket");}
// user-pointer
pcb->callback_arg = (void*) this;
tcp_recv(pcb, _onData);
}
/** cleanup everything */
void cleanup() {
debugMod(NAME, "cleanup()");
// TODO
}
};
#endif
#endif // TCP_H