DeviceSimulator.h 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435
  1. #pragma once
  2. //用于定义实现一些模拟器通用的函数功能
  3. #include <iostream>
  4. #include <string>
  5. #include <cstdlib>
  6. #include <stdio.h>
  7. #include <stdlib.h>
  8. #include <fstream>
  9. #include <map>
  10. #include <limits.h>
  11. #include "DeviceBaseClass.h"
  12. #include "path.h"
  13. #include "json/json.h"
  14. #include "CommSimulatorRestful.hpp"
  15. #include "RestfulFunc.h"
  16. #ifdef _MSC_VER
  17. #include <windows.h>
  18. #include <WinSock2.h>
  19. #include <direct.h>
  20. #define FindLibName "cmblog4vendor.dll"
  21. #else
  22. #include <dlfcn.h>
  23. #include <arpa/inet.h> //ntohl
  24. #include <unistd.h> // Linux系统中
  25. #include <netdb.h>
  26. #include <net/if.h>
  27. #include <arpa/inet.h>
  28. #include <sys/ioctl.h>
  29. #include <sys/types.h>
  30. #include <sys/socket.h>
  31. #include <netinet/in.h>
  32. #endif
  33. using namespace std;
  34. #define DEFAULT_DATA_URL "http://emulatoruser.paasuat.cmbchina.cn/emulator/avs/retrieveEmulatorData"
  35. //获取模拟器库文件所在目录
  36. string GetCurrentDirectory() {
  37. #ifdef _MSC_VER
  38. HMODULE hModule = GetModuleHandle(FindLibName);
  39. char path[MAX_PATH];
  40. GetModuleFileName(hModule, path, MAX_PATH);
  41. string fullPath(path);
  42. size_t pos = fullPath.find_last_of("\\/");
  43. return fullPath.substr(0, pos);
  44. #else
  45. Dl_info dlInfo;
  46. dladdr((void*)GetCurrentDirectory, &dlInfo);
  47. char* path = realpath(dlInfo.dli_fname, NULL);
  48. string fullPath(path);
  49. free(path);
  50. size_t pos = fullPath.find_last_of("/");
  51. return fullPath.substr(0, pos);
  52. #endif
  53. }
  54. //读取配置文件功能类
  55. class iniReader
  56. {
  57. public:
  58. iniReader()
  59. {
  60. }
  61. ~iniReader()
  62. {
  63. }
  64. bool ReadConfig(const string& filename)
  65. {
  66. settings_.clear();
  67. ifstream infile(filename.c_str());//构造默认调用open,所以可以不调用open
  68. //ifstream infile;
  69. //infile.open(filename.c_str());
  70. //bool ret = infile.is_open()
  71. if (!infile) {
  72. return false;
  73. }
  74. string line, key, value, section;
  75. map<string, string> k_v;
  76. map<string, map<string, string> >::iterator it;
  77. while (getline(infile, line))
  78. {
  79. if (AnalyseLine(line, section, key, value))
  80. {
  81. it = settings_.find(section);
  82. if (it != settings_.end())
  83. {
  84. k_v[key] = value;
  85. it->second = k_v;
  86. }
  87. else
  88. {
  89. k_v.clear();
  90. settings_.insert(make_pair(section, k_v));
  91. }
  92. }
  93. key.clear();
  94. value.clear();
  95. }
  96. infile.close();
  97. return true;
  98. }
  99. string ReadString(const char* section, const char* item, const char* default_value)
  100. {
  101. string tmp_s(section);
  102. string tmp_i(item);
  103. string def(default_value);
  104. map<string, string> k_v;
  105. map<string, string>::iterator it_item;
  106. map<string, map<string, string> >::iterator it;
  107. it = settings_.find(tmp_s);
  108. if (it == settings_.end())
  109. {
  110. //printf("111");
  111. return def;
  112. }
  113. k_v = it->second;
  114. it_item = k_v.find(tmp_i);
  115. if (it_item == k_v.end())
  116. {
  117. //printf("222");
  118. return def;
  119. }
  120. return it_item->second;
  121. }
  122. int ReadInt(const char* section, const char* item, const int& default_value)
  123. {
  124. string tmp_s(section);
  125. string tmp_i(item);
  126. map<string, string> k_v;
  127. map<string, string>::iterator it_item;
  128. map<string, map<string, string> >::iterator it;
  129. it = settings_.find(tmp_s);
  130. if (it == settings_.end())
  131. {
  132. return default_value;
  133. }
  134. k_v = it->second;
  135. it_item = k_v.find(tmp_i);
  136. if (it_item == k_v.end())
  137. {
  138. return default_value;
  139. }
  140. return atoi(it_item->second.c_str());
  141. }
  142. float ReadFloat(const char* section, const char* item, const float& default_value)
  143. {
  144. string tmp_s(section);
  145. string tmp_i(item);
  146. map<string, string> k_v;
  147. map<string, string>::iterator it_item;
  148. map<string, map<string, string> >::iterator it;
  149. it = settings_.find(tmp_s);
  150. if (it == settings_.end())
  151. {
  152. return default_value;
  153. }
  154. k_v = it->second;
  155. it_item = k_v.find(tmp_i);
  156. if (it_item == k_v.end())
  157. {
  158. return default_value;
  159. }
  160. return atof(it_item->second.c_str());
  161. }
  162. private:
  163. bool IsSpace(char c)
  164. {
  165. if (' ' == c || '\t' == c)
  166. return true;
  167. return false;
  168. }
  169. bool IsCommentChar(char c)
  170. {
  171. switch (c)
  172. {
  173. case '#':
  174. return true;
  175. default:
  176. return false;
  177. }
  178. }
  179. void Trim(string& str)
  180. {
  181. if (str.empty())
  182. {
  183. return;
  184. }
  185. int i, start_pos, end_pos;
  186. for (i = 0; i < str.size(); ++i) {
  187. if (!IsSpace(str[i])) {
  188. break;
  189. }
  190. }
  191. if (i == str.size())
  192. {
  193. str = "";
  194. return;
  195. }
  196. start_pos = i;
  197. for (i = str.size() - 1; i >= 0; --i) {
  198. if (!IsSpace(str[i])) {
  199. break;
  200. }
  201. }
  202. end_pos = i;
  203. str = str.substr(start_pos, end_pos - start_pos + 1);
  204. }
  205. bool AnalyseLine(const string& line, string& section, string& key, string& value)
  206. {
  207. if (line.empty())
  208. return false;
  209. int start_pos = 0, end_pos = line.size() - 1, pos, s_startpos, s_endpos;
  210. if ((pos = line.find("#")) != -1)
  211. {
  212. if (0 == pos)
  213. {
  214. return false;
  215. }
  216. end_pos = pos - 1;
  217. }
  218. if (((s_startpos = line.find("[")) != -1) && ((s_endpos = line.find("]"))) != -1)
  219. {
  220. section = line.substr(s_startpos + 1, s_endpos - 1);
  221. return true;
  222. }
  223. string new_line = line.substr(start_pos, start_pos + 1 - end_pos);
  224. if ((pos = new_line.find('=')) == -1)
  225. return false;
  226. key = new_line.substr(0, pos);
  227. value = new_line.substr(pos + 1, end_pos + 1 - (pos + 1));
  228. Trim(key);
  229. if (key.empty()) {
  230. return false;
  231. }
  232. Trim(value);
  233. if ((pos = value.find("\r")) > 0)
  234. {
  235. value.replace(pos, 1, "");
  236. }
  237. if ((pos = value.find("\n")) > 0)
  238. {
  239. value.replace(pos, 1, "");
  240. }
  241. return true;
  242. }
  243. private:
  244. //map<string, string> settings_;
  245. map<string, map<string, string> >settings_;
  246. };
  247. //获取硬件模拟器数据服务地址
  248. string GetSimulatorUrl()
  249. {
  250. string depCfgPath = GetCurrentDirectory() + SPLIT_SLASH_STR + "cmbsz.ini";
  251. iniReader iniRead;
  252. bool ret = iniRead.ReadConfig(depCfgPath);
  253. if (!ret)
  254. {
  255. return DEFAULT_DATA_URL;
  256. }
  257. string urlStr = iniRead.ReadString("server", "url", "");
  258. if (urlStr.length() <= 0)
  259. {
  260. urlStr = DEFAULT_DATA_URL;
  261. }
  262. return urlStr;
  263. }
  264. //读取本地IP功能函数
  265. string GetLocalIP()
  266. {
  267. #ifdef _MSC_VER
  268. hostent* ent = gethostbyname(NULL);
  269. if (ent && ent->h_addr_list[0] != NULL)
  270. {
  271. int i = 0;
  272. for (; ent->h_addr_list[i] != NULL; ++i)
  273. {
  274. struct in_addr* in = (struct in_addr*)ent->h_addr_list[i];
  275. if (in->S_un.S_un_b.s_b1 == 99 || in->S_un.S_un_b.s_b1 == 10)
  276. break;
  277. }
  278. if (ent->h_addr_list[i] == NULL)
  279. i = 0;
  280. auto in = (struct in_addr*)ent->h_addr_list[i];
  281. char xIP[64] = {};
  282. 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);
  283. //LOG4VTM(INFO, ("ip:%s", xIP));
  284. return string(xIP);
  285. }
  286. return "";
  287. #else
  288. string re = "";
  289. int sfd, intr;
  290. struct ifreq buf[16];
  291. struct ifconf ifc;
  292. sfd = socket(AF_INET, SOCK_DGRAM, 0);
  293. if (sfd < 0)
  294. return "wrong 1: get ip socket failed";
  295. ifc.ifc_len = sizeof(buf);
  296. ifc.ifc_buf = (caddr_t)buf;
  297. if (ioctl(sfd, SIOCGIFCONF, (char*)&ifc))
  298. return "wrong 2: get ip ioctl failed";
  299. intr = ifc.ifc_len / sizeof(struct ifreq);
  300. while (intr-- > 0 && ioctl(sfd, SIOCGIFADDR, (char*)&buf[intr]));
  301. close(sfd);
  302. unsigned long ip = ntohl(((struct sockaddr_in*)(&buf[intr].ifr_addr))->sin_addr.s_addr);
  303. char* str = new char[1024];
  304. sprintf(str, "%u.%u.%u.%u", ip >> 24 & 0xFF, ip >> 16 & 0xFF, ip >> 8 & 0xFF, ip >> 0 & 0xFF);
  305. re = str;
  306. return re;
  307. #endif
  308. }
  309. ///////////////以下为模拟器通用http访问实现////////////////////
  310. typedef struct SimulatorCommonReq
  311. {
  312. string ip;
  313. string entityName;
  314. string adapterInterName;
  315. JSONCONVERT2OBJECT_MEMEBER_REGISTER(ip, entityName, adapterInterName)
  316. }SimulatorCommonReq;
  317. typedef struct SimulatorDevCategoryAns
  318. {
  319. int errNum;
  320. string szType; //device type sth like "CMB.Printer.HP1234"
  321. string szModel; //device model
  322. string szVendor; //device vendor
  323. int eState; //device status
  324. struct DevSoftVersion
  325. {
  326. int wMajor; //release major version
  327. int wMinor; //release minor version
  328. int wRevision; //bug repair version with the major and minor version remains the same
  329. int wBuild; //compile version
  330. JSONCONVERT2OBJECT_MEMEBER_REGISTER(wMajor, wMinor, wRevision, wBuild)
  331. }version; //software version
  332. JSONCONVERT2OBJECT_MEMEBER_REGISTER(errNum, szType, szModel, szVendor, eState, version)
  333. }SimulatorDevCategoryAns;
  334. typedef struct SimulatorDevErrInfoAns
  335. {
  336. int errNum;
  337. int dwErrMsgLen;
  338. string szErrMsg;
  339. JSONCONVERT2OBJECT_MEMEBER_REGISTER(errNum, dwErrMsgLen, szErrMsg)
  340. }SimulatorDevErrInfoAns;
  341. ErrorCodeEnum SimulatorHttpFunction(string entityName, string devFuncName)
  342. {
  343. ErrorCodeEnum err = Error_NotImpl;
  344. SimulatorCommonReq simulatorCommonReq;
  345. simulatorCommonReq.ip = GetLocalIP();
  346. simulatorCommonReq.entityName = entityName;
  347. simulatorCommonReq.adapterInterName = devFuncName;
  348. HttpClientResponseResult result;
  349. HttpClientRequestConfig config(HttpRequestMethod::POST, GetSimulatorUrl().c_str(), NULL);
  350. SP::Simulator::Restful::FulfillRequestJsonBody(&config, simulatorCommonReq);
  351. RestfulClient client = RestfulClient::getInstance();
  352. config.PreDo();
  353. client.Do(&config, &result);
  354. if (result.ResponseOK()) {
  355. SP::Simulator::Restful::CommSimulatorRes commRes;
  356. SP::Simulator::Restful::GetStatusFromDebranchResponse(result.content, commRes);
  357. err = (ErrorCodeEnum)commRes.errNum;
  358. }
  359. else {
  360. err = Error_NetBroken;
  361. }
  362. return err;
  363. }
  364. template <typename T>
  365. ErrorCodeEnum SimulatorHttpFunction(string entityName, string devFuncName, T& obj)
  366. {
  367. ErrorCodeEnum err = Error_NotImpl;
  368. SimulatorCommonReq simulatorCommonReq;
  369. simulatorCommonReq.ip = GetLocalIP();
  370. simulatorCommonReq.entityName = entityName;
  371. simulatorCommonReq.adapterInterName = devFuncName;
  372. HttpClientResponseResult result;
  373. HttpClientRequestConfig config(HttpRequestMethod::POST, GetSimulatorUrl().c_str(), NULL);
  374. SP::Simulator::Restful::FulfillRequestJsonBody(&config, simulatorCommonReq);
  375. RestfulClient client = RestfulClient::getInstance();
  376. config.PreDo();
  377. client.Do(&config, &result);
  378. if (result.ResponseOK()) {
  379. SP::Simulator::Restful::CommSimulatorRes commRes;
  380. SP::Simulator::Restful::GetStatusFromDebranchResponse(result.content, commRes);
  381. err = (ErrorCodeEnum)commRes.errNum;
  382. if (err == Error_Succeed)
  383. {
  384. bool res = SP::Simulator::Restful::ExtractDataFromDebranchResponse(result.content, obj);
  385. //DEBUG
  386. if (!res)
  387. {
  388. err = Error_Bug;
  389. }
  390. }
  391. }
  392. else {
  393. err = Error_NetBroken;
  394. }
  395. return err;
  396. }