From adb6723a74d42bfe5d39771b8685038b9286b5c2 Mon Sep 17 00:00:00 2001 From: zzh <838331105@qq.com> Date: Thu, 4 Dec 2025 13:38:51 +0800 Subject: [PATCH] =?UTF-8?q?=E4=B8=8A=E6=8A=A5=E7=81=AF=E6=9D=A1=E5=BF=83?= =?UTF-8?q?=E8=B7=B3=E7=A6=BB=E7=BA=BF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- CMakeLists.txt | 4 +- cp.sh | 2 +- jd_ota/start.sh | 8 ++-- main.c | 93 ++++++++++++++++++++++++++++++++++++++++- mqtt_utils/mqtt_utils.c | 2 +- start.sh | 8 ++-- 6 files changed, 104 insertions(+), 13 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index c5c96f1..98e1641 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -133,8 +133,8 @@ SET(SRC_TX_SERVER ${CMAKE_CURRENT_LIST_DIR}/main.c ) -ADD_EXECUTABLE(jd_server ${SRC_TX_SERVER}) +ADD_EXECUTABLE(mt_server ${SRC_TX_SERVER}) -TARGET_LINK_LIBRARIES(jd_server pthread dl "-L${CMAKE_CURRENT_LIST_DIR}/openssl/lib" "-lssl" "-lcrypto") +TARGET_LINK_LIBRARIES(mt_server pthread dl "-L${CMAKE_CURRENT_LIST_DIR}/openssl/lib" "-lssl" "-lcrypto") diff --git a/cp.sh b/cp.sh index 2d645eb..c65e08d 100755 --- a/cp.sh +++ b/cp.sh @@ -4,7 +4,7 @@ #sshpass -p "PddloTSecPwdOnly!" scp ./output/bin/tx_server root@192.168.0.148:/home/linaro/ #sshpass -p "TxApPwd#2025!" scp ./output/bin/tx_server root@10.10.12.2:/root #sshpass -p "TxApPwd#2025!" scp ./*.zip root@10.10.12.4:/root/ota -sshpass -p "TxApPwd#2025!" scp output/bin/jd_server root@10.10.12.2:/root +sshpass -p "TxApPwd#2025!" scp output/bin/mt_server root@10.10.12.2:/root #sshpass -p "TxApPwd#2025!" scp tx_ota/*.bin root@10.10.12.4:/root/ota #sshpass -p "PddloTSecPwdOnly!" scp ./output/bin/tx_server root@10.10.12.4:/root/ #sshpass -p "PddloTSecPwdOnly!" scp tx_ota/*.bin root@10.10.12.6:/root/ota diff --git a/jd_ota/start.sh b/jd_ota/start.sh index 5ab8c12..df8f006 100755 --- a/jd_ota/start.sh +++ b/jd_ota/start.sh @@ -1,16 +1,16 @@ #!/bin/sh cd /root -filename=/root/ota/jd_server +filename=/root/ota/mt_server filesize=`ls -l $filename | awk '{ print $5 }'` maxsize=$((440000)) if [ $filesize -gt $maxsize ] then echo "$filesize > $maxsize copy" cp $filename . - chmod 777 jd_server - ./jd_server + chmod 777 mt_server + ./mt_server else echo "$filesize < $maxsize not copy" - ./jd_server + ./mt_server fi diff --git a/main.c b/main.c index 6524253..7ab77d5 100644 --- a/main.c +++ b/main.c @@ -86,6 +86,9 @@ char productid[16]="WcLightStrip"; char appKey[32]="fdhQmhqhvbL1cf1K9mUqt"; char appSecret[64]="RxU8NZjfZaxsKg2B3Dr6sx"; char hostDomain[64]="auk-iot.test1.zservey.com"; +int mqtt_port=1883; +//char hostDomain[64]="127.0.0.1"; +//int mqtt_port=1884; char mqttRawPassword[64]=""; char input_value[1024]={0};//94b4e39caf1d45ee9a071d701cc037b4 char input_value_copy[1024]={0}; @@ -124,13 +127,98 @@ char lightsn28[7]={0}; char lightsn29[7]={0}; char lightsn30[7]={0}; int lightsnNum=0; -int mqtt_port=1883; void hmacsha1_hex(char *key, char* data, char *signhex, int signhex_len); void *thread_mqtt_status_check(void *arg); void *thread_station_heartbeat(void *arg); void *thread_heartbeat_check(void *arg); void update_lightbar_heartbeat(uint32_t tagCode); +void report_lightbar_login(uint32_t tagCode); +void report_lightbar_logout(uint32_t tagCode); + +/*================================================================================*/ +// 上报灯条子设备登录 +void report_lightbar_login(uint32_t tagCode) { + char subDeviceKey[32] = {0}; + char topic[256] = {0}; + char payload[512] = {0}; + char password[64] = {0}; + char stringToSign[256] = {0}; + char timestamp_str[32] = {0}; + struct timeval tv; + + // 构建subDeviceKey: AD10 + tagCode (十六进制) + snprintf(subDeviceKey, sizeof(subDeviceKey), "AD10%08X", tagCode); + + // 获取当前时间戳(毫秒) + gettimeofday(&tv, NULL); + long timestamp = tv.tv_sec * 1000 + tv.tv_usec / 1000; + snprintf(timestamp_str, sizeof(timestamp_str), "%ld", timestamp); + + // 构建待签名串: subDeviceKey + subDeviceKey + productKey + algorithm + timestamp + snprintf(stringToSign, sizeof(stringToSign), "%s%s%s%s%s", + subDeviceKey, subDeviceKey, "WcSubLightStrip", MQTT_ALGORITHM, timestamp_str); + + // 使用HMAC-SHA1计算password + hmacsha1_hex("EKmmjqyLRgghANEiQAA5LZ", stringToSign, password, sizeof(password)); + + // 构建topic + snprintf(topic, sizeof(topic), "/auk/iot/things/v1/sub/device/up/login/WcSubLightStrip/%s/%ld", + subDeviceKey, timestamp); + + // 构建JSON payload + snprintf(payload, sizeof(payload), + "{\"password\":\"%s\",\"timestamp\":%ld,\"signingAlgorithm\":\"%s\"}", + password, timestamp, MQTT_ALGORITHM); + + LOG_I("Lightbar login report - topic: %s\n", topic); + LOG_I("Lightbar login report - payload: %s\n", payload); + + // 发送MQTT消息 + mqtt_utils_publish(&mqtt_config, topic, 0, payload, strlen(payload)); +} + +/*================================================================================*/ +// 上报灯条子设备登出 +void report_lightbar_logout(uint32_t tagCode) { + char subDeviceKey[32] = {0}; + char topic[256] = {0}; + char payload[512] = {0}; + char password[64] = {0}; + char stringToSign[256] = {0}; + char timestamp_str[32] = {0}; + struct timeval tv; + + // 构建subDeviceKey: AD10 + tagCode (十六进制) + snprintf(subDeviceKey, sizeof(subDeviceKey), "AD10%08X", tagCode); + + // 获取当前时间戳(毫秒) + gettimeofday(&tv, NULL); + long timestamp = tv.tv_sec * 1000 + tv.tv_usec / 1000; + snprintf(timestamp_str, sizeof(timestamp_str), "%ld", timestamp); + + // 构建待签名串: subDeviceKey + subDeviceKey + productKey + algorithm + timestamp + snprintf(stringToSign, sizeof(stringToSign), "%s%s%s%s%s", + subDeviceKey, subDeviceKey, "WcSubLightStrip", MQTT_ALGORITHM, timestamp_str); + + // 使用HMAC-SHA1计算password + hmacsha1_hex("EKmmjqyLRgghANEiQAA5LZ", stringToSign, password, sizeof(password)); + + // 构建topic (logout) + snprintf(topic, sizeof(topic), "/auk/iot/things/v1/sub/device/up/logout/WcSubLightStrip/%s/%ld", + subDeviceKey, timestamp); + + // 构建JSON payload + snprintf(payload, sizeof(payload), + "{\"password\":\"%s\",\"timestamp\":%ld,\"signingAlgorithm\":\"%s\"}", + password, timestamp, MQTT_ALGORITHM); + + LOG_I("Lightbar logout report - topic: %s\n", topic); + LOG_I("Lightbar logout report - payload: %s\n", payload); + + // 发送MQTT消息 + mqtt_utils_publish(&mqtt_config, topic, 0, payload, strlen(payload)); +} /*================================================================================*/ // 更新灯条心跳时间 @@ -154,6 +242,7 @@ void update_lightbar_heartbeat(uint32_t tagCode) { if (!lightbarHeartbeat[found].isOnline) { lightbarHeartbeat[found].isOnline = true; LOG_I("Lightbar %08X back online\n", tagCode); + report_lightbar_login(tagCode); } } else if (lightbarHeartbeatCount < MAX_LIGHTBAR_NUM) { // 添加新灯条 @@ -162,6 +251,7 @@ void update_lightbar_heartbeat(uint32_t tagCode) { lightbarHeartbeat[lightbarHeartbeatCount].isOnline = true; lightbarHeartbeatCount++; LOG_I("New lightbar %08X registered, total: %d\n", tagCode, lightbarHeartbeatCount); + report_lightbar_login(tagCode); } pthread_mutex_unlock(&heartbeatMutex); @@ -185,6 +275,7 @@ void *thread_heartbeat_check(void *arg) { lightbarHeartbeat[i].isOnline = false; LOG_I("!!! Lightbar %06X OFFLINE - no heartbeat for %ld seconds (>%d sec)\n", lightbarHeartbeat[i].tagCode, (long)elapsed, HEARTBEAT_TIMEOUT_SEC); + report_lightbar_logout(lightbarHeartbeat[i].tagCode); } } } diff --git a/mqtt_utils/mqtt_utils.c b/mqtt_utils/mqtt_utils.c index 92ae316..d89c53f 100644 --- a/mqtt_utils/mqtt_utils.c +++ b/mqtt_utils/mqtt_utils.c @@ -122,7 +122,7 @@ void mqtt_net_failure(int *failure_times){ (*failure_times)++; LOG_I("mqtt net failure_times = %d\n", *failure_times); if(*failure_times >= 5){ - system("systemctl restart jd_server"); + system("systemctl restart mt_server"); } } } diff --git a/start.sh b/start.sh index 5eca55e..bb53ccc 100755 --- a/start.sh +++ b/start.sh @@ -1,16 +1,16 @@ #!/bin/sh cd /root -filename=/home/linaro/jd_server +filename=/home/linaro/mt_server filesize=`ls -l $filename | awk '{ print $5 }'` maxsize=$((440000)) if [ $filesize -gt $maxsize ] then echo "$filesize > $maxsize copy" cp $filename . - chmod 777 jd_server -./jd_server + chmod 777 mt_server +./mt_server else echo "$filesize < $maxsize not copy" - ./jd_server + ./mt_server fi