DriverManager.cpp 63 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788
  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. RET_STATUS ret = (RET_STATUS)rootclient.AddDeviceDescrpt(path.c_str(), TargetType.c_str(),(const char*)getLocalMachineId(), procId, addr);
  1492. if (ret == RET_FAILED)
  1493. {
  1494. FERROR("AddDeviceDescrpt [{$}] Failed [{$}]", RootPath, path);
  1495. //log here
  1496. //try updadate this driver handle
  1497. //assert(0);//NOT FINISHED YET
  1498. //unhook it
  1499. map<string, UINT64>::iterator iterBk = m_DeviceFullPathMap.begin();
  1500. while (iterBk != iter)
  1501. {
  1502. if (rootclient.DelDeviceDescrpt(iterBk->first.c_str()) != RET_SUCCEED)
  1503. {
  1504. //log here
  1505. //shit on going!!!!
  1506. rootclient.Close();
  1507. return false;
  1508. }
  1509. ++iterBk;
  1510. }
  1511. rootclient.Close();
  1512. return false;
  1513. }
  1514. else if (ret == RET_INVALID)
  1515. {
  1516. return false;
  1517. }
  1518. ++iter;
  1519. }
  1520. // Ccos PATH
  1521. iter = m_CcosDeviceFullPathMap.begin();
  1522. while (iter != m_CcosDeviceFullPathMap.end())
  1523. {
  1524. string path = iter->first;
  1525. UINT64 procId = (UINT64)getpid();
  1526. UINT64 addr = iter->second;
  1527. string TargetType = "Device";
  1528. if (iter->first == m_DriverFullPathMap)
  1529. {
  1530. TargetType = "Driver";
  1531. }
  1532. //regist driver
  1533. RET_STATUS ret = (RET_STATUS)rootclient.AddDeviceDescrpt(path.c_str(), TargetType.c_str(), (const char*)getLocalMachineId(), procId, addr);
  1534. if (ret == RET_FAILED)
  1535. {
  1536. //log here
  1537. //try updadate this driver handle
  1538. //assert(0);//NOT FINISHED YET
  1539. FERROR("AddDeviceDescrpt [{$}] Failed [{$}]", RootPath, path);
  1540. //unhook it
  1541. map<string, UINT64>::iterator iterBk = m_CcosDeviceFullPathMap.begin();
  1542. while (iterBk != iter)
  1543. {
  1544. if (rootclient.DelDeviceDescrpt(iterBk->first.c_str()) != RET_SUCCEED)
  1545. {
  1546. //log here
  1547. //shit on going!!!!
  1548. rootclient.Close();
  1549. return false;
  1550. }
  1551. ++iterBk;
  1552. }
  1553. rootclient.Close();
  1554. return false;
  1555. }
  1556. else if (ret == RET_INVALID)
  1557. {
  1558. return false;
  1559. }
  1560. ++iter;
  1561. }
  1562. }
  1563. rootclient.Close();
  1564. RegistToDrvObject(false,true);//后执行,保证Regist成功
  1565. FINFO("End of Success. [{$}]", RootPath);
  1566. return true;
  1567. }
  1568. void DriverHandle::RegistToDrvObject(bool dyn, bool regist)
  1569. {
  1570. map<string, UINT64>::iterator iter;
  1571. if (dyn)
  1572. {
  1573. iter = m_DynDeviceFullPathMap.begin();
  1574. while (iter != m_DynDeviceFullPathMap.end())
  1575. {
  1576. string path = iter->first;
  1577. //regist driver
  1578. if (regist)
  1579. {
  1580. ((LogicDriver*)m_pDPC)->AddDevice(path.c_str());
  1581. }
  1582. else
  1583. {
  1584. ((LogicDriver*)m_pDPC)->DelDevice(path.c_str());
  1585. }
  1586. ++iter;
  1587. }
  1588. }
  1589. else
  1590. {
  1591. /*
  1592. if (m_pParent)
  1593. {
  1594. //mdpc parent exist,this is SubDPC object
  1595. string path = m_DriverFullPathMap;
  1596. //regist DPC2MDPC
  1597. if (regist)
  1598. {
  1599. ((LogicDriver*)m_pParent)->AddDevice(path.c_str());
  1600. }
  1601. else
  1602. {
  1603. ((LogicDriver*)m_pParent)->DelDevice(path.c_str());
  1604. }
  1605. }*/
  1606. //go normal
  1607. iter = m_DeviceFullPathMap.begin();
  1608. while (iter != m_DeviceFullPathMap.end())
  1609. {
  1610. string path = iter->first;
  1611. if (m_DriverFullPathMap == path)
  1612. {
  1613. //exept it's own driver
  1614. ++iter;
  1615. continue;
  1616. }
  1617. //regist driver
  1618. if (regist)
  1619. {
  1620. ((LogicDriver*)m_pDPC)->AddDevice(path.c_str());
  1621. }
  1622. else
  1623. {
  1624. ((LogicDriver*)m_pDPC)->DelDevice(path.c_str());
  1625. }
  1626. ++iter;
  1627. }
  1628. }
  1629. }
  1630. bool DriverHandle::DynRegistToRoot(const char* RootPath, DWORD timeout)
  1631. {
  1632. BusUnitClient rootclient("DriverHandle_dyn_reg");
  1633. //me second
  1634. if (rootclient.Open(RootPath, ACTION, timeout) == false)
  1635. {
  1636. std::cout << "Thread:" << GetCurrentThreadId() << ", Open RootPath failed. Path: " << RootPath << ", Timeout: " << timeout << std::endl;
  1637. return false;
  1638. }
  1639. if (rootclient.GetExitFlag() == 0)
  1640. {
  1641. map<string, UINT64>::iterator iter = m_DynDeviceFullPathMap.begin();
  1642. while (iter != m_DynDeviceFullPathMap.end())
  1643. {
  1644. string TargetType = "Device";
  1645. string path = iter->first;
  1646. UINT64 procId = (UINT64)getpid();
  1647. UINT64 addr = iter->second;
  1648. //regist driver
  1649. if (rootclient.AddDeviceDescrpt(path.c_str(), TargetType.c_str(),(const char*)getLocalMachineId(), procId, addr) != RET_SUCCEED)
  1650. {
  1651. //log here
  1652. //unhook it
  1653. std::cout << "Thread:" << GetCurrentThreadId() << ", Starting rollback for m_DynDeviceFullPathMap. Current path: " << path << std::endl;
  1654. map<string, UINT64>::iterator iterBk = m_DynDeviceFullPathMap.begin();
  1655. while (iterBk != iter)
  1656. {
  1657. if (rootclient.DelDeviceDescrpt(iterBk->first.c_str()) != RET_SUCCEED)
  1658. {
  1659. //log here
  1660. //shit on going!!!!
  1661. std::cout << "Thread:" << GetCurrentThreadId() << ", Failed to rollback device. Path: " << iterBk->first << ". Critical error!" << std::endl;
  1662. rootclient.Close();
  1663. return false;
  1664. }
  1665. ++iterBk;
  1666. }
  1667. rootclient.Close();
  1668. return false;
  1669. }
  1670. ++iter;
  1671. }
  1672. iter = m_CcosDynDeviceFullPathMap.begin();
  1673. while (iter != m_CcosDynDeviceFullPathMap.end())
  1674. {
  1675. string TargetType = "Device";
  1676. string path = iter->first;
  1677. UINT64 procId = (UINT64)getpid();
  1678. UINT64 addr = iter->second;
  1679. //regist driver
  1680. if (rootclient.AddDeviceDescrpt(path.c_str(), TargetType.c_str(), (const char*)getLocalMachineId(), procId, addr) != RET_SUCCEED)
  1681. {
  1682. std::cout << "Thread:" << GetCurrentThreadId() << ", Failed to register Ccos device. Path: " << path << std::endl;
  1683. //log here
  1684. //unhook it
  1685. map<string, UINT64>::iterator iterBk = m_CcosDynDeviceFullPathMap.begin();
  1686. while (iterBk != iter)
  1687. {
  1688. if (rootclient.DelDeviceDescrpt(iterBk->first.c_str()) != RET_SUCCEED)
  1689. {
  1690. //log here
  1691. //shit on going!!!!
  1692. rootclient.Close();
  1693. return false;
  1694. }
  1695. ++iterBk;
  1696. }
  1697. rootclient.Close();
  1698. return false;
  1699. }
  1700. ++iter;
  1701. }
  1702. }
  1703. rootclient.Close();
  1704. RegistToDrvObject();//后执行,保证Regist成功
  1705. return true;
  1706. }
  1707. bool DriverHandle::DynUnRegistFromRoot()
  1708. {
  1709. DWORD timeout = 0;
  1710. string rootpath;
  1711. BusUnitClient rootclient("DriverHandle_UnReg");
  1712. RegistToDrvObject(true,false);//先执行,不管unregist成功与否
  1713. if (m_AsRootDriver == false)
  1714. {
  1715. rootpath = (const char*)getRootpath();
  1716. timeout = 2000;
  1717. }
  1718. else
  1719. {
  1720. //rootpath = (const char*)getChannelRootpath();
  1721. return true;//DriverManager中已经卸载了Root在Channel中的东西
  1722. }
  1723. if (m_AsRootDriver)
  1724. {
  1725. timeout = 2000;
  1726. }
  1727. else
  1728. {
  1729. timeout = INFINITE;
  1730. }
  1731. if (rootclient.Open(rootpath.c_str(), ACTION, timeout) == false)
  1732. {
  1733. //printf("Thread:%d,Open %s Failed\n", GetCurrentThreadId(), rootpath.c_str());
  1734. return false;
  1735. }
  1736. map<string, UINT64>::iterator iter = m_DynDeviceFullPathMap.begin();
  1737. while (iter != m_DynDeviceFullPathMap.end())
  1738. {
  1739. string path = iter->first;
  1740. //regist driver
  1741. if (rootclient.DelDeviceDescrpt(path.c_str()) != RET_SUCCEED)
  1742. {
  1743. //log here
  1744. rootclient.Close();
  1745. return false;
  1746. }
  1747. ++iter;
  1748. }
  1749. iter = m_CcosDynDeviceFullPathMap.begin();
  1750. while (iter != m_CcosDynDeviceFullPathMap.end())
  1751. {
  1752. string path = iter->first;
  1753. //regist driver
  1754. if (rootclient.DelDeviceDescrpt(path.c_str()) != RET_SUCCEED)
  1755. {
  1756. //log here
  1757. rootclient.Close();
  1758. return false;
  1759. }
  1760. ++iter;
  1761. }
  1762. rootclient.Close();
  1763. return true;
  1764. }
  1765. bool DriverHandle::UnRegistFromRoot()
  1766. {
  1767. DWORD timeout = 0;
  1768. string rootpath;
  1769. BusUnitClient rootclient("DriverHandle_unregroot");
  1770. RegistToDrvObject(false, false);//先执行,不管unregist成功与否
  1771. if (m_AsRootDriver == false)
  1772. {
  1773. rootpath = (const char*)getRootpath();
  1774. timeout = 2000;
  1775. }
  1776. else
  1777. {
  1778. rootpath = (const char*)getChannelRootpath();
  1779. }
  1780. if (m_AsRootDriver)
  1781. {
  1782. timeout = 2000;
  1783. }
  1784. else
  1785. {
  1786. timeout = INFINITE;
  1787. }
  1788. if (rootclient.Open(rootpath.c_str(), ACTION, timeout) == false)
  1789. {
  1790. //printf("Thread:%d,Open %s Failed\n", GetCurrentThreadId(),rootpath.c_str());
  1791. return false;
  1792. }
  1793. map<string, UINT64>::iterator iter = m_DeviceFullPathMap.begin();
  1794. while (iter != m_DeviceFullPathMap.end())
  1795. {
  1796. string path = iter->first;
  1797. //regist driver
  1798. if (rootclient.DelDeviceDescrpt(path.c_str()) != RET_SUCCEED)
  1799. {
  1800. //log here
  1801. rootclient.Close();
  1802. return false;
  1803. }
  1804. ++iter;
  1805. }
  1806. rootclient.Close();
  1807. return true;
  1808. }
  1809. void DriverHandle::DynGetSysIFList(vector<UINT64> &ifList)
  1810. {
  1811. }
  1812. void DriverHandle::GetSysIFList(vector<UINT64> &ifList)
  1813. {
  1814. }
  1815. //------------drv manager---------------------------
  1816. DriverManager::DriverManager()
  1817. {
  1818. m_pRootDrvHandle = NULL;
  1819. }
  1820. DriverManager::~DriverManager()
  1821. {
  1822. if (m_pRootDrvHandle == NULL)
  1823. return;
  1824. UnloadDrivers();
  1825. UnloadRootDriver();
  1826. }
  1827. bool DriverManager::UnRegistFromChannel(DWORD timeout)
  1828. {
  1829. string RootPath;
  1830. string ChannelPath;
  1831. BusUnitClient rootclient("DriverManager_unreg");
  1832. RootPath = (const char *)getRootpath();
  1833. ChannelPath = (const char*)getChannelRootpath();
  1834. if (RootPath == ChannelPath)
  1835. {
  1836. return false;
  1837. }
  1838. if (rootclient.Open(ChannelPath.c_str(), ACTION, timeout) == false)
  1839. {
  1840. //printf("Thread:%d,Open %s Failed\n", GetCurrentThreadId(), ChannelPath.c_str());
  1841. return false;
  1842. }
  1843. if (rootclient.DelDeviceDescrpt(RootPath.c_str()) != RET_SUCCEED)
  1844. {
  1845. //log here
  1846. rootclient.Close();
  1847. return false;
  1848. }
  1849. rootclient.Close();
  1850. return true;
  1851. }
  1852. void DriverManager::UnloadRootDriver()
  1853. {
  1854. UnRegistFromChannel(500);
  1855. FINFO("UnRegistFromChannel Fnished");
  1856. Thread_Lock();
  1857. if (m_pRootDrvHandle)
  1858. {
  1859. FINFO("Unload Root Driver Begin");
  1860. //vector<UINT64> ifList;
  1861. //m_pRootDrvHandle->GetSysIFList(ifList);
  1862. //UpdateSysIF(ifList,true);
  1863. delete m_pRootDrvHandle;
  1864. m_pRootDrvHandle = NULL;
  1865. FINFO("Unload Root Driver Done");
  1866. }
  1867. Thread_UnLock();
  1868. }
  1869. void DriverManager::UnloadDrivers()
  1870. {
  1871. while (1)
  1872. {
  1873. DriverHandle* pHandle = NULL;
  1874. Thread_Lock();
  1875. vector<DriverHandle*>::iterator iter = m_SubDriverList.begin();
  1876. if (iter != m_SubDriverList.end())
  1877. {
  1878. pHandle = (*iter);
  1879. iter = m_SubDriverList.erase(iter);
  1880. break;
  1881. }
  1882. Thread_UnLock();
  1883. if (pHandle == NULL)
  1884. {
  1885. break;
  1886. }
  1887. //unload one
  1888. UnloadDynDrvHandle(pHandle);
  1889. UnloadDrvHandle(pHandle);
  1890. }
  1891. }
  1892. bool DriverManager::DynLoadDriver(DriverHandle *pDrv)
  1893. {
  1894. std::cout << "DriverManager::DynLoadDriver IN" << std::endl;
  1895. bool ret = false;
  1896. const char *pRoot = getRootpath();
  1897. //新流程如下.
  1898. //先注册
  1899. if (pDrv->DynRegistToRoot(pRoot) && pDrv->DynStartWork())
  1900. {
  1901. //后插入DPC
  1902. DynAddDriver(pDrv);
  1903. ret = true;
  1904. }
  1905. if (ret == false)
  1906. {
  1907. UnloadDynDrvHandle(pDrv);
  1908. }
  1909. return ret;
  1910. }
  1911. bool DriverManager::DynDriverInitLoadAll()
  1912. {
  1913. Thread_Lock();
  1914. int x = 0;
  1915. //auto it = m_SubDriverList.begin();
  1916. //while (it != m_SubDriverList.end()) {
  1917. // (*it)->InitLoadDynDriver();
  1918. // it++;
  1919. // x++;
  1920. //}
  1921. FINFO("Begin");
  1922. std::cout << "DriverManager::DynDriverInitLoadAll m_SubDriverList.size: " << m_SubDriverList.size() << std::endl;
  1923. for (; x < m_SubDriverList.size(); x++)
  1924. {
  1925. std::cout << "SubDriverList InitLoadDynDriver " << x << std::endl;
  1926. FINFO("SubDriverList InitLoadDynDriver {$}",x );
  1927. m_SubDriverList[x]->InitLoadDynDriver();
  1928. FINFO("SubDriverList InitLoadDynDriver {$} over.", x);
  1929. std::cout << "SubDriverList InitLoadDynDriver " << x << " over"<<std::endl;
  1930. }
  1931. FINFO("End");
  1932. Thread_UnLock();
  1933. return true;
  1934. }
  1935. void DriverManager::DynAddDriver(DriverHandle *pDrv)
  1936. {
  1937. std::cout << "DriverManager::DynAddDriver IN" << std::endl;
  1938. Thread_Lock();
  1939. //vector<UINT64> IFList;
  1940. //pDrv->DynGetSysIFList(IFList);
  1941. //UpdateSysIF(IFList);
  1942. for (int x = 0; x < m_SubDriverList.size(); x++)
  1943. {
  1944. if (pDrv == m_SubDriverList[x])
  1945. return;
  1946. }
  1947. m_SubDriverList.push_back(pDrv);
  1948. std::cout << "DriverManager::DynAddDriver m_SubDriverList.push_back->size:" << m_SubDriverList.size() << std::endl;
  1949. Thread_UnLock();
  1950. }
  1951. void DriverManager::AddDriver(DriverHandle *pDrv)
  1952. {
  1953. std::cout << "DriverManager::AddDriver IN" << std::endl;
  1954. Thread_Lock();
  1955. //vector<UINT64> IFList;
  1956. //pDrv->GetSysIFList(IFList);
  1957. //UpdateSysIF(IFList);
  1958. for (int x = 0; x < m_SubDriverList.size(); x++)
  1959. {
  1960. if (pDrv == m_SubDriverList[x])
  1961. return;
  1962. }
  1963. m_SubDriverList.push_back(pDrv);
  1964. std::cout << "DriverManager::AddDriver m_SubDriverList.push_back->size:"<< m_SubDriverList.size() << std::endl;
  1965. Thread_UnLock();
  1966. }
  1967. bool DriverManager::GetThreadsLogger(DWORD Tid, PVOID &pLogger)
  1968. {
  1969. Thread_Lock();
  1970. for (size_t i = 0; i < m_SubDriverList.size(); i++)
  1971. {
  1972. if (m_SubDriverList[i]->GetThreadsLogger(Tid, pLogger))
  1973. {
  1974. Thread_UnLock();
  1975. return true;
  1976. }
  1977. }
  1978. if (m_pRootDrvHandle)
  1979. {
  1980. if (m_pRootDrvHandle->GetThreadsLogger(Tid, pLogger))
  1981. {
  1982. Thread_UnLock();
  1983. return true;
  1984. }
  1985. }
  1986. Thread_UnLock();
  1987. return false;
  1988. }
  1989. bool DriverManager::UpdateRootDrvStatus(int Status)
  1990. {
  1991. std::cout << "UpdateRootDrvStatus IN" << std::endl;
  1992. bool ret = true;
  1993. if (m_pRootDrvHandle)
  1994. {
  1995. BusUnitClient rootclient("DriverManager_UpdateRootDrvStatus");
  1996. if (rootclient.Open(getRootpath(), ACTION) == false)
  1997. {
  1998. std::cout << "Thread:" << GetCurrentThreadId() << ",Open Failed" << std::endl;
  1999. return false;
  2000. }
  2001. if (rootclient.SetDeviceStatus(Status) != RET_SUCCEED)
  2002. {
  2003. std::cout << "Thread:" << GetCurrentThreadId() << ",SetDeviceStatus Failed" << std::endl;
  2004. //printf("Thread:%d,SetDeviceStatus Failed\n", GetCurrentThreadId());
  2005. ret = false;
  2006. }
  2007. rootclient.Close();
  2008. std::cout << "Thread:" << GetCurrentThreadId() << ",SetDeviceStatus Succeed" << std::endl;
  2009. //printf("Thread:%d,SetDeviceStatus Succeed\n", GetCurrentThreadId());
  2010. }
  2011. return ret;
  2012. }
  2013. void DriverManager::AddDevice(const char* devUri, LogicDevice* pDevice)
  2014. {
  2015. std::cout << "Add Device " << devUri<<" with "<< pDevice->GetCcosRootPath()<< endl;
  2016. FINFO("Add Device {$} with {$}", devUri, pDevice->GetCcosRootPath());
  2017. m_CcosDeviceMap[devUri] = pDevice;
  2018. }
  2019. const char* DriverManager::GetAllDevice(void** pDevices, int& nDevNums)
  2020. {
  2021. m_strAllDeviceUri = "";
  2022. auto it = m_CcosDeviceMap.begin();
  2023. nDevNums = 0;
  2024. if (pDevices == nullptr) {
  2025. return m_strAllDeviceUri.c_str();
  2026. }
  2027. while (it != m_CcosDeviceMap.end())
  2028. {
  2029. if (nDevNums >= 30) {
  2030. std::cout << "Warning: Device count exceeds max limit (" << 30 << ")" << std::endl;
  2031. break;
  2032. }
  2033. m_strAllDeviceUri += it->first + ";";
  2034. pDevices[nDevNums++] = it->second;
  2035. it++;
  2036. }
  2037. return m_strAllDeviceUri.c_str();
  2038. }
  2039. bool DriverManager::LoadRootDrv()
  2040. {
  2041. //get busid
  2042. string busId = (const char*)getLocalEbusId();
  2043. if (busId.size() == 0)
  2044. {
  2045. //some log here
  2046. return false;
  2047. }
  2048. //load drv
  2049. DriverHandle *pHandle = new DriverHandle();
  2050. pHandle->m_AsRootDriver = true;//need to set this
  2051. string path = GetProcessDirectory() + "/" + "rootdrv.xml";
  2052. if (pHandle->LoadDriver(path.c_str(), false))
  2053. {
  2054. //load succeed
  2055. //printf("Load %s succeed\n", path.c_str());
  2056. //Load only device,not include driver .!!!!!!
  2057. //hook it to tree
  2058. map<string, UINT64>::iterator iter = pHandle->m_DeviceFullPathMap.begin();
  2059. while (iter != pHandle->m_DeviceFullPathMap.end())
  2060. {
  2061. string path = iter->first;
  2062. UINT64 procId = (UINT64)getpid();
  2063. UINT64 addr = iter->second;
  2064. if (path == string(getRootpath()))
  2065. {
  2066. //make tree node
  2067. if (g_DrvTree.AddTree(path.c_str(), procId, addr))
  2068. {
  2069. //done
  2070. //save it
  2071. m_pRootDrvHandle = (pHandle);
  2072. //
  2073. LogicDriver *driver = m_pRootDrvHandle->GetDpcDriver();
  2074. driver->SetTreeRoot(true);
  2075. driver->SetClientRootID(path.c_str(), path.c_str()+1);
  2076. std::cout << "driver SetClientRootID Real CCOS path ["
  2077. << path << "] from ["
  2078. << driver->GetCcosRootPath() << "]" << std::endl;
  2079. FINFO("driver SetClientRootID Real CCOS path [{$}] ", path, driver->GetCcosRootPath());
  2080. //driver->SubscribeSelf(); //这个是OemBusUnit对象,可以支持Open
  2081. //start it
  2082. {
  2083. //这里有个瑕疵:
  2084. //Driver带着一个Device加载,但是Driver意义不大,故在AddTree中过滤掉.
  2085. //但是SysIF中是带着Driver的地址的....
  2086. //vector<UINT64> ifList;
  2087. //m_pRootDrvHandle->GetSysIFList(ifList);
  2088. //UpdateSysIF(ifList);
  2089. return m_pRootDrvHandle->StartWork();
  2090. }
  2091. }
  2092. }
  2093. ++iter;
  2094. }
  2095. }
  2096. //printf("Load %s failed\n", path.c_str());
  2097. delete pHandle;
  2098. return false;
  2099. }
  2100. bool DriverManager::LoadDrv(ResDataObject &ConfigContext)
  2101. {
  2102. //NOT FINISHED YET
  2103. assert(0);
  2104. return false;
  2105. }
  2106. bool DriverManager::LoadDriver(const char *pConfigFilepath)
  2107. {
  2108. bool ret = false;
  2109. DriverHandle *pHandle = new DriverHandle();
  2110. string path = pConfigFilepath;
  2111. std::cout << "------------ CDI: DriverManager::LoadDriver" << pConfigFilepath << std::endl;
  2112. if (pHandle->LoadDriver(path.c_str(), true))
  2113. {
  2114. //load succeed
  2115. const char *pRoot = getRootpath();
  2116. //save it
  2117. AddDriver(pHandle);//这点比较闹心,已经注册到系统,所以...
  2118. LogicDriver *pRootDriver = m_pRootDrvHandle->GetDpcDriver();
  2119. LogicDriver* pClientDriver = pHandle->GetDpcDriver();
  2120. pRootDriver->AddEbusChildren(pClientDriver, pClientDriver->GetRootPath().c_str());
  2121. //pHandle->get
  2122. //start it
  2123. ret = pHandle->StartWork();
  2124. if (ret)
  2125. {
  2126. //regist to tree
  2127. ret = pHandle->RegistToRoot(pRoot);
  2128. }
  2129. if (ret == false)
  2130. {
  2131. //所以得先卸载from系统,然后再清除...
  2132. UnloadDrvHandle(pHandle);
  2133. }
  2134. }
  2135. else
  2136. {
  2137. delete pHandle;
  2138. }
  2139. return ret;
  2140. }
  2141. bool DriverManager::UnloadDynDrvHandle(DriverHandle *pHandle)
  2142. {
  2143. bool ret = false;
  2144. if (pHandle)
  2145. {
  2146. Thread_Lock();
  2147. {
  2148. //kick the SysIF from the map
  2149. //vector<UINT64> ifList;
  2150. //pHandle->DynGetSysIFList(ifList);
  2151. //UpdateSysIF(ifList, true);
  2152. pHandle->UnloadDynDriver();
  2153. ret = true;
  2154. }
  2155. Thread_UnLock();
  2156. }
  2157. return ret;
  2158. }
  2159. bool DriverManager::UnloadDrvHandle(DriverHandle *pHandle)
  2160. {
  2161. bool ret = false;
  2162. if (pHandle)
  2163. {
  2164. Thread_Lock();
  2165. vector<DriverHandle*>::iterator iter = find(m_SubDriverList.begin(), m_SubDriverList.end(), pHandle);
  2166. if (iter != m_SubDriverList.end())
  2167. {
  2168. //do unload work
  2169. //kick it from list
  2170. m_SubDriverList.erase(iter);
  2171. //kick the SysIF from the map
  2172. //vector<UINT64> ifList;
  2173. //pHandle->GetSysIFList(ifList);
  2174. //UpdateSysIF(ifList, true);
  2175. ret = true;
  2176. }
  2177. Thread_UnLock();
  2178. //kick the logicdevice from root
  2179. delete pHandle;
  2180. //if (pHandle->UnloadDriver() == false)
  2181. //{
  2182. // //put some log here
  2183. //}
  2184. }
  2185. return ret;
  2186. }
  2187. //return next try tick
  2188. bool DriverHandle::InitLoadDynDriver()
  2189. {
  2190. bool ret = true;
  2191. if (!m_bCommonDPC)
  2192. return true;
  2193. LogicDevice* pDevice = m_pDPC;
  2194. if (pDevice)
  2195. {
  2196. LogicDriver* pDriver = (LogicDriver*)pDevice;
  2197. //check connection
  2198. //if (pDriver->GetConnectionStatus())
  2199. //{
  2200. // //ok
  2201. // return false;
  2202. //}
  2203. //makesure it's disconnected
  2204. //if (m_pDrvHandle)
  2205. {
  2206. //FINFO("Try UnloadDynDrv ");
  2207. //UnloadDynDriver();
  2208. //if (g_DrvManager.UnloadDynDrvHandle(this))
  2209. {
  2210. //do the connection
  2211. FINFO("Try Connect ");
  2212. //if (pDriver->Connect())
  2213. bool bret = pDriver->Connect();
  2214. FINFO("Try LoadDynDriver with Connect Result ={$} ", bret);
  2215. //if(bret)
  2216. {
  2217. //do load logicdevices
  2218. if (LoadDynDriver())
  2219. {
  2220. FINFO("Try DrvManager::DynLoadDriver ");
  2221. if (g_DrvManager.DynLoadDriver(this))
  2222. {
  2223. //ok
  2224. FINFO("Try AutomaticConnect Succeed");
  2225. return true;
  2226. }
  2227. FERROR("Try DrvManager::DynLoadDriver Failed");
  2228. }
  2229. else
  2230. {
  2231. UnloadDynDriver();
  2232. }
  2233. //report error here
  2234. FERROR("Try LoadDynDriver Failed");
  2235. pDriver->DisConnect();
  2236. return false;
  2237. }
  2238. }
  2239. }
  2240. }
  2241. return false;
  2242. }