Compare commits

..

2 Commits

Author SHA1 Message Date
zzh
e9b7bc62a8 恢复上一次提交前更改 2026-01-09 10:41:53 +08:00
zzh
d8ed14d14d 增加子设备密钥配置 2026-01-09 10:41:38 +08:00
3 changed files with 44 additions and 60 deletions

36
main.c
View File

@ -157,7 +157,7 @@ int fd;
int UPCASE=0;
int count_value=0;
int getPayloadTime=100*1000;//usecond
char softwareVersion[16]="2.1.28";
char softwareVersion[16]="2.1.30";
//char stationsn[16]="d126ei4lj4cc00";//TJ250995217957
//char productid[8]="10045";
//char appSecret[64]="s3izIliw0CF48Pcsi16rjOmoFRf5WEt8";
@ -165,6 +165,7 @@ char stationsn[32]="90A9F73002CD";
char productid[16]="WcLightStrip";
char appKey[32]="fdhQmhqhvbL1cf1K9mUqt";
char appSecret[64]="RxU8NZjfZaxsKg2B3Dr6sx";
char subid[64]="EKmmjqyLRgghANEiQAA5LZ"; // 默认值,可从文件覆盖
char hostDomain[64]="auk-iot.test1.zservey.com";
int mqtt_port=1883;
//char hostDomain[64]="127.0.0.1";
@ -275,7 +276,7 @@ void report_lightbar_login(uint16_t tagCodeHead, uint32_t tagCode) {
subDeviceKey, subDeviceKey, "WcSubLightStrip", MQTT_ALGORITHM, timestamp_str);
// 使用HMAC-SHA1计算password
hmacsha1_hex("EKmmjqyLRgghANEiQAA5LZ", stringToSign, password, sizeof(password));
hmacsha1_hex(subid, stringToSign, password, sizeof(password));
// 构建topic
snprintf(topic, sizeof(topic), "/auk/iot/things/v1/sub/device/up/login/WcSubLightStrip/%s/%ld",
@ -286,8 +287,8 @@ void report_lightbar_login(uint16_t tagCodeHead, uint32_t tagCode) {
"{\"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);
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));
@ -636,7 +637,7 @@ void report_lightbar_logout(uint16_t tagCodeHead, uint32_t tagCode) {
subDeviceKey, subDeviceKey, "WcSubLightStrip", MQTT_ALGORITHM, timestamp_str);
// 使用HMAC-SHA1计算password
hmacsha1_hex("EKmmjqyLRgghANEiQAA5LZ", stringToSign, password, sizeof(password));
hmacsha1_hex(subid, stringToSign, password, sizeof(password));
// 构建topic (logout)
snprintf(topic, sizeof(topic), "/auk/iot/things/v1/sub/device/up/logout/WcSubLightStrip/%s/%ld",
@ -2380,10 +2381,6 @@ void *thread_uart_recv_data(void *arg){
while(1){
int ret = uart_data_receive_data_back(&uartRecvData,&parmAck,&tagCodeHead,&tagCode,&tagSignal,&totalLen,&tagFeature,
&count,&batteryV,&version,&ledCtrl,&signCode,&reserve,&lableParm);
if(ret < 0) {
usleep(100000); // 失败时延迟100ms
continue;
}
// 只在有效数据时才打印tag不为0
if(tagCode != 0) {
@ -2418,15 +2415,11 @@ void *thread_uart_recv_back(void *arg){
//LOG_I("Before uart_data_receive_data_back call\n");
int ret = uart_data_receive_data_back(&uartRecvBack,&parmAck,&tagCodeHead,&tagCode,&tagSignal,&totalLen,&tagFeature,
&count,&batteryV,&version,&ledCtrl,&signCode,&reserve,&lableParm);
if(ret < 0) {
//LOG_I("uart_data_receive_data_back failed with ret=%d, sleeping 100ms\n", ret);
usleep(100000); // 失败时延迟100ms
continue;
}
//LOG_I("After uart_data_receive_data_back call, ret: %d\n", parmAck);
// 只在有效数据且不是心跳时才打印
if(tagCode != 0 && tagFeature != 0xFF) {
if(tagCode != 0) {
LOG_I("recv_back:%04x,%04x,tag:%08x,%02x,%02x,%02x,%02x,battery:%02x,%04x,%02x,%04x,reserve:%04x,%08x\n",
parmAck,tagCodeHead,tagCode,tagSignal,totalLen,tagFeature,count,batteryV,version,ledCtrl,signCode,reserve,lableParm);
}
@ -3529,6 +3522,19 @@ int main(int argc, char *argv[])
LOG_I("use default productid:%s\n",productid);
}
// 读取subid配置
readresult=file_to_buffer("/root/subid",&len);
if(readresult!=NULL){
trim_whitespace(readresult); // 先清理空白字符
memset(subid, 0, sizeof(subid));
strncpy(subid, readresult, sizeof(subid)-1);
free(readresult);
readresult = NULL;
LOG_I("saved subid:%s\n", subid);
} else {
LOG_I("subid file not found, using default:%s\n", subid);
}
// 读取appSecret配置
readresult=file_to_buffer("appSecret",&len);
if(readresult!=NULL){

View File

@ -3421,23 +3421,13 @@ int uart_data_receive_version(uart_utils_t *uart){
int uart_data_receive_data_back(uart_utils_t *uart,uint16_t *parmAck,uint16_t *tagCodeHead,uint32_t *tagCode,uint8_t *tagSignal,
uint8_t *totalLen,uint8_t *tagFeature,uint8_t *count,uint8_t *batteryV,uint16_t *version,uint8_t *ledCtrl,
uint16_t *signCode,uint16_t *reserve,uint32_t *lableParm){
//LOG_I("ENTER: uart_data_receive_data_back\n");
int ret = 0;
jt_data_back_package_t jt_data_back_package;
uint8_t *buf = (uint8_t *)&jt_data_back_package;
int pkg_size = sizeof(jt_data_back_package_t);
//LOG_I("DEBUG: uart_data_receive_data_back starting, uart_fd=%d, buffer size: %d bytes\n", uart->uart_fd, pkg_size);
ret = uart_read_until_time(uart->uart_fd,(char *)buf, pkg_size, 1000, 50);
//LOG_I("DEBUG: uart_read_until_time returned %d (expected %d)\n", ret, pkg_size);
if(ret != pkg_size){
if(ret == 0) {
// 超时,不打印错误日志
return -1;
}
//LOG_I("uart_data_receive_data_back failed - ret=%d, expected=%d\n", ret, pkg_size);
return -1;
}

View File

@ -2,7 +2,6 @@
#include <string.h>
#include <unistd.h>
#include <sys/select.h>
#include <errno.h>
#include "read_utils.h"
#define PRINT_TIME_TAG
#define DBG_TAG "read_utils"
@ -21,20 +20,15 @@ int read_data_until_time(int fd, char *buffer, int len, int timeout_first, int t
int i;
memset(buffer, 0, len);
tv.tv_sec = 0;
tv.tv_usec = timeout_first*1000;
for(i=0; i<len; i++){
FD_ZERO(&fds);
FD_SET(fd, &fds);
// 每次都要重新设置超时时间,因为 select 会修改 tv
tv.tv_sec = 0;
tv.tv_usec = (i == 0) ? timeout_first*1000 : timeout_interval*1000;
// 确保超时时间正确设置
if(tv.tv_usec >= 1000000) {
tv.tv_sec = tv.tv_usec / 1000000;
tv.tv_usec = tv.tv_usec % 1000000;
}
ret = select(fd + 1, &fds, NULL, NULL, &tv);
//ret = select(FD_SETSIZE, &fds, NULL, NULL, &tv);
ret = select(FD_SETSIZE, &fds, NULL, NULL, 0);
if(ret < 0){
LOG_I("%s:select error, errno=%d (%s), fd=%d\n",__func__, errno, strerror(errno), fd);
LOG_I("%s:seclect error\n",__func__);
i = -1;
break;
}else if(ret > 0){
@ -51,6 +45,8 @@ int read_data_until_time(int fd, char *buffer, int len, int timeout_first, int t
buffer[i] = c;
}
}
tv.tv_sec = 0;
tv.tv_usec = timeout_interval*1000;
}else{
//LOG_I("read time out\n");
break;
@ -72,19 +68,14 @@ int read_data_until_char(int fd, char *buffer, int len, char until, int timeout_
memset(buffer, 0, len);
for(i=0; i<len; i++){
tv.tv_sec = 0;
tv.tv_usec = timeout_first*1000;
FD_ZERO(&fds);
FD_SET(fd, &fds);
// 每次都要重新设置超时时间,因为 select 会修改 tv
tv.tv_sec = 0;
tv.tv_usec = (i == 0) ? timeout_first*1000 : timeout_interval*1000;
// 确保超时时间正确设置
if(tv.tv_usec >= 1000000) {
tv.tv_sec = tv.tv_usec / 1000000;
tv.tv_usec = tv.tv_usec % 1000000;
}
ret = select(fd + 1, &fds, NULL, NULL, &tv);
//ret = select(FD_SETSIZE, &fds, NULL, NULL, &tv);
ret = select(FD_SETSIZE, &fds, NULL, NULL, 0);
if(ret < 0){
LOG_I("%s:select error, errno=%d (%s), fd=%d\n",__func__, errno, strerror(errno), fd);
LOG_I("%s:seclect error\n",__func__);
i = -1;
break;
}else if(ret > 0){
@ -102,13 +93,15 @@ int read_data_until_char(int fd, char *buffer, int len, char until, int timeout_
LOG_D("i= %d\n", i);
LOG_D("\t,[%#x], <%c>\n", c, c);
#endif
}
}
}else{
tv.tv_sec = 0;
tv.tv_usec = timeout_interval*1000;
if(c == until){
i++;
break;
}
}
}
}else{
//LOG_I("read time out\n");
}
}
@ -128,19 +121,14 @@ int read_data_until_str(int fd, char *buffer, int len, char *until, int until_le
memset(buffer, 0, len);
for(i=0; i<len; i++){
tv.tv_sec = 0;
tv.tv_usec = timeout_first*1000;
FD_ZERO(&fds);
FD_SET(fd, &fds);
// 每次都要重新设置超时时间,因为 select 会修改 tv
tv.tv_sec = 0;
tv.tv_usec = (i == 0) ? timeout_first*1000 : timeout_interval*1000;
// 确保超时时间正确设置
if(tv.tv_usec >= 1000000) {
tv.tv_sec = tv.tv_usec / 1000000;
tv.tv_usec = tv.tv_usec % 1000000;
}
ret = select(fd + 1, &fds, NULL, NULL, &tv);
//ret = select(FD_SETSIZE, &fds, NULL, NULL, &tv);
ret = select(FD_SETSIZE, &fds, NULL, NULL, 0);
if(ret < 0){
LOG_I("%s:select error, errno=%d (%s), fd=%d\n",__func__, errno, strerror(errno), fd);
LOG_I("%s:seclect error\n",__func__);
i = -1;
break;
}else if(ret > 0){