OemBusUnit.cpp 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  1. #include "DevTree.h"
  2. #include "LocalConfig.h"
  3. #include "common_api.h"
  4. #include "OemBusUnit.h"
  5. #include "OemBusUnitDevice.h"
  6. //OEMBUSUNIT_API DPC* GetDPC()
  7. //{
  8. // return (DPC*)(new BusUnitDPC());
  9. //}
  10. //
  11. //OEMBUSUNIT_API void ReleaseDPC(DPC *p)
  12. //{
  13. // BusUnitDPC *org = (BusUnitDPC*)p;
  14. // delete org;
  15. //}
  16. //Log4CPP::Logger* mLog::gLogger = nullptr;
  17. OEMBUSUNIT_C_API DriverDPC* GetDriverDPC()
  18. {
  19. return (DriverDPC*)(new BusUnitDPC());
  20. }
  21. OEMBUSUNIT_C_API void ReleaseDriverDPC(DriverDPC *p)
  22. {
  23. BusUnitDPC *org = (BusUnitDPC*)p;
  24. delete org;
  25. }
  26. BusUnitDPC::BusUnitDPC(void)
  27. {
  28. m_pWorkpath = new std::string();
  29. m_pRootDevice = 0;
  30. m_FirstLoad = true;
  31. m_ExitFlag = LinuxEvent::CreateEvent(LinuxEvent::MANUAL_RESET,false);
  32. //if (//mLog::gLogger == nullptr)
  33. //{
  34. // string strLogPath = GetProcessDirectory() + R"(\Conf\Log4CPP.Config.xml)";
  35. // string LogHost = ((string)getLogRootpath()).c_str();
  36. // if (LogHost.length() <= 1)
  37. // {
  38. // char szName[256];
  39. // sprintf(szName, "/LogicDevice_%08d", GetCurrentProcessId());
  40. // LogHost = szName;
  41. // }
  42. // Log4CPP::ThreadContext::Map::Set(ECOM::Utility::Hash("LogFileName"), "Platform");
  43. // //Log4CPP::GlobalContext::Map::Set("LogHost", LogHost.c_str());
  44. // Log4CPP::ThreadContext::Map::Set(ECOM::Utility::Hash("LogHost"), LogHost.c_str() + 1);
  45. // auto rc = Log4CPP::LogManager::LoadConfigFile(strLogPath.c_str());
  46. // //mLog::gLogger = Log4CPP::LogManager::GetLogger("Platform");
  47. // //mLog::FINFO("Code Build datetime [{$} {$}]", __DATE__, __TIME__);
  48. //}
  49. }
  50. BusUnitDPC::~BusUnitDPC(void)
  51. {
  52. delete m_pWorkpath;
  53. }
  54. void BusUnitDPC::SetExitFlag()
  55. {
  56. m_ExitFlag->SetEvent();
  57. }
  58. bool BusUnitDPC::DriverEntry(ResDataObject &Configuration)
  59. {
  60. return LogicDriver::DriverEntry(Configuration);
  61. }
  62. bool SYSTEM_CALL BusUnitDPC::Driver_Probe(ResDataObject& PARAM_OUT HardwareInfo)
  63. {
  64. ResDataObject Config;
  65. GetConfiguration(&Config);
  66. HardwareInfo.add("MajorID", (const char*)Config["MajorID"]);
  67. HardwareInfo.add("MinorID", (const char*)Config["MinorID"]);
  68. HardwareInfo.add("VendorID", (const char*)Config["VendorID"]);
  69. HardwareInfo.add("ProductID", (const char*)Config["ProductID"]);
  70. HardwareInfo.add("SerialID", (const char*)Config["SerialID"]);
  71. //HardwareInfo.add("MajorID", "root");
  72. //HardwareInfo.add("MinorID", "drv");
  73. //HardwareInfo.add("VendorID", "Ecom");
  74. //HardwareInfo.add("ProductID", "bus");
  75. //HardwareInfo.add("SerialID", "425");
  76. return true;
  77. }
  78. RET_STATUS SYSTEM_CALL BusUnitDPC::GetDriverDictionary(ResDataObject& PARAM_OUT DriverInfo)
  79. {
  80. DriverInfo.add("DriverType", "root");
  81. DriverInfo.add("DeviceName", "root");
  82. DriverInfo.add("DateOfManufacture", "2017.10.20");
  83. DriverInfo.add("SoftwareVersion", "Alpha1.0");
  84. DriverInfo.add("HardwareVersion", "Alpha1.0");
  85. DriverInfo.add("Description", "Root Drver");
  86. DriverInfo.add("Properties", "None");
  87. return RET_SUCCEED;
  88. }
  89. bool BusUnitDPC::SetDriverWorkPath(const char *pWorkPath)
  90. {
  91. (*m_pWorkpath) = pWorkPath;
  92. //要把读取单一硬件属性的过程放到此处
  93. return true;
  94. }
  95. bool BusUnitDPC::SetDeviceWorkPath(const char *pWorkPath)
  96. {
  97. return true;
  98. }
  99. void BusUnitDPC::OnSetClientID()
  100. {
  101. if (m_pRootDevice != nullptr) {
  102. //mLog::FINFO("reset root SetClientRootID ? [{$}] ", m_strCCOSDevicePath);
  103. std::cout << "BusUnitDPC::OnSetClientID [ 0 reset root SetClientRootID ?" << m_strCCOSDevicePath << "]" << std::endl;
  104. if (m_strCCOSDevicePath.find('/') == string::npos)
  105. {
  106. std::cout << "BusUnitDPC::OnSetClientID [ reset root SetClientRootID ?" << m_strCCOSDevicePath << "]" << std::endl;
  107. m_strCCOSDevicePath = "CCOS/HOST/" + m_strCCOSDevicePath;
  108. }
  109. m_pRootDevice->SetClientRootID(m_strEBusRoot.c_str(), m_strCCOSDevicePath.c_str());
  110. }
  111. }
  112. void BusUnitDPC::SubscribeSelf() {
  113. //这里订阅主题
  114. //if (m_pRootDevice != nullptr) {
  115. // m_pRootDevice->SubscribeSelf();
  116. //}
  117. //SubscribeTopic(m_pMqttConntion, m_strEBusRoot.c_str());
  118. }
  119. /*
  120. RET_STATUS BusUnitDPC::ProcessRequest(ResDataObject* pCmd, PACKET_CMD cmd)
  121. {
  122. if (cmd == PACKET_CMD_OPEN || PACKET_CMD_CLOSE)
  123. {
  124. ResDataObject resRes, resResponse;
  125. GetDeviceResource(&resRes);
  126. PacketAnalizer::MakeOpenResponse(*pCmd, resResponse, resRes);
  127. ResDataObject resTopic;
  128. PacketAnalizer::GetContextTopic(pCmd, resTopic);
  129. //std::cout << "publis " << (const char*)resTopic << "msg body" << resResponse.encode() << endl;
  130. PublishAction(&resResponse, (const char*)resTopic, m_pMqttConntion);
  131. return RET_SUCCEED;
  132. }
  133. return LogicDevice::ProcessRequest(pCmd, cmd);
  134. }*/
  135. PVOID SYSTEM_CALL BusUnitDPC::LoadDriver()
  136. {
  137. std::cout << "enter BusUnitDPC::LoadDriver() "<< endl;
  138. OemBusUnitDevice *p = new OemBusUnitDevice();
  139. p->SetDrvDPC(this);
  140. m_pRootDevice = p;
  141. LogicDevice *pret = (LogicDevice *)p;
  142. DevTree* pTree = (DevTree*)GetDriverTree();
  143. pTree->Add((PVOID)pret, TYPE_DEVICE);
  144. CCOS_PROC_TYPE Mode = GetConfigMode();
  145. if (Mode == CCOS_PROC_CHANNEL)
  146. {
  147. p->LoadAllConfigDrivers();
  148. }
  149. LogicDriver::Connect();//make sure it's connected
  150. return (PVOID)pTree;
  151. }
  152. void SYSTEM_CALL BusUnitDPC::UnloadAllRegistedDrivers()
  153. {
  154. Thread_Lock();
  155. ((OemBusUnitDevice*)m_pRootDevice)->UnloadAllRegistedDrivers();
  156. Thread_UnLock();
  157. }
  158. void SYSTEM_CALL BusUnitDPC::UnloadDriver()
  159. {
  160. //kill all drivers before me
  161. //UnloadAllRegistedDrivers();
  162. DevTree* pTree = (DevTree*)GetDriverTree();
  163. size_t Size = pTree->size();
  164. for (size_t i = 0; i < Size; i++)
  165. {
  166. DevTreeNode node = (*pTree)[i];
  167. if (node.m_NodeType == TYPE_DEVICE)
  168. {
  169. OemBusUnitDevice *pdev = (OemBusUnitDevice*)node.m_pObject;
  170. delete pdev;
  171. }
  172. else if (node.m_NodeType == TYPE_DRIVER)
  173. {
  174. //...
  175. }
  176. }
  177. pTree->clear();
  178. Thread_Lock();
  179. m_pRootDevice = 0;
  180. Thread_UnLock();
  181. }
  182. bool BusUnitDPC::Connect()
  183. {
  184. return true;
  185. }
  186. void BusUnitDPC::DisConnect()
  187. {
  188. return;
  189. }
  190. bool SYSTEM_CALL BusUnitDPC::Device_Probe(ResDataObject& PARAM_OUT HardwareInfo)
  191. {
  192. //不支持情况直接反馈失败
  193. return false;
  194. }
  195. PVOID BusUnitDPC::LoadLogicDevices()
  196. {
  197. //不支持情况直接反馈失败
  198. return NULL;
  199. }
  200. void BusUnitDPC::UnloadLogicDevices()
  201. {
  202. //do nothing
  203. return;
  204. }
  205. DWORD SYSTEM_CALL BusUnitDPC::OnNotify(std::vector <std::shared_ptr<LinuxEvent>> evtList, DWORD count)
  206. {
  207. std::vector <std::shared_ptr<LinuxEvent>> pWaitList = evtList;
  208. pWaitList.push_back( m_ExitFlag);
  209. while (1)
  210. {
  211. ////mLog::FINFO("OnNotify Loop Entry-----------");
  212. //进程崩溃后,可以做到秒起,但是基于目前的SM模块启动需要5秒时间,意义不是很大...
  213. DWORD dwResult = LinuxEvent::WaitForMultipleEvents(pWaitList, 2000);
  214. if ((dwResult >= WAIT_OBJECT_0) && (dwResult <= WAIT_OBJECT_0 + count - 1))
  215. {
  216. //mLog::FINFO( "OnNotify Platform. it might be exit the process....");
  217. pWaitList.clear();
  218. return dwResult - WAIT_OBJECT_0;
  219. }
  220. if (dwResult == WAIT_OBJECT_0 + count)
  221. {
  222. pWaitList.clear();
  223. return 0;//exit flag
  224. }
  225. //timeout
  226. CCOS_PROC_TYPE Mode = GetConfigMode();
  227. if (Mode == CCOS_PROC_CHANNEL)
  228. {
  229. //timeout
  230. Thread_Lock();
  231. if (m_pRootDevice)
  232. {
  233. //first load
  234. if (m_FirstLoad)
  235. {
  236. ((OemBusUnitDevice*)m_pRootDevice)->LoadAllConfigDriver();
  237. m_FirstLoad = false;
  238. }
  239. else
  240. {
  241. //watch dog
  242. ((OemBusUnitDevice*)m_pRootDevice)->CheckAllLiveDriver();
  243. }
  244. }
  245. Thread_UnLock();
  246. }
  247. }
  248. return 0;
  249. }