From bc186360fc94b589d83a6a494af98d5275033090 Mon Sep 17 00:00:00 2001 From: pandx Date: Wed, 23 Oct 2024 16:10:23 +0800 Subject: [PATCH] modify log info. --- common/common_func.cpp | 1216 ++++++++++++++----------------- common/common_func.hpp | 40 +- jsonparse/communication_cmd.hpp | 2 +- jsonparse/mqtt_cmd_parse.cpp | 25 - main.cpp | 6 +- uart/uart.cpp | 1 - 6 files changed, 549 insertions(+), 741 deletions(-) diff --git a/common/common_func.cpp b/common/common_func.cpp index e72f3c0..ef3b75b 100644 --- a/common/common_func.cpp +++ b/common/common_func.cpp @@ -38,8 +38,8 @@ static boost::mutex s_config_mu; extern zlog_category_t *zct; extern zlog_category_t *zbt; -string GetLocalTimeWithMs(void) { - string defaultTime = "19700101000000000"; +std::string GetLocalTimeWithMs(void) { + std::string defaultTime = "19700101000000000"; try { struct timeval curTime; gettimeofday(&curTime, NULL); @@ -47,16 +47,14 @@ string GetLocalTimeWithMs(void) { char buffer[80] = {0}; struct tm nowTime; - localtime_r(&curTime.tv_sec, &nowTime); //闂佺娉涢敃銈囨闁秴绀嗛柟娈垮枟閻i亶鏌涙繝鍕付闁宦板姂瀹曟濡烽妶鍜佹Щ闂佸搫鍟冲▔娑㈠垂鎼淬劍鐓€鐎广儱娲﹂悾閬嶆煕閹邦剚鍣归柣掳鍔嶇粙澶愵敇閵娧咁槷缂備焦宕樺▔鏇㈠煝閼测斁鍋撻悷閭︽Ц闁告瑱鎷� + localtime_r(&curTime.tv_sec, &nowTime); //闂備胶顢婂▔娑㈡晝閵堝洦顐介梺顒€绉寸粈鍡涙煙濞堝灝鏋熼柣锝変憾閺屾稒绻濋崟顐粯闂佸鏉垮鐎规洘顨婃俊鐑藉Χ閸滀焦些闂備礁鎼崯鍐测枖濞戙垹鍨傞幖娣妽閻撯偓閻庡箍鍎卞ú锕傛偩闁秵鐓曢柟閭﹀墯閸e綊鏌f幊閸斿秶绮欐径鎰垫晣闁靛ě鍜佹Х缂傚倷鐒﹀畷妯衡枖閺囥垹鐓濋柤娴嬫杹閸嬫捇鎮烽柇锔叫﹂梺鍛婄懕閹凤拷 strftime(buffer, sizeof(buffer), "%Y%m%d%H%M%S", &nowTime); char currentTime[84] = {0}; snprintf(currentTime, sizeof(currentTime), "%s%03d", buffer, milli); - return currentTime; - } catch (const std::exception &e) { - return defaultTime; } catch (...) { + zlog_error(zct, "GetLocalTimeWithMs failed, use default time"); return defaultTime; } } @@ -65,19 +63,20 @@ int code_convert(const char *from_charset, const char *to_charset, char *inbuf, iconv_t cd; char **pin = &inbuf; char **pout = &outbuf; - cd = iconv_open(to_charset, from_charset); - if (cd == 0) return -1; + if (cd == 0) { + zlog_error(zct, "iconv_open failed"); + return -1; + } memset(outbuf, 0, outlen); - if ((int)iconv(cd, pin, &inlen, pout, &outlen) == -1) { + zlog_error(zct, "iconv failed"); iconv_close(cd); return -1; } iconv_close(cd); *pout = '\0'; - return 0; } @@ -87,8 +86,11 @@ int g2u(char *inbuf, size_t inlen, char *outbuf, size_t outlen) { return code_co std::string GBKToUTF8(const std::string &strGBK) { int length = strGBK.size() * 2 + 1; - char *temp = (char *)malloc(sizeof(char) * length); + if (temp == NULL) { + zlog_error(zct, "fail to malloc size:%d", length); + return ""; + } if (g2u((char *)strGBK.c_str(), strGBK.size(), temp, length) >= 0) { std::string str_result; @@ -103,14 +105,16 @@ std::string GBKToUTF8(const std::string &strGBK) { std::string UTFtoGBK(const char *utf8) { int length = strlen(utf8); - char *temp = (char *)malloc(sizeof(char) * length); + if (temp == NULL) { + zlog_error(zct, "fail to malloc size:%d", length); + return ""; + } if (u2g((char *)utf8, length, temp, length) >= 0) { std::string str_result; str_result.append(temp); free(temp); - return str_result; } else { free(temp); @@ -121,7 +125,8 @@ std::string UTFtoGBK(const char *utf8) { std::string convertEncoding(const std::string &input, const char *fromEncoding, const char *toEncoding) { iconv_t conv = iconv_open(toEncoding, fromEncoding); if (conv == (iconv_t)-1) { - throw std::runtime_error("iconv_open failed"); + zlog_error(zct, "iconv_open failed"); + return ""; } size_t inBytesLeft = input.size(); @@ -133,7 +138,8 @@ std::string convertEncoding(const std::string &input, const char *fromEncoding, if (iconv(conv, &inBuf, &inBytesLeft, &outPtr, &outBytesLeft) == (size_t)-1) { delete[] outBuf; iconv_close(conv); - throw std::runtime_error("iconv failed"); + zlog_error(zct, "iconv failed"); + return ""; } std::string result(outBuf, outPtr); @@ -145,19 +151,23 @@ std::string convertEncoding(const std::string &input, const char *fromEncoding, void InitGpio(unsigned int gpioN, unsigned int inout) { int fd = 0; char tmp[100] = {0}; - //闂佺懓鐏氶幐鍝ユ閹辩倍io闁荤姳鐒﹂崕鎶剿囬鍕闁搞儻闄勯锟� + //闂備胶鎳撻悘姘跺箰閸濄儲顫曢柟杈╁€峣o闂佽崵濮抽悞锕傚磿閹跺壙鍥敆閸曨偅顥濋梺鎼炲劵闂勫嫰顢曢敓锟� fd = open("/sys/class/gpio/export", O_WRONLY); if (-1 == fd) { - printf("[%s]:[%d] open gpio export file error\r\n", __FUNCTION__, __LINE__); + zlog_error(zbt, "open gpio export file error"); + return; } - //闂佹眹鍨归悿鍥敋閻炵棷io + //闂備焦鐪归崹褰掓偪閸ヮ剦鏁嬮柣鐐垫7io sprintf(tmp, "%d", gpioN); if (write(fd, tmp, strlen(tmp)) < 0) { - printf("write file operation error %s\r\n", tmp); + zlog_error(zbt, "write file operation error:%s", tmp); + close(fd); + return; } + close(fd); sleep(1); -//闂備焦婢樼粔鍫曟偪閸炲兎io闂佸搫鍊婚幊鎾诲箖閿燂拷 +//闂傚倷鐒﹀妯肩矓閸洘鍋柛鐐插厧io闂備礁鎼崐濠氬箠閹捐绠栭柨鐕傛嫹 #ifdef G2UL_GATEWAY char tmp2[100] = {0}; if (gpioN == 507) @@ -190,34 +200,38 @@ void InitGpio(unsigned int gpioN, unsigned int inout) { memcpy(tmp2, "P11_1", 5); else if (gpioN == 489) memcpy(tmp2, "P16_1", 5); + sprintf(tmp, "/sys/class/gpio/%s/direction", tmp2); #else if IMX6UL_GATEWAY sprintf(tmp, "/sys/class/gpio/gpio%d/direction", gpioN); #endif - print_info("open GPIO = %s\n", tmp); + zlog_info(zbt, "open GPIO = %s", tmp); fd = open(tmp, O_WRONLY); if (-1 == fd) { - printf("[%s]:[%d] open gpio direction file error\r\n", __FUNCTION__, __LINE__); + zlog_error(zbt, "open gpio direction file error"); close(fd); + return; } if (inout == 0) { - print_info("=====InitGpio=====in\n"); + zlog_info(zbt, "=====InitGpio=====in"); if (-1 == write(fd, "in", sizeof("in"))) { - print_info("[%s]:[%d] [%d]write operation direction error\r\n", __FUNCTION__, __LINE__, gpioN); + zlog_error(zbt, "[%d]write operation direction error", gpioN); close(fd); + return; } } else if (inout == 1) { - print_info("=====InitGpio=====out\n"); + zlog_info(zbt, "=====InitGpio=====out"); if (-1 == write(fd, "out", sizeof("out"))) { - print_info("[%s]:[%d] [%d]write operation direction error\r\n", __FUNCTION__, __LINE__, gpioN); + zlog_error(zbt, "[%d]write operation direction error", gpioN); close(fd); + return; } } close(fd); - // printf("gpio%d init %d\r\n",gpioN,inout); } + int gpio_set(unsigned int gpioN, char x) { int fd = 0; char tmp[100] = {0}; @@ -257,33 +271,34 @@ int gpio_set(unsigned int gpioN, char x) { #else if IMX6UL_GATEWAY sprintf(tmp, "/sys/class/gpio/gpio%d/value", gpioN); #endif - // printf("%s\r\n",tmp); - // print_info("set GPIO = %s\n",tmp); - //闂佺懓鐏氶幐鍝ユ閹辩倍io value闂佸搫鍊稿ú锝呪枎閿燂拷 + + zlog_info(zbt, "set GPIO = %s", tmp); + //闂備胶鎳撻悘姘跺箰閸濄儲顫曢柟杈╁€峣o value闂備礁鎼崐绋棵洪敐鍛瀻闁跨噦鎷� fd = open(tmp, O_WRONLY); if (-1 == fd) { - print_red("[%s]:[%d][%s] open gpio export file error\r\n", __FUNCTION__, __LINE__, tmp); + zlog_error(zbt, "[%s] open gpio export file error", tmp); close(fd); - return (-1); // exit(1); + return (-1); } - //闁荤姳绀佹晶浠嬫偪閸℃稒鍋ㄩ梻鍫熺洴閹革拷 + //闂佽崵濮崇粈浣规櫠娴犲鍋柛鈩冪⊕閸嬨劑姊婚崼鐔烘创闁归潻鎷� if (x) { if (-1 == write(fd, "1", sizeof("1"))) { - print_red("[%s]:[%d] %d write operation value error\r\n", __FUNCTION__, __LINE__, gpioN); + zlog_error(zbt, "%d write operation value error", gpioN); close(fd); - return (-1); // exit(1); + return (-1); } } else { if (-1 == write(fd, "0", sizeof("0"))) { - print_red("[%s]:[%d] %d write operation value error\r\n", __FUNCTION__, __LINE__, gpioN); + zlog_error(zbt, "%d write operation value errorn", gpioN); close(fd); - return (-1); // exit(1); + return (-1); } } - // printf("gpio%d set %d ok\r\n",gpioN,x); + zlog_info(zbt, "gpio%d set %d ok", gpioN, x) close(fd); return 0; } + int gpio_read(unsigned int gpioN) { int fd = 0; char value; @@ -326,52 +341,27 @@ int gpio_read(unsigned int gpioN) { #else if IMX6UL_GATEWAY sprintf(tmp, "/sys/class/gpio/gpio%d/value", gpioN); #endif - //闂佺懓鐏氶幐鍝ユ閹辩倍io value闂佸搫鍊稿ú锝呪枎閿燂拷 + //闂備胶鎳撻悘姘跺箰閸濄儲顫曢柟杈╁€峣o value闂備礁鎼崐绋棵洪敐鍛瀻闁跨噦鎷� fd = open(tmp, O_RDONLY); if (-1 == fd) { - print_red("[%s]:[%d] %d open gpio export file error\r\n", __FUNCTION__, __LINE__, gpioN); - return (-1); // exit(1); + zlog_error(zbt, "%d open gpio export file error", gpioN); + return (-1); } - //闁荤姴娲╅褑銇愰敓锟� value闂佸搫鍊稿ú锝呪枎閿燂拷 + //闂佽崵濮村ú鈺咁敋瑜戦妵鎰版晸閿燂拷 value闂備礁鎼崐绋棵洪敐鍛瀻闁跨噦鎷� if (-1 == read(fd, &value, sizeof(value))) { - print_red("[%s]:[%d] %d read gpiovalue is fail\r\n", __FUNCTION__, __LINE__, gpioN); + zlog_error(zbt, "%d read gpiovalue is fail", gpioN); close(fd); - return (-1); // exit(1); + return (-1); } close(fd); - // printf("gpio%d get %d\r\n",gpioN,value); + zlog_info(zbt, "gpio%d get %d", gpioN, value); return value; } -int CheckFileVersion(int argc, char **argv) { - std::string strVersion = GlobalConfig::Version; - int optionchar; /*闂備緡鍋勯ˇ鐢稿Υ瀹ュ洠鍋撳☉娆樻闁瑰嚖鎷�??*/ - if (argc >= 2) { - if (strcmp(argv[1], "--debugmode") == 0) { - GlobalConfig::RUN_MODE = 1; - return 0; - } - optionchar = getopt(argc, argv, "vVhH?"); - switch (optionchar) { - case 'v': - case 'V': - GlobalConfig::RUN_MODE = 1; - print_debug("Compile time:%s %s\n", __DATE__, __TIME__); - print_debug("The internal version is:%s.\n", strVersion.c_str()); - break; - case 'h': - case 'H': - case '?': printf("Please input -v or -V to get the file version.\n"); break; - default: break; - } - return 1; - } - return 0; -} - -int config_uart(const char *Port, int speed) { - int iFd = open(Port, O_RDWR | O_NOCTTY); +int config_uart(const char *port, int speed) { + int iFd = open(port, O_RDWR | O_NOCTTY); if (iFd < 0) { + zlog_error(zbt, "open port:%s failed", port); return -1; } struct termios opt; @@ -379,6 +369,7 @@ int config_uart(const char *Port, int speed) { cfsetispeed(&opt, speed); cfsetospeed(&opt, speed); if (tcgetattr(iFd, &opt) < 0) { + zlog_error(zbt, "tcgetattr failed"); return -1; } opt.c_lflag &= ~(ECHO | ICANON | IEXTEN | ISIG); @@ -389,10 +380,10 @@ int config_uart(const char *Port, int speed) { opt.c_cc[VMIN] = 255; opt.c_cc[VTIME] = 5; if (tcsetattr(iFd, TCSANOW, &opt) < 0) { + zlog_error(zbt, "tcsetattr failed"); return -1; } tcflush(iFd, TCIOFLUSH); - return iFd; } @@ -401,6 +392,7 @@ int write_data(int fd, char *buff, int len) { char buf[100]; ret = write(fd, buff, len); if (ret < 0) { + zlog_error(zct, "write failed"); return -1; } return ret; @@ -426,7 +418,7 @@ int read_data(int fd, char *buff, int len, int timeout) { ret = read(fd, buff, len); if (ret < 0) { - perror("read_data"); + zlog_error(zct, "read_data failed"); return -1; } buff[ret] = '\0'; @@ -434,21 +426,25 @@ int read_data(int fd, char *buff, int len, int timeout) { } int ModifyMac(char *buff) { - FILE *fp = fopen("/opt/system/mac", "w"); + FILE *fp = NULL; + fp = fopen("/opt/system/mac", "w"); + if (fp == NULL) { + zlog_error(zbt, "fail to open /opt/system/mac"); + return 1; + } fprintf(fp, buff); fprintf(fp, "\n"); fclose(fp); system("cp /opt/system/mac /opt/system/macbak"); system("/opt/Cidn/init.sh"); + return 0; } void mssleep(unsigned long microseconds) { struct timespec req; struct timespec rem; - req.tv_sec = microseconds / 1000000; req.tv_nsec = (microseconds % 1000000) * 1000; - nanosleep(&req, &rem); } @@ -458,9 +454,7 @@ std::string GetCurrentTime() { char time_now[128]; gettimeofday(&tv, NULL); localtime_r(&tv.tv_sec, &nowtime); - sprintf(time_now, "%d-%d-%d %d:%d:%d.%03d ", nowtime.tm_year + 1900, nowtime.tm_mon + 1, nowtime.tm_mday, nowtime.tm_hour, nowtime.tm_min, nowtime.tm_sec, (int)(tv.tv_usec / 1000)); - std::string strtime_now = std::string((char *)time_now); return strtime_now; } @@ -476,7 +470,7 @@ tm *get_current_date() { imonth = tm_info->tm_mon + 1; day = tm_info->tm_mday; hour = tm_info->tm_hour; - print_info("year = %d,month = %d,day = %d\n", iyear, imonth, day); + zlog_info(zct, "year = %d,month = %d,day = %d", iyear, imonth, day); return tm_info; } @@ -485,11 +479,12 @@ int system_custom(const char *cmd, char *buf) { int res; char temp[1024] = {0}; if (cmd == NULL) { + zlog_error(zct, "cmd is NULL"); return -1; } if ((fp = popen(cmd, "r")) == NULL) { - print_error("popen error\n"); + zlog_error(zct, "popen error"); return -1; } else { buf[0] = '\0'; @@ -536,35 +531,36 @@ std::string GetRTC(char *timestamp, int &millisecond) { char rtcTime[100] = {0x00}; int fd = open("/dev/rtc0", O_RDONLY); if (fd < 0) { - perror("open /dev/rtc0"); - } else { - struct rtc_time rtc_tm; - if (ioctl(fd, RTC_RD_TIME, &rtc_tm) < 0) { - perror("ioctl RTC_RD_TIME"); - } else { - clock_gettime(CLOCK_REALTIME, &ts); - millisecond = (int)(ts.tv_nsec / 1000000); - printf("RTC date/time is %d-%d-%d, %02d:%02d:%02d\n", rtc_tm.tm_year + 1900, rtc_tm.tm_mon + 1, rtc_tm.tm_mday, rtc_tm.tm_hour, rtc_tm.tm_min, rtc_tm.tm_sec); - sprintf(rtcTime, "%d-%d-%d, %02d:%02d:%02d", rtc_tm.tm_year + 1900, rtc_tm.tm_mon + 1, rtc_tm.tm_mday, rtc_tm.tm_hour, rtc_tm.tm_min, rtc_tm.tm_sec); + zlog_error(zct, "fail to open /dev/rtc0"); + return ""; + } + struct rtc_time rtc_tm; + if (ioctl(fd, RTC_RD_TIME, &rtc_tm) < 0) { + zlog_error(zct, "fail to ioctl RTC_RD_TIME"); + return ""; + } + clock_gettime(CLOCK_REALTIME, &ts); + millisecond = (int)(ts.tv_nsec / 1000000); + printf("RTC date/time is %d-%d-%d, %02d:%02d:%02d\n", rtc_tm.tm_year + 1900, rtc_tm.tm_mon + 1, rtc_tm.tm_mday, rtc_tm.tm_hour, rtc_tm.tm_min, rtc_tm.tm_sec); + sprintf(rtcTime, "%d-%d-%d, %02d:%02d:%02d", rtc_tm.tm_year + 1900, rtc_tm.tm_mon + 1, rtc_tm.tm_mday, rtc_tm.tm_hour, rtc_tm.tm_min, rtc_tm.tm_sec); - // 闁诲繐绻愰弫鍍糲_time缂傚倷鐒﹂幐濠氭倵椤栨稒濯撮柟鎹愵潐缁侇噣鏌熺拠鈩冪窔閻犳劗鍔巌me_t闂佸搫鍟悥鐓幬涢崸妤€绠i柨鐕傛嫹 - tm.tm_year = rtc_tm.tm_year; - tm.tm_mon = rtc_tm.tm_mon; - tm.tm_mday = rtc_tm.tm_mday; - tm.tm_hour = rtc_tm.tm_hour; - tm.tm_min = rtc_tm.tm_min; - tm.tm_sec = rtc_tm.tm_sec; - tm.tm_isdst = -1; // 婵炴垶鎸哥粔铏箾閸ヮ剚鍋ㄩ柕濞垮労濡叉儳霉閻橆喖鈧牕顪冮敓锟� + // 闂佽绻愮换鎰板极閸嶇巢_time缂傚倸鍊烽悞锕傚箰婵犳碍鍊垫い鏍ㄧ⊕婵挳鏌熼幑鎰垫綈缂佷緡鍣i弻鐔烘嫚閳╁啰绐旈柣鐘冲姉閸斿穼me_t闂備礁鎼崯顐︽偉閻撳宫娑㈠锤濡も偓缁狅綁鏌ㄩ悤鍌涘 + tm.tm_year = rtc_tm.tm_year; + tm.tm_mon = rtc_tm.tm_mon; + tm.tm_mday = rtc_tm.tm_mday; + tm.tm_hour = rtc_tm.tm_hour; + tm.tm_min = rtc_tm.tm_min; + tm.tm_sec = rtc_tm.tm_sec; + tm.tm_isdst = -1; // 濠电偞鍨堕幐鍝ョ矓閾忣偆绠鹃柛銉墯閸嬨劑鏌曟繛鍨姶婵″弶鍎抽湁闁绘﹩鍠栭埀顒佺墪椤啴鏁撻敓锟� - rtc_timestamp = mktime(&tm); + rtc_timestamp = mktime(&tm); - if (rtc_timestamp == -1) { - perror("mktime"); - } - printf("RTC timestamp is %ld,millisecond = %d\n", rtc_timestamp, millisecond); - sprintf(timestamp, "%ld", rtc_timestamp); - } + if (rtc_timestamp == -1) { + zlog_error(zct, "fail to mktime"); + return ""; } + printf("RTC timestamp is %ld,millisecond = %d\n", rtc_timestamp, millisecond); + sprintf(timestamp, "%ld", rtc_timestamp); close(fd); return std::string(rtcTime); } @@ -605,7 +601,7 @@ std::vector ReadStrUpdate(std::string filename) { std::fstream is; is.open(filename.c_str(), std::ios::in); if (reader.parse(is, root)) { - print_info("root = %d\n", root.size()); + zlog_info(zbt, "root = %d", root.size()); for (int i = 0; i < root.size(); i++) { hwVersion = root[i]["hw_vesion"]; for (int i = 0; i < hwVersion.size(); i++) { @@ -613,7 +609,7 @@ std::vector ReadStrUpdate(std::string filename) { } datanodeUpdate.strUpdataFileName = root[i]["fw_name"].asString(); datanodeUpdate.strSoftVersion = root[i]["sf_vesion"].asString(); - print_info("strUpdataFileName = %s,strSoftVersion = %s\n", datanodeUpdate.strUpdataFileName.c_str(), datanodeUpdate.strSoftVersion.c_str()); + zlog_info(zbt, "strUpdataFileName = %s, strSoftVersion = %s", datanodeUpdate.strUpdataFileName.c_str(), datanodeUpdate.strSoftVersion.c_str()); vecDataNodeUpdate.push_back(datanodeUpdate); } } @@ -628,76 +624,77 @@ void ReadStrConfig(std::string filename) { Json::Reader reader; is.open(filename.c_str(), std::ios::in); string zigbeeChannel; - if (reader.parse(is, root)) { - gateWay = root["gateWay"]; - dataNode = root["dataNodeArray"]; - print_info("dataNode = %d\n", dataNode.size()); - for (int i = 0; i < dataNode.size(); i++) { - string softVersion = dataNode[i]["softVersion"].asString(); - string bpNo = dataNode[i]["bpNo"].asString(); - print_info("bpNo = %s\n", bpNo.c_str()); - string wakeupTime = dataNode[i]["wakeupTime"].asString(); - int viff = dataNode[i]["viff"].asInt(); - string StaticTime = dataNode[i]["StaticTime"].asString(); - int configFlag = dataNode[i]["configFlag"].asInt(); - int rangeValue = dataNode[i]["range"].asInt(); - int updateValue = dataNode[i]["update"].asInt(); - string zigbeeLongAddr = dataNode[i]["zigbeeLongAddr"].asString(); - int accFlag = dataNode[i]["accFlag"].asInt(); - print_info("accFlag = %d\n", accFlag); - int temTopFlag = dataNode[i]["temTopFlag"].asInt(); - string startBrands = dataNode[i]["startBrands"].asString(); - int waveInterVal = dataNode[i]["waveInterVal"].asInt(); - string zigbeePanId = dataNode[i]["zigbeePanId"].asString(); - int waveTime = dataNode[i]["waveTime"].asInt(); - int zigbeePower = dataNode[i]["zigbeePower"].asInt(); - int zigbeeRetry = dataNode[i]["zigbeeRetry"].asInt(); - string stopBrands = dataNode[i]["stopBrands"].asString(); - int featureInterVal = dataNode[i]["featureInterVal"].asInt(); - int zigbeeFlag = dataNode[i]["zigbeeFlag"].asInt(); - string zigbeeDesAddr = dataNode[i]["zigbeeDesAddr"].asString(); - print_info("zigbeeDesAddr = %s\n", zigbeeDesAddr.c_str()); - int ZigbeeRetryGap = dataNode[i]["zigbeeRetryGap"].asInt(); - string dataNodeNo = dataNode[i]["dataNodeNo"].asString(); - int initFlag = dataNode[i]["initFlag"].asInt(); - string faultFrequency = dataNode[i]["faultFrequency"].asString(); - int temBotFlag = dataNode[i]["temBotFlag"].asInt(); - string bateryV = dataNode[i]["bateryV"].asString(); - int ACCSampleTime = dataNode[i]["ACCSampleTime"].asInt(); - print_info("ACCSampleTime = %d\n", ACCSampleTime); - string firstPowerTime = dataNode[i]["firstPowerTime"].asString(); - string serialNo = dataNode[i]["serialNo"].asString(); - string zigbeeAddr = dataNode[i]["zigbeeAddr"].asString(); - string productNo = dataNode[i]["productNo"].asString(); - string timeStamp = dataNode[i]["timeStamp"].asString(); - zigbeeChannel = dataNode[i]["zigbeeChannel"].asString(); - int RSSI = dataNode[i]["RSSI"].asInt(); - print_info("RSSI = %d\n", RSSI); - string hardVersion = dataNode[i]["hardVersion"].asString(); - string envelopeBandPass = dataNode[i]["envelopeBandPass"].asString(); - int samplingRate = dataNode[i]["samplingRate"].asInt(); - string dataNodeName = dataNode[i]["dataNodeName"].asString(); - int status = dataNode[i]["status"].asInt(); - int EquipSta = dataNode[i]["equipSta"].asInt(); - char insertSql[1024] = {0}; - char whereCon[100] = {0x00}; - sprintf(whereCon, "dataNodeNo = '%s'", dataNodeNo.c_str()); - int rows = sqlite_db_ctrl::instance().GetTableRows(T_SENSOR_INFO(TNAME), whereCon); - if (rows > 0) continue; - sprintf(insertSql, "'%s','%s','%d','%d','%d','%d','%d','%d',\ - '%s','%s','%s','%s','%s','%d',\ - '%d','%d','%d','%s','%d','%s',\ - '%s','%d','%d','%d','%s','%d','%s','%s',\ - '%s','%d','%s','%s','%s',\ - '%d','%d','%d','%d','%d','%s','%d','%d','%d','0','0,0'", - dataNodeNo.c_str(), dataNodeName.c_str(), initFlag, accFlag, zigbeeFlag, temTopFlag, temBotFlag, EquipSta, hardVersion.c_str(), softVersion.c_str(), bpNo.c_str(), serialNo.c_str(), firstPowerTime.c_str(), atoi(wakeupTime.c_str()), atoi(StaticTime.c_str()), - waveTime, atoi(bateryV.c_str()), productNo.c_str(), configFlag, startBrands.c_str(), stopBrands.c_str(), featureInterVal, waveInterVal, samplingRate, "", rangeValue, envelopeBandPass.c_str(), faultFrequency.c_str(), zigbeePanId.c_str(), - atoi(zigbeeChannel.c_str()), zigbeeAddr.c_str(), zigbeeLongAddr.c_str(), zigbeeDesAddr.c_str(), zigbeePower, zigbeeRetry, ZigbeeRetryGap, ACCSampleTime, status, timeStamp.c_str(), viff, RSSI, updateValue); - sqlite_db_ctrl::instance().InsertData(T_SENSOR_INFO(TNAME), insertSql); - } - } else { - print_info("parse error\n"); + if (reader.parse(is, root) == 0) { + zlog_error(zbt, "fail to parse file:%s", filename.c_str()); + return; } + gateWay = root["gateWay"]; + dataNode = root["dataNodeArray"]; + zlog_info(zbt, "dataNode = %d", dataNode.size()); + for (int i = 0; i < dataNode.size(); i++) { + std::string softVersion = dataNode[i]["softVersion"].asString(); + std::string bpNo = dataNode[i]["bpNo"].asString(); + zlog_info(zbt, "bpNo = %s", bpNo.c_str()); + std::string wakeupTime = dataNode[i]["wakeupTime"].asString(); + int viff = dataNode[i]["viff"].asInt(); + std::string StaticTime = dataNode[i]["StaticTime"].asString(); + int configFlag = dataNode[i]["configFlag"].asInt(); + int rangeValue = dataNode[i]["range"].asInt(); + int updateValue = dataNode[i]["update"].asInt(); + std::string zigbeeLongAddr = dataNode[i]["zigbeeLongAddr"].asString(); + int accFlag = dataNode[i]["accFlag"].asInt(); + zlog_info(zbt, "accFlag = %d", accFlag); + int temTopFlag = dataNode[i]["temTopFlag"].asInt(); + std::string startBrands = dataNode[i]["startBrands"].asString(); + int waveInterVal = dataNode[i]["waveInterVal"].asInt(); + std::string zigbeePanId = dataNode[i]["zigbeePanId"].asString(); + int waveTime = dataNode[i]["waveTime"].asInt(); + int zigbeePower = dataNode[i]["zigbeePower"].asInt(); + int zigbeeRetry = dataNode[i]["zigbeeRetry"].asInt(); + std::string stopBrands = dataNode[i]["stopBrands"].asString(); + int featureInterVal = dataNode[i]["featureInterVal"].asInt(); + int zigbeeFlag = dataNode[i]["zigbeeFlag"].asInt(); + std::string zigbeeDesAddr = dataNode[i]["zigbeeDesAddr"].asString(); + zlog_info(zbt, "zigbeeDesAddr = %s", zigbeeDesAddr.c_str()); + int ZigbeeRetryGap = dataNode[i]["zigbeeRetryGap"].asInt(); + std::string dataNodeNo = dataNode[i]["dataNodeNo"].asString(); + int initFlag = dataNode[i]["initFlag"].asInt(); + std::string faultFrequency = dataNode[i]["faultFrequency"].asString(); + int temBotFlag = dataNode[i]["temBotFlag"].asInt(); + std::string bateryV = dataNode[i]["bateryV"].asString(); + int ACCSampleTime = dataNode[i]["ACCSampleTime"].asInt(); + zlog_info(zbt, "ACCSampleTime = %d", ACCSampleTime); + std::string firstPowerTime = dataNode[i]["firstPowerTime"].asString(); + std::string serialNo = dataNode[i]["serialNo"].asString(); + std::string zigbeeAddr = dataNode[i]["zigbeeAddr"].asString(); + std::string productNo = dataNode[i]["productNo"].asString(); + std::string timeStamp = dataNode[i]["timeStamp"].asString(); + zigbeeChannel = dataNode[i]["zigbeeChannel"].asString(); + int RSSI = dataNode[i]["RSSI"].asInt(); + zlog_info(zbt, "RSSI = %d", RSSI); + std::string hardVersion = dataNode[i]["hardVersion"].asString(); + std::string envelopeBandPass = dataNode[i]["envelopeBandPass"].asString(); + int samplingRate = dataNode[i]["samplingRate"].asInt(); + std::string dataNodeName = dataNode[i]["dataNodeName"].asString(); + int status = dataNode[i]["status"].asInt(); + int EquipSta = dataNode[i]["equipSta"].asInt(); + char insertSql[1024] = {0}; + char whereCon[100] = {0x00}; + sprintf(whereCon, "dataNodeNo = '%s'", dataNodeNo.c_str()); + int rows = sqlite_db_ctrl::instance().GetTableRows(T_SENSOR_INFO(TNAME), whereCon); + if (rows > 0) continue; + sprintf(insertSql, "'%s','%s','%d','%d','%d','%d','%d','%d',\ + '%s','%s','%s','%s','%s','%d',\ + '%d','%d','%d','%s','%d','%s',\ + '%s','%d','%d','%d','%s','%d','%s','%s',\ + '%s','%d','%s','%s','%s',\ + '%d','%d','%d','%d','%d','%s','%d','%d','%d','0','0,0'", + dataNodeNo.c_str(), dataNodeName.c_str(), initFlag, accFlag, zigbeeFlag, temTopFlag, temBotFlag, EquipSta, hardVersion.c_str(), softVersion.c_str(), bpNo.c_str(), serialNo.c_str(), firstPowerTime.c_str(), atoi(wakeupTime.c_str()), atoi(StaticTime.c_str()), + waveTime, atoi(bateryV.c_str()), productNo.c_str(), configFlag, startBrands.c_str(), stopBrands.c_str(), featureInterVal, waveInterVal, samplingRate, "", rangeValue, envelopeBandPass.c_str(), faultFrequency.c_str(), zigbeePanId.c_str(), + atoi(zigbeeChannel.c_str()), zigbeeAddr.c_str(), zigbeeLongAddr.c_str(), zigbeeDesAddr.c_str(), zigbeePower, zigbeeRetry, ZigbeeRetryGap, ACCSampleTime, status, timeStamp.c_str(), viff, RSSI, updateValue); + sqlite_db_ctrl::instance().InsertData(T_SENSOR_INFO(TNAME), insertSql); + } + is.close(); } @@ -706,138 +703,156 @@ void ImportConfig(std::string filename) { std::fstream is; Json::Reader reader; is.open(filename.c_str(), std::ios::in); - string zigbeeChannel; - - Json::Value jsSystemSetting, jsonValnet, jsonValnet1, jsSystemInfo, jsonValZigbee, jsondataNodeArray; - if (reader.parse(is, root)) { - jsSystemInfo = root["SystemInfo"]; - jsonValZigbee = root["zigbee"]; - jsonValnet1 = root["eth1"]; - jsonValnet = root["eth0"]; - jsSystemSetting = root["ServerConfig"]; - jsondataNodeArray = root["dataNodeArray"]; - char insertSql[1024] = {0}; - for (int i = 0; i < jsondataNodeArray.size(); i++) { - Json::Value valNode = jsondataNodeArray[i]; - vector vecDataNode; - for (size_t j = 0; j < valNode.size(); j++) { - vecDataNode.push_back(string(valNode[j].asString())); - } - char dataNodeName[100] = {0x00}; - hexToAscii(vecDataNode[1].c_str(), dataNodeName); - sprintf(insertSql, "'%s','%s','%s','%s','%s','%s','%s','%s',\ - '%s','%s','%s','%s','%s','%s',\ - '%s','%s','%s','%s','%s','%s',\ - '%s','%s','%s','%s','%s','%s','%s','%s',\ - '%s','%s','%s','%s','%s',\ - '%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s'", - vecDataNode[0].c_str(), dataNodeName, vecDataNode[2].c_str(), vecDataNode[3].c_str(), vecDataNode[4].c_str(), vecDataNode[5].c_str(), vecDataNode[6].c_str(), vecDataNode[7].c_str(), vecDataNode[8].c_str(), vecDataNode[9].c_str(), vecDataNode[10].c_str(), - vecDataNode[11].c_str(), vecDataNode[12].c_str(), vecDataNode[13].c_str(), vecDataNode[14].c_str(), vecDataNode[15].c_str(), vecDataNode[16].c_str(), vecDataNode[17].c_str(), vecDataNode[18].c_str(), vecDataNode[19].c_str(), vecDataNode[20].c_str(), - vecDataNode[21].c_str(), vecDataNode[22].c_str(), vecDataNode[23].c_str(), vecDataNode[24].c_str(), vecDataNode[25].c_str(), vecDataNode[26].c_str(), vecDataNode[27].c_str(), vecDataNode[28].c_str(), vecDataNode[29].c_str(), vecDataNode[30].c_str(), - vecDataNode[31].c_str(), vecDataNode[32].c_str(), vecDataNode[33].c_str(), vecDataNode[34].c_str(), vecDataNode[35].c_str(), vecDataNode[36].c_str(), vecDataNode[37].c_str(), vecDataNode[38].c_str(), vecDataNode[39].c_str(), vecDataNode[40].c_str(), - vecDataNode[41].c_str(), vecDataNode[42].c_str(), vecDataNode[43].c_str(), vecDataNode[44].c_str()); - sqlite_db_ctrl::instance().InsertData(T_SENSOR_INFO(TNAME), insertSql); - } - - WriteStr2Config(SERVERCONFIG, "Server", "localServerIpAddress", jsSystemSetting["ServerIpAddress"].asString()); - WriteStr2Config(SERVERCONFIG, "Server", "localServerPort", jsSystemSetting["ServerPort"].asString()); - WriteStr2Config(SERVERCONFIG, "Server", "CommMode", /*param.mCommMode*/ "2"); - WriteStr2Config(SERVERCONFIG, "Server", "UserName", jsSystemSetting["UserName"].asString()); - WriteStr2Config(SERVERCONFIG, "Server", "Password", jsSystemSetting["Password"].asString()); - WriteStr2Config(SERVERCONFIG, "Server", "APN", jsSystemSetting["APN"].asString()); - char APN[100] = {0x00}; - string apn = jsSystemSetting["APN"].asString(); - sprintf(APN, "sed -i '15c \t\t\t\t/opt/quectel-CM/quectel-CM -s %s > /dev/null &' /etc/init.d/S95check5G", apn.c_str()); - system(APN); - - WriteStr2Config(ZIGBEECONFIG, "Zigbee", "channel", jsonValZigbee["channel"].asString()); - WriteStr2Config(ZIGBEECONFIG, "Zigbee", "PanID", jsonValZigbee["PanID"].asString()); - - WriteStr2Config(SYSTEMINFOFILE, "Version", "GateWayVersion", jsSystemInfo["GateWayVersion"].asString()); - WriteStr2Config(SYSTEMINFOFILE, "Version", "SystemVersion", jsSystemInfo["SystemVersion"].asString()); - WriteStr2Config(SYSTEMINFOFILE, "Version", "WebVersion", jsSystemInfo["WebVersion"].asString()); - WriteStr2Config(SYSTEMINFOFILE, "Version", "GateWayHwVesion", jsSystemInfo["GateWayHwVesion"].asString()); - WriteStr2Config(SYSTEMINFOFILE, "Version", "GateWayProduct", jsSystemInfo["GateWayProduct"].asString()); + if (!is.is_open()) { + zlog_error(zbt, "fail to open:%s", filename.c_str()); + return; } + + if (reader.parse(is, root) == 0) { + zlog_error(zbt, "fail to parse:%s", filename.c_str()); + return; + } + std::string zigbeeChannel; + Json::Value jsSystemSetting, jsonValnet, jsonValnet1, jsSystemInfo, jsonValZigbee, jsondataNodeArray; + + jsSystemInfo = root["SystemInfo"]; + jsonValZigbee = root["zigbee"]; + jsonValnet1 = root["eth1"]; + jsonValnet = root["eth0"]; + jsSystemSetting = root["ServerConfig"]; + jsondataNodeArray = root["dataNodeArray"]; + char insertSql[1024] = {0}; + for (int i = 0; i < jsondataNodeArray.size(); i++) { + Json::Value valNode = jsondataNodeArray[i]; + std::vector vecDataNode; + for (size_t j = 0; j < valNode.size(); j++) { + vecDataNode.push_back(string(valNode[j].asString())); + } + char dataNodeName[100] = {0x00}; + hexToAscii(vecDataNode[1].c_str(), dataNodeName); + memset(insertSql, 0, 1024); + sprintf(insertSql, "'%s','%s','%s','%s','%s','%s','%s','%s',\ + '%s','%s','%s','%s','%s','%s',\ + '%s','%s','%s','%s','%s','%s',\ + '%s','%s','%s','%s','%s','%s','%s','%s',\ + '%s','%s','%s','%s','%s',\ + '%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s'", + vecDataNode[0].c_str(), dataNodeName, vecDataNode[2].c_str(), vecDataNode[3].c_str(), + vecDataNode[4].c_str(), vecDataNode[5].c_str(), vecDataNode[6].c_str(), + vecDataNode[7].c_str(), vecDataNode[8].c_str(), vecDataNode[9].c_str(), vecDataNode[10].c_str(), + vecDataNode[11].c_str(), vecDataNode[12].c_str(), vecDataNode[13].c_str(), vecDataNode[14].c_str(), + vecDataNode[15].c_str(), vecDataNode[16].c_str(), vecDataNode[17].c_str(), vecDataNode[18].c_str(), + vecDataNode[19].c_str(), vecDataNode[20].c_str(), + vecDataNode[21].c_str(), vecDataNode[22].c_str(), vecDataNode[23].c_str(), vecDataNode[24].c_str(), + vecDataNode[25].c_str(), vecDataNode[26].c_str(), vecDataNode[27].c_str(), vecDataNode[28].c_str(), + vecDataNode[29].c_str(), vecDataNode[30].c_str(), + vecDataNode[31].c_str(), vecDataNode[32].c_str(), vecDataNode[33].c_str(), vecDataNode[34].c_str(), + vecDataNode[35].c_str(), vecDataNode[36].c_str(), vecDataNode[37].c_str(), vecDataNode[38].c_str(), + vecDataNode[39].c_str(), vecDataNode[40].c_str(), + vecDataNode[41].c_str(), vecDataNode[42].c_str(), vecDataNode[43].c_str(), vecDataNode[44].c_str()); + sqlite_db_ctrl::instance().InsertData(T_SENSOR_INFO(TNAME), insertSql); + } + + WriteStr2Config(SERVERCONFIG, "Server", "localServerIpAddress", jsSystemSetting["ServerIpAddress"].asString()); + WriteStr2Config(SERVERCONFIG, "Server", "localServerPort", jsSystemSetting["ServerPort"].asString()); + WriteStr2Config(SERVERCONFIG, "Server", "CommMode", "2"); + WriteStr2Config(SERVERCONFIG, "Server", "UserName", jsSystemSetting["UserName"].asString()); + WriteStr2Config(SERVERCONFIG, "Server", "Password", jsSystemSetting["Password"].asString()); + WriteStr2Config(SERVERCONFIG, "Server", "APN", jsSystemSetting["APN"].asString()); + char APN[100] = {0x00}; + std::string apn = jsSystemSetting["APN"].asString(); + sprintf(APN, "sed -i '15c \t\t\t\t/opt/quectel-CM/quectel-CM -s %s > /dev/null &' /etc/init.d/S95check5G", apn.c_str()); + system(APN); + + WriteStr2Config(ZIGBEECONFIG, "Zigbee", "channel", jsonValZigbee["channel"].asString()); + WriteStr2Config(ZIGBEECONFIG, "Zigbee", "PanID", jsonValZigbee["PanID"].asString()); + + WriteStr2Config(SYSTEMINFOFILE, "Version", "GateWayVersion", jsSystemInfo["GateWayVersion"].asString()); + WriteStr2Config(SYSTEMINFOFILE, "Version", "SystemVersion", jsSystemInfo["SystemVersion"].asString()); + WriteStr2Config(SYSTEMINFOFILE, "Version", "WebVersion", jsSystemInfo["WebVersion"].asString()); + WriteStr2Config(SYSTEMINFOFILE, "Version", "GateWayHwVesion", jsSystemInfo["GateWayHwVesion"].asString()); + WriteStr2Config(SYSTEMINFOFILE, "Version", "GateWayProduct", jsSystemInfo["GateWayProduct"].asString()); } + int UpdataDataNodeConfig(std::string filename) { - vector vecDataNode; //婵犮垹婀辨晶妤€危閹寸偟鈻旈柍褜鍓氱粙澶愵敂閸曨厽鎲荤紓浣诡殣缂傛氨鎲伴崱娑樿Е闁硅揪绠戝▍锟� - //婵炲濮伴崕鎾敋娴兼潙绀傞柕澶涢檮閻撴瑧鈧鍠栫换鎴炴櫠閿曗偓椤曪綁鍩€椤掑嫬妫橀柛銉檮椤愶拷 - ifstream csv_data(filename, ios::in); + std::vector vecDataNode; //濠电姰鍨瑰﹢杈ㄦ櫠濡も偓鍗遍柟瀵稿仧閳绘棃鏌嶈閸撴氨绮欐径鎰垫晜闁告洦鍘介幉鑽ょ磽娴h娈g紓鍌涙皑閹蹭即宕卞☉妯啃曢梺纭呮彧缁犳垵鈻嶉敓锟� + //濠电偛顕慨浼村磿閹绢噮鏁嬪ù鍏兼綑缁€鍌炴煏婢舵盯妾柣鎾寸懅閳ь剚顔栭崰鏍崲閹寸偞娅犻柨鏇楀亾妞ゆ洩缍侀崺鈧い鎺戝濡﹢鏌涢妷顖炴妞ゆ劧鎷� + std::ifstream csv_data(filename, ios::in); int iRet = 0; if (!csv_data.is_open()) { - cout << "Error: opening file fail" << endl; + zlog_error(zbt, "UpdataDataNodeConfig fail to open:%s", filename.c_str()); return -1; - } else { - string line; + } + std::string line; + std::vector words; //濠电姰鍨瑰﹢杈ㄦ櫠濡も偓鍗遍柟瀵稿仧閳绘棃鏌嶈閸撴氨绮欐径鎰垫晜闁告洦鍘介幉鑽ょ磽娴h娈g紓鍌涙皑閹蹭即宕卞☉妯啃曢梺纭呮彧缁犳垵鈻嶉敓锟� + std::string word; - vector words; //婵犮垹婀辨晶妤€危閹寸偟鈻旈柍褜鍓氱粙澶愵敂閸曨厽鎲荤紓浣诡殣缂傛氨鎲伴崱娑樿Е闁硅揪绠戝▍锟� - string word; + DataNodeInfo dataNode; + // ------------闂佽崵濮村ú鈺咁敋瑜戦妵鎰板炊椤掆偓閺嬩線鏌eΔ鈧悧鍡欑矈閿燂拷----------------- + // 闂佽崵濮村ú鈺咁敋瑜戦妵鎰板炊椤掆偓閸愨偓闂佹悶鍎洪崜锕傚汲椤栫偞鍋犳繛鎴f珪鐎氾拷 + getline(csv_data, line); - DataNodeInfo dataNode; - // ------------闁荤姴娲╅褑銇愰崶顒€鏋侀柣妤€鐗嗙粊锟�----------------- - // 闁荤姴娲╅褑銇愰崶顒€鍐€闁搞儺鍓﹂弳顖炴偠濞戣櫕瀚� - getline(csv_data, line); - - istringstream sin; - // 闂佸湱枪椤︽娊銆侀幋鐘冲珰閻犲洦褰冪徊鍧楁煛娴e搫顣肩€规搫鎷� - while (getline(csv_data, line)) { - words.clear(); - sin.clear(); - sin.str(line); - while (getline(sin, word, ',')) { - cout << word << endl; - words.push_back(word); - } - string mac = words[1]; - if (mac != GlobalConfig::MacAddr_G) { - iRet = -2; - break; - } - dataNode.ZigbeeLongAddr = words[7]; - dataNode.FeatureInterVal = atoi(words[16].c_str()); - dataNode.WaveInterVal = atoi(words[17].c_str()); - dataNode.SamplingRate = atoi(words[18].c_str()); - print_info("words[17] = %s\n", words[19].c_str()); - - if (words[19].find("8g") != string::npos) { - dataNode.Range = 0; - } else if (words[19].find("16g") != string::npos) { - dataNode.Range = 1; - } else if (words[19].find("32g") != string::npos) { - dataNode.Range = 2; - } else if (words[19].find("64g") != string::npos) { - dataNode.Range = 3; - } else if (words[19].find("50g") != string::npos) { - dataNode.Range = 0; - } - - dataNode.ACCSampleTime = atoi(words[20].c_str()); - dataNode.VIntegralFilterFrequency = atoi(words[21].c_str()); - dataNode.ZigbeePower = atoi(words[22].c_str()); - dataNode.ZigbeeRetry = atoi(words[23].c_str()); - int update = atoi(words[24].c_str()); - if (update == 1) vecDataNode.push_back(dataNode); + std::istringstream sin; + // 闂備礁婀辨灙妞わ附濞婇妴渚€骞嬮悩鍐茬彴闁荤姴娲﹁ぐ鍐緤閸ф鐓涘ù锝呮惈椤h偐鈧鎼幏锟� + while (getline(csv_data, line)) { + words.clear(); + sin.clear(); + sin.str(line); + while (getline(sin, word, ',')) { + words.push_back(word); } - csv_data.close(); + std::string mac = words[1]; + if (mac != GlobalConfig::MacAddr_G) { + iRet = -2; + break; + } + dataNode.ZigbeeLongAddr = words[7]; + dataNode.FeatureInterVal = atoi(words[16].c_str()); + dataNode.WaveInterVal = atoi(words[17].c_str()); + dataNode.SamplingRate = atoi(words[18].c_str()); + + if (words[19].find("8g") != string::npos) { + dataNode.Range = 0; + } else if (words[19].find("16g") != string::npos) { + dataNode.Range = 1; + } else if (words[19].find("32g") != string::npos) { + dataNode.Range = 2; + } else if (words[19].find("64g") != string::npos) { + dataNode.Range = 3; + } else if (words[19].find("50g") != string::npos) { + dataNode.Range = 0; + } + + dataNode.ACCSampleTime = atoi(words[20].c_str()); + dataNode.VIntegralFilterFrequency = atoi(words[21].c_str()); + dataNode.ZigbeePower = atoi(words[22].c_str()); + dataNode.ZigbeeRetry = atoi(words[23].c_str()); + int update = atoi(words[24].c_str()); + if (update == 1) vecDataNode.push_back(dataNode); } - - char whereCon[1024] = {0}; - char updateSql[1024] = {0}; - if (vecDataNode.size() > 0) { - for (int i = 0; i < vecDataNode.size(); i++) { - sprintf(updateSql, "featureInterVal='%d',waveInterVal='%d',range='%d',samplingRate='%d',AccSampleTime = '%d',viff ='%d' ,ZigbeePower = '%d',ZigbeeRetry = '%d',UpdateFlag = 0", vecDataNode[i].FeatureInterVal, vecDataNode[i].WaveInterVal, vecDataNode[i].Range, - vecDataNode[i].SamplingRate, vecDataNode[i].ACCSampleTime, vecDataNode[i].VIntegralFilterFrequency, vecDataNode[i].ZigbeePower, vecDataNode[i].ZigbeeRetry); - sprintf(whereCon, "dataNodeNo='%s'", vecDataNode[i].ZigbeeLongAddr.c_str()); - - int iRet = sqlite_db_ctrl::instance().UpdateTableData(T_SENSOR_INFO(TNAME), updateSql, whereCon); - memset(whereCon, 0x00, sizeof(whereCon)); - memset(updateSql, 0x00, sizeof(updateSql)); - } - iRet = vecDataNode.size(); - } else { + csv_data.close(); + if (vecDataNode.size() == 0) { iRet = -3; - } + zlog_error(zbt, "UpdataDataNodeConfig vecDataNode is 0"); + return iRet; + } + + char whereCon[1024] = {0}; + char updateSql[1024] = {0}; + for (int i = 0; i < vecDataNode.size(); i++) { + sprintf(updateSql, "featureInterVal='%d',waveInterVal='%d',range='%d',samplingRate='%d',AccSampleTime = '%d',viff ='%d' ,ZigbeePower = '%d',ZigbeeRetry = '%d',UpdateFlag = 0", vecDataNode[i].FeatureInterVal, vecDataNode[i].WaveInterVal, vecDataNode[i].Range, + vecDataNode[i].SamplingRate, vecDataNode[i].ACCSampleTime, vecDataNode[i].VIntegralFilterFrequency, vecDataNode[i].ZigbeePower, vecDataNode[i].ZigbeeRetry); + sprintf(whereCon, "dataNodeNo='%s'", vecDataNode[i].ZigbeeLongAddr.c_str()); + + iRet = sqlite_db_ctrl::instance().UpdateTableData(T_SENSOR_INFO(TNAME), updateSql, whereCon); + if (iRet != 0) { + zlog_error(zbt, "UpdataDataNodeConfig UpdateTableData fail"); + return -4; + } + memset(whereCon, 0x00, sizeof(whereCon)); + memset(updateSql, 0x00, sizeof(updateSql)); + } + iRet = vecDataNode.size(); return iRet; } @@ -850,13 +865,22 @@ int WriteStr2Config(std::string filename, std::string config, std::string option Json::Reader reader; std::fstream is; is.open(filename.c_str(), std::ios::in); - if (reader.parse(is, root)) { - subroot = root[config]; - if (listable) - list = root[config]["list"]; - else - op = root[config]["option"]; + if (!is.is_open()) { + zlog_error(zct, "fail to open: %s", filename.c_str()); + return 1; } + if (reader.parse(is, root) == 0) { + zlog_error(zct, "fail to parse: %s", filename.c_str()); + return 2; + } + + subroot = root[config]; + if (listable) { + list = root[config]["list"]; + } else { + op = root[config]["option"]; + } + is.close(); if (listable) { @@ -881,20 +905,19 @@ std::string GetLocalMac(const char *net) { char mac_addr[30]; sock_mac = socket(AF_INET, SOCK_STREAM, 0); if (sock_mac == -1) { - perror("create socket falise...mac/n"); + zlog_error(zct, "[GetLocalMac] fail to create socket"); return ""; } memset(&ifr_mac, 0, sizeof(ifr_mac)); strncpy(ifr_mac.ifr_name, net, sizeof(ifr_mac.ifr_name) - 1); if ((ioctl(sock_mac, SIOCGIFHWADDR, &ifr_mac)) < 0) { - printf("mac ioctl error/n"); + zlog_error(zct, "mac ioctl error"); return ""; } sprintf(mac_addr, "%02x%02x%02x%02x%02x%02x", (unsigned char)ifr_mac.ifr_hwaddr.sa_data[0], (unsigned char)ifr_mac.ifr_hwaddr.sa_data[1], (unsigned char)ifr_mac.ifr_hwaddr.sa_data[2], (unsigned char)ifr_mac.ifr_hwaddr.sa_data[3], (unsigned char)ifr_mac.ifr_hwaddr.sa_data[4], (unsigned char)ifr_mac.ifr_hwaddr.sa_data[5]); - printf("local mac:%s /n", mac_addr); + zlog_info(zbt, "net :%s, local mac:%s", net, mac_addr); close(sock_mac); - LOG_INFO("net : %s,mac:%s\n", net, mac_addr); return std::string(mac_addr); } @@ -902,7 +925,7 @@ std::string GetGwIp_(const char *eth_name) { int sockfd; char gwip_[16] = {0}; if (-1 == (sockfd = socket(PF_INET, SOCK_STREAM, 0))) { - perror_info("socket"); + zlog_error(zct, "[GetGwIp_] fail to create socket"); return ""; } struct ifreq req; @@ -910,13 +933,13 @@ std::string GetGwIp_(const char *eth_name) { bzero(&req, sizeof(struct ifreq)); strcpy(req.ifr_name, eth_name); - // print_info("eth_name = %s\n",eth_name); ioctl(sockfd, SIOCGIFADDR, &req); host = (struct sockaddr_in *)&req.ifr_addr; close(sockfd); if (host) { strcpy(gwip_, inet_ntoa(host->sin_addr)); } + zlog_info(zbt, "eth_name :%s, local ip:%s", eth_name, gwip_); return std::string(gwip_); } @@ -931,49 +954,60 @@ std::string IpAddrInit() { std::string strip = ""; strip = GetGwIp_(WLAN0); if (strip.compare("0.0.0.0") != 0) { + zlog_info(zct, "[IpAddrInit] wlan0: %s", strip.c_str()); return strip; } strip = GetGwIp_(WLAN2); if (strip.compare("0.0.0.0") != 0) { + zlog_info(zct, "[IpAddrInit] wlan2: %s", strip.c_str()); return strip; } strip = GetGwIp_(ETH1); if (strip.compare("0.0.0.0") != 0 && strip.compare("192.168.188.188") != 0) { + zlog_info(zct, "[IpAddrInit] eth1: %s", strip.c_str()); return strip; } strip = GetGwIp_(ETH2); if (strip.compare("0.0.0.0") != 0 && strip.compare("192.168.188.188") != 0) { + zlog_info(zct, "[IpAddrInit] eth2: %s", strip.c_str()); return strip; } strip = GetGwIp_(ETH0); if (strip.compare("0.0.0.0") != 0) { + zlog_info(zct, "[IpAddrInit] eth0: %s", strip.c_str()); return strip; } strip = GetGwIp_(USB0); if (strip.compare("0.0.0.0") != 0) { + zlog_info(zct, "[IpAddrInit] usb0: %s", strip.c_str()); return strip; } strip = GetGwIp_(WWAN0); if (strip.compare("0.0.0.0") != 0) { + zlog_info(zct, "[IpAddrInit] wwan0: %s", strip.c_str()); return strip; } - // return strip; + zlog_warn(zct, "[IpAddrInit] ip: %s", strip.c_str()); + return strip; } -std::string GetWorkNic() { - const char *WLAN0 = "wlan0"; - const char *ETH0 = "eth0"; - std::string strip; - strip = GetGwIp_(WLAN0); - if (strip.compare("0.0.0.0") != 0) { - return std::string(WLAN0); - } - strip = GetGwIp_(ETH0); - if (strip.compare("0.0.0.0") != 0) { - return std::string(ETH0); - } - return std::string(ETH0); -} +// std::string GetWorkNic() { +// const char *WLAN0 = "wlan0"; +// const char *ETH0 = "eth0"; +// std::string strip; +// strip = GetGwIp_(WLAN0); +// if (strip.compare("0.0.0.0") != 0) { +// zlog_info(zbt, "[GetWorkNic] wlan0: %s", strip.c_str()); +// return std::string(WLAN0); +// } +// strip = GetGwIp_(ETH0); +// if (strip.compare("0.0.0.0") != 0) { +// zlog_info(zbt, "[GetWorkNic] eth0: %s", strip.c_str()); +// return std::string(ETH0); +// } +// zlog_error(zbt, "[GetWorkNic] no ip return str eth0"); +// return std::string(ETH0); +// } std::string &ClearAllSpace(std::string &str) { int index = 0; @@ -1029,19 +1063,6 @@ std::string GetSysInfo() { return std::string(sysinfo); } -void StartWriteToDat() { print_info("start writetoDat\n"); } - -float *ReSample(int WaveDataLength, int N, int *NewWaveDataLength, std::vector &WaveData) { - if (N <= 0) return NULL; - int NewLength = (int)WaveDataLength / N; - float *_OutputData = new float[NewLength]; - for (int i = 0; i < NewLength; i++) { - _OutputData[i] = WaveData[i * N]; - } - *NewWaveDataLength = NewLength; - return _OutputData; -} - int SetTime(unsigned long seconds, int milliseconds) { struct timeval tv; time_t timep = (time_t)seconds; @@ -1050,20 +1071,13 @@ int SetTime(unsigned long seconds, int milliseconds) { return settimeofday(&tv, NULL); } -void RemoveConfig() { - char cmd[32] = {0}; - sprintf(cmd, "rm /CIDW/config/*"); - system(cmd); - exit(0); -} - -extern void ZoneConfig(std::string zoneid) { +void ZoneConfig(std::string zoneid) { int a = 0; std::string zonelists[] = {"UTC+12", "UTC+11", "UTC+10", "UTC+9", "UTC+8", "UTC+7", "UTC+6", "UTC+5", "UTC+4", "UTC+3", "UTC+2", "UTC+1", "UTC+0", "UTC-1", "UTC-2", "UTC-3", "UTC-4", "UTC-5", "UTC-6", "UTC-7", "UTC-8", "UTC-9", "UTC-10", "UTC-11"}; for (int i = 0; i < sizeof(zonelists); i++) { if (zoneid.compare(zonelists[i]) == 0) { - a = i; - cout << "a = " << a << endl; + a = i; + zlog_info(zbt, "[ZoneConfig] zoneid:%s, a: %d", zoneid.c_str(), a); break; } } @@ -1094,42 +1108,42 @@ extern void ZoneConfig(std::string zoneid) { case 23: zoneid = "GMT+11"; break; } char cmd[256] = {0}; - // sprintf(cmd, "rm /etc/localtime"); - // system(cmd); memset(cmd, 0, 256); sprintf(cmd, "sudo ln -sf /usr/share/zoneinfo/Etc/%s /etc/localtime", zoneid.c_str()); system(cmd); - print_info("change timezone success!\n"); + zlog_info(zbt, "change timezone success!"); } std::string GetFileContent(std::string filename, int line) { std::string strFileContent(""); std::ifstream ifileOut(filename.c_str()); - if (ifileOut.is_open()) { //闂佸搫鍊稿ú锝呪枎閵忋倕绠ラ柟鎯х-绾撅拷 - int i = 1; - while (!ifileOut.eof()) { - if (line == 0) { - std::string strTemp(""); - getline(ifileOut, strTemp); - strFileContent += strTemp; - strFileContent += "\r\n"; - } - if (line != 0) { - std::string strTemp(""); - getline(ifileOut, strTemp); - if (line == i) { - strFileContent = strTemp; - break; - } - } - ++i; - } - ifileOut.close(); + if (!ifileOut.is_open()) { + zlog_error(zct, "fail to open:%s", filename.c_str()); + return strFileContent; } + + int i = 1; + while (!ifileOut.eof()) { + if (line == 0) { + std::string strTemp(""); + getline(ifileOut, strTemp); + strFileContent += strTemp; + strFileContent += "\r\n"; + } else { + std::string strTemp(""); + getline(ifileOut, strTemp); + if (line == i) { + strFileContent = strTemp; + break; + } + } + ++i; + } + ifileOut.close(); return strFileContent; } -// BOOST闂佺儵鍋撻崝宥夘敆濠婂牆绀嗘繛鍡樺竾閳ь剙鍟蹇涘箚瑜忕涵鈧俊銈囧Т閻線鎯佹繅鐪曢梺闈╅檮濠㈡ê顭囬崘顔艰Е闁割偆鍠撻妴锟� +// BOOST闂備胶鍎甸崑鎾诲礉瀹ュ鏁嗘繝濠傜墕缁€鍡樼箾閸℃ê绔鹃柍褜鍓欓崯顖氼焽韫囨稑绠氱憸蹇曟兜閳ь剚淇婇妶鍥㈤柣顓濈窔閹焦绻呴惇鏇㈡⒑闂堚晠妾繝銏∶…鍥礃椤旇壈袝闂佸壊鍋嗛崰鎾诲Υ閿燂拷 bool CheckIP(const char *ip) { boost::xpressive::cregex reg_ip = boost::xpressive::cregex::compile("(25[0-4]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[1-9])[.](25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[0-9])[.](25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[0-9])[.](25[0-4]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[1-9])"); @@ -1139,20 +1153,20 @@ bool CheckIP(const char *ip) { bool IsValidMask(std::string mask) { int iRet = -1; - // 闁诲繐绻愰幑鍑遍梺闈╅檮濠㈡ê顭囬崘顔藉仺闁瑰吀璁查崑鎾斥堪閸涱厺鍖栭梺鍛婂笒濡鐣烽崟顒佷氦婵炴垶锚閻撴垿鏌嶉妷锔剧畾婵炴潙妫濋獮鎴﹀閳ュ磭浜� 闂佺偨鍎茬划鍫㈣姳閳哄倹浜ゆ繛鎴灻悡鎴︽煛娴e摜鎽犻柡鍡欏枛閸ㄩ箖鏁撻敓锟� + // 闂佽绻愮换鎰板箲閸戦亶姊洪棃鈺呮婵犮垺锚椤洭宕橀钘変缓闂佺懓鍚€鐠佹煡宕戦幘鏂ュ牚闁告侗鍘洪崠鏍⒑閸涘﹤绗掓俊顐n殔閻g兘宕熼浣锋唉濠电偞鍨堕敋闁绘挻鍨块弻宥夊Ψ閿斿墽鐣惧┑鐐存綑濡繈鐛幋锕€顫呴柍銉ョ-娴滐拷 闂備胶鍋ㄩ崕鑼垝閸垼濮抽柍鍝勫€规禍銈嗙箾閹寸伝顏堟偂閹达附鐓涘ù锝呮憸閹界娀鏌¢崱娆忔灈闁搞劑绠栭弫鎾绘晸閿燂拷 struct in_addr s; iRet = inet_pton(AF_INET, mask.c_str(), &s); - // 闁哄鍎愰崜姘暦閺屻儱绠i柟閭﹀墮椤娀寮堕埡鍌涚叆婵炶鎷�1闂佹寧绋戦惌渚€顢氶埡鍛強閹兼番鍨虹瑧闂佸搫鐗嗛ˇ浼村疾閵夆晜鍎嶉柛鎴瘽闂侀潻闄勫妯侯焽閿燂拷 + // 闂佸搫顦遍崕鎰板礈濮橆剛鏆﹂柡灞诲劚缁狅綁鏌熼柇锕€澧い顐ゅ█瀵爼鍩¢崒娑氬弳濠电偠顕滈幏锟�1闂備焦瀵х粙鎴︽儗娓氣偓椤㈡岸鍩¢崨顓炲挤闁瑰吋鐣崹铏圭懅闂備礁鎼悧鍡浰囨导鏉戠柧闁靛鏅滈崕宥夋煕閹搭厽鐦介梻渚€娼婚梽鍕熆濡警鐒介柨鐕傛嫹 if (iRet == 1) { - // 婵炲濮村ù椋庣礊婢跺瞼纾兼繝濠傚暟閹界喖鏌ら崫鍕偓鍧楀Υ鎼淬垺鍎熼煫鍥ㄧ缁侇噣鏌熺拠鈩冪窔閻犳劗鍠愮粙澶岀磼濡崵鐨婚柣搴㈢⊕椤ㄥ淇婇鐔翠簻闁告繂瀚喊锟� + // 濠电偛顕慨鏉懨规搴g濠㈣泛鐬肩壕鍏肩節婵犲倸鏆熼柟鐣屽枛閺屻倝宕崟顐熷亾閸ф违閹兼番鍨洪崕鐔肩叓閸ャ劎顣茬紒渚囧櫍閺岀喓鎷犻埄鍐獢闁荤姵鍔楅崰鎰矙婢跺瞼纾兼俊顖濆吹閻ㄥ鏌f惔銏⑩姇妞ゃ劌顦版穱濠囶敇閻旂繝绨婚梺鍛婄箓鐎氼喚鍠婇敓锟� unsigned int addr = ntohl(s.s_addr); - // 闁哄鍎愰崜姘暦閸欏鈻旈弶鐐村閻у矂寮堕埡鍌溾槈闁糕晜顨堥埀顒佺⊕椤ㄥ牓顢栨担鍦枖闁跨噦鎷� + // 闂佸搫顦遍崕鎰板礈濮橆剛鏆﹂柛娆忣槺閳绘棃寮堕悙鏉戭棆闁谎冪焸瀵爼鍩¢崒婧炬闂佺硶鏅滈〃鍫ュ焵椤掍胶鈯曟い銊ョ墦椤㈡牗鎷呴崷顓涙灃闂佽法鍣﹂幏锟� std::bitset<32> b((int)addr); std::string strMask = b.to_string(); - // 闂佸搫琚崕鍙夌珶濡崵顩茬€光偓閸愵亞顔掗梺鍛婂笩濞夋盯鎮鸿缁參鏁傞懗顖f船婵炴垶鎼╅崢鎯р枔閿燂拷"01"闂佹寧绋戦懟顖炪€呰瀵ǹ饪伴埀顒傜箔婢跺备鍋撳☉娅亜锕㈤鍫熸櫖鐎光偓閸愭儳娈梺鍝勫鐎涒晛危閹间礁瀚夊璺侯儐濞呭繘鏌i妸銉ヮ仼闁烩剝鐟х槐鏃堝箣閻愯尪绀嬮梺娲诲櫙閹凤拷 + // 闂備礁鎼悮顐﹀磿閸欏鐝舵俊顖濆吹椤╄尙鈧厜鍋撻柛鎰典簽椤旀帡姊洪崨濠傜婵炲鐩幃楦款槾缂侇噮鍙冮弫鍌炴嚄椤栵絾鑸瑰┑鐐村灦閹尖晠宕㈤幆褉鏋旈柨鐕傛嫹"01"闂備焦瀵х粙鎴︽嚐椤栫偑鈧懓顦圭€殿喚枪楗即鍩€椤掑倻绠斿璺哄閸嬫挸鈽夊▍顓т簻閿曘垽顢旈崼鐔告珫閻庡厜鍋撻柛鎰劤濞堫垶姊洪崫鍕仼閻庢稈鏅涘嵄闁归棿绀佺€氬顭跨捄渚剱婵炲懎绻橀弻锝夊Ω閵夈儺浠奸梺鐑╁墲閻熝呮閺冨牆绠i柣鎰蔼缁€瀣⒑濞茶娅欓柟鍑ゆ嫹 return (strMask.find("01") == std::string::npos); } @@ -1178,19 +1192,20 @@ double GetHardDiskFree() { } int getSysIntValue(char *key) { + if (key == NULL) { + zlog_error(zct, "key is NULL"); + return 0; + } FILE *fp = NULL; - int value = 0; - if (key == NULL) return 0; - + int value = 0; fp = fopen(key, "r"); if (fp == NULL) { - printf("Error: could not open %s file\n", key); + zlog_error(zct, "could not open %s file", key); return 1; } fscanf(fp, "%d", &value); fclose(fp); - fp = NULL; return value; } @@ -1261,7 +1276,6 @@ std::string GetSysStatus() { sprintf(key, "/sys/class/thermal/thermal_zone0/temp"); int temp = getSysIntValue(key); - print_info("rateUser = %f,mem_total = %d,mem_free = %d,fMemRate = %f\n", rateUser, mem_total, mem_free, fMemRate); Json::Value jsData; Json::FastWriter fw; jsData["dataNodeGatewayNo"] = GlobalConfig::MacAddr_G; @@ -1277,7 +1291,6 @@ std::string GetSysStatus() { double total = atof(strhardTotal.substr(0, strhardTotal.length() - 1).c_str()); double free = atof(strhardFree.substr(0, strhardFree.length() - 1).c_str()); double use = ((total - free) / total) * 100; - // jsData["hardDiskUse"] = atof(strrateHardUse.substr(0,strrateHardUse.length() - 1)); jsData["hardDiskUse"] = use; jsData["cpuSystemUse"] = rateSys; @@ -1289,22 +1302,24 @@ std::string GetSysStatus() { return strJson; } -// 闁诲繐绻愬Λ妤€鐣烽悢鍝モ枖闁跨噦鎷�16闁哄鏅滅粙鎴﹀春濡ゅ啠鍋撳☉娆樻畼妞ゆ垳鐒﹀顏堫敆娴gǹ绨ユ繛鎴炴尭閹碱偊顢氶鑺ュ劅闁哄啫鍊归悾閬嶆倵濞戞瑯娈欏┑鈽嗗弮瀹曟劙鏁撻敓锟� +// 闂佽绻愮换鎰涘Δ鈧悾鐑芥偄閸濄儮鏋栭梺璺ㄥ櫐閹凤拷16闂佸搫顦弲婊呯矙閹达箑鏄ユ俊銈呭暊閸嬫挸鈽夊▎妯荤暭濡炪倖鍨抽悞锕€顭囬鍫晢濞达絿枪缁ㄣ儲绻涢幋鐐村碍闁圭⒈鍋婇、姘额敊閼恒儱鍔呴梺鍝勫暙閸婂綊鎮鹃柆宥嗗€垫繛鎴炵懐濞堟瑥鈹戦埥鍡楀籍鐎规洘鍔欓弫鎾绘晸閿燂拷 unsigned char hexCharToByte(char c) { if (c >= '0' && c <= '9') return c - '0'; if (c >= 'A' && c <= 'F') return c - 'A' + 10; if (c >= 'a' && c <= 'f') return c - 'a' + 10; - return 0; // 闁诲海鏁婚埀顒佺〒閼归箖鏌¢崘顏勑i柡鍛劦閹啴宕熼銈嗘喕缂備焦顨愮拋锝囨濠靛洦浜ら柡鍌涘缁€鈧�0 + return 0; // 闂佽娴烽弫濠氬焵椤掍胶銆掗柤褰掔畺閺岋繝宕橀鍕戯綁鏌¢崨顐㈠姦闁诡垰鍟村畷鐔碱敃閵堝棙鍠曠紓鍌欑劍椤ㄦ劗鎷嬮敐鍥潟婵犻潧娲︽禍銈夋煛閸屾稑顕滅紒鈧埀锟�0 } -// 闁诲繐楠忛幏锟�16闁哄鏅滅粙鎴﹀春濡ゅ啠鍋撳☉娆樻畼妞ゆ垳鐒︾粙澶愭嚑閼哥數銈梺纭咁嚃濠⑩偓閻犳劗鍠撻埀顒佺⊕椤ㄥ淇婇銏犳瀬闁规鍠氶惌锟� +// 闂佽绻愭蹇涘箯閿燂拷16闂佸搫顦弲婊呯矙閹达箑鏄ユ俊銈呭暊閸嬫挸鈽夊▎妯荤暭濡炪倖鍨抽悞锔剧矙婢舵劖鍤戦柤鍝ユ暩閵堫噣姊虹涵鍜佸殐婵犫懇鍋撻柣鐘冲姉閸犳捇鍩€椤掍胶鈯曟い銊ヮ槹娣囧﹪顢橀姀鐘崇€梺瑙勵問閸犳岸鎯岄敓锟� int hexStringToBytes(const char *hexStr, unsigned char *bytes, size_t bytesSize) { size_t hexLen = strlen(hexStr); if (hexLen % 2 != 0) { - return -1; // Hex闁诲孩绋掗〃鍫ヮ敄娴e湱鈻旈柡灞诲劜濮f劙骞栨潏鍓х暠缂併劏灏欓幏鐘诲Ψ閿斿彨锕傛煕鐎n厼鐓愰柡鍡嫹 + zlog_error(zct, "hexLen:%d is odd", hexLen); + return -1; // Hex闂佽瀛╃粙鎺椼€冮崼銉晞濞达絽婀遍埢鏃堟煛鐏炶鍔滄慨锝嗗姍楠炴牗娼忛崜褏鏆犵紓浣靛姀鐏忔瑩骞忛悩璇参ㄩ柨鏂垮建閿曞倹鐓曢悗锝庡幖閻撴劙鏌¢崱顓熷 } if (bytesSize < hexLen / 2) { - return -1; // 闁诲孩绋掗〃澶嬩繆椤撱垹鏋侀柟娈垮枤閻鏌i妸銉ヮ仼闁靛洤娲ㄦ禍姝岀疀鎼达絿鎲归柣鈩冨笒閸樻牗绂嶉幒鏂哄亾閻熺増璐¢柟鎯у悑濞碱亪顢楁担绋跨哎闂佸憡鑹惧ù宄扳枔閹寸姭鍋撳☉娆樻畽濠碘槄鎷� + zlog_error(zct, "bytesSize:%d < hexLen:%d", bytesSize, hexLen); + return -1; // 闂佽瀛╃粙鎺椼€冩径瀣╃箚妞ゆ挶鍨归弸渚€鏌熷▓鍨灓闁活厼顑夐弻锝夊Ω閵夈儺浠奸梺闈涙搐濞层劍绂嶅宀€鐤€閹艰揪绲块幉褰掓煟閳╁啫绗掗柛妯荤墬缁傚秹骞掗弬鍝勪壕闁荤喓澧楃拹锟犳煙閹冩倯婵炵⒈浜、妤佹媴缁嬭法鍝庨梻浣告啞閼规儳霉瀹勬壋鏋旈柟瀵稿Л閸嬫挸鈽夊▎妯荤暯婵犵妲勯幏锟� } for (size_t i = 0; i < hexLen; i += 2) { @@ -1313,7 +1328,7 @@ int hexStringToBytes(const char *hexStr, unsigned char *bytes, size_t bytesSize) bytes[i / 2] = (highNibble << 4) | lowNibble; } - return 0; // 闂佺懓鐡ㄩ崝鏇熸叏閿燂拷 + return 0; // 闂備胶鎳撻悺銊╁礉閺囩喐鍙忛柨鐕傛嫹 } void stringToHex(const char *str, char *hexStr) { @@ -1330,13 +1345,13 @@ void hexToAscii(const char *hexStr, char *asciiStr) { int i, j = 0; for (i = 0; i < len; i += 2) { - // 闁荤姴娲╅褑銇愰崶銊р枖闁靛牆瀚伴崵瀣倵濞戞瑯娈樻い鎴滅窔閻涱喚鎹勯搹瑙勵啈闂佺ǹ绻戦崕鐓幟归崱娑樼妞ゎ偒鍙€缁€瀣煛娴e摜鎽犻柡鍡嫹 + // 闂佽崵濮村ú鈺咁敋瑜戦妵鎰板炊閵娧€鏋栭梺闈涚墕鐎氫即宕电€n喗鍊垫繛鎴炵懐濞堟ɑ銇勯幋婊呯獢闁绘侗鍠氶幑鍕惞鐟欏嫷鍟堥梻浣呵圭换鎴﹀磿閻撳篃褰掑幢濞戞顔夊銈庡亽閸欌偓缂佲偓鐎n喗鐓涘ù锝呮憸閹界娀鏌¢崱顓熷 int byte; sscanf(&hexStr[i], "%2x", &byte); - // 闁诲繐绻愬Λ娆撳汲閿濆鏋侀柟宄版湰缁侇噣鏌熺拠鈩冪窔閻犳劗鍠撻埀顒傛暩閹虫挾鑺遍弻銉﹀剭闁告垶鈹嶤II闁诲孩绋掗〃鍫ヮ敄閿燂拷 + // 闂佽绻愮换鎰涘▎鎾虫辈闁挎繂顦弸渚€鏌熷畡鐗堟拱缂佷緡鍣i弻鐔烘嫚閳╁啰绐旈柣鐘冲姉閸犳捇鍩€椤掑倹鏆╅柟铏尵閼洪亶寮婚妷锕€鍓梺鍛婂灦閳瑰钉II闂佽瀛╃粙鎺椼€冮崼銉晞闁跨噦鎷� asciiStr[j++] = (char)byte; } - // 濠电儑缍€椤曆勬叏閻愮鍋撳☉娆樻畼妞ゆ垳鐒︾粙澶愭嚑椤掑倹灏濋梺鍝勵槺閸犳捇顢栭敓锟� + // 婵犵數鍎戠紞鈧い鏇嗗嫭鍙忛柣鎰暯閸嬫挸鈽夊▎妯荤暭濡炪倖鍨抽悞锔剧矙婢舵劖鍤戞い鎺戝€圭亸婵嬫⒑閸濆嫷妲洪柛鐘虫崌椤㈡牠鏁撻敓锟� asciiStr[j] = '\0'; } @@ -1383,22 +1398,23 @@ void swap(char *data) { int OpenWatchDog() { int fd = -1; InitGpio(GlobalConfig::GPIO_G.hardWatchDog, 1); - gpio_set(GlobalConfig::GPIO_G.hardWatchDog, 1); //婵°倕鍊归…鍥极閹间胶宓侀柧蹇e亝缁犳帡鏌℃笟濠冨 + gpio_set(GlobalConfig::GPIO_G.hardWatchDog, 1); //濠德板€曢崐褰掆€﹂崶顒€鏋侀柟闂磋兌瀹撲線鏌ц箛锝呬簼缂佺姵甯¢弻鈩冪瑹婵犲啫顏� if (0 > (fd = open("/dev/watchdog", O_WRONLY))) { - print_info("Failed:Open /dev/watchdog"); + zlog_error(zbt, "fail to open /dev/watchdog"); } return fd; } int WriteWatchDog(int fd) { if (1 != write(fd, "0", 1)) { - print_info("Failed:feeding watchdog"); + zlog_error(zbt, "fail to feed watchdog"); + return 1; } + return 0; } -int CloseWatchDog(int fd) { close(fd); } -string TrimStringLeft(const char *szData, const char *szTargets) { - string strData = szData; +std::string TrimStringLeft(const char *szData, const char *szTargets) { + std::string strData = szData; int nPos = 0; nPos = strData.find(szTargets); while (nPos == 0) { @@ -1408,8 +1424,8 @@ string TrimStringLeft(const char *szData, const char *szTargets) { return strData; } -string TrimStringRight(const char *szData, const char *szTargets) { - string strData = szData; +std::string TrimStringRight(const char *szData, const char *szTargets) { + std::string strData = szData; int nPos = 0; nPos = strData.rfind(szTargets); while ((nPos == (int)(strData.size() - 1)) && (nPos >= 0)) { @@ -1419,9 +1435,9 @@ string TrimStringRight(const char *szData, const char *szTargets) { return strData; } -string GetOneContent(const char *szData, int nRow, const char *szSeparate) { - string strParam = ""; - string strTemp = szData; +std::string GetOneContent(const char *szData, int nRow, const char *szSeparate) { + std::string strParam = ""; + std::string strTemp = szData; int nFirstPos = -1; for (int i = 0; i < nRow; i++) { nFirstPos = strTemp.find(szSeparate, nFirstPos + 1); @@ -1434,32 +1450,32 @@ string GetOneContent(const char *szData, int nRow, const char *szSeparate) { } else { strParam = strTemp.substr(nFirstPos + 1, nSecondPos - nFirstPos - 1); } - - { - strParam = TrimStringRight(strParam.c_str(), " "); - strParam = TrimStringLeft(strParam.c_str(), " "); - } + + strParam = TrimStringRight(strParam.c_str(), " "); + strParam = TrimStringLeft(strParam.c_str(), " "); return strParam.c_str(); } int set_opt(int fd, int nSpeed, int nBits, char nEvent, int nStop) { + zlog_info(zbt, "[set_opt] speed:%d, bits:%d, event:%c, stop:%d", nSpeed, nBits, nEvent, nStop); struct termios newtio, oldtio; - if (tcgetattr(fd, &oldtio) != 0) { //濠碘槅鍋€閸嬫挻绻涢弶鎴剮閻熸洖妫濆畷锝夋晲閸℃锕傛煕濮樺墽鐣辩憸鐗堢叀閹粙鏁撻敓锟� - perror("SetupSerial 1"); + if (tcgetattr(fd, &oldtio) != 0) { //婵犵妲呴崑鈧柛瀣尰缁绘盯寮堕幋顓炲壆闁荤喐娲栧Λ婵嗙暦閿濆鏅查柛鈩冾焺閿曞倹鐓曟慨妯哄⒔閻h京鎲搁悧鍫㈠弨闁诡喕绮欓弫鎾绘晸閿燂拷 + zlog_error(zbt, "tcgetattr fail"); return -1; } bzero(&newtio, sizeof(newtio)); newtio.c_cflag |= CLOCAL | CREAD; newtio.c_cflag &= ~CSIZE; - switch (nBits) //闁荤姳绀佹晶浠嬫偪閸℃稑鏋侀柣妤€鐗嗙粊锕€霉閿濆繑瀚� - { + switch (nBits) { case 7: newtio.c_cflag |= CS7; break; case 8: newtio.c_cflag |= CS8; break; + default: + zlog_error(zbt, "invalid nbits:%d", nBits); + break; } - switch (nEvent) //闁荤姳绀佹晶浠嬫偪閸℃あ娑㈠焵椤掍緡娈介悘鐐跺亹缁夛拷 - { + switch (nEvent) { case 'O': newtio.c_cflag |= PARENB; newtio.c_cflag |= PARODD; @@ -1471,9 +1487,12 @@ int set_opt(int fd, int nSpeed, int nBits, char nEvent, int nStop) { newtio.c_cflag &= ~PARODD; break; case 'N': newtio.c_cflag &= ~PARENB; break; + default: + zlog_error(zbt, "invalid event:%c", nEvent); + break; } - switch (nSpeed) //闁荤姳绀佹晶浠嬫偪閸℃せ鏋栭柕蹇嬪灩椤ユ鏌e⿰鎰 + switch (nSpeed) //闂佽崵濮崇粈浣规櫠娴犲鍋柛鈩冦仜閺嬫牠鏌曡箛瀣仼妞ゃ儲顨婇弻锝呪堪閹邦厼顏� { case 2400: cfsetispeed(&newtio, B2400); @@ -1498,46 +1517,51 @@ int set_opt(int fd, int nSpeed, int nBits, char nEvent, int nStop) { default: cfsetispeed(&newtio, B9600); cfsetospeed(&newtio, B9600); + zlog_error(zbt, "invalid speed:%d, use B9600", nSpeed); break; } - if (nStop == 1) //闁荤姳绀佹晶浠嬫偪閸℃稑纾绘繝濠傚閸撴儳霉閿濆繑瀚� + if (nStop == 1) //闂佽崵濮崇粈浣规櫠娴犲鍋柛鈩冪☉绾剧粯绻濇繝鍌氼伌闁告挻鍎抽湁闁挎繂绻戠€氾拷 newtio.c_cflag &= ~CSTOPB; else if (nStop == 2) newtio.c_cflag |= CSTOPB; newtio.c_cc[VTIME] = 0; newtio.c_cc[VMIN] = 0; tcflush(fd, TCIFLUSH); - if ((tcsetattr(fd, TCSANOW, &newtio)) != 0) //闁荤姳绀佹晶浠嬫偪閸℃鈻旈柟缁樺笒缂嶆捇鏌涘▎蹇撯偓褰掑汲閿燂拷 - { - perror("com set error"); + if ((tcsetattr(fd, TCSANOW, &newtio)) != 0) { + zlog_error(zbt, "tcsetattr fail"); return -1; } return 0; } -int getcsq() { +int getcsq() { int ret = 0; char csq[128] = {0}; - int fd = 0, sig = 0; if ((fd = open("/dev/ttyUSB2", O_RDWR | O_NOCTTY | O_NDELAY)) < 0) { - // LOG_ERROR("open %s is FAILED\n\n","/dev/ttyUSB2"); + zlog_error(zct, "fail to open /dev/ttyUSB2"); + return 1; } else { set_opt(fd, 9600, 8, 'N', 1); } set_opt(fd, 115200, 8, 'N', 1); - // write(fd,"AT+QENG=\"servingcell\"\r\n",27);//閻庢鍠掗崑鎾绘煕濮樼厧鐏犻柣锝囧亾閹峰懘鏁撻敓锟� - write(fd, "AT+CSQ", 6); + ret = write(fd, "AT+CSQ", 6); + if (ret < 0) { + zlog_error(zct, "fail to write AT+CSQ"); + close(fd); + return 1; + } sleep(1); unsigned char rbuf[128] = {0x00}; - int len = read(fd, rbuf, sizeof(rbuf)); // 闂侀潻璐熼崝瀣啺閸℃稑鐭楅柨婵嗙墱閸ゃ垽鏌涜箛瀣姎闁烩姍鍛當闁挎棁鍎婚々锟� - - print_info("rbuf = %s,len = %d\n", rbuf, len); + int len = read(fd, rbuf, sizeof(rbuf)); // 闂備線娼荤拹鐔煎礉鐎n剚鍟洪柛鈩冪☉閻鏌ㄥ┑鍡欏⒈闁搞們鍨介弻娑滅疀鐎n亜濮庨梺鐑╁閸涱垳鐣堕梺鎸庢閸庡銆呴敓锟� + close(fd); + zlog_info(zct, "rbuf = %s,len = %d", rbuf, len); sleep(1); if (len < 0) { - print_info("Can't get /dev/ttyUSBx Serial Port data!\n"); + zlog_error(zct, "Can't get /dev/ttyUSBx Serial Port data!"); + return 1; } const char *str2 = "+QENG: "; @@ -1545,15 +1569,13 @@ int getcsq() { if (pdata) strncpy(csq, pdata + 7, sizeof(csq)); else { + zlog_error(zct, "fail to find +QENG: "); return -1; } - // printf("SIM-CSQ: %s\n", csq); GlobalConfig::NetStatus = GetOneContent(csq, 1, ","); string signal = GetOneContent(csq, 13, ","); GlobalConfig::NetSignal = atoi(signal.c_str()); - print_info("NetStatus = %s,NetSignal = %d\n", GlobalConfig::NetStatus.c_str(), GlobalConfig::NetSignal); - - close(fd); + zlog_info(zct, "NetStatus = %s,NetSignal = %d", GlobalConfig::NetStatus.c_str(), GlobalConfig::NetSignal); return atoi(signal.c_str()); } @@ -1567,9 +1589,10 @@ void IniReadValue(char *section, char *key, char *val, const char *file) { bool bFoundKey = false; fp = fopen(file, "r"); if (fp == NULL) { - printf("%s: Opent file %s failed.\n", __FILE__, file); + zlog_error(zbt, "fail to open %s", file); return; } + while (feof(fp) == 0) { memset(lineContent, 0, LINE_CONTENT_MAX_LEN); fgets(lineContent, LINE_CONTENT_MAX_LEN, fp); @@ -1611,35 +1634,32 @@ void IniReadValue(char *section, char *key, char *val, const char *file) { break; } } - if (!bFoundSection) { - printf("No section = %s\n", section); - } else if (!bFoundKey) { - printf("No key = %s\n", key); - } fclose(fp); + if (!bFoundSection) { + zlog_error(zbt, "No section = %s", section); + } else if (!bFoundKey) { + zlog_error(zbt, "No key = %s", key); + } } int readStringValue(const char *section, char *key, char *val, const char *file) { char sect[SECTION_MAX_LEN]; - // printf("section = %s, key = %s, file = %s\n", section, key, file); + zlog_info(zct, "section = %s, key = %s, file = %s", section, key, file); if (section == NULL || key == NULL || val == NULL || file == NULL) { - printf("%s: input parameter(s) is NULL!\n", __func__); + zlog_error(zct, "input parameter, section:%s, key:%s, val:%s, file:%s exist NULL", section, key, val, file); return -1; } memset(sect, 0, SECTION_MAX_LEN); sprintf(sect, "[%s]", section); - // printf("reading value...\n"); IniReadValue(sect, key, val, file); - return 0; } int readIntValue(const char *section, char *key, const char *file) { char strValue[STRVALUE_MAX_LEN]; memset(strValue, '\0', STRVALUE_MAX_LEN); - if (readStringValue(section, key, strValue, file) != 0) { - printf("%s: error", __func__); + if (readStringValue(section, key, strValue, file) != 0) { return 0; } return (atoi(strValue)); @@ -1660,9 +1680,10 @@ void IniWriteValue(const char *section, char *key, char *val, const char *file) n = sprintf(strWrite, "%s=%s\n", key, val); fp = fopen(file, "r+"); if (fp == NULL) { - printf("%s: Opent file %s failed.\n", __FILE__, file); + zlog_error(zct, "fail to open:%s", file); return; } + while (feof(fp) == 0) { memset(lineContent, 0, LINE_CONTENT_MAX_LEN); fgets(lineContent, LINE_CONTENT_MAX_LEN, fp); @@ -1692,46 +1713,45 @@ void IniWriteValue(const char *section, char *key, char *val, const char *file) break; } } - if (!bFoundSection) { - printf("No section = %s\n", section); - } else if (!bFoundKey) { - printf("No key = %s\n", key); - } fclose(fp); + if (!bFoundSection) { + zlog_error(zct, "No section = %s", section); + } else if (!bFoundKey) { + zlog_error(zct, "No key = %s", key); + } } int writeStringVlaue(const char *section, char *key, char *val, const char *file) { char sect[SECTION_MAX_LEN]; - // printf("section = %s, key = %s, file = %s\n", section, key, file); + zlog_debug(zct, "section = %s, key = %s, file = %s\n", section, key, file); if (section == NULL || key == NULL || val == NULL || file == NULL) { - printf("%s: input parameter(s) is NULL!\n", __func__); + zlog_error(zct, "input parameter(s) is NULL!"); return -1; } - memset(sect, '\0', SECTION_MAX_LEN); + memset(sect, 0, SECTION_MAX_LEN); sprintf(sect, "[%s]", section); IniWriteValue(sect, key, val, file); } int writeIntValue(const char *section, char *key, int val, const char *file) { char strValue[STRVALUE_MAX_LEN]; - memset(strValue, '\0', STRVALUE_MAX_LEN); + memset(strValue, 0, STRVALUE_MAX_LEN); sprintf(strValue, "%d", val); - writeStringVlaue(section, key, strValue, file); } int getDiskInfo(char *diskTotal, char *diskFree) { DISK diskInfo; - /* 1.闂佸吋鍎抽崲鑼躲亹閿燂拷/home/婵炴垶鎸搁澶婎焽娴煎瓨鍎嶉柛鏇ㄥ厴閸嬫挾鎷犲顔兼櫍闂備浇顕滈幏锟� */ + /* 1.闂備礁鍚嬮崕鎶藉床閼艰翰浜归柨鐕傛嫹/home/濠电偞鍨堕幐鎼侇敄婢跺⿴鐒藉ù鐓庣摠閸庡秹鏌涢弴銊ュ幋闁稿鎸鹃幏鐘差啅椤斿吋娅嶉梻鍌欐祰椤曟粓骞忛敓锟� */ statfs("/", &diskInfo); - unsigned long long blocksize = diskInfo.f_bsize; //濠殿噯绲界换瀣煂濞撲康ock闂備焦褰冮懟顖溾偓鍨耿瀹曘儵顢曢妶鍡欐殸闁诲孩绋掗〃澶嬩繆椤撱垹鏋侀柨鐕傛嫹 - unsigned long long totalsize = blocksize * diskInfo.f_blocks; //闂佽鍓濆畷闈涒枔閹寸姭鍋撳☉娆樻畽濠碘槅鍙冨顐ゆ暜椤斿墽顦甪_blocks婵炴垶鎸鹃鐚痮ck闂佹眹鍔岀€氼參寮抽悢鍏煎剮闁跨噦鎷� + unsigned long long blocksize = diskInfo.f_bsize; //婵犳鍣徊鐣屾崲鐎n喗鐓傛繛鎾插悍ock闂傚倷鐒﹁ぐ鍐嚐椤栨壕鍋撻崹顐€跨€规洏鍎甸、鏇㈠Χ閸℃瑦娈搁梺璇插缁嬫帡銆冩径瀣╃箚妞ゆ挶鍨归弸渚€鏌ㄩ悤鍌涘 + unsigned long long totalsize = blocksize * diskInfo.f_blocks; //闂備浇顕栭崜婵嗙暦闂堟稈鏋旈柟瀵稿Л閸嬫挸鈽夊▎妯荤暯婵犵妲呴崣鍐嚕椤愩倖鏆滄い鏂垮⒔椤︾敧_blocks濠电偞鍨堕幐楣冾敋閻氱棶ck闂備焦鐪归崝宀€鈧凹鍙冨鎶芥偄閸忕厧鍓梺璺ㄥ櫐閹凤拷 //printf("Total_size=%llu B =%llu KB =%llu MB = %llu GB\n",\ totalsize,totalsize>>10,totalsize>>20, totalsize>>30); - /* 2.闂佸吋鍎抽崲鑼躲亹閸ャ劎鈻旈柍褜鍓氱粙澶屸偓锝庝簻閳锋牕霉閿濆棛鎳呴柍瑙勭墵濮婂寮堕幋鐐差伅闂佸憡鐟崹鎶藉极閵堝洨鐭氭繛宸簼閿涚喖鏌i妸銉ヮ仼闁靛洤娲ㄦ禍鎼佹晸閿燂拷 */ - unsigned long long freeDisk = diskInfo.f_bfree * blocksize; //闂佸憡鎸撮弲娆戠礊閹寸姷鐭氭繛宸簼閿涚喖鏌i妸銉ヮ仼闁靛洤娲ㄦ禍鎼佹晸閿燂拷 - unsigned long long availableDisk = diskInfo.f_bavail * blocksize; //闂佸憡鐟崹鎶藉极閵堝洨鐭氭繛宸簼閿涚喎顭块崼鍡楀暟濮o拷 + /* 2.闂備礁鍚嬮崕鎶藉床閼艰翰浜归柛銉e妿閳绘棃鏌嶈閸撴氨绮欐径灞稿亾閿濆簼绨婚柍閿嬬墪闇夐柨婵嗘閹冲懘鏌嶇憴鍕⒌婵﹤顭峰鍫曞箣閻愬樊浼呴梻浣告啞閻燁垶宕归幎钘夋瀬闁靛牆娲ㄩ惌姘箾瀹割喕绨奸柨娑氬枛閺岋綁濡搁妷銉患闂侀潧娲ゅú銊︾閹间焦鏅搁柨鐕傛嫹 */ + unsigned long long freeDisk = diskInfo.f_bfree * blocksize; //闂備礁鎲¢幐鎾疾濞嗘垹绀婇柟瀵稿Х閻碍绻涘顔荤凹闁挎稓鍠栭弻锝夊Ω閵夈儺浠奸梺闈涙搐濞层劍绂嶉幖浣规櫢闁跨噦鎷� + unsigned long long availableDisk = diskInfo.f_bavail * blocksize; //闂備礁鎲¢悷顖炲垂閹惰棄鏋侀柕鍫濇川閻碍绻涘顔荤凹闁挎稓鍠庨…鍧楀醇閸℃鏆熸慨锝忔嫹 //printf("Disk_free=%llu MB =%llu GB Disk_available=%llu MB = %llu GB\n",\ freeDisk>>20,freeDisk>>30,availableDisk>>20, availableDisk>>30); sprintf(diskTotal, "%llu", totalsize >> 20); @@ -1739,104 +1759,16 @@ int getDiskInfo(char *diskTotal, char *diskFree) { return 1; } -bool NetIsOk() { - double rtt; - struct hostent *host; - struct protoent *protocol; - int i, recv_status; - -#ifdef _USE_DNS //婵犵鈧啿鈧綊鎮樻径灞稿亾鐟欏嫮鐓紒鐘靛枔閹风娀濡烽妷褎娈查梺鎸庣☉閼活垶宕归鐐茬煑妞ゆ牗鐟ょ花鏉棵归敐鍫熺《闁轰降鍊濆畷娲偄闁垮鈧娊寮堕埡鍌滎灱妞ゃ垺鍨垮畷姘跺Χ閸℃鍔风紓鍌氬暞閸ㄥ湱鍒掗懜鍨氦闁绘劖娼欐径宥夋煥濞戞ê顨欑紒韬插劜娣囧﹪宕愰崓绺�.baidu.com - /* 闁荤姳绀佹晶浠嬫偪閸℃稒鍎庢い鏃傚亾閻i亶鏌涢敂鑺ョ凡婵炵厧鍟粚閬嶅焺閸愌呯 */ - char hostname[32]; - sprintf(hostname, "%s", "www.baidu.com") bzero(&dest_addr, sizeof(dest_addr)); - dest_addr.sin_family = AF_INET; - - if ((host = gethostbyname(hostname)) == NULL) { - LOG_ERROR("[NetStatus] error : Can't get serverhost info!\n"); - return false; - } - - bcopy((char *)host->h_addr, (char *)&dest_addr.sin_addr, host->h_length); -#else //婵犵鈧啿鈧綊鎮樻径瀣枖鐎广儱瀚埢蹇涙煟閳ь剙濡奸柣鎾寸懇瀹曘儱顓艰箛鏇狀槷闂佸憡甯楅悷銉ㄣ亹瑜旈幊妤呮偩鐏炵偓娈p闂侀潻闄勫妯侯焽閸愵喗鍎庨悗娑櫭径宥夋煕濞嗘劕鐏撮柍褜鍏涘銉絤p闂佸憡鐗曢幏鎴犳閿燂拷 - dest_addr.sin_addr.s_addr = inet_addr(GlobalConfig::ServerIP.c_str()); -#endif - - if ((sockfd = socket(AF_INET, SOCK_RAW, IPPROTO_ICMP)) < 0) { /* 闂佸憡甯楃粙鎴犵磽閹捐鍌ㄩ柣鏂款殠濞兼“CMP婵犻潧鍊归〃鍡欐暜鐎电硶鍋撳☉铏 */ - LOG_ERROR("[NetStatus] error : socket %d\n", sockfd); - return false; - } - - int iFlag; - if (iFlag = fcntl(sockfd, F_GETFL, 0) < 0) { - LOG_ERROR("[NetStatus] error : fcntl(sockfd,F_GETFL,0)"); - _CloseSocket(); - return false; - } - iFlag |= O_NONBLOCK; - if (iFlag = fcntl(sockfd, F_SETFL, iFlag) < 0) { - LOG_ERROR("[NetStatus] error : fcntl(sockfd,F_SETFL,iFlag )"); - _CloseSocket(); - return false; - } - print_info("================NetIsOk check=============\n"); - pid = getpid(); - for (i = 0; i < MAX_NO_PACKETS; i++) { - if (send_packet(i, sendpacket) < 0) { - LOG_ERROR("[NetStatus] error : send_packet"); - _CloseSocket(); - return false; - } - - if (recv_packet(i, recvpacket) > 0) { - _CloseSocket(); - return true; - } - } - _CloseSocket(); - return false; -} - -int send_packet(int pkt_no, char *sendpacket) { - int packetsize; - packetsize = pack(pkt_no, sendpacket); - gettimeofday(&tvsend, NULL); - print_info("================NetIsOk send_packet=============\n"); - if (sendto(sockfd, sendpacket, packetsize, 0, (struct sockaddr *)&dest_addr, sizeof(dest_addr)) < 0) { - LOG_ERROR("[NetStatus] error : sendto error"); - return -1; - } - return 1; -} - -int pack(int pkt_no, char *sendpacket) { - int i, packsize; - struct icmp *icmp; - struct timeval *tval; - icmp = (struct icmp *)sendpacket; - icmp->icmp_type = ICMP_ECHO; //闁荤姳绀佹晶浠嬫偪閸℃瑧灏甸悹鍥皺閳ь剟鏀辩粙澶屽姬閹垫湚P闁荤姴娲弨閬嶆儑娴兼潙绠柕澶涢檮閻庯拷 - icmp->icmp_code = 0; - icmp->icmp_cksum = 0; - icmp->icmp_seq = pkt_no; - icmp->icmp_id = pid; //闁荤姳绀佹晶浠嬫偪閸℃哎浜归柟鎯у暱椤ゅ懘寮堕埡鍌溿€掗柍鑽ょ“D婵炴垶鎸鹃惁鈥揗P闂佸搫绉村ú銊╁Φ濮樿京绠旈柨鐕傛嫹 - packsize = ICMP_HEADSIZE + sizeof(struct timeval); - tval = (struct timeval *)icmp->icmp_data; - gettimeofday(tval, NULL); - icmp->icmp_cksum = cal_chksum((unsigned short *)icmp, packsize); - return packsize; -} - unsigned short cal_chksum(unsigned short *addr, int len) { int nleft = len; int sum = 0; unsigned short *w = addr; unsigned short answer = 0; - while (nleft > 1) //闂佺娉涢〃鎵朚P闂佺缈伴崕閬嶅Φ閺冣偓缁傚秴鐣濋崘顏嗩啋闂佸憡甯熼崺鏍汲閻旂厧绠叉い鏃囶唺缁拷2闁诲孩绋掗〃澶嬩繆椤撶喓鈻旈柛婵嗗缁€瀣归敐鍛ら柣銈庡櫍瀹曟繈鎮╅悜妯洪棷闂佸搫顧€閹凤拷 - { + while (nleft > 1) { sum += *w++; nleft -= 2; } - if (nleft == 1) //闂佸吋妞垮绗碝P闂佺缈伴崕閬嶅Φ閺冣偓缁嬪宕崟鍨杽闂佽桨绶氶。锕傛煂濠婂應鍋撳☉娆樻畽濠碘槄鎷�,婵炴潙鍚嬮懝鐐櫠閹稿海鈻旈悗锝庡亝娴犳﹢鏌涘顒傚嚬缂佹柨顕埀顒佺⊕椤ㄥ淇婇敓锟�.闂佺娉涢敃銉ャ€掗崼鏇炶Е閹煎瓨绻勯鍗炩槈閹垮啩绨婚柣鈯欏洦鍤嶉柛灞剧箥濞兼帒鈽夐幘瑙勩€冪紒鏂跨摠缁嬪鏁撻敓锟�2闁诲孩绋掗〃澶嬩繆椤撱垹鏋侀柣妤€鐗嗙粊锕傛煟閵娿儱顏柣婵愬枤閳ь剚绋掗〃澶嬩繆閿燂拷,闁哄鏅滈悷銈夋煂閿燂拷2闁诲孩绋掗〃澶嬩繆椤撱垹鏋侀柣妤€鐗嗙粊锕傛煟閵娿儱顏х紓宥呮閳ь剚绋掗〃澶嬩繆椤撶喓鈻旈柨鐕傛嫹0,缂傚倷缍€閸涱垱鏆扮紓渚€纭搁崹鐗堟叏閿燂拷 - { + if (nleft == 1) { *(unsigned char *)(&answer) = *(unsigned char *)w; sum += answer; } @@ -1846,110 +1778,49 @@ unsigned short cal_chksum(unsigned short *addr, int len) { return answer; } -int recv_packet(int pkt_no, char *recvpacket) { - int n; - fd_set rfds; - FD_ZERO(&rfds); - FD_SET(sockfd, &rfds); - signal(SIGALRM, timeout); - unsigned int fromlen = sizeof(recv_addr); - alarm(MAX_WAIT_TIME); - print_info("================NetIsOk recv_packet=============\n"); - while (1) { - select(sockfd + 1, &rfds, NULL, NULL, NULL); - if (FD_ISSET(sockfd, &rfds)) { - if ((n = recvfrom(sockfd, recvpacket, PACKET_SIZE, 0, (struct sockaddr *)&recv_addr, &fromlen)) < 0) { - print_info("================NetIsOk recvfrom=============errno = %d\n", errno); - if (errno == EINTR) { - return -1; - LOG_ERROR("recvfrom error"); - return -2; - } - } - } - gettimeofday(&tvrecv, NULL); - if (unpack(pkt_no, recvpacket, n) == -1) continue; - return 1; - } -} - -int unpack(int cur_seq, char *buf, int len) { - int iphdrlen; - struct ip *ip; - struct icmp *icmp; - ip = (struct ip *)buf; - iphdrlen = ip->ip_hl << 2; //濠殿噣鈧稑鐗歱闂佺缈伴崕閬嶅Φ閺冨牊鈷愰柛顭戝枛椤旓拷,闂佸憡顨堥悧鐜撮梺纭呯堪閸庨亶濡甸弮鍫熷剭闁告洦鍨卞В鎰板箹鏉堥箖妾柣鏍电秬缁犳稑顪冮崜褜鍟�4 - icmp = (struct icmp *)(buf + iphdrlen); //闁烩剝甯掗敃銊ф崲閸嶇幋闂佺缈伴崕閬嶅Φ閿燂拷,闂佸湱枪濞层倝骞冨ǎ娆矼P闂佺缈伴崕閬嶅Φ閿燂拷 - len -= iphdrlen; // ICMP闂佺缈伴崕閬嶅Φ閺冨牆鐭楀┑鍌氳埗MP闂佽桨鑳舵晶妤€鐣垫笟鈧獮搴ㄥΨ瑜庨悾閬嶆煙椤掆偓椤兘寮虫潏銊﹀劅闁跨噦鎷� - if (len < 8) return -1; - if ((icmp->icmp_type == ICMP_ECHOREPLY) && (icmp->icmp_id == pid) && (icmp->icmp_seq == cur_seq)) - return 0; - else - return -1; -} - -void timeout(int signo) { - LOG_ERROR("Request Timed Out\n"); - _CloseSocket(); -} - -void tv_sub(struct timeval *out, struct timeval *in) { - if ((out->tv_usec -= in->tv_usec) < 0) { - --out->tv_sec; - out->tv_usec += 1000000; - } - out->tv_sec -= in->tv_sec; -} - -void _CloseSocket() { - close(sockfd); - sockfd = 0; -} - int socketHeart(const char *pSendData) { - print_info("socketHeart\n"); - int sockfd; // Socket闂佸搫鍊稿ú锝呪枎閵忋倕绠甸煫鍥ㄨ壘閻楁氨绱掑Δ瀣 - struct sockaddr_in serverAddr {}; // Server闂侀潻闄勫妯侯焽閸愵亞纾奸柟鎯ь嚟閳ь剦鍨遍幏鍛存晸閿燂拷 + zlog_info(zct, "socketHeart"); + int sockfd; // Socket闂備礁鎼崐绋棵洪敐鍛瀻闁靛繈鍊曠粻鐢哥叓閸ャ劏澹橀柣妤佹皑缁辨帒螖鐎n偄顏� + struct sockaddr_in serverAddr {}; // Server闂備線娼婚梽鍕熆濡警鐒介柛鎰典簽绾惧ジ鏌熼幆褜鍤熼柍褜鍓﹂崹閬嶅箯閸涘瓨鏅搁柨鐕傛嫹 - // 闂佸憡甯楃粙鎴犵磽閹绘籍cket + // 闂備礁鎲$敮妤冪矙閹寸姷纾介柟缁樼睄cket if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1) { - std::cerr << "Failed to create socket." << std::endl; + zlog_error(zct, "Failed to create socket."); return 1; } - // 闁荤姳绀佹晶浠嬫偪閸濈垁rver闂侀潻闄勫妯侯焽閸愨晝鈹嶉柍鈺佸暕缁憋拷 + // 闂佽崵濮崇粈浣规櫠娴犲鍋柛婵堝瀬rver闂備線娼婚梽鍕熆濡警鐒介柛鎰ㄦ櫇閳瑰秹鏌嶉埡浣告殨缂佹唻鎷� serverAddr.sin_family = AF_INET; - serverAddr.sin_port = htons(18393); // TCP婵帗绋掓灙妞ゆ捁宕电划鈺咁敍濮橆剛绋夐梺鍛婄懆濞撳湱鎷归敓锟�80 - inet_pton(AF_INET, /*GlobalConfig::ServerIP.c_str()*/ "192.168.1.147", &serverAddr.sin_addr); + serverAddr.sin_port = htons(18393); // TCP濠殿喗甯楃粙鎺撶仚濡炪倖鎹佸畷鐢靛垝閳哄拋鏁嶆慨姗嗗墰缁嬪姊洪崨濠勬噯婵炴挸婀遍幏褰掓晸閿燂拷80 + inet_pton(AF_INET, "192.168.1.147", &serverAddr.sin_addr); - // 闁哄鏅濋崑鐐垫暜閹绢喖绀嗛柣褎绱磖ver + // 闂佸搫顦弲婵嬪磻閻愬灚鏆滈柟缁㈠枛缁€鍡涙煟瑜庣槐纾杤er if (connect(sockfd, reinterpret_cast(&serverAddr), sizeof(serverAddr)) == -1) { - std::cerr << "Failed to connect to the server." << std::endl; + zlog_error(zct, "Failed to connect to the server."); close(sockfd); return 1; } - // 闂佸憡鐟﹂崹鍧楀焵椤戣法顦︾紒鍝勫⒔閹瑰嫰鎼归锝嗩啀闂佺顕栭崰鏍偓纰夋嫹 + // 闂備礁鎲¢悷锕傚垂閸ф鐒垫い鎴f硶椤︼妇绱掗崫鍕挃闁圭懓瀚伴幖褰掝敃閿濆棭鍟€闂備胶顢婇鏍窗閺嶎偀鍋撶喊澶嬪 const char heartbeat[] = "Heartbeat"; ssize_t bytesSent = send(sockfd, pSendData, strlen(pSendData), MSG_NOSIGNAL); if (bytesSent == -1) { - std::cerr << "Failed to send heartbeat packet." << std::endl; + zlog_error(zct, "Failed to send heartbeat packet."); close(sockfd); return 1; } else if (static_cast(bytesSent) != strlen(pSendData)) { - std::cerr << "Partially sent heartbeat packet." << std::endl; + zlog_error(zct, "Partially sent heartbeat packet."); close(sockfd); return 1; } - // 闂佺ǹ绻戞繛濠偽涚喊鈺玞ket闁哄鏅濋崑鐐垫暜閿燂拷 + // 闂備胶枪缁绘垶绻涙繝鍋芥稓鍠婇埡鐜瀔et闂佸搫顦弲婵嬪磻閻愬灚鏆滈柨鐕傛嫹 close(sockfd); - return 0; } -// Ping闂佸憡鍨兼慨銈夊汲閻斿吋鏅悗鍦交meout婵炴垶鎹囩紓姘辩矓閹绢喖绫嶉柤绋跨仛椤ρ囨⒒閸屾繃褰х紒杈ㄧ箞瀹曪繝寮村杈┬㈤梺鍝勫濡,10000 濠殿噯缍嗛崑鍡涳綖閿燂拷=10 缂備礁顧€閹凤拷 -//闂佺懓鐡ㄩ崝鏇熸叏濞戙垺鏅慨妯虹-缁犳煡鏌涢妷顖涘0闂佹寧绋戦懟顖炲Φ閹寸姵瀚婚柕澶樺灣缁愭寮堕埡鍌涚叆婵炶鎷�1闂佺鎻幏锟�-1 +// Ping闂備礁鎲¢崹鍏兼叏閵堝姹查柣鏂垮悑閺咁剟鎮楅崷顓炰氦meout濠电偞鍨堕幑鍥╃磽濮樿京鐭撻柟缁㈠枛缁秹鏌ょ粙璺ㄤ粵妞は佸洦鈷掗柛灞剧箖瑜把呯磼鏉堛劎绠炵€规洩绻濆鏉戭潩鏉堚敩銏ゆ⒑閸濆嫬顏俊顐ゎ攳,10000 婵犳鍣紞鍡涘磻閸℃冻缍栭柨鐕傛嫹=10 缂傚倷绀侀¨鈧柟鍑ゆ嫹 +//闂備胶鎳撻悺銊╁礉閺囩喐鍙忔繛鎴欏灪閺咁剚鎱ㄥΟ铏癸紞缂佺姵鐓¢弻娑㈠Ψ椤栨稑顏�0闂備焦瀵х粙鎴︽嚐椤栫偛桅闁瑰濮电€氬鏌曟径妯虹仯缂佹劖顨婂鍫曞煛閸屾稓鍙嗗┑鐐额嚋閹凤拷1闂備胶顢婇幓顏堝箯閿燂拷-1 int Ping(const char *ips, int timeout) { struct timeval *tval; int maxfds = 0; @@ -1958,81 +1829,80 @@ int Ping(const char *ips, int timeout) { int iRet = 0; struct sockaddr_in addr; struct sockaddr_in from; - // 闁荤姳鐒﹂崕鎶芥偩缂嶎晼婵烇絽娲犻崜婵囧閿燂拷 + // 闂佽崵濮抽悞锕傚磿閹惰姤鍋╃紓宥庢櫦濠电儑绲藉ú鐘诲礈濠靛洤顕遍柨鐕傛嫹 bzero(&addr, sizeof(addr)); addr.sin_family = AF_INET; - addr.sin_addr.s_addr = inet_addr(ips); int sockfd; - // 闂佸憡鐟﹂悧鏇犳閻Ζcket 闂侀潧妫撮幏锟� 婵犵鈧啿鈧綊鎮樻径濞炬煢闁炽儱鍟块~顤箄do 闁哄鏅滈悷鈺呭闯闁垮顕辨慨姗嗗幖琚氶梻浣稿皡閹凤拷 + // 闂備礁鎲¢悷锕傛偋閺囩姵顐介柣顏呂朿ket 闂備線娼уΛ鎾箯閿燂拷 濠电姷顣介埀顒€鍟块埀顒€缍婇幃妯诲緞婵炵偓鐓㈤梺鐐藉劚閸熷潡锝為·绠刣o 闂佸搫顦弲婊堟偡閳哄懎闂梺鍨儏椤曡鲸鎱ㄥ鍡楀箹鐞氭岸姊绘担绋跨殹闁瑰嚖鎷� sockfd = socket(AF_INET, SOCK_RAW, IPPROTO_ICMP); if (sockfd < 0) { - print_error("ip:%s,socket error\n", ips); + zlog_error(zct, "ip:%s,socket error", ips); return -1; } struct timeval timeo; - // 闁荤姳鐒﹂崕鎶芥偩缂嶇棤meOut闂佸搫鍟悥鐓幬涢敓锟� + // 闂佽崵濮抽悞锕傚磿閹惰姤鍋╃紓宥囨¥meOut闂備礁鎼崯顐︽偉閻撳宫娑㈡晸閿燂拷 timeo.tv_sec = timeout / 1000; timeo.tv_usec = timeout % 1000; if (setsockopt(sockfd, SOL_SOCKET, SO_SNDTIMEO, &timeo, sizeof(timeo)) == -1) { - print_error("ip:%s,setsockopt error\n", ips); + zlog_error(zct, "ip:%s,setsockopt error", ips); close(sockfd); return -1; } char sendpacket[2048]; char recvpacket[2048]; - // 闁荤姳鐒﹂崕鎶芥偩缂嶇導ng闂佸憡鐗幏锟� + // 闂佽崵濮抽悞锕傚磿閹惰姤鍋╃紓宥囧皫ng闂備礁鎲¢悧顓㈠箯閿燂拷 memset(sendpacket, 0, sizeof(sendpacket)); pid_t pid; - // 闂佸憡鐟﹂悧鏇犳閻╊棷D闂佹寧绋戞總鏃傜礊閺冣偓缁嬪寮у〒濯檊闂佹眹鍔岄崵鐚爍uence ID + // 闂備礁鎲¢悷锕傛偋閺囩姵顐介柣鈺婃7D闂備焦瀵х粙鎴炵附閺冨倻绀婇柡鍐e亾缂佸顦卞褍銆掓刊妾婇梻浣圭湽閸斿矂宕甸悮鐖島ence ID pid = getpid(); struct ip *iph; struct icmp *icmp; icmp = (struct icmp *)sendpacket; - icmp->icmp_type = ICMP_ECHO; //闂佹悶鍎抽崑鐐参熸径灞惧珰闂佸灝顑囧﹢锟� + icmp->icmp_type = ICMP_ECHO; //闂備焦鎮堕崕鎶藉磻閻愬弬鐔稿緞鐏炴儳鐝伴梻浣哥仢椤戝洤锕㈤敓锟� icmp->icmp_code = 0; icmp->icmp_cksum = 0; icmp->icmp_seq = 0; icmp->icmp_id = pid; tval = (struct timeval *)icmp->icmp_data; gettimeofday(tval, NULL); - icmp->icmp_cksum = cal_chksum((unsigned short *)icmp, sizeof(struct icmp)); //闂佸搫绋勭换婵嬫偘閿燂拷 + icmp->icmp_cksum = cal_chksum((unsigned short *)icmp, sizeof(struct icmp)); //闂備礁鎼粙鍕崲濠靛鍋橀柨鐕傛嫹 int n = sendto(sockfd, (char *)&sendpacket, sizeof(struct icmp), 0, (struct sockaddr *)&addr, sizeof(addr)); if (n < 1) { - print_error("ip:%s,sendto error\n", ips); + zlog_error(zct, "ip:%s,sendto error", ips); close(sockfd); return -1; } - // 闂佽浜介崕杈亹閿燂拷 - // 闂佹眹鍨硅ぐ澶岃姳椤掑嫬鐭楁い鏍ㄧ箓閸樻挳鏌熼幁鎺戝姎鐟滄澘鐗撳畷姘跺级閸喖绶繛瀵稿У濠€顣弉g闂佹眹鍔岀€氼剛鑺遍懠顒傞┏闁哄啠鍋撶紒澶屽厴楠炰線顢涢妶鍥╊槷闂佸湱顣介崑鎾趁归悩顔煎姤缂佺粯鐗犻弻灞界暆閸愮偓鐭楅梺鐑╁亾閸斿矂骞嗘繝鍥ㄥ仢闁跨噦鎷� + // 闂備浇顫夋禍浠嬪磿鏉堫偁浜归柨鐕傛嫹 + // 闂備焦鐪归崹纭呫亹婢跺矁濮虫い鎺戝閻銇勯弽銊х畵闁告ɑ鎸抽弻鐔煎箒閹烘垵濮庨悷婊勬緲閻楁挸鐣峰璺虹骇闁割煈鍠栫欢顓熺箾鐎电ǹ校婵犫偓椤e級g闂備焦鐪归崝宀€鈧凹鍓涢懞閬嶆嚑椤掑倿鈹忛梺鍝勫暊閸嬫挾绱掓径灞藉幋妤犵偘绶氶、娑㈠Χ閸モ晩妲烽梻浣告贡椤d粙宕戦幘瓒佸綊鎮╅鐓庡Г缂備胶绮悧鐘诲蓟鐏炵晫鏆嗛柛鎰亾閻姊洪悜鈺佷壕闁告柨鐭傞獮鍡樼節閸ャ劌浠㈤梺璺ㄥ櫐閹凤拷 int cnt = 0; while (1) { - // 闁荤姳鐒﹂崕鎶芥偩缂嶇棤meOut闂佸搫鍟悥鐓幬涢崸妤佹櫖鐎光偓閸愵亞顔愬┑鐐叉閸撴繃鏅堕悩璇插強妞ゆ牗绮嶉崺鍌涙叏濠垫挾绉柟鐧哥稻閹峰懎饪伴崨顔芥闂佹椿鐓夐幏锟� + // 闂佽崵濮抽悞锕傚磿閹惰姤鍋╃紓宥囨¥meOut闂備礁鎼崯顐︽偉閻撳宫娑㈠锤濡や焦娅栭悗鍏夊亾闁告劦浜為鎰攽閻愬弶顥戦柛鎾寸箖閺呭爼鎮╃拠鎻掑挤濡炪倖鐗楃划宥夊春閸屾稒鍙忔繝鍨尵缁夘剟鏌熼惂鍝ョɑ闁瑰嘲鎳庨オ浼村川椤旇姤顓归梻浣规た閻撳骞忛敓锟� FD_ZERO(&readfds); FD_SET(sockfd, &readfds); maxfds = sockfd + 1; n = select(maxfds, &readfds, NULL, NULL, &timeo); if (n <= 0) { - print_info("ip:%s,Time out error\n", ips); + zlog_info(zct, "ip:%s,Time out error", ips); close(sockfd); iRet = -1; break; } - // 闂佽浜介崕杈亹閿燂拷 + // 闂備浇顫夋禍浠嬪磿鏉堫偁浜归柨鐕傛嫹 memset(recvpacket, 0, sizeof(recvpacket)); int fromlen = sizeof(from); n = recvfrom(sockfd, recvpacket, sizeof(recvpacket), 0, (struct sockaddr *)&from, (socklen_t *)&fromlen); - print_info("recvfrom Len:%d\n", n); + zlog_info(zct, "recvfrom Len:%d", n); if (n < 1) { close(sockfd); iRet = 1; @@ -2040,27 +1910,26 @@ int Ping(const char *ips, int timeout) { } char *from_ip = (char *)inet_ntoa(from.sin_addr); - // 闂佸憡甯囬崐鏍蓟閸ヮ剙鍙婃い鏍ㄧ閸庡﹪鏌¢崟闈涚仴闁搞倖绮岄蹇涙儎椤﹀級g闂佹眹鍔岀€氼剙煤閺嶃劌绶為柨鐕傛嫹 + // 闂備礁鎲$敮鍥磹閺嶎厼钃熼柛銉墮閸欏﹥銇勯弽銊ь暡闁稿骸锕弻锟犲礋闂堟稓浠撮梺鎼炲€栫划宀勵敊韫囨稒鍎庢い锕€绱歡闂備焦鐪归崝宀€鈧凹鍓欑叅闁哄秲鍔岀欢鐐烘煥閻曞倹瀚� if (strcmp(from_ip, ips) != 0) { - print_info("NowPingip:%s Fromip:%s NowPingip is not same to Fromip,so ping wrong!\n", ips, from_ip); + zlog_info(zct, "NowPingip:%s Fromip:%s NowPingip is not same to Fromip,so ping wrong!", ips, from_ip); close(sockfd); iRet = 1; break; } iph = (struct ip *)recvpacket; - icmp = (struct icmp *)(recvpacket + (iph->ip_hl << 2)); - print_info("ip:%s,icmp->icmp_type:%d,icmp->icmp_id:%d\n", ips, icmp->icmp_type, icmp->icmp_id); - // 闂佸憡甯囬崐鏍蓟閸㈡迹ng闂佹悶鍎抽崑娑⑺囬弻銉ョ闁告侗鍨遍悾閬嶆煟濡灝鐓愰柍褜鍓ㄩ幏锟� - if (icmp->icmp_type == ICMP_ECHOREPLY && icmp->icmp_id == pid) // ICMP_ECHOREPLY闂佹悶鍎抽崑鐐参熸径瀣劅闁哄啫鍊婚幗锟� + zlog_info(zct, "ip:%s,icmp->icmp_type:%d,icmp->icmp_id:%d\n", ips, icmp->icmp_type, icmp->icmp_id); + // 闂備礁鎲$敮鍥磹閺嶎厼钃熼柛銏¤抗ng闂備焦鎮堕崕鎶藉磻濞戔懞鍥蓟閵夈儳顦遍梺鍛婁緱閸ㄩ亶鎮鹃柆宥嗙厽婵☆垰鐏濋悡鎰版煃瑜滈崜銊╁箯閿燂拷 + if (icmp->icmp_type == ICMP_ECHOREPLY && icmp->icmp_id == pid) // ICMP_ECHOREPLY闂備焦鎮堕崕鎶藉磻閻愬弬鐔稿緞鐎n偄鍔呴梺鍝勫暙閸婂骞楅敓锟� { - // 濠殿喗绻愮徊浠嬫偉閸撲椒鐒婇梺顒€绉崑鎾诲焵椤掑嫬绀勯柛婵嗗閸庢洟鏌e⿰鎰 - print_info("icmp succecss ............. \n"); + // 婵犳鍠楃换鎰緤娴犲鍋夐柛鎾叉閻掑﹪姊洪鈧粔顕€宕戦幘璇茬劦妞ゆ帒瀚粈鍕煕濠靛棗顏柛搴㈡礋閺岋絽饪伴幇顓烆伓 + zlog_info(zct, "icmp succecss ............. \n"); break; } else if (cnt < 3) { - // 闂佸憡鐔粻鎴﹀垂椤栨粎纾肩憸蹇涙偨閼姐倗椹抽柨鐕傛嫹 + // 闂備礁鎲¢悢顒傜不閹达箑鍨傛い鏍ㄧ矌绾捐偐鎲歌箛娑欏仺闁煎鍊楁す鎶芥煥閻曞倹瀚� cnt++; continue; } else { @@ -2084,6 +1953,7 @@ int get_netlink_status(const char *if_name) { ifr.ifr_data = (char *)&edata; if ((skfd = socket(AF_INET, SOCK_DGRAM, 0)) == 0) return -1; if (ioctl(skfd, SIOCETHTOOL, &ifr) == -1) { + zlog_error(zct, "ioctl failed, if_name:%s", if_name); close(skfd); return -1; } @@ -2091,56 +1961,44 @@ int get_netlink_status(const char *if_name) { return edata.data; } -// 闁诲繐绻愬Λ娑欐櫠濡ゅ懎瀚夋い鎺嗗亾鐟滄媽娉曢埀顒佺⊕椤ㄥ牓顢栨担鍦枖闁告瑥顦鏇㈡煕閹烘垶顥欓悹鎰枛瀵偆鈧稒蓱濞堝墎绱撴担绋款仾闁搞劍宀搁幆鍐礋椤愶絽鈧姊虹拠褎瀚� +// 闂佽绻愮换鎰涘☉娆愭珷婵°倕鎳庣€氬銇勯幒鍡椾壕閻熸粍濯藉▔鏇㈠焵椤掍胶鈯曟い銊ョ墦椤㈡牗鎷呴崷顓涙灃闂佸憡鐟ラˇ顖烆敁閺囥垺鐓曢柟鐑樺灦椤ユ瑩鎮归幇顔兼灈鐎殿噮鍋嗛埀顒佺⊕钃辨繛鍫濆缁辨挻鎷呯粙娆句痪闂佹悶鍔嶅畝鎼佸箚閸愵喖绀嬫い鎰剁到閳ь剙顭峰铏规嫚瑜庣€氾拷 std::vector splitVersion(const std::string &version) { std::vector parts; std::stringstream ss(version); std::string part; - // 闂佺儵鍋撻崝搴ㄥ磻閿濆棙濯存繝濞惧亾閻犳劗鍠栧畷姘跺幢濞戞瑯鍚樼紓浣诡殣鐠侊絿妲愬┑瀣闁糕剝顨呴ˉ蹇涙煟濡も偓閻楀﹤锕㈡导鏉戠煑闁跨噦鎷� + // 闂備胶鍎甸崑鎾诲礉鎼淬劌纾婚柨婵嗘婵瓨绻濇繛鎯т壕闁荤姵鍔楅崰鏍х暦濮樿泛骞㈡繛鎴炵懐閸氭绱撴担璇℃閻犱緤绲垮Σ鎰攽鐎n亞顦梺绯曞墲椤ㄥ懘藟韫囨稒鐓熸俊銈傚亾闁绘锕ら敃銏″閺夋垹鐓戦梺璺ㄥ櫐閹凤拷 while (std::getline(ss, part, '.')) { - parts.push_back(std::stoi(part)); // 闁诲繐绻愬Λ妤呭垂鎼淬劌绀堥柟缁樺笚閸婄敻鏌i妸銉ヮ伃闁稿孩鎸冲畷姘跺幢濮橆厾銈梺纭咁嚃濠⑩偓閻犳劗鍠栧顐も偓娑櫳戝▓锟� + parts.push_back(std::stoi(part)); // 闂佽绻愮换鎰涘Δ鍛瀭閹兼番鍔岀粈鍫ユ煙缂佹ê绗氶柛濠勬暬閺岋綁濡搁妷銉純闂佺ǹ瀛╅幐鍐茬暦濮樿泛骞㈡慨姗嗗幘閵堫噣姊虹涵鍜佸殐婵犫懇鍋撻柣鐘冲姉閸犳牕顕i銈傚亾濞戞鎴濃枔閿燂拷 } return parts; } -// 濠殿噯绲惧Λ浣烘濠靛洨鈻旈柕鍫濆閸ゅ鏌eΔ鈧悧濠傦耿娴兼潙鐭楅柨鐕傛嫹 +// 婵犳鍣徊鎯涙担鐑橆偨婵犻潧娲ㄩ埢鏃堟煏閸繂顏柛銈咁儔閺岋絽螖閳ь剟鎮ф繝鍌﹁€垮ù鍏兼綑閻鏌ㄩ悤鍌涘 int compareVersions(const std::string &version1, const std::string &version2) { std::vector v1 = splitVersion(version1); std::vector v2 = splitVersion(version2); - // 闂佺懓鐏氶崕鎶藉春瀹€鍕珘闁逞屽墴濮婂湱鎹勯妸锔炬殸闂佺粯顨呴悧濠傦耿娴兼潙鐭楅柣鎴f閸斻儵鏌涢幒鎴烆棦闁哄棜椴搁幆鏃堟晸閿燂拷 + // 闂備胶鎳撻悘姘跺磿閹惰棄鏄ョ€光偓閸曨偄鐝橀梺閫炲苯澧存慨濠傛贡閹瑰嫰濡搁敂鐐闂備胶绮〃鍛存偋婵犲偊鑰垮ù鍏兼綑閻鏌i幋锝嗩棄闁告柣鍎甸弻娑㈠箳閹寸儐妫﹂梺鍝勬妞存悂骞嗛弮鍫熸櫢闁跨噦鎷� size_t maxLength = std::max(v1.size(), v2.size()); for (size_t i = 0; i < maxLength; ++i) { - int num1 = i < v1.size() ? v1[i] : 0; // 婵犵鈧啿鈧綊鎮樻径鎰摕闁圭増婢橀崝銉╂煕閹烘垶顥欑紒妤€顦遍埀顒佺⊕閿氭繝鈧鍫熸櫖鐎光偓閸愮偓缍婃繛鎴炴惈閹凤拷0 + int num1 = i < v1.size() ? v1[i] : 0; // 濠电姷顣介埀顒€鍟块埀顒€缍婇幃妯诲緞閹邦剝鎽曢梺鍦濠㈡﹢宕濋妷鈺傜厱闁圭儤鍨堕ˉ娆戠磼濡も偓椤﹂亶鍩€椤掍胶鈯曢柨姘節閳ь剟顢旈崼鐔告珫閻庡厜鍋撻柛鎰亾缂嶅﹥绻涢幋鐐存儓闁瑰嚖鎷�0 int num2 = i < v2.size() ? v2[i] : 0; - if (num1 > num2) return 1; // version1 婵犮垹鐖㈤崒婊嗗 version2 - if (num1 < num2) return -1; // version1 闁诲繐绻愮换瀣姳閿燂拷 version2 + if (num1 > num2) return 1; // version1 濠电姰鍨归悥銏ゅ磼濠婂棗顥� version2 + if (num1 < num2) return -1; // version1 闂佽绻愮换鎰崲鐎n剝濮抽柨鐕傛嫹 version2 } - return 0; // 闂佺粯顨呴悧濠傦耿娴兼潙鐭楀瀣缁佹煡鏌涘鐐 + return 0; // 闂備胶绮〃鍛存偋婵犲偊鑰垮ù鍏兼綑閻顪冪€n亜顒㈢紒浣圭叀閺屾稑顫濋悙顒€顏� } void Binary_Bit(unsigned char *p_data, unsigned char position, int flag) { - //婵炲瓨绮岄惌浣烘崲濮椻偓瀹曟岸鎳滈悽闈涘福婵炶揪绲界粔鍫曟偪閸℃稒鈷栭柤鎼佹涧閻忔鏌ら弶鎸庡櫢缂佹棑鎷� position婵炴垶鎹佸銊х礊閸涙潙鏋侀柨鐕傛嫹(婵炲濯撮幏锟�0 閻庢鍠掗崑鎾斥攽椤曞棙瀚�) + //濠电偛鐡ㄧ划宀勬儗娴g儤宕叉慨妞诲亾鐎规洘宀搁幊婊堟偨闂堟稑绂忓┑鐐舵彧缁茬晫绮旈崼鏇熷仾闁糕剝绋掗埛鏍煠閹间焦娑ч柣蹇旑殜閺屻倝寮堕幐搴℃缂備焦妫戦幏锟� position濠电偞鍨堕幑浣割浖閵娧呯闁告稒娼欓弸渚€鏌ㄩ悤鍌涘(濠电偛顕刊鎾箯閿燂拷0 闁诲孩顔栭崰鎺楀磻閹炬枼鏀芥い鏇炴鐎氾拷) if (flag) { *p_data |= 0x01 << (position); } else { *p_data &= ~(0x01 << (position)); } -} - -static const char *JSON_FIELD_CMD = "cmd"; //闂佸憡顨呯换妤咁敊閿燂拷: 闂佸憡绋掗崹婵嬪箮閵堝洠鍋撳☉娆樻畷妞ゆ棑鎷� -static const char *JSON_FIELD_NAME = "dataWatchName"; //闂佸憡顨呯换妤咁敊閿燂拷: 缂傚倷绀侀悧蹇涱敂椤掑嫬瑙︾€广儱娉﹂敓锟� -static const char *JSON_FIELD_dataNodeGatewayNo = "dataNodeGatewayNo"; -static const char *JSON_FIELD_ASSETID = "dataWatchAssetId"; //闂佸憡顨呯换妤咁敊閿燂拷: 闁荤姍鍐仹濡ょ姴娲ㄧ槐鎾诲冀瑜嶆繛锟� 闁诲孩绋掗〃鍡涱敊閿燂拷 -static const char *JSON_FIELD_ADDEDBY = "dataWatchAddedBy"; //闂佸憡顨呯换妤咁敊閿燂拷: 濠电儑缍€椤曆勬叏閻愬顩查柨鐕傛嫹 闁诲孩绋掗〃鍡涱敊閿燂拷 -static const char *JSON_FIELD_DEVICETYPE = "deviceType"; -static const char *JSON_FIELD_ADDEDDATE = "dataWatchAddedDate"; -static const char *JSON_FIELD_IPADDRESS = "dataWatchIpAddress"; -static const char *JSON_FIELD_SN = "serialNumber"; -static const char *JSON_FIELD_VERSION = "softVersion"; -static const char *JSON_FIELD_TIMEZONE = "timezone"; +} \ No newline at end of file diff --git a/common/common_func.hpp b/common/common_func.hpp index cb1b164..266deb9 100644 --- a/common/common_func.hpp +++ b/common/common_func.hpp @@ -67,9 +67,11 @@ typedef struct statfs DISK, *pDISK; #define UINT16_HIGH(x) ((x & 0xFF00u) >> 8u) #define UINT16_LOW(x) ((x & 0x00FFu)) -#define GENERAL_BUF_SIZE 128 * 1024 * 10 -enum TIME_SIZE { TIME_MINUTE = 5, TIME_SECEOND = 8 }; +enum TIME_SIZE { + TIME_MINUTE = 5, + TIME_SECEOND = 8 +}; struct DevData { char mData[128]; @@ -329,12 +331,6 @@ extern int write_data(int fd, char* buff, int len); extern int read_data(int fd, char* buff, int len, int timeout); extern int ModifyMac(char* buff); extern void mssleep(unsigned long microseconds); -/** -* @brief 系统运行时输入入参进行系统软件版本查询 -* @param argv 输入参数 -vV版本查询 --debugmode日志调试模式 -* @return 函数返回0执行成功 -*/ -extern int CheckFileVersion(int argc, char** argv); /** * @brief 获取当前实时时间 @@ -413,7 +409,7 @@ extern std::string IpAddrInit(); * @brief 获取正在工作的网卡 * @return std::string */ -extern std::string GetWorkNic(); +// extern std::string GetWorkNic(); /** * @brief 获取系统基本信息 @@ -439,24 +435,12 @@ extern void StartWriteToDat(); */ extern void BackupDatFile(); -/** -* @brief 降采样 -* @return double -*/ -extern float* ReSample(int WaveDataLength, int N, int* NewWaveDataLength, std::vector& WaveData); - /** * @brief 设置系统时间 * @return int */ extern int SetTime(unsigned long seconds, int milliseconds = 0); -/** -* @brief 删除配置文件 -* @return int -*/ -extern void RemoveConfig(); - /** * @brief 获取文件内容 * @param filename 文件名 @@ -491,9 +475,9 @@ extern char* solve(char* dest, const char* src); extern void swap(char* data); extern int hexStringToBytes(const char* hexStr, unsigned char* bytes, size_t bytesSize); -extern int OpenWatchDog(); -extern int WriteWatchDog(int fd); -extern int CloseWatchDog(int fd); +int OpenWatchDog(); +int WriteWatchDog(int fd); + //获取4G信号强度 extern int getcsq(); extern std::string GetGwIp_(const char* eth_name); @@ -505,18 +489,10 @@ extern int writeIntValue(const char* section, char* key, int val, const char* fi int getDiskInfo(char* diskTotal, char* diskFree); -void timeout(int signo); unsigned short cal_chksum(unsigned short* addr, int len); -int pack(int pkt_no, char* sendpacket); -int send_packet(int pkt_no, char* sendpacket); -int recv_packet(int pkt_no, char* recvpacket); -int unpack(int cur_seq, char* buf, int len); -void tv_sub(struct timeval* out, struct timeval* in); -void _CloseSocket(); int socketHeart(const char* pSendData); -extern bool NetIsOk(); extern int Ping(const char* ips, int timeout); extern int get_netlink_status(const char* if_name); extern int compareVersions(const std::string& version1, const std::string& version2); diff --git a/jsonparse/communication_cmd.hpp b/jsonparse/communication_cmd.hpp index cc710b1..e5a96f3 100644 --- a/jsonparse/communication_cmd.hpp +++ b/jsonparse/communication_cmd.hpp @@ -40,7 +40,7 @@ public: std::string JsonCmd_51(Json::Value &recvBody); std::string JsonCmd_52(); // upload static data std::string JsonCmd_53(Json::Value &recvBody); - void JsonCmd_38(Json::Value &recvBody); //获取原始数据 + // web command parse std::string JsonCmd_Cgi_01(Param_01 ¶m); //用户操作处理 diff --git a/jsonparse/mqtt_cmd_parse.cpp b/jsonparse/mqtt_cmd_parse.cpp index 98e0309..4921ee1 100644 --- a/jsonparse/mqtt_cmd_parse.cpp +++ b/jsonparse/mqtt_cmd_parse.cpp @@ -488,31 +488,6 @@ std::string JsonData::JsonCmd_29(Param_29 ¶m) { return show_value_.write(jsonVal); } -void JsonData::JsonCmd_38(Json::Value &recvBody) { - Json::Value jsChannel = recvBody["channelArray"]; - print_info("jsChannel size : %d\n", jsChannel.size()); - if (jsChannel.isArray()) { - for (int i = 0; i < jsChannel.size(); i++) { - std::string strChannelid = jsChannel[i].asString(); - WAVE_GENERAL wavedata; - - WAVE_CONTAIN wave; - memset(wave.channelId, 0, 16); - sprintf(wave.channelId, "%s", strChannelid.c_str()); - - memset(wave.SensorEngineeringUnit, 0, 32); - sprintf(wave.SensorEngineeringUnit, "%s", wavedata.SensorEngineeringUnit.c_str()); - - wave.total = 1; - wave.count = 1; - wave.type = 0; - wave.flag = 0; - wave.number = wavedata.number; - memcpy(wave.waveData, wavedata.waveData, wavedata.number * sizeof(float)); - } - } -} - void JsonData::DataNodeStatusCheck() { array_t vetRes = sqlite_db_ctrl::instance().GetDataMultiLine(T_SENSOR_INFO(TNAME), "*", NULL); int nSize = vetRes.size(); diff --git a/main.cpp b/main.cpp index 6fcae02..2e5112d 100644 --- a/main.cpp +++ b/main.cpp @@ -26,7 +26,7 @@ zlog_category_t *zct = NULL; zlog_category_t *zbt = NULL; int main(int argc, char *argv[]) { - int rc = zlog_init("/opt/wlg.conf"); + int rc = zlog_init("/opt/configenv/wlg.conf"); if (rc) { printf("init failed\n"); return -1; @@ -40,8 +40,8 @@ int main(int argc, char *argv[]) { return -2; } - zlog_info(zbt, " Firmware compile time:%s %s,version %s\n", __DATE__, __TIME__, GlobalConfig::Version.c_str()); - zlog_info(zbt, "####CIDNSOFT start####\n"); + zlog_info(zbt, " Firmware compile time:%s %s,version %s", __DATE__, __TIME__, GlobalConfig::Version.c_str()); + zlog_info(zbt, "####CIDNSOFT start####"); g_VecWaveDataX.reserve(1000); g_VecWaveDataY.reserve(1000); diff --git a/uart/uart.cpp b/uart/uart.cpp index ce74838..a7a6976 100644 --- a/uart/uart.cpp +++ b/uart/uart.cpp @@ -14,7 +14,6 @@ #include #include "utility/serial.h" -char g_UartRecvBuf[GENERAL_BUF_SIZE]; int offSize = 0; pTestRecvCallBack pTestRecv;