mirror of
https://github.com/esp8266/Arduino.git
synced 2025-04-21 10:26:06 +03:00
restore proper arduino Client:: & Wire:: API (#5969)
This commit is contained in:
parent
5dd780c571
commit
cdb549572d
@ -26,7 +26,7 @@
|
|||||||
class Client: public Stream {
|
class Client: public Stream {
|
||||||
|
|
||||||
public:
|
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 int connect(const char *host, uint16_t port) =0;
|
||||||
virtual size_t write(uint8_t) =0;
|
virtual size_t write(uint8_t) =0;
|
||||||
virtual size_t write(const uint8_t *buf, size_t size) =0;
|
virtual size_t write(const uint8_t *buf, size_t size) =0;
|
||||||
@ -34,15 +34,19 @@ class Client: public Stream {
|
|||||||
virtual int read() = 0;
|
virtual int read() = 0;
|
||||||
virtual int read(uint8_t *buf, size_t size) = 0;
|
virtual int read(uint8_t *buf, size_t size) = 0;
|
||||||
virtual int peek() = 0;
|
virtual int peek() = 0;
|
||||||
virtual bool flush(unsigned int maxWaitMs = 0) = 0;
|
virtual void flush() = 0;
|
||||||
virtual bool stop(unsigned int maxWaitMs = 0) = 0;
|
virtual void stop() = 0;
|
||||||
virtual uint8_t connected() = 0;
|
virtual uint8_t connected() = 0;
|
||||||
virtual operator bool() = 0;
|
virtual operator bool() = 0;
|
||||||
protected:
|
protected:
|
||||||
CONST uint8_t* rawIPAddress(CONST IPAddress& addr) {
|
uint8_t* rawIPAddress(IPAddress& addr) {
|
||||||
return addr.raw_address();
|
return addr.raw_address();
|
||||||
}
|
}
|
||||||
;
|
#if LWIP_VERSION_MAJOR != 1
|
||||||
|
const uint8_t* rawIPAddress(const IPAddress& addr) {
|
||||||
|
return addr.raw_address();
|
||||||
|
}
|
||||||
|
#endif
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
@ -79,13 +79,18 @@ class UDP: public Stream {
|
|||||||
virtual void flush() =0; // Finish reading the current packet
|
virtual void flush() =0; // Finish reading the current packet
|
||||||
|
|
||||||
// Return the IP address of the host who sent the current incoming 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
|
// Return the port of the host who sent the current incoming packet
|
||||||
virtual uint16_t remotePort() const =0;
|
virtual uint16_t remotePort() =0;
|
||||||
protected:
|
protected:
|
||||||
CONST uint8_t* rawIPAddress(CONST IPAddress& addr) {
|
uint8_t* rawIPAddress(IPAddress& addr) {
|
||||||
return addr.raw_address();
|
return addr.raw_address();
|
||||||
}
|
}
|
||||||
|
#if LWIP_VERSION_MAJOR != 1
|
||||||
|
const uint8_t* rawIPAddress(const IPAddress& addr) {
|
||||||
|
return addr.raw_address();
|
||||||
|
}
|
||||||
|
#endif
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
@ -137,7 +137,7 @@ int WiFiClient::connect(const String& host, uint16_t port)
|
|||||||
return connect(host.c_str(), 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) {
|
if (_client) {
|
||||||
stop();
|
stop();
|
||||||
|
@ -53,29 +53,31 @@ public:
|
|||||||
WiFiClient& operator=(const WiFiClient&);
|
WiFiClient& operator=(const WiFiClient&);
|
||||||
|
|
||||||
uint8_t status();
|
uint8_t status();
|
||||||
virtual int connect(CONST IPAddress& ip, uint16_t port);
|
virtual int connect(IPAddress ip, uint16_t port) override;
|
||||||
virtual int connect(const char *host, uint16_t port);
|
virtual int connect(const char *host, uint16_t port) override;
|
||||||
virtual int connect(const String& host, uint16_t port);
|
virtual int connect(const String& host, uint16_t port);
|
||||||
virtual size_t write(uint8_t);
|
virtual size_t write(uint8_t) override;
|
||||||
virtual size_t write(const uint8_t *buf, size_t size);
|
virtual size_t write(const uint8_t *buf, size_t size) override;
|
||||||
virtual size_t write_P(PGM_P buf, size_t size);
|
virtual size_t write_P(PGM_P buf, size_t size);
|
||||||
size_t write(Stream& stream);
|
size_t write(Stream& stream);
|
||||||
|
|
||||||
// This one is deprecated, use write(Stream& instead)
|
// This one is deprecated, use write(Stream& instead)
|
||||||
size_t write(Stream& stream, size_t unitSize) __attribute__ ((deprecated));
|
size_t write(Stream& stream, size_t unitSize) __attribute__ ((deprecated));
|
||||||
|
|
||||||
virtual int available();
|
virtual int available() override;
|
||||||
virtual int read();
|
virtual int read() override;
|
||||||
virtual int read(uint8_t *buf, size_t size);
|
virtual int read(uint8_t *buf, size_t size) override;
|
||||||
virtual int peek();
|
virtual int peek() override;
|
||||||
virtual size_t peekBytes(uint8_t *buffer, size_t length);
|
virtual size_t peekBytes(uint8_t *buffer, size_t length);
|
||||||
size_t peekBytes(char *buffer, size_t length) {
|
size_t peekBytes(char *buffer, size_t length) {
|
||||||
return peekBytes((uint8_t *) buffer, length);
|
return peekBytes((uint8_t *) buffer, length);
|
||||||
}
|
}
|
||||||
virtual bool flush(unsigned int maxWaitMs = 0);
|
virtual void flush() override { (void)flush(0); }
|
||||||
virtual bool stop(unsigned int maxWaitMs = 0);
|
virtual void stop() override { (void)stop(0); }
|
||||||
virtual uint8_t connected();
|
bool flush(unsigned int maxWaitMs);
|
||||||
virtual operator bool();
|
bool stop(unsigned int maxWaitMs);
|
||||||
|
virtual uint8_t connected() override;
|
||||||
|
virtual operator bool() override;
|
||||||
|
|
||||||
IPAddress remoteIP();
|
IPAddress remoteIP();
|
||||||
uint16_t remotePort();
|
uint16_t remotePort();
|
||||||
|
@ -95,7 +95,7 @@ WiFiClientSecure::WiFiClientSecure(ClientContext* client, bool usePMEM,
|
|||||||
_ssl->connectServer(client, _timeout);
|
_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)) {
|
if (!WiFiClient::connect(ip, port)) {
|
||||||
return 0;
|
return 0;
|
||||||
|
@ -35,7 +35,7 @@ public:
|
|||||||
WiFiClientSecure() __attribute__((deprecated("Upgrade to BearSSL is advised, check https://github.com/esp8266/Arduino/blob/master/libraries/ESP8266WiFi/src/WiFiClientSecure.h#L25-L99")));
|
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;
|
~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 String& host, uint16_t port) override;
|
||||||
int connect(const char* name, uint16_t port) override;
|
int connect(const char* name, uint16_t port) override;
|
||||||
|
|
||||||
@ -51,7 +51,8 @@ public:
|
|||||||
int read() override;
|
int read() override;
|
||||||
int peek() override;
|
int peek() override;
|
||||||
size_t peekBytes(uint8_t *buffer, size_t length) 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 setCACert(const uint8_t* pk, size_t size);
|
||||||
bool setCertificate(const uint8_t* pk, size_t size);
|
bool setCertificate(const uint8_t* pk, size_t size);
|
||||||
|
@ -210,7 +210,7 @@ bool WiFiClientSecure::flush(unsigned int maxWaitMs) {
|
|||||||
return WiFiClient::flush(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)) {
|
if (!WiFiClient::connect(ip, port)) {
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
@ -37,7 +37,7 @@ class WiFiClientSecure : public WiFiClient {
|
|||||||
WiFiClientSecure(const WiFiClientSecure &rhs);
|
WiFiClientSecure(const WiFiClientSecure &rhs);
|
||||||
~WiFiClientSecure() override;
|
~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 String& host, uint16_t port) override;
|
||||||
int connect(const char* name, uint16_t port) override;
|
int connect(const char* name, uint16_t port) override;
|
||||||
|
|
||||||
@ -56,8 +56,10 @@ class WiFiClientSecure : public WiFiClient {
|
|||||||
int read() override;
|
int read() override;
|
||||||
int peek() override;
|
int peek() override;
|
||||||
size_t peekBytes(uint8_t *buffer, size_t length) override;
|
size_t peekBytes(uint8_t *buffer, size_t length) override;
|
||||||
bool flush(unsigned int maxWaitMs = 0) override;
|
bool flush(unsigned int maxWaitMs);
|
||||||
bool stop(unsigned int maxWaitMs = 0) override;
|
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
|
// Allow sessions to be saved/restored automatically to a memory area
|
||||||
void setSession(Session *session) { _session = session; }
|
void setSession(Session *session) { _session = session; }
|
||||||
|
@ -231,7 +231,7 @@ void WiFiUDP::flush()
|
|||||||
endPacket();
|
endPacket();
|
||||||
}
|
}
|
||||||
|
|
||||||
IPAddress WiFiUDP::remoteIP() const
|
IPAddress WiFiUDP::remoteIP()
|
||||||
{
|
{
|
||||||
if (!_ctx)
|
if (!_ctx)
|
||||||
return INADDR_ANY;
|
return INADDR_ANY;
|
||||||
@ -239,7 +239,7 @@ IPAddress WiFiUDP::remoteIP() const
|
|||||||
return _ctx->getRemoteAddress();
|
return _ctx->getRemoteAddress();
|
||||||
}
|
}
|
||||||
|
|
||||||
uint16_t WiFiUDP::remotePort() const
|
uint16_t WiFiUDP::remotePort()
|
||||||
{
|
{
|
||||||
if (!_ctx)
|
if (!_ctx)
|
||||||
return 0;
|
return 0;
|
||||||
|
@ -95,9 +95,9 @@ public:
|
|||||||
void flush() override; // Finish reading the current packet
|
void flush() override; // Finish reading the current packet
|
||||||
|
|
||||||
// Return the IP address of the host who sent the current incoming 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
|
// 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,
|
// Return the destination address for incoming packets,
|
||||||
// useful to distinguish multicast and ordinary packets
|
// useful to distinguish multicast and ordinary packets
|
||||||
IPAddress destinationIP() const;
|
IPAddress destinationIP() const;
|
||||||
|
@ -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)
|
if (_sock != MAX_SOCK_NUM)
|
||||||
return 0;
|
return 0;
|
||||||
|
|
||||||
@ -119,15 +119,13 @@ int EthernetClient::peek() {
|
|||||||
return b;
|
return b;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool EthernetClient::flush(unsigned int maxWaitMs) {
|
void EthernetClient::flush() {
|
||||||
(void)maxWaitMs;
|
|
||||||
::flush(_sock);
|
::flush(_sock);
|
||||||
return true;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
bool EthernetClient::stop(unsigned int maxWaitMs) {
|
void EthernetClient::stop() {
|
||||||
if (_sock == MAX_SOCK_NUM)
|
if (_sock == MAX_SOCK_NUM)
|
||||||
return true;
|
return;
|
||||||
|
|
||||||
// attempt to close the connection gracefully (send a FIN to other side)
|
// attempt to close the connection gracefully (send a FIN to other side)
|
||||||
disconnect(_sock);
|
disconnect(_sock);
|
||||||
@ -135,27 +133,20 @@ bool EthernetClient::stop(unsigned int maxWaitMs) {
|
|||||||
|
|
||||||
// wait up to a second for the connection to close
|
// wait up to a second for the connection to close
|
||||||
uint8_t s;
|
uint8_t s;
|
||||||
if (maxWaitMs == 0)
|
|
||||||
maxWaitMs = 1000;
|
|
||||||
do {
|
do {
|
||||||
s = status();
|
s = status();
|
||||||
if (s == SnSR::CLOSED)
|
if (s == SnSR::CLOSED)
|
||||||
break; // exit the loop
|
break; // exit the loop
|
||||||
delay(1);
|
delay(1);
|
||||||
} while (millis() - start < maxWaitMs);
|
} while (millis() - start < 1000);
|
||||||
|
|
||||||
bool ret = true;
|
|
||||||
|
|
||||||
// if it hasn't closed, close it forcefully
|
// if it hasn't closed, close it forcefully
|
||||||
if (s != SnSR::CLOSED) {
|
if (s != SnSR::CLOSED) {
|
||||||
ret = false;
|
|
||||||
close(_sock);
|
close(_sock);
|
||||||
}
|
}
|
||||||
|
|
||||||
EthernetClass::_server_port[_sock] = 0;
|
EthernetClass::_server_port[_sock] = 0;
|
||||||
_sock = MAX_SOCK_NUM;
|
_sock = MAX_SOCK_NUM;
|
||||||
|
|
||||||
return ret;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
uint8_t EthernetClient::connected() {
|
uint8_t EthernetClient::connected() {
|
||||||
|
@ -12,7 +12,7 @@ public:
|
|||||||
EthernetClient(uint8_t sock);
|
EthernetClient(uint8_t sock);
|
||||||
|
|
||||||
uint8_t status();
|
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 int connect(const char *host, uint16_t port);
|
||||||
virtual size_t write(uint8_t);
|
virtual size_t write(uint8_t);
|
||||||
virtual size_t write(const uint8_t *buf, size_t size);
|
virtual size_t write(const uint8_t *buf, size_t size);
|
||||||
@ -20,8 +20,8 @@ public:
|
|||||||
virtual int read();
|
virtual int read();
|
||||||
virtual int read(uint8_t *buf, size_t size);
|
virtual int read(uint8_t *buf, size_t size);
|
||||||
virtual int peek();
|
virtual int peek();
|
||||||
virtual bool flush(unsigned int maxWaitMs = 0);
|
virtual void flush();
|
||||||
virtual bool stop(unsigned int maxWaitMs = 0);
|
virtual void stop();
|
||||||
virtual uint8_t connected();
|
virtual uint8_t connected();
|
||||||
virtual operator bool();
|
virtual operator bool();
|
||||||
virtual bool operator==(const bool value) { return bool() == value; }
|
virtual bool operator==(const bool value) { return bool() == value; }
|
||||||
|
@ -94,9 +94,9 @@ public:
|
|||||||
virtual void flush(); // Finish reading the current packet
|
virtual void flush(); // Finish reading the current packet
|
||||||
|
|
||||||
// Return the IP address of the host who sent the current incoming 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
|
// 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
|
#endif
|
||||||
|
@ -265,6 +265,13 @@ void TwoWire::onRequestService(void)
|
|||||||
user_onRequest();
|
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) ) {
|
void TwoWire::onReceive( void (*function)(size_t) ) {
|
||||||
user_onReceive = function;
|
user_onReceive = function;
|
||||||
}
|
}
|
||||||
|
@ -76,7 +76,8 @@ class TwoWire : public Stream
|
|||||||
virtual int read(void);
|
virtual int read(void);
|
||||||
virtual int peek(void);
|
virtual int peek(void);
|
||||||
virtual void flush(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) );
|
void onRequest( void (*)(void) );
|
||||||
|
|
||||||
inline size_t write(unsigned long n) { return write((uint8_t)n); }
|
inline size_t write(unsigned long n) { return write((uint8_t)n); }
|
||||||
|
Loading…
x
Reference in New Issue
Block a user