From 04380ffc3353cecbbeac3d317f64bd11f215cffa Mon Sep 17 00:00:00 2001 From: Sadeep Madurange Date: Sun, 8 Sep 2024 17:01:11 +0800 Subject: wip --- esp32/main/main.c | 74 ++++++++++++++++++++++--------------------------------- 1 file changed, 30 insertions(+), 44 deletions(-) (limited to 'esp32/main/main.c') diff --git a/esp32/main/main.c b/esp32/main/main.c index d18b137..344a995 100644 --- a/esp32/main/main.c +++ b/esp32/main/main.c @@ -27,10 +27,6 @@ const char *TAG = "mimir"; -static int sock; -static size_t sock_addr_len; -static struct sockaddr *sock_addr; - static i2s_chan_handle_t chan; static void i2s_read_task(void *args) @@ -39,14 +35,43 @@ static void i2s_read_task(void *args) size_t n; char *buf = calloc(1, BUFLEN); + char *ip_addr; + int family, port; + struct timeval timeout; + struct sockaddr_in dest_addr; + + family = AF_INET; + port = CONFIG_UDP_PORT; + ip_addr = CONFIG_UDP_ADDR; + + int sock = socket(family, SOCK_DGRAM, IPPROTO_IP); + + if (sock < 0) { + ESP_LOGE(TAG, "Unable to create socket: errno %d", errno); + return; + } + + timeout.tv_sec = 10; + timeout.tv_usec = 0; + setsockopt(sock, SOL_SOCKET, SO_RCVTIMEO, &timeout, sizeof timeout); + + dest_addr.sin_addr.s_addr = inet_addr(ip_addr); + dest_addr.sin_family = family; + dest_addr.sin_port = htons(port); + + ESP_LOGI(TAG, "Socket created, sending to %s:%d", ip_addr, port); + ESP_ERROR_CHECK(i2s_channel_enable(chan)); for (;;) { if (i2s_channel_read(chan, buf, BUFLEN, &n, 1000) == ESP_OK) { if (n > 0) { - rc = sendto(sock, buf, n, 0, sock_addr, sock_addr_len); + rc = sendto(sock, buf, n, 0, (struct sockaddr *) &dest_addr, + sizeof dest_addr); if (rc < 0) ESP_LOGE(TAG, "sendto() failed: %s", strerror(errno)); + + vTaskDelay(1 / portTICK_PERIOD_MS); } } else printf("Read Task: i2s read failed\n"); @@ -86,52 +111,13 @@ static inline void i2s_init(void) ESP_ERROR_CHECK(i2s_channel_init_std_mode(chan, &std_cfg)); } -static void udp_client_init(void) -{ - char *ip_addr; - int family, port; - struct timeval timeout; - struct sockaddr_in *dest_addr; - - family = AF_INET; - port = CONFIG_UDP_PORT; - ip_addr = CONFIG_UDP_ADDR; - - sock = socket(family, SOCK_DGRAM, IPPROTO_IP); - - if (sock < 0) { - ESP_LOGE(TAG, "Unable to create socket: errno %d", errno); - return; - } - - timeout.tv_sec = 10; - timeout.tv_usec = 0; - setsockopt(sock, SOL_SOCKET, SO_RCVTIMEO, &timeout, sizeof timeout); - - if (!(dest_addr = malloc(sizeof(dest_addr)))) { - ESP_LOGE(TAG, "malloc() failed for destination address"); - return; - } - - dest_addr->sin_addr.s_addr = inet_addr(ip_addr); - dest_addr->sin_family = family; - dest_addr->sin_port = htons(port); - - sock_addr = (struct sockaddr *) dest_addr; - sock_addr_len = sizeof dest_addr; - - ESP_LOGI(TAG, "Socket created, sending to %s:%d", ip_addr, port); -} - void app_main(void) { ESP_ERROR_CHECK(nvs_flash_init()); ESP_ERROR_CHECK(esp_event_loop_create_default()); wifi_connect(); - i2s_init(); - udp_client_init(); xTaskCreate(i2s_read_task, "i2s_read_task", 4096, NULL, 5, NULL); -- cgit v1.2.3