- Dual-Core Architecture: Dedicated 1000Hz USB gaming pipeline on Core 1 and BLE/Wi-Fi/Web telemetry on Core 0. - Multi-Input BLE Host: Parsers for Xbox Wireless, Google Stadia, 8BitDo (Ultimate/Pro2/SN30), and Generic HOGP gamepads. - Multi-Console USB Emulation: Nintendo Switch, PC DirectInput, PS3, PS4, Xbox 360 (libxsm3 cryptographic challenge-response), Original Xbox, and Couch Mouse/Keyboard. - Onboard Web Portal & Captive Portal: Real-time SVG gamepad visualizer, stick deadzone calibration, button combo remapping, and Wi-Fi manager. - Low-Latency Radio Management: Interactive SoftAP toggle to drop to 0-latency gaming mode. - Dual OTA Firmware Flashing: Browser-based wireless firmware updater and automated Python OTA script. - Strict Build Quality: Clean build under -Wall -Wextra with zero warnings or errors. - Versioning & Licensing: Semantic Versioning v1.0.0 and GNU GPLv3 with Commercial Restrictions.
1329 lines
46 KiB
C
1329 lines
46 KiB
C
#include "config_server.h"
|
|
#include "web_assets.h"
|
|
#include "dns_server.h"
|
|
#include "profile_manager.h"
|
|
#include "input_calibration.h"
|
|
#include "ble_hid_host.h"
|
|
#include "status_led.h"
|
|
#include "console_output/usb_gamepad.h"
|
|
|
|
#include "esp_wifi.h"
|
|
#include "esp_event.h"
|
|
#include "esp_netif.h"
|
|
#include "esp_http_server.h"
|
|
#include "esp_log.h"
|
|
#include "nvs_flash.h"
|
|
#include "nvs.h"
|
|
#include "mdns.h"
|
|
#include "esp_ota_ops.h"
|
|
#include "esp_app_desc.h"
|
|
#include "esp_timer.h"
|
|
#include <string.h>
|
|
#include <stdlib.h>
|
|
#include <ctype.h>
|
|
#include "freertos/FreeRTOS.h"
|
|
#include "freertos/task.h"
|
|
#include "freertos/semphr.h"
|
|
#include <lwip/sockets.h>
|
|
#include <lwip/tcp.h>
|
|
|
|
static const char *TAG = "CONFIG_SRV";
|
|
|
|
static esp_err_t httpd_open_fn(httpd_handle_t hd, int sockfd)
|
|
{
|
|
int enable = 1;
|
|
setsockopt(sockfd, IPPROTO_TCP, TCP_NODELAY, &enable, sizeof(enable));
|
|
return ESP_OK;
|
|
}
|
|
|
|
#define CONFIG_AP_SSID "MimiCon-Setup"
|
|
#define CONFIG_AP_CHANNEL 1
|
|
#define CONFIG_AP_MAX_CONN 4
|
|
|
|
static bool s_wifi_inited = false;
|
|
static bool s_mdns_inited = false;
|
|
static esp_netif_t *s_ap_netif = NULL;
|
|
static esp_netif_t *s_sta_netif = NULL;
|
|
static httpd_handle_t s_http_server = NULL;
|
|
|
|
static volatile bool s_is_active = false;
|
|
|
|
/* Wi-Fi STA State */
|
|
static bool s_sta_configured = false;
|
|
static bool s_sta_connected = false;
|
|
static char s_sta_ssid[33] = {0};
|
|
static char s_sta_ip[16] = {0};
|
|
|
|
static controller_state_t s_latest_state = {0};
|
|
static SemaphoreHandle_t s_state_mutex = NULL;
|
|
|
|
static TaskHandle_t s_config_task_handle = NULL;
|
|
typedef enum {
|
|
CONFIG_CMD_NONE,
|
|
CONFIG_CMD_START,
|
|
CONFIG_CMD_STOP,
|
|
CONFIG_CMD_TOGGLE,
|
|
} config_cmd_t;
|
|
static volatile config_cmd_t s_pending_cmd = CONFIG_CMD_NONE;
|
|
|
|
/* -------------------------------------------------------------------------- */
|
|
/* NVS and Helper Functions */
|
|
/* -------------------------------------------------------------------------- */
|
|
|
|
static bool json_get_string(const char *json, const char *key, char *dest, size_t max_len)
|
|
{
|
|
if (!json || !key || !dest || max_len == 0) return false;
|
|
dest[0] = '\0';
|
|
|
|
char search_key[64];
|
|
snprintf(search_key, sizeof(search_key), "\"%s\"", key);
|
|
const char *p = strstr(json, search_key);
|
|
if (!p) return false;
|
|
|
|
p += strlen(search_key);
|
|
while (*p && (*p == ' ' || *p == ':' || *p == '\t' || *p == '\r' || *p == '\n')) {
|
|
p++;
|
|
}
|
|
if (*p != '\"') return false;
|
|
p++; /* skip opening quote */
|
|
|
|
size_t i = 0;
|
|
while (*p && *p != '\"' && i + 1 < max_len) {
|
|
if (*p == '\\' && *(p + 1)) {
|
|
p++;
|
|
}
|
|
dest[i++] = *p++;
|
|
}
|
|
dest[i] = '\0';
|
|
return true;
|
|
}
|
|
|
|
static bool json_get_int(const char *json, const char *key, int *val)
|
|
{
|
|
if (!json || !key || !val) return false;
|
|
char search_key[64];
|
|
snprintf(search_key, sizeof(search_key), "\"%s\"", key);
|
|
const char *p = strstr(json, search_key);
|
|
if (!p) return false;
|
|
|
|
p += strlen(search_key);
|
|
while (*p && (*p == ' ' || *p == ':' || *p == '\t')) p++;
|
|
*val = atoi(p);
|
|
return true;
|
|
}
|
|
|
|
static bool json_get_bool(const char *json, const char *key, bool *val)
|
|
{
|
|
if (!json || !key || !val) return false;
|
|
char search_key[64];
|
|
snprintf(search_key, sizeof(search_key), "\"%s\"", key);
|
|
const char *p = strstr(json, search_key);
|
|
if (!p) return false;
|
|
|
|
p += strlen(search_key);
|
|
while (*p && (*p == ' ' || *p == ':' || *p == '\t')) p++;
|
|
if (strncmp(p, "true", 4) == 0 || *p == '1') {
|
|
*val = true;
|
|
return true;
|
|
} else if (strncmp(p, "false", 5) == 0 || *p == '0') {
|
|
*val = false;
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
static bool read_saved_wifi_creds(char *ssid, size_t ssid_len, char *pass, size_t pass_len)
|
|
{
|
|
nvs_handle_t nvs_h;
|
|
if (nvs_open("wifi_cfg", NVS_READONLY, &nvs_h) != ESP_OK) {
|
|
return false;
|
|
}
|
|
esp_err_t err_s = nvs_get_str(nvs_h, "sta_ssid", ssid, &ssid_len);
|
|
nvs_get_str(nvs_h, "sta_pass", pass, &pass_len);
|
|
nvs_close(nvs_h);
|
|
return (err_s == ESP_OK && strlen(ssid) > 0);
|
|
}
|
|
|
|
static esp_err_t save_wifi_creds(const char *ssid, const char *pass)
|
|
{
|
|
nvs_handle_t nvs_h;
|
|
esp_err_t err = nvs_open("wifi_cfg", NVS_READWRITE, &nvs_h);
|
|
if (err != ESP_OK) return err;
|
|
|
|
nvs_set_str(nvs_h, "sta_ssid", ssid);
|
|
nvs_set_str(nvs_h, "sta_pass", pass ? pass : "");
|
|
err = nvs_commit(nvs_h);
|
|
nvs_close(nvs_h);
|
|
return err;
|
|
}
|
|
|
|
static esp_err_t clear_wifi_creds(void)
|
|
{
|
|
nvs_handle_t nvs_h;
|
|
esp_err_t err = nvs_open("wifi_cfg", NVS_READWRITE, &nvs_h);
|
|
if (err != ESP_OK) return err;
|
|
|
|
nvs_erase_key(nvs_h, "sta_ssid");
|
|
nvs_erase_key(nvs_h, "sta_pass");
|
|
err = nvs_commit(nvs_h);
|
|
nvs_close(nvs_h);
|
|
return err;
|
|
}
|
|
|
|
static void wifi_event_handler(void *arg, esp_event_base_t event_base,
|
|
int32_t event_id, void *event_data)
|
|
{
|
|
if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_START) {
|
|
if (s_sta_configured) {
|
|
ESP_LOGI(TAG, "STA interface started, connecting to %s...", s_sta_ssid);
|
|
esp_wifi_connect();
|
|
}
|
|
} else if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_DISCONNECTED) {
|
|
s_sta_connected = false;
|
|
s_sta_ip[0] = '\0';
|
|
ESP_LOGI(TAG, "Wi-Fi Station disconnected");
|
|
} else if (event_base == IP_EVENT && event_id == IP_EVENT_STA_GOT_IP) {
|
|
ip_event_got_ip_t *event = (ip_event_got_ip_t *)event_data;
|
|
esp_ip4addr_ntoa(&event->ip_info.ip, s_sta_ip, sizeof(s_sta_ip));
|
|
s_sta_connected = true;
|
|
ESP_LOGI(TAG, "=====================================================");
|
|
ESP_LOGI(TAG, " Wi-Fi Station Connected! ");
|
|
ESP_LOGI(TAG, " SSID: %s ", s_sta_ssid);
|
|
ESP_LOGI(TAG, " IP: %s ", s_sta_ip);
|
|
ESP_LOGI(TAG, " mDNS: http://mimicon.local/ (or http://esp32-hid.local/)");
|
|
ESP_LOGI(TAG, "=====================================================");
|
|
}
|
|
}
|
|
|
|
/* -------------------------------------------------------------------------- */
|
|
/* HTTP URI Handlers */
|
|
/* -------------------------------------------------------------------------- */
|
|
|
|
static esp_err_t index_get_handler(httpd_req_t *req)
|
|
{
|
|
httpd_resp_set_type(req, "text/html; charset=utf-8");
|
|
return httpd_resp_send(req, INDEX_HTML, strlen(INDEX_HTML));
|
|
}
|
|
|
|
static esp_err_t captive_portal_handler(httpd_req_t *req)
|
|
{
|
|
httpd_resp_set_status(req, "302 Found");
|
|
httpd_resp_set_hdr(req, "Location", "http://192.168.4.1/");
|
|
return httpd_resp_send(req, NULL, 0);
|
|
}
|
|
|
|
static esp_err_t api_status_handler(httpd_req_t *req)
|
|
{
|
|
char json[768];
|
|
console_mode_t mode = profile_manager_get_mode();
|
|
combo_config_t combos;
|
|
profile_manager_get_combos(&combos);
|
|
|
|
const esp_app_desc_t *app_desc = esp_app_get_description();
|
|
const esp_partition_t *running_part = esp_ota_get_running_partition();
|
|
const esp_partition_t *next_part = esp_ota_get_next_update_partition(NULL);
|
|
|
|
ble_hid_host_refresh_battery();
|
|
int8_t battery = ble_hid_host_get_battery_level();
|
|
|
|
wifi_mode_t cur_mode = WIFI_MODE_NULL;
|
|
esp_wifi_get_mode(&cur_mode);
|
|
bool ap_active = (cur_mode == WIFI_MODE_AP || cur_mode == WIFI_MODE_APSTA);
|
|
|
|
snprintf(json, sizeof(json),
|
|
"{\"mode\":%d,\"mode_name\":\"%s\","
|
|
"\"ble_connected\":%s,\"ble_device\":\"%s\","
|
|
"\"battery\":%d,"
|
|
"\"led_brightness\":%d,"
|
|
"\"ap_active\":%s,"
|
|
"\"sta_connected\":%s,\"sta_ssid\":\"%s\",\"sta_ip\":\"%s\","
|
|
"\"mdns_url\":\"http://mimicon.local\","
|
|
"\"combos\":{\"a\":%d,\"b\":%d,\"x\":%d,\"y\":%d},"
|
|
"\"app_version\":\"%s\","
|
|
"\"compile_time\":\"%s %s\","
|
|
"\"running_part\":\"%s\","
|
|
"\"next_part\":\"%s\"}",
|
|
(int)mode,
|
|
profile_manager_mode_name(mode),
|
|
ble_hid_host_is_connected() ? "true" : "false",
|
|
ble_hid_host_get_device_name(),
|
|
(int)battery,
|
|
(int)status_led_get_brightness(),
|
|
ap_active ? "true" : "false",
|
|
s_sta_connected ? "true" : "false",
|
|
s_sta_ssid,
|
|
s_sta_ip,
|
|
(int)combos.combo_a,
|
|
(int)combos.combo_b,
|
|
(int)combos.combo_x,
|
|
(int)combos.combo_y,
|
|
app_desc ? app_desc->version : "1.0.0",
|
|
app_desc ? app_desc->date : "",
|
|
app_desc ? app_desc->time : "",
|
|
running_part ? running_part->label : "factory",
|
|
next_part ? next_part->label : "ota_0");
|
|
|
|
httpd_resp_set_type(req, "application/json");
|
|
httpd_resp_set_hdr(req, "Access-Control-Allow-Origin", "*");
|
|
return httpd_resp_send(req, json, strlen(json));
|
|
}
|
|
|
|
static void format_state_json(char *buf, size_t buf_len)
|
|
{
|
|
if (s_state_mutex) {
|
|
xSemaphoreTake(s_state_mutex, portMAX_DELAY);
|
|
}
|
|
|
|
snprintf(buf, buf_len,
|
|
"{\"a\":%d,\"b\":%d,\"x\":%d,\"y\":%d,\"lb\":%d,\"rb\":%d,"
|
|
"\"select\":%d,\"start\":%d,\"home\":%d,\"l3\":%d,\"r3\":%d,"
|
|
"\"dup\":%d,\"ddown\":%d,\"dleft\":%d,\"dright\":%d,"
|
|
"\"lx\":%d,\"ly\":%d,\"rx\":%d,\"ry\":%d,"
|
|
"\"lt\":%d,\"rt\":%d}",
|
|
s_latest_state.btn_a ? 1 : 0,
|
|
s_latest_state.btn_b ? 1 : 0,
|
|
s_latest_state.btn_x ? 1 : 0,
|
|
s_latest_state.btn_y ? 1 : 0,
|
|
s_latest_state.btn_lb ? 1 : 0,
|
|
s_latest_state.btn_rb ? 1 : 0,
|
|
s_latest_state.btn_select ? 1 : 0,
|
|
s_latest_state.btn_start ? 1 : 0,
|
|
s_latest_state.btn_system ? 1 : 0,
|
|
s_latest_state.btn_l3 ? 1 : 0,
|
|
s_latest_state.btn_r3 ? 1 : 0,
|
|
s_latest_state.dpad_up ? 1 : 0,
|
|
s_latest_state.dpad_down ? 1 : 0,
|
|
s_latest_state.dpad_left ? 1 : 0,
|
|
s_latest_state.dpad_right ? 1 : 0,
|
|
(int)s_latest_state.lx,
|
|
(int)s_latest_state.ly,
|
|
(int)s_latest_state.rx,
|
|
(int)s_latest_state.ry,
|
|
(int)s_latest_state.lt,
|
|
(int)s_latest_state.rt);
|
|
|
|
if (s_state_mutex) {
|
|
xSemaphoreGive(s_state_mutex);
|
|
}
|
|
}
|
|
|
|
static esp_err_t api_state_handler(httpd_req_t *req)
|
|
{
|
|
char json[320];
|
|
format_state_json(json, sizeof(json));
|
|
|
|
httpd_resp_set_type(req, "application/json");
|
|
httpd_resp_set_hdr(req, "Access-Control-Allow-Origin", "*");
|
|
return httpd_resp_send(req, json, strlen(json));
|
|
}
|
|
|
|
#if CONFIG_HTTPD_WS_SUPPORT
|
|
static esp_err_t ws_state_handler(httpd_req_t *req)
|
|
{
|
|
if (req->method == HTTP_GET) {
|
|
ESP_LOGI(TAG, "WebSocket visualizer client connected");
|
|
return ESP_OK;
|
|
}
|
|
|
|
httpd_ws_frame_t ws_pkt;
|
|
uint8_t buf[256];
|
|
memset(&ws_pkt, 0, sizeof(httpd_ws_frame_t));
|
|
ws_pkt.payload = buf;
|
|
ws_pkt.type = HTTPD_WS_TYPE_TEXT;
|
|
esp_err_t ret = httpd_ws_recv_frame(req, &ws_pkt, sizeof(buf) - 1);
|
|
if (ret != ESP_OK) {
|
|
return ret;
|
|
}
|
|
buf[ws_pkt.len] = '\0';
|
|
|
|
if (strstr((char *)buf, "\"b\":") || strstr((char *)buf, "\"b\" :")) {
|
|
int buttons = 0, lx = 0, ly = 0, rx = 0, ry = 0, lt = 0, rt = 0;
|
|
json_get_int((char *)buf, "b", &buttons);
|
|
json_get_int((char *)buf, "lx", &lx);
|
|
json_get_int((char *)buf, "ly", &ly);
|
|
json_get_int((char *)buf, "rx", &rx);
|
|
json_get_int((char *)buf, "ry", &ry);
|
|
json_get_int((char *)buf, "lt", <);
|
|
json_get_int((char *)buf, "rt", &rt);
|
|
|
|
controller_state_t st = {0};
|
|
st.btn_a = (buttons & (1 << 0)) != 0;
|
|
st.btn_b = (buttons & (1 << 1)) != 0;
|
|
st.btn_x = (buttons & (1 << 2)) != 0;
|
|
st.btn_y = (buttons & (1 << 3)) != 0;
|
|
st.btn_lb = (buttons & (1 << 4)) != 0;
|
|
st.btn_rb = (buttons & (1 << 5)) != 0;
|
|
st.btn_select = (buttons & (1 << 8)) != 0;
|
|
st.btn_start = (buttons & (1 << 9)) != 0;
|
|
st.btn_system = (buttons & (1 << 10)) != 0;
|
|
st.btn_l3 = (buttons & (1 << 11)) != 0;
|
|
st.btn_r3 = (buttons & (1 << 12)) != 0;
|
|
st.dpad_up = (buttons & (1 << 13)) != 0;
|
|
st.dpad_down = (buttons & (1 << 14)) != 0;
|
|
st.dpad_left = (buttons & (1 << 15)) != 0;
|
|
st.dpad_right = (buttons & (1 << 16)) != 0;
|
|
st.lx = (int16_t)lx;
|
|
st.ly = (int16_t)ly;
|
|
st.rx = (int16_t)rx;
|
|
st.ry = (int16_t)ry;
|
|
st.lt = (uint8_t)(lt > 255 ? 255 : (lt < 0 ? 0 : lt));
|
|
st.rt = (uint8_t)(rt > 255 ? 255 : (rt < 0 ? 0 : rt));
|
|
|
|
controller_state_t calibrated;
|
|
input_calibration_apply(&st, &calibrated);
|
|
usb_gamepad_send_state(&calibrated);
|
|
config_server_set_latest_state(&calibrated);
|
|
return ESP_OK;
|
|
}
|
|
|
|
char json[320];
|
|
format_state_json(json, sizeof(json));
|
|
|
|
memset(&ws_pkt, 0, sizeof(httpd_ws_frame_t));
|
|
ws_pkt.payload = (uint8_t *)json;
|
|
ws_pkt.len = strlen(json);
|
|
ws_pkt.type = HTTPD_WS_TYPE_TEXT;
|
|
return httpd_ws_send_frame(req, &ws_pkt);
|
|
}
|
|
#endif
|
|
|
|
static esp_err_t api_mode_handler(httpd_req_t *req)
|
|
{
|
|
char buf[128];
|
|
int ret = httpd_req_recv(req, buf, sizeof(buf) - 1);
|
|
if (ret <= 0) {
|
|
httpd_resp_send_500(req);
|
|
return ESP_FAIL;
|
|
}
|
|
buf[ret] = '\0';
|
|
|
|
char *p = strstr(buf, "\"mode\":");
|
|
if (!p) p = strstr(buf, "\"mode\" :");
|
|
if (p) {
|
|
int mode_val = atoi(p + 7);
|
|
if (mode_val >= 0 && mode_val < CONSOLE_MODE_MAX) {
|
|
ESP_LOGI(TAG, "Web client requested console mode %d", mode_val);
|
|
profile_manager_set_mode((console_mode_t)mode_val);
|
|
}
|
|
}
|
|
|
|
httpd_resp_set_type(req, "application/json");
|
|
return httpd_resp_send(req, "{\"status\":\"ok\"}", 15);
|
|
}
|
|
|
|
static esp_err_t api_led_handler(httpd_req_t *req)
|
|
{
|
|
char buf[128];
|
|
int ret = httpd_req_recv(req, buf, sizeof(buf) - 1);
|
|
if (ret <= 0) {
|
|
httpd_resp_send_500(req);
|
|
return ESP_FAIL;
|
|
}
|
|
buf[ret] = '\0';
|
|
|
|
char *p = strstr(buf, "\"brightness\":");
|
|
if (!p) p = strstr(buf, "\"brightness\" :");
|
|
if (p) {
|
|
int br = atoi(p + 13);
|
|
if (br >= 0 && br <= 100) {
|
|
profile_manager_set_led_brightness((uint8_t)br);
|
|
}
|
|
}
|
|
|
|
httpd_resp_set_type(req, "application/json");
|
|
return httpd_resp_send(req, "{\"status\":\"ok\"}", 15);
|
|
}
|
|
|
|
static esp_err_t api_combos_get_handler(httpd_req_t *req)
|
|
{
|
|
combo_config_t combos;
|
|
profile_manager_get_combos(&combos);
|
|
|
|
char json[128];
|
|
snprintf(json, sizeof(json), "{\"combo_a\":%d,\"combo_b\":%d,\"combo_x\":%d,\"combo_y\":%d}",
|
|
combos.combo_a, combos.combo_b, combos.combo_x, combos.combo_y);
|
|
|
|
httpd_resp_set_type(req, "application/json");
|
|
httpd_resp_set_hdr(req, "Access-Control-Allow-Origin", "*");
|
|
return httpd_resp_send(req, json, strlen(json));
|
|
}
|
|
|
|
static esp_err_t api_combos_post_handler(httpd_req_t *req)
|
|
{
|
|
char buf[160];
|
|
int ret = httpd_req_recv(req, buf, sizeof(buf) - 1);
|
|
if (ret <= 0) {
|
|
httpd_resp_send_500(req);
|
|
return ESP_FAIL;
|
|
}
|
|
buf[ret] = '\0';
|
|
|
|
combo_config_t combos;
|
|
profile_manager_get_combos(&combos);
|
|
|
|
int val = -1;
|
|
if (json_get_int(buf, "combo_a", &val)) combos.combo_a = (int8_t)val;
|
|
if (json_get_int(buf, "combo_b", &val)) combos.combo_b = (int8_t)val;
|
|
if (json_get_int(buf, "combo_x", &val)) combos.combo_x = (int8_t)val;
|
|
if (json_get_int(buf, "combo_y", &val)) combos.combo_y = (int8_t)val;
|
|
|
|
profile_manager_set_combos(&combos);
|
|
|
|
httpd_resp_set_type(req, "application/json");
|
|
return httpd_resp_send(req, "{\"status\":\"ok\"}", 15);
|
|
}
|
|
|
|
static esp_err_t api_ble_disconnect_handler(httpd_req_t *req)
|
|
{
|
|
ble_hid_host_disconnect();
|
|
httpd_resp_set_type(req, "application/json");
|
|
return httpd_resp_send(req, "{\"status\":\"ok\"}", 15);
|
|
}
|
|
|
|
static esp_err_t api_ble_clear_bonds_handler(httpd_req_t *req)
|
|
{
|
|
ble_hid_host_clear_bonds();
|
|
httpd_resp_set_type(req, "application/json");
|
|
return httpd_resp_send(req, "{\"status\":\"ok\"}", 15);
|
|
}
|
|
|
|
static esp_err_t api_ble_rumble_handler(httpd_req_t *req)
|
|
{
|
|
ESP_LOGI(TAG, "Triggering BLE controller test rumble...");
|
|
ble_hid_host_test_rumble(150);
|
|
httpd_resp_set_type(req, "application/json");
|
|
return httpd_resp_send(req, "{\"status\":\"ok\"}", 15);
|
|
}
|
|
|
|
static esp_err_t api_ble_peers_get_handler(httpd_req_t *req)
|
|
{
|
|
ble_peer_info_t peers[BLE_MAX_PEERS];
|
|
int count = ble_hid_host_get_peers(peers, BLE_MAX_PEERS);
|
|
|
|
char *json = malloc(1024);
|
|
if (!json) {
|
|
httpd_resp_send_500(req);
|
|
return ESP_FAIL;
|
|
}
|
|
|
|
size_t len = 0;
|
|
len += snprintf(json + len, 1024 - len, "[");
|
|
for (int i = 0; i < count; i++) {
|
|
char clean_name[32] = {0};
|
|
int c_idx = 0;
|
|
for (int s = 0; s < strlen(peers[i].name) && c_idx < 31; s++) {
|
|
char ch = peers[i].name[s];
|
|
if (ch == '\"' || ch == '\\') clean_name[c_idx++] = ' ';
|
|
else if (ch >= 32 && ch <= 126) clean_name[c_idx++] = ch;
|
|
}
|
|
|
|
len += snprintf(json + len, 1024 - len,
|
|
"%s{\"name\":\"%s\",\"mac\":\"%s\",\"type\":%d,\"connected\":%s,\"battery\":%d}",
|
|
(i > 0) ? "," : "",
|
|
clean_name,
|
|
peers[i].mac_str,
|
|
(int)peers[i].controller_type,
|
|
peers[i].is_connected ? "true" : "false",
|
|
(int)peers[i].battery);
|
|
if (len + 64 >= 1024) break;
|
|
}
|
|
len += snprintf(json + len, 1024 - len, "]");
|
|
|
|
httpd_resp_set_type(req, "application/json");
|
|
httpd_resp_set_hdr(req, "Access-Control-Allow-Origin", "*");
|
|
esp_err_t res = httpd_resp_send(req, json, len);
|
|
free(json);
|
|
return res;
|
|
}
|
|
|
|
static esp_err_t api_ble_unpair_handler(httpd_req_t *req)
|
|
{
|
|
char buf[128];
|
|
int ret = httpd_req_recv(req, buf, sizeof(buf) - 1);
|
|
if (ret <= 0) {
|
|
httpd_resp_send_500(req);
|
|
return ESP_FAIL;
|
|
}
|
|
buf[ret] = '\0';
|
|
|
|
char mac[20] = {0};
|
|
if (json_get_string(buf, "mac", mac, sizeof(mac))) {
|
|
ble_hid_host_unpair_device(mac);
|
|
}
|
|
|
|
httpd_resp_set_type(req, "application/json");
|
|
return httpd_resp_send(req, "{\"status\":\"ok\"}", 15);
|
|
}
|
|
|
|
static esp_err_t api_remote_input_handler(httpd_req_t *req)
|
|
{
|
|
char buf[256];
|
|
int ret = httpd_req_recv(req, buf, sizeof(buf) - 1);
|
|
if (ret <= 0) {
|
|
httpd_resp_send_500(req);
|
|
return ESP_FAIL;
|
|
}
|
|
buf[ret] = '\0';
|
|
|
|
int buttons = 0, lx = 0, ly = 0, rx = 0, ry = 0, lt = 0, rt = 0;
|
|
json_get_int(buf, "b", &buttons);
|
|
json_get_int(buf, "lx", &lx);
|
|
json_get_int(buf, "ly", &ly);
|
|
json_get_int(buf, "rx", &rx);
|
|
json_get_int(buf, "ry", &ry);
|
|
json_get_int(buf, "lt", <);
|
|
json_get_int(buf, "rt", &rt);
|
|
|
|
controller_state_t st = {0};
|
|
st.btn_a = (buttons & (1 << 0)) != 0;
|
|
st.btn_b = (buttons & (1 << 1)) != 0;
|
|
st.btn_x = (buttons & (1 << 2)) != 0;
|
|
st.btn_y = (buttons & (1 << 3)) != 0;
|
|
st.btn_lb = (buttons & (1 << 4)) != 0;
|
|
st.btn_rb = (buttons & (1 << 5)) != 0;
|
|
st.btn_lt = (buttons & (1 << 6)) != 0;
|
|
st.btn_rt = (buttons & (1 << 7)) != 0;
|
|
st.btn_select = (buttons & (1 << 8)) != 0;
|
|
st.btn_start = (buttons & (1 << 9)) != 0;
|
|
st.btn_system = (buttons & (1 << 10)) != 0;
|
|
st.btn_l3 = (buttons & (1 << 11)) != 0;
|
|
st.btn_r3 = (buttons & (1 << 12)) != 0;
|
|
st.dpad_up = (buttons & (1 << 13)) != 0;
|
|
st.dpad_down = (buttons & (1 << 14)) != 0;
|
|
st.dpad_left = (buttons & (1 << 15)) != 0;
|
|
st.dpad_right = (buttons & (1 << 16)) != 0;
|
|
|
|
st.lx = (int16_t)lx;
|
|
st.ly = (int16_t)ly;
|
|
st.rx = (int16_t)rx;
|
|
st.ry = (int16_t)ry;
|
|
st.lt = (uint8_t)(lt > 255 ? 255 : (lt < 0 ? 0 : lt));
|
|
st.rt = (uint8_t)(rt > 255 ? 255 : (rt < 0 ? 0 : rt));
|
|
|
|
controller_state_t calibrated;
|
|
input_calibration_apply(&st, &calibrated);
|
|
usb_gamepad_send_state(&calibrated);
|
|
config_server_set_latest_state(&calibrated);
|
|
|
|
httpd_resp_set_type(req, "application/json");
|
|
httpd_resp_set_hdr(req, "Access-Control-Allow-Origin", "*");
|
|
return httpd_resp_send(req, "{\"status\":\"ok\"}", 15);
|
|
}
|
|
|
|
static esp_err_t api_calibration_get_handler(httpd_req_t *req)
|
|
{
|
|
input_calibration_cfg_t cfg;
|
|
input_calibration_get_config(&cfg);
|
|
|
|
char json[256];
|
|
snprintf(json, sizeof(json),
|
|
"{\"left_deadzone\":%d,\"right_deadzone\":%d,\"trigger_deadzone\":%d,"
|
|
"\"swap_ab\":%s,\"swap_xy\":%s,\"swap_bumpers_triggers\":%s,"
|
|
"\"invert_lx\":%s,\"invert_ly\":%s,\"invert_rx\":%s,\"invert_ry\":%s}",
|
|
cfg.left_deadzone, cfg.right_deadzone, cfg.trigger_deadzone,
|
|
cfg.swap_ab ? "true" : "false",
|
|
cfg.swap_xy ? "true" : "false",
|
|
cfg.swap_bumpers_triggers ? "true" : "false",
|
|
cfg.invert_lx ? "true" : "false",
|
|
cfg.invert_ly ? "true" : "false",
|
|
cfg.invert_rx ? "true" : "false",
|
|
cfg.invert_ry ? "true" : "false");
|
|
|
|
httpd_resp_set_type(req, "application/json");
|
|
httpd_resp_set_hdr(req, "Access-Control-Allow-Origin", "*");
|
|
return httpd_resp_send(req, json, strlen(json));
|
|
}
|
|
|
|
static esp_err_t api_calibration_post_handler(httpd_req_t *req)
|
|
{
|
|
char buf[320];
|
|
int ret = httpd_req_recv(req, buf, sizeof(buf) - 1);
|
|
if (ret <= 0) {
|
|
httpd_resp_send_500(req);
|
|
return ESP_FAIL;
|
|
}
|
|
buf[ret] = '\0';
|
|
|
|
input_calibration_cfg_t cfg;
|
|
input_calibration_get_config(&cfg);
|
|
|
|
int val = 0;
|
|
if (json_get_int(buf, "left_deadzone", &val)) cfg.left_deadzone = (uint8_t)val;
|
|
if (json_get_int(buf, "right_deadzone", &val)) cfg.right_deadzone = (uint8_t)val;
|
|
if (json_get_int(buf, "trigger_deadzone", &val)) cfg.trigger_deadzone = (uint8_t)val;
|
|
|
|
bool b = false;
|
|
if (json_get_bool(buf, "swap_ab", &b)) cfg.swap_ab = b;
|
|
if (json_get_bool(buf, "swap_xy", &b)) cfg.swap_xy = b;
|
|
if (json_get_bool(buf, "swap_bumpers_triggers", &b)) cfg.swap_bumpers_triggers = b;
|
|
if (json_get_bool(buf, "invert_lx", &b)) cfg.invert_lx = b;
|
|
if (json_get_bool(buf, "invert_ly", &b)) cfg.invert_ly = b;
|
|
if (json_get_bool(buf, "invert_rx", &b)) cfg.invert_rx = b;
|
|
if (json_get_bool(buf, "invert_ry", &b)) cfg.invert_ry = b;
|
|
|
|
input_calibration_set_config(&cfg);
|
|
|
|
httpd_resp_set_type(req, "application/json");
|
|
return httpd_resp_send(req, "{\"status\":\"ok\"}", 15);
|
|
}
|
|
|
|
static esp_err_t api_calibration_reset_handler(httpd_req_t *req)
|
|
{
|
|
input_calibration_reset_defaults();
|
|
httpd_resp_set_type(req, "application/json");
|
|
return httpd_resp_send(req, "{\"status\":\"ok\"}", 15);
|
|
}
|
|
|
|
static esp_err_t api_exit_handler(httpd_req_t *req)
|
|
{
|
|
ESP_LOGI(TAG, "Client requested exit from Config Mode");
|
|
config_server_request_stop();
|
|
httpd_resp_set_type(req, "application/json");
|
|
return httpd_resp_send(req, "{\"status\":\"exiting\"}", 20);
|
|
}
|
|
|
|
static esp_err_t api_wifi_scan_handler(httpd_req_t *req)
|
|
{
|
|
ESP_LOGI(TAG, "Starting Wi-Fi scan...");
|
|
wifi_scan_config_t scan_cfg = {
|
|
.ssid = NULL,
|
|
.bssid = NULL,
|
|
.channel = 0,
|
|
.show_hidden = false,
|
|
.scan_type = WIFI_SCAN_TYPE_ACTIVE,
|
|
};
|
|
|
|
esp_err_t err = esp_wifi_scan_start(&scan_cfg, true);
|
|
if (err != ESP_OK) {
|
|
ESP_LOGE(TAG, "Wi-Fi scan failed: %s", esp_err_to_name(err));
|
|
httpd_resp_set_type(req, "application/json");
|
|
return httpd_resp_send(req, "[]", 2);
|
|
}
|
|
|
|
uint16_t ap_count = 0;
|
|
esp_wifi_scan_get_ap_num(&ap_count);
|
|
if (ap_count > 20) ap_count = 20;
|
|
|
|
wifi_ap_record_t *ap_records = NULL;
|
|
if (ap_count > 0) {
|
|
ap_records = malloc(sizeof(wifi_ap_record_t) * ap_count);
|
|
if (ap_records) {
|
|
esp_wifi_scan_get_ap_records(&ap_count, ap_records);
|
|
}
|
|
}
|
|
|
|
size_t json_cap = 2048;
|
|
char *json = malloc(json_cap);
|
|
if (!json) {
|
|
if (ap_records) free(ap_records);
|
|
httpd_resp_send_500(req);
|
|
return ESP_FAIL;
|
|
}
|
|
|
|
size_t len = 0;
|
|
len += snprintf(json + len, json_cap - len, "[");
|
|
|
|
int written = 0;
|
|
if (ap_records) {
|
|
for (int i = 0; i < ap_count; i++) {
|
|
if (ap_records[i].ssid[0] == '\0') continue;
|
|
|
|
bool dup = false;
|
|
for (int j = 0; j < i; j++) {
|
|
if (strcmp((char *)ap_records[i].ssid, (char *)ap_records[j].ssid) == 0) {
|
|
dup = true;
|
|
break;
|
|
}
|
|
}
|
|
if (dup) continue;
|
|
|
|
if (written > 0 && len + 2 < json_cap) {
|
|
len += snprintf(json + len, json_cap - len, ",");
|
|
}
|
|
|
|
char clean_ssid[33] = {0};
|
|
size_t c_idx = 0;
|
|
for (size_t s = 0; s < strlen((char *)ap_records[i].ssid) && c_idx + 1 < sizeof(clean_ssid); s++) {
|
|
char ch = ap_records[i].ssid[s];
|
|
if (ch == '\"' || ch == '\\') clean_ssid[c_idx++] = ' ';
|
|
else if (ch >= 32 && ch <= 126) clean_ssid[c_idx++] = ch;
|
|
}
|
|
|
|
len += snprintf(json + len, json_cap - len,
|
|
"{\"ssid\":\"%s\",\"rssi\":%d,\"auth\":%d}",
|
|
clean_ssid,
|
|
(int)ap_records[i].rssi,
|
|
(int)ap_records[i].authmode);
|
|
written++;
|
|
if (len + 64 >= json_cap) break;
|
|
}
|
|
free(ap_records);
|
|
}
|
|
|
|
if (len + 2 < json_cap) {
|
|
len += snprintf(json + len, json_cap - len, "]");
|
|
}
|
|
|
|
httpd_resp_set_type(req, "application/json");
|
|
httpd_resp_set_hdr(req, "Access-Control-Allow-Origin", "*");
|
|
esp_err_t res = httpd_resp_send(req, json, len);
|
|
free(json);
|
|
return res;
|
|
}
|
|
|
|
static esp_err_t api_wifi_connect_handler(httpd_req_t *req)
|
|
{
|
|
char buf[256];
|
|
int ret = httpd_req_recv(req, buf, sizeof(buf) - 1);
|
|
if (ret <= 0) {
|
|
httpd_resp_send_500(req);
|
|
return ESP_FAIL;
|
|
}
|
|
buf[ret] = '\0';
|
|
|
|
char ssid[33] = {0};
|
|
char pass[65] = {0};
|
|
|
|
json_get_string(buf, "ssid", ssid, sizeof(ssid));
|
|
json_get_string(buf, "password", pass, sizeof(pass));
|
|
|
|
if (strlen(ssid) == 0) {
|
|
httpd_resp_set_type(req, "application/json");
|
|
return httpd_resp_send(req, "{\"status\":\"error\",\"message\":\"Empty SSID\"}", HTTPD_RESP_USE_STRLEN);
|
|
}
|
|
|
|
ESP_LOGI(TAG, "Saving Wi-Fi credentials for SSID: '%s'", ssid);
|
|
save_wifi_creds(ssid, pass);
|
|
|
|
strncpy(s_sta_ssid, ssid, sizeof(s_sta_ssid) - 1);
|
|
s_sta_configured = true;
|
|
s_sta_connected = false;
|
|
s_sta_ip[0] = '\0';
|
|
|
|
wifi_config_t wifi_sta_cfg = {0};
|
|
strncpy((char *)wifi_sta_cfg.sta.ssid, ssid, sizeof(wifi_sta_cfg.sta.ssid) - 1);
|
|
strncpy((char *)wifi_sta_cfg.sta.password, pass, sizeof(wifi_sta_cfg.sta.password) - 1);
|
|
|
|
esp_wifi_set_config(WIFI_IF_STA, &wifi_sta_cfg);
|
|
esp_wifi_disconnect();
|
|
esp_wifi_connect();
|
|
|
|
httpd_resp_set_type(req, "application/json");
|
|
return httpd_resp_send(req, "{\"status\":\"connecting\"}", HTTPD_RESP_USE_STRLEN);
|
|
}
|
|
|
|
static esp_err_t api_wifi_forget_handler(httpd_req_t *req)
|
|
{
|
|
ESP_LOGI(TAG, "Clearing saved Wi-Fi credentials...");
|
|
clear_wifi_creds();
|
|
s_sta_configured = false;
|
|
s_sta_connected = false;
|
|
s_sta_ssid[0] = '\0';
|
|
s_sta_ip[0] = '\0';
|
|
|
|
wifi_config_t empty_cfg = {0};
|
|
esp_wifi_set_config(WIFI_IF_STA, &empty_cfg);
|
|
esp_wifi_disconnect();
|
|
|
|
httpd_resp_set_type(req, "application/json");
|
|
return httpd_resp_send(req, "{\"status\":\"ok\"}", HTTPD_RESP_USE_STRLEN);
|
|
}
|
|
|
|
static void delayed_restart_timer_callback(void *arg)
|
|
{
|
|
ESP_LOGI(TAG, "Restarting ESP32-S3 into new firmware now...");
|
|
esp_restart();
|
|
}
|
|
|
|
static esp_err_t api_ota_upload_handler(httpd_req_t *req)
|
|
{
|
|
ESP_LOGI(TAG, "Starting OTA firmware upload, total size: %d bytes", req->content_len);
|
|
|
|
if (req->content_len <= 0) {
|
|
httpd_resp_set_status(req, "400 Bad Request");
|
|
httpd_resp_set_type(req, "application/json");
|
|
return httpd_resp_send(req, "{\"status\":\"error\",\"message\":\"Empty payload\"}", HTTPD_RESP_USE_STRLEN);
|
|
}
|
|
|
|
const esp_partition_t *update_partition = esp_ota_get_next_update_partition(NULL);
|
|
if (!update_partition) {
|
|
ESP_LOGE(TAG, "No OTA partition found");
|
|
httpd_resp_set_status(req, "500 Internal Server Error");
|
|
httpd_resp_set_type(req, "application/json");
|
|
return httpd_resp_send(req, "{\"status\":\"error\",\"message\":\"No OTA partition found\"}", HTTPD_RESP_USE_STRLEN);
|
|
}
|
|
|
|
ESP_LOGI(TAG, "Target OTA partition: %s (address: 0x%08lx, max size: 0x%08lx)",
|
|
update_partition->label,
|
|
(unsigned long)update_partition->address,
|
|
(unsigned long)update_partition->size);
|
|
|
|
if (req->content_len > update_partition->size) {
|
|
ESP_LOGE(TAG, "Binary size (%d) exceeds partition capacity (%lu)",
|
|
req->content_len, (unsigned long)update_partition->size);
|
|
httpd_resp_set_status(req, "400 Bad Request");
|
|
httpd_resp_set_type(req, "application/json");
|
|
return httpd_resp_send(req, "{\"status\":\"error\",\"message\":\"Binary size exceeds partition size\"}", HTTPD_RESP_USE_STRLEN);
|
|
}
|
|
|
|
esp_ota_handle_t ota_handle = 0;
|
|
esp_err_t err = esp_ota_begin(update_partition, OTA_WITH_SEQUENTIAL_WRITES, &ota_handle);
|
|
if (err != ESP_OK) {
|
|
ESP_LOGE(TAG, "esp_ota_begin failed: %s", esp_err_to_name(err));
|
|
httpd_resp_set_status(req, "500 Internal Server Error");
|
|
httpd_resp_set_type(req, "application/json");
|
|
return httpd_resp_send(req, "{\"status\":\"error\",\"message\":\"Failed to begin OTA flash session\"}", HTTPD_RESP_USE_STRLEN);
|
|
}
|
|
|
|
char *buf = malloc(2048);
|
|
if (!buf) {
|
|
esp_ota_abort(ota_handle);
|
|
httpd_resp_send_500(req);
|
|
return ESP_FAIL;
|
|
}
|
|
|
|
int remaining = req->content_len;
|
|
while (remaining > 0) {
|
|
int to_read = remaining > 2048 ? 2048 : remaining;
|
|
int recv_len = httpd_req_recv(req, buf, to_read);
|
|
if (recv_len <= 0) {
|
|
if (recv_len == HTTPD_SOCK_ERR_TIMEOUT) {
|
|
continue;
|
|
}
|
|
ESP_LOGE(TAG, "OTA receive interrupted (%d)", recv_len);
|
|
esp_ota_abort(ota_handle);
|
|
free(buf);
|
|
httpd_resp_set_status(req, "500 Internal Server Error");
|
|
httpd_resp_set_type(req, "application/json");
|
|
return httpd_resp_send(req, "{\"status\":\"error\",\"message\":\"Connection lost during upload\"}", HTTPD_RESP_USE_STRLEN);
|
|
}
|
|
|
|
err = esp_ota_write(ota_handle, (const void *)buf, recv_len);
|
|
if (err != ESP_OK) {
|
|
ESP_LOGE(TAG, "esp_ota_write failed: %s", esp_err_to_name(err));
|
|
esp_ota_abort(ota_handle);
|
|
free(buf);
|
|
httpd_resp_set_status(req, "500 Internal Server Error");
|
|
httpd_resp_set_type(req, "application/json");
|
|
return httpd_resp_send(req, "{\"status\":\"error\",\"message\":\"Flash write error\"}", HTTPD_RESP_USE_STRLEN);
|
|
}
|
|
|
|
remaining -= recv_len;
|
|
}
|
|
|
|
free(buf);
|
|
|
|
err = esp_ota_end(ota_handle);
|
|
if (err != ESP_OK) {
|
|
ESP_LOGE(TAG, "esp_ota_end validation failed: %s", esp_err_to_name(err));
|
|
httpd_resp_set_status(req, "500 Internal Server Error");
|
|
httpd_resp_set_type(req, "application/json");
|
|
return httpd_resp_send(req, "{\"status\":\"error\",\"message\":\"Firmware verification failed\"}", HTTPD_RESP_USE_STRLEN);
|
|
}
|
|
|
|
err = esp_ota_set_boot_partition(update_partition);
|
|
if (err != ESP_OK) {
|
|
ESP_LOGE(TAG, "esp_ota_set_boot_partition failed: %s", esp_err_to_name(err));
|
|
httpd_resp_set_status(req, "500 Internal Server Error");
|
|
httpd_resp_set_type(req, "application/json");
|
|
return httpd_resp_send(req, "{\"status\":\"error\",\"message\":\"Failed to set boot partition\"}", HTTPD_RESP_USE_STRLEN);
|
|
}
|
|
|
|
ESP_LOGI(TAG, "OTA update complete! New boot partition: %s. Scheduling reboot...", update_partition->label);
|
|
|
|
httpd_resp_set_type(req, "application/json");
|
|
httpd_resp_set_hdr(req, "Access-Control-Allow-Origin", "*");
|
|
httpd_resp_send(req, "{\"status\":\"ok\",\"message\":\"Update successful, rebooting...\"}", HTTPD_RESP_USE_STRLEN);
|
|
|
|
const esp_timer_create_args_t restart_timer_args = {
|
|
.callback = &delayed_restart_timer_callback,
|
|
.name = "ota_reboot"
|
|
};
|
|
esp_timer_handle_t restart_timer;
|
|
if (esp_timer_create(&restart_timer_args, &restart_timer) == ESP_OK) {
|
|
esp_timer_start_once(restart_timer, 1500000); /* 1.5 seconds */
|
|
} else {
|
|
esp_restart();
|
|
}
|
|
|
|
return ESP_OK;
|
|
}
|
|
|
|
static esp_err_t api_ap_toggle_handler(httpd_req_t *req)
|
|
{
|
|
wifi_mode_t current_mode = WIFI_MODE_NULL;
|
|
esp_wifi_get_mode(¤t_mode);
|
|
|
|
bool ap_now_active = false;
|
|
|
|
if (current_mode == WIFI_MODE_APSTA || current_mode == WIFI_MODE_AP) {
|
|
/* User wants to turn OFF SoftAP */
|
|
if (s_sta_connected) {
|
|
esp_wifi_set_mode(WIFI_MODE_STA);
|
|
dns_server_stop();
|
|
ap_now_active = false;
|
|
ESP_LOGI(TAG, "SoftAP hotspot turned OFF (Station mode remains active)");
|
|
} else {
|
|
/* If no Station connection, turning off AP shuts down server */
|
|
ESP_LOGI(TAG, "SoftAP hotspot turned OFF with no STA -> requesting server stop");
|
|
char resp[64];
|
|
snprintf(resp, sizeof(resp), "{\"status\":\"ok\",\"ap_active\":false,\"stopped\":true}");
|
|
httpd_resp_set_type(req, "application/json");
|
|
httpd_resp_set_hdr(req, "Access-Control-Allow-Origin", "*");
|
|
httpd_resp_send(req, resp, strlen(resp));
|
|
config_server_request_stop();
|
|
return ESP_OK;
|
|
}
|
|
} else {
|
|
/* User wants to turn ON SoftAP */
|
|
esp_wifi_set_mode(s_sta_configured ? WIFI_MODE_APSTA : WIFI_MODE_AP);
|
|
dns_server_start();
|
|
ap_now_active = true;
|
|
ESP_LOGI(TAG, "SoftAP hotspot turned ON: %s", CONFIG_AP_SSID);
|
|
}
|
|
|
|
char resp[64];
|
|
snprintf(resp, sizeof(resp), "{\"status\":\"ok\",\"ap_active\":%s}", ap_now_active ? "true" : "false");
|
|
httpd_resp_set_type(req, "application/json");
|
|
httpd_resp_set_hdr(req, "Access-Control-Allow-Origin", "*");
|
|
return httpd_resp_send(req, resp, strlen(resp));
|
|
}
|
|
|
|
/* -------------------------------------------------------------------------- */
|
|
/* Internal Server Start / Stop (Executed safely in dedicated task) */
|
|
/* -------------------------------------------------------------------------- */
|
|
|
|
static esp_err_t config_server_start_internal(void)
|
|
{
|
|
if (s_is_active) {
|
|
return ESP_OK;
|
|
}
|
|
|
|
ESP_LOGI(TAG, "Starting Config Server (AP+STA mode and mDNS)...");
|
|
|
|
/* 1. Initialize TCP/IP and Wi-Fi stack once */
|
|
if (!s_wifi_inited) {
|
|
esp_netif_init();
|
|
esp_event_loop_create_default();
|
|
s_ap_netif = esp_netif_create_default_wifi_ap();
|
|
s_sta_netif = esp_netif_create_default_wifi_sta();
|
|
|
|
wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
|
|
esp_wifi_init(&cfg);
|
|
esp_wifi_set_storage(WIFI_STORAGE_RAM);
|
|
|
|
esp_event_handler_instance_register(WIFI_EVENT, ESP_EVENT_ANY_ID, &wifi_event_handler, NULL, NULL);
|
|
esp_event_handler_instance_register(IP_EVENT, IP_EVENT_STA_GOT_IP, &wifi_event_handler, NULL, NULL);
|
|
|
|
s_wifi_inited = true;
|
|
}
|
|
|
|
/* 2. Configure and start SoftAP + Station */
|
|
wifi_config_t wifi_ap_config = {
|
|
.ap = {
|
|
.ssid = CONFIG_AP_SSID,
|
|
.ssid_len = strlen(CONFIG_AP_SSID),
|
|
.channel = CONFIG_AP_CHANNEL,
|
|
.password = "",
|
|
.max_connection = CONFIG_AP_MAX_CONN,
|
|
.authmode = WIFI_AUTH_OPEN,
|
|
},
|
|
};
|
|
|
|
esp_err_t err = esp_wifi_set_mode(WIFI_MODE_APSTA);
|
|
if (err == ESP_OK) err = esp_wifi_set_config(WIFI_IF_AP, &wifi_ap_config);
|
|
|
|
char saved_pass[65] = {0};
|
|
if (read_saved_wifi_creds(s_sta_ssid, sizeof(s_sta_ssid), saved_pass, sizeof(saved_pass))) {
|
|
s_sta_configured = true;
|
|
wifi_config_t wifi_sta_config = {0};
|
|
strncpy((char *)wifi_sta_config.sta.ssid, s_sta_ssid, sizeof(wifi_sta_config.sta.ssid) - 1);
|
|
strncpy((char *)wifi_sta_config.sta.password, saved_pass, sizeof(wifi_sta_config.sta.password) - 1);
|
|
esp_wifi_set_config(WIFI_IF_STA, &wifi_sta_config);
|
|
ESP_LOGI(TAG, "Loaded saved Wi-Fi STA SSID: %s", s_sta_ssid);
|
|
} else {
|
|
s_sta_configured = false;
|
|
s_sta_ssid[0] = '\0';
|
|
}
|
|
|
|
if (err == ESP_OK) err = esp_wifi_start();
|
|
|
|
if (err != ESP_OK) {
|
|
ESP_LOGE(TAG, "Failed to start Wi-Fi: %s", esp_err_to_name(err));
|
|
return err;
|
|
}
|
|
|
|
if (s_sta_configured) {
|
|
esp_wifi_connect();
|
|
}
|
|
|
|
ESP_LOGI(TAG, "=====================================================");
|
|
ESP_LOGI(TAG, " Config Wi-Fi SoftAP Started! ");
|
|
ESP_LOGI(TAG, " SoftAP SSID: %s (Open) ", CONFIG_AP_SSID);
|
|
ESP_LOGI(TAG, " SoftAP URL: http://192.168.4.1/ ");
|
|
ESP_LOGI(TAG, " mDNS URL: http://mimicon.local/ ");
|
|
ESP_LOGI(TAG, "=====================================================");
|
|
|
|
/* 3. Start mDNS Responder */
|
|
if (!s_mdns_inited) {
|
|
esp_err_t m_err = mdns_init();
|
|
if (m_err == ESP_OK) {
|
|
mdns_hostname_set("mimicon");
|
|
mdns_instance_name_set("MimiCon Gamepad Mimic");
|
|
mdns_service_add("MimiCon-Web", "_http", "_tcp", 80, NULL, 0);
|
|
s_mdns_inited = true;
|
|
ESP_LOGI(TAG, "mDNS responder active: http://mimicon.local/");
|
|
} else {
|
|
ESP_LOGW(TAG, "Failed to initialize mDNS: %s", esp_err_to_name(m_err));
|
|
}
|
|
}
|
|
|
|
/* 4. Start DNS Captive Portal server */
|
|
dns_server_start();
|
|
|
|
/* 5. Start HTTP Web Server */
|
|
httpd_config_t config = HTTPD_DEFAULT_CONFIG();
|
|
config.stack_size = 8192;
|
|
config.max_uri_handlers = 32;
|
|
config.uri_match_fn = httpd_uri_match_wildcard;
|
|
config.core_id = 0;
|
|
config.open_fn = httpd_open_fn;
|
|
|
|
err = httpd_start(&s_http_server, &config);
|
|
if (err == ESP_OK) {
|
|
httpd_uri_t uri_root = { .uri = "/", .method = HTTP_GET, .handler = index_get_handler };
|
|
httpd_register_uri_handler(s_http_server, &uri_root);
|
|
|
|
httpd_uri_t uri_idx = { .uri = "/index.html", .method = HTTP_GET, .handler = index_get_handler };
|
|
httpd_register_uri_handler(s_http_server, &uri_idx);
|
|
|
|
httpd_uri_t uri_gen204 = { .uri = "/generate_204", .method = HTTP_GET, .handler = captive_portal_handler };
|
|
httpd_register_uri_handler(s_http_server, &uri_gen204);
|
|
|
|
httpd_uri_t uri_hotspot = { .uri = "/hotspot-detect.html", .method = HTTP_GET, .handler = captive_portal_handler };
|
|
httpd_register_uri_handler(s_http_server, &uri_hotspot);
|
|
|
|
httpd_uri_t uri_ncsi = { .uri = "/ncsi.txt", .method = HTTP_GET, .handler = captive_portal_handler };
|
|
httpd_register_uri_handler(s_http_server, &uri_ncsi);
|
|
|
|
httpd_uri_t uri_conn = { .uri = "/connecttest.txt", .method = HTTP_GET, .handler = captive_portal_handler };
|
|
httpd_register_uri_handler(s_http_server, &uri_conn);
|
|
|
|
httpd_uri_t uri_status = { .uri = "/api/status", .method = HTTP_GET, .handler = api_status_handler };
|
|
httpd_register_uri_handler(s_http_server, &uri_status);
|
|
|
|
httpd_uri_t uri_state = { .uri = "/api/state", .method = HTTP_GET, .handler = api_state_handler };
|
|
httpd_register_uri_handler(s_http_server, &uri_state);
|
|
|
|
#if CONFIG_HTTPD_WS_SUPPORT
|
|
httpd_uri_t uri_ws_state = {
|
|
.uri = "/ws/state",
|
|
.method = HTTP_GET,
|
|
.handler = ws_state_handler,
|
|
.user_ctx = NULL,
|
|
.is_websocket = true
|
|
};
|
|
httpd_register_uri_handler(s_http_server, &uri_ws_state);
|
|
#endif
|
|
|
|
httpd_uri_t uri_mode = { .uri = "/api/mode", .method = HTTP_POST, .handler = api_mode_handler };
|
|
httpd_register_uri_handler(s_http_server, &uri_mode);
|
|
|
|
httpd_uri_t uri_combos_get = { .uri = "/api/combos", .method = HTTP_GET, .handler = api_combos_get_handler };
|
|
httpd_register_uri_handler(s_http_server, &uri_combos_get);
|
|
|
|
httpd_uri_t uri_combos_post = { .uri = "/api/combos", .method = HTTP_POST, .handler = api_combos_post_handler };
|
|
httpd_register_uri_handler(s_http_server, &uri_combos_post);
|
|
|
|
httpd_uri_t uri_led = { .uri = "/api/led", .method = HTTP_POST, .handler = api_led_handler };
|
|
httpd_register_uri_handler(s_http_server, &uri_led);
|
|
|
|
httpd_uri_t uri_disc = { .uri = "/api/ble/disconnect", .method = HTTP_POST, .handler = api_ble_disconnect_handler };
|
|
httpd_register_uri_handler(s_http_server, &uri_disc);
|
|
|
|
httpd_uri_t uri_clear = { .uri = "/api/ble/clear_bonds", .method = HTTP_POST, .handler = api_ble_clear_bonds_handler };
|
|
httpd_register_uri_handler(s_http_server, &uri_clear);
|
|
|
|
httpd_uri_t uri_rumble = { .uri = "/api/ble/rumble", .method = HTTP_POST, .handler = api_ble_rumble_handler };
|
|
httpd_register_uri_handler(s_http_server, &uri_rumble);
|
|
|
|
httpd_uri_t uri_peers = { .uri = "/api/ble/peers", .method = HTTP_GET, .handler = api_ble_peers_get_handler };
|
|
httpd_register_uri_handler(s_http_server, &uri_peers);
|
|
|
|
httpd_uri_t uri_unpair = { .uri = "/api/ble/unpair", .method = HTTP_POST, .handler = api_ble_unpair_handler };
|
|
httpd_register_uri_handler(s_http_server, &uri_unpair);
|
|
|
|
httpd_uri_t uri_remote = { .uri = "/api/remote/input", .method = HTTP_POST, .handler = api_remote_input_handler };
|
|
httpd_register_uri_handler(s_http_server, &uri_remote);
|
|
|
|
httpd_uri_t uri_calib_get = { .uri = "/api/calibration", .method = HTTP_GET, .handler = api_calibration_get_handler };
|
|
httpd_register_uri_handler(s_http_server, &uri_calib_get);
|
|
|
|
httpd_uri_t uri_calib_post = { .uri = "/api/calibration", .method = HTTP_POST, .handler = api_calibration_post_handler };
|
|
httpd_register_uri_handler(s_http_server, &uri_calib_post);
|
|
|
|
httpd_uri_t uri_calib_rst = { .uri = "/api/calibration/reset", .method = HTTP_POST, .handler = api_calibration_reset_handler };
|
|
httpd_register_uri_handler(s_http_server, &uri_calib_rst);
|
|
|
|
httpd_uri_t uri_scan = { .uri = "/api/wifi/scan", .method = HTTP_GET, .handler = api_wifi_scan_handler };
|
|
httpd_register_uri_handler(s_http_server, &uri_scan);
|
|
|
|
httpd_uri_t uri_wifi_conn = { .uri = "/api/wifi/connect", .method = HTTP_POST, .handler = api_wifi_connect_handler };
|
|
httpd_register_uri_handler(s_http_server, &uri_wifi_conn);
|
|
|
|
httpd_uri_t uri_wifi_forget = { .uri = "/api/wifi/forget", .method = HTTP_POST, .handler = api_wifi_forget_handler };
|
|
httpd_register_uri_handler(s_http_server, &uri_wifi_forget);
|
|
|
|
httpd_uri_t uri_exit = { .uri = "/api/exit", .method = HTTP_POST, .handler = api_exit_handler };
|
|
httpd_register_uri_handler(s_http_server, &uri_exit);
|
|
|
|
httpd_uri_t uri_ap_toggle = { .uri = "/api/ap/toggle", .method = HTTP_POST, .handler = api_ap_toggle_handler };
|
|
httpd_register_uri_handler(s_http_server, &uri_ap_toggle);
|
|
|
|
httpd_uri_t uri_ota_upload = { .uri = "/api/ota/upload", .method = HTTP_POST, .handler = api_ota_upload_handler };
|
|
httpd_register_uri_handler(s_http_server, &uri_ota_upload);
|
|
} else {
|
|
ESP_LOGE(TAG, "Failed to start HTTP server: %s", esp_err_to_name(err));
|
|
}
|
|
|
|
s_is_active = true;
|
|
status_led_set_mode(LED_STATE_CONFIG_PORTAL);
|
|
|
|
return ESP_OK;
|
|
}
|
|
|
|
static esp_err_t config_server_stop_internal(void)
|
|
{
|
|
if (!s_is_active) {
|
|
return ESP_OK;
|
|
}
|
|
|
|
ESP_LOGI(TAG, "Stopping Config Server and Wi-Fi...");
|
|
|
|
if (s_mdns_inited) {
|
|
mdns_free();
|
|
s_mdns_inited = false;
|
|
}
|
|
|
|
if (s_http_server) {
|
|
httpd_stop(s_http_server);
|
|
s_http_server = NULL;
|
|
}
|
|
|
|
dns_server_stop();
|
|
esp_wifi_stop();
|
|
|
|
s_is_active = false;
|
|
s_sta_connected = false;
|
|
s_sta_ip[0] = '\0';
|
|
|
|
ESP_LOGI(TAG, "Wi-Fi radio stopped. Radio fully dedicated to BLE gaming with 0 latency!");
|
|
profile_manager_refresh_led();
|
|
|
|
return ESP_OK;
|
|
}
|
|
|
|
/* -------------------------------------------------------------------------- */
|
|
/* Dedicated Config Task (Stack: 6144 bytes) */
|
|
/* -------------------------------------------------------------------------- */
|
|
|
|
static void config_mgr_task(void *pvParameters)
|
|
{
|
|
ESP_LOGI(TAG, "Config Manager Task running.");
|
|
|
|
while (1) {
|
|
/* Sleep until command notification is received */
|
|
ulTaskNotifyTake(pdTRUE, portMAX_DELAY);
|
|
|
|
if (s_pending_cmd != CONFIG_CMD_NONE) {
|
|
config_cmd_t cmd = s_pending_cmd;
|
|
s_pending_cmd = CONFIG_CMD_NONE;
|
|
|
|
if (cmd == CONFIG_CMD_TOGGLE) {
|
|
if (s_is_active) {
|
|
config_server_stop_internal();
|
|
} else {
|
|
config_server_start_internal();
|
|
}
|
|
} else if (cmd == CONFIG_CMD_START) {
|
|
if (!s_is_active) {
|
|
config_server_start_internal();
|
|
}
|
|
} else if (cmd == CONFIG_CMD_STOP) {
|
|
if (s_is_active) {
|
|
config_server_stop_internal();
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
/* -------------------------------------------------------------------------- */
|
|
/* Public API */
|
|
/* -------------------------------------------------------------------------- */
|
|
|
|
esp_err_t config_server_init(void)
|
|
{
|
|
if (!s_state_mutex) {
|
|
s_state_mutex = xSemaphoreCreateMutex();
|
|
}
|
|
if (!s_config_task_handle) {
|
|
BaseType_t ret = xTaskCreatePinnedToCore(config_mgr_task, "cfg_mgr_task", 6144, NULL, 5, &s_config_task_handle, 0);
|
|
if (ret != pdPASS) {
|
|
ESP_LOGE(TAG, "Failed to create config manager task");
|
|
return ESP_FAIL;
|
|
}
|
|
}
|
|
return ESP_OK;
|
|
}
|
|
|
|
void config_server_request_toggle(void)
|
|
{
|
|
s_pending_cmd = CONFIG_CMD_TOGGLE;
|
|
if (s_config_task_handle) {
|
|
xTaskNotifyGive(s_config_task_handle);
|
|
}
|
|
}
|
|
|
|
void config_server_request_start(void)
|
|
{
|
|
s_pending_cmd = CONFIG_CMD_START;
|
|
if (s_config_task_handle) {
|
|
xTaskNotifyGive(s_config_task_handle);
|
|
}
|
|
}
|
|
|
|
void config_server_request_stop(void)
|
|
{
|
|
s_pending_cmd = CONFIG_CMD_STOP;
|
|
if (s_config_task_handle) {
|
|
xTaskNotifyGive(s_config_task_handle);
|
|
}
|
|
}
|
|
|
|
void config_server_toggle(void)
|
|
{
|
|
config_server_request_toggle();
|
|
}
|
|
|
|
bool config_server_is_active(void)
|
|
{
|
|
return s_is_active;
|
|
}
|
|
|
|
void config_server_set_latest_state(const controller_state_t *state)
|
|
{
|
|
if (!state || !s_state_mutex) return;
|
|
if (xSemaphoreTake(s_state_mutex, 0) == pdTRUE) {
|
|
s_latest_state = *state;
|
|
xSemaphoreGive(s_state_mutex);
|
|
}
|
|
}
|
|
|
|
bool config_server_has_saved_wifi(void)
|
|
{
|
|
char ssid[33] = {0};
|
|
char pass[65] = {0};
|
|
return read_saved_wifi_creds(ssid, sizeof(ssid), pass, sizeof(pass));
|
|
}
|