S&B Volcano vaporizer remote control with Pi Pico W
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

http_socket.c 9.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395
  1. /*
  2. * http_socket.c
  3. *
  4. * Based on BittyHTTP example socket interface.
  5. *
  6. * Copyright (c) 2023 Thomas Buck (thomas@xythobuz.de)
  7. *
  8. * This program is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation, either version 3 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * See <http://www.gnu.org/licenses/>.
  19. */
  20. #include <string.h>
  21. #include "lwip/tcp.h"
  22. #include "lwip/dns.h"
  23. #include "SocketsCon.h"
  24. #include "config.h"
  25. #include "log.h"
  26. #include "main.h"
  27. #include "ring.h"
  28. #include "http.h"
  29. #define MAX_SOCK 4
  30. #define SOCK_RECV_BUFF 512
  31. #define HTTP_DNS_TIMEOUT_MS 5000
  32. #define HTTP_CONNECT_TIMEOUT_MS 5000
  33. struct tcp_sock {
  34. bool set;
  35. struct tcp_pcb *pcb;
  36. // TODO listening server socket has unused buffer
  37. uint8_t rx_buf[SOCK_RECV_BUFF];
  38. struct ring_buffer rx_rb;
  39. struct tcp_pcb *child_buf[MAX_SOCK];
  40. struct ring_buffer child_rb;
  41. };
  42. static struct tcp_sock sock[MAX_SOCK] = {0};
  43. bool SocketsCon_InitSocketConSystem(void) {
  44. return true;
  45. }
  46. void SocketsCon_ShutdownSocketConSystem(void) { }
  47. static void tcp_server_err(void *arg, err_t err) {
  48. debug("tcp error %d", err);
  49. struct SocketCon *Con = arg;
  50. Con->ErrorCode = e_ConnectErrorMAX;
  51. Con->State = e_ConnectState_Error;
  52. }
  53. static err_t tcp_server_recv(void *arg, struct tcp_pcb *tpcb, struct pbuf *p, err_t err) {
  54. (void)tpcb;
  55. debug("tcp recv %d %d", p->len, err);
  56. struct SocketCon *Con = arg;
  57. size_t idx = Con->SocketFD;
  58. if (rb_space(&sock[idx].rx_rb) < p->len) {
  59. debug("not enough space (%d < %d)", rb_space(&sock[idx].rx_rb), p->len);
  60. tcp_abort(sock[idx].pcb);
  61. Con->ErrorCode = e_ConnectErrorMAX;
  62. Con->State = e_ConnectState_Error;
  63. return ERR_ABRT;
  64. }
  65. rb_add(&sock[idx].rx_rb, p->payload, p->len);
  66. return ERR_OK;
  67. }
  68. bool SocketsCon_InitSockCon(struct SocketCon *Con) {
  69. if (!Con) {
  70. debug("invalid param");
  71. return false;
  72. }
  73. ssize_t next = -1;
  74. for (size_t i = 0; i < MAX_SOCK; i++) {
  75. if (!sock[i].set) {
  76. next = i;
  77. break;
  78. }
  79. }
  80. if (next < 0) {
  81. debug("error: too many sockets");
  82. return false;
  83. }
  84. debug("new socket at %d", next);
  85. sock[next].pcb = tcp_new_ip_type(IPADDR_TYPE_ANY);
  86. if (sock[next].pcb == NULL) {
  87. debug("error allocating new socket");
  88. return false;
  89. }
  90. tcp_arg(sock[next].pcb, Con);
  91. tcp_err(sock[next].pcb, tcp_server_err);
  92. tcp_recv(sock[next].pcb, tcp_server_recv);
  93. Con->SocketFD = next;
  94. sock[next].set = true;
  95. struct ring_buffer tmp = RB_INIT(sock[next].rx_buf, SOCK_RECV_BUFF, sizeof(uint8_t));
  96. sock[next].rx_rb = tmp;
  97. struct ring_buffer tmp2 = RB_INIT(sock[next].child_buf, MAX_SOCK, sizeof(struct tcp_pcb *));
  98. sock[next].child_rb = tmp2;
  99. Con->ErrorCode = e_ConnectError_AllOk;
  100. Con->State = e_ConnectState_Idle;
  101. Con->ReadInProgress = false;
  102. return true;
  103. }
  104. static err_t tcp_server_connected(void *arg, struct tcp_pcb *tpcb, err_t err) {
  105. (void)tpcb;
  106. debug("tcp connected");
  107. struct SocketCon *Con = arg;
  108. Con->State = (err == ERR_OK) ? e_ConnectState_Connected : e_ConnectState_Idle;
  109. return ERR_OK;
  110. }
  111. bool SocketsCon_Connect(struct SocketCon *Con,
  112. const char *ServerName, int portNo) {
  113. if ((!Con) || (!ServerName)) {
  114. debug("invalid param");
  115. return false;
  116. }
  117. ip_addr_t ipaddr;
  118. err_t err;
  119. uint32_t start_time = to_ms_since_boot(get_absolute_time());
  120. do {
  121. err = dns_gethostbyname(ServerName, &ipaddr, NULL, NULL);
  122. main_loop_hw();
  123. uint32_t now = to_ms_since_boot(get_absolute_time());
  124. if ((now - start_time) >= HTTP_DNS_TIMEOUT_MS) {
  125. break;
  126. }
  127. } while (err == ERR_INPROGRESS);
  128. if (err != ERR_OK) {
  129. debug("error getting IP for '%s'", ServerName);
  130. return false;
  131. } else {
  132. debug("IP %s for '%s'", ip4addr_ntoa(&ipaddr), ServerName);
  133. }
  134. Con->State = e_ConnectState_Connecting;
  135. size_t idx = Con->SocketFD;
  136. err = tcp_connect(sock[idx].pcb,
  137. &ipaddr, portNo,
  138. tcp_server_connected);
  139. if (err != ERR_OK) {
  140. debug("error connecting (%d)", err);
  141. Con->State = e_ConnectState_Idle;
  142. return false;
  143. }
  144. start_time = to_ms_since_boot(get_absolute_time());
  145. while (Con->State == e_ConnectState_Connecting) {
  146. main_loop_hw();
  147. uint32_t now = to_ms_since_boot(get_absolute_time());
  148. if ((now - start_time) >= HTTP_CONNECT_TIMEOUT_MS) {
  149. break;
  150. }
  151. }
  152. // TODO ?
  153. //if (Con->State == e_ConnectState_Error) {
  154. // Con->State = e_ConnectState_Idle;
  155. //}
  156. return (Con->State == e_ConnectState_Connected);
  157. }
  158. bool SocketsCon_EnableAddressReuse(struct SocketCon *Con, bool Enable) {
  159. (void)Con;
  160. (void)Enable;
  161. return true;
  162. }
  163. void SocketsCon_Tick(struct SocketCon *Con) { (void)Con; }
  164. bool SocketsCon_Write(struct SocketCon *Con, const void *buf, int num) {
  165. if ((!Con) || (!buf) || (num <= 0)) {
  166. debug("invalid param");
  167. return false;
  168. }
  169. size_t idx = Con->SocketFD;
  170. err_t err = tcp_write(sock[idx].pcb,
  171. buf, num, TCP_WRITE_FLAG_COPY);
  172. if (err != ERR_OK) {
  173. debug("error writing to socket");
  174. return false;
  175. }
  176. return true;
  177. }
  178. int SocketsCon_Read(struct SocketCon *Con, void *buf, int num) {
  179. if ((!Con) || (!buf) || (num <= 0)) {
  180. debug("invalid param");
  181. return false;
  182. }
  183. // TODO irq disable?
  184. size_t idx = Con->SocketFD;
  185. size_t len = rb_get(&sock[idx].rx_rb, buf, num);
  186. // TODO irq enable?
  187. return len;
  188. }
  189. void SocketsCon_Close(struct SocketCon *Con) {
  190. if (!Con) {
  191. debug("invalid param");
  192. return;
  193. }
  194. size_t idx = Con->SocketFD;
  195. sock[idx].set = false;
  196. err_t err = tcp_close(sock[idx].pcb);
  197. if (err != ERR_OK) {
  198. debug("error closing socket (%d)", err);
  199. // TODO retry?
  200. }
  201. }
  202. static err_t tcp_server_accept(void *arg, struct tcp_pcb *newpcb, err_t err) {
  203. if (err != ERR_OK) {
  204. debug("ignoring failed accept");
  205. return ERR_OK;
  206. }
  207. struct SocketCon *Con = arg;
  208. size_t idx = Con->SocketFD;
  209. if (rb_space(&sock[idx].child_rb) <= 0) {
  210. debug("no space for new connection");
  211. tcp_abort(newpcb);
  212. return ERR_OK; // ERR_ABRT ?
  213. }
  214. debug("new connection (%d)", err);
  215. rb_push(&sock[idx].child_rb, &newpcb);
  216. return ERR_OK;
  217. }
  218. bool SocketsCon_Listen(struct SocketCon *Con, const char *bindadd, int PortNo) {
  219. if (!Con) {
  220. debug("invalid param");
  221. return false;
  222. }
  223. ip_addr_t ipaddr;
  224. err_t err;
  225. if (bindadd) {
  226. uint32_t start_time = to_ms_since_boot(get_absolute_time());
  227. do {
  228. err = dns_gethostbyname(bindadd, &ipaddr, NULL, NULL);
  229. main_loop_hw();
  230. uint32_t now = to_ms_since_boot(get_absolute_time());
  231. if ((now - start_time) >= HTTP_DNS_TIMEOUT_MS) {
  232. break;
  233. }
  234. } while (err == ERR_INPROGRESS);
  235. if (err != ERR_OK) {
  236. debug("error getting IP for '%s'", bindadd);
  237. return false;
  238. } else {
  239. debug("IP %s for '%s'", ip4addr_ntoa(&ipaddr), bindadd);
  240. }
  241. }
  242. size_t idx = Con->SocketFD;
  243. err = tcp_bind(sock[idx].pcb,
  244. bindadd ? &ipaddr : IP_ANY_TYPE,
  245. PortNo);
  246. if (err != ERR_OK) {
  247. debug("error binding to '%s'", bindadd);
  248. return false;
  249. }
  250. struct tcp_pcb *tmp = tcp_listen(sock[idx].pcb);
  251. if (tmp == NULL) {
  252. debug("error listening on socket");
  253. return false;
  254. }
  255. sock[idx].pcb = tmp;
  256. tcp_accept(sock[idx].pcb, tcp_server_accept);
  257. return true;
  258. }
  259. bool SocketsCon_Accept(struct SocketCon *Con, struct SocketCon *NewCon) {
  260. if ((!Con) || (!NewCon)) {
  261. debug("invalid param");
  262. return false;
  263. }
  264. size_t idx = Con->SocketFD;
  265. size_t new_idx = NewCon->SocketFD;
  266. if (rb_len(&sock[idx].child_rb) <= 0) {
  267. return false;
  268. }
  269. debug("accepting new connection");
  270. struct tcp_pcb *new_pcb;
  271. rb_pop(&sock[idx].child_rb, &new_pcb);
  272. err_t err = tcp_close(sock[new_idx].pcb);
  273. if (err != ERR_OK) {
  274. debug("error closing prev new socket");
  275. }
  276. sock[new_idx].pcb = new_pcb;
  277. return true;
  278. }
  279. bool SocketsCon_HasError(struct SocketCon *Con) {
  280. if (!Con) {
  281. debug("invalid param");
  282. return true;
  283. }
  284. return Con->State == e_ConnectState_Error;
  285. }
  286. bool SocketsCon_IsConnected(struct SocketCon *Con) {
  287. if (!Con) {
  288. debug("invalid param");
  289. return false;
  290. }
  291. return Con->State == e_ConnectState_Connected;
  292. }
  293. int SocketsCon_GetLastErrNo(struct SocketCon *Con) {
  294. if (!Con) {
  295. debug("invalid param");
  296. return -1;
  297. }
  298. return Con->Last_errno;
  299. }
  300. e_ConnectErrorType SocketsCon_GetErrorCode(struct SocketCon *Con) {
  301. if (!Con) {
  302. debug("invalid param");
  303. return e_ConnectErrorMAX;
  304. }
  305. return Con->ErrorCode;
  306. }
  307. bool SocketsCon_GetSocketHandle(struct SocketCon *Con,
  308. t_ConSocketHandle *RetHandle) {
  309. if (!Con) {
  310. debug("invalid param");
  311. return false;
  312. }
  313. if (Con->SocketFD < 0) {
  314. return false;
  315. }
  316. *RetHandle = Con->SocketFD;
  317. return true;
  318. }