123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716 |
- // 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<PVOID, map<string, AttrActions>>();
- m_pRegistedActions = new map<PVOID, map<string, PVOID>>();
- 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<PVOID, map<string, AttrActions>>::iterator iter = m_pRegistedAttributes->begin();
- while (iter != m_pRegistedAttributes->end())
- {
- map<string, AttrActions>::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<PVOID, map<string, PVOID>>::iterator iter = m_pRegistedActions->begin();
- while (iter != m_pRegistedActions->end())
- {
- map<string, PVOID>::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<PVOID, map<string, PVOID>>::iterator iter = m_pRegistedActions->find(pTarget);
- if (iter == m_pRegistedActions->end())
- {
- //not exist.just assign
- (*m_pRegistedActions)[pTarget][pFuncName] = FunctionAddr;
- }
- else
- {
- //target exist
- map<string, PVOID>::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<PVOID, map<string, AttrActions>>::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<string, AttrActions>::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<PVOID, map<string, AttrActions>>::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<string, AttrActions>::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<PVOID, map<string, AttrActions>>::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<string, AttrActions>::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<PVOID, map<string, AttrActions>>::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<string, AttrActions>::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<PVOID, map<string, AttrActions>>::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<string, AttrActions>::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<PVOID, map<string, AttrActions>>::iterator iter = m_pRegistedAttributes->begin();
- while (iter != m_pRegistedAttributes->end())
- {
- map<string, AttrActions>::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<PVOID, map<string, AttrActions>>::iterator iter = m_pRegistedAttributes->begin();
- while (iter != m_pRegistedAttributes->end())
- {
- map<string, AttrActions>::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<PVOID, map<string, PVOID>>::iterator iter = m_pRegistedActions->begin();
- while (iter != m_pRegistedActions->end())
- {
- map<string, PVOID>::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);
- }
|