comm.cpp 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434
  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 str2int(const string str, int& ret) {
  66. if (str.size() == 0) return 1;
  67. ret = 0;
  68. int symbol = 0;
  69. for (int i = 0; i < str.size(); i++) {
  70. if (i == 0 && str[i] == '-') {
  71. symbol = 1;
  72. continue;
  73. }
  74. if (i == 0 && str[i] == '+') continue;
  75. if (i == 0) {
  76. while (str[i] == '0' && i < str.size()) {
  77. ++i;
  78. }
  79. if (i == str.size()) return 0;
  80. }
  81. if (str[i] < '0' || str[i] >'9') return 2;
  82. ret += (str[i] - '0') * pow(10, str.size() - i - 1);
  83. }
  84. if (symbol) ret -= 2 * ret;
  85. return 0;
  86. }
  87. int str2int(const string str) {
  88. int ret;
  89. str2int(str, ret);
  90. return ret;
  91. }
  92. unsigned char Ch2Hex(char ch)
  93. {
  94. static const char* hex = "0123456789ABCDEF";
  95. for (unsigned char i = 0; i != 16; ++i)
  96. if (ch == hex[i])
  97. return i;
  98. return 0;
  99. }
  100. char* Hex2Str(const char* src, int& dstLen)
  101. {
  102. int i = 0;
  103. int cnt = 0;
  104. int len = strlen(src);
  105. unsigned char* d = new unsigned char[len];
  106. memset(d, 0, len);
  107. while (*src)
  108. {
  109. if (i & 1)
  110. {
  111. d[cnt++] |= Ch2Hex(*src);
  112. }
  113. else
  114. {
  115. d[cnt] = Ch2Hex(*src) << 4;
  116. }
  117. src++;
  118. i++;
  119. }
  120. dstLen = cnt;
  121. return (char*)d;
  122. }
  123. char* Str2Hex(const char* src, int srcLen)
  124. {
  125. string ret;
  126. static const char* hex = "0123456789ABCDEF";
  127. for (int i = 0; i != srcLen; ++i)
  128. {
  129. ret.push_back(hex[(src[i] >> 4) & 0xf]);
  130. ret.push_back(hex[src[i] & 0xf]);
  131. }
  132. char* tmp = new char[ret.length() + 1];
  133. memset(tmp, 0, ret.length() + 1);
  134. memcpy(tmp, ret.c_str(), ret.length());
  135. return tmp;
  136. }
  137. #ifdef __linux__
  138. void parse_cpu_id(const char* file_name, const char* match_words, std::string& cpu_id)
  139. {
  140. cpu_id.c_str();
  141. std::ifstream ifs(file_name, std::ios::binary);
  142. if (!ifs.is_open())
  143. {
  144. return;
  145. }
  146. char line[4096] = { 0 };
  147. while (!ifs.eof())
  148. {
  149. ifs.getline(line, sizeof(line));
  150. if (!ifs.good())
  151. {
  152. break;
  153. }
  154. const char* cpu = strstr(line, match_words);
  155. if (NULL == cpu)
  156. {
  157. continue;
  158. }
  159. cpu += strlen(match_words);
  160. while ('\0' != cpu[0])
  161. {
  162. if (' ' != cpu[0])
  163. {
  164. cpu_id.push_back(cpu[0]);
  165. }
  166. ++cpu;
  167. }
  168. if (!cpu_id.empty())
  169. {
  170. break;
  171. }
  172. }
  173. ifs.close();
  174. }
  175. bool get_cpu_id_by_system(std::string& cpu_id, const string save_path)
  176. {
  177. //cpu_id.c_str();
  178. if (save_path.size() + strlen("/.dmidecode_result.txt") > MAX_PATH_SIZE) return false;
  179. char dmidecode_result[MAX_PATH_SIZE] = { 0 };
  180. strcpy(dmidecode_result, save_path.c_str());
  181. strcat(dmidecode_result, "/.dmidecode_result.txt");
  182. char command[512] = { 0 };
  183. snprintf(command, sizeof(command), "dmidecode -t 4 | grep ID > %s", dmidecode_result);
  184. if (0 == system(command))
  185. {
  186. parse_cpu_id(dmidecode_result, "ID:", cpu_id);
  187. }
  188. unlink(dmidecode_result);
  189. return(!cpu_id.empty());
  190. }
  191. void parse_board_serial(const char* file_name, const char* match_words, std::string& board_serial)
  192. {
  193. board_serial.c_str();
  194. std::ifstream ifs(file_name, std::ios::binary);
  195. if (!ifs.is_open())
  196. {
  197. return;
  198. }
  199. char line[4096] = { 0 };
  200. while (!ifs.eof())
  201. {
  202. ifs.getline(line, sizeof(line));
  203. if (!ifs.good())
  204. {
  205. break;
  206. }
  207. const char* board = strstr(line, match_words);
  208. if (NULL == board)
  209. {
  210. continue;
  211. }
  212. board += strlen(match_words);
  213. while ('\0' != board[0])
  214. {
  215. if (' ' != board[0])
  216. {
  217. board_serial.push_back(board[0]);
  218. }
  219. ++board;
  220. }
  221. if ("None" == board_serial)
  222. {
  223. board_serial.clear();
  224. continue;
  225. }
  226. if (!board_serial.empty())
  227. {
  228. break;
  229. }
  230. }
  231. ifs.close();
  232. }
  233. bool get_board_serial_by_system(std::string& board_serial,const string save_path)
  234. {
  235. //board_serial.c_str();
  236. if (save_path.size() + strlen("/.dmidecode_result.txt") > MAX_PATH_SIZE) return false;
  237. char dmidecode_result[MAX_PATH_SIZE] = { 0 };
  238. strcpy(dmidecode_result, save_path.c_str());
  239. strcat(dmidecode_result, "/.dmidecode_result.txt");
  240. char command[512] = { 0 };
  241. snprintf(command, sizeof(command), "dmidecode -t 2 | grep Serial > %s", dmidecode_result);
  242. if (0 == system(command))
  243. {
  244. parse_board_serial(dmidecode_result, "Serial Number:", board_serial);
  245. }
  246. else {
  247. return false;
  248. }
  249. unlink(dmidecode_result);
  250. return true;
  251. }
  252. bool parse_disk_serial(const char* line, int line_size, const char* match_words, std::string& serial_no)
  253. {
  254. const char* serial_s = strstr(line, match_words);
  255. if (NULL == serial_s)
  256. {
  257. return(false);
  258. }
  259. serial_s += strlen(match_words);
  260. while (isspace(serial_s[0]))
  261. {
  262. ++serial_s;
  263. }
  264. const char* serial_e = line + line_size;
  265. const char* comma = strchr(serial_s, ',');
  266. if (NULL != comma)
  267. {
  268. serial_e = comma;
  269. }
  270. while (serial_e > serial_s && isspace(serial_e[-1]))
  271. {
  272. --serial_e;
  273. }
  274. if (serial_e <= serial_s)
  275. {
  276. return(false);
  277. }
  278. std::string(serial_s, serial_e).swap(serial_no);
  279. return(true);
  280. }
  281. void get_disk_serial(const char* file_name, const char* match_words, std::vector<string>& serial_no)
  282. {
  283. std::ifstream ifs(file_name, std::ios::binary);
  284. if (!ifs.is_open())
  285. {
  286. return;
  287. }
  288. char line[4096] = { 0 };
  289. while (!ifs.eof())
  290. {
  291. ifs.getline(line, sizeof(line));
  292. if (!ifs.good())
  293. {
  294. break;
  295. }
  296. if (0 == ifs.gcount())
  297. {
  298. continue;
  299. }
  300. string disk_serial;
  301. if (parse_disk_serial(line, ifs.gcount() - 1, match_words, disk_serial))
  302. {
  303. //break;
  304. serial_no.push_back(disk_serial);
  305. }
  306. }
  307. ifs.close();
  308. }
  309. bool isSpace(char x) { return x == ' '; }
  310. bool get_disk_serial_by_system(std::vector<string>& serial_no, int& errCode, const string save_path)
  311. {
  312. //if (save_path.size() + strlen("/.lshw_result.txt") > MAX_PATH_SIZE) return false;
  313. //char lshw_result[MAX_PATH_SIZE] = { 0 };
  314. //strcpy(lshw_result, save_path.c_str());
  315. //strcat(lshw_result, "/.lshw_result.txt");
  316. //char command[512] = { 0 };
  317. //snprintf(command, sizeof(command), "lshw -class disk | grep serial > %s", lshw_result);
  318. //if (0 == system(command))
  319. //{
  320. // get_disk_serial(lshw_result, "serial:", serial_no);
  321. //}
  322. //else {
  323. // return false;
  324. //}
  325. errCode = 0;
  326. struct hd_driveid id;
  327. int fd = open("/dev/hda", O_RDONLY | O_NONBLOCK);
  328. if (fd < 0 && errno == 2) {
  329. fd = open("/dev/sda", O_RDONLY | O_NONBLOCK);
  330. if (fd < 0 && errno ==2 ) {
  331. fd = open("/dev/nvme0n1", O_RDONLY | O_NONBLOCK);
  332. if (fd < 0) {
  333. perror("read failed:");
  334. errCode = errno;
  335. return false;
  336. }
  337. }
  338. }
  339. if (!ioctl(fd, HDIO_GET_IDENTITY, &id)) {
  340. string xx((const char*)(id.serial_no));
  341. xx.erase(remove_if(xx.begin(), xx.end(), isSpace), xx.end());
  342. serial_no.push_back(xx);
  343. }
  344. close(fd);
  345. //unlink(lshw_result);
  346. return(true);
  347. }
  348. #include <unistd.h>
  349. bool dir_is_exist(string dirPath) {
  350. return (access(dirPath.c_str(), F_OK) == 0);
  351. }
  352. #include <sys/stat.h>
  353. //创建成功返回0
  354. int dir_create(string dirPath) {
  355. if (!dir_is_exist(dirPath)) {
  356. return mkdir(dirPath.c_str(), S_IRWXU);
  357. }
  358. return 0;
  359. }
  360. #include <dirent.h>
  361. bool set_system_time_by_sec(int sec)
  362. {
  363. struct timeval tv;
  364. gettimeofday(&tv, NULL);
  365. tv.tv_sec += sec;
  366. //cout << tv.tv_sec << endl;
  367. if (settimeofday(&tv, NULL) < 0)
  368. {
  369. return false;
  370. }
  371. return true;
  372. }
  373. #endif // __linux__