mirror of
https://github.com/esp8266/Arduino.git
synced 2025-06-17 22:23:10 +03:00
Adding SPI library and revising Ethernet library (Christian Maglie).
This commit is contained in:
@ -1,81 +1,77 @@
|
||||
#include "w5100.h"
|
||||
#include "socket.h"
|
||||
extern "C" {
|
||||
#include "types.h"
|
||||
#include "w5100.h"
|
||||
#include "socket.h"
|
||||
#include "string.h"
|
||||
#include "string.h"
|
||||
}
|
||||
|
||||
#include "WProgram.h"
|
||||
|
||||
#include "Ethernet.h"
|
||||
#include "Client.h"
|
||||
#include "Server.h"
|
||||
#include "ethernet.h"
|
||||
#include "client.h"
|
||||
#include "server.h"
|
||||
|
||||
uint16_t Client::_srcport = 0;
|
||||
uint16_t Client::_srcport = 1024;
|
||||
|
||||
Client::Client(uint8_t sock) {
|
||||
_sock = sock;
|
||||
Client::Client() : _connected(false) {
|
||||
}
|
||||
|
||||
Client::Client(uint8_t *ip, uint16_t port) {
|
||||
_ip = ip;
|
||||
_port = port;
|
||||
_sock = 255;
|
||||
Client::Client(uint8_t sock) : _connected(true), _sock(sock) {
|
||||
}
|
||||
|
||||
Client::Client(uint8_t *ip, uint16_t port) : _ip(ip), _port(port), _connected(false) {
|
||||
}
|
||||
|
||||
uint8_t Client::connect() {
|
||||
if (_sock != 255)
|
||||
if (_connected)
|
||||
return 0;
|
||||
|
||||
for (int i = 0; i < MAX_SOCK_NUM; i++) {
|
||||
uint8_t s = getSn_SR(i);
|
||||
if (s == SOCK_CLOSED || s == SOCK_FIN_WAIT) {
|
||||
|
||||
int i;
|
||||
for (i=0; i<MAX_SOCK_NUM; i++) {
|
||||
uint8_t s = W5100.readSnSR(i);
|
||||
if (s == SnSR::CLOSED || s == SnSR::FIN_WAIT) {
|
||||
_sock = i;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (_sock == 255)
|
||||
|
||||
if (i == MAX_SOCK_NUM)
|
||||
return 0;
|
||||
|
||||
|
||||
_srcport++;
|
||||
if (_srcport + 1024 == 0) _srcport = 0;
|
||||
socket(_sock, Sn_MR_TCP, _srcport + 1024, 0);
|
||||
|
||||
if (!::connect(_sock, _ip, _port)) {
|
||||
_sock = 255;
|
||||
if (_srcport == 0) _srcport = 1024;
|
||||
socket(_sock, SnMR::TCP, _srcport, 0);
|
||||
|
||||
if (!::connect(_sock, _ip, _port))
|
||||
return 0;
|
||||
}
|
||||
|
||||
while (status() != SOCK_ESTABLISHED) {
|
||||
|
||||
while (status() != SnSR::ESTABLISHED) {
|
||||
delay(1);
|
||||
if (status() == SOCK_CLOSED) {
|
||||
_sock = 255;
|
||||
if (status() == SnSR::CLOSED)
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
_connected = true;
|
||||
return 1;
|
||||
}
|
||||
|
||||
void Client::write(uint8_t b) {
|
||||
if (_sock != 255)
|
||||
if (_connected)
|
||||
send(_sock, &b, 1);
|
||||
}
|
||||
|
||||
void Client::write(const char *str) {
|
||||
if (_sock != 255)
|
||||
if (_connected)
|
||||
send(_sock, (const uint8_t *)str, strlen(str));
|
||||
}
|
||||
|
||||
void Client::write(const uint8_t *buf, size_t size) {
|
||||
if (_sock != 255)
|
||||
if (_connected)
|
||||
send(_sock, buf, size);
|
||||
}
|
||||
|
||||
int Client::available() {
|
||||
if (_sock != 255)
|
||||
return getSn_RX_RSR(_sock);
|
||||
if (_connected)
|
||||
return W5100.getRXReceivedSize(_sock);
|
||||
return 0;
|
||||
}
|
||||
|
||||
@ -93,41 +89,33 @@ void Client::flush() {
|
||||
}
|
||||
|
||||
void Client::stop() {
|
||||
if (_sock == 255)
|
||||
if (!_connected)
|
||||
return;
|
||||
|
||||
|
||||
// attempt to close the connection gracefully (send a FIN to other side)
|
||||
disconnect(_sock);
|
||||
unsigned long start = millis();
|
||||
|
||||
|
||||
// wait a second for the connection to close
|
||||
while (status() != SOCK_CLOSED && millis() - start < 1000)
|
||||
while (status() != SnSR::CLOSED && millis() - start < 1000)
|
||||
delay(1);
|
||||
|
||||
|
||||
// if it hasn't closed, close it forcefully
|
||||
if (status() != SOCK_CLOSED)
|
||||
if (status() != SnSR::CLOSED)
|
||||
close(_sock);
|
||||
|
||||
|
||||
EthernetClass::_server_port[_sock] = 0;
|
||||
_sock = 255;
|
||||
_connected = false;
|
||||
}
|
||||
|
||||
uint8_t Client::connected() {
|
||||
if (_sock == 255) {
|
||||
return 0;
|
||||
} else {
|
||||
uint8_t s = status();
|
||||
return !(s == SOCK_LISTEN || s == SOCK_CLOSED || s == SOCK_FIN_WAIT ||
|
||||
(s == SOCK_CLOSE_WAIT && !available()));
|
||||
}
|
||||
uint8_t s = status();
|
||||
return !(s == SnSR::LISTEN || s == SnSR::CLOSED || s == SnSR::FIN_WAIT ||
|
||||
(s == SnSR::CLOSE_WAIT && !available()));
|
||||
}
|
||||
|
||||
uint8_t Client::status() {
|
||||
if (_sock == 255) {
|
||||
return SOCK_CLOSED;
|
||||
} else {
|
||||
return getSn_SR(_sock);
|
||||
}
|
||||
return W5100.readSnSR(_sock);
|
||||
}
|
||||
|
||||
// the next three functions are a hack so we can compare the client returned
|
||||
@ -136,13 +124,13 @@ uint8_t Client::status() {
|
||||
// library.
|
||||
|
||||
uint8_t Client::operator==(int p) {
|
||||
return _sock == 255;
|
||||
return !_connected;
|
||||
}
|
||||
|
||||
uint8_t Client::operator!=(int p) {
|
||||
return _sock != 255;
|
||||
return _connected;
|
||||
}
|
||||
|
||||
Client::operator bool() {
|
||||
return _sock != 255;
|
||||
return _connected;
|
||||
}
|
||||
|
Reference in New Issue
Block a user