RedFlyServer.cpp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392
  1. #include <inttypes.h>
  2. #if (defined(__AVR__) || defined(ARDUINO_ARCH_AVR))
  3. # include <avr/pgmspace.h>
  4. #endif
  5. #if ARDUINO >= 100
  6. # include "Arduino.h"
  7. #else
  8. # include "WProgram.h"
  9. #endif
  10. #include "RedFly.h"
  11. #include "RedFlyServer.h"
  12. #define MAX_ERRORS 10
  13. //-------------------- Constructor/Destructor --------------------
  14. RedFlyServer::RedFlyServer(void)
  15. {
  16. s_port = 80;
  17. s_socket = INVALID_SOCKET;
  18. return;
  19. }
  20. RedFlyServer::RedFlyServer(uint16_t port)
  21. {
  22. s_port = port;
  23. s_socket = INVALID_SOCKET;
  24. return;
  25. }
  26. RedFlyServer::~RedFlyServer(void)
  27. {
  28. stop();
  29. return;
  30. }
  31. //-------------------- Public --------------------
  32. void RedFlyServer::begin(void)
  33. {
  34. connectSocket(PROTO_TCP);
  35. return;
  36. }
  37. void RedFlyServer::beginUDP(void)
  38. {
  39. connectSocket(PROTO_UDP);
  40. return;
  41. }
  42. int RedFlyServer::connect(void)
  43. {
  44. return connectSocket(PROTO_TCP);
  45. }
  46. int RedFlyServer::connectUDP(void)
  47. {
  48. return connectSocket(PROTO_UDP);
  49. }
  50. int RedFlyServer::connect(uint16_t port)
  51. {
  52. s_port = port;
  53. return connectSocket(PROTO_TCP);
  54. }
  55. int RedFlyServer::connectUDP(uint16_t port)
  56. {
  57. s_port = port;
  58. return connectSocket(PROTO_UDP);
  59. }
  60. int RedFlyServer::connectSocket(uint8_t p)
  61. {
  62. if(s_socket != INVALID_SOCKET)
  63. {
  64. return 0;
  65. }
  66. s_socket = RedFly.socketListen(p, s_port);
  67. if(s_socket == INVALID_SOCKET)
  68. {
  69. return 0;
  70. }
  71. c_ip[0] = 0;
  72. c_ip[1] = 0;
  73. c_ip[2] = 0;
  74. c_ip[3] = 0;
  75. c_port = 0;
  76. proto = p;
  77. error = 0;
  78. return 1;
  79. }
  80. uint8_t RedFlyServer::connected(void)
  81. {
  82. if(s_socket == INVALID_SOCKET)
  83. {
  84. return 0;
  85. }
  86. if(RedFly.socketClosed(s_socket)) //socket closed?
  87. {
  88. s_socket = INVALID_SOCKET;
  89. return 0;
  90. }
  91. if(error >= MAX_ERRORS)
  92. {
  93. RedFly.socketClose(s_socket);
  94. s_socket = INVALID_SOCKET;
  95. return 0;
  96. }
  97. return 1;
  98. }
  99. void RedFlyServer::stop(void)
  100. {
  101. if(s_socket == INVALID_SOCKET)
  102. {
  103. return;
  104. }
  105. flush(); //clear buffer
  106. RedFly.socketClose(s_socket);
  107. s_socket = INVALID_SOCKET;
  108. error = 0;
  109. return;
  110. }
  111. uint8_t RedFlyServer::status(void)
  112. {
  113. if(s_socket == INVALID_SOCKET)
  114. {
  115. return 1;
  116. }
  117. if(RedFly.socketStatus(s_socket)) //socket closed?
  118. {
  119. s_socket = INVALID_SOCKET;
  120. return 1;
  121. }
  122. return 0;
  123. }
  124. uint8_t RedFlyServer::getsocket(void)
  125. {
  126. return s_socket;
  127. }
  128. void RedFlyServer::getip(uint8_t *ip)
  129. {
  130. ip[0] = c_ip[0];
  131. ip[1] = c_ip[1];
  132. ip[2] = c_ip[2];
  133. ip[3] = c_ip[3];
  134. return;
  135. }
  136. uint16_t RedFlyServer::getport(void)
  137. {
  138. return c_port;
  139. }
  140. int RedFlyServer::available(void)
  141. {
  142. uint8_t socket=s_socket;
  143. uint16_t len=0;
  144. if(socket != INVALID_SOCKET)
  145. {
  146. RedFly.socketRead(&socket, &len, 0, 0);
  147. }
  148. return (int)len;
  149. }
  150. int RedFlyServer::read(void)
  151. {
  152. uint8_t b;
  153. uint8_t socket=s_socket;
  154. uint16_t len, rd;
  155. if(socket == INVALID_SOCKET)
  156. {
  157. return -1;
  158. }
  159. if(RedFly.socketState(socket) == SOCKET_UDP)
  160. {
  161. rd = RedFly.socketRead(&socket, &len, c_ip, &c_port, &b, 1);
  162. }
  163. else
  164. {
  165. rd = RedFly.socketRead(&socket, &len, &b, 1);
  166. }
  167. if(rd == 0)
  168. {
  169. return -1;
  170. }
  171. if(rd == 0xFFFF) //socket closed?
  172. {
  173. s_socket = INVALID_SOCKET;
  174. return -1;
  175. }
  176. return b;
  177. }
  178. int RedFlyServer::read(uint8_t *s, size_t sz)
  179. {
  180. int c, rd;
  181. for(rd=0; sz;)
  182. {
  183. c = read();
  184. if(c != -1)
  185. {
  186. *s++ = (uint8_t)c;
  187. sz--;
  188. rd++;
  189. }
  190. else
  191. {
  192. break;
  193. }
  194. }
  195. return rd;
  196. }
  197. void RedFlyServer::flush(void)
  198. {
  199. for(int len=available(); len!=0; len--)
  200. {
  201. read();
  202. }
  203. return;
  204. }
  205. size_t RedFlyServer::write(uint8_t b)
  206. {
  207. if(s_socket != INVALID_SOCKET)
  208. {
  209. if(RedFly.socketSend(s_socket, (uint8_t*)&b, 1, c_ip, c_port))
  210. {
  211. if(++error >= MAX_ERRORS)
  212. {
  213. RedFly.socketClose(s_socket);
  214. s_socket = INVALID_SOCKET;
  215. }
  216. }
  217. else
  218. {
  219. error = 0;
  220. return 1;
  221. }
  222. }
  223. return 0;
  224. }
  225. size_t RedFlyServer::write(const char *s)
  226. {
  227. if(s_socket != INVALID_SOCKET)
  228. {
  229. if(RedFly.socketSend(s_socket, (char*)s, c_ip, c_port))
  230. {
  231. if(++error >= MAX_ERRORS)
  232. {
  233. RedFly.socketClose(s_socket);
  234. s_socket = INVALID_SOCKET;
  235. }
  236. }
  237. else
  238. {
  239. error = 0;
  240. return strlen(s);
  241. }
  242. }
  243. return 0;
  244. }
  245. size_t RedFlyServer::write(const uint8_t *s, size_t size)
  246. {
  247. if(s_socket != INVALID_SOCKET)
  248. {
  249. if(RedFly.socketSend(s_socket, (uint8_t*)s, size, c_ip, c_port))
  250. {
  251. if(++error >= MAX_ERRORS)
  252. {
  253. RedFly.socketClose(s_socket);
  254. s_socket = INVALID_SOCKET;
  255. }
  256. }
  257. else
  258. {
  259. error = 0;
  260. return size;
  261. }
  262. }
  263. return 0;
  264. }
  265. size_t RedFlyServer::print_P(PGM_P s)
  266. {
  267. if(s_socket != INVALID_SOCKET)
  268. {
  269. if(RedFly.socketSendPGM(s_socket, s, c_ip, c_port))
  270. {
  271. if(++error >= MAX_ERRORS)
  272. {
  273. RedFly.socketClose(s_socket);
  274. s_socket = INVALID_SOCKET;
  275. }
  276. }
  277. else
  278. {
  279. error = 0;
  280. return strlen_P(s);
  281. }
  282. }
  283. return 0;
  284. }
  285. size_t RedFlyServer::println_P(PGM_P s)
  286. {
  287. size_t len;
  288. len = print_P(s);
  289. if(len)
  290. {
  291. len += print_P(PSTR("\r\n"));
  292. }
  293. return len;
  294. }
  295. //the next function allows us to use the client returned by
  296. //RedFlyServer::available() as the condition in an if-statement.
  297. RedFlyServer::operator bool()
  298. {
  299. return s_socket != INVALID_SOCKET;
  300. }