NewModelDPC.cpp 44 KB

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