1
0
mirror of https://github.com/arduino/Arduino.git synced 2024-12-01 12:24:14 +01:00
Arduino/hardware/arduino/avr/libraries/WiFi/WiFiServer.cpp

90 lines
1.9 KiB
C++
Raw Normal View History

2011-02-28 13:49:08 +01:00
#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)
2011-02-28 13:49:08 +01:00
{
_port = port;
}
void WiFiServer::begin()
2011-02-28 13:49:08 +01:00
{
uint8_t _sock = WiFiClass::getSocket();
if (_sock != NO_SOCKET_AVAIL)
{
ServerDrv::startServer(_port, _sock);
2011-02-28 13:49:08 +01:00
WiFiClass::_server_port[_sock] = _port;
2013-02-08 19:22:23 +01:00
WiFiClass::_state[_sock] = _sock;
2011-02-28 13:49:08 +01:00
}
}
WiFiClient WiFiServer::available(byte* status)
2011-02-28 13:49:08 +01:00
{
static int cycle_server_down = 0;
const int TH_SERVER_DOWN = 50;
2011-02-28 13:49:08 +01:00
for (int sock = 0; sock < MAX_SOCK_NUM; sock++)
{
if (WiFiClass::_server_port[sock] == _port)
2011-02-28 13:49:08 +01:00
{
WiFiClient client(sock);
uint8_t _status = client.status();
uint8_t _ser_status = this->status();
2011-04-21 19:16:39 +02:00
if (status != NULL)
*status = _status;
2011-02-28 13:49:08 +01:00
//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)
2011-02-28 13:49:08 +01:00
{
return client; //TODO
}
}
}
return WiFiClient(255);
2011-02-28 13:49:08 +01:00
}
uint8_t WiFiServer::status() {
return ServerDrv::getServerState(0);
}
size_t WiFiServer::write(uint8_t b)
2011-02-28 13:49:08 +01:00
{
return write(&b, 1);
2011-02-28 13:49:08 +01:00
}
size_t WiFiServer::write(const uint8_t *buffer, size_t size)
2011-02-28 13:49:08 +01:00
{
size_t n = 0;
2011-02-28 13:49:08 +01:00
for (int sock = 0; sock < MAX_SOCK_NUM; sock++)
{
if (WiFiClass::_server_port[sock] != 0)
{
WiFiClient client(sock);
2011-02-28 13:49:08 +01:00
if (WiFiClass::_server_port[sock] == _port &&
client.status() == ESTABLISHED)
{
n+=client.write(buffer, size);
2011-02-28 13:49:08 +01:00
}
}
}
return n;
2011-02-28 13:49:08 +01:00
}