DriverManager.cpp 58 KB

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