mirror of
https://github.com/esp8266/Arduino.git
synced 2025-07-26 07:02:15 +03:00
Add ESP8266WiFi library
This commit is contained in:
208
libraries/ESP8266WiFi/src/include/ClientContext.h
Normal file
208
libraries/ESP8266WiFi/src/include/ClientContext.h
Normal file
@ -0,0 +1,208 @@
|
||||
#ifndef CLIENTCONTEXT_H
|
||||
#define CLIENTCONTEXT_H
|
||||
|
||||
class ClientContext;
|
||||
class WiFiClient;
|
||||
|
||||
typedef void(*discard_cb_t)(void*, ClientContext*);
|
||||
|
||||
extern "C" void esp_yield();
|
||||
extern "C" void esp_schedule();
|
||||
|
||||
|
||||
class ClientContext
|
||||
{
|
||||
public:
|
||||
ClientContext(tcp_pcb* pcb, size_t rx_buffer_size,
|
||||
discard_cb_t discard_cb, void* discard_cb_arg)
|
||||
: _pcb(pcb)
|
||||
, _rx_buf(rx_buffer_size)
|
||||
, _discard_cb(discard_cb)
|
||||
, _discard_cb_arg(discard_cb_arg)
|
||||
, _refcnt(0)
|
||||
, _next(0)
|
||||
{
|
||||
tcp_setprio(pcb, TCP_PRIO_MIN);
|
||||
tcp_arg(pcb, this);
|
||||
tcp_recv(pcb, &_s_recv);
|
||||
tcp_sent(pcb, &_s_sent);
|
||||
tcp_err(pcb, &_s_error);
|
||||
// tcp_poll(pcb, &_s_poll, 0);
|
||||
}
|
||||
|
||||
~ClientContext()
|
||||
{
|
||||
}
|
||||
|
||||
ClientContext* next() const
|
||||
{
|
||||
return _next;
|
||||
}
|
||||
|
||||
ClientContext* next(ClientContext* new_next)
|
||||
{
|
||||
_next = new_next;
|
||||
}
|
||||
|
||||
void ref()
|
||||
{
|
||||
++_refcnt;
|
||||
}
|
||||
|
||||
void unref()
|
||||
{
|
||||
DEBUGV("WC:ur %d\r\n", _refcnt);
|
||||
if (--_refcnt == 0 && _pcb)
|
||||
{
|
||||
// if (_discard_cb)
|
||||
// (*_discard_cb)(_discard_cb_arg, this);
|
||||
// else
|
||||
// {
|
||||
tcp_arg(_pcb, NULL);
|
||||
tcp_sent(_pcb, NULL);
|
||||
tcp_recv(_pcb, NULL);
|
||||
tcp_err(_pcb, NULL);
|
||||
tcp_close(_pcb);
|
||||
_pcb = 0;
|
||||
delete this;
|
||||
//tcp_abort(_pcb);
|
||||
// }
|
||||
}
|
||||
}
|
||||
|
||||
size_t getSize() const
|
||||
{
|
||||
return _rx_buf.getSize();
|
||||
}
|
||||
|
||||
char read()
|
||||
{
|
||||
return _rx_buf.read();
|
||||
}
|
||||
|
||||
size_t read(char* dst, size_t size)
|
||||
{
|
||||
DEBUGV("WC:rd\r\n");
|
||||
return _rx_buf.read(dst, size);
|
||||
}
|
||||
|
||||
char peek()
|
||||
{
|
||||
return _rx_buf.peek();
|
||||
}
|
||||
|
||||
void flush()
|
||||
{
|
||||
return _rx_buf.flush();
|
||||
}
|
||||
|
||||
uint8_t state() const
|
||||
{
|
||||
if (!_pcb)
|
||||
return CLOSED;
|
||||
|
||||
return _pcb->state;
|
||||
}
|
||||
|
||||
size_t write(const char* data, size_t size)
|
||||
{
|
||||
if (!_pcb)
|
||||
return 0;
|
||||
|
||||
size_t room = tcp_sndbuf(_pcb);
|
||||
size_t will_send = (room < size) ? room : size;
|
||||
err_t err = tcp_write(_pcb, data, will_send, 0);
|
||||
if (err != ERR_OK)
|
||||
return 0;
|
||||
|
||||
_size_sent = will_send;
|
||||
DEBUGV("WC:wr\r\n");
|
||||
delay(5000); // max send timeout
|
||||
DEBUGV("WC:ww\r\n");
|
||||
return will_send - _size_sent;
|
||||
}
|
||||
|
||||
|
||||
private:
|
||||
|
||||
err_t _recv(tcp_pcb* pcb, pbuf* pb, err_t err)
|
||||
{
|
||||
|
||||
if (pb == 0) // connection closed
|
||||
{
|
||||
DEBUGV("WC:rcl\r\n");
|
||||
tcp_arg(pcb, NULL);
|
||||
tcp_sent(pcb, NULL);
|
||||
tcp_recv(pcb, NULL);
|
||||
tcp_err(pcb, NULL);
|
||||
tcp_close(pcb);
|
||||
_pcb = 0;
|
||||
return ERR_OK;
|
||||
}
|
||||
|
||||
DEBUGV("WC:rcr\r\n");
|
||||
size_t len = pb->len;
|
||||
_rx_buf.write(reinterpret_cast<const char*>(pb->payload), pb->len);
|
||||
pbuf_free(pb);
|
||||
|
||||
tcp_recved(pcb, len);
|
||||
return ERR_OK;
|
||||
}
|
||||
|
||||
void _error(err_t err)
|
||||
{
|
||||
DEBUGV("WC:er\r\n");
|
||||
_pcb = 0;
|
||||
if (_size_sent)
|
||||
esp_schedule();
|
||||
|
||||
}
|
||||
|
||||
err_t _poll(tcp_pcb* pcb)
|
||||
{
|
||||
return ERR_OK;
|
||||
}
|
||||
|
||||
err_t _sent(tcp_pcb* pcb, uint16_t len)
|
||||
{
|
||||
DEBUGV("WC:sent %d\r\n", len);
|
||||
_size_sent -= len;
|
||||
if (_size_sent == 0)
|
||||
esp_schedule();
|
||||
return ERR_OK;
|
||||
}
|
||||
|
||||
|
||||
static err_t _s_recv(void *arg, struct tcp_pcb *tpcb, struct pbuf *pb, err_t err)
|
||||
{
|
||||
return reinterpret_cast<ClientContext*>(arg)->_recv(tpcb, pb, err);
|
||||
}
|
||||
|
||||
static void _s_error(void *arg, err_t err)
|
||||
{
|
||||
reinterpret_cast<ClientContext*>(arg)->_error(err);
|
||||
}
|
||||
|
||||
static err_t _s_poll(void *arg, struct tcp_pcb *tpcb)
|
||||
{
|
||||
return reinterpret_cast<ClientContext*>(arg)->_poll(tpcb);
|
||||
}
|
||||
|
||||
static err_t _s_sent(void *arg, struct tcp_pcb *tpcb, uint16_t len)
|
||||
{
|
||||
return reinterpret_cast<ClientContext*>(arg)->_sent(tpcb, len);
|
||||
}
|
||||
|
||||
private:
|
||||
ClientContext* _next;
|
||||
int _refcnt;
|
||||
tcp_pcb* _pcb;
|
||||
cbuf _rx_buf;
|
||||
discard_cb_t _discard_cb;
|
||||
void* _discard_cb_arg;
|
||||
size_t _size_sent;
|
||||
};
|
||||
|
||||
|
||||
|
||||
#endif//CLIENTCONTEXT_H
|
2034
libraries/ESP8266WiFi/src/include/lwipopts.h
Normal file
2034
libraries/ESP8266WiFi/src/include/lwipopts.h
Normal file
File diff suppressed because it is too large
Load Diff
87
libraries/ESP8266WiFi/src/include/wl_definitions.h
Normal file
87
libraries/ESP8266WiFi/src/include/wl_definitions.h
Normal file
@ -0,0 +1,87 @@
|
||||
/*
|
||||
wl_definitions.h - 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
|
||||
*/
|
||||
/*
|
||||
* wl_definitions.h
|
||||
*
|
||||
* Created on: Mar 6, 2011
|
||||
* Author: dlafauci
|
||||
*/
|
||||
|
||||
#ifndef WL_DEFINITIONS_H_
|
||||
#define WL_DEFINITIONS_H_
|
||||
|
||||
// Maximum size of a SSID
|
||||
#define WL_SSID_MAX_LENGTH 32
|
||||
// Length of passphrase. Valid lengths are 8-63.
|
||||
#define WL_WPA_KEY_MAX_LENGTH 63
|
||||
// Length of key in bytes. Valid values are 5 and 13.
|
||||
#define WL_WEP_KEY_MAX_LENGTH 13
|
||||
// Size of a MAC-address or BSSID
|
||||
#define WL_MAC_ADDR_LENGTH 6
|
||||
// Size of a MAC-address or BSSID
|
||||
#define WL_IPV4_LENGTH 4
|
||||
// Maximum size of a SSID list
|
||||
#define WL_NETWORKS_LIST_MAXNUM 10
|
||||
// Maxmium number of socket
|
||||
#define MAX_SOCK_NUM 4
|
||||
// Socket not available constant
|
||||
#define SOCK_NOT_AVAIL 255
|
||||
// Default state value for Wifi state field
|
||||
#define NA_STATE -1
|
||||
//Maximum number of attempts to establish wifi connection
|
||||
#define WL_MAX_ATTEMPT_CONNECTION 10
|
||||
|
||||
typedef enum {
|
||||
WL_NO_SHIELD = 255,
|
||||
WL_IDLE_STATUS = 0,
|
||||
WL_NO_SSID_AVAIL,
|
||||
WL_SCAN_COMPLETED,
|
||||
WL_CONNECTED,
|
||||
WL_CONNECT_FAILED,
|
||||
WL_CONNECTION_LOST,
|
||||
WL_DISCONNECTED
|
||||
} wl_status_t;
|
||||
|
||||
/* Encryption modes */
|
||||
enum wl_enc_type { /* Values map to 802.11 encryption suites... */
|
||||
ENC_TYPE_WEP = 5,
|
||||
ENC_TYPE_TKIP = 2,
|
||||
ENC_TYPE_CCMP = 4,
|
||||
/* ... except these two, 7 and 8 are reserved in 802.11-2007 */
|
||||
ENC_TYPE_NONE = 7,
|
||||
ENC_TYPE_AUTO = 8
|
||||
};
|
||||
|
||||
#if !defined(LWIP_INTERNAL)
|
||||
enum wl_tcp_state {
|
||||
CLOSED = 0,
|
||||
LISTEN = 1,
|
||||
SYN_SENT = 2,
|
||||
SYN_RCVD = 3,
|
||||
ESTABLISHED = 4,
|
||||
FIN_WAIT_1 = 5,
|
||||
FIN_WAIT_2 = 6,
|
||||
CLOSE_WAIT = 7,
|
||||
CLOSING = 8,
|
||||
LAST_ACK = 9,
|
||||
TIME_WAIT = 10
|
||||
};
|
||||
#endif
|
||||
|
||||
#endif /* WL_DEFINITIONS_H_ */
|
Reference in New Issue
Block a user