NewModelDPC.cpp 44 KB

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