mod_DeviceControl.cpp 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628
  1. // mod_DeviceControl.cpp : Defines the exported functions for the DLL application.
  2. //
  3. #include "stdafx.h"
  4. #include "mod_DeviceControl.h"
  5. #include "DeviceControl_msg_g.h"
  6. #include "toolkit.h"
  7. #include "CommEntityUtil.hpp"
  8. #include <string>
  9. #include <map>
  10. #include <regex.h>
  11. #include "array.h"
  12. #include "fileutil.h"
  13. #include <vector>
  14. #include <sys/utsname.h>
  15. #include "RestfulFunc.h"
  16. #include "DeviceAdaptChecker.h"
  17. #include "EventCode.h"
  18. void CDeviceControlServerSession::Handle_USB(SpReqAnsContext<DeviceControlService_USB_Req, DeviceControlService_USB_Ans>::Pointer ctx)
  19. {
  20. LOG_FUNCTION();
  21. m_pEntity->USB(ctx);
  22. }
  23. void CDeviceControlServerSession::Handle_GetUkeyID(SpReqAnsContext<DeviceControlService_GetUkeyID_Req, DeviceControlService_GetUkeyID_Ans>::Pointer ctx)
  24. {
  25. LOG_FUNCTION();
  26. m_pEntity->GetUkeyID(ctx);
  27. }
  28. void CDeviceControlServerSession::Handle_InitToken(SpReqAnsContext<DeviceControlService_InitToken_Req, DeviceControlService_InitToken_Ans>::Pointer ctx)
  29. {
  30. LOG_FUNCTION();
  31. m_pEntity->InitToken(ctx);
  32. }
  33. void CDeviceControlServerSession::Handle_QueryUSBStatus(SpReqAnsContext<DeviceControlService_QueryUSBStatus_Req, DeviceControlService_QueryUSBStatus_Ans>::Pointer ctx)
  34. {
  35. LOG_FUNCTION();
  36. m_pEntity->QueryUSBStatus(ctx);
  37. }
  38. void CDeviceControlServerSession::Handle_QueryHardwareInfo(SpReqAnsContext<DeviceControlService_QueryHardwareInfo_Req, DeviceControlService_QueryHardwareInfo_Ans>::Pointer ctx)
  39. {
  40. LOG_FUNCTION();
  41. m_pEntity->QueryHardwareInfo(ctx);
  42. }
  43. void CDeviceControlServerSession::Handle_ReadCenterConfigStr(SpReqAnsContext<DeviceControlService_ReadCenterConfigStr_Req, DeviceControlService_ReadCenterConfigStr_Ans>::Pointer ctx)
  44. {
  45. LOG_FUNCTION();
  46. m_pEntity->ReadCenterConfigStr(ctx);
  47. }
  48. void CDeviceControlServerSession::Handle_ReadConfigValue(SpReqAnsContext<DeviceControlService_ReadConfigValue_Req, DeviceControlService_ReadConfigValue_Ans>::Pointer ctx)
  49. {
  50. m_pEntity->ReadConfigValue(ctx);
  51. }
  52. void CDeviceControlServerSession::Handle_GetDevInfo(SpReqAnsContext<DeviceControlService_GetDevInfo_Req, DeviceControlService_GetDevInfo_Ans>::Pointer ctx)
  53. {
  54. LOG_FUNCTION();
  55. m_pEntity->GetDevInfo(ctx);
  56. }
  57. void CDeviceControlServerSession::Handle_TestConnectivity(SpReqAnsContext<DeviceControlService_TestConnectivity_Req, DeviceControlService_TestConnectivity_Ans>::Pointer ctx)
  58. {
  59. LOG_FUNCTION();
  60. m_pEntity->TestConnectivity(ctx);
  61. }
  62. void CDeviceControlServerSession::Handle_GetVendorFileList(SpReqAnsContext<DeviceControlService_GetVendorFileList_Req, DeviceControlService_GetVendorFileList_Ans>::Pointer ctx)
  63. {
  64. LOG_FUNCTION();
  65. m_pEntity->GetVendorFileList(ctx);
  66. }
  67. void CDeviceControlServerSession::Handle_TestAdapterFileAvailable(SpReqAnsContext<DeviceControlService_TestAdapterFileAvailable_Req, DeviceControlService_TestAdapterFileAvailable_Ans>::Pointer ctx)
  68. {
  69. LOG_FUNCTION();
  70. m_pEntity->TestAdapterFileAvailable(ctx);
  71. }
  72. void CDeviceControlEntity::QueryHardwareInfo(SpReqAnsContext<DeviceControlService_QueryHardwareInfo_Req, DeviceControlService_QueryHardwareInfo_Ans>::Pointer ctx)
  73. {
  74. CSystemStaticInfo info;
  75. GetFunction()->GetSystemStaticInfo(info);
  76. CSimpleStringA deviceFct("");
  77. NetworkAddressesList macAddrs;
  78. NetworkAddressesList ipAddrs;
  79. SP::Module::Net::GetINETMacAddresses(macAddrs, ipAddrs);
  80. //根据设备类型获取厂商信息
  81. if (info.strMachineType == "RVC.PAD")
  82. {
  83. DbgWithLink(LOG_LEVEL_DEBUG, LOG_TYPE_SYSTEM)("This is PAD device.");
  84. //国产化目前没有pad设备,后续按需修改
  85. }
  86. else
  87. {
  88. CSmartPointer<IConfigInfo> spConfig;
  89. GetFunction()->OpenConfig(Config_Root, spConfig);
  90. spConfig->ReadConfigValue("Device.PinPad", "Vendor", deviceFct);
  91. }
  92. //获取操作系统版本信息
  93. utsname sysInfo;
  94. uname(&sysInfo);
  95. CSimpleStringA sysVer("UOS ");
  96. sysVer.Append(sysInfo.release);
  97. if (ctx != NULL) {
  98. ctx->Ans.ip = ipAddrs;
  99. ctx->Ans.mac = macAddrs;
  100. ctx->Ans.machineType = info.strMachineType;
  101. ctx->Ans.site = info.strSite;
  102. ctx->Ans.terminalNo = info.strTerminalID;
  103. ctx->Ans.termLimit = __ReadCenterConfigStr("TermLimitSwitch", "IEBrowser").second;
  104. ctx->Ans.termVersion = info.InstallVersion.ToString();
  105. ctx->Ans.reserved3 = sysVer;
  106. ctx->Ans.reserved4 = deviceFct;
  107. ctx->Answer(Error_Succeed);
  108. }
  109. }
  110. void CDeviceControlEntity::ReadCenterConfigStr(SpReqAnsContext<DeviceControlService_ReadCenterConfigStr_Req, DeviceControlService_ReadCenterConfigStr_Ans>::Pointer ctx)
  111. {
  112. auto ret = __ReadCenterConfigStr(ctx->Req.key, ctx->Req.entityName);
  113. ctx->Ans.value = ret.second;
  114. ctx->Ans.reserved1 = ctx->Ans.reserved2 = 0;
  115. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("%s: ReadCenterConfigStr key:%s, entityName:%s: %s",
  116. SpStrError(ret.first),
  117. ctx->Req.key.GetData(), ctx->Req.entityName.GetData(), ctx->Ans.value.GetData());
  118. ctx->Answer(ret.first);
  119. }
  120. void CDeviceControlEntity::ReadConfigValue(SpReqAnsContext<DeviceControlService_ReadConfigValue_Req, DeviceControlService_ReadConfigValue_Ans>::Pointer ctx)
  121. {
  122. LOG_FUNCTION();
  123. if (ctx->Req.configType < 0 || ctx->Req.configType > Config_Cache + 1) {
  124. ctx->Answer(Error_Param);
  125. return;
  126. }
  127. if (ctx->Req.configType == 0) {
  128. if (ctx->Req.section.Compare("SougouInput") == 0) {
  129. if (ctx->Req.key.Compare("Snapshot") == 0) {
  130. CSimpleStringA shellScriptPath;
  131. GetFunction()->GetPath("Base", shellScriptPath);
  132. shellScriptPath += SPLIT_SLASH_STR;
  133. shellScriptPath += "res" SPLIT_SLASH_STR "RunScript" SPLIT_SLASH_STR;
  134. std::string fetch_scripts(shellScriptPath.GetData());
  135. fetch_scripts += "sogouime-info-cmb.sh";
  136. if (!ExistsFileA(fetch_scripts.c_str())) {
  137. ctx->Answer(Error_NotExist);
  138. return;
  139. } else {
  140. do {
  141. char app[MAX_PATH] = { '\0' };
  142. tk_process_t* process = NULL;
  143. tk_process_option_t option;
  144. option.exit_cb = NULL;
  145. option.file = NULL;
  146. option.flags = 0;
  147. sprintf(app, "bash %s", fetch_scripts.c_str());
  148. option.params = app;
  149. const int res = process_spawn(&option, &process);
  150. if (0 == res) {
  151. FREE(process);
  152. LogWarn(Severity_Middle, Error_Debug, LOG_DEVICECONTROL_SOGOU_SCRIPTS_EXECUTE_SUCC,
  153. CSimpleStringA::Format("%s", fetch_scripts.c_str()));
  154. } else {
  155. LogWarn(Severity_Middle, Error_Unexpect, LOG_DEVICECONTROL_SOGOU_SCRIPTS_EXECUTE_FAILED,
  156. CSimpleStringA::Format("%s: %d", fetch_scripts.c_str(), res));
  157. ctx->Answer(Error_NotExist);
  158. return;
  159. }
  160. } while (false);
  161. Sleep(5000);
  162. do {
  163. std::string succStr, errStr;
  164. SYSTEMTIME curTime = CSmallDateTime::GetNow().ToSystemTime();
  165. CSimpleStringA logFileName = CSimpleStringA::Format("/opt/rvc/dbg/sogouinfo/%04d%02d%02d.log", curTime.wYear, curTime.wMonth, curTime.wDay);
  166. std::string runStr("cat ");
  167. if (ExistsFileA(logFileName)) {
  168. runStr += logFileName.GetData();
  169. if (SP::Module::Util::ShellExecute(runStr, succStr, errStr)) {
  170. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("execute {%s} suc: %s", runStr.c_str(), succStr.c_str());
  171. ctx->Ans.reserved3 = succStr.c_str();
  172. }
  173. }
  174. } while (false);
  175. ctx->Answer(Error_Succeed);
  176. return;
  177. }
  178. } else if (ctx->Req.key.Compare("SogouRestart") == 0) {
  179. ctx->Answer(m_fsm.RestartSogouServices());
  180. return;
  181. }
  182. else {
  183. ctx->Answer(Error_NotImpl);
  184. return;
  185. }
  186. } else {
  187. ctx->Answer(Error_NotImpl);
  188. return;
  189. }
  190. }
  191. ConfigTypeEnum type = static_cast<ConfigTypeEnum>(ctx->Req.configType - 1);
  192. CSmartPointer<IConfigInfo> spConfig;
  193. ErrorCodeEnum err = GetFunction()->OpenConfig(type, spConfig);
  194. if (err == Error_Succeed) {
  195. CSimpleStringA str(true);
  196. CSimpleStringA section(ctx->Req.section);
  197. CSimpleStringA key(ctx->Req.key);
  198. #ifndef DEVOPS_ON_PRD
  199. /** 被逼的,方便测试环境验证,并且在只读的情况下 [Gifur@2022127]*/
  200. if (ctx->Req.reserved1 == 1 && !ctx->Req.option) {
  201. #ifdef DEVOPS_ON_ST
  202. section.Append("_ST");
  203. #else defined(DEVOPS_ON_UAT)
  204. section.Append("_UAT");
  205. #endif // DEVOPS_ON_ST
  206. }
  207. #endif
  208. err = spConfig->ReadConfigValue(section, key, str);
  209. if (err == Error_Succeed) ctx->Ans.value = str;
  210. if (ctx->Req.option && ctx->Req.reserved3.Compare(str) != 0) {
  211. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("save value: %s", ctx->Req.reserved3.GetData());
  212. err = spConfig->WriteConfigValue(section, key, ctx->Req.reserved3);
  213. } else if (ctx->Req.option) {
  214. DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM)("stored value is the same as the coming one: %s", str.GetData());
  215. }
  216. }
  217. ctx->Answer(err);
  218. return;
  219. }
  220. void CDeviceControlEntity::TestConnectivity(SpReqAnsContext<DeviceControlService_TestConnectivity_Req, DeviceControlService_TestConnectivity_Ans>::Pointer ctx)
  221. {
  222. LOG_FUNCTION();
  223. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("%d: %s:%d", ctx->Req.protocol, ctx->Req.ip.GetData(), ctx->Req.port);
  224. ErrorCodeEnum result(Error_NotInit);
  225. if (ctx->Req.protocol == 0) {
  226. result = Error_Param;
  227. } else if (ctx->Req.protocol == 1) { //Ping
  228. if (ctx->Req.ip.IsNullOrEmpty()) {
  229. result = Error_Param;
  230. } else {
  231. const int res = PingTest(std::string(ctx->Req.ip));
  232. if (res == 0) {
  233. ctx->Ans.result = 0;
  234. ctx->Ans.additionalMsg.Clear();
  235. } else {
  236. ctx->Ans.result = -1;
  237. ctx->Ans.additionalMsg = CSimpleStringA::Format("Ping %s failed: %d", ctx->Req.ip.GetData(), res);
  238. }
  239. result = Error_Succeed;
  240. }
  241. } else if (ctx->Req.protocol == 2) { //Normal socket connect Test
  242. if (ctx->Req.ip.IsNullOrEmpty() || ctx->Req.port <= 0) {
  243. result = Error_Param;
  244. } else {
  245. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("Test socket connect available: %s:%d", ctx->Req.ip.GetData(), ctx->Req.port);
  246. std::string errMsg("");
  247. const int ret = SocketConnectTest(ctx->Req.ip.GetData(), ctx->Req.port, errMsg);
  248. ctx->Ans.result = ret;
  249. ctx->Ans.additionalMsg = errMsg.c_str();
  250. result = Error_Succeed;
  251. }
  252. }
  253. else if (ctx->Req.protocol == 4) { //Http Test
  254. if (ctx->Req.ip.IsNullOrEmpty()) {
  255. result = Error_Param;
  256. } else {
  257. std::string msg;
  258. const int res = HttpProbe(std::string(ctx->Req.ip), msg);
  259. ctx->Ans.result = res;
  260. ctx->Ans.additionalMsg = msg.c_str();
  261. result = Error_Succeed;
  262. }
  263. } else {
  264. result = Error_NotSupport;
  265. }
  266. ctx->Answer(result);
  267. }
  268. static CSimpleStringA GetRegexPatternString(const DeviceControlService_GetVendorFileList_Req& req, const CSimpleStringA& strMachineType)
  269. {
  270. const char* prefix = "^lib";
  271. const char* suffix = "\\.so$";
  272. CSimpleStringA strRegexPattern(prefix);
  273. //^lib(Gpio|PinPad)\.[a-zA-Z]*\.[0-9]*\.[0-9]*\.so$
  274. if (req.adapter.IsNullOrEmpty() || req.adapter.Compare("*") == 0) {
  275. CSimpleStringA tmp("(");
  276. if (strMachineType.Compare("RVC.Stand2S") == 0) {
  277. for (int i = 0; i < array_size(adapterList_4_stand2s); ++i) {
  278. if (i != 0) tmp += "|";
  279. tmp += adapterList_4_stand2s[i];
  280. }
  281. } else if (strMachineType.Compare("RVC.Stand1SPlus") == 0) {
  282. for (int i = 0; i < array_size(adapterList_4_stand1s); ++i) {
  283. if (i != 0) tmp += "|";
  284. tmp += adapterList_4_stand1s[i];
  285. }
  286. } else {
  287. tmp += ".*";
  288. }
  289. tmp += (")");
  290. strRegexPattern += tmp;
  291. } else {
  292. strRegexPattern += "(";
  293. strRegexPattern += req.adapter;
  294. strRegexPattern += ")";
  295. }
  296. strRegexPattern += "\\.";
  297. if (req.vendor.IsNullOrEmpty() || req.vendor.Compare("*") == 0) {
  298. strRegexPattern += "[a-zA-Z]*";
  299. } else {
  300. strRegexPattern += "(";
  301. strRegexPattern += req.vendor;
  302. strRegexPattern += ")";
  303. }
  304. strRegexPattern += "\\.";
  305. if (req.version.IsNullOrEmpty() || req.version.Compare("*") == 0) {
  306. strRegexPattern += "[0-9]*";
  307. } else {
  308. strRegexPattern += "(";
  309. strRegexPattern += req.version;
  310. strRegexPattern += ")";
  311. }
  312. strRegexPattern += "\\.";
  313. if (req.batch.IsNullOrEmpty() || req.batch.Compare("*") == 0) {
  314. strRegexPattern += "[0-9]*";
  315. } else {
  316. strRegexPattern += "(";
  317. strRegexPattern += req.batch;
  318. strRegexPattern += ")";
  319. }
  320. strRegexPattern += suffix;
  321. return strRegexPattern;
  322. }
  323. void CDeviceControlEntity::GetVendorFileList(SpReqAnsContext<DeviceControlService_GetVendorFileList_Req, DeviceControlService_GetVendorFileList_Ans>::Pointer ctx)
  324. {
  325. LOG_FUNCTION();
  326. ErrorCodeEnum result(Error_Succeed);
  327. int tmpResult = 0;
  328. CSimpleStringA tmpMsg(true);
  329. CSimpleStringA strDepDirPath;
  330. result = GetFunction()->GetPath("Dep", strDepDirPath);
  331. if (ctx->Req.root) {
  332. CAdapterLoadChecker* checker = new CAdapterLoadChecker();
  333. checker->Init(this);
  334. CAutoArray<AdapterRootConfigInfo> tmp;
  335. if (ctx->Req.adapter.IsNullOrEmpty() || ctx->Req.adapter.Compare("*") == 0) {
  336. tmp = checker->GetConfigedAdapterInfo();
  337. } else {
  338. tmp = checker->GetConfigedAdapterInfo(ctx->Req.adapter);
  339. }
  340. delete checker;
  341. checker = NULL;
  342. const int curSize = tmp.GetCount();
  343. ctx->Ans.filenames.Init(curSize);
  344. ctx->Ans.status.Init(curSize);
  345. ctx->Ans.port.Init(curSize);
  346. ctx->Ans.baudrate.Init(curSize);
  347. CSimpleStringA strDepDirPath2 = strDepDirPath + SPLIT_SLASH_STR;
  348. for (int i = 0; i < curSize; ++i) {
  349. ctx->Ans.filenames[i] = tmp[i].strDepDirWithSlashOrFileName;
  350. ctx->Ans.port[i] = tmp[i].dwPort;
  351. ctx->Ans.baudrate[i] = tmp[i].dwBaudRate;
  352. CSimpleStringA fullPath = strDepDirPath2 + ctx->Ans.filenames[i];
  353. ctx->Ans.status[i] = 0x2;
  354. if (ExistsFileA(fullPath)) {
  355. ctx->Ans.status[i] |= 0x1;
  356. }
  357. }
  358. } else {
  359. std::vector<std::string> adapterFiles;
  360. array_header_t* subs = fileutil_get_sub_files_a(strDepDirPath);
  361. if (subs) {
  362. regex_t reg;
  363. CSystemStaticInfo info;
  364. GetFunction()->GetSystemStaticInfo(info);
  365. CSimpleStringA pattern = GetRegexPatternString(ctx->Req, info.strMachineType);
  366. int ret = regcomp(&reg, pattern, REG_EXTENDED | REG_NOSUB);
  367. if (ret) {
  368. char ebuff[256];
  369. regerror(ret, &reg, ebuff, 256);
  370. DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM)("regex failed: %s", ebuff);
  371. tmpResult = Error_Unexpect;
  372. tmpMsg = CSimpleStringA::Format("内部错误:REGEX %s", ebuff);
  373. } else {
  374. DbgWithLink(LOG_LEVEL_DEBUG, LOG_TYPE_SYSTEM)("pattern: %s", pattern.GetData());
  375. for (int i = 0; i < subs->nelts; ++i) {
  376. char* filenamePath = ARRAY_IDX(subs, i, char*);
  377. char* filename = &filenamePath[strDepDirPath.GetLength() + 1];
  378. ret = regexec(&reg, filename, 0, NULL, 0);
  379. if (0 == ret) {
  380. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("filename %s matched!", filename);
  381. adapterFiles.push_back(std::string(filename));
  382. }
  383. }
  384. }
  385. const int curSize = adapterFiles.size();
  386. ctx->Ans.filenames.Init(curSize);
  387. ctx->Ans.status.Init(curSize);
  388. ctx->Ans.port.Init(curSize);
  389. ctx->Ans.baudrate.Init(curSize);
  390. for (int i = 0; i < curSize; ++i) {
  391. ctx->Ans.status[i] = 0;
  392. ctx->Ans.port[i] = 0;
  393. ctx->Ans.baudrate[i] = 0;
  394. }
  395. if (curSize > 0) {
  396. CAdapterLoadChecker* checker = new CAdapterLoadChecker();
  397. checker->Init(this);
  398. int cnt = 0;
  399. for (auto iter = adapterFiles.cbegin(); iter != adapterFiles.cend(); ++iter, ++cnt) {
  400. ctx->Ans.filenames[cnt] = iter->c_str();
  401. ctx->Ans.status[cnt] = 0x1;
  402. AdapterRootConfigInfo info;
  403. if (checker->IsThisFileConfigure(iter->c_str(), &info)) {
  404. ctx->Ans.status[cnt] |= 0x2;
  405. ctx->Ans.port[cnt] = info.dwPort;
  406. ctx->Ans.baudrate[cnt] = info.dwBaudRate;
  407. }
  408. }
  409. delete checker;
  410. }
  411. toolkit_array_free2(subs);
  412. }
  413. }
  414. ctx->Ans.result = tmpResult;
  415. ctx->Ans.msg = tmpMsg;
  416. ctx->Answer(result);
  417. return;
  418. }
  419. void CDeviceControlEntity::TestAdapterFileAvailable(
  420. SpReqAnsContext<DeviceControlService_TestAdapterFileAvailable_Req, DeviceControlService_TestAdapterFileAvailable_Ans>::Pointer ctx)
  421. {
  422. LOG_FUNCTION();
  423. ErrorCodeEnum result(Error_Succeed);
  424. int tmpResult = 0;
  425. CSimpleStringA tmpMsg(true);
  426. if (ctx->Req.type == 1) /*循环验证配置适配器的有效性*/ {
  427. CAdapterLoadChecker* checker = new CAdapterLoadChecker();
  428. checker->Init(this);
  429. CAutoArray<CAdapterLoader*>& tmp = checker->AdapterCoinfigList();
  430. const int curSize = tmp.GetCount();
  431. std::vector<std::string> errlibNames;
  432. std::vector<std::string> errMsgs;
  433. for (int i = 0; i < curSize; ++i) {
  434. ErrorCodeEnum ec(Error_Succeed);
  435. CSimpleStringA strLastErrMsg(true);
  436. VendorLibInfoEx& libInfo = tmp[i]->vendorLibInfo;
  437. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("to check %s", libInfo.toLibNameString().GetData());
  438. ec = CAdapterLoadChecker::CheckDeviceAndAdapterAvailable(libInfo, strLastErrMsg);
  439. if (ec != Error_Succeed) {
  440. errlibNames.push_back(std::string(libInfo.toLibNameString().GetData()));
  441. errMsgs.push_back(std::string(strLastErrMsg.GetData()));
  442. strLastErrMsg = CSimpleStringA::Format("%s: %s", libInfo.toLibNameString().GetData(), strLastErrMsg.GetData());
  443. if (!tmpMsg.IsNullOrEmpty()) tmpMsg += "\n";
  444. tmpMsg += strLastErrMsg;
  445. if (!!(ctx->Req.options & 0x1)) {
  446. DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM)("detect critical error limit, break it.");
  447. tmpResult = ec;
  448. break;
  449. }
  450. }
  451. BroadcastAdapterTestResult(libInfo.toLibNameString(), ec, strLastErrMsg);
  452. }
  453. delete checker;
  454. checker = NULL;
  455. if (!(ctx->Req.options & 0x1) && !tmpMsg.IsNullOrEmpty()) {
  456. tmpResult = Error_Failed;
  457. }
  458. if (!errlibNames.empty()) {
  459. const int curErrSize = errlibNames.size();
  460. ctx->Ans.reserved1.Init(curErrSize);
  461. ctx->Ans.reserved2.Init(curErrSize);
  462. ctx->Ans.reserved3.Init(curErrSize);
  463. int cnt(0);
  464. for (auto iter = errlibNames.cbegin(); iter != errlibNames.cend(); ++iter) {
  465. ctx->Ans.reserved1[cnt] = 0;
  466. ctx->Ans.reserved2[cnt] = iter->c_str();
  467. ctx->Ans.reserved3[cnt] = errMsgs[cnt].c_str();
  468. cnt++;
  469. }
  470. }
  471. } else {
  472. if (ctx->Req.filename.IsNullOrEmpty()) {
  473. ctx->Answer(Error_Param);
  474. return;
  475. }
  476. VendorLibInfoEx libInfo;
  477. if (libInfo.Load(ctx->Req.filename)) {
  478. libInfo.optCfg.dwPort = ctx->Req.port;
  479. libInfo.optCfg.dwBaudRate = ctx->Req.baudrate;
  480. libInfo.optCfg.dwParam1 = ctx->Req.param1;
  481. libInfo.optCfg.dwParam2 = ctx->Req.param2;
  482. GetFunction()->GetPath("Dep", libInfo.optCfg.strDepDirWithSlashOrFileName);
  483. libInfo.optCfg.strDepDirWithSlashOrFileName += SPLIT_SLASH_STR;
  484. } else if (ctx->Req.filename.IndexOf(".") == -1) { //pure adapter name
  485. CAdapterLoader loader(ctx->Req.filename, this);
  486. tmpResult = loader.LoadConfigFromRootIni();
  487. libInfo = loader.vendorLibInfo;
  488. } else {
  489. DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM)("invalid adapter file name: %s", ctx->Req.filename.GetData());
  490. ctx->Answer(Error_MisMatched);
  491. return;
  492. }
  493. if (tmpResult == 0) {
  494. tmpResult = CAdapterLoadChecker::CheckDeviceAndAdapterAvailable(libInfo, tmpMsg);
  495. }
  496. }
  497. ctx->Ans.status = tmpResult;
  498. ctx->Ans.msg = tmpMsg;
  499. ctx->Answer(result);
  500. return;
  501. }
  502. std::pair<ErrorCodeEnum, CSimpleStringA> CDeviceControlEntity::__ReadCenterConfigStr(CSimpleStringA key, CSimpleStringA entityName = "")
  503. {
  504. CSimpleStringA str = "";
  505. CSmartPointer<IConfigInfo> spCerConfig;
  506. ErrorCodeEnum err = GetFunction()->OpenConfig(Config_CenterSetting, spCerConfig);
  507. if (err != Error_Succeed)
  508. DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM)("OpenConfig failed: %s", SpStrError(err));
  509. else
  510. err = spCerConfig->ReadConfigValue(
  511. entityName == CSimpleStringA("") ? "IEBrowser" : (const char*)entityName.GetData()
  512. , (const char*)key.GetData(), str);
  513. if (str.IsNullOrEmpty() && key.Compare("CenterConfigUrl") == 0 && entityName.Compare("CenterSetting") == 0) {
  514. DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM)("Try to read CenterSettings download url from shell.ini again");
  515. CSmartPointer<IConfigInfo> pConfigShell;
  516. GetFunction()->OpenConfig(Config_Shell, pConfigShell);
  517. CSimpleStringA section("CenterSettingsDownloadUrl");
  518. pConfigShell->ReadConfigValue(section, "LAN", str);
  519. if (Error_Succeed != err && !str.IsNullOrEmpty()) {
  520. err = Error_Succeed;
  521. }
  522. }
  523. return std::make_pair(err, str);
  524. }
  525. void CDeviceControlEntity::BroadcastAdapterTestResult(const CSimpleStringA& adapterName, ErrorCodeEnum result, const CSimpleStringA& strErrMsg)
  526. {
  527. LOG_FUNCTION();
  528. DeviceControl::AdapterTestResult evt;
  529. evt.result = result;
  530. evt.adapterInfo = adapterName;
  531. evt.errMsg = strErrMsg;
  532. SpSendBroadcast(GetFunction(), SP_MSG_OF(AdapterTestResult), SP_MSG_SIG_OF(AdapterTestResult), evt);
  533. }
  534. SP_BEGIN_ENTITY_MAP()
  535. SP_ENTITY(CDeviceControlEntity)
  536. SP_END_ENTITY_MAP()