BusUnitLogic (2).h 11 KB

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