NewModelDPC.cpp 45 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565
  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. static 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. std::array<imgFuncThreadParams, MAX_IMG_THREAD>& GetParamImgThread() {
  209. static std::array<imgFuncThreadParams, MAX_IMG_THREAD> instance;
  210. return instance;
  211. }
  212. // 获取线程句柄数组
  213. std::array<pthread_t, MAX_IMG_THREAD>& GetHImgThread() {
  214. static std::array<pthread_t, MAX_IMG_THREAD> instance;
  215. return instance;
  216. }
  217. // 获取空闲事件向量
  218. std::vector<std::shared_ptr<LinuxEvent>>& GetHIdleEvent() {
  219. static std::vector<std::shared_ptr<LinuxEvent>> instance;
  220. return instance;
  221. }
  222. // 获取数据已读事件向量
  223. std::vector<std::shared_ptr<LinuxEvent>>& GetHDataReaded() {
  224. static std::vector<std::shared_ptr<LinuxEvent>> instance;
  225. return instance;
  226. }
  227. // 获取数据就绪事件向量
  228. std::vector<std::shared_ptr<LinuxEvent>>& GetHDataReadyEvent() {
  229. static std::vector<std::shared_ptr<LinuxEvent>> instance;
  230. return instance;
  231. }
  232. void* ImgeProcessThread(void* pPara);
  233. bool NewModelDPC::InitDeviceMgr(const char *pDriverpath)
  234. {
  235. std::string strDriverPath = pDriverpath;
  236. std::string FilePath = GetFileDirectory(strDriverPath);
  237. std::cout << "InitDeviceMgr: Adding to LD_LIBRARY_PATH: " << FilePath << std::endl;
  238. if (!AddEnvPath(FilePath.c_str())) {
  239. std::cerr << "NewModelDPC::InitDeviceMgr: Failed to add library path to LD_LIBRARY_PATH: "
  240. << FilePath << std::endl;
  241. }
  242. m_Module = dlopen(pDriverpath, RTLD_LAZY);
  243. if (m_Module)
  244. {
  245. std::cout << "NewModelDPC::InitDeviceMgr: Successfully loaded driver library: " << pDriverpath << std::endl;
  246. //mLog::FINFO("Load Driver ok.. {$}", pDriverpath);
  247. typedef nsDEV::IODriver* (*GetDriverMgrAPI)();
  248. std::cout << "NewModelDPC::InitDeviceMgr: Trying to get 'CreateIODriver' function address" << std::endl;
  249. GetDriverMgrAPI callfunc = (GetDriverMgrAPI)dlsym(m_Module, "CreateIODriver");
  250. const char* dlsym_error = dlerror();
  251. if (dlsym_error) {
  252. std::cout << "NewModelDPC::InitDeviceMgr: Failed to get 'CreateIODriver' function: " << dlsym_error << std::endl;
  253. // 处理错误
  254. dlclose(m_Module);
  255. return false;
  256. }
  257. if (callfunc)
  258. {
  259. std::cout << "NewModelDPC::InitDeviceMgr: Successfully obtained 'CreateIODriver' function" << std::endl;
  260. auto pDriver = reinterpret_cast <nsDEV::IODriver*> (callfunc());
  261. if (pDriver)
  262. {
  263. std::cout << "NewModelDPC::InitDeviceMgr: Successfully created IODriver instance" << std::endl;
  264. m_NewDevManager.reset(pDriver);
  265. std::cout << "NewModelDPC::InitDeviceMgr: Starting to create " << MAX_IMG_THREAD << " image processing threads" << std::endl;
  266. for (int x = 0; x < MAX_IMG_THREAD; x++)
  267. {
  268. GetHDataReadyEvent().push_back(LinuxEvent::CreateEvent(LinuxEvent::AUTO_RESET, false));
  269. GetHIdleEvent().push_back(LinuxEvent::CreateEvent(LinuxEvent::AUTO_RESET, true));
  270. GetHDataReaded().push_back(LinuxEvent::CreateEvent(LinuxEvent::AUTO_RESET, false));
  271. std::cout << "NewModelDPC::InitDeviceMgr: Creating image processing thread " << x << std::endl;
  272. // 创建线程
  273. //int ret = pthread_create(&GetHImgThread()[x], nullptr, ImgeProcessThread, (void*)(intptr_t)x);
  274. //if (ret != 0) {
  275. // std::cout << "NewModelDPC::InitDeviceMgr: Failed to create thread " << x << " with error code: " << ret << std::endl;
  276. // // 线程创建失败处理
  277. //}
  278. //else {
  279. // std::cout << "NewModelDPC::InitDeviceMgr: Successfully created image processing thread " << x << std::endl;
  280. // // 可选:设置线程名称或属性
  281. //}
  282. }
  283. GetHDataReadyEvent().push_back(g_hExit);
  284. GetHIdleEvent().push_back(g_hExit);
  285. std::cout << "NewModelDPC::InitDeviceMgr: Initialized data for image processing threads" << std::endl;
  286. ///初始化 图像处理线程需要的数据
  287. return true;
  288. }
  289. else
  290. {
  291. std::cout << "NewModelDPC::InitDeviceMgr: Library " << pDriverpath << " returned null IODriver instance" << std::endl;
  292. dlclose(m_Module);
  293. //mLog::FERROR("lib:{$} no Object returned.ErrNo:{$}\n", pDriverpath, errNo);
  294. }
  295. }
  296. else
  297. {
  298. std::cout << "NewModelDPC::InitDeviceMgr: Library " << pDriverpath << " has no 'CreateIODriver' entry point" << std::endl;
  299. dlclose(m_Module);
  300. //mLog::FERROR("lib:{$} no CreateIODriver entry.ErrNo:{$}\n", pDriverpath, errNo);
  301. }
  302. }
  303. else
  304. {
  305. // 获取dlopen错误
  306. const char* dl_error = dlerror();
  307. std::cout << "NewModelDPC::InitDeviceMgr: Failed to load library " << pDriverpath << ". Error: " << (dl_error ? dl_error : "Unknown error") << std::endl;
  308. //mLog::FERROR("Load lib:{$} failed.ErrNo:{$}\n", pDriverpath, errNo);
  309. }
  310. return false;
  311. }
  312. static void TrimeSDKPath(std::string& sdkPath)
  313. {
  314. if (sdkPath.length() > 0)
  315. {
  316. if (sdkPath[0] == '\\' || sdkPath[0] == '/')
  317. sdkPath = sdkPath.substr(1);
  318. if (sdkPath.length() > 0)
  319. {
  320. if (sdkPath[sdkPath.length() - 1] == '\\' || sdkPath[sdkPath.length() - 1] == '/')
  321. {
  322. sdkPath = sdkPath.substr(0, sdkPath.length() - 1);
  323. }
  324. }
  325. }
  326. }
  327. namespace fs = std::filesystem;
  328. void copy_directory(const fs::path& src, const fs::path& dst) {
  329. if (!fs::exists(dst)) {
  330. fs::create_directory(dst);
  331. }
  332. for (auto& dir_entry : fs::recursive_directory_iterator(src)) {
  333. const auto& path = dir_entry.path();
  334. auto relative_path = path.lexically_relative(src);
  335. fs::path dest_path = dst / relative_path;
  336. if (fs::is_directory(path)) {
  337. fs::create_directories(dest_path);
  338. }
  339. else if (fs::is_regular_file(path)) {
  340. fs::copy(path, dest_path, fs::copy_options::update_existing);
  341. }
  342. else {
  343. std::cerr << "Unhandled path: " << path << std::endl;
  344. }
  345. }
  346. }
  347. bool NewModelDPC::DriverEntry(ResDataObject &Configuration)
  348. {
  349. std::cout << "Entering NewModelDPC::DriverEntry" << std::endl;
  350. const char *pDriverpath = Configuration["oemdriver"];
  351. std::cout << "Checking oemdriver configuration" << std::endl;
  352. if (pDriverpath == 0 || strlen(pDriverpath) == 0)
  353. {
  354. std::cout << "Error: context of oemdriver is empty" << std::endl;
  355. //mLog::FERROR("context of oemdriver is empty");
  356. return false;
  357. }
  358. std::cout << "Updating hardware information from configuration" << std::endl;
  359. m_resHardwareInfo.update("MajorID", (const char*)Configuration["MajorID"]);
  360. m_resHardwareInfo.update("MinorID", (const char*)Configuration["MinorID"]);
  361. m_resHardwareInfo.update("VendorID", (const char*)Configuration["VendorID"]);
  362. m_resHardwareInfo.update("ProductID", (const char*)Configuration["ProductID"]);
  363. m_resHardwareInfo.update("SerialID", (const char*)Configuration["SerialID"]);
  364. string sdkPath,realSdkPath;
  365. m_nServerIdx = -1;
  366. if (Configuration.GetKeyCount("ServerID") > 0)
  367. {
  368. std::cout << "ServerID configuration found" << std::endl;
  369. //有ServerID配置
  370. m_nServerIdx = (int)Configuration["ServerID"];
  371. std::cout << "ServerID value: " << m_nServerIdx << std::endl;
  372. if (m_nServerIdx > 0)
  373. {
  374. std::cout << "ServerID > 0, need to clone SDK path from 0" << std::endl;
  375. //mLog::FINFO("Need clone SDK path from 0");
  376. if (Configuration.GetKeyCount("SDKPath") > 0)
  377. {
  378. sdkPath = (const char*)Configuration["SDKPath"];
  379. realSdkPath = sdkPath;
  380. std::cout << "Original SDKPath: " << sdkPath << std::endl;
  381. TrimeSDKPath(realSdkPath);
  382. std::cout << "Trimmed SDKPath: " << realSdkPath << std::endl;
  383. realSdkPath = GetProcessDirectory() + "\\" + realSdkPath;
  384. std::cout << "Full SDKPath: " << realSdkPath << std::endl;
  385. string newSdkPath = realSdkPath + to_string(m_nServerIdx);
  386. std::cout << "Trying to copy directory from " << realSdkPath << " to " << newSdkPath << std::endl;
  387. //mLog::FINFO("Try Copy Directory from {$} to {$}", realSdkPath, newSdkPath);
  388. try {
  389. copy_directory(realSdkPath, newSdkPath);
  390. std::cout << "Successfully copied directory" << std::endl;
  391. }
  392. catch (...)
  393. {
  394. std::cout << "Warning: Failed to copy directory (may already exist)" << std::endl;
  395. //mLog::FWARN("Copy Directory from {$} to {$} Error maybe exists.", realSdkPath, newSdkPath);
  396. }
  397. if (sdkPath.length() > 0)
  398. {
  399. std::cout << "Updating SDKPath with ServerID suffix" << std::endl;
  400. if (sdkPath[sdkPath.length() - 1] == '\\' )
  401. {
  402. sdkPath = sdkPath.substr(0, sdkPath.length() - 1) + to_string(m_nServerIdx) + "\\";
  403. }
  404. else if (sdkPath[sdkPath.length() - 1] == '/')
  405. {
  406. sdkPath = sdkPath.substr(0, sdkPath.length() - 1) + to_string(m_nServerIdx) + "/";
  407. }
  408. else
  409. {
  410. sdkPath = sdkPath + to_string(m_nServerIdx);
  411. }
  412. Configuration.update("SDKPath", sdkPath.c_str());
  413. std::cout << "Updated SDKPath: " << sdkPath << std::endl;
  414. }
  415. }
  416. else
  417. {
  418. std::cout << "No SDKPath configuration found" << std::endl;
  419. }
  420. }
  421. }
  422. std::cout << "Initializing Device Manager with driver path: " << pDriverpath << std::endl;
  423. if (InitDeviceMgr(pDriverpath) == false)
  424. {
  425. std::cout << "Error: CreateIODriver Failed. Path: " << pDriverpath << std::endl;
  426. //mLog::FERROR("CreateIODriver Failed.{$}",pDriverpath);
  427. return false;
  428. }
  429. std::cout << "Device Manager initialized successfully" << std::endl;
  430. std::cout << "Calling ModuleDriver::DriverEntry" << std::endl;
  431. bool ret = ModuleDriver::DriverEntry(Configuration);
  432. std::cout << "ModuleDriver::DriverEntry returned: " << (ret ? "true" : "false") << std::endl;
  433. string moduleConfig = GetModuleConfigrateFilePath(m_resHardwareInfo);
  434. std::cout << "Trying to load configuration from module config: " << moduleConfig << std::endl;
  435. string filepath;
  436. try {
  437. ResDataObject moduleConf;
  438. moduleConf.loadFile(moduleConfig.c_str());
  439. filepath = moduleConfig;
  440. std::cout << "Successfully loaded configuration from module config: " << moduleConfig << std::endl;
  441. //mLog::FINFO("Load Configration from Mudle Config [{$}]", moduleConfig);
  442. }
  443. catch (...)
  444. {
  445. std::cout << "Error: Failed to load configuration from module config: " << moduleConfig << std::endl;
  446. //mLog::FERROR("Failed to Load Configration from Driver Config [{$}]", moduleConfig);
  447. filepath = GetDriverConfigFilePath();
  448. std::cout << "Trying to load configuration from driver config: " << filepath << std::endl;
  449. //mLog::FERROR("Try Load Configration from Driver Config [{$}]", moduleConfig);
  450. }
  451. //这里修改SDKPath和ServerID
  452. if (m_nServerIdx > 0 && sdkPath.length() > 0)
  453. {
  454. std::cout << "Updating SDKPath and ServerID in config file: " << m_nServerIdx << std::endl;
  455. //mLog::FINFO("Change SDKPath and ServerID {$}", m_nServerIdx);
  456. ResDataObject resCtx;
  457. try
  458. {
  459. resCtx.loadFile(filepath.c_str());
  460. std::cout << "Loaded config file: " << filepath << std::endl;
  461. if (resCtx.GetKeyCount("CONFIGURATION") > 0)
  462. {
  463. std::cout << "Updating CONFIGURATION section" << std::endl;
  464. if (resCtx["CONFIGURATION"].GetKeyCount("SDKPath") > 0 )
  465. {
  466. resCtx["CONFIGURATION"]["SDKPath"].update("Value", sdkPath.c_str());
  467. std::cout << "Updated SDKPath in config to: " << sdkPath << std::endl;
  468. }
  469. else
  470. {
  471. ResDataObject resObj;
  472. resObj.add("Value", sdkPath.c_str());
  473. resCtx["CONFIGURATION"].add("SDKPath", resObj);
  474. std::cout << "Added SDKPath to config: " << sdkPath << std::endl;
  475. }
  476. if (resCtx["CONFIGURATION"].GetKeyCount("ServerID") > 0)
  477. {
  478. resCtx["CONFIGURATION"]["ServerID"].update("Value", m_nServerIdx);
  479. std::cout << "Updated ServerID in config to: " << m_nServerIdx << std::endl;
  480. }
  481. else
  482. {
  483. ResDataObject resObj;
  484. resObj.add("Value", m_nServerIdx);
  485. resCtx["CONFIGURATION"].add("ServerID", resObj);
  486. std::cout << "Added ServerID to config: " << m_nServerIdx << std::endl;
  487. }
  488. if (resCtx["CONFIGURATION"].GetKeyCount("ComServerID") > 0)
  489. {
  490. resCtx["CONFIGURATION"]["ComServerID"].update("Value", m_nServerIdx);
  491. std::cout << "Updated ComServerID in config to: " << m_nServerIdx << std::endl;
  492. }
  493. resCtx.SaveFile(filepath.c_str());
  494. std::cout << "Saved updated config file: " << filepath << std::endl;
  495. }
  496. else
  497. {
  498. std::cout << "CONFIGURATION section not found in config file" << std::endl;
  499. }
  500. }
  501. catch (...)
  502. {
  503. //mLog::FERROR("Read config file error {$}", filepath);
  504. std::cout << "Error: Read config file error: " << filepath << std::endl;
  505. }
  506. }
  507. if (ret)
  508. {
  509. //这里检查模型对象的配置项是否存在,如果是走模型配置,在安装的时候,会自动创建一份修改后的配置文件
  510. //这里可以直接检查到
  511. std::cout << "Calling m_NewDevManager->DriverEntry with path: " << filepath << std::endl;
  512. //mLog::FINFO("m_NewDevManager->DriverEntry {$}", filepath);
  513. ret = m_NewDevManager->DriverEntry(filepath);
  514. std::cout << "m_NewDevManager->DriverEntry returned: " << (ret ? "true" : "false") << std::endl;
  515. //mLog::FINFO("m_NewDevManager->Prepare ");
  516. std::cout << "Calling m_NewDevManager->Prepare" << std::endl;
  517. m_NewDevManager->Prepare();
  518. std::cout << "m_NewDevManager->Prepare completed" << std::endl;
  519. }
  520. else
  521. {
  522. std::cout << "Error: LogicDriver::DriverEntry Failed. Path: " << filepath.c_str() << std::endl;
  523. //mLog::FERROR("LogicDriver::DriverEntry Failed.{$}", filepath.c_str());
  524. }
  525. std::cout << "Exiting NewModelDPC::DriverEntry with return value: " << (ret ? "true" : "false") << std::endl;
  526. return ret;
  527. }
  528. DWORD SYSTEM_CALL NewModelDPC::OnNotify(std::vector<std::shared_ptr<LinuxEvent>> evtList, DWORD count)
  529. {
  530. DWORD dwResult = LinuxEvent::WaitForMultipleEvents(evtList, 2500);
  531. if ((dwResult >= WAIT_OBJECT_0) && (dwResult < WAIT_OBJECT_0 + count - 1))
  532. {
  533. //mLog::FINFO("OnNotify Platform");
  534. return dwResult - WAIT_OBJECT_0;
  535. }
  536. else
  537. {
  538. if (NULL != m_NewDevManager)
  539. {
  540. if (!m_NewDevManager->isConnected())
  541. {
  542. //mLog::FWARN("CONNECTED is false");
  543. if (*m_pConnectionStatus)
  544. {
  545. //mLog::FWARN("Notify disCONNECTED ");
  546. LogicDriver::DisConnect();
  547. }
  548. if (!*m_pConnectionStatus)
  549. {
  550. //mLog::FINFO("Try CONNECT");
  551. if (m_NewDevManager->Connect())
  552. {
  553. //mLog::FINFO("try CONNECTED is ok");
  554. LogicDriver::Connect();
  555. //mLog::FINFO("Notify CONNECTED is ok");
  556. }
  557. else
  558. {
  559. //mLog::FWARN("Notify disCONNECTED ");
  560. LogicDriver::DisConnect();
  561. //mLog::FWARN("Try CONNECT failed. ");
  562. }
  563. }
  564. }
  565. else
  566. {
  567. if (!*m_pConnectionStatus)
  568. {
  569. //mLog::FINFO("Auto CONNECTED is ok");
  570. LogicDriver::Connect();
  571. //mLog::FINFO("Notify CONNECTED is ok");
  572. }
  573. }
  574. //此处从程序启动后会一直打印,重复调用work函数
  575. ////mLog::FINFO("ImageSaveDev->work()");
  576. //((ImageSaveDev*)m_pImgSaveDev)->Work();
  577. }
  578. }
  579. return 1;
  580. }
  581. RET_STATUS NewModelDPC::OnAction(const char* pszActionName, const char* pszParams, ResDataObject& resResponse)
  582. {
  583. RET_STATUS ret = ModuleDriver::OnAction(pszActionName, pszParams, resResponse);
  584. if (ret != RET_NOSUPPORT)
  585. {
  586. //成功或者失败,
  587. return ret;
  588. }
  589. string action = pszActionName;
  590. if (action == "InstallVirutalDevice")
  591. {
  592. //安装虚拟设备
  593. /// "ABS": {
  594. // "InstalHost":"Detector",
  595. // "ImgeProcessDevice" : "ABS\\ABSX64.dll"
  596. // },
  597. // "DAP": {
  598. // "MessageProcessDevice": "DAP\\DAPX64.dll"
  599. // }
  600. //
  601. //找到配置项,检查文件
  602. //ResDataObject resTest,resdll;
  603. //resdll.add("ImgeProcessDevice", "ABS\\ABSX64.dll");
  604. //resTest.add("ABS", resdll);
  605. ResDataObject resConf;
  606. string fileName = GetDriverConfigFilePath();
  607. //mLog::FINFO("Try Load xml Config file {$} ", fileName);
  608. resConf.loadFile(fileName.c_str());
  609. //string test = resTest.encode();
  610. if (resConf.GetKeyCount("CONFIGURATION") <= 0)
  611. {
  612. //mLog::FERROR("config file {$} content error", fileName);
  613. ret = RET_FAILED;
  614. return ret;
  615. }
  616. //resConf = resConf["CONFIGURATION"];
  617. ResDataObject resVirtual;
  618. resVirtual.decode(pszParams);
  619. bool changed = false;
  620. string strServDevPath;
  621. for (int vdIdx = 0; vdIdx < resVirtual.size(); vdIdx++)
  622. {
  623. bool got = false;
  624. string vdModuleName;
  625. //DTS/ABS/...
  626. if (strcmp(resVirtual.GetKey(vdIdx), "RespTopic") == 0)
  627. continue;
  628. if (resVirtual[vdIdx].GetKeyCount("ServDevPath") > 0)
  629. strServDevPath = (const char*)resVirtual[vdIdx]["ServDevPath"];
  630. else
  631. strServDevPath = "";
  632. for (int cfIdx = 0; cfIdx < resVirtual[vdIdx].size(); cfIdx++)
  633. {
  634. string va = (const char*)resVirtual[vdIdx][cfIdx];
  635. if (va.length() > 4 && va.substr(va.length() - 4, 4) == ".dll")
  636. {
  637. string type = resVirtual[vdIdx].GetKey(cfIdx);
  638. vdModuleName = va;
  639. int idx = resConf["CONFIGURATION"].GetFirstOf(type.c_str());
  640. while (idx != -1)
  641. {
  642. string confV = resConf["CONFIGURATION"][idx].encode();
  643. if (confV == va)
  644. {
  645. got = true;
  646. break;
  647. }
  648. idx = resConf["CONFIGURATION"].GetNextOf(type.c_str(), idx);
  649. }
  650. if (!got)
  651. {
  652. resConf["CONFIGURATION"].add(type.c_str(), va.c_str());
  653. changed = true;
  654. break;
  655. }
  656. }
  657. }
  658. if (resVirtual[vdIdx].GetKeyCount("ServDevPath") > 0)
  659. {
  660. }
  661. if (got || changed)
  662. {
  663. //新装
  664. string newdev = "CCOS/DEVICE/" + string(resVirtual.GetKey(vdIdx));
  665. string devPath = newdev;
  666. devPath += "/" + string((const char*)m_resHardwareInfo["VendorID"]);// , (const char*)Configuration["VendorID"]);
  667. devPath += "/" + string((const char*)m_resHardwareInfo["ProductID"]);//, (const char*)Configuration["ProductID"]);
  668. devPath += "/" + string((const char*)m_resHardwareInfo["SerialID"]);//, (const char*)Configuration["SerialID"]);
  669. //mLog::FINFO("New Virtual Device Path {$}", devPath);
  670. resResponse.add(strServDevPath.c_str(), "");
  671. if (strServDevPath.length() <= 0)
  672. strServDevPath = devPath;
  673. if (!got)
  674. {
  675. //mLog::FINFO("New install VD {$]", strServDevPath);
  676. //之前没有配置,则需要测试,并设置
  677. LoadVirtualDevice(vdModuleName.c_str(), false, false, devPath, strServDevPath);
  678. }
  679. else
  680. {
  681. //mLog::FINFO("VD had installed aready {$]", strServDevPath);
  682. for(int x=0;x<m_arrImgeProcessDevices.size();x++)
  683. if (m_arrImgeProcessDevices[x]->GetCcosRootPath() == strServDevPath)
  684. {
  685. //
  686. //mLog::FINFO("VD {$} had installed aready type ImageProcessDevices with idx {$]", strServDevPath, x);
  687. resResponse.update(strServDevPath.c_str(), "ON");
  688. }
  689. for (int x = 0; x < m_arrMessageProcessDevices.size(); x++)
  690. if (m_arrMessageProcessDevices[x]->GetCcosRootPath() == strServDevPath)
  691. {
  692. //
  693. //mLog::FINFO("VD {$} had installed aready type MessageProcessDevices with idx {$]", strServDevPath, x);
  694. resResponse.update(strServDevPath.c_str(), "ON");
  695. }
  696. if(strlen(resResponse[strServDevPath.c_str()]) == 0)
  697. resResponse.update(strServDevPath.c_str(), "OFF");
  698. }
  699. }
  700. }
  701. if (changed)
  702. {
  703. //ResDataObject save;
  704. //save.add("CONFIGURATION", resConf);
  705. resConf.SaveFile(fileName.c_str());
  706. }
  707. ret = RET_SUCCEED;
  708. //resResponse = "Install ok.";
  709. }
  710. else if (action == "restart")
  711. {
  712. resResponse = "reStart ok.";
  713. ret = RET_SUCCEED;
  714. exit(1);
  715. }
  716. return ret;
  717. }
  718. bool NewModelDPC::GetDeviceType(GUID &DevType)
  719. {
  720. std::string DriverType = m_NewDevManager->GetGUID();
  721. if (DriverType.size() == 0)
  722. {
  723. //mLog::FERROR("GetDeviceType Failed");
  724. return false;
  725. }
  726. //if (m_pNewDevManager->GetDriverType(DriverType) == false)
  727. //{
  728. // return false;
  729. //}
  730. return string_2_guid(DriverType.c_str(), DevType);
  731. }
  732. bool SYSTEM_CALL NewModelDPC::Driver_Probe(ResDataObject& PARAM_OUT HardwareInfo)
  733. {
  734. //string DriverInfo = m_NewDevManager->DriverProbe();
  735. //mLog::FINFO("Driver : DriverInfo {$}", m_resHardwareInfo.encode());
  736. //if (m_pNewDevManager->Driver_Probe(DriverInfo))
  737. {
  738. ResDataObject Config = m_resHardwareInfo;
  739. HardwareInfo = m_resHardwareInfo;
  740. //if (Config.decode(DriverInfo.c_str()))
  741. {
  742. //HardwareInfo.add("MajorID", (const char*)Config["MajorID"]);
  743. //HardwareInfo.add("MinorID", (const char*)Config["MinorID"]);
  744. //HardwareInfo.add("VendorID", (const char*)Config["VendorID"]);
  745. //HardwareInfo.add("ProductID", (const char*)Config["ProductID"]);
  746. //HardwareInfo.add("SerialID", (const char*)Config["SerialID"]);
  747. ostringstream os,os2;
  748. os << (const char*)Config["VendorID"] << "_" << (const char*)Config["ProductID"] << "_" << (const char*)Config["SerialID"];
  749. DEVICE_ID = os.str();
  750. os2 << "" << (const char*)Config["MajorID"] ;//<< "/" << (const char*)Config["VendorID"] << "/" << (const char*)Config["ProductID"];
  751. os2 << "/" << (const char*)Config["SerialID"] << "/";
  752. TOPIC_PREFIX = os2.str();
  753. return true;
  754. }
  755. //else
  756. //{
  757. // //mLog::FERROR("Driver_Probe Failed");
  758. //}
  759. }
  760. return false;
  761. }
  762. RET_STATUS NewModelDPC::GetDeviceResource(ResDataObject *pDeviceResource)
  763. {
  764. RET_STATUS ret = RET_FAILED;
  765. ResDataObject BaseLayerConfig;
  766. if ((*m_config).GetPropties().size() > 0)
  767. {
  768. ModuleDriver::GetDeviceResource(pDeviceResource);
  769. //mLog::FINFO("ModuleDriver : GetDeviceResource {$}", pDeviceResource->encode());
  770. //BaseLayerConfig = *pDeviceResource;
  771. }
  772. std::string ResInfo;// = m_NewDevManager->GetResource();
  773. //mLog::FINFO("IO Driver : GetDeviceResource {$}", ResInfo);
  774. //if (m_pNewDevManager->GetDriverResource(ResInfo))
  775. {
  776. //low layerInfo
  777. ResDataObject LowLayerConfig;
  778. if ((*m_config).GetPropties().size() <= 0)
  779. {
  780. ResInfo = m_NewDevManager->GetResource();
  781. if (LowLayerConfig.decode(ResInfo.c_str()) == false)
  782. {
  783. //mLog::FERROR("GetDriverResource Failed");
  784. return ret;
  785. }
  786. ResDataObject BaseLayerConfig;
  787. ret = LogicDriver::GetDeviceResource(&BaseLayerConfig);
  788. if (ret < RET_SUCCEED)
  789. {
  790. return ret;
  791. }
  792. }
  793. //base LogicDriverinfo
  794. if (BaseLayerConfig.size() <= 0)
  795. {
  796. ret = LogicDriver::GetDeviceResource(&BaseLayerConfig);
  797. //mLog::FINFO("LogicDriver : GetDeviceResource {$}", BaseLayerConfig.encode());
  798. if (ret < RET_SUCCEED)
  799. {
  800. return ret;
  801. }
  802. }
  803. if (LowLayerConfig.GetFirstOf("Attribute") >= 0)
  804. {
  805. //loop Low Layer Attribute
  806. for (size_t i = 0; i < LowLayerConfig["Attribute"].size(); i++)
  807. {
  808. const char *pKey = LowLayerConfig["Attribute"].GetKey(i);
  809. if (pKey)
  810. {
  811. int BaseIdx = BaseLayerConfig["Attribute"].GetFirstOf(pKey);
  812. if (BaseIdx >= 0)
  813. {
  814. //exist then overwrite
  815. BaseLayerConfig["Attribute"][BaseIdx] = LowLayerConfig["Attribute"][i];
  816. }
  817. else
  818. {
  819. //not exist then add
  820. BaseLayerConfig["Attribute"].add(pKey, LowLayerConfig["Attribute"][i]);
  821. }
  822. }
  823. }
  824. }
  825. if (LowLayerConfig.GetFirstOf("Action") >= 0)
  826. {
  827. //loop Low Layer Action
  828. for (size_t i = 0; i < LowLayerConfig["Action"].size(); i++)
  829. {
  830. const char *pKey = LowLayerConfig["Action"].GetKey(i);
  831. if (pKey)
  832. {
  833. int BaseIdx = BaseLayerConfig["Action"].GetFirstOf(pKey);
  834. if (BaseIdx >= 0)
  835. {
  836. //exist then overwrite
  837. BaseLayerConfig["Action"][BaseIdx] = LowLayerConfig["Action"][i];
  838. }
  839. else
  840. {
  841. //not exist then add
  842. BaseLayerConfig["Action"].add(pKey, LowLayerConfig["Action"][i]);
  843. }
  844. }
  845. }
  846. }
  847. (*pDeviceResource) = BaseLayerConfig;
  848. //mLog::FINFO("GetDeviceResource Result {$}", BaseLayerConfig.encode());
  849. return RET_SUCCEED;
  850. }
  851. return ret;
  852. }
  853. /*
  854. void NewModelDPC::SubscribeSelf() {
  855. //这里订阅topic
  856. }*/
  857. bool NewModelDPC::Device_Probe(ResDataObject &HardwareInfo)
  858. {
  859. //string DeviceInfo = m_NewDevManager->DeviceProbe();
  860. //mLog::FINFO("Driver : Device_Probe {$}", m_resHardwareInfo.encode());
  861. //if (m_pNewDevManager->Device_Probe(DeviceInfo))
  862. {
  863. //ResDataObject Config;
  864. //if (Config.decode(DeviceInfo.c_str()))
  865. {
  866. //HardwareInfo.add("MajorID", (const char*)Config["MajorID"]);
  867. //HardwareInfo.add("MinorID", (const char*)Config["MinorID"]);
  868. //HardwareInfo.add("VendorID", (const char*)Config["VendorID"]);
  869. //HardwareInfo.add("ProductID", (const char*)Config["ProductID"]);
  870. //HardwareInfo.add("SerialID", (const char*)Config["SerialID"]);
  871. HardwareInfo = m_resHardwareInfo;
  872. return true;
  873. }
  874. //else
  875. //{
  876. // //mLog::FERROR("Device_Probe Failed");
  877. //}
  878. }
  879. return false;
  880. }
  881. bool NewModelDPC::SetDeviceWorkPath(const char *pWorkPath)
  882. {
  883. return true;
  884. }
  885. bool NewModelDPC::SetDriverWorkPath(const char *pWorkPath)
  886. {
  887. (*m_pWorkpath) = pWorkPath;
  888. //要把读取单一硬件属性的过程放到此处
  889. return true;
  890. }
  891. bool NewModelDPC::Connect()
  892. {
  893. bool bret = m_NewDevManager->Connect();
  894. if (bret)
  895. {
  896. LogicDriver::Connect();//make sure it's connected
  897. //mLog::FINFO("Connect Succeed");
  898. return true;
  899. }
  900. else
  901. {
  902. //mLog::FERROR("Connect Failed");
  903. }
  904. return false;
  905. }
  906. bool SYSTEM_CALL NewModelDPC::GetConnectionStatus()
  907. {
  908. return m_NewDevManager->isConnected();
  909. }
  910. void NewModelDPC::DisConnect()
  911. {
  912. m_NewDevManager->Disconnect();
  913. LogicDriver::DisConnect();//make sure it's Disconnected
  914. //mLog::FINFO("DisConnect Succeed");
  915. }
  916. bool SYSTEM_CALL NewModelDPC::OnHeartBeat()
  917. {
  918. return m_NewDevManager->OnHeartBeat();
  919. }
  920. std::string getCurrentTime() {
  921. auto now = std::chrono::system_clock::now();
  922. std::time_t now_time = std::chrono::system_clock::to_time_t(now);
  923. std::tm* local_time = std::localtime(&now_time);
  924. char time_str[20];
  925. std::strftime(time_str, sizeof(time_str), "%Y-%m-%d %H:%M:%S", local_time);
  926. return std::string(time_str);
  927. }
  928. void* ImgeProcessThread(void* pPara)
  929. {
  930. std::cout << "[" << getCurrentTime() << "] ImgeProcessThread: Thread started. pPara = " << pPara << std::endl;
  931. intptr_t nIndex = reinterpret_cast<intptr_t>(pPara);
  932. std::cout << "[" << getCurrentTime() << "] ImgeProcessThread: nIndex = " << nIndex << std::endl;
  933. if (nIndex < 0 || nIndex >= GetHDataReadyEvent().size()) {
  934. std::cout << "[" << getCurrentTime() << "] ERROR: ImgeProcessThread: nIndex is negative (" << nIndex << "), thread exit." << std::endl;
  935. return nullptr;
  936. }
  937. imgFuncThreadParams* pParam = GetParamImgThread().data() + nIndex;
  938. std::vector<std::shared_ptr<LinuxEvent>> hWait;
  939. if (!GetHDataReadyEvent()[nIndex]) {
  940. std::cout << "[" << getCurrentTime() << "] ERROR: ImgeProcessThread: GetHDataReadyEvent()[" << nIndex << "] is null, thread exit." << std::endl;
  941. return nullptr;
  942. }
  943. hWait.push_back(GetHDataReadyEvent()[nIndex]); // 第一个事件
  944. hWait.push_back(g_hExit);
  945. while (true)
  946. {
  947. DWORD dwWait = LinuxEvent::WaitForMultipleEvents(hWait, 1000);
  948. if (dwWait == WAIT_OBJECT_0)
  949. {
  950. //有图像
  951. int nNum = std::get<0>(*pParam);
  952. ImageProcessAPI* func = std::get<1>(*pParam);
  953. LogicDevice** pDev = std::get<8>(*pParam);
  954. string Head = std::get<3>(*pParam);
  955. string context = std::get<4>(*pParam);
  956. GetHDataReaded()[nIndex]->SetEvent();
  957. if (func != nullptr)
  958. {
  959. //string temp = std::get<3>(*pParam);
  960. for (int x = 0; x < nNum; x++)
  961. {
  962. if (func[x] == nullptr)
  963. {
  964. //mLog::FERROR("Thread[{$}] ImageProcessFunc is null with devpath= {$}", nIndex, pDev[x]->GetCcosRootPath());
  965. continue;
  966. }
  967. //mLog::FINFO("Thread[{$}] start ImageProcess {$} from {$} with devpath= {$}", nIndex, Head, std::get<7>(*pParam)->GetCcosRootPath(), pDev[x]->GetCcosRootPath());
  968. 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]);
  969. //mLog::FINFO("Thread[{$}] ImageProcessFunc Over with devpath= {$}", nIndex, pDev[x]->GetCcosRootPath());
  970. }
  971. }
  972. else
  973. {
  974. //mLog::FERROR("Thread[{$}] ImageProcessFunc arrpoint is null ", nIndex);
  975. }
  976. GetHIdleEvent()[nIndex]->SetEvent();
  977. }
  978. else if(dwWait == WAIT_OBJECT_0 + 1)
  979. {
  980. //mLog::FINFO("ImageThread {$} Got exit", nIndex);
  981. }
  982. }
  983. //delete pParam;
  984. //std::get<1>(*pParam) = Type;
  985. //std::get<2>(*pParam) = (char*)imageHead.c_str();
  986. //std::get<3>(*pParam) = (char*)resContext.encode();
  987. //std::get<4>(*pParam) = pFrameBuff;
  988. //std::get<5>(*pParam) = FrameSize;
  989. //std::get<6>(*pParam) = pDeviceFrom;
  990. //std::get<7>(*pParam) = m_arrImgeProcessDevices[x];
  991. return 0;
  992. }
  993. /// <summary>
  994. ///
  995. /// </summary>
  996. /// <param name="Type"></param>
  997. /// <param name="imageHead"></param>
  998. /// <param name="pFrameBuff"></param>
  999. /// <param name="FrameSize"></param>
  1000. /// <param name="pDeviceFrom"></param>
  1001. /// <returns></returns>
  1002. int NewModelDPC::ImageRrocess(int Type, string& imageHead, ResDataObject& resContext, char* pFrameBuff, DWORD FrameSize, NewModelDevice* pDeviceFrom)
  1003. {
  1004. //mLog::FINFO("Begin");
  1005. //std::vector<HANDLE> threads;
  1006. if (m_arrImgeFunc.size() <= 0)
  1007. return 0;
  1008. char* context = (char*)resContext.encode();
  1009. char* header = (char*)imageHead.c_str();
  1010. //HANDLE thread = INVALID_HANDLE_VALUE;
  1011. //for (int x = 0; x < m_arrImgeFunc.size(); x++)
  1012. {
  1013. DWORD dwWait = LinuxEvent::WaitForMultipleEvents(GetHIdleEvent(), -1);
  1014. //mLog::FINFO("Wait for ImageProcessThrad Idle return {$}", dwWait);
  1015. if (dwWait != WAIT_TIMEOUT && dwWait >= WAIT_OBJECT_0 && dwWait < WAIT_OBJECT_0 + MAX_IMG_THREAD)
  1016. {
  1017. imgFuncThreadParams* pParam = GetParamImgThread().data() + (dwWait - WAIT_OBJECT_0);
  1018. std::get<0>(*pParam) = m_arrImgeFunc.size();
  1019. std::get<1>(*pParam) = m_arrImgeFunc.data();
  1020. std::get<2>(*pParam) = Type;
  1021. std::get<3>(*pParam) = header;
  1022. std::get<4>(*pParam) = context;
  1023. std::get<5>(*pParam) = pFrameBuff;
  1024. std::get<6>(*pParam) = FrameSize;
  1025. std::get<7>(*pParam) = pDeviceFrom;
  1026. std::get<8>(*pParam) = (LogicDevice**)m_arrImgeProcessDevices.data();
  1027. //mLog::FINFO("Notify ImageProcessThread [{$}] work ", dwWait - WAIT_OBJECT_0);
  1028. GetHDataReadyEvent()[dwWait - WAIT_OBJECT_0]->SetEvent();
  1029. //等待线程 读取完数据 ,就可以下一个
  1030. //mLog::FINFO("Start Wait {$} Thread to Read Data.", m_arrImgeFunc.size());
  1031. DWORD dwRet = GetHDataReaded()[dwWait - WAIT_OBJECT_0]->Wait(4000);
  1032. //if (dwRet == WAIT_OBJECT_0)
  1033. //mLog::FINFO("Over Succeded with ret {$}", dwRet);
  1034. //else
  1035. //mLog::FERROR("Over Timeout with ret {$}", dwRet);
  1036. }
  1037. else
  1038. {
  1039. //mLog::FERROR("No Process this Iamge with ret {$}", dwWait);
  1040. }
  1041. }
  1042. //if (m_arrImgeFunc != nullptr)
  1043. // return m_arrImgeFunc(Type, imageHead, (char*)resContext.encode(), pFrameBuff, FrameSize, pDeviceFrom, m_arrImgeProcessDevices);
  1044. return m_arrImgeFunc.size();
  1045. }
  1046. /// <summary>
  1047. ///
  1048. /// </summary>
  1049. /// <param name="resNotify"></param>
  1050. /// <param name="pDeviceFrom"></param>
  1051. /// <returns></returns>
  1052. int NewModelDPC::NotifyMessageProcess(ResDataObject& resNotify, NewModelDevice* pDeviceFrom)
  1053. {
  1054. //mLog::FINFO("Begin");
  1055. ResDataObject resResp;
  1056. RET_STATUS ret = RET_FAILED;
  1057. //if (m_arrMessageProcessDevices != nullptr)
  1058. for(int x=0;x< m_arrMessageProcessDevices.size();x++)
  1059. {
  1060. ret = m_arrMessageProcessDevices[x]->Request(&resNotify, &resResp);
  1061. }
  1062. //if (m_arrImgeProcessDevices != nullptr)
  1063. for(int x=0;x< m_arrImgeProcessDevices.size();x++)
  1064. {
  1065. ret = m_arrImgeProcessDevices[x]->Request(&resNotify, &resResp);
  1066. }
  1067. //mLog::FINFO("End");
  1068. return ret ;
  1069. }
  1070. void InitLogicDevice(LogicDevice* p, string guidname)
  1071. {
  1072. }
  1073. PVOID NewModelDPC::LoadLogicDevices()
  1074. {
  1075. std::cout << "**************** NewModelDPC :: LoadLogicDevices" << std::endl;
  1076. //mLog::FINFO( "Load Logic Devices");
  1077. int nDeviceNumber = 1;
  1078. string filepath = GetDriverConfigFilePath();
  1079. ResDataObject Config;
  1080. string imgLogicDevice, noImgLogicDevice;
  1081. std::cout << "**************** NewModelDPC Load Config " << filepath << std::endl;
  1082. if (Config.loadFile(filepath.c_str()))
  1083. {
  1084. int nHaveDeviceNumber = (int)Config["CONFIGURATION"].GetKeyCount("DeviceNumber");
  1085. if (nHaveDeviceNumber > 0)
  1086. {
  1087. nDeviceNumber = Config["CONFIGURATION"]["DeviceNumber"];
  1088. //mLog::FINFO("Have {$} Devices", nDeviceNumber);
  1089. }
  1090. else
  1091. {
  1092. //mLog::FWARN("Have no DeviceNumber Attribute");
  1093. }
  1094. int nHaveImgLogicDevice = (int)Config["CONFIGURATION"].GetKeyCount("ImgeProcessDevice");
  1095. if (nHaveImgLogicDevice > 0)
  1096. {
  1097. int first = Config["CONFIGURATION"].GetFirstOf("ImgeProcessDevice");
  1098. while (first != -1)
  1099. {
  1100. imgLogicDevice = (const char*)Config["CONFIGURATION"][first];
  1101. if (imgLogicDevice.length() > 0)
  1102. {
  1103. //图像处理逻辑设备
  1104. LoadVirtualDevice(imgLogicDevice.c_str(), true);
  1105. }
  1106. first = Config["CONFIGURATION"].GetNextOf("ImgeProcessDevice", first);
  1107. }
  1108. }
  1109. int nHaveNoneImgLogicDevice = (int)Config["CONFIGURATION"].GetKeyCount("MessageProcessDevice");
  1110. if (nHaveNoneImgLogicDevice > 0)
  1111. {
  1112. int first = Config["CONFIGURATION"].GetFirstOf("MessageProcessDevice");
  1113. while (first != -1)
  1114. {
  1115. imgLogicDevice = (const char*)Config["CONFIGURATION"][first];
  1116. if (imgLogicDevice.length() > 0)
  1117. {
  1118. //图像处理逻辑设备
  1119. LoadVirtualDevice(imgLogicDevice.c_str(), false);
  1120. }
  1121. first = Config["CONFIGURATION"].GetNextOf("MessageProcessDevice", first);
  1122. }
  1123. //noImgLogicDevice = (const char*)Config["CONFIGURATION"]["MessageProcessDevice"];
  1124. }
  1125. }
  1126. else
  1127. {
  1128. //mLog::FERROR("Load config file failed {$}",filepath.c_str());
  1129. }
  1130. DevTree *pTree = (DevTree *)GetDeviceTree();
  1131. //imgLogicDevice "VirtualDevice/ABS/ABSX64.dll"
  1132. //m_arrImgeProcessDevices = nullptr;
  1133. //if (imgLogicDevice.length() > 0)
  1134. //{
  1135. // //图像处理逻辑设备
  1136. // LoadVirtualDevice(imgLogicDevice.c_str(), true);
  1137. //}
  1138. //noImgLogicDevice "VirtualDevice/AGD/AGDX64.dll"
  1139. //m_arrMessageProcessDevices = nullptr;
  1140. //if (noImgLogicDevice.length() > 0)
  1141. //{
  1142. // //非图像处理设备
  1143. // LoadVirtualDevice(noImgLogicDevice.c_str());
  1144. //}
  1145. for (int i = 0; i < nDeviceNumber; i++)
  1146. {
  1147. NewModelDevice *p = new NewModelDevice();
  1148. std::cout << " ****************NewModelDPC Try new Device " << i << " of " << nDeviceNumber << std::endl;
  1149. auto pIoDevice = m_NewDevManager->CreateDevice(i);
  1150. if (pIoDevice)
  1151. {
  1152. GUID guid2;
  1153. string guidname;
  1154. //((LogicDevice*)pChild)->GetDeviceType(guid);
  1155. //bool bget = pIoDevice->GetDeviceType(guid2);
  1156. //guidname = pIoDevice->GetGUID();
  1157. //guid_2_string(guid2, guidname);
  1158. ResDataObject subpath;
  1159. subpath = (guidname).c_str();
  1160. ResDataObject probe,conn_usless;
  1161. Device_Probe(probe);
  1162. //此时知道宿主设备是谁,如果Generator/Detector
  1163. //
  1164. string hostName;
  1165. bool isMain = false;
  1166. if (probe.GetKeyCount("MajorID") > 0)
  1167. {
  1168. //找到宿主
  1169. hostName = "CCOS/DEVICE/" + string((const char* )probe["MajorID"]);
  1170. }
  1171. string fullpath = MakeDevicePath(probe, conn_usless, subpath).encode();
  1172. string ccospath = MakeCcosPath(probe, conn_usless, subpath, false).encode();
  1173. if (hostName.length() > 0)
  1174. {
  1175. if (ccospath.substr(0, hostName.length()) == hostName)
  1176. {
  1177. //宿主设备
  1178. isMain = true;
  1179. //mLog::FINFO("Got MainHost {$} of {$}", hostName, ccospath);
  1180. }
  1181. }
  1182. if (isMain && m_MainHostDevice == nullptr)
  1183. {
  1184. m_MainHostDevice = p;
  1185. //mLog::FINFO("Add ImageProcess Deivce : {$} ", m_arrImgeProcessDevices.size());
  1186. //嵌入式设备加载
  1187. for (int x = 0; x < m_arrImgeProcessDevices.size(); x++)
  1188. {
  1189. GUID guid2;
  1190. string guidname;
  1191. m_arrImgeProcessDevices[x]->GetDeviceType(guid2);
  1192. guid_2_string(guid2, guidname);
  1193. ResDataObject subpath;
  1194. subpath = (guidname).c_str();
  1195. ResDataObject probe, conn_usless;
  1196. Device_Probe(probe);
  1197. string fullpatht = MakeDevicePath(probe, conn_usless, subpath).encode();
  1198. string ccospatht = MakeCcosPath(probe, conn_usless, subpath, false).encode();
  1199. m_arrImgeProcessDevices[x]->SetClientRootID(fullpatht.c_str(), ccospatht.c_str());
  1200. m_arrImgeProcessDevices[x]->CompleteInit();
  1201. pTree->Add((PVOID)(m_arrImgeProcessDevices[x]), TYPE_DEVICE);
  1202. //mLog::FINFO("Try add ccos child {$} ", ccospatht);
  1203. m_MainHostDevice->AddCcosChildren(m_arrImgeProcessDevices[x], ccospatht.c_str());
  1204. }
  1205. //mLog::FINFO("Add MessageProcess Deivce : {$} ", m_arrImgeProcessDevices.size());
  1206. //if (m_arrMessageProcessDevices != nullptr)
  1207. for (int x = 0; x < m_arrMessageProcessDevices.size(); x++)
  1208. {
  1209. GUID guid2;
  1210. string guidname;
  1211. m_arrMessageProcessDevices[x]->GetDeviceType(guid2);
  1212. guid_2_string(guid2, guidname);
  1213. ResDataObject subpath;
  1214. subpath = (guidname).c_str();
  1215. ResDataObject probe, conn_usless;
  1216. Device_Probe(probe);
  1217. string fullpatht = MakeDevicePath(probe, conn_usless, subpath).encode();
  1218. string ccospatht = MakeCcosPath(probe, conn_usless, subpath, false).encode();
  1219. m_arrMessageProcessDevices[x]->SetClientRootID(fullpatht.c_str(), ccospatht.c_str());
  1220. m_arrMessageProcessDevices[x]->CompleteInit();
  1221. pTree->Add((PVOID)(m_arrMessageProcessDevices[x]), TYPE_DEVICE);
  1222. //mLog::FINFO("Try add ccos child {$} ", ccospatht);
  1223. m_MainHostDevice->AddCcosChildren(m_arrMessageProcessDevices[x], ccospatht.c_str());
  1224. }
  1225. }
  1226. //pDevice->SetEbusRoot(fullpath.c_str());
  1227. p->SetClientRootID(fullpath.c_str(), ccospath.c_str());
  1228. //mLog::FINFO(" SetClientRootID ", ccospath);
  1229. //p->SetClientRootID();
  1230. //mLog::FINFO("Get IoDevice Succeed");
  1231. p->SetDrvDPC(this);
  1232. std::shared_ptr<LinuxEvent> DisconnectEvt = GetPassiveDisConnectEvtHandle();
  1233. //p->ConnectMQTTServer();
  1234. p->Init(std::move(pIoDevice), DisconnectEvt);
  1235. //p->SubscribeAction();
  1236. //mLog::FINFO("Create IoDevice Succeed");
  1237. }
  1238. else
  1239. {
  1240. //mLog::FERROR("Create IoDevice Failed");
  1241. delete p;
  1242. return NULL;
  1243. }
  1244. //*/
  1245. ResDataObject resPri;
  1246. string strPri;
  1247. for (int x = 1; x < m_arrImgeProcessDevices.size(); x++)
  1248. {
  1249. for (int y = 0; y < x; y++)
  1250. {
  1251. unsigned int xPri, yPri;
  1252. xPri = yPri = -1;
  1253. if (m_arrImgeProcessDevices[x]->DevGet("", "ImageProcessIndex", resPri) == RET_SUCCEED)
  1254. {
  1255. strPri = (const char*)resPri;
  1256. if (strPri.length() > 0)
  1257. xPri = atoi(strPri.c_str());
  1258. //mLog::FINFO("Vd {$} ImageProcessIdex=[{$}]", m_arrImgeProcessDevices[x]->GetCcosRootPath(), strPri);
  1259. }
  1260. if (m_arrImgeProcessDevices[y]->DevGet("", "ImageProcessIndex", resPri) == RET_SUCCEED)
  1261. {
  1262. strPri = (const char*)resPri;
  1263. if (strPri.length() > 0)
  1264. xPri = atoi(strPri.c_str());
  1265. //mLog::FINFO("Vd {$} ImageProcessIdex=[{$}]", m_arrImgeProcessDevices[y]->GetCcosRootPath(), strPri);
  1266. }
  1267. if (xPri < yPri)
  1268. {
  1269. auto* pTemp = m_arrImgeProcessDevices[x];
  1270. auto fTemp = m_arrImgeFunc[x];
  1271. m_arrImgeProcessDevices[x] = m_arrImgeProcessDevices[y];
  1272. m_arrImgeProcessDevices[y] = pTemp;
  1273. m_arrImgeFunc[x] = m_arrImgeFunc[y];
  1274. m_arrImgeFunc[y] = fTemp;
  1275. //mLog::FINFO("Exchange Vd [{$}] <=> [{$}]", x, y);
  1276. }
  1277. }
  1278. }
  1279. //*/
  1280. LogicDevice *pret = (LogicDevice *)p;
  1281. pTree->Add((PVOID)pret, TYPE_DEVICE);
  1282. LogicDevice::AddEbusChildren(p, "");
  1283. }
  1284. return (PVOID)pTree;
  1285. }
  1286. void NewModelDPC::UnloadLogicDevices()
  1287. {
  1288. //mLog::FINFO( "UnLoad Logic Devices");
  1289. DevTree *pTree = (DevTree *)GetDeviceTree();
  1290. size_t Size = pTree->size();
  1291. for (size_t i = 0; i < Size; i++)
  1292. {
  1293. DevTreeNode node = (*pTree)[i];
  1294. if (node.m_NodeType == TYPE_DEVICE)
  1295. {
  1296. NewModelDevice *pdev = (NewModelDevice*)node.m_pObject;
  1297. delete pdev;
  1298. }
  1299. else
  1300. {
  1301. //tree dev
  1302. //do not copy this!!!!
  1303. //UnloadLogicDevices(node.m_pObject);
  1304. }
  1305. }
  1306. pTree->clear();
  1307. //mLog::FINFO( "UnLoad Over");
  1308. }
  1309. RET_STATUS DATA_ACTION NewModelDPC::GetDeviceConfig(ResDataObject PARAM_OUT* pDeviceConfig)
  1310. {
  1311. if ((*m_config).GetPropties().size() > 0)
  1312. {
  1313. //mLog::FERROR("GetDeviceConfig has module config do not return anything.");
  1314. return RET_FAILED;
  1315. }
  1316. //mLog::FINFO( "GetDeviceConfig start");
  1317. std::string strDeviceConfig;
  1318. if (m_NewDevManager->GetDeviceConfig(strDeviceConfig))
  1319. {
  1320. pDeviceConfig->decode(strDeviceConfig.c_str());
  1321. //mLog::FINFO( "GetDeviceConfig ok {$}", strDeviceConfig);
  1322. }
  1323. else
  1324. {
  1325. //mLog::FERROR( "GetDeviceConfig failed");
  1326. return RET_FAILED;
  1327. }
  1328. //mLog::FINFO( "GetDeviceConfig Over");
  1329. return RET_SUCCEED;
  1330. }
  1331. RET_STATUS DATA_ACTION NewModelDPC::SetDeviceConfig(ResDataObject PARAM_IN* DeviceConfig)
  1332. {
  1333. if ((*m_config).GetPropties().size() > 0)
  1334. {
  1335. return RET_FAILED;
  1336. }
  1337. //mLog::FINFO( "SetDeviceConfig start");
  1338. std::string strDeviceConfig = DeviceConfig->encode();
  1339. if (m_NewDevManager->SetDeviceConfig(strDeviceConfig))
  1340. {
  1341. }
  1342. else
  1343. {
  1344. return RET_FAILED;
  1345. }
  1346. //mLog::FINFO( "SetDeviceConfig Over");
  1347. return RET_SUCCEED;
  1348. }