2011-02-28 13:49:08 +01:00
|
|
|
extern "C" {
|
2011-03-08 22:13:54 +01:00
|
|
|
#include "utility/wl_definitions.h"
|
|
|
|
#include "utility/wl_types.h"
|
2011-02-28 13:49:08 +01:00
|
|
|
#include "socket.h"
|
|
|
|
#include "string.h"
|
2011-05-18 08:39:47 +02:00
|
|
|
#include "utility/debug.h"
|
2011-02-28 13:49:08 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#include "WiFi.h"
|
2011-08-31 10:34:51 +02:00
|
|
|
#include "WiFiClient.h"
|
|
|
|
#include "WiFiServer.h"
|
2011-02-28 13:49:08 +01:00
|
|
|
#include "server_drv.h"
|
|
|
|
|
2012-02-04 12:29:12 +01:00
|
|
|
|
2011-08-31 10:34:51 +02:00
|
|
|
uint16_t WiFiClient::_srcport = 1024;
|
2011-02-28 13:49:08 +01:00
|
|
|
|
2011-08-31 10:34:51 +02:00
|
|
|
WiFiClient::WiFiClient() : _sock(MAX_SOCK_NUM) {
|
2011-05-18 15:37:29 +02:00
|
|
|
}
|
|
|
|
|
2011-08-31 10:34:51 +02:00
|
|
|
WiFiClient::WiFiClient(uint8_t sock) : _sock(sock) {
|
2011-02-28 13:49:08 +01:00
|
|
|
}
|
|
|
|
|
2011-08-31 10:34:51 +02:00
|
|
|
int WiFiClient::connect(const char* host, uint16_t port) {
|
2011-05-18 15:37:29 +02:00
|
|
|
/* TODO Add DNS wifi spi function to resolve DNS */
|
|
|
|
#if 0
|
|
|
|
// Look up the host first
|
|
|
|
int ret = 0;
|
|
|
|
DNSClient dns;
|
|
|
|
IPAddress remote_addr;
|
|
|
|
|
|
|
|
dns.begin(Ethernet.dnsServerIP());
|
|
|
|
ret = dns.getHostByName(host, remote_addr);
|
|
|
|
if (ret == 1) {
|
|
|
|
return connect(remote_addr, port);
|
|
|
|
} else {
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
#endif
|
2012-02-04 17:01:24 +01:00
|
|
|
return 0;
|
2011-02-28 13:49:08 +01:00
|
|
|
}
|
|
|
|
|
2011-08-31 10:34:51 +02:00
|
|
|
int WiFiClient::connect(IPAddress ip, uint16_t port) {
|
2011-03-08 22:13:54 +01:00
|
|
|
_sock = getFirstSocket();
|
2011-04-24 00:45:47 +02:00
|
|
|
if (_sock != NO_SOCKET_AVAIL)
|
2011-02-28 13:49:08 +01:00
|
|
|
{
|
2012-02-04 12:29:12 +01:00
|
|
|
ServerDrv::startClient(uint32_t(ip), port, _sock);
|
2011-05-18 15:37:29 +02:00
|
|
|
WiFiClass::_state[_sock] = _sock;
|
2011-02-28 13:49:08 +01:00
|
|
|
}else{
|
2011-04-24 00:45:47 +02:00
|
|
|
return 0;
|
2011-02-28 13:49:08 +01:00
|
|
|
}
|
2011-04-24 00:45:47 +02:00
|
|
|
return 1;
|
2011-02-28 13:49:08 +01:00
|
|
|
}
|
|
|
|
|
2011-08-31 10:34:51 +02:00
|
|
|
size_t WiFiClient::write(uint8_t b) {
|
2012-02-10 00:49:42 +01:00
|
|
|
return write(&b, 1);
|
2011-02-28 13:49:08 +01:00
|
|
|
}
|
|
|
|
|
2011-08-31 10:34:51 +02:00
|
|
|
size_t WiFiClient::write(const uint8_t *buf, size_t size) {
|
2012-02-10 00:49:42 +01:00
|
|
|
if (_sock >= MAX_SOCK_NUM)
|
2011-02-28 13:49:08 +01:00
|
|
|
{
|
2012-02-10 00:49:42 +01:00
|
|
|
setWriteError();
|
|
|
|
return 0;
|
2011-02-28 13:49:08 +01:00
|
|
|
}
|
2012-02-10 00:49:42 +01:00
|
|
|
if (size==0)
|
|
|
|
{
|
|
|
|
setWriteError();
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if ((!ServerDrv::sendData(_sock, buf, size)) ||
|
|
|
|
(!ServerDrv::checkDataSent(_sock)))
|
|
|
|
{
|
|
|
|
setWriteError();
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return size;
|
2011-02-28 13:49:08 +01:00
|
|
|
}
|
|
|
|
|
2011-08-31 10:34:51 +02:00
|
|
|
int WiFiClient::available() {
|
2011-02-28 13:49:08 +01:00
|
|
|
if (_sock != 255)
|
|
|
|
{
|
|
|
|
return ServerDrv::availData(_sock);
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2011-08-31 10:34:51 +02:00
|
|
|
int WiFiClient::read() {
|
2011-02-28 13:49:08 +01:00
|
|
|
uint8_t b;
|
|
|
|
if (!available())
|
|
|
|
return -1;
|
2012-02-10 00:49:42 +01:00
|
|
|
|
2011-02-28 13:49:08 +01:00
|
|
|
ServerDrv::getData(_sock, &b);
|
|
|
|
return b;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-08-31 10:34:51 +02:00
|
|
|
int WiFiClient::read(uint8_t* buf, size_t size) {
|
2011-03-08 22:13:54 +01:00
|
|
|
if (!ServerDrv::getDataBuf(_sock, buf, &size))
|
2011-02-28 13:49:08 +01:00
|
|
|
return -1;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2011-08-31 10:34:51 +02:00
|
|
|
int WiFiClient::peek() {
|
2011-05-18 15:37:29 +02:00
|
|
|
//TODO to be implemented
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2011-08-31 10:34:51 +02:00
|
|
|
void WiFiClient::flush() {
|
2011-02-28 13:49:08 +01:00
|
|
|
while (available())
|
|
|
|
read();
|
|
|
|
}
|
|
|
|
|
2011-08-31 10:34:51 +02:00
|
|
|
void WiFiClient::stop() {
|
2012-02-04 12:29:12 +01:00
|
|
|
|
2011-02-28 13:49:08 +01:00
|
|
|
if (_sock == 255)
|
|
|
|
return;
|
2012-02-04 12:29:12 +01:00
|
|
|
|
|
|
|
ServerDrv::stopClient(_sock);
|
|
|
|
|
2011-02-28 13:49:08 +01:00
|
|
|
unsigned long start = millis();
|
|
|
|
|
|
|
|
// wait a second for the connection to close
|
|
|
|
while (status() != CLOSED && millis() - start < 1000)
|
|
|
|
delay(1);
|
|
|
|
|
|
|
|
_sock = 255;
|
|
|
|
}
|
|
|
|
|
2011-08-31 10:34:51 +02:00
|
|
|
uint8_t WiFiClient::connected() {
|
2012-02-10 00:49:42 +01:00
|
|
|
|
2011-02-28 13:49:08 +01:00
|
|
|
if (_sock == 255) {
|
|
|
|
return 0;
|
|
|
|
} else {
|
|
|
|
uint8_t s = status();
|
2012-02-10 00:49:42 +01:00
|
|
|
|
2011-02-28 13:49:08 +01:00
|
|
|
return !(s == LISTEN || s == CLOSED || s == FIN_WAIT_1 || s == FIN_WAIT_2 ||
|
|
|
|
(s == CLOSE_WAIT && !available()));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-08-31 10:34:51 +02:00
|
|
|
uint8_t WiFiClient::status() {
|
2011-02-28 13:49:08 +01:00
|
|
|
if (_sock == 255) {
|
|
|
|
return CLOSED;
|
|
|
|
} else {
|
2012-02-04 12:29:12 +01:00
|
|
|
return ServerDrv::getClientState(_sock);
|
2011-02-28 13:49:08 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-08-31 10:34:51 +02:00
|
|
|
WiFiClient::operator bool() {
|
2011-02-28 13:49:08 +01:00
|
|
|
return _sock != 255;
|
|
|
|
}
|
|
|
|
|
2011-03-08 22:13:54 +01:00
|
|
|
// Private Methods
|
2011-08-31 10:34:51 +02:00
|
|
|
uint8_t WiFiClient::getFirstSocket()
|
2011-03-08 22:13:54 +01:00
|
|
|
{
|
|
|
|
for (int i = 0; i < MAX_SOCK_NUM; i++) {
|
2011-04-24 00:45:47 +02:00
|
|
|
if (WiFiClass::_state[i] == 0)
|
2011-03-08 22:13:54 +01:00
|
|
|
{
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return SOCK_NOT_AVAIL;
|
2011-02-28 13:49:08 +01:00
|
|
|
}
|
2011-03-08 22:13:54 +01:00
|
|
|
|