Skip to content

Commit

Permalink
restore proper arduino Client:: & Wire:: API (#5969)
Browse files Browse the repository at this point in the history
  • Loading branch information
d-a-v committed Apr 26, 2019
1 parent 5dd780c commit cdb5495
Show file tree
Hide file tree
Showing 15 changed files with 65 additions and 52 deletions.
14 changes: 9 additions & 5 deletions cores/esp8266/Client.h
Expand Up @@ -26,23 +26,27 @@
class Client: public Stream {

public:
virtual int connect(CONST IPAddress& ip, uint16_t port) =0;
virtual int connect(IPAddress ip, uint16_t port) =0;
virtual int connect(const char *host, uint16_t port) =0;
virtual size_t write(uint8_t) =0;
virtual size_t write(const uint8_t *buf, size_t size) =0;
virtual int available() = 0;
virtual int read() = 0;
virtual int read(uint8_t *buf, size_t size) = 0;
virtual int peek() = 0;
virtual bool flush(unsigned int maxWaitMs = 0) = 0;
virtual bool stop(unsigned int maxWaitMs = 0) = 0;
virtual void flush() = 0;
virtual void stop() = 0;
virtual uint8_t connected() = 0;
virtual operator bool() = 0;
protected:
CONST uint8_t* rawIPAddress(CONST IPAddress& addr) {
uint8_t* rawIPAddress(IPAddress& addr) {
return addr.raw_address();
}
;
#if LWIP_VERSION_MAJOR != 1
const uint8_t* rawIPAddress(const IPAddress& addr) {
return addr.raw_address();
}
#endif
};

#endif
11 changes: 8 additions & 3 deletions cores/esp8266/Udp.h
Expand Up @@ -79,13 +79,18 @@ class UDP: public Stream {
virtual void flush() =0; // Finish reading the current packet

// Return the IP address of the host who sent the current incoming packet
virtual IPAddress remoteIP() const =0;
virtual IPAddress remoteIP() =0;
// Return the port of the host who sent the current incoming packet
virtual uint16_t remotePort() const =0;
virtual uint16_t remotePort() =0;
protected:
CONST uint8_t* rawIPAddress(CONST IPAddress& addr) {
uint8_t* rawIPAddress(IPAddress& addr) {
return addr.raw_address();
}
#if LWIP_VERSION_MAJOR != 1
const uint8_t* rawIPAddress(const IPAddress& addr) {
return addr.raw_address();
}
#endif
};

#endif
2 changes: 1 addition & 1 deletion libraries/ESP8266WiFi/src/WiFiClient.cpp
Expand Up @@ -137,7 +137,7 @@ int WiFiClient::connect(const String& host, uint16_t port)
return connect(host.c_str(), port);
}

int WiFiClient::connect(CONST IPAddress& ip, uint16_t port)
int WiFiClient::connect(IPAddress ip, uint16_t port)
{
if (_client) {
stop();
Expand Down
26 changes: 14 additions & 12 deletions libraries/ESP8266WiFi/src/WiFiClient.h
Expand Up @@ -53,29 +53,31 @@ class WiFiClient : public Client, public SList<WiFiClient> {
WiFiClient& operator=(const WiFiClient&);

uint8_t status();
virtual int connect(CONST IPAddress& ip, uint16_t port);
virtual int connect(const char *host, uint16_t port);
virtual int connect(IPAddress ip, uint16_t port) override;
virtual int connect(const char *host, uint16_t port) override;
virtual int connect(const String& host, uint16_t port);
virtual size_t write(uint8_t);
virtual size_t write(const uint8_t *buf, size_t size);
virtual size_t write(uint8_t) override;
virtual size_t write(const uint8_t *buf, size_t size) override;
virtual size_t write_P(PGM_P buf, size_t size);
size_t write(Stream& stream);

// This one is deprecated, use write(Stream& instead)
size_t write(Stream& stream, size_t unitSize) __attribute__ ((deprecated));

virtual int available();
virtual int read();
virtual int read(uint8_t *buf, size_t size);
virtual int peek();
virtual int available() override;
virtual int read() override;
virtual int read(uint8_t *buf, size_t size) override;
virtual int peek() override;
virtual size_t peekBytes(uint8_t *buffer, size_t length);
size_t peekBytes(char *buffer, size_t length) {
return peekBytes((uint8_t *) buffer, length);
}
virtual bool flush(unsigned int maxWaitMs = 0);
virtual bool stop(unsigned int maxWaitMs = 0);
virtual uint8_t connected();
virtual operator bool();
virtual void flush() override { (void)flush(0); }
virtual void stop() override { (void)stop(0); }
bool flush(unsigned int maxWaitMs);
bool stop(unsigned int maxWaitMs);
virtual uint8_t connected() override;
virtual operator bool() override;

IPAddress remoteIP();
uint16_t remotePort();
Expand Down
2 changes: 1 addition & 1 deletion libraries/ESP8266WiFi/src/WiFiClientSecureAxTLS.cpp
Expand Up @@ -95,7 +95,7 @@ WiFiClientSecure::WiFiClientSecure(ClientContext* client, bool usePMEM,
_ssl->connectServer(client, _timeout);
}

int WiFiClientSecure::connect(CONST IPAddress& ip, uint16_t port)
int WiFiClientSecure::connect(IPAddress ip, uint16_t port)
{
if (!WiFiClient::connect(ip, port)) {
return 0;
Expand Down
5 changes: 3 additions & 2 deletions libraries/ESP8266WiFi/src/WiFiClientSecureAxTLS.h
Expand Up @@ -35,7 +35,7 @@ class WiFiClientSecure : public WiFiClient {
WiFiClientSecure() __attribute__((deprecated("Upgrade to BearSSL is advised, check https://github.com/esp8266/Arduino/blob/master/libraries/ESP8266WiFi/src/WiFiClientSecure.h#L25-L99")));
~WiFiClientSecure() override;

int connect(CONST IPAddress& ip, uint16_t port) override;
int connect(IPAddress ip, uint16_t port) override;
int connect(const String& host, uint16_t port) override;
int connect(const char* name, uint16_t port) override;

Expand All @@ -51,7 +51,8 @@ class WiFiClientSecure : public WiFiClient {
int read() override;
int peek() override;
size_t peekBytes(uint8_t *buffer, size_t length) override;
bool stop(unsigned int maxWaitMs = 0) override;
void stop() override { (void)stop(0); }
bool stop(unsigned int maxWaitMs);

bool setCACert(const uint8_t* pk, size_t size);
bool setCertificate(const uint8_t* pk, size_t size);
Expand Down
2 changes: 1 addition & 1 deletion libraries/ESP8266WiFi/src/WiFiClientSecureBearSSL.cpp
Expand Up @@ -210,7 +210,7 @@ bool WiFiClientSecure::flush(unsigned int maxWaitMs) {
return WiFiClient::flush(maxWaitMs);
}

int WiFiClientSecure::connect(CONST IPAddress& ip, uint16_t port) {
int WiFiClientSecure::connect(IPAddress ip, uint16_t port) {
if (!WiFiClient::connect(ip, port)) {
return 0;
}
Expand Down
8 changes: 5 additions & 3 deletions libraries/ESP8266WiFi/src/WiFiClientSecureBearSSL.h
Expand Up @@ -37,7 +37,7 @@ class WiFiClientSecure : public WiFiClient {
WiFiClientSecure(const WiFiClientSecure &rhs);
~WiFiClientSecure() override;

int connect(CONST IPAddress& ip, uint16_t port) override;
int connect(IPAddress ip, uint16_t port) override;
int connect(const String& host, uint16_t port) override;
int connect(const char* name, uint16_t port) override;

Expand All @@ -56,8 +56,10 @@ class WiFiClientSecure : public WiFiClient {
int read() override;
int peek() override;
size_t peekBytes(uint8_t *buffer, size_t length) override;
bool flush(unsigned int maxWaitMs = 0) override;
bool stop(unsigned int maxWaitMs = 0) override;
bool flush(unsigned int maxWaitMs);
bool stop(unsigned int maxWaitMs);
void flush() override { (void)flush(0); }
void stop() override { (void)stop(0); }

// Allow sessions to be saved/restored automatically to a memory area
void setSession(Session *session) { _session = session; }
Expand Down
4 changes: 2 additions & 2 deletions libraries/ESP8266WiFi/src/WiFiUdp.cpp
Expand Up @@ -231,15 +231,15 @@ void WiFiUDP::flush()
endPacket();
}

IPAddress WiFiUDP::remoteIP() const
IPAddress WiFiUDP::remoteIP()
{
if (!_ctx)
return INADDR_ANY;

return _ctx->getRemoteAddress();
}

uint16_t WiFiUDP::remotePort() const
uint16_t WiFiUDP::remotePort()
{
if (!_ctx)
return 0;
Expand Down
4 changes: 2 additions & 2 deletions libraries/ESP8266WiFi/src/WiFiUdp.h
Expand Up @@ -95,9 +95,9 @@ class WiFiUDP : public UDP, public SList<WiFiUDP> {
void flush() override; // Finish reading the current packet

// Return the IP address of the host who sent the current incoming packet
IPAddress remoteIP() const override;
IPAddress remoteIP() override;
// Return the port of the host who sent the current incoming packet
uint16_t remotePort() const override;
uint16_t remotePort() override;
// Return the destination address for incoming packets,
// useful to distinguish multicast and ordinary packets
IPAddress destinationIP() const;
Expand Down
19 changes: 5 additions & 14 deletions libraries/Ethernet/src/EthernetClient.cpp
Expand Up @@ -35,7 +35,7 @@ int EthernetClient::connect(const char* host, uint16_t port) {
}
}

int EthernetClient::connect(CONST IPAddress& ip, uint16_t port) {
int EthernetClient::connect(IPAddress ip, uint16_t port) {
if (_sock != MAX_SOCK_NUM)
return 0;

Expand Down Expand Up @@ -119,43 +119,34 @@ int EthernetClient::peek() {
return b;
}

bool EthernetClient::flush(unsigned int maxWaitMs) {
(void)maxWaitMs;
void EthernetClient::flush() {
::flush(_sock);
return true;
}

bool EthernetClient::stop(unsigned int maxWaitMs) {
void EthernetClient::stop() {
if (_sock == MAX_SOCK_NUM)
return true;
return;

// attempt to close the connection gracefully (send a FIN to other side)
disconnect(_sock);
unsigned long start = millis();

// wait up to a second for the connection to close
uint8_t s;
if (maxWaitMs == 0)
maxWaitMs = 1000;
do {
s = status();
if (s == SnSR::CLOSED)
break; // exit the loop
delay(1);
} while (millis() - start < maxWaitMs);

bool ret = true;
} while (millis() - start < 1000);

// if it hasn't closed, close it forcefully
if (s != SnSR::CLOSED) {
ret = false;
close(_sock);
}

EthernetClass::_server_port[_sock] = 0;
_sock = MAX_SOCK_NUM;

return ret;
}

uint8_t EthernetClient::connected() {
Expand Down
6 changes: 3 additions & 3 deletions libraries/Ethernet/src/EthernetClient.h
Expand Up @@ -12,16 +12,16 @@ class EthernetClient : public Client {
EthernetClient(uint8_t sock);

uint8_t status();
virtual int connect(CONST IPAddress& ip, uint16_t port);
virtual int connect(IPAddress ip, uint16_t port);
virtual int connect(const char *host, uint16_t port);
virtual size_t write(uint8_t);
virtual size_t write(const uint8_t *buf, size_t size);
virtual int available();
virtual int read();
virtual int read(uint8_t *buf, size_t size);
virtual int peek();
virtual bool flush(unsigned int maxWaitMs = 0);
virtual bool stop(unsigned int maxWaitMs = 0);
virtual void flush();
virtual void stop();
virtual uint8_t connected();
virtual operator bool();
virtual bool operator==(const bool value) { return bool() == value; }
Expand Down
4 changes: 2 additions & 2 deletions libraries/Ethernet/src/EthernetUdp.h
Expand Up @@ -94,9 +94,9 @@ class EthernetUDP : public UDP {
virtual void flush(); // Finish reading the current packet

// Return the IP address of the host who sent the current incoming packet
virtual IPAddress remoteIP() const { return _remoteIP; };
virtual IPAddress remoteIP() { return _remoteIP; };
// Return the port of the host who sent the current incoming packet
virtual uint16_t remotePort() const { return _remotePort; };
virtual uint16_t remotePort() { return _remotePort; };
};

#endif
7 changes: 7 additions & 0 deletions libraries/Wire/Wire.cpp
Expand Up @@ -265,6 +265,13 @@ void TwoWire::onRequestService(void)
user_onRequest();
}

void TwoWire::onReceive( void (*function)(int) ) {
// arduino api compatibility fixer:
// really hope size parameter will not exceed 2^31 :)
static_assert(sizeof(int) == sizeof(size_t), "something is wrong in Arduino kingdom");
user_onReceive = reinterpret_cast<void(*)(size_t)>(function);
}

void TwoWire::onReceive( void (*function)(size_t) ) {
user_onReceive = function;
}
Expand Down
3 changes: 2 additions & 1 deletion libraries/Wire/Wire.h
Expand Up @@ -76,7 +76,8 @@ class TwoWire : public Stream
virtual int read(void);
virtual int peek(void);
virtual void flush(void);
void onReceive( void (*)(size_t) );
void onReceive( void (*)(int) ); // arduino api
void onReceive( void (*)(size_t) ); // legacy esp8266 backward compatibility
void onRequest( void (*)(void) );

inline size_t write(unsigned long n) { return write((uint8_t)n); }
Expand Down

0 comments on commit cdb5495

Please sign in to comment.