mirror of
https://github.com/esp8266/Arduino.git
synced 2025-07-10 04:22:05 +03:00
Add ESP8266WiFi library
This commit is contained in:
140
libraries/ESP8266WiFi/src/WiFiServer.cpp
Normal file
140
libraries/ESP8266WiFi/src/WiFiServer.cpp
Normal file
@ -0,0 +1,140 @@
|
||||
/*
|
||||
WiFiServer.cpp - Library for Arduino Wifi shield.
|
||||
Copyright (c) 2011-2014 Arduino. All right reserved.
|
||||
|
||||
This library is free software; you can redistribute it and/or
|
||||
modify it under the terms of the GNU Lesser General Public
|
||||
License as published by the Free Software Foundation; either
|
||||
version 2.1 of the License, or (at your option) any later version.
|
||||
|
||||
This library is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
Lesser General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Lesser General Public
|
||||
License along with this library; if not, write to the Free Software
|
||||
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
||||
*/
|
||||
|
||||
#define LWIP_INTERNAL
|
||||
|
||||
#include "debug.h"
|
||||
#include "ESP8266WiFi.h"
|
||||
#include "WiFiClient.h"
|
||||
#include "WiFiServer.h"
|
||||
#include "lwip/opt.h"
|
||||
#include "lwip/tcp.h"
|
||||
#include "lwip/inet.h"
|
||||
#include "cbuf.h"
|
||||
#include "include/ClientContext.h"
|
||||
|
||||
WiFiServer::WiFiServer(uint16_t port, size_t rx_buffer_size)
|
||||
{
|
||||
_port = port;
|
||||
_pcb = 0;
|
||||
_unclaimed = 0;
|
||||
_discarded = 0;
|
||||
_rx_buffer_size = rx_buffer_size;
|
||||
}
|
||||
|
||||
void WiFiServer::begin()
|
||||
{
|
||||
err_t err;
|
||||
|
||||
tcp_pcb* pcb = tcp_new();
|
||||
if (!pcb)
|
||||
return;
|
||||
|
||||
err = tcp_bind(pcb, INADDR_ANY, _port);
|
||||
|
||||
if (err != ERR_OK)
|
||||
{
|
||||
tcp_close(pcb);
|
||||
return;
|
||||
}
|
||||
|
||||
tcp_pcb* listen_pcb = tcp_listen(pcb);
|
||||
if (!listen_pcb)
|
||||
{
|
||||
tcp_close(pcb);
|
||||
return;
|
||||
}
|
||||
_pcb = listen_pcb;
|
||||
tcp_accept(listen_pcb, &WiFiServer::_s_accept);
|
||||
tcp_arg(listen_pcb, (void*) this);
|
||||
}
|
||||
|
||||
|
||||
WiFiClient WiFiServer::available(byte* status)
|
||||
{
|
||||
if (_unclaimed)
|
||||
{
|
||||
WiFiClient result(_unclaimed);
|
||||
_unclaimed = _unclaimed->next();
|
||||
// printf("WiFiServer::available\r\n");
|
||||
DEBUGV("WS:av\r\n");
|
||||
return result;
|
||||
}
|
||||
|
||||
return WiFiClient();
|
||||
}
|
||||
|
||||
uint8_t WiFiServer::status() {
|
||||
if (!_pcb)
|
||||
return CLOSED;
|
||||
return _pcb->state;
|
||||
}
|
||||
|
||||
|
||||
size_t WiFiServer::write(uint8_t b)
|
||||
{
|
||||
return write(&b, 1);
|
||||
}
|
||||
|
||||
size_t WiFiServer::write(const uint8_t *buffer, size_t size)
|
||||
{
|
||||
// write to all clients
|
||||
// not implemented
|
||||
return 0;
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
T* slist_append_tail(T* head, T* item)
|
||||
{
|
||||
if (!head)
|
||||
return item;
|
||||
T* last = head;
|
||||
while(last->next())
|
||||
last = last->next();
|
||||
last->next(item);
|
||||
return head;
|
||||
}
|
||||
|
||||
int8_t WiFiServer::_accept(tcp_pcb* apcb, int8_t err)
|
||||
{
|
||||
DEBUGV("WS:ac\r\n");
|
||||
ClientContext* client = new ClientContext(apcb, _rx_buffer_size, &WiFiServer::_s_discard, this);
|
||||
_unclaimed = slist_append_tail(_unclaimed, client);
|
||||
tcp_accepted(_pcb);
|
||||
// printf("WiFiServer::_accept\r\n");
|
||||
return ERR_OK;
|
||||
}
|
||||
|
||||
void WiFiServer::_discard(ClientContext* client)
|
||||
{
|
||||
_discarded = slist_append_tail(_discarded, client);
|
||||
DEBUGV("WS:dis\r\n");
|
||||
// printf("WiFiServer::_discard\r\n");
|
||||
}
|
||||
|
||||
int8_t WiFiServer::_s_accept(void *arg, tcp_pcb* newpcb, int8_t err)
|
||||
{
|
||||
return reinterpret_cast<WiFiServer*>(arg)->_accept(newpcb, err);
|
||||
}
|
||||
|
||||
void WiFiServer::_s_discard(void* server, ClientContext* ctx)
|
||||
{
|
||||
reinterpret_cast<WiFiServer*>(server)->_discard(ctx);
|
||||
}
|
||||
|
Reference in New Issue
Block a user