diff options
Diffstat (limited to 'esp32/main')
| -rw-r--r-- | esp32/main/main.c | 54 |
1 files changed, 24 insertions, 30 deletions
diff --git a/esp32/main/main.c b/esp32/main/main.c index 758a71a..e9b26a3 100644 --- a/esp32/main/main.c +++ b/esp32/main/main.c @@ -27,21 +27,27 @@ #define I2S_SD GPIO_NUM_1 #define I2S_SCK GPIO_NUM_5 +static int sock; +static size_t sock_addr_len; +static struct sockaddr *sock_addr; + static i2s_chan_handle_t chan; -static char *data = calloc(1, BUFLEN + 1); static void i2s_read_task(void *args) { + int rc; size_t n; - char *buf = calloc(1, BUFLEN); + char *buf = calloc(1, BUFLEN + 1); ESP_ERROR_CHECK(i2s_channel_enable(chan)); for (;;) { if (i2s_channel_read(chan, buf, BUFLEN, &n, 1000) == ESP_OK) { if (n > 0) { - memcpy(data, buf, n); - data[n] = '\0'; + buf[n] = '\0'; + rc = sendto(sock, s, strlen(s), 0, sock_addr, sock_addr_len); + if (rc < 0) + ESP_LOGE(TAG, "Error occurred during sending: errno %d", errno); } } else printf("Read Task: i2s read failed\n"); @@ -81,23 +87,18 @@ static inline void i2s_init(void) ESP_ERROR_CHECK(i2s_channel_init_std_mode(chan, &std_cfg)); } -static void udp_send_task(void *args) +static void udp_client_init(void) { const int family = AF_INET; const int port = CONFIG_UDP_PORT; const char[] ip_addr = CONFIG_UDP_ADDR; - int sock; struct timeval timeout; - struct sockaddr_in addr; + struct sockaddr_in *dest_addr; addr_family = AF_INET; - dest_addr.sin_addr.s_addr = inet_addr(ip_addr); - dest_addr.sin_family = family; - dest_addr.sin_port = htons(port); - - int sock = socket(family, SOCK_DGRAM, IPPROTO_IP); + sock = socket(family, SOCK_DGRAM, IPPROTO_IP); if (sock < 0) { ESP_LOGE(TAG, "Unable to create socket: errno %d", errno); @@ -108,25 +109,19 @@ static void udp_send_task(void *args) timeout.tv_usec = 0; setsockopt(sock, SOL_SOCKET, SO_RCVTIMEO, &timeout, sizeof timeout); - ESP_LOGI(TAG, "Socket created, sending to %s:%d", ip_addr, port); + if (!(dest_addr = malloc(sizeof(dest_addr)))) { + ESP_LOGE(TAG, "malloc() failed for destination address"); + return; + } - for (;;) { - int err = sendto(sock, data, strlen(data), 0, - (struct sockaddr *)&addr, sizeof addr); + dest_addr->sin_addr.s_addr = inet_addr(ip_addr); + dest_addr->sin_family = family; + dest_addr->sin_port = htons(port); - if (err < 0) { - ESP_LOGE(TAG, "Error occurred during sending: errno %d", errno); - break; - } + sock_addr = (struct sock_addr *) dest_addr; + sock_addr_len = sizeof dest_addr; - if (sock != -1) { - ESP_LOGE(TAG, "Shutting down socket and restarting..."); - shutdown(sock, 0); - close(sock); - } - } - - vTaskDelete(NULL); + ESP_LOGI(TAG, "Socket created, sending to %s:%d", ip_addr, port); } void app_main(void) @@ -135,10 +130,9 @@ void app_main(void) ESP_ERROR_CHECK(esp_event_loop_create_default()); wifi_connect(); - i2s_init(); + i2s_init(); xTaskCreate(i2s_read_task, "i2s_read_task", 4096, NULL, 5, NULL); - xTaskCreate(udp_send_task, "udp_send_task", 4096, NULL, 5, NULL); for (;;) vTaskDelay(500 / portTICK_PERIOD_MS); |
