123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392 |
- #include <inttypes.h>
- #if (defined(__AVR__) || defined(ARDUINO_ARCH_AVR))
- # include <avr/pgmspace.h>
- #endif
- #if ARDUINO >= 100
- # include "Arduino.h"
- #else
- # include "WProgram.h"
- #endif
- #include "RedFly.h"
- #include "RedFlyServer.h"
- #define MAX_ERRORS 10
- //-------------------- Constructor/Destructor --------------------
- RedFlyServer::RedFlyServer(void)
- {
- s_port = 80;
- s_socket = INVALID_SOCKET;
- return;
- }
- RedFlyServer::RedFlyServer(uint16_t port)
- {
- s_port = port;
- s_socket = INVALID_SOCKET;
- return;
- }
- RedFlyServer::~RedFlyServer(void)
- {
- stop();
- return;
- }
- //-------------------- Public --------------------
- void RedFlyServer::begin(void)
- {
- connectSocket(PROTO_TCP);
- return;
- }
- void RedFlyServer::beginUDP(void)
- {
- connectSocket(PROTO_UDP);
- return;
- }
- int RedFlyServer::connect(void)
- {
- return connectSocket(PROTO_TCP);
- }
- int RedFlyServer::connectUDP(void)
- {
- return connectSocket(PROTO_UDP);
- }
- int RedFlyServer::connect(uint16_t port)
- {
- s_port = port;
- return connectSocket(PROTO_TCP);
- }
- int RedFlyServer::connectUDP(uint16_t port)
- {
- s_port = port;
- return connectSocket(PROTO_UDP);
- }
- int RedFlyServer::connectSocket(uint8_t p)
- {
- if(s_socket != INVALID_SOCKET)
- {
- return 0;
- }
- s_socket = RedFly.socketListen(p, s_port);
- if(s_socket == INVALID_SOCKET)
- {
- return 0;
- }
- c_ip[0] = 0;
- c_ip[1] = 0;
- c_ip[2] = 0;
- c_ip[3] = 0;
- c_port = 0;
- proto = p;
- error = 0;
- return 1;
- }
- uint8_t RedFlyServer::connected(void)
- {
- if(s_socket == INVALID_SOCKET)
- {
- return 0;
- }
- if(RedFly.socketClosed(s_socket)) //socket closed?
- {
- s_socket = INVALID_SOCKET;
- return 0;
- }
- if(error >= MAX_ERRORS)
- {
- RedFly.socketClose(s_socket);
- s_socket = INVALID_SOCKET;
- return 0;
- }
- return 1;
- }
- void RedFlyServer::stop(void)
- {
- if(s_socket == INVALID_SOCKET)
- {
- return;
- }
- flush(); //clear buffer
- RedFly.socketClose(s_socket);
- s_socket = INVALID_SOCKET;
- error = 0;
- return;
- }
- uint8_t RedFlyServer::status(void)
- {
- if(s_socket == INVALID_SOCKET)
- {
- return 1;
- }
- if(RedFly.socketStatus(s_socket)) //socket closed?
- {
- s_socket = INVALID_SOCKET;
- return 1;
- }
- return 0;
- }
- uint8_t RedFlyServer::getsocket(void)
- {
- return s_socket;
- }
- void RedFlyServer::getip(uint8_t *ip)
- {
- ip[0] = c_ip[0];
- ip[1] = c_ip[1];
- ip[2] = c_ip[2];
- ip[3] = c_ip[3];
- return;
- }
- uint16_t RedFlyServer::getport(void)
- {
- return c_port;
- }
- int RedFlyServer::available(void)
- {
- uint8_t socket=s_socket;
- uint16_t len=0;
- if(socket != INVALID_SOCKET)
- {
- RedFly.socketRead(&socket, &len, 0, 0);
- }
- return (int)len;
- }
- int RedFlyServer::read(void)
- {
- uint8_t b;
- uint8_t socket=s_socket;
- uint16_t len, rd;
- if(socket == INVALID_SOCKET)
- {
- return -1;
- }
- if(RedFly.socketState(socket) == SOCKET_UDP)
- {
- rd = RedFly.socketRead(&socket, &len, c_ip, &c_port, &b, 1);
- }
- else
- {
- rd = RedFly.socketRead(&socket, &len, &b, 1);
- }
- if(rd == 0)
- {
- return -1;
- }
- if(rd == 0xFFFF) //socket closed?
- {
- s_socket = INVALID_SOCKET;
- return -1;
- }
- return b;
- }
- int RedFlyServer::read(uint8_t *s, size_t sz)
- {
- int c, rd;
- for(rd=0; sz;)
- {
- c = read();
- if(c != -1)
- {
- *s++ = (uint8_t)c;
- sz--;
- rd++;
- }
- else
- {
- break;
- }
- }
- return rd;
- }
- void RedFlyServer::flush(void)
- {
- for(int len=available(); len!=0; len--)
- {
- read();
- }
- return;
- }
- size_t RedFlyServer::write(uint8_t b)
- {
- if(s_socket != INVALID_SOCKET)
- {
- if(RedFly.socketSend(s_socket, (uint8_t*)&b, 1, c_ip, c_port))
- {
- if(++error >= MAX_ERRORS)
- {
- RedFly.socketClose(s_socket);
- s_socket = INVALID_SOCKET;
- }
- }
- else
- {
- error = 0;
- return 1;
- }
- }
- return 0;
- }
- size_t RedFlyServer::write(const char *s)
- {
- if(s_socket != INVALID_SOCKET)
- {
- if(RedFly.socketSend(s_socket, (char*)s, c_ip, c_port))
- {
- if(++error >= MAX_ERRORS)
- {
- RedFly.socketClose(s_socket);
- s_socket = INVALID_SOCKET;
- }
- }
- else
- {
- error = 0;
- return strlen(s);
- }
- }
- return 0;
- }
- size_t RedFlyServer::write(const uint8_t *s, size_t size)
- {
- if(s_socket != INVALID_SOCKET)
- {
- if(RedFly.socketSend(s_socket, (uint8_t*)s, size, c_ip, c_port))
- {
- if(++error >= MAX_ERRORS)
- {
- RedFly.socketClose(s_socket);
- s_socket = INVALID_SOCKET;
- }
- }
- else
- {
- error = 0;
- return size;
- }
- }
- return 0;
- }
- size_t RedFlyServer::print_P(PGM_P s)
- {
- if(s_socket != INVALID_SOCKET)
- {
- if(RedFly.socketSendPGM(s_socket, s, c_ip, c_port))
- {
- if(++error >= MAX_ERRORS)
- {
- RedFly.socketClose(s_socket);
- s_socket = INVALID_SOCKET;
- }
- }
- else
- {
- error = 0;
- return strlen_P(s);
- }
- }
- return 0;
- }
- size_t RedFlyServer::println_P(PGM_P s)
- {
- size_t len;
- len = print_P(s);
- if(len)
- {
- len += print_P(PSTR("\r\n"));
- }
- return len;
- }
- //the next function allows us to use the client returned by
- //RedFlyServer::available() as the condition in an if-statement.
- RedFlyServer::operator bool()
- {
- return s_socket != INVALID_SOCKET;
- }
|