Compare commits
5 Commits
876f563492
...
093cf261f8
| Author | SHA1 | Date | |
|---|---|---|---|
| 093cf261f8 | |||
| ad4da8ea92 | |||
| e8670b0775 | |||
| 6baed46a84 | |||
| efd64e3648 |
@ -60,13 +60,13 @@ int debug_print(const char *__restrict format, ...)
|
||||
va_end(valist);
|
||||
|
||||
printf("%s", buffer);
|
||||
/*
|
||||
char opTime[16]={0};
|
||||
char logFileName[32]={0};
|
||||
getDayStr(opTime,16);
|
||||
sprintf(logFileName,"Log.%s",opTime);
|
||||
buffer_to_file(logFileName,buffer,strlen(buffer),"a");
|
||||
*/
|
||||
|
||||
char opTime[16]={0};
|
||||
char logFileName[32]={0};
|
||||
getDayStr(opTime,16);
|
||||
sprintf(logFileName,"Log.%s",opTime);
|
||||
buffer_to_file(logFileName,buffer,strlen(buffer),"a");
|
||||
|
||||
return ret;
|
||||
}
|
||||
#endif
|
||||
|
||||
405
main.c
405
main.c
@ -4,6 +4,7 @@
|
||||
#define DBG_LVL DBG_INFO
|
||||
#include "debug_print.h"
|
||||
#include <sys/prctl.h>
|
||||
#include <sys/statvfs.h>
|
||||
#if 0
|
||||
#include <fcntl.h>
|
||||
#include <sys/mman.h>
|
||||
@ -59,7 +60,7 @@ int fd;
|
||||
int UPCASE=0;
|
||||
int count_value=0;
|
||||
int getPayloadTime=120*1000;//usecond
|
||||
char softwareVersion[16]="1.1.19";
|
||||
char softwareVersion[16]="1.1.25";
|
||||
char stationsn[16]="TJ251372224247";//TJ250995217957
|
||||
char productid[8]="10045";
|
||||
char appKey[32]="fdhQmhqhvbL1cf1K9mUqt";
|
||||
@ -121,9 +122,19 @@ static time_t first_3015_time = 0;
|
||||
static bool is_3015_collecting = false;
|
||||
static pthread_mutex_t lightsn_buffer_mutex = PTHREAD_MUTEX_INITIALIZER;
|
||||
|
||||
// 3022合包缓存定义(放在文件顶部或main函数外部)
|
||||
#define LABEL_BATCH_SIZE 20
|
||||
static uint32_t tags_buf[LABEL_BATCH_SIZE] = {0};
|
||||
static uint8_t lable1_buf[LABEL_BATCH_SIZE] = {0};
|
||||
static uint16_t lable2_buf[LABEL_BATCH_SIZE] = {0};
|
||||
static uint32_t lable3_buf[LABEL_BATCH_SIZE] = {0};
|
||||
static int label_buf_count = 0;
|
||||
static time_t label_buf_first_time = 0;
|
||||
|
||||
// 函数声明
|
||||
void *thread_timeout_check(void *arg);
|
||||
void *thread_3015_lighton_merge(void *arg);
|
||||
void *thread_label_batch_send(void *arg);
|
||||
|
||||
int getLedOtaVersion(char *filename);
|
||||
int getApOtaVersion(char *filename,char *modename);
|
||||
@ -208,11 +219,14 @@ void do_removelog(void)
|
||||
int logcount=0;
|
||||
|
||||
fp=popen("ls Log.*|wc -l", "r");
|
||||
fgets(buffer,sizeof(buffer),fp);
|
||||
logcount=atoi(buffer);
|
||||
if (fp) {
|
||||
fgets(buffer,sizeof(buffer),fp);
|
||||
logcount=atoi(buffer);
|
||||
pclose(fp);
|
||||
}
|
||||
LOG_I("logcount:%d\n",logcount);
|
||||
if(logcount>=3){
|
||||
sprintf(syscmd,"ls Log.*|head -%d|xargs rm -fr",logcount-2);
|
||||
if(logcount>=2){
|
||||
sprintf(syscmd,"ls Log.*|head -%d|xargs rm -fr",logcount-1);
|
||||
LOG_I("%s\n",syscmd);
|
||||
system(syscmd);
|
||||
}else{
|
||||
@ -220,6 +234,65 @@ void do_removelog(void)
|
||||
}
|
||||
}
|
||||
|
||||
// 获取CPU使用率
|
||||
float get_cpu_usage() {
|
||||
FILE *fp;
|
||||
char buf[256];
|
||||
unsigned long long user1, nice1, system1, idle1, iowait1, irq1, softirq1, steal1;
|
||||
unsigned long long user2, nice2, system2, idle2, iowait2, irq2, softirq2, steal2;
|
||||
fp = fopen("/proc/stat", "r");
|
||||
fgets(buf, sizeof(buf), fp);
|
||||
sscanf(buf, "cpu %llu %llu %llu %llu %llu %llu %llu %llu", &user1, &nice1, &system1, &idle1, &iowait1, &irq1, &softirq1, &steal1);
|
||||
fclose(fp);
|
||||
usleep(1000000); // 1秒采样
|
||||
fp = fopen("/proc/stat", "r");
|
||||
fgets(buf, sizeof(buf), fp);
|
||||
sscanf(buf, "cpu %llu %llu %llu %llu %llu %llu %llu %llu", &user2, &nice2, &system2, &idle2, &iowait2, &irq2, &softirq2, &steal2);
|
||||
fclose(fp);
|
||||
unsigned long long total1 = user1+nice1+system1+idle1+iowait1+irq1+softirq1+steal1;
|
||||
unsigned long long total2 = user2+nice2+system2+idle2+iowait2+irq2+softirq2+steal2;
|
||||
unsigned long long idle_diff = idle2 - idle1;
|
||||
unsigned long long total_diff = total2 - total1;
|
||||
if (total_diff == 0) return 0.0;
|
||||
return 100.0 * (1.0 - (float)idle_diff / total_diff);
|
||||
}
|
||||
|
||||
// 获取内存信息
|
||||
void get_mem_usage(long *total, long *free, long *available, long *used, float *percent) {
|
||||
FILE *fp = fopen("/proc/meminfo", "r");
|
||||
char key[64];
|
||||
long value;
|
||||
char unit[16];
|
||||
long mem_total = 0, mem_free = 0, mem_available = 0, buffers = 0, cached = 0;
|
||||
while (fscanf(fp, "%63s %ld %15s\n", key, &value, unit) == 3) {
|
||||
if (strcmp(key, "MemTotal:") == 0) mem_total = value;
|
||||
else if (strcmp(key, "MemFree:") == 0) mem_free = value;
|
||||
else if (strcmp(key, "MemAvailable:") == 0) mem_available = value;
|
||||
else if (strcmp(key, "Buffers:") == 0) buffers = value;
|
||||
else if (strcmp(key, "Cached:") == 0) cached = value;
|
||||
}
|
||||
fclose(fp);
|
||||
*total = mem_total / 1024;
|
||||
*free = mem_free / 1024;
|
||||
*available = mem_available / 1024;
|
||||
*used = (*total) - (*available);
|
||||
*percent = (*total) ? 100.0 * (*used) / (*total) : 0.0;
|
||||
}
|
||||
|
||||
// 获取磁盘信息
|
||||
void get_disk_usage(const char *path, long *total, long *free, long *used, float *percent) {
|
||||
struct statvfs stat;
|
||||
if (statvfs(path, &stat) == 0) {
|
||||
*total = (stat.f_blocks * stat.f_frsize) / 1024 / 1024;
|
||||
*free = (stat.f_bfree * stat.f_frsize) / 1024 / 1024;
|
||||
*used = *total - *free;
|
||||
*percent = (*total) ? 100.0 * (*used) / (*total) : 0.0;
|
||||
} else {
|
||||
*total = *free = *used = 0;
|
||||
*percent = 0.0;
|
||||
}
|
||||
}
|
||||
|
||||
void removeLog(){
|
||||
int len=0;
|
||||
char *savedtime=NULL;
|
||||
@ -237,7 +310,7 @@ void removeLog(){
|
||||
}
|
||||
saved=atol(savedtime);
|
||||
LOG_I("now=%ld,saved=%ld,nowtime=%s,savedtime=%s\n",now,saved,nowtime,savedtime);
|
||||
if(now-saved>=(60*60*24*5)){
|
||||
if(now-saved>=(60*60*24*2)){
|
||||
//if(now-saved>=10){
|
||||
buffer_to_file("logTime",nowtime,strlen(nowtime),"wb");
|
||||
do_removelog();
|
||||
@ -271,6 +344,7 @@ void checkOtaKey(void){
|
||||
LOG_I("OTA enable\n");
|
||||
}
|
||||
}
|
||||
free(readresult);
|
||||
}
|
||||
usleep(100*1000);
|
||||
}
|
||||
@ -324,6 +398,8 @@ void otaLeds(){
|
||||
memset(senddata,0,4096);
|
||||
memcpy(senddata,readresult+i*4096,len%4096);
|
||||
uart_data_send_ledota(&uartSend,senddata,len%4096);
|
||||
|
||||
free(readresult);
|
||||
}
|
||||
#endif
|
||||
}
|
||||
@ -382,6 +458,8 @@ void otaAp(){
|
||||
}if(strcmp(modename,"HTMODE")==0){
|
||||
|
||||
}
|
||||
|
||||
free(readresult);
|
||||
}
|
||||
}
|
||||
|
||||
@ -1137,8 +1215,11 @@ void showShellInfo(char *command){
|
||||
FILE *fp;
|
||||
char buffer[512]={0};
|
||||
fp=popen(command, "r");
|
||||
fread(buffer,1,sizeof(buffer),fp);
|
||||
LOG_I("\n%s",buffer);
|
||||
if (fp) {
|
||||
fread(buffer,1,sizeof(buffer),fp);
|
||||
pclose(fp);
|
||||
LOG_I("\n%s",buffer);
|
||||
}
|
||||
}
|
||||
|
||||
void getTimeStr(char *buffer,int len){
|
||||
@ -1529,29 +1610,17 @@ void *thread_uart_recv_ack(void *arg){
|
||||
isLightOnByRule=false;
|
||||
}
|
||||
if(isLabelUp){
|
||||
#if 1
|
||||
uint32_t tag=strtol(mqtt_parm.msg_sn,NULL,16);
|
||||
uint16_t label2=strtol(mqtt_parm.msg_label2,NULL,16)&0xFFFF;
|
||||
uint32_t label3=strtol(mqtt_parm.msg_label3,NULL,16);
|
||||
uart_data_send_lable(&uartSend,tag,atoi(mqtt_parm.msg_label1),label2,label3,
|
||||
0,0,0,0,
|
||||
0,0,0,0,
|
||||
0,0,0,0,
|
||||
0,0,0,0,
|
||||
1);
|
||||
#else
|
||||
uint32_t tag=strtol("00A04C0F",NULL,16);
|
||||
uint16_t label2=strtol("FF01",NULL,16)&0xFFFF;
|
||||
uint32_t label3=strtol("663855c0",NULL,16);
|
||||
uart_data_send_lable(&uartSend,tag,atoi("03"),label2,label3,
|
||||
0,0,0,0,
|
||||
0,0,0,0,
|
||||
0,0,0,0,
|
||||
0,0,0,0,
|
||||
1);
|
||||
|
||||
#if 0
|
||||
time_t now = time(NULL);
|
||||
if(label_buf_count > 0 && (label_buf_count >= LABEL_BATCH_SIZE || (now - label_buf_first_time) >= 180)) {
|
||||
uart_data_send_head_lableup(&uartSend, 5, label_buf_count);
|
||||
usleep(10000);
|
||||
uart_data_send_lable(&uartSend, tags_buf, lable1_buf, lable2_buf, lable3_buf, label_buf_count);
|
||||
label_buf_count = 0;
|
||||
label_buf_first_time = 0;
|
||||
isLabelUp = false;
|
||||
}
|
||||
#endif
|
||||
isLabelUp=false;
|
||||
}
|
||||
}else if(parm_ack==0x4646){
|
||||
last_2323_time = 0; // 重置时间
|
||||
@ -1914,7 +1983,7 @@ void *thread_mqtt_recv(void *arg){
|
||||
system("mv /userdata/tx_ota /userdata/ota");
|
||||
system("mv /userdata/ota/ustart.sh /userdata");
|
||||
system("sync");
|
||||
sleep(1);
|
||||
sleep(15);
|
||||
system("reboot");
|
||||
}else if(strcmp(mqtt_parm.msg_type,"5003")==0){
|
||||
LOG_I("5003 need report station info\n");
|
||||
@ -1938,21 +2007,21 @@ void *thread_mqtt_recv(void *arg){
|
||||
}else if(strcmp(mqtt_parm.msg_type,"3015")==0){
|
||||
LOG_I("3015 light on (merge mode)\n");
|
||||
get_string_from_json_string_by_key_unescape(msg_data, "scene", mqtt_parm.msg_scene, sizeof(mqtt_parm.msg_scene));
|
||||
LOG_I("scene:%s\n",mqtt_parm.msg_scene);
|
||||
DEBUG_TX("scene:%s\n",mqtt_parm.msg_scene);
|
||||
get_string_from_json_string_by_key_unescape(msg_data, "lights", mqtt_parm.msg_lights, sizeof(mqtt_parm.msg_lights));
|
||||
LOG_I("lights:%s\n",mqtt_parm.msg_lights);
|
||||
DEBUG_TX("lights:%s\n",mqtt_parm.msg_lights);
|
||||
get_size_from_json_string_arry_by_key(msg_data, "lights", &mqtt_parm.msg_opNumber);
|
||||
LOG_I("lights size = %d\n", mqtt_parm.msg_opNumber);
|
||||
DEBUG_TX("lights size = %d\n", mqtt_parm.msg_opNumber);
|
||||
get_string_from_json_string_by_key_unescape(msg_data, "color", mqtt_parm.msg_color, sizeof(mqtt_parm.msg_color));
|
||||
LOG_I("color:%s\n",mqtt_parm.msg_color);
|
||||
DEBUG_TX("color:%s\n",mqtt_parm.msg_color);
|
||||
get_string_from_json_string_by_key_unescape(msg_data, "sound", mqtt_parm.msg_sound, sizeof(mqtt_parm.msg_sound));
|
||||
LOG_I("sound:%s\n",mqtt_parm.msg_sound);
|
||||
DEBUG_TX("sound:%s\n",mqtt_parm.msg_sound);
|
||||
get_string_from_json_string_by_key_unescape(msg_data, "flash", mqtt_parm.msg_flash, sizeof(mqtt_parm.msg_flash));
|
||||
LOG_I("flash:%s\n",mqtt_parm.msg_flash);
|
||||
DEBUG_TX("flash:%s\n",mqtt_parm.msg_flash);
|
||||
get_string_from_json_string_by_key_unescape(msg_data, "flashInterval", mqtt_parm.msg_flashInterval, sizeof(mqtt_parm.msg_flashInterval));
|
||||
LOG_I("flashInterval:%s\n",mqtt_parm.msg_flashInterval);
|
||||
DEBUG_TX("flashInterval:%s\n",mqtt_parm.msg_flashInterval);
|
||||
get_string_from_json_string_by_key_unescape(msg_data, "lightDuration", mqtt_parm.msg_lightDuration, sizeof(mqtt_parm.msg_lightDuration));
|
||||
LOG_I("lightDuration:%s\n",mqtt_parm.msg_lightDuration);
|
||||
DEBUG_TX("lightDuration:%s\n",mqtt_parm.msg_lightDuration);
|
||||
pthread_mutex_lock(&lightsn_buffer_mutex);
|
||||
if (!is_3015_collecting) {
|
||||
is_3015_collecting = true;
|
||||
@ -1984,51 +2053,34 @@ void *thread_mqtt_recv(void *arg){
|
||||
mqtt_service_reply(stationsn, mqtt_parm.msg_messageId, "ok", 1, productid);
|
||||
}else if(strcmp(mqtt_parm.msg_type,"3022")==0){
|
||||
LOG_I("3022 updata lable\n");
|
||||
get_string_from_json_string_by_key_unescape(msg_data, "label1", mqtt_parm.msg_label1, sizeof(mqtt_parm.msg_label1));
|
||||
LOG_I("label1:%s\n",mqtt_parm.msg_label1);
|
||||
get_string_from_json_string_by_key_unescape(msg_data, "label2", mqtt_parm.msg_label2, sizeof(mqtt_parm.msg_label2));
|
||||
LOG_I("label2:%s\n",mqtt_parm.msg_label2);
|
||||
get_string_from_json_string_by_key_unescape(msg_data, "label3", mqtt_parm.msg_label3, sizeof(mqtt_parm.msg_label3));
|
||||
LOG_I("label3:%s\n",mqtt_parm.msg_label3);
|
||||
get_string_from_json_string_by_key_unescape(msg_data, "sn", mqtt_parm.msg_sn, sizeof(mqtt_parm.msg_sn));
|
||||
LOG_I("sn:%s\n",mqtt_parm.msg_sn);
|
||||
if(strcmp(mqtt_parm.msg_sn,"")!=0){
|
||||
mqtt_service_reply(stationsn,mqtt_parm.msg_messageId,"ok",1,productid);
|
||||
isSendComEnd=false;
|
||||
isLightOn=false;
|
||||
isLightOnByRule=false;
|
||||
isLabelUp=true;
|
||||
//isSearchLabel=false;
|
||||
// 分组信息缓存与批量处理
|
||||
typedef struct {
|
||||
char label1[64];
|
||||
char label2[64];
|
||||
char label3[64];
|
||||
char sn[64];
|
||||
} GroupInfo;
|
||||
#define GROUP_MAX 20
|
||||
static GroupInfo group_buffer[GROUP_MAX];
|
||||
static int group_count = 0;
|
||||
static time_t last_write_time = 0;
|
||||
// 保存一次分组信息
|
||||
strncpy(group_buffer[group_count].label1, mqtt_parm.msg_label1, sizeof(group_buffer[group_count].label1));
|
||||
strncpy(group_buffer[group_count].label2, mqtt_parm.msg_label2, sizeof(group_buffer[group_count].label2));
|
||||
strncpy(group_buffer[group_count].label3, mqtt_parm.msg_label3, sizeof(group_buffer[group_count].label3));
|
||||
strncpy(group_buffer[group_count].sn, mqtt_parm.msg_sn, sizeof(group_buffer[group_count].sn));
|
||||
group_count++;
|
||||
if (group_count == 1) {
|
||||
last_write_time = time(NULL);
|
||||
}
|
||||
time_t now = time(NULL);
|
||||
if (group_count >= GROUP_MAX || (now - last_write_time) >= 180) {
|
||||
for (int i = 0; i < group_count; i++) {
|
||||
uart_data_send_head_lableup(&uartSend, 5, 1);
|
||||
}
|
||||
group_count = 0;
|
||||
last_write_time = now;
|
||||
char sn_str[32] = {0};
|
||||
char label1_str[8] = {0};
|
||||
char label2_str[8] = {0};
|
||||
char label3_str[16] = {0};
|
||||
get_string_from_json_string_by_key_unescape(msg_data, "sn", sn_str, sizeof(sn_str));
|
||||
get_string_from_json_string_by_key_unescape(msg_data, "label1", label1_str, sizeof(label1_str));
|
||||
get_string_from_json_string_by_key_unescape(msg_data, "label2", label2_str, sizeof(label2_str));
|
||||
get_string_from_json_string_by_key_unescape(msg_data, "label3", label3_str, sizeof(label3_str));
|
||||
LOG_I("sn:%s\n", sn_str);
|
||||
LOG_I("label1:%s\n", label1_str);
|
||||
LOG_I("label2:%s\n", label2_str);
|
||||
LOG_I("label3:%s\n", label3_str);
|
||||
if(strcmp(sn_str,"")!=0){
|
||||
if(label_buf_count == 0) {
|
||||
label_buf_first_time = time(NULL);
|
||||
}
|
||||
tags_buf[label_buf_count] = strtoul(sn_str, NULL, 16);
|
||||
lable1_buf[label_buf_count] = (uint8_t)atoi(label1_str);
|
||||
lable2_buf[label_buf_count] = (uint16_t)strtoul(label2_str, NULL, 16);
|
||||
lable3_buf[label_buf_count] = strtoul(label3_str, NULL, 16);
|
||||
label_buf_count++;
|
||||
mqtt_service_reply(stationsn, mqtt_parm.msg_messageId, "ok", 1, productid);
|
||||
isSendComEnd = true;
|
||||
isLightOn = false;
|
||||
isLightOnByRule = false;
|
||||
isLabelUp = true;
|
||||
}else{
|
||||
mqtt_service_reply(stationsn,mqtt_parm.msg_messageId,"sn is empty",0,productid);
|
||||
mqtt_service_reply(stationsn, mqtt_parm.msg_messageId, "sn is empty", 0, productid);
|
||||
}
|
||||
}else if(strcmp(mqtt_parm.msg_type,"3023")==0){
|
||||
LOG_I("3023 light on by rule\n");
|
||||
@ -2066,7 +2118,7 @@ void *thread_mqtt_recv(void *arg){
|
||||
if(strcmp(msg_labelconfig_value,"")!=0){
|
||||
mqtt_service_reply(stationsn,mqtt_parm.msg_messageId,"ok",1,productid);
|
||||
isSendComEnd=false;
|
||||
isLightOn=false;
|
||||
isLightOn=false;
|
||||
isLightOnByRule=true;
|
||||
isLabelUp=false;
|
||||
//isSearchLabel=false;
|
||||
@ -2153,7 +2205,71 @@ void *thread_mqtt_recv(void *arg){
|
||||
}else{
|
||||
mqtt_service_reply(stationsn,mqtt_parm.msg_messageId,"light disabled",0,productid);
|
||||
}
|
||||
}
|
||||
} else if(strcmp(mqtt_parm.msg_type,"2333")==0){
|
||||
LOG_I("2333 文件操作任务\n");
|
||||
char action[16] = {0};
|
||||
char filename[128] = {0};
|
||||
char fullpath[256] = {0};
|
||||
int op_result = 0;
|
||||
// 解析action和filename
|
||||
get_string_from_json_string_by_key_unescape(msg_data, "action", action, sizeof(action));
|
||||
get_string_from_json_string_by_key_unescape(msg_data, "filename", filename, sizeof(filename));
|
||||
LOG_I("action: %s, filename: %s\n", action, filename);
|
||||
// 安全性校验:不允许..和/
|
||||
if(strstr(filename, "..")){
|
||||
LOG_I("非法文件名: %s\n", filename);
|
||||
mqtt_service_reply(stationsn, mqtt_parm.msg_messageId, "invalid filename", 0, productid);
|
||||
}else if(strlen(filename) == 0){
|
||||
LOG_I("文件名为空\n");
|
||||
mqtt_service_reply(stationsn, mqtt_parm.msg_messageId, "filename empty", 0, productid);
|
||||
}else{
|
||||
snprintf(fullpath, sizeof(fullpath), "%s", filename);
|
||||
if(strcmp(action, "create")==0){
|
||||
FILE *fp = fopen(fullpath, "w");
|
||||
if(fp){
|
||||
fclose(fp);
|
||||
LOG_I("创建文件成功: %s\n", fullpath);
|
||||
mqtt_service_reply(stationsn, mqtt_parm.msg_messageId, "create ok", 1, productid);
|
||||
}else{
|
||||
LOG_I("创建文件失败: %s, err: %s\n", fullpath, strerror(errno));
|
||||
mqtt_service_reply(stationsn, mqtt_parm.msg_messageId, "create failed", 0, productid);
|
||||
}
|
||||
}else if(strcmp(action, "delete")==0){
|
||||
op_result = unlink(fullpath);
|
||||
if(op_result == 0){
|
||||
LOG_I("删除文件成功: %s\n", fullpath);
|
||||
mqtt_service_reply(stationsn, mqtt_parm.msg_messageId, "delete ok", 1, productid);
|
||||
}else if(errno == ENOENT){
|
||||
LOG_I("文件不存在, 视为成功: %s\n", fullpath);
|
||||
mqtt_service_reply(stationsn, mqtt_parm.msg_messageId, "delete ok (not exist)", 1, productid);
|
||||
}else{
|
||||
LOG_I("删除文件失败: %s, err: %s\n", fullpath, strerror(errno));
|
||||
mqtt_service_reply(stationsn, mqtt_parm.msg_messageId, "delete failed", 0, productid);
|
||||
}
|
||||
}else{
|
||||
LOG_I("未知action: %s\n", action);
|
||||
mqtt_service_reply(stationsn, mqtt_parm.msg_messageId, "unknown action", 0, productid);
|
||||
}
|
||||
}
|
||||
} else if(strcmp(mqtt_parm.msg_type,"2334")==0){
|
||||
LOG_I("2334 获取系统资源任务\n");
|
||||
float cpu_usage = get_cpu_usage();
|
||||
long mem_total, mem_free, mem_available, mem_used;
|
||||
float mem_percent;
|
||||
get_mem_usage(&mem_total, &mem_free, &mem_available, &mem_used, &mem_percent);
|
||||
long disk_total, disk_free, disk_used;
|
||||
float disk_percent;
|
||||
get_disk_usage("/", &disk_total, &disk_free, &disk_used, &disk_percent);
|
||||
|
||||
char json[512];
|
||||
snprintf(json, sizeof(json),
|
||||
"{\"cpu\":\"%.2f%%\",\"mem_total\":\"%ldMB\",\"mem_used\":\"%ldMB\",\"mem_free\":\"%ldMB\",\"mem_percent\":\"%.2f%%\"," \
|
||||
"disk_total\":\"%ldMB\",\"disk_used\":\"%ldMB\",\"disk_free\":\"%ldMB\",\"disk_percent\":\"%.2f%%\"}",
|
||||
cpu_usage, mem_total, mem_used, mem_free, mem_percent,
|
||||
disk_total, disk_used, disk_free, disk_percent);
|
||||
|
||||
mqtt_resource_reply(stationsn, mqtt_parm.msg_messageId, "system", 1, productid, json);
|
||||
}
|
||||
}
|
||||
}
|
||||
usleep(getPayloadTime);
|
||||
@ -2182,9 +2298,12 @@ void getLocalIp(char *local_ip){
|
||||
char buffer[64]={0};
|
||||
//get_ip_by_domain(hostname, local_ip, 32);
|
||||
fp=popen("ifconfig eth0 | grep 'inet' | awk '{print $2}' | cut -d':' -f2","r");
|
||||
fgets(buffer,sizeof(buffer),fp);
|
||||
//LOG_I("buffer:%s\n",buffer);
|
||||
memcpy(local_ip,buffer,strlen(buffer)-1);
|
||||
if (fp) {
|
||||
fgets(buffer,sizeof(buffer),fp);
|
||||
//LOG_I("buffer:%s\n",buffer);
|
||||
memcpy(local_ip,buffer,strlen(buffer)-1);
|
||||
pclose(fp);
|
||||
}
|
||||
}
|
||||
|
||||
int getLedOtaVersion(char *filename){
|
||||
@ -2192,13 +2311,19 @@ int getLedOtaVersion(char *filename){
|
||||
char buffer[128]={0};
|
||||
int ver=0;
|
||||
fp=popen("ls ota/F8*.bin","r");//tx is F8 begin
|
||||
fgets(buffer,sizeof(buffer),fp);
|
||||
//LOG_I("buffer:%s\n",buffer);
|
||||
memcpy(filename,buffer,strlen(buffer)-1);
|
||||
if (fp) {
|
||||
fgets(buffer,sizeof(buffer),fp);
|
||||
//LOG_I("buffer:%s\n",buffer);
|
||||
memcpy(filename,buffer,strlen(buffer)-1);
|
||||
pclose(fp);
|
||||
}
|
||||
fp=popen("ls ota/F8*.bin|cut -d'_' -f3|cut -d'.' -f3","r");
|
||||
fgets(buffer,sizeof(buffer),fp);
|
||||
//LOG_I("buffer:%s\n",buffer);
|
||||
ver=atoi(buffer);
|
||||
if (fp) {
|
||||
fgets(buffer,sizeof(buffer),fp);
|
||||
//LOG_I("buffer:%s\n",buffer);
|
||||
ver=atoi(buffer);
|
||||
pclose(fp);
|
||||
}
|
||||
LOG_I("ver:%d\n",ver);
|
||||
return ver;
|
||||
}
|
||||
@ -2207,19 +2332,32 @@ int getApOtaVersion(char *filename,char *modename){
|
||||
FILE *fp;
|
||||
char buffer[128]={0};
|
||||
int ver=0;
|
||||
fp=popen("ls ota/AP*.bin","r");//AP begin
|
||||
fgets(buffer,sizeof(buffer),fp);
|
||||
//LOG_I("buffer:%s\n",buffer);
|
||||
memcpy(filename,buffer,strlen(buffer)-1);
|
||||
fp=popen("ls ota/AP*.bin|cut -d'_' -f6|cut -d'.' -f3","r");
|
||||
fgets(buffer,sizeof(buffer),fp);
|
||||
//LOG_I("buffer:%s\n",buffer);
|
||||
ver=atoi(buffer);
|
||||
LOG_I("ver:%d\n",ver);
|
||||
fp=popen("ls ota/AP*.bin|cut -d'_' -f3","r");
|
||||
fgets(buffer,sizeof(buffer),fp);
|
||||
//LOG_I("buffer:%s\n",buffer);
|
||||
memcpy(modename,buffer,strlen(buffer)-1);
|
||||
|
||||
fp = popen("ls ota/AP*.bin", "r"); // AP begin
|
||||
if (fp) {
|
||||
fgets(buffer, sizeof(buffer), fp);
|
||||
//LOG_I("buffer:%s\n",buffer);
|
||||
memcpy(filename, buffer, strlen(buffer) - 1);
|
||||
pclose(fp);
|
||||
}
|
||||
|
||||
fp = popen("ls ota/AP*.bin|cut -d'_' -f6|cut -d'.' -f3", "r");
|
||||
if (fp) {
|
||||
fgets(buffer, sizeof(buffer), fp);
|
||||
//LOG_I("buffer:%s\n",buffer);
|
||||
ver = atoi(buffer);
|
||||
LOG_I("ver:%d\n", ver);
|
||||
pclose(fp);
|
||||
}
|
||||
|
||||
fp = popen("ls ota/AP*.bin|cut -d'_' -f3", "r");
|
||||
if (fp) {
|
||||
fgets(buffer, sizeof(buffer), fp);
|
||||
//LOG_I("buffer:%s\n",buffer);
|
||||
memcpy(modename, buffer, strlen(buffer) - 1);
|
||||
pclose(fp);
|
||||
}
|
||||
|
||||
return ver;
|
||||
}
|
||||
|
||||
@ -2429,7 +2567,7 @@ int main(int argc, char *argv[])
|
||||
pthread_detach(pt_removeduplicatetag);
|
||||
}
|
||||
|
||||
#if 0
|
||||
#if 1
|
||||
ret = pthread_create(&pt_removelog,NULL,thread_removelog,NULL);
|
||||
if(ret!=0){
|
||||
LOG_I("pthread_create removelog fail\n");
|
||||
@ -2449,7 +2587,7 @@ int main(int argc, char *argv[])
|
||||
pthread_detach(timeout_thread);
|
||||
}
|
||||
|
||||
// 创建3015合并点亮线程
|
||||
// 新增:3015合并点亮线程
|
||||
pthread_t pt_3015_merge;
|
||||
ret = pthread_create(&pt_3015_merge, NULL, thread_3015_lighton_merge, NULL);
|
||||
if (ret != 0) {
|
||||
@ -2459,6 +2597,16 @@ int main(int argc, char *argv[])
|
||||
LOG_I("pthread_create 3015_merge success\n");
|
||||
}
|
||||
|
||||
// 新增:3022合包批量下发线程
|
||||
pthread_t pt_label_batch_send;
|
||||
ret = pthread_create(&pt_label_batch_send, NULL, thread_label_batch_send, NULL);
|
||||
if (ret != 0) {
|
||||
LOG_I("pthread_create label_batch_send fail\n");
|
||||
} else {
|
||||
pthread_detach(pt_label_batch_send);
|
||||
LOG_I("pthread_create label_batch_send success\n");
|
||||
}
|
||||
|
||||
ret = pthread_create(&pt_ota,NULL,thread_ota,NULL);
|
||||
if(ret!=0){
|
||||
LOG_I("pthread_create ota fail\n");
|
||||
@ -2548,7 +2696,7 @@ int main(int argc, char *argv[])
|
||||
{
|
||||
// 先检查标准输入是否可用
|
||||
if (feof(stdin) || ferror(stdin)) {
|
||||
LOG_I("stdin error or EOF, sleeping for 1 second\n");
|
||||
//LOG_I("stdin error or EOF, sleeping for 1 second\n");
|
||||
sleep(1);
|
||||
continue;
|
||||
}
|
||||
@ -2601,29 +2749,33 @@ void *thread_timeout_check(void *arg) {
|
||||
|
||||
// 合并点亮线程
|
||||
void *thread_3015_lighton_merge(void *arg) {
|
||||
while (1) {
|
||||
int total_all = 0;
|
||||
|
||||
while (1) {
|
||||
pthread_mutex_lock(&lightsn_buffer_mutex);
|
||||
if (is_3015_collecting) {
|
||||
time_t now = time(NULL);
|
||||
if ((now - first_3015_time >= 3) || (lightsn_buffer_count >= MAX_LIGHTSN_BUFFER)) {
|
||||
int total = lightsn_buffer_count;
|
||||
int batch_size = 10;
|
||||
total_all += total;
|
||||
int batch_size = 10;
|
||||
int sent = 0;
|
||||
while (sent < total) {
|
||||
int this_batch = (total - sent > batch_size) ? batch_size : (total - sent);
|
||||
if (isLightEnable && this_batch > 0) {
|
||||
DEBUG_TX("-------total:%d, sent:%d, this batch: %d------\n", total, sent, this_batch);
|
||||
if (isLightEnable && this_batch > 0) {
|
||||
isLightOn = true;
|
||||
isLightOnByRule = false;
|
||||
isLabelUp = false;
|
||||
uint32_t tags[10] = {0};
|
||||
uint8_t tag_leds[10] = {0};
|
||||
for (int i = 0; i < this_batch; i++) {
|
||||
LOG_I("sn[%d]=%s\n", sent + i + 1, lightsn_buffer[sent + i]);
|
||||
DEBUG_TX("sn[%d]=%s\n", sent + i + 1, lightsn_buffer[sent + i]);
|
||||
// 1. SN转MAC(HEX字符串转uint32_t)
|
||||
if (strlen(lightsn_buffer[sent + i]) == 8) {
|
||||
tags[i] = (uint32_t)strtoul(lightsn_buffer[sent + i], NULL, 16);
|
||||
} else {
|
||||
LOG_I("[WARN] tag[%d] sn格式非法: %s, 跳过", sent + i, lightsn_buffer[sent + i]);
|
||||
DEBUG_TX("[WARN] tag[%d] sn格式非法: %s, 跳过", sent + i, lightsn_buffer[sent + i]);
|
||||
continue;
|
||||
}
|
||||
// 2. 颜色+闪光+声音合成LED控制字节(协议格式)
|
||||
@ -2682,14 +2834,16 @@ void *thread_3015_lighton_merge(void *arg) {
|
||||
}
|
||||
sent += this_batch;
|
||||
if (sent < total) {
|
||||
pthread_mutex_unlock(&lightsn_buffer_mutex); // 释放锁等待
|
||||
sleep(3);
|
||||
pthread_mutex_lock(&lightsn_buffer_mutex); // 重新加锁
|
||||
}
|
||||
|
||||
DEBUG_TX("total_all: %d\n", total_all);
|
||||
}
|
||||
|
||||
// 不要立即清空buffer,等待isLightOn处理完成
|
||||
is_3015_collecting = false;
|
||||
first_3015_time = 0;
|
||||
isLightOn = false;
|
||||
}
|
||||
}
|
||||
pthread_mutex_unlock(&lightsn_buffer_mutex);
|
||||
@ -2698,3 +2852,22 @@ void *thread_3015_lighton_merge(void *arg) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
// 3022合包批量下发线程
|
||||
void *thread_label_batch_send(void *arg) {
|
||||
while (1) {
|
||||
if (isLightOnByRule == false && isLightOn == false) {
|
||||
time_t now = time(NULL);
|
||||
if (label_buf_count > 0 && (label_buf_count >= 10 || (now - label_buf_first_time) >= 180)) {
|
||||
uart_data_send_head_lableup(&uartSend, 5, label_buf_count);
|
||||
usleep(10000);
|
||||
uart_data_send_lable(&uartSend, tags_buf, lable1_buf, lable2_buf, lable3_buf, label_buf_count);
|
||||
label_buf_count = 0;
|
||||
label_buf_first_time = 0;
|
||||
isLabelUp = false;
|
||||
}
|
||||
}
|
||||
usleep(1000 * 1000); // 每秒检查一次
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
|
||||
1
main.h
1
main.h
@ -27,6 +27,7 @@ int wdt_val;
|
||||
#define CLOSE_WDT buf = 'V'; write(fdw,&buf,1);close(fdw) //关闭看门狗
|
||||
#define REBOOTTIME 3 //看门狗超时时间默认为14秒,可设为1秒,3秒和14秒
|
||||
#define SET_WDT_TIMEOUT wdt_val = REBOOTTIME; ioctl(fdw,WDIOC_SETTIMEOUT,&wdt_val) //设置看门狗超时时间
|
||||
extern char stationsn[16];
|
||||
|
||||
#include "command.h"
|
||||
#include "queue.h"
|
||||
|
||||
@ -69,7 +69,25 @@ void mqtt_server_light_status_report(char *sn,char *msg_id,char *scene,json_obje
|
||||
struct timeval tv;
|
||||
gettimeofday(&tv, NULL);
|
||||
snprintf(time_buffer,sizeof(time_buffer),"%ld",tv.tv_sec*1000+tv.tv_usec);
|
||||
LOG_I("%s\n",__func__);
|
||||
//LOG_I("%s\n",__func__);
|
||||
|
||||
// 打印去重后的sn
|
||||
if (json_object_is_type(lights, json_type_array)) {
|
||||
int arr_len = json_object_array_length(lights);
|
||||
for (int i = 0; i < arr_len; ++i) {
|
||||
json_object *item = json_object_array_get_idx(lights, i);
|
||||
if (item && json_object_is_type(item, json_type_object)) {
|
||||
json_object *sn_obj = NULL;
|
||||
if (json_object_object_get_ex(item, "sn", &sn_obj)) {
|
||||
const char *sn_str = json_object_get_string(sn_obj);
|
||||
if (sn_str && strlen(sn_str) == 8) {
|
||||
//uint32_t sn = (uint32_t)strtoul(sn_str, NULL, 16);
|
||||
DEBUG_TX("%s\n", sn_str);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
root = json_object_new_object();
|
||||
if(root == NULL){
|
||||
@ -294,6 +312,36 @@ json_error:
|
||||
json_object_put(root);
|
||||
}
|
||||
|
||||
void mqtt_resource_reply(char *sn,char *msg_id,char *msg,int success,char *product_id, char *resource)
|
||||
{
|
||||
char topic[128] = "";
|
||||
const char *payload = NULL;
|
||||
json_object *root = NULL;
|
||||
|
||||
LOG_I("%s\n", __func__);
|
||||
root = json_object_new_object();
|
||||
if(root == NULL){
|
||||
LOG_I("json_object_new_object error\n");
|
||||
goto json_error;
|
||||
}
|
||||
|
||||
json_object_object_add(root, "deviceId", json_object_new_string(sn));
|
||||
json_object_object_add(root, "messageId", json_object_new_string(msg_id));
|
||||
json_object_object_add(root, "msg", json_object_new_string(msg));
|
||||
json_object_object_add(root, "success", json_object_new_boolean(success));
|
||||
json_object_object_add(root, "productId", json_object_new_string(mqtt_conf->productcode));
|
||||
json_object_object_add(root, "resource", json_object_new_string(resource));
|
||||
|
||||
snprintf(topic, sizeof(topic), "iot/%s/%s/resource/report", mqtt_config.productcode, mqtt_config.username);
|
||||
//LOG_I("publish topic:[%s]\n", topic);
|
||||
payload = json_object_to_json_string(root);
|
||||
//LOG_I("payload[%d][%s]\n", strlen(payload), payload);
|
||||
mqtt_utils_publish(mqtt_conf, topic, 2, payload, strlen(payload));
|
||||
|
||||
json_error:
|
||||
json_object_put(root);
|
||||
}
|
||||
|
||||
void mqtt_net_failure(int *failure_times){
|
||||
int fd = open("/sys/class/gpio/gpio113/value", O_WRONLY);
|
||||
if (fd < 0) {
|
||||
@ -306,6 +354,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("sync");
|
||||
system("reboot");
|
||||
}
|
||||
}
|
||||
|
||||
@ -155,6 +155,7 @@ void mqtt_handle_error_event_report(char *cellname,char *batchid,char *optime,in
|
||||
void mqtt_server_station_status_report(char *msg_id,char *product_id,char *sn,char *local_ip, char *base_version,
|
||||
char *status, char *duration);
|
||||
void mqtt_service_reply(char *sn,char *msg_id,char *msg,int success,char *product_id);
|
||||
void mqtt_resource_reply(char *sn,char *msg_id,char *msg,int success,char *product_id, char *resource);
|
||||
void mqtt_server_light_status_report(char *sn,char *msg_id,char *scene,json_object *lights);
|
||||
void mqtt_server_light_status_report_2(char *sn,char *msg_id,char *scene,json_object *lights);
|
||||
void mqtt_server_station_status_report_test(char *msg_id,char *product_id,char *sn,char *local_ip, char *base_version,
|
||||
|
||||
@ -220,49 +220,33 @@ error:
|
||||
return 0;
|
||||
}
|
||||
|
||||
int uart_data_send_head_lableup(uart_utils_t *uart,uint8_t wakeup_time,uint16_t tag_num){
|
||||
int uart_data_send_head_lableup(uart_utils_t *uart, uint8_t wakeup_time, uint16_t tag_num) {
|
||||
int ret = 0;
|
||||
uint8_t data_len=0;
|
||||
if(tag_num==1){
|
||||
data_len=16;
|
||||
}else if(tag_num==2){
|
||||
data_len=27;
|
||||
}else if(tag_num==3){
|
||||
data_len=38;
|
||||
}else if(tag_num==4){
|
||||
data_len=49;
|
||||
}else if(tag_num==5){
|
||||
data_len=60;
|
||||
}
|
||||
jt_head_package_t jt_head_package ={
|
||||
.pre=0x2323,//# 0x23 $ 0x24
|
||||
.wakeupTime=wakeup_time,//5s,<120s
|
||||
.func='B',
|
||||
.len1=0x0000,
|
||||
.len2=data_len,
|
||||
.reserve=ntohs(0x0003),
|
||||
.lableNum=ntohs(tag_num),
|
||||
uint8_t data_len = 16 + (tag_num > 0 ? (tag_num - 1) * 11 : 0); // 兼容原协议
|
||||
jt_head_package_t jt_head_package = {
|
||||
.pre = 0x2323,
|
||||
.wakeupTime = wakeup_time,
|
||||
.func = 'B',
|
||||
.len1 = 0x0000,
|
||||
.len2 = data_len,
|
||||
.reserve = ntohs(0x0003),
|
||||
.lableNum = ntohs(tag_num),
|
||||
};
|
||||
|
||||
LOG_I("%s:HEAD:%04x,%02x,%02x,%04x,%02x,%04x,%04x\r\n",__func__,jt_head_package.pre,jt_head_package.wakeupTime,jt_head_package.func,
|
||||
jt_head_package.len1,jt_head_package.len2,jt_head_package.reserve,jt_head_package.lableNum);
|
||||
|
||||
if (uart == NULL){
|
||||
LOG_I("%s:HEAD:%04x,%02x,%02x,%04x,%02x,%04x,%04x\r\n", __func__, jt_head_package.pre, jt_head_package.wakeupTime, jt_head_package.func,
|
||||
jt_head_package.len1, jt_head_package.len2, jt_head_package.reserve, jt_head_package.lableNum);
|
||||
if (uart == NULL) {
|
||||
LOG_I("uart NULL pointer\n");
|
||||
ret = -1;
|
||||
goto error;
|
||||
}
|
||||
if(sizeof(jt_head_package) == write(uart->uart_fd, &jt_head_package, sizeof(jt_head_package))){
|
||||
if (sizeof(jt_head_package) == write(uart->uart_fd, &jt_head_package, sizeof(jt_head_package))) {
|
||||
ret = 0;
|
||||
//LOG_I("%s success\n",__func__);
|
||||
}else{
|
||||
//LOG_I("%s fail\n", __func__);
|
||||
} else {
|
||||
ret = -2;
|
||||
goto error;
|
||||
}
|
||||
error:
|
||||
return ret;
|
||||
return 0;
|
||||
}
|
||||
|
||||
int uart_data_send_head_lightonrule(uart_utils_t *uart,uint8_t wakeup_time){
|
||||
@ -430,267 +414,48 @@ error:
|
||||
return 0;
|
||||
}
|
||||
|
||||
int uart_data_send_lable(uart_utils_t *uart,uint32_t tag_1,uint8_t tag_1_lable1,uint16_t tag_1_lable2,uint32_t tag_1_lable3,
|
||||
uint32_t tag_2,uint8_t tag_2_lable1,uint16_t tag_2_lable2,uint32_t tag_2_lable3,
|
||||
uint32_t tag_3,uint8_t tag_3_lable1,uint16_t tag_3_lable2,uint32_t tag_3_lable3,
|
||||
uint32_t tag_4,uint8_t tag_4_lable1,uint16_t tag_4_lable2,uint32_t tag_4_lable3,
|
||||
uint32_t tag_5,uint8_t tag_5_lable1,uint16_t tag_5_lable2,uint32_t tag_5_lable3,
|
||||
uint32_t tag_num){
|
||||
int uart_data_send_lable(uart_utils_t *uart, uint32_t tags[], uint8_t lable1[], uint16_t lable2[], uint32_t lable3[], uint32_t tag_num) {
|
||||
int ret = 0;
|
||||
if (uart == NULL){
|
||||
if (uart == NULL) {
|
||||
LOG_I("uart NULL pointer\n");
|
||||
ret = -1;
|
||||
goto error;
|
||||
return -1;
|
||||
}
|
||||
|
||||
uint8_t tag_1_1=(tag_1>>24)&0xFF;
|
||||
uint8_t tag_1_2=(tag_1>>16)&0xFF;
|
||||
uint8_t tag_1_3=(tag_1>>8)&0xFF;
|
||||
uint8_t tag_1_4=(tag_1)&0xFF;
|
||||
uint8_t tag_1_lable2_1=(tag_1_lable2>>8)&0xFF;
|
||||
uint8_t tag_1_lable2_2=(tag_1_lable2)&0xFF;
|
||||
uint8_t tag_1_lable3_1=(tag_1_lable3>>24)&0xFF;
|
||||
uint8_t tag_1_lable3_2=(tag_1_lable3>>16)&0xFF;
|
||||
uint8_t tag_1_lable3_3=(tag_1_lable3>>8)&0xFF;
|
||||
uint8_t tag_1_lable3_4=(tag_1_lable3)&0xFF;
|
||||
|
||||
uint8_t tag_2_1=(tag_2>>24)&0xFF;
|
||||
uint8_t tag_2_2=(tag_2>>16)&0xFF;
|
||||
uint8_t tag_2_3=(tag_2>>8)&0xFF;
|
||||
uint8_t tag_2_4=(tag_2)&0xFF;
|
||||
uint8_t tag_2_lable2_1=(tag_2_lable2>>8)&0xFF;
|
||||
uint8_t tag_2_lable2_2=(tag_2_lable2)&0xFF;
|
||||
uint8_t tag_2_lable3_1=(tag_2_lable3>>24)&0xFF;
|
||||
uint8_t tag_2_lable3_2=(tag_2_lable3>>16)&0xFF;
|
||||
uint8_t tag_2_lable3_3=(tag_2_lable3>>8)&0xFF;
|
||||
uint8_t tag_2_lable3_4=(tag_2_lable3)&0xFF;
|
||||
|
||||
uint8_t tag_3_1=(tag_3>>24)&0xFF;
|
||||
uint8_t tag_3_2=(tag_3>>16)&0xFF;
|
||||
uint8_t tag_3_3=(tag_3>>8)&0xFF;
|
||||
uint8_t tag_3_4=(tag_3)&0xFF;
|
||||
uint8_t tag_3_lable2_1=(tag_3_lable2>>8)&0xFF;
|
||||
uint8_t tag_3_lable2_2=(tag_3_lable2)&0xFF;
|
||||
uint8_t tag_3_lable3_1=(tag_3_lable3>>24)&0xFF;
|
||||
uint8_t tag_3_lable3_2=(tag_3_lable3>>16)&0xFF;
|
||||
uint8_t tag_3_lable3_3=(tag_3_lable3>>8)&0xFF;
|
||||
uint8_t tag_3_lable3_4=(tag_3_lable3)&0xFF;
|
||||
|
||||
uint8_t tag_4_1=(tag_4>>24)&0xFF;
|
||||
uint8_t tag_4_2=(tag_4>>16)&0xFF;
|
||||
uint8_t tag_4_3=(tag_4>>8)&0xFF;
|
||||
uint8_t tag_4_4=(tag_4)&0xFF;
|
||||
uint8_t tag_4_lable2_1=(tag_4_lable2>>8)&0xFF;
|
||||
uint8_t tag_4_lable2_2=(tag_4_lable2)&0xFF;
|
||||
uint8_t tag_4_lable3_1=(tag_4_lable3>>24)&0xFF;
|
||||
uint8_t tag_4_lable3_2=(tag_4_lable3>>16)&0xFF;
|
||||
uint8_t tag_4_lable3_3=(tag_4_lable3>>8)&0xFF;
|
||||
uint8_t tag_4_lable3_4=(tag_4_lable3)&0xFF;
|
||||
|
||||
uint8_t tag_5_1=(tag_5>>24)&0xFF;
|
||||
uint8_t tag_5_2=(tag_5>>16)&0xFF;
|
||||
uint8_t tag_5_3=(tag_5>>8)&0xFF;
|
||||
uint8_t tag_5_4=(tag_5)&0xFF;
|
||||
uint8_t tag_5_lable2_1=(tag_5_lable2>>8)&0xFF;
|
||||
uint8_t tag_5_lable2_2=(tag_5_lable2)&0xFF;
|
||||
uint8_t tag_5_lable3_1=(tag_5_lable3>>24)&0xFF;
|
||||
uint8_t tag_5_lable3_2=(tag_5_lable3>>16)&0xFF;
|
||||
uint8_t tag_5_lable3_3=(tag_5_lable3>>8)&0xFF;
|
||||
uint8_t tag_5_lable3_4=(tag_5_lable3)&0xFF;
|
||||
if(tag_num==1){
|
||||
uint8_t test1[14] = {0x00,0x00,13,tag_1_1,tag_1_2,tag_1_3,tag_1_4,
|
||||
tag_1_lable1,tag_1_lable2_1,tag_1_lable2_2,tag_1_lable3_1,tag_1_lable3_2,tag_1_lable3_3,tag_1_lable3_4};
|
||||
uint16_t crc_c=CRC16_XMODEM(test1,sizeof(test1));
|
||||
for(int i=0;i<14;i++){
|
||||
printf("%02x ",test1[i]);
|
||||
}
|
||||
LOG_I("\r\n");
|
||||
LOG_I("%s XModem_CRC16 = %04x\r\n",__func__,crc_c);
|
||||
jt_lable_led1_package_t jt_lable_led1_package ={
|
||||
.len1=0x0000,
|
||||
.len2=13,
|
||||
.tag1=ntohl(tag_1),
|
||||
.tag1_lable1=tag_1_lable1,
|
||||
.tag1_lable2=ntohs(tag_1_lable2),
|
||||
.tag1_lable3=ntohl(tag_1_lable3),
|
||||
.crc=ntohs(crc_c),
|
||||
};
|
||||
if(sizeof(jt_lable_led1_package) == write(uart->uart_fd, &jt_lable_led1_package, sizeof(jt_lable_led1_package))){
|
||||
ret = 0;
|
||||
//LOG_I("%s success\n",__func__);
|
||||
}else{
|
||||
//LOG_I("%s fail\n", __func__);
|
||||
ret = -2;
|
||||
goto error;
|
||||
}
|
||||
}else if(tag_num==2){
|
||||
uint8_t test1[25] = {0x00,0x00,24,tag_1_1,tag_1_2,tag_1_3,tag_1_4,
|
||||
tag_1_lable1,tag_1_lable2_1,tag_1_lable2_2,tag_1_lable3_1,tag_1_lable3_2,tag_1_lable3_3,tag_1_lable3_4,
|
||||
tag_2_1,tag_2_2,tag_2_3,tag_2_4,
|
||||
tag_2_lable1,tag_2_lable2_1,tag_2_lable2_2,tag_2_lable3_1,tag_2_lable3_2,tag_2_lable3_3,tag_2_lable3_4};
|
||||
uint16_t crc_c=CRC16_XMODEM(test1,sizeof(test1));
|
||||
for(int i=0;i<25;i++){
|
||||
printf("%02x ",test1[i]);
|
||||
}
|
||||
LOG_I("\r\n");
|
||||
LOG_I("%s XModem_CRC16 = %04x\r\n",__func__,crc_c);
|
||||
jt_lable_led2_package_t jt_lable_led2_package ={
|
||||
.len1=0x0000,
|
||||
.len2=24,
|
||||
.tag1=ntohl(tag_1),
|
||||
.tag1_lable1=tag_1_lable1,
|
||||
.tag1_lable2=ntohs(tag_1_lable2),
|
||||
.tag1_lable3=ntohl(tag_1_lable3),
|
||||
.tag2=ntohl(tag_2),
|
||||
.tag2_lable1=tag_2_lable1,
|
||||
.tag2_lable2=ntohs(tag_2_lable2),
|
||||
.tag2_lable3=ntohl(tag_2_lable3),
|
||||
.crc=ntohs(crc_c),
|
||||
};
|
||||
if(sizeof(jt_lable_led2_package) == write(uart->uart_fd, &jt_lable_led2_package, sizeof(jt_lable_led2_package))){
|
||||
ret = 0;
|
||||
//LOG_I("%s success\n",__func__);
|
||||
}else{
|
||||
//LOG_I("%s fail\n", __func__);
|
||||
ret = -2;
|
||||
goto error;
|
||||
}
|
||||
}else if(tag_num==3){
|
||||
uint8_t test1[36] = {0x00,0x00,35,tag_1_1,tag_1_2,tag_1_3,tag_1_4,
|
||||
tag_1_lable1,tag_1_lable2_1,tag_1_lable2_2,tag_1_lable3_1,tag_1_lable3_2,tag_1_lable3_3,tag_1_lable3_4,
|
||||
tag_2_1,tag_2_2,tag_2_3,tag_2_4,
|
||||
tag_2_lable1,tag_2_lable2_1,tag_2_lable2_2,tag_2_lable3_1,tag_2_lable3_2,tag_2_lable3_3,tag_2_lable3_4,
|
||||
tag_3_1,tag_3_2,tag_3_3,tag_3_4,
|
||||
tag_3_lable1,tag_3_lable2_1,tag_3_lable2_2,tag_3_lable3_1,tag_3_lable3_2,tag_3_lable3_3,tag_3_lable3_4};
|
||||
uint16_t crc_c=CRC16_XMODEM(test1,sizeof(test1));
|
||||
for(int i=0;i<36;i++){
|
||||
printf("%02x ",test1[i]);
|
||||
}
|
||||
LOG_I("\r\n");
|
||||
LOG_I("%s XModem_CRC16 = %04x\r\n",__func__,crc_c);
|
||||
jt_lable_led3_package_t jt_lable_led3_package ={
|
||||
.len1=0x0000,
|
||||
.len2=35,
|
||||
.tag1=ntohl(tag_1),
|
||||
.tag1_lable1=tag_1_lable1,
|
||||
.tag1_lable2=ntohs(tag_1_lable2),
|
||||
.tag1_lable3=ntohl(tag_1_lable3),
|
||||
.tag2=ntohl(tag_2),
|
||||
.tag2_lable1=tag_2_lable1,
|
||||
.tag2_lable2=ntohs(tag_2_lable2),
|
||||
.tag2_lable3=ntohl(tag_2_lable3),
|
||||
.tag3=ntohl(tag_3),
|
||||
.tag3_lable1=tag_3_lable1,
|
||||
.tag3_lable2=ntohs(tag_3_lable2),
|
||||
.tag3_lable3=ntohl(tag_3_lable3),
|
||||
.crc=ntohs(crc_c),
|
||||
};
|
||||
if(sizeof(jt_lable_led3_package) == write(uart->uart_fd, &jt_lable_led3_package, sizeof(jt_lable_led3_package))){
|
||||
ret = 0;
|
||||
//LOG_I("%s success\n",__func__);
|
||||
}else{
|
||||
//LOG_I("%s fail\n", __func__);
|
||||
ret = -2;
|
||||
goto error;
|
||||
}
|
||||
}else if(tag_num==4){
|
||||
uint8_t test1[47] = {0x00,0x00,46,tag_1_1,tag_1_2,tag_1_3,tag_1_4,
|
||||
tag_1_lable1,tag_1_lable2_1,tag_1_lable2_2,tag_1_lable3_1,tag_1_lable3_2,tag_1_lable3_3,tag_1_lable3_4,
|
||||
tag_2_1,tag_2_2,tag_2_3,tag_2_4,
|
||||
tag_2_lable1,tag_2_lable2_1,tag_2_lable2_2,tag_2_lable3_1,tag_2_lable3_2,tag_2_lable3_3,tag_2_lable3_4,
|
||||
tag_3_1,tag_3_2,tag_3_3,tag_3_4,
|
||||
tag_3_lable1,tag_3_lable2_1,tag_3_lable2_2,tag_3_lable3_1,tag_3_lable3_2,tag_3_lable3_3,tag_3_lable3_4,
|
||||
tag_4_1,tag_4_2,tag_4_3,tag_4_4,
|
||||
tag_4_lable1,tag_4_lable2_1,tag_4_lable2_2,tag_4_lable3_1,tag_4_lable3_2,tag_4_lable3_3,tag_4_lable3_4};
|
||||
uint16_t crc_c=CRC16_XMODEM(test1,sizeof(test1));
|
||||
for(int i=0;i<47;i++){
|
||||
printf("%02x ",test1[i]);
|
||||
}
|
||||
LOG_I("\r\n");
|
||||
LOG_I("%s XModem_CRC16 = %04x\r\n",__func__,crc_c);
|
||||
jt_lable_led4_package_t jt_lable_led4_package ={
|
||||
.len1=0x0000,
|
||||
.len2=46,
|
||||
.tag1=ntohl(tag_1),
|
||||
.tag1_lable1=tag_1_lable1,
|
||||
.tag1_lable2=ntohs(tag_1_lable2),
|
||||
.tag1_lable3=ntohl(tag_1_lable3),
|
||||
.tag2=ntohl(tag_2),
|
||||
.tag2_lable1=tag_2_lable1,
|
||||
.tag2_lable2=ntohs(tag_2_lable2),
|
||||
.tag2_lable3=ntohl(tag_2_lable3),
|
||||
.tag3=ntohl(tag_3),
|
||||
.tag3_lable1=tag_3_lable1,
|
||||
.tag3_lable2=ntohs(tag_3_lable2),
|
||||
.tag3_lable3=ntohl(tag_3_lable3),
|
||||
.tag4=ntohl(tag_4),
|
||||
.tag4_lable1=tag_4_lable1,
|
||||
.tag4_lable2=ntohs(tag_4_lable2),
|
||||
.tag4_lable3=ntohl(tag_4_lable3),
|
||||
.crc=ntohs(crc_c),
|
||||
};
|
||||
if(sizeof(jt_lable_led4_package) == write(uart->uart_fd, &jt_lable_led4_package, sizeof(jt_lable_led4_package))){
|
||||
ret = 0;
|
||||
//LOG_I("%s success\n",__func__);
|
||||
}else{
|
||||
//LOG_I("%s fail\n", __func__);
|
||||
ret = -2;
|
||||
goto error;
|
||||
}
|
||||
}else if(tag_num==5){
|
||||
uint8_t test1[58] = {0x00,0x00,57,tag_1_1,tag_1_2,tag_1_3,tag_1_4,
|
||||
tag_1_lable1,tag_1_lable2_1,tag_1_lable2_2,tag_1_lable3_1,tag_1_lable3_2,tag_1_lable3_3,tag_1_lable3_4,
|
||||
tag_2_1,tag_2_2,tag_2_3,tag_2_4,
|
||||
tag_2_lable1,tag_2_lable2_1,tag_2_lable2_2,tag_2_lable3_1,tag_2_lable3_2,tag_2_lable3_3,tag_2_lable3_4,
|
||||
tag_3_1,tag_3_2,tag_3_3,tag_3_4,
|
||||
tag_3_lable1,tag_3_lable2_1,tag_3_lable2_2,tag_3_lable3_1,tag_3_lable3_2,tag_3_lable3_3,tag_3_lable3_4,
|
||||
tag_4_1,tag_4_2,tag_4_3,tag_4_4,
|
||||
tag_4_lable1,tag_4_lable2_1,tag_4_lable2_2,tag_4_lable3_1,tag_4_lable3_2,tag_4_lable3_3,tag_4_lable3_4,
|
||||
tag_5_1,tag_5_2,tag_5_3,tag_5_4,
|
||||
tag_5_lable1,tag_5_lable2_1,tag_5_lable2_2,tag_5_lable3_1,tag_5_lable3_2,tag_5_lable3_3,tag_5_lable3_4};
|
||||
uint16_t crc_c=CRC16_XMODEM(test1,sizeof(test1));
|
||||
for(int i=0;i<58;i++){
|
||||
printf("%02x ",test1[i]);
|
||||
}
|
||||
LOG_I("\r\n");
|
||||
LOG_I("%s XModem_CRC16 = %04x\r\n",__func__,crc_c);
|
||||
jt_lable_led5_package_t jt_lable_led5_package ={
|
||||
.len1=0x0000,
|
||||
.len2=57,
|
||||
.tag1=ntohl(tag_1),
|
||||
.tag1_lable1=tag_1_lable1,
|
||||
.tag1_lable2=ntohs(tag_1_lable2),
|
||||
.tag1_lable3=ntohl(tag_1_lable3),
|
||||
.tag2=ntohl(tag_2),
|
||||
.tag2_lable1=tag_2_lable1,
|
||||
.tag2_lable2=ntohs(tag_2_lable2),
|
||||
.tag2_lable3=ntohl(tag_2_lable3),
|
||||
.tag3=ntohl(tag_3),
|
||||
.tag3_lable1=tag_3_lable1,
|
||||
.tag3_lable2=ntohs(tag_3_lable2),
|
||||
.tag3_lable3=ntohl(tag_3_lable3),
|
||||
.tag4=ntohl(tag_4),
|
||||
.tag4_lable1=tag_4_lable1,
|
||||
.tag4_lable2=ntohs(tag_4_lable2),
|
||||
.tag4_lable3=ntohl(tag_4_lable3),
|
||||
.tag5=ntohl(tag_5),
|
||||
.tag5_lable1=tag_5_lable1,
|
||||
.tag5_lable2=ntohs(tag_5_lable2),
|
||||
.tag5_lable3=ntohl(tag_5_lable3),
|
||||
.crc=ntohs(crc_c),
|
||||
};
|
||||
if(sizeof(jt_lable_led5_package) == write(uart->uart_fd, &jt_lable_led5_package, sizeof(jt_lable_led5_package))){
|
||||
ret = 0;
|
||||
//LOG_I("%s success\n",__func__);
|
||||
}else{
|
||||
//LOG_I("%s fail\n", __func__);
|
||||
ret = -2;
|
||||
goto error;
|
||||
}
|
||||
if (tag_num > MAX_LABEL_NUM) tag_num = MAX_LABEL_NUM;
|
||||
uint8_t data_len = 16 + (tag_num > 0 ? (tag_num - 1) * 11 : 0); // 和头部一致
|
||||
uint8_t *buf = (uint8_t *)malloc(data_len);
|
||||
if (!buf) return -2;
|
||||
memset(buf, 0, data_len);
|
||||
buf[0] = 0x00; // len1
|
||||
buf[1] = 0x00; // len2
|
||||
buf[2] = data_len - 3; // 数据区长度(不含len1/len2/len3)
|
||||
for (uint32_t i = 0; i < tag_num; ++i) {
|
||||
int base = 3 + i * 11;
|
||||
buf[base + 0] = (tags[i] >> 24) & 0xFF;
|
||||
buf[base + 1] = (tags[i] >> 16) & 0xFF;
|
||||
buf[base + 2] = (tags[i] >> 8) & 0xFF;
|
||||
buf[base + 3] = tags[i] & 0xFF;
|
||||
buf[base + 4] = lable1[i];
|
||||
buf[base + 5] = (lable2[i] >> 8) & 0xFF;
|
||||
buf[base + 6] = lable2[i] & 0xFF;
|
||||
buf[base + 7] = (lable3[i] >> 24) & 0xFF;
|
||||
buf[base + 8] = (lable3[i] >> 16) & 0xFF;
|
||||
buf[base + 9] = (lable3[i] >> 8) & 0xFF;
|
||||
buf[base + 10] = lable3[i] & 0xFF;
|
||||
}
|
||||
error:
|
||||
uint16_t crc_c = CRC16_XMODEM(buf, data_len - 2);
|
||||
buf[data_len - 2] = (crc_c >> 8) & 0xFF;
|
||||
buf[data_len - 1] = crc_c & 0xFF;
|
||||
// 打印即将发送的数据内容
|
||||
printf("[uart_data_send_lable] send: ");
|
||||
for (int i = 0; i < data_len; ++i) {
|
||||
printf("%02X ", buf[i]);
|
||||
}
|
||||
printf("\n");
|
||||
int write_ret = write(uart->uart_fd, buf, data_len);
|
||||
LOG_I("[uart_data_send_lable] write_ret=%d, data_len=%d", write_ret, data_len);
|
||||
if (write_ret == data_len) ret = 0; else ret = -3;
|
||||
free(buf);
|
||||
return ret;
|
||||
return 0;
|
||||
}
|
||||
|
||||
int uart_data_send_lighton(uart_utils_t *uart,uint8_t ledctl,uint16_t flash_i,uint16_t light_d,
|
||||
@ -733,15 +498,15 @@ int uart_data_send_lighton(uart_utils_t *uart,uint8_t ledctl,uint16_t flash_i,ui
|
||||
buf[7] = light_d_l;
|
||||
|
||||
// 填充tag数据
|
||||
uint32_t tags[30] = {tag_1, tag_2, tag_3, tag_4, tag_5, tag_6, tag_7, tag_8, tag_9, tag_10,
|
||||
uint32_t tags_array[30] = {tag_1, tag_2, tag_3, tag_4, tag_5, tag_6, tag_7, tag_8, tag_9, tag_10,
|
||||
tag_11, tag_12, tag_13, tag_14, tag_15, tag_16, tag_17, tag_18, tag_19, tag_20,
|
||||
tag_21, tag_22, tag_23, tag_24, tag_25, tag_26, tag_27, tag_28, tag_29, tag_30};
|
||||
|
||||
for (int i = 0; i < tag_num; ++i) {
|
||||
buf[8 + i * 4] = (tags[i] >> 24) & 0xFF;
|
||||
buf[9 + i * 4] = (tags[i] >> 16) & 0xFF;
|
||||
buf[10 + i * 4] = (tags[i] >> 8) & 0xFF;
|
||||
buf[11 + i * 4] = tags[i] & 0xFF;
|
||||
buf[8 + i * 4] = (tags_array[i] >> 24) & 0xFF;
|
||||
buf[9 + i * 4] = (tags_array[i] >> 16) & 0xFF;
|
||||
buf[10 + i * 4] = (tags_array[i] >> 8) & 0xFF;
|
||||
buf[11 + i * 4] = tags_array[i] & 0xFF;
|
||||
}
|
||||
|
||||
// 计算CRC - 计算除了CRC本身之外的所有数据
|
||||
@ -771,7 +536,7 @@ int uart_data_send_lighton(uart_utils_t *uart,uint8_t ledctl,uint16_t flash_i,ui
|
||||
|
||||
DEBUG_TX("lightsnNum = %d\n", tag_num);
|
||||
for(int i=0;i<tag_num;i++) {
|
||||
DEBUG_TX("tag%d = %08X\n", i+1, tags[i]);
|
||||
DEBUG_TX("tag%d = %08X\n", i+1, tags_array[i]);
|
||||
}
|
||||
|
||||
error:
|
||||
|
||||
@ -308,12 +308,7 @@ int uart_data_send_lighton(uart_utils_t *uart,uint8_t ledctl,uint16_t flash_i,ui
|
||||
uint32_t tag_21,uint32_t tag_22,uint32_t tag_23,uint32_t tag_24,uint32_t tag_25,
|
||||
uint32_t tag_26,uint32_t tag_27,uint32_t tag_28,uint32_t tag_29,uint32_t tag_30,
|
||||
uint32_t tag_num);
|
||||
int uart_data_send_lable(uart_utils_t *uart,uint32_t tag_1,uint8_t tag_1_lable1,uint16_t tag_1_lable2,uint32_t tag_1_lable3,
|
||||
uint32_t tag_2,uint8_t tag_2_lable1,uint16_t tag_2_lable2,uint32_t tag_2_lable3,
|
||||
uint32_t tag_3,uint8_t tag_3_lable1,uint16_t tag_3_lable2,uint32_t tag_3_lable3,
|
||||
uint32_t tag_4,uint8_t tag_4_lable1,uint16_t tag_4_lable2,uint32_t tag_4_lable3,
|
||||
uint32_t tag_5,uint8_t tag_5_lable1,uint16_t tag_5_lable2,uint32_t tag_5_lable3,
|
||||
uint32_t tag_num);
|
||||
int uart_data_send_lable(uart_utils_t *uart, uint32_t tags[], uint8_t lable1[], uint16_t lable2[], uint32_t lable3[], uint32_t tag_num);
|
||||
int uart_data_send_light_rule(uart_utils_t *uart,uint8_t lable_1,uint16_t lable_2,uint32_t lable_3,
|
||||
uint8_t lable_1_rule, uint8_t lable_2_rule, uint8_t lable_3_rule,
|
||||
uint8_t ledctrl, uint16_t flash_i, uint16_t light_d);
|
||||
@ -332,4 +327,20 @@ int uart_data_receive_version(uart_utils_t *uart);
|
||||
|
||||
int uart_data_send_head_lightband(uart_utils_t *uart, uint8_t wakeup_time, uint16_t tag_num);
|
||||
int uart_data_send_lightband(uart_utils_t *uart, uint8_t ledctl, uint16_t flash_i, uint16_t light_d, uint32_t tags[], uint8_t tag_leds[], uint16_t tag_num);
|
||||
|
||||
#define MAX_LABEL_NUM 30
|
||||
typedef struct {
|
||||
uint16_t len1;
|
||||
uint8_t len2;
|
||||
struct {
|
||||
uint32_t tag;
|
||||
uint8_t lable1;
|
||||
uint16_t lable2;
|
||||
uint32_t lable3;
|
||||
} items[MAX_LABEL_NUM];
|
||||
uint16_t crc;
|
||||
} __attribute__((packed)) jt_lable_ledx_package_t;
|
||||
|
||||
int uart_data_send_head_lableup(uart_utils_t *uart, uint8_t wakeup_time, uint16_t tag_num);
|
||||
int uart_data_send_lable(uart_utils_t *uart, uint32_t tags[], uint8_t lable1[], uint16_t lable2[], uint32_t lable3[], uint32_t tag_num);
|
||||
#endif
|
||||
|
||||
Loading…
Reference in New Issue
Block a user