From de2e70fd89ecff408b900f91597cc4a2f9cf3dd0 Mon Sep 17 00:00:00 2001 From: wangfq Date: Tue, 7 Jul 2026 21:15:55 +0800 Subject: [PATCH] =?UTF-8?q?feat(vd960DBN):=20MQTT=20ssc=5Fnet=5Fset=20/=20?= =?UTF-8?q?iot=5Fnet=5Fset=20/=20iot=5Ftopic=5Fset=20=E5=AE=9E=E7=8E=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 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 --- .../OnlyUpdateApp_Peripheral/APP/net_srv.c | 185 +++++++++++++++++- 1 file changed, 182 insertions(+), 3 deletions(-) diff --git a/vd960DBN/BLE/OnlyUpdateApp_Peripheral/APP/net_srv.c b/vd960DBN/BLE/OnlyUpdateApp_Peripheral/APP/net_srv.c index f3ebdd1..daf2134 100644 --- a/vd960DBN/BLE/OnlyUpdateApp_Peripheral/APP/net_srv.c +++ b/vd960DBN/BLE/OnlyUpdateApp_Peripheral/APP/net_srv.c @@ -1187,7 +1187,78 @@ void manage_mqtt_recv_message(char * msg, int length) mqtt_publish(resp_topic, resp, 1); 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]; @@ -1203,7 +1274,69 @@ void manage_mqtt_recv_message(char * msg, int length) mqtt_publish(resp_topic, resp, 1); 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]; @@ -1219,7 +1352,53 @@ void manage_mqtt_recv_message(char * msg, int length) mqtt_publish(resp_topic, resp, 1); 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];