123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479 |
- #include <stdio.h>
- #include "DevBusManager.h"
- #include "LocalConfig.h"
- #include "common_api.h"
- #include "PacketAnalizer.h"
- #include "P2PModule.h"
- #include "LogLocalHelper.h"
- #include "Log4CPP.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* 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);
- 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);
- FINFO("DelDevice Dev:{$}", (const char*)Path);
- pClient->DelDeviceDescrpt((const char*)Path);
- }
- }
- }
- }
- else
- {
- FINFO("Notify Channels env is not right");
- }
- }
- catch (...)
- {
- //printf("Send Notify to Channel Failed.unknown exp happen\n");
- 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;
- 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;
- }
- FERROR("same name Multiple busid exist in the Local Channel.BusId:{$}",lBusId);
- continue;
- }
- //insert
- m_LocalChannelDevList[lBusId] = ddlist[i];
-
- }
- else
- {
- 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())
- {
- FERROR("same name Multiple busid exist in the Eth Channel.BusId:{$}", lBusId);
- continue;
- }
- //insert
- m_EthChannelDevList[lBusId] = ddlist[i];
- }
- else
- {
- FERROR("Wrong Dev Path:{$}", ddlist[i].GetKey());
- }
- }
- }
- pthread_mutex_unlock(&m_Critical);
- }
- //good
- bool DevBusManager::DoLocalChannelPrcedure(void)
- {
- 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
- /*
- 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);
- 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);
- 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());
- 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());
- FINFO("TryConnectChannel Local({$}) Ready.ClientStatus:{$}\n", Local, pClient->IsClosed());
- return true;
- }
- //printf("TryConnectChannel Local(%d).channel exist,but not ready\n", Local);
- 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);
- FERROR("open Local({$}) channel:{$} Failed.RetCode:{$}\n", Local, (const char*)getChannelRootpath(), (int)ret);
- }
- }
- else
- {
- FINFO("Dev {$} Is not in Usage State", (const char*)getRootpath());
- }
- ThisProc.Close();
- }
- else
- {
- FINFO("Open {$} Failed", (const char*)getRootpath());
- }
- }
- ClearChannelList(Local);
- //printf("TryConnectChannel Local(%d) Not Ready\n", Local);
- 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;
- vec.resize(3);
- vec[0] = m_ExitFlag;
- vec[1] = m_prootClient->GetNotifyHandle();
- vec[2] = m_pLocalChannelClient->GetNotifyHandle();
- //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)
- //{
- // 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)
- // {
- // FINFO("EthChannelClient is Closed.try connect eth channel");
- // TryConnectChannel(false);
- // }
- //}
- }
- else if (wait == WAIT_OBJECT_0)
- {
- 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");
- // FINFO("Eth DisConnected.Enable Eth is TRUE: Try Connect\n");
- // if (m_pBusEth->Connect(m_EthConnection))
- // {
- // printf("Eth Connection: Succeed\n");
- // FINFO("Eth Connection: Succeed\n");
- // TryConnectChannel(false);
- // }
- // else
- // {
- // printf("Eth Connection: Failed\n");
- // FINFO("Eth Connection: Failed\n");
- // }
- // }
- //}
- //if (m_pBusLocal)
- //{
- // //check local
- // if (m_pBusLocal->IsConnected() == false)
- // {
- // printf("Local Connection: Try Connect\n");
- // FERROR("Local Connection: Try Connect\n");
- // if (m_pBusLocal->Connect(m_LocalConnection))
- // {
- // printf("Local Connection: Succeed\n");
- // FINFO("Local Connection: Succeed\n");
- // TryConnectChannel(true);
- // }
- // else
- // {
- // printf("Local Connection: Failed\n");
- // 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())
- {
- 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");
- 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());
- FINFO("Thread:{$},WTF Happen?? Failed\n", GetCurrentThreadId());
- return false;
- }
- }
- }
- catch (...)
- {
- //printf("Thread:%d,Exeption in DrvTree Thread\n", GetCurrentThreadId());
- 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");
- // 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");
- // FINFO("Adjust Eth Connection: Try Connect\n");
- // if (m_pBusEth->Connect(m_EthConnection))
- // {
- // printf("Eth Connection: Succeed\n");
- // FINFO("Eth Connection: Succeed\n");
- // TryConnectChannel(false);
- // }
- // else
- // {
- // printf("Eth Connection: Failed\n");
- // FINFO("Eth Connection: Failed\n");
- // }
- // }
- //}
- }
- //good
- bool DevBusManager::OnStartThread()
- {
- //printf("========== DevBusManager::OnStartThread 0X%08X \n", GetCurrentThreadId());
- 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();
- string strLogPath = GetProcessDirectory() + R"(/Conf/log_config.xml)";
- string LogHost = (string)getRootpath();
- std::string moduleName = "eBus";
- bool logret = initLogModule(
- LogHost, // 主机名(用于日志路径中的{host}占位符)
- moduleName, // 唯一模块名
- strLogPath, // 配置文件路径
- true // 是否输出到控制台(可选)
- );
- if (!logret) {
- std::cerr << "Log init failed!" << std::endl;
- }
- // 绑定当前动态库的模块名(调用自身实现的接口)
- DeviceBusSetLocalModuleName(moduleName);
- //share memory settting
- std::cout << "-*-*-* LoadBusDrv Start" << CurrentDateTime() << "" << endl;
- 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)
- // {
- // 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;
- 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)
- //{
- // 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;
- // }
- // FINFO("Connect Local Ebus Over.");
- //}
- //if (m_pBusEth)
- //{
- // 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;
- // }
- // 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)
- {
- FINFO("Disconnect Local EBUS");
- //if (m_pBusLocal->IsConnected())
- {
- m_pLocalChannelClient->Close();
- }
- //m_pBusLocal->DisConnect();
- //m_pBusLocal->Quit();
- //FINFO("Stop Local Ebus Thread");
- //if (m_pBusLocalThread->StopThread(100) == false)
- //{
- // FINFO("Local Receive Bus Thread:StopThread Used Terminate!!!!!!!!!!!!!!!");
- //}
- //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)
- {
- 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();
- //FINFO("Stop Eth Ebus Thread");
- //if (m_pBusEthThread->StopThread(100) == false)
- //{
- // FINFO("Eth Receive Bus Thread:StopThread Used Terminate!!!!!!!!!!!!!!!");
- //}
- //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)
- {
- 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();
- 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)
- // {
- // FINFO("Select Local Bus");
- // pBus = m_pBusLocal;
- // }
- //}
- //else
- //{
- // if (m_pBusEth)
- // {
- // FINFO("Select Eth Bus");
- // pBus = m_pBusEth;
- // }
- //}
- //if (pBus)
- //{
- // return pBus->SendPacket(TargetbusId.c_str(), packet.encode(), pBlockData, Size);
- //}
- //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);
- }
|