Refactor Web Server

This commit is contained in:
dark98
2026-09-03 02:12:07 +01:00
parent 7bde605438
commit ee046a001e
8 changed files with 271 additions and 78 deletions
+6 -76
View File
@@ -1,34 +1,23 @@
#include "sys_wifi.h"
#include "web_server.h"
#include "os_event.h"
#include "os_status.h"
#include "sdkconfig.h"
#include "esp_event.h"
#include "esp_http_server.h"
#include "esp_log.h"
#include "esp_netif.h"
#include "esp_ota_ops.h"
#include "esp_wifi.h"
#include "nvs.h"
#include "nvs_flash.h"
#include <ctype.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
static const char *TAG = "SYS_WIFI";
static esp_netif_t *s_sta_netif;
static esp_netif_t *s_ap_netif;
static httpd_handle_t s_http_server;
static char s_sta_ssid[33];
static char s_sta_password[65];
static void ota_reboot_task(void *arg)
{
(void)arg;
vTaskDelay(pdMS_TO_TICKS(1000));
esp_restart();
}
static void copy_wifi_string(uint8_t *destination, size_t destination_size, const char *source)
{
size_t length = strlen(source);
@@ -43,18 +32,6 @@ static void set_status(wifi_state_t state, int8_t rssi)
sys_event_post(SYS_EVENT_WIFI_STATUS_CHANGED, (int32_t)state, NULL);
}
static esp_err_t save_credentials(const char *ssid, const char *password)
{
nvs_handle_t nvs;
esp_err_t err = nvs_open("wifi", NVS_READWRITE, &nvs);
if (err != ESP_OK) return err;
err = nvs_set_str(nvs, "ssid", ssid);
if (err == ESP_OK) err = nvs_set_str(nvs, "password", password);
if (err == ESP_OK) err = nvs_commit(nvs);
nvs_close(nvs);
return err;
}
static esp_err_t load_credentials(void)
{
nvs_handle_t nvs;
@@ -68,56 +45,7 @@ static esp_err_t load_credentials(void)
return err;
}
static int hex_value(char value)
{
if (value >= '0' && value <= '9') return value - '0';
if (value >= 'a' && value <= 'f') return value - 'a' + 10;
if (value >= 'A' && value <= 'F') return value - 'A' + 10;
return -1;
}
static bool form_value(const char *body, const char *key, char *output, size_t output_size)
{
size_t key_len = strlen(key);
const char *cursor = body;
size_t output_len = 0;
while (cursor && *cursor) {
if ((cursor == body || cursor[-1] == '&') && strncmp(cursor, key, key_len) == 0 && cursor[key_len] == '=') {
cursor += key_len + 1;
while (*cursor && *cursor != '&') {
int value = 0;
if (*cursor == '+') {
value = ' ';
cursor++;
} else if (*cursor == '%' && hex_value(cursor[1]) >= 0 && hex_value(cursor[2]) >= 0) {
value = (hex_value(cursor[1]) << 4) | hex_value(cursor[2]);
cursor += 3;
} else {
value = (unsigned char)*cursor++;
}
if (output_len + 1 >= output_size) return false;
output[output_len++] = (char)value;
}
output[output_len] = '\0';
return output_len > 0;
}
cursor = strchr(cursor, '&');
if (cursor) cursor++;
}
return false;
}
static void append_json_string(httpd_req_t *request, const uint8_t *value, size_t length)
{
for (size_t i = 0; i < length && value[i] != '\0'; i++) {
char escaped[2] = {(char)value[i], '\0'};
if (value[i] == '"' || value[i] == '\\') {
httpd_resp_send_chunk(request, "\\", 1);
}
httpd_resp_send_chunk(request, escaped, 1);
}
}
#if 0
static esp_err_t provisioning_scan(httpd_req_t *request)
{
wifi_scan_config_t scan_config = {
@@ -342,6 +270,8 @@ static void start_provisioning_server(void)
httpd_register_uri_handler(s_http_server, &post_uri);
}
#endif
static void start_softap(void)
{
wifi_config_t ap_config = {0};
@@ -353,7 +283,7 @@ static void start_softap(void)
esp_wifi_set_config(WIFI_IF_AP, &ap_config);
esp_wifi_start();
set_status(WIFI_STATE_AP_MODE, 0);
start_provisioning_server();
web_server_start();
ESP_LOGI(TAG, "Provisioning AP '%s', browse to http://192.168.4.1", CONFIG_SYS_WIFI_AP_SSID);
}
@@ -380,7 +310,7 @@ static void wifi_event_handler(void *arg, esp_event_base_t event_base, int32_t e
int8_t rssi = 0;
if (esp_wifi_sta_get_ap_info(&record) == ESP_OK) rssi = record.rssi;
set_status(WIFI_STATE_CONNECTED, rssi);
start_provisioning_server();
web_server_start();
const ip_event_got_ip_t *got_ip = (const ip_event_got_ip_t *)event_data;
ESP_LOGI(TAG, "Wi-Fi connected, web server at http://" IPSTR, IP2STR(&got_ip->ip_info.ip));
}