BusUnitClient.cpp 8.5 KB

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