mod_DeviceControl.cpp 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735
  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 "array.h"
  11. #include "fileutil.h"
  12. #include <vector>
  13. #include <iostream>
  14. #include <memory>
  15. #include "EventCode.h"
  16. #if defined(RVC_OS_WIN)
  17. #include<windows.h>
  18. #else
  19. #include <regex.h>
  20. #include <sys/utsname.h>
  21. #include "RestfulFunc.h"
  22. #define NOT_SUPPOT_X11
  23. #ifndef NOT_SUPPOT_X11
  24. #include <X11/Xlib.h>
  25. #include <X11/Xutil.h>
  26. #endif //NOT_SUPPOT_X11
  27. #include "DeviceAdaptChecker.h"
  28. #endif //RVC_OS_WIN
  29. #define DeviceControl_MouseMove 0x21400301 //鼠标移动
  30. void CDeviceControlServerSession::Handle_USB(SpReqAnsContext<DeviceControlService_USB_Req, DeviceControlService_USB_Ans>::Pointer ctx)
  31. {
  32. DbgToBeidou(ctx->link, __FUNCTION__)();
  33. m_pEntity->USB(ctx);
  34. }
  35. void CDeviceControlServerSession::Handle_GetUkeyID(SpReqAnsContext<DeviceControlService_GetUkeyID_Req, DeviceControlService_GetUkeyID_Ans>::Pointer ctx)
  36. {
  37. DbgToBeidou(ctx->link, __FUNCTION__)();
  38. m_pEntity->GetUkeyID(ctx);
  39. }
  40. void CDeviceControlServerSession::Handle_InitToken(SpReqAnsContext<DeviceControlService_InitToken_Req, DeviceControlService_InitToken_Ans>::Pointer ctx)
  41. {
  42. DbgToBeidou(ctx->link, __FUNCTION__)();
  43. m_pEntity->InitToken(ctx);
  44. }
  45. void CDeviceControlServerSession::Handle_QueryUSBStatus(SpReqAnsContext<DeviceControlService_QueryUSBStatus_Req, DeviceControlService_QueryUSBStatus_Ans>::Pointer ctx)
  46. {
  47. DbgToBeidou(ctx->link, __FUNCTION__)();
  48. m_pEntity->QueryUSBStatus(ctx);
  49. }
  50. void CDeviceControlServerSession::Handle_QueryHardwareInfo(SpReqAnsContext<DeviceControlService_QueryHardwareInfo_Req, DeviceControlService_QueryHardwareInfo_Ans>::Pointer ctx)
  51. {
  52. DbgToBeidou(ctx->link, __FUNCTION__)();
  53. m_pEntity->QueryHardwareInfo(ctx);
  54. }
  55. void CDeviceControlServerSession::Handle_ReadCenterConfigStr(SpReqAnsContext<DeviceControlService_ReadCenterConfigStr_Req, DeviceControlService_ReadCenterConfigStr_Ans>::Pointer ctx)
  56. {
  57. DbgToBeidou(ctx->link, __FUNCTION__)();
  58. m_pEntity->ReadCenterConfigStr(ctx);
  59. }
  60. void CDeviceControlServerSession::Handle_ReadConfigValue(SpReqAnsContext<DeviceControlService_ReadConfigValue_Req, DeviceControlService_ReadConfigValue_Ans>::Pointer ctx)
  61. {
  62. DbgToBeidou(ctx->link, __FUNCTION__)();
  63. m_pEntity->ReadConfigValue(ctx);
  64. }
  65. void CDeviceControlServerSession::Handle_GetDevInfo(SpReqAnsContext<DeviceControlService_GetDevInfo_Req, DeviceControlService_GetDevInfo_Ans>::Pointer ctx)
  66. {
  67. DbgToBeidou(ctx->link, __FUNCTION__)();
  68. m_pEntity->GetDevInfo(ctx);
  69. }
  70. void CDeviceControlServerSession::Handle_TestConnectivity(SpReqAnsContext<DeviceControlService_TestConnectivity_Req, DeviceControlService_TestConnectivity_Ans>::Pointer ctx)
  71. {
  72. DbgToBeidou(ctx->link, __FUNCTION__)();
  73. m_pEntity->TestConnectivity(ctx);
  74. }
  75. void CDeviceControlServerSession::Handle_GetVendorFileList(SpReqAnsContext<DeviceControlService_GetVendorFileList_Req, DeviceControlService_GetVendorFileList_Ans>::Pointer ctx)
  76. {
  77. DbgToBeidou(ctx->link, __FUNCTION__)();
  78. m_pEntity->GetVendorFileList(ctx);
  79. }
  80. void CDeviceControlServerSession::Handle_TestAdapterFileAvailable(SpReqAnsContext<DeviceControlService_TestAdapterFileAvailable_Req, DeviceControlService_TestAdapterFileAvailable_Ans>::Pointer ctx)
  81. {
  82. DbgToBeidou(ctx->link, __FUNCTION__)();
  83. m_pEntity->TestAdapterFileAvailable(ctx);
  84. }
  85. void CDeviceControlEntity::QueryHardwareInfo(SpReqAnsContext<DeviceControlService_QueryHardwareInfo_Req, DeviceControlService_QueryHardwareInfo_Ans>::Pointer ctx)
  86. {
  87. CSystemStaticInfo info;
  88. GetFunction()->GetSystemStaticInfo(info);
  89. CSimpleStringA deviceFct("");
  90. CAutoArray<SP::Module::Net::NetworkAdapterItem> netList;
  91. SP::Module::Net::GetINETMacAddresses(netList);
  92. CAutoArray<CSimpleStringA> ipAddrs, macAddrs;
  93. for (int i = 0; i < netList.GetCount(); i++) {
  94. CSimpleStringA tmpip = netList[i].ip.c_str();
  95. CSimpleStringA tmpmac = netList[i].mac.c_str();
  96. ipAddrs.Append(&tmpip, 0, 1);
  97. macAddrs.Append(&tmpmac, 0, 1);
  98. }
  99. CSmartPointer<IConfigInfo> spConfig;
  100. GetFunction()->OpenConfig(Config_Root, spConfig);
  101. spConfig->ReadConfigValue("Device.PinPad", "Vendor", deviceFct);
  102. //获取操作系统版本信息
  103. #if defined(_MSC_VER)
  104. //获取windows系统版本
  105. DWORD dwMajorVer, dwMinorVer, dwBuildNumber;
  106. BOOL bRet = FALSE;
  107. HMODULE hModNtdll = NULL;
  108. if (hModNtdll = ::LoadLibraryW(L"ntdll.dll")) {
  109. typedef void (WINAPI* pfRTLGETNTVERSIONNUMBERS)(DWORD*, DWORD*, DWORD*);
  110. pfRTLGETNTVERSIONNUMBERS pfRtlGetNtVersionNumbers;
  111. pfRtlGetNtVersionNumbers = (pfRTLGETNTVERSIONNUMBERS)::GetProcAddress(hModNtdll, "RtlGetNtVersionNumbers");
  112. if (pfRtlGetNtVersionNumbers) {
  113. pfRtlGetNtVersionNumbers(&dwMajorVer, &dwMinorVer, &dwBuildNumber);
  114. dwBuildNumber &= 0x0ffff;
  115. bRet = TRUE;
  116. }
  117. ::FreeLibrary(hModNtdll);
  118. hModNtdll = NULL;
  119. }
  120. char buffer[1024];
  121. sprintf(buffer, "%d.%d.%d", dwMajorVer, dwMinorVer, dwBuildNumber);
  122. CSimpleStringA sysVer = (string("Windows ver[") + string(buffer) + "]").c_str();
  123. #else
  124. utsname sysInfo;
  125. uname(&sysInfo);
  126. CSimpleStringA sysVer("UOS ");
  127. sysVer.Append(sysInfo.release);
  128. #endif //_MSC_VER
  129. if (ctx != NULL) {
  130. ctx->Ans.ip = ipAddrs;
  131. ctx->Ans.mac = macAddrs;
  132. ctx->Ans.machineType = info.strMachineType;
  133. ctx->Ans.site = info.strSite;
  134. ctx->Ans.terminalNo = info.strTerminalID;
  135. ctx->Ans.termLimit = "";
  136. ctx->Ans.termVersion = info.InstallVersion.ToString();
  137. #ifdef DEVOPS_ON_ST /*DevOps流水线编译,ST环境*/
  138. ctx->Ans.reserved1 = 1;
  139. #elif defined(DEVOPS_ON_UAT)/*DevOps流水线编译,UAT环境*/
  140. ctx->Ans.reserved1 = 2;
  141. #elif defined(DEVOPS_ON_PRD)/*DevOps流水线编译,PRD环境*/
  142. ctx->Ans.reserved1 = 3;
  143. #elif defined(DEVOPS_ON_DEV)/*DevOps流水线编译,Dev环境*/
  144. ctx->Ans.reserved1 = 0;
  145. #else/*本地编译等非DevOps环境编译的版本*/
  146. ctx->Ans.reserved1 = 0;
  147. #endif
  148. ctx->Ans.reserved3 = sysVer;
  149. ctx->Ans.reserved4 = deviceFct;
  150. ctx->Answer(Error_Succeed);
  151. }
  152. }
  153. bool get_current_cursorPos(int& x, int& y)
  154. {
  155. #if (defined _WIN32 || defined _WIN64)
  156. POINT p;
  157. if (GetCursorPos(&p)) {
  158. x = p.x;
  159. y = p.y;
  160. return true;
  161. }
  162. #else
  163. #ifndef NOT_SUPPOT_X11
  164. std::shared_ptr<Display> display(XOpenDisplay(NULL), [](Display* p) {
  165. XCloseDisplay(p);
  166. });
  167. XEvent event;
  168. if (XQueryPointer(display.get(), XDefaultRootWindow(display), &event.xbutton.root, &event.xbutton.window, &event.xbutton.x_root, &event.xbutton.y_root,
  169. &event.xbutton.x, &event.xbutton.y, &event.xbutton.state)) {
  170. x = event.xbutton.x_root;
  171. y = event.xbutton.y_root;
  172. return true;
  173. }
  174. #endif // !NOT_SUPPOT_X11
  175. #endif
  176. return false;
  177. }
  178. DWORD cursorPosUpdate(void* param)
  179. {
  180. int curX = 0, curY = 0;
  181. while (true) {
  182. int tmpX = 0, tmpY = 0;
  183. get_current_cursorPos(tmpX, tmpY);
  184. if (tmpX != curX || tmpY != curY) {
  185. LogEvent(Severity_Low, DeviceControl_MouseMove,
  186. CSimpleString::Format("cursor move from (%d, %d) to (%d, %d)", curX, curY, tmpX, tmpY));
  187. curX = tmpX;
  188. curY = tmpY;
  189. }
  190. Sleep(20000);
  191. }
  192. return 0;
  193. }
  194. void CDeviceControlEntity::ReadCenterConfigStr(SpReqAnsContext<DeviceControlService_ReadCenterConfigStr_Req, DeviceControlService_ReadCenterConfigStr_Ans>::Pointer ctx)
  195. {
  196. auto ret = __ReadCenterConfigStr(ctx->Req.key, ctx->Req.entityName);
  197. ctx->Ans.value = ret.second;
  198. ctx->Ans.reserved1 = ctx->Ans.reserved2 = 0;
  199. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("%s: ReadCenterConfigStr key:%s, entityName:%s: %s",
  200. SpStrError(ret.first),
  201. ctx->Req.key.GetData(), ctx->Req.entityName.GetData(), ctx->Ans.value.GetData());
  202. ctx->Answer(ret.first);
  203. }
  204. void CDeviceControlEntity::ReadConfigValue(SpReqAnsContext<DeviceControlService_ReadConfigValue_Req, DeviceControlService_ReadConfigValue_Ans>::Pointer ctx)
  205. {
  206. if (ctx->Req.configType < 0 || ctx->Req.configType > Config_Cache + 1) {
  207. ctx->Answer(Error_Param);
  208. return;
  209. }
  210. if (ctx->Req.configType == 0) {
  211. if (ctx->Req.section.Compare("SougouInput") == 0) {
  212. if (ctx->Req.key.Compare("Snapshot") == 0) {
  213. CSimpleStringA shellScriptPath;
  214. GetFunction()->GetPath("Base", shellScriptPath);
  215. shellScriptPath += SPLIT_SLASH_STR;
  216. shellScriptPath += "res" SPLIT_SLASH_STR "RunScript" SPLIT_SLASH_STR;
  217. std::string fetch_scripts(shellScriptPath.GetData());
  218. fetch_scripts += "sogouime-info-cmb.sh";
  219. if (!ExistsFileA(fetch_scripts.c_str())) {
  220. ctx->Answer(Error_NotExist);
  221. return;
  222. } else {
  223. do {
  224. char app[MAX_PATH] = { '\0' };
  225. tk_process_t* process = NULL;
  226. tk_process_option_t option;
  227. option.exit_cb = NULL;
  228. option.file = NULL;
  229. option.flags = 0;
  230. sprintf(app, "bash %s", fetch_scripts.c_str());
  231. option.params = app;
  232. const int res = process_spawn(&option, &process);
  233. if (0 == res) {
  234. FREE(process);
  235. LogWarn(Severity_Middle, Error_Debug, LOG_DEVICECONTROL_SOGOU_SCRIPTS_EXECUTE_SUCC,
  236. CSimpleStringA::Format("%s", fetch_scripts.c_str()));
  237. } else {
  238. LogWarn(Severity_Middle, Error_Unexpect, LOG_DEVICECONTROL_SOGOU_SCRIPTS_EXECUTE_FAILED,
  239. CSimpleStringA::Format("%s: %d", fetch_scripts.c_str(), res));
  240. ctx->Answer(Error_NotExist);
  241. return;
  242. }
  243. } while (false);
  244. Sleep(5000);
  245. do {
  246. std::string succStr, errStr;
  247. SYSTEMTIME curTime = CSmallDateTime::GetNow().ToSystemTime();
  248. CSimpleStringA logFileName = CSimpleStringA::Format("/opt/rvc/dbg/sogouinfo/%04d%02d%02d.log", curTime.wYear, curTime.wMonth, curTime.wDay);
  249. std::string runStr("cat ");
  250. if (ExistsFileA(logFileName)) {
  251. runStr += logFileName.GetData();
  252. if (SP::Module::Util::ShellExecute(runStr, succStr, errStr)) {
  253. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("execute {%s} suc: %s", runStr.c_str(), succStr.c_str());
  254. ctx->Ans.reserved3 = succStr.c_str();
  255. }
  256. }
  257. } while (false);
  258. ctx->Answer(Error_Succeed);
  259. return;
  260. }
  261. } else if (ctx->Req.key.Compare("SogouRestart") == 0) {
  262. ctx->Answer(m_fsm.RestartSogouServices());
  263. return;
  264. }
  265. else {
  266. ctx->Answer(Error_NotImpl);
  267. return;
  268. }
  269. } else {
  270. ctx->Answer(Error_NotImpl);
  271. return;
  272. }
  273. }
  274. ConfigTypeEnum type = static_cast<ConfigTypeEnum>(ctx->Req.configType - 1);
  275. CSmartPointer<IConfigInfo> spConfig;
  276. ErrorCodeEnum err = GetFunction()->OpenConfig(type, spConfig);
  277. if (err == Error_Succeed) {
  278. CSimpleStringA str(true);
  279. CSimpleStringA section(ctx->Req.section);
  280. CSimpleStringA key(ctx->Req.key);
  281. #ifndef DEVOPS_ON_PRD
  282. /** 被逼的,方便测试环境验证,并且在只读的情况下 [Gifur@2022127]*/
  283. if (ctx->Req.reserved1 == 1 && !ctx->Req.option) {
  284. #ifdef DEVOPS_ON_ST
  285. section.Append("_ST");
  286. #else defined(DEVOPS_ON_UAT)
  287. section.Append("_UAT");
  288. #endif // DEVOPS_ON_ST
  289. }
  290. #endif
  291. err = spConfig->ReadConfigValue(section, key, str);
  292. if (err == Error_Succeed) ctx->Ans.value = str;
  293. if (ctx->Req.option && ctx->Req.reserved3.Compare(str) != 0) {
  294. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("save value: %s", ctx->Req.reserved3.GetData());
  295. err = spConfig->WriteConfigValue(section, key, ctx->Req.reserved3);
  296. } else if (ctx->Req.option) {
  297. DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM)("stored value is the same as the coming one: %s", str.GetData());
  298. }
  299. }
  300. ctx->Answer(err);
  301. return;
  302. }
  303. void CDeviceControlEntity::TestConnectivity(SpReqAnsContext<DeviceControlService_TestConnectivity_Req, DeviceControlService_TestConnectivity_Ans>::Pointer ctx)
  304. {
  305. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("%d: %s:%d", ctx->Req.protocol, ctx->Req.ip.GetData(), ctx->Req.port);
  306. #if defined(_MSC_VER)
  307. ctx->Answer(Error_NotImpl);
  308. #else
  309. ErrorCodeEnum result(Error_NotInit);
  310. if (ctx->Req.protocol == 0) {
  311. result = Error_Param;
  312. } else if (ctx->Req.protocol == 1) { //Ping
  313. if (ctx->Req.ip.IsNullOrEmpty()) {
  314. result = Error_Param;
  315. } else {
  316. const int res = PingTest(std::string(ctx->Req.ip));
  317. if (res == 0) {
  318. ctx->Ans.result = 0;
  319. ctx->Ans.additionalMsg.Clear();
  320. } else {
  321. ctx->Ans.result = -1;
  322. ctx->Ans.additionalMsg = CSimpleStringA::Format("Ping %s failed: %d", ctx->Req.ip.GetData(), res);
  323. }
  324. result = Error_Succeed;
  325. }
  326. } else if (ctx->Req.protocol == 2) { //Normal socket connect Test
  327. if (ctx->Req.ip.IsNullOrEmpty() || ctx->Req.port <= 0) {
  328. result = Error_Param;
  329. } else {
  330. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("Test socket connect available: %s:%d", ctx->Req.ip.GetData(), ctx->Req.port);
  331. std::string errMsg("");
  332. const int ret = SocketConnectTest(ctx->Req.ip.GetData(), ctx->Req.port, errMsg);
  333. ctx->Ans.result = ret;
  334. ctx->Ans.additionalMsg = errMsg.c_str();
  335. result = Error_Succeed;
  336. }
  337. } else if (ctx->Req.protocol == 4) { //Http Test
  338. if (ctx->Req.ip.IsNullOrEmpty()) {
  339. result = Error_Param;
  340. } else {
  341. std::string msg;
  342. const int res = HttpProbe(std::string(ctx->Req.ip), msg);
  343. ctx->Ans.result = res;
  344. ctx->Ans.additionalMsg = msg.c_str();
  345. result = Error_Succeed;
  346. }
  347. } else {
  348. result = Error_NotSupport;
  349. }
  350. ctx->Answer(result);
  351. #endif //_MSC_VER
  352. }
  353. static CSimpleStringA GetRegexPatternString(const DeviceControlService_GetVendorFileList_Req& req, const CSimpleStringA& strMachineType)
  354. {
  355. const char* prefix = "^lib";
  356. const char* suffix = "\\.so$";
  357. CSimpleStringA strRegexPattern(prefix);
  358. #if defined(RVC_OS_LINUX)
  359. //^lib(Gpio|PinPad)\.[a-zA-Z]*\.[0-9]*\.[0-9]*\.so$
  360. if (req.adapter.IsNullOrEmpty() || req.adapter.Compare("*") == 0) {
  361. CSimpleStringA tmp("(");
  362. if (strMachineType.Compare("RVC.Stand2S") == 0) {
  363. for (int i = 0; i < array_size(adapterList_4_stand2s); ++i) {
  364. if (i != 0) tmp += "|";
  365. tmp += adapterList_4_stand2s[i];
  366. }
  367. } else if (strMachineType.Compare("RVC.Stand1SPlus") == 0) {
  368. for (int i = 0; i < array_size(adapterList_4_stand1s); ++i) {
  369. if (i != 0) tmp += "|";
  370. tmp += adapterList_4_stand1s[i];
  371. }
  372. } else {
  373. tmp += ".*";
  374. }
  375. tmp += (")");
  376. strRegexPattern += tmp;
  377. } else {
  378. strRegexPattern += "(";
  379. strRegexPattern += req.adapter;
  380. strRegexPattern += ")";
  381. }
  382. strRegexPattern += "\\.";
  383. if (req.vendor.IsNullOrEmpty() || req.vendor.Compare("*") == 0) {
  384. strRegexPattern += "[a-zA-Z]*";
  385. } else {
  386. strRegexPattern += "(";
  387. strRegexPattern += req.vendor;
  388. strRegexPattern += ")";
  389. }
  390. strRegexPattern += "\\.";
  391. if (req.version.IsNullOrEmpty() || req.version.Compare("*") == 0) {
  392. strRegexPattern += "[0-9]*";
  393. } else {
  394. strRegexPattern += "(";
  395. strRegexPattern += req.version;
  396. strRegexPattern += ")";
  397. }
  398. strRegexPattern += "\\.";
  399. if (req.batch.IsNullOrEmpty() || req.batch.Compare("*") == 0) {
  400. strRegexPattern += "[0-9]*";
  401. } else {
  402. strRegexPattern += "(";
  403. strRegexPattern += req.batch;
  404. strRegexPattern += ")";
  405. }
  406. strRegexPattern += suffix;
  407. #endif //RVC_OS_LINUX
  408. return strRegexPattern;
  409. }
  410. void CDeviceControlEntity::GetVendorFileList(SpReqAnsContext<DeviceControlService_GetVendorFileList_Req, DeviceControlService_GetVendorFileList_Ans>::Pointer ctx)
  411. {
  412. #if defined(_MSC_VER)
  413. ctx->Answer(Error_NotImpl);
  414. #else
  415. ErrorCodeEnum result(Error_Succeed);
  416. int tmpResult = 0;
  417. CSimpleStringA tmpMsg(true);
  418. CSimpleStringA strDepDirPath;
  419. result = GetFunction()->GetPath("Dep", strDepDirPath);
  420. if (ctx->Req.root) {
  421. CAdapterLoadChecker* checker = new CAdapterLoadChecker();
  422. checker->Init(this);
  423. CAutoArray<AdapterRootConfigInfo> tmp;
  424. if (ctx->Req.adapter.IsNullOrEmpty() || ctx->Req.adapter.Compare("*") == 0) {
  425. tmp = checker->GetConfigedAdapterInfo();
  426. } else {
  427. tmp = checker->GetConfigedAdapterInfo(ctx->Req.adapter);
  428. }
  429. delete checker;
  430. checker = NULL;
  431. const int curSize = tmp.GetCount();
  432. ctx->Ans.filenames.Init(curSize);
  433. ctx->Ans.status.Init(curSize);
  434. ctx->Ans.port.Init(curSize);
  435. ctx->Ans.baudrate.Init(curSize);
  436. CSimpleStringA strDepDirPath2 = strDepDirPath + SPLIT_SLASH_STR;
  437. for (int i = 0; i < curSize; ++i) {
  438. ctx->Ans.filenames[i] = tmp[i].strDepDirWithSlashOrFileName;
  439. ctx->Ans.port[i] = tmp[i].dwPort;
  440. ctx->Ans.baudrate[i] = tmp[i].dwBaudRate;
  441. CSimpleStringA fullPath = strDepDirPath2 + ctx->Ans.filenames[i];
  442. ctx->Ans.status[i] = 0x2;
  443. if (ExistsFileA(fullPath)) {
  444. ctx->Ans.status[i] |= 0x1;
  445. }
  446. }
  447. } else {
  448. std::vector<std::string> adapterFiles;
  449. array_header_t* subs = fileutil_get_sub_files_a(strDepDirPath);
  450. if (subs) {
  451. regex_t reg;
  452. CSystemStaticInfo info;
  453. GetFunction()->GetSystemStaticInfo(info);
  454. CSimpleStringA pattern = GetRegexPatternString(ctx->Req, info.strMachineType);
  455. int ret = regcomp(&reg, pattern, REG_EXTENDED | REG_NOSUB);
  456. if (ret) {
  457. char ebuff[256];
  458. regerror(ret, &reg, ebuff, 256);
  459. DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM)("regex failed: %s", ebuff);
  460. tmpResult = Error_Unexpect;
  461. tmpMsg = CSimpleStringA::Format("内部错误:REGEX %s", ebuff);
  462. } else {
  463. DbgWithLink(LOG_LEVEL_DEBUG, LOG_TYPE_SYSTEM)("pattern: %s", pattern.GetData());
  464. for (int i = 0; i < subs->nelts; ++i) {
  465. char* filenamePath = ARRAY_IDX(subs, i, char*);
  466. char* filename = &filenamePath[strDepDirPath.GetLength() + 1];
  467. ret = regexec(&reg, filename, 0, NULL, 0);
  468. if (0 == ret) {
  469. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("filename %s matched!", filename);
  470. adapterFiles.push_back(std::string(filename));
  471. }
  472. }
  473. }
  474. const int curSize = adapterFiles.size();
  475. ctx->Ans.filenames.Init(curSize);
  476. ctx->Ans.status.Init(curSize);
  477. ctx->Ans.port.Init(curSize);
  478. ctx->Ans.baudrate.Init(curSize);
  479. for (int i = 0; i < curSize; ++i) {
  480. ctx->Ans.status[i] = 0;
  481. ctx->Ans.port[i] = 0;
  482. ctx->Ans.baudrate[i] = 0;
  483. }
  484. if (curSize > 0) {
  485. CAdapterLoadChecker* checker = new CAdapterLoadChecker();
  486. checker->Init(this);
  487. int cnt = 0;
  488. for (auto iter = adapterFiles.cbegin(); iter != adapterFiles.cend(); ++iter, ++cnt) {
  489. ctx->Ans.filenames[cnt] = iter->c_str();
  490. ctx->Ans.status[cnt] = 0x1;
  491. AdapterRootConfigInfo info;
  492. if (checker->IsThisFileConfigure(iter->c_str(), &info)) {
  493. ctx->Ans.status[cnt] |= 0x2;
  494. ctx->Ans.port[cnt] = info.dwPort;
  495. ctx->Ans.baudrate[cnt] = info.dwBaudRate;
  496. }
  497. }
  498. delete checker;
  499. }
  500. toolkit_array_free2(subs);
  501. }
  502. }
  503. ctx->Ans.result = tmpResult;
  504. ctx->Ans.msg = tmpMsg;
  505. ctx->Answer(result);
  506. return;
  507. #endif //_MSC_VER
  508. }
  509. void CDeviceControlEntity::TestAdapterFileAvailable(
  510. SpReqAnsContext<DeviceControlService_TestAdapterFileAvailable_Req, DeviceControlService_TestAdapterFileAvailable_Ans>::Pointer ctx)
  511. {
  512. #if defined(_MSC_VER)
  513. ctx->Answer(Error_NotImpl);
  514. #else
  515. ErrorCodeEnum result(Error_Succeed);
  516. int tmpResult = 0;
  517. CSimpleStringA tmpMsg(true);
  518. if (ctx->Req.type == 1) /*循环验证配置适配器的有效性*/ {
  519. CAdapterLoadChecker* checker = new CAdapterLoadChecker();
  520. checker->Init(this);
  521. CAutoArray<CAdapterLoader*>& tmp = checker->AdapterCoinfigList();
  522. const int curSize = tmp.GetCount();
  523. std::vector<std::string> errlibNames;
  524. std::vector<std::string> errMsgs;
  525. for (int i = 0; i < curSize; ++i) {
  526. ErrorCodeEnum ec(Error_Succeed);
  527. CSimpleStringA strLastErrMsg(true);
  528. VendorLibInfoEx& libInfo = tmp[i]->vendorLibInfo;
  529. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("to check %s", libInfo.toLibNameString().GetData());
  530. ec = CAdapterLoadChecker::CheckDeviceAndAdapterAvailable(libInfo, strLastErrMsg);
  531. if (ec != Error_Succeed) {
  532. errlibNames.push_back(std::string(libInfo.toLibNameString().GetData()));
  533. errMsgs.push_back(std::string(strLastErrMsg.GetData()));
  534. strLastErrMsg = CSimpleStringA::Format("%s: %s", libInfo.toLibNameString().GetData(), strLastErrMsg.GetData());
  535. if (!tmpMsg.IsNullOrEmpty()) tmpMsg += "\n";
  536. tmpMsg += strLastErrMsg;
  537. if (!!(ctx->Req.options & 0x1)) {
  538. DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM)("detect critical error limit, break it.");
  539. tmpResult = ec;
  540. break;
  541. }
  542. }
  543. BroadcastAdapterTestResult(libInfo.toLibNameString(), ec, strLastErrMsg);
  544. }
  545. delete checker;
  546. checker = NULL;
  547. if (!(ctx->Req.options & 0x1) && !tmpMsg.IsNullOrEmpty()) {
  548. tmpResult = Error_Failed;
  549. }
  550. if (!errlibNames.empty()) {
  551. const int curErrSize = errlibNames.size();
  552. ctx->Ans.reserved1.Init(curErrSize);
  553. ctx->Ans.reserved2.Init(curErrSize);
  554. ctx->Ans.reserved3.Init(curErrSize);
  555. int cnt(0);
  556. for (auto iter = errlibNames.cbegin(); iter != errlibNames.cend(); ++iter) {
  557. ctx->Ans.reserved1[cnt] = 0;
  558. ctx->Ans.reserved2[cnt] = iter->c_str();
  559. ctx->Ans.reserved3[cnt] = errMsgs[cnt].c_str();
  560. cnt++;
  561. }
  562. }
  563. } else {
  564. if (ctx->Req.filename.IsNullOrEmpty()) {
  565. ctx->Answer(Error_Param);
  566. return;
  567. }
  568. VendorLibInfoEx libInfo;
  569. if (libInfo.Load(ctx->Req.filename)) {
  570. libInfo.optCfg.dwPort = ctx->Req.port;
  571. libInfo.optCfg.dwBaudRate = ctx->Req.baudrate;
  572. libInfo.optCfg.dwParam1 = ctx->Req.param1;
  573. libInfo.optCfg.dwParam2 = ctx->Req.param2;
  574. GetFunction()->GetPath("Dep", libInfo.optCfg.strDepDirWithSlashOrFileName);
  575. libInfo.optCfg.strDepDirWithSlashOrFileName += SPLIT_SLASH_STR;
  576. } else if (ctx->Req.filename.IndexOf(".") == -1) { //pure adapter name
  577. CAdapterLoader loader(ctx->Req.filename, this);
  578. tmpResult = loader.LoadConfigFromRootIni();
  579. libInfo = loader.vendorLibInfo;
  580. } else {
  581. DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM)("invalid adapter file name: %s", ctx->Req.filename.GetData());
  582. ctx->Answer(Error_MisMatched);
  583. return;
  584. }
  585. if (tmpResult == 0) {
  586. tmpResult = CAdapterLoadChecker::CheckDeviceAndAdapterAvailable(libInfo, tmpMsg);
  587. }
  588. }
  589. ctx->Ans.status = tmpResult;
  590. ctx->Ans.msg = tmpMsg;
  591. ctx->Answer(result);
  592. #endif //_MSC_VER
  593. return;
  594. }
  595. std::pair<ErrorCodeEnum, CSimpleStringA> CDeviceControlEntity::__ReadCenterConfigStr(CSimpleStringA key, CSimpleStringA entityName = "")
  596. {
  597. CSimpleStringA str = "";
  598. CSmartPointer<IConfigInfo> spCerConfig;
  599. ErrorCodeEnum err = GetFunction()->OpenConfig(Config_CenterSetting, spCerConfig);
  600. if (err != Error_Succeed)
  601. DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM)("OpenConfig failed: %s", SpStrError(err));
  602. else
  603. err = spCerConfig->ReadConfigValue(
  604. entityName == CSimpleStringA("") ? "IEBrowser" : (const char*)entityName.GetData()
  605. , (const char*)key.GetData(), str);
  606. if (str.IsNullOrEmpty() && key.Compare("CenterConfigUrl") == 0 && entityName.Compare("CenterSetting") == 0) {
  607. DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM)("Try to read CenterSettings download url from shell.ini again");
  608. CSmartPointer<IConfigInfo> pConfigShell;
  609. GetFunction()->OpenConfig(Config_Shell, pConfigShell);
  610. CSimpleStringA section("CenterSettingsDownloadUrl");
  611. pConfigShell->ReadConfigValue(section, "LAN", str);
  612. if (Error_Succeed != err && !str.IsNullOrEmpty()) {
  613. err = Error_Succeed;
  614. }
  615. }
  616. return std::make_pair(err, str);
  617. }
  618. void CDeviceControlEntity::BroadcastAdapterTestResult(const CSimpleStringA& adapterName, ErrorCodeEnum result, const CSimpleStringA& strErrMsg)
  619. {
  620. DeviceControl::AdapterTestResult evt;
  621. evt.result = result;
  622. evt.adapterInfo = adapterName;
  623. evt.errMsg = strErrMsg;
  624. SpSendBroadcast(GetFunction(), SP_MSG_OF(AdapterTestResult), SP_MSG_SIG_OF(AdapterTestResult), evt);
  625. }
  626. void CDeviceControlEntity::OnPreStart(CAutoArray<CSimpleStringA> strArgs, CSmartPointer<ITransactionContext> pTransactionContext)
  627. {
  628. LOG_FUNCTION();
  629. ErrorCodeEnum rt = m_fsm.Init(this);
  630. ///**TODO(Gifur@9/28/2023): UOS 下需要监控鼠标移动状态吗? */
  631. #if defined(_MSC_VER)
  632. CloseHandle(CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)&cursorPosUpdate, NULL, 0, NULL));
  633. #endif //_MSC_VER
  634. pTransactionContext->SendAnswer(rt);
  635. }
  636. SP_BEGIN_ENTITY_MAP()
  637. SP_ENTITY(CDeviceControlEntity)
  638. SP_END_ENTITY_MAP()