1793 lines
80 KiB
C++
1793 lines
80 KiB
C++
#include "common_func.hpp"
|
||
#include <time.h>
|
||
#include <sys/time.h>
|
||
#include <unistd.h>
|
||
#include <sys/types.h>
|
||
#include <sys/stat.h>
|
||
#include <sys/select.h>
|
||
#include <sys/types.h>
|
||
#include <fcntl.h>
|
||
#include <limits.h>
|
||
#include <stdio.h>
|
||
#include <string.h>
|
||
#include <stdlib.h>
|
||
#include <unistd.h>
|
||
#include <linux/sockios.h>
|
||
#include <linux/rtc.h>
|
||
#include <sys/socket.h>
|
||
#include <netinet/in.h>
|
||
#include <net/if.h>
|
||
#include <stdlib.h>
|
||
#include <memory.h>
|
||
#include <string>
|
||
#include <list>
|
||
#include <iostream>
|
||
#include <fstream>
|
||
#include <arpa/inet.h>
|
||
#include <sys/ioctl.h>
|
||
#include <sys/mman.h>
|
||
#include <fcntl.h>
|
||
#include <memory.h>
|
||
#include <sys/ipc.h>
|
||
#include <sys/shm.h>
|
||
#include "dirent.h"
|
||
#include <iconv.h>
|
||
#include <termios.h>
|
||
#include <netinet/in.h>
|
||
#include <netinet/ip.h>
|
||
#include <netinet/ip_icmp.h>
|
||
#include <netdb.h>
|
||
#include <boost/algorithm/string/split.hpp>
|
||
#include <boost/thread/mutex.hpp>
|
||
#include <boost/algorithm/string/classification.hpp>
|
||
#include <boost/lexical_cast.hpp>
|
||
#include <boost/xpressive/xpressive_dynamic.hpp>
|
||
#include <json/json.h>
|
||
#include <zlog.h>
|
||
#include "global.hpp"
|
||
#include "dbaccess/sql_db.hpp"
|
||
|
||
|
||
#define ETHTOOL_GLINK 0x0000000a /* Get link status (ethtool_value) */
|
||
#define MAX_WAIT_TIME 1
|
||
#define MAX_NO_PACKETS 1
|
||
#define ICMP_HEADSIZE 8
|
||
#define PACKET_SIZE 4096
|
||
struct timeval tvsend, tvrecv;
|
||
struct sockaddr_in dest_addr, recv_addr;
|
||
int sockfd;
|
||
pid_t pid;
|
||
char sendpacket[PACKET_SIZE];
|
||
char recvpacket[PACKET_SIZE];
|
||
|
||
static boost::mutex s_config_mu;
|
||
|
||
extern zlog_category_t *zct;
|
||
extern zlog_category_t *zbt;
|
||
|
||
std::string GetLocalTimeWithMs(void) {
|
||
std::string defaultTime = "19700101000000000";
|
||
try {
|
||
struct timeval curTime;
|
||
gettimeofday(&curTime, NULL);
|
||
int milli = curTime.tv_usec / 1000;
|
||
|
||
char buffer[80] = {0};
|
||
struct tm nowTime;
|
||
localtime_r(&curTime.tv_sec, &nowTime); //é—‚å‚šå€·é‘³å •ã€<C3A3>æ¿ å‚—æž–æ¿žæˆ™åžºé<C2BA>…濋柕é<E280A2>?濇å?„椤æ„?ç²™å?Šæ´ªî”—鈧粔瀵哥矆閸℃稒é<E28099>“æ¬<C3A6>ç¹›é<E280BA>«æ¿ˆä»¢é–ºå??å––é<E28093>Œï½‰æ•<C3A6>æ¾¶å?‹å–šé—<C3A9>å“„æœ?绋掔æ<E2809D>¢å©µå¬?礋椤愵澀ç»?梻浣割å<C2B2>”閺夊ç<C5A0><C3A7>é¡?å›?æ‚—ç‘™å‹?礃椤ㄥ﹥淇婇悜钘壩ч柛婊€é<C2AC>’︿簺闂傚倷绀侀幖é?<3F>ï¹€ç–?é–¸æ„祴é<C2B4>‹æ ¨ç¹›éŽ´æ¬<C3A6>ç<EFBFBD>©é–¸ã„¥å€¿éªžæ ?ǎ顒€濡介柣鎾亾é—<C3A9>è?²é?¸ç» 嶉崕é<E280A2>—ç<E28094>»æ´?敃é<C692>Œæ¶˜ä»¼é—‚侇剙绉甸悡é<C2A1>‡ãˆ¡ç…™é—<C3A9>箑澧柛é”<C3A9>å‘?秺閺岋絾骞婇柛é<E280BA>‚跨Фç¼<C3A7>瑦寰勯幇é<E280A1>¨î‚£æ«<C3A6>闂侀潧臎閸滀焦啸缂傚倸é<C2B8>Šçƒ½æ‚žé”•€é<C2AC>£å³°ÎŸç<C5B8>›â„ƒç<C692>ƒé—<C3A9>å“„æ´¢é<C2A2>¨å½’悡婵å¬?ç… æ¿žæ<C5BE>‘é<E280A0>‰å½’柛瀣崌閹兘é<CB9C>Œå›¬æ•‚é<E2809A>™î‚?箓å?Šæ´ªå´¨æ¿ å‹?åš‚é—<C3A9>瑰嚖鎷�
|
||
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 (...) {
|
||
zlog_error(zct, "GetLocalTimeWithMs failed, use default time");
|
||
return defaultTime;
|
||
}
|
||
}
|
||
|
||
int code_convert(const char *from_charset, const char *to_charset, char *inbuf, size_t inlen, char *outbuf, size_t outlen) {
|
||
iconv_t cd;
|
||
char **pin = &inbuf;
|
||
char **pout = &outbuf;
|
||
cd = iconv_open(to_charset, from_charset);
|
||
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 -2;
|
||
}
|
||
iconv_close(cd);
|
||
*pout = '\0';
|
||
return 0;
|
||
}
|
||
|
||
int InitGpio(unsigned int gpioN, unsigned int inout) {
|
||
int fd = 0;
|
||
char tmp[100] = {0};
|
||
//é—‚å‚šå€·é‘³å •å¹ŠéŽ¾ç»˜å€¶æ¿®æ¨¿æ³›ç» ä¼´æŸ›å©µå‹?劜椤洟é<C5B8>Œç†¸æ½<C3A6>鈺佲å<C2B2>“宄闂備浇宕åž?慨鎶芥倿閿曞倸纾å<C2BE>—柟璺哄閸ãƒ?剦é<C2A6><C3A9>嗛柛é<E280BA>‡ã„¥äº<C3A4>椤ユ繈姊洪幖é<E28093><C3A9>æ<EFBFBD>’姷闂傚å«?瀚伴ã€<C3A3>é<EFBFBD>‡ãˆ¡æ™¸é–¿ç‡‚æ‹?
|
||
fd = open("/sys/class/gpio/export", O_WRONLY);
|
||
if (-1 == fd) {
|
||
zlog_error(zbt, "open gpio export file error");
|
||
return 1;
|
||
}
|
||
//闂傚倷é<C2B7>’﹂惇褰掑垂瑜版帗é<E28094>‹î<E280B9>ˆæŸ›éŠ‰î†¼å¢»é–ºä½¸îƒ‰é<E280B0>Œï½‰æ‚™é<E284A2>?î‚?ç´¬io
|
||
sprintf(tmp, "%d", gpioN);
|
||
if (write(fd, tmp, strlen(tmp)) < 0) {
|
||
zlog_error(zbt, "write file operation error:%s", tmp);
|
||
close(fd);
|
||
return 2;
|
||
}
|
||
|
||
close(fd);
|
||
sleep(1);
|
||
//闂傚倸é<C2B8>Šçƒ½æ‚žé”•€é¡?åž?Ο鑲╃煋é—<C3A9>割å<C2B2>…娲橀崑é?<3F>å Ÿç…•é–»æ„?彃é<C692>˜îœ¯o闂傚倷绀侀幖é?<3F>ï¹€ç£¹å©µçŠ³è‰¾ç» çŠ»æŸŸéŽ¹æ„µåš™ç¼<C3A7>çŠ³ç‰ é<C2A0>Œã„©æ‚¤é<C2A4>Œæ¶˜î<CB9C>?
|
||
#ifdef G2UL_GATEWAY
|
||
char tmp2[100] = {0};
|
||
if (gpioN == 507)
|
||
memcpy(tmp2, "P18_3", 5);
|
||
else if (gpioN == 499)
|
||
memcpy(tmp2, "P17_3", 5);
|
||
else if (gpioN == 496)
|
||
memcpy(tmp2, "P17_0", 5);
|
||
else if (gpioN == 130)
|
||
memcpy(tmp2, "P9_0", 4);
|
||
else if (gpioN == 408)
|
||
memcpy(tmp2, "P6_0", 4);
|
||
else if (gpioN == 363)
|
||
memcpy(tmp2, "P0_3", 4);
|
||
else if (gpioN == 498)
|
||
memcpy(tmp2, "P17_2", 5);
|
||
else if (gpioN == 466)
|
||
memcpy(tmp2, "P13_2", 5);
|
||
else if (gpioN == 488)
|
||
memcpy(tmp2, "P16_0", 5);
|
||
else if (gpioN == 474)
|
||
memcpy(tmp2, "P14_2", 5);
|
||
else if (gpioN == 497)
|
||
memcpy(tmp2, "P17_1", 5);
|
||
else if (gpioN == 409)
|
||
memcpy(tmp2, "P6_1", 4);
|
||
else if (gpioN == 410)
|
||
memcpy(tmp2, "P6_2", 4);
|
||
else if (gpioN == 449)
|
||
memcpy(tmp2, "P11_1", 5);
|
||
else if (gpioN == 489)
|
||
memcpy(tmp2, "P16_1", 5);
|
||
|
||
sprintf(tmp, "/sys/class/gpio/%s/direction", tmp2);
|
||
#endif
|
||
#ifdef IMX6UL_GATEWAY
|
||
sprintf(tmp, "/sys/class/gpio/gpio%d/direction", gpioN);
|
||
#endif
|
||
|
||
zlog_info(zbt, "open GPIO = %s", tmp);
|
||
fd = open(tmp, O_WRONLY);
|
||
if (-1 == fd) {
|
||
zlog_error(zbt, "open gpio direction file error");
|
||
close(fd);
|
||
return 3;
|
||
}
|
||
|
||
if (inout == 0) {
|
||
zlog_info(zbt, "=====InitGpio=====in");
|
||
if (-1 == write(fd, "in", sizeof("in"))) {
|
||
zlog_error(zbt, "[%d]write operation direction error", gpioN);
|
||
close(fd);
|
||
return 4;
|
||
}
|
||
} else if (inout == 1) {
|
||
zlog_info(zbt, "=====InitGpio=====out");
|
||
if (-1 == write(fd, "out", sizeof("out"))) {
|
||
zlog_error(zbt, "[%d]write operation direction error", gpioN);
|
||
close(fd);
|
||
return 5;
|
||
}
|
||
}
|
||
close(fd);
|
||
return 0;
|
||
}
|
||
|
||
int gpio_set(unsigned int gpioN, char x) {
|
||
int fd = 0;
|
||
char tmp[100] = {0};
|
||
#ifdef G2UL_GATEWAY
|
||
char tmp2[100] = {0};
|
||
if (gpioN == 507)
|
||
memcpy(tmp2, "P18_3", 5);
|
||
else if (gpioN == 499)
|
||
memcpy(tmp2, "P17_3", 5);
|
||
else if (gpioN == 496)
|
||
memcpy(tmp2, "P17_0", 5);
|
||
else if (gpioN == 130)
|
||
memcpy(tmp2, "P9_0", 4);
|
||
else if (gpioN == 408)
|
||
memcpy(tmp2, "P6_0", 4);
|
||
else if (gpioN == 363)
|
||
memcpy(tmp2, "P0_3", 4);
|
||
else if (gpioN == 498)
|
||
memcpy(tmp2, "P17_2", 5);
|
||
else if (gpioN == 466)
|
||
memcpy(tmp2, "P13_2", 5);
|
||
else if (gpioN == 488)
|
||
memcpy(tmp2, "P16_0", 5);
|
||
else if (gpioN == 474)
|
||
memcpy(tmp2, "P14_2", 5);
|
||
else if (gpioN == 497)
|
||
memcpy(tmp2, "P17_1", 5);
|
||
else if (gpioN == 409)
|
||
memcpy(tmp2, "P6_1", 4);
|
||
else if (gpioN == 410)
|
||
memcpy(tmp2, "P6_2", 4);
|
||
else if (gpioN == 449)
|
||
memcpy(tmp2, "P11_1", 5);
|
||
else if (gpioN == 489)
|
||
memcpy(tmp2, "P16_1", 5);
|
||
sprintf(tmp, "/sys/class/gpio/%s/value", tmp2);
|
||
#endif
|
||
#ifdef IMX6UL_GATEWAY
|
||
sprintf(tmp, "/sys/class/gpio/gpio%d/value", gpioN);
|
||
#endif
|
||
|
||
zlog_info(zbt, "set GPIO = %s", tmp);
|
||
//é—‚å‚šå€·é‘³å •å¹ŠéŽ¾ç»˜å€¶æ¿®æ¨¿æ³›ç» ä¼´æŸ›å©µå‹?劜椤洟é<C5B8>Œç†¸æ½<C3A6>鈺佲å<C2B2>“宄 value闂傚倷绀侀幖é?<3F>﹀磹ç¼<C3A7>å¬?5娲î<C2B2>…晲閸涱äº<C3A4>é<EFBFBD>Žå©šæ¢ºç’ºã„¥æ«<C3A6>閹凤拷
|
||
fd = open(tmp, O_WRONLY);
|
||
if (-1 == fd) {
|
||
zlog_error(zbt, "[%s] open gpio export file error", tmp);
|
||
close(fd);
|
||
return (-1);
|
||
}
|
||
//闂備浇宕åž?慨宕囩矆娴hî<CB86>‰å¨…犲ùé<C2B9>˜å·?å„<C3A5>閸嬵亪é<C2AA>Œæ¶¢åŸ„é<E2809E><C3A9>î<EFBFBD><C3AE>å?‡é—<C3A9>稿é<C692>”æˆ<C3A6>î<EFBFBD><C3AE>æ¿ æ°?醇閻旂儤é<C2A4>’涢梺褰掓交閹凤æ‹?
|
||
if (x) {
|
||
if (-1 == write(fd, "1", sizeof("1"))) {
|
||
zlog_error(zbt, "%d write operation value error", gpioN);
|
||
close(fd);
|
||
return (-2);
|
||
}
|
||
} else {
|
||
if (-1 == write(fd, "0", sizeof("0"))) {
|
||
zlog_error(zbt, "%d write operation value errorn", gpioN);
|
||
close(fd);
|
||
return (-3);
|
||
}
|
||
}
|
||
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;
|
||
|
||
char tmp[100] = {0};
|
||
|
||
#ifdef G2UL_GATEWAY
|
||
char tmp2[100] = {0};
|
||
if (gpioN == 507)
|
||
memcpy(tmp2, "P18_3", 5);
|
||
else if (gpioN == 499)
|
||
memcpy(tmp2, "P17_3", 5);
|
||
else if (gpioN == 496)
|
||
memcpy(tmp2, "P17_0", 5);
|
||
else if (gpioN == 130)
|
||
memcpy(tmp2, "P9_0", 4);
|
||
else if (gpioN == 408)
|
||
memcpy(tmp2, "P6_0", 4);
|
||
else if (gpioN == 363)
|
||
memcpy(tmp2, "P0_3", 4);
|
||
else if (gpioN == 498)
|
||
memcpy(tmp2, "P17_2", 5);
|
||
else if (gpioN == 466)
|
||
memcpy(tmp2, "P13_2", 5);
|
||
else if (gpioN == 488)
|
||
memcpy(tmp2, "P16_0", 5);
|
||
else if (gpioN == 474)
|
||
memcpy(tmp2, "P14_2", 5);
|
||
else if (gpioN == 497)
|
||
memcpy(tmp2, "P17_1", 5);
|
||
else if (gpioN == 409)
|
||
memcpy(tmp2, "P6_1", 4);
|
||
else if (gpioN == 410)
|
||
memcpy(tmp2, "P6_2", 4);
|
||
else if (gpioN == 449)
|
||
memcpy(tmp2, "P11_1", 5);
|
||
else if (gpioN == 489)
|
||
memcpy(tmp2, "P16_1", 5);
|
||
sprintf(tmp, "/sys/class/gpio/%s/value", tmp2);
|
||
#endif
|
||
#ifdef IMX6UL_GATEWAY
|
||
sprintf(tmp, "/sys/class/gpio/gpio%d/value", gpioN);
|
||
#endif
|
||
//é—‚å‚šå€·é‘³å •å¹ŠéŽ¾ç»˜å€¶æ¿®æ¨¿æ³›ç» ä¼´æŸ›å©µå‹?劜椤洟é<C5B8>Œç†¸æ½<C3A6>鈺佲å<C2B2>“宄 value闂傚倷绀侀幖é?<3F>﹀磹ç¼<C3A7>å¬?5娲î<C2B2>…晲閸涱äº<C3A4>é<EFBFBD>Žå©šæ¢ºç’ºã„¥æ«<C3A6>閹凤拷
|
||
fd = open(tmp, O_RDONLY);
|
||
if (-1 == fd) {
|
||
zlog_error(zbt, "%d open gpio export file error", gpioN);
|
||
return (-1);
|
||
}
|
||
//闂備浇宕åž?æ…¨é<C2A8>‰æ‡¨æ´ªåŸ¡é<C2A1>œä½¹æ™ªé<C2AA>Ÿæ»„垿濡甸幇é<E280A1>—å Ÿæ«¢é—<C3A9>跨噦鎷� value闂傚倷绀侀幖é?<3F>﹀磹ç¼<C3A7>å¬?5娲î<C2B2>…晲閸涱äº<C3A4>é<EFBFBD>Žå©šæ¢ºç’ºã„¥æ«<C3A6>閹凤拷
|
||
if (-1 == read(fd, &value, sizeof(value))) {
|
||
zlog_error(zbt, "%d read gpiovalue is fail", gpioN);
|
||
close(fd);
|
||
return (-2);
|
||
}
|
||
close(fd);
|
||
zlog_info(zbt, "gpio%d get %d", gpioN, value);
|
||
return value;
|
||
}
|
||
|
||
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;
|
||
tcgetattr(iFd, &opt);
|
||
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);
|
||
opt.c_iflag &= ~(BRKINT | ICRNL | INPCK | ISTRIP | IXON);
|
||
opt.c_oflag &= ~(OPOST);
|
||
opt.c_cflag &= ~(CSIZE | PARENB | CBAUD);
|
||
opt.c_cflag |= (CS8 | speed);
|
||
opt.c_cc[VMIN] = 255;
|
||
opt.c_cc[VTIME] = 5;
|
||
if (tcsetattr(iFd, TCSANOW, &opt) < 0) {
|
||
zlog_error(zbt, "tcsetattr failed");
|
||
return -2;
|
||
}
|
||
tcflush(iFd, TCIOFLUSH);
|
||
return iFd;
|
||
}
|
||
|
||
int write_data(int fd, char *buff, int len) {
|
||
int ret;
|
||
ret = write(fd, buff, len);
|
||
if (ret < 0) {
|
||
zlog_error(zct, "write failed");
|
||
return -1;
|
||
}
|
||
return ret;
|
||
}
|
||
|
||
int read_data(int fd, char *buff, int len, int timeout) {
|
||
int ret;
|
||
struct timeval to;
|
||
fd_set rdfds;
|
||
|
||
to.tv_sec = 0;
|
||
to.tv_usec = timeout;
|
||
|
||
FD_ZERO(&rdfds);
|
||
FD_SET(fd, &rdfds);
|
||
|
||
if (timeout > 0) {
|
||
ret = select(fd + 1, &rdfds, NULL, NULL, &to);
|
||
if (ret <= 0) {
|
||
return ret;
|
||
}
|
||
}
|
||
|
||
ret = read(fd, buff, len);
|
||
if (ret < 0) {
|
||
zlog_error(zct, "read_data failed");
|
||
return -1;
|
||
}
|
||
buff[ret] = '\0';
|
||
return ret;
|
||
}
|
||
|
||
int ModifyMac(char *buff) {
|
||
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);
|
||
}
|
||
|
||
std::string GetCurrentTime() {
|
||
struct tm nowtime;
|
||
struct timeval tv;
|
||
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;
|
||
}
|
||
|
||
tm *get_current_date() {
|
||
time_t t = time(NULL);
|
||
struct tm *tm_info = localtime(&t);
|
||
int iyear = 0;
|
||
int imonth = 0;
|
||
int day = 0;
|
||
iyear = tm_info->tm_year + 1900;
|
||
imonth = tm_info->tm_mon + 1;
|
||
day = tm_info->tm_mday;
|
||
zlog_info(zct, "year = %d,month = %d,day = %d", iyear, imonth, day);
|
||
return tm_info;
|
||
}
|
||
|
||
int system_custom(const char *cmd, char *buf) {
|
||
FILE *fp;
|
||
int res;
|
||
char temp[1024] = {0};
|
||
if (cmd == NULL) {
|
||
zlog_error(zct, "cmd is NULL");
|
||
return -1;
|
||
}
|
||
|
||
if ((fp = popen(cmd, "r")) == NULL) {
|
||
zlog_error(zct, "popen error");
|
||
return -1;
|
||
} else {
|
||
buf[0] = '\0';
|
||
while (fgets(temp, sizeof(temp), fp)) {
|
||
strcat(buf, temp);
|
||
}
|
||
res = pclose(fp);
|
||
}
|
||
|
||
char *p;
|
||
int pos = 0;
|
||
p = strstr(buf, "\n");
|
||
if (p != NULL) {
|
||
pos = p - buf;
|
||
buf[pos] = '\0';
|
||
}
|
||
|
||
return res;
|
||
}
|
||
|
||
void GetTimeNet(char *timebuf, int type) {
|
||
struct timeval tv;
|
||
gettimeofday(&tv, NULL);
|
||
int millisecond = tv.tv_usec / 1000;
|
||
if (type == 0) {
|
||
sprintf(timebuf, "%ld%03d", tv.tv_sec, millisecond);
|
||
} else {
|
||
sprintf(timebuf, "%ld", tv.tv_sec);
|
||
}
|
||
}
|
||
|
||
std::string GetRTC(char *timestamp, int &millisecond) {
|
||
time_t rtc_timestamp;
|
||
struct tm tm;
|
||
struct timespec ts;
|
||
char rtcTime[100] = {0x00};
|
||
int fd = open("/dev/rtc0", O_RDONLY);
|
||
if (fd < 0) {
|
||
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);
|
||
|
||
// 闂備浇é?•Ñ…æ<E280A6>¢éŽ°î†½å´²é–¹ç‰ˆæ¾˜é<CB9C>‹ä¾€æŸ›å?¥å›§å<C2A7>‘_time缂傚倸é<C2B8>Šæ<C5A0><C3A6>å´<C3A5>é<EFBFBD>‘èŠ¥å€¿é–¿æ›žå€¸ç» æ<C2A0>¿â”‘é<E28098>˜å´‡î•²é–¸å©‚ç<E2809A>šéŠ‡å‹¯å¼½éŠŠÑ€å§‡æ¿ æ®¿åž±éŽ¸æŠ½å¼»é<C2BB>”ç…Žç?²é–¹æ<C2B9>¿ç<C2BF>šç¼<C3A7>å ¢ç´“æµ£é£Žè´°é–¸ï½<C3AF>ç¶<C3A7>å??婚悢é<C2A2>‘樺ç<C2BA>°é—<C3A9>斥晛é<E280BA>Ÿæ‰®ç²£é<C2A3>ƒå Ÿç…Ÿé–»æ¨ºå•¿æ¿®å¤<C3A5>柛é<E280BA>‚跨┘me_t闂傚倷绀侀幖é?<3F>ï¹€ç–?椤愶附é<E2809E>‹å¤<C3A5>柣鎾冲濞戙垹閿ゆ俊銈傚亾缂佺媴ç¼<C3A7>侀弻銊╂å<E2809A>†é–¸å±¾ç¨‘é?<3F>�
|
||
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; // 婵犵數é<C2B8>‹ç‚ºå´¹é<C2B9>?曞ç?°é–¸æ¿„儳é<C2B3>?撻柧蹇e亞ç¼<C3A7>犻箖é<E28093>Œæ¶¢å¦·é?”ç…Žî<C5BD>?é—<C3A9>ç?¿îƒƒé<C692>”戦弻é<C2BB>‡ç†ºç?¾é–¸îŸ‘å––æ¿?跺┑鈥冲级閸庢娊婀侀梺ç¼<C3A7>æ©ˆä¾›é–¸çŠ³ç‰ é<C2A0>©â‚?椤æŽ<C3A6>胶澧î<C2A7>…ã<E280A6>„é¡<C3A9>勫暣閺佹æ<C2B9>‡é<E280A1><C3A9>撻敓锟ï¿?
|
||
|
||
rtc_timestamp = mktime(&tm);
|
||
|
||
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);
|
||
}
|
||
|
||
std::string ReadStrByOpt(std::string filename, std::string config, std::string option) {
|
||
boost::mutex::scoped_lock lock(s_config_mu);
|
||
Json::Value root;
|
||
Json::Reader reader;
|
||
std::string value;
|
||
std::fstream is;
|
||
is.open(filename.c_str(), std::ios::in);
|
||
if (reader.parse(is, root)) {
|
||
value = root[config]["option"][option].asString();
|
||
}
|
||
is.close();
|
||
return value;
|
||
}
|
||
|
||
std::vector<DataNodeUpdate> ReadStrUpdate(std::string filename) {
|
||
boost::mutex::scoped_lock lock(s_config_mu);
|
||
Json::Value root, hwVersion;
|
||
Json::Reader reader;
|
||
std::vector<std::string> value;
|
||
std::vector<DataNodeUpdate> vecDataNodeUpdate;
|
||
DataNodeUpdate datanodeUpdate;
|
||
std::fstream is;
|
||
is.open(filename.c_str(), std::ios::in);
|
||
if (reader.parse(is, root)) {
|
||
zlog_info(zbt, "root = %d", root.size());
|
||
for (size_t i = 0; i < root.size(); i++) {
|
||
hwVersion = root[i]["hw_vesion"];
|
||
for (size_t j = 0; j < hwVersion.size(); j++) {
|
||
datanodeUpdate.hwVersion.push_back(hwVersion[j].asString());
|
||
}
|
||
datanodeUpdate.strUpdataFileName = root[i]["fw_name"].asString();
|
||
datanodeUpdate.strSoftVersion = root[i]["sf_vesion"].asString();
|
||
zlog_info(zbt, "strUpdataFileName = %s, strSoftVersion = %s", datanodeUpdate.strUpdataFileName.c_str(), datanodeUpdate.strSoftVersion.c_str());
|
||
vecDataNodeUpdate.push_back(datanodeUpdate);
|
||
}
|
||
}
|
||
is.close();
|
||
|
||
return vecDataNodeUpdate;
|
||
}
|
||
|
||
void ReadStrConfig(std::string filename) {
|
||
Json::Value root, gateWay, dataNode;
|
||
std::fstream is;
|
||
Json::Reader reader;
|
||
is.open(filename.c_str(), std::ios::in);
|
||
std::string zigbeeChannel;
|
||
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 (size_t i = 0; i < dataNode.size(); i++) {
|
||
std::string softVersion = dataNode[i]["softVersion"].asString();
|
||
std::string bpNo = dataNode[i]["bpNo"].asString();
|
||
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();
|
||
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();
|
||
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();
|
||
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();
|
||
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);
|
||
int res = sqlite_db_ctrl::instance().InsertData(T_SENSOR_INFO(TNAME), insertSql);
|
||
if (res != 0)
|
||
{
|
||
zlog_error(zbt, "res:%d", res);
|
||
}
|
||
|
||
}
|
||
|
||
is.close();
|
||
}
|
||
|
||
void ImportConfig(std::string filename) {
|
||
Json::Value root, gateWay, dataNode;
|
||
std::fstream is;
|
||
Json::Reader reader;
|
||
int res = 0;
|
||
is.open(filename.c_str(), std::ios::in);
|
||
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 (size_t i = 0; i < jsondataNodeArray.size(); i++) {
|
||
Json::Value valNode = jsondataNodeArray[i];
|
||
std::vector<std::string> vecDataNode;
|
||
for (size_t j = 0; j < valNode.size(); j++) {
|
||
vecDataNode.push_back(std::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());
|
||
res = sqlite_db_ctrl::instance().InsertData(T_SENSOR_INFO(TNAME), insertSql);
|
||
if (res != 0){
|
||
zlog_error(zbt, "res:%d", res);
|
||
}
|
||
}
|
||
|
||
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) {
|
||
std::vector<DataNodeInfo> vecDataNode; //婵犵數濮伴崹é<C2B9>Ÿå¸®è€¿é<C2BF>‰å ›åŠ<C3A5>娅犳俊銈傚亾閸æ?…亶é<C2B6>Œç†ºâ‚?ç”µÇ¹æµ Ñ‡æŸ<C3A6>ç¼<C3A7>樻閺屽秷顧侀柛鎾å˜çš‘ç¼<C3A7>瑦å?°å‹¯å¹‡é<E280A1>¨î‚£æ«†é—‚ä½¸æ†¡å¨²ï¹‚å´¢æµ å??箟閼å§<C3A5>倗纾藉ùé”<C3A9>å ?嚃濞å ?絿绱撻崒娑欑殤é—<C3A9>硅弓é<E2809C>—冲畷é<C2B7>—ç‚©æ?ˆæ¿¡îˆšæ™<C3A6>é<EFBFBD>‡ãˆ¡â’‘绾æ‡<C3A6>褰х紒é<E28099>˜å†²ç<C2B2>¥é–³è¯²ç§¹é<C2B9><C3A9>撻敓锟ï¿?
|
||
//婵犵數é<C2B8>‹æ¶¢î”šé¡“熸å<C2B8><C3A5>娴兼潙纾å<C2BE>—柟ç¼<C3A7>㈠櫘閺佸霉閸忓å<E2809C>‹ç¼<C3A7>æˆ ç´’éˆ§î„„å´’é<E28099><C3A9>å¯¸åŽªæ¿ ãˆ£åŸ–é<E28093>©îˆšîžŠé¡•€é<C2AC>Œï½‰å¹˜ç€µå‘Šå™?é—<C3A9>逞屽å¢?æ¤¤æ—€ç‰ å®•ä¼´å¼½é¡<C3A9>e床é—<C3A9>ç‘°î‡é<C2AD>‹ç‚²â–<C3A2>é<EFBFBD>˜ç»˜ç…¥é–ºå›¨îš…æµœæƒ§îœƒéŠˆå—˜ç©¿ç¼‚å¶„ç·šå®•æ´ªåŸ€é¡’ä½µäºœé–¹çƒ˜åžµé¡<C3A9>╂俊é?<3F>ï½<C3AF>耿閺屾盯濡烽é<E280BA><C3A9>æ<EFBFBD>?â‚?嶅銈嗗姧閹凤拷
|
||
std::ifstream csv_data(filename, std::ios::in);
|
||
int iRet = 0;
|
||
if (!csv_data.is_open()) {
|
||
zlog_error(zbt, "UpdataDataNodeConfig fail to open:%s", filename.c_str());
|
||
return -1;
|
||
}
|
||
std::string line;
|
||
std::vector<std::string> words; //婵犵數濮伴崹é<C2B9>Ÿå¸®è€¿é<C2BF>‰å ›åŠ<C3A5>娅犳俊銈傚亾閸æ?…亶é<C2B6>Œç†ºâ‚?ç”µÇ¹æµ Ñ‡æŸ<C3A6>ç¼<C3A7>樻閺屽秷顧侀柛鎾å˜çš‘ç¼<C3A7>瑦å?°å‹¯å¹‡é<E280A1>¨î‚£æ«†é—‚ä½¸æ†¡å¨²ï¹‚å´¢æµ å??箟閼å§<C3A5>倗纾藉ùé”<C3A9>å ?嚃濞å ?絿绱撻崒娑欑殤é—<C3A9>硅弓é<E2809C>—冲畷é<C2B7>—ç‚©æ?ˆæ¿¡îˆšæ™<C3A6>é<EFBFBD>‡ãˆ¡â’‘绾æ‡<C3A6>褰х紒é<E28099>˜å†²ç<C2B2>¥é–³è¯²ç§¹é<C2B9><C3A9>撻敓锟ï¿?
|
||
std::string word;
|
||
|
||
DataNodeInfo dataNode;
|
||
// ------------闂備浇宕åž?æ…¨é<C2A8>‰æ‡¨æ´ªåŸ¡é<C2A1>œä½¹æ™ªé<C2AA>Ÿæ»„垿濡甸幇é<E280A1>‰è·¨å€žå?žã‚†å·»é<C2BB>‹æ’»æŸ¡ç€£â•ƒçª”é–ºå²‹çµ½èž–é–³ÑŒå‰ŸéŽ®Ñ‡å´±å¨†æˆ ç„¾é—<C3A9>跨噦鎷ï¿?-----------------
|
||
// 闂備浇宕åž?æ…¨é<C2A8>‰æ‡¨æ´ªåŸ¡é<C2A1>œä½¹æ™ªé<C2AA>Ÿæ»„垿濡甸幇é<E280A1>‰è·¨å€žå?žã‚†å·»é<C2BB>‹æ’»æŸ›éŽ°ã„¥äº¾é—‚å‚™ç„¦éŽ?å •å´•å¨²î<C2B2>„礈閿曞倸姹å<C2B9>‰ã<E280B0>„é<E2809E><C3A9>仦閸å¬?姵绻涢幋é”<C3A9>嗙彧閻庢熬鎷�
|
||
getline(csv_data, line);
|
||
|
||
std::istringstream sin;
|
||
// 闂傚倷绀佸﹢æ<C2A2>ˆã„§ä»šæ¿¡ç‚?倧闄å‹?ç¹›æ¿ å›?Υ娓氣å<C2A3>“æ? 炲鎮╅崘鑼酱闂佽崵æ¿?æ<>‘ú锕併亹閸愵亜绶ら柛褎顨嗛悡娑æ¨<C3A6>å½’æ•<C3A6>é<EFBFBD>›î†½å„“å?žã‚<C3A3>ç¶€é<E282AC>‹æ„°åŸ€é¡’冾æ½<C3A6>閹碱å<C2B1>Šéªžå¿›æ•“锟ï¿?
|
||
while (getline(csv_data, line)) {
|
||
words.clear();
|
||
sin.clear();
|
||
sin.str(line);
|
||
while (getline(sin, word, ',')) {
|
||
words.push_back(word);
|
||
}
|
||
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") != std::string::npos) {
|
||
dataNode.Range = 0;
|
||
} else if (words[19].find("16g") != std::string::npos) {
|
||
dataNode.Range = 1;
|
||
} else if (words[19].find("32g") != std::string::npos) {
|
||
dataNode.Range = 2;
|
||
} else if (words[19].find("64g") != std::string::npos) {
|
||
dataNode.Range = 3;
|
||
} else if (words[19].find("50g") != std::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);
|
||
}
|
||
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 (size_t 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;
|
||
}
|
||
|
||
int WriteStr2Config(std::string filename, std::string config, std::string option, std::string value, bool listable) {
|
||
boost::mutex::scoped_lock lock(s_config_mu);
|
||
Json::Value root;
|
||
Json::Value subroot;
|
||
Json::Value list;
|
||
Json::Value op;
|
||
Json::Reader reader;
|
||
std::fstream is;
|
||
is.open(filename.c_str(), std::ios::in);
|
||
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) {
|
||
list[option].append(Json::Value(value));
|
||
subroot["list"] = list;
|
||
} else {
|
||
op[option] = Json::Value(value);
|
||
subroot["option"] = op;
|
||
}
|
||
root[config] = subroot;
|
||
Json::StyledWriter sw;
|
||
std::ofstream os;
|
||
os.open(filename.c_str());
|
||
os << sw.write(root);
|
||
os.close();
|
||
return 0;
|
||
}
|
||
|
||
std::string GetLocalMac(const char *net) {
|
||
int sock_mac;
|
||
struct ifreq ifr_mac;
|
||
char mac_addr[30];
|
||
sock_mac = socket(AF_INET, SOCK_STREAM, 0);
|
||
if (sock_mac == -1) {
|
||
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) {
|
||
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]);
|
||
zlog_info(zbt, "net :%s, local mac:%s", net, mac_addr);
|
||
close(sock_mac);
|
||
return std::string(mac_addr);
|
||
}
|
||
|
||
std::string GetGwIp_(const char *eth_name) {
|
||
int sockfd;
|
||
char gwip_[16] = {0};
|
||
if (-1 == (sockfd = socket(PF_INET, SOCK_STREAM, 0))) {
|
||
zlog_error(zct, "[GetGwIp_] fail to create socket");
|
||
return "";
|
||
}
|
||
struct ifreq req;
|
||
struct sockaddr_in *host;
|
||
|
||
bzero(&req, sizeof(struct ifreq));
|
||
strcpy(req.ifr_name, eth_name);
|
||
ioctl(sockfd, SIOCGIFADDR, &req);
|
||
host = (struct sockaddr_in *)&req.ifr_addr;
|
||
close(sockfd);
|
||
if (host) {
|
||
strcpy(gwip_, inet_ntoa(host->sin_addr));
|
||
}
|
||
return std::string(gwip_);
|
||
}
|
||
|
||
std::string IpAddrInit() {
|
||
const char *WLAN2 = "wlan2";
|
||
const char *WLAN0 = "wlan0";
|
||
const char *ETH0 = "eth0";
|
||
const char *USB0 = "usb0";
|
||
const char *WWAN0 = "wwan0";
|
||
const char *ETH1 = "eth1";
|
||
const char *ETH2 = "eth2";
|
||
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;
|
||
}
|
||
zlog_warn(zct, "[IpAddrInit] ip: %s", strip.c_str());
|
||
return strip;
|
||
}
|
||
|
||
int SetTime(unsigned long seconds, int milliseconds) {
|
||
struct timeval tv;
|
||
time_t timep = (time_t)seconds;
|
||
tv.tv_sec = timep;
|
||
tv.tv_usec = milliseconds * 1000;
|
||
return settimeofday(&tv, NULL);
|
||
}
|
||
|
||
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 (size_t i = 0; i < sizeof(zonelists); i++) {
|
||
if (zoneid.compare(zonelists[i]) == 0) {
|
||
a = i;
|
||
zlog_info(zbt, "[ZoneConfig] zoneid:%s, a: %d", zoneid.c_str(), a);
|
||
break;
|
||
}
|
||
}
|
||
switch (a) {
|
||
case 0: zoneid = "GMT-12"; break;
|
||
case 1: zoneid = "GMT-11"; break;
|
||
case 2: zoneid = "GMT-10"; break;
|
||
case 3: zoneid = "GMT-9"; break;
|
||
case 4: zoneid = "GMT-8"; break;
|
||
case 5: zoneid = "GMT-7"; break;
|
||
case 6: zoneid = "GMT-6"; break;
|
||
case 7: zoneid = "GMT-5"; break;
|
||
case 8: zoneid = "GMT-4"; break;
|
||
case 9: zoneid = "GMT-3"; break;
|
||
case 10: zoneid = "GMT-2"; break;
|
||
case 11: zoneid = "GMT-1"; break;
|
||
case 12: zoneid = "GMT-0"; break;
|
||
case 13: zoneid = "GMT+1"; break;
|
||
case 14: zoneid = "GMT+2"; break;
|
||
case 15: zoneid = "GMT+3"; break;
|
||
case 16: zoneid = "GMT+4"; break;
|
||
case 17: zoneid = "GMT+5"; break;
|
||
case 18: zoneid = "GMT+6"; break;
|
||
case 19: zoneid = "GMT+7"; break;
|
||
case 20: zoneid = "GMT+8"; break;
|
||
case 21: zoneid = "GMT+9"; break;
|
||
case 22: zoneid = "GMT+10"; break;
|
||
case 23: zoneid = "GMT+11"; break;
|
||
}
|
||
char cmd[256] = {0};
|
||
memset(cmd, 0, 256);
|
||
sprintf(cmd, "sudo ln -sf /usr/share/zoneinfo/Etc/%s /etc/localtime", zoneid.c_str());
|
||
system(cmd);
|
||
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()) {
|
||
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é—‚å‚šå€·é‘³å •å´•é<E280A2>¢ç?¿ç£»é–¹æ<C2B9><C3A6>绀夌€广儱顦介å¼?é<>¡æ?¼ç¯€å©µçŠ²å€»æ¾§æ› ç´’éˆ§î„„å´±å¦¯è‚©î†•é—<C3A9>糕å‰<C3A5>锚ç¼<C3A7>旈箖é<E28093>Œå¶ˆî™<C3AE>é–¸æ’´ç‘©å®•îˆžî”›å§˜è‚©åŠ·é—Šî‚¢æ´¦ç»‹æˆ ç²»å?˜è¾¨å–<C3A5>韫囨洘é<CB9C><C3A9>滈æŸ<C3A6>è?œé<C593>“æ°?ç©±æ¿ å›?Χ閸ヮç<C2AE><C3A7>éŠ<C3A9>ゆ煟椤撴繄ç»<C3A7>旈柟顖欑åŠ<C3A5>ç¼<C3A7>诲懘éŽ?å›?å¼´éŠ<C3A9>♀拺闂傚牃é<C692>…犲é?”界節閵忊埗é?<3F>å šâ‚?﹂崶顒€绀冩ã<C2A9>„é<E2809E>ƒå›§î—<C3AE>ç<EFBFBD>šæ¿‹æ¢»æµ£ç?¿î—<C3AE>é–¸å??æ£<C3A6>宕伴幘璇å<E280A1>‚ラ柨é<C2A8>•å‚›å«?
|
||
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])");
|
||
return boost::xpressive::regex_match(ip, reg_ip);
|
||
}
|
||
|
||
bool IsValidMask(std::string mask) {
|
||
int iRet = -1;
|
||
|
||
// 闂備浇é?•Ñ…æ<E280A6>¢éŽ°î†½å´²é–¹ç‰ˆæ¾˜ç» æŸ¥æŸ›éŽ´ï¸¿æ†¾æ¿?婃椽å¦?å†?埡é<C2A1>›î‡…€嶅┑é<E28098>˜î†¼ç<C2BC>?é–¿æ°ã<C2AD>„é?“炴喘瀹曟﹢顢欓挊澶å?Œç´¦é—‚備胶鎳撻崥鈧悹浣åœ?å<>€ç€¹æ›Ÿåž¿éªžæ©€å¼?銉ョå¢?闂佸憡渚楅崢娲î<C2B2>„礌閺嶎厽鈷戦柛娑橈工ç¼<C3A7>æ?<3F>帗淇婇é”<C3A9>庢當é—<C3A9>伙絿é<C2BF><C3A9>樺畷é<C2B7>”碱敆娴i攱é<C2B1>žå?‹ç¹<C3A7>é<EFBFBD>¢é<C2A2>›ä»¦é–¸ã„¥çˆ¼é<C2BC><C3A9>å??梺ç¼<C3A7>樻尰閸ㄥ潡å??诲澶娢ㄩ柨é<C2A8>‚垮⒔閻f儳鈹戦悙瀛æ?¼ç¨‡å©µâ˜†å<E280A0>…ç»»å ¥æ‚°é¡•â‚¬éªžå??敃鈧~é<C5BE>›å˜ç…ƒé–µå¤ˆå„³é”›å¶…ù婊æ„å«? é—‚å‚šå€·é‘³å •å´‘éŠŠâ•<C3A2>磿閼碱剙é<E284A2>¨æ¿‹æŸ›é?<3F>犲ç<C2B2>±å©µî†½å¨Šé<C5A0>Œå¶‰å´?é<>•å<C5B8>“ç‘™å‹?閵å <C3A5>æ?›ç» 鹃柟瀵é•<C3A9>紳椤忓牊é<C5A0>‹å‚žæŸŸæ<C5B8>ˆé¹ƒæ??閻撴稑霉閿濆æ‡<C3A6>鎲æ<C2B2><C3A6>柟é<C5B8>£å±½â–ˆé–ºå²‹ç¹<C3A7>宕å<E280A2>žâ–Žè¹‡æ—‚亪闂佹悶é<C2B6>”æˆ ç²»é<C2BB><C3A9>î…žæž<C3A6>閹剧粯é<C2AF>…æ<E280A6><C3A6>柨é<C2A8>•å‚›å??
|
||
struct in_addr s;
|
||
iRet = inet_pton(AF_INET, mask.c_str(), &s);
|
||
|
||
// é—‚å‚™ç¤<C3A7>éŽ¼î‚¦Ë‡é–¬å¶…ç£¿é–¹ç‰ˆæ¾˜ç»€å Ÿæ…¨å§—å——å¢°é–ºå—?箓é<E2809C>Œï¼„仦璇æ<E280A1>’姎缂佺å?´ç¼<C3A7>侀弻é<C2BB>”兼焽閿曗å<E28094>“å?¢ÑŒå–—銇勯銈å‘?æž…é<E280A6>Žæ®¿å––é<E28093>–奸崺锟犲磼濞戞艾å??è™?ç¹<C3A7>é<EFBFBD>¢é<C2A2>›ä»©æ¤¤æ›Ÿç²“骞忛敓锟�1闂傚倷é<C2B7>’︾€笛呯矙閹达附é<E2809E>Žæ?€ã€’å?˜ï½…亾å?žã‚ƒåžºå®€æ<E282AC><C3A6>崺锟犲å·<C3A5>椤撶å<C2B6>›éŽ¸ã‚‰æ¢ºé<C2BA>Ÿæ<C5B8>¿æ‚‘閻näº?宕归æ<E28099>¹é<C2B9>¦î…Ÿå™®é—‚傚倷绀侀幖é?<3F>︽å<C2BD>‹é–¸â„ƒè›‹é<E280B9>¥ã„¥î‡±é–ºå¤‹åž¹é<C2B9>ŒÑ‡æ¢ºé—ˆæ¶±æ§´é–ºå‘?粓宕曞澶å??厱é—<C3A9>规惌é<C592>˜ä»‹æƒ<C3A6>æµ å¬?⒒娓氣å<C2A3>“濞煎å?Šä»‹å´Ÿé?<3F>î‚¤å”µå©µâ˜†åž¯ç’€ï¹‚æ‚žæµ å??煥閻曞倹瀚�
|
||
if (iRet == 1) {
|
||
// 婵犵數é<C2B8>‹æ¶¢î”šé¡“熸å<C2B8><C3A5>閺夋嚚瑙勵槹鎼达絿顦ç¹<C3A7>éŠ<C3A9>f硾閻å<C692><C3A5>æ¾¹æ›¢å´—é‘²â•ƒç˜ˆæ¿ ç”µå§´é<C2B4>Šæ<C5A0><C3A6>å¼³é<C2B3>”兼煙閻eè‹?é<>‹æ¶¢æŸ¡ç<C2A1>žè¯²â‚¬æ¿†ç•·é¡<C3A9>﹀礋椤æ„?喎浜鹃柛褎顨呰繚é—<C3A9>ç‘°å<C2B0>‹é<E280B9>£î<C2A3>ˆå´¹å¨²î<C2B2>„磿閻旇å<E280A1><C3A5>é<EFBFBD>™æ’»æŸ›éŠ‰ï½…å?¿æ¤¤ï½ˆå°™ç»±æŽ“笟é<C5B8>¥Ñ„ç<E2809E><C3A7>é—<C3A9>å“„çž¼é<C2BC> æ’»å¹<C3A5>é<EFBFBD>˜è¯²ç„ºé–¸æ„µäºžé<C5BE>›ãˆ¤æ¢ºé‘½ã‚…Ð£é–¸æ—€îšŠå®•ä¼´å¹‡é¡”å‰§ç…“æ¿ ãˆ£æ³›é<E280BA>?è‚©å?•é<E280A2><C3A9>é—´ç¹†æ¤¤æ ¨ç¹‚é<E2809A>šå½’柣銊ヮ煼閺岋絾éŽ?æ—ˆå?€éˆ¶â•<C3A2>î<EFBFBD>Œæ¿¡ç‚?們é<E28098>”岄ˇé<E280A1>—å ¢â”<C3A2>婵犲浂é<E2809A><C3A9>å›?柣é<C2A3>ƒå‚œç¯€ç¼<C3A7>ㄥå?Šæ´ªå´¨æ¿ å‹?畵閻庢凹é<C2B9> æ°¶å´°æ¿ å›¨æ™¸é–¿ç‡‚æ‹?
|
||
unsigned int addr = ntohl(s.s_addr);
|
||
|
||
// é—‚å‚™ç¤<C3A7>éŽ¼î‚¦Ë‡é–¬å¶…ç£¿é–¹ç‰ˆæ¾˜ç»€å Ÿæ…¨å§—å——å¢°é–ºå—?箓é<E2809C>Œæ¶˜â–Žè¹‡ï½†Ð¨é—<C3A9>å´‡ç²?å¦?å†?é<C2A3>«æ›Ÿå€·é–ºå¤‹åŸˆå¦?嗛梺璋庡啰é<C2B0>’哥€殿喖é<E28093>–å¥¸å´ºé”ŸçŠ²ç£¼æ¿ Ñ…å<E280A6>“é¡?嶉梻浣虹《閺å‘?粓銆å†?å´¼éŠ‰ãƒ§åŠ¦å¦žã‚†å¸Šé‘³å •åŸŠé<C5A0>‡ç†´äºœé–µå¨¿å„³æ¾§ï¸½ã<C2BD>„éŠ<C3A9>$å??閹峰懘宕烽娑欑亙闂備浇娉曢崳锕傚ç??閿燂æ‹?
|
||
std::bitset<32> b((int)addr);
|
||
std::string strMask = b.to_string();
|
||
|
||
// 闂傚倷绀侀幖é?<3F>ゆå<E280A0>–椤愶箑纾å<C2BE>—柛娆忣槺閻濊埖淇婇婵嗗惞å?žã‚‚晞ç<C5BE><C3A7>欓埀顒€é<C2AC>˜æ»ˆå´‘鎾绘煕閹æ<C2B9>¿å<C2BF>€ç»¨èŠ¥ã<C2A5>„é<E2809E>ƒâ‚?ç”?″î<C2B3><C3AE>娲î<C2B2>„å·<C3A5>婵犲倻é?‘勫┑é<E28098><C3A9>å·?槹閻╊垶骞冩ウ娆ç‚?Ь缂備緡é<C2A1>£î‡€å´£é<C2A3><C3A9>æž<C3A6>閸岀å<E282AC>žé<C5BE>¤å‹?ã<>„é<E2809E><C3A9>电稻閼哥懓鈹戦悙é<E284A2>‰æˆ 仸é—<C3A9>ç‘°çš·é<C2B7>…犲畷éŠ<C3A9>ゅ箚瑜å?<3F>弸é<C2B8>ƒå Ÿç…¥é–»æ›žå€¹ç€šï¿½"01"闂傚倷é<C2B7>’︾€笛呯矙閹达附é<E2809E>¤æ„ã<C2AD>„é<E2809E><C3A9>î‚?亼閳ь剙鎳撻ˇé<E280A1>¦î…›å<E280BA>“娈垮枤é<C2A4>‹î<E280B9>…îšœé¡<C3A9>冨嵆閸┾å<C2BE>“妞ゆ帒é<E28099>Šè<C5A0>¤ç²»é<C2BB>‚æ?¾ç††é<E280A0> å“„æ<E2809E>?顦柛瀣å°?閳藉î˜?鈻嶉è?Œç»¨å©šæŸ¨é<C2A8>‡æ¨ºç<C2BA>²æ¤¤ãˆ¡æ?ƒå®•奸悢é<C2A2>›å©„彨é—<C3A9>诲é?¸é<C2B8>˜æ»ˆå´‘鎾绘煕閹邦厼é<C2BC>”ゆ繛é<E280BA>«î‚¢ç<C2A2>¦æ¿?婃椽宕崟é?<3F>î‚¡æ‚£é—<C3A9>诲å?©ç»‹å ¥å¼²å¨‘樺ç¥?闂佸綊妫跨粈浣衡å<C2A1>“å?˜î„‰æ§¸æ¤¤î…¡æ³•鎹勬笟é?–æ°¬å£‰æ¿ ç”µå<C2B5>›éŽ³åº£æ<C2A3>¢å?—€å¯?婚æ•<C3A6>澶娢╅柕澶å <C3A5>労娴犲ジ姊洪悜鈺佸⒉é—<C3A9>è<EFBFBD>¤å•™é<E284A2>›î‡…潟é—<C3A9>å“„å•?é<>—嗙粻é”<C3A9>夋煟閹邦垰钄肩紒鈧â‚?n喗鈷戞繛鑼é¢<C3A9>嚙濞呮瑩é<C2A9>Œç†¼å´™éŠˆå——î<E28094>?
|
||
return (strMask.find("01") == std::string::npos);
|
||
}
|
||
|
||
return false;
|
||
}
|
||
|
||
int getSysIntValue(char *key) {
|
||
if (key == NULL) {
|
||
zlog_error(zct, "key is NULL");
|
||
return 0;
|
||
}
|
||
FILE *fp = NULL;
|
||
int value = 0;
|
||
fp = fopen(key, "r");
|
||
if (fp == NULL) {
|
||
zlog_error(zct, "could not open %s file", key);
|
||
return 1;
|
||
}
|
||
|
||
fscanf(fp, "%d", &value);
|
||
fclose(fp);
|
||
return value;
|
||
}
|
||
|
||
std::string GetSysStatus() {
|
||
long mem_used = -1;
|
||
long mem_free = -1;
|
||
long mem_total = -1;
|
||
long mem_cached = -1;
|
||
char name1[20];
|
||
std::string strMemTotal = GetFileContent("/proc/meminfo", 1);
|
||
std::string strMemFree = GetFileContent("/proc/meminfo", 2);
|
||
std::string strMemCache = GetFileContent("/proc/meminfo", 5);
|
||
|
||
sscanf(strMemTotal.c_str(), "%s%ld", name1, &mem_total);
|
||
sscanf(strMemFree.c_str(), "%s%ld", name1, &mem_free);
|
||
sscanf(strMemCache.c_str(), "%s%ld", name1, &mem_cached);
|
||
mem_used = mem_total - mem_free;
|
||
float fMemRate = 1.0 * mem_used / mem_total;
|
||
|
||
char name[8];
|
||
double cpu_sys = -1;
|
||
double cpu_user = -1;
|
||
double cpu_total = -1;
|
||
long int user, nice, sys, idle, iowait, irq, softirq;
|
||
std::string strCpu1 = GetFileContent("/proc/stat", 1);
|
||
sscanf(strCpu1.c_str(), "%s%ld%ld%ld%ld%ld%ld%ld", name, &user, &nice, &sys, &idle, &iowait, &irq, &softirq);
|
||
sleep(1);
|
||
long int userNext, niceNext, sysNext, idleNext, iowaitNext, irqNext, softirqNext;
|
||
std::string strCpu2 = GetFileContent("/proc/stat", 1);
|
||
sscanf(strCpu2.c_str(), "%s%ld%ld%ld%ld%ld%ld%ld", name, &userNext, &niceNext, &sysNext, &idleNext, &iowaitNext, &irqNext, &softirqNext);
|
||
cpu_total = (userNext + niceNext + sysNext + idleNext + iowaitNext + irqNext + softirqNext) - (user + nice + sys + idle + iowait + irq + softirq);
|
||
cpu_user = userNext - user;
|
||
cpu_sys = sysNext - sys;
|
||
float rateUser = cpu_user * 100.0 / cpu_total;
|
||
float rateSys = cpu_sys * 100.0 / cpu_total;
|
||
if (rateUser > 95) {
|
||
rateUser = 92;
|
||
}
|
||
char hardTotal[32];
|
||
char hardFree[32];
|
||
char rateHardUse[32];
|
||
char chRes[100];
|
||
memset(chRes, 0, 100);
|
||
#ifdef IMX6UL_GATEWAY
|
||
char hardName[32];
|
||
char hardUse[32];
|
||
const char *getEmmcInfo = "df -h | grep /opt";
|
||
system_custom(getEmmcInfo, chRes);
|
||
sscanf(chRes, "%s%s%s%s%s", hardName, hardTotal, hardUse, hardFree, rateHardUse);
|
||
#endif
|
||
#ifdef G2UL_GATEWAY
|
||
getDiskInfo(hardTotal, hardFree);
|
||
#endif
|
||
|
||
std::string strhardTotal(hardTotal);
|
||
std::string strhardFree(hardFree);
|
||
std::string strrateHardUse(rateHardUse);
|
||
|
||
char key[128] = {0};
|
||
memset(key, 0, sizeof(key));
|
||
sprintf(key, "/sys/class/thermal/thermal_zone0/temp");
|
||
int temp = getSysIntValue(key);
|
||
|
||
Json::Value jsData;
|
||
Json::FastWriter fw;
|
||
jsData["dataNodeGatewayNo"] = GlobalConfig::MacAddr_G;
|
||
jsData["cpuUserUse"] = rateUser;
|
||
jsData["memoryTotal"] = (int)mem_total;
|
||
jsData["memoryFree"] = (int)mem_free;
|
||
jsData["memoryUse"] = fMemRate * 100;
|
||
jsData["hardDiskTotal"] = atof(strhardTotal.substr(0, strhardTotal.length() - 1).c_str());
|
||
jsData["hardDiskFree"] = atof(strhardFree.substr(0, strhardFree.length() - 1).c_str());
|
||
jsData["temperature"] = temp / 1000.0;
|
||
jsData["temperatureNR5G"] = atoi(GlobalConfig::NR5GTemp.c_str());
|
||
|
||
float total = atof(strhardTotal.substr(0, strhardTotal.length() - 1).c_str());
|
||
float free = atof(strhardFree.substr(0, strhardFree.length() - 1).c_str());
|
||
float use = ((total - free) / total) * 100;
|
||
jsData["hardDiskUse"] = use;
|
||
jsData["cpuSystemUse"] = rateSys;
|
||
|
||
char localtimestamp[32] = {0};
|
||
GetTimeNet(localtimestamp, 1);
|
||
std::string nowTimetamp = std::string(localtimestamp);
|
||
jsData["updateTime"] = nowTimetamp;
|
||
std::string strJson = fw.write(jsData);
|
||
return strJson;
|
||
}
|
||
|
||
// 闂備浇é?•Ñ…æ<E280A6>¢éŽ°î†½å´²é–¹é‚¦å„µå¨‘æ?œæ—ˆåŸ€é¡’勬å<C2AC>©é–»æˆ£å§¤é<C2A4>‹å‹¯æŸ›å©µå‹?劗閺å¬?ç‰ å?Šè™¹æ<C2B9>„銊ユç<C2A6>¢é—<C3A9>瑰嚖鎷ï¿?16é—‚å‚™ç¤<C3A7>鎼ˇé?<3F>ï¹€ç–¾æ¿ å©‚æ‡<C3A6>é<EFBFBD>?欓柟æ<C5B8>ˆå‰§ç•±é–ºå‹©å„²æ·‡å©‡å¦¶é<C2B6>›î…Ÿæ®œé—<C3A9>ç?¿îƒ†éޏæ<C2B8><C3A6>埥澶娾枎濡îˆ<C3AE>å´µé<C2B5>†î…Ÿä¿Šé<C5A0><C3A9>î<EFBFBD>„â‚?æ ?崹鎶芥倿閿曗å<E28094>“椤æ´?顢旈崼é?<3F>f櫌婵炶æ<C2B6>ªç»²æŒŽç<C5BD>™ç¼‚併劊é<C5A0>ŽèŒ¬æ<C2AC>¢å¨‘㈠ç?£é–»æ„?潙纰嶉梺é<C2BA>¦î…›æ‹¡é–¸å¬?ï¹?銆佸î<C2B8><C3AE>棰濇晩é—<C3A9>兼亽é<C2BD>Žé<C5BD><C3A9>å´<C3A5>é<EFBFBD>›å˜â’‘閸濆å??é<>†æ¬“æŸ›æ¿ å‚œç§ºé–¹î‡€ç®–é<E28093>Œå——î…¼é<C2BC>¡æ¤»å<C2BB>“é<E2809C>¨î‚¤ç?¾é–¹å¯¸å<C2B8>Ÿé޳æ„ç¹›é<E280BA>«ç†ºæ‡ƒé–³è?„åž¿é<C2BF>©ãƒ©å´±å¦¤â‚?ç»?嶉悗瑙勬礃閸旀瑩å??幘ç¼<C3A7>樻櫢é—<C3A9>跨噦鎷�
|
||
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; // 闂備浇é?•уùé<C2B9>‘è—‰æž<C3A6>婵犳艾é<C2BE>’åž?ã<>„鎺嶈兌閵嗘帡é<C2A1>Œã‚ˆã<CB86><C3A3>鎺旂æš?é—<C3A9>哄矉绻濆畷姗€顢旈崟鎴秮閺岋ç¹<C3A7>宕ㄩéŠ<C3A9>犲Е闂佽é<E28093>¨ä¼´å´¯é<C2AF>‰æˆ 暦閻旂⒈é<CB86><C3A9>冮柕é<E280A2>«æ¿‡î—žé–¸çŠ³æ´œç»±æ’»å´’å¨†æˆ<C3A6>妽å?žã‚ƒåŠ<C3A5>é<EFBFBD>”楅å¹<C3A5>瀣晲閸ãƒ?ç…ˆå?¼ç†·â”‘é<E28098>˜ç»˜æ¶§æ¿žè?§é™„绂嶉妶澶å¬?厸é—<C3A9>稿本绋戦å?Šå‘¯ç£¼é–³ÑŒå‰Ÿé<C5B8>©â‚?閿燂æ‹?0
|
||
}
|
||
|
||
// 闂備浇é?•Ñ…æ<E280A6>¢éŽ°î…§æ®½éŸ«å›¨ç¨‘ç» îˆžæŸ¨é<C2A8>•å‚›å??16é—‚å‚™ç¤<C3A7>鎼ˇé?<3F>ï¹€ç–¾æ¿ å©‚æ‡<C3A6>é<EFBFBD>?欓柟æ<C5B8>ˆå‰§ç•±é–ºå‹©å„²æ·‡å©‡å¦¶é<C2B6>›î…Ÿæ®œé—<C3A9>ç?¿îƒ†éޏæ<C2B8><C3A6>埥澶娾枎濡îˆ<C3AE>å´µé<C2B5>†î…Ÿä¿Šé<C5A0><C3A9>î<EFBFBD>„â‚?æ ?崹鎶芥倿閿斿墽é<C2BD>?æ¬<C3A6>鑸é<C2B8>›å§ˆé–¸ã‚†åž¿é<C2BF>Œã‚‰å´?銉︽毄é—<C3A9>é<EFBFBD>›ç‰œé<C593>£ï½…î<E280A6><C3AE>é“<C3A9>规兜閸滀ç¤<C3A7>娈æ„?┑é<E28098>˜î‚£å™°é–¸å??æ<>‡é<E280A1>Œï½‰æ‚©é<C2A9><C3A9>æ<EFBFBD>’î<E28099>Žé—<C3A9>哥姵鎹囬崺鈧î„<C3AE>ã<EFBFBD>„鎺嶈兌閳洘銇勯妸銉Ч濞e洤锕î<E280A2>ˆã€<C3A3>å?—€æ¿?â‚?閻樺ç£?鈧剟å?Šè™¹æ†´é<C2B4>•é<E280A2>›æ™±é—<C3A9>哥姵宀æ<E282AC><C3A6>幆宀å‹?晸閿燂拷
|
||
int hexStringToBytes(const char *hexStr, unsigned char *bytes, size_t bytesSize) {
|
||
size_t hexLen = strlen(hexStr);
|
||
if (hexLen % 2 != 0) {
|
||
zlog_error(zct, "hexLen:%d is odd", hexLen);
|
||
return -1; // Hex闂備浇é?•х€涒æ™<C3A6>ç»?欓幒妞尖å<E28093>“é<E2809C><C3A9>醇閵å?˜å–—é<E28094>…ç‚´ç¹›æ<E280BA>ˆå‰§åˆ°æ¿ €é–?å¶…ç…<C3A7>閺冨牊é<C5A0>“涢悘é<CB9C><C3A9>é?<3F>嚙閸旀ç²<C3A7>鎱ㄩæ•<C3A6>é<EFBFBD>¡æ?€î<E282AC>’妤犵å<C2B5>žé<C5BE>—æ?€îŸ‡è¹‡æ¶˜ç?ˆç‘œå¿›å¼³é<C2B3>˜ç”µç£½å¨´ï½‰æ½§æ¿?â‚?閻忓繑é<E28098>Ÿâ•…ç<E280A6>®è¹‡æ¶™å<E284A2><C3A5>é<EFBFBD> 囧弬銊╂煥閺傚ç<C5A1><C3A7>寤洪柨é<C2A8>‡ç‚²â‚¬å½’æ‚¡é<C2A1>‡ãˆ¡å€µé–¿æ¿†é?¸éªžæ ?柣鎾æ<C2BE>‘å?<3F>閺岋ç¹<C3A7>宕é<E280A2><C3A9>é<CB9C>”奉伓
|
||
}
|
||
if (bytesSize < hexLen / 2) {
|
||
zlog_error(zct, "bytesSize:%d < hexLen:%d", bytesSize, hexLen);
|
||
return -1; // 闂備浇é?•х€涒æ™<C3A6>ç»?欓幒妞尖å<E28093>“é<E2809C><C3A9>â•<C3A2>ç·žé<C5BE>Žï½‚æ™<C3A6>ç» æ°?銈嗘尪閸ㄥ綊å??告笟鈧弻é<C2BB>”å°<C3A5>枔閸喗é<E28094><C3A9>撻梺娲è?²å¹–椤æˆ<C3A6>å??婚æ•<C3A6>澶娢╅柕澶å <C3A5>労娴犲ジ姊洪棃娑æ¬<C3A6>悙婵炲眰é<C2B0>”嶇粋宥å’<C3A5>潰瀹€鈧悿鈧柟鑹版彧ç¼<C3A7>æ<EFBFBD>’潡骞å?Žã<C5BD><C3A3>鎺撶厽é—<C3A9>斥晛é<E280BA>Ÿî‚¤ç²?鎺æ?<3F>ç…•æ¿¡îˆ<C3AE>崵澧紒é<E28099>Œæ°±Ð§æ¥ 炴帡å??î„„å´«é<C2AB>•î<E280A2>„闂佽崵é<C2B5> 撴晶妤冩å?»é–¿ç†ºå§µé<C2B5>“欓柟é?–å——å•?é<>Šîˆšâ”‘é<E28098><C3A9>碘拡娴滎äº?銆佸Δ浣瑰缂佸娉曢å´?æ<>´ã„¦â’’娴e憡é<C2A1>Ÿç‚ºæŸ¤ç‘™å‹?劤闇夌€瑰å??æ¾¹å??弸é<C2B8>ƒå Ÿç…™é<E284A2>Žç”µÇ¹è¢¥é—<C3A9>稿鎸æ<C2B8><C3A6>埥澶娾枎濡îˆ<C3AE>å´µé<C2B5>†îˆšâ”‘é<E28098>˜æ?¿æš¯æ¿¡æ<C2A1>’嫰骞忛敓锟�
|
||
}
|
||
|
||
for (size_t i = 0; i < hexLen; i += 2) {
|
||
unsigned char highNibble = hexCharToByte(hexStr[i]);
|
||
unsigned char lowNibble = hexCharToByte(hexStr[i + 1]);
|
||
bytes[i / 2] = (highNibble << 4) | lowNibble;
|
||
}
|
||
|
||
return 0; // é—‚å‚šå€·é‘³å •å¹ŠéŽ¾ç»˜å<CB9C>¤é–µå¨¾æ™›ç»€å¤<C3A5>柡é<C2A1>¥â•<C3A2>枑閸æ¬<C3A6>繘é<CB9C>Œã„©æ‚¤é<C2A4>Œæ¶˜î<CB9C>¶
|
||
}
|
||
|
||
void stringToHex(const char *str, char *hexStr) {
|
||
while (*str) {
|
||
sprintf(hexStr, "%02x", (unsigned char)*str);
|
||
hexStr += 2;
|
||
str++;
|
||
}
|
||
*hexStr = '\0';
|
||
}
|
||
|
||
void hexToAscii(const char *hexStr, char *asciiStr) {
|
||
int len = strlen(hexStr);
|
||
int i, j = 0;
|
||
|
||
for (i = 0; i < len; i += 2) {
|
||
// 闂備浇宕åž?æ…¨é<C2A8>‰æ‡¨æ´ªåŸ¡é<C2A1>œä½¹æ™ªé<C2AA>Ÿæ»„垿濡甸幇é<E280A1>‰è·¨å€žé—<C3A9>é<EFBFBD>›Ä›éˆ§î„„弸é<C2B8><C3A9>î…Ÿâ’‘é—‚å Ÿç¨“æ¾§æ›¢æ‚—å?˜î‚¢åµ†ç€¹æ› 數鈧綆é<E280A0> æ?…å´<C3A5>é<EFBFBD>?ç?¾é–¹å¯¸å<C2B8>Ÿé޳æ„ç¹›é<E280BA>«ç†’戦妵é<C2B5>•ç?£æ¿ å©‚æ‡<C3A6>é<EFBFBD>›ãˆ¤æ¢ºç¼<C3A7>樹緱閸犳岸骞戦崟顖涙å„?閻熸瑥瀚烽å´?é<>?ユ⒒娴e懙é<E284A2>¦î…Ÿå´²é–¹è¾¾ç®‘纾å<C2BE>—柣鎾崇ç˜<C3A7>瑜版帒骞㈡繛鎴烆焽椤斿î˜?é¡?å†?妶æ<C2B6>´â€²èˆªé—<C3A9>告瑢é<C2A2>‹æ’¶ç´“æµ£æ<C2A3>’亾閻åº?綆é<E280A0> 楅悡娑æ¨<C3A6>å½’æ•<C3A6>é<EFBFBD>›î†½å–<C3A5>é—<C3A9>åœ?æ™?濞€閺岋ç¹<C3A7>宕é<E280A2><C3A9>é<CB9C>”å?‰ä¼“
|
||
int byte;
|
||
sscanf(&hexStr[i], "%2x", &byte);
|
||
// 闂備浇é?•Ñ…æ<E280A6>¢éŽ°î†½å´²é–¹é‚¦å„µå¨‘æ¨·æžŽé–¹æ<C2B9><C3A6>æ«•æ<E280A2>ˆå ¥æ¢ºéŽ¸åº£ç®“æ¤¤ï¸»åž¶å??告笟鈧弻é<C2BB>”风暋閻æ?€ç‰ŠéŽ·è¾©ç´“æµ£é?Žè´°é–¸ï½<C3AF>ç¶<C3A7>å??婚悢é<C2A2>‘樺ç<C2BA>°é—<C3A9>斥晛é<E280BA>Ÿæ‰®ç²£é<C2A3>ƒå Ÿç…Ÿé–»æ¨ºå•¿æ¿®å¤<C3A5>柛é<E280BA>˜è™«å´Œé–¸â”¾å<C2BE>“妞ゆ帒é<E28099>Šå½’弳鈺呮煙閾忣å<C2A3>…ç<E280A6><C3A7>甸柤娲î<C2B2>ƒæ†¾ç€µî†¼îŸ·æ¿¡çƒ½æ•ƒéˆ§î„„å´œé?“㈡⒑閸涘﹤é<C2A4><C3A9>﹂æŸ<C3A6>é<EFBFBD>Ÿä¼´æ‹¤II闂備浇é?•х€涒æ™<C3A6>ç»?欓幒妞尖å<E28093>“é<E2809C><C3A9>醇閵å?˜å–—é<E28094>…為梺璺ㄥæ«<C3A6>閹凤æ‹?
|
||
asciiStr[j++] = (char)byte;
|
||
}
|
||
// æ¿ ç”µå§·é<C2B7><C3A9>æ<EFBFBD><C3A6>崕鎴犵礊閳ь剚銇å‹?å¼´é<C2B4>¡æ¥€î<E282AC>?é–¸æ¬<C3A6>繘é<CB9C>Œï½‰å¹‡é¡•呮毌é—<C3A9>稿鎸æ<C2B8><C3A6>埥澶娾枎濡îˆ<C3AE>å´µé<C2B5>†î…Ÿä¿Šé<C5A0><C3A9>î<EFBFBD>„â‚?æ ?崹鎶芥倿閿斿墽é<C2BD>?æ¬<C3A6>鑸é<C2B8>›å§ˆé–¸ã‚†åž¶éŠ‡å‹¯å¹’éŽ´æ¿ƒå<C692>“é<E2809C>¦î…<C3AE>ç„Šæ¿ é<C2A0>›îƒ†éˆ·æˆ¦æŸ›å©µå——î<E28094>´æ¿¡å<C2A1>‰æ¤½é<C2BD>Œæ¶¢æ‚©é“<C3A9>磳å?žã‚ƒåžºé<C2BA>—犻弫鎾绘晸閿燂拷
|
||
asciiStr[j] = '\0';
|
||
}
|
||
|
||
unsigned char ch2hex(char ch) {
|
||
static const char *hex = "0123456789ABCDEF";
|
||
for (unsigned char i = 0; i != 16; ++i)
|
||
if (ch == hex[i]) return i;
|
||
return 0;
|
||
}
|
||
|
||
|
||
char *solve(char *dest, const char *src) {
|
||
int i = 0;
|
||
int cnt = 0;
|
||
unsigned char *d = (unsigned char *)dest;
|
||
while (*src) {
|
||
if (i & 1) {
|
||
d[cnt++] |= ch2hex(*src);
|
||
} else {
|
||
d[cnt] = ch2hex(*src) << 4;
|
||
}
|
||
src++;
|
||
i++;
|
||
}
|
||
return dest;
|
||
}
|
||
|
||
void swap(char *data) {
|
||
int tmp;
|
||
tmp = data[1];
|
||
data[1] = data[0];
|
||
data[0] = tmp;
|
||
}
|
||
|
||
int OpenWatchDog() {
|
||
int fd = -1;
|
||
InitGpio(GlobalConfig::GPIO_G.hardWatchDog, 1);
|
||
gpio_set(GlobalConfig::GPIO_G.hardWatchDog, 1); //婵犲痉é<E280B0>‰åº“å<E2809C>“é<E2809C>‡ãˆ ç£¹ç‘œç‰ˆå·»éˆ§î„Šç®“å®•å •î”—éˆ§î„„å¼¸æ¸šâ‚?é<>Œç†¼æ¢»çº¾å¬?厡é<C2A1>Žè§„挷绶氶弻è?‘ç» æ¶¢æ•<C3A6>é<EFBFBD>›î„‚凹缂備胶æ¿?电敮锟犲蓟閳â•<C3A2>å•°é<C2B0>Ÿç‘°â”‘é<E28098>˜æ<CB9C>’暙椤忥æ‹?
|
||
if (0 > (fd = open("/dev/watchdog", O_WRONLY))) {
|
||
zlog_error(zbt, "fail to open /dev/watchdog");
|
||
}
|
||
return fd;
|
||
}
|
||
|
||
int WriteWatchDog(int fd) {
|
||
if (1 != write(fd, "0", 1)) {
|
||
zlog_error(zbt, "fail to feed watchdog");
|
||
return 1;
|
||
}
|
||
return 0;
|
||
}
|
||
|
||
std::string TrimStringLeft(const char *szData, const char *szTargets) {
|
||
std::string strData = szData;
|
||
int nPos = 0;
|
||
nPos = strData.find(szTargets);
|
||
while (nPos == 0) {
|
||
strData.erase(nPos, 1);
|
||
nPos = strData.find(szTargets);
|
||
}
|
||
return strData;
|
||
}
|
||
|
||
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)) {
|
||
strData.erase(nPos, 1);
|
||
nPos = strData.rfind(szTargets);
|
||
}
|
||
return strData;
|
||
}
|
||
|
||
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);
|
||
if (nFirstPos < 0) return strParam.c_str();
|
||
}
|
||
|
||
int nSecondPos = strTemp.find(szSeparate, nFirstPos + 1);
|
||
if (nSecondPos < 0) {
|
||
strParam = strTemp.substr(nFirstPos + 1);
|
||
} else {
|
||
strParam = strTemp.substr(nFirstPos + 1, nSecondPos - nFirstPos - 1);
|
||
}
|
||
|
||
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) { //æ¿ ç”µå§·é¡£è—‰Î£é<C2A3>›æ<E280BA>‘磻閳ь剟é<C5B8>Œæ¶šâ‚?nå<C5BD>…ç<E280A6><C3A7>扮紒ç¼<C3A7>樼洴瀵爼骞å¬?é<CB9C><C3A9>æ<EFBFBD>’闂佽崵é<C2B5> æ„?úé<C2BA><C3A9>涘┑é<E28098>¡æ¬<C3A6>殾é—<C3A9>挎繂顦伴弲é<C2B2>Œãƒ¦ç…•é–³â•<C3A2>å–šé<C5A1>’æ´?柨é<C2A8>‡ç‚²â‚¬å½’æ‚¡é<C2A1>‡ç†¸å<C2B8><C3A5>濡æ<C5A1>«éˆ·æ—ˆæŸ£é”<C3A9>å œå<C593>«é–¹å<C2B9>‰æ‚‚鎮ч崼éŠ<C3A9>犲绩闂佽é<E28093> æ› åˆ’å¨†æ’³æž<C3A6>閹剧粯é<C2AF>…æ<E280A6><C3A6>柨é<C2A8>•å‚›å??
|
||
zlog_error(zbt, "tcgetattr fail");
|
||
return -1;
|
||
}
|
||
bzero(&newtio, sizeof(newtio));
|
||
newtio.c_cflag |= CLOCAL | CREAD;
|
||
newtio.c_cflag &= ~CSIZE;
|
||
|
||
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) {
|
||
case 'O':
|
||
newtio.c_cflag |= PARENB;
|
||
newtio.c_cflag |= PARODD;
|
||
newtio.c_iflag |= (INPCK | ISTRIP);
|
||
break;
|
||
case 'E':
|
||
newtio.c_iflag |= (INPCK | ISTRIP);
|
||
newtio.c_cflag |= PARENB;
|
||
newtio.c_cflag &= ~PARODD;
|
||
break;
|
||
case 'N': newtio.c_cflag &= ~PARENB; break;
|
||
default:
|
||
zlog_error(zbt, "invalid event:%c", nEvent);
|
||
break;
|
||
}
|
||
|
||
switch (nSpeed) //闂備浇宕åž?慨宕囩矆娴hî<CB86>‰å¨…犲ùé<C2B9>˜å·?å„<C3A5>閸嬵亪é<C2AA>Œæ¶¢åŸ„é<E2809E><C3A9>︿粶é—<C3A9>哄é<E280A0>—犻弻é<C2BB>‡ï¼„ç–€é<E282AC>Žï½Žäºžæµ 煎銈冨劜椤ㄥ﹪å¯?婚æ•<C3A6>é<EFBFBD>›î<E280BA>„牚é—<C3A9>å½’å<E28099>Šé<C5A0>˜å¥¸î””锟ï¿?
|
||
{
|
||
case 2400:
|
||
cfsetispeed(&newtio, B2400);
|
||
cfsetospeed(&newtio, B2400);
|
||
break;
|
||
case 4800:
|
||
cfsetispeed(&newtio, B4800);
|
||
cfsetospeed(&newtio, B4800);
|
||
break;
|
||
case 9600:
|
||
cfsetispeed(&newtio, B9600);
|
||
cfsetospeed(&newtio, B9600);
|
||
break;
|
||
case 115200:
|
||
cfsetispeed(&newtio, B115200);
|
||
cfsetospeed(&newtio, B115200);
|
||
break;
|
||
case 460800:
|
||
cfsetispeed(&newtio, B460800);
|
||
cfsetospeed(&newtio, B460800);
|
||
break;
|
||
default:
|
||
cfsetispeed(&newtio, B9600);
|
||
cfsetospeed(&newtio, B9600);
|
||
zlog_error(zbt, "invalid speed:%d, use B9600", nSpeed);
|
||
break;
|
||
}
|
||
if (nStop == 1) //闂備浇宕åž?慨宕囩矆娴hî<CB86>‰å¨…犲ùé<C2B9>˜å·?å„<C3A5>閸嬵亪é<C2AA>Œæ¶¢åŸ„é<E2809E><C3A9>î<EFBFBD><C3AE>æ?ˆç¼<C3A7>惧墽ç»?æ<C593>¢å©µå›©ç¯€é–¸å±¾å‡¹æµ¼å²„梺é<C2BA>›å©ƒå°°é–¸åº¢å¨Šå©€ä¾€æ¢ºéŽ¸åº£ç®“ç¼<C3A7>绘垹鈧î„<C3AE>熬鎷�
|
||
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) {
|
||
zlog_error(zbt, "tcsetattr fail");
|
||
return -1;
|
||
}
|
||
return 0;
|
||
}
|
||
|
||
void IniReadValue(const char *section, const char *key, char *val, const char *file) {
|
||
FILE *fp;
|
||
int i = 0;
|
||
int lineContentLen = 0;
|
||
int position = 0;
|
||
char lineContent[LINE_CONTENT_MAX_LEN];
|
||
bool bFoundSection = false;
|
||
bool bFoundKey = false;
|
||
fp = fopen(file, "r");
|
||
if (fp == NULL) {
|
||
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);
|
||
if ((lineContent[0] == ';') || (lineContent[0] == '\0') || (lineContent[0] == '\r') || (lineContent[0] == '\n')) {
|
||
continue;
|
||
}
|
||
|
||
// check section
|
||
if (strncmp(lineContent, section, strlen(section)) == 0) {
|
||
bFoundSection = true;
|
||
// printf("Found section = %s\n", lineContent);
|
||
while (feof(fp) == 0) {
|
||
memset(lineContent, 0, LINE_CONTENT_MAX_LEN);
|
||
fgets(lineContent, LINE_CONTENT_MAX_LEN, fp);
|
||
// check key
|
||
if (strncmp(lineContent, key, strlen(key)) == 0) {
|
||
bFoundKey = true;
|
||
lineContentLen = strlen(lineContent);
|
||
// find value
|
||
for (i = strlen(key); i < lineContentLen; i++) {
|
||
if (lineContent[i] == '=') {
|
||
position = i + 1;
|
||
break;
|
||
}
|
||
}
|
||
if (i >= lineContentLen) break;
|
||
strncpy(val, lineContent + position, strlen(lineContent + position));
|
||
lineContentLen = strlen(val);
|
||
for (i = 0; i < lineContentLen; i++) {
|
||
if ((lineContent[i] == '\0') || (lineContent[i] == '\r') || (lineContent[i] == '\n')) {
|
||
val[i] = '\0';
|
||
break;
|
||
}
|
||
}
|
||
} else if (lineContent[0] == '[') {
|
||
break;
|
||
}
|
||
}
|
||
break;
|
||
}
|
||
}
|
||
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, const char *key, char *val, const char *file) {
|
||
zlog_info(zct, "section = %s, key = %s, file = %s", section, key, file);
|
||
if (section == NULL || key == NULL || val == NULL || file == NULL) {
|
||
zlog_error(zct, "input parameter, section:%s, key:%s, val:%s, file:%s exist NULL", section, key, val, file);
|
||
return -1;
|
||
}
|
||
char sect[SECTION_MAX_LEN];
|
||
memset(sect, 0, SECTION_MAX_LEN);
|
||
sprintf(sect, "[%s]", section);
|
||
IniReadValue(sect, key, val, file);
|
||
return 0;
|
||
}
|
||
|
||
int readIntValue(const char *section, const char *key, const char *file) {
|
||
char strValue[STRVALUE_MAX_LEN];
|
||
memset(strValue, '\0', STRVALUE_MAX_LEN);
|
||
if (readStringValue(section, key, strValue, file) != 0) {
|
||
return 0;
|
||
}
|
||
return (atoi(strValue));
|
||
}
|
||
|
||
void IniWriteValue(const char *section, const char *key, char *val, const char *file) {
|
||
FILE *fp;
|
||
char lineContent[LINE_CONTENT_MAX_LEN];
|
||
char strWrite[LINE_CONTENT_MAX_LEN];
|
||
bool bFoundSection = false;
|
||
bool bFoundKey = false;
|
||
int err = 0;
|
||
memset(lineContent, '\0', LINE_CONTENT_MAX_LEN);
|
||
memset(strWrite, '\0', LINE_CONTENT_MAX_LEN);
|
||
sprintf(strWrite, "%s=%s\n", key, val);
|
||
fp = fopen(file, "r+");
|
||
if (fp == NULL) {
|
||
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);
|
||
if ((lineContent[0] == ';') || (lineContent[0] == '\0') || (lineContent[0] == '\r') || (lineContent[0] == '\n')) {
|
||
continue;
|
||
}
|
||
// check section
|
||
if (strncmp(lineContent, section, strlen(section)) == 0) {
|
||
bFoundSection = true;
|
||
while (feof(fp) == 0) {
|
||
memset(lineContent, 0, LINE_CONTENT_MAX_LEN);
|
||
fgets(lineContent, LINE_CONTENT_MAX_LEN, fp);
|
||
// check key
|
||
if (strncmp(lineContent, key, strlen(key)) == 0) {
|
||
bFoundKey = true;
|
||
printf("%s: %s=%s\n", __func__, key, val);
|
||
fseek(fp, (0 - strlen(lineContent)), SEEK_CUR);
|
||
err = fputs(strWrite, fp);
|
||
if (err < 0) {
|
||
printf("%s err.\n", __func__);
|
||
}
|
||
break;
|
||
} else if (lineContent[0] == '[') {
|
||
break;
|
||
}
|
||
}
|
||
break;
|
||
}
|
||
}
|
||
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, const char *key, char *val, const char *file) {
|
||
// char sect[SECTION_MAX_LEN];
|
||
zlog_debug(zct, "section = %s, key = %s, file = %s", section, key, file);
|
||
if (section == NULL || key == NULL || val == NULL || file == NULL) {
|
||
zlog_error(zct, "input parameter(s) is NULL!");
|
||
return -1;
|
||
}
|
||
// memset(sect, 0, SECTION_MAX_LEN);
|
||
// sprintf(sect, "[%s]", section);
|
||
IniWriteValue(section, key, val, file);
|
||
return 0;
|
||
}
|
||
|
||
int writeIntValue(const char *section, const char *key, int val, const char *file) {
|
||
char strValue[STRVALUE_MAX_LEN];
|
||
memset(strValue, 0, STRVALUE_MAX_LEN);
|
||
sprintf(strValue, "%d", val);
|
||
writeStringVlaue(section, key, strValue, file);
|
||
return 0;
|
||
}
|
||
|
||
int getDiskInfo(char *diskTotal, char *diskFree) {
|
||
DISK diskInfo;
|
||
/* 1.闂傚倷绀侀崥瀣磿閹惰æ?„æ<E2809E>´å©‡æŸ¤é‘¹æ‰®å ?娴滃綊é<C5A0>Œã„©æ‚¤é<C2A4>Œæ¶˜î<CB9C>?/home/婵犵數é<C2B8>‹ç‚ºå´¹é<C2B9>?曞ç?°é–¹é—´ç·¡é<C2A1><C3A9>勫璺衡看閻掕棄霉閻撳海鎽犻柛æ<E280BA>´ï¼„Ч閺屾ç›?å¯?æ’?妸銉ュç?£é—‚佺ǹé?‘å—›å¹<C3A5>楣冨ç??閻樺樊é<C5A0>Ÿå‘?ã<>„é<E2809E>‚åž?悑濞å‘?ç§¹å?Šå?šå´’娆愮グå?žã‚†æ´˜ç»®æ’»ç<C2BB>®è¹‡æ¶™æ™¸é–¿ç‡‚æ‹· */
|
||
statfs("/", &diskInfo);
|
||
unsigned long long blocksize = diskInfo.f_bsize;
|
||
unsigned long long totalsize = blocksize * diskInfo.f_blocks;
|
||
|
||
|
||
/* 2.闂傚倷绀侀崥瀣磿閹惰æ?„æ<E2809E>´å©‡æŸ¤é‘¹æ‰®å ?娴滃綊é<C5A0>Œæ¶¢å¦·é”<C3A9>å‘îž‹é—<C3A9>崇粯å¦?å†?å¼»å?¥å «æª¨é—<C3A9>告挻姘ㄧ划娆æ„?ç·žé<C5BE><C3A9>炵ǹ浜鹃柨婵嗙凹ç¼<C3A7>ㄥé<C2B7>Œå¶‰æŸ¨ç€£î„€â…¹é—‚囧é<C2B5>Œã„¥â”‘é<E28098>¡æ©†æ£¤é—<C3A9>瑰啿鎳橀弻å?¥å›¨å–†é–¸æ›¨åŽ¼éˆ·å±½â”‘é?”硷工椤嘲顕i崼é<C2BC>‡ç‚µî…ºé—<C3A9>绘劕妯婃å?¼é<C2BC>›å˜â’’娴e憡é<C2A1>Ÿç‚ºæŸ£é<C2A3>•佸ç<C2B8>¦ç€¹æ›žç¶Šéªžåº¨æŒŠæ¾¶å??â‚?梺闈涚墕濞层劑éŽ?å±½î<C2BD><C3AE>顓犵é<E280A2>Žç‘°å?Šé<C5A0> æ› èŠ±æ¿‚å‘Šç…¥æ¿žæˆžè‰¾é<C2BE>‹æ¶¢æŸ¡å®€å¬?ç§?婵℃悂濡烽妷顔绘å<CB9C>…闂備線娼уú銈å<CB86>¤æ´ªå¦¸é””绢洸é—<C3A9>归棿é<C2BF>’﹂弲鎼佹煥閻曞倹瀚� */
|
||
unsigned long long freeDisk = diskInfo.f_bfree * blocksize; //闂傚倷绀侀幉锟犲ç?°é–¹ç»¢å––é<E28093>¤ç‚¬ç¹›é<E280BA>¡æ¨ºç<C2BA>©ç¼<C3A7>â‚¬æ¿ å›¨ç…™é<E284A2>Žç”µÇ¹å•¸é—<C3A9>活厽纰嶇æ<E280A1>¢å¨‘橆啅椤旇崵é<C2B5>‘å½’æ¢ºéŽ¸åº£ã€’é–¸çŠ³ç‰ å¯?婚æ•<C3A6>澶娢╅柕澶å <C3A5>労娴犲ジ姊洪棃娑æ¬<C3A6>悙婵炲眰é<C2B0>”嶇粋宥å?Šç?¹å¨´ï½ˆî<CB86>‰å¨…㈤梺璺ㄥæ«<C3A6>閹凤æ‹?
|
||
// unsigned long long availableDisk = diskInfo.f_bavail * blocksize; //闂傚倷绀侀幉锟犳å<C2B3>¡æ¤¤æ «å<C2AB>›é<E280BA>¨å‚žæŸŸéŽ?版閺å?©ç·šé<C5A1>Œæ›¢å´¼å©µå›§çª›é—<C3A9>活厽纰嶇æ<E280A1>¢å¨‘橆啅椤旇崵é<C2B5>‘归梺鎸庣〒閸犲酣鈥﹂崸妤â‚?閱囬柛鈩冾殔閺嗙å–<C3A5>鎱ㄩæ•<C3A6>蹇斿î<C2BF>?
|
||
|
||
sprintf(diskTotal, "%llu", totalsize >> 20);
|
||
sprintf(diskFree, "%llu", freeDisk >> 20);
|
||
return 1;
|
||
}
|
||
|
||
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) {
|
||
sum += *w++;
|
||
nleft -= 2;
|
||
}
|
||
if (nleft == 1) {
|
||
*(unsigned char *)(&answer) = *(unsigned char *)w;
|
||
sum += answer;
|
||
}
|
||
sum = (sum >> 16) + (sum & 0xffff);
|
||
sum += (sum >> 16);
|
||
answer = ~sum;
|
||
return answer;
|
||
}
|
||
|
||
int socketHeart(const char *pSendData) {
|
||
zlog_info(zct, "socketHeart");
|
||
int sockfd; // Socket闂傚倷绀侀幖é?<3F>﹀磹ç¼<C3A7>å¬?5娲î<C2B2>…晲閸涱äº<C3A4>é<EFBFBD>Žå©šæ¢ºé—ˆæ¶šç®žé–¸å©ƒæ´œç»?婚悽é<C2BD><C3A9>ュ彄é—<C3A9>æ<EFBFBD>žå„¯é<C2AF>”忔竟å?—€é<C2AC>Œï½…Î”æµ£åœæ®¤ç¼‚佽鲸ç”?掕ç<E280A2>ƒé–»åº¯ç¶†é<E280A0>‹å‹?锟ï¿?
|
||
struct sockaddr_in serverAddr {}; // Server闂傚倷绶æ°?îŸ‡æ¿ æ°?⒔閸曨å<C2A8>’é<E28099>”嗘俊é?–欒閻æŽ<C3A6>ç²™é<E284A2>Œæ¶¢å¹‡é<E280A1><C3A9>哥敖ç¼<C3A7>炬儳銈æ<CB86><C3A6>å¼»é<C2BB>”煎箚瑜滈崵é<C2B5>”兼煃瑜滈崜锕傚垂é—<C3A9>î„€ç§´ç» îˆžæŸ›å¨‘æ?¼æ‘ 閺呮悂é<E2809A>Œã„©æ‚¤é<C2A4>Œæ¶˜î<CB9C>?
|
||
|
||
// 闂傚倷绀侀幉锛å‹?暜濡ゅ啰é<C2B0>?欓柟瀵ç?¿Ð¥ç»¾å<C2BE>¥ç²™é<E284A2>Œç†ºç´’妯肩潉cket
|
||
if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
|
||
zlog_error(zct, "Failed to create socket.");
|
||
return 1;
|
||
}
|
||
|
||
// 闂備浇宕åž?慨宕囩矆娴hî<CB86>‰å¨…犲ùé<C2B9>˜å·?å„<C3A5>閸嬵亪é<C2AA>Œæ¶˜â”‘é<E28098>«æ¿ˆâ‚¬ç‘€ver闂傚倷绶æ°?îŸ‡æ¿ æ°?⒔閸曨å<C2A8>’é<E28099>”嗘俊é?–欒閻æŽ<C3A6>ç²™é<E284A2>Œæ¶¢å¹‡éŠŠï¸½ç<C2BD>–é—<C3A9>崇懓绉归弻宥å?Šç…›å¨´ï½…憡娈ㄧ紓浣瑰敾閹凤拷
|
||
serverAddr.sin_family = AF_INET;
|
||
serverAddr.sin_port = htons(18393); // TCP婵犳é<E28093> æ¥ƒæ•®å¦¤å†ªçŸ™é–¹çƒ˜æŒ¾æµ æ°ä¿Šé<C5A0><C3A9>î<EFBFBD>„€æ ?幑浣哥暦閻㈤潧é<C2A7>¨æ¿‹æŸ<C3A6>é<EFBFBD><C3A9>勬媼閺佸秵鎱ㄥî<C2A5>œé<C593>¡æ¥€î<E282AC>?缂佸顦é<C2A6>›î<E280BA><C3AE>娲î<C2B2>„å·<C3A5>婵犲å??é<>£îˆšâ”‘é<E28098><C3A9>å˜å°æ¿ €é–?å¶…ç??瑜版帗é<E28094>…æ<E280A6><C3A6>柨é<C2A8>•å‚›å«?80
|
||
inet_pton(AF_INET, "192.168.1.147", &serverAddr.sin_addr);
|
||
|
||
// é—‚å‚™ç¤<C3A7>鎼ˇé?<3F>ï¹€ç–¾æ¿ é<C2A0>›îƒ…纾å?šæŸ£éŽ°î„‚ä»›é–ºå—˜ç²“é<E2809C>Œç†ºç´’éŠ<C3A9>犳ç<C2B3>ˆç¼‚ä½²å<C2B2>“閸℃稒é<E28099>“熺憸æ<C2B8>´ï½†î<E280A0>•绾炬潳er
|
||
if (connect(sockfd, reinterpret_cast<struct sockaddr *>(&serverAddr), sizeof(serverAddr)) == -1) {
|
||
zlog_error(zct, "Failed to connect to the server.");
|
||
close(sockfd);
|
||
return 1;
|
||
}
|
||
|
||
// 闂傚倷绀侀幉锟犳å<C2B3>¡é–¿æ›žå€¸é<C2B8>¨å‚žæŸ›è¤Žé¡¨å‘´æ‚žé<C5BE>?î‚ äºœé–¹è¾¾çµ¾çºèˆµã<C2B5>„锔煎î›?ç¼<C3A7>辨帡宕崟é¡<C3A9>熸寖闂佸湱鎳撶â‚?æ°?å<>³éªžæ ?ã<><C3A3>鎺濇æ™<C3A6>é—<C3A9>挎繂妫崯鈧梻é<C2BB>Œæ¬’兌椤㈠ï¹?顢氶弽é?“炵ç<C2B5>¥é—<C3A9>å“„ç¨<C3A7>é<EFBFBD>‹â‚¬é–¸å??挾é<C2BE> 婃径瀣î<C2A3><C3AE>ä¼?
|
||
|
||
ssize_t bytesSent = send(sockfd, pSendData, strlen(pSendData), MSG_NOSIGNAL);
|
||
if (bytesSent == -1) {
|
||
zlog_error(zct, "Failed to send heartbeat packet.");
|
||
close(sockfd);
|
||
return 1;
|
||
} else if (static_cast<size_t>(bytesSent) != strlen(pSendData)) {
|
||
zlog_error(zct, "Partially sent heartbeat packet.");
|
||
close(sockfd);
|
||
return 1;
|
||
}
|
||
|
||
// 闂傚倷鑳舵ç<C2B5>™ç¼‚佺ç²?é<>¨å‰<C3A5>æ<EFBFBD>¢å¨‘欑節閸å??å§¤ç»‹æ’»å´°æ¿ å›§ç…›é–»æ»…â‚¬æ”…té—‚å‚™ç¤<C3A7>鎼ˇé?<3F>ï¹€ç–¾æ¿ é<C2A0>›îƒ…纾å?šæŸ£éŽ°î„‚ä»›é–ºå—˜ç²“é<E2809C>Œã„©æ‚¤é<C2A4>Œæ¶˜î<CB9C>¶
|
||
close(sockfd);
|
||
return 0;
|
||
}
|
||
|
||
// Ping闂傚倷绀侀幉锟犲垂閸忓å<E2809C>‹é<E280B9>™å¿›æŸ•é<E280A2>?æ¿<C3A6>槸æ¿?è§„ç…¡é<C2A1>Œï½‰å¼?é<>?å€?é—<C3A9>å“„æ‹‹é<E280B9>“ç†¼å¹ƒå¦¤å‘æ<C2AD>¶æ¤¤æ’¶å<C2B6>˜å§˜î›“eout婵犵數é<C2B8>‹ç‚ºå´¹é<C2B9>?曞ç?²é–¸ãƒ¢æ™<C3A6>纾芥慨妯夸å<C2B8>«é–»î…Ÿæ<C5B8>‡é<E280A1>Œç†ºç´’éŠ<C3A9>犳ç<C2B3>ˆç¼‚侇å<E280A1>„绉归弻銈囩矙é<E284A2> æ<C2A0>’劋绮é<C2AE>›îœƒéŠ‡îˆ™ç¤<C3A7>娲﹂埛鎺æ?<3F>ç…•é<E280A2><C3A9>ç‚²å¢½ç» æ ?憸鎶婂æ‡<C3A6>纾奸弶é<C2B6>«æ¶˜å?¿ç¼<C3A7>犵å<C2B5>Ÿéˆ§î„ƒî<C692>‰å¨²â•ƒæ<C692>¢å©µå—©åš•閺夋埈娼╅弶é<C2B6>«æ°?暕閵忋倖鈷戦柛婵嗗î<E28094>©æ¤¤å¿£å<C2A3>…淇婇銈庢敵,10000 æ¿ ç”µå§µé?”æ å´³é?–滅礊閸℃稑纾å?šæŸ›éˆ©å†¨å–•ç¼‚å¶†ç‰ é<C2A0>Œã„©æ‚¤é<C2A4>Œæ¶˜î<CB9C>¶=10 缂傚倸é<C2B8>Šé£Žç²ˆæ¸šâ‚¬ç¯“閳ь剟é<C5B8>Œç†¼å´™éŠˆå——î<E28094>?
|
||
//é—‚å‚šå€·é‘³å •å¹ŠéŽ¾ç»˜å<CB9C>¤é–µå¨¾æ™›ç»€å¤<C3A5>柡é<C2A1>¥â•<C3A2>枑閸æ¬<C3A6>繑绻涢幋娆忕仾é—<C3A9>å“„æ‹‹é<E280B9>“氶幈銊ノ熼æ<C2BC>¹é<C2B9>§å“¥ç¤Šç¼‚備胶æ¿?甸悡锟犲蓟濞戙垹å”?妞ゆ牗绋戦锟�0闂傚倷é<C2B7>’︾€笛呯矙閹达附é<E2809E>¤æ„ã<C2AD>„é<E2809E><C3A9>î‚?仜å?—呴梺é<C2BA>Ÿé‚¦åšŸå©µî†¾æ•¸éˆ§î„<C3AE>艾é¡?烽弻é<C2BB>‡ç†·ç·žæ¿¡îˆ<C3AE>æ?£æµ 紓浣瑰姈椤ㄥ﹤顕i崼é<C2BC>‡ç‚µåޏé—<C3A9>ç?¿æœ¬ç»‹æ’»å´£é<C2A3>¡æ¤»æ”½é–»æ„°î–‚é<E2809A>¤å¬?柟é<C5B8>‘ゆå??1é—‚å‚šå€·é‘³å •ã€<C3A3>æ¿ å›§ç?µæ¤¤å¿“ç‰†ç» îˆžæŸ¨é<C2A8>•å‚›å??-1
|
||
int Ping(const char *ips, int timeout) {
|
||
struct timeval *tval;
|
||
int maxfds = 0;
|
||
fd_set readfds;
|
||
|
||
int iRet = 0;
|
||
struct sockaddr_in addr;
|
||
struct sockaddr_in from;
|
||
// 闂備浇宕åž?慨鎶芥倿閿曞倸纾å<C2BE>—柟éŽ?æ<>¿Ð“閸嬧æ™<C3A6>绱撳æ<C2BC>´ãˆ¡î<C2A1>£å©µçŠµæ•¸é<C2B8>Žæˆ 徊钘å?<3F>洪悩璇èŒ?î˜?婵犻潧娲らé–?嶆煥閻曞倹瀚�
|
||
bzero(&addr, sizeof(addr));
|
||
addr.sin_family = AF_INET;
|
||
addr.sin_addr.s_addr = inet_addr(ips);
|
||
|
||
int sockfd;
|
||
// 闂傚倷绀侀幉锟犳å<C2B3>¡é–¿æ›žå€¹é<C2B9>‹å¬?柡é<C2A1>¥â•<C3A2>У椤æ„?ç²™é<E284A2>Œï½‰î””é<E2809D>›å‚›æºˆket 闂傚倷绶æ°?è?<3F>èžžé–¹ç»¢å––ç» îˆžæŸ¨é<C2A8>•å‚›å«? å©µçŠµæ•¸æ¿®çƒ½ã€‚æµ å¬?焵椤掆å<E280A0>“閸熷潡é<C2A1>©â‚?椤掆å<E280A0>“ç¼‚å¶…ï¹ªéªžå†¨ÎŸç’‡èŒ¬çªžæ¿ ç”µå<C2B5>Ÿé<C5B8>‹æ’»æ‚¡éŠ<C3A9>ゆ⒑閻愯棄é<E2809E>”氶柛é<E280BA>”锋å<E280B9>¥é–¿æ¿ˆå<CB86>¤è·?ç¼<C3A7>犲垼o é—‚å‚™ç¤<C3A7>鎼ˇé?<3F>ï¹€ç–¾æ¿ å©‚ç‰Šé<C5A0>‹ï¿ æŸ<C3A6>é<EFBFBD><C3A9>å‹?噹闂傤垶姊洪崹é?•å‘剰å?žã‚†æ´<C3A6>椴æ<C2B4><C3A6>å¹ˆéŠŠãƒ®æ½¨é–¸â„ƒîš…ç» å½’æ‚¶å?˜î…žå“ºæ¿®å©„粯鎷呯粙璺ㄦî†<C3AE>闂佺懓é<E2809C>¤æ ?å¹<C3A5>锟ï¿?
|
||
sockfd = socket(AF_INET, SOCK_RAW, IPPROTO_ICMP);
|
||
if (sockfd < 0) {
|
||
zlog_error(zct, "ip:%s,socket error", ips);
|
||
return -1;
|
||
}
|
||
|
||
struct timeval timeo;
|
||
// 闂備浇宕åž?慨鎶芥倿閿曞倸纾å<C2BE>—柟éŽ?æ<>¿Ð“閸嬧æ™<C3A6>绱撳é<C2BC>¥îŸ’éª<C3A9>meOut闂傚倷绀侀幖é?<3F>ï¹€ç–?椤愶附é<E2809E>‹å¤<C3A5>柣鎾冲濞戙垺é<C2BA>…æ<E280A6><C3A6>柨é<C2A8>•å‚›å??
|
||
timeo.tv_sec = timeout / 1000;
|
||
timeo.tv_usec = timeout % 1000;
|
||
|
||
if (setsockopt(sockfd, SOL_SOCKET, SO_SNDTIMEO, &timeo, sizeof(timeo)) == -1) {
|
||
zlog_error(zct, "ip:%s,setsockopt error", ips);
|
||
close(sockfd);
|
||
return -1;
|
||
}
|
||
|
||
char sendpacket[2048];
|
||
char recvpacket[2048];
|
||
// 闂備浇宕åž?慨鎶芥倿閿曞倸纾å<C2BE>—柟éŽ?æ<>¿Ð“閸嬧æ™<C3A6>绱撳é<C2BC>¥Ñ…毇ng闂傚倷绀侀幉锟犳å<C2B3>‹æ¤¤æ’±åž¹ç» 柨é<C2A8>•å‚›å«?
|
||
memset(sendpacket, 0, sizeof(sendpacket));
|
||
|
||
pid_t pid;
|
||
// 闂傚倷绀侀幉锟犳å<C2B3>¡é–¿æ›žå€¹é<C2B9>‹å¬?柡é<C2A1>¥â•<C3A2>У椤æ„?ç²™é<E284A2>Œï½‰åŸ¡æ¿ 冿紬D闂傚倷é<C2B7>’︾€笛呯矙閹寸å<C2B8>Ÿé—„勯柡é<C2A1><C3A9>ㄥ€è<C2AC>¤ç²ˆæ¿ 囨煛閸愶絽浜剧紓浣割å„<C3A5>椤﹀å´?顕h閵嗘帗é<E28094>’å©‚îžŠæ¿ å›¨â’’å¨´ï½…æ¹±å©€ä»‹æŸ›é<E280BA>‚è·¨ç„¸ç€¹æ› æ•»éŽ®î‡€æ‚¥å®„ç§‚nce ID
|
||
pid = getpid();
|
||
|
||
struct ip *iph;
|
||
struct icmp *icmp;
|
||
|
||
icmp = (struct icmp *)sendpacket;
|
||
icmp->icmp_type = ICMP_ECHO; //闂傚倷é<C2B7>’﹂幃é<C692>?曞磿閹惰棄纾婚柣鎰棘閻旂ǹ绶為悘é<CB9C><C3A9>æ<EFBFBD>‘劤閻濅å<E280A6>³å§Šç»˜æ‹…é<E280A6><C3A9>ヤ虎妞ゆ垵娲ら敃éŠ<C3A9>ゆ晸閿燂拷
|
||
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)); //闂傚倷绀侀幖é?<3F>ょ矙閸曨厽宕å<E280A2>‰ç¹<C3A7>闈涱å„<C3A5>é–¸å??ï¹¢é<C2A2>Œã„©æ‚¤é<C2A4>Œæ¶˜î<CB9C>?
|
||
|
||
int n = sendto(sockfd, (char *)&sendpacket, sizeof(struct icmp), 0, (struct sockaddr *)&addr, sizeof(addr));
|
||
if (n < 1) {
|
||
zlog_error(zct, "ip:%s,sendto error", ips);
|
||
close(sockfd);
|
||
return -1;
|
||
}
|
||
|
||
// 闂傚倷娴å›?~澶å¬?娴犲纾å<C2BE>—å¼¶é<C2B6>«î‚?亖娴滃綊é<C5A0>Œã„©æ‚¤é<C2A4>Œæ¶˜î<CB9C>¶
|
||
// 闂傚倷é<C2B7>’﹂惇褰掑垂绾懌浜瑰璺虹焷婵櫕銇å‹?幒鎴æ¿<C3A6>仼é—<C3A9>活厽顨嗛妵é<C2B5>•冀閵娧å‘?暤闂佸憡蓱閹告娊å¯?婚悢é<C2A2>“庣畳é—<C3A9>åœ?儤é<C2A4>¨åž?æ…¨æ<C2A8>´ã„¦å<C2A6>¡æ¿ å©‚å«?绶查柣妤佹å°é–»ï½…嘲顫濈æ<CB86>„é“<C3A9>å½’ç<E28099>“闂佸å?Šé<C5A0>“å ¥å´°é<C2B0><C3A9>î‚£î‚½æ¤¤æ’¶å–“ç» é¹ƒæ‚—é<E28094>¢ç™»è?„ç‰Žæ¿ ç”µå§?é<>‹æ’´ã<C2B4>„é”<C3A9>å‘?礆g闂傚倷é<C2B7>’﹂惇褰掑礉瀹€鈧埀顒佸åš?閸撴ç›?鎳為柆å?¥å——殤å?žã‚†å¸’é<E28099>Šå<C5A0>—埞蹇涙⒑閸濆å??é<>†å©‡æŸ›ç€£î‚£å°µç¼<C3A7>辨帗寰å‹ä»¦é’˜å¤Šç?£æ¿¡ã‚‡å§·é<C2B7>‹æ?¼æ?¢å?˜èº²â‚?佸☉éŠ<C3A9>犖ч柛銉㈡櫓濡茬兘å?Šç»˜æ‹…é<E280A6>›å©…ç?€å¦žã‚<C3A3>絼绮æ¬<C3A6>畷鎴﹀箻é<C2BB>¡æŽ<C3A6>ç?<3F>ç¼<C3A7>婇幃鈺å’<C3A5>敊閻撳é?¸è?šç¼‚傚倷鑳å‰<C3A5>划顖炴å<C2B4>‹é–»æ¨¿î‡§é’ƒç†¼æ‚˜é<CB9C><C3A9>åž?櫕閺嗗æ?<3F>é<EFBFBD>Œæ¶¢å¹‡é¡”间壕é—<C3A9>活厽顨婂î<E2809A><C3AE>娲î<C2B2>…倻閳轰æ?’澹曢梺é<C2BA>›å©ƒç…¥é–»î…žå€¿é<C2BF>›î‡€å´±å¦¯è‚©ç˜ˆé—<C3A9>æ<EFBFBD>žå„?é<>”屾禒éŠ<C3A9>ゆ⒑é<E28098> æ<C2A0>’劌娅愰柟é<C5B8>‘ゆå«?
|
||
int cnt = 0;
|
||
while (1) {
|
||
// 闂備浇宕åž?慨鎶芥倿閿曞倸纾å<C2BE>—柟éŽ?æ<>¿Ð“閸嬧æ™<C3A6>绱撳é<C2BC>¥îŸ’éª<C3A9>meOut闂傚倷绀侀幖é?<3F>ï¹€ç–?椤愶附é<E2809E>‹å¤<C3A5>柣鎾冲濞戙垹閿ゆ俊銈å‹?åŠ<C3A5>濞å‘?ç‰ éŽ®æ¥…å´—æ¾¶å©<C3A5>壕闂佸憡é<C2A1>”︽ç¦<C3A7>é<EFBFBD><C3A9>ä¾?敊閹邦兘é<CB9C>€ä»‹æŸ£éŽ°î„€çº§æ¤¤ãƒ¦åž¿é<C2BF>Œæ¶¢å¹˜ç€µå“¥ç•ºé—<C3A9>哄懎é<C5BD>–奸幃鈺冩嫚閹绘帒鎸ゆ俊é<C5A0><C3A9>î<EFBFBD>„â‚?æ ?悧妤冨åž<C3A5>瀹ュî˜?é<>„ラ柛ç<E280BA>žå‰§âЕ閏æ¬<C3A6>繑绻濋崹é¡<C3A9>e暗缂佸é<C2BD>“熼弻é<C2BB>”兼儌閸濄儳蓱闂佺懓é<E2809C>¢æŸ¥å¹Šæ<C5A0>´ã„£å<C2A3>‘娴兼潙宸濇ã<E280A1>„é<E2809E>ƒå›?Г椤撳綊å?Šç»˜æ‹…瑙勩仧é—<C3A9>绘挸顦甸ç<C2B8>®è¹‡æ¶™æ™¸é–¿ç‡‚æ‹·
|
||
FD_ZERO(&readfds);
|
||
FD_SET(sockfd, &readfds);
|
||
maxfds = sockfd + 1;
|
||
n = select(maxfds, &readfds, NULL, NULL, &timeo);
|
||
if (n <= 0) {
|
||
zlog_info(zct, "ip:%s,Time out error", ips);
|
||
close(sockfd);
|
||
iRet = -1;
|
||
break;
|
||
}
|
||
|
||
// 闂傚倷娴å›?~澶å¬?娴犲纾å<C2BE>—å¼¶é<C2B6>«î‚?亖娴滃綊é<C5A0>Œã„©æ‚¤é<C2A4>Œæ¶˜î<CB9C>¶
|
||
memset(recvpacket, 0, sizeof(recvpacket));
|
||
int fromlen = sizeof(from);
|
||
n = recvfrom(sockfd, recvpacket, sizeof(recvpacket), 0, (struct sockaddr *)&from, (socklen_t *)&fromlen);
|
||
zlog_info(zct, "recvfrom Len:%d", n);
|
||
if (n < 1) {
|
||
close(sockfd);
|
||
iRet = 1;
|
||
break;
|
||
}
|
||
|
||
char *from_ip = (char *)inet_ntoa(from.sin_addr);
|
||
// 闂傚倷绀侀幉锛å‹?暜閸ãƒ?剙纾归柡宥庡幖閽å†?å––é<E28093>Œæ¶¢å¦·é¡”ç…Žî<C5BD>©é—<C3A9>告瑥锕ラ妵é<C2B5>•冀閵娧屾æ?¹é—‚佺ǹæ? æ<C2A0><C3A6>敃é?<3F>å <C3A5>蓟閿熺姴绀å¬?梻é<C2BB>«ç†ºã€’娴犳挳姊洪幖é<E28093><C3A9>测å<E280B9>“é<E2809C><C3A9>î‚¢åž<C3A5>瀹€é<C2AC>•垫晩闊洦绋掗崕æ<E280A2>´â‚?亜閿曗å<E28094>“ç¼<C3A7>辨闂傚倷é<C2B7>’﹂惇褰掑礉瀹€鈧埀顒佸åš?閸撴瑧é<C2A7>™å‘´æ¢ºé<C2BA><C3A9>å‹Ð 閸斿瞼娆㈤悙é<E284A2>‘æ?¼å<C2BC>†é—<C3A9>绘洖é<E28093>Šåœ?â‚?氾拷
|
||
if (strcmp(from_ip, ips) != 0) {
|
||
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));
|
||
|
||
zlog_info(zct, "ip:%s,icmp->icmp_type:%d,icmp->icmp_id:%d\n", ips, icmp->icmp_type, icmp->icmp_id);
|
||
// 闂傚倷绀侀幉锛å‹?暜閸ãƒ?剙纾归柡宥庡幖閽å†?å––é<E28093>Œæ¶¢å?€é™‡éŽ¶æ¢Ÿg闂傚倷é<C2B7>’﹂幃é<C692>?曞磿閹惰棄纾绘繛鎴旀åš<C3A5>é–¸ãƒ?剙钃熼柕澶å <C3A5>åŠ¤æ¤¤ï¹‚äº¶å§Šæ´ªå´¨æ¿ ä½ºç¹<C3A7>é—<C3A9>æ<EFBFBD>žåŠ‘æµœå •å¹ƒæ¥£å†©ç„¼ç€¹ãƒ¥æ£›é<E280BA>˜è—‰â”‘鈽嗗ç<E28094> 閻忔繈éŽ?ï¿ å¹‡é<E280A1>—å ¢åŽ“é<E2809C>Ÿæ»„粓宕滈妸鈺佺é—<C3A9>跨噦鎷ï¿?
|
||
if (icmp->icmp_type == ICMP_ECHOREPLY && icmp->icmp_id == pid) // ICMP_ECHOREPLY闂傚倷é<C2B7>’﹂幃é<C692>?曞磿閹惰棄纾婚柣鎰棘閻旂ǹ绶為悗é”<C3A9>庡亜閸斿懘姊洪å´?é<>•î‚£æ®?é—<C3A9>ç?¿ï¹¤é¡?烽ç<C2BD>®å¦¤å‘®æ™¸é–¿ç‡‚æ‹·
|
||
{
|
||
// æ¿ ç”µå§µé?”æ 崰妤冩崲閹邦喖绶ゅùé<C2B9>˜å·?å„<C3A5>é–¸å¬?é<C2B5>Œæ¶¢å¹˜é<CB9C>™å?˜æ¨‚é—<C3A9>绘帒锕î<E280A2>„î<E2809E><C3AE>娲î<C2B2><C3AE>敆閳ь剛绮旈鈧畷鎴﹀箻é<C2BB> å›?å°™é<E284A2>”﹀銈嗗笒é<E28099>Žæ°¼å‰›ç»®å ¥å´Ÿé¡–涚厱婵犻潧å¦?楅é¡<C3A9>å Ÿç…•éŽ¼æ·?垺绀å¬?柡宀å¬?到æ?—î<E28094>ƒå<C692>³éªžå›?é<CB9C>‘å—•ç´¦
|
||
zlog_info(zct, "icmp succecss ............. \n");
|
||
break;
|
||
} else if (cnt < 3) {
|
||
// 闂傚倷绀侀幉锟犳å<C2B3>„椤掑倻涓嶉柟æ<C5B8>ˆå‰§ç•±é–¸ã„¥å€¹éЇå‹?弽銊х煂ç¼<C3A7>ç‚?å´˜é<CB9C>‹æ„°å¹‰å§<C3A5>å²€ç–€æ¿žæˆžç‘¥æµ æ´?梺é<C2BA>“åº?潟閸婃銇欓幎鑺ョå<C2A7>†é—<C3A9>绘洖é<E28093>Šåœ?â‚?氾拷
|
||
cnt++;
|
||
continue;
|
||
} else {
|
||
close(sockfd);
|
||
iRet = -1;
|
||
break;
|
||
}
|
||
}
|
||
close(sockfd);
|
||
return iRet;
|
||
}
|
||
|
||
int get_netlink_status(const char *if_name) {
|
||
int skfd;
|
||
struct ifreq ifr;
|
||
struct ethtool_value edata;
|
||
edata.cmd = ETHTOOL_GLINK;
|
||
edata.data = 0;
|
||
memset(&ifr, 0, sizeof(ifr));
|
||
strncpy(ifr.ifr_name, if_name, sizeof(ifr.ifr_name) - 1);
|
||
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;
|
||
}
|
||
close(skfd);
|
||
return edata.data;
|
||
}
|
||
|
||
// 闂備浇é?•Ñ…æ<E280A6>¢éŽ°î†½å´²é–¹é‚¦å„µå¨‘æ¨·æ§ˆæ¿žå—˜åŠ–é<E28093><C3A9>峰┑掳é<C2B3>Šæ›¢å¹Šæ<C5A0>´ï½‚å<E2809A>“姘槹閵囧å?°éªžæŽ—崱妞惧é—<C3A9>è<EFBFBD>¤å–<C3A5>ç»?嶆刊钘å?†æž–閺囥垹é<C2B9>’åž?ã<>„鎺嶈兌閳洘銇å‹?妸銉уⅵå?žã‚ƒåžºé<C2BA>—楅å¹<C3A5>é<EFBFBD>›æ<E280BA>‘æ<E28098>¶æ¤¤æ’´ç¨’é<E28099><C3A9>冮梻浣告啞閻熴儵è—<C3A8>æ¤¤æ «å„<C3A5>é<EFBFBD><C3A9>侀柡é<C2A1>¥ãƒ¥ç<C2A5>ªé–»æ’´æ´Ÿé<C5B8>Œç†¼æ‚œå¦?虹仸妞ゃ儲é<C2B2>Ÿâ•…幃褰掑箛椤斿å<C2BF>‹é<E280B9><C3A9>å ¥æ‚—å¨ˆåž?櫘閸å¬?æ£<C3A6>é<EFBFBD>©â‚?椤æŽ<C3A6>胶鈯曢拑æ<E28098>ˆã„§ç®¾é–¸î‚£ç¹‚顣崇紒æ<E28099>ˆã„¦å°°é–¹å³°æ‡<C3A6>ç»®æ¬<C3A6>â–Žé<C5BD>™ãƒ§æ£?é—‚å‚™ç„¦éŽ®å •å´<C3A5>宥呯暆閹间ç?<3F>ç» æ°¶æŸ›éŽ°é<C2B0>›æž›ç¼<C3A7>â‚?ç€£î‚ äºœé–¹æ<C2B9>¿å?Žé<C5BD>’ä¼´æŸ<C3A6>褜é<C593>“欓…宄邦潖閾忚î<C5A1>‰ç€šæ°±æ†¸æ<C2B8>´ï½‚å<E2809A>“姘炬å«?
|
||
std::vector<int> splitVersion(const std::string &version) {
|
||
std::vector<int> parts;
|
||
std::stringstream ss(version);
|
||
std::string part;
|
||
|
||
// é—‚å‚šå€·é‘³å •å´•é<E280A2>¢ç?¿ç£»é–¹æ<C2B9><C3A6>绀å¤<C3A5>幖娣妼绾惧é<C2B7>Œã„¥â”‘é<E28098>¡æ©†æ£¡æ¿ æ?¿åž°é<C2B0>¡ã„§æ<C2A7>¢å©µå›©ç?¾é–¹îˆ†å‚šî—šé—‚佽崵æ¿?甸å´<C3A5>妤å‘窗閺åµ?å‘?殾婵ǹ娉涢ç<C2A2>®éŠ<C3A9>$箾閹寸å<C2B8>ŸéŽ³æ„°æŸ›å?˜î…§ç„½ç¼<C3A7>è¾¨æŒ»éŽ·å‘¯æ‹ éˆ©å†¾å<C2BE>…é—<C3A9>è<EFBFBD>¤å§³ç»¶ã‚‡å¾Šé<C5A0>?i幇é?‘芥斀閻庯綆浜為ˇé?<3F>︽⒑ç¼<C3A7>洖澧å<C2A7>‰ã<E280B0>„銊ユ嚇钘熼ç…?é<>¥ã„§âŠ•é–»æ’¶å–<C3A5>淇婇妶é<C2B6>Œæ°?壕闂佺粯顨呴敃銈夋æ™<C3A6>閵忊â‚?æ„<C3A6>嚤é—<C3A9>哄é<C2B0>¨å½’悡鎴︽⒑é<E28098> æ<C2A0>’劌娅愰柟é<C5B8>‘ゆå??
|
||
while (std::getline(ss, part, '.')) {
|
||
parts.push_back(std::stoi(part)); // 闂備浇é?•Ñ…æ<E280A6>¢éŽ°î†½å´²é–¹é‚¦å„µå¨‘æ?œæ—ˆå´¨é?“炵â‚?柟é<C5B8><C3A9>肩暘閸斿瞼ç»?å ¥å´¼éŠ‰ï¸¾åŽµç¼‚å‚™ç„¦é”šç¼<C3A7>æ¥<C3A6>岸é<C2B8>Œæ¶™ç¹<C3A7>é<EFBFBD>•î„<C3AE>毈é—<C3A9>哄矉ç¼<C3A7>佹俊鎼佸Ψ閵å?˜å–•绱旈梻浣呵åœâ‚?æ¶’æ™ éªžæ„°å´˜é‘¼î„<C3AE>æ?¾å©µî†½Ç¹å¨‰æ¶¢ç<C2A2>®éŠ<C3A9>℃å<C692><C3A5>濮楀棗骞橀柕é<E280A2>«î‚¢æ«<C3A6>æ¿?å©…æ?£å¨‘ç”¸å´ªæµ£å‘Šç•°æ¿ ç”µå§éŽ³å›¬å´‘éŽ¾ç»˜ç…Ÿé–»æ¨ºå•¿æ¿®å¤<C3A5>柛é<E280BA>˜å´‡å¢?椤曪ç¶<C3A7>顢曢妶é<C2B6>Œæ°«å£•婵炴垶é¡<C3A9>伴幋婵冩ç<C2A9><C3A7>é—<C3A9>跨噦鎷ï¿?
|
||
}
|
||
|
||
return parts;
|
||
}
|
||
|
||
// æ¿ ç”µå§µé?”æ å´³é?–滃緤閹ç<C2A3>›å¨‘æ¬<C3A6>媴閻戞﹩é<C2A9>‹ã„¥â”‘é<E28098>˜ç»˜æ¶§æ¿žå±‚劑é<E28098>©ãˆ¤å¼®é<C2AE>?熺厪é—<C3A9>割å<C2B2>…绻傞顓㈡煕閵å <C3A5>æ‹‹é<E280B9>Žæ—ˆæŸ¡å®€å¬?到铻æ ?æŸ<C3A6>è?œé<C593>“熼幃褎绻濋崒锕佲å<C2B2>“é<E2809C>¨î†šå½’å´—é<E28094><C3A9>肩稇é—<C3A9>活厽顨婇弻銊╂å<E2809A>†é–¸å±¾ç¨‘é¡<C3A9>�
|
||
int compareVersions(const std::string &version1, const std::string &version2) {
|
||
std::vector<int> v1 = splitVersion(version1);
|
||
std::vector<int> v2 = splitVersion(version2);
|
||
|
||
// é—‚å‚šå€·é‘³å •å¹ŠéŽ¾ç»˜å€¶æ¿®æ¨¿æ³›çº¾å<C2BE>—柟鎯版閺勩儳鈧厜é<C593>‹æ’»æŸ›é<E280BA>‡ã„¥äºœé–»æ¿‡ï¹¢å§Šæ´ªæŸ…é<E280A6><C3A9>茶å?°å?¢Ñƒç“¨éŽ±ã„¦ç¹<C3A7>é<EFBFBD>Œæ¶œç?€é—<C3A9>åœæ‡“瀚版俊鎼佹晜閻愵剚é?”忛梻é<C2BB>Œæ¬’å…Œç¼<C3A7>垶銆å†?å´¨ç€›æ¨ºäº±æ¿ ç”µå§´é<C2B4>‹å©‡æ‡“é<E2809C>¨î†šå½’å´—é<E28094><C3A9>肩稇é—<C3A9>活厽顨婇弻é”<C3A9>å?Šç?£é–¿æ¿†æ£å¦?å‹?梺é<C2BA>›å©ƒç…Ÿé–¸åº£æ•»å??诲☉éŠ<C3A9>犵é—<C3A9>ç‘°î‡é<C2AD>Žæ„¬Î›é”•傛⒑閸濆å??顥″瀛樻å€<C3A5>æ? 炲æ£<C3A6>å??崼é<C2BC>”告î<C5A0>Ÿé—‚佽法é<E280A2>£ï¹‚å¹<C3A5>锟�
|
||
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; // å©µçŠµæ•¸æ¿®çƒ½ã€‚æµ å¬?焵椤掆å<E280A0>“閸熷潡é<C2A1>©â‚?椤掆å<E280A0>“缂嶅﹪骞冨Ο璇茬窞é—<C3A9>å½’å<E28099>Šé<C5A0>“æ¿‹å¹—é<E28094>‡ãˆ¡â’‘閸︻厼顣兼ç¹<C3A7>éŠ<C3A9>★耿瀹曟繈濡烽埡é<C2A1>Œæ»ƒå¹ˆé—‚佸湱é<C2B1>Žã‚‰å´¹é<C2B9>«æ›€å¤Šâ–ŽéŽ´çŠµï¼œå©µÂ°å€<C3A5>é<EFBFBD>‹æ’´ã<C2B4>„锕備憾閸┾å<C2BE>“å¦žã‚†å¸Šé‘³å •åŸŠé<C5A0>‡ãˆ¡ç…¥æ¿®æ©†åŽ¾ç»¡â‚¬é—<C3A9>逞屽墴椤㈡æ?ƒå®•奸悢é<C2A2>›å©„彨é—<C3A9>诲é?¸é<C2B8>˜æ»ˆå´‘鎾绘煕閹邦喖浜剧紓å?¥å’ƒæ”»ç¼<C3A7>绘盯骞å??悙瀛樺創闂佺懓é<E2809C>¤æ å¹<C3A5>锟ï¿?0
|
||
int num2 = i < v2.size() ? v2[i] : 0;
|
||
|
||
if (num1 > num2) return 1; // version1 婵犵數濮伴崹褰掓å<E2809C>‰é–µå¿‹å€•纾兼ç¹<C3A7>æ¿ å‚›î—œæ¤¤ãƒ?æ‹? version2
|
||
if (num1 < num2) return -1; // version1 闂備浇é?•Ñ…æ<E280A6>¢éŽ°î†½å´²é–¹é‚¦å–—å®•æŸ¥æ‚—é”<C3A9>庡墲婵娊é<C5A0>Œã„©æ‚¤é<C2A4>Œæ¶˜î<CB9C>? version2
|
||
}
|
||
|
||
return 0; // 闂傚倷鑳å‰<C3A5>划顖炪â‚?å†?å´¨ç€›æ¨ºäº±æ¿ ç”µå§´é<C2B4>‹å©‡æ‡“é<E2809C>¨î†šå½’å´—é<E28094><C3A9>肩稇é—<C3A9>活厽顨呴—é<E2809D><C3A9>î<EFBFBD><C3AE>å<EFBFBD>“é”<C3A9>åº<C3A5>簻椤掋垻绱掓担é<E280A6>¦î…žå¼¨é—<C3A9>å“„æœ?绋戦~婵å¬?倷椤掆å<E280A0>“椤忥æ‹?
|
||
}
|
||
|
||
void Binary_Bit(unsigned char *p_data, unsigned char position, int flag) {
|
||
//婵犵數é<C2B8>‹æ¶¢æ‚ºéŠŠÑƒåž<C3A5>瀹€é<C2AC>•剹濞达絿é<C2BF>Žã‚…ç•·é<C2B7>™å¤‹å<E280B9><C3A5>æ¿¡ç‚¶î‡§æµœé¹ƒæ‚—ç‘™å‹¬ç¤ƒç€¹â‚¬éŽ¼ä½¸ç® æ¿ å©‚ç‰Šé<C5A0>‹ã„©æ¢»é<C2BB>«ç†ºâ˜‰ç¼<C3A7>傚ç¹<C3A7>鈹戦悙鑸é<C2B8>›æ¶§ç¼‚佽尙é<E284A2>…划é<E28099>ƒå <C3A5>é†‡é–ºå›©å–Žæµ é¹ƒæ¢ºç»?曞墲ç¼<C3A7>å??帡é<C2A1>©æ¶¢å¼½é¡“熺厾é—<C3A9>归棿é<C2BF>’﹀☉è?”é<E2809D>Œï½ˆç®›é<E280BA>ƒæˆžç–<C3A7>é—<C3A9>å“„è??é<>Šæ¿†î‡£é<C2A3>«æ›žç?°éŽ¼ç²¹å‰<C3A5>é¡<C3A9>熺紓é<E2809C>Œæ¬‘åŠ<C3A5>濡垿骞忛敓锟� position婵犵數é<C2B8>‹ç‚ºå´¹é<C2B9>?曞ç?²å¨´ï½…å?Šå¨´æ ?柕濞уæ‡<C3A6>顦梺é<C2BA>›å©„⊕濞兼瑩å¯?告笟鈧弻銊╂å<E2809A>†é–¸å±¾ç¨‘é?<3F>�(婵犵數é<C2B8>‹æ¶¢î”šé¡“ç†·åž”é–¹ç»¢å––ç» îˆžæŸ¨é<C2A8>•å‚›å??0 闂佽瀛╅é<E284A2><C3A9>窗閹烘纾婚柟é<C5B8><C3A9>î„<C3AE>ç<EFBFBD>±é–ºâ‚?鑺ャ亜閺囩å<C2A9>žé¡¥ç‚ºæ‚—å?˜ç‚¬å«?)
|
||
if (flag) {
|
||
*p_data |= 0x01 << (position);
|
||
} else {
|
||
*p_data &= ~(0x01 << (position));
|
||
}
|
||
}
|