diff options
| author | Edvin <[email protected]> | 2025-12-22 11:14:02 +0100 |
|---|---|---|
| committer | Edvin <[email protected]> | 2025-12-22 11:14:02 +0100 |
| commit | 93a45dacb2a2f7179baa0fdf89d6390518abb81a (patch) | |
| tree | 081687018f93cfad16b52e2333dadc0934b377ac /main | |
| parent | d64ce2dc70a07285bb3222e69463e11385fc9e27 (diff) | |
Final commit
Diffstat (limited to 'main')
| -rw-r--r-- | main/CDIO.c | 328 |
1 files changed, 253 insertions, 75 deletions
diff --git a/main/CDIO.c b/main/CDIO.c index bc77561..8e76b64 100644 --- a/main/CDIO.c +++ b/main/CDIO.c @@ -4,74 +4,29 @@ #include "driver/uart.h" #include "EZADC.h" - -#include "rgb_led.h" - #include "EZWIFI.h" +#include "rgb_led.h" -rgb_led MY_LED; - -char *data = (char *) "1\n"; - -void socket_transmitter_sta_loop(bool (*is_wifi_connected)()) { - int socket_fd = -1; - while (1) { - close(socket_fd); - char *ip = (char *) "192.168.4.1"; - struct sockaddr_in caddr; - caddr.sin_family = AF_INET; - caddr.sin_port = htons(2223); - while (!is_wifi_connected()) { - // wait until connected to AP - printf("wifi not connected. waiting...\n"); - vTaskDelay(1000 / portTICK_PERIOD_MS); - } - printf("initial wifi connection established.\n"); - if (inet_aton(ip, &caddr.sin_addr) == 0) { - printf("ERROR: inet_aton\n"); - continue; - } +#define AP 1 - socket_fd = socket(PF_INET, SOCK_DGRAM, 0); - if (socket_fd == -1) { - printf("ERROR: Socket creation error [%s]\n", strerror(errno)); - continue; - } - if (connect(socket_fd, (const struct sockaddr *) &caddr, sizeof(struct sockaddr)) == -1) { - printf("ERROR: socket connection error [%s]\n", strerror(errno)); - continue; - } +#define SSID "CDIO" - printf("sending frames.\n"); - int i = 0; - while (1) { - //double start_time = get_steady_clock_timestamp(); - if (!is_wifi_connected()) { - printf("ERROR: wifi is not connected\n"); - break; - } +#define CONFIG_WIFI_BANDWIDTH WIFI_BW_HT40 +#define CONFIG_SEND_FREQUENCY 20 +#define CONFIG_LESS_INTERFERENCE_CHANNEL 11 - if (sendto(socket_fd, &data, strlen(data), 0, (const struct sockaddr *) &caddr, sizeof(caddr)) != - strlen(data)) { - vTaskDelay(1); - continue; - } +#define PORT 3333 +#define KEEPALIVE_IDLE 1 +#define KEEPALIVE_INTERVAL 1 +#define KEEPALIVE_COUNT 1 - vTaskDelay(pdMS_TO_TICKS(100)); +static const char *TAG = "CDIO CSI"; - //double end_time = get_steady_clock_timestamp(); - //lag = end_time - start_time; - } - } -} +static const char *payload = "ESP"; -/* TaskHandle_t xHandle = NULL; */ +static int port_iterate; -/* void vTask_socket_transmitter_sta_loop(void *pvParamteres) { */ -/* for(;;) { */ -/* socket_transmitter_sta_loop(&is_wifi_connected); */ -/* } */ -/* } */ +rgb_led MY_LED; static void echo_task(void *arg) { @@ -123,31 +78,254 @@ static void echo_task(void *arg) } } +void battery_task (void *pvParameters) +{ + int battery_voltage; + ADC MY_ADC; + init_adc(&MY_ADC); -void app_main(void) + config_adc(&MY_ADC, 7); + + while(1) + { + battery_voltage = ez_read(&MY_ADC); + //ESP_LOGI(TAG, "Battery voltage: %d", battery_voltage); + + if(battery_voltage > 2000) { + rgb_set_color(&MY_LED, rgb_green); + } + else if(battery_voltage > 1800) { + rgb_set_color(&MY_LED, rgb_yellow); + } + else { + rgb_set_color(&MY_LED, rgb_red); + } + + vTaskDelay(pdMS_TO_TICKS(1000)); + } +} + +static void do_retransmit(const int sock) +{ + int len; + char rx_buffer[128]; + int8_t csi_buffer[256]; + wifi_csi_info_t *info; + + do { + len = recv(sock, rx_buffer, sizeof(rx_buffer) - 1, 0); + if (len < 0) { + ESP_LOGE(TAG, "Error occurred during receiving: errno %d", errno); + } else if (len == 0) { + ESP_LOGW(TAG, "Connection closed"); + } else { + rx_buffer[len] = 0; // Null-terminate whatever is received and treat it like a string + ESP_LOGI(TAG, "Received %d bytes: %s", len, rx_buffer); + + if (strcmp(rx_buffer, "CSI") == 0) + { + info = get_csi(); + + wifi_csi_info_t d = info[0]; + char mac[20] = {0}; + sprintf(mac,"%02X:%02X:%02X:%02X:%02X:%02X", d.mac[0], d.mac[1], d.mac[2], d.mac[3], d.mac[4], d.mac[5]); + + ets_printf("MAC: %s\nLength: %d\n", mac, info->len); + + send(sock, info->buf, sizeof(info->buf), 0); + } + // else + // { + // send() can return less bytes than supplied length. + // Walk-around for robust implementation. + int to_write = len; + while (to_write > 0) { + int written = send(sock, rx_buffer + (len - to_write), to_write, 0); + if (written < 0) { + ESP_LOGE(TAG, "Error occurred during sending: errno %d", errno); + // Failed to retransmit, giving up + return; + } + to_write -= written; + // } + } + } + } while (len > 0); +} + +static void tcp_server_task(void *pvParameters) +{ + char addr_str[128]; + int addr_family = (int)pvParameters; + int ip_protocol = 0; + int keepAlive = 1; + int keepIdle = KEEPALIVE_IDLE; + int keepInterval = KEEPALIVE_INTERVAL; + int keepCount = KEEPALIVE_COUNT; + struct sockaddr_storage dest_addr; + + if (addr_family == AF_INET) { + struct sockaddr_in *dest_addr_ip4 = (struct sockaddr_in *)&dest_addr; + dest_addr_ip4->sin_addr.s_addr = htonl(INADDR_ANY); + dest_addr_ip4->sin_family = AF_INET; + dest_addr_ip4->sin_port = htons(PORT + port_iterate); + ip_protocol = IPPROTO_IP; + } + + int listen_sock = socket(addr_family, SOCK_STREAM, ip_protocol); + if (listen_sock < 0) { + ESP_LOGE(TAG, "Unable to create socket: errno %d", errno); + vTaskDelete(NULL); + return; + } + int opt = 1; + setsockopt(listen_sock, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt)); + + ESP_LOGI(TAG, "Socket created"); + + int err = bind(listen_sock, (struct sockaddr *)&dest_addr, sizeof(dest_addr)); + if (err != 0) { + ESP_LOGE(TAG, "Socket unable to bind: errno %d", errno); + ESP_LOGE(TAG, "IPPROTO: %d", addr_family); + goto CLEAN_UP; + } + ESP_LOGI(TAG, "Socket bound, port %d", PORT); + + err = listen(listen_sock, 1); + if (err != 0) { + ESP_LOGE(TAG, "Error occurred during listen: errno %d", errno); + goto CLEAN_UP; + } + + while (1) { + + ESP_LOGI(TAG, "Socket listening"); + + struct sockaddr_storage source_addr; // Large enough for both IPv4 or IPv6 + socklen_t addr_len = sizeof(source_addr); + int sock = accept(listen_sock, (struct sockaddr *)&source_addr, &addr_len); + if (sock < 0) { + ESP_LOGE(TAG, "Unable to accept connection: errno %d", errno); + break; + } + + // Set tcp keepalive option + setsockopt(sock, SOL_SOCKET, SO_KEEPALIVE, &keepAlive, sizeof(int)); + setsockopt(sock, IPPROTO_TCP, TCP_KEEPIDLE, &keepIdle, sizeof(int)); + setsockopt(sock, IPPROTO_TCP, TCP_KEEPINTVL, &keepInterval, sizeof(int)); + setsockopt(sock, IPPROTO_TCP, TCP_KEEPCNT, &keepCount, sizeof(int)); + // Convert ip address to string + if (source_addr.ss_family == PF_INET) { + inet_ntoa_r(((struct sockaddr_in *)&source_addr)->sin_addr, addr_str, sizeof(addr_str) - 1); + } + + ESP_LOGI(TAG, "Socket accepted ip address: %s", addr_str); + + do_retransmit(sock); + + shutdown(sock, 0); + close(sock); + } + +CLEAN_UP: + close(listen_sock); + vTaskDelete(NULL); +} + +void tcp_client_task(void *pvParameters) { + char rx_buffer[128]; + char host_ip[] = "192.168.4.1"; + int addr_family = 0; + int ip_protocol = 0; + + while(1) + { + struct sockaddr_in dest_addr; + inet_pton(AF_INET, host_ip, &dest_addr.sin_addr); + dest_addr.sin_family = AF_INET; + dest_addr.sin_port = htons(PORT); + addr_family = AF_INET; + ip_protocol = IPPROTO_IP; + + int connected = 0; + while(!connected) + { + connected = is_wifi_connected(); + vTaskDelay(pdTICKS_TO_MS(1000)); + } + + int sock = socket(addr_family, SOCK_STREAM, ip_protocol); + if (sock < 0) { + ESP_LOGE(TAG, "Unable to create socket: errno %d", errno); + break; + } + ESP_LOGI(TAG, "Socket created, connecting to %s:%d", host_ip, PORT); + + int err = connect(sock, (struct sockaddr *)&dest_addr, sizeof(dest_addr)); + if (err != 0) { + ESP_LOGE(TAG, "Socket unable to connect: errno %d", errno); + break; + } + ESP_LOGI(TAG, "Successfully connected"); + + while (1) { + int err = send(sock, payload, strlen(payload), 0); + if (err < 0) { + ESP_LOGE(TAG, "Error occurred during sending: errno %d", errno); + break; + } + + int len = recv(sock, rx_buffer, sizeof(rx_buffer) - 1, 0); + // Error occurred during receiving + if (len < 0) { + ESP_LOGE(TAG, "recv failed: errno %d", errno); + break; + } + // Data received + else { + rx_buffer[len] = 0; // Null-terminate whatever we received and treat like a string + ESP_LOGI(TAG, "Received %d bytes from %s:", len, host_ip); + ESP_LOGI(TAG, "%s", rx_buffer); + } + vTaskDelay(pdTICKS_TO_MS(100)); + } + + if (sock != -1) { + ESP_LOGE(TAG, "Shutting down socket and restarting..."); + shutdown(sock, 0); + close(sock); + } + } +} + +void app_main(void) +{ // Init LED rgb_init_LED(&MY_LED, 27, 12, 13); rgb_set_color(&MY_LED, rgb_black); - xTaskCreate(echo_task, "uart_echo_task", 3072, NULL, 10, NULL); + xTaskCreate(echo_task, "uart_echo", 3072, NULL, 10, NULL); + + xTaskCreate(battery_task, "battery", 2048, NULL, 1, NULL); - // Access point - setup_softap(); +#if AP + setup_softap(); - setup_csi(); - - for(;;) { - vTaskDelay(10); - } + setup_csi("F8:B3:B7:5A:34:F4"); - // Station - /* setup_station(); */ + for (int i = 0;i < 2;i++) + { + port_iterate = i; + xTaskCreate(tcp_server_task, "tcp_server", 4096, (void*)AF_INET, 5, NULL); + vTaskDelay(pdTICKS_TO_MS(100)); + } +#else + setup_station(); - /* setup_csi(); */ - - /* for(;;) { */ - /* socket_transmitter_sta_loop(&is_wifi_connected); */ - /* } */ + // setup_csi("F0:24:F9:54:3B:89"); + + xTaskCreate(tcp_client_task, "tcp_client", 4096, (void*)AF_INET, 5, NULL); +#endif } |
