WiFiEspClient.cpp 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. /*--------------------------------------------------------------------
  2. This file is part of the Arduino WiFiEsp library.
  3. The Arduino WiFiEsp library is free software: you can redistribute it
  4. and/or modify it under the terms of the GNU General Public License as
  5. published by the Free Software Foundation, either version 3 of the
  6. License, or (at your option) any later version.
  7. The Arduino WiFiEsp library is distributed in the hope that it will be
  8. useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with The Arduino WiFiEsp library. If not, see
  13. <http://www.gnu.org/licenses/>.
  14. --------------------------------------------------------------------*/
  15. #include <inttypes.h>
  16. #include "WiFiEsp.h"
  17. #include "WiFiEspClient.h"
  18. #include "WiFiEspServer.h"
  19. #include "utility/EspDrv.h"
  20. #include "utility/debug.h"
  21. WiFiEspClient::WiFiEspClient() : _sock(255)
  22. {
  23. }
  24. WiFiEspClient::WiFiEspClient(uint8_t sock) : _sock(sock)
  25. {
  26. }
  27. ////////////////////////////////////////////////////////////////////////////////
  28. // Overrided Print methods
  29. ////////////////////////////////////////////////////////////////////////////////
  30. // the standard print method will call write for each character in the buffer
  31. // this is very slow on ESP
  32. size_t WiFiEspClient::print(const __FlashStringHelper *ifsh)
  33. {
  34. printFSH(ifsh, false);
  35. }
  36. // if we do override this, the standard println will call the print
  37. // method twice
  38. size_t WiFiEspClient::println(const __FlashStringHelper *ifsh)
  39. {
  40. printFSH(ifsh, true);
  41. }
  42. ////////////////////////////////////////////////////////////////////////////////
  43. // Implementation of Client virtual methods
  44. ////////////////////////////////////////////////////////////////////////////////
  45. int WiFiEspClient::connectSSL(const char* host, uint16_t port)
  46. {
  47. return connect(host, port, SSL_MODE);
  48. }
  49. int WiFiEspClient::connectSSL(IPAddress ip, uint16_t port)
  50. {
  51. char s[16];
  52. sprintf_P(s, PSTR("%d.%d.%d.%d"), ip[0], ip[1], ip[2], ip[3]);
  53. return connect(s, port, SSL_MODE);
  54. }
  55. int WiFiEspClient::connect(const char* host, uint16_t port)
  56. {
  57. return connect(host, port, TCP_MODE);
  58. }
  59. int WiFiEspClient::connect(IPAddress ip, uint16_t port)
  60. {
  61. char s[16];
  62. sprintf_P(s, PSTR("%d.%d.%d.%d"), ip[0], ip[1], ip[2], ip[3]);
  63. return connect(s, port, TCP_MODE);
  64. }
  65. /* Private method */
  66. int WiFiEspClient::connect(const char* host, uint16_t port, uint8_t protMode)
  67. {
  68. LOGINFO1(F("Connecting to"), host);
  69. _sock = WiFiEspClass::getFreeSocket();
  70. if (_sock != NO_SOCKET_AVAIL)
  71. {
  72. if (!EspDrv::startClient(host, port, _sock, protMode))
  73. return 0;
  74. WiFiEspClass::allocateSocket(_sock);
  75. }
  76. else
  77. {
  78. LOGERROR(F("No socket available"));
  79. return 0;
  80. }
  81. return 1;
  82. }
  83. size_t WiFiEspClient::write(uint8_t b)
  84. {
  85. return write(&b, 1);
  86. }
  87. size_t WiFiEspClient::write(const uint8_t *buf, size_t size)
  88. {
  89. if (_sock >= MAX_SOCK_NUM or size==0)
  90. {
  91. setWriteError();
  92. return 0;
  93. }
  94. bool r = EspDrv::sendData(_sock, buf, size);
  95. if (!r)
  96. {
  97. setWriteError();
  98. LOGERROR1(F("Failed to write to socket"), _sock);
  99. delay(4000);
  100. stop();
  101. return 0;
  102. }
  103. return size;
  104. }
  105. int WiFiEspClient::available()
  106. {
  107. if (_sock != 255)
  108. {
  109. int bytes = EspDrv::availData(_sock);
  110. if (bytes>0)
  111. {
  112. return bytes;
  113. }
  114. }
  115. return 0;
  116. }
  117. int WiFiEspClient::read()
  118. {
  119. uint8_t b;
  120. if (!available())
  121. return -1;
  122. bool connClose = false;
  123. EspDrv::getData(_sock, &b, false, &connClose);
  124. if (connClose)
  125. {
  126. WiFiEspClass::releaseSocket(_sock);
  127. _sock = 255;
  128. }
  129. return b;
  130. }
  131. int WiFiEspClient::read(uint8_t* buf, size_t size)
  132. {
  133. if (!available())
  134. return -1;
  135. return EspDrv::getDataBuf(_sock, buf, size);
  136. }
  137. int WiFiEspClient::peek()
  138. {
  139. uint8_t b;
  140. if (!available())
  141. return -1;
  142. bool connClose = false;
  143. EspDrv::getData(_sock, &b, true, &connClose);
  144. if (connClose)
  145. {
  146. WiFiEspClass::releaseSocket(_sock);
  147. _sock = 255;
  148. }
  149. return b;
  150. }
  151. void WiFiEspClient::flush()
  152. {
  153. while (available())
  154. read();
  155. }
  156. void WiFiEspClient::stop()
  157. {
  158. if (_sock == 255)
  159. return;
  160. LOGINFO1(F("Disconnecting "), _sock);
  161. EspDrv::stopClient(_sock);
  162. WiFiEspClass::releaseSocket(_sock);
  163. _sock = 255;
  164. }
  165. uint8_t WiFiEspClient::connected()
  166. {
  167. return (status() == ESTABLISHED);
  168. }
  169. WiFiEspClient::operator bool()
  170. {
  171. return _sock != 255;
  172. }
  173. ////////////////////////////////////////////////////////////////////////////////
  174. // Additional WiFi standard methods
  175. ////////////////////////////////////////////////////////////////////////////////
  176. uint8_t WiFiEspClient::status()
  177. {
  178. if (_sock == 255)
  179. {
  180. return CLOSED;
  181. }
  182. if (EspDrv::availData(_sock))
  183. {
  184. return ESTABLISHED;
  185. }
  186. if (EspDrv::getClientState(_sock))
  187. {
  188. return ESTABLISHED;
  189. }
  190. WiFiEspClass::releaseSocket(_sock);
  191. _sock = 255;
  192. return CLOSED;
  193. }
  194. IPAddress WiFiEspClient::remoteIP()
  195. {
  196. IPAddress ret;
  197. EspDrv::getRemoteIpAddress(ret);
  198. return ret;
  199. }
  200. ////////////////////////////////////////////////////////////////////////////////
  201. // Private Methods
  202. ////////////////////////////////////////////////////////////////////////////////
  203. size_t WiFiEspClient::printFSH(const __FlashStringHelper *ifsh, bool appendCrLf)
  204. {
  205. size_t size = strlen_P((char*)ifsh);
  206. if (_sock >= MAX_SOCK_NUM or size==0)
  207. {
  208. setWriteError();
  209. return 0;
  210. }
  211. bool r = EspDrv::sendData(_sock, ifsh, size, appendCrLf);
  212. if (!r)
  213. {
  214. setWriteError();
  215. LOGERROR1(F("Failed to write to socket"), _sock);
  216. delay(4000);
  217. stop();
  218. return 0;
  219. }
  220. return size;
  221. }