MobileDialFSM.cpp 204 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275
  1. #include "stdafx2.h"
  2. #include "MobileDialFSM.h"
  3. #include "MbnEventImpl.h"
  4. #include "WirelessAgentSafeClient.h"
  5. #define WIN32_LEAN_AND_MEAN
  6. #include <windows.h>
  7. #include <winsock2.h>
  8. #include <iphlpapi.h>
  9. #include <ws2tcpip.h>
  10. #include <iostream>
  11. #include <fstream>
  12. #include <string>
  13. #include <vector>
  14. using namespace std;
  15. using namespace MobileDial;
  16. #include "EventCode.h"
  17. #include "GetDevInfoHelper.h"
  18. #include "string_util.h"
  19. #include "MobileDlg.h"
  20. #define SUPPORT_DIAL_FROM_WEB 1
  21. #pragma comment (lib, "Ws2_32.lib")
  22. const int DATA_BUFSIZE = 8192;
  23. const DWORD MOBILEDIAL_TIMER_ID = 1;
  24. const int CHECK_CONNECT_TIMER_ID = 2;
  25. const int CHECK_CONNECT_BACKUP_TIMER_ID = 5;
  26. const int CHECK_CONNECT_DIAL_TIMER_ID = 6;
  27. const int RAS_CONNECTION_NOTIFICATION_TIMER_ID = 7;
  28. const int CHECK_RASDEVICE_CHANGED_TIMER_ID = 8;
  29. const int CHECK_PRE_RASDEVICE_CHANGED_TIMER_ID = 9;
  30. const int CHECK_CONNECT_MBN_TIMER_ID = 10;
  31. const int FAILURE_WAIT_TIMER_ID = 3;
  32. const int CHECK_NETCARD_TIMER_ID = 4;
  33. const int TIMER_TRY_LINK_ID = 11;
  34. const int TIMER_CHECK_DEVICE_ID = 12;
  35. const int TIMER_CHECK_DEVICE_NOCHANGED_ID = 13;
  36. const int MAX_TRY_CONNECT_TIMES = 3;
  37. const int MAX_TRY_WLAN_CONNETC_TIMES = 6;
  38. const DWORD BATTERY_TIMER_INTERVAL = 3000;
  39. const DWORD DEV_CHANGED_TIMER_INTERVAL = 3500;
  40. const DWORD RASCONNECTION_TIMER_INTERVAL = 500;
  41. const int DEFAULT_FAIL_CONNECT_MAX = 3;
  42. const int DEFAULT_FAIL_CONNECT_LESS = 1;
  43. const DWORD MOBILEDIAL_HBEAT_INTERVAL = 10000;
  44. const DWORD MOBILEDIAL_LIB_INTERVAL = 9000;
  45. const DWORD MOBILEDIAL_FLB_INTERVAL = 5000;
  46. const DWORD MOBILEDIAL_QUICK_TIMER_INTERVAL = 800;
  47. const DWORD MOBILEDIAL_FASTER_INTERVAL = 1000;
  48. const DWORD MOBILEDIAL_QUICK_TIMER_PLUS_INTERVAL = 3000;
  49. const DWORD MOBILEDIAL_FASTER_PLUS_INTERVAL = 5000;
  50. const DWORD MOBILEDIAL_CHECKDONGLE_INTERVAL = 5000;
  51. const DWORD MOBILEDIAL_WAITDIALRT_INTERVAL = 10000;
  52. const DWORD MOBILEDIAL_FASTERDIALRT_INTERVAL = 6000;
  53. const DWORD MOBILEDIAL_WLAN_CONNECT_INTERVAL = 10000;
  54. const int PING_INTERVAL = (60 * 1000 / MOBILEDIAL_FASTER_INTERVAL) - 1;//1 min
  55. const int ATTEMPT_INTERVAL = (1 * 60 * 1000 / MOBILEDIAL_CHECKDONGLE_INTERVAL) - 1;//1 min
  56. HRASCONN g_hrasconn = NULL;
  57. DWORD g_dwRasError;
  58. BOOL g_bFirstConnect = TRUE;
  59. const int DEFAULT_TIMEOUT_SEC = 3;
  60. const char* interfaceList[] = {
  61. "PC UI Interface",
  62. "ZTE NMEA Device",
  63. "ZTE Modem Device",
  64. "Vodafone Mobile Broadband Secondary Port Modem (Huawei)"
  65. };
  66. const int sizeInterfaceList = 4; // [Josephus in 14:50:28 2016/9/3]
  67. const char* modelList[] = {
  68. RASDT_Modem
  69. };
  70. const int sizeModelList = 1;
  71. //0: Connected
  72. //1: Disconnected
  73. //2: Error
  74. //3: Connect event
  75. //4: Cancel event.
  76. const int RasEventCount = 5;
  77. HANDLE g_RasEvents[RasEventCount] = { NULL };
  78. const int PROFILE_MAX_COUNT = 10;
  79. #define SYSVAR_NET_STATUS "NetState" //系统变量名称
  80. const char* sysVarNetStatusEnum[] = {"N", "O", "D", "I", "C", "E"};
  81. #define NET_STATUS_UNINIT 0 //初始状态
  82. #define NET_STATUS_NO_DEVICE 1 //没有设备
  83. #define NET_STATUS_DIAL_OFF 2 //设备没有拨号
  84. #define NET_STATUS_DISCONNECT 3 //上网卡已经连接但是网络不通
  85. #define NET_STATUS_CONNECTED 4 //网络已连接
  86. #define NET_STATUS_ERROR 5 //异常
  87. void WINAPI CustomRasDialFunc(UINT unMsg, RASCONNSTATE rasconnstate, DWORD dwError)
  88. {
  89. static DWORD dwWatiTime = 1500;
  90. if (dwError){
  91. Sleep(dwWatiTime);
  92. g_dwRasError = dwError;
  93. Dbg ("RasDial -- Error receive...");
  94. if(g_RasEvents[2] != NULL)
  95. SetEvent(g_RasEvents[2]);
  96. return;
  97. }
  98. bool fDoneState = !!(rasconnstate & RASCS_DONE);
  99. bool fPausedState = !!(rasconnstate & RASCS_PAUSED);
  100. bool fRunState = !(fDoneState || fPausedState);
  101. // Map each of the states of RasDial() and display on the screen
  102. // the next state that RasDial() is entering
  103. switch (rasconnstate)
  104. {
  105. // Running States
  106. case RASCS_OpenPort:
  107. Dbg ("RasDial -- Opening port...");
  108. break;
  109. case RASCS_PortOpened:
  110. Dbg ("RasDial -- Port opened.");
  111. break;
  112. case RASCS_ConnectDevice:
  113. Dbg ("RasDial -- Connecting device...");
  114. break;
  115. case RASCS_DeviceConnected:
  116. Dbg ("RasDial -- Device connected.");
  117. break;
  118. case RASCS_AllDevicesConnected:
  119. Dbg ("RasDial -- All devices connected.");
  120. break;
  121. case RASCS_Authenticate:
  122. Dbg ("RasDial -- Authenticating...");
  123. break;
  124. case RASCS_AuthNotify:
  125. Dbg ("RasDial -- Authentication notify.");
  126. break;
  127. case RASCS_AuthRetry:
  128. Dbg ("RasDial -- Retrying authentication...");
  129. break;
  130. case RASCS_AuthCallback:
  131. Dbg ("RasDial -- Authentication callback...");
  132. break;
  133. case RASCS_AuthChangePassword:
  134. Dbg ("RasDial -- Change password...");
  135. break;
  136. case RASCS_AuthProject:
  137. Dbg ("RasDial -- Projection phase started...");
  138. break;
  139. case RASCS_AuthLinkSpeed:
  140. Dbg ("RasDial -- Negotiating speed...");
  141. break;
  142. case RASCS_AuthAck:
  143. Dbg ("RasDial -- Authentication acknowledge...");
  144. break;
  145. case RASCS_ReAuthenticate:
  146. Dbg ("RasDial -- Retrying Authentication...");
  147. break;
  148. case RASCS_Authenticated:
  149. Dbg ("RasDial -- Authentication complete.");
  150. break;
  151. case RASCS_PrepareForCallback:
  152. Dbg ("RasDial -- Preparing for callback...");
  153. break;
  154. case RASCS_WaitForModemReset:
  155. Dbg ("RasDial -- Waiting for modem reset...");
  156. break;
  157. case RASCS_WaitForCallback:
  158. Dbg ("RasDial -- Waiting for callback...");
  159. break;
  160. case RASCS_Projected:
  161. Dbg ("RasDial -- Projection completed.");
  162. break;
  163. #if (WINVER >= 0x400)
  164. case RASCS_StartAuthentication:
  165. Dbg ("RasDial -- Starting authentication...");
  166. break;
  167. case RASCS_CallbackComplete:
  168. Dbg ("RasDial -- Callback complete.");
  169. break;
  170. case RASCS_LogonNetwork:
  171. Dbg ("RasDial -- Logon to the network.");
  172. break;
  173. #endif
  174. case RASCS_SubEntryConnected:
  175. Dbg ("RasDial -- Subentry connected.");
  176. break;
  177. case RASCS_SubEntryDisconnected:
  178. Dbg ("RasDial -- Subentry disconnected.");
  179. #if (WINVER >= 0x601)
  180. case RASCS_ApplySettings:
  181. Dbg ("RasDial -- Apply settings.");
  182. break;
  183. #endif
  184. // The RAS Paused States will not occur because
  185. // we did not use the RASDIALEXTENSIONS structure
  186. // to set the RDEOPT_PausedState option flag.
  187. // RASCS_RetryAuthentication: RasDial正在等待新的用户凭据
  188. // RASCS_CallbackSetByCaller: RasDial正在等待客户机的回拨号码
  189. // RASCS_PasswordExpired: RasDial希望用户提供一个新密码
  190. // Terminal States
  191. case RASCS_Connected:
  192. Sleep(dwWatiTime);
  193. Dbg ("RasDial -- Connection completed.");
  194. if(g_RasEvents[0] != NULL)
  195. SetEvent(g_RasEvents[0]);
  196. break;
  197. case RASCS_Disconnected:
  198. Sleep(dwWatiTime);
  199. Dbg ("RasDial -- Disconnecting...");
  200. if(g_RasEvents[1] != NULL)
  201. SetEvent(g_RasEvents[1]);
  202. break;
  203. default:
  204. Dbg ("RasDial -- Unknown Status = %d", rasconnstate);
  205. break;
  206. }
  207. }
  208. UINT WINAPI RasEventThreadFunc(PVOID param)
  209. {
  210. CMobileDialFSM* pFsm = (CMobileDialFSM*)param;
  211. if(pFsm == NULL)
  212. return -1;
  213. char szRasString[256];
  214. BOOL fError = FALSE;
  215. do
  216. {
  217. DWORD dwRes = WaitForMultipleObjects(5, g_RasEvents, FALSE, INFINITE);
  218. switch(dwRes)
  219. {
  220. case WAIT_OBJECT_0 + 2:
  221. {
  222. memset((void*)szRasString, 0, sizeof(char)*256);
  223. RasGetErrorString((UINT)g_dwRasError, szRasString, 256);
  224. Dbg ("RasDial -- Error: %d - %s.", g_dwRasError, szRasString);
  225. pFsm->m_csErrMsg = CSimpleStringA::Format("%s : %d", szRasString, g_dwRasError);
  226. pFsm->TellDlgTheResult(Error_Unexpect, WM_DIAL_RESULT, ADVANCE_RAS);
  227. //pFsm->SetRasStatus(MBD_RASDIAL_FAILED);
  228. // bug which makes recent dial info cannot be cleared. [1/24/2019 17:33 @Gifur]
  229. //pFsm->RasHangUpSuffix();
  230. FSMEvent* fsmEvent = new FSMEvent(USER_EVT_CONNECT_COMPLETE);
  231. fsmEvent->param1 = -1;
  232. fsmEvent->param2 = ADVANCE_RAS;
  233. pFsm->PostEventLIFO(fsmEvent);
  234. }
  235. break;
  236. case WAIT_OBJECT_0:
  237. {
  238. Dbg ("RasDial Connected Event.");
  239. pFsm->TellDlgTheResult(Error_Succeed, WM_DIAL_RESULT, ADVANCE_RAS);
  240. //pFsm->SetRasStatus(MBD_ACTIVATION_CONNECTED);
  241. FSMEvent* fsmEvent = new FSMEvent(USER_EVT_CONNECT_COMPLETE);
  242. fsmEvent->param1 = 1;
  243. fsmEvent->param2 = ADVANCE_RAS;
  244. pFsm->PostEventLIFO(fsmEvent);
  245. }
  246. break;
  247. case WAIT_OBJECT_0 + 1:
  248. {
  249. Dbg ("RasDial DisConnected Event.");
  250. pFsm->TellDlgTheResult(Error_Succeed, WM_DIAL_DISCONNECT, 1);
  251. pFsm->SetRasStatus(MBD_ACTIVATION_DISCONNECTED);
  252. FSMEvent* fsmEvent = new FSMEvent(USER_EVT_DISCONNECT_COMPLETE);
  253. fsmEvent->param1 = 1;
  254. fsmEvent->param2 = ADVANCE_RAS;
  255. pFsm->PostEventLIFO(fsmEvent);
  256. }
  257. break;
  258. case WAIT_OBJECT_0 + 3:
  259. {
  260. //FSMEvent* fsmEvent = new FSMEvent(USER_EVT_DEVICE_ARRIVAL);
  261. //fsmEvent->param1 = 1;
  262. //fsmEvent->param2 = 3;
  263. //pFsm->PostEventLIFO(fsmEvent);
  264. }
  265. break;
  266. case WAIT_OBJECT_0 + 4:
  267. {
  268. //FSMEvent* fsmEvent = new FSMEvent(USER_EVT_DEVICE_LEAVE);
  269. //fsmEvent->param1 = 1;
  270. //fsmEvent->param2 = 3;
  271. //pFsm->PostEventLIFO(fsmEvent);
  272. }
  273. break;
  274. default:
  275. fError = TRUE;
  276. break;
  277. }
  278. } while (!fError);
  279. Dbg("Exit the RasEventThreadFunc thread");
  280. return 0;
  281. }
  282. CSimpleStringA GuidToString(GUID guid, bool bCover)
  283. {
  284. char c[GUID_LEN] = {0};
  285. if(bCover) {
  286. sprintf_s(c, "{%08X-%04X-%04X-%02X%02X-%02X%02X%02X%02X%02X%02X}",
  287. guid.Data1, guid.Data2, guid.Data3,
  288. guid.Data4[0], guid.Data4[1], guid.Data4[2], guid.Data4[3],
  289. guid.Data4[4], guid.Data4[5], guid.Data4[6], guid.Data4[7]);
  290. } else {
  291. sprintf_s(c, "%08X-%04X-%04X-%02X%02X-%02X%02X%02X%02X%02X%02X",
  292. guid.Data1, guid.Data2, guid.Data3,
  293. guid.Data4[0], guid.Data4[1], guid.Data4[2], guid.Data4[3],
  294. guid.Data4[4], guid.Data4[5], guid.Data4[6], guid.Data4[7]);
  295. }
  296. CSimpleStringA s(c);
  297. return s;
  298. }
  299. CSimpleStringA CreateGuidString(bool bCover)
  300. {
  301. GUID guid;
  302. CoCreateGuid(&guid);
  303. return GuidToString(guid, bCover);
  304. }
  305. template<class T>
  306. class TimerOutHelper : public ITimerListener
  307. {
  308. public:
  309. typedef void (T::*FuncTimer)();
  310. TimerOutHelper(T *p, FuncTimer pTimerFunc, bool bDeleteSelf = false)
  311. : m_pObject(p), m_pTimer(pTimerFunc), m_bDeleteSelf(bDeleteSelf) {}
  312. virtual void OnTimeout(DWORD dwTimerID)
  313. {
  314. (m_pObject->*m_pTimer)();
  315. if (m_bDeleteSelf)
  316. delete this;
  317. }
  318. private:
  319. T *m_pObject;
  320. FuncTimer m_pTimer;
  321. bool m_bDeleteSelf;
  322. };
  323. CMobileDialFSM::CMobileDialFSM()
  324. :m_testResult(Error_Succeed),m_csMachineType(true),m_csSite(true),m_csPhoneNum(true),m_csEntryName(true)
  325. ,m_csUserName(true),m_csPassword(true),m_csVpnName(true)
  326. ,m_sock(INVALID_SOCKET),m_bConnected(false),m_dwCkFailCount(0)
  327. ,m_dwFailMax(DEFAULT_FAIL_CONNECT_MAX),m_batteryRemains(100),m_batteryRemainsF(100),m_bACPower(0), m_bACPowerF(0)
  328. ,m_n1G(0),m_n3G(0),m_n4G(0),m_bRCfgHaveDial(false), m_bPadAndFLB(false), m_bPadAndLIB(false)
  329. ,m_TimerInterval(MOBILEDIAL_FLB_INTERVAL),m_nRecoverMax(0)
  330. ,m_signalVal(0), m_dwLsErrCode(0), m_curState(MBD_NOT_INITIALIZED),m_3gState(MBD_NOT_INITIALIZED),m_HwState(MBD_NOT_INITIALIZED)
  331. ,m_34GReserved(0),m_34GDialSuc(0),m_bNotify(true),m_bBackupAvail(false)
  332. ,ping1(NULL), ping2(NULL),m_hMainWnd(NULL),m_bManageByUser(false),m_nAdvancedGen(0)
  333. ,m_hRasListenThread(NULL),m_csCurEntryGUID(true),m_SafeWirelessClient(NULL),m_WifiChangedEvt(NULL)
  334. ,m_strReservedPasword(true),m_HwDeviceMnt(NULL),m_strTerminalNo(true),m_ecSelfTest(Error_Succeed)
  335. ,m_fMBBDeviceMonitoring(FALSE),m_fMBBDeviceMonitorStop(FALSE),m_dwEntityID(0x212),m_mifiBattery(0)
  336. ,m_bManufactureSignal(FALSE),m_curMIFIDevName(true)
  337. {
  338. m_pMbn = new CMbnImplHelper(this);
  339. m_rasEntries.Clear();
  340. memset(&m_recentInfo, 0, sizeof(RecentDialInfo));
  341. InitializeCriticalSection(&m_cs4WirlessClient);
  342. m_HwDeviceMnt = new hw::hw_device_mnt();
  343. mMBBParam.connectType = 0;
  344. };
  345. CMobileDialFSM::~CMobileDialFSM()
  346. {
  347. LOG_FUNCTION();
  348. if(m_pMbn) {
  349. if(m_bPadAndFLB && !isUsingNewVerAllInOne() ) {
  350. m_pMbn->UnAdviseCustomEvents();
  351. }
  352. delete m_pMbn;
  353. m_pMbn = NULL;
  354. }
  355. if(m_bPadAndFLB && !isUsingNewVerAllInOne() ) {
  356. if(m_hRasListenThread != NULL) {
  357. TerminateThread(m_hRasListenThread, -1);
  358. CloseHandle(m_hRasListenThread);
  359. m_hRasListenThread = NULL;
  360. }
  361. for(int i=0; i<RasEventCount; ++i) {
  362. if(g_RasEvents[i] != NULL) {
  363. CloseHandle(g_RasEvents[i]);
  364. g_RasEvents[i] = NULL;
  365. }
  366. }
  367. mBlackSheepCount = 0;
  368. m_blackSheep.Clear();
  369. }
  370. if(ping1) {
  371. delete ping1;
  372. ping1 = NULL;
  373. }
  374. if(ping2) {
  375. delete ping2;
  376. ping2 = NULL;
  377. }
  378. m_fMBBDeviceMonitorStop = TRUE;
  379. // Dangerous ??
  380. // https://msdn.microsoft.com/en-us/library/windows/desktop/aa377567(v=vs.85).aspx
  381. // 不做清理了
  382. //if(g_hrasconn != NULL) {
  383. // RasHangUp(g_hrasconn);
  384. // g_hrasconn = NULL;
  385. // Sleep(3000);
  386. //}
  387. if(m_SafeWirelessClient != NULL) {
  388. delete m_SafeWirelessClient;
  389. m_SafeWirelessClient = NULL;
  390. }
  391. //DeleteCriticalSection(&m_cs4WirlessClient);
  392. if(m_WifiChangedEvt) {
  393. Dbg("destroy wifi change event listerner...");
  394. if(m_WifiChangedEvt->m_bRegistered) {
  395. spagent::spagent_assistor::unregister_notification_events(conn_tool_wifi, m_WifiChangedEvt);
  396. }
  397. delete m_WifiChangedEvt;
  398. m_WifiChangedEvt = NULL;
  399. }
  400. //if(m_fMBBDeviceMonitoring) {
  401. // do {
  402. // Dbg("wait monitor circle break!");
  403. // Sleep(300);
  404. // } while (m_fMBBDeviceMonitoring);
  405. //}
  406. if(m_HwDeviceMnt) {
  407. auto temp = m_HwDeviceMnt;
  408. m_HwDeviceMnt = NULL;
  409. Dbg("destroy hw instance...");
  410. delete temp;
  411. }
  412. }
  413. ErrorCodeEnum CMobileDialFSM::OnInit()
  414. {
  415. LOG_FUNCTION();
  416. Dbg("Mod version: %s, Complied at: %s %s", MOD_VERSION, __DATE__, __TIME__);
  417. #ifdef TWINKLE_LOCAL_DEBUG
  418. if (AllocConsole())
  419. {
  420. FILE* here = NULL;
  421. freopen_s(&here, "CONOUT$", "w", stdout);
  422. freopen_s(&here, "CONOUT$", "w", stderr);
  423. Dbg("Debug console created.");
  424. }
  425. #endif
  426. ErrorCodeEnum eErr;
  427. int netAccessFlag=99, iFailMax = 99;
  428. int value = 0;
  429. CSimpleStringA csValue;
  430. CSimpleStringA csEntityName = GetEntityBase()->GetEntityName();
  431. CSmartPointer<IConfigInfo> spConfig, spConfigCen, spConfigRun;
  432. GetEntityBase()->GetFunction()->OpenConfig(Config_Software, spConfig);
  433. GetEntityBase()->GetFunction()->OpenConfig(Config_Run, spConfigRun);
  434. m_dwFailMax = DEFAULT_FAIL_CONNECT_MAX;
  435. m_TimeoutSec = DEFAULT_TIMEOUT_SEC;
  436. m_csVpnName = "";
  437. spConfigRun->ReadConfigValue("init", "param1", m_csPhoneNum);
  438. if (m_csPhoneNum.GetLength() < 1) {
  439. Dbg("There are no config at runtime, config it's the new terminal device.");
  440. m_bRCfgHaveDial = false;
  441. } else {
  442. Dbg("For param, to read run cfg.");
  443. m_bRCfgHaveDial = true;
  444. spConfigRun->ReadConfigValue("init", "param2", m_csEntryName);
  445. spConfigRun->ReadConfigValue("init", "param3", m_csUserName);
  446. spConfigRun->ReadConfigValue("init", "param4", m_csPassword);
  447. spConfigRun->ReadConfigValue("init", "param5", m_csVpnName);
  448. }
  449. if(!m_csVpnName.IsNullOrEmpty()) {
  450. Dbg("Vpn Name: %s", (LPCTSTR)m_csVpnName);
  451. }
  452. int nValue = 0;
  453. if(spConfigRun->ReadConfigValueInt("init", "ManualManage", nValue) == 0 && nValue > 0) {
  454. Dbg("Require manage by user!!");
  455. m_bManageByUser = true;
  456. }
  457. if(spConfigRun->ReadConfigValueInt("init", "GenAdvanced", nValue) == 0 && nValue > 0) {
  458. Dbg("Advanced Gen choose: %d", nValue);
  459. if(nValue == ADVANCE_RAS || nValue == ADVANCE_MBN || nValue == ADVANCE_MBB) {
  460. CSimpleStringA csTip;
  461. if(nValue == ADVANCE_RAS) {
  462. csTip = "当前终端选择的上网类型: 3G-RAS";
  463. } else if(nValue == ADVANCE_MBN) {
  464. csTip = "当前终端选择的上网类型: 4G-MBN";
  465. } else {
  466. csTip = "当前终端选择的上网类型: HuaWei-MBB";
  467. }
  468. LogEvent(Severity_High, 0, (LPCTSTR)csTip);
  469. m_nAdvancedGen = nValue;
  470. }
  471. }
  472. m_recentInfo.usDialType = CNT_INVALID;
  473. m_recentInfo.ulRequestID = 0;
  474. if(spConfigRun->ReadConfigValue("Recent", "Type", csValue) == Error_Succeed
  475. && csValue.GetLength() > 0) {
  476. if(!csValue.Compare("MBN") && m_nAdvancedGen != ADVANCE_RAS && m_nAdvancedGen != ADVANCE_MBB) {
  477. m_recentInfo.usDialType = CNT_MBN;
  478. } else if(!csValue.Compare("RAS") && m_nAdvancedGen != ADVANCE_MBN && m_nAdvancedGen != ADVANCE_MBB) {
  479. m_recentInfo.usDialType = CNT_RAS;
  480. }
  481. }
  482. if(m_recentInfo.usDialType != CNT_INVALID) {
  483. spConfigRun->ReadConfigValue("Recent", "Dongle", m_recentInfo.strDongle);
  484. spConfigRun->ReadConfigValue("Recent", "Profile", m_recentInfo.strProfile);
  485. spConfigRun->ReadConfigValueInt("Recent", "Manual", m_recentInfo.fByManual);
  486. Dbg("Advanced connect device: %s", (LPCTSTR)m_recentInfo.strDongle);
  487. }
  488. GetCurWindowsVersion();
  489. GetEntityBase()->GetFunction()->OpenConfig(Config_CenterSetting, spConfigCen);
  490. // Read from CenterSetting in correct format [Josephus in 12:15:21 2016/4/18]
  491. CSimpleStringA csServer(""), csServerBackup("");
  492. bool bLoadInCentSet = false;
  493. spConfigCen->ReadConfigValue(csEntityName, "Server", csServer);
  494. spConfigCen->ReadConfigValue(csEntityName, "Server_Backup", csServerBackup);
  495. if(csServer.GetLength() >= 7){
  496. CAutoArray<CSimpleStringA> servSplits = csServer.Split(' ');
  497. if(servSplits.GetCount() >= 2){
  498. m_csIp = servSplits[0];
  499. m_csPort = servSplits[1];
  500. bLoadInCentSet = true;
  501. }else{
  502. Dbg("The serverString loaded in CenterSetting has illegal format %s", (LPCTSTR)csServer);
  503. }
  504. }
  505. if(!bLoadInCentSet && csServerBackup.GetLength() >= 7)
  506. {
  507. Dbg("To use the ServerBackup string(%s)", (LPCTSTR)csServerBackup);
  508. CAutoArray<CSimpleStringA> servSplits = csServerBackup.Split(' ');
  509. if(servSplits.GetCount() >= 2){
  510. m_csIp = servSplits[0];
  511. m_csPort = servSplits[1];
  512. bLoadInCentSet = true;
  513. }else{
  514. Dbg("The serverBackupString loaded in CenterSetting has illegal format");
  515. }
  516. }
  517. Dbg("DualActive mark.");
  518. m_csIpBackup = "";
  519. m_csPortBackup = "";
  520. if(!bLoadInCentSet) {
  521. Dbg("For IP and port, no center setting, use local cfg.");
  522. spConfig->ReadConfigValue("init", "ip", m_csIp);
  523. spConfig->ReadConfigValue("init", "port", m_csPort);
  524. }
  525. else {
  526. if(csServerBackup.GetLength() >= 7) {
  527. Dbg("Store ServerBackup string(%s) after DualActive", (LPCTSTR)csServerBackup);
  528. CAutoArray<CSimpleStringA> servSplits = csServerBackup.Split(' ');
  529. if(servSplits.GetCount() >= 2) {
  530. if(!!servSplits[0].Compare("99.1.100.215")) {
  531. m_csIpBackup = servSplits[0];
  532. m_csPortBackup = servSplits[1];
  533. }
  534. }else{
  535. Dbg("The serverBackupString loaded in CenterSetting has illegal format after DualActive");
  536. }
  537. }
  538. }
  539. if((m_csIp.GetLength() < 7 || m_csPort.GetLength() <= 0)
  540. && (m_csIpBackup.GetLength() < 7 || m_csPortBackup.GetLength() <= 0)
  541. )
  542. {
  543. Dbg("Get ip and port failed [IP#%s][Port#%s].", (LPCTSTR)m_csIp, (LPCTSTR)m_csPort);
  544. Dbg("Get backup ip and port failed [IP#%s][Port#%s].", (LPCTSTR)m_csIpBackup, (LPCTSTR)m_csPortBackup);
  545. LogError(Severity_High, Error_Param, CombineErrorCode(MBD_CENTERSETTING_LACK),
  546. "未能从配置中读取用于检测网络连接的服务其地址或端口,请检查!");
  547. ShowFatalError("请配置用于MobileDial模块检测网络的服务器IP和端口");
  548. return Error_Param;
  549. }
  550. CSystemStaticInfo sysInfo;
  551. eErr = GetEntityBase()->GetFunction()->GetSystemStaticInfo(sysInfo);
  552. if (eErr != Error_Succeed) {
  553. Dbg("Get System Static info failed(%d).",eErr);
  554. return Error_Unexpect;
  555. }
  556. m_csMachineType = sysInfo.strMachineType;
  557. m_csSite = sysInfo.strSite;
  558. m_strTerminalNo = sysInfo.strTerminalID;
  559. //m_strTerminalNo = "12345";
  560. if(m_strTerminalNo.GetLength() < 10) {
  561. const int suffixLen = 10 - m_strTerminalNo.GetLength();
  562. CSimpleStringA strTempTerminal(true);
  563. strTempTerminal.Append("0000000000", suffixLen);
  564. strTempTerminal += m_strTerminalNo;
  565. m_strTerminalNo = strTempTerminal;
  566. } else if(m_strTerminalNo.GetLength() > 10) {
  567. m_strTerminalNo = m_strTerminalNo.SubString(0, 10);
  568. }
  569. //Dbg("new terminal no: %s", m_strTerminalNo.GetData());
  570. //return Error_Bug;
  571. //m_bPadAndFLB = false;
  572. //for(int i=MBD_NOT_INITIALIZED; i<=MBD_INTERNAL_ERROR; ++i) {
  573. // DWORD dwUserCode = (DWORD)((m_dwEntityID << 16) + (DWORD)(i));
  574. // CSimpleStringA str = ConvertStatusToString((MOBILEDIAL_CONNECTION_STATE)i);
  575. // if(str.IndexOf("MOBILEDIAL_UNEXPECT_STATE(") == -1) {
  576. // SetCurState((MOBILEDIAL_CONNECTION_STATE)i);
  577. // CSimpleStringA csData = GetFurtherDetail();
  578. // Dbg("0x%X - %s", dwUserCode, str.GetData());
  579. // Dbg("%s", csData.GetData());
  580. // }
  581. //}
  582. //return Error_Cancel;
  583. Dbg("%s:%s,%s,%s", (LPCTSTR)m_csPhoneNum, (LPCTSTR)m_csEntryName,(LPCTSTR)m_csMachineType, (LPCTSTR)m_csSite);
  584. Dbg("%s::%s, %s::%s", (LPCTSTR)m_csIp, (LPCTSTR)m_csPort, (LPCTSTR)m_csIpBackup, (LPCTSTR)m_csPortBackup);
  585. m_bBackupAvail = (m_csIpBackup.GetLength() >= 7 && m_csPortBackup.GetLength() > 0);
  586. if(m_bBackupAvail) {
  587. if(m_csIpBackup.Compare((LPCTSTR)m_csIp) == 0 && m_csPortBackup.Compare((LPCTSTR)m_csPortBackup) == 0) {
  588. m_bBackupAvail = false;
  589. }
  590. }
  591. WSADATA wsaData;
  592. int iResult = WSAStartup(MAKEWORD(2,2), &wsaData);
  593. if (iResult != 0) {
  594. Dbg("WSAStartup failed with error %d", iResult);
  595. RecordLastErrorMsg(iResult);
  596. return Error_IO;
  597. }
  598. if (CreateCheckSocket() != 0) {
  599. Dbg("failed to create socket for connecting test at the begin");
  600. WSACleanup();
  601. return Error_IO;
  602. }
  603. closesocket(m_sock);
  604. m_sock = INVALID_SOCKET;
  605. //m_csSite = "cmb.yzx";
  606. //int bPingDisable = 0;
  607. //spConfigCen->ReadConfigValueInt(csEntityName, "PingDisable", bPingDisable);
  608. //if(!bPingDisable) {
  609. // ping1 = new CTWPing();
  610. // ping1->Initial(m_csIp);
  611. // if(m_bBackupAvail && m_csIp.Compare(m_csIpBackup)) {
  612. // ping2 = new CTWPing();
  613. // ping2->Initial(m_csIpBackup);
  614. // }
  615. //}
  616. if(!m_csMachineType.Compare("RVC.Pad", true) && !m_csSite.Compare("cmb.FLB", true)) {
  617. Dbg("Detect MachineType: %s & SiteType: %s", (LPCTSTR)m_csMachineType, (LPCTSTR)m_csSite);
  618. // 行内PAD,删除旧的配置文件
  619. if(m_bRCfgHaveDial && !m_csPhoneNum.Compare("#777") && !m_csEntryName.Compare("szbranch")
  620. && !m_csUserName.Compare("123@szchinacmb.vpdn.gd") && !m_csPassword.Compare("123")) {
  621. Dbg("delete the old test dial param: %d", RasDeleteEntryA(NULL, m_csEntryName));
  622. spConfigRun->WriteConfigValue("init", "param1", "");
  623. spConfigRun->WriteConfigValue("init", "param2", "");
  624. spConfigRun->WriteConfigValue("init", "param3", "");
  625. spConfigRun->WriteConfigValue("init", "param4", "");
  626. spConfigRun->WriteConfigValue("init", "param5", "");
  627. m_csEntryName = m_csPhoneNum = m_csUserName = m_csPassword = "";
  628. }
  629. m_bPadAndFLB = true;
  630. if(isUsingNewVerAllInOne()) {
  631. Dbg("Detect new version all_in_one condition.");
  632. m_nAdvancedGen = 0;
  633. } else {
  634. if(m_nAdvancedGen != ADVANCE_MBB) {
  635. m_blackSheep.Init(MAX_BLACK_SHEEP_NUM);
  636. mBlackSheepCount = 0;
  637. if(m_nAdvancedGen != ADVANCE_RAS) {
  638. Init4GComponent();
  639. }
  640. if(m_nAdvancedGen != ADVANCE_MBN) {
  641. Init3GComponent();
  642. }
  643. } else {
  644. InitHuaweiComponent();
  645. }
  646. Recognized3GOr4G();
  647. this->GetEntityBase()->GetFunction()->PostThreadPoolTask(new MBBDevicMonitorTask(this));
  648. }
  649. } else {
  650. if(!m_csMachineType.Compare("RVC.Pad", true) && !m_csSite.Compare("cmb.LIB", true)) {
  651. Dbg("Detect MachineType: %s & SiteType: %s", (LPCTSTR)m_csMachineType, (LPCTSTR)m_csSite);
  652. m_bPadAndLIB = true;
  653. }
  654. m_TimerInterval = MOBILEDIAL_LIB_INTERVAL;
  655. }
  656. //电池电源监听线程
  657. ITimerListener *pListener = new TimerOutHelper<CMobileDialFSM>(this,
  658. &CMobileDialFSM::BatteryNotifierTimeOut);
  659. GetEntityBase()->GetFunction()->SetTimer(MOBILEDIAL_TIMER_ID, pListener, BATTERY_TIMER_INTERVAL);
  660. return Error_Succeed;
  661. }
  662. int CMobileDialFSM::ReadRecentValiDongle()
  663. {
  664. CSimpleStringA csInterfaceID;
  665. CSmartPointer<IConfigInfo> spConfigRun;
  666. int nType = 0;
  667. if(!m_pMbn->GetInterfaceID(csInterfaceID)) {
  668. Dbg("GetInterfaceID failed in ReadRecentValiDongle.");
  669. } else {
  670. Dbg("GetInterfaceID(%s) suc.", (LPCTSTR)csInterfaceID);
  671. if(GetEntityBase()->GetFunction()->OpenConfig(Config_Run, spConfigRun) == Error_Succeed) {
  672. spConfigRun->ReadConfigValueInt("Interfaces", (LPCTSTR)csInterfaceID, nType);
  673. }
  674. Dbg("Interface:%s: nType:%d.", (LPCTSTR)csInterfaceID, nType);
  675. }
  676. return nType;
  677. }
  678. void CMobileDialFSM::WriteCurValiDongle(const CSimpleStringA& csInterfaceID, const int nType)
  679. {
  680. CSmartPointer<IConfigInfo> spConfigRun;
  681. if(GetEntityBase()->GetFunction()->OpenConfig(Config_Run, spConfigRun) == Error_Succeed) {
  682. spConfigRun->WriteConfigValueInt("Interfaces", (LPCTSTR)csInterfaceID, nType);
  683. }
  684. return;
  685. }
  686. void CMobileDialFSM::Recognized3GOr4G()
  687. {
  688. LOG_FUNCTION();
  689. static bool s_old3G = false;
  690. static bool s_old4G = false;
  691. SetCurState(MBD_NOT_INITIALIZED);
  692. s_old4G = m_n4G > 0;
  693. s_old3G = m_n3G > 0;
  694. ResetDongleInfos();
  695. m_n3G = m_n4G = 0;
  696. m_34GReserved = 0;
  697. m_34GDialSuc = 0;
  698. if(isUsingNewVerAllInOne()) {
  699. LogEvent(Severity_Middle,CombineErrorCode(MBD_HOLD_MOBILEDONGLE), "当前使用新版分体式多合一设备");
  700. BOOL bActive = FALSE, bConnected = FALSE;
  701. ErrorCodeEnum ec = m_SafeWirelessClient->IsActive(bActive, bConnected);
  702. if(ec == Error_Succeed) {
  703. if(bConnected) {
  704. int nNum = 0;
  705. ec = m_SafeWirelessClient->GetAPNConfigs(-1, nNum);
  706. }
  707. }
  708. return;
  709. }
  710. if(m_nAdvancedGen != ADVANCE_MBB) {
  711. int nMBN = m_nAdvancedGen != ADVANCE_RAS ? m_pMbn->EnumerateInterfaces(TRUE) : 0;
  712. int nRAS = m_nAdvancedGen != ADVANCE_MBN ? EnumCurRasDevices(TRUE) : 0;
  713. if(nMBN == 0 && nRAS == 0) {
  714. SetCurState(MBD_NO_MOBILEDONGLE);
  715. } else {
  716. if(nRAS > 0) {
  717. m_n3G = nRAS;
  718. if(!s_old3G) LogEvent(Severity_Middle,CombineErrorCode(MBD_HOLD_MOBILEDONGLE),"检测到3G无线宽带上网卡");
  719. }
  720. if(nMBN > 0) {
  721. m_n4G = nMBN;
  722. if(!s_old4G) LogEvent(Severity_Middle,CombineErrorCode(MBD_HOLD_MOBILEDONGLE),"检测到4G无线宽带上网卡");
  723. }
  724. if((nMBN < 0 && nRAS < 0)
  725. || (m_nAdvancedGen == ADVANCE_RAS && nRAS < 0)
  726. || (m_nAdvancedGen == ADVANCE_MBN && nMBN < 0)) {
  727. SetCurState(MBD_INTERNAL_ERROR);
  728. }
  729. }
  730. } else {
  731. CheckHWDeviceStatus();
  732. }
  733. return;
  734. }
  735. ErrorCodeEnum CMobileDialFSM::OnExit()
  736. {
  737. GetEntityBase()->GetFunction()->ResetTimer(0, 0);
  738. if(m_bPadAndFLB && !isUsingNewVerAllInOne() )
  739. {
  740. DisConnect();
  741. m_pMbn->UnAdviseCustomEvents();
  742. for(int i=0; i<RasEventCount; ++i) {
  743. if(g_RasEvents[i] != NULL)
  744. {
  745. CloseHandle(g_RasEvents[i]);
  746. g_RasEvents[i] = NULL;
  747. }
  748. }
  749. }
  750. WSACleanup();
  751. return Error_Succeed;
  752. }
  753. //不做更新网卡状态的操作,所以从其他状态跳转过来时要先刷新
  754. void CMobileDialFSM::s0_on_entry()
  755. {
  756. LOG_FUNCTION();
  757. if (m_bPadAndFLB && !isUsingNewVerAllInOne()) {
  758. const MOBILEDIAL_CONNECTION_STATE status = GetCurState();
  759. #if SUPPORT_DIAL_FROM_WEB
  760. if(status == MBD_ACTIVATION_DISCONNECTED || status == MBD_WLAN_DISCONNECTED) {
  761. #else
  762. if(status == MBD_NO_MOBILEDONGLE || status == MBD_ACTIVATION_DISCONNECTED || status == MBD_WLAN_DISCONNECTED) {
  763. #endif
  764. Dbg("Walk convenient way in connect style: %d", status);
  765. FSMEvent* fsmEvent = new FSMEvent(USER_EVT_CKECK_CONNECTION_FINISHED);
  766. fsmEvent->param1 = 3;
  767. fsmEvent->param2 = 1;
  768. PostEventFIFO(fsmEvent);
  769. return;
  770. } else if(status == MBD_ACTIVATION_CONNECTED) {
  771. LogEvent(Severity_Middle, CombineErrorCode(status), "无线宽带上网卡已连接");
  772. if(g_bFirstConnect) {
  773. g_bFirstConnect = FALSE;
  774. }
  775. }
  776. }
  777. Dbg("Walk normal way in connect style.");
  778. m_TimeoutSec = DEFAULT_TIMEOUT_SEC;
  779. CheckConnectionTask* task = new CheckConnectionTask(this);
  780. GetEntityBase()->GetFunction()->PostThreadPoolTask(task);
  781. SetSysVal(NET_STATUS_UNINIT);
  782. }
  783. void CMobileDialFSM::s0_on_exit()
  784. {
  785. }
  786. unsigned int CMobileDialFSM::s0_on_event(FSMEvent* pEvt)
  787. {
  788. int nRes = pEvt->param1;
  789. Dbg("s0_on_event %d, %d %d.", pEvt->iEvt, nRes, pEvt->param2);
  790. switch(pEvt->iEvt)
  791. {
  792. case USER_EVT_CKECK_CONNECTION_FINISHED:
  793. pEvt->SetHandled();
  794. {
  795. if(nRes != 3) {
  796. bool bShouldBackup = !!(pEvt->param2 & 0x4);
  797. bool bRealBackup = !!(pEvt->param2 & 0x2);
  798. CSimpleStringA csIpPrint = bRealBackup ? m_csIpBackup : m_csIp;
  799. CSimpleStringA csPortPrint = bRealBackup ? m_csPortBackup : m_csPort;
  800. if(nRes == 0) {
  801. CSimpleStringA csMsg;
  802. csMsg = CSimpleStringA::Format("s0(idle): Connect to server(%s:%s) failed(%d) FailCount(%d) FailMax(%d).",
  803. (LPCTSTR)csIpPrint, (LPCTSTR)csPortPrint, m_dwLsErrCode, ++m_dwCkFailCount, m_dwFailMax);
  804. LogEvent(Severity_Middle, CombineErrorCode(MBD_CHECK_NETWORK_CONNETCTED), (LPCTSTR)csMsg);
  805. const MOBILEDIAL_CONNECTION_STATE state = GetCurState();
  806. Dbg("s0(idle): %s.", ConvertStatusToString(state));
  807. // fix bug which lead netbroken show more easy and earlier --Josephus at 8:59:47 20161211
  808. if(m_dwCkFailCount < m_dwFailMax) {
  809. m_TimeoutSec = DEFAULT_TIMEOUT_SEC + 2 * m_dwCkFailCount;
  810. if(bShouldBackup && !bRealBackup) {
  811. Dbg("backup ip and port maybe no exist.");
  812. break;
  813. }
  814. //未达到连续次数的网络断开情况,防止状态跳转
  815. nRes = 4;
  816. if(state == MBD_NETWORK_TIMEOUT) {
  817. Sleep(MOBILEDIAL_QUICK_TIMER_INTERVAL);
  818. }
  819. else {
  820. Sleep( MOBILEDIAL_FASTER_INTERVAL);
  821. }
  822. CheckConnectionTask* task = new CheckConnectionTask(this);
  823. if(bRealBackup) {
  824. task->m_bUseBackup = true;
  825. }
  826. GetEntityBase()->GetFunction()->PostThreadPoolTask(task);
  827. }
  828. else if(!bShouldBackup && m_bBackupAvail) {
  829. m_dwCkFailCount = 0;
  830. m_TimeoutSec = DEFAULT_TIMEOUT_SEC;
  831. //未达到连续次数的网络断开情况,防止状态跳转
  832. nRes = 4;
  833. if(state == MBD_NETWORK_TIMEOUT){
  834. Sleep(MOBILEDIAL_QUICK_TIMER_INTERVAL);
  835. }
  836. else{
  837. Sleep(MOBILEDIAL_FASTER_INTERVAL);
  838. }
  839. CheckConnectionTask* task = new CheckConnectionTask(this);
  840. task->m_bUseBackup = true;
  841. GetEntityBase()->GetFunction()->PostThreadPoolTask(task);
  842. }
  843. }
  844. else {
  845. Dbg("s0(idle): Connect to server(%s:%s) result(%d)(%d), pad&FLB(%d) %s",
  846. (LPCTSTR)csIpPrint, (LPCTSTR)csPortPrint, nRes,
  847. m_dwLsErrCode, m_bPadAndFLB,
  848. ConvertStatusToString(GetCurState()));
  849. }
  850. m_dwOldErrCode = m_dwLsErrCode;
  851. }
  852. else if(pEvt->param2 == 1) {
  853. Dbg("Received convenient way.");
  854. nRes = 0;
  855. }
  856. }
  857. break;
  858. default:
  859. break;
  860. }
  861. return nRes;
  862. }
  863. //Connected
  864. void CMobileDialFSM::s1_on_entry()
  865. {
  866. LOG_FUNCTION();
  867. LogEvent(Severity_Middle, CombineErrorCode(MBD_VALID_CONNECTED), "网络已连接");
  868. ShowFatalError("网络已连接", TRUE);
  869. m_bConnected = true;
  870. SetCurState(MBD_VALID_CONNECTED);
  871. SendNetStatusMsg();
  872. SetSysVal(NET_STATUS_CONNECTED);
  873. ReportConnectType();
  874. m_dwCkFailCount = 0;
  875. m_TimeoutSec = DEFAULT_TIMEOUT_SEC;
  876. ScheduleTimer(CHECK_CONNECT_TIMER_ID, m_TimerInterval);
  877. }
  878. void CMobileDialFSM::s1_on_exit()
  879. {
  880. CancelTimer(CHECK_CONNECT_TIMER_ID);
  881. CancelTimer(CHECK_CONNECT_BACKUP_TIMER_ID);
  882. if(m_nAdvancedGen == 2) {
  883. if(mMBBParam.connectType == MBB_CONNECT_WIRE) {
  884. if(CheckLANConnectStatus() == 0) {
  885. SetHWStatus(MBD_NO_MOBILEDONGLE);
  886. } else {
  887. SetHWStatus(MBD_ACTIVATION_CONNECTED);
  888. }
  889. }
  890. }
  891. }
  892. unsigned int CMobileDialFSM::s1_on_event(FSMEvent* pEvt)
  893. {
  894. int nRes = 0;
  895. Dbg("s1_on_event %d, %d, %d", pEvt->iEvt, pEvt->param1, pEvt->param2);
  896. switch(pEvt->iEvt)
  897. {
  898. case EVT_TIMER:
  899. {
  900. if(pEvt->param1 == CHECK_CONNECT_TIMER_ID || pEvt->param1 == CHECK_CONNECT_BACKUP_TIMER_ID)
  901. {
  902. pEvt->SetHandled();
  903. if(IsConnectedFromHB()) {
  904. m_dwCkFailCount = 0;
  905. ScheduleTimer(CHECK_CONNECT_TIMER_ID, MOBILEDIAL_HBEAT_INTERVAL);
  906. } else {
  907. CheckConnectionTask* task = new CheckConnectionTask(this);
  908. if(pEvt->param1 == CHECK_CONNECT_BACKUP_TIMER_ID)
  909. task->m_bUseBackup = true;
  910. GetEntityBase()->GetFunction()->PostThreadPoolTask(task);
  911. }
  912. }
  913. }
  914. break;
  915. case USER_EVT_CKECK_CONNECTION_FINISHED:
  916. pEvt->SetHandled();
  917. {
  918. nRes = pEvt->param1;
  919. bool bShouldBackup = !!(pEvt->param2 & 0x4);
  920. bool bRealBackup = !!(pEvt->param2 & 0x2);
  921. UINT uTimerID = bRealBackup ? CHECK_CONNECT_BACKUP_TIMER_ID : CHECK_CONNECT_TIMER_ID;
  922. if(nRes == 1)
  923. {
  924. m_dwCkFailCount = 0;
  925. ScheduleTimer(uTimerID, m_TimerInterval);
  926. }
  927. else if(nRes == 0)
  928. {
  929. CSimpleStringA csIpPrint = bRealBackup ? m_csIpBackup : m_csIp;
  930. CSimpleStringA csPortPrint = bRealBackup ? m_csPortBackup : m_csPort;
  931. CSimpleStringA csMsg;
  932. csMsg = CSimpleStringA::Format("s1(connected): Connect to server(%s:%s) failed(%d) FailCount(%d) FailMax(%d).",
  933. (LPCTSTR)csIpPrint, (LPCTSTR)csPortPrint, m_dwLsErrCode, ++m_dwCkFailCount, m_dwFailMax);
  934. LogEvent(Severity_Middle, CombineErrorCode(MBD_CHECK_NETWORK_CONNETCTED), (LPCTSTR)csMsg);
  935. const MOBILEDIAL_CONNECTION_STATE state = GetCurState();
  936. Dbg("s1(connected): %s.", ConvertStatusToString(state));
  937. if(m_dwCkFailCount < m_dwFailMax) {
  938. if(bShouldBackup && !bRealBackup) {
  939. break;
  940. }
  941. m_TimeoutSec = DEFAULT_TIMEOUT_SEC + 2 * m_dwCkFailCount;
  942. //未达到连续次数的网络断开情况,防止状态跳转,并将时间间隔改成1s聆询
  943. nRes = 3;
  944. if(state == MBD_NETWORK_TIMEOUT)
  945. {
  946. ScheduleTimer(uTimerID, MOBILEDIAL_QUICK_TIMER_INTERVAL);
  947. }
  948. else
  949. {
  950. ScheduleTimer(uTimerID, MOBILEDIAL_FASTER_INTERVAL);
  951. }
  952. }
  953. else if(!bShouldBackup && m_bBackupAvail)
  954. {
  955. m_dwCkFailCount = 0;
  956. m_TimeoutSec = DEFAULT_TIMEOUT_SEC;
  957. //未达到连续次数的网络断开情况,防止状态跳转,并将时间间隔改成1s聆询
  958. nRes = 3;
  959. if(state == MBD_NETWORK_TIMEOUT) {
  960. ScheduleTimer(CHECK_CONNECT_BACKUP_TIMER_ID, MOBILEDIAL_QUICK_TIMER_INTERVAL);
  961. } else {
  962. ScheduleTimer(CHECK_CONNECT_BACKUP_TIMER_ID, MOBILEDIAL_FASTER_INTERVAL);
  963. }
  964. }
  965. m_dwOldErrCode = m_dwLsErrCode;
  966. }
  967. }
  968. break;
  969. case USER_EVT_SIGNAL:
  970. pEvt->SetHandled();
  971. {
  972. NetStatusVar evt;
  973. m_signalVal = pEvt->param1;
  974. Dbg("s1(connected): Send Signal %d !!", m_signalVal);
  975. evt.netstatus = m_signalVal;
  976. SpSendBroadcast(m_pEntity->GetFunction(),SP_MSG_OF(NetStatusVar),SP_MSG_SIG_OF(NetStatusVar),evt);
  977. }
  978. break;
  979. case USER_EVT_DEVICE_LEAVE:
  980. pEvt->SetHandled();
  981. BroadcastCableChangedFSMEvent(pEvt);
  982. if(pEvt->param2 == 0) {
  983. nRes = 1;
  984. } else {
  985. nRes = 0;
  986. Recognized3GOr4G();
  987. auto state = GetCurState();
  988. if(state == MBD_NO_MOBILEDONGLE) {
  989. nRes = 1;
  990. } else if(state == MBD_INTERNAL_ERROR) {
  991. PostEventFIFO(new FSMEvent(USER_EVT_FAILURE));
  992. } else if(state == MBD_ACTIVATION_DISCONNECTED) {
  993. FSMEvent* fsmEvent = new FSMEvent(USER_EVT_DISCONNECT_COMPLETE);
  994. fsmEvent->param1 = 0;
  995. fsmEvent->param2 = 0;
  996. PostEventFIFO(fsmEvent);
  997. } else {
  998. nRes = 2;
  999. }
  1000. }
  1001. break;
  1002. default:
  1003. break;
  1004. }
  1005. return nRes;
  1006. }
  1007. //Disconnected
  1008. void CMobileDialFSM::s2_on_entry()
  1009. {
  1010. LOG_FUNCTION();
  1011. m_bConnected = false;
  1012. m_dwCkFailCount = 0;
  1013. m_TimeoutSec = DEFAULT_TIMEOUT_SEC + 2 * m_dwFailMax;
  1014. SendNetStatusMsg();
  1015. SetSysVal(NET_STATUS_DISCONNECT);
  1016. MOBILEDIAL_CONNECTION_STATE status = GetCurState();
  1017. if(m_bPadAndFLB && !isUsingNewVerAllInOne() && status > MBD_NETWORK_SEPERATE)
  1018. {
  1019. LogEvent(Severity_Middle,CombineErrorCode(MBD_INVALID_CONNECTED), "网络已断开");
  1020. if(status == MBD_NO_MOBILEDONGLE){
  1021. FSMEvent* fsmEvent = new FSMEvent(USER_EVT_DEVICE_LEAVE);
  1022. fsmEvent->param1 = 0;
  1023. fsmEvent->param2 = 0;
  1024. PostEventFIFO(fsmEvent);
  1025. }
  1026. else if(status == MBD_ACTIVATION_DISCONNECTED){
  1027. FSMEvent* fsmEvent = new FSMEvent(USER_EVT_DISCONNECT_COMPLETE);
  1028. fsmEvent->param1 = 0;
  1029. fsmEvent->param2 = 0;
  1030. PostEventFIFO(fsmEvent);
  1031. } else if(status == MBD_WLAN_DISCONNECTED) {
  1032. FSMEvent* fsmEvent = new FSMEvent(EVT_USER_WLAN_CONNECT);
  1033. fsmEvent->param1 = 0;
  1034. fsmEvent->param2 = 0;
  1035. PostEventFIFO(fsmEvent);
  1036. }
  1037. else {
  1038. PostEventFIFO(new FSMEvent(USER_EVT_FAILURE));
  1039. }
  1040. }
  1041. else
  1042. {
  1043. CSimpleStringA csTemp;
  1044. if(m_bPadAndLIB && CheckLANConnectStatus() == 0) {
  1045. SetCurState(MBD_NO_FOUND_CABLE);
  1046. ShowFatalError(GetFurtherDetail());
  1047. if(status == MBD_NETWORK_TIMEOUT){
  1048. ScheduleTimer(CHECK_CONNECT_TIMER_ID, MOBILEDIAL_QUICK_TIMER_INTERVAL);
  1049. }else{
  1050. ScheduleTimer(CHECK_CONNECT_TIMER_ID, MOBILEDIAL_FASTER_INTERVAL);
  1051. }
  1052. } else{
  1053. ShowFatalError(GetFurtherDetail());
  1054. if(status == MBD_NETWORK_TIMEOUT) {
  1055. ScheduleTimer(CHECK_CONNECT_TIMER_ID, MOBILEDIAL_QUICK_TIMER_PLUS_INTERVAL);
  1056. }else{
  1057. ScheduleTimer(CHECK_CONNECT_TIMER_ID, MOBILEDIAL_FASTER_PLUS_INTERVAL);
  1058. }
  1059. }
  1060. }
  1061. }
  1062. void CMobileDialFSM::s2_on_exit()
  1063. {
  1064. CancelTimer(CHECK_CONNECT_TIMER_ID);
  1065. //SetSysVal(NET_STATUS_UNINIT);
  1066. }
  1067. unsigned int CMobileDialFSM::s2_on_event(FSMEvent* pEvt)
  1068. {
  1069. int nRes = 0;
  1070. static int s_nTimes = -1;
  1071. static bool bCheckSwitch = false;
  1072. switch(pEvt->iEvt)
  1073. {
  1074. case EVT_TIMER:
  1075. {
  1076. if(pEvt->param1 != CHECK_CONNECT_TIMER_ID)
  1077. break;
  1078. pEvt->SetHandled();
  1079. if(IsConnectedFromHB()) {
  1080. Dbg("(s2_event)Connected status from HB, change status.");
  1081. LogEvent(Severity_High, CombineErrorCode(MBD_VALID_CONNECTED), "心跳正常,执行跳转");
  1082. // s_nTimes = 0;
  1083. nRes = 1;
  1084. }
  1085. else {
  1086. if(s_nTimes == 0) {
  1087. PingTask* pingTask = new PingTask(this);
  1088. GetEntityBase()->GetFunction()->PostEntityTaskFIFO(pingTask);
  1089. s_nTimes++;
  1090. } else if(s_nTimes != -1) {
  1091. if(++s_nTimes > PING_INTERVAL) {
  1092. s_nTimes = 0;
  1093. }
  1094. }
  1095. if(m_bPadAndFLB && !isUsingNewVerAllInOne() && m_n3G > 0) {
  1096. if(!bCheckSwitch) {
  1097. if(EnumCurRasDevices() == 0) {
  1098. FSMEvent* fsmEvent = new FSMEvent(USER_EVT_DEVICE_LEAVE);
  1099. fsmEvent->param1 = 0;
  1100. fsmEvent->param2 = 3;
  1101. PostEventFIFO(fsmEvent);
  1102. }
  1103. bCheckSwitch = true;
  1104. } else {
  1105. bCheckSwitch = false;
  1106. }
  1107. }
  1108. ScheduleTimer(CHECK_CONNECT_TIMER_ID, MOBILEDIAL_FASTER_PLUS_INTERVAL);
  1109. }
  1110. }
  1111. break;
  1112. case USER_EVT_PING_RESULT:
  1113. {
  1114. pEvt->SetHandled();
  1115. const int nPingResult = pEvt->param1;
  1116. if(nPingResult == 0) {
  1117. LogWarn(Severity_Middle, Error_NetBroken, 0, "尝试Ping连接至分行服务失败!");
  1118. } else if(nPingResult > 0) {
  1119. if(nPingResult == 3) {
  1120. LogEvent(Severity_None, 0,
  1121. "Attempt to ping to branch server both succ.");
  1122. }else if(nPingResult == 2) {
  1123. LogEvent(Severity_None, 0,
  1124. CSimpleStringA::Format("Attempt to ping to branch server[%s] succ.",
  1125. (LPCTSTR)m_csIpBackup));
  1126. }else if(nPingResult == 1) {
  1127. LogEvent(Severity_None, 0,
  1128. CSimpleStringA::Format(
  1129. "Attempt to ping to branch server[%s] succ.", (LPCTSTR)m_csIp));
  1130. }
  1131. }
  1132. else
  1133. {
  1134. s_nTimes = -1;
  1135. Dbg("CheckNetSituationWithPing returned %d, Set flag !", nPingResult);
  1136. }
  1137. }
  1138. break;
  1139. case USER_EVT_DEVICE_LEAVE:
  1140. pEvt->SetHandled();
  1141. BroadcastCableChangedFSMEvent(pEvt);
  1142. if(pEvt->param2 == 0) {
  1143. nRes = 1;
  1144. } else {
  1145. nRes = 0;
  1146. Recognized3GOr4G();
  1147. auto state = GetCurState();
  1148. if(state == MBD_NO_MOBILEDONGLE) {
  1149. nRes = 1;
  1150. } else if(state == MBD_INTERNAL_ERROR) {
  1151. PostEventFIFO(new FSMEvent(USER_EVT_FAILURE));
  1152. } else if(state == MBD_ACTIVATION_DISCONNECTED)
  1153. {
  1154. FSMEvent* fsmEvent = new FSMEvent(USER_EVT_DISCONNECT_COMPLETE);
  1155. fsmEvent->param1 = 0;
  1156. fsmEvent->param2 = 0;
  1157. PostEventFIFO(fsmEvent);
  1158. } else {
  1159. nRes = 2;
  1160. }
  1161. }
  1162. break;
  1163. case USER_EVT_FAILURE:
  1164. Dbg("s2(disconnected): goto failed status %s", ConvertStatusToString(GetCurState()));
  1165. pEvt->SetHandled();
  1166. break;
  1167. case EVT_USER_WLAN_CONNECT:
  1168. pEvt->SetHandled();
  1169. break;
  1170. case EVT_USER_WIRELESS_STATE:
  1171. BroadcastCableChangedFSMEvent(pEvt);
  1172. pEvt->SetHandled();
  1173. nRes = pEvt->param1;
  1174. break;
  1175. default:
  1176. break;
  1177. }
  1178. return nRes;
  1179. }
  1180. //Failed
  1181. void CMobileDialFSM::s3_on_entry()
  1182. {
  1183. LOG_FUNCTION();
  1184. const MOBILEDIAL_CONNECTION_STATE state = GetCurState();
  1185. CSimpleStringA csErrorString = GetFurtherDetail();
  1186. ShowFatalError(csErrorString);
  1187. SetSysVal(NET_STATUS_ERROR);
  1188. if(state == MBD_NETWORK_INVALID_SOCKET) {//hardly happen.
  1189. if(WSACleanup() == SOCKET_ERROR) {
  1190. Dbg("s3(failed): WSACleanup() failed");
  1191. RecordLastErrorMsg(WSAGetLastError());
  1192. return;
  1193. }
  1194. Sleep(300);
  1195. WSADATA wsaData;
  1196. int iResult = WSAStartup(MAKEWORD(2,2), &wsaData);
  1197. if (iResult != 0) {
  1198. Dbg("s3(failed): WSAStartup() failed");
  1199. RecordLastErrorMsg(iResult);
  1200. return;
  1201. }
  1202. FSMEvent* fsmEvent = new FSMEvent(USER_EVT_RECOVER);
  1203. PostEventLIFO(fsmEvent);
  1204. }
  1205. else {
  1206. m_bConnected = false;
  1207. m_dwCkFailCount = 0;
  1208. SendNetStatusMsg();
  1209. int mustFlag = 0;
  1210. if(state == MBD_NO_PROFILE) {
  1211. mustFlag = 4;
  1212. } else if(state == MBD_ACTIVATION_CONNECTING) {
  1213. ScheduleTimer(FAILURE_WAIT_TIMER_ID, MOBILEDIAL_FASTER_INTERVAL);
  1214. return;
  1215. }
  1216. m_nRecoverMax = 0;
  1217. ScheduleTimer(FAILURE_WAIT_TIMER_ID, MOBILEDIAL_CHECKDONGLE_INTERVAL);
  1218. }
  1219. }
  1220. void CMobileDialFSM::s3_on_exit()
  1221. {
  1222. CancelTimer(FAILURE_WAIT_TIMER_ID);
  1223. //SetSysVal(NET_STATUS_UNINIT);
  1224. }
  1225. unsigned int CMobileDialFSM::s3_on_event(FSMEvent* pEvt)
  1226. {
  1227. int nRes = pEvt->param1;
  1228. Dbg("s3_on_event(failed) %d, %d, %d", pEvt->iEvt, pEvt->param1, pEvt->param2);
  1229. switch(pEvt->iEvt)
  1230. {
  1231. case USER_EVT_ATTEMPT:
  1232. pEvt->SetHandled();
  1233. break;
  1234. case USER_EVT_RECOVER:
  1235. pEvt->SetHandled();
  1236. if(GetCurState() == MBD_ACTIVATION_DISCONNECTED) {
  1237. //
  1238. // 因没有处理 OnConnectionComplete: newInterface: requestID: 0 status 0} 事件
  1239. // 导致当前处于没连接状态,但是网络好的情况 [6/16/2020 17:47 @Gifur]
  1240. //
  1241. Recognized3GOr4G();
  1242. }
  1243. break;
  1244. //case USER_EVT_STATUSCHANGE:
  1245. // pEvt->SetHandled();
  1246. // {
  1247. // Dbg("s3(failed): StatusChanged[%s]", ConvertStatusToString((MOBILEDIAL_CONNECTION_STATE)nRes));
  1248. // Recognized3GOr4G();
  1249. // }
  1250. // break;
  1251. case EVT_TIMER:
  1252. pEvt->SetHandled();
  1253. if(pEvt->param1 == FAILURE_WAIT_TIMER_ID)
  1254. {
  1255. if(IsConnectedFromHB()) {
  1256. Dbg("s3(failed): EVT_TIMER Creates USER_EVT_RECOVER triggered by heartbeat !!");
  1257. PostEventLIFO(new FSMEvent(USER_EVT_RECOVER));
  1258. } else {
  1259. if(m_bPadAndFLB && !isUsingNewVerAllInOne() ) {
  1260. if(m_nAdvancedGen != 4 && m_nAdvancedGen != 2) {
  1261. int nRAS = EnumCurRasDevices();
  1262. if(nRAS >= 0 && nRAS != m_n3G) {
  1263. FSMEvent* fsmEvent = new FSMEvent(m_n3G > nRAS ? USER_EVT_DEVICE_LEAVE : USER_EVT_DEVICE_ARRIVAL);
  1264. fsmEvent->param1 = 0;
  1265. fsmEvent->param2 = 3;
  1266. PostEventLIFO(fsmEvent);
  1267. }
  1268. }
  1269. }
  1270. if(!m_bManageByUser && ++m_nRecoverMax > ATTEMPT_INTERVAL) {
  1271. Recognized3GOr4G();
  1272. PostEventLIFO(new FSMEvent(USER_EVT_ATTEMPT));
  1273. }
  1274. else {
  1275. ScheduleTimer(FAILURE_WAIT_TIMER_ID, MOBILEDIAL_CHECKDONGLE_INTERVAL);
  1276. }
  1277. }
  1278. }
  1279. break;
  1280. case USER_EVT_DEVICE_LEAVE:
  1281. pEvt->SetHandled();
  1282. BroadcastCableChangedFSMEvent(pEvt);
  1283. if(pEvt->param2 == 0) {
  1284. nRes = 1;
  1285. } else {
  1286. nRes = 0;
  1287. Recognized3GOr4G();
  1288. auto state = GetCurState();
  1289. if(state == MBD_NO_MOBILEDONGLE) {
  1290. nRes = 1;
  1291. } else if(state == MBD_ACTIVATION_DISCONNECTED) {
  1292. FSMEvent* fsmEvent = new FSMEvent(USER_EVT_DISCONNECT_COMPLETE);
  1293. fsmEvent->param1 = 0;
  1294. fsmEvent->param2 = 0;
  1295. PostEventFIFO(fsmEvent);
  1296. } else {
  1297. nRes = 2;
  1298. }
  1299. }
  1300. break;
  1301. case USER_EVT_DEVICE_ARRIVAL:
  1302. pEvt->SetHandled();
  1303. {
  1304. BroadcastCableChangedFSMEvent(pEvt);
  1305. Recognized3GOr4G();
  1306. if(GetCurState() == MBD_ACTIVATION_DISCONNECTED){
  1307. PostEventFIFO(new FSMEvent(USER_EVT_DISCONNECT_COMPLETE));
  1308. nRes = 1;
  1309. } else {
  1310. PostEventFIFO(new FSMEvent(USER_EVT_RECOVER));
  1311. nRes = 0;
  1312. }
  1313. }
  1314. break;
  1315. case USER_EVT_CONNECT_COMPLETE:
  1316. BroadcastCableChangedFSMEvent(pEvt);
  1317. case USER_EVT_DISCONNECT_COMPLETE:
  1318. {
  1319. pEvt->SetHandled();
  1320. //如果窗口是打开的那就让拨号窗口去更新状态
  1321. if(!m_hMainWnd) {
  1322. Recognized3GOr4G();
  1323. }
  1324. //PostEventLIFO(new FSMEvent(USER_EVT_RECOVER));
  1325. }
  1326. break;
  1327. default:
  1328. break;
  1329. }
  1330. return nRes;
  1331. }
  1332. //OffLine
  1333. void CMobileDialFSM::s4_on_entry()
  1334. {
  1335. LOG_FUNCTION();
  1336. #if !SUPPORT_DIAL_FROM_WEB
  1337. m_34GDialSuc = 0;
  1338. ShowFatalError("未检测到无线宽带上网卡,请重连设备");
  1339. m_bConnected = false;
  1340. m_dwCkFailCount = 0;
  1341. SendNetStatusMsg();
  1342. SetSysVal(NET_STATUS_NO_DEVICE);
  1343. ScheduleTimer(CHECK_NETCARD_TIMER_ID, MOBILEDIAL_CHECKDONGLE_INTERVAL);
  1344. #else
  1345. Dbg("Walk normal way in connect style.");
  1346. m_TimeoutSec = DEFAULT_TIMEOUT_SEC;
  1347. CheckConnectionTask* task = new CheckConnectionTask(this);
  1348. GetEntityBase()->GetFunction()->PostThreadPoolTask(task);
  1349. m_offDeviceFlag = 0;
  1350. #endif
  1351. }
  1352. void CMobileDialFSM::s4_on_exit()
  1353. {
  1354. m_offDeviceFlag = 0;
  1355. CancelTimer(CHECK_NETCARD_TIMER_ID);
  1356. //SetSysVal(NET_STATUS_UNINIT);
  1357. }
  1358. unsigned int CMobileDialFSM::s4_on_event(FSMEvent* pEvt)
  1359. {
  1360. int nRes = pEvt->param1;
  1361. Dbg("s4_on_event %d, %d, %d", pEvt->iEvt, pEvt->param1, pEvt->param2);
  1362. switch(pEvt->iEvt)
  1363. {
  1364. #if SUPPORT_DIAL_FROM_WEB
  1365. case USER_EVT_CKECK_CONNECTION_FINISHED:
  1366. pEvt->SetHandled();
  1367. {
  1368. bool bShouldBackup = !!(pEvt->param2 & 0x4);
  1369. bool bRealBackup = !!(pEvt->param2 & 0x2);
  1370. CSimpleStringA csIpPrint = bRealBackup ? m_csIpBackup : m_csIp;
  1371. CSimpleStringA csPortPrint = bRealBackup ? m_csPortBackup : m_csPort;
  1372. if(nRes == 0) {
  1373. CSimpleStringA csMsg;
  1374. csMsg = CSimpleStringA::Format("s4(no device): Connect to server(%s:%s) failed(%d) FailCount(%d) FailMax(%d).",
  1375. (LPCTSTR)csIpPrint, (LPCTSTR)csPortPrint, m_dwLsErrCode, ++m_dwCkFailCount, m_dwFailMax);
  1376. LogEvent(Severity_Middle, CombineErrorCode(MBD_CHECK_NETWORK_CONNETCTED), (LPCTSTR)csMsg);
  1377. const MOBILEDIAL_CONNECTION_STATE state = GetCurState();
  1378. Dbg("s4(no device): %s.", ConvertStatusToString(state));
  1379. // fix bug which lead netbroken show more easy and earlier --Josephus at 8:59:47 20161211
  1380. if(m_dwCkFailCount < DEFAULT_FAIL_CONNECT_LESS) {
  1381. m_TimeoutSec = DEFAULT_TIMEOUT_SEC + 2 * m_dwCkFailCount;
  1382. if(bShouldBackup && !bRealBackup) {
  1383. Dbg("backup ip and port maybe no exist.");
  1384. break;
  1385. }
  1386. //未达到连续次数的网络断开情况,防止状态跳转
  1387. nRes = 4;
  1388. if(state == MBD_NETWORK_TIMEOUT) {
  1389. Sleep(MOBILEDIAL_QUICK_TIMER_INTERVAL);
  1390. }
  1391. else {
  1392. Sleep( MOBILEDIAL_FASTER_INTERVAL);
  1393. }
  1394. CheckConnectionTask* task = new CheckConnectionTask(this);
  1395. if(bRealBackup) {
  1396. task->m_bUseBackup = true;
  1397. }
  1398. GetEntityBase()->GetFunction()->PostThreadPoolTask(task);
  1399. }
  1400. else if(!bShouldBackup && m_bBackupAvail) {
  1401. m_dwCkFailCount = 0;
  1402. m_TimeoutSec = DEFAULT_TIMEOUT_SEC;
  1403. //未达到连续次数的网络断开情况,防止状态跳转
  1404. nRes = 4;
  1405. if(state == MBD_NETWORK_TIMEOUT){
  1406. Sleep(MOBILEDIAL_QUICK_TIMER_INTERVAL);
  1407. }
  1408. else{
  1409. Sleep(MOBILEDIAL_FASTER_INTERVAL);
  1410. }
  1411. CheckConnectionTask* task = new CheckConnectionTask(this);
  1412. task->m_bUseBackup = true;
  1413. GetEntityBase()->GetFunction()->PostThreadPoolTask(task);
  1414. } else {
  1415. if(m_offDeviceFlag == 0) {
  1416. m_34GDialSuc = 0;
  1417. ShowFatalError("未检测到无线宽带上网卡,请重连设备");
  1418. m_bConnected = false;
  1419. m_dwCkFailCount = 0;
  1420. SendNetStatusMsg();
  1421. SetSysVal(NET_STATUS_NO_DEVICE);
  1422. m_offDeviceFlag = 1;
  1423. }
  1424. ScheduleTimer(CHECK_NETCARD_TIMER_ID, MOBILEDIAL_CHECKDONGLE_INTERVAL);
  1425. }
  1426. }
  1427. else { //没网卡也能连接上网,适用于分行使用网页拨号上网的情况
  1428. Dbg("s4(no device): Connect to server(%s:%s) result(%d)(%d), pad&FLB(%d) %s",
  1429. (LPCTSTR)csIpPrint, (LPCTSTR)csPortPrint, nRes,
  1430. m_dwLsErrCode, m_bPadAndFLB,
  1431. ConvertStatusToString(GetCurState()));
  1432. }
  1433. m_dwOldErrCode = m_dwLsErrCode;
  1434. }
  1435. break;
  1436. #endif
  1437. case USER_EVT_DEVICE_ARRIVAL:
  1438. pEvt->SetHandled();
  1439. {
  1440. BroadcastCableChangedFSMEvent(pEvt);
  1441. Recognized3GOr4G();
  1442. if(GetCurState() == MBD_ACTIVATION_DISCONNECTED){
  1443. nRes = 1;
  1444. } else {
  1445. nRes = 0;
  1446. }
  1447. }
  1448. break;
  1449. case EVT_TIMER:
  1450. if(pEvt->param1 == CHECK_NETCARD_TIMER_ID)
  1451. {
  1452. pEvt->SetHandled();
  1453. if(m_nAdvancedGen != 4 && m_nAdvancedGen != 2) {
  1454. if(EnumCurRasDevices() > 0) {
  1455. FSMEvent* pEvt = new FSMEvent(USER_EVT_DEVICE_ARRIVAL);
  1456. pEvt->param1 = 0;
  1457. pEvt->param2 = 3;
  1458. PostEventLIFO(pEvt);
  1459. break;
  1460. } else {
  1461. #if !SUPPORT_DIAL_FROM_WEB
  1462. ScheduleTimer(CHECK_NETCARD_TIMER_ID, MOBILEDIAL_CHECKDONGLE_INTERVAL);
  1463. #endif
  1464. }
  1465. } else if(m_nAdvancedGen == 2) {
  1466. LOG_ASSERT(mMBBParam.connectType == MBB_CONNECT_WIRE);
  1467. if(CheckLANConnectStatus() == 0) {
  1468. //SetHWStatus(MBD_NO_MOBILEDONGLE);
  1469. } else {
  1470. //SetHWStatus(MBD_ACTIVATION_CONNECTED);
  1471. FSMEvent* fsmEvent = new FSMEvent(USER_EVT_DEVICE_ARRIVAL);
  1472. fsmEvent->param1 = 0;
  1473. fsmEvent->param2 = 2;
  1474. PostEventFIFO(fsmEvent);
  1475. break;
  1476. }
  1477. }
  1478. #if SUPPORT_DIAL_FROM_WEB
  1479. m_TimeoutSec = DEFAULT_TIMEOUT_SEC;
  1480. CheckConnectionTask* task = new CheckConnectionTask(this);
  1481. GetEntityBase()->GetFunction()->PostThreadPoolTask(task);
  1482. #endif
  1483. }
  1484. break;
  1485. default:
  1486. break;
  1487. }
  1488. return nRes;
  1489. }
  1490. //Deactivated
  1491. void CMobileDialFSM::s5_on_entry()
  1492. {
  1493. LOG_FUNCTION();
  1494. LogEvent(Severity_High, CombineErrorCode(MBD_CABLE_DIALUP_DISCONNETED), "无线宽带上网卡未连接");
  1495. //BroadcastCableChangedEvent(MBD_CABLE_DIALUP_DISCONNETED);
  1496. SendMsgToDialog("无线宽带上网卡未连接");
  1497. m_bConnected = false;
  1498. m_34GDialSuc = 0;
  1499. m_dwCkFailCount = 0;
  1500. SendNetStatusMsg();
  1501. SetSysVal(NET_STATUS_DIAL_OFF);
  1502. if(!m_bManageByUser || g_bFirstConnect) {
  1503. if(g_bFirstConnect) {
  1504. g_bFirstConnect = FALSE;
  1505. Dbg("Execute First ConnectTask");
  1506. }
  1507. // 处理3G网卡在连接的状态下直接拔掉的情形:
  1508. // 先接收到断开连接的事件,后续才发现到设备拔离的事件,所以要做延迟拨号处理,但测试发现尽管延迟了3秒都没有起到真正的作用
  1509. if(m_n3G > 0) {
  1510. ScheduleTimer(CHECK_PRE_RASDEVICE_CHANGED_TIMER_ID, MOBILEDIAL_FASTERDIALRT_INTERVAL);
  1511. } else {
  1512. ConnectTask* task = new ConnectTask(this);
  1513. GetEntityBase()->GetFunction()->PostThreadPoolTask(task);
  1514. }
  1515. } else {
  1516. Dbg("Detect ManageByUser mode !!!");
  1517. if(m_n3G > 0) {
  1518. ScheduleTimer(CHECK_RASDEVICE_CHANGED_TIMER_ID, MOBILEDIAL_FASTERDIALRT_INTERVAL);
  1519. }
  1520. }
  1521. }
  1522. void CMobileDialFSM::s5_on_exit()
  1523. {
  1524. CancelTimer(CHECK_CONNECT_DIAL_TIMER_ID);
  1525. CancelTimer(CHECK_CONNECT_TIMER_ID);
  1526. CancelTimer(CHECK_RASDEVICE_CHANGED_TIMER_ID);
  1527. //SetSysVal(NET_STATUS_UNINIT);
  1528. }
  1529. unsigned int CMobileDialFSM::s5_on_event(FSMEvent* pEvt)
  1530. {
  1531. int nRes = pEvt->param1;
  1532. Dbg("s5_on_event %d, %d, %d", pEvt->iEvt, pEvt->param1, pEvt->param2);
  1533. switch(pEvt->iEvt)
  1534. {
  1535. case USER_EVT_CONNECT_COMPLETE:
  1536. pEvt->SetHandled();
  1537. {
  1538. BroadcastCableChangedFSMEvent(pEvt);
  1539. if(pEvt->param2 == ADVANCE_RAS) {
  1540. if(pEvt->param1 == 1) {
  1541. UpdateRasConnectState(true);
  1542. nRes = 0;
  1543. } else if(pEvt->param1 == -1) {
  1544. _FormatRasError(g_dwRasError);
  1545. if(m_34GReserved != ADVANCE_RAS) {
  1546. Dbg("Manual dial from outside(%d) ?", m_34GReserved);
  1547. m_34GReserved = ADVANCE_RAS;
  1548. } else {
  1549. EraseTheRecentInfo((ULONG)g_hrasconn);
  1550. }
  1551. // Move here [1/24/2019 17:34 @Gifur]
  1552. RasHangUpSuffix();
  1553. SetRasStatus(MBD_RASDIAL_FAILED);
  1554. m_34GDialSuc = 0;
  1555. nRes = 1;
  1556. }
  1557. } else if(pEvt->param2 == ADVANCE_MBB) {
  1558. LOG_ASSERT(g_DongleInfos.GetCount() > 0);
  1559. _GetMBBDeviceInfo(g_DongleInfos[0]);
  1560. }
  1561. }
  1562. break;
  1563. case USER_EVT_CONNECT_FINISHED:
  1564. pEvt->SetHandled();
  1565. {
  1566. if(pEvt->param1 < 0) {
  1567. nRes = 0;
  1568. } else {
  1569. SendNetStatusMsg();
  1570. if(pEvt->param1 == ADVANCE_RAS)
  1571. {
  1572. LogEvent(Severity_Middle,CombineErrorCode(MBD_RAS_DIALUP_CONNETED),"3G拨号成功,等待响应");
  1573. m_34GDialSuc = ADVANCE_RAS;
  1574. ScheduleTimer(CHECK_CONNECT_DIAL_TIMER_ID, MOBILEDIAL_FASTERDIALRT_INTERVAL);
  1575. }
  1576. else if(pEvt->param1 == ADVANCE_MBN)
  1577. {
  1578. LogEvent(Severity_Middle,CombineErrorCode(MBD_MBN_DIALUP_CONNETED),"4G拨号成功,等待响应");
  1579. m_34GDialSuc = ADVANCE_MBN;
  1580. ScheduleTimer(CHECK_CONNECT_MBN_TIMER_ID, MOBILEDIAL_WAITDIALRT_INTERVAL);
  1581. }
  1582. else if(pEvt->param1 == ADVANCE_MBB) {
  1583. LogEvent(Severity_Middle,CombineErrorCode(MBD_MBB_DIALUP_CONNECTED),"MBB 拨号成功");
  1584. m_34GDialSuc = ADVANCE_MBB;
  1585. FSMEvent* fsmEvent = new FSMEvent(USER_EVT_CONNECT_COMPLETE);
  1586. fsmEvent->param1 = 0;
  1587. fsmEvent->param2 = ADVANCE_MBB;
  1588. PostEventFIFO(fsmEvent);
  1589. }
  1590. }
  1591. }
  1592. break;
  1593. case EVT_TIMER:
  1594. {
  1595. if(pEvt->param1 == CHECK_CONNECT_MBN_TIMER_ID) {
  1596. pEvt->SetHandled();
  1597. Dbg("timeout after 4g dial suc, change status auto.");
  1598. nRes = 0;
  1599. if(m_pMbn->CheckAndRefleshCurInterface()) {
  1600. const MOBILEDIAL_CONNECTION_STATE state = GetCurState();
  1601. if(state < MBD_NETWORK_SEPERATE)
  1602. {//find connected.
  1603. FSMEvent* pEvent = new FSMEvent(USER_EVT_CONNECT_COMPLETE);
  1604. pEvent->param1 = 0;
  1605. pEvent->param2 = 0;
  1606. PostEventFIFO(pEvent);
  1607. nRes = 1; //cancel event.
  1608. LogEvent(Severity_Middle,CombineErrorCode(MBD_UNKNOWN),"响应超时,自动跳转");
  1609. }
  1610. else if(state == MBD_ACTIVATION_CONNECTING)
  1611. {//still connecting
  1612. nRes = 1; //cancel event.
  1613. ScheduleTimer(CHECK_CONNECT_MBN_TIMER_ID, MOBILEDIAL_FASTERDIALRT_INTERVAL);
  1614. }
  1615. else if(state == MBD_ACTIVATION_DISCONNECTED)
  1616. {//hardly happen but still has chance.
  1617. nRes = 1; //cancel event.
  1618. if(!m_bManageByUser) {
  1619. ConnectTask* task = new ConnectTask(this);
  1620. GetEntityBase()->GetFunction()->PostThreadPoolTask(task);
  1621. } else {
  1622. Dbg("Detect ManageByUser mode !!!");
  1623. }
  1624. }
  1625. }
  1626. }
  1627. else if(pEvt->param1 == CHECK_CONNECT_DIAL_TIMER_ID)
  1628. {
  1629. pEvt->SetHandled();
  1630. Dbg("timeout after 3g dial suc, change status auto.");
  1631. nRes = 0;
  1632. MOBILEDIAL_CONNECTION_STATE state = GetRasStatus();
  1633. if(state == MBD_ACTIVATION_CONNECTED) {
  1634. nRes = 1; //cancel event.
  1635. FSMEvent* pEvent = new FSMEvent(USER_EVT_CONNECT_COMPLETE);
  1636. pEvent->param1 = 0;
  1637. pEvent->param2 = ADVANCE_RAS;
  1638. PostEventFIFO(pEvent);
  1639. LogEvent(Severity_Middle,CombineErrorCode(MBD_RAS_DIALUP_CONNETED),"3G拨号成功,超时跳转");
  1640. }
  1641. else if(state == MBD_ACTIVATION_CONNECTING) {
  1642. nRes = 1; //cancel event.
  1643. if(MOBILEDIAL_WAITDIALRT_INTERVAL > MOBILEDIAL_FASTERDIALRT_INTERVAL) {
  1644. ScheduleTimer(CHECK_CONNECT_DIAL_TIMER_ID,
  1645. MOBILEDIAL_WAITDIALRT_INTERVAL - MOBILEDIAL_FASTERDIALRT_INTERVAL);
  1646. } else {
  1647. ScheduleTimer(CHECK_CONNECT_DIAL_TIMER_ID, MOBILEDIAL_WAITDIALRT_INTERVAL);
  1648. }
  1649. }
  1650. }
  1651. else if(pEvt->param1 == CHECK_RASDEVICE_CHANGED_TIMER_ID
  1652. || pEvt->param1 == CHECK_PRE_RASDEVICE_CHANGED_TIMER_ID) {
  1653. if(m_nAdvancedGen != 4 && m_nAdvancedGen != 2 && EnumCurRasDevices() == 0) {
  1654. FSMEvent* fsmEvent = new FSMEvent(USER_EVT_DEVICE_LEAVE);
  1655. fsmEvent->param1 = 0;
  1656. fsmEvent->param2 = 3;
  1657. PostEventFIFO(fsmEvent);
  1658. break;
  1659. } else if(pEvt->param1 == CHECK_PRE_RASDEVICE_CHANGED_TIMER_ID) {
  1660. ConnectTask* task = new ConnectTask(this);
  1661. GetEntityBase()->GetFunction()->PostThreadPoolTask(task);
  1662. }
  1663. ScheduleTimer(CHECK_RASDEVICE_CHANGED_TIMER_ID, MOBILEDIAL_FASTERDIALRT_INTERVAL);
  1664. }
  1665. }
  1666. break;
  1667. case USER_EVT_DEVICE_LEAVE:
  1668. pEvt->SetHandled();
  1669. BroadcastCableChangedFSMEvent(pEvt);
  1670. if(pEvt->param2 == 0) {
  1671. nRes = 1;
  1672. } else {
  1673. nRes = 0;
  1674. Recognized3GOr4G();
  1675. auto state = GetCurState();
  1676. if(state == MBD_NO_MOBILEDONGLE) {
  1677. nRes = 1;
  1678. } else if(state == MBD_ACTIVATION_DISCONNECTED) {
  1679. if(!m_bManageByUser) {
  1680. ConnectTask* task = new ConnectTask(this);
  1681. GetEntityBase()->GetFunction()->PostThreadPoolTask(task);
  1682. } else {
  1683. Dbg("Detect ManageByUser mode !!!");
  1684. }
  1685. } else {
  1686. nRes = 2;
  1687. }
  1688. }
  1689. break;
  1690. default:
  1691. break;
  1692. }
  1693. return nRes;
  1694. }
  1695. void CMobileDialFSM::s6_on_entry()
  1696. {
  1697. LOG_FUNCTION();
  1698. if(m_nAdvancedGen != 4 && m_nAdvancedGen != 2 && !isUsingNewVerAllInOne()) {
  1699. ScheduleTimer(CHECK_RASDEVICE_CHANGED_TIMER_ID, DEV_CHANGED_TIMER_INTERVAL);
  1700. }
  1701. return;
  1702. }
  1703. void CMobileDialFSM::s6_on_exit()
  1704. {
  1705. LOG_FUNCTION();
  1706. CancelTimer(CHECK_RASDEVICE_CHANGED_TIMER_ID);
  1707. Recognized3GOr4G();
  1708. return;
  1709. }
  1710. unsigned int CMobileDialFSM::s6_on_event(FSMEvent* pEvt)
  1711. {
  1712. int nRes = pEvt->param1;
  1713. Dbg("s6_on_event %d, %d, %d", pEvt->iEvt, pEvt->param1, pEvt->param2);
  1714. switch(pEvt->iEvt) {
  1715. case EVT_TIMER:
  1716. if(pEvt->param1 == CHECK_RASDEVICE_CHANGED_TIMER_ID) {
  1717. pEvt->SetHandled();
  1718. int nTimerInterval = DEV_CHANGED_TIMER_INTERVAL;
  1719. int nRAS = EnumCurRasDevices();
  1720. if(nRAS != m_n3G) {
  1721. TellDlgTheResult(Error_Succeed, WM_DONGLE_CHANGE, 0);
  1722. } else {
  1723. nTimerInterval = DEV_CHANGED_TIMER_INTERVAL * 3;
  1724. }
  1725. ScheduleTimer(CHECK_RASDEVICE_CHANGED_TIMER_ID, nTimerInterval);
  1726. }
  1727. break;
  1728. default:
  1729. break;
  1730. }
  1731. return 0;
  1732. }
  1733. /*Linking*/
  1734. void CMobileDialFSM::s7_on_entry()
  1735. {
  1736. LOG_FUNCTION();
  1737. if(mMBBParam.wlanConnectParam.strPassword.IsNullOrEmpty()
  1738. || mMBBParam.wlanConnectParam.strUsername.IsNullOrEmpty()) {
  1739. SetHWStatus(MBD_RUN_CONFIG_INCOMPLEMENT);
  1740. PostEventFIFO(new FSMEvent(USER_EVT_FAILURE));
  1741. } else {
  1742. m_dwWlanConnectTimes = 0;
  1743. ScheduleTimer(TIMER_TRY_LINK_ID, MOBILEDIAL_QUICK_TIMER_INTERVAL);
  1744. SetHWStatus(MBD_WLAN_DISCONNECTED);
  1745. }
  1746. m_bConnected = false;
  1747. ShowFatalError(GetFurtherDetail());
  1748. SendNetStatusMsg();
  1749. }
  1750. void CMobileDialFSM::s7_on_exit()
  1751. {
  1752. if(GetHWStatus() == MBD_WLAN_CONNECTED) {
  1753. m_dwWlanConnectTimes = 0;
  1754. ShowFatalError("WLAN 已连接", TRUE);
  1755. Recognized3GOr4G();
  1756. }
  1757. CancelTimer(TIMER_TRY_LINK_ID);
  1758. }
  1759. unsigned int CMobileDialFSM::s7_on_event(FSMEvent* evt)
  1760. {
  1761. unsigned int uRet = 0;
  1762. Dbg("s7_on_event %d, %d, %d", evt->iEvt, evt->param1, evt->param2);
  1763. switch(evt->iEvt) {
  1764. case EVT_TIMER:
  1765. {
  1766. if(evt->param1 == TIMER_TRY_LINK_ID) {
  1767. evt->SetHandled();
  1768. if(NeedToConnectWifi()) {
  1769. SetHWStatus(MBD_WLAN_CONNECTING);
  1770. WlanConnectTask* task = new WlanConnectTask(this);
  1771. this->GetEntityBase()->GetFunction()->PostThreadPoolTask(task);
  1772. } else {
  1773. PostEventFIFO(new FSMEvent(USER_EVT_ATTEMPT));
  1774. }
  1775. ShowFatalError(GetFurtherDetail());
  1776. SendNetStatusMsg();
  1777. }
  1778. }
  1779. break;
  1780. case EVT_USER_WLAN_CONNECT_FINISHED:
  1781. {
  1782. evt->SetHandled();
  1783. uRet = evt->param1;
  1784. m_dwWlanConnectTimes++;
  1785. if(evt->param1 == 0) {
  1786. const ErrorCodeEnum ec = (ErrorCodeEnum)evt->param2;
  1787. if(ec == Error_NotExist) {
  1788. SetHWStatus(MBD_WLAN_ENTRY_NO_FOUND);
  1789. } else {
  1790. SetHWStatus(MBD_WLAN_CONNECT_FAILED);
  1791. }
  1792. ShowFatalError(GetFurtherDetail());
  1793. SendNetStatusMsg();
  1794. if(m_dwWlanConnectTimes >= MAX_TRY_WLAN_CONNETC_TIMES) {
  1795. LogError(Severity_High, Error_TimeOut, CombineErrorCode(MBD_WLAN_CONNECT_FAILED),
  1796. CSimpleStringA::Format("连续%d次连接WLAN失败,请检查MIFI设备是否正常运行!", m_dwWlanConnectTimes)
  1797. );
  1798. ScheduleTimer(TIMER_TRY_LINK_ID, MOBILEDIAL_WLAN_CONNECT_INTERVAL * 2);
  1799. } else {
  1800. ScheduleTimer(TIMER_TRY_LINK_ID, MOBILEDIAL_WLAN_CONNECT_INTERVAL);
  1801. }
  1802. } else {
  1803. SetHWStatus(MBD_WLAN_CONNECTED);
  1804. }
  1805. }
  1806. break;
  1807. case EVT_USER_WIRELESS_STATE:
  1808. //BroadcastCableChangedFSMEvent(evt);
  1809. evt->SetHandled();
  1810. uRet = evt->param1;
  1811. if(evt->param1 == 1) {
  1812. SetHWStatus(MBD_WLAN_CONNECTED);
  1813. } else {
  1814. SetHWStatus(MBD_WLAN_CONNECT_FAILED);
  1815. ScheduleTimer(TIMER_TRY_LINK_ID, MOBILEDIAL_WLAN_CONNECT_INTERVAL);
  1816. }
  1817. break;
  1818. case USER_EVT_FAILURE:
  1819. evt->SetHandled();
  1820. break;
  1821. default:
  1822. break;
  1823. }
  1824. return uRet;
  1825. }
  1826. void CMobileDialFSM::s8_on_entry()
  1827. {
  1828. LOG_FUNCTION();
  1829. MBBConfigUpdateTask* task = new MBBConfigUpdateTask(this);
  1830. GetEntityBase()->GetFunction()->PostThreadPoolTask(task);
  1831. }
  1832. void CMobileDialFSM::s8_on_exit()
  1833. {
  1834. Recognized3GOr4G();
  1835. }
  1836. unsigned int CMobileDialFSM::s8_on_event(FSMEvent* evt)
  1837. {
  1838. unsigned int uRet = 0;
  1839. Dbg("s8_on_event %d, %d, %d", evt->iEvt, evt->param1, evt->param2);
  1840. if(evt->iEvt == USER_EVT_CONFIG_DETECT_FINISHED) {
  1841. evt->SetHandled();
  1842. if(evt->param1 == 1) {
  1843. if(m_nAdvancedGen != ADVANCE_MBB) {
  1844. InitHuaweiComponent();
  1845. m_nAdvancedGen = ADVANCE_MBB;
  1846. CSmartPointer<IConfigInfo> spConfigRun;
  1847. GetEntityBase()->GetFunction()->OpenConfig(Config_Run, spConfigRun);
  1848. spConfigRun->WriteConfigValueInt("init", "GenAdvanced", m_nAdvancedGen);
  1849. SetHWStatus(MBD_WLAN_CONFIG_REBOOT);
  1850. ShowFatalError(GetFurtherDetail());
  1851. SendNetStatusMsg();
  1852. ScheduleTimer(TIMER_CHECK_DEVICE_ID, DEV_CHANGED_TIMER_INTERVAL);
  1853. } else {
  1854. SetHWStatus(MBD_WLAN_DEVICE_PLUG_OFF);
  1855. ShowFatalError(GetFurtherDetail());
  1856. SendNetStatusMsg();
  1857. ScheduleTimer(TIMER_CHECK_DEVICE_NOCHANGED_ID, DEV_CHANGED_TIMER_INTERVAL);
  1858. }
  1859. uRet = 1; //stop from backing to s0.
  1860. }
  1861. }
  1862. else if(evt->iEvt == EVT_TIMER) {
  1863. if(evt->param1 == TIMER_CHECK_DEVICE_ID || evt->param1 == TIMER_CHECK_DEVICE_NOCHANGED_ID) {
  1864. evt->SetHandled();
  1865. if(!m_HwDeviceMnt->detect_devices()) {
  1866. if(evt->param1 == TIMER_CHECK_DEVICE_ID) {
  1867. Dbg("wait the selfchecker entity to kill it or manully.");
  1868. m_ecSelfTest = Error_InvalidState;
  1869. SetHWStatus(MBD_WLAN_CONFIG_WAIT_REBOOT);
  1870. ShowFatalError(GetFurtherDetail());
  1871. SendNetStatusMsg();
  1872. } else {
  1873. uRet = 1;
  1874. }
  1875. } else {
  1876. if(evt->param1 == TIMER_CHECK_DEVICE_ID) {
  1877. SetHWStatus(MBD_WLAN_CONFIG_REBOOT);
  1878. ShowFatalError(GetFurtherDetail());
  1879. SendNetStatusMsg();
  1880. ScheduleTimer(TIMER_CHECK_DEVICE_ID, DEV_CHANGED_TIMER_INTERVAL);
  1881. } else {
  1882. SetHWStatus(MBD_WLAN_DEVICE_PLUG_OFF);
  1883. ShowFatalError(GetFurtherDetail());
  1884. SendNetStatusMsg();
  1885. ScheduleTimer(TIMER_CHECK_DEVICE_NOCHANGED_ID, DEV_CHANGED_TIMER_INTERVAL);
  1886. }
  1887. }
  1888. }
  1889. }
  1890. return uRet;
  1891. }
  1892. int CMobileDialFSM::EnumPhoneEntry()
  1893. {
  1894. DWORD dwCb = 0;
  1895. DWORD dwRet = ERROR_SUCCESS;
  1896. DWORD dwEntries = 0;
  1897. LPRASENTRYNAME lpRasEntryName = NULL;
  1898. // Call RasEnumEntries with lpRasEntryName = NULL. dwCb is returned with the required buffer size and
  1899. // a return code of ERROR_BUFFER_TOO_SMALL
  1900. dwRet = RasEnumEntries(NULL, NULL, lpRasEntryName, &dwCb, &dwEntries);
  1901. if (dwRet == ERROR_BUFFER_TOO_SMALL)
  1902. {
  1903. // Allocate the memory needed for the array of RAS entry names.
  1904. lpRasEntryName = (LPRASENTRYNAME) HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, dwCb);
  1905. if (lpRasEntryName == NULL){
  1906. Dbg("HeapAlloc failed when RasEnumEntries");
  1907. return -1;
  1908. }
  1909. // The first RASENTRYNAME structure in the array must contain the structure size
  1910. lpRasEntryName[0].dwSize = sizeof(RASENTRYNAME);
  1911. // Call RasEnumEntries to enumerate all RAS entry names
  1912. dwRet = RasEnumEntries(NULL, NULL, lpRasEntryName, &dwCb, &dwEntries);
  1913. // If successful, print the RAS entry names
  1914. if (ERROR_SUCCESS == dwRet)
  1915. {
  1916. Dbg("The following RAS entry names were found:");
  1917. for (DWORD i = 0; i < dwEntries; i++) {
  1918. if (m_csEntryName.GetLength() != 0 &&
  1919. _strnicmp(lpRasEntryName[i].szEntryName,m_csEntryName,m_csEntryName.GetLength()) == 0) {
  1920. Dbg("Find specified entry: %s",(LPCTSTR)m_csEntryName);
  1921. LPRASENTRY lpRasentry = NULL;
  1922. LPBYTE lpDevInfo = NULL;
  1923. DWORD dwEntrySz = 0, dwDevInfoSz = 0;
  1924. dwRet = RasGetEntryProperties(NULL, lpRasEntryName[i].szEntryName, NULL, &dwEntrySz, NULL, &dwDevInfoSz);
  1925. if(dwRet == ERROR_BUFFER_TOO_SMALL) {
  1926. if(dwEntrySz != 0)
  1927. lpRasentry = (LPRASENTRY)HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, dwEntrySz);
  1928. if(dwDevInfoSz != 0)
  1929. lpDevInfo = (LPBYTE)HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, dwDevInfoSz);
  1930. }
  1931. if(lpRasentry != NULL) {
  1932. lpRasentry->dwSize = sizeof(RASENTRY);
  1933. dwRet = RasGetEntryProperties(NULL, lpRasEntryName[i].szEntryName, lpRasentry, &dwEntrySz, lpDevInfo, &dwDevInfoSz);
  1934. if(dwRet != 0) {
  1935. Dbg("RasGetEntryProperties twice failed: %d", dwRet);
  1936. } else {
  1937. Dbg("DeviceName: %s, DeviceType: %s", lpRasentry->szDeviceName, lpRasentry->szDeviceType);
  1938. CSimpleStringA csEntryGUID = GuidToString(lpRasentry->guidId);
  1939. if(!csEntryGUID.IsNullOrEmpty())
  1940. {
  1941. CSmartPointer<IConfigInfo> spConfigRun;
  1942. CSimpleStringA csValue;
  1943. GetEntityBase()->GetFunction()->OpenConfig(Config_Run, spConfigRun);
  1944. if(Error_Succeed != spConfigRun->ReadConfigValue(csEntryGUID, "Status", csValue)
  1945. || csValue.GetLength() <= 0) {
  1946. Dbg("Cannot found entry(%s) info from runtime file: (%s), set temporary",
  1947. (LPCTSTR)csEntryGUID, (LPCTSTR)csValue);
  1948. spConfigRun->WriteConfigValue(csEntryGUID, "Type", "RAS");
  1949. spConfigRun->WriteConfigValue(csEntryGUID, "Status", "Invalid-Temporary");
  1950. spConfigRun->WriteConfigValue(csEntryGUID, "EntryName", m_csEntryName);
  1951. spConfigRun->WriteConfigValue("RAS", "EntryGUID", csEntryGUID);
  1952. }
  1953. }
  1954. }
  1955. }
  1956. if(lpRasentry) {
  1957. HeapFree(GetProcessHeap(), 0, lpRasentry);
  1958. lpRasentry = NULL;
  1959. }
  1960. if(lpDevInfo) {
  1961. HeapFree(GetProcessHeap(), 0, lpDevInfo);
  1962. lpDevInfo = NULL;
  1963. }
  1964. //Deallocate memory for the connection buffer
  1965. HeapFree(GetProcessHeap(), 0, lpRasEntryName);
  1966. lpRasEntryName = NULL;
  1967. return 0;
  1968. } else {
  1969. Dbg("EntryName[%d]: %s", i, lpRasEntryName[i].szEntryName);
  1970. }
  1971. }
  1972. }
  1973. //Deallocate memory for the connection buffer
  1974. HeapFree(GetProcessHeap(), 0, lpRasEntryName);
  1975. lpRasEntryName = NULL;
  1976. return -2;
  1977. }
  1978. // There was either a problem with RAS or there are RAS entry names to enumerate
  1979. if(dwEntries >= 1) {
  1980. Dbg("The operation failed to acquire the buffer size.");
  1981. } else {
  1982. Dbg("There were no RAS entry names found.");
  1983. return -2;
  1984. }
  1985. return -3;
  1986. }
  1987. int CMobileDialFSM::UpdateRasConnectState(bool bUpdateCmd)
  1988. {
  1989. LOG_FUNCTION();
  1990. DWORD dwCb = 0;
  1991. DWORD dwRet = ERROR_SUCCESS;
  1992. DWORD dwConnections = 0;
  1993. LPRASCONN lpRasConn = NULL;
  1994. if(bUpdateCmd) {
  1995. int cnt = g_DongleInfos.GetCount();
  1996. for(int i=0; i<cnt; ++i) {
  1997. CSimpleStringA strBackup(true);
  1998. if(g_DongleInfos[i].usDeviceType == DEVTYPE_MODEM) {
  1999. if(g_DongleInfos[i].state == MBD_ACTIVATION_CONNECTED) {
  2000. //if(g_DongleInfos[i].subInfo.mbnCntState == MBN_ACTIVATION_STATE_ACTIVATED) {
  2001. Dbg("old connected entry: %s", (LPCTSTR)g_DongleInfos[i].subInfo.strProfileName);
  2002. strBackup = (LPCTSTR)g_DongleInfos[i].subInfo.strProfileName;
  2003. g_DongleInfos[i].state = MBD_ACTIVATION_DISCONNECTED;
  2004. SetRasStatus(MBD_ACTIVATION_DISCONNECTED);
  2005. }
  2006. g_DongleInfos[i].subInfo.mbnCntState = MBN_ACTIVATION_STATE_DEACTIVATED;
  2007. g_DongleInfos[i].subInfo.strProfileName = "";
  2008. int cnt2 = g_DongleInfos[i].profileList.GetCount();
  2009. if(cnt2 > 0 && !strBackup.IsNullOrEmpty()) {
  2010. MBNProfileList& profiles = g_DongleInfos[i].profileList;
  2011. for(int j=0; j<cnt2; ++j) {
  2012. if(!profiles[j].strProfileName.Compare(strBackup)) {
  2013. Dbg("Comfire: found the old connected ras entry: %s",
  2014. (LPCTSTR)profiles[j].AccessInfo.strProfileDesc);
  2015. break;
  2016. }
  2017. }
  2018. } else if(cnt2 == 0) {
  2019. g_DongleInfos[i].state = MBD_NO_ENTRY;
  2020. }
  2021. }
  2022. }
  2023. }
  2024. dwRet = RasEnumConnections(lpRasConn, &dwCb, &dwConnections);
  2025. if (dwRet == ERROR_BUFFER_TOO_SMALL && dwConnections > 0) {
  2026. lpRasConn = (LPRASCONN) HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, dwCb);
  2027. if (lpRasConn == NULL) {
  2028. SetRasStatus(MBD_INTERNAL_ERROR);
  2029. Dbg("HeapAlloc failed when RasEnumConnections");
  2030. return -1;
  2031. }
  2032. lpRasConn[0].dwSize = sizeof(RASCONN);
  2033. int nRes = dwConnections;
  2034. dwRet = RasEnumConnections(lpRasConn, &dwCb, &dwConnections);
  2035. if (ERROR_SUCCESS == dwRet) {
  2036. Dbg("The following RAS connections are currently active:");
  2037. for (DWORD i = 0; i < dwConnections; i++) {
  2038. RASCONNSTATUS rasStatus;
  2039. rasStatus.dwSize = sizeof(RASCONNSTATUS);
  2040. dwRet = RasGetConnectStatus(lpRasConn[i].hrasconn, &rasStatus);
  2041. const RASCONNSTATE& cntState = rasStatus.rasconnstate;
  2042. if(dwRet == ERROR_SUCCESS){
  2043. Dbg("%s - rasconnstate: %d", lpRasConn[i].szEntryName, rasStatus.rasconnstate);
  2044. CSimpleStringA csEntryGUID = GuidToString(lpRasConn[i].guidEntry);
  2045. SetGlobalConnectState(csEntryGUID, cntState);
  2046. if(cntState == RASCS_Connected) {
  2047. Dbg("Reset the ras handle to bcz it has connected");
  2048. g_hrasconn = lpRasConn[i].hrasconn;
  2049. SetRasStatusWithConnStatus(cntState);
  2050. //if(g_hrasconn != lpRasConn[i].hrasconn)
  2051. if(m_csCurEntryGUID.Compare(csEntryGUID))
  2052. {
  2053. m_csCurEntryGUID = CSimpleStringA((LPCTSTR)csEntryGUID);
  2054. CSmartPointer<IConfigInfo> spConfigRun;
  2055. CSimpleStringA csValue;
  2056. GetEntityBase()->GetFunction()->OpenConfig(Config_Run, spConfigRun);
  2057. if(Error_Succeed != spConfigRun->ReadConfigValue(csEntryGUID, "Status", csValue)
  2058. || csValue.GetLength() <= 0 || csValue.IsStartWith("Invalid")) {
  2059. Dbg("Cannot found entry info from runtime file or invalid: %s", (LPCTSTR)csValue);
  2060. } else {
  2061. Dbg("Read %s from run-config", (LPCTSTR)csEntryGUID);
  2062. CSimpleStringA csTest;
  2063. spConfigRun->ReadConfigValue("RAS", "EntryGUID", csTest);
  2064. Dbg("test: %s", (LPCTSTR)csTest);
  2065. spConfigRun->ReadConfigValue(csEntryGUID, "PhoneNum", m_csPhoneNum);
  2066. spConfigRun->ReadConfigValue(csEntryGUID, "EntryName", m_csEntryName);
  2067. spConfigRun->ReadConfigValue(csEntryGUID, "UserName", m_csUserName);
  2068. #ifdef USING_ENCRYPT
  2069. spConfigRun->ReadConfigValue(csEntryGUID, "Password", csValue);
  2070. m_csPassword = DecryptString(csValue);
  2071. #else
  2072. spConfigRun->ReadConfigValue(csEntryGUID, "Password", m_csPassword);
  2073. #endif
  2074. spConfigRun->ReadConfigValue(csEntryGUID, "APN", m_csVpnName);
  2075. }
  2076. }
  2077. } else if(g_hrasconn == lpRasConn[i].hrasconn) {
  2078. SetRasStatusWithConnStatus(cntState);
  2079. }
  2080. }else{
  2081. Dbg("%s - RasGetConnectStatus failed: %u", lpRasConn[i].szEntryName, dwRet);
  2082. }
  2083. }
  2084. } else {
  2085. Dbg("RasEnumConnections twice failed %u", dwRet);
  2086. SetRasStatus(MBD_INTERNAL_ERROR);
  2087. nRes = -1;
  2088. }
  2089. //Deallocate memory for the connection buffer
  2090. HeapFree(GetProcessHeap(), 0, lpRasConn);
  2091. lpRasConn = NULL;
  2092. return nRes;
  2093. }
  2094. // There was either a problem with RAS or there are no connections to enumerate
  2095. if(dwConnections >= 1) {
  2096. Dbg("The operation failed to acquire the buffer size.");
  2097. SetRasStatus(MBD_INTERNAL_ERROR);
  2098. return -1;
  2099. }
  2100. Dbg("There are no active RAS connections.");
  2101. SetRasStatus(MBD_ACTIVATION_DISCONNECTED);
  2102. return 0;
  2103. }
  2104. int CMobileDialFSM::DealConnctionActivity(LPCTSTR lpcszEntryName)
  2105. {
  2106. LOG_FUNCTION();
  2107. DWORD dwCb = 0;
  2108. DWORD dwRet = ERROR_SUCCESS;
  2109. DWORD dwConnections = 0;
  2110. LPRASCONN lpRasConn = NULL;
  2111. dwRet = RasEnumConnections(lpRasConn, &dwCb, &dwConnections);
  2112. if (dwRet == ERROR_BUFFER_TOO_SMALL && dwConnections > 0) {
  2113. lpRasConn = (LPRASCONN) HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, dwCb);
  2114. if (lpRasConn == NULL) {
  2115. Dbg("HeapAlloc failed when RasEnumConnections");
  2116. return -1;
  2117. }
  2118. lpRasConn[0].dwSize = sizeof(RASCONN);
  2119. int nRes = 0;
  2120. dwRet = RasEnumConnections(lpRasConn, &dwCb, &dwConnections);
  2121. if (ERROR_SUCCESS == dwRet) {
  2122. Dbg("The following RAS connections are currently active: %d", dwConnections);
  2123. for (DWORD i = 0; i < dwConnections; i++) {
  2124. RASCONNSTATUS rasStatus;
  2125. rasStatus.dwSize = sizeof(RASCONNSTATUS);
  2126. dwRet = RasGetConnectStatus(lpRasConn[i].hrasconn, &rasStatus);
  2127. const RASCONNSTATE& cntState = rasStatus.rasconnstate;
  2128. if(dwRet == ERROR_SUCCESS){
  2129. Dbg("%s - rasconnstate: %d", lpRasConn[i].szEntryName, rasStatus.rasconnstate);
  2130. if(lpcszEntryName != NULL && strlen(lpcszEntryName) > 0
  2131. && !strcmp(lpRasConn[i].szEntryName, lpcszEntryName)) {
  2132. CSimpleStringA csEntryGUID = GuidToString(lpRasConn[i].guidEntry);
  2133. /*if(cntState != RASCS_Connected)*/ {
  2134. Dbg("Close the ras handle");
  2135. DWORD dwRet = 0;
  2136. dwRet = RasHangUp(lpRasConn[i].hrasconn);
  2137. Dbg("RasHangUp returned %d", dwRet);
  2138. if(dwRet == ERROR_SUCCESS) {
  2139. RASCONNSTATUS connStatus;
  2140. connStatus.dwSize = sizeof(RASCONNSTATUS);
  2141. Dbg("TickCount allocated 3 seconds after RasHangUp...");
  2142. //Reduce timeout tick in case occuring
  2143. //UnLoading-Entity -Josephus@2017627 16:54:10
  2144. DWORD tcLast = GetTickCount() + 3 * 1100;
  2145. while((( dwRet = RasGetConnectStatus(lpRasConn[i].hrasconn, &connStatus))
  2146. != ERROR_INVALID_HANDLE) && (tcLast > GetTickCount())) {
  2147. Sleep(0);
  2148. }
  2149. bool fResult = (dwRet == ERROR_INVALID_HANDLE);
  2150. Dbg("Cycle is done %snormally", fResult ? "" : "ab");
  2151. }
  2152. }
  2153. nRes = 1;
  2154. break;
  2155. }
  2156. }else{
  2157. Dbg("%s - RasGetConnectStatus failed: %u", lpRasConn[i].szEntryName, dwRet);
  2158. }
  2159. }
  2160. } else {
  2161. Dbg("RasEnumConnections twice failed %u", dwRet);
  2162. nRes = -1;
  2163. }
  2164. //Deallocate memory for the connection buffer
  2165. HeapFree(GetProcessHeap(), 0, lpRasConn);
  2166. lpRasConn = NULL;
  2167. return nRes;
  2168. }
  2169. // There was either a problem with RAS or there are no connections to enumerate
  2170. if(dwConnections >= 1) {
  2171. Dbg("The operation failed to acquire the buffer size.");
  2172. return -1;
  2173. }
  2174. Dbg("There are no active RAS connections.");
  2175. return 0;
  2176. }
  2177. int CMobileDialFSM::CreatePhoneEntry()
  2178. {
  2179. return CreatePhoneEntryEx("", NULL, FALSE);
  2180. }
  2181. int CMobileDialFSM::CreatePhoneEntryEx(LPCTSTR lpcszDevName, const AccessKeyInfo * pNewInfo, bool fApplyNow)
  2182. {
  2183. DWORD dwCb = 0;
  2184. DWORD dwRet = ERROR_SUCCESS;
  2185. DWORD dwDevices = 0;
  2186. BOOL bOrigin = FALSE;
  2187. LPRASDEVINFO lpRasDevInfo = NULL;
  2188. RASDEVINFO specialRasDevInfo = {0};
  2189. if(strlen(lpcszDevName) == 0 && pNewInfo == NULL) {
  2190. bOrigin = TRUE;
  2191. }
  2192. specialRasDevInfo.dwSize = 0;
  2193. strcpy_s(specialRasDevInfo.szDeviceName, lpcszDevName);
  2194. strcpy_s(specialRasDevInfo.szDeviceType, RASDT_Modem);
  2195. LPRASENTRY lpRasExistedEntry = NULL;
  2196. LPBYTE lpRasExistedDevInfo = NULL;
  2197. // Call RasEnumDevices with lpRasDevInfo = NULL. dwCb is returned with the required buffer size and
  2198. // a return code of ERROR_BUFFER_TOO_SMALL
  2199. dwRet = RasEnumDevices(lpRasDevInfo, &dwCb, &dwDevices);
  2200. if (dwRet == ERROR_BUFFER_TOO_SMALL)
  2201. {
  2202. // Allocate the memory needed for the array of RAS structure(s).
  2203. lpRasDevInfo = (LPRASDEVINFO) HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, dwCb);
  2204. if (lpRasDevInfo == NULL)
  2205. {
  2206. Dbg("HeapAlloc for RasEnumDevices failed");
  2207. if(!bOrigin) m_csErrMsg = "程序异常-申请内存失败!";
  2208. return -1;
  2209. }
  2210. else
  2211. {
  2212. // The first RASDEVINFO structure in the array must contain the structure size
  2213. lpRasDevInfo[0].dwSize = sizeof(RASDEVINFO);
  2214. // Call RasEnumDevices to enumerate RAS devices
  2215. dwRet = RasEnumDevices(lpRasDevInfo, &dwCb, &dwDevices);
  2216. int bFound = FALSE;
  2217. // If successful, print the names of the RAS devices
  2218. if (ERROR_SUCCESS == dwRet)
  2219. {
  2220. Dbg("The following RAS devices were found:");
  2221. for (DWORD i = 0; i < dwDevices; i++)
  2222. {
  2223. Dbg("%s <%s>",lpRasDevInfo[i].szDeviceName,lpRasDevInfo[i].szDeviceType);
  2224. if(specialRasDevInfo.dwSize == 0) {
  2225. if(!bOrigin) {
  2226. if(!_strnicmp(lpRasDevInfo[i].szDeviceName, lpcszDevName, strlen(lpcszDevName))) {
  2227. specialRasDevInfo.dwSize = strlen(lpcszDevName) + strlen(lpRasDevInfo[i].szDeviceType);
  2228. strcpy_s(specialRasDevInfo.szDeviceName, lpRasDevInfo[i].szDeviceName);
  2229. strcpy_s(specialRasDevInfo.szDeviceType, lpRasDevInfo[i].szDeviceType);
  2230. Dbg("found it 1: %s::%s", specialRasDevInfo.szDeviceName,
  2231. specialRasDevInfo.szDeviceType);
  2232. bFound = TRUE;
  2233. break;
  2234. }
  2235. } else {
  2236. for(int j=0; j<sizeModelList; ++j)
  2237. {
  2238. if(!_strnicmp(lpRasDevInfo[i].szDeviceType, modelList[j], strlen(modelList[j])))
  2239. {
  2240. specialRasDevInfo.dwSize = strlen(modelList[j]);
  2241. strcpy_s(specialRasDevInfo.szDeviceName, lpRasDevInfo[i].szDeviceName);
  2242. strcpy_s(specialRasDevInfo.szDeviceType, lpRasDevInfo[i].szDeviceType);
  2243. Dbg("found it 2: %s::%s", specialRasDevInfo.szDeviceName,
  2244. specialRasDevInfo.szDeviceType);
  2245. bFound = TRUE;
  2246. break;
  2247. }
  2248. }
  2249. }
  2250. }
  2251. }
  2252. }
  2253. //Deallocate memory for the connection buffer
  2254. HeapFree(GetProcessHeap(), 0, lpRasDevInfo);
  2255. lpRasDevInfo = NULL;
  2256. if(!bFound) {
  2257. Dbg("Cannot found the modem :%s", lpcszDevName);
  2258. if(!bOrigin) m_csErrMsg = "所对应的调制解调器不存在!";
  2259. return -1;
  2260. }
  2261. }
  2262. }
  2263. LPRASENTRY lpDefaultRasEntry = NULL;
  2264. LPBYTE lpDefaultDeviceInfo = NULL;
  2265. DWORD dwDefaultEntrySize = 0;
  2266. DWORD dwDefaultInforSize = 0;
  2267. {
  2268. if((dwRet = RasGetEntryProperties(NULL, "", NULL,
  2269. &dwDefaultEntrySize, NULL, &dwDefaultInforSize)) != 0)
  2270. {
  2271. if(dwRet == ERROR_BUFFER_TOO_SMALL)
  2272. {
  2273. lpDefaultRasEntry = (LPRASENTRY) HeapAlloc(
  2274. GetProcessHeap(), HEAP_ZERO_MEMORY, dwDefaultEntrySize);
  2275. if(dwDefaultInforSize != 0) {
  2276. lpDefaultDeviceInfo = (LPBYTE) HeapAlloc(
  2277. GetProcessHeap(), HEAP_ZERO_MEMORY, dwDefaultInforSize);
  2278. Dbg("dwDefaultInforSize: %u", dwDefaultInforSize);
  2279. }
  2280. lpDefaultRasEntry->dwSize = sizeof(RASENTRY);
  2281. if((dwRet = RasGetEntryProperties(NULL, "", lpDefaultRasEntry, &dwDefaultEntrySize,
  2282. lpDefaultDeviceInfo, &dwDefaultInforSize)) != 0) {
  2283. Dbg("RasGetEntryProperties twice failed");
  2284. if(lpDefaultRasEntry) {
  2285. HeapFree(GetProcessHeap(), 0, lpDefaultRasEntry);
  2286. lpDefaultRasEntry = NULL;
  2287. }
  2288. if(lpDefaultDeviceInfo) {
  2289. HeapFree(GetProcessHeap(), 0, lpDefaultDeviceInfo);
  2290. lpDefaultDeviceInfo = NULL;
  2291. }
  2292. if(!bOrigin) m_csErrMsg = "RasGetEntryProperties error";
  2293. return -1;
  2294. }
  2295. }
  2296. else {
  2297. Dbg("RasGetEntryProperties failed : %d", dwRet);
  2298. if(!bOrigin) m_csErrMsg = "RasGetEntryProperties error";
  2299. return -1;
  2300. }
  2301. } else {
  2302. Dbg("RasGetEntryProperties improper : %d", dwRet);
  2303. if(!bOrigin) m_csErrMsg = "RasGetEntryProperties improper";
  2304. return -1;
  2305. }
  2306. }
  2307. RASENTRY rasEntry;
  2308. ZeroMemory(&rasEntry, sizeof(RASENTRY));
  2309. /*
  2310. rasEntry.dwSize = sizeof(RASENTRY);
  2311. rasEntry.dwCountryCode = 86;
  2312. rasEntry.dwCountryID = 86;
  2313. rasEntry.dwDialExtraPercent = 75;
  2314. rasEntry.dwDialExtraSampleSeconds = 120;
  2315. rasEntry.dwDialMode = RASEDM_DialAll;
  2316. rasEntry.dwEncryptionType = 3;
  2317. rasEntry.dwfNetProtocols = RASNP_Ip;
  2318. rasEntry.dwfOptions = RASEO_RequirePAP | RASEO_RequireMsCHAP2;
  2319. if((RASEO_RequireMsCHAP2 | RASEO_RequireMsCHAP | RASEO_RequireCHAP
  2320. | RASEO_PreviewDomain | RASEO_PreviewUserPw
  2321. | RASEO2_DisableClassBasedStaticRoute | RASEO2_IPv6ExplicitMetric | RASEO2_IPv4ExplicitMetric
  2322. | RASEO2_SharePhoneNumbers | RASEO2_ReconnectIfDropped
  2323. | RASEO2_UsePreSharedKey) == 1024262928)
  2324. {
  2325. Dbg("the same options mask.");
  2326. }
  2327. rasEntry.dwfOptions2 = 367;
  2328. rasEntry.dwFramingProtocol = RASFP_Ppp;
  2329. rasEntry.dwHangUpExtraPercent = 10;
  2330. rasEntry.dwHangUpExtraSampleSeconds = 120;
  2331. rasEntry.dwRedialCount = 3;
  2332. rasEntry.dwRedialPause = 60;
  2333. rasEntry.dwType = RASET_Phone; //指定电话薄条目类型,这里指明是电话线
  2334. rasEntry.dwVpnStrategy = VS_Default;
  2335. */
  2336. assert(lpDefaultRasEntry != NULL);
  2337. memcpy_s(&rasEntry, sizeof(RASENTRY), lpDefaultRasEntry, sizeof(RASENTRY));
  2338. rasEntry.dwSize = sizeof(RASENTRY);
  2339. DWORD dwTempMask = ~(RASEO_RequireCHAP | RASEO_RequireMsCHAP2 | RASEO_RequirePAP);
  2340. Dbg("test: 0x%08X, 0x%08X", rasEntry.dwfOptions, dwTempMask);
  2341. if(bOrigin) {
  2342. rasEntry.dwfOptions &= (~RASEO_RequireCHAP);
  2343. } else {
  2344. rasEntry.dwfOptions &= dwTempMask;
  2345. if((pNewInfo->usAPType & AUPR_PAP) == AUPR_PAP) {
  2346. rasEntry.dwfOptions |= RASEO_RequirePAP;
  2347. }
  2348. if((pNewInfo->usAPType & AUPR_CHAP) == AUPR_CHAP) {
  2349. rasEntry.dwfOptions |= RASEO_RequireCHAP;
  2350. }
  2351. if((pNewInfo->usAPType & AUPR_MSV2) == AUPR_MSV2) {
  2352. rasEntry.dwfOptions |= RASEO_RequireMsCHAP2;
  2353. }
  2354. }
  2355. Dbg("Use default entry !!! 0X%08X", rasEntry.dwfOptions);
  2356. strcpy_s(rasEntry.szDeviceName, specialRasDevInfo.szDeviceName);
  2357. strcpy_s(rasEntry.szDeviceType, specialRasDevInfo.szDeviceType);
  2358. if(bOrigin) {
  2359. strcpy_s(rasEntry.szLocalPhoneNumber, (LPCTSTR)m_csPhoneNum);
  2360. } else {
  2361. strcpy_s(rasEntry.szLocalPhoneNumber, (LPCTSTR)pNewInfo->strPhoneNum);
  2362. }
  2363. if(lpDefaultRasEntry) {
  2364. HeapFree(GetProcessHeap(), 0, lpDefaultRasEntry);
  2365. lpDefaultRasEntry = NULL;
  2366. }
  2367. if(lpDefaultDeviceInfo) {
  2368. HeapFree(GetProcessHeap(), 0, lpDefaultDeviceInfo);
  2369. lpDefaultDeviceInfo = NULL;
  2370. }
  2371. DWORD dwValid = 0;
  2372. if(bOrigin) {
  2373. dwValid = RasValidateEntryName(NULL, m_csEntryName);
  2374. if(dwValid != ERROR_SUCCESS && dwValid != ERROR_ALREADY_EXISTS){
  2375. if(dwValid == ERROR_INVALID_NAME){
  2376. Dbg("EntryName(%s) is invalid format", (LPCTSTR)m_csEntryName);
  2377. }else{
  2378. Dbg("validate entry name (%s) failed: %d", (LPCTSTR)m_csEntryName, dwValid);
  2379. }
  2380. return -1;
  2381. }
  2382. } else {
  2383. dwValid = RasValidateEntryName(NULL, pNewInfo->strProfileDesc);
  2384. if(dwValid != ERROR_SUCCESS && dwValid != ERROR_ALREADY_EXISTS){
  2385. if(dwValid == ERROR_INVALID_NAME){
  2386. Dbg("EntryName(%s) is invalid format", (LPCTSTR)pNewInfo->strProfileDesc);
  2387. }else{
  2388. Dbg("validate entry name (%s) failed: %d", (LPCTSTR)pNewInfo->strProfileDesc, dwValid);
  2389. }
  2390. m_csErrMsg = CSimpleStringA::Format("连接名称[%s]校验无效:0x%X", (LPCTSTR)pNewInfo->strProfileDesc, dwValid);
  2391. return -1;
  2392. }
  2393. }
  2394. if(dwValid == ERROR_ALREADY_EXISTS)
  2395. {
  2396. if(!bOrigin) {
  2397. m_csErrMsg = CSimpleStringA::Format("该连接名称[%s]已存在!", (LPCTSTR)pNewInfo->strProfileDesc);
  2398. return -1;
  2399. }
  2400. Dbg("EntryName(%s) is already existed", (LPCTSTR)m_csEntryName);
  2401. }
  2402. DWORD dwSetEntry = ERROR_SUCCESS;
  2403. LPCTSTR lpcszTmpVpn = NULL;
  2404. if(bOrigin) {
  2405. lpcszTmpVpn = (LPCTSTR)m_csVpnName;
  2406. } else {
  2407. lpcszTmpVpn = (LPCTSTR)pNewInfo->strAccessString;
  2408. }
  2409. //TODO APN
  2410. if(lpcszTmpVpn != NULL && strlen(lpcszTmpVpn) > 0)
  2411. {
  2412. BYTE lpb[436];
  2413. size_t szLen = strlen(lpcszTmpVpn);
  2414. CSimpleStringW lpwczTmpVpn = CSimpleStringA2W(CSimpleStringA(lpcszTmpVpn));
  2415. int szWlen = wcslen((LPCWSTR)lpwczTmpVpn);
  2416. DWORD dwSizeOflpb = sizeof(lpb);
  2417. memset(lpb, 0, dwSizeOflpb);
  2418. lpb[0]=180, lpb[1]=1, lpb[4]=180,lpb[5]=1;
  2419. lpb[8]=180, lpb[9]=1, lpb[12]=1, lpb[16]=15;
  2420. lpb[20]=1, lpb[24]=2, lpb[76]=1, lpb[80]=2, lpb[84]=2;
  2421. for(int i=0,stip=0; i<szWlen; ++i, stip+=2) {
  2422. *(lpb + 88 + stip) = (BYTE)(lpwczTmpVpn[i]);
  2423. }
  2424. lpb[344] = 1;
  2425. lpb[248] = 1;
  2426. dwSetEntry = RasSetEntryProperties(NULL, bOrigin ? m_csEntryName : pNewInfo->strProfileDesc, &rasEntry, sizeof(rasEntry)
  2427. , lpb, dwSizeOflpb);
  2428. if (dwSetEntry == ERROR_SUCCESS){
  2429. Dbg("set entry(%s) with lpb successfully.", (LPCTSTR)(bOrigin ? m_csEntryName : pNewInfo->strProfileDesc));
  2430. } else {
  2431. Dbg("set entry(%s) with lpb(%s) failed: %d", (LPCTSTR)(bOrigin ? m_csEntryName : pNewInfo->strProfileDesc), lpcszTmpVpn, dwSetEntry);
  2432. }
  2433. }
  2434. else
  2435. {
  2436. //lpbDeviceInfo 参数是一个指向缓冲区的指针,该缓冲区中包含TAPI设备的配置信息。
  2437. //在Windows 2000和Windows NT平台上,没有使用这个参数,因此,应该设为NULL。
  2438. dwSetEntry = RasSetEntryProperties(NULL, bOrigin ? m_csEntryName : pNewInfo->strProfileDesc, &rasEntry, sizeof(rasEntry), NULL, 0);
  2439. if(dwSetEntry == ERROR_SUCCESS) {
  2440. Dbg("set entry(%s) successfully.", (LPCTSTR)(bOrigin ? m_csEntryName : pNewInfo->strProfileDesc));
  2441. } else {
  2442. Dbg("set entry(%s) failed: %d", (LPCTSTR)(bOrigin ? m_csEntryName : pNewInfo->strProfileDesc), dwSetEntry);
  2443. }
  2444. }
  2445. if (dwSetEntry == ERROR_SUCCESS){
  2446. do {
  2447. if((dwRet = RasGetEntryProperties(NULL, bOrigin ? m_csEntryName : pNewInfo->strProfileDesc, NULL,
  2448. &dwDefaultEntrySize, NULL, &dwDefaultInforSize)) != 0) {
  2449. if(dwRet == ERROR_BUFFER_TOO_SMALL) {
  2450. lpDefaultRasEntry = (LPRASENTRY) HeapAlloc(
  2451. GetProcessHeap(), HEAP_ZERO_MEMORY, dwDefaultEntrySize);
  2452. if(dwDefaultInforSize != 0) {
  2453. lpDefaultDeviceInfo = (LPBYTE) HeapAlloc(
  2454. GetProcessHeap(), HEAP_ZERO_MEMORY, dwDefaultInforSize);
  2455. Dbg("dwDefaultInforSize2: %u", dwDefaultInforSize);
  2456. }
  2457. lpDefaultRasEntry->dwSize = sizeof(RASENTRY);
  2458. if((dwRet = RasGetEntryProperties(NULL, bOrigin ? m_csEntryName : pNewInfo->strProfileDesc,
  2459. lpDefaultRasEntry, &dwDefaultEntrySize,
  2460. lpDefaultDeviceInfo, &dwDefaultInforSize)) != 0) {
  2461. Dbg("RasGetEntryProperties2 twice failed");
  2462. break;
  2463. }
  2464. CSmartPointer<IConfigInfo> spConfigRun;
  2465. GetEntityBase()->GetFunction()->OpenConfig(Config_Run, spConfigRun);
  2466. CSimpleStringA csEntryGUID = GuidToString(lpDefaultRasEntry->guidId);
  2467. spConfigRun->WriteConfigValue(csEntryGUID, "Type", "RAS");
  2468. spConfigRun->WriteConfigValue(csEntryGUID, "Status", "Create");
  2469. if(!bOrigin) {
  2470. spConfigRun->WriteConfigValue(csEntryGUID, "EntryName", pNewInfo->strProfileDesc);
  2471. spConfigRun->WriteConfigValue(csEntryGUID, "PhoneNum", pNewInfo->strPhoneNum);
  2472. spConfigRun->WriteConfigValue(csEntryGUID, "UserName", pNewInfo->strUserName);
  2473. #ifdef USING_ENCRYPT
  2474. CSimpleStringA csEValue = EncryptString(pNewInfo->strPassword);
  2475. spConfigRun->WriteConfigValue(csEntryGUID, "Password", csEValue);
  2476. #else
  2477. spConfigRun->WriteConfigValue(csEntryGUID, "Password", pNewInfo->strPassword);
  2478. #endif
  2479. spConfigRun->WriteConfigValue(csEntryGUID, "APN", pNewInfo->strAccessString);
  2480. } else {
  2481. spConfigRun->WriteConfigValue(csEntryGUID, "EntryName", m_csEntryName);
  2482. spConfigRun->WriteConfigValue(csEntryGUID, "PhoneNum", m_csPhoneNum);
  2483. spConfigRun->WriteConfigValue(csEntryGUID, "UserName", m_csUserName);
  2484. #ifdef USING_ENCRYPT
  2485. CSimpleStringA csEValue = EncryptString(m_csPassword);
  2486. spConfigRun->WriteConfigValue(csEntryGUID, "Password", csEValue);
  2487. #else
  2488. spConfigRun->WriteConfigValue(csEntryGUID, "Password", m_csPassword);
  2489. #endif
  2490. spConfigRun->WriteConfigValue(csEntryGUID, "APN", m_csVpnName);
  2491. }
  2492. }
  2493. else {
  2494. Dbg("RasGetEntryProperties2 failed : %d", dwRet);
  2495. break;
  2496. }
  2497. } else {
  2498. Dbg("RasGetEntryProperties2 improper : %d", dwRet);
  2499. break;
  2500. }
  2501. } while (FALSE);
  2502. if(lpDefaultRasEntry) {
  2503. HeapFree(GetProcessHeap(), 0, lpDefaultRasEntry);
  2504. lpDefaultRasEntry = NULL;
  2505. }
  2506. if(lpDefaultDeviceInfo) {
  2507. HeapFree(GetProcessHeap(), 0, lpDefaultDeviceInfo);
  2508. lpDefaultDeviceInfo = NULL;
  2509. }
  2510. return 0;
  2511. }
  2512. return -1;
  2513. }
  2514. ErrorCodeEnum CMobileDialFSM::UpdateEntryInfo(LPCTSTR lpcszEntryGUID, const AccessKeyInfo& newInfo, bool fApplyNow)
  2515. {
  2516. ErrorCodeEnum rc = Error_Unexpect;
  2517. CSmartPointer<IConfigInfo> spConfigRun;
  2518. rc = GetEntityBase()->GetFunction()->OpenConfig(Config_Run, spConfigRun);
  2519. CSimpleStringA csValue;
  2520. CSimpleStringA csEntryName(true);
  2521. bool fRuncfgMark = true;
  2522. bool fReCreate = false;
  2523. if(Error_Succeed != spConfigRun->ReadConfigValue(lpcszEntryGUID, "Status", csValue)
  2524. || csValue.GetLength() <= 0 || csValue.IsStartWith("Invalid")) {
  2525. int cnt = m_rasEntries.GetCount();
  2526. for(int i=0; i<cnt; ++i) {
  2527. if(!strcmp(m_rasEntries[i].szGUID, lpcszEntryGUID)) {
  2528. csEntryName = m_rasEntries[i].szEntryName;
  2529. fRuncfgMark = false;
  2530. break;
  2531. }
  2532. }
  2533. } else {
  2534. spConfigRun->ReadConfigValue(lpcszEntryGUID, "EntryName", csEntryName);
  2535. }
  2536. Dbg("UpdateEntryInfo(%s),mark(%d)", lpcszEntryGUID, fRuncfgMark);
  2537. if(csEntryName.GetLength() <= 0) {
  2538. m_csErrMsg = "获取电话簿名称失败";
  2539. return Error_NotExist;
  2540. }
  2541. DWORD dwValide = RasValidateEntryName(NULL, csEntryName);
  2542. if(dwValide == ERROR_ALREADY_EXISTS) {
  2543. Dbg("The entry is comfirmed exist, so it seems normal ??");
  2544. Dbg("DealConnctionActivity returned: %d", DealConnctionActivity(csEntryName));
  2545. dwValide = RasDeleteEntry(NULL, csEntryName);
  2546. if(dwValide == ERROR_SUCCESS) {
  2547. Dbg("Delete the previous one succ.");
  2548. fReCreate = true;
  2549. } else {
  2550. Dbg("Delete entry(%s) failed: %d", (LPCTSTR)csEntryName, dwValide);
  2551. m_csErrMsg = CSimpleStringA::Format("更新前清理电话簿失败 : %d", dwValide);
  2552. return Error_Unexpect;
  2553. ERROR_INVALID_NAME;
  2554. }
  2555. } else {
  2556. Dbg("Valid entry name return: %d", dwValide);
  2557. m_csErrMsg = "校验电话簿名称失败";
  2558. return Error_Unexpect;
  2559. }
  2560. Dbg("Update EntryName: %s", (LPCTSTR)csEntryName);
  2561. LPRASENTRY lpRasentry = NULL;
  2562. LPBYTE lpDevInfo = NULL;
  2563. DWORD dwEntrySz = 0, dwDevInfoSz = 0;
  2564. DWORD dwRet = RasGetEntryProperties(NULL, /*(LPCTSTR)csEntryName*/"", NULL, &dwEntrySz, NULL, &dwDevInfoSz);
  2565. if(dwRet == ERROR_BUFFER_TOO_SMALL) {
  2566. if(dwEntrySz != 0) {
  2567. lpRasentry = (LPRASENTRY)HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, dwEntrySz);
  2568. if(dwDevInfoSz != 0)
  2569. lpDevInfo = (LPBYTE)HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, dwDevInfoSz);
  2570. }
  2571. if(lpRasentry == NULL) {
  2572. m_csErrMsg = "分配内存失败";
  2573. return Error_Resource;
  2574. }
  2575. lpRasentry->dwSize = sizeof(RASENTRY);
  2576. dwRet = RasGetEntryProperties(NULL, /*(LPCTSTR)csEntryName*/"", lpRasentry, &dwEntrySz, lpDevInfo, &dwDevInfoSz);
  2577. if(dwRet != ERROR_SUCCESS) {
  2578. if(lpRasentry) {
  2579. HeapFree(GetProcessHeap(), 0, lpRasentry);
  2580. lpRasentry = NULL;
  2581. }
  2582. if(lpDevInfo) {
  2583. HeapFree(GetProcessHeap(), 0, lpDevInfo);
  2584. lpDevInfo = NULL;
  2585. }
  2586. m_csErrMsg = CSimpleStringA::Format("RasGetEntryProperties failed: %d", dwRet);
  2587. return Error_Unexpect;
  2588. }
  2589. RASENTRY rasEntry;
  2590. ZeroMemory(&rasEntry, sizeof(RASENTRY));
  2591. memcpy_s(&rasEntry, sizeof(RASENTRY), lpRasentry, sizeof(RASENTRY));
  2592. rasEntry.dwSize = sizeof(RASENTRY);
  2593. DWORD dwTempMask = ~(RASEO_RequireCHAP | RASEO_RequireMsCHAP2 | RASEO_RequirePAP);
  2594. rasEntry.dwfOptions &= dwTempMask;
  2595. if((newInfo.usAPType & AUPR_PAP) == AUPR_PAP) {
  2596. rasEntry.dwfOptions |= RASEO_RequirePAP;
  2597. Dbg("Add: AUPR_PAP");
  2598. }
  2599. if((newInfo.usAPType & AUPR_CHAP) == AUPR_CHAP) {
  2600. rasEntry.dwfOptions |= RASEO_RequireCHAP;
  2601. Dbg("Add: AUPR_CHAP");
  2602. }
  2603. if((newInfo.usAPType & AUPR_MSV2) == AUPR_MSV2) {
  2604. rasEntry.dwfOptions |= RASEO_RequireMsCHAP2;
  2605. Dbg("Add: AUPR_MSV2");
  2606. }
  2607. strcpy_s(rasEntry.szLocalPhoneNumber, (LPCTSTR)newInfo.strPhoneNum);
  2608. if(lpRasentry) {
  2609. HeapFree(GetProcessHeap(), 0, lpRasentry);
  2610. lpRasentry = NULL;
  2611. }
  2612. if(lpDevInfo) {
  2613. HeapFree(GetProcessHeap(), 0, lpDevInfo);
  2614. lpDevInfo = NULL;
  2615. }
  2616. //TODO: AccessString
  2617. BYTE lpb[436];
  2618. DWORD dwSizeOflpb = 0;
  2619. bool fHaveApn = false;
  2620. if(!newInfo.strAccessString.IsNullOrEmpty()) {
  2621. fHaveApn = true;
  2622. size_t szLen = strlen(newInfo.strAccessString);
  2623. CSimpleStringW lpwczTmpVpn = CSimpleStringA2W(newInfo.strAccessString);
  2624. int szWlen = wcslen((LPCWSTR)lpwczTmpVpn);
  2625. dwSizeOflpb = sizeof(lpb);
  2626. memset(lpb, 0, dwSizeOflpb);
  2627. lpb[0]=180, lpb[1]=1, lpb[4]=180,lpb[5]=1;
  2628. lpb[8]=180, lpb[9]=1, lpb[12]=1, lpb[16]=15;
  2629. lpb[20]=1, lpb[24]=2, lpb[76]=1, lpb[80]=2, lpb[84]=2;
  2630. for(int i=0,stip=0; i<szWlen; ++i, stip+=2) {
  2631. *(lpb + 88 + stip) = (BYTE)(lpwczTmpVpn[i]);
  2632. }
  2633. lpb[344] = 1;
  2634. lpb[248] = 1;
  2635. Dbg("Have accss string: %s", (LPCTSTR)newInfo.strAccessString);
  2636. }
  2637. dwRet = RasSetEntryProperties(NULL, (LPCTSTR)csEntryName, &rasEntry, sizeof(rasEntry),
  2638. fHaveApn ? lpb : NULL, dwSizeOflpb);
  2639. if(dwRet == ERROR_SUCCESS) {
  2640. Dbg("update entry(%s) successfully.", (LPCTSTR)csEntryName);
  2641. } else {
  2642. Dbg("update entry(%s) failed: %d", (LPCTSTR)csEntryName, dwRet);
  2643. if(lpRasentry) {
  2644. HeapFree(GetProcessHeap(), 0, lpRasentry);
  2645. lpRasentry = NULL;
  2646. }
  2647. if(lpDevInfo) {
  2648. HeapFree(GetProcessHeap(), 0, lpDevInfo);
  2649. lpDevInfo = NULL;
  2650. }
  2651. m_csErrMsg = CSimpleStringA::Format("RasSetEntryProperties failed: %d", dwRet);
  2652. return Error_Unexpect;
  2653. }
  2654. //RASCREDENTIALS credentials;
  2655. //memset(&credentials, 0, sizeof(RASCREDENTIALS));
  2656. //credentials.dwSize = sizeof(RASCREDENTIALS);
  2657. //dwRet = RasGetCredentials(NULL, csEntryName, &credentials);
  2658. //if(dwRet == ERROR_SUCCESS) {
  2659. // Dbg("Old UserName: %s", credentials.szUserName);
  2660. // Dbg("Old Password: %s", credentials.szPassword);
  2661. // Dbg("Old Mask: 0x%X", credentials.dwMask);
  2662. // strcpy_s(credentials.szUserName, newInfo.strUserName);
  2663. // strcpy_s(credentials.szPassword, newInfo.strPassword);
  2664. // strcpy_s(credentials.szDomain, "");
  2665. // credentials.dwMask = 0;
  2666. // credentials.dwSize = sizeof(RASCREDENTIALS);
  2667. // dwRet = RasSetCredentials(NULL, csEntryName, &credentials, FALSE);
  2668. // if(dwRet == 0) {
  2669. // Dbg("RasSetCredentials succ");
  2670. // } else {
  2671. // Dbg("RasSetCredentials failed: %d", dwRet);
  2672. // }
  2673. //} else {
  2674. // Dbg("RasGetCredentials failed: %d", dwRet);
  2675. //}
  2676. CSimpleStringA csUpdatedGUID;
  2677. bool fAborted = false;
  2678. if(fReCreate) {
  2679. do {
  2680. fAborted = true;
  2681. if((dwRet = RasGetEntryProperties(NULL, csEntryName, NULL,
  2682. &dwEntrySz, NULL, &dwDevInfoSz)) != 0) {
  2683. if(dwRet == ERROR_BUFFER_TOO_SMALL) {
  2684. lpRasentry = (LPRASENTRY) HeapAlloc(
  2685. GetProcessHeap(), HEAP_ZERO_MEMORY, dwEntrySz);
  2686. if(dwDevInfoSz != 0) {
  2687. lpDevInfo = (LPBYTE) HeapAlloc(
  2688. GetProcessHeap(), HEAP_ZERO_MEMORY, dwDevInfoSz);
  2689. Dbg("dwDevInfoSz: %u", dwDevInfoSz);
  2690. }
  2691. lpRasentry->dwSize = sizeof(RASENTRY);
  2692. if((dwRet = RasGetEntryProperties(NULL, (LPCTSTR)csEntryName,
  2693. lpRasentry, &dwEntrySz, lpDevInfo, &dwDevInfoSz)) != 0) {
  2694. Dbg("RasGetEntryProperties twice failed");
  2695. break;
  2696. }
  2697. csUpdatedGUID = GuidToString(lpRasentry->guidId);
  2698. if(!csUpdatedGUID.IsNullOrEmpty()) {
  2699. fAborted = false;
  2700. }
  2701. }
  2702. else {
  2703. Dbg("RasGetEntryProperties3 failed : %d", dwRet);
  2704. break;
  2705. }
  2706. } else {
  2707. Dbg("RasGetEntryProperties3 improper : %d", dwRet);
  2708. break;
  2709. }
  2710. } while (FALSE);
  2711. } else {
  2712. csUpdatedGUID = lpcszEntryGUID;
  2713. }
  2714. if(!fAborted) {
  2715. CSmartPointer<IConfigInfo> spConfigRun;
  2716. GetEntityBase()->GetFunction()->OpenConfig(Config_Run, spConfigRun);
  2717. spConfigRun->WriteConfigValue(csUpdatedGUID, "Status", "Update");
  2718. if(!fRuncfgMark || fReCreate) {
  2719. spConfigRun->WriteConfigValue(csUpdatedGUID, "Type", "RAS");
  2720. spConfigRun->WriteConfigValue(csUpdatedGUID, "EntryName", newInfo.strProfileDesc);
  2721. if(fReCreate) {
  2722. spConfigRun->WriteConfigValue(lpcszEntryGUID, "Status", "Invalid-Update-Delete");
  2723. spConfigRun->WriteConfigValue(lpcszEntryGUID, "EntryName", "");
  2724. spConfigRun->WriteConfigValue(lpcszEntryGUID, "PhoneNum", "");
  2725. spConfigRun->WriteConfigValue(lpcszEntryGUID, "UserName", "");
  2726. spConfigRun->WriteConfigValue(lpcszEntryGUID, "Password", "");
  2727. spConfigRun->WriteConfigValue(lpcszEntryGUID, "APN", "");
  2728. Sleep(800);
  2729. }
  2730. }
  2731. spConfigRun->WriteConfigValue(csUpdatedGUID, "PhoneNum", newInfo.strPhoneNum);
  2732. spConfigRun->WriteConfigValue(csUpdatedGUID, "UserName", newInfo.strUserName);
  2733. #ifdef USING_ENCRYPT
  2734. CSimpleStringA csEValue = EncryptString(newInfo.strPassword);
  2735. spConfigRun->WriteConfigValue(csUpdatedGUID, "Password", csEValue);
  2736. #else
  2737. spConfigRun->WriteConfigValue(csUpdatedGUID, "Password", newInfo.strPassword);
  2738. #endif
  2739. spConfigRun->WriteConfigValue(csUpdatedGUID, "APN", newInfo.strAccessString);
  2740. }
  2741. if(lpRasentry) {
  2742. HeapFree(GetProcessHeap(), 0, lpRasentry);
  2743. lpRasentry = NULL;
  2744. }
  2745. if(lpDevInfo) {
  2746. HeapFree(GetProcessHeap(), 0, lpDevInfo);
  2747. lpDevInfo = NULL;
  2748. }
  2749. m_csErrMsg = "";
  2750. return fAborted ? Error_Unexpect : Error_Succeed;
  2751. }else {
  2752. m_csErrMsg = CSimpleStringA::Format("RasGetEntryProperties failed: %d", dwRet);
  2753. return Error_Unexpect;
  2754. }
  2755. }
  2756. BOOL CMobileDialFSM::SetGlobalConnectState(LPCTSTR lpcszEntryGUID, const RASCONNSTATE state)
  2757. {
  2758. int cnt = g_DongleInfos.GetCount();
  2759. int aim = -1;
  2760. for(int i=0; i<cnt && aim == -1; ++i) {
  2761. if(g_DongleInfos[i].usDeviceType == DEVTYPE_MODEM) {
  2762. int cnt2 = g_DongleInfos[i].profileList.GetCount();
  2763. if(cnt2 > 0) {
  2764. MBNProfileList& profiles = g_DongleInfos[i].profileList;
  2765. for(int j=0; j<cnt2; ++j) {
  2766. if(!profiles[j].strProfileName.Compare(lpcszEntryGUID)) {
  2767. aim = i;
  2768. Dbg("Set the connState of entry : %s", (LPCTSTR)profiles[j].AccessInfo.strProfileDesc);
  2769. break;
  2770. }
  2771. }
  2772. }
  2773. }
  2774. }
  2775. if(aim == -1) {
  2776. return FALSE;
  2777. }
  2778. if(state == RASCS_Connected) {
  2779. g_DongleInfos[aim].subInfo.mbnCntState = MBN_ACTIVATION_STATE_ACTIVATED;
  2780. g_DongleInfos[aim].subInfo.strProfileName = lpcszEntryGUID;
  2781. g_DongleInfos[aim].state = MBD_ACTIVATION_CONNECTED;
  2782. } else if(g_DongleInfos[aim].state == MBD_ACTIVATION_CONNECTED) {
  2783. if(g_DongleInfos[aim].subInfo.strProfileName.GetLength() > 0) {
  2784. if(!g_DongleInfos[aim].subInfo.strProfileName.Compare(lpcszEntryGUID)) {
  2785. //TODO: more various situation?
  2786. g_DongleInfos[aim].subInfo.mbnCntState = MBN_ACTIVATION_STATE_DEACTIVATED;
  2787. g_DongleInfos[aim].subInfo.strProfileName = "";
  2788. g_DongleInfos[aim].state = MBD_ACTIVATION_DISCONNECTED;
  2789. }
  2790. }
  2791. }
  2792. return TRUE;
  2793. }
  2794. int CMobileDialFSM::Dial3GConnect()
  2795. {
  2796. LOG_FUNCTION();
  2797. m_34GReserved = 3;
  2798. if(m_34GDialSuc == 3) {
  2799. m_34GDialSuc = 0;
  2800. }
  2801. if (EnumPhoneEntry() != 0) {
  2802. Dbg("no sepecified entry(%s), begin to create it...", (LPCTSTR)m_csEntryName);
  2803. if(CreatePhoneEntry() != 0) {
  2804. Dbg("Create phone entry failed.");
  2805. SetRasStatus(MBD_CREATE_ENTRY_FAILED);
  2806. return 0;
  2807. }else{
  2808. Dbg("Create phone entry suc.");
  2809. }
  2810. }
  2811. RASDIALPARAMS Rasparam;
  2812. Rasparam.dwSize=sizeof(RASDIALPARAMS);
  2813. strcpy_s(Rasparam.szPhoneNumber,m_csPhoneNum);
  2814. strcpy_s(Rasparam.szEntryName,m_csEntryName);
  2815. strcpy_s(Rasparam.szUserName,m_csUserName);
  2816. strcpy_s(Rasparam.szPassword,m_csPassword);
  2817. strcpy_s(Rasparam.szDomain, "");
  2818. if(g_hrasconn != NULL) {
  2819. Dbg("g_hrasconn has value !");
  2820. RasHangUpSuffix();
  2821. }
  2822. DWORD dwDial = RasDial(NULL, NULL, &Rasparam, 0, /*&CustomRasDialFunc*/NULL, &g_hrasconn);
  2823. if (dwDial == ERROR_SUCCESS) {
  2824. Dbg("3g condition, ras dial successfully ");
  2825. SetRasStatus(MBD_ACTIVATION_CONNECTING);
  2826. {
  2827. CSmartPointer<IConfigInfo> spConfigRun;
  2828. CSimpleStringA csValue;
  2829. CSimpleStringA csEntryGUID;
  2830. GetEntityBase()->GetFunction()->OpenConfig(Config_Run, spConfigRun);
  2831. if(Error_Succeed == spConfigRun->ReadConfigValue("RAS", "EntryGUID", csEntryGUID)
  2832. && !csEntryGUID.IsNullOrEmpty()) {
  2833. Dbg("Update original dial param.");
  2834. if(Error_Succeed == spConfigRun->ReadConfigValue(csEntryGUID, "Status", csValue)
  2835. && !csValue.Compare("Invalid-Temporary")) {
  2836. spConfigRun->WriteConfigValue(csEntryGUID, "Type", "RAS");
  2837. spConfigRun->WriteConfigValue(csEntryGUID, "EntryName", m_csEntryName);
  2838. spConfigRun->WriteConfigValue(csEntryGUID, "PhoneNum", m_csPhoneNum);
  2839. spConfigRun->WriteConfigValue(csEntryGUID, "UserName", m_csUserName);
  2840. #ifdef USING_ENCRYPT
  2841. CSimpleStringA csEValue = EncryptString(m_csPassword);
  2842. spConfigRun->WriteConfigValue(csEntryGUID, "Password", csEValue);
  2843. #else
  2844. spConfigRun->WriteConfigValue(csEntryGUID, "Password", m_csPassword);
  2845. #endif
  2846. spConfigRun->WriteConfigValue(csEntryGUID, "APN", m_csVpnName);
  2847. spConfigRun->WriteConfigValue(csEntryGUID, "Status", "Init");
  2848. spConfigRun->WriteConfigValue("RAS", "EntryGUID", "");
  2849. }
  2850. }
  2851. }
  2852. m_34GDialSuc = 3;
  2853. return 1;
  2854. }
  2855. //hardly useful.
  2856. //Condition1: replaced by disconnect from mbn.
  2857. //Condition2: repalced by error_socket or timeout even invalid connect from fsm.
  2858. SetCurState(MBD_CONNECTION_FAILURE);
  2859. _FormatRasError(dwDial);
  2860. if(g_hrasconn != NULL) {
  2861. RasHangUpSuffix();
  2862. }
  2863. SetRasStatus(MBD_CONNECTION_FAILURE);
  2864. return 0;
  2865. }
  2866. int CMobileDialFSM::Dial3GConnectEx(LPCTSTR lpcszDevName, LPCTSTR lpcszEntryGUID)
  2867. {
  2868. LOG_FUNCTION();
  2869. if(GetCurState() == MBD_ACTIVATION_CONNECTED) {
  2870. assert(FALSE && "不应该处于已连接的状态");
  2871. SetRasStatus(MBD_UNKNOWN);
  2872. return 0;
  2873. }
  2874. m_34GReserved = 3;
  2875. if(m_34GDialSuc == 3) {
  2876. m_34GDialSuc = 0;
  2877. }
  2878. CSmartPointer<IConfigInfo> spConfigRun;
  2879. ErrorCodeEnum rc = Error_NoDefine;
  2880. GetEntityBase()->GetFunction()->OpenConfig(Config_Run, spConfigRun);
  2881. CSimpleStringA csValue;
  2882. CSimpleStringA csPhoneNum;
  2883. CSimpleStringA csEntryName;
  2884. CSimpleStringA csUserName;
  2885. CSimpleStringA csPassword;
  2886. CSimpleStringA csVpn;
  2887. if(Error_Succeed == spConfigRun->ReadConfigValue(lpcszEntryGUID, "Status", csValue)
  2888. && csValue.GetLength() > 0) {
  2889. if(csValue.IsStartWith("Invalid")) {
  2890. Dbg("the runcfg record for %s is invalid : %s", lpcszEntryGUID, (LPCTSTR)csValue);
  2891. m_csErrMsg = CSimpleStringA::Format("运行时配置信息无效:%s", (LPCTSTR)csValue);
  2892. return -1;
  2893. }
  2894. spConfigRun->ReadConfigValue(lpcszEntryGUID, "EntryName", csEntryName);
  2895. spConfigRun->ReadConfigValue(lpcszEntryGUID, "PhoneNum", csPhoneNum);
  2896. spConfigRun->ReadConfigValue(lpcszEntryGUID, "UserName", csUserName);
  2897. #ifdef USING_ENCRYPT
  2898. spConfigRun->ReadConfigValue(lpcszEntryGUID, "Password", csValue);
  2899. csPassword = DecryptString(csValue);
  2900. #else
  2901. spConfigRun->ReadConfigValue(lpcszEntryGUID, "Password", csPassword);
  2902. #endif
  2903. spConfigRun->ReadConfigValue(lpcszEntryGUID, "APN", csVpn);
  2904. }else {
  2905. Dbg("runcfg is not exist ras record for %s", lpcszEntryGUID);
  2906. m_csErrMsg = "在运行时配置信息中未找到拨号参数";
  2907. SetRasStatus(MBD_RUN_ENTRY_NOTFOUND);
  2908. return -1;
  2909. }
  2910. RASDIALPARAMS Rasparam;
  2911. memset(&Rasparam, 0, sizeof(RASDIALPARAMS));
  2912. Rasparam.dwSize=sizeof(RASDIALPARAMS);
  2913. strcpy_s(Rasparam.szPhoneNumber, csPhoneNum);
  2914. strcpy_s(Rasparam.szEntryName, csEntryName);
  2915. strcpy_s(Rasparam.szUserName, csUserName);
  2916. strcpy_s(Rasparam.szPassword, csPassword);
  2917. strcpy_s(Rasparam.szDomain, "");
  2918. if(g_hrasconn != NULL) {
  2919. RasHangUpSuffix();
  2920. }
  2921. bool bAsyn = !!(m_hRasListenThread != NULL);
  2922. if(bAsyn) {
  2923. // LogEvent(Severity_Low, Error_NoDefine, "Cancel the Ras asy dial!");
  2924. // bAsyn = false;
  2925. }
  2926. DWORD dwDial = RasDial(NULL, NULL, &Rasparam, 0, bAsyn ? &CustomRasDialFunc : NULL, &g_hrasconn);
  2927. if (dwDial == ERROR_SUCCESS) {
  2928. Dbg("3g condition, ras dial successfully bAsyn:%d", bAsyn);
  2929. if(m_recentInfo.usDialType == CNT_INVALID
  2930. || (m_recentInfo.strDongle.Compare(lpcszDevName) || m_recentInfo.strProfile.Compare(lpcszEntryGUID) )) {
  2931. Dbg("Store the new dial param.");
  2932. m_34GDialSuc = 3;
  2933. m_recentInfo.usDialType = CNT_RAS;
  2934. m_recentInfo.strDongle = lpcszDevName;
  2935. m_recentInfo.strProfile = lpcszEntryGUID;
  2936. CSmartPointer<IConfigInfo> spConfigRun;
  2937. GetEntityBase()->GetFunction()->OpenConfig(Config_Run, spConfigRun);
  2938. spConfigRun->WriteConfigValue("Recent", "Type", "RAS");
  2939. spConfigRun->WriteConfigValue("Recent", "Dongle", m_recentInfo.strDongle);
  2940. spConfigRun->WriteConfigValue("Recent", "Profile", m_recentInfo.strProfile);
  2941. spConfigRun->WriteConfigValueInt("Recent", "Manual", 0);
  2942. spConfigRun->WriteConfigValueHexInt("Recent", "RequestID", (ULONG)g_hrasconn);
  2943. } else if(m_recentInfo.usDialType == CNT_RAS) {
  2944. m_recentInfo.ulRequestID = (ULONG)g_hrasconn;
  2945. CSmartPointer<IConfigInfo> spConfigRun;
  2946. GetEntityBase()->GetFunction()->OpenConfig(Config_Run, spConfigRun);
  2947. spConfigRun->WriteConfigValueHexInt("Recent", "RequestID", (ULONG)g_hrasconn);
  2948. }
  2949. SetRasStatus(MBD_ACTIVATION_CONNECTING);
  2950. m_34GDialSuc = 3;
  2951. return 1;
  2952. }
  2953. _FormatRasError(dwDial);
  2954. if(g_hrasconn != NULL) {
  2955. RasHangUpSuffix();
  2956. }
  2957. SetRasStatus(MBD_CONNECTION_FAILURE);
  2958. return 0;
  2959. }
  2960. int CMobileDialFSM::Dial4GConnect()
  2961. {
  2962. m_34GReserved = 4;
  2963. if(m_pMbn->Connect()) {
  2964. Dbg("4g condition, mbn connect successfully");
  2965. m_34GDialSuc = 4;
  2966. return 1;
  2967. }
  2968. Dbg("4g condition, mbn connect failed.");
  2969. return 0;
  2970. }
  2971. int CMobileDialFSM::Dial4GConnectEx(LPCTSTR lpcszInterface, LPCTSTR lpcszProfileName)
  2972. {
  2973. m_34GReserved = 4;
  2974. if(m_34GDialSuc == 4) {
  2975. m_34GDialSuc = 0;
  2976. }
  2977. ULONG ulResult = m_pMbn->ConnectEx2(lpcszInterface, lpcszProfileName);
  2978. if(ulResult != (ULONG)-1) {
  2979. Dbg("4g Dial4GConnectEx successfully");
  2980. if(m_recentInfo.usDialType == CNT_INVALID
  2981. || (m_recentInfo.strDongle.Compare(lpcszInterface) || m_recentInfo.strProfile.Compare(lpcszProfileName) )) {
  2982. Dbg("Store the new dial param.");
  2983. m_34GDialSuc = 4;
  2984. m_recentInfo.usDialType = CNT_MBN;
  2985. m_recentInfo.strDongle = lpcszInterface;
  2986. m_recentInfo.strProfile = lpcszProfileName;
  2987. m_recentInfo.ulRequestID = ulResult;
  2988. CSmartPointer<IConfigInfo> spConfigRun;
  2989. GetEntityBase()->GetFunction()->OpenConfig(Config_Run, spConfigRun);
  2990. spConfigRun->WriteConfigValue("Recent", "Type", "MBN");
  2991. spConfigRun->WriteConfigValue("Recent", "Dongle", m_recentInfo.strDongle);
  2992. spConfigRun->WriteConfigValue("Recent", "Profile", m_recentInfo.strProfile);
  2993. spConfigRun->WriteConfigValueInt("Recent", "Manual", 0);
  2994. spConfigRun->WriteConfigValueHexInt("Recent", "RequestID", ulResult);
  2995. } else if(m_recentInfo.usDialType == CNT_MBN) {
  2996. m_recentInfo.ulRequestID = ulResult;
  2997. CSmartPointer<IConfigInfo> spConfigRun;
  2998. GetEntityBase()->GetFunction()->OpenConfig(Config_Run, spConfigRun);
  2999. spConfigRun->WriteConfigValueHexInt("Recent", "RequestID", ulResult);
  3000. }
  3001. return 1;
  3002. }
  3003. if(m_34GDialSuc == 4) {
  3004. m_34GDialSuc = 0;
  3005. }
  3006. Dbg("Dial4GConnectEx failed.");
  3007. return 0;
  3008. }
  3009. int CMobileDialFSM::DialConnect()
  3010. {
  3011. static int s_x[MAX_BLACK_SHEEP_NUM];
  3012. static int s_y[MAX_BLACK_SHEEP_NUM];
  3013. static int s_z[MAX_BLACK_SHEEP_NUM];
  3014. static int s_pt;
  3015. LOG_FUNCTION();
  3016. s_pt = 0;
  3017. Dbg("---------- The first step arrived.");
  3018. SetCurState(MBD_ACTIVATION_CONNECTING);
  3019. auto state3G = GetRasStatus();
  3020. auto state4G = m_pMbn->GetCurState();
  3021. auto stateMBB = GetHWStatus();
  3022. Dbg("[MBN#%s][RAS#%s][MBB#%s]",
  3023. ConvertStatusToString(state4G), ConvertStatusToString(state3G), ConvertStatusToString(stateMBB));
  3024. m_34GReserved = 0;
  3025. if(m_nAdvancedGen == ADVANCE_MBB) {
  3026. if(SwithMBBConnectStatus(TRUE)) {
  3027. return ADVANCE_MBB;
  3028. }
  3029. return -1;
  3030. }
  3031. if(m_recentInfo.usDialType != CNT_INVALID) {
  3032. if((m_recentInfo.usDialType == CNT_RAS && m_n3G > 0) || (m_recentInfo.usDialType == CNT_MBN && m_n4G > 0)) {
  3033. Dbg("[%s]: Start to find the specified dial param.", m_recentInfo.usDialType == CNT_MBN ? "4G" : "3G");
  3034. const int cnt = g_DongleInfos.GetCount();
  3035. int aim = -1;
  3036. Dbg("dongles'count: %d", cnt);
  3037. for(int i=0; i<cnt; ++i) {
  3038. if(!m_recentInfo.strDongle.Compare(g_DongleInfos[i].csInterfaceId)) {
  3039. const int cnt2 = g_DongleInfos[i].profileList.GetCount();
  3040. Dbg("%d", cnt2);
  3041. for(int j=0; j<cnt2; ++j) {
  3042. if(!m_recentInfo.strProfile.Compare(g_DongleInfos[i].profileList[j].strProfileName)) {
  3043. Dbg("found connect profile: %s(%s)", (LPCTSTR)g_DongleInfos[i].csInterfaceId,
  3044. (LPCTSTR)g_DongleInfos[i].profileList[j].AccessInfo.strProfileDesc);
  3045. aim = i;
  3046. break;
  3047. }
  3048. }
  3049. if(aim != -1) {
  3050. break;
  3051. }
  3052. }
  3053. }
  3054. if(aim != -1) {
  3055. if(m_recentInfo.usDialType == CNT_RAS) {
  3056. if(Dial3GConnectEx(m_recentInfo.strDongle, m_recentInfo.strProfile) > 0) {
  3057. return 3;
  3058. } else if(g_DongleInfos[aim].state == MBD_ACTIVATION_DISCONNECTED) {
  3059. SetCurState(MBD_CONNECTION_FAILURE);
  3060. return -1;
  3061. }
  3062. } else {
  3063. if(Dial4GConnectEx(m_recentInfo.strDongle, m_recentInfo.strProfile) > 0) {
  3064. return 4;
  3065. } else if(g_DongleInfos[aim].state == MBD_ACTIVATION_DISCONNECTED) {
  3066. SetCurState(MBD_CONNECTION_FAILURE);
  3067. return -1;
  3068. }
  3069. }
  3070. Dbg("Nonono, give you one more chance");
  3071. } else {
  3072. Dbg("[%s]: not found yet", m_recentInfo.usDialType == CNT_MBN ? "4G" : "3G");
  3073. }
  3074. }
  3075. }
  3076. Dbg("---------- The second step arrived.");
  3077. int cnt = g_DongleInfos.GetCount();
  3078. int aim = -1;
  3079. bool fp3G = false, fp4G = false;
  3080. for(int i=0; i<cnt; ++i) {
  3081. int cnt2 = g_DongleInfos[i].profileList.GetCount();
  3082. if(cnt2 > 0) {
  3083. if(g_DongleInfos[i].usDeviceType == DEVTYPE_MODEM && !fp3G) { fp3G = true;}
  3084. else if(g_DongleInfos[i].usDeviceType == DEVTYPE_INTER && !fp4G) { fp4G = true; }
  3085. }
  3086. if(g_DongleInfos[i].state != MBD_ACTIVATION_DISCONNECTED) {
  3087. Dbg("Ignore it bcz Dongle[%d]'s state: %s", i, ConvertStatusToString(g_DongleInfos[i].state));
  3088. continue;
  3089. }
  3090. if(cnt2 > 0) {
  3091. for(int j=0; j<cnt2; ++j) {
  3092. if(!g_DongleInfos[i].profileList[j].AccessInfo.IsDefault) {
  3093. Dbg("Ignore it bcz Profiles[%d]'s un-default", j);
  3094. // is not default but apply and can be used to connecting ?? [11/23/2019 15:15 @Gifur]
  3095. if(cnt2 != 1) {
  3096. continue;
  3097. } else {
  3098. Dbg("only one profile, try it.");
  3099. }
  3100. }
  3101. {
  3102. int aim2 = -1;
  3103. for(int z=0; z<mBlackSheepCount; ++z) {
  3104. if(!g_DongleInfos[i].csInterfaceId.Compare(m_blackSheep[z].strDongle)
  3105. && !g_DongleInfos[i].profileList[j].strProfileName.Compare(m_blackSheep[z].strProfile)) {
  3106. aim2 = z;
  3107. break;
  3108. }
  3109. }
  3110. if(aim2 != -1) {
  3111. s_x[s_pt] = i, s_y[s_pt] = j;
  3112. s_z[s_pt] = aim2;
  3113. Dbg("blacksheep: [%d] [%d][%d][%d] ", s_pt, i, j, aim2);
  3114. s_pt++;
  3115. continue;
  3116. }
  3117. }
  3118. if(g_DongleInfos[i].profileList[j].AccessInfo.usCntType == CNT_MBN) {
  3119. if(Dial4GConnectEx(g_DongleInfos[i].csInterfaceId, g_DongleInfos[i].profileList[j].strProfileName) > 0) {
  3120. aim = 4;
  3121. Dbg("[MBN](%s)(%s) connect succ.", (LPCTSTR)g_DongleInfos[i].csInterfaceId,
  3122. (LPCTSTR)g_DongleInfos[i].profileList[j].AccessInfo.strProfileDesc);
  3123. break;
  3124. }
  3125. } else if(g_DongleInfos[i].profileList[j].AccessInfo.usCntType == CNT_RAS) {
  3126. if(Dial3GConnectEx(g_DongleInfos[i].csInterfaceId, g_DongleInfos[i].profileList[j].strProfileName) > 0) {
  3127. aim = 3;
  3128. Dbg("[RAS](%s)(%s) connect succ.", (LPCTSTR)g_DongleInfos[i].csInterfaceId,
  3129. (LPCTSTR)g_DongleInfos[i].profileList[j].AccessInfo.strProfileDesc);
  3130. break;
  3131. }
  3132. }
  3133. }
  3134. }
  3135. if(aim != -1) {
  3136. return aim;
  3137. }
  3138. }
  3139. if(s_pt > 0) {
  3140. Dbg("---------- The third step arrived.");
  3141. bool loop = true;
  3142. int times = 0;
  3143. do
  3144. {
  3145. DWORD dwMinError = (DWORD)-1;
  3146. int aim3 = -1, k = -1;
  3147. for(int i=0; i<s_pt; ++i) {
  3148. if(s_z[i] < mBlackSheepCount && dwMinError > m_blackSheep[s_z[i]].dwErrCount) {
  3149. aim3 = s_z[i];
  3150. k = i;
  3151. dwMinError = m_blackSheep[s_z[i]].dwErrCount;
  3152. }
  3153. }
  3154. if(aim3 >= 0) {
  3155. int i = s_x[k];
  3156. int j = s_y[k];
  3157. if(g_DongleInfos[i].profileList[j].AccessInfo.usCntType == CNT_MBN) {
  3158. if(Dial4GConnectEx(g_DongleInfos[i].csInterfaceId, g_DongleInfos[i].profileList[j].strProfileName) > 0) {
  3159. aim = 4;
  3160. Dbg("BlackSheep#[MBN](%s)(%s) connect succ.", (LPCTSTR)g_DongleInfos[i].csInterfaceId,
  3161. (LPCTSTR)g_DongleInfos[i].profileList[j].AccessInfo.strProfileDesc);
  3162. }
  3163. } else if(g_DongleInfos[i].profileList[j].AccessInfo.usCntType == CNT_RAS) {
  3164. if(Dial3GConnectEx(g_DongleInfos[i].csInterfaceId, g_DongleInfos[i].profileList[j].strProfileName) > 0) {
  3165. aim = 3;
  3166. Dbg("BlackSheep#[RAS](%s)(%s) connect succ.", (LPCTSTR)g_DongleInfos[i].csInterfaceId,
  3167. (LPCTSTR)g_DongleInfos[i].profileList[j].AccessInfo.strProfileDesc);
  3168. }
  3169. }
  3170. if(aim != -1) {
  3171. return aim;
  3172. } else {
  3173. times++;
  3174. Dbg("Black sheep cannot connect(%d/%d) ?", times, s_pt);
  3175. m_blackSheep[aim3].dwErrCount += 2;
  3176. if(times >= s_pt) {
  3177. break;
  3178. }
  3179. Sleep(1000);
  3180. }
  3181. } else {
  3182. loop = false;
  3183. }
  3184. } while (loop);
  3185. }
  3186. Dbg("---------- The fourth step arrived.");
  3187. if(m_n3G > 0) {
  3188. if(!m_csEntryName.IsNullOrEmpty() && Dial3GConnect()) {
  3189. return 3;
  3190. }
  3191. }
  3192. SetCurState(MBD_CONNECTION_FAILURE);
  3193. return -1;
  3194. }
  3195. int CMobileDialFSM::DisConnect(BOOL bManual)
  3196. {
  3197. LOG_FUNCTION();
  3198. if(m_pMbn->GetCurState() == MBD_ACTIVATION_CONNECTED)
  3199. {
  3200. /*if(m_n4G > 0)*/if(m_pMbn->m_ulRequestID != ULONG(-1))
  3201. {
  3202. BOOL bRet = m_pMbn->DisConnect();
  3203. // Description update... [Josephus in 14:37:06 2016/7/12]
  3204. Dbg("4g Mbn disconnect return %d. requestID(%d)", bRet, m_pMbn->m_ulRequestID);
  3205. }
  3206. /*else if(m_n3G > 0)*/
  3207. if(g_hrasconn != NULL)
  3208. {
  3209. DWORD dwRet = RasHangUp(g_hrasconn);
  3210. Dbg("RasHangUp in Disconnect function returned %d", dwRet);
  3211. if(dwRet == ERROR_SUCCESS)
  3212. {
  3213. RASCONNSTATUS connStatus;
  3214. connStatus.dwSize = sizeof(RASCONNSTATUS);
  3215. Dbg("TickCount after RasHangUp...");
  3216. DWORD tcLast = GetTickCount() + 10000;
  3217. while((RasGetConnectStatus(g_hrasconn, &connStatus)
  3218. != ERROR_INVALID_HANDLE) && (tcLast > GetTickCount()))
  3219. {
  3220. Sleep(50);
  3221. }
  3222. Dbg("TickCount is done.");
  3223. }
  3224. }
  3225. }
  3226. RasHangUpSuffix();
  3227. return 0;
  3228. }
  3229. BOOL CMobileDialFSM::Dis4GConnect(LPCTSTR lpcszInterface)
  3230. {
  3231. assert(m_pMbn->GetCurState() == MBD_ACTIVATION_CONNECTED);
  3232. BOOL bRet = m_pMbn->DisConnect();
  3233. return bRet;
  3234. }
  3235. int CMobileDialFSM::CreateCheckSocket()
  3236. {
  3237. int iResult = 0;
  3238. if(m_sock != INVALID_SOCKET)
  3239. {
  3240. closesocket(m_sock);
  3241. m_sock = INVALID_SOCKET;
  3242. }
  3243. m_sock = socket(AF_INET, SOCK_STREAM, 0);
  3244. if (m_sock == INVALID_SOCKET)
  3245. {
  3246. m_dwLsErrCode = WSAGetLastError();
  3247. Dbg("create socket failed with error: %d", m_dwLsErrCode);
  3248. SetCurState(MBD_NETWORK_INVALID_SOCKET);
  3249. return -1;
  3250. }
  3251. // Set socket into nonblocking mode to prevent its connection action from delaying too much
  3252. //[Josephus in 17:17:48 2016/3/31]
  3253. ULONG ul = 1;
  3254. iResult = ioctlsocket(m_sock, FIONBIO, (unsigned long*)&ul);
  3255. if(iResult == SOCKET_ERROR)
  3256. {
  3257. m_dwLsErrCode = WSAGetLastError();
  3258. Dbg("Set socket into nonblocking mode failed %d", m_dwLsErrCode);
  3259. SetCurState(MBD_NETWORK_INVALID_SOCKET);
  3260. closesocket(m_sock);
  3261. m_sock = INVALID_SOCKET;
  3262. return -1;
  3263. }
  3264. return 0;
  3265. }
  3266. void CMobileDialFSM::BatteryNotifierTimeOut()
  3267. {
  3268. SYSTEM_POWER_STATUS sys_power_status;
  3269. BOOL bRet = GetSystemPowerStatus(&sys_power_status);
  3270. if (bRet != 0)
  3271. {
  3272. if (sys_power_status.ACLineStatus == 255)
  3273. {
  3274. Dbg("unknown AC power status.");
  3275. m_bACPower = 2;
  3276. }
  3277. else
  3278. m_bACPower = sys_power_status.ACLineStatus;
  3279. if (sys_power_status.BatteryLifePercent == 255)
  3280. {
  3281. #ifndef TWINKLE_LOCAL_DEBUG
  3282. Dbg("unknown battery power");
  3283. #endif
  3284. }
  3285. else {
  3286. m_batteryRemains = sys_power_status.BatteryLifePercent;
  3287. }
  3288. //Dbg("oiltest %d",m_batteryRemains);
  3289. if (m_batteryRemains != m_batteryRemainsF || m_bACPower != m_bACPowerF)
  3290. {
  3291. m_batteryRemainsF = m_batteryRemains;
  3292. m_bACPowerF = m_bACPower;
  3293. BatteryVar evt;
  3294. evt.battery = (m_batteryRemains<<16)+m_bACPower;
  3295. SpSendBroadcast(m_pEntity->GetFunction(),SP_MSG_OF(BatteryVar),SP_MSG_SIG_OF(BatteryVar),evt);
  3296. Dbg("oiltest power left %d(%x)",sys_power_status.BatteryLifePercent,evt.battery);
  3297. }
  3298. }
  3299. GetEntityBase()->GetFunction()->ResetTimer(MOBILEDIAL_TIMER_ID, BATTERY_TIMER_INTERVAL);
  3300. return;
  3301. }
  3302. int CMobileDialFSM::CheckConnenction(bool bUseBackup, int& nRetType)
  3303. {
  3304. static int iResult = 0;
  3305. int nRes = 0;
  3306. nRetType = 0;
  3307. iResult = CreateCheckSocket();
  3308. if(iResult < 0)
  3309. {
  3310. Dbg("Create sockect for connect operation failed %d", WSAGetLastError());
  3311. nRes = 2;
  3312. }
  3313. else
  3314. {
  3315. // Connect to server.
  3316. SOCKADDR_IN addrSrv;
  3317. addrSrv.sin_family = AF_INET;
  3318. if(bUseBackup && m_bBackupAvail)
  3319. {
  3320. addrSrv.sin_addr.S_un.S_addr = inet_addr(m_csIpBackup);
  3321. addrSrv.sin_port = htons(atoi(m_csPortBackup));
  3322. nRetType = 2;
  3323. }
  3324. else
  3325. {
  3326. addrSrv.sin_addr.S_un.S_addr = inet_addr(m_csIp);
  3327. addrSrv.sin_port = htons(atoi(m_csPort));
  3328. nRetType = 1;
  3329. }
  3330. //Dbg("prepared for connecting");
  3331. timeval tm;
  3332. FD_SET set;
  3333. FD_SET exceptSet;
  3334. iResult = connect(m_sock, (SOCKADDR*)&addrSrv, sizeof(SOCKADDR));
  3335. m_dwLsErrCode = WSAGetLastError();
  3336. if(iResult == SOCKET_ERROR && m_dwLsErrCode == WSAEWOULDBLOCK)
  3337. {
  3338. //Dbg("start to select");
  3339. // Set config value [Josephus in 9:54:07 2016/4/6]
  3340. tm.tv_sec = m_TimeoutSec;
  3341. tm.tv_usec = 0;
  3342. FD_ZERO(&set);
  3343. FD_SET(m_sock, &set);
  3344. FD_ZERO(&exceptSet);
  3345. FD_SET(m_sock, &exceptSet);
  3346. iResult = select(m_sock+1, NULL, &set, &exceptSet, &tm);
  3347. if(iResult == 0)
  3348. {
  3349. Dbg("timeout(%d)...", tm.tv_sec);
  3350. //timeout
  3351. iResult = SOCKET_ERROR;
  3352. SetCurState(MBD_NETWORK_TIMEOUT);
  3353. }
  3354. else if(iResult > 0)
  3355. {
  3356. if(FD_ISSET(m_sock, &set))
  3357. {
  3358. iResult = 0;
  3359. SetCurState(MBD_VALID_CONNECTED);
  3360. }
  3361. else if(FD_ISSET(m_sock, &exceptSet))
  3362. {
  3363. DWORD optVal;
  3364. int optLen = sizeof(DWORD);
  3365. if(getsockopt(m_sock, SOL_SOCKET, SO_ERROR, (char*)&optVal, &optLen) != SOCKET_ERROR)
  3366. {
  3367. Dbg("find it in exceptSet(socket#%d) %d", m_sock, optVal);
  3368. m_dwLsErrCode = optVal;
  3369. }
  3370. else
  3371. {
  3372. Dbg("find it in exceptSet(socket#%d) but getOpt failed", m_sock);
  3373. }
  3374. iResult = SOCKET_ERROR;
  3375. SetCurState(MBD_NETWORK_NOTFOUND_SOCKET);
  3376. }
  3377. else
  3378. {
  3379. Dbg("didn't find it in writeset(socket#%d)", m_sock);
  3380. iResult = SOCKET_ERROR;
  3381. SetCurState(MBD_NETWORK_NOTFOUND_SOCKET);
  3382. }
  3383. }
  3384. else
  3385. {
  3386. // [Josephus in 14:42:30 2016/7/12]
  3387. Dbg("socket(%d), iResult(%d)...", m_sock, iResult);
  3388. SetCurState(MBD_NETWORK_ERROR_SOCKET);
  3389. m_dwLsErrCode = WSAGetLastError();
  3390. }
  3391. }
  3392. if (iResult == SOCKET_ERROR)
  3393. {
  3394. Dbg("Set disconnect flag.");
  3395. nRes = 0;
  3396. }
  3397. else
  3398. {
  3399. Dbg("Set connected flag.");
  3400. nRes = 1;
  3401. Sleep(2000);
  3402. }
  3403. // cleanup
  3404. closesocket(m_sock);
  3405. m_sock = INVALID_SOCKET;
  3406. }
  3407. return nRes;
  3408. }
  3409. void CMobileDialFSM::SendNetStatusMsg()
  3410. {
  3411. NetStatusVar evt;
  3412. if(m_bPadAndFLB && isUsingNewVerAllInOne()) {
  3413. if(m_signalVal == 0) {
  3414. Dbg("Connected condition prior to check NetCard Set Signal default 4!!");
  3415. m_signalVal = 4;
  3416. }
  3417. Dbg("FLB type pad so Send real signal value! %d ", m_signalVal);
  3418. evt.netstatus = m_signalVal;
  3419. SpSendBroadcast(m_pEntity->GetFunction(),SP_MSG_OF(NetStatusVar),SP_MSG_SIG_OF(NetStatusVar),evt);
  3420. return;
  3421. }
  3422. if(!m_bConnected)
  3423. {
  3424. Dbg("Send Signal 0 !!");
  3425. evt.netstatus = 0;
  3426. m_signalVal = 0;
  3427. SpSendBroadcast(m_pEntity->GetFunction(),SP_MSG_OF(NetStatusVar),SP_MSG_SIG_OF(NetStatusVar),evt);
  3428. }
  3429. else if(!m_bPadAndFLB)
  3430. {
  3431. Dbg("not pad or FLB so Send Signal default 4!!");
  3432. evt.netstatus = 4;
  3433. m_signalVal = 4;
  3434. SpSendBroadcast(m_pEntity->GetFunction(),SP_MSG_OF(NetStatusVar),SP_MSG_SIG_OF(NetStatusVar),evt);
  3435. }
  3436. else
  3437. {
  3438. m_signalVal = m_pMbn->m_nSignalVal;
  3439. if(m_signalVal == 0) {
  3440. Dbg("Connected condition prior to check NetCard Set Signal default 4!!");
  3441. m_signalVal = 4;
  3442. }
  3443. Dbg("Send Signal %d !!", m_signalVal);
  3444. evt.netstatus = m_signalVal;
  3445. SpSendBroadcast(m_pEntity->GetFunction(),SP_MSG_OF(NetStatusVar),SP_MSG_SIG_OF(NetStatusVar),evt);
  3446. }
  3447. }
  3448. int CMobileDialFSM::SearchForCOMPorts()
  3449. {
  3450. int nCnt = 0;
  3451. m_mapPort.clear();
  3452. GUID guid = GUID_CLASS_COMPORT;
  3453. HDEVINFO hDevInfo = SetupDiGetClassDevs(&guid, NULL, NULL, DIGCF_PRESENT | DIGCF_DEVICEINTERFACE);
  3454. if(hDevInfo == INVALID_HANDLE_VALUE) {
  3455. Dbg("SetupDiGetClassDevs failed with error %d", GetLastError());
  3456. return -1;
  3457. }
  3458. SP_DEVINFO_DATA devInfoData;
  3459. ZeroMemory(&devInfoData, sizeof(devInfoData));
  3460. devInfoData.cbSize = sizeof(SP_DEVINFO_DATA);
  3461. int nIndex = 0;
  3462. while(SetupDiEnumDeviceInfo(hDevInfo, nIndex++, &devInfoData)) {
  3463. HKEY hKey = SetupDiOpenDevRegKey(hDevInfo, &devInfoData, DICS_FLAG_GLOBAL, 0, DIREG_DEV, KEY_QUERY_VALUE);
  3464. if(hKey){
  3465. DWORD dwDataType = 0;
  3466. char szPort[256] = {0};
  3467. DWORD dwSize = sizeof(szPort);
  3468. if(RegQueryValueEx(hKey, "PortName", NULL, &dwDataType, (LPBYTE)szPort, &dwSize) == ERROR_SUCCESS) {
  3469. if(dwDataType == REG_SZ) {
  3470. char szPortName[256] = {0};
  3471. DWORD dwSizeName = sizeof(szPortName);
  3472. pair<std::string, std::string> pairPort;
  3473. pairPort.second = szPort;
  3474. if(SetupDiGetDeviceRegistryProperty(hDevInfo, &devInfoData, SPDRP_DEVICEDESC, NULL,
  3475. (PBYTE)szPortName, dwSizeName, &dwSizeName)) {
  3476. pairPort.first = szPortName;
  3477. }else {
  3478. pairPort.first = "";
  3479. }
  3480. m_mapPort.insert(pairPort);
  3481. nCnt++;
  3482. }
  3483. }
  3484. RegCloseKey(hKey);
  3485. }
  3486. }
  3487. SetupDiDestroyDeviceInfoList(hDevInfo);
  3488. return nCnt;
  3489. }
  3490. void CMobileDialFSM::_EntriesToDongle(const RasEntryWithDevice& entry, ptMbnProfileInfo info)
  3491. {
  3492. InitAccessKeyInfo(&info->AccessInfo);
  3493. info->bUserDefined = true;
  3494. info->AccessInfo.usCntType = CNT_RAS;
  3495. info->AccessInfo.strProfileDesc = entry.szEntryName;
  3496. info->AccessInfo.strPhoneNum = entry.szPhoneNumber;
  3497. info->strProfileName = entry.szGUID;
  3498. info->AccessInfo.IsDefault = TRUE;
  3499. Dbg("ADD: %s", (LPCTSTR)info->strProfileName);
  3500. if((entry.dwfOptions & RASEO_RequirePAP) == RASEO_RequirePAP) {
  3501. info->AccessInfo.usAPType |= AUPR_PAP;
  3502. }
  3503. if((entry.dwfOptions & RASEO_RequireCHAP) == RASEO_RequireCHAP) {
  3504. info->AccessInfo.usAPType |= AUPR_CHAP;
  3505. }
  3506. if((entry.dwfOptions & RASEO_RequireMsCHAP2) == RASEO_RequireMsCHAP2) {
  3507. info->AccessInfo.usAPType |= AUPR_MSV2;
  3508. }
  3509. {
  3510. CSmartPointer<IConfigInfo> spConfigRun;
  3511. GetEntityBase()->GetFunction()->OpenConfig(Config_Run, spConfigRun);
  3512. CSimpleStringA csEntryGUID = info->strProfileName;
  3513. CSimpleStringA csValue;
  3514. if(Error_Succeed == spConfigRun->ReadConfigValue(csEntryGUID, "Status", csValue)
  3515. && csValue.GetLength() > 0) {
  3516. if(csValue.IsStartWith("Invalid")) {
  3517. Dbg("the runcfg record for %s is invalid : %s", (LPCTSTR)csEntryGUID, (LPCTSTR)csValue);
  3518. } else {
  3519. spConfigRun->ReadConfigValue(csEntryGUID, "PhoneNum", info->AccessInfo.strPhoneNum);
  3520. spConfigRun->ReadConfigValue(csEntryGUID, "UserName", info->AccessInfo.strUserName);
  3521. #ifdef USING_ENCRYPT
  3522. spConfigRun->ReadConfigValue(csEntryGUID, "Password", csValue);
  3523. info->AccessInfo.strPassword = DecryptString(csValue);
  3524. #else
  3525. spConfigRun->ReadConfigValue(csEntryGUID, "Password", info->AccessInfo.strPassword);
  3526. #endif
  3527. spConfigRun->ReadConfigValue(csEntryGUID, "APN", info->AccessInfo.strAccessString);
  3528. }
  3529. }else {
  3530. Dbg("runcfg is not exist ras record for %s", (LPCTSTR)csEntryGUID);
  3531. }
  3532. }
  3533. }
  3534. //int CMobileDialFSM::WriteBackDirCfgToRunCfg()
  3535. //{
  3536. // LOG_FUNCTION();
  3537. // CSmartPointer<IConfigInfo> spConfigRun;
  3538. // GetEntityBase()->GetFunction()->OpenConfig(Config_Run, spConfigRun);
  3539. // spConfigRun->WriteConfigValue("init", "param1", m_csPhoneNum);
  3540. // spConfigRun->WriteConfigValue("init", "param2", m_csEntryName);
  3541. // spConfigRun->WriteConfigValue("init", "param3", m_csUserName);
  3542. // spConfigRun->WriteConfigValue("init", "param4", m_csPassword);
  3543. // m_bRCfgHaveDial = true;
  3544. // return 0;
  3545. //}
  3546. void CMobileDialFSM::RecordLastErrorMsg(int nLastError)
  3547. {
  3548. switch(nLastError)
  3549. {
  3550. case WSASYSNOTREADY:
  3551. Dbg("ErrorCode(%d), Text[%s]",
  3552. nLastError,
  3553. "The underlying network subsystem is not ready for network communication.");
  3554. break;
  3555. case WSAVERNOTSUPPORTED:
  3556. Dbg("ErrorCode(%d), Text[%s]",
  3557. nLastError,
  3558. "The version of Windows Sockets support requested is not provided by this particular Windows Sockets implementation");
  3559. break;
  3560. case WSAEINPROGRESS :
  3561. Dbg("ErrorCode(%d), Text[%s]",
  3562. nLastError,
  3563. "A blocking Windows Sockets 1.1 operation is in progress.");
  3564. break;
  3565. case WSAEPROCLIM:
  3566. Dbg("ErrorCode(%d), Text[%s]",
  3567. nLastError, "A limit on the number of tasks supported by the Windows Sockets implementation has been reached.");
  3568. break;
  3569. case WSAEFAULT:
  3570. Dbg("ErrorCode(%d), Text[%s]",
  3571. nLastError,
  3572. "The lpWSAData parameter is not a valid pointer.");
  3573. break;
  3574. //Above for WSAStartup
  3575. case WSANOTINITIALISED:
  3576. Dbg("ErrorCode(%d), Text[%s]",
  3577. nLastError,
  3578. "A successful WSAStartup call must occur before using this function.");
  3579. break;
  3580. case WSAENETDOWN:
  3581. Dbg("ErrorCode(%d), Text[%s]",
  3582. nLastError,
  3583. "The network subsystem has failed.");
  3584. break;
  3585. //Above for WSACleanup
  3586. default:
  3587. Dbg("ErrorCode(%d), josephtest can not explain the text current.");
  3588. break;
  3589. }
  3590. }
  3591. void CMobileDialFSM::SelfTest(EntityTestEnum eTestType,CSmartPointer<ITransactionContext> pTransactionContext)
  3592. {
  3593. pTransactionContext->SendAnswer(m_ecSelfTest);
  3594. }
  3595. MOBILEDIAL_CONNECTION_STATE CMobileDialFSM::GetCurState()
  3596. {
  3597. if(m_bPadAndFLB && !isUsingNewVerAllInOne())
  3598. {
  3599. const MOBILEDIAL_CONNECTION_STATE st3G = GetRasStatus();
  3600. const MOBILEDIAL_CONNECTION_STATE st4G = m_pMbn->GetCurState();
  3601. const MOBILEDIAL_CONNECTION_STATE stHW = GetHWStatus();
  3602. if(m_nAdvancedGen != 0) {
  3603. if(m_nAdvancedGen == ADVANCE_RAS && st3G != MBD_ACTIVATION_CONNECTED) {
  3604. return st3G;
  3605. }
  3606. if(m_nAdvancedGen == ADVANCE_MBN && st4G != MBD_ACTIVATION_CONNECTED) {
  3607. return st4G;
  3608. }
  3609. if(m_nAdvancedGen == ADVANCE_MBB && stHW != MBD_ACTIVATION_CONNECTED && stHW != MBD_WLAN_CONNECTED) {
  3610. return stHW;
  3611. }
  3612. } else {
  3613. if(m_n3G > 0 || m_n4G > 0) {
  3614. if(st3G == MBD_ACTIVATION_CONNECTED || st4G == MBD_ACTIVATION_CONNECTED) {
  3615. if(m_34GReserved != 0){
  3616. m_34GReserved = 0;
  3617. }
  3618. } else {
  3619. if(m_n3G > 0 && m_n4G > 0) {
  3620. if(m_34GReserved == ADVANCE_RAS) {
  3621. Dbg("Ras status handled.1");
  3622. return st3G;
  3623. } else if(m_34GReserved == ADVANCE_MBN) {
  3624. Dbg("Mbn status handled.1");
  3625. return st4G;
  3626. }
  3627. if(st3G == MBD_ACTIVATION_DISCONNECTED || st4G == MBD_ACTIVATION_DISCONNECTED) {
  3628. return MBD_ACTIVATION_DISCONNECTED;
  3629. }
  3630. Dbg("[MBN#%s][RAS#%s]", ConvertStatusToString(st4G), ConvertStatusToString(st3G));
  3631. if(m_pMbn->HasAnyProfile()) {
  3632. Dbg("Mbn status handled.3");
  3633. return st4G;
  3634. }
  3635. if(st4G == MBD_NO_PROFILE && st3G == MBD_NO_ENTRY) {
  3636. return MBD_NO_PROFILE;
  3637. }
  3638. return MBD_INVALID_BOTH_DONGLE;
  3639. } else if(m_n3G > 0) {
  3640. Dbg("Ras status handled.2");
  3641. return st3G;
  3642. } else {
  3643. Dbg("Mbn status handled.2");
  3644. return st4G;
  3645. }
  3646. }
  3647. } else if(m_curState != MBD_VALID_CONNECTED) {
  3648. if(MBD_MIFI_START < stHW && stHW < MBD_MIFI_END) {
  3649. Dbg("hw handle.");
  3650. return stHW;
  3651. }
  3652. Dbg("no any dongle ...");
  3653. SetCurState(MBD_NO_MOBILEDONGLE);
  3654. }
  3655. }
  3656. }
  3657. Dbg("Main status handled.");
  3658. return m_curState;
  3659. }
  3660. void CMobileDialFSM::OnStateTrans(int iSrcState, int iDstState)
  3661. {
  3662. //if(iSrcState == FSM_STATE_INIT)
  3663. if(s1 == iSrcState && (iDstState == s4 || iDstState == s5)) m_bNotify = TRUE;
  3664. }
  3665. BOOL CMobileDialFSM::FormatLastError(DWORD dwCode, CSimpleStringA& csOut)
  3666. {
  3667. LPVOID lpMsgBuf;
  3668. if(!FormatMessage(
  3669. FORMAT_MESSAGE_ALLOCATE_BUFFER |
  3670. FORMAT_MESSAGE_FROM_SYSTEM |
  3671. FORMAT_MESSAGE_MAX_WIDTH_MASK,
  3672. NULL,
  3673. dwCode,
  3674. MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
  3675. (LPTSTR) &lpMsgBuf,
  3676. 0, NULL ))
  3677. {
  3678. return FALSE;
  3679. }
  3680. csOut = (LPCTSTR)lpMsgBuf;
  3681. LocalFree(lpMsgBuf);
  3682. return TRUE;
  3683. }
  3684. CSimpleStringA CMobileDialFSM::GetFurtherDetail(BOOL slvNeed)
  3685. {
  3686. const MOBILEDIAL_CONNECTION_STATE state = GetCurState();
  3687. CSimpleStringA csTemp;
  3688. CSimpleStringA csDetail;
  3689. DWORD nSuffixCode = 0;
  3690. BOOL fReportOrNot = TRUE;
  3691. if(m_bConnected) {
  3692. fReportOrNot = FALSE;
  3693. }
  3694. LOG_TRACE("[MBN#%s][RAS#%s][HW#%s][FSM#%s]",
  3695. ConvertStatusToString(m_pMbn->GetCurState()),
  3696. ConvertStatusToString(m_3gState),
  3697. ConvertStatusToString(m_HwState),
  3698. ConvertStatusToString(state));
  3699. if(m_bPadAndFLB) {
  3700. nSuffixCode = 0x01000000;
  3701. nSuffixCode |= (m_nAdvancedGen << 20);
  3702. if(m_n3G > 0) nSuffixCode |= (m_n3G << 18);
  3703. if(m_n4G > 0) nSuffixCode |= (m_n4G << 16);
  3704. }
  3705. nSuffixCode |= state;
  3706. //Dbg("nSuffixCode : %d(0x%08x).", nSuffixCode, nSuffixCode);
  3707. if(state < MBD_NETWORK_SEPERATE) {
  3708. if(state == MBD_NETWORK_TIMEOUT) {
  3709. if(m_bPadAndFLB) {
  3710. if(!isUsingNewVerAllInOne()) {
  3711. if(m_nAdvancedGen == 2 && mMBBParam.connectType == MBB_CONNECT_WIFI) {
  3712. csTemp = "WLAN已连接,但网络请求超时";
  3713. } else {
  3714. csTemp = "上网卡已连接,但网络请求超时";
  3715. }
  3716. }
  3717. } /*else {
  3718. csTemp = "网线已连接,但网络请求超时";
  3719. }*/
  3720. if(csTemp.IsNullOrEmpty()) {
  3721. csTemp = "网络请求超时";
  3722. }
  3723. }
  3724. // error!!! should be fixed in 1.9.0 [Josephus in 8:59:23 2016/9/22]
  3725. else if(state == MBD_NETWORK_ERROR_SOCKET || state == MBD_NETWORK_NOTFOUND_SOCKET) {
  3726. // [Josephus in 13:36:22 2016/9/6]
  3727. CSimpleStringA csCode;
  3728. if(m_bPadAndFLB) {
  3729. if(FormatLastError(m_dwLsErrCode, csCode)) {
  3730. CSimpleStringA csTest;
  3731. if(!isUsingNewVerAllInOne()) {
  3732. if(m_nAdvancedGen == 2 && mMBBParam.connectType == MBB_CONNECT_WIFI) {
  3733. csTest = CSimpleStringA::Format("WLAN已连接,但网络出现错误:%s", (LPCTSTR)csCode);
  3734. } else {
  3735. csTest = CSimpleStringA::Format("上网卡已连接,但网络出现错误:%s", (LPCTSTR)csCode);
  3736. }
  3737. } else {
  3738. csTest = CSimpleStringA::Format("网络出现错误:%s", (LPCTSTR)csCode);
  3739. }
  3740. LogEvent(Severity_Middle,CombineErrorCode(state), (LPCTSTR)csTest);
  3741. }
  3742. if(m_dwLsErrCode == 0) {
  3743. if(!isUsingNewVerAllInOne()) {
  3744. if(m_nAdvancedGen == 2 && mMBBParam.connectType == MBB_CONNECT_WIFI) {
  3745. csTemp = "WLAN已连接,但网络出现错误";
  3746. } else {
  3747. csTemp = "上网卡已连接,但网络出现错误";
  3748. }
  3749. } else {
  3750. csTemp = "网络出现错误";
  3751. }
  3752. } else{
  3753. char cdString[128] = {0};
  3754. sprintf_s(cdString, 128, "GLE=%08X", m_dwLsErrCode);
  3755. csTemp = CSimpleStringA::Format("网络出现错误[%s]", cdString);
  3756. }
  3757. }
  3758. } else if(state == MBD_NETWORK_INVALID_SOCKET) {
  3759. csTemp = "创建套接字失败";
  3760. } else if(state == MBD_NO_FOUND_CABLE){
  3761. csTemp = "网线未连接,请重新连接网线或设备";
  3762. } else {
  3763. char cdString[128] = {0};
  3764. sprintf_s(cdString, 128, "%08x", nSuffixCode);
  3765. csTemp = CSimpleStringA::Format("连接上网错误[%s]", cdString);
  3766. }
  3767. } else {
  3768. if(state == MBD_READY_SIM_NO_INSERT) {
  3769. csTemp = "SIM卡未插入卡托,请检查无线宽带上网卡";
  3770. } else if(state > MBD_READY_BAD_SIM_START && state < MBD_READY_BAD_SIM_END) {
  3771. char cdString[128] = {0};
  3772. sprintf_s(cdString, 128, "%08x", nSuffixCode);
  3773. csTemp = CSimpleStringA::Format("SIM卡出现异常[%s],请尝试重连设备", cdString);
  3774. } else if(state == MBD_READY_NO_SERVICE) {
  3775. csTemp = "无线宽带上网卡无服务,请检查卡托或咨询相关运营商";
  3776. } else if(state == MBD_CONNECTION_INVALID_ACCESS) {
  3777. csTemp = "连接准入字符串设置错误,请检查配置后再重连设备";
  3778. } else if(state == MBD_CONNECTION_INVALID_NAMEORPASSWORD) {
  3779. csTemp = "拨号所使用的用户名或密码错误";
  3780. } else if(state == MBD_UNKNOWN) {
  3781. csTemp = "无线宽带上网卡状态不可用,请等候一段时间或重连设备";
  3782. } else if(state == MBD_NO_PROFILE) {
  3783. csTemp = "找不到拨号参数,请先对4G上网卡进行配置";
  3784. } else if(state == MBD_NO_ENTRY){
  3785. csTemp = "找不到拨号参数,请先对上网卡进行配置";
  3786. } else if(state == MBD_RUN_ENTRY_NOTFOUND) {//仅3G拨号时出现的错误
  3787. csTemp = "运行时找不到3G拨号所需数据,请更新数据";
  3788. } else if(state == MBD_CREATE_ENTRY_FAILED) {
  3789. csTemp = "创建拨号所需的电话簿失败";
  3790. } else if(state == MBD_NO_MOBILEDONGLE) {
  3791. csTemp = "未检测到无线宽带上网卡,请重连设备";
  3792. } else if(state == MBD_CONNECTION_FAILURE || state == MBD_RASDIAL_FAILED) {//34G轮询拨号或仅4G拨号可能出现的错误
  3793. if(!_IsMBBProfileConfiged()) {
  3794. csTemp = "尝试拨号上网失败,请检查配置后再重连设备";
  3795. } else {
  3796. csTemp = "尝试拨号上网失败,请检查配置并确认是否在可用时间段";
  3797. }
  3798. } else if(state == MBD_ACTIVATION_CONNECTING) {
  3799. csTemp = "无线宽带上网卡正在进行拨号";
  3800. fReportOrNot = FALSE;
  3801. } else if(state == MBD_ACTIVATION_DISCONNECTING) {
  3802. csTemp = "无线宽带上网卡正在断开连接";
  3803. fReportOrNot = FALSE;
  3804. } else if(state == MBD_ACTIVATION_DISCONNECTED) {
  3805. csTemp = "无线宽带上网卡未连接";
  3806. fReportOrNot = FALSE;
  3807. } else if(state == MBD_INVALID_BOTH_DONGLE) {
  3808. csTemp = "上网卡出现异常,请检查配置或重连设备";
  3809. } else if(state == MBD_WLAN_INVALID_NAMEORPASSWORD) {
  3810. csTemp = "无效的 WLAN 连接参数";
  3811. } else if(state == MBD_WLAN_OFF) {
  3812. csTemp = "WLAN 被禁用";
  3813. } else if(state == MBD_WLAN_DISCONNECTED) {
  3814. csTemp = "WLAN 未连接";
  3815. } else if(state == MBD_WLAN_CONNECTED) {
  3816. csTemp = "WLAN 已连接";
  3817. fReportOrNot = FALSE;
  3818. } else if(state == MBD_WLAN_CONNECTING) {
  3819. csTemp = "正在连接 WLAN...";
  3820. fReportOrNot = FALSE;
  3821. } else if(state == MBD_WLAN_ENTRY_NO_FOUND) {
  3822. csTemp = "找不到指定的 SSID 名称";
  3823. fReportOrNot = FALSE;
  3824. } else if(state == MBD_WLAN_CONNECT_FAILED) {
  3825. csTemp = "尝试连接 WLAN 失败";
  3826. fReportOrNot = FALSE;
  3827. } else if(state == MBD_WLAN_CONFIG_CHANGED) {
  3828. csTemp = "WLAN 配置已更新";
  3829. fReportOrNot = FALSE;
  3830. } else if(state == MBD_NO_FOUND_CABLE) {
  3831. csTemp = "网线未连接,请重新连接网线或设备";
  3832. } else if(state == MBD_WLAN_CONFIG_REBOOT) {
  3833. csTemp = "上网模式已同步更新为WLAN连接,请拔离MIFI设备并重启终端应用";
  3834. fReportOrNot = FALSE;
  3835. } else if(state == MBD_WLAN_DEVICE_PLUG_OFF) {
  3836. csTemp = "上网模式已同步更新,请拔离MIFI设备,或者你可以更改为有线连接方式";
  3837. fReportOrNot = FALSE;
  3838. } else if(state == MBD_WLAN_CONFIG_WAIT_REBOOT) {
  3839. csTemp = "上网模式同步更新完成,请重启终端应用或等候拨号模块自动重启";
  3840. fReportOrNot = FALSE;
  3841. } else if(state == MBD_MIFI_LOGIN_INVALID_NAMEORPASSWORD) {
  3842. csTemp = "登录到MIFI设备失败,无效的用户名或密码";
  3843. } else if(state == MBD_INTERNAL_ERROR) {
  3844. csTemp = CSimpleStringA::Format("拨号程序执行异常[0x%08x],请尝试重连设备", m_pMbn->GetMBNCode());
  3845. } else {
  3846. char cdString[128] = {0};
  3847. sprintf_s(cdString, 128, "%08x", nSuffixCode);
  3848. csTemp = CSimpleStringA::Format("连接上网错误[%s]", cdString);
  3849. }
  3850. }
  3851. if(slvNeed) {
  3852. csDetail = CSimpleStringA::Format("%s#0x%08x", (LPCTSTR)csTemp, nSuffixCode);
  3853. } else {
  3854. csDetail = CSimpleStringA((LPCTSTR)csTemp.GetData());
  3855. }
  3856. if(fReportOrNot && !slvNeed) {
  3857. LogError(Severity_High, Error_InvalidState, CombineErrorCode(state), csDetail.GetData());
  3858. }
  3859. return csDetail;
  3860. }
  3861. int CMobileDialFSM::CheckLANConnectStatus()
  3862. {
  3863. DWORD dwFlag;
  3864. BOOL bRet = InternetGetConnectedState(&dwFlag, 0);
  3865. Dbg("Return Status: %d, GetLastError: %d", bRet, GetLastError());
  3866. if((dwFlag & INTERNET_CONNECTION_CONFIGURED) == INTERNET_CONNECTION_CONFIGURED)
  3867. {
  3868. Dbg("Local system has a valid connection to the Internet, but it might or might not be currently connected.");
  3869. }
  3870. if((dwFlag & INTERNET_CONNECTION_LAN) == INTERNET_CONNECTION_LAN)
  3871. {
  3872. Dbg("Local system uses a local area network to connect to the Internet.");
  3873. }
  3874. if((dwFlag & INTERNET_CONNECTION_MODEM) == INTERNET_CONNECTION_MODEM)
  3875. {
  3876. Dbg("Local system uses a modem to connect to the Internet.");
  3877. }
  3878. if((dwFlag & INTERNET_CONNECTION_MODEM_BUSY) == INTERNET_CONNECTION_MODEM_BUSY)
  3879. {
  3880. Dbg("No longer used: INTERNET_CONNECTION_MODEM_BUSY");
  3881. }
  3882. if((dwFlag & INTERNET_CONNECTION_OFFLINE) == INTERNET_CONNECTION_OFFLINE)
  3883. {
  3884. Dbg("Local system is in offline mode.");
  3885. }
  3886. if((dwFlag & INTERNET_CONNECTION_PROXY) == INTERNET_CONNECTION_PROXY)
  3887. {
  3888. Dbg("Local system uses a proxy server to connect to the Internet.");
  3889. }
  3890. if((dwFlag & INTERNET_RAS_INSTALLED) == INTERNET_RAS_INSTALLED)
  3891. {
  3892. Dbg("Local system has RAS installed.");
  3893. }
  3894. if(bRet)
  3895. return 1;
  3896. return 0;
  3897. }
  3898. void CMobileDialFSM::SendMsgToDialog(LPCTSTR errMsg, BOOL bSolvedInfor)
  3899. {
  3900. static char szMessage[256];
  3901. if(errMsg != NULL) {
  3902. memset(szMessage, 0, sizeof(szMessage));
  3903. szMessage[0] = '\n';
  3904. if(!bSolvedInfor) {
  3905. strcpy_s(szMessage, errMsg);
  3906. }
  3907. TellDlgTheResult(bSolvedInfor ? Error_Succeed : Error_Bug ,
  3908. WM_FATALMESSAGE_CHANGE, (LPARAM)szMessage);
  3909. }
  3910. }
  3911. void CMobileDialFSM::ShowFatalError(LPCTSTR errMsg, BOOL bSolvedInfor)
  3912. {
  3913. static int nFatalTimes = 0;
  3914. CSimpleStringA uiState = "";
  3915. bool bDisplay = true;
  3916. auto err = this->GetEntityBase()->GetFunction()->GetSysVar("UIState",uiState);
  3917. if(err == Error_Succeed && uiState.GetLength() > 0 && uiState[0] != 'X'){
  3918. bDisplay = false;
  3919. }
  3920. if(errMsg != NULL) {
  3921. if(!bSolvedInfor) {
  3922. nFatalTimes++;
  3923. if(bDisplay) GetEntityBase()->GetFunction()->ShowFatalError(errMsg);
  3924. else Dbg("ShowFatalError:%s", errMsg);
  3925. } else if(nFatalTimes != 0) {
  3926. nFatalTimes = 0;
  3927. if(bDisplay) GetEntityBase()->GetFunction()->ShowFatalError(errMsg);
  3928. else Dbg("ShowFatalSucceed:%s", errMsg);
  3929. }
  3930. SendMsgToDialog(errMsg, bSolvedInfor);
  3931. }
  3932. }
  3933. void CMobileDialFSM::TryOpenWwanSvc()
  3934. {
  3935. ServiceStatus status = SERVICE_STATUS_UNKNOWN;
  3936. BOOLEAN bRet = CSvcManager::GetInstance()->QueryServiceStatus(SERVICE_WWAN_AutoConfig, status);
  3937. Dbg("The %s service query result: %d, status: %d.", SERVICE_WWAN_AutoConfig, bRet, status);
  3938. if(bRet) {
  3939. if(status == SERVICE_STATUS_STOPPED) {
  3940. bRet = CSvcManager::GetInstance()->StartServiceWith(SERVICE_WWAN_AutoConfig);
  3941. Dbg("CustomStartService %s returned %d.", SERVICE_WWAN_AutoConfig, bRet);
  3942. if(!bRet)
  3943. {
  3944. LastErrorInfo infor;
  3945. CSvcManager::GetInstance()->GetLastErrMsg(infor);
  3946. Dbg("GetLastErr: %s.", infor.szErrMsg);
  3947. }
  3948. }
  3949. else if(status == SERVICE_STATUS_PASUSED) {
  3950. bRet = CSvcManager::GetInstance()->StopService(SERVICE_WWAN_AutoConfig);
  3951. Dbg("StopService %s returned %d.", SERVICE_WWAN_AutoConfig, bRet);
  3952. if(!bRet) {
  3953. LastErrorInfo infor;
  3954. CSvcManager::GetInstance()->GetLastErrMsg(infor);
  3955. Dbg("GetLastErr: %s.", infor.szErrMsg);
  3956. }
  3957. }
  3958. }
  3959. }
  3960. int CMobileDialFSM::RefreshRasEntry()
  3961. {
  3962. LOG_FUNCTION();
  3963. DWORD dwCb = 0;
  3964. DWORD dwRet = ERROR_SUCCESS;
  3965. DWORD dwEntries = 0;
  3966. LPRASENTRYNAME lpRasEntryName = NULL;
  3967. m_rasEntries.Clear();
  3968. dwRet = RasEnumEntries(NULL, NULL, lpRasEntryName, &dwCb, &dwEntries);
  3969. if (dwRet == ERROR_BUFFER_TOO_SMALL && dwEntries > 0) {
  3970. lpRasEntryName = (LPRASENTRYNAME) HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, dwCb);
  3971. if (lpRasEntryName == NULL){
  3972. Dbg("HeapAlloc failed when RasEnumEntries");
  3973. SetRasStatus(MBD_INTERNAL_ERROR);
  3974. return -1;
  3975. }
  3976. lpRasEntryName[0].dwSize = sizeof(RASENTRYNAME);
  3977. dwRet = RasEnumEntries(NULL, NULL, lpRasEntryName, &dwCb, &dwEntries);
  3978. if (ERROR_SUCCESS == dwRet) {
  3979. Dbg("The following RAS entry names were found:");
  3980. m_rasEntries.Init(dwEntries);
  3981. for (DWORD i = 0; i < dwEntries; i++) {
  3982. strcpy_s(m_rasEntries[i].szEntryName, lpRasEntryName[i].szEntryName);
  3983. m_rasEntries[i].szDeviceType[0] = '\0';
  3984. m_rasEntries[i].szDeviceName[0] = '\0';
  3985. m_rasEntries[i].szPhoneNumber[0] = '\0';
  3986. m_rasEntries[i].dwfOptions = 0;
  3987. Dbg("Refresh EntryName: %s", m_rasEntries[i].szEntryName);
  3988. LPRASENTRY lpRasentry = NULL;
  3989. LPBYTE lpDevInfo = NULL;
  3990. DWORD dwEntrySz = 0, dwDevInfoSz = 0;
  3991. dwRet = RasGetEntryProperties(NULL, lpRasEntryName[i].szEntryName, NULL, &dwEntrySz, NULL, &dwDevInfoSz);
  3992. if(dwRet == ERROR_BUFFER_TOO_SMALL) {
  3993. if(dwEntrySz != 0)
  3994. lpRasentry = (LPRASENTRY)HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, dwEntrySz);
  3995. if(dwDevInfoSz != 0)
  3996. lpDevInfo = (LPBYTE)HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, dwDevInfoSz);
  3997. }
  3998. if(lpRasentry != NULL) {
  3999. lpRasentry->dwSize = sizeof(RASENTRY);
  4000. dwRet = RasGetEntryProperties(NULL, lpRasEntryName[i].szEntryName, lpRasentry, &dwEntrySz, lpDevInfo, &dwDevInfoSz);
  4001. if(dwRet != 0) {
  4002. Dbg("RasGetEntryProperties twice failed: %d", dwRet);
  4003. } else {
  4004. Dbg("DeviceName: %s, DeviceType: %s", lpRasentry->szDeviceName, lpRasentry->szDeviceType);
  4005. strcpy_s(m_rasEntries[i].szDeviceName, lpRasentry->szDeviceName);
  4006. strcpy_s(m_rasEntries[i].szDeviceType, lpRasentry->szDeviceType);
  4007. strcpy_s(m_rasEntries[i].szPhoneNumber, lpRasentry->szLocalPhoneNumber);
  4008. strcpy_s(m_rasEntries[i].szGUID, (LPCTSTR)GuidToString(lpRasentry->guidId));
  4009. Dbg("EntryGUID: %s", m_rasEntries[i].szGUID);
  4010. m_rasEntries[i].dwfOptions = lpRasentry->dwfOptions;
  4011. }
  4012. }
  4013. if(lpRasentry) {
  4014. HeapFree(GetProcessHeap(), 0, lpRasentry);
  4015. lpRasentry = NULL;
  4016. }
  4017. if(lpDevInfo) {
  4018. HeapFree(GetProcessHeap(), 0, lpDevInfo);
  4019. lpDevInfo = NULL;
  4020. }
  4021. }
  4022. } else {
  4023. HeapFree(GetProcessHeap(), 0, lpRasEntryName);
  4024. lpRasEntryName = NULL;
  4025. SetRasStatus(MBD_INTERNAL_ERROR);
  4026. return -2;
  4027. }
  4028. HeapFree(GetProcessHeap(), 0, lpRasEntryName);
  4029. lpRasEntryName = NULL;
  4030. return dwEntries;
  4031. }
  4032. // There was either a problem with RAS or there are RAS entry names to enumerate
  4033. if(dwEntries >= 1) {
  4034. Dbg("The operation failed to acquire the buffer size.");
  4035. SetRasStatus(MBD_INTERNAL_ERROR);
  4036. return -3;
  4037. }
  4038. Dbg("There were no RAS entry names found.");
  4039. SetRasStatus(MBD_NO_ENTRY);
  4040. return 0;
  4041. }
  4042. ErrorCodeEnum CMobileDialFSM::AddNewProfile(
  4043. const CSimpleStringA csInterfaceID, const AccessKeyInfo& newInfo, bool fApplyNow)
  4044. {
  4045. LOG_FUNCTION();
  4046. ErrorCodeEnum rc = Error_NoDefine;
  4047. //
  4048. if(newInfo.usCntType == CNT_MBN) {
  4049. rc = m_pMbn->AddProfile(csInterfaceID, newInfo, fApplyNow);
  4050. if(rc == Error_Succeed) {
  4051. }
  4052. }else if(newInfo.usCntType == CNT_RAS) {
  4053. rc = CreatePhoneEntryEx(csInterfaceID, &newInfo, fApplyNow) < 0 ? Error_Unexpect : Error_Succeed;
  4054. if(rc == Error_Succeed) {
  4055. }
  4056. } else if(newInfo.usCntType == CNT_ANDROID) {
  4057. int slotID = strnicmp(csInterfaceID, "MAIN", strlen("MAIN")) == 0 ? 0 : 1;
  4058. rc = m_SafeWirelessClient->UpdateOrInsertAPN(slotID, newInfo, fApplyNow);
  4059. } else if(newInfo.usCntType == CNT_MBB) {
  4060. hw::apn_config_t config;
  4061. _ConvertToMBBApnStruct(newInfo, config);
  4062. int res = m_HwDeviceMnt->add_profile(config, fApplyNow);
  4063. if(res == 0) {
  4064. _StoreMBBPassword(newInfo.strProfileDesc, newInfo.strPassword);
  4065. rc = Error_Succeed;
  4066. } else {
  4067. const auto tmp = MBBResult2Status(res);
  4068. //SetHWStatus(tmp);
  4069. m_csErrMsg = CSimpleStringA::Format("添加配置到MIFI设备失败(%s)!", ConvertStatusToString(tmp));
  4070. rc = Error_Unexpect;
  4071. }
  4072. } else {
  4073. m_csErrMsg = "不支持的连接配置方式!";
  4074. }
  4075. return rc;
  4076. }
  4077. ErrorCodeEnum CMobileDialFSM::UpdateProfile(
  4078. const CSimpleStringA csInterfaceID, LPCTSTR lpszProfileName, const AccessKeyInfo& newInfo, bool fApplyNow)
  4079. {
  4080. LOG_FUNCTION();
  4081. if(newInfo.usCntType == CNT_MBN) {
  4082. return m_pMbn->UpdateProfile(csInterfaceID, lpszProfileName, newInfo, fApplyNow);
  4083. } else if(newInfo.usCntType == CNT_RAS) {
  4084. return UpdateEntryInfo(lpszProfileName, newInfo, fApplyNow);
  4085. } else if(newInfo.usCntType == CNT_ANDROID) {
  4086. int profileID = 0;
  4087. if(lpszProfileName == NULL || strlen(lpszProfileName) == 0) {
  4088. m_csErrMsg = "无效配置标识";
  4089. return Error_Param;
  4090. } else {
  4091. profileID = atoi(lpszProfileName);
  4092. }
  4093. int slotID = strnicmp(csInterfaceID, "MAIN", strlen("MAIN")) == 0 ? 0 : 1;
  4094. return m_SafeWirelessClient->UpdateOrInsertAPN(slotID, newInfo, fApplyNow, profileID);
  4095. } else if(newInfo.usCntType == CNT_MBB) {
  4096. hw::connect_profile_t config;
  4097. _ConvertToMBBApnStruct(newInfo, config.apn_config);
  4098. int profileID = atoi(lpszProfileName);
  4099. config.index = profileID;
  4100. config.is_default = fApplyNow;
  4101. int res = m_HwDeviceMnt->update_profile(config);
  4102. Dbg("update profile with id: %d returned %d", profileID, res);
  4103. if(res == 0) {
  4104. _StoreMBBPassword(newInfo.strProfileDesc, newInfo.strPassword);
  4105. return Error_Succeed;
  4106. } else {
  4107. const auto tmp = MBBResult2Status(res);
  4108. //SetHWStatus(tmp);
  4109. m_csErrMsg = CSimpleStringA::Format("更新配置到MIFI设备失败 (%s)!", ConvertStatusToString(tmp));
  4110. return Error_Unexpect;
  4111. }
  4112. }
  4113. else {
  4114. m_csErrMsg = "不支持的连接配置方式!";
  4115. return Error_NotMeetCondition;
  4116. }
  4117. }
  4118. ErrorCodeEnum CMobileDialFSM::DeleteProfile(int nDongleIdx, int nProfileIdx)
  4119. {
  4120. LOG_FUNCTION();
  4121. if(g_DongleInfos[nDongleIdx].usDeviceType == DEVTYPE_INTER) {
  4122. return m_pMbn->DeleteProfile(g_DongleInfos[nDongleIdx].csInterfaceId, g_DongleInfos[nDongleIdx].profileList[nProfileIdx].strProfileName);
  4123. } else if(g_DongleInfos[nDongleIdx].usDeviceType == DEVTYPE_MODEM) {
  4124. Dbg("DealConnctionActivity returned: %d", DealConnctionActivity(g_DongleInfos[nDongleIdx].profileList[nProfileIdx].AccessInfo.strProfileDesc));
  4125. DWORD dwRet = RasDeleteEntry(NULL, g_DongleInfos[nDongleIdx].profileList[nProfileIdx].AccessInfo.strProfileDesc);
  4126. if(dwRet == 0) {
  4127. return Error_Succeed;
  4128. } else {
  4129. m_csErrMsg = CSimpleStringA::Format("RasDeleteEntry failed: %d", dwRet);
  4130. Dbg("Delete entry(%s) failed : %d", (LPCTSTR)g_DongleInfos[nDongleIdx].profileList[nProfileIdx].AccessInfo.strProfileDesc, dwRet);
  4131. return Error_Unexpect;
  4132. }
  4133. } else if((g_DongleInfos[nDongleIdx].usDeviceType & DEVTYPE_ANDROID_UKNOWN )
  4134. || (g_DongleInfos[nDongleIdx].usDeviceType & DEVTYPE_MBB)) {
  4135. int profileID = 0;
  4136. if(!g_DongleInfos[nDongleIdx].profileList[nProfileIdx].strProfileName.IsNullOrEmpty()) {
  4137. profileID = atoi(g_DongleInfos[nDongleIdx].profileList[nProfileIdx].strProfileName);
  4138. }
  4139. if(profileID == 0) {
  4140. Dbg("invalid profile id! profileName:%s", g_DongleInfos[nDongleIdx].profileList[nProfileIdx].strProfileName.GetData());
  4141. m_csErrMsg = "无效配置序号";
  4142. return Error_Param;
  4143. }
  4144. if(g_DongleInfos[nDongleIdx].usDeviceType & DEVTYPE_ANDROID_UKNOWN) {
  4145. return m_SafeWirelessClient->DeleteProfile(profileID);
  4146. } else {
  4147. int res = m_HwDeviceMnt->delete_profile_by(profileID);
  4148. if(res == 0) {
  4149. Dbg("delete profile succ.");
  4150. _StoreMBBPassword(g_DongleInfos[nDongleIdx].profileList[nProfileIdx].AccessInfo.strProfileDesc, "");
  4151. return Error_Succeed;
  4152. } else {
  4153. const auto tmp = MBBResult2Status(res);
  4154. //SetHWStatus(tmp);
  4155. m_csErrMsg = CSimpleStringA::Format("删除配置失败(%s)!", ConvertStatusToString(tmp));
  4156. return Error_Unexpect;
  4157. }
  4158. }
  4159. }
  4160. m_csErrMsg = "不支持的连接配置文件类型";
  4161. return Error_NoDefine;
  4162. }
  4163. ErrorCodeEnum CMobileDialFSM::ConnectWithDefault()
  4164. {
  4165. if(m_SafeWirelessClient) {
  4166. m_csErrMsg = "";
  4167. return m_SafeWirelessClient->ConnectWithDefault();
  4168. }
  4169. return Error_NotInit;
  4170. }
  4171. int CMobileDialFSM::RetrieveDongleInfo()
  4172. {
  4173. ResetDongleInfos();
  4174. if(isUsingNewVerAllInOne()) {
  4175. BOOL bActive = FALSE, bConnected = FALSE;
  4176. ErrorCodeEnum ec = m_SafeWirelessClient->IsActive(bActive, bConnected);
  4177. if(ec == Error_Succeed) {
  4178. if(!bConnected) {
  4179. SendMsgToDialog("当前未连接到多合一,请稍后再试!");
  4180. return -2;
  4181. }
  4182. int nNum = 0;
  4183. ec = m_SafeWirelessClient->GetAPNConfigs(-1, nNum);
  4184. return nNum;
  4185. } else {
  4186. SendMsgToDialog(CSimpleStringA::Format("连接多合一管控实体失败(0x%X)", ec));
  4187. return -1;
  4188. }
  4189. }
  4190. if(m_nAdvancedGen == ADVANCE_MBB) {
  4191. const auto hwStatus = GetHWStatus();
  4192. //if(hwStatus == MBD_WLAN_CONNECTED) {
  4193. // int nNum3 = RetriveMifiInfo();
  4194. // if(nNum3 > 0) {
  4195. // return nNum3;
  4196. // }
  4197. //} else {
  4198. // m_csErrMsg = GetFurtherDetail();
  4199. //}
  4200. if(hwStatus == MBD_NO_MOBILEDONGLE
  4201. || hwStatus == MBD_WLAN_DISCONNECTED
  4202. || hwStatus == MBD_WLAN_CONNECTING
  4203. || hwStatus == MBD_WLAN_CONNECT_FAILED) {
  4204. m_csErrMsg = GetFurtherDetail();
  4205. } else {
  4206. int nNum3 = RetriveMifiInfo();
  4207. if(nNum3 > 0) {
  4208. return nNum3;
  4209. }
  4210. }
  4211. return -500;
  4212. }
  4213. int nNum = m_nAdvancedGen != 3 ? m_pMbn->EnumerateInterfaces(TRUE) : 0;
  4214. int nNum2 = m_nAdvancedGen !=4 ? EnumCurRasDevices(TRUE) : 0;
  4215. m_n3G = nNum2;
  4216. m_n4G = nNum;
  4217. if(nNum >= 0 || nNum2 >= 0) {
  4218. if(nNum2 >= 0 && nNum >= 0) {
  4219. return nNum + nNum2;
  4220. } else if(nNum >= 0)
  4221. return nNum;
  4222. else
  4223. return nNum2;
  4224. }
  4225. return (nNum < 0 ? nNum : nNum2);
  4226. }
  4227. void CMobileDialFSM::TellDlgTheResult(ErrorCodeEnum ec, UINT uMsg, LPARAM lparam)
  4228. {
  4229. if(m_hMainWnd) {
  4230. PostMessage(m_hMainWnd, uMsg, ec, (LPARAM)lparam);
  4231. }
  4232. }
  4233. BOOL CMobileDialFSM::UpdateProfileInfo(int nCurSel)
  4234. {
  4235. LOG_FUNCTION();
  4236. LOG_ASSERT(nCurSel >= 0);
  4237. BOOL bRes = FALSE;
  4238. if(g_DongleInfos[nCurSel].usDeviceType == DEVTYPE_INTER) {
  4239. bRes = m_pMbn->SearchForProfileName(&g_DongleInfos[nCurSel]);
  4240. if(!bRes && m_pMbn->m_tmpState == MBD_NO_PROFILE) {
  4241. Dbg("Because there are no profile...");
  4242. return TRUE;
  4243. }
  4244. } else if(g_DongleInfos[nCurSel].usDeviceType == DEVTYPE_MODEM) {
  4245. int nEntryNum = RefreshRasEntry();
  4246. if(nEntryNum < 0) {
  4247. return FALSE;
  4248. }
  4249. int nGongleCnt = g_DongleInfos.GetCount();
  4250. for(int i=0; i<nGongleCnt; ++i) {
  4251. if(g_DongleInfos[i].usDeviceType == DEVTYPE_MODEM) {
  4252. int cnt = 0;
  4253. for(int j=0; j<nEntryNum; ++j) {
  4254. if(!g_DongleInfos[i].subInfo.strProviderName.Compare(m_rasEntries[j].szDeviceName)) {
  4255. cnt++;
  4256. }
  4257. }
  4258. g_DongleInfos[i].profileList.Clear();
  4259. if(cnt > 0) {
  4260. g_DongleInfos[i].profileList.Init(cnt);
  4261. for(int j=0, z=0; j<nEntryNum; ++j) {
  4262. if(!g_DongleInfos[i].subInfo.strProviderName.Compare(m_rasEntries[j].szDeviceName)) {
  4263. _EntriesToDongle(m_rasEntries[j], &g_DongleInfos[i].profileList[z]);
  4264. z++;
  4265. }
  4266. }
  4267. }
  4268. }
  4269. }
  4270. return TRUE;
  4271. } else if(g_DongleInfos[nCurSel].usDeviceType == DEVTYPE_MBB) {
  4272. LOG_ASSERT(nCurSel == 0);
  4273. g_DongleInfos[nCurSel].profileList.Clear();
  4274. int nNum = _GetMBBProfiles(g_DongleInfos[nCurSel]);
  4275. if(nNum < 0) {
  4276. m_csErrMsg = CSimpleStringA::Format("获取配置文件失败:%d", nNum);
  4277. return FALSE;
  4278. }
  4279. return TRUE;
  4280. } else if(g_DongleInfos[nCurSel].usDeviceType & DEVTYPE_ANDROID_UKNOWN) {
  4281. BOOL bActive = FALSE, bConnected = FALSE;
  4282. ErrorCodeEnum ec = m_SafeWirelessClient->IsActive(bActive, bConnected);
  4283. if(ec == Error_Succeed) {
  4284. if(!bConnected) {
  4285. m_csErrMsg = "当前未连接到多合一,请稍后再试!";
  4286. return FALSE;
  4287. }
  4288. int nNum = 0;
  4289. ec = m_SafeWirelessClient->GetAPNConfigsPure(nCurSel, nNum);
  4290. if(ec == Error_Succeed) {
  4291. return TRUE;
  4292. }
  4293. m_csErrMsg = CSimpleStringA::Format("调用接口 GetAPNConfigsPure 失败:%d", ec);
  4294. return FALSE;
  4295. } else {
  4296. m_csErrMsg = CSimpleStringA::Format("连接多合一管控实体失败(0x%X)", ec);
  4297. return FALSE;
  4298. }
  4299. } else {
  4300. m_csErrMsg = CSimpleStringA::Format("未知连接类型:%d", g_DongleInfos[nCurSel].usDeviceType);
  4301. }
  4302. return bRes;
  4303. }
  4304. BOOL CMobileDialFSM::UpdateSubInfo(int nCurSel)
  4305. {
  4306. LOG_FUNCTION();
  4307. LOG_ASSERT(nCurSel >= 0);
  4308. if(g_DongleInfos[nCurSel].usDeviceType == DEVTYPE_INTER) {
  4309. return m_pMbn->GetInterfaceInfo(g_DongleInfos[nCurSel].csInterfaceId,
  4310. &g_DongleInfos[nCurSel].subInfo);
  4311. } else if(g_DongleInfos[nCurSel].usDeviceType == DEVTYPE_MODEM) {
  4312. return UpdateRasConnectState(true) >= 0 ? TRUE : FALSE;
  4313. } else if(g_DongleInfos[nCurSel].usDeviceType & DEVTYPE_ANDROID_UKNOWN) {
  4314. int slotNum = 1;
  4315. if(g_DongleInfos[nCurSel].usDeviceType == DEVTYPE_ANDROID_MAIN) {
  4316. slotNum = 1; // sim_slot_main
  4317. } else if(g_DongleInfos[nCurSel].usDeviceType == DEVTYPE_ANDROID_VICE) {
  4318. slotNum = 2; // sim_slot_vice
  4319. } else {
  4320. m_csErrMsg = CSimpleStringA::Format("无效卡槽序号:%d", g_DongleInfos[nCurSel].usDeviceType);
  4321. return FALSE;
  4322. }
  4323. int nNum = 0;
  4324. ErrorCodeEnum ec = m_SafeWirelessClient->GetAPNConfigs(-1, nNum, true, false);
  4325. if(ec == Error_Succeed) {
  4326. return TRUE;
  4327. }
  4328. m_csErrMsg = CSimpleStringA::Format("调用接口 GetAPNConfigs 失败:%d", ec);
  4329. return FALSE;
  4330. } else if(g_DongleInfos[nCurSel].usDeviceType == DEVTYPE_MBB) {
  4331. if(_GetMBBDeviceInfo(g_DongleInfos[nCurSel]) == 0){
  4332. return TRUE;
  4333. }
  4334. return FALSE;
  4335. }
  4336. return FALSE;
  4337. }
  4338. void CMobileDialFSM::StorePersistentRequetsId(ULONG ulRequestID)
  4339. {
  4340. CSmartPointer<IConfigInfo> spConfigRun;
  4341. GetEntityBase()->GetFunction()->OpenConfig(Config_Run, spConfigRun);
  4342. spConfigRun->WriteConfigValueHexInt("MBN", "RequestID", ulRequestID);
  4343. //Dbg("Store RequestID: %d", ulRequestID);
  4344. }
  4345. BOOL CMobileDialFSM::GetPersistentRequestId(ULONG& ulRequestID)
  4346. {
  4347. UINT64 data;
  4348. CSmartPointer<IConfigInfo> spConfigRun;
  4349. GetEntityBase()->GetFunction()->OpenConfig(Config_Run, spConfigRun);
  4350. if(spConfigRun->ReadConfigValueHexInt("MBN", "RequestID", data) == Error_Succeed) {
  4351. ulRequestID = (ULONG)data;
  4352. return TRUE;
  4353. }
  4354. Dbg("Read run config value for RequestID failed");
  4355. return FALSE;
  4356. }
  4357. void CMobileDialFSM::EraseTheRecentInfo(const ULONG ulRequestID)
  4358. {
  4359. LOG_FUNCTION();
  4360. Dbg("the coming id: %d", ulRequestID);
  4361. Dbg("recent type: %d", m_recentInfo.usDialType);
  4362. Dbg("recent request id: %d", m_recentInfo.ulRequestID);
  4363. if(ulRequestID == 0) {
  4364. return;
  4365. }
  4366. if(m_recentInfo.usDialType == CNT_MBN && m_recentInfo.ulRequestID == ulRequestID
  4367. || (m_recentInfo.usDialType == CNT_RAS && m_recentInfo.ulRequestID == ulRequestID)) {
  4368. Dbg("Start to remove recent infor : %d", m_recentInfo.usDialType == CNT_RAS);
  4369. {
  4370. int aim = -1;
  4371. for(int i=0; i<mBlackSheepCount; ++i) {
  4372. if(!m_recentInfo.strDongle.Compare(m_blackSheep[i].strDongle)
  4373. && !m_recentInfo.strProfile.Compare(m_blackSheep[i].strProfile)) {
  4374. aim = i;
  4375. break;
  4376. }
  4377. }
  4378. if(aim != -1) {
  4379. m_blackSheep[aim].dwErrCount++;
  4380. } else if(mBlackSheepCount < MAX_BLACK_SHEEP_NUM) {
  4381. m_blackSheep[mBlackSheepCount].strDongle = m_recentInfo.strDongle;
  4382. m_blackSheep[mBlackSheepCount].strProfile = m_recentInfo.strProfile;
  4383. m_blackSheep[mBlackSheepCount].usDialType = m_recentInfo.usDialType;
  4384. m_blackSheep[mBlackSheepCount].dwErrCount = 1;
  4385. m_blackSheep[mBlackSheepCount].Flag = FALSE;
  4386. mBlackSheepCount++;
  4387. }
  4388. }
  4389. m_recentInfo.usDialType = CNT_INVALID;
  4390. m_recentInfo.ulRequestID = 0;
  4391. CSmartPointer<IConfigInfo> spConfigRun;
  4392. GetEntityBase()->GetFunction()->OpenConfig(Config_Run, spConfigRun);
  4393. spConfigRun->WriteConfigValue("Recent", "Type", "Invalid");
  4394. spConfigRun->WriteConfigValue("Recent", "Dongle", "");
  4395. spConfigRun->WriteConfigValue("Recent", "Profile", "");
  4396. spConfigRun->WriteConfigValueInt("Recent", "Manual", 0);
  4397. spConfigRun->WriteConfigValueHexInt("Recent", "RequestID", 0);
  4398. } else {
  4399. Dbg("try to erase the recent dial info failed!");
  4400. }
  4401. }
  4402. void CMobileDialFSM::SetAutoManage(bool bAuto /*= true*/)
  4403. {
  4404. CSmartPointer<IConfigInfo> spConfigRun;
  4405. GetEntityBase()->GetFunction()->OpenConfig(Config_Run, spConfigRun);
  4406. spConfigRun->WriteConfigValueInt("init", "ManualManage", bAuto ? 0 : 1);
  4407. if(m_bManageByUser == bAuto) {
  4408. m_bManageByUser = !bAuto;
  4409. if(!m_bManageByUser && GetCurrState()->id == s5) {
  4410. Dbg("Recover and should to run...s5 entry");
  4411. ConnectTask* task = new ConnectTask(this);
  4412. GetEntityBase()->GetFunction()->PostThreadPoolTask(task);
  4413. }
  4414. }
  4415. }
  4416. void CMobileDialFSM::SetAdvancedGen(int newValue)
  4417. {
  4418. if(newValue == 1) {
  4419. newValue = ADVANCE_RAS;
  4420. } else if(newValue == 2) {
  4421. newValue = ADVANCE_MBN;
  4422. } else if(newValue == 3) {
  4423. newValue = 0;
  4424. } else if(newValue == 4) {
  4425. newValue = ADVANCE_MBB;
  4426. } else {
  4427. return;
  4428. }
  4429. CSmartPointer<IConfigInfo> spConfigRun;
  4430. GetEntityBase()->GetFunction()->OpenConfig(Config_Run, spConfigRun);
  4431. spConfigRun->WriteConfigValueInt("init", "GenAdvanced", newValue);
  4432. //m_nAdvancedGen = newValue;
  4433. }
  4434. // 映射到窗体的Radio控件的顺序
  4435. int CMobileDialFSM::GetAdvancedGen(bool fRead5RunCfg /*= false*/)
  4436. {
  4437. int ret = 0;
  4438. if(!fRead5RunCfg) {
  4439. ret = m_nAdvancedGen;
  4440. } else {
  4441. CSmartPointer<IConfigInfo> spConfigRun;
  4442. GetEntityBase()->GetFunction()->OpenConfig(Config_Run, spConfigRun);
  4443. spConfigRun->ReadConfigValueInt("init", "GenAdvanced", ret);
  4444. if(ret != ADVANCE_MBB && ret != ADVANCE_MBN && ret != ADVANCE_RAS) {
  4445. ret = 0;
  4446. }
  4447. }
  4448. if(ret == ADVANCE_RAS) {
  4449. return 1;
  4450. } else if(ret == ADVANCE_MBN) {
  4451. return 2;
  4452. } else if(ret == ADVANCE_MBB) {
  4453. return 4;
  4454. }
  4455. return 3;
  4456. }
  4457. BOOL CMobileDialFSM::isUsingNewVerAllInOne()
  4458. {
  4459. #if HAS_NEW_ALL_IN_ONE
  4460. static int status = -1;
  4461. if(status == -1) {
  4462. if(m_SafeWirelessClient == NULL) {
  4463. EnterCriticalSection(&m_cs4WirlessClient);
  4464. if(m_SafeWirelessClient == NULL) {
  4465. m_SafeWirelessClient = new MySpace::WirelessAgentSafeClient(this);
  4466. }
  4467. LeaveCriticalSection(&m_cs4WirlessClient);
  4468. }
  4469. BOOL isActive = FALSE, isConnected = FALSE;
  4470. ErrorCodeEnum ec = m_SafeWirelessClient->IsActive(isActive, isConnected);
  4471. if(ec == Error_Succeed) {
  4472. if(isActive) {
  4473. status = 1;
  4474. } else {
  4475. status = 0;
  4476. }
  4477. }
  4478. }
  4479. if(status == -1) {
  4480. BOOL active = FALSE;
  4481. CSimpleStringA strDevName;
  4482. ErrorCodeEnum ec = SpDetectWirelessConfig(GetEntityBase(), active, strDevName);
  4483. if(active) {
  4484. status = 1;
  4485. } else if(ec == 0) {
  4486. status = 0;
  4487. }
  4488. }
  4489. if(status == 1) {
  4490. return TRUE;
  4491. }
  4492. #endif
  4493. return FALSE;
  4494. }
  4495. ErrorCodeEnum CMobileDialFSM::SwitchDefaultSlot(int slotIdx)
  4496. {
  4497. return m_SafeWirelessClient->SetDefaultSlot(slotIdx);
  4498. }
  4499. void CMobileDialFSM::SaveMBBConfig(const MBBParams* param, BOOL autoSave)
  4500. {
  4501. CSmartPointer<IConfigInfo> spConfigRun;
  4502. GetEntityBase()->GetFunction()->OpenConfig(Config_Run, spConfigRun);
  4503. spConfigRun->WriteConfigValue("MBB", "Username", param->loginParam.strUsername);
  4504. CSimpleStringA csEValue = EncryptString(param->loginParam.strPassword);
  4505. spConfigRun->WriteConfigValue("MBB", "Password", csEValue);
  4506. spConfigRun->WriteConfigValueInt("MBB", "ConnectType", param->connectType);
  4507. spConfigRun->WriteConfigValue("MBB", "SSID", param->wlanConnectParam.strUsername);
  4508. csEValue = EncryptString(param->wlanConnectParam.strPassword);
  4509. spConfigRun->WriteConfigValue("MBB", "SharedKey", csEValue);
  4510. spConfigRun->WriteConfigValueInt("MBB", "Hidden", param->wlanConnectParam.Flag);
  4511. spConfigRun->WriteConfigValueInt("MBB", "Customized", autoSave ? FALSE : TRUE);
  4512. if(!autoSave) {
  4513. Dbg("Update MBB param from outside.");
  4514. }
  4515. }
  4516. bool CMobileDialFSM::GetMBBConfig(MBBParams* param, BOOL fSetDefaultIfEmpty)
  4517. {
  4518. if(param == NULL) {
  4519. return false;
  4520. }
  4521. CSmartPointer<IConfigInfo> spConfigRun;
  4522. GetEntityBase()->GetFunction()->OpenConfig(Config_Run, spConfigRun);
  4523. param->connectType = MBB_CONNECT_UNKONWN;
  4524. spConfigRun->ReadConfigValueInt("MBB", "ConnectType", param->connectType);
  4525. param->loginParam.strUsername.Clear();
  4526. spConfigRun->ReadConfigValue("MBB", "Username", param->loginParam.strUsername);
  4527. CSimpleStringA csEValue;
  4528. spConfigRun->ReadConfigValue("MBB", "Password", csEValue);
  4529. param->loginParam.strPassword.Clear();
  4530. param->loginParam.strPassword = DecryptString(csEValue);
  4531. param->loginParam.Flag = FALSE;
  4532. param->wlanConnectParam.strUsername.Clear();
  4533. spConfigRun->ReadConfigValue("MBB", "SSID", param->wlanConnectParam.strUsername);
  4534. csEValue.Clear();
  4535. spConfigRun->ReadConfigValue("MBB", "SharedKey", csEValue);
  4536. param->wlanConnectParam.strPassword.Clear();
  4537. param->wlanConnectParam.strPassword = DecryptString(csEValue);
  4538. param->wlanConnectParam.Flag = FALSE;
  4539. spConfigRun->ReadConfigValueInt("MBB", "Hidden", param->wlanConnectParam.Flag);
  4540. param->customSet = FALSE;
  4541. spConfigRun->ReadConfigValueInt("MBB", "Customized", param->customSet);
  4542. BOOL fResetFlag = FALSE;
  4543. if(fSetDefaultIfEmpty) {
  4544. if(param->connectType == MBB_CONNECT_UNKONWN) {
  4545. param->connectType = MBB_CONNECT_WIFI;
  4546. fResetFlag = TRUE;
  4547. }
  4548. if(param->loginParam.strUsername.IsNullOrEmpty()
  4549. || param->loginParam.strPassword.IsNullOrEmpty()) {
  4550. param->loginParam.strUsername = DEFAULT_MIFI_LOGIN_USERNAME;
  4551. param->loginParam.strPassword = DEFAULT_PASSWORD_PREFIX;
  4552. param->loginParam.strPassword += m_strTerminalNo.SubString(0, 6);
  4553. param->loginParam.Flag = FALSE;
  4554. fResetFlag = TRUE;
  4555. }
  4556. if(param->wlanConnectParam.strUsername.IsNullOrEmpty()
  4557. || param->wlanConnectParam.strPassword.IsNullOrEmpty()) {
  4558. CSimpleStringA strNewSSID(m_strTerminalNo.GetData());
  4559. strNewSSID += "-MIFI";
  4560. CSimpleStringA strNewPassword(DEFAULT_PASSWORD_PREFIX);
  4561. strNewPassword += m_strTerminalNo.SubString(6, m_strTerminalNo.GetLength() - 6);
  4562. param->wlanConnectParam.strUsername = strNewSSID;
  4563. param->wlanConnectParam.strPassword = strNewPassword;
  4564. param->wlanConnectParam.Flag = TRUE;
  4565. param->customSet = FALSE;
  4566. fResetFlag = TRUE;
  4567. }
  4568. }
  4569. if(param != &mMBBParam && !fResetFlag) {
  4570. mMBBParam.connectType = param->connectType;
  4571. mMBBParam.loginParam.strUsername = (LPCTSTR)param->loginParam.strUsername;
  4572. mMBBParam.loginParam.strPassword = (LPCTSTR)param->loginParam.strPassword;
  4573. mMBBParam.loginParam.Flag = param->loginParam.Flag;
  4574. mMBBParam.wlanConnectParam.strUsername = (LPCTSTR)param->wlanConnectParam.strUsername;
  4575. mMBBParam.wlanConnectParam.strPassword = (LPCTSTR)param->wlanConnectParam.strPassword;
  4576. mMBBParam.wlanConnectParam.Flag = param->wlanConnectParam.Flag;
  4577. mMBBParam.customSet = param->customSet;
  4578. } else {
  4579. csEValue.Clear();
  4580. spConfigRun->ReadConfigValue("MBB", "StoredSharedKey", csEValue);
  4581. m_strReservedPasword = DecryptString(csEValue); /*暂时没有用*/
  4582. }
  4583. return true;
  4584. }
  4585. ErrorCodeEnum CMobileDialFSM::ConnectWlan()
  4586. {
  4587. connect_param conn_param_;
  4588. memset(&conn_param_, 0, sizeof(conn_param_));
  4589. conn_param_.temporary_mode = true;
  4590. //if(m_strReservedPasword.IsNullOrEmpty()
  4591. // || 0 != mMBBParam.wlanConnectParam.strPassword.Compare(m_strReservedPasword.GetData())) {
  4592. // LogEvent(Severity_Middle, 0, "Need to update password!");
  4593. // conn_param_.rewrite_password = true;
  4594. // strcpy_s(conn_param_.new_password, mMBBParam.wlanConnectParam.strPassword.GetData());
  4595. //}
  4596. conn_param_.hidden_mode = !!mMBBParam.wlanConnectParam.Flag;
  4597. strcpy_s(conn_param_.friendly_name, mMBBParam.wlanConnectParam.strUsername.GetData());
  4598. strcpy_s(conn_param_.password, mMBBParam.wlanConnectParam.strPassword.GetData());
  4599. Dbg("Start to connect wifi %s", conn_param_.friendly_name);
  4600. Dbg("hidden or not? %s", conn_param_.hidden_mode ? "true" : "false");
  4601. if(m_dwWlanConnectTimes % 3 == 0 && !conn_param_.hidden_mode) {
  4602. LogWarn(Severity_Low, Error_DataCheck, CombineErrorCode(MBD_WLAN_SSID_VISIBLE), "安全起见,请勿将 WLAN 设为广播模式!!");
  4603. }
  4604. int rc = spagent::spagent_assistor::connect_wifi_with_param(&conn_param_);
  4605. Dbg("connect to wifi(%s) returned: %d", conn_param_.friendly_name, rc);
  4606. if(rc != 0) {
  4607. LogWarn(Severity_Low, Error_NetBroken, 0, (LPCTSTR)CSimpleStringA::Format("connect to %s failed, hidden mode(%s): %d",
  4608. conn_param_.friendly_name, (conn_param_.hidden_mode ? "true" : "false"), rc));
  4609. }
  4610. return (ErrorCodeEnum)rc;
  4611. }
  4612. BOOL CMobileDialFSM::NeedToConnectWifi()
  4613. {
  4614. LOG_ASSERT(!mMBBParam.wlanConnectParam.strUsername.IsNullOrEmpty());
  4615. std::string ssid;
  4616. //Dbg("start to find out whether is connected or not..");
  4617. int rc = spagent::spagent_assistor::is_connected(conn_tool_wifi, ssid);
  4618. if(rc == 0) {
  4619. Dbg("wifi is not connect !");
  4620. } else if(rc > 0) {
  4621. Dbg("wifi is connected and connected ssid is: %s", ssid.c_str());
  4622. if(mMBBParam.wlanConnectParam.strUsername.Compare(ssid.c_str())) {
  4623. //Dbg("force reconnect wifi bcz the ssid is not: %s", (LPCTSTR)mMBBParam.wlanConnectParam.strUsername);
  4624. LogWarn(Severity_High, Error_Unexpect, CombineErrorCode(MBD_WLAN_CONNECTED_WITH_ILLEGAL_SSID),
  4625. CSimpleStringA::Format("Unexpected wifi %s is connected, force to reconnect to %s immediately!",
  4626. ssid.c_str(), mMBBParam.wlanConnectParam.strUsername.GetData()));
  4627. rc = 0;
  4628. }
  4629. }
  4630. return (rc == 0);
  4631. }
  4632. void CMobileDialFSM::UpdateWifiPassword(const CSimpleStringA& password)
  4633. {
  4634. CSmartPointer<IConfigInfo> spConfigRun;
  4635. GetEntityBase()->GetFunction()->OpenConfig(Config_Run, spConfigRun);
  4636. CSimpleStringA csEValue = EncryptString(password);
  4637. spConfigRun->WriteConfigValue("MBB", "StoredSharedKey", csEValue);
  4638. m_strReservedPasword = (LPCTSTR)password;
  4639. }
  4640. int CMobileDialFSM::RetriveMifiInfo()
  4641. {
  4642. if(m_HwDeviceMnt->detect_devices()) {
  4643. int res = _LoginMBBDevice(mMBBParam);
  4644. if(res != 0) {
  4645. return res;
  4646. }
  4647. g_DongleInfos.Init(1);
  4648. MbnDongleInfo& dongle = g_DongleInfos[0];
  4649. dongle.profileList.Clear();
  4650. res = _GetMBBDeviceInfo(dongle);
  4651. if(res < 0) {
  4652. return res;
  4653. }
  4654. _GetMBBProfiles(dongle);
  4655. return 1;
  4656. } else {
  4657. SendMsgToDialog("未找到MIFI设备!");
  4658. return 0;
  4659. }
  4660. }
  4661. BOOL CMobileDialFSM::SwithMBBConnectStatus(BOOL toConnect)
  4662. {
  4663. if(!toConnect) {
  4664. int res = m_HwDeviceMnt->disconnect();
  4665. if(res == 0) {
  4666. Dbg("disconnect mifi network succ.");
  4667. return TRUE;
  4668. }
  4669. else {
  4670. const auto tmp = MBBResult2Status(res);
  4671. //SetHWStatus(tmp);
  4672. m_csErrMsg = CSimpleStringA::Format("MIFI断开上网连接失败!(%s)", ConvertStatusToString(tmp));
  4673. return FALSE;
  4674. }
  4675. } else {
  4676. int res = m_HwDeviceMnt->connect();
  4677. if(res == 0) {
  4678. Dbg("connect mifi network succ.");
  4679. return TRUE;
  4680. }
  4681. else {
  4682. const auto tmp = MBBResult2Status(res);
  4683. //SetHWStatus(tmp);
  4684. m_csErrMsg = CSimpleStringA::Format("MIFI拨号上网连接失败!(%s)", ConvertStatusToString(tmp));
  4685. return FALSE;
  4686. }
  4687. }
  4688. }
  4689. void CMobileDialFSM::CheckHWDeviceStatus()
  4690. {
  4691. BOOL fToExtend = FALSE;
  4692. if(m_nAdvancedGen == ADVANCE_MBB) {
  4693. if(mMBBParam.connectType == MBB_CONNECT_WIRE) {
  4694. if(CheckLANConnectStatus() == 0) {
  4695. SetHWStatus(MBD_NO_MOBILEDONGLE);
  4696. } else {
  4697. SetHWStatus(MBD_ACTIVATION_CONNECTED);
  4698. fToExtend = TRUE;
  4699. }
  4700. } else if(mMBBParam.connectType == MBB_CONNECT_WIFI) {
  4701. if(NeedToConnectWifi()) {
  4702. SetHWStatus(MBD_WLAN_DISCONNECTED);
  4703. } else {
  4704. SetHWStatus(MBD_WLAN_CONNECTED);
  4705. fToExtend = TRUE;
  4706. }
  4707. } else {
  4708. SetHWStatus(MBD_RUN_CONFIG_INCOMPLEMENT);
  4709. }
  4710. Dbg("Huawei status: %s", ConvertStatusToString(GetHWStatus()));
  4711. if(fToExtend) {
  4712. const int res = RetriveMifiInfo();
  4713. }
  4714. }
  4715. }
  4716. void CMobileDialFSM::_ConvertToMBBApnStruct(const AccessKeyInfo& newInfo, hw::apn_config_t& config)
  4717. {
  4718. memset(&config, 0, sizeof(hw::apn_config_t));
  4719. wcscpy_s(config.profile_name, spagent::util::string_util::s2w(newInfo.strProfileDesc.GetData()).c_str());
  4720. wcscpy_s(config.apn_name, spagent::util::string_util::s2w(newInfo.strAccessString.GetData()).c_str());
  4721. wcscpy_s(config.username, spagent::util::string_util::s2w(newInfo.strUserName.GetData()).c_str());
  4722. wcscpy_s(config.password, spagent::util::string_util::s2w(newInfo.strPassword.GetData()).c_str());
  4723. if(newInfo.usAPType == AUPR_PAP) {
  4724. config.auth_mode = hw::auth_pap;
  4725. } else if(newInfo.usAPType == AUPR_CHAP) {
  4726. config.auth_mode = hw::auth_chap;
  4727. } else if(newInfo.usAPType == AUPR_PAPORCHAP) {
  4728. config.auth_mode = hw::auth_none;
  4729. } else {
  4730. config.auth_mode = hw::auth_auto;
  4731. }
  4732. if(newInfo.strIPType.Compare("IPv4") == 0) {
  4733. config.ip_type = hw::ipv4;
  4734. } else if(newInfo.strIPType.Compare("IPv6") == 0) {
  4735. config.ip_type = hw::ipv6;
  4736. } else /*if(newInfo.strIPType.Compare("IPv4v6") == 0)*/ {
  4737. config.ip_type = hw::ipv46;
  4738. }
  4739. }
  4740. int CMobileDialFSM::_GetMBBDeviceInfo(MbnDongleInfo& dongle)
  4741. {
  4742. InitMbnDongleSubInfo(&dongle.subInfo);
  4743. dongle.state = MBD_ACTIVATION_DISCONNECTED;
  4744. dongle.usDeviceType = DEVTYPE_MBB;
  4745. hw::dev_info_t dev_info;
  4746. Dbg("start to get dev info...");
  4747. int res = m_HwDeviceMnt->get_dev_info(dev_info);
  4748. if(res == 0) {
  4749. dongle.subInfo.strProviderName = spagent::util::string_util::w2s(dev_info.dev_name).c_str();
  4750. dongle.subInfo.strDeviceID = spagent::util::string_util::w2s(dev_info.imsi).c_str();
  4751. dongle.subInfo.strManufacturer = spagent::util::string_util::w2s(dev_info.sn).c_str();
  4752. dongle.subInfo.strCellularClass = spagent::util::string_util::w2s(dev_info.work_mode).c_str();
  4753. dongle.subInfo.strFirmware = spagent::util::string_util::w2s(dev_info.hardware_version).c_str();
  4754. dongle.subInfo.strSimIccID = spagent::util::string_util::w2s(dev_info.iccid).c_str();
  4755. dongle.subInfo.strMode = spagent::util::string_util::w2s(dev_info.provider_name).c_str();
  4756. dongle.csInterfaceId = spagent::util::string_util::w2s(dev_info.dev_name).c_str();
  4757. dongle.subInfo.ulSingnal = dev_info.network_status.signal_strength;
  4758. dongle.subInfo.mbnCntState = MBN_ACTIVATION_STATE_NONE;
  4759. Dbg("connect status: %d", (int)dev_info.network_status.connect_status);
  4760. if(dev_info.network_status.connect_status == hw::connected) {
  4761. dongle.subInfo.mbnCntState = MBN_ACTIVATION_STATE_ACTIVATED;
  4762. } else if(dev_info.network_status.connect_status == hw::connecting) {
  4763. dongle.subInfo.mbnCntState = MBN_ACTIVATION_STATE_ACTIVATING;
  4764. } else if(dev_info.network_status.connect_status == hw::disconnecting) {
  4765. dongle.subInfo.mbnCntState = MBN_ACTIVATION_STATE_DEACTIVATING;
  4766. } else if(dev_info.network_status.connect_status == hw::disconnected) {
  4767. dongle.subInfo.mbnCntState = MBN_ACTIVATION_STATE_DEACTIVATED;
  4768. }
  4769. dongle.state = GetMBBConnectState(dev_info.network_status);
  4770. SetHWStatus(dongle.state);
  4771. Dbg("dongle connect status: %d", (int)dongle.subInfo.mbnCntState);
  4772. } else {
  4773. Dbg("get the mifi dev information failed: %d", res);
  4774. const auto tmp = MBBResult2Status(res);
  4775. //SetHWStatus(tmp);
  4776. SendMsgToDialog(CSimpleStringA::Format("获取MIFI设备信息失败 (%s)", ConvertStatusToString(tmp)));
  4777. res = -1;
  4778. }
  4779. return res;
  4780. }
  4781. int CMobileDialFSM::_GetMBBProfiles(MbnDongleInfo& dongle)
  4782. {
  4783. std::vector<hw::connect_profile_t> profiles;
  4784. Dbg("start to get connect profile...");
  4785. hw::connect_profile_t* profile = new hw::connect_profile_t[PROFILE_MAX_COUNT];
  4786. memset(profile, 0, sizeof(hw::connect_profile_t)*PROFILE_MAX_COUNT);
  4787. int profileNum = PROFILE_MAX_COUNT;
  4788. //int nNum = m_HwDeviceMnt->get_connect_profiles(profiles);
  4789. int nNum = m_HwDeviceMnt->get_profiles(profile, profileNum);
  4790. Dbg("get_connect_profiles returned %d", nNum);
  4791. if(nNum > 0) {
  4792. dongle.profileList.Init(nNum);
  4793. int idx = 0;
  4794. //while(it != nullptr) {
  4795. for(int i=0; i<nNum; ++i) {
  4796. const hw::connect_profile_t* it = &profile[i];
  4797. //for(auto it=profiles.cbegin(); it!=profiles.cend(); ++it) {
  4798. MbnProfileInfo& profile = dongle.profileList[idx];
  4799. InitAccessKeyInfo(&dongle.profileList[idx].AccessInfo);
  4800. profile.bUserDefined = true;
  4801. profile.AccessInfo.usCntType = CNT_MBB;
  4802. profile.AccessInfo.strProfileDesc = spagent::util::string_util::w2s(it->apn_config.profile_name).c_str();
  4803. profile.strProfileName = CSimpleStringA::Format("%d", it->index);
  4804. profile.AccessInfo.IsDefault = it->is_default;
  4805. if(it->is_default) {
  4806. dongle.subInfo.strProfileName = profile.strProfileName.GetData();
  4807. }
  4808. profile.AccessInfo.strAccessString = spagent::util::string_util::w2s(it->apn_config.apn_name).c_str();
  4809. profile.AccessInfo.strUserName = spagent::util::string_util::w2s(it->apn_config.username).c_str();
  4810. profile.AccessInfo.strPassword = spagent::util::string_util::w2s(it->apn_config.password).c_str();
  4811. //TODO: 新版的MIFI有给定特殊的********号字符,需要做处理,后续验证
  4812. if(profile.AccessInfo.strPassword.IsNullOrEmpty()) {
  4813. _RetrieveMBBPassword(profile.AccessInfo.strProfileDesc, profile.AccessInfo.strPassword);
  4814. }
  4815. if(it->apn_config.auth_mode == hw::auth_auto) {
  4816. profile.AccessInfo.usAPType = AUPR_AUTO;
  4817. } else if(it->apn_config.auth_mode == hw::auth_pap) {
  4818. profile.AccessInfo.usAPType = AUPR_PAP;
  4819. } else if(it->apn_config.auth_mode == hw::auth_chap) {
  4820. profile.AccessInfo.usAPType = AUPR_CHAP;
  4821. } else if(it->apn_config.auth_mode == hw::auth_none) {
  4822. profile.AccessInfo.usAPType = AUPR_PAPORCHAP;
  4823. }
  4824. if(it->apn_config.ip_type == hw::ipv4) {
  4825. profile.AccessInfo.strIPType = "IPv4";
  4826. } else if(it->apn_config.ip_type == hw::ipv6) {
  4827. profile.AccessInfo.strIPType = "IPv6";
  4828. } else {
  4829. profile.AccessInfo.strIPType = "IPv4v6";
  4830. }
  4831. idx++;
  4832. }
  4833. }
  4834. else {
  4835. const auto tmp = MBBResult2Status(nNum);
  4836. }
  4837. delete [] profile;
  4838. return nNum;
  4839. }
  4840. void CMobileDialFSM::_StoreMBBPassword(const CSimpleStringA& profileName, const CSimpleStringA& password)
  4841. {
  4842. CSmartPointer<IConfigInfo> spConfigRun;
  4843. GetEntityBase()->GetFunction()->OpenConfig(Config_Run, spConfigRun);
  4844. CSimpleStringA csEValue = EncryptString(password);
  4845. spConfigRun->WriteConfigValue(profileName, "Password", csEValue);
  4846. }
  4847. void CMobileDialFSM::_RetrieveMBBPassword(const CSimpleStringA& profileName, CSimpleStringA& password)
  4848. {
  4849. CSmartPointer<IConfigInfo> spConfigRun;
  4850. GetEntityBase()->GetFunction()->OpenConfig(Config_Run, spConfigRun);
  4851. CSimpleStringA csValue;
  4852. spConfigRun->ReadConfigValue(profileName, "Password", csValue);
  4853. password = DecryptString(csValue);
  4854. }
  4855. int CMobileDialFSM::_LoginMBBDevice(const MBBParams& newParam)
  4856. {
  4857. int res = 0;
  4858. bool firstLogin = false;
  4859. if(!m_HwDeviceMnt->has_login(firstLogin)) {
  4860. std::wstring username = spagent::util::string_util::s2w(newParam.loginParam.strUsername.GetData());
  4861. std::wstring password = spagent::util::string_util::s2w(newParam.loginParam.strPassword.GetData());
  4862. //Dbg("start to login: %s, %s", (LPCTSTR)newParam.loginParam.strUsername, (LPCTSTR)newParam.loginParam.strPassword);
  4863. Dbg("start to login: %s %d", (LPCTSTR)newParam.loginParam.strUsername, newParam.loginParam.strPassword.GetLength());
  4864. res = m_HwDeviceMnt->login(username, password);
  4865. const auto tmp = MBBResult2Status(res);
  4866. if(res == -3) {
  4867. Dbg("login failed: incorrect usrname or password!");
  4868. SendMsgToDialog("登录密码不正确!");
  4869. } else if(res != 0) {
  4870. Dbg("login returned %s", ConvertStatusToString(tmp));
  4871. //SetHWStatus(tmp);
  4872. m_csErrMsg = CSimpleStringA::Format("登录到MIFI设备失败(%s)!", ConvertStatusToString(tmp));
  4873. SendMsgToDialog(m_csErrMsg);
  4874. }
  4875. }
  4876. return res;
  4877. }
  4878. int CMobileDialFSM::_UpdateMBBDualParam(int configMask, MBBParams& newParam)
  4879. {
  4880. /*对于MIFI3,需要先改密码然后再更新WIFI信息,否则后面的MAC过滤地址会报9003的错误*/
  4881. if(!!(configMask & 4)) {
  4882. std::wstring username = CSimpleStringA2W(newParam.loginParam.strUsername).GetData();
  4883. std::wstring password = CSimpleStringA2W(newParam.loginParam.strPassword).GetData();
  4884. const CSimpleStringA oldPassword = newParam.loginParam.strPassword;
  4885. newParam.loginParam.strPassword = DEFAULT_PASSWORD_PREFIX;
  4886. newParam.loginParam.strPassword += m_strTerminalNo.SubString(0, 6);
  4887. std::wstring newPassword = CSimpleStringA2W(newParam.loginParam.strPassword).GetData();
  4888. int res = m_HwDeviceMnt->change_password(username, password, newPassword);
  4889. Dbg("update login password returned: %d",res);
  4890. if(res != 0) {
  4891. newParam.loginParam.strPassword = oldPassword;
  4892. configMask &= (int(~4));
  4893. const auto tmp = MBBResult2Status(res);
  4894. Dbg("change_password failed: %s", ConvertStatusToString(tmp));
  4895. return -1;
  4896. }
  4897. }
  4898. if(!!(configMask & 1)) {
  4899. hw::wlan_config_t new_wlan_config;
  4900. memset(&new_wlan_config, 0, sizeof(hw::wlan_config_t));
  4901. CSimpleStringW strUsernameW = CSimpleStringA2W(newParam.wlanConnectParam.strUsername);
  4902. CSimpleStringW strPasswordW = CSimpleStringA2W(newParam.wlanConnectParam.strPassword);
  4903. wcscpy_s(new_wlan_config.ssid, strUsernameW.GetData());
  4904. wcscpy_s(new_wlan_config.password, strPasswordW.GetData());
  4905. new_wlan_config.hidden = !!newParam.wlanConnectParam.Flag;
  4906. int save_res = m_HwDeviceMnt->set_wlan_config(new_wlan_config, true);
  4907. Dbg("update wlan config returned: %d", save_res);
  4908. if(save_res != 0) {
  4909. const auto tmp = MBBResult2Status(save_res);
  4910. Dbg("set wlan config failed: %s", ConvertStatusToString(tmp));
  4911. return -1;
  4912. }
  4913. }
  4914. if(!!(configMask & 2) || !!(configMask & 4)) {
  4915. Dbg("Write to the run config");
  4916. LOG_ASSERT(!newParam.loginParam.strUsername.IsNullOrEmpty());
  4917. LOG_ASSERT(!newParam.loginParam.strPassword.IsNullOrEmpty());
  4918. LOG_ASSERT(!newParam.wlanConnectParam.strUsername.IsNullOrEmpty());
  4919. LOG_ASSERT(!newParam.wlanConnectParam.strPassword.IsNullOrEmpty());
  4920. SaveMBBConfig(&newParam);
  4921. }
  4922. return 0;
  4923. }
  4924. MOBILEDIAL_CONNECTION_STATE CMobileDialFSM::GetMBBConnectState(const hw::network_info_t& info)
  4925. {
  4926. if(info.sim_locked) {
  4927. return MBD_READY_DEVICE_LOCKED;
  4928. } else if(info.sim_status == hw::sim_not_found) {
  4929. return MBD_READY_SIM_NO_INSERT;
  4930. } else if(info.sim_status == hw::invalid_sim) {
  4931. return MBD_READY_BAD_SIM;
  4932. }
  4933. if(info.connect_status == hw::connected) {
  4934. return MBD_ACTIVATION_CONNECTED;
  4935. } else if(info.connect_status == hw::connecting) {
  4936. return MBD_ACTIVATION_CONNECTING;
  4937. } else if(info.connect_status == hw::disconnecting) {
  4938. return MBD_ACTIVATION_DISCONNECTING;
  4939. } else if(info.connect_status == hw::disconnected) {
  4940. return MBD_ACTIVATION_DISCONNECTED;
  4941. } else if(info.connect_status == hw::connect_failed
  4942. || info.connect_status == hw::connect_failed_with_poor) {
  4943. return MBD_CONNECTION_FAILURE;
  4944. } else if(info.connect_status == hw::no_service) {
  4945. return MBD_READY_NO_SERVICE;
  4946. } else if(info.connect_status == hw::connect_dial_up_param_failed) {
  4947. return MBD_CONNECTION_FAILURE;
  4948. } else {
  4949. return MBD_ACTIVATION_NONE;
  4950. }
  4951. }
  4952. ErrorCodeEnum CMobileDialFSM::_InsertLocal2MacFilter()
  4953. {
  4954. ErrorCodeEnum ec = Error_Succeed;
  4955. if(mMBBParam.connectType == MBB_CONNECT_WIRE) {
  4956. return ec;
  4957. }
  4958. std::vector<std::string> wlanKeys;
  4959. wlanKeys.push_back("wlan");
  4960. CSmartPointer<IConfigInfo> spConfigRun;
  4961. GetEntityBase()->GetFunction()->OpenConfig(Config_Run, spConfigRun);
  4962. CSimpleStringA csWlanName;
  4963. BOOL fNeedEnum = TRUE;
  4964. std::string strNewMac;
  4965. LOG_ASSERT(!m_strTerminalNo.IsNullOrEmpty());
  4966. LOG_ASSERT(!mMBBParam.wlanConnectParam.strUsername.IsNullOrEmpty());
  4967. CSimpleStringA strTerminalxSSID = m_strTerminalNo + "x" + mMBBParam.wlanConnectParam.strUsername;
  4968. int insertOrNot = 0;
  4969. //spConfigRun->ReadConfigValueInt("MIFI", strTerminalxSSID, insertOrNot);
  4970. //if(insertOrNot > 0) {
  4971. // return Error_Succeed;
  4972. //}
  4973. spConfigRun->ReadConfigValue("MIFI", "WlanMacName", csWlanName);
  4974. if(!csWlanName.IsNullOrEmpty()) {
  4975. Dbg("add wlan name from run config: %s", csWlanName.GetData());
  4976. CAutoArray<CSimpleStringA> csMacAddress = csWlanName.Split(':');
  4977. if(csMacAddress.GetCount() == 6) {
  4978. fNeedEnum = FALSE;
  4979. strNewMac = csWlanName.GetData();
  4980. }
  4981. }
  4982. if(fNeedEnum) {
  4983. int aimIndex = -1;
  4984. network_adapters local_adapters;
  4985. const int count_of_adapters = spagent::spagent_assistor::get_network_adapters(local_adapters);
  4986. for(int i=0; i<count_of_adapters; ++i) {
  4987. std::string compare_name = local_adapters.adapters[i].friend_name;
  4988. Dbg("wlan name[%d]: %s", i, compare_name.c_str());
  4989. if(!csWlanName.IsNullOrEmpty()) {
  4990. if(csWlanName.Compare(compare_name.c_str(), true) == 0) {
  4991. aimIndex = i;
  4992. break;
  4993. }
  4994. } else {
  4995. std::string name = spagent::util::string_util::tolower(compare_name);
  4996. for(auto it=wlanKeys.cbegin(); it!=wlanKeys.cend(); ++it) {
  4997. if(name.find(*it) != std::string::npos) {
  4998. aimIndex = i;
  4999. break;
  5000. }
  5001. }
  5002. if(aimIndex != -1) {
  5003. break;
  5004. }
  5005. }
  5006. }
  5007. if(aimIndex != -1) {
  5008. strNewMac = local_adapters.adapters[aimIndex].mac;
  5009. Dbg("combine mac address: %s", strNewMac.c_str());
  5010. ec = Error_Succeed;
  5011. } else {
  5012. ec = Error_NotExist;
  5013. LogWarn(Severity_Middle, ec, CombineErrorCode(MBD_INTERNAL_ERROR),
  5014. "获取不到WLAN网卡信息,请联系信息技术人员协助处理!");
  5015. }
  5016. }
  5017. if(!strNewMac.empty()) {
  5018. Dbg("s2w....");
  5019. std::wstring wMacString = spagent::util::string_util::s2w(
  5020. spagent::util::string_util::toupper(strNewMac));
  5021. Dbg("insert mac filter....");
  5022. hw::mac_filter_t filter_info;
  5023. int res = 0;
  5024. for(int t=1; t<=5; ++t)
  5025. {
  5026. memset(&filter_info, 0, sizeof(filter_info));
  5027. wcscpy_s(filter_info.mac_filter, wMacString.c_str());
  5028. res = m_HwDeviceMnt->insert_mac_filter(filter_info);
  5029. Dbg("insert mac returned: %d", res);
  5030. if(res == 0)
  5031. break;
  5032. Sleep(300*t);
  5033. }
  5034. if(res != 0) {
  5035. //LogError(Severity_High, Error_Unexpect,
  5036. // CombineErrorCode(MBBResult2Status(res)),
  5037. // );
  5038. ShowFatalError(CSimpleStringA::Format("写入MAC白名单失败:%s", ConvertStatusToString(MBBResult2Status(res))));
  5039. } else {
  5040. spConfigRun->WriteConfigValueInt("MIFI", strTerminalxSSID, 1);
  5041. }
  5042. } else {
  5043. ShowFatalError("获取不到 WLAN 网卡信息,请联系信息技术人员协助处理!");
  5044. }
  5045. return ec;
  5046. }
  5047. void CMobileDialFSM::SetSysVal(int nFlag)
  5048. {
  5049. CSmartPointer<IEntityFunction> spFunction = this->GetEntityBase()->GetFunction();
  5050. CSimpleStringA csStatus;
  5051. CSimpleStringA csNewStatus = sysVarNetStatusEnum[NET_STATUS_UNINIT];
  5052. ErrorCodeEnum err;
  5053. err = spFunction->GetSysVar(SYSVAR_NET_STATUS, csStatus);
  5054. Dbg("Current sys val of " SYSVAR_NET_STATUS " : %s(%d)", (LPCTSTR)csStatus, err);
  5055. if(nFlag >= sizeof(sysVarNetStatusEnum)/sizeof(sysVarNetStatusEnum[0])) {
  5056. Dbg("out range of current sys var list: %d", nFlag);
  5057. } else {
  5058. csNewStatus = sysVarNetStatusEnum[nFlag];
  5059. }
  5060. if(csNewStatus != csStatus) {
  5061. err = spFunction->SetSysVar(SYSVAR_NET_STATUS, csNewStatus, true);
  5062. Dbg("SetSysVar " SYSVAR_NET_STATUS " from '%s' to '%s' returned %d",
  5063. (LPCTSTR)csStatus, (LPCTSTR)csNewStatus, err);
  5064. } else {
  5065. Dbg("system value is same with already one.");
  5066. }
  5067. return;
  5068. }
  5069. void CMobileDialFSM::ReportConnectType()
  5070. {
  5071. CSimpleStringA str(true);
  5072. MOBILEDIAL_CONNECTION_STATE state = MBD_NOT_INITIALIZED;
  5073. if(!m_bManufactureSignal) {
  5074. if(m_bPadAndLIB) {
  5075. state = MBD_CABLE_TYPE_WIRE;
  5076. } else if(m_nAdvancedGen == ADVANCE_MBB) {
  5077. state = MBD_CABLE_TYPE_MBB;
  5078. } else if(m_nAdvancedGen == ADVANCE_RAS) {
  5079. state = MBD_CABLE_TYPE_DONGLE;
  5080. } else if(g_DongleInfos.GetCount() > 0){
  5081. const int cnt = g_DongleInfos.GetCount();
  5082. for(int i=0; i<cnt; ++i) {
  5083. if(g_DongleInfos[i].state == MBD_ACTIVATION_CONNECTED) {
  5084. if(g_DongleInfos[i].usDeviceType == DEVTYPE_MODEM) {
  5085. state = MBD_CABLE_TYPE_DONGLE;
  5086. } else if(g_DongleInfos[i].usDeviceType == DEVTYPE_MBB){
  5087. state = MBD_CABLE_TYPE_MBB;
  5088. } else if(!g_DongleInfos[i].subInfo.strManufacturer.IsNullOrEmpty()) {
  5089. const CSimpleStringA& man = g_DongleInfos[i].subInfo.strManufacturer;
  5090. const CSimpleStringA& mode = g_DongleInfos[i].subInfo.strMode;
  5091. if(!man.Compare("Surface", true)
  5092. && !mode.Compare("Surface Mobile Broadband")) {
  5093. state = MBD_CABLE_TYPE_LTE;
  5094. } else if(man.Compare("huawei", true) == 0) {
  5095. state = MBD_CABLE_TYPE_DONGLE;
  5096. } else {
  5097. Dbg("Unrecognized: %s", man.GetData());
  5098. state = MBD_CABLE_TYPE_DONGLE;
  5099. }
  5100. }
  5101. }
  5102. }
  5103. } else {
  5104. // 可能会因为有线插入可联网的华为 MIFI 触发该操作 [6/30/2020 11:06 @Gifur]
  5105. Dbg("unknown connect type. don't know how just connected!");
  5106. state = MBD_CABLE_TYPE_UNKNOWN;
  5107. }
  5108. }
  5109. if(state != MBD_NOT_INITIALIZED) {
  5110. LogWarn(Severity_Low, Error_NoDefine, CombineErrorCode(state), ConvertStatusToString(state));
  5111. m_bManufactureSignal = TRUE;
  5112. }
  5113. }
  5114. BOOL CMobileDialFSM::BornValidSSIDAndPsw(CSimpleStringA& strNewSSID, CSimpleStringA& strNewPsw)
  5115. {
  5116. strNewSSID = m_strTerminalNo.GetData();
  5117. strNewSSID += "-MIFIX";
  5118. strNewPsw = DEFAULT_PASSWORD_PREFIX;
  5119. strNewPsw += m_strTerminalNo.SubString(6, m_strTerminalNo.GetLength() - 6);
  5120. return TRUE;
  5121. }
  5122. void CMobileDialFSM::MBBDeviceMonitoring()
  5123. {
  5124. LOG_FUNCTION();
  5125. if(m_fMBBDeviceMonitoring) {
  5126. Dbg("the monitoring task has been setup before.");
  5127. return;
  5128. }
  5129. m_fMBBDeviceMonitoring = TRUE;
  5130. m_fMBBDeviceMonitorStop = FALSE;
  5131. int res = 0;
  5132. int sleepTimes = 0;
  5133. volatile int sleepMaxTimes = 6;
  5134. while(m_HwDeviceMnt != NULL && !m_fMBBDeviceMonitorStop) {
  5135. sleepMaxTimes = 6;
  5136. hw::network_info_t info;
  5137. res = m_HwDeviceMnt->get_network_info(info);
  5138. if(res == 0) {
  5139. CSimpleStringA strNewDevName = CSimpleStringW2A(CSimpleStringW(info.dev_name));
  5140. if(m_curMIFIDevName.IsNullOrEmpty() || m_curMIFIDevName.Compare(strNewDevName) != 0) {
  5141. /*借来一用,反正新版多合一没戏了*/
  5142. EnterCriticalSection(&m_cs4WirlessClient);
  5143. m_curMIFIDevName = strNewDevName;
  5144. LeaveCriticalSection(&m_cs4WirlessClient);
  5145. }
  5146. if((m_nAdvancedGen != ADVANCE_MBB || mMBBParam.connectType != MBB_CONNECT_WIRE)
  5147. && !IsWifiConnected()) {
  5148. Dbg("plug in status: %d", info.usb_plug_in_status ? 1 : 0);
  5149. if(info.usb_plug_in_status) {
  5150. if(GetCurrState()->id != CMobileDialFSM::s8
  5151. && GetCurrState()->id != CMobileDialFSM::s2) {
  5152. ShowFatalError(CSimpleStringA::Format("检测到华为MIFI设备 %s 插入", m_curMIFIDevName.GetData()));
  5153. if(DetectMBBConfigUpdate(FALSE)) {
  5154. Dbg("post USER_EVT_CONFIG_DETECT event.");
  5155. FSMEvent* fsmEvent = new FSMEvent(USER_EVT_CONFIG_DETECT);
  5156. fsmEvent->param1 = 1;
  5157. PostEventFIFO(fsmEvent);
  5158. }
  5159. } else {
  5160. LogEvent(Severity_Middle, CombineErrorCode(MBD_HOLD_MOBILEDONGLE),
  5161. CSimpleStringA::Format("检测到华为MIFI: %s 设备插入", m_curMIFIDevName.GetData()));
  5162. sleepMaxTimes = 20;
  5163. }
  5164. }
  5165. } else {
  5166. if(m_mifiBattery != (int)info.battery_percent) {
  5167. Dbg("mifi's battery changed from %d to %d", m_mifiBattery, (int)info.battery_percent);
  5168. if(info.battery_percent == 0) {
  5169. LogWarn(Severity_High, Error_Hardware,
  5170. CombineErrorCode(MBD_MIFI_LOW_BATTERY), "MIFI设备电量低!");
  5171. }
  5172. m_mifiBattery = (int)info.battery_percent;
  5173. }
  5174. const MOBILEDIAL_CONNECTION_STATE current_state = GetMBBConnectState(info);
  5175. if(GetHWStatus() != current_state) {
  5176. const auto old_status = SetHWStatus(current_state);
  5177. if(current_state == MBD_ACTIVATION_CONNECTED) {
  5178. TellDlgTheResult(Error_Succeed, WM_DIAL_RESULT, ADVANCE_MBB);
  5179. FSMEvent* fsmEvent = new FSMEvent(USER_EVT_CONNECT_COMPLETE);
  5180. fsmEvent->param1 = 0;
  5181. fsmEvent->param2 = ADVANCE_MBB;
  5182. PostEventLIFO(fsmEvent);
  5183. } else if(current_state == MBD_ACTIVATION_DISCONNECTED) {
  5184. TellDlgTheResult(Error_Succeed, WM_DIAL_DISCONNECT, ADVANCE_MBB);
  5185. FSMEvent* fsmEvent = new FSMEvent(USER_EVT_DISCONNECT_COMPLETE);
  5186. fsmEvent->param1 = 0;
  5187. fsmEvent->param2 = ADVANCE_MBB;
  5188. }
  5189. }
  5190. if(current_state == MBD_ACTIVATION_CONNECTED) {
  5191. TellDlgTheResult(Error_Succeed, WM_SIGNAL_CHANGE, info.signal_strength);
  5192. const int signalVal = (int)(0.05f * info.signal_strength / 5 * 4);
  5193. if(signalVal != m_signalVal) {
  5194. Dbg("mifi's network signal changed from %d to %d", m_signalVal, signalVal);
  5195. FSMEvent* signalEvent = new FSMEvent(USER_EVT_SIGNAL);
  5196. signalEvent->param1 = signalVal;
  5197. PostEventFIFO(signalEvent);
  5198. m_signalVal = signalVal;
  5199. }
  5200. }
  5201. }
  5202. } else if(res != -12 && res != -18 && res != -10) {
  5203. LogWarn(Severity_Low, Error_Unexpect, CombineErrorCode(MBBResult2Status(res)),
  5204. CSimpleStringA::Format("Try to get MBB network infor failed: %s!",
  5205. ConvertStatusToString(MBBResult2Status(res))));
  5206. } else if(m_nAdvancedGen != ADVANCE_MBB) {
  5207. if(MBD_MIFI_START < GetHWStatus() && GetHWStatus() < MBD_MIFI_END) {
  5208. SetHWStatus(MBD_NOT_INITIALIZED);
  5209. }
  5210. }
  5211. sleepTimes = 0;
  5212. while(!m_fMBBDeviceMonitorStop && ++sleepTimes <= sleepMaxTimes) {
  5213. Sleep(1000);
  5214. }
  5215. }
  5216. Dbg("out of while %d", m_fMBBDeviceMonitorStop);
  5217. m_fMBBDeviceMonitoring = FALSE;
  5218. }
  5219. void CMobileDialFSM::BroadcastCableChangedEvent(MOBILEDIAL_CONNECTION_STATE state)
  5220. {
  5221. //let it empty at current time.
  5222. bool fDeal = true;
  5223. MOBILEDIAL_CONNECTION_STATE cur_state = state;
  5224. if(state >= MBD_CABLE_DEVICE_REMOVE && state < MBD_CABLE_DEVICE_ARRIVAL) {
  5225. cur_state = MBD_CABLE_DEVICE_REMOVE;
  5226. } else if(state >= MBD_CABLE_DEVICE_ARRIVAL && state < MBD_CABLE_DIALUP_CONNETED) {
  5227. cur_state = MBD_CABLE_DEVICE_ARRIVAL;
  5228. } else if(state >= MBD_CABLE_DIALUP_CONNETED && state < MBD_CABLE_DIALUP_DISCONNETED) {
  5229. cur_state = MBD_CABLE_DIALUP_CONNETED;
  5230. } else if(state >= MBD_CABLE_DIALUP_DISCONNETED && state <= MBD_RAS_DIALUP_DISCONNETED_IN_BUSINESS) {
  5231. cur_state = MBD_CABLE_DIALUP_DISCONNETED;
  5232. } else {
  5233. Dbg("unsupport event: %s", ConvertStatusToString(state));
  5234. fDeal = false;
  5235. }
  5236. if(fDeal) {
  5237. LogEvent(Severity_High, CombineErrorCode(cur_state), ConvertStatusToString(state));
  5238. }
  5239. }
  5240. void CMobileDialFSM::BroadcastCableChangedFSMEvent(const FSMEvent* pEvt)
  5241. {
  5242. switch(pEvt->iEvt) {
  5243. case USER_EVT_DEVICE_ARRIVAL:
  5244. {
  5245. if(pEvt->param2 == 3) {
  5246. if(pEvt->param1 == 1) {
  5247. Dbg("UserCode:0x%08X, 收到3G网卡插入的外部监听事件", CombineErrorCode(MBD_RAS_DEVICE_ARRIVAL));
  5248. //LogEvent(Severity_Middle, CombineErrorCode(MBD_RAS_DEVICE_ARRIVAL), "收到3G网卡插入的外部监听事件");
  5249. } else if(pEvt->param1 == 0) {
  5250. Dbg("UserCode:0x%08X, 收到3G网卡插入的内部处理事件", CombineErrorCode(MBD_RAS_DEVICE_ARRIVAL));
  5251. //LogEvent(Severity_Middle, CombineErrorCode(MBD_RAS_DEVICE_ARRIVAL), "收到3G网卡插入的内部处理事件");
  5252. }
  5253. BroadcastCableChangedEvent(MBD_RAS_DEVICE_ARRIVAL);
  5254. } else if(pEvt->param2 == 4) {
  5255. if(pEvt->param1 == 1) {
  5256. Dbg("UserCode:0x%08X, 收到4G网卡插入的外部监听事件", CombineErrorCode(MBD_MBN_DEVICE_ARRIVAL));
  5257. //LogEvent(Severity_Middle, CombineErrorCode(MBD_MBN_DEVICE_ARRIVAL), "收到4G网卡插入的外部监听事件");
  5258. } else if(pEvt->param1 == 0) {
  5259. Dbg("UserCode:0x%08X, 收到4G网卡插入的内部处理事件", CombineErrorCode(MBD_MBN_DEVICE_ARRIVAL));
  5260. LogEvent(Severity_Middle, CombineErrorCode(MBD_MBN_DEVICE_ARRIVAL), "收到4G网卡插入的内部处理事件");
  5261. }
  5262. BroadcastCableChangedEvent(MBD_MBN_DEVICE_ARRIVAL);
  5263. } else if(pEvt->param2 == 2) {
  5264. LogEvent(Severity_Middle, CombineErrorCode(MBD_MBB_DEVICE_ARRIVAL), "收到有线卡托插入的外部监听事件");
  5265. }
  5266. }
  5267. break;
  5268. case USER_EVT_DEVICE_LEAVE:
  5269. {
  5270. if(pEvt->param2 == 3) {
  5271. if(pEvt->param1 == 1) {
  5272. Dbg("UserCode:0x%08X, 收到3G网卡拔离的外部监听事件", CombineErrorCode(MBD_RAS_DEVICE_REMOVE));
  5273. //LogEvent(Severity_Middle, CombineErrorCode(MBD_RAS_DEVICE_REMOVE), "收到3G网卡拔离的外部监听事件");
  5274. } else if(pEvt->param1 == 0) {
  5275. Dbg("UserCode:0x%08X, 收到3G网卡拔离的内部处理事件", CombineErrorCode(MBD_RAS_DEVICE_REMOVE));
  5276. //LogEvent(Severity_Middle, CombineErrorCode(MBD_RAS_DEVICE_REMOVE), "收到3G网卡拔离的内部处理事件");
  5277. }
  5278. BroadcastCableChangedEvent(MBD_RAS_DEVICE_REMOVE);
  5279. } else if(pEvt->param2 == 4) {
  5280. if(pEvt->param1 == 1) {
  5281. Dbg("UserCode:0x%08X, 收到4G网卡拔离的外部监听事件", CombineErrorCode(MBD_MBN_DEVICE_REMOVE));
  5282. //LogEvent(Severity_Middle, CombineErrorCode(MBD_MBN_DEVICE_REMOVE), "收到4G网卡拔离的外部监听事件");
  5283. } else if(pEvt->param1 == 0) {
  5284. Dbg("UserCode:0x%08X, 收到4G网卡拔离的内部处理事件", CombineErrorCode(MBD_MBN_DEVICE_REMOVE));
  5285. //LogEvent(Severity_Middle, CombineErrorCode(MBD_MBN_DEVICE_REMOVE), "收到4G网卡拔离的内部处理事件");
  5286. }
  5287. BroadcastCableChangedEvent(MBD_MBN_DEVICE_REMOVE);
  5288. }
  5289. }
  5290. break;
  5291. case USER_EVT_CONNECT_COMPLETE:
  5292. {
  5293. Dbg("connect complement! param1(%d)::param2(%d)", pEvt->param1, pEvt->param2);
  5294. if(pEvt->param2 == ADVANCE_RAS) {
  5295. if(pEvt->param1 == 0) {
  5296. Dbg("UserCode:0x%08X, 收到3G网卡已连接的内部处理事件", CombineErrorCode(MBD_RAS_DIALUP_CONNETED));
  5297. //LogEvent(Severity_Middle,CombineErrorCode(MBD_RAS_DIALUP_CONNETED),"收到3G网卡已连接的内部处理事件");
  5298. } else if(pEvt->param1 == 1) {
  5299. Dbg("UserCode:0x%08X, 收到3G网卡已连接的外部监听事件", CombineErrorCode(MBD_RAS_DIALUP_CONNETED));
  5300. //LogEvent(Severity_Middle,CombineErrorCode(MBD_RAS_DIALUP_CONNETED),"收到3G网卡已连接的外部监听事件");
  5301. } else if(pEvt->param1 == -1) {
  5302. Dbg("UserCode:0x%08X, 收到3G网卡连接失败的外部监听事件", CombineErrorCode(MBD_RAS_DIALUP_CONNETED));
  5303. //LogEvent(Severity_Middle,CombineErrorCode(MBD_RAS_DIALUP_CONNETED),"收到3G网卡连接失败的外部监听事件");
  5304. break;
  5305. }
  5306. BroadcastCableChangedEvent(MBD_RAS_DIALUP_CONNETED);
  5307. } else if(pEvt->param2 <= 1 && pEvt->param2 >= -1) {
  5308. if(pEvt->param1 == 0) {
  5309. if(pEvt->param2) {
  5310. Dbg("UserCode:0x%08X, 收到4G网卡拨号连接的 MBN 事件", CombineErrorCode(MBD_MBN_DIALUP_CONNETED));
  5311. BroadcastCableChangedEvent(MBD_MBN_DIALUP_CONNETED);
  5312. break;
  5313. }
  5314. } else if(pEvt->param1 == -1) {
  5315. break;
  5316. }
  5317. BroadcastCableChangedEvent(MBD_CABLE_DIALUP_CONNETED);
  5318. } else {
  5319. BroadcastCableChangedEvent(MBD_CABLE_DIALUP_CONNETED);
  5320. }
  5321. }
  5322. break;
  5323. case USER_EVT_DISCONNECT_COMPLETE:
  5324. {// let the FSM(s5) do this [4/15/2020 18:59 @Gifur]
  5325. if(pEvt->param2 == ADVANCE_RAS && pEvt->param1 == 1) {
  5326. Dbg("UserCode:0x%08X, 收到3G网卡连接断开的外部监听事件", CombineErrorCode(MBD_RAS_DIALUP_DISCONNETED));
  5327. //LogEvent(Severity_Middle,CombineErrorCode(MBD_RAS_DIALUP_DISCONNETED),"收到3G网卡连接断开的外部监听事件");
  5328. //BroadcastCableChangedEvent(MBD_RAS_DIALUP_DISCONNETED);
  5329. } if(pEvt->param2 == ADVANCE_MBN) {
  5330. Dbg("UserCode:0x%08X, 收到4G网卡连接断开事件", CombineErrorCode(MBD_MBN_DIALUP_DISCONNETED));
  5331. //LogEvent(Severity_Middle,CombineErrorCode(MBD_MBN_DIALUP_DISCONNETED),"收到4G网卡连接断开事件");
  5332. BroadcastCableChangedEvent(MBD_MBN_DIALUP_DISCONNETED);
  5333. } else {
  5334. //BroadcastCableChangedEvent(MBD_CABLE_DIALUP_DISCONNETED);
  5335. }
  5336. }
  5337. break;
  5338. case EVT_USER_WIRELESS_STATE:
  5339. {
  5340. if(pEvt->param1 == 0) {
  5341. if(pEvt->param2 == 1)
  5342. //LogEvent(Severity_Middle, CombineErrorCode(MBD_WLAN_DISCONNECTED), "收到指定WIFI断开连接的事件");
  5343. LogError(Severity_Middle, Error_NetBroken, CombineErrorCode(MBD_WLAN_DISCONNECTED), "收到指定WIFI断开连接的事件");
  5344. else if(pEvt->param2 == 2)
  5345. LogWarn(Severity_Middle, Error_Unexpect, CombineErrorCode(MBD_WLAN_CONNECTED), "收到未知WIFI连接的事件");
  5346. } else if(pEvt->param1 == 1) {
  5347. LogEvent(Severity_Middle, CombineErrorCode(MBD_WLAN_CONNECTED), "收到指定WIFI连接的事件");
  5348. }
  5349. }
  5350. break;
  5351. }
  5352. }
  5353. int CMobileDialFSM::DealMBBConfigUpdate(BOOL fCheckOnly)
  5354. {
  5355. LOG_FUNCTION();
  5356. int ec = 0;
  5357. bool fFirstLogin = false;
  5358. MBBParams defaultParam;
  5359. MBBParams* paramPtr = NULL;
  5360. if(fCheckOnly) {
  5361. paramPtr = &defaultParam;
  5362. } else {
  5363. paramPtr = &mMBBParam;
  5364. }
  5365. // 1: write to device,
  5366. // 2: update from device
  5367. // 3: both save and need to update the login password.
  5368. int nNeedConfig = 0;
  5369. if(fCheckOnly || m_HwDeviceMnt->detect_devices()) {
  5370. GetMBBConfig(paramPtr);
  5371. bool fEmptyParam = false;
  5372. if(paramPtr->loginParam.strUsername.IsNullOrEmpty() || paramPtr->loginParam.strPassword.IsNullOrEmpty()) {
  5373. paramPtr->loginParam.strUsername = DEFAULT_MIFI_LOGIN_USERNAME;
  5374. paramPtr->loginParam.strPassword = DEFAULT_PASSWORD_PREFIX;
  5375. LOG_ASSERT(!m_strTerminalNo.IsNullOrEmpty());
  5376. paramPtr->loginParam.strPassword += m_strTerminalNo.SubString(0, 6);
  5377. Dbg("default password.");
  5378. fEmptyParam = true;
  5379. bool firstLogin;
  5380. LOG_ASSERT(!m_HwDeviceMnt->has_login(firstLogin));
  5381. }
  5382. int res = 0;
  5383. res = _LoginMBBDevice(*paramPtr);
  5384. if(res == -10) {
  5385. Dbg("detect it's invalid certificate.");
  5386. Sleep(100);
  5387. res = _LoginMBBDevice(*paramPtr);
  5388. }
  5389. if(res == -3) {
  5390. if(!IsMIFI3()) {
  5391. paramPtr->loginParam.strPassword = DEFAULT_MIFI_LOGIN_PASSWORD;
  5392. res = _LoginMBBDevice(*paramPtr);
  5393. //if(res == -3) {
  5394. // Dbg("use backup default password.");
  5395. // paramPtr->loginParam.strPassword = DEFAULT_MIFI_LOGIN_PASSWORD_BACKUP;
  5396. // res = _LoginMBBDevice(*paramPtr);
  5397. //}
  5398. } else {
  5399. // 添加对华为MIFI3初始化的处理办法 [7/23/2020 14:49 @Gifur]
  5400. if(fCheckOnly) {
  5401. Dbg("detect mifi3 and login failed, check flag return 1");
  5402. return 1;
  5403. } else {
  5404. Dbg("open CMIFIPswDlg...");
  5405. CMIFIPswDlg dlgMain;
  5406. int nRet = dlgMain.DoModal();
  5407. Dbg("open CMIFIPswDlg return: %d %d", nRet, dlgMain.m_strLoginPassword.GetLength());
  5408. if(nRet > 0 && dlgMain.m_strLoginPassword.GetLength() != 0) {
  5409. paramPtr->loginParam.strPassword = (LPCTSTR)dlgMain.m_strLoginPassword;
  5410. Dbg("user input password: %s", paramPtr->loginParam.strPassword.GetData());
  5411. res = _LoginMBBDevice(*paramPtr);
  5412. }
  5413. }
  5414. }
  5415. if(res == 0) {
  5416. fFirstLogin = true;
  5417. }
  5418. }
  5419. if(res == 0) {
  5420. if(!fEmptyParam && !fFirstLogin && paramPtr->customSet) {
  5421. /*如果是手工添加的配置内容,不做更新操作*/
  5422. Dbg("stop from updating mifi config due to customized configure.");
  5423. return 6;
  5424. }
  5425. hw::wlan_config_t wlan_config;
  5426. res = m_HwDeviceMnt->get_wlan_config(wlan_config);
  5427. if(res == 0) {
  5428. CSimpleStringA strSSID(CSimpleStringW2A(wlan_config.ssid));
  5429. CSimpleStringA strPassword(CSimpleStringW2A(wlan_config.password));
  5430. BOOL fvalidSSID = FALSE;
  5431. const BOOL fPswEmpty = strPassword.IsNullOrEmpty();
  5432. if(strSSID.IndexOf("-") == 10) {//TODO: 判断名称是否符合要求
  5433. fvalidSSID = TRUE;
  5434. CSimpleStringA strThirdTerminalNo = strSSID.SubString(0, 10);
  5435. LogEvent(Severity_Low, CombineErrorCode(MBD_HOLD_MOBILEDONGLE),
  5436. CSimpleStringA::Format("the terminal sn in ssid: %s", strThirdTerminalNo.GetData()));
  5437. if(fPswEmpty) {
  5438. Dbg("wlan password is empty.");
  5439. CSimpleStringA strNewPassword(DEFAULT_PASSWORD_PREFIX);
  5440. strNewPassword += strThirdTerminalNo.SubString(6, strThirdTerminalNo.GetLength() - 6);
  5441. strPassword = strNewPassword;
  5442. Dbg("combile the existed password @1");
  5443. //Dbg("combile the existed password: %s", strPassword.GetData());
  5444. }
  5445. }
  5446. if(strSSID.IsStartWith(m_strTerminalNo.GetData())) {
  5447. //自家的设备
  5448. if(strPassword.IsNullOrEmpty()) {
  5449. Dbg("wlan password is empty.");
  5450. CSimpleStringA strNewPassword(DEFAULT_PASSWORD_PREFIX);
  5451. strNewPassword += m_strTerminalNo.SubString(6, m_strTerminalNo.GetLength() - 6);
  5452. strPassword = strNewPassword;
  5453. Dbg("combile the existed password @2");
  5454. //Dbg("combile the existed password: %s", strPassword.GetData());
  5455. }
  5456. if((paramPtr->wlanConnectParam.strUsername.Compare(strSSID) != 0
  5457. || paramPtr->wlanConnectParam.strPassword.Compare(strPassword) != 0
  5458. || wlan_config.hidden != (!!paramPtr->wlanConnectParam.Flag))) {
  5459. //从设备中写进来
  5460. paramPtr->wlanConnectParam.strUsername = strSSID;
  5461. paramPtr->wlanConnectParam.strPassword = strPassword;
  5462. paramPtr->wlanConnectParam.Flag = wlan_config.hidden ? TRUE : FALSE;
  5463. paramPtr->connectType = MBB_CONNECT_WIFI;
  5464. nNeedConfig = 2;
  5465. }
  5466. } else if(fvalidSSID) {
  5467. //他家的设备
  5468. Dbg("the third terminal sn.");
  5469. if(strSSID.Compare(paramPtr->wlanConnectParam.strUsername) != 0
  5470. || strPassword.Compare(paramPtr->wlanConnectParam.strPassword) != 0
  5471. || wlan_config.hidden != (!!paramPtr->wlanConnectParam.Flag)) {
  5472. paramPtr->wlanConnectParam.strUsername = strSSID;
  5473. paramPtr->wlanConnectParam.strPassword = strPassword;
  5474. paramPtr->wlanConnectParam.Flag = wlan_config.hidden ? TRUE : FALSE;
  5475. paramPtr->connectType = MBB_CONNECT_WIFI;
  5476. nNeedConfig = 2;
  5477. }
  5478. if(fPswEmpty) {
  5479. LogWarn(Severity_Middle, Error_Unexpect, CombineErrorCode(MBD_MIFI_NO_DATA),
  5480. "尝试获取WLAN密码失败,该设备不支持共享使用!");
  5481. }
  5482. } else {
  5483. //新的设备(有可能)
  5484. CSimpleStringA strNewSSID(m_strTerminalNo.GetData());
  5485. CSimpleStringA strNewPassword(DEFAULT_PASSWORD_PREFIX);
  5486. strNewPassword += m_strTerminalNo.SubString(6, m_strTerminalNo.GetLength() - 6);
  5487. hw::dev_info_t dev_info;
  5488. int get_dev_res = m_HwDeviceMnt->get_dev_info(dev_info);
  5489. if(get_dev_res == 0) {
  5490. CSimpleStringA strDevName(CSimpleStringW2A(dev_info.dev_name));
  5491. const int index_dev = strDevName.IndexOf("-");
  5492. strNewSSID += "-";
  5493. if(index_dev != -1) {
  5494. strNewSSID += strDevName.SubString(0, index_dev);
  5495. } else {
  5496. strNewSSID += strDevName;
  5497. }
  5498. } else {
  5499. Dbg("get dev info faiiled: %d", get_dev_res);
  5500. strNewSSID += "-MIFI";
  5501. }
  5502. paramPtr->wlanConnectParam.strUsername = strNewSSID;
  5503. paramPtr->wlanConnectParam.strPassword = strNewPassword;
  5504. paramPtr->wlanConnectParam.Flag = TRUE;
  5505. Dbg("new ssid name: %s", paramPtr->wlanConnectParam.strUsername.GetData());
  5506. //Dbg("new password: %s", paramPtr->wlanConnectParam.strPassword.GetData());
  5507. paramPtr->connectType = MBB_CONNECT_WIFI;
  5508. nNeedConfig = 3;
  5509. }
  5510. if((nNeedConfig & 2) == 0 && fEmptyParam) {
  5511. nNeedConfig |= 2;
  5512. }
  5513. if(fFirstLogin) {
  5514. //密码要更新
  5515. nNeedConfig |= 4;
  5516. }
  5517. //////////////////////////////////////////////////////////////////////////
  5518. if(!!nNeedConfig) {
  5519. ec = 1;
  5520. }
  5521. if(!fCheckOnly) {
  5522. if(0 != _UpdateMBBDualParam(nNeedConfig, *paramPtr)) {
  5523. ec = 4;
  5524. } else if(_InsertLocal2MacFilter() != Error_Succeed){
  5525. //MIFI3必须在更改了密码之后才能插入过滤白名单
  5526. //Always return Succeed.
  5527. ec = 6;
  5528. }
  5529. } else {
  5530. Dbg("only check this time.");
  5531. if(fFirstLogin || fEmptyParam) {
  5532. Dbg("logout returned: %d.", m_HwDeviceMnt->logout());
  5533. }
  5534. }
  5535. } else {
  5536. Dbg("get wlan config failed: %d", res);
  5537. ec = 2;
  5538. }
  5539. } else {
  5540. Dbg("login failed: %d", res);
  5541. if(-17 == res) {
  5542. //LogWarn(Severity_Middle, Error_Unexpect, CombineErrorCode(MBBResult2Status(res)),
  5543. // "尝试登录MIFI失败次数超限,请拔离设备10分钟再试");
  5544. ShowFatalError("尝试登录MIFI失败次数超限,请拔离设备10分钟后再试,或直接重置设备!");
  5545. Sleep(10000);
  5546. } else {
  5547. LogWarn(Severity_Middle, Error_Unexpect, CombineErrorCode(MBBResult2Status(res)),
  5548. CSimpleStringA::Format("尝试登录到 MIFI 设备失败(%s)!", ConvertStatusToString(MBBResult2Status(res))));
  5549. }
  5550. //SetHWStatus(MBBResult2Status(res));
  5551. if(res == -3) {
  5552. //SetHWStatus(MBD_MIFI_LOGIN_INVALID_NAMEORPASSWORD);
  5553. //ShowFatalError(GetFurtherDetail());
  5554. //SendNetStatusMsg();
  5555. ec = 5;
  5556. } else {
  5557. ec = 3;
  5558. }
  5559. }
  5560. } else {
  5561. Dbg("no detect device");
  5562. ec = 4;
  5563. }
  5564. return ec;
  5565. }
  5566. BOOL CMobileDialFSM::DetectMBBConfigUpdate(BOOL fNeedCheck)
  5567. {
  5568. static BOOL fFirstFlag = TRUE;
  5569. if((m_nAdvancedGen != ADVANCE_MBB || mMBBParam.connectType != MBB_CONNECT_WIRE) && !IsWifiConnected()) {
  5570. if(!fNeedCheck || m_HwDeviceMnt->detect_devices()) {
  5571. int res = 1;
  5572. if(fFirstFlag && (res = DealMBBConfigUpdate(TRUE)) == 1) {
  5573. return TRUE;
  5574. } else if(fFirstFlag && res == 0) {
  5575. fFirstFlag = FALSE;
  5576. }
  5577. } else if(!fFirstFlag) {
  5578. fFirstFlag = TRUE;
  5579. }
  5580. }
  5581. return FALSE;
  5582. }
  5583. void CMobileDialFSM::ForbidAutoConnnectBySystem()
  5584. {
  5585. LOG_FUNCTION();
  5586. HKEY hKey = NULL;
  5587. HKEY hSubKey = NULL;
  5588. HKEY hSvcKey = NULL;
  5589. BOOL fNeedCheck = FALSE;
  5590. BOOL fEnableSet = FALSE;
  5591. DWORD dwFlag = KEY_READ | KEY_QUERY_VALUE | KEY_ENUMERATE_SUB_KEYS;
  5592. if (gArch == 64/*Win64Bit*/) {
  5593. dwFlag |= KEY_WOW64_64KEY;
  5594. } else {
  5595. dwFlag |= KEY_WOW64_32KEY;
  5596. }
  5597. LONG lResult = RegOpenKeyEx(HKEY_LOCAL_MACHINE, TEXT("SOFTWARE\\Policies\\Microsoft\\Windows"),
  5598. 0, dwFlag, &hKey);
  5599. if(lResult == ERROR_SUCCESS) {
  5600. lResult = RegOpenKeyEx(hKey, "WwanSvc\\GroupPolicy", 0, dwFlag, &hSubKey);
  5601. if(lResult == ERROR_FILE_NOT_FOUND) {
  5602. DWORD dwDisposition = REG_OPENED_EXISTING_KEY;
  5603. Dbg("Start to create \"WwanSvc\\GroupPolicy\" key...");
  5604. lResult = RegCreateKeyEx(hKey, "WwanSvc\\GroupPolicy",
  5605. 0, NULL,REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, NULL, &hSubKey, &dwDisposition);
  5606. Dbg("Create it return %d", lResult);
  5607. }
  5608. if(lResult == ERROR_SUCCESS) {
  5609. DWORD dwType = REG_DWORD;
  5610. DWORD dwValue = 0;
  5611. DWORD dwSize = sizeof(dwValue);
  5612. lResult = RegQueryValueEx(hSubKey, "DisableWwanAutoConnect", NULL, &dwType, (LPBYTE)&dwValue, &dwSize);
  5613. if(lResult == ERROR_FILE_NOT_FOUND || (lResult == ERROR_SUCCESS && dwValue == 0)) {
  5614. fNeedCheck = TRUE;
  5615. } else {
  5616. Dbg("needless to check: result:%d, value:%d", lResult, dwValue);
  5617. }
  5618. }
  5619. } else {
  5620. Dbg("RegOpenKeyEx \"SOFTWARE\\Policies\\Microsoft\\Windows\" failed %d", lResult);
  5621. }
  5622. if(fNeedCheck) {
  5623. lResult = RegOpenKeyEx(HKEY_LOCAL_MACHINE, TEXT("SOFTWARE\\Microsoft\\WwanSvc\\DisallowAutoConnectByClient"), 0, dwFlag, &hSvcKey);
  5624. if(lResult == ERROR_FILE_NOT_FOUND) {
  5625. fEnableSet = TRUE;
  5626. } else if(lResult == ERROR_SUCCESS) {
  5627. DWORD dwClass = 0;
  5628. DWORD dwCount = 0, dwSubKeys = 0;
  5629. lResult = RegQueryInfoKey(hSvcKey, NULL, &dwClass, NULL, &dwSubKeys, NULL, NULL, &dwCount, NULL, NULL, NULL, NULL);
  5630. if(lResult != ERROR_SUCCESS) {
  5631. fEnableSet = FALSE;
  5632. }
  5633. else if(dwCount > 0) {
  5634. Dbg("Number of subValue: %d", dwCount);
  5635. char szValue[128];
  5636. DWORD dwLen = sizeof(szValue);
  5637. DWORD dwValue = 0;
  5638. DWORD dwSize = sizeof(dwValue);
  5639. DWORD dwType = REG_DWORD;
  5640. for(int i=0; lResult == ERROR_SUCCESS && i<(int)dwCount; ++i) {
  5641. memset(szValue, 0, sizeof(szValue));
  5642. lResult = RegEnumValue(hSvcKey, i, szValue, &dwLen, NULL, &dwType, (LPBYTE)&dwValue, &dwSize);
  5643. if(lResult == ERROR_SUCCESS) {
  5644. Dbg("RegEnumValue: %s - %d", szValue, dwValue);
  5645. if(dwValue > 0) {
  5646. Dbg("Because...");
  5647. fEnableSet = FALSE;
  5648. }
  5649. }
  5650. }
  5651. } else {
  5652. fEnableSet = TRUE;
  5653. }
  5654. }
  5655. }
  5656. if(fEnableSet) {
  5657. DWORD dwType = REG_DWORD;
  5658. DWORD dwValue = 1;
  5659. DWORD dwSize = sizeof(dwValue);
  5660. lResult = RegSetValueEx(hSubKey, "DisableWwanAutoConnect", 0, REG_DWORD, (const BYTE*)&dwValue, dwSize);
  5661. if(lResult == ERROR_SUCCESS) {
  5662. Dbg("RegSetValueEx for DisableWwanAutoConnect succ.");
  5663. } else {
  5664. Dbg("RegSetValueEx for DisableWwanAutoConnect failed, %d", lResult);
  5665. }
  5666. }
  5667. RegCloseKey(hKey);
  5668. RegCloseKey(hSvcKey);
  5669. RegCloseKey(hSubKey);
  5670. }
  5671. const MOBILEDIAL_CONNECTION_STATE& CMobileDialFSM::GetRasStatus() const
  5672. {
  5673. return m_3gState;
  5674. }
  5675. BOOL CMobileDialFSM::RasHangUpSuffix(BOOL bOrigin)
  5676. {
  5677. LOG_FUNCTION();
  5678. BOOL fResult = TRUE;
  5679. if(g_hrasconn != NULL) {
  5680. DWORD dwRet = 0;
  5681. dwRet = RasHangUp(g_hrasconn);
  5682. Dbg("RasHangUp in DisConnect returned %d", dwRet);
  5683. if(dwRet == ERROR_SUCCESS) {
  5684. RASCONNSTATUS connStatus;
  5685. connStatus.dwSize = sizeof(RASCONNSTATUS);
  5686. Dbg("TickCount allocated 3 seconds after RasHangUp...");
  5687. //Reduce timeout tick in case occuring
  5688. //UnLoading-Entity -Josephus@2017627 16:54:10
  5689. DWORD tcLast = GetTickCount() + 3 * 1100;
  5690. while((( dwRet = RasGetConnectStatus(g_hrasconn, &connStatus))
  5691. != ERROR_INVALID_HANDLE) && (tcLast > GetTickCount())) {
  5692. Sleep(0);
  5693. }
  5694. fResult = (dwRet == ERROR_INVALID_HANDLE);
  5695. Dbg("Cycle is done %snormally", fResult ? "" : "ab");
  5696. }
  5697. else {
  5698. if(dwRet == ERROR_INVALID_HANDLE) {
  5699. Dbg("to RasHandup: The handle specified in RashandUp is invalid.");
  5700. }
  5701. else if(dwRet == ERROR_NO_CONNECTION) {
  5702. Dbg("to RasHandup: The connection was dropped.");
  5703. }
  5704. else {
  5705. Dbg("Invoke RasHangUp failed returned: %d", dwRet);
  5706. }
  5707. fResult = FALSE;
  5708. }
  5709. g_hrasconn = NULL;
  5710. } else {
  5711. m_csErrMsg = "RAS句柄不存在";
  5712. fResult = FALSE;
  5713. }
  5714. return fResult;
  5715. }
  5716. int CMobileDialFSM::EnumCurRasDevices(BOOL bUpdate)
  5717. {
  5718. if(m_nAdvancedGen == 4 || m_nAdvancedGen == 2) {
  5719. return 0;
  5720. }
  5721. LOG_FUNCTION();
  5722. Dbg("bUpdate: %d", bUpdate);
  5723. int nRes = 0;
  5724. DWORD dwCb = 0;
  5725. DWORD dwRet = ERROR_SUCCESS;
  5726. DWORD dwDevices = 0;
  5727. LPRASDEVINFO lpRasDevInfo = NULL;
  5728. dwRet = RasEnumDevices(lpRasDevInfo, &dwCb, &dwDevices);
  5729. if (dwRet == ERROR_BUFFER_TOO_SMALL) {
  5730. lpRasDevInfo = (LPRASDEVINFO)HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, dwCb);
  5731. if (lpRasDevInfo == NULL) {
  5732. Dbg("HeapAlloc failed when enuming ras devices.");
  5733. SetRasStatus(MBD_INTERNAL_ERROR);
  5734. return -1;
  5735. }
  5736. lpRasDevInfo[0].dwSize = sizeof(RASDEVINFO);
  5737. dwRet = RasEnumDevices(lpRasDevInfo, &dwCb, &dwDevices);
  5738. if (ERROR_SUCCESS == dwRet) {
  5739. for (DWORD i = 0; i < dwDevices; i++) {
  5740. if(!_strnicmp(lpRasDevInfo[i].szDeviceType, RASDT_Modem, strlen(RASDT_Modem))) {
  5741. nRes++;
  5742. if(!bUpdate) {
  5743. Dbg("%s <%s>",lpRasDevInfo[i].szDeviceName,lpRasDevInfo[i].szDeviceType);
  5744. }
  5745. }
  5746. }
  5747. Dbg("The above RAS devices were found(%d)", nRes);
  5748. if(nRes > 0) {
  5749. if(bUpdate) {
  5750. int nEntryNum = RefreshRasEntry();
  5751. Dbg("nEntryNum(%d) vs Count(%d)", nEntryNum, m_rasEntries.GetCount());
  5752. DongleList donglist;
  5753. donglist.Init(nRes);
  5754. Dbg("Dream device count: %d", nRes);
  5755. bool fHasCfg = false;
  5756. DWORD k = 0;
  5757. for (DWORD i = 0; i < dwDevices; i++) {
  5758. if(!_strnicmp(lpRasDevInfo[i].szDeviceType, RASDT_Modem, strlen(RASDT_Modem))) {
  5759. Dbg("%s <%s>",lpRasDevInfo[i].szDeviceName,lpRasDevInfo[i].szDeviceType);
  5760. memset(&donglist[k], 0, sizeof(MbnDongleInfo));
  5761. donglist[k].usDeviceType = DEVTYPE_MODEM;
  5762. //TODO:
  5763. donglist[k].csInterfaceId = lpRasDevInfo[i].szDeviceName;
  5764. InitMbnDongleSubInfo(&donglist[k].subInfo);
  5765. donglist[k].subInfo.ulSingnal = 0xFFFFFFFF;
  5766. donglist[k].subInfo.strProviderName = lpRasDevInfo[i].szDeviceName;
  5767. donglist[k].subInfo.mbnCntState = MBN_ACTIVATION_STATE_DEACTIVATED;
  5768. donglist[k].subInfo.strProfileName = "";
  5769. donglist[k].state = /*MBD_NOT_INITIALIZED*/MBD_ACTIVATION_DISCONNECTED;
  5770. int num = 0;
  5771. for(int j=0; j<nEntryNum; ++j) {
  5772. if(!strcmp(lpRasDevInfo[i].szDeviceName, m_rasEntries[j].szDeviceName)) {
  5773. num++;
  5774. }
  5775. }
  5776. if(num > 0) {
  5777. donglist[k].profileList.Init(num);
  5778. for(int j=0, z=0; j<nEntryNum; ++j) {
  5779. if(!strcmp(lpRasDevInfo[i].szDeviceName, m_rasEntries[j].szDeviceName)) {
  5780. _EntriesToDongle(m_rasEntries[j], &donglist[k].profileList[z]);
  5781. z++;
  5782. }
  5783. }
  5784. fHasCfg = true;
  5785. } else {
  5786. donglist[k].state = MBD_NO_ENTRY;
  5787. }
  5788. k++;
  5789. }
  5790. }
  5791. LOG_ASSERT(k == nRes);
  5792. g_DongleInfos.Append(donglist);
  5793. if(fHasCfg) {
  5794. UpdateRasConnectState();
  5795. } else {
  5796. SetRasStatus(MBD_NO_ENTRY);
  5797. }
  5798. }
  5799. } else {
  5800. SetRasStatus(MBD_NO_MOBILEDONGLE);
  5801. }
  5802. } else {
  5803. SetRasStatus(MBD_INTERNAL_ERROR);
  5804. Dbg("RasEnumDevices failed.");
  5805. HeapFree(GetProcessHeap(), 0, lpRasDevInfo);
  5806. lpRasDevInfo = NULL;
  5807. return -1;
  5808. }
  5809. HeapFree(GetProcessHeap(), 0, lpRasDevInfo);
  5810. lpRasDevInfo = NULL;
  5811. return nRes;
  5812. }
  5813. if (dwDevices >= 1)
  5814. {
  5815. Dbg("The operation failed to acquire the buffer size.");
  5816. SetRasStatus(MBD_INTERNAL_ERROR);
  5817. return -1;
  5818. }
  5819. Dbg("There were no RAS devices found.");
  5820. SetRasStatus(MBD_NO_MOBILEDONGLE);
  5821. return 0;
  5822. }
  5823. BOOL CMobileDialFSM::Init3GComponent()
  5824. {
  5825. bool bContinue = true;
  5826. for(int i=0; i<RasEventCount; ++i)
  5827. {
  5828. if(g_RasEvents[i] == NULL) {
  5829. if((g_RasEvents[i] = CreateEventA(NULL, FALSE, FALSE, NULL)) == NULL){
  5830. Dbg("CreateEvent[%d] failed with error %u.", i, GetLastError());
  5831. bContinue = false;
  5832. }
  5833. }
  5834. }
  5835. if(!bContinue) {
  5836. Dbg("Cancel to create listern thread");
  5837. return FALSE;
  5838. }
  5839. if(m_hRasListenThread != NULL) {
  5840. TerminateThread(m_hRasListenThread, -1);
  5841. CloseHandle(m_hRasListenThread);
  5842. m_hRasListenThread = NULL;
  5843. }
  5844. m_hRasListenThread = (HANDLE)_beginthreadex(NULL, 0, RasEventThreadFunc, (void*)this, 0, NULL);
  5845. if(m_hRasListenThread == INVALID_HANDLE_VALUE || m_hRasListenThread == NULL) {
  5846. LogError(Severity_None, Error_Unexpect, 0, "Create thread for 3G RasDial disconnect failed !");
  5847. m_hRasListenThread = NULL;
  5848. return FALSE;
  5849. }
  5850. DWORD dwRet = RasConnectionNotification((HRASCONN)INVALID_HANDLE_VALUE, g_RasEvents[1], RASCN_Disconnection);
  5851. if(dwRet == ERROR_SUCCESS) {
  5852. Dbg("RasConnectionNotification for disconnect succ.");
  5853. } else {
  5854. Dbg("RasConnectionNotification for disconnect failed : %d", dwRet);
  5855. }
  5856. dwRet = RasConnectionNotification((HRASCONN)INVALID_HANDLE_VALUE,
  5857. g_RasEvents[0], RASCN_Connection);
  5858. if(dwRet == ERROR_SUCCESS) {
  5859. Dbg("RasConnectionNotification for connect succ.");
  5860. } else {
  5861. Dbg("RasConnectionNotification for connect failed : %d", dwRet);
  5862. }
  5863. dwRet = RasConnectionNotification((HRASCONN)INVALID_HANDLE_VALUE,
  5864. g_RasEvents[3], RASCN_BandwidthAdded);
  5865. if(dwRet == ERROR_SUCCESS) {
  5866. Dbg("RasConnectionNotification for add succ.");
  5867. } else {
  5868. Dbg("RasConnectionNotification for add failed : %d", dwRet);
  5869. }
  5870. dwRet = RasConnectionNotification((HRASCONN)INVALID_HANDLE_VALUE,
  5871. g_RasEvents[4], RASCN_BandwidthRemoved);
  5872. if(dwRet == ERROR_SUCCESS) {
  5873. Dbg("RasConnectionNotification for remove succ.");
  5874. } else {
  5875. Dbg("RasConnectionNotification for remove failed : %d", dwRet);
  5876. }
  5877. return TRUE;
  5878. }
  5879. BOOL CMobileDialFSM::Init4GComponent()
  5880. {
  5881. //ForbidAutoConnnectBySystem();
  5882. if(m_pMbn) {
  5883. m_pMbn->AdviseCntMnrEvent();
  5884. m_pMbn->AdviseConnectEvent();
  5885. m_pMbn->AdviseSignalEvent();
  5886. m_pMbn->AdviseCntProfileMnrEvent();
  5887. m_pMbn->AdviseCntProfileEvent();
  5888. }
  5889. return TRUE;
  5890. }
  5891. BOOL CMobileDialFSM::InitHuaweiComponent()
  5892. {
  5893. GetMBBConfig(&mMBBParam);
  5894. Dbg("connect type: %d", mMBBParam.connectType);
  5895. Dbg("username: %s", (LPCTSTR)mMBBParam.loginParam.strUsername);
  5896. Dbg("ssid: %s", (LPCTSTR)mMBBParam.wlanConnectParam.strUsername);
  5897. if(m_WifiChangedEvt == NULL) {
  5898. spagent::spagent_assistor::init("mobile");
  5899. m_WifiChangedEvt = new WifiChangeEvent(this);
  5900. if(mMBBParam.connectType == MBB_CONNECT_WIRE) {
  5901. spagent::spagent_assistor::disconnect(conn_tool_wifi, true);
  5902. }
  5903. }
  5904. if(!m_WifiChangedEvt->m_bRegistered) {
  5905. int rc = spagent::spagent_assistor::register_notification_events(conn_tool_wifi, m_WifiChangedEvt);
  5906. Dbg("Register wifi changed event return: %d", rc);
  5907. m_WifiChangedEvt->m_bRegistered = rc == 0 ? TRUE : FALSE;
  5908. }
  5909. return TRUE;
  5910. }
  5911. BOOL CMobileDialFSM::IsWifiConnected()
  5912. {
  5913. std::string ssid;
  5914. return spagent::spagent_assistor::is_connected(conn_tool_wifi, ssid) ? TRUE : FALSE;
  5915. }
  5916. void CMobileDialFSM::_FormatRasError(DWORD dwRasCode)
  5917. {
  5918. DWORD cBufSize = 256;
  5919. char lpszBuf[256] = {0};
  5920. DWORD dwRetVal = RasGetErrorString((UINT)dwRasCode, lpszBuf, cBufSize);
  5921. if(dwRetVal == ERROR_SUCCESS) {
  5922. LogError(Severity_Middle, Error_Unrecover, CombineErrorCode(MBD_RASDIAL_FAILED),
  5923. CSimpleStringA::Format("错误 %d:%s", dwRasCode, lpszBuf));
  5924. m_csErrMsg = CSimpleStringA::Format("%s : %d", lpszBuf, dwRasCode);
  5925. }else {
  5926. m_csErrMsg = CSimpleStringA::Format("RasDial failed: %u", dwRasCode);
  5927. LogError(Severity_Middle, Error_Unrecover, CombineErrorCode(MBD_RASDIAL_FAILED), CSimpleStringA::Format("3G拨号失败:%u", dwRasCode));
  5928. }
  5929. }
  5930. int CMobileDialFSM::CheckNetSituationWithPing()
  5931. {
  5932. int nRes = 0;
  5933. if((ping1 == NULL || !ping1->IsValid()) && (ping2 == NULL || !ping2->IsValid()))
  5934. return -1;
  5935. int t1 = 0, t2 = 0;
  5936. const int max_times = 4;
  5937. if(ping1 != NULL && ping1->IsValid())
  5938. {
  5939. for(t1=0; t1<max_times; ++t1)
  5940. {
  5941. if(ping1->PingOnce() == 0)
  5942. {
  5943. nRes += 1;
  5944. break;
  5945. }
  5946. Sleep(1000);
  5947. }
  5948. }
  5949. if(ping2 != NULL && ping2->IsValid())
  5950. {
  5951. for(t2=0; t2<max_times; ++t2)
  5952. {
  5953. Sleep(1000);
  5954. if(ping2->PingOnce() == 0)
  5955. {
  5956. nRes += 2;
  5957. break;
  5958. }
  5959. }
  5960. }
  5961. return nRes;
  5962. }