feat(vd960DBN): MQTT ssc_net_set / iot_net_set / iot_topic_set 实现

- ssc_net_set: 解析 dev_ip/subnet_mask/route_ip/lssc_ip/dns/port,调用 write_net_config
- iot_net_set: 解析 host/port/client_id/username/password,调用 write_net_config
- iot_topic_set: 解析 client_id_enable/topic_pub/topic_sub,调用 write_net_config
- 三个 SET 命令均写入 flash 并在完成后返回 code=0 success
This commit is contained in:
wangfq
2026-07-07 21:15:55 +08:00
parent 2bbe388738
commit de2e70fd89
@@ -1188,6 +1188,77 @@ void manage_mqtt_recv_message(char * msg, int length)
return;
}
// --- ssc_net_set ---
if(strcmp(cmd_str, "ssc_net_set") == 0) {
Local_Net_Cfg lcfg = local_net_cfg;
NET_CENTER_INFO ccfg = net_center_info;
char *data_buf = (char *)malloc(256);
if(data_buf) {
memset(data_buf, 0, 256);
simple_parse_json(msg, "\"data\"", data_buf);
if(strlen(data_buf) > 0) {
char ip_str[32];
int plen;
// dev_ip
memset(ip_str, 0, sizeof(ip_str));
simple_parse_json(data_buf, "\"dev_ip\"", ip_str);
if(strlen(ip_str) > 0) {
char *s = ip_str; if(s[0]=='"') s++; plen=strlen(s); if(plen>0&&s[plen-1]=='"') s[plen-1]=0;
get_ipstr_to_array(s, lcfg.lip);
}
// subnet_mask
memset(ip_str, 0, sizeof(ip_str));
simple_parse_json(data_buf, "\"subnet_mask\"", ip_str);
if(strlen(ip_str) > 0) {
char *s = ip_str; if(s[0]=='"') s++; plen=strlen(s); if(plen>0&&s[plen-1]=='"') s[plen-1]=0;
get_ipstr_to_array(s, lcfg.sub);
}
// route_ip
memset(ip_str, 0, sizeof(ip_str));
simple_parse_json(data_buf, "\"route_ip\"", ip_str);
if(strlen(ip_str) > 0) {
char *s = ip_str; if(s[0]=='"') s++; plen=strlen(s); if(plen>0&&s[plen-1]=='"') s[plen-1]=0;
get_ipstr_to_array(s, lcfg.gw);
}
// lssc_ip
memset(ip_str, 0, sizeof(ip_str));
simple_parse_json(data_buf, "\"lssc_ip\"", ip_str);
if(strlen(ip_str) > 0) {
char *s = ip_str; if(s[0]=='"') s++; plen=strlen(s); if(plen>0&&s[plen-1]=='"') s[plen-1]=0;
get_ipstr_to_array(s, ccfg.lssc_ip);
}
// dns
memset(ip_str, 0, sizeof(ip_str));
simple_parse_json(data_buf, "\"dns\"", ip_str);
if(strlen(ip_str) > 0) {
char *s = ip_str; if(s[0]=='"') s++; plen=strlen(s); if(plen>0&&s[plen-1]=='"') s[plen-1]=0;
get_ipstr_to_array(s, lcfg.dns);
}
// port
memset(tmp, 0, sizeof(tmp));
simple_parse_json(data_buf, "\"port\"", tmp);
if(strlen(tmp) > 0) {
uint32_t p = strtoul(tmp, NULL, 10);
if(p > 0 && p <= 65535) ccfg.tcp_port = (uint16_t)p;
}
}
free(data_buf);
}
write_net_config(&lcfg, &ccfg, &iot_net_info, &g_iot_topic);
local_net_cfg = lcfg;
net_center_info = ccfg;
char resp[256];
snprintf(resp, sizeof(resp),
"{\"msg_id\":%lu,\"cmd\":\"ssc_net_set\","
"\"ts\":%lu,\"code\":0,\"msg\":\"success\"}",
msg_id, mstick() / 1000);
mqtt_publish(resp_topic, resp, 1);
PRINT("IOT: ssc_net_set done\n");
return;
}
// --- iot_net_query ---
if(strcmp(cmd_str, "iot_net_query") == 0) {
char resp[512];
@@ -1204,6 +1275,68 @@ void manage_mqtt_recv_message(char * msg, int length)
return;
}
// --- iot_net_set ---
if(strcmp(cmd_str, "iot_net_set") == 0) {
IOT_NET_INFO icfg = iot_net_info;
char *data_buf = (char *)malloc(256);
if(data_buf) {
memset(data_buf, 0, 256);
simple_parse_json(msg, "\"data\"", data_buf);
if(strlen(data_buf) > 0) {
char fld[64];
int plen;
// host
memset(fld, 0, sizeof(fld));
simple_parse_json(data_buf, "\"host\"", fld);
if(strlen(fld) > 0) {
char *s = fld; if(s[0]=='"') s++; plen=strlen(s); if(plen>0&&s[plen-1]=='"') s[plen-1]=0;
strncpy((char *)icfg.remote_addr, s, 63);
}
// port
memset(tmp, 0, sizeof(tmp));
simple_parse_json(data_buf, "\"port\"", tmp);
if(strlen(tmp) > 0) {
uint32_t p = strtoul(tmp, NULL, 10);
if(p > 0 && p <= 65535) icfg.mqtt_port = (uint16_t)p;
}
// client_id
memset(fld, 0, sizeof(fld));
simple_parse_json(data_buf, "\"client_id\"", fld);
if(strlen(fld) > 0) {
char *s = fld; if(s[0]=='"') s++; plen=strlen(s); if(plen>0&&s[plen-1]=='"') s[plen-1]=0;
strncpy((char *)icfg.client_id, s, 63);
}
// username
memset(fld, 0, sizeof(fld));
simple_parse_json(data_buf, "\"username\"", fld);
if(strlen(fld) > 0) {
char *s = fld; if(s[0]=='"') s++; plen=strlen(s); if(plen>0&&s[plen-1]=='"') s[plen-1]=0;
strncpy((char *)icfg.username, s, 63);
}
// password
memset(fld, 0, sizeof(fld));
simple_parse_json(data_buf, "\"password\"", fld);
if(strlen(fld) > 0) {
char *s = fld; if(s[0]=='"') s++; plen=strlen(s); if(plen>0&&s[plen-1]=='"') s[plen-1]=0;
strncpy((char *)icfg.password, s, 31);
}
}
free(data_buf);
}
write_net_config(&local_net_cfg, &net_center_info, &icfg, &g_iot_topic);
iot_net_info = icfg;
char resp[256];
snprintf(resp, sizeof(resp),
"{\"msg_id\":%lu,\"cmd\":\"iot_net_set\","
"\"ts\":%lu,\"code\":0,\"msg\":\"success\"}",
msg_id, mstick() / 1000);
mqtt_publish(resp_topic, resp, 1);
PRINT("IOT: iot_net_set done\n");
return;
}
// --- iot_topic_query ---
if(strcmp(cmd_str, "iot_topic_query") == 0) {
char resp[512];
@@ -1220,6 +1353,52 @@ void manage_mqtt_recv_message(char * msg, int length)
return;
}
// --- iot_topic_set ---
if(strcmp(cmd_str, "iot_topic_set") == 0) {
IOT_Topic topic = g_iot_topic;
char *data_buf = (char *)malloc(256);
if(data_buf) {
memset(data_buf, 0, 256);
simple_parse_json(msg, "\"data\"", data_buf);
if(strlen(data_buf) > 0) {
char fld[64];
int plen;
// client_id_enable (bool)
memset(tmp, 0, sizeof(tmp));
simple_parse_json(data_buf, "\"client_id_enable\"", tmp);
if(strlen(tmp) > 0) topic.clientid_enable = (uint8_t)(strstr(tmp, "true") ? 1 : 0);
// topic_pub
memset(fld, 0, sizeof(fld));
simple_parse_json(data_buf, "\"topic_pub\"", fld);
if(strlen(fld) > 0) {
char *s = fld; if(s[0]=='"') s++; plen=strlen(s); if(plen>0&&s[plen-1]=='"') s[plen-1]=0;
strncpy((char *)topic.topic_pub, s, 63);
}
// topic_sub
memset(fld, 0, sizeof(fld));
simple_parse_json(data_buf, "\"topic_sub\"", fld);
if(strlen(fld) > 0) {
char *s = fld; if(s[0]=='"') s++; plen=strlen(s); if(plen>0&&s[plen-1]=='"') s[plen-1]=0;
strncpy((char *)topic.topic_sub, s, 63);
}
}
free(data_buf);
}
write_net_config(&local_net_cfg, &net_center_info, &iot_net_info, &topic);
g_iot_topic = topic;
char resp[256];
snprintf(resp, sizeof(resp),
"{\"msg_id\":%lu,\"cmd\":\"iot_topic_set\","
"\"ts\":%lu,\"code\":0,\"msg\":\"success\"}",
msg_id, mstick() / 1000);
mqtt_publish(resp_topic, resp, 1);
PRINT("IOT: iot_topic_set done\n");
return;
}
// --- pwd_set ---
if(strcmp(cmd_str, "pwd_set") == 0) {
char resp[256];