Skip to content

Commit 943dd71

Browse files
committed
Added WFI to save power in temporary polling implementation
1 parent f2715b7 commit 943dd71

File tree

5 files changed

+40
-10
lines changed

5 files changed

+40
-10
lines changed

Socket.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,10 @@ int Socket::getsockopt(int level, int optname, void *optval, unsigned *optlen)
109109

110110
}
111111

112+
void Socket::wakeup()
113+
{
114+
}
115+
112116
void Socket::thunk(void *data)
113117
{
114118
Socket *self = (Socket *)data;

Socket.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,7 @@ class Socket {
169169
int open(NetworkStack *iface, nsapi_protocol_t proto);
170170

171171
static void thunk(void *);
172+
static void wakeup();
172173

173174
NetworkStack *_iface;
174175
void *_socket;

TCPServer.cpp

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,10 @@ int TCPServer::accept(TCPSocket *connection)
4444
{
4545
mbed::Timer timer;
4646
timer.start();
47+
mbed::Timeout timeout;
48+
if (_timeout >= 0) {
49+
timeout.attach_us(&Socket::wakeup, _timeout * 1000);
50+
}
4751

4852
if (connection->_socket) {
4953
connection->close();
@@ -61,9 +65,10 @@ int TCPServer::accept(TCPSocket *connection)
6165
}
6266

6367
if (err != NSAPI_ERROR_WOULD_BLOCK
64-
|| _timeout < 0
65-
|| timer.read_ms() > _timeout) {
68+
|| (_timeout >= 0 && timer.read_ms() >= _timeout)) {
6669
return err;
6770
}
71+
72+
__WFI();
6873
}
6974
}

TCPSocket.cpp

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,10 @@ int TCPSocket::send(const void *data, unsigned size)
5454
{
5555
mbed::Timer timer;
5656
timer.start();
57+
mbed::Timeout timeout;
58+
if (_timeout >= 0) {
59+
timeout.attach_us(&Socket::wakeup, _timeout * 1000);
60+
}
5761

5862
while (true) {
5963
if (!_socket) {
@@ -62,17 +66,22 @@ int TCPSocket::send(const void *data, unsigned size)
6266

6367
int sent = _iface->socket_send(_socket, data, size);
6468
if (sent != NSAPI_ERROR_WOULD_BLOCK
65-
|| _timeout < 0
66-
|| timer.read_ms() > _timeout) {
69+
|| (_timeout >= 0 && timer.read_ms() >= _timeout)) {
6770
return sent;
6871
}
72+
73+
__WFI();
6974
}
7075
}
7176

7277
int TCPSocket::recv(void *data, unsigned size)
7378
{
7479
mbed::Timer timer;
7580
timer.start();
81+
mbed::Timeout timeout;
82+
if (_timeout >= 0) {
83+
timeout.attach_us(&Socket::wakeup, _timeout * 1000);
84+
}
7685

7786
while (true) {
7887
if (!_socket) {
@@ -81,9 +90,10 @@ int TCPSocket::recv(void *data, unsigned size)
8190

8291
int recv = _iface->socket_recv(_socket, data, size);
8392
if (recv != NSAPI_ERROR_WOULD_BLOCK
84-
|| _timeout < 0
85-
|| timer.read_ms() > _timeout) {
93+
|| (_timeout >= 0 && timer.read_ms() >= _timeout)) {
8694
return recv;
8795
}
96+
97+
__WFI();
8898
}
8999
}

UDPSocket.cpp

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,10 @@ int UDPSocket::sendto(const SocketAddress &address, const void *data, unsigned s
4545
{
4646
mbed::Timer timer;
4747
timer.start();
48+
mbed::Timeout timeout;
49+
if (_timeout >= 0) {
50+
timeout.attach_us(&Socket::wakeup, _timeout * 1000);
51+
}
4852

4953
while (true) {
5054
if (!_socket) {
@@ -53,17 +57,22 @@ int UDPSocket::sendto(const SocketAddress &address, const void *data, unsigned s
5357

5458
int sent = _iface->socket_sendto(_socket, address, data, size);
5559
if (sent != NSAPI_ERROR_WOULD_BLOCK
56-
|| _timeout < 0
57-
|| timer.read_ms() > _timeout) {
60+
|| (_timeout >= 0 && timer.read_ms() >= _timeout)) {
5861
return sent;
5962
}
63+
64+
__WFI();
6065
}
6166
}
6267

6368
int UDPSocket::recvfrom(SocketAddress *address, void *buffer, unsigned size)
6469
{
6570
mbed::Timer timer;
6671
timer.start();
72+
mbed::Timeout timeout;
73+
if (_timeout >= 0) {
74+
timeout.attach_us(&Socket::wakeup, _timeout * 1000);
75+
}
6776

6877
while (true) {
6978
if (!_socket) {
@@ -72,9 +81,10 @@ int UDPSocket::recvfrom(SocketAddress *address, void *buffer, unsigned size)
7281

7382
int recv = _iface->socket_recvfrom(_socket, address, buffer, size);
7483
if (recv != NSAPI_ERROR_WOULD_BLOCK
75-
|| _timeout < 0
76-
|| timer.read_ms() > _timeout) {
84+
|| (_timeout >= 0 && timer.read_ms() >= _timeout)) {
7785
return recv;
7886
}
87+
88+
__WFI();
7989
}
8090
}

0 commit comments

Comments
 (0)