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.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. /**
  2. * Copyright (c) 2022 Brian Starkey <stark3y@gmail.com>
  3. *
  4. * Based on the Pico W tcp_server example:
  5. * Copyright (c) 2022 Raspberry Pi (Trading) Ltd.
  6. *
  7. * SPDX-License-Identifier: BSD-3-Clause
  8. */
  9. #include <string.h>
  10. #include <stdlib.h>
  11. #include "pico/stdlib.h"
  12. #include "pico/cyw43_arch.h"
  13. #include "lwip/pbuf.h"
  14. #include "lwip/tcp.h"
  15. extern const char *wifi_ssid;
  16. extern const char *wifi_pass;
  17. #define TCP_PORT 4242
  18. #define DEBUG_printf printf
  19. #define POLL_TIME_S 5
  20. #define MAX_LEN 2048
  21. enum conn_state {
  22. CONN_STATE_WAIT_FOR_SYNC,
  23. CONN_STATE_READ_OPCODE,
  24. CONN_STATE_READ_ARGS,
  25. CONN_STATE_READ_DATA,
  26. CONN_STATE_HANDLE,
  27. CONN_STATE_WRITE_RESP,
  28. CONN_STATE_ERROR,
  29. CONN_STATE_CLOSED,
  30. };
  31. struct tcp_ctx {
  32. struct tcp_pcb *serv_pcb;
  33. volatile bool serv_done;
  34. struct tcp_pcb *conn_pcb;
  35. uint8_t buf[MAX_LEN];
  36. size_t bytes_remaining;
  37. enum conn_state conn_state;
  38. };
  39. static err_t tcp_conn_close(struct tcp_ctx *ctx)
  40. {
  41. err_t err = ERR_OK;
  42. cyw43_arch_gpio_put (0, false);
  43. ctx->conn_state = CONN_STATE_CLOSED;
  44. if (!ctx->conn_pcb) {
  45. return err;
  46. }
  47. tcp_arg(ctx->conn_pcb, NULL);
  48. tcp_poll(ctx->conn_pcb, NULL, 0);
  49. tcp_sent(ctx->conn_pcb, NULL);
  50. tcp_recv(ctx->conn_pcb, NULL);
  51. tcp_err(ctx->conn_pcb, NULL);
  52. err = tcp_close(ctx->conn_pcb);
  53. if (err != ERR_OK) {
  54. DEBUG_printf("close failed %d, calling abort\n", err);
  55. tcp_abort(ctx->conn_pcb);
  56. err = ERR_ABRT;
  57. }
  58. ctx->conn_pcb = NULL;
  59. return err;
  60. }
  61. static err_t tcp_server_close(void *arg)
  62. {
  63. struct tcp_ctx *ctx = (struct tcp_ctx *)arg;
  64. err_t err = ERR_OK;
  65. err = tcp_conn_close(ctx);
  66. if ((err != ERR_OK) && ctx->serv_pcb) {
  67. tcp_arg(ctx->serv_pcb, NULL);
  68. tcp_abort(ctx->serv_pcb);
  69. ctx->serv_pcb = NULL;
  70. return ERR_ABRT;
  71. }
  72. if (!ctx->serv_pcb) {
  73. return err;
  74. }
  75. tcp_arg(ctx->serv_pcb, NULL);
  76. err = tcp_close(ctx->serv_pcb);
  77. if (err != ERR_OK) {
  78. tcp_abort(ctx->serv_pcb);
  79. err = ERR_ABRT;
  80. }
  81. ctx->serv_pcb = NULL;
  82. return err;
  83. }
  84. static void tcp_server_complete(void *arg, int status)
  85. {
  86. struct tcp_ctx *ctx = (struct tcp_ctx *)arg;
  87. if (status == 0) {
  88. DEBUG_printf("server completed normally\n");
  89. } else {
  90. DEBUG_printf("server error %d\n", status);
  91. }
  92. tcp_server_close(ctx);
  93. ctx->serv_done = true;
  94. }
  95. static err_t tcp_conn_complete(void *arg, int status)
  96. {
  97. struct tcp_ctx *ctx = (struct tcp_ctx *)arg;
  98. if (status == 0) {
  99. DEBUG_printf("conn completed normally\n");
  100. } else {
  101. DEBUG_printf("conn error %d\n", status);
  102. }
  103. return tcp_conn_close(ctx);
  104. }
  105. static err_t tcp_conn_sent(void *arg, struct tcp_pcb *tpcb, u16_t len)
  106. {
  107. struct tcp_ctx *ctx = (struct tcp_ctx *)arg;
  108. DEBUG_printf("tcp_server_sent %u\n", len);
  109. return ERR_OK;
  110. }
  111. static err_t tcp_conn_recv(void *arg, struct tcp_pcb *tpcb, struct pbuf *p, err_t err)
  112. {
  113. struct tcp_ctx *ctx = (struct tcp_ctx *)arg;
  114. if (!p) {
  115. return tcp_conn_complete(ctx, 0);
  116. }
  117. // this method is callback from lwIP, so cyw43_arch_lwip_begin is not required, however you
  118. // can use this method to cause an assertion in debug mode, if this method is called when
  119. // cyw43_arch_lwip_begin IS needed
  120. cyw43_arch_lwip_check();
  121. if (p->tot_len > 0) {
  122. DEBUG_printf("tcp_server_recv %d err %d\n", p->tot_len, err);
  123. // Receive the buffer
  124. pbuf_copy_partial(p, ctx->buf, p->tot_len, 0);
  125. ctx->buf[p->tot_len] = '\0';
  126. printf("%s\n", ctx->buf);
  127. tcp_recved(tpcb, p->tot_len);
  128. }
  129. pbuf_free(p);
  130. return ERR_OK;
  131. }
  132. static err_t tcp_conn_poll(void *arg, struct tcp_pcb *tpcb)
  133. {
  134. DEBUG_printf("tcp_server_poll_fn\n");
  135. return ERR_OK;
  136. }
  137. static void tcp_conn_err(void *arg, err_t err)
  138. {
  139. struct tcp_ctx *ctx = (struct tcp_ctx *)arg;
  140. DEBUG_printf("tcp_conn_err %d\n", err);
  141. ctx->conn_pcb = NULL;
  142. ctx->conn_state = CONN_STATE_CLOSED;
  143. ctx->bytes_remaining = 0;
  144. cyw43_arch_gpio_put (0, false);
  145. }
  146. static void tcp_conn_wait_for_sync(struct tcp_ctx *ctx)
  147. {
  148. ctx->conn_state = CONN_STATE_WAIT_FOR_SYNC;
  149. ctx->bytes_remaining = 4;
  150. }
  151. static void tcp_conn_init(struct tcp_ctx *ctx, struct tcp_pcb *pcb)
  152. {
  153. ctx->conn_pcb = pcb;
  154. tcp_arg(pcb, ctx);
  155. tcp_sent(pcb, tcp_conn_sent);
  156. tcp_recv(pcb, tcp_conn_recv);
  157. tcp_poll(pcb, tcp_conn_poll, POLL_TIME_S * 2);
  158. tcp_err(pcb, tcp_conn_err);
  159. cyw43_arch_gpio_put (0, true);
  160. tcp_conn_wait_for_sync(ctx);
  161. }
  162. static err_t tcp_server_accept(void *arg, struct tcp_pcb *client_pcb, err_t err)
  163. {
  164. struct tcp_ctx *ctx = (struct tcp_ctx *)arg;
  165. if (err != ERR_OK || client_pcb == NULL) {
  166. DEBUG_printf("Failure in accept\n");
  167. tcp_server_complete(ctx, err);
  168. return ERR_VAL;
  169. }
  170. DEBUG_printf("Connection opened\n");
  171. if (ctx->conn_pcb) {
  172. DEBUG_printf("Already have a connection\n");
  173. tcp_abort(client_pcb);
  174. return ERR_ABRT;
  175. }
  176. tcp_conn_init(ctx, client_pcb);
  177. return ERR_OK;
  178. }
  179. static err_t tcp_server_listen(struct tcp_ctx *ctx)
  180. {
  181. DEBUG_printf("Starting server at %s on port %u\n", ip4addr_ntoa(netif_ip4_addr(netif_list)), TCP_PORT);
  182. ctx->serv_done = false;
  183. struct tcp_pcb *pcb = tcp_new_ip_type(IPADDR_TYPE_ANY);
  184. if (!pcb) {
  185. DEBUG_printf("failed to create pcb\n");
  186. return ERR_MEM;
  187. }
  188. err_t err = tcp_bind(pcb, NULL, TCP_PORT);
  189. if (err) {
  190. DEBUG_printf("failed to bind to port %d\n", TCP_PORT);
  191. tcp_abort(pcb);
  192. return err;
  193. }
  194. ctx->serv_pcb = tcp_listen_with_backlog_and_err(pcb, 1, &err);
  195. if (!ctx->serv_pcb) {
  196. DEBUG_printf("failed to listen: %d\n", err);
  197. return err;
  198. }
  199. tcp_arg(ctx->serv_pcb, ctx);
  200. tcp_accept(ctx->serv_pcb, tcp_server_accept);
  201. return ERR_OK;
  202. }
  203. int main()
  204. {
  205. stdio_init_all();
  206. sleep_ms(1000);
  207. if (cyw43_arch_init()) {
  208. printf("failed to initialise\n");
  209. return 1;
  210. }
  211. cyw43_arch_enable_sta_mode();
  212. printf("Connecting to WiFi...\n");
  213. if (cyw43_arch_wifi_connect_timeout_ms(wifi_ssid, wifi_pass, CYW43_AUTH_WPA2_AES_PSK, 30000)) {
  214. printf("failed to connect.\n");
  215. return 1;
  216. } else {
  217. printf("Connected.\n");
  218. }
  219. struct tcp_ctx tcp = { 0 };
  220. for ( ; ; ) {
  221. err_t err = tcp_server_listen(&tcp);
  222. if (err != ERR_OK) {
  223. printf("Failed to start server: %d\n", err);
  224. sleep_ms(1000);
  225. continue;
  226. }
  227. while (!tcp.serv_done) {
  228. sleep_ms(1000);
  229. }
  230. }
  231. cyw43_arch_deinit();
  232. return 0;
  233. }