// FuncLayer.cpp : 定义 DLL 应用程序的导出函数。 // #include "stdafx.h" #include "FuncLayer.h" #include "common_api.h" #include "PacketAnalizer.h" #define FUNCLAYERGUID "{78EC0791-F6D3-4897-9776-03AF2080F549}" AttrActions::AttrActions() { m_pTarget = 0; m_pGetAddr = 0; m_pSetAddr = 0; m_pAddAddr = 0; m_pDelAddr = 0; m_pUpdateAddr = 0; } AttrActions::AttrActions(ResDataNode *pTarget) { m_pTarget = pTarget; m_pGetAddr = 0; m_pSetAddr = 0; m_pAddAddr = 0; m_pDelAddr = 0; m_pUpdateAddr = 0; }; AttrActions::~AttrActions(){ } void AttrActions::GetAction(PVOID Addr) { m_pGetAddr = (REGISTATTRCHANGEADDR)Addr; } void AttrActions::SetAction(PVOID Addr) { m_pSetAddr = (REGISTATTRCHANGEADDR)Addr; } void AttrActions::AddAction(PVOID Addr) { m_pAddAddr = (REGISTATTRCHANGEADDR)Addr; } void AttrActions::DelAction(PVOID Addr) { m_pDelAddr = (REGISTATTRCHANGEADDR)Addr; } void AttrActions::UpdateAction(PVOID Addr) { m_pUpdateAddr = (REGISTATTRCHANGEADDR)Addr; } FuncLayer::FuncLayer(void) { m_pRegistedAttributes = new map>(); m_pRegistedActions = new map>(); m_RegistDone = CreateEvent(0, 1, 0, 0); } FuncLayer::~FuncLayer(void) { delete m_pRegistedActions; delete m_pRegistedAttributes; CloseHandle(m_RegistDone); } //get device type bool SYSTEM_CALL FuncLayer::GetDeviceType(GUID &DevType) { //for test return string_2_guid(FUNCLAYERGUID, DevType); } bool SYSTEM_CALL FuncLayer::GetDeviceResourceAttrs(ResDataObject &Res) { bool ret = true; try{ map>::iterator iter = m_pRegistedAttributes->begin(); while (iter != m_pRegistedAttributes->end()) { map::iterator iterattr = iter->second.begin(); while (iterattr != iter->second.end()) { ResDataObject Context = (*(iterattr->second.m_pTarget)); Res.add(iterattr->first.c_str(), Context); ++iterattr; } ++iter; } } catch (ResDataObjectExption &exp) { TPRINTA_ERROR(exp.what()); ret = false; } catch (...) { TPRINTA_ERROR("Exeption happend in GetDeviceResourceAttrs"); ret = false; } return ret; } bool SYSTEM_CALL FuncLayer::GetDeviceResourceActions(ResDataObject &Res) { bool ret = true; try{ map>::iterator iter = m_pRegistedActions->begin(); while (iter != m_pRegistedActions->end()) { map::iterator iteraction = iter->second.begin(); while (iteraction != iter->second.end()) { Res.add(iteraction->first.c_str(), ""); ++iteraction; } ++iter; } } catch (ResDataObjectExption &exp) { TPRINTA_ERROR(exp.what()); ret = false; } catch (...) { TPRINTA_ERROR("Exeption happend in GetDeviceResourceActions"); ret = false; } return ret; } //get device resource RET_STATUS SYSTEM_CALL FuncLayer::GetDeviceResource(ResDataObject PARAM_OUT *pDeviceResource) { bool ret = true; if (m_RegistDone == false) { return RET_INVALID; } //attributes //make device type ret &= pDeviceResource->add("ClientType", DPC_UnitClient); GUID DeviceType; if (GetDeviceType(DeviceType)) { string GuidStr; guid_2_string(DeviceType, GuidStr); ret &= pDeviceResource->add("DeviceType", GuidStr.c_str()); } else { ret &= pDeviceResource->add("DeviceType", FUNCLAYERGUID); } //make attributes ResDataObject val; GetDeviceResourceAttrs(val); ret &= pDeviceResource->add("Attribute", val); ResDataObject Actions; GetDeviceResourceActions(Actions); if (ret) { pDeviceResource->add("Action", Actions); return RET_SUCCEED; } return RET_FAILED; } //notify to lower layer RET_STATUS SYSTEM_CALL FuncLayer::CmdToLogicDev(ResDataObject PARAM_IN *pCmd) { assert(0);//not happening return RET_FAILED; } //ResourceCommand Request In and Response Out RET_STATUS SYSTEM_CALL FuncLayer::Request(ResDataObject PARAM_IN *pRequest, ResDataObject PARAM_OUT *pResponse) { RET_STATUS ret = RET_INVALID; if (m_RegistDone == false) { return RET_INVALID; } ResDataObject res; string key = PacketAnalizer::GetPacketKey(pRequest); PACKET_CMD cmd = PacketAnalizer::GetPacketCmd(pRequest); if (cmd == PACKET_CMD_GET) { //get request ret = GET(key.c_str(), res); if (ret >= RET_SUCCEED) { pResponse->update("CONTEXT", res); } } else if ((cmd == PACKET_CMD_UPDATE) || (cmd == PACKET_CMD_ADD) || (cmd == PACKET_CMD_DEL) || (cmd == PACKET_CMD_PART_UPDATE)) { //set request PacketAnalizer::GetPacketContext(pRequest, res); ret = Dispatch_AttributeChange(cmd,key.c_str(), res); //set no need context update } else if (cmd == PACKET_CMD_EXE) { //action request ResDataObject req; PacketAnalizer::GetPacketContext(pRequest, req); ret = Action(key.c_str(), req, res); if (ret >= RET_SUCCEED) { pResponse->update("CONTEXT", res); } } else { //ignore others ret = RET_NOSUPPORT; } PacketAnalizer::MakeRetCode(ret, pResponse); return ret; } bool SYSTEM_CALL FuncLayer::RegistFunc(PVOID pTarget, const char *pFuncName, PVOID FunctionAddr) { bool ret = true; if (WaitForSingleObject(m_RegistDone, 0) == WAIT_OBJECT_0 || strlen(pFuncName) == 0) { return false; } map>::iterator iter = m_pRegistedActions->find(pTarget); if (iter == m_pRegistedActions->end()) { //not exist.just assign (*m_pRegistedActions)[pTarget][pFuncName] = FunctionAddr; } else { //target exist map::iterator iterfunc = iter->second.find(pFuncName); if (iterfunc == iter->second.end()) { //func not exist.just assin (*m_pRegistedActions)[pTarget][pFuncName] = FunctionAddr; } else { //func exist,no can do. ret = false; } } return ret; } bool SYSTEM_CALL FuncLayer::RegistAttributeGet(PVOID pTarget, ResDataNode *pDataNode, PVOID FunctionAddr) { bool ret = true; if (WaitForSingleObject(m_RegistDone, 0) == WAIT_OBJECT_0 || strlen(pDataNode->GetKey()) == 0) { return false; } map>::iterator iter = m_pRegistedAttributes->find(pTarget); if (iter == m_pRegistedAttributes->end()) { //not exist.just assign pDataNode->SetLogicDevice(this); AttrActions attractions(pDataNode); attractions.GetAction(FunctionAddr); (*m_pRegistedAttributes)[pTarget][pDataNode->GetKey()] = attractions; } else { //target exist map::iterator iterfunc = iter->second.find(pDataNode->GetKey()); if (iterfunc == iter->second.end()) { //key not exist.just assin pDataNode->SetLogicDevice(this); AttrActions attractions(pDataNode); attractions.GetAction(FunctionAddr); (*m_pRegistedAttributes)[pTarget][pDataNode->GetKey()] = attractions; } else { //target exist iterfunc->second.GetAction(FunctionAddr); } } return ret; } bool SYSTEM_CALL FuncLayer::RegistAttributeAdd(PVOID pTarget, ResDataNode *pDataNode, PVOID FunctionAddr) { bool ret = true; if (WaitForSingleObject(m_RegistDone, 0) == WAIT_OBJECT_0 || strlen(pDataNode->GetKey()) == 0) { return false; } map>::iterator iter = m_pRegistedAttributes->find(pTarget); if (iter == m_pRegistedAttributes->end()) { //not exist.just assign pDataNode->SetLogicDevice(this); AttrActions attractions(pDataNode); attractions.AddAction(FunctionAddr); (*m_pRegistedAttributes)[pTarget][pDataNode->GetKey()] = attractions; } else { //target exist map::iterator iterfunc = iter->second.find(pDataNode->GetKey()); if (iterfunc == iter->second.end()) { //key not exist.just assin pDataNode->SetLogicDevice(this); AttrActions attractions(pDataNode); attractions.AddAction(FunctionAddr); (*m_pRegistedAttributes)[pTarget][pDataNode->GetKey()] = attractions; } else { //target exist iterfunc->second.AddAction(FunctionAddr); } } return ret; } bool SYSTEM_CALL FuncLayer::RegistAttributeDel(PVOID pTarget, ResDataNode *pDataNode, PVOID FunctionAddr) { bool ret = true; if (WaitForSingleObject(m_RegistDone, 0) == WAIT_OBJECT_0 || strlen(pDataNode->GetKey()) == 0) { return false; } map>::iterator iter = m_pRegistedAttributes->find(pTarget); if (iter == m_pRegistedAttributes->end()) { //not exist.just assign pDataNode->SetLogicDevice(this); AttrActions attractions(pDataNode); attractions.DelAction(FunctionAddr); (*m_pRegistedAttributes)[pTarget][pDataNode->GetKey()] = attractions; } else { //target exist map::iterator iterfunc = iter->second.find(pDataNode->GetKey()); if (iterfunc == iter->second.end()) { //key not exist.just assin pDataNode->SetLogicDevice(this); AttrActions attractions(pDataNode); attractions.DelAction(FunctionAddr); (*m_pRegistedAttributes)[pTarget][pDataNode->GetKey()] = attractions; } else { //target exist iterfunc->second.DelAction(FunctionAddr); } } return ret; } bool SYSTEM_CALL FuncLayer::RegistAttributeSet(PVOID pTarget, ResDataNode *pDataNode, PVOID FunctionAddr) { bool ret = true; if (WaitForSingleObject(m_RegistDone, 0) == WAIT_OBJECT_0 || strlen(pDataNode->GetKey()) == 0) { return false; } map>::iterator iter = m_pRegistedAttributes->find(pTarget); if (iter == m_pRegistedAttributes->end()) { //not exist.just assign pDataNode->SetLogicDevice(this); AttrActions attractions(pDataNode); attractions.SetAction(FunctionAddr); (*m_pRegistedAttributes)[pTarget][pDataNode->GetKey()] = attractions; } else { //target exist map::iterator iterfunc = iter->second.find(pDataNode->GetKey()); if (iterfunc == iter->second.end()) { //key not exist.just assin pDataNode->SetLogicDevice(this); AttrActions attractions(pDataNode); attractions.SetAction(FunctionAddr); (*m_pRegistedAttributes)[pTarget][pDataNode->GetKey()] = attractions; } else { //target exist iterfunc->second.SetAction(FunctionAddr); } } return ret; } bool SYSTEM_CALL FuncLayer::RegistAttributeUpdate(PVOID pTarget, ResDataNode *pDataNode, PVOID FunctionAddr) { bool ret = true; if (WaitForSingleObject(m_RegistDone, 0) == WAIT_OBJECT_0 || strlen(pDataNode->GetKey()) == 0) { return false; } map>::iterator iter = m_pRegistedAttributes->find(pTarget); if (iter == m_pRegistedAttributes->end()) { //not exist.just assign pDataNode->SetLogicDevice(this); AttrActions attractions(pDataNode); attractions.UpdateAction(FunctionAddr); (*m_pRegistedAttributes)[pTarget][pDataNode->GetKey()] = attractions; } else { //target exist map::iterator iterfunc = iter->second.find(pDataNode->GetKey()); if (iterfunc == iter->second.end()) { //key not exist.just assin pDataNode->SetLogicDevice(this); AttrActions attractions(pDataNode); attractions.UpdateAction(FunctionAddr); (*m_pRegistedAttributes)[pTarget][pDataNode->GetKey()] = attractions; } else { //target exist iterfunc->second.UpdateAction(FunctionAddr); } } return ret; } void SYSTEM_CALL FuncLayer::SetRegistStatus(bool Done) { if (Done) { SetEvent(m_RegistDone); } else { ResetEvent(m_RegistDone); } } RET_STATUS DEVICE_ACTION FuncLayer::GET(const char *pszKey, ResDataObject &Context) { map>::iterator iter = m_pRegistedAttributes->begin(); while (iter != m_pRegistedAttributes->end()) { map::iterator iterattr = iter->second.find(pszKey); if (iterattr != iter->second.end()) { //found attr Context = (ResDataObject)(*(iterattr->second.m_pTarget)); return RET_SUCCEED; } ++iter; } return RET_NOSUPPORT; } RET_STATUS DEVICE_ACTION FuncLayer::ADD(const char *pszKey, ResDataObject &Context) { return RET_NOSUPPORT; } RET_STATUS DEVICE_ACTION FuncLayer::DEL(const char *pszKey, ResDataObject &Context) { return RET_NOSUPPORT; } RET_STATUS DEVICE_ACTION FuncLayer::SET(const char *pszKey, ResDataObject &Context) { return RET_NOSUPPORT; } RET_STATUS DEVICE_ACTION FuncLayer::UPDATE(const char *pszKey, ResDataObject &Context) { return RET_NOSUPPORT; } RET_STATUS DEVICE_ACTION FuncLayer::Dispatch_AttributeChange(INT cmd, const char *pszKey, ResDataObject &Context) { map>::iterator iter = m_pRegistedAttributes->begin(); while (iter != m_pRegistedAttributes->end()) { map::iterator iterattr = iter->second.find(pszKey); if (iterattr != iter->second.end()) { //found attr if (cmd == PACKET_CMD_GET) { if (iterattr->second.m_pGetAddr) { REGISTATTRCHANGEADDR AttrFunc = (REGISTATTRCHANGEADDR)iterattr->second.m_pGetAddr; return AttrFunc(iter->first, Context); } //go normal routine return GET(pszKey, Context); } else if (cmd == PACKET_CMD_UPDATE) { if (iterattr->second.m_pSetAddr) { REGISTATTRCHANGEADDR AttrFunc = (REGISTATTRCHANGEADDR)iterattr->second.m_pSetAddr; return AttrFunc(iter->first, Context); } //go normal routine return SET(pszKey, Context); } else if (cmd == PACKET_CMD_ADD) { if (iterattr->second.m_pAddAddr) { REGISTATTRCHANGEADDR AttrFunc = (REGISTATTRCHANGEADDR)iterattr->second.m_pAddAddr; return AttrFunc(iter->first, Context); } //go normal routine return ADD(pszKey, Context); } else if (cmd == PACKET_CMD_DEL) { if (iterattr->second.m_pDelAddr) { REGISTATTRCHANGEADDR AttrFunc = (REGISTATTRCHANGEADDR)iterattr->second.m_pDelAddr; return AttrFunc(iter->first, Context); } //go normal routine return DEL(pszKey, Context); } else if (cmd == PACKET_CMD_PART_UPDATE) { if (iterattr->second.m_pUpdateAddr) { REGISTATTRCHANGEADDR AttrFunc = (REGISTATTRCHANGEADDR)iterattr->second.m_pUpdateAddr; return AttrFunc(iter->first, Context); } //go normal routine return UPDATE(pszKey, Context); } else { //do nothing } //got match and done here break; } ++iter; } return RET_NOSUPPORT; } RET_STATUS DEVICE_ACTION FuncLayer::Action(const char *pszKey, ResDataObject &req, ResDataObject &res) { map>::iterator iter = m_pRegistedActions->begin(); while (iter != m_pRegistedActions->end()) { map::iterator iteraction = iter->second.find(pszKey); if (iteraction != iter->second.end()) { //found action REGISTFUNCTYPE0 actionfunc = (REGISTFUNCTYPE0)iteraction->second; return actionfunc(iter->first, req, res); } ++iter; } return RET_NOSUPPORT; } RET_STATUS DEVICE_ACTION FuncLayer::PostNotify(ResDataObject &Notify) { //暂时用继承方式,后续调整为引用. return CmdFromLogicDev(&Notify); }