DiosProc.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467
  1. // DiosProc.cpp : 定义控制台应用程序的入口点。
  2. //
  3. #include "stdafx.h"
  4. #include <conio.h>
  5. #include <stdio.h>
  6. //#include "scf.h"
  7. #include "CDI.h"
  8. #include "LocalConfig.h"
  9. #include "BusUnitClient.h"
  10. #include "LogicClient.h"
  11. #include "common_api.h"
  12. #include "PacketAnalizer.h"
  13. #include "ConsoleThread.h"
  14. #include "AutoDmp.h"
  15. #include <iostream>
  16. #define CRTDBG_MAP_ALLOC
  17. #include <stdlib.h>
  18. #include <crtdbg.h>
  19. #include "ShareMemory_Client.h"
  20. #include <chrono>
  21. inline std::string CurrentDateTime()
  22. {
  23. // 获取当前时间点
  24. auto now = std::chrono::system_clock::now();
  25. // 将时间长度转换为微秒数
  26. auto now_us = std::chrono::duration_cast<std::chrono::microseconds>(now.time_since_epoch());
  27. // 再转成tm格式
  28. auto now_time_t = std::chrono::system_clock::to_time_t(now);
  29. auto now_tm = std::localtime(&now_time_t);
  30. // 可以直接输出到标准输出
  31. // std::cout << std::put_time(now_tm, "%Y-%m-%d %H:%M:%S.") << std::setfill('0') << std::setw(6) << now_us.count() % 1000000 << std::endl;
  32. // 格式化字符,年月日时分秒
  33. std::string now_time_str;
  34. char buf[64];
  35. std::strftime(buf, sizeof(buf), "%Y-%m-%d %H:%M:%S", now_tm);
  36. now_time_str += buf;
  37. // 格式化微秒
  38. snprintf(buf, sizeof(buf), ".%06lld ", now_us.count() % 1000000);
  39. now_time_str += buf;
  40. //printf("%s\n", now_time_str.c_str());
  41. return now_time_str;
  42. }
  43. AutoDmp atdmp;
  44. ConsoleThread g_ConsoleThread;
  45. bool StartConsoleThreads(bool start)
  46. {
  47. bool ret = true;
  48. g_ConsoleThread.SetName("ConsoleThread");
  49. if (start)
  50. {
  51. ret &= (g_ConsoleThread).StartThread();
  52. }
  53. else
  54. {
  55. (g_ConsoleThread).StopThread(100);
  56. }
  57. return ret;
  58. }
  59. bool WINAPI ConsoleHandler(DWORD msgType)
  60. {
  61. if ((msgType == CTRL_CLOSE_EVENT) ||
  62. (msgType == CTRL_SHUTDOWN_EVENT))
  63. {
  64. printf("Close console window!\n");
  65. StartConsoleThreads(false);
  66. printf("StopThread Finished!\n");
  67. GetCommandDispathIF()->WaitExit(100, true);
  68. printf("WaitExit Finished!\n");
  69. Sleep(1000);
  70. /* Note: The system gives you very limited time to exit in this condition */
  71. return true;
  72. }
  73. /*
  74. Other messages:
  75. CTRL_BREAK_EVENT Ctrl-Break pressed
  76. CTRL_LOGOFF_EVENT User log off
  77. CTRL_SHUTDOWN_EVENT System shutdown
  78. */
  79. return false;
  80. }
  81. bool EntryCheck(int argc, _TCHAR* argv[])
  82. {
  83. //only one driver exist
  84. if (argc < 2)
  85. {
  86. printf("not Driver exist.exit in 3sec\n");
  87. Sleep(3000);
  88. return false;
  89. }
  90. for (int idx = 1; idx < argc; idx++)
  91. {
  92. printf("Add DriverConfig:%s\n", argv[idx]);
  93. if (AddDriverConfig(argv[idx]) == false)
  94. {
  95. printf("DriverConfig File not Right.%s \nexit in 3sec\n", argv[1]);
  96. Sleep(3000);
  97. return false;
  98. }
  99. }
  100. return true;
  101. }
  102. class TestMqtt : public Dios_Thread
  103. {
  104. dios_mqtt_connection* m_pConns;
  105. int m_nIndex;
  106. int m_nSendNum;
  107. public:
  108. TestMqtt(dios_mqtt_connection* pConn, int nIndex, int nSendNum) {
  109. m_pConns = pConn; m_nIndex = nIndex;
  110. m_nSendNum = nSendNum;
  111. }
  112. ~TestMqtt(){}
  113. bool Exec() override;
  114. //bool OnStartThread() override;
  115. //bool OnEndThread() override;
  116. };
  117. bool TestMqtt::Exec()
  118. {
  119. int y = 0;
  120. for ( y= 0; y < m_nSendNum; y++)
  121. {
  122. char szTopic[256];
  123. char szContent[128];
  124. char szKey[32];
  125. //for (int z = 0; z < nConnNum; z++)
  126. {
  127. ResDataObject NotifyData;
  128. sprintf(szContent, "%d_%d_%s", y, m_nIndex, CurrentDateTime().c_str());
  129. sprintf(szTopic, "DIOS/TEST/Notify/%d/%d/TestV", GetCurrentProcessId(), m_nIndex);
  130. sprintf(szKey, "%d", GetTickCount());
  131. PacketAnalizer::MakeNotify(NotifyData, PACKET_CMD_UPDATE, szKey, szContent);
  132. //CmdFromLogicDev(&NotifyData);
  133. int ret = PublishAction(&NotifyData, szTopic, m_pConns);
  134. cout << "Send [" << szContent << "] to [" << szTopic << "] result " << ret << endl;
  135. }
  136. }
  137. std::wcout << "Send Over " << m_nIndex << " Count " << y << endl;
  138. return false;
  139. }
  140. int _tmain(int argc, _TCHAR* argv[])
  141. {
  142. printf("Main Thread:%d\n", GetCurrentThreadId());
  143. std::cout << "Main " << argc << endl;
  144. for (size_t i = 0; i < argc; i++)
  145. {
  146. cout << "param " << i << " : " << argv[i] << std::endl;
  147. }
  148. if (argc > 1)
  149. {
  150. if (strcmp(argv[1], "test") == 0)
  151. {
  152. char szName[20][256];
  153. int nConnNum = 5;
  154. int nSendNum = 10;
  155. if (argc > 2)
  156. nConnNum = atoi(argv[2]);
  157. if (argc > 3)
  158. nSendNum = atoi(argv[3]);
  159. if (nConnNum > 20)
  160. nConnNum = 20;
  161. dios_mqtt_connection** pConns = new dios_mqtt_connection * [nConnNum];
  162. TestMqtt** pTests = new TestMqtt * [nConnNum];
  163. for (int x = 0; x < nConnNum; x++)
  164. {
  165. sprintf(szName[x], "TEST_MQTT_Client_%d_%d", x, GetCurrentProcessId());
  166. pConns[x] = NewConnection(szName[x], [szName,x](ResDataObject* rsp, const char* topic, void* conn_void) {
  167. dios_mqtt_connection* conn = (dios_mqtt_connection*)conn_void;
  168. std::cout << "Server [" << szName[x] << "] " << CurrentDateTime() << "Get Msg from " << topic << /*" Msg Body " << rsp->encode() <<*/ endl;
  169. if (PacketAnalizer::GetPacketType(rsp) == PACKET_TYPE_RES)
  170. {
  171. //check cmd
  172. if ((PacketAnalizer::GetPacketCmd(rsp) == PACKET_CMD_OPEN) /*&& (PacketAnalizer::GetPacketKey(rsp) == realPath)*/)
  173. {
  174. //std::cout << "\n\n-----------" << CurrentDateTime() << "LogicClient::Open OK [" << m_strClientName << "] try open path:" << realPath << endl << endl;
  175. //ret = ProcessOpenResponse(*rsp);
  176. //if (m_bNeedNotify) {
  177. string notifyTopic = "DIOS/Tetst/";
  178. notifyTopic += "/Notify/#";
  179. SubscribeTopic(conn, notifyTopic.c_str());
  180. //}
  181. }
  182. else
  183. {
  184. std::cout << szName[x] << CurrentDateTime() << "Try Conn callback get unkown packet pointer [" << (UINT64)conn << endl;
  185. std::cout << "Unkown Packet: cmd [" << PacketAnalizer::GetPacketCmd(rsp) << "] packet key: " << PacketAnalizer::GetPacketKey(rsp) << "" << endl;
  186. //if (m_pPacketArrivedCallbackFunc != nullptr)
  187. //{
  188. // m_pPacketArrivedCallbackFunc(rsp, topic, conn);
  189. //}
  190. //else
  191. //{
  192. // m_pPacketReceivedQue->Lock();
  193. // m_pPacketReceivedQue->InQueue(*rsp);
  194. // //SetEvent(m_NotifyEvent);//notify to user
  195. // SetEvent(m_ResponseEvent);
  196. // m_pPacketReceivedQue->UnLock();
  197. //}
  198. }
  199. }
  200. else
  201. {
  202. }
  203. });
  204. pTests[x] = new TestMqtt(pConns[x], x , nSendNum);
  205. char szTopic[256];
  206. sprintf(szTopic, "DIOS/TEST/CLIENT/C%06d", GetCurrentProcessId());
  207. SubscribeTopic(pConns[x], szTopic);
  208. //SubscribeTopic(pConns[x], "DIOS/TEST/Action/#", true);
  209. }
  210. std::cout << "Waiting quit..." << endl;
  211. while (1)
  212. {
  213. int c = getchar();
  214. switch (c)
  215. {
  216. case 's':
  217. {
  218. for (int z = 0; z < nConnNum; z++)
  219. {
  220. pTests[z]->StartThread(false);
  221. }
  222. }
  223. break;
  224. case 'x':
  225. {
  226. for (int x = 0; x < nConnNum; x++)
  227. {
  228. cout << "Try ResetConnection" << x << endl;
  229. ResetConnection(pConns[x]);
  230. //Sleep(2500);
  231. cout << "ResetConnection Over" << x << endl;
  232. }
  233. }
  234. break;
  235. case 'q':
  236. {
  237. break;
  238. }
  239. }
  240. if (c == 'q')
  241. break;
  242. std::cout << "Please Input Command " << endl;
  243. Sleep(10);
  244. }
  245. for (int x = 0; x < nConnNum; x++)
  246. {
  247. CloseConnection(pConns[x]);
  248. }
  249. delete[] pConns;
  250. std::cout << "Hello World!\n";
  251. return 0;
  252. }
  253. }
  254. bool status = true;
  255. //这里把多个 通OEMDriver的 xml一起启动
  256. if (EntryCheck(argc, argv) == false)
  257. {
  258. return false;
  259. }
  260. if (GetCommandDispathIF()->InitAs(DIOS_PROC_MASTER, (UINT64)GetCurrentProcessId()))
  261. {
  262. if (SetConsoleCtrlHandler((PHANDLER_ROUTINE)ConsoleHandler, true))
  263. {
  264. //WaitSomeInfo("Driver init Done.Press Any Key to show All Driver List\n", 1000);
  265. //1. do the bus test
  266. //Sleep(3000);
  267. {
  268. LogicClient buClient("diosproc_exe", "", "", false);
  269. if (buClient.Open((const char*)getRootpath(), ALL_ACCESS) == RET_SUCCEED)
  270. {
  271. //buClient.ExitDriverProc();
  272. buClient.Close();
  273. //DWORD count = buClient.GetDeviceCount();
  274. //for (DWORD i = 0; i < count; i++)
  275. //{
  276. // UINT64 ProcId, Addr;
  277. // ResDataObject DevType,devpath, MachineId;
  278. // buClient.GetDeviceDescript(i, devpath, DevType, MachineId, ProcId, Addr);
  279. // printf("path:%s\nProc:%I64u\nAddr:%I64u\n", (const char*)devpath, ProcId, Addr);
  280. // LogicClient DevClient("diosproc_exe_temp","diosproc");
  281. // if (DevClient.Open((const char*)devpath, ALL_ACCESS) >= RET_SUCCEED)
  282. // {
  283. // DevClient.Close();
  284. // printf("Open Succeed\n");
  285. // }
  286. // else
  287. // {
  288. // printf("Open Failed\n");
  289. // }
  290. //}
  291. status = true;
  292. }
  293. else
  294. {
  295. status = false;
  296. }
  297. }
  298. //start work
  299. if (status)
  300. {
  301. if (StartConsoleThreads(true))
  302. {
  303. printf("Done Init Channel.going to sleep...\n");
  304. while (1){
  305. if (GetCommandDispathIF()->WaitExit(500) == true)
  306. {
  307. StartConsoleThreads(false);
  308. return 1;
  309. }
  310. }
  311. }
  312. }
  313. printf("Can't Start Console Thread.Exit in 3sec\n");
  314. Sleep(3000);
  315. return 0;
  316. }
  317. }
  318. return 0;
  319. }
  320. //int _tmain(int argc, _TCHAR* argv[])
  321. //{
  322. // printf("Main Thread:%d\n", GetCurrentThreadId());
  323. // if (GetCommandDispathIF()->InitAs(true, (UINT64)GetCurrentProcessId()))
  324. // {
  325. // if (SetConsoleCtrlHandler((PHANDLER_ROUTINE)ConsoleHandler, true))
  326. // {
  327. // //1. do the bus test
  328. // BusUnitClient buClient;
  329. // if (buClient.Open(getRootpath().c_str(), ALL_ACCESS) == RET_SUCCEED)
  330. // {
  331. // buClient.Close();
  332. // DWORD count = buClient.GetDeviceCount();
  333. // for (DWORD i = 0; i < count; i++)
  334. // {
  335. // UINT64 ProcId, Addr;
  336. // ResDataObject devpath;
  337. // buClient.GetDeviceDescript(i, devpath, ProcId, Addr);
  338. // printf("path:%s\nProc:%I64u\nAddr:%I64u\n", (const char*)devpath, ProcId, Addr);
  339. // }
  340. // }
  341. //
  342. // //1. do the Wheel test
  343. // DWORD loop = 8;
  344. // CommonLogicClient WheelClient;
  345. // HANDLE notifyhandle = WheelClient.GetNotifyHandle();
  346. // if (WheelClient.Open("/diosAlpha/Major/Minor/Ecom/AbstractSeq/1234/{4E4206DA-DA34-4E71-7F42-97E0ABFF3F81}",
  347. // ALL_ACCESS) == RET_SUCCEED)
  348. // {
  349. // while (loop--)
  350. // {
  351. // ResDataObject Request,Response;
  352. // WheelClient.Action("OpenLogicDevice", Request, Response);
  353. // WheelClient.Action("GetSEQResource", Request, Response);
  354. // WheelClient.Action("FrameInState", Request, Response);
  355. // //bool status = false;
  356. // //PACKET_CMD cmd;
  357. // //ResDataObject data;
  358. // //WheelClient.StartRoll();
  359. // ////Notify
  360. // //cmd = PACKET_CMD_NONE;
  361. // //printf("Wait for a notify\n");
  362. // //while (cmd == PACKET_CMD_NONE)
  363. // //{
  364. // // WaitForSingleObject(notifyhandle, INFINITE);
  365. // // while (WheelClient.IsDataArrived())
  366. // // {
  367. // // cmd = WheelClient.ReadCmd(data);
  368. // // printf("cmd:%d\n", cmd);
  369. // // if (cmd == PACKET_CMD_UPDATE)
  370. // // {
  371. // // //got update notify
  372. // // WheelClient.GetLocalStatus(status);
  373. // // printf("current status :%d\n", status);
  374. // // }
  375. // // }
  376. // //}
  377. //
  378. // //WheelClient.StopRoll();
  379. // ////Notify
  380. // //cmd = PACKET_CMD_NONE;
  381. // //printf("Wait for a notify\n");
  382. // //while (cmd == PACKET_CMD_NONE)
  383. // //{
  384. // // WaitForSingleObject(notifyhandle, INFINITE);
  385. // // while (WheelClient.IsDataArrived())
  386. // // {
  387. // // cmd = WheelClient.ReadCmd(data);
  388. // // printf("cmd:%d\n", cmd);
  389. // // if (cmd == PACKET_CMD_UPDATE)
  390. // // {
  391. // // //got update notify
  392. // // WheelClient.GetLocalStatus(status);
  393. // // printf("current status :%d\n", status);
  394. // // }
  395. // // }
  396. // //}
  397. //
  398. // }
  399. //
  400. // WheelClient.Close();
  401. // }
  402. //
  403. // //printf("Press Ctrl-C or [x] to close...\n");
  404. // while (1){ Sleep(2000); }
  405. // }
  406. //
  407. // }
  408. // return 0;
  409. //}