BusUnitLogic.cpp 42 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751
  1. // BusUnitLogic.cpp : 定义 DLL 应用程序的导出函数。
  2. //
  3. #include <time.h>
  4. #include "common_api.h"
  5. #include "LocalConfig.h"
  6. #include "LogicDriver.h"
  7. #include "BusUnitLogic.h"
  8. #include "PacketAnalizer.h"
  9. #include "LogicClient.h"
  10. #define BusUnitGuidStr "{181F92D6-A546-4ACF-8EF1-2CCAA9EA974E}"
  11. // 这是导出变量的一个示例
  12. BUSUNITLOGIC_API int nBusUnitLogic=0;
  13. // 这是导出函数的一个示例。
  14. BUSUNITLOGIC_API int fnBusUnitLogic(void)
  15. {
  16. return 42;
  17. }
  18. /// <summary>
  19. /// 创建带Console显示的进程
  20. /// </summary>
  21. /// <param name="pfullFilePath">进程启动命令行</param>
  22. /// <param name="pfullWorkPath">工作路径</param>
  23. /// <param name="pinfo">返回进程Info</param>
  24. /// <param name="ShowWindow">是否显示Console</param>
  25. /// <returns></returns>
  26. BOOL CreateTheProcess_Console_Config(const TCHAR *pfullFilePath, const TCHAR *pfullWorkPath, PROCESS_INFORMATION &pinfo, bool ShowWindow)
  27. {
  28. string fullFilePath = pfullFilePath;
  29. size_t index = fullFilePath.rfind('\\');
  30. char szCommand[MAX_PATH];
  31. memset(szCommand, 0, MAX_PATH);
  32. strcpy_s(szCommand, MAX_PATH, pfullFilePath);
  33. STARTUPINFO si;
  34. ZeroMemory(&si, sizeof(si));
  35. si.cb = sizeof(si);
  36. ZeroMemory(&pinfo, sizeof(pinfo));
  37. if (ShowWindow == false)
  38. {
  39. si.dwFlags = STARTF_USESHOWWINDOW;
  40. si.wShowWindow = SW_HIDE;
  41. }
  42. if (!CreateProcess(NULL, // No module name (use command line)
  43. szCommand, // Command line
  44. NULL, // Process handle not inheritable
  45. NULL, // Thread handle not inheritable
  46. FALSE, // Set handle inheritance to FALSE
  47. CREATE_NEW_CONSOLE, // No creation flags
  48. NULL, // Use parent's environment block
  49. //NULL,
  50. pfullWorkPath, // Use parent's starting directory
  51. &si, // Pointer to STARTUPINFO structure
  52. &pinfo) // Pointer to PROCESS_INFORMATION structure
  53. )
  54. {
  55. printf("CreateProcess failed (%d).\n", GetLastError());
  56. printf("Command path :%s\n", szCommand);
  57. return FALSE;
  58. }
  59. return TRUE;
  60. }
  61. // 这是已导出类的构造函数。
  62. // 有关类定义的信息,请参阅 BusUnitLogic.h
  63. BusUnitLogic::BusUnitLogic()
  64. {
  65. m_DevList = new DeviceDescriptList();
  66. m_pbusID = new BaseJsonDataObject<string>();
  67. m_pbusID->SetKey("BusId");
  68. (*m_pbusID) = (const char*)getLocalEbusId();
  69. m_pMachineID = new BaseJsonDataObject<string>();
  70. m_pMachineID->SetKey("MachineId");
  71. (*m_pMachineID) = (const char*)getLocalMachineId();
  72. m_pProcID = new BaseJsonDataObject<UINT64>();
  73. m_pProcID->SetKey("ProcId");
  74. (*m_pProcID) = (UINT64)GetCurrentProcessId();
  75. m_pState = new BaseJsonDataObject<int>();
  76. m_pState->SetKey("Status");
  77. (*m_pState) = 0;
  78. m_pExitFlag = new BaseJsonDataObject<int>();
  79. m_pExitFlag->SetKey("ExitFlag");
  80. (*m_pExitFlag) = 0;
  81. m_pEnableEthBus = new BaseJsonDataObject<int>();
  82. m_pEnableEthBus->SetKey("EnableEthBus");
  83. (*m_pEnableEthBus) = 0;//default is disconnect
  84. m_pEthBusRouterIp = new BaseJsonDataObject<string>();
  85. m_pEthBusRouterIp->SetKey("EthBusRouterIp");
  86. (*m_pEthBusRouterIp) = (const char*)"";
  87. m_pForTest = new BaseJsonDataObject<bool>();
  88. m_pForTest->SetKey("ForTestFlag");
  89. (*m_pForTest) = 0;//default is 0
  90. m_pFullDriverList = new DriverDescriptList();
  91. m_pFullDriverList->SetKey("FullDriverList");
  92. m_pConfigDriverList = new DriverDescriptList();
  93. m_pConfigDriverList->SetKey("ConfigDriverList");
  94. m_pProcessInfo = new map<string, vector<TargetDriverInfo>>();
  95. m_bConfigRemoveDriver = false;
  96. return;
  97. }
  98. BusUnitLogic::~BusUnitLogic()
  99. {
  100. delete m_DevList;
  101. delete m_pbusID;
  102. delete m_pMachineID;
  103. delete m_pProcID;
  104. delete m_pState;
  105. delete m_pExitFlag;
  106. delete m_pEnableEthBus;
  107. delete m_pEthBusRouterIp;
  108. delete m_pForTest;
  109. delete m_pFullDriverList;
  110. delete m_pConfigDriverList;
  111. delete m_pProcessInfo;
  112. }
  113. /// <summary>
  114. /// 获取Bus单元逻辑设备的GUID类型
  115. /// </summary>
  116. /// <param name="DevType">返回值</param>
  117. /// <returns></returns>
  118. bool SYSTEM_CALL BusUnitLogic::GetDeviceType(GUID &DevType)
  119. {
  120. return string_2_guid(BusUnitGuidStr, DevType);
  121. }
  122. /// <summary>
  123. /// 测试数据更新验证,如果值相同,则生成通知
  124. /// </summary>
  125. /// <param name="Flag">判定条件值</param>
  126. /// <returns></returns>
  127. int DATA_ACTION BusUnitLogic::ForTest(bool Flag)
  128. {
  129. RET_STATUS ret = RET_SUCCEED;
  130. if ((*m_pForTest) != Flag)
  131. {
  132. (*m_pForTest) = Flag;
  133. ResDataObject NotifyData;
  134. PacketAnalizer::MakeNotify(NotifyData, PACKET_CMD_UPDATE, m_pForTest->GetKey(), m_pForTest->GetVal());
  135. ret = CmdFromLogicDev(&NotifyData);
  136. }
  137. return ret;
  138. }
  139. //get device resource
  140. /// <summary>
  141. /// 返回Bus单元逻辑设备的资源描述,这里新增了订阅Action
  142. /// </summary>
  143. /// <param name="pDeviceResource">返回值</param>
  144. /// <returns></returns>
  145. RET_STATUS SYSTEM_CALL BusUnitLogic::GetDeviceResource(ResDataObject PARAM_OUT *pDeviceResource)
  146. {
  147. bool ret = true;
  148. //make device type
  149. ret &= pDeviceResource->add("ClientType", DPC_UnitClient);
  150. ret &= pDeviceResource->add("DeviceType", BusUnitGuidStr);
  151. //make attributes
  152. ResDataObject AttrVal;
  153. AttrVal.add(m_pMachineID->GetKey(), m_pMachineID->GetVal());
  154. AttrVal.add(m_pbusID->GetKey(), m_pbusID->GetVal());
  155. AttrVal.add(m_pProcID->GetKey(), m_pProcID->GetVal());
  156. AttrVal.add(m_pState->GetKey(), m_pState->GetVal());
  157. AttrVal.add(m_pExitFlag->GetKey(), m_pExitFlag->GetVal());
  158. AttrVal.add(m_pEnableEthBus->GetKey(), m_pEnableEthBus->GetVal());
  159. AttrVal.add(m_pEthBusRouterIp->GetKey(), m_pEthBusRouterIp->GetVal());
  160. AttrVal.add(m_pForTest->GetKey(), m_pForTest->GetVal());
  161. ResDataObject arraylist;
  162. m_DevList->GetResDataObject(arraylist);
  163. AttrVal.add(m_DevList->GetKey(), arraylist);
  164. //full driver list
  165. arraylist.clear();
  166. m_pFullDriverList->GetResDataObject(arraylist);
  167. AttrVal.add(m_pFullDriverList->GetKey(), arraylist);
  168. //config driver list
  169. arraylist.clear();
  170. m_pConfigDriverList->GetResDataObject(arraylist);
  171. AttrVal.add(m_pConfigDriverList->GetKey(), arraylist);
  172. ret &= pDeviceResource->add("Attribute", AttrVal);
  173. //Actions
  174. ResDataObject ActionVal;
  175. ret &= ActionVal.add("AddDeviceDescrpt", "");
  176. ret &= ActionVal.add("DelDeviceDescrpt", "");
  177. ret &= ActionVal.add("ExitDriverProc", "");
  178. ret &= ActionVal.add("SetDeviceStatus", "");
  179. ret &= ActionVal.add("SetEthBusSwitch", "");
  180. ret &= ActionVal.add("SetEthBusRouterIp", "");
  181. ret &= ActionVal.add("ForTest", "");
  182. ret &= ActionVal.add("ConfigLoadDriver", "");
  183. ret &= ActionVal.add("ConfigRemoveDriver", "");
  184. ret &= ActionVal.add("SubscribeTopic", ""); //DPC 订阅事件
  185. ret &= ActionVal.add("Unsubscribe", ""); //取消订阅
  186. if (ret)
  187. {
  188. pDeviceResource->add("Action", ActionVal);
  189. return RET_SUCCEED;
  190. }
  191. return RET_FAILED;
  192. }
  193. //ResourceCommand Request In and Response Out
  194. RET_STATUS SYSTEM_CALL BusUnitLogic::Request(ResDataObject PARAM_IN *pRequest, ResDataObject PARAM_OUT *pResponse)
  195. {
  196. ResDataObject res;
  197. RET_STATUS ret = RET_NOSUPPORT;
  198. //1. analize request
  199. PACKET_CMD cmd = PacketAnalizer::GetPacketCmd(pRequest);
  200. //cmd:get,set,add,del,exe,
  201. //ignore open/close
  202. //Action和Attr应该做成一个MAP,经过MAP访问这些资源,不应该是switch case
  203. //为了快速做一版,先保留switch case!!!
  204. string keystr = PacketAnalizer::GetPacketKey(pRequest);
  205. ACTION_SYNC_MODE syncmode = PacketAnalizer::GetPacketSyncMode(pRequest);
  206. switch (cmd){
  207. case PACKET_CMD_EXE:
  208. {
  209. if (keystr == "AddDeviceDescrpt")
  210. {
  211. bool result = true;
  212. ResDataObject obj1,obj2,obj3,obj4,obj5,obj6;
  213. result &= PacketAnalizer::GetParam(pRequest, 0, obj1);
  214. result &= PacketAnalizer::GetParam(pRequest, 1, obj2);
  215. result &= PacketAnalizer::GetParam(pRequest, 2, obj3);
  216. result &= PacketAnalizer::GetParam(pRequest, 3, obj4);
  217. result &= PacketAnalizer::GetParam(pRequest, 4, obj5);
  218. result &= PacketAnalizer::GetParam(pRequest, 5, obj6);
  219. if (result)
  220. {
  221. //2. call api
  222. ret = (RET_STATUS)AddDeviceDescrpt((const char *)obj1, (const char *)obj2, (const char *)obj3, (UINT64)obj4, (UINT64)obj5, (bool)obj6);
  223. }
  224. else
  225. {
  226. //put log here
  227. }
  228. }
  229. else if (keystr == "ConfigLoadDriver")
  230. {
  231. //1. 获取参数
  232. ResDataObject obj;
  233. if (PacketAnalizer::GetParam(pRequest, 0, obj))
  234. {
  235. //2. call api
  236. char szFixDrv[MAX_PATH];
  237. memset(szFixDrv, 0, MAX_PATH);
  238. ret = (RET_STATUS)ConfigLoadDriver((const char *)obj, szFixDrv,MAX_PATH);
  239. //3. 打包
  240. res.add("P0", szFixDrv);
  241. pResponse->update("CONTEXT", res);
  242. }
  243. }
  244. else if (keystr == "ConfigRemoveDriver")
  245. {
  246. //1. 获取参数
  247. ResDataObject obj;
  248. if (PacketAnalizer::GetParam(pRequest, 0, obj))
  249. {
  250. //2. call api
  251. ret = (RET_STATUS)ConfigRemoveDriver((const char *)obj);
  252. //3. 打包
  253. }
  254. }
  255. else if (keystr == "DelDeviceDescrpt")
  256. {
  257. //1. 获取参数
  258. ResDataObject obj;
  259. if (PacketAnalizer::GetParam(pRequest, 0, obj))
  260. {
  261. //2. call api
  262. ret = (RET_STATUS)DelDeviceDescrpt((const char *)obj);
  263. //3. 打包
  264. }
  265. }
  266. else if (keystr == "ExitDriverProc")
  267. {
  268. ret = (RET_STATUS)ExitDriverProc();
  269. }
  270. else if (keystr == "SetDeviceStatus")
  271. {
  272. //1. 获取参数
  273. ResDataObject obj;
  274. if (PacketAnalizer::GetParam(pRequest, 0, obj))
  275. {
  276. //2. call api
  277. ret = (RET_STATUS)SetDeviceStatus((int)obj);
  278. //3. 打包
  279. }
  280. }
  281. else if (keystr == "SetEthBusSwitch")
  282. {
  283. //1. 获取参数
  284. ResDataObject obj;
  285. if (PacketAnalizer::GetParam(pRequest, 0, obj))
  286. {
  287. //2. call api
  288. ret = (RET_STATUS)SetEthBusSwitch((int)obj);
  289. //3. 打包
  290. }
  291. }
  292. else if (keystr == "SetEthBusRouterIp")
  293. {
  294. //1. 获取参数
  295. ResDataObject obj;
  296. if (PacketAnalizer::GetParam(pRequest, 0, obj))
  297. {
  298. //2. call api
  299. ret = (RET_STATUS)SetEthBusRouterIp((const char*)obj);
  300. //3. 打包
  301. }
  302. }
  303. else if (keystr == "Get")
  304. {
  305. //1. 获取参数
  306. ResDataObject Res;
  307. ResDataObject obj;
  308. if (PacketAnalizer::GetParam(pRequest, 0, obj))
  309. {
  310. //2. call api
  311. ret = (RET_STATUS)Get((const char*)obj,Res);
  312. //3. 打包
  313. if (ret == RET_SUCCEED)
  314. {
  315. PacketAnalizer::UpdatePacketContext((*pResponse), Res);
  316. }
  317. }
  318. }
  319. else if (keystr == "ForTest")
  320. {
  321. ResDataObject Res;
  322. ResDataObject obj;
  323. if (PacketAnalizer::GetParam(pRequest, 0, obj))
  324. {
  325. //2. call api
  326. ret = (RET_STATUS)ForTest((bool)obj);
  327. }
  328. }
  329. else if (keystr == "SubscribeTopic") {
  330. // 收到 应用或者其他模块的 订阅消息
  331. // 往MQTT broker 里订阅
  332. }
  333. }
  334. break;
  335. default:
  336. //对当前设备来讲,其他都是浮云
  337. break;
  338. }
  339. //return status
  340. PacketAnalizer::MakeRetCode(ret, pResponse);
  341. return ret;
  342. }
  343. //notify to lower layer
  344. RET_STATUS SYSTEM_CALL BusUnitLogic::CmdToLogicDev(ResDataObject PARAM_IN *pCmd)
  345. {
  346. assert(0);//not happening
  347. return RET_FAILED;
  348. }
  349. //errors,warnings
  350. void BusUnitLogic::SetErrorInfo(int errCode, char *pErrInfo)
  351. {
  352. }
  353. void BusUnitLogic::SetWarningInfo(int warningCode, char *pWarningInfo)
  354. {
  355. }
  356. bool SYSTEM_CALL BusUnitLogic::CheckBusIdsExistance(const char *pszBusId)
  357. {
  358. //ResDataObject ConfigDrvList;
  359. //GetDriverConfigFiles(ConfigDrvList);
  360. //map<string, string> HitCheck;
  361. ////check static first
  362. //for (size_t i = 0; i < ConfigDrvList.size(); i++)
  363. //{
  364. // ResDataObject BusId;
  365. // ResDataObject DriverBusId;
  366. // if (GetDriverEbusId(ConfigDrvList.GetKey(i), BusId, DriverBusId))
  367. // {
  368. // map<string, string>::iterator iter = HitCheck.find((const char*)BusId);
  369. // if (iter != HitCheck.end())
  370. // {
  371. // //exist one
  372. // TPRINTA_WARN("Same BusId:%s on both Driver\nDrvA:%s\nDrvB:%s", (const char*)BusId, ConfigDrvList.GetKey(i), iter->second.c_str());
  373. // }
  374. // HitCheck[(const char*)BusId] = ConfigDrvList.GetKey(i);
  375. // if (pszBusId)
  376. // {
  377. // iter = HitCheck.find(pszBusId);
  378. // if (iter != HitCheck.end())
  379. // {
  380. // //exist one
  381. // TPRINTA_WARN("Same BusId:%s on Driver\nDrv:%s\nRequest BusId:%s", (const char*)BusId, ConfigDrvList.GetKey(i), pszBusId);
  382. // }
  383. // }
  384. // }
  385. //}
  386. if (pszBusId)
  387. {
  388. //check live second
  389. map<string, vector<TargetDriverInfo>>::iterator iter = m_pProcessInfo->begin();
  390. while (iter != m_pProcessInfo->end())
  391. {
  392. if (iter->first == pszBusId)
  393. {
  394. TPRINTA_WARN("Same BusId:%s Already on the run...", pszBusId);
  395. return false;
  396. }
  397. ++iter;
  398. }
  399. }
  400. return true;
  401. }
  402. bool SYSTEM_CALL BusUnitLogic::LoadAllConfigDrivers()
  403. {
  404. bool ret = true;
  405. //get list of fulldrvlist
  406. ResDataObject FullDrvList;
  407. GetFullDriverConfigFiles(FullDrvList);
  408. //get list of configdrvlist
  409. ResDataObject ConfigDrvList;
  410. GetDriverConfigFiles(ConfigDrvList);
  411. if (CheckBusIdsExistance(0) == false)
  412. {
  413. return false;
  414. }
  415. //add second
  416. for (size_t i = 0; i < FullDrvList.size(); i++)
  417. {
  418. ResDataObject BusId;
  419. ResDataObject DriverBusId;
  420. //get ebus driver path
  421. if (GetDriverEbusId(FullDrvList.GetKey(i), BusId, DriverBusId))
  422. {
  423. ret &= m_pFullDriverList->AddDriver(DriverBusId);
  424. if (ret == false)
  425. {
  426. GPRINTA_ERROR("AddDriver of FullDrv:%s Failed.", FullDrvList.GetKey(i));
  427. }
  428. }
  429. else
  430. {
  431. GPRINTA_ERROR("GetDriverEbusId configfile:%s Failed.", FullDrvList.GetKey(i));
  432. }
  433. }
  434. for (size_t i = 0; i < ConfigDrvList.size(); i++)
  435. {
  436. ResDataObject BusId;
  437. ResDataObject DriverBusId;
  438. if (GetDriverEbusId(ConfigDrvList.GetKey(i), BusId, DriverBusId))
  439. {
  440. ret &= m_pConfigDriverList->AddDriver(DriverBusId);
  441. if (ret == false)
  442. {
  443. GPRINTA_ERROR("AddDriver of ConfigDrv:%s Failed.", ConfigDrvList.GetKey(i));
  444. }
  445. }
  446. else
  447. {
  448. GPRINTA_ERROR("GetDriverEbusId configfile:%s Failed.", ConfigDrvList.GetKey(i));
  449. }
  450. }
  451. return ret;
  452. }
  453. string GetCurTime64ToString()
  454. {
  455. __time64_t now;
  456. struct tm t1;
  457. _time64(&now);//UTC
  458. _localtime64_s(&t1, &now);//LOCAL
  459. string ret;
  460. ret = FormatstdString("%04d%02d%02d%02d%02d%02d", t1.tm_year + 1900, t1.tm_mon + 1, t1.tm_mday, t1.tm_hour, t1.tm_min, t1.tm_sec);
  461. return ret;
  462. }
  463. int DATA_ACTION BusUnitLogic::MakeDriverNotify(const char *pszDriverpath, bool Add)
  464. {
  465. RET_STATUS ret = RET_SUCCEED;
  466. //exiting...
  467. if ((*m_pExitFlag) != 0)
  468. {
  469. return RET_INVALID;
  470. }
  471. ResDataObject NotifyData;
  472. ResDataObject theContext;
  473. theContext.add(pszDriverpath, "");
  474. if (Add)
  475. {
  476. if (m_pConfigDriverList->AddDriver(pszDriverpath))
  477. {
  478. printf("AddDriver %s ok\n", pszDriverpath);
  479. TPRINTA_DEBUG("AddDriver %s ok\n", pszDriverpath);
  480. PacketAnalizer::MakeNotify(NotifyData, PACKET_CMD_ADD, m_pConfigDriverList->GetKey(), theContext);
  481. CmdFromLogicDev(&NotifyData);
  482. }
  483. else
  484. {
  485. printf("AddDriver %s failed\n", pszDriverpath);
  486. TPRINTA_ERROR("AddDriver %s failed\n", pszDriverpath);
  487. ret = RET_FAILED;
  488. }
  489. }
  490. else
  491. {
  492. if (m_pConfigDriverList->DelDriver(pszDriverpath))
  493. {
  494. printf("DelDriver %s ok\n", pszDriverpath);
  495. TPRINTA_DEBUG("DelDriver %s ok\n", pszDriverpath);
  496. PacketAnalizer::MakeNotify(NotifyData, PACKET_CMD_DEL, m_pConfigDriverList->GetKey(), theContext);
  497. CmdFromLogicDev(&NotifyData);
  498. }
  499. else
  500. {
  501. printf("DelDriver %s failed\n", pszDriverpath);
  502. TPRINTA_ERROR("DelDriver %s failed\n", pszDriverpath);
  503. ret = RET_FAILED;
  504. }
  505. }
  506. return ret;
  507. }
  508. int DATA_ACTION BusUnitLogic::ConfigLoadDriver(const char *pszDriverpath, char *pszFixDriverpath, DWORD FixDrvLen)
  509. {
  510. int ret = RET_SUCCEED;
  511. bool X64Driver = true;
  512. ResDataObject fileList;
  513. //find driver from fulldrvlist
  514. //for test
  515. TPRINTA_DEBUG("Enter ConfigLoadDriver:%s\n", pszDriverpath);
  516. LogicDriver* pDPC = (LogicDriver*)GetDrvDPC();
  517. if (pDPC == 0)
  518. {
  519. TPRINTA_DEBUG("GetDrvDPC failed");
  520. return RET_FAILED;
  521. }
  522. m_bConfigRemoveDriver = true;
  523. pDPC->Thread_Lock(2000);
  524. TPRINTA_INFO("DPC lock ok");
  525. //find x64
  526. if (GetSpecificDriverConfigFiles(fileList, true))
  527. {
  528. //check x64FLAG
  529. //assert(0);//NOT FINISHED YET
  530. //exist x64
  531. for (size_t i = 0; i < fileList.size(); i++)
  532. {
  533. ResDataObject BusId;
  534. ResDataObject DriverBusId;
  535. //get ebus driver path
  536. if (GetDriverEbusId(fileList.GetKey(i), BusId, DriverBusId))
  537. {
  538. //for test
  539. TPRINTA_DEBUG("file[%zd] File:%s\n Buspath:%s\n",i, fileList.GetKey(i), (const char *)DriverBusId);
  540. if (DriverBusId == pszDriverpath)
  541. {
  542. string FullFilePath = fileList.GetKey(i);
  543. //copy configuration
  544. //make new title
  545. string TargetFileTitle = GetFileTitle(FullFilePath);
  546. string TimePos = GetCurTime64ToString();
  547. string NewTargetFileTitle = FormatstdString("\\%s_%s.xml", TargetFileTitle.c_str(), TimePos.c_str());
  548. //make full filepath
  549. //ResDataObject DriverConfigPath = GetDriverConfigPath();
  550. string DriverConfigPath = GetFileDirectory(FullFilePath);//...\FullConfig
  551. DriverConfigPath = GetFileDirectory(DriverConfigPath);//...procDir
  552. DriverConfigPath += "\\DriverConfig";
  553. string FullNewTargetFilePath = DriverConfigPath.c_str();
  554. FullNewTargetFilePath += NewTargetFileTitle;
  555. //for test
  556. TPRINTA_DEBUG("Got Match:%s\nCopy: Org:%s\nDes:%s\n", FullFilePath.c_str(), fileList.GetKey(i), FullNewTargetFilePath.c_str());
  557. if (CopyFile(fileList.GetKey(i), FullNewTargetFilePath.c_str(), TRUE))
  558. {
  559. TargetDriverInfo DrvInfo;
  560. if (GetDriverEbusId(FullNewTargetFilePath.c_str(), DrvInfo.m_RootBusId, DrvInfo.m_DriverBusId, true) == false)
  561. {
  562. return RET_FAILED;
  563. }
  564. //got match
  565. if (CheckBusIdsExistance(DrvInfo.m_RootBusId) == false)
  566. {
  567. //exist bus id
  568. UnloadDriver(DrvInfo.m_RootBusId);
  569. }
  570. //reload
  571. ret = LoadAllConfigDriver(true);
  572. Sleep(6000);//must sleep 6s
  573. if (ret >= RET_SUCCEED)
  574. {
  575. strcpy_s(pszFixDriverpath, FixDrvLen, (const char *)DrvInfo.m_DriverBusId);
  576. }
  577. TPRINTA_DEBUG("LoadAllConfigDriver return:%d\n", ret);
  578. m_bConfigRemoveDriver = false;
  579. pDPC->Thread_UnLock();
  580. TPRINTA_INFO("DPC Unlock ok");
  581. return ret;
  582. }
  583. else
  584. {
  585. TPRINTA_ERROR("CopyFile failed\n");
  586. }
  587. }
  588. }
  589. else
  590. {
  591. TPRINTA_ERROR("GetDriverEbusId failed\n");
  592. }
  593. }
  594. }
  595. else
  596. {
  597. TPRINTA_ERROR("GetSpecificDriverConfigFiles failed\n");
  598. }
  599. pDPC->Thread_UnLock();
  600. TPRINTA_INFO("DPC Unlock ok");
  601. m_bConfigRemoveDriver = false;
  602. return RET_SUCCEED;
  603. }
  604. bool SYSTEM_CALL BusUnitLogic::CheckAndKillLiveDriver(const char *pszDriverpath)
  605. {
  606. printf("Try Del Driver:%s\n", pszDriverpath);
  607. TPRINTA_DEBUG("Try Del Driver:%s\n", pszDriverpath);
  608. map<string, vector<TargetDriverInfo>>::iterator iter = m_pProcessInfo->begin();
  609. while (iter != m_pProcessInfo->end())
  610. {
  611. for (size_t DriverIdx = 0; DriverIdx < iter->second.size(); DriverIdx++)
  612. {
  613. string DrvBusId = (const char*)(iter->second)[DriverIdx].m_DriverBusId;
  614. TPRINTA_DEBUG("Test Match:%s\n", DrvBusId.c_str());
  615. if (DrvBusId == pszDriverpath)
  616. {
  617. TPRINTA_DEBUG("Got Match:%s\n", DrvBusId.c_str());
  618. //got one
  619. if (WaitForSingleObject(((iter->second)[0].m_info.hProcess), 0) == WAIT_TIMEOUT)
  620. {
  621. //process exist
  622. TPRINTA_DEBUG("Target Process Exist");
  623. string strBusIdCopy = DrvBusId;
  624. LogicClient chClient1;
  625. chClient1.SetRouter(CCOS_PACKET_ROUTE_LOCAL);
  626. DrvBusId = "/";
  627. DrvBusId += iter->first.c_str();
  628. //TPRINTA_INFO("DrvBusId :%s\n", DrvBusId.c_str());
  629. TPRINTA_INFO("open :%s\n", strBusIdCopy.c_str());
  630. int nRet = -1;
  631. if (chClient1.Open(strBusIdCopy.c_str(), ALL_ACCESS, ONE_ACTION_TIMEOUT) >= RET_SUCCEED)
  632. {
  633. //if (strBusIdCopy.find("Detector"))//disconnect before Del
  634. //{
  635. // bool bret = false;
  636. // ACTION_SYNC_MODE syncmode = chClient1.GetRequestSyncMode();
  637. // TPRINTA_DEBUG("RequestSyncMode is %d\n",syncmode);
  638. // if (syncmode != ACTION_SYNC)
  639. // {
  640. // bret = chClient1.SetRequestSyncMode(ACTION_SYNC);
  641. // TPRINTA_DEBUG("SetRequestSyncMode to sync\n");
  642. // }
  643. // ResDataObject req, resp;
  644. // int nRet = chClient1.Action("DisConnect", req, resp, ONE_ACTION_TIMEOUT);
  645. // if (nRet >= RET_SUCCEED)
  646. // {
  647. // TPRINTA_DEBUG("Succeed Action: DisConnect\n");
  648. // Sleep(2000);//wait detector finish the disconnect.UI send cmd wait 3s
  649. // }
  650. // else
  651. // {
  652. // TPRINTA_DEBUG("Failed Action: DisConnect ret = %d\n", nRet);
  653. // }
  654. // if (bret)
  655. // {
  656. // chClient1.SetRequestSyncMode(syncmode);
  657. // TPRINTA_DEBUG("SetRequestSyncMode back\n");
  658. // }
  659. //}
  660. ResDataObject req;
  661. nRet = chClient1.Action_Req("ExitDriverProc", req, ONE_ACTION_TIMEOUT);
  662. if (nRet >= RET_SUCCEED)
  663. {
  664. printf("Succeed Action:ExitDriverProc %s ...\n", strBusIdCopy.c_str());
  665. TPRINTA_DEBUG("Succeed Action: ExitDriverProc %s ...\n", strBusIdCopy.c_str());
  666. chClient1.Close();
  667. if (WaitForSingleObject(((iter->second)[0].m_info.hProcess), ONE_ACTION_TIMEOUT) == WAIT_TIMEOUT)
  668. {
  669. TerminateProcess(((iter->second)[0].m_info.hProcess), 0);
  670. }
  671. TPRINTA_DEBUG("Try Delete Driver File:%s\n", (const char*)(iter->second)[DriverIdx].m_CcosProcInfo["DriverConfigPath"]);
  672. if (DeleteFile((const char*)(iter->second)[DriverIdx].m_CcosProcInfo["DriverConfigPath"]))
  673. {
  674. TPRINTA_DEBUG("Try Delete Driver File Succeed\n");
  675. iter->second.erase(iter->second.begin() + DriverIdx);
  676. //if (iter->second.size() == 0)
  677. {
  678. m_pProcessInfo->erase(iter);
  679. }
  680. return true;
  681. }
  682. else
  683. {
  684. DWORD nret = GetLastError();
  685. TPRINTA_DEBUG("Try Delete Driver File failed %d\n", nret);
  686. }
  687. }
  688. else
  689. {
  690. printf("Failed Action:ExitDriverProc -> %s...\n", strBusIdCopy.c_str());
  691. //TPRINTA_ERROR("Failed Action: ExitDriverProc -> %s ...\n", strBusIdCopy.c_str());
  692. TPRINTA_DEBUG("Failed Action: ExitDriverProc ret %d-> %s ...\n",nRet, strBusIdCopy.c_str());
  693. chClient1.Close();
  694. }
  695. //if (chClient1.Action(pszAction, req, res, Timeout) < RET_SUCCEED)
  696. //{
  697. // printf("Failed Action:%s ...\n",pszAction);
  698. // TPRINTA_ERROR("Failed Action: %s ...\n", pszAction);
  699. // chClient1.Close();
  700. //}
  701. //else
  702. //{
  703. // printf("Succeed Action:%s ...\n", pszAction);
  704. // TPRINTA_DEBUG("Succeed Action: %s ...\n", pszAction);
  705. // return true;
  706. //}
  707. //chClient1.Close();
  708. }
  709. else
  710. {
  711. printf("Open local %s Failed\n", DrvBusId.c_str());
  712. TPRINTA_DEBUG("Open local %s Failed ret %d\n", strBusIdCopy.c_str(),nRet);
  713. }
  714. }
  715. else
  716. {
  717. TPRINTA_DEBUG("Target Process Not Exist");
  718. TPRINTA_DEBUG("Try Delete Driver File:%s\n", (const char*)(iter->second)[DriverIdx].m_CcosProcInfo["DriverConfigPath"]);
  719. if (DeleteFile((const char*)(iter->second)[DriverIdx].m_CcosProcInfo["DriverConfigPath"]))
  720. {
  721. TPRINTA_DEBUG("Try Delete Driver File Succeed\n");
  722. iter->second.erase(iter->second.begin() + DriverIdx);
  723. //if (iter->second.size() == 0)
  724. {
  725. m_pProcessInfo->erase(iter);
  726. }
  727. return true;
  728. }
  729. }
  730. return false;
  731. }
  732. }
  733. ++iter;
  734. }
  735. return true;
  736. }
  737. int DATA_ACTION BusUnitLogic::ConfigRemoveDriver(const char *pszDriverpath)
  738. {
  739. int ret = RET_SUCCEED;
  740. TPRINTA_DEBUG("Enter ConfigRemoveDriver:%s\n", pszDriverpath);
  741. //find driver from configdrvlist
  742. if (m_pConfigDriverList->IsDriverExist(pszDriverpath) == false)
  743. {
  744. //not exist then return succeed.
  745. TPRINTA_DEBUG("Target Driver Not Exist %s",pszDriverpath);
  746. return RET_FAILED;
  747. }
  748. LogicDriver* pDPC = (LogicDriver*)GetDrvDPC();
  749. if (pDPC == 0)
  750. {
  751. TPRINTA_DEBUG("GetDrvDPC failed");
  752. return RET_FAILED;
  753. }
  754. m_bConfigRemoveDriver = true;
  755. pDPC->Thread_Lock(3000);
  756. TPRINTA_INFO("DPC lock ok");
  757. try{
  758. //check Live status
  759. if (CheckAndKillLiveDriver(pszDriverpath))
  760. {
  761. ret = MakeDriverNotify(pszDriverpath, false);
  762. //reload
  763. ret = LoadAllConfigDriver(true);
  764. Sleep(6000);//must sleep 6s
  765. TPRINTA_DEBUG("sleep over");
  766. }
  767. else
  768. {
  769. ret = RET_FAILED;
  770. }
  771. }
  772. catch (...)
  773. {
  774. ret = RET_FAILED;
  775. TPRINTA_DEBUG("crash");
  776. }
  777. pDPC->Thread_UnLock();
  778. TPRINTA_INFO("DPC Unlock ok");
  779. m_bConfigRemoveDriver = false;
  780. TPRINTA_DEBUG("ConfigRemoveDriver over");
  781. return ret;
  782. }
  783. //Data Access
  784. int DATA_ACTION BusUnitLogic::AddDeviceDescrpt(const char PARAM_IN *pDevPath, const char PARAM_IN *pTargetType, const char PARAM_IN *pMachineId, UINT64 ProcId, UINT64 Addr, bool forceAdd)
  785. {
  786. DeviceDescript dd;
  787. //exiting...
  788. if ((*m_pExitFlag) != 0)
  789. {
  790. return RET_INVALID;
  791. }
  792. dd.SetKey(pDevPath);
  793. dd.m_TargetType = pTargetType;
  794. dd.m_ProcId = ProcId;
  795. dd.m_Address = Addr;
  796. dd.m_MachineId = pMachineId;
  797. ResDataObject NotifyData;
  798. ResDataObject theContext;
  799. dd.GetResDataObject(theContext);
  800. if (m_DevList->AddDriver(dd))
  801. {
  802. PacketAnalizer::MakeNotify(NotifyData, PACKET_CMD_ADD, m_DevList->GetKey(), theContext);
  803. }
  804. else
  805. {
  806. PacketAnalizer::MakeNotify(NotifyData, PACKET_CMD_ONLINE, m_DevList->GetKey(), theContext);
  807. }
  808. CmdFromLogicDev(&NotifyData);
  809. return RET_SUCCEED;
  810. }
  811. int DATA_ACTION BusUnitLogic::Get(const char PARAM_IN *pKey,ResDataObject &Res)
  812. {
  813. string Key = pKey;
  814. if (Key == m_DevList->GetKey())
  815. {
  816. ResDataObject arraylist;
  817. m_DevList->GetResDataObject(arraylist);
  818. Res.add(m_DevList->GetKey(), arraylist);
  819. return RET_SUCCEED;
  820. }
  821. return RET_FAILED;
  822. }
  823. int DATA_ACTION BusUnitLogic::DelDeviceDescrpt(const char PARAM_IN *pDevPath)
  824. {
  825. if (m_DevList->DelVal(pDevPath))
  826. {
  827. //3. Notify
  828. ResDataObject NotifyData;
  829. ResDataObject theContext;
  830. theContext.add("DevicePath", pDevPath);
  831. PacketAnalizer::MakeNotify(NotifyData, PACKET_CMD_DEL, m_DevList->GetKey(), theContext);
  832. CmdFromLogicDev(&NotifyData);
  833. }
  834. return RET_SUCCEED;
  835. }
  836. int DATA_ACTION BusUnitLogic::SetDeviceStatus(int Status)
  837. {
  838. RET_STATUS ret = RET_SUCCEED;
  839. if ((*m_pState) != Status)
  840. {
  841. ResDataObject NotifyData;
  842. (*m_pState) = Status;
  843. PacketAnalizer::MakeNotify(NotifyData, PACKET_CMD_UPDATE, m_pState->GetKey(), m_pState->GetVal());
  844. ret = CmdFromLogicDev(&NotifyData);
  845. }
  846. return ret;
  847. }
  848. int DATA_ACTION BusUnitLogic::SetEthBusSwitch(int Switch)
  849. {
  850. RET_STATUS ret = RET_SUCCEED;
  851. if ((*m_pEnableEthBus) != Switch)
  852. {
  853. ResDataObject NotifyData;
  854. (*m_pEnableEthBus) = Switch;
  855. PacketAnalizer::MakeNotify(NotifyData, PACKET_CMD_UPDATE, m_pEnableEthBus->GetKey(), m_pEnableEthBus->GetVal());
  856. ret = CmdFromLogicDev(&NotifyData);
  857. }
  858. return ret;
  859. }
  860. int DATA_ACTION BusUnitLogic::SetEthBusRouterIp(const char PARAM_IN *pRouterIp)
  861. {
  862. RET_STATUS ret = RET_SUCCEED;
  863. std::string Param = pRouterIp;
  864. std::string RouterIp = (*m_pEthBusRouterIp);
  865. if (RouterIp != Param)
  866. {
  867. ResDataObject NotifyData;
  868. (*m_pEthBusRouterIp) = Param;
  869. PacketAnalizer::MakeNotify(NotifyData, PACKET_CMD_UPDATE, m_pEthBusRouterIp->GetKey(), m_pEthBusRouterIp->GetVal());
  870. ret = CmdFromLogicDev(&NotifyData);
  871. }
  872. return ret;
  873. }
  874. int DATA_ACTION BusUnitLogic::ExitDriverProc()
  875. {
  876. RET_STATUS ret = RET_SUCCEED;
  877. if ((*m_pExitFlag) == 0)
  878. {
  879. ResDataObject NotifyData;
  880. SetExitFlag(1);
  881. PacketAnalizer::MakeNotify(NotifyData, PACKET_CMD_UPDATE, m_pExitFlag->GetKey(), m_pExitFlag->GetVal());
  882. ret = CmdFromLogicDev(&NotifyData);
  883. }
  884. return ret;
  885. }
  886. int SYSTEM_CALL BusUnitLogic::GetExitFlag()
  887. {
  888. return (*m_pExitFlag);
  889. }
  890. int SYSTEM_CALL BusUnitLogic::SetExitFlag(int ExitFlag)
  891. {
  892. (*m_pExitFlag) = ExitFlag;
  893. return 1;
  894. }
  895. int SYSTEM_CALL BusUnitLogic::GetbusId(ResDataObject &obj)
  896. {
  897. obj = m_pbusID->GetVal();
  898. return RET_SUCCEED;
  899. }
  900. int SYSTEM_CALL BusUnitLogic::GetMachineId(ResDataObject &obj)
  901. {
  902. obj = m_pMachineID->GetVal();
  903. return RET_SUCCEED;
  904. }
  905. int SYSTEM_CALL BusUnitLogic::GetProcId(UINT64 &obj)
  906. {
  907. obj = (*m_pProcID);
  908. return RET_SUCCEED;
  909. }
  910. int SYSTEM_CALL BusUnitLogic::SetbusId(ResDataObject &obj)
  911. {
  912. (*m_pbusID) = (const char *)obj;
  913. return RET_SUCCEED;
  914. }
  915. int SYSTEM_CALL BusUnitLogic::SetMachineId(ResDataObject &obj)
  916. {
  917. (*m_pMachineID) = (const char *)obj;
  918. return RET_SUCCEED;
  919. }
  920. int SYSTEM_CALL BusUnitLogic::SetProcId(UINT64 obj)
  921. {
  922. (*m_pProcID) = (UINT64)obj;
  923. return RET_SUCCEED;
  924. }
  925. DWORD SYSTEM_CALL BusUnitLogic::GetDeviceCount()
  926. {
  927. return (DWORD)m_DevList->m_DeviceList.size();
  928. }
  929. bool SYSTEM_CALL BusUnitLogic::GetDeviceDescript(DWORD Idx, ResDataObject &DevPath, ResDataObject &DevType, ResDataObject &MachineId, UINT64 &ProcId, UINT64 &Addr)
  930. {
  931. if (m_DevList->m_DeviceList.size() > Idx)
  932. {
  933. DevPath = m_DevList->m_DeviceList[Idx].GetKey();
  934. DevType = m_DevList->m_DeviceList[Idx].m_TargetType.GetVal();
  935. MachineId = m_DevList->m_DeviceList[Idx].m_MachineId.GetVal();
  936. ProcId = m_DevList->m_DeviceList[Idx].m_ProcId;
  937. Addr = m_DevList->m_DeviceList[Idx].m_Address;
  938. return true;
  939. }
  940. return false;
  941. }
  942. int SYSTEM_CALL BusUnitLogic::GetDeviceStatus()
  943. {
  944. return (*m_pState);
  945. }
  946. int SYSTEM_CALL BusUnitLogic::GetEthBusSwitch()
  947. {
  948. return (*m_pEnableEthBus);
  949. }
  950. int SYSTEM_CALL BusUnitLogic::GetEthBusRouterIp(ResDataObject &obj)
  951. {
  952. obj = (*m_pEthBusRouterIp).GetVal();
  953. return 1;
  954. }
  955. /// <summary>
  956. /// 检查所有驱动进程,如果进程Crash了,需要根据配置重新拉起来
  957. /// </summary>
  958. /// <returns></returns>
  959. void SYSTEM_CALL BusUnitLogic::CheckAllLiveDriver()
  960. {
  961. map<string, vector<TargetDriverInfo>>::iterator iter = m_pProcessInfo->begin();
  962. while (iter != m_pProcessInfo->end())
  963. {
  964. if (m_bConfigRemoveDriver)
  965. {
  966. //Sleep(2000);
  967. TPRINTA_INFO("ConfigDriver working");
  968. return;
  969. }
  970. //for (size_t DriverIdx = 0; DriverIdx < iter->second.size(); DriverIdx++)
  971. {
  972. string DrvBusId = "/";
  973. DrvBusId += (const char*)iter->first.c_str();
  974. {
  975. //got one
  976. if (WaitForSingleObject(((iter->second)[0].m_info.hProcess), 0) == WAIT_TIMEOUT)
  977. {
  978. //process exist
  979. LogicClient chClient1;
  980. chClient1.SetRouter(CCOS_PACKET_ROUTE_LOCAL);
  981. if (chClient1.Open(DrvBusId.c_str(), ALL_ACCESS, ONE_ACTION_TIMEOUT) >= RET_SUCCEED)
  982. {
  983. chClient1.Close();
  984. }
  985. else
  986. {
  987. printf("Open local %s Failed while check Live\n", DrvBusId.c_str());
  988. TPRINTA_ERROR("Open local %s Failed while check Live\n", DrvBusId.c_str());
  989. }
  990. }
  991. else
  992. {
  993. if (m_bConfigRemoveDriver)
  994. {
  995. TPRINTA_INFO("ConfigDriver working 2");
  996. return;
  997. }
  998. //target process crashed
  999. printf("Autoload Flag:%d \n BusId:%s\n", (bool)(iter->second)[0].m_CcosProcInfo["AutoLoad"], iter->first.c_str());
  1000. //do reload
  1001. if ((bool)(iter->second)[0].m_CcosProcInfo["AutoLoad"])
  1002. {
  1003. memset(&(iter->second)[0].m_info, 0, sizeof(PROCESS_INFORMATION));
  1004. if (CreateTheProcess_Console_Config((iter->second)[0].m_CcosProcInfo["DriverPath"],
  1005. (iter->second)[0].m_CcosProcInfo["WorkPath"],
  1006. (iter->second)[0].m_info,
  1007. (iter->second)[0].m_CcosProcInfo["ShowWindow"]) == FALSE)
  1008. {
  1009. printf("Reload Failed.Can't Create Process for config:%s\n",
  1010. (const char*)(iter->second)[0].m_CcosProcInfo["DriverConfigPath"]);
  1011. TPRINTA_ERROR("Reload Failed.Can't Create Process for config:%s",
  1012. (const char*)(iter->second)[0].m_CcosProcInfo["DriverConfigPath"]);
  1013. }
  1014. else
  1015. {
  1016. printf("Reload Succeed.Create Process for config:%s Succeed\n",
  1017. (const char*)(iter->second)[0].m_CcosProcInfo["DriverConfigPath"]);
  1018. TPRINTA_INFO("Reload Succeed.Create Process for config:%s Succeed",
  1019. (const char*)(iter->second)[0].m_CcosProcInfo["DriverConfigPath"]);
  1020. Sleep(6000);//wait
  1021. }
  1022. }
  1023. else
  1024. {
  1025. //kick iter;
  1026. printf("Kick crashed process. BusId:%s\n", iter->first.c_str());
  1027. TPRINTA_INFO("Kick crashed process. BusId:%s\n", iter->first.c_str());
  1028. DelDeviceDescrpt(DrvBusId.c_str());
  1029. iter = m_pProcessInfo->erase(iter);
  1030. continue;
  1031. }
  1032. }
  1033. }
  1034. }
  1035. ++iter;
  1036. }
  1037. return;
  1038. }
  1039. int SYSTEM_CALL BusUnitLogic::LoadAllConfigDriver(bool ForReload)
  1040. {
  1041. RET_STATUS ret = RET_SUCCEED;
  1042. //this is for dpc call
  1043. bool FullDriverList = false;
  1044. ResDataObject fileList;
  1045. //find driver from fulldrvlist
  1046. if (CheckBusIdsExistance(0) == false)
  1047. {
  1048. TPRINTA_ERROR("CheckBusIdsExistance(0) == false");
  1049. return RET_FAILED;
  1050. }
  1051. LogicDriver* pDPC = (LogicDriver*)GetDrvDPC();
  1052. if (pDPC == 0)
  1053. {
  1054. TPRINTA_ERROR("pDPC == 0");
  1055. return RET_FAILED;
  1056. }
  1057. TPRINTA_INFO("GetDrvDPC ok");
  1058. //pDPC->Thread_Lock();
  1059. //TPRINTA_INFO("DPC lock ok");
  1060. try{
  1061. //find x64
  1062. if (GetSpecificDriverConfigFiles(fileList, FullDriverList))
  1063. {
  1064. TPRINTA_INFO("GetSpecificDriverConfigFiles ok");
  1065. map<string,int> ConflictBusIds;
  1066. map<string, string> BusIdMap;//BusId <-> Dir
  1067. map<string, vector<TargetDriverInfo>> LoadDriverMap;
  1068. //exist x64
  1069. for (size_t i = 0; i < fileList.size(); i++)
  1070. {
  1071. TargetDriverInfo DrvInfo;
  1072. if (GetDriverEbusId(fileList.GetKey(i), DrvInfo.m_RootBusId, DrvInfo.m_DriverBusId))
  1073. {
  1074. //get ebus driver path
  1075. if (GetDriverProcInfo(fileList.GetKey(i), DrvInfo.m_CcosProcInfo))
  1076. {
  1077. map<string,string>::iterator BusIdsIter = BusIdMap.find((const char*)DrvInfo.m_RootBusId);
  1078. if (BusIdsIter != BusIdMap.end())
  1079. {
  1080. //same busId Exist
  1081. if (BusIdsIter->second == (const char*)fileList[i])
  1082. {
  1083. //same dir,ok
  1084. }
  1085. else
  1086. {
  1087. //different dir,ng
  1088. ConflictBusIds[BusIdsIter->first] = 1;
  1089. }
  1090. }
  1091. else
  1092. {
  1093. //no BusId exist
  1094. BusIdMap[(const char*)DrvInfo.m_RootBusId] = (const char*)fileList[i];
  1095. }
  1096. LoadDriverMap[(const char *)DrvInfo.m_RootBusId].push_back(DrvInfo);
  1097. }
  1098. else
  1099. {
  1100. TPRINTA_INFO("GetDriverProcInfo failed %s ", fileList.GetKey(i));
  1101. }
  1102. }
  1103. else
  1104. {
  1105. TPRINTA_INFO("GetDriverEbusId failed %s ", fileList.GetKey(i));
  1106. }
  1107. }
  1108. map<string, vector<TargetDriverInfo>>::iterator DelIter;
  1109. //kick same busids drivers
  1110. map<string, int>::iterator ConflictIter = ConflictBusIds.begin();
  1111. while (ConflictIter != ConflictBusIds.end())
  1112. {
  1113. DelIter = LoadDriverMap.find(ConflictIter->first);
  1114. if (DelIter != LoadDriverMap.end())
  1115. {
  1116. TPRINTA_ERROR("busid:%s conflict", DelIter->first.c_str());
  1117. for (size_t DriverIdx = 0; DriverIdx < DelIter->second.size(); DriverIdx++)
  1118. {
  1119. const char *pszConfigFilepath = DelIter->second[DriverIdx].m_CcosProcInfo["DriverConfigPath"];//config file path
  1120. TPRINTA_ERROR("busid:%s conflict.Ignore ConfigFile:%s", DelIter->first.c_str(),pszConfigFilepath);
  1121. }
  1122. LoadDriverMap.erase(DelIter);
  1123. }
  1124. ++ConflictIter;
  1125. }
  1126. ConflictBusIds.clear();
  1127. TPRINTA_INFO("LoadDriverMap size %d ", LoadDriverMap.size());
  1128. //kick condition like (same busid but not same procType)
  1129. DelIter = LoadDriverMap.begin();
  1130. while (DelIter != LoadDriverMap.end())
  1131. {
  1132. if (DelIter->second.size() > 1)
  1133. {
  1134. bool ConflictProc = false;
  1135. //more than one driver exist
  1136. for (size_t DriverIdx = 0; DriverIdx < DelIter->second.size() - 1; DriverIdx++)
  1137. {
  1138. if (ConflictProc)
  1139. {
  1140. break;
  1141. }
  1142. //compare each others driverproc
  1143. string DriverProc = "";
  1144. ResDataObject DriverProcessPath;
  1145. GetDriverProcessPath(DelIter->second[DriverIdx].m_CcosProcInfo["DriverConfigPath"], DriverProcessPath);
  1146. DriverProc = (const char*)DriverProcessPath;
  1147. for (size_t CompareIdx = DriverIdx + 1; CompareIdx < DelIter->second.size(); CompareIdx++)
  1148. {
  1149. if (ConflictProc)
  1150. {
  1151. break;
  1152. }
  1153. string CompareProc = "";
  1154. ResDataObject CompareProcessPath;
  1155. GetDriverProcessPath(DelIter->second[CompareIdx].m_CcosProcInfo["DriverConfigPath"], CompareProcessPath);
  1156. CompareProc = (const char*)CompareProcessPath;
  1157. if (CompareProc != DriverProc)
  1158. {
  1159. ConflictBusIds[DelIter->first] = 1;
  1160. ConflictProc = true;
  1161. }
  1162. }
  1163. }
  1164. }
  1165. ++DelIter;
  1166. }
  1167. //kick again
  1168. ConflictIter = ConflictBusIds.begin();
  1169. while (ConflictIter != ConflictBusIds.end())
  1170. {
  1171. DelIter = LoadDriverMap.find(ConflictIter->first);
  1172. if (DelIter != LoadDriverMap.end())
  1173. {
  1174. TPRINTA_ERROR("busid:%s processType conflict", DelIter->first.c_str());
  1175. for (size_t DriverIdx = 0; DriverIdx < DelIter->second.size(); DriverIdx++)
  1176. {
  1177. const char* pszConfigFilepath = DelIter->second[DriverIdx].m_CcosProcInfo["DriverConfigPath"];//config file path
  1178. TPRINTA_ERROR("busid:%s processType conflict.Ignore ConfigFile:%s", DelIter->first.c_str(), pszConfigFilepath);
  1179. }
  1180. LoadDriverMap.erase(DelIter);
  1181. }
  1182. ++ConflictIter;
  1183. }
  1184. map<string, vector<TargetDriverInfo>>::iterator iter = LoadDriverMap.begin();
  1185. while (iter != LoadDriverMap.end())
  1186. {
  1187. if (ForReload)
  1188. {
  1189. if (CheckBusIdsExistance(iter->first.c_str()) == false)
  1190. {
  1191. //target exist
  1192. ++iter;
  1193. continue;
  1194. }
  1195. }
  1196. //make full process path
  1197. bool FirstIn = true;
  1198. string FullDriverPath = "";
  1199. for (size_t DriverIdx = 0; DriverIdx < iter->second.size(); DriverIdx++)
  1200. {
  1201. //make driverpath
  1202. if (FirstIn)
  1203. {
  1204. ResDataObject DriverProcessPath;
  1205. GetDriverProcessPath(iter->second[DriverIdx].m_CcosProcInfo["DriverConfigPath"], DriverProcessPath);
  1206. FullDriverPath = (const char *)DriverProcessPath;
  1207. FirstIn = false;
  1208. }
  1209. string ConfigFile = (const char *)iter->second[DriverIdx].m_CcosProcInfo["DriverConfigPath"];
  1210. //get config file title
  1211. string ConfigFileName = GetFileName(ConfigFile);
  1212. FullDriverPath += " " + ConfigFileName;
  1213. }
  1214. //rest all of same busid
  1215. for (size_t DriverIdx = 0; DriverIdx < iter->second.size(); DriverIdx++)
  1216. {
  1217. iter->second[DriverIdx].m_CcosProcInfo["DriverPath"] = FullDriverPath.c_str();//[fullpath of exe] [config1.xml config2.xml ...]
  1218. }
  1219. //create process
  1220. if (CreateTheProcess_Console_Config(iter->second[0].m_CcosProcInfo["DriverPath"],
  1221. iter->second[0].m_CcosProcInfo["WorkPath"],
  1222. iter->second[0].m_info,
  1223. iter->second[0].m_CcosProcInfo["ShowWindow"]) == FALSE)
  1224. {
  1225. TPRINTA_ERROR("Can't Create Process:%s", FullDriverPath.c_str());
  1226. }
  1227. else
  1228. {
  1229. TPRINTA_INFO("Create Process:%s ok", FullDriverPath.c_str());
  1230. for (size_t DriverIdx = 0; DriverIdx < iter->second.size(); DriverIdx++)
  1231. {
  1232. (*m_pProcessInfo)[(const char*)iter->second[DriverIdx].m_RootBusId].push_back(iter->second[DriverIdx]);
  1233. MakeDriverNotify((const char *)iter->second[DriverIdx].m_DriverBusId, true);
  1234. }
  1235. }
  1236. ++iter;
  1237. }
  1238. }
  1239. else
  1240. {
  1241. TPRINTA_ERROR("GetSpecificDriverConfigFiles failed");
  1242. }
  1243. }
  1244. catch (...)
  1245. {
  1246. TPRINTA_ERROR("crash");
  1247. ret = RET_FAILED;
  1248. }
  1249. //pDPC->Thread_UnLock();
  1250. //TPRINTA_INFO("DPC Unlock ok");
  1251. return ret;
  1252. }
  1253. void SYSTEM_CALL BusUnitLogic::UnloadDriver(const char *pszBusId)
  1254. {
  1255. TPRINTA_DEBUG("Enter UnloadDriver:%s", pszBusId);
  1256. map<string, vector<TargetDriverInfo>>::iterator iter = m_pProcessInfo->find(pszBusId);
  1257. if (iter != m_pProcessInfo->end())
  1258. {
  1259. //found target busid.
  1260. //notify target quit first
  1261. string DrvBusId = "/";
  1262. DrvBusId += (const char*)iter->first.c_str();
  1263. {
  1264. //got one
  1265. if (WaitForSingleObject((iter->second[0].m_info.hProcess), 0) == WAIT_TIMEOUT)
  1266. {
  1267. //process exist
  1268. LogicClient chClient1;
  1269. chClient1.SetRouter(CCOS_PACKET_ROUTE_LOCAL);
  1270. if (chClient1.Open(DrvBusId.c_str(), ALL_ACCESS, ONE_ACTION_TIMEOUT) >= RET_SUCCEED)
  1271. {
  1272. ResDataObject req;
  1273. if (chClient1.Action_Req("ExitDriverProc", req, ONE_ACTION_TIMEOUT) >= RET_SUCCEED)
  1274. {
  1275. printf("Succeed Action:ExitDriverProc %s ...\n", DrvBusId.c_str());
  1276. TPRINTA_DEBUG("Succeed Action: ExitDriverProc %s ...\n", DrvBusId.c_str());
  1277. chClient1.Close();
  1278. }
  1279. else
  1280. {
  1281. printf("Failed Action:ExitDriverProc -> %s...\n", DrvBusId.c_str());
  1282. TPRINTA_ERROR("Failed Action: ExitDriverProc -> %s ...\n", DrvBusId.c_str());
  1283. chClient1.Close();
  1284. }
  1285. }
  1286. else
  1287. {
  1288. printf("Open local %s Failed\n", DrvBusId.c_str());
  1289. TPRINTA_ERROR("Open local %s Failed\n", DrvBusId.c_str());
  1290. }
  1291. }
  1292. }
  1293. //wait exit
  1294. DWORD wait = WaitForMultipleObjects(1, &(iter->second[0].m_info.hProcess), TRUE, ONE_ACTION_TIMEOUT - 1000);
  1295. if (wait == WAIT_TIMEOUT)
  1296. {
  1297. TerminateProcess((iter->second[0].m_info.hProcess), 0);
  1298. printf("TerminateProcess %s ...\n", DrvBusId.c_str());
  1299. TPRINTA_DEBUG("TerminateProcess %s ...\n", DrvBusId.c_str());
  1300. }
  1301. //make notify
  1302. for (size_t DriverIdx = 0; DriverIdx < iter->second.size(); DriverIdx++)
  1303. {
  1304. MakeDriverNotify((const char *)iter->second[DriverIdx].m_DriverBusId, false);
  1305. }
  1306. //kick busid
  1307. m_pProcessInfo->erase(iter);
  1308. }
  1309. TPRINTA_DEBUG("Exit UnloadDriver:%s", pszBusId);
  1310. return;
  1311. }
  1312. void SYSTEM_CALL BusUnitLogic::UnloadAllRegistedDrivers()
  1313. {
  1314. map<string, vector<TargetDriverInfo>>::iterator iter = m_pProcessInfo->begin();
  1315. while (iter != m_pProcessInfo->end())
  1316. {
  1317. string DrvBusId = "/";
  1318. DrvBusId += (const char*)iter->first.c_str();
  1319. {
  1320. //got one
  1321. if (WaitForSingleObject((iter->second[0].m_info.hProcess), 0) == WAIT_TIMEOUT)
  1322. {
  1323. //process exist
  1324. LogicClient chClient1;
  1325. chClient1.SetRouter(CCOS_PACKET_ROUTE_LOCAL);
  1326. if (chClient1.Open(DrvBusId.c_str(), ALL_ACCESS, ONE_ACTION_TIMEOUT) >= RET_SUCCEED)
  1327. {
  1328. ResDataObject req;
  1329. if (chClient1.Action_Req("ExitDriverProc", req, ONE_ACTION_TIMEOUT) >= RET_SUCCEED)
  1330. {
  1331. printf("Succeed Action:ExitDriverProc %s ...\n", DrvBusId.c_str());
  1332. TPRINTA_DEBUG("Succeed Action: ExitDriverProc %s ...\n", DrvBusId.c_str());
  1333. chClient1.Close();
  1334. }
  1335. else
  1336. {
  1337. printf("Failed Action:ExitDriverProc -> %s...\n", DrvBusId.c_str());
  1338. TPRINTA_ERROR("Failed Action: ExitDriverProc -> %s ...\n", DrvBusId.c_str());
  1339. chClient1.Close();
  1340. }
  1341. }
  1342. else
  1343. {
  1344. printf("Open local %s Failed\n", DrvBusId.c_str());
  1345. TPRINTA_ERROR("Open local %s Failed\n", DrvBusId.c_str());
  1346. }
  1347. }
  1348. }
  1349. ++iter;
  1350. }
  1351. DWORD Idx = 0;
  1352. if (m_pProcessInfo->size() > 64)
  1353. {
  1354. printf("\n\n\n\nthe process number out of 64\n\n\n\n");
  1355. TPRINTA_FATAL("the process number out of 64");
  1356. map<string, vector<TargetDriverInfo>>::iterator iter = m_pProcessInfo->begin();
  1357. while (iter != m_pProcessInfo->end())
  1358. {
  1359. TerminateProcess((iter->second[0].m_info.hProcess), 0);
  1360. ++iter;
  1361. }
  1362. }
  1363. else
  1364. {
  1365. HANDLE *pAllHandles = new HANDLE[m_pProcessInfo->size()];
  1366. memset(pAllHandles, 0, sizeof(HANDLE)*m_pProcessInfo->size());
  1367. map<string, vector<TargetDriverInfo>>::iterator iter = m_pProcessInfo->begin();
  1368. while (iter != m_pProcessInfo->end())
  1369. {
  1370. pAllHandles[Idx] = (iter->second[0].m_info.hProcess);
  1371. ++iter;
  1372. }
  1373. DWORD wait = WaitForMultipleObjects((DWORD)m_pProcessInfo->size(), pAllHandles, TRUE, ONE_ACTION_TIMEOUT - 1000);
  1374. if (wait == WAIT_TIMEOUT)
  1375. {
  1376. map<string, vector<TargetDriverInfo>>::iterator iter = m_pProcessInfo->begin();
  1377. while (iter != m_pProcessInfo->end())
  1378. {
  1379. TerminateProcess((iter->second[0].m_info.hProcess), 0);
  1380. ++iter;
  1381. }
  1382. }
  1383. delete[]pAllHandles;
  1384. }
  1385. return;
  1386. }