Added WFI to save power in temporary polling implementation

pull/2216/head^2
Christopher Haster 2016-04-20 19:30:16 -05:00
parent f2715b7e77
commit 943dd711a9
5 changed files with 40 additions and 10 deletions

View File

@ -109,6 +109,10 @@ int Socket::getsockopt(int level, int optname, void *optval, unsigned *optlen)
} }
void Socket::wakeup()
{
}
void Socket::thunk(void *data) void Socket::thunk(void *data)
{ {
Socket *self = (Socket *)data; Socket *self = (Socket *)data;

View File

@ -169,6 +169,7 @@ protected:
int open(NetworkStack *iface, nsapi_protocol_t proto); int open(NetworkStack *iface, nsapi_protocol_t proto);
static void thunk(void *); static void thunk(void *);
static void wakeup();
NetworkStack *_iface; NetworkStack *_iface;
void *_socket; void *_socket;

View File

@ -44,6 +44,10 @@ int TCPServer::accept(TCPSocket *connection)
{ {
mbed::Timer timer; mbed::Timer timer;
timer.start(); timer.start();
mbed::Timeout timeout;
if (_timeout >= 0) {
timeout.attach_us(&Socket::wakeup, _timeout * 1000);
}
if (connection->_socket) { if (connection->_socket) {
connection->close(); connection->close();
@ -61,9 +65,10 @@ int TCPServer::accept(TCPSocket *connection)
} }
if (err != NSAPI_ERROR_WOULD_BLOCK if (err != NSAPI_ERROR_WOULD_BLOCK
|| _timeout < 0 || (_timeout >= 0 && timer.read_ms() >= _timeout)) {
|| timer.read_ms() > _timeout) {
return err; return err;
} }
__WFI();
} }
} }

View File

@ -54,6 +54,10 @@ int TCPSocket::send(const void *data, unsigned size)
{ {
mbed::Timer timer; mbed::Timer timer;
timer.start(); timer.start();
mbed::Timeout timeout;
if (_timeout >= 0) {
timeout.attach_us(&Socket::wakeup, _timeout * 1000);
}
while (true) { while (true) {
if (!_socket) { if (!_socket) {
@ -62,10 +66,11 @@ int TCPSocket::send(const void *data, unsigned size)
int sent = _iface->socket_send(_socket, data, size); int sent = _iface->socket_send(_socket, data, size);
if (sent != NSAPI_ERROR_WOULD_BLOCK if (sent != NSAPI_ERROR_WOULD_BLOCK
|| _timeout < 0 || (_timeout >= 0 && timer.read_ms() >= _timeout)) {
|| timer.read_ms() > _timeout) {
return sent; return sent;
} }
__WFI();
} }
} }
@ -73,6 +78,10 @@ int TCPSocket::recv(void *data, unsigned size)
{ {
mbed::Timer timer; mbed::Timer timer;
timer.start(); timer.start();
mbed::Timeout timeout;
if (_timeout >= 0) {
timeout.attach_us(&Socket::wakeup, _timeout * 1000);
}
while (true) { while (true) {
if (!_socket) { if (!_socket) {
@ -81,9 +90,10 @@ int TCPSocket::recv(void *data, unsigned size)
int recv = _iface->socket_recv(_socket, data, size); int recv = _iface->socket_recv(_socket, data, size);
if (recv != NSAPI_ERROR_WOULD_BLOCK if (recv != NSAPI_ERROR_WOULD_BLOCK
|| _timeout < 0 || (_timeout >= 0 && timer.read_ms() >= _timeout)) {
|| timer.read_ms() > _timeout) {
return recv; return recv;
} }
__WFI();
} }
} }

View File

@ -45,6 +45,10 @@ int UDPSocket::sendto(const SocketAddress &address, const void *data, unsigned s
{ {
mbed::Timer timer; mbed::Timer timer;
timer.start(); timer.start();
mbed::Timeout timeout;
if (_timeout >= 0) {
timeout.attach_us(&Socket::wakeup, _timeout * 1000);
}
while (true) { while (true) {
if (!_socket) { if (!_socket) {
@ -53,10 +57,11 @@ int UDPSocket::sendto(const SocketAddress &address, const void *data, unsigned s
int sent = _iface->socket_sendto(_socket, address, data, size); int sent = _iface->socket_sendto(_socket, address, data, size);
if (sent != NSAPI_ERROR_WOULD_BLOCK if (sent != NSAPI_ERROR_WOULD_BLOCK
|| _timeout < 0 || (_timeout >= 0 && timer.read_ms() >= _timeout)) {
|| timer.read_ms() > _timeout) {
return sent; return sent;
} }
__WFI();
} }
} }
@ -64,6 +69,10 @@ int UDPSocket::recvfrom(SocketAddress *address, void *buffer, unsigned size)
{ {
mbed::Timer timer; mbed::Timer timer;
timer.start(); timer.start();
mbed::Timeout timeout;
if (_timeout >= 0) {
timeout.attach_us(&Socket::wakeup, _timeout * 1000);
}
while (true) { while (true) {
if (!_socket) { if (!_socket) {
@ -72,9 +81,10 @@ int UDPSocket::recvfrom(SocketAddress *address, void *buffer, unsigned size)
int recv = _iface->socket_recvfrom(_socket, address, buffer, size); int recv = _iface->socket_recvfrom(_socket, address, buffer, size);
if (recv != NSAPI_ERROR_WOULD_BLOCK if (recv != NSAPI_ERROR_WOULD_BLOCK
|| _timeout < 0 || (_timeout >= 0 && timer.read_ms() >= _timeout)) {
|| timer.read_ms() > _timeout) {
return recv; return recv;
} }
__WFI();
} }
} }