From 451a260cd03f524c0288829757772ea07fbc87e6 Mon Sep 17 00:00:00 2001 From: msp Date: Wed, 26 Oct 2022 10:33:30 -0400 Subject: [PATCH] git things working over TCP, still working on UDP --- main/espd.c | 2 +- main/espd.h | 2 ++ wifi/net.c | 79 ++++++++++++++++++++++++++++++++++++++++++++--------- wifi/wifi.c | 4 +++ 4 files changed, 73 insertions(+), 14 deletions(-) diff --git a/main/espd.c b/main/espd.c index 692a43b..b249e28 100644 --- a/main/espd.c +++ b/main/espd.c @@ -179,7 +179,6 @@ void pdmain_print( const char *s) } void trymem(int foo); -void net_alive( void); /* allow deprecated form if new one unavailable */ #ifndef I2S_COMM_FORMAT_STAND_I2S @@ -202,6 +201,7 @@ void app_main(void) ESP_LOGI(TAG, "[ 1a ] start network"); wifi_init(); net_init(); + net_hello(); #endif ESP_LOGI(TAG, "[ 2 ] now write some shit"); diff --git a/main/espd.h b/main/espd.h index 5556f20..07ca284 100644 --- a/main/espd.h +++ b/main/espd.h @@ -24,7 +24,9 @@ void pd_bt_writeback(unsigned char *s, int length); #ifdef PD_USE_WIFI void wifi_init(void); /* wifi.c - manage 802.11 connection */ void net_init( void); /* init */ +void net_hello( void); /* send initial TCP packet when connected */ void net_alive( void); /* send keep-alive packet if needed */ void net_sendudp(void *msg, int len, int port); /* send whatev */ void net_sendtcp(void *msg, int len); +extern char wifi_ipaddr[]; #endif diff --git a/wifi/net.c b/wifi/net.c index 555e0b5..a7b48ae 100644 --- a/wifi/net.c +++ b/wifi/net.c @@ -25,7 +25,6 @@ void tcpreceivertask(void *z) struct sockaddr_in dest_addr; ESP_LOGI(TAG, "tcpreceivertask starting..."); - ESP_LOGE(TAG, "(ignore this - testing error printout)"); dest_addr.sin_addr.s_addr = inet_addr(CONFIG_ESP_WIFI_SENDADDR); dest_addr.sin_family = AF_INET; dest_addr.sin_port = htons(CONFIG_ESP_WIFI_SENDPORT); @@ -71,11 +70,63 @@ void tcpreceivertask(void *z) } } +void udpreceivertask(void *z) +{ + char rx_buffer[1000]; + int ip_protocol = 0, err, newsocket; + struct sockaddr_in dest_addr; + + ESP_LOGI(TAG, "udpreceivertask starting..."); + dest_addr.sin_addr.s_addr = inet_addr(CONFIG_ESP_WIFI_SENDADDR); + dest_addr.sin_family = AF_INET; + dest_addr.sin_port = htons(CONFIG_ESP_WIFI_SENDPORT); + + newsocket = socket(AF_INET, SOCK_DGRAM, IPPROTO_IP); + if (newsocket < 0) { + ESP_LOGE(TAG, "Unable to create socket: errno %d", errno); + return; + } + + ESP_LOGI(TAG, "udp connecting..."); + while (err = connect(newsocket, &dest_addr, sizeof(dest_addr)) < 0) + { + ESP_LOGE(TAG, "Socket unable to bind: errno %d - retrying", errno); + close(newsocket); + vTaskDelay(2000 / portTICK_PERIOD_MS); + newsocket = socket(AF_INET, SOCK_DGRAM, IPPROTO_IP); + if (newsocket < 0) { + ESP_LOGE(TAG, "Unable to create socket: errno %d", errno); + return; + } + } + while (1) + { + int len = recv(newsocket, rx_buffer, sizeof(rx_buffer) - 1, 0); + + if (len < 0) { + ESP_LOGE(TAG, "recvfrom failed: errno %d -- restarting", errno); + esp_restart(); + } + else if (len == 0) + { + ESP_LOGE(TAG, "unexpected EOF on socket"); + vTaskDelay(500 / portTICK_PERIOD_MS); + } + else + { + rx_buffer[len] = 0; + ESP_LOGI(TAG, "rcv: %s", rx_buffer); + pd_fromhost(rx_buffer, strlen(rx_buffer)); + } + } +} + static int udp_out_sock; static struct sockaddr_in udp_out_addr; void net_init( void) { + esp_log_level_set(TAG, ESP_LOG_INFO); ESP_LOGI(TAG, "net_init..."); /* socket for sending UDP messages */ udp_out_sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_IP); @@ -85,7 +136,8 @@ void net_init( void) /* this will get overridden later: */ udp_out_addr.sin_port = htons(CONFIG_ESP_WIFI_SENDPORT); - xTaskCreate(tcpreceivertask, "udprcv", 6000, NULL, PRIORITY_WIFI, NULL); + xTaskCreate(tcpreceivertask, "tcprcv", 6000, NULL, PRIORITY_WIFI, NULL); + xTaskCreate(udpreceivertask, "udprcv", 3000, NULL, PRIORITY_WIFI, NULL); while (!tcp_socket) { ESP_LOGE(TAG, "sendtcp: waiting for socket"); @@ -135,23 +187,24 @@ void net_sendtcp(void *msg, int len) } } -extern char wifi_mac[]; -/* esp_err_t esp_base_mac_addr_get(uint8_t *mac); */ +void net_hello( void) +{ + int elapsed = esp_timer_get_time() - whensent; + char buf[80]; + uint8_t mac[6]; + { + esp_base_mac_addr_get(&mac); + sprintf(buf, "hello %02x:%02x:%02x:%02x:%02x:%02x %s;\n", + mac[0],mac[1],mac[2],mac[3],mac[4],mac[5], wifi_ipaddr); + net_sendtcp(buf, strlen(buf)); + } +} void net_alive( void) { int elapsed = esp_timer_get_time() - whensent; char buf[80]; uint8_t mac[6]; - static int notfirst; - if (!notfirst) - { - notfirst = 1; - esp_base_mac_addr_get(&mac); - sprintf(buf, "hello %02x:%02x:%02x:%02x:%02x:%02x;\n", - mac[0],mac[1],mac[2],mac[3],mac[4],mac[5]); - net_sendtcp(buf, strlen(buf)); - } if (elapsed > 1000000) { esp_base_mac_addr_get(&mac); diff --git a/wifi/wifi.c b/wifi/wifi.c index d3a23a3..8dc6a14 100644 --- a/wifi/wifi.c +++ b/wifi/wifi.c @@ -35,6 +35,7 @@ static EventGroupHandle_t s_wifi_event_group; static const char *TAG = "ESPD"; static int s_retry_num = 0; char wifi_mac[80]; +char wifi_ipaddr[20]; static void event_handler(void* arg, esp_event_base_t event_base, int32_t event_id, void* event_data) @@ -53,6 +54,9 @@ static void event_handler(void* arg, esp_event_base_t event_base, } else if (event_base == IP_EVENT && event_id == IP_EVENT_STA_GOT_IP) { ip_event_got_ip_t* event = (ip_event_got_ip_t*) event_data; ESP_LOGI(TAG, "got ip:" IPSTR, IP2STR(&event->ip_info.ip)); + snprintf(wifi_ipaddr, sizeof(wifi_ipaddr), + IPSTR, IP2STR(&event->ip_info.ip)); + wifi_ipaddr[sizeof(wifi_ipaddr)-1] = 0; s_retry_num = 0; xEventGroupSetBits(s_wifi_event_group, WIFI_CONNECTED_BIT); }