Skip to content

Commit a5e512a

Browse files
committed
Manage Client status
1 parent 540e0ab commit a5e512a

File tree

2 files changed

+20
-8
lines changed

2 files changed

+20
-8
lines changed

libraries/Ethernet/src/EthernetClient.cpp

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,31 @@
44
#define SOCKET_TIMEOUT 1000
55
#endif
66

7-
arduino::EthernetClient::EthernetClient() {
7+
arduino::EthernetClient::EthernetClient()
8+
: _status(false)
9+
{
810
}
911

1012
uint8_t arduino::EthernetClient::status() {
1113
return _status;
1214
}
1315

16+
void arduino::EthernetClient::setSocket(Socket* _sock) {
17+
sock = _sock;
18+
_status = true;
19+
}
20+
1421
void arduino::EthernetClient::getStatus() {
22+
if (sock == nullptr)
23+
return;
24+
1525
uint8_t data[256];
1626
int ret = sock->recv(data, rxBuffer.availableForStore());
1727
for (int i = 0; i < ret; i++) {
1828
rxBuffer.store_char(data[i]);
1929
}
2030
if (ret < 0 && ret != NSAPI_ERROR_WOULD_BLOCK) {
21-
_status = LinkOFF;
31+
_status = false;
2232
}
2333
}
2434

@@ -34,7 +44,10 @@ int arduino::EthernetClient::connect(SocketAddress socketAddress) {
3444
address = socketAddress;
3545
sock->set_timeout(SOCKET_TIMEOUT);
3646
nsapi_error_t returnCode = static_cast<TCPSocket*>(sock)->connect(socketAddress);
37-
return returnCode == NSAPI_ERROR_OK ? 1 : 0;
47+
auto ret = returnCode == NSAPI_ERROR_OK ? 1 : 0;
48+
if (ret)
49+
_status = true;
50+
return ret;
3851
}
3952

4053
int arduino::EthernetClient::connect(IPAddress ip, uint16_t port) {
@@ -127,11 +140,12 @@ void arduino::EthernetClient::stop() {
127140
if (sock != NULL) {
128141
sock->close();
129142
sock = NULL;
143+
_status = false;
130144
}
131145
}
132146

133147
uint8_t arduino::EthernetClient::connected() {
134-
return _status != LinkOFF;
148+
return _status == true;
135149
}
136150

137151
IPAddress arduino::EthernetClient::remoteIP() {

libraries/Ethernet/src/EthernetClient.h

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,9 +57,7 @@ class EthernetClient : public arduino::Client {
5757
return sock != NULL;
5858
}
5959

60-
void setSocket(Socket* _sock) {
61-
sock = _sock;
62-
}
60+
void setSocket(Socket* _sock);
6361

6462
IPAddress remoteIP();
6563
uint16_t remotePort();
@@ -78,7 +76,7 @@ class EthernetClient : public arduino::Client {
7876
static uint16_t _srcport;
7977
Socket* sock;
8078
RingBufferN<256> rxBuffer;
81-
uint8_t _status;
79+
bool _status;
8280
mbed::Callback<int(void)> beforeConnect;
8381
SocketAddress address;
8482

0 commit comments

Comments
 (0)