feat: manage_mqtt_recv_message 实现 V1.01 协议命令分发
net_srv.c: manage_mqtt_recv_message() 从占位打印升级为完整命令分发器:
已实现(8条):
- dev_info_query: 返回设备信息(序列码/版本/子码)
- ssc_net_query: 返回 SSC 网络配置
- iot_net_query: 返回 IoT 网络配置(MQTT broker)
- iot_topic_query: 返回 topic 配置
- pwd_verify: 验证设备密码
- factory_reset: 出厂初始化+重启
- device_reset: 设备复位
未实现(返回 code=4):
- dev_serial_set, pwd_set, ssc_net_set 等 SET 类命令
所有响应通过 g_iot_topic.topic_pub (dld960/{sn}/dev) 发布
This commit is contained in:
@@ -1075,14 +1075,192 @@ void manage_mqtt_recv_message(char * msg, int length)
|
|||||||
if(strstr(temp_guide, "Heartbeat") != NULL)
|
if(strstr(temp_guide, "Heartbeat") != NULL)
|
||||||
{
|
{
|
||||||
dev_response_heartbeat(NULL);
|
dev_response_heartbeat(NULL);
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
// 新协议: 解析 cmd 并打印(后续可扩展命令处理)
|
|
||||||
else if(strlen(temp_guide) > 0)
|
// === V1.01 新协议: cmd 命令分发 ===
|
||||||
|
// 解析 msg_id
|
||||||
|
char tmp[32] = {0};
|
||||||
|
uint32_t msg_id = 0;
|
||||||
|
simple_parse_json(msg, "\"msg_id\"", tmp);
|
||||||
|
if(strlen(tmp) > 0) msg_id = (uint32_t)strtoul(tmp, NULL, 10);
|
||||||
|
|
||||||
|
// 去掉 temp_guide 里的引号
|
||||||
|
char *cmd_str = temp_guide;
|
||||||
|
if(cmd_str[0] == '"') cmd_str++;
|
||||||
|
int cmd_len = strlen(cmd_str);
|
||||||
|
if(cmd_len > 0 && cmd_str[cmd_len - 1] == '"') cmd_str[cmd_len - 1] = '\0';
|
||||||
|
|
||||||
|
// 响应 topic: g_iot_topic.topic_pub (= dld960/{sn}/dev)
|
||||||
|
char *resp_topic = (char *)g_iot_topic.topic_pub;
|
||||||
|
|
||||||
|
// --- dev_info_query ---
|
||||||
|
if(strcmp(cmd_str, "dev_info_query") == 0) {
|
||||||
|
char resp[600];
|
||||||
|
snprintf(resp, sizeof(resp),
|
||||||
|
"{\"msg_id\":%lu,\"cmd\":\"dev_info_query\","
|
||||||
|
"\"ts\":%lu,\"code\":0,\"msg\":\"success\","
|
||||||
|
"\"data\":{"
|
||||||
|
"\"dev_serial\":\"%s\",\"hard_ver\":\"%s\",\"soft_ver\":\"%s\","
|
||||||
|
"\"model\":\"%s\",\"product_code\":\"960001\","
|
||||||
|
"\"sub_code\":{\"net\":%s,\"iot\":%s},"
|
||||||
|
"\"bus\":{\"bus1\":0,\"bus2\":0,\"bus3\":0,\"bus4\":0}}}",
|
||||||
|
msg_id, mstick() / 1000,
|
||||||
|
g_dev_number_str, HARDWARE_VER, FIRMWARE_VER, PRODUCT_MODEL,
|
||||||
|
g_sub_code_enable.net_enable ? "true" : "false",
|
||||||
|
g_sub_code_enable.iot_enable ? "true" : "false");
|
||||||
|
mqtt_publish(resp_topic, resp, 1);
|
||||||
|
PRINT("IOT: dev_info_query → response\n");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// --- pwd_verify ---
|
||||||
|
if(strcmp(cmd_str, "pwd_verify") == 0) {
|
||||||
|
char *data_buf = (char *)malloc(256);
|
||||||
|
char password[16] = {0};
|
||||||
|
if(data_buf) {
|
||||||
|
memset(data_buf, 0, 256);
|
||||||
|
simple_parse_json(msg, "\"data\"", data_buf);
|
||||||
|
if(strlen(data_buf) > 0) {
|
||||||
|
memset(tmp, 0, sizeof(tmp));
|
||||||
|
simple_parse_json(data_buf, "\"password\"", tmp);
|
||||||
|
char *pwd = tmp;
|
||||||
|
if(pwd[0] == '"') pwd++;
|
||||||
|
int plen = strlen(pwd);
|
||||||
|
if(plen > 0 && pwd[plen - 1] == '"') pwd[plen - 1] = '\0';
|
||||||
|
strncpy(password, pwd, 15);
|
||||||
|
}
|
||||||
|
free(data_buf);
|
||||||
|
}
|
||||||
|
|
||||||
|
if(strlen(password) == 6 && memcmp(password, g_dev_password, 6) == 0) {
|
||||||
|
char resp[256];
|
||||||
|
snprintf(resp, sizeof(resp),
|
||||||
|
"{\"msg_id\":%lu,\"cmd\":\"pwd_verify\","
|
||||||
|
"\"ts\":%lu,\"code\":0,\"msg\":\"success\"}",
|
||||||
|
msg_id, mstick() / 1000);
|
||||||
|
mqtt_publish(resp_topic, resp, 1);
|
||||||
|
PRINT("IOT: pwd_verify OK\n");
|
||||||
|
} else {
|
||||||
|
char resp[256];
|
||||||
|
snprintf(resp, sizeof(resp),
|
||||||
|
"{\"msg_id\":%lu,\"cmd\":\"pwd_verify\","
|
||||||
|
"\"ts\":%lu,\"code\":2,\"msg\":\"password incorrect\"}",
|
||||||
|
msg_id, mstick() / 1000);
|
||||||
|
mqtt_publish(resp_topic, resp, 1);
|
||||||
|
}
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// --- dev_serial_set ---
|
||||||
|
if(strcmp(cmd_str, "dev_serial_set") == 0) {
|
||||||
|
// TODO: 实现序列号修改
|
||||||
|
char resp[256];
|
||||||
|
snprintf(resp, sizeof(resp),
|
||||||
|
"{\"msg_id\":%lu,\"cmd\":\"dev_serial_set\","
|
||||||
|
"\"ts\":%lu,\"code\":4,\"msg\":\"not implemented yet\"}",
|
||||||
|
msg_id, mstick() / 1000);
|
||||||
|
mqtt_publish(resp_topic, resp, 1);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// --- ssc_net_query ---
|
||||||
|
if(strcmp(cmd_str, "ssc_net_query") == 0) {
|
||||||
|
char resp[512];
|
||||||
|
snprintf(resp, sizeof(resp),
|
||||||
|
"{\"msg_id\":%lu,\"cmd\":\"ssc_net_query\","
|
||||||
|
"\"ts\":%lu,\"code\":0,\"msg\":\"success\","
|
||||||
|
"\"data\":{"
|
||||||
|
"\"dev_ip\":\"%d.%d.%d.%d\","
|
||||||
|
"\"subnet_mask\":\"%d.%d.%d.%d\","
|
||||||
|
"\"route_ip\":\"%d.%d.%d.%d\","
|
||||||
|
"\"lssc_ip\":\"%d.%d.%d.%d\","
|
||||||
|
"\"dns\":\"%d.%d.%d.%d\","
|
||||||
|
"\"port\":%d}}",
|
||||||
|
msg_id, mstick() / 1000,
|
||||||
|
local_net_cfg.lip[0], local_net_cfg.lip[1], local_net_cfg.lip[2], local_net_cfg.lip[3],
|
||||||
|
local_net_cfg.sub[0], local_net_cfg.sub[1], local_net_cfg.sub[2], local_net_cfg.sub[3],
|
||||||
|
local_net_cfg.gw[0], local_net_cfg.gw[1], local_net_cfg.gw[2], local_net_cfg.gw[3],
|
||||||
|
net_center_info.lssc_ip[0], net_center_info.lssc_ip[1], net_center_info.lssc_ip[2], net_center_info.lssc_ip[3],
|
||||||
|
local_net_cfg.dns[0], local_net_cfg.dns[1], local_net_cfg.dns[2], local_net_cfg.dns[3],
|
||||||
|
local_net_cfg.port_ssc_tcp);
|
||||||
|
mqtt_publish(resp_topic, resp, 1);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// --- iot_net_query ---
|
||||||
|
if(strcmp(cmd_str, "iot_net_query") == 0) {
|
||||||
|
char resp[512];
|
||||||
|
snprintf(resp, sizeof(resp),
|
||||||
|
"{\"msg_id\":%lu,\"cmd\":\"iot_net_query\","
|
||||||
|
"\"ts\":%lu,\"code\":0,\"msg\":\"success\","
|
||||||
|
"\"data\":{"
|
||||||
|
"\"host\":\"%s\",\"port\":%d,"
|
||||||
|
"\"client_id\":\"%s\",\"username\":\"%s\",\"password\":\"%s\"}}",
|
||||||
|
msg_id, mstick() / 1000,
|
||||||
|
iot_net_info.remote_addr, iot_net_info.mqtt_port,
|
||||||
|
iot_net_info.client_id, iot_net_info.username, iot_net_info.password);
|
||||||
|
mqtt_publish(resp_topic, resp, 1);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// --- iot_topic_query ---
|
||||||
|
if(strcmp(cmd_str, "iot_topic_query") == 0) {
|
||||||
|
char resp[512];
|
||||||
|
snprintf(resp, sizeof(resp),
|
||||||
|
"{\"msg_id\":%lu,\"cmd\":\"iot_topic_query\","
|
||||||
|
"\"ts\":%lu,\"code\":0,\"msg\":\"success\","
|
||||||
|
"\"data\":{"
|
||||||
|
"\"client_id_enable\":%s,"
|
||||||
|
"\"topic_pub\":\"%s\",\"topic_sub\":\"%s\"}}",
|
||||||
|
msg_id, mstick() / 1000,
|
||||||
|
g_iot_topic.clientid_enable ? "true" : "false",
|
||||||
|
g_iot_topic.topic_pub, g_iot_topic.topic_sub);
|
||||||
|
mqtt_publish(resp_topic, resp, 1);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// --- pwd_set ---
|
||||||
|
if(strcmp(cmd_str, "pwd_set") == 0) {
|
||||||
|
char resp[256];
|
||||||
|
snprintf(resp, sizeof(resp),
|
||||||
|
"{\"msg_id\":%lu,\"cmd\":\"pwd_set\","
|
||||||
|
"\"ts\":%lu,\"code\":4,\"msg\":\"not implemented yet\"}",
|
||||||
|
msg_id, mstick() / 1000);
|
||||||
|
mqtt_publish(resp_topic, resp, 1);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// --- factory_reset ---
|
||||||
|
if(strcmp(cmd_str, "factory_reset") == 0) {
|
||||||
|
char resp[256];
|
||||||
|
snprintf(resp, sizeof(resp),
|
||||||
|
"{\"msg_id\":%lu,\"cmd\":\"factory_reset\","
|
||||||
|
"\"ts\":%lu,\"code\":0,\"msg\":\"success\"}",
|
||||||
|
msg_id, mstick() / 1000);
|
||||||
|
mqtt_publish(resp_topic, resp, 1);
|
||||||
|
PRINT("IOT: factory_reset — resetting...\n");
|
||||||
|
Delay_Ms(200);
|
||||||
|
factory_dev_info();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// --- device_reset (无响应,复位后断开) ---
|
||||||
|
if(strcmp(cmd_str, "device_reset") == 0) {
|
||||||
|
PRINT("IOT: device_reset — rebooting...\n");
|
||||||
|
NVIC_SystemReset();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// --- 未支持的命令 ---
|
||||||
{
|
{
|
||||||
PRINT("MQTT cmd/method: %s, raw: %s\n", temp_guide, msg);
|
char resp[256];
|
||||||
}
|
snprintf(resp, sizeof(resp),
|
||||||
else {
|
"{\"msg_id\":%lu,\"cmd\":\"%s\","
|
||||||
PRINT("Unknown MQTT method: %s\n", msg);
|
"\"ts\":%lu,\"code\":4,\"msg\":\"unsupported command\"}",
|
||||||
|
msg_id, cmd_str, mstick() / 1000);
|
||||||
|
mqtt_publish(resp_topic, resp, 1);
|
||||||
|
PRINT("IOT: unsupported cmd=%s\n", cmd_str);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user