BusUnitClient.cpp 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425
  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. std::cout << m_strClientName << " BusUnitClient::Open [" << pPath << "] Succeeded." << std::endl;
  174. //mLog::FINFO("{$} BusUnitClient::Open [{$}] Succcced.", m_strClientName, pPath);// << endl;
  175. //need update local data!!!!
  176. bool sRet = true;
  177. ResDataObject Attribute;
  178. ResDataObject Temp;
  179. sRet &= TryGetValue(m_DeviceResource, "Attribute", Attribute);
  180. if (sRet)
  181. {
  182. sRet &= TryGetValue(Attribute, "MachineId", Temp);
  183. BusUnitLogic::SetMachineId(Temp);
  184. }
  185. if (sRet)
  186. {
  187. Temp.clear();
  188. sRet &= TryGetValue(Attribute, "BusId", Temp);
  189. BusUnitLogic::SetbusId(Temp);
  190. }
  191. if (sRet)
  192. {
  193. Temp.clear();
  194. sRet &= TryGetValue(Attribute, "ProcId", Temp);
  195. BusUnitLogic::SetProcId((UINT64)Temp);
  196. }
  197. if (sRet)
  198. {
  199. Temp.clear();
  200. sRet &= TryGetValue(Attribute, "DeviceList", Temp);
  201. if (sRet)
  202. {
  203. size_t Size = Temp.size();
  204. for (size_t i = 0; i < Size; i++)
  205. {
  206. try {
  207. std::cout << m_strClientName << " Add DeviceList idx=" << i << " from Open " << pPath << std::endl;
  208. //mLog::FINFO("{$} Add DeviceList idx={$} from Open {$}", m_strClientName, i, pPath);
  209. BusUnitLogic::AddDeviceDescrpt(Temp.GetKey(i),
  210. (const char*)Temp[i]["TargetType"],
  211. (const char*)Temp[i]["MachineId"],
  212. (UINT64)Temp[i]["ProcId"],
  213. (UINT64)Temp[i]["Addr"]);
  214. }
  215. catch (...)
  216. {
  217. std::cout << "[ERROR] " << m_strClientName << " Failed to add DeviceList idx=" << i
  218. << " from Open " << pPath << ". Exception occurred." << std::endl;
  219. ret = RET_FAILED;
  220. }
  221. }
  222. }
  223. }
  224. if (sRet)
  225. {
  226. Temp.clear();
  227. sRet &= TryGetValue(Attribute, "Status", Temp);
  228. BusUnitLogic::SetDeviceStatus((int)Temp);
  229. }
  230. if (sRet)
  231. {
  232. Temp.clear();
  233. sRet &= TryGetValue(Attribute, "EnableEthBus", Temp);
  234. BusUnitLogic::SetEthBusSwitch((int)Temp);
  235. }
  236. if (sRet)
  237. {
  238. Temp.clear();
  239. sRet &= TryGetValue(Attribute, "EthBusRouterIp", Temp);
  240. BusUnitLogic::SetEthBusRouterIp((const char*)Temp);
  241. }
  242. if (sRet)
  243. {
  244. Temp.clear();
  245. sRet &= TryGetValue(Attribute, "ExitFlag", Temp);
  246. BusUnitLogic::SetExitFlag((int)Temp);
  247. }
  248. //last check
  249. if (ret < RET_SUCCEED)
  250. {
  251. LogicClient::Close();
  252. }
  253. }
  254. if (ret < RET_SUCCEED)
  255. std::cout << m_strClientName << " BusUnitClient::Open " << pPath << " Failed." << std::endl;
  256. std::cout << m_strClientName << " BusUnitClient::Open " << pPath << " OVER." << std::endl;
  257. return ret;
  258. }
  259. void BusUnitClient::SubscribeSelf()
  260. {
  261. //订阅主题
  262. }
  263. //get device resource
  264. RET_STATUS SYSTEM_CALL BusUnitClient::GetDeviceResource(ResDataObject PARAM_OUT *pDeviceResource)
  265. {
  266. if (IsClosed())
  267. {
  268. return RET_FAILED;
  269. }
  270. return ((LogicClient*)this)->GetDeviceResource(pDeviceResource);
  271. }
  272. //ResourceCommand Request In and Response Out
  273. RET_STATUS SYSTEM_CALL BusUnitClient::Request(ResDataObject PARAM_IN *pRequest, ResDataObject PARAM_OUT *pResponse)
  274. {
  275. //do nothing
  276. return RET_NOSUPPORT;
  277. }
  278. //notify to lower layer
  279. //notify&response from real logic device
  280. RET_STATUS SYSTEM_CALL BusUnitClient::CmdToLogicDev(ResDataObject PARAM_IN *pCmd)
  281. {
  282. //response and notify comes here
  283. LogicClient::PacketArrived(pCmd);
  284. return RET_SUCCEED;
  285. }
  286. //Data Access
  287. int BusUnitClient::AddDeviceDescrpt(const char PARAM_IN *pDevPath, const char PARAM_IN *pTargetType, const char PARAM_IN *pMachineId, UINT64 ProcId, UINT64 Addr, bool forceAdd)
  288. {
  289. //pack get status
  290. ResDataObject req, res,obj;
  291. //make params
  292. req.add("P0", pDevPath);
  293. req.add("P1", pTargetType);
  294. req.add("P2", pMachineId);
  295. req.add("P3", ProcId);
  296. req.add("P4", Addr);
  297. req.add("P5", forceAdd);
  298. //printf("\r\n BusUnitClient::AddDeviceDescrpt add to Root %s \r\n\r\n", pDevPath);
  299. return Action("AddDeviceDescrpt", req, res);
  300. }
  301. int BusUnitClient::DelDeviceDescrpt(const char PARAM_IN *pDevPath)
  302. {
  303. ResDataObject req, res, obj;
  304. //make params
  305. req.add("P0", pDevPath);
  306. return Action("DelDeviceDescrpt", req, res,50);
  307. }
  308. int BusUnitClient::SetDeviceStatus(int PARAM_IN DevStatus)
  309. {
  310. ResDataObject req, res, obj;
  311. //make params
  312. req.add("P0", DevStatus);
  313. return Action("SetDeviceStatus", req, res);
  314. }
  315. int BusUnitClient::SetEthBusSwitch(int Switch)
  316. {
  317. ResDataObject req, res;
  318. //make params
  319. req.add("P0", Switch);
  320. return Action("SetEthBusSwitch", req, res);
  321. }
  322. int BusUnitClient::SetEthBusRouterIp(const char PARAM_IN *pRouterIp)
  323. {
  324. ResDataObject req, res;
  325. //make params
  326. req.add("P0", pRouterIp);
  327. return Action("SetEthBusRouterIp", req, res);
  328. }
  329. int BusUnitClient::ExitDriverProc()
  330. {
  331. ResDataObject req, res;
  332. return Action_Req("ExitDriverProc", req, res);
  333. }
  334. bool BusUnitClient::GetDeviceUsage()
  335. {
  336. if (IsClosed())
  337. return false;
  338. if (GetExitFlag() != 0)
  339. return false;
  340. if (GetDeviceStatus() != 1)
  341. return false;
  342. //if (IsClosed() == false && GetDeviceStatus() == 1 && GetExitFlag() == 0)
  343. //{
  344. // return true;
  345. //}
  346. return true;
  347. }
  348. int BusUnitClient::ForTest(bool Flag)
  349. {
  350. ResDataObject req, res;
  351. //make params
  352. req.add("P0", Flag);
  353. return Action_Req("ForTest", req, res);
  354. }