NewModelDPC.cpp 44 KB

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