Skip to content

Commit

Permalink
host emulation: improve udp, persistent spiffs (#5605)
Browse files Browse the repository at this point in the history
  • Loading branch information
d-a-v committed Jan 15, 2019
1 parent 8a64a12 commit 6bd26a3
Show file tree
Hide file tree
Showing 16 changed files with 266 additions and 142 deletions.
Expand Up @@ -17,7 +17,7 @@
upload the contents of the data folder with MkSPIFFS Tool ("ESP8266 Sketch Data Upload" in Tools menu in Arduino IDE)
or you can upload the contents of a folder if you CD in that folder and run the following command:
for file in `ls -A1`; do curl -F "file=@$PWD/$file" esp8266fs.local/edit; done
for file in `\ls -A1`; do curl -F "file=@$PWD/$file" esp8266fs.local/edit; done
access the sample web page at http://esp8266fs.local
edit the page by going to http://esp8266fs.local/edit
Expand Down
6 changes: 4 additions & 2 deletions tests/host/Makefile
Expand Up @@ -19,8 +19,8 @@ LCOV ?= lcov
GENHTML ?= genhtml

ifeq ($(FORCE32),1)
ABILITY32 = $(shell echo 'int main(){return sizeof(long);}'|$(CXX) -m32 -x c++ - -o sizeoflong 2>/dev/null && ./sizeoflong; echo $$?; rm -f sizeoflong;)
ifneq ($(ABILITY32),4)
SIZEOFLONG = $(shell echo 'int main(){return sizeof(long);}'|$(CXX) -m32 -x c++ - -o sizeoflong 2>/dev/null && ./sizeoflong; echo $$?; rm -f sizeoflong;)
ifneq ($(SIZEOFLONG),4)
$(warning Cannot compile in 32 bit mode, switching to native mode)
else
N32 = 32
Expand Down 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
90 changes: 45 additions & 45 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,36 @@ 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();
exit(1);
}
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 +107,37 @@ 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);
}
}

if (spiffs_kb)
{
String name = argv[0];
name += "-spiffs";
name += String(spiffs_kb > 0? spiffs_kb: -spiffs_kb, DEC);
name += "KB";
mock_start_spiffs(name, 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;
}

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

#include "spiffs_mock.h"

SpiffsMock* spiffs_mock = nullptr;

void mock_start_spiffs (const String& fname, size_t size_kb, size_t block_kb, size_t page_b)
{
spiffs_mock = new SpiffsMock(size_kb * 1024, block_kb * 1024, page_b, fname);
}

void mock_stop_spiffs ()
{
if (spiffs_mock)
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();
}
}
}
25 changes: 16 additions & 9 deletions tests/host/common/ClientContextSocket.cpp
Expand Up @@ -66,20 +66,24 @@ int mockConnect (uint32_t ipv4, int& sock, int port)
return 1;
}

size_t mockFillInBuf (int sock, char* ccinbuf, size_t& ccinbufsize)
ssize_t mockFillInBuf (int sock, char* ccinbuf, size_t& ccinbufsize)
{
size_t maxread = CCBUFSIZE - ccinbufsize;
ssize_t ret = ::read(sock, ccinbuf + ccinbufsize, maxread);
if (ret == -1)
{
if (errno != EAGAIN)
fprintf(stderr, MOCK "ClientContext::(read/peek): filling buffer for %zd bytes: %s\n", maxread, strerror(errno));
{
fprintf(stderr, MOCK "ClientContext::(read/peek fd=%i): filling buffer for %zd bytes: %s\n", sock, maxread, strerror(errno));
return -1;
}
ret = 0;
}
return ccinbufsize += ret;
ccinbufsize += ret;
return ret;
}

size_t mockPeekBytes (int sock, char* dst, size_t usersize, int timeout_ms, char* ccinbuf, size_t& ccinbufsize)
ssize_t mockPeekBytes (int sock, char* dst, size_t usersize, int timeout_ms, char* ccinbuf, size_t& ccinbufsize)
{
if (usersize > CCBUFSIZE)
fprintf(stderr, MOCK "CCBUFSIZE(%d) should be increased by %zd bytes (-> %zd)\n", CCBUFSIZE, usersize - CCBUFSIZE, usersize);
Expand All @@ -96,7 +100,8 @@ size_t mockPeekBytes (int sock, char* dst, size_t usersize, int timeout_ms, char
}

// check incoming data data
mockFillInBuf(sock, ccinbuf, ccinbufsize);
if (mockFillInBuf(sock, ccinbuf, ccinbufsize) < 0)
return -1;
if (usersize <= ccinbufsize)
{
// data just received
Expand All @@ -113,16 +118,18 @@ size_t mockPeekBytes (int sock, char* dst, size_t usersize, int timeout_ms, char
return retsize;
}

size_t mockRead (int sock, char* dst, size_t size, int timeout_ms, char* ccinbuf, size_t& ccinbufsize)
ssize_t mockRead (int sock, char* dst, size_t size, int timeout_ms, char* ccinbuf, size_t& ccinbufsize)
{
size_t copied = mockPeekBytes(sock, dst, size, timeout_ms, ccinbuf, ccinbufsize);
ssize_t copied = mockPeekBytes(sock, dst, size, timeout_ms, ccinbuf, ccinbufsize);
if (copied < 0)
return -1;
// swallow (XXX use a circular buffer)
memmove(ccinbuf, ccinbuf + copied, ccinbufsize - copied);
ccinbufsize -= copied;
return copied;
}

size_t mockWrite (int sock, const uint8_t* data, size_t size, int timeout_ms)
ssize_t mockWrite (int sock, const uint8_t* data, size_t size, int timeout_ms)
{
struct pollfd p;
p.fd = sock;
Expand All @@ -140,7 +147,7 @@ size_t mockWrite (int sock, const uint8_t* data, size_t size, int timeout_ms)
if (ret == -1)
{
fprintf(stderr, MOCK "ClientContext::read: write(%d): %s\n", sock, strerror(errno));
return 0;
return -1;
}
if (ret != (int)size)
{
Expand Down
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

0 comments on commit 6bd26a3

Please sign in to comment.