123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539 |
- // NewModelDPC.cpp : 定义 DLL 应用程序的导出函数。
- //
- #include "NewModelDPC.h"
- #include "DevTree.h"
- #include "NewModelDevice.h"
- #include "common_api.h"
- #include <iostream>
- #include <sstream>
- #include "LocalConfig.h"
- #include "ModuleConfig.h"
- #include <filesystem>
- #include <system_error>
- #include <pthread.h>
- #include <dlfcn.h> // 动态库加载
- #include <algorithm> // std::transform
- #include <cstring> // strerror
- #include <unistd.h> // 环境变量操作
- #include <cctype> // toupper
- NEWMODELDPC_C_API DriverDPC* GetDriverDPC()
- {
- return (DriverDPC*)(new NewModelDPC());
- }
- NEWMODELDPC_C_API void ReleaseDriverDPC(DriverDPC *p)
- {
- NewModelDPC *org = (NewModelDPC*)p;
- delete org;
- }
- typedef nsDEV::IODriver* (*GetDriverMgrAPI)();
- typedef VirtualDevice* (*CreateVirtuaDeviceAPI)();
- typedef void (*FreeVirtualDeviceAPI)(VirtualDevice*);
- //Log4CPP::Logger* mLog::gLogger = nullptr;
- std::shared_ptr<LinuxEvent> g_hExit;
- NewModelDPC::NewModelDPC(void)
- {
- m_pWorkpath = new std::string();
- m_MainHostDevice = nullptr;
- //m_pMidObject = 0;
- //m_pMidObject.reset(new eDEV::Mediator());
- //if (//mLog::gLogger == nullptr)
- //{
- // string strLogPath = GetProcessDirectory() + R"(\Conf\Log4CPP.Config.xml)";
- // string LogHost = ((string)getLogRootpath()).c_str();
- // Log4CPP::ThreadContext::Map::Set(ECOM::Utility::Hash("LogFileName"), "Platform");
- // //Log4CPP::GlobalContext::Map::Set("LogHost", LogHost.c_str());
- // Log4CPP::ThreadContext::Map::Set(ECOM::Utility::Hash("LogHost"), LogHost.c_str() + 1);
- // auto rc = Log4CPP::LogManager::LoadConfigFile(strLogPath.c_str());
- // //mLog::gLogger = Log4CPP::LogManager::GetLogger("Platform");
- // //mLog::FINFO("Code Build datetime [{$} {$}]", __DATE__, __TIME__);
- //}
- g_hExit = LinuxEvent::CreateEvent(LinuxEvent::AUTO_RESET,false);
- }
- NewModelDPC::~NewModelDPC(void)
- {
- delete m_pWorkpath;
- g_hExit->SetEvent();
- }
- bool NewModelDPC::LoadVirtualDevice(const char* pDevicePath, bool isImgDevice, bool bAddToList, std::string devPath, std::string devSrvPath)
- {
- std::string devDriverPath = GetProcessDirectory() + "/VirtualDevices/" + pDevicePath;
- std::string devCheck, devType = pDevicePath;
- devCheck = devType;
- std::transform(devType.begin(), devType.end(), devCheck.begin(), ::toupper);
- //if (devCheck == "GRID/GRIDX64.DLL" || devCheck == "GRID\\GRIDX64.DLL")
- //{
- // //mLog::FINFO("Try check Grid Device");
- // if (!DongleCheck("1028"))
- // {
- // //mLog::FWARN("VirtulGrid Dongle check failed...");
- // return false;
- // }
- // else
- // {
- // //mLog::FINFO("VirtulGrid Dongle check SUCC...");
- // }
- //}
- //else
- //{
- // //mLog::FINFO("Try check Virutal Device {$} == {$}", devType, devCheck);
- //}
- //if (devCheck == "DAP/DAPX64.DLL" || devCheck == "DAP\\DAPX64.DLL")
- //{
- // //mLog::FINFO("Try check DAP Device");
- // if (!DongleCheck("1038"))
- // {
- // //mLog::FWARN("Virtul DAP Dongle check failed...");
- // return false;
- // }
- // else
- // {
- // //mLog::FINFO("Virtul DAP Dongle check SUCC...");
- // }
- //}
- //else
- //{
- // //mLog::FINFO("Try check Virutal Device {$} == {$}", devType, devCheck);
- //}
- std::string FilePath = GetFileDirectory(devDriverPath);
- AddEnvPath(FilePath.c_str());
- void* hModle = dlopen(devDriverPath.c_str(), RTLD_LAZY);
- if (!hModle) {
- const char* err = dlerror();
- //mLog::FERROR("Load lib:{$} failed. Err:{$}", devDriverPath, err);
- return false;
- }
- //m_arrImgeFunc = nullptr;
- typedef VirtualDevice* (*CreateVirtuaDeviceAPI)();
- //mLog::FINFO("Load Driver ok.. {$}", devDeriverPath);
- CreateVirtuaDeviceAPI callfunc = (CreateVirtuaDeviceAPI)dlsym(hModle, "CreateVirtualDevice");
- if (callfunc)
- {
- VirtualDevice* pDevice = callfunc();
- if (pDevice)
- {
- int nIndex = -1;
- if (isImgDevice)
- {
- if (bAddToList)
- {
- //"ImageProcessIndex":
- // "DescKey":"ImageProcessIndex",
- // "Value" : "2"
- /*ResDataObject resPri;
- if (RET_SUCCEED == pDevice->DevGet("", "ImageProcessIndex", resPri))
- {
- string strPri = (const char*)resPri;
- if (strPri.length() > 0)
- {
- resPri = "";
- int nPri = atoi(strPri.c_str());
- for (int x = 0; x < m_arrImgeProcessDevices.size(); x++)
- {
- if (m_arrImgeProcessDevices[x]->DevGet("", "ImageProcessIndex", resPri) == RET_SUCCEED)
- {
- strPri = (const char*)resPri;
- if (atoi(strPri.c_str()) < nPri)
- {
- nIndex = x;
- continue;
- }
- else
- break;
- }
- }
- }
- }*/
- ImageProcessAPI func = (ImageProcessAPI)dlsym(hModle, "DeviceImgProcess");
- if (nIndex >= 0)
- {
- //mLog::FINFO("Inesrt {$} at [{$}] ImageProcess Array", pDevice->GetCcosRootPath(), nIndex);
- m_arrImgeProcessDevices.insert(m_arrImgeProcessDevices.begin() + nIndex, pDevice);
- m_arrImgeFunc.insert(m_arrImgeFunc.begin() + nIndex, func);
- }
- else
- {
- //mLog::FINFO("Append {$} ImageProcess Array at [{$}] ", pDevice->GetCcosRootPath(), m_arrImgeFunc.size());
- m_arrImgeProcessDevices.push_back(pDevice);
- m_arrImgeFunc.push_back(func);
- }
- }
- m_hImgeDeviceHandle = hModle;
- }
- else
- {
- if (bAddToList)
- {
- m_arrMessageProcessDevices.push_back(pDevice);
- m_hMessageDeviceHandle = hModle;
- }
- }
- if (!bAddToList)
- {
- //mLog::FINFO("Only Init Module from Template ");
- FreeVirtualDeviceAPI freeFunc = (FreeVirtualDeviceAPI)dlsym(hModle, "ReleaseVirtualDevice");
- if (freeFunc && pDevice)
- {
- //创建对象,只是模型文件操作
- //mLog::FINFO(" SetDeviceInfo {$} devSrvPath: {$} ",devPath, devSrvPath);
- pDevice->SetDeviceInfo(devPath, devSrvPath);
- //mLog::FINFO("First Install Test and check Module Begin ");
- pDevice->SetClientRootID("", devSrvPath.c_str());
- //mLog::FINFO("Test and check Module End ");
- freeFunc(pDevice);
- }
- }
- //m_NewDevManager.reset(pDriver);
- //succeed
- return true;
- }
- else
- {
- // mLog::FERROR("CreateVirtualDevice returned nullptr");
- dlclose(hModle);
- return false;
- }
- }
- else
- {
- char* err = dlerror();
- // mLog::FERROR("dlsym failed: {}", err);
- dlclose(hModle);
- return false;
- }
-
- return false;
- }
- const int MAX_IMG_THREAD = 4;
- //using imgFuncThreadParams = std::tuple<ImageProcessAPI, int, char*, char*, char*, DWORD, LogicDevice*, LogicDevice*>;
- using imgFuncThreadParams = std::tuple<int, ImageProcessAPI*, int, char*, char*, char*, DWORD, LogicDevice*, LogicDevice**>;
- imgFuncThreadParams g_paramImgThread[MAX_IMG_THREAD];
- pthread_t g_hImgThread[MAX_IMG_THREAD];
- std::vector<std::shared_ptr<LinuxEvent>> g_hIdleEvent;
- std::vector<std::shared_ptr<LinuxEvent>> g_hDataReaded;
- std::vector<std::shared_ptr<LinuxEvent>> g_hDataReadyEvent;
- void* ImgeProcessThread(void* pPara);
- bool NewModelDPC::InitDeviceMgr(const char *pDriverpath)
- {
- std::cout << "NewModelDPC::InitDeviceMgr: Starting to load library: " << pDriverpath << std::endl;
- m_Module = dlopen(pDriverpath, RTLD_LAZY);
- if (m_Module)
- {
- std::cout << "NewModelDPC::InitDeviceMgr: Successfully loaded driver library: " << pDriverpath << std::endl;
- //mLog::FINFO("Load Driver ok.. {$}", pDriverpath);
- typedef nsDEV::IODriver* (*GetDriverMgrAPI)();
- std::cout << "NewModelDPC::InitDeviceMgr: Trying to get 'CreateIODriver' function address" << std::endl;
- GetDriverMgrAPI callfunc = (GetDriverMgrAPI)dlsym(m_Module, "CreateIODriver");
- const char* dlsym_error = dlerror();
- if (dlsym_error) {
- std::cout << "NewModelDPC::InitDeviceMgr: Failed to get 'CreateIODriver' function: " << dlsym_error << std::endl;
- // 处理错误
- dlclose(m_Module);
- return false;
- }
- if (callfunc)
- {
- std::cout << "NewModelDPC::InitDeviceMgr: Successfully obtained 'CreateIODriver' function" << std::endl;
- auto pDriver = reinterpret_cast <nsDEV::IODriver*> (callfunc());
- if (pDriver)
- {
- std::cout << "NewModelDPC::InitDeviceMgr: Successfully created IODriver instance" << std::endl;
- m_NewDevManager.reset(pDriver);
- std::cout << "NewModelDPC::InitDeviceMgr: Starting to create " << MAX_IMG_THREAD << " image processing threads" << std::endl;
- for (int x = 0; x < MAX_IMG_THREAD; x++)
- {
- g_hDataReadyEvent.push_back(LinuxEvent::CreateEvent(LinuxEvent::AUTO_RESET, false));
- g_hIdleEvent.push_back(LinuxEvent::CreateEvent(LinuxEvent::AUTO_RESET, true));
- g_hDataReaded.push_back(LinuxEvent::CreateEvent(LinuxEvent::AUTO_RESET, false));
- std::cout << "NewModelDPC::InitDeviceMgr: Creating image processing thread " << x << std::endl;
- // 创建线程
- int ret = pthread_create(&g_hImgThread[x], nullptr, ImgeProcessThread, (void*)(intptr_t)x);
- if (ret != 0) {
- std::cout << "NewModelDPC::InitDeviceMgr: Failed to create thread " << x << " with error code: " << ret << std::endl;
- // 线程创建失败处理
- }
- else {
- std::cout << "NewModelDPC::InitDeviceMgr: Successfully created image processing thread " << x << std::endl;
- // 可选:设置线程名称或属性
- }
- }
- g_hDataReadyEvent.push_back(g_hExit);
- g_hIdleEvent.push_back(g_hExit);
- std::cout << "NewModelDPC::InitDeviceMgr: Initialized data for image processing threads" << std::endl;
- ///初始化 图像处理线程需要的数据
- return true;
- }
- else
- {
- std::cout << "NewModelDPC::InitDeviceMgr: Library " << pDriverpath << " returned null IODriver instance" << std::endl;
- dlclose(m_Module);
- //mLog::FERROR("lib:{$} no Object returned.ErrNo:{$}\n", pDriverpath, errNo);
- }
- }
- else
- {
- std::cout << "NewModelDPC::InitDeviceMgr: Library " << pDriverpath << " has no 'CreateIODriver' entry point" << std::endl;
- dlclose(m_Module);
- //mLog::FERROR("lib:{$} no CreateIODriver entry.ErrNo:{$}\n", pDriverpath, errNo);
- }
- }
- else
- {
- // 获取dlopen错误
- const char* dl_error = dlerror();
- std::cout << "NewModelDPC::InitDeviceMgr: Failed to load library " << pDriverpath << ". Error: " << (dl_error ? dl_error : "Unknown error") << std::endl;
- //mLog::FERROR("Load lib:{$} failed.ErrNo:{$}\n", pDriverpath, errNo);
- }
- return false;
- }
- static void TrimeSDKPath(std::string& sdkPath)
- {
- if (sdkPath.length() > 0)
- {
- if (sdkPath[0] == '\\' || sdkPath[0] == '/')
- sdkPath = sdkPath.substr(1);
- if (sdkPath.length() > 0)
- {
- if (sdkPath[sdkPath.length() - 1] == '\\' || sdkPath[sdkPath.length() - 1] == '/')
- {
- sdkPath = sdkPath.substr(0, sdkPath.length() - 1);
- }
- }
- }
- }
- namespace fs = std::filesystem;
- void copy_directory(const fs::path& src, const fs::path& dst) {
- if (!fs::exists(dst)) {
- fs::create_directory(dst);
- }
- for (auto& dir_entry : fs::recursive_directory_iterator(src)) {
- const auto& path = dir_entry.path();
- auto relative_path = path.lexically_relative(src);
- fs::path dest_path = dst / relative_path;
- if (fs::is_directory(path)) {
- fs::create_directories(dest_path);
- }
- else if (fs::is_regular_file(path)) {
- fs::copy(path, dest_path, fs::copy_options::update_existing);
- }
- else {
- std::cerr << "Unhandled path: " << path << std::endl;
- }
- }
- }
- bool NewModelDPC::DriverEntry(ResDataObject &Configuration)
- {
- std::cout << "Entering NewModelDPC::DriverEntry" << std::endl;
- const char *pDriverpath = Configuration["oemdriver"];
- std::cout << "Checking oemdriver configuration" << std::endl;
- if (pDriverpath == 0 || strlen(pDriverpath) == 0)
- {
- std::cout << "Error: context of oemdriver is empty" << std::endl;
- //mLog::FERROR("context of oemdriver is empty");
- return false;
- }
- std::cout << "Updating hardware information from configuration" << std::endl;
- m_resHardwareInfo.update("MajorID", (const char*)Configuration["MajorID"]);
- m_resHardwareInfo.update("MinorID", (const char*)Configuration["MinorID"]);
- m_resHardwareInfo.update("VendorID", (const char*)Configuration["VendorID"]);
- m_resHardwareInfo.update("ProductID", (const char*)Configuration["ProductID"]);
- m_resHardwareInfo.update("SerialID", (const char*)Configuration["SerialID"]);
- string sdkPath,realSdkPath;
- m_nServerIdx = -1;
- if (Configuration.GetKeyCount("ServerID") > 0)
- {
- std::cout << "ServerID configuration found" << std::endl;
- //有ServerID配置
- m_nServerIdx = (int)Configuration["ServerID"];
- std::cout << "ServerID value: " << m_nServerIdx << std::endl;
- if (m_nServerIdx > 0)
- {
- std::cout << "ServerID > 0, need to clone SDK path from 0" << std::endl;
- //mLog::FINFO("Need clone SDK path from 0");
- if (Configuration.GetKeyCount("SDKPath") > 0)
- {
- sdkPath = (const char*)Configuration["SDKPath"];
- realSdkPath = sdkPath;
- std::cout << "Original SDKPath: " << sdkPath << std::endl;
- TrimeSDKPath(realSdkPath);
- std::cout << "Trimmed SDKPath: " << realSdkPath << std::endl;
- realSdkPath = GetProcessDirectory() + "\\" + realSdkPath;
- std::cout << "Full SDKPath: " << realSdkPath << std::endl;
- string newSdkPath = realSdkPath + to_string(m_nServerIdx);
- std::cout << "Trying to copy directory from " << realSdkPath << " to " << newSdkPath << std::endl;
- //mLog::FINFO("Try Copy Directory from {$} to {$}", realSdkPath, newSdkPath);
- try {
- copy_directory(realSdkPath, newSdkPath);
- std::cout << "Successfully copied directory" << std::endl;
- }
- catch (...)
- {
- std::cout << "Warning: Failed to copy directory (may already exist)" << std::endl;
- //mLog::FWARN("Copy Directory from {$} to {$} Error maybe exists.", realSdkPath, newSdkPath);
- }
- if (sdkPath.length() > 0)
- {
- std::cout << "Updating SDKPath with ServerID suffix" << std::endl;
- if (sdkPath[sdkPath.length() - 1] == '\\' )
- {
- sdkPath = sdkPath.substr(0, sdkPath.length() - 1) + to_string(m_nServerIdx) + "\\";
- }
- else if (sdkPath[sdkPath.length() - 1] == '/')
- {
- sdkPath = sdkPath.substr(0, sdkPath.length() - 1) + to_string(m_nServerIdx) + "/";
- }
- else
- {
- sdkPath = sdkPath + to_string(m_nServerIdx);
- }
- Configuration.update("SDKPath", sdkPath.c_str());
- std::cout << "Updated SDKPath: " << sdkPath << std::endl;
- }
- }
- else
- {
- std::cout << "No SDKPath configuration found" << std::endl;
- }
- }
- }
- std::cout << "Initializing Device Manager with driver path: " << pDriverpath << std::endl;
- if (InitDeviceMgr(pDriverpath) == false)
- {
- std::cout << "Error: CreateIODriver Failed. Path: " << pDriverpath << std::endl;
- //mLog::FERROR("CreateIODriver Failed.{$}",pDriverpath);
- return false;
- }
- std::cout << "Device Manager initialized successfully" << std::endl;
- std::cout << "Calling ModuleDriver::DriverEntry" << std::endl;
- bool ret = ModuleDriver::DriverEntry(Configuration);
- std::cout << "ModuleDriver::DriverEntry returned: " << (ret ? "true" : "false") << std::endl;
- string moduleConfig = GetModuleConfigrateFilePath(m_resHardwareInfo);
- std::cout << "Trying to load configuration from module config: " << moduleConfig << std::endl;
- string filepath;
- try {
- ResDataObject moduleConf;
- moduleConf.loadFile(moduleConfig.c_str());
- filepath = moduleConfig;
- std::cout << "Successfully loaded configuration from module config: " << moduleConfig << std::endl;
- //mLog::FINFO("Load Configration from Mudle Config [{$}]", moduleConfig);
- }
- catch (...)
- {
- std::cout << "Error: Failed to load configuration from module config: " << moduleConfig << std::endl;
- //mLog::FERROR("Failed to Load Configration from Driver Config [{$}]", moduleConfig);
- filepath = GetDriverConfigFilePath();
- std::cout << "Trying to load configuration from driver config: " << filepath << std::endl;
- //mLog::FERROR("Try Load Configration from Driver Config [{$}]", moduleConfig);
- }
- //这里修改SDKPath和ServerID
- if (m_nServerIdx > 0 && sdkPath.length() > 0)
- {
- std::cout << "Updating SDKPath and ServerID in config file: " << m_nServerIdx << std::endl;
- //mLog::FINFO("Change SDKPath and ServerID {$}", m_nServerIdx);
- ResDataObject resCtx;
- try
- {
- resCtx.loadFile(filepath.c_str());
- std::cout << "Loaded config file: " << filepath << std::endl;
- if (resCtx.GetKeyCount("CONFIGURATION") > 0)
- {
- std::cout << "Updating CONFIGURATION section" << std::endl;
- if (resCtx["CONFIGURATION"].GetKeyCount("SDKPath") > 0 )
- {
- resCtx["CONFIGURATION"]["SDKPath"].update("Value", sdkPath.c_str());
- std::cout << "Updated SDKPath in config to: " << sdkPath << std::endl;
- }
- else
- {
- ResDataObject resObj;
- resObj.add("Value", sdkPath.c_str());
- resCtx["CONFIGURATION"].add("SDKPath", resObj);
- std::cout << "Added SDKPath to config: " << sdkPath << std::endl;
- }
- if (resCtx["CONFIGURATION"].GetKeyCount("ServerID") > 0)
- {
- resCtx["CONFIGURATION"]["ServerID"].update("Value", m_nServerIdx);
- std::cout << "Updated ServerID in config to: " << m_nServerIdx << std::endl;
- }
- else
- {
- ResDataObject resObj;
- resObj.add("Value", m_nServerIdx);
- resCtx["CONFIGURATION"].add("ServerID", resObj);
- std::cout << "Added ServerID to config: " << m_nServerIdx << std::endl;
- }
- if (resCtx["CONFIGURATION"].GetKeyCount("ComServerID") > 0)
- {
- resCtx["CONFIGURATION"]["ComServerID"].update("Value", m_nServerIdx);
- std::cout << "Updated ComServerID in config to: " << m_nServerIdx << std::endl;
- }
-
- resCtx.SaveFile(filepath.c_str());
- std::cout << "Saved updated config file: " << filepath << std::endl;
- }
- else
- {
- std::cout << "CONFIGURATION section not found in config file" << std::endl;
- }
- }
- catch (...)
- {
- //mLog::FERROR("Read config file error {$}", filepath);
- std::cout << "Error: Read config file error: " << filepath << std::endl;
- }
- }
- if (ret)
- {
- //这里检查模型对象的配置项是否存在,如果是走模型配置,在安装的时候,会自动创建一份修改后的配置文件
- //这里可以直接检查到
- std::cout << "Calling m_NewDevManager->DriverEntry with path: " << filepath << std::endl;
- //mLog::FINFO("m_NewDevManager->DriverEntry {$}", filepath);
- ret = m_NewDevManager->DriverEntry(filepath);
- std::cout << "m_NewDevManager->DriverEntry returned: " << (ret ? "true" : "false") << std::endl;
- //mLog::FINFO("m_NewDevManager->Prepare ");
- std::cout << "Calling m_NewDevManager->Prepare" << std::endl;
- m_NewDevManager->Prepare();
- std::cout << "m_NewDevManager->Prepare completed" << std::endl;
- }
- else
- {
- std::cout << "Error: LogicDriver::DriverEntry Failed. Path: " << filepath.c_str() << std::endl;
- //mLog::FERROR("LogicDriver::DriverEntry Failed.{$}", filepath.c_str());
- }
- std::cout << "Exiting NewModelDPC::DriverEntry with return value: " << (ret ? "true" : "false") << std::endl;
- return ret;
- }
- DWORD SYSTEM_CALL NewModelDPC::OnNotify(std::vector<std::shared_ptr<LinuxEvent>> evtList, DWORD count)
- {
- DWORD dwResult = LinuxEvent::WaitForMultipleEvents(evtList, 2500);
- if ((dwResult >= WAIT_OBJECT_0) && (dwResult < WAIT_OBJECT_0 + count - 1))
- {
- //mLog::FINFO("OnNotify Platform");
- return dwResult - WAIT_OBJECT_0;
- }
- else
- {
- if (NULL != m_NewDevManager)
- {
- if (!m_NewDevManager->isConnected())
- {
- //mLog::FWARN("CONNECTED is false");
- if (*m_pConnectionStatus)
- {
- //mLog::FWARN("Notify disCONNECTED ");
- LogicDriver::DisConnect();
- }
- if (!*m_pConnectionStatus)
- {
- //mLog::FINFO("Try CONNECT");
- if (m_NewDevManager->Connect())
- {
- //mLog::FINFO("try CONNECTED is ok");
- LogicDriver::Connect();
-
- //mLog::FINFO("Notify CONNECTED is ok");
- }
- else
- {
- //mLog::FWARN("Notify disCONNECTED ");
- LogicDriver::DisConnect();
- //mLog::FWARN("Try CONNECT failed. ");
- }
- }
-
- }
- else
- {
- if (!*m_pConnectionStatus)
- {
- //mLog::FINFO("Auto CONNECTED is ok");
- LogicDriver::Connect();
- //mLog::FINFO("Notify CONNECTED is ok");
- }
- }
- //此处从程序启动后会一直打印,重复调用work函数
- ////mLog::FINFO("ImageSaveDev->work()");
- //((ImageSaveDev*)m_pImgSaveDev)->Work();
- }
- }
- return 1;
- }
- RET_STATUS NewModelDPC::OnAction(const char* pszActionName, const char* pszParams, ResDataObject& resResponse)
- {
- RET_STATUS ret = ModuleDriver::OnAction(pszActionName, pszParams, resResponse);
- if (ret != RET_NOSUPPORT)
- {
- //成功或者失败,
- return ret;
- }
- string action = pszActionName;
- if (action == "InstallVirutalDevice")
- {
- //安装虚拟设备
- /// "ABS": {
- // "InstalHost":"Detector",
- // "ImgeProcessDevice" : "ABS\\ABSX64.dll"
- // },
- // "DAP": {
- // "MessageProcessDevice": "DAP\\DAPX64.dll"
- // }
- //
- //找到配置项,检查文件
- //ResDataObject resTest,resdll;
- //resdll.add("ImgeProcessDevice", "ABS\\ABSX64.dll");
- //resTest.add("ABS", resdll);
- ResDataObject resConf;
- string fileName = GetDriverConfigFilePath();
- //mLog::FINFO("Try Load xml Config file {$} ", fileName);
- resConf.loadFile(fileName.c_str());
- //string test = resTest.encode();
- if (resConf.GetKeyCount("CONFIGURATION") <= 0)
- {
- //mLog::FERROR("config file {$} content error", fileName);
- ret = RET_FAILED;
- return ret;
- }
- //resConf = resConf["CONFIGURATION"];
- ResDataObject resVirtual;
- resVirtual.decode(pszParams);
- bool changed = false;
- string strServDevPath;
- for (int vdIdx = 0; vdIdx < resVirtual.size(); vdIdx++)
- {
- bool got = false;
- string vdModuleName;
- //DTS/ABS/...
- if (strcmp(resVirtual.GetKey(vdIdx), "RespTopic") == 0)
- continue;
- if (resVirtual[vdIdx].GetKeyCount("ServDevPath") > 0)
- strServDevPath = (const char*)resVirtual[vdIdx]["ServDevPath"];
- else
- strServDevPath = "";
- for (int cfIdx = 0; cfIdx < resVirtual[vdIdx].size(); cfIdx++)
- {
- string va = (const char*)resVirtual[vdIdx][cfIdx];
- if (va.length() > 4 && va.substr(va.length() - 4, 4) == ".dll")
- {
- string type = resVirtual[vdIdx].GetKey(cfIdx);
- vdModuleName = va;
- int idx = resConf["CONFIGURATION"].GetFirstOf(type.c_str());
- while (idx != -1)
- {
- string confV = resConf["CONFIGURATION"][idx].encode();
- if (confV == va)
- {
- got = true;
- break;
- }
- idx = resConf["CONFIGURATION"].GetNextOf(type.c_str(), idx);
- }
- if (!got)
- {
- resConf["CONFIGURATION"].add(type.c_str(), va.c_str());
- changed = true;
- break;
- }
- }
- }
- if (resVirtual[vdIdx].GetKeyCount("ServDevPath") > 0)
- {
- }
- if (got || changed)
- {
- //新装
- string newdev = "CCOS/DEVICE/" + string(resVirtual.GetKey(vdIdx));
- string devPath = newdev;
- devPath += "/" + string((const char*)m_resHardwareInfo["VendorID"]);// , (const char*)Configuration["VendorID"]);
- devPath += "/" + string((const char*)m_resHardwareInfo["ProductID"]);//, (const char*)Configuration["ProductID"]);
- devPath += "/" + string((const char*)m_resHardwareInfo["SerialID"]);//, (const char*)Configuration["SerialID"]);
- //mLog::FINFO("New Virtual Device Path {$}", devPath);
- resResponse.add(strServDevPath.c_str(), "");
- if (strServDevPath.length() <= 0)
- strServDevPath = devPath;
- if (!got)
- {
- //mLog::FINFO("New install VD {$]", strServDevPath);
- //之前没有配置,则需要测试,并设置
- LoadVirtualDevice(vdModuleName.c_str(), false, false, devPath, strServDevPath);
- }
- else
- {
- //mLog::FINFO("VD had installed aready {$]", strServDevPath);
- for(int x=0;x<m_arrImgeProcessDevices.size();x++)
- if (m_arrImgeProcessDevices[x]->GetCcosRootPath() == strServDevPath)
- {
- //
- //mLog::FINFO("VD {$} had installed aready type ImageProcessDevices with idx {$]", strServDevPath, x);
- resResponse.update(strServDevPath.c_str(), "ON");
- }
- for (int x = 0; x < m_arrMessageProcessDevices.size(); x++)
- if (m_arrMessageProcessDevices[x]->GetCcosRootPath() == strServDevPath)
- {
- //
- //mLog::FINFO("VD {$} had installed aready type MessageProcessDevices with idx {$]", strServDevPath, x);
- resResponse.update(strServDevPath.c_str(), "ON");
- }
- if(strlen(resResponse[strServDevPath.c_str()]) == 0)
- resResponse.update(strServDevPath.c_str(), "OFF");
- }
- }
- }
- if (changed)
- {
- //ResDataObject save;
- //save.add("CONFIGURATION", resConf);
- resConf.SaveFile(fileName.c_str());
-
- }
- ret = RET_SUCCEED;
- //resResponse = "Install ok.";
- }
- else if (action == "restart")
- {
- resResponse = "reStart ok.";
- ret = RET_SUCCEED;
- exit(1);
- }
- return ret;
- }
- bool NewModelDPC::GetDeviceType(GUID &DevType)
- {
- std::string DriverType = m_NewDevManager->GetGUID();
- if (DriverType.size() == 0)
- {
- //mLog::FERROR("GetDeviceType Failed");
- return false;
- }
- //if (m_pNewDevManager->GetDriverType(DriverType) == false)
- //{
- // return false;
- //}
- return string_2_guid(DriverType.c_str(), DevType);
- }
- std::string DEVICE_ID = "";
- std::string TOPIC_PREFIX = "";
- bool SYSTEM_CALL NewModelDPC::Driver_Probe(ResDataObject& PARAM_OUT HardwareInfo)
- {
- //string DriverInfo = m_NewDevManager->DriverProbe();
- //mLog::FINFO("Driver : DriverInfo {$}", m_resHardwareInfo.encode());
- //if (m_pNewDevManager->Driver_Probe(DriverInfo))
- {
- ResDataObject Config = m_resHardwareInfo;
- HardwareInfo = m_resHardwareInfo;
- //if (Config.decode(DriverInfo.c_str()))
- {
- //HardwareInfo.add("MajorID", (const char*)Config["MajorID"]);
- //HardwareInfo.add("MinorID", (const char*)Config["MinorID"]);
- //HardwareInfo.add("VendorID", (const char*)Config["VendorID"]);
- //HardwareInfo.add("ProductID", (const char*)Config["ProductID"]);
- //HardwareInfo.add("SerialID", (const char*)Config["SerialID"]);
- ostringstream os,os2;
- os << (const char*)Config["VendorID"] << "_" << (const char*)Config["ProductID"] << "_" << (const char*)Config["SerialID"];
- DEVICE_ID = os.str();
-
- os2 << "" << (const char*)Config["MajorID"] ;//<< "/" << (const char*)Config["VendorID"] << "/" << (const char*)Config["ProductID"];
- os2 << "/" << (const char*)Config["SerialID"] << "/";
- TOPIC_PREFIX = os2.str();
- return true;
- }
- //else
- //{
- // //mLog::FERROR("Driver_Probe Failed");
- //}
- }
- return false;
- }
- RET_STATUS NewModelDPC::GetDeviceResource(ResDataObject *pDeviceResource)
- {
- RET_STATUS ret = RET_FAILED;
- ResDataObject BaseLayerConfig;
- if ((*m_config).GetPropties().size() > 0)
- {
- ModuleDriver::GetDeviceResource(pDeviceResource);
- //mLog::FINFO("ModuleDriver : GetDeviceResource {$}", pDeviceResource->encode());
- //BaseLayerConfig = *pDeviceResource;
- }
- std::string ResInfo;// = m_NewDevManager->GetResource();
- //mLog::FINFO("IO Driver : GetDeviceResource {$}", ResInfo);
- //if (m_pNewDevManager->GetDriverResource(ResInfo))
- {
- //low layerInfo
- ResDataObject LowLayerConfig;
- if ((*m_config).GetPropties().size() <= 0)
- {
- ResInfo = m_NewDevManager->GetResource();
- if (LowLayerConfig.decode(ResInfo.c_str()) == false)
- {
- //mLog::FERROR("GetDriverResource Failed");
- return ret;
- }
- ResDataObject BaseLayerConfig;
- ret = LogicDriver::GetDeviceResource(&BaseLayerConfig);
- if (ret < RET_SUCCEED)
- {
- return ret;
- }
- }
- //base LogicDriverinfo
- if (BaseLayerConfig.size() <= 0)
- {
- ret = LogicDriver::GetDeviceResource(&BaseLayerConfig);
- //mLog::FINFO("LogicDriver : GetDeviceResource {$}", BaseLayerConfig.encode());
- if (ret < RET_SUCCEED)
- {
- return ret;
- }
- }
- if (LowLayerConfig.GetFirstOf("Attribute") >= 0)
- {
- //loop Low Layer Attribute
- for (size_t i = 0; i < LowLayerConfig["Attribute"].size(); i++)
- {
- const char *pKey = LowLayerConfig["Attribute"].GetKey(i);
- if (pKey)
- {
- int BaseIdx = BaseLayerConfig["Attribute"].GetFirstOf(pKey);
- if (BaseIdx >= 0)
- {
- //exist then overwrite
- BaseLayerConfig["Attribute"][BaseIdx] = LowLayerConfig["Attribute"][i];
- }
- else
- {
- //not exist then add
- BaseLayerConfig["Attribute"].add(pKey, LowLayerConfig["Attribute"][i]);
- }
- }
- }
- }
- if (LowLayerConfig.GetFirstOf("Action") >= 0)
- {
- //loop Low Layer Action
- for (size_t i = 0; i < LowLayerConfig["Action"].size(); i++)
- {
- const char *pKey = LowLayerConfig["Action"].GetKey(i);
- if (pKey)
- {
- int BaseIdx = BaseLayerConfig["Action"].GetFirstOf(pKey);
- if (BaseIdx >= 0)
- {
- //exist then overwrite
- BaseLayerConfig["Action"][BaseIdx] = LowLayerConfig["Action"][i];
- }
- else
- {
- //not exist then add
- BaseLayerConfig["Action"].add(pKey, LowLayerConfig["Action"][i]);
- }
- }
- }
- }
- (*pDeviceResource) = BaseLayerConfig;
- //mLog::FINFO("GetDeviceResource Result {$}", BaseLayerConfig.encode());
- return RET_SUCCEED;
- }
- return ret;
- }
- /*
- void NewModelDPC::SubscribeSelf() {
- //这里订阅topic
- }*/
- bool NewModelDPC::Device_Probe(ResDataObject &HardwareInfo)
- {
- //string DeviceInfo = m_NewDevManager->DeviceProbe();
- //mLog::FINFO("Driver : Device_Probe {$}", m_resHardwareInfo.encode());
- //if (m_pNewDevManager->Device_Probe(DeviceInfo))
- {
- //ResDataObject Config;
- //if (Config.decode(DeviceInfo.c_str()))
- {
- //HardwareInfo.add("MajorID", (const char*)Config["MajorID"]);
- //HardwareInfo.add("MinorID", (const char*)Config["MinorID"]);
- //HardwareInfo.add("VendorID", (const char*)Config["VendorID"]);
- //HardwareInfo.add("ProductID", (const char*)Config["ProductID"]);
- //HardwareInfo.add("SerialID", (const char*)Config["SerialID"]);
- HardwareInfo = m_resHardwareInfo;
- return true;
- }
- //else
- //{
- // //mLog::FERROR("Device_Probe Failed");
- //}
- }
- return false;
- }
- bool NewModelDPC::SetDeviceWorkPath(const char *pWorkPath)
- {
- return true;
- }
- bool NewModelDPC::SetDriverWorkPath(const char *pWorkPath)
- {
- (*m_pWorkpath) = pWorkPath;
- //要把读取单一硬件属性的过程放到此处
- return true;
- }
- bool NewModelDPC::Connect()
- {
- bool bret = m_NewDevManager->Connect();
- if (bret)
- {
- LogicDriver::Connect();//make sure it's connected
- //mLog::FINFO("Connect Succeed");
- return true;
- }
- else
- {
- //mLog::FERROR("Connect Failed");
- }
- return false;
- }
- bool SYSTEM_CALL NewModelDPC::GetConnectionStatus()
- {
- return m_NewDevManager->isConnected();
- }
- void NewModelDPC::DisConnect()
- {
- m_NewDevManager->Disconnect();
- LogicDriver::DisConnect();//make sure it's Disconnected
- //mLog::FINFO("DisConnect Succeed");
- }
- bool SYSTEM_CALL NewModelDPC::OnHeartBeat()
- {
- return m_NewDevManager->OnHeartBeat();
- }
- std::string getCurrentTime() {
- auto now = std::chrono::system_clock::now();
- std::time_t now_time = std::chrono::system_clock::to_time_t(now);
- std::tm* local_time = std::localtime(&now_time);
- char time_str[20];
- std::strftime(time_str, sizeof(time_str), "%Y-%m-%d %H:%M:%S", local_time);
- return std::string(time_str);
- }
- void* ImgeProcessThread(void* pPara)
- {
- std::cout << "[" << getCurrentTime() << "] ImgeProcessThread: Thread started. pPara = " << pPara << std::endl;
- intptr_t nIndex = reinterpret_cast<intptr_t>(pPara);
- std::cout << "[" << getCurrentTime() << "] ImgeProcessThread: nIndex = " << nIndex << std::endl;
- if (nIndex < 0 || nIndex >= g_hDataReadyEvent.size()) {
- std::cout << "[" << getCurrentTime() << "] ERROR: ImgeProcessThread: nIndex is negative (" << nIndex << "), thread exit." << std::endl;
- return nullptr;
- }
- imgFuncThreadParams* pParam = g_paramImgThread + nIndex;
- std::vector<std::shared_ptr<LinuxEvent>> hWait;
- if (!g_hDataReadyEvent[nIndex]) {
- std::cout << "[" << getCurrentTime() << "] ERROR: ImgeProcessThread: g_hDataReadyEvent[" << nIndex << "] is null, thread exit." << std::endl;
- return nullptr;
- }
- hWait.push_back(g_hDataReadyEvent[nIndex]); // 第一个事件
- hWait.push_back(g_hExit);
- while (true)
- {
- DWORD dwWait = LinuxEvent::WaitForMultipleEvents(hWait, 1000);
- if (dwWait == WAIT_OBJECT_0)
- {
- //有图像
- int nNum = std::get<0>(*pParam);
- ImageProcessAPI* func = std::get<1>(*pParam);
- LogicDevice** pDev = std::get<8>(*pParam);
- string Head = std::get<3>(*pParam);
- string context = std::get<4>(*pParam);
- g_hDataReaded[nIndex]->SetEvent();
- if (func != nullptr)
- {
- //string temp = std::get<3>(*pParam);
- for (int x = 0; x < nNum; x++)
- {
- if (func[x] == nullptr)
- {
- //mLog::FERROR("Thread[{$}] ImageProcessFunc is null with devpath= {$}", nIndex, pDev[x]->GetCcosRootPath());
- continue;
- }
- //mLog::FINFO("Thread[{$}] start ImageProcess {$} from {$} with devpath= {$}", nIndex, Head, std::get<7>(*pParam)->GetCcosRootPath(), pDev[x]->GetCcosRootPath());
- 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]);
- //mLog::FINFO("Thread[{$}] ImageProcessFunc Over with devpath= {$}", nIndex, pDev[x]->GetCcosRootPath());
- }
- }
- else
- {
- //mLog::FERROR("Thread[{$}] ImageProcessFunc arrpoint is null ", nIndex);
- }
- g_hIdleEvent[nIndex]->SetEvent();
- }
- else if(dwWait == WAIT_OBJECT_0 + 1)
- {
- //mLog::FINFO("ImageThread {$} Got exit", nIndex);
- }
- }
- //delete pParam;
- //std::get<1>(*pParam) = Type;
- //std::get<2>(*pParam) = (char*)imageHead.c_str();
- //std::get<3>(*pParam) = (char*)resContext.encode();
- //std::get<4>(*pParam) = pFrameBuff;
- //std::get<5>(*pParam) = FrameSize;
- //std::get<6>(*pParam) = pDeviceFrom;
- //std::get<7>(*pParam) = m_arrImgeProcessDevices[x];
- return 0;
- }
- /// <summary>
- ///
- /// </summary>
- /// <param name="Type"></param>
- /// <param name="imageHead"></param>
- /// <param name="pFrameBuff"></param>
- /// <param name="FrameSize"></param>
- /// <param name="pDeviceFrom"></param>
- /// <returns></returns>
- int NewModelDPC::ImageRrocess(int Type, string& imageHead, ResDataObject& resContext, char* pFrameBuff, DWORD FrameSize, NewModelDevice* pDeviceFrom)
- {
- //mLog::FINFO("Begin");
- //std::vector<HANDLE> threads;
- if (m_arrImgeFunc.size() <= 0)
- return 0;
- char* context = (char*)resContext.encode();
- char* header = (char*)imageHead.c_str();
- //HANDLE thread = INVALID_HANDLE_VALUE;
- //for (int x = 0; x < m_arrImgeFunc.size(); x++)
- {
- DWORD dwWait = LinuxEvent::WaitForMultipleEvents(g_hIdleEvent, -1);
- //mLog::FINFO("Wait for ImageProcessThrad Idle return {$}", dwWait);
- if (dwWait != WAIT_TIMEOUT && dwWait >= WAIT_OBJECT_0 && dwWait < WAIT_OBJECT_0 + MAX_IMG_THREAD)
- {
- imgFuncThreadParams* pParam = g_paramImgThread + (dwWait - WAIT_OBJECT_0);
- std::get<0>(*pParam) = m_arrImgeFunc.size();
- std::get<1>(*pParam) = m_arrImgeFunc.data();
- std::get<2>(*pParam) = Type;
- std::get<3>(*pParam) = header;
- std::get<4>(*pParam) = context;
- std::get<5>(*pParam) = pFrameBuff;
- std::get<6>(*pParam) = FrameSize;
- std::get<7>(*pParam) = pDeviceFrom;
- std::get<8>(*pParam) = (LogicDevice**)m_arrImgeProcessDevices.data();
- //mLog::FINFO("Notify ImageProcessThread [{$}] work ", dwWait - WAIT_OBJECT_0);
- g_hDataReadyEvent[dwWait - WAIT_OBJECT_0]->SetEvent();
- //等待线程 读取完数据 ,就可以下一个
- //mLog::FINFO("Start Wait {$} Thread to Read Data.", m_arrImgeFunc.size());
- DWORD dwRet = g_hDataReaded[dwWait - WAIT_OBJECT_0]->Wait(4000);
- //if (dwRet == WAIT_OBJECT_0)
- //mLog::FINFO("Over Succeded with ret {$}", dwRet);
- //else
- //mLog::FERROR("Over Timeout with ret {$}", dwRet);
- }
- else
- {
- //mLog::FERROR("No Process this Iamge with ret {$}", dwWait);
- }
- }
- //if (m_arrImgeFunc != nullptr)
- // return m_arrImgeFunc(Type, imageHead, (char*)resContext.encode(), pFrameBuff, FrameSize, pDeviceFrom, m_arrImgeProcessDevices);
- return m_arrImgeFunc.size();
- }
- /// <summary>
- ///
- /// </summary>
- /// <param name="resNotify"></param>
- /// <param name="pDeviceFrom"></param>
- /// <returns></returns>
- int NewModelDPC::NotifyMessageProcess(ResDataObject& resNotify, NewModelDevice* pDeviceFrom)
- {
- //mLog::FINFO("Begin");
- ResDataObject resResp;
- RET_STATUS ret = RET_FAILED;
- //if (m_arrMessageProcessDevices != nullptr)
- for(int x=0;x< m_arrMessageProcessDevices.size();x++)
- {
- ret = m_arrMessageProcessDevices[x]->Request(&resNotify, &resResp);
- }
- //if (m_arrImgeProcessDevices != nullptr)
- for(int x=0;x< m_arrImgeProcessDevices.size();x++)
- {
- ret = m_arrImgeProcessDevices[x]->Request(&resNotify, &resResp);
- }
- //mLog::FINFO("End");
- return ret ;
- }
- void InitLogicDevice(LogicDevice* p, string guidname)
- {
- }
- PVOID NewModelDPC::LoadLogicDevices()
- {
- std::cout << "**************** NewModelDPC :: LoadLogicDevices" << std::endl;
- //mLog::FINFO( "Load Logic Devices");
- int nDeviceNumber = 1;
- string filepath = GetDriverConfigFilePath();
- ResDataObject Config;
- string imgLogicDevice, noImgLogicDevice;
- std::cout << "**************** NewModelDPC Load Config " << filepath << std::endl;
- if (Config.loadFile(filepath.c_str()))
- {
- int nHaveDeviceNumber = (int)Config["CONFIGURATION"].GetKeyCount("DeviceNumber");
- if (nHaveDeviceNumber > 0)
- {
- nDeviceNumber = Config["CONFIGURATION"]["DeviceNumber"];
- //mLog::FINFO("Have {$} Devices", nDeviceNumber);
- }
- else
- {
- //mLog::FWARN("Have no DeviceNumber Attribute");
- }
- int nHaveImgLogicDevice = (int)Config["CONFIGURATION"].GetKeyCount("ImgeProcessDevice");
- if (nHaveImgLogicDevice > 0)
- {
- int first = Config["CONFIGURATION"].GetFirstOf("ImgeProcessDevice");
- while (first != -1)
- {
- imgLogicDevice = (const char*)Config["CONFIGURATION"][first];
- if (imgLogicDevice.length() > 0)
- {
- //图像处理逻辑设备
- LoadVirtualDevice(imgLogicDevice.c_str(), true);
- }
- first = Config["CONFIGURATION"].GetNextOf("ImgeProcessDevice", first);
- }
-
- }
- int nHaveNoneImgLogicDevice = (int)Config["CONFIGURATION"].GetKeyCount("MessageProcessDevice");
- if (nHaveNoneImgLogicDevice > 0)
- {
- int first = Config["CONFIGURATION"].GetFirstOf("MessageProcessDevice");
- while (first != -1)
- {
- imgLogicDevice = (const char*)Config["CONFIGURATION"][first];
- if (imgLogicDevice.length() > 0)
- {
- //图像处理逻辑设备
- LoadVirtualDevice(imgLogicDevice.c_str(), false);
- }
- first = Config["CONFIGURATION"].GetNextOf("MessageProcessDevice", first);
- }
- //noImgLogicDevice = (const char*)Config["CONFIGURATION"]["MessageProcessDevice"];
- }
- }
- else
- {
- //mLog::FERROR("Load config file failed {$}",filepath.c_str());
- }
-
- DevTree *pTree = (DevTree *)GetDeviceTree();
- //imgLogicDevice "VirtualDevice/ABS/ABSX64.dll"
- //m_arrImgeProcessDevices = nullptr;
- //if (imgLogicDevice.length() > 0)
- //{
- // //图像处理逻辑设备
- // LoadVirtualDevice(imgLogicDevice.c_str(), true);
- //}
- //noImgLogicDevice "VirtualDevice/AGD/AGDX64.dll"
- //m_arrMessageProcessDevices = nullptr;
- //if (noImgLogicDevice.length() > 0)
- //{
- // //非图像处理设备
- // LoadVirtualDevice(noImgLogicDevice.c_str());
- //}
- for (int i = 0; i < nDeviceNumber; i++)
- {
- NewModelDevice *p = new NewModelDevice();
- std::cout << " ****************NewModelDPC Try new Device " << i << " of " << nDeviceNumber << std::endl;
- auto pIoDevice = m_NewDevManager->CreateDevice(i);
- if (pIoDevice)
- {
- GUID guid2;
- string guidname;
- //((LogicDevice*)pChild)->GetDeviceType(guid);
- //bool bget = pIoDevice->GetDeviceType(guid2);
- //guidname = pIoDevice->GetGUID();
- //guid_2_string(guid2, guidname);
- ResDataObject subpath;
- subpath = (guidname).c_str();
- ResDataObject probe,conn_usless;
- Device_Probe(probe);
- //此时知道宿主设备是谁,如果Generator/Detector
- //
- string hostName;
- bool isMain = false;
- if (probe.GetKeyCount("MajorID") > 0)
- {
- //找到宿主
- hostName = "CCOS/DEVICE/" + string((const char* )probe["MajorID"]);
- }
- string fullpath = MakeDevicePath(probe, conn_usless, subpath).encode();
- string ccospath = MakeCcosPath(probe, conn_usless, subpath, false).encode();
- if (hostName.length() > 0)
- {
- if (ccospath.substr(0, hostName.length()) == hostName)
- {
- //宿主设备
- isMain = true;
- //mLog::FINFO("Got MainHost {$} of {$}", hostName, ccospath);
- }
- }
- if (isMain && m_MainHostDevice == nullptr)
- {
- m_MainHostDevice = p;
- //mLog::FINFO("Add ImageProcess Deivce : {$} ", m_arrImgeProcessDevices.size());
- //嵌入式设备加载
- for (int x = 0; x < m_arrImgeProcessDevices.size(); x++)
- {
- GUID guid2;
- string guidname;
- m_arrImgeProcessDevices[x]->GetDeviceType(guid2);
- guid_2_string(guid2, guidname);
- ResDataObject subpath;
- subpath = (guidname).c_str();
- ResDataObject probe, conn_usless;
- Device_Probe(probe);
- string fullpatht = MakeDevicePath(probe, conn_usless, subpath).encode();
- string ccospatht = MakeCcosPath(probe, conn_usless, subpath, false).encode();
- m_arrImgeProcessDevices[x]->SetClientRootID(fullpatht.c_str(), ccospatht.c_str());
- m_arrImgeProcessDevices[x]->CompleteInit();
- pTree->Add((PVOID)(m_arrImgeProcessDevices[x]), TYPE_DEVICE);
- //mLog::FINFO("Try add ccos child {$} ", ccospatht);
- m_MainHostDevice->AddCcosChildren(m_arrImgeProcessDevices[x], ccospatht.c_str());
- }
- //mLog::FINFO("Add MessageProcess Deivce : {$} ", m_arrImgeProcessDevices.size());
- //if (m_arrMessageProcessDevices != nullptr)
- for (int x = 0; x < m_arrMessageProcessDevices.size(); x++)
- {
- GUID guid2;
- string guidname;
- m_arrMessageProcessDevices[x]->GetDeviceType(guid2);
- guid_2_string(guid2, guidname);
- ResDataObject subpath;
- subpath = (guidname).c_str();
- ResDataObject probe, conn_usless;
- Device_Probe(probe);
- string fullpatht = MakeDevicePath(probe, conn_usless, subpath).encode();
- string ccospatht = MakeCcosPath(probe, conn_usless, subpath, false).encode();
- m_arrMessageProcessDevices[x]->SetClientRootID(fullpatht.c_str(), ccospatht.c_str());
- m_arrMessageProcessDevices[x]->CompleteInit();
- pTree->Add((PVOID)(m_arrMessageProcessDevices[x]), TYPE_DEVICE);
- //mLog::FINFO("Try add ccos child {$} ", ccospatht);
- m_MainHostDevice->AddCcosChildren(m_arrMessageProcessDevices[x], ccospatht.c_str());
- }
- }
- //pDevice->SetEbusRoot(fullpath.c_str());
- p->SetClientRootID(fullpath.c_str(), ccospath.c_str());
- //mLog::FINFO(" SetClientRootID ", ccospath);
- //p->SetClientRootID();
- //mLog::FINFO("Get IoDevice Succeed");
- p->SetDrvDPC(this);
- std::shared_ptr<LinuxEvent> DisconnectEvt = GetPassiveDisConnectEvtHandle();
- //p->ConnectMQTTServer();
- p->Init(std::move(pIoDevice), DisconnectEvt);
- //p->SubscribeAction();
- //mLog::FINFO("Create IoDevice Succeed");
- }
- else
- {
- //mLog::FERROR("Create IoDevice Failed");
-
- delete p;
- return NULL;
- }
- //*/
- ResDataObject resPri;
- string strPri;
- for (int x = 1; x < m_arrImgeProcessDevices.size(); x++)
- {
- for (int y = 0; y < x; y++)
- {
- unsigned int xPri, yPri;
- xPri = yPri = -1;
- if (m_arrImgeProcessDevices[x]->DevGet("", "ImageProcessIndex", resPri) == RET_SUCCEED)
- {
- strPri = (const char*)resPri;
- if (strPri.length() > 0)
- xPri = atoi(strPri.c_str());
- //mLog::FINFO("Vd {$} ImageProcessIdex=[{$}]", m_arrImgeProcessDevices[x]->GetCcosRootPath(), strPri);
- }
- if (m_arrImgeProcessDevices[y]->DevGet("", "ImageProcessIndex", resPri) == RET_SUCCEED)
- {
- strPri = (const char*)resPri;
- if (strPri.length() > 0)
- xPri = atoi(strPri.c_str());
- //mLog::FINFO("Vd {$} ImageProcessIdex=[{$}]", m_arrImgeProcessDevices[y]->GetCcosRootPath(), strPri);
- }
- if (xPri < yPri)
- {
- auto* pTemp = m_arrImgeProcessDevices[x];
- auto fTemp = m_arrImgeFunc[x];
- m_arrImgeProcessDevices[x] = m_arrImgeProcessDevices[y];
- m_arrImgeProcessDevices[y] = pTemp;
- m_arrImgeFunc[x] = m_arrImgeFunc[y];
- m_arrImgeFunc[y] = fTemp;
- //mLog::FINFO("Exchange Vd [{$}] <=> [{$}]", x, y);
- }
- }
- }
- //*/
- LogicDevice *pret = (LogicDevice *)p;
- pTree->Add((PVOID)pret, TYPE_DEVICE);
- LogicDevice::AddEbusChildren(p, "");
- }
- return (PVOID)pTree;
- }
- void NewModelDPC::UnloadLogicDevices()
- {
- //mLog::FINFO( "UnLoad Logic Devices");
- DevTree *pTree = (DevTree *)GetDeviceTree();
- size_t Size = pTree->size();
- for (size_t i = 0; i < Size; i++)
- {
- DevTreeNode node = (*pTree)[i];
- if (node.m_NodeType == TYPE_DEVICE)
- {
- NewModelDevice *pdev = (NewModelDevice*)node.m_pObject;
- delete pdev;
- }
- else
- {
- //tree dev
- //do not copy this!!!!
- //UnloadLogicDevices(node.m_pObject);
- }
- }
- pTree->clear();
- //mLog::FINFO( "UnLoad Over");
- }
- RET_STATUS DATA_ACTION NewModelDPC::GetDeviceConfig(ResDataObject PARAM_OUT* pDeviceConfig)
- {
- if ((*m_config).GetPropties().size() > 0)
- {
- //mLog::FERROR("GetDeviceConfig has module config do not return anything.");
- return RET_FAILED;
- }
- //mLog::FINFO( "GetDeviceConfig start");
- std::string strDeviceConfig;
- if (m_NewDevManager->GetDeviceConfig(strDeviceConfig))
- {
- pDeviceConfig->decode(strDeviceConfig.c_str());
- //mLog::FINFO( "GetDeviceConfig ok {$}", strDeviceConfig);
- }
- else
- {
- //mLog::FERROR( "GetDeviceConfig failed");
- return RET_FAILED;
- }
- //mLog::FINFO( "GetDeviceConfig Over");
- return RET_SUCCEED;
- }
- RET_STATUS DATA_ACTION NewModelDPC::SetDeviceConfig(ResDataObject PARAM_IN* DeviceConfig)
- {
- if ((*m_config).GetPropties().size() > 0)
- {
- return RET_FAILED;
- }
- //mLog::FINFO( "SetDeviceConfig start");
- std::string strDeviceConfig = DeviceConfig->encode();
- if (m_NewDevManager->SetDeviceConfig(strDeviceConfig))
- {
- }
- else
- {
- return RET_FAILED;
- }
- //mLog::FINFO( "SetDeviceConfig Over");
- return RET_SUCCEED;
- }
|