From bd11d026e888d550e05720eeedc00840d6662f93 Mon Sep 17 00:00:00 2001 From: david gauchard Date: Sat, 19 Jan 2019 03:19:17 +0100 Subject: [PATCH] fix host emulation on macOS (#5633) --- cores/esp8266/HardwareSerial.h | 2 +- tests/host/common/Arduino.cpp | 3 ++ tests/host/common/Arduino.h | 3 ++ tests/host/common/ClientContextSocket.cpp | 35 ++++++++++++++++------ tests/host/common/MockWiFiServerSocket.cpp | 8 +---- tests/host/common/UdpContextSocket.cpp | 6 ++++ tests/host/common/c_types.h | 4 +-- tests/host/common/include/ClientContext.h | 1 + tests/host/common/mock.h | 1 + tests/host/common/user_interface.cpp | 4 +++ 10 files changed, 48 insertions(+), 19 deletions(-) diff --git a/cores/esp8266/HardwareSerial.h b/cores/esp8266/HardwareSerial.h index f66bb8a77..ce9a1c566 100644 --- a/cores/esp8266/HardwareSerial.h +++ b/cores/esp8266/HardwareSerial.h @@ -167,7 +167,7 @@ public: { return write((uint8_t) n); } - size_t write(const uint8_t *buffer, size_t size) + size_t write(const uint8_t *buffer, size_t size) override { return uart_write(_uart, (const char*)buffer, size); } diff --git a/tests/host/common/Arduino.cpp b/tests/host/common/Arduino.cpp index b758ec720..5f173646a 100644 --- a/tests/host/common/Arduino.cpp +++ b/tests/host/common/Arduino.cpp @@ -43,6 +43,9 @@ extern "C" void esp_yield() extern "C" void __panic_func(const char* file, int line, const char* func) { + (void)file; + (void)line; + (void)func; abort(); } diff --git a/tests/host/common/Arduino.h b/tests/host/common/Arduino.h index 84b8226a1..a1aba082c 100644 --- a/tests/host/common/Arduino.h +++ b/tests/host/common/Arduino.h @@ -34,6 +34,7 @@ extern "C" { #include #include #include +#include #include "binary.h" #include "twi.h" @@ -249,11 +250,13 @@ extern "C" { #include "Updater.h" #include "debug.h" +#if 0 #ifndef _GLIBCXX_VECTOR // arduino is not compatible with std::vector #define min(a,b) ((a)<(b)?(a):(b)) #define max(a,b) ((a)>(b)?(a):(b)) #endif +#endif #define _min(a,b) ((a)<(b)?(a):(b)) #define _max(a,b) ((a)>(b)?(a):(b)) diff --git a/tests/host/common/ClientContextSocket.cpp b/tests/host/common/ClientContextSocket.cpp index 8778a1f49..2bee9fa1c 100644 --- a/tests/host/common/ClientContextSocket.cpp +++ b/tests/host/common/ClientContextSocket.cpp @@ -39,6 +39,27 @@ #include #include +int mockSockSetup (int sock) +{ + if (fcntl(sock, F_SETFL, O_NONBLOCK) == -1) + { + fprintf(stderr, MOCK "socket fcntl(O_NONBLOCK): %s\n", strerror(errno)); + close(sock); + return -1; + } + +#ifndef MSG_NOSIGNAL + int i = 1; + if (setsockopt(sock, SOL_SOCKET, SO_NOSIGPIPE, &i, sizeof i) == -1) + { + fprintf(stderr, MOCK "sockopt( SO_NOSIGPIPE)(macOS): %s\n", strerror(errno)); + return -1; + } +#endif + + return sock; +} + int mockConnect (uint32_t ipv4, int& sock, int port) { struct sockaddr_in server; @@ -56,14 +77,7 @@ int mockConnect (uint32_t ipv4, int& sock, int port) return 0; } - if (fcntl(sock, F_SETFL, O_NONBLOCK) == -1) - { - fprintf(stderr, MOCK "ClientContext::connect: fcntl(O_NONBLOCK): %s\n", strerror(errno)); - close(sock); - return 0; - } - - return 1; + return mockSockSetup(sock) == -1? 0: 1; } ssize_t mockFillInBuf (int sock, char* ccinbuf, size_t& ccinbufsize) @@ -142,8 +156,11 @@ ssize_t mockWrite (int sock, const uint8_t* data, size_t size, int timeout_ms) } if (ret) { - //ret = ::write(sock, data, size); +#ifndef MSG_NOSIGNAL + ret = ::write(sock, data, size); +#else ret = ::send(sock, data, size, MSG_NOSIGNAL); +#endif if (ret == -1) { fprintf(stderr, MOCK "ClientContext::read: write(%d): %s\n", sock, strerror(errno)); diff --git a/tests/host/common/MockWiFiServerSocket.cpp b/tests/host/common/MockWiFiServerSocket.cpp index e5fefc932..5e83c3717 100644 --- a/tests/host/common/MockWiFiServerSocket.cpp +++ b/tests/host/common/MockWiFiServerSocket.cpp @@ -55,13 +55,7 @@ int serverAccept (int srvsock) perror(MOCK "accept()"); exit(EXIT_FAILURE); } - if (fcntl(clisock, F_SETFL, O_NONBLOCK) == -1) - { - fprintf(stderr, MOCK "ClientContext::accept: fcntl(O_NONBLOCK): %s\n", strerror(errno)); - close(clisock); - exit(EXIT_FAILURE); - } - return clisock; + return mockSockSetup(clisock); } void WiFiServer::begin (uint16_t port) diff --git a/tests/host/common/UdpContextSocket.cpp b/tests/host/common/UdpContextSocket.cpp index f257c57c9..a1135552c 100644 --- a/tests/host/common/UdpContextSocket.cpp +++ b/tests/host/common/UdpContextSocket.cpp @@ -37,6 +37,7 @@ #include #include #include +#include int mockUDPSocket () { @@ -87,7 +88,12 @@ bool mockUDPListen (int sock, uint32_t dstaddr, uint16_t port, uint32_t mcast) mreq.imr_interface.s_addr = htonl(INADDR_ANY); if (global_ipv4_netfmt) { +#if __APPLE__ + int idx = if_nametoindex(host_interface); + if (setsockopt(sock, IPPROTO_TCP, IP_BOUND_IF, &idx, sizeof(idx)) == -1) +#else if (setsockopt(sock, SOL_SOCKET, SO_BINDTODEVICE, host_interface, strlen(host_interface)) == -1) +#endif fprintf(stderr, MOCK "UDP multicast: can't setup bind/output on interface %s: %s\n", host_interface, strerror(errno)); if (setsockopt(sock, IPPROTO_IP, IP_MULTICAST_IF, &mreq.imr_interface, sizeof(struct in_addr)) == -1) fprintf(stderr, MOCK "UDP multicast: can't setup bind/input on interface %s: %s\n", host_interface, strerror(errno)); diff --git a/tests/host/common/c_types.h b/tests/host/common/c_types.h index 765e307a9..897c2cbc6 100644 --- a/tests/host/common/c_types.h +++ b/tests/host/common/c_types.h @@ -44,7 +44,7 @@ typedef signed long long sint64_t; typedef float real32_t; typedef double real64_t; -typedef unsigned char uint8; +// CONFLICT typedef unsigned char uint8; typedef unsigned char u8; typedef signed char sint8; typedef signed char int8; @@ -53,7 +53,7 @@ typedef unsigned short uint16; typedef unsigned short u16; typedef signed short sint16; typedef signed short s16; -typedef unsigned int uint32; +// CONFLICT typedef unsigned int uint32; typedef unsigned int u_int; typedef unsigned int u32; typedef signed int sint32; diff --git a/tests/host/common/include/ClientContext.h b/tests/host/common/include/ClientContext.h index bbd3b0688..3d212d73e 100644 --- a/tests/host/common/include/ClientContext.h +++ b/tests/host/common/include/ClientContext.h @@ -198,6 +198,7 @@ public: bool wait_until_sent(int max_wait_ms = WIFICLIENT_MAX_FLUSH_WAIT_MS) { + (void)max_wait_ms; return true; } diff --git a/tests/host/common/mock.h b/tests/host/common/mock.h index b48c0eda3..23b5a675d 100644 --- a/tests/host/common/mock.h +++ b/tests/host/common/mock.h @@ -95,6 +95,7 @@ extern uint32_t global_ipv4_netfmt; // selected interface addresse to bind to #endif // tcp +int mockSockSetup (int sock); int mockConnect (uint32_t addr, int& sock, int port); ssize_t mockFillInBuf (int sock, char* ccinbuf, size_t& ccinbufsize); ssize_t mockPeekBytes (int sock, char* dst, size_t size, int timeout_ms, char* buf, size_t& bufsize); diff --git a/tests/host/common/user_interface.cpp b/tests/host/common/user_interface.cpp index 42bafb3fc..34e9621e2 100644 --- a/tests/host/common/user_interface.cpp +++ b/tests/host/common/user_interface.cpp @@ -175,6 +175,7 @@ uint8 wifi_get_listen_interval (void) bool wifi_get_macaddr(uint8 if_index, uint8 *macaddr) { + (void)if_index; macaddr[0] = 0xde; macaddr[1] = 0xba; macaddr[2] = 0x7a; @@ -237,6 +238,7 @@ bool wifi_set_opmode_current (uint8 opmode) bool wifi_set_phy_mode (phy_mode_t mode) { + (void)mode; return true; } @@ -398,6 +400,7 @@ bool wifi_softap_set_dhcps_offer_option(uint8 level, void* optarg) bool wifi_station_scan(struct scan_config *config, scan_done_cb_t cb) { + (void)config; cb(nullptr, FAIL); return false; } @@ -434,6 +437,7 @@ void dns_setserver (u8_t numdns, ip_addr_t *dnsserver) ip_addr_t dns_getserver (u8_t numdns) { + (void)numdns; ip_addr_t addr = { 0x7f000001 }; return addr; }