DeviceSimulator.h 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  1. //用于定义实现一些模拟器通用的函数功能
  2. #include <string>
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <fstream>
  6. #include <string>
  7. #include <map>
  8. #include "path.h"
  9. #ifdef RVC_OS_WIN
  10. #include <WinSock2.h>
  11. #include <direct.h>
  12. #define GetCurrentDir _getcwd
  13. #else
  14. #include <arpa/inet.h> //ntohl
  15. #include <unistd.h> // Linux系统中
  16. #include <netdb.h>
  17. #include <net/if.h>
  18. #include <arpa/inet.h>
  19. #include <sys/ioctl.h>
  20. #include <sys/types.h>
  21. #include <sys/socket.h>
  22. #include <netinet/in.h>
  23. #define GetCurrentDir getcwd
  24. #endif
  25. std::string GetCurrentDirectory() {
  26. char buff[256];
  27. GetCurrentDir(buff, 256);
  28. std::string current_working_directory(buff);
  29. return current_working_directory;
  30. }
  31. //读取配置文件功能类
  32. class iniReader
  33. {
  34. public:
  35. iniReader()
  36. {
  37. }
  38. ~iniReader()
  39. {
  40. }
  41. bool ReadConfig(const std::string& filename)
  42. {
  43. settings_.clear();
  44. std::ifstream infile(filename.c_str());//构造默认调用open,所以可以不调用open
  45. //std::ifstream infile;
  46. //infile.open(filename.c_str());
  47. //bool ret = infile.is_open()
  48. if (!infile) {
  49. return false;
  50. }
  51. std::string line, key, value, section;
  52. std::map<std::string, std::string> k_v;
  53. std::map<std::string, std::map<std::string, std::string> >::iterator it;
  54. while (getline(infile, line))
  55. {
  56. if (AnalyseLine(line, section, key, value))
  57. {
  58. it = settings_.find(section);
  59. if (it != settings_.end())
  60. {
  61. k_v[key] = value;
  62. it->second = k_v;
  63. }
  64. else
  65. {
  66. k_v.clear();
  67. settings_.insert(std::make_pair(section, k_v));
  68. }
  69. }
  70. key.clear();
  71. value.clear();
  72. }
  73. infile.close();
  74. return true;
  75. }
  76. std::string ReadString(const char* section, const char* item, const char* default_value)
  77. {
  78. std::string tmp_s(section);
  79. std::string tmp_i(item);
  80. std::string def(default_value);
  81. std::map<std::string, std::string> k_v;
  82. std::map<std::string, std::string>::iterator it_item;
  83. std::map<std::string, std::map<std::string, std::string> >::iterator it;
  84. it = settings_.find(tmp_s);
  85. if (it == settings_.end())
  86. {
  87. //printf("111");
  88. return def;
  89. }
  90. k_v = it->second;
  91. it_item = k_v.find(tmp_i);
  92. if (it_item == k_v.end())
  93. {
  94. //printf("222");
  95. return def;
  96. }
  97. return it_item->second;
  98. }
  99. int ReadInt(const char* section, const char* item, const int& default_value)
  100. {
  101. std::string tmp_s(section);
  102. std::string tmp_i(item);
  103. std::map<std::string, std::string> k_v;
  104. std::map<std::string, std::string>::iterator it_item;
  105. std::map<std::string, std::map<std::string, std::string> >::iterator it;
  106. it = settings_.find(tmp_s);
  107. if (it == settings_.end())
  108. {
  109. return default_value;
  110. }
  111. k_v = it->second;
  112. it_item = k_v.find(tmp_i);
  113. if (it_item == k_v.end())
  114. {
  115. return default_value;
  116. }
  117. return atoi(it_item->second.c_str());
  118. }
  119. float ReadFloat(const char* section, const char* item, const float& default_value)
  120. {
  121. std::string tmp_s(section);
  122. std::string tmp_i(item);
  123. std::map<std::string, std::string> k_v;
  124. std::map<std::string, std::string>::iterator it_item;
  125. std::map<std::string, std::map<std::string, std::string> >::iterator it;
  126. it = settings_.find(tmp_s);
  127. if (it == settings_.end())
  128. {
  129. return default_value;
  130. }
  131. k_v = it->second;
  132. it_item = k_v.find(tmp_i);
  133. if (it_item == k_v.end())
  134. {
  135. return default_value;
  136. }
  137. return atof(it_item->second.c_str());
  138. }
  139. private:
  140. bool IsSpace(char c)
  141. {
  142. if (' ' == c || '\t' == c)
  143. return true;
  144. return false;
  145. }
  146. bool IsCommentChar(char c)
  147. {
  148. switch (c)
  149. {
  150. case '#':
  151. return true;
  152. default:
  153. return false;
  154. }
  155. }
  156. void Trim(std::string& str)
  157. {
  158. if (str.empty())
  159. {
  160. return;
  161. }
  162. int i, start_pos, end_pos;
  163. for (i = 0; i < str.size(); ++i) {
  164. if (!IsSpace(str[i])) {
  165. break;
  166. }
  167. }
  168. if (i == str.size())
  169. {
  170. str = "";
  171. return;
  172. }
  173. start_pos = i;
  174. for (i = str.size() - 1; i >= 0; --i) {
  175. if (!IsSpace(str[i])) {
  176. break;
  177. }
  178. }
  179. end_pos = i;
  180. str = str.substr(start_pos, end_pos - start_pos + 1);
  181. }
  182. bool AnalyseLine(const std::string& line, std::string& section, std::string& key, std::string& value)
  183. {
  184. if (line.empty())
  185. return false;
  186. int start_pos = 0, end_pos = line.size() - 1, pos, s_startpos, s_endpos;
  187. if ((pos = line.find("#")) != -1)
  188. {
  189. if (0 == pos)
  190. {
  191. return false;
  192. }
  193. end_pos = pos - 1;
  194. }
  195. if (((s_startpos = line.find("[")) != -1) && ((s_endpos = line.find("]"))) != -1)
  196. {
  197. section = line.substr(s_startpos + 1, s_endpos - 1);
  198. return true;
  199. }
  200. std::string new_line = line.substr(start_pos, start_pos + 1 - end_pos);
  201. if ((pos = new_line.find('=')) == -1)
  202. return false;
  203. key = new_line.substr(0, pos);
  204. value = new_line.substr(pos + 1, end_pos + 1 - (pos + 1));
  205. Trim(key);
  206. if (key.empty()) {
  207. return false;
  208. }
  209. Trim(value);
  210. if ((pos = value.find("\r")) > 0)
  211. {
  212. value.replace(pos, 1, "");
  213. }
  214. if ((pos = value.find("\n")) > 0)
  215. {
  216. value.replace(pos, 1, "");
  217. }
  218. return true;
  219. }
  220. private:
  221. //std::map<std::string, std::string> settings_;
  222. std::map<std::string, std::map<std::string, std::string> >settings_;
  223. };
  224. //获取硬件模拟器地址
  225. std::string GetSimulatorUrl()
  226. {
  227. std::string depCfgPath = GetCurrentDirectory() + SPLIT_SLASH_STR + "dep" + SPLIT_SLASH_STR + "cmbsz.ini";
  228. iniReader iniRead;
  229. bool ret = iniRead.ReadConfig(depCfgPath);
  230. if (!ret)
  231. {
  232. return "";
  233. }
  234. std::string urlStr = iniRead.ReadString("server", "url", "");
  235. return urlStr;
  236. }
  237. //读取本地IP功能函数
  238. std::string GetLocalIP()
  239. {
  240. #ifdef RVC_OS_WIN
  241. hostent* ent = gethostbyname(NULL);
  242. if (ent && ent->h_addr_list[0] != NULL)
  243. {
  244. int i = 0;
  245. for (; ent->h_addr_list[i] != NULL; ++i)
  246. {
  247. struct in_addr* in = (struct in_addr*)ent->h_addr_list[i];
  248. if (in->S_un.S_un_b.s_b1 == 99 || in->S_un.S_un_b.s_b1 == 10)
  249. break;
  250. }
  251. if (ent->h_addr_list[i] == NULL)
  252. i = 0;
  253. auto in = (struct in_addr*)ent->h_addr_list[i];
  254. char xIP[64] = {};
  255. sprintf(xIP, "%d.%d.%d.%d", in->S_un.S_un_b.s_b1, in->S_un.S_un_b.s_b2, in->S_un.S_un_b.s_b3, in->S_un.S_un_b.s_b4);
  256. //LOG4VTM(INFO, ("ip:%s", xIP));
  257. return std::string(xIP);
  258. }
  259. #else
  260. std::string re = "";
  261. int sfd, intr;
  262. struct ifreq buf[16];
  263. struct ifconf ifc;
  264. sfd = socket(AF_INET, SOCK_DGRAM, 0);
  265. if (sfd < 0)
  266. return "wrong 1: get ip socket failed";
  267. ifc.ifc_len = sizeof(buf);
  268. ifc.ifc_buf = (caddr_t)buf;
  269. if (ioctl(sfd, SIOCGIFCONF, (char*)&ifc))
  270. return "wrong 2: get ip ioctl failed";
  271. intr = ifc.ifc_len / sizeof(struct ifreq);
  272. while (intr-- > 0 && ioctl(sfd, SIOCGIFADDR, (char*)&buf[intr]));
  273. close(sfd);
  274. unsigned long ip = ntohl(((struct sockaddr_in*)(&buf[intr].ifr_addr))->sin_addr.s_addr);
  275. char* str = new char[1024];
  276. sprintf(str, "%u.%u.%u.%u", ip >> 24 & 0xFF, ip >> 16 & 0xFF, ip >> 8 & 0xFF, ip >> 0 & 0xFF);
  277. re = str;
  278. return re;
  279. #endif
  280. }
  281. std::string GetValueFromIni(const std::string& filePath, const std::string& section, const std::string& key) {
  282. std::ifstream file(filePath);
  283. std::string line;
  284. std::string value;
  285. bool sectionFound = false;
  286. while (std::getline(file, line)) {
  287. if (!sectionFound && line == "[" + section + "]") {
  288. sectionFound = true;
  289. }
  290. else if (sectionFound) {
  291. size_t found = line.find(key);
  292. if (found != std::string::npos) {
  293. size_t equalsPos = line.find("=");
  294. value = line.substr(equalsPos + 1);
  295. break;
  296. }
  297. }
  298. }
  299. return value;
  300. }
  301. int StrBuf2HexBuf(LPCTSTR strBuf, PBYTE* hexBuf)
  302. {
  303. int len = strlen(strBuf);
  304. if (len == 0 || len % 2 != 0)
  305. return 0;
  306. BYTE* buf = new BYTE[len / 2];
  307. if (buf == NULL)
  308. return 0;
  309. int j = 0;
  310. for (int i = 0; i < len;)
  311. {
  312. int tmpVal;
  313. sscanf(strBuf + i, "%2X", &tmpVal);
  314. buf[j] = tmpVal;
  315. //buf[j] = char2int(strBuf[i])*16 + char2int(strBuf[i+1]);
  316. i += 2;
  317. j++;
  318. }
  319. //memcpy(buf,strBuf,len);
  320. *hexBuf = buf;
  321. return j;
  322. }
  323. int HexBuf2StrBuf(PBYTE hexBuf, char** strBuf, DWORD len)
  324. {
  325. char* tmpStr = *strBuf;
  326. int count = 0;
  327. for (int i = 0; i < len; ++i)
  328. {
  329. sprintf(tmpStr + count, "%0.2X", hexBuf[i]);
  330. count += 2;
  331. }
  332. return 0;
  333. }