1
0
mirror of https://github.com/esp8266/Arduino.git synced 2025-07-27 18:02:17 +03:00

Sketch emulation on host (#5342)

* WIP compile examples on host with 'make examples'

* WIP bufferize tcp input

* WIP Makefile

* WIP network to rework, tcp/udp to factorize, udp addresses broken

* minor changes to the core

* WIP basic udp working

* WIP mdns

* WIP mcast receiving, not sending

* WIP mdns OK

* beta version

* SSL + doc

* update travis host test command

* licenses

* typo

* doc: arduino builder is not around: declare functions before calling them

* fix with latest SSL PR, compile in 32 bits mode

* fix make clean

* make -m32 optional

* 32bits compiler ability tester

* WIP

* WIP (fix 1 vtable error, still another one to hunt with using spiffs)

* example astyle

* fix os_printf_plus

* load / save mock spiffs

* fix style

* fix using spiffs/mock

* don't mess ram

* update doc

* remove leftover

* optimization -Os except for CI, rename ARCH32 to FORCE32

* revert useless cast (not even compiled)

* remove unused function

* use proper type for pointer arithmetics

* makefile: sketch object and cpp file moved to bin/ directories
easier to clean, and IDE don't like them

* changes for review

* make use of %zd

* less verbose makefile by default (option)

* update readme
This commit is contained in:
david gauchard
2018-11-20 21:51:45 +01:00
committed by Develo
parent b504881be4
commit 74ca42f829
51 changed files with 3760 additions and 96 deletions

View File

@ -0,0 +1,286 @@
/*
ClientContext.h - emulation of TCP connection handling on top of lwIP
Copyright (c) 2014 Ivan Grokhotkov. All rights reserved.
This file is part of the esp8266 core for Arduino environment.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#ifndef CLIENTCONTEXT_H
#define CLIENTCONTEXT_H
class ClientContext;
class WiFiClient;
extern "C" void esp_yield();
extern "C" void esp_schedule();
#include <include/DataSource.h>
bool getDefaultPrivateGlobalSyncValue ();
typedef void (*discard_cb_t)(void*, ClientContext*);
class ClientContext
{
public:
ClientContext(tcp_pcb* pcb, discard_cb_t discard_cb, void* discard_cb_arg) :
_discard_cb(discard_cb), _discard_cb_arg(discard_cb_arg), _refcnt(0), _next(0),
_sync(::getDefaultPrivateGlobalSyncValue()), _sock(-1)
{
(void)pcb;
}
ClientContext (int sock) :
_discard_cb(nullptr), _discard_cb_arg(nullptr), _refcnt(0), _next(nullptr),
_sync(::getDefaultPrivateGlobalSyncValue()), _sock(sock)
{
}
err_t abort()
{
if (_sock >= 0)
::close(_sock);
_sock = -1;
return ERR_ABRT;
}
err_t close()
{
abort();
return ERR_OK;
}
~ClientContext()
{
abort();
}
ClientContext* next() const
{
return _next;
}
ClientContext* next(ClientContext* new_next)
{
_next = new_next;
return _next;
}
void ref()
{
++_refcnt;
DEBUGV(":ref %d\r\n", _refcnt);
}
void unref()
{
DEBUGV(":ur %d\r\n", _refcnt);
if(--_refcnt == 0) {
discard_received();
close();
if (_discard_cb)
_discard_cb(_discard_cb_arg, this);
DEBUGV(":del\r\n");
delete this;
}
}
int connect(ip_addr_t* addr, uint16_t port)
{
return mockConnect(addr->addr, _sock, port);
}
size_t availableForWrite()
{
// XXXFIXME be smarter
return 512;
}
void setNoDelay(bool nodelay)
{
fprintf(stderr, MOCK "TODO setNoDelay(%d)\n", (int)nodelay);
}
bool getNoDelay() const
{
fprintf(stderr, MOCK "TODO getNoDelay()\n");
return false;
}
void setTimeout(int timeout_ms)
{
_timeout_ms = timeout_ms;
}
int getTimeout() const
{
return _timeout_ms;
}
uint32_t getRemoteAddress() const
{
fprintf(stderr, MOCK "TODO getRemoteAddress()\n");
return 0;
}
uint16_t getRemotePort() const
{
fprintf(stderr, MOCK "TODO getRemotePort()\n");
return 0;
}
uint32_t getLocalAddress() const
{
fprintf(stderr, MOCK "TODO getLocalAddress()\n");
return 0;
}
uint16_t getLocalPort() const
{
fprintf(stderr, MOCK "TODO getLocalPort()\n");
return 0;
}
size_t getSize()
{
return _inbufsize?: mockFillInBuf(_sock, _inbuf, _inbufsize);
}
int read()
{
char c;
return read(&c, 1)? c: -1;
}
size_t read (char* dst, size_t size)
{
return mockRead(_sock, dst, size, 0, _inbuf, _inbufsize);
}
int peek()
{
char c;
return peekBytes(&c, 1)? c: -1;
}
size_t peekBytes(char *dst, size_t size)
{
return mockPeekBytes(_sock, dst, size, _timeout_ms, _inbuf, _inbufsize);
}
void discard_received()
{
fprintf(stderr, MOCK "TODO: ClientContext::discard_received()\n");
}
bool wait_until_sent(int max_wait_ms = WIFICLIENT_MAX_FLUSH_WAIT_MS)
{
return true;
}
uint8_t state() const
{
return _sock >= 0? ESTABLISHED: CLOSED;
}
size_t write(const uint8_t* data, size_t size)
{
return mockWrite(_sock, data, size, _timeout_ms);
}
size_t write(Stream& stream)
{
size_t avail = stream.available();
uint8_t buf [avail];
avail = stream.readBytes(buf, avail);
size_t totwrote = 0;
uint8_t* w = buf;
while (avail)
{
size_t wrote = write(w, avail);
w += wrote;
avail -= wrote;
totwrote += wrote;
}
return totwrote;
}
size_t write_P(PGM_P buf, size_t size)
{
return write((const uint8_t*)buf, size);
}
void keepAlive (uint16_t idle_sec = TCP_DEFAULT_KEEPALIVE_IDLE_SEC, uint16_t intv_sec = TCP_DEFAULT_KEEPALIVE_INTERVAL_SEC, uint8_t count = TCP_DEFAULT_KEEPALIVE_COUNT)
{
fprintf(stderr, MOCK "TODO ClientContext::keepAlive()\n");
}
bool isKeepAliveEnabled () const
{
fprintf(stderr, MOCK "TODO ClientContext::isKeepAliveEnabled()\n");
return false;
}
uint16_t getKeepAliveIdle () const
{
fprintf(stderr, MOCK "TODO ClientContext::getKeepAliveIdle()\n");
return 0;
}
uint16_t getKeepAliveInterval () const
{
fprintf(stderr, MOCK "TODO ClientContext::getKeepAliveInternal()\n");
return 0;
}
uint8_t getKeepAliveCount () const
{
fprintf(stderr, MOCK "TODO ClientContext::getKeepAliveCount()\n");
return 0;
}
bool getSync () const
{
fprintf(stderr, MOCK "TODO ClientContext::getSync()\n");
return _sync;
}
void setSync (bool sync)
{
fprintf(stderr, MOCK "TODO ClientContext::setSync()\n");
_sync = sync;
}
private:
discard_cb_t _discard_cb = nullptr;
void* _discard_cb_arg = nullptr;
int8_t _refcnt;
ClientContext* _next;
bool _sync;
// MOCK
int _sock = -1;
int _timeout_ms = 5000;
char _inbuf [CCBUFSIZE];
size_t _inbufsize = 0;
};
#endif //CLIENTCONTEXT_H

View File

@ -0,0 +1,254 @@
/*
UdpContext.h - emulation of UDP connection handling on top of lwIP
Copyright (c) 2014 Ivan Grokhotkov. All rights reserved.
This file is part of the esp8266 core for Arduino environment.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#ifndef UDPCONTEXT_H
#define UDPCONTEXT_H
#include <functional>
class UdpContext;
#define GET_IP_HDR(pb) reinterpret_cast<ip_hdr*>(((uint8_t*)((pb)->payload)) - UDP_HLEN - IP_HLEN);
#define GET_UDP_HDR(pb) reinterpret_cast<udp_hdr*>(((uint8_t*)((pb)->payload)) - UDP_HLEN);
class UdpContext
{
public:
typedef std::function<void(void)> rxhandler_t;
UdpContext(): _on_rx(nullptr), _refcnt(0)
{
_sock = mockUDPSocket();
}
~UdpContext()
{
}
void ref()
{
++_refcnt;
}
void unref()
{
if(--_refcnt == 0) {
delete this;
}
}
bool connect (ip_addr_t addr, uint16_t port)
{
_dst = addr;
_dstport = port;
return true;
}
bool listen(ip_addr_t addr, uint16_t port)
{
bool ret = mockUDPListen(_sock, addr.addr, port, staticMCastAddr);
register_udp(_sock, this);
return ret;
}
void disconnect()
{
if (_sock >= 0)
{
close(_sock);
register_udp(_sock, nullptr);
}
_sock = -1;
}
void setMulticastInterface(const ip_addr_t& addr)
{
// user multicast, and this is how it works with posix: send to multicast address:
_dst.addr = staticMCastAddr;
}
void setMulticastTTL(int ttl)
{
//fprintf(stderr, MOCK "TODO: UdpContext::setMulticastTTL\n");
}
// warning: handler is called from tcp stack context
// esp_yield and non-reentrant functions which depend on it will fail
void onRx(rxhandler_t handler) {
_on_rx = handler;
}
size_t getSize()
{
return _inbufsize;
}
size_t tell() const
{
return 0;
}
void seek(const size_t pos)
{
fprintf(stderr, MOCK "TODO: implement UDP offset\n");
if (!isValidOffset(pos))
{
fprintf(stderr, MOCK "UDPContext::seek too far (%zd >= %zd)\n", pos, _inbufsize);
exit(EXIT_FAILURE);
}
}
bool isValidOffset(const size_t pos) const {
return pos <= _inbufsize;
}
uint32_t getRemoteAddress()
{
return _dst.addr;
}
uint16_t getRemotePort()
{
return _dstport;
}
uint32_t getDestAddress()
{
fprintf(stderr, MOCK "TODO: implement UDP getDestAddress\n");
return 0; //ip_hdr* iphdr = GET_IP_HDR(_rx_buf);
}
uint16_t getLocalPort()
{
fprintf(stderr, MOCK "TODO: implement UDP getLocalPort\n");
return 0; //
}
bool next()
{
_inbufsize = 0;
mockUDPFillInBuf(_sock, _inbuf, _inbufsize, addrsize, addr, _dstport);
if (_inbufsize > 0)
{
translate_addr();
return true;
}
return false;
}
int read()
{
char c;
return read(&c, 1)? c: -1;
}
size_t read(char* dst, size_t size)
{
return mockUDPRead(_sock, dst, size, _timeout_ms, _inbuf, _inbufsize);
}
int peek()
{
char c;
return mockUDPPeekBytes(_sock, &c, 1, _timeout_ms, _inbuf, _inbufsize)?: -1;
}
void flush()
{
//fprintf(stderr, MOCK "UdpContext::flush() does not follow arduino's flush concept\n");
//exit(EXIT_FAILURE);
// would be:
_inbufsize = 0;
}
size_t append (const char* data, size_t size)
{
if (size + _outbufsize > sizeof _outbuf)
{
fprintf(stderr, MOCK "UdpContext::append: increase CCBUFSIZE (%d -> %zd)\n", CCBUFSIZE, (size + _outbufsize));
exit(EXIT_FAILURE);
}
memcpy(_outbuf + _outbufsize, data, size);
_outbufsize += size;
return size;
}
bool send (ip_addr_t* addr = 0, uint16_t port = 0)
{
uint32_t dst = addr? addr->addr: _dst.addr;
uint16_t dstport = port?: _dstport;
size_t ret = mockUDPWrite(_sock, (const uint8_t*)_outbuf, _outbufsize, _timeout_ms, dst, dstport);
_outbufsize = 0;
return ret > 0;
}
void mock_cb (void)
{
if (_on_rx) _on_rx();
}
public:
static uint32_t staticMCastAddr;
private:
void translate_addr ()
{
if (addrsize == 4)
{
uint32_t ipv4;
memcpy(&ipv4, addr, 4);
ip4_addr_set_u32(&ip_2_ip4(_dst), ipv4);
// ^ this is a workaround for "type-punned pointer" with "*(uint32*)addr"
//ip4_addr_set_u32(&ip_2_ip4(_dst), *(uint32_t*)addr);
}
else
fprintf(stderr, MOCK "TODO unhandled udp address of size %d\n", (int)addrsize);
}
int _sock = -1;
rxhandler_t _on_rx;
int _refcnt = 0;
ip_addr_t _dst;
uint16_t _dstport;
char _inbuf [CCBUFSIZE];
size_t _inbufsize = 0;
char _outbuf [CCBUFSIZE];
size_t _outbufsize = 0;
int _timeout_ms = 0;
uint8_t addrsize;
uint8_t addr[16];
};
inline err_t igmp_joingroup (const ip4_addr_t *ifaddr, const ip4_addr_t *groupaddr)
{
(void)ifaddr;
UdpContext::staticMCastAddr = groupaddr->addr;
return ERR_OK;
}
#endif//UDPCONTEXT_H