diff options
author | Cristian Maglie <c.maglie@bug.st> | 2012-10-22 09:17:24 +0200 |
---|---|---|
committer | Cristian Maglie <c.maglie@bug.st> | 2012-10-22 09:17:24 +0200 |
commit | e03133b1b43fd445d3ed79a83d12786ef351cd35 (patch) | |
tree | b8c10307727d0cb1571b1eb139f2f98537e29995 /libraries/WiFi/WiFiServer.cpp | |
parent | 46d658fac872f7aa8856c792b1744b74f2d9946d (diff) |
Moved WiFi library in the proper place
Diffstat (limited to 'libraries/WiFi/WiFiServer.cpp')
-rw-r--r-- | libraries/WiFi/WiFiServer.cpp | 88 |
1 files changed, 88 insertions, 0 deletions
diff --git a/libraries/WiFi/WiFiServer.cpp b/libraries/WiFi/WiFiServer.cpp new file mode 100644 index 0000000..77dbac0 --- /dev/null +++ b/libraries/WiFi/WiFiServer.cpp @@ -0,0 +1,88 @@ +#include <string.h> +#include "server_drv.h" + +extern "C" { + #include "utility/debug.h" +} + +#include "WiFi.h" +#include "WiFiClient.h" +#include "WiFiServer.h" + +WiFiServer::WiFiServer(uint16_t port) +{ + _port = port; +} + +void WiFiServer::begin() +{ + uint8_t _sock = WiFiClass::getSocket(); + if (_sock != NO_SOCKET_AVAIL) + { + ServerDrv::startServer(_port, _sock); + WiFiClass::_server_port[_sock] = _port; + } +} + +WiFiClient WiFiServer::available(byte* status) +{ + static int cycle_server_down = 0; + const int TH_SERVER_DOWN = 50; + + for (int sock = 0; sock < MAX_SOCK_NUM; sock++) + { + if (WiFiClass::_server_port[sock] == _port) + { + WiFiClient client(sock); + uint8_t _status = client.status(); + uint8_t _ser_status = this->status(); + + if (status != NULL) + *status = _status; + + //server not in listen state, restart it + if ((_ser_status == 0)&&(cycle_server_down++ > TH_SERVER_DOWN)) + { + ServerDrv::startServer(_port, sock); + cycle_server_down = 0; + } + + if (_status == ESTABLISHED) + { + return client; //TODO + } + } + } + + return WiFiClient(255); +} + +uint8_t WiFiServer::status() { + return ServerDrv::getServerState(0); +} + + +size_t WiFiServer::write(uint8_t b) +{ + return write(&b, 1); +} + +size_t WiFiServer::write(const uint8_t *buffer, size_t size) +{ + size_t n = 0; + + for (int sock = 0; sock < MAX_SOCK_NUM; sock++) + { + if (WiFiClass::_server_port[sock] != 0) + { + WiFiClient client(sock); + + if (WiFiClass::_server_port[sock] == _port && + client.status() == ESTABLISHED) + { + n+=client.write(buffer, size); + } + } + } + return n; +} |