207 lines
7.2 KiB
Python
207 lines
7.2 KiB
Python
#!/usr/bin/env python3
|
|
"""
|
|
Update web_server.c with enhanced UI and add Bluetooth/advanced API endpoints
|
|
"""
|
|
|
|
import re
|
|
|
|
# Read the enhanced UI C string
|
|
with open('enhanced_ui_c_string.txt', 'r') as f:
|
|
enhanced_html = f.read()
|
|
|
|
# Read current web_server.c
|
|
with open('web_server.c', 'r') as f:
|
|
web_server_content = f.read()
|
|
|
|
# Find the HTML section (from "static const char index_html[]" to "</html>";)
|
|
pattern = r'(static const char index_html\[\] = ).*?("</html>";)'
|
|
match = re.search(pattern, web_server_content, re.DOTALL)
|
|
|
|
if match:
|
|
# Replace the HTML section
|
|
# Extract the enhanced HTML (remove the "static const char index_html[] = " part from our file)
|
|
enhanced_html_clean = enhanced_html.replace('static const char index_html[] = \\\n', '').rstrip(';')
|
|
|
|
# Replace in web_server.c
|
|
new_content = web_server_content[:match.start()] + enhanced_html_clean + ';' + web_server_content[match.end():]
|
|
|
|
# Add Bluetooth API handlers before the register_uri_handlers function
|
|
bt_handlers = '''
|
|
// Bluetooth API handlers
|
|
static esp_err_t api_bt_scan_start_handler(httpd_req_t *req) {
|
|
httpd_resp_set_type(req, "application/json");
|
|
bool success = bt_scan_start();
|
|
if (success) {
|
|
httpd_resp_sendstr(req, "{\\"status\\":\\"success\\",\\"message\\":\\"BT scan started\\"}");
|
|
} else {
|
|
httpd_resp_sendstr(req, "{\\"status\\":\\"error\\",\\"message\\":\\"Failed to start BT scan\\"}");
|
|
}
|
|
return ESP_OK;
|
|
}
|
|
|
|
static esp_err_t api_bt_scan_stop_handler(httpd_req_t *req) {
|
|
httpd_resp_set_type(req, "application/json");
|
|
bool success = bt_scan_stop();
|
|
if (success) {
|
|
httpd_resp_sendstr(req, "{\\"status\\":\\"success\\",\\"message\\":\\"BT scan stopped\\"}");
|
|
} else {
|
|
httpd_resp_sendstr(req, "{\\"status\\":\\"error\\",\\"message\\":\\"Failed to stop BT scan\\"}");
|
|
}
|
|
return ESP_OK;
|
|
}
|
|
|
|
static esp_err_t api_bt_devices_handler(httpd_req_t *req) {
|
|
httpd_resp_set_type(req, "application/json");
|
|
bt_device_t devices[50];
|
|
int count = bt_get_devices(devices, 50);
|
|
|
|
cJSON *root = cJSON_CreateObject();
|
|
cJSON *devices_array = cJSON_AddArrayToObject(root, "devices");
|
|
|
|
for (int i = 0; i < count; i++) {
|
|
cJSON *device = cJSON_CreateObject();
|
|
cJSON_AddStringToObject(device, "name", devices[i].name);
|
|
char addr_str[18];
|
|
snprintf(addr_str, sizeof(addr_str), "%02x:%02x:%02x:%02x:%02x:%02x",
|
|
devices[i].addr[0], devices[i].addr[1], devices[i].addr[2],
|
|
devices[i].addr[3], devices[i].addr[4], devices[i].addr[5]);
|
|
cJSON_AddStringToObject(device, "addr", addr_str);
|
|
cJSON_AddNumberToObject(device, "rssi", devices[i].rssi);
|
|
cJSON_AddNumberToObject(device, "type", devices[i].adv_type);
|
|
cJSON_AddItemToArray(devices_array, device);
|
|
}
|
|
|
|
char *json_response = cJSON_PrintUnformatted(root);
|
|
httpd_resp_sendstr(req, json_response);
|
|
free(json_response);
|
|
cJSON_Delete(root);
|
|
return ESP_OK;
|
|
}
|
|
|
|
static esp_err_t api_bt_jam_start_handler(httpd_req_t *req) {
|
|
httpd_resp_set_type(req, "application/json");
|
|
char buf[256];
|
|
int ret = httpd_req_recv(req, buf, sizeof(buf)-1);
|
|
if (ret <= 0) {
|
|
httpd_resp_sendstr(req, "{\\"status\\":\\"error\\",\\"message\\":\\"No data\\"}");
|
|
return ESP_FAIL;
|
|
}
|
|
buf[ret] = 0;
|
|
|
|
cJSON *root = cJSON_Parse(buf);
|
|
if (!root) {
|
|
httpd_resp_sendstr(req, "{\\"status\\":\\"error\\",\\"message\\":\\"Bad JSON\\"}");
|
|
return ESP_FAIL;
|
|
}
|
|
|
|
uint8_t *target_addr = NULL;
|
|
uint32_t duration = 30;
|
|
|
|
cJSON *target = cJSON_GetObjectItem(root, "target");
|
|
if (target && cJSON_IsString(target)) {
|
|
// Parse target address
|
|
target_addr = malloc(6);
|
|
if (target_addr) {
|
|
sscanf(target->valuestring, "%hhx:%hhx:%hhx:%hhx:%hhx:%hhx",
|
|
&target_addr[0], &target_addr[1], &target_addr[2],
|
|
&target_addr[3], &target_addr[4], &target_addr[5]);
|
|
}
|
|
}
|
|
|
|
cJSON *dur = cJSON_GetObjectItem(root, "duration");
|
|
if (dur && cJSON_IsNumber(dur)) {
|
|
duration = (uint32_t)dur->valueint;
|
|
}
|
|
|
|
bool success = bt_jam_start(target_addr, duration);
|
|
if (target_addr) free(target_addr);
|
|
|
|
cJSON_Delete(root);
|
|
|
|
if (success) {
|
|
httpd_resp_sendstr(req, "{\\"status\\":\\"success\\",\\"message\\":\\"BT jamming started\\"}");
|
|
} else {
|
|
httpd_resp_sendstr(req, "{\\"status\\":\\"error\\",\\"message\\":\\"Failed to start jamming\\"}");
|
|
}
|
|
return ESP_OK;
|
|
}
|
|
|
|
static esp_err_t api_bt_jam_stop_handler(httpd_req_t *req) {
|
|
httpd_resp_set_type(req, "application/json");
|
|
bool success = bt_jam_stop();
|
|
if (success) {
|
|
httpd_resp_sendstr(req, "{\\"status\\":\\"success\\",\\"message\\":\\"BT jamming stopped\\"}");
|
|
} else {
|
|
httpd_resp_sendstr(req, "{\\"status\\":\\"error\\",\\"message\\":\\"Jamming not active\\"}");
|
|
}
|
|
return ESP_OK;
|
|
}
|
|
|
|
'''
|
|
|
|
# Insert BT handlers before register_uri_handlers
|
|
register_pattern = r'(// Register URI handlers|esp_err_t register_uri_handlers)'
|
|
register_match = re.search(register_pattern, new_content)
|
|
if register_match:
|
|
new_content = new_content[:register_match.start()] + bt_handlers + new_content[register_match.start():]
|
|
|
|
# Add BT URI registrations in register_uri_handlers function
|
|
bt_registrations = '''
|
|
// Register Bluetooth endpoints
|
|
httpd_uri_t bt_scan_start_uri = {
|
|
.uri = "/api/bt/scan/start",
|
|
.method = HTTP_GET,
|
|
.handler = api_bt_scan_start_handler,
|
|
.user_ctx = NULL
|
|
};
|
|
httpd_register_uri_handler(server, &bt_scan_start_uri);
|
|
|
|
httpd_uri_t bt_scan_stop_uri = {
|
|
.uri = "/api/bt/scan/stop",
|
|
.method = HTTP_GET,
|
|
.handler = api_bt_scan_stop_handler,
|
|
.user_ctx = NULL
|
|
};
|
|
httpd_register_uri_handler(server, &bt_scan_stop_uri);
|
|
|
|
httpd_uri_t bt_devices_uri = {
|
|
.uri = "/api/bt/devices",
|
|
.method = HTTP_GET,
|
|
.handler = api_bt_devices_handler,
|
|
.user_ctx = NULL
|
|
};
|
|
httpd_register_uri_handler(server, &bt_devices_uri);
|
|
|
|
httpd_uri_t bt_jam_start_uri = {
|
|
.uri = "/api/bt/jam/start",
|
|
.method = HTTP_POST,
|
|
.handler = api_bt_jam_start_handler,
|
|
.user_ctx = NULL
|
|
};
|
|
httpd_register_uri_handler(server, &bt_jam_start_uri);
|
|
|
|
httpd_uri_t bt_jam_stop_uri = {
|
|
.uri = "/api/bt/jam/stop",
|
|
.method = HTTP_GET,
|
|
.handler = api_bt_jam_stop_handler,
|
|
.user_ctx = NULL
|
|
};
|
|
httpd_register_uri_handler(server, &bt_jam_stop_uri);
|
|
|
|
'''
|
|
|
|
# Find where to insert BT registrations (after deauth registrations)
|
|
deauth_reg_pattern = r'(httpd_register_uri_handler\(server, &deauth_status_uri\);|// Register reboot endpoint)'
|
|
deauth_reg_match = re.search(deauth_reg_pattern, new_content)
|
|
if deauth_reg_match:
|
|
new_content = new_content[:deauth_reg_match.start()] + bt_registrations + new_content[deauth_reg_match.start():]
|
|
|
|
# Write updated file
|
|
with open('web_server.c', 'w') as f:
|
|
f.write(new_content)
|
|
|
|
print("Updated web_server.c with enhanced UI and Bluetooth APIs")
|
|
print("Added Bluetooth scan, devices, and jamming endpoints")
|
|
else:
|
|
print("ERROR: Could not find HTML section in web_server.c")
|