ContainerDPC.cpp 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923
  1. // ContainerDPC.cpp : 定义 DLL 应用程序的导出函数。
  2. //
  3. #include "ContainerDPC.h"
  4. #include "DevTree.h"
  5. #include "ContainerDevice.h"
  6. #include "common_api.h"
  7. #include <iostream>
  8. #include <sstream>
  9. #include <dlfcn.h> // Linux 动态加载库头文件
  10. #include <cstring> // 用于 strlen 等操作
  11. #include "LocalConfig.h"
  12. #include "LogLocalHelper.h"
  13. #include "Log4CPP.h"
  14. //CONTAINERDPC_C_API DriverDPC* GetDriverDPC()
  15. //{
  16. // return (DriverDPC*)(new ContainerDPC());
  17. //}
  18. //
  19. //CONTAINERDPC_C_API void ReleaseDriverDPC(DriverDPC *p)
  20. //{
  21. // ContainerDPC *org = (ContainerDPC*)p;
  22. // delete org;
  23. //}
  24. //typedef nsDEV::IODriver* (*GetDriverMgrAPI)();
  25. ContainerDPC::ContainerDPC(void)
  26. {
  27. m_pWorkpath = new std::string();
  28. //m_pMidObject = 0;
  29. //m_pMidObject.reset(new eDEV::Mediator());
  30. memset(&m_ContainerManager, 0, sizeof(m_ContainerManager));
  31. }
  32. ContainerDPC::~ContainerDPC(void)
  33. {
  34. delete m_pWorkpath;
  35. }
  36. bool ContainerDPC::InitDeviceMgr(const char *pDriverpath)
  37. {
  38. m_DriverFunctions = GetDllFunctionInfo(pDriverpath);
  39. for (const auto& function : m_DriverFunctions)
  40. {
  41. std::cout << "Function Name: " << std::get<0>(function) << std::endl;
  42. std::cout << "Function Address: " << std::get<1>(function) << std::endl;
  43. std::cout << "Function Ordinal: " << std::get<2>(function) << std::endl;
  44. std::cout << std::endl;
  45. }
  46. // 加载动态库:RTLD_NOW 表示立即解析符号
  47. m_Module = dlopen(pDriverpath, RTLD_NOW);
  48. if (m_Module)
  49. {
  50. /*----------------------------------------------------------
  51. 加载驱动管理器核心函数
  52. ----------------------------------------------------------*/
  53. //枚举所有的导出函数
  54. m_ContainerManager.DRV_DriverEntry = (BoolCallString)dlsym(m_Module, "DRV_DriverEntry");
  55. m_ContainerManager.DRV_Prepare = (VoidCall)dlsym(m_Module, "DRV_Prepare");
  56. //m_ContainerManager.DRV_DriverProbe = (StringCall)dlsym(m_Module, "DRV_DriverProbe");
  57. //m_ContainerManager.DRV_GetGUID = (StringCall)dlsym(m_Module, "DRV_GetGUID");
  58. //m_ContainerManager.DRV_GetResource = (StringCall)dlsym(m_Module, "DRV_GetResource");
  59. //m_ContainerManager.DRV_DeviceProbe = (StringCall)dlsym(m_Module, "DRV_DeviceProbe");
  60. m_ContainerManager.DRV_OnHeartBeat = (BoolCall)dlsym(m_Module, "DRV_OnHeartBeat");
  61. m_ContainerManager.DRV_ReLoadConfig = (BoolCall)dlsym(m_Module, "DRV_ReLoadConfig");
  62. //m_ContainerManager.DRV_Connect = (BoolCall)dlsym(m_Module, "DRV_Connect");
  63. //m_ContainerManager.DRV_Disconnect = (VoidCall)dlsym(m_Module, "DRV_Disconnect");
  64. m_ContainerManager.DRV_CreateDevice = (VoindPointerCallInt)dlsym(m_Module, "DRV_CreateDevice");
  65. //m_ContainerManager.DRV_FreeDevice = (HandleCall)dlsym(m_Module, "DRV_FreeDevice");
  66. //m_ContainerManager.DRV_GetDeviceConfig = (BoolCallString)dlsym(m_Module, "DRV_GetDeviceConfig");
  67. //m_ContainerManager.DRV_SetDeviceConfig = (BoolCallStringRef)dlsym(m_Module, "DRV_SetDeviceConfig");
  68. m_ContainerManager.DRV_Get = (StdGetCall)dlsym(m_Module, "DRV_Get");
  69. //m_ContainerManager. = ()dlsym(m_Module, "");
  70. m_ContainerManager.GetCallTmplMap.clear();
  71. /*
  72. */
  73. m_ContainerDeviceManager.DEV_GetGUID = (GetDevCall)dlsym(m_Module, "DEV_GetGUID");
  74. m_ContainerDeviceManager.DEV_GetResource = (GetDevCall)dlsym(m_Module, "DEV_GetResource");
  75. m_ContainerDeviceManager.DEV_CompleteInit = (VoidDevCall)dlsym(m_Module, "DEV_CompleteInit");
  76. m_ContainerDeviceManager.DEV_Prepare = (BoolDevCall)dlsym(m_Module, "DEV_Prepare");
  77. m_ContainerDeviceManager.DEV_CompleteUnInit = (VoidDevCall)dlsym(m_Module, "DEV_CompleteUnInit");
  78. //m_ContainerDeviceManager.DEV_SubscribeSelf = (VoidDevCall)dlsym(m_Module, "DEV_SubscribeSelf");
  79. m_ContainerDeviceManager.DEV_Add = (DevAddCall)dlsym(m_Module, "DEV_Add");
  80. m_ContainerDeviceManager.Dev_Delete = (DevDeleteCall)dlsym(m_Module, "Dev_Delete");
  81. m_ContainerDeviceManager.Dev_Update = (DevUpdateCall)dlsym(m_Module, "Dev_Update");
  82. m_ContainerDeviceManager.Dev_Action = (DevActionCall)dlsym(m_Module, "Dev_Action");
  83. m_ContainerDeviceManager.Dev_Get = (DevGetCall)dlsym(m_Module, "Dev_Get");
  84. m_ContainerDeviceManager.Dev_Set = (DevSetCall)dlsym(m_Module, "Dev_Set");
  85. m_ContainerDeviceManager.DevPath_Add = (DevPathAddCall)dlsym(m_Module, "DevPath_Add");
  86. m_ContainerDeviceManager.DevPath_Delete = (DevPathDeleteCall)dlsym(m_Module, "DevPath_Delete");
  87. m_ContainerDeviceManager.DevPath_Update = (DevPathUpdateCall)dlsym(m_Module, "DevPath_Update");
  88. m_ContainerDeviceManager.DevPath_Action = (DevPathActionCall)dlsym(m_Module, "DevPath_Action");
  89. m_ContainerDeviceManager.DevPath_Get = (DevPathGetCall)dlsym(m_Module, "DevPath_Get");
  90. m_ContainerDeviceManager.DevPath_Set = (DevPathSetCall)dlsym(m_Module, "DevPath_Set");
  91. for (const auto& function : m_DriverFunctions)
  92. {
  93. string funcName = std::get<0>(function);
  94. std::cout << "Function Name: " << funcName << std::endl;
  95. const size_t DRV_GET_STRLEN = strlen("DRV_GET_");
  96. if (funcName.substr(0, DRV_GET_STRLEN) == "DRV_GET_")
  97. {
  98. m_ContainerManager.GetCallTmplMap[funcName.substr(DRV_GET_STRLEN)] = (TmplGetCall)dlsym(m_Module, funcName.c_str());
  99. }
  100. /*
  101. */
  102. const size_t DEV_Add_STRLEN = strlen("DEV_Add_");
  103. string keys = funcName.substr(0, DEV_Add_STRLEN);
  104. if (keys == "DEV_Add_")
  105. {
  106. m_ContainerDeviceManager.DevAddTmplMap[funcName.substr(DEV_Add_STRLEN)] = (DevActionTmplCall)dlsym(m_Module, funcName.c_str());
  107. }
  108. else if (keys == "DEV_Dele")
  109. {
  110. const size_t DEV_Delete_STRLEN = strlen("DEV_Delete_");
  111. m_ContainerDeviceManager.DevDeleteTmplMap[funcName.substr(DEV_Delete_STRLEN)] = (DevActionTmplCall)dlsym(m_Module, funcName.c_str());
  112. }
  113. else if (keys == "DEV_Upda")
  114. {
  115. const size_t DEV_Update_STRLEN = strlen("DEV_Update_");
  116. m_ContainerDeviceManager.DevUpdateTmplMap[funcName.substr(DEV_Update_STRLEN)] = (DevActionTmplCall)dlsym(m_Module, funcName.c_str());
  117. }
  118. else if (keys == "DEV_Acti")
  119. {
  120. const size_t DEV_Action_STRLEN = strlen("DEV_Action_");
  121. m_ContainerDeviceManager.DevActionTmplMap[funcName.substr(DEV_Action_STRLEN)] = (DevActionTmplCall)dlsym(m_Module, funcName.c_str());
  122. }
  123. else if (keys == "DEV_Get_")
  124. {
  125. m_ContainerDeviceManager.DevGetTmplMap[funcName.substr(DEV_Add_STRLEN)] = (DevGetTmplCall)dlsym(m_Module, funcName.c_str());
  126. }
  127. else if (keys == "DEV_Set_")
  128. {
  129. m_ContainerDeviceManager.DevSetTmplMap[funcName.substr(DEV_Add_STRLEN)] = (DevSetTmplCall)dlsym(m_Module, funcName.c_str());
  130. }
  131. else if (keys == "DEVPath_")
  132. {
  133. //与patch有关的,需要进一步判断
  134. /*
  135. */
  136. const size_t DEVPATH_Add_STRLEN = strlen("DEVPath_Add_");
  137. string pathkeys = funcName.substr(0, DEVPATH_Add_STRLEN);
  138. size_t funcNameLen = funcName.length();
  139. if (pathkeys == "DEVPath_Add_")
  140. {
  141. if (funcName[funcNameLen - 1] == '_')
  142. {
  143. //根设备的模型方法 DEVPath_Add_ XXX_
  144. m_ContainerDeviceManager.LogicDevAddTmpMap[funcName.substr(DEVPATH_Add_STRLEN)] = (DevTemplPathActionTmplCall)dlsym(m_Module, funcName.c_str());
  145. }
  146. else
  147. {
  148. size_t findNamePos = funcName.substr(DEVPATH_Add_STRLEN).find_first_of('_');
  149. if (findNamePos == string::npos)
  150. {
  151. //DEVPath_Add_XXX ,入口参数第一个是设备路径
  152. m_ContainerDeviceManager.DevPathAddTmplMap[funcName.substr(DEVPATH_Add_STRLEN)] = (DevPathActionTmplCall)dlsym(m_Module, funcName.c_str());
  153. }
  154. else
  155. {
  156. //DEVPath_Add_ XXX_Dev01_SubDev
  157. m_ContainerDeviceManager.LogicDevAddTmpMap[funcName.substr(DEVPATH_Add_STRLEN)] = (DevTemplPathActionTmplCall)dlsym(m_Module, funcName.c_str());
  158. }
  159. }
  160. }
  161. else if (pathkeys == "DEVPath_Dele")
  162. {
  163. const size_t DEV_Delete_STRLEN = strlen("DEVPath_Delete_");
  164. if (funcName[funcNameLen - 1] == '_')
  165. {
  166. //根设备的模型方法 DEVPath_Delete_ XXX_
  167. m_ContainerDeviceManager.LogicDevDeleteTmpMap[funcName.substr(DEV_Delete_STRLEN)] = (DevTemplPathActionTmplCall)dlsym(m_Module, funcName.c_str());
  168. }
  169. else
  170. {
  171. size_t findNamePos = funcName.substr(DEV_Delete_STRLEN).find_first_of('_');
  172. if (findNamePos == string::npos)
  173. {
  174. //DEVPath_Delete_XXX ,入口参数第一个是设备路径
  175. m_ContainerDeviceManager.DevPathDeleteTmplMap[funcName.substr(DEV_Delete_STRLEN)] = (DevPathActionTmplCall)dlsym(m_Module, funcName.c_str());
  176. }
  177. else
  178. {
  179. //DEVPath_Delete_ XXX_Dev01_SubDev
  180. m_ContainerDeviceManager.LogicDevDeleteTmpMap[funcName.substr(DEV_Delete_STRLEN)] = (DevTemplPathActionTmplCall)dlsym(m_Module, funcName.c_str());
  181. }
  182. }
  183. }
  184. else if (pathkeys == "DEVPath_Upda")
  185. {
  186. const size_t DEV_Update_STRLEN = strlen("DEVPath_Update_");
  187. if (funcName[funcNameLen - 1] == '_')
  188. {
  189. //根设备的模型方法 DEVPath_Update_ XXX_
  190. m_ContainerDeviceManager.LogicDevUpdateTmpMap[funcName.substr(DEV_Update_STRLEN)] = (DevTemplPathActionTmplCall)dlsym(m_Module, funcName.c_str());
  191. }
  192. else
  193. {
  194. size_t findNamePos = funcName.substr(DEV_Update_STRLEN).find_first_of('_');
  195. if (findNamePos == string::npos)
  196. {
  197. //DEVPath_Update_XXX ,入口参数第一个是设备路径
  198. m_ContainerDeviceManager.DevPathUpdateTmplMap[funcName.substr(DEV_Update_STRLEN)] = (DevPathActionTmplCall)dlsym(m_Module, funcName.c_str());
  199. }
  200. else
  201. {
  202. //DEVPath_Update_ XXX_Dev01_SubDev
  203. m_ContainerDeviceManager.LogicDevUpdateTmpMap[funcName.substr(DEV_Update_STRLEN)] = (DevTemplPathActionTmplCall)dlsym(m_Module, funcName.c_str());
  204. }
  205. }
  206. }
  207. else if (pathkeys == "DEVPath_Acti")
  208. {
  209. const size_t DEV_Action_STRLEN = strlen("DEVPath_Action_");
  210. if (funcName[funcNameLen - 1] == '_')
  211. {
  212. //根设备的模型方法 DEVPath_Action_ XXX_
  213. m_ContainerDeviceManager.LogicDevActionTmpMap[funcName.substr(DEV_Action_STRLEN)] = (DevTemplPathActionTmplCall)dlsym(m_Module, funcName.c_str());
  214. }
  215. else
  216. {
  217. size_t findNamePos = funcName.substr(DEV_Action_STRLEN).find_first_of('_');
  218. if (findNamePos == string::npos)
  219. {
  220. //DEVPath_Update_XXX ,入口参数第一个是设备路径
  221. m_ContainerDeviceManager.DevPathActionTmplMap[funcName.substr(DEV_Action_STRLEN)] = (DevPathActionTmplCall)dlsym(m_Module, funcName.c_str());
  222. }
  223. else
  224. {
  225. //DEVPath_Update_ XXX_Dev01_SubDev
  226. m_ContainerDeviceManager.LogicDevActionTmpMap[funcName.substr(DEV_Action_STRLEN)] = (DevTemplPathActionTmplCall)dlsym(m_Module, funcName.c_str());
  227. }
  228. }
  229. }
  230. else if (pathkeys == "DEVPath_Get_")
  231. {
  232. if (funcName[funcNameLen - 1] == '_')
  233. {
  234. //根设备的模型方法 DEVPath_Get_ XXX_
  235. m_ContainerDeviceManager.LogicDevGetTmpMap[funcName.substr(DEVPATH_Add_STRLEN)] = (DevTemplPathGetTmplCall)dlsym(m_Module, funcName.c_str());
  236. }
  237. else
  238. {
  239. size_t findNamePos = funcName.substr(DEVPATH_Add_STRLEN).find_first_of('_');
  240. if (findNamePos == string::npos)
  241. {
  242. //DEVPath_Get_XXX ,入口参数第一个是设备路径
  243. m_ContainerDeviceManager.DevPathGetTmplMap[funcName.substr(DEVPATH_Add_STRLEN)] = (DevPathGetTmplCall)dlsym(m_Module, funcName.c_str());
  244. }
  245. else
  246. {
  247. //DEVPath_Get_ XXX_Dev01_SubDev
  248. m_ContainerDeviceManager.LogicDevGetTmpMap[funcName.substr(DEVPATH_Add_STRLEN)] = (DevTemplPathGetTmplCall)dlsym(m_Module, funcName.c_str());
  249. }
  250. }
  251. }
  252. else if (pathkeys == "DEVPath_Set_")
  253. {
  254. if (funcName[funcNameLen - 1] == '_')
  255. {
  256. //根设备的模型方法 DEVPath_Set_ XXX_
  257. m_ContainerDeviceManager.LogicDevSetTmpMap[funcName.substr(DEVPATH_Add_STRLEN)] = (DevTemplPathSetTmplCall)dlsym(m_Module, funcName.c_str());
  258. }
  259. else
  260. {
  261. size_t findNamePos = funcName.substr(DEVPATH_Add_STRLEN).find_first_of('_');
  262. if (findNamePos == string::npos)
  263. {
  264. //DEVPath_Set_XXX ,入口参数第一个是设备路径
  265. m_ContainerDeviceManager.DevPathSetTmplMap[funcName.substr(DEVPATH_Add_STRLEN)] = (DevPathSetTmplCall)dlsym(m_Module, funcName.c_str());
  266. }
  267. else
  268. {
  269. //DEVPath_Set_ XXX_Dev01_SubDev
  270. m_ContainerDeviceManager.LogicDevSetTmpMap[funcName.substr(DEVPATH_Add_STRLEN)] = (DevTemplPathSetTmplCall)dlsym(m_Module, funcName.c_str());
  271. }
  272. }
  273. }
  274. }
  275. }
  276. //GetDriverMgrAPI callfunc = (GetDriverMgrAPI)dlsym(m_Module, "CreateIODriver");
  277. //if (callfunc)
  278. //{
  279. // auto pDriver = reinterpret_cast <nsDEV::IODriver*> (callfunc());
  280. // if (pDriver)
  281. // {
  282. // m_NewDevManager.reset(pDriver);
  283. // //succeed
  284. // return true;
  285. // }
  286. // else
  287. // {
  288. // DWORD errNo = GetLastError();
  289. // FINFO(("lib:%s no Object returned.ErrNo:%d\n", pDriverpath, errNo);
  290. // }
  291. //}
  292. //else
  293. //{
  294. // DWORD errNo = GetLastError();
  295. // FINFO(("lib:%s no CreateIODriver entry.ErrNo:%d\n", pDriverpath, errNo);
  296. //}
  297. return CheckDriver();
  298. }
  299. else
  300. {
  301. // Linux 错误处理:使用 dlerror() 获取错误信息
  302. const char* errMsg = dlerror();
  303. std::cerr << "Load lib:" << pDriverpath << " failed. Error: "
  304. << (errMsg ? errMsg : "Unknown error") << std::endl;
  305. return false;
  306. }
  307. return false;
  308. }
  309. bool ContainerDPC::CheckDriver()
  310. {
  311. bool bOK = true;
  312. bOK &= m_ContainerManager.DRV_DriverEntry != NULL;
  313. bOK &= m_ContainerManager.DRV_Prepare != NULL;
  314. //bOK &= m_ContainerManager.DRV_DriverProbe != NULL;
  315. //bOK &= m_ContainerManager.DRV_GetGUID != NULL;
  316. //bOK &= m_ContainerManager.DRV_GetResource != NULL;
  317. //bOK &= m_ContainerManager.DRV_DeviceProbe != NULL;
  318. bOK &= m_ContainerManager.DRV_OnHeartBeat != NULL;
  319. bOK &= m_ContainerManager.DRV_ReLoadConfig != NULL;
  320. //bOK &= m_ContainerManager.DRV_Connect != NULL;
  321. //bOK &= m_ContainerManager.DRV_Disconnect != NULL;
  322. bOK &= m_ContainerManager.DRV_CreateDevice != NULL;
  323. bOK &= m_ContainerManager.DRV_FreeDevice != NULL;
  324. //bOK &= m_ContainerManager.DRV_GetDeviceConfig != NULL;
  325. //bOK &= m_ContainerManager.DRV_SetDeviceConfig != NULL;
  326. bOK &= m_ContainerManager.DRV_Get != NULL;
  327. return bOK;
  328. }
  329. bool ContainerDPC::DriverEntry(ResDataObject &Configuration)
  330. {
  331. m_Config = Configuration;
  332. const char *pDriverpath = Configuration["oemdriver"];
  333. if (pDriverpath == 0 || strlen(pDriverpath) == 0)
  334. {
  335. FINFO("context of oemdriver is empty");
  336. return false;
  337. }
  338. if (InitDeviceMgr(pDriverpath) == false)
  339. {
  340. FINFO("CreateIODriver Failed.{$}",pDriverpath);
  341. return false;
  342. }
  343. bool ret = LogicDriver::DriverEntry(Configuration);
  344. string filepath = GetDriverConfigFilePath();
  345. m_strConfigFilePath = filepath;
  346. if (ret)
  347. {
  348. ret = m_ContainerManager.DRV_DriverEntry(filepath);
  349. //ret = m_NewDevManager->DriverEntry(filepath);
  350. m_ContainerManager.DRV_Prepare();
  351. //m_NewDevManager->Prepare();
  352. }
  353. else
  354. {
  355. FINFO("LogicDriver::DriverEntry Failed.{$}", filepath.c_str());
  356. }
  357. return ret;
  358. }
  359. bool ContainerDPC::GetDeviceType(GUID &DevType)
  360. {
  361. string DriverType = "";
  362. try
  363. {
  364. DriverType = (string)m_Config["GUID"];//m_ContainerManager.DRV_GetGUID(); // m_NewDevManager->GetGUID();
  365. }
  366. catch (...)
  367. {
  368. }
  369. if (DriverType.size() == 0)
  370. {
  371. FINFO("GetDeviceType Failed");
  372. return false;
  373. }
  374. //if (m_pNewDevManager->GetDriverType(DriverType) == false)
  375. //{
  376. // return false;
  377. //}
  378. return string_2_guid(DriverType.c_str(), DevType);
  379. }
  380. string DEVICE_ID = "";
  381. string TOPIC_PREFIX = "";
  382. bool SYSTEM_CALL ContainerDPC::Driver_Probe(ResDataObject& PARAM_OUT HardwareInfo)
  383. {
  384. //string DriverInfo = "";// m_NewDevManager->DriverProbe();
  385. //if (m_pNewDevManager->Driver_Probe(DriverInfo))
  386. {
  387. ResDataObject Config = m_Config;
  388. //if (Config.decode(DriverInfo.c_str()))
  389. {
  390. HardwareInfo.add("MajorID", (const char*)Config["MajorID"]);
  391. HardwareInfo.add("MinorID", (const char*)Config["MinorID"]);
  392. HardwareInfo.add("VendorID", (const char*)Config["VendorID"]);
  393. HardwareInfo.add("ProductID", (const char*)Config["ProductID"]);
  394. HardwareInfo.add("SerialID", (const char*)Config["SerialID"]);
  395. ostringstream os,os2;
  396. os << (const char*)Config["VendorID"] << "_" << (const char*)Config["ProductID"] << "_" << (const char*)Config["SerialID"];
  397. DEVICE_ID = os.str();
  398. os2 << "" << (const char*)Config["MajorID"] ;//<< "/" << (const char*)Config["VendorID"] << "/" << (const char*)Config["ProductID"];
  399. os2 << "/" << (const char*)Config["SerialID"] << "/";
  400. TOPIC_PREFIX = os2.str();
  401. return true;
  402. }
  403. }
  404. return false;
  405. }
  406. /// <summary>
  407. /// 读取配置项的所有key:value
  408. /// </summary>
  409. /// <returns></returns>
  410. ResDataObject GetDriverConfigration(CCOS_DRIVER_OBJ* pDriver);
  411. RET_STATUS ContainerDPC::GetDeviceResource(ResDataObject *pDeviceResource)
  412. {
  413. RET_STATUS ret = RET_FAILED;
  414. string ResInfo = "";// m_NewDevManager->GetResource();
  415. //if (m_pNewDevManager->GetDriverResource(ResInfo))
  416. {
  417. //low layerInfo
  418. ResDataObject LowLayerConfig;
  419. if (LowLayerConfig.decode(ResInfo.c_str()) == false)
  420. {
  421. FINFO("GetDriverResource Failed");
  422. return ret;
  423. }
  424. //base LogicDriverinfo
  425. ResDataObject BaseLayerConfig;
  426. ret = LogicDriver::GetDeviceResource(&BaseLayerConfig);
  427. if (ret < RET_SUCCEED)
  428. {
  429. return ret;
  430. }
  431. //配置文件属性,WEBconfig可以读/写,只读属性属于初始配置值,不能修改
  432. BaseLayerConfig["Configrations"] = GetDriverConfigration(&m_ContainerManager);
  433. //驱动属性,GET
  434. //BaseLayerConfig.update("Attribute", GetDriverAttribute());
  435. ////驱动可写属性 SET
  436. //BaseLayerConfig.update("Set", GetDriverWritableAttribute());
  437. ////update
  438. //BaseLayerConfig.update("Update", GetDriverUpdateItems());
  439. ////add
  440. //BaseLayerConfig.update("Add", GetDriverAddItems());
  441. ////delete
  442. //BaseLayerConfig.update("Delete", GetDriverDeleteItems());
  443. ////action
  444. //BaseLayerConfig.update("Action", GetDriverActionItems());
  445. if (LowLayerConfig.GetFirstOf("Attribute") >= 0)
  446. {
  447. //loop Low Layer Attribute
  448. for (size_t i = 0; i < LowLayerConfig["Attribute"].size(); i++)
  449. {
  450. const char *pKey = LowLayerConfig["Attribute"].GetKey(i);
  451. if (pKey)
  452. {
  453. int BaseIdx = BaseLayerConfig["Attribute"].GetFirstOf(pKey);
  454. if (BaseIdx >= 0)
  455. {
  456. //exist then overwrite
  457. BaseLayerConfig["Attribute"][BaseIdx] = LowLayerConfig["Attribute"][i];
  458. }
  459. else
  460. {
  461. //not exist then add
  462. BaseLayerConfig["Attribute"].add(pKey, LowLayerConfig["Attribute"][i]);
  463. }
  464. }
  465. }
  466. }
  467. if (LowLayerConfig.GetFirstOf("Action") >= 0)
  468. {
  469. //loop Low Layer Action
  470. for (size_t i = 0; i < LowLayerConfig["Action"].size(); i++)
  471. {
  472. const char *pKey = LowLayerConfig["Action"].GetKey(i);
  473. if (pKey)
  474. {
  475. int BaseIdx = BaseLayerConfig["Action"].GetFirstOf(pKey);
  476. if (BaseIdx >= 0)
  477. {
  478. //exist then overwrite
  479. BaseLayerConfig["Action"][BaseIdx] = LowLayerConfig["Action"][i];
  480. }
  481. else
  482. {
  483. //not exist then add
  484. BaseLayerConfig["Action"].add(pKey, LowLayerConfig["Action"][i]);
  485. }
  486. }
  487. }
  488. }
  489. (*pDeviceResource) = BaseLayerConfig;
  490. return RET_SUCCEED;
  491. }
  492. return ret;
  493. }
  494. /*
  495. void ContainerDPC::SubscribeSelf() {
  496. //这里订阅topic
  497. }*/
  498. bool ContainerDPC::Device_Probe(ResDataObject &HardwareInfo)
  499. {
  500. string DeviceInfo = ""; // m_NewDevManager->DeviceProbe();
  501. //if (m_pNewDevManager->Device_Probe(DeviceInfo))
  502. {
  503. ResDataObject Config = m_Config;
  504. //if (Config.decode(DeviceInfo.c_str()))
  505. {
  506. HardwareInfo.add("MajorID", (const char*)Config["MajorID"]);
  507. HardwareInfo.add("MinorID", (const char*)Config["MinorID"]);
  508. HardwareInfo.add("VendorID", (const char*)Config["VendorID"]);
  509. HardwareInfo.add("ProductID", (const char*)Config["ProductID"]);
  510. HardwareInfo.add("SerialID", (const char*)Config["SerialID"]);
  511. return true;
  512. }
  513. }
  514. return false;
  515. }
  516. bool ContainerDPC::SetDeviceWorkPath(const char *pWorkPath)
  517. {
  518. return true;
  519. }
  520. bool ContainerDPC::SetDriverWorkPath(const char *pWorkPath)
  521. {
  522. (*m_pWorkpath) = pWorkPath;
  523. //要把读取单一硬件属性的过程放到此处
  524. return true;
  525. }
  526. bool ContainerDPC::Connect()
  527. {
  528. bool bret = true;// m_ContainerManager.DRV_Connect(); // m_NewDevManager->Connect();
  529. if (bret)
  530. {
  531. LogicDriver::Connect();//make sure it's connected
  532. FINFO("Connect Succeed");
  533. return true;
  534. }
  535. else
  536. {
  537. FINFO("Connect Failed");
  538. }
  539. return false;
  540. }
  541. bool SYSTEM_CALL ContainerDPC::GetConnectionStatus()
  542. {
  543. return false; // m_NewDevManager->isConnected();
  544. }
  545. void ContainerDPC::DisConnect()
  546. {
  547. //m_NewDevManager->Disconnect();
  548. //m_ContainerManager.DRV_Disconnect();
  549. LogicDriver::DisConnect();//make sure it's Disconnected
  550. FINFO("DisConnect Succeed");
  551. }
  552. bool SYSTEM_CALL ContainerDPC::OnHeartBeat()
  553. {
  554. return m_ContainerManager.DRV_OnHeartBeat(); // m_NewDevManager->OnHeartBeat();
  555. }
  556. PVOID ContainerDPC::LoadLogicDevices()
  557. {
  558. std::cout << "**************** ContainerDPC :: LoadLogicDevices" << std::endl;
  559. FINFO( "Load Logic Devices");
  560. int nDeviceNumber = 1;
  561. string filepath = GetDriverConfigFilePath();
  562. ResDataObject Config;
  563. std::cout << "**************** ContainerDPC Load Config " << filepath << std::endl;
  564. if (Config.loadFile(filepath.c_str()))
  565. {
  566. int nHaveDeviceNumber = (int)Config["CONFIGURATION"].GetKeyCount("DeviceNumber");
  567. if (nHaveDeviceNumber > 0)
  568. {
  569. nDeviceNumber = Config["CONFIGURATION"]["DeviceNumber"];
  570. FINFO("Have %d Devices", nDeviceNumber);
  571. }
  572. else
  573. {
  574. FINFO("Have no DeviceNumber Attribute");
  575. }
  576. }
  577. else
  578. {
  579. FINFO("Load config file failed %d",filepath.c_str());
  580. }
  581. DevTree *pTree = (DevTree *)GetDeviceTree();
  582. for (int i = 0; i < nDeviceNumber; i++)
  583. {
  584. ContainerDevice *p = new ContainerDevice();
  585. std::cout << " ****************ContainerDPC Try new Device " << i << " of " << nDeviceNumber << std::endl;
  586. //auto pIoDevice = m_NewDevManager->CreateDevice(i);
  587. DEVICE_HANDLE pContainerDevice = m_ContainerManager.DRV_CreateDevice(i);
  588. if (pContainerDevice)
  589. {
  590. GUID guid2;
  591. string guidname;
  592. //((LogicDevice*)pChild)->GetDeviceType(guid);
  593. //bool bget = pIoDevice->GetDeviceType(guid2);
  594. guidname = m_ContainerDeviceManager.DEV_GetGUID(pContainerDevice);// pIoDevice->GetGUID();
  595. //guid_2_string(guid2, guidname);
  596. ResDataObject subpath;
  597. subpath = (guidname).c_str();
  598. ResDataObject probe,conn_usless;
  599. Device_Probe(probe);
  600. string fullpath = MakeDevicePath(probe, conn_usless, subpath).encode();
  601. string ccospath = MakeCcosPath(probe, conn_usless, subpath).encode();
  602. //pDevice->SetEbusRoot(fullpath.c_str());
  603. p->SetClientRootID(fullpath.c_str(), ccospath.c_str());
  604. //p->SetClientRootID();
  605. FINFO("Get IoDevice Succeed");
  606. std::shared_ptr<LinuxEvent> DisconnectEvt = GetPassiveDisConnectEvtHandle();
  607. //p->ConnectMQTTServer();
  608. p->Init(&m_ContainerDeviceManager, pContainerDevice, DisconnectEvt);
  609. //p->SubscribeAction();
  610. FINFO("Create IoDevice Succeed");
  611. }
  612. else
  613. {
  614. // FERROR("Create IoDevice Failed");
  615. // delete p;
  616. // return NULL;
  617. }
  618. LogicDevice *pret = (LogicDevice *)p;
  619. pTree->Add((PVOID)pret, TYPE_DEVICE);
  620. LogicDevice::AddEbusChildren(p, "");
  621. }
  622. return (PVOID)pTree;
  623. }
  624. void ContainerDPC::UnloadLogicDevices()
  625. {
  626. FINFO( "UnLoad Logic Devices");
  627. DevTree *pTree = (DevTree *)GetDeviceTree();
  628. size_t Size = pTree->size();
  629. for (size_t i = 0; i < Size; i++)
  630. {
  631. DevTreeNode node = (*pTree)[i];
  632. if (node.m_NodeType == TYPE_DEVICE)
  633. {
  634. ContainerDevice *pdev = (ContainerDevice*)node.m_pObject;
  635. delete pdev;
  636. }
  637. else
  638. {
  639. //tree dev
  640. //do not copy this!!!!
  641. //UnloadLogicDevices(node.m_pObject);
  642. }
  643. }
  644. pTree->clear();
  645. FINFO( "UnLoad Over");
  646. }
  647. ResDataObject GetDriverConfigration(CCOS_DRIVER_OBJ* pDriver)
  648. {
  649. ResDataObject res;
  650. return res;
  651. }
  652. //std::string FPDDemoDriver::GetResource()
  653. //{
  654. // ResDataObject r_config, temp;
  655. // if (!temp.loadFile(m_ConfigFileName.c_str()))
  656. // {
  657. // FINFO("FPDDemoDriver::GetResource loadFile failed :{$} \n", m_ConfigFileName);
  658. // return "";
  659. // }
  660. //
  661. // m_ConfigAll = temp;
  662. //
  663. // r_config = temp["CONFIGURATION"];
  664. // m_Configurations = r_config;
  665. //
  666. // ResDataObject DescriptionTemp;
  667. // ResDataObject ListTemp;
  668. // string strTemp = ""; //用于读取字符串配置信息
  669. // string strIndex = ""; //用于读取配置信息中的List项
  670. // int nTemp = -1; //用于读取整型配置信息
  671. // char sstream[10] = { 0 }; //用于转换值
  672. // string strValue = ""; //用于存储配置的值
  673. // string strType = ""; //用于存储配置的类型 int/float/string...
  674. //
  675. // /***
  676. // * 1. 通过循环,将所有配置项写到pDeviceConfig
  677. // * 2. 记录配置项的内部key以及配置类型,类型对应了不同配置文件路径,用于读写真实值
  678. // ***/
  679. // try
  680. // {
  681. // int nConfigInfoCount = (int)m_Configurations["ConfigToolInfo"].GetKeyCount("AttributeInfo");
  682. // m_pAttribute->clear();
  683. // m_pDescription->clear();
  684. // for (int nInfoIndex = 0; nInfoIndex < nConfigInfoCount; nInfoIndex++)
  685. // {
  686. // DescriptionTemp.clear();
  687. // ListTemp.clear();
  688. // //AttributeType
  689. // strTemp = (string)m_Configurations["ConfigToolInfo"][nInfoIndex]["AttributeDescripition"]["Type"];
  690. // DescriptionTemp.add(AttributeType, strTemp.c_str());
  691. // FINFO(g_pFPDCtrlLog, "--> {$}: {$}", AttributeType, strTemp.c_str());
  692. // strType = strTemp; //记录配置项的类型
  693. //
  694. // //AttributeKey
  695. // //1. 根据AttributeType,内部key和配置路径,拿到当前的真实值
  696. // strTemp = (string)m_Configurations["ConfigToolInfo"][nInfoIndex]["InnerKey"];
  697. // nTemp = (int)m_Configurations["ConfigToolInfo"][nInfoIndex]["PathID"];
  698. // GetDeviceConfigValue(r_config, strTemp.c_str(), nTemp, strValue);
  699. // //2. 赋值
  700. // strTemp = (string)m_Configurations["ConfigToolInfo"][nInfoIndex]["AttributeKey"];
  701. // if ("int" == strType)
  702. // {
  703. // (*m_pAttribute).add(strTemp.c_str(), atoi(strValue.c_str()));
  704. // FINFO(g_pFPDCtrlLog, "Key {$}: {$}", strTemp.c_str(), atoi(strValue.c_str()));
  705. // }
  706. // else if ("float" == strType)
  707. // {
  708. // (*m_pAttribute).add(strTemp.c_str(), atof(strValue.c_str()));
  709. // FINFO(g_pFPDCtrlLog, "Key {$}: {$}", strTemp.c_str(), atof(strValue.c_str()));
  710. // }
  711. // else //其它先按string类型处理
  712. // {
  713. // (*m_pAttribute).add(strTemp.c_str(), strValue.c_str());
  714. // FINFO(g_pFPDCtrlLog, "Key {$}: {$}", strTemp.c_str(), strValue.c_str());
  715. // }
  716. //
  717. // //AttributeAccess
  718. // strTemp = (string)m_Configurations["ConfigToolInfo"][nInfoIndex]["AttributeDescripition"]["Access"];
  719. // DescriptionTemp.add(AttributeAccess, strTemp.c_str());
  720. // FINFO(g_pFPDCtrlLog, "{$}: {$}", AttributeAccess, strTemp.c_str());
  721. //
  722. // //AttributeRangeMin
  723. // strTemp = (string)m_Configurations["ConfigToolInfo"][nInfoIndex]["AttributeDescripition"]["RangeMin"];
  724. // if (strTemp != "") //不需要的配置项为空
  725. // {
  726. // DescriptionTemp.add(AttributeRangeMin, strTemp.c_str());
  727. // FINFO(g_pFPDCtrlLog, "{$}: {$}", AttributeRangeMin, strTemp.c_str());
  728. // }
  729. //
  730. // //AttributeRangeMax
  731. // strTemp = (string)m_Configurations["ConfigToolInfo"][nInfoIndex]["AttributeDescripition"]["RangeMax"];
  732. // if (strTemp != "") //不需要的配置项为空
  733. // {
  734. // DescriptionTemp.add(AttributeRangeMax, strTemp.c_str());
  735. // FINFO(g_pFPDCtrlLog, "{$}: {$}", AttributeRangeMax, strTemp.c_str());
  736. // }
  737. //
  738. // //AttributeList
  739. // nTemp = m_Configurations["ConfigToolInfo"][nInfoIndex]["AttributeDescripition"]["ListNum"];
  740. // if (nTemp > 0) //ListNum不大于0时说明不需要list配置
  741. // {
  742. // for (int nListIndex = 0; nListIndex < nTemp; nListIndex++)
  743. // {
  744. // strTemp = (string)m_Configurations["ConfigToolInfo"][nInfoIndex]["AttributeDescripition"]["ListInfo"][nListIndex];
  745. // auto temKey = std::to_string(nListIndex);
  746. // ListTemp.add(temKey.c_str(), strTemp.c_str());
  747. // FINFO(g_pFPDCtrlLog, "list {$}: {$}", nListIndex, strTemp.c_str());
  748. // }
  749. // DescriptionTemp.add(AttributeList, ListTemp);
  750. // }
  751. //
  752. // //AttributeRequired
  753. // strTemp = (string)m_Configurations["ConfigToolInfo"][nInfoIndex]["AttributeDescripition"]["Required"];
  754. // DescriptionTemp.add(AttributeRequired, strTemp.c_str());
  755. // FINFO(g_pFPDCtrlLog, "{$}: {$}", AttributeRequired, strTemp.c_str());
  756. //
  757. // //AttributeDefaultValue
  758. // strTemp = (string)m_Configurations["ConfigToolInfo"][nInfoIndex]["AttributeDescripition"]["DefaultValue"];
  759. // if (strTemp != "") //不需要的配置项为空
  760. // {
  761. // DescriptionTemp.add(AttributeDefaultValue, strTemp.c_str());
  762. // FINFO(g_pFPDCtrlLog, "{$}: {$}", AttributeDefaultValue, strTemp.c_str());
  763. // }
  764. //
  765. // strTemp = (string)m_Configurations["ConfigToolInfo"][nInfoIndex]["AttributeKey"];
  766. // (*m_pDescription).add(strTemp.c_str(), DescriptionTemp);
  767. // }
  768. // }
  769. // catch (exception e)
  770. // {
  771. // FERROR("Get config error: {$}", e.what());
  772. // return "";
  773. // }
  774. //
  775. // ResDataObject resDeviceResource;
  776. // resDeviceResource.add(ConfKey::CcosDetectorAttribute, (*m_pAttribute));
  777. // resDeviceResource.add(ConfKey::CcosDetectorDescription, (*m_pDescription));
  778. //
  779. // ResDataObject DescriptionTempEx;
  780. // DescriptionTempEx.add(ConfKey::CcosDetectorConfig, resDeviceResource);
  781. //
  782. // m_DeviceConfig = DescriptionTempEx;
  783. //
  784. // string res = DescriptionTempEx.encode();
  785. //
  786. // FINFO("resDeviceResource :{$} \n", DescriptionTempEx.encode());
  787. // return res;
  788. //}
  789. RET_STATUS DATA_ACTION ContainerDPC::GetDeviceConfig(ResDataObject PARAM_OUT* pDeviceConfig)
  790. {
  791. FINFO( "GetDeviceConfig start");
  792. std::string strDeviceConfig;
  793. //if (m_NewDevManager->GetDeviceConfig(strDeviceConfig))
  794. //{
  795. // pDeviceConfig->decode(strDeviceConfig.c_str());
  796. //}
  797. //else
  798. //{
  799. // return RET_FAILED;
  800. //}
  801. FINFO( "GetDeviceConfig Over");
  802. return RET_SUCCEED;
  803. }
  804. RET_STATUS DATA_ACTION ContainerDPC::SetDeviceConfig(ResDataObject PARAM_IN* DeviceConfig)
  805. {
  806. FINFO( "SetDeviceConfig start");
  807. std::string strDeviceConfig = DeviceConfig->encode();
  808. //if (m_NewDevManager->SetDeviceConfig(strDeviceConfig))
  809. //{
  810. //}
  811. //else
  812. //{
  813. // return RET_FAILED;
  814. //}
  815. FINFO( "SetDeviceConfig Over");
  816. return RET_SUCCEED;
  817. }