BusUnitLogic.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508
  1. #pragma once
  2. #define BUSUNITLOGIC_API
  3. #include <algorithm>
  4. #include "LogicDevice.h"
  5. #define ONE_ACTION_TIMEOUT (5000)
  6. class TargetDriverInfo
  7. {
  8. public:
  9. PROCESS_INFORMATION m_info;
  10. ResDataObject m_RootBusId;
  11. ResDataObject m_DriverBusId;
  12. ResDataObject m_CcosProcInfo;
  13. public:
  14. TargetDriverInfo()
  15. {
  16. memset(&m_info, 0, sizeof(PROCESS_INFORMATION));
  17. };
  18. ~TargetDriverInfo()
  19. {
  20. };
  21. };
  22. class DeviceDescript : public BaseJsonDataObject<bool>
  23. {
  24. public:
  25. BaseJsonDataObject<string> m_TargetType;
  26. BaseJsonDataObject<string> m_MachineId;
  27. BaseJsonDataObject<UINT64> m_ProcId;
  28. BaseJsonDataObject<UINT64> m_Address;
  29. DeviceDescript()
  30. {
  31. SetKey("");
  32. m_TargetType.SetKey("TargetType");
  33. m_TargetType = "";
  34. m_MachineId.SetKey("MachineId");
  35. m_MachineId = "";
  36. m_ProcId.SetKey("ProcId");
  37. m_ProcId = 0;
  38. m_Address.SetKey("Addr");
  39. m_Address = 0;
  40. };
  41. ~DeviceDescript()
  42. {
  43. };
  44. virtual void GetResDataObject(ResDataObject &obj)
  45. {
  46. ResDataObject Temp;
  47. Temp.add(m_TargetType.GetKey(), m_TargetType.GetVal());
  48. Temp.add(m_MachineId.GetKey(), m_MachineId.GetVal());
  49. Temp.add(m_ProcId.GetKey(), m_ProcId.GetVal());
  50. Temp.add(m_Address.GetKey(), m_Address.GetVal());
  51. obj.add(GetKey(), Temp);
  52. };
  53. virtual bool SetResDataObject(ResDataObject &obj)
  54. {
  55. bool ret = true;
  56. try {
  57. const char *pKey = obj.GetKey(0);
  58. SetKey(pKey);
  59. m_ProcId = obj[0][m_ProcId.GetKey()];
  60. m_TargetType = (const char*)obj[0][m_TargetType.GetKey()];
  61. m_MachineId = (const char*)obj[0][m_MachineId.GetKey()];
  62. m_Address = obj[0][m_Address.GetKey()];
  63. }
  64. catch (...)
  65. {
  66. ret = false;
  67. }
  68. return ret;
  69. };
  70. virtual const char *GetVal()
  71. {
  72. ResDataObject obj;
  73. obj.add(m_ProcId.GetKey(), m_ProcId.GetVal());
  74. obj.add(m_MachineId.GetKey(), m_MachineId.GetVal());
  75. obj.add(m_TargetType.GetKey(), m_TargetType.GetVal());
  76. obj.add(m_Address.GetKey(), m_Address.GetVal());
  77. (m_ValString) = obj.encode();
  78. return m_ValString.c_str();
  79. };
  80. virtual bool SetVal(const char* pValString)
  81. {
  82. bool ret = true;
  83. ResDataObject obj;
  84. if (obj.decode(pValString))
  85. {
  86. int idx;
  87. idx = obj.GetFirstOf(m_TargetType.GetKey());
  88. if (idx >= 0)
  89. {
  90. m_TargetType.SetVal(obj[idx]);
  91. }
  92. else
  93. {
  94. ret = false;
  95. }
  96. idx = obj.GetFirstOf(m_MachineId.GetKey());
  97. if (idx >= 0)
  98. {
  99. m_MachineId.SetVal(obj[idx]);
  100. }
  101. else
  102. {
  103. ret = false;
  104. }
  105. idx = obj.GetFirstOf(m_ProcId.GetKey());
  106. if (idx >= 0)
  107. {
  108. m_ProcId.SetVal(obj[idx]);
  109. }
  110. else
  111. {
  112. ret = false;
  113. }
  114. idx = obj.GetFirstOf(m_Address.GetKey());
  115. if (idx >= 0)
  116. {
  117. m_Address.SetVal(obj[idx]);
  118. }
  119. else
  120. {
  121. ret = false;
  122. }
  123. }
  124. else
  125. {
  126. ret = false;
  127. }
  128. return ret;
  129. };
  130. };
  131. class DeviceDescriptList : public BaseJsonDataObject<bool>
  132. {
  133. public:
  134. vector<DeviceDescript> m_DeviceList;
  135. DeviceDescriptList()
  136. {
  137. SetKey("DeviceList");
  138. };
  139. ~DeviceDescriptList()
  140. {
  141. };
  142. void GetResDataObject(ResDataObject &obj)
  143. {
  144. for (size_t i = 0; i < m_DeviceList.size(); i++)
  145. {
  146. ResDataObject unit;
  147. //NOT FINISHED YET
  148. //it needs get object method and needs get object context method
  149. m_DeviceList[i].GetResDataObject(unit);//the whole shit
  150. obj.add(m_DeviceList[i].GetKey(), (ResDataObject &)unit[0]);//get unit[keystring]...
  151. }
  152. };
  153. bool SetResDataObject(ResDataObject &obj)
  154. {
  155. bool ret = true;
  156. try {
  157. m_DeviceList.clear();
  158. size_t total = obj.size();
  159. for (size_t i = 0; i < total; i++)
  160. {
  161. DeviceDescript descript;
  162. if (descript.SetResDataObject(obj[i]) == false)
  163. {
  164. ret = false;
  165. break;
  166. }
  167. }
  168. }
  169. catch (...)
  170. {
  171. ret = false;
  172. }
  173. return ret;
  174. };
  175. virtual const char *GetVal()
  176. {
  177. ResDataObject obj;
  178. for (DWORD i = 0; i < m_DeviceList.size(); i++)
  179. {
  180. obj.add(m_DeviceList[i].GetKey(), m_DeviceList[i].GetVal());
  181. }
  182. (m_ValString) = obj.encode();
  183. return m_ValString.c_str();
  184. };
  185. virtual bool SetVal(const char* pValString)
  186. {
  187. bool ret = true;
  188. ResDataObject obj;
  189. m_DeviceList.clear();
  190. try {
  191. obj.decode(pValString);
  192. for (size_t i = 0; i < obj.size(); i++)
  193. {
  194. DeviceDescript dd;
  195. dd.SetVal(obj[i].encode());
  196. m_DeviceList.push_back(dd);
  197. }
  198. }
  199. catch (...)
  200. {
  201. ret = false;
  202. }
  203. return ret;
  204. };
  205. virtual bool AddVal(const char* pValString)
  206. {
  207. try {
  208. DeviceDescript dd;
  209. if (dd.SetVal(pValString))
  210. {
  211. m_DeviceList.push_back(dd);
  212. return true;
  213. }
  214. }
  215. catch (...)
  216. {
  217. }
  218. return true;
  219. };
  220. virtual bool DelVal(const char* pValString)
  221. {
  222. //key is the path
  223. vector<DeviceDescript>::iterator iter = m_DeviceList.begin();
  224. while (iter != m_DeviceList.end())
  225. {
  226. if (string(iter->GetKey()) == string(pValString))
  227. {
  228. iter = m_DeviceList.erase(iter);
  229. continue;
  230. }
  231. ++iter;
  232. }
  233. return true;
  234. };
  235. bool DelValEx(const char* pValString, DeviceDescript &ddd)
  236. {
  237. //key is the path
  238. vector<DeviceDescript>::iterator iter = m_DeviceList.begin();
  239. while (iter != m_DeviceList.end())
  240. {
  241. if (string(iter->GetKey()) == string(pValString))
  242. {
  243. ddd = (*iter);
  244. iter = m_DeviceList.erase(iter);
  245. return true;
  246. }
  247. ++iter;
  248. }
  249. return false;
  250. };
  251. bool AddDriver(DeviceDescript &dd)
  252. {
  253. bool ret = true;
  254. try {
  255. for (size_t i = 0; i < m_DeviceList.size(); i++)
  256. {
  257. if (string((m_DeviceList[i].GetKey())) == string(dd.GetKey()))
  258. {
  259. return false;
  260. }
  261. }
  262. m_DeviceList.push_back(dd);
  263. }
  264. catch (...)
  265. {
  266. ret = false;
  267. }
  268. return ret;
  269. };
  270. bool DelDriver(const char* pPath)
  271. {
  272. //key is the path
  273. vector<DeviceDescript>::iterator iter = m_DeviceList.begin();
  274. while (iter != m_DeviceList.end())
  275. {
  276. std::string existfileLow = (iter->GetKey());
  277. std::string filenameLow = pPath;
  278. transform(filenameLow.begin(), filenameLow.end(), filenameLow.begin(), tolower);
  279. transform(existfileLow.begin(), existfileLow.end(), existfileLow.begin(), tolower);
  280. if (existfileLow == filenameLow)
  281. {
  282. iter = m_DeviceList.erase(iter);
  283. continue;
  284. }
  285. ++iter;
  286. }
  287. return true;
  288. };
  289. };
  290. class DriverDescriptList : public BaseJsonDataObject<bool>
  291. {
  292. public:
  293. vector<string> m_DriverList;
  294. DriverDescriptList()
  295. {
  296. //SetKey("DeviceList");
  297. };
  298. ~DriverDescriptList()
  299. {
  300. };
  301. bool IsDriverExist(const char *pszDrvPath)
  302. {
  303. vector<string>::iterator it = find(m_DriverList.begin(), m_DriverList.end(), pszDrvPath);
  304. if (it != m_DriverList.end())
  305. {
  306. //already exist
  307. return true;
  308. }
  309. return false;
  310. };
  311. bool AddDriver(const char *pszDrvPath)
  312. {
  313. vector<string>::iterator it = find(m_DriverList.begin(), m_DriverList.end(), pszDrvPath);
  314. if (it != m_DriverList.end())
  315. {
  316. //already exist
  317. return false;
  318. }
  319. m_DriverList.push_back(pszDrvPath);
  320. return true;
  321. };
  322. bool DelDriver(const char* pPath)
  323. {
  324. vector<string>::iterator it = find(m_DriverList.begin(), m_DriverList.end(), pPath);
  325. if (it != m_DriverList.end())
  326. {
  327. m_DriverList.erase(it);
  328. return true;
  329. }
  330. return false;
  331. };
  332. void GetResDataObject(ResDataObject &obj)
  333. {
  334. for (size_t i = 0; i < m_DriverList.size(); i++)
  335. {
  336. obj.add(m_DriverList[i].c_str(), "");
  337. }
  338. };
  339. };
  340. // 此类是从 BusUnitLogic.dll 导出的
  341. class BUSUNITLOGIC_API BusUnitLogic : public LogicDevice
  342. {
  343. bool m_bConfigRemoveDriver;
  344. protected://for internal use
  345. BaseJsonDataObject<string> *m_pbusID;
  346. BaseJsonDataObject<string> *m_pMachineID;
  347. BaseJsonDataObject<UINT64> *m_pProcID;
  348. BaseJsonDataObject<int> *m_pState;
  349. BaseJsonDataObject<int> *m_pExitFlag;
  350. DeviceDescriptList *m_DevList; //此BusID上挂载的设备描述列表
  351. BaseJsonDataObject<int> *m_pEnableEthBus;
  352. BaseJsonDataObject<string> *m_pEthBusRouterIp;
  353. BaseJsonDataObject<bool> *m_pForTest;
  354. DriverDescriptList *m_pFullDriverList;
  355. DriverDescriptList *m_pConfigDriverList;
  356. map<string, vector<TargetDriverInfo>> *m_pProcessInfo;
  357. bool SYSTEM_CALL CheckAndKillLiveDriver(const char *pszDriverpath);
  358. void SYSTEM_CALL UnloadDriver(const char *pszBusId);
  359. public:
  360. BusUnitLogic(void);
  361. virtual ~BusUnitLogic(void);
  362. //get device type
  363. virtual bool SYSTEM_CALL GetDeviceType(GUID &DevType);
  364. //get device resource
  365. virtual RET_STATUS SYSTEM_CALL GetDeviceResource(ResDataObject PARAM_OUT *pDeviceResource);
  366. //ResourceCommand Request In and Response Out
  367. virtual RET_STATUS SYSTEM_CALL Request(ResDataObject PARAM_IN *pRequest, ResDataObject PARAM_OUT *pResponse);
  368. //notify to lower layer
  369. virtual RET_STATUS SYSTEM_CALL CmdToLogicDev(ResDataObject PARAM_IN *pCmd);
  370. //errors,warnings
  371. void SetErrorInfo(int errCode, char *pErrInfo);
  372. void SetWarningInfo(int warningCode, char *pWarningInfo);
  373. //Data Access
  374. virtual int DATA_ACTION Get(const char PARAM_IN *pKey, ResDataObject &Res);
  375. virtual int DATA_ACTION AddDeviceDescrpt(const char PARAM_IN *pDevPath,const char PARAM_IN *pTargetType, const char PARAM_IN *pMachineId, UINT64 ProcId, UINT64 Addr,bool forceAdd = false);
  376. virtual int DATA_ACTION DelDeviceDescrpt(const char PARAM_IN *pDevPath);
  377. virtual int DATA_ACTION ExitDriverProc();
  378. int SYSTEM_CALL GetExitFlag();
  379. int SYSTEM_CALL SetExitFlag(int ExitFlag);
  380. virtual int DATA_ACTION SetDeviceStatus(int Status);//设置设备状态
  381. int SYSTEM_CALL GetDeviceStatus();
  382. virtual int DATA_ACTION SetEthBusSwitch(int Switch);//设置网络EBUS开关
  383. int SYSTEM_CALL GetEthBusSwitch();
  384. virtual int DATA_ACTION SetEthBusRouterIp(const char PARAM_IN *pRouterIp);//设置网络EBUS的RouterIp
  385. int SYSTEM_CALL GetEthBusRouterIp(ResDataObject &obj);
  386. virtual int DATA_ACTION ForTest(bool Flag);
  387. virtual int DATA_ACTION ConfigLoadDriver(const char *pszDriverpath,char *pszFixDriverpath,DWORD FixDrvLen);
  388. virtual int DATA_ACTION ConfigRemoveDriver(const char *pszDriverpath);
  389. bool SYSTEM_CALL CheckBusIdsExistance(const char *pszBusId);
  390. bool SYSTEM_CALL LoadAllConfigDrivers();
  391. int DATA_ACTION MakeDriverNotify(const char *pszDriverpath, bool Add);
  392. //Data Access of internal
  393. int SYSTEM_CALL GetbusId(ResDataObject &obj);//
  394. int SYSTEM_CALL GetMachineId(ResDataObject &obj);//
  395. int SYSTEM_CALL GetProcId(UINT64 &obj);//
  396. int SYSTEM_CALL SetbusId(ResDataObject &obj);//
  397. int SYSTEM_CALL SetMachineId(ResDataObject &obj);//
  398. int SYSTEM_CALL SetProcId(UINT64 obj);//
  399. DWORD SYSTEM_CALL GetDeviceCount();//
  400. bool SYSTEM_CALL GetDeviceDescript(DWORD Idx, ResDataObject &DevPath, ResDataObject &DevType, ResDataObject &MachineId, UINT64 &ProcId, UINT64 &Addr);//
  401. //Actions
  402. int LoadAllConfigDriver(bool ForReload = false);
  403. void UnloadAllRegistedDrivers();
  404. void CheckAllLiveDriver();
  405. };