FuncLayer.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716
  1. // FuncLayer.cpp : 定义 DLL 应用程序的导出函数。
  2. //
  3. #include "stdafx.h"
  4. #include "FuncLayer.h"
  5. #include "common_api.h"
  6. #include "PacketAnalizer.h"
  7. #define FUNCLAYERGUID "{78EC0791-F6D3-4897-9776-03AF2080F549}"
  8. AttrActions::AttrActions()
  9. {
  10. m_pTarget = 0;
  11. m_pGetAddr = 0;
  12. m_pSetAddr = 0;
  13. m_pAddAddr = 0;
  14. m_pDelAddr = 0;
  15. m_pUpdateAddr = 0;
  16. }
  17. AttrActions::AttrActions(ResDataNode *pTarget)
  18. {
  19. m_pTarget = pTarget;
  20. m_pGetAddr = 0;
  21. m_pSetAddr = 0;
  22. m_pAddAddr = 0;
  23. m_pDelAddr = 0;
  24. m_pUpdateAddr = 0;
  25. };
  26. AttrActions::~AttrActions(){
  27. }
  28. void AttrActions::GetAction(PVOID Addr)
  29. {
  30. m_pGetAddr = (REGISTATTRCHANGEADDR)Addr;
  31. }
  32. void AttrActions::SetAction(PVOID Addr)
  33. {
  34. m_pSetAddr = (REGISTATTRCHANGEADDR)Addr;
  35. }
  36. void AttrActions::AddAction(PVOID Addr)
  37. {
  38. m_pAddAddr = (REGISTATTRCHANGEADDR)Addr;
  39. }
  40. void AttrActions::DelAction(PVOID Addr)
  41. {
  42. m_pDelAddr = (REGISTATTRCHANGEADDR)Addr;
  43. }
  44. void AttrActions::UpdateAction(PVOID Addr)
  45. {
  46. m_pUpdateAddr = (REGISTATTRCHANGEADDR)Addr;
  47. }
  48. FuncLayer::FuncLayer(void)
  49. {
  50. m_pRegistedAttributes = new map<PVOID, map<string, AttrActions>>();
  51. m_pRegistedActions = new map<PVOID, map<string, PVOID>>();
  52. m_RegistDone = CreateEvent(0, 1, 0, 0);
  53. }
  54. FuncLayer::~FuncLayer(void)
  55. {
  56. delete m_pRegistedActions;
  57. delete m_pRegistedAttributes;
  58. CloseHandle(m_RegistDone);
  59. }
  60. //get device type
  61. bool SYSTEM_CALL FuncLayer::GetDeviceType(GUID &DevType)
  62. {
  63. //for test
  64. return string_2_guid(FUNCLAYERGUID, DevType);
  65. }
  66. bool SYSTEM_CALL FuncLayer::GetDeviceResourceAttrs(ResDataObject &Res)
  67. {
  68. bool ret = true;
  69. try{
  70. map<PVOID, map<string, AttrActions>>::iterator iter = m_pRegistedAttributes->begin();
  71. while (iter != m_pRegistedAttributes->end())
  72. {
  73. map<string, AttrActions>::iterator iterattr = iter->second.begin();
  74. while (iterattr != iter->second.end())
  75. {
  76. ResDataObject Context = (*(iterattr->second.m_pTarget));
  77. Res.add(iterattr->first.c_str(), Context);
  78. ++iterattr;
  79. }
  80. ++iter;
  81. }
  82. }
  83. catch (ResDataObjectExption &exp)
  84. {
  85. TPRINTA_ERROR(exp.what());
  86. ret = false;
  87. }
  88. catch (...)
  89. {
  90. TPRINTA_ERROR("Exeption happend in GetDeviceResourceAttrs");
  91. ret = false;
  92. }
  93. return ret;
  94. }
  95. bool SYSTEM_CALL FuncLayer::GetDeviceResourceActions(ResDataObject &Res)
  96. {
  97. bool ret = true;
  98. try{
  99. map<PVOID, map<string, PVOID>>::iterator iter = m_pRegistedActions->begin();
  100. while (iter != m_pRegistedActions->end())
  101. {
  102. map<string, PVOID>::iterator iteraction = iter->second.begin();
  103. while (iteraction != iter->second.end())
  104. {
  105. Res.add(iteraction->first.c_str(), "");
  106. ++iteraction;
  107. }
  108. ++iter;
  109. }
  110. }
  111. catch (ResDataObjectExption &exp)
  112. {
  113. TPRINTA_ERROR(exp.what());
  114. ret = false;
  115. }
  116. catch (...)
  117. {
  118. TPRINTA_ERROR("Exeption happend in GetDeviceResourceActions");
  119. ret = false;
  120. }
  121. return ret;
  122. }
  123. //get device resource
  124. RET_STATUS SYSTEM_CALL FuncLayer::GetDeviceResource(ResDataObject PARAM_OUT *pDeviceResource)
  125. {
  126. bool ret = true;
  127. if (m_RegistDone == false)
  128. {
  129. return RET_INVALID;
  130. }
  131. //attributes
  132. //make device type
  133. ret &= pDeviceResource->add("ClientType", DPC_UnitClient);
  134. GUID DeviceType;
  135. if (GetDeviceType(DeviceType))
  136. {
  137. string GuidStr;
  138. guid_2_string(DeviceType, GuidStr);
  139. ret &= pDeviceResource->add("DeviceType", GuidStr.c_str());
  140. }
  141. else
  142. {
  143. ret &= pDeviceResource->add("DeviceType", FUNCLAYERGUID);
  144. }
  145. //make attributes
  146. ResDataObject val;
  147. GetDeviceResourceAttrs(val);
  148. ret &= pDeviceResource->add("Attribute", val);
  149. ResDataObject Actions;
  150. GetDeviceResourceActions(Actions);
  151. if (ret)
  152. {
  153. pDeviceResource->add("Action", Actions);
  154. return RET_SUCCEED;
  155. }
  156. return RET_FAILED;
  157. }
  158. //notify to lower layer
  159. RET_STATUS SYSTEM_CALL FuncLayer::CmdToLogicDev(ResDataObject PARAM_IN *pCmd)
  160. {
  161. assert(0);//not happening
  162. return RET_FAILED;
  163. }
  164. //ResourceCommand Request In and Response Out
  165. RET_STATUS SYSTEM_CALL FuncLayer::Request(ResDataObject PARAM_IN *pRequest, ResDataObject PARAM_OUT *pResponse)
  166. {
  167. RET_STATUS ret = RET_INVALID;
  168. if (m_RegistDone == false)
  169. {
  170. return RET_INVALID;
  171. }
  172. ResDataObject res;
  173. string key = PacketAnalizer::GetPacketKey(pRequest);
  174. PACKET_CMD cmd = PacketAnalizer::GetPacketCmd(pRequest);
  175. if (cmd == PACKET_CMD_GET)
  176. {
  177. //get request
  178. ret = GET(key.c_str(), res);
  179. if (ret >= RET_SUCCEED)
  180. {
  181. pResponse->update("CONTEXT", res);
  182. }
  183. }
  184. else if ((cmd == PACKET_CMD_UPDATE) || (cmd == PACKET_CMD_ADD) || (cmd == PACKET_CMD_DEL) || (cmd == PACKET_CMD_PART_UPDATE))
  185. {
  186. //set request
  187. PacketAnalizer::GetPacketContext(pRequest, res);
  188. ret = Dispatch_AttributeChange(cmd,key.c_str(), res);
  189. //set no need context update
  190. }
  191. else if (cmd == PACKET_CMD_EXE)
  192. {
  193. //action request
  194. ResDataObject req;
  195. PacketAnalizer::GetPacketContext(pRequest, req);
  196. ret = Action(key.c_str(), req, res);
  197. if (ret >= RET_SUCCEED)
  198. {
  199. pResponse->update("CONTEXT", res);
  200. }
  201. }
  202. else
  203. {
  204. //ignore others
  205. ret = RET_NOSUPPORT;
  206. }
  207. PacketAnalizer::MakeRetCode(ret, pResponse);
  208. return ret;
  209. }
  210. bool SYSTEM_CALL FuncLayer::RegistFunc(PVOID pTarget, const char *pFuncName, PVOID FunctionAddr)
  211. {
  212. bool ret = true;
  213. if (WaitForSingleObject(m_RegistDone, 0) == WAIT_OBJECT_0 || strlen(pFuncName) == 0)
  214. {
  215. return false;
  216. }
  217. map<PVOID, map<string, PVOID>>::iterator iter = m_pRegistedActions->find(pTarget);
  218. if (iter == m_pRegistedActions->end())
  219. {
  220. //not exist.just assign
  221. (*m_pRegistedActions)[pTarget][pFuncName] = FunctionAddr;
  222. }
  223. else
  224. {
  225. //target exist
  226. map<string, PVOID>::iterator iterfunc = iter->second.find(pFuncName);
  227. if (iterfunc == iter->second.end())
  228. {
  229. //func not exist.just assin
  230. (*m_pRegistedActions)[pTarget][pFuncName] = FunctionAddr;
  231. }
  232. else
  233. {
  234. //func exist,no can do.
  235. ret = false;
  236. }
  237. }
  238. return ret;
  239. }
  240. bool SYSTEM_CALL FuncLayer::RegistAttributeGet(PVOID pTarget, ResDataNode *pDataNode, PVOID FunctionAddr)
  241. {
  242. bool ret = true;
  243. if (WaitForSingleObject(m_RegistDone, 0) == WAIT_OBJECT_0 || strlen(pDataNode->GetKey()) == 0)
  244. {
  245. return false;
  246. }
  247. map<PVOID, map<string, AttrActions>>::iterator iter = m_pRegistedAttributes->find(pTarget);
  248. if (iter == m_pRegistedAttributes->end())
  249. {
  250. //not exist.just assign
  251. pDataNode->SetLogicDevice(this);
  252. AttrActions attractions(pDataNode);
  253. attractions.GetAction(FunctionAddr);
  254. (*m_pRegistedAttributes)[pTarget][pDataNode->GetKey()] = attractions;
  255. }
  256. else
  257. {
  258. //target exist
  259. map<string, AttrActions>::iterator iterfunc = iter->second.find(pDataNode->GetKey());
  260. if (iterfunc == iter->second.end())
  261. {
  262. //key not exist.just assin
  263. pDataNode->SetLogicDevice(this);
  264. AttrActions attractions(pDataNode);
  265. attractions.GetAction(FunctionAddr);
  266. (*m_pRegistedAttributes)[pTarget][pDataNode->GetKey()] = attractions;
  267. }
  268. else
  269. {
  270. //target exist
  271. iterfunc->second.GetAction(FunctionAddr);
  272. }
  273. }
  274. return ret;
  275. }
  276. bool SYSTEM_CALL FuncLayer::RegistAttributeAdd(PVOID pTarget, ResDataNode *pDataNode, PVOID FunctionAddr)
  277. {
  278. bool ret = true;
  279. if (WaitForSingleObject(m_RegistDone, 0) == WAIT_OBJECT_0 || strlen(pDataNode->GetKey()) == 0)
  280. {
  281. return false;
  282. }
  283. map<PVOID, map<string, AttrActions>>::iterator iter = m_pRegistedAttributes->find(pTarget);
  284. if (iter == m_pRegistedAttributes->end())
  285. {
  286. //not exist.just assign
  287. pDataNode->SetLogicDevice(this);
  288. AttrActions attractions(pDataNode);
  289. attractions.AddAction(FunctionAddr);
  290. (*m_pRegistedAttributes)[pTarget][pDataNode->GetKey()] = attractions;
  291. }
  292. else
  293. {
  294. //target exist
  295. map<string, AttrActions>::iterator iterfunc = iter->second.find(pDataNode->GetKey());
  296. if (iterfunc == iter->second.end())
  297. {
  298. //key not exist.just assin
  299. pDataNode->SetLogicDevice(this);
  300. AttrActions attractions(pDataNode);
  301. attractions.AddAction(FunctionAddr);
  302. (*m_pRegistedAttributes)[pTarget][pDataNode->GetKey()] = attractions;
  303. }
  304. else
  305. {
  306. //target exist
  307. iterfunc->second.AddAction(FunctionAddr);
  308. }
  309. }
  310. return ret;
  311. }
  312. bool SYSTEM_CALL FuncLayer::RegistAttributeDel(PVOID pTarget, ResDataNode *pDataNode, PVOID FunctionAddr)
  313. {
  314. bool ret = true;
  315. if (WaitForSingleObject(m_RegistDone, 0) == WAIT_OBJECT_0 || strlen(pDataNode->GetKey()) == 0)
  316. {
  317. return false;
  318. }
  319. map<PVOID, map<string, AttrActions>>::iterator iter = m_pRegistedAttributes->find(pTarget);
  320. if (iter == m_pRegistedAttributes->end())
  321. {
  322. //not exist.just assign
  323. pDataNode->SetLogicDevice(this);
  324. AttrActions attractions(pDataNode);
  325. attractions.DelAction(FunctionAddr);
  326. (*m_pRegistedAttributes)[pTarget][pDataNode->GetKey()] = attractions;
  327. }
  328. else
  329. {
  330. //target exist
  331. map<string, AttrActions>::iterator iterfunc = iter->second.find(pDataNode->GetKey());
  332. if (iterfunc == iter->second.end())
  333. {
  334. //key not exist.just assin
  335. pDataNode->SetLogicDevice(this);
  336. AttrActions attractions(pDataNode);
  337. attractions.DelAction(FunctionAddr);
  338. (*m_pRegistedAttributes)[pTarget][pDataNode->GetKey()] = attractions;
  339. }
  340. else
  341. {
  342. //target exist
  343. iterfunc->second.DelAction(FunctionAddr);
  344. }
  345. }
  346. return ret;
  347. }
  348. bool SYSTEM_CALL FuncLayer::RegistAttributeSet(PVOID pTarget, ResDataNode *pDataNode, PVOID FunctionAddr)
  349. {
  350. bool ret = true;
  351. if (WaitForSingleObject(m_RegistDone, 0) == WAIT_OBJECT_0 || strlen(pDataNode->GetKey()) == 0)
  352. {
  353. return false;
  354. }
  355. map<PVOID, map<string, AttrActions>>::iterator iter = m_pRegistedAttributes->find(pTarget);
  356. if (iter == m_pRegistedAttributes->end())
  357. {
  358. //not exist.just assign
  359. pDataNode->SetLogicDevice(this);
  360. AttrActions attractions(pDataNode);
  361. attractions.SetAction(FunctionAddr);
  362. (*m_pRegistedAttributes)[pTarget][pDataNode->GetKey()] = attractions;
  363. }
  364. else
  365. {
  366. //target exist
  367. map<string, AttrActions>::iterator iterfunc = iter->second.find(pDataNode->GetKey());
  368. if (iterfunc == iter->second.end())
  369. {
  370. //key not exist.just assin
  371. pDataNode->SetLogicDevice(this);
  372. AttrActions attractions(pDataNode);
  373. attractions.SetAction(FunctionAddr);
  374. (*m_pRegistedAttributes)[pTarget][pDataNode->GetKey()] = attractions;
  375. }
  376. else
  377. {
  378. //target exist
  379. iterfunc->second.SetAction(FunctionAddr);
  380. }
  381. }
  382. return ret;
  383. }
  384. bool SYSTEM_CALL FuncLayer::RegistAttributeUpdate(PVOID pTarget, ResDataNode *pDataNode, PVOID FunctionAddr)
  385. {
  386. bool ret = true;
  387. if (WaitForSingleObject(m_RegistDone, 0) == WAIT_OBJECT_0 || strlen(pDataNode->GetKey()) == 0)
  388. {
  389. return false;
  390. }
  391. map<PVOID, map<string, AttrActions>>::iterator iter = m_pRegistedAttributes->find(pTarget);
  392. if (iter == m_pRegistedAttributes->end())
  393. {
  394. //not exist.just assign
  395. pDataNode->SetLogicDevice(this);
  396. AttrActions attractions(pDataNode);
  397. attractions.UpdateAction(FunctionAddr);
  398. (*m_pRegistedAttributes)[pTarget][pDataNode->GetKey()] = attractions;
  399. }
  400. else
  401. {
  402. //target exist
  403. map<string, AttrActions>::iterator iterfunc = iter->second.find(pDataNode->GetKey());
  404. if (iterfunc == iter->second.end())
  405. {
  406. //key not exist.just assin
  407. pDataNode->SetLogicDevice(this);
  408. AttrActions attractions(pDataNode);
  409. attractions.UpdateAction(FunctionAddr);
  410. (*m_pRegistedAttributes)[pTarget][pDataNode->GetKey()] = attractions;
  411. }
  412. else
  413. {
  414. //target exist
  415. iterfunc->second.UpdateAction(FunctionAddr);
  416. }
  417. }
  418. return ret;
  419. }
  420. void SYSTEM_CALL FuncLayer::SetRegistStatus(bool Done)
  421. {
  422. if (Done)
  423. {
  424. SetEvent(m_RegistDone);
  425. }
  426. else
  427. {
  428. ResetEvent(m_RegistDone);
  429. }
  430. }
  431. RET_STATUS DEVICE_ACTION FuncLayer::GET(const char *pszKey, ResDataObject &Context)
  432. {
  433. map<PVOID, map<string, AttrActions>>::iterator iter = m_pRegistedAttributes->begin();
  434. while (iter != m_pRegistedAttributes->end())
  435. {
  436. map<string, AttrActions>::iterator iterattr = iter->second.find(pszKey);
  437. if (iterattr != iter->second.end())
  438. {
  439. //found attr
  440. Context = (ResDataObject)(*(iterattr->second.m_pTarget));
  441. return RET_SUCCEED;
  442. }
  443. ++iter;
  444. }
  445. return RET_NOSUPPORT;
  446. }
  447. RET_STATUS DEVICE_ACTION FuncLayer::ADD(const char *pszKey, ResDataObject &Context)
  448. {
  449. return RET_NOSUPPORT;
  450. }
  451. RET_STATUS DEVICE_ACTION FuncLayer::DEL(const char *pszKey, ResDataObject &Context)
  452. {
  453. return RET_NOSUPPORT;
  454. }
  455. RET_STATUS DEVICE_ACTION FuncLayer::SET(const char *pszKey, ResDataObject &Context)
  456. {
  457. return RET_NOSUPPORT;
  458. }
  459. RET_STATUS DEVICE_ACTION FuncLayer::UPDATE(const char *pszKey, ResDataObject &Context)
  460. {
  461. return RET_NOSUPPORT;
  462. }
  463. RET_STATUS DEVICE_ACTION FuncLayer::Dispatch_AttributeChange(INT cmd, const char *pszKey, ResDataObject &Context)
  464. {
  465. map<PVOID, map<string, AttrActions>>::iterator iter = m_pRegistedAttributes->begin();
  466. while (iter != m_pRegistedAttributes->end())
  467. {
  468. map<string, AttrActions>::iterator iterattr = iter->second.find(pszKey);
  469. if (iterattr != iter->second.end())
  470. {
  471. //found attr
  472. if (cmd == PACKET_CMD_GET)
  473. {
  474. if (iterattr->second.m_pGetAddr)
  475. {
  476. REGISTATTRCHANGEADDR AttrFunc = (REGISTATTRCHANGEADDR)iterattr->second.m_pGetAddr;
  477. return AttrFunc(iter->first, Context);
  478. }
  479. //go normal routine
  480. return GET(pszKey, Context);
  481. }
  482. else if (cmd == PACKET_CMD_UPDATE)
  483. {
  484. if (iterattr->second.m_pSetAddr)
  485. {
  486. REGISTATTRCHANGEADDR AttrFunc = (REGISTATTRCHANGEADDR)iterattr->second.m_pSetAddr;
  487. return AttrFunc(iter->first, Context);
  488. }
  489. //go normal routine
  490. return SET(pszKey, Context);
  491. }
  492. else if (cmd == PACKET_CMD_ADD)
  493. {
  494. if (iterattr->second.m_pAddAddr)
  495. {
  496. REGISTATTRCHANGEADDR AttrFunc = (REGISTATTRCHANGEADDR)iterattr->second.m_pAddAddr;
  497. return AttrFunc(iter->first, Context);
  498. }
  499. //go normal routine
  500. return ADD(pszKey, Context);
  501. }
  502. else if (cmd == PACKET_CMD_DEL)
  503. {
  504. if (iterattr->second.m_pDelAddr)
  505. {
  506. REGISTATTRCHANGEADDR AttrFunc = (REGISTATTRCHANGEADDR)iterattr->second.m_pDelAddr;
  507. return AttrFunc(iter->first, Context);
  508. }
  509. //go normal routine
  510. return DEL(pszKey, Context);
  511. }
  512. else if (cmd == PACKET_CMD_PART_UPDATE)
  513. {
  514. if (iterattr->second.m_pUpdateAddr)
  515. {
  516. REGISTATTRCHANGEADDR AttrFunc = (REGISTATTRCHANGEADDR)iterattr->second.m_pUpdateAddr;
  517. return AttrFunc(iter->first, Context);
  518. }
  519. //go normal routine
  520. return UPDATE(pszKey, Context);
  521. }
  522. else
  523. {
  524. //do nothing
  525. }
  526. //got match and done here
  527. break;
  528. }
  529. ++iter;
  530. }
  531. return RET_NOSUPPORT;
  532. }
  533. RET_STATUS DEVICE_ACTION FuncLayer::Action(const char *pszKey, ResDataObject &req, ResDataObject &res)
  534. {
  535. map<PVOID, map<string, PVOID>>::iterator iter = m_pRegistedActions->begin();
  536. while (iter != m_pRegistedActions->end())
  537. {
  538. map<string, PVOID>::iterator iteraction = iter->second.find(pszKey);
  539. if (iteraction != iter->second.end())
  540. {
  541. //found action
  542. REGISTFUNCTYPE0 actionfunc = (REGISTFUNCTYPE0)iteraction->second;
  543. return actionfunc(iter->first, req, res);
  544. }
  545. ++iter;
  546. }
  547. return RET_NOSUPPORT;
  548. }
  549. RET_STATUS DEVICE_ACTION FuncLayer::PostNotify(ResDataObject &Notify)
  550. {
  551. //暂时用继承方式,后续调整为引用.
  552. return CmdFromLogicDev(&Notify);
  553. }