#include "uart.hpp" #include #include #include #include #include #include #include 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); for (int i = 0; i < len; i++) print_debug("%02X ", buffer[i] & 0xFF); 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); for (int i = 0; i < len; i++) print_debug("%02X ", buffer[i] & 0xFF); close(fdSwitch); } void int2bytes(int i, unsigned char* bytes, int size) { memset(bytes, 0, sizeof(unsigned char) * size); bytes[0] = (unsigned char)(0xff & i); bytes[1] = (unsigned char)((0xff00 & i) >> 8); bytes[2] = (unsigned char)((0xff0000 & i) >> 16); bytes[3] = (unsigned char)((0xff000000 & i) >> 24); } bool Uart::ReadUpdatePackge(unsigned char* pDestShortAddr) { // compare hardversion in update list std::vector strHWversion; std::string strFileName = "", strSoftVersion = ""; std::vector vecDataNodeUpdate; vecDataNodeUpdate = ReadStrUpdate("/opt/DataNode/config.json"); char gethardVersion_sql[32] = {0}; char selectsql[1024] = {0}; sprintf(gethardVersion_sql, "zigbeeShortAddr='%02x%02x'", pDestShortAddr[0], pDestShortAddr[1]); sprintf(selectsql, "%s,%s", T_SENSOR_INFO(HARDVERSION), T_SENSOR_INFO(SOFTVERSION)); vec_t vecResult = sqlite_db_ctrl::instance().GetDataSingleLine(T_SENSOR_INFO(TNAME), selectsql, gethardVersion_sql); if (vecResult.size() < 1) { return false; } zlog_info(zct,"hardversion %s,softversion %s", vecResult[0].c_str(), vecResult[1].c_str()); int thisindex = -1; int flag = -1; for (int j = 0; j < vecDataNodeUpdate.size(); j++) { for (int i = 0; i < vecDataNodeUpdate[j].hwVersion.size(); i++) { if (vecResult[0] == vecDataNodeUpdate[j].hwVersion[i]) { if (vecResult[1] == vecDataNodeUpdate[j].strSoftVersion) { flag = 0; break; } else { thisindex = i; strFileName = vecDataNodeUpdate[j].strUpdataFileName; break; } } } if (thisindex >= 0 || flag == 0) break; } if (thisindex < 0) return false; zlog_info(zct,"thisindex = %d", thisindex); FILE* pFile = NULL; int thisSize = 0; DataNodeUpdateFile = "/opt/DataNode/" + strFileName; zlog_info(zct,"strFileName = %s", DataNodeUpdateFile.c_str()); pFile = fopen(DataNodeUpdateFile.c_str(), "rb"); if (pFile == NULL) { return false; } else { while (fgetc(pFile) != EOF) { ++thisSize; } fclose(pFile); } unsigned char Data[12] = {0x00}; unsigned char size[4] = {0x00}; zlog_info(zct,"thisSize = %d", thisSize); //帧头[3byte] 节点地址[2byte] 数据类型[1byte] 序号[1byte] 升级包大小[4byte] CRC校验[1byte] Data[0] = 0xAA; Data[1] = 0x55; Data[2] = 0xAA; Data[3] = pDestShortAddr[0]; Data[4] = pDestShortAddr[1]; Data[5] = 0x20; Data[6] = 0x00; int2bytes(thisSize, size, 4); Data[7] = size[3]; Data[8] = size[2]; Data[9] = size[1]; Data[10] = size[0]; unsigned char tmp = 0x00; for (int i = 0; i < 11; i++) { tmp += Data[i]; } Data[11] = tmp; sleep(1); WriteToUart((const char*)Data, 12); int iRet = CheckZigbeeACK(); if (iRet == 0) { zlog_info(zct,"Packge ACK send success,shortAddr = %02x%02x", pDestShortAddr[0], pDestShortAddr[1]); } else { zzlog_error(zct,zct,"Packge ACK send failed,shortAddr = %02x%02x", pDestShortAddr[0], pDestShortAddr[1]); } string strTime = GetLocalTimeWithMs(); zlog_info(zct,"ReadUpdatePackge strTime = %s", strTime.c_str()); return true; } void Uart::UpdateWirelessNode(unsigned short shortAdd) { string strTime = GetLocalTimeWithMs(); zlog_info(zct,"UpdateWirelessNode start = %s UpdateWirelessNode id = %02x %02x", strTime.c_str(), UINT16_HIGH(shortAdd), UINT16_LOW(shortAdd)); FILE* pFile = NULL; int thisSize = 0; char* buffer = NULL; int resendCount = 0; pFile = fopen(DataNodeUpdateFile.c_str(), "rb"); if (pFile != NULL) { while (fgetc(pFile) != EOF) { ++thisSize; } rewind(pFile); buffer = (char*)malloc(thisSize); // fread(buffer, sizeof(char), thisSize, pFile); fclose(pFile); int Count = thisSize / 92; int lastSize = thisSize % 92; unsigned char UpdateData[100] = {0x00}; //帧头[3byte] 节点地址[2byte] 数据类型[1byte] 序号[1byte] 数据包[92byte] CRC校验[1byte] zlog_info(zct,"Start Update!!! file Size = %d", thisSize); unsigned char tmp = 0x00; gpio_set(GlobalConfig::GPIO_G.zigAckreset, 0); mssleep(1000); gpio_set(GlobalConfig::GPIO_G.zigAckreset, 1); 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] = 0x21; UpdateData[6] = 0xff & j; memcpy(&UpdateData[7], buffer + 92 * j, 92); tmp = 0x00; for (int k = 0; k < 99; k++) { tmp += UpdateData[k]; } UpdateData[99] = tmp; WriteToUart((const char*)UpdateData, 100); if (gpio_read(GlobalConfig::GPIO_G.zigAckrep) == 48) gpio_set(GlobalConfig::GPIO_G.zigAckreset, 1); time = 0; do { value = gpio_read(GlobalConfig::GPIO_G.zigAckrep); if (value == 49) break; mssleep(10000); time += 1; } while (time < 150); if (time >= 150) { resendCount++; if (resendCount < 5) { zlog_error(zct," RESEND gpio_read failed shortAdd %x %x,error index %d", UINT16_HIGH(shortAdd), UINT16_LOW(shortAdd), j); } else { zlog_error(zct,"gpio_read failed shortAdd %x %x,error index %d", UINT16_HIGH(shortAdd), UINT16_LOW(shortAdd), j); zlog_error(zct,"gpio_read failed \n"); GlobalConfig::EnterZigBeeWaveTransmittingFlag_G = NO_ENTER_TRANSMITTING_STATUS; GlobalConfig::EnterZigBeeWaveTransmittingCnt_G = 0; bUpdate = false; goto endUpdate; } } gpio_set(GlobalConfig::GPIO_G.zigAckreset, 0); mssleep(2000); memset(UpdateData, 0x00, sizeof(UpdateData)); mssleep(5000); } if (gpio_read(GlobalConfig::GPIO_G.zigAckrep) == 48) gpio_set(GlobalConfig::GPIO_G.zigAckreset, 1); if (lastSize > 0) { UpdateData[0] = 0xAA; UpdateData[1] = 0x55; UpdateData[2] = 0xAA; UpdateData[3] = UINT16_HIGH(shortAdd); UpdateData[4] = UINT16_LOW(shortAdd); UpdateData[5] = 0x21; UpdateData[6] = 0xff & Count; memcpy(&UpdateData[7], buffer + 92 * Count, lastSize); tmp = 0x00; for (int k = 0; k < 99; k++) { tmp += UpdateData[k]; } UpdateData[99] = tmp; WriteToUart((const char*)UpdateData, 100); boost::this_thread::sleep(boost::posix_time::milliseconds(10)); int time = 0; do { int value = gpio_read(GlobalConfig::GPIO_G.zigAckrep); if (value == 49) { gpio_set(GlobalConfig::GPIO_G.zigAckreset, 0); mssleep(10000); if (gpio_read(GlobalConfig::GPIO_G.zigAckrep) == 48) gpio_set(GlobalConfig::GPIO_G.zigAckreset, 1); break; } mssleep(10000); time += 1; } while (time < 150); if (time >= 150) { zlog_error(zct,"gpio_read failed shortAdd %x %x", UINT16_HIGH(shortAdd), UINT16_LOW(shortAdd)); zlog_error(zct,"gpio_read failed "); GlobalConfig::EnterZigBeeWaveTransmittingFlag_G = NO_ENTER_TRANSMITTING_STATUS; GlobalConfig::EnterZigBeeWaveTransmittingCnt_G = 0; bUpdate = false; goto endUpdate; } memset(UpdateData, 0x00, sizeof(UpdateData)); } zlog_info(zct,"Update END!!! file Size = %d", thisSize); }else{ zlog_error(zct,"open file failed "); } endUpdate: resendCount = 0; free(buffer); tcflush(fd, TCIFLUSH); boost::this_thread::sleep(boost::posix_time::seconds(1)); bUpdate = false; bSendTimeStamp = false; modify_LocalAddr(0x8888); bModifyAddr = true; boost::this_thread::sleep(boost::posix_time::milliseconds(100)); GlobalConfig::Zigbee_G.MyAddr = 0x8888; DataNodeUpdateFile = ""; GlobalConfig::EnterZigBeeWaveTransmittingFlag_G = NO_ENTER_TRANSMITTING_STATUS; GlobalConfig::EnterZigBeeWaveTransmittingCnt_G = 0; zlog_info(zct,"UpdateWirelessNode end"); } int Uart::UpdateConfig(unsigned char* pDestShortAddr) { char whereCon[64] = {0}; char selCon[100] = {0x00}; sprintf(whereCon, "zigbeeShortAddr='%02x%02x'", pDestShortAddr[0], pDestShortAddr[1]); vec_t vecResultNode = sqlite_db_ctrl::instance().GetDataSingleLine(T_SENSOR_INFO(TNAME), "*", whereCon); if (vecResultNode.size() <= 0) return -1; if (vecResultNode[41] == "0") { zlog_info(zct,"UpdateConfig"); bUpdateconfig = true; unsigned short localAddr = 0x9999; GlobalConfig::EnterZigBeeWaveTransmittingFlag_G = ENTER_TRANSMITTING_STATUS; GlobalConfig::EnterZigBeeWaveTransmittingCnt_G = 0; char tmpbuf[8] = {0x00}; sprintf(tmpbuf, "%02x%02x", pDestShortAddr[0], pDestShortAddr[1]); m_strDestShortAddr = std::string(tmpbuf); GlobalConfig::Zigbee_G.MyAddr = 0x9999; boost::this_thread::sleep(boost::posix_time::milliseconds(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] = pDestShortAddr[0]; UpdateData[4] = pDestShortAddr[1]; UpdateData[5] = 0x22; 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; } } 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; } } 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()); vector 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) { free(bytes); } else { 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); } 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", pDestShortAddr[0], pDestShortAddr[1]); } else { zlog_error(zct,"updataconfig ACK send failed,shortAddr = %02x%02x", pDestShortAddr[0], pDestShortAddr[1]); } return 0; } else if (vecResultNode[41] == "-1") { bUpdateconfig = true; unsigned short localAddr = 0x9999; GlobalConfig::EnterZigBeeWaveTransmittingFlag_G = ENTER_TRANSMITTING_STATUS; GlobalConfig::EnterZigBeeWaveTransmittingCnt_G = 0; string strName = sqlite_db_ctrl::instance().GetData(T_SENSOR_INFO(TNAME), " dataNodeName ", whereCon); unsigned char UpdateData[100] = {0x00}; //帧头[3byte] 节点地址[2byte] 数据类型[1byte] 序号[1byte] 数据包[92byte] CRC校验[1byte] UpdateData[0] = 0xAA; UpdateData[1] = 0x55; UpdateData[2] = 0xAA; UpdateData[3] = pDestShortAddr[0]; UpdateData[4] = pDestShortAddr[1]; UpdateData[5] = 0x22; UpdateData[6] = 0x00; UpdateData[7] = 0x02; char hex[200] = {0X00}; stringToHex(strName.c_str(), hex); size_t bytesSize = strlen(hex) / 2; unsigned char* 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]; } 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,"updataname ACK send success,shortAddr = %02x%02x", pDestShortAddr[0], pDestShortAddr[1]); } else { zlog_error(zct,"updataname ACK send failed,shortAddr = %02x%02x", pDestShortAddr[0], pDestShortAddr[1]); } return 0; } } else { return -1; } } 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; 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); 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) { zlog_info(zct,"NodeTime ACK send success,shortAddr = %02x%02x", pDestShortAddr[0], pDestShortAddr[1]); } else { zlog_error(zct,"NodeTime ACK send failed,shortAddr = %02x%02x", pDestShortAddr[0], pDestShortAddr[1]); } return iRet; } 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; }