12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751 |
- // BusUnitLogic.cpp : 定义 DLL 应用程序的导出函数。
- //
- #include <time.h>
- #include "common_api.h"
- #include "LocalConfig.h"
- #include "LogicDriver.h"
- #include "BusUnitLogic.h"
- #include "PacketAnalizer.h"
- #include "LogicClient.h"
- #define BusUnitGuidStr "{181F92D6-A546-4ACF-8EF1-2CCAA9EA974E}"
- // 这是导出变量的一个示例
- BUSUNITLOGIC_API int nBusUnitLogic=0;
- // 这是导出函数的一个示例。
- BUSUNITLOGIC_API int fnBusUnitLogic(void)
- {
- return 42;
- }
- /// <summary>
- /// 创建带Console显示的进程
- /// </summary>
- /// <param name="pfullFilePath">进程启动命令行</param>
- /// <param name="pfullWorkPath">工作路径</param>
- /// <param name="pinfo">返回进程Info</param>
- /// <param name="ShowWindow">是否显示Console</param>
- /// <returns></returns>
- BOOL CreateTheProcess_Console_Config(const TCHAR *pfullFilePath, const TCHAR *pfullWorkPath, PROCESS_INFORMATION &pinfo, bool ShowWindow)
- {
- string fullFilePath = pfullFilePath;
- size_t index = fullFilePath.rfind('\\');
- char szCommand[MAX_PATH];
- memset(szCommand, 0, MAX_PATH);
- strcpy_s(szCommand, MAX_PATH, pfullFilePath);
- STARTUPINFO si;
- ZeroMemory(&si, sizeof(si));
- si.cb = sizeof(si);
- ZeroMemory(&pinfo, sizeof(pinfo));
- if (ShowWindow == false)
- {
- si.dwFlags = STARTF_USESHOWWINDOW;
- si.wShowWindow = SW_HIDE;
- }
- if (!CreateProcess(NULL, // No module name (use command line)
- szCommand, // Command line
- NULL, // Process handle not inheritable
- NULL, // Thread handle not inheritable
- FALSE, // Set handle inheritance to FALSE
- CREATE_NEW_CONSOLE, // No creation flags
- NULL, // Use parent's environment block
- //NULL,
- pfullWorkPath, // Use parent's starting directory
- &si, // Pointer to STARTUPINFO structure
- &pinfo) // Pointer to PROCESS_INFORMATION structure
- )
- {
- printf("CreateProcess failed (%d).\n", GetLastError());
- printf("Command path :%s\n", szCommand);
- return FALSE;
- }
- return TRUE;
- }
- // 这是已导出类的构造函数。
- // 有关类定义的信息,请参阅 BusUnitLogic.h
- BusUnitLogic::BusUnitLogic()
- {
- m_DevList = new DeviceDescriptList();
- m_pbusID = new BaseJsonDataObject<string>();
- m_pbusID->SetKey("BusId");
- (*m_pbusID) = (const char*)getLocalEbusId();
- m_pMachineID = new BaseJsonDataObject<string>();
- m_pMachineID->SetKey("MachineId");
- (*m_pMachineID) = (const char*)getLocalMachineId();
- m_pProcID = new BaseJsonDataObject<UINT64>();
- m_pProcID->SetKey("ProcId");
- (*m_pProcID) = (UINT64)GetCurrentProcessId();
-
- m_pState = new BaseJsonDataObject<int>();
- m_pState->SetKey("Status");
- (*m_pState) = 0;
- m_pExitFlag = new BaseJsonDataObject<int>();
- m_pExitFlag->SetKey("ExitFlag");
- (*m_pExitFlag) = 0;
-
- m_pEnableEthBus = new BaseJsonDataObject<int>();
- m_pEnableEthBus->SetKey("EnableEthBus");
- (*m_pEnableEthBus) = 0;//default is disconnect
-
- m_pEthBusRouterIp = new BaseJsonDataObject<string>();
- m_pEthBusRouterIp->SetKey("EthBusRouterIp");
- (*m_pEthBusRouterIp) = (const char*)"";
- m_pForTest = new BaseJsonDataObject<bool>();
- m_pForTest->SetKey("ForTestFlag");
- (*m_pForTest) = 0;//default is 0
- m_pFullDriverList = new DriverDescriptList();
- m_pFullDriverList->SetKey("FullDriverList");
- m_pConfigDriverList = new DriverDescriptList();
- m_pConfigDriverList->SetKey("ConfigDriverList");
- m_pProcessInfo = new map<string, vector<TargetDriverInfo>>();
- m_bConfigRemoveDriver = false;
- return;
- }
- BusUnitLogic::~BusUnitLogic()
- {
- delete m_DevList;
- delete m_pbusID;
- delete m_pMachineID;
- delete m_pProcID;
- delete m_pState;
- delete m_pExitFlag;
- delete m_pEnableEthBus;
- delete m_pEthBusRouterIp;
- delete m_pForTest;
- delete m_pFullDriverList;
- delete m_pConfigDriverList;
- delete m_pProcessInfo;
- }
- /// <summary>
- /// 获取Bus单元逻辑设备的GUID类型
- /// </summary>
- /// <param name="DevType">返回值</param>
- /// <returns></returns>
- bool SYSTEM_CALL BusUnitLogic::GetDeviceType(GUID &DevType)
- {
- return string_2_guid(BusUnitGuidStr, DevType);
- }
- /// <summary>
- /// 测试数据更新验证,如果值相同,则生成通知
- /// </summary>
- /// <param name="Flag">判定条件值</param>
- /// <returns></returns>
- int DATA_ACTION BusUnitLogic::ForTest(bool Flag)
- {
- RET_STATUS ret = RET_SUCCEED;
- if ((*m_pForTest) != Flag)
- {
- (*m_pForTest) = Flag;
- ResDataObject NotifyData;
- PacketAnalizer::MakeNotify(NotifyData, PACKET_CMD_UPDATE, m_pForTest->GetKey(), m_pForTest->GetVal());
- ret = CmdFromLogicDev(&NotifyData);
- }
- return ret;
- }
- //get device resource
- /// <summary>
- /// 返回Bus单元逻辑设备的资源描述,这里新增了订阅Action
- /// </summary>
- /// <param name="pDeviceResource">返回值</param>
- /// <returns></returns>
- RET_STATUS SYSTEM_CALL BusUnitLogic::GetDeviceResource(ResDataObject PARAM_OUT *pDeviceResource)
- {
- bool ret = true;
- //make device type
- ret &= pDeviceResource->add("ClientType", DPC_UnitClient);
- ret &= pDeviceResource->add("DeviceType", BusUnitGuidStr);
- //make attributes
- ResDataObject AttrVal;
- AttrVal.add(m_pMachineID->GetKey(), m_pMachineID->GetVal());
- AttrVal.add(m_pbusID->GetKey(), m_pbusID->GetVal());
- AttrVal.add(m_pProcID->GetKey(), m_pProcID->GetVal());
- AttrVal.add(m_pState->GetKey(), m_pState->GetVal());
- AttrVal.add(m_pExitFlag->GetKey(), m_pExitFlag->GetVal());
-
- AttrVal.add(m_pEnableEthBus->GetKey(), m_pEnableEthBus->GetVal());
- AttrVal.add(m_pEthBusRouterIp->GetKey(), m_pEthBusRouterIp->GetVal());
- AttrVal.add(m_pForTest->GetKey(), m_pForTest->GetVal());
-
- ResDataObject arraylist;
- m_DevList->GetResDataObject(arraylist);
- AttrVal.add(m_DevList->GetKey(), arraylist);
- //full driver list
- arraylist.clear();
- m_pFullDriverList->GetResDataObject(arraylist);
- AttrVal.add(m_pFullDriverList->GetKey(), arraylist);
- //config driver list
- arraylist.clear();
- m_pConfigDriverList->GetResDataObject(arraylist);
- AttrVal.add(m_pConfigDriverList->GetKey(), arraylist);
- ret &= pDeviceResource->add("Attribute", AttrVal);
- //Actions
- ResDataObject ActionVal;
- ret &= ActionVal.add("AddDeviceDescrpt", "");
- ret &= ActionVal.add("DelDeviceDescrpt", "");
- ret &= ActionVal.add("ExitDriverProc", "");
- ret &= ActionVal.add("SetDeviceStatus", "");
- ret &= ActionVal.add("SetEthBusSwitch", "");
- ret &= ActionVal.add("SetEthBusRouterIp", "");
- ret &= ActionVal.add("ForTest", "");
- ret &= ActionVal.add("ConfigLoadDriver", "");
- ret &= ActionVal.add("ConfigRemoveDriver", "");
- ret &= ActionVal.add("SubscribeTopic", ""); //DPC 订阅事件
- ret &= ActionVal.add("Unsubscribe", ""); //取消订阅
- if (ret)
- {
- pDeviceResource->add("Action", ActionVal);
- return RET_SUCCEED;
- }
- return RET_FAILED;
- }
- //ResourceCommand Request In and Response Out
- RET_STATUS SYSTEM_CALL BusUnitLogic::Request(ResDataObject PARAM_IN *pRequest, ResDataObject PARAM_OUT *pResponse)
- {
- ResDataObject res;
- RET_STATUS ret = RET_NOSUPPORT;
- //1. analize request
- PACKET_CMD cmd = PacketAnalizer::GetPacketCmd(pRequest);
- //cmd:get,set,add,del,exe,
- //ignore open/close
- //Action和Attr应该做成一个MAP,经过MAP访问这些资源,不应该是switch case
- //为了快速做一版,先保留switch case!!!
- string keystr = PacketAnalizer::GetPacketKey(pRequest);
- ACTION_SYNC_MODE syncmode = PacketAnalizer::GetPacketSyncMode(pRequest);
- switch (cmd){
- case PACKET_CMD_EXE:
- {
- if (keystr == "AddDeviceDescrpt")
- {
- bool result = true;
- ResDataObject obj1,obj2,obj3,obj4,obj5,obj6;
- result &= PacketAnalizer::GetParam(pRequest, 0, obj1);
- result &= PacketAnalizer::GetParam(pRequest, 1, obj2);
- result &= PacketAnalizer::GetParam(pRequest, 2, obj3);
- result &= PacketAnalizer::GetParam(pRequest, 3, obj4);
- result &= PacketAnalizer::GetParam(pRequest, 4, obj5);
- result &= PacketAnalizer::GetParam(pRequest, 5, obj6);
- if (result)
- {
- //2. call api
- ret = (RET_STATUS)AddDeviceDescrpt((const char *)obj1, (const char *)obj2, (const char *)obj3, (UINT64)obj4, (UINT64)obj5, (bool)obj6);
- }
- else
- {
- //put log here
- }
- }
- else if (keystr == "ConfigLoadDriver")
- {
- //1. 获取参数
- ResDataObject obj;
- if (PacketAnalizer::GetParam(pRequest, 0, obj))
- {
- //2. call api
- char szFixDrv[MAX_PATH];
- memset(szFixDrv, 0, MAX_PATH);
- ret = (RET_STATUS)ConfigLoadDriver((const char *)obj, szFixDrv,MAX_PATH);
- //3. 打包
- res.add("P0", szFixDrv);
- pResponse->update("CONTEXT", res);
- }
- }
- else if (keystr == "ConfigRemoveDriver")
- {
- //1. 获取参数
- ResDataObject obj;
- if (PacketAnalizer::GetParam(pRequest, 0, obj))
- {
- //2. call api
- ret = (RET_STATUS)ConfigRemoveDriver((const char *)obj);
- //3. 打包
- }
- }
- else if (keystr == "DelDeviceDescrpt")
- {
- //1. 获取参数
- ResDataObject obj;
- if (PacketAnalizer::GetParam(pRequest, 0, obj))
- {
- //2. call api
- ret = (RET_STATUS)DelDeviceDescrpt((const char *)obj);
- //3. 打包
- }
- }
- else if (keystr == "ExitDriverProc")
- {
- ret = (RET_STATUS)ExitDriverProc();
- }
- else if (keystr == "SetDeviceStatus")
- {
- //1. 获取参数
- ResDataObject obj;
- if (PacketAnalizer::GetParam(pRequest, 0, obj))
- {
- //2. call api
- ret = (RET_STATUS)SetDeviceStatus((int)obj);
- //3. 打包
- }
- }
- else if (keystr == "SetEthBusSwitch")
- {
- //1. 获取参数
- ResDataObject obj;
- if (PacketAnalizer::GetParam(pRequest, 0, obj))
- {
- //2. call api
- ret = (RET_STATUS)SetEthBusSwitch((int)obj);
- //3. 打包
- }
- }
- else if (keystr == "SetEthBusRouterIp")
- {
- //1. 获取参数
- ResDataObject obj;
- if (PacketAnalizer::GetParam(pRequest, 0, obj))
- {
- //2. call api
- ret = (RET_STATUS)SetEthBusRouterIp((const char*)obj);
- //3. 打包
- }
- }
- else if (keystr == "Get")
- {
- //1. 获取参数
- ResDataObject Res;
- ResDataObject obj;
- if (PacketAnalizer::GetParam(pRequest, 0, obj))
- {
- //2. call api
- ret = (RET_STATUS)Get((const char*)obj,Res);
- //3. 打包
- if (ret == RET_SUCCEED)
- {
- PacketAnalizer::UpdatePacketContext((*pResponse), Res);
- }
- }
- }
- else if (keystr == "ForTest")
- {
- ResDataObject Res;
- ResDataObject obj;
- if (PacketAnalizer::GetParam(pRequest, 0, obj))
- {
- //2. call api
- ret = (RET_STATUS)ForTest((bool)obj);
- }
- }
- else if (keystr == "SubscribeTopic") {
- // 收到 应用或者其他模块的 订阅消息
- // 往MQTT broker 里订阅
- }
- }
- break;
- default:
- //对当前设备来讲,其他都是浮云
- break;
- }
- //return status
- PacketAnalizer::MakeRetCode(ret, pResponse);
- return ret;
- }
- //notify to lower layer
- RET_STATUS SYSTEM_CALL BusUnitLogic::CmdToLogicDev(ResDataObject PARAM_IN *pCmd)
- {
- assert(0);//not happening
- return RET_FAILED;
- }
- //errors,warnings
- void BusUnitLogic::SetErrorInfo(int errCode, char *pErrInfo)
- {
- }
- void BusUnitLogic::SetWarningInfo(int warningCode, char *pWarningInfo)
- {
- }
- bool SYSTEM_CALL BusUnitLogic::CheckBusIdsExistance(const char *pszBusId)
- {
- //ResDataObject ConfigDrvList;
- //GetDriverConfigFiles(ConfigDrvList);
- //map<string, string> HitCheck;
- ////check static first
- //for (size_t i = 0; i < ConfigDrvList.size(); i++)
- //{
- // ResDataObject BusId;
- // ResDataObject DriverBusId;
- // if (GetDriverEbusId(ConfigDrvList.GetKey(i), BusId, DriverBusId))
- // {
- // map<string, string>::iterator iter = HitCheck.find((const char*)BusId);
- // if (iter != HitCheck.end())
- // {
- // //exist one
- // TPRINTA_WARN("Same BusId:%s on both Driver\nDrvA:%s\nDrvB:%s", (const char*)BusId, ConfigDrvList.GetKey(i), iter->second.c_str());
- // }
- // HitCheck[(const char*)BusId] = ConfigDrvList.GetKey(i);
- // if (pszBusId)
- // {
- // iter = HitCheck.find(pszBusId);
- // if (iter != HitCheck.end())
- // {
- // //exist one
- // TPRINTA_WARN("Same BusId:%s on Driver\nDrv:%s\nRequest BusId:%s", (const char*)BusId, ConfigDrvList.GetKey(i), pszBusId);
- // }
- // }
- // }
- //}
- if (pszBusId)
- {
- //check live second
- map<string, vector<TargetDriverInfo>>::iterator iter = m_pProcessInfo->begin();
- while (iter != m_pProcessInfo->end())
- {
- if (iter->first == pszBusId)
- {
- TPRINTA_WARN("Same BusId:%s Already on the run...", pszBusId);
- return false;
- }
- ++iter;
- }
- }
- return true;
- }
- bool SYSTEM_CALL BusUnitLogic::LoadAllConfigDrivers()
- {
- bool ret = true;
- //get list of fulldrvlist
- ResDataObject FullDrvList;
- GetFullDriverConfigFiles(FullDrvList);
- //get list of configdrvlist
- ResDataObject ConfigDrvList;
- GetDriverConfigFiles(ConfigDrvList);
- if (CheckBusIdsExistance(0) == false)
- {
- return false;
- }
- //add second
- for (size_t i = 0; i < FullDrvList.size(); i++)
- {
- ResDataObject BusId;
- ResDataObject DriverBusId;
- //get ebus driver path
- if (GetDriverEbusId(FullDrvList.GetKey(i), BusId, DriverBusId))
- {
- ret &= m_pFullDriverList->AddDriver(DriverBusId);
- if (ret == false)
- {
- GPRINTA_ERROR("AddDriver of FullDrv:%s Failed.", FullDrvList.GetKey(i));
- }
- }
- else
- {
- GPRINTA_ERROR("GetDriverEbusId configfile:%s Failed.", FullDrvList.GetKey(i));
- }
- }
- for (size_t i = 0; i < ConfigDrvList.size(); i++)
- {
- ResDataObject BusId;
- ResDataObject DriverBusId;
- if (GetDriverEbusId(ConfigDrvList.GetKey(i), BusId, DriverBusId))
- {
- ret &= m_pConfigDriverList->AddDriver(DriverBusId);
- if (ret == false)
- {
- GPRINTA_ERROR("AddDriver of ConfigDrv:%s Failed.", ConfigDrvList.GetKey(i));
- }
- }
- else
- {
- GPRINTA_ERROR("GetDriverEbusId configfile:%s Failed.", ConfigDrvList.GetKey(i));
- }
- }
- return ret;
- }
- string GetCurTime64ToString()
- {
- __time64_t now;
- struct tm t1;
- _time64(&now);//UTC
- _localtime64_s(&t1, &now);//LOCAL
- string ret;
- ret = FormatstdString("%04d%02d%02d%02d%02d%02d", t1.tm_year + 1900, t1.tm_mon + 1, t1.tm_mday, t1.tm_hour, t1.tm_min, t1.tm_sec);
- return ret;
- }
- int DATA_ACTION BusUnitLogic::MakeDriverNotify(const char *pszDriverpath, bool Add)
- {
- RET_STATUS ret = RET_SUCCEED;
- //exiting...
- if ((*m_pExitFlag) != 0)
- {
- return RET_INVALID;
- }
- ResDataObject NotifyData;
- ResDataObject theContext;
- theContext.add(pszDriverpath, "");
- if (Add)
- {
- if (m_pConfigDriverList->AddDriver(pszDriverpath))
- {
- printf("AddDriver %s ok\n", pszDriverpath);
- TPRINTA_DEBUG("AddDriver %s ok\n", pszDriverpath);
- PacketAnalizer::MakeNotify(NotifyData, PACKET_CMD_ADD, m_pConfigDriverList->GetKey(), theContext);
- CmdFromLogicDev(&NotifyData);
- }
- else
- {
- printf("AddDriver %s failed\n", pszDriverpath);
- TPRINTA_ERROR("AddDriver %s failed\n", pszDriverpath);
- ret = RET_FAILED;
- }
- }
- else
- {
- if (m_pConfigDriverList->DelDriver(pszDriverpath))
- {
- printf("DelDriver %s ok\n", pszDriverpath);
- TPRINTA_DEBUG("DelDriver %s ok\n", pszDriverpath);
- PacketAnalizer::MakeNotify(NotifyData, PACKET_CMD_DEL, m_pConfigDriverList->GetKey(), theContext);
- CmdFromLogicDev(&NotifyData);
- }
- else
- {
- printf("DelDriver %s failed\n", pszDriverpath);
- TPRINTA_ERROR("DelDriver %s failed\n", pszDriverpath);
- ret = RET_FAILED;
- }
- }
- return ret;
- }
- int DATA_ACTION BusUnitLogic::ConfigLoadDriver(const char *pszDriverpath, char *pszFixDriverpath, DWORD FixDrvLen)
- {
- int ret = RET_SUCCEED;
- bool X64Driver = true;
- ResDataObject fileList;
- //find driver from fulldrvlist
- //for test
- TPRINTA_DEBUG("Enter ConfigLoadDriver:%s\n", pszDriverpath);
- LogicDriver* pDPC = (LogicDriver*)GetDrvDPC();
- if (pDPC == 0)
- {
- TPRINTA_DEBUG("GetDrvDPC failed");
- return RET_FAILED;
- }
- m_bConfigRemoveDriver = true;
- pDPC->Thread_Lock(2000);
- TPRINTA_INFO("DPC lock ok");
- //find x64
- if (GetSpecificDriverConfigFiles(fileList, true))
- {
- //check x64FLAG
- //assert(0);//NOT FINISHED YET
- //exist x64
- for (size_t i = 0; i < fileList.size(); i++)
- {
- ResDataObject BusId;
- ResDataObject DriverBusId;
- //get ebus driver path
- if (GetDriverEbusId(fileList.GetKey(i), BusId, DriverBusId))
- {
- //for test
- TPRINTA_DEBUG("file[%zd] File:%s\n Buspath:%s\n",i, fileList.GetKey(i), (const char *)DriverBusId);
- if (DriverBusId == pszDriverpath)
- {
- string FullFilePath = fileList.GetKey(i);
- //copy configuration
- //make new title
- string TargetFileTitle = GetFileTitle(FullFilePath);
- string TimePos = GetCurTime64ToString();
- string NewTargetFileTitle = FormatstdString("\\%s_%s.xml", TargetFileTitle.c_str(), TimePos.c_str());
- //make full filepath
- //ResDataObject DriverConfigPath = GetDriverConfigPath();
- string DriverConfigPath = GetFileDirectory(FullFilePath);//...\FullConfig
- DriverConfigPath = GetFileDirectory(DriverConfigPath);//...procDir
- DriverConfigPath += "\\DriverConfig";
- string FullNewTargetFilePath = DriverConfigPath.c_str();
- FullNewTargetFilePath += NewTargetFileTitle;
- //for test
- TPRINTA_DEBUG("Got Match:%s\nCopy: Org:%s\nDes:%s\n", FullFilePath.c_str(), fileList.GetKey(i), FullNewTargetFilePath.c_str());
- if (CopyFile(fileList.GetKey(i), FullNewTargetFilePath.c_str(), TRUE))
- {
- TargetDriverInfo DrvInfo;
- if (GetDriverEbusId(FullNewTargetFilePath.c_str(), DrvInfo.m_RootBusId, DrvInfo.m_DriverBusId, true) == false)
- {
- return RET_FAILED;
- }
- //got match
- if (CheckBusIdsExistance(DrvInfo.m_RootBusId) == false)
- {
- //exist bus id
- UnloadDriver(DrvInfo.m_RootBusId);
- }
- //reload
- ret = LoadAllConfigDriver(true);
- Sleep(6000);//must sleep 6s
- if (ret >= RET_SUCCEED)
- {
- strcpy_s(pszFixDriverpath, FixDrvLen, (const char *)DrvInfo.m_DriverBusId);
- }
-
- TPRINTA_DEBUG("LoadAllConfigDriver return:%d\n", ret);
- m_bConfigRemoveDriver = false;
- pDPC->Thread_UnLock();
- TPRINTA_INFO("DPC Unlock ok");
- return ret;
- }
- else
- {
- TPRINTA_ERROR("CopyFile failed\n");
- }
- }
- }
- else
- {
- TPRINTA_ERROR("GetDriverEbusId failed\n");
- }
- }
- }
- else
- {
- TPRINTA_ERROR("GetSpecificDriverConfigFiles failed\n");
- }
- pDPC->Thread_UnLock();
- TPRINTA_INFO("DPC Unlock ok");
- m_bConfigRemoveDriver = false;
- return RET_SUCCEED;
- }
- bool SYSTEM_CALL BusUnitLogic::CheckAndKillLiveDriver(const char *pszDriverpath)
- {
- printf("Try Del Driver:%s\n", pszDriverpath);
- TPRINTA_DEBUG("Try Del Driver:%s\n", pszDriverpath);
- map<string, vector<TargetDriverInfo>>::iterator iter = m_pProcessInfo->begin();
- while (iter != m_pProcessInfo->end())
- {
- for (size_t DriverIdx = 0; DriverIdx < iter->second.size(); DriverIdx++)
- {
- string DrvBusId = (const char*)(iter->second)[DriverIdx].m_DriverBusId;
- TPRINTA_DEBUG("Test Match:%s\n", DrvBusId.c_str());
- if (DrvBusId == pszDriverpath)
- {
- TPRINTA_DEBUG("Got Match:%s\n", DrvBusId.c_str());
- //got one
- if (WaitForSingleObject(((iter->second)[0].m_info.hProcess), 0) == WAIT_TIMEOUT)
- {
- //process exist
- TPRINTA_DEBUG("Target Process Exist");
- string strBusIdCopy = DrvBusId;
- LogicClient chClient1;
- chClient1.SetRouter(CCOS_PACKET_ROUTE_LOCAL);
- DrvBusId = "/";
- DrvBusId += iter->first.c_str();
- //TPRINTA_INFO("DrvBusId :%s\n", DrvBusId.c_str());
- TPRINTA_INFO("open :%s\n", strBusIdCopy.c_str());
- int nRet = -1;
- if (chClient1.Open(strBusIdCopy.c_str(), ALL_ACCESS, ONE_ACTION_TIMEOUT) >= RET_SUCCEED)
- {
- //if (strBusIdCopy.find("Detector"))//disconnect before Del
- //{
- // bool bret = false;
- // ACTION_SYNC_MODE syncmode = chClient1.GetRequestSyncMode();
- // TPRINTA_DEBUG("RequestSyncMode is %d\n",syncmode);
- // if (syncmode != ACTION_SYNC)
- // {
- // bret = chClient1.SetRequestSyncMode(ACTION_SYNC);
- // TPRINTA_DEBUG("SetRequestSyncMode to sync\n");
- // }
- // ResDataObject req, resp;
- // int nRet = chClient1.Action("DisConnect", req, resp, ONE_ACTION_TIMEOUT);
- // if (nRet >= RET_SUCCEED)
- // {
- // TPRINTA_DEBUG("Succeed Action: DisConnect\n");
- // Sleep(2000);//wait detector finish the disconnect.UI send cmd wait 3s
- // }
- // else
- // {
- // TPRINTA_DEBUG("Failed Action: DisConnect ret = %d\n", nRet);
- // }
- // if (bret)
- // {
- // chClient1.SetRequestSyncMode(syncmode);
- // TPRINTA_DEBUG("SetRequestSyncMode back\n");
- // }
- //}
- ResDataObject req;
- nRet = chClient1.Action_Req("ExitDriverProc", req, ONE_ACTION_TIMEOUT);
- if (nRet >= RET_SUCCEED)
- {
- printf("Succeed Action:ExitDriverProc %s ...\n", strBusIdCopy.c_str());
- TPRINTA_DEBUG("Succeed Action: ExitDriverProc %s ...\n", strBusIdCopy.c_str());
- chClient1.Close();
- if (WaitForSingleObject(((iter->second)[0].m_info.hProcess), ONE_ACTION_TIMEOUT) == WAIT_TIMEOUT)
- {
- TerminateProcess(((iter->second)[0].m_info.hProcess), 0);
- }
- TPRINTA_DEBUG("Try Delete Driver File:%s\n", (const char*)(iter->second)[DriverIdx].m_CcosProcInfo["DriverConfigPath"]);
- if (DeleteFile((const char*)(iter->second)[DriverIdx].m_CcosProcInfo["DriverConfigPath"]))
- {
- TPRINTA_DEBUG("Try Delete Driver File Succeed\n");
- iter->second.erase(iter->second.begin() + DriverIdx);
- //if (iter->second.size() == 0)
- {
- m_pProcessInfo->erase(iter);
- }
- return true;
- }
- else
- {
- DWORD nret = GetLastError();
- TPRINTA_DEBUG("Try Delete Driver File failed %d\n", nret);
- }
- }
- else
- {
- printf("Failed Action:ExitDriverProc -> %s...\n", strBusIdCopy.c_str());
- //TPRINTA_ERROR("Failed Action: ExitDriverProc -> %s ...\n", strBusIdCopy.c_str());
- TPRINTA_DEBUG("Failed Action: ExitDriverProc ret %d-> %s ...\n",nRet, strBusIdCopy.c_str());
- chClient1.Close();
- }
- //if (chClient1.Action(pszAction, req, res, Timeout) < RET_SUCCEED)
- //{
- // printf("Failed Action:%s ...\n",pszAction);
- // TPRINTA_ERROR("Failed Action: %s ...\n", pszAction);
- // chClient1.Close();
- //}
- //else
- //{
- // printf("Succeed Action:%s ...\n", pszAction);
- // TPRINTA_DEBUG("Succeed Action: %s ...\n", pszAction);
- // return true;
- //}
- //chClient1.Close();
- }
- else
- {
- printf("Open local %s Failed\n", DrvBusId.c_str());
- TPRINTA_DEBUG("Open local %s Failed ret %d\n", strBusIdCopy.c_str(),nRet);
- }
- }
- else
- {
- TPRINTA_DEBUG("Target Process Not Exist");
- TPRINTA_DEBUG("Try Delete Driver File:%s\n", (const char*)(iter->second)[DriverIdx].m_CcosProcInfo["DriverConfigPath"]);
- if (DeleteFile((const char*)(iter->second)[DriverIdx].m_CcosProcInfo["DriverConfigPath"]))
- {
- TPRINTA_DEBUG("Try Delete Driver File Succeed\n");
- iter->second.erase(iter->second.begin() + DriverIdx);
- //if (iter->second.size() == 0)
- {
- m_pProcessInfo->erase(iter);
- }
- return true;
- }
- }
- return false;
- }
- }
- ++iter;
- }
- return true;
- }
- int DATA_ACTION BusUnitLogic::ConfigRemoveDriver(const char *pszDriverpath)
- {
- int ret = RET_SUCCEED;
- TPRINTA_DEBUG("Enter ConfigRemoveDriver:%s\n", pszDriverpath);
- //find driver from configdrvlist
- if (m_pConfigDriverList->IsDriverExist(pszDriverpath) == false)
- {
- //not exist then return succeed.
- TPRINTA_DEBUG("Target Driver Not Exist %s",pszDriverpath);
- return RET_FAILED;
- }
- LogicDriver* pDPC = (LogicDriver*)GetDrvDPC();
- if (pDPC == 0)
- {
- TPRINTA_DEBUG("GetDrvDPC failed");
- return RET_FAILED;
- }
- m_bConfigRemoveDriver = true;
- pDPC->Thread_Lock(3000);
- TPRINTA_INFO("DPC lock ok");
- try{
- //check Live status
- if (CheckAndKillLiveDriver(pszDriverpath))
- {
- ret = MakeDriverNotify(pszDriverpath, false);
- //reload
- ret = LoadAllConfigDriver(true);
- Sleep(6000);//must sleep 6s
- TPRINTA_DEBUG("sleep over");
- }
- else
- {
- ret = RET_FAILED;
- }
- }
- catch (...)
- {
- ret = RET_FAILED;
- TPRINTA_DEBUG("crash");
- }
- pDPC->Thread_UnLock();
- TPRINTA_INFO("DPC Unlock ok");
- m_bConfigRemoveDriver = false;
- TPRINTA_DEBUG("ConfigRemoveDriver over");
- return ret;
- }
- //Data Access
- int DATA_ACTION BusUnitLogic::AddDeviceDescrpt(const char PARAM_IN *pDevPath, const char PARAM_IN *pTargetType, const char PARAM_IN *pMachineId, UINT64 ProcId, UINT64 Addr, bool forceAdd)
- {
- DeviceDescript dd;
-
- //exiting...
- if ((*m_pExitFlag) != 0)
- {
- return RET_INVALID;
- }
- dd.SetKey(pDevPath);
- dd.m_TargetType = pTargetType;
- dd.m_ProcId = ProcId;
- dd.m_Address = Addr;
- dd.m_MachineId = pMachineId;
- ResDataObject NotifyData;
- ResDataObject theContext;
- dd.GetResDataObject(theContext);
- if (m_DevList->AddDriver(dd))
- {
- PacketAnalizer::MakeNotify(NotifyData, PACKET_CMD_ADD, m_DevList->GetKey(), theContext);
- }
- else
- {
- PacketAnalizer::MakeNotify(NotifyData, PACKET_CMD_ONLINE, m_DevList->GetKey(), theContext);
- }
- CmdFromLogicDev(&NotifyData);
- return RET_SUCCEED;
- }
- int DATA_ACTION BusUnitLogic::Get(const char PARAM_IN *pKey,ResDataObject &Res)
- {
- string Key = pKey;
- if (Key == m_DevList->GetKey())
- {
- ResDataObject arraylist;
- m_DevList->GetResDataObject(arraylist);
- Res.add(m_DevList->GetKey(), arraylist);
- return RET_SUCCEED;
- }
- return RET_FAILED;
- }
- int DATA_ACTION BusUnitLogic::DelDeviceDescrpt(const char PARAM_IN *pDevPath)
- {
- if (m_DevList->DelVal(pDevPath))
- {
- //3. Notify
- ResDataObject NotifyData;
- ResDataObject theContext;
- theContext.add("DevicePath", pDevPath);
- PacketAnalizer::MakeNotify(NotifyData, PACKET_CMD_DEL, m_DevList->GetKey(), theContext);
- CmdFromLogicDev(&NotifyData);
- }
- return RET_SUCCEED;
- }
- int DATA_ACTION BusUnitLogic::SetDeviceStatus(int Status)
- {
- RET_STATUS ret = RET_SUCCEED;
- if ((*m_pState) != Status)
- {
- ResDataObject NotifyData;
- (*m_pState) = Status;
- PacketAnalizer::MakeNotify(NotifyData, PACKET_CMD_UPDATE, m_pState->GetKey(), m_pState->GetVal());
- ret = CmdFromLogicDev(&NotifyData);
- }
- return ret;
- }
- int DATA_ACTION BusUnitLogic::SetEthBusSwitch(int Switch)
- {
- RET_STATUS ret = RET_SUCCEED;
- if ((*m_pEnableEthBus) != Switch)
- {
- ResDataObject NotifyData;
- (*m_pEnableEthBus) = Switch;
- PacketAnalizer::MakeNotify(NotifyData, PACKET_CMD_UPDATE, m_pEnableEthBus->GetKey(), m_pEnableEthBus->GetVal());
- ret = CmdFromLogicDev(&NotifyData);
- }
- return ret;
- }
- int DATA_ACTION BusUnitLogic::SetEthBusRouterIp(const char PARAM_IN *pRouterIp)
- {
- RET_STATUS ret = RET_SUCCEED;
- std::string Param = pRouterIp;
- std::string RouterIp = (*m_pEthBusRouterIp);
- if (RouterIp != Param)
- {
- ResDataObject NotifyData;
- (*m_pEthBusRouterIp) = Param;
- PacketAnalizer::MakeNotify(NotifyData, PACKET_CMD_UPDATE, m_pEthBusRouterIp->GetKey(), m_pEthBusRouterIp->GetVal());
- ret = CmdFromLogicDev(&NotifyData);
- }
- return ret;
- }
- int DATA_ACTION BusUnitLogic::ExitDriverProc()
- {
- RET_STATUS ret = RET_SUCCEED;
- if ((*m_pExitFlag) == 0)
- {
- ResDataObject NotifyData;
- SetExitFlag(1);
- PacketAnalizer::MakeNotify(NotifyData, PACKET_CMD_UPDATE, m_pExitFlag->GetKey(), m_pExitFlag->GetVal());
- ret = CmdFromLogicDev(&NotifyData);
- }
- return ret;
- }
- int SYSTEM_CALL BusUnitLogic::GetExitFlag()
- {
- return (*m_pExitFlag);
- }
- int SYSTEM_CALL BusUnitLogic::SetExitFlag(int ExitFlag)
- {
- (*m_pExitFlag) = ExitFlag;
- return 1;
- }
- int SYSTEM_CALL BusUnitLogic::GetbusId(ResDataObject &obj)
- {
- obj = m_pbusID->GetVal();
- return RET_SUCCEED;
- }
- int SYSTEM_CALL BusUnitLogic::GetMachineId(ResDataObject &obj)
- {
- obj = m_pMachineID->GetVal();
- return RET_SUCCEED;
- }
- int SYSTEM_CALL BusUnitLogic::GetProcId(UINT64 &obj)
- {
- obj = (*m_pProcID);
- return RET_SUCCEED;
- }
- int SYSTEM_CALL BusUnitLogic::SetbusId(ResDataObject &obj)
- {
- (*m_pbusID) = (const char *)obj;
- return RET_SUCCEED;
- }
- int SYSTEM_CALL BusUnitLogic::SetMachineId(ResDataObject &obj)
- {
- (*m_pMachineID) = (const char *)obj;
- return RET_SUCCEED;
- }
- int SYSTEM_CALL BusUnitLogic::SetProcId(UINT64 obj)
- {
- (*m_pProcID) = (UINT64)obj;
- return RET_SUCCEED;
- }
- DWORD SYSTEM_CALL BusUnitLogic::GetDeviceCount()
- {
- return (DWORD)m_DevList->m_DeviceList.size();
- }
- bool SYSTEM_CALL BusUnitLogic::GetDeviceDescript(DWORD Idx, ResDataObject &DevPath, ResDataObject &DevType, ResDataObject &MachineId, UINT64 &ProcId, UINT64 &Addr)
- {
- if (m_DevList->m_DeviceList.size() > Idx)
- {
- DevPath = m_DevList->m_DeviceList[Idx].GetKey();
- DevType = m_DevList->m_DeviceList[Idx].m_TargetType.GetVal();
- MachineId = m_DevList->m_DeviceList[Idx].m_MachineId.GetVal();
- ProcId = m_DevList->m_DeviceList[Idx].m_ProcId;
- Addr = m_DevList->m_DeviceList[Idx].m_Address;
- return true;
- }
- return false;
- }
- int SYSTEM_CALL BusUnitLogic::GetDeviceStatus()
- {
- return (*m_pState);
- }
- int SYSTEM_CALL BusUnitLogic::GetEthBusSwitch()
- {
- return (*m_pEnableEthBus);
- }
- int SYSTEM_CALL BusUnitLogic::GetEthBusRouterIp(ResDataObject &obj)
- {
- obj = (*m_pEthBusRouterIp).GetVal();
- return 1;
- }
- /// <summary>
- /// 检查所有驱动进程,如果进程Crash了,需要根据配置重新拉起来
- /// </summary>
- /// <returns></returns>
- void SYSTEM_CALL BusUnitLogic::CheckAllLiveDriver()
- {
- map<string, vector<TargetDriverInfo>>::iterator iter = m_pProcessInfo->begin();
- while (iter != m_pProcessInfo->end())
- {
- if (m_bConfigRemoveDriver)
- {
- //Sleep(2000);
- TPRINTA_INFO("ConfigDriver working");
- return;
- }
- //for (size_t DriverIdx = 0; DriverIdx < iter->second.size(); DriverIdx++)
- {
- string DrvBusId = "/";
- DrvBusId += (const char*)iter->first.c_str();
- {
- //got one
- if (WaitForSingleObject(((iter->second)[0].m_info.hProcess), 0) == WAIT_TIMEOUT)
- {
- //process exist
- LogicClient chClient1;
- chClient1.SetRouter(CCOS_PACKET_ROUTE_LOCAL);
- if (chClient1.Open(DrvBusId.c_str(), ALL_ACCESS, ONE_ACTION_TIMEOUT) >= RET_SUCCEED)
- {
- chClient1.Close();
- }
- else
- {
- printf("Open local %s Failed while check Live\n", DrvBusId.c_str());
- TPRINTA_ERROR("Open local %s Failed while check Live\n", DrvBusId.c_str());
- }
- }
- else
- {
- if (m_bConfigRemoveDriver)
- {
- TPRINTA_INFO("ConfigDriver working 2");
- return;
- }
- //target process crashed
- printf("Autoload Flag:%d \n BusId:%s\n", (bool)(iter->second)[0].m_CcosProcInfo["AutoLoad"], iter->first.c_str());
- //do reload
- if ((bool)(iter->second)[0].m_CcosProcInfo["AutoLoad"])
- {
- memset(&(iter->second)[0].m_info, 0, sizeof(PROCESS_INFORMATION));
- if (CreateTheProcess_Console_Config((iter->second)[0].m_CcosProcInfo["DriverPath"],
- (iter->second)[0].m_CcosProcInfo["WorkPath"],
- (iter->second)[0].m_info,
- (iter->second)[0].m_CcosProcInfo["ShowWindow"]) == FALSE)
- {
- printf("Reload Failed.Can't Create Process for config:%s\n",
- (const char*)(iter->second)[0].m_CcosProcInfo["DriverConfigPath"]);
- TPRINTA_ERROR("Reload Failed.Can't Create Process for config:%s",
- (const char*)(iter->second)[0].m_CcosProcInfo["DriverConfigPath"]);
- }
- else
- {
- printf("Reload Succeed.Create Process for config:%s Succeed\n",
- (const char*)(iter->second)[0].m_CcosProcInfo["DriverConfigPath"]);
- TPRINTA_INFO("Reload Succeed.Create Process for config:%s Succeed",
- (const char*)(iter->second)[0].m_CcosProcInfo["DriverConfigPath"]);
- Sleep(6000);//wait
- }
- }
- else
- {
- //kick iter;
- printf("Kick crashed process. BusId:%s\n", iter->first.c_str());
- TPRINTA_INFO("Kick crashed process. BusId:%s\n", iter->first.c_str());
- DelDeviceDescrpt(DrvBusId.c_str());
- iter = m_pProcessInfo->erase(iter);
- continue;
- }
- }
- }
- }
- ++iter;
- }
- return;
- }
- int SYSTEM_CALL BusUnitLogic::LoadAllConfigDriver(bool ForReload)
- {
- RET_STATUS ret = RET_SUCCEED;
- //this is for dpc call
- bool FullDriverList = false;
- ResDataObject fileList;
- //find driver from fulldrvlist
- if (CheckBusIdsExistance(0) == false)
- {
- TPRINTA_ERROR("CheckBusIdsExistance(0) == false");
- return RET_FAILED;
- }
- LogicDriver* pDPC = (LogicDriver*)GetDrvDPC();
- if (pDPC == 0)
- {
- TPRINTA_ERROR("pDPC == 0");
- return RET_FAILED;
- }
- TPRINTA_INFO("GetDrvDPC ok");
- //pDPC->Thread_Lock();
- //TPRINTA_INFO("DPC lock ok");
- try{
- //find x64
- if (GetSpecificDriverConfigFiles(fileList, FullDriverList))
- {
- TPRINTA_INFO("GetSpecificDriverConfigFiles ok");
- map<string,int> ConflictBusIds;
- map<string, string> BusIdMap;//BusId <-> Dir
- map<string, vector<TargetDriverInfo>> LoadDriverMap;
- //exist x64
- for (size_t i = 0; i < fileList.size(); i++)
- {
- TargetDriverInfo DrvInfo;
- if (GetDriverEbusId(fileList.GetKey(i), DrvInfo.m_RootBusId, DrvInfo.m_DriverBusId))
- {
- //get ebus driver path
- if (GetDriverProcInfo(fileList.GetKey(i), DrvInfo.m_CcosProcInfo))
- {
- map<string,string>::iterator BusIdsIter = BusIdMap.find((const char*)DrvInfo.m_RootBusId);
- if (BusIdsIter != BusIdMap.end())
- {
- //same busId Exist
- if (BusIdsIter->second == (const char*)fileList[i])
- {
- //same dir,ok
- }
- else
- {
- //different dir,ng
- ConflictBusIds[BusIdsIter->first] = 1;
- }
- }
- else
- {
- //no BusId exist
- BusIdMap[(const char*)DrvInfo.m_RootBusId] = (const char*)fileList[i];
- }
- LoadDriverMap[(const char *)DrvInfo.m_RootBusId].push_back(DrvInfo);
- }
- else
- {
- TPRINTA_INFO("GetDriverProcInfo failed %s ", fileList.GetKey(i));
- }
- }
- else
- {
- TPRINTA_INFO("GetDriverEbusId failed %s ", fileList.GetKey(i));
- }
- }
- map<string, vector<TargetDriverInfo>>::iterator DelIter;
- //kick same busids drivers
- map<string, int>::iterator ConflictIter = ConflictBusIds.begin();
- while (ConflictIter != ConflictBusIds.end())
- {
- DelIter = LoadDriverMap.find(ConflictIter->first);
- if (DelIter != LoadDriverMap.end())
- {
- TPRINTA_ERROR("busid:%s conflict", DelIter->first.c_str());
- for (size_t DriverIdx = 0; DriverIdx < DelIter->second.size(); DriverIdx++)
- {
- const char *pszConfigFilepath = DelIter->second[DriverIdx].m_CcosProcInfo["DriverConfigPath"];//config file path
- TPRINTA_ERROR("busid:%s conflict.Ignore ConfigFile:%s", DelIter->first.c_str(),pszConfigFilepath);
- }
- LoadDriverMap.erase(DelIter);
- }
- ++ConflictIter;
- }
- ConflictBusIds.clear();
- TPRINTA_INFO("LoadDriverMap size %d ", LoadDriverMap.size());
- //kick condition like (same busid but not same procType)
- DelIter = LoadDriverMap.begin();
- while (DelIter != LoadDriverMap.end())
- {
- if (DelIter->second.size() > 1)
- {
- bool ConflictProc = false;
- //more than one driver exist
- for (size_t DriverIdx = 0; DriverIdx < DelIter->second.size() - 1; DriverIdx++)
- {
- if (ConflictProc)
- {
- break;
- }
- //compare each others driverproc
- string DriverProc = "";
- ResDataObject DriverProcessPath;
- GetDriverProcessPath(DelIter->second[DriverIdx].m_CcosProcInfo["DriverConfigPath"], DriverProcessPath);
- DriverProc = (const char*)DriverProcessPath;
- for (size_t CompareIdx = DriverIdx + 1; CompareIdx < DelIter->second.size(); CompareIdx++)
- {
- if (ConflictProc)
- {
- break;
- }
- string CompareProc = "";
- ResDataObject CompareProcessPath;
- GetDriverProcessPath(DelIter->second[CompareIdx].m_CcosProcInfo["DriverConfigPath"], CompareProcessPath);
- CompareProc = (const char*)CompareProcessPath;
- if (CompareProc != DriverProc)
- {
- ConflictBusIds[DelIter->first] = 1;
- ConflictProc = true;
- }
- }
- }
- }
- ++DelIter;
- }
- //kick again
- ConflictIter = ConflictBusIds.begin();
- while (ConflictIter != ConflictBusIds.end())
- {
- DelIter = LoadDriverMap.find(ConflictIter->first);
- if (DelIter != LoadDriverMap.end())
- {
- TPRINTA_ERROR("busid:%s processType conflict", DelIter->first.c_str());
- for (size_t DriverIdx = 0; DriverIdx < DelIter->second.size(); DriverIdx++)
- {
- const char* pszConfigFilepath = DelIter->second[DriverIdx].m_CcosProcInfo["DriverConfigPath"];//config file path
- TPRINTA_ERROR("busid:%s processType conflict.Ignore ConfigFile:%s", DelIter->first.c_str(), pszConfigFilepath);
- }
- LoadDriverMap.erase(DelIter);
- }
- ++ConflictIter;
- }
- map<string, vector<TargetDriverInfo>>::iterator iter = LoadDriverMap.begin();
- while (iter != LoadDriverMap.end())
- {
- if (ForReload)
- {
- if (CheckBusIdsExistance(iter->first.c_str()) == false)
- {
- //target exist
- ++iter;
- continue;
- }
- }
- //make full process path
- bool FirstIn = true;
- string FullDriverPath = "";
- for (size_t DriverIdx = 0; DriverIdx < iter->second.size(); DriverIdx++)
- {
- //make driverpath
- if (FirstIn)
- {
- ResDataObject DriverProcessPath;
- GetDriverProcessPath(iter->second[DriverIdx].m_CcosProcInfo["DriverConfigPath"], DriverProcessPath);
- FullDriverPath = (const char *)DriverProcessPath;
- FirstIn = false;
- }
- string ConfigFile = (const char *)iter->second[DriverIdx].m_CcosProcInfo["DriverConfigPath"];
- //get config file title
- string ConfigFileName = GetFileName(ConfigFile);
-
- FullDriverPath += " " + ConfigFileName;
- }
- //rest all of same busid
- for (size_t DriverIdx = 0; DriverIdx < iter->second.size(); DriverIdx++)
- {
- iter->second[DriverIdx].m_CcosProcInfo["DriverPath"] = FullDriverPath.c_str();//[fullpath of exe] [config1.xml config2.xml ...]
- }
- //create process
- if (CreateTheProcess_Console_Config(iter->second[0].m_CcosProcInfo["DriverPath"],
- iter->second[0].m_CcosProcInfo["WorkPath"],
- iter->second[0].m_info,
- iter->second[0].m_CcosProcInfo["ShowWindow"]) == FALSE)
- {
- TPRINTA_ERROR("Can't Create Process:%s", FullDriverPath.c_str());
- }
- else
- {
- TPRINTA_INFO("Create Process:%s ok", FullDriverPath.c_str());
- for (size_t DriverIdx = 0; DriverIdx < iter->second.size(); DriverIdx++)
- {
- (*m_pProcessInfo)[(const char*)iter->second[DriverIdx].m_RootBusId].push_back(iter->second[DriverIdx]);
- MakeDriverNotify((const char *)iter->second[DriverIdx].m_DriverBusId, true);
- }
- }
- ++iter;
- }
- }
- else
- {
- TPRINTA_ERROR("GetSpecificDriverConfigFiles failed");
- }
- }
- catch (...)
- {
- TPRINTA_ERROR("crash");
- ret = RET_FAILED;
- }
- //pDPC->Thread_UnLock();
- //TPRINTA_INFO("DPC Unlock ok");
- return ret;
- }
- void SYSTEM_CALL BusUnitLogic::UnloadDriver(const char *pszBusId)
- {
- TPRINTA_DEBUG("Enter UnloadDriver:%s", pszBusId);
- map<string, vector<TargetDriverInfo>>::iterator iter = m_pProcessInfo->find(pszBusId);
- if (iter != m_pProcessInfo->end())
- {
- //found target busid.
- //notify target quit first
- string DrvBusId = "/";
- DrvBusId += (const char*)iter->first.c_str();
- {
- //got one
- if (WaitForSingleObject((iter->second[0].m_info.hProcess), 0) == WAIT_TIMEOUT)
- {
- //process exist
- LogicClient chClient1;
- chClient1.SetRouter(CCOS_PACKET_ROUTE_LOCAL);
- if (chClient1.Open(DrvBusId.c_str(), ALL_ACCESS, ONE_ACTION_TIMEOUT) >= RET_SUCCEED)
- {
- ResDataObject req;
- if (chClient1.Action_Req("ExitDriverProc", req, ONE_ACTION_TIMEOUT) >= RET_SUCCEED)
- {
- printf("Succeed Action:ExitDriverProc %s ...\n", DrvBusId.c_str());
- TPRINTA_DEBUG("Succeed Action: ExitDriverProc %s ...\n", DrvBusId.c_str());
- chClient1.Close();
- }
- else
- {
- printf("Failed Action:ExitDriverProc -> %s...\n", DrvBusId.c_str());
- TPRINTA_ERROR("Failed Action: ExitDriverProc -> %s ...\n", DrvBusId.c_str());
- chClient1.Close();
- }
- }
- else
- {
- printf("Open local %s Failed\n", DrvBusId.c_str());
- TPRINTA_ERROR("Open local %s Failed\n", DrvBusId.c_str());
- }
- }
- }
- //wait exit
- DWORD wait = WaitForMultipleObjects(1, &(iter->second[0].m_info.hProcess), TRUE, ONE_ACTION_TIMEOUT - 1000);
- if (wait == WAIT_TIMEOUT)
- {
- TerminateProcess((iter->second[0].m_info.hProcess), 0);
- printf("TerminateProcess %s ...\n", DrvBusId.c_str());
- TPRINTA_DEBUG("TerminateProcess %s ...\n", DrvBusId.c_str());
- }
- //make notify
- for (size_t DriverIdx = 0; DriverIdx < iter->second.size(); DriverIdx++)
- {
- MakeDriverNotify((const char *)iter->second[DriverIdx].m_DriverBusId, false);
- }
- //kick busid
- m_pProcessInfo->erase(iter);
- }
- TPRINTA_DEBUG("Exit UnloadDriver:%s", pszBusId);
- return;
- }
- void SYSTEM_CALL BusUnitLogic::UnloadAllRegistedDrivers()
- {
- map<string, vector<TargetDriverInfo>>::iterator iter = m_pProcessInfo->begin();
- while (iter != m_pProcessInfo->end())
- {
- string DrvBusId = "/";
- DrvBusId += (const char*)iter->first.c_str();
- {
- //got one
- if (WaitForSingleObject((iter->second[0].m_info.hProcess), 0) == WAIT_TIMEOUT)
- {
- //process exist
- LogicClient chClient1;
- chClient1.SetRouter(CCOS_PACKET_ROUTE_LOCAL);
- if (chClient1.Open(DrvBusId.c_str(), ALL_ACCESS, ONE_ACTION_TIMEOUT) >= RET_SUCCEED)
- {
- ResDataObject req;
- if (chClient1.Action_Req("ExitDriverProc", req, ONE_ACTION_TIMEOUT) >= RET_SUCCEED)
- {
- printf("Succeed Action:ExitDriverProc %s ...\n", DrvBusId.c_str());
- TPRINTA_DEBUG("Succeed Action: ExitDriverProc %s ...\n", DrvBusId.c_str());
- chClient1.Close();
- }
- else
- {
- printf("Failed Action:ExitDriverProc -> %s...\n", DrvBusId.c_str());
- TPRINTA_ERROR("Failed Action: ExitDriverProc -> %s ...\n", DrvBusId.c_str());
- chClient1.Close();
- }
- }
- else
- {
- printf("Open local %s Failed\n", DrvBusId.c_str());
- TPRINTA_ERROR("Open local %s Failed\n", DrvBusId.c_str());
- }
- }
- }
- ++iter;
- }
- DWORD Idx = 0;
- if (m_pProcessInfo->size() > 64)
- {
- printf("\n\n\n\nthe process number out of 64\n\n\n\n");
- TPRINTA_FATAL("the process number out of 64");
- map<string, vector<TargetDriverInfo>>::iterator iter = m_pProcessInfo->begin();
- while (iter != m_pProcessInfo->end())
- {
- TerminateProcess((iter->second[0].m_info.hProcess), 0);
- ++iter;
- }
- }
- else
- {
- HANDLE *pAllHandles = new HANDLE[m_pProcessInfo->size()];
- memset(pAllHandles, 0, sizeof(HANDLE)*m_pProcessInfo->size());
- map<string, vector<TargetDriverInfo>>::iterator iter = m_pProcessInfo->begin();
- while (iter != m_pProcessInfo->end())
- {
- pAllHandles[Idx] = (iter->second[0].m_info.hProcess);
- ++iter;
- }
- DWORD wait = WaitForMultipleObjects((DWORD)m_pProcessInfo->size(), pAllHandles, TRUE, ONE_ACTION_TIMEOUT - 1000);
- if (wait == WAIT_TIMEOUT)
- {
- map<string, vector<TargetDriverInfo>>::iterator iter = m_pProcessInfo->begin();
- while (iter != m_pProcessInfo->end())
- {
- TerminateProcess((iter->second[0].m_info.hProcess), 0);
- ++iter;
- }
- }
- delete[]pAllHandles;
- }
- return;
- }
|