123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775 |
- #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())
- {
- //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)
- {
- //printf("Thread:%d,Open Failed\n", GetCurrentThreadId());
- 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
- 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!!!!
- 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)
- {
- //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;
- while (it != m_CcosDeviceMap.end())
- {
- 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;
- }
|