12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786 |
- #include "common_api.h"
- #include "DriverManager.h"
- #include "DPC.h"
- #include "CDI.h"
- #include "DrvTree.h"
- //#include "logger.h"
- #include "LocalConfig.h"
- #include "BusUnitClient.h"
- #include "LogicDriver.h"
- #include "HandleManager.h"
- #include "PacketAnalizer.h"
- #include <iostream>
- typedef DriverDPC* (*GetDriverDPC)();
- typedef void(*ReleaseDriverDPC)(DriverDPC *p);
- //extern Log4CPP::Logger* mLog::gLogger;
- DriverManager g_DrvManager;
- DriverHandle::DriverHandle()
- {
- m_DllFileHandle = 0;
- m_pRootDevice = 0;
- m_pDynRootDevice = 0;
- m_pDPC = 0;
- m_HandleStatus = false;
- m_AsRootDriver = false;
- //m_pLog = CreateLogger();
- //m_pDynLog = CreateLogger();
- m_IsRootHandle = false;
- //m_pParent = NULL;
- }
- DriverHandle::~DriverHandle()
- {
- UnloadDriver();
- }
- void DriverHandle::InitDynLogForDPC(DriverHandle *pParent)
- {
- //log init
- //ResDataObject val;
- //string FullLogFileName;
- //if (pParent->m_DllFileTitle.size() > 0)
- //{
- // m_DllFileTitle = pParent->m_DllFileTitle;
- // FullLogFileName = m_DynLogPath + m_DllFileTitle + string(".log");
- //}
- //else
- //{
- // FullLogFileName = m_DynLogPath + "drvlog.log";
- //}
- //m_pDynLog->SetLogFilepath(FullLogFileName.c_str());
- //if (pParent == NULL)
- //{
- // if (TryGetValue(m_DrvConfig, "LOGLEVEL", val))
- // {
- // Logger_Pattern lp;
- // m_pDynLog->GetLogPattern(lp);
- // if (lp.SetLevel((LONG)val))
- // {
- // m_pDynLog->SetLogPattern(lp);
- // }
- // }
- // //dpc logger
- // m_pDPC->SetDynLogHandle(m_pDynLog);
- //}
- //else// if (pParent != this)
- //{
- // if (m_pDPC)
- // {
- // LONG Level;
- // Logger_Pattern lp;
- // pParent->m_pLog->GetLogPattern(lp);
- // Level = lp.m_Level;
- // m_pDynLog->GetLogPattern(lp);
- // lp.SetLevel(Level);
- // m_pDynLog->SetLogPattern(lp);
- // //dpc logger
- // m_pDPC->SetDynLogHandle(m_pDynLog);
- // }
- //}
- }
- void DriverHandle::InitLogForDPC(DriverHandle *pParent)
- {
- //log init
- //ResDataObject val;
- //string FullLogFileName;
- //if (pParent == NULL)
- //{
- // if (TryGetValue(m_DrvConfig, "LOGLEVEL", val))
- // {
- // Logger_Pattern lp;
- // m_pLog->GetLogPattern(lp);
- // if (lp.SetLevel((LONG)val))
- // {
- // m_pLog->SetLogPattern(lp);
- // }
- // }
- // //dpc logger
- // ((DriverDPC*)m_pDPC)->SetLogHandle(m_pLog);
- //}
- ////else if (pParent == this)
- ////{
- ////}
- //else
- //{
- // if (pParent->m_DllFileTitle.size() > 0)
- // {
- // m_DllFileTitle = pParent->m_DllFileTitle;
- // FullLogFileName = m_LogPath + m_DllFileTitle + string(".log");
- // }
- // else
- // {
- // FullLogFileName = m_LogPath + "drvlog.log";
- // }
- // m_pLog->SetLogFilepath(FullLogFileName.c_str());
- // if (m_pDPC)
- // {
- // Logger_Pattern lp;
- // pParent->m_pLog->GetLogPattern(lp);
- // m_pLog->SetLogPattern(lp);
- // //dpc logger
- // ((DriverDPC*)m_pDPC)->SetLogHandle(m_pLog);
- // }
- //}
- }
- bool DriverHandle::GetThreadsLogger(DWORD ThreadId,PVOID &pLogger)
- {
- //if (m_NotifyThread.GetTID() == ThreadId)
- //{
- // pLogger = m_pLog;
- // return true;
- //}
- return false;
- }
- bool DriverHandle::InitDynSubDriverObject()
- {
- if (m_pDynRootDevice)
- {
- //loop drvtree
- size_t Size = m_pDynRootDevice->size();
- for (size_t i = 0; i < Size; i++)
- {
- DevTreeNode node = (*m_pDynRootDevice)[i];
- if (node.m_NodeType == TYPE_DRIVER)
- {
- LogicDriver *pDPC = (LogicDriver*)node.m_pObject;
- DriverHandle *pSubHandle = new DriverHandle();
- if (DynInitHandle(pSubHandle, pDPC))
- {
- m_DynSubDriverList.PushBack(pSubHandle);
- }
- else
- {
- return false;
- }
- }
- }
- }
- return true;
- }
- bool DriverHandle::InitSubDriverObject()
- {
- //mLog::FINFO("CDI ");
- if (m_pRootDevice)
- {
- //loop drvtree
- size_t Size = m_pRootDevice->size();
- //mLog::FINFO("CDI ----- DriverHandle::InitSubDriverObject {$} devices", Size);
- for (size_t i = 0; i < Size; i++)
- {
- DevTreeNode node = (*m_pRootDevice)[i];
- if (node.m_NodeType == TYPE_DRIVER)
- {
- //mLog::FINFO("CDI ----- DriverHandle::InitSubDriverObject InitHandle(pSubHandle, pDPC) again {$}", (int)node.m_NodeType );
- LogicDriver*pDPC = (LogicDriver*)node.m_pObject;
- DriverHandle *pSubHandle = new DriverHandle();
- if (InitHandle(pSubHandle, pDPC))
- {
- m_hSubDriverList.PushBack(pSubHandle);
- }
- else
- {
- //mLog::FINFO("CDI ---- DriverHandle::InitSubDriverObject InitHandle(pSubHandle, pDPC) failed", (int)node.m_NodeType);
- return false;
- }
- }
- else {
- //mLog::FINFO("CDI ----- DriverHandle::InitSubDriverObject node.m_NodeType", (int)node.m_NodeType);
-
- }
- }
- }
- return true;
- }
- //这是一个嵌套函数,用的时候要小心
- bool DriverHandle::DynInitHandle(DriverHandle *pSubHandle, LogicDriver*pDPC)
- {
- bool ret = false;
- string procdir;
- if (m_pDynRootDevice != nullptr)
- return true;
- procdir = GetProcessDirectory();
- pSubHandle->m_pDPC = pDPC;
- std::cout << "CDI ----- DriverHandle::DynInitHandle" << std::endl;
- //mLog::FINFO("CDI ----- DriverHandle::DynInitHandle");
- //Probe
- //mLog::FINFO("Device_Probe ");
- if (pDPC->Device_Probe(pSubHandle->m_DynProbe))
- {
- //mLog::FINFO("CDI After Device_Probe");
- ResDataObject ProcDirRes;
- ProcDirRes = procdir.c_str();
- pSubHandle->m_DynWorkPath = (const char*)MakeWorkPath(pSubHandle->m_DynProbe, pSubHandle->m_Connection, ProcDirRes,true);
- pSubHandle->m_DynLogPath = (const char*)MakeWorkPath(pSubHandle->m_DynProbe, pSubHandle->m_Connection, ProcDirRes, false);
- if (pSubHandle->m_DynWorkPath.size() > 0)
- {
- pSubHandle->InitDynLogForDPC(this);
- if (pDPC->SetDeviceWorkPath(pSubHandle->m_DynWorkPath.c_str()))
- {
- //mLog::FINFO("CDI pDPC->LoadLogicDevices ");
- PVOID pDevice = pDPC->LoadLogicDevices();
- if (pDevice)
- {
- pSubHandle->m_pDynRootDevice = (DevTree*)pDevice;
- //先假设驱动对象不能动态生成,只能以静态的形式存在!!!
- //do init Driver Object first
- //if (pSubHandle->InitDynSubDriverObject() == false)
- //{
- // //mLog::FERROR( "InitSubDriverObject Failed.\n");
- // return false;
- //}
- //mLog::FINFO("CDI do init device Object second");
- //do init device Object second
- if (DynMakeDevicePaths())
- {
- //mLog::FINFO("CDI DynMakeDeviceSysIFsAndThreads");
- if (DynMakeDeviceSysIFsAndThreads())
- {
- return true;
- }
- else
- {
- //DWORD errNo = GetLastError();
- //mLog::FERROR( "MakeDeviceSysIFsAndThreads Failed.ErrNo:{$}\n", errNo);
- return false;
- }
- }
- else
- {
- //DWORD errNo = GetLastError();
- //mLog::FERROR( "MakeDevicePaths Failed.ErrNo:{$}\n", errNo);
- return false;
- }
- }
- //先假设驱动对象不能动态生成,只能以静态的形式存在!!!
- //为Sub对象 创建Driver线程和Notify线程
- //if (pSubHandle->DynMakeDriverPaths())
- //{
- // if (pSubHandle->DynMakeDriverSysIFsAndThreads())
- // {
- // return true;
- // }
- // else
- // {
- // DWORD errNo = GetLastError();
- // //mLog::FERROR( "MakeDriverSysIFsAndThreads Failed.ErrNo:%d\n", errNo);
- // return false;
- // }
- //}
- //else
- //{
- // DWORD errNo = GetLastError();
- // //mLog::FERROR( "MakeDriverPaths Failed.ErrNo:%d\n", errNo);
- // return false;
- //}
- }
- else
- {
- //DWORD errNo = GetLastError();
- //mLog::FERROR( "SetWorkPath Failed.ErrNo:{$}\n", errNo);
- }
- }
- else
- {
- //DWORD errNo = GetLastError();
- //mLog::FERROR( "Probe Info not right.ErrNo:{$}\n", errNo);
- }
- }
- else
- {
- //DWORD errNo = GetLastError();
- //mLog::FERROR( "Call Probe Failed.ErrNo:{$}\n", errNo);
- }
- return false;
- }
- //这是一个嵌套函数,用的时候要小心
- bool DriverHandle::InitHandle(DriverHandle *pSubHandle, LogicDriver*pDPC)
- {
- bool ret = false;
- string procdir;
- procdir = GetProcessDirectory();
- //Logger *p = GetGlobalLogger();
- //mLog::FINFO( "CDI DriverHandle::InitHandle");
- std::cout << "CDI: DriverHandle::InitHandle" << std::endl;
- pSubHandle->m_pDPC = pDPC;
- //Probe
- std::cout << "CDI: First pDPC->Driver_Probe" << std::endl;
- //mLog::FINFO("CDI First pDPC->Driver_Probe");
- if (pDPC->Driver_Probe(pSubHandle->m_Probe))
- {
- std::cout << "CDI: After pDPC->Driver_Probe" << std::endl;
- //mLog::FINFO("CDI After pDPC->Driver_Probe");
- if (!m_AsRootDriver)
- {
- std::cout << "CDI: !m_AsRootDriver" << std::endl;
- //mLog::FINFO("CDI !m_AsRootDriver");
- GUID guid;
- string guidname;
- ((LogicDriver*)m_pDPC)->GetDeviceType(guid);
- guid_2_string(guid, guidname);
- ResDataObject subpath;
- subpath = (guidname).c_str();
- string fullpath = MakeDevicePath(m_Probe, m_Connection, subpath).encode();
- string ccospath = MakeCcosPath(m_Probe, m_Connection, subpath, true).encode();
- //pDPC->SetEbusRoot(fullpath.c_str());
- pDPC->SetClientRootID(fullpath.c_str(), ccospath.c_str());
- std::cout << "CDI: pDPC->SetClientRootID fullpath:[" << fullpath
- << "] ccospath:[" << ccospath << "] use subpath["
- << (const char*)subpath << "] Real CCOS path ["
- << pDPC->GetCcosRootPath() << "]" << std::endl;
- //mLog::FINFO("CDI pDPC->SetClientRootID fulpath:[{$}] ccospath:[{$}] use subpath[{$}] Real CCOS path [{$}]", fullpath, ccospath, (const char*)subpath, pDPC->GetCcosRootPath());
- g_DrvManager.AddDevice(pDPC->GetCcosRootPath().c_str(), pDPC);
- g_DrvManager.AddDevice(pDPC->GetCcosAbstractPath().c_str(), pDPC);
- }
- std::cout << "CDI: SetWorkPath" << std::endl;
- //mLog::FINFO("CDI SetWorkPath");
- ResDataObject ProcDirRes;
- ProcDirRes = procdir.c_str();
- pSubHandle->m_WorkPath = (const char*)MakeWorkPath(pSubHandle->m_Probe, pSubHandle->m_Connection, ProcDirRes,true);
- pSubHandle->m_LogPath = (const char*)MakeWorkPath(pSubHandle->m_Probe, pSubHandle->m_Connection, ProcDirRes, false);
- if (pSubHandle->m_WorkPath.size() > 0)
- {
- pSubHandle->InitLogForDPC(this);
- if (pDPC->SetDriverWorkPath(pSubHandle->m_WorkPath.c_str()))
- {
- PVOID pDevice = NULL;
- if(pDPC->GetDPCType() == DPCDRIVER_NORMAL)
- {
- std::cout << "CDI: ----- DriverHandle::InitHandle DPCDRIVER_NORMAL" << std::endl;
- //mLog::FINFO("CDI ----- DriverHandle::InitHandle DPCDRIVER_NORMAL");
-
- pDevice = pDPC->LoadDriver(); //从DPC 上拿到设备树
- std::cout << "CDI: pDPC->LoadDriver over." << std::endl;
- //mLog::FINFO("CDI pDPC->LoadDriver over.");
- }
- else// if (pDPC->GetDPCType() == DPCDRIVER_MDPC)
- {
- std::cout << "CDI: ----- DriverHandle::InitHandle ??? DPCDRIVER_MDPC ??" << std::endl;
- //mLog::FINFO("CDI ----- DriverHandle::InitHandle ??? DPCDRIVER_MDPC ??");
- //no load driver method calling.
- }
- //Step1:do init subdriver first
- if (pDevice)
- {
- std::cout << "CDI: Got DevTree" << std::endl;
- //mLog::FINFO("CDI Got DevTree");
- pSubHandle->m_pRootDevice = (DevTree*)pDevice;
- //pDevice 以及有这个树上的子节点了
- for (int x = 0; x < pSubHandle->m_pRootDevice->size(); x++) {
- LogicDriver* pChild = (LogicDriver*)((*(pSubHandle->m_pRootDevice))[x].m_pObject);
- LogicDevice* pDevice = (LogicDevice*)((*(pSubHandle->m_pRootDevice))[x].m_pObject);
- std::cout << "CDI: " << x << " Node " << std::endl;
- //mLog::FINFO("CDI {$} Node ", x);
- //*/
- if (!m_AsRootDriver) {
- std::cout << "CDI: !m_AsRootDriver true" << std::endl;
- //mLog::FINFO("CDI !m_AsRootDriver true");
- //LogicDevice* pDevice = (LogicDevice*)pChild;
- GUID guid2;
- string guidname;
- //((LogicDevice*)pChild)->GetDeviceType(guid);
- pDevice->GetDeviceType(guid2);
- guid_2_string(guid2, guidname);
- ResDataObject subpath;
- subpath = (guidname).c_str();
- string fullpath = MakeDevicePath(m_Probe, m_Connection, subpath).encode();
- //pDevice->SetEbusRoot(fullpath.c_str());
- string ccospath = MakeCcosPath(m_Probe, m_Connection, subpath).encode();
- pDevice->SetClientRootID(fullpath.c_str(), ccospath.c_str());
- std::cout << "CDI: pDevice->SetClientRootID fullpath :[" << fullpath
- << "] ccos path [" << ccospath
- << "] Real CCOS path [" << pDevice->GetCcosRootPath() << "] " << std::endl;
- //mLog::FINFO("CDI pDevice->SetClientRootID fullpath :[{$}] ccos path [{$}] Real CCOS path [{$}] ", fullpath, ccospath, pDevice->GetCcosRootPath());
- g_DrvManager.AddDevice(pDevice->GetCcosAbstractPath().c_str(), pDevice);
- g_DrvManager.AddDevice(pDevice->GetCcosRootPath().c_str(), pDevice);
- }
- else
- {
- string path = string(getRootpath());
- pDPC->SetClientRootID(path.c_str(), path.c_str() + 1);
- //mLog::FINFO("CDI Bus pDPC->SetClientRootID fulpath:[{$}] ccospath:[{$}] Real CCOS path [{$}]", path, path.c_str() + 1, pDPC->GetCcosRootPath());
- std::cout << "CDI: Bus pDPC->SetClientRootID fulpath:[" << path
- << "] ccospath:[" << (path.c_str() + 1)
- << "] Real CCOS path [" << pDPC->GetCcosRootPath() << "]" << std::endl;
- const std::string& temppath = pDevice->GetCcosRootPath();
- if (!temppath.empty()) {
- g_DrvManager.AddDevice(temppath.c_str(), pDevice);
- }
- else {
- // 添加错误处理日志
- std::cerr << "ERROR: Empty path for device " << pDevice << std::endl;
- }
- std::cout << "CDI: ----- g_DrvManager.AddDevice over" << std::endl;
- }
- std::cout << "CDI: ----- pDPC->AddEbusChildren" << std::endl;
- pDPC->AddEbusChildren(pChild, pChild->GetRootPath().c_str());
- //*/
- }
-
- //do init Driver Object first
- std::cout << "CDI: ----- DriverHandle::InitHandle DPCDRIVER_NORMAL" << std::endl;
- //mLog::FINFO("CDI ----- DriverHandle::InitHandle DPCDRIVER_NORMAL");
- if (pSubHandle->InitSubDriverObject() == false)
- {
- std::cout << "CDI: ----- DriverHandle::InitHandle InitSubDriverObject Failed" << std::endl;
- std::cout << "InitSubDriverObject Failed." << std::endl;
- //mLog::FINFO("CDI ----- DriverHandle::InitHandle InitSubDriverObject Failed");
- //mLog::FERROR( "InitSubDriverObject Failed.\n");
- return false;
- }
- }
- //Step2:do init this driver second
- //为Sub对象 创建Driver线程和Notify线程
-
- if (pSubHandle->MakeDriverPaths())
- {
- if (pSubHandle->MakeDriverSysIFsAndThreads() == false)
- {
- int errNo = errno; // Linux使用errno获取错误码
- std::cout << "CDI: ----- DriverHandle::InitHandle MakeDriverSysIFsAndThreads Failed.ErrNo " << errNo
- << " (" << strerror(errNo) << ")" << std::endl; // strerror将错误码转为描述字符串
- std::cout << "MakeDriverSysIFsAndThreads Failed.ErrNo:" << errNo
- << " (" << strerror(errNo) << ")" << std::endl;
- //mLog::FINFO("CDI ----- DriverHandle::InitHandle MakeDriverSysIFsAndThreads Failed.ErrNo {$}", errNo);
- //mLog::FERROR( "MakeDriverSysIFsAndThreads Failed.ErrNo:{$}\n", errNo);
- return false;
- }
- }
- else
- {
- int errNo = errno;
- std::cout << "CDI: ----- DriverHandle::InitHandle MakeDriverPaths Failed.ErrNo " << errNo
- << " (" << strerror(errNo) << ")" << std::endl;
- std::cout << "MakeDriverPaths Failed.ErrNo:" << errNo
- << " (" << strerror(errNo) << ")" << std::endl;
- //mLog::FINFO("CDI ----- DriverHandle::InitHandle MakeDriverPaths Failed.ErrNo {$}", errNo);
- //mLog::FERROR( "MakeDriverPaths Failed.ErrNo:{$}\n", errNo);
- return false;
- }
- //Step3:do init sub devices third
- if (pDevice)
- {
- std::cout << "CDI: do init device Object second" << std::endl;
- //mLog::FINFO("CDI do init device Object second");
- //do init device Object second
- if (pSubHandle->MakeDevicePaths())
- {
- if (pSubHandle->MakeDeviceSysIFsAndThreads())
- {
- std::cout << "CDI: pSubHandle->MakeDeviceSysIFsAndThreads" << std::endl;
- //mLog::FINFO("CDI pSubHandle->MakeDeviceSysIFsAndThreads");
- return true;
- }
- else
- {
- int errNo = errno;
- std::cout << "CDI: ----- DriverHandle::InitHandle MakeDeviceSysIFsAndThreads Failed.ErrNo " << errNo
- << " (" << strerror(errNo) << ")" << std::endl;
- std::cout << "MakeDeviceSysIFsAndThreads Failed.ErrNo:" << errNo
- << " (" << strerror(errNo) << ")" << std::endl;
- //std::cout << "CDI ----- DriverHandle::InitHandle MakeDeviceSysIFsAndThreads Failed.ErrNo" << errNo << std::endl;
- //mLog::FERROR( "MakeDeviceSysIFsAndThreads Failed.ErrNo:{$}\n", errNo);
- return false;
- }
- }
- else
- {
- int errNo = errno;
- std::cout << "CDI: ----- DriverHandle::InitHandle MakeDevicePaths Failed.ErrNo " << errNo
- << " (" << strerror(errNo) << ")" << std::endl;
- std::cout << "MakeDevicePaths Failed.ErrNo:" << errNo
- << " (" << strerror(errNo) << ")" << std::endl;
- //std::cout << "CDI ----- DriverHandle::InitHandle MakeDevicePaths Failed.ErrNo" << errNo << std::endl;
- //mLog::FERROR( "MakeDevicePaths Failed.ErrNo:{$}\n", errNo);
- return false;
- }
- }
- return true;
- }
- else
- {
- int errNo = errno;
- std::cout << "CDI: ----- DriverHandle::InitHandle SetDriverWorkPath Failed.ErrNo " << errNo
- << " (" << strerror(errNo) << ")" << std::endl;
- std::cout << "SetWorkPath Failed.ErrNo:" << errNo
- << " (" << strerror(errNo) << ")" << std::endl;
- //std::cout << "CDI ----- DriverHandle::InitHandle" << errNo << std::endl;
- //mLog::FERROR( "SetWorkPath Failed.ErrNo:{$}\n", errNo);
- }
- }
- else
- {
- int errNo = errno;
- std::cout << "CDI: ----- DriverHandle::InitHandle Probe Info not right.ErrNo " << errNo
- << " (" << strerror(errNo) << ")" << std::endl;
- std::cout << "Probe Info not right.ErrNo:" << errNo
- << " (" << strerror(errNo) << ")" << std::endl;
- //std::cout << "CDI ----- DriverHandle::InitHandle Probe Info not right.ErrNo" << errNo << std::endl;
- //mLog::FERROR( "Probe Info not right.ErrNo:{$}\n", errNo);
- }
- }
- else
- {
- int errNo = errno;
- std::cout << "CDI: ----- DriverHandle::InitHandle Call Probe Failed.ErrNo " << errNo
- << " (" << strerror(errNo) << ")" << std::endl;
- std::cout << "Call Probe Failed.ErrNo:" << errNo
- << " (" << strerror(errNo) << ")" << std::endl;
- //std::cout << "CDI ----- DriverHandle::InitHandle Call Probe Failed.ErrNo" << errNo << std::endl;
- //mLog::FERROR( "Call Probe Failed.ErrNo:{$}\n",errNo);
- }
- return false;
- }
- bool DriverHandle::IsDPCMatch(LogicDriver*pDriver)
- {
- return (m_pDPC == pDriver);
- }
- /// <summary>
- /// 驱动统一加载入口,使用DPC
- /// </summary>
- /// <param name="config"></param>
- /// <returns></returns>
- bool DriverHandle::LoadDriver(ResDataObject &config)
- {
- string procdir;
- string drvpath;
- string root;
- procdir = GetProcessDirectory();
- //Logger *p = GetGlobalLogger();
- //mLog::FINFO("LoadDriver Begin \n");
- std::cout << "------------ CDI: DriverHandle::LoadDriver ResDataObject" << std::endl;
- try {
- //load drvpath
- m_DrvConfig = config["CONFIGURATION"];
- drvpath = (const char *)m_DrvConfig["driver"];
- root = (const char*)m_DrvConfig["MajorID"];
- string temp("%ROOT%");
- drvpath = ReplaceSubString(drvpath, temp, procdir);
- m_DllFileTitle = GetFileTitle(drvpath);
- string FilePath = GetFileDirectory(drvpath);
- AddEnvPath(FilePath.c_str());
- //oemdriver
- int oemidx = m_DrvConfig.GetFirstOf("oemdriver");
- if (oemidx >= 0)
- {
- //exist
- string oemdrvpath = (const char *)m_DrvConfig["oemdriver"];
- string temp("%ROOT%");
- oemdrvpath = ReplaceSubString(oemdrvpath, temp, procdir);
- FilePath = GetFileDirectory(oemdrvpath);
- AddEnvPath(FilePath.c_str());
- m_DrvConfig["oemdriver"] = oemdrvpath.c_str();
- }
- else
- {
- m_DrvConfig.add("oemdriver", "");
- }
- TryGetValue(m_DrvConfig, "connections", m_Connection);
- //mLog::FINFO("GetDriver ok \n");
- }
- catch (ResDataObjectExption &exp)
- {
- //exp.what()
- //mLog::FINFO( exp.what());
- //mLog::FINFO( "Unknown Exp Happened while load drvfile:{$}\n", drvpath.c_str());
- return false;
- }
- catch (...)
- {
- //mLog::FINFO("Unknown Exp Happened while load drvfile:{$}\n", drvpath.c_str());
- return false;
- }
- m_DllFileHandle = dlopen(drvpath.c_str(), RTLD_LAZY);
- std::cout << "CDI: LoadLibrary :"<< drvpath << std::endl;
- //mLog::FINFO("CDI: LoadLibrary {$} ", drvpath.c_str());
- if (m_DllFileHandle)
- {
- std::cout << "load drvfile:" << drvpath << " OK" << std::endl;
- //mLog::FINFO("load drvfile:{$} OK \n", drvpath.c_str());
- typedef LogicDriver* (*GetDriverDPC)();
- GetDriverDPC dpcfunc = (GetDriverDPC)dlsym(m_DllFileHandle, "GetDriverDPC");
- const char* dlsym_error = dlerror();
- if (!dlsym_error)
- {
- std::cout << "Successfully obtained 'GetDriverDPC' function address" << std::endl;
- //mLog::FINFO("GetDriverDPC FUNC ADDR :{$} OK \n", drvpath.c_str());
- //第一次调用的是OemBusUint DPC
- //第二次调用是真实的DPC,如ImageSave/NewModuleDPC
- m_pDPC =(LogicDriver*) dpcfunc();
- if (m_pDPC)
- {
- std::cout << "Successfully obtained driver instance from 'GetDriverDPC'" << std::endl;
- //mLog::FINFO("GetDriverDPC CALL OK \n");
- //取到了DPC->LogicDriver的子类
- LogicDriver* pDriver = (LogicDriver*)m_pDPC;
-
-
- m_pDPC->SetDriverConfigFilePath(m_DriverConfigFilePath.c_str());
- //m_NotifyThread.m_pDPC = m_pDPC;//init dpc notify thread
- std::cout << "Calling DriverEntry with config: " << m_DrvConfig.encode() << std::endl;
- if (m_pDPC->DriverEntry(m_DrvConfig))
- {
- std::cout << "DriverEntry called successfully" << std::endl;
- //mLog::FINFO("DriverEntry CALL OK \n");
- std::cout << "------------ CDI: InitHandle start" << std::endl;
- InitLogForDPC(NULL);//for me
- if (InitHandle(this, m_pDPC))
- {
- //mLog::FINFO("InitHandle CALL OK \n");
- m_HandleStatus = true;
- m_IsRootHandle = true;
- //mLog::FINFO("LoadDriver Over \n");
- std::cout << "------------ CDI: InitHandle ok" << std::endl;
- return true;
- //return InitLoadDynDriver();
- }
- std::cout << "------------ CDI: InitHandle failed" << std::endl;
- //mLog::FINFO("lib:{$} Load Driver Failed.\n", drvpath.c_str());
- }
- else
- {
- std::cerr << "DriverEntry failed: " << dlerror() << std::endl;
- }
- }
- else
- {
- std::cerr << "No DPC object returned: " << dlerror() << std::endl;
- }
- }
- else
- {
- std::cerr << "Cannot load symbol GetDriverDPC: " << dlsym_error << std::endl;
- dlclose(m_DllFileHandle);
- //mLog::FINFO( "lib:{$} no GetDriverDPC entry.ErrNo:{$}\n", drvpath.c_str(), errNo);
- }
- }
- else
- {
- std::cerr << "Cannot load library: " << dlerror() << std::endl;
- //mLog::FINFO("Load lib:{$} failed.ErrNo:{$}\n", drvpath.c_str(), errNo);
- }
- //mLog::FINFO("CDI: InitHandle fail\n");
- std::cerr << "CDI: InitHandle fail" << std::endl;
- return false;
- }
- //Process:x64,x32
- //StartType:ignore
- //LoadType:module, or new
- //drvtype:lead,standalone,subsystem,device
- //driver:dpcpath
- //entrysource:xxx(it must load to memory)
- //connection:connections
- bool DriverHandle::LoadDriver(const char *pConfigFilepath, bool bCommonDPC)
- {
- ResDataObject config;
- //Logger *p = GetGlobalLogger();
- m_bCommonDPC = bCommonDPC;
- //NOT FINISHED YET
- //ignore all other shit,focus on the drvpath
- std::cout << "------------ CDI: DriverHandle::LoadDriver" << pConfigFilepath << std::endl;
- try {
- if (config.loadFile(pConfigFilepath) == false)
- {
- //log here
- return false;
- }
- m_DriverConfigFilePath = pConfigFilepath;
- //for file path
- //int idx = config["CONFIGURATION"].GetFirstOf("DRIVERCONFIGFILEPATH");
- //if (idx >= 0)
- //{
- // config["CONFIGURATION"].update("DRIVERCONFIGFILEPATH", pConfigFilepath);
- //}
- //else
- //{
- // config["CONFIGURATION"].add("DRIVERCONFIGFILEPATH", pConfigFilepath);
- //}
- return LoadDriver(config);
- }
- catch (ResDataObjectExption &exp)
- {
- cout << "What happpend " << exp.what() << endl;
- //exp.what()
- //mLog::FERROR( exp.what());
- }
- catch (...)
- {
- //mLog::FERROR( "Unknown Exp Happened\n");
- }
-
- return false;
- }
- void DriverHandle::MakeShortPaths(string stackpath, DevTree *pDevTree, map<string, vector<LogicDevice*>> &ShortpathMap)
- {
- if (stackpath.size() > 0)
- {
- stackpath += "/";
- }
- stackpath += pDevTree->GetName();
- //loop drvtree
- size_t Size = pDevTree->size();
- for (size_t i = 0; i < Size; i++)
- {
- DevTreeNode node = (*pDevTree)[i];
- if (node.m_NodeType == TYPE_DEVICE)
- {
- GUID guid;
- string name;
- string shortname;
- LogicDevice *p = (LogicDevice *)node.m_pObject;
- p->GetDeviceType(guid);
- guid_2_string(guid, name);
- shortname = stackpath + string("/") + name;
- ShortpathMap[shortname].push_back(p);
- }
- else
- {
- //it's a tree
- MakeShortPaths(stackpath, (DevTree*)node.m_pObject, ShortpathMap);
- }
- }
- }
- bool DriverHandle::DynMakeDriverPaths()
- {
- //assert(0);//NOT FINISHED YET
- return true;
- }
- bool DriverHandle::MakeDriverPaths()
- {
- //assert(0);
- //子逻辑设备情况,会有多个设备,且有GUID重复的设备,所以需要map
- //GUID\1,GUID\2这种名字区分开来
- //但是驱动对象就只有一个,就是本驱动
- //路径是 本驱动::Probe+GUID
- //详细参照MakeDriverSysIFsAndThreads
- return true;
- }
- bool DriverHandle::DynMakeDevicePaths()
- {
- if (m_pDynRootDevice)
- {
- map<string, vector<LogicDevice*>> ShortpathMap;
- //loop devtree
- size_t Size = m_pDynRootDevice->size();
- for (size_t i = 0; i < Size; i++)
- {
- DevTreeNode node = (*m_pDynRootDevice)[i];
- if (node.m_NodeType == TYPE_DEVICE)
- {
- GUID guid;
- string name;
- LogicDevice *p = (LogicDevice *)node.m_pObject;
- p->GetDeviceType(guid);
- guid_2_string(guid, name);
- ShortpathMap[name].push_back(p);
- }
- //else
- //{
- // //it's a driver
- // //the first level is equivalent to serialNo!!!!!
- // // major/minor/vendor/product/[serialno]/guid
- // MakeShortPaths(string(""), (DevTree*)node.m_pObject, ShortpathMap);
- //}
- }
- //loop shortpath
- map<string, vector<LogicDevice*>>::iterator iter = ShortpathMap.begin();
- while (iter != ShortpathMap.end())
- {
- if (iter->second.size() > 1)
- {
- //we have multiple same type devices in a raw
- for (size_t i = 0; i < iter->second.size(); i++)
- {
- string shortname = iter->first;
- shortname += "/";
- std::stringstream strm;
- strm << shortname << i;
- m_DynRootDeviceMap[strm.str()] = (iter->second)[i];
- }
- }
- else
- {
- m_DynRootDeviceMap[iter->first] = (iter->second)[0];
- }
- ++iter;
- }
- return true;
- }
- return false;
- }
- bool DriverHandle::MakeDevicePaths()
- {
- if (m_pRootDevice)
- {
- map<string, vector<LogicDevice*>> ShortpathMap;
- //loop devtree
- size_t Size = m_pRootDevice->size();
- for (size_t i = 0; i < Size; i++)
- {
- DevTreeNode node = (*m_pRootDevice)[i];
- if (node.m_NodeType == TYPE_DEVICE)
- {
- GUID guid;
- string name;
- LogicDevice *p = (LogicDevice *)node.m_pObject;
- p->GetDeviceType(guid);
- guid_2_string(guid, name);
- ShortpathMap[name].push_back(p);
- }
- //else
- //{
- // //it's a driver
- // //the first level is equivalent to serialNo!!!!!
- // // major/minor/vendor/product/[serialno]/guid
- // MakeShortPaths(string(""), (DevTree*)node.m_pObject, ShortpathMap);
- //}
- }
- //loop shortpath
- map<string, vector<LogicDevice*>>::iterator iter = ShortpathMap.begin();
- while (iter != ShortpathMap.end())
- {
- if (iter->second.size() > 1)
- {
- //we have multiple same type devices in a raw
- for (size_t i = 0; i < iter->second.size(); i++)
- {
- string shortname = iter->first;
- shortname += "/";
- std::stringstream strm;
- strm << shortname << i;
- m_RootDeviceMap[strm.str()] = (iter->second)[i];
- }
- }
- else
- {
- m_RootDeviceMap[iter->first] = (iter->second)[0];
- }
- ++iter;
- }
- return true;
- }
- return false;
- }
- #define BusUnitGuidStr "{181F92D6-A546-4ACF-8EF1-2CCAA9EA974E}"
- bool DriverHandle::DynMakeDriverSysIFsAndThreads()
- {
- //ServerSysIF *pSys = new ServerSysIF();
- //if (pSys)
- {
- //update m_AddressMap
- //UINT64 addr = (UINT64)((LogicDeviceSysIF*)pSys);//be careful..it's changing!!!!
- //m_DynAddressMap[addr] = pSys;
- //bind sys <-> logic
- //((LogicDriver*)m_pDPC)->SetSysLogicDevice((LogicDeviceSysIF*)pSys);
- //pSys->SetLogicDevice(((LogicDriver*)m_pDPC));
- //make full device path
- GUID guid;
- string guidname;
- ((LogicDriver*)m_pDPC)->GetDeviceType(guid);
- guid_2_string(guid, guidname);
- ResDataObject subpath;
- subpath = (guidname).c_str();
- string fullpath = MakeDevicePath(m_DynProbe, m_Connection, subpath).encode();
- string ccosfull = MakeCcosPath(m_DynProbe, m_Connection, subpath).encode();
- //map it
- m_DynDeviceFullPathMap[fullpath] = 0;// addr;
- m_CcosDynDeviceFullPathMap[ccosfull] = 0;
- //log config
- //((LogicDevice*)m_pDPC)->SetLogHandle(m_pLog);
- //m_NotifyThread.SetLogger(m_pDynLog);
- //mLog::FINFO("Set Notify Thread used DPC fullpath [{$}] ccosfull [{$}]", fullpath, ccosfull);
- m_NotifyThread.SetDPC(m_pDPC);
- return true;
- }
- return false;
- }
- /// <summary>
- /// 设置Driver的SysIF 和 Thread
- /// </summary>
- /// <returns></returns>
- bool DriverHandle::MakeDriverSysIFsAndThreads()
- {
- std::cout << "============== CDI : DriverHandle::MakeDriverSysIFsAndThreads" << std::endl;
- //ServerSysIF *pSys = new ServerSysIF();
- //if (pSys)
- {
- //update m_AddressMap
- //UINT64 addr = (UINT64)((LogicDeviceSysIF*)pSys);//be careful..it's changing!!!!
- //m_AddressMap[addr] = pSys;
- //bind sys <-> thread
-
- //bind sys <-> logic
- //((LogicDriver*)m_pDPC)->SetSysLogicDevice((LogicDeviceSysIF*)pSys);
- //pSys->SetLogicDevice(((LogicDriver*)m_pDPC));
- //make full device path
- GUID guid;
- string guidname;
- ((LogicDriver*)m_pDPC)->GetDeviceType(guid);
- guid_2_string(guid, guidname);
- ResDataObject subpath;
- subpath = (guidname).c_str();
- string fullpath = MakeDevicePath(m_Probe, m_Connection, subpath).encode();
- string ccosfull = MakeCcosPath(m_Probe, m_Connection, subpath).encode();
- //map it
- m_DriverFullPathMap = fullpath;
- m_DeviceFullPathMap[fullpath] = 0; //addr; 原来是SysIF对象地址
- m_CcosDeviceFullPathMap[ccosfull] = 0;
- //m_NotifyThread.SetLogger(m_pLog);
- //mLog::FINFO("Set Notify Thread used DPC fullpath [{$}] ccosfull [{$}]", fullpath, ccosfull);
- m_NotifyThread.SetDPC(m_pDPC);
- std::cout << "============== CDI : DriverHandle::MakeDriverSysIFsAndThreads OK" << std::endl;
- return true;
- }
- std::cout << "============== CDI : DriverHandle::MakeDriverSysIFsAndThreads Failed" << std::endl;
- return false;
- }
- bool DriverHandle::DynMakeDeviceSysIFsAndThreads()
- {
- //mLog::FINFO("CDI DriverHandle::DynMakeDeviceSysIFsAndThreads");
- //loop m_DeviceFullPathMap,make SysIF and attach it to logicdevice
- //Dual_Driver_Thread *pMainThread = NULL;
- map<string, LogicDevice*>::iterator iter = m_DynRootDeviceMap.begin();
- while (iter != m_DynRootDeviceMap.end())
- {
- //mLog::FINFO("CDI DriverHandle::DynMakeDeviceSysIFsAndThreads {$}", iter->first);
- //ServerSysIF *pSys = new ServerSysIF();
- //if (pSys)
- {
- //update m_AddressMap
-
- //UINT64 addr = (UINT64)((LogicDeviceSysIF*)pSys);//be careful..it's changing!!!!
- //m_DynAddressMap[addr] = pSys;
- /*
- //bind sys <-> thread
- if (iter->second->GetDevice_Thread_Priority() == THREAD_PRIORITY_NONE)
- {
- if (pMainThread == NULL)
- {
- pMainThread = new Dual_Driver_Thread();
- pMainThread->SetLogger(m_pDynLog);
- }
- pSys->SetWorkThread(pMainThread->GetReqThread(), pMainThread->GetResThread());
- pMainThread->RegistSysIFObject((LogicDeviceSysIF*)pSys);
- m_DynThreadMap[pMainThread].push_back(addr);
- }
- else
- {
- //create a thread for the deivce
- Dual_Driver_Thread *pThread = new Dual_Driver_Thread();
- pThread->SetLogger(m_pDynLog);
- //NOT FINISHED YET
- //thread priority need to be set.
- pSys->SetWorkThread(pThread->GetReqThread(), pThread->GetResThread());
- pThread->RegistSysIFObject((LogicDeviceSysIF*)pSys);
- m_DynThreadMap[pThread].push_back(addr);
- }
- */
- //bind sys <-> logic
- //iter->second->SetSysLogicDevice((LogicDeviceSysIF*)pSys);
- //pSys->SetLogicDevice(iter->second);
- //make full device path
- ResDataObject subpath;
- subpath = (iter->first).c_str();
- string fullpath = MakeDevicePath(m_DynProbe, m_Connection, subpath).encode();
- string ccospath = MakeCcosPath(m_Probe, m_Connection, subpath).encode();
- //map it
- GUID guid;
- string guidname;
- iter->second->GetDeviceType(guid);
- guid_2_string(guid, guidname);
- if (guidname == string(BusUnitGuidStr))
- {
- //it's root device
- fullpath = (const char*)getRootpath();
- }
- m_DynDeviceFullPathMap[fullpath] = 0;// addr; 原来是SysIf的内存指针地址
- iter->second->SetClientRootID(fullpath.c_str(), ccospath.c_str());
- g_DrvManager.AddDevice(iter->second->GetCcosRootPath().c_str(), iter->second);
- g_DrvManager.AddDevice(iter->second->GetCcosAbstractPath().c_str(), iter->second);
- //mLog::FINFO("CDI SetClientRootID dev {$} fullpath {$} ccospath {$} Real CCOS path [{$}]", iter->first, fullpath, ccospath, iter->second->GetCcosRootPath());
- //log config
- //iter->second->SetLogHandle(m_pDynLog);
- //NOT FINISHED YET.
- //only device add succeed.no driver info here
- //这部分已经移到注册的地方,因为设备没有Ready,现在AddDevice有点太快.
- //((LogicDriver*)m_pDPC)->AddDevice(fullpath.c_str());
- }
- //else
- //{
- // std::cout << "============== CDI : DriverHandle::DynMakeDeviceSysIFsAndThreads failed" << std::endl;
- // //WTF?
- // //log here
- // return false;
- //}
- ++iter;
- }
- //mLog::FINFO("CDI DriverHandle::DynMakeDeviceSysIFsAndThreads ok");
- return true;
- }
- /// <summary>
- /// 设置Device的SysIF 和 Thread
- /// </summary>
- /// <returns></returns>
- bool DriverHandle::MakeDeviceSysIFsAndThreads()
- {
- //mLog::FINFO("CDI DriverHandle::MakeDeviceSysIFsAndThreads ");
- //loop m_DeviceFullPathMap,make SysIF and attach it to logicdevice
- //Dual_Driver_Thread *pMainThread = NULL;
- map<string, LogicDevice*>::iterator iter = m_RootDeviceMap.begin();
- while (iter != m_RootDeviceMap.end())
- {
- //mLog::FINFO("CDI DriverHandle::MakeDeviceSysIFsAndThreads {$}", iter->first);
- //ServerSysIF *pSys = new ServerSysIF();
- //if (pSys)
- {
- //update m_AddressMap
- //UINT64 addr = (UINT64)((LogicDeviceSysIF*)pSys);//be careful..it's changing!!!!
- //m_AddressMap[addr] = pSys;
- /*
- //bind sys <-> thread
- if (iter->second->GetDevice_Thread_Priority() == THREAD_PRIORITY_NONE)
- {
- if (pMainThread == NULL)
- {
- pMainThread = new Dual_Driver_Thread();
- pMainThread->SetLogger(m_pLog);
- }
- pSys->SetWorkThread(pMainThread->GetReqThread(), pMainThread->GetResThread());
- pMainThread->RegistSysIFObject((LogicDeviceSysIF*)pSys);
- m_ThreadMap[pMainThread].push_back(addr);
- }
- else
- {
- //create a thread for the deivce
- Dual_Driver_Thread *pThread = new Dual_Driver_Thread();
- pThread->SetLogger(m_pLog);
- //NOT FINISHED YET
- //thread priority need to be set.
- pSys->SetWorkThread(pThread->GetReqThread(), pThread->GetResThread());
- pThread->RegistSysIFObject((LogicDeviceSysIF*)pSys);
- m_ThreadMap[pThread].push_back(addr);
- }
- */
- //bind sys <-> logic
- //iter->second->SetSysLogicDevice((LogicDeviceSysIF*)pSys);
- //pSys->SetLogicDevice(iter->second);
- //make full device path
- ResDataObject subpath;
- subpath = (iter->first).c_str();
- string fullpath = MakeDevicePath(m_Probe, m_Connection, subpath).encode();
- string ccosfull = MakeCcosPath(m_Probe, m_Connection, subpath).encode();
- //map it
- GUID guid;
- string guidname;
- iter->second->GetDeviceType(guid);
- guid_2_string(guid, guidname);
- if (guidname == string(BusUnitGuidStr))
- {
- //it's root device
-
- fullpath = (const char*)getRootpath();
- }
- else {
- m_CcosDeviceFullPathMap[ccosfull] = 0;
- }
- m_DeviceFullPathMap[fullpath] = 0; // addr; 原来是SysIf对象地址指针
- //mLog::FINFO("CDI m_DeviceFullPathMap[fullpath] = 0 fullpath {$} ccospath {$}", fullpath, ccosfull);
- //log config
- //
- //iter->second->SetLogHandle(m_pLog);
- //NOT FINISHED YET.
- //only device add succeed.no driver info here
- //这部分已经移到注册的地方,因为设备没有Ready,现在AddDevice有点太快.
- //((LogicDriver*)m_pDPC)->AddDevice(fullpath.c_str());
-
- }
- //else
- //{
- // std::cout << "============== CDI : DriverHandle::MakeDeviceSysIFsAndThreads failed" << std::endl;
- // //WTF?
- // //log here
- // return false;
- //}
- ++iter;
- }
- //mLog::FINFO("CDI DriverHandle::MakeDeviceSysIFsAndThreads ok");
- return true;
- }
- bool DriverHandle::DynStartWork()
- {
- bool ret = false;
- //线程启动之前通知单元设备已经Ready
- //CompleteDynInit(true);
- return true;
- /*
- map<Dual_Driver_Thread*, vector<UINT64>>::iterator iter = m_DynThreadMap.begin();
- while (iter != m_DynThreadMap.end())
- {
- if (iter->first->StartThread() == false)
- {
- return ret;
- }
- iter++;
- }
- return true;*/
- }
- void DriverHandle::DynStopWork()
- {
- /*
- bool ret = false;
- map<Dual_Driver_Thread*, vector<UINT64>>::iterator iter = m_DynThreadMap.begin();
- while (iter != m_DynThreadMap.end())
- {
- if (iter->first->StopThread() == false)
- {
- //mLog::FINFO("{$} Drv:StopThread Used Terminate!!!!!!!!!!!!!!!", m_DllFileTitle.c_str());
- }
- iter++;
- }*/
- //线程Stop后通知单元设备要退出
- CompleteDynInit(false);
- }
- void DriverHandle::CompleteStaticInit(bool Init)
- {
- //mLog::FINFO("CDI {$}", Init);
- //dpc first
- if (m_pDPC)
- {
- std::cout << "CDI m_pDPC "<< std::endl;
- //mLog::FINFO("CDI m_pDPC ");
- if (Init)
- {
- ((LogicDriver*)m_pDPC)->CompleteInit();
- }
- else
- {
- ((LogicDriver*)m_pDPC)->CompleteUnInit();
- }
- std::cout << "CDI Driver CompleteInit "<< Init << std::endl;
- //mLog::FINFO("CDI Driver CompleteInit {$}", Init);
- //init this
- map<string, LogicDevice*>::iterator iter = m_RootDeviceMap.begin();
- while (iter != m_RootDeviceMap.end())
- {
- std::cout << "CDI Try CompleteInit "<< iter->first << std::endl;
- //mLog::FINFO("CDI Try CompleteInit {$}", iter->first);
- if (Init)
- {
- iter->second->CompleteInit();
- }
- else
- {
- iter->second->CompleteUnInit();
- }
- //mLog::FINFO("CDI CompleteInit over.");
- ++iter;
- }
- //sub drivers
- for (size_t i = 0; i < m_hSubDriverList.Size(); i++)
- {
- //mLog::FINFO("CDI m_hSubDriverList try CompleteStaticInit {$}", i);
- m_hSubDriverList[i]->CompleteStaticInit(Init);
- }
- }
- }
- void DriverHandle::CompleteDynInit(bool Init)
- {
- //mLog::FINFO("CDI {$}", Init);
- //init this
- map<string, LogicDevice*>::iterator iter = m_DynRootDeviceMap.begin();
- while (iter != m_DynRootDeviceMap.end())
- {
- std::cout << "CDI Try Dyn CompleteInit " << iter->first << std::endl;
- //mLog::FINFO("CDI Try Dyn CompleteInit {$}", iter->first);
- if (Init)
- {
- iter->second->CompleteInit();
- }
- else
- {
- iter->second->CompleteUnInit();
- }
- ++iter;
- }
- //sub drivers
- for (size_t i = 0; i < m_DynSubDriverList.Size(); i++)
- {
- //mLog::FINFO("CDI m_DynSubDriverList try CompleteStaticInit {$}", i);
- m_DynSubDriverList[i]->CompleteDynInit(Init);
- }
- }
- bool DriverHandle::StartWork()
- {
- bool ret = true;
- //mLog::FINFO("StartWork Begin with {$}", m_DllFileTitle);
- std::cout << "StartWork Begin with " << m_DllFileTitle << std::endl;
- //线程启动之前通知设备单元,初始化全部结束
- CompleteStaticInit(true);
-
- //工作线程在卸载动态单元的时候,会有Pause通知线程的操作.
- //如果工作线程太快,通知线程还没有启动,工作线程已经进入Pause流程,导致死循环.
- //mLog::FINFO("CDI DriverHandle::StartWork m_NotifyThread.StartThread()");
- m_NotifyThread.SetName((m_DllFileTitle + "_DriverHandle::Notify").c_str());
- //mLog::FINFO("Start NotifyThread");
- ret = m_NotifyThread.StartThread();//notify first
- //mLog::FINFO("Start NotifyThread over with TID=[{$}]", m_NotifyThread.GetTID());
- /*
- map<Dual_Driver_Thread*, vector<UINT64>>::iterator iter = m_ThreadMap.begin();
- while (iter != m_ThreadMap.end())
- {
- printf("iter->first->StartThread() \n ");
- if (iter->first->StartThread() == false)
- {
- return false;
- }
- iter++;
- }*/
- //sub drivers
- if (ret)
- {
- for (size_t i = 0; i < m_hSubDriverList.Size(); i++)
- {
- //mLog::FINFO("StartWork Start SubDriverList NotifyThread {$}", i);
- ret &= m_hSubDriverList[i]->StartWork();
- //mLog::FINFO("StartWork Start SubDriverList Over NotifyThread {$}", i);
- if (ret == false)
- {
- return ret;
- }
- }
- }
- //mLog::FINFO("StartWork Over with {$}" , m_DllFileTitle);
- return ret;
- }
- void DriverHandle::StopWork()
- {
- bool ret = false;
- //map<Dual_Driver_Thread*, vector<UINT64>>::iterator iter = m_ThreadMap.begin();
- /* while (iter != m_ThreadMap.end())
- {
- if (iter->first->StopThread() == false)
- {
- //mLog::FINFO("{$} Drv:StopThread Used Terminate!!!!!!!!!!!!!!!", m_DllFileTitle.c_str());
- }
- iter++;
- }*/
- if (m_NotifyThread.StopThread() == false)
- {
- //mLog::FINFO("{$} Drv:StopNotifyThread Used Terminate!!!!!!!!!!!!!!!", m_DllFileTitle.c_str());
- }
- CompleteStaticInit(false);
- //delete thread
- /*
- iter = m_ThreadMap.begin();
- while (iter != m_ThreadMap.end())
- {
- delete (iter->first);
- iter++;
- }*/
- //mLog::FINFO("{$} Drv:delete Thread Done", m_DllFileTitle.c_str());
- //mLog::FINFO("{$} Drv:StopThread Done", m_DllFileTitle.c_str());
- }
- bool DriverHandle::DynClearDriverHandles()
- {
- vector<UINT64> ifList;
- DynGetSysIFList(ifList);
- g_HandleManager.ClearLocalDrvHandles(ifList);
- return true;
- }
- bool DriverHandle::ClearDriverHandles()
- {
- vector<UINT64> ifList;
- GetSysIFList(ifList);
- g_HandleManager.ClearLocalDrvHandles(ifList);
- return true;
- }
- bool DriverHandle::LoadDynDriver()
- {
- bool ret = false;
- DWORD timeout = INFINITE;
- string rootpath = "";
- //check connection first
- if (((LogicDriver*)m_pDPC)->GetConnectionStatus() == false)
- {
- //mLog::FERROR( "DPC Connection is broken");
- //return ret;
- }
- //after connect succed
- if (m_AsRootDriver == false)
- {
- rootpath = (const char*)getRootpath();
- timeout = 2000;
- }
- else
- {
- rootpath = (const char*)getChannelRootpath();
- }
- //Get logic devices
- //make path & work path etc
- //make Threads
- //start dyn Threads
- m_NotifyThread.PauseThread();
- if (DynInitHandle(this, m_pDPC))
- {
- //if (DynRegistToRoot(rootpath.c_str(),timeout))
- //{
- // return DynStartWork();
- //}
- ret = true;
- }
- m_NotifyThread.ResumeThread();
- return ret;
- }
- bool DriverHandle::UnloadDynDriver()
- {
- m_NotifyThread.PauseThread();
- if (m_HandleStatus == true)
- {
- DynUnRegistFromRoot();//unregist from root
- }
- //stop thread
- DynStopWork();
- //clear HandleList
- if (DynClearDriverHandles() == false)
- {
- //put some log here
- //mLog::FINFO("{$} Drv:Dyn Clear Handle Failed", m_DllFileTitle.c_str());
- }
- //mLog::FINFO("{$} Drv:Unload DPC", m_DllFileTitle.c_str());
- if (m_pDPC)
- {
- //unload devices
- if (m_pDynRootDevice)
- {
- //mLog::FINFO("{$} Drv:UnloadDriver", m_DllFileTitle.c_str());
- m_pDPC->UnloadLogicDevices();
- ((LogicDriver*)m_pDPC)->DisConnect();
- m_pDynRootDevice = NULL;//suppose it deleted DPC::UnloadDriver
- }
- }
- //delete sysif
- //map<UINT64, ServerSysIF*>::iterator iter1 = m_DynAddressMap.begin();
- //while (iter1 != m_DynAddressMap.end())
- //{
- // delete (iter1->second);
- // iter1++;
- //}
- ////mLog::FINFO("%s Drv:Dyn delete sysif Done", m_DllFileTitle.c_str());
- m_NotifyThread.ResumeThread();
- //
- //clear all maps
- m_DynDeviceFullPathMap.clear();
- m_DynRootDeviceMap.clear();
- //m_DynAddressMap.clear();
- //m_DynThreadMap.clear();// thread : DeviceSysIF[]
- //mLog::FINFO("{$} Drv:UnloadDevice Done", m_DllFileTitle.c_str());
- return true;
- }
- bool DriverHandle::UnloadDriver()
- {
- //mLog::FINFO("UnloadDrv:{$}", m_DllFileTitle.c_str());
- //unload sub drivers
- for (size_t i = 0; i < m_hSubDriverList.Size(); i++)
- {
- delete m_hSubDriverList[i];
- }
- m_hSubDriverList.Clear();
- UnloadDynDriver();
- if (m_HandleStatus == true)
- {
- // root驱动卸载过程中,把自己从Channel卸载,
- //这个操作因为在自我毁灭过程中进行,所以已经处于加锁状态.
- //因为加锁,外部命令无法反馈到当前驱动模块.
- //
- if (m_AsRootDriver == false)
- {
- UnRegistFromRoot();//unregist from root
- }
- }
- //stop thread
- StopWork();
- //clear HandleList
- if (ClearDriverHandles() == false)
- {
- //put some log here
- //mLog::FINFO("{$} Drv:Clear Handle Failed", m_DllFileTitle.c_str());
- }
- //mLog::FINFO( "{$} Drv:Unload DPC", m_DllFileTitle.c_str());
- if (m_pDPC)
- {
- //unload devices
- if (m_pRootDevice /* || m_pDPC->GetDPCType() == DPCDRIVER_MDPC*/)
- {
- //mLog::FINFO( "{$} Drv:UnloadDriver", m_DllFileTitle.c_str());
- m_pDPC->UnloadDriver();
- m_pRootDevice = NULL;//suppose it deleted DPC::UnloadDriver
- }
- //disconnect
- //m_pDPC->DisConnect();
- ////mLog::FINFO( "{$} Drv:DisConnectDPC Done", m_DllFileTitle.c_str());
- //only root DPC needs to be deleted
- if (m_IsRootHandle)
- {
- //unload dpc
- ReleaseDriverDPC dpcfunc = (ReleaseDriverDPC)dlsym(m_DllFileHandle, "ReleaseDriverDPC");
- if (dpcfunc)
- {
- dpcfunc(m_pDPC);
- //mLog::FINFO("{$} Drv:ReleaseDPC Done", m_DllFileTitle.c_str());
- }
- }
- //else
- //{
- // delete m_pDPC;//不要擅自删除DPC,非ROOT DPC让驱动自行删除.
- //}
- //FreeLibrary(m_DllFileHandle);
- m_pDPC = NULL;
- }
- //delete sysif
- //map<UINT64, ServerSysIF*>::iterator iter1 = m_AddressMap.begin();
- //while (iter1 != m_AddressMap.end())
- //{
- // //delete (iter1->second);
- // iter1++;
- //}
- ////mLog::FINFO( "{$} Drv:delete sysif Done", m_DllFileTitle.c_str());
- //clear all maps
- m_DllFileHandle = NULL;
- m_pRootDevice = NULL;
- m_pDPC = NULL;
- m_Probe.clear();
- m_Connection.clear();
- m_WorkPath = "";
- m_DeviceFullPathMap.clear();
- m_CcosDeviceFullPathMap.clear();
- m_CcosDynDeviceFullPathMap.clear();
- m_RootDeviceMap.clear();
- //m_AddressMap.clear();
- //m_ThreadMap.clear();// thread : DeviceSysIF[]
- //if (m_pLog)
- //{
- // ReleseLogger(m_pLog);
- // m_pLog = NULL;
- //}
- m_HandleStatus = false;
- //mLog::FINFO( "{$} Drv:UnloadDriver Done", m_DllFileTitle.c_str());
- return true;
- }
- bool DriverHandle::RegistToRoot(const char* RootPath, DWORD timeout)
- {
- BusUnitClient rootclient("DriverHandle_regroot");
-
- //mLog::FINFO("Begain [{$}]", RootPath);
- //sublist first
- for (size_t i = 0; i < m_hSubDriverList.Size(); i++)
- {
- if (m_hSubDriverList[i]->RegistToRoot(RootPath, timeout) == false)
- {
- //mLog::FERROR("RegistToRoot Failed [{$}]", RootPath);
- return false;
- }
- }
- //me second
- if (rootclient.Open(RootPath, ACTION, timeout) == false)
- {
- //mLog::FERROR("Open Failed [{$}]", RootPath);
- //printf("Thread:%d,Open Failed\n", GetCurrentThreadId());
- return false;
- }
- if (rootclient.GetExitFlag() == 0)
- {
- map<string, UINT64>::iterator iter = m_DeviceFullPathMap.begin();
- while (iter != m_DeviceFullPathMap.end())
- {
- string path = iter->first;
- UINT64 procId = (UINT64)getpid();
- UINT64 addr = iter->second;
- string TargetType = "Device";
- if (iter->first == m_DriverFullPathMap)
- {
- TargetType = "Driver";
- }
- //regist driver
- //mLog::FINFO("AddDeviceDescrpt [{$}] Try [{$}]", RootPath, path);
- RET_STATUS ret = (RET_STATUS)rootclient.AddDeviceDescrpt(path.c_str(), TargetType.c_str(),(const char*)getLocalMachineId(), procId, addr);
- if (ret == RET_FAILED)
- {
- //mLog::FERROR("AddDeviceDescrpt [{$}] Failed [{$}]", RootPath, path);
- //log here
- //try updadate this driver handle
- //assert(0);//NOT FINISHED YET
- //unhook it
- map<string, UINT64>::iterator iterBk = m_DeviceFullPathMap.begin();
- while (iterBk != iter)
- {
- if (rootclient.DelDeviceDescrpt(iterBk->first.c_str()) != RET_SUCCEED)
- {
- //log here
- //shit on going!!!!
- rootclient.Close();
- return false;
- }
- ++iterBk;
- }
- rootclient.Close();
- return false;
- }
- else if (ret == RET_INVALID)
- {
- return false;
- }
- ++iter;
- }
- // Ccos PATH
- iter = m_CcosDeviceFullPathMap.begin();
- while (iter != m_CcosDeviceFullPathMap.end())
- {
- string path = iter->first;
- UINT64 procId = (UINT64)getpid();
- UINT64 addr = iter->second;
- string TargetType = "Device";
- if (iter->first == m_DriverFullPathMap)
- {
- TargetType = "Driver";
- }
- //regist driver
- RET_STATUS ret = (RET_STATUS)rootclient.AddDeviceDescrpt(path.c_str(), TargetType.c_str(), (const char*)getLocalMachineId(), procId, addr);
- if (ret == RET_FAILED)
- {
- //log here
- //try updadate this driver handle
- //assert(0);//NOT FINISHED YET
- //mLog::FERROR("AddDeviceDescrpt [{$}] Failed [{$}]", RootPath, path);
- //unhook it
- map<string, UINT64>::iterator iterBk = m_CcosDeviceFullPathMap.begin();
- while (iterBk != iter)
- {
- if (rootclient.DelDeviceDescrpt(iterBk->first.c_str()) != RET_SUCCEED)
- {
- //log here
- //shit on going!!!!
- rootclient.Close();
- return false;
- }
- ++iterBk;
- }
- rootclient.Close();
- return false;
- }
- else if (ret == RET_INVALID)
- {
- return false;
- }
- ++iter;
- }
- }
- rootclient.Close();
- RegistToDrvObject(false,true);//后执行,保证Regist成功
- //mLog::FINFO("End of Success. [{$}]", RootPath);
- return true;
- }
- void DriverHandle::RegistToDrvObject(bool dyn, bool regist)
- {
- map<string, UINT64>::iterator iter;
- if (dyn)
- {
- iter = m_DynDeviceFullPathMap.begin();
- while (iter != m_DynDeviceFullPathMap.end())
- {
- string path = iter->first;
- //regist driver
- if (regist)
- {
- ((LogicDriver*)m_pDPC)->AddDevice(path.c_str());
- }
- else
- {
- ((LogicDriver*)m_pDPC)->DelDevice(path.c_str());
- }
- ++iter;
- }
- }
- else
- {
- /*
- if (m_pParent)
- {
- //mdpc parent exist,this is SubDPC object
- string path = m_DriverFullPathMap;
- //regist DPC2MDPC
- if (regist)
- {
- ((LogicDriver*)m_pParent)->AddDevice(path.c_str());
- }
- else
- {
- ((LogicDriver*)m_pParent)->DelDevice(path.c_str());
- }
- }*/
- //go normal
- iter = m_DeviceFullPathMap.begin();
- while (iter != m_DeviceFullPathMap.end())
- {
- string path = iter->first;
- if (m_DriverFullPathMap == path)
- {
- //exept it's own driver
- ++iter;
- continue;
- }
- //regist driver
- if (regist)
- {
- ((LogicDriver*)m_pDPC)->AddDevice(path.c_str());
- }
- else
- {
- ((LogicDriver*)m_pDPC)->DelDevice(path.c_str());
- }
- ++iter;
- }
- }
- }
- bool DriverHandle::DynRegistToRoot(const char* RootPath, DWORD timeout)
- {
- BusUnitClient rootclient("DriverHandle_dyn_reg");
- //me second
- if (rootclient.Open(RootPath, ACTION, timeout) == false)
- {
- std::cout << "Thread:" << GetCurrentThreadId() << ", Open RootPath failed. Path: " << RootPath << ", Timeout: " << timeout << std::endl;
- return false;
- }
- if (rootclient.GetExitFlag() == 0)
- {
- map<string, UINT64>::iterator iter = m_DynDeviceFullPathMap.begin();
- while (iter != m_DynDeviceFullPathMap.end())
- {
- string TargetType = "Device";
- string path = iter->first;
- UINT64 procId = (UINT64)getpid();
- UINT64 addr = iter->second;
- //regist driver
- if (rootclient.AddDeviceDescrpt(path.c_str(), TargetType.c_str(),(const char*)getLocalMachineId(), procId, addr) != RET_SUCCEED)
- {
- //log here
- //unhook it
- std::cout << "Thread:" << GetCurrentThreadId() << ", Starting rollback for m_DynDeviceFullPathMap. Current path: " << path << std::endl;
- map<string, UINT64>::iterator iterBk = m_DynDeviceFullPathMap.begin();
- while (iterBk != iter)
- {
- if (rootclient.DelDeviceDescrpt(iterBk->first.c_str()) != RET_SUCCEED)
- {
- //log here
- //shit on going!!!!
- std::cout << "Thread:" << GetCurrentThreadId() << ", Failed to rollback device. Path: " << iterBk->first << ". Critical error!" << std::endl;
- rootclient.Close();
- return false;
- }
- ++iterBk;
- }
- rootclient.Close();
- return false;
- }
- ++iter;
- }
- iter = m_CcosDynDeviceFullPathMap.begin();
- while (iter != m_CcosDynDeviceFullPathMap.end())
- {
- string TargetType = "Device";
- string path = iter->first;
- UINT64 procId = (UINT64)getpid();
- UINT64 addr = iter->second;
- //regist driver
- if (rootclient.AddDeviceDescrpt(path.c_str(), TargetType.c_str(), (const char*)getLocalMachineId(), procId, addr) != RET_SUCCEED)
- {
- std::cout << "Thread:" << GetCurrentThreadId() << ", Failed to register Ccos device. Path: " << path << std::endl;
- //log here
- //unhook it
- map<string, UINT64>::iterator iterBk = m_CcosDynDeviceFullPathMap.begin();
- while (iterBk != iter)
- {
- if (rootclient.DelDeviceDescrpt(iterBk->first.c_str()) != RET_SUCCEED)
- {
- //log here
- //shit on going!!!!
- rootclient.Close();
- return false;
- }
- ++iterBk;
- }
- rootclient.Close();
- return false;
- }
- ++iter;
- }
- }
- rootclient.Close();
- RegistToDrvObject();//后执行,保证Regist成功
- return true;
- }
- bool DriverHandle::DynUnRegistFromRoot()
- {
- DWORD timeout = 0;
- string rootpath;
- BusUnitClient rootclient("DriverHandle_UnReg");
- RegistToDrvObject(true,false);//先执行,不管unregist成功与否
- if (m_AsRootDriver == false)
- {
- rootpath = (const char*)getRootpath();
- timeout = 2000;
- }
- else
- {
- //rootpath = (const char*)getChannelRootpath();
- return true;//DriverManager中已经卸载了Root在Channel中的东西
- }
- if (m_AsRootDriver)
- {
- timeout = 2000;
- }
- else
- {
- timeout = INFINITE;
- }
- if (rootclient.Open(rootpath.c_str(), ACTION, timeout) == false)
- {
- //printf("Thread:%d,Open %s Failed\n", GetCurrentThreadId(), rootpath.c_str());
- return false;
- }
- map<string, UINT64>::iterator iter = m_DynDeviceFullPathMap.begin();
- while (iter != m_DynDeviceFullPathMap.end())
- {
- string path = iter->first;
- //regist driver
- if (rootclient.DelDeviceDescrpt(path.c_str()) != RET_SUCCEED)
- {
- //log here
- rootclient.Close();
- return false;
- }
- ++iter;
- }
- iter = m_CcosDynDeviceFullPathMap.begin();
- while (iter != m_CcosDynDeviceFullPathMap.end())
- {
- string path = iter->first;
- //regist driver
- if (rootclient.DelDeviceDescrpt(path.c_str()) != RET_SUCCEED)
- {
- //log here
- rootclient.Close();
- return false;
- }
- ++iter;
- }
- rootclient.Close();
- return true;
- }
- bool DriverHandle::UnRegistFromRoot()
- {
- DWORD timeout = 0;
- string rootpath;
- BusUnitClient rootclient("DriverHandle_unregroot");
- RegistToDrvObject(false, false);//先执行,不管unregist成功与否
- if (m_AsRootDriver == false)
- {
- rootpath = (const char*)getRootpath();
- timeout = 2000;
- }
- else
- {
- rootpath = (const char*)getChannelRootpath();
- }
- if (m_AsRootDriver)
- {
- timeout = 2000;
- }
- else
- {
- timeout = INFINITE;
- }
- if (rootclient.Open(rootpath.c_str(), ACTION, timeout) == false)
- {
- //printf("Thread:%d,Open %s Failed\n", GetCurrentThreadId(),rootpath.c_str());
- return false;
- }
- map<string, UINT64>::iterator iter = m_DeviceFullPathMap.begin();
- while (iter != m_DeviceFullPathMap.end())
- {
- string path = iter->first;
- //regist driver
- if (rootclient.DelDeviceDescrpt(path.c_str()) != RET_SUCCEED)
- {
- //log here
- rootclient.Close();
- return false;
- }
- ++iter;
- }
- rootclient.Close();
- return true;
- }
- void DriverHandle::DynGetSysIFList(vector<UINT64> &ifList)
- {
- }
- void DriverHandle::GetSysIFList(vector<UINT64> &ifList)
- {
- }
- //------------drv manager---------------------------
- DriverManager::DriverManager()
- {
- m_pRootDrvHandle = NULL;
- }
- DriverManager::~DriverManager()
- {
- if (m_pRootDrvHandle == NULL)
- return;
- UnloadDrivers();
- UnloadRootDriver();
- }
- bool DriverManager::UnRegistFromChannel(DWORD timeout)
- {
- string RootPath;
- string ChannelPath;
- BusUnitClient rootclient("DriverManager_unreg");
- RootPath = (const char *)getRootpath();
- ChannelPath = (const char*)getChannelRootpath();
- if (RootPath == ChannelPath)
- {
- return false;
- }
- if (rootclient.Open(ChannelPath.c_str(), ACTION, timeout) == false)
- {
- //printf("Thread:%d,Open %s Failed\n", GetCurrentThreadId(), ChannelPath.c_str());
- return false;
- }
- if (rootclient.DelDeviceDescrpt(RootPath.c_str()) != RET_SUCCEED)
- {
- //log here
- rootclient.Close();
- return false;
- }
- rootclient.Close();
- return true;
- }
- void DriverManager::UnloadRootDriver()
- {
- UnRegistFromChannel(500);
- //mLog::FINFO("UnRegistFromChannel Fnished");
- Thread_Lock();
- if (m_pRootDrvHandle)
- {
- //mLog::FINFO("Unload Root Driver Begin");
- //vector<UINT64> ifList;
- //m_pRootDrvHandle->GetSysIFList(ifList);
- //UpdateSysIF(ifList,true);
- delete m_pRootDrvHandle;
- m_pRootDrvHandle = NULL;
- //mLog::FINFO("Unload Root Driver Done");
- }
- Thread_UnLock();
- }
- void DriverManager::UnloadDrivers()
- {
- while (1)
- {
- DriverHandle* pHandle = NULL;
- Thread_Lock();
- vector<DriverHandle*>::iterator iter = m_SubDriverList.begin();
- if (iter != m_SubDriverList.end())
- {
- pHandle = (*iter);
- iter = m_SubDriverList.erase(iter);
- break;
- }
- Thread_UnLock();
- if (pHandle == NULL)
- {
- break;
- }
- //unload one
- UnloadDynDrvHandle(pHandle);
- UnloadDrvHandle(pHandle);
- }
- }
- bool DriverManager::DynLoadDriver(DriverHandle *pDrv)
- {
- std::cout << "DriverManager::DynLoadDriver IN" << std::endl;
- bool ret = false;
- const char *pRoot = getRootpath();
- //新流程如下.
- //先注册
- if (pDrv->DynRegistToRoot(pRoot) && pDrv->DynStartWork())
- {
- //后插入DPC
- DynAddDriver(pDrv);
- ret = true;
- }
-
- if (ret == false)
- {
- UnloadDynDrvHandle(pDrv);
- }
- return ret;
- }
- bool DriverManager::DynDriverInitLoadAll()
- {
- Thread_Lock();
- int x = 0;
- //auto it = m_SubDriverList.begin();
- //while (it != m_SubDriverList.end()) {
- // (*it)->InitLoadDynDriver();
- // it++;
- // x++;
- //}
- //mLog::FINFO("Begin");
- std::cout << "DriverManager::DynDriverInitLoadAll m_SubDriverList.size: " << m_SubDriverList.size() << std::endl;
- for (; x < m_SubDriverList.size(); x++)
- {
- std::cout << "SubDriverList InitLoadDynDriver " << x << std::endl;
- //mLog::FINFO("SubDriverList InitLoadDynDriver {$}",x );
- m_SubDriverList[x]->InitLoadDynDriver();
- //mLog::FINFO("SubDriverList InitLoadDynDriver {$} over.", x);
- std::cout << "SubDriverList InitLoadDynDriver " << x << " over"<<std::endl;
- }
- //mLog::FINFO("End");
- Thread_UnLock();
- return true;
- }
- void DriverManager::DynAddDriver(DriverHandle *pDrv)
- {
- std::cout << "DriverManager::DynAddDriver IN" << std::endl;
- Thread_Lock();
- //vector<UINT64> IFList;
- //pDrv->DynGetSysIFList(IFList);
- //UpdateSysIF(IFList);
- for (int x = 0; x < m_SubDriverList.size(); x++)
- {
- if (pDrv == m_SubDriverList[x])
- return;
- }
- m_SubDriverList.push_back(pDrv);
- std::cout << "DriverManager::DynAddDriver m_SubDriverList.push_back->size:" << m_SubDriverList.size() << std::endl;
- Thread_UnLock();
- }
- void DriverManager::AddDriver(DriverHandle *pDrv)
- {
- std::cout << "DriverManager::AddDriver IN" << std::endl;
- Thread_Lock();
- //vector<UINT64> IFList;
- //pDrv->GetSysIFList(IFList);
- //UpdateSysIF(IFList);
- for (int x = 0; x < m_SubDriverList.size(); x++)
- {
- if (pDrv == m_SubDriverList[x])
- return;
- }
- m_SubDriverList.push_back(pDrv);
- std::cout << "DriverManager::AddDriver m_SubDriverList.push_back->size:"<< m_SubDriverList.size() << std::endl;
- Thread_UnLock();
- }
- bool DriverManager::GetThreadsLogger(DWORD Tid, PVOID &pLogger)
- {
- Thread_Lock();
- for (size_t i = 0; i < m_SubDriverList.size(); i++)
- {
- if (m_SubDriverList[i]->GetThreadsLogger(Tid, pLogger))
- {
- Thread_UnLock();
- return true;
- }
- }
- if (m_pRootDrvHandle)
- {
- if (m_pRootDrvHandle->GetThreadsLogger(Tid, pLogger))
- {
- Thread_UnLock();
- return true;
- }
- }
- Thread_UnLock();
- return false;
- }
- bool DriverManager::UpdateRootDrvStatus(int Status)
- {
- std::cout << "UpdateRootDrvStatus IN" << std::endl;
- bool ret = true;
- if (m_pRootDrvHandle)
- {
- BusUnitClient rootclient("DriverManager_UpdateRootDrvStatus");
- if (rootclient.Open(getRootpath(), ACTION) == false)
- {
- std::cout << "Thread:" << GetCurrentThreadId() << ",Open Failed" << std::endl;
- return false;
- }
- if (rootclient.SetDeviceStatus(Status) != RET_SUCCEED)
- {
- std::cout << "Thread:" << GetCurrentThreadId() << ",SetDeviceStatus Failed" << std::endl;
- //printf("Thread:%d,SetDeviceStatus Failed\n", GetCurrentThreadId());
- ret = false;
- }
- rootclient.Close();
- std::cout << "Thread:" << GetCurrentThreadId() << ",SetDeviceStatus Succeed" << std::endl;
- //printf("Thread:%d,SetDeviceStatus Succeed\n", GetCurrentThreadId());
- }
- return ret;
- }
- void DriverManager::AddDevice(const char* devUri, LogicDevice* pDevice)
- {
- std::cout << "Add Device " << devUri<<" with "<< pDevice->GetCcosRootPath()<< endl;
- //mLog::FINFO("Add Device {$} with {$}", devUri, pDevice->GetCcosRootPath());
- m_CcosDeviceMap[devUri] = pDevice;
- }
- const char* DriverManager::GetAllDevice(void** pDevices, int& nDevNums)
- {
- m_strAllDeviceUri = "";
- auto it = m_CcosDeviceMap.begin();
- nDevNums = 0;
- if (pDevices == nullptr) {
- return m_strAllDeviceUri.c_str();
- }
- while (it != m_CcosDeviceMap.end())
- {
- if (nDevNums >= 30) {
- std::cout << "Warning: Device count exceeds max limit (" << 30 << ")" << std::endl;
- break;
- }
- m_strAllDeviceUri += it->first + ";";
- pDevices[nDevNums++] = it->second;
- it++;
- }
- return m_strAllDeviceUri.c_str();
- }
- bool DriverManager::LoadRootDrv()
- {
- //get busid
- string busId = (const char*)getLocalEbusId();
- if (busId.size() == 0)
- {
- //some log here
- return false;
- }
- //load drv
- DriverHandle *pHandle = new DriverHandle();
- pHandle->m_AsRootDriver = true;//need to set this
- string path = GetProcessDirectory() + "/" + "rootdrv.xml";
- if (pHandle->LoadDriver(path.c_str(), false))
- {
- //load succeed
- //printf("Load %s succeed\n", path.c_str());
-
- //Load only device,not include driver .!!!!!!
- //hook it to tree
- map<string, UINT64>::iterator iter = pHandle->m_DeviceFullPathMap.begin();
- while (iter != pHandle->m_DeviceFullPathMap.end())
- {
- string path = iter->first;
- UINT64 procId = (UINT64)getpid();
- UINT64 addr = iter->second;
- if (path == string(getRootpath()))
- {
- //make tree node
- if (g_DrvTree.AddTree(path.c_str(), procId, addr))
- {
- //done
- //save it
- m_pRootDrvHandle = (pHandle);
- //
- LogicDriver *driver = m_pRootDrvHandle->GetDpcDriver();
- driver->SetTreeRoot(true);
- driver->SetClientRootID(path.c_str(), path.c_str()+1);
- std::cout << "driver SetClientRootID Real CCOS path ["
- << path << "] from ["
- << driver->GetCcosRootPath() << "]" << std::endl;
- //mLog::FINFO("driver SetClientRootID Real CCOS path [{$}] ", path, driver->GetCcosRootPath());
- //driver->SubscribeSelf(); //这个是OemBusUnit对象,可以支持Open
- //start it
- {
- //这里有个瑕疵:
- //Driver带着一个Device加载,但是Driver意义不大,故在AddTree中过滤掉.
- //但是SysIF中是带着Driver的地址的....
- //vector<UINT64> ifList;
- //m_pRootDrvHandle->GetSysIFList(ifList);
- //UpdateSysIF(ifList);
- return m_pRootDrvHandle->StartWork();
- }
- }
- }
- ++iter;
- }
- }
- //printf("Load %s failed\n", path.c_str());
- delete pHandle;
- return false;
- }
- bool DriverManager::LoadDrv(ResDataObject &ConfigContext)
- {
- //NOT FINISHED YET
- assert(0);
- return false;
- }
- bool DriverManager::LoadDriver(const char *pConfigFilepath)
- {
- bool ret = false;
- DriverHandle *pHandle = new DriverHandle();
- string path = pConfigFilepath;
- std::cout << "------------ CDI: DriverManager::LoadDriver" << pConfigFilepath << std::endl;
- if (pHandle->LoadDriver(path.c_str(), true))
- {
- //load succeed
- const char *pRoot = getRootpath();
-
- //save it
- AddDriver(pHandle);//这点比较闹心,已经注册到系统,所以...
- LogicDriver *pRootDriver = m_pRootDrvHandle->GetDpcDriver();
- LogicDriver* pClientDriver = pHandle->GetDpcDriver();
-
- pRootDriver->AddEbusChildren(pClientDriver, pClientDriver->GetRootPath().c_str());
- //pHandle->get
- //start it
- ret = pHandle->StartWork();
- if (ret)
- {
- //regist to tree
- ret = pHandle->RegistToRoot(pRoot);
- }
- if (ret == false)
- {
- //所以得先卸载from系统,然后再清除...
- UnloadDrvHandle(pHandle);
- }
- }
- else
- {
- delete pHandle;
- }
- return ret;
- }
- bool DriverManager::UnloadDynDrvHandle(DriverHandle *pHandle)
- {
- bool ret = false;
- if (pHandle)
- {
- Thread_Lock();
- {
- //kick the SysIF from the map
- //vector<UINT64> ifList;
- //pHandle->DynGetSysIFList(ifList);
- //UpdateSysIF(ifList, true);
- pHandle->UnloadDynDriver();
- ret = true;
- }
- Thread_UnLock();
- }
- return ret;
- }
- bool DriverManager::UnloadDrvHandle(DriverHandle *pHandle)
- {
- bool ret = false;
- if (pHandle)
- {
- Thread_Lock();
- vector<DriverHandle*>::iterator iter = find(m_SubDriverList.begin(), m_SubDriverList.end(), pHandle);
- if (iter != m_SubDriverList.end())
- {
- //do unload work
- //kick it from list
- m_SubDriverList.erase(iter);
- //kick the SysIF from the map
- //vector<UINT64> ifList;
- //pHandle->GetSysIFList(ifList);
- //UpdateSysIF(ifList, true);
- ret = true;
- }
- Thread_UnLock();
- //kick the logicdevice from root
- delete pHandle;
- //if (pHandle->UnloadDriver() == false)
- //{
- // //put some log here
- //}
- }
- return ret;
- }
- //return next try tick
- bool DriverHandle::InitLoadDynDriver()
- {
- bool ret = true;
- if (!m_bCommonDPC)
- return true;
- LogicDevice* pDevice = m_pDPC;
- if (pDevice)
- {
- LogicDriver* pDriver = (LogicDriver*)pDevice;
- //check connection
- //if (pDriver->GetConnectionStatus())
- //{
- // //ok
- // return false;
- //}
- //makesure it's disconnected
- //if (m_pDrvHandle)
- {
- ////mLog::FINFO("Try UnloadDynDrv ");
- //UnloadDynDriver();
- //if (g_DrvManager.UnloadDynDrvHandle(this))
- {
- //do the connection
- //mLog::FINFO("Try Connect ");
- //if (pDriver->Connect())
- bool bret = pDriver->Connect();
- //mLog::FINFO("Try LoadDynDriver with Connect Result ={$} ", bret);
- //if(bret)
- {
- //do load logicdevices
- if (LoadDynDriver())
- {
- //mLog::FINFO("Try DrvManager::DynLoadDriver ");
- if (g_DrvManager.DynLoadDriver(this))
- {
- //ok
- //mLog::FINFO("Try AutomaticConnect Succeed");
- return true;
- }
- //mLog::FERROR("Try DrvManager::DynLoadDriver Failed");
- }
- else
- {
- UnloadDynDriver();
- }
- //report error here
- //mLog::FERROR("Try LoadDynDriver Failed");
- pDriver->DisConnect();
- return false;
- }
- }
- }
- }
- return false;
- }
|