NewModelDPC.cpp 38 KB

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