MbnEventImpl.cpp 81 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735
  1. #include "stdafx2.h"
  2. #include "MbnEventImpl.h"
  3. #include "tinyxml2.h"
  4. typedef BOOL(WINAPI *PFuncWow64DisableWow64FsRedirection)(PVOID);
  5. typedef BOOL(WINAPI *PFuncWow64RevertWow64FsRedirection)(PVOID);
  6. typedef BOOL(WINAPI *PFuncWow64EnableWow64FsRedirection)(BOOLEAN);
  7. BOOL bEnterWowRedirect = FALSE;
  8. using namespace tinyxml2;
  9. void FreeBString(BSTR& bstrValue)
  10. {
  11. if(bstrValue != NULL)
  12. {
  13. SysFreeString(bstrValue);
  14. }
  15. return;
  16. }
  17. LPCTSTR ConvertToString(BSTR& bstrValue)
  18. {
  19. LPCTSTR lpcszTmp = (LPCTSTR)_com_util::ConvertBSTRToString(bstrValue);
  20. if(strlen(lpcszTmp) <= 0) {
  21. return MBN_ERROR_INVALID_STRING;
  22. }
  23. return lpcszTmp;
  24. }
  25. BOOL DisableWowRedirection()
  26. {
  27. PFuncWow64DisableWow64FsRedirection func = NULL;
  28. PVOID OldWow64RedirectionValue;
  29. BOOL Result = FALSE;
  30. HMODULE hMoudle = GetModuleHandle(TEXT("kernel32"));
  31. if (hMoudle) {
  32. func = (PFuncWow64DisableWow64FsRedirection)::GetProcAddress(hMoudle,
  33. "Wow64DisableWow64FsRedirection");
  34. }
  35. if (func) {
  36. Result = func(&OldWow64RedirectionValue);
  37. }
  38. return Result;
  39. }
  40. BOOLEAN RevertWowRedirection()
  41. {
  42. PFuncWow64RevertWow64FsRedirection func = NULL;
  43. PVOID OldWow64RedirectionValue;
  44. BOOL Result = FALSE;
  45. HMODULE hMoudle = GetModuleHandle(TEXT("kernel32"));
  46. if (hMoudle) {
  47. func = (PFuncWow64RevertWow64FsRedirection)::GetProcAddress(hMoudle,
  48. "Wow64RevertWow64FsRedirection");
  49. }
  50. if (func) {
  51. Result = func(&OldWow64RedirectionValue);
  52. }
  53. return Result;
  54. }
  55. void EnterWowRedirection()
  56. {
  57. if(gArch == 64) {
  58. if(!bEnterWowRedirect) {
  59. DisableWowRedirection();
  60. bEnterWowRedirect = TRUE;
  61. }
  62. }
  63. }
  64. void LeaveWowRedirection()
  65. {
  66. if(gArch == 64) {
  67. if(bEnterWowRedirect) {
  68. RevertWowRedirection();
  69. bEnterWowRedirect = FALSE;
  70. }
  71. }
  72. }
  73. HRESULT STDMETHODCALLTYPE CustomSignalEvents::OnSignalStateChange(IMbnSignal *newInterface)
  74. {
  75. HRESULT result = S_OK;
  76. ULONG ulValue;
  77. result = newInterface->GetSignalStrength(&ulValue);
  78. if(FAILED(result)) {
  79. Dbg(">>>OnSignalStateChange: GetSignalStrength failed [result#%d]", result);
  80. } else {
  81. m_nFormattedVal = GetSignalRank(ulValue);
  82. Dbg(">>>OnSignalStateChange: %d(%u)", m_nFormattedVal, ulValue);
  83. if(m_pHelper) {
  84. m_pHelper->SetSignalVal(m_nFormattedVal, ulValue);
  85. }
  86. }
  87. return S_OK;
  88. }
  89. HRESULT STDMETHODCALLTYPE CustomConnectionEvents::OnConnectComplete(
  90. IMbnConnection *newConnection,
  91. ULONG requestID, HRESULT status)
  92. {
  93. HRESULT result = S_OK;
  94. //置连接成功状态
  95. if(m_pHelper)
  96. {
  97. BSTR interfaceID;
  98. result = newConnection->get_InterfaceID(&interfaceID);
  99. if(SUCCEEDED(result))
  100. {
  101. m_pHelper->OnConnectComplete(requestID, ConvertToString(interfaceID), status);
  102. FreeBString(interfaceID);
  103. }
  104. else
  105. {
  106. m_pHelper->OnConnectComplete(requestID, "", status);
  107. }
  108. }
  109. return result;
  110. }
  111. HRESULT STDMETHODCALLTYPE CustomConnectionEvents::OnDisconnectComplete(
  112. IMbnConnection *newConnection,
  113. ULONG reqeustID, HRESULT status)
  114. {
  115. HRESULT result = S_OK;
  116. //置断开连接状态
  117. if(m_pHelper)
  118. {
  119. BSTR interfaceID;
  120. result = newConnection->get_InterfaceID(&interfaceID);
  121. if(SUCCEEDED(result))
  122. {
  123. m_pHelper->OnDisconnectComplete(reqeustID, ConvertToString(interfaceID));
  124. FreeBString(interfaceID);
  125. }
  126. else
  127. {
  128. m_pHelper->OnDisconnectComplete(reqeustID, "");
  129. }
  130. }
  131. return result;
  132. }
  133. HRESULT STDMETHODCALLTYPE CustomConnectionEvents::OnConnectStateChange( IMbnConnection *newConnection)
  134. {
  135. HRESULT result = S_OK;
  136. MBN_ACTIVATION_STATE state;
  137. BSTR profileName;
  138. result = newConnection->GetConnectionState(&state, &profileName);
  139. if(SUCCEEDED(result))
  140. {
  141. BSTR interfaceID;
  142. result = newConnection->get_InterfaceID(&interfaceID);
  143. if(SUCCEEDED(result))
  144. {
  145. m_pHelper->OnConnectStateChange(result, state, ConvertToString(interfaceID));
  146. FreeBString(interfaceID);
  147. }
  148. else
  149. {
  150. m_pHelper->OnConnectStateChange(result, state, "");
  151. }
  152. }
  153. return S_OK;
  154. }
  155. HRESULT STDMETHODCALLTYPE CustomConnectionEvents::OnVoiceCallStateChange( IMbnConnection *newConnection)
  156. {
  157. HRESULT result = S_OK;
  158. //Dbg(">>>OnVoiceCallStateChange !!?");
  159. return S_OK;
  160. }
  161. HRESULT STDMETHODCALLTYPE CustomConnctionMnrEvents::OnConnectionArrival(IMbnConnection *newConnection)
  162. {
  163. HRESULT result = S_OK;
  164. //检测到设备连接
  165. if(m_pHelper)
  166. {
  167. BSTR interfaceID;
  168. result = newConnection->get_InterfaceID(&interfaceID);
  169. if(SUCCEEDED(result))
  170. {
  171. m_pHelper->OnConnectionArrival(ConvertToString(interfaceID));
  172. FreeBString(interfaceID);
  173. }
  174. }
  175. return S_OK;
  176. }
  177. HRESULT STDMETHODCALLTYPE CustomConnctionMnrEvents::OnConnectionRemoval(IMbnConnection *oldConnection)
  178. {
  179. HRESULT result = S_OK;
  180. //检测到设备移除
  181. if(m_pHelper)
  182. {
  183. BSTR interfaceID;
  184. result = oldConnection->get_InterfaceID(&interfaceID);
  185. if(SUCCEEDED(result))
  186. {
  187. m_pHelper->OnConnectionRemoval(ConvertToString(interfaceID));
  188. FreeBString(interfaceID);
  189. }
  190. }
  191. return S_OK;
  192. }
  193. HRESULT STDMETHODCALLTYPE CustomConnectionProfileManagerEvents::OnConnectionProfileArrival(
  194. IMbnConnectionProfile *newConnectionProfile)
  195. {
  196. m_pHelper->OnConnectionProfileArrival(newConnectionProfile);
  197. return S_OK;
  198. }
  199. HRESULT STDMETHODCALLTYPE CustomConnectionProfileManagerEvents::OnConnectionProfileRemoval(
  200. IMbnConnectionProfile *oldConnectionProfile)
  201. {
  202. m_pHelper->OnConnectionProfileRemoval(oldConnectionProfile);
  203. return S_OK;
  204. }
  205. HRESULT STDMETHODCALLTYPE CustomConnectionProfileEvents::OnProfileUpdate(IMbnConnectionProfile *newProfile)
  206. {
  207. m_pHelper->OnProfileUpdate(newProfile);
  208. return S_OK;
  209. }
  210. int CMbnImplHelper::EnumerateInterfaces(BOOL bUpdateOnly)
  211. {
  212. LOG_FUNCTION();
  213. BOOL bCircle = FALSE;
  214. static int nWwanFlag = 0;
  215. HRESULT result = S_OK;
  216. LONG lowLocalBound = 1;
  217. LONG highLocalBound = 0;
  218. ATL::CComPtr<IMbnInterfaceManager> pInterfaceMnr = NULL;
  219. result = CoCreateInstance(CLSID_MbnInterfaceManager, NULL, CLSCTX_ALL,
  220. IID_IMbnInterfaceManager,(LPVOID*)&pInterfaceMnr);
  221. if(FAILED(result)) {
  222. SetInteralError(result);
  223. if(bUpdateOnly) {
  224. SetStatusFromTmp2Main();
  225. }
  226. Dbg("*** ERROR: Failed to Initialize IMbnInterfaceManager with code 0x%08x", result);
  227. return -1;
  228. }
  229. int retVal = -1;
  230. SAFEARRAY* mbnInterfaces = NULL;
  231. if(bUpdateOnly) {
  232. ClearCurInterface();
  233. }
  234. do {
  235. bCircle = FALSE;
  236. result = pInterfaceMnr->GetInterfaces(&mbnInterfaces);
  237. if(FAILED(result)) {
  238. SetInteralError(result);
  239. if(result == E_POINTER) {
  240. Dbg("*** ERROR: The mbnInterfaces parameter is NULL");
  241. } else if(result == E_OUTOFMEMORY) {
  242. Dbg("*** ERROR: Could not allocate the required memory for inferfaces");
  243. } else {
  244. //经验证,很多情况下是因为没有插入网卡设备才会报此错误
  245. if(result == HRESULT_FROM_WIN32(ERROR_NOT_FOUND))
  246. {
  247. SetTmpState(MBD_NO_MOBILEDONGLE);
  248. Dbg("*** ERROR: Failed to retrieve MBN Interfaces because no interface...");
  249. retVal = 0;
  250. }
  251. else {
  252. Dbg("*** ERROR: Failed to retrieve MBN Interfaces with code 0x%08x", result);
  253. if(result == 0x80070426 && nWwanFlag < 2) {
  254. //Wwan autoconfig 服务未启动
  255. nWwanFlag++;
  256. Dbg("Specified error code: Some service has not been started.");
  257. m_pFSM->TryOpenWwanSvc();
  258. Sleep(1500);
  259. bCircle = TRUE;
  260. continue;
  261. }
  262. }
  263. }
  264. break;
  265. }
  266. result = SafeArrayGetLBound(mbnInterfaces, 1, &lowLocalBound);
  267. if(FAILED(result)) {
  268. SetInteralError(result);
  269. Dbg("***ERROR: Failed to retrieve First Interface with code 0x%08x", result);
  270. break;
  271. }
  272. result = SafeArrayGetUBound(mbnInterfaces, 1, &highLocalBound);
  273. if(FAILED(result)) {
  274. SetInteralError(result);
  275. Dbg("***ERROR: Failed to retrieve Last Interface with code 0x%08x", result);
  276. break;
  277. }
  278. if(lowLocalBound > highLocalBound)
  279. {
  280. result = HRESULT_FROM_WIN32(ERROR_NOT_FOUND);
  281. Dbg("***Warning: There are no interface been found !!!");
  282. SetTmpState(MBD_NO_MOBILEDONGLE);
  283. retVal = 0;
  284. break;
  285. }
  286. Dbg("Interfaces: low:%d, high:%d.", lowLocalBound, highLocalBound);
  287. if(bUpdateOnly) {
  288. g_DongleInfos.Init(highLocalBound-lowLocalBound+1);
  289. m_bHasProfile = FALSE;
  290. } else {
  291. retVal = (int)(highLocalBound - lowLocalBound + 1);
  292. break;
  293. }
  294. ATL::CComPtr<IMbnInterface> curInterface = NULL;
  295. CSimpleStringA csSecondInterfaceID(true);
  296. CSimpleStringA csFirstInterfaceID(true);
  297. MOBILEDIAL_CONNECTION_STATE cuState = MBD_INTERNAL_ERROR;
  298. int errorIndex = 0;
  299. for(LONG index=lowLocalBound; index<=highLocalBound; index++)
  300. {
  301. const int cur = index - lowLocalBound;
  302. ptMbnDongleInfo curDongle = NULL;
  303. curDongle = &(g_DongleInfos[cur]);
  304. memset(curDongle, 0, sizeof(MbnDongleInfo));
  305. curInterface = NULL;
  306. result = SafeArrayGetElement(mbnInterfaces, &index, (void*)(&curInterface));
  307. if(FAILED(result)) {
  308. SetTmpState(MBD_INTERNAL_ERROR);
  309. Dbg("***ERROR: Failed to retrieve interface element with code 0x%08x", result);
  310. continue;
  311. }
  312. BSTR bsInterfaceID;
  313. result = curInterface->get_InterfaceID(&bsInterfaceID);
  314. if(SUCCEEDED(result)) {
  315. CSimpleStringA csInterfaceID = ConvertToString(bsInterfaceID);
  316. curDongle->usDeviceType = DEVTYPE_INTER;
  317. curDongle->csInterfaceId = csInterfaceID;
  318. GetInterfaceInfo(curDongle->csInterfaceId, &(curDongle->subInfo));
  319. if(!SearchForProfileName(curDongle)) {
  320. Dbg("WARN: search profiles for %s failed!", (LPCTSTR)csInterfaceID);
  321. } else if(!m_bHasProfile && curDongle->profileList.GetCount() > 0) {
  322. m_bHasProfile = TRUE;
  323. }
  324. if(curDongle->subInfo.mbnCntState == MBN_ACTIVATION_STATE_ACTIVATED) {
  325. Dbg("bcz it's connected now...");
  326. curDongle->state = MBD_ACTIVATION_CONNECTED;
  327. SetCurInterface(csInterfaceID);
  328. } else {
  329. BOOL isValid = _IsValidInterface(curInterface);
  330. curDongle->state = m_tmpState;
  331. Dbg("IsValid: %d", isValid);
  332. if(!isValid && (
  333. curDongle->subInfo.strProviderName.IsNullOrEmpty()
  334. || !curDongle->subInfo.strProviderName.Compare(MBN_ERROR_INVALID_STRING))) {
  335. curDongle->subInfo.strProviderName =
  336. CSimpleStringA::Format(MBN_ERROR_INVALID_STRING_WITH_PARAM, ++errorIndex);
  337. }
  338. if(isValid && curDongle->profileList.GetCount() <= 0) {
  339. curDongle->state = MBD_NO_PROFILE;
  340. }
  341. if(!m_bSelected) {
  342. if(isValid) {
  343. if(curDongle->state != MBD_NO_PROFILE) {
  344. csFirstInterfaceID = csInterfaceID;
  345. } else {
  346. csSecondInterfaceID = csInterfaceID;
  347. }
  348. } else if(csSecondInterfaceID.IsNullOrEmpty()) {
  349. csSecondInterfaceID = csInterfaceID;
  350. }
  351. }
  352. }
  353. } else {
  354. SetInteralError(result);
  355. Dbg("Get interface id for index#%d failed: 0x%X", cur, result);
  356. }
  357. }
  358. retVal = (int)(highLocalBound - lowLocalBound + 1);
  359. if(!m_bSelected) {
  360. if(!csFirstInterfaceID.IsNullOrEmpty()) {
  361. SetCurInterface(csFirstInterfaceID);
  362. } else if(!csSecondInterfaceID.IsNullOrEmpty()) {
  363. SetCurInterface(csSecondInterfaceID);
  364. } else {
  365. SetTmpState(MBD_INTERNAL_ERROR);
  366. }
  367. }
  368. } while (bCircle);
  369. if(mbnInterfaces) {
  370. SafeArrayDestroy(mbnInterfaces);
  371. mbnInterfaces = NULL;
  372. }
  373. if(bUpdateOnly && !m_bSelected) {
  374. SetStatusFromTmp2Main();
  375. }
  376. return retVal;
  377. }
  378. BOOL CMbnImplHelper::GetInterface(LPCTSTR interfaceID)
  379. {
  380. HRESULT result = S_OK;
  381. ATL::CComPtr<IMbnInterfaceManager> pInterfaceMnr = NULL;
  382. result = CoCreateInstance(CLSID_MbnInterfaceManager, NULL, CLSCTX_ALL,
  383. IID_IMbnInterfaceManager,(LPVOID*)&pInterfaceMnr);
  384. if(FAILED(result))
  385. {
  386. SetInteralError(result);
  387. Dbg("*** ERROR: Failed to Initialize IMbnInterfaceManager with code 0x%08x", result);
  388. return FALSE;
  389. }
  390. m_pTmpInterface = NULL;
  391. result = pInterfaceMnr->GetInterface(
  392. (LPCWSTR)CSimpleStringA2W(CSimpleStringA(interfaceID)), &m_pTmpInterface);
  393. if(FAILED(result))
  394. {
  395. m_pFSM->m_csErrMsg = CSimpleStringA::Format("获取网卡接口失败 (0x%X)", result);
  396. if(result == HRESULT_FROM_WIN32(ERROR_NOT_FOUND))
  397. {
  398. m_pFSM->m_csErrMsg = "未找到对应的网卡";
  399. Dbg("There is no available interface matching the interfaceID[%s].", interfaceID);
  400. }
  401. else if(result == E_POINTER)
  402. {
  403. Dbg("The mbnInterface parameter is NULL.");
  404. }
  405. else if(result == E_OUTOFMEMORY)
  406. {
  407. Dbg("Could not allocate the required memory.");
  408. }
  409. else
  410. {
  411. Dbg("Retrive interface by %s failed returned 0x%08x", interfaceID, result);
  412. }
  413. return FALSE;
  414. }
  415. return TRUE;
  416. }
  417. ATL::CComPtr<IMbnInterface> CMbnImplHelper::GetInterfaceEx(LPCTSTR interfaceID)
  418. {
  419. HRESULT result = S_OK;
  420. ATL::CComPtr<IMbnInterfaceManager> pInterfaceMnr = NULL;
  421. result = CoCreateInstance(CLSID_MbnInterfaceManager, NULL, CLSCTX_ALL,
  422. IID_IMbnInterfaceManager,(LPVOID*)&pInterfaceMnr);
  423. if(FAILED(result))
  424. {
  425. SetInteralError(result);
  426. Dbg("*** ERROR: Failed to Initialize IMbnInterfaceManager with code 0x%08x", result);
  427. return NULL;
  428. }
  429. ATL::CComPtr<IMbnInterface> pInterface = NULL;
  430. result = pInterfaceMnr->GetInterface(
  431. (LPCWSTR)CSimpleStringA2W(CSimpleStringA(interfaceID)), &pInterface);
  432. if(FAILED(result))
  433. {
  434. SetTmpState(MBD_INTERNAL_ERROR);
  435. m_pFSM->m_csErrMsg = CSimpleStringA::Format("获取网卡接口失败 (0x%X)", result);
  436. if(result == HRESULT_FROM_WIN32(ERROR_NOT_FOUND)) {
  437. m_pFSM->m_csErrMsg = "未找到对应的网卡";
  438. SetTmpState(MBD_NO_MOBILEDONGLE);
  439. Dbg("There is no available interface matching the interfaceID[%s].", interfaceID);
  440. } else if(result == E_POINTER) {
  441. Dbg("The mbnInterface parameter is NULL.");
  442. }
  443. else if(result == E_OUTOFMEMORY) {
  444. Dbg("Could not allocate the required memory.");
  445. } else {
  446. Dbg("Retrive interface by %s failed returned 0x%08x", interfaceID, result);
  447. }
  448. return NULL;
  449. }
  450. return pInterface;
  451. }
  452. BOOL CMbnImplHelper::SetCurInterface(LPCTSTR interfaceID)
  453. {
  454. LOG_FUNCTION();
  455. BOOL fValid = FALSE;
  456. ATL::CComPtr<IMbnInterface> pInterface = GetInterfaceEx(interfaceID);
  457. if(pInterface) {
  458. m_interfaceID = CSimpleStringA(interfaceID);
  459. SetRequestID();
  460. m_bSelected = TRUE;
  461. m_nWhichGnrt = 0;
  462. fValid = _IsValidInterface(pInterface);
  463. SetStatusFromTmp2Main();
  464. if(fValid && !SearchForProfileName()) {
  465. SetStatusFromTmp2Main();
  466. }
  467. //TODO: BUGFIX: [11/23/2019 15:53 @Gifur]
  468. if(!fValid) {
  469. Dbg("Although is invalid, give it a chance.");
  470. fValid = TRUE;
  471. }
  472. } else {
  473. SetStatusFromTmp2Main();
  474. }
  475. Dbg("Set CurInterface as %s returned %d.", interfaceID, fValid);
  476. return fValid;
  477. }
  478. BOOL CMbnImplHelper::CheckAndRefleshCurInterface()
  479. {
  480. if(m_bSelected) {
  481. if(IsValidInterface(m_interfaceID)) {
  482. return GetConnectState();
  483. } else {
  484. SetStatusFromTmp2Main();
  485. return FALSE;
  486. }
  487. }
  488. return FALSE;
  489. }
  490. BOOL CMbnImplHelper::IsValidInterface(LPCTSTR interfaceID)
  491. {
  492. LOG_FUNCTION();
  493. ATL::CComPtr<IMbnInterface> pInterface = GetInterfaceEx(interfaceID);
  494. if(!pInterface) {
  495. Dbg("GetInterfaceEx failed !");
  496. return FALSE;
  497. }
  498. int nWaitTimes = 3, nTriTimes = 0;
  499. DWORD dwSleep = 1200;
  500. for(; nTriTimes < nWaitTimes; ++nTriTimes) {
  501. m_tmpState = MBD_UNKNOWN;
  502. if(_IsValidInterface(pInterface)) {
  503. Dbg("Confirm _IsValidInterface is OK, then break!!");
  504. break;
  505. }
  506. if(m_tmpState == MBD_READY_SIM_NO_INSERT) {
  507. nTriTimes = nWaitTimes + 1;
  508. Dbg("no insert sim!!");
  509. break;
  510. }
  511. Sleep(dwSleep);
  512. pInterface = GetInterfaceEx(interfaceID);
  513. if(!pInterface) {
  514. Dbg("GetInterfaceEx in %d times failed!", nTriTimes);
  515. return FALSE;
  516. }
  517. }
  518. if(nTriTimes >= nWaitTimes) {
  519. Dbg("invalid dongle!!");
  520. return FALSE;
  521. }
  522. nTriTimes = 0;
  523. nWaitTimes = 5;
  524. BOOL bRet = TRUE;
  525. MBN_ACTIVATION_STATE state;
  526. for(nTriTimes=0; nTriTimes<nWaitTimes; ++nTriTimes)
  527. {
  528. bRet = GetConnectState(pInterface, state);
  529. if(bRet && (state == MBN_ACTIVATION_STATE_ACTIVATED
  530. || state == MBN_ACTIVATION_STATE_DEACTIVATED)) {
  531. break;
  532. }
  533. else if(!bRet) {
  534. Dbg("get cn state failed!!");
  535. break;
  536. } else {
  537. SetTmpState((MOBILEDIAL_CONNECTION_STATE)(MBD_ACTIVATION_NONE + state));
  538. }
  539. Sleep(dwSleep);
  540. pInterface = GetInterfaceEx(interfaceID);
  541. if(!pInterface) {
  542. Dbg("GetInterfaceEx in %d times failed!", nTriTimes);
  543. return FALSE;
  544. }
  545. }
  546. if(bRet) {
  547. int nGnrt = GetGeneration(pInterface);
  548. nWaitTimes = 3;
  549. dwSleep = 1200;
  550. for(nTriTimes=0; nGnrt <= 2 && nTriTimes < nWaitTimes; ++nTriTimes) {
  551. Dbg("GetGeneration [res=%d] [times=%d]", nGnrt, nTriTimes);
  552. Sleep(dwSleep);
  553. pInterface = GetInterfaceEx(interfaceID);
  554. if(!pInterface) {
  555. Dbg("GetInterfaceEx in %d times failed!", nTriTimes);
  556. return FALSE;
  557. }
  558. nGnrt = GetGeneration(pInterface);
  559. }
  560. m_nWhichGnrt = nGnrt;
  561. if(nTriTimes >= nWaitTimes) {
  562. bRet = FALSE;
  563. }
  564. }
  565. return bRet;
  566. }
  567. BOOL CMbnImplHelper::_IsValidInterface(ATL::CComPtr<IMbnInterface> pInterface)
  568. {
  569. LOG_FUNCTION();
  570. if(pInterface == NULL) {
  571. SetTmpState(MBD_NO_MOBILEDONGLE);
  572. return FALSE;
  573. }
  574. MBN_INTERFACE_CAPS interfaceCaps;
  575. HRESULT result = S_OK;
  576. result = pInterface->GetInterfaceCapability(&interfaceCaps);
  577. if(SUCCEEDED(result)) {
  578. if(interfaceCaps.dataClass & MBN_DATA_CLASS_CUSTOM)
  579. SysFreeString(interfaceCaps.customDataClass);
  580. SysFreeString(interfaceCaps.deviceID);
  581. SysFreeString(interfaceCaps.manufacturer);
  582. SysFreeString(interfaceCaps.model);
  583. SysFreeString(interfaceCaps.firmwareInfo);
  584. if((interfaceCaps.gsmBandClass & MBN_BAND_CLASS_CUSTOM) ||
  585. (interfaceCaps.cdmaBandClass & MBN_BAND_CLASS_CUSTOM))
  586. SysFreeString(interfaceCaps.customBandClass);
  587. } else {
  588. SetInteralError(result);
  589. Dbg(">>> GetInterfaceCapability failed returned 0x%x", result);
  590. return FALSE;
  591. }
  592. MOBILEDIAL_CONNECTION_STATE& curState = m_tmpState;
  593. ////////////////////////////Ready State//////////////////////////////////////////////
  594. MBN_READY_STATE readyState;
  595. result = pInterface->GetReadyState(&readyState);
  596. if(SUCCEEDED(result)) {
  597. if(readyState != MBN_READY_STATE_INITIALIZED) {
  598. Dbg(">>> Ready State: %s", StateToString(readyState, curState));
  599. return FALSE;
  600. }
  601. } else {
  602. Dbg(">>> Get Ready State failed returned 0x%x", result);
  603. curState = MBD_READY_BAD_SIM; //or MBD_INTERNAL_ERROR?
  604. return FALSE;
  605. }
  606. ////////////////////////////Registration State//////////////////////////////////////////////
  607. IMbnRegistration* pRegistration;
  608. result = pInterface->QueryInterface(IID_IMbnRegistration, (void**)&pRegistration);
  609. if(SUCCEEDED(result)) {
  610. MBN_REGISTER_STATE registerState;
  611. result = pRegistration->GetRegisterState(&registerState);
  612. if(SUCCEEDED(result)) {
  613. if(registerState != MBN_REGISTER_STATE_HOME
  614. && registerState != MBN_REGISTER_STATE_ROAMING
  615. && registerState != MBN_REGISTER_STATE_PARTNER) {
  616. Dbg(">>> Register State: %s", RegisterStateToString(registerState, curState));
  617. return FALSE;
  618. }
  619. } else {
  620. Dbg(">>> Get RegisterState failed returned 0x%x", result);
  621. curState = MBD_READY_BAD_SIM;
  622. return FALSE;
  623. }
  624. BSTR providerName;
  625. result = pRegistration->GetProviderName(&providerName);
  626. if(SUCCEEDED(result)) {
  627. Dbg(">>> Provider Name: %s", ConvertToString(providerName));
  628. SysFreeString(providerName);
  629. }
  630. SAFE_RELEASE(pRegistration);
  631. } else {
  632. Dbg(">>> Query Regist Interface failed returned 0x%x", result);
  633. curState = MBD_READY_BAD_SIM;
  634. return FALSE;
  635. }
  636. ////////////////////////////Connect State//////////////////////////////////////////////
  637. IMbnConnection* pIMbnConnection = NULL;
  638. result = pInterface->GetConnection(&pIMbnConnection);
  639. if(FAILED(result)) {
  640. Dbg(">>> Get connection failed returned 0x%x", result);
  641. curState = MBD_READY_BAD_SIM;
  642. return FALSE;
  643. } else {
  644. BSTR profileName;
  645. MBN_ACTIVATION_STATE cntState;
  646. result = pIMbnConnection->GetConnectionState(&cntState, &profileName);
  647. if(SUCCEEDED(result)) {
  648. Dbg(">>> Connection State: %s", ActivationStateToString(cntState));
  649. if(cntState == MBN_ACTIVATION_STATE_ACTIVATED) {
  650. if(profileName) {
  651. Dbg(">>> Profile Name: %s", ConvertToString(profileName));
  652. FreeBString(profileName);
  653. } else {
  654. Dbg(">>> Cannot fetch connected profile name!");
  655. }
  656. }
  657. curState = (MOBILEDIAL_CONNECTION_STATE)(MBD_ACTIVATION_NONE + cntState);
  658. if(cntState != MBN_ACTIVATION_STATE_ACTIVATED && cntState != MBN_ACTIVATION_STATE_DEACTIVATED) {
  659. return FALSE;
  660. }
  661. }
  662. else {
  663. Dbg(">>> Get ConnectionState failed returned 0x%x", result);
  664. curState = MBD_READY_BAD_SIM;
  665. return FALSE;
  666. }
  667. }
  668. return TRUE;
  669. }
  670. BOOL CMbnImplHelper::GetInterfaceInfo(LPCTSTR interfaceID, pMbnDongleSubInfo pDongleSubInfo)
  671. {
  672. LOG_FUNCTION();
  673. BOOL res = TRUE;
  674. MBN_INTERFACE_CAPS interfaceCaps;
  675. HRESULT result = S_OK;
  676. assert(pDongleSubInfo != NULL && "pDongleSubInfo != NULL");
  677. if(!GetInterface(interfaceID)) {
  678. return FALSE;
  679. }
  680. memset(pDongleSubInfo, 0, sizeof(MbnDongleSubInfo));
  681. InitMbnDongleSubInfo(pDongleSubInfo);
  682. //TODO: LOCK
  683. ATL::CComPtr<IMbnInterface> curInterface = m_pTmpInterface;
  684. result = curInterface->GetInterfaceCapability(&interfaceCaps);
  685. if(SUCCEEDED(result))
  686. {
  687. pDongleSubInfo->strDeviceID = ConvertToString(interfaceCaps.deviceID);
  688. pDongleSubInfo->strManufacturer = ConvertToString(interfaceCaps.manufacturer);
  689. pDongleSubInfo->strMode = ConvertToString(interfaceCaps.model);
  690. pDongleSubInfo->strFirmware = ConvertToString(interfaceCaps.firmwareInfo);
  691. pDongleSubInfo->strCellularClass = CellularToString(interfaceCaps.cellularClass);
  692. Dbg(">>> Device ID: %s", ConvertToString(interfaceCaps.deviceID));
  693. Dbg(">>> Manufacturer: %s", ConvertToString(interfaceCaps.manufacturer));
  694. Dbg(">>> Mode: %s", ConvertToString(interfaceCaps.model));
  695. Dbg(">>> FirmwareInfor: %s", ConvertToString(interfaceCaps.firmwareInfo));
  696. Dbg(">>> CellularClass: %s", CellularToString(interfaceCaps.cellularClass));
  697. if(interfaceCaps.dataClass & MBN_DATA_CLASS_CUSTOM)
  698. SysFreeString(interfaceCaps.customDataClass);
  699. SysFreeString(interfaceCaps.deviceID);
  700. SysFreeString(interfaceCaps.manufacturer);
  701. SysFreeString(interfaceCaps.model);
  702. SysFreeString(interfaceCaps.firmwareInfo);
  703. if((interfaceCaps.gsmBandClass & MBN_BAND_CLASS_CUSTOM) ||
  704. (interfaceCaps.cdmaBandClass & MBN_BAND_CLASS_CUSTOM))
  705. SysFreeString(interfaceCaps.customBandClass);
  706. }
  707. else
  708. {
  709. SetInteralError(result);
  710. Dbg(">>> GetInterfaceCapability failed returned 0x%x", result);
  711. m_pFSM->m_csErrMsg = "GetInterfaceCapability failed";
  712. return FALSE;
  713. }
  714. MOBILEDIAL_CONNECTION_STATE curState;
  715. int nErrorCount = 0;
  716. ////////////////////////////Ready State//////////////////////////////////////////////
  717. MBN_READY_STATE readyState;
  718. result = curInterface->GetReadyState(&readyState);
  719. if(SUCCEEDED(result))
  720. {
  721. pDongleSubInfo->mbnReadyState = readyState;
  722. //pDongleSubInfo->strReadyState = StateToString(readyState, curState);
  723. Dbg(">>> Ready State: %s", StateToString(readyState, curState));
  724. }
  725. else
  726. { pDongleSubInfo->mbnReadyState = MBN_READY_STATE_OFF;
  727. Dbg(">>> Get Ready State failed returned 0x%x", result);
  728. nErrorCount++;
  729. }
  730. ////////////////////////////Registration State//////////////////////////////////////////////
  731. IMbnRegistration* pRegistration;
  732. result = curInterface->QueryInterface(IID_IMbnRegistration, (void**)&pRegistration);
  733. if(SUCCEEDED(result))
  734. {
  735. MBN_REGISTER_STATE registerState;
  736. result = pRegistration->GetRegisterState(&registerState);
  737. if(SUCCEEDED(result))
  738. {
  739. Dbg(">>> Register State: %s", RegisterStateToString(registerState, curState));
  740. //pDongleSubInfo->strRegisterState = RegisterStateToString(registerState, curState);
  741. pDongleSubInfo->mbnRegState = registerState;
  742. }
  743. else
  744. {
  745. pDongleSubInfo->mbnRegState = MBN_REGISTER_STATE_NONE;
  746. Dbg(">>> Get RegisterState failed returned 0x%x", result);
  747. nErrorCount++;
  748. }
  749. MBN_DATA_CLASS dataClass;
  750. result = pRegistration->GetCurrentDataClass((ULONG*)&dataClass);
  751. if(SUCCEEDED(result)) {
  752. pDongleSubInfo->strDataClass = DataClassToString(dataClass);
  753. Dbg(">>> Data Class: %s(0x%08x)", DataClassToString(dataClass), dataClass);
  754. }
  755. else {
  756. Dbg(">>> Get DataClass failed returned 0x%x", result);
  757. nErrorCount++;
  758. }
  759. BSTR providerName;
  760. result = pRegistration->GetProviderName(&providerName);
  761. if(SUCCEEDED(result))
  762. {
  763. pDongleSubInfo->strProviderName = ConvertToString(providerName);
  764. Dbg(">>> Provider Name: %s", ConvertToString(providerName));
  765. SysFreeString(providerName);
  766. }
  767. else {
  768. Dbg(">>> Get ProviderName failed returned 0x%x", result);
  769. nErrorCount++;
  770. }
  771. SAFE_RELEASE(pRegistration);
  772. }
  773. else
  774. {
  775. Dbg(">>> Query Regist Interface failed returned 0x%x", result);
  776. nErrorCount++;
  777. }
  778. ////////////////////////////Connect State//////////////////////////////////////////////
  779. IMbnConnection* pIMbnConnection = NULL;
  780. result = curInterface->GetConnection(&pIMbnConnection);
  781. if(FAILED(result)) {
  782. Dbg(">>> Get connection failed returned 0x%x", result);
  783. nErrorCount++;
  784. }
  785. else
  786. {
  787. BSTR profileName;
  788. MBN_ACTIVATION_STATE cntState;
  789. result = pIMbnConnection->GetConnectionState(&cntState, &profileName);
  790. if(SUCCEEDED(result))
  791. {
  792. //pDongleSubInfo->strConnectState = ActivationStateToString(cntState);
  793. pDongleSubInfo->mbnCntState = cntState;
  794. Dbg(">>> Connection State: %s", ActivationStateToString(cntState));
  795. if(cntState == MBN_ACTIVATION_STATE_ACTIVATED) {
  796. if(profileName) {
  797. Dbg(">>> Profile Name: %s", ConvertToString(profileName));
  798. pDongleSubInfo->strProfileName = ConvertToString(profileName);
  799. FreeBString(profileName);
  800. } else {
  801. Dbg(">>> Cannot fetch connected profile name!");
  802. pDongleSubInfo->strProfileName = "";
  803. }
  804. }
  805. }
  806. else
  807. {
  808. Dbg(">>> Get ConnectionState failed returned 0x%x", result);
  809. nErrorCount++;
  810. }
  811. SAFE_RELEASE(pIMbnConnection);
  812. }
  813. ////////////////////////////Subscriber State//////////////////////////////////////////////
  814. ATL::CComPtr<IMbnSubscriberInformation> pSubInfo = NULL;
  815. result = curInterface->GetSubscriberInformation(&pSubInfo);
  816. if(SUCCEEDED(result))
  817. {
  818. BSTR subId;
  819. pSubInfo->get_SubscriberID(&subId);
  820. Dbg(">>> subID: %s", ConvertToString(subId));
  821. pDongleSubInfo->strIMSI = ConvertToString(subId);
  822. SysFreeString(subId);
  823. BSTR simIccId;
  824. result = pSubInfo->get_SimIccID(&simIccId);
  825. Dbg(">>> SimIccID: %s", ConvertToString(simIccId));
  826. pDongleSubInfo->strSimIccID = ConvertToString(simIccId);
  827. }
  828. IMbnSignal* pImbnSignal = NULL;
  829. result = curInterface->QueryInterface(IID_IMbnSignal, (void**)&pImbnSignal);
  830. if(SUCCEEDED(result))
  831. {
  832. ULONG ulSignalStrength;
  833. result = pImbnSignal->GetSignalStrength(&ulSignalStrength);
  834. if(FAILED(result) || ulSignalStrength == MBN_RSSI_UNKNOWN)
  835. {
  836. Dbg(">>> Signal Strength: UNKNOWN");
  837. pDongleSubInfo->ulSingnal = 0UL;
  838. }
  839. else
  840. {
  841. Dbg(">>> Signal Strength: %2.0lf%%", 100.0*(double)ulSignalStrength/31.0);
  842. pDongleSubInfo->ulSingnal = ulSignalStrength;
  843. }
  844. SAFE_RELEASE(pImbnSignal);
  845. }
  846. return TRUE;
  847. }
  848. BOOL CMbnImplHelper::AdviseSignalEvent()
  849. {
  850. if(m_bSignalEventInitied)
  851. {
  852. return TRUE;
  853. }
  854. HRESULT result = S_OK;
  855. ATL::CComPtr<IMbnInterfaceManager> pInterfaceMnr = NULL;
  856. result = CoCreateInstance(CLSID_MbnInterfaceManager, NULL, CLSCTX_ALL,
  857. IID_IMbnInterfaceManager,(LPVOID*)&pInterfaceMnr);
  858. if(FAILED(result))
  859. {
  860. Dbg("*** ERROR: Failed to Initialize IMbnInterfaceManager with code 0x%08x", result);
  861. return FALSE;
  862. }
  863. result = pInterfaceMnr->QueryInterface(IID_IConnectionPointContainer, (void**)&m_pContainer);
  864. if(SUCCEEDED(result))
  865. {
  866. result = m_pContainer->FindConnectionPoint(IID_IMbnSignalEvents, &m_pCntPointForSignal);
  867. if(SUCCEEDED(result))
  868. {
  869. IUnknown *pSignalUnk = (IUnknown*)m_signalEvent;
  870. result = m_pCntPointForSignal->Advise(pSignalUnk, &m_dwCkSignal);
  871. if(SUCCEEDED(result))
  872. {
  873. Dbg("Advise success, Waiting SignalStateChange Event...");
  874. m_bSignalEventInitied = TRUE;
  875. return TRUE;
  876. }
  877. else
  878. {
  879. Dbg("Advise SignalStateChange Event failed 0x%x", result);
  880. return FALSE;
  881. }
  882. }
  883. else
  884. {
  885. Dbg("FindConnectionPoint of IID_IMbnSignalEvents failed 0x%x", result);
  886. return FALSE;
  887. }
  888. }
  889. return FALSE;
  890. }
  891. BOOL CMbnImplHelper::AdviseCntMnrEvent()
  892. {
  893. if(m_bCntMnrEventInitied)
  894. {
  895. return TRUE;
  896. }
  897. HRESULT result = S_OK;
  898. ATL::CComPtr<IMbnConnectionManager> pCntMnr = NULL;
  899. result = CoCreateInstance(CLSID_MbnConnectionManager, NULL, CLSCTX_ALL,
  900. IID_IMbnConnectionManager, (LPVOID*)&pCntMnr);
  901. if(FAILED(result))
  902. {
  903. Dbg("*** ERROR: Failed to Initialize IMbnConnectionManager with code 0x%08x", result);
  904. return FALSE;
  905. }
  906. result = pCntMnr->QueryInterface(IID_IConnectionPointContainer, (void**)&m_pContainer);
  907. if(SUCCEEDED(result))
  908. {
  909. result = m_pContainer->FindConnectionPoint(IID_IMbnConnectionManagerEvents, &m_pCntPointForCntMnrEvent);
  910. if(SUCCEEDED(result))
  911. {
  912. IUnknown *pConnectUnk = (IUnknown*)m_cntMnrEvent;
  913. result = m_pCntPointForCntMnrEvent->Advise(pConnectUnk, &m_dwCkCntMnr);
  914. if(SUCCEEDED(result))
  915. {
  916. Dbg("Advise success, Waiting Connection Manager Event...");
  917. m_bCntMnrEventInitied = TRUE;
  918. return TRUE;
  919. }
  920. else
  921. {
  922. Dbg("Advise ConnectionEvent failed 0x%x", result);
  923. return FALSE;
  924. }
  925. }
  926. else
  927. {
  928. Dbg("FindConnectionPoint of IID_IMbnConnectionManagerEvents failed 0x%x", result);
  929. if(result == CONNECT_E_NOCONNECTION)
  930. {
  931. Dbg("This connectable object does not support the outgoing interface specified by riid.");
  932. }
  933. return FALSE;
  934. }
  935. }
  936. return FALSE;
  937. }
  938. BOOL CMbnImplHelper::AdviseConnectEvent()
  939. {
  940. if(m_bCntEventInitied)
  941. {
  942. return TRUE;
  943. }
  944. HRESULT result = S_OK;
  945. ATL::CComPtr<IMbnConnectionManager> pCntMnr = NULL;
  946. result = CoCreateInstance(CLSID_MbnConnectionManager, NULL, CLSCTX_ALL,
  947. IID_IMbnConnectionManager, (LPVOID*)&pCntMnr);
  948. if(FAILED(result))
  949. {
  950. Dbg("*** ERROR: Failed to Initialize IMbnConnectionManager with code 0x%08x", result);
  951. return FALSE;
  952. }
  953. result = pCntMnr->QueryInterface(IID_IConnectionPointContainer, (void**)&m_pContainer);
  954. if(SUCCEEDED(result))
  955. {
  956. result = m_pContainer->FindConnectionPoint(IID_IMbnConnectionEvents, &m_pCntPointForCntEvent);
  957. if(SUCCEEDED(result))
  958. {
  959. IUnknown *pConnectUnk = (IUnknown*)m_cntEvent;
  960. result = m_pCntPointForCntEvent->Advise(pConnectUnk, &m_dwCkCnt);
  961. if(SUCCEEDED(result))
  962. {
  963. Dbg("Advise success, Waiting Connection Event...");
  964. m_bCntEventInitied = TRUE;
  965. return TRUE;
  966. }
  967. else
  968. {
  969. Dbg("Advise ConnectionEvent failed 0x%x", result);
  970. return FALSE;
  971. }
  972. }
  973. else
  974. {
  975. Dbg("FindConnectionPoint of IID_IMbnConnectionEvents failed 0x%x", result);
  976. if(result == CONNECT_E_NOCONNECTION)
  977. {
  978. Dbg("This connectable object does not support the outgoing interface specified by riid.");
  979. }
  980. return FALSE;
  981. }
  982. }
  983. return FALSE;
  984. }
  985. BOOL CMbnImplHelper::AdviseCntProfileMnrEvent()
  986. {
  987. if(m_bCntPfMnrEventInited)
  988. {
  989. return TRUE;
  990. }
  991. HRESULT result = S_OK;
  992. ATL::CComPtr<IMbnConnectionProfileManager> pCntProfileMnr = NULL;
  993. result = CoCreateInstance(CLSID_MbnConnectionProfileManager, NULL, CLSCTX_ALL,
  994. IID_IMbnConnectionProfileManager, (LPVOID*)&pCntProfileMnr);
  995. if(FAILED(result))
  996. {
  997. Dbg("*** ERROR: Failed to Initialize IMbnConnectionProfileManager with code 0x%08x", result);
  998. return FALSE;
  999. }
  1000. result = pCntProfileMnr->QueryInterface(IID_IConnectionPointContainer, (void**)&m_pContainer);
  1001. if(SUCCEEDED(result))
  1002. {
  1003. result = m_pContainer->FindConnectionPoint(
  1004. IID_IMbnConnectionProfileManagerEvents, &m_pCntPointForCntProfileMnrEvent);
  1005. if(SUCCEEDED(result))
  1006. {
  1007. IUnknown *pConnectUnk = (IUnknown*)m_cntPfMnrEvent;
  1008. result = m_pCntPointForCntProfileMnrEvent->Advise(pConnectUnk, &m_dwCkCntPfMnr);
  1009. if(SUCCEEDED(result))
  1010. {
  1011. Dbg("Advise success, Waiting Connection profile Manager Event...");
  1012. m_bCntPfMnrEventInited = TRUE;
  1013. return TRUE;
  1014. }
  1015. else
  1016. {
  1017. Dbg("Advise Connection profile Manager failed 0x%x", result);
  1018. return FALSE;
  1019. }
  1020. }
  1021. else
  1022. {
  1023. Dbg("FindConnectionPoint of IID_IMbnConnectionProfileManagerEvents failed 0x%x", result);
  1024. if(result == CONNECT_E_NOCONNECTION)
  1025. {
  1026. Dbg("This connectable object does not support the outgoing interface specified by riid.");
  1027. }
  1028. return FALSE;
  1029. }
  1030. }
  1031. return FALSE;
  1032. }
  1033. BOOL CMbnImplHelper::AdviseCntProfileEvent()
  1034. {
  1035. if(m_bCntPfEventInited)
  1036. {
  1037. return TRUE;
  1038. }
  1039. HRESULT result = S_OK;
  1040. ATL::CComPtr<IMbnConnectionProfileManager> pCntProfileMnr = NULL;
  1041. result = CoCreateInstance(CLSID_MbnConnectionProfileManager, NULL, CLSCTX_ALL,
  1042. IID_IMbnConnectionProfileManager, (LPVOID*)&pCntProfileMnr);
  1043. if(FAILED(result))
  1044. {
  1045. Dbg("*** ERROR: Failed to Initialize IMbnConnectionProfileManager with code 0x%08x", result);
  1046. return FALSE;
  1047. }
  1048. result = pCntProfileMnr->QueryInterface(IID_IConnectionPointContainer, (void**)&m_pContainer);
  1049. if(SUCCEEDED(result))
  1050. {
  1051. result = m_pContainer->FindConnectionPoint(
  1052. IID_IMbnConnectionProfileEvents, &m_pCntPointForProfileEvent);
  1053. if(SUCCEEDED(result))
  1054. {
  1055. IUnknown *pConnectUnk = (IUnknown*)m_cntPfEvent;
  1056. result = m_pCntPointForProfileEvent->Advise(pConnectUnk, &m_dwCkPf);
  1057. if(SUCCEEDED(result))
  1058. {
  1059. Dbg("Advise success, Waiting Connection profile Event...");
  1060. m_bCntPfEventInited = TRUE;
  1061. return TRUE;
  1062. }
  1063. else
  1064. {
  1065. Dbg("Advise Connection profile failed 0x%x", result);
  1066. return FALSE;
  1067. }
  1068. }
  1069. else
  1070. {
  1071. Dbg("FindConnectionPoint of IID_IMbnConnectionProfileEvents failed 0x%x", result);
  1072. if(result == CONNECT_E_NOCONNECTION)
  1073. {
  1074. Dbg("This connectable object does not support the outgoing interface specified by riid.");
  1075. }
  1076. return FALSE;
  1077. }
  1078. }
  1079. return FALSE;
  1080. }
  1081. BOOL CMbnImplHelper::UnAdviseCustomEvents()
  1082. {
  1083. HRESULT result = S_OK;
  1084. if(m_bCntEventInitied)
  1085. {
  1086. result = m_pCntPointForCntEvent->Unadvise(m_dwCkCnt);
  1087. Dbg("Unadvise Connection Event return 0x%x", result);
  1088. if(SUCCEEDED(result))
  1089. {
  1090. SAFE_RELEASE(m_pCntPointForCntEvent);
  1091. m_bCntEventInitied = FALSE;
  1092. }
  1093. }
  1094. else
  1095. {
  1096. }
  1097. if(m_bSignalEventInitied)
  1098. {
  1099. result = m_pCntPointForSignal->Unadvise(m_dwCkSignal);
  1100. Dbg("Unadvise Signal Event return 0x%x", result);
  1101. if(SUCCEEDED(result))
  1102. {
  1103. SAFE_RELEASE(m_pCntPointForSignal);
  1104. m_bSignalEventInitied = FALSE;
  1105. }
  1106. }
  1107. else
  1108. {
  1109. }
  1110. if(m_bCntMnrEventInitied)
  1111. {
  1112. result = m_pCntPointForCntMnrEvent->Unadvise(m_dwCkCntMnr);
  1113. Dbg("Unadvise Connection Manage Event return 0x%x", result);
  1114. if(SUCCEEDED(result))
  1115. {
  1116. SAFE_RELEASE(m_pCntPointForCntMnrEvent);
  1117. m_bCntMnrEventInitied = FALSE;
  1118. }
  1119. }
  1120. else
  1121. {
  1122. }
  1123. if(m_bCntPfMnrEventInited)
  1124. {
  1125. result = m_pCntPointForCntProfileMnrEvent->Unadvise(m_dwCkCntPfMnr);
  1126. if(SUCCEEDED(result))
  1127. {
  1128. SAFE_RELEASE(m_pCntPointForCntProfileMnrEvent);
  1129. m_bCntPfMnrEventInited = FALSE;
  1130. }
  1131. }
  1132. else
  1133. {
  1134. }
  1135. if(m_bCntPfEventInited)
  1136. {
  1137. result = m_pCntPointForProfileEvent->Unadvise(m_dwCkPf);
  1138. if(SUCCEEDED(result))
  1139. {
  1140. SAFE_RELEASE(m_pCntPointForProfileEvent);
  1141. m_bCntPfEventInited = FALSE;
  1142. }
  1143. }
  1144. else
  1145. {
  1146. }
  1147. return TRUE;
  1148. }
  1149. BOOL CMbnImplHelper::GetConnectState()
  1150. {
  1151. if(m_bSelected) {
  1152. ATL::CComPtr<IMbnInterface> pInterface = GetInterfaceEx(m_interfaceID);
  1153. if(GetConnectState(pInterface, m_cntState)) {
  1154. SetCurState((MOBILEDIAL_CONNECTION_STATE)(MBD_ACTIVATION_NONE + m_cntState));
  1155. Dbg("GetConnectState{%s}.", ConvertStatusToString(m_curState));
  1156. return TRUE;
  1157. }
  1158. SetStatusFromTmp2Main();
  1159. }
  1160. return FALSE;
  1161. }
  1162. BOOL CMbnImplHelper::GetConnectState(ATL::CComPtr<IMbnInterface> pInterface, MBN_ACTIVATION_STATE& state)
  1163. {
  1164. state = MBN_ACTIVATION_STATE_NONE;
  1165. HRESULT result = S_OK;
  1166. if(!pInterface) {
  1167. Dbg("pInterface is invalid");
  1168. SetTmpState(MBD_INTERNAL_ERROR);
  1169. return FALSE;
  1170. }
  1171. ATL::CComPtr<IMbnConnection> pConnection = NULL;
  1172. result = pInterface->GetConnection(&pConnection);
  1173. if(FAILED(result))
  1174. {
  1175. if(result == HRESULT_FROM_WIN32(ERROR_NOT_FOUND)){
  1176. Dbg("***Either there is no available connection or the device is not registered to a network.");
  1177. SetTmpState(MBD_READY_BAD_SIM);
  1178. }
  1179. else {
  1180. SetInteralError(result);
  1181. Dbg("***ERROR: Failed to retrieve mbnConnection with code 0x%08x", result);
  1182. }
  1183. return FALSE;
  1184. }
  1185. BSTR profileName;
  1186. result = pConnection->GetConnectionState(&state, &profileName);
  1187. if(FAILED(result)) {
  1188. if(result == E_MBN_SIM_NOT_INSERTED) {
  1189. Dbg("***ERROR: A SIM is not inserted in the device.");
  1190. SetTmpState(MBD_READY_SIM_NO_INSERT);
  1191. }
  1192. else if(result == E_MBN_PIN_REQUIRED) {
  1193. SetTmpState(MBD_CONNECTION_PIN_REQUIRED);
  1194. Dbg("***ERROR: A PIN is required to get the call state.");
  1195. }
  1196. else if(result == E_MBN_BAD_SIM) {
  1197. Dbg("***ERROR: A bad SIM is inserted in the device.");
  1198. SetTmpState(MBD_READY_BAD_SIM);
  1199. }
  1200. else if(result == E_PENDING) {
  1201. SetTmpState(MBD_UNKNOWN);
  1202. Dbg("***ERROR: The activation state not available.");
  1203. }else {
  1204. SetInteralError(result);
  1205. Dbg("***ERROR: Failed to retrieve GetConnectionState with code 0x%08x", result);
  1206. }
  1207. return FALSE;
  1208. }
  1209. if(state == MBN_ACTIVATION_STATE_ACTIVATED){
  1210. FreeBString(profileName);
  1211. }
  1212. SetTmpState((MOBILEDIAL_CONNECTION_STATE)(MBD_ACTIVATION_NONE + state));
  1213. return TRUE;
  1214. }
  1215. int CMbnImplHelper::GetGeneration(ATL::CComPtr<IMbnInterface> pInterface)
  1216. {
  1217. int nRes = 1;
  1218. if(!pInterface) {
  1219. Dbg("Detect pInterface is NULL in GetGeneration");
  1220. SetTmpState(MBD_INTERNAL_ERROR);
  1221. return nRes;
  1222. }
  1223. HRESULT result = S_OK;
  1224. IMbnRegistration* pRegistration;
  1225. result = pInterface->QueryInterface(IID_IMbnRegistration, (void**)&pRegistration);
  1226. if(SUCCEEDED(result)) {
  1227. MBN_DATA_CLASS dataClass;
  1228. result = pRegistration->GetCurrentDataClass((ULONG*)&dataClass);
  1229. if(SUCCEEDED(result)) {
  1230. Dbg(">>> Data Class: %s(0x%08x)", DataClassToString(dataClass), dataClass);
  1231. SAFE_RELEASE(pRegistration);
  1232. if(dataClass == MBN_DATA_CLASS_NONE) {
  1233. //m_nWhichGnrt = 2;
  1234. nRes = 2;
  1235. }
  1236. else if((dataClass & MBN_DATA_CLASS_LTE) == MBN_DATA_CLASS_LTE) {
  1237. //m_nWhichGnrt = 4;
  1238. nRes = 4;
  1239. }else {
  1240. //m_nWhichGnrt = 3;
  1241. nRes = 3;
  1242. }
  1243. } else {
  1244. if(result == E_PENDING) {
  1245. Dbg("***Warning: The data classes are not available.");
  1246. //m_nWhichGnrt = 2;
  1247. SetTmpState(MBD_UNKNOWN);
  1248. nRes = 2;
  1249. }
  1250. else if(result == E_MBN_PIN_REQUIRED) {
  1251. Dbg("***ERROR: Data Class - A PIN is required to get the data classes.");
  1252. SetTmpState(MBD_CONNECTION_PIN_REQUIRED);
  1253. }
  1254. else if(result == E_MBN_SIM_NOT_INSERTED) {
  1255. Dbg("***ERROR: Data Class - A SIM is not inserted in the device.");
  1256. SetTmpState(MBD_READY_SIM_NO_INSERT);
  1257. nRes = 0;
  1258. }
  1259. else if(result == E_MBN_BAD_SIM) {
  1260. Dbg("***ERROR: Data Class - A bad SIM is inserted in the device.");
  1261. SetTmpState(MBD_READY_BAD_SIM);
  1262. }
  1263. else {
  1264. SetInteralError(result);
  1265. Dbg("***ERROR: Failed to retrieve DataClass failed returned 0x%08x.", result);
  1266. }
  1267. }
  1268. }
  1269. return nRes;
  1270. }
  1271. BOOL CMbnImplHelper::GetSignalValue(ATL::CComPtr<IMbnInterface> pInterface, ULONG& ulSignalStrength)
  1272. {
  1273. HRESULT result = S_OK;
  1274. if(!pInterface)
  1275. {
  1276. //SetCurState(MBD_NO_MOBILEDONGLE);
  1277. Dbg("Detect pInterface is NULL in SetSingnalValue");
  1278. return FALSE;
  1279. }
  1280. IMbnSignal* pImbnSignal = NULL;
  1281. result = pInterface->QueryInterface(IID_IMbnSignal, (void**)&pImbnSignal);
  1282. if(SUCCEEDED(result))
  1283. {
  1284. result = pImbnSignal->GetSignalStrength(&ulSignalStrength);
  1285. SAFE_RELEASE(pImbnSignal);
  1286. if(SUCCEEDED(result)) {
  1287. //SetSignalVal(GetSignalRank(ulSignalStrength), ulSignalStrength);
  1288. return TRUE;
  1289. }
  1290. }
  1291. return FALSE;
  1292. }
  1293. BOOL CMbnImplHelper::SearchForProfileName(ptMbnDongleInfo pInfo)
  1294. {
  1295. LOG_FUNCTION();
  1296. MbnDongleInfo origin;
  1297. bool bSpecified = true;
  1298. if(pInfo == NULL) {
  1299. memset(&origin, 0, sizeof(origin));
  1300. pInfo = &origin;
  1301. bSpecified = false;
  1302. InitMbnDongleSubInfo(&(pInfo->subInfo));
  1303. GetInterfaceInfo(m_interfaceID, &(pInfo->subInfo));
  1304. pInfo->profileList.Clear();
  1305. } else {
  1306. Dbg("Search for ProfileName of %s", (LPCTSTR)pInfo->csInterfaceId);
  1307. if(pInfo->profileList.GetCount() > 0) {
  1308. pInfo->profileList.Clear();
  1309. }
  1310. }
  1311. if(!bSpecified && !m_bSelected) {
  1312. Dbg("Detect m_pCurInterface is NULL in ");
  1313. return FALSE;
  1314. }
  1315. HRESULT result = S_OK;
  1316. ATL::CComPtr<IMbnConnectionProfileManager> pCntProfileMnr = NULL;
  1317. result = CoCreateInstance(CLSID_MbnConnectionProfileManager, NULL, CLSCTX_ALL, IID_IMbnConnectionProfileManager,
  1318. (LPVOID*)&pCntProfileMnr);
  1319. if(FAILED(result))
  1320. {
  1321. SetInteralError(result);
  1322. Dbg("*** ERROR: Failed to Initialize IMbnConnectionProfileManager with code 0x%08x", result);
  1323. return FALSE;
  1324. }
  1325. SAFEARRAY* cntProfiles = NULL;
  1326. ATL::CComPtr<IMbnInterface> pInterface = NULL;
  1327. if((pInterface = GetInterfaceEx(bSpecified? pInfo->csInterfaceId : m_interfaceID)) == NULL) {
  1328. Dbg("WARN: GetInterface failed !");
  1329. return FALSE;
  1330. }
  1331. //{
  1332. // BSTR bsInterfaceID;
  1333. // if(SUCCEEDED(pInterface->get_InterfaceID(&bsInterfaceID))) {
  1334. // CSimpleStringA csInterfaceID = ConvertToString(bsInterfaceID);
  1335. // Dbg("the id: %s", (LPCTSTR)csInterfaceID);
  1336. // }
  1337. //}
  1338. result = pCntProfileMnr->GetConnectionProfiles(pInterface, &cntProfiles);
  1339. if(FAILED(result))
  1340. {
  1341. SetInteralError(result);
  1342. if(result == HRESULT_FROM_WIN32(ERROR_NOT_FOUND)) {
  1343. SetTmpState(MBD_NO_PROFILE);
  1344. Dbg("***ERROR: GetConnectionProfiles >>> cannot found any profiles maybe is 3g dongle");
  1345. }
  1346. else if(result == E_HANDLE) {
  1347. Dbg("***ERROR: GetConnectionProfiles The interface is invalid 0x%08x", result);
  1348. }
  1349. else if(result == HRESULT_FROM_WIN32(ERROR_NOT_READY)) {
  1350. Dbg("Unable to obtain the subscriber ID because the device is not MBN_READY_STATE_INITIALIZED");
  1351. }
  1352. else {
  1353. Dbg("***ERROR: GetConnectionProfiles with code 0x%08x", result);
  1354. }
  1355. return FALSE;
  1356. }
  1357. LONG lBound, hBound;
  1358. result = SafeArrayGetLBound(cntProfiles, 1, &lBound);
  1359. if(FAILED(result))
  1360. {
  1361. SetInteralError(result);
  1362. Dbg("***ERROR: Failed to retrieve First profile with code 0x%08x", result);
  1363. return FALSE;
  1364. }
  1365. result = SafeArrayGetUBound(cntProfiles, 1, &hBound);
  1366. if(FAILED(result))
  1367. {
  1368. SetInteralError(result);
  1369. Dbg("***ERROR: Failed to retrieve End profile with code 0x%08x", result);
  1370. return FALSE;
  1371. }
  1372. if(lBound > hBound) {
  1373. result = HRESULT_FROM_WIN32(ERROR_NOT_FOUND);
  1374. Dbg("***Warning: There are no profile been found !!!");
  1375. SetTmpState(MBD_NO_PROFILE);
  1376. return FALSE;
  1377. }
  1378. Dbg("profile bound: %d, %d", lBound, hBound);
  1379. MBNProfileList tmpList;
  1380. tmpList.Init(hBound - lBound + 1 );
  1381. int start=0;
  1382. ATL::CComPtr<IMbnConnectionProfile> pCntProfile = NULL;
  1383. BOOL bFouned = FALSE;
  1384. SetTmpState(MBD_NO_PROFILE);
  1385. for(LONG index=lBound; index<=hBound; index++)
  1386. {
  1387. int cur = index - lBound;
  1388. pCntProfile = NULL;
  1389. result = SafeArrayGetElement(cntProfiles, &index, (void*)(&pCntProfile));
  1390. if(FAILED(result))
  1391. {
  1392. SetInteralError(result);
  1393. Dbg("SafeArrayGetElement for index#%d failed: 0x%X", cur, result);
  1394. continue;
  1395. }
  1396. const ptMbnProfileInfo pCurPf = &(tmpList[start]);
  1397. InitAccessKeyInfo(&pCurPf->AccessInfo);
  1398. pCurPf->AccessInfo.usCntType = CNT_MBN;
  1399. BSTR strProfile;
  1400. result = pCntProfile->GetProfileXmlData(&strProfile);
  1401. if(SUCCEEDED(result))
  1402. {
  1403. m_csXmlData = ConvertToString(strProfile);
  1404. FreeBString(strProfile);
  1405. //Dbg("****************************************");
  1406. //Dbg("%s", (LPCTSTR)m_csXmlData);
  1407. //Dbg("****************************************");
  1408. tinyxml2::XMLDocument xmlData;
  1409. if(xmlData.Parse(m_csXmlData) != tinyxml2::XML_SUCCESS) {
  1410. Dbg("ERROR: xml parse failed: %d", xmlData.ErrorID());
  1411. SetInteralError((LRESULT)xmlData.ErrorID());
  1412. } else {
  1413. tinyxml2::XMLElement* root = NULL;
  1414. BOOL bExt = FALSE;
  1415. if((root = xmlData.FirstChildElement("MBNProfileExt")) == NULL) {
  1416. root = xmlData.FirstChildElement("MBNProfile");
  1417. } else {
  1418. bExt = TRUE;
  1419. }
  1420. if(root != NULL) {
  1421. if(root->FirstChildElement("SubscriberID") != NULL) {
  1422. if(pInfo->subInfo.strIMSI.GetLength() > 0) {
  1423. CSimpleStringA strIMSI = root->FirstChildElement("SubscriberID")->GetText();
  1424. if(strIMSI.Compare(pInfo->subInfo.strIMSI)) {
  1425. Dbg("profile[%d]: Ignore it: [%s] vs [%s]", cur, (LPCTSTR)pInfo->subInfo.strIMSI, (LPCTSTR)strIMSI);
  1426. SetTmpState(MBD_NO_PROFILE);
  1427. continue;
  1428. }
  1429. }
  1430. }
  1431. if(root->FirstChildElement("ProfileCreationType") != NULL) {
  1432. const char * defineType = root->FirstChildElement("ProfileCreationType")->GetText();
  1433. if(!_strnicmp(defineType, "UserProvisioned", strlen("UserProvisioned"))) {
  1434. pCurPf->bUserDefined = true;
  1435. } else {
  1436. pCurPf->bUserDefined = false;
  1437. Dbg("profile[%d]: Ignore it: ProfileCreationType: %s", cur, defineType);
  1438. SetTmpState(MBD_NO_PROFILE);
  1439. continue;
  1440. }
  1441. }
  1442. if(root->FirstChildElement("Name") != NULL) {
  1443. pCurPf->strProfileName = root->FirstChildElement("Name")->GetText();
  1444. Dbg("profile[%d]: profileName:%s", cur, (LPCTSTR)pCurPf->strProfileName);
  1445. }
  1446. if(root->FirstChildElement( "Description" ) != NULL) {
  1447. pCurPf->AccessInfo.strProfileDesc = root->FirstChildElement( "Description" )->GetText();
  1448. } else if(!bExt) {
  1449. pCurPf->AccessInfo.strProfileDesc = pCurPf->strProfileName;
  1450. }
  1451. if(root->FirstChildElement("IsDefault") != NULL) {
  1452. CSimpleStringA csDefault = root->FirstChildElement("IsDefault")->GetText();
  1453. if(!csDefault.Compare("true", true)) {
  1454. pCurPf->AccessInfo.IsDefault = TRUE;
  1455. }
  1456. }
  1457. //Win10: 拨号的文件一定要处于“已应用”的状态才能成功拨号。。
  1458. if(!bSpecified) {
  1459. if(pCurPf->bUserDefined) {
  1460. m_csProfileName = pCurPf->strProfileName;
  1461. Dbg("Get ProfileNameEx[%s] suc.", (LPCTSTR)m_csProfileName);
  1462. bFouned = TRUE;
  1463. break;
  1464. }
  1465. else {
  1466. Dbg("ProfileNameEx[%d]: [%s]", cur, (LPCTSTR)pCurPf->strProfileName);
  1467. }
  1468. }
  1469. //Dbg("serious problem?");
  1470. tinyxml2::XMLElement* context = root->FirstChildElement("Context");
  1471. if(context != NULL) {
  1472. //Dbg("serious context?");
  1473. if(context->FirstChildElement("AuthProtocol") != NULL)
  1474. pCurPf->AccessInfo.strAuthProtocol = context->FirstChildElement("AuthProtocol")->GetText();
  1475. if(context->FirstChildElement("IPType") != NULL)
  1476. pCurPf->AccessInfo.strIPType = context->FirstChildElement("IPType")->GetText();
  1477. if(context->FirstChildElement("AccessString") != NULL)
  1478. pCurPf->AccessInfo.strAccessString = context->FirstChildElement("AccessString")->GetText();
  1479. tinyxml2::XMLElement* usercred = context->FirstChildElement("UserLogonCred");
  1480. if(usercred != NULL) {
  1481. //Dbg("serious usercred?");
  1482. tinyxml2::XMLElement* username = usercred->FirstChildElement("UserName");
  1483. if(username != NULL) {
  1484. pCurPf->AccessInfo.strUserName = username->GetText();
  1485. }
  1486. tinyxml2::XMLElement* password = usercred->FirstChildElement("Password");
  1487. if(password != NULL) {
  1488. pCurPf->AccessInfo.strPassword = password->GetText();
  1489. Dbg("Password: %s", (LPCTSTR)pCurPf->AccessInfo.strPassword);
  1490. } else {
  1491. CSmartPointer<IConfigInfo> spConfigRun;
  1492. m_pFSM->GetEntityBase()->GetFunction()->OpenConfig(Config_Run, spConfigRun);
  1493. CSimpleStringA csValue;
  1494. if(Error_Succeed == spConfigRun->ReadConfigValue(pCurPf->strProfileName, "Status", csValue)
  1495. && csValue.GetLength() > 0) {
  1496. if(csValue.IsStartWith("Invalid")) {
  1497. Dbg("the runcfg record for %s is invalid : %s", (LPCTSTR)pCurPf->strProfileName, (LPCTSTR)csValue);
  1498. pCurPf->AccessInfo.strPassword = DEFAULT_PASSWORD_CHECK;
  1499. } else {
  1500. #ifdef USING_ENCRYPT
  1501. spConfigRun->ReadConfigValue(pCurPf->strProfileName, "Password", csValue);
  1502. pCurPf->AccessInfo.strPassword = DecryptString(csValue);
  1503. #else
  1504. spConfigRun->ReadConfigValue(pCurPf->strProfileName, "Password", pCurPf->AccessInfo.strPassword);
  1505. #endif
  1506. if(!bExt) {
  1507. spConfigRun->ReadConfigValue(pCurPf->strProfileName, "FriendlyName", csValue);
  1508. if(!csValue.IsNullOrEmpty()) {
  1509. pCurPf->AccessInfo.strProfileDesc = csValue;
  1510. Dbg("Get the friendlyName from runcfg: %s", (LPCTSTR)pCurPf->AccessInfo.strProfileDesc);
  1511. }
  1512. }
  1513. }
  1514. }else {
  1515. Dbg("runcfg is not exist mbn record for %s", (LPCTSTR)pCurPf->strProfileName);
  1516. pCurPf->AccessInfo.strPassword = DEFAULT_PASSWORD_CHECK;
  1517. }
  1518. }
  1519. }
  1520. }
  1521. start++;
  1522. }
  1523. }
  1524. } else {
  1525. SetInteralError(result);
  1526. Dbg("Invoke GetProfileXmlData failed return 0x%x", result);
  1527. }
  1528. }
  1529. Dbg("Finally get valid profile's count: %d", start);
  1530. if(bSpecified && start > 0) {
  1531. pInfo->profileList.Init(start);
  1532. pInfo->profileList.Copy(tmpList, 0, start);
  1533. }
  1534. tmpList.Clear();
  1535. if(cntProfiles) {
  1536. SafeArrayDestroy(cntProfiles);
  1537. cntProfiles = NULL;
  1538. }
  1539. if((bSpecified && start > 0) || bFouned) {
  1540. return TRUE;
  1541. }
  1542. return FALSE;
  1543. }
  1544. BOOL CMbnImplHelper::Connect()
  1545. {
  1546. LOG_FUNCTION();
  1547. m_bDialFromDialog = FALSE;
  1548. if(!GetConnectState()) {
  1549. Dbg("Get ConnectState failed.");
  1550. return FALSE;
  1551. }
  1552. if(m_cntState != MBN_ACTIVATION_STATE_DEACTIVATED) {
  1553. Dbg(">>>Connect interrupt because %s", ConnectStateToString(m_cntState));
  1554. if(m_cntState == MBN_ACTIVATION_STATE_ACTIVATED || m_cntState == MBN_ACTIVATION_STATE_ACTIVATING) {
  1555. Dbg("Due to CntStatus, connect suc previously !!!");
  1556. return TRUE;
  1557. }
  1558. } else {
  1559. Dbg("Try to Connecting...");
  1560. HRESULT result = S_OK;
  1561. ATL::CComPtr<IMbnInterface> pInterface = GetInterfaceEx(m_interfaceID);
  1562. if(pInterface == NULL) {
  1563. SetStatusFromTmp2Main();
  1564. return FALSE;
  1565. }
  1566. ATL::CComPtr<IMbnConnection> pConnection = NULL;
  1567. result = pInterface->GetConnection(&pConnection);
  1568. if(FAILED(result)) {
  1569. SetInteralError(result);
  1570. SetStatusFromTmp2Main();
  1571. Dbg("***ERROR: Failed to retrieve mbnConnection with code 0x%08x", result);
  1572. return FALSE;
  1573. }
  1574. if(!SearchForProfileName()) {
  1575. SetStatusFromTmp2Main();
  1576. return FALSE;
  1577. }
  1578. ULONG requestID;
  1579. result = pConnection->Connect(MBN_CONNECTION_MODE_PROFILE,
  1580. (LPCWSTR)CSimpleStringA2W(m_csProfileName), &requestID);
  1581. if(SUCCEEDED(result)) {
  1582. Dbg("Connect successfully with ProfileName[%s][%u]", (LPCTSTR)m_csProfileName, requestID);
  1583. m_ulTempID = requestID;
  1584. //等待监听结果或者直接调用GetConnectionState函数
  1585. SetCurState(MBD_ACTIVATION_CONNECTING);
  1586. return TRUE;
  1587. } else {
  1588. Dbg("Connect failed with profileName[%s]", (LPCTSTR)m_csProfileName);
  1589. SetCurState(MBD_CONNECTION_FAILURE);
  1590. switch(result)
  1591. {
  1592. case E_HANDLE :
  1593. {
  1594. Dbg("***ERROR: Invalid interface. Most likely the device has been removed from the system.");
  1595. ClearCurInterface();
  1596. }
  1597. break;
  1598. case E_INVALIDARG :
  1599. {//hardly.
  1600. Dbg("***ERROR: An invalid profile name was specified or the argument is not compliant to XML schema.");
  1601. }
  1602. break;
  1603. case E_MBN_MAX_ACTIVATED_CONTEXTS:
  1604. {
  1605. SetCurState(MBD_CONNECTION_MAX_CONTEXTS);
  1606. Dbg("***ERROR: There is already an active context. Multiple active contexts are not supported.");
  1607. }
  1608. break;
  1609. default:
  1610. if(result == HRESULT_FROM_WIN32(ERROR_SERVICE_NOT_ACTIVE)) {
  1611. Dbg("***ERROR: The Mobile Broadband service is not running on this system.");
  1612. } else if(result == HRESULT_FROM_WIN32(ERROR_NOT_FOUND)) {
  1613. Dbg("***ERROR: Invalid interface. Most likely the device has been removed from the system.");
  1614. ClearCurInterface();
  1615. } else {
  1616. Dbg("***ERROR: Connect failed with ProfileName[%s] return 0x%x", (LPCTSTR)m_csProfileName, result);
  1617. }
  1618. break;
  1619. }
  1620. }
  1621. }
  1622. return FALSE;
  1623. }
  1624. ULONG CMbnImplHelper::ConnectEx2(LPCTSTR lpcszInterface, LPCTSTR lpcszProfileName)
  1625. {
  1626. LOG_FUNCTION();
  1627. if(!GetConnectState()) {
  1628. Dbg("Get ConnectState failed.");
  1629. m_pFSM->m_csErrMsg = "获取网卡连接状态失败";
  1630. return -1;
  1631. }
  1632. if(m_cntState != MBN_ACTIVATION_STATE_DEACTIVATED) {
  1633. Dbg(">>>Connect interrupt because %s", ConnectStateToString(m_cntState));
  1634. if(m_cntState == MBN_ACTIVATION_STATE_ACTIVATED || m_cntState == MBN_ACTIVATION_STATE_ACTIVATING) {
  1635. Dbg("Due to CntStatus, connect suc previously !!!");
  1636. return -1;
  1637. }
  1638. } else {
  1639. Dbg("Try to Connecting...");
  1640. HRESULT result = S_OK;
  1641. ATL::CComPtr<IMbnConnection> pConnection = NULL;
  1642. ATL::CComPtr<IMbnInterface> pInterface;
  1643. if(!GetInterface(lpcszInterface)) {
  1644. Dbg("WARN: GetInterface failed !");
  1645. m_pFSM->m_csErrMsg = "获取网卡接口句柄失败";
  1646. SetStatusFromTmp2Main();
  1647. return -1;
  1648. }
  1649. pInterface = m_pTmpInterface;
  1650. result = pInterface->GetConnection(&pConnection);
  1651. if(FAILED(result)) {
  1652. //Can ignore hardly happen
  1653. m_pFSM->m_csErrMsg = "获取网卡连接句柄失败";
  1654. SetInteralError(result);
  1655. SetStatusFromTmp2Main();
  1656. Dbg("***ERROR: Failed to retrieve mbnConnection with code 0x%08x", result);
  1657. return -1;
  1658. }
  1659. ULONG requestID;
  1660. CSimpleStringA csProfileName(lpcszProfileName);
  1661. result = pConnection->Connect(MBN_CONNECTION_MODE_PROFILE,
  1662. (LPCWSTR)CSimpleStringA2W(csProfileName), &requestID);
  1663. if(SUCCEEDED(result)) {
  1664. Dbg("ConnectEx successfully with ProfileName[%s][%u]", (LPCTSTR)csProfileName, requestID);
  1665. m_ulTempID = requestID;
  1666. m_bDialFromDialog = TRUE;
  1667. SetCurState(MBD_ACTIVATION_CONNECTING);
  1668. return requestID;
  1669. } else {
  1670. SetCurState(MBD_CONNECTION_FAILURE);
  1671. switch(result) {
  1672. case E_HANDLE :
  1673. {
  1674. m_pFSM->m_csErrMsg = "无效接口";
  1675. Dbg("***ERROR: Invalid interface. Most likely the device has been removed from the system.");
  1676. }
  1677. break;
  1678. case E_INVALIDARG :
  1679. {
  1680. m_pFSM->m_csErrMsg = "无效的连接配置文件";
  1681. Dbg("***ERROR: An invalid profile name was specified or the argument is not compliant to XML schema.");
  1682. }
  1683. break;
  1684. case E_MBN_MAX_ACTIVATED_CONTEXTS:
  1685. {
  1686. SetCurState(MBD_CONNECTION_MAX_CONTEXTS);
  1687. m_pFSM->m_csErrMsg = "不支持多个激活上下文";
  1688. Dbg("***ERROR: There is already an active context. Multiple active contexts are not supported.");
  1689. }
  1690. break;
  1691. default:
  1692. m_pFSM->m_csErrMsg = CSimpleStringA::Format("MBN拨号连接失败:0x%X", result);
  1693. if(result == HRESULT_FROM_WIN32(ERROR_SERVICE_NOT_ACTIVE)) {
  1694. Dbg("***ERROR: The Mobile Broadband service is not running on this system.");
  1695. }
  1696. else if(result == HRESULT_FROM_WIN32(ERROR_NOT_FOUND)) {
  1697. Dbg("***ERROR: Invalid interface. Most likely the device has been removed from the system.");
  1698. }
  1699. else {
  1700. Dbg("***ERROR: Connect failed with ProfileName[%s] return 0x%x", (LPCTSTR)csProfileName, result);
  1701. }
  1702. break;
  1703. }
  1704. }
  1705. }
  1706. return -1;
  1707. }
  1708. BOOL CMbnImplHelper::ConnectEx(LPCTSTR lpcszInterface, LPCTSTR lpcszProfileName)
  1709. {
  1710. LOG_FUNCTION();
  1711. if(!GetConnectState()) {
  1712. Dbg("Get ConnectState failed.");
  1713. m_pFSM->m_csErrMsg = "获取网卡连接状态失败";
  1714. return FALSE;
  1715. }
  1716. if(m_cntState != MBN_ACTIVATION_STATE_DEACTIVATED) {
  1717. Dbg(">>>Connect interrupt because %s", ConnectStateToString(m_cntState));
  1718. if(m_cntState == MBN_ACTIVATION_STATE_ACTIVATED || m_cntState == MBN_ACTIVATION_STATE_ACTIVATING) {
  1719. Dbg("Due to CntStatus, connect suc previously !!!");
  1720. return TRUE;
  1721. }
  1722. } else {
  1723. Dbg("Try to Connecting...");
  1724. HRESULT result = S_OK;
  1725. ATL::CComPtr<IMbnConnection> pConnection = NULL;
  1726. ATL::CComPtr<IMbnInterface> pInterface;
  1727. if(!GetInterface(lpcszInterface)) {
  1728. Dbg("WARN: GetInterface failed !");
  1729. m_pFSM->m_csErrMsg = "获取网卡接口句柄失败";
  1730. SetStatusFromTmp2Main();
  1731. return FALSE;
  1732. }
  1733. pInterface = m_pTmpInterface;
  1734. result = pInterface->GetConnection(&pConnection);
  1735. if(FAILED(result)) {
  1736. //Can ignore hardly happen
  1737. m_pFSM->m_csErrMsg = "获取网卡连接句柄失败";
  1738. SetInteralError(result);
  1739. SetStatusFromTmp2Main();
  1740. Dbg("***ERROR: Failed to retrieve mbnConnection with code 0x%08x", result);
  1741. return FALSE;
  1742. }
  1743. ULONG requestID;
  1744. CSimpleStringA csProfileName(lpcszProfileName);
  1745. result = pConnection->Connect(MBN_CONNECTION_MODE_PROFILE,
  1746. (LPCWSTR)CSimpleStringA2W(csProfileName), &requestID);
  1747. if(SUCCEEDED(result)) {
  1748. Dbg("ConnectEx successfully with ProfileName[%s][%u]", (LPCTSTR)csProfileName, requestID);
  1749. m_ulTempID = requestID;
  1750. m_bDialFromDialog = TRUE;
  1751. SetCurState(MBD_ACTIVATION_CONNECTING);
  1752. return TRUE;
  1753. } else {
  1754. SetCurState(MBD_CONNECTION_FAILURE);
  1755. switch(result) {
  1756. case E_HANDLE :
  1757. {
  1758. m_pFSM->m_csErrMsg = "无效接口";
  1759. Dbg("***ERROR: Invalid interface. Most likely the device has been removed from the system.");
  1760. }
  1761. break;
  1762. case E_INVALIDARG :
  1763. {
  1764. m_pFSM->m_csErrMsg = "无效的连接配置文件";
  1765. Dbg("***ERROR: An invalid profile name was specified or the argument is not compliant to XML schema.");
  1766. }
  1767. break;
  1768. case E_MBN_MAX_ACTIVATED_CONTEXTS:
  1769. {
  1770. SetCurState(MBD_CONNECTION_MAX_CONTEXTS);
  1771. m_pFSM->m_csErrMsg = "不支持多个激活上下文";
  1772. Dbg("***ERROR: There is already an active context. Multiple active contexts are not supported.");
  1773. }
  1774. break;
  1775. default:
  1776. m_pFSM->m_csErrMsg = CSimpleStringA::Format("MBN拨号连接失败:0x%X", result);
  1777. if(result == HRESULT_FROM_WIN32(ERROR_SERVICE_NOT_ACTIVE)) {
  1778. Dbg("***ERROR: The Mobile Broadband service is not running on this system.");
  1779. }
  1780. else if(result == HRESULT_FROM_WIN32(ERROR_NOT_FOUND)) {
  1781. Dbg("***ERROR: Invalid interface. Most likely the device has been removed from the system.");
  1782. }
  1783. else {
  1784. Dbg("***ERROR: Connect failed with ProfileName[%s] return 0x%x", (LPCTSTR)csProfileName, result);
  1785. }
  1786. break;
  1787. }
  1788. }
  1789. }
  1790. return FALSE;
  1791. }
  1792. BOOL CMbnImplHelper::DisConnect(BOOL bManual, LPCTSTR lpszInterface)
  1793. {
  1794. m_bDialFromDialog = bManual;
  1795. Dbg("Set m_bDialFromDialog with %d", m_bDialFromDialog);
  1796. if(!GetConnectState()) {
  1797. Dbg("Get ConnectState failed.");
  1798. m_pFSM->m_csErrMsg = "获取连接状态失败";
  1799. return FALSE;
  1800. }
  1801. if(m_cntState != MBN_ACTIVATION_STATE_ACTIVATED) {
  1802. Dbg(">>>Connect interrupt because %s", ConnectStateToString(m_cntState));
  1803. m_pFSM->m_csErrMsg = ConnectStateToString(m_cntState);
  1804. } else {
  1805. m_ulTempID = (ULONG)-1;
  1806. if(m_ulRequestID == ULONG(-1)) {
  1807. Dbg("The request ID is invalid, most likely connect manully before.");
  1808. m_pFSM->m_csErrMsg = "拨号请求ID丢失,可能此次连接并不是由本应用发起的";
  1809. if(!m_pFSM->GetPersistentRequestId(m_ulTempID)) {
  1810. return FALSE;
  1811. }
  1812. m_ulRequestID = m_ulTempID;
  1813. }
  1814. assert(!bManual || !m_interfaceID.Compare(lpszInterface));
  1815. ATL::CComPtr<IMbnInterface> pInterface = GetInterfaceEx(m_interfaceID);
  1816. if(pInterface == NULL) {
  1817. return FALSE;
  1818. }
  1819. HRESULT result = S_OK;
  1820. ATL::CComPtr<IMbnConnection> pConnection = NULL;
  1821. result = pInterface->GetConnection(&pConnection);
  1822. if(FAILED(result)) {
  1823. //Can igonre because hardly happen.
  1824. m_pFSM->m_csErrMsg = "获取网卡连接句柄失败";
  1825. Dbg("***ERROR: Failed to retrieve mbnConnection with code 0x%08x", result);
  1826. SetInteralError(result);
  1827. return FALSE;
  1828. }
  1829. result = pConnection->Disconnect(&m_ulRequestID);
  1830. if(FAILED(result))
  1831. {
  1832. if(m_ulTempID != (ULONG)-1) {
  1833. m_pFSM->m_csErrMsg = CSimpleStringA::Format(
  1834. "拨号请求ID丢失,可能此次连接并不是由本应用发起的:0x%X", result);
  1835. m_ulRequestID = (ULONG)-1;
  1836. return FALSE;
  1837. }
  1838. //Can igonre because hardly happen.
  1839. m_pFSM->m_csErrMsg = CSimpleStringA::Format("0x%X", result);
  1840. SetInteralError(result);
  1841. if(result == HRESULT_FROM_WIN32(ERROR_NOT_FOUND)) {
  1842. m_pFSM->m_csErrMsg.Clear();
  1843. Dbg("***Warning: The connection has already been disconnected.");
  1844. return TRUE;
  1845. } else {
  1846. Dbg("Disconnect with [RequestID#%d] failed return 0x%x", m_ulRequestID, result);
  1847. }
  1848. }
  1849. else {
  1850. m_pFSM->m_csErrMsg.Clear();
  1851. Dbg("Disconnect suc.");
  1852. SetCurState(MBD_ACTIVATION_DISCONNECTING);
  1853. return TRUE;
  1854. }
  1855. }
  1856. return FALSE;
  1857. }
  1858. void CMbnImplHelper::OnConnectComplete(ULONG requestID, LPCTSTR newInterface, HRESULT status)
  1859. {
  1860. #ifdef TWINKLE_LOCAL_DEBUG
  1861. #define Dbg(fmt, ...) \
  1862. TwDbg(fmt, ##__VA_ARGS__); \
  1863. printf(fmt "\n", ##__VA_ARGS__)
  1864. #endif
  1865. Dbg(">>>OnConnectionComplete: newInterface: %s, requestID: %d status 0x%08x", newInterface, requestID, status);
  1866. int nFormattedCode = 0;
  1867. BOOL exceptConnectActionFlag = FALSE;
  1868. if(requestID == 0)
  1869. {
  1870. Dbg("New condition: m_interfaceID[%s] newInterface[%s]", (LPCTSTR)m_interfaceID, newInterface);
  1871. if(!m_interfaceID.IsNullOrEmpty() && m_interfaceID.Compare(newInterface) == 0 && status == S_OK) {
  1872. exceptConnectActionFlag = TRUE;
  1873. } else {
  1874. return;
  1875. }
  1876. }
  1877. FSMEvent* fsmEvent = new FSMEvent(USER_EVT_CONNECT_COMPLETE);
  1878. if(GetCurState() == MBD_ACTIVATION_CONNECTING && requestID == m_ulTempID)
  1879. {
  1880. Dbg("Receive the specified ConnectComplete Event.");
  1881. fsmEvent->param2 = 0;
  1882. }
  1883. else if(m_bSelected && m_interfaceID == newInterface)
  1884. {
  1885. Dbg("Most likely Connect manually.");
  1886. fsmEvent->param2 = 1;
  1887. }
  1888. else
  1889. {
  1890. fsmEvent->param2 = -1;
  1891. Dbg("Disgusting thing happen. Josephtest m_interfaceID[%s] m_ulRequestID[%d]",
  1892. (LPCTSTR)m_interfaceID, m_ulRequestID);
  1893. if(!exceptConnectActionFlag) {
  1894. Dbg("Set it as the new king...");
  1895. SetCurInterface(newInterface);
  1896. }
  1897. }
  1898. if(status == S_OK)
  1899. {
  1900. //连接成功
  1901. fsmEvent->param1 = 0;
  1902. SetRequestID(requestID);
  1903. // m_ulRequestID = requestID;
  1904. m_ulTempID = (ULONG)-1;
  1905. SetCurState(MBD_ACTIVATION_CONNECTED);
  1906. }
  1907. else
  1908. {
  1909. //连接失败
  1910. fsmEvent->param1 = 1;
  1911. m_ulTempID = (ULONG)-1;
  1912. if(fsmEvent->param2 == 0) {
  1913. //清理掉最近拨号成功的记录
  1914. m_pFSM->EraseTheRecentInfo(requestID);
  1915. }
  1916. if(status == E_MBN_SIM_NOT_INSERTED)
  1917. {
  1918. SetCurState(MBD_READY_SIM_NO_INSERT);
  1919. }
  1920. else if(status == E_MBN_RADIO_POWER_OFF)
  1921. {
  1922. SetCurState(MBD_POWER_OFF);
  1923. }
  1924. else if(status == E_MBN_MAX_ACTIVATED_CONTEXTS)
  1925. {
  1926. SetCurState(MBD_CONNECTION_MAX_CONTEXTS);
  1927. }
  1928. else if(status == E_MBN_INVALID_ACCESS_STRING)
  1929. {
  1930. SetCurState(MBD_CONNECTION_INVALID_ACCESS);
  1931. }
  1932. else if(status == E_MBN_PIN_REQUIRED)
  1933. {
  1934. SetCurState(MBD_CONNECTION_PIN_REQUIRED);
  1935. }
  1936. //TODO: delete "|| status == E_MBN_FAILURE" ?? 2016-9-23
  1937. // Eventually Cancel it. [Josephus in 7:54:05 2016/9/28]
  1938. else if(status == HRESULT_FROM_WIN32(ERROR_INVALID_PASSWORD)/* || status == E_MBN_FAILURE*/)
  1939. {
  1940. SetCurState(MBD_CONNECTION_INVALID_NAMEORPASSWORD);
  1941. }
  1942. else
  1943. {
  1944. SetMBNCode(status);
  1945. SetCurState(MBD_CONNECTION_FAILURE);
  1946. Dbg("Connection Complete return error status 0x08%x", status);
  1947. }
  1948. }
  1949. if(m_bDialFromDialog) {
  1950. m_bDialFromDialog = FALSE;
  1951. if(status == S_OK) {
  1952. m_pFSM->TellDlgTheResult(Error_Succeed, WM_DIAL_RESULT, 4);
  1953. m_pFSM->m_csErrMsg.Clear();
  1954. Dbg("Confirm connection is succ, reset current interface.");
  1955. SetCurInterface(newInterface);
  1956. } else {
  1957. m_pFSM->m_csErrMsg = CSimpleStringA::Format("0x%X", status);
  1958. m_pFSM->TellDlgTheResult(Error_FailVerify, WM_DIAL_RESULT, 4);
  1959. }
  1960. } else {
  1961. m_pFSM->TellDlgTheResult(status == S_OK ? Error_Succeed : Error_FailVerify, WM_DIAL_RESULT, -4);
  1962. }
  1963. m_pFSM->PostEventLIFO(fsmEvent);
  1964. #ifdef TWINKLE_LOCAL_DEBUG
  1965. #undef Dbg
  1966. #endif
  1967. }
  1968. void CMbnImplHelper::OnDisconnectComplete(ULONG requestID, LPCTSTR newInterface)
  1969. {
  1970. Dbg(">>>OnDisconnectComplete: requestID: %d", requestID);
  1971. if(requestID == 0 && m_interfaceID.Compare(newInterface) != 0)
  1972. {
  1973. Dbg("New condition: m_interfaceID[%s] newInterface[%s]", (LPCTSTR)m_interfaceID, newInterface);
  1974. return;
  1975. }
  1976. if(m_bDialFromDialog) {
  1977. m_bDialFromDialog = FALSE;
  1978. m_pFSM->TellDlgTheResult(Error_Succeed, WM_DIAL_DISCONNECT, 1);
  1979. } else {
  1980. m_pFSM->TellDlgTheResult(Error_Succeed, WM_DIAL_DISCONNECT, 0);
  1981. }
  1982. if(requestID == m_ulRequestID)
  1983. {
  1984. Dbg("Receive the specified DisconnectComplete Event.");
  1985. SetRequestID();
  1986. SetCurState(MBD_ACTIVATION_DISCONNECTED);
  1987. FSMEvent* fsmEvent = new FSMEvent(USER_EVT_DISCONNECT_COMPLETE);
  1988. fsmEvent->param1 = (int)requestID;
  1989. fsmEvent->param2 = ADVANCE_MBN;
  1990. m_pFSM->PostEventLIFO(fsmEvent);
  1991. return;
  1992. }
  1993. else if(m_interfaceID == newInterface)
  1994. {
  1995. Dbg("Detect it connected manully before InterfaceID[%s].", newInterface);
  1996. SetCurState(MBD_ACTIVATION_DISCONNECTED);
  1997. FSMEvent* fsmEvent = new FSMEvent(USER_EVT_DISCONNECT_COMPLETE);
  1998. fsmEvent->param1 = (int)requestID;
  1999. fsmEvent->param2 = ADVANCE_MBN;
  2000. m_pFSM->PostEventLIFO(fsmEvent);
  2001. return;
  2002. }
  2003. Dbg("Disgusting thing happen, bSelected[%d] newInterface[%s]", m_bSelected, newInterface);
  2004. }
  2005. void CMbnImplHelper::OnConnectStateChange(HRESULT hr, MBN_ACTIVATION_STATE state, LPCTSTR newInterface)
  2006. {
  2007. if(m_bSelected && m_interfaceID == newInterface)
  2008. {
  2009. if(FAILED(hr))
  2010. {
  2011. if(hr == E_MBN_SIM_NOT_INSERTED)
  2012. {
  2013. Dbg("***OnConnectStateChange: A SIM is not inserted in the device.");
  2014. SetCurState(MBD_READY_SIM_NO_INSERT);
  2015. }
  2016. else if(hr == E_MBN_PIN_REQUIRED)
  2017. {
  2018. Dbg("***OnConnectStateChange: A PIN is required to get the call state.");
  2019. SetCurState(MBD_CONNECTION_PIN_REQUIRED);
  2020. }
  2021. else if(hr == E_MBN_BAD_SIM)
  2022. {
  2023. Dbg("***OnConnectStateChange: A bad SIM is inserted in the device.");
  2024. SetCurState(MBD_READY_BAD_SIM);
  2025. }
  2026. else if(hr == E_PENDING)
  2027. {
  2028. //SetCurState(MBD_UNKNOWN);
  2029. Dbg("***OnConnectStateChange: The activation state not available");
  2030. }
  2031. }
  2032. else
  2033. {
  2034. SetCurState((MOBILEDIAL_CONNECTION_STATE)((int)MBD_ACTIVATION_NONE + state));
  2035. Dbg("***OnConnectStateChange: %s", ConnectStateToString(state));
  2036. }
  2037. }
  2038. }
  2039. void CMbnImplHelper::OnConnectionArrival(LPCTSTR newInterface)
  2040. {
  2041. Dbg(">>>OnConnectionArrival: %s", newInterface);
  2042. if(!CheckAndRefleshCurInterface()){
  2043. //Sleep(2500);
  2044. if(SetCurInterface(newInterface)){
  2045. if(m_pFSM){
  2046. FSMEvent* fsmEvent = new FSMEvent(USER_EVT_DEVICE_ARRIVAL);
  2047. fsmEvent->param1 = 1;
  2048. fsmEvent->param2 = 4;
  2049. m_pFSM->PostEventFIFO(fsmEvent);
  2050. }
  2051. }
  2052. }
  2053. m_pFSM->TellDlgTheResult(Error_Succeed, WM_DONGLE_CHANGE, 1);
  2054. }
  2055. void CMbnImplHelper::OnConnectionRemoval(LPCTSTR newInterface)
  2056. {
  2057. Dbg(">>>OnConnectionRemoval: %s", newInterface);
  2058. if(m_bSelected && m_interfaceID == newInterface)
  2059. {
  2060. Dbg("specified interface remove");
  2061. ClearCurInterface();
  2062. if(m_pFSM)
  2063. {
  2064. FSMEvent* fsmEvent = new FSMEvent(USER_EVT_DEVICE_LEAVE);
  2065. fsmEvent->param1 = 1;
  2066. fsmEvent->param2 = 4;
  2067. m_pFSM->PostEventLIFO(fsmEvent);
  2068. }
  2069. }
  2070. m_pFSM->TellDlgTheResult(Error_Succeed, WM_DONGLE_CHANGE, 0);
  2071. }
  2072. BOOL CMbnImplHelper::IsTheDealProfileEvents(IMbnConnectionProfile *profile, BOOL bSucc)
  2073. {
  2074. BSTR strProfile;
  2075. HRESULT result = profile->GetProfileXmlData(&strProfile);
  2076. if(SUCCEEDED(result)) {
  2077. CSimpleStringA content = ConvertToString(strProfile);
  2078. FreeBString(strProfile);
  2079. tinyxml2::XMLDocument xmlData;
  2080. if(xmlData.Parse(content) != tinyxml2::XML_SUCCESS) {
  2081. Dbg("ERROR: xml parse failed: %d", xmlData.ErrorID());
  2082. } else {
  2083. tinyxml2::XMLElement* root = NULL;
  2084. BOOL bExt = FALSE;
  2085. if((root = xmlData.FirstChildElement("MBNProfileExt")) == NULL) {
  2086. root = xmlData.FirstChildElement("MBNProfile");
  2087. } else {
  2088. bExt = TRUE;
  2089. }
  2090. if(root != NULL) {
  2091. CSimpleStringA csProfileName = root->FirstChildElement( "Name" )->GetText();
  2092. if(m_csTmpProfileName.GetLength() > 0 && !csProfileName.Compare(m_csTmpProfileName)) {
  2093. if(!bSucc) {
  2094. CSmartPointer<IConfigInfo> spConfigRun;
  2095. m_pFSM->GetEntityBase()->GetFunction()->OpenConfig(Config_Run, spConfigRun);
  2096. CSimpleStringA csAction;
  2097. if(Error_Succeed == spConfigRun->ReadConfigValue(m_csTmpProfileName, "Status", csAction)
  2098. && csAction.GetLength() > 0) {
  2099. CSimpleStringA csNewAction("Invalid-");
  2100. csNewAction += csAction;
  2101. spConfigRun->WriteConfigValue(m_csTmpProfileName, "Status", csNewAction);
  2102. }
  2103. }
  2104. m_csTmpProfileName = "";
  2105. return TRUE;
  2106. }else {
  2107. CSmartPointer<IConfigInfo> spConfigRun;
  2108. m_pFSM->GetEntityBase()->GetFunction()->OpenConfig(Config_Run, spConfigRun);
  2109. CSimpleStringA csAction;
  2110. if(Error_Succeed == spConfigRun->ReadConfigValue(csProfileName, "Status", csAction)
  2111. && csAction.GetLength() > 0) {
  2112. CSimpleStringA csNewAction("Invalid-OutChanged");
  2113. spConfigRun->WriteConfigValue(csProfileName, "Status", csNewAction);
  2114. }
  2115. Dbg("Maybe add/delete/update profile from system: (%s)(%s)", (LPCTSTR)m_csTmpProfileName, (LPCTSTR)csProfileName);
  2116. }
  2117. }
  2118. }
  2119. }
  2120. return FALSE;
  2121. }
  2122. void CMbnImplHelper::OnConnectionProfileArrival(IMbnConnectionProfile *newConnectionProfile)
  2123. {
  2124. Dbg(">>>OnConnectionProfileArrival !");
  2125. if(IsTheDealProfileEvents(newConnectionProfile, TRUE)) {
  2126. Dbg("Add new profile succ !");
  2127. m_pFSM->TellDlgTheResult(Error_Succeed, WM_ADD_PROFILE_RESULT, 0);
  2128. return;
  2129. }
  2130. m_pFSM->TellDlgTheResult(Error_Succeed, WM_ADD_PROFILE_RESULT, 1);
  2131. }
  2132. void CMbnImplHelper::OnConnectionProfileRemoval(IMbnConnectionProfile *oldConnectionProfile)
  2133. {
  2134. Dbg(">>>OnConnectionProfileRemoval !");
  2135. if(IsTheDealProfileEvents(oldConnectionProfile, TRUE)) {
  2136. Dbg("delete profile succ !");
  2137. m_pFSM->TellDlgTheResult(Error_Succeed, WM_DELETE_PROFILE_RESULT, 0);
  2138. return;
  2139. }
  2140. m_pFSM->TellDlgTheResult(Error_Succeed, WM_DELETE_PROFILE_RESULT, 1);
  2141. }
  2142. void CMbnImplHelper::OnProfileUpdate(IMbnConnectionProfile *newProfile)
  2143. {
  2144. Dbg(">>> OnProfileUpdate !");
  2145. if(IsTheDealProfileEvents(newProfile, TRUE)) {
  2146. Dbg("Update profile succ !");
  2147. m_pFSM->TellDlgTheResult(Error_Succeed, WM_UPDATE_PROFILE_RESULT, 0);
  2148. return;
  2149. }
  2150. m_pFSM->TellDlgTheResult(Error_Succeed, WM_UPDATE_PROFILE_RESULT, 1);
  2151. }
  2152. ErrorCodeEnum CMbnImplHelper::AddProfile(
  2153. const CSimpleStringA& csInterfaceID, const AccessKeyInfo& newInfo, bool fApplyNow)
  2154. {
  2155. CSimpleStringW content;
  2156. ErrorCodeEnum rc = _CreateProfile(csInterfaceID, newInfo, fApplyNow, false, content);
  2157. if(rc == Error_Succeed) {
  2158. //Add to profile manager
  2159. HRESULT result = S_OK;
  2160. ATL::CComPtr<IMbnConnectionProfileManager> pCntProfileMnr = NULL;
  2161. result = CoCreateInstance(CLSID_MbnConnectionProfileManager, NULL, CLSCTX_ALL, IID_IMbnConnectionProfileManager,
  2162. (LPVOID*)&pCntProfileMnr);
  2163. if(FAILED(result))
  2164. {
  2165. SetInteralError(result);
  2166. Dbg("*** ERROR: Failed to Initialize IMbnConnectionProfileManager with code 0x%08x", result);
  2167. m_pFSM->m_csErrMsg = CSimpleStringA::Format("未知错误:0x%08x", result);
  2168. return Error_NotInit;
  2169. }
  2170. CSimpleStringA csTmpProfileName(m_csTmpProfileName);
  2171. Dbg("===========================================");
  2172. Dbg("%s", (LPCTSTR)CSimpleStringW2A(content));
  2173. Dbg("===========================================");
  2174. //MessageBoxW(NULL, content, L"Content", MB_OK);
  2175. result = pCntProfileMnr->CreateConnectionProfile(content);
  2176. if(FAILED(result)) {
  2177. if(result == HRESULT_FROM_WIN32(ERROR_ALREADY_EXISTS)) {
  2178. Dbg("***ERROR: CreateConnectionProfile >>> A profile with the given name already exists.");
  2179. m_pFSM->m_csErrMsg = "该配置已存在,请重命名配置名称!";
  2180. } else if(result == E_MBN_INVALID_PROFILE) {
  2181. Dbg("***ERROR: CreateConnectionProfile >>> The profile does not compact with the Mobile Broadband profile schema.");
  2182. m_pFSM->m_csErrMsg = "配置文件有误,请检查卡托是否正常(当前网卡名称不应带\"Invalid\")!";
  2183. } else if(result == E_MBN_DEFAULT_PROFILE_EXIST) {
  2184. Dbg("***ERROR: CreateConnectionProfile >>> However the default profile already exists for the Mobile Broadband device.");
  2185. m_pFSM->m_csErrMsg = "请求设置为默认配置,但已存在默认配置!";
  2186. } else {
  2187. Dbg("***ERROR: CreateConnectionProfile with code 0x%08x", result);
  2188. m_pFSM->m_csErrMsg = CSimpleStringA::Format("未知错误:0x%08x", result);
  2189. }
  2190. return Error_Unexpect;
  2191. }
  2192. Dbg("The CreateConnectionProfile method completed successfully.");
  2193. m_pFSM->m_csErrMsg = "";
  2194. if(csTmpProfileName.GetLength() > 0)
  2195. {
  2196. CSmartPointer<IConfigInfo> spConfigRun;
  2197. m_pFSM->GetEntityBase()->GetFunction()->OpenConfig(Config_Run, spConfigRun);
  2198. spConfigRun->WriteConfigValue(csTmpProfileName, "Type", "MBN");
  2199. spConfigRun->WriteConfigValue(csTmpProfileName, "Status", "Create");
  2200. #ifdef USING_ENCRYPT
  2201. CSimpleStringA csEValue = EncryptString(newInfo.strPassword);
  2202. spConfigRun->WriteConfigValue(csTmpProfileName, "Password", csEValue);
  2203. #else
  2204. spConfigRun->WriteConfigValue(csTmpProfileName, "Password", newInfo.strPassword);
  2205. #endif
  2206. spConfigRun->WriteConfigValue(csTmpProfileName, "FriendlyName", newInfo.strProfileDesc);
  2207. spConfigRun->WriteConfigValue(csTmpProfileName, "APN", newInfo.strAccessString);
  2208. }
  2209. if(!m_bCntPfMnrEventInited) {
  2210. return Error_Succeed;
  2211. }
  2212. /*Microsoft just KengDie, the event somtimes not fires and actually the proile add action is not succ.*/
  2213. /*The dongle is connected and we cannot enumrate the profile list with provided api, KAO!*/
  2214. Dbg("pending to received add profile finished event!");
  2215. return Error_Pending;
  2216. }
  2217. Dbg("_CreateProfile failed!");
  2218. return Error_Unexpect;
  2219. }
  2220. ErrorCodeEnum CMbnImplHelper::_CreateProfile(
  2221. const CSimpleStringA& csInterfaceID, const AccessKeyInfo& newInfo, bool fApplyNow,
  2222. bool bUpdate, CSimpleStringW& csContent)
  2223. {
  2224. MbnDongleSubInfo subInfo;
  2225. memset(&subInfo, 0, sizeof(MbnDongleSubInfo));
  2226. if(!GetInterfaceInfo(csInterfaceID, &subInfo)) {
  2227. Dbg("GetInterfaceInfo failed!");
  2228. m_pFSM->m_csErrMsg = "获取网卡参数信息失败!";
  2229. return Error_InvalidState;
  2230. }
  2231. tinyxml2::XMLDocument file;
  2232. XMLDeclaration* declaration = file.NewDeclaration("xml version=\"1.0\"");
  2233. CSimpleStringA strRootElement("MBNProfileExt");
  2234. CSimpleStringA strNamespace("http://www.microsoft.com/networking/WWAN/profile/v4");
  2235. BOOL bExt = FALSE;
  2236. if(wv <= 7) {
  2237. strNamespace = "http://www.microsoft.com/networking/WWAN/profile/v1";
  2238. strRootElement = "MBNProfile";
  2239. } else if(wv == 8) {
  2240. strNamespace = "http://www.microsoft.com/networking/WWAN/profile/v1";
  2241. strRootElement = "MBNProfile";
  2242. } else if(wv >= 10) {
  2243. bExt = TRUE;
  2244. } else {
  2245. Dbg("Cannot figure out Windows version: %d", wv);
  2246. GetCurWindowsVersion();
  2247. return Error_InvalidState;
  2248. }
  2249. file.InsertFirstChild(declaration);
  2250. tinyxml2::XMLElement* root = file.NewElement(strRootElement);
  2251. root->SetAttribute("xmlns", (LPCTSTR)strNamespace);
  2252. file.InsertEndChild(root);
  2253. if(!bUpdate) {
  2254. m_csTmpProfileName = CreateGuidString(false);
  2255. } else {
  2256. assert(m_csTmpProfileName.GetLength() > 0);
  2257. }
  2258. XMLElement* Name = file.NewElement("Name");
  2259. Name->SetText((LPCTSTR)m_csTmpProfileName);
  2260. root->InsertEndChild(Name);
  2261. if(bExt) {
  2262. XMLElement* Description = file.NewElement("Description");
  2263. Description->SetText((LPCTSTR)newInfo.strProfileDesc);
  2264. root->InsertEndChild(Description);
  2265. }
  2266. XMLElement* IsDefault = file.NewElement("IsDefault");
  2267. IsDefault->SetText(fApplyNow ? "true" : "false");
  2268. root->InsertEndChild(IsDefault);
  2269. /*if(!bUpdate)*/ {
  2270. XMLElement* ProfileCreationType = file.NewElement("ProfileCreationType");
  2271. ProfileCreationType->SetText("UserProvisioned");
  2272. root->InsertEndChild(ProfileCreationType);
  2273. XMLElement* SubscriberID = file.NewElement("SubscriberID");
  2274. SubscriberID->SetText(subInfo.strIMSI);
  2275. root->InsertEndChild(SubscriberID);
  2276. XMLElement* SimIccID = file.NewElement("SimIccID");
  2277. SimIccID->SetText(subInfo.strSimIccID);
  2278. root->InsertEndChild(SimIccID);
  2279. XMLElement* HomeProviderName = file.NewElement("HomeProviderName");
  2280. HomeProviderName->SetText(subInfo.strProviderName);
  2281. root->InsertEndChild(HomeProviderName);
  2282. XMLElement* ConnectionMode = file.NewElement("ConnectionMode");
  2283. if(bExt) {
  2284. ConnectionMode->SetText("auto");
  2285. } else {
  2286. ConnectionMode->SetText("manual");
  2287. }
  2288. root->InsertEndChild(ConnectionMode);
  2289. }
  2290. XMLElement* Context = file.NewElement("Context");
  2291. root->InsertEndChild(Context);
  2292. XMLElement* AccessString = file.NewElement("AccessString");
  2293. AccessString->SetText(newInfo.strAccessString);
  2294. Context->InsertEndChild(AccessString);
  2295. XMLElement* UserLogonCred = file.NewElement("UserLogonCred");
  2296. Context->InsertEndChild(UserLogonCred);
  2297. XMLElement* UserName = file.NewElement("UserName");
  2298. UserName->SetText(newInfo.strUserName);
  2299. UserLogonCred->InsertEndChild(UserName);
  2300. if(newInfo.strPassword.GetLength() > 0) {
  2301. XMLElement* Password = file.NewElement("Password");
  2302. Password->SetText(newInfo.strPassword);
  2303. UserLogonCred->InsertEndChild(Password);
  2304. }
  2305. XMLElement* Compression = file.NewElement("Compression");
  2306. Compression->SetText("DISABLE");
  2307. Context->InsertEndChild(Compression);
  2308. XMLElement* AuthProtocol = file.NewElement("AuthProtocol");
  2309. AuthProtocol->SetText(newInfo.strAuthProtocol);
  2310. Context->InsertEndChild(AuthProtocol);
  2311. if(bExt) {
  2312. XMLElement* IPType = file.NewElement("IPType");
  2313. IPType->SetText(newInfo.strIPType);
  2314. Context->InsertEndChild(IPType);
  2315. }
  2316. if(bExt) {
  2317. XMLElement* PurposeGroups = file.NewElement("PurposeGroups");
  2318. root->InsertEndChild(PurposeGroups);
  2319. XMLElement* PurposeGroupGuid = file.NewElement("PurposeGroupGuid");
  2320. PurposeGroupGuid->SetText("{3E5545D2-1137-4DC8-A198-33F1C657515F}");
  2321. PurposeGroups->InsertEndChild(PurposeGroupGuid);
  2322. XMLElement* ProfileConditionedOn = file.NewElement("ProfileConditionedOn");
  2323. root->InsertEndChild(ProfileConditionedOn);
  2324. XMLElement* IMSI = file.NewElement("IMSI");
  2325. IMSI->SetText(subInfo.strIMSI);
  2326. ProfileConditionedOn->InsertEndChild(IMSI);
  2327. XMLElement* IsExclusiveToOther = file.NewElement("IsExclusiveToOther");
  2328. IsExclusiveToOther->SetText("true");
  2329. root->InsertEndChild(IsExclusiveToOther);
  2330. }
  2331. else {
  2332. XMLElement* DisplayProviderName = file.NewElement("DisplayProviderName");
  2333. DisplayProviderName->SetAttribute("xmlns", "http://www.microsoft.com/networking/WWAN/profile/v2");
  2334. DisplayProviderName->SetText(subInfo.strProviderName);
  2335. root->InsertEndChild(DisplayProviderName);
  2336. }
  2337. //CSimpleStringA csGUID = CreateGuidString();
  2338. //CSimpleStringA csXmlFile = "C:\\rvc\\Temp\\" + csGUID;
  2339. //csXmlFile += ".xml";
  2340. //file.SaveFile((LPCTSTR)csXmlFile);
  2341. XMLPrinter printer;
  2342. file.Print(&printer);
  2343. csContent = CSimpleStringA2W(CSimpleStringA(printer.CStr()));
  2344. if(csContent.GetLength() <= 0) {
  2345. m_pFSM->m_csErrMsg = "生成配置文件内容失败!";
  2346. return Error_IO;
  2347. }
  2348. return Error_Succeed;
  2349. }
  2350. ErrorCodeEnum CMbnImplHelper::UpdateProfile(const CSimpleStringA& csInterfaceID, LPCTSTR lpszProfileName, const AccessKeyInfo& newInfo, bool fApplyNow)
  2351. {
  2352. CSimpleStringW content;
  2353. Dbg("Try to update profile[%s]", lpszProfileName);
  2354. m_csTmpProfileName = CSimpleStringA(lpszProfileName);
  2355. ErrorCodeEnum rc = _CreateProfile(csInterfaceID, newInfo, fApplyNow, true, content);
  2356. if(rc == Error_Succeed) {
  2357. rc = DealWithProfile(csInterfaceID, lpszProfileName, false, content);
  2358. if(rc == Error_Succeed) {
  2359. if(strlen(lpszProfileName) > 0)
  2360. {
  2361. CSmartPointer<IConfigInfo> spConfigRun;
  2362. m_pFSM->GetEntityBase()->GetFunction()->OpenConfig(Config_Run, spConfigRun);
  2363. spConfigRun->WriteConfigValue(lpszProfileName, "Status", "Update");
  2364. spConfigRun->WriteConfigValue(lpszProfileName, "FriendlyName", newInfo.strProfileDesc);
  2365. #ifdef USING_ENCRYPT
  2366. CSimpleStringA csEValue = EncryptString(newInfo.strPassword);
  2367. spConfigRun->WriteConfigValue(lpszProfileName, "Password", csEValue);
  2368. #else
  2369. spConfigRun->WriteConfigValue(lpszProfileName, "Password", newInfo.strPassword);
  2370. #endif
  2371. spConfigRun->WriteConfigValue(lpszProfileName, "APN", newInfo.strAccessString);
  2372. }
  2373. if(m_bCntPfEventInited) {
  2374. Dbg("Wait for profile update event");
  2375. return Error_Pending;
  2376. }
  2377. }
  2378. return rc;
  2379. }
  2380. Dbg("_CreateProfile failed!");
  2381. return Error_Unexpect;
  2382. }
  2383. ErrorCodeEnum CMbnImplHelper::DealWithProfile(
  2384. const CSimpleStringA& csInterfaceID, LPCTSTR lpszProfileName, BOOL bDelete, const CSimpleStringW& csUpdateCtx)
  2385. {
  2386. HRESULT result = S_OK;
  2387. ATL::CComPtr<IMbnInterface> pInterface = GetInterfaceEx(csInterfaceID);
  2388. if(pInterface == NULL) {
  2389. m_pFSM->m_csErrMsg = "获取网卡接口失败";
  2390. return Error_Unexpect;
  2391. }
  2392. ATL::CComPtr<IMbnConnectionProfileManager> pCntProfileMnr = NULL;
  2393. result = CoCreateInstance(CLSID_MbnConnectionProfileManager, NULL, CLSCTX_ALL, IID_IMbnConnectionProfileManager,
  2394. (LPVOID*)&pCntProfileMnr);
  2395. if(FAILED(result))
  2396. {
  2397. SetInteralError(result);
  2398. m_pFSM->m_csErrMsg = "获取配置文件管理句柄失败";
  2399. Dbg("*** ERROR: Failed to Initialize IMbnConnectionProfileManager with code 0x%08x", result);
  2400. return Error_Unexpect;
  2401. }
  2402. SAFEARRAY* cntProfiles = NULL;
  2403. result = pCntProfileMnr->GetConnectionProfiles(pInterface, &cntProfiles);
  2404. if(FAILED(result)){
  2405. SetInteralError(result);
  2406. m_pFSM->m_csErrMsg = CSimpleStringA::Format("获取配置文件列表失败:0x%X", result);
  2407. return Error_Unexpect;
  2408. }
  2409. LONG lBound, hBound;
  2410. result = SafeArrayGetLBound(cntProfiles, 1, &lBound);
  2411. if(FAILED(result)) {
  2412. m_pFSM->m_csErrMsg = CSimpleStringA::Format("获取配置文件上下限:0x%X", result);
  2413. return Error_Unexpect;
  2414. }
  2415. result = SafeArrayGetUBound(cntProfiles, 1, &hBound);
  2416. if(FAILED(result)){
  2417. m_pFSM->m_csErrMsg = CSimpleStringA::Format("获取配置文件上下限:0x%X", result);
  2418. return Error_Unexpect;
  2419. }
  2420. if(lBound > hBound) {
  2421. m_pFSM->m_csErrMsg = "找不到任何对应的配置文件";
  2422. return Error_Unexpect;
  2423. }
  2424. Dbg("profile bound: %d, %d", lBound, hBound);
  2425. ATL::CComPtr<IMbnConnectionProfile> pCntProfile = NULL;
  2426. for(LONG index=lBound; index<=hBound; index++) {
  2427. pCntProfile = NULL;
  2428. result = SafeArrayGetElement(cntProfiles, &index, (void*)(&pCntProfile));
  2429. if(SUCCEEDED(result)) {
  2430. BSTR strProfile;
  2431. result = pCntProfile->GetProfileXmlData(&strProfile);
  2432. if(SUCCEEDED(result)) {
  2433. CSimpleStringA csXmlData = ConvertToString(strProfile);
  2434. FreeBString(strProfile);
  2435. tinyxml2::XMLDocument xmlData;
  2436. if(xmlData.Parse(csXmlData) == tinyxml2::XML_SUCCESS) {
  2437. tinyxml2::XMLElement* root = NULL;
  2438. if((root = xmlData.FirstChildElement("MBNProfileExt")) == NULL) {
  2439. root = xmlData.FirstChildElement("MBNProfile");
  2440. }
  2441. if(root != NULL) {
  2442. CSimpleStringA csName = root->FirstChildElement( "Name" )->GetText();
  2443. Dbg("Profiles[%d]: profileName:%s", index, (LPCTSTR)csName);
  2444. if(!csName.Compare(lpszProfileName)) {
  2445. Dbg("specified profileName:%s", lpszProfileName);
  2446. if(bDelete) {
  2447. Dbg("Found it, start to delete it");
  2448. result = pCntProfile->Delete();
  2449. if(cntProfiles) {
  2450. SafeArrayDestroy(cntProfiles);
  2451. cntProfiles = NULL;
  2452. }
  2453. if(SUCCEEDED(result)) {
  2454. Dbg("Delete succ.");
  2455. m_csTmpProfileName = csName;
  2456. return Error_Succeed;
  2457. } else {
  2458. m_pFSM->m_csErrMsg = CSimpleStringA::Format("删除失败:0x%X", result);
  2459. Dbg("delete profile failed, 0x%X", result);
  2460. return Error_Unexpect;
  2461. }
  2462. } else {
  2463. result = pCntProfile->UpdateProfile(csUpdateCtx);
  2464. if(cntProfiles) {
  2465. SafeArrayDestroy(cntProfiles);
  2466. cntProfiles = NULL;
  2467. }
  2468. if(SUCCEEDED(result)) {
  2469. Dbg("Update succ.");
  2470. m_csTmpProfileName = csName;
  2471. return Error_Succeed;
  2472. } else {
  2473. //https://docs.microsoft.com/en-gb/windows/desktop/com/com-error-codes-9
  2474. if(result == HRESULT_FROM_WIN32(ERROR_NOT_FOUND)) {
  2475. } else if(result == HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND)) {
  2476. } else if(result == HRESULT_FROM_WIN32(ERROR_SERVICE_NOT_ACTIVE)) {
  2477. } else {
  2478. m_pFSM->m_csErrMsg = CSimpleStringA::Format("更新失败:0x%X", result);
  2479. }
  2480. Dbg("update profile failed, 0x%X", result);
  2481. return Error_Unexpect;
  2482. }
  2483. }
  2484. }
  2485. }
  2486. }
  2487. }
  2488. }
  2489. }
  2490. if(cntProfiles) {
  2491. SafeArrayDestroy(cntProfiles);
  2492. cntProfiles = NULL;
  2493. }
  2494. m_pFSM->m_csErrMsg = "找不到对应的配置文件";
  2495. return Error_NotExist;
  2496. }
  2497. ErrorCodeEnum CMbnImplHelper::DeleteProfile(const CSimpleStringA& csInterfaceID, LPCTSTR lpszProfileName)
  2498. {
  2499. Dbg("Try to delete profile[%s]", lpszProfileName);
  2500. CSimpleStringW csEmpty = "";
  2501. ErrorCodeEnum rc = DealWithProfile(csInterfaceID, lpszProfileName, true, csEmpty);
  2502. if(rc == Error_Succeed || rc == Error_Pending) {
  2503. if(m_csTmpProfileName.GetLength() > 0) {
  2504. CSmartPointer<IConfigInfo> spConfigRun;
  2505. m_pFSM->GetEntityBase()->GetFunction()->OpenConfig(Config_Run, spConfigRun);
  2506. spConfigRun->WriteConfigValue(m_csTmpProfileName, "Status", "Invalid-Delete");
  2507. spConfigRun->WriteConfigValue(m_csTmpProfileName, "EntryName", "");
  2508. spConfigRun->WriteConfigValue(m_csTmpProfileName, "PhoneNum", "");
  2509. spConfigRun->WriteConfigValue(m_csTmpProfileName, "UserName", "");
  2510. spConfigRun->WriteConfigValue(m_csTmpProfileName, "Password", "");
  2511. spConfigRun->WriteConfigValue(m_csTmpProfileName, "APN", "");
  2512. }
  2513. }
  2514. return rc;
  2515. }