From bccf187c1a5af8c8dad7f5b9b2eacfd6e04b47fc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?King=20K=C3=A9vin?= Date: Thu, 14 Jul 2022 18:56:36 +0200 Subject: [PATCH] esp8266: add listening to port --- lib/radio_esp8266.c | 17 +++++++++++++++++ lib/radio_esp8266.h | 6 ++++++ 2 files changed, 23 insertions(+) diff --git a/lib/radio_esp8266.c b/lib/radio_esp8266.c index ff281f4..7e51580 100644 --- a/lib/radio_esp8266.c +++ b/lib/radio_esp8266.c @@ -116,6 +116,23 @@ void radio_esp8266_tcp_open(char* host, uint16_t port) } } +bool radio_esp8266_listen(bool udp, uint16_t port) +{ + char command[256] = {0}; // string to create command + int length = snprintf(command, LENGTH(command), "AT+CIPSTART=\"%s\",\"0.0.0.0\",0,%u\r\n", udp ? "UDP" : "TCP", port); // create AT command to establish a listening connection + if (!length) { + return false; + } + radio_esp8266_transmit((uint8_t*)command, length); + while (!radio_esp8266_activity || !radio_esp8266_success) { // wait for response + __WFI(); // sleep until something happened + } + if (!radio_esp8266_success) { // send AT command did not succeed + return false; + } + return true; +} + void radio_esp8266_send(uint8_t* data, uint8_t length) { char command[16 + 1] = {0}; // string to create command diff --git a/lib/radio_esp8266.h b/lib/radio_esp8266.h index fcd2326..aa80a1e 100644 --- a/lib/radio_esp8266.h +++ b/lib/radio_esp8266.h @@ -22,6 +22,12 @@ void radio_esp8266_setup(void); * @note wait for activity to get success status */ void radio_esp8266_tcp_open(char* host, uint16_t port); +/** open listening connection + * @param[in] udp if it's an UDP or TCP connection + * @param[in] port port to listen to + * @return if operation succeeded + */ +bool radio_esp8266_listen(bool udp, uint16_t port); /** send data (requires established connection) * @param[in] data data to send * @param[in] length size of data to send