RedFlyClient.cpp 6.2 KB

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