comm.cpp 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385
  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. #ifdef __linux__
  93. void parse_cpu_id(const char* file_name, const char* match_words, std::string& cpu_id)
  94. {
  95. cpu_id.c_str();
  96. std::ifstream ifs(file_name, std::ios::binary);
  97. if (!ifs.is_open())
  98. {
  99. return;
  100. }
  101. char line[4096] = { 0 };
  102. while (!ifs.eof())
  103. {
  104. ifs.getline(line, sizeof(line));
  105. if (!ifs.good())
  106. {
  107. break;
  108. }
  109. const char* cpu = strstr(line, match_words);
  110. if (NULL == cpu)
  111. {
  112. continue;
  113. }
  114. cpu += strlen(match_words);
  115. while ('\0' != cpu[0])
  116. {
  117. if (' ' != cpu[0])
  118. {
  119. cpu_id.push_back(cpu[0]);
  120. }
  121. ++cpu;
  122. }
  123. if (!cpu_id.empty())
  124. {
  125. break;
  126. }
  127. }
  128. ifs.close();
  129. }
  130. bool get_cpu_id_by_system(std::string& cpu_id, const string save_path)
  131. {
  132. //cpu_id.c_str();
  133. if (save_path.size() + strlen("/.dmidecode_result.txt") > MAX_PATH_SIZE) return false;
  134. char dmidecode_result[MAX_PATH_SIZE] = { 0 };
  135. strcpy(dmidecode_result, save_path.c_str());
  136. strcat(dmidecode_result, "/.dmidecode_result.txt");
  137. char command[512] = { 0 };
  138. snprintf(command, sizeof(command), "dmidecode -t 4 | grep ID > %s", dmidecode_result);
  139. if (0 == system(command))
  140. {
  141. parse_cpu_id(dmidecode_result, "ID:", cpu_id);
  142. }
  143. unlink(dmidecode_result);
  144. return(!cpu_id.empty());
  145. }
  146. void parse_board_serial(const char* file_name, const char* match_words, std::string& board_serial)
  147. {
  148. board_serial.c_str();
  149. std::ifstream ifs(file_name, std::ios::binary);
  150. if (!ifs.is_open())
  151. {
  152. return;
  153. }
  154. char line[4096] = { 0 };
  155. while (!ifs.eof())
  156. {
  157. ifs.getline(line, sizeof(line));
  158. if (!ifs.good())
  159. {
  160. break;
  161. }
  162. const char* board = strstr(line, match_words);
  163. if (NULL == board)
  164. {
  165. continue;
  166. }
  167. board += strlen(match_words);
  168. while ('\0' != board[0])
  169. {
  170. if (' ' != board[0])
  171. {
  172. board_serial.push_back(board[0]);
  173. }
  174. ++board;
  175. }
  176. if ("None" == board_serial)
  177. {
  178. board_serial.clear();
  179. continue;
  180. }
  181. if (!board_serial.empty())
  182. {
  183. break;
  184. }
  185. }
  186. ifs.close();
  187. }
  188. bool get_board_serial_by_system(std::string& board_serial,const string save_path)
  189. {
  190. //board_serial.c_str();
  191. if (save_path.size() + strlen("/.dmidecode_result.txt") > MAX_PATH_SIZE) return false;
  192. char dmidecode_result[MAX_PATH_SIZE] = { 0 };
  193. strcpy(dmidecode_result, save_path.c_str());
  194. strcat(dmidecode_result, "/.dmidecode_result.txt");
  195. char command[512] = { 0 };
  196. snprintf(command, sizeof(command), "dmidecode -t 2 | grep Serial > %s", dmidecode_result);
  197. if (0 == system(command))
  198. {
  199. parse_board_serial(dmidecode_result, "Serial Number:", board_serial);
  200. }
  201. else {
  202. return false;
  203. }
  204. unlink(dmidecode_result);
  205. return true;
  206. }
  207. bool parse_disk_serial(const char* line, int line_size, const char* match_words, std::string& serial_no)
  208. {
  209. const char* serial_s = strstr(line, match_words);
  210. if (NULL == serial_s)
  211. {
  212. return(false);
  213. }
  214. serial_s += strlen(match_words);
  215. while (isspace(serial_s[0]))
  216. {
  217. ++serial_s;
  218. }
  219. const char* serial_e = line + line_size;
  220. const char* comma = strchr(serial_s, ',');
  221. if (NULL != comma)
  222. {
  223. serial_e = comma;
  224. }
  225. while (serial_e > serial_s && isspace(serial_e[-1]))
  226. {
  227. --serial_e;
  228. }
  229. if (serial_e <= serial_s)
  230. {
  231. return(false);
  232. }
  233. std::string(serial_s, serial_e).swap(serial_no);
  234. return(true);
  235. }
  236. void get_disk_serial(const char* file_name, const char* match_words, std::vector<string>& serial_no)
  237. {
  238. std::ifstream ifs(file_name, std::ios::binary);
  239. if (!ifs.is_open())
  240. {
  241. return;
  242. }
  243. char line[4096] = { 0 };
  244. while (!ifs.eof())
  245. {
  246. ifs.getline(line, sizeof(line));
  247. if (!ifs.good())
  248. {
  249. break;
  250. }
  251. if (0 == ifs.gcount())
  252. {
  253. continue;
  254. }
  255. string disk_serial;
  256. if (parse_disk_serial(line, ifs.gcount() - 1, match_words, disk_serial))
  257. {
  258. //break;
  259. serial_no.push_back(disk_serial);
  260. }
  261. }
  262. ifs.close();
  263. }
  264. bool isSpace(char x) { return x == ' '; }
  265. bool get_disk_serial_by_system(std::vector<string>& serial_no, int& errCode, const string save_path)
  266. {
  267. //if (save_path.size() + strlen("/.lshw_result.txt") > MAX_PATH_SIZE) return false;
  268. //char lshw_result[MAX_PATH_SIZE] = { 0 };
  269. //strcpy(lshw_result, save_path.c_str());
  270. //strcat(lshw_result, "/.lshw_result.txt");
  271. //char command[512] = { 0 };
  272. //snprintf(command, sizeof(command), "lshw -class disk | grep serial > %s", lshw_result);
  273. //if (0 == system(command))
  274. //{
  275. // get_disk_serial(lshw_result, "serial:", serial_no);
  276. //}
  277. //else {
  278. // return false;
  279. //}
  280. errCode = 0;
  281. struct hd_driveid id;
  282. int fd = open("/dev/hda", O_RDONLY | O_NONBLOCK);
  283. if (fd < 0 && errno == 2) {
  284. fd = open("/dev/sda", O_RDONLY | O_NONBLOCK);
  285. if (fd < 0 && errno ==2 ) {
  286. fd = open("/dev/nvme0n1", O_RDONLY | O_NONBLOCK);
  287. if (fd < 0) {
  288. perror("read failed:");
  289. errCode = errno;
  290. return false;
  291. }
  292. }
  293. }
  294. if (!ioctl(fd, HDIO_GET_IDENTITY, &id)) {
  295. string xx((const char*)(id.serial_no));
  296. xx.erase(remove_if(xx.begin(), xx.end(), isSpace), xx.end());
  297. serial_no.push_back(xx);
  298. }
  299. close(fd);
  300. //unlink(lshw_result);
  301. return(true);
  302. }
  303. #include <unistd.h>
  304. bool dir_is_exist(string dirPath) {
  305. return (access(dirPath.c_str(), F_OK) == 0);
  306. }
  307. #include <sys/stat.h>
  308. //创建成功返回0
  309. int dir_create(string dirPath) {
  310. if (!dir_is_exist(dirPath)) {
  311. return mkdir(dirPath.c_str(), S_IRWXU);
  312. }
  313. return 0;
  314. }
  315. #include <dirent.h>
  316. bool set_system_time_by_sec(int sec)
  317. {
  318. struct timeval tv;
  319. gettimeofday(&tv, NULL);
  320. tv.tv_sec += sec;
  321. //cout << tv.tv_sec << endl;
  322. if (settimeofday(&tv, NULL) < 0)
  323. {
  324. return false;
  325. }
  326. return true;
  327. }
  328. #endif // __linux__