DriverManager.cpp 64 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775
  1. #include "common_api.h"
  2. #include "DriverManager.h"
  3. #include "DPC.h"
  4. #include "CDI.h"
  5. #include "DrvTree.h"
  6. //#include "logger.h"
  7. #include "LocalConfig.h"
  8. #include "BusUnitClient.h"
  9. #include "LogicDriver.h"
  10. #include "HandleManager.h"
  11. #include "PacketAnalizer.h"
  12. #include <iostream>
  13. typedef DriverDPC* (*GetDriverDPC)();
  14. typedef void(*ReleaseDriverDPC)(DriverDPC *p);
  15. //extern Log4CPP::Logger* mLog::gLogger;
  16. DriverManager g_DrvManager;
  17. DriverHandle::DriverHandle()
  18. {
  19. m_DllFileHandle = 0;
  20. m_pRootDevice = 0;
  21. m_pDynRootDevice = 0;
  22. m_pDPC = 0;
  23. m_HandleStatus = false;
  24. m_AsRootDriver = false;
  25. //m_pLog = CreateLogger();
  26. //m_pDynLog = CreateLogger();
  27. m_IsRootHandle = false;
  28. //m_pParent = NULL;
  29. }
  30. DriverHandle::~DriverHandle()
  31. {
  32. UnloadDriver();
  33. }
  34. void DriverHandle::InitDynLogForDPC(DriverHandle *pParent)
  35. {
  36. //log init
  37. //ResDataObject val;
  38. //string FullLogFileName;
  39. //if (pParent->m_DllFileTitle.size() > 0)
  40. //{
  41. // m_DllFileTitle = pParent->m_DllFileTitle;
  42. // FullLogFileName = m_DynLogPath + m_DllFileTitle + string(".log");
  43. //}
  44. //else
  45. //{
  46. // FullLogFileName = m_DynLogPath + "drvlog.log";
  47. //}
  48. //m_pDynLog->SetLogFilepath(FullLogFileName.c_str());
  49. //if (pParent == NULL)
  50. //{
  51. // if (TryGetValue(m_DrvConfig, "LOGLEVEL", val))
  52. // {
  53. // Logger_Pattern lp;
  54. // m_pDynLog->GetLogPattern(lp);
  55. // if (lp.SetLevel((LONG)val))
  56. // {
  57. // m_pDynLog->SetLogPattern(lp);
  58. // }
  59. // }
  60. // //dpc logger
  61. // m_pDPC->SetDynLogHandle(m_pDynLog);
  62. //}
  63. //else// if (pParent != this)
  64. //{
  65. // if (m_pDPC)
  66. // {
  67. // LONG Level;
  68. // Logger_Pattern lp;
  69. // pParent->m_pLog->GetLogPattern(lp);
  70. // Level = lp.m_Level;
  71. // m_pDynLog->GetLogPattern(lp);
  72. // lp.SetLevel(Level);
  73. // m_pDynLog->SetLogPattern(lp);
  74. // //dpc logger
  75. // m_pDPC->SetDynLogHandle(m_pDynLog);
  76. // }
  77. //}
  78. }
  79. void DriverHandle::InitLogForDPC(DriverHandle *pParent)
  80. {
  81. //log init
  82. //ResDataObject val;
  83. //string FullLogFileName;
  84. //if (pParent == NULL)
  85. //{
  86. // if (TryGetValue(m_DrvConfig, "LOGLEVEL", val))
  87. // {
  88. // Logger_Pattern lp;
  89. // m_pLog->GetLogPattern(lp);
  90. // if (lp.SetLevel((LONG)val))
  91. // {
  92. // m_pLog->SetLogPattern(lp);
  93. // }
  94. // }
  95. // //dpc logger
  96. // ((DriverDPC*)m_pDPC)->SetLogHandle(m_pLog);
  97. //}
  98. ////else if (pParent == this)
  99. ////{
  100. ////}
  101. //else
  102. //{
  103. // if (pParent->m_DllFileTitle.size() > 0)
  104. // {
  105. // m_DllFileTitle = pParent->m_DllFileTitle;
  106. // FullLogFileName = m_LogPath + m_DllFileTitle + string(".log");
  107. // }
  108. // else
  109. // {
  110. // FullLogFileName = m_LogPath + "drvlog.log";
  111. // }
  112. // m_pLog->SetLogFilepath(FullLogFileName.c_str());
  113. // if (m_pDPC)
  114. // {
  115. // Logger_Pattern lp;
  116. // pParent->m_pLog->GetLogPattern(lp);
  117. // m_pLog->SetLogPattern(lp);
  118. // //dpc logger
  119. // ((DriverDPC*)m_pDPC)->SetLogHandle(m_pLog);
  120. // }
  121. //}
  122. }
  123. bool DriverHandle::GetThreadsLogger(DWORD ThreadId,PVOID &pLogger)
  124. {
  125. //if (m_NotifyThread.GetTID() == ThreadId)
  126. //{
  127. // pLogger = m_pLog;
  128. // return true;
  129. //}
  130. return false;
  131. }
  132. bool DriverHandle::InitDynSubDriverObject()
  133. {
  134. if (m_pDynRootDevice)
  135. {
  136. //loop drvtree
  137. size_t Size = m_pDynRootDevice->size();
  138. for (size_t i = 0; i < Size; i++)
  139. {
  140. DevTreeNode node = (*m_pDynRootDevice)[i];
  141. if (node.m_NodeType == TYPE_DRIVER)
  142. {
  143. LogicDriver *pDPC = (LogicDriver*)node.m_pObject;
  144. DriverHandle *pSubHandle = new DriverHandle();
  145. if (DynInitHandle(pSubHandle, pDPC))
  146. {
  147. m_DynSubDriverList.PushBack(pSubHandle);
  148. }
  149. else
  150. {
  151. return false;
  152. }
  153. }
  154. }
  155. }
  156. return true;
  157. }
  158. bool DriverHandle::InitSubDriverObject()
  159. {
  160. //mLog::FINFO("CDI ");
  161. if (m_pRootDevice)
  162. {
  163. //loop drvtree
  164. size_t Size = m_pRootDevice->size();
  165. //mLog::FINFO("CDI ----- DriverHandle::InitSubDriverObject {$} devices", Size);
  166. for (size_t i = 0; i < Size; i++)
  167. {
  168. DevTreeNode node = (*m_pRootDevice)[i];
  169. if (node.m_NodeType == TYPE_DRIVER)
  170. {
  171. //mLog::FINFO("CDI ----- DriverHandle::InitSubDriverObject InitHandle(pSubHandle, pDPC) again {$}", (int)node.m_NodeType );
  172. LogicDriver*pDPC = (LogicDriver*)node.m_pObject;
  173. DriverHandle *pSubHandle = new DriverHandle();
  174. if (InitHandle(pSubHandle, pDPC))
  175. {
  176. m_hSubDriverList.PushBack(pSubHandle);
  177. }
  178. else
  179. {
  180. //mLog::FINFO("CDI ---- DriverHandle::InitSubDriverObject InitHandle(pSubHandle, pDPC) failed", (int)node.m_NodeType);
  181. return false;
  182. }
  183. }
  184. else {
  185. //mLog::FINFO("CDI ----- DriverHandle::InitSubDriverObject node.m_NodeType", (int)node.m_NodeType);
  186. }
  187. }
  188. }
  189. return true;
  190. }
  191. //这是一个嵌套函数,用的时候要小心
  192. bool DriverHandle::DynInitHandle(DriverHandle *pSubHandle, LogicDriver*pDPC)
  193. {
  194. bool ret = false;
  195. string procdir;
  196. if (m_pDynRootDevice != nullptr)
  197. return true;
  198. procdir = GetProcessDirectory();
  199. pSubHandle->m_pDPC = pDPC;
  200. std::cout << "CDI ----- DriverHandle::DynInitHandle" << std::endl;
  201. //mLog::FINFO("CDI ----- DriverHandle::DynInitHandle");
  202. //Probe
  203. //mLog::FINFO("Device_Probe ");
  204. if (pDPC->Device_Probe(pSubHandle->m_DynProbe))
  205. {
  206. //mLog::FINFO("CDI After Device_Probe");
  207. ResDataObject ProcDirRes;
  208. ProcDirRes = procdir.c_str();
  209. pSubHandle->m_DynWorkPath = (const char*)MakeWorkPath(pSubHandle->m_DynProbe, pSubHandle->m_Connection, ProcDirRes,true);
  210. pSubHandle->m_DynLogPath = (const char*)MakeWorkPath(pSubHandle->m_DynProbe, pSubHandle->m_Connection, ProcDirRes, false);
  211. if (pSubHandle->m_DynWorkPath.size() > 0)
  212. {
  213. pSubHandle->InitDynLogForDPC(this);
  214. if (pDPC->SetDeviceWorkPath(pSubHandle->m_DynWorkPath.c_str()))
  215. {
  216. //mLog::FINFO("CDI pDPC->LoadLogicDevices ");
  217. PVOID pDevice = pDPC->LoadLogicDevices();
  218. if (pDevice)
  219. {
  220. pSubHandle->m_pDynRootDevice = (DevTree*)pDevice;
  221. //先假设驱动对象不能动态生成,只能以静态的形式存在!!!
  222. //do init Driver Object first
  223. //if (pSubHandle->InitDynSubDriverObject() == false)
  224. //{
  225. // //mLog::FERROR( "InitSubDriverObject Failed.\n");
  226. // return false;
  227. //}
  228. //mLog::FINFO("CDI do init device Object second");
  229. //do init device Object second
  230. if (DynMakeDevicePaths())
  231. {
  232. //mLog::FINFO("CDI DynMakeDeviceSysIFsAndThreads");
  233. if (DynMakeDeviceSysIFsAndThreads())
  234. {
  235. return true;
  236. }
  237. else
  238. {
  239. //DWORD errNo = GetLastError();
  240. //mLog::FERROR( "MakeDeviceSysIFsAndThreads Failed.ErrNo:{$}\n", errNo);
  241. return false;
  242. }
  243. }
  244. else
  245. {
  246. //DWORD errNo = GetLastError();
  247. //mLog::FERROR( "MakeDevicePaths Failed.ErrNo:{$}\n", errNo);
  248. return false;
  249. }
  250. }
  251. //先假设驱动对象不能动态生成,只能以静态的形式存在!!!
  252. //为Sub对象 创建Driver线程和Notify线程
  253. //if (pSubHandle->DynMakeDriverPaths())
  254. //{
  255. // if (pSubHandle->DynMakeDriverSysIFsAndThreads())
  256. // {
  257. // return true;
  258. // }
  259. // else
  260. // {
  261. // DWORD errNo = GetLastError();
  262. // //mLog::FERROR( "MakeDriverSysIFsAndThreads Failed.ErrNo:%d\n", errNo);
  263. // return false;
  264. // }
  265. //}
  266. //else
  267. //{
  268. // DWORD errNo = GetLastError();
  269. // //mLog::FERROR( "MakeDriverPaths Failed.ErrNo:%d\n", errNo);
  270. // return false;
  271. //}
  272. }
  273. else
  274. {
  275. //DWORD errNo = GetLastError();
  276. //mLog::FERROR( "SetWorkPath Failed.ErrNo:{$}\n", errNo);
  277. }
  278. }
  279. else
  280. {
  281. //DWORD errNo = GetLastError();
  282. //mLog::FERROR( "Probe Info not right.ErrNo:{$}\n", errNo);
  283. }
  284. }
  285. else
  286. {
  287. //DWORD errNo = GetLastError();
  288. //mLog::FERROR( "Call Probe Failed.ErrNo:{$}\n", errNo);
  289. }
  290. return false;
  291. }
  292. //这是一个嵌套函数,用的时候要小心
  293. bool DriverHandle::InitHandle(DriverHandle *pSubHandle, LogicDriver*pDPC)
  294. {
  295. bool ret = false;
  296. string procdir;
  297. procdir = GetProcessDirectory();
  298. //Logger *p = GetGlobalLogger();
  299. //mLog::FINFO( "CDI DriverHandle::InitHandle");
  300. std::cout << "CDI: DriverHandle::InitHandle" << std::endl;
  301. pSubHandle->m_pDPC = pDPC;
  302. //Probe
  303. std::cout << "CDI: First pDPC->Driver_Probe" << std::endl;
  304. //mLog::FINFO("CDI First pDPC->Driver_Probe");
  305. if (pDPC->Driver_Probe(pSubHandle->m_Probe))
  306. {
  307. std::cout << "CDI: After pDPC->Driver_Probe" << std::endl;
  308. //mLog::FINFO("CDI After pDPC->Driver_Probe");
  309. if (!m_AsRootDriver)
  310. {
  311. std::cout << "CDI: !m_AsRootDriver" << std::endl;
  312. //mLog::FINFO("CDI !m_AsRootDriver");
  313. GUID guid;
  314. string guidname;
  315. ((LogicDriver*)m_pDPC)->GetDeviceType(guid);
  316. guid_2_string(guid, guidname);
  317. ResDataObject subpath;
  318. subpath = (guidname).c_str();
  319. string fullpath = MakeDevicePath(m_Probe, m_Connection, subpath).encode();
  320. string ccospath = MakeCcosPath(m_Probe, m_Connection, subpath, true).encode();
  321. //pDPC->SetEbusRoot(fullpath.c_str());
  322. pDPC->SetClientRootID(fullpath.c_str(), ccospath.c_str());
  323. std::cout << "CDI: pDPC->SetClientRootID fullpath:[" << fullpath
  324. << "] ccospath:[" << ccospath << "] use subpath["
  325. << (const char*)subpath << "] Real CCOS path ["
  326. << pDPC->GetCcosRootPath() << "]" << std::endl;
  327. //mLog::FINFO("CDI pDPC->SetClientRootID fulpath:[{$}] ccospath:[{$}] use subpath[{$}] Real CCOS path [{$}]", fullpath, ccospath, (const char*)subpath, pDPC->GetCcosRootPath());
  328. g_DrvManager.AddDevice(pDPC->GetCcosRootPath().c_str(), pDPC);
  329. g_DrvManager.AddDevice(pDPC->GetCcosAbstractPath().c_str(), pDPC);
  330. }
  331. std::cout << "CDI: SetWorkPath" << std::endl;
  332. //mLog::FINFO("CDI SetWorkPath");
  333. ResDataObject ProcDirRes;
  334. ProcDirRes = procdir.c_str();
  335. pSubHandle->m_WorkPath = (const char*)MakeWorkPath(pSubHandle->m_Probe, pSubHandle->m_Connection, ProcDirRes,true);
  336. pSubHandle->m_LogPath = (const char*)MakeWorkPath(pSubHandle->m_Probe, pSubHandle->m_Connection, ProcDirRes, false);
  337. if (pSubHandle->m_WorkPath.size() > 0)
  338. {
  339. pSubHandle->InitLogForDPC(this);
  340. if (pDPC->SetDriverWorkPath(pSubHandle->m_WorkPath.c_str()))
  341. {
  342. PVOID pDevice = NULL;
  343. if(pDPC->GetDPCType() == DPCDRIVER_NORMAL)
  344. {
  345. std::cout << "CDI: ----- DriverHandle::InitHandle DPCDRIVER_NORMAL" << std::endl;
  346. //mLog::FINFO("CDI ----- DriverHandle::InitHandle DPCDRIVER_NORMAL");
  347. pDevice = pDPC->LoadDriver(); //从DPC 上拿到设备树
  348. std::cout << "CDI: pDPC->LoadDriver over." << std::endl;
  349. //mLog::FINFO("CDI pDPC->LoadDriver over.");
  350. }
  351. else// if (pDPC->GetDPCType() == DPCDRIVER_MDPC)
  352. {
  353. std::cout << "CDI: ----- DriverHandle::InitHandle ??? DPCDRIVER_MDPC ??" << std::endl;
  354. //mLog::FINFO("CDI ----- DriverHandle::InitHandle ??? DPCDRIVER_MDPC ??");
  355. //no load driver method calling.
  356. }
  357. //Step1:do init subdriver first
  358. if (pDevice)
  359. {
  360. std::cout << "CDI: Got DevTree" << std::endl;
  361. //mLog::FINFO("CDI Got DevTree");
  362. pSubHandle->m_pRootDevice = (DevTree*)pDevice;
  363. //pDevice 以及有这个树上的子节点了
  364. for (int x = 0; x < pSubHandle->m_pRootDevice->size(); x++) {
  365. LogicDriver* pChild = (LogicDriver*)((*(pSubHandle->m_pRootDevice))[x].m_pObject);
  366. LogicDevice* pDevice = (LogicDevice*)((*(pSubHandle->m_pRootDevice))[x].m_pObject);
  367. std::cout << "CDI: " << x << " Node " << std::endl;
  368. //mLog::FINFO("CDI {$} Node ", x);
  369. //*/
  370. if (!m_AsRootDriver) {
  371. std::cout << "CDI: !m_AsRootDriver true" << std::endl;
  372. //mLog::FINFO("CDI !m_AsRootDriver true");
  373. //LogicDevice* pDevice = (LogicDevice*)pChild;
  374. GUID guid2;
  375. string guidname;
  376. //((LogicDevice*)pChild)->GetDeviceType(guid);
  377. pDevice->GetDeviceType(guid2);
  378. guid_2_string(guid2, guidname);
  379. ResDataObject subpath;
  380. subpath = (guidname).c_str();
  381. string fullpath = MakeDevicePath(m_Probe, m_Connection, subpath).encode();
  382. //pDevice->SetEbusRoot(fullpath.c_str());
  383. string ccospath = MakeCcosPath(m_Probe, m_Connection, subpath).encode();
  384. pDevice->SetClientRootID(fullpath.c_str(), ccospath.c_str());
  385. std::cout << "CDI: pDevice->SetClientRootID fullpath :[" << fullpath
  386. << "] ccos path [" << ccospath
  387. << "] Real CCOS path [" << pDevice->GetCcosRootPath() << "] " << std::endl;
  388. //mLog::FINFO("CDI pDevice->SetClientRootID fullpath :[{$}] ccos path [{$}] Real CCOS path [{$}] ", fullpath, ccospath, pDevice->GetCcosRootPath());
  389. g_DrvManager.AddDevice(pDevice->GetCcosAbstractPath().c_str(), pDevice);
  390. g_DrvManager.AddDevice(pDevice->GetCcosRootPath().c_str(), pDevice);
  391. }
  392. else
  393. {
  394. string path = string(getRootpath());
  395. pDPC->SetClientRootID(path.c_str(), path.c_str() + 1);
  396. //mLog::FINFO("CDI Bus pDPC->SetClientRootID fulpath:[{$}] ccospath:[{$}] Real CCOS path [{$}]", path, path.c_str() + 1, pDPC->GetCcosRootPath());
  397. std::cout << "CDI: Bus pDPC->SetClientRootID fulpath:[" << path
  398. << "] ccospath:[" << (path.c_str() + 1)
  399. << "] Real CCOS path [" << pDPC->GetCcosRootPath() << "]" << std::endl;
  400. const std::string& temppath = pDevice->GetCcosRootPath();
  401. if (!temppath.empty()) {
  402. g_DrvManager.AddDevice(temppath.c_str(), pDevice);
  403. }
  404. else {
  405. // 添加错误处理日志
  406. std::cerr << "ERROR: Empty path for device " << pDevice << std::endl;
  407. }
  408. std::cout << "CDI: ----- g_DrvManager.AddDevice over" << std::endl;
  409. }
  410. std::cout << "CDI: ----- pDPC->AddEbusChildren" << std::endl;
  411. pDPC->AddEbusChildren(pChild, pChild->GetRootPath().c_str());
  412. //*/
  413. }
  414. //do init Driver Object first
  415. std::cout << "CDI: ----- DriverHandle::InitHandle DPCDRIVER_NORMAL" << std::endl;
  416. //mLog::FINFO("CDI ----- DriverHandle::InitHandle DPCDRIVER_NORMAL");
  417. if (pSubHandle->InitSubDriverObject() == false)
  418. {
  419. std::cout << "CDI: ----- DriverHandle::InitHandle InitSubDriverObject Failed" << std::endl;
  420. std::cout << "InitSubDriverObject Failed." << std::endl;
  421. //mLog::FINFO("CDI ----- DriverHandle::InitHandle InitSubDriverObject Failed");
  422. //mLog::FERROR( "InitSubDriverObject Failed.\n");
  423. return false;
  424. }
  425. }
  426. //Step2:do init this driver second
  427. //为Sub对象 创建Driver线程和Notify线程
  428. if (pSubHandle->MakeDriverPaths())
  429. {
  430. if (pSubHandle->MakeDriverSysIFsAndThreads() == false)
  431. {
  432. int errNo = errno; // Linux使用errno获取错误码
  433. std::cout << "CDI: ----- DriverHandle::InitHandle MakeDriverSysIFsAndThreads Failed.ErrNo " << errNo
  434. << " (" << strerror(errNo) << ")" << std::endl; // strerror将错误码转为描述字符串
  435. std::cout << "MakeDriverSysIFsAndThreads Failed.ErrNo:" << errNo
  436. << " (" << strerror(errNo) << ")" << std::endl;
  437. //mLog::FINFO("CDI ----- DriverHandle::InitHandle MakeDriverSysIFsAndThreads Failed.ErrNo {$}", errNo);
  438. //mLog::FERROR( "MakeDriverSysIFsAndThreads Failed.ErrNo:{$}\n", errNo);
  439. return false;
  440. }
  441. }
  442. else
  443. {
  444. int errNo = errno;
  445. std::cout << "CDI: ----- DriverHandle::InitHandle MakeDriverPaths Failed.ErrNo " << errNo
  446. << " (" << strerror(errNo) << ")" << std::endl;
  447. std::cout << "MakeDriverPaths Failed.ErrNo:" << errNo
  448. << " (" << strerror(errNo) << ")" << std::endl;
  449. //mLog::FINFO("CDI ----- DriverHandle::InitHandle MakeDriverPaths Failed.ErrNo {$}", errNo);
  450. //mLog::FERROR( "MakeDriverPaths Failed.ErrNo:{$}\n", errNo);
  451. return false;
  452. }
  453. //Step3:do init sub devices third
  454. if (pDevice)
  455. {
  456. std::cout << "CDI: do init device Object second" << std::endl;
  457. //mLog::FINFO("CDI do init device Object second");
  458. //do init device Object second
  459. if (pSubHandle->MakeDevicePaths())
  460. {
  461. if (pSubHandle->MakeDeviceSysIFsAndThreads())
  462. {
  463. std::cout << "CDI: pSubHandle->MakeDeviceSysIFsAndThreads" << std::endl;
  464. //mLog::FINFO("CDI pSubHandle->MakeDeviceSysIFsAndThreads");
  465. return true;
  466. }
  467. else
  468. {
  469. int errNo = errno;
  470. std::cout << "CDI: ----- DriverHandle::InitHandle MakeDeviceSysIFsAndThreads Failed.ErrNo " << errNo
  471. << " (" << strerror(errNo) << ")" << std::endl;
  472. std::cout << "MakeDeviceSysIFsAndThreads Failed.ErrNo:" << errNo
  473. << " (" << strerror(errNo) << ")" << std::endl;
  474. //std::cout << "CDI ----- DriverHandle::InitHandle MakeDeviceSysIFsAndThreads Failed.ErrNo" << errNo << std::endl;
  475. //mLog::FERROR( "MakeDeviceSysIFsAndThreads Failed.ErrNo:{$}\n", errNo);
  476. return false;
  477. }
  478. }
  479. else
  480. {
  481. int errNo = errno;
  482. std::cout << "CDI: ----- DriverHandle::InitHandle MakeDevicePaths Failed.ErrNo " << errNo
  483. << " (" << strerror(errNo) << ")" << std::endl;
  484. std::cout << "MakeDevicePaths Failed.ErrNo:" << errNo
  485. << " (" << strerror(errNo) << ")" << std::endl;
  486. //std::cout << "CDI ----- DriverHandle::InitHandle MakeDevicePaths Failed.ErrNo" << errNo << std::endl;
  487. //mLog::FERROR( "MakeDevicePaths Failed.ErrNo:{$}\n", errNo);
  488. return false;
  489. }
  490. }
  491. return true;
  492. }
  493. else
  494. {
  495. int errNo = errno;
  496. std::cout << "CDI: ----- DriverHandle::InitHandle SetDriverWorkPath Failed.ErrNo " << errNo
  497. << " (" << strerror(errNo) << ")" << std::endl;
  498. std::cout << "SetWorkPath Failed.ErrNo:" << errNo
  499. << " (" << strerror(errNo) << ")" << std::endl;
  500. //std::cout << "CDI ----- DriverHandle::InitHandle" << errNo << std::endl;
  501. //mLog::FERROR( "SetWorkPath Failed.ErrNo:{$}\n", errNo);
  502. }
  503. }
  504. else
  505. {
  506. int errNo = errno;
  507. std::cout << "CDI: ----- DriverHandle::InitHandle Probe Info not right.ErrNo " << errNo
  508. << " (" << strerror(errNo) << ")" << std::endl;
  509. std::cout << "Probe Info not right.ErrNo:" << errNo
  510. << " (" << strerror(errNo) << ")" << std::endl;
  511. //std::cout << "CDI ----- DriverHandle::InitHandle Probe Info not right.ErrNo" << errNo << std::endl;
  512. //mLog::FERROR( "Probe Info not right.ErrNo:{$}\n", errNo);
  513. }
  514. }
  515. else
  516. {
  517. int errNo = errno;
  518. std::cout << "CDI: ----- DriverHandle::InitHandle Call Probe Failed.ErrNo " << errNo
  519. << " (" << strerror(errNo) << ")" << std::endl;
  520. std::cout << "Call Probe Failed.ErrNo:" << errNo
  521. << " (" << strerror(errNo) << ")" << std::endl;
  522. //std::cout << "CDI ----- DriverHandle::InitHandle Call Probe Failed.ErrNo" << errNo << std::endl;
  523. //mLog::FERROR( "Call Probe Failed.ErrNo:{$}\n",errNo);
  524. }
  525. return false;
  526. }
  527. bool DriverHandle::IsDPCMatch(LogicDriver*pDriver)
  528. {
  529. return (m_pDPC == pDriver);
  530. }
  531. /// <summary>
  532. /// 驱动统一加载入口,使用DPC
  533. /// </summary>
  534. /// <param name="config"></param>
  535. /// <returns></returns>
  536. bool DriverHandle::LoadDriver(ResDataObject &config)
  537. {
  538. string procdir;
  539. string drvpath;
  540. string root;
  541. procdir = GetProcessDirectory();
  542. //Logger *p = GetGlobalLogger();
  543. //mLog::FINFO("LoadDriver Begin \n");
  544. std::cout << "------------ CDI: DriverHandle::LoadDriver ResDataObject" << std::endl;
  545. try {
  546. //load drvpath
  547. m_DrvConfig = config["CONFIGURATION"];
  548. drvpath = (const char *)m_DrvConfig["driver"];
  549. root = (const char*)m_DrvConfig["MajorID"];
  550. string temp("%ROOT%");
  551. drvpath = ReplaceSubString(drvpath, temp, procdir);
  552. m_DllFileTitle = GetFileTitle(drvpath);
  553. string FilePath = GetFileDirectory(drvpath);
  554. AddEnvPath(FilePath.c_str());
  555. //oemdriver
  556. int oemidx = m_DrvConfig.GetFirstOf("oemdriver");
  557. if (oemidx >= 0)
  558. {
  559. //exist
  560. string oemdrvpath = (const char *)m_DrvConfig["oemdriver"];
  561. string temp("%ROOT%");
  562. oemdrvpath = ReplaceSubString(oemdrvpath, temp, procdir);
  563. FilePath = GetFileDirectory(oemdrvpath);
  564. AddEnvPath(FilePath.c_str());
  565. m_DrvConfig["oemdriver"] = oemdrvpath.c_str();
  566. }
  567. else
  568. {
  569. m_DrvConfig.add("oemdriver", "");
  570. }
  571. TryGetValue(m_DrvConfig, "connections", m_Connection);
  572. //mLog::FINFO("GetDriver ok \n");
  573. }
  574. catch (ResDataObjectExption &exp)
  575. {
  576. //exp.what()
  577. //mLog::FINFO( exp.what());
  578. //mLog::FINFO( "Unknown Exp Happened while load drvfile:{$}\n", drvpath.c_str());
  579. return false;
  580. }
  581. catch (...)
  582. {
  583. //mLog::FINFO("Unknown Exp Happened while load drvfile:{$}\n", drvpath.c_str());
  584. return false;
  585. }
  586. m_DllFileHandle = dlopen(drvpath.c_str(), RTLD_LAZY);
  587. std::cout << "CDI: LoadLibrary :"<< drvpath << std::endl;
  588. //mLog::FINFO("CDI: LoadLibrary {$} ", drvpath.c_str());
  589. if (m_DllFileHandle)
  590. {
  591. std::cout << "load drvfile:" << drvpath << " OK" << std::endl;
  592. //mLog::FINFO("load drvfile:{$} OK \n", drvpath.c_str());
  593. typedef LogicDriver* (*GetDriverDPC)();
  594. GetDriverDPC dpcfunc = (GetDriverDPC)dlsym(m_DllFileHandle, "GetDriverDPC");
  595. const char* dlsym_error = dlerror();
  596. if (!dlsym_error)
  597. {
  598. std::cout << "Successfully obtained 'GetDriverDPC' function address" << std::endl;
  599. //mLog::FINFO("GetDriverDPC FUNC ADDR :{$} OK \n", drvpath.c_str());
  600. //第一次调用的是OemBusUint DPC
  601. //第二次调用是真实的DPC,如ImageSave/NewModuleDPC
  602. m_pDPC =(LogicDriver*) dpcfunc();
  603. if (m_pDPC)
  604. {
  605. std::cout << "Successfully obtained driver instance from 'GetDriverDPC'" << std::endl;
  606. //mLog::FINFO("GetDriverDPC CALL OK \n");
  607. //取到了DPC->LogicDriver的子类
  608. LogicDriver* pDriver = (LogicDriver*)m_pDPC;
  609. m_pDPC->SetDriverConfigFilePath(m_DriverConfigFilePath.c_str());
  610. //m_NotifyThread.m_pDPC = m_pDPC;//init dpc notify thread
  611. std::cout << "Calling DriverEntry with config: " << m_DrvConfig.encode() << std::endl;
  612. if (m_pDPC->DriverEntry(m_DrvConfig))
  613. {
  614. std::cout << "DriverEntry called successfully" << std::endl;
  615. //mLog::FINFO("DriverEntry CALL OK \n");
  616. std::cout << "------------ CDI: InitHandle start" << std::endl;
  617. InitLogForDPC(NULL);//for me
  618. if (InitHandle(this, m_pDPC))
  619. {
  620. //mLog::FINFO("InitHandle CALL OK \n");
  621. m_HandleStatus = true;
  622. m_IsRootHandle = true;
  623. //mLog::FINFO("LoadDriver Over \n");
  624. std::cout << "------------ CDI: InitHandle ok" << std::endl;
  625. return true;
  626. //return InitLoadDynDriver();
  627. }
  628. std::cout << "------------ CDI: InitHandle failed" << std::endl;
  629. //mLog::FINFO("lib:{$} Load Driver Failed.\n", drvpath.c_str());
  630. }
  631. else
  632. {
  633. std::cerr << "DriverEntry failed: " << dlerror() << std::endl;
  634. }
  635. }
  636. else
  637. {
  638. std::cerr << "No DPC object returned: " << dlerror() << std::endl;
  639. }
  640. }
  641. else
  642. {
  643. std::cerr << "Cannot load symbol GetDriverDPC: " << dlsym_error << std::endl;
  644. dlclose(m_DllFileHandle);
  645. //mLog::FINFO( "lib:{$} no GetDriverDPC entry.ErrNo:{$}\n", drvpath.c_str(), errNo);
  646. }
  647. }
  648. else
  649. {
  650. std::cerr << "Cannot load library: " << dlerror() << std::endl;
  651. //mLog::FINFO("Load lib:{$} failed.ErrNo:{$}\n", drvpath.c_str(), errNo);
  652. }
  653. //mLog::FINFO("CDI: InitHandle fail\n");
  654. std::cerr << "CDI: InitHandle fail" << std::endl;
  655. return false;
  656. }
  657. //Process:x64,x32
  658. //StartType:ignore
  659. //LoadType:module, or new
  660. //drvtype:lead,standalone,subsystem,device
  661. //driver:dpcpath
  662. //entrysource:xxx(it must load to memory)
  663. //connection:connections
  664. bool DriverHandle::LoadDriver(const char *pConfigFilepath, bool bCommonDPC)
  665. {
  666. ResDataObject config;
  667. //Logger *p = GetGlobalLogger();
  668. m_bCommonDPC = bCommonDPC;
  669. //NOT FINISHED YET
  670. //ignore all other shit,focus on the drvpath
  671. std::cout << "------------ CDI: DriverHandle::LoadDriver" << pConfigFilepath << std::endl;
  672. try {
  673. if (config.loadFile(pConfigFilepath) == false)
  674. {
  675. //log here
  676. return false;
  677. }
  678. m_DriverConfigFilePath = pConfigFilepath;
  679. //for file path
  680. //int idx = config["CONFIGURATION"].GetFirstOf("DRIVERCONFIGFILEPATH");
  681. //if (idx >= 0)
  682. //{
  683. // config["CONFIGURATION"].update("DRIVERCONFIGFILEPATH", pConfigFilepath);
  684. //}
  685. //else
  686. //{
  687. // config["CONFIGURATION"].add("DRIVERCONFIGFILEPATH", pConfigFilepath);
  688. //}
  689. return LoadDriver(config);
  690. }
  691. catch (ResDataObjectExption &exp)
  692. {
  693. cout << "What happpend " << exp.what() << endl;
  694. //exp.what()
  695. //mLog::FERROR( exp.what());
  696. }
  697. catch (...)
  698. {
  699. //mLog::FERROR( "Unknown Exp Happened\n");
  700. }
  701. return false;
  702. }
  703. void DriverHandle::MakeShortPaths(string stackpath, DevTree *pDevTree, map<string, vector<LogicDevice*>> &ShortpathMap)
  704. {
  705. if (stackpath.size() > 0)
  706. {
  707. stackpath += "/";
  708. }
  709. stackpath += pDevTree->GetName();
  710. //loop drvtree
  711. size_t Size = pDevTree->size();
  712. for (size_t i = 0; i < Size; i++)
  713. {
  714. DevTreeNode node = (*pDevTree)[i];
  715. if (node.m_NodeType == TYPE_DEVICE)
  716. {
  717. GUID guid;
  718. string name;
  719. string shortname;
  720. LogicDevice *p = (LogicDevice *)node.m_pObject;
  721. p->GetDeviceType(guid);
  722. guid_2_string(guid, name);
  723. shortname = stackpath + string("/") + name;
  724. ShortpathMap[shortname].push_back(p);
  725. }
  726. else
  727. {
  728. //it's a tree
  729. MakeShortPaths(stackpath, (DevTree*)node.m_pObject, ShortpathMap);
  730. }
  731. }
  732. }
  733. bool DriverHandle::DynMakeDriverPaths()
  734. {
  735. //assert(0);//NOT FINISHED YET
  736. return true;
  737. }
  738. bool DriverHandle::MakeDriverPaths()
  739. {
  740. //assert(0);
  741. //子逻辑设备情况,会有多个设备,且有GUID重复的设备,所以需要map
  742. //GUID\1,GUID\2这种名字区分开来
  743. //但是驱动对象就只有一个,就是本驱动
  744. //路径是 本驱动::Probe+GUID
  745. //详细参照MakeDriverSysIFsAndThreads
  746. return true;
  747. }
  748. bool DriverHandle::DynMakeDevicePaths()
  749. {
  750. if (m_pDynRootDevice)
  751. {
  752. map<string, vector<LogicDevice*>> ShortpathMap;
  753. //loop devtree
  754. size_t Size = m_pDynRootDevice->size();
  755. for (size_t i = 0; i < Size; i++)
  756. {
  757. DevTreeNode node = (*m_pDynRootDevice)[i];
  758. if (node.m_NodeType == TYPE_DEVICE)
  759. {
  760. GUID guid;
  761. string name;
  762. LogicDevice *p = (LogicDevice *)node.m_pObject;
  763. p->GetDeviceType(guid);
  764. guid_2_string(guid, name);
  765. ShortpathMap[name].push_back(p);
  766. }
  767. //else
  768. //{
  769. // //it's a driver
  770. // //the first level is equivalent to serialNo!!!!!
  771. // // major/minor/vendor/product/[serialno]/guid
  772. // MakeShortPaths(string(""), (DevTree*)node.m_pObject, ShortpathMap);
  773. //}
  774. }
  775. //loop shortpath
  776. map<string, vector<LogicDevice*>>::iterator iter = ShortpathMap.begin();
  777. while (iter != ShortpathMap.end())
  778. {
  779. if (iter->second.size() > 1)
  780. {
  781. //we have multiple same type devices in a raw
  782. for (size_t i = 0; i < iter->second.size(); i++)
  783. {
  784. string shortname = iter->first;
  785. shortname += "/";
  786. std::stringstream strm;
  787. strm << shortname << i;
  788. m_DynRootDeviceMap[strm.str()] = (iter->second)[i];
  789. }
  790. }
  791. else
  792. {
  793. m_DynRootDeviceMap[iter->first] = (iter->second)[0];
  794. }
  795. ++iter;
  796. }
  797. return true;
  798. }
  799. return false;
  800. }
  801. bool DriverHandle::MakeDevicePaths()
  802. {
  803. if (m_pRootDevice)
  804. {
  805. map<string, vector<LogicDevice*>> ShortpathMap;
  806. //loop devtree
  807. size_t Size = m_pRootDevice->size();
  808. for (size_t i = 0; i < Size; i++)
  809. {
  810. DevTreeNode node = (*m_pRootDevice)[i];
  811. if (node.m_NodeType == TYPE_DEVICE)
  812. {
  813. GUID guid;
  814. string name;
  815. LogicDevice *p = (LogicDevice *)node.m_pObject;
  816. p->GetDeviceType(guid);
  817. guid_2_string(guid, name);
  818. ShortpathMap[name].push_back(p);
  819. }
  820. //else
  821. //{
  822. // //it's a driver
  823. // //the first level is equivalent to serialNo!!!!!
  824. // // major/minor/vendor/product/[serialno]/guid
  825. // MakeShortPaths(string(""), (DevTree*)node.m_pObject, ShortpathMap);
  826. //}
  827. }
  828. //loop shortpath
  829. map<string, vector<LogicDevice*>>::iterator iter = ShortpathMap.begin();
  830. while (iter != ShortpathMap.end())
  831. {
  832. if (iter->second.size() > 1)
  833. {
  834. //we have multiple same type devices in a raw
  835. for (size_t i = 0; i < iter->second.size(); i++)
  836. {
  837. string shortname = iter->first;
  838. shortname += "/";
  839. std::stringstream strm;
  840. strm << shortname << i;
  841. m_RootDeviceMap[strm.str()] = (iter->second)[i];
  842. }
  843. }
  844. else
  845. {
  846. m_RootDeviceMap[iter->first] = (iter->second)[0];
  847. }
  848. ++iter;
  849. }
  850. return true;
  851. }
  852. return false;
  853. }
  854. #define BusUnitGuidStr "{181F92D6-A546-4ACF-8EF1-2CCAA9EA974E}"
  855. bool DriverHandle::DynMakeDriverSysIFsAndThreads()
  856. {
  857. //ServerSysIF *pSys = new ServerSysIF();
  858. //if (pSys)
  859. {
  860. //update m_AddressMap
  861. //UINT64 addr = (UINT64)((LogicDeviceSysIF*)pSys);//be careful..it's changing!!!!
  862. //m_DynAddressMap[addr] = pSys;
  863. //bind sys <-> logic
  864. //((LogicDriver*)m_pDPC)->SetSysLogicDevice((LogicDeviceSysIF*)pSys);
  865. //pSys->SetLogicDevice(((LogicDriver*)m_pDPC));
  866. //make full device path
  867. GUID guid;
  868. string guidname;
  869. ((LogicDriver*)m_pDPC)->GetDeviceType(guid);
  870. guid_2_string(guid, guidname);
  871. ResDataObject subpath;
  872. subpath = (guidname).c_str();
  873. string fullpath = MakeDevicePath(m_DynProbe, m_Connection, subpath).encode();
  874. string ccosfull = MakeCcosPath(m_DynProbe, m_Connection, subpath).encode();
  875. //map it
  876. m_DynDeviceFullPathMap[fullpath] = 0;// addr;
  877. m_CcosDynDeviceFullPathMap[ccosfull] = 0;
  878. //log config
  879. //((LogicDevice*)m_pDPC)->SetLogHandle(m_pLog);
  880. //m_NotifyThread.SetLogger(m_pDynLog);
  881. //mLog::FINFO("Set Notify Thread used DPC fullpath [{$}] ccosfull [{$}]", fullpath, ccosfull);
  882. m_NotifyThread.SetDPC(m_pDPC);
  883. return true;
  884. }
  885. return false;
  886. }
  887. /// <summary>
  888. /// 设置Driver的SysIF 和 Thread
  889. /// </summary>
  890. /// <returns></returns>
  891. bool DriverHandle::MakeDriverSysIFsAndThreads()
  892. {
  893. std::cout << "============== CDI : DriverHandle::MakeDriverSysIFsAndThreads" << std::endl;
  894. //ServerSysIF *pSys = new ServerSysIF();
  895. //if (pSys)
  896. {
  897. //update m_AddressMap
  898. //UINT64 addr = (UINT64)((LogicDeviceSysIF*)pSys);//be careful..it's changing!!!!
  899. //m_AddressMap[addr] = pSys;
  900. //bind sys <-> thread
  901. //bind sys <-> logic
  902. //((LogicDriver*)m_pDPC)->SetSysLogicDevice((LogicDeviceSysIF*)pSys);
  903. //pSys->SetLogicDevice(((LogicDriver*)m_pDPC));
  904. //make full device path
  905. GUID guid;
  906. string guidname;
  907. ((LogicDriver*)m_pDPC)->GetDeviceType(guid);
  908. guid_2_string(guid, guidname);
  909. ResDataObject subpath;
  910. subpath = (guidname).c_str();
  911. string fullpath = MakeDevicePath(m_Probe, m_Connection, subpath).encode();
  912. string ccosfull = MakeCcosPath(m_Probe, m_Connection, subpath).encode();
  913. //map it
  914. m_DriverFullPathMap = fullpath;
  915. m_DeviceFullPathMap[fullpath] = 0; //addr; 原来是SysIF对象地址
  916. m_CcosDeviceFullPathMap[ccosfull] = 0;
  917. //m_NotifyThread.SetLogger(m_pLog);
  918. //mLog::FINFO("Set Notify Thread used DPC fullpath [{$}] ccosfull [{$}]", fullpath, ccosfull);
  919. m_NotifyThread.SetDPC(m_pDPC);
  920. std::cout << "============== CDI : DriverHandle::MakeDriverSysIFsAndThreads OK" << std::endl;
  921. return true;
  922. }
  923. std::cout << "============== CDI : DriverHandle::MakeDriverSysIFsAndThreads Failed" << std::endl;
  924. return false;
  925. }
  926. bool DriverHandle::DynMakeDeviceSysIFsAndThreads()
  927. {
  928. //mLog::FINFO("CDI DriverHandle::DynMakeDeviceSysIFsAndThreads");
  929. //loop m_DeviceFullPathMap,make SysIF and attach it to logicdevice
  930. //Dual_Driver_Thread *pMainThread = NULL;
  931. map<string, LogicDevice*>::iterator iter = m_DynRootDeviceMap.begin();
  932. while (iter != m_DynRootDeviceMap.end())
  933. {
  934. //mLog::FINFO("CDI DriverHandle::DynMakeDeviceSysIFsAndThreads {$}", iter->first);
  935. //ServerSysIF *pSys = new ServerSysIF();
  936. //if (pSys)
  937. {
  938. //update m_AddressMap
  939. //UINT64 addr = (UINT64)((LogicDeviceSysIF*)pSys);//be careful..it's changing!!!!
  940. //m_DynAddressMap[addr] = pSys;
  941. /*
  942. //bind sys <-> thread
  943. if (iter->second->GetDevice_Thread_Priority() == THREAD_PRIORITY_NONE)
  944. {
  945. if (pMainThread == NULL)
  946. {
  947. pMainThread = new Dual_Driver_Thread();
  948. pMainThread->SetLogger(m_pDynLog);
  949. }
  950. pSys->SetWorkThread(pMainThread->GetReqThread(), pMainThread->GetResThread());
  951. pMainThread->RegistSysIFObject((LogicDeviceSysIF*)pSys);
  952. m_DynThreadMap[pMainThread].push_back(addr);
  953. }
  954. else
  955. {
  956. //create a thread for the deivce
  957. Dual_Driver_Thread *pThread = new Dual_Driver_Thread();
  958. pThread->SetLogger(m_pDynLog);
  959. //NOT FINISHED YET
  960. //thread priority need to be set.
  961. pSys->SetWorkThread(pThread->GetReqThread(), pThread->GetResThread());
  962. pThread->RegistSysIFObject((LogicDeviceSysIF*)pSys);
  963. m_DynThreadMap[pThread].push_back(addr);
  964. }
  965. */
  966. //bind sys <-> logic
  967. //iter->second->SetSysLogicDevice((LogicDeviceSysIF*)pSys);
  968. //pSys->SetLogicDevice(iter->second);
  969. //make full device path
  970. ResDataObject subpath;
  971. subpath = (iter->first).c_str();
  972. string fullpath = MakeDevicePath(m_DynProbe, m_Connection, subpath).encode();
  973. string ccospath = MakeCcosPath(m_Probe, m_Connection, subpath).encode();
  974. //map it
  975. GUID guid;
  976. string guidname;
  977. iter->second->GetDeviceType(guid);
  978. guid_2_string(guid, guidname);
  979. if (guidname == string(BusUnitGuidStr))
  980. {
  981. //it's root device
  982. fullpath = (const char*)getRootpath();
  983. }
  984. m_DynDeviceFullPathMap[fullpath] = 0;// addr; 原来是SysIf的内存指针地址
  985. iter->second->SetClientRootID(fullpath.c_str(), ccospath.c_str());
  986. g_DrvManager.AddDevice(iter->second->GetCcosRootPath().c_str(), iter->second);
  987. g_DrvManager.AddDevice(iter->second->GetCcosAbstractPath().c_str(), iter->second);
  988. //mLog::FINFO("CDI SetClientRootID dev {$} fullpath {$} ccospath {$} Real CCOS path [{$}]", iter->first, fullpath, ccospath, iter->second->GetCcosRootPath());
  989. //log config
  990. //iter->second->SetLogHandle(m_pDynLog);
  991. //NOT FINISHED YET.
  992. //only device add succeed.no driver info here
  993. //这部分已经移到注册的地方,因为设备没有Ready,现在AddDevice有点太快.
  994. //((LogicDriver*)m_pDPC)->AddDevice(fullpath.c_str());
  995. }
  996. //else
  997. //{
  998. // std::cout << "============== CDI : DriverHandle::DynMakeDeviceSysIFsAndThreads failed" << std::endl;
  999. // //WTF?
  1000. // //log here
  1001. // return false;
  1002. //}
  1003. ++iter;
  1004. }
  1005. //mLog::FINFO("CDI DriverHandle::DynMakeDeviceSysIFsAndThreads ok");
  1006. return true;
  1007. }
  1008. /// <summary>
  1009. /// 设置Device的SysIF 和 Thread
  1010. /// </summary>
  1011. /// <returns></returns>
  1012. bool DriverHandle::MakeDeviceSysIFsAndThreads()
  1013. {
  1014. //mLog::FINFO("CDI DriverHandle::MakeDeviceSysIFsAndThreads ");
  1015. //loop m_DeviceFullPathMap,make SysIF and attach it to logicdevice
  1016. //Dual_Driver_Thread *pMainThread = NULL;
  1017. map<string, LogicDevice*>::iterator iter = m_RootDeviceMap.begin();
  1018. while (iter != m_RootDeviceMap.end())
  1019. {
  1020. //mLog::FINFO("CDI DriverHandle::MakeDeviceSysIFsAndThreads {$}", iter->first);
  1021. //ServerSysIF *pSys = new ServerSysIF();
  1022. //if (pSys)
  1023. {
  1024. //update m_AddressMap
  1025. //UINT64 addr = (UINT64)((LogicDeviceSysIF*)pSys);//be careful..it's changing!!!!
  1026. //m_AddressMap[addr] = pSys;
  1027. /*
  1028. //bind sys <-> thread
  1029. if (iter->second->GetDevice_Thread_Priority() == THREAD_PRIORITY_NONE)
  1030. {
  1031. if (pMainThread == NULL)
  1032. {
  1033. pMainThread = new Dual_Driver_Thread();
  1034. pMainThread->SetLogger(m_pLog);
  1035. }
  1036. pSys->SetWorkThread(pMainThread->GetReqThread(), pMainThread->GetResThread());
  1037. pMainThread->RegistSysIFObject((LogicDeviceSysIF*)pSys);
  1038. m_ThreadMap[pMainThread].push_back(addr);
  1039. }
  1040. else
  1041. {
  1042. //create a thread for the deivce
  1043. Dual_Driver_Thread *pThread = new Dual_Driver_Thread();
  1044. pThread->SetLogger(m_pLog);
  1045. //NOT FINISHED YET
  1046. //thread priority need to be set.
  1047. pSys->SetWorkThread(pThread->GetReqThread(), pThread->GetResThread());
  1048. pThread->RegistSysIFObject((LogicDeviceSysIF*)pSys);
  1049. m_ThreadMap[pThread].push_back(addr);
  1050. }
  1051. */
  1052. //bind sys <-> logic
  1053. //iter->second->SetSysLogicDevice((LogicDeviceSysIF*)pSys);
  1054. //pSys->SetLogicDevice(iter->second);
  1055. //make full device path
  1056. ResDataObject subpath;
  1057. subpath = (iter->first).c_str();
  1058. string fullpath = MakeDevicePath(m_Probe, m_Connection, subpath).encode();
  1059. string ccosfull = MakeCcosPath(m_Probe, m_Connection, subpath).encode();
  1060. //map it
  1061. GUID guid;
  1062. string guidname;
  1063. iter->second->GetDeviceType(guid);
  1064. guid_2_string(guid, guidname);
  1065. if (guidname == string(BusUnitGuidStr))
  1066. {
  1067. //it's root device
  1068. fullpath = (const char*)getRootpath();
  1069. }
  1070. else {
  1071. m_CcosDeviceFullPathMap[ccosfull] = 0;
  1072. }
  1073. m_DeviceFullPathMap[fullpath] = 0; // addr; 原来是SysIf对象地址指针
  1074. //mLog::FINFO("CDI m_DeviceFullPathMap[fullpath] = 0 fullpath {$} ccospath {$}", fullpath, ccosfull);
  1075. //log config
  1076. //
  1077. //iter->second->SetLogHandle(m_pLog);
  1078. //NOT FINISHED YET.
  1079. //only device add succeed.no driver info here
  1080. //这部分已经移到注册的地方,因为设备没有Ready,现在AddDevice有点太快.
  1081. //((LogicDriver*)m_pDPC)->AddDevice(fullpath.c_str());
  1082. }
  1083. //else
  1084. //{
  1085. // std::cout << "============== CDI : DriverHandle::MakeDeviceSysIFsAndThreads failed" << std::endl;
  1086. // //WTF?
  1087. // //log here
  1088. // return false;
  1089. //}
  1090. ++iter;
  1091. }
  1092. //mLog::FINFO("CDI DriverHandle::MakeDeviceSysIFsAndThreads ok");
  1093. return true;
  1094. }
  1095. bool DriverHandle::DynStartWork()
  1096. {
  1097. bool ret = false;
  1098. //线程启动之前通知单元设备已经Ready
  1099. //CompleteDynInit(true);
  1100. return true;
  1101. /*
  1102. map<Dual_Driver_Thread*, vector<UINT64>>::iterator iter = m_DynThreadMap.begin();
  1103. while (iter != m_DynThreadMap.end())
  1104. {
  1105. if (iter->first->StartThread() == false)
  1106. {
  1107. return ret;
  1108. }
  1109. iter++;
  1110. }
  1111. return true;*/
  1112. }
  1113. void DriverHandle::DynStopWork()
  1114. {
  1115. /*
  1116. bool ret = false;
  1117. map<Dual_Driver_Thread*, vector<UINT64>>::iterator iter = m_DynThreadMap.begin();
  1118. while (iter != m_DynThreadMap.end())
  1119. {
  1120. if (iter->first->StopThread() == false)
  1121. {
  1122. //mLog::FINFO("{$} Drv:StopThread Used Terminate!!!!!!!!!!!!!!!", m_DllFileTitle.c_str());
  1123. }
  1124. iter++;
  1125. }*/
  1126. //线程Stop后通知单元设备要退出
  1127. CompleteDynInit(false);
  1128. }
  1129. void DriverHandle::CompleteStaticInit(bool Init)
  1130. {
  1131. //mLog::FINFO("CDI {$}", Init);
  1132. //dpc first
  1133. if (m_pDPC)
  1134. {
  1135. std::cout << "CDI m_pDPC "<< std::endl;
  1136. //mLog::FINFO("CDI m_pDPC ");
  1137. if (Init)
  1138. {
  1139. ((LogicDriver*)m_pDPC)->CompleteInit();
  1140. }
  1141. else
  1142. {
  1143. ((LogicDriver*)m_pDPC)->CompleteUnInit();
  1144. }
  1145. std::cout << "CDI Driver CompleteInit "<< Init << std::endl;
  1146. //mLog::FINFO("CDI Driver CompleteInit {$}", Init);
  1147. //init this
  1148. map<string, LogicDevice*>::iterator iter = m_RootDeviceMap.begin();
  1149. while (iter != m_RootDeviceMap.end())
  1150. {
  1151. //mLog::FINFO("CDI Try CompleteInit {$}", iter->first);
  1152. if (Init)
  1153. {
  1154. iter->second->CompleteInit();
  1155. }
  1156. else
  1157. {
  1158. iter->second->CompleteUnInit();
  1159. }
  1160. //mLog::FINFO("CDI CompleteInit over.");
  1161. ++iter;
  1162. }
  1163. //sub drivers
  1164. for (size_t i = 0; i < m_hSubDriverList.Size(); i++)
  1165. {
  1166. //mLog::FINFO("CDI m_hSubDriverList try CompleteStaticInit {$}", i);
  1167. m_hSubDriverList[i]->CompleteStaticInit(Init);
  1168. }
  1169. }
  1170. }
  1171. void DriverHandle::CompleteDynInit(bool Init)
  1172. {
  1173. //mLog::FINFO("CDI {$}", Init);
  1174. //init this
  1175. map<string, LogicDevice*>::iterator iter = m_DynRootDeviceMap.begin();
  1176. while (iter != m_DynRootDeviceMap.end())
  1177. {
  1178. std::cout << "CDI Try Dyn CompleteInit " << iter->first << std::endl;
  1179. //mLog::FINFO("CDI Try Dyn CompleteInit {$}", iter->first);
  1180. if (Init)
  1181. {
  1182. iter->second->CompleteInit();
  1183. }
  1184. else
  1185. {
  1186. iter->second->CompleteUnInit();
  1187. }
  1188. ++iter;
  1189. }
  1190. //sub drivers
  1191. for (size_t i = 0; i < m_DynSubDriverList.Size(); i++)
  1192. {
  1193. //mLog::FINFO("CDI m_DynSubDriverList try CompleteStaticInit {$}", i);
  1194. m_DynSubDriverList[i]->CompleteDynInit(Init);
  1195. }
  1196. }
  1197. bool DriverHandle::StartWork()
  1198. {
  1199. bool ret = true;
  1200. //mLog::FINFO("StartWork Begin with {$}", m_DllFileTitle);
  1201. std::cout << "StartWork Begin with " << m_DllFileTitle << std::endl;
  1202. //线程启动之前通知设备单元,初始化全部结束
  1203. CompleteStaticInit(true);
  1204. //工作线程在卸载动态单元的时候,会有Pause通知线程的操作.
  1205. //如果工作线程太快,通知线程还没有启动,工作线程已经进入Pause流程,导致死循环.
  1206. //mLog::FINFO("CDI DriverHandle::StartWork m_NotifyThread.StartThread()");
  1207. m_NotifyThread.SetName((m_DllFileTitle + "_DriverHandle::Notify").c_str());
  1208. //mLog::FINFO("Start NotifyThread");
  1209. ret = m_NotifyThread.StartThread();//notify first
  1210. //mLog::FINFO("Start NotifyThread over with TID=[{$}]", m_NotifyThread.GetTID());
  1211. /*
  1212. map<Dual_Driver_Thread*, vector<UINT64>>::iterator iter = m_ThreadMap.begin();
  1213. while (iter != m_ThreadMap.end())
  1214. {
  1215. printf("iter->first->StartThread() \n ");
  1216. if (iter->first->StartThread() == false)
  1217. {
  1218. return false;
  1219. }
  1220. iter++;
  1221. }*/
  1222. //sub drivers
  1223. if (ret)
  1224. {
  1225. for (size_t i = 0; i < m_hSubDriverList.Size(); i++)
  1226. {
  1227. //mLog::FINFO("StartWork Start SubDriverList NotifyThread {$}", i);
  1228. ret &= m_hSubDriverList[i]->StartWork();
  1229. //mLog::FINFO("StartWork Start SubDriverList Over NotifyThread {$}", i);
  1230. if (ret == false)
  1231. {
  1232. return ret;
  1233. }
  1234. }
  1235. }
  1236. //mLog::FINFO("StartWork Over with {$}" , m_DllFileTitle);
  1237. return ret;
  1238. }
  1239. void DriverHandle::StopWork()
  1240. {
  1241. bool ret = false;
  1242. //map<Dual_Driver_Thread*, vector<UINT64>>::iterator iter = m_ThreadMap.begin();
  1243. /* while (iter != m_ThreadMap.end())
  1244. {
  1245. if (iter->first->StopThread() == false)
  1246. {
  1247. //mLog::FINFO("{$} Drv:StopThread Used Terminate!!!!!!!!!!!!!!!", m_DllFileTitle.c_str());
  1248. }
  1249. iter++;
  1250. }*/
  1251. if (m_NotifyThread.StopThread() == false)
  1252. {
  1253. //mLog::FINFO("{$} Drv:StopNotifyThread Used Terminate!!!!!!!!!!!!!!!", m_DllFileTitle.c_str());
  1254. }
  1255. CompleteStaticInit(false);
  1256. //delete thread
  1257. /*
  1258. iter = m_ThreadMap.begin();
  1259. while (iter != m_ThreadMap.end())
  1260. {
  1261. delete (iter->first);
  1262. iter++;
  1263. }*/
  1264. //mLog::FINFO("{$} Drv:delete Thread Done", m_DllFileTitle.c_str());
  1265. //mLog::FINFO("{$} Drv:StopThread Done", m_DllFileTitle.c_str());
  1266. }
  1267. bool DriverHandle::DynClearDriverHandles()
  1268. {
  1269. vector<UINT64> ifList;
  1270. DynGetSysIFList(ifList);
  1271. g_HandleManager.ClearLocalDrvHandles(ifList);
  1272. return true;
  1273. }
  1274. bool DriverHandle::ClearDriverHandles()
  1275. {
  1276. vector<UINT64> ifList;
  1277. GetSysIFList(ifList);
  1278. g_HandleManager.ClearLocalDrvHandles(ifList);
  1279. return true;
  1280. }
  1281. bool DriverHandle::LoadDynDriver()
  1282. {
  1283. bool ret = false;
  1284. DWORD timeout = INFINITE;
  1285. string rootpath = "";
  1286. //check connection first
  1287. if (((LogicDriver*)m_pDPC)->GetConnectionStatus() == false)
  1288. {
  1289. //mLog::FERROR( "DPC Connection is broken");
  1290. //return ret;
  1291. }
  1292. //after connect succed
  1293. if (m_AsRootDriver == false)
  1294. {
  1295. rootpath = (const char*)getRootpath();
  1296. timeout = 2000;
  1297. }
  1298. else
  1299. {
  1300. rootpath = (const char*)getChannelRootpath();
  1301. }
  1302. //Get logic devices
  1303. //make path & work path etc
  1304. //make Threads
  1305. //start dyn Threads
  1306. m_NotifyThread.PauseThread();
  1307. if (DynInitHandle(this, m_pDPC))
  1308. {
  1309. //if (DynRegistToRoot(rootpath.c_str(),timeout))
  1310. //{
  1311. // return DynStartWork();
  1312. //}
  1313. ret = true;
  1314. }
  1315. m_NotifyThread.ResumeThread();
  1316. return ret;
  1317. }
  1318. bool DriverHandle::UnloadDynDriver()
  1319. {
  1320. m_NotifyThread.PauseThread();
  1321. if (m_HandleStatus == true)
  1322. {
  1323. DynUnRegistFromRoot();//unregist from root
  1324. }
  1325. //stop thread
  1326. DynStopWork();
  1327. //clear HandleList
  1328. if (DynClearDriverHandles() == false)
  1329. {
  1330. //put some log here
  1331. //mLog::FINFO("{$} Drv:Dyn Clear Handle Failed", m_DllFileTitle.c_str());
  1332. }
  1333. //mLog::FINFO("{$} Drv:Unload DPC", m_DllFileTitle.c_str());
  1334. if (m_pDPC)
  1335. {
  1336. //unload devices
  1337. if (m_pDynRootDevice)
  1338. {
  1339. //mLog::FINFO("{$} Drv:UnloadDriver", m_DllFileTitle.c_str());
  1340. m_pDPC->UnloadLogicDevices();
  1341. ((LogicDriver*)m_pDPC)->DisConnect();
  1342. m_pDynRootDevice = NULL;//suppose it deleted DPC::UnloadDriver
  1343. }
  1344. }
  1345. //delete sysif
  1346. //map<UINT64, ServerSysIF*>::iterator iter1 = m_DynAddressMap.begin();
  1347. //while (iter1 != m_DynAddressMap.end())
  1348. //{
  1349. // delete (iter1->second);
  1350. // iter1++;
  1351. //}
  1352. ////mLog::FINFO("%s Drv:Dyn delete sysif Done", m_DllFileTitle.c_str());
  1353. m_NotifyThread.ResumeThread();
  1354. //
  1355. //clear all maps
  1356. m_DynDeviceFullPathMap.clear();
  1357. m_DynRootDeviceMap.clear();
  1358. //m_DynAddressMap.clear();
  1359. //m_DynThreadMap.clear();// thread : DeviceSysIF[]
  1360. //mLog::FINFO("{$} Drv:UnloadDevice Done", m_DllFileTitle.c_str());
  1361. return true;
  1362. }
  1363. bool DriverHandle::UnloadDriver()
  1364. {
  1365. //mLog::FINFO("UnloadDrv:{$}", m_DllFileTitle.c_str());
  1366. //unload sub drivers
  1367. for (size_t i = 0; i < m_hSubDriverList.Size(); i++)
  1368. {
  1369. delete m_hSubDriverList[i];
  1370. }
  1371. m_hSubDriverList.Clear();
  1372. UnloadDynDriver();
  1373. if (m_HandleStatus == true)
  1374. {
  1375. // root驱动卸载过程中,把自己从Channel卸载,
  1376. //这个操作因为在自我毁灭过程中进行,所以已经处于加锁状态.
  1377. //因为加锁,外部命令无法反馈到当前驱动模块.
  1378. //
  1379. if (m_AsRootDriver == false)
  1380. {
  1381. UnRegistFromRoot();//unregist from root
  1382. }
  1383. }
  1384. //stop thread
  1385. StopWork();
  1386. //clear HandleList
  1387. if (ClearDriverHandles() == false)
  1388. {
  1389. //put some log here
  1390. //mLog::FINFO("{$} Drv:Clear Handle Failed", m_DllFileTitle.c_str());
  1391. }
  1392. //mLog::FINFO( "{$} Drv:Unload DPC", m_DllFileTitle.c_str());
  1393. if (m_pDPC)
  1394. {
  1395. //unload devices
  1396. if (m_pRootDevice /* || m_pDPC->GetDPCType() == DPCDRIVER_MDPC*/)
  1397. {
  1398. //mLog::FINFO( "{$} Drv:UnloadDriver", m_DllFileTitle.c_str());
  1399. m_pDPC->UnloadDriver();
  1400. m_pRootDevice = NULL;//suppose it deleted DPC::UnloadDriver
  1401. }
  1402. //disconnect
  1403. //m_pDPC->DisConnect();
  1404. ////mLog::FINFO( "{$} Drv:DisConnectDPC Done", m_DllFileTitle.c_str());
  1405. //only root DPC needs to be deleted
  1406. if (m_IsRootHandle)
  1407. {
  1408. //unload dpc
  1409. ReleaseDriverDPC dpcfunc = (ReleaseDriverDPC)dlsym(m_DllFileHandle, "ReleaseDriverDPC");
  1410. if (dpcfunc)
  1411. {
  1412. dpcfunc(m_pDPC);
  1413. //mLog::FINFO("{$} Drv:ReleaseDPC Done", m_DllFileTitle.c_str());
  1414. }
  1415. }
  1416. //else
  1417. //{
  1418. // delete m_pDPC;//不要擅自删除DPC,非ROOT DPC让驱动自行删除.
  1419. //}
  1420. //FreeLibrary(m_DllFileHandle);
  1421. m_pDPC = NULL;
  1422. }
  1423. //delete sysif
  1424. //map<UINT64, ServerSysIF*>::iterator iter1 = m_AddressMap.begin();
  1425. //while (iter1 != m_AddressMap.end())
  1426. //{
  1427. // //delete (iter1->second);
  1428. // iter1++;
  1429. //}
  1430. ////mLog::FINFO( "{$} Drv:delete sysif Done", m_DllFileTitle.c_str());
  1431. //clear all maps
  1432. m_DllFileHandle = NULL;
  1433. m_pRootDevice = NULL;
  1434. m_pDPC = NULL;
  1435. m_Probe.clear();
  1436. m_Connection.clear();
  1437. m_WorkPath = "";
  1438. m_DeviceFullPathMap.clear();
  1439. m_CcosDeviceFullPathMap.clear();
  1440. m_CcosDynDeviceFullPathMap.clear();
  1441. m_RootDeviceMap.clear();
  1442. //m_AddressMap.clear();
  1443. //m_ThreadMap.clear();// thread : DeviceSysIF[]
  1444. //if (m_pLog)
  1445. //{
  1446. // ReleseLogger(m_pLog);
  1447. // m_pLog = NULL;
  1448. //}
  1449. m_HandleStatus = false;
  1450. //mLog::FINFO( "{$} Drv:UnloadDriver Done", m_DllFileTitle.c_str());
  1451. return true;
  1452. }
  1453. bool DriverHandle::RegistToRoot(const char* RootPath, DWORD timeout)
  1454. {
  1455. BusUnitClient rootclient("DriverHandle_regroot");
  1456. //mLog::FINFO("Begain [{$}]", RootPath);
  1457. //sublist first
  1458. for (size_t i = 0; i < m_hSubDriverList.Size(); i++)
  1459. {
  1460. if (m_hSubDriverList[i]->RegistToRoot(RootPath, timeout) == false)
  1461. {
  1462. //mLog::FERROR("RegistToRoot Failed [{$}]", RootPath);
  1463. return false;
  1464. }
  1465. }
  1466. //me second
  1467. if (rootclient.Open(RootPath, ACTION, timeout) == false)
  1468. {
  1469. //mLog::FERROR("Open Failed [{$}]", RootPath);
  1470. //printf("Thread:%d,Open Failed\n", GetCurrentThreadId());
  1471. return false;
  1472. }
  1473. if (rootclient.GetExitFlag() == 0)
  1474. {
  1475. map<string, UINT64>::iterator iter = m_DeviceFullPathMap.begin();
  1476. while (iter != m_DeviceFullPathMap.end())
  1477. {
  1478. string path = iter->first;
  1479. UINT64 procId = (UINT64)getpid();
  1480. UINT64 addr = iter->second;
  1481. string TargetType = "Device";
  1482. if (iter->first == m_DriverFullPathMap)
  1483. {
  1484. TargetType = "Driver";
  1485. }
  1486. //regist driver
  1487. //mLog::FINFO("AddDeviceDescrpt [{$}] Try [{$}]", RootPath, path);
  1488. RET_STATUS ret = (RET_STATUS)rootclient.AddDeviceDescrpt(path.c_str(), TargetType.c_str(),(const char*)getLocalMachineId(), procId, addr);
  1489. if (ret == RET_FAILED)
  1490. {
  1491. //mLog::FERROR("AddDeviceDescrpt [{$}] Failed [{$}]", RootPath, path);
  1492. //log here
  1493. //try updadate this driver handle
  1494. //assert(0);//NOT FINISHED YET
  1495. //unhook it
  1496. map<string, UINT64>::iterator iterBk = m_DeviceFullPathMap.begin();
  1497. while (iterBk != iter)
  1498. {
  1499. if (rootclient.DelDeviceDescrpt(iterBk->first.c_str()) != RET_SUCCEED)
  1500. {
  1501. //log here
  1502. //shit on going!!!!
  1503. rootclient.Close();
  1504. return false;
  1505. }
  1506. ++iterBk;
  1507. }
  1508. rootclient.Close();
  1509. return false;
  1510. }
  1511. else if (ret == RET_INVALID)
  1512. {
  1513. return false;
  1514. }
  1515. ++iter;
  1516. }
  1517. // Ccos PATH
  1518. iter = m_CcosDeviceFullPathMap.begin();
  1519. while (iter != m_CcosDeviceFullPathMap.end())
  1520. {
  1521. string path = iter->first;
  1522. UINT64 procId = (UINT64)getpid();
  1523. UINT64 addr = iter->second;
  1524. string TargetType = "Device";
  1525. if (iter->first == m_DriverFullPathMap)
  1526. {
  1527. TargetType = "Driver";
  1528. }
  1529. //regist driver
  1530. RET_STATUS ret = (RET_STATUS)rootclient.AddDeviceDescrpt(path.c_str(), TargetType.c_str(), (const char*)getLocalMachineId(), procId, addr);
  1531. if (ret == RET_FAILED)
  1532. {
  1533. //log here
  1534. //try updadate this driver handle
  1535. //assert(0);//NOT FINISHED YET
  1536. //mLog::FERROR("AddDeviceDescrpt [{$}] Failed [{$}]", RootPath, path);
  1537. //unhook it
  1538. map<string, UINT64>::iterator iterBk = m_CcosDeviceFullPathMap.begin();
  1539. while (iterBk != iter)
  1540. {
  1541. if (rootclient.DelDeviceDescrpt(iterBk->first.c_str()) != RET_SUCCEED)
  1542. {
  1543. //log here
  1544. //shit on going!!!!
  1545. rootclient.Close();
  1546. return false;
  1547. }
  1548. ++iterBk;
  1549. }
  1550. rootclient.Close();
  1551. return false;
  1552. }
  1553. else if (ret == RET_INVALID)
  1554. {
  1555. return false;
  1556. }
  1557. ++iter;
  1558. }
  1559. }
  1560. rootclient.Close();
  1561. RegistToDrvObject(false,true);//后执行,保证Regist成功
  1562. //mLog::FINFO("End of Success. [{$}]", RootPath);
  1563. return true;
  1564. }
  1565. void DriverHandle::RegistToDrvObject(bool dyn, bool regist)
  1566. {
  1567. map<string, UINT64>::iterator iter;
  1568. if (dyn)
  1569. {
  1570. iter = m_DynDeviceFullPathMap.begin();
  1571. while (iter != m_DynDeviceFullPathMap.end())
  1572. {
  1573. string path = iter->first;
  1574. //regist driver
  1575. if (regist)
  1576. {
  1577. ((LogicDriver*)m_pDPC)->AddDevice(path.c_str());
  1578. }
  1579. else
  1580. {
  1581. ((LogicDriver*)m_pDPC)->DelDevice(path.c_str());
  1582. }
  1583. ++iter;
  1584. }
  1585. }
  1586. else
  1587. {
  1588. /*
  1589. if (m_pParent)
  1590. {
  1591. //mdpc parent exist,this is SubDPC object
  1592. string path = m_DriverFullPathMap;
  1593. //regist DPC2MDPC
  1594. if (regist)
  1595. {
  1596. ((LogicDriver*)m_pParent)->AddDevice(path.c_str());
  1597. }
  1598. else
  1599. {
  1600. ((LogicDriver*)m_pParent)->DelDevice(path.c_str());
  1601. }
  1602. }*/
  1603. //go normal
  1604. iter = m_DeviceFullPathMap.begin();
  1605. while (iter != m_DeviceFullPathMap.end())
  1606. {
  1607. string path = iter->first;
  1608. if (m_DriverFullPathMap == path)
  1609. {
  1610. //exept it's own driver
  1611. ++iter;
  1612. continue;
  1613. }
  1614. //regist driver
  1615. if (regist)
  1616. {
  1617. ((LogicDriver*)m_pDPC)->AddDevice(path.c_str());
  1618. }
  1619. else
  1620. {
  1621. ((LogicDriver*)m_pDPC)->DelDevice(path.c_str());
  1622. }
  1623. ++iter;
  1624. }
  1625. }
  1626. }
  1627. bool DriverHandle::DynRegistToRoot(const char* RootPath, DWORD timeout)
  1628. {
  1629. BusUnitClient rootclient("DriverHandle_dyn_reg");
  1630. //me second
  1631. if (rootclient.Open(RootPath, ACTION, timeout) == false)
  1632. {
  1633. //printf("Thread:%d,Open Failed\n", GetCurrentThreadId());
  1634. return false;
  1635. }
  1636. if (rootclient.GetExitFlag() == 0)
  1637. {
  1638. map<string, UINT64>::iterator iter = m_DynDeviceFullPathMap.begin();
  1639. while (iter != m_DynDeviceFullPathMap.end())
  1640. {
  1641. string TargetType = "Device";
  1642. string path = iter->first;
  1643. UINT64 procId = (UINT64)getpid();
  1644. UINT64 addr = iter->second;
  1645. //regist driver
  1646. if (rootclient.AddDeviceDescrpt(path.c_str(), TargetType.c_str(),(const char*)getLocalMachineId(), procId, addr) != RET_SUCCEED)
  1647. {
  1648. //log here
  1649. //unhook it
  1650. map<string, UINT64>::iterator iterBk = m_DynDeviceFullPathMap.begin();
  1651. while (iterBk != iter)
  1652. {
  1653. if (rootclient.DelDeviceDescrpt(iterBk->first.c_str()) != RET_SUCCEED)
  1654. {
  1655. //log here
  1656. //shit on going!!!!
  1657. rootclient.Close();
  1658. return false;
  1659. }
  1660. ++iterBk;
  1661. }
  1662. rootclient.Close();
  1663. return false;
  1664. }
  1665. ++iter;
  1666. }
  1667. iter = m_CcosDynDeviceFullPathMap.begin();
  1668. while (iter != m_CcosDynDeviceFullPathMap.end())
  1669. {
  1670. string TargetType = "Device";
  1671. string path = iter->first;
  1672. UINT64 procId = (UINT64)getpid();
  1673. UINT64 addr = iter->second;
  1674. //regist driver
  1675. if (rootclient.AddDeviceDescrpt(path.c_str(), TargetType.c_str(), (const char*)getLocalMachineId(), procId, addr) != RET_SUCCEED)
  1676. {
  1677. //log here
  1678. //unhook it
  1679. map<string, UINT64>::iterator iterBk = m_CcosDynDeviceFullPathMap.begin();
  1680. while (iterBk != iter)
  1681. {
  1682. if (rootclient.DelDeviceDescrpt(iterBk->first.c_str()) != RET_SUCCEED)
  1683. {
  1684. //log here
  1685. //shit on going!!!!
  1686. rootclient.Close();
  1687. return false;
  1688. }
  1689. ++iterBk;
  1690. }
  1691. rootclient.Close();
  1692. return false;
  1693. }
  1694. ++iter;
  1695. }
  1696. }
  1697. rootclient.Close();
  1698. RegistToDrvObject();//后执行,保证Regist成功
  1699. return true;
  1700. }
  1701. bool DriverHandle::DynUnRegistFromRoot()
  1702. {
  1703. DWORD timeout = 0;
  1704. string rootpath;
  1705. BusUnitClient rootclient("DriverHandle_UnReg");
  1706. RegistToDrvObject(true,false);//先执行,不管unregist成功与否
  1707. if (m_AsRootDriver == false)
  1708. {
  1709. rootpath = (const char*)getRootpath();
  1710. timeout = 2000;
  1711. }
  1712. else
  1713. {
  1714. //rootpath = (const char*)getChannelRootpath();
  1715. return true;//DriverManager中已经卸载了Root在Channel中的东西
  1716. }
  1717. if (m_AsRootDriver)
  1718. {
  1719. timeout = 2000;
  1720. }
  1721. else
  1722. {
  1723. timeout = INFINITE;
  1724. }
  1725. if (rootclient.Open(rootpath.c_str(), ACTION, timeout) == false)
  1726. {
  1727. //printf("Thread:%d,Open %s Failed\n", GetCurrentThreadId(), rootpath.c_str());
  1728. return false;
  1729. }
  1730. map<string, UINT64>::iterator iter = m_DynDeviceFullPathMap.begin();
  1731. while (iter != m_DynDeviceFullPathMap.end())
  1732. {
  1733. string path = iter->first;
  1734. //regist driver
  1735. if (rootclient.DelDeviceDescrpt(path.c_str()) != RET_SUCCEED)
  1736. {
  1737. //log here
  1738. rootclient.Close();
  1739. return false;
  1740. }
  1741. ++iter;
  1742. }
  1743. iter = m_CcosDynDeviceFullPathMap.begin();
  1744. while (iter != m_CcosDynDeviceFullPathMap.end())
  1745. {
  1746. string path = iter->first;
  1747. //regist driver
  1748. if (rootclient.DelDeviceDescrpt(path.c_str()) != RET_SUCCEED)
  1749. {
  1750. //log here
  1751. rootclient.Close();
  1752. return false;
  1753. }
  1754. ++iter;
  1755. }
  1756. rootclient.Close();
  1757. return true;
  1758. }
  1759. bool DriverHandle::UnRegistFromRoot()
  1760. {
  1761. DWORD timeout = 0;
  1762. string rootpath;
  1763. BusUnitClient rootclient("DriverHandle_unregroot");
  1764. RegistToDrvObject(false, false);//先执行,不管unregist成功与否
  1765. if (m_AsRootDriver == false)
  1766. {
  1767. rootpath = (const char*)getRootpath();
  1768. timeout = 2000;
  1769. }
  1770. else
  1771. {
  1772. rootpath = (const char*)getChannelRootpath();
  1773. }
  1774. if (m_AsRootDriver)
  1775. {
  1776. timeout = 2000;
  1777. }
  1778. else
  1779. {
  1780. timeout = INFINITE;
  1781. }
  1782. if (rootclient.Open(rootpath.c_str(), ACTION, timeout) == false)
  1783. {
  1784. //printf("Thread:%d,Open %s Failed\n", GetCurrentThreadId(),rootpath.c_str());
  1785. return false;
  1786. }
  1787. map<string, UINT64>::iterator iter = m_DeviceFullPathMap.begin();
  1788. while (iter != m_DeviceFullPathMap.end())
  1789. {
  1790. string path = iter->first;
  1791. //regist driver
  1792. if (rootclient.DelDeviceDescrpt(path.c_str()) != RET_SUCCEED)
  1793. {
  1794. //log here
  1795. rootclient.Close();
  1796. return false;
  1797. }
  1798. ++iter;
  1799. }
  1800. rootclient.Close();
  1801. return true;
  1802. }
  1803. void DriverHandle::DynGetSysIFList(vector<UINT64> &ifList)
  1804. {
  1805. }
  1806. void DriverHandle::GetSysIFList(vector<UINT64> &ifList)
  1807. {
  1808. }
  1809. //------------drv manager---------------------------
  1810. DriverManager::DriverManager()
  1811. {
  1812. m_pRootDrvHandle = NULL;
  1813. }
  1814. DriverManager::~DriverManager()
  1815. {
  1816. if (m_pRootDrvHandle == NULL)
  1817. return;
  1818. UnloadDrivers();
  1819. UnloadRootDriver();
  1820. }
  1821. bool DriverManager::UnRegistFromChannel(DWORD timeout)
  1822. {
  1823. string RootPath;
  1824. string ChannelPath;
  1825. BusUnitClient rootclient("DriverManager_unreg");
  1826. RootPath = (const char *)getRootpath();
  1827. ChannelPath = (const char*)getChannelRootpath();
  1828. if (RootPath == ChannelPath)
  1829. {
  1830. return false;
  1831. }
  1832. if (rootclient.Open(ChannelPath.c_str(), ACTION, timeout) == false)
  1833. {
  1834. //printf("Thread:%d,Open %s Failed\n", GetCurrentThreadId(), ChannelPath.c_str());
  1835. return false;
  1836. }
  1837. if (rootclient.DelDeviceDescrpt(RootPath.c_str()) != RET_SUCCEED)
  1838. {
  1839. //log here
  1840. rootclient.Close();
  1841. return false;
  1842. }
  1843. rootclient.Close();
  1844. return true;
  1845. }
  1846. void DriverManager::UnloadRootDriver()
  1847. {
  1848. UnRegistFromChannel(500);
  1849. //mLog::FINFO("UnRegistFromChannel Fnished");
  1850. Thread_Lock();
  1851. if (m_pRootDrvHandle)
  1852. {
  1853. //mLog::FINFO("Unload Root Driver Begin");
  1854. //vector<UINT64> ifList;
  1855. //m_pRootDrvHandle->GetSysIFList(ifList);
  1856. //UpdateSysIF(ifList,true);
  1857. delete m_pRootDrvHandle;
  1858. m_pRootDrvHandle = NULL;
  1859. //mLog::FINFO("Unload Root Driver Done");
  1860. }
  1861. Thread_UnLock();
  1862. }
  1863. void DriverManager::UnloadDrivers()
  1864. {
  1865. while (1)
  1866. {
  1867. DriverHandle* pHandle = NULL;
  1868. Thread_Lock();
  1869. vector<DriverHandle*>::iterator iter = m_SubDriverList.begin();
  1870. if (iter != m_SubDriverList.end())
  1871. {
  1872. pHandle = (*iter);
  1873. iter = m_SubDriverList.erase(iter);
  1874. break;
  1875. }
  1876. Thread_UnLock();
  1877. if (pHandle == NULL)
  1878. {
  1879. break;
  1880. }
  1881. //unload one
  1882. UnloadDynDrvHandle(pHandle);
  1883. UnloadDrvHandle(pHandle);
  1884. }
  1885. }
  1886. bool DriverManager::DynLoadDriver(DriverHandle *pDrv)
  1887. {
  1888. std::cout << "DriverManager::DynLoadDriver IN" << std::endl;
  1889. bool ret = false;
  1890. const char *pRoot = getRootpath();
  1891. //新流程如下.
  1892. //先注册
  1893. if (pDrv->DynRegistToRoot(pRoot) && pDrv->DynStartWork())
  1894. {
  1895. //后插入DPC
  1896. DynAddDriver(pDrv);
  1897. ret = true;
  1898. }
  1899. if (ret == false)
  1900. {
  1901. UnloadDynDrvHandle(pDrv);
  1902. }
  1903. return ret;
  1904. }
  1905. bool DriverManager::DynDriverInitLoadAll()
  1906. {
  1907. Thread_Lock();
  1908. int x = 0;
  1909. //auto it = m_SubDriverList.begin();
  1910. //while (it != m_SubDriverList.end()) {
  1911. // (*it)->InitLoadDynDriver();
  1912. // it++;
  1913. // x++;
  1914. //}
  1915. //mLog::FINFO("Begin");
  1916. std::cout << "DriverManager::DynDriverInitLoadAll m_SubDriverList.size: " << m_SubDriverList.size() << std::endl;
  1917. for (; x < m_SubDriverList.size(); x++)
  1918. {
  1919. std::cout << "SubDriverList InitLoadDynDriver " << x << std::endl;
  1920. //mLog::FINFO("SubDriverList InitLoadDynDriver {$}",x );
  1921. m_SubDriverList[x]->InitLoadDynDriver();
  1922. //mLog::FINFO("SubDriverList InitLoadDynDriver {$} over.", x);
  1923. std::cout << "SubDriverList InitLoadDynDriver " << x << " over"<<std::endl;
  1924. }
  1925. //mLog::FINFO("End");
  1926. Thread_UnLock();
  1927. return true;
  1928. }
  1929. void DriverManager::DynAddDriver(DriverHandle *pDrv)
  1930. {
  1931. std::cout << "DriverManager::DynAddDriver IN" << std::endl;
  1932. Thread_Lock();
  1933. //vector<UINT64> IFList;
  1934. //pDrv->DynGetSysIFList(IFList);
  1935. //UpdateSysIF(IFList);
  1936. for (int x = 0; x < m_SubDriverList.size(); x++)
  1937. {
  1938. if (pDrv == m_SubDriverList[x])
  1939. return;
  1940. }
  1941. m_SubDriverList.push_back(pDrv);
  1942. std::cout << "DriverManager::DynAddDriver m_SubDriverList.push_back->size:" << m_SubDriverList.size() << std::endl;
  1943. Thread_UnLock();
  1944. }
  1945. void DriverManager::AddDriver(DriverHandle *pDrv)
  1946. {
  1947. std::cout << "DriverManager::AddDriver IN" << std::endl;
  1948. Thread_Lock();
  1949. //vector<UINT64> IFList;
  1950. //pDrv->GetSysIFList(IFList);
  1951. //UpdateSysIF(IFList);
  1952. for (int x = 0; x < m_SubDriverList.size(); x++)
  1953. {
  1954. if (pDrv == m_SubDriverList[x])
  1955. return;
  1956. }
  1957. m_SubDriverList.push_back(pDrv);
  1958. std::cout << "DriverManager::AddDriver m_SubDriverList.push_back->size:"<< m_SubDriverList.size() << std::endl;
  1959. Thread_UnLock();
  1960. }
  1961. bool DriverManager::GetThreadsLogger(DWORD Tid, PVOID &pLogger)
  1962. {
  1963. Thread_Lock();
  1964. for (size_t i = 0; i < m_SubDriverList.size(); i++)
  1965. {
  1966. if (m_SubDriverList[i]->GetThreadsLogger(Tid, pLogger))
  1967. {
  1968. Thread_UnLock();
  1969. return true;
  1970. }
  1971. }
  1972. if (m_pRootDrvHandle)
  1973. {
  1974. if (m_pRootDrvHandle->GetThreadsLogger(Tid, pLogger))
  1975. {
  1976. Thread_UnLock();
  1977. return true;
  1978. }
  1979. }
  1980. Thread_UnLock();
  1981. return false;
  1982. }
  1983. bool DriverManager::UpdateRootDrvStatus(int Status)
  1984. {
  1985. std::cout << "UpdateRootDrvStatus IN" << std::endl;
  1986. bool ret = true;
  1987. if (m_pRootDrvHandle)
  1988. {
  1989. BusUnitClient rootclient("DriverManager_UpdateRootDrvStatus");
  1990. if (rootclient.Open(getRootpath(), ACTION) == false)
  1991. {
  1992. std::cout << "Thread:" << GetCurrentThreadId() << ",Open Failed" << std::endl;
  1993. return false;
  1994. }
  1995. if (rootclient.SetDeviceStatus(Status) != RET_SUCCEED)
  1996. {
  1997. std::cout << "Thread:" << GetCurrentThreadId() << ",SetDeviceStatus Failed" << std::endl;
  1998. //printf("Thread:%d,SetDeviceStatus Failed\n", GetCurrentThreadId());
  1999. ret = false;
  2000. }
  2001. rootclient.Close();
  2002. std::cout << "Thread:" << GetCurrentThreadId() << ",SetDeviceStatus Succeed" << std::endl;
  2003. //printf("Thread:%d,SetDeviceStatus Succeed\n", GetCurrentThreadId());
  2004. }
  2005. return ret;
  2006. }
  2007. void DriverManager::AddDevice(const char* devUri, LogicDevice* pDevice)
  2008. {
  2009. std::cout << "Add Device " << devUri<<" with "<< pDevice->GetCcosRootPath()<< endl;
  2010. //mLog::FINFO("Add Device {$} with {$}", devUri, pDevice->GetCcosRootPath());
  2011. m_CcosDeviceMap[devUri] = pDevice;
  2012. }
  2013. const char* DriverManager::GetAllDevice(void** pDevices, int& nDevNums)
  2014. {
  2015. m_strAllDeviceUri = "";
  2016. auto it = m_CcosDeviceMap.begin();
  2017. nDevNums = 0;
  2018. while (it != m_CcosDeviceMap.end())
  2019. {
  2020. m_strAllDeviceUri += it->first + ";";
  2021. pDevices[nDevNums++] = it->second;
  2022. it++;
  2023. }
  2024. return m_strAllDeviceUri.c_str();
  2025. }
  2026. bool DriverManager::LoadRootDrv()
  2027. {
  2028. //get busid
  2029. string busId = (const char*)getLocalEbusId();
  2030. if (busId.size() == 0)
  2031. {
  2032. //some log here
  2033. return false;
  2034. }
  2035. //load drv
  2036. DriverHandle *pHandle = new DriverHandle();
  2037. pHandle->m_AsRootDriver = true;//need to set this
  2038. string path = GetProcessDirectory() + "/" + "rootdrv.xml";
  2039. if (pHandle->LoadDriver(path.c_str(), false))
  2040. {
  2041. //load succeed
  2042. //printf("Load %s succeed\n", path.c_str());
  2043. //Load only device,not include driver .!!!!!!
  2044. //hook it to tree
  2045. map<string, UINT64>::iterator iter = pHandle->m_DeviceFullPathMap.begin();
  2046. while (iter != pHandle->m_DeviceFullPathMap.end())
  2047. {
  2048. string path = iter->first;
  2049. UINT64 procId = (UINT64)getpid();
  2050. UINT64 addr = iter->second;
  2051. if (path == string(getRootpath()))
  2052. {
  2053. //make tree node
  2054. if (g_DrvTree.AddTree(path.c_str(), procId, addr))
  2055. {
  2056. //done
  2057. //save it
  2058. m_pRootDrvHandle = (pHandle);
  2059. //
  2060. LogicDriver *driver = m_pRootDrvHandle->GetDpcDriver();
  2061. driver->SetTreeRoot(true);
  2062. driver->SetClientRootID(path.c_str(), path.c_str()+1);
  2063. std::cout << "driver SetClientRootID Real CCOS path ["
  2064. << path << "] from ["
  2065. << driver->GetCcosRootPath() << "]" << std::endl;
  2066. //mLog::FINFO("driver SetClientRootID Real CCOS path [{$}] ", path, driver->GetCcosRootPath());
  2067. //driver->SubscribeSelf(); //这个是OemBusUnit对象,可以支持Open
  2068. //start it
  2069. {
  2070. //这里有个瑕疵:
  2071. //Driver带着一个Device加载,但是Driver意义不大,故在AddTree中过滤掉.
  2072. //但是SysIF中是带着Driver的地址的....
  2073. //vector<UINT64> ifList;
  2074. //m_pRootDrvHandle->GetSysIFList(ifList);
  2075. //UpdateSysIF(ifList);
  2076. return m_pRootDrvHandle->StartWork();
  2077. }
  2078. }
  2079. }
  2080. ++iter;
  2081. }
  2082. }
  2083. //printf("Load %s failed\n", path.c_str());
  2084. delete pHandle;
  2085. return false;
  2086. }
  2087. bool DriverManager::LoadDrv(ResDataObject &ConfigContext)
  2088. {
  2089. //NOT FINISHED YET
  2090. assert(0);
  2091. return false;
  2092. }
  2093. bool DriverManager::LoadDriver(const char *pConfigFilepath)
  2094. {
  2095. bool ret = false;
  2096. DriverHandle *pHandle = new DriverHandle();
  2097. string path = pConfigFilepath;
  2098. std::cout << "------------ CDI: DriverManager::LoadDriver" << pConfigFilepath << std::endl;
  2099. if (pHandle->LoadDriver(path.c_str(), true))
  2100. {
  2101. //load succeed
  2102. const char *pRoot = getRootpath();
  2103. //save it
  2104. AddDriver(pHandle);//这点比较闹心,已经注册到系统,所以...
  2105. LogicDriver *pRootDriver = m_pRootDrvHandle->GetDpcDriver();
  2106. LogicDriver* pClientDriver = pHandle->GetDpcDriver();
  2107. pRootDriver->AddEbusChildren(pClientDriver, pClientDriver->GetRootPath().c_str());
  2108. //pHandle->get
  2109. //start it
  2110. ret = pHandle->StartWork();
  2111. if (ret)
  2112. {
  2113. //regist to tree
  2114. ret = pHandle->RegistToRoot(pRoot);
  2115. }
  2116. if (ret == false)
  2117. {
  2118. //所以得先卸载from系统,然后再清除...
  2119. UnloadDrvHandle(pHandle);
  2120. }
  2121. }
  2122. else
  2123. {
  2124. delete pHandle;
  2125. }
  2126. return ret;
  2127. }
  2128. bool DriverManager::UnloadDynDrvHandle(DriverHandle *pHandle)
  2129. {
  2130. bool ret = false;
  2131. if (pHandle)
  2132. {
  2133. Thread_Lock();
  2134. {
  2135. //kick the SysIF from the map
  2136. //vector<UINT64> ifList;
  2137. //pHandle->DynGetSysIFList(ifList);
  2138. //UpdateSysIF(ifList, true);
  2139. pHandle->UnloadDynDriver();
  2140. ret = true;
  2141. }
  2142. Thread_UnLock();
  2143. }
  2144. return ret;
  2145. }
  2146. bool DriverManager::UnloadDrvHandle(DriverHandle *pHandle)
  2147. {
  2148. bool ret = false;
  2149. if (pHandle)
  2150. {
  2151. Thread_Lock();
  2152. vector<DriverHandle*>::iterator iter = find(m_SubDriverList.begin(), m_SubDriverList.end(), pHandle);
  2153. if (iter != m_SubDriverList.end())
  2154. {
  2155. //do unload work
  2156. //kick it from list
  2157. m_SubDriverList.erase(iter);
  2158. //kick the SysIF from the map
  2159. //vector<UINT64> ifList;
  2160. //pHandle->GetSysIFList(ifList);
  2161. //UpdateSysIF(ifList, true);
  2162. ret = true;
  2163. }
  2164. Thread_UnLock();
  2165. //kick the logicdevice from root
  2166. delete pHandle;
  2167. //if (pHandle->UnloadDriver() == false)
  2168. //{
  2169. // //put some log here
  2170. //}
  2171. }
  2172. return ret;
  2173. }
  2174. //return next try tick
  2175. bool DriverHandle::InitLoadDynDriver()
  2176. {
  2177. bool ret = true;
  2178. if (!m_bCommonDPC)
  2179. return true;
  2180. LogicDevice* pDevice = m_pDPC;
  2181. if (pDevice)
  2182. {
  2183. LogicDriver* pDriver = (LogicDriver*)pDevice;
  2184. //check connection
  2185. //if (pDriver->GetConnectionStatus())
  2186. //{
  2187. // //ok
  2188. // return false;
  2189. //}
  2190. //makesure it's disconnected
  2191. //if (m_pDrvHandle)
  2192. {
  2193. ////mLog::FINFO("Try UnloadDynDrv ");
  2194. //UnloadDynDriver();
  2195. //if (g_DrvManager.UnloadDynDrvHandle(this))
  2196. {
  2197. //do the connection
  2198. //mLog::FINFO("Try Connect ");
  2199. //if (pDriver->Connect())
  2200. bool bret = pDriver->Connect();
  2201. //mLog::FINFO("Try LoadDynDriver with Connect Result ={$} ", bret);
  2202. //if(bret)
  2203. {
  2204. //do load logicdevices
  2205. if (LoadDynDriver())
  2206. {
  2207. //mLog::FINFO("Try DrvManager::DynLoadDriver ");
  2208. if (g_DrvManager.DynLoadDriver(this))
  2209. {
  2210. //ok
  2211. //mLog::FINFO("Try AutomaticConnect Succeed");
  2212. return true;
  2213. }
  2214. //mLog::FERROR("Try DrvManager::DynLoadDriver Failed");
  2215. }
  2216. else
  2217. {
  2218. UnloadDynDriver();
  2219. }
  2220. //report error here
  2221. //mLog::FERROR("Try LoadDynDriver Failed");
  2222. pDriver->DisConnect();
  2223. return false;
  2224. }
  2225. }
  2226. }
  2227. }
  2228. return false;
  2229. }