DriverManager.cpp 64 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786
  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. std::cout << "CDI Try CompleteInit "<< iter->first << std::endl;
  1152. //mLog::FINFO("CDI Try CompleteInit {$}", iter->first);
  1153. if (Init)
  1154. {
  1155. iter->second->CompleteInit();
  1156. }
  1157. else
  1158. {
  1159. iter->second->CompleteUnInit();
  1160. }
  1161. //mLog::FINFO("CDI CompleteInit over.");
  1162. ++iter;
  1163. }
  1164. //sub drivers
  1165. for (size_t i = 0; i < m_hSubDriverList.Size(); i++)
  1166. {
  1167. //mLog::FINFO("CDI m_hSubDriverList try CompleteStaticInit {$}", i);
  1168. m_hSubDriverList[i]->CompleteStaticInit(Init);
  1169. }
  1170. }
  1171. }
  1172. void DriverHandle::CompleteDynInit(bool Init)
  1173. {
  1174. //mLog::FINFO("CDI {$}", Init);
  1175. //init this
  1176. map<string, LogicDevice*>::iterator iter = m_DynRootDeviceMap.begin();
  1177. while (iter != m_DynRootDeviceMap.end())
  1178. {
  1179. std::cout << "CDI Try Dyn CompleteInit " << iter->first << std::endl;
  1180. //mLog::FINFO("CDI Try Dyn CompleteInit {$}", iter->first);
  1181. if (Init)
  1182. {
  1183. iter->second->CompleteInit();
  1184. }
  1185. else
  1186. {
  1187. iter->second->CompleteUnInit();
  1188. }
  1189. ++iter;
  1190. }
  1191. //sub drivers
  1192. for (size_t i = 0; i < m_DynSubDriverList.Size(); i++)
  1193. {
  1194. //mLog::FINFO("CDI m_DynSubDriverList try CompleteStaticInit {$}", i);
  1195. m_DynSubDriverList[i]->CompleteDynInit(Init);
  1196. }
  1197. }
  1198. bool DriverHandle::StartWork()
  1199. {
  1200. bool ret = true;
  1201. //mLog::FINFO("StartWork Begin with {$}", m_DllFileTitle);
  1202. std::cout << "StartWork Begin with " << m_DllFileTitle << std::endl;
  1203. //线程启动之前通知设备单元,初始化全部结束
  1204. CompleteStaticInit(true);
  1205. //工作线程在卸载动态单元的时候,会有Pause通知线程的操作.
  1206. //如果工作线程太快,通知线程还没有启动,工作线程已经进入Pause流程,导致死循环.
  1207. //mLog::FINFO("CDI DriverHandle::StartWork m_NotifyThread.StartThread()");
  1208. m_NotifyThread.SetName((m_DllFileTitle + "_DriverHandle::Notify").c_str());
  1209. //mLog::FINFO("Start NotifyThread");
  1210. ret = m_NotifyThread.StartThread();//notify first
  1211. //mLog::FINFO("Start NotifyThread over with TID=[{$}]", m_NotifyThread.GetTID());
  1212. /*
  1213. map<Dual_Driver_Thread*, vector<UINT64>>::iterator iter = m_ThreadMap.begin();
  1214. while (iter != m_ThreadMap.end())
  1215. {
  1216. printf("iter->first->StartThread() \n ");
  1217. if (iter->first->StartThread() == false)
  1218. {
  1219. return false;
  1220. }
  1221. iter++;
  1222. }*/
  1223. //sub drivers
  1224. if (ret)
  1225. {
  1226. for (size_t i = 0; i < m_hSubDriverList.Size(); i++)
  1227. {
  1228. //mLog::FINFO("StartWork Start SubDriverList NotifyThread {$}", i);
  1229. ret &= m_hSubDriverList[i]->StartWork();
  1230. //mLog::FINFO("StartWork Start SubDriverList Over NotifyThread {$}", i);
  1231. if (ret == false)
  1232. {
  1233. return ret;
  1234. }
  1235. }
  1236. }
  1237. //mLog::FINFO("StartWork Over with {$}" , m_DllFileTitle);
  1238. return ret;
  1239. }
  1240. void DriverHandle::StopWork()
  1241. {
  1242. bool ret = false;
  1243. //map<Dual_Driver_Thread*, vector<UINT64>>::iterator iter = m_ThreadMap.begin();
  1244. /* while (iter != m_ThreadMap.end())
  1245. {
  1246. if (iter->first->StopThread() == false)
  1247. {
  1248. //mLog::FINFO("{$} Drv:StopThread Used Terminate!!!!!!!!!!!!!!!", m_DllFileTitle.c_str());
  1249. }
  1250. iter++;
  1251. }*/
  1252. if (m_NotifyThread.StopThread() == false)
  1253. {
  1254. //mLog::FINFO("{$} Drv:StopNotifyThread Used Terminate!!!!!!!!!!!!!!!", m_DllFileTitle.c_str());
  1255. }
  1256. CompleteStaticInit(false);
  1257. //delete thread
  1258. /*
  1259. iter = m_ThreadMap.begin();
  1260. while (iter != m_ThreadMap.end())
  1261. {
  1262. delete (iter->first);
  1263. iter++;
  1264. }*/
  1265. //mLog::FINFO("{$} Drv:delete Thread Done", m_DllFileTitle.c_str());
  1266. //mLog::FINFO("{$} Drv:StopThread Done", m_DllFileTitle.c_str());
  1267. }
  1268. bool DriverHandle::DynClearDriverHandles()
  1269. {
  1270. vector<UINT64> ifList;
  1271. DynGetSysIFList(ifList);
  1272. g_HandleManager.ClearLocalDrvHandles(ifList);
  1273. return true;
  1274. }
  1275. bool DriverHandle::ClearDriverHandles()
  1276. {
  1277. vector<UINT64> ifList;
  1278. GetSysIFList(ifList);
  1279. g_HandleManager.ClearLocalDrvHandles(ifList);
  1280. return true;
  1281. }
  1282. bool DriverHandle::LoadDynDriver()
  1283. {
  1284. bool ret = false;
  1285. DWORD timeout = INFINITE;
  1286. string rootpath = "";
  1287. //check connection first
  1288. if (((LogicDriver*)m_pDPC)->GetConnectionStatus() == false)
  1289. {
  1290. //mLog::FERROR( "DPC Connection is broken");
  1291. //return ret;
  1292. }
  1293. //after connect succed
  1294. if (m_AsRootDriver == false)
  1295. {
  1296. rootpath = (const char*)getRootpath();
  1297. timeout = 2000;
  1298. }
  1299. else
  1300. {
  1301. rootpath = (const char*)getChannelRootpath();
  1302. }
  1303. //Get logic devices
  1304. //make path & work path etc
  1305. //make Threads
  1306. //start dyn Threads
  1307. m_NotifyThread.PauseThread();
  1308. if (DynInitHandle(this, m_pDPC))
  1309. {
  1310. //if (DynRegistToRoot(rootpath.c_str(),timeout))
  1311. //{
  1312. // return DynStartWork();
  1313. //}
  1314. ret = true;
  1315. }
  1316. m_NotifyThread.ResumeThread();
  1317. return ret;
  1318. }
  1319. bool DriverHandle::UnloadDynDriver()
  1320. {
  1321. m_NotifyThread.PauseThread();
  1322. if (m_HandleStatus == true)
  1323. {
  1324. DynUnRegistFromRoot();//unregist from root
  1325. }
  1326. //stop thread
  1327. DynStopWork();
  1328. //clear HandleList
  1329. if (DynClearDriverHandles() == false)
  1330. {
  1331. //put some log here
  1332. //mLog::FINFO("{$} Drv:Dyn Clear Handle Failed", m_DllFileTitle.c_str());
  1333. }
  1334. //mLog::FINFO("{$} Drv:Unload DPC", m_DllFileTitle.c_str());
  1335. if (m_pDPC)
  1336. {
  1337. //unload devices
  1338. if (m_pDynRootDevice)
  1339. {
  1340. //mLog::FINFO("{$} Drv:UnloadDriver", m_DllFileTitle.c_str());
  1341. m_pDPC->UnloadLogicDevices();
  1342. ((LogicDriver*)m_pDPC)->DisConnect();
  1343. m_pDynRootDevice = NULL;//suppose it deleted DPC::UnloadDriver
  1344. }
  1345. }
  1346. //delete sysif
  1347. //map<UINT64, ServerSysIF*>::iterator iter1 = m_DynAddressMap.begin();
  1348. //while (iter1 != m_DynAddressMap.end())
  1349. //{
  1350. // delete (iter1->second);
  1351. // iter1++;
  1352. //}
  1353. ////mLog::FINFO("%s Drv:Dyn delete sysif Done", m_DllFileTitle.c_str());
  1354. m_NotifyThread.ResumeThread();
  1355. //
  1356. //clear all maps
  1357. m_DynDeviceFullPathMap.clear();
  1358. m_DynRootDeviceMap.clear();
  1359. //m_DynAddressMap.clear();
  1360. //m_DynThreadMap.clear();// thread : DeviceSysIF[]
  1361. //mLog::FINFO("{$} Drv:UnloadDevice Done", m_DllFileTitle.c_str());
  1362. return true;
  1363. }
  1364. bool DriverHandle::UnloadDriver()
  1365. {
  1366. //mLog::FINFO("UnloadDrv:{$}", m_DllFileTitle.c_str());
  1367. //unload sub drivers
  1368. for (size_t i = 0; i < m_hSubDriverList.Size(); i++)
  1369. {
  1370. delete m_hSubDriverList[i];
  1371. }
  1372. m_hSubDriverList.Clear();
  1373. UnloadDynDriver();
  1374. if (m_HandleStatus == true)
  1375. {
  1376. // root驱动卸载过程中,把自己从Channel卸载,
  1377. //这个操作因为在自我毁灭过程中进行,所以已经处于加锁状态.
  1378. //因为加锁,外部命令无法反馈到当前驱动模块.
  1379. //
  1380. if (m_AsRootDriver == false)
  1381. {
  1382. UnRegistFromRoot();//unregist from root
  1383. }
  1384. }
  1385. //stop thread
  1386. StopWork();
  1387. //clear HandleList
  1388. if (ClearDriverHandles() == false)
  1389. {
  1390. //put some log here
  1391. //mLog::FINFO("{$} Drv:Clear Handle Failed", m_DllFileTitle.c_str());
  1392. }
  1393. //mLog::FINFO( "{$} Drv:Unload DPC", m_DllFileTitle.c_str());
  1394. if (m_pDPC)
  1395. {
  1396. //unload devices
  1397. if (m_pRootDevice /* || m_pDPC->GetDPCType() == DPCDRIVER_MDPC*/)
  1398. {
  1399. //mLog::FINFO( "{$} Drv:UnloadDriver", m_DllFileTitle.c_str());
  1400. m_pDPC->UnloadDriver();
  1401. m_pRootDevice = NULL;//suppose it deleted DPC::UnloadDriver
  1402. }
  1403. //disconnect
  1404. //m_pDPC->DisConnect();
  1405. ////mLog::FINFO( "{$} Drv:DisConnectDPC Done", m_DllFileTitle.c_str());
  1406. //only root DPC needs to be deleted
  1407. if (m_IsRootHandle)
  1408. {
  1409. //unload dpc
  1410. ReleaseDriverDPC dpcfunc = (ReleaseDriverDPC)dlsym(m_DllFileHandle, "ReleaseDriverDPC");
  1411. if (dpcfunc)
  1412. {
  1413. dpcfunc(m_pDPC);
  1414. //mLog::FINFO("{$} Drv:ReleaseDPC Done", m_DllFileTitle.c_str());
  1415. }
  1416. }
  1417. //else
  1418. //{
  1419. // delete m_pDPC;//不要擅自删除DPC,非ROOT DPC让驱动自行删除.
  1420. //}
  1421. //FreeLibrary(m_DllFileHandle);
  1422. m_pDPC = NULL;
  1423. }
  1424. //delete sysif
  1425. //map<UINT64, ServerSysIF*>::iterator iter1 = m_AddressMap.begin();
  1426. //while (iter1 != m_AddressMap.end())
  1427. //{
  1428. // //delete (iter1->second);
  1429. // iter1++;
  1430. //}
  1431. ////mLog::FINFO( "{$} Drv:delete sysif Done", m_DllFileTitle.c_str());
  1432. //clear all maps
  1433. m_DllFileHandle = NULL;
  1434. m_pRootDevice = NULL;
  1435. m_pDPC = NULL;
  1436. m_Probe.clear();
  1437. m_Connection.clear();
  1438. m_WorkPath = "";
  1439. m_DeviceFullPathMap.clear();
  1440. m_CcosDeviceFullPathMap.clear();
  1441. m_CcosDynDeviceFullPathMap.clear();
  1442. m_RootDeviceMap.clear();
  1443. //m_AddressMap.clear();
  1444. //m_ThreadMap.clear();// thread : DeviceSysIF[]
  1445. //if (m_pLog)
  1446. //{
  1447. // ReleseLogger(m_pLog);
  1448. // m_pLog = NULL;
  1449. //}
  1450. m_HandleStatus = false;
  1451. //mLog::FINFO( "{$} Drv:UnloadDriver Done", m_DllFileTitle.c_str());
  1452. return true;
  1453. }
  1454. bool DriverHandle::RegistToRoot(const char* RootPath, DWORD timeout)
  1455. {
  1456. BusUnitClient rootclient("DriverHandle_regroot");
  1457. //mLog::FINFO("Begain [{$}]", RootPath);
  1458. //sublist first
  1459. for (size_t i = 0; i < m_hSubDriverList.Size(); i++)
  1460. {
  1461. if (m_hSubDriverList[i]->RegistToRoot(RootPath, timeout) == false)
  1462. {
  1463. //mLog::FERROR("RegistToRoot Failed [{$}]", RootPath);
  1464. return false;
  1465. }
  1466. }
  1467. //me second
  1468. if (rootclient.Open(RootPath, ACTION, timeout) == false)
  1469. {
  1470. //mLog::FERROR("Open Failed [{$}]", RootPath);
  1471. //printf("Thread:%d,Open Failed\n", GetCurrentThreadId());
  1472. return false;
  1473. }
  1474. if (rootclient.GetExitFlag() == 0)
  1475. {
  1476. map<string, UINT64>::iterator iter = m_DeviceFullPathMap.begin();
  1477. while (iter != m_DeviceFullPathMap.end())
  1478. {
  1479. string path = iter->first;
  1480. UINT64 procId = (UINT64)getpid();
  1481. UINT64 addr = iter->second;
  1482. string TargetType = "Device";
  1483. if (iter->first == m_DriverFullPathMap)
  1484. {
  1485. TargetType = "Driver";
  1486. }
  1487. //regist driver
  1488. //mLog::FINFO("AddDeviceDescrpt [{$}] Try [{$}]", RootPath, path);
  1489. RET_STATUS ret = (RET_STATUS)rootclient.AddDeviceDescrpt(path.c_str(), TargetType.c_str(),(const char*)getLocalMachineId(), procId, addr);
  1490. if (ret == RET_FAILED)
  1491. {
  1492. //mLog::FERROR("AddDeviceDescrpt [{$}] Failed [{$}]", RootPath, path);
  1493. //log here
  1494. //try updadate this driver handle
  1495. //assert(0);//NOT FINISHED YET
  1496. //unhook it
  1497. map<string, UINT64>::iterator iterBk = m_DeviceFullPathMap.begin();
  1498. while (iterBk != iter)
  1499. {
  1500. if (rootclient.DelDeviceDescrpt(iterBk->first.c_str()) != RET_SUCCEED)
  1501. {
  1502. //log here
  1503. //shit on going!!!!
  1504. rootclient.Close();
  1505. return false;
  1506. }
  1507. ++iterBk;
  1508. }
  1509. rootclient.Close();
  1510. return false;
  1511. }
  1512. else if (ret == RET_INVALID)
  1513. {
  1514. return false;
  1515. }
  1516. ++iter;
  1517. }
  1518. // Ccos PATH
  1519. iter = m_CcosDeviceFullPathMap.begin();
  1520. while (iter != m_CcosDeviceFullPathMap.end())
  1521. {
  1522. string path = iter->first;
  1523. UINT64 procId = (UINT64)getpid();
  1524. UINT64 addr = iter->second;
  1525. string TargetType = "Device";
  1526. if (iter->first == m_DriverFullPathMap)
  1527. {
  1528. TargetType = "Driver";
  1529. }
  1530. //regist driver
  1531. RET_STATUS ret = (RET_STATUS)rootclient.AddDeviceDescrpt(path.c_str(), TargetType.c_str(), (const char*)getLocalMachineId(), procId, addr);
  1532. if (ret == RET_FAILED)
  1533. {
  1534. //log here
  1535. //try updadate this driver handle
  1536. //assert(0);//NOT FINISHED YET
  1537. //mLog::FERROR("AddDeviceDescrpt [{$}] Failed [{$}]", RootPath, path);
  1538. //unhook it
  1539. map<string, UINT64>::iterator iterBk = m_CcosDeviceFullPathMap.begin();
  1540. while (iterBk != iter)
  1541. {
  1542. if (rootclient.DelDeviceDescrpt(iterBk->first.c_str()) != RET_SUCCEED)
  1543. {
  1544. //log here
  1545. //shit on going!!!!
  1546. rootclient.Close();
  1547. return false;
  1548. }
  1549. ++iterBk;
  1550. }
  1551. rootclient.Close();
  1552. return false;
  1553. }
  1554. else if (ret == RET_INVALID)
  1555. {
  1556. return false;
  1557. }
  1558. ++iter;
  1559. }
  1560. }
  1561. rootclient.Close();
  1562. RegistToDrvObject(false,true);//后执行,保证Regist成功
  1563. //mLog::FINFO("End of Success. [{$}]", RootPath);
  1564. return true;
  1565. }
  1566. void DriverHandle::RegistToDrvObject(bool dyn, bool regist)
  1567. {
  1568. map<string, UINT64>::iterator iter;
  1569. if (dyn)
  1570. {
  1571. iter = m_DynDeviceFullPathMap.begin();
  1572. while (iter != m_DynDeviceFullPathMap.end())
  1573. {
  1574. string path = iter->first;
  1575. //regist driver
  1576. if (regist)
  1577. {
  1578. ((LogicDriver*)m_pDPC)->AddDevice(path.c_str());
  1579. }
  1580. else
  1581. {
  1582. ((LogicDriver*)m_pDPC)->DelDevice(path.c_str());
  1583. }
  1584. ++iter;
  1585. }
  1586. }
  1587. else
  1588. {
  1589. /*
  1590. if (m_pParent)
  1591. {
  1592. //mdpc parent exist,this is SubDPC object
  1593. string path = m_DriverFullPathMap;
  1594. //regist DPC2MDPC
  1595. if (regist)
  1596. {
  1597. ((LogicDriver*)m_pParent)->AddDevice(path.c_str());
  1598. }
  1599. else
  1600. {
  1601. ((LogicDriver*)m_pParent)->DelDevice(path.c_str());
  1602. }
  1603. }*/
  1604. //go normal
  1605. iter = m_DeviceFullPathMap.begin();
  1606. while (iter != m_DeviceFullPathMap.end())
  1607. {
  1608. string path = iter->first;
  1609. if (m_DriverFullPathMap == path)
  1610. {
  1611. //exept it's own driver
  1612. ++iter;
  1613. continue;
  1614. }
  1615. //regist driver
  1616. if (regist)
  1617. {
  1618. ((LogicDriver*)m_pDPC)->AddDevice(path.c_str());
  1619. }
  1620. else
  1621. {
  1622. ((LogicDriver*)m_pDPC)->DelDevice(path.c_str());
  1623. }
  1624. ++iter;
  1625. }
  1626. }
  1627. }
  1628. bool DriverHandle::DynRegistToRoot(const char* RootPath, DWORD timeout)
  1629. {
  1630. BusUnitClient rootclient("DriverHandle_dyn_reg");
  1631. //me second
  1632. if (rootclient.Open(RootPath, ACTION, timeout) == false)
  1633. {
  1634. std::cout << "Thread:" << GetCurrentThreadId() << ", Open RootPath failed. Path: " << RootPath << ", Timeout: " << timeout << std::endl;
  1635. return false;
  1636. }
  1637. if (rootclient.GetExitFlag() == 0)
  1638. {
  1639. map<string, UINT64>::iterator iter = m_DynDeviceFullPathMap.begin();
  1640. while (iter != m_DynDeviceFullPathMap.end())
  1641. {
  1642. string TargetType = "Device";
  1643. string path = iter->first;
  1644. UINT64 procId = (UINT64)getpid();
  1645. UINT64 addr = iter->second;
  1646. //regist driver
  1647. if (rootclient.AddDeviceDescrpt(path.c_str(), TargetType.c_str(),(const char*)getLocalMachineId(), procId, addr) != RET_SUCCEED)
  1648. {
  1649. //log here
  1650. //unhook it
  1651. std::cout << "Thread:" << GetCurrentThreadId() << ", Starting rollback for m_DynDeviceFullPathMap. Current path: " << path << std::endl;
  1652. map<string, UINT64>::iterator iterBk = m_DynDeviceFullPathMap.begin();
  1653. while (iterBk != iter)
  1654. {
  1655. if (rootclient.DelDeviceDescrpt(iterBk->first.c_str()) != RET_SUCCEED)
  1656. {
  1657. //log here
  1658. //shit on going!!!!
  1659. std::cout << "Thread:" << GetCurrentThreadId() << ", Failed to rollback device. Path: " << iterBk->first << ". Critical error!" << std::endl;
  1660. rootclient.Close();
  1661. return false;
  1662. }
  1663. ++iterBk;
  1664. }
  1665. rootclient.Close();
  1666. return false;
  1667. }
  1668. ++iter;
  1669. }
  1670. iter = m_CcosDynDeviceFullPathMap.begin();
  1671. while (iter != m_CcosDynDeviceFullPathMap.end())
  1672. {
  1673. string TargetType = "Device";
  1674. string path = iter->first;
  1675. UINT64 procId = (UINT64)getpid();
  1676. UINT64 addr = iter->second;
  1677. //regist driver
  1678. if (rootclient.AddDeviceDescrpt(path.c_str(), TargetType.c_str(), (const char*)getLocalMachineId(), procId, addr) != RET_SUCCEED)
  1679. {
  1680. std::cout << "Thread:" << GetCurrentThreadId() << ", Failed to register Ccos device. Path: " << path << std::endl;
  1681. //log here
  1682. //unhook it
  1683. map<string, UINT64>::iterator iterBk = m_CcosDynDeviceFullPathMap.begin();
  1684. while (iterBk != iter)
  1685. {
  1686. if (rootclient.DelDeviceDescrpt(iterBk->first.c_str()) != RET_SUCCEED)
  1687. {
  1688. //log here
  1689. //shit on going!!!!
  1690. rootclient.Close();
  1691. return false;
  1692. }
  1693. ++iterBk;
  1694. }
  1695. rootclient.Close();
  1696. return false;
  1697. }
  1698. ++iter;
  1699. }
  1700. }
  1701. rootclient.Close();
  1702. RegistToDrvObject();//后执行,保证Regist成功
  1703. return true;
  1704. }
  1705. bool DriverHandle::DynUnRegistFromRoot()
  1706. {
  1707. DWORD timeout = 0;
  1708. string rootpath;
  1709. BusUnitClient rootclient("DriverHandle_UnReg");
  1710. RegistToDrvObject(true,false);//先执行,不管unregist成功与否
  1711. if (m_AsRootDriver == false)
  1712. {
  1713. rootpath = (const char*)getRootpath();
  1714. timeout = 2000;
  1715. }
  1716. else
  1717. {
  1718. //rootpath = (const char*)getChannelRootpath();
  1719. return true;//DriverManager中已经卸载了Root在Channel中的东西
  1720. }
  1721. if (m_AsRootDriver)
  1722. {
  1723. timeout = 2000;
  1724. }
  1725. else
  1726. {
  1727. timeout = INFINITE;
  1728. }
  1729. if (rootclient.Open(rootpath.c_str(), ACTION, timeout) == false)
  1730. {
  1731. //printf("Thread:%d,Open %s Failed\n", GetCurrentThreadId(), rootpath.c_str());
  1732. return false;
  1733. }
  1734. map<string, UINT64>::iterator iter = m_DynDeviceFullPathMap.begin();
  1735. while (iter != m_DynDeviceFullPathMap.end())
  1736. {
  1737. string path = iter->first;
  1738. //regist driver
  1739. if (rootclient.DelDeviceDescrpt(path.c_str()) != RET_SUCCEED)
  1740. {
  1741. //log here
  1742. rootclient.Close();
  1743. return false;
  1744. }
  1745. ++iter;
  1746. }
  1747. iter = m_CcosDynDeviceFullPathMap.begin();
  1748. while (iter != m_CcosDynDeviceFullPathMap.end())
  1749. {
  1750. string path = iter->first;
  1751. //regist driver
  1752. if (rootclient.DelDeviceDescrpt(path.c_str()) != RET_SUCCEED)
  1753. {
  1754. //log here
  1755. rootclient.Close();
  1756. return false;
  1757. }
  1758. ++iter;
  1759. }
  1760. rootclient.Close();
  1761. return true;
  1762. }
  1763. bool DriverHandle::UnRegistFromRoot()
  1764. {
  1765. DWORD timeout = 0;
  1766. string rootpath;
  1767. BusUnitClient rootclient("DriverHandle_unregroot");
  1768. RegistToDrvObject(false, false);//先执行,不管unregist成功与否
  1769. if (m_AsRootDriver == false)
  1770. {
  1771. rootpath = (const char*)getRootpath();
  1772. timeout = 2000;
  1773. }
  1774. else
  1775. {
  1776. rootpath = (const char*)getChannelRootpath();
  1777. }
  1778. if (m_AsRootDriver)
  1779. {
  1780. timeout = 2000;
  1781. }
  1782. else
  1783. {
  1784. timeout = INFINITE;
  1785. }
  1786. if (rootclient.Open(rootpath.c_str(), ACTION, timeout) == false)
  1787. {
  1788. //printf("Thread:%d,Open %s Failed\n", GetCurrentThreadId(),rootpath.c_str());
  1789. return false;
  1790. }
  1791. map<string, UINT64>::iterator iter = m_DeviceFullPathMap.begin();
  1792. while (iter != m_DeviceFullPathMap.end())
  1793. {
  1794. string path = iter->first;
  1795. //regist driver
  1796. if (rootclient.DelDeviceDescrpt(path.c_str()) != RET_SUCCEED)
  1797. {
  1798. //log here
  1799. rootclient.Close();
  1800. return false;
  1801. }
  1802. ++iter;
  1803. }
  1804. rootclient.Close();
  1805. return true;
  1806. }
  1807. void DriverHandle::DynGetSysIFList(vector<UINT64> &ifList)
  1808. {
  1809. }
  1810. void DriverHandle::GetSysIFList(vector<UINT64> &ifList)
  1811. {
  1812. }
  1813. //------------drv manager---------------------------
  1814. DriverManager::DriverManager()
  1815. {
  1816. m_pRootDrvHandle = NULL;
  1817. }
  1818. DriverManager::~DriverManager()
  1819. {
  1820. if (m_pRootDrvHandle == NULL)
  1821. return;
  1822. UnloadDrivers();
  1823. UnloadRootDriver();
  1824. }
  1825. bool DriverManager::UnRegistFromChannel(DWORD timeout)
  1826. {
  1827. string RootPath;
  1828. string ChannelPath;
  1829. BusUnitClient rootclient("DriverManager_unreg");
  1830. RootPath = (const char *)getRootpath();
  1831. ChannelPath = (const char*)getChannelRootpath();
  1832. if (RootPath == ChannelPath)
  1833. {
  1834. return false;
  1835. }
  1836. if (rootclient.Open(ChannelPath.c_str(), ACTION, timeout) == false)
  1837. {
  1838. //printf("Thread:%d,Open %s Failed\n", GetCurrentThreadId(), ChannelPath.c_str());
  1839. return false;
  1840. }
  1841. if (rootclient.DelDeviceDescrpt(RootPath.c_str()) != RET_SUCCEED)
  1842. {
  1843. //log here
  1844. rootclient.Close();
  1845. return false;
  1846. }
  1847. rootclient.Close();
  1848. return true;
  1849. }
  1850. void DriverManager::UnloadRootDriver()
  1851. {
  1852. UnRegistFromChannel(500);
  1853. //mLog::FINFO("UnRegistFromChannel Fnished");
  1854. Thread_Lock();
  1855. if (m_pRootDrvHandle)
  1856. {
  1857. //mLog::FINFO("Unload Root Driver Begin");
  1858. //vector<UINT64> ifList;
  1859. //m_pRootDrvHandle->GetSysIFList(ifList);
  1860. //UpdateSysIF(ifList,true);
  1861. delete m_pRootDrvHandle;
  1862. m_pRootDrvHandle = NULL;
  1863. //mLog::FINFO("Unload Root Driver Done");
  1864. }
  1865. Thread_UnLock();
  1866. }
  1867. void DriverManager::UnloadDrivers()
  1868. {
  1869. while (1)
  1870. {
  1871. DriverHandle* pHandle = NULL;
  1872. Thread_Lock();
  1873. vector<DriverHandle*>::iterator iter = m_SubDriverList.begin();
  1874. if (iter != m_SubDriverList.end())
  1875. {
  1876. pHandle = (*iter);
  1877. iter = m_SubDriverList.erase(iter);
  1878. break;
  1879. }
  1880. Thread_UnLock();
  1881. if (pHandle == NULL)
  1882. {
  1883. break;
  1884. }
  1885. //unload one
  1886. UnloadDynDrvHandle(pHandle);
  1887. UnloadDrvHandle(pHandle);
  1888. }
  1889. }
  1890. bool DriverManager::DynLoadDriver(DriverHandle *pDrv)
  1891. {
  1892. std::cout << "DriverManager::DynLoadDriver IN" << std::endl;
  1893. bool ret = false;
  1894. const char *pRoot = getRootpath();
  1895. //新流程如下.
  1896. //先注册
  1897. if (pDrv->DynRegistToRoot(pRoot) && pDrv->DynStartWork())
  1898. {
  1899. //后插入DPC
  1900. DynAddDriver(pDrv);
  1901. ret = true;
  1902. }
  1903. if (ret == false)
  1904. {
  1905. UnloadDynDrvHandle(pDrv);
  1906. }
  1907. return ret;
  1908. }
  1909. bool DriverManager::DynDriverInitLoadAll()
  1910. {
  1911. Thread_Lock();
  1912. int x = 0;
  1913. //auto it = m_SubDriverList.begin();
  1914. //while (it != m_SubDriverList.end()) {
  1915. // (*it)->InitLoadDynDriver();
  1916. // it++;
  1917. // x++;
  1918. //}
  1919. //mLog::FINFO("Begin");
  1920. std::cout << "DriverManager::DynDriverInitLoadAll m_SubDriverList.size: " << m_SubDriverList.size() << std::endl;
  1921. for (; x < m_SubDriverList.size(); x++)
  1922. {
  1923. std::cout << "SubDriverList InitLoadDynDriver " << x << std::endl;
  1924. //mLog::FINFO("SubDriverList InitLoadDynDriver {$}",x );
  1925. m_SubDriverList[x]->InitLoadDynDriver();
  1926. //mLog::FINFO("SubDriverList InitLoadDynDriver {$} over.", x);
  1927. std::cout << "SubDriverList InitLoadDynDriver " << x << " over"<<std::endl;
  1928. }
  1929. //mLog::FINFO("End");
  1930. Thread_UnLock();
  1931. return true;
  1932. }
  1933. void DriverManager::DynAddDriver(DriverHandle *pDrv)
  1934. {
  1935. std::cout << "DriverManager::DynAddDriver IN" << std::endl;
  1936. Thread_Lock();
  1937. //vector<UINT64> IFList;
  1938. //pDrv->DynGetSysIFList(IFList);
  1939. //UpdateSysIF(IFList);
  1940. for (int x = 0; x < m_SubDriverList.size(); x++)
  1941. {
  1942. if (pDrv == m_SubDriverList[x])
  1943. return;
  1944. }
  1945. m_SubDriverList.push_back(pDrv);
  1946. std::cout << "DriverManager::DynAddDriver m_SubDriverList.push_back->size:" << m_SubDriverList.size() << std::endl;
  1947. Thread_UnLock();
  1948. }
  1949. void DriverManager::AddDriver(DriverHandle *pDrv)
  1950. {
  1951. std::cout << "DriverManager::AddDriver IN" << std::endl;
  1952. Thread_Lock();
  1953. //vector<UINT64> IFList;
  1954. //pDrv->GetSysIFList(IFList);
  1955. //UpdateSysIF(IFList);
  1956. for (int x = 0; x < m_SubDriverList.size(); x++)
  1957. {
  1958. if (pDrv == m_SubDriverList[x])
  1959. return;
  1960. }
  1961. m_SubDriverList.push_back(pDrv);
  1962. std::cout << "DriverManager::AddDriver m_SubDriverList.push_back->size:"<< m_SubDriverList.size() << std::endl;
  1963. Thread_UnLock();
  1964. }
  1965. bool DriverManager::GetThreadsLogger(DWORD Tid, PVOID &pLogger)
  1966. {
  1967. Thread_Lock();
  1968. for (size_t i = 0; i < m_SubDriverList.size(); i++)
  1969. {
  1970. if (m_SubDriverList[i]->GetThreadsLogger(Tid, pLogger))
  1971. {
  1972. Thread_UnLock();
  1973. return true;
  1974. }
  1975. }
  1976. if (m_pRootDrvHandle)
  1977. {
  1978. if (m_pRootDrvHandle->GetThreadsLogger(Tid, pLogger))
  1979. {
  1980. Thread_UnLock();
  1981. return true;
  1982. }
  1983. }
  1984. Thread_UnLock();
  1985. return false;
  1986. }
  1987. bool DriverManager::UpdateRootDrvStatus(int Status)
  1988. {
  1989. std::cout << "UpdateRootDrvStatus IN" << std::endl;
  1990. bool ret = true;
  1991. if (m_pRootDrvHandle)
  1992. {
  1993. BusUnitClient rootclient("DriverManager_UpdateRootDrvStatus");
  1994. if (rootclient.Open(getRootpath(), ACTION) == false)
  1995. {
  1996. std::cout << "Thread:" << GetCurrentThreadId() << ",Open Failed" << std::endl;
  1997. return false;
  1998. }
  1999. if (rootclient.SetDeviceStatus(Status) != RET_SUCCEED)
  2000. {
  2001. std::cout << "Thread:" << GetCurrentThreadId() << ",SetDeviceStatus Failed" << std::endl;
  2002. //printf("Thread:%d,SetDeviceStatus Failed\n", GetCurrentThreadId());
  2003. ret = false;
  2004. }
  2005. rootclient.Close();
  2006. std::cout << "Thread:" << GetCurrentThreadId() << ",SetDeviceStatus Succeed" << std::endl;
  2007. //printf("Thread:%d,SetDeviceStatus Succeed\n", GetCurrentThreadId());
  2008. }
  2009. return ret;
  2010. }
  2011. void DriverManager::AddDevice(const char* devUri, LogicDevice* pDevice)
  2012. {
  2013. std::cout << "Add Device " << devUri<<" with "<< pDevice->GetCcosRootPath()<< endl;
  2014. //mLog::FINFO("Add Device {$} with {$}", devUri, pDevice->GetCcosRootPath());
  2015. m_CcosDeviceMap[devUri] = pDevice;
  2016. }
  2017. const char* DriverManager::GetAllDevice(void** pDevices, int& nDevNums)
  2018. {
  2019. m_strAllDeviceUri = "";
  2020. auto it = m_CcosDeviceMap.begin();
  2021. nDevNums = 0;
  2022. if (pDevices == nullptr) {
  2023. return m_strAllDeviceUri.c_str();
  2024. }
  2025. while (it != m_CcosDeviceMap.end())
  2026. {
  2027. if (nDevNums >= 30) {
  2028. std::cout << "Warning: Device count exceeds max limit (" << 30 << ")" << std::endl;
  2029. break;
  2030. }
  2031. m_strAllDeviceUri += it->first + ";";
  2032. pDevices[nDevNums++] = it->second;
  2033. it++;
  2034. }
  2035. return m_strAllDeviceUri.c_str();
  2036. }
  2037. bool DriverManager::LoadRootDrv()
  2038. {
  2039. //get busid
  2040. string busId = (const char*)getLocalEbusId();
  2041. if (busId.size() == 0)
  2042. {
  2043. //some log here
  2044. return false;
  2045. }
  2046. //load drv
  2047. DriverHandle *pHandle = new DriverHandle();
  2048. pHandle->m_AsRootDriver = true;//need to set this
  2049. string path = GetProcessDirectory() + "/" + "rootdrv.xml";
  2050. if (pHandle->LoadDriver(path.c_str(), false))
  2051. {
  2052. //load succeed
  2053. //printf("Load %s succeed\n", path.c_str());
  2054. //Load only device,not include driver .!!!!!!
  2055. //hook it to tree
  2056. map<string, UINT64>::iterator iter = pHandle->m_DeviceFullPathMap.begin();
  2057. while (iter != pHandle->m_DeviceFullPathMap.end())
  2058. {
  2059. string path = iter->first;
  2060. UINT64 procId = (UINT64)getpid();
  2061. UINT64 addr = iter->second;
  2062. if (path == string(getRootpath()))
  2063. {
  2064. //make tree node
  2065. if (g_DrvTree.AddTree(path.c_str(), procId, addr))
  2066. {
  2067. //done
  2068. //save it
  2069. m_pRootDrvHandle = (pHandle);
  2070. //
  2071. LogicDriver *driver = m_pRootDrvHandle->GetDpcDriver();
  2072. driver->SetTreeRoot(true);
  2073. driver->SetClientRootID(path.c_str(), path.c_str()+1);
  2074. std::cout << "driver SetClientRootID Real CCOS path ["
  2075. << path << "] from ["
  2076. << driver->GetCcosRootPath() << "]" << std::endl;
  2077. //mLog::FINFO("driver SetClientRootID Real CCOS path [{$}] ", path, driver->GetCcosRootPath());
  2078. //driver->SubscribeSelf(); //这个是OemBusUnit对象,可以支持Open
  2079. //start it
  2080. {
  2081. //这里有个瑕疵:
  2082. //Driver带着一个Device加载,但是Driver意义不大,故在AddTree中过滤掉.
  2083. //但是SysIF中是带着Driver的地址的....
  2084. //vector<UINT64> ifList;
  2085. //m_pRootDrvHandle->GetSysIFList(ifList);
  2086. //UpdateSysIF(ifList);
  2087. return m_pRootDrvHandle->StartWork();
  2088. }
  2089. }
  2090. }
  2091. ++iter;
  2092. }
  2093. }
  2094. //printf("Load %s failed\n", path.c_str());
  2095. delete pHandle;
  2096. return false;
  2097. }
  2098. bool DriverManager::LoadDrv(ResDataObject &ConfigContext)
  2099. {
  2100. //NOT FINISHED YET
  2101. assert(0);
  2102. return false;
  2103. }
  2104. bool DriverManager::LoadDriver(const char *pConfigFilepath)
  2105. {
  2106. bool ret = false;
  2107. DriverHandle *pHandle = new DriverHandle();
  2108. string path = pConfigFilepath;
  2109. std::cout << "------------ CDI: DriverManager::LoadDriver" << pConfigFilepath << std::endl;
  2110. if (pHandle->LoadDriver(path.c_str(), true))
  2111. {
  2112. //load succeed
  2113. const char *pRoot = getRootpath();
  2114. //save it
  2115. AddDriver(pHandle);//这点比较闹心,已经注册到系统,所以...
  2116. LogicDriver *pRootDriver = m_pRootDrvHandle->GetDpcDriver();
  2117. LogicDriver* pClientDriver = pHandle->GetDpcDriver();
  2118. pRootDriver->AddEbusChildren(pClientDriver, pClientDriver->GetRootPath().c_str());
  2119. //pHandle->get
  2120. //start it
  2121. ret = pHandle->StartWork();
  2122. if (ret)
  2123. {
  2124. //regist to tree
  2125. ret = pHandle->RegistToRoot(pRoot);
  2126. }
  2127. if (ret == false)
  2128. {
  2129. //所以得先卸载from系统,然后再清除...
  2130. UnloadDrvHandle(pHandle);
  2131. }
  2132. }
  2133. else
  2134. {
  2135. delete pHandle;
  2136. }
  2137. return ret;
  2138. }
  2139. bool DriverManager::UnloadDynDrvHandle(DriverHandle *pHandle)
  2140. {
  2141. bool ret = false;
  2142. if (pHandle)
  2143. {
  2144. Thread_Lock();
  2145. {
  2146. //kick the SysIF from the map
  2147. //vector<UINT64> ifList;
  2148. //pHandle->DynGetSysIFList(ifList);
  2149. //UpdateSysIF(ifList, true);
  2150. pHandle->UnloadDynDriver();
  2151. ret = true;
  2152. }
  2153. Thread_UnLock();
  2154. }
  2155. return ret;
  2156. }
  2157. bool DriverManager::UnloadDrvHandle(DriverHandle *pHandle)
  2158. {
  2159. bool ret = false;
  2160. if (pHandle)
  2161. {
  2162. Thread_Lock();
  2163. vector<DriverHandle*>::iterator iter = find(m_SubDriverList.begin(), m_SubDriverList.end(), pHandle);
  2164. if (iter != m_SubDriverList.end())
  2165. {
  2166. //do unload work
  2167. //kick it from list
  2168. m_SubDriverList.erase(iter);
  2169. //kick the SysIF from the map
  2170. //vector<UINT64> ifList;
  2171. //pHandle->GetSysIFList(ifList);
  2172. //UpdateSysIF(ifList, true);
  2173. ret = true;
  2174. }
  2175. Thread_UnLock();
  2176. //kick the logicdevice from root
  2177. delete pHandle;
  2178. //if (pHandle->UnloadDriver() == false)
  2179. //{
  2180. // //put some log here
  2181. //}
  2182. }
  2183. return ret;
  2184. }
  2185. //return next try tick
  2186. bool DriverHandle::InitLoadDynDriver()
  2187. {
  2188. bool ret = true;
  2189. if (!m_bCommonDPC)
  2190. return true;
  2191. LogicDevice* pDevice = m_pDPC;
  2192. if (pDevice)
  2193. {
  2194. LogicDriver* pDriver = (LogicDriver*)pDevice;
  2195. //check connection
  2196. //if (pDriver->GetConnectionStatus())
  2197. //{
  2198. // //ok
  2199. // return false;
  2200. //}
  2201. //makesure it's disconnected
  2202. //if (m_pDrvHandle)
  2203. {
  2204. ////mLog::FINFO("Try UnloadDynDrv ");
  2205. //UnloadDynDriver();
  2206. //if (g_DrvManager.UnloadDynDrvHandle(this))
  2207. {
  2208. //do the connection
  2209. //mLog::FINFO("Try Connect ");
  2210. //if (pDriver->Connect())
  2211. bool bret = pDriver->Connect();
  2212. //mLog::FINFO("Try LoadDynDriver with Connect Result ={$} ", bret);
  2213. //if(bret)
  2214. {
  2215. //do load logicdevices
  2216. if (LoadDynDriver())
  2217. {
  2218. //mLog::FINFO("Try DrvManager::DynLoadDriver ");
  2219. if (g_DrvManager.DynLoadDriver(this))
  2220. {
  2221. //ok
  2222. //mLog::FINFO("Try AutomaticConnect Succeed");
  2223. return true;
  2224. }
  2225. //mLog::FERROR("Try DrvManager::DynLoadDriver Failed");
  2226. }
  2227. else
  2228. {
  2229. UnloadDynDriver();
  2230. }
  2231. //report error here
  2232. //mLog::FERROR("Try LoadDynDriver Failed");
  2233. pDriver->DisConnect();
  2234. return false;
  2235. }
  2236. }
  2237. }
  2238. }
  2239. return false;
  2240. }