DeviceSimulator.h 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445
  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. #include "CommEntityRestful.hpp"
  10. #include <SpBase.h>
  11. #include "IHttpFunc.h"
  12. #ifdef RVC_OS_WIN
  13. #include <WinSock2.h>
  14. #include <direct.h>
  15. #define GetCurrentDir _getcwd
  16. #else
  17. #include <arpa/inet.h> //ntohl
  18. #include <unistd.h> // Linux系统中
  19. #include <netdb.h>
  20. #include <net/if.h>
  21. #include <arpa/inet.h>
  22. #include <sys/ioctl.h>
  23. #include <sys/types.h>
  24. #include <sys/socket.h>
  25. #include <netinet/in.h>
  26. #define GetCurrentDir getcwd
  27. #endif
  28. #define REFLECTION(var) #var
  29. using namespace std;
  30. static void HttpsLogCallBack(const char* logtxt) {}
  31. string GetCurrentDirectory() {
  32. char buff[256];
  33. GetCurrentDir(buff, 256);
  34. string current_working_directory(buff);
  35. return current_working_directory;
  36. }
  37. //读取配置文件功能类
  38. class iniReader
  39. {
  40. public:
  41. iniReader()
  42. {
  43. }
  44. ~iniReader()
  45. {
  46. }
  47. bool ReadConfig(const string& filename)
  48. {
  49. settings_.clear();
  50. ifstream infile(filename.c_str());//构造默认调用open,所以可以不调用open
  51. //ifstream infile;
  52. //infile.open(filename.c_str());
  53. //bool ret = infile.is_open()
  54. if (!infile) {
  55. return false;
  56. }
  57. string line, key, value, section;
  58. map<string, string> k_v;
  59. map<string, map<string, string> >::iterator it;
  60. while (getline(infile, line))
  61. {
  62. if (AnalyseLine(line, section, key, value))
  63. {
  64. it = settings_.find(section);
  65. if (it != settings_.end())
  66. {
  67. k_v[key] = value;
  68. it->second = k_v;
  69. }
  70. else
  71. {
  72. k_v.clear();
  73. settings_.insert(make_pair(section, k_v));
  74. }
  75. }
  76. key.clear();
  77. value.clear();
  78. }
  79. infile.close();
  80. return true;
  81. }
  82. string ReadString(const char* section, const char* item, const char* default_value)
  83. {
  84. string tmp_s(section);
  85. string tmp_i(item);
  86. string def(default_value);
  87. map<string, string> k_v;
  88. map<string, string>::iterator it_item;
  89. map<string, map<string, string> >::iterator it;
  90. it = settings_.find(tmp_s);
  91. if (it == settings_.end())
  92. {
  93. //printf("111");
  94. return def;
  95. }
  96. k_v = it->second;
  97. it_item = k_v.find(tmp_i);
  98. if (it_item == k_v.end())
  99. {
  100. //printf("222");
  101. return def;
  102. }
  103. return it_item->second;
  104. }
  105. int ReadInt(const char* section, const char* item, const int& default_value)
  106. {
  107. string tmp_s(section);
  108. string tmp_i(item);
  109. map<string, string> k_v;
  110. map<string, string>::iterator it_item;
  111. map<string, map<string, string> >::iterator it;
  112. it = settings_.find(tmp_s);
  113. if (it == settings_.end())
  114. {
  115. return default_value;
  116. }
  117. k_v = it->second;
  118. it_item = k_v.find(tmp_i);
  119. if (it_item == k_v.end())
  120. {
  121. return default_value;
  122. }
  123. return atoi(it_item->second.c_str());
  124. }
  125. float ReadFloat(const char* section, const char* item, const float& default_value)
  126. {
  127. string tmp_s(section);
  128. string tmp_i(item);
  129. map<string, string> k_v;
  130. map<string, string>::iterator it_item;
  131. map<string, map<string, string> >::iterator it;
  132. it = settings_.find(tmp_s);
  133. if (it == settings_.end())
  134. {
  135. return default_value;
  136. }
  137. k_v = it->second;
  138. it_item = k_v.find(tmp_i);
  139. if (it_item == k_v.end())
  140. {
  141. return default_value;
  142. }
  143. return atof(it_item->second.c_str());
  144. }
  145. private:
  146. bool IsSpace(char c)
  147. {
  148. if (' ' == c || '\t' == c)
  149. return true;
  150. return false;
  151. }
  152. bool IsCommentChar(char c)
  153. {
  154. switch (c)
  155. {
  156. case '#':
  157. return true;
  158. default:
  159. return false;
  160. }
  161. }
  162. void Trim(string& str)
  163. {
  164. if (str.empty())
  165. {
  166. return;
  167. }
  168. int i, start_pos, end_pos;
  169. for (i = 0; i < str.size(); ++i) {
  170. if (!IsSpace(str[i])) {
  171. break;
  172. }
  173. }
  174. if (i == str.size())
  175. {
  176. str = "";
  177. return;
  178. }
  179. start_pos = i;
  180. for (i = str.size() - 1; i >= 0; --i) {
  181. if (!IsSpace(str[i])) {
  182. break;
  183. }
  184. }
  185. end_pos = i;
  186. str = str.substr(start_pos, end_pos - start_pos + 1);
  187. }
  188. bool AnalyseLine(const string& line, string& section, string& key, string& value)
  189. {
  190. if (line.empty())
  191. return false;
  192. int start_pos = 0, end_pos = line.size() - 1, pos, s_startpos, s_endpos;
  193. if ((pos = line.find("#")) != -1)
  194. {
  195. if (0 == pos)
  196. {
  197. return false;
  198. }
  199. end_pos = pos - 1;
  200. }
  201. if (((s_startpos = line.find("[")) != -1) && ((s_endpos = line.find("]"))) != -1)
  202. {
  203. section = line.substr(s_startpos + 1, s_endpos - 1);
  204. return true;
  205. }
  206. string new_line = line.substr(start_pos, start_pos + 1 - end_pos);
  207. if ((pos = new_line.find('=')) == -1)
  208. return false;
  209. key = new_line.substr(0, pos);
  210. value = new_line.substr(pos + 1, end_pos + 1 - (pos + 1));
  211. Trim(key);
  212. if (key.empty()) {
  213. return false;
  214. }
  215. Trim(value);
  216. if ((pos = value.find("\r")) > 0)
  217. {
  218. value.replace(pos, 1, "");
  219. }
  220. if ((pos = value.find("\n")) > 0)
  221. {
  222. value.replace(pos, 1, "");
  223. }
  224. return true;
  225. }
  226. private:
  227. //map<string, string> settings_;
  228. map<string, map<string, string> >settings_;
  229. };
  230. typedef struct SimulatorCommonReq : CHTTPReq {
  231. std::string ip;
  232. std::string entityName;
  233. std::string adapterInterName;
  234. string ToJson() {
  235. Json::Value value;
  236. value[REFLECTION(ip)] = ip;
  237. value[REFLECTION(entityName)] = entityName;
  238. value[REFLECTION(adapterInterName)] = adapterInterName;
  239. Json::FastWriter writer;
  240. string strData = writer.write(value);
  241. return strData;
  242. }
  243. } SimulatorCommonReq;
  244. typedef struct SimulatorCommonAns : CHTTPRet {
  245. DevCategoryInfo devCategoryInfo;
  246. bool success;
  247. bool result;
  248. int errNum;
  249. bool Parse(string strData) {
  250. Json::Value root;
  251. Json::Reader reader;
  252. reader.parse(strData, root, false);
  253. success = root[REFLECTION(success)].asBool();
  254. result = root["data"][REFLECTION(result)].asBool();
  255. errNum = root["data"][REFLECTION(errNum)].asInt();
  256. devCategoryInfo.eState = (DevStateEnum)root["data"][REFLECTION(eState)].asInt();
  257. devCategoryInfo.version.wBuild = root["data"][REFLECTION(wBuild)].asInt();
  258. devCategoryInfo.version.wMajor = root["data"][REFLECTION(wMajor)].asInt();
  259. devCategoryInfo.version.wMinor = root["data"][REFLECTION(wMinor)].asInt();
  260. devCategoryInfo.version.wRevision = root["data"][REFLECTION(wRevision)].asInt();
  261. strcpy(devCategoryInfo.szModel, root["data"][REFLECTION(szModel)].asString().c_str());
  262. strcpy(devCategoryInfo.szType, root["data"][REFLECTION(szType)].asString().c_str());
  263. strcpy(devCategoryInfo.szVendor, root["data"][REFLECTION(szVendor)].asString().c_str());
  264. return true;
  265. }
  266. }SimulatorCommonAns;
  267. //获取硬件模拟器地址
  268. string GetSimulatorUrl()
  269. {
  270. string depCfgPath = GetCurrentDirectory() + SPLIT_SLASH_STR + "dep" + SPLIT_SLASH_STR + "cmbsz.ini";
  271. iniReader iniRead;
  272. bool ret = iniRead.ReadConfig(depCfgPath);
  273. if (!ret)
  274. {
  275. return "";
  276. }
  277. string urlStr = iniRead.ReadString("server", "url", "");
  278. return urlStr;
  279. }
  280. //读取本地IP功能函数
  281. string GetLocalIP()
  282. {
  283. #ifdef RVC_OS_WIN
  284. hostent* ent = gethostbyname(NULL);
  285. if (ent && ent->h_addr_list[0] != NULL)
  286. {
  287. int i = 0;
  288. for (; ent->h_addr_list[i] != NULL; ++i)
  289. {
  290. struct in_addr* in = (struct in_addr*)ent->h_addr_list[i];
  291. if (in->S_un.S_un_b.s_b1 == 99 || in->S_un.S_un_b.s_b1 == 10)
  292. break;
  293. }
  294. if (ent->h_addr_list[i] == NULL)
  295. i = 0;
  296. auto in = (struct in_addr*)ent->h_addr_list[i];
  297. char xIP[64] = {};
  298. 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);
  299. //LOG4VTM(INFO, ("ip:%s", xIP));
  300. return string(xIP);
  301. }
  302. return "";
  303. #else
  304. string re = "";
  305. int sfd, intr;
  306. struct ifreq buf[16];
  307. struct ifconf ifc;
  308. sfd = socket(AF_INET, SOCK_DGRAM, 0);
  309. if (sfd < 0)
  310. return "wrong 1: get ip socket failed";
  311. ifc.ifc_len = sizeof(buf);
  312. ifc.ifc_buf = (caddr_t)buf;
  313. if (ioctl(sfd, SIOCGIFCONF, (char*)&ifc))
  314. return "wrong 2: get ip ioctl failed";
  315. intr = ifc.ifc_len / sizeof(struct ifreq);
  316. while (intr-- > 0 && ioctl(sfd, SIOCGIFADDR, (char*)&buf[intr]));
  317. close(sfd);
  318. unsigned long ip = ntohl(((struct sockaddr_in*)(&buf[intr].ifr_addr))->sin_addr.s_addr);
  319. char* str = new char[1024];
  320. sprintf(str, "%u.%u.%u.%u", ip >> 24 & 0xFF, ip >> 16 & 0xFF, ip >> 8 & 0xFF, ip >> 0 & 0xFF);
  321. re = str;
  322. return re;
  323. #endif
  324. }
  325. string GetValueFromIni(const string& filePath, const string& section, const string& key) {
  326. ifstream file(filePath);
  327. string line;
  328. string value;
  329. bool sectionFound = false;
  330. while (getline(file, line)) {
  331. if (!sectionFound && line == "[" + section + "]") {
  332. sectionFound = true;
  333. }
  334. else if (sectionFound) {
  335. size_t found = line.find(key);
  336. if (found != string::npos) {
  337. size_t equalsPos = line.find("=");
  338. value = line.substr(equalsPos + 1);
  339. break;
  340. }
  341. }
  342. }
  343. return value;
  344. }
  345. int StrBuf2HexBuf(LPCTSTR strBuf, PBYTE* hexBuf)
  346. {
  347. int len = strlen(strBuf);
  348. if (len == 0 || len % 2 != 0)
  349. return 0;
  350. BYTE* buf = new BYTE[len / 2];
  351. if (buf == NULL)
  352. return 0;
  353. int j = 0;
  354. for (int i = 0; i < len;)
  355. {
  356. int tmpVal;
  357. sscanf(strBuf + i, "%2X", &tmpVal);
  358. buf[j] = tmpVal;
  359. //buf[j] = char2int(strBuf[i])*16 + char2int(strBuf[i+1]);
  360. i += 2;
  361. j++;
  362. }
  363. //memcpy(buf,strBuf,len);
  364. *hexBuf = buf;
  365. return j;
  366. }
  367. int HexBuf2StrBuf(PBYTE hexBuf, char** strBuf, DWORD len)
  368. {
  369. char* tmpStr = *strBuf;
  370. int count = 0;
  371. for (int i = 0; i < len; ++i)
  372. {
  373. sprintf(tmpStr + count, "%0.2X", hexBuf[i]);
  374. count += 2;
  375. }
  376. return 0;
  377. }
  378. template <typename T>
  379. ErrorCodeEnum SimulatorHttpFunction(string entityName, string devFuncName, T& obj)
  380. {
  381. IHttpFunc* client;
  382. client = create_http(HttpsLogCallBack);
  383. SimulatorCommonReq simulatorCommonReq;
  384. simulatorCommonReq.ip = GetLocalIP();
  385. simulatorCommonReq.entityName = entityName;
  386. simulatorCommonReq.adapterInterName = devFuncName;
  387. simulatorCommonReq.m_url = GetSimulatorUrl();
  388. ErrorCodeEnum err = Error_NotImpl;
  389. bool ret = client->Post(simulatorCommonReq, obj);
  390. if (ret) {
  391. if (!obj.success)
  392. {
  393. return Error_NotImpl;
  394. }
  395. else
  396. {
  397. err = (ErrorCodeEnum)obj.errNum;
  398. }
  399. }
  400. else
  401. {
  402. return Error_NotImpl;
  403. }
  404. return err;
  405. }