12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481 |
- #include <stdio.h>
- #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<DeviceDescript> 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<string, DeviceDescript>::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<string, DeviceDescript>::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<string, DeviceDescript>::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<string, DeviceDescript>::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
- /// <summary>
- /// 测试连接Channel,本进程是否OK
- /// </summary>
- /// <param name="Local">是否本地</param>
- /// <returns></returns>
- 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<std::shared_ptr<LinuxEvent>> 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);
- }
|