comm.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643
  1. #include "comm.h"
  2. #include <cstdarg>
  3. #ifndef _WIN32
  4. #include <sys/ioctl.h>
  5. #include <sys/stat.h>
  6. #include <linux/hdreg.h>
  7. #include <sys/fcntl.h>
  8. #endif //NOT _WIN32
  9. #define MAX_PATH_SIZE 256
  10. void GetNewForm(const char* form, char* newForm) {
  11. int indexNum = 0;
  12. int acount = 0;
  13. newForm[0] = '{';
  14. for (int i = 0; i < strlen(form); i++)
  15. {
  16. //if((i-1 >= 0 && form[i]=='\\') || (i-1 < 0))
  17. if (form[i] == '%') {
  18. if (acount != 0)
  19. {
  20. newForm[++indexNum] = '"';
  21. if (acount % 2 != 0) {
  22. newForm[++indexNum] = ':';
  23. }
  24. else {
  25. newForm[++indexNum] = ',';
  26. }
  27. }
  28. newForm[++indexNum] = '"';
  29. acount++;
  30. }
  31. if (form[i] == ' ') continue;
  32. newForm[++indexNum] = form[i];
  33. }
  34. newForm[++indexNum] = '"';
  35. newForm[++indexNum] = '}';
  36. }
  37. string GetOutPutStr(const char* form, ...) {
  38. char* newForm = new char[strlen(form) * 3 + 5];
  39. memset(newForm, 0, strlen(form) * 3 + 5);
  40. if (strlen(form) < 2) {
  41. strcpy(newForm, "{\"\"}");
  42. }
  43. else {
  44. GetNewForm(form, newForm);
  45. }
  46. va_list vaList;
  47. va_start(vaList, form);
  48. #ifdef RVC_OS_WIN
  49. int acount = _vscprintf(newForm, vaList);
  50. #else
  51. int acount = vsnprintf(0, 0, newForm, vaList);
  52. va_end(vaList);
  53. va_start(vaList, form);
  54. #endif
  55. char* buf = new char[acount + 1];
  56. memset(buf, 0, acount + 1);
  57. vsprintf(buf, newForm, vaList);
  58. va_end(vaList);
  59. string ret;
  60. ret.assign(buf);
  61. delete buf;
  62. delete newForm;
  63. return ret;
  64. }
  65. int no_begin_with(const char strA[], const char strB[]) {
  66. if (strA == NULL) return 1;
  67. if (strB == NULL) return 1;
  68. if (strlen(strA) == 0 || strlen(strB) == 0 || strlen(strA) < strlen(strB)) return 1;
  69. const char* pA = strA;
  70. const char* pB = strB;
  71. while (*pA != '\0' && *pB != '\0') {
  72. if (*pA != *pB) return 1;
  73. pA++;
  74. pB++;
  75. }
  76. return 0;
  77. }
  78. int str2int(const string str, int& ret) {
  79. if (str.size() == 0) return 1;
  80. ret = 0;
  81. int symbol = 0;
  82. for (int i = 0; i < str.size(); i++) {
  83. if (i == 0 && str[i] == '-') {
  84. symbol = 1;
  85. continue;
  86. }
  87. if (i == 0 && str[i] == '+') continue;
  88. if (i == 0) {
  89. while (str[i] == '0' && i < str.size()) {
  90. ++i;
  91. }
  92. if (i == str.size()) return 0;
  93. }
  94. if (str[i] < '0' || str[i] >'9') return 2;
  95. ret += (str[i] - '0') * pow(10, str.size() - i - 1);
  96. }
  97. if (symbol) ret -= 2 * ret;
  98. return 0;
  99. }
  100. int str2int(const string str) {
  101. int ret;
  102. str2int(str, ret);
  103. return ret;
  104. }
  105. vector<string> split_str(string strA, string strB) {
  106. vector<string> ret;
  107. //cout << "ret:" << ret.size() << endl;
  108. if (strA.size() == 0 || strB.size() == 0) {
  109. return ret;
  110. }
  111. char* chA = new char[strA.size()];
  112. char* chB = new char[strB.size()];
  113. memset(chA, 0, sizeof(chA));
  114. memset(chB, 0, sizeof(chB));
  115. strcpy(chA, strA.c_str());
  116. strcpy(chB, strB.c_str());
  117. char* tmp = strtok(chA, chB);
  118. while (tmp) {
  119. //cout << "tmp:" << tmp << endl;
  120. ret.push_back(tmp);
  121. tmp = strtok(NULL, chB);
  122. }
  123. delete[] chA;
  124. delete[] chB;
  125. return ret;
  126. }
  127. int ip2byte(const string str, BYTE ip[]) {
  128. vector<string> ret = split_str(str, ".");
  129. vector<string>::iterator it = ret.begin();
  130. int index = 0;
  131. while (it != ret.end()) {
  132. int tmp = 0;
  133. if (str2int(*it, tmp)) return 1;
  134. else {
  135. if (tmp > 255) return 2;
  136. else {
  137. ip[index] = tmp;
  138. index++;
  139. }
  140. }
  141. it++;
  142. }
  143. return 0;
  144. }
  145. unsigned char Ch2Hex(char ch)
  146. {
  147. static const char* hex = "0123456789ABCDEF";
  148. for (unsigned char i = 0; i != 16; ++i)
  149. if (ch == hex[i])
  150. return i;
  151. return 0;
  152. }
  153. char* Hex2Str(const char* src, int& dstLen)
  154. {
  155. int i = 0;
  156. int cnt = 0;
  157. int len = strlen(src);
  158. unsigned char* d = new unsigned char[len];
  159. memset(d, 0, len);
  160. while (*src)
  161. {
  162. if (i & 1)
  163. {
  164. d[cnt++] |= Ch2Hex(*src);
  165. }
  166. else
  167. {
  168. d[cnt] = Ch2Hex(*src) << 4;
  169. }
  170. src++;
  171. i++;
  172. }
  173. dstLen = cnt;
  174. return (char*)d;
  175. }
  176. char* Str2Hex(const char* src, int srcLen)
  177. {
  178. string ret;
  179. static const char* hex = "0123456789ABCDEF";
  180. for (int i = 0; i != srcLen; ++i)
  181. {
  182. ret.push_back(hex[(src[i] >> 4) & 0xf]);
  183. ret.push_back(hex[src[i] & 0xf]);
  184. }
  185. char* tmp = new char[ret.length() + 1];
  186. memset(tmp, 0, ret.length() + 1);
  187. memcpy(tmp, ret.c_str(), ret.length());
  188. return tmp;
  189. }
  190. #ifdef __linux__
  191. int getIPFromLinux(char* ip) {
  192. //if (strlen(ip) < 15) return 1;
  193. struct ifaddrs* ifAddrStruct = NULL;
  194. void* tmpAddrPtr = NULL;
  195. getifaddrs(&ifAddrStruct);
  196. while (ifAddrStruct != NULL) {
  197. if (ifAddrStruct->ifa_addr->sa_family == AF_INET) {
  198. // is a valid IP4 Address
  199. tmpAddrPtr = &((struct sockaddr_in*)ifAddrStruct->ifa_addr)->sin_addr;
  200. char addressBuffer[INET_ADDRSTRLEN];
  201. inet_ntop(AF_INET, tmpAddrPtr, addressBuffer, INET_ADDRSTRLEN);
  202. //printf("%s IP Address %s\n", ifAddrStruct->ifa_name, addressBuffer);
  203. if (!no_begin_with(addressBuffer, "99") || !no_begin_with(addressBuffer, "10")) {
  204. memset(ip, 0, sizeof(ip));
  205. strcpy(ip, addressBuffer);
  206. }
  207. else if (strlen(ip) == 0 && no_begin_with(addressBuffer, "127.0")) {
  208. memset(ip, 0, sizeof(ip));
  209. strcpy(ip, addressBuffer);
  210. }
  211. }
  212. /*
  213. else if (ifAddrStruct->ifa_addr->sa_family==AF_INET6) { // check it is IP6
  214. // is a valid IP6 Address
  215. tmpAddrPtr=&((struct sockaddr_in *)ifAddrStruct->ifa_addr)->sin_addr;
  216. char addressBuffer[INET6_ADDRSTRLEN];
  217. inet_ntop(AF_INET6, tmpAddrPtr, addressBuffer, INET6_ADDRSTRLEN);
  218. printf("%s IP Address %s\n", ifAddrStruct->ifa_name, addressBuffer);
  219. }
  220. */
  221. ifAddrStruct = ifAddrStruct->ifa_next;
  222. }
  223. //printf("ip=%s\n", ip);
  224. return 0;
  225. }
  226. //invalid function
  227. bool get_cpu_id_by_asm(std::string& cpu_id)
  228. {
  229. return(true);
  230. }
  231. void parse_cpu_id(const char* file_name, const char* match_words, std::string& cpu_id)
  232. {
  233. cpu_id.c_str();
  234. std::ifstream ifs(file_name, std::ios::binary);
  235. if (!ifs.is_open())
  236. {
  237. return;
  238. }
  239. char line[4096] = { 0 };
  240. while (!ifs.eof())
  241. {
  242. ifs.getline(line, sizeof(line));
  243. if (!ifs.good())
  244. {
  245. break;
  246. }
  247. const char* cpu = strstr(line, match_words);
  248. if (NULL == cpu)
  249. {
  250. continue;
  251. }
  252. cpu += strlen(match_words);
  253. while ('\0' != cpu[0])
  254. {
  255. if (' ' != cpu[0])
  256. {
  257. cpu_id.push_back(cpu[0]);
  258. }
  259. ++cpu;
  260. }
  261. if (!cpu_id.empty())
  262. {
  263. break;
  264. }
  265. }
  266. ifs.close();
  267. }
  268. bool get_cpu_id_by_system(std::string& cpu_id, const string save_path)
  269. {
  270. //cpu_id.c_str();
  271. if (save_path.size() + strlen("/.dmidecode_result.txt") > MAX_PATH_SIZE) return false;
  272. char dmidecode_result[MAX_PATH_SIZE] = { 0 };
  273. strcpy(dmidecode_result, save_path.c_str());
  274. strcat(dmidecode_result, "/.dmidecode_result.txt");
  275. char command[512] = { 0 };
  276. snprintf(command, sizeof(command), "dmidecode -t 4 | grep ID > %s", dmidecode_result);
  277. if (0 == system(command))
  278. {
  279. parse_cpu_id(dmidecode_result, "ID:", cpu_id);
  280. }
  281. unlink(dmidecode_result);
  282. return(!cpu_id.empty());
  283. }
  284. void parse_board_serial(const char* file_name, const char* match_words, std::string& board_serial)
  285. {
  286. board_serial.c_str();
  287. std::ifstream ifs(file_name, std::ios::binary);
  288. if (!ifs.is_open())
  289. {
  290. return;
  291. }
  292. char line[4096] = { 0 };
  293. while (!ifs.eof())
  294. {
  295. ifs.getline(line, sizeof(line));
  296. if (!ifs.good())
  297. {
  298. break;
  299. }
  300. const char* board = strstr(line, match_words);
  301. if (NULL == board)
  302. {
  303. continue;
  304. }
  305. board += strlen(match_words);
  306. while ('\0' != board[0])
  307. {
  308. if (' ' != board[0])
  309. {
  310. board_serial.push_back(board[0]);
  311. }
  312. ++board;
  313. }
  314. if ("None" == board_serial)
  315. {
  316. board_serial.clear();
  317. continue;
  318. }
  319. if (!board_serial.empty())
  320. {
  321. break;
  322. }
  323. }
  324. ifs.close();
  325. }
  326. bool get_board_serial_by_system(std::string& board_serial,const string save_path)
  327. {
  328. //board_serial.c_str();
  329. if (save_path.size() + strlen("/.dmidecode_result.txt") > MAX_PATH_SIZE) return false;
  330. char dmidecode_result[MAX_PATH_SIZE] = { 0 };
  331. strcpy(dmidecode_result, save_path.c_str());
  332. strcat(dmidecode_result, "/.dmidecode_result.txt");
  333. char command[512] = { 0 };
  334. snprintf(command, sizeof(command), "dmidecode -t 2 | grep Serial > %s", dmidecode_result);
  335. if (0 == system(command))
  336. {
  337. parse_board_serial(dmidecode_result, "Serial Number:", board_serial);
  338. }
  339. else {
  340. return false;
  341. }
  342. unlink(dmidecode_result);
  343. return true;
  344. }
  345. bool parse_disk_serial(const char* line, int line_size, const char* match_words, std::string& serial_no)
  346. {
  347. const char* serial_s = strstr(line, match_words);
  348. if (NULL == serial_s)
  349. {
  350. return(false);
  351. }
  352. serial_s += strlen(match_words);
  353. while (isspace(serial_s[0]))
  354. {
  355. ++serial_s;
  356. }
  357. const char* serial_e = line + line_size;
  358. const char* comma = strchr(serial_s, ',');
  359. if (NULL != comma)
  360. {
  361. serial_e = comma;
  362. }
  363. while (serial_e > serial_s && isspace(serial_e[-1]))
  364. {
  365. --serial_e;
  366. }
  367. if (serial_e <= serial_s)
  368. {
  369. return(false);
  370. }
  371. std::string(serial_s, serial_e).swap(serial_no);
  372. return(true);
  373. }
  374. void get_disk_serial(const char* file_name, const char* match_words, std::vector<string>& serial_no)
  375. {
  376. std::ifstream ifs(file_name, std::ios::binary);
  377. if (!ifs.is_open())
  378. {
  379. return;
  380. }
  381. char line[4096] = { 0 };
  382. while (!ifs.eof())
  383. {
  384. ifs.getline(line, sizeof(line));
  385. if (!ifs.good())
  386. {
  387. break;
  388. }
  389. if (0 == ifs.gcount())
  390. {
  391. continue;
  392. }
  393. string disk_serial;
  394. if (parse_disk_serial(line, ifs.gcount() - 1, match_words, disk_serial))
  395. {
  396. //break;
  397. serial_no.push_back(disk_serial);
  398. }
  399. }
  400. ifs.close();
  401. }
  402. bool isSpace(char x) { return x == ' '; }
  403. bool get_disk_serial_by_system(std::vector<string>& serial_no, int& errCode, const string save_path)
  404. {
  405. //if (save_path.size() + strlen("/.lshw_result.txt") > MAX_PATH_SIZE) return false;
  406. //char lshw_result[MAX_PATH_SIZE] = { 0 };
  407. //strcpy(lshw_result, save_path.c_str());
  408. //strcat(lshw_result, "/.lshw_result.txt");
  409. //char command[512] = { 0 };
  410. //snprintf(command, sizeof(command), "lshw -class disk | grep serial > %s", lshw_result);
  411. //if (0 == system(command))
  412. //{
  413. // get_disk_serial(lshw_result, "serial:", serial_no);
  414. //}
  415. //else {
  416. // return false;
  417. //}
  418. errCode = 0;
  419. struct hd_driveid id;
  420. int fd = open("/dev/hda", O_RDONLY | O_NONBLOCK);
  421. if (fd < 0 && errno == 2) {
  422. fd = open("/dev/sda", O_RDONLY | O_NONBLOCK);
  423. if (fd < 0 && errno ==2 ) {
  424. fd = open("/dev/nvme0n1", O_RDONLY | O_NONBLOCK);
  425. if (fd < 0) {
  426. perror("read failed:");
  427. errCode = errno;
  428. return false;
  429. }
  430. }
  431. }
  432. if (!ioctl(fd, HDIO_GET_IDENTITY, &id)) {
  433. string xx((const char*)(id.serial_no));
  434. xx.erase(remove_if(xx.begin(), xx.end(), isSpace), xx.end());
  435. serial_no.push_back(xx);
  436. }
  437. close(fd);
  438. //unlink(lshw_result);
  439. return(true);
  440. }
  441. #include <unistd.h>
  442. bool file_is_exist(string filePath) {
  443. return (access(filePath.c_str(), F_OK) == 0);
  444. }
  445. bool dir_is_exist(string dirPath) {
  446. return (access(dirPath.c_str(), F_OK) == 0);
  447. }
  448. //创建成功返回0,创建失败返回1,已经存在返回2
  449. int file_create(string filePath) {
  450. if (!file_is_exist(filePath)) {
  451. FILE* fp;
  452. if (fp = fopen(filePath.c_str(), "w")) {
  453. fclose(fp);
  454. return 0;
  455. }
  456. else {
  457. return 1;
  458. }
  459. }
  460. return 2;
  461. }
  462. #include <sys/stat.h>
  463. //创建成功返回0
  464. int dir_create(string dirPath) {
  465. if (!dir_is_exist(dirPath)) {
  466. return mkdir(dirPath.c_str(), S_IRWXU);
  467. }
  468. return 0;
  469. }
  470. //删除成功返回0
  471. int remove_file(string filePath) {
  472. return remove(filePath.c_str());
  473. }
  474. #include <dirent.h>
  475. int remove_dir(string dirPath) {
  476. DIR* dir;
  477. struct dirent* ptr;
  478. dir = opendir(dirPath.c_str());
  479. if (dir == NULL) return 1;
  480. while ((ptr = readdir(dir)) != NULL) {
  481. if (strcmp(ptr->d_name, ".") == 0 || strcmp(ptr->d_name, "..") == 0) continue;
  482. char path[1024] = { 0 };
  483. strcpy(path, dirPath.c_str());
  484. strcat(path, "/");
  485. strcat(path, ptr->d_name);
  486. if (ptr->d_type == DT_DIR) {
  487. remove_dir(path);
  488. }
  489. else {
  490. if (remove_file(path) != 0) return 1;
  491. }
  492. }
  493. closedir(dir);
  494. return rmdir(dirPath.c_str());
  495. }
  496. bool set_system_time(TIME* _time)
  497. {
  498. struct tm p;
  499. p.tm_year = _time->year - 1900;
  500. p.tm_mon = _time->month - 1;
  501. p.tm_mday = _time->day;
  502. p.tm_hour = _time->hour;
  503. p.tm_min = _time->minute;
  504. p.tm_sec = _time->second;
  505. //cout << p.tm_year << " " << p.tm_mon << " " << p.tm_mday << " " << p.tm_hour << " " << p.tm_min << " " << p.tm_sec << endl;
  506. time_t sec = mktime(&p);
  507. //cout << sec << endl;
  508. struct timeval tv;
  509. tv.tv_sec = sec;
  510. //cout << tv.tv_sec << endl;
  511. if (settimeofday(&tv, NULL) < 0)
  512. {
  513. return false;
  514. }
  515. return true;
  516. }
  517. bool set_system_time_by_sec(int sec)
  518. {
  519. struct timeval tv;
  520. gettimeofday(&tv, NULL);
  521. tv.tv_sec += sec;
  522. //cout << tv.tv_sec << endl;
  523. if (settimeofday(&tv, NULL) < 0)
  524. {
  525. return false;
  526. }
  527. return true;
  528. }
  529. TIME* get_system_time()
  530. {
  531. TIME* t = new TIME();
  532. time_t timep;
  533. time(&timep);
  534. struct tm* p;
  535. p = localtime(&timep);
  536. t->year = 1900 + p->tm_year;
  537. t->month = 1 + p->tm_mon;
  538. t->day = p->tm_mday;
  539. t->hour = p->tm_hour;
  540. t->minute = p->tm_min;
  541. t->second = p->tm_sec;
  542. return t;
  543. }
  544. string time2str(const TIME* tim) {
  545. if (tim == NULL) return "";
  546. string ret;
  547. char timeStr[16] = { 0 };
  548. sprintf(timeStr, "%04d%02d%02d%02d%02d%02d", tim->year,
  549. tim->month, tim->day, tim->hour, tim->minute, tim->second);
  550. ret = timeStr;
  551. return ret;
  552. }
  553. TIME* str2time(string str) {
  554. if (str.length() < 14) return NULL;
  555. TIME* ret = new TIME();
  556. ret->year = str2int(str.substr(0, 4));
  557. ret->month = str2int(str.substr(4, 2));
  558. ret->day = str2int(str.substr(6, 2));
  559. ret->hour = str2int(str.substr(8, 2));
  560. ret->minute = str2int(str.substr(10, 2));
  561. ret->second = str2int(str.substr(12, 2));
  562. return ret;
  563. }
  564. #endif // __linux__