BusUnitLogic.h 11 KB

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