| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735 |
- #include "stdafx2.h"
- #include "MbnEventImpl.h"
- #include "tinyxml2.h"
- typedef BOOL(WINAPI *PFuncWow64DisableWow64FsRedirection)(PVOID);
- typedef BOOL(WINAPI *PFuncWow64RevertWow64FsRedirection)(PVOID);
- typedef BOOL(WINAPI *PFuncWow64EnableWow64FsRedirection)(BOOLEAN);
- BOOL bEnterWowRedirect = FALSE;
- using namespace tinyxml2;
- void FreeBString(BSTR& bstrValue)
- {
- if(bstrValue != NULL)
- {
- SysFreeString(bstrValue);
- }
- return;
- }
- LPCTSTR ConvertToString(BSTR& bstrValue)
- {
- LPCTSTR lpcszTmp = (LPCTSTR)_com_util::ConvertBSTRToString(bstrValue);
- if(strlen(lpcszTmp) <= 0) {
- return MBN_ERROR_INVALID_STRING;
- }
- return lpcszTmp;
- }
- BOOL DisableWowRedirection()
- {
- PFuncWow64DisableWow64FsRedirection func = NULL;
- PVOID OldWow64RedirectionValue;
- BOOL Result = FALSE;
- HMODULE hMoudle = GetModuleHandle(TEXT("kernel32"));
- if (hMoudle) {
- func = (PFuncWow64DisableWow64FsRedirection)::GetProcAddress(hMoudle,
- "Wow64DisableWow64FsRedirection");
- }
- if (func) {
- Result = func(&OldWow64RedirectionValue);
- }
- return Result;
- }
- BOOLEAN RevertWowRedirection()
- {
- PFuncWow64RevertWow64FsRedirection func = NULL;
- PVOID OldWow64RedirectionValue;
- BOOL Result = FALSE;
- HMODULE hMoudle = GetModuleHandle(TEXT("kernel32"));
- if (hMoudle) {
- func = (PFuncWow64RevertWow64FsRedirection)::GetProcAddress(hMoudle,
- "Wow64RevertWow64FsRedirection");
- }
- if (func) {
- Result = func(&OldWow64RedirectionValue);
- }
- return Result;
- }
- void EnterWowRedirection()
- {
- if(gArch == 64) {
- if(!bEnterWowRedirect) {
- DisableWowRedirection();
- bEnterWowRedirect = TRUE;
- }
- }
- }
- void LeaveWowRedirection()
- {
- if(gArch == 64) {
- if(bEnterWowRedirect) {
- RevertWowRedirection();
- bEnterWowRedirect = FALSE;
- }
- }
- }
- HRESULT STDMETHODCALLTYPE CustomSignalEvents::OnSignalStateChange(IMbnSignal *newInterface)
- {
- HRESULT result = S_OK;
- ULONG ulValue;
- result = newInterface->GetSignalStrength(&ulValue);
- if(FAILED(result)) {
- Dbg(">>>OnSignalStateChange: GetSignalStrength failed [result#%d]", result);
- } else {
- m_nFormattedVal = GetSignalRank(ulValue);
- Dbg(">>>OnSignalStateChange: %d(%u)", m_nFormattedVal, ulValue);
- if(m_pHelper) {
- m_pHelper->SetSignalVal(m_nFormattedVal, ulValue);
- }
- }
- return S_OK;
- }
- HRESULT STDMETHODCALLTYPE CustomConnectionEvents::OnConnectComplete(
- IMbnConnection *newConnection,
- ULONG requestID, HRESULT status)
- {
- HRESULT result = S_OK;
- //置连接成功状态
- if(m_pHelper)
- {
- BSTR interfaceID;
- result = newConnection->get_InterfaceID(&interfaceID);
- if(SUCCEEDED(result))
- {
- m_pHelper->OnConnectComplete(requestID, ConvertToString(interfaceID), status);
- FreeBString(interfaceID);
- }
- else
- {
- m_pHelper->OnConnectComplete(requestID, "", status);
- }
- }
- return result;
- }
- HRESULT STDMETHODCALLTYPE CustomConnectionEvents::OnDisconnectComplete(
- IMbnConnection *newConnection,
- ULONG reqeustID, HRESULT status)
- {
- HRESULT result = S_OK;
- //置断开连接状态
- if(m_pHelper)
- {
- BSTR interfaceID;
- result = newConnection->get_InterfaceID(&interfaceID);
- if(SUCCEEDED(result))
- {
- m_pHelper->OnDisconnectComplete(reqeustID, ConvertToString(interfaceID));
- FreeBString(interfaceID);
- }
- else
- {
- m_pHelper->OnDisconnectComplete(reqeustID, "");
- }
- }
- return result;
- }
- HRESULT STDMETHODCALLTYPE CustomConnectionEvents::OnConnectStateChange( IMbnConnection *newConnection)
- {
- HRESULT result = S_OK;
- MBN_ACTIVATION_STATE state;
- BSTR profileName;
- result = newConnection->GetConnectionState(&state, &profileName);
- if(SUCCEEDED(result))
- {
- BSTR interfaceID;
- result = newConnection->get_InterfaceID(&interfaceID);
- if(SUCCEEDED(result))
- {
- m_pHelper->OnConnectStateChange(result, state, ConvertToString(interfaceID));
- FreeBString(interfaceID);
- }
- else
- {
- m_pHelper->OnConnectStateChange(result, state, "");
- }
- }
- return S_OK;
- }
- HRESULT STDMETHODCALLTYPE CustomConnectionEvents::OnVoiceCallStateChange( IMbnConnection *newConnection)
- {
- HRESULT result = S_OK;
- //Dbg(">>>OnVoiceCallStateChange !!?");
- return S_OK;
- }
- HRESULT STDMETHODCALLTYPE CustomConnctionMnrEvents::OnConnectionArrival(IMbnConnection *newConnection)
- {
- HRESULT result = S_OK;
- //检测到设备连接
- if(m_pHelper)
- {
- BSTR interfaceID;
- result = newConnection->get_InterfaceID(&interfaceID);
- if(SUCCEEDED(result))
- {
- m_pHelper->OnConnectionArrival(ConvertToString(interfaceID));
- FreeBString(interfaceID);
- }
- }
- return S_OK;
- }
- HRESULT STDMETHODCALLTYPE CustomConnctionMnrEvents::OnConnectionRemoval(IMbnConnection *oldConnection)
- {
- HRESULT result = S_OK;
- //检测到设备移除
- if(m_pHelper)
- {
- BSTR interfaceID;
- result = oldConnection->get_InterfaceID(&interfaceID);
- if(SUCCEEDED(result))
- {
- m_pHelper->OnConnectionRemoval(ConvertToString(interfaceID));
- FreeBString(interfaceID);
- }
- }
-
- return S_OK;
- }
- HRESULT STDMETHODCALLTYPE CustomConnectionProfileManagerEvents::OnConnectionProfileArrival(
- IMbnConnectionProfile *newConnectionProfile)
- {
- m_pHelper->OnConnectionProfileArrival(newConnectionProfile);
- return S_OK;
- }
- HRESULT STDMETHODCALLTYPE CustomConnectionProfileManagerEvents::OnConnectionProfileRemoval(
- IMbnConnectionProfile *oldConnectionProfile)
- {
- m_pHelper->OnConnectionProfileRemoval(oldConnectionProfile);
- return S_OK;
- }
- HRESULT STDMETHODCALLTYPE CustomConnectionProfileEvents::OnProfileUpdate(IMbnConnectionProfile *newProfile)
- {
- m_pHelper->OnProfileUpdate(newProfile);
- return S_OK;
- }
- int CMbnImplHelper::EnumerateInterfaces(BOOL bUpdateOnly)
- {
- LOG_FUNCTION();
- BOOL bCircle = FALSE;
- static int nWwanFlag = 0;
- HRESULT result = S_OK;
- LONG lowLocalBound = 1;
- LONG highLocalBound = 0;
- ATL::CComPtr<IMbnInterfaceManager> pInterfaceMnr = NULL;
- result = CoCreateInstance(CLSID_MbnInterfaceManager, NULL, CLSCTX_ALL,
- IID_IMbnInterfaceManager,(LPVOID*)&pInterfaceMnr);
- if(FAILED(result)) {
- SetInteralError(result);
- if(bUpdateOnly) {
- SetStatusFromTmp2Main();
- }
- Dbg("*** ERROR: Failed to Initialize IMbnInterfaceManager with code 0x%08x", result);
- return -1;
- }
- int retVal = -1;
- SAFEARRAY* mbnInterfaces = NULL;
- if(bUpdateOnly) {
- ClearCurInterface();
- }
- do {
- bCircle = FALSE;
- result = pInterfaceMnr->GetInterfaces(&mbnInterfaces);
- if(FAILED(result)) {
- SetInteralError(result);
- if(result == E_POINTER) {
- Dbg("*** ERROR: The mbnInterfaces parameter is NULL");
- } else if(result == E_OUTOFMEMORY) {
- Dbg("*** ERROR: Could not allocate the required memory for inferfaces");
- } else {
- //经验证,很多情况下是因为没有插入网卡设备才会报此错误
- if(result == HRESULT_FROM_WIN32(ERROR_NOT_FOUND))
- {
- SetTmpState(MBD_NO_MOBILEDONGLE);
- Dbg("*** ERROR: Failed to retrieve MBN Interfaces because no interface...");
- retVal = 0;
- }
- else {
- Dbg("*** ERROR: Failed to retrieve MBN Interfaces with code 0x%08x", result);
- if(result == 0x80070426 && nWwanFlag < 2) {
- //Wwan autoconfig 服务未启动
- nWwanFlag++;
- Dbg("Specified error code: Some service has not been started.");
- m_pFSM->TryOpenWwanSvc();
- Sleep(1500);
- bCircle = TRUE;
- continue;
- }
- }
- }
- break;
- }
- result = SafeArrayGetLBound(mbnInterfaces, 1, &lowLocalBound);
- if(FAILED(result)) {
- SetInteralError(result);
- Dbg("***ERROR: Failed to retrieve First Interface with code 0x%08x", result);
- break;
- }
- result = SafeArrayGetUBound(mbnInterfaces, 1, &highLocalBound);
- if(FAILED(result)) {
- SetInteralError(result);
- Dbg("***ERROR: Failed to retrieve Last Interface with code 0x%08x", result);
- break;
- }
- if(lowLocalBound > highLocalBound)
- {
- result = HRESULT_FROM_WIN32(ERROR_NOT_FOUND);
- Dbg("***Warning: There are no interface been found !!!");
- SetTmpState(MBD_NO_MOBILEDONGLE);
- retVal = 0;
- break;
- }
- Dbg("Interfaces: low:%d, high:%d.", lowLocalBound, highLocalBound);
- if(bUpdateOnly) {
- g_DongleInfos.Init(highLocalBound-lowLocalBound+1);
- m_bHasProfile = FALSE;
- } else {
- retVal = (int)(highLocalBound - lowLocalBound + 1);
- break;
- }
- ATL::CComPtr<IMbnInterface> curInterface = NULL;
- CSimpleStringA csSecondInterfaceID(true);
- CSimpleStringA csFirstInterfaceID(true);
- MOBILEDIAL_CONNECTION_STATE cuState = MBD_INTERNAL_ERROR;
- int errorIndex = 0;
- for(LONG index=lowLocalBound; index<=highLocalBound; index++)
- {
- const int cur = index - lowLocalBound;
- ptMbnDongleInfo curDongle = NULL;
- curDongle = &(g_DongleInfos[cur]);
- memset(curDongle, 0, sizeof(MbnDongleInfo));
- curInterface = NULL;
- result = SafeArrayGetElement(mbnInterfaces, &index, (void*)(&curInterface));
- if(FAILED(result)) {
- SetTmpState(MBD_INTERNAL_ERROR);
- Dbg("***ERROR: Failed to retrieve interface element with code 0x%08x", result);
- continue;
- }
- BSTR bsInterfaceID;
- result = curInterface->get_InterfaceID(&bsInterfaceID);
- if(SUCCEEDED(result)) {
- CSimpleStringA csInterfaceID = ConvertToString(bsInterfaceID);
- curDongle->usDeviceType = DEVTYPE_INTER;
- curDongle->csInterfaceId = csInterfaceID;
- GetInterfaceInfo(curDongle->csInterfaceId, &(curDongle->subInfo));
- if(!SearchForProfileName(curDongle)) {
- Dbg("WARN: search profiles for %s failed!", (LPCTSTR)csInterfaceID);
- } else if(!m_bHasProfile && curDongle->profileList.GetCount() > 0) {
- m_bHasProfile = TRUE;
- }
- if(curDongle->subInfo.mbnCntState == MBN_ACTIVATION_STATE_ACTIVATED) {
- Dbg("bcz it's connected now...");
- curDongle->state = MBD_ACTIVATION_CONNECTED;
- SetCurInterface(csInterfaceID);
- } else {
- BOOL isValid = _IsValidInterface(curInterface);
- curDongle->state = m_tmpState;
- Dbg("IsValid: %d", isValid);
- if(!isValid && (
- curDongle->subInfo.strProviderName.IsNullOrEmpty()
- || !curDongle->subInfo.strProviderName.Compare(MBN_ERROR_INVALID_STRING))) {
- curDongle->subInfo.strProviderName =
- CSimpleStringA::Format(MBN_ERROR_INVALID_STRING_WITH_PARAM, ++errorIndex);
- }
- if(isValid && curDongle->profileList.GetCount() <= 0) {
- curDongle->state = MBD_NO_PROFILE;
- }
- if(!m_bSelected) {
- if(isValid) {
- if(curDongle->state != MBD_NO_PROFILE) {
- csFirstInterfaceID = csInterfaceID;
- } else {
- csSecondInterfaceID = csInterfaceID;
- }
- } else if(csSecondInterfaceID.IsNullOrEmpty()) {
- csSecondInterfaceID = csInterfaceID;
- }
- }
- }
- } else {
- SetInteralError(result);
- Dbg("Get interface id for index#%d failed: 0x%X", cur, result);
- }
- }
- retVal = (int)(highLocalBound - lowLocalBound + 1);
- if(!m_bSelected) {
- if(!csFirstInterfaceID.IsNullOrEmpty()) {
- SetCurInterface(csFirstInterfaceID);
- } else if(!csSecondInterfaceID.IsNullOrEmpty()) {
- SetCurInterface(csSecondInterfaceID);
- } else {
- SetTmpState(MBD_INTERNAL_ERROR);
- }
- }
- } while (bCircle);
- if(mbnInterfaces) {
- SafeArrayDestroy(mbnInterfaces);
- mbnInterfaces = NULL;
- }
- if(bUpdateOnly && !m_bSelected) {
- SetStatusFromTmp2Main();
- }
- return retVal;
- }
- BOOL CMbnImplHelper::GetInterface(LPCTSTR interfaceID)
- {
- HRESULT result = S_OK;
- ATL::CComPtr<IMbnInterfaceManager> pInterfaceMnr = NULL;
- result = CoCreateInstance(CLSID_MbnInterfaceManager, NULL, CLSCTX_ALL,
- IID_IMbnInterfaceManager,(LPVOID*)&pInterfaceMnr);
- if(FAILED(result))
- {
- SetInteralError(result);
- Dbg("*** ERROR: Failed to Initialize IMbnInterfaceManager with code 0x%08x", result);
- return FALSE;
- }
- m_pTmpInterface = NULL;
- result = pInterfaceMnr->GetInterface(
- (LPCWSTR)CSimpleStringA2W(CSimpleStringA(interfaceID)), &m_pTmpInterface);
- if(FAILED(result))
- {
- m_pFSM->m_csErrMsg = CSimpleStringA::Format("获取网卡接口失败 (0x%X)", result);
- if(result == HRESULT_FROM_WIN32(ERROR_NOT_FOUND))
- {
- m_pFSM->m_csErrMsg = "未找到对应的网卡";
- Dbg("There is no available interface matching the interfaceID[%s].", interfaceID);
- }
- else if(result == E_POINTER)
- {
- Dbg("The mbnInterface parameter is NULL.");
- }
- else if(result == E_OUTOFMEMORY)
- {
- Dbg("Could not allocate the required memory.");
- }
- else
- {
- Dbg("Retrive interface by %s failed returned 0x%08x", interfaceID, result);
- }
- return FALSE;
- }
- return TRUE;
- }
- ATL::CComPtr<IMbnInterface> CMbnImplHelper::GetInterfaceEx(LPCTSTR interfaceID)
- {
- HRESULT result = S_OK;
- ATL::CComPtr<IMbnInterfaceManager> pInterfaceMnr = NULL;
- result = CoCreateInstance(CLSID_MbnInterfaceManager, NULL, CLSCTX_ALL,
- IID_IMbnInterfaceManager,(LPVOID*)&pInterfaceMnr);
- if(FAILED(result))
- {
- SetInteralError(result);
- Dbg("*** ERROR: Failed to Initialize IMbnInterfaceManager with code 0x%08x", result);
- return NULL;
- }
- ATL::CComPtr<IMbnInterface> pInterface = NULL;
- result = pInterfaceMnr->GetInterface(
- (LPCWSTR)CSimpleStringA2W(CSimpleStringA(interfaceID)), &pInterface);
- if(FAILED(result))
- {
- SetTmpState(MBD_INTERNAL_ERROR);
- m_pFSM->m_csErrMsg = CSimpleStringA::Format("获取网卡接口失败 (0x%X)", result);
- if(result == HRESULT_FROM_WIN32(ERROR_NOT_FOUND)) {
- m_pFSM->m_csErrMsg = "未找到对应的网卡";
- SetTmpState(MBD_NO_MOBILEDONGLE);
- Dbg("There is no available interface matching the interfaceID[%s].", interfaceID);
- } else if(result == E_POINTER) {
- Dbg("The mbnInterface parameter is NULL.");
- }
- else if(result == E_OUTOFMEMORY) {
- Dbg("Could not allocate the required memory.");
- } else {
- Dbg("Retrive interface by %s failed returned 0x%08x", interfaceID, result);
- }
- return NULL;
- }
- return pInterface;
- }
- BOOL CMbnImplHelper::SetCurInterface(LPCTSTR interfaceID)
- {
- LOG_FUNCTION();
- BOOL fValid = FALSE;
- ATL::CComPtr<IMbnInterface> pInterface = GetInterfaceEx(interfaceID);
- if(pInterface) {
- m_interfaceID = CSimpleStringA(interfaceID);
- SetRequestID();
- m_bSelected = TRUE;
- m_nWhichGnrt = 0;
- fValid = _IsValidInterface(pInterface);
- SetStatusFromTmp2Main();
- if(fValid && !SearchForProfileName()) {
- SetStatusFromTmp2Main();
- }
- //TODO: BUGFIX: [11/23/2019 15:53 @Gifur]
- if(!fValid) {
- Dbg("Although is invalid, give it a chance.");
- fValid = TRUE;
- }
- } else {
- SetStatusFromTmp2Main();
- }
- Dbg("Set CurInterface as %s returned %d.", interfaceID, fValid);
- return fValid;
- }
- BOOL CMbnImplHelper::CheckAndRefleshCurInterface()
- {
- if(m_bSelected) {
- if(IsValidInterface(m_interfaceID)) {
- return GetConnectState();
- } else {
- SetStatusFromTmp2Main();
- return FALSE;
- }
- }
- return FALSE;
- }
- BOOL CMbnImplHelper::IsValidInterface(LPCTSTR interfaceID)
- {
- LOG_FUNCTION();
- ATL::CComPtr<IMbnInterface> pInterface = GetInterfaceEx(interfaceID);
- if(!pInterface) {
- Dbg("GetInterfaceEx failed !");
- return FALSE;
- }
- int nWaitTimes = 3, nTriTimes = 0;
- DWORD dwSleep = 1200;
- for(; nTriTimes < nWaitTimes; ++nTriTimes) {
- m_tmpState = MBD_UNKNOWN;
- if(_IsValidInterface(pInterface)) {
- Dbg("Confirm _IsValidInterface is OK, then break!!");
- break;
- }
- if(m_tmpState == MBD_READY_SIM_NO_INSERT) {
- nTriTimes = nWaitTimes + 1;
- Dbg("no insert sim!!");
- break;
- }
- Sleep(dwSleep);
- pInterface = GetInterfaceEx(interfaceID);
- if(!pInterface) {
- Dbg("GetInterfaceEx in %d times failed!", nTriTimes);
- return FALSE;
- }
- }
- if(nTriTimes >= nWaitTimes) {
- Dbg("invalid dongle!!");
- return FALSE;
- }
- nTriTimes = 0;
- nWaitTimes = 5;
- BOOL bRet = TRUE;
- MBN_ACTIVATION_STATE state;
- for(nTriTimes=0; nTriTimes<nWaitTimes; ++nTriTimes)
- {
- bRet = GetConnectState(pInterface, state);
- if(bRet && (state == MBN_ACTIVATION_STATE_ACTIVATED
- || state == MBN_ACTIVATION_STATE_DEACTIVATED)) {
- break;
- }
- else if(!bRet) {
- Dbg("get cn state failed!!");
- break;
- } else {
- SetTmpState((MOBILEDIAL_CONNECTION_STATE)(MBD_ACTIVATION_NONE + state));
- }
- Sleep(dwSleep);
- pInterface = GetInterfaceEx(interfaceID);
- if(!pInterface) {
- Dbg("GetInterfaceEx in %d times failed!", nTriTimes);
- return FALSE;
- }
- }
- if(bRet) {
- int nGnrt = GetGeneration(pInterface);
- nWaitTimes = 3;
- dwSleep = 1200;
- for(nTriTimes=0; nGnrt <= 2 && nTriTimes < nWaitTimes; ++nTriTimes) {
- Dbg("GetGeneration [res=%d] [times=%d]", nGnrt, nTriTimes);
- Sleep(dwSleep);
- pInterface = GetInterfaceEx(interfaceID);
- if(!pInterface) {
- Dbg("GetInterfaceEx in %d times failed!", nTriTimes);
- return FALSE;
- }
- nGnrt = GetGeneration(pInterface);
- }
- m_nWhichGnrt = nGnrt;
- if(nTriTimes >= nWaitTimes) {
- bRet = FALSE;
- }
- }
- return bRet;
- }
- BOOL CMbnImplHelper::_IsValidInterface(ATL::CComPtr<IMbnInterface> pInterface)
- {
- LOG_FUNCTION();
- if(pInterface == NULL) {
- SetTmpState(MBD_NO_MOBILEDONGLE);
- return FALSE;
- }
- MBN_INTERFACE_CAPS interfaceCaps;
- HRESULT result = S_OK;
- result = pInterface->GetInterfaceCapability(&interfaceCaps);
- if(SUCCEEDED(result)) {
- if(interfaceCaps.dataClass & MBN_DATA_CLASS_CUSTOM)
- SysFreeString(interfaceCaps.customDataClass);
- SysFreeString(interfaceCaps.deviceID);
- SysFreeString(interfaceCaps.manufacturer);
- SysFreeString(interfaceCaps.model);
- SysFreeString(interfaceCaps.firmwareInfo);
- if((interfaceCaps.gsmBandClass & MBN_BAND_CLASS_CUSTOM) ||
- (interfaceCaps.cdmaBandClass & MBN_BAND_CLASS_CUSTOM))
- SysFreeString(interfaceCaps.customBandClass);
- } else {
- SetInteralError(result);
- Dbg(">>> GetInterfaceCapability failed returned 0x%x", result);
- return FALSE;
- }
- MOBILEDIAL_CONNECTION_STATE& curState = m_tmpState;
- ////////////////////////////Ready State//////////////////////////////////////////////
- MBN_READY_STATE readyState;
- result = pInterface->GetReadyState(&readyState);
- if(SUCCEEDED(result)) {
-
- if(readyState != MBN_READY_STATE_INITIALIZED) {
- Dbg(">>> Ready State: %s", StateToString(readyState, curState));
- return FALSE;
- }
- } else {
- Dbg(">>> Get Ready State failed returned 0x%x", result);
- curState = MBD_READY_BAD_SIM; //or MBD_INTERNAL_ERROR?
- return FALSE;
- }
- ////////////////////////////Registration State//////////////////////////////////////////////
- IMbnRegistration* pRegistration;
- result = pInterface->QueryInterface(IID_IMbnRegistration, (void**)&pRegistration);
- if(SUCCEEDED(result)) {
- MBN_REGISTER_STATE registerState;
- result = pRegistration->GetRegisterState(®isterState);
- if(SUCCEEDED(result)) {
- if(registerState != MBN_REGISTER_STATE_HOME
- && registerState != MBN_REGISTER_STATE_ROAMING
- && registerState != MBN_REGISTER_STATE_PARTNER) {
- Dbg(">>> Register State: %s", RegisterStateToString(registerState, curState));
- return FALSE;
- }
- } else {
- Dbg(">>> Get RegisterState failed returned 0x%x", result);
- curState = MBD_READY_BAD_SIM;
- return FALSE;
- }
- BSTR providerName;
- result = pRegistration->GetProviderName(&providerName);
- if(SUCCEEDED(result)) {
- Dbg(">>> Provider Name: %s", ConvertToString(providerName));
- SysFreeString(providerName);
- }
- SAFE_RELEASE(pRegistration);
- } else {
- Dbg(">>> Query Regist Interface failed returned 0x%x", result);
- curState = MBD_READY_BAD_SIM;
- return FALSE;
- }
- ////////////////////////////Connect State//////////////////////////////////////////////
- IMbnConnection* pIMbnConnection = NULL;
- result = pInterface->GetConnection(&pIMbnConnection);
- if(FAILED(result)) {
- Dbg(">>> Get connection failed returned 0x%x", result);
- curState = MBD_READY_BAD_SIM;
- return FALSE;
- } else {
- BSTR profileName;
- MBN_ACTIVATION_STATE cntState;
- result = pIMbnConnection->GetConnectionState(&cntState, &profileName);
- if(SUCCEEDED(result)) {
- Dbg(">>> Connection State: %s", ActivationStateToString(cntState));
- if(cntState == MBN_ACTIVATION_STATE_ACTIVATED) {
- if(profileName) {
- Dbg(">>> Profile Name: %s", ConvertToString(profileName));
- FreeBString(profileName);
- } else {
- Dbg(">>> Cannot fetch connected profile name!");
- }
- }
-
- curState = (MOBILEDIAL_CONNECTION_STATE)(MBD_ACTIVATION_NONE + cntState);
- if(cntState != MBN_ACTIVATION_STATE_ACTIVATED && cntState != MBN_ACTIVATION_STATE_DEACTIVATED) {
- return FALSE;
- }
- }
- else {
- Dbg(">>> Get ConnectionState failed returned 0x%x", result);
- curState = MBD_READY_BAD_SIM;
- return FALSE;
- }
- }
- return TRUE;
- }
- BOOL CMbnImplHelper::GetInterfaceInfo(LPCTSTR interfaceID, pMbnDongleSubInfo pDongleSubInfo)
- {
- LOG_FUNCTION();
- BOOL res = TRUE;
- MBN_INTERFACE_CAPS interfaceCaps;
- HRESULT result = S_OK;
- assert(pDongleSubInfo != NULL && "pDongleSubInfo != NULL");
- if(!GetInterface(interfaceID)) {
- return FALSE;
- }
- memset(pDongleSubInfo, 0, sizeof(MbnDongleSubInfo));
- InitMbnDongleSubInfo(pDongleSubInfo);
- //TODO: LOCK
- ATL::CComPtr<IMbnInterface> curInterface = m_pTmpInterface;
- result = curInterface->GetInterfaceCapability(&interfaceCaps);
- if(SUCCEEDED(result))
- {
- pDongleSubInfo->strDeviceID = ConvertToString(interfaceCaps.deviceID);
- pDongleSubInfo->strManufacturer = ConvertToString(interfaceCaps.manufacturer);
- pDongleSubInfo->strMode = ConvertToString(interfaceCaps.model);
- pDongleSubInfo->strFirmware = ConvertToString(interfaceCaps.firmwareInfo);
- pDongleSubInfo->strCellularClass = CellularToString(interfaceCaps.cellularClass);
- Dbg(">>> Device ID: %s", ConvertToString(interfaceCaps.deviceID));
- Dbg(">>> Manufacturer: %s", ConvertToString(interfaceCaps.manufacturer));
- Dbg(">>> Mode: %s", ConvertToString(interfaceCaps.model));
- Dbg(">>> FirmwareInfor: %s", ConvertToString(interfaceCaps.firmwareInfo));
- Dbg(">>> CellularClass: %s", CellularToString(interfaceCaps.cellularClass));
- if(interfaceCaps.dataClass & MBN_DATA_CLASS_CUSTOM)
- SysFreeString(interfaceCaps.customDataClass);
- SysFreeString(interfaceCaps.deviceID);
- SysFreeString(interfaceCaps.manufacturer);
- SysFreeString(interfaceCaps.model);
- SysFreeString(interfaceCaps.firmwareInfo);
- if((interfaceCaps.gsmBandClass & MBN_BAND_CLASS_CUSTOM) ||
- (interfaceCaps.cdmaBandClass & MBN_BAND_CLASS_CUSTOM))
- SysFreeString(interfaceCaps.customBandClass);
- }
- else
- {
- SetInteralError(result);
- Dbg(">>> GetInterfaceCapability failed returned 0x%x", result);
- m_pFSM->m_csErrMsg = "GetInterfaceCapability failed";
- return FALSE;
- }
- MOBILEDIAL_CONNECTION_STATE curState;
- int nErrorCount = 0;
- ////////////////////////////Ready State//////////////////////////////////////////////
- MBN_READY_STATE readyState;
- result = curInterface->GetReadyState(&readyState);
- if(SUCCEEDED(result))
- {
- pDongleSubInfo->mbnReadyState = readyState;
- //pDongleSubInfo->strReadyState = StateToString(readyState, curState);
- Dbg(">>> Ready State: %s", StateToString(readyState, curState));
- }
- else
- { pDongleSubInfo->mbnReadyState = MBN_READY_STATE_OFF;
- Dbg(">>> Get Ready State failed returned 0x%x", result);
- nErrorCount++;
- }
- ////////////////////////////Registration State//////////////////////////////////////////////
- IMbnRegistration* pRegistration;
- result = curInterface->QueryInterface(IID_IMbnRegistration, (void**)&pRegistration);
- if(SUCCEEDED(result))
- {
- MBN_REGISTER_STATE registerState;
- result = pRegistration->GetRegisterState(®isterState);
- if(SUCCEEDED(result))
- {
- Dbg(">>> Register State: %s", RegisterStateToString(registerState, curState));
- //pDongleSubInfo->strRegisterState = RegisterStateToString(registerState, curState);
- pDongleSubInfo->mbnRegState = registerState;
- }
- else
- {
- pDongleSubInfo->mbnRegState = MBN_REGISTER_STATE_NONE;
- Dbg(">>> Get RegisterState failed returned 0x%x", result);
- nErrorCount++;
- }
- MBN_DATA_CLASS dataClass;
- result = pRegistration->GetCurrentDataClass((ULONG*)&dataClass);
- if(SUCCEEDED(result)) {
- pDongleSubInfo->strDataClass = DataClassToString(dataClass);
- Dbg(">>> Data Class: %s(0x%08x)", DataClassToString(dataClass), dataClass);
- }
- else {
- Dbg(">>> Get DataClass failed returned 0x%x", result);
- nErrorCount++;
- }
- BSTR providerName;
- result = pRegistration->GetProviderName(&providerName);
- if(SUCCEEDED(result))
- {
- pDongleSubInfo->strProviderName = ConvertToString(providerName);
- Dbg(">>> Provider Name: %s", ConvertToString(providerName));
- SysFreeString(providerName);
- }
- else {
- Dbg(">>> Get ProviderName failed returned 0x%x", result);
- nErrorCount++;
- }
- SAFE_RELEASE(pRegistration);
- }
- else
- {
- Dbg(">>> Query Regist Interface failed returned 0x%x", result);
- nErrorCount++;
- }
- ////////////////////////////Connect State//////////////////////////////////////////////
- IMbnConnection* pIMbnConnection = NULL;
- result = curInterface->GetConnection(&pIMbnConnection);
- if(FAILED(result)) {
- Dbg(">>> Get connection failed returned 0x%x", result);
- nErrorCount++;
- }
- else
- {
- BSTR profileName;
- MBN_ACTIVATION_STATE cntState;
- result = pIMbnConnection->GetConnectionState(&cntState, &profileName);
- if(SUCCEEDED(result))
- {
- //pDongleSubInfo->strConnectState = ActivationStateToString(cntState);
- pDongleSubInfo->mbnCntState = cntState;
- Dbg(">>> Connection State: %s", ActivationStateToString(cntState));
- if(cntState == MBN_ACTIVATION_STATE_ACTIVATED) {
- if(profileName) {
- Dbg(">>> Profile Name: %s", ConvertToString(profileName));
- pDongleSubInfo->strProfileName = ConvertToString(profileName);
- FreeBString(profileName);
- } else {
- Dbg(">>> Cannot fetch connected profile name!");
- pDongleSubInfo->strProfileName = "";
- }
- }
- }
- else
- {
- Dbg(">>> Get ConnectionState failed returned 0x%x", result);
- nErrorCount++;
- }
- SAFE_RELEASE(pIMbnConnection);
- }
- ////////////////////////////Subscriber State//////////////////////////////////////////////
-
- ATL::CComPtr<IMbnSubscriberInformation> pSubInfo = NULL;
- result = curInterface->GetSubscriberInformation(&pSubInfo);
- if(SUCCEEDED(result))
- {
- BSTR subId;
- pSubInfo->get_SubscriberID(&subId);
- Dbg(">>> subID: %s", ConvertToString(subId));
- pDongleSubInfo->strIMSI = ConvertToString(subId);
- SysFreeString(subId);
- BSTR simIccId;
- result = pSubInfo->get_SimIccID(&simIccId);
- Dbg(">>> SimIccID: %s", ConvertToString(simIccId));
- pDongleSubInfo->strSimIccID = ConvertToString(simIccId);
- }
- IMbnSignal* pImbnSignal = NULL;
- result = curInterface->QueryInterface(IID_IMbnSignal, (void**)&pImbnSignal);
- if(SUCCEEDED(result))
- {
- ULONG ulSignalStrength;
- result = pImbnSignal->GetSignalStrength(&ulSignalStrength);
- if(FAILED(result) || ulSignalStrength == MBN_RSSI_UNKNOWN)
- {
- Dbg(">>> Signal Strength: UNKNOWN");
- pDongleSubInfo->ulSingnal = 0UL;
- }
- else
- {
- Dbg(">>> Signal Strength: %2.0lf%%", 100.0*(double)ulSignalStrength/31.0);
- pDongleSubInfo->ulSingnal = ulSignalStrength;
- }
- SAFE_RELEASE(pImbnSignal);
- }
- return TRUE;
- }
- BOOL CMbnImplHelper::AdviseSignalEvent()
- {
- if(m_bSignalEventInitied)
- {
- return TRUE;
- }
- HRESULT result = S_OK;
- ATL::CComPtr<IMbnInterfaceManager> pInterfaceMnr = NULL;
- result = CoCreateInstance(CLSID_MbnInterfaceManager, NULL, CLSCTX_ALL,
- IID_IMbnInterfaceManager,(LPVOID*)&pInterfaceMnr);
- if(FAILED(result))
- {
- Dbg("*** ERROR: Failed to Initialize IMbnInterfaceManager with code 0x%08x", result);
- return FALSE;
- }
- result = pInterfaceMnr->QueryInterface(IID_IConnectionPointContainer, (void**)&m_pContainer);
- if(SUCCEEDED(result))
- {
- result = m_pContainer->FindConnectionPoint(IID_IMbnSignalEvents, &m_pCntPointForSignal);
- if(SUCCEEDED(result))
- {
- IUnknown *pSignalUnk = (IUnknown*)m_signalEvent;
- result = m_pCntPointForSignal->Advise(pSignalUnk, &m_dwCkSignal);
- if(SUCCEEDED(result))
- {
- Dbg("Advise success, Waiting SignalStateChange Event...");
- m_bSignalEventInitied = TRUE;
- return TRUE;
- }
- else
- {
- Dbg("Advise SignalStateChange Event failed 0x%x", result);
- return FALSE;
- }
- }
- else
- {
- Dbg("FindConnectionPoint of IID_IMbnSignalEvents failed 0x%x", result);
- return FALSE;
- }
- }
- return FALSE;
- }
- BOOL CMbnImplHelper::AdviseCntMnrEvent()
- {
- if(m_bCntMnrEventInitied)
- {
- return TRUE;
- }
- HRESULT result = S_OK;
- ATL::CComPtr<IMbnConnectionManager> pCntMnr = NULL;
- result = CoCreateInstance(CLSID_MbnConnectionManager, NULL, CLSCTX_ALL,
- IID_IMbnConnectionManager, (LPVOID*)&pCntMnr);
- if(FAILED(result))
- {
- Dbg("*** ERROR: Failed to Initialize IMbnConnectionManager with code 0x%08x", result);
- return FALSE;
- }
- result = pCntMnr->QueryInterface(IID_IConnectionPointContainer, (void**)&m_pContainer);
- if(SUCCEEDED(result))
- {
- result = m_pContainer->FindConnectionPoint(IID_IMbnConnectionManagerEvents, &m_pCntPointForCntMnrEvent);
- if(SUCCEEDED(result))
- {
- IUnknown *pConnectUnk = (IUnknown*)m_cntMnrEvent;
- result = m_pCntPointForCntMnrEvent->Advise(pConnectUnk, &m_dwCkCntMnr);
- if(SUCCEEDED(result))
- {
- Dbg("Advise success, Waiting Connection Manager Event...");
- m_bCntMnrEventInitied = TRUE;
- return TRUE;
- }
- else
- {
- Dbg("Advise ConnectionEvent failed 0x%x", result);
- return FALSE;
- }
- }
- else
- {
- Dbg("FindConnectionPoint of IID_IMbnConnectionManagerEvents failed 0x%x", result);
- if(result == CONNECT_E_NOCONNECTION)
- {
- Dbg("This connectable object does not support the outgoing interface specified by riid.");
- }
- return FALSE;
- }
- }
- return FALSE;
- }
- BOOL CMbnImplHelper::AdviseConnectEvent()
- {
- if(m_bCntEventInitied)
- {
- return TRUE;
- }
- HRESULT result = S_OK;
- ATL::CComPtr<IMbnConnectionManager> pCntMnr = NULL;
- result = CoCreateInstance(CLSID_MbnConnectionManager, NULL, CLSCTX_ALL,
- IID_IMbnConnectionManager, (LPVOID*)&pCntMnr);
- if(FAILED(result))
- {
- Dbg("*** ERROR: Failed to Initialize IMbnConnectionManager with code 0x%08x", result);
- return FALSE;
- }
- result = pCntMnr->QueryInterface(IID_IConnectionPointContainer, (void**)&m_pContainer);
- if(SUCCEEDED(result))
- {
- result = m_pContainer->FindConnectionPoint(IID_IMbnConnectionEvents, &m_pCntPointForCntEvent);
- if(SUCCEEDED(result))
- {
- IUnknown *pConnectUnk = (IUnknown*)m_cntEvent;
- result = m_pCntPointForCntEvent->Advise(pConnectUnk, &m_dwCkCnt);
- if(SUCCEEDED(result))
- {
- Dbg("Advise success, Waiting Connection Event...");
- m_bCntEventInitied = TRUE;
- return TRUE;
- }
- else
- {
- Dbg("Advise ConnectionEvent failed 0x%x", result);
- return FALSE;
- }
- }
- else
- {
- Dbg("FindConnectionPoint of IID_IMbnConnectionEvents failed 0x%x", result);
- if(result == CONNECT_E_NOCONNECTION)
- {
- Dbg("This connectable object does not support the outgoing interface specified by riid.");
- }
- return FALSE;
- }
- }
- return FALSE;
- }
- BOOL CMbnImplHelper::AdviseCntProfileMnrEvent()
- {
- if(m_bCntPfMnrEventInited)
- {
- return TRUE;
- }
- HRESULT result = S_OK;
- ATL::CComPtr<IMbnConnectionProfileManager> pCntProfileMnr = NULL;
- result = CoCreateInstance(CLSID_MbnConnectionProfileManager, NULL, CLSCTX_ALL,
- IID_IMbnConnectionProfileManager, (LPVOID*)&pCntProfileMnr);
- if(FAILED(result))
- {
- Dbg("*** ERROR: Failed to Initialize IMbnConnectionProfileManager with code 0x%08x", result);
- return FALSE;
- }
- result = pCntProfileMnr->QueryInterface(IID_IConnectionPointContainer, (void**)&m_pContainer);
- if(SUCCEEDED(result))
- {
- result = m_pContainer->FindConnectionPoint(
- IID_IMbnConnectionProfileManagerEvents, &m_pCntPointForCntProfileMnrEvent);
- if(SUCCEEDED(result))
- {
- IUnknown *pConnectUnk = (IUnknown*)m_cntPfMnrEvent;
- result = m_pCntPointForCntProfileMnrEvent->Advise(pConnectUnk, &m_dwCkCntPfMnr);
- if(SUCCEEDED(result))
- {
- Dbg("Advise success, Waiting Connection profile Manager Event...");
- m_bCntPfMnrEventInited = TRUE;
- return TRUE;
- }
- else
- {
- Dbg("Advise Connection profile Manager failed 0x%x", result);
- return FALSE;
- }
- }
- else
- {
- Dbg("FindConnectionPoint of IID_IMbnConnectionProfileManagerEvents failed 0x%x", result);
- if(result == CONNECT_E_NOCONNECTION)
- {
- Dbg("This connectable object does not support the outgoing interface specified by riid.");
- }
- return FALSE;
- }
- }
- return FALSE;
- }
- BOOL CMbnImplHelper::AdviseCntProfileEvent()
- {
- if(m_bCntPfEventInited)
- {
- return TRUE;
- }
- HRESULT result = S_OK;
- ATL::CComPtr<IMbnConnectionProfileManager> pCntProfileMnr = NULL;
- result = CoCreateInstance(CLSID_MbnConnectionProfileManager, NULL, CLSCTX_ALL,
- IID_IMbnConnectionProfileManager, (LPVOID*)&pCntProfileMnr);
- if(FAILED(result))
- {
- Dbg("*** ERROR: Failed to Initialize IMbnConnectionProfileManager with code 0x%08x", result);
- return FALSE;
- }
- result = pCntProfileMnr->QueryInterface(IID_IConnectionPointContainer, (void**)&m_pContainer);
- if(SUCCEEDED(result))
- {
- result = m_pContainer->FindConnectionPoint(
- IID_IMbnConnectionProfileEvents, &m_pCntPointForProfileEvent);
- if(SUCCEEDED(result))
- {
- IUnknown *pConnectUnk = (IUnknown*)m_cntPfEvent;
- result = m_pCntPointForProfileEvent->Advise(pConnectUnk, &m_dwCkPf);
- if(SUCCEEDED(result))
- {
- Dbg("Advise success, Waiting Connection profile Event...");
- m_bCntPfEventInited = TRUE;
- return TRUE;
- }
- else
- {
- Dbg("Advise Connection profile failed 0x%x", result);
- return FALSE;
- }
- }
- else
- {
- Dbg("FindConnectionPoint of IID_IMbnConnectionProfileEvents failed 0x%x", result);
- if(result == CONNECT_E_NOCONNECTION)
- {
- Dbg("This connectable object does not support the outgoing interface specified by riid.");
- }
- return FALSE;
- }
- }
- return FALSE;
- }
- BOOL CMbnImplHelper::UnAdviseCustomEvents()
- {
- HRESULT result = S_OK;
- if(m_bCntEventInitied)
- {
- result = m_pCntPointForCntEvent->Unadvise(m_dwCkCnt);
- Dbg("Unadvise Connection Event return 0x%x", result);
- if(SUCCEEDED(result))
- {
- SAFE_RELEASE(m_pCntPointForCntEvent);
- m_bCntEventInitied = FALSE;
- }
- }
- else
- {
- }
- if(m_bSignalEventInitied)
- {
- result = m_pCntPointForSignal->Unadvise(m_dwCkSignal);
- Dbg("Unadvise Signal Event return 0x%x", result);
- if(SUCCEEDED(result))
- {
- SAFE_RELEASE(m_pCntPointForSignal);
- m_bSignalEventInitied = FALSE;
- }
- }
- else
- {
- }
- if(m_bCntMnrEventInitied)
- {
- result = m_pCntPointForCntMnrEvent->Unadvise(m_dwCkCntMnr);
- Dbg("Unadvise Connection Manage Event return 0x%x", result);
- if(SUCCEEDED(result))
- {
- SAFE_RELEASE(m_pCntPointForCntMnrEvent);
- m_bCntMnrEventInitied = FALSE;
- }
- }
- else
- {
- }
- if(m_bCntPfMnrEventInited)
- {
- result = m_pCntPointForCntProfileMnrEvent->Unadvise(m_dwCkCntPfMnr);
- if(SUCCEEDED(result))
- {
- SAFE_RELEASE(m_pCntPointForCntProfileMnrEvent);
- m_bCntPfMnrEventInited = FALSE;
- }
- }
- else
- {
- }
- if(m_bCntPfEventInited)
- {
- result = m_pCntPointForProfileEvent->Unadvise(m_dwCkPf);
- if(SUCCEEDED(result))
- {
- SAFE_RELEASE(m_pCntPointForProfileEvent);
- m_bCntPfEventInited = FALSE;
- }
- }
- else
- {
- }
- return TRUE;
- }
- BOOL CMbnImplHelper::GetConnectState()
- {
- if(m_bSelected) {
- ATL::CComPtr<IMbnInterface> pInterface = GetInterfaceEx(m_interfaceID);
- if(GetConnectState(pInterface, m_cntState)) {
- SetCurState((MOBILEDIAL_CONNECTION_STATE)(MBD_ACTIVATION_NONE + m_cntState));
- Dbg("GetConnectState{%s}.", ConvertStatusToString(m_curState));
- return TRUE;
- }
- SetStatusFromTmp2Main();
- }
- return FALSE;
- }
- BOOL CMbnImplHelper::GetConnectState(ATL::CComPtr<IMbnInterface> pInterface, MBN_ACTIVATION_STATE& state)
- {
- state = MBN_ACTIVATION_STATE_NONE;
- HRESULT result = S_OK;
- if(!pInterface) {
- Dbg("pInterface is invalid");
- SetTmpState(MBD_INTERNAL_ERROR);
- return FALSE;
- }
- ATL::CComPtr<IMbnConnection> pConnection = NULL;
- result = pInterface->GetConnection(&pConnection);
- if(FAILED(result))
- {
- if(result == HRESULT_FROM_WIN32(ERROR_NOT_FOUND)){
- Dbg("***Either there is no available connection or the device is not registered to a network.");
- SetTmpState(MBD_READY_BAD_SIM);
- }
- else {
- SetInteralError(result);
- Dbg("***ERROR: Failed to retrieve mbnConnection with code 0x%08x", result);
- }
- return FALSE;
- }
- BSTR profileName;
- result = pConnection->GetConnectionState(&state, &profileName);
- if(FAILED(result)) {
- if(result == E_MBN_SIM_NOT_INSERTED) {
- Dbg("***ERROR: A SIM is not inserted in the device.");
- SetTmpState(MBD_READY_SIM_NO_INSERT);
- }
- else if(result == E_MBN_PIN_REQUIRED) {
- SetTmpState(MBD_CONNECTION_PIN_REQUIRED);
- Dbg("***ERROR: A PIN is required to get the call state.");
- }
- else if(result == E_MBN_BAD_SIM) {
- Dbg("***ERROR: A bad SIM is inserted in the device.");
- SetTmpState(MBD_READY_BAD_SIM);
- }
- else if(result == E_PENDING) {
- SetTmpState(MBD_UNKNOWN);
- Dbg("***ERROR: The activation state not available.");
- }else {
- SetInteralError(result);
- Dbg("***ERROR: Failed to retrieve GetConnectionState with code 0x%08x", result);
- }
- return FALSE;
- }
- if(state == MBN_ACTIVATION_STATE_ACTIVATED){
- FreeBString(profileName);
- }
- SetTmpState((MOBILEDIAL_CONNECTION_STATE)(MBD_ACTIVATION_NONE + state));
- return TRUE;
- }
- int CMbnImplHelper::GetGeneration(ATL::CComPtr<IMbnInterface> pInterface)
- {
- int nRes = 1;
- if(!pInterface) {
- Dbg("Detect pInterface is NULL in GetGeneration");
- SetTmpState(MBD_INTERNAL_ERROR);
- return nRes;
- }
- HRESULT result = S_OK;
- IMbnRegistration* pRegistration;
- result = pInterface->QueryInterface(IID_IMbnRegistration, (void**)&pRegistration);
- if(SUCCEEDED(result)) {
- MBN_DATA_CLASS dataClass;
- result = pRegistration->GetCurrentDataClass((ULONG*)&dataClass);
- if(SUCCEEDED(result)) {
- Dbg(">>> Data Class: %s(0x%08x)", DataClassToString(dataClass), dataClass);
- SAFE_RELEASE(pRegistration);
- if(dataClass == MBN_DATA_CLASS_NONE) {
- //m_nWhichGnrt = 2;
- nRes = 2;
- }
- else if((dataClass & MBN_DATA_CLASS_LTE) == MBN_DATA_CLASS_LTE) {
- //m_nWhichGnrt = 4;
- nRes = 4;
- }else {
- //m_nWhichGnrt = 3;
- nRes = 3;
- }
- } else {
- if(result == E_PENDING) {
- Dbg("***Warning: The data classes are not available.");
- //m_nWhichGnrt = 2;
- SetTmpState(MBD_UNKNOWN);
- nRes = 2;
- }
- else if(result == E_MBN_PIN_REQUIRED) {
- Dbg("***ERROR: Data Class - A PIN is required to get the data classes.");
- SetTmpState(MBD_CONNECTION_PIN_REQUIRED);
- }
- else if(result == E_MBN_SIM_NOT_INSERTED) {
- Dbg("***ERROR: Data Class - A SIM is not inserted in the device.");
- SetTmpState(MBD_READY_SIM_NO_INSERT);
- nRes = 0;
- }
- else if(result == E_MBN_BAD_SIM) {
- Dbg("***ERROR: Data Class - A bad SIM is inserted in the device.");
- SetTmpState(MBD_READY_BAD_SIM);
- }
- else {
- SetInteralError(result);
- Dbg("***ERROR: Failed to retrieve DataClass failed returned 0x%08x.", result);
- }
- }
- }
- return nRes;
- }
- BOOL CMbnImplHelper::GetSignalValue(ATL::CComPtr<IMbnInterface> pInterface, ULONG& ulSignalStrength)
- {
- HRESULT result = S_OK;
- if(!pInterface)
- {
- //SetCurState(MBD_NO_MOBILEDONGLE);
- Dbg("Detect pInterface is NULL in SetSingnalValue");
- return FALSE;
- }
-
- IMbnSignal* pImbnSignal = NULL;
- result = pInterface->QueryInterface(IID_IMbnSignal, (void**)&pImbnSignal);
- if(SUCCEEDED(result))
- {
- result = pImbnSignal->GetSignalStrength(&ulSignalStrength);
- SAFE_RELEASE(pImbnSignal);
- if(SUCCEEDED(result)) {
- //SetSignalVal(GetSignalRank(ulSignalStrength), ulSignalStrength);
- return TRUE;
- }
- }
- return FALSE;
- }
- BOOL CMbnImplHelper::SearchForProfileName(ptMbnDongleInfo pInfo)
- {
- LOG_FUNCTION();
- MbnDongleInfo origin;
- bool bSpecified = true;
- if(pInfo == NULL) {
- memset(&origin, 0, sizeof(origin));
- pInfo = &origin;
- bSpecified = false;
- InitMbnDongleSubInfo(&(pInfo->subInfo));
- GetInterfaceInfo(m_interfaceID, &(pInfo->subInfo));
- pInfo->profileList.Clear();
- } else {
- Dbg("Search for ProfileName of %s", (LPCTSTR)pInfo->csInterfaceId);
- if(pInfo->profileList.GetCount() > 0) {
- pInfo->profileList.Clear();
- }
- }
- if(!bSpecified && !m_bSelected) {
- Dbg("Detect m_pCurInterface is NULL in ");
- return FALSE;
- }
- HRESULT result = S_OK;
- ATL::CComPtr<IMbnConnectionProfileManager> pCntProfileMnr = NULL;
- result = CoCreateInstance(CLSID_MbnConnectionProfileManager, NULL, CLSCTX_ALL, IID_IMbnConnectionProfileManager,
- (LPVOID*)&pCntProfileMnr);
- if(FAILED(result))
- {
- SetInteralError(result);
- Dbg("*** ERROR: Failed to Initialize IMbnConnectionProfileManager with code 0x%08x", result);
- return FALSE;
- }
- SAFEARRAY* cntProfiles = NULL;
- ATL::CComPtr<IMbnInterface> pInterface = NULL;
- if((pInterface = GetInterfaceEx(bSpecified? pInfo->csInterfaceId : m_interfaceID)) == NULL) {
- Dbg("WARN: GetInterface failed !");
- return FALSE;
- }
- //{
- // BSTR bsInterfaceID;
- // if(SUCCEEDED(pInterface->get_InterfaceID(&bsInterfaceID))) {
- // CSimpleStringA csInterfaceID = ConvertToString(bsInterfaceID);
- // Dbg("the id: %s", (LPCTSTR)csInterfaceID);
- // }
- //}
- result = pCntProfileMnr->GetConnectionProfiles(pInterface, &cntProfiles);
- if(FAILED(result))
- {
- SetInteralError(result);
- if(result == HRESULT_FROM_WIN32(ERROR_NOT_FOUND)) {
- SetTmpState(MBD_NO_PROFILE);
- Dbg("***ERROR: GetConnectionProfiles >>> cannot found any profiles maybe is 3g dongle");
- }
- else if(result == E_HANDLE) {
- Dbg("***ERROR: GetConnectionProfiles The interface is invalid 0x%08x", result);
- }
- else if(result == HRESULT_FROM_WIN32(ERROR_NOT_READY)) {
- Dbg("Unable to obtain the subscriber ID because the device is not MBN_READY_STATE_INITIALIZED");
- }
- else {
- Dbg("***ERROR: GetConnectionProfiles with code 0x%08x", result);
- }
- return FALSE;
- }
- LONG lBound, hBound;
- result = SafeArrayGetLBound(cntProfiles, 1, &lBound);
- if(FAILED(result))
- {
- SetInteralError(result);
- Dbg("***ERROR: Failed to retrieve First profile with code 0x%08x", result);
- return FALSE;
- }
- result = SafeArrayGetUBound(cntProfiles, 1, &hBound);
- if(FAILED(result))
- {
- SetInteralError(result);
- Dbg("***ERROR: Failed to retrieve End profile with code 0x%08x", result);
- return FALSE;
- }
- if(lBound > hBound) {
- result = HRESULT_FROM_WIN32(ERROR_NOT_FOUND);
- Dbg("***Warning: There are no profile been found !!!");
- SetTmpState(MBD_NO_PROFILE);
- return FALSE;
- }
- Dbg("profile bound: %d, %d", lBound, hBound);
- MBNProfileList tmpList;
- tmpList.Init(hBound - lBound + 1 );
- int start=0;
- ATL::CComPtr<IMbnConnectionProfile> pCntProfile = NULL;
- BOOL bFouned = FALSE;
- SetTmpState(MBD_NO_PROFILE);
- for(LONG index=lBound; index<=hBound; index++)
- {
- int cur = index - lBound;
- pCntProfile = NULL;
- result = SafeArrayGetElement(cntProfiles, &index, (void*)(&pCntProfile));
- if(FAILED(result))
- {
- SetInteralError(result);
- Dbg("SafeArrayGetElement for index#%d failed: 0x%X", cur, result);
- continue;
- }
- const ptMbnProfileInfo pCurPf = &(tmpList[start]);
- InitAccessKeyInfo(&pCurPf->AccessInfo);
- pCurPf->AccessInfo.usCntType = CNT_MBN;
- BSTR strProfile;
- result = pCntProfile->GetProfileXmlData(&strProfile);
- if(SUCCEEDED(result))
- {
- m_csXmlData = ConvertToString(strProfile);
- FreeBString(strProfile);
- //Dbg("****************************************");
- //Dbg("%s", (LPCTSTR)m_csXmlData);
- //Dbg("****************************************");
- tinyxml2::XMLDocument xmlData;
- if(xmlData.Parse(m_csXmlData) != tinyxml2::XML_SUCCESS) {
- Dbg("ERROR: xml parse failed: %d", xmlData.ErrorID());
- SetInteralError((LRESULT)xmlData.ErrorID());
- } else {
- tinyxml2::XMLElement* root = NULL;
- BOOL bExt = FALSE;
- if((root = xmlData.FirstChildElement("MBNProfileExt")) == NULL) {
- root = xmlData.FirstChildElement("MBNProfile");
- } else {
- bExt = TRUE;
- }
- if(root != NULL) {
- if(root->FirstChildElement("SubscriberID") != NULL) {
- if(pInfo->subInfo.strIMSI.GetLength() > 0) {
- CSimpleStringA strIMSI = root->FirstChildElement("SubscriberID")->GetText();
- if(strIMSI.Compare(pInfo->subInfo.strIMSI)) {
- Dbg("profile[%d]: Ignore it: [%s] vs [%s]", cur, (LPCTSTR)pInfo->subInfo.strIMSI, (LPCTSTR)strIMSI);
- SetTmpState(MBD_NO_PROFILE);
- continue;
- }
- }
- }
- if(root->FirstChildElement("ProfileCreationType") != NULL) {
- const char * defineType = root->FirstChildElement("ProfileCreationType")->GetText();
- if(!_strnicmp(defineType, "UserProvisioned", strlen("UserProvisioned"))) {
- pCurPf->bUserDefined = true;
- } else {
- pCurPf->bUserDefined = false;
- Dbg("profile[%d]: Ignore it: ProfileCreationType: %s", cur, defineType);
- SetTmpState(MBD_NO_PROFILE);
- continue;
- }
- }
- if(root->FirstChildElement("Name") != NULL) {
- pCurPf->strProfileName = root->FirstChildElement("Name")->GetText();
- Dbg("profile[%d]: profileName:%s", cur, (LPCTSTR)pCurPf->strProfileName);
- }
- if(root->FirstChildElement( "Description" ) != NULL) {
- pCurPf->AccessInfo.strProfileDesc = root->FirstChildElement( "Description" )->GetText();
- } else if(!bExt) {
- pCurPf->AccessInfo.strProfileDesc = pCurPf->strProfileName;
- }
- if(root->FirstChildElement("IsDefault") != NULL) {
- CSimpleStringA csDefault = root->FirstChildElement("IsDefault")->GetText();
- if(!csDefault.Compare("true", true)) {
- pCurPf->AccessInfo.IsDefault = TRUE;
- }
- }
- //Win10: 拨号的文件一定要处于“已应用”的状态才能成功拨号。。
- if(!bSpecified) {
- if(pCurPf->bUserDefined) {
- m_csProfileName = pCurPf->strProfileName;
- Dbg("Get ProfileNameEx[%s] suc.", (LPCTSTR)m_csProfileName);
- bFouned = TRUE;
- break;
- }
- else {
- Dbg("ProfileNameEx[%d]: [%s]", cur, (LPCTSTR)pCurPf->strProfileName);
- }
- }
- //Dbg("serious problem?");
- tinyxml2::XMLElement* context = root->FirstChildElement("Context");
- if(context != NULL) {
- //Dbg("serious context?");
- if(context->FirstChildElement("AuthProtocol") != NULL)
- pCurPf->AccessInfo.strAuthProtocol = context->FirstChildElement("AuthProtocol")->GetText();
- if(context->FirstChildElement("IPType") != NULL)
- pCurPf->AccessInfo.strIPType = context->FirstChildElement("IPType")->GetText();
- if(context->FirstChildElement("AccessString") != NULL)
- pCurPf->AccessInfo.strAccessString = context->FirstChildElement("AccessString")->GetText();
- tinyxml2::XMLElement* usercred = context->FirstChildElement("UserLogonCred");
- if(usercred != NULL) {
- //Dbg("serious usercred?");
- tinyxml2::XMLElement* username = usercred->FirstChildElement("UserName");
- if(username != NULL) {
- pCurPf->AccessInfo.strUserName = username->GetText();
- }
- tinyxml2::XMLElement* password = usercred->FirstChildElement("Password");
- if(password != NULL) {
- pCurPf->AccessInfo.strPassword = password->GetText();
- Dbg("Password: %s", (LPCTSTR)pCurPf->AccessInfo.strPassword);
- } else {
- CSmartPointer<IConfigInfo> spConfigRun;
- m_pFSM->GetEntityBase()->GetFunction()->OpenConfig(Config_Run, spConfigRun);
- CSimpleStringA csValue;
- if(Error_Succeed == spConfigRun->ReadConfigValue(pCurPf->strProfileName, "Status", csValue)
- && csValue.GetLength() > 0) {
- if(csValue.IsStartWith("Invalid")) {
- Dbg("the runcfg record for %s is invalid : %s", (LPCTSTR)pCurPf->strProfileName, (LPCTSTR)csValue);
- pCurPf->AccessInfo.strPassword = DEFAULT_PASSWORD_CHECK;
- } else {
- #ifdef USING_ENCRYPT
- spConfigRun->ReadConfigValue(pCurPf->strProfileName, "Password", csValue);
- pCurPf->AccessInfo.strPassword = DecryptString(csValue);
- #else
- spConfigRun->ReadConfigValue(pCurPf->strProfileName, "Password", pCurPf->AccessInfo.strPassword);
- #endif
- if(!bExt) {
- spConfigRun->ReadConfigValue(pCurPf->strProfileName, "FriendlyName", csValue);
- if(!csValue.IsNullOrEmpty()) {
- pCurPf->AccessInfo.strProfileDesc = csValue;
- Dbg("Get the friendlyName from runcfg: %s", (LPCTSTR)pCurPf->AccessInfo.strProfileDesc);
- }
- }
- }
- }else {
- Dbg("runcfg is not exist mbn record for %s", (LPCTSTR)pCurPf->strProfileName);
- pCurPf->AccessInfo.strPassword = DEFAULT_PASSWORD_CHECK;
- }
- }
- }
- }
- start++;
- }
- }
- } else {
- SetInteralError(result);
- Dbg("Invoke GetProfileXmlData failed return 0x%x", result);
- }
- }
- Dbg("Finally get valid profile's count: %d", start);
- if(bSpecified && start > 0) {
- pInfo->profileList.Init(start);
- pInfo->profileList.Copy(tmpList, 0, start);
- }
- tmpList.Clear();
- if(cntProfiles) {
- SafeArrayDestroy(cntProfiles);
- cntProfiles = NULL;
- }
- if((bSpecified && start > 0) || bFouned) {
- return TRUE;
- }
- return FALSE;
- }
- BOOL CMbnImplHelper::Connect()
- {
- LOG_FUNCTION();
-
- m_bDialFromDialog = FALSE;
- if(!GetConnectState()) {
- Dbg("Get ConnectState failed.");
- return FALSE;
- }
- if(m_cntState != MBN_ACTIVATION_STATE_DEACTIVATED) {
- Dbg(">>>Connect interrupt because %s", ConnectStateToString(m_cntState));
- if(m_cntState == MBN_ACTIVATION_STATE_ACTIVATED || m_cntState == MBN_ACTIVATION_STATE_ACTIVATING) {
- Dbg("Due to CntStatus, connect suc previously !!!");
- return TRUE;
- }
- } else {
- Dbg("Try to Connecting...");
- HRESULT result = S_OK;
- ATL::CComPtr<IMbnInterface> pInterface = GetInterfaceEx(m_interfaceID);
- if(pInterface == NULL) {
- SetStatusFromTmp2Main();
- return FALSE;
- }
- ATL::CComPtr<IMbnConnection> pConnection = NULL;
- result = pInterface->GetConnection(&pConnection);
- if(FAILED(result)) {
- SetInteralError(result);
- SetStatusFromTmp2Main();
- Dbg("***ERROR: Failed to retrieve mbnConnection with code 0x%08x", result);
- return FALSE;
- }
- if(!SearchForProfileName()) {
- SetStatusFromTmp2Main();
- return FALSE;
- }
- ULONG requestID;
- result = pConnection->Connect(MBN_CONNECTION_MODE_PROFILE,
- (LPCWSTR)CSimpleStringA2W(m_csProfileName), &requestID);
- if(SUCCEEDED(result)) {
- Dbg("Connect successfully with ProfileName[%s][%u]", (LPCTSTR)m_csProfileName, requestID);
- m_ulTempID = requestID;
- //等待监听结果或者直接调用GetConnectionState函数
- SetCurState(MBD_ACTIVATION_CONNECTING);
- return TRUE;
- } else {
- Dbg("Connect failed with profileName[%s]", (LPCTSTR)m_csProfileName);
- SetCurState(MBD_CONNECTION_FAILURE);
- switch(result)
- {
- case E_HANDLE :
- {
- Dbg("***ERROR: Invalid interface. Most likely the device has been removed from the system.");
- ClearCurInterface();
- }
- break;
- case E_INVALIDARG :
- {//hardly.
- Dbg("***ERROR: An invalid profile name was specified or the argument is not compliant to XML schema.");
- }
- break;
- case E_MBN_MAX_ACTIVATED_CONTEXTS:
- {
- SetCurState(MBD_CONNECTION_MAX_CONTEXTS);
- Dbg("***ERROR: There is already an active context. Multiple active contexts are not supported.");
- }
- break;
- default:
- if(result == HRESULT_FROM_WIN32(ERROR_SERVICE_NOT_ACTIVE)) {
- Dbg("***ERROR: The Mobile Broadband service is not running on this system.");
- } else if(result == HRESULT_FROM_WIN32(ERROR_NOT_FOUND)) {
- Dbg("***ERROR: Invalid interface. Most likely the device has been removed from the system.");
- ClearCurInterface();
- } else {
- Dbg("***ERROR: Connect failed with ProfileName[%s] return 0x%x", (LPCTSTR)m_csProfileName, result);
- }
- break;
- }
- }
- }
- return FALSE;
- }
- ULONG CMbnImplHelper::ConnectEx2(LPCTSTR lpcszInterface, LPCTSTR lpcszProfileName)
- {
- LOG_FUNCTION();
- if(!GetConnectState()) {
- Dbg("Get ConnectState failed.");
- m_pFSM->m_csErrMsg = "获取网卡连接状态失败";
- return -1;
- }
- if(m_cntState != MBN_ACTIVATION_STATE_DEACTIVATED) {
- Dbg(">>>Connect interrupt because %s", ConnectStateToString(m_cntState));
- if(m_cntState == MBN_ACTIVATION_STATE_ACTIVATED || m_cntState == MBN_ACTIVATION_STATE_ACTIVATING) {
- Dbg("Due to CntStatus, connect suc previously !!!");
- return -1;
- }
- } else {
- Dbg("Try to Connecting...");
- HRESULT result = S_OK;
- ATL::CComPtr<IMbnConnection> pConnection = NULL;
- ATL::CComPtr<IMbnInterface> pInterface;
- if(!GetInterface(lpcszInterface)) {
- Dbg("WARN: GetInterface failed !");
- m_pFSM->m_csErrMsg = "获取网卡接口句柄失败";
- SetStatusFromTmp2Main();
- return -1;
- }
- pInterface = m_pTmpInterface;
- result = pInterface->GetConnection(&pConnection);
- if(FAILED(result)) {
- //Can ignore hardly happen
- m_pFSM->m_csErrMsg = "获取网卡连接句柄失败";
- SetInteralError(result);
- SetStatusFromTmp2Main();
- Dbg("***ERROR: Failed to retrieve mbnConnection with code 0x%08x", result);
- return -1;
- }
- ULONG requestID;
- CSimpleStringA csProfileName(lpcszProfileName);
- result = pConnection->Connect(MBN_CONNECTION_MODE_PROFILE,
- (LPCWSTR)CSimpleStringA2W(csProfileName), &requestID);
- if(SUCCEEDED(result)) {
- Dbg("ConnectEx successfully with ProfileName[%s][%u]", (LPCTSTR)csProfileName, requestID);
- m_ulTempID = requestID;
- m_bDialFromDialog = TRUE;
- SetCurState(MBD_ACTIVATION_CONNECTING);
- return requestID;
- } else {
- SetCurState(MBD_CONNECTION_FAILURE);
- switch(result) {
- case E_HANDLE :
- {
- m_pFSM->m_csErrMsg = "无效接口";
- Dbg("***ERROR: Invalid interface. Most likely the device has been removed from the system.");
- }
- break;
- case E_INVALIDARG :
- {
- m_pFSM->m_csErrMsg = "无效的连接配置文件";
- Dbg("***ERROR: An invalid profile name was specified or the argument is not compliant to XML schema.");
- }
- break;
- case E_MBN_MAX_ACTIVATED_CONTEXTS:
- {
- SetCurState(MBD_CONNECTION_MAX_CONTEXTS);
- m_pFSM->m_csErrMsg = "不支持多个激活上下文";
- Dbg("***ERROR: There is already an active context. Multiple active contexts are not supported.");
- }
- break;
- default:
- m_pFSM->m_csErrMsg = CSimpleStringA::Format("MBN拨号连接失败:0x%X", result);
- if(result == HRESULT_FROM_WIN32(ERROR_SERVICE_NOT_ACTIVE)) {
- Dbg("***ERROR: The Mobile Broadband service is not running on this system.");
- }
- else if(result == HRESULT_FROM_WIN32(ERROR_NOT_FOUND)) {
- Dbg("***ERROR: Invalid interface. Most likely the device has been removed from the system.");
- }
- else {
- Dbg("***ERROR: Connect failed with ProfileName[%s] return 0x%x", (LPCTSTR)csProfileName, result);
- }
- break;
- }
- }
- }
- return -1;
- }
- BOOL CMbnImplHelper::ConnectEx(LPCTSTR lpcszInterface, LPCTSTR lpcszProfileName)
- {
- LOG_FUNCTION();
- if(!GetConnectState()) {
- Dbg("Get ConnectState failed.");
- m_pFSM->m_csErrMsg = "获取网卡连接状态失败";
- return FALSE;
- }
- if(m_cntState != MBN_ACTIVATION_STATE_DEACTIVATED) {
- Dbg(">>>Connect interrupt because %s", ConnectStateToString(m_cntState));
- if(m_cntState == MBN_ACTIVATION_STATE_ACTIVATED || m_cntState == MBN_ACTIVATION_STATE_ACTIVATING) {
- Dbg("Due to CntStatus, connect suc previously !!!");
- return TRUE;
- }
- } else {
- Dbg("Try to Connecting...");
- HRESULT result = S_OK;
- ATL::CComPtr<IMbnConnection> pConnection = NULL;
- ATL::CComPtr<IMbnInterface> pInterface;
- if(!GetInterface(lpcszInterface)) {
- Dbg("WARN: GetInterface failed !");
- m_pFSM->m_csErrMsg = "获取网卡接口句柄失败";
- SetStatusFromTmp2Main();
- return FALSE;
- }
- pInterface = m_pTmpInterface;
- result = pInterface->GetConnection(&pConnection);
- if(FAILED(result)) {
- //Can ignore hardly happen
- m_pFSM->m_csErrMsg = "获取网卡连接句柄失败";
- SetInteralError(result);
- SetStatusFromTmp2Main();
- Dbg("***ERROR: Failed to retrieve mbnConnection with code 0x%08x", result);
- return FALSE;
- }
- ULONG requestID;
- CSimpleStringA csProfileName(lpcszProfileName);
- result = pConnection->Connect(MBN_CONNECTION_MODE_PROFILE,
- (LPCWSTR)CSimpleStringA2W(csProfileName), &requestID);
- if(SUCCEEDED(result)) {
- Dbg("ConnectEx successfully with ProfileName[%s][%u]", (LPCTSTR)csProfileName, requestID);
- m_ulTempID = requestID;
- m_bDialFromDialog = TRUE;
- SetCurState(MBD_ACTIVATION_CONNECTING);
- return TRUE;
- } else {
- SetCurState(MBD_CONNECTION_FAILURE);
- switch(result) {
- case E_HANDLE :
- {
- m_pFSM->m_csErrMsg = "无效接口";
- Dbg("***ERROR: Invalid interface. Most likely the device has been removed from the system.");
- }
- break;
- case E_INVALIDARG :
- {
- m_pFSM->m_csErrMsg = "无效的连接配置文件";
- Dbg("***ERROR: An invalid profile name was specified or the argument is not compliant to XML schema.");
- }
- break;
- case E_MBN_MAX_ACTIVATED_CONTEXTS:
- {
- SetCurState(MBD_CONNECTION_MAX_CONTEXTS);
- m_pFSM->m_csErrMsg = "不支持多个激活上下文";
- Dbg("***ERROR: There is already an active context. Multiple active contexts are not supported.");
- }
- break;
- default:
- m_pFSM->m_csErrMsg = CSimpleStringA::Format("MBN拨号连接失败:0x%X", result);
- if(result == HRESULT_FROM_WIN32(ERROR_SERVICE_NOT_ACTIVE)) {
- Dbg("***ERROR: The Mobile Broadband service is not running on this system.");
- }
- else if(result == HRESULT_FROM_WIN32(ERROR_NOT_FOUND)) {
- Dbg("***ERROR: Invalid interface. Most likely the device has been removed from the system.");
- }
- else {
- Dbg("***ERROR: Connect failed with ProfileName[%s] return 0x%x", (LPCTSTR)csProfileName, result);
- }
- break;
- }
- }
- }
- return FALSE;
- }
- BOOL CMbnImplHelper::DisConnect(BOOL bManual, LPCTSTR lpszInterface)
- {
- m_bDialFromDialog = bManual;
- Dbg("Set m_bDialFromDialog with %d", m_bDialFromDialog);
- if(!GetConnectState()) {
- Dbg("Get ConnectState failed.");
- m_pFSM->m_csErrMsg = "获取连接状态失败";
- return FALSE;
- }
- if(m_cntState != MBN_ACTIVATION_STATE_ACTIVATED) {
- Dbg(">>>Connect interrupt because %s", ConnectStateToString(m_cntState));
- m_pFSM->m_csErrMsg = ConnectStateToString(m_cntState);
- } else {
- m_ulTempID = (ULONG)-1;
- if(m_ulRequestID == ULONG(-1)) {
- Dbg("The request ID is invalid, most likely connect manully before.");
- m_pFSM->m_csErrMsg = "拨号请求ID丢失,可能此次连接并不是由本应用发起的";
- if(!m_pFSM->GetPersistentRequestId(m_ulTempID)) {
- return FALSE;
- }
- m_ulRequestID = m_ulTempID;
- }
- assert(!bManual || !m_interfaceID.Compare(lpszInterface));
- ATL::CComPtr<IMbnInterface> pInterface = GetInterfaceEx(m_interfaceID);
- if(pInterface == NULL) {
- return FALSE;
- }
- HRESULT result = S_OK;
- ATL::CComPtr<IMbnConnection> pConnection = NULL;
- result = pInterface->GetConnection(&pConnection);
- if(FAILED(result)) {
- //Can igonre because hardly happen.
- m_pFSM->m_csErrMsg = "获取网卡连接句柄失败";
- Dbg("***ERROR: Failed to retrieve mbnConnection with code 0x%08x", result);
- SetInteralError(result);
- return FALSE;
- }
- result = pConnection->Disconnect(&m_ulRequestID);
- if(FAILED(result))
- {
- if(m_ulTempID != (ULONG)-1) {
- m_pFSM->m_csErrMsg = CSimpleStringA::Format(
- "拨号请求ID丢失,可能此次连接并不是由本应用发起的:0x%X", result);
- m_ulRequestID = (ULONG)-1;
- return FALSE;
- }
- //Can igonre because hardly happen.
- m_pFSM->m_csErrMsg = CSimpleStringA::Format("0x%X", result);
- SetInteralError(result);
- if(result == HRESULT_FROM_WIN32(ERROR_NOT_FOUND)) {
- m_pFSM->m_csErrMsg.Clear();
- Dbg("***Warning: The connection has already been disconnected.");
- return TRUE;
- } else {
- Dbg("Disconnect with [RequestID#%d] failed return 0x%x", m_ulRequestID, result);
- }
- }
- else {
- m_pFSM->m_csErrMsg.Clear();
- Dbg("Disconnect suc.");
- SetCurState(MBD_ACTIVATION_DISCONNECTING);
- return TRUE;
- }
- }
- return FALSE;
- }
- void CMbnImplHelper::OnConnectComplete(ULONG requestID, LPCTSTR newInterface, HRESULT status)
- {
- #ifdef TWINKLE_LOCAL_DEBUG
- #define Dbg(fmt, ...) \
- TwDbg(fmt, ##__VA_ARGS__); \
- printf(fmt "\n", ##__VA_ARGS__)
- #endif
- Dbg(">>>OnConnectionComplete: newInterface: %s, requestID: %d status 0x%08x", newInterface, requestID, status);
- int nFormattedCode = 0;
- BOOL exceptConnectActionFlag = FALSE;
- if(requestID == 0)
- {
- Dbg("New condition: m_interfaceID[%s] newInterface[%s]", (LPCTSTR)m_interfaceID, newInterface);
- if(!m_interfaceID.IsNullOrEmpty() && m_interfaceID.Compare(newInterface) == 0 && status == S_OK) {
- exceptConnectActionFlag = TRUE;
- } else {
- return;
- }
- }
- FSMEvent* fsmEvent = new FSMEvent(USER_EVT_CONNECT_COMPLETE);
- if(GetCurState() == MBD_ACTIVATION_CONNECTING && requestID == m_ulTempID)
- {
- Dbg("Receive the specified ConnectComplete Event.");
- fsmEvent->param2 = 0;
- }
- else if(m_bSelected && m_interfaceID == newInterface)
- {
- Dbg("Most likely Connect manually.");
- fsmEvent->param2 = 1;
- }
- else
- {
- fsmEvent->param2 = -1;
- Dbg("Disgusting thing happen. Josephtest m_interfaceID[%s] m_ulRequestID[%d]",
- (LPCTSTR)m_interfaceID, m_ulRequestID);
- if(!exceptConnectActionFlag) {
- Dbg("Set it as the new king...");
- SetCurInterface(newInterface);
- }
- }
- if(status == S_OK)
- {
- //连接成功
- fsmEvent->param1 = 0;
- SetRequestID(requestID);
- // m_ulRequestID = requestID;
- m_ulTempID = (ULONG)-1;
- SetCurState(MBD_ACTIVATION_CONNECTED);
- }
- else
- {
- //连接失败
- fsmEvent->param1 = 1;
- m_ulTempID = (ULONG)-1;
- if(fsmEvent->param2 == 0) {
- //清理掉最近拨号成功的记录
- m_pFSM->EraseTheRecentInfo(requestID);
- }
- if(status == E_MBN_SIM_NOT_INSERTED)
- {
- SetCurState(MBD_READY_SIM_NO_INSERT);
- }
- else if(status == E_MBN_RADIO_POWER_OFF)
- {
- SetCurState(MBD_POWER_OFF);
- }
- else if(status == E_MBN_MAX_ACTIVATED_CONTEXTS)
- {
- SetCurState(MBD_CONNECTION_MAX_CONTEXTS);
- }
- else if(status == E_MBN_INVALID_ACCESS_STRING)
- {
- SetCurState(MBD_CONNECTION_INVALID_ACCESS);
- }
- else if(status == E_MBN_PIN_REQUIRED)
- {
- SetCurState(MBD_CONNECTION_PIN_REQUIRED);
- }
- //TODO: delete "|| status == E_MBN_FAILURE" ?? 2016-9-23
- // Eventually Cancel it. [Josephus in 7:54:05 2016/9/28]
- else if(status == HRESULT_FROM_WIN32(ERROR_INVALID_PASSWORD)/* || status == E_MBN_FAILURE*/)
- {
- SetCurState(MBD_CONNECTION_INVALID_NAMEORPASSWORD);
- }
- else
- {
- SetMBNCode(status);
- SetCurState(MBD_CONNECTION_FAILURE);
- Dbg("Connection Complete return error status 0x08%x", status);
- }
- }
- if(m_bDialFromDialog) {
- m_bDialFromDialog = FALSE;
- if(status == S_OK) {
- m_pFSM->TellDlgTheResult(Error_Succeed, WM_DIAL_RESULT, 4);
- m_pFSM->m_csErrMsg.Clear();
- Dbg("Confirm connection is succ, reset current interface.");
- SetCurInterface(newInterface);
- } else {
- m_pFSM->m_csErrMsg = CSimpleStringA::Format("0x%X", status);
- m_pFSM->TellDlgTheResult(Error_FailVerify, WM_DIAL_RESULT, 4);
- }
- } else {
- m_pFSM->TellDlgTheResult(status == S_OK ? Error_Succeed : Error_FailVerify, WM_DIAL_RESULT, -4);
- }
- m_pFSM->PostEventLIFO(fsmEvent);
- #ifdef TWINKLE_LOCAL_DEBUG
- #undef Dbg
- #endif
- }
- void CMbnImplHelper::OnDisconnectComplete(ULONG requestID, LPCTSTR newInterface)
- {
- Dbg(">>>OnDisconnectComplete: requestID: %d", requestID);
- if(requestID == 0 && m_interfaceID.Compare(newInterface) != 0)
- {
- Dbg("New condition: m_interfaceID[%s] newInterface[%s]", (LPCTSTR)m_interfaceID, newInterface);
- return;
- }
- if(m_bDialFromDialog) {
- m_bDialFromDialog = FALSE;
- m_pFSM->TellDlgTheResult(Error_Succeed, WM_DIAL_DISCONNECT, 1);
- } else {
- m_pFSM->TellDlgTheResult(Error_Succeed, WM_DIAL_DISCONNECT, 0);
- }
-
- if(requestID == m_ulRequestID)
- {
- Dbg("Receive the specified DisconnectComplete Event.");
- SetRequestID();
- SetCurState(MBD_ACTIVATION_DISCONNECTED);
- FSMEvent* fsmEvent = new FSMEvent(USER_EVT_DISCONNECT_COMPLETE);
- fsmEvent->param1 = (int)requestID;
- fsmEvent->param2 = ADVANCE_MBN;
- m_pFSM->PostEventLIFO(fsmEvent);
- return;
- }
- else if(m_interfaceID == newInterface)
- {
- Dbg("Detect it connected manully before InterfaceID[%s].", newInterface);
- SetCurState(MBD_ACTIVATION_DISCONNECTED);
- FSMEvent* fsmEvent = new FSMEvent(USER_EVT_DISCONNECT_COMPLETE);
- fsmEvent->param1 = (int)requestID;
- fsmEvent->param2 = ADVANCE_MBN;
- m_pFSM->PostEventLIFO(fsmEvent);
- return;
- }
- Dbg("Disgusting thing happen, bSelected[%d] newInterface[%s]", m_bSelected, newInterface);
- }
- void CMbnImplHelper::OnConnectStateChange(HRESULT hr, MBN_ACTIVATION_STATE state, LPCTSTR newInterface)
- {
- if(m_bSelected && m_interfaceID == newInterface)
- {
- if(FAILED(hr))
- {
- if(hr == E_MBN_SIM_NOT_INSERTED)
- {
- Dbg("***OnConnectStateChange: A SIM is not inserted in the device.");
- SetCurState(MBD_READY_SIM_NO_INSERT);
- }
- else if(hr == E_MBN_PIN_REQUIRED)
- {
- Dbg("***OnConnectStateChange: A PIN is required to get the call state.");
- SetCurState(MBD_CONNECTION_PIN_REQUIRED);
- }
- else if(hr == E_MBN_BAD_SIM)
- {
- Dbg("***OnConnectStateChange: A bad SIM is inserted in the device.");
- SetCurState(MBD_READY_BAD_SIM);
- }
- else if(hr == E_PENDING)
- {
- //SetCurState(MBD_UNKNOWN);
- Dbg("***OnConnectStateChange: The activation state not available");
- }
- }
- else
- {
- SetCurState((MOBILEDIAL_CONNECTION_STATE)((int)MBD_ACTIVATION_NONE + state));
- Dbg("***OnConnectStateChange: %s", ConnectStateToString(state));
- }
- }
- }
- void CMbnImplHelper::OnConnectionArrival(LPCTSTR newInterface)
- {
- Dbg(">>>OnConnectionArrival: %s", newInterface);
- if(!CheckAndRefleshCurInterface()){
- //Sleep(2500);
- if(SetCurInterface(newInterface)){
- if(m_pFSM){
- FSMEvent* fsmEvent = new FSMEvent(USER_EVT_DEVICE_ARRIVAL);
- fsmEvent->param1 = 1;
- fsmEvent->param2 = 4;
- m_pFSM->PostEventFIFO(fsmEvent);
- }
- }
- }
- m_pFSM->TellDlgTheResult(Error_Succeed, WM_DONGLE_CHANGE, 1);
- }
- void CMbnImplHelper::OnConnectionRemoval(LPCTSTR newInterface)
- {
- Dbg(">>>OnConnectionRemoval: %s", newInterface);
- if(m_bSelected && m_interfaceID == newInterface)
- {
- Dbg("specified interface remove");
- ClearCurInterface();
- if(m_pFSM)
- {
- FSMEvent* fsmEvent = new FSMEvent(USER_EVT_DEVICE_LEAVE);
- fsmEvent->param1 = 1;
- fsmEvent->param2 = 4;
- m_pFSM->PostEventLIFO(fsmEvent);
- }
- }
- m_pFSM->TellDlgTheResult(Error_Succeed, WM_DONGLE_CHANGE, 0);
- }
- BOOL CMbnImplHelper::IsTheDealProfileEvents(IMbnConnectionProfile *profile, BOOL bSucc)
- {
- BSTR strProfile;
- HRESULT result = profile->GetProfileXmlData(&strProfile);
- if(SUCCEEDED(result)) {
- CSimpleStringA content = ConvertToString(strProfile);
- FreeBString(strProfile);
- tinyxml2::XMLDocument xmlData;
- if(xmlData.Parse(content) != tinyxml2::XML_SUCCESS) {
- Dbg("ERROR: xml parse failed: %d", xmlData.ErrorID());
- } else {
- tinyxml2::XMLElement* root = NULL;
- BOOL bExt = FALSE;
- if((root = xmlData.FirstChildElement("MBNProfileExt")) == NULL) {
- root = xmlData.FirstChildElement("MBNProfile");
- } else {
- bExt = TRUE;
- }
- if(root != NULL) {
- CSimpleStringA csProfileName = root->FirstChildElement( "Name" )->GetText();
- if(m_csTmpProfileName.GetLength() > 0 && !csProfileName.Compare(m_csTmpProfileName)) {
- if(!bSucc) {
- CSmartPointer<IConfigInfo> spConfigRun;
- m_pFSM->GetEntityBase()->GetFunction()->OpenConfig(Config_Run, spConfigRun);
- CSimpleStringA csAction;
- if(Error_Succeed == spConfigRun->ReadConfigValue(m_csTmpProfileName, "Status", csAction)
- && csAction.GetLength() > 0) {
- CSimpleStringA csNewAction("Invalid-");
- csNewAction += csAction;
- spConfigRun->WriteConfigValue(m_csTmpProfileName, "Status", csNewAction);
- }
- }
- m_csTmpProfileName = "";
- return TRUE;
- }else {
- CSmartPointer<IConfigInfo> spConfigRun;
- m_pFSM->GetEntityBase()->GetFunction()->OpenConfig(Config_Run, spConfigRun);
- CSimpleStringA csAction;
- if(Error_Succeed == spConfigRun->ReadConfigValue(csProfileName, "Status", csAction)
- && csAction.GetLength() > 0) {
- CSimpleStringA csNewAction("Invalid-OutChanged");
- spConfigRun->WriteConfigValue(csProfileName, "Status", csNewAction);
- }
- Dbg("Maybe add/delete/update profile from system: (%s)(%s)", (LPCTSTR)m_csTmpProfileName, (LPCTSTR)csProfileName);
- }
- }
- }
- }
- return FALSE;
- }
- void CMbnImplHelper::OnConnectionProfileArrival(IMbnConnectionProfile *newConnectionProfile)
- {
- Dbg(">>>OnConnectionProfileArrival !");
- if(IsTheDealProfileEvents(newConnectionProfile, TRUE)) {
- Dbg("Add new profile succ !");
- m_pFSM->TellDlgTheResult(Error_Succeed, WM_ADD_PROFILE_RESULT, 0);
- return;
- }
- m_pFSM->TellDlgTheResult(Error_Succeed, WM_ADD_PROFILE_RESULT, 1);
- }
- void CMbnImplHelper::OnConnectionProfileRemoval(IMbnConnectionProfile *oldConnectionProfile)
- {
- Dbg(">>>OnConnectionProfileRemoval !");
- if(IsTheDealProfileEvents(oldConnectionProfile, TRUE)) {
- Dbg("delete profile succ !");
- m_pFSM->TellDlgTheResult(Error_Succeed, WM_DELETE_PROFILE_RESULT, 0);
- return;
- }
- m_pFSM->TellDlgTheResult(Error_Succeed, WM_DELETE_PROFILE_RESULT, 1);
- }
- void CMbnImplHelper::OnProfileUpdate(IMbnConnectionProfile *newProfile)
- {
- Dbg(">>> OnProfileUpdate !");
- if(IsTheDealProfileEvents(newProfile, TRUE)) {
- Dbg("Update profile succ !");
- m_pFSM->TellDlgTheResult(Error_Succeed, WM_UPDATE_PROFILE_RESULT, 0);
- return;
- }
- m_pFSM->TellDlgTheResult(Error_Succeed, WM_UPDATE_PROFILE_RESULT, 1);
- }
- ErrorCodeEnum CMbnImplHelper::AddProfile(
- const CSimpleStringA& csInterfaceID, const AccessKeyInfo& newInfo, bool fApplyNow)
- {
- CSimpleStringW content;
- ErrorCodeEnum rc = _CreateProfile(csInterfaceID, newInfo, fApplyNow, false, content);
- if(rc == Error_Succeed) {
- //Add to profile manager
- HRESULT result = S_OK;
- ATL::CComPtr<IMbnConnectionProfileManager> pCntProfileMnr = NULL;
- result = CoCreateInstance(CLSID_MbnConnectionProfileManager, NULL, CLSCTX_ALL, IID_IMbnConnectionProfileManager,
- (LPVOID*)&pCntProfileMnr);
- if(FAILED(result))
- {
- SetInteralError(result);
- Dbg("*** ERROR: Failed to Initialize IMbnConnectionProfileManager with code 0x%08x", result);
- m_pFSM->m_csErrMsg = CSimpleStringA::Format("未知错误:0x%08x", result);
- return Error_NotInit;
- }
- CSimpleStringA csTmpProfileName(m_csTmpProfileName);
- Dbg("===========================================");
- Dbg("%s", (LPCTSTR)CSimpleStringW2A(content));
- Dbg("===========================================");
- //MessageBoxW(NULL, content, L"Content", MB_OK);
- result = pCntProfileMnr->CreateConnectionProfile(content);
- if(FAILED(result)) {
- if(result == HRESULT_FROM_WIN32(ERROR_ALREADY_EXISTS)) {
- Dbg("***ERROR: CreateConnectionProfile >>> A profile with the given name already exists.");
- m_pFSM->m_csErrMsg = "该配置已存在,请重命名配置名称!";
- } else if(result == E_MBN_INVALID_PROFILE) {
- Dbg("***ERROR: CreateConnectionProfile >>> The profile does not compact with the Mobile Broadband profile schema.");
- m_pFSM->m_csErrMsg = "配置文件有误,请检查卡托是否正常(当前网卡名称不应带\"Invalid\")!";
- } else if(result == E_MBN_DEFAULT_PROFILE_EXIST) {
- Dbg("***ERROR: CreateConnectionProfile >>> However the default profile already exists for the Mobile Broadband device.");
- m_pFSM->m_csErrMsg = "请求设置为默认配置,但已存在默认配置!";
- } else {
- Dbg("***ERROR: CreateConnectionProfile with code 0x%08x", result);
- m_pFSM->m_csErrMsg = CSimpleStringA::Format("未知错误:0x%08x", result);
- }
- return Error_Unexpect;
- }
- Dbg("The CreateConnectionProfile method completed successfully.");
- m_pFSM->m_csErrMsg = "";
- if(csTmpProfileName.GetLength() > 0)
- {
- CSmartPointer<IConfigInfo> spConfigRun;
- m_pFSM->GetEntityBase()->GetFunction()->OpenConfig(Config_Run, spConfigRun);
- spConfigRun->WriteConfigValue(csTmpProfileName, "Type", "MBN");
- spConfigRun->WriteConfigValue(csTmpProfileName, "Status", "Create");
- #ifdef USING_ENCRYPT
- CSimpleStringA csEValue = EncryptString(newInfo.strPassword);
- spConfigRun->WriteConfigValue(csTmpProfileName, "Password", csEValue);
- #else
- spConfigRun->WriteConfigValue(csTmpProfileName, "Password", newInfo.strPassword);
- #endif
- spConfigRun->WriteConfigValue(csTmpProfileName, "FriendlyName", newInfo.strProfileDesc);
- spConfigRun->WriteConfigValue(csTmpProfileName, "APN", newInfo.strAccessString);
- }
- if(!m_bCntPfMnrEventInited) {
- return Error_Succeed;
- }
- /*Microsoft just KengDie, the event somtimes not fires and actually the proile add action is not succ.*/
- /*The dongle is connected and we cannot enumrate the profile list with provided api, KAO!*/
- Dbg("pending to received add profile finished event!");
- return Error_Pending;
- }
- Dbg("_CreateProfile failed!");
- return Error_Unexpect;
- }
- ErrorCodeEnum CMbnImplHelper::_CreateProfile(
- const CSimpleStringA& csInterfaceID, const AccessKeyInfo& newInfo, bool fApplyNow,
- bool bUpdate, CSimpleStringW& csContent)
- {
- MbnDongleSubInfo subInfo;
- memset(&subInfo, 0, sizeof(MbnDongleSubInfo));
- if(!GetInterfaceInfo(csInterfaceID, &subInfo)) {
- Dbg("GetInterfaceInfo failed!");
- m_pFSM->m_csErrMsg = "获取网卡参数信息失败!";
- return Error_InvalidState;
- }
-
- tinyxml2::XMLDocument file;
- XMLDeclaration* declaration = file.NewDeclaration("xml version=\"1.0\"");
- CSimpleStringA strRootElement("MBNProfileExt");
- CSimpleStringA strNamespace("http://www.microsoft.com/networking/WWAN/profile/v4");
- BOOL bExt = FALSE;
- if(wv <= 7) {
- strNamespace = "http://www.microsoft.com/networking/WWAN/profile/v1";
- strRootElement = "MBNProfile";
- } else if(wv == 8) {
- strNamespace = "http://www.microsoft.com/networking/WWAN/profile/v1";
- strRootElement = "MBNProfile";
- } else if(wv >= 10) {
- bExt = TRUE;
- } else {
- Dbg("Cannot figure out Windows version: %d", wv);
- GetCurWindowsVersion();
- return Error_InvalidState;
- }
- file.InsertFirstChild(declaration);
- tinyxml2::XMLElement* root = file.NewElement(strRootElement);
- root->SetAttribute("xmlns", (LPCTSTR)strNamespace);
- file.InsertEndChild(root);
- if(!bUpdate) {
- m_csTmpProfileName = CreateGuidString(false);
- } else {
- assert(m_csTmpProfileName.GetLength() > 0);
- }
- XMLElement* Name = file.NewElement("Name");
- Name->SetText((LPCTSTR)m_csTmpProfileName);
- root->InsertEndChild(Name);
- if(bExt) {
- XMLElement* Description = file.NewElement("Description");
- Description->SetText((LPCTSTR)newInfo.strProfileDesc);
- root->InsertEndChild(Description);
- }
- XMLElement* IsDefault = file.NewElement("IsDefault");
- IsDefault->SetText(fApplyNow ? "true" : "false");
- root->InsertEndChild(IsDefault);
- /*if(!bUpdate)*/ {
- XMLElement* ProfileCreationType = file.NewElement("ProfileCreationType");
- ProfileCreationType->SetText("UserProvisioned");
- root->InsertEndChild(ProfileCreationType);
-
- XMLElement* SubscriberID = file.NewElement("SubscriberID");
- SubscriberID->SetText(subInfo.strIMSI);
- root->InsertEndChild(SubscriberID);
- XMLElement* SimIccID = file.NewElement("SimIccID");
- SimIccID->SetText(subInfo.strSimIccID);
- root->InsertEndChild(SimIccID);
- XMLElement* HomeProviderName = file.NewElement("HomeProviderName");
- HomeProviderName->SetText(subInfo.strProviderName);
- root->InsertEndChild(HomeProviderName);
- XMLElement* ConnectionMode = file.NewElement("ConnectionMode");
- if(bExt) {
- ConnectionMode->SetText("auto");
- } else {
- ConnectionMode->SetText("manual");
- }
- root->InsertEndChild(ConnectionMode);
- }
- XMLElement* Context = file.NewElement("Context");
- root->InsertEndChild(Context);
- XMLElement* AccessString = file.NewElement("AccessString");
- AccessString->SetText(newInfo.strAccessString);
- Context->InsertEndChild(AccessString);
- XMLElement* UserLogonCred = file.NewElement("UserLogonCred");
- Context->InsertEndChild(UserLogonCred);
- XMLElement* UserName = file.NewElement("UserName");
- UserName->SetText(newInfo.strUserName);
- UserLogonCred->InsertEndChild(UserName);
- if(newInfo.strPassword.GetLength() > 0) {
- XMLElement* Password = file.NewElement("Password");
- Password->SetText(newInfo.strPassword);
- UserLogonCred->InsertEndChild(Password);
- }
- XMLElement* Compression = file.NewElement("Compression");
- Compression->SetText("DISABLE");
- Context->InsertEndChild(Compression);
- XMLElement* AuthProtocol = file.NewElement("AuthProtocol");
- AuthProtocol->SetText(newInfo.strAuthProtocol);
- Context->InsertEndChild(AuthProtocol);
- if(bExt) {
- XMLElement* IPType = file.NewElement("IPType");
- IPType->SetText(newInfo.strIPType);
- Context->InsertEndChild(IPType);
- }
- if(bExt) {
- XMLElement* PurposeGroups = file.NewElement("PurposeGroups");
- root->InsertEndChild(PurposeGroups);
- XMLElement* PurposeGroupGuid = file.NewElement("PurposeGroupGuid");
- PurposeGroupGuid->SetText("{3E5545D2-1137-4DC8-A198-33F1C657515F}");
- PurposeGroups->InsertEndChild(PurposeGroupGuid);
- XMLElement* ProfileConditionedOn = file.NewElement("ProfileConditionedOn");
- root->InsertEndChild(ProfileConditionedOn);
- XMLElement* IMSI = file.NewElement("IMSI");
- IMSI->SetText(subInfo.strIMSI);
- ProfileConditionedOn->InsertEndChild(IMSI);
- XMLElement* IsExclusiveToOther = file.NewElement("IsExclusiveToOther");
- IsExclusiveToOther->SetText("true");
- root->InsertEndChild(IsExclusiveToOther);
- }
- else {
- XMLElement* DisplayProviderName = file.NewElement("DisplayProviderName");
- DisplayProviderName->SetAttribute("xmlns", "http://www.microsoft.com/networking/WWAN/profile/v2");
- DisplayProviderName->SetText(subInfo.strProviderName);
- root->InsertEndChild(DisplayProviderName);
- }
- //CSimpleStringA csGUID = CreateGuidString();
- //CSimpleStringA csXmlFile = "C:\\rvc\\Temp\\" + csGUID;
- //csXmlFile += ".xml";
- //file.SaveFile((LPCTSTR)csXmlFile);
- XMLPrinter printer;
- file.Print(&printer);
- csContent = CSimpleStringA2W(CSimpleStringA(printer.CStr()));
- if(csContent.GetLength() <= 0) {
- m_pFSM->m_csErrMsg = "生成配置文件内容失败!";
- return Error_IO;
- }
- return Error_Succeed;
- }
- ErrorCodeEnum CMbnImplHelper::UpdateProfile(const CSimpleStringA& csInterfaceID, LPCTSTR lpszProfileName, const AccessKeyInfo& newInfo, bool fApplyNow)
- {
- CSimpleStringW content;
- Dbg("Try to update profile[%s]", lpszProfileName);
- m_csTmpProfileName = CSimpleStringA(lpszProfileName);
- ErrorCodeEnum rc = _CreateProfile(csInterfaceID, newInfo, fApplyNow, true, content);
- if(rc == Error_Succeed) {
- rc = DealWithProfile(csInterfaceID, lpszProfileName, false, content);
- if(rc == Error_Succeed) {
- if(strlen(lpszProfileName) > 0)
- {
- CSmartPointer<IConfigInfo> spConfigRun;
- m_pFSM->GetEntityBase()->GetFunction()->OpenConfig(Config_Run, spConfigRun);
- spConfigRun->WriteConfigValue(lpszProfileName, "Status", "Update");
- spConfigRun->WriteConfigValue(lpszProfileName, "FriendlyName", newInfo.strProfileDesc);
- #ifdef USING_ENCRYPT
- CSimpleStringA csEValue = EncryptString(newInfo.strPassword);
- spConfigRun->WriteConfigValue(lpszProfileName, "Password", csEValue);
- #else
- spConfigRun->WriteConfigValue(lpszProfileName, "Password", newInfo.strPassword);
- #endif
- spConfigRun->WriteConfigValue(lpszProfileName, "APN", newInfo.strAccessString);
- }
- if(m_bCntPfEventInited) {
- Dbg("Wait for profile update event");
- return Error_Pending;
- }
- }
- return rc;
- }
- Dbg("_CreateProfile failed!");
- return Error_Unexpect;
- }
- ErrorCodeEnum CMbnImplHelper::DealWithProfile(
- const CSimpleStringA& csInterfaceID, LPCTSTR lpszProfileName, BOOL bDelete, const CSimpleStringW& csUpdateCtx)
- {
- HRESULT result = S_OK;
- ATL::CComPtr<IMbnInterface> pInterface = GetInterfaceEx(csInterfaceID);
- if(pInterface == NULL) {
- m_pFSM->m_csErrMsg = "获取网卡接口失败";
- return Error_Unexpect;
- }
- ATL::CComPtr<IMbnConnectionProfileManager> pCntProfileMnr = NULL;
- result = CoCreateInstance(CLSID_MbnConnectionProfileManager, NULL, CLSCTX_ALL, IID_IMbnConnectionProfileManager,
- (LPVOID*)&pCntProfileMnr);
- if(FAILED(result))
- {
- SetInteralError(result);
- m_pFSM->m_csErrMsg = "获取配置文件管理句柄失败";
- Dbg("*** ERROR: Failed to Initialize IMbnConnectionProfileManager with code 0x%08x", result);
- return Error_Unexpect;
- }
- SAFEARRAY* cntProfiles = NULL;
- result = pCntProfileMnr->GetConnectionProfiles(pInterface, &cntProfiles);
- if(FAILED(result)){
- SetInteralError(result);
- m_pFSM->m_csErrMsg = CSimpleStringA::Format("获取配置文件列表失败:0x%X", result);
- return Error_Unexpect;
- }
- LONG lBound, hBound;
- result = SafeArrayGetLBound(cntProfiles, 1, &lBound);
- if(FAILED(result)) {
- m_pFSM->m_csErrMsg = CSimpleStringA::Format("获取配置文件上下限:0x%X", result);
- return Error_Unexpect;
- }
- result = SafeArrayGetUBound(cntProfiles, 1, &hBound);
- if(FAILED(result)){
- m_pFSM->m_csErrMsg = CSimpleStringA::Format("获取配置文件上下限:0x%X", result);
- return Error_Unexpect;
- }
- if(lBound > hBound) {
- m_pFSM->m_csErrMsg = "找不到任何对应的配置文件";
- return Error_Unexpect;
- }
-
- Dbg("profile bound: %d, %d", lBound, hBound);
- ATL::CComPtr<IMbnConnectionProfile> pCntProfile = NULL;
- for(LONG index=lBound; index<=hBound; index++) {
- pCntProfile = NULL;
- result = SafeArrayGetElement(cntProfiles, &index, (void*)(&pCntProfile));
- if(SUCCEEDED(result)) {
- BSTR strProfile;
- result = pCntProfile->GetProfileXmlData(&strProfile);
- if(SUCCEEDED(result)) {
- CSimpleStringA csXmlData = ConvertToString(strProfile);
- FreeBString(strProfile);
- tinyxml2::XMLDocument xmlData;
- if(xmlData.Parse(csXmlData) == tinyxml2::XML_SUCCESS) {
- tinyxml2::XMLElement* root = NULL;
- if((root = xmlData.FirstChildElement("MBNProfileExt")) == NULL) {
- root = xmlData.FirstChildElement("MBNProfile");
- }
- if(root != NULL) {
- CSimpleStringA csName = root->FirstChildElement( "Name" )->GetText();
- Dbg("Profiles[%d]: profileName:%s", index, (LPCTSTR)csName);
- if(!csName.Compare(lpszProfileName)) {
- Dbg("specified profileName:%s", lpszProfileName);
- if(bDelete) {
- Dbg("Found it, start to delete it");
- result = pCntProfile->Delete();
- if(cntProfiles) {
- SafeArrayDestroy(cntProfiles);
- cntProfiles = NULL;
- }
- if(SUCCEEDED(result)) {
- Dbg("Delete succ.");
- m_csTmpProfileName = csName;
- return Error_Succeed;
- } else {
- m_pFSM->m_csErrMsg = CSimpleStringA::Format("删除失败:0x%X", result);
- Dbg("delete profile failed, 0x%X", result);
- return Error_Unexpect;
- }
- } else {
- result = pCntProfile->UpdateProfile(csUpdateCtx);
- if(cntProfiles) {
- SafeArrayDestroy(cntProfiles);
- cntProfiles = NULL;
- }
- if(SUCCEEDED(result)) {
- Dbg("Update succ.");
- m_csTmpProfileName = csName;
- return Error_Succeed;
- } else {
- //https://docs.microsoft.com/en-gb/windows/desktop/com/com-error-codes-9
- if(result == HRESULT_FROM_WIN32(ERROR_NOT_FOUND)) {
- } else if(result == HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND)) {
- } else if(result == HRESULT_FROM_WIN32(ERROR_SERVICE_NOT_ACTIVE)) {
- } else {
- m_pFSM->m_csErrMsg = CSimpleStringA::Format("更新失败:0x%X", result);
- }
-
- Dbg("update profile failed, 0x%X", result);
- return Error_Unexpect;
- }
- }
- }
- }
- }
- }
- }
- }
- if(cntProfiles) {
- SafeArrayDestroy(cntProfiles);
- cntProfiles = NULL;
- }
- m_pFSM->m_csErrMsg = "找不到对应的配置文件";
- return Error_NotExist;
- }
- ErrorCodeEnum CMbnImplHelper::DeleteProfile(const CSimpleStringA& csInterfaceID, LPCTSTR lpszProfileName)
- {
- Dbg("Try to delete profile[%s]", lpszProfileName);
- CSimpleStringW csEmpty = "";
- ErrorCodeEnum rc = DealWithProfile(csInterfaceID, lpszProfileName, true, csEmpty);
- if(rc == Error_Succeed || rc == Error_Pending) {
- if(m_csTmpProfileName.GetLength() > 0) {
- CSmartPointer<IConfigInfo> spConfigRun;
- m_pFSM->GetEntityBase()->GetFunction()->OpenConfig(Config_Run, spConfigRun);
- spConfigRun->WriteConfigValue(m_csTmpProfileName, "Status", "Invalid-Delete");
- spConfigRun->WriteConfigValue(m_csTmpProfileName, "EntryName", "");
- spConfigRun->WriteConfigValue(m_csTmpProfileName, "PhoneNum", "");
- spConfigRun->WriteConfigValue(m_csTmpProfileName, "UserName", "");
- spConfigRun->WriteConfigValue(m_csTmpProfileName, "Password", "");
- spConfigRun->WriteConfigValue(m_csTmpProfileName, "APN", "");
- }
- }
- return rc;
- }
|