BusUnitLogic.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531
  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. mutable std::mutex m_mutex;
  142. DeviceDescriptList()
  143. {
  144. SetKey("DeviceList");
  145. };
  146. ~DeviceDescriptList()
  147. {
  148. };
  149. void GetResDataObject(ResDataObject &obj)
  150. {
  151. std::lock_guard<std::mutex> lock(m_mutex);
  152. for (size_t i = 0; i < m_DeviceList.size(); i++)
  153. {
  154. ResDataObject unit;
  155. //NOT FINISHED YET
  156. //it needs get object method and needs get object context method
  157. m_DeviceList[i].GetResDataObject(unit);//the whole shit
  158. obj.add(m_DeviceList[i].GetKey(), (ResDataObject &)unit[0]);//get unit[keystring]...
  159. }
  160. };
  161. bool SetResDataObject(ResDataObject &obj)
  162. {
  163. std::lock_guard<std::mutex> lock(m_mutex);
  164. bool ret = true;
  165. try {
  166. m_DeviceList.clear();
  167. size_t total = obj.size();
  168. for (size_t i = 0; i < total; i++)
  169. {
  170. DeviceDescript descript;
  171. if (descript.SetResDataObject(obj[i]) == false)
  172. {
  173. ret = false;
  174. break;
  175. }
  176. }
  177. }
  178. catch (...)
  179. {
  180. ret = false;
  181. }
  182. return ret;
  183. };
  184. virtual const char *GetVal()
  185. {
  186. std::lock_guard<std::mutex> lock(m_mutex);
  187. ResDataObject obj;
  188. for (DWORD i = 0; i < m_DeviceList.size(); i++)
  189. {
  190. obj.add(m_DeviceList[i].GetKey(), m_DeviceList[i].GetVal());
  191. }
  192. (m_ValString) = obj.encode();
  193. return m_ValString.c_str();
  194. };
  195. virtual bool SetVal(const char* pValString)
  196. {
  197. std::lock_guard<std::mutex> lock(m_mutex);
  198. bool ret = true;
  199. ResDataObject obj;
  200. m_DeviceList.clear();
  201. try {
  202. obj.decode(pValString);
  203. for (size_t i = 0; i < obj.size(); i++)
  204. {
  205. DeviceDescript dd;
  206. dd.SetVal(obj[i].encode());
  207. m_DeviceList.push_back(dd);
  208. }
  209. }
  210. catch (...)
  211. {
  212. ret = false;
  213. }
  214. return ret;
  215. };
  216. virtual bool AddVal(const char* pValString)
  217. {
  218. std::lock_guard<std::mutex> lock(m_mutex);
  219. try {
  220. DeviceDescript dd;
  221. if (dd.SetVal(pValString))
  222. {
  223. m_DeviceList.push_back(dd);
  224. return true;
  225. }
  226. }
  227. catch (...)
  228. {
  229. }
  230. return true;
  231. };
  232. virtual bool DelVal(const char* pValString)
  233. {
  234. std::lock_guard<std::mutex> lock(m_mutex);
  235. //key is the path
  236. vector<DeviceDescript>::iterator iter = m_DeviceList.begin();
  237. while (iter != m_DeviceList.end())
  238. {
  239. if (string(iter->GetKey()) == string(pValString))
  240. {
  241. iter = m_DeviceList.erase(iter);
  242. continue;
  243. }
  244. ++iter;
  245. }
  246. return true;
  247. };
  248. bool DelValEx(const char* pValString, DeviceDescript &ddd)
  249. {
  250. std::lock_guard<std::mutex> lock(m_mutex);
  251. //key is the path
  252. vector<DeviceDescript>::iterator iter = m_DeviceList.begin();
  253. while (iter != m_DeviceList.end())
  254. {
  255. if (string(iter->GetKey()) == string(pValString))
  256. {
  257. ddd = (*iter);
  258. iter = m_DeviceList.erase(iter);
  259. return true;
  260. }
  261. ++iter;
  262. }
  263. return false;
  264. };
  265. bool AddDriver(DeviceDescript &dd)
  266. {
  267. std::lock_guard<std::mutex> lock(m_mutex);
  268. bool ret = true;
  269. try {
  270. for (size_t i = 0; i < m_DeviceList.size(); i++)
  271. {
  272. if (string((m_DeviceList[i].GetKey())) == string(dd.GetKey()))
  273. {
  274. //如果存在,则更新
  275. m_DeviceList[i] = dd;
  276. return true;
  277. }
  278. }
  279. m_DeviceList.push_back(dd);
  280. }
  281. catch (...)
  282. {
  283. ret = false;
  284. }
  285. return ret;
  286. };
  287. bool DelDriver(const char* pPath)
  288. {
  289. std::lock_guard<std::mutex> lock(m_mutex);
  290. //key is the path
  291. std::string targetPath = pPath;
  292. vector<DeviceDescript>::iterator iter = m_DeviceList.begin();
  293. while (iter != m_DeviceList.end())
  294. {
  295. // 直接比较路径字符串,区分大小写
  296. if (iter->GetKey() == targetPath)
  297. {
  298. iter = m_DeviceList.erase(iter);
  299. continue;
  300. }
  301. ++iter;
  302. }
  303. return true;
  304. };
  305. };
  306. class DriverDescriptList : public BaseJsonDataObject<bool>
  307. {
  308. public:
  309. vector<string> m_DriverList;
  310. DriverDescriptList()
  311. {
  312. //SetKey("DeviceList");
  313. };
  314. ~DriverDescriptList()
  315. {
  316. };
  317. bool IsDriverExist(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 true;
  324. }
  325. return false;
  326. };
  327. bool AddDriver(const char *pszDrvPath)
  328. {
  329. vector<string>::iterator it = find(m_DriverList.begin(), m_DriverList.end(), pszDrvPath);
  330. if (it != m_DriverList.end())
  331. {
  332. //already exist
  333. return false;
  334. }
  335. m_DriverList.push_back(pszDrvPath);
  336. return true;
  337. };
  338. bool DelDriver(const char* pPath)
  339. {
  340. vector<string>::iterator it = find(m_DriverList.begin(), m_DriverList.end(), pPath);
  341. if (it != m_DriverList.end())
  342. {
  343. m_DriverList.erase(it);
  344. return true;
  345. }
  346. return false;
  347. };
  348. void GetResDataObject(ResDataObject &obj)
  349. {
  350. for (size_t i = 0; i < m_DriverList.size(); i++)
  351. {
  352. obj.add(m_DriverList[i].c_str(), "");
  353. }
  354. };
  355. };
  356. class DriverConfigManager;
  357. // 此类是从 BusUnitLogic.dll 导出的
  358. class BUSUNITLOGIC_API BusUnitLogic : public LogicDevice
  359. {
  360. bool m_bConfigRemoveDriver;
  361. DriverConfigManager* m_DriverConfigMange;
  362. protected://for internal use
  363. BaseJsonDataObject<string> *m_pbusID;
  364. BaseJsonDataObject<string> *m_pMachineID;
  365. BaseJsonDataObject<UINT64> *m_pProcID;
  366. BaseJsonDataObject<int> *m_pState;
  367. BaseJsonDataObject<int> *m_pExitFlag;
  368. BaseJsonDataObject<int>* m_pGrpcPort;
  369. DeviceDescriptList *m_DevList; //此BusID上挂载的设备描述列表
  370. DeviceDescriptList* m_pCcosDevList;// CCOS标准路径设备
  371. BaseJsonDataObject<int> *m_pEnableEthBus;
  372. BaseJsonDataObject<string> *m_pEthBusRouterIp;
  373. BaseJsonDataObject<bool> *m_pForTest;
  374. DriverDescriptList *m_pFullDriverList;
  375. DriverDescriptList *m_pConfigDriverList;
  376. map<string, vector<TargetDriverInfo>> *m_pProcessInfo;
  377. bool SYSTEM_CALL CheckAndKillLiveDriver(const char *pszDriverpath, bool bRemoveDriver = true);
  378. void SYSTEM_CALL UnloadDriver(const char *pszBusId);
  379. public:
  380. BusUnitLogic(void);
  381. virtual ~BusUnitLogic(void);
  382. //get device type
  383. virtual bool SYSTEM_CALL GetDeviceType(GUID &DevType);
  384. //get device resource
  385. virtual RET_STATUS SYSTEM_CALL GetDeviceResource(ResDataObject PARAM_OUT *pDeviceResource);
  386. //ResourceCommand Request In and Response Out
  387. virtual RET_STATUS SYSTEM_CALL Request(ResDataObject PARAM_IN *pRequest, ResDataObject PARAM_OUT *pResponse);
  388. //notify to lower layer
  389. virtual RET_STATUS SYSTEM_CALL CmdToLogicDev(ResDataObject PARAM_IN *pCmd);
  390. //errors,warnings
  391. void SetErrorInfo(int errCode, char *pErrInfo);
  392. void SetWarningInfo(int warningCode, char *pWarningInfo);
  393. //Data Access
  394. virtual int DATA_ACTION Get(const char PARAM_IN *pKey, ResDataObject &Res);
  395. 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);
  396. virtual int DATA_ACTION DelDeviceDescrpt(const char PARAM_IN *pDevPath);
  397. virtual int DATA_ACTION ExitDriverProc();
  398. int SYSTEM_CALL GetExitFlag();
  399. int SYSTEM_CALL SetExitFlag(int ExitFlag);
  400. virtual int DATA_ACTION SetDeviceStatus(int Status);//设置设备状态
  401. int SYSTEM_CALL GetDeviceStatus();
  402. virtual int DATA_ACTION SetEthBusSwitch(int Switch);//设置网络EBUS开关
  403. int SYSTEM_CALL GetEthBusSwitch();
  404. virtual int DATA_ACTION SetEthBusRouterIp(const char PARAM_IN *pRouterIp);//设置网络EBUS的RouterIp
  405. int SYSTEM_CALL GetEthBusRouterIp(ResDataObject &obj);
  406. virtual int DATA_ACTION ForTest(bool Flag);
  407. virtual int DATA_ACTION ConfigLoadDriver(const char *pszDriverpath,char *pszFixDriverpath,DWORD FixDrvLen, ResDataObject resValue, ResDataObject &resConfig, ResDataObject &resCcosProcConf);
  408. virtual int DATA_ACTION ConfigRemoveDriver(const char *pszDriverpath);
  409. bool SYSTEM_CALL CheckBusIdsExistance(const char *pszBusId);
  410. bool SYSTEM_CALL LoadAllConfigDrivers();
  411. int DATA_ACTION MakeDriverNotify(const char *pszDriverpath, bool Add);
  412. //Data Access of internal
  413. int SYSTEM_CALL GetbusId(ResDataObject &obj);//
  414. int SYSTEM_CALL GetMachineId(ResDataObject &obj);//
  415. int SYSTEM_CALL GetProcId(UINT64 &obj);//
  416. int SYSTEM_CALL SetbusId(ResDataObject &obj);//
  417. int SYSTEM_CALL SetMachineId(ResDataObject &obj);//
  418. int SYSTEM_CALL SetProcId(UINT64 obj);//
  419. DWORD SYSTEM_CALL GetDeviceCount();//
  420. bool SYSTEM_CALL GetDeviceDescript(DWORD Idx, ResDataObject &DevPath, ResDataObject &DevType, ResDataObject &MachineId, UINT64 &ProcId, UINT64 &Addr);//
  421. //Actions
  422. int LoadAllConfigDriver(bool ForReload = false, ResDataObject* pResource = nullptr);
  423. void UnloadAllRegistedDrivers();
  424. void CheckAllLiveDriver();
  425. void SubscribeSelf() override;
  426. void OnSetClientID() override;
  427. virtual RET_STATUS ProcessRequest(ResDataObject* pCmd, PACKET_CMD cmd) override;
  428. };