Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

host emulation: improve udp, persistent spiffs #5605

Merged
merged 15 commits into from Jan 15, 2019
Merged
2 changes: 2 additions & 0 deletions tests/host/Makefile
Expand Up @@ -82,6 +82,8 @@ MOCK_CPP_FILES := $(MOCK_CPP_FILES_COMMON) $(addprefix common/,\

MOCK_CPP_FILES_EMU := $(MOCK_CPP_FILES_COMMON) $(addprefix common/,\
ArduinoMain.cpp \
ArduinoMainUdp.cpp \
ArduinoMainSpiffs.cpp \
user_interface.cpp \
)

Expand Down
83 changes: 40 additions & 43 deletions tests/host/common/ArduinoMain.cpp
Expand Up @@ -32,37 +32,13 @@
#include <Arduino.h>
#include <user_interface.h> // wifi_get_ip_info()

#include <functional>
#include "lwip/opt.h"
#include "lwip/udp.h"
#include "lwip/inet.h"
#include "lwip/igmp.h"
#include "lwip/mem.h"
#include <include/UdpContext.h>
#include <poll.h>

#include <signal.h>
#include <unistd.h> // usleep
#include <getopt.h>

#include <map>

#if 0
#include "../common/spiffs_mock.h"
#include <spiffs/spiffs.h>
SPIFFS_MOCK_DECLARE(/*size_kb*/1024, /(blovk_kb*/8, /*page_b*/512);
#endif

std::map<int,UdpContext*> udps;

void register_udp (int sock, UdpContext* udp)
{
if (udp)
udps[sock] = udp;
else
udps.erase(sock);
}

bool user_exit = false;
const char* host_interface = nullptr;
size_t spiffs_kb = 1024;

void help (const char* argv0, int exitcode)
{
Expand All @@ -73,7 +49,9 @@ void help (const char* argv0, int exitcode)
" -i <interface> - use this interface for IP address\n"
" -l - bind tcp/udp servers to interface only (not 0.0.0.0)\n"
" -f - no throttle (possibly 100%%CPU)\n"
, argv0);
" -S - spiffs size in KBytes (default: %zd)\n"
" (negative value will force mismatched size)\n"
, argv0, spiffs_kb);
exit(exitcode);
}

Expand All @@ -83,15 +61,38 @@ static struct option options[] =
{ "fast", no_argument, NULL, 'f' },
{ "local", no_argument, NULL, 'l' },
{ "interface", required_argument, NULL, 'i' },
{ "spiffskb", required_argument, NULL, 'S' },
};

void save ()
{
mock_stop_spiffs();
}


void control_c (int sig)
{
(void)sig;

if (user_exit)
{
fprintf(stderr, MOCK "stuck, killing\n");
save();
abort();
}
user_exit = true;
}

int main (int argc, char* const argv [])
{
signal(SIGINT, control_c);

bool fast = false;


for (;;)
{
int n = getopt_long(argc, argv, "hlfi:", options, NULL);
int n = getopt_long(argc, argv, "hlfi:S:", options, NULL);
if (n < 0)
break;
switch (n)
Expand All @@ -108,36 +109,32 @@ int main (int argc, char* const argv [])
case 'f':
fast = true;
break;
case 'S':
spiffs_kb = atoi(optarg);
break;
default:
fprintf(stderr, MOCK "bad option '%c'\n", n);
exit(EXIT_FAILURE);
}
}

mock_start_spiffs(spiffs_kb);

// setup global global_ipv4_netfmt
wifi_get_ip_info(0, nullptr);

setup();
while (true)
while (!user_exit)
{
if (!fast)
usleep(10000); // not 100% cpu

loop();

// check incoming udp
for (auto& udp: udps)
{
pollfd p;
p.fd = udp.first;
p.events = POLLIN;
if (poll(&p, 1, 0) && p.revents == POLLIN)
{
fprintf(stderr, MOCK "UDP poll(%d) -> cb\r", p.fd);
udp.second->mock_cb();
}
}
check_incoming_udp();
}

save();

return 0;
}

22 changes: 22 additions & 0 deletions tests/host/common/ArduinoMainSpiffs.cpp
@@ -0,0 +1,22 @@

#include "spiffs_mock.h"

#if 0
#include "../common/spiffs_mock.h"
#include <spiffs/spiffs.h>
SPIFFS_MOCK_DECLARE(/*size_kb*/64, /*block_kb*/8, /*page_b*/512, true);
#endif

SpiffsMock* spiffs_mock = nullptr;

void mock_start_spiffs (size_t size_kb, size_t block_kb, size_t page_b)
{
spiffs_mock = new SpiffsMock(size_kb * 1024, block_kb * 1024, page_b, true);
}

void mock_stop_spiffs ()
{
delete spiffs_mock;
spiffs_mock = nullptr;
}

65 changes: 65 additions & 0 deletions tests/host/common/ArduinoMainUdp.cpp
@@ -0,0 +1,65 @@
/*
Arduino emulator main loop
Copyright (c) 2018 david gauchard. All rights reserved.

Permission is hereby granted, free of charge, to any person obtaining a
copy of this software and associated documentation files (the "Software"),
to deal with the Software without restriction, including without limitation
the rights to use, copy, modify, merge, publish, distribute, sublicense,
and/or sell copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following conditions:

- Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimers.

- Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimers in the
documentation and/or other materials provided with the distribution.

- The names of its contributors may not be used to endorse or promote
products derived from this Software without specific prior written
permission.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
THE CONTRIBUTORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS WITH THE SOFTWARE.
*/

#include "lwip/opt.h"
#include "lwip/udp.h"
#include "lwip/inet.h"
#include "lwip/igmp.h"
#include "lwip/mem.h"
#include <include/UdpContext.h>
#include <poll.h>
#include <map>

std::map<int,UdpContext*> udps;

void register_udp (int sock, UdpContext* udp)
{
if (udp)
udps[sock] = udp;
else
udps.erase(sock);
}

void check_incoming_udp ()
{
// check incoming udp
for (auto& udp: udps)
{
pollfd p;
p.fd = udp.first;
p.events = POLLIN;
if (poll(&p, 1, 0) && p.revents == POLLIN)
{
fprintf(stderr, MOCK "UDP poll(%d) -> cb\r", p.fd);
udp.second->mock_cb();
}
}
}
1 change: 1 addition & 0 deletions tests/host/common/MockSerial.cpp
Expand Up @@ -40,6 +40,7 @@ HardwareSerial::HardwareSerial (int uart_nr)
{
if (uart_nr != 0)
fprintf(stderr, MOCK "FIXME HardwareSerial::HardwareSerial(%d)\n", uart_nr);
_uart = (decltype(_uart))1; // not used, for 'while (!Serial);' to pass
}

void HardwareSerial::begin (unsigned long baud, SerialConfig config, SerialMode mode, uint8_t tx_pin)
Expand Down
11 changes: 8 additions & 3 deletions tests/host/common/UdpContextSocket.cpp
Expand Up @@ -150,12 +150,17 @@ size_t mockUDPPeekBytes (int sock, char* dst, size_t usersize, int timeout_ms, c
return retsize;
}

size_t mockUDPRead (int sock, char* dst, size_t size, int timeout_ms, char* ccinbuf, size_t& ccinbufsize)
void mockUDPSwallow (size_t copied, char* ccinbuf, size_t& ccinbufsize)
{
size_t copied = mockUDPPeekBytes(sock, dst, size, timeout_ms, ccinbuf, ccinbufsize);
// swallow (XXX use a circular buffer?)
// poor man buffer
memmove(ccinbuf, ccinbuf + copied, ccinbufsize - copied);
ccinbufsize -= copied;
}

size_t mockUDPRead (int sock, char* dst, size_t size, int timeout_ms, char* ccinbuf, size_t& ccinbufsize)
{
size_t copied = mockUDPPeekBytes(sock, dst, size, timeout_ms, ccinbuf, ccinbufsize);
mockUDPSwallow(copied, ccinbuf, ccinbufsize);
return copied;
}

Expand Down
2 changes: 2 additions & 0 deletions tests/host/common/esp8266_peri.h
Expand Up @@ -3,5 +3,7 @@
#define FAKE_ESP8266_PERI_H

const int GPI = 0;
const int GPO = 0;
const int GP16I = 0;

#endif
10 changes: 1 addition & 9 deletions tests/host/common/include/UdpContext.h
Expand Up @@ -79,20 +79,12 @@ class UdpContext
_sock = -1;
}

#if 0
void setMulticastInterface(const ip_addr_t& addr)
{
(void)addr;
// user multicast, and this is how it works with posix: send to multicast address:
_dst.addr = staticMCastAddr;
}
#endif
void setMulticastInterface(const ip_addr_t* addr)
{
(void)addr;
// user multicast, and this is how it works with posix: send to multicast address:
_dst.addr = staticMCastAddr;
}

void setMulticastTTL(int ttl)
{
Expand All @@ -118,12 +110,12 @@ class UdpContext

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);
}
mockUDPSwallow(pos, _inbuf, _inbufsize);
}

bool isValidOffset(const size_t pos) const {
Expand Down
13 changes: 12 additions & 1 deletion tests/host/common/mock.h
Expand Up @@ -91,7 +91,7 @@ extern uint32_t global_ipv4_netfmt; // selected interface addresse to bind to
#ifdef __cplusplus

#ifndef CCBUFSIZE
#define CCBUFSIZE 8192
#define CCBUFSIZE 65536
#endif

// tcp
Expand All @@ -103,12 +103,14 @@ size_t mockWrite (int sock, const uint8_t* data, size_t size, int timeout_ms
int serverAccept (int sock);

// udp
void check_incoming_udp ();
int mockUDPSocket ();
bool mockUDPListen (int sock, uint32_t dstaddr, uint16_t port, uint32_t mcast = 0);
size_t mockUDPFillInBuf (int sock, char* ccinbuf, size_t& ccinbufsize, uint8_t& addrsize, uint8_t addr[16], uint16_t& port);
size_t mockUDPPeekBytes (int sock, char* dst, size_t usersize, int timeout_ms, char* ccinbuf, size_t& ccinbufsize);
size_t mockUDPRead (int sock, char* dst, size_t size, int timeout_ms, char* ccinbuf, size_t& ccinbufsize);
size_t mockUDPWrite (int sock, const uint8_t* data, size_t size, int timeout_ms, uint32_t ipv4, uint16_t port);
void mockUDPSwallow (size_t copied, char* ccinbuf, size_t& ccinbufsize);

class UdpContext;
void register_udp (int sock, UdpContext* udp = nullptr);
Expand All @@ -117,6 +119,11 @@ class InterruptLock { };

//

void mock_start_spiffs (size_t size_kb, size_t block_kb = 8, size_t page_b = 512);
void mock_stop_spiffs ();

//

#define CORE_MOCK 1

#define ARDUINO 267
Expand All @@ -142,4 +149,8 @@ class InterruptLock { };

//

#include <common/esp8266_peri.h>

//

#endif // __cplusplus