BusUnitLogic.h 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407
  1. #pragma once
  2. // 下列 ifdef 块是创建使从 DLL 导出更简单的
  3. // 宏的标准方法。此 DLL 中的所有文件都是用命令行上定义的 BUSUNITLOGIC_EXPORTS
  4. // 符号编译的。在使用此 DLL 的
  5. // 任何其他项目上不应定义此符号。这样,源文件中包含此文件的任何其他项目都会将
  6. // BUSUNITLOGIC_API 函数视为是从 DLL 导入的,而此 DLL 则将用此宏定义的
  7. // 符号视为是被导出的。
  8. #ifndef BUSUNITLOGIC_EXPORTS
  9. #ifdef _WIN64
  10. #ifdef _DEBUG
  11. #pragma comment(lib, "BusUnitLogicX64D.lib")
  12. #else
  13. #pragma comment(lib, "BusUnitLogicX64.lib")
  14. #endif
  15. #else
  16. #ifdef _DEBUG
  17. #pragma comment(lib, "BusUnitLogicD.lib")
  18. #else
  19. #pragma comment(lib, "BusUnitLogic.lib")
  20. #endif
  21. #endif
  22. #endif
  23. #ifdef BUSUNITLOGIC_EXPORTS
  24. #define BUSUNITLOGIC_API __declspec(dllexport)
  25. #else
  26. #define BUSUNITLOGIC_API __declspec(dllimport)
  27. #endif
  28. #include "LogicDevice.h"
  29. class DeviceDescript : public BaseJsonDataObject<bool>
  30. {
  31. public:
  32. BaseJsonDataObject<string> m_MachineId;
  33. BaseJsonDataObject<UINT64> m_ProcId;
  34. BaseJsonDataObject<UINT64> m_Address;
  35. DeviceDescript()
  36. {
  37. SetKey("");
  38. m_MachineId.SetKey("MachineId");
  39. m_MachineId = "";
  40. m_ProcId.SetKey("ProcId");
  41. m_ProcId = 0;
  42. m_Address.SetKey("Addr");
  43. m_Address = 0;
  44. };
  45. ~DeviceDescript()
  46. {
  47. };
  48. virtual void GetResDataObject(ResDataObject &obj)
  49. {
  50. ResDataObject Temp;
  51. Temp.add(m_MachineId.GetKey(), m_MachineId.GetVal());
  52. Temp.add(m_ProcId.GetKey(), m_ProcId.GetVal());
  53. Temp.add(m_Address.GetKey(), m_Address.GetVal());
  54. obj.add(GetKey(), Temp);
  55. };
  56. virtual bool SetResDataObject(ResDataObject &obj)
  57. {
  58. bool ret = true;
  59. try {
  60. const char *pKey = obj.GetKey(0);
  61. SetKey(pKey);
  62. m_ProcId = obj[0][m_ProcId.GetKey()];
  63. m_MachineId = (const char*)obj[0][m_MachineId.GetKey()];
  64. m_Address = obj[0][m_Address.GetKey()];
  65. }
  66. catch (...)
  67. {
  68. ret = false;
  69. }
  70. return ret;
  71. };
  72. virtual const char *GetVal()
  73. {
  74. ResDataObject obj;
  75. obj.add(m_ProcId.GetKey(), m_ProcId.GetVal());
  76. obj.add(m_MachineId.GetKey(), m_MachineId.GetVal());
  77. obj.add(m_Address.GetKey(), m_Address.GetVal());
  78. (m_ValString) = obj.encode();
  79. return m_ValString.c_str();
  80. };
  81. virtual bool SetVal(const char* pValString)
  82. {
  83. bool ret = true;
  84. ResDataObject obj;
  85. if (obj.decode(pValString))
  86. {
  87. int idx;
  88. idx = obj.GetFirstOf(m_MachineId.GetKey());
  89. if (idx >= 0)
  90. {
  91. m_MachineId.SetVal(obj[idx]);
  92. }
  93. else
  94. {
  95. ret = false;
  96. }
  97. idx = obj.GetFirstOf(m_ProcId.GetKey());
  98. if (idx >= 0)
  99. {
  100. m_ProcId.SetVal(obj[idx]);
  101. }
  102. else
  103. {
  104. ret = false;
  105. }
  106. idx = obj.GetFirstOf(m_Address.GetKey());
  107. if (idx >= 0)
  108. {
  109. m_Address.SetVal(obj[idx]);
  110. }
  111. else
  112. {
  113. ret = false;
  114. }
  115. }
  116. else
  117. {
  118. ret = false;
  119. }
  120. return ret;
  121. };
  122. };
  123. class DeviceDescriptList : public BaseJsonDataObject<bool>
  124. {
  125. public:
  126. vector<DeviceDescript> m_DeviceList;
  127. DeviceDescriptList()
  128. {
  129. SetKey("DeviceList");
  130. };
  131. ~DeviceDescriptList()
  132. {
  133. };
  134. void GetResDataObject(ResDataObject &obj)
  135. {
  136. for (size_t i = 0; i < m_DeviceList.size(); i++)
  137. {
  138. ResDataObject unit;
  139. //NOT FINISHED YET
  140. //it needs get object method and needs get object context method
  141. m_DeviceList[i].GetResDataObject(unit);//the whole shit
  142. obj.add(m_DeviceList[i].GetKey(), (ResDataObject &)unit[0]);//get unit[keystring]...
  143. }
  144. };
  145. bool SetResDataObject(ResDataObject &obj)
  146. {
  147. bool ret = true;
  148. try {
  149. m_DeviceList.clear();
  150. size_t total = obj.size();
  151. for (size_t i = 0; i < total; i++)
  152. {
  153. DeviceDescript descript;
  154. if (descript.SetResDataObject(obj[i]) == false)
  155. {
  156. ret = false;
  157. break;
  158. }
  159. }
  160. }
  161. catch (...)
  162. {
  163. ret = false;
  164. }
  165. return ret;
  166. };
  167. virtual const char *GetVal()
  168. {
  169. ResDataObject obj;
  170. for (DWORD i = 0; i < m_DeviceList.size(); i++)
  171. {
  172. obj.add(m_DeviceList[i].GetKey(), m_DeviceList[i].GetVal());
  173. }
  174. (m_ValString) = obj.encode();
  175. return m_ValString.c_str();
  176. };
  177. virtual bool SetVal(const char* pValString)
  178. {
  179. bool ret = true;
  180. ResDataObject obj;
  181. m_DeviceList.clear();
  182. try {
  183. obj.decode(pValString);
  184. for (size_t i = 0; i < obj.size(); i++)
  185. {
  186. DeviceDescript dd;
  187. dd.SetVal(obj[i].encode());
  188. m_DeviceList.push_back(dd);
  189. }
  190. }
  191. catch (...)
  192. {
  193. ret = false;
  194. }
  195. return ret;
  196. };
  197. virtual bool AddVal(const char* pValString)
  198. {
  199. try {
  200. DeviceDescript dd;
  201. if (dd.SetVal(pValString))
  202. {
  203. m_DeviceList.push_back(dd);
  204. return true;
  205. }
  206. }
  207. catch (...)
  208. {
  209. }
  210. return true;
  211. };
  212. virtual bool DelVal(const char* pValString)
  213. {
  214. //key is the path
  215. vector<DeviceDescript>::iterator iter = m_DeviceList.begin();
  216. while (iter != m_DeviceList.end())
  217. {
  218. if (string(iter->GetKey()) == string(pValString))
  219. {
  220. iter = m_DeviceList.erase(iter);
  221. continue;
  222. }
  223. ++iter;
  224. }
  225. return true;
  226. };
  227. bool DelValEx(const char* pValString, DeviceDescript &ddd)
  228. {
  229. //key is the path
  230. vector<DeviceDescript>::iterator iter = m_DeviceList.begin();
  231. while (iter != m_DeviceList.end())
  232. {
  233. if (string(iter->GetKey()) == string(pValString))
  234. {
  235. ddd = (*iter);
  236. iter = m_DeviceList.erase(iter);
  237. return true;
  238. }
  239. ++iter;
  240. }
  241. return false;
  242. };
  243. bool AddDriver(DeviceDescript &dd)
  244. {
  245. bool ret = true;
  246. try {
  247. for (size_t i = 0; i < m_DeviceList.size(); i++)
  248. {
  249. if (string((m_DeviceList[i].GetKey())) == string(dd.GetKey()))
  250. {
  251. return false;
  252. }
  253. }
  254. m_DeviceList.push_back(dd);
  255. }
  256. catch (...)
  257. {
  258. ret = false;
  259. }
  260. return ret;
  261. };
  262. bool DelDriver(const char* pPath)
  263. {
  264. //key is the path
  265. vector<DeviceDescript>::iterator iter = m_DeviceList.begin();
  266. while (iter != m_DeviceList.end())
  267. {
  268. std::string existfileLow = (iter->GetKey());
  269. std::string filenameLow = pPath;
  270. transform(filenameLow.begin(), filenameLow.end(), filenameLow.begin(), tolower);
  271. transform(existfileLow.begin(), existfileLow.end(), existfileLow.begin(), tolower);
  272. if (existfileLow == filenameLow)
  273. {
  274. iter = m_DeviceList.erase(iter);
  275. continue;
  276. }
  277. ++iter;
  278. }
  279. return true;
  280. };
  281. };
  282. // 此类是从 BusUnitLogic.dll 导出的
  283. class BUSUNITLOGIC_API BusUnitLogic : public LogicDevice
  284. {
  285. protected://for internal use
  286. BaseJsonDataObject<string> *m_pbusID;
  287. BaseJsonDataObject<string> *m_pMachineID;
  288. BaseJsonDataObject<UINT64> *m_pProcID;
  289. BaseJsonDataObject<int> *m_pState;
  290. BaseJsonDataObject<int> *m_pExitFlag;
  291. DeviceDescriptList *m_DevList;
  292. BaseJsonDataObject<int> *m_pEnableEthBus;
  293. BaseJsonDataObject<string> *m_pEthBusRouterIp;
  294. BaseJsonDataObject<bool> *m_pForTest;
  295. public:
  296. BusUnitLogic(void);
  297. virtual ~BusUnitLogic(void);
  298. //get device type
  299. virtual bool SYSTEM_CALL GetDeviceType(GUID &DevType);
  300. //get device resource
  301. virtual RET_STATUS SYSTEM_CALL GetDeviceResource(ResDataObject PARAM_OUT *pDeviceResource);
  302. //ResourceCommand Request In and Response Out
  303. virtual RET_STATUS SYSTEM_CALL Request(ResDataObject PARAM_IN *pRequest, ResDataObject PARAM_OUT *pResponse);
  304. //notify to lower layer
  305. virtual RET_STATUS SYSTEM_CALL CmdToLogicDev(ResDataObject PARAM_IN *pCmd);
  306. //errors,warnings
  307. void SetErrorInfo(int errCode, char *pErrInfo);
  308. void SetWarningInfo(int warningCode, char *pWarningInfo);
  309. //Data Access
  310. virtual int DATA_ACTION Get(const char PARAM_IN *pKey, ResDataObject &Res);
  311. virtual int DATA_ACTION AddDeviceDescrpt(const char PARAM_IN *pDevPath, const char PARAM_IN *pMachineId, UINT64 ProcId, UINT64 Addr,bool forceAdd = false);
  312. virtual int DATA_ACTION DelDeviceDescrpt(const char PARAM_IN *pDevPath);
  313. virtual int DATA_ACTION ExitDriverProc();
  314. int SYSTEM_CALL GetExitFlag();
  315. int SYSTEM_CALL SetExitFlag(int ExitFlag);
  316. virtual int DATA_ACTION SetDeviceStatus(int Status);//设置设备状态
  317. int SYSTEM_CALL GetDeviceStatus();
  318. virtual int DATA_ACTION SetEthBusSwitch(int Switch);//设置网络EBUS开关
  319. int SYSTEM_CALL GetEthBusSwitch();
  320. virtual int DATA_ACTION SetEthBusRouterIp(const char PARAM_IN *pRouterIp);//设置网络EBUS的RouterIp
  321. int SYSTEM_CALL GetEthBusRouterIp(ResDataObject &obj);
  322. virtual int DATA_ACTION ForTest(bool Flag);
  323. //Data Access of internal
  324. int SYSTEM_CALL GetbusId(ResDataObject &obj);//
  325. int SYSTEM_CALL GetMachineId(ResDataObject &obj);//
  326. int SYSTEM_CALL GetProcId(UINT64 &obj);//
  327. int SYSTEM_CALL SetbusId(ResDataObject &obj);//
  328. int SYSTEM_CALL SetMachineId(ResDataObject &obj);//
  329. int SYSTEM_CALL SetProcId(UINT64 obj);//
  330. DWORD SYSTEM_CALL GetDeviceCount();//
  331. bool SYSTEM_CALL GetDeviceDescript(DWORD Idx, ResDataObject &DevPath, ResDataObject &MachineId, UINT64 &ProcId, UINT64 &Addr);//
  332. //Actions
  333. };