DriverManager.cpp 64 KB

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