|
|
@@ -237,6 +237,25 @@ static void handle_api_settings(struct mg_connection *c, struct mg_http_message
|
|
|
char param_copy[64];
|
|
|
snprintf(param_copy, sizeof(param_copy), "%s", param);
|
|
|
|
|
|
+ /* Validate device address (0-247) */
|
|
|
+ if (dev_addr == 0 || dev_addr > 247) {
|
|
|
+ char err_msg[128];
|
|
|
+ snprintf(err_msg, sizeof(err_msg),
|
|
|
+ "{\"error\":\"Invalid device_addr: %d. Must be 1-247.\"}", dev_addr);
|
|
|
+ cJSON_Delete(req);
|
|
|
+ mg_http_reply(c, 400, "Content-Type: application/json\r\nAccess-Control-Allow-Origin: *\r\n",
|
|
|
+ "%s", err_msg);
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ /* Check if Modbus is connected BEFORE validation */
|
|
|
+ if (!g_ctx.modbus || !g_ctx.modbus->connected) {
|
|
|
+ cJSON_Delete(req);
|
|
|
+ mg_http_reply(c, 503, "Content-Type: application/json\r\nAccess-Control-Allow-Origin: *\r\n",
|
|
|
+ "{\"error\":\"Modbus not connected. Please check device connection.\"}");
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
/* Map parameter name to register address */
|
|
|
uint16_t reg_addr = 0xFFFF;
|
|
|
const char *param_desc = "";
|
|
|
@@ -260,7 +279,7 @@ static void handle_api_settings(struct mg_connection *c, struct mg_http_message
|
|
|
param_desc = "充电模式";
|
|
|
} else {
|
|
|
cJSON_Delete(req);
|
|
|
- mg_http_reply(c, 400, "Content-Type: application/json\r\n",
|
|
|
+ mg_http_reply(c, 400, "Content-Type: application/json\r\nAccess-Control-Allow-Origin: *\r\n",
|
|
|
"{\"error\":\"Unknown param. Valid: battery_type, battery_level, "
|
|
|
"charge_current_limit, full_charge_voltage, device_address, charge_mode\"}");
|
|
|
return;
|
|
|
@@ -273,22 +292,15 @@ static void handle_api_settings(struct mg_connection *c, struct mg_http_message
|
|
|
current_battery_type = dev_data.battery_type_raw;
|
|
|
}
|
|
|
|
|
|
- /* Validate */
|
|
|
+ /* Validate value range */
|
|
|
if (modbus_validate_setting(reg_addr, value, current_battery_type) != 0) {
|
|
|
char err_msg[256];
|
|
|
snprintf(err_msg, sizeof(err_msg),
|
|
|
"{\"error\":\"Invalid value for %s (reg 0x%04X). Value %u out of range.\"}",
|
|
|
param_copy, reg_addr, value);
|
|
|
cJSON_Delete(req);
|
|
|
- mg_http_reply(c, 400, "Content-Type: application/json\r\n", "%s", err_msg);
|
|
|
- return;
|
|
|
- }
|
|
|
-
|
|
|
- /* Check if Modbus is connected */
|
|
|
- if (!g_ctx.modbus || !g_ctx.modbus->connected) {
|
|
|
- cJSON_Delete(req);
|
|
|
- mg_http_reply(c, 503, "Content-Type: application/json\r\n",
|
|
|
- "{\"error\":\"Modbus not connected\"}");
|
|
|
+ mg_http_reply(c, 400, "Content-Type: application/json\r\nAccess-Control-Allow-Origin: *\r\n",
|
|
|
+ "%s", err_msg);
|
|
|
return;
|
|
|
}
|
|
|
|