DriverManager.cpp 64 KB

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