Skip to content

fix host emulation on macOS #5633

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

Merged
merged 3 commits into from
Jan 19, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion cores/esp8266/HardwareSerial.h
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ class HardwareSerial: public Stream
{
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);
}
Expand Down
3 changes: 3 additions & 0 deletions tests/host/common/Arduino.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

Expand Down
3 changes: 3 additions & 0 deletions tests/host/common/Arduino.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ extern "C" {
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <errno.h>

#include "binary.h"
#include "twi.h"
Expand Down Expand Up @@ -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))
Expand Down
35 changes: 26 additions & 9 deletions tests/host/common/ClientContextSocket.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,27 @@
#include <fcntl.h>
#include <errno.h>

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;
Expand All @@ -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)
Expand Down Expand Up @@ -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));
Expand Down
8 changes: 1 addition & 7 deletions tests/host/common/MockWiFiServerSocket.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
6 changes: 6 additions & 0 deletions tests/host/common/UdpContextSocket.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
#include <fcntl.h>
#include <errno.h>
#include <assert.h>
#include <net/if.h>

int mockUDPSocket ()
{
Expand Down Expand Up @@ -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));
Expand Down
4 changes: 2 additions & 2 deletions tests/host/common/c_types.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down
1 change: 1 addition & 0 deletions tests/host/common/include/ClientContext.h
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,7 @@ class ClientContext

bool wait_until_sent(int max_wait_ms = WIFICLIENT_MAX_FLUSH_WAIT_MS)
{
(void)max_wait_ms;
return true;
}

Expand Down
1 change: 1 addition & 0 deletions tests/host/common/mock.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
4 changes: 4 additions & 0 deletions tests/host/common/user_interface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -237,6 +238,7 @@ bool wifi_set_opmode_current (uint8 opmode)

bool wifi_set_phy_mode (phy_mode_t mode)
{
(void)mode;
return true;
}

Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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;
}
Expand Down