2016-10-06 13:21:30 +02:00
|
|
|
/*
|
|
|
|
Client.h - Client class for Raspberry Pi
|
|
|
|
Copyright (c) 2016 Hristo Gochkov 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
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "WiFiClient.h"
|
|
|
|
#include <lwip/sockets.h>
|
|
|
|
#include <lwip/netdb.h>
|
|
|
|
#include <errno.h>
|
|
|
|
|
2017-02-28 23:37:00 +01:00
|
|
|
#define WIFI_CLIENT_MAX_WRITE_RETRY (10)
|
|
|
|
#define WIFI_CLIENT_SELECT_TIMEOUT_US (100000)
|
2017-03-02 01:49:53 +01:00
|
|
|
#define WIFI_CLIENT_FLUSH_BUFFER_SIZE (1024)
|
2017-02-28 23:37:00 +01:00
|
|
|
|
2016-10-06 13:21:30 +02:00
|
|
|
#undef connect
|
|
|
|
#undef write
|
|
|
|
#undef read
|
|
|
|
|
2017-03-01 23:47:16 +01:00
|
|
|
class WiFiClientSocketHandle {
|
|
|
|
private:
|
|
|
|
int sockfd;
|
|
|
|
|
|
|
|
public:
|
|
|
|
WiFiClientSocketHandle(int fd):sockfd(fd)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
~WiFiClientSocketHandle()
|
|
|
|
{
|
|
|
|
close(sockfd);
|
|
|
|
}
|
|
|
|
|
|
|
|
int fd()
|
|
|
|
{
|
|
|
|
return sockfd;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
WiFiClient::WiFiClient():_connected(false),next(NULL)
|
2016-10-06 13:21:30 +02:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2017-03-01 23:47:16 +01:00
|
|
|
WiFiClient::WiFiClient(int fd):_connected(true),next(NULL)
|
2016-10-06 13:21:30 +02:00
|
|
|
{
|
2017-03-01 23:47:16 +01:00
|
|
|
clientSocketHandle.reset(new WiFiClientSocketHandle(fd));
|
2016-10-06 13:21:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
WiFiClient::~WiFiClient()
|
|
|
|
{
|
2016-11-29 02:27:01 +01:00
|
|
|
stop();
|
2016-10-06 13:21:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
WiFiClient & WiFiClient::operator=(const WiFiClient &other)
|
|
|
|
{
|
|
|
|
stop();
|
2017-03-01 23:47:16 +01:00
|
|
|
clientSocketHandle = other.clientSocketHandle;
|
2016-10-06 13:21:30 +02:00
|
|
|
_connected = other._connected;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
void WiFiClient::stop()
|
|
|
|
{
|
2017-03-01 23:47:16 +01:00
|
|
|
clientSocketHandle = NULL;
|
|
|
|
_connected = false;
|
2016-10-06 13:21:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
int WiFiClient::connect(IPAddress ip, uint16_t port)
|
|
|
|
{
|
2017-03-01 23:47:16 +01:00
|
|
|
int sockfd = socket(AF_INET, SOCK_STREAM, 0);
|
2016-10-06 13:21:30 +02:00
|
|
|
if (sockfd < 0) {
|
2016-11-13 16:30:21 +01:00
|
|
|
log_e("socket: %d", errno);
|
2016-10-06 13:21:30 +02:00
|
|
|
return 0;
|
|
|
|
}
|
2017-03-01 23:47:16 +01:00
|
|
|
|
2016-10-06 13:21:30 +02:00
|
|
|
uint32_t ip_addr = ip;
|
|
|
|
struct sockaddr_in serveraddr;
|
|
|
|
bzero((char *) &serveraddr, sizeof(serveraddr));
|
|
|
|
serveraddr.sin_family = AF_INET;
|
|
|
|
bcopy((const void *)(&ip_addr), (void *)&serveraddr.sin_addr.s_addr, 4);
|
|
|
|
serveraddr.sin_port = htons(port);
|
|
|
|
int res = lwip_connect_r(sockfd, (struct sockaddr*)&serveraddr, sizeof(serveraddr));
|
|
|
|
if (res < 0) {
|
2016-11-13 16:30:21 +01:00
|
|
|
log_e("lwip_connect_r: %d", errno);
|
2016-10-06 13:21:30 +02:00
|
|
|
close(sockfd);
|
|
|
|
return 0;
|
|
|
|
}
|
2017-03-01 23:47:16 +01:00
|
|
|
clientSocketHandle.reset(new WiFiClientSocketHandle(sockfd));
|
2016-10-06 13:21:30 +02:00
|
|
|
_connected = true;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int WiFiClient::connect(const char *host, uint16_t port)
|
|
|
|
{
|
|
|
|
struct hostent *server;
|
|
|
|
server = gethostbyname(host);
|
|
|
|
if (server == NULL) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
IPAddress srv((const uint8_t *)(server->h_addr));
|
|
|
|
return connect(srv, port);
|
|
|
|
}
|
|
|
|
|
|
|
|
int WiFiClient::setSocketOption(int option, char* value, size_t len)
|
|
|
|
{
|
2017-03-01 23:47:16 +01:00
|
|
|
int res = setsockopt(fd(), SOL_SOCKET, option, value, len);
|
2016-10-06 13:21:30 +02:00
|
|
|
if(res < 0) {
|
2016-11-13 16:30:21 +01:00
|
|
|
log_e("%d", errno);
|
2016-10-06 13:21:30 +02:00
|
|
|
}
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
int WiFiClient::setTimeout(uint32_t seconds)
|
|
|
|
{
|
|
|
|
struct timeval tv;
|
|
|
|
tv.tv_sec = seconds;
|
|
|
|
tv.tv_usec = 0;
|
|
|
|
if(setSocketOption(SO_RCVTIMEO, (char *)&tv, sizeof(struct timeval)) < 0) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return setSocketOption(SO_SNDTIMEO, (char *)&tv, sizeof(struct timeval));
|
|
|
|
}
|
|
|
|
|
|
|
|
int WiFiClient::setOption(int option, int *value)
|
|
|
|
{
|
2017-03-01 23:47:16 +01:00
|
|
|
int res = setsockopt(fd(), IPPROTO_TCP, option, (char *) value, sizeof(int));
|
2016-10-06 13:21:30 +02:00
|
|
|
if(res < 0) {
|
2016-11-13 16:30:21 +01:00
|
|
|
log_e("%d", errno);
|
2016-10-06 13:21:30 +02:00
|
|
|
}
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
int WiFiClient::getOption(int option, int *value)
|
|
|
|
{
|
|
|
|
size_t size = sizeof(int);
|
2017-03-01 23:47:16 +01:00
|
|
|
int res = getsockopt(fd(), IPPROTO_TCP, option, (char *)value, &size);
|
2016-10-06 13:21:30 +02:00
|
|
|
if(res < 0) {
|
2016-11-13 16:30:21 +01:00
|
|
|
log_e("%d", errno);
|
2016-10-06 13:21:30 +02:00
|
|
|
}
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
int WiFiClient::setNoDelay(bool nodelay)
|
|
|
|
{
|
|
|
|
int flag = nodelay;
|
|
|
|
return setOption(TCP_NODELAY, &flag);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool WiFiClient::getNoDelay()
|
|
|
|
{
|
|
|
|
int flag = 0;
|
|
|
|
getOption(TCP_NODELAY, &flag);
|
|
|
|
return flag;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t WiFiClient::write(uint8_t data)
|
|
|
|
{
|
|
|
|
return write(&data, 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
int WiFiClient::read()
|
|
|
|
{
|
|
|
|
uint8_t data = 0;
|
|
|
|
int res = read(&data, 1);
|
|
|
|
if(res < 0) {
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
return data;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t WiFiClient::write(const uint8_t *buf, size_t size)
|
|
|
|
{
|
2017-02-28 23:37:00 +01:00
|
|
|
int res =0;
|
|
|
|
int retry = WIFI_CLIENT_MAX_WRITE_RETRY;
|
|
|
|
int socketFileDescriptor = fd();
|
|
|
|
|
|
|
|
if(!_connected || (socketFileDescriptor < 0)) {
|
2016-10-06 13:21:30 +02:00
|
|
|
return 0;
|
|
|
|
}
|
2017-02-28 23:37:00 +01:00
|
|
|
|
|
|
|
while(retry) {
|
|
|
|
//use select to make sure the socket is ready for writing
|
|
|
|
fd_set set;
|
|
|
|
struct timeval tv;
|
|
|
|
FD_ZERO(&set); // empties the set
|
|
|
|
FD_SET(socketFileDescriptor, &set); // adds FD to the set
|
|
|
|
tv.tv_sec = 0;
|
|
|
|
tv.tv_usec = WIFI_CLIENT_SELECT_TIMEOUT_US;
|
|
|
|
retry--;
|
|
|
|
|
|
|
|
if(select(socketFileDescriptor + 1, NULL, &set, NULL, &tv) < 0) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(FD_ISSET(socketFileDescriptor, &set)) {
|
|
|
|
res = send(socketFileDescriptor, (void*) buf, size, MSG_DONTWAIT);
|
|
|
|
if(res < 0) {
|
|
|
|
log_e("%d", errno);
|
|
|
|
if(errno != EAGAIN) {
|
|
|
|
//if resource was busy, can try again, otherwise give up
|
|
|
|
stop();
|
|
|
|
res = 0;
|
|
|
|
retry = 0;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
//completed successfully
|
|
|
|
retry = 0;
|
|
|
|
}
|
|
|
|
}
|
2016-10-06 13:21:30 +02:00
|
|
|
}
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
int WiFiClient::read(uint8_t *buf, size_t size)
|
|
|
|
{
|
|
|
|
if(!available()) {
|
|
|
|
return -1;
|
|
|
|
}
|
2017-03-01 23:47:16 +01:00
|
|
|
int res = recv(fd(), buf, size, MSG_DONTWAIT);
|
2016-10-06 13:21:30 +02:00
|
|
|
if(res < 0 && errno != EWOULDBLOCK) {
|
2016-11-13 16:30:21 +01:00
|
|
|
log_e("%d", errno);
|
2016-12-01 12:27:51 +01:00
|
|
|
stop();
|
2016-10-06 13:21:30 +02:00
|
|
|
}
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
int WiFiClient::available()
|
|
|
|
{
|
|
|
|
if(!_connected) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
int count;
|
2017-03-01 23:47:16 +01:00
|
|
|
int res = ioctl(fd(), FIONREAD, &count);
|
2016-10-06 13:21:30 +02:00
|
|
|
if(res < 0) {
|
2016-11-13 16:30:21 +01:00
|
|
|
log_e("%d", errno);
|
2016-12-01 12:27:51 +01:00
|
|
|
stop();
|
2016-10-06 13:21:30 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return count;
|
|
|
|
}
|
|
|
|
|
2017-03-02 01:49:53 +01:00
|
|
|
// Though flushing means to send all pending data,
|
|
|
|
// seems that in Arduino it also means to clear RX
|
|
|
|
void WiFiClient::flush() {
|
|
|
|
size_t a = available(), toRead = 0;
|
|
|
|
if(!a){
|
|
|
|
return;//nothing to flush
|
|
|
|
}
|
|
|
|
uint8_t * buf = (uint8_t *)malloc(WIFI_CLIENT_FLUSH_BUFFER_SIZE);
|
|
|
|
if(!buf){
|
|
|
|
return;//memory error
|
|
|
|
}
|
|
|
|
while(a){
|
|
|
|
toRead = (a>WIFI_CLIENT_FLUSH_BUFFER_SIZE)?WIFI_CLIENT_FLUSH_BUFFER_SIZE:a;
|
|
|
|
if(recv(fd(), buf, toRead, MSG_DONTWAIT) < 0) {
|
|
|
|
if(errno != EWOULDBLOCK){
|
|
|
|
log_e("%d", errno);
|
|
|
|
stop();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
delay(1);//give some time
|
|
|
|
}
|
|
|
|
a = available();
|
|
|
|
}
|
|
|
|
free(buf);
|
|
|
|
}
|
|
|
|
|
2016-10-06 13:21:30 +02:00
|
|
|
uint8_t WiFiClient::connected()
|
|
|
|
{
|
|
|
|
uint8_t dummy = 0;
|
|
|
|
read(&dummy, 0);
|
|
|
|
return _connected;
|
|
|
|
}
|
|
|
|
|
2017-03-01 23:47:16 +01:00
|
|
|
IPAddress WiFiClient::remoteIP(int fd) const
|
2016-10-06 13:21:30 +02:00
|
|
|
{
|
|
|
|
struct sockaddr_storage addr;
|
|
|
|
socklen_t len = sizeof addr;
|
|
|
|
getpeername(fd, (struct sockaddr*)&addr, &len);
|
|
|
|
struct sockaddr_in *s = (struct sockaddr_in *)&addr;
|
|
|
|
return IPAddress((uint32_t)(s->sin_addr.s_addr));
|
|
|
|
}
|
|
|
|
|
2017-03-01 23:47:16 +01:00
|
|
|
uint16_t WiFiClient::remotePort(int fd) const
|
2016-10-06 13:21:30 +02:00
|
|
|
{
|
|
|
|
struct sockaddr_storage addr;
|
|
|
|
socklen_t len = sizeof addr;
|
|
|
|
getpeername(fd, (struct sockaddr*)&addr, &len);
|
|
|
|
struct sockaddr_in *s = (struct sockaddr_in *)&addr;
|
|
|
|
return ntohs(s->sin_port);
|
|
|
|
}
|
|
|
|
|
2017-03-01 23:47:16 +01:00
|
|
|
IPAddress WiFiClient::remoteIP() const
|
2016-10-06 13:21:30 +02:00
|
|
|
{
|
2017-03-01 23:47:16 +01:00
|
|
|
return remoteIP(fd());
|
2016-10-06 13:21:30 +02:00
|
|
|
}
|
|
|
|
|
2017-03-01 23:47:16 +01:00
|
|
|
uint16_t WiFiClient::remotePort() const
|
2016-10-06 13:21:30 +02:00
|
|
|
{
|
2017-03-01 23:47:16 +01:00
|
|
|
return remotePort(fd());
|
2016-10-06 13:21:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool WiFiClient::operator==(const WiFiClient& rhs)
|
|
|
|
{
|
2017-03-01 23:47:16 +01:00
|
|
|
return clientSocketHandle == rhs.clientSocketHandle && remotePort() == rhs.remotePort() && remoteIP() == rhs.remoteIP();
|
|
|
|
}
|
|
|
|
|
|
|
|
int WiFiClient::fd() const
|
|
|
|
{
|
|
|
|
if (clientSocketHandle == NULL) {
|
|
|
|
return -1;
|
|
|
|
} else {
|
|
|
|
return clientSocketHandle->fd();
|
|
|
|
}
|
2016-10-06 13:21:30 +02:00
|
|
|
}
|
2017-03-01 23:47:16 +01:00
|
|
|
|