// BusUnitClient.cpp : 定义 DLL 应用程序的导出函数。 // #include "PacketAnalizer.h" #include "BusUnitClient.h" #include // 这是已导出类的构造函数。 // 有关类定义的信息,请参阅 BusUnitClient.h /* BusUnitClient::BusUnitClient() { //m_pSysIF->SetLogicDevice(pLogicDeivce); }*/ BusUnitClient::BusUnitClient(string szClientName, string szType) : LogicClient(szClientName,"busUnit", szType) { pLogicDeivce = (LogicDevice*)this; pLogicDeivce = (LogicDevice*)this; } BusUnitClient::~BusUnitClient() { return; } BusUnitClient& BusUnitClient::operator = (const BusUnitClient &tValue) { //strongly recommended do not assign a client to another one if (this != &tValue) { m_FileFlags = tValue.m_FileFlags; (*m_pFilePath) = (*(tValue.m_pFilePath)); //do open if (tValue.m_ClosedFlag == false) { if (Open((tValue.m_pFilePath)->c_str(), (CCOS_FILE_FLAGS)tValue.m_FileFlags) <= 0) { //failed here //put some log //printf("Thread:%d,Open Failed\n", GetCurrentThreadId()); } } } return (*this); } PACKET_CMD BusUnitClient::ReadCmd(ResDataObject &CmdObject) { PACKET_CMD ret = PACKET_CMD_NONE; if (IsDataArrived()) { ret = LogicClient::ReadNotify(CmdObject); ResDataObject Context; string key = PacketAnalizer::GetPacketKey(&CmdObject); if (PacketAnalizer::GetPacketContext(&CmdObject, Context) == false) { //log here return PACKET_CMD_NONE; } if (ret == PACKET_CMD_ADD) { try { string reqkey = PacketAnalizer::GetPacketKey(&CmdObject); //mLog::FINFO("Add {$} with {$}", reqkey, CmdObject.encode()); if (reqkey == "DeviceList") { DeviceDescript dd; dd.SetResDataObject(Context); BusUnitLogic::AddDeviceDescrpt(dd.GetKey(), dd.m_TargetType.GetVal(), dd.m_MachineId.GetVal(), dd.m_ProcId, dd.m_Address); } else if(reqkey == "CcosDeviceList") { DeviceDescript dd; dd.SetResDataObject(Context); BusUnitLogic::AddDeviceDescrpt(dd.GetKey(), dd.m_TargetType.GetVal(), dd.m_MachineId.GetVal(), dd.m_ProcId, dd.m_Address); } } catch (...) { //log here ret = PACKET_CMD_NONE; } return ret; } else if (ret == PACKET_CMD_DEL) { try { string reqkey = PacketAnalizer::GetPacketKey(&CmdObject); if (reqkey == "DeviceList") { BusUnitLogic::DelDeviceDescrpt((const char *)Context[0]); } } catch (...) { //log here ret = PACKET_CMD_NONE; } return ret; } else if (ret == PACKET_CMD_UPDATE) { try { string key = PacketAnalizer::GetPacketKey(&CmdObject); ResDataObject Context; if (PacketAnalizer::GetPacketContext(&CmdObject, Context)) { if (key == "Status") { if (BusUnitLogic::GetDeviceStatus() != (int)Context) { BusUnitLogic::SetDeviceStatus((int)Context); } else { ret = PACKET_CMD_NONE; } } else if (key == "ExitFlag") { if (BusUnitLogic::GetExitFlag() != (int)Context) { BusUnitLogic::SetExitFlag((int)Context); } else { ret = PACKET_CMD_NONE; } } else if (key == "EnableEthBus") { if (BusUnitLogic::GetEthBusSwitch() != (int)Context) { BusUnitLogic::SetEthBusSwitch((int)Context); } else { ret = PACKET_CMD_NONE; } } else if (key == "EthBusRouterIp") { string IpStr; ResDataObject Ip; BusUnitLogic::GetEthBusRouterIp(Ip); IpStr = (const char*)Ip; if (IpStr != (const char*)Context) { BusUnitLogic::SetEthBusRouterIp((const char*)Context); } else { ret = PACKET_CMD_NONE; } } } } catch (...) { //log here ret = PACKET_CMD_NONE; } return ret; } else if (ret != PACKET_CMD_NONE) { return ret; } } return ret; } int BusUnitClient::Open(const char *pPath, int flags, DWORD timeout) { std::cout << "BusUnitClient::Open - pPath:"<< pPath<<" flags:"<< flags << std::endl; RET_STATUS ret = (RET_STATUS)LogicClient::Open(pPath, flags,"", timeout); if (ret >= RET_SUCCEED) { std::cout << m_strClientName << " BusUnitClient::Open [" << pPath << "] Succeeded." << std::endl; //mLog::FINFO("{$} BusUnitClient::Open [{$}] Succcced.", m_strClientName, pPath);// << endl; //need update local data!!!! bool sRet = true; ResDataObject Attribute; ResDataObject Temp; sRet &= TryGetValue(m_DeviceResource, "Attribute", Attribute); if (sRet) { sRet &= TryGetValue(Attribute, "MachineId", Temp); BusUnitLogic::SetMachineId(Temp); } if (sRet) { Temp.clear(); sRet &= TryGetValue(Attribute, "BusId", Temp); BusUnitLogic::SetbusId(Temp); } if (sRet) { Temp.clear(); sRet &= TryGetValue(Attribute, "ProcId", Temp); BusUnitLogic::SetProcId((UINT64)Temp); } if (sRet) { Temp.clear(); sRet &= TryGetValue(Attribute, "DeviceList", Temp); if (sRet) { size_t Size = Temp.size(); for (size_t i = 0; i < Size; i++) { try { std::cout << m_strClientName << " Add DeviceList idx=" << i << " from Open " << pPath << std::endl; //mLog::FINFO("{$} Add DeviceList idx={$} from Open {$}", m_strClientName, i, pPath); BusUnitLogic::AddDeviceDescrpt(Temp.GetKey(i), (const char*)Temp[i]["TargetType"], (const char*)Temp[i]["MachineId"], (UINT64)Temp[i]["ProcId"], (UINT64)Temp[i]["Addr"]); } catch (...) { std::cout << "[ERROR] " << m_strClientName << " Failed to add DeviceList idx=" << i << " from Open " << pPath << ". Exception occurred." << std::endl; ret = RET_FAILED; } } } } if (sRet) { Temp.clear(); sRet &= TryGetValue(Attribute, "Status", Temp); BusUnitLogic::SetDeviceStatus((int)Temp); } if (sRet) { Temp.clear(); sRet &= TryGetValue(Attribute, "EnableEthBus", Temp); BusUnitLogic::SetEthBusSwitch((int)Temp); } if (sRet) { Temp.clear(); sRet &= TryGetValue(Attribute, "EthBusRouterIp", Temp); BusUnitLogic::SetEthBusRouterIp((const char*)Temp); } if (sRet) { Temp.clear(); sRet &= TryGetValue(Attribute, "ExitFlag", Temp); BusUnitLogic::SetExitFlag((int)Temp); } //last check if (ret < RET_SUCCEED) { LogicClient::Close(); } } if (ret < RET_SUCCEED) std::cout << m_strClientName << " BusUnitClient::Open " << pPath << " Failed." << std::endl; std::cout << m_strClientName << " BusUnitClient::Open " << pPath << " OVER." << std::endl; return ret; } void BusUnitClient::SubscribeSelf() { //订阅主题 } //get device resource RET_STATUS SYSTEM_CALL BusUnitClient::GetDeviceResource(ResDataObject PARAM_OUT *pDeviceResource) { if (IsClosed()) { return RET_FAILED; } return ((LogicClient*)this)->GetDeviceResource(pDeviceResource); } //ResourceCommand Request In and Response Out RET_STATUS SYSTEM_CALL BusUnitClient::Request(ResDataObject PARAM_IN *pRequest, ResDataObject PARAM_OUT *pResponse) { //do nothing return RET_NOSUPPORT; } //notify to lower layer //notify&response from real logic device RET_STATUS SYSTEM_CALL BusUnitClient::CmdToLogicDev(ResDataObject PARAM_IN *pCmd) { //response and notify comes here LogicClient::PacketArrived(pCmd); return RET_SUCCEED; } //Data Access int BusUnitClient::AddDeviceDescrpt(const char PARAM_IN *pDevPath, const char PARAM_IN *pTargetType, const char PARAM_IN *pMachineId, UINT64 ProcId, UINT64 Addr, bool forceAdd) { //pack get status ResDataObject req, res,obj; //make params req.add("P0", pDevPath); req.add("P1", pTargetType); req.add("P2", pMachineId); req.add("P3", ProcId); req.add("P4", Addr); req.add("P5", forceAdd); //printf("\r\n BusUnitClient::AddDeviceDescrpt add to Root %s \r\n\r\n", pDevPath); return Action("AddDeviceDescrpt", req, res); } int BusUnitClient::DelDeviceDescrpt(const char PARAM_IN *pDevPath) { ResDataObject req, res, obj; //make params req.add("P0", pDevPath); return Action("DelDeviceDescrpt", req, res,50); } int BusUnitClient::SetDeviceStatus(int PARAM_IN DevStatus) { ResDataObject req, res, obj; //make params req.add("P0", DevStatus); return Action("SetDeviceStatus", req, res); } int BusUnitClient::SetEthBusSwitch(int Switch) { ResDataObject req, res; //make params req.add("P0", Switch); return Action("SetEthBusSwitch", req, res); } int BusUnitClient::SetEthBusRouterIp(const char PARAM_IN *pRouterIp) { ResDataObject req, res; //make params req.add("P0", pRouterIp); return Action("SetEthBusRouterIp", req, res); } int BusUnitClient::ExitDriverProc() { ResDataObject req, res; return Action_Req("ExitDriverProc", req, res); } bool BusUnitClient::GetDeviceUsage() { if (IsClosed()) return false; if (GetExitFlag() != 0) return false; if (GetDeviceStatus() != 1) return false; //if (IsClosed() == false && GetDeviceStatus() == 1 && GetExitFlag() == 0) //{ // return true; //} return true; } int BusUnitClient::ForTest(bool Flag) { ResDataObject req, res; //make params req.add("P0", Flag); return Action_Req("ForTest", req, res); }