diff options
| author | Sadeep Madurange <sadeep@asciimx.com> | 2024-09-08 16:41:32 +0800 |
|---|---|---|
| committer | Sadeep Madurange <sadeep@asciimx.com> | 2024-09-08 16:41:32 +0800 |
| commit | d34a707a58aa3f803d49b930072d7a76348212d6 (patch) | |
| tree | 7a95424926bf3b82cb67132b253877a23348b267 /esp32/main/main.c | |
| parent | 65a58c9a20b87c630d617a472a4a84d3b794e665 (diff) | |
| download | esp32-inmp441-driver-d34a707a58aa3f803d49b930072d7a76348212d6.tar.gz | |
Minor improvements like error instead of errno.
Diffstat (limited to 'esp32/main/main.c')
| -rw-r--r-- | esp32/main/main.c | 70 |
1 files changed, 38 insertions, 32 deletions
diff --git a/esp32/main/main.c b/esp32/main/main.c index 7ddcf5e..d18b137 100644 --- a/esp32/main/main.c +++ b/esp32/main/main.c @@ -1,5 +1,4 @@ #include <errno.h> -#include <sys/param.h> #include <freertos/FreeRTOS.h> #include <freertos/task.h> @@ -7,7 +6,6 @@ #include <esp_event.h> #include <esp_log.h> #include <esp_netif.h> -#include <esp_system.h> #include <nvs_flash.h> #include <driver/gpio.h> @@ -29,6 +27,10 @@ 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) @@ -37,39 +39,12 @@ 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; - - 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); - - 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); - - 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, (struct sockaddr *)&dest_addr, - sizeof(dest_addr)); + rc = sendto(sock, buf, n, 0, sock_addr, sock_addr_len); if (rc < 0) ESP_LOGE(TAG, "sendto() failed: %s", strerror(errno)); } @@ -113,6 +88,39 @@ static inline void i2s_init(void) 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) @@ -130,5 +138,3 @@ void app_main(void) for (;;) vTaskDelay(500 / portTICK_PERIOD_MS); } - - |
