WLG/uart/uart_parameter_config.cpp

628 lines
25 KiB
C++
Raw Normal View History

2024-10-22 19:04:25 +08:00
#include "uart.hpp"
#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
#include <termios.h>
#include <sys/types.h>
#include <sys/stat.h>
2024-10-23 22:25:03 +08:00
#include <boost/algorithm/string.hpp>
2024-10-23 19:51:01 +08:00
#include <zlog.h>
2024-10-23 22:25:03 +08:00
#include "common/common_func.hpp"
2024-11-08 17:19:52 +08:00
#include "minilzo/minilzo.h"
2024-10-22 19:04:25 +08:00
2024-10-23 20:33:05 +08:00
extern zlog_category_t* zct;
extern zlog_category_t* zbt;
2024-11-09 16:15:39 +08:00
#define IN_LEN (128*1024ul)
#define OUT_LEN (IN_LEN + IN_LEN / 16 + 64 + 3)
unsigned char fw_outdata[50000]={0};
unsigned char fw_senddata[50000]={0};
/* Work-memory needed for compression. Allocate memory in units
* of 'lzo_align_t' (instead of 'char') to make sure it is properly aligned.
*/
2024-11-13 17:50:54 +08:00
/*#define HEAP_ALLOC(var,size) \
2024-11-09 16:15:39 +08:00
lzo_align_t __LZO_MMODEL var [ ((size) + (sizeof(lzo_align_t) - 1)) / sizeof(lzo_align_t) ]
2024-11-13 17:50:54 +08:00
static HEAP_ALLOC(wrkmem, LZO1X_1_MEM_COMPRESS);*/
2024-10-23 20:33:05 +08:00
2024-10-22 19:04:25 +08:00
void Uart::openSwitch() {
char buffer[100] = {0x00};
int len;
int fdSwitch = config_uart("/dev/ttymxc1", B38400);
char strSend[8] = {0x01, 0x06, 0x00, 0x00, 0x00, 0x00, 0x89, 0xCA};
len = write_data(fdSwitch, (char*)strSend, 8);
sleep(1);
len = read_data(fdSwitch, (char*)buffer, 100, 10);
2024-10-23 22:25:03 +08:00
for (int i = 0; i < len; i++) zlog_info(zct, "%02X ", buffer[i] & 0xFF);
2024-10-22 19:04:25 +08:00
char strSend2[8] = {0x01, 0x06, 0x00, 0x00, 0x00, 0x01, 0x48, 0x0A};
len = write_data(fdSwitch, (char*)strSend2, 8);
sleep(1);
len = read_data(fdSwitch, (char*)buffer, 100, 10);
2024-10-23 22:25:03 +08:00
for (int i = 0; i < len; i++) zlog_info(zct, "%02X ", buffer[i] & 0xFF);
2024-10-22 19:04:25 +08:00
close(fdSwitch);
}
2024-11-09 16:15:39 +08:00
void Uart::UpdateWirelessNode(uint16_t shortAdd) {
2024-10-29 19:26:36 +08:00
2024-11-10 16:43:15 +08:00
int upgrade_status = 2;
char localtimestamp[32] = {0};
GetTimeNet(localtimestamp, 1);
char insertSql[100] = {0};
char wherecon[50] = {0};
2024-11-16 17:57:38 +08:00
sprintf(wherecon," short_Addr = '%02x%02x' and status = 3 order by start_timestamp DESC",UINT16_HIGH(shortAdd), UINT16_LOW(shortAdd));
2024-11-10 16:43:15 +08:00
std::string spend_count = sqlite_db_ctrl::instance().GetData(" firmware_upgrade ","spend_count",wherecon);
if (atoi(spend_count.c_str()) >= 10){
zlog_warn(zbt, "UpdateWirelessNode spend_count %d,shortAddr = %02x%02x", atoi(spend_count.c_str()), UINT16_HIGH(shortAdd), UINT16_LOW(shortAdd));
return ;
}
if (spend_count == "")spend_count = "0";
memset(wherecon,0,sizeof(wherecon));
2024-11-12 20:04:43 +08:00
sprintf(wherecon," short_Addr = '%02x%02x' order by submit_timestamp DESC",UINT16_HIGH(shortAdd), UINT16_LOW(shortAdd));
2024-11-10 16:43:15 +08:00
std::string firmware_name = sqlite_db_ctrl::instance().GetData(" firmware_upgrade ","firmware_name",wherecon);
memset(wherecon,0,sizeof(wherecon));
sprintf(wherecon," zigbeeShortAddr = '%02x%02x' ",UINT16_HIGH(shortAdd), UINT16_LOW(shortAdd));
vec_t vecResult = sqlite_db_ctrl::instance().GetDataSingleLine(T_SENSOR_INFO(TNAME), " hardVersion,softVersion,ProductNo,upgradeStatus ", wherecon);
2024-11-12 20:04:43 +08:00
if (vecResult[3] == "2"){
2024-11-10 16:43:15 +08:00
zlog_warn(zbt, "UpdateWirelessNode already ,shortAddr = %02x%02x", UINT16_HIGH(shortAdd), UINT16_LOW(shortAdd));
return;
}
2024-10-23 20:33:05 +08:00
std::string strTime = GetLocalTimeWithMs();
2024-11-13 17:50:54 +08:00
zlog_warn(zbt, "UpdateWirelessNode start = %s UpdateWirelessNode id = %02x %02x", strTime.c_str(), UINT16_HIGH(shortAdd), UINT16_LOW(shortAdd));
2024-11-09 16:15:39 +08:00
bUpdate = true;
2024-10-22 19:04:25 +08:00
FILE* pFile = NULL;
2024-11-09 16:15:39 +08:00
long unsigned int thisSize = 0;
long unsigned int out_len = 0;
unsigned char* buffer = NULL;
int compress = 0;
memset(fw_outdata,0,sizeof(fw_outdata));
memset(fw_senddata,0,sizeof(fw_senddata));
2024-11-10 16:43:15 +08:00
DataNodeUpdateFile = "/opt/DataNode/" + firmware_name;
2024-11-09 16:15:39 +08:00
zlog_info(zct, "strFileName = %s", DataNodeUpdateFile.c_str());
2024-10-22 19:04:25 +08:00
pFile = fopen(DataNodeUpdateFile.c_str(), "rb");
2024-10-23 20:33:05 +08:00
if (pFile != NULL) {
2024-10-22 19:04:25 +08:00
while (fgetc(pFile) != EOF) {
++thisSize;
}
rewind(pFile);
2024-11-09 16:15:39 +08:00
buffer = (unsigned char*)malloc(thisSize);
fread(buffer, sizeof(unsigned char), thisSize, pFile);
2024-10-22 19:04:25 +08:00
fclose(pFile);
2024-11-10 16:43:15 +08:00
char sensor_type[6] = {0};
char sf_version[10] = {0};
memcpy(sensor_type, buffer, 5);
printf("model:%s\n", sensor_type);
char c[2] = {0};
c[0] = buffer[5];
uint8_t hw_ver = atoi(c);
c[0] = buffer[6];
uint8_t sf_ver_m = atoi(c);
c[0] = buffer[7];
uint8_t sf_ver_s = atoi(c);
sprintf(sf_version,"%d.%d",sf_ver_m,sf_ver_s);
unsigned char ch_crc = 0x00;
int packgeSize = 0;
ch_crc = buffer[12];
packgeSize = BUILD_UINT32(buffer[8],buffer[9],buffer[10],buffer[11]);
zlog_info(zct,"sf_ver_m = %d\n",sf_ver_m);
zlog_info(zct,"sf_ver_s = %d\n",sf_ver_s);
zlog_info(zct,"hw_ver = %d\n",hw_ver);
zlog_info(zct,"sensor_type = %s\n",sensor_type);
zlog_info(zct,"packgeSize = %d\n",packgeSize);
zlog_info(zct,"ch_crc = %02x\n",ch_crc);
sprintf(insertSql, " '%02x%02x','','%s','','','%d','%d.%d','%s',1,''",UINT16_HIGH(shortAdd), UINT16_LOW(shortAdd),localtimestamp,atoi(spend_count.c_str()) + 1,sf_ver_m,sf_ver_s,vecResult[1].c_str());
sqlite_db_ctrl::instance().InsertData(" firmware_upgrade ", insertSql);
// int r = lzo1x_1_compress(buffer, thisSize, fw_outdata, &out_len, wrkmem);
// if (r == LZO_E_OK){
// printf("compressed %lu bytes into %lu bytes\n",
// (unsigned long) thisSize, (unsigned long) out_len);
// if (out_len < thisSize){
// zlog_warn(zct, "compressed %lu bytes into %lu bytes", (unsigned long)thisSize, (unsigned long)out_len);
// memcpy(fw_senddata,fw_outdata,out_len);
// thisSize = out_len;
// compress = 1;
// }
// }
// else
// {
// /* this should NEVER happen */
// zlog_error(zct,"internal error - compression failed: %d", r);
// }
// /* check for an incompressible block */
// if (out_len >= thisSize)
// {
// zlog_warn(zct,"This block contains incompressible data.out_len = %lu,thisSize = %lu",out_len,thisSize);
// memcpy(fw_senddata,buffer,thisSize);
// }
memcpy(fw_senddata,buffer,thisSize);
2024-11-09 16:15:39 +08:00
unsigned char Data[100] = {0x00};
unsigned char size[4] = {0x00};
zlog_info(zct, "thisSize = %d", (int)thisSize);
//帧头[3byte] 节点地址[2byte] 数据类型[1byte] 序号[1byte] 升级包大小[4byte] CRC校验[1byte]
Data[0] = 0xAA;
Data[1] = 0x55;
Data[2] = 0xAA;
Data[3] = UINT16_HIGH(shortAdd);
Data[4] = UINT16_LOW(shortAdd);
Data[5] = 0x0C;
Data[6] = 0x00;
if (compress == 1){
int2bytes(out_len, size, 4);
}else{
int2bytes(thisSize, size, 4);
}
Data[13] = size[0];
Data[14] = size[1];
Data[15] = size[2];
Data[16] = size[3];
Data[17] = compress & 0xFF;
unsigned char tmp = 0x00;
for (int i = 0; i < 100; i++) {
tmp += Data[i];
}
Data[99] = tmp;
sleep(1);
2024-11-16 20:07:07 +08:00
zlog_info(zct, "thisSize1 = %d", (int)thisSize);
2024-11-09 16:15:39 +08:00
WriteToUart((const char*)Data, 100);
2024-11-16 20:07:07 +08:00
zlog_info(zct, "thisSize2 = %d", (int)thisSize);
2024-11-09 16:15:39 +08:00
int iRet = CheckZigbeeACK();
if (iRet == 0) {
zlog_info(zct, "Packge ACK send success,shortAddr = %02x%02x", UINT16_HIGH(shortAdd), UINT16_LOW(shortAdd));
} else {
2024-11-13 17:50:54 +08:00
zlog_warn(zct, "Packge ACK send failed,shortAddr = %02x%02x", UINT16_HIGH(shortAdd), UINT16_LOW(shortAdd));
2024-11-09 16:15:39 +08:00
}
mssleep(10000);
2024-10-22 19:04:25 +08:00
int Count = thisSize / 92;
int lastSize = thisSize % 92;
unsigned char UpdateData[100] = {0x00};
//帧头[3byte] 节点地址[2byte] 数据类型[1byte] 序号[1byte] 数据包[92byte] CRC校验[1byte]
2024-11-13 17:50:54 +08:00
zlog_warn(zbt, "Start Update!!! file Size = %d,fileName = %s,Count = %d,lastSize = %d,shortAddr = %02x%02x", (int)thisSize,DataNodeUpdateFile.c_str(),Count,lastSize,UINT16_HIGH(shortAdd), UINT16_LOW(shortAdd));
2024-11-09 16:15:39 +08:00
tmp = 0x00;
2024-10-22 19:04:25 +08:00
gpio_set(GlobalConfig::GPIO_G.zigAckreset, 0);
mssleep(1000);
gpio_set(GlobalConfig::GPIO_G.zigAckreset, 1);
2024-11-09 16:15:39 +08:00
for(int j = 0; j < Count;j++){
int time ,value;
UpdateData[0]=0xAA;
UpdateData[1]=0x55;
UpdateData[2]=0xAA;
UpdateData[3]=UINT16_HIGH(shortAdd);
UpdateData[4]=UINT16_LOW(shortAdd);
UpdateData[5]=0x10;
UpdateData[6]=0xff & j;
2024-11-10 16:43:15 +08:00
memcpy(&UpdateData[7],fw_senddata + 92 * j,92);
2024-10-22 19:04:25 +08:00
tmp = 0x00;
2024-11-09 16:15:39 +08:00
for(int k = 0; k < 99;k++){
tmp +=UpdateData[k];
2024-10-22 19:04:25 +08:00
}
UpdateData[99] = tmp;
2024-11-09 16:15:39 +08:00
WriteToUart((const char*)UpdateData,100);
if(gpio_read(GlobalConfig::GPIO_G.zigAckrep) == 48)
gpio_set(GlobalConfig::GPIO_G.zigAckreset,1);
2024-10-22 19:04:25 +08:00
time = 0;
2024-11-09 16:15:39 +08:00
do{
2024-10-22 19:04:25 +08:00
value = gpio_read(GlobalConfig::GPIO_G.zigAckrep);
2024-11-09 16:15:39 +08:00
if(value == 49)
break;
2024-10-22 19:04:25 +08:00
mssleep(10000);
time += 1;
2024-11-09 16:15:39 +08:00
}while(time < 150);
if(time >= 150){
2024-11-13 17:50:54 +08:00
zlog_warn(zct, "gpio_read failed shortAdd %02x %02x,index = %d", UINT16_HIGH(shortAdd), UINT16_LOW(shortAdd),j);
zlog_warn(zct, "gpio_read failed \n");
2024-11-09 16:15:39 +08:00
bUpdate = false;
2024-11-10 16:43:15 +08:00
upgrade_status = 3;
2024-11-09 16:15:39 +08:00
goto endUpdate;
2024-10-22 19:04:25 +08:00
}
2024-11-09 16:15:39 +08:00
gpio_set(GlobalConfig::GPIO_G.zigAckreset,0);
2024-10-22 19:04:25 +08:00
mssleep(2000);
2024-11-09 16:15:39 +08:00
memset(UpdateData,0x00,sizeof(UpdateData));
2024-10-22 19:04:25 +08:00
mssleep(5000);
}
2024-11-09 16:15:39 +08:00
if(gpio_read(GlobalConfig::GPIO_G.zigAckrep) == 48)
gpio_set(GlobalConfig::GPIO_G.zigAckreset,1);
2024-10-22 19:04:25 +08:00
if (lastSize > 0) {
UpdateData[0] = 0xAA;
UpdateData[1] = 0x55;
UpdateData[2] = 0xAA;
UpdateData[3] = UINT16_HIGH(shortAdd);
UpdateData[4] = UINT16_LOW(shortAdd);
2024-11-09 16:15:39 +08:00
UpdateData[5] = 0x10;
2024-10-22 19:04:25 +08:00
UpdateData[6] = 0xff & Count;
2024-11-09 16:15:39 +08:00
memcpy(&UpdateData[7], fw_senddata + 92 * Count, lastSize);
2024-10-22 19:04:25 +08:00
tmp = 0x00;
for (int k = 0; k < 99; k++) {
tmp += UpdateData[k];
}
UpdateData[99] = tmp;
WriteToUart((const char*)UpdateData, 100);
2024-11-09 16:15:39 +08:00
mssleep(1000);
2024-10-22 19:04:25 +08:00
int time = 0;
2024-11-09 16:15:39 +08:00
do{
2024-10-22 19:04:25 +08:00
int value = gpio_read(GlobalConfig::GPIO_G.zigAckrep);
2024-11-09 16:15:39 +08:00
if(value == 49){
gpio_set(GlobalConfig::GPIO_G.zigAckreset,0);
2024-10-22 19:04:25 +08:00
mssleep(10000);
2024-11-09 16:15:39 +08:00
if(gpio_read(GlobalConfig::GPIO_G.zigAckrep) == 48)
gpio_set(GlobalConfig::GPIO_G.zigAckreset,1);
break;
}
2024-10-22 19:04:25 +08:00
mssleep(10000);
time += 1;
2024-11-09 16:15:39 +08:00
}while(time < 150);
2024-10-22 19:04:25 +08:00
if (time >= 150) {
2024-11-13 17:50:54 +08:00
zlog_warn(zct, "gpio_read failed shortAdd %02x %02x", UINT16_HIGH(shortAdd), UINT16_LOW(shortAdd));
zlog_warn(zct, "gpio_read failed \n");
2024-10-22 19:04:25 +08:00
bUpdate = false;
2024-11-10 16:43:15 +08:00
upgrade_status = 3;
2024-10-22 19:04:25 +08:00
goto endUpdate;
}
memset(UpdateData, 0x00, sizeof(UpdateData));
}
2024-11-09 16:15:39 +08:00
zlog_warn(zct, "Update END!!! file Size = %d", (int)thisSize);
2024-10-23 20:33:05 +08:00
} else {
2024-11-13 17:50:54 +08:00
zlog_warn(zct, "open file failed ");
2024-10-22 19:04:25 +08:00
}
endUpdate:
2024-11-10 16:43:15 +08:00
char localtimestamp_end[32] = {0};
GetTimeNet(localtimestamp_end, 1);
char updateSql[1024] = { 0 };
memset(wherecon,0,sizeof(wherecon));
sprintf(wherecon," start_timestamp = '%s' and short_addr = '%02x%02x'",localtimestamp,UINT16_HIGH(shortAdd), UINT16_LOW(shortAdd));
sprintf(updateSql, " end_timestamp = '%s',spend_time = %ld,status = %d",localtimestamp_end,atol(localtimestamp_end)-atol(localtimestamp), upgrade_status);
sqlite_db_ctrl::instance().UpdateTableData(" firmware_upgrade ", updateSql,wherecon);
2024-10-22 19:04:25 +08:00
free(buffer);
tcflush(fd, TCIFLUSH);
2024-10-23 22:25:03 +08:00
sleep(1);
2024-10-22 19:04:25 +08:00
bUpdate = false;
bSendTimeStamp = false;
DataNodeUpdateFile = "";
2024-11-13 17:50:54 +08:00
zlog_warn(zbt, "UpdateWirelessNode end,shortAddr = %02x%02x",UINT16_HIGH(shortAdd), UINT16_LOW(shortAdd));
2024-10-22 19:04:25 +08:00
}
2024-11-09 16:15:39 +08:00
int Uart::UpdateConfig(uint16_t ushortAdd) {
2024-10-22 19:04:25 +08:00
char whereCon[64] = {0};
2024-11-05 11:10:50 +08:00
char selCon[1024] = {0x00};
2024-11-09 16:15:39 +08:00
sprintf(whereCon, "zigbeeShortAddr='%02x%02x'", UINT16_HIGH(ushortAdd),UINT16_LOW(ushortAdd));
2024-10-22 19:04:25 +08:00
2024-10-22 20:56:21 +08:00
vec_t vecResultNode = sqlite_db_ctrl::instance().GetDataSingleLine(T_SENSOR_INFO(TNAME), "*", whereCon);
2024-10-22 19:04:25 +08:00
if (vecResultNode.size() <= 0) return -1;
2024-11-09 16:15:39 +08:00
2024-11-13 17:50:54 +08:00
zlog_warn(zct, "UpdateConfig ushortAdd = %02x%02x",UINT16_HIGH(ushortAdd),UINT16_LOW(ushortAdd));
2024-11-09 16:15:39 +08:00
GlobalConfig::EnterZigBeeWaveTransmittingFlag_G = ENTER_TRANSMITTING_STATUS;
GlobalConfig::EnterZigBeeWaveTransmittingCnt_G = 0;
char tmpbuf[8] = {0x00};
sprintf(tmpbuf, "%02x%02x", UINT16_HIGH(ushortAdd),UINT16_LOW(ushortAdd));
m_strDestShortAddr = std::string(tmpbuf);
mssleep(500);
vec_t vecResult;
sprintf(selCon, "featureInterVal,waveInterVal,range,samplingRate,ACCSampleTime,startBrands,stopBrands,\
envelopeBandPass,faultFrequency,timeStamp,viff,ZigbeePower,ZigbeeRetry,MeasurementID,NodeWaveSend");
vecResult = sqlite_db_ctrl::instance().GetDataSingleLine(T_SENSOR_INFO(TNAME), selCon, whereCon);
zlog_info(zct, "vecResult size = %d", vecResult.size());
if (vecResult.size() < 1) {
return -1;
}
unsigned char UpdateData[100] = {0x00};
//帧头[3byte] 节点地址[2byte] 数据类型[1byte] 序号[1byte] 数据包[92byte] CRC校验[1byte]
UpdateData[0] = 0xAA;
UpdateData[1] = 0x55;
UpdateData[2] = 0xAA;
UpdateData[3] = UINT16_HIGH(ushortAdd);
UpdateData[4] = UINT16_LOW(ushortAdd);
2024-11-10 16:43:15 +08:00
UpdateData[5] = 0x0A;
2024-11-09 16:15:39 +08:00
UpdateData[6] = 0x00;
UpdateData[7] = 0x01;
UpdateData[8] = UINT16_LOW(atoi(vecResult[0].c_str()));
UpdateData[9] = UINT16_HIGH(atoi(vecResult[1].c_str()));
UpdateData[10] = UINT16_LOW(atoi(vecResult[1].c_str()));
int y = 0;
if (vecResultNode[17] == "01") {
if (vecResult[3] == "3200") {
y = 0;
} else if (vecResult[3] == "6400") {
y = 1;
} else if (vecResult[3] == "12800") {
y = 2;
} else if (vecResult[3] == "25600") {
y = 3;
2024-10-22 19:04:25 +08:00
}
2024-11-09 16:15:39 +08:00
} else if (vecResultNode[17] == "02") {
if (vecResult[3] == "8000") {
y = 0;
} else if (vecResult[3] == "16000") {
y = 1;
} else if (vecResult[3] == "24000") {
y = 2;
} else if (vecResult[3] == "32000") {
y = 3;
} else if (vecResult[3] == "48000") {
y = 4;
} else if (vecResult[3] == "96000") {
y = 5;
} else if (vecResult[3] == "192000") {
y = 6;
2024-10-22 19:04:25 +08:00
}
2024-11-09 16:15:39 +08:00
}
UpdateData[18] = atoi(vecResult[11].c_str()) & 0xFF;
UpdateData[19] = atoi(vecResult[12].c_str()) & 0xFF;
int x = atoi(vecResult[2].c_str());
UpdateData[21] = BUILD_UINT2(x, y);
UpdateData[22] = atoi(vecResult[4].c_str()) & 0xFF;
zlog_info(zct, "vecResult size = %s==%s==%s==%s=%s\n", vecResult[5].c_str(), vecResult[6].c_str(), vecResult[7].c_str(), vecResult[8].c_str(), vecResult[14].c_str());
std::vector<std::string> vStart, vStop, vEnvelopeBandPass, vfaultFrequency, vNodeWaveSend;
boost::split(vStart, vecResult[5], boost::is_any_of(","), boost::token_compress_on);
boost::split(vStop, vecResult[6], boost::is_any_of(","), boost::token_compress_on);
boost::split(vEnvelopeBandPass, vecResult[7], boost::is_any_of(","), boost::token_compress_on);
boost::split(vfaultFrequency, vecResult[8], boost::is_any_of(","), boost::token_compress_on);
if (vecResult[14] != "") boost::split(vNodeWaveSend, vecResult[14], boost::is_any_of(","), boost::token_compress_on);
UpdateData[23] = UINT16_HIGH(atoi(vStart[0].c_str()));
UpdateData[24] = UINT16_LOW(atoi(vStart[0].c_str()));
UpdateData[25] = UINT16_HIGH(atoi(vStop[0].c_str()));
UpdateData[26] = UINT16_LOW(atoi(vStop[0].c_str()));
UpdateData[27] = UINT16_HIGH(atoi(vStart[1].c_str()));
UpdateData[28] = UINT16_LOW(atoi(vStart[1].c_str()));
UpdateData[29] = UINT16_HIGH(atoi(vStop[1].c_str()));
UpdateData[30] = UINT16_LOW(atoi(vStop[1].c_str()));
UpdateData[31] = UINT16_HIGH(atoi(vStart[2].c_str()));
UpdateData[32] = UINT16_LOW(atoi(vStart[2].c_str()));
UpdateData[33] = UINT16_HIGH(atoi(vStop[2].c_str()));
UpdateData[34] = UINT16_LOW(atoi(vStop[2].c_str()));
UpdateData[35] = UINT16_HIGH(atoi(vStart[3].c_str()));
UpdateData[36] = UINT16_LOW(atoi(vStart[3].c_str()));
UpdateData[37] = UINT16_HIGH(atoi(vStop[3].c_str()));
UpdateData[38] = UINT16_LOW(atoi(vStop[3].c_str()));
UpdateData[39] = UINT16_HIGH(atoi(vStart[4].c_str()));
UpdateData[40] = UINT16_LOW(atoi(vStart[4].c_str()));
UpdateData[41] = UINT16_HIGH(atoi(vStop[4].c_str()));
UpdateData[42] = UINT16_LOW(atoi(vStop[4].c_str()));
UpdateData[43] = UINT16_HIGH(atoi(vEnvelopeBandPass[0].c_str()));
UpdateData[44] = UINT16_LOW(atoi(vEnvelopeBandPass[0].c_str()));
UpdateData[45] = UINT16_HIGH(atoi(vEnvelopeBandPass[1].c_str()));
UpdateData[46] = UINT16_LOW(atoi(vEnvelopeBandPass[1].c_str()));
UpdateData[47] = UINT16_HIGH(atoi(vfaultFrequency[0].c_str()));
UpdateData[48] = UINT16_LOW(atoi(vfaultFrequency[0].c_str()));
UpdateData[49] = UINT16_HIGH(atoi(vfaultFrequency[1].c_str()));
UpdateData[50] = UINT16_LOW(atoi(vfaultFrequency[1].c_str()));
UpdateData[51] = UINT16_HIGH(atoi(vfaultFrequency[2].c_str()));
UpdateData[52] = UINT16_LOW(atoi(vfaultFrequency[2].c_str()));
UpdateData[53] = UINT16_HIGH(atoi(vfaultFrequency[3].c_str()));
UpdateData[54] = UINT16_LOW(atoi(vfaultFrequency[3].c_str()));
UpdateData[55] = UINT32_HIGH_1(atoi(vecResult[9].c_str()));
UpdateData[56] = UINT32_HIGH_2(atoi(vecResult[9].c_str()));
UpdateData[57] = UINT32_LOW_1(atoi(vecResult[9].c_str()));
UpdateData[58] = UINT32_LOW_2(atoi(vecResult[9].c_str()));
UpdateData[59] = (atoi(vecResult[10].c_str())) & 0xFF;
size_t bytesSize = strlen(vecResult[13].c_str()) / 2;
unsigned char* bytes = (unsigned char*)malloc(bytesSize);
if (hexStringToBytes(vecResult[13].c_str(), bytes, bytesSize) == 0) {
UpdateData[60] = bytes[0];
UpdateData[61] = bytes[1];
UpdateData[62] = bytes[2];
UpdateData[63] = bytes[3];
UpdateData[64] = bytes[4];
UpdateData[65] = bytes[5];
UpdateData[66] = bytes[6];
UpdateData[67] = bytes[7];
}
unsigned char byte = 0;
if (vNodeWaveSend.size() > 0) {
if (vNodeWaveSend[0] == "0") {
Binary_Bit(&byte, 0, 0);
2024-10-22 19:04:25 +08:00
}
2024-11-09 16:15:39 +08:00
if (vNodeWaveSend[0] == "1") {
Binary_Bit(&byte, 0, 1);
}
if (vNodeWaveSend[1] == "0") {
Binary_Bit(&byte, 1, 0);
}
if (vNodeWaveSend[1] == "1") {
Binary_Bit(&byte, 1, 1);
}
if (vNodeWaveSend[2] == "0") {
Binary_Bit(&byte, 2, 0);
}
if (vNodeWaveSend[2] == "1") {
Binary_Bit(&byte, 2, 1);
}
}
UpdateData[68] = byte;
free(bytes);
unsigned char tmp1 = 0x00;
for (int k = 0; k < 99; k++) {
tmp1 += UpdateData[k];
}
UpdateData[99] = tmp1;
tcflush(fd, TCIFLUSH);
WriteToUart((const char*)UpdateData, 100);
int iRet = CheckZigbeeACK();
if (iRet == 0) {
zlog_info(zct, "updataconfig ACK send success,shortAddr = %02x%02x", UINT16_HIGH(ushortAdd),UINT16_LOW(ushortAdd));
} else {
2024-11-13 17:50:54 +08:00
zlog_warn(zct, "updataconfig ACK send failed,shortAddr = %02x%02x", UINT16_HIGH(ushortAdd),UINT16_LOW(ushortAdd));
2024-11-09 16:15:39 +08:00
}
2024-11-10 16:43:15 +08:00
mssleep(50000);
2024-11-09 16:15:39 +08:00
std::string strName = sqlite_db_ctrl::instance().GetData(T_SENSOR_INFO(TNAME), " dataNodeName ", whereCon);
memset(UpdateData,0x00,sizeof(UpdateData));
//帧头[3byte] 节点地址[2byte] 数据类型[1byte] 序号[1byte] 数据包[92byte] CRC校验[1byte]
UpdateData[0] = 0xAA;
UpdateData[1] = 0x55;
UpdateData[2] = 0xAA;
UpdateData[3] = UINT16_HIGH(ushortAdd);
UpdateData[4] = UINT16_LOW(ushortAdd);
2024-11-10 16:43:15 +08:00
UpdateData[5] = 0x0A;
UpdateData[6] = 0x01;
2024-11-16 20:07:07 +08:00
UpdateData[7] = 0x00;
2024-11-10 16:43:15 +08:00
char hex[200] = {0x00};
2024-11-09 16:15:39 +08:00
stringToHex(strName.c_str(), hex);
bytesSize = strlen(hex) / 2;
bytes = (unsigned char*)malloc(bytesSize);
if (hexStringToBytes(hex, bytes, bytesSize) != 0) {
free(bytes);
} else {
for (size_t i = 0; i < bytesSize; i++) {
UpdateData[8 + i] = bytes[i];
2024-10-22 19:04:25 +08:00
}
free(bytes);
unsigned char tmp1 = 0x00;
for (int k = 0; k < 99; k++) {
tmp1 += UpdateData[k];
}
UpdateData[99] = tmp1;
tcflush(fd, TCIFLUSH);
WriteToUart((const char*)UpdateData, 100);
int iRet = CheckZigbeeACK();
if (iRet == 0) {
2024-11-09 16:15:39 +08:00
zlog_info(zct, "updataname ACK send success,shortAddr = %02x%02x", UINT16_HIGH(ushortAdd),UINT16_LOW(ushortAdd));
2024-10-22 19:04:25 +08:00
} else {
2024-11-13 17:50:54 +08:00
zlog_warn(zct, "updataname ACK send failed,shortAddr = %02x%02x", UINT16_HIGH(ushortAdd),UINT16_LOW(ushortAdd));
2024-10-22 19:04:25 +08:00
}
return 0;
}
2024-11-09 16:15:39 +08:00
return 0;
2024-10-22 19:04:25 +08:00
}
2024-10-23 20:33:05 +08:00
2024-10-22 19:04:25 +08:00
int Uart::UpdateWirelessNodeTime(unsigned char* pDestShortAddr, int modifyaddr /*,int nodewaveindex,int nodetime,int nodeindex*/) {
if (modifyaddr) modify_DistAddr(pDestShortAddr);
mssleep(10000);
char localtimestamp[32] = {0x00};
int millisecond = 0;
2024-10-23 20:33:05 +08:00
std::string rtcTime = GetRTC(localtimestamp, millisecond);
zlog_info(zct, "ShortAddr = %02x%02x,rtcTime = %s,localtimestamp = %s,millisecond = %d,bSendTimeStamp = %d ", pDestShortAddr[0], pDestShortAddr[1], rtcTime.c_str(), localtimestamp, millisecond, bSendTimeStamp);
2024-10-22 19:04:25 +08:00
unsigned char UpdateData[100] = {0x00};
UpdateData[0] = 0xAA;
UpdateData[1] = 0x55;
UpdateData[2] = 0xAA;
UpdateData[3] = pDestShortAddr[0];
UpdateData[4] = pDestShortAddr[1];
UpdateData[5] = 0x23;
UpdateData[6] = 0x00;
UpdateData[7] = 0x00;
UpdateData[8] = UINT32_LOW_2(atol(localtimestamp));
UpdateData[9] = UINT32_LOW_1(atol(localtimestamp));
UpdateData[10] = UINT32_HIGH_2(atol(localtimestamp));
UpdateData[11] = UINT32_HIGH_1(atol(localtimestamp));
UpdateData[12] = UINT16_LOW(millisecond);
UpdateData[13] = UINT16_HIGH(millisecond);
unsigned char tmp = 0x00;
for (int k = 0; k < 99; k++) {
tmp += UpdateData[k];
}
UpdateData[99] = tmp;
WriteToUart((const char*)UpdateData, 100);
int iRet = CheckZigbeeACK();
if (iRet == 0) {
2024-10-23 20:33:05 +08:00
zlog_info(zct, "NodeTime ACK send success,shortAddr = %02x%02x", pDestShortAddr[0], pDestShortAddr[1]);
2024-10-22 19:04:25 +08:00
} else {
2024-11-13 17:50:54 +08:00
zlog_warn(zct, "NodeTime ACK send failed,shortAddr = %02x%02x", pDestShortAddr[0], pDestShortAddr[1]);
2024-10-22 19:04:25 +08:00
}
return iRet;
}
2024-10-29 19:26:36 +08:00
int Uart::TaskResp(ScheduleTask scheduleTask){
2024-10-22 19:04:25 +08:00
2024-10-29 19:26:36 +08:00
unsigned char UpdateData[100] = {0};
UpdateData[0] = 0xAA;
UpdateData[1] = 0x55;
UpdateData[2] = 0xAA;
UpdateData[3] = (scheduleTask.shortAddr >> 8) & 0xFF;
UpdateData[4] = scheduleTask.shortAddr & 0xFF;
UpdateData[5] = scheduleTask.cmd & 0xFF;
UpdateData[6] = 0x00;
2024-10-31 19:30:10 +08:00
if (scheduleTask.cmd == REVIVE_DURATION)
{
2024-11-06 21:16:18 +08:00
UpdateData[7] = UINT16_LOW(scheduleTask.duration);
UpdateData[8] = UINT16_HIGH(scheduleTask.duration);
2024-10-31 19:30:10 +08:00
}
if (scheduleTask.cmd == MEAS_EVAL)
{
UpdateData[9] = UINT32_LOW_2(scheduleTask.timeStamp);
UpdateData[10] = UINT32_LOW_1(scheduleTask.timeStamp);
UpdateData[11] = UINT32_HIGH_2(scheduleTask.timeStamp);
UpdateData[12] = UINT32_HIGH_1(scheduleTask.timeStamp);
}
2024-10-29 19:26:36 +08:00
unsigned char tmp = 0x00;
for (int k = 0; k < 99; k++) {
tmp += UpdateData[k];
}
UpdateData[99] = tmp;
2024-11-05 17:22:55 +08:00
mssleep(10000);
2024-10-29 19:26:36 +08:00
WriteToUart((const char*)UpdateData, 100);
int iRet = CheckZigbeeACK();
if (iRet == 0) {
zlog_info(zct, "TaskResp ACK send success,shortAddr = %02x%02x", UINT16_HIGH(scheduleTask.shortAddr),UINT16_LOW(scheduleTask.shortAddr));
2024-10-29 19:26:36 +08:00
} else {
2024-11-13 17:50:54 +08:00
zlog_warn(zct, "TaskResp ACK send failed,shortAddr = %02x%02x", UINT16_HIGH(scheduleTask.shortAddr),UINT16_LOW(scheduleTask.shortAddr));
2024-10-29 19:26:36 +08:00
}
return iRet;
}
int Uart::SendReviveDuration(ReviveDuration recvDuration){
unsigned char UpdateData[100] = {0};
UpdateData[0] = 0xAA;
UpdateData[1] = 0x55;
UpdateData[2] = 0xAA;
UpdateData[3] = (recvDuration.shortAddr >> 8) & 0xFF;
UpdateData[4] = recvDuration.shortAddr & 0xFF;
UpdateData[5] = recvDuration.cmd;
UpdateData[6] = 0x00;
UpdateData[7] = (recvDuration.duration >> 8) & 0xFF;
UpdateData[8] = recvDuration.duration & 0xFF;
unsigned char tmp = 0x00;
for (int k = 0; k < 99; k++) {
tmp += UpdateData[k];
}
UpdateData[99] = tmp;
WriteToUart((const char*)UpdateData, 100);
int iRet = CheckZigbeeACK();
if (iRet == 0) {
zlog_info(zct, "SendReviveDuration ACK send success,shortAddr = %d", recvDuration.shortAddr);
} else {
2024-11-13 17:50:54 +08:00
zlog_warn(zct, "SendReviveDuration ACK send failed,shortAddr = %d", recvDuration.shortAddr);
2024-10-29 19:26:36 +08:00
}
return iRet;
}
2024-10-22 19:04:25 +08:00
int Uart::CheckZigbeeACK() {
if (gpio_read(GlobalConfig::GPIO_G.zigAckrep) == 48) gpio_set(GlobalConfig::GPIO_G.zigAckreset, 1);
int time = 0, value = 0, iRet = -1;
do {
value = gpio_read(GlobalConfig::GPIO_G.zigAckrep);
if (value == 49) {
iRet = 0;
break;
}
mssleep(10000);
} while (time < 150);
return iRet;
}