12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565 |
- // NewModelDPC.cpp : 定义 DLL 应用程序的导出函数。
- //
- #include <iostream>
- #include <sstream>
- #include <filesystem>
- #include <system_error>
- #include <pthread.h>
- #include <dlfcn.h> // 动态库加载
- #include <algorithm> // std::transform
- #include <cstring> // strerror
- #include <unistd.h> // 环境变量操作
- #include <cctype> // toupper
- #include "NewModelDPC.h"
- #include "DevTree.h"
- #include "NewModelDevice.h"
- #include "common_api.h"
- #include "LocalConfig.h"
- #include "ModuleConfig.h"
- #include "LogLocalHelper.h"
- #include "Log4CPP.h"
- 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;
- static 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());
- string strLogPath = GetProcessDirectory() + R"(/Conf/log_config.xml)";
- string LogHost = (string)getRootpath();
- std::string moduleName = "Platform";
- bool ret = initLogModule(
- LogHost, // 主机名(用于日志路径中的{host}占位符)
- moduleName, // 唯一模块名
- strLogPath, // 配置文件路径
- true // 是否输出到控制台(可选)
- );
- if (!ret) {
- std::cerr << "Log init failed!" << std::endl;
- }
- // 绑定当前动态库的模块名(调用自身实现的接口)
- NewModelDPC_SetLocalModuleName(moduleName);
- 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")
- //{
- // FINFO("Try check Grid Device");
- // if (!DongleCheck("1028"))
- // {
- // FWARN("VirtulGrid Dongle check failed...");
- // return false;
- // }
- // else
- // {
- // FINFO("VirtulGrid Dongle check SUCC...");
- // }
- //}
- //else
- //{
- // FINFO("Try check Virutal Device {$} == {$}", devType, devCheck);
- //}
- //if (devCheck == "DAP/DAPX64.DLL" || devCheck == "DAP\\DAPX64.DLL")
- //{
- // FINFO("Try check DAP Device");
- // if (!DongleCheck("1038"))
- // {
- // FWARN("Virtul DAP Dongle check failed...");
- // return false;
- // }
- // else
- // {
- // FINFO("Virtul DAP Dongle check SUCC...");
- // }
- //}
- //else
- //{
- // 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();
- FERROR("Load lib:{$} failed. Err:{$}", devDriverPath, err);
- return false;
- }
- //m_arrImgeFunc = nullptr;
- typedef VirtualDevice* (*CreateVirtuaDeviceAPI)();
- FINFO("Load Driver ok.. {$}", devDriverPath);
- 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)
- {
- 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
- {
- 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)
- {
- FINFO("Only Init Module from Template ");
- FreeVirtualDeviceAPI freeFunc = (FreeVirtualDeviceAPI)dlsym(hModle, "ReleaseVirtualDevice");
- if (freeFunc && pDevice)
- {
- //创建对象,只是模型文件操作
- FINFO(" SetDeviceInfo {$} devSrvPath: {$} ",devPath, devSrvPath);
- pDevice->SetDeviceInfo(devPath, devSrvPath);
- FINFO("First Install Test and check Module Begin ");
- pDevice->SetClientRootID("", devSrvPath.c_str());
- 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**>;
- std::array<imgFuncThreadParams, MAX_IMG_THREAD>& GetParamImgThread() {
- static std::array<imgFuncThreadParams, MAX_IMG_THREAD> instance;
- return instance;
- }
- // 获取线程句柄数组
- std::array<pthread_t, MAX_IMG_THREAD>& GetHImgThread() {
- static std::array<pthread_t, MAX_IMG_THREAD> instance;
- return instance;
- }
- // 获取空闲事件向量
- std::vector<std::shared_ptr<LinuxEvent>>& GetHIdleEvent() {
- static std::vector<std::shared_ptr<LinuxEvent>> instance;
- return instance;
- }
- // 获取数据已读事件向量
- std::vector<std::shared_ptr<LinuxEvent>>& GetHDataReaded() {
- static std::vector<std::shared_ptr<LinuxEvent>> instance;
- return instance;
- }
- // 获取数据就绪事件向量
- std::vector<std::shared_ptr<LinuxEvent>>& GetHDataReadyEvent() {
- static std::vector<std::shared_ptr<LinuxEvent>> instance;
- return instance;
- }
- void* ImgeProcessThread(void* pPara);
- bool NewModelDPC::InitDeviceMgr(const char *pDriverpath)
- {
- std::string strDriverPath = pDriverpath;
- std::string FilePath = GetFileDirectory(strDriverPath);
- std::cout << "InitDeviceMgr: Adding to LD_LIBRARY_PATH: " << FilePath << std::endl;
- if (!AddEnvPath(FilePath.c_str())) {
- std::cerr << "NewModelDPC::InitDeviceMgr: Failed to add library path to LD_LIBRARY_PATH: "
- << FilePath << std::endl;
- }
- m_Module = dlopen(pDriverpath, RTLD_LAZY);
- if (m_Module)
- {
- std::cout << "NewModelDPC::InitDeviceMgr: Successfully loaded driver library: " << pDriverpath << std::endl;
- 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++)
- {
- GetHDataReadyEvent().push_back(LinuxEvent::CreateEvent(LinuxEvent::AUTO_RESET, false));
- GetHIdleEvent().push_back(LinuxEvent::CreateEvent(LinuxEvent::AUTO_RESET, true));
- GetHDataReaded().push_back(LinuxEvent::CreateEvent(LinuxEvent::AUTO_RESET, false));
- std::cout << "NewModelDPC::InitDeviceMgr: Creating image processing thread " << x << std::endl;
- // 创建线程
- //int ret = pthread_create(&GetHImgThread()[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;
- // // 可选:设置线程名称或属性
- //}
- }
- GetHDataReadyEvent().push_back(g_hExit);
- GetHIdleEvent().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);
- 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);
- 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;
- 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;
- 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;
- 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;
- 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;
- 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;
- 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;
- FINFO("Load Configration from Mudle Config [{$}]", moduleConfig);
- }
- catch (...)
- {
- std::cout << "Error: Failed to load configuration from module config: " << moduleConfig << std::endl;
- FERROR("Failed to Load Configration from Driver Config [{$}]", moduleConfig);
- filepath = GetDriverConfigFilePath();
- std::cout << "Trying to load configuration from driver config: " << filepath << std::endl;
- 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;
- 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 (...)
- {
- 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;
- FINFO("m_NewDevManager->DriverEntry {$}", filepath);
- ret = m_NewDevManager->DriverEntry(filepath);
- std::cout << "m_NewDevManager->DriverEntry returned: " << (ret ? "true" : "false") << std::endl;
- 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;
- 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))
- {
- FINFO("OnNotify Platform");
- return dwResult - WAIT_OBJECT_0;
- }
- else
- {
- if (NULL != m_NewDevManager)
- {
- if (!m_NewDevManager->isConnected())
- {
- FWARN("CONNECTED is false");
- if (*m_pConnectionStatus)
- {
- FWARN("Notify disCONNECTED ");
- LogicDriver::DisConnect();
- }
- if (!*m_pConnectionStatus)
- {
- FINFO("Try CONNECT");
- if (m_NewDevManager->Connect())
- {
- FINFO("try CONNECTED is ok");
- LogicDriver::Connect();
-
- FINFO("Notify CONNECTED is ok");
- }
- else
- {
- FWARN("Notify disCONNECTED ");
- LogicDriver::DisConnect();
- FWARN("Try CONNECT failed. ");
- }
- }
-
- }
- else
- {
- if (!*m_pConnectionStatus)
- {
- FINFO("Auto CONNECTED is ok");
- LogicDriver::Connect();
- FINFO("Notify CONNECTED is ok");
- }
- }
- //此处从程序启动后会一直打印,重复调用work函数
- //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();
- FINFO("Try Load xml Config file {$} ", fileName);
- resConf.loadFile(fileName.c_str());
- //string test = resTest.encode();
- if (resConf.GetKeyCount("CONFIGURATION") <= 0)
- {
- 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"]);
- FINFO("New Virtual Device Path {$}", devPath);
- resResponse.add(strServDevPath.c_str(), "");
- if (strServDevPath.length() <= 0)
- strServDevPath = devPath;
- if (!got)
- {
- FINFO("New install VD {$]", strServDevPath);
- //之前没有配置,则需要测试,并设置
- LoadVirtualDevice(vdModuleName.c_str(), false, false, devPath, strServDevPath);
- }
- else
- {
- FINFO("VD had installed aready {$]", strServDevPath);
- for(int x=0;x<m_arrImgeProcessDevices.size();x++)
- if (m_arrImgeProcessDevices[x]->GetCcosRootPath() == strServDevPath)
- {
- //
- 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)
- {
- //
- 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)
- {
- FERROR("GetDeviceType Failed");
- return false;
- }
- //if (m_pNewDevManager->GetDriverType(DriverType) == false)
- //{
- // return false;
- //}
- return string_2_guid(DriverType.c_str(), DevType);
- }
- bool SYSTEM_CALL NewModelDPC::Driver_Probe(ResDataObject& PARAM_OUT HardwareInfo)
- {
- //string DriverInfo = m_NewDevManager->DriverProbe();
- 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
- //{
- // 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);
- FINFO("ModuleDriver : GetDeviceResource {$}", pDeviceResource->encode());
- //BaseLayerConfig = *pDeviceResource;
- }
- std::string ResInfo;// = m_NewDevManager->GetResource();
- 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)
- {
- 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);
- 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;
- 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();
- 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
- //{
- // 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
- FINFO("Connect Succeed");
- return true;
- }
- else
- {
- 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
- 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 >= GetHDataReadyEvent().size()) {
- std::cout << "[" << getCurrentTime() << "] ERROR: ImgeProcessThread: nIndex is negative (" << nIndex << "), thread exit." << std::endl;
- return nullptr;
- }
- imgFuncThreadParams* pParam = GetParamImgThread().data() + nIndex;
- std::vector<std::shared_ptr<LinuxEvent>> hWait;
- if (!GetHDataReadyEvent()[nIndex]) {
- std::cout << "[" << getCurrentTime() << "] ERROR: ImgeProcessThread: GetHDataReadyEvent()[" << nIndex << "] is null, thread exit." << std::endl;
- return nullptr;
- }
- hWait.push_back(GetHDataReadyEvent()[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);
- GetHDataReaded()[nIndex]->SetEvent();
- if (func != nullptr)
- {
- //string temp = std::get<3>(*pParam);
- for (int x = 0; x < nNum; x++)
- {
- if (func[x] == nullptr)
- {
- FERROR("Thread[{$}] ImageProcessFunc is null with devpath= {$}", nIndex, pDev[x]->GetCcosRootPath());
- continue;
- }
- 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]);
- FINFO("Thread[{$}] ImageProcessFunc Over with devpath= {$}", nIndex, pDev[x]->GetCcosRootPath());
- }
- }
- else
- {
- FERROR("Thread[{$}] ImageProcessFunc arrpoint is null ", nIndex);
- }
- GetHIdleEvent()[nIndex]->SetEvent();
- }
- else if(dwWait == WAIT_OBJECT_0 + 1)
- {
- 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)
- {
- 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(GetHIdleEvent(), -1);
- FINFO("Wait for ImageProcessThrad Idle return {$}", dwWait);
- if (dwWait != WAIT_TIMEOUT && dwWait >= WAIT_OBJECT_0 && dwWait < WAIT_OBJECT_0 + MAX_IMG_THREAD)
- {
- imgFuncThreadParams* pParam = GetParamImgThread().data() + (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();
- FINFO("Notify ImageProcessThread [{$}] work ", dwWait - WAIT_OBJECT_0);
- GetHDataReadyEvent()[dwWait - WAIT_OBJECT_0]->SetEvent();
- //等待线程 读取完数据 ,就可以下一个
- FINFO("Start Wait {$} Thread to Read Data.", m_arrImgeFunc.size());
- DWORD dwRet = GetHDataReaded()[dwWait - WAIT_OBJECT_0]->Wait(4000);
- //if (dwRet == WAIT_OBJECT_0)
- FINFO("Over Succeded with ret {$}", dwRet);
- //else
- FERROR("Over Timeout with ret {$}", dwRet);
- }
- else
- {
- 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)
- {
- 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);
- }
- FINFO("End");
- return ret ;
- }
- void InitLogicDevice(LogicDevice* p, string guidname)
- {
- }
- PVOID NewModelDPC::LoadLogicDevices()
- {
- std::cout << "**************** NewModelDPC :: LoadLogicDevices" << std::endl;
- 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"];
- FINFO("Have {$} Devices", nDeviceNumber);
- }
- else
- {
- 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
- {
- 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;
- FINFO("Got MainHost {$} of {$}", hostName, ccospath);
- }
- }
- if (isMain && m_MainHostDevice == nullptr)
- {
- m_MainHostDevice = p;
- 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);
- FINFO("Try add ccos child {$} ", ccospatht);
- m_MainHostDevice->AddCcosChildren(m_arrImgeProcessDevices[x], ccospatht.c_str());
- }
- 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);
- 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());
- FINFO(" SetClientRootID ", ccospath);
- //p->SetClientRootID();
- FINFO("Get IoDevice Succeed");
- p->SetDrvDPC(this);
- std::shared_ptr<LinuxEvent> DisconnectEvt = GetPassiveDisConnectEvtHandle();
- //p->ConnectMQTTServer();
- p->Init(std::move(pIoDevice), DisconnectEvt);
- //p->SubscribeAction();
- FINFO("Create IoDevice Succeed");
- }
- else
- {
- 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());
- 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());
- 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;
- FINFO("Exchange Vd [{$}] <=> [{$}]", x, y);
- }
- }
- }
- //*/
- LogicDevice *pret = (LogicDevice *)p;
- pTree->Add((PVOID)pret, TYPE_DEVICE);
- LogicDevice::AddEbusChildren(p, "");
- }
- return (PVOID)pTree;
- }
- void NewModelDPC::UnloadLogicDevices()
- {
- 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();
- FINFO( "UnLoad Over");
- }
- RET_STATUS DATA_ACTION NewModelDPC::GetDeviceConfig(ResDataObject PARAM_OUT* pDeviceConfig)
- {
- if ((*m_config).GetPropties().size() > 0)
- {
- FERROR("GetDeviceConfig has module config do not return anything.");
- return RET_FAILED;
- }
- FINFO( "GetDeviceConfig start");
- std::string strDeviceConfig;
- if (m_NewDevManager->GetDeviceConfig(strDeviceConfig))
- {
- pDeviceConfig->decode(strDeviceConfig.c_str());
- FINFO( "GetDeviceConfig ok {$}", strDeviceConfig);
- }
- else
- {
- FERROR( "GetDeviceConfig failed");
- return RET_FAILED;
- }
- 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;
- }
- FINFO( "SetDeviceConfig start");
- std::string strDeviceConfig = DeviceConfig->encode();
- if (m_NewDevManager->SetDeviceConfig(strDeviceConfig))
- {
- }
- else
- {
- return RET_FAILED;
- }
- FINFO( "SetDeviceConfig Over");
- return RET_SUCCEED;
- }
|