123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458 |
- // CcosProc.cpp : 定义控制台应用程序的入口点。
- //
- #include <stdio.h>
- //#include "scf.h"
- #include "CDI.h"
- #include "LocalConfig.h"
- #include "BusUnitClient.h"
- #include "LogicClient.h"
- #include "common_api.h"
- #include "PacketAnalizer.h"
- #include "ConsoleThread.h"
- #include "AutoDmp.h"
- #include <iostream>
- #define CRTDBG_MAP_ALLOC
- #include <stdlib.h>
- #include "ShareMemory_Client.h"
- #include <chrono>
- inline std::string CurrentDateTime()
- {
- // 获取当前时间点
- auto now = std::chrono::system_clock::now();
- // 将时间长度转换为微秒数
- auto now_us = std::chrono::duration_cast<std::chrono::microseconds>(now.time_since_epoch());
- // 再转成tm格式
- auto now_time_t = std::chrono::system_clock::to_time_t(now);
- auto now_tm = std::localtime(&now_time_t);
- // 可以直接输出到标准输出
- // 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;
- // 格式化字符,年月日时分秒
- std::string now_time_str;
- char buf[64];
- std::strftime(buf, sizeof(buf), "%Y-%m-%d %H:%M:%S", now_tm);
- now_time_str += buf;
- // 格式化微秒
- snprintf(buf, sizeof(buf), ".%06ld ", now_us.count() % 1000000);
- now_time_str += buf;
- //printf("%s\n", now_time_str.c_str());
- return now_time_str;
- }
- AutoDmp atdmp;
- ConsoleThread g_ConsoleThread;
- bool StartConsoleThreads(bool start)
- {
- bool ret = true;
- g_ConsoleThread.SetName("ConsoleThread");
- if (start)
- {
- ret &= (g_ConsoleThread).StartThread();
- }
- else
- {
- (g_ConsoleThread).StopThread(100);
- }
- return ret;
- }
- void ConsoleHandler(int signo)
- {
- // 处理常见的终止信号
- switch (signo) {
- case SIGINT: // Ctrl+C (类似 Windows 的 CTRL_CLOSE_EVENT)
- case SIGTERM: // 终止信号 (类似 Windows 的 CTRL_SHUTDOWN_EVENT)
- printf("Received termination signal! Shutting down...\n");
- // 停止控制台线程
- StartConsoleThreads(false);
- // 等待命令分发系统退出,最多等待 500ms
- // 注意: Linux 下需要确保 WaitExit 的实现支持超时和强制退出参数
- GetCommandDispathIF()->WaitExit(500, true);
- // 在 Linux 中,我们需要执行最终的清理并退出
- // Windows 会自动终止进程,但 Linux 需要显式退出
- exit(EXIT_SUCCESS);
- break;
- default:
- // 其他信号可以在这里处理
- // 例如: SIGQUIT (Ctrl+\), SIGHUP (终端断开) 等
- break;
- }
- }
- bool EntryCheck(int argc, char* argv[])
- {
- //only one driver exist
- if (argc < 2)
- {
- printf("not Driver exist.exit in 3sec\n");
- sleep(3);
- return false;
- }
- for (int idx = 1; idx < argc; idx++)
- {
- printf("Add DriverConfig:%s\n", argv[idx]);
- if (AddDriverConfig(argv[idx]) == false)
- {
- printf("DriverConfig File not Right.%s \nexit in 3sec\n", argv[1]);
- sleep(3);
- return false;
- }
- }
- return true;
- }
- class TestMqtt : public Ccos_Thread
- {
- ccos_mqtt_connection* m_pConns;
- int m_nIndex;
- int m_nSendNum;
- public:
- TestMqtt(ccos_mqtt_connection* pConn, int nIndex, int nSendNum) {
- m_pConns = pConn; m_nIndex = nIndex;
- m_nSendNum = nSendNum;
- }
- ~TestMqtt(){}
- bool Exec() override;
- //bool OnStartThread() override;
- //bool OnEndThread() override;
- };
- bool TestMqtt::Exec()
- {
- int y = 0;
- for ( y= 0; y < m_nSendNum; y++)
- {
- char szTopic[256];
- char szContent[128];
- char szKey[32];
- //for (int z = 0; z < nConnNum; z++)
- {
- ResDataObject NotifyData;
- sprintf(szContent, "%d_%d_%s", y, m_nIndex, CurrentDateTime().c_str());
- sprintf(szTopic, "CCOS/TEST/Notify/%d/%d/TestV", getpid(), m_nIndex);
- sprintf(szKey, "%lu", GetTickCount());
- PacketAnalizer::MakeNotify(NotifyData, PACKET_CMD_UPDATE, szKey, szContent);
- //CmdFromLogicDev(&NotifyData);
- int ret = PublishAction(&NotifyData, szTopic, m_pConns);
- cout << "Send [" << szContent << "] to [" << szTopic << "] result " << ret << endl;
- }
- }
- std::wcout << "Send Over " << m_nIndex << " Count " << y << endl;
- return false;
- }
- int _tmain(int argc, char* argv[])
- {
- printf("Main Thread:%lu\n", GetCurrentThreadId());
- std::cout << "Main " << argc << endl;
- for (size_t i = 0; i < argc; i++)
- {
- cout << "param " << i << " : " << argv[i] << std::endl;
- }
- if (argc > 1)
- {
- if (strcmp(argv[1], "test") == 0)
- {
- char szName[20][256];
- int nConnNum = 5;
- int nSendNum = 10;
- if (argc > 2)
- nConnNum = atoi(argv[2]);
- if (argc > 3)
- nSendNum = atoi(argv[3]);
- if (nConnNum > 20)
- nConnNum = 20;
- ccos_mqtt_connection** pConns = new ccos_mqtt_connection * [nConnNum];
- TestMqtt** pTests = new TestMqtt * [nConnNum];
- for (int x = 0; x < nConnNum; x++)
- {
- sprintf(szName[x], "TEST_MQTT_Client_%d_%d", x, getpid());
- pConns[x] = NewConnection(szName[x], [szName,x](ResDataObject* rsp, const char* topic, void* conn_void) {
- ccos_mqtt_connection* conn = (ccos_mqtt_connection*)conn_void;
- std::cout << "Server [" << szName[x] << "] " << CurrentDateTime() << "Get Msg from " << topic << /*" Msg Body " << rsp->encode() <<*/ endl;
- if (PacketAnalizer::GetPacketType(rsp) == PACKET_TYPE_RES)
- {
- //check cmd
- if ((PacketAnalizer::GetPacketCmd(rsp) == PACKET_CMD_OPEN) /*&& (PacketAnalizer::GetPacketKey(rsp) == realPath)*/)
- {
- //std::cout << "\n\n-----------" << CurrentDateTime() << "LogicClient::Open OK [" << m_strClientName << "] try open path:" << realPath << endl << endl;
- //ret = ProcessOpenResponse(*rsp);
- //if (m_bNeedNotify) {
- string notifyTopic = "CCOS/Tetst/";
- notifyTopic += "/Notify/#";
- SubscribeTopic(conn, notifyTopic.c_str());
- //}
- }
- else
- {
- std::cout << szName[x] << CurrentDateTime() << "Try Conn callback get unkown packet pointer [" << (UINT64)conn << endl;
- std::cout << "Unkown Packet: cmd [" << PacketAnalizer::GetPacketCmd(rsp) << "] packet key: " << PacketAnalizer::GetPacketKey(rsp) << "" << endl;
- //if (m_pPacketArrivedCallbackFunc != nullptr)
- //{
- // m_pPacketArrivedCallbackFunc(rsp, topic, conn);
- //}
- //else
- //{
- // m_pPacketReceivedQue->Lock();
- // m_pPacketReceivedQue->InQueue(*rsp);
- // //SetEvent(m_NotifyEvent);//notify to user
- // SetEvent(m_ResponseEvent);
- // m_pPacketReceivedQue->UnLock();
- //}
- }
- }
- else
- {
- }
- });
- pTests[x] = new TestMqtt(pConns[x], x , nSendNum);
- char szTopic[256];
- sprintf(szTopic, "CCOS/TEST/CLIENT/C%06d", getpid());
- SubscribeTopic(pConns[x], szTopic);
- //SubscribeTopic(pConns[x], "CCOS/TEST/Action/#", true);
- }
- std::cout << "Waiting quit..." << endl;
- while (1)
- {
- int c = getchar();
- switch (c)
- {
- case 's':
- {
- for (int z = 0; z < nConnNum; z++)
- {
- pTests[z]->StartThread(false);
- }
- }
- break;
- case 'x':
- {
- for (int x = 0; x < nConnNum; x++)
- {
- cout << "Try ResetConnection" << x << endl;
- ResetConnection(pConns[x]);
- //Sleep(2500);
- cout << "ResetConnection Over" << x << endl;
- }
- }
- break;
- case 'q':
- {
- break;
- }
- }
- if (c == 'q')
- break;
- std::cout << "Please Input Command " << endl;
- usleep(10000);
- }
- for (int x = 0; x < nConnNum; x++)
- {
- CloseConnection(pConns[x]);
- }
- delete[] pConns;
- std::cout << "Hello World!\n";
- return 0;
- }
- }
-
- bool status = true;
- //这里把多个 通OEMDriver的 xml一起启动
- if (EntryCheck(argc, argv) == false)
- {
- return false;
- }
- if (GetCommandDispathIF()->InitAs(CCOS_PROC_MASTER, (UINT64)getpid()))
- {
- // 设置信号处理
- signal(SIGINT, ConsoleHandler);
- signal(SIGTERM, ConsoleHandler);
- //1. do the bus test
- //Sleep(3000);
- LogicClient buClient("ccosproc_exe", "", "", false);
- if (buClient.Open((const char*)getRootpath(), ALL_ACCESS) == RET_SUCCEED)
- {
- //buClient.ExitDriverProc();
- buClient.Close();
- //DWORD count = buClient.GetDeviceCount();
- //for (DWORD i = 0; i < count; i++)
- //{
- // UINT64 ProcId, Addr;
- // ResDataObject DevType,devpath, MachineId;
- // buClient.GetDeviceDescript(i, devpath, DevType, MachineId, ProcId, Addr);
- // printf("path:%s\nProc:%I64u\nAddr:%I64u\n", (const char*)devpath, ProcId, Addr);
- // LogicClient DevClient("ccosproc_exe_temp","ccosproc");
- // if (DevClient.Open((const char*)devpath, ALL_ACCESS) >= RET_SUCCEED)
- // {
- // DevClient.Close();
- // printf("Open Succeed\n");
- // }
- // else
- // {
- // printf("Open Failed\n");
- // }
- //}
- status = true;
- }
- else
- {
- status = false;
- }
- //start work
- if (status)
- {
- if (StartConsoleThreads(true))
- {
- printf("Done Init Channel.going to sleep...\n");
- while (1) {
- if (GetCommandDispathIF()->WaitExit(500) == true)
- {
- StartConsoleThreads(false);
- return 1;
- }
- }
- }
- }
- printf("Can't Start Console Thread.Exit in 3sec\n");
- sleep(3);
- return 0;
- }
- return 0;
- }
- //int _tmain(int argc, _TCHAR* argv[])
- //{
- // printf("Main Thread:%d\n", GetCurrentThreadId());
- // if (GetCommandDispathIF()->InitAs(true, (UINT64)GetCurrentProcessId()))
- // {
- // if (SetConsoleCtrlHandler((PHANDLER_ROUTINE)ConsoleHandler, true))
- // {
- // //1. do the bus test
- // BusUnitClient buClient;
- // if (buClient.Open(getRootpath().c_str(), ALL_ACCESS) == RET_SUCCEED)
- // {
- // buClient.Close();
- // DWORD count = buClient.GetDeviceCount();
- // for (DWORD i = 0; i < count; i++)
- // {
- // UINT64 ProcId, Addr;
- // ResDataObject devpath;
- // buClient.GetDeviceDescript(i, devpath, ProcId, Addr);
- // printf("path:%s\nProc:%I64u\nAddr:%I64u\n", (const char*)devpath, ProcId, Addr);
- // }
- // }
- //
- // //1. do the Wheel test
- // DWORD loop = 8;
- // CommonLogicClient WheelClient;
- // HANDLE notifyhandle = WheelClient.GetNotifyHandle();
- // if (WheelClient.Open("/ccosAlpha/Major/Minor/Ecom/AbstractSeq/1234/{4E4206DA-DA34-4E71-7F42-97E0ABFF3F81}",
- // ALL_ACCESS) == RET_SUCCEED)
- // {
- // while (loop--)
- // {
- // ResDataObject Request,Response;
- // WheelClient.Action("OpenLogicDevice", Request, Response);
- // WheelClient.Action("GetSEQResource", Request, Response);
- // WheelClient.Action("FrameInState", Request, Response);
- // //bool status = false;
- // //PACKET_CMD cmd;
- // //ResDataObject data;
- // //WheelClient.StartRoll();
- // ////Notify
- // //cmd = PACKET_CMD_NONE;
- // //printf("Wait for a notify\n");
- // //while (cmd == PACKET_CMD_NONE)
- // //{
- // // WaitForSingleObject(notifyhandle, INFINITE);
- // // while (WheelClient.IsDataArrived())
- // // {
- // // cmd = WheelClient.ReadCmd(data);
- // // printf("cmd:%d\n", cmd);
- // // if (cmd == PACKET_CMD_UPDATE)
- // // {
- // // //got update notify
- // // WheelClient.GetLocalStatus(status);
- // // printf("current status :%d\n", status);
- // // }
- // // }
- // //}
- //
- // //WheelClient.StopRoll();
- // ////Notify
- // //cmd = PACKET_CMD_NONE;
- // //printf("Wait for a notify\n");
- // //while (cmd == PACKET_CMD_NONE)
- // //{
- // // WaitForSingleObject(notifyhandle, INFINITE);
- // // while (WheelClient.IsDataArrived())
- // // {
- // // cmd = WheelClient.ReadCmd(data);
- // // printf("cmd:%d\n", cmd);
- // // if (cmd == PACKET_CMD_UPDATE)
- // // {
- // // //got update notify
- // // WheelClient.GetLocalStatus(status);
- // // printf("current status :%d\n", status);
- // // }
- // // }
- // //}
- //
- // }
- //
- // WheelClient.Close();
- // }
- //
- // //printf("Press Ctrl-C or [x] to close...\n");
- // while (1){ Sleep(2000); }
- // }
- //
- // }
- // return 0;
- //}
|