BusUnitClient.cpp 8.9 KB

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