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