2024-10-22 20:56:21 +08:00
|
|
|
|
#include "common_func.hpp"
|
2024-10-22 19:04:25 +08:00
|
|
|
|
#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 <linux/rtc.h>
|
2024-10-22 20:56:21 +08:00
|
|
|
|
#include <boost/xpressive/xpressive_dynamic.hpp>
|
|
|
|
|
|
#include "global.hpp"
|
|
|
|
|
|
#include "dbaccess/sql_db.hpp"
|
|
|
|
|
|
|
2024-10-22 19:04:25 +08:00
|
|
|
|
|
|
|
|
|
|
#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;
|
|
|
|
|
|
Mutex g_tDbMutex;
|
|
|
|
|
|
string GetLocalTimeWithMs(void) {
|
|
|
|
|
|
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); //闁硅泛锕ょ欢閬嶅礆閹殿喗鐣遍柛濠勫帶閻°劑宕楅妷銈咁槻闁哄啳娉涢崹搴ㄦ煀瀹ュ洦鐣遍柛鎰噹閻°劍绋夐銊х缂佹崘娉曢埢鑲┾偓鐟邦槸閸欙拷
|
|
|
|
|
|
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 (...) {
|
|
|
|
|
|
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) return -1;
|
|
|
|
|
|
|
|
|
|
|
|
memset(outbuf, 0, outlen);
|
|
|
|
|
|
|
|
|
|
|
|
if ((int)iconv(cd, pin, &inlen, pout, &outlen) == -1) {
|
|
|
|
|
|
iconv_close(cd);
|
|
|
|
|
|
return -1;
|
|
|
|
|
|
}
|
|
|
|
|
|
iconv_close(cd);
|
|
|
|
|
|
*pout = '\0';
|
|
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int u2g(char *inbuf, size_t inlen, char *outbuf, size_t outlen) { return code_convert("utf-8", "gb2312", inbuf, inlen, outbuf, outlen); }
|
|
|
|
|
|
|
|
|
|
|
|
int g2u(char *inbuf, size_t inlen, char *outbuf, size_t outlen) { return code_convert("gb2312", "utf-8", inbuf, inlen, outbuf, outlen); }
|
|
|
|
|
|
|
|
|
|
|
|
std::string GBKToUTF8(const std::string &strGBK) {
|
|
|
|
|
|
int length = strGBK.size() * 2 + 1;
|
|
|
|
|
|
|
|
|
|
|
|
char *temp = (char *)malloc(sizeof(char) * length);
|
|
|
|
|
|
|
|
|
|
|
|
if (g2u((char *)strGBK.c_str(), strGBK.size(), temp, length) >= 0) {
|
|
|
|
|
|
std::string str_result;
|
|
|
|
|
|
str_result.append(temp);
|
|
|
|
|
|
free(temp);
|
|
|
|
|
|
return str_result;
|
|
|
|
|
|
} else {
|
|
|
|
|
|
free(temp);
|
|
|
|
|
|
return "";
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
std::string UTFtoGBK(const char *utf8) {
|
|
|
|
|
|
int length = strlen(utf8);
|
|
|
|
|
|
|
|
|
|
|
|
char *temp = (char *)malloc(sizeof(char) * length);
|
|
|
|
|
|
|
|
|
|
|
|
if (u2g((char *)utf8, length, temp, length) >= 0) {
|
|
|
|
|
|
std::string str_result;
|
|
|
|
|
|
str_result.append(temp);
|
|
|
|
|
|
free(temp);
|
|
|
|
|
|
|
|
|
|
|
|
return str_result;
|
|
|
|
|
|
} else {
|
|
|
|
|
|
free(temp);
|
|
|
|
|
|
return "";
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
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");
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
size_t inBytesLeft = input.size();
|
|
|
|
|
|
size_t outBytesLeft = inBytesLeft * 2; // GBK may require up to twice the space of UTF-8
|
|
|
|
|
|
char *inBuf = const_cast<char *>(input.c_str());
|
|
|
|
|
|
char *outBuf = new char[outBytesLeft];
|
|
|
|
|
|
char *outPtr = outBuf;
|
|
|
|
|
|
|
|
|
|
|
|
if (iconv(conv, &inBuf, &inBytesLeft, &outPtr, &outBytesLeft) == (size_t)-1) {
|
|
|
|
|
|
delete[] outBuf;
|
|
|
|
|
|
iconv_close(conv);
|
|
|
|
|
|
throw std::runtime_error("iconv failed");
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
std::string result(outBuf, outPtr);
|
|
|
|
|
|
delete[] outBuf;
|
|
|
|
|
|
iconv_close(conv);
|
|
|
|
|
|
return result;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void InitGpio(unsigned int gpioN, unsigned int inout) {
|
|
|
|
|
|
int fd = 0;
|
|
|
|
|
|
char tmp[100] = {0};
|
|
|
|
|
|
//闁瑰灚鎸哥槐鎱籶io閻犱焦鍎抽ˇ顒勫棘閸ワ附顐<E99984>
|
|
|
|
|
|
fd = open("/sys/class/gpio/export", O_WRONLY);
|
|
|
|
|
|
if (-1 == fd) {
|
|
|
|
|
|
printf("[%s]:[%d] open gpio export file error\r\n", __FUNCTION__, __LINE__);
|
|
|
|
|
|
}
|
|
|
|
|
|
//闁汇垹鐤囬鐞痯io
|
|
|
|
|
|
sprintf(tmp, "%d", gpioN);
|
|
|
|
|
|
if (write(fd, tmp, strlen(tmp)) < 0) {
|
|
|
|
|
|
printf("write file operation error %s\r\n", tmp);
|
|
|
|
|
|
}
|
|
|
|
|
|
close(fd);
|
|
|
|
|
|
sleep(1);
|
|
|
|
|
|
//闂佹澘绉堕悿鍞僷io闁哄倻鎳撻幃锟<E5B983>
|
|
|
|
|
|
#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);
|
|
|
|
|
|
#else if IMX6UL_GATEWAY
|
|
|
|
|
|
sprintf(tmp, "/sys/class/gpio/gpio%d/direction", gpioN);
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
print_info("open GPIO = %s\n", tmp);
|
|
|
|
|
|
fd = open(tmp, O_WRONLY);
|
|
|
|
|
|
if (-1 == fd) {
|
|
|
|
|
|
printf("[%s]:[%d] open gpio direction file error\r\n", __FUNCTION__, __LINE__);
|
|
|
|
|
|
close(fd);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (inout == 0) {
|
|
|
|
|
|
print_info("=====InitGpio=====in\n");
|
|
|
|
|
|
if (-1 == write(fd, "in", sizeof("in"))) {
|
|
|
|
|
|
print_info("[%s]:[%d] [%d]write operation direction error\r\n", __FUNCTION__, __LINE__, gpioN);
|
|
|
|
|
|
close(fd);
|
|
|
|
|
|
}
|
|
|
|
|
|
} else if (inout == 1) {
|
|
|
|
|
|
print_info("=====InitGpio=====out\n");
|
|
|
|
|
|
if (-1 == write(fd, "out", sizeof("out"))) {
|
|
|
|
|
|
print_info("[%s]:[%d] [%d]write operation direction error\r\n", __FUNCTION__, __LINE__, gpioN);
|
|
|
|
|
|
close(fd);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
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};
|
|
|
|
|
|
#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);
|
|
|
|
|
|
#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闁哄倸娲e▎锟<E2968E>
|
|
|
|
|
|
fd = open(tmp, O_WRONLY);
|
|
|
|
|
|
if (-1 == fd) {
|
|
|
|
|
|
print_red("[%s]:[%d][%s] open gpio export file error\r\n", __FUNCTION__, __LINE__, tmp);
|
|
|
|
|
|
close(fd);
|
|
|
|
|
|
return (-1); // exit(1);
|
|
|
|
|
|
}
|
|
|
|
|
|
//閻犱礁澧介悿鍡涙偨闂堟盯鎸<E79BAF>
|
|
|
|
|
|
if (x) {
|
|
|
|
|
|
if (-1 == write(fd, "1", sizeof("1"))) {
|
|
|
|
|
|
print_red("[%s]:[%d] %d write operation value error\r\n", __FUNCTION__, __LINE__, gpioN);
|
|
|
|
|
|
close(fd);
|
|
|
|
|
|
return (-1); // exit(1);
|
|
|
|
|
|
}
|
|
|
|
|
|
} else {
|
|
|
|
|
|
if (-1 == write(fd, "0", sizeof("0"))) {
|
|
|
|
|
|
print_red("[%s]:[%d] %d write operation value error\r\n", __FUNCTION__, __LINE__, gpioN);
|
|
|
|
|
|
close(fd);
|
|
|
|
|
|
return (-1); // exit(1);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
// printf("gpio%d set %d ok\r\n",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);
|
|
|
|
|
|
#else if IMX6UL_GATEWAY
|
|
|
|
|
|
sprintf(tmp, "/sys/class/gpio/gpio%d/value", gpioN);
|
|
|
|
|
|
#endif
|
|
|
|
|
|
//闁瑰灚鎸哥槐鎱籶io value闁哄倸娲e▎锟<E2968E>
|
|
|
|
|
|
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);
|
|
|
|
|
|
}
|
|
|
|
|
|
//閻犲洩顕цぐ锟<E38190> value闁哄倸娲e▎锟<E2968E>
|
|
|
|
|
|
if (-1 == read(fd, &value, sizeof(value))) {
|
|
|
|
|
|
print_red("[%s]:[%d] %d read gpiovalue is fail\r\n", __FUNCTION__, __LINE__, gpioN);
|
|
|
|
|
|
close(fd);
|
|
|
|
|
|
return (-1); // exit(1);
|
|
|
|
|
|
}
|
|
|
|
|
|
close(fd);
|
|
|
|
|
|
// printf("gpio%d get %d\r\n",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, speed_t speed) {
|
|
|
|
|
|
int iFd = open(Port, O_RDWR | O_NOCTTY);
|
|
|
|
|
|
if (iFd < 0) {
|
|
|
|
|
|
return -1;
|
|
|
|
|
|
}
|
|
|
|
|
|
struct termios opt;
|
|
|
|
|
|
tcgetattr(iFd, &opt);
|
|
|
|
|
|
cfsetispeed(&opt, speed);
|
|
|
|
|
|
cfsetospeed(&opt, speed);
|
|
|
|
|
|
if (tcgetattr(iFd, &opt) < 0) {
|
|
|
|
|
|
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) {
|
|
|
|
|
|
return -1;
|
|
|
|
|
|
}
|
|
|
|
|
|
tcflush(iFd, TCIOFLUSH);
|
|
|
|
|
|
|
|
|
|
|
|
return iFd;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int write_data(int fd, char *buff, int len) {
|
|
|
|
|
|
int ret;
|
|
|
|
|
|
char buf[100];
|
|
|
|
|
|
ret = write(fd, buff, len);
|
|
|
|
|
|
if (ret < 0) {
|
|
|
|
|
|
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) {
|
|
|
|
|
|
perror("read_data");
|
|
|
|
|
|
return -1;
|
|
|
|
|
|
}
|
|
|
|
|
|
buff[ret] = '\0';
|
|
|
|
|
|
return ret;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int ModifyMac(char *buff) {
|
|
|
|
|
|
FILE *fp = fopen("/opt/system/mac", "w");
|
|
|
|
|
|
fprintf(fp, buff);
|
|
|
|
|
|
fprintf(fp, "\n");
|
|
|
|
|
|
fclose(fp);
|
|
|
|
|
|
system("cp /opt/system/mac /opt/system/macbak");
|
|
|
|
|
|
system("/opt/Cidn/init.sh");
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
|
int hour = 0;
|
|
|
|
|
|
iyear = tm_info->tm_year + 1900;
|
|
|
|
|
|
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);
|
|
|
|
|
|
return tm_info;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int system_custom(const char *cmd, char *buf) {
|
|
|
|
|
|
FILE *fp;
|
|
|
|
|
|
int res;
|
|
|
|
|
|
char temp[1024] = {0};
|
|
|
|
|
|
if (cmd == NULL) {
|
|
|
|
|
|
return -1;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if ((fp = popen(cmd, "r")) == NULL) {
|
|
|
|
|
|
print_error("popen error\n");
|
|
|
|
|
|
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;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
std::string GetDayDate() {
|
|
|
|
|
|
time_t t = time(0);
|
|
|
|
|
|
char tmp[64];
|
|
|
|
|
|
strftime(tmp, sizeof(tmp), "%Y-%m-%d", localtime(&t));
|
|
|
|
|
|
std::string data = std::string((char *)tmp);
|
|
|
|
|
|
return data;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
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) {
|
|
|
|
|
|
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);
|
|
|
|
|
|
|
|
|
|
|
|
// 閻忓繐鏁僼c_time缂備焦鎸婚悗顖涙媴閹捐绁柟璇℃線鐠愮劎ime_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; // 濞戞挸绉虫繛鍥偨閵娿儺妲惧ù鐘€栧锟<EE9C83>
|
|
|
|
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
close(fd);
|
|
|
|
|
|
return std::string(rtcTime);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void GetTime_(char time_buff[], TIME_SIZE len) {
|
|
|
|
|
|
int i = sizeof(time_buff);
|
|
|
|
|
|
memset(time_buff, 0, i);
|
|
|
|
|
|
time_t timep;
|
|
|
|
|
|
time(&timep);
|
|
|
|
|
|
strcpy(time_buff, ctime(&timep));
|
|
|
|
|
|
std::string strtemp = time_buff;
|
|
|
|
|
|
strtemp = strtemp.substr(11, len);
|
|
|
|
|
|
memset(time_buff, 0, strlen(time_buff));
|
|
|
|
|
|
strcpy(time_buff, strtemp.c_str());
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
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)) {
|
|
|
|
|
|
print_info("root = %d\n", root.size());
|
|
|
|
|
|
for (int i = 0; i < root.size(); i++) {
|
|
|
|
|
|
hwVersion = root[i]["hw_vesion"];
|
|
|
|
|
|
for (int i = 0; i < hwVersion.size(); i++) {
|
|
|
|
|
|
datanodeUpdate.hwVersion.push_back(hwVersion[i].asString());
|
|
|
|
|
|
}
|
|
|
|
|
|
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());
|
|
|
|
|
|
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);
|
|
|
|
|
|
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());
|
2024-10-22 20:56:21 +08:00
|
|
|
|
int rows = sqlite_db_ctrl::instance().GetTableRows(T_SENSOR_INFO(TNAME), whereCon);
|
2024-10-22 19:04:25 +08:00
|
|
|
|
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);
|
2024-10-22 20:56:21 +08:00
|
|
|
|
sqlite_db_ctrl::instance().InsertData(T_SENSOR_INFO(TNAME), insertSql);
|
2024-10-22 19:04:25 +08:00
|
|
|
|
}
|
|
|
|
|
|
} else {
|
|
|
|
|
|
print_info("parse error\n");
|
|
|
|
|
|
}
|
|
|
|
|
|
is.close();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void ImportConfig(std::string filename) {
|
|
|
|
|
|
Json::Value root, gateWay, dataNode;
|
|
|
|
|
|
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<string> 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());
|
2024-10-22 20:56:21 +08:00
|
|
|
|
sqlite_db_ctrl::instance().InsertData(T_SENSOR_INFO(TNAME), insertSql);
|
2024-10-22 19:04:25 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
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());
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
int UpdataDataNodeConfig(std::string filename) {
|
|
|
|
|
|
vector<DataNodeInfo> vecDataNode; //濠㈠湱澧楀Σ鎴炵▔閳ь剚绋夐鍕憻缂佹缂氱憰鍡涘触閹达箑娅<E7AE91>
|
|
|
|
|
|
//濞寸姰鍎撮浼村礂閵夛附鐓欑€殿喖绻戞晶锕€顕i埀顒勫棘閸ワ附顐<E99984>
|
|
|
|
|
|
ifstream csv_data(filename, ios::in);
|
|
|
|
|
|
int iRet = 0;
|
|
|
|
|
|
if (!csv_data.is_open()) {
|
|
|
|
|
|
cout << "Error: opening file fail" << endl;
|
|
|
|
|
|
return -1;
|
|
|
|
|
|
} else {
|
|
|
|
|
|
string line;
|
|
|
|
|
|
|
|
|
|
|
|
vector<string> words; //濠㈠湱澧楀Σ鎴炵▔閳ь剚绋夐鍕憻缂佹缂氱憰鍡涘触閹达箑娅<E7AE91>
|
|
|
|
|
|
string word;
|
|
|
|
|
|
|
|
|
|
|
|
DataNodeInfo dataNode;
|
|
|
|
|
|
// ------------閻犲洩顕цぐ鍥极閻楀牆绁<E78986>-----------------
|
|
|
|
|
|
// 閻犲洩顕цぐ鍥冀閸ヮ剦鏆悶娑虫嫹
|
|
|
|
|
|
getline(csv_data, line);
|
|
|
|
|
|
|
|
|
|
|
|
istringstream sin;
|
|
|
|
|
|
// 闁圭ǹ顦抽、鎴犳嫚鐠囨彃绲块柡浣哄瀹擄拷
|
|
|
|
|
|
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);
|
|
|
|
|
|
}
|
|
|
|
|
|
csv_data.close();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
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());
|
|
|
|
|
|
|
2024-10-22 20:56:21 +08:00
|
|
|
|
int iRet = sqlite_db_ctrl::instance().UpdateTableData(T_SENSOR_INFO(TNAME), updateSql, whereCon);
|
2024-10-22 19:04:25 +08:00
|
|
|
|
memset(whereCon, 0x00, sizeof(whereCon));
|
|
|
|
|
|
memset(updateSql, 0x00, sizeof(updateSql));
|
|
|
|
|
|
}
|
|
|
|
|
|
iRet = vecDataNode.size();
|
|
|
|
|
|
} else {
|
|
|
|
|
|
iRet = -3;
|
|
|
|
|
|
}
|
|
|
|
|
|
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 (reader.parse(is, root)) {
|
|
|
|
|
|
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) {
|
|
|
|
|
|
perror("create socket falise...mac/n");
|
|
|
|
|
|
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");
|
|
|
|
|
|
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);
|
|
|
|
|
|
close(sock_mac);
|
|
|
|
|
|
LOG_INFO("net : %s,mac:%s\n", net, mac_addr);
|
|
|
|
|
|
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))) {
|
|
|
|
|
|
perror_info("socket");
|
|
|
|
|
|
return "";
|
|
|
|
|
|
}
|
|
|
|
|
|
struct ifreq req;
|
|
|
|
|
|
struct sockaddr_in *host;
|
|
|
|
|
|
|
|
|
|
|
|
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));
|
|
|
|
|
|
}
|
|
|
|
|
|
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) {
|
|
|
|
|
|
return strip;
|
|
|
|
|
|
}
|
|
|
|
|
|
strip = GetGwIp_(WLAN2);
|
|
|
|
|
|
if (strip.compare("0.0.0.0") != 0) {
|
|
|
|
|
|
return strip;
|
|
|
|
|
|
}
|
|
|
|
|
|
strip = GetGwIp_(ETH1);
|
|
|
|
|
|
if (strip.compare("0.0.0.0") != 0 && strip.compare("192.168.188.188") != 0) {
|
|
|
|
|
|
return strip;
|
|
|
|
|
|
}
|
|
|
|
|
|
strip = GetGwIp_(ETH2);
|
|
|
|
|
|
if (strip.compare("0.0.0.0") != 0 && strip.compare("192.168.188.188") != 0) {
|
|
|
|
|
|
return strip;
|
|
|
|
|
|
}
|
|
|
|
|
|
strip = GetGwIp_(ETH0);
|
|
|
|
|
|
if (strip.compare("0.0.0.0") != 0) {
|
|
|
|
|
|
return strip;
|
|
|
|
|
|
}
|
|
|
|
|
|
strip = GetGwIp_(USB0);
|
|
|
|
|
|
if (strip.compare("0.0.0.0") != 0) {
|
|
|
|
|
|
return strip;
|
|
|
|
|
|
}
|
|
|
|
|
|
strip = GetGwIp_(WWAN0);
|
|
|
|
|
|
if (strip.compare("0.0.0.0") != 0) {
|
|
|
|
|
|
return strip;
|
|
|
|
|
|
}
|
|
|
|
|
|
// 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 &ClearAllSpace(std::string &str) {
|
|
|
|
|
|
int index = 0;
|
|
|
|
|
|
if (!str.empty()) {
|
|
|
|
|
|
while ((index = str.find(' ', index)) != string::npos) {
|
|
|
|
|
|
str.erase(index, 1);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
return str;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
std::string GetSysInfo() {
|
|
|
|
|
|
const char *getCpuUse = "top -b -n 1 |grep Cpu | cut -d \",\" -f 1 | cut -d \":\" -f 2 |tr -d ' us'";
|
|
|
|
|
|
char chRes[100] = {0};
|
|
|
|
|
|
system_custom(getCpuUse, chRes);
|
|
|
|
|
|
std::string CpuUse = std::string(chRes);
|
|
|
|
|
|
|
|
|
|
|
|
const char *getCpuSys = "top -b -n 1 |grep Cpu | cut -d \",\" -f 2 |tr -d ' sy'";
|
|
|
|
|
|
memset(chRes, 0, 100);
|
|
|
|
|
|
system_custom(getCpuSys, chRes);
|
|
|
|
|
|
std::string CpuSys = std::string(chRes);
|
|
|
|
|
|
|
|
|
|
|
|
const char *getMemtotal = "cat /proc/meminfo | grep MemTotal | awk -F"
|
|
|
|
|
|
":"
|
|
|
|
|
|
" '{print $2}' |tr -d ' kB'";
|
|
|
|
|
|
memset(chRes, 0, 100);
|
|
|
|
|
|
system_custom(getMemtotal, chRes);
|
|
|
|
|
|
std::string MemTotal = std::string(chRes);
|
|
|
|
|
|
|
|
|
|
|
|
const char *getMemFree = "cat /proc/meminfo | grep MemFree | awk -F"
|
|
|
|
|
|
":"
|
|
|
|
|
|
" '{print $2}' |tr -d ' kB'";
|
|
|
|
|
|
memset(chRes, 0, 100);
|
|
|
|
|
|
system_custom(getMemFree, chRes);
|
|
|
|
|
|
std::string MemFree = std::string(chRes);
|
|
|
|
|
|
|
|
|
|
|
|
float a = atof(MemFree.c_str());
|
|
|
|
|
|
float b = atof(MemTotal.c_str());
|
|
|
|
|
|
float c = (1 - a / b) * 100;
|
|
|
|
|
|
|
|
|
|
|
|
const char *getEmmcInfo = "df -h | grep /dev/root";
|
|
|
|
|
|
memset(chRes, 0, 100);
|
|
|
|
|
|
system_custom(getEmmcInfo, chRes);
|
|
|
|
|
|
std::string Emmcinfo = std::string(chRes);
|
|
|
|
|
|
std::size_t found = Emmcinfo.find("%");
|
|
|
|
|
|
if (found != std::string::npos) {
|
|
|
|
|
|
Emmcinfo = Emmcinfo.substr(found - 3, 3);
|
|
|
|
|
|
}
|
|
|
|
|
|
Emmcinfo = ClearAllSpace(Emmcinfo);
|
|
|
|
|
|
|
|
|
|
|
|
char sysinfo[128] = {0};
|
|
|
|
|
|
sprintf(sysinfo, "%-13s%-13s%-13s%-13s ", to_string(c).substr(0, 4).c_str(), CpuSys.c_str(), CpuUse.c_str(), Emmcinfo.c_str());
|
|
|
|
|
|
return std::string(sysinfo);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void StartWriteToDat() { print_info("start writetoDat\n"); }
|
|
|
|
|
|
|
|
|
|
|
|
float *ReSample(int WaveDataLength, int N, int *NewWaveDataLength, std::vector<float> &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;
|
|
|
|
|
|
tv.tv_sec = timep;
|
|
|
|
|
|
tv.tv_usec = milliseconds * 1000;
|
|
|
|
|
|
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) {
|
|
|
|
|
|
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;
|
|
|
|
|
|
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};
|
|
|
|
|
|
// 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");
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
std::string GetFileContent(std::string filename, int line) {
|
|
|
|
|
|
std::string strFileContent("");
|
|
|
|
|
|
std::ifstream ifileOut(filename.c_str());
|
|
|
|
|
|
if (ifileOut.is_open()) { //闁哄倸娲e▎銏ゅ箥閹惧磭纾<E7A3AD>
|
|
|
|
|
|
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();
|
|
|
|
|
|
}
|
|
|
|
|
|
return strFileContent;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// BOOST闁烩偓鍔嶉婊堝礆濞嗘帇鈧啯娼忛幆褏纭€濡ょ姴鐭侀惁濉眕闁革附婢樺鍐触閸喓銆<E59693>
|
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
|
|
|
|
// 閻忓繐鎹凱闁革附婢樺鍐偨閹典讲鍋撳⿰鍛化闁告帒妫楀畷鍕交濞戞ê鐓戦柍銉︾箚濞村棝骞戦姀鈥崇亣 闁炽儲绮堢花鈺傛交濞戞ê鐓戦柡浣哥摠閺嗙喖鍨鹃敓锟<E69593>
|
|
|
|
|
|
struct in_addr s;
|
|
|
|
|
|
iRet = inet_pton(AF_INET, mask.c_str(), &s);
|
|
|
|
|
|
|
|
|
|
|
|
// 閺夌儐鍓氬畷鏌ュ箣閹邦剙顫犻弶鈺傛煥濞诧拷1闁挎稑鐭侀鈺呭及鎼淬垺笑闁哄牆顦伴弲銉╂儍閸戭毝闁革附婢樺锟<EE9F87>
|
|
|
|
|
|
if (iRet == 1) {
|
|
|
|
|
|
// 濞寸姴娴风紞澶岀磼濠婂啰鎽熼柤鍝勫€块妴搴㈡償韫囨碍绁柟璇℃線鐠愮喐绋夌紒妯荤皻閻庢稒顨夋俊顓熴亜閸濆嫮纰<E5ABAE>
|
|
|
|
|
|
unsigned int addr = ntohl(s.s_addr);
|
|
|
|
|
|
|
|
|
|
|
|
// 閺夌儐鍓氬畷鍙夌▔鏉炴壆鐧岄弶鈺傜☉閸╂鈧稒顨堥浣圭▔閿燂拷
|
|
|
|
|
|
std::bitset<32> b((int)addr);
|
|
|
|
|
|
std::string strMask = b.to_string();
|
|
|
|
|
|
|
|
|
|
|
|
// 闁哄被鍎叉竟妯荤瀹€鍐闁告帟娉涢悺褏绮敂鑳洬濞戞搩鍘惧▓锟<E29693>"01"闁挎稑鑻々褔寮稿⿰鈧粭澶屸偓娑櫭﹢顏堟晬瀹€鍐惧殯闁哄嫬瀛╁Σ鎼佸嫉婢跺娅忛柣銊ュ閻℃瑧绱旈幋鐐茶礋闁活噯鎷<E599AF>
|
|
|
|
|
|
return (strMask.find("01") == std::string::npos);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
double GetHardDiskFree() {
|
|
|
|
|
|
char hardName[32];
|
|
|
|
|
|
char hardTotal[32];
|
|
|
|
|
|
char hardUse[32];
|
|
|
|
|
|
char hardFree[32];
|
|
|
|
|
|
char rateHardUse[32];
|
|
|
|
|
|
const char *getEmmcInfo = "df -h | grep /opt";
|
|
|
|
|
|
char chRes[100];
|
|
|
|
|
|
memset(chRes, 0, 100);
|
|
|
|
|
|
system_custom(getEmmcInfo, chRes);
|
|
|
|
|
|
sscanf(chRes, "%s%s%s%s%s", hardName, hardTotal, hardUse, hardFree, rateHardUse);
|
|
|
|
|
|
std::string strhardTotal(hardTotal);
|
|
|
|
|
|
std::string strhardFree(hardFree);
|
|
|
|
|
|
std::string strrateHardUse(rateHardUse);
|
|
|
|
|
|
double freeDisk = atof(strhardFree.substr(0, strhardFree.length() - 1).c_str());
|
|
|
|
|
|
return freeDisk;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int getSysIntValue(char *key) {
|
|
|
|
|
|
FILE *fp = NULL;
|
|
|
|
|
|
int value = 0;
|
|
|
|
|
|
if (key == NULL) return 0;
|
|
|
|
|
|
|
|
|
|
|
|
fp = fopen(key, "r");
|
|
|
|
|
|
if (fp == NULL) {
|
|
|
|
|
|
printf("Error: could not open %s file\n", key);
|
|
|
|
|
|
return 1;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
fscanf(fp, "%d", &value);
|
|
|
|
|
|
fclose(fp);
|
|
|
|
|
|
fp = NULL;
|
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
|
|
|
|
float fCpuRate;
|
|
|
|
|
|
char name[8];
|
|
|
|
|
|
double cpu_idle = -1;
|
|
|
|
|
|
double cpu_sys = -1;
|
|
|
|
|
|
double cpu_user = -1;
|
|
|
|
|
|
double cpu_total = -1;
|
|
|
|
|
|
double cpu_wait = -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%d", 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%d", 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;
|
|
|
|
|
|
cpu_wait = iowaitNext - iowait;
|
|
|
|
|
|
cpu_idle = idleNext - idle;
|
|
|
|
|
|
fCpuRate = 1.0 * (cpu_total - cpu_idle) / cpu_total;
|
|
|
|
|
|
float rateUser = cpu_user * 100.0 / cpu_total;
|
|
|
|
|
|
float rateSys = cpu_sys * 100.0 / cpu_total;
|
|
|
|
|
|
if (rateUser > 95) {
|
|
|
|
|
|
rateUser = 92;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
char hardName[32];
|
|
|
|
|
|
char hardTotal[32];
|
|
|
|
|
|
char hardUse[32];
|
|
|
|
|
|
char hardFree[32];
|
|
|
|
|
|
char rateHardUse[32];
|
|
|
|
|
|
const char *getEmmcInfo = "df -h | grep /opt";
|
|
|
|
|
|
char chRes[100];
|
|
|
|
|
|
memset(chRes, 0, 100);
|
|
|
|
|
|
#ifdef IMX6UL_GATEWAY
|
|
|
|
|
|
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);
|
|
|
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
|
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());
|
|
|
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
|
|
|
|
char localtimestamp[32] = {0};
|
|
|
|
|
|
GetTimeNet(localtimestamp, 1);
|
|
|
|
|
|
std::string nowTimetamp = std::string(localtimestamp);
|
|
|
|
|
|
jsData["updateTime"] = nowTimetamp;
|
|
|
|
|
|
std::string strJson = fw.write(jsData);
|
|
|
|
|
|
return strJson;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 閻忓繐妫楀畷鐔哥▔閿燂拷16閺夆晜绋戦崺妤冣偓娑欘殘椤戜焦娼浣稿簥濞戞挸鎼顔芥償閺冨倹鐣遍悗娑欘殙婵☆參宕愰敓锟<E69593>
|
|
|
|
|
|
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; // 閻庣敻鈧稓鑹鹃柡鍐У閺呫儵鎯冮崟顐ゆ憻缂佹璁g槐婵囨交閺傛寧绀€0
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 閻忓骏鎷<E9AA8F>16閺夆晜绋戦崺妤冣偓娑欘殘椤戜焦绋夐懠鑸电ギ闁硅婢€鐠愮喓鈧稒顨夋俊顓㈠极閹殿喚鐭<E5969A>
|
|
|
|
|
|
int hexStringToBytes(const char *hexStr, unsigned char *bytes, size_t bytesSize) {
|
|
|
|
|
|
size_t hexLen = strlen(hexStr);
|
|
|
|
|
|
if (hexLen % 2 != 0) {
|
|
|
|
|
|
return -1; // Hex閻庢稒顨堥浣圭▔閺屻儲姣愰幖杈剧畱缁ㄨ尙鎷犻妷锔叫﹂柛瀣煐閺嗭拷
|
|
|
|
|
|
}
|
|
|
|
|
|
if (bytesSize < hexLen / 2) {
|
|
|
|
|
|
return -1; // 閻庢稒顨夋俊顓㈠极閹殿喚鐭嬮柣銊ュ閵囧洨浜歌箛搴g憹閻℃帒鍘栨禍鎺斺偓鐟版贡閹惧吋娼浣稿簥闁告艾娴峰▓鎴犫偓娑欘殙婵★拷
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
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; // 闁瑰瓨鍔曟慨锟<E685A8>
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
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) {
|
|
|
|
|
|
// 閻犲洩顕цぐ鍥ㄧ▔閵堝嫰鍤嬮悗娑欘殘椤戜線鐛捄铏规闁稿繑鍎煎ù鍡涘箲椤叀绀嬮柡浣哥摠閺嗭拷
|
|
|
|
|
|
int byte;
|
|
|
|
|
|
sscanf(&hexStr[i], "%2x", &byte);
|
|
|
|
|
|
// 閻忓繐妫欓弳锝夊极閹峰本绁柟璇℃線鐠愮喓鈧數鎳撶花鏌ユ儍閸戞┎CII閻庢稒顨堥锟<EE9496>
|
|
|
|
|
|
asciiStr[j++] = (char)byte;
|
|
|
|
|
|
}
|
|
|
|
|
|
// 婵烇綀顕ф慨鐐碘偓娑欘殘椤戜焦绋夐懠顒傛尝闁哄鍠撻锟<EE9496>
|
|
|
|
|
|
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;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
string tohex(const string &str) {
|
|
|
|
|
|
string ret;
|
|
|
|
|
|
static const char *hex = "0123456789ABCDEF";
|
|
|
|
|
|
for (int i = 0; i != str.size(); ++i) {
|
|
|
|
|
|
ret.push_back(hex[(str[i] >> 4) & 0xf]);
|
|
|
|
|
|
ret.push_back(hex[str[i] & 0xf]);
|
|
|
|
|
|
}
|
|
|
|
|
|
return ret;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
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); //濡ゅ倹顭囬弫鎼佺嵁閾忣偅绠掗柡渚婃嫹
|
|
|
|
|
|
if (0 > (fd = open("/dev/watchdog", O_WRONLY))) {
|
|
|
|
|
|
print_info("Failed:Open /dev/watchdog");
|
|
|
|
|
|
}
|
|
|
|
|
|
return fd;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int WriteWatchDog(int fd) {
|
|
|
|
|
|
if (1 != write(fd, "0", 1)) {
|
|
|
|
|
|
print_info("Failed:feeding watchdog");
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int CloseWatchDog(int fd) { close(fd); }
|
|
|
|
|
|
string TrimStringLeft(const char *szData, const char *szTargets) {
|
|
|
|
|
|
string strData = szData;
|
|
|
|
|
|
int nPos = 0;
|
|
|
|
|
|
nPos = strData.find(szTargets);
|
|
|
|
|
|
while (nPos == 0) {
|
|
|
|
|
|
strData.erase(nPos, 1);
|
|
|
|
|
|
nPos = strData.find(szTargets);
|
|
|
|
|
|
}
|
|
|
|
|
|
return strData;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
string TrimStringRight(const char *szData, const char *szTargets) {
|
|
|
|
|
|
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;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
string GetOneContent(const char *szData, int nRow, const char *szSeparate) {
|
|
|
|
|
|
string strParam = "";
|
|
|
|
|
|
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) {
|
|
|
|
|
|
struct termios newtio, oldtio;
|
|
|
|
|
|
if (tcgetattr(fd, &oldtio) != 0) { //婵☆偀鍋撴繛鏉戭儎鐟曞棝宕i敐鍡樞﹂柛姘剧畱瑜版煡鎮介敓锟<E69593>
|
|
|
|
|
|
perror("SetupSerial 1");
|
|
|
|
|
|
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;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
switch (nEvent) //閻犱礁澧介悿鍡椢涢埀顒侇殽鐏炶偐绉<E58190>
|
|
|
|
|
|
{
|
|
|
|
|
|
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;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
switch (nSpeed) //閻犱礁澧介悿鍡椻枖閵忋垹顥楅柣婊愭嫹
|
|
|
|
|
|
{
|
|
|
|
|
|
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);
|
|
|
|
|
|
break;
|
|
|
|
|
|
}
|
|
|
|
|
|
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) //閻犱礁澧介悿鍡樼▔閹绘帒缍撻柛娆忓€归弳锟<E5BCB3>
|
|
|
|
|
|
{
|
|
|
|
|
|
perror("com set error");
|
|
|
|
|
|
return -1;
|
|
|
|
|
|
}
|
|
|
|
|
|
return 0;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
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");
|
|
|
|
|
|
} else {
|
|
|
|
|
|
set_opt(fd, 9600, 8, 'N', 1);
|
|
|
|
|
|
}
|
|
|
|
|
|
set_opt(fd, 115200, 8, 'N', 1);
|
|
|
|
|
|
|
|
|
|
|
|
// write(fd,"AT+QENG=\"servingcell\"\r\n",27);//鐎殿喒鍋撻柛姘煎灠閻g偓鎷呴敓锟<E69593>
|
|
|
|
|
|
write(fd, "AT+CSQ", 6);
|
|
|
|
|
|
sleep(1);
|
|
|
|
|
|
unsigned char rbuf[128] = {0x00};
|
|
|
|
|
|
int len = read(fd, rbuf, sizeof(rbuf)); // 闁革负鍔嬬憰鍡涘矗閿濆牜鍤㈤柛蹇嬪劚閻⊙呯箔閿旇儻顩<E584BB>
|
|
|
|
|
|
|
|
|
|
|
|
print_info("rbuf = %s,len = %d\n", rbuf, len);
|
|
|
|
|
|
sleep(1);
|
|
|
|
|
|
if (len < 0) {
|
|
|
|
|
|
print_info("Can't get /dev/ttyUSBx Serial Port data!\n");
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
const char *str2 = "+QENG: ";
|
|
|
|
|
|
char *pdata = strstr((char *)rbuf, str2);
|
|
|
|
|
|
if (pdata)
|
|
|
|
|
|
strncpy(csq, pdata + 7, sizeof(csq));
|
|
|
|
|
|
else {
|
|
|
|
|
|
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);
|
|
|
|
|
|
return atoi(signal.c_str());
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void IniReadValue(char *section, 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) {
|
|
|
|
|
|
printf("%s: Opent file %s failed.\n", __FILE__, 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;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
if (!bFoundSection) {
|
|
|
|
|
|
printf("No section = %s\n", section);
|
|
|
|
|
|
} else if (!bFoundKey) {
|
|
|
|
|
|
printf("No key = %s\n", key);
|
|
|
|
|
|
}
|
|
|
|
|
|
fclose(fp);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
|
if (section == NULL || key == NULL || val == NULL || file == NULL) {
|
|
|
|
|
|
printf("%s: input parameter(s) is NULL!\n", __func__);
|
|
|
|
|
|
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__);
|
|
|
|
|
|
return 0;
|
|
|
|
|
|
}
|
|
|
|
|
|
return (atoi(strValue));
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void IniWriteValue(const char *section, char *key, char *val, const char *file) {
|
|
|
|
|
|
FILE *fp;
|
|
|
|
|
|
int i = 0, n = 0, err = 0;
|
|
|
|
|
|
int lineContentLen = 0;
|
|
|
|
|
|
int position = 0;
|
|
|
|
|
|
char lineContent[LINE_CONTENT_MAX_LEN];
|
|
|
|
|
|
char strWrite[LINE_CONTENT_MAX_LEN];
|
|
|
|
|
|
bool bFoundSection = false;
|
|
|
|
|
|
bool bFoundKey = false;
|
|
|
|
|
|
|
|
|
|
|
|
memset(lineContent, '\0', LINE_CONTENT_MAX_LEN);
|
|
|
|
|
|
memset(strWrite, '\0', LINE_CONTENT_MAX_LEN);
|
|
|
|
|
|
n = sprintf(strWrite, "%s=%s\n", key, val);
|
|
|
|
|
|
fp = fopen(file, "r+");
|
|
|
|
|
|
if (fp == NULL) {
|
|
|
|
|
|
printf("%s: Opent file %s failed.\n", __FILE__, 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;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
if (!bFoundSection) {
|
|
|
|
|
|
printf("No section = %s\n", section);
|
|
|
|
|
|
} else if (!bFoundKey) {
|
|
|
|
|
|
printf("No key = %s\n", key);
|
|
|
|
|
|
}
|
|
|
|
|
|
fclose(fp);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
|
if (section == NULL || key == NULL || val == NULL || file == NULL) {
|
|
|
|
|
|
printf("%s: input parameter(s) is NULL!\n", __func__);
|
|
|
|
|
|
return -1;
|
|
|
|
|
|
}
|
|
|
|
|
|
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);
|
|
|
|
|
|
sprintf(strValue, "%d", val);
|
|
|
|
|
|
|
|
|
|
|
|
writeStringVlaue(section, key, strValue, file);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int getDiskInfo(char *diskTotal, char *diskFree) {
|
|
|
|
|
|
DISK diskInfo;
|
|
|
|
|
|
/* 1.闁兼儳鍢茶ぐ锟<E38190>/home/濞戞挸顑夊浼存儍閸曨兘鍋撶拠宸晣闂佽鎷<EE879C> */
|
|
|
|
|
|
statfs("/", &diskInfo);
|
|
|
|
|
|
unsigned long long blocksize = diskInfo.f_bsize; //婵絽绻嬮柌娓俵ock闂佹彃鑻€垫﹢宕ラ銈嗙暠閻庢稒顨夋俊顓㈠极閿燂拷
|
|
|
|
|
|
unsigned long long totalsize = blocksize * diskInfo.f_blocks; //闁诡剝宕靛▓鎴犫偓娑欘殙婵☆參寮敮顔剧f_blocks濞戞挾顕猯ock闁汇劌瀚弳鐔兼儎閿燂拷
|
|
|
|
|
|
//printf("Total_size=%llu B =%llu KB =%llu MB = %llu GB\n",\
|
|
|
|
|
|
totalsize,totalsize>>10,totalsize>>20, totalsize>>30);
|
|
|
|
|
|
|
|
|
|
|
|
/* 2.闁兼儳鍢茶ぐ鍥ㄧ▔閳ь剚绋夌€n亜鈷栧ù锝嗙懅閳规牠姊婚弶鎴炲闁告瑯鍨抽弫銈囩矚濞差亝锛熼柣銊ュ閵囧洨浜搁敓锟<E69593> */
|
|
|
|
|
|
unsigned long long freeDisk = diskInfo.f_bfree * blocksize; //闁告挴鏅欑紞鎴犵矚濞差亝锛熼柣銊ュ閵囧洨浜搁敓锟<E69593>
|
|
|
|
|
|
unsigned long long availableDisk = diskInfo.f_bavail * blocksize; //闁告瑯鍨抽弫銈囩矚濞差亝锛熷鍫嗗啰姣<E595B0>
|
|
|
|
|
|
//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);
|
|
|
|
|
|
sprintf(diskFree, "%llu", freeDisk >> 20);
|
|
|
|
|
|
return 1;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool NetIsOk() {
|
|
|
|
|
|
double rtt;
|
|
|
|
|
|
struct hostent *host;
|
|
|
|
|
|
struct protoent *protocol;
|
|
|
|
|
|
int i, recv_status;
|
|
|
|
|
|
|
|
|
|
|
|
#ifdef _USE_DNS //濠碘€冲€归悘澶屸偓瑙勭煯缁犵喓鎷犻妷銉ф殲闁挎稑鑻崹顖炲矗椤栨瑤绨板ù锝堟硶閺併倝宕洪悢閿嬪€抽弶鈺傜椤㈡垿宕氶妶鍡樼劷缂傚啯鍨圭划鑸垫交閻愭潙澶嶉柨娑樺缁躲儲淇婇崐鍍縲.baidu.com
|
|
|
|
|
|
/* 閻犱礁澧介悿鍡涙儎椤旂偓鐣遍柛锔芥緲濞煎啯绌遍埄鍐х礀 */
|
|
|
|
|
|
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 //濠碘€冲€归悘澶嬬▔瀹ュ嫬鈻忛柣鈧妼閻撴瑩宕ュ蹇曠闁告帗鐟ヨぐ褔鎳楅悾灞炬殢ip闁革附婢樺鍐儎鐎涙ê澶嶉柛娆愬灴閳ь兛宸ヽmp闁告牕鎷戠槐锟<E6A790>
|
|
|
|
|
|
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閻犲洭鏀遍惇浼村箮閵夛附鐎<E99984>
|
|
|
|
|
|
icmp->icmp_code = 0;
|
|
|
|
|
|
icmp->icmp_cksum = 0;
|
|
|
|
|
|
icmp->icmp_seq = pkt_no;
|
|
|
|
|
|
icmp->icmp_id = pid; //閻犱礁澧介悿鍡氥亹閹惧啿顤呴弶鈺傜〒閳荤硰D濞戞挾鐦–MP闁哄秴娲ㄩ妵姘辩箔閿燂拷
|
|
|
|
|
|
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) //闁硅泛顨扖MP闁硅翰鍎遍妵鏃€绂嶅畝鍐闁告帟鍩栭弳鐔煎箲椤旇绨<EE868A>2閻庢稒顨夋俊顓熺▔閸濆嫬绀嬪ù锝呯Ф閻ゎ噣宕濋悩鐑樺闯闁哄鎷<EE9980>
|
|
|
|
|
|
{
|
|
|
|
|
|
sum += *w++;
|
|
|
|
|
|
nleft -= 2;
|
|
|
|
|
|
}
|
|
|
|
|
|
if (nleft == 1) //闁兼椿妾笴MP闁硅翰鍎遍妵鏃€绋夐崫鍕垫疄闁轰線顣﹂柌婊呪偓娑欘殙婵★拷,濞村吋鑹炬晶鎸庣▔鐎n偅浠橀柛姘凹缁斿鈧稒顨夋俊锟<E4BF8A>.闁硅泛锕ュ〒鍫曞触鎼存繄顏卞☉鎿冧簻閻⊙囨嚍閸屾繍娼掑☉鎾规〃缁斿瓨绋夐敓锟<E69593>2閻庢稒顨夋俊顓㈠极閻楀牆绁﹂柣銊ュ閻濐喚鈧稒顨夋俊锟<E4BF8A>,閺夆晜鐟ら柌锟<E69F8C>2閻庢稒顨夋俊顓㈠极閻楀牆绁﹂柣銊ュ缂嶅棛鈧稒顨夋俊顓熺▔閿燂拷0,缂備綀鍛暰缂侀硸鍨版慨锟<E685A8>
|
|
|
|
|
|
{
|
|
|
|
|
|
*(unsigned char *)(&answer) = *(unsigned char *)w;
|
|
|
|
|
|
sum += answer;
|
|
|
|
|
|
}
|
|
|
|
|
|
sum = (sum >> 16) + (sum & 0xffff);
|
|
|
|
|
|
sum += (sum >> 16);
|
|
|
|
|
|
answer = ~sum;
|
|
|
|
|
|
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; //婵€涘牚p闁硅翰鍎遍妵鏃堟⒐閸喖顔<E59696>,闁告鐗玴闁硅翰鍎遍妵鏃堟儍閸曨垱姣愰幖杈鹃檮閻栵綀绠涘鍓ь啋4
|
|
|
|
|
|
icmp = (struct icmp *)(buf + iphdrlen); //閻℃帒锕ㄧ换鍍玴闁硅翰鍎遍妵锟<E5A6B5>,闁圭ǹ娲ら幃娣欳MP闁硅翰鍎遍妵锟<E5A6B5>
|
|
|
|
|
|
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闁哄倸娲e▎銏ゅ箵韫囨艾鐗氱紒妤嬫嫹
|
|
|
|
|
|
struct sockaddr_in serverAddr {}; // Server闁革附婢樺鍐磼閹惧鈧垱鎷呴敓锟<E69593>
|
|
|
|
|
|
|
|
|
|
|
|
// 闁告帗绋戠紓鎻漮cket
|
|
|
|
|
|
if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
|
|
|
|
|
|
std::cerr << "Failed to create socket." << std::endl;
|
|
|
|
|
|
return 1;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 閻犱礁澧介悿鍝爀rver闁革附婢樺鍐╃┍閳╁啩绱<E595A9>
|
|
|
|
|
|
serverAddr.sin_family = AF_INET;
|
|
|
|
|
|
serverAddr.sin_port = htons(18393); // TCP濮掓稒枪椤撹崵绮╅姘稉闁告瑨娓圭拹锟<E68BB9>80
|
|
|
|
|
|
inet_pton(AF_INET, /*GlobalConfig::ServerIP.c_str()*/ "192.168.1.147", &serverAddr.sin_addr);
|
|
|
|
|
|
|
|
|
|
|
|
// 閺夆晝鍋炵敮鎾礆閻ф紴rver
|
|
|
|
|
|
if (connect(sockfd, reinterpret_cast<struct sockaddr *>(&serverAddr), sizeof(serverAddr)) == -1) {
|
|
|
|
|
|
std::cerr << "Failed to connect to the server." << std::endl;
|
|
|
|
|
|
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;
|
|
|
|
|
|
close(sockfd);
|
|
|
|
|
|
return 1;
|
|
|
|
|
|
} else if (static_cast<size_t>(bytesSent) != strlen(pSendData)) {
|
|
|
|
|
|
std::cerr << "Partially sent heartbeat packet." << std::endl;
|
|
|
|
|
|
close(sockfd);
|
|
|
|
|
|
return 1;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 闁稿繑濞婂Λ纰╫cket閺夆晝鍋炵敮锟<E695AE>
|
|
|
|
|
|
close(sockfd);
|
|
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Ping闁告垼濮ら弳鐔兼晬鐎圭彻meout濞戞捇缂氱粔鎾籍閼稿灚顦ч梻鍌濇彧缁辨繈宕¢弴姘辩Т闁哄嫮妫硈,10000 婵綆鍋嗛~锟<EFBD9E>=10 缂佸鎷<EE9980>
|
|
|
|
|
|
//闁瑰瓨鍔曟慨娑㈡晬濮樺磭绠查柛銉嫹0闁挎稑鑻妵鎴犳嫻閵夘垳绐楅弶鈺傛煥濞诧拷1闁硅揪鎷<E68FAA>-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;
|
|
|
|
|
|
// 閻犱焦鍎抽悾缍濞e洠鍓濇导锟<E5AFBC>
|
|
|
|
|
|
bzero(&addr, sizeof(addr));
|
|
|
|
|
|
addr.sin_family = AF_INET;
|
|
|
|
|
|
|
|
|
|
|
|
addr.sin_addr.s_addr = inet_addr(ips);
|
|
|
|
|
|
|
|
|
|
|
|
int sockfd;
|
|
|
|
|
|
// 闁告瑦鐗曠欢鐪榦cket 闁靛棴鎷<E6A3B4> 濠碘€冲€归悘澶娾柦閳ュ啿顫udo 閺夆晜鐟╅崳閿嬪濮橆厼袚闂佸尅鎷<E5B085>
|
|
|
|
|
|
sockfd = socket(AF_INET, SOCK_RAW, IPPROTO_ICMP);
|
|
|
|
|
|
if (sockfd < 0) {
|
|
|
|
|
|
print_error("ip:%s,socket error\n", ips);
|
|
|
|
|
|
return -1;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
struct timeval timeo;
|
|
|
|
|
|
// 閻犱焦鍎抽悾缍痠meOut闁哄啫鐖煎Λ锟<CE9B>
|
|
|
|
|
|
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);
|
|
|
|
|
|
close(sockfd);
|
|
|
|
|
|
return -1;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
char sendpacket[2048];
|
|
|
|
|
|
char recvpacket[2048];
|
|
|
|
|
|
// 閻犱焦鍎抽悾缍猧ng闁告牭鎷<E789AD>
|
|
|
|
|
|
memset(sendpacket, 0, sizeof(sendpacket));
|
|
|
|
|
|
|
|
|
|
|
|
pid_t pid;
|
|
|
|
|
|
// 闁告瑦鐗曠欢鐩D闁挎稑濂旂紞鏃€绋夌弧娓媙g闁汇劌鍤猠quence ID
|
|
|
|
|
|
pid = getpid();
|
|
|
|
|
|
|
|
|
|
|
|
struct ip *iph;
|
|
|
|
|
|
struct icmp *icmp;
|
|
|
|
|
|
|
|
|
|
|
|
icmp = (struct icmp *)sendpacket;
|
|
|
|
|
|
icmp->icmp_type = ICMP_ECHO; //闁搞儳鍋炲Ο澶屾嫚闁垮婀<EE8387>
|
|
|
|
|
|
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)); //闁哄稄绻濋悰锟<E682B0>
|
|
|
|
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
|
close(sockfd);
|
|
|
|
|
|
return -1;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 闁规亽鍎辫ぐ锟<E38190>
|
|
|
|
|
|
// 闁汇垹褰夌花顒勫矗椤栨繂鍘撮柟鎭掑劚瑜板牓宕氶弶鍨緭濞寸姵婀ng闁汇劌瀚花鑼驳閺冣偓缁夌兘骞侀銈囩闁圭鍋撳ù鐘劥缁绘牠鏌屽畝鍐炬矗闁烩偓鍔岄幆濠囨偝閿燂拷
|
|
|
|
|
|
int cnt = 0;
|
|
|
|
|
|
while (1) {
|
|
|
|
|
|
// 閻犱焦鍎抽悾缍痠meOut闁哄啫鐖煎Λ鍧楁晬瀹€鍐婵炲棌鍓濇晶鐘诲及椤栨粍鍩傛慨婵撶秬閹癸絾鎷呭⿰鍛殢闁活煉鎷<E78589>
|
|
|
|
|
|
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);
|
|
|
|
|
|
close(sockfd);
|
|
|
|
|
|
iRet = -1;
|
|
|
|
|
|
break;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 闁规亽鍎辫ぐ锟<E38190>
|
|
|
|
|
|
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);
|
|
|
|
|
|
if (n < 1) {
|
|
|
|
|
|
close(sockfd);
|
|
|
|
|
|
iRet = 1;
|
|
|
|
|
|
break;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
|
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闁搞儳鍋涢ˇ鏌ュ礌閸涱垱鐣遍柣妯垮煐閳ь剨鎷<E589A8>
|
|
|
|
|
|
if (icmp->icmp_type == ICMP_ECHOREPLY && icmp->icmp_id == pid) // ICMP_ECHOREPLY闁搞儳鍋炲Ο澶嬫償閺冨倻鎽<E580BB>
|
|
|
|
|
|
{
|
|
|
|
|
|
// 婵繐绲介悥鍓佷焊闁秮鍋撻埀顒勫礄閸濆嫭鍎曢柣婊愭嫹
|
|
|
|
|
|
print_info("icmp succecss ............. \n");
|
|
|
|
|
|
break;
|
|
|
|
|
|
} else if (cnt < 3) {
|
|
|
|
|
|
// 闁告熬绠戦崹顖滅磼瑜忛悽鑽ょ驳閿燂拷
|
|
|
|
|
|
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) {
|
|
|
|
|
|
close(skfd);
|
|
|
|
|
|
return -1;
|
|
|
|
|
|
}
|
|
|
|
|
|
close(skfd);
|
|
|
|
|
|
return edata.data;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 閻忓繐妫涙晶妤呭嫉椤掆偓瑜拌法鈧稒顨堥浣圭▔閸欏顎曢柛鎺戞鐠愮喖寮€涙ɑ娈剁紓浣稿閸ㄦ岸鎯冮崟顐e€婚梺璇ф嫹
|
|
|
|
|
|
std::vector<int> splitVersion(const std::string &version) {
|
|
|
|
|
|
std::vector<int> parts;
|
|
|
|
|
|
std::stringstream ss(version);
|
|
|
|
|
|
std::string part;
|
|
|
|
|
|
|
|
|
|
|
|
// 闁烩偓鍔庨崑锝嗘媴濠娾偓鐠愮喖宕氶崱娑欘吘缂佹璁g槐婵嬪礆閸℃顥忛柣妤€鐗婂﹢浼村矗閿燂拷
|
|
|
|
|
|
while (std::getline(ss, part, '.')) {
|
|
|
|
|
|
parts.push_back(std::stoi(part)); // 閻忓繐妫楅崹搴ㄥ礈閹绘帗鍊甸柣銊ュ閸庢挳宕氶崱姘ギ闁硅婢€鐠愮喖寮€涙ɑ娈<C991>
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return parts;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 婵絾妫佺欢婵囩▔閵堝嫰鍤嬮柣妤€鐗婂﹢浼村矗閿燂拷
|
|
|
|
|
|
int compareVersions(const std::string &version1, const std::string &version2) {
|
|
|
|
|
|
std::vector<int> v1 = splitVersion(version1);
|
|
|
|
|
|
std::vector<int> v2 = splitVersion(version2);
|
|
|
|
|
|
|
|
|
|
|
|
// 闁瑰灚鍎抽崺宀勫嫉閳ь剟姊圭捄銊︾暠闁绘鐗婂﹢浼村矗閻戣棄鍔ラ柛鎺戞閺嗚鲸鎯旈敓锟<E69593>
|
|
|
|
|
|
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; // 濠碘€冲€归悘澶愬蓟閹版澘鍔ラ柛鎺戞缁楀鈧稒锚濠€顏堟晬瀹€鍐炬綊濞戞搫鎷<E690AB>0
|
|
|
|
|
|
int num2 = i < v2.size() ? v2[i] : 0;
|
|
|
|
|
|
|
|
|
|
|
|
if (num1 > num2) return 1; // version1 濠㈠爢鍌滆壘 version2
|
|
|
|
|
|
if (num1 < num2) return -1; // version1 閻忓繐绻嬬花锟<E88AB1> version2
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return 0; // 闁绘鐗婂﹢浼村矗妞嬪孩绁查柛姘炬嫹
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void Binary_Bit(unsigned char *p_data, unsigned char position, int flag) {
|
|
|
|
|
|
//濞存粌鐭佺换姗€宕氶懜鐢靛帣濞达絽绉堕悿鍡涙⒖閼搁潧鐏楅柤鏉挎噸缁旓拷 position濞戞捁妗ㄧ紞鍛村极閿燂拷(濞寸媴鎷<E5AAB4>0 鐎殿喒鍋撳┑顕嗘嫹)
|
|
|
|
|
|
if (flag) {
|
|
|
|
|
|
*p_data |= 0x01 << (position);
|
|
|
|
|
|
} else {
|
|
|
|
|
|
*p_data &= ~(0x01 << (position));
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static const char *JSON_FIELD_CMD = "cmd"; //闁告绻楅锟<EE9499>: 闁告稒鍨濋幎銈団偓娑欘殕椤旓拷
|
|
|
|
|
|
static const char *JSON_FIELD_NAME = "dataWatchName"; //闁告绻楅锟<EE9499>: 缂備礁鐗忛顒勫触瀹ュ泦锟<E6B3A6>
|
|
|
|
|
|
static const char *JSON_FIELD_dataNodeGatewayNo = "dataNodeGatewayNo";
|
|
|
|
|
|
static const char *JSON_FIELD_ASSETID = "dataWatchAssetId"; //闁告绻楅锟<EE9499>: 閻犙冨妤犲洨绱撻弽褍濞<E8A48D> 閻庢稒顨嗛锟<EE9499>
|
|
|
|
|
|
static const char *JSON_FIELD_ADDEDBY = "dataWatchAddedBy"; //闁告绻楅锟<EE9499>: 婵烇綀顕ф慨鐐寸閿燂拷 閻庢稒顨嗛锟<EE9499>
|
|
|
|
|
|
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";
|