// // Created by jedi on 25.06.21. // #include "web.h" #include "system.h" #include "lux.h" #include "wifi.h" #include #include #include extern "C" { #include #include } #include #include extern "C" { #include } #define vTaskDelayMs(ms) vTaskDelay((ms) / portTICK_PERIOD_MS) struct { bool global; bool connection; bool wifi; } has_changed; void websocket_task(void *pvParameter) { auto *pcb = (struct tcp_pcb *) pvParameter; size_t connstarttime = xTaskGetTickCount(); has_changed = {true, true, true}; for (;;) { if(pcb == nullptr || pcb->state != ESTABLISHED) { printf("Connection closed, deleting task\n"); break; } //Global Info if(has_changed.global) { has_changed.global = false; timeval tv{}; gettimeofday(&tv, nullptr); size_t uptime = xTaskGetTickCount() * portTICK_PERIOD_MS / 1000; int heap = (int) xPortGetFreeHeapSize(); uint32_t chip_id = sdk_system_get_chip_id(); uint32_t flash_id = sdk_spi_flash_get_id(); uint32_t flash_size = sdk_flashchip.chip_size >> 10; char *hostname = nullptr; sysparam_get_string("hostname", &hostname); /* Generate response in JSON format */ char response[160]; size_t len = snprintf(response, sizeof(response), "{\"walltime\" : \"%d\"," "\"uptime\" : \"%d\"," " \"heap\" : \"%d\"," " \"chipid\" : \"%08x\"," " \"flashid\" : \"0x%08x\"," " \"flashsize\" : \"%u\"," " \"hostname\" : \"%s\"" "}", (int) tv.tv_sec, uptime, heap, chip_id, flash_id, flash_size, hostname); free(hostname); if(len < sizeof(response)) { LOCK_TCPIP_CORE(); websocket_write(pcb, (unsigned char *) response, len, WS_TEXT_MODE); UNLOCK_TCPIP_CORE(); } else printf("buffer too small 1"); vTaskDelayMs(2000); } //Connection Info if(has_changed.connection) { has_changed.connection = false; timeval tv{}; gettimeofday(&tv, nullptr); size_t connuptime = (xTaskGetTickCount() - connstarttime) * portTICK_PERIOD_MS / 1000; printf("conn %d: " IPSTR " <-> " IPSTR " \n", pcb->netif_idx, IP2STR(&pcb->local_ip), IP2STR(&pcb->remote_ip)); char response[160]; size_t len = snprintf(response, sizeof(response), "{\"connage\" : \"%d\"," "\"clientip\" : \"" IPSTR "\"" "}", connuptime, IP2STR(&pcb->remote_ip)); if(len < sizeof(response)) { LOCK_TCPIP_CORE(); websocket_write(pcb, (unsigned char *) response, len, WS_TEXT_MODE); UNLOCK_TCPIP_CORE(); } else printf("buffer too small 1"); vTaskDelayMs(2000); } if(has_changed.wifi) { has_changed.wifi = false; uint8_t opmode = sdk_wifi_get_opmode(); const char *opmode_str = "??"; switch (opmode) { case NULL_MODE: opmode_str = "Null"; break; case STATION_MODE: opmode_str = "Station"; break; case SOFTAP_MODE: opmode_str = "SoftAP"; break; case STATIONAP_MODE: opmode_str = "StationAP"; break; default: break; } /*struct sockaddr_storage addr; socklen_t addr_len = sizeof(addr); if (getpeername(s, (struct sockaddr *)&addr, &addr_len) == 0) { printf("peer\n"); }*/ if(opmode == SOFTAP_MODE || opmode == STATIONAP_MODE) { uint8_t hwaddr[6]; sdk_wifi_get_macaddr(SOFTAP_IF, hwaddr); ip_info info{}; sdk_wifi_get_ip_info(SOFTAP_IF, &info); char *apssid = nullptr; sysparam_get_string("wifi_ap_ssid", &apssid); /* Generate response in JSON format */ char response[128]; size_t len = snprintf(response, sizeof(response), "{\"opmode\" : \"%s\"," " \"apssid\" : \"%s\"," " \"apip\" : \"" IPSTR "\"," " \"apmac\" : \"" MACSTR "\"" "}", opmode_str, apssid, IP2STR(&info.ip), MAC2STR(hwaddr)); free(apssid); if(len < sizeof(response)) { LOCK_TCPIP_CORE(); websocket_write(pcb, (unsigned char *) response, len, WS_TEXT_MODE); UNLOCK_TCPIP_CORE(); } else printf("buffer too small 2"); } vTaskDelayMs(2000); if(opmode == STATION_MODE || opmode == STATIONAP_MODE) { uint8_t hwaddr[6]; sdk_wifi_get_macaddr(STATION_IF, hwaddr); ip_info info{}; sdk_wifi_get_ip_info(STATION_IF, &info); char *stassid = nullptr; sysparam_get_string("wifi_sta_ssid", &stassid); /* Generate response in JSON format */ char response[128]; size_t len = snprintf(response, sizeof(response), "{\"opmode\" : \"%s\"," " \"stassid\" : \"%s\"," " \"staip\" : \"" IPSTR "\"," " \"stamac\" : \"" MACSTR "\"" "}", opmode_str, stassid, IP2STR(&info.ip), MAC2STR(hwaddr)); free(stassid); if(len < sizeof(response)) { LOCK_TCPIP_CORE(); websocket_write(pcb, (unsigned char *) response, len, WS_TEXT_MODE); UNLOCK_TCPIP_CORE(); } else printf("buffer too small 3"); } } vTaskDelayMs(250); { uint8_t response[3]; uint16_t val; val = sdk_system_adc_read(); response[2] = (uint8_t) val; response[1] = val >> 8; response[0] = 'V'; LOCK_TCPIP_CORE(); websocket_write(pcb, response, 3, WS_BIN_MODE); UNLOCK_TCPIP_CORE(); } vTaskDelayMs(500); } vTaskDelete(nullptr); } struct fw_frame { char t; uint8_t reserved[3]; uint16_t seq; uint16_t len; uint32_t hash; uint8_t data[]; } __attribute__((packed)); struct fw_check { char t; uint8_t reserved[3]; uint32_t len; uint32_t hash; } __attribute__((packed)); /** * This function is called when websocket frame is received. * * Note: this function is executed on TCP thread and should return as soon * as possible. */ void websocket_cb(struct tcp_pcb *pcb, char *data, u16_t data_len, uint8_t /*mode*/) { //mode should be WS_BIN_MODE or WS_TEXT_MODE uint8_t response[3]; uint16_t val = 0; char cmd = '0'; bool togl = 0; int8_t en = 0; /*uint8_t ap_disable_if_sta = 0; uint8_t ssid_hidden = 0; uint8_t dns_enable = 0; uint8_t mdns_enable = 0;*/ switch (data[0]) { case 'V': // ADC /* This should be done on a separate thread in 'real' applications */ val = sdk_system_adc_read(); cmd = 'V'; break; case 'R': // Restart cmd = 'R'; break; case 'X': // Clear Config cmd = 'X'; break; case 'D': // Disable LED signal_led(false); val = 1; cmd = 'G'; break; case 'E': // Enable LED signal_led(true); val = 0; cmd = 'G'; break; case 'F': togl = ~togl; signal_led(togl); { auto *f = (fw_frame *) data; if(f->seq == 0) { system_otaflash_init(); } val = system_otaflash_chunk(f->data, ntohs(f->len), ntohs(f->seq), ntohl(f->hash)); } cmd = 'F'; break; case 'C': signal_led(false); { auto *f = (fw_check *) data; val = system_otaflash_verify_and_switch(ntohl(f->len), ntohl(f->hash)); } cmd = 'C'; break; case 'S': { if(data[1] == 'E') en = 1; char *ssid = &data[2]; size_t ssid_len = strlen(ssid); char *password = &data[3 + ssid_len]; size_t password_len = strlen(password); (void) password_len; sysparam_set_int8("wifi_sta_enable", en); sysparam_set_string("wifi_sta_ssid", ssid); sysparam_set_string("wifi_sta_password", password); } cmd = 'S'; break; case 'A': { if(data[1] == 'E') en = 1; char *ssid = &data[2]; size_t ssid_len = strlen(ssid); char *password = &data[3 + ssid_len]; size_t password_len = strlen(password); (void) password_len; sysparam_set_int8("wifi_ap_enable", en); sysparam_set_string("wifi_ap_ssid", ssid); sysparam_set_string("wifi_ap_password", password); //sysparam_set_int8("wifi_ap_dns", dns_enable); //sysparam_set_int8("wifi_ap_mdns", mdns_enable); } cmd = 'A'; break; default: printf("[websocket_callback]:\n%.*s\n", (int) data_len, (char *) data); printf("Unknown command %c\n", data[0]); val = 0; break; } response[2] = (uint8_t) val; response[1] = val >> 8; response[0] = cmd; LOCK_TCPIP_CORE(); websocket_write(pcb, response, 3, WS_BIN_MODE); UNLOCK_TCPIP_CORE(); if(data[0] == 'R') { // Restart vTaskDelay(500 / portTICK_PERIOD_MS); vPortEnterCritical(); sdk_system_restart(); } else if(data[0] == 'X') { // Clear Config vTaskDelay(500 / portTICK_PERIOD_MS); system_clear_config(); } } /** * This function is called when new websocket is open and * creates a new websocket_task if requested URI equals '/stream'. */ void websocket_open_cb(struct tcp_pcb *pcb, const char *uri) { printf("WS URI: %s\n", uri); if(!strcmp(uri, "/stream")) { xTaskCreate(&websocket_task, "websocket_task", 512, (void *) pcb, 3, nullptr); } } extern "C" void httpd_task(void *pvParameters) { (void) pvParameters; while (!uxSemaphoreGetCount(wifi_available_semaphore)) vTaskDelay(500 / portTICK_PERIOD_MS); websocket_register_callbacks((tWsOpenHandler) websocket_open_cb, (tWsHandler) websocket_cb); httpd_init(); vTaskDelete(nullptr); }