CanModelDPC.cpp 37 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414
  1. // CanModelDPC.cpp : 定义 DLL 应用程序的导出函数。
  2. //
  3. #include "stdafx.h"
  4. #include "CanModelDPC.h"
  5. #include "DevTree.h"
  6. #include "CanModelDevice.h"
  7. #include "common_api.h"
  8. #include <iostream>
  9. #include <sstream>
  10. #include "localConfig.h"
  11. #include "ModuleConfig.h"
  12. #include <iostream>
  13. #include <filesystem>
  14. #include <system_error>
  15. CANMODELDPC_C_API DriverDPC* GetDriverDPC()
  16. {
  17. return (DriverDPC*)(new CanModelDPC());
  18. }
  19. CANMODELDPC_C_API void ReleaseDriverDPC(DriverDPC *p)
  20. {
  21. CanModelDPC *org = (CanModelDPC*)p;
  22. delete org;
  23. }
  24. typedef nsDEV::IODriver* (*GetDriverMgrAPI)();
  25. typedef VirtualDevice* (*CreateVirtuaDeviceAPI)();
  26. typedef void (*FreeVirtualDeviceAPI)(VirtualDevice*);
  27. Log4CPP::Logger* //mLog::gLogger = nullptr;
  28. HANDLE g_hExit = INVALID_HANDLE_VALUE;
  29. CanModelDPC::CanModelDPC(void)
  30. {
  31. m_pWorkpath = new std::string();
  32. m_MainHostDevice = nullptr;
  33. //m_pMidObject = 0;
  34. //m_pMidObject.reset(new eDEV::Mediator());
  35. if (//mLog::gLogger == nullptr)
  36. {
  37. string strLogPath = GetProcessDirectory() + R"(\Conf\Log4CPP.Config.xml)";
  38. string LogHost = ((string)getLogRootpath()).c_str();
  39. Log4CPP::ThreadContext::Map::Set(ECOM::Utility::Hash("LogFileName"), "Platform");
  40. //Log4CPP::GlobalContext::Map::Set("LogHost", LogHost.c_str());
  41. Log4CPP::ThreadContext::Map::Set(ECOM::Utility::Hash("LogHost"), LogHost.c_str() + 1);
  42. auto rc = Log4CPP::LogManager::LoadConfigFile(strLogPath.c_str());
  43. //mLog::gLogger = Log4CPP::LogManager::GetLogger("Platform");
  44. //mLog::FINFO("Code Build datetime [{$} {$}]", __DATE__, __TIME__);
  45. }
  46. g_hExit = CreateEvent(NULL, TRUE, FALSE, NULL);
  47. }
  48. CanModelDPC::~CanModelDPC(void)
  49. {
  50. delete m_pWorkpath;
  51. SetEvent(g_hExit);
  52. }
  53. bool CanModelDPC::LoadVirtualDevice(const char* pDevicePath, bool isImgDevice, bool bAddToList, string devPath, string devSrvPath)
  54. {
  55. string devDeriverPath = GetProcessDirectory() + "\\VirtualDevices\\" + pDevicePath;
  56. string FilePath = GetFileDirectory(devDeriverPath);
  57. AddEnvPath(FilePath.c_str());
  58. auto hModle = LoadLibrary(devDeriverPath.c_str());
  59. //m_arrImgeFunc = nullptr;
  60. if (hModle)
  61. {
  62. VirtualDevice* pDevice = nullptr;
  63. //mLog::FINFO("Load Driver ok.. {$}", devDeriverPath);
  64. CreateVirtuaDeviceAPI callfunc = (CreateVirtuaDeviceAPI)GetProcAddress(hModle, "CreateVirtualDevice");
  65. if (callfunc)
  66. {
  67. pDevice = callfunc();
  68. if (pDevice)
  69. {
  70. int nIndex = -1;
  71. if (isImgDevice)
  72. {
  73. if (bAddToList)
  74. {
  75. //"ImageProcessIndex":
  76. // "DescKey":"ImageProcessIndex",
  77. // "Value" : "2"
  78. /*ResDataObject resPri;
  79. if (RET_SUCCEED == pDevice->DevGet("", "ImageProcessIndex", resPri))
  80. {
  81. string strPri = (const char*)resPri;
  82. if (strPri.length() > 0)
  83. {
  84. resPri = "";
  85. int nPri = atoi(strPri.c_str());
  86. for (int x = 0; x < m_arrImgeProcessDevices.size(); x++)
  87. {
  88. if (m_arrImgeProcessDevices[x]->DevGet("", "ImageProcessIndex", resPri) == RET_SUCCEED)
  89. {
  90. strPri = (const char*)resPri;
  91. if (atoi(strPri.c_str()) < nPri)
  92. {
  93. nIndex = x;
  94. continue;
  95. }
  96. else
  97. break;
  98. }
  99. }
  100. }
  101. }*/
  102. ImageProcessAPI func = (ImageProcessAPI)GetProcAddress(hModle, "DeviceImgProcess");
  103. if (nIndex >= 0)
  104. {
  105. //mLog::FINFO("Inesrt {$} at [{$}] ImageProcess Array", pDevice->GetCcosRootPath(), nIndex);
  106. m_arrImgeProcessDevices.insert(m_arrImgeProcessDevices.begin()+nIndex, pDevice);
  107. m_arrImgeFunc.insert(m_arrImgeFunc.begin()+nIndex, func);
  108. }
  109. else
  110. {
  111. //mLog::FINFO("Append {$} ImageProcess Array at [{$}] ", pDevice->GetCcosRootPath(), m_arrImgeFunc.size());
  112. m_arrImgeProcessDevices.push_back(pDevice);
  113. m_arrImgeFunc.push_back(func);
  114. }
  115. }
  116. m_hImgeDeviceHandle = hModle;
  117. }
  118. else
  119. {
  120. if (bAddToList)
  121. {
  122. m_arrMessageProcessDevices.push_back( pDevice);
  123. m_hMessageDeviceHandle = hModle;
  124. }
  125. }
  126. if (!bAddToList)
  127. {
  128. //mLog::FINFO("Only Init Module from Template ");
  129. FreeVirtualDeviceAPI freeFunc = (FreeVirtualDeviceAPI)GetProcAddress(hModle, "ReleaseVirtualDevice");
  130. if (freeFunc && pDevice)
  131. {
  132. //创建对象,只是模型文件操作
  133. //mLog::FINFO(" SetDeviceInfo {$} devSrvPath: {$} ",devPath, devSrvPath);
  134. pDevice->SetDeviceInfo(devPath, devSrvPath);
  135. //mLog::FINFO("First Install Test and check Module Begin ");
  136. pDevice->SetClientRootID("", devSrvPath.c_str());
  137. //mLog::FINFO("Test and check Module End ");
  138. freeFunc(pDevice);
  139. }
  140. }
  141. //m_NewDevManager.reset(pDriver);
  142. //succeed
  143. return true;
  144. }
  145. else
  146. {
  147. DWORD errNo = GetLastError();
  148. //mLog::FERROR("lib:{$} no Object returned.ErrNo:{$}\n", devDeriverPath, errNo);
  149. }
  150. }
  151. else
  152. {
  153. DWORD errNo = GetLastError();
  154. //mLog::FERROR("lib:{$} no CreateIODriver entry.ErrNo:{$}\n", devDeriverPath, errNo);
  155. }
  156. }
  157. else
  158. {
  159. DWORD errNo = GetLastError();
  160. //mLog::FERROR("Load lib:{$} failed.ErrNo:{$}\n", devDeriverPath, errNo);
  161. }
  162. return false;
  163. }
  164. const int MAX_IMG_THREAD = 4;
  165. //using imgFuncThreadParams = std::tuple<ImageProcessAPI, int, char*, char*, char*, DWORD, LogicDevice*, LogicDevice*>;
  166. using imgFuncThreadParams = std::tuple<int, ImageProcessAPI*, int, char*, char*, char*, DWORD, LogicDevice*, LogicDevice**>;
  167. imgFuncThreadParams g_paramImgThread[MAX_IMG_THREAD];
  168. HANDLE g_hImgThread[MAX_IMG_THREAD];
  169. HANDLE g_hIdleEvent[MAX_IMG_THREAD+1];
  170. HANDLE g_hDataReaded[MAX_IMG_THREAD];
  171. HANDLE g_hDataReadyEvent[MAX_IMG_THREAD+1];
  172. DWORD WINAPI ImgeProcessThread(LPVOID pPara);
  173. bool CanModelDPC::InitDeviceMgr(const char *pDriverpath)
  174. {
  175. m_Module = LoadLibrary(pDriverpath);
  176. if (m_Module)
  177. {
  178. //mLog::FINFO("Load Driver ok.. {$}", pDriverpath);
  179. GetDriverMgrAPI callfunc = (GetDriverMgrAPI)GetProcAddress(m_Module, "CreateIODriver");
  180. if (callfunc)
  181. {
  182. auto pDriver = reinterpret_cast <nsDEV::IODriver*> (callfunc());
  183. if (pDriver)
  184. {
  185. m_NewDevManager.reset(pDriver);
  186. //succeed
  187. SECURITY_ATTRIBUTES sa = { 0 };
  188. sa.bInheritHandle = TRUE;
  189. sa.nLength = sizeof(SECURITY_ATTRIBUTES);
  190. DWORD threadID;
  191. for (int x = 0; x < MAX_IMG_THREAD; x++)
  192. {
  193. g_hDataReadyEvent[x] = CreateEvent(NULL, FALSE, FALSE, NULL);
  194. g_hIdleEvent[x] = CreateEvent(NULL, FALSE, TRUE, NULL);
  195. g_hDataReaded[x] = CreateEvent(NULL, FALSE, FALSE, NULL);
  196. g_hImgThread[x] = CreateThread(&sa, 0, ImgeProcessThread, (void*)x, 0, &threadID);
  197. //mLog::FINFO("Create ImageProcessThread ID={$}", threadID);
  198. }
  199. g_hDataReadyEvent[MAX_IMG_THREAD] = g_hExit;
  200. g_hIdleEvent[MAX_IMG_THREAD] = g_hExit;
  201. ///初始化 图像处理线程需要的数据
  202. return true;
  203. }
  204. else
  205. {
  206. DWORD errNo = GetLastError();
  207. //mLog::FERROR("lib:{$} no Object returned.ErrNo:{$}\n", pDriverpath, errNo);
  208. }
  209. }
  210. else
  211. {
  212. DWORD errNo = GetLastError();
  213. //mLog::FERROR("lib:{$} no CreateIODriver entry.ErrNo:{$}\n", pDriverpath, errNo);
  214. }
  215. }
  216. else
  217. {
  218. DWORD errNo = GetLastError();
  219. //mLog::FERROR("Load lib:{$} failed.ErrNo:{$}\n", pDriverpath, errNo);
  220. }
  221. return false;
  222. }
  223. static void TrimeSDKPath(string& sdkPath)
  224. {
  225. if (sdkPath.length() > 0)
  226. {
  227. if (sdkPath[0] == '\\' || sdkPath[0] == '/')
  228. sdkPath = sdkPath.substr(1);
  229. if (sdkPath.length() > 0)
  230. {
  231. if (sdkPath[sdkPath.length() - 1] == '\\' || sdkPath[sdkPath.length() - 1] == '/')
  232. {
  233. sdkPath = sdkPath.substr(0, sdkPath.length() - 1);
  234. }
  235. }
  236. }
  237. }
  238. namespace fs = std::filesystem;
  239. void copy_directory(const fs::path& src, const fs::path& dst) {
  240. if (!fs::exists(dst)) {
  241. fs::create_directory(dst);
  242. }
  243. for (auto& dir_entry : fs::recursive_directory_iterator(src)) {
  244. const auto& path = dir_entry.path();
  245. auto relative_path = path.lexically_relative(src);
  246. fs::path dest_path = dst / relative_path;
  247. if (fs::is_directory(path)) {
  248. fs::create_directories(dest_path);
  249. }
  250. else if (fs::is_regular_file(path)) {
  251. fs::copy(path, dest_path, fs::copy_options::update_existing);
  252. }
  253. else {
  254. std::cerr << "Unhandled path: " << path << std::endl;
  255. }
  256. }
  257. }
  258. bool CanModelDPC::DriverEntry(ResDataObject &Configuration)
  259. {
  260. const char *pDriverpath = Configuration["oemdriver"];
  261. if (pDriverpath == 0 || strlen(pDriverpath) == 0)
  262. {
  263. //mLog::FERROR("context of oemdriver is empty");
  264. return false;
  265. }
  266. m_resHardwareInfo.update("MajorID", (const char*)Configuration["MajorID"]);
  267. m_resHardwareInfo.update("MinorID", (const char*)Configuration["MinorID"]);
  268. m_resHardwareInfo.update("VendorID", (const char*)Configuration["VendorID"]);
  269. m_resHardwareInfo.update("ProductID", (const char*)Configuration["ProductID"]);
  270. m_resHardwareInfo.update("SerialID", (const char*)Configuration["SerialID"]);
  271. string sdkPath,realSdkPath;
  272. m_nServerIdx = -1;
  273. if (Configuration.GetKeyCount("ServerID") > 0)
  274. {
  275. //有ServerID配置
  276. m_nServerIdx = (int)Configuration["ServerID"];
  277. if (m_nServerIdx > 0)
  278. {
  279. //mLog::FINFO("Need clone SDK path from 0");
  280. if (Configuration.GetKeyCount("SDKPath") > 0)
  281. {
  282. sdkPath = (const char*)Configuration["SDKPath"];
  283. realSdkPath = sdkPath;
  284. TrimeSDKPath(realSdkPath);
  285. realSdkPath = GetProcessDirectory() + "\\" + realSdkPath;
  286. string newSdkPath = realSdkPath + to_string(m_nServerIdx);
  287. //mLog::FINFO("Try Copy Directory from {$} to {$}", realSdkPath, newSdkPath);
  288. try {
  289. copy_directory(realSdkPath, newSdkPath);
  290. }
  291. catch (...)
  292. {
  293. //mLog::FWARN("Copy Directory from {$} to {$} Error maybe exists.", realSdkPath, newSdkPath);
  294. }
  295. if (sdkPath.length() > 0)
  296. {
  297. if (sdkPath[sdkPath.length() - 1] == '\\' )
  298. {
  299. sdkPath = sdkPath.substr(0, sdkPath.length() - 1) + to_string(m_nServerIdx) + "\\";
  300. }
  301. else if (sdkPath[sdkPath.length() - 1] == '/')
  302. {
  303. sdkPath = sdkPath.substr(0, sdkPath.length() - 1) + to_string(m_nServerIdx) + "/";
  304. }
  305. else
  306. {
  307. sdkPath = sdkPath + to_string(m_nServerIdx);
  308. }
  309. Configuration.update("SDKPath", sdkPath.c_str());
  310. }
  311. }
  312. }
  313. }
  314. if (InitDeviceMgr(pDriverpath) == false)
  315. {
  316. //mLog::FERROR("CreateIODriver Failed.{$}",pDriverpath);
  317. return false;
  318. }
  319. bool ret = ModuleDriver::DriverEntry(Configuration);
  320. string moduleConfig = GetModuleConfigrateFilePath(m_resHardwareInfo);
  321. string filepath;
  322. try {
  323. ResDataObject moduleConf;
  324. moduleConf.loadFile(moduleConfig.c_str());
  325. filepath = moduleConfig;
  326. //mLog::FINFO("Load Configration from Mudle Config [{$}]", moduleConfig);
  327. }
  328. catch (...)
  329. {
  330. //mLog::FERROR("Failed to Load Configration from Driver Config [{$}]", moduleConfig);
  331. filepath = GetDriverConfigFilePath();
  332. //mLog::FERROR("Try Load Configration from Driver Config [{$}]", moduleConfig);
  333. }
  334. //这里修改SDKPath和ServerID
  335. if (m_nServerIdx > 0 && sdkPath.length() > 0)
  336. {
  337. //mLog::FINFO("Change SDKPath and ServerID {$}", m_nServerIdx);
  338. ResDataObject resCtx;
  339. try
  340. {
  341. resCtx.loadFile(filepath.c_str());
  342. if (resCtx.GetKeyCount("CONFIGURATION") > 0)
  343. {
  344. if (resCtx["CONFIGURATION"].GetKeyCount("SDKPath") > 0 )
  345. {
  346. resCtx["CONFIGURATION"]["SDKPath"].update("Value", sdkPath.c_str());
  347. }
  348. else
  349. {
  350. ResDataObject resObj;
  351. resObj.add("Value", sdkPath.c_str());
  352. resCtx["CONFIGURATION"].add("SDKPath", resObj);
  353. }
  354. if (resCtx["CONFIGURATION"].GetKeyCount("ServerID") > 0)
  355. {
  356. resCtx["CONFIGURATION"]["ServerID"].update("Value", m_nServerIdx);
  357. }
  358. else
  359. {
  360. ResDataObject resObj;
  361. resObj.add("Value", m_nServerIdx);
  362. resCtx["CONFIGURATION"].add("ServerID", resObj);
  363. }
  364. if (resCtx["CONFIGURATION"].GetKeyCount("ComServerID") > 0)
  365. {
  366. resCtx["CONFIGURATION"]["ComServerID"].update("Value", m_nServerIdx);
  367. }
  368. resCtx.SaveFile(filepath.c_str());
  369. }
  370. }
  371. catch (...)
  372. {
  373. //mLog::FERROR("Read config file error {$}", filepath);
  374. }
  375. }
  376. if (ret)
  377. {
  378. //这里检查模型对象的配置项是否存在,如果是走模型配置,在安装的时候,会自动创建一份修改后的配置文件
  379. //这里可以直接检查到
  380. //mLog::FINFO("m_NewDevManager->DriverEntry {$}", filepath);
  381. ret = m_NewDevManager->DriverEntry(filepath);
  382. //mLog::FINFO("m_NewDevManager->Prepare ");
  383. m_NewDevManager->Prepare();
  384. }
  385. else
  386. {
  387. //mLog::FERROR("LogicDriver::DriverEntry Failed.{$}", filepath.c_str());
  388. }
  389. return ret;
  390. }
  391. DWORD SYSTEM_CALL CanModelDPC::OnNotify(HANDLE evtList[], DWORD count)
  392. {
  393. DWORD dwResult = WaitForMultipleObjects(count, evtList, FALSE, 2500);
  394. if ((dwResult >= WAIT_OBJECT_0) && (dwResult < WAIT_OBJECT_0 + count - 1))
  395. {
  396. //mLog::FINFO("OnNotify Platform");
  397. return dwResult - WAIT_OBJECT_0;
  398. }
  399. else
  400. {
  401. if (NULL != m_NewDevManager)
  402. {
  403. if (!m_NewDevManager->isConnected())
  404. {
  405. //mLog::FWARN("CONNECTED is false");
  406. if (*m_pConnectionStatus)
  407. {
  408. //mLog::FWARN("Notify disCONNECTED ");
  409. LogicDriver::DisConnect();
  410. }
  411. if (!*m_pConnectionStatus)
  412. {
  413. //mLog::FINFO("Try CONNECT");
  414. if (m_NewDevManager->Connect())
  415. {
  416. //mLog::FINFO("try CONNECTED is ok");
  417. LogicDriver::Connect();
  418. //mLog::FINFO("Notify CONNECTED is ok");
  419. }
  420. else
  421. {
  422. //mLog::FWARN("Notify disCONNECTED ");
  423. LogicDriver::DisConnect();
  424. //mLog::FWARN("Try CONNECT failed. ");
  425. }
  426. }
  427. }
  428. else
  429. {
  430. if (!*m_pConnectionStatus)
  431. {
  432. //mLog::FINFO("Auto CONNECTED is ok");
  433. LogicDriver::Connect();
  434. //mLog::FINFO("Notify CONNECTED is ok");
  435. }
  436. }
  437. //此处从程序启动后会一直打印,重复调用work函数
  438. ////mLog::FINFO("ImageSaveDev->work()");
  439. //((ImageSaveDev*)m_pImgSaveDev)->Work();
  440. }
  441. }
  442. return 1;
  443. }
  444. RET_STATUS CanModelDPC::OnAction(const char* pszActionName, const char* pszParams, ResDataObject& resResponse)
  445. {
  446. RET_STATUS ret = ModuleDriver::OnAction(pszActionName, pszParams, resResponse);
  447. if (ret != RET_NOSUPPORT)
  448. {
  449. //成功或者失败,
  450. return ret;
  451. }
  452. string action = pszActionName;
  453. if (action == "InstallVirutalDevice")
  454. {
  455. //安装虚拟设备
  456. /// "ABS": {
  457. // "InstalHost":"Detector",
  458. // "ImgeProcessDevice" : "ABS\\ABSX64.dll"
  459. // },
  460. // "DAP": {
  461. // "MessageProcessDevice": "DAP\\DAPX64.dll"
  462. // }
  463. //
  464. //找到配置项,检查文件
  465. //ResDataObject resTest,resdll;
  466. //resdll.add("ImgeProcessDevice", "ABS\\ABSX64.dll");
  467. //resTest.add("ABS", resdll);
  468. ResDataObject resConf;
  469. string fileName = GetDriverConfigFilePath();
  470. //mLog::FINFO("Try Load xml Config file {$} ", fileName);
  471. resConf.loadFile(fileName.c_str());
  472. //string test = resTest.encode();
  473. if (resConf.GetKeyCount("CONFIGURATION") <= 0)
  474. {
  475. //mLog::FERROR("config file {$} content error", fileName);
  476. ret = RET_FAILED;
  477. return ret;
  478. }
  479. //resConf = resConf["CONFIGURATION"];
  480. ResDataObject resVirtual;
  481. resVirtual.decode(pszParams);
  482. bool changed = false;
  483. string strServDevPath;
  484. for (int vdIdx = 0; vdIdx < resVirtual.size(); vdIdx++)
  485. {
  486. bool got = false;
  487. string vdModuleName;
  488. //DTS/ABS/...
  489. if (strcmp(resVirtual.GetKey(vdIdx), "RespTopic") == 0)
  490. continue;
  491. if (resVirtual[vdIdx].GetKeyCount("ServDevPath") > 0)
  492. strServDevPath = (const char*)resVirtual[vdIdx]["ServDevPath"];
  493. else
  494. strServDevPath = "";
  495. for (int cfIdx = 0; cfIdx < resVirtual[vdIdx].size(); cfIdx++)
  496. {
  497. string va = (const char*)resVirtual[vdIdx][cfIdx];
  498. if (va.length() > 4 && va.substr(va.length() - 4, 4) == ".dll")
  499. {
  500. string type = resVirtual[vdIdx].GetKey(cfIdx);
  501. vdModuleName = va;
  502. int idx = resConf["CONFIGURATION"].GetFirstOf(type.c_str());
  503. while (idx != -1)
  504. {
  505. string confV = resConf["CONFIGURATION"][idx];
  506. if (confV == va)
  507. {
  508. got = true;
  509. break;
  510. }
  511. idx = resConf["CONFIGURATION"].GetNextOf(type.c_str(), idx);
  512. }
  513. if (!got)
  514. {
  515. resConf["CONFIGURATION"].add(type.c_str(), va.c_str());
  516. changed = true;
  517. break;
  518. }
  519. }
  520. }
  521. if (resVirtual[vdIdx].GetKeyCount("ServDevPath") > 0)
  522. {
  523. }
  524. if (got || changed)
  525. {
  526. //新装
  527. string newdev = "CCOS/DEVICE/" + string(resVirtual.GetKey(vdIdx));
  528. string devPath = newdev;
  529. devPath += "/" + string((const char*)m_resHardwareInfo["VendorID"]);// , (const char*)Configuration["VendorID"]);
  530. devPath += "/" + string((const char*)m_resHardwareInfo["ProductID"]);//, (const char*)Configuration["ProductID"]);
  531. devPath += "/" + string((const char*)m_resHardwareInfo["SerialID"]);//, (const char*)Configuration["SerialID"]);
  532. //mLog::FINFO("New Virtual Device Path {$}", devPath);
  533. resResponse.add(strServDevPath.c_str(), "");
  534. if (strServDevPath.length() <= 0)
  535. strServDevPath = devPath;
  536. if (!got)
  537. {
  538. //mLog::FINFO("New install VD {$]", strServDevPath);
  539. //之前没有配置,则需要测试,并设置
  540. LoadVirtualDevice(vdModuleName.c_str(), false, false, devPath, strServDevPath);
  541. }
  542. else
  543. {
  544. //mLog::FINFO("VD had installed aready {$]", strServDevPath);
  545. for(int x=0;x<m_arrImgeProcessDevices.size();x++)
  546. if (m_arrImgeProcessDevices[x]->GetCcosRootPath() == strServDevPath)
  547. {
  548. //
  549. //mLog::FINFO("VD {$} had installed aready type ImageProcessDevices with idx {$]", strServDevPath, x);
  550. resResponse.update(strServDevPath.c_str(), "ON");
  551. }
  552. for (int x = 0; x < m_arrMessageProcessDevices.size(); x++)
  553. if (m_arrMessageProcessDevices[x]->GetCcosRootPath() == strServDevPath)
  554. {
  555. //
  556. //mLog::FINFO("VD {$} had installed aready type MessageProcessDevices with idx {$]", strServDevPath, x);
  557. resResponse.update(strServDevPath.c_str(), "ON");
  558. }
  559. if(strlen(resResponse[strServDevPath.c_str()]) == 0)
  560. resResponse.update(strServDevPath.c_str(), "OFF");
  561. }
  562. }
  563. }
  564. if (changed)
  565. {
  566. //ResDataObject save;
  567. //save.add("CONFIGURATION", resConf);
  568. resConf.SaveFile(fileName.c_str());
  569. }
  570. ret = RET_SUCCEED;
  571. //resResponse = "Install ok.";
  572. }
  573. else if (action == "restart")
  574. {
  575. resResponse = "reStart ok.";
  576. ret = RET_SUCCEED;
  577. exit(1);
  578. }
  579. return ret;
  580. }
  581. bool CanModelDPC::GetDeviceType(GUID &DevType)
  582. {
  583. string DriverType = m_NewDevManager->GetGUID();
  584. if (DriverType.size() == 0)
  585. {
  586. //mLog::FERROR("GetDeviceType Failed");
  587. return false;
  588. }
  589. //if (m_pNewDevManager->GetDriverType(DriverType) == false)
  590. //{
  591. // return false;
  592. //}
  593. return string_2_guid(DriverType.c_str(), DevType);
  594. }
  595. string DEVICE_ID = "";
  596. string TOPIC_PREFIX = "";
  597. bool SYSTEM_CALL CanModelDPC::Driver_Probe(ResDataObject& PARAM_OUT HardwareInfo)
  598. {
  599. //string DriverInfo = m_NewDevManager->DriverProbe();
  600. //mLog::FINFO("Driver : DriverInfo {$}", m_resHardwareInfo.encode());
  601. //if (m_pNewDevManager->Driver_Probe(DriverInfo))
  602. {
  603. ResDataObject Config = m_resHardwareInfo;
  604. HardwareInfo = m_resHardwareInfo;
  605. //if (Config.decode(DriverInfo.c_str()))
  606. {
  607. //HardwareInfo.add("MajorID", (const char*)Config["MajorID"]);
  608. //HardwareInfo.add("MinorID", (const char*)Config["MinorID"]);
  609. //HardwareInfo.add("VendorID", (const char*)Config["VendorID"]);
  610. //HardwareInfo.add("ProductID", (const char*)Config["ProductID"]);
  611. //HardwareInfo.add("SerialID", (const char*)Config["SerialID"]);
  612. ostringstream os,os2;
  613. os << (const char*)Config["VendorID"] << "_" << (const char*)Config["ProductID"] << "_" << (const char*)Config["SerialID"];
  614. DEVICE_ID = os.str();
  615. os2 << "" << (const char*)Config["MajorID"] ;//<< "/" << (const char*)Config["VendorID"] << "/" << (const char*)Config["ProductID"];
  616. os2 << "/" << (const char*)Config["SerialID"] << "/";
  617. TOPIC_PREFIX = os2.str();
  618. return true;
  619. }
  620. //else
  621. //{
  622. // //mLog::FERROR("Driver_Probe Failed");
  623. //}
  624. }
  625. return false;
  626. }
  627. RET_STATUS CanModelDPC::GetDeviceResource(ResDataObject *pDeviceResource)
  628. {
  629. RET_STATUS ret = RET_FAILED;
  630. ResDataObject BaseLayerConfig;
  631. if ((*m_config).GetPropties().size() > 0)
  632. {
  633. ModuleDriver::GetDeviceResource(pDeviceResource);
  634. //mLog::FINFO("ModuleDriver : GetDeviceResource {$}", pDeviceResource->encode());
  635. //BaseLayerConfig = *pDeviceResource;
  636. }
  637. string ResInfo;// = m_NewDevManager->GetResource();
  638. //mLog::FINFO("IO Driver : GetDeviceResource {$}", ResInfo);
  639. //if (m_pNewDevManager->GetDriverResource(ResInfo))
  640. {
  641. //low layerInfo
  642. ResDataObject LowLayerConfig;
  643. if ((*m_config).GetPropties().size() <= 0)
  644. {
  645. ResInfo = m_NewDevManager->GetResource();
  646. if (LowLayerConfig.decode(ResInfo.c_str()) == false)
  647. {
  648. //mLog::FERROR("GetDriverResource Failed");
  649. return ret;
  650. }
  651. ResDataObject BaseLayerConfig;
  652. ret = LogicDriver::GetDeviceResource(&BaseLayerConfig);
  653. if (ret < RET_SUCCEED)
  654. {
  655. return ret;
  656. }
  657. }
  658. //base LogicDriverinfo
  659. if (BaseLayerConfig.size() <= 0)
  660. {
  661. ret = LogicDriver::GetDeviceResource(&BaseLayerConfig);
  662. //mLog::FINFO("LogicDriver : GetDeviceResource {$}", BaseLayerConfig.encode());
  663. if (ret < RET_SUCCEED)
  664. {
  665. return ret;
  666. }
  667. }
  668. if (LowLayerConfig.GetFirstOf("Attribute") >= 0)
  669. {
  670. //loop Low Layer Attribute
  671. for (size_t i = 0; i < LowLayerConfig["Attribute"].size(); i++)
  672. {
  673. const char *pKey = LowLayerConfig["Attribute"].GetKey(i);
  674. if (pKey)
  675. {
  676. int BaseIdx = BaseLayerConfig["Attribute"].GetFirstOf(pKey);
  677. if (BaseIdx >= 0)
  678. {
  679. //exist then overwrite
  680. BaseLayerConfig["Attribute"][BaseIdx] = LowLayerConfig["Attribute"][i];
  681. }
  682. else
  683. {
  684. //not exist then add
  685. BaseLayerConfig["Attribute"].add(pKey, LowLayerConfig["Attribute"][i]);
  686. }
  687. }
  688. }
  689. }
  690. if (LowLayerConfig.GetFirstOf("Action") >= 0)
  691. {
  692. //loop Low Layer Action
  693. for (size_t i = 0; i < LowLayerConfig["Action"].size(); i++)
  694. {
  695. const char *pKey = LowLayerConfig["Action"].GetKey(i);
  696. if (pKey)
  697. {
  698. int BaseIdx = BaseLayerConfig["Action"].GetFirstOf(pKey);
  699. if (BaseIdx >= 0)
  700. {
  701. //exist then overwrite
  702. BaseLayerConfig["Action"][BaseIdx] = LowLayerConfig["Action"][i];
  703. }
  704. else
  705. {
  706. //not exist then add
  707. BaseLayerConfig["Action"].add(pKey, LowLayerConfig["Action"][i]);
  708. }
  709. }
  710. }
  711. }
  712. (*pDeviceResource) = BaseLayerConfig;
  713. //mLog::FINFO("GetDeviceResource Result {$}", BaseLayerConfig.encode());
  714. return RET_SUCCEED;
  715. }
  716. return ret;
  717. }
  718. /*
  719. void CanModelDPC::SubscribeSelf() {
  720. //这里订阅topic
  721. }*/
  722. bool CanModelDPC::Device_Probe(ResDataObject &HardwareInfo)
  723. {
  724. //string DeviceInfo = m_NewDevManager->DeviceProbe();
  725. //mLog::FINFO("Driver : Device_Probe {$}", m_resHardwareInfo.encode());
  726. //if (m_pNewDevManager->Device_Probe(DeviceInfo))
  727. {
  728. //ResDataObject Config;
  729. //if (Config.decode(DeviceInfo.c_str()))
  730. {
  731. //HardwareInfo.add("MajorID", (const char*)Config["MajorID"]);
  732. //HardwareInfo.add("MinorID", (const char*)Config["MinorID"]);
  733. //HardwareInfo.add("VendorID", (const char*)Config["VendorID"]);
  734. //HardwareInfo.add("ProductID", (const char*)Config["ProductID"]);
  735. //HardwareInfo.add("SerialID", (const char*)Config["SerialID"]);
  736. HardwareInfo = m_resHardwareInfo;
  737. return true;
  738. }
  739. //else
  740. //{
  741. // //mLog::FERROR("Device_Probe Failed");
  742. //}
  743. }
  744. return false;
  745. }
  746. bool CanModelDPC::SetDeviceWorkPath(const char *pWorkPath)
  747. {
  748. return true;
  749. }
  750. bool CanModelDPC::SetDriverWorkPath(const char *pWorkPath)
  751. {
  752. (*m_pWorkpath) = pWorkPath;
  753. //要把读取单一硬件属性的过程放到此处
  754. return true;
  755. }
  756. bool CanModelDPC::Connect()
  757. {
  758. bool bret = m_NewDevManager->Connect();
  759. if (bret)
  760. {
  761. LogicDriver::Connect();//make sure it's connected
  762. //mLog::FINFO("Connect Succeed");
  763. return true;
  764. }
  765. else
  766. {
  767. //mLog::FERROR("Connect Failed");
  768. }
  769. return false;
  770. }
  771. bool SYSTEM_CALL CanModelDPC::GetConnectionStatus()
  772. {
  773. return m_NewDevManager->isConnected();
  774. }
  775. void CanModelDPC::DisConnect()
  776. {
  777. m_NewDevManager->Disconnect();
  778. LogicDriver::DisConnect();//make sure it's Disconnected
  779. //mLog::FINFO("DisConnect Succeed");
  780. }
  781. bool SYSTEM_CALL CanModelDPC::OnHeartBeat()
  782. {
  783. return m_NewDevManager->OnHeartBeat();
  784. }
  785. DWORD WINAPI ImgeProcessThread(LPVOID pPara)
  786. {
  787. int nIndex = (int)pPara;
  788. imgFuncThreadParams* pParam = g_paramImgThread + nIndex;
  789. HANDLE hWait[2];
  790. hWait[0] = g_hDataReadyEvent[nIndex];
  791. hWait[1] = g_hExit;
  792. while (true)
  793. {
  794. DWORD dwWait = WaitForMultipleObjects(2, hWait, FALSE, 1000);
  795. if (dwWait == WAIT_OBJECT_0)
  796. {
  797. //有图像
  798. int nNum = std::get<0>(*pParam);
  799. ImageProcessAPI* func = std::get<1>(*pParam);
  800. LogicDevice** pDev = std::get<8>(*pParam);
  801. string Head = std::get<3>(*pParam);
  802. string context = std::get<4>(*pParam);
  803. SetEvent(g_hDataReaded[nIndex]);
  804. if (func != nullptr)
  805. {
  806. //string temp = std::get<3>(*pParam);
  807. for (int x = 0; x < nNum; x++)
  808. {
  809. if (func[x] == nullptr)
  810. {
  811. //mLog::FERROR("Thread[{$}] ImageProcessFunc is null with devpath= {$}", nIndex, pDev[x]->GetCcosRootPath());
  812. continue;
  813. }
  814. //mLog::FINFO("Thread[{$}] start ImageProcess {$} from {$} with devpath= {$}", nIndex, Head, std::get<7>(*pParam)->GetCcosRootPath(), pDev[x]->GetCcosRootPath());
  815. func[x](std::get<2>(*pParam), Head, (char*)context.c_str(), std::get<5>(*pParam), std::get<6>(*pParam),std::get<7>(*pParam), pDev[x]);
  816. //mLog::FINFO("Thread[{$}] ImageProcessFunc Over with devpath= {$}", nIndex, pDev[x]->GetCcosRootPath());
  817. }
  818. }
  819. else
  820. {
  821. //mLog::FERROR("Thread[{$}] ImageProcessFunc arrpoint is null ", nIndex);
  822. }
  823. SetEvent(g_hIdleEvent[nIndex]);
  824. }
  825. else if(dwWait == WAIT_OBJECT_0 + 1)
  826. {
  827. //mLog::FINFO("ImageThread {$} Got exit", nIndex);
  828. }
  829. }
  830. //delete pParam;
  831. //std::get<1>(*pParam) = Type;
  832. //std::get<2>(*pParam) = (char*)imageHead.c_str();
  833. //std::get<3>(*pParam) = (char*)resContext.encode();
  834. //std::get<4>(*pParam) = pFrameBuff;
  835. //std::get<5>(*pParam) = FrameSize;
  836. //std::get<6>(*pParam) = pDeviceFrom;
  837. //std::get<7>(*pParam) = m_arrImgeProcessDevices[x];
  838. return 0;
  839. }
  840. /// <summary>
  841. ///
  842. /// </summary>
  843. /// <param name="Type"></param>
  844. /// <param name="imageHead"></param>
  845. /// <param name="pFrameBuff"></param>
  846. /// <param name="FrameSize"></param>
  847. /// <param name="pDeviceFrom"></param>
  848. /// <returns></returns>
  849. int CanModelDPC::ImageRrocess(int Type, string& imageHead, ResDataObject& resContext, char* pFrameBuff, DWORD FrameSize, CanModelDevice* pDeviceFrom)
  850. {
  851. //mLog::FINFO("Begin");
  852. //std::vector<HANDLE> threads;
  853. if (m_arrImgeFunc.size() <= 0)
  854. return 0;
  855. char* context = (char*)resContext.encode();
  856. char* header = (char*)imageHead.c_str();
  857. HANDLE thread = INVALID_HANDLE_VALUE;
  858. //for (int x = 0; x < m_arrImgeFunc.size(); x++)
  859. {
  860. DWORD dwWait = WaitForMultipleObjects(MAX_IMG_THREAD + 1, g_hIdleEvent, FALSE, INFINITE);
  861. //mLog::FINFO("Wait for ImageProcessThrad Idle return {$}", dwWait);
  862. if (dwWait != WAIT_TIMEOUT && dwWait >= WAIT_OBJECT_0 && dwWait < WAIT_OBJECT_0 + MAX_IMG_THREAD)
  863. {
  864. imgFuncThreadParams* pParam = g_paramImgThread + (dwWait - WAIT_OBJECT_0);
  865. std::get<0>(*pParam) = m_arrImgeFunc.size();
  866. std::get<1>(*pParam) = m_arrImgeFunc.data();
  867. std::get<2>(*pParam) = Type;
  868. std::get<3>(*pParam) = header;
  869. std::get<4>(*pParam) = context;
  870. std::get<5>(*pParam) = pFrameBuff;
  871. std::get<6>(*pParam) = FrameSize;
  872. std::get<7>(*pParam) = pDeviceFrom;
  873. std::get<8>(*pParam) = (LogicDevice**)m_arrImgeProcessDevices.data();
  874. //mLog::FINFO("Notify ImageProcessThread [{$}] work ", dwWait - WAIT_OBJECT_0);
  875. SetEvent(g_hDataReadyEvent[dwWait - WAIT_OBJECT_0]);
  876. //等待线程 读取完数据 ,就可以下一个
  877. //mLog::FINFO("Start Wait {$} Thread to Read Data.", m_arrImgeFunc.size());
  878. DWORD dwRet = WaitForSingleObject(g_hDataReaded[dwWait - WAIT_OBJECT_0], 4000);
  879. if (dwRet == WAIT_OBJECT_0)
  880. //mLog::FINFO("Over Succeded with ret {$}", dwRet);
  881. else
  882. //mLog::FERROR("Over Timeout with ret {$}", dwRet);
  883. }
  884. else
  885. {
  886. //mLog::FERROR("No Process this Iamge with ret {$}", dwWait);
  887. }
  888. }
  889. //if (m_arrImgeFunc != nullptr)
  890. // return m_arrImgeFunc(Type, imageHead, (char*)resContext.encode(), pFrameBuff, FrameSize, pDeviceFrom, m_arrImgeProcessDevices);
  891. return m_arrImgeFunc.size();
  892. }
  893. /// <summary>
  894. ///
  895. /// </summary>
  896. /// <param name="resNotify"></param>
  897. /// <param name="pDeviceFrom"></param>
  898. /// <returns></returns>
  899. int CanModelDPC::NotifyMessageProcess(ResDataObject& resNotify, CanModelDevice* pDeviceFrom)
  900. {
  901. //mLog::FINFO("Begin");
  902. ResDataObject resResp;
  903. RET_STATUS ret = RET_FAILED;
  904. //if (m_arrMessageProcessDevices != nullptr)
  905. for(int x=0;x< m_arrMessageProcessDevices.size();x++)
  906. {
  907. ret = m_arrMessageProcessDevices[x]->Request(&resNotify, &resResp);
  908. }
  909. //if (m_arrImgeProcessDevices != nullptr)
  910. for(int x=0;x< m_arrImgeProcessDevices.size();x++)
  911. {
  912. ret = m_arrImgeProcessDevices[x]->Request(&resNotify, &resResp);
  913. }
  914. //mLog::FINFO("End");
  915. return ret ;
  916. }
  917. void InitLogicDevice(LogicDevice* p, string guidname)
  918. {
  919. }
  920. PVOID CanModelDPC::LoadLogicDevices()
  921. {
  922. std::cout << "**************** CanModelDPC :: LoadLogicDevices" << std::endl;
  923. //mLog::FINFO( "Load Logic Devices");
  924. int nDeviceNumber = 1;
  925. string filepath = GetDriverConfigFilePath();
  926. ResDataObject Config;
  927. string imgLogicDevice, noImgLogicDevice;
  928. std::cout << "**************** CanModelDPC Load Config " << filepath << std::endl;
  929. if (Config.loadFile(filepath.c_str()))
  930. {
  931. int nHaveDeviceNumber = (int)Config["CONFIGURATION"].GetKeyCount("DeviceNumber");
  932. if (nHaveDeviceNumber > 0)
  933. {
  934. nDeviceNumber = Config["CONFIGURATION"]["DeviceNumber"];
  935. //mLog::FINFO("Have {$} Devices", nDeviceNumber);
  936. }
  937. else
  938. {
  939. //mLog::FWARN("Have no DeviceNumber Attribute");
  940. }
  941. int nHaveImgLogicDevice = (int)Config["CONFIGURATION"].GetKeyCount("ImgeProcessDevice");
  942. if (nHaveImgLogicDevice > 0)
  943. {
  944. int first = Config["CONFIGURATION"].GetFirstOf("ImgeProcessDevice");
  945. while (first != -1)
  946. {
  947. imgLogicDevice = (const char*)Config["CONFIGURATION"][first];
  948. if (imgLogicDevice.length() > 0)
  949. {
  950. //图像处理逻辑设备
  951. LoadVirtualDevice(imgLogicDevice.c_str(), true);
  952. }
  953. first = Config["CONFIGURATION"].GetNextOf("ImgeProcessDevice", first);
  954. }
  955. }
  956. int nHaveNoneImgLogicDevice = (int)Config["CONFIGURATION"].GetKeyCount("MessageProcessDevice");
  957. if (nHaveNoneImgLogicDevice > 0)
  958. {
  959. int first = Config["CONFIGURATION"].GetFirstOf("MessageProcessDevice");
  960. while (first != -1)
  961. {
  962. imgLogicDevice = (const char*)Config["CONFIGURATION"][first];
  963. if (imgLogicDevice.length() > 0)
  964. {
  965. //图像处理逻辑设备
  966. LoadVirtualDevice(imgLogicDevice.c_str(), false);
  967. }
  968. first = Config["CONFIGURATION"].GetNextOf("MessageProcessDevice", first);
  969. }
  970. //noImgLogicDevice = (const char*)Config["CONFIGURATION"]["MessageProcessDevice"];
  971. }
  972. }
  973. else
  974. {
  975. //mLog::FERROR("Load config file failed {$}",filepath.c_str());
  976. }
  977. DevTree *pTree = (DevTree *)GetDeviceTree();
  978. //imgLogicDevice "VirtualDevice/ABS/ABSX64.dll"
  979. //m_arrImgeProcessDevices = nullptr;
  980. //if (imgLogicDevice.length() > 0)
  981. //{
  982. // //图像处理逻辑设备
  983. // LoadVirtualDevice(imgLogicDevice.c_str(), true);
  984. //}
  985. //noImgLogicDevice "VirtualDevice/AGD/AGDX64.dll"
  986. //m_arrMessageProcessDevices = nullptr;
  987. //if (noImgLogicDevice.length() > 0)
  988. //{
  989. // //非图像处理设备
  990. // LoadVirtualDevice(noImgLogicDevice.c_str());
  991. //}
  992. for (int i = 0; i < nDeviceNumber; i++)
  993. {
  994. CanModelDevice *p = new CanModelDevice();
  995. std::cout << " ****************CanModelDPC Try new Device " << i << " of " << nDeviceNumber << std::endl;
  996. auto pIoDevice = m_NewDevManager->CreateDevice(i);
  997. if (pIoDevice)
  998. {
  999. GUID guid2;
  1000. string guidname;
  1001. //((LogicDevice*)pChild)->GetDeviceType(guid);
  1002. //bool bget = pIoDevice->GetDeviceType(guid2);
  1003. guidname = pIoDevice->GetGUID();
  1004. //guid_2_string(guid2, guidname);
  1005. ResDataObject subpath;
  1006. subpath = (guidname).c_str();
  1007. ResDataObject probe,conn_usless;
  1008. Device_Probe(probe);
  1009. //此时知道宿主设备是谁,如果Generator/Detector
  1010. //
  1011. string hostName;
  1012. bool isMain = false;
  1013. if (probe.GetKeyCount("MajorID") > 0)
  1014. {
  1015. //找到宿主
  1016. hostName = "CCOS/DEVICE/" + string((const char* )probe["MajorID"]);
  1017. }
  1018. string fullpath = MakeDevicePath(probe, conn_usless, subpath);
  1019. string ccospath = MakeCcosPath(probe, conn_usless, subpath, false);
  1020. if (hostName.length() > 0)
  1021. {
  1022. if (ccospath.substr(0, hostName.length()) == hostName)
  1023. {
  1024. //宿主设备
  1025. isMain = true;
  1026. //mLog::FINFO("Got MainHost {$} of {$}", hostName, ccospath);
  1027. }
  1028. }
  1029. if (isMain && m_MainHostDevice == nullptr)
  1030. {
  1031. m_MainHostDevice = p;
  1032. //mLog::FINFO("Add ImageProcess Deivce : {$} ", m_arrImgeProcessDevices.size());
  1033. //嵌入式设备加载
  1034. for (int x = 0; x < m_arrImgeProcessDevices.size(); x++)
  1035. {
  1036. GUID guid2;
  1037. string guidname;
  1038. m_arrImgeProcessDevices[x]->GetDeviceType(guid2);
  1039. guid_2_string(guid2, guidname);
  1040. ResDataObject subpath;
  1041. subpath = (guidname).c_str();
  1042. ResDataObject probe, conn_usless;
  1043. Device_Probe(probe);
  1044. string fullpatht = MakeDevicePath(probe, conn_usless, subpath);
  1045. string ccospatht = MakeCcosPath(probe, conn_usless, subpath, false);
  1046. m_arrImgeProcessDevices[x]->SetClientRootID(fullpatht.c_str(), ccospatht.c_str());
  1047. m_arrImgeProcessDevices[x]->CompleteInit();
  1048. pTree->Add((PVOID)(m_arrImgeProcessDevices[x]), TYPE_DEVICE);
  1049. //mLog::FINFO("Try add ccos child {$} ", ccospatht);
  1050. m_MainHostDevice->AddCcosChildren(m_arrImgeProcessDevices[x], ccospatht.c_str());
  1051. }
  1052. //mLog::FINFO("Add MessageProcess Deivce : {$} ", m_arrImgeProcessDevices.size());
  1053. //if (m_arrMessageProcessDevices != nullptr)
  1054. for (int x = 0; x < m_arrMessageProcessDevices.size(); x++)
  1055. {
  1056. GUID guid2;
  1057. string guidname;
  1058. m_arrMessageProcessDevices[x]->GetDeviceType(guid2);
  1059. guid_2_string(guid2, guidname);
  1060. ResDataObject subpath;
  1061. subpath = (guidname).c_str();
  1062. ResDataObject probe, conn_usless;
  1063. Device_Probe(probe);
  1064. string fullpatht = MakeDevicePath(probe, conn_usless, subpath);
  1065. string ccospatht = MakeCcosPath(probe, conn_usless, subpath, false);
  1066. m_arrMessageProcessDevices[x]->SetClientRootID(fullpatht.c_str(), ccospatht.c_str());
  1067. m_arrMessageProcessDevices[x]->CompleteInit();
  1068. pTree->Add((PVOID)(m_arrMessageProcessDevices[x]), TYPE_DEVICE);
  1069. //mLog::FINFO("Try add ccos child {$} ", ccospatht);
  1070. m_MainHostDevice->AddCcosChildren(m_arrMessageProcessDevices[x], ccospatht.c_str());
  1071. }
  1072. }
  1073. //pDevice->SetEbusRoot(fullpath.c_str());
  1074. p->SetClientRootID(fullpath.c_str(), ccospath.c_str());
  1075. //mLog::FINFO(" SetClientRootID ", ccospath);
  1076. //p->SetClientRootID();
  1077. //mLog::FINFO("Get IoDevice Succeed");
  1078. p->SetDrvDPC(this);
  1079. HANDLE DisconnectEvt = GetPassiveDisConnectEvtHandle();
  1080. //p->ConnectMQTTServer();
  1081. p->Init(std::move(pIoDevice), DisconnectEvt);
  1082. //p->SubscribeAction();
  1083. //mLog::FINFO("Create IoDevice Succeed");
  1084. }
  1085. else
  1086. {
  1087. //mLog::FERROR("Create IoDevice Failed");
  1088. delete p;
  1089. return NULL;
  1090. }
  1091. //*/
  1092. ResDataObject resPri;
  1093. string strPri;
  1094. for (int x = 1; x < m_arrImgeProcessDevices.size(); x++)
  1095. {
  1096. for (int y = 0; y < x; y++)
  1097. {
  1098. unsigned int xPri, yPri;
  1099. xPri = yPri = -1;
  1100. if (m_arrImgeProcessDevices[x]->DevGet("", "ImageProcessIndex", resPri) == RET_SUCCEED)
  1101. {
  1102. strPri = (const char*)resPri;
  1103. if (strPri.length() > 0)
  1104. xPri = atoi(strPri.c_str());
  1105. //mLog::FINFO("Vd {$} ImageProcessIdex=[{$}]", m_arrImgeProcessDevices[x]->GetCcosRootPath(), strPri);
  1106. }
  1107. if (m_arrImgeProcessDevices[y]->DevGet("", "ImageProcessIndex", resPri) == RET_SUCCEED)
  1108. {
  1109. strPri = (const char*)resPri;
  1110. if (strPri.length() > 0)
  1111. xPri = atoi(strPri.c_str());
  1112. //mLog::FINFO("Vd {$} ImageProcessIdex=[{$}]", m_arrImgeProcessDevices[y]->GetCcosRootPath(), strPri);
  1113. }
  1114. if (xPri < yPri)
  1115. {
  1116. auto* pTemp = m_arrImgeProcessDevices[x];
  1117. auto fTemp = m_arrImgeFunc[x];
  1118. m_arrImgeProcessDevices[x] = m_arrImgeProcessDevices[y];
  1119. m_arrImgeProcessDevices[y] = pTemp;
  1120. m_arrImgeFunc[x] = m_arrImgeFunc[y];
  1121. m_arrImgeFunc[y] = fTemp;
  1122. //mLog::FINFO("Exchange Vd [{$}] <=> [{$}]", x, y);
  1123. }
  1124. }
  1125. }
  1126. //*/
  1127. LogicDevice *pret = (LogicDevice *)p;
  1128. pTree->Add((PVOID)pret, TYPE_DEVICE);
  1129. LogicDevice::AddEbusChildren(p, "");
  1130. }
  1131. return (PVOID)pTree;
  1132. }
  1133. void CanModelDPC::UnloadLogicDevices()
  1134. {
  1135. //mLog::FINFO( "UnLoad Logic Devices");
  1136. DevTree *pTree = (DevTree *)GetDeviceTree();
  1137. size_t Size = pTree->size();
  1138. for (size_t i = 0; i < Size; i++)
  1139. {
  1140. DevTreeNode node = (*pTree)[i];
  1141. if (node.m_NodeType == TYPE_DEVICE)
  1142. {
  1143. CanModelDevice *pdev = (CanModelDevice*)node.m_pObject;
  1144. delete pdev;
  1145. }
  1146. else
  1147. {
  1148. //tree dev
  1149. //do not copy this!!!!
  1150. //UnloadLogicDevices(node.m_pObject);
  1151. }
  1152. }
  1153. pTree->clear();
  1154. //mLog::FINFO( "UnLoad Over");
  1155. }
  1156. RET_STATUS DATA_ACTION CanModelDPC::GetDeviceConfig(ResDataObject PARAM_OUT* pDeviceConfig)
  1157. {
  1158. if ((*m_config).GetPropties().size() > 0)
  1159. {
  1160. //mLog::FERROR("GetDeviceConfig has module config do not return anything.");
  1161. return RET_FAILED;
  1162. }
  1163. //mLog::FINFO( "GetDeviceConfig start");
  1164. std::string strDeviceConfig;
  1165. if (m_NewDevManager->GetDeviceConfig(strDeviceConfig))
  1166. {
  1167. pDeviceConfig->decode(strDeviceConfig.c_str());
  1168. //mLog::FINFO( "GetDeviceConfig ok {$}", strDeviceConfig);
  1169. }
  1170. else
  1171. {
  1172. //mLog::FERROR( "GetDeviceConfig failed");
  1173. return RET_FAILED;
  1174. }
  1175. //mLog::FINFO( "GetDeviceConfig Over");
  1176. return RET_SUCCEED;
  1177. }
  1178. RET_STATUS DATA_ACTION CanModelDPC::SetDeviceConfig(ResDataObject PARAM_IN* DeviceConfig)
  1179. {
  1180. if ((*m_config).GetPropties().size() > 0)
  1181. {
  1182. return RET_FAILED;
  1183. }
  1184. //mLog::FINFO( "SetDeviceConfig start");
  1185. std::string strDeviceConfig = DeviceConfig->encode();
  1186. if (m_NewDevManager->SetDeviceConfig(strDeviceConfig))
  1187. {
  1188. }
  1189. else
  1190. {
  1191. return RET_FAILED;
  1192. }
  1193. //mLog::FINFO( "SetDeviceConfig Over");
  1194. return RET_SUCCEED;
  1195. }