BusUnitClient.cpp 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391
  1. // BusUnitClient.cpp : 定义 DLL 应用程序的导出函数。
  2. //
  3. #include "stdafx.h"
  4. #include "PacketAnalizer.h"
  5. #include "BusUnitClient.h"
  6. // 这是已导出类的构造函数。
  7. // 有关类定义的信息,请参阅 BusUnitClient.h
  8. BusUnitClient::BusUnitClient()
  9. {
  10. pLogicDeivce = (LogicDevice*)this;
  11. //m_pSysIF->SetLogicDevice(pLogicDeivce);
  12. }
  13. BusUnitClient::~BusUnitClient()
  14. {
  15. return;
  16. }
  17. BusUnitClient& BusUnitClient::operator = (const BusUnitClient &tValue)
  18. {
  19. //strongly recommended do not assign a client to another one
  20. if (this != &tValue)
  21. {
  22. m_FileFlags = tValue.m_FileFlags;
  23. (*m_pFilePath) = (*(tValue.m_pFilePath));
  24. //do open
  25. if (tValue.m_ClosedFlag == false)
  26. {
  27. if (Open((tValue.m_pFilePath)->c_str(), (CCOS_FILE_FLAGS)tValue.m_FileFlags) <= 0)
  28. {
  29. //failed here
  30. //put some log
  31. printf("Thread:%d,Open Failed\n", GetCurrentThreadId());
  32. }
  33. }
  34. }
  35. return (*this);
  36. }
  37. PACKET_CMD BusUnitClient::ReadCmd(ResDataObject &CmdObject)
  38. {
  39. PACKET_CMD ret = PACKET_CMD_NONE;
  40. if (IsDataArrived())
  41. {
  42. ret = LogicClient::ReadCmd(CmdObject);
  43. ResDataObject Context;
  44. string key = PacketAnalizer::GetPacketKey(&CmdObject);
  45. if (PacketAnalizer::GetPacketContext(&CmdObject, Context) == false)
  46. {
  47. //log here
  48. return PACKET_CMD_NONE;
  49. }
  50. if (ret == PACKET_CMD_ADD)
  51. {
  52. try {
  53. string reqkey = PacketAnalizer::GetPacketKey(&CmdObject);
  54. if (reqkey == "DeviceList")
  55. {
  56. DeviceDescript dd;
  57. dd.SetResDataObject(Context);
  58. BusUnitLogic::AddDeviceDescrpt(dd.GetKey(), dd.m_TargetType.GetVal(), dd.m_MachineId.GetVal(), dd.m_ProcId, dd.m_Address);
  59. }
  60. }
  61. catch (...)
  62. {
  63. //log here
  64. ret = PACKET_CMD_NONE;
  65. }
  66. return ret;
  67. }
  68. else if (ret == PACKET_CMD_DEL)
  69. {
  70. try {
  71. string reqkey = PacketAnalizer::GetPacketKey(&CmdObject);
  72. if (reqkey == "DeviceList")
  73. {
  74. BusUnitLogic::DelDeviceDescrpt((const char *)Context[0]);
  75. }
  76. }
  77. catch (...)
  78. {
  79. //log here
  80. ret = PACKET_CMD_NONE;
  81. }
  82. return ret;
  83. }
  84. else if (ret == PACKET_CMD_UPDATE)
  85. {
  86. try {
  87. string key = PacketAnalizer::GetPacketKey(&CmdObject);
  88. ResDataObject Context;
  89. if (PacketAnalizer::GetPacketContext(&CmdObject, Context))
  90. {
  91. if (key == "Status")
  92. {
  93. if (BusUnitLogic::GetDeviceStatus() != (int)Context)
  94. {
  95. BusUnitLogic::SetDeviceStatus((int)Context);
  96. }
  97. else
  98. {
  99. ret = PACKET_CMD_NONE;
  100. }
  101. }
  102. else if (key == "ExitFlag")
  103. {
  104. if (BusUnitLogic::GetExitFlag() != (int)Context)
  105. {
  106. BusUnitLogic::SetExitFlag((int)Context);
  107. }
  108. else
  109. {
  110. ret = PACKET_CMD_NONE;
  111. }
  112. }
  113. else if (key == "EnableEthBus")
  114. {
  115. if (BusUnitLogic::GetEthBusSwitch() != (int)Context)
  116. {
  117. BusUnitLogic::SetEthBusSwitch((int)Context);
  118. }
  119. else
  120. {
  121. ret = PACKET_CMD_NONE;
  122. }
  123. }
  124. else if (key == "EthBusRouterIp")
  125. {
  126. string IpStr;
  127. ResDataObject Ip;
  128. BusUnitLogic::GetEthBusRouterIp(Ip);
  129. IpStr = (const char*)Ip;
  130. if (IpStr != (const char*)Context)
  131. {
  132. BusUnitLogic::SetEthBusRouterIp((const char*)Context);
  133. }
  134. else
  135. {
  136. ret = PACKET_CMD_NONE;
  137. }
  138. }
  139. }
  140. }
  141. catch (...)
  142. {
  143. //log here
  144. ret = PACKET_CMD_NONE;
  145. }
  146. return ret;
  147. }
  148. else if (ret != PACKET_CMD_NONE)
  149. {
  150. return ret;
  151. }
  152. }
  153. return ret;
  154. }
  155. int BusUnitClient::Open(const char *pPath, int flags, DWORD timeout)
  156. {
  157. RET_STATUS ret = (RET_STATUS)LogicClient::Open(pPath, flags, timeout);
  158. if (ret >= RET_SUCCEED)
  159. {
  160. //need update local data!!!!
  161. bool sRet = true;
  162. ResDataObject Attribute;
  163. ResDataObject Temp;
  164. sRet &= TryGetValue(m_DeviceResource, "Attribute", Attribute);
  165. if (sRet)
  166. {
  167. sRet &= TryGetValue(Attribute, "MachineId", Temp);
  168. BusUnitLogic::SetMachineId(Temp);
  169. }
  170. if (sRet)
  171. {
  172. Temp.clear();
  173. sRet &= TryGetValue(Attribute, "BusId", Temp);
  174. BusUnitLogic::SetbusId(Temp);
  175. }
  176. if (sRet)
  177. {
  178. Temp.clear();
  179. sRet &= TryGetValue(Attribute, "ProcId", Temp);
  180. BusUnitLogic::SetProcId((UINT64)Temp);
  181. }
  182. if (sRet)
  183. {
  184. Temp.clear();
  185. sRet &= TryGetValue(Attribute, "DeviceList", Temp);
  186. if (sRet)
  187. {
  188. size_t Size = Temp.size();
  189. for (size_t i = 0; i < Size; i++)
  190. {
  191. try {
  192. BusUnitLogic::AddDeviceDescrpt(Temp.GetKey(i),
  193. (const char*)Temp[i]["TargetType"],
  194. (const char*)Temp[i]["MachineId"],
  195. (UINT64)Temp[i]["ProcId"],
  196. (UINT64)Temp[i]["Addr"]);
  197. }
  198. catch (...)
  199. {
  200. ret = RET_FAILED;
  201. }
  202. }
  203. }
  204. }
  205. if (sRet)
  206. {
  207. Temp.clear();
  208. sRet &= TryGetValue(Attribute, "Status", Temp);
  209. BusUnitLogic::SetDeviceStatus((int)Temp);
  210. }
  211. if (sRet)
  212. {
  213. Temp.clear();
  214. sRet &= TryGetValue(Attribute, "EnableEthBus", Temp);
  215. BusUnitLogic::SetEthBusSwitch((int)Temp);
  216. }
  217. if (sRet)
  218. {
  219. Temp.clear();
  220. sRet &= TryGetValue(Attribute, "EthBusRouterIp", Temp);
  221. BusUnitLogic::SetEthBusRouterIp((const char*)Temp);
  222. }
  223. if (sRet)
  224. {
  225. Temp.clear();
  226. sRet &= TryGetValue(Attribute, "ExitFlag", Temp);
  227. BusUnitLogic::SetExitFlag((int)Temp);
  228. }
  229. //last check
  230. if (ret < RET_SUCCEED)
  231. {
  232. LogicClient::Close();
  233. }
  234. }
  235. return ret;
  236. }
  237. //get device resource
  238. RET_STATUS SYSTEM_CALL BusUnitClient::GetDeviceResource(ResDataObject PARAM_OUT *pDeviceResource)
  239. {
  240. if (IsClosed())
  241. {
  242. return RET_FAILED;
  243. }
  244. return ((LogicClient*)this)->GetDeviceResource(pDeviceResource);
  245. }
  246. //ResourceCommand Request In and Response Out
  247. RET_STATUS SYSTEM_CALL BusUnitClient::Request(ResDataObject PARAM_IN *pRequest, ResDataObject PARAM_OUT *pResponse)
  248. {
  249. //do nothing
  250. return RET_NOSUPPORT;
  251. }
  252. //notify to lower layer
  253. //notify&response from real logic device
  254. RET_STATUS SYSTEM_CALL BusUnitClient::CmdToLogicDev(ResDataObject PARAM_IN *pCmd)
  255. {
  256. //response and notify comes here
  257. PacketArrived(pCmd);
  258. return RET_SUCCEED;
  259. }
  260. //Data Access
  261. int BusUnitClient::AddDeviceDescrpt(const char PARAM_IN *pDevPath, const char PARAM_IN *pTargetType, const char PARAM_IN *pMachineId, UINT64 ProcId, UINT64 Addr, bool forceAdd)
  262. {
  263. //pack get status
  264. ResDataObject req, res,obj;
  265. //make params
  266. req.add("P0", pDevPath);
  267. req.add("P1", pTargetType);
  268. req.add("P2", pMachineId);
  269. req.add("P3", ProcId);
  270. req.add("P4", Addr);
  271. req.add("P5", forceAdd);
  272. printf("\r\n BusUnitClient::AddDeviceDescrpt add to Root %s \r\n\r\n", pDevPath);
  273. return Action("AddDeviceDescrpt", req, res);
  274. }
  275. int BusUnitClient::DelDeviceDescrpt(const char PARAM_IN *pDevPath)
  276. {
  277. ResDataObject req, res, obj;
  278. //make params
  279. req.add("P0", pDevPath);
  280. return Action("DelDeviceDescrpt", req, res,50);
  281. }
  282. int BusUnitClient::SetDeviceStatus(int PARAM_IN DevStatus)
  283. {
  284. ResDataObject req, res, obj;
  285. //make params
  286. req.add("P0", DevStatus);
  287. return Action("SetDeviceStatus", req, res);
  288. }
  289. int BusUnitClient::SetEthBusSwitch(int Switch)
  290. {
  291. ResDataObject req, res;
  292. //make params
  293. req.add("P0", Switch);
  294. return Action("SetEthBusSwitch", req, res);
  295. }
  296. int BusUnitClient::SetEthBusRouterIp(const char PARAM_IN *pRouterIp)
  297. {
  298. ResDataObject req, res;
  299. //make params
  300. req.add("P0", pRouterIp);
  301. return Action("SetEthBusRouterIp", req, res);
  302. }
  303. int BusUnitClient::ExitDriverProc()
  304. {
  305. ResDataObject req, res;
  306. return Action_Req("ExitDriverProc", req, res);
  307. }
  308. bool BusUnitClient::GetDeviceUsage()
  309. {
  310. if (IsClosed() == false && GetDeviceStatus() == 1 && GetExitFlag() == 0)
  311. {
  312. return true;
  313. }
  314. return false;
  315. }
  316. int BusUnitClient::ForTest(bool Flag)
  317. {
  318. ResDataObject req, res;
  319. //make params
  320. req.add("P0", Flag);
  321. return Action_Req("ForTest", req, res);
  322. }