CcosProc.cpp 11 KB

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