comm.cpp 13 KB

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