#include #include "DevBusManager.h" #include "LocalConfig.h" #include "common_api.h" #include "PacketAnalizer.h" //#include "HandleManager.h" #include "P2PModule.h" #ifndef _CRT_SECURE_NO_WARNINGS #define _CRT_SECURE_NO_WARNINGS #endif inline string CurrentDateTime() { std::time_t now = std::chrono::system_clock::to_time_t(std::chrono::system_clock::now()); char buf[100] = { 0 }; std::strftime(buf, sizeof(buf), " %Y-%m-%d %H:%M:%S ", std::localtime(&now)); return buf; } DevBusManager g_BusManager; //Log4CPP::Logger* ////mLog::gLogger; //------------bus manager--------------------------- DevBusManager::DevBusManager() { //m_pBusLocal = NULL; m_pLocalChannelClient = NULL; //m_pBusEth = NULL; //2025-2-21 m_pEthChannelClient = NULL; m_EnableEth = 0;//default is 0 m_prootClient = NULL; SetName("DevBusManager"); } DevBusManager::~DevBusManager() { UnloadBusDrv(); } //#define NOTIFYCHANNEL ("Dev.DeviceService") //void DevBusManager::NotifyChannel_old(bool Local, bool Connect) //{ // bool ret; // //criticalsection // pthread_mutex_lock(&m_Critical); // if (m_prootClient->GetDeviceStatus() == 1) // { // ResDataObject sub; // try { // sub.add("Source", getLocalEbusId()); // sub.add("Sender", ""); // sub.add("Content", getRootpath()); // sub.add("MediaType", ""); // sub.add("RequestId", ""); // sub.add("PrivateData", ""); // sub.add("ResourceNodeName", NOTIFYCHANNEL); // sub.add("AppRequest", ""); // sub.add("Timestamp", ""); // // if (Local == true) // { // if (m_pBusLocal != NULL && m_pBusLocal->IsConnected() == true) // { // if (Connect) // { // //make packet // sub.add("RequestOperation", "Create"); // printf("Send Notify to Local Channel Create\n"); // } // else // { // //make packet // sub.add("RequestOperation", "Delete"); // printf("Send Notify to Local Channel Delete\n"); // } // //Send // ret = m_pBusLocal->SendRawPacket(NOTIFYCHANNEL, sub.encode(), CCOS_HW_CHANNEL + 2); // if (ret == true) // { // printf("Send Notify to Local Channel OK\n"); // } // else // { // printf("Send Notify to Local Channel Failed\n"); // } // // } // else // { // printf("can't Send Notify to Local Channel .Local bus is Disconnected\n"); // } // } // else // { // if (m_pBusEth != NULL && m_pBusEth->IsConnected() == true) // { // if (Connect) // { // //make packet // sub.add("RequestOperation", "Create"); // printf("Send Notify to Eth Channel Create\n"); // } // else // { // //make packet // sub.add("RequestOperation", "Delete"); // printf("Send Notify to Eth Channel Delete\n"); // } // //Send // ret = m_pBusEth->SendRawPacket(NOTIFYCHANNEL, sub.encode(), CCOS_HW_CHANNEL + 2); // if (ret == true) // { // printf("Send Notify to Eth Channel OK\n"); // } // else // { // printf("Send Notify to Eth Channel Failed\n"); // } // // } // else // { // printf("can't Send Notify to Eth Channel .Eth bus is Disconnected\n"); // } // // } // // // // } // catch (...) // { // printf("Send Notify to Eth Channel Failed.unknown exp happen\n"); // // } // // } // else // { // printf("Can't Send Notify to Channel:%d with Notify:%d .Device is not ready\n", Local, Connect); // } // // pthread_mutex_unlock(&m_Critical); //} //good void DevBusManager::NotifyChannel(bool Local, bool Connect) { //DeviceBUS *pBus = NULL; BusUnitClient *pClient = NULL; if (Local) { //pBus = m_pBusLocal; pClient = m_pLocalChannelClient; } else { //pBus = m_pBusEth; //2025-2-21 pClient = m_pEthChannelClient; } //criticalsection pthread_mutex_lock(&m_Critical); try { if ( pClient != NULL && pClient->IsClosed() == false) { //machineid //localbusid //procid //address ResDataObject Path; CcosDevFileHandle Handle; if (m_prootClient->GetFileHandle(Handle) && m_prootClient->GetFilePath(Path)) { CCOS_PROC_TYPE master = GetConfigMode(); bool NotChannel = (master != CCOS_PROC_CHANNEL); if (Connect) { //printf("AddDevice Dev:%s", (const char*)Path); ////mLog::FINFO("AddDevice Dev:{$},MachineId:{$},PId:{$} ,Addr:{$} ,Force:{$}", (const char*)Path, (const char*)Handle.GetTarget(false)[CCOS_PACK_TARGET_MACHINEID], (UINT64)Handle.GetTarget(false)[CCOS_PACK_TARGET_PROCID], (UINT64)Handle.GetTarget(false)[CCOS_PACK_TARGET_ADDR], NotChannel ); //regist rootdev to channel pClient->AddDeviceDescrpt( (const char*)Path, (const char *)"Root", Handle.GetTarget(false)[CCOS_PACK_TARGET_MACHINEID], Handle.GetTarget(false)[CCOS_PACK_TARGET_PROCID], Handle.GetTarget(false)[CCOS_PACK_TARGET_ADDR], NotChannel );//force to update } else { //unregist rootdev from channel //exption:channel can't del itself if (NotChannel) { //printf("DelDevice Dev:%s", (const char*)Path); ////mLog::FINFO("DelDevice Dev:{$}", (const char*)Path); pClient->DelDeviceDescrpt((const char*)Path); } } } } else { ////mLog::FINFO("Notify Channels env is not right"); } } catch (...) { //printf("Send Notify to Channel Failed.unknown exp happen\n"); ////mLog::FINFO("Send Notify to Channel Failed.unknown exp happen\n"); } pthread_mutex_unlock(&m_Critical); } //good void DevBusManager::ClearChannelList(bool Local) { pthread_mutex_lock(&m_Critical); if (Local) { m_LocalChannelDevList.clear(); } else { m_EthChannelDevList.clear(); } pthread_mutex_unlock(&m_Critical); } //good void DevBusManager::UpdateChannelList(bool Local) { BusUnitClient *pClient = NULL; vector ddlist; ////mLog::FINFO("UpdateChannelList {$}", Local); if (Local) { pClient = m_pLocalChannelClient; } else { //2025-2-21 pClient = m_pEthChannelClient; } if (pClient && pClient->IsClosed() == false) { UINT64 Proc = 0; UINT64 Addr = 0; ResDataObject DevType,DevPath, MId; for (DWORD i = 0; i < pClient->GetDeviceCount(); i++) { if (pClient->GetDeviceDescript(i, DevPath, DevType, MId, Proc, Addr) == false) { break; } DeviceDescript dev; dev.SetKey((const char*)DevPath); dev.m_TargetType = (const char*)DevType; dev.m_MachineId = (const char*)MId; dev.m_ProcId = Proc; dev.m_Address = Addr; ddlist.push_back(dev); } } pthread_mutex_lock(&m_Critical); string lBusId; if (Local) { m_LocalChannelDevList.clear(); for (size_t i = 0; i < ddlist.size(); i++) { lBusId = ""; if (getBusIdFromFilePath(ddlist[i].GetKey(), lBusId)) { //got busId from path map::iterator proc_iter = m_LocalChannelDevList.find(lBusId); if (proc_iter != m_LocalChannelDevList.end()) { //printf("dd exist\n"); //for debug //for (size_t ei = 0; ei < ddlist.size(); ei++) //{ //printf("ddlist[%zu].BusId:%s\n",ei, ddlist[ei].GetKey()); //} //printf("Channel exist\n"); map::iterator iterdebug = m_LocalChannelDevList.begin(); while (iterdebug != m_LocalChannelDevList.end()) { //printf("channelexist.BusId:%s\n", iterdebug->first.c_str()); ++iterdebug; } ////mLog::FERROR("same name Multiple busid exist in the Local Channel.BusId:{$}",lBusId); continue; } //insert m_LocalChannelDevList[lBusId] = ddlist[i]; } else { ////mLog::FERROR("Wrong Dev Path:{$}", ddlist[i].GetKey()); } } } else { m_EthChannelDevList.clear(); for (size_t i = 0; i < ddlist.size(); i++) { lBusId = ""; if (getBusIdFromFilePath(ddlist[i].GetKey(), lBusId)) { //got busId from path map::iterator proc_iter = m_EthChannelDevList.find(lBusId); if (proc_iter != m_EthChannelDevList.end()) { ////mLog::FERROR("same name Multiple busid exist in the Eth Channel.BusId:{$}", lBusId); continue; } //insert m_EthChannelDevList[lBusId] = ddlist[i]; } else { ////mLog::FERROR("Wrong Dev Path:{$}", ddlist[i].GetKey()); } } } pthread_mutex_unlock(&m_Critical); } //good bool DevBusManager::DoLocalChannelPrcedure(void) { ////mLog::FINFO("do..something local"); ResDataObject packet, Context; try { //printf("Thread:%d,DrvTree Thread Loop\n", GetCurrentThreadId()); //Local Channel存在 且 所有的设置都正常(其他判断应该在TIMEOUT里面进行) if (m_pLocalChannelClient->IsClosed() == false) { //只接收通知 //设备更新通知,ADD&DEL DEVICE, //Ready通知,退出通知,UPDATE exitFlag PACKET_CMD cmd; do { cmd = m_pLocalChannelClient->ReadCmd(packet); if (cmd == PACKET_CMD_UPDATE) { if (m_pLocalChannelClient->GetDeviceUsage() == false) { //channel not ready continue; } } if (cmd == PACKET_CMD_ADD || cmd == PACKET_CMD_DEL) { UpdateChannelList(true); } else { //ignore others //printf("Thread:%d,DrvTree Thread Ignore\n", GetCurrentThreadId(), cmd); } } while (cmd != PACKET_CMD_NONE); } else { //printf("Thread:%d,Local Channel not in Open State\n", GetCurrentThreadId()); TryConnectChannel(true); } } catch (...) { //printf("Thread:%d,Exeption in DrvTree Thread\n", GetCurrentThreadId()); return false; } return true; } //good bool DevBusManager::DoEthChannelPrcedure(void) { return true; //2025.2.21 /* ////mLog::FINFO("do..something Eth"); ResDataObject packet, Context; try { //printf("Thread:%d,DrvTree Thread Loop\n", GetCurrentThreadId()); //2025-2-21 if (m_pEthChannelClient == NULL) { return false; } PACKET_CMD cmd; //Local Channel存在 且 所有的设置都正常(其他判断应该在TIMEOUT里面进行) if (m_EnableEth) { if (m_pEthChannelClient->IsClosed() == false) { //只接收通知 //设备更新通知,ADD&DEL DEVICE, //Ready通知,退出通知,UPDATE exitFlag do { cmd = m_pEthChannelClient->ReadCmd(packet); if (cmd == PACKET_CMD_UPDATE) { if (m_pEthChannelClient->GetDeviceUsage() == false) { //channel not ready continue; } } if (cmd == PACKET_CMD_ADD || cmd == PACKET_CMD_DEL) { UpdateChannelList(false); } else { //ignore others //printf("Thread:%d,DrvTree Thread Ignore\n", GetCurrentThreadId(), cmd); } } while (cmd != PACKET_CMD_NONE); } else { //printf("Thread:%d,Eth Channel not in Open State\n", GetCurrentThreadId()); TryConnectChannel(false); } } else { //already disconnected //just clear the packet in the Client do { cmd = m_pEthChannelClient->ReadCmd(packet); } while (cmd != PACKET_CMD_NONE); } } catch (...) { //printf("Thread:%d,Exeption in DrvTree Thread\n", GetCurrentThreadId()); return false; } */ return true; } //good CCOS_PACKET_ROUTE DevBusManager::GetBusIdDirection(string &BusId) { string lBusId; if (BusId.size() == 0) { return CCOS_PACKET_ROUTE_NOTARGET; } pthread_mutex_lock(&m_Critical); map::iterator proc_iter = m_LocalChannelDevList.find(BusId); if (proc_iter != m_LocalChannelDevList.end()) { //got match //got match pthread_mutex_unlock(&m_Critical); return CCOS_PACKET_ROUTE_LOCAL; } proc_iter = m_EthChannelDevList.find(BusId); if (proc_iter != m_EthChannelDevList.end()) { //got match //got match pthread_mutex_unlock(&m_Critical); return CCOS_PACKET_ROUTE_ETH; } pthread_mutex_unlock(&m_Critical); return CCOS_PACKET_ROUTE_ANY; } //good /// /// 测试连接Channel,本进程是否OK /// /// 是否本地 /// bool DevBusManager::TryConnectChannel(bool Local) { return true; //DeviceBUS *pBus = NULL; BusUnitClient *pClient = NULL; //printf("TryConnectChannel Local:%d\n", Local); ////mLog::FINFO("TryConnectChannel Local:{$}\n", Local); if (Local) { //pBus = m_pBusLocal; pClient = m_pLocalChannelClient; } else { //pBus = m_pBusEth; //2025.2.21 pClient = m_pEthChannelClient; } if ( pClient != NULL) { //disconnect first //printf("Disconnect Local(%d) first \n",Local); ////mLog::FINFO("Disconnect Local({$}) first \n", Local); if (pClient->IsClosed() == false) { NotifyChannel(Local, false); pClient->Close(); } BusUnitClient ThisProc("CCOS_CHANNEL_TEST_CLIENT"); DWORD tick = GetTickCount(); if (ThisProc.Open(getRootpath(), ALL_ACCESS, MAX_CHECK_TIME_PERIOD*4 ) >= RET_SUCCEED) { tick = GetTickCount() - tick; if (ThisProc.GetDeviceUsage()) { //this proc is ready //reconnect sec //printf("open Local(%d) channel:%s \n", Local, (const char*)getChannelRootpath()); ////mLog::FINFO("open Local({$}) channel:{$} \n", Local, (const char*)getChannelRootpath()); RET_STATUS ret = (RET_STATUS)pClient->Open((const char*)getChannelRootpath(), ALL_ACCESS, MAX_CHECK_TIME_PERIOD / 2); if (ret >= RET_SUCCEED) { if (pClient->GetDeviceUsage()) { //it's ready UpdateChannelList(Local); //regist procRoot as device NotifyChannel(Local, true); //second clear ThisProc.Close(); //printf("TryConnectChannel Local(%d) Ready.ClientStatus:%d\n", Local, pClient->IsClosed()); ////mLog::FINFO("TryConnectChannel Local({$}) Ready.ClientStatus:{$}\n", Local, pClient->IsClosed()); return true; } //printf("TryConnectChannel Local(%d).channel exist,but not ready\n", Local); ////mLog::FINFO("TryConnectChannel Local({$}).channel exist,but not ready\n", Local); //channel exist,but not ready pClient->Close(); } else { //printf("open Local(%d) channel:%s Failed.RetCode:%d\n", Local, (const char*)getChannelRootpath(),ret); ////mLog::FERROR("open Local({$}) channel:{$} Failed.RetCode:{$}\n", Local, (const char*)getChannelRootpath(), (int)ret); } } else { ////mLog::FINFO("Dev {$} Is not in Usage State", (const char*)getRootpath()); } ThisProc.Close(); } else { ////mLog::FINFO("Open {$} Failed", (const char*)getRootpath()); } } ClearChannelList(Local); //printf("TryConnectChannel Local(%d) Not Ready\n", Local); ////mLog::FINFO("TryConnectChannel Local({$}) Not Ready\n", Local); return false; } //good bool DevBusManager::TryDisConnectChannel(bool Local) { //DeviceBUS *pBus = NULL; BusUnitClient *pClient = NULL; if (Local) { //pBus = m_pBusLocal; pClient = m_pLocalChannelClient; } else { //pBus = m_pBusEth; //2025.2.21 pClient = m_pEthChannelClient; } if ( pClient != NULL ) { if (pClient->IsClosed() == false) { if (pClient->GetDeviceUsage()) { //unregist procRoot as device NotifyChannel(Local, false); } pClient->Close(); } } ClearChannelList(Local); return true; } //using set_ws_param = std::tuple< SubSystemDevImpl*, const char*, int >; void* DevBusManagerExecThreadFunc(void* pPara) { DevBusManager* pDevManger = (DevBusManager*)pPara; pDevManger->ExecFunc(); return 0; } bool DevBusManager::ExecFunc(void) { DWORD waitcount = 3; std::vector> vec = { 0 }; vec[0] = m_ExitFlag; vec[1] = m_prootClient->GetNotifyHandle(); vec[2] = m_pLocalChannelClient->GetNotifyHandle(); //2025.2.21 //if (m_pEthChannelClient) //{ // vec[3] = m_pEthChannelClient->GetNotifyHandle(); // waitcount = 4; //} DWORD wait = LinuxEvent::WaitForMultipleEvents(vec, MAX_CHECK_TIME_PERIOD); //printf("Thread:%d,hit DrvTree Entry.code:%d\n", GetCurrentThreadId(), wait); if (wait == WAIT_TIMEOUT) { //timeout //检查 LocalChannel 和EthChannel 的状态,并更新 //just try it //在Bus处于连接状态下,检查Client的状态 //2025.2.21 B //if (m_pLocalChannelClient->IsClosed() == true) //{ // ////mLog::FINFO("LocalChannelClient is Closed.try connect local channel"); // TryConnectChannel(true); //} //if (m_pBusEth != NULL && m_pBusEth->IsConnected()) // //2025.2.21 //if(m_pEthChannelClient) //{ // if (m_pEthChannelClient->IsClosed() == true) // { // ////mLog::FINFO("EthChannelClient is Closed.try connect eth channel"); // TryConnectChannel(false); // } //} } else if (wait == WAIT_OBJECT_0) { ////mLog::FINFO("Exit Evt .Exit Thread"); return false; } else if (wait == WAIT_OBJECT_0 + 1) { //got notify if (DoRootDevPrcedure() == false) { return false; } } else if (wait == WAIT_OBJECT_0 + 2) { //do local channel work //got notify if (DoLocalChannelPrcedure() == false) { return false; } } else if (wait == WAIT_OBJECT_0 + 3) { //do eth channel work //got notify if (DoEthChannelPrcedure() == false) { return false; } } //if (m_pBusEth) //{ // if (m_pBusEth->IsConnected() == false && m_EnableEth != 0) // { // printf("Eth DisConnected.Enable Eth is TRUE: Try Connect\n"); // ////mLog::FINFO("Eth DisConnected.Enable Eth is TRUE: Try Connect\n"); // if (m_pBusEth->Connect(m_EthConnection)) // { // printf("Eth Connection: Succeed\n"); // ////mLog::FINFO("Eth Connection: Succeed\n"); // TryConnectChannel(false); // } // else // { // printf("Eth Connection: Failed\n"); // ////mLog::FINFO("Eth Connection: Failed\n"); // } // } //} //if (m_pBusLocal) //{ // //check local // if (m_pBusLocal->IsConnected() == false) // { // printf("Local Connection: Try Connect\n"); // ////mLog::FERROR("Local Connection: Try Connect\n"); // if (m_pBusLocal->Connect(m_LocalConnection)) // { // printf("Local Connection: Succeed\n"); // ////mLog::FINFO("Local Connection: Succeed\n"); // TryConnectChannel(true); // } // else // { // printf("Local Connection: Failed\n"); // ////mLog::FERROR("Local Connection: Failed\n"); // } // } //} return true; } //good bool DevBusManager::Exec(void) { pthread_t threadID; pthread_attr_t attr; struct timespec ts; int ret = 0; // 初始化线程属性(可连接状态) pthread_attr_init(&attr); pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE); // 创建线程 if (pthread_create(&threadID, &attr, DevBusManagerExecThreadFunc, this) != 0) { pthread_attr_destroy(&attr); return false; } pthread_attr_destroy(&attr); // 计算45秒后的绝对时间(用于超时控制) struct timeval now; gettimeofday(&now, NULL); ts.tv_sec = now.tv_sec + 45; ts.tv_nsec = now.tv_usec * 1000; // 带超时的线程等待 ret = pthread_timedjoin_np(threadID, NULL, &ts); if (ret == 0) { usleep(1000000); // 睡眠1秒(1000毫秒) return true; } // 超时或错误处理 if (ret == ETIMEDOUT) { pthread_detach(threadID); // 防止资源泄漏 } return false; } //good void DevBusManager::UpdateEthConnectOption(int Enable, ResDataObject &routerIp) { string temp; m_EthConnection.clear(); m_EnableEth = Enable; m_EthConnection.add(EBUS_CONNECTION_LOCAL, 0); temp = (const char*)getLocalEbusId(); if (temp.size() > 0) { m_EthConnection.add(EBUS_CONNECTION_BUSID, temp.c_str()); } temp = (const char*)getLocalEbusPort(); if (temp.size() > 0) { m_EthConnection.add(EBUS_CONNECTION_PORT, temp.c_str()); } if (m_EnableEth) { //go eth temp = (const char*)routerIp; if (temp.size() > 0) { m_EthRouterIp = temp.c_str(); m_EthConnection.add(EBUS_CONNECTION_ROUTERIP, temp.c_str()); } else { temp = (const char*)getLocalEbusRouterIp(); if (temp.size() > 0) { m_EthRouterIp = temp.c_str(); } else { m_EthRouterIp = "127.0.0.1"; } m_EthConnection.add(EBUS_CONNECTION_ROUTERIP, temp.c_str()); } } } //good bool DevBusManager::DoRootDevPrcedure(void) { ResDataObject packet, Context; try { //printf("Thread:%d,DrvTree Thread Loop\n", GetCurrentThreadId()); if (m_prootClient->IsClosed() == false) { PACKET_CMD cmd; do { cmd = m_prootClient->ReadCmd(packet); if (cmd == PACKET_CMD_UPDATE) { string key = PacketAnalizer::GetPacketKey(&packet); if (key == "Status") { if (m_prootClient->GetDeviceUsage()) { ////mLog::FINFO("Got Root Status Usage Evt.regist rootdev to channels "); NotifyChannel(true, true);//local NotifyChannel(false, true);//eth } else { //this proc not ready continue; } } else { //printf("Got Eth Connection Update\n"); //printf("Got Eth Connection Update\n"); ////mLog::FINFO("Got Other Updates"); ResDataObject Ip; m_prootClient->GetEthBusRouterIp(Ip); AdjustEthConnection(m_prootClient->GetEthBusSwitch(), Ip); } } else { //ignore others //printf("Thread:%d,DrvTree Thread Ignore\n", GetCurrentThreadId(), cmd); } } while (cmd != PACKET_CMD_NONE); } else { { //printf("Thread:%d,WTF Happen?? Failed\n", GetCurrentThreadId()); ////mLog::FINFO("Thread:{$},WTF Happen?? Failed\n", GetCurrentThreadId()); return false; } } } catch (...) { //printf("Thread:%d,Exeption in DrvTree Thread\n", GetCurrentThreadId()); ////mLog::FINFO("Thread:{$},Exeption in DrvTree Thread\n", GetCurrentThreadId()); return false; } return true; } //good void DevBusManager::AdjustEthConnection(int Enable, ResDataObject &routerIp) { //if (m_pBusEth) //{ // string temp; // //check the ip // const char* pIp = (const char*)routerIp; // bool sameip = false; // // if (pIp[0] != 0) // { // sameip = (m_EthRouterIp == routerIp); // } // else // { // sameip = true; // } // //printf("Eth Connection: Enable:%d,Ip:%s\n",Enable,(const char*)routerIp); // bool Changed = ((Enable != m_EnableEth) || !sameip); // if (Changed) // { // printf("Eth Connection: Changed.Disconnect\n"); // ////mLog::FINFO("Eth Connection: Changed.Disconnect Eth first.oldip:{$},newip:{$},oldEnableFlag:{$},newEnableFlag:{$}",(const char*)m_EthRouterIp ,pIp, m_EnableEth, Enable); // ///TODO : 需要将HANDLEmanger 接口转出 // //clear handles from eth // //g_HandleManager.ClearRemoteHandles(); // //unregist from channel // TryDisConnectChannel(false); // //update connections // UpdateEthConnectOption(Enable, routerIp); // //Disconnect ethbus here // m_pBusEth->DisConnect(); // } // if (m_EnableEth && m_pBusEth->IsConnected() == false) // { // //go eth or local // printf("Adjust Eth Connection: Try Connect\n"); // ////mLog::FINFO("Adjust Eth Connection: Try Connect\n"); // if (m_pBusEth->Connect(m_EthConnection)) // { // printf("Eth Connection: Succeed\n"); // ////mLog::FINFO("Eth Connection: Succeed\n"); // TryConnectChannel(false); // } // else // { // printf("Eth Connection: Failed\n"); // ////mLog::FINFO("Eth Connection: Failed\n"); // } // } //} } //good bool DevBusManager::OnStartThread() { //printf("========== DevBusManager::OnStartThread 0X%08X \n", GetCurrentThreadId()); ////mLog::FINFO("This is {$} BusManager Thread", (const char*)getRootpath()); //Sleep(5000); std::cout << "DevBusManager::OnStartThread - Thread ID: 0X" << std::hex << std::uppercase << GetCurrentThreadId() << std::dec << std::endl; std::cout << "DevBusManager::OnStartThread - Attempting to open root client with path: " << (const char*)getRootpath() << std::endl; if (m_prootClient->Open((const char*)getRootpath(), ALL_ACCESS) < RET_SUCCEED) { std::cout << "ERROR: DevBusManager OnStartThread m_prootClient Open FAILED - Path: " << (const char*)getRootpath() << std::endl; return false; } std::cout << "DevBusManager::OnStartThread - Successfully opened root client" << std::endl; //just try it //TryConnectChannel(true); //TryConnectChannel(false); return true; } //good bool DevBusManager::OnEndThread() { //printf("========== DevBusManager::OnEndThread 0X%08X \n", GetCurrentThreadId()); return true; } void DevBusManager::InitLog() { //string logfile = GetProcessDirectory() + "\\logs\\eBus_Status.log"; //Logger *p = CreateLogger(); //p->SetLogFilepath(logfile.c_str()); //SetLogger(p); } //good bool DevBusManager::LoadBusDrv() { CCOS_PROC_TYPE master = GetConfigMode(); //if (////mLog::gLogger == nullptr) //{ // string strLogPath = GetProcessDirectory() + R"(\Conf\Log4CPP.Config.xml)"; // string LogHost = ((string)getLogRootpath()).c_str(); // Log4CPP::ThreadContext::Map::Set(ECOM::Utility::Hash("LogFileName"), "eBus"); // //Log4CPP::GlobalContext::Map::Set("LogHost", LogHost.c_str()); // Log4CPP::ThreadContext::Map::Set(ECOM::Utility::Hash("LogHost"), LogHost.c_str() + 1); // auto rc = Log4CPP::LogManager::LoadConfigFile(strLogPath.c_str()); // ////mLog::gLogger = Log4CPP::LogManager::GetLogger("eBus"); // ////mLog::FINFO("Code Build datetime [{$} {$}]", __DATE__, __TIME__); //} //share memory settting std::cout << "-*-*-* LoadBusDrv Start" << CurrentDateTime() << "" << endl; ////mLog::FINFO("InitEbusEnv"); //InitEbusEnv(getEbusLogFlag()); ResDataObject P2pServer; ResDataObject P2pClient; bool P2pEnable = getP2pFlag(); if (P2pEnable) { P2pServer = getP2pServerIp(); P2pClient = getP2pRole(); } //load local //if (master == 0 || master == 1 || master == 2) { //Slave&Master&Channel&Client m_pLocalChannelClient = new BusUnitClient("LocalChannelClient"); m_pLocalChannelClient->SetRouter(CCOS_PACKET_ROUTE_LOCAL); //m_pLocalChannelClient->SetClientRootID(, ""); //m_pBusLocal = (DeviceBUS*)new eBus(); //if (master == CCOS_PROC_MASTER && P2pEnable) //{ // if ((bool)P2pClient == true) // { // ////mLog::FINFO("InitP2P {$}", (const char*)P2pServer); // m_pBusLocal->InitP2P((const char*)P2pServer, (const char*)getLocalEbusId(), false); // } //} //m_pBusLocalThread = new BusThread(); //string logfile = GetProcessDirectory() + "\\logs\\Local_ebusDispathlog.log"; //m_pBusLocalLog = CreateLogger(); //m_pBusLocalLog->SetLogFilepath(logfile.c_str()); //m_pBusLocalThread->SetLogger(m_pBusLocalLog); //m_pBusLocalThread->SetName("BusLocalThread"); //m_pBusLocal->SetReceiveThread(m_pBusLocalThread); // //logfile = GetProcessDirectory() + "\\logs\\Local_ebuslog.log"; //m_pBusLocal->SetLogPath(logfile.c_str()); } std::cout << "-*-*-* LocalChannelClient over." << CurrentDateTime() << "" << endl; ////mLog::FINFO("LocalChannelClient over."); //load eth //if (master == 1 || master == 3) //Channel & Slave no need to connect eth if (master == CCOS_PROC_MASTER ) { //Master&Client // //2025.2.21 //m_pEthChannelClient = new BusUnitClient("EthChannelClient"); //m_pEthChannelClient->SetRouter(CCOS_PACKET_ROUTE_ETH); //m_pEthChannelClient->SetClientRootID(, "") //m_pBusEth = (DeviceBUS*)new eBus(); //if (master == CCOS_PROC_MASTER && P2pEnable) //{ // if ((bool)P2pClient == false) // { // m_pBusEth->InitP2P((const char*)P2pServer, (const char*)getLocalEbusId(), true); // } //} //m_pBusEthThread = new BusThread(); //m_pBusEthThread->m_bLocal = false;//make thread notice packet they received is from where //string logfile = GetProcessDirectory() + "\\logs\\Eth_ebusDispathlog.log"; //m_pBusEthLog = CreateLogger(); //m_pBusEthLog->SetLogFilepath(logfile.c_str()); //m_pBusEthThread->SetLogger(m_pBusEthLog); //m_pBusEthThread->SetName("BusEthThread"); //m_pBusEth->SetReceiveThread(m_pBusEthThread); //logfile = GetProcessDirectory() + "\\logs\\Eth_ebuslog.log"; //m_pBusEth->SetLogPath(logfile.c_str()); } if (m_prootClient == NULL) { m_prootClient = new BusUnitClient("DevBusManager_rootClient"); } //connect local //if (m_pBusLocal) //{ // ////mLog::FINFO("Connect Local Ebus"); // m_LocalConnection.add(EBUS_CONNECTION_LOCAL, 1); // //m_LocalConnection.add(EBUS_CONNECTION_BUSID, (UINT64)GetCurrentProcessId()); // m_LocalConnection.add(EBUS_CONNECTION_BUSID, (const char*)getLocalEbusId()); // // // if (m_pBusLocal->Connect(m_LocalConnection) == false) // { // return false; // } // if (m_pBusLocalThread->StartThread() == false) // { // return false; // } // ////mLog::FINFO("Connect Local Ebus Over."); //} //if (m_pBusEth) //{ // ////mLog::FINFO("Connect Eth Ebus"); // //try connect eth // int EnableEth = m_prootClient->GetEthBusSwitch(); // ResDataObject Ip; // m_prootClient->GetEthBusRouterIp(Ip); // UpdateEthConnectOption(EnableEth, Ip); // if (m_EnableEth) // { // if (m_pBusEth->Connect(m_EthConnection)) // { // printf("ETH Connect Succeed in the Entry\n"); // //just try it // } // else // { // printf("ETH Connect Failed in the Entry\n"); // } // } // if (m_pBusEthThread->StartThread() == false) // { // return false; // } // ////mLog::FINFO("Connect Eth Ebus over."); //} //std::cout << "-*-*-* Connect Ebus over." << CurrentDateTime() << "" << endl; InitLog(); bool ret = StartThread(); std::cout << "-*-*-* StartThread over." << CurrentDateTime() << "" << endl; return ret; } //good bool DevBusManager::UnloadBusDrv() { StopThread(100); //if (m_pBusLocal) { NotifyChannel(true, false); } //if (m_pBusEth) { NotifyChannel(false, false); } if (m_pLocalChannelClient) { ////mLog::FINFO("Disconnect Local EBUS"); //if (m_pBusLocal->IsConnected()) { m_pLocalChannelClient->Close(); } //m_pBusLocal->DisConnect(); //m_pBusLocal->Quit(); //////mLog::FINFO("Stop Local Ebus Thread"); //if (m_pBusLocalThread->StopThread(100) == false) //{ // ////mLog::FINFO("Local Receive Bus Thread:StopThread Used Terminate!!!!!!!!!!!!!!!"); //} //////mLog::FINFO("Del Local Ebus Handles"); //if (m_pBusLocalLog) //{ // ReleseLogger(m_pBusLocalLog); // m_pBusLocalLog = NULL; //} delete m_pLocalChannelClient; m_pLocalChannelClient = NULL; //delete m_pBusLocal; //m_pBusLocal = NULL; //delete m_pBusLocalThread; //m_pBusLocalThread = NULL; } //if (m_pBusEth) { ////mLog::FINFO("Disconnect Eth EBUS"); //if (m_pBusEth->IsConnected()) // //2025.2.21 //if(m_pEthChannelClient) //{ // m_pEthChannelClient->Close(); // delete m_pEthChannelClient; // m_pEthChannelClient = NULL; //} //m_pBusEth->DisConnect(); //m_pBusEth->Quit(); //////mLog::FINFO("Stop Eth Ebus Thread"); //if (m_pBusEthThread->StopThread(100) == false) //{ // ////mLog::FINFO("Eth Receive Bus Thread:StopThread Used Terminate!!!!!!!!!!!!!!!"); //} //////mLog::FINFO("Del Eth Ebus Handles"); //if (m_pBusEthLog) //{ // ReleseLogger(m_pBusEthLog); // m_pBusEthLog = NULL; //} //delete m_pBusEth; //m_pBusEth = NULL; //delete m_pBusEthThread; //m_pBusEthThread = NULL; } if (m_prootClient) { ////mLog::FINFO("Del Root Drv Handle"); m_prootClient->Close(); delete m_prootClient; m_prootClient = NULL; } //Logger *pBusLogger = (Logger *)GetLogger(); //if (pBusLogger) //{ // ReleseLogger(pBusLogger); // SetLogger(NULL); //} //QuitEbusEnv(); ////mLog::FINFO("Done Clear DevBusManager"); return true; } //good bool DevBusManager::SendSMPacket(ResDataObject &packet, bool Local, string &TargetbusId, unsigned long long nShareMemID) { //DeviceBUS *pBus = NULL; //if (Local) //{ // if (m_pBusLocal) // { // pBus = m_pBusLocal; // } //} //else //{ // if (m_pBusEth) // { // pBus = m_pBusEth; // } //} //if (pBus) //{ // return pBus->SendSMPacket(TargetbusId.c_str(), packet.encode(), nShareMemID); //} return false; } //good bool DevBusManager::SendPacket(ResDataObject &packet, bool Local, string &TargetbusId, char* pBlockData, DWORD Size) { //DeviceBUS *pBus = NULL; //if (Local) //{ // if (m_pBusLocal) // { // ////mLog::FINFO("Select Local Bus"); // pBus = m_pBusLocal; // } //} //else //{ // if (m_pBusEth) // { // ////mLog::FINFO("Select Eth Bus"); // pBus = m_pBusEth; // } //} //if (pBus) //{ // return pBus->SendPacket(TargetbusId.c_str(), packet.encode(), pBlockData, Size); //} //////mLog::FINFO("Select None Bus"); return false; } void DevBusManager::UnRegistThread(DWORD Tid) { //if (m_pBusLocal) //{ // m_pBusLocal->UnRegistThread(Tid); //} //if (m_pBusEth) //{ // m_pBusEth->UnRegistThread(Tid); //} } DEVICEBUS_API bool LoadBusDrv() { return g_BusManager.LoadBusDrv(); } DEVICEBUS_API bool UnloadBusDrv() { return g_BusManager.UnloadBusDrv(); } DEVICEBUS_API void BusUnRegistThread(DWORD tid) { //g_BusManager.UnRegistThread(tid); } DEVICEBUS_API CCOS_PACKET_ROUTE GetBusIdDirection(string& BusId) { return CCOS_PACKET_ROUTE_NOTARGET;// g_BusManager.GetBusIdDirection(BusId); } DEVICEBUS_API bool BusSendSMPacket(ResDataObject& packet, bool Local, string& TargetbusId, unsigned long long nShareMemID) { return true;// g_BusManager.SendSMPacket(packet, Local, TargetbusId, nShareMemID); } DEVICEBUS_API bool BusSendPacket(ResDataObject& packet, bool Local, string& TargetbusId, char* pBlockData, DWORD Size) { return true;// g_BusManager.SendPacket(packet, Local, TargetbusId, pBlockData, Size); }