chan_unistim.c 212 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206
  1. /*
  2. * Asterisk -- An open source telephony toolkit.
  3. *
  4. * UNISTIM channel driver for asterisk
  5. *
  6. * Copyright (C) 2005 - 2007, Cedric Hans
  7. *
  8. * Cedric Hans <cedric.hans@mlkj.net>
  9. *
  10. * Asterisk 1.4 patch by Peter Be
  11. *
  12. * See http://www.asterisk.org for more information about
  13. * the Asterisk project. Please do not directly contact
  14. * any of the maintainers of this project for assistance;
  15. * the project provides a web site, mailing lists and IRC
  16. * channels for your use.
  17. *
  18. * This program is free software, distributed under the terms of
  19. * the GNU General Public License Version 2. See the LICENSE file
  20. * at the top of the source tree.
  21. */
  22. /*!
  23. * \file
  24. *
  25. * \brief chan_unistim channel driver for Asterisk
  26. * \author Cedric Hans <cedric.hans@mlkj.net>
  27. *
  28. * Unistim (Unified Networks IP Stimulus) channel driver
  29. * for Nortel i2002, i2004 and i2050
  30. *
  31. * \ingroup channel_drivers
  32. */
  33. /*** MODULEINFO
  34. <support_level>extended</support_level>
  35. ***/
  36. #include "asterisk.h"
  37. #include <sys/stat.h>
  38. #include <signal.h>
  39. #if defined(__CYGWIN__) || defined(__NetBSD__)
  40. /*
  41. * cygwin headers are partly inconsistent. struct iovec is defined in sys/uio.h
  42. * which is not included by default by sys/socket.h - in_pktinfo is defined in
  43. * w32api/ws2tcpip.h but this probably has compatibility problems with sys/socket.h
  44. * So for the time being we simply disable HAVE_PKTINFO when building under cygwin.
  45. * This should be done in some common header, but for now this is the only file
  46. * using iovec and in_pktinfo so it suffices to apply the fix here.
  47. */
  48. #ifdef HAVE_PKTINFO
  49. #undef HAVE_PKTINFO
  50. #endif
  51. #endif /* __CYGWIN__ || __NetBSD__ */
  52. #include "asterisk/paths.h" /* ast_config_AST_LOG_DIR used in (too ?) many places */
  53. #include "asterisk/network.h"
  54. #include "asterisk/channel.h"
  55. #include "asterisk/config.h"
  56. #include "asterisk/module.h"
  57. #include "asterisk/pbx.h"
  58. #include "asterisk/rtp_engine.h"
  59. #include "asterisk/unaligned.h"
  60. #include "asterisk/netsock2.h"
  61. #include "asterisk/acl.h"
  62. #include "asterisk/callerid.h"
  63. #include "asterisk/cli.h"
  64. #include "asterisk/app.h"
  65. #include "asterisk/mwi.h"
  66. #include "asterisk/musiconhold.h"
  67. #include "asterisk/causes.h"
  68. #include "asterisk/indications.h"
  69. #include "asterisk/pickup.h"
  70. #include "asterisk/astobj2.h"
  71. #include "asterisk/astdb.h"
  72. #include "asterisk/features_config.h"
  73. #include "asterisk/bridge.h"
  74. #include "asterisk/stasis_channels.h"
  75. #include "asterisk/format_cache.h"
  76. #define DEFAULTCONTEXT "default"
  77. #define DEFAULTCALLERID "Unknown"
  78. #define DEFAULTCALLERNAME " "
  79. #define DEFAULTHEIGHT 3
  80. #define USTM_LOG_DIR "unistimHistory"
  81. #define USTM_LANG_DIR "unistimLang"
  82. /*! Size of the transmit buffer */
  83. #define MAX_BUF_SIZE 64
  84. /*! Number of slots for the transmit queue */
  85. #define MAX_BUF_NUMBER 150
  86. /*! Number of digits displayed on screen */
  87. #define MAX_SCREEN_NUMBER 15
  88. /*! Length of month label size */
  89. #define MONTH_LABEL_SIZE 3
  90. /*! Try x times before removing the phone */
  91. #define NB_MAX_RETRANSMIT 8
  92. /*! Nb of milliseconds waited when no events are scheduled */
  93. #define IDLE_WAIT 1000
  94. /*! Wait x milliseconds before resending a packet */
  95. #define RETRANSMIT_TIMER 2000
  96. /*! How often the mailbox is checked for new messages */
  97. #define TIMER_MWI 5000
  98. /*! Timeout value for entered number being dialed */
  99. #define DEFAULT_INTERDIGIT_TIMER 4000
  100. /*! Not used */
  101. #define DEFAULT_CODEC 0x00
  102. #define SIZE_PAGE 4096
  103. #define DEVICE_NAME_LEN 16
  104. #define AST_CONFIG_MAX_PATH 255
  105. #define MAX_ENTRY_LOG 30
  106. #define SUB_REAL 0
  107. #define SUB_RING 1
  108. #define SUB_THREEWAY 2
  109. struct ast_format_cap *global_cap;
  110. enum autoprovision {
  111. AUTOPROVISIONING_NO = 0,
  112. AUTOPROVISIONING_YES,
  113. AUTOPROVISIONING_TN
  114. };
  115. enum autoprov_extn {
  116. /*! Do not create an extension into the default dialplan */
  117. EXTENSION_NONE = 0,
  118. /*! Prompt user for an extension number and register it */
  119. EXTENSION_ASK,
  120. /*! Register an extension with the line=> value */
  121. EXTENSION_LINE,
  122. /*! Used with AUTOPROVISIONING_TN */
  123. EXTENSION_TN
  124. };
  125. #define OUTPUT_HANDSET 0xC0
  126. #define OUTPUT_HEADPHONE 0xC1
  127. #define OUTPUT_SPEAKER 0xC2
  128. #define VOLUME_LOW 0x01
  129. #define VOLUME_LOW_SPEAKER 0x03
  130. #define VOLUME_NORMAL 0x02
  131. #define VOLUME_INSANELY_LOUD 0x07
  132. #define MUTE_OFF 0x00
  133. #define MUTE_ON 0xFF
  134. #define MUTE_ON_DISCRET 0xCE
  135. #define LED_BAR_OFF 0x00 /* bar off */
  136. #define LED_BAR_ON 0x01 /* bar on */
  137. #define LED_BAR_P2 0x02 /* bar 1s on/1s */
  138. #define LED_BAR_P3 0x03 /* bar 2.5s on/0.5s off */
  139. #define LED_BAR_P4 0x04 /* bar 0.6s on/0.3s off */
  140. #define LED_BAR_P5 0x05 /* bar 0.5s on/0.5s off */
  141. #define LED_BAR_P6 0x06 /* bar 2s on/0.5s off */
  142. #define LED_BAR_P7 0x07 /* bar off */
  143. #define LED_SPEAKER_OFF 0x08
  144. #define LED_SPEAKER_ON 0x09
  145. #define LED_HEADPHONE_OFF 0x010
  146. #define LED_HEADPHONE_ON 0x011
  147. #define LED_MUTE_OFF 0x018
  148. #define LED_MUTE_ON 0x019
  149. #define LED_MUTE_BLINK 0x1A
  150. #define SIZE_HEADER 6
  151. #define SIZE_MAC_ADDR 17
  152. #define TEXT_LENGTH_MAX 24
  153. #define TEXT_LINE0 0x00
  154. #define TEXT_LINE1 0x20
  155. #define TEXT_LINE2 0x40
  156. #define TEXT_NORMAL 0x05
  157. #define TEXT_INVERSE 0x25
  158. #define STATUS_LENGTH_MAX 28
  159. #define FAV_ICON_NONE 0x00
  160. #define FAV_ICON_ONHOOK_BLACK 0x20
  161. #define FAV_ICON_ONHOOK_WHITE 0x21
  162. #define FAV_ICON_SPEAKER_ONHOOK_BLACK 0x22
  163. #define FAV_ICON_SPEAKER_ONHOOK_WHITE 0x23
  164. #define FAV_ICON_OFFHOOK_BLACK 0x24
  165. #define FAV_ICON_OFFHOOK_WHITE 0x25
  166. #define FAV_ICON_ONHOLD_BLACK 0x26
  167. #define FAV_ICON_ONHOLD_WHITE 0x27
  168. #define FAV_ICON_SPEAKER_OFFHOOK_BLACK 0x28
  169. #define FAV_ICON_SPEAKER_OFFHOOK_WHITE 0x29
  170. #define FAV_ICON_PHONE_BLACK 0x2A
  171. #define FAV_ICON_PHONE_WHITE 0x2B
  172. #define FAV_ICON_SPEAKER_ONHOLD_BLACK 0x2C
  173. #define FAV_ICON_SPEAKER_ONHOLD_WHITE 0x2D
  174. #define FAV_ICON_HEADPHONES 0x2E
  175. #define FAV_ICON_HEADPHONES_ONHOLD 0x2F
  176. #define FAV_ICON_HOME 0x30
  177. #define FAV_ICON_CITY 0x31
  178. #define FAV_ICON_SHARP 0x32
  179. #define FAV_ICON_PAGER 0x33
  180. #define FAV_ICON_CALL_CENTER 0x34
  181. #define FAV_ICON_FAX 0x35
  182. #define FAV_ICON_MAILBOX 0x36
  183. #define FAV_ICON_REFLECT 0x37
  184. #define FAV_ICON_COMPUTER 0x38
  185. #define FAV_ICON_FORWARD 0x39
  186. #define FAV_ICON_LOCKED 0x3A
  187. #define FAV_ICON_TRASH 0x3B
  188. #define FAV_ICON_INBOX 0x3C
  189. #define FAV_ICON_OUTBOX 0x3D
  190. #define FAV_ICON_MEETING 0x3E
  191. #define FAV_ICON_BOX 0x3F
  192. #define FAV_BLINK_FAST 0x20
  193. #define FAV_BLINK_SLOW 0x40
  194. #define FAV_MAX_LENGTH 0x0A
  195. #define FAVNUM 6
  196. #define EXPNUM 24
  197. #define FAV_LINE_ICON FAV_ICON_ONHOOK_BLACK
  198. static void dummy(char *unused, ...)
  199. {
  200. return;
  201. }
  202. /*! \brief Global jitterbuffer configuration - by default, jb is disabled
  203. * \note Values shown here match the defaults shown in unistim.conf.sample */
  204. static struct ast_jb_conf default_jbconf =
  205. {
  206. .flags = 0,
  207. .max_size = 200,
  208. .resync_threshold = 1000,
  209. .impl = "fixed",
  210. .target_extra = 40,
  211. };
  212. static struct ast_jb_conf global_jbconf;
  213. /* #define DUMP_PACKET 1 */
  214. /* #define DEBUG_TIMER ast_verbose */
  215. #define DEBUG_TIMER dummy
  216. /*! Enable verbose output. can also be set with the CLI */
  217. static int unistimdebug = 0;
  218. static int unistim_port;
  219. static enum autoprovision autoprovisioning = AUTOPROVISIONING_NO;
  220. static int unistim_keepalive;
  221. static int unistimsock = -1;
  222. static struct {
  223. unsigned int tos;
  224. unsigned int tos_audio;
  225. unsigned int cos;
  226. unsigned int cos_audio;
  227. } qos = { 0, 0, 0, 0 };
  228. static struct io_context *io;
  229. static struct ast_sched_context *sched;
  230. static struct sockaddr_in public_ip = { 0, };
  231. static unsigned char *buff; /*! Receive buffer address */
  232. static int unistim_reloading = 0;
  233. AST_MUTEX_DEFINE_STATIC(unistim_reload_lock);
  234. /*! This is the thread for the monitor which checks for input on the channels
  235. * which are not currently in use. */
  236. static pthread_t monitor_thread = AST_PTHREADT_NULL;
  237. /*! Protect the monitoring thread, so only one process can kill or start it, and not
  238. * when it's doing something critical. */
  239. AST_MUTEX_DEFINE_STATIC(monlock);
  240. /*! Protect the session list */
  241. AST_MUTEX_DEFINE_STATIC(sessionlock);
  242. /*! Protect the device list */
  243. AST_MUTEX_DEFINE_STATIC(devicelock);
  244. enum phone_state {
  245. STATE_INIT,
  246. STATE_AUTHDENY,
  247. STATE_MAINPAGE,
  248. STATE_EXTENSION,
  249. STATE_DIALPAGE,
  250. STATE_RINGING,
  251. STATE_CALL,
  252. STATE_SELECTOPTION,
  253. STATE_SELECTCODEC,
  254. STATE_SELECTLANGUAGE,
  255. STATE_CLEANING,
  256. STATE_HISTORY
  257. };
  258. enum handset_state {
  259. STATE_ONHOOK,
  260. STATE_OFFHOOK,
  261. };
  262. enum phone_key {
  263. KEY_0 = 0x40,
  264. KEY_1 = 0x41,
  265. KEY_2 = 0x42,
  266. KEY_3 = 0x43,
  267. KEY_4 = 0x44,
  268. KEY_5 = 0x45,
  269. KEY_6 = 0x46,
  270. KEY_7 = 0x47,
  271. KEY_8 = 0x48,
  272. KEY_9 = 0x49,
  273. KEY_STAR = 0x4a,
  274. KEY_SHARP = 0x4b,
  275. KEY_UP = 0x4c,
  276. KEY_DOWN = 0x4d,
  277. KEY_RIGHT = 0x4e,
  278. KEY_LEFT = 0x4f,
  279. KEY_QUIT = 0x50,
  280. KEY_COPY = 0x51,
  281. KEY_FUNC1 = 0x54,
  282. KEY_FUNC2 = 0x55,
  283. KEY_FUNC3 = 0x56,
  284. KEY_FUNC4 = 0x57,
  285. KEY_ONHOLD = 0x5b,
  286. KEY_HANGUP = 0x5c,
  287. KEY_MUTE = 0x5d,
  288. KEY_HEADPHN = 0x5e,
  289. KEY_LOUDSPK = 0x5f,
  290. KEY_FAV0 = 0x60,
  291. KEY_FAV1 = 0x61,
  292. KEY_FAV2 = 0x62,
  293. KEY_FAV3 = 0x63,
  294. KEY_FAV4 = 0x64,
  295. KEY_FAV5 = 0x65,
  296. KEY_COMPUTR = 0x7b,
  297. KEY_CONF = 0x7c,
  298. KEY_SNDHIST = 0x7d,
  299. KEY_RCVHIST = 0x7e,
  300. KEY_INDEX = 0x7f
  301. };
  302. enum charset {
  303. LANG_DEFAULT,
  304. ISO_8859_1,
  305. ISO_8859_2,
  306. ISO_8859_4,
  307. ISO_8859_5,
  308. ISO_2022_JP,
  309. };
  310. static const int dtmf_row[] = { 697, 770, 852, 941 };
  311. static const float dtmf_col[] = { 1209, 1336, 1477, 1633 };
  312. struct wsabuf {
  313. u_long len;
  314. unsigned char *buf;
  315. };
  316. struct unistim_subchannel {
  317. ast_mutex_t lock;
  318. unsigned int subtype; /*! SUB_REAL, SUB_RING or SUB_THREEWAY */
  319. struct ast_channel *owner; /*! Asterisk channel used by the subchannel */
  320. struct unistim_line *parent; /*! Unistim line */
  321. struct ast_rtp_instance *rtp; /*! RTP handle */
  322. int softkey; /*! Softkey assigned */
  323. pthread_t ss_thread; /*! unistim_ss thread handle */
  324. int alreadygone;
  325. int holding; /*! this subchannel holds someone */
  326. signed char ringvolume;
  327. signed char ringstyle;
  328. int moh; /*!< Music on hold in progress */
  329. AST_LIST_ENTRY(unistim_subchannel) list;
  330. };
  331. /*!
  332. * \todo Convert to stringfields
  333. */
  334. struct unistim_line {
  335. ast_mutex_t lock;
  336. char name[80]; /*! Like 200 */
  337. char fullname[101]; /*! Like USTM/200\@black */
  338. char exten[AST_MAX_EXTENSION]; /*! Extension where to start */
  339. char cid_num[AST_MAX_EXTENSION]; /*! CallerID Number */
  340. char mailbox[AST_MAX_EXTENSION]; /*! Mailbox for MWI */
  341. char musicclass[MAX_MUSICCLASS]; /*! MusicOnHold class */
  342. ast_group_t callgroup; /*! Call group */
  343. ast_group_t pickupgroup; /*! Pickup group */
  344. char accountcode[AST_MAX_ACCOUNT_CODE]; /*! Account code (for billing) */
  345. int amaflags; /*! AMA flags (for billing) */
  346. struct ast_format_cap *cap; /*! Codec supported */
  347. char parkinglot[AST_MAX_CONTEXT]; /*! Parkinglot */
  348. struct unistim_line *next;
  349. struct unistim_device *parent;
  350. AST_LIST_ENTRY(unistim_line) list;
  351. };
  352. /*!
  353. * \brief A device containing one or more lines
  354. */
  355. static struct unistim_device {
  356. ast_mutex_t lock;
  357. int receiver_state; /*!< state of the receiver (see ReceiverState) */
  358. int size_phone_number; /*!< size of the phone number */
  359. char context[AST_MAX_EXTENSION]; /*!< Context to start in */
  360. char phone_number[AST_MAX_EXTENSION]; /*!< the phone number entered by the user */
  361. char redial_number[AST_MAX_EXTENSION]; /*!< the last phone number entered by the user */
  362. char id[18]; /*!< mac address of the current phone in ascii */
  363. char name[DEVICE_NAME_LEN]; /*!< name of the device */
  364. int hasexp; /*!< if device have expansion connected */
  365. char expsoftkeylabel[EXPNUM][11]; /*!< soft key label */
  366. char softkeylabel[FAVNUM][11]; /*!< soft key label */
  367. char softkeynumber[FAVNUM][AST_MAX_EXTENSION]; /*!< number dialed when the soft key is pressed */
  368. char softkeyicon[FAVNUM]; /*!< icon number */
  369. char softkeydevice[FAVNUM][16]; /*!< name of the device monitored */
  370. struct unistim_subchannel *ssub[FAVNUM];
  371. struct unistim_line *sline[FAVNUM];
  372. struct unistim_device *sp[FAVNUM]; /*!< pointer to the device monitored by this soft key */
  373. char language[MAX_LANGUAGE]; /*!< Language for asterisk sounds */
  374. int height; /*!< The number of lines the phone can display */
  375. char maintext0[25]; /*!< when the phone is idle, display this string on line 0 */
  376. char maintext1[25]; /*!< when the phone is idle, display this string on line 1 */
  377. char maintext2[25]; /*!< when the phone is idle, display this string on line 2 */
  378. char titledefault[13]; /*!< title (text before date/time) */
  379. char datetimeformat; /*!< format used for displaying time/date */
  380. signed char contrast; /*!< contrast */
  381. char country[3]; /*!< country used for dial tone frequency */
  382. struct ast_tone_zone *tz; /*!< Tone zone for res_indications (ring, busy, congestion) */
  383. signed char ringvolume; /*!< Ring volume */
  384. signed char ringstyle; /*!< Ring melody */
  385. signed char cwvolume; /*!< Ring volume on call waiting */
  386. signed char cwstyle; /*!< Ring melody on call waiting */
  387. int interdigit_timer; /*!< Interdigit timer for dialing number by timeout */
  388. int dtmfduration; /*!< DTMF playback duration */
  389. time_t nextdial; /*!< Timer used for dial by timeout */
  390. int rtp_port; /*!< RTP port used by the phone */
  391. int rtp_method; /*!< Select the unistim data used to establish a RTP session */
  392. int status_method; /*!< Select the unistim packet used for sending status text */
  393. char codec_number; /*!< The current codec used to make calls */
  394. int missed_call; /*!< Number of call unanswered */
  395. int callhistory; /*!< Allowed to record call history */
  396. int sharp_dial; /*!< Execute Dial on '#' or not */
  397. char lst_cid[TEXT_LENGTH_MAX]; /*!< Last callerID received */
  398. char lst_cnm[TEXT_LENGTH_MAX]; /*!< Last callername recevied */
  399. char call_forward[AST_MAX_EXTENSION]; /*!< Forward number */
  400. int output; /*!< Handset, headphone or speaker */
  401. int previous_output; /*!< Previous output */
  402. int volume; /*!< Default volume */
  403. int selected; /*!< softkey selected */
  404. int microphone; /*!< Microphone mode (audio tx) */
  405. int lastmsgssent; /*! Used by MWI */
  406. time_t nextmsgcheck; /*! Used by MWI */
  407. int nat; /*!< Used by the obscure ast_rtp_setnat */
  408. enum autoprov_extn extension; /*!< See ifdef EXTENSION for valid values */
  409. char extension_number[11]; /*!< Extension number entered by the user */
  410. signed char to_delete; /*!< Used in reload */
  411. struct ast_silence_generator *silence_generator;
  412. AST_LIST_HEAD(,unistim_subchannel) subs; /*!< pointer to our current connection, channel... */
  413. AST_LIST_HEAD(,unistim_line) lines;
  414. struct ast_ha *ha;
  415. struct unistimsession *session;
  416. struct unistim_device *next;
  417. } *devices = NULL;
  418. static struct unistimsession {
  419. ast_mutex_t lock;
  420. struct sockaddr_in sin; /*!< IP address of the phone */
  421. struct sockaddr_in sout; /*!< IP address of server */
  422. int timeout; /*!< time-out in ticks : resend packet if no ack was received before the timeout occured */
  423. unsigned short seq_phone; /*!< sequence number for the next packet (when we receive a request) */
  424. unsigned short seq_server; /*!< sequence number for the next packet (when we send a request) */
  425. unsigned short last_seq_ack; /*!< sequence number of the last ACK received */
  426. unsigned long tick_next_ping; /*!< time for the next ping */
  427. int last_buf_available; /*!< number of a free slot */
  428. int nb_retransmit; /*!< number of retransmission */
  429. int state; /*!< state of the phone (see phone_state) */
  430. int size_buff_entry; /*!< size of the buffer used to enter datas */
  431. char buff_entry[16]; /*!< Buffer for temporary datas */
  432. char macaddr[18]; /*!< mac address of the phone (not always available) */
  433. char firmware[8]; /*!< firmware of the phone (not always available) */
  434. struct wsabuf wsabufsend[MAX_BUF_NUMBER]; /*!< Size of each paquet stored in the buffer array & pointer to this buffer */
  435. unsigned char buf[MAX_BUF_NUMBER][MAX_BUF_SIZE]; /*!< Buffer array used to keep the lastest non-acked paquets */
  436. struct unistim_device *device;
  437. struct unistimsession *next;
  438. } *sessions = NULL;
  439. /*! Store on screen phone menu item (label and handler function) */
  440. struct unistim_menu_item {
  441. char *label;
  442. int state;
  443. void (*handle_option)(struct unistimsession *);
  444. };
  445. /*! Language item for currently existed translations */
  446. struct unistim_languages {
  447. char *label;
  448. char *lang_short;
  449. int encoding;
  450. struct ao2_container *trans;
  451. };
  452. /*!
  453. * \page Unistim datagram formats
  454. *
  455. * Format of datagrams :
  456. * bytes 0 & 1 : ffff for discovery packet, 0000 for everything else
  457. * byte 2 : sequence number (high part)
  458. * byte 3 : sequence number (low part)
  459. * byte 4 : 2 = ask question or send info, 1 = answer or ACK, 0 = retransmit request
  460. * byte 5 : direction, 1 = server to phone, 2 = phone to server arguments
  461. */
  462. static const unsigned char packet_rcv_discovery[] =
  463. { 0xff, 0xff, 0xff, 0xff, 0x02, 0x02, 0xff, 0xff, 0xff, 0xff, 0x9e, 0x03, 0x08 };
  464. static const unsigned char packet_send_discovery_ack[] =
  465. { 0x00, 0x00, /*Initial Seq (2 bytes) */ 0x00, 0x00, 0x00, 0x01 };
  466. static const unsigned char packet_recv_firm_version[] =
  467. { 0x00, 0x00, 0x00, 0x13, 0x9a, 0x0a, 0x02 };
  468. static const unsigned char packet_recv_it_type[] =
  469. { 0x00, 0x00, 0x00, 0x13, 0x9a, 0x04, 0x03 };
  470. static const unsigned char packet_recv_pressed_key[] =
  471. { 0x00, 0x00, 0x00, 0x13, 0x99, 0x04, 0x00 };
  472. static const unsigned char packet_recv_pick_up[] =
  473. { 0x00, 0x00, 0x00, 0x13, 0x99, 0x03, 0x04 };
  474. static const unsigned char packet_recv_hangup[] =
  475. { 0x00, 0x00, 0x00, 0x13, 0x99, 0x03, 0x03 };
  476. static const unsigned char packet_recv_r2[] = { 0x00, 0x00, 0x00, 0x13, 0x96, 0x03, 0x03 };
  477. /*! Expansion module (i2004 KEM) */
  478. static const unsigned char packet_recv_expansion_pressed_key[] =
  479. { 0x00, 0x00, 0x00, 0x13, 0x89, 0x04, 0x59 };
  480. static const unsigned char packet_send_expansion_next[] = { 0x09, 0x03, 0x17 };
  481. static const unsigned char packet_send_expansion_icon[] = { 0x09, 0x06, 0x59, 0x05, /*pos */ 0x47, /*icon */ 0x20 }; /* display an icon in front of the text zone */
  482. static const unsigned char packet_send_expansion_text[] = { 0x09, 0x0f, 0x57, 0x19, /*pos */ 0x47, /*text */ 0x20,
  483. 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20 /*end_text */ };
  484. /*! TransportAdapter */
  485. static const unsigned char packet_recv_resume_connection_with_server[] =
  486. { 0xff, 0xff, 0xff, 0xff, 0x9e, 0x03, 0x08 };
  487. static const unsigned char packet_recv_mac_addr[] =
  488. { 0xff, 0xff, 0xff, 0xff, 0x9a, 0x0d, 0x07 /*MacAddr */ };
  489. static const unsigned char packet_send_date_time3[] =
  490. { 0x11, 0x09, 0x02, 0x02, /*Month */ 0x05, /*Day */ 0x06, /*Hour */ 0x07,
  491. /*Minutes */ 0x08, 0x32
  492. };
  493. static const unsigned char packet_send_date_time[] =
  494. { 0x11, 0x09, 0x02, 0x0a, /*Month */ 0x05, /*Day */ 0x06, /*Hour */ 0x07, /*Minutes */
  495. 0x08, 0x32, 0x17, 0x04, 0x24, 0x07, 0x19,
  496. 0x04, 0x07, 0x00, 0x19, 0x05, 0x09, 0x3e, 0x0f, 0x16, 0x05, 0x00, 0x80, 0x00, 0x1e,
  497. 0x05, 0x12, 0x00, 0x78
  498. };
  499. static const unsigned char packet_send_no_ring[] =
  500. { 0x16, 0x04, 0x1a, 0x00, 0x16, 0x04, 0x11, 0x00 };
  501. static const unsigned char packet_send_s4[] =
  502. { 0x16, 0x04, 0x1a, 0x00, 0x16, 0x04, 0x11, 0x00, 0x16, 0x06, 0x32, 0xdf, 0x00, 0xff,
  503. 0x16, 0x05, 0x1c, 0x00, 0x00, 0x17, 0x05,
  504. 0x0b, 0x00, 0x00, 0x19, 0x04, 0x00, 0x00, 0x19, 0x04, 0x00, 0x08, 0x19, 0x04, 0x00,
  505. 0x10, 0x19, 0x04, 0x00, 0x18, 0x16, 0x05,
  506. 0x31, 0x00, 0x00, 0x16, 0x05, 0x04, 0x00, 0x00
  507. };
  508. static const unsigned char packet_send_call[] =
  509. { 0x16, 0x04, 0x1a, 0x00, 0x16, 0x04, 0x11, 0x00, 0x16, 0x06, 0x32, 0xdf,
  510. 0x00, 0xff, 0x16, 0x05, 0x1c, 0x00, 0x00, 0x16, 0x0a, 0x38, 0x00, 0x12, 0xca, 0x03,
  511. 0xc0, 0xc3, 0xc5, 0x16, 0x16, 0x30, 0x00,
  512. 0x00, /*codec */ 0x12, 0x12, /* frames per packet */ 0x01, 0x5c, 0x00, /*port RTP */
  513. 0x0f, 0xa0, /* port RTCP */ 0x9c, 0x41,
  514. /*port RTP */ 0x0f, 0xa0, /* port RTCP */ 0x9c, 0x41, /* IP Address */ 0x0a, 0x01,
  515. 0x16, 0x66
  516. };
  517. static const unsigned char packet_send_stream_based_tone_off[] =
  518. { 0x16, 0x05, 0x1c, 0x00, 0x00 };
  519. static const unsigned char packet_send_mute[] = { 0x16, 0x05, 0x04, 0x00, 0x00 };
  520. #ifdef NOT_USED
  521. static const unsigned char packet_send_CloseAudioStreamRX[] = { 0x16, 0x05, 0x31, 0x00, 0xff };
  522. static const unsigned char packet_send_CloseAudioStreamTX[] = { 0x16, 0x05, 0x31, 0xff, 0x00 };
  523. #endif
  524. static const unsigned char packet_send_stream_based_tone_on[] =
  525. { 0x16, 0x06, 0x1b, 0x00, 0x00, 0x05 };
  526. static const unsigned char packet_send_stream_based_tone_single_freq[] =
  527. { 0x16, 0x06, 0x1d, 0x00, 0x01, 0xb8 };
  528. static const unsigned char packet_send_stream_based_tone_dual_freq[] =
  529. { 0x16, 0x08, 0x1d, 0x00, 0x01, 0xb8, 0x01, 0x5e };
  530. static const unsigned char packet_send_select_output[] =
  531. { 0x16, 0x06, 0x32, 0xc0, 0x01, 0x00 };
  532. static const unsigned char packet_send_ring[] =
  533. { 0x16, 0x06, 0x32, 0xdf, 0x00, 0xff, 0x16, 0x05, 0x1c, 0x00, 0x00, 0x16,
  534. 0x04, 0x1a, 0x01, 0x16, 0x05, 0x12, 0x13 /* Ring type 10 to 17 */ , 0x18, 0x16, 0x04, 0x18, /* volume 00, 10, 20... */
  535. 0x20, 0x16, 0x04, 0x10, 0x00
  536. };
  537. //static const unsigned char packet_send_end_call[] =
  538. // { 0x16, 0x06, 0x32, 0xdf, 0x00, 0xff, 0x16, 0x05, 0x31, 0x00, 0x00, /* Headset LED off */ 0x19, 0x04, 0x00,
  539. //0x10, /* Mute LED off */ 0x19, 0x04, 0x00, 0x18,/* Stream unmute */ 0x16, 0x05, 0x04, 0x00, 0x00, /* Query RTCP */ 0x16, 0x04, 0x37, 0x10 };
  540. static const unsigned char packet_send_end_call[] =
  541. { 0x16, 0x06, 0x32, 0xdf, 0x00, 0xff, 0x16, 0x05, 0x31, 0x00, 0x00, /* Query RTCP */ 0x16, 0x04, 0x37, 0x10 };
  542. static const unsigned char packet_send_s9[] =
  543. { 0x16, 0x06, 0x32, 0xdf, 0x00, 0xff, 0x19, 0x04, 0x00, 0x10, 0x16, 0x05, 0x1c, 0x00,
  544. 0x00 };
  545. static const unsigned char packet_send_rtp_packet_size[] =
  546. { 0x16, 0x08, 0x38, 0x00, 0x00, 0xe0, 0x00, 0xa0 };
  547. static const unsigned char packet_send_jitter_buffer_conf[] =
  548. { 0x16, 0x0e, 0x3a, 0x00, /* jitter */ 0x02, /* high water mark */ 0x04, 0x00, 0x00,
  549. /* early packet resync 2 bytes */ 0x3e, 0x80,
  550. 0x00, 0x00, /* late packet resync 2 bytes */ 0x3e, 0x80
  551. };
  552. /* Duration in ms div 2 (0x20 = 64ms, 0x08 = 16ms)
  553. static unsigned char packet_send_StreamBasedToneCad[] =
  554. { 0x16, 0x0a, 0x1e, 0x00, duration on 0x0a, duration off 0x0d, duration on 0x0a, duration off 0x0d, duration on 0x0a, duration off 0x2b }; */
  555. static const unsigned char packet_send_open_audio_stream_rx[] =
  556. { 0x16, 0x1a, 0x30, 0x00, 0xff, /* Codec */ 0x00, 0x00, 0x01, 0x00, 0xb8, 0xb8, 0x0e,
  557. 0x0e, 0x01, /* Port */ 0x14, 0x50, 0x00,
  558. 0x00, /* Port */ 0x14, 0x50, 0x00, 0x00, /* Dest IP */ 0x0a, 0x93, 0x69, 0x05
  559. };
  560. static const unsigned char packet_send_open_audio_stream_tx[] =
  561. { 0x16, 0x1a, 0x30, 0xff, 0x00, 0x00, /* Codec */ 0x00, 0x01, 0x00, 0xb8, 0xb8, 0x0e,
  562. 0x0e, 0x01, /* Local port */ 0x14, 0x50,
  563. 0x00, 0x00, /* Rmt Port */ 0x14, 0x50, 0x00, 0x00, /* Dest IP */ 0x0a, 0x93, 0x69, 0x05
  564. };
  565. static const unsigned char packet_send_open_audio_stream_rx3[] =
  566. { 0x16, 0x1a, 0x30, 0x00, 0xff, /* Codec */ 0x00, 0x00, 0x02, 0x01, 0xb8, 0xb8, 0x06,
  567. 0x06, 0x81, /* RTP Port */ 0x14, 0x50,
  568. /* RTCP Port */ 0x14,
  569. 0x51, /* RTP Port */ 0x14, 0x50, /* RTCP Port */ 0x00, 0x00, /* Dest IP */ 0x0a, 0x93,
  570. 0x69, 0x05
  571. };
  572. static const unsigned char packet_send_open_audio_stream_tx3[] =
  573. { 0x16, 0x1a, 0x30, 0xff, 0x00, 0x00, /* Codec */ 0x00, 0x02, 0x01, 0xb8, 0xb8, 0x06,
  574. 0x06, 0x81, /* RTP Local port */ 0x14, 0x50,
  575. /* RTCP Port */ 0x00, 0x00, /* RTP Rmt Port */ 0x14, 0x50, /* RTCP Port */ 0x00, 0x00,
  576. /* Dest IP */ 0x0a, 0x93, 0x69, 0x05
  577. };
  578. static const unsigned char packet_send_arrow[] = { 0x17, 0x04, 0x04, 0x00 };
  579. static const unsigned char packet_send_blink_cursor[] = { 0x17, 0x04, 0x10, 0x86 };
  580. static const unsigned char packet_send_date_time2[] = { 0x17, 0x04, 0x17, 0x3d, 0x11, 0x09, 0x02, 0x0a, /*Month */ 0x05, /*Day */
  581. 0x06, /*Hour */ 0x07, /*Minutes */ 0x08, 0x32
  582. };
  583. static const unsigned char packet_send_Contrast[] =
  584. { 0x17, 0x04, 0x24, /*Contrast */ 0x08 };
  585. static const unsigned char packet_send_start_timer[] =
  586. { 0x17, 0x05, 0x0b, /*Timer option*/0x05, /* Timer ID */0x00, 0x17, 0x08, 0x16,
  587. /* Text */ 'T', 'i', 'm', 'e', 'r' };
  588. static const unsigned char packet_send_stop_timer[] = { 0x17, 0x05, 0x0b, 0x02, 0x00 };
  589. static const unsigned char packet_send_icon[] = { 0x17, 0x05, 0x14, /*pos */ 0x00, /*icon */ 0x25 }; /* display an icon in front of the text zone */
  590. static const unsigned char packet_send_S7[] = { 0x17, 0x06, 0x0f, 0x30, 0x07, 0x07 };
  591. static const unsigned char packet_send_set_pos_cursor[] =
  592. { 0x17, 0x06, 0x10, 0x81, 0x04, /*pos */ 0x20 };
  593. static unsigned char monthlabels[] =
  594. { 'J', 'a', 'n', 'F', 'e', 'b', 'M', 'a', 'r', 'A', 'p', 'r', 'M', 'a', 'y', 'J', 'u', 'n',
  595. 'J', 'u', 'l', 'A', 'u', 'g', 'S', 'e', 'p', 'O', 'c', 't', 'N', 'o', 'v', 'D', 'e', 'c' };
  596. static unsigned char packet_send_monthlabels_download[] =
  597. { 0x17, 0x0a, 0x15, /* Month (3 char) */ '-', '-', '-', '-', '-', '-', 0x20 };
  598. static const unsigned char packet_send_favorite[] =
  599. { 0x17, 0x0f, 0x19, 0x10, /*pos */ 0x01, /*name */ 0x20, 0x20, 0x20, 0x20, 0x20, 0x20,
  600. 0x20, 0x20, 0x20, 0x20, /*end_name */ 0x19,
  601. 0x05, 0x0f, /*pos */ 0x01, /*icone */ 0x00
  602. };
  603. static const unsigned char packet_send_title[] =
  604. { 0x17, 0x10, 0x19, 0x02, /*text */ 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20,
  605. 0x20, 0x20, 0x20, 0x20 /*end_text */ };
  606. static const unsigned char packet_send_text[] =
  607. { 0x17, 0x1e, 0x1b, 0x04, /*pos */ 0x00, /*inverse */ 0x25, /*text */ 0x20, 0x20,
  608. 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20,
  609. 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20,
  610. /*end_text */ 0x17, 0x04, 0x10, 0x87
  611. };
  612. static const unsigned char packet_send_status[] =
  613. { 0x17, 0x20, 0x19, 0x08, /*text */ 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20,
  614. 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20,
  615. 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20 /*end_text */
  616. };
  617. static const unsigned char packet_send_status2[] =
  618. { 0x17, 0x0b, 0x19, /* pos [08|28|48|68] */ 0x00, /* text */ 0x20, 0x20, 0x20, 0x20,
  619. 0x20, 0x20, 0x20 /* end_text */ };
  620. /* Multiple character set support */
  621. /* ISO-8859-1 - Western European) */
  622. static const unsigned char packet_send_charset_iso_8859_1[] =
  623. { 0x17, 0x08, 0x21, 0x1b, 0x2d, 0x41, 0x1b, 0x00 };
  624. /* ISO-8859-2 - Central European) */
  625. static const unsigned char packet_send_charset_iso_8859_2[] =
  626. { 0x17, 0x08, 0x21, 0x1b, 0x2d, 0x42, 0x1b, 0x00 };
  627. /* ISO-8859-4 - Baltic) */
  628. static const unsigned char packet_send_charset_iso_8859_4[] =
  629. { 0x17, 0x08, 0x21, 0x1b, 0x2d, 0x44, 0x1b, 0x00 };
  630. /* ISO 8859-5 - cyrillic */
  631. static const unsigned char packet_send_charset_iso_8859_5[] =
  632. { 0x17, 0x08, 0x21, 0x1b, 0x2d, 0x4c, 0x1b, 0x00 };
  633. /* Japanese (ISO-2022-JP ?) */
  634. static const unsigned char packet_send_charset_iso_2022_jp[] =
  635. { 0x17, 0x08, 0x21, 0x1b, 0x29, 0x49, 0x1b, 0x7e };
  636. static const unsigned char packet_send_led_update[] = { 0x19, 0x04, 0x00, 0x00 };
  637. static const unsigned char packet_send_query_basic_manager_04[] = { 0x1a, 0x04, 0x01, 0x04 };
  638. static const unsigned char packet_send_query_mac_address[] = { 0x1a, 0x04, 0x01, 0x08 };
  639. static const unsigned char packet_send_query_basic_manager_10[] = { 0x1a, 0x04, 0x01, 0x10 };
  640. static const unsigned char packet_send_S1[] = { 0x1a, 0x07, 0x07, 0x00, 0x00, 0x00, 0x13 };
  641. static unsigned char packet_send_ping[] =
  642. { 0x1e, 0x05, 0x12, 0x00, /*Watchdog timer */ 0x78 };
  643. #define BUFFSEND unsigned char buffsend[64] = { 0x00, 0x00, 0xaa, 0xbb, 0x02, 0x01 }
  644. static const char tdesc[] = "UNISTIM Channel Driver";
  645. static const char channel_type[] = "USTM";
  646. /*! Protos */
  647. static struct ast_channel *unistim_new(struct unistim_subchannel *sub, int state, const struct ast_assigned_ids *assignedids, const struct ast_channel *requestor);
  648. static int load_module(void);
  649. static int reload(void);
  650. static int unload_module(void);
  651. static int reload_config(void);
  652. static void unistim_set_owner(struct unistim_subchannel *sub, struct ast_channel *chan);
  653. static void show_main_page(struct unistimsession *pte);
  654. static struct ast_channel *unistim_request(const char *type, struct ast_format_cap *cap, const struct ast_assigned_ids *assignedids, const struct ast_channel *requestor,
  655. const char *dest, int *cause);
  656. static int unistim_call(struct ast_channel *ast, const char *dest, int timeout);
  657. static int unistim_hangup(struct ast_channel *ast);
  658. static int unistim_answer(struct ast_channel *ast);
  659. static struct ast_frame *unistim_read(struct ast_channel *ast);
  660. static int unistim_write(struct ast_channel *ast, struct ast_frame *frame);
  661. static int unistim_indicate(struct ast_channel *ast, int ind, const void *data,
  662. size_t datalen);
  663. static int unistim_fixup(struct ast_channel *oldchan, struct ast_channel *newchan);
  664. static int unistim_senddigit_begin(struct ast_channel *ast, char digit);
  665. static int unistim_senddigit_end(struct ast_channel *ast, char digit,
  666. unsigned int duration);
  667. static int unistim_sendtext(struct ast_channel *ast, const char *text);
  668. static int write_entry_history(struct unistimsession *pte, FILE * f, char c,
  669. char *line1);
  670. static void change_callerid(struct unistimsession *pte, int type, char *callerid);
  671. static struct ast_channel_tech unistim_tech = {
  672. .type = channel_type,
  673. .description = tdesc,
  674. .properties = AST_CHAN_TP_WANTSJITTER | AST_CHAN_TP_CREATESJITTER,
  675. .requester = unistim_request,
  676. .call = unistim_call,
  677. .hangup = unistim_hangup,
  678. .answer = unistim_answer,
  679. .read = unistim_read,
  680. .write = unistim_write,
  681. .indicate = unistim_indicate,
  682. .fixup = unistim_fixup,
  683. .send_digit_begin = unistim_senddigit_begin,
  684. .send_digit_end = unistim_senddigit_end,
  685. .send_text = unistim_sendtext,
  686. };
  687. static void send_start_rtp(struct unistim_subchannel *);
  688. static void send_callerid_screen(struct unistimsession *, struct unistim_subchannel *);
  689. static void key_favorite(struct unistimsession *, char);
  690. static void handle_select_codec(struct unistimsession *);
  691. static void handle_select_language(struct unistimsession *);
  692. static int find_language(const char*);
  693. static int unistim_free_sub(struct unistim_subchannel *);
  694. static struct unistim_menu_item options_menu[] =
  695. {
  696. {"Change codec", STATE_SELECTCODEC, handle_select_codec},
  697. {"Language", STATE_SELECTLANGUAGE, handle_select_language},
  698. {NULL, 0, NULL}
  699. };
  700. static struct unistim_languages options_languages[] =
  701. {
  702. {"English", "en", ISO_8859_1, NULL},
  703. {"French", "fr", ISO_8859_1, NULL},
  704. {"Russian", "ru", ISO_8859_5, NULL},
  705. {NULL, NULL, 0, NULL}
  706. };
  707. static char ustm_strcopy[1024];
  708. struct ustm_lang_entry {
  709. const char *str_orig;
  710. const char *str_trans;
  711. };
  712. static int lang_hash_fn(const void *obj, const int flags)
  713. {
  714. const struct ustm_lang_entry *entry = obj;
  715. return ast_str_hash(entry->str_orig);
  716. }
  717. static int lang_cmp_fn(void *obj, void *arg, int flags)
  718. {
  719. struct ustm_lang_entry *entry1 = obj;
  720. struct ustm_lang_entry *entry2 = arg;
  721. return (!strcmp(entry1->str_orig, entry2->str_orig)) ? (CMP_MATCH | CMP_STOP) : 0;
  722. }
  723. static const char *ustmtext(const char *str, struct unistimsession *pte)
  724. {
  725. struct ustm_lang_entry *lang_entry;
  726. struct ustm_lang_entry le_search;
  727. struct unistim_languages *lang = NULL;
  728. int size;
  729. if (pte->device) {
  730. lang = &options_languages[find_language(pte->device->language)];
  731. }
  732. if (!lang) {
  733. return str;
  734. }
  735. /* Check if specified language exists */
  736. if (!lang->trans) {
  737. char tmp[1024], *p, *p_orig = NULL, *p_trans = NULL;
  738. FILE *f;
  739. lang->trans = ao2_container_alloc_hash(AO2_ALLOC_OPT_LOCK_MUTEX, 0, 8,
  740. lang_hash_fn, NULL, lang_cmp_fn);
  741. if (!lang->trans) {
  742. ast_log(LOG_ERROR, "Unable to allocate container for translation!\n");
  743. return str;
  744. }
  745. snprintf(tmp, sizeof(tmp), "%s/%s/%s.po", ast_config_AST_VAR_DIR,
  746. USTM_LANG_DIR, lang->lang_short);
  747. f = fopen(tmp, "r");
  748. if (!f) {
  749. ast_log(LOG_WARNING, "There is no translation file for '%s'\n", lang->lang_short);
  750. return str;
  751. }
  752. while (fgets(tmp, sizeof(tmp), f)) {
  753. if (!(p = strchr(tmp, '\n'))) {
  754. ast_log(LOG_ERROR, "Too long line found in language file - truncated!\n");
  755. continue;
  756. }
  757. *p = '\0';
  758. if (!(p = strchr(tmp, '"'))) {
  759. continue;
  760. }
  761. if (tmp == strstr(tmp, "msgid")) {
  762. p_orig = ast_strdup(p + 1);
  763. p = strchr(p_orig, '"');
  764. } else if (tmp == strstr(tmp, "msgstr")) {
  765. p_trans = ast_strdup(p + 1);
  766. p = strchr(p_trans, '"');
  767. } else {
  768. continue;
  769. }
  770. *p = '\0';
  771. if (!p_trans || !p_orig) {
  772. continue;
  773. }
  774. if (ast_strlen_zero(p_trans)) {
  775. ast_free(p_trans);
  776. ast_free(p_orig);
  777. p_trans = NULL;
  778. p_orig = NULL;
  779. continue;
  780. }
  781. if (!(lang_entry = ao2_alloc(sizeof(*lang_entry), NULL))) {
  782. fclose(f);
  783. return str;
  784. }
  785. lang_entry->str_trans = p_trans;
  786. lang_entry->str_orig = p_orig;
  787. ao2_link(lang->trans, lang_entry);
  788. p_trans = NULL;
  789. p_orig = NULL;
  790. }
  791. fclose(f);
  792. }
  793. le_search.str_orig = str;
  794. if ((lang_entry = ao2_find(lang->trans, &le_search, OBJ_POINTER))) {
  795. size = strlen(lang_entry->str_trans)+1;
  796. if (size > 1024) {
  797. size = 1024;
  798. }
  799. memcpy(ustm_strcopy, lang_entry->str_trans, size);
  800. ao2_ref(lang_entry, -1);
  801. return ustm_strcopy;
  802. }
  803. return str;
  804. }
  805. static void display_last_error(const char *sz_msg)
  806. {
  807. /* Display the error message */
  808. ast_log(LOG_WARNING, "%s : (%d) %s\n", sz_msg, errno, strerror(errno));
  809. }
  810. static unsigned int get_tick_count(void)
  811. {
  812. struct timeval now = ast_tvnow();
  813. return (now.tv_sec * 1000) + (now.tv_usec / 1000);
  814. }
  815. /* Send data to a phone without retransmit nor buffering */
  816. static void send_raw_client(int size, const unsigned char *data, struct sockaddr_in *addr_to,
  817. const struct sockaddr_in *addr_ourip)
  818. {
  819. #ifdef HAVE_PKTINFO
  820. struct iovec msg_iov;
  821. struct msghdr msg;
  822. char buffer[CMSG_SPACE(sizeof(struct in_pktinfo))];
  823. struct cmsghdr *ip_msg = (struct cmsghdr *) buffer;
  824. struct in_pktinfo *pki = (struct in_pktinfo *) CMSG_DATA(ip_msg);
  825. /* cast this to a non-const pointer, since the sendmsg() API
  826. * does not provide read-only and write-only flavors of the
  827. * structures used for its arguments, but in this case we know
  828. * the data will not be modified
  829. */
  830. msg_iov.iov_base = (char *) data;
  831. msg_iov.iov_len = size;
  832. msg.msg_name = addr_to; /* optional address */
  833. msg.msg_namelen = sizeof(struct sockaddr_in); /* size of address */
  834. msg.msg_iov = &msg_iov; /* scatter/gather array */
  835. msg.msg_iovlen = 1; /* # elements in msg_iov */
  836. msg.msg_control = ip_msg; /* ancillary data */
  837. msg.msg_controllen = sizeof(buffer); /* ancillary data buffer len */
  838. msg.msg_flags = 0; /* flags on received message */
  839. ip_msg->cmsg_len = CMSG_LEN(sizeof(*pki));
  840. ip_msg->cmsg_level = IPPROTO_IP;
  841. ip_msg->cmsg_type = IP_PKTINFO;
  842. pki->ipi_ifindex = 0; /* Interface index, 0 = use interface specified in routing table */
  843. pki->ipi_spec_dst.s_addr = addr_ourip->sin_addr.s_addr; /* Local address */
  844. /* pki->ipi_addr = ; Header Destination address - ignored by kernel */
  845. #ifdef DUMP_PACKET
  846. if (unistimdebug) {
  847. int tmp;
  848. ast_verb(0, "\n**> From %s sending %d bytes to %s ***\n",
  849. ast_inet_ntoa(addr_ourip->sin_addr), (int) size,
  850. ast_inet_ntoa(addr_to->sin_addr));
  851. for (tmp = 0; tmp < size; tmp++)
  852. ast_verb(0, "%02hhx ", data[tmp]);
  853. ast_verb(0, "\n******************************************\n");
  854. }
  855. #endif
  856. if (sendmsg(unistimsock, &msg, 0) == -1) {
  857. display_last_error("Error sending datas");
  858. }
  859. #else
  860. if (sendto(unistimsock, data, size, 0, (struct sockaddr *) addr_to, sizeof(*addr_to))
  861. == -1)
  862. display_last_error("Error sending datas");
  863. #endif
  864. }
  865. static void send_client(int size, const unsigned char *data, struct unistimsession *pte)
  866. {
  867. unsigned int tick;
  868. int buf_pos;
  869. unsigned short seq = ntohs(++pte->seq_server);
  870. ast_mutex_lock(&pte->lock);
  871. buf_pos = pte->last_buf_available;
  872. if (buf_pos >= MAX_BUF_NUMBER) {
  873. ast_log(LOG_WARNING, "Error : send queue overflow\n");
  874. ast_mutex_unlock(&pte->lock);
  875. return;
  876. }
  877. memcpy((void *)data + sizeof(unsigned short), (void *)&seq, sizeof(unsigned short));
  878. pte->wsabufsend[buf_pos].len = size;
  879. memcpy(pte->wsabufsend[buf_pos].buf, data, size);
  880. tick = get_tick_count();
  881. pte->timeout = tick + RETRANSMIT_TIMER;
  882. /*#ifdef DUMP_PACKET */
  883. if (unistimdebug) {
  884. ast_verb(0, "Sending datas with seq #0x%04x Using slot #%d :\n", (unsigned)pte->seq_server, buf_pos);
  885. }
  886. /*#endif */
  887. send_raw_client(pte->wsabufsend[buf_pos].len, pte->wsabufsend[buf_pos].buf, &(pte->sin),
  888. &(pte->sout));
  889. pte->last_buf_available++;
  890. ast_mutex_unlock(&pte->lock);
  891. }
  892. static void send_ping(struct unistimsession *pte)
  893. {
  894. BUFFSEND;
  895. if (unistimdebug) {
  896. ast_verb(0, "Sending ping\n");
  897. }
  898. pte->tick_next_ping = get_tick_count() + unistim_keepalive;
  899. memcpy(buffsend + SIZE_HEADER, packet_send_ping, sizeof(packet_send_ping));
  900. send_client(SIZE_HEADER + sizeof(packet_send_ping), buffsend, pte);
  901. }
  902. static int get_to_address(int fd, struct sockaddr_in *toAddr)
  903. {
  904. #ifdef HAVE_PKTINFO
  905. int err;
  906. char cmbuf[0x100];
  907. struct cmsghdr *cmsg;
  908. struct sockaddr_in peeraddr;
  909. struct in_addr addr;
  910. struct msghdr mh = {
  911. .msg_name = &peeraddr,
  912. .msg_namelen = sizeof(peeraddr),
  913. .msg_control = cmbuf,
  914. .msg_controllen = sizeof(cmbuf),
  915. };
  916. memset(&addr, 0, sizeof(addr));
  917. /* Get info about the incoming packet */
  918. err = recvmsg(fd, &mh, MSG_PEEK);
  919. if (err == -1) {
  920. ast_log(LOG_WARNING, "recvmsg returned an error: %s\n", strerror(errno));
  921. return err;
  922. }
  923. for(cmsg = CMSG_FIRSTHDR(&mh);
  924. cmsg != NULL;
  925. cmsg = CMSG_NXTHDR(&mh, cmsg))
  926. {
  927. if (cmsg->cmsg_level == IPPROTO_IP && cmsg->cmsg_type == IP_PKTINFO) {
  928. struct in_pktinfo *pkt = (struct in_pktinfo*)CMSG_DATA(cmsg);
  929. addr = pkt->ipi_addr;
  930. if (unistimdebug) {
  931. ast_verb(0, "message received on address %s\n", ast_inet_ntoa(addr));
  932. }
  933. }
  934. }
  935. memcpy(&toAddr->sin_addr, &addr, sizeof(struct in_addr));
  936. return err;
  937. #else
  938. memcpy(toAddr, &public_ip, sizeof(*toAddr));
  939. return 0;
  940. #endif
  941. }
  942. /* Allocate memory & initialize structures for a new phone */
  943. /* addr_from : ip address of the phone */
  944. static struct unistimsession *create_client(const struct sockaddr_in *addr_from)
  945. {
  946. int tmp;
  947. struct unistimsession *s;
  948. if (!(s = ast_calloc(1, sizeof(*s))))
  949. return NULL;
  950. memcpy(&s->sin, addr_from, sizeof(struct sockaddr_in));
  951. if (get_to_address(unistimsock, &s->sout) < 0) {
  952. ast_free(s);
  953. return NULL;
  954. }
  955. s->sout.sin_family = AF_INET;
  956. if (unistimdebug) {
  957. ast_verb(0, "Creating a new entry for the phone from %s received via server ip %s\n",
  958. ast_inet_ntoa(addr_from->sin_addr), ast_inet_ntoa(s->sout.sin_addr));
  959. }
  960. ast_mutex_init(&s->lock);
  961. ast_mutex_lock(&sessionlock);
  962. s->next = sessions;
  963. sessions = s;
  964. s->timeout = get_tick_count() + RETRANSMIT_TIMER;
  965. s->state = STATE_INIT;
  966. s->tick_next_ping = get_tick_count() + unistim_keepalive;
  967. /* Initialize struct wsabuf */
  968. for (tmp = 0; tmp < MAX_BUF_NUMBER; tmp++) {
  969. s->wsabufsend[tmp].buf = s->buf[tmp];
  970. }
  971. ast_mutex_unlock(&sessionlock);
  972. return s;
  973. }
  974. static void send_end_call(struct unistimsession *pte)
  975. {
  976. BUFFSEND;
  977. if (unistimdebug) {
  978. ast_verb(0, "Sending end call\n");
  979. }
  980. memcpy(buffsend + SIZE_HEADER, packet_send_end_call, sizeof(packet_send_end_call));
  981. send_client(SIZE_HEADER + sizeof(packet_send_end_call), buffsend, pte);
  982. }
  983. static void set_ping_timer(struct unistimsession *pte)
  984. {
  985. unsigned int tick = 0; /* XXX what is this for, anyways */
  986. pte->timeout = pte->tick_next_ping;
  987. DEBUG_TIMER("tick = %u next ping at %u tick\n", tick, pte->timeout);
  988. return;
  989. }
  990. /* Checking if our send queue is empty,
  991. * if true, setting up a timer for keepalive */
  992. static void check_send_queue(struct unistimsession *pte)
  993. {
  994. /* Check if our send queue contained only one element */
  995. if (pte->last_buf_available == 1) {
  996. if (unistimdebug) {
  997. ast_verb(0, "Our single packet was ACKed.\n");
  998. }
  999. pte->last_buf_available--;
  1000. set_ping_timer(pte);
  1001. return;
  1002. }
  1003. /* Check if this ACK catch up our latest packet */
  1004. else if (pte->last_seq_ack + 1 == pte->seq_server + 1) {
  1005. if (unistimdebug) {
  1006. ast_verb(0, "Our send queue is completely ACKed.\n");
  1007. }
  1008. pte->last_buf_available = 0; /* Purge the send queue */
  1009. set_ping_timer(pte);
  1010. return;
  1011. }
  1012. if (unistimdebug) {
  1013. ast_verb(0, "We still have packets in our send queue\n");
  1014. }
  1015. return;
  1016. }
  1017. static void send_start_timer(struct unistimsession *pte)
  1018. {
  1019. BUFFSEND;
  1020. if (unistimdebug) {
  1021. ast_verb(0, "Sending start timer\n");
  1022. }
  1023. memcpy(buffsend + SIZE_HEADER, packet_send_start_timer, sizeof(packet_send_start_timer));
  1024. send_client(SIZE_HEADER + sizeof(packet_send_start_timer), buffsend, pte);
  1025. }
  1026. static void send_stop_timer(struct unistimsession *pte)
  1027. {
  1028. BUFFSEND;
  1029. if (unistimdebug) {
  1030. ast_verb(0, "Sending stop timer\n");
  1031. }
  1032. memcpy(buffsend + SIZE_HEADER, packet_send_stop_timer, sizeof(packet_send_stop_timer));
  1033. send_client(SIZE_HEADER + sizeof(packet_send_stop_timer), buffsend, pte);
  1034. }
  1035. static void send_icon(unsigned char pos, unsigned char status, struct unistimsession *pte)
  1036. {
  1037. BUFFSEND;
  1038. if (unistimdebug) {
  1039. ast_verb(0, "Sending icon pos %d with status 0x%02hhx\n", pos, status);
  1040. }
  1041. memcpy(buffsend + SIZE_HEADER, packet_send_icon, sizeof(packet_send_icon));
  1042. buffsend[9] = pos;
  1043. buffsend[10] = status;
  1044. send_client(SIZE_HEADER + sizeof(packet_send_icon), buffsend, pte);
  1045. }
  1046. static void send_expansion_next(struct unistimsession *pte)
  1047. {
  1048. BUFFSEND;
  1049. memcpy(buffsend + SIZE_HEADER, packet_send_expansion_next, sizeof(packet_send_expansion_next));
  1050. send_client(SIZE_HEADER + sizeof(packet_send_expansion_next), buffsend, pte);
  1051. }
  1052. static void send_expansion_icon(unsigned char pos, unsigned char status, struct unistimsession *pte)
  1053. {
  1054. BUFFSEND;
  1055. if (unistimdebug) {
  1056. ast_verb(0, "Sending expansion icon pos %d with status 0x%02hhx\n", pos, status);
  1057. }
  1058. memcpy(buffsend + SIZE_HEADER, packet_send_expansion_icon, sizeof(packet_send_expansion_icon));
  1059. buffsend[10] = pos;
  1060. buffsend[11] = status;
  1061. send_client(SIZE_HEADER + sizeof(packet_send_expansion_icon), buffsend, pte);
  1062. }
  1063. /* inverse : TEXT_INVERSE : yes, TEXT_NORMAL : no */
  1064. static void send_expansion_text(unsigned char pos, struct unistimsession *pte, const char *text)
  1065. {
  1066. int i;
  1067. BUFFSEND;
  1068. if (!text) {
  1069. ast_log(LOG_ERROR, "[expansion] Asked to display NULL text (pos %d)\n", pos);
  1070. return;
  1071. }
  1072. if (unistimdebug) {
  1073. ast_verb(0, "[expansion] Sending text at pos %d\n", pos);
  1074. }
  1075. memcpy(buffsend + SIZE_HEADER, packet_send_expansion_text, sizeof(packet_send_expansion_text));
  1076. buffsend[10] = pos;
  1077. i = strlen(text);
  1078. if (i > TEXT_LENGTH_MAX) {
  1079. i = TEXT_LENGTH_MAX;
  1080. }
  1081. memcpy(buffsend + 11, text, i);
  1082. send_client(SIZE_HEADER + sizeof(packet_send_expansion_text), buffsend, pte);
  1083. }
  1084. static void send_tone(struct unistimsession *pte, uint16_t tone1, uint16_t tone2)
  1085. {
  1086. BUFFSEND;
  1087. if (!tone1) {
  1088. if (unistimdebug) {
  1089. ast_verb(0, "Sending Stream Based Tone Off\n");
  1090. }
  1091. memcpy(buffsend + SIZE_HEADER, packet_send_stream_based_tone_off,
  1092. sizeof(packet_send_stream_based_tone_off));
  1093. send_client(SIZE_HEADER + sizeof(packet_send_stream_based_tone_off), buffsend, pte);
  1094. return;
  1095. }
  1096. /* Since most of the world use a continuous tone, it's useless
  1097. if (unistimdebug)
  1098. ast_verb(0, "Sending Stream Based Tone Cadence Download\n");
  1099. memcpy (buffsend + SIZE_HEADER, packet_send_StreamBasedToneCad, sizeof (packet_send_StreamBasedToneCad));
  1100. send_client (SIZE_HEADER + sizeof (packet_send_StreamBasedToneCad), buffsend, pte); */
  1101. if (unistimdebug) {
  1102. ast_verb(0, "Sending Stream Based Tone Frequency Component List Download %d %d\n", tone1, tone2);
  1103. }
  1104. tone1 *= 8;
  1105. if (!tone2) {
  1106. memcpy(buffsend + SIZE_HEADER, packet_send_stream_based_tone_single_freq,
  1107. sizeof(packet_send_stream_based_tone_single_freq));
  1108. put_unaligned_uint16(&buffsend[10], htons(tone1));
  1109. send_client(SIZE_HEADER + sizeof(packet_send_stream_based_tone_single_freq), buffsend,
  1110. pte);
  1111. } else {
  1112. tone2 *= 8;
  1113. memcpy(buffsend + SIZE_HEADER, packet_send_stream_based_tone_dual_freq,
  1114. sizeof(packet_send_stream_based_tone_dual_freq));
  1115. put_unaligned_uint16(&buffsend[10], htons(tone1));
  1116. put_unaligned_uint16(&buffsend[12], htons(tone2));
  1117. send_client(SIZE_HEADER + sizeof(packet_send_stream_based_tone_dual_freq), buffsend,
  1118. pte);
  1119. }
  1120. if (unistimdebug) {
  1121. ast_verb(0, "Sending Stream Based Tone On\n");
  1122. }
  1123. memcpy(buffsend + SIZE_HEADER, packet_send_stream_based_tone_on,
  1124. sizeof(packet_send_stream_based_tone_on));
  1125. send_client(SIZE_HEADER + sizeof(packet_send_stream_based_tone_on), buffsend, pte);
  1126. }
  1127. /* Positions for favorites
  1128. |--------------------|
  1129. | 5 2 | <-- not on screen in i2002
  1130. | 4 1 |
  1131. | 3 0 |
  1132. KEM Positions
  1133. |--------------------|
  1134. | 12 24 |
  1135. | 11 23 |
  1136. | 10 22 |
  1137. | 9 21 |
  1138. | 8 20 |
  1139. | 7 19 |
  1140. | 6 18 |
  1141. | 5 17 |
  1142. | 4 16 |
  1143. | 3 15 |
  1144. | 2 14 |
  1145. | 1 13 |
  1146. */
  1147. /* status (icons) : 00 = nothing, 2x/3x = see parser.h, 4x/5x = blink fast, 6x/7x = blink slow */
  1148. static void
  1149. send_favorite(unsigned char pos, unsigned char status, struct unistimsession *pte,
  1150. const char *text)
  1151. {
  1152. BUFFSEND;
  1153. int i;
  1154. if (unistimdebug) {
  1155. ast_verb(0, "Sending favorite pos %d with status 0x%02hhx\n", pos, status);
  1156. }
  1157. memcpy(buffsend + SIZE_HEADER, packet_send_favorite, sizeof(packet_send_favorite));
  1158. buffsend[10] = pos;
  1159. buffsend[24] = pos;
  1160. buffsend[25] = status;
  1161. i = strlen(ustmtext(text, pte));
  1162. if (i > FAV_MAX_LENGTH) {
  1163. i = FAV_MAX_LENGTH;
  1164. }
  1165. memcpy(buffsend + FAV_MAX_LENGTH + 1, ustmtext(text, pte), i);
  1166. send_client(SIZE_HEADER + sizeof(packet_send_favorite), buffsend, pte);
  1167. }
  1168. static void send_favorite_short(unsigned char pos, unsigned char status, struct unistimsession *pte) {
  1169. send_favorite(pos, status, pte, pte->device->softkeylabel[pos]);
  1170. return;
  1171. }
  1172. static void send_favorite_selected(unsigned char status, struct unistimsession *pte) {
  1173. if (pte->device->selected != -1) {
  1174. send_favorite(pte->device->selected, status, pte, pte->device->softkeylabel[pte->device->selected]);
  1175. }
  1176. return;
  1177. }
  1178. static void send_expansion_short(unsigned char pos, unsigned char status, struct unistimsession *pte) {
  1179. send_expansion_icon(pos, status, pte);
  1180. send_expansion_text(pos, pte, ustmtext(pte->device->expsoftkeylabel[pos], pte));
  1181. send_expansion_next(pte);
  1182. return;
  1183. }
  1184. static int soft_key_visible(struct unistim_device* d, unsigned char num)
  1185. {
  1186. if(d->height == 1 && num % 3 == 2) {
  1187. return 0;
  1188. }
  1189. return 1;
  1190. }
  1191. static void refresh_all_favorite(struct unistimsession *pte)
  1192. {
  1193. unsigned char i = 0;
  1194. char data[256];
  1195. struct unistim_line *line;
  1196. line = AST_LIST_FIRST(&pte->device->lines);
  1197. if (unistimdebug) {
  1198. ast_verb(0, "Refreshing all favorite\n");
  1199. }
  1200. for (i = 0; i < FAVNUM; i++) {
  1201. unsigned char status = pte->device->softkeyicon[i];
  1202. if (!soft_key_visible(pte->device, i)) {
  1203. continue;
  1204. }
  1205. if (!strcasecmp(pte->device->softkeylabel[i], "DND") && line) {
  1206. if (!ast_db_get("DND", line->name, data, sizeof(data))) {
  1207. status = FAV_ICON_SPEAKER_ONHOOK_WHITE;
  1208. }
  1209. }
  1210. send_favorite_short(i, status, pte);
  1211. }
  1212. if (pte->device->hasexp) {
  1213. for (i = 0; i < EXPNUM; i++) {
  1214. send_expansion_short(i, FAV_ICON_NONE, pte);
  1215. }
  1216. }
  1217. }
  1218. static int is_key_favorite(struct unistim_device *d, int fav)
  1219. {
  1220. if ((fav < 0) || (fav >= FAVNUM)) {
  1221. return 0;
  1222. }
  1223. if (d->sline[fav]) {
  1224. return 0;
  1225. }
  1226. if (d->softkeynumber[fav][0] == '\0') {
  1227. return 0;
  1228. }
  1229. return 1;
  1230. }
  1231. static int is_key_line(struct unistim_device *d, int fav)
  1232. {
  1233. if ((fav < 0) || (fav >= FAVNUM)) {
  1234. return 0;
  1235. }
  1236. if (!d->sline[fav]) {
  1237. return 0;
  1238. }
  1239. if (is_key_favorite(d, fav)) {
  1240. return 0;
  1241. }
  1242. return 1;
  1243. }
  1244. static int get_active_softkey(struct unistimsession *pte)
  1245. {
  1246. return pte->device->selected;
  1247. }
  1248. static int get_avail_softkey(struct unistimsession *pte, const char* name)
  1249. {
  1250. int i;
  1251. if (!is_key_line(pte->device, pte->device->selected)) {
  1252. pte->device->selected = -1;
  1253. }
  1254. for (i = 0; i < FAVNUM; i++) {
  1255. if (pte->device->selected != -1 && pte->device->selected != i) {
  1256. continue;
  1257. }
  1258. if (!soft_key_visible(pte->device, i)) {
  1259. continue;
  1260. }
  1261. if (pte->device->ssub[i]) {
  1262. continue;
  1263. }
  1264. if (is_key_line(pte->device, i)) {
  1265. if (name && strcmp(name, pte->device->sline[i]->name)) {
  1266. continue;
  1267. }
  1268. if (unistimdebug) {
  1269. ast_verb(0, "Found softkey %d for device %s\n", i, name);
  1270. }
  1271. return i;
  1272. }
  1273. }
  1274. return -1;
  1275. }
  1276. /* Change the status for this phone (pte) and update for each phones where pte is bookmarked
  1277. * use FAV_ICON_*_BLACK constant in status parameters */
  1278. static void change_favorite_icon(struct unistimsession *pte, unsigned char status)
  1279. {
  1280. struct unistim_device *d = devices;
  1281. int i;
  1282. /* Update the current phone line softkey icon */
  1283. if (pte->state != STATE_CLEANING) {
  1284. int softkeylinepos = get_active_softkey(pte);
  1285. if (softkeylinepos != -1) {
  1286. send_favorite_short(softkeylinepos, status, pte);
  1287. }
  1288. }
  1289. /* Notify other phones if we're in their bookmark */
  1290. while (d) {
  1291. for (i = 0; i < FAVNUM; i++) {
  1292. if (d->sp[i] == pte->device) { /* It's us ? */
  1293. if (d->softkeyicon[i] != status) { /* Avoid resending the same icon */
  1294. d->softkeyicon[i] = status;
  1295. if (d->session) {
  1296. send_favorite(i, status + 1, d->session, d->softkeylabel[i]);
  1297. }
  1298. }
  1299. }
  1300. }
  1301. d = d->next;
  1302. }
  1303. }
  1304. static int register_extension(const struct unistimsession *pte)
  1305. {
  1306. struct unistim_line *line;
  1307. line = AST_LIST_FIRST(&pte->device->lines);
  1308. if (unistimdebug) {
  1309. ast_verb(0, "Trying to register extension '%s' into context '%s' to %s\n",
  1310. pte->device->extension_number, pte->device->context,
  1311. line->fullname);
  1312. }
  1313. return ast_add_extension(pte->device->context, 0,
  1314. pte->device->extension_number, 1, NULL, NULL, "Dial",
  1315. line->fullname, 0, "Unistim");
  1316. }
  1317. static int unregister_extension(const struct unistimsession *pte)
  1318. {
  1319. if (unistimdebug) {
  1320. ast_verb(0, "Trying to unregister extension '%s' context '%s'\n",
  1321. pte->device->extension_number, pte->device->context);
  1322. }
  1323. return ast_context_remove_extension(pte->device->context,
  1324. pte->device->extension_number, 1, "Unistim");
  1325. }
  1326. /* Free memory allocated for a phone */
  1327. static void close_client(struct unistimsession *s)
  1328. {
  1329. struct unistim_subchannel *sub = NULL;
  1330. struct unistimsession *cur, *prev = NULL;
  1331. ast_mutex_lock(&sessionlock);
  1332. cur = sessions;
  1333. /* Looking for the session in the linked chain */
  1334. while (cur) {
  1335. if (cur == s) {
  1336. break;
  1337. }
  1338. prev = cur;
  1339. cur = cur->next;
  1340. }
  1341. if (cur) { /* Session found ? */
  1342. if (cur->device) { /* This session was registered ? */
  1343. s->state = STATE_CLEANING;
  1344. if (unistimdebug) {
  1345. ast_verb(0, "close_client session %p device %p\n", s, s->device);
  1346. }
  1347. change_favorite_icon(s, FAV_ICON_NONE);
  1348. ast_mutex_lock(&s->device->lock);
  1349. AST_LIST_LOCK(&s->device->subs);
  1350. AST_LIST_TRAVERSE_SAFE_BEGIN(&s->device->subs, sub, list) {
  1351. if (!sub) {
  1352. continue;
  1353. }
  1354. if (sub->owner) { /* Call in progress ? */
  1355. if (unistimdebug) {
  1356. ast_verb(0, "Aborting call\n");
  1357. }
  1358. ast_queue_hangup_with_cause(sub->owner, AST_CAUSE_NETWORK_OUT_OF_ORDER);
  1359. } else {
  1360. if (unistimdebug) {
  1361. ast_debug(1, "Released sub %u of channel %s@%s\n", sub->subtype, sub->parent->name, s->device->name);
  1362. }
  1363. AST_LIST_REMOVE_CURRENT(list);
  1364. unistim_free_sub(sub);
  1365. }
  1366. }
  1367. AST_LIST_TRAVERSE_SAFE_END;
  1368. AST_LIST_UNLOCK(&s->device->subs);
  1369. if (!ast_strlen_zero(s->device->extension_number)) {
  1370. unregister_extension(s);
  1371. }
  1372. cur->device->session = NULL;
  1373. ast_mutex_unlock(&s->device->lock);
  1374. } else {
  1375. if (unistimdebug) {
  1376. ast_verb(0, "Freeing an unregistered client\n");
  1377. }
  1378. }
  1379. if (prev) {
  1380. prev->next = cur->next;
  1381. } else {
  1382. sessions = cur->next;
  1383. }
  1384. ast_mutex_destroy(&s->lock);
  1385. ast_free(s);
  1386. } else {
  1387. ast_log(LOG_WARNING, "Trying to delete non-existent session %p?\n", s);
  1388. }
  1389. ast_mutex_unlock(&sessionlock);
  1390. return;
  1391. }
  1392. /* Return 1 if the session chained link was modified */
  1393. static int send_retransmit(struct unistimsession *pte)
  1394. {
  1395. int i;
  1396. ast_mutex_lock(&pte->lock);
  1397. if (++pte->nb_retransmit >= NB_MAX_RETRANSMIT) {
  1398. if (unistimdebug) {
  1399. ast_verb(0, "Too many retransmit - freeing client\n");
  1400. }
  1401. ast_mutex_unlock(&pte->lock);
  1402. close_client(pte);
  1403. return 1;
  1404. }
  1405. pte->timeout = get_tick_count() + RETRANSMIT_TIMER;
  1406. for (i = pte->last_buf_available - (pte->seq_server - pte->last_seq_ack);
  1407. i < pte->last_buf_available; i++) {
  1408. if (i < 0) {
  1409. ast_log(LOG_WARNING,
  1410. "Asked to retransmit an ACKed slot ! last_buf_available=%d, seq_server = #0x%04x last_seq_ack = #0x%04x\n",
  1411. pte->last_buf_available, (unsigned)pte->seq_server, (unsigned)pte->last_seq_ack);
  1412. continue;
  1413. }
  1414. if (unistimdebug) {
  1415. unsigned short *sbuf = (unsigned short *) pte->wsabufsend[i].buf;
  1416. unsigned short seq;
  1417. seq = ntohs(sbuf[1]);
  1418. ast_verb(0, "Retransmit slot #%d (seq=#0x%04x), last ack was #0x%04x\n", i,
  1419. (unsigned)seq, (unsigned)pte->last_seq_ack);
  1420. }
  1421. send_raw_client(pte->wsabufsend[i].len, pte->wsabufsend[i].buf, &pte->sin,
  1422. &pte->sout);
  1423. }
  1424. ast_mutex_unlock(&pte->lock);
  1425. return 0;
  1426. }
  1427. /* inverse : TEXT_INVERSE : yes, TEXT_NORMAL : no */
  1428. static void send_text(unsigned char pos, unsigned char inverse, struct unistimsession *pte,
  1429. const char *text)
  1430. {
  1431. int i;
  1432. BUFFSEND;
  1433. if (!text) {
  1434. ast_log(LOG_ERROR, "Asked to display NULL text (pos %d, inverse flag %d)\n", pos, inverse);
  1435. return;
  1436. }
  1437. if (pte->device && pte->device->height == 1 && pos != TEXT_LINE0) {
  1438. return;
  1439. }
  1440. if (unistimdebug) {
  1441. ast_verb(0, "Sending text at pos %d, inverse flag %d\n", pos, inverse);
  1442. }
  1443. memcpy(buffsend + SIZE_HEADER, packet_send_text, sizeof(packet_send_text));
  1444. buffsend[10] = pos;
  1445. buffsend[11] = inverse;
  1446. i = strlen(text);
  1447. if (i > TEXT_LENGTH_MAX) {
  1448. i = TEXT_LENGTH_MAX;
  1449. }
  1450. memcpy(buffsend + 12, text, i);
  1451. send_client(SIZE_HEADER + sizeof(packet_send_text), buffsend, pte);
  1452. }
  1453. static void send_text_status(struct unistimsession *pte, const char *text)
  1454. {
  1455. BUFFSEND;
  1456. int i;
  1457. if (unistimdebug) {
  1458. ast_verb(0, "Sending status text\n");
  1459. }
  1460. if (pte->device) {
  1461. if (pte->device->status_method == 1) { /* For new firmware and i2050 soft phone */
  1462. int n = strlen(text);
  1463. /* Must send individual button separately */
  1464. int j;
  1465. for (i = 0, j = 0; i < 4; i++, j += 7) {
  1466. int pos = 0x08 + (i * 0x20);
  1467. memcpy(buffsend + SIZE_HEADER, packet_send_status2,
  1468. sizeof(packet_send_status2));
  1469. buffsend[9] = pos;
  1470. memcpy(buffsend + 10, (j < n) ? (text + j) : " ", 7);
  1471. send_client(SIZE_HEADER + sizeof(packet_send_status2), buffsend, pte);
  1472. }
  1473. return;
  1474. }
  1475. }
  1476. memcpy(buffsend + SIZE_HEADER, packet_send_status, sizeof(packet_send_status));
  1477. i = strlen(text);
  1478. if (i > STATUS_LENGTH_MAX) {
  1479. i = STATUS_LENGTH_MAX;
  1480. }
  1481. memcpy(buffsend + 10, text, i);
  1482. send_client(SIZE_HEADER + sizeof(packet_send_status), buffsend, pte);
  1483. }
  1484. static void send_led_update(struct unistimsession *pte, unsigned char led)
  1485. {
  1486. BUFFSEND;
  1487. if (unistimdebug) {
  1488. ast_verb(0, "Sending led_update (%x)\n", (unsigned)led);
  1489. }
  1490. memcpy(buffsend + SIZE_HEADER, packet_send_led_update, sizeof(packet_send_led_update));
  1491. buffsend[9] = led;
  1492. send_client(SIZE_HEADER + sizeof(packet_send_led_update), buffsend, pte);
  1493. }
  1494. static void send_mute(struct unistimsession *pte, unsigned char mute)
  1495. {
  1496. /*
  1497. 0x00 = unmute TX, 0x01 = mute TX
  1498. 0x20 = unmute RX, 0x21 = mute RX
  1499. */
  1500. BUFFSEND;
  1501. if (unistimdebug) {
  1502. ast_verb(0, "Sending mute packet (%x)\n", (unsigned)mute);
  1503. }
  1504. memcpy(buffsend + SIZE_HEADER, packet_send_mute, sizeof(packet_send_mute));
  1505. buffsend[9] = mute;
  1506. send_client(SIZE_HEADER + sizeof(packet_send_mute), buffsend, pte);
  1507. }
  1508. /* output = OUTPUT_HANDSET, OUTPUT_HEADPHONE or OUTPUT_SPEAKER
  1509. * volume = VOLUME_LOW, VOLUME_NORMAL, VOLUME_INSANELY_LOUD
  1510. * mute = MUTE_OFF, MUTE_ON */
  1511. static void
  1512. send_select_output(struct unistimsession *pte, unsigned char output, unsigned char volume,
  1513. unsigned char mute)
  1514. {
  1515. BUFFSEND;
  1516. int mute_icon = -1;
  1517. if (unistimdebug) {
  1518. ast_verb(0, "Sending select output packet output=%x volume=%x mute=%x\n",
  1519. (unsigned)output, (unsigned)volume, (unsigned)mute);
  1520. }
  1521. memcpy(buffsend + SIZE_HEADER, packet_send_select_output,
  1522. sizeof(packet_send_select_output));
  1523. buffsend[9] = output;
  1524. if (output == OUTPUT_SPEAKER && volume == VOLUME_LOW) {
  1525. volume = VOLUME_LOW_SPEAKER;
  1526. }
  1527. buffsend[10] = volume;
  1528. if (mute == MUTE_ON_DISCRET) {
  1529. buffsend[11] = MUTE_ON;
  1530. } else {
  1531. buffsend[11] = mute;
  1532. }
  1533. send_client(SIZE_HEADER + sizeof(packet_send_select_output), buffsend, pte);
  1534. if (output == OUTPUT_HANDSET) {
  1535. mute_icon = (mute == MUTE_ON) ? FAV_ICON_ONHOLD_BLACK : FAV_ICON_OFFHOOK_BLACK;
  1536. send_led_update(pte, LED_SPEAKER_OFF);
  1537. send_led_update(pte, LED_HEADPHONE_OFF);
  1538. } else if (output == OUTPUT_HEADPHONE) {
  1539. mute_icon = (mute == MUTE_ON)? FAV_ICON_HEADPHONES_ONHOLD : FAV_ICON_HEADPHONES;
  1540. send_led_update(pte, LED_SPEAKER_OFF);
  1541. send_led_update(pte, LED_HEADPHONE_ON);
  1542. } else if (output == OUTPUT_SPEAKER) {
  1543. send_led_update(pte, LED_SPEAKER_ON);
  1544. send_led_update(pte, LED_HEADPHONE_OFF);
  1545. if (pte->device->receiver_state == STATE_OFFHOOK) {
  1546. mute_icon = (mute == MUTE_ON)? FAV_ICON_SPEAKER_ONHOLD_BLACK : FAV_ICON_SPEAKER_ONHOOK_BLACK;
  1547. } else {
  1548. mute_icon = (mute == MUTE_ON)? FAV_ICON_SPEAKER_ONHOLD_BLACK : FAV_ICON_SPEAKER_OFFHOOK_BLACK;
  1549. }
  1550. } else {
  1551. ast_log(LOG_WARNING, "Invalid output (%d)\n", output);
  1552. }
  1553. if (mute_icon != -1) {
  1554. change_favorite_icon(pte, mute_icon);
  1555. }
  1556. if (output != pte->device->output) {
  1557. pte->device->previous_output = pte->device->output;
  1558. }
  1559. pte->device->output = output;
  1560. }
  1561. static void send_ring(struct unistimsession *pte, signed char volume, signed char style)
  1562. {
  1563. BUFFSEND;
  1564. if (unistimdebug) {
  1565. ast_verb(0, "Sending ring packet\n");
  1566. }
  1567. memcpy(buffsend + SIZE_HEADER, packet_send_ring, sizeof(packet_send_ring));
  1568. buffsend[24] = style + 0x10;
  1569. buffsend[29] = volume * 0x10;
  1570. send_client(SIZE_HEADER + sizeof(packet_send_ring), buffsend, pte);
  1571. }
  1572. static void send_no_ring(struct unistimsession *pte)
  1573. {
  1574. BUFFSEND;
  1575. if (unistimdebug) {
  1576. ast_verb(0, "Sending no ring packet\n");
  1577. }
  1578. memcpy(buffsend + SIZE_HEADER, packet_send_no_ring, sizeof(packet_send_no_ring));
  1579. send_client(SIZE_HEADER + sizeof(packet_send_no_ring), buffsend, pte);
  1580. }
  1581. static void send_texttitle(struct unistimsession *pte, const char *text)
  1582. {
  1583. BUFFSEND;
  1584. int i;
  1585. if (unistimdebug) {
  1586. ast_verb(0, "Sending title text\n");
  1587. }
  1588. memcpy(buffsend + SIZE_HEADER, packet_send_title, sizeof(packet_send_title));
  1589. i = strlen(text);
  1590. if (i > 12) {
  1591. i = 12;
  1592. }
  1593. memcpy(buffsend + 10, text, i);
  1594. send_client(SIZE_HEADER + sizeof(packet_send_title), buffsend, pte);
  1595. }
  1596. static void send_idle_clock(struct unistimsession *pte)
  1597. {
  1598. send_text(TEXT_LINE0, TEXT_NORMAL, pte, "");
  1599. }
  1600. static void send_month_labels(struct unistimsession *pte, int month)
  1601. {
  1602. BUFFSEND;
  1603. char month_name[MONTH_LABEL_SIZE + 1];
  1604. int i = 0;
  1605. if (unistimdebug) {
  1606. ast_verb(0, "Sending Month Labels\n");
  1607. }
  1608. month_name[MONTH_LABEL_SIZE] = '\0';
  1609. memcpy(buffsend + SIZE_HEADER, packet_send_monthlabels_download, sizeof(packet_send_monthlabels_download));
  1610. while (i < 2) {
  1611. memcpy(month_name, &monthlabels[month * MONTH_LABEL_SIZE], MONTH_LABEL_SIZE);
  1612. memcpy(buffsend + SIZE_HEADER + 3 + i*MONTH_LABEL_SIZE, ustmtext(month_name, pte), MONTH_LABEL_SIZE);
  1613. ast_log(LOG_WARNING,"%s\n", month_name);
  1614. ast_log(LOG_WARNING,"%s\n", ustmtext(month_name, pte));
  1615. month = (month + 1)%12;
  1616. i++;
  1617. }
  1618. send_client(SIZE_HEADER + sizeof(packet_send_monthlabels_download), buffsend, pte);
  1619. }
  1620. static void send_date_time(struct unistimsession *pte)
  1621. {
  1622. BUFFSEND;
  1623. struct timeval now = ast_tvnow();
  1624. struct ast_tm atm = { 0, };
  1625. if (unistimdebug) {
  1626. ast_verb(0, "Sending Time & Date\n");
  1627. }
  1628. memcpy(buffsend + SIZE_HEADER, packet_send_date_time, sizeof(packet_send_date_time));
  1629. ast_localtime(&now, &atm, NULL);
  1630. buffsend[10] = (unsigned char) atm.tm_mon + 1;
  1631. buffsend[11] = (unsigned char) atm.tm_mday;
  1632. buffsend[12] = (unsigned char) atm.tm_hour;
  1633. buffsend[13] = (unsigned char) atm.tm_min;
  1634. send_client(SIZE_HEADER + sizeof(packet_send_date_time), buffsend, pte);
  1635. send_month_labels(pte, atm.tm_mon);
  1636. }
  1637. static void send_date_time2(struct unistimsession *pte)
  1638. {
  1639. BUFFSEND;
  1640. struct timeval now = ast_tvnow();
  1641. struct ast_tm atm = { 0, };
  1642. if (unistimdebug) {
  1643. ast_verb(0, "Sending Time & Date #2\n");
  1644. }
  1645. memcpy(buffsend + SIZE_HEADER, packet_send_date_time2, sizeof(packet_send_date_time2));
  1646. ast_localtime(&now, &atm, NULL);
  1647. if (pte->device) {
  1648. buffsend[9] = pte->device->datetimeformat;
  1649. } else {
  1650. buffsend[9] = 61;
  1651. }
  1652. buffsend[14] = (unsigned char) atm.tm_mon + 1;
  1653. buffsend[15] = (unsigned char) atm.tm_mday;
  1654. buffsend[16] = (unsigned char) atm.tm_hour;
  1655. buffsend[17] = (unsigned char) atm.tm_min;
  1656. send_client(SIZE_HEADER + sizeof(packet_send_date_time2), buffsend, pte);
  1657. }
  1658. static void send_date_time3(struct unistimsession *pte)
  1659. {
  1660. BUFFSEND;
  1661. struct timeval now = ast_tvnow();
  1662. struct ast_tm atm = { 0, };
  1663. if (unistimdebug) {
  1664. ast_verb(0, "Sending Time & Date #3\n");
  1665. }
  1666. memcpy(buffsend + SIZE_HEADER, packet_send_date_time3, sizeof(packet_send_date_time3));
  1667. ast_localtime(&now, &atm, NULL);
  1668. buffsend[10] = (unsigned char) atm.tm_mon + 1;
  1669. buffsend[11] = (unsigned char) atm.tm_mday;
  1670. buffsend[12] = (unsigned char) atm.tm_hour;
  1671. buffsend[13] = (unsigned char) atm.tm_min;
  1672. send_client(SIZE_HEADER + sizeof(packet_send_date_time3), buffsend, pte);
  1673. }
  1674. static void send_blink_cursor(struct unistimsession *pte)
  1675. {
  1676. BUFFSEND;
  1677. if (unistimdebug) {
  1678. ast_verb(0, "Sending set blink\n");
  1679. }
  1680. memcpy(buffsend + SIZE_HEADER, packet_send_blink_cursor, sizeof(packet_send_blink_cursor));
  1681. send_client(SIZE_HEADER + sizeof(packet_send_blink_cursor), buffsend, pte);
  1682. return;
  1683. }
  1684. /* pos : 0xab (a=0/2/4 = line ; b = row) */
  1685. static void send_cursor_pos(struct unistimsession *pte, unsigned char pos)
  1686. {
  1687. BUFFSEND;
  1688. if (unistimdebug) {
  1689. ast_verb(0, "Sending set cursor position\n");
  1690. }
  1691. memcpy(buffsend + SIZE_HEADER, packet_send_set_pos_cursor,
  1692. sizeof(packet_send_set_pos_cursor));
  1693. buffsend[11] = pos;
  1694. send_client(SIZE_HEADER + sizeof(packet_send_set_pos_cursor), buffsend, pte);
  1695. return;
  1696. }
  1697. static void send_charset_update(struct unistimsession *pte, int charset)
  1698. {
  1699. const unsigned char* packet_send_charset;
  1700. int packet_size;
  1701. BUFFSEND;
  1702. if (unistimdebug) {
  1703. ast_verb(0, "Sending set default charset\n");
  1704. }
  1705. if (charset == LANG_DEFAULT) {
  1706. charset = options_languages[find_language(pte->device->language)].encoding;
  1707. }
  1708. switch (charset) {
  1709. case ISO_8859_2:
  1710. packet_send_charset = packet_send_charset_iso_8859_2;
  1711. packet_size = sizeof(packet_send_charset_iso_8859_2);
  1712. break;
  1713. case ISO_8859_4:
  1714. packet_send_charset = packet_send_charset_iso_8859_4;
  1715. packet_size = sizeof(packet_send_charset_iso_8859_4);
  1716. break;
  1717. case ISO_8859_5:
  1718. packet_send_charset = packet_send_charset_iso_8859_5;
  1719. packet_size = sizeof(packet_send_charset_iso_8859_5);
  1720. break;
  1721. case ISO_2022_JP:
  1722. packet_send_charset = packet_send_charset_iso_2022_jp;
  1723. packet_size = sizeof(packet_send_charset_iso_2022_jp);
  1724. break;
  1725. case ISO_8859_1:
  1726. default:
  1727. packet_send_charset = packet_send_charset_iso_8859_1;
  1728. packet_size = sizeof(packet_send_charset_iso_8859_1);
  1729. }
  1730. memcpy(buffsend + SIZE_HEADER, packet_send_charset, packet_size);
  1731. send_client(SIZE_HEADER + packet_size, buffsend, pte);
  1732. return;
  1733. }
  1734. static void rcv_resume_connection_with_server(struct unistimsession *pte)
  1735. {
  1736. BUFFSEND;
  1737. if (unistimdebug) {
  1738. ast_verb(0, "ResumeConnectionWithServer received\n");
  1739. ast_verb(0, "Sending packet_send_query_mac_address\n");
  1740. }
  1741. memcpy(buffsend + SIZE_HEADER, packet_send_query_mac_address,
  1742. sizeof(packet_send_query_mac_address));
  1743. send_client(SIZE_HEADER + sizeof(packet_send_query_mac_address), buffsend, pte);
  1744. return;
  1745. }
  1746. static int unistim_register(struct unistimsession *s)
  1747. {
  1748. struct unistim_device *d;
  1749. ast_mutex_lock(&devicelock);
  1750. d = devices;
  1751. while (d) {
  1752. if (!strcasecmp(s->macaddr, d->id)) {
  1753. /* XXX Deal with IP authentication */
  1754. s->device = d;
  1755. d->session = s;
  1756. d->codec_number = DEFAULT_CODEC;
  1757. d->missed_call = 0;
  1758. d->receiver_state = STATE_ONHOOK;
  1759. break;
  1760. }
  1761. d = d->next;
  1762. }
  1763. ast_mutex_unlock(&devicelock);
  1764. if (!d) {
  1765. return 0;
  1766. }
  1767. return 1;
  1768. }
  1769. static void unistim_line_copy(struct unistim_line *dst, struct unistim_line *src)
  1770. {
  1771. struct ast_format_cap *tmp = src->cap;
  1772. memcpy(dst, src, sizeof(*dst)); /* this over writes the cap ptr, so we have to reset it */
  1773. src->cap = tmp;
  1774. ast_format_cap_append_from_cap(src->cap, dst->cap, AST_MEDIA_TYPE_UNKNOWN);
  1775. }
  1776. static struct unistim_line *unistim_line_destroy(struct unistim_line *l)
  1777. {
  1778. if (!l) {
  1779. return NULL;
  1780. }
  1781. ao2_ref(l->cap, -1);
  1782. ast_free(l);
  1783. return NULL;
  1784. }
  1785. static struct unistim_line *unistim_line_alloc(void)
  1786. {
  1787. struct unistim_line *l;
  1788. if (!(l = ast_calloc(1, sizeof(*l)))) {
  1789. return NULL;
  1790. }
  1791. if (!(l->cap = ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_DEFAULT))) {
  1792. ast_free(l);
  1793. return NULL;
  1794. }
  1795. return l;
  1796. }
  1797. static int unistim_free_sub(struct unistim_subchannel *sub) {
  1798. if (unistimdebug) {
  1799. ast_debug(1, "Released sub %u of channel %s@%s\n", sub->subtype, sub->parent->name, sub->parent->parent->name);
  1800. }
  1801. ast_mutex_destroy(&sub->lock);
  1802. ast_free(sub);
  1803. return 0;
  1804. }
  1805. static struct unistim_subchannel *unistim_alloc_sub(struct unistim_device *d, int x)
  1806. {
  1807. struct unistim_subchannel *sub;
  1808. if (!(sub = ast_calloc(1, sizeof(*sub)))) {
  1809. return NULL;
  1810. }
  1811. if (unistimdebug) {
  1812. ast_verb(3, "Allocating UNISTIM subchannel #%d on %s ptr=%p\n", x, d->name, sub);
  1813. }
  1814. sub->ss_thread = AST_PTHREADT_NULL;
  1815. sub->subtype = x;
  1816. AST_LIST_LOCK(&d->subs);
  1817. AST_LIST_INSERT_TAIL(&d->subs, sub, list);
  1818. AST_LIST_UNLOCK(&d->subs);
  1819. ast_mutex_init(&sub->lock);
  1820. return sub;
  1821. }
  1822. static int unistim_unalloc_sub(struct unistim_device *d, struct unistim_subchannel *sub)
  1823. {
  1824. struct unistim_subchannel *s;
  1825. AST_LIST_LOCK(&d->subs);
  1826. AST_LIST_TRAVERSE_SAFE_BEGIN(&d->subs, s, list) {
  1827. if (!s) {
  1828. continue;
  1829. }
  1830. if (s != sub) {
  1831. continue;
  1832. }
  1833. AST_LIST_REMOVE_CURRENT(list);
  1834. unistim_free_sub(sub);
  1835. }
  1836. AST_LIST_TRAVERSE_SAFE_END;
  1837. AST_LIST_UNLOCK(&d->subs);
  1838. return 0;
  1839. }
  1840. static const char *subtype_tostr(const int type)
  1841. {
  1842. switch (type) {
  1843. case SUB_REAL:
  1844. return "REAL";
  1845. case SUB_RING:
  1846. return "RINGING";
  1847. case SUB_THREEWAY:
  1848. return "THREEWAY";
  1849. }
  1850. return "UNKNOWN";
  1851. }
  1852. static const char *ptestate_tostr(const int type)
  1853. {
  1854. switch (type) {
  1855. case STATE_INIT:
  1856. return "INIT";
  1857. case STATE_AUTHDENY:
  1858. return "AUTHDENY";
  1859. case STATE_MAINPAGE:
  1860. return "MAINPAGE";
  1861. case STATE_EXTENSION:
  1862. return "EXTENSION";
  1863. case STATE_DIALPAGE:
  1864. return "DIALPAGE";
  1865. case STATE_RINGING:
  1866. return "RINGING";
  1867. case STATE_CALL:
  1868. return "CALL";
  1869. case STATE_SELECTOPTION:
  1870. return "SELECTOPTION";
  1871. case STATE_SELECTCODEC:
  1872. return "SELECTCODEC";
  1873. case STATE_SELECTLANGUAGE:
  1874. return "SELECTLANGUAGE";
  1875. case STATE_CLEANING:
  1876. return "CLEARING";
  1877. case STATE_HISTORY:
  1878. return "HISTORY";
  1879. }
  1880. return "UNKNOWN";
  1881. }
  1882. static void rcv_mac_addr(struct unistimsession *pte, const unsigned char *buf)
  1883. {
  1884. BUFFSEND;
  1885. int tmp, i = 0;
  1886. char addrmac[19];
  1887. int res = 0;
  1888. for (tmp = 15; tmp < 15 + SIZE_HEADER; tmp++) {
  1889. sprintf(&addrmac[i], "%02hhx", buf[tmp]);
  1890. i += 2;
  1891. }
  1892. if (unistimdebug) {
  1893. ast_verb(0, "MAC Address received: %s\n", addrmac);
  1894. }
  1895. strcpy(pte->macaddr, addrmac);
  1896. res = unistim_register(pte);
  1897. if (!res) {
  1898. switch (autoprovisioning) {
  1899. case AUTOPROVISIONING_NO:
  1900. ast_log(LOG_WARNING, "No entry found for this phone : %s\n", addrmac);
  1901. pte->state = STATE_AUTHDENY;
  1902. break;
  1903. case AUTOPROVISIONING_YES:
  1904. {
  1905. struct unistim_device *d = NULL, *newd = NULL;
  1906. struct unistim_line *newl = NULL, *l = NULL;
  1907. if (unistimdebug) {
  1908. ast_verb(0, "New phone, autoprovisioning on\n");
  1909. }
  1910. /* First : locate the [template] section */
  1911. ast_mutex_lock(&devicelock);
  1912. d = devices;
  1913. while (d) {
  1914. if (strcasecmp(d->name, "template")) {
  1915. d = d->next;
  1916. continue;
  1917. }
  1918. /* Found, cloning this entry */
  1919. if (!(newd = ast_malloc(sizeof(*newd)))) {
  1920. ast_mutex_unlock(&devicelock);
  1921. return;
  1922. }
  1923. memcpy(newd, d, sizeof(*newd));
  1924. ast_mutex_init(&newd->lock);
  1925. newd->lines.first = NULL;
  1926. newd->lines.last = NULL;
  1927. AST_LIST_LOCK(&d->lines);
  1928. AST_LIST_TRAVERSE(&d->lines, l, list) {
  1929. if (!(newl = unistim_line_alloc())) {
  1930. break;
  1931. }
  1932. unistim_line_copy(l, newl);
  1933. newl->parent = newd;
  1934. ast_copy_string(newl->name, l->name, sizeof(newl->name));
  1935. snprintf(newl->fullname, sizeof(newl->fullname), "USTM/%s@%s",
  1936. newl->name, newd->name);
  1937. snprintf(l->name, sizeof(l->name), "%d", atoi(l->name) + 1);
  1938. AST_LIST_LOCK(&newd->lines);
  1939. AST_LIST_INSERT_TAIL(&newd->lines, newl, list);
  1940. AST_LIST_UNLOCK(&newd->lines);
  1941. }
  1942. AST_LIST_UNLOCK(&d->lines);
  1943. if (!newl) {
  1944. ast_free(newd);
  1945. ast_mutex_unlock(&devicelock);
  1946. }
  1947. /* Ok, now updating some fields */
  1948. ast_copy_string(newd->id, addrmac, sizeof(newd->id));
  1949. ast_copy_string(newd->name, addrmac, sizeof(newd->name));
  1950. if (newd->extension == EXTENSION_NONE) {
  1951. newd->extension = EXTENSION_ASK;
  1952. }
  1953. newd->receiver_state = STATE_ONHOOK;
  1954. newd->session = pte;
  1955. newd->language[0] = '\0';
  1956. newd->to_delete = -1;
  1957. newd->next = NULL;
  1958. pte->device = newd;
  1959. /* Go to the end of the linked chain */
  1960. while (d->next) {
  1961. d = d->next;
  1962. }
  1963. d->next = newd;
  1964. d = newd;
  1965. break;
  1966. }
  1967. ast_mutex_unlock(&devicelock);
  1968. if (!d) {
  1969. ast_log(LOG_WARNING, "No entry [template] found in unistim.conf\n");
  1970. pte->state = STATE_AUTHDENY;
  1971. }
  1972. }
  1973. break;
  1974. case AUTOPROVISIONING_TN:
  1975. pte->state = STATE_AUTHDENY;
  1976. break;
  1977. default:
  1978. ast_log(LOG_WARNING, "Internal error : unknown autoprovisioning value = %u\n",
  1979. autoprovisioning);
  1980. }
  1981. }
  1982. if (pte->state != STATE_AUTHDENY) {
  1983. struct unistim_line *line;
  1984. struct unistim_subchannel *sub;
  1985. ast_verb(3, "Device '%s' successfuly registered\n", pte->device->name);
  1986. AST_LIST_LOCK(&pte->device->subs);
  1987. AST_LIST_TRAVERSE_SAFE_BEGIN(&pte->device->subs, sub, list) {
  1988. if (sub) {
  1989. ast_log(LOG_ERROR, "Subchannel lost sice reboot. Hanged channel may apear!\n");
  1990. AST_LIST_REMOVE_CURRENT(list);
  1991. ast_free(sub);
  1992. }
  1993. }
  1994. AST_LIST_TRAVERSE_SAFE_END;
  1995. AST_LIST_UNLOCK(&pte->device->subs);
  1996. switch (pte->device->extension) {
  1997. case EXTENSION_NONE:
  1998. pte->state = STATE_MAINPAGE;
  1999. break;
  2000. case EXTENSION_ASK:
  2001. /* Checking if we already have an extension number */
  2002. if (ast_strlen_zero(pte->device->extension_number)) {
  2003. pte->state = STATE_EXTENSION;
  2004. } else {
  2005. /* Yes, because of a phone reboot. We don't ask again for the TN */
  2006. if (register_extension(pte)) {
  2007. pte->state = STATE_EXTENSION;
  2008. } else {
  2009. pte->state = STATE_MAINPAGE;
  2010. }
  2011. }
  2012. break;
  2013. case EXTENSION_LINE:
  2014. line = AST_LIST_FIRST(&pte->device->lines);
  2015. ast_copy_string(pte->device->extension_number, line->name,
  2016. sizeof(pte->device->extension_number));
  2017. if (register_extension(pte)) {
  2018. pte->state = STATE_EXTENSION;
  2019. } else {
  2020. pte->state = STATE_MAINPAGE;
  2021. }
  2022. break;
  2023. case EXTENSION_TN:
  2024. /* If we are here, it's because of a phone reboot */
  2025. pte->state = STATE_MAINPAGE;
  2026. break;
  2027. default:
  2028. ast_log(LOG_WARNING, "Internal error, extension value unknown : %u\n",
  2029. pte->device->extension);
  2030. pte->state = STATE_AUTHDENY;
  2031. break;
  2032. }
  2033. }
  2034. if (pte->state == STATE_EXTENSION) {
  2035. if (pte->device->extension != EXTENSION_TN) {
  2036. pte->device->extension = EXTENSION_ASK;
  2037. }
  2038. pte->device->extension_number[0] = '\0';
  2039. }
  2040. if (unistimdebug) {
  2041. ast_verb(0, "\nSending S1\n");
  2042. }
  2043. memcpy(buffsend + SIZE_HEADER, packet_send_S1, sizeof(packet_send_S1));
  2044. send_client(SIZE_HEADER + sizeof(packet_send_S1), buffsend, pte);
  2045. if (unistimdebug) {
  2046. ast_verb(0, "Sending query_basic_manager_04\n");
  2047. }
  2048. memcpy(buffsend + SIZE_HEADER, packet_send_query_basic_manager_04,
  2049. sizeof(packet_send_query_basic_manager_04));
  2050. send_client(SIZE_HEADER + sizeof(packet_send_query_basic_manager_04), buffsend, pte);
  2051. if (unistimdebug) {
  2052. ast_verb(0, "Sending query_basic_manager_10\n");
  2053. }
  2054. memcpy(buffsend + SIZE_HEADER, packet_send_query_basic_manager_10,
  2055. sizeof(packet_send_query_basic_manager_10));
  2056. send_client(SIZE_HEADER + sizeof(packet_send_query_basic_manager_10), buffsend, pte);
  2057. send_date_time(pte);
  2058. return;
  2059. }
  2060. static int write_entry_history(struct unistimsession *pte, FILE * f, char c, char *line1)
  2061. {
  2062. if (fwrite(&c, 1, 1, f) != 1) {
  2063. display_last_error("Unable to write history log header.");
  2064. return -1;
  2065. }
  2066. if (fwrite(line1, TEXT_LENGTH_MAX, 1, f) != 1) {
  2067. display_last_error("Unable to write history entry - date.");
  2068. return -1;
  2069. }
  2070. if (fwrite(pte->device->lst_cid, TEXT_LENGTH_MAX, 1, f) != 1) {
  2071. display_last_error("Unable to write history entry - callerid.");
  2072. return -1;
  2073. }
  2074. if (fwrite(pte->device->lst_cnm, TEXT_LENGTH_MAX, 1, f) != 1) {
  2075. display_last_error("Unable to write history entry - callername.");
  2076. return -1;
  2077. }
  2078. return 0;
  2079. }
  2080. static int write_history(struct unistimsession *pte, char way, char ismissed)
  2081. {
  2082. char tmp[AST_CONFIG_MAX_PATH], tmp2[AST_CONFIG_MAX_PATH];
  2083. char line1[TEXT_LENGTH_MAX + 1];
  2084. char count = 0, *histbuf;
  2085. int size;
  2086. FILE *f, *f2;
  2087. struct timeval now = ast_tvnow();
  2088. struct ast_tm atm = { 0, };
  2089. if (!pte->device) {
  2090. return -1;
  2091. }
  2092. if (!pte->device->callhistory) {
  2093. return 0;
  2094. }
  2095. if (strchr(pte->device->name, '/') || (pte->device->name[0] == '.')) {
  2096. ast_log(LOG_WARNING, "Account code '%s' insecure for writing file\n",
  2097. pte->device->name);
  2098. return -1;
  2099. }
  2100. snprintf(tmp, sizeof(tmp), "%s/%s", ast_config_AST_LOG_DIR, USTM_LOG_DIR);
  2101. if (ast_mkdir(tmp, 0770)) {
  2102. ast_log(LOG_WARNING, "Unable to create directory for history\n");
  2103. return -1;
  2104. }
  2105. ast_localtime(&now, &atm, NULL);
  2106. if (ismissed) {
  2107. if (way == 'i') {
  2108. ast_copy_string(tmp2, ustmtext("Miss", pte), sizeof(tmp2));
  2109. } else {
  2110. ast_copy_string(tmp2, ustmtext("Fail", pte), sizeof(tmp2));
  2111. }
  2112. } else {
  2113. ast_copy_string(tmp2, ustmtext("Answ", pte), sizeof(tmp2));
  2114. }
  2115. snprintf(line1, sizeof(line1), "%04d/%02d/%02d %02d:%02d:%02d %s",
  2116. atm.tm_year + 1900, atm.tm_mon + 1, atm.tm_mday, atm.tm_hour,
  2117. atm.tm_min, atm.tm_sec, tmp2);
  2118. snprintf(tmp, sizeof(tmp), "%s/%s/%s-%c.csv", ast_config_AST_LOG_DIR,
  2119. USTM_LOG_DIR, pte->device->name, way);
  2120. if ((f = fopen(tmp, "r"))) {
  2121. struct stat bufstat;
  2122. if (stat(tmp, &bufstat)) {
  2123. display_last_error("Unable to stat history log.");
  2124. fclose(f);
  2125. return -1;
  2126. }
  2127. size = 1 + (MAX_ENTRY_LOG * TEXT_LENGTH_MAX * 3);
  2128. if (bufstat.st_size != size) {
  2129. ast_log(LOG_WARNING,
  2130. "History file %s has an incorrect size (%d instead of %d). It will be replaced by a new one.",
  2131. tmp, (int) bufstat.st_size, size);
  2132. fclose(f);
  2133. f = NULL;
  2134. count = 1;
  2135. }
  2136. }
  2137. /* If we can't open the log file, we create a brand new one */
  2138. if (!f) {
  2139. char c = 1;
  2140. int i;
  2141. if ((errno != ENOENT) && (count == 0)) {
  2142. display_last_error("Unable to open history log.");
  2143. return -1;
  2144. }
  2145. f = fopen(tmp, "w");
  2146. if (!f) {
  2147. display_last_error("Unable to create history log.");
  2148. return -1;
  2149. }
  2150. if (write_entry_history(pte, f, c, line1)) {
  2151. fclose(f);
  2152. return -1;
  2153. }
  2154. memset(line1, ' ', TEXT_LENGTH_MAX);
  2155. for (i = 3; i < MAX_ENTRY_LOG * 3; i++) {
  2156. if (fwrite(line1, TEXT_LENGTH_MAX, 1, f) != 1) {
  2157. display_last_error("Unable to write history entry - stuffing.");
  2158. fclose(f);
  2159. return -1;
  2160. }
  2161. }
  2162. if (fclose(f)) {
  2163. display_last_error("Unable to close history - creation.");
  2164. }
  2165. return 0;
  2166. }
  2167. /* We can open the log file, we create a temporary one, we add our entry and copy the rest */
  2168. if (fread(&count, 1, 1, f) != 1) {
  2169. display_last_error("Unable to read history header.");
  2170. fclose(f);
  2171. return -1;
  2172. }
  2173. if (count > MAX_ENTRY_LOG) {
  2174. ast_log(LOG_WARNING, "Invalid count in history header of %s (%d max %d)\n", tmp,
  2175. count, MAX_ENTRY_LOG);
  2176. fclose(f);
  2177. return -1;
  2178. }
  2179. snprintf(tmp2, sizeof(tmp2), "%s/%s/%s-%c.csv.tmp", ast_config_AST_LOG_DIR,
  2180. USTM_LOG_DIR, pte->device->name, way);
  2181. if (!(f2 = fopen(tmp2, "w"))) {
  2182. display_last_error("Unable to create temporary history log.");
  2183. fclose(f);
  2184. return -1;
  2185. }
  2186. if (++count > MAX_ENTRY_LOG) {
  2187. count = MAX_ENTRY_LOG;
  2188. }
  2189. if (write_entry_history(pte, f2, count, line1)) {
  2190. fclose(f);
  2191. fclose(f2);
  2192. return -1;
  2193. }
  2194. size = (MAX_ENTRY_LOG - 1) * TEXT_LENGTH_MAX * 3;
  2195. if (!(histbuf = ast_malloc(size))) {
  2196. fclose(f);
  2197. fclose(f2);
  2198. return -1;
  2199. }
  2200. if (fread(histbuf, size, 1, f) != 1) {
  2201. ast_free(histbuf);
  2202. fclose(f);
  2203. fclose(f2);
  2204. display_last_error("Unable to read previous history entries.");
  2205. return -1;
  2206. }
  2207. if (fwrite(histbuf, size, 1, f2) != 1) {
  2208. ast_free(histbuf);
  2209. fclose(f);
  2210. fclose(f2);
  2211. display_last_error("Unable to write previous history entries.");
  2212. return -1;
  2213. }
  2214. ast_free(histbuf);
  2215. if (fclose(f)) {
  2216. display_last_error("Unable to close history log.");
  2217. }
  2218. if (fclose(f2)) {
  2219. display_last_error("Unable to close temporary history log.");
  2220. }
  2221. if (unlink(tmp)) {
  2222. display_last_error("Unable to remove old history log.");
  2223. }
  2224. if (rename(tmp2, tmp)) {
  2225. display_last_error("Unable to rename new history log.");
  2226. }
  2227. return 0;
  2228. }
  2229. static int attempt_transfer(struct unistim_subchannel *p1, struct unistim_subchannel *p2)
  2230. {
  2231. RAII_VAR(struct ast_channel *, chana, NULL, ast_channel_unref);
  2232. RAII_VAR(struct ast_channel *, chanb, NULL, ast_channel_unref);
  2233. if (!p1->owner || !p2->owner) {
  2234. ast_log(LOG_WARNING, "Transfer attempted without dual ownership?\n");
  2235. return -1;
  2236. }
  2237. chana = ast_channel_ref(p1->owner);
  2238. chanb = ast_channel_ref(p2->owner);
  2239. switch (ast_bridge_transfer_attended(chana, chanb)) {
  2240. case AST_BRIDGE_TRANSFER_INVALID:
  2241. ast_log(LOG_WARNING, "Transfer failed. Invalid bridge setup\n");
  2242. break;
  2243. case AST_BRIDGE_TRANSFER_NOT_PERMITTED:
  2244. ast_log(LOG_WARNING, "Transfer not permitted\n");
  2245. break;
  2246. case AST_BRIDGE_TRANSFER_FAIL:
  2247. ast_log(LOG_WARNING, "Transfer encountered internal error\n");
  2248. break;
  2249. case AST_BRIDGE_TRANSFER_SUCCESS:
  2250. return 0;
  2251. }
  2252. /* Control only reaches this point if transfer has failed */
  2253. ast_softhangup_nolock(chana, AST_SOFTHANGUP_DEV);
  2254. ast_softhangup_nolock(chanb, AST_SOFTHANGUP_DEV);
  2255. return -1;
  2256. }
  2257. void change_callerid(struct unistimsession *pte, int type, char *callerid)
  2258. {
  2259. char *data;
  2260. int size;
  2261. if (type) {
  2262. data = pte->device->lst_cnm;
  2263. } else {
  2264. data = pte->device->lst_cid;
  2265. }
  2266. /* This is very nearly strncpy(), except that the remaining buffer
  2267. * is padded with ' ', instead of '\0' */
  2268. memset(data, ' ', TEXT_LENGTH_MAX);
  2269. size = strlen(callerid);
  2270. if (size > TEXT_LENGTH_MAX) {
  2271. size = TEXT_LENGTH_MAX;
  2272. }
  2273. memcpy(data, callerid, size);
  2274. }
  2275. static struct unistim_subchannel* get_sub(struct unistim_device *device, int type)
  2276. {
  2277. struct unistim_subchannel *sub = NULL;
  2278. AST_LIST_LOCK(&device->subs);
  2279. AST_LIST_TRAVERSE(&device->subs, sub, list) {
  2280. if (!sub) {
  2281. continue;
  2282. }
  2283. if (sub->subtype == type) {
  2284. break;
  2285. }
  2286. }
  2287. AST_LIST_UNLOCK(&device->subs);
  2288. return sub;
  2289. }
  2290. static struct unistim_subchannel* get_sub_holding(struct unistim_device *device, int type, int holding)
  2291. {
  2292. struct unistim_subchannel *sub = NULL;
  2293. AST_LIST_LOCK(&device->subs);
  2294. AST_LIST_TRAVERSE(&device->subs, sub, list) {
  2295. if (!sub) {
  2296. continue;
  2297. }
  2298. if (sub->subtype == type && sub->holding == holding) {
  2299. break;
  2300. }
  2301. }
  2302. AST_LIST_UNLOCK(&device->subs);
  2303. return sub;
  2304. }
  2305. static void sub_start_silence(struct unistimsession *pte, struct unistim_subchannel *sub)
  2306. {
  2307. /* Silence our channel */
  2308. if (!pte->device->silence_generator) {
  2309. pte->device->silence_generator =
  2310. ast_channel_start_silence_generator(sub->owner);
  2311. if (pte->device->silence_generator == NULL) {
  2312. ast_log(LOG_WARNING, "Unable to start a silence generator.\n");
  2313. } else if (unistimdebug) {
  2314. ast_verb(0, "Starting silence generator\n");
  2315. }
  2316. }
  2317. }
  2318. static void sub_stop_silence(struct unistimsession *pte, struct unistim_subchannel *sub)
  2319. {
  2320. /* Stop the silence generator */
  2321. if (pte->device->silence_generator) {
  2322. if (unistimdebug) {
  2323. ast_verb(0, "Stopping silence generator\n");
  2324. }
  2325. if (sub->owner) {
  2326. ast_channel_stop_silence_generator(sub->owner, pte->device->silence_generator);
  2327. } else {
  2328. ast_log(LOG_WARNING, "Trying to stop silence generator on a null channel!\n");
  2329. }
  2330. pte->device->silence_generator = NULL;
  2331. }
  2332. }
  2333. static void sub_hold(struct unistimsession *pte, struct unistim_subchannel *sub)
  2334. {
  2335. if (!sub) {
  2336. return;
  2337. }
  2338. sub->moh = 1;
  2339. sub->holding = 1;
  2340. send_favorite_short(sub->softkey, FAV_ICON_ONHOLD_BLACK + FAV_BLINK_SLOW, pte);
  2341. send_select_output(pte, pte->device->output, pte->device->volume, MUTE_ON);
  2342. send_stop_timer(pte);
  2343. if (sub->owner) {
  2344. ast_queue_hold(sub->owner, NULL);
  2345. }
  2346. return;
  2347. }
  2348. static void sub_unhold(struct unistimsession *pte, struct unistim_subchannel *sub)
  2349. {
  2350. struct unistim_subchannel *sub_real;
  2351. sub_real = get_sub(pte->device, SUB_REAL);
  2352. if (sub_real) {
  2353. sub_hold(pte, sub_real);
  2354. }
  2355. sub->moh = 0;
  2356. sub->holding = 0;
  2357. send_favorite_short(sub->softkey, FAV_ICON_OFFHOOK_BLACK, pte);
  2358. send_select_output(pte, pte->device->output, pte->device->volume, MUTE_OFF);
  2359. send_start_timer(pte);
  2360. if (sub->owner) {
  2361. ast_queue_unhold(sub->owner);
  2362. if (sub->rtp) {
  2363. send_start_rtp(sub);
  2364. }
  2365. }
  2366. return;
  2367. }
  2368. static void close_call(struct unistimsession *pte)
  2369. {
  2370. struct unistim_subchannel *sub, *sub_transf;
  2371. sub = get_sub(pte->device, SUB_REAL);
  2372. sub_transf = get_sub(pte->device, SUB_THREEWAY);
  2373. send_stop_timer(pte);
  2374. if (!sub) {
  2375. ast_log(LOG_WARNING, "Close call without sub\n");
  2376. return;
  2377. }
  2378. send_favorite_short(sub->softkey, FAV_LINE_ICON, pte);
  2379. if (sub->owner) {
  2380. sub->alreadygone = 1;
  2381. if (sub_transf) {
  2382. sub_transf->alreadygone = 1;
  2383. if (attempt_transfer(sub, sub_transf) < 0) {
  2384. ast_verb(0, "attempt_transfer failed.\n");
  2385. }
  2386. } else {
  2387. ast_queue_hangup(sub->owner);
  2388. }
  2389. } else {
  2390. if (sub_transf) {
  2391. if (sub_transf->owner) {
  2392. ast_queue_hangup_with_cause(sub_transf->owner, AST_CAUSE_NORMAL_CLEARING);
  2393. } else {
  2394. ast_log(LOG_WARNING, "threeway sub without owner\n");
  2395. }
  2396. } else {
  2397. ast_verb(0, "USTM(%s@%s-%d) channel already destroyed\n", sub->parent->name,
  2398. pte->device->name, sub->softkey);
  2399. }
  2400. }
  2401. change_callerid(pte, 0, pte->device->redial_number);
  2402. change_callerid(pte, 1, "");
  2403. write_history(pte, 'o', pte->device->missed_call);
  2404. pte->device->missed_call = 0;
  2405. show_main_page(pte);
  2406. return;
  2407. }
  2408. static void ignore_call(struct unistimsession *pte)
  2409. {
  2410. send_no_ring(pte);
  2411. return;
  2412. }
  2413. static void discard_call(struct unistimsession *pte)
  2414. {
  2415. struct unistim_subchannel* sub;
  2416. sub = get_sub(pte->device, SUB_RING);
  2417. if (!sub) {
  2418. return;
  2419. }
  2420. ast_queue_hangup_with_cause(sub->owner, AST_CAUSE_NORMAL_CLEARING);
  2421. return;
  2422. }
  2423. static void *unistim_ss(void *data)
  2424. {
  2425. struct ast_channel *chan = data;
  2426. struct unistim_subchannel *sub = ast_channel_tech_pvt(chan);
  2427. struct unistim_line *l = sub->parent;
  2428. struct unistimsession *s = l->parent->session;
  2429. int res;
  2430. if (!s) {
  2431. return NULL;
  2432. }
  2433. ast_verb(3, "Starting switch on '%s@%s-%d' to %s\n", l->name, l->parent->name, sub->softkey, s->device->phone_number);
  2434. ast_channel_lock(chan);
  2435. ast_channel_exten_set(chan, s->device->phone_number);
  2436. ast_setstate(chan, AST_STATE_RING);
  2437. ast_channel_unlock(chan);
  2438. ast_copy_string(s->device->redial_number, s->device->phone_number,
  2439. sizeof(s->device->redial_number));
  2440. res = ast_pbx_run(chan);
  2441. if (res) {
  2442. ast_log(LOG_WARNING, "PBX exited non-zero\n");
  2443. send_tone(s, 1000, 0);
  2444. }
  2445. return NULL;
  2446. }
  2447. static int find_rtp_port(struct unistim_subchannel *s)
  2448. {
  2449. struct unistim_subchannel *sub = NULL;
  2450. int rtp_start = s->parent->parent->rtp_port;
  2451. struct ast_sockaddr us_tmp;
  2452. struct sockaddr_in us = { 0, };
  2453. AST_LIST_LOCK(&s->parent->parent->subs);
  2454. AST_LIST_TRAVERSE(&s->parent->parent->subs, sub, list) {
  2455. if (!sub) {
  2456. continue;
  2457. }
  2458. if (sub->rtp) {
  2459. ast_rtp_instance_get_remote_address(sub->rtp, &us_tmp);
  2460. ast_sockaddr_to_sin(&us_tmp, &us);
  2461. if (htons(us.sin_port)) {
  2462. rtp_start = htons(us.sin_port) + 1;
  2463. break;
  2464. }
  2465. }
  2466. }
  2467. AST_LIST_UNLOCK(&s->parent->parent->subs);
  2468. return rtp_start;
  2469. }
  2470. static void send_start_rtp(struct unistim_subchannel *sub)
  2471. {
  2472. BUFFSEND;
  2473. int codec;
  2474. struct sockaddr_in public = { 0, };
  2475. struct sockaddr_in us = { 0, };
  2476. struct sockaddr_in sin = { 0, };
  2477. struct ast_sockaddr us_tmp;
  2478. struct ast_sockaddr sin_tmp;
  2479. struct unistimsession *pte;
  2480. ast_rtp_instance_get_local_address(sub->rtp, &us_tmp);
  2481. ast_sockaddr_to_sin(&us_tmp, &us);
  2482. ast_rtp_instance_get_remote_address(sub->rtp, &sin_tmp);
  2483. ast_sockaddr_to_sin(&sin_tmp, &sin);
  2484. /* Setting up RTP of the phone */
  2485. if (public_ip.sin_family == 0) { /* NAT IP override ? */
  2486. memcpy(&public, &us, sizeof(public)); /* No defined, using IP from recvmsg */
  2487. } else {
  2488. memcpy(&public, &public_ip, sizeof(public)); /* override */
  2489. }
  2490. if (unistimdebug) {
  2491. ast_verb(0, "RTP started : Our IP/port is : %s:%hd with codec %s\n",
  2492. ast_inet_ntoa(us.sin_addr),
  2493. htons(us.sin_port), ast_format_get_name(ast_channel_readformat(sub->owner)));
  2494. ast_verb(0, "Starting phone RTP stack. Our public IP is %s\n",
  2495. ast_inet_ntoa(public.sin_addr));
  2496. }
  2497. pte = sub->parent->parent->session;
  2498. codec = ast_rtp_codecs_payload_code_tx(ast_rtp_instance_get_codecs(sub->rtp),
  2499. 1, ast_channel_readformat(sub->owner), 0);
  2500. if ((ast_format_cmp(ast_channel_readformat(sub->owner), ast_format_ulaw) == AST_FORMAT_CMP_EQUAL) ||
  2501. (ast_format_cmp(ast_channel_readformat(sub->owner), ast_format_alaw) == AST_FORMAT_CMP_EQUAL)) {
  2502. if (unistimdebug) {
  2503. ast_verb(0, "Sending packet_send_rtp_packet_size for codec %d\n", codec);
  2504. }
  2505. memcpy(buffsend + SIZE_HEADER, packet_send_rtp_packet_size,
  2506. sizeof(packet_send_rtp_packet_size));
  2507. buffsend[10] = (int) codec & 0xffffffffLL;
  2508. send_client(SIZE_HEADER + sizeof(packet_send_rtp_packet_size), buffsend, pte);
  2509. }
  2510. if (unistimdebug) {
  2511. ast_verb(0, "Sending Jitter Buffer Parameters Configuration\n");
  2512. }
  2513. memcpy(buffsend + SIZE_HEADER, packet_send_jitter_buffer_conf,
  2514. sizeof(packet_send_jitter_buffer_conf));
  2515. send_client(SIZE_HEADER + sizeof(packet_send_jitter_buffer_conf), buffsend, pte);
  2516. if (pte->device->rtp_method != 0) {
  2517. uint16_t rtcpsin_port = ntohs(us.sin_port) + 1; /* RTCP port is RTP + 1 */
  2518. if (unistimdebug) {
  2519. ast_verb(0, "Sending OpenAudioStreamTX using method #%d\n", pte->device->rtp_method);
  2520. }
  2521. if (pte->device->rtp_method == 3) {
  2522. memcpy(buffsend + SIZE_HEADER, packet_send_open_audio_stream_tx3,
  2523. sizeof(packet_send_open_audio_stream_tx3));
  2524. } else {
  2525. memcpy(buffsend + SIZE_HEADER, packet_send_open_audio_stream_tx,
  2526. sizeof(packet_send_open_audio_stream_tx));
  2527. }
  2528. if (pte->device->rtp_method != 2) {
  2529. memcpy(buffsend + 28, &public.sin_addr, sizeof(public.sin_addr));
  2530. put_unaligned_uint16(&buffsend[20], sin.sin_port);
  2531. put_unaligned_uint16(&buffsend[22], htons(rtcpsin_port));
  2532. put_unaligned_uint16(&buffsend[24], us.sin_port);
  2533. put_unaligned_uint16(&buffsend[26], htons(rtcpsin_port));
  2534. } else {
  2535. memcpy(buffsend + 23, &public.sin_addr, sizeof(public.sin_addr));
  2536. put_unaligned_uint16(&buffsend[15], sin.sin_port);
  2537. put_unaligned_uint16(&buffsend[19], us.sin_port);
  2538. }
  2539. buffsend[11] = codec; /* rx */
  2540. buffsend[12] = codec; /* tx */
  2541. send_client(SIZE_HEADER + sizeof(packet_send_open_audio_stream_tx), buffsend, pte);
  2542. if (unistimdebug) {
  2543. ast_verb(0, "Sending OpenAudioStreamRX\n");
  2544. }
  2545. if (pte->device->rtp_method == 3) {
  2546. memcpy(buffsend + SIZE_HEADER, packet_send_open_audio_stream_rx3,
  2547. sizeof(packet_send_open_audio_stream_rx3));
  2548. } else {
  2549. memcpy(buffsend + SIZE_HEADER, packet_send_open_audio_stream_rx,
  2550. sizeof(packet_send_open_audio_stream_rx));
  2551. }
  2552. if (pte->device->rtp_method != 2) {
  2553. memcpy(buffsend + 28, &public.sin_addr, sizeof(public.sin_addr));
  2554. put_unaligned_uint16(&buffsend[20], sin.sin_port);
  2555. put_unaligned_uint16(&buffsend[22], htons(rtcpsin_port));
  2556. put_unaligned_uint16(&buffsend[24], us.sin_port);
  2557. put_unaligned_uint16(&buffsend[26], htons(rtcpsin_port));
  2558. } else {
  2559. memcpy(buffsend + 23, &public.sin_addr, sizeof(public.sin_addr));
  2560. put_unaligned_uint16(&buffsend[15], sin.sin_port);
  2561. put_unaligned_uint16(&buffsend[19], us.sin_port);
  2562. }
  2563. buffsend[11] = codec; /* rx */
  2564. buffsend[12] = codec; /* tx */
  2565. send_client(SIZE_HEADER + sizeof(packet_send_open_audio_stream_rx), buffsend, pte);
  2566. } else {
  2567. uint16_t rtcpsin_port = htons(us.sin_port) + 1; /* RTCP port is RTP + 1 */
  2568. if (unistimdebug) {
  2569. ast_verb(0, "Sending packet_send_call default method\n");
  2570. }
  2571. memcpy(buffsend + SIZE_HEADER, packet_send_call, sizeof(packet_send_call));
  2572. memcpy(buffsend + 53, &public.sin_addr, sizeof(public.sin_addr));
  2573. /* Destination port when sending RTP */
  2574. put_unaligned_uint16(&buffsend[49], us.sin_port);
  2575. /* Destination port when sending RTCP */
  2576. put_unaligned_uint16(&buffsend[51], htons(rtcpsin_port));
  2577. /* Codec */
  2578. buffsend[40] = codec;
  2579. buffsend[41] = codec;
  2580. if (ast_format_cmp(ast_channel_readformat(sub->owner), ast_format_ulaw) == AST_FORMAT_CMP_EQUAL) {
  2581. buffsend[42] = 1; /* 1 = 20ms (160 bytes), 2 = 40ms (320 bytes) */
  2582. } else if (ast_format_cmp(ast_channel_readformat(sub->owner), ast_format_alaw) == AST_FORMAT_CMP_EQUAL) {
  2583. buffsend[42] = 1; /* 1 = 20ms (160 bytes), 2 = 40ms (320 bytes) */
  2584. } else if (ast_format_cmp(ast_channel_readformat(sub->owner), ast_format_g723) == AST_FORMAT_CMP_EQUAL) {
  2585. buffsend[42] = 2; /* 1 = 30ms (24 bytes), 2 = 60 ms (48 bytes) */
  2586. } else if (ast_format_cmp(ast_channel_readformat(sub->owner), ast_format_g729) == AST_FORMAT_CMP_EQUAL) {
  2587. buffsend[42] = 2; /* 1 = 10ms (10 bytes), 2 = 20ms (20 bytes) */
  2588. } else {
  2589. ast_log(LOG_WARNING, "Unsupported codec %s!\n",
  2590. ast_format_get_name(ast_channel_readformat(sub->owner)));
  2591. }
  2592. /* Source port for transmit RTP and Destination port for receiving RTP */
  2593. put_unaligned_uint16(&buffsend[45], sin.sin_port);
  2594. put_unaligned_uint16(&buffsend[47], htons(rtcpsin_port));
  2595. send_client(SIZE_HEADER + sizeof(packet_send_call), buffsend, pte);
  2596. }
  2597. }
  2598. static void start_rtp(struct unistim_subchannel *sub)
  2599. {
  2600. struct sockaddr_in sin = { 0, };
  2601. struct sockaddr_in sout = { 0, };
  2602. struct ast_sockaddr sin_tmp;
  2603. struct ast_sockaddr sout_tmp;
  2604. /* Sanity checks */
  2605. if (!sub) {
  2606. ast_log(LOG_WARNING, "start_rtp with a null subchannel !\n");
  2607. return;
  2608. }
  2609. if (!sub->parent) {
  2610. ast_log(LOG_WARNING, "start_rtp with a null line!\n");
  2611. return;
  2612. }
  2613. if (!sub->parent->parent) {
  2614. ast_log(LOG_WARNING, "start_rtp with a null device!\n");
  2615. return;
  2616. }
  2617. if (!sub->parent->parent->session) {
  2618. ast_log(LOG_WARNING, "start_rtp with a null session!\n");
  2619. return;
  2620. }
  2621. if (!sub->owner) {
  2622. ast_log(LOG_WARNING, "start_rtp with a null asterisk channel!\n");
  2623. return;
  2624. }
  2625. sout = sub->parent->parent->session->sout;
  2626. ast_mutex_lock(&sub->lock);
  2627. /* Allocate the RTP */
  2628. if (unistimdebug) {
  2629. ast_verb(0, "Starting RTP. Bind on %s\n", ast_inet_ntoa(sout.sin_addr));
  2630. }
  2631. ast_sockaddr_from_sin(&sout_tmp, &sout);
  2632. sub->rtp = ast_rtp_instance_new("asterisk", sched, &sout_tmp, NULL);
  2633. if (!sub->rtp) {
  2634. ast_log(LOG_WARNING, "Unable to create RTP session: %s binaddr=%s\n",
  2635. strerror(errno), ast_inet_ntoa(sout.sin_addr));
  2636. ast_mutex_unlock(&sub->lock);
  2637. return;
  2638. }
  2639. ast_rtp_instance_set_prop(sub->rtp, AST_RTP_PROPERTY_RTCP, 1);
  2640. ast_rtp_instance_set_channel_id(sub->rtp, ast_channel_uniqueid(sub->owner));
  2641. ast_channel_internal_fd_set(sub->owner, 0, ast_rtp_instance_fd(sub->rtp, 0));
  2642. ast_channel_internal_fd_set(sub->owner, 1, ast_rtp_instance_fd(sub->rtp, 1));
  2643. ast_rtp_instance_set_qos(sub->rtp, qos.tos_audio, qos.cos_audio, "UNISTIM RTP");
  2644. ast_rtp_instance_set_prop(sub->rtp, AST_RTP_PROPERTY_NAT, sub->parent->parent->nat);
  2645. /* Create the RTP connection */
  2646. sin.sin_family = AF_INET;
  2647. /* Setting up RTP for our side */
  2648. memcpy(&sin.sin_addr, &sub->parent->parent->session->sin.sin_addr,
  2649. sizeof(sin.sin_addr));
  2650. sin.sin_port = htons(find_rtp_port(sub));
  2651. ast_sockaddr_from_sin(&sin_tmp, &sin);
  2652. ast_rtp_instance_set_remote_address(sub->rtp, &sin_tmp);
  2653. if (ast_format_cap_iscompatible_format(ast_channel_nativeformats(sub->owner), ast_channel_readformat(sub->owner)) == AST_FORMAT_CMP_NOT_EQUAL) {
  2654. struct ast_format *tmpfmt;
  2655. struct ast_str *cap_buf = ast_str_alloca(AST_FORMAT_CAP_NAMES_LEN);
  2656. tmpfmt = ast_format_cap_get_format(ast_channel_nativeformats(sub->owner), 0);
  2657. ast_log(LOG_WARNING,
  2658. "Our read/writeformat has been changed to something incompatible: %s, using %s best codec from %s\n",
  2659. ast_format_get_name(ast_channel_readformat(sub->owner)),
  2660. ast_format_get_name(tmpfmt),
  2661. ast_format_cap_get_names(ast_channel_nativeformats(sub->owner), &cap_buf));
  2662. ast_channel_set_readformat(sub->owner, tmpfmt);
  2663. ast_channel_set_writeformat(sub->owner, tmpfmt);
  2664. ao2_ref(tmpfmt, -1);
  2665. }
  2666. send_start_rtp(sub);
  2667. ast_mutex_unlock(&sub->lock);
  2668. }
  2669. static void send_dial_tone(struct unistimsession *pte)
  2670. {
  2671. struct ast_tone_zone_sound *ts = NULL;
  2672. struct ast_tone_zone_part tone_data;
  2673. char *s = NULL;
  2674. char *ind;
  2675. if ((ts = ast_get_indication_tone(pte->device->tz, "dial"))) {
  2676. ind = ast_strdupa(ts->data);
  2677. s = strsep(&ind, ",");
  2678. ast_tone_zone_part_parse(s, &tone_data);
  2679. send_tone(pte, tone_data.freq1, tone_data.freq2);
  2680. if (unistimdebug) {
  2681. ast_verb(0, "Country code found (%s), freq1=%u freq2=%u\n",
  2682. pte->device->tz->country, tone_data.freq1, tone_data.freq2);
  2683. }
  2684. ts = ast_tone_zone_sound_unref(ts);
  2685. }
  2686. }
  2687. static void show_phone_number(struct unistimsession *pte)
  2688. {
  2689. char tmp[TEXT_LENGTH_MAX + 1];
  2690. const char *tmp_number = ustmtext("Number:", pte);
  2691. int line, tmp_copy, offset = 0, i;
  2692. pte->device->phone_number[pte->device->size_phone_number] = '\0';
  2693. if (pte->device->size_phone_number > MAX_SCREEN_NUMBER) {
  2694. offset = pte->device->size_phone_number - MAX_SCREEN_NUMBER - 1;
  2695. if (offset > strlen(tmp_number)) {
  2696. offset = strlen(tmp_number);
  2697. }
  2698. tmp_copy = strlen(tmp_number) - offset + 1;
  2699. if (tmp_copy > sizeof(tmp)) {
  2700. tmp_copy = sizeof(tmp);
  2701. }
  2702. memcpy(tmp, tmp_number + offset, tmp_copy);
  2703. } else {
  2704. ast_copy_string(tmp, tmp_number, sizeof(tmp));
  2705. }
  2706. offset = (pte->device->size_phone_number >= TEXT_LENGTH_MAX) ? (pte->device->size_phone_number - TEXT_LENGTH_MAX +1) : 0;
  2707. if (pte->device->size_phone_number) {
  2708. memcpy(tmp + strlen(tmp), pte->device->phone_number + offset, pte->device->size_phone_number - offset + 1);
  2709. }
  2710. offset = strlen(tmp);
  2711. for (i = strlen(tmp); i < TEXT_LENGTH_MAX; i++) {
  2712. tmp[i] = '.';
  2713. }
  2714. tmp[i] = '\0';
  2715. line = (pte->device->height == 1) ? TEXT_LINE0 : TEXT_LINE2;
  2716. send_text(line, TEXT_NORMAL, pte, tmp);
  2717. send_blink_cursor(pte);
  2718. send_cursor_pos(pte, (unsigned char) (line + offset));
  2719. send_led_update(pte, LED_BAR_OFF);
  2720. }
  2721. static void handle_dial_page(struct unistimsession *pte)
  2722. {
  2723. pte->state = STATE_DIALPAGE;
  2724. if (pte->device->call_forward[0] == -1) {
  2725. send_text(TEXT_LINE0, TEXT_NORMAL, pte, "");
  2726. send_text(TEXT_LINE1, TEXT_NORMAL, pte, ustmtext("Enter forward", pte));
  2727. send_text_status(pte, ustmtext("Fwd Cancel BackSp Erase", pte));
  2728. if (pte->device->call_forward[1] != 0) {
  2729. ast_copy_string(pte->device->phone_number, pte->device->call_forward + 1,
  2730. sizeof(pte->device->phone_number));
  2731. show_phone_number(pte);
  2732. send_led_update(pte, LED_BAR_OFF);
  2733. return;
  2734. }
  2735. } else {
  2736. if ((pte->device->output == OUTPUT_HANDSET) &&
  2737. (pte->device->receiver_state == STATE_ONHOOK)) {
  2738. send_select_output(pte, OUTPUT_SPEAKER, pte->device->volume, MUTE_OFF);
  2739. } else {
  2740. send_select_output(pte, pte->device->output, pte->device->volume, MUTE_OFF);
  2741. }
  2742. send_dial_tone(pte);
  2743. if (pte->device->height > 1) {
  2744. send_text(TEXT_LINE0, TEXT_NORMAL, pte, ustmtext("Enter the number to dial", pte));
  2745. send_text(TEXT_LINE1, TEXT_NORMAL, pte, ustmtext("and press Call", pte));
  2746. }
  2747. if (ast_strlen_zero(pte->device->redial_number)) {
  2748. send_text_status(pte, ustmtext("Call BackSp Erase", pte));
  2749. } else {
  2750. send_text_status(pte, ustmtext("Call Redial BackSp Erase", pte));
  2751. }
  2752. }
  2753. pte->device->size_phone_number = 0;
  2754. pte->device->phone_number[0] = 0;
  2755. show_phone_number(pte);
  2756. change_favorite_icon(pte, FAV_ICON_PHONE_BLACK);
  2757. send_icon(TEXT_LINE0, FAV_ICON_NONE, pte);
  2758. pte->device->missed_call = 0;
  2759. send_led_update(pte, LED_BAR_OFF);
  2760. pte->device->lastmsgssent = -1;
  2761. return;
  2762. }
  2763. static void swap_subs(struct unistim_subchannel *a, struct unistim_subchannel *b)
  2764. {
  2765. struct ast_rtp_instance *rtp;
  2766. int fds;
  2767. if (unistimdebug) {
  2768. ast_verb(0, "Swapping %p and %p\n", a, b);
  2769. }
  2770. if ((!a->owner) || (!b->owner)) {
  2771. ast_log(LOG_WARNING,
  2772. "Attempted to swap subchannels with a null owner : sub #%p=%p sub #%p=%p\n",
  2773. a, a->owner, b, b->owner);
  2774. return;
  2775. }
  2776. rtp = a->rtp;
  2777. a->rtp = b->rtp;
  2778. b->rtp = rtp;
  2779. fds = ast_channel_fd(a->owner, 0);
  2780. ast_channel_internal_fd_set(a->owner, 0, ast_channel_fd(b->owner, 0));
  2781. ast_channel_internal_fd_set(b->owner, 0, fds);
  2782. fds = ast_channel_fd(a->owner, 1);
  2783. ast_channel_internal_fd_set(a->owner, 1, ast_channel_fd(b->owner, 1));
  2784. ast_channel_internal_fd_set(b->owner, 1, fds);
  2785. }
  2786. /* Step 1 : Music On Hold for peer, Dialing screen for us */
  2787. static void transfer_call_step1(struct unistimsession *pte)
  2788. {
  2789. struct unistim_subchannel *sub /*, *sub_trans */;
  2790. struct unistim_device *d = pte->device;
  2791. sub = get_sub(d, SUB_REAL);
  2792. /* sub_trans = get_sub(d, SUB_THREEWAY); */
  2793. if (!sub || !sub->owner) {
  2794. ast_log(LOG_WARNING, "Unable to find subchannel for music on hold\n");
  2795. return;
  2796. }
  2797. /* Start music on hold if appropriate */
  2798. if (sub->moh) {
  2799. ast_log(LOG_WARNING, "Transfer with peer already listening music on hold\n");
  2800. } else {
  2801. ast_queue_hold(sub->owner, sub->parent->musicclass);
  2802. sub->moh = 1;
  2803. sub->subtype = SUB_THREEWAY;
  2804. }
  2805. sub_start_silence(pte, sub);
  2806. handle_dial_page(pte);
  2807. }
  2808. static void transfer_cancel_step2(struct unistimsession *pte)
  2809. {
  2810. struct unistim_subchannel *sub, *sub_trans;
  2811. struct unistim_device *d = pte->device;
  2812. sub = get_sub(d, SUB_REAL);
  2813. sub_trans = get_sub(d, SUB_THREEWAY);
  2814. if (!sub || !sub->owner) {
  2815. ast_log(LOG_WARNING, "Unable to find subchannel for music on hold\n");
  2816. return;
  2817. }
  2818. if (sub_trans) {
  2819. if (unistimdebug) {
  2820. ast_verb(0, "Transfer canceled, hangup our threeway channel\n");
  2821. }
  2822. if (sub->owner) {
  2823. swap_subs(sub, sub_trans);
  2824. ast_queue_unhold(sub_trans->owner);
  2825. sub_trans->moh = 0;
  2826. sub_trans->subtype = SUB_REAL;
  2827. sub->subtype = SUB_THREEWAY;
  2828. ast_queue_hangup_with_cause(sub->owner, AST_CAUSE_NORMAL_CLEARING);
  2829. } else {
  2830. ast_log(LOG_WARNING, "Canceling a threeway channel without owner\n");
  2831. }
  2832. return;
  2833. }
  2834. }
  2835. /* From phone to PBX */
  2836. static void handle_call_outgoing(struct unistimsession *s)
  2837. {
  2838. struct ast_channel *c;
  2839. struct unistim_subchannel *sub;
  2840. int softkey;
  2841. s->state = STATE_CALL;
  2842. sub = get_sub(s->device, SUB_THREEWAY);
  2843. if (sub) {
  2844. /* If sub for threway call created than we use transfer behavior */
  2845. struct unistim_subchannel *sub_trans = NULL;
  2846. struct unistim_device *d = s->device;
  2847. sub_trans = get_sub(d, SUB_REAL);
  2848. if (sub_trans) {
  2849. ast_log(LOG_WARNING, "Can't transfer while active subchannel exists!\n");
  2850. return;
  2851. }
  2852. if (!sub->owner) {
  2853. ast_log(LOG_WARNING, "Unable to find subchannel with music on hold\n");
  2854. return;
  2855. }
  2856. sub_trans = unistim_alloc_sub(d, SUB_REAL);
  2857. if (!sub_trans) {
  2858. ast_log(LOG_WARNING, "Unable to allocate three-way subchannel\n");
  2859. return;
  2860. }
  2861. sub_trans->parent = sub->parent;
  2862. sub_stop_silence(s, sub);
  2863. send_tone(s, 0, 0);
  2864. /* Make new channel */
  2865. c = unistim_new(sub_trans, AST_STATE_DOWN, NULL, NULL);
  2866. if (!c) {
  2867. ast_log(LOG_WARNING, "Cannot allocate new structure on channel %p\n", sub->parent);
  2868. return;
  2869. }
  2870. /* Swap things around between the three-way and real call */
  2871. swap_subs(sub, sub_trans);
  2872. send_select_output(s, s->device->output, s->device->volume, MUTE_OFF);
  2873. if (s->device->height == 1) {
  2874. send_text(TEXT_LINE0, TEXT_NORMAL, s, s->device->phone_number);
  2875. } else {
  2876. send_text(TEXT_LINE0, TEXT_NORMAL, s, ustmtext("Calling (pre-transfer)", s));
  2877. send_text(TEXT_LINE1, TEXT_NORMAL, s, s->device->phone_number);
  2878. send_text(TEXT_LINE2, TEXT_NORMAL, s, ustmtext("Dialing...", s));
  2879. }
  2880. send_text_status(s, ustmtext("TransfrCancel", s));
  2881. if (ast_pthread_create(&sub->ss_thread, NULL, unistim_ss, c)) {
  2882. ast_log(LOG_WARNING, "Unable to start simple switch on channel %p\n", c);
  2883. sub->ss_thread = AST_PTHREADT_NULL;
  2884. ast_hangup(c);
  2885. return;
  2886. }
  2887. if (unistimdebug) {
  2888. ast_verb(0, "Started three way call on channel %p (%s) subchan %u\n",
  2889. sub_trans->owner, ast_channel_name(sub_trans->owner), sub_trans->subtype);
  2890. }
  2891. return;
  2892. }
  2893. softkey = get_avail_softkey(s, NULL);
  2894. if (softkey == -1) {
  2895. ast_log(LOG_WARNING, "Have no avail softkey for calling\n");
  2896. return;
  2897. }
  2898. sub = get_sub(s->device, SUB_REAL);
  2899. if (sub) { /* have already call assigned */
  2900. sub_hold(s, sub); /* Need to put on hold */
  2901. }
  2902. if (!(sub = unistim_alloc_sub(s->device, SUB_REAL))) {
  2903. ast_log(LOG_WARNING, "Unable to allocate subchannel!\n");
  2904. return;
  2905. }
  2906. sub->parent = s->device->sline[softkey];
  2907. s->device->ssub[softkey] = sub;
  2908. sub->softkey = softkey;
  2909. if (unistimdebug) {
  2910. ast_verb(0, "Using softkey %d, line %p\n", sub->softkey, sub->parent);
  2911. }
  2912. send_favorite_short(sub->softkey, FAV_ICON_OFFHOOK_BLACK, s);
  2913. s->device->selected = -1;
  2914. if (!sub->owner) { /* A call is already in progress ? */
  2915. RAII_VAR(struct ast_features_pickup_config *, pickup_cfg, NULL, ao2_cleanup);
  2916. const char *pickupexten;
  2917. c = unistim_new(sub, AST_STATE_DOWN, NULL, NULL); /* No, starting a new one */
  2918. if (!sub->rtp) { /* Need to start RTP before calling ast_pbx_run */
  2919. start_rtp(sub);
  2920. }
  2921. if (c) {
  2922. ast_channel_lock(c);
  2923. pickup_cfg = ast_get_chan_features_pickup_config(c);
  2924. if (!pickup_cfg) {
  2925. ast_log(LOG_ERROR, "Unable to retrieve pickup configuration options. Unable to detect call pickup extension\n");
  2926. pickupexten = "";
  2927. } else {
  2928. pickupexten = ast_strdupa(pickup_cfg->pickupexten);
  2929. }
  2930. ast_channel_unlock(c);
  2931. }
  2932. if (c && !strcmp(s->device->phone_number, pickupexten)) {
  2933. if (unistimdebug) {
  2934. ast_verb(0, "Try to pickup in unistim_new\n");
  2935. }
  2936. send_text(TEXT_LINE0, TEXT_NORMAL, s, "");
  2937. send_text_status(s, ustmtext(" Transf Hangup", s));
  2938. send_start_timer(s);
  2939. if (ast_pickup_call(c)) {
  2940. ast_log(LOG_NOTICE, "Nothing to pick up\n");
  2941. ast_channel_hangupcause_set(c, AST_CAUSE_CALL_REJECTED);
  2942. } else {
  2943. ast_channel_hangupcause_set(c, AST_CAUSE_NORMAL_CLEARING);
  2944. }
  2945. ast_hangup(c);
  2946. c = NULL;
  2947. } else if (c) {
  2948. send_select_output(s, s->device->output, s->device->volume, MUTE_OFF);
  2949. send_tone(s, 0, 0); /* Dialing empty number should also stop dial tone */
  2950. if (s->device->height == 1) {
  2951. if (strlen(s->device->phone_number) > 0) {
  2952. send_text(TEXT_LINE0, TEXT_NORMAL, s, s->device->phone_number);
  2953. } else {
  2954. send_text(TEXT_LINE0, TEXT_NORMAL, s, ustmtext("Calling...", s));
  2955. }
  2956. } else {
  2957. send_text(TEXT_LINE0, TEXT_NORMAL, s, ustmtext("Calling :", s));
  2958. send_text(TEXT_LINE1, TEXT_NORMAL, s, s->device->phone_number);
  2959. send_text(TEXT_LINE2, TEXT_NORMAL, s, ustmtext("Dialing...", s));
  2960. }
  2961. send_text_status(s, ustmtext(" Hangup", s));
  2962. /* start switch */
  2963. if (ast_pthread_create(&sub->ss_thread, NULL, unistim_ss, c)) {
  2964. ast_log(LOG_WARNING, "Unable to create switch thread\n");
  2965. sub->ss_thread = AST_PTHREADT_NULL;
  2966. ast_queue_hangup_with_cause(c, AST_CAUSE_SWITCH_CONGESTION);
  2967. }
  2968. } else
  2969. ast_log(LOG_WARNING, "Unable to create channel for %s@%s\n",
  2970. sub->parent->name, s->device->name);
  2971. } else {
  2972. ast_debug(1, "Current sub [%s] already has owner\n", ast_channel_name(sub->owner));
  2973. }
  2974. return;
  2975. }
  2976. /* From PBX to phone */
  2977. static void handle_call_incoming(struct unistimsession *s)
  2978. {
  2979. struct unistim_subchannel *sub = NULL;
  2980. int i;
  2981. s->state = STATE_CALL;
  2982. s->device->missed_call = 0;
  2983. send_no_ring(s);
  2984. sub = get_sub(s->device, SUB_RING); /* Put other SUB_REAL call on hold */
  2985. if (!sub) {
  2986. ast_log(LOG_WARNING, "No ringing lines on: %s\n", s->device->name);
  2987. return;
  2988. }
  2989. /* Change icons for all ringing keys */
  2990. for (i = 0; i < FAVNUM; i++) {
  2991. if (!s->device->ssub[i]) { /* No sub assigned - skip */
  2992. continue;
  2993. }
  2994. if (s->device->ssub[i]->subtype == SUB_REAL) {
  2995. sub_hold(s, s->device->ssub[i]);
  2996. }
  2997. if (s->device->ssub[i] != sub) {
  2998. continue;
  2999. }
  3000. if (sub->softkey == i) { /* If softkey assigned at this moment - do not erase */
  3001. continue;
  3002. }
  3003. if (sub->softkey < 0) { /* If softkey not defined - first one used */
  3004. sub->softkey = i;
  3005. continue;
  3006. }
  3007. send_favorite_short(i, FAV_LINE_ICON, s);
  3008. s->device->ssub[i] = NULL;
  3009. }
  3010. if (sub->softkey < 0) {
  3011. ast_log(LOG_WARNING, "Can not assign softkey for incoming call on: %s\n", s->device->name);
  3012. return;
  3013. }
  3014. send_favorite_short(sub->softkey, FAV_ICON_OFFHOOK_BLACK, s);
  3015. sub->parent = s->device->sline[sub->softkey];
  3016. sub->subtype = SUB_REAL;
  3017. if (unistimdebug) {
  3018. ast_verb(0, "Handle Call Incoming for %s@%s\n", sub->parent->name,
  3019. s->device->name);
  3020. }
  3021. start_rtp(sub);
  3022. if (!sub->rtp) {
  3023. ast_log(LOG_WARNING, "Unable to create channel for %s@%s\n", sub->parent->name, s->device->name);
  3024. return;
  3025. }
  3026. if (sub->owner) {
  3027. ast_queue_control(sub->owner, AST_CONTROL_ANSWER);
  3028. }
  3029. send_text(TEXT_LINE2, TEXT_NORMAL, s, ustmtext("is on-line", s));
  3030. send_text_status(s, ustmtext(" Transf Hangup", s));
  3031. send_start_timer(s);
  3032. if ((s->device->output == OUTPUT_HANDSET) &&
  3033. (s->device->receiver_state == STATE_ONHOOK)) {
  3034. send_select_output(s, OUTPUT_SPEAKER, s->device->volume, MUTE_OFF);
  3035. } else {
  3036. send_select_output(s, s->device->output, s->device->volume, MUTE_OFF);
  3037. }
  3038. write_history(s, 'i', 0);
  3039. return;
  3040. }
  3041. static int send_dtmf_tone(struct unistimsession *pte, char digit)
  3042. {
  3043. int row, col;
  3044. if (unistimdebug) {
  3045. ast_verb(0, "Phone Play Digit %c\n", digit);
  3046. }
  3047. if (pte->device->dtmfduration > 0) {
  3048. row = (digit - '1') % 3;
  3049. col = (digit - '1' - row) / 3;
  3050. if (digit >= '1' && digit <='9') {
  3051. send_tone(pte, dtmf_row[row], dtmf_col[col]);
  3052. } else if (digit >= 'A' && digit <= 'D') {
  3053. send_tone(pte, dtmf_row[digit-'A'], dtmf_col[3]);
  3054. } else if (digit == '*') {
  3055. send_tone(pte, dtmf_row[3], dtmf_col[0]);
  3056. } else if (digit == '0') {
  3057. send_tone(pte, dtmf_row[3], dtmf_col[1]);
  3058. } else if (digit == '#') {
  3059. send_tone(pte, dtmf_row[3], dtmf_col[2]);
  3060. } else {
  3061. send_tone(pte, 500, 2000);
  3062. }
  3063. }
  3064. return 0;
  3065. }
  3066. static int unistim_do_senddigit(struct unistimsession *pte, char digit)
  3067. {
  3068. struct ast_frame f = { .frametype = AST_FRAME_DTMF, .subclass.integer = digit, .src = "unistim" };
  3069. struct unistim_subchannel *sub;
  3070. sub = get_sub(pte->device, SUB_REAL);
  3071. if (!sub || !sub->owner || sub->alreadygone) {
  3072. ast_log(LOG_WARNING, "Unable to find subchannel in dtmf senddigit\n");
  3073. return -1;
  3074. }
  3075. /* Send DTMF indication _before_ playing sounds */
  3076. ast_queue_frame(sub->owner, &f);
  3077. if (pte->device->dtmfduration > 0) {
  3078. if (unistimdebug) {
  3079. ast_verb(0, "Send Digit %c (%i ms)\n", digit, pte->device->dtmfduration);
  3080. }
  3081. send_dtmf_tone(pte, digit);
  3082. usleep(pte->device->dtmfduration * 1000); /* XXX Less than perfect, blocking an important thread is not a good idea */
  3083. send_tone(pte, 0, 0);
  3084. }
  3085. return 0;
  3086. }
  3087. static void handle_key_fav(struct unistimsession *pte, char keycode)
  3088. {
  3089. int keynum = keycode - KEY_FAV0;
  3090. struct unistim_subchannel *sub, *sub_key = NULL;
  3091. sub = get_sub_holding(pte->device, SUB_REAL, 0);
  3092. /* Make an action on selected favorite key */
  3093. if (!pte->device->ssub[keynum]) { /* Key have no assigned call */
  3094. sub = get_sub_holding(pte->device, SUB_REAL, 0);
  3095. send_favorite_selected(FAV_LINE_ICON, pte);
  3096. if (is_key_line(pte->device, keynum)) {
  3097. if (unistimdebug) {
  3098. ast_verb(0, "Handle line w/o sub - dialpage\n");
  3099. }
  3100. pte->device->selected = keynum;
  3101. sub_hold(pte, sub); /* Put active call on hold */
  3102. send_stop_timer(pte);
  3103. handle_dial_page(pte);
  3104. } else if (is_key_favorite(pte->device, keynum)) {
  3105. /* Put active call on hold in handle_call_outgoing function, after preparation and
  3106. checking if lines available for calling */
  3107. if (unistimdebug) {
  3108. ast_verb(0, "Handle favorite w/o sub - dialing\n");
  3109. }
  3110. if ((pte->device->output == OUTPUT_HANDSET) &&
  3111. (pte->device->receiver_state == STATE_ONHOOK)) {
  3112. send_select_output(pte, OUTPUT_SPEAKER, pte->device->volume, MUTE_OFF);
  3113. } else {
  3114. send_select_output(pte, pte->device->output, pte->device->volume, MUTE_OFF);
  3115. }
  3116. key_favorite(pte, keycode);
  3117. }
  3118. } else {
  3119. sub_key = pte->device->ssub[keynum];
  3120. /* Favicon have assigned sub, activate it and put current on hold */
  3121. if (sub_key->subtype == SUB_REAL && !sub_key->holding) {
  3122. sub_hold(pte, sub_key);
  3123. show_main_page(pte);
  3124. } else if (sub_key->subtype == SUB_REAL && sub_key->holding) {
  3125. /* We are going to unhold line (we should put active line on hold, of any) */
  3126. if (pte->state == STATE_DIALPAGE){
  3127. send_tone(pte, 0, 0);
  3128. }
  3129. sub_hold(pte, sub);
  3130. send_callerid_screen(pte, sub_key);
  3131. sub_unhold(pte, sub_key);
  3132. pte->state = STATE_CALL;
  3133. } else if (sub_key->subtype == SUB_RING) {
  3134. sub_hold(pte, sub);
  3135. sub_key->softkey = keynum;
  3136. handle_call_incoming(pte);
  3137. }
  3138. }
  3139. }
  3140. static void key_call(struct unistimsession *pte, char keycode)
  3141. {
  3142. struct unistim_subchannel *sub = get_sub(pte->device, SUB_REAL);
  3143. struct unistim_subchannel *sub_3way = get_sub(pte->device, SUB_THREEWAY);
  3144. if (!sub) {
  3145. return;
  3146. }
  3147. if ((keycode >= KEY_0) && (keycode <= KEY_SHARP)) {
  3148. if (keycode == KEY_SHARP) {
  3149. keycode = '#';
  3150. } else if (keycode == KEY_STAR) {
  3151. keycode = '*';
  3152. } else {
  3153. keycode -= 0x10;
  3154. }
  3155. unistim_do_senddigit(pte, keycode);
  3156. return;
  3157. }
  3158. switch (keycode) {
  3159. case KEY_FUNC1:
  3160. if (sub->owner && ast_channel_state(sub->owner) == AST_STATE_UP) {
  3161. if (sub_3way) {
  3162. close_call(pte);
  3163. }
  3164. }
  3165. break;
  3166. case KEY_FUNC2:
  3167. if (sub_3way) {
  3168. transfer_cancel_step2(pte);
  3169. } else if (ast_channel_state(sub->owner) == AST_STATE_UP) {
  3170. transfer_call_step1(pte);
  3171. }
  3172. break;
  3173. case KEY_HANGUP:
  3174. case KEY_FUNC4:
  3175. if (!sub_3way) {
  3176. close_call(pte);
  3177. }
  3178. break;
  3179. case KEY_FAV0:
  3180. case KEY_FAV1:
  3181. case KEY_FAV2:
  3182. case KEY_FAV3:
  3183. case KEY_FAV4:
  3184. case KEY_FAV5:
  3185. handle_key_fav(pte, keycode);
  3186. break;
  3187. case KEY_HEADPHN:
  3188. if (pte->device->output == OUTPUT_HEADPHONE) {
  3189. send_select_output(pte, OUTPUT_HANDSET, pte->device->volume, MUTE_OFF);
  3190. } else {
  3191. send_select_output(pte, OUTPUT_HEADPHONE, pte->device->volume, MUTE_OFF);
  3192. }
  3193. break;
  3194. case KEY_LOUDSPK:
  3195. if (pte->device->output != OUTPUT_SPEAKER)
  3196. send_select_output(pte, OUTPUT_SPEAKER, pte->device->volume, MUTE_OFF);
  3197. else
  3198. send_select_output(pte, pte->device->previous_output, pte->device->volume,
  3199. MUTE_OFF);
  3200. break;
  3201. case KEY_ONHOLD:
  3202. if (!sub) {
  3203. if(pte->device->ssub[pte->device->selected]) {
  3204. sub = pte->device->ssub[pte->device->selected];
  3205. } else {
  3206. break;
  3207. }
  3208. }
  3209. if (sub->holding) {
  3210. sub_unhold(pte, sub);
  3211. } else {
  3212. sub_hold(pte, sub);
  3213. }
  3214. break;
  3215. }
  3216. return;
  3217. }
  3218. static void key_ringing(struct unistimsession *pte, char keycode)
  3219. {
  3220. switch (keycode) {
  3221. case KEY_FAV0:
  3222. case KEY_FAV1:
  3223. case KEY_FAV2:
  3224. case KEY_FAV3:
  3225. case KEY_FAV4:
  3226. case KEY_FAV5:
  3227. handle_key_fav(pte, keycode);
  3228. break;
  3229. case KEY_FUNC3:
  3230. ignore_call(pte);
  3231. break;
  3232. case KEY_HANGUP:
  3233. case KEY_FUNC4:
  3234. discard_call(pte);
  3235. break;
  3236. case KEY_LOUDSPK:
  3237. pte->device->output = OUTPUT_SPEAKER;
  3238. handle_call_incoming(pte);
  3239. break;
  3240. case KEY_HEADPHN:
  3241. pte->device->output = OUTPUT_HEADPHONE;
  3242. handle_call_incoming(pte);
  3243. break;
  3244. case KEY_FUNC1:
  3245. handle_call_incoming(pte);
  3246. break;
  3247. }
  3248. return;
  3249. }
  3250. static void key_favorite(struct unistimsession *pte, char keycode)
  3251. {
  3252. int fav = keycode - KEY_FAV0;
  3253. if (!is_key_favorite(pte->device, fav)) {
  3254. ast_log(LOG_WARNING, "It's not a favorite key\n");
  3255. return;
  3256. }
  3257. ast_copy_string(pte->device->phone_number, pte->device->softkeynumber[fav],
  3258. sizeof(pte->device->phone_number));
  3259. handle_call_outgoing(pte);
  3260. return;
  3261. }
  3262. static void key_dial_page(struct unistimsession *pte, char keycode)
  3263. {
  3264. struct unistim_subchannel *sub = get_sub(pte->device, SUB_THREEWAY);
  3265. pte->device->nextdial = 0;
  3266. if (keycode == KEY_FUNC3) {
  3267. if (pte->device->size_phone_number <= 1) {
  3268. keycode = KEY_FUNC4;
  3269. } else {
  3270. pte->device->size_phone_number -= 2;
  3271. keycode = pte->device->phone_number[pte->device->size_phone_number] + 0x10;
  3272. }
  3273. }
  3274. if (keycode == KEY_SHARP && pte->device->sharp_dial == 1) {
  3275. keycode = KEY_FUNC1;
  3276. }
  3277. if ((keycode >= KEY_0) && (keycode <= KEY_SHARP)) {
  3278. int i = pte->device->size_phone_number;
  3279. if (pte->device->size_phone_number == 0) {
  3280. send_tone(pte, 0, 0);
  3281. }
  3282. if (keycode == KEY_SHARP) {
  3283. keycode = '#';
  3284. } else if (keycode == KEY_STAR) {
  3285. keycode = '*';
  3286. } else {
  3287. keycode -= 0x10;
  3288. }
  3289. pte->device->phone_number[i] = keycode;
  3290. pte->device->size_phone_number++;
  3291. pte->device->phone_number[i + 1] = 0;
  3292. show_phone_number(pte);
  3293. if (ast_exists_extension(NULL, pte->device->context, pte->device->phone_number, 1, NULL) &&
  3294. !ast_matchmore_extension(NULL, pte->device->context, pte->device->phone_number, 1, NULL)) {
  3295. keycode = KEY_FUNC1;
  3296. } else {
  3297. if (pte->device->interdigit_timer) {
  3298. pte->device->nextdial = get_tick_count() + pte->device->interdigit_timer;
  3299. }
  3300. }
  3301. }
  3302. if (keycode == KEY_FUNC4) {
  3303. pte->device->size_phone_number = 0;
  3304. show_phone_number(pte);
  3305. return;
  3306. }
  3307. if (pte->device->call_forward[0] == -1) {
  3308. if (keycode == KEY_FUNC1) {
  3309. ast_copy_string(pte->device->call_forward, pte->device->phone_number,
  3310. sizeof(pte->device->call_forward));
  3311. show_main_page(pte);
  3312. } else if ((keycode == KEY_FUNC2) || (keycode == KEY_HANGUP)) {
  3313. pte->device->call_forward[0] = '\0';
  3314. send_led_update(pte, LED_SPEAKER_OFF);
  3315. send_led_update(pte, LED_HEADPHONE_OFF);
  3316. show_main_page(pte);
  3317. }
  3318. return;
  3319. }
  3320. switch (keycode) {
  3321. case KEY_FUNC2:
  3322. if (ast_strlen_zero(pte->device->redial_number)) {
  3323. break;
  3324. }
  3325. ast_copy_string(pte->device->phone_number, pte->device->redial_number,
  3326. sizeof(pte->device->phone_number));
  3327. case KEY_FUNC1:
  3328. handle_call_outgoing(pte);
  3329. break;
  3330. case KEY_HANGUP:
  3331. if (sub && sub->owner) {
  3332. sub_stop_silence(pte, sub);
  3333. send_tone(pte, 0, 0);
  3334. ast_queue_unhold(sub->owner);
  3335. sub->moh = 0;
  3336. sub->subtype = SUB_REAL;
  3337. pte->state = STATE_CALL;
  3338. send_text_status(pte, ustmtext(" Transf Hangup", pte));
  3339. send_callerid_screen(pte, sub);
  3340. } else {
  3341. send_led_update(pte, LED_SPEAKER_OFF);
  3342. send_led_update(pte, LED_HEADPHONE_OFF);
  3343. show_main_page(pte);
  3344. }
  3345. break;
  3346. case KEY_FAV0:
  3347. case KEY_FAV1:
  3348. case KEY_FAV2:
  3349. case KEY_FAV3:
  3350. case KEY_FAV4:
  3351. case KEY_FAV5:
  3352. send_favorite_selected(FAV_LINE_ICON, pte);
  3353. pte->device->selected = -1;
  3354. handle_key_fav(pte, keycode);
  3355. break;
  3356. case KEY_LOUDSPK:
  3357. if (pte->device->output == OUTPUT_SPEAKER) {
  3358. if (pte->device->receiver_state == STATE_OFFHOOK) {
  3359. send_select_output(pte, pte->device->previous_output, pte->device->volume,
  3360. MUTE_OFF);
  3361. } else {
  3362. show_main_page(pte);
  3363. }
  3364. } else {
  3365. send_select_output(pte, OUTPUT_SPEAKER, pte->device->volume, MUTE_OFF);
  3366. }
  3367. break;
  3368. case KEY_HEADPHN:
  3369. if (pte->device->output == OUTPUT_HEADPHONE) {
  3370. if (pte->device->receiver_state == STATE_OFFHOOK) {
  3371. send_select_output(pte, OUTPUT_HANDSET, pte->device->volume, MUTE_OFF);
  3372. } else {
  3373. show_main_page(pte);
  3374. }
  3375. } else {
  3376. send_select_output(pte, OUTPUT_HEADPHONE, pte->device->volume, MUTE_OFF);
  3377. }
  3378. break;
  3379. }
  3380. return;
  3381. }
  3382. static void handle_select_option(struct unistimsession *pte)
  3383. {
  3384. char tmp[128];
  3385. if (pte->state != STATE_SELECTOPTION) {
  3386. pte->state = STATE_SELECTOPTION;
  3387. pte->size_buff_entry = 1;
  3388. pte->buff_entry[0] = 0; /* Position in menu */
  3389. }
  3390. snprintf(tmp, sizeof(tmp), "%d. %s", pte->buff_entry[0] + 1, ustmtext(options_menu[(int)pte->buff_entry[0]].label, pte));
  3391. send_text(TEXT_LINE0, TEXT_NORMAL, pte, tmp);
  3392. send_text_status(pte, ustmtext("Select Cancel", pte));
  3393. return;
  3394. }
  3395. static void key_select_option(struct unistimsession *pte, char keycode)
  3396. {
  3397. switch (keycode) {
  3398. case KEY_DOWN:
  3399. pte->buff_entry[0]++;
  3400. if (options_menu[(int)pte->buff_entry[0]].label == NULL) {
  3401. pte->buff_entry[0]--;
  3402. }
  3403. break;
  3404. case KEY_UP:
  3405. if (pte->buff_entry[0] > 0) {
  3406. pte->buff_entry[0]--;
  3407. }
  3408. break;
  3409. case KEY_FUNC1:
  3410. options_menu[(int)pte->buff_entry[0]].handle_option(pte);
  3411. return;
  3412. case KEY_HANGUP:
  3413. case KEY_FUNC4:
  3414. show_main_page(pte);
  3415. return;
  3416. }
  3417. handle_select_option(pte);
  3418. return;
  3419. }
  3420. #define SELECTCODEC_START_ENTRY_POS 15
  3421. #define SELECTCODEC_MAX_LENGTH 2
  3422. #define SELECTCODEC_MSG "Codec number : .."
  3423. static void handle_select_codec(struct unistimsession *pte)
  3424. {
  3425. char buf[30], buf2[6];
  3426. pte->state = STATE_SELECTCODEC;
  3427. ast_copy_string(buf, ustmtext("Using codec", pte), sizeof(buf));
  3428. snprintf(buf2, sizeof(buf2), " %d", pte->device->codec_number);
  3429. strcat(buf, buf2);
  3430. strcat(buf, " (G711u=0,");
  3431. send_text(TEXT_LINE0, TEXT_NORMAL, pte, buf);
  3432. send_text(TEXT_LINE1, TEXT_NORMAL, pte, "G723=4,G711a=8,G729A=18)");
  3433. send_text(TEXT_LINE2, TEXT_INVERSE, pte, SELECTCODEC_MSG);
  3434. send_blink_cursor(pte);
  3435. send_cursor_pos(pte, TEXT_LINE2 + SELECTCODEC_START_ENTRY_POS);
  3436. pte->size_buff_entry = 0;
  3437. send_text_status(pte, ustmtext("Select BackSp Erase Cancel", pte));
  3438. return;
  3439. }
  3440. static void key_select_codec(struct unistimsession *pte, char keycode)
  3441. {
  3442. if (keycode == KEY_FUNC2) {
  3443. if (pte->size_buff_entry <= 1) {
  3444. keycode = KEY_FUNC3;
  3445. } else {
  3446. pte->size_buff_entry -= 2;
  3447. keycode = pte->buff_entry[pte->size_buff_entry] + 0x10;
  3448. }
  3449. }
  3450. if ((keycode >= KEY_0) && (keycode <= KEY_9)) {
  3451. char tmpbuf[] = SELECTCODEC_MSG;
  3452. int i = 0;
  3453. if (pte->size_buff_entry >= SELECTCODEC_MAX_LENGTH) {
  3454. return;
  3455. }
  3456. while (i < pte->size_buff_entry) {
  3457. tmpbuf[i + SELECTCODEC_START_ENTRY_POS] = pte->buff_entry[i];
  3458. i++;
  3459. }
  3460. tmpbuf[i + SELECTCODEC_START_ENTRY_POS] = keycode - 0x10;
  3461. pte->buff_entry[i] = keycode - 0x10;
  3462. pte->size_buff_entry++;
  3463. send_text(TEXT_LINE2, TEXT_INVERSE, pte, tmpbuf);
  3464. send_blink_cursor(pte);
  3465. send_cursor_pos(pte,
  3466. (unsigned char) (TEXT_LINE2 + SELECTCODEC_START_ENTRY_POS + 1 + i));
  3467. return;
  3468. }
  3469. switch (keycode) {
  3470. case KEY_FUNC1:
  3471. if (pte->size_buff_entry == 1) {
  3472. pte->device->codec_number = pte->buff_entry[0] - 48;
  3473. } else if (pte->size_buff_entry == 2) {
  3474. pte->device->codec_number =
  3475. ((pte->buff_entry[0] - 48) * 10) + (pte->buff_entry[1] - 48);
  3476. }
  3477. show_main_page(pte);
  3478. break;
  3479. case KEY_FUNC3:
  3480. pte->size_buff_entry = 0;
  3481. send_text(TEXT_LINE2, TEXT_INVERSE, pte, SELECTCODEC_MSG);
  3482. send_blink_cursor(pte);
  3483. send_cursor_pos(pte, TEXT_LINE2 + SELECTCODEC_START_ENTRY_POS);
  3484. break;
  3485. case KEY_HANGUP:
  3486. case KEY_FUNC4:
  3487. show_main_page(pte);
  3488. break;
  3489. }
  3490. return;
  3491. }
  3492. static int find_language(const char* lang)
  3493. {
  3494. int i = 0;
  3495. while (options_languages[i].lang_short != NULL) {
  3496. if(!strcmp(options_languages[i].lang_short, lang)) {
  3497. return i;
  3498. }
  3499. i++;
  3500. }
  3501. return 0;
  3502. }
  3503. static void handle_select_language(struct unistimsession *pte)
  3504. {
  3505. char tmp_language[40];
  3506. struct unistim_languages lang;
  3507. if (pte->state != STATE_SELECTLANGUAGE) {
  3508. pte->state = STATE_SELECTLANGUAGE;
  3509. pte->size_buff_entry = 1;
  3510. pte->buff_entry[0] = find_language(pte->device->language);
  3511. }
  3512. lang = options_languages[(int)pte->buff_entry[0]];
  3513. ast_copy_string(tmp_language, pte->device->language, sizeof(tmp_language));
  3514. ast_copy_string(pte->device->language, lang.lang_short, sizeof(pte->device->language));
  3515. send_charset_update(pte, lang.encoding);
  3516. send_text(TEXT_LINE0, TEXT_NORMAL, pte, ustmtext(lang.label, pte));
  3517. ast_copy_string(pte->device->language, tmp_language, sizeof(pte->device->language));
  3518. lang = options_languages[find_language(pte->device->language)];
  3519. send_charset_update(pte, lang.encoding);
  3520. send_text_status(pte, ustmtext("Select Cancel", pte));
  3521. return;
  3522. }
  3523. static void key_select_language(struct unistimsession *pte, char keycode)
  3524. {
  3525. switch (keycode) {
  3526. case KEY_DOWN:
  3527. pte->buff_entry[0]++;
  3528. if (options_languages[(int)pte->buff_entry[0]].label == NULL) {
  3529. pte->buff_entry[0]--;
  3530. }
  3531. break;
  3532. case KEY_UP:
  3533. if (pte->buff_entry[0] > 0) {
  3534. pte->buff_entry[0]--;
  3535. }
  3536. break;
  3537. case KEY_FUNC1:
  3538. ast_copy_string(pte->device->language, options_languages[(int)pte->buff_entry[0]].lang_short, sizeof(pte->device->language));
  3539. send_charset_update(pte, options_languages[(int)pte->buff_entry[0]].encoding);
  3540. refresh_all_favorite(pte);
  3541. show_main_page(pte);
  3542. return;
  3543. case KEY_HANGUP:
  3544. case KEY_FUNC4:
  3545. handle_select_option(pte);
  3546. return;
  3547. }
  3548. handle_select_language(pte);
  3549. return;
  3550. }
  3551. #define SELECTEXTENSION_START_ENTRY_POS 0
  3552. #define SELECTEXTENSION_MAX_LENGTH 10
  3553. #define SELECTEXTENSION_MSG ".........."
  3554. static void show_extension_page(struct unistimsession *pte)
  3555. {
  3556. pte->state = STATE_EXTENSION;
  3557. send_text(TEXT_LINE0, TEXT_NORMAL, pte, ustmtext("Please enter a Terminal", pte));
  3558. send_text(TEXT_LINE1, TEXT_NORMAL, pte, ustmtext("Number (TN) :", pte));
  3559. send_text(TEXT_LINE2, TEXT_NORMAL, pte, SELECTEXTENSION_MSG);
  3560. send_blink_cursor(pte);
  3561. send_cursor_pos(pte, TEXT_LINE2 + SELECTEXTENSION_START_ENTRY_POS);
  3562. send_text_status(pte, ustmtext("Enter BackSpcErase", pte));
  3563. pte->size_buff_entry = 0;
  3564. return;
  3565. }
  3566. static void key_select_extension(struct unistimsession *pte, char keycode)
  3567. {
  3568. if (keycode == KEY_FUNC2) {
  3569. if (pte->size_buff_entry <= 1) {
  3570. keycode = KEY_FUNC3;
  3571. } else {
  3572. pte->size_buff_entry -= 2;
  3573. keycode = pte->buff_entry[pte->size_buff_entry] + 0x10;
  3574. }
  3575. }
  3576. if ((keycode >= KEY_0) && (keycode <= KEY_9)) {
  3577. char tmpbuf[] = SELECTEXTENSION_MSG;
  3578. int i = 0;
  3579. if (pte->size_buff_entry >= SELECTEXTENSION_MAX_LENGTH) {
  3580. return;
  3581. }
  3582. while (i < pte->size_buff_entry) {
  3583. tmpbuf[i + SELECTEXTENSION_START_ENTRY_POS] = pte->buff_entry[i];
  3584. i++;
  3585. }
  3586. tmpbuf[i + SELECTEXTENSION_START_ENTRY_POS] = keycode - 0x10;
  3587. pte->buff_entry[i] = keycode - 0x10;
  3588. pte->size_buff_entry++;
  3589. send_text(TEXT_LINE2, TEXT_NORMAL, pte, tmpbuf);
  3590. send_blink_cursor(pte);
  3591. send_cursor_pos(pte, (unsigned char) (TEXT_LINE2 + SELECTEXTENSION_START_ENTRY_POS + 1 + i));
  3592. return;
  3593. }
  3594. switch (keycode) {
  3595. case KEY_FUNC1:
  3596. if (pte->size_buff_entry < 1) {
  3597. return;
  3598. }
  3599. if (autoprovisioning == AUTOPROVISIONING_TN) {
  3600. struct unistim_device *d;
  3601. /* First step : looking for this TN in our device list */
  3602. ast_mutex_lock(&devicelock);
  3603. d = devices;
  3604. pte->buff_entry[pte->size_buff_entry] = '\0';
  3605. while (d) {
  3606. if (d->id[0] == 'T') { /* It's a TN device ? */
  3607. /* It's the TN we're looking for ? */
  3608. if (!strcmp((d->id) + 1, pte->buff_entry)) {
  3609. pte->device = d;
  3610. d->session = pte;
  3611. d->codec_number = DEFAULT_CODEC;
  3612. d->missed_call = 0;
  3613. d->receiver_state = STATE_ONHOOK;
  3614. strcpy(d->id, pte->macaddr);
  3615. pte->device->extension_number[0] = 'T';
  3616. pte->device->extension = EXTENSION_TN;
  3617. ast_copy_string((pte->device->extension_number) + 1,
  3618. pte->buff_entry, pte->size_buff_entry + 1);
  3619. ast_mutex_unlock(&devicelock);
  3620. show_main_page(pte);
  3621. refresh_all_favorite(pte);
  3622. return;
  3623. }
  3624. }
  3625. d = d->next;
  3626. }
  3627. ast_mutex_unlock(&devicelock);
  3628. send_text(TEXT_LINE0, TEXT_NORMAL, pte, ustmtext("Invalid Terminal Number.", pte));
  3629. send_text(TEXT_LINE1, TEXT_NORMAL, pte, ustmtext("Please try again :", pte));
  3630. send_cursor_pos(pte, (unsigned char) (TEXT_LINE2 + SELECTEXTENSION_START_ENTRY_POS +
  3631. pte->size_buff_entry));
  3632. send_blink_cursor(pte);
  3633. } else {
  3634. ast_copy_string(pte->device->extension_number, pte->buff_entry,
  3635. pte->size_buff_entry + 1);
  3636. if (register_extension(pte)) {
  3637. send_text(TEXT_LINE0, TEXT_NORMAL, pte, ustmtext("Invalid extension.", pte));
  3638. send_text(TEXT_LINE1, TEXT_NORMAL, pte, ustmtext("Please try again :", pte));
  3639. send_cursor_pos(pte, (unsigned char) (TEXT_LINE2 +
  3640. SELECTEXTENSION_START_ENTRY_POS +
  3641. pte->size_buff_entry));
  3642. send_blink_cursor(pte);
  3643. } else
  3644. show_main_page(pte);
  3645. }
  3646. break;
  3647. case KEY_FUNC3:
  3648. pte->size_buff_entry = 0;
  3649. send_text(TEXT_LINE2, TEXT_NORMAL, pte, SELECTEXTENSION_MSG);
  3650. send_blink_cursor(pte);
  3651. send_cursor_pos(pte, TEXT_LINE2 + SELECTEXTENSION_START_ENTRY_POS);
  3652. break;
  3653. }
  3654. return;
  3655. }
  3656. static void show_entry_history(struct unistimsession *pte, FILE ** f)
  3657. {
  3658. char line[TEXT_LENGTH_MAX + 1], status[STATUS_LENGTH_MAX + 1], func1[10], func2[10],
  3659. func3[10];
  3660. /* Display date/time and call status */
  3661. if (fread(line, TEXT_LENGTH_MAX, 1, *f) != 1) {
  3662. display_last_error("Can't read history date entry");
  3663. fclose(*f);
  3664. return;
  3665. }
  3666. line[sizeof(line) - 1] = '\0';
  3667. if (pte->device->height == 1) {
  3668. if (pte->buff_entry[3] == 1) {
  3669. send_text(TEXT_LINE0, TEXT_NORMAL, pte, line);
  3670. }
  3671. } else {
  3672. send_text(TEXT_LINE0, TEXT_NORMAL, pte, line);
  3673. }
  3674. /* Display number */
  3675. if (fread(line, TEXT_LENGTH_MAX, 1, *f) != 1) {
  3676. display_last_error("Can't read callerid entry");
  3677. fclose(*f);
  3678. return;
  3679. }
  3680. line[sizeof(line) - 1] = '\0';
  3681. ast_copy_string(pte->device->lst_cid, line, sizeof(pte->device->lst_cid));
  3682. ast_trim_blanks(pte->device->lst_cid);
  3683. if (pte->device->height == 1) {
  3684. if (pte->buff_entry[3] == 2) {
  3685. send_text(TEXT_LINE0, TEXT_NORMAL, pte, line);
  3686. }
  3687. } else {
  3688. send_text(TEXT_LINE1, TEXT_NORMAL, pte, line);
  3689. }
  3690. /* Display name */
  3691. if (fread(line, TEXT_LENGTH_MAX, 1, *f) != 1) {
  3692. display_last_error("Can't read callername entry");
  3693. fclose(*f);
  3694. return;
  3695. }
  3696. line[sizeof(line) - 1] = '\0';
  3697. if (pte->device->height == 1) {
  3698. if (pte->buff_entry[3] == 3) {
  3699. send_text(TEXT_LINE0, TEXT_NORMAL, pte, line);
  3700. }
  3701. } else {
  3702. send_text(TEXT_LINE2, TEXT_NORMAL, pte, line);
  3703. }
  3704. fclose(*f);
  3705. snprintf(line, sizeof(line), "%s %03d/%03d", ustmtext("Call", pte), pte->buff_entry[2],
  3706. pte->buff_entry[1]);
  3707. send_texttitle(pte, line);
  3708. if (pte->buff_entry[2] == 1) {
  3709. ast_copy_string(func1, " ", sizeof(func1));
  3710. } else {
  3711. ast_copy_string(func1, ustmtext("Prev ", pte), sizeof(func1));
  3712. }
  3713. if (pte->buff_entry[2] >= pte->buff_entry[1]) {
  3714. ast_copy_string(func2, " ", sizeof(func2));
  3715. } else {
  3716. ast_copy_string(func2, ustmtext("Next ", pte), sizeof(func2));
  3717. }
  3718. if (strlen(pte->device->lst_cid)) {
  3719. ast_copy_string(func3, ustmtext("Redial ", pte), sizeof(func3));
  3720. } else {
  3721. ast_copy_string(func3, " ", sizeof(func3));
  3722. }
  3723. snprintf(status, sizeof(status), "%s%s%s%s", func1, func2, func3, ustmtext("Cancel", pte));
  3724. send_text_status(pte, status);
  3725. }
  3726. static char open_history(struct unistimsession *pte, char way, FILE ** f)
  3727. {
  3728. char tmp[AST_CONFIG_MAX_PATH];
  3729. char count;
  3730. snprintf(tmp, sizeof(tmp), "%s/%s/%s-%c.csv", ast_config_AST_LOG_DIR,
  3731. USTM_LOG_DIR, pte->device->name, way);
  3732. *f = fopen(tmp, "r");
  3733. if (!*f) {
  3734. display_last_error("Unable to open history file");
  3735. return 0;
  3736. }
  3737. if (fread(&count, 1, 1, *f) != 1) {
  3738. display_last_error("Unable to read history header - display.");
  3739. fclose(*f);
  3740. *f = NULL;
  3741. return 0;
  3742. }
  3743. if (count > MAX_ENTRY_LOG) {
  3744. ast_log(LOG_WARNING, "Invalid count in history header of %s (%d max %d)\n", tmp,
  3745. count, MAX_ENTRY_LOG);
  3746. fclose(*f);
  3747. *f = NULL;
  3748. return 0;
  3749. }
  3750. return count;
  3751. }
  3752. static void show_history(struct unistimsession *pte, char way)
  3753. {
  3754. FILE *f;
  3755. char count;
  3756. if (!pte->device) {
  3757. return;
  3758. }
  3759. if (!pte->device->callhistory) {
  3760. return;
  3761. }
  3762. count = open_history(pte, way, &f);
  3763. if (!count) {
  3764. return;
  3765. }
  3766. pte->buff_entry[0] = way;
  3767. pte->buff_entry[1] = count;
  3768. pte->buff_entry[2] = 1;
  3769. pte->buff_entry[3] = 1;
  3770. show_entry_history(pte, &f);
  3771. pte->state = STATE_HISTORY;
  3772. }
  3773. static void show_main_page(struct unistimsession *pte)
  3774. {
  3775. char tmpbuf[TEXT_LENGTH_MAX + 1];
  3776. const char *text;
  3777. if ((pte->device->extension == EXTENSION_ASK) &&
  3778. (ast_strlen_zero(pte->device->extension_number))) {
  3779. show_extension_page(pte);
  3780. return;
  3781. }
  3782. pte->state = STATE_MAINPAGE;
  3783. send_led_update(pte, LED_BAR_OFF);
  3784. pte->device->lastmsgssent = -1;
  3785. send_tone(pte, 0, 0);
  3786. send_stop_timer(pte); /* case of holding call */
  3787. send_select_output(pte, pte->device->output, pte->device->volume, MUTE_ON_DISCRET);
  3788. send_led_update(pte, LED_SPEAKER_OFF);
  3789. send_led_update(pte, LED_HEADPHONE_OFF);
  3790. if (!ast_strlen_zero(pte->device->call_forward)) {
  3791. if (pte->device->height == 1) {
  3792. char tmp_field[100];
  3793. snprintf(tmp_field, sizeof(tmp_field), "%s %s", ustmtext("Fwd to:", pte), pte->device->call_forward);
  3794. send_text(TEXT_LINE0, TEXT_NORMAL, pte, tmp_field);
  3795. } else {
  3796. send_text(TEXT_LINE0, TEXT_NORMAL, pte, ustmtext("Call forwarded to :", pte));
  3797. send_text(TEXT_LINE1, TEXT_NORMAL, pte, pte->device->call_forward);
  3798. }
  3799. send_icon(TEXT_LINE0, FAV_ICON_REFLECT + FAV_BLINK_SLOW, pte);
  3800. if (ast_strlen_zero(pte->device->redial_number)) {
  3801. send_text_status(pte, ustmtext("Dial NoFwd ", pte));
  3802. } else {
  3803. send_text_status(pte, ustmtext("Dial Redial NoFwd ", pte));
  3804. }
  3805. } else {
  3806. if ((pte->device->extension == EXTENSION_ASK) || (pte->device->extension == EXTENSION_TN)) {
  3807. if (ast_strlen_zero(pte->device->redial_number)) {
  3808. send_text_status(pte, ustmtext("Dial Fwd Unregis", pte));
  3809. } else {
  3810. send_text_status(pte, ustmtext("Dial Redial Fwd Unregis", pte));
  3811. }
  3812. } else {
  3813. if (ast_strlen_zero(pte->device->redial_number)) {
  3814. send_text_status(pte, ustmtext("Dial Fwd Pickup", pte));
  3815. } else {
  3816. send_text_status(pte, ustmtext("Dial Redial Fwd Pickup", pte));
  3817. }
  3818. }
  3819. send_text(TEXT_LINE1, TEXT_NORMAL, pte, pte->device->maintext1);
  3820. if (pte->device->missed_call == 0) {
  3821. send_date_time2(pte);
  3822. send_idle_clock(pte);
  3823. if (strlen(pte->device->maintext0)) {
  3824. send_text(TEXT_LINE0, TEXT_NORMAL, pte, ustmtext(pte->device->maintext0, pte));
  3825. }
  3826. } else {
  3827. if (pte->device->missed_call == 1) {
  3828. text = ustmtext("unanswered call", pte);
  3829. } else {
  3830. text = ustmtext("unanswered calls", pte);
  3831. }
  3832. snprintf(tmpbuf, sizeof(tmpbuf), "%d %s", pte->device->missed_call, text);
  3833. send_text(TEXT_LINE0, TEXT_NORMAL, pte, tmpbuf);
  3834. send_icon(TEXT_LINE0, FAV_ICON_CALL_CENTER + FAV_BLINK_SLOW, pte);
  3835. }
  3836. }
  3837. if (pte->device->height > 1) {
  3838. if (ast_strlen_zero(pte->device->maintext2)) {
  3839. strcpy(tmpbuf, "IP : ");
  3840. strcat(tmpbuf, ast_inet_ntoa(pte->sin.sin_addr));
  3841. send_text(TEXT_LINE2, TEXT_NORMAL, pte, tmpbuf);
  3842. } else {
  3843. send_text(TEXT_LINE2, TEXT_NORMAL, pte, ustmtext(pte->device->maintext2, pte));
  3844. }
  3845. }
  3846. send_texttitle(pte, ustmtext(pte->device->titledefault, pte));
  3847. change_favorite_icon(pte, FAV_LINE_ICON);
  3848. }
  3849. static void key_main_page(struct unistimsession *pte, char keycode)
  3850. {
  3851. if (pte->device->missed_call) {
  3852. send_icon(TEXT_LINE0, FAV_ICON_NONE, pte);
  3853. pte->device->missed_call = 0;
  3854. }
  3855. if ((keycode >= KEY_0) && (keycode <= KEY_SHARP)) {
  3856. handle_dial_page(pte);
  3857. key_dial_page(pte, keycode);
  3858. return;
  3859. }
  3860. switch (keycode) {
  3861. case KEY_FUNC1:
  3862. pte->device->selected = get_avail_softkey(pte, NULL);
  3863. handle_dial_page(pte);
  3864. break;
  3865. case KEY_FUNC2:
  3866. if (ast_strlen_zero(pte->device->redial_number)) {
  3867. break;
  3868. }
  3869. if ((pte->device->output == OUTPUT_HANDSET) &&
  3870. (pte->device->receiver_state == STATE_ONHOOK)) {
  3871. send_select_output(pte, OUTPUT_SPEAKER, pte->device->volume, MUTE_OFF);
  3872. } else {
  3873. send_select_output(pte, pte->device->output, pte->device->volume, MUTE_OFF);
  3874. }
  3875. ast_copy_string(pte->device->phone_number, pte->device->redial_number,
  3876. sizeof(pte->device->phone_number));
  3877. handle_call_outgoing(pte);
  3878. break;
  3879. case KEY_FUNC3:
  3880. if (!ast_strlen_zero(pte->device->call_forward)) {
  3881. /* Cancel call forwarding */
  3882. memmove(pte->device->call_forward + 1, pte->device->call_forward,
  3883. sizeof(pte->device->call_forward) - 1);
  3884. pte->device->call_forward[0] = '\0';
  3885. send_icon(TEXT_LINE0, FAV_ICON_NONE, pte);
  3886. pte->device->output = OUTPUT_HANDSET; /* Seems to be reseted somewhere */
  3887. show_main_page(pte);
  3888. break;
  3889. }
  3890. pte->device->call_forward[0] = -1;
  3891. handle_dial_page(pte);
  3892. break;
  3893. case KEY_FUNC4:
  3894. if (pte->device->extension == EXTENSION_ASK) {
  3895. unregister_extension(pte);
  3896. pte->device->extension_number[0] = '\0';
  3897. show_extension_page(pte);
  3898. } else if (pte->device->extension == EXTENSION_TN) {
  3899. ast_mutex_lock(&devicelock);
  3900. strcpy(pte->device->id, pte->device->extension_number);
  3901. pte->buff_entry[0] = '\0';
  3902. pte->size_buff_entry = 0;
  3903. pte->device->session = NULL;
  3904. pte->device = NULL;
  3905. ast_mutex_unlock(&devicelock);
  3906. show_extension_page(pte);
  3907. } else { /* Pickup function */
  3908. /* XXX Is there a way to get a specific channel here? */
  3909. RAII_VAR(struct ast_features_pickup_config *, pickup_cfg,
  3910. ast_get_chan_features_pickup_config(NULL), ao2_cleanup);
  3911. if (!pickup_cfg) {
  3912. ast_log(LOG_ERROR, "Unable to retrieve pickup configuration options. Unable to detect call pickup extension\n");
  3913. break;
  3914. }
  3915. pte->device->selected = -1;
  3916. ast_copy_string(pte->device->phone_number, pickup_cfg->pickupexten,
  3917. sizeof(pte->device->phone_number));
  3918. handle_call_outgoing(pte);
  3919. }
  3920. break;
  3921. case KEY_FAV0:
  3922. case KEY_FAV1:
  3923. case KEY_FAV2:
  3924. case KEY_FAV3:
  3925. case KEY_FAV4:
  3926. case KEY_FAV5:
  3927. handle_key_fav(pte, keycode);
  3928. break;
  3929. case KEY_CONF:
  3930. handle_select_option(pte);
  3931. break;
  3932. case KEY_LOUDSPK:
  3933. send_select_output(pte, OUTPUT_SPEAKER, pte->device->volume, MUTE_OFF);
  3934. handle_dial_page(pte);
  3935. break;
  3936. case KEY_HEADPHN:
  3937. send_select_output(pte, OUTPUT_HEADPHONE, pte->device->volume, MUTE_OFF);
  3938. handle_dial_page(pte);
  3939. break;
  3940. case KEY_SNDHIST:
  3941. show_history(pte, 'o');
  3942. break;
  3943. case KEY_RCVHIST:
  3944. show_history(pte, 'i');
  3945. break;
  3946. }
  3947. return;
  3948. }
  3949. static void key_history(struct unistimsession *pte, char keycode)
  3950. {
  3951. FILE *f;
  3952. char count;
  3953. long offset;
  3954. int flag = 0;
  3955. switch (keycode) {
  3956. case KEY_LEFT:
  3957. if (pte->device->height == 1) {
  3958. if (pte->buff_entry[3] <= 1) {
  3959. return;
  3960. }
  3961. pte->buff_entry[3]--;
  3962. flag = 1;
  3963. break;
  3964. }
  3965. case KEY_UP:
  3966. case KEY_FUNC1:
  3967. if (pte->buff_entry[2] <= 1) {
  3968. return;
  3969. }
  3970. pte->buff_entry[2]--;
  3971. flag = 1;
  3972. break;
  3973. case KEY_RIGHT:
  3974. if (pte->device->height == 1) {
  3975. if (pte->buff_entry[3] == 3) {
  3976. return;
  3977. }
  3978. pte->buff_entry[3]++;
  3979. flag = 1;
  3980. break;
  3981. }
  3982. case KEY_DOWN:
  3983. case KEY_FUNC2:
  3984. if (pte->buff_entry[2] >= pte->buff_entry[1]) {
  3985. return;
  3986. }
  3987. pte->buff_entry[2]++;
  3988. flag = 1;
  3989. break;
  3990. case KEY_FUNC3:
  3991. if (ast_strlen_zero(pte->device->lst_cid)) {
  3992. break;
  3993. }
  3994. ast_copy_string(pte->device->redial_number, pte->device->lst_cid,
  3995. sizeof(pte->device->redial_number));
  3996. key_main_page(pte, KEY_FUNC2);
  3997. break;
  3998. case KEY_FUNC4:
  3999. case KEY_HANGUP:
  4000. show_main_page(pte);
  4001. break;
  4002. case KEY_SNDHIST:
  4003. if (pte->buff_entry[0] == 'i') {
  4004. show_history(pte, 'o');
  4005. } else {
  4006. show_main_page(pte);
  4007. }
  4008. break;
  4009. case KEY_RCVHIST:
  4010. if (pte->buff_entry[0] == 'i') {
  4011. show_main_page(pte);
  4012. } else {
  4013. show_history(pte, 'i');
  4014. }
  4015. break;
  4016. }
  4017. if (flag) {
  4018. count = open_history(pte, pte->buff_entry[0], &f);
  4019. if (!count) {
  4020. return;
  4021. }
  4022. offset = ((pte->buff_entry[2] - 1) * TEXT_LENGTH_MAX * 3);
  4023. if (fseek(f, offset, SEEK_CUR)) {
  4024. display_last_error("Unable to seek history entry.");
  4025. fclose(f);
  4026. return;
  4027. }
  4028. show_entry_history(pte, &f);
  4029. }
  4030. return;
  4031. }
  4032. static void init_phone_step2(struct unistimsession *pte)
  4033. {
  4034. BUFFSEND;
  4035. if (unistimdebug) {
  4036. ast_verb(0, "Sending S4\n");
  4037. }
  4038. memcpy(buffsend + SIZE_HEADER, packet_send_s4, sizeof(packet_send_s4));
  4039. send_client(SIZE_HEADER + sizeof(packet_send_s4), buffsend, pte);
  4040. send_date_time2(pte);
  4041. send_date_time3(pte);
  4042. if (unistimdebug) {
  4043. ast_verb(0, "Sending S7\n");
  4044. }
  4045. memcpy(buffsend + SIZE_HEADER, packet_send_S7, sizeof(packet_send_S7));
  4046. send_client(SIZE_HEADER + sizeof(packet_send_S7), buffsend, pte);
  4047. if (unistimdebug) {
  4048. ast_verb(0, "Sending Contrast\n");
  4049. }
  4050. memcpy(buffsend + SIZE_HEADER, packet_send_Contrast, sizeof(packet_send_Contrast));
  4051. if (pte->device != NULL) {
  4052. buffsend[9] = pte->device->contrast;
  4053. }
  4054. send_client(SIZE_HEADER + sizeof(packet_send_Contrast), buffsend, pte);
  4055. if (unistimdebug) {
  4056. ast_verb(0, "Sending S9\n");
  4057. }
  4058. memcpy(buffsend + SIZE_HEADER, packet_send_s9, sizeof(packet_send_s9));
  4059. send_client(SIZE_HEADER + sizeof(packet_send_s9), buffsend, pte);
  4060. send_no_ring(pte);
  4061. if (unistimdebug) {
  4062. ast_verb(0, "Sending S7\n");
  4063. }
  4064. memcpy(buffsend + SIZE_HEADER, packet_send_S7, sizeof(packet_send_S7));
  4065. send_client(SIZE_HEADER + sizeof(packet_send_S7), buffsend, pte);
  4066. send_led_update(pte, LED_BAR_OFF);
  4067. send_ping(pte);
  4068. if (unistimdebug) {
  4069. ast_verb(0, "Sending init language\n");
  4070. }
  4071. if (pte->device) {
  4072. send_charset_update(pte, options_languages[find_language(pte->device->language)].encoding);
  4073. }
  4074. if (pte->state < STATE_MAINPAGE) {
  4075. if (autoprovisioning == AUTOPROVISIONING_TN) {
  4076. show_extension_page(pte);
  4077. return;
  4078. } else {
  4079. int i;
  4080. char tmp[30];
  4081. for (i = 1; i < FAVNUM; i++) {
  4082. send_favorite(i, 0, pte, "");
  4083. }
  4084. send_text(TEXT_LINE0, TEXT_NORMAL, pte, ustmtext("Phone is not registered", pte));
  4085. send_text(TEXT_LINE1, TEXT_NORMAL, pte, ustmtext("in unistim.conf", pte));
  4086. strcpy(tmp, "MAC = ");
  4087. strcat(tmp, pte->macaddr);
  4088. send_text(TEXT_LINE2, TEXT_NORMAL, pte, tmp);
  4089. send_text_status(pte, "");
  4090. send_texttitle(pte, ustmtext("UNISTIM for*", pte));
  4091. return;
  4092. }
  4093. }
  4094. show_main_page(pte);
  4095. refresh_all_favorite(pte);
  4096. if (unistimdebug) {
  4097. ast_verb(0, "Sending arrow\n");
  4098. }
  4099. memcpy(buffsend + SIZE_HEADER, packet_send_arrow, sizeof(packet_send_arrow));
  4100. send_client(SIZE_HEADER + sizeof(packet_send_arrow), buffsend, pte);
  4101. return;
  4102. }
  4103. /* Toggles the state of microphone muting */
  4104. static void microphone_mute_toggle(struct unistimsession *pte)
  4105. {
  4106. if (pte->device->microphone == MUTE_OFF) {
  4107. pte->device->microphone = MUTE_ON;
  4108. send_led_update(pte, LED_MUTE_ON);
  4109. } else if (pte->device->microphone == MUTE_ON) {
  4110. pte->device->microphone = MUTE_OFF;
  4111. send_led_update(pte, LED_MUTE_OFF);
  4112. }
  4113. send_mute(pte, (pte->device->microphone & 0x01));
  4114. }
  4115. static void process_request(int size, unsigned char *buf, struct unistimsession *pte)
  4116. {
  4117. char tmpbuf[255];
  4118. if (memcmp
  4119. (buf + SIZE_HEADER, packet_recv_resume_connection_with_server,
  4120. sizeof(packet_recv_resume_connection_with_server)) == 0) {
  4121. rcv_resume_connection_with_server(pte);
  4122. return;
  4123. }
  4124. if (memcmp(buf + SIZE_HEADER, packet_recv_firm_version, sizeof(packet_recv_firm_version)) == 0) {
  4125. buf[size] = 0;
  4126. if (unistimdebug) {
  4127. ast_verb(0, "Got the firmware version : '%s'\n", buf + 13);
  4128. }
  4129. ast_copy_string(pte->firmware, (char *) (buf + 13), sizeof(pte->firmware));
  4130. init_phone_step2(pte);
  4131. return;
  4132. }
  4133. if (memcmp(buf + SIZE_HEADER, packet_recv_it_type, sizeof(packet_recv_it_type)) == 0) {
  4134. char type = buf[13];
  4135. if (unistimdebug) {
  4136. ast_verb(0, "Got the equipment type: '%d'\n", type);
  4137. }
  4138. switch (type) {
  4139. case 0x03: /* i2002 */
  4140. if (pte->device) {
  4141. pte->device->height = 1;
  4142. }
  4143. break;
  4144. }
  4145. return;
  4146. }
  4147. if (memcmp(buf + SIZE_HEADER, packet_recv_mac_addr, sizeof(packet_recv_mac_addr)) == 0) {
  4148. rcv_mac_addr(pte, buf);
  4149. return;
  4150. }
  4151. if (memcmp(buf + SIZE_HEADER, packet_recv_r2, sizeof(packet_recv_r2)) == 0) {
  4152. if (unistimdebug) {
  4153. ast_verb(0, "R2 received\n");
  4154. }
  4155. return;
  4156. }
  4157. if (pte->state < STATE_MAINPAGE) {
  4158. if (unistimdebug) {
  4159. ast_verb(0, "Request not authorized in this state\n");
  4160. }
  4161. return;
  4162. }
  4163. if (!memcmp(buf + SIZE_HEADER, packet_recv_expansion_pressed_key, sizeof(packet_recv_expansion_pressed_key))) {
  4164. char keycode = buf[13];
  4165. if (unistimdebug) {
  4166. ast_verb(0, "Expansion key pressed: keycode = 0x%02hhx - current state: %s\n", (unsigned char)keycode,
  4167. ptestate_tostr(pte->state));
  4168. }
  4169. }
  4170. if (!memcmp(buf + SIZE_HEADER, packet_recv_pressed_key, sizeof(packet_recv_pressed_key))) {
  4171. char keycode = buf[13];
  4172. if (unistimdebug) {
  4173. ast_verb(0, "Key pressed: keycode = 0x%02hhx - current state: %s\n", (unsigned char)keycode,
  4174. ptestate_tostr(pte->state));
  4175. }
  4176. if (keycode == KEY_MUTE) {
  4177. microphone_mute_toggle(pte);
  4178. }
  4179. switch (pte->state) {
  4180. case STATE_INIT:
  4181. if (unistimdebug) {
  4182. ast_verb(0, "No keys allowed in the init state\n");
  4183. }
  4184. break;
  4185. case STATE_AUTHDENY:
  4186. if (unistimdebug) {
  4187. ast_verb(0, "No keys allowed in authdeny state\n");
  4188. }
  4189. break;
  4190. case STATE_MAINPAGE:
  4191. key_main_page(pte, keycode);
  4192. break;
  4193. case STATE_DIALPAGE:
  4194. key_dial_page(pte, keycode);
  4195. break;
  4196. case STATE_RINGING:
  4197. key_ringing(pte, keycode);
  4198. break;
  4199. case STATE_CALL:
  4200. key_call(pte, keycode);
  4201. break;
  4202. case STATE_EXTENSION:
  4203. key_select_extension(pte, keycode);
  4204. break;
  4205. case STATE_SELECTOPTION:
  4206. key_select_option(pte, keycode);
  4207. break;
  4208. case STATE_SELECTCODEC:
  4209. key_select_codec(pte, keycode);
  4210. break;
  4211. case STATE_SELECTLANGUAGE:
  4212. key_select_language(pte, keycode);
  4213. break;
  4214. case STATE_HISTORY:
  4215. key_history(pte, keycode);
  4216. break;
  4217. default:
  4218. ast_log(LOG_WARNING, "Key : Unknown state\n");
  4219. }
  4220. return;
  4221. }
  4222. if (memcmp(buf + SIZE_HEADER, packet_recv_pick_up, sizeof(packet_recv_pick_up)) == 0) {
  4223. if (unistimdebug) {
  4224. ast_verb(0, "Handset off hook, current state: %s\n", ptestate_tostr(pte->state));
  4225. }
  4226. if (!pte->device) { /* We are not yet registered (asking for a TN in AUTOPROVISIONING_TN) */
  4227. return;
  4228. }
  4229. pte->device->receiver_state = STATE_OFFHOOK;
  4230. if (pte->device->output == OUTPUT_HEADPHONE) {
  4231. send_select_output(pte, OUTPUT_HEADPHONE, pte->device->volume, MUTE_OFF);
  4232. } else {
  4233. send_select_output(pte, OUTPUT_HANDSET, pte->device->volume, MUTE_OFF);
  4234. }
  4235. if (pte->state == STATE_RINGING) {
  4236. handle_call_incoming(pte);
  4237. } else if ((pte->state == STATE_DIALPAGE) || (pte->state == STATE_CALL)) {
  4238. send_select_output(pte, OUTPUT_HANDSET, pte->device->volume, MUTE_OFF);
  4239. } else if (pte->state == STATE_EXTENSION) { /* We must have a TN before calling */
  4240. return;
  4241. } else {
  4242. pte->device->selected = get_avail_softkey(pte, NULL);
  4243. send_select_output(pte, OUTPUT_HANDSET, pte->device->volume, MUTE_OFF);
  4244. handle_dial_page(pte);
  4245. }
  4246. return;
  4247. }
  4248. if (memcmp(buf + SIZE_HEADER, packet_recv_hangup, sizeof(packet_recv_hangup)) == 0) {
  4249. if (unistimdebug) {
  4250. ast_verb(0, "Handset on hook, current state: %s\n", ptestate_tostr(pte->state));
  4251. }
  4252. if (!pte->device) {
  4253. return;
  4254. }
  4255. pte->device->receiver_state = STATE_ONHOOK;
  4256. if (pte->state == STATE_CALL) {
  4257. if (pte->device->output != OUTPUT_SPEAKER) {
  4258. close_call(pte);
  4259. }
  4260. } else if (pte->state == STATE_EXTENSION) {
  4261. return;
  4262. } else {
  4263. pte->device->nextdial = 0;
  4264. show_main_page(pte);
  4265. }
  4266. return;
  4267. }
  4268. strcpy(tmpbuf, ast_inet_ntoa(pte->sin.sin_addr));
  4269. strcat(tmpbuf, " Unknown request packet\n");
  4270. if (unistimdebug) {
  4271. ast_debug(1, "%s", tmpbuf);
  4272. }
  4273. return;
  4274. }
  4275. static void parsing(int size, unsigned char *buf, struct unistimsession *pte,
  4276. struct sockaddr_in *addr_from)
  4277. {
  4278. unsigned short *sbuf = (unsigned short *) buf;
  4279. unsigned short seq;
  4280. char tmpbuf[255];
  4281. strcpy(tmpbuf, ast_inet_ntoa(addr_from->sin_addr));
  4282. if (size < 10) {
  4283. if (size == 0) {
  4284. ast_log(LOG_WARNING, "%s Read error\n", tmpbuf);
  4285. } else {
  4286. ast_log(LOG_NOTICE, "%s Packet too short - ignoring\n", tmpbuf);
  4287. }
  4288. return;
  4289. }
  4290. if (sbuf[0] == 0xffff) { /* Starting with 0xffff ? *//* Yes, discovery packet ? */
  4291. if (size != sizeof(packet_rcv_discovery)) {
  4292. ast_log(LOG_NOTICE, "%s Invalid size of a discovery packet\n", tmpbuf);
  4293. } else {
  4294. if (memcmp(buf, packet_rcv_discovery, sizeof(packet_rcv_discovery)) == 0) {
  4295. if (unistimdebug) {
  4296. ast_verb(0, "Discovery packet received - Sending Discovery ACK\n");
  4297. }
  4298. if (pte) { /* A session was already active for this IP ? */
  4299. if (pte->state == STATE_INIT) { /* Yes, but it's a dupe */
  4300. if (unistimdebug) {
  4301. ast_verb(1, "Duplicated Discovery packet\n");
  4302. }
  4303. send_raw_client(sizeof(packet_send_discovery_ack),
  4304. packet_send_discovery_ack, addr_from, &pte->sout);
  4305. pte->seq_phone = (short) 0x0000; /* reset sequence number */
  4306. } else { /* No, probably a reboot, phone side */
  4307. close_client(pte); /* Cleanup the previous session */
  4308. if (create_client(addr_from)) {
  4309. send_raw_client(sizeof(packet_send_discovery_ack),
  4310. packet_send_discovery_ack, addr_from, &pte->sout);
  4311. }
  4312. }
  4313. } else {
  4314. /* Creating new entry in our phone list */
  4315. if ((pte = create_client(addr_from))) {
  4316. send_raw_client(sizeof(packet_send_discovery_ack),
  4317. packet_send_discovery_ack, addr_from, &pte->sout);
  4318. }
  4319. }
  4320. return;
  4321. }
  4322. ast_log(LOG_NOTICE, "%s Invalid discovery packet\n", tmpbuf);
  4323. }
  4324. return;
  4325. }
  4326. if (!pte) {
  4327. if (unistimdebug) {
  4328. ast_verb(0, "%s Not a discovery packet from an unknown source : ignoring\n", tmpbuf);
  4329. }
  4330. return;
  4331. }
  4332. if (sbuf[0] != 0) { /* Starting with something else than 0x0000 ? */
  4333. ast_log(LOG_NOTICE, "Unknown packet received - ignoring\n");
  4334. return;
  4335. }
  4336. if (buf[5] != 2) {
  4337. ast_log(LOG_NOTICE, "%s Wrong direction : got 0x%02hhx expected 0x02\n", tmpbuf, buf[5]);
  4338. return;
  4339. }
  4340. seq = ntohs(sbuf[1]);
  4341. if (buf[4] == 1) {
  4342. ast_mutex_lock(&pte->lock);
  4343. if (unistimdebug) {
  4344. ast_verb(0, "ACK received for packet #0x%04x\n", (unsigned)seq);
  4345. }
  4346. pte->nb_retransmit = 0;
  4347. if ((pte->last_seq_ack) + 1 == seq) {
  4348. pte->last_seq_ack++;
  4349. check_send_queue(pte);
  4350. ast_mutex_unlock(&pte->lock);
  4351. return;
  4352. }
  4353. if (pte->last_seq_ack > seq) {
  4354. if (pte->last_seq_ack == 0xffff) {
  4355. ast_verb(0, "ACK at 0xffff, restarting counter.\n");
  4356. pte->last_seq_ack = 0;
  4357. } else {
  4358. ast_log(LOG_NOTICE,
  4359. "%s Warning : ACK received for an already ACKed packet : #0x%04x we are at #0x%04x\n",
  4360. tmpbuf, (unsigned)seq, (unsigned)pte->last_seq_ack);
  4361. }
  4362. ast_mutex_unlock(&pte->lock);
  4363. return;
  4364. }
  4365. if (pte->seq_server < seq) {
  4366. ast_log(LOG_NOTICE,
  4367. "%s Error : ACK received for a non-existent packet : #0x%04x\n",
  4368. tmpbuf, (unsigned)pte->seq_server);
  4369. ast_mutex_unlock(&pte->lock);
  4370. return;
  4371. }
  4372. if (unistimdebug) {
  4373. ast_verb(0, "%s ACK gap : Received ACK #0x%04x, previous was #0x%04x\n",
  4374. tmpbuf, (unsigned)seq, (unsigned)pte->last_seq_ack);
  4375. }
  4376. pte->last_seq_ack = seq;
  4377. check_send_queue(pte);
  4378. ast_mutex_unlock(&pte->lock);
  4379. return;
  4380. }
  4381. if (buf[4] == 2) {
  4382. if (unistimdebug) {
  4383. ast_verb(0, "Request received\n");
  4384. }
  4385. if (pte->seq_phone == seq) {
  4386. /* Send ACK */
  4387. buf[4] = 1;
  4388. buf[5] = 1;
  4389. send_raw_client(SIZE_HEADER, buf, addr_from, &pte->sout);
  4390. pte->seq_phone++;
  4391. process_request(size, buf, pte);
  4392. return;
  4393. }
  4394. if (pte->seq_phone > seq) {
  4395. ast_log(LOG_NOTICE,
  4396. "%s Warning : received a retransmitted packet : #0x%04x (we are at #0x%04x)\n",
  4397. tmpbuf, (unsigned)seq, (unsigned)pte->seq_phone);
  4398. /* BUG ? pte->device->seq_phone = seq; */
  4399. /* Send ACK */
  4400. buf[4] = 1;
  4401. buf[5] = 1;
  4402. send_raw_client(SIZE_HEADER, buf, addr_from, &pte->sout);
  4403. return;
  4404. }
  4405. ast_log(LOG_NOTICE,
  4406. "%s Warning : we lost a packet : received #0x%04x (we are at #0x%04x)\n",
  4407. tmpbuf, (unsigned)seq, (unsigned)pte->seq_phone);
  4408. return;
  4409. }
  4410. if (buf[4] == 0) {
  4411. ast_log(LOG_NOTICE, "%s Retransmit request for packet #0x%04x\n", tmpbuf, (unsigned)seq);
  4412. if (pte->last_seq_ack > seq) {
  4413. ast_log(LOG_NOTICE,
  4414. "%s Error : received a request for an already ACKed packet : #0x%04x\n",
  4415. tmpbuf, (unsigned)pte->last_seq_ack);
  4416. return;
  4417. }
  4418. if (pte->seq_server < seq) {
  4419. ast_log(LOG_NOTICE,
  4420. "%s Error : received a request for a non-existent packet : #0x%04x\n",
  4421. tmpbuf, (unsigned)pte->seq_server);
  4422. return;
  4423. }
  4424. send_retransmit(pte);
  4425. return;
  4426. }
  4427. ast_log(LOG_NOTICE, "%s Unknown request : got 0x%02hhx expected 0x00,0x01 or 0x02\n", tmpbuf, buf[4]);
  4428. return;
  4429. }
  4430. static struct unistimsession *channel_to_session(struct ast_channel *ast)
  4431. {
  4432. struct unistim_subchannel *sub;
  4433. if (!ast) {
  4434. ast_log(LOG_WARNING, "Unistim callback function called with a null channel\n");
  4435. return NULL;
  4436. }
  4437. if (!ast_channel_tech_pvt(ast)) {
  4438. ast_log(LOG_WARNING, "Unistim callback function called without a tech_pvt\n");
  4439. return NULL;
  4440. }
  4441. sub = ast_channel_tech_pvt(ast);
  4442. if (!sub->parent) {
  4443. ast_log(LOG_WARNING, "Unistim callback function called without a line\n");
  4444. return NULL;
  4445. }
  4446. if (!sub->parent->parent) {
  4447. ast_log(LOG_WARNING, "Unistim callback function called without a device\n");
  4448. return NULL;
  4449. }
  4450. ast_mutex_lock(&sub->parent->parent->lock);
  4451. if (!sub->parent->parent->session) {
  4452. ast_log(LOG_WARNING, "Unistim callback function called without a session\n");
  4453. ast_mutex_unlock(&sub->parent->parent->lock);
  4454. return NULL;
  4455. }
  4456. ast_mutex_unlock(&sub->parent->parent->lock);
  4457. return sub->parent->parent->session;
  4458. }
  4459. static void send_callerid_screen(struct unistimsession *pte, struct unistim_subchannel *sub)
  4460. {
  4461. char *cidname_str;
  4462. char *cidnum_str;
  4463. if (!sub) {
  4464. return;
  4465. }
  4466. if (sub->owner) {
  4467. if (ast_channel_connected(sub->owner)->id.number.valid && ast_channel_connected(sub->owner)->id.number.str) {
  4468. cidnum_str = ast_channel_connected(sub->owner)->id.number.str;
  4469. } else {
  4470. cidnum_str = DEFAULTCALLERID;
  4471. }
  4472. change_callerid(pte, 0, cidnum_str);
  4473. if (strlen(cidnum_str) == 0) {
  4474. cidnum_str = DEFAULTCALLERID;
  4475. }
  4476. if (ast_channel_connected(sub->owner)->id.name.valid && ast_channel_connected(sub->owner)->id.name.str) {
  4477. cidname_str = ast_channel_connected(sub->owner)->id.name.str;
  4478. } else {
  4479. cidname_str = DEFAULTCALLERNAME;
  4480. }
  4481. change_callerid(pte, 1, cidname_str);
  4482. if (strlen(cidname_str) == 0) {
  4483. cidname_str = DEFAULTCALLERNAME;
  4484. }
  4485. if (pte->device->height == 1) {
  4486. char tmpstr[256];
  4487. snprintf(tmpstr, sizeof(tmpstr), "%s %s", cidnum_str, ustmtext(cidname_str, pte));
  4488. send_text(TEXT_LINE0, TEXT_NORMAL, pte, tmpstr);
  4489. } else {
  4490. send_text(TEXT_LINE0, TEXT_NORMAL, pte, cidname_str);
  4491. send_text(TEXT_LINE1, TEXT_NORMAL, pte, ustmtext(cidnum_str, pte));
  4492. }
  4493. }
  4494. }
  4495. /*--- unistim_call: Initiate UNISTIM call from PBX ---*/
  4496. /* used from the dial() application */
  4497. static int unistim_call(struct ast_channel *ast, const char *dest, int timeout)
  4498. {
  4499. int res = 0, i;
  4500. struct unistim_subchannel *sub, *sub_real;
  4501. struct unistimsession *session;
  4502. signed char ringstyle, ringvolume;
  4503. session = channel_to_session(ast);
  4504. if (!session) {
  4505. ast_log(LOG_ERROR, "Device not registered, cannot call %s\n", dest);
  4506. return -1;
  4507. }
  4508. sub = ast_channel_tech_pvt(ast);
  4509. sub_real = get_sub(session->device, SUB_REAL);
  4510. if ((ast_channel_state(ast) != AST_STATE_DOWN) && (ast_channel_state(ast) != AST_STATE_RESERVED)) {
  4511. ast_log(LOG_WARNING, "unistim_call called on %s, neither down nor reserved\n",
  4512. ast_channel_name(ast));
  4513. return -1;
  4514. }
  4515. if (unistimdebug) {
  4516. ast_verb(3, "unistim_call(%s)\n", ast_channel_name(ast));
  4517. }
  4518. session->state = STATE_RINGING;
  4519. send_callerid_screen(session, sub);
  4520. if (ast_strlen_zero(ast_channel_call_forward(ast))) { /* Send ring only if no call forward, otherwise short ring will apear */
  4521. send_text(TEXT_LINE2, TEXT_NORMAL, session, ustmtext("is calling you.", session));
  4522. send_text_status(session, ustmtext("Accept Ignore Hangup", session));
  4523. if (sub_real) {
  4524. ringstyle = session->device->cwstyle;
  4525. ringvolume = session->device->cwvolume;
  4526. } else {
  4527. ringstyle = (sub->ringstyle == -1) ? session->device->ringstyle : sub->ringstyle;
  4528. ringvolume = (sub->ringvolume == -1) ? session->device->ringvolume : sub->ringvolume;
  4529. }
  4530. send_ring(session, ringvolume, ringstyle);
  4531. change_favorite_icon(session, FAV_ICON_SPEAKER_ONHOOK_BLACK + FAV_BLINK_FAST);
  4532. /* Send call identification to all */
  4533. for (i = 0; i < FAVNUM; i++) {
  4534. if (!soft_key_visible(session->device, i)) {
  4535. continue;
  4536. }
  4537. if (session->device->ssub[i]) {
  4538. continue;
  4539. }
  4540. if (is_key_line(session->device, i) && !strcmp(sub->parent->name, session->device->sline[i]->name)) {
  4541. if (unistimdebug) {
  4542. ast_verb(0, "Found softkey %d for line %s\n", i, sub->parent->name);
  4543. }
  4544. send_favorite_short(i, FAV_ICON_SPEAKER_ONHOOK_BLACK + FAV_BLINK_FAST, session);
  4545. session->device->ssub[i] = sub;
  4546. }
  4547. }
  4548. }
  4549. ast_setstate(ast, AST_STATE_RINGING);
  4550. ast_queue_control(ast, AST_CONTROL_RINGING);
  4551. return res;
  4552. }
  4553. static int unistim_hangup_clean(struct ast_channel *ast, struct unistim_subchannel *sub) {
  4554. ast_mutex_lock(&sub->lock);
  4555. ast_channel_tech_pvt_set(ast, NULL);
  4556. unistim_set_owner(sub, NULL);
  4557. sub->alreadygone = 0;
  4558. if (sub->rtp) {
  4559. if (unistimdebug) {
  4560. ast_verb(0, "Destroying RTP session\n");
  4561. }
  4562. ast_rtp_instance_stop(sub->rtp);
  4563. ast_rtp_instance_destroy(sub->rtp);
  4564. sub->rtp = NULL;
  4565. }
  4566. ast_mutex_unlock(&sub->lock);
  4567. return 0;
  4568. }
  4569. /*--- unistim_hangup: Hangup UNISTIM call */
  4570. static int unistim_hangup(struct ast_channel *ast)
  4571. {
  4572. struct unistim_subchannel *sub = NULL, *sub_real = NULL, *sub_trans = NULL;
  4573. struct unistim_line *l;
  4574. struct unistim_device *d;
  4575. struct unistimsession *s;
  4576. int i,end_call = 1;
  4577. s = channel_to_session(ast);
  4578. sub = ast_channel_tech_pvt(ast);
  4579. l = sub->parent;
  4580. d = l->parent;
  4581. if (!s) {
  4582. ast_debug(1, "Asked to hangup channel not connected\n");
  4583. unistim_hangup_clean(ast, sub);
  4584. return 0;
  4585. }
  4586. if (unistimdebug) {
  4587. ast_verb(0, "unistim_hangup(%s) on %s@%s (STATE_%s)\n", ast_channel_name(ast), l->name, l->parent->name, ptestate_tostr(s->state));
  4588. }
  4589. sub_trans = get_sub(d, SUB_THREEWAY);
  4590. sub_real = get_sub(d, SUB_REAL);
  4591. if (sub_trans && (sub_trans->owner) && (sub->subtype == SUB_REAL)) { /* 3rd party busy or congested and transfer_cancel_step2 does not called */
  4592. if (unistimdebug) {
  4593. ast_verb(0, "Threeway call disconnected, switching to real call\n");
  4594. }
  4595. ast_queue_unhold(sub_trans->owner);
  4596. sub_trans->moh = 0;
  4597. sub_trans->subtype = SUB_REAL;
  4598. swap_subs(sub_trans, sub);
  4599. send_text_status(s, ustmtext(" Transf Hangup", s));
  4600. send_callerid_screen(s, sub_trans);
  4601. unistim_hangup_clean(ast, sub);
  4602. unistim_unalloc_sub(d, sub);
  4603. return 0;
  4604. }
  4605. if (sub_real && (sub_real->owner) && (sub->subtype == SUB_THREEWAY) && (s->state == STATE_CALL)) { /* 3way call cancelled by softkey pressed */
  4606. if (unistimdebug) {
  4607. ast_verb(0, "Real call disconnected, stay in call\n");
  4608. }
  4609. send_text_status(s, ustmtext(" Transf Hangup", s));
  4610. send_callerid_screen(s, sub_real);
  4611. unistim_hangup_clean(ast, sub);
  4612. unistim_unalloc_sub(d, sub);
  4613. return 0;
  4614. }
  4615. if (sub->subtype == SUB_REAL) {
  4616. sub_stop_silence(s, sub);
  4617. } else if (sub->subtype == SUB_RING) {
  4618. send_no_ring(s);
  4619. for (i = 0; i < FAVNUM; i++) {
  4620. if (!soft_key_visible(s->device, i)) {
  4621. continue;
  4622. }
  4623. if (d->ssub[i] != sub) {
  4624. if (d->ssub[i] != NULL) { /* Found other subchannel active other than hangup'ed one */
  4625. end_call = 0;
  4626. }
  4627. continue;
  4628. }
  4629. if (is_key_line(d, i) && !strcmp(l->name, d->sline[i]->name)) {
  4630. send_favorite_short(i, FAV_LINE_ICON, s);
  4631. d->ssub[i] = NULL;
  4632. continue;
  4633. }
  4634. }
  4635. }
  4636. if (end_call) {
  4637. send_end_call(s); /* Send end call packet only if ending active call, in other way sound should be loosed */
  4638. }
  4639. sub->moh = 0;
  4640. if (sub->softkey >= 0) {
  4641. send_favorite_short(sub->softkey, FAV_LINE_ICON, s);
  4642. }
  4643. /* Delete assign sub to softkey */
  4644. for (i = 0; i < FAVNUM; i++) {
  4645. if (d->ssub[i] == sub) {
  4646. d->ssub[i] = NULL;
  4647. break;
  4648. }
  4649. }
  4650. /*refresh_all_favorite(s); */ /* TODO: Update favicons in case of DND keys */
  4651. if (s->state == STATE_RINGING && sub->subtype == SUB_RING) {
  4652. send_no_ring(s);
  4653. if (ast_channel_hangupcause(ast) != AST_CAUSE_ANSWERED_ELSEWHERE) {
  4654. d->missed_call++;
  4655. write_history(s, 'i', 1);
  4656. }
  4657. if (!sub_real) {
  4658. show_main_page(s);
  4659. } else { /* hangup on a ringing line: reset status to reflect that we're still on an active call */
  4660. s->state = STATE_CALL;
  4661. send_callerid_screen(s, sub_real);
  4662. send_text(TEXT_LINE2, TEXT_NORMAL, s, ustmtext("is on-line", s));
  4663. send_text_status(s, ustmtext(" Transf Hangup", s));
  4664. send_favorite_short(sub->softkey, FAV_ICON_OFFHOOK_BLACK, s);
  4665. }
  4666. }
  4667. if (s->state == STATE_CALL && sub->subtype == SUB_REAL) {
  4668. close_call(s);
  4669. }
  4670. sub->softkey = -1;
  4671. unistim_hangup_clean(ast, sub);
  4672. unistim_unalloc_sub(d, sub);
  4673. return 0;
  4674. }
  4675. /*--- unistim_answer: Answer UNISTIM call */
  4676. static int unistim_answer(struct ast_channel *ast)
  4677. {
  4678. int res = 0;
  4679. struct unistim_subchannel *sub;
  4680. struct unistim_line *l;
  4681. struct unistim_device *d;
  4682. struct unistimsession *s;
  4683. s = channel_to_session(ast);
  4684. if (!s) {
  4685. ast_log(LOG_WARNING, "unistim_answer on a disconnected device ?\n");
  4686. return -1;
  4687. }
  4688. sub = ast_channel_tech_pvt(ast);
  4689. l = sub->parent;
  4690. d = l->parent;
  4691. if (unistimdebug) {
  4692. ast_verb(0, "unistim_answer(%s) on %s@%s-%d\n", ast_channel_name(ast), l->name,
  4693. l->parent->name, sub->softkey);
  4694. }
  4695. send_text(TEXT_LINE2, TEXT_NORMAL, s, ustmtext("is now on-line", s));
  4696. if (get_sub(d, SUB_THREEWAY)) {
  4697. send_text_status(s, ustmtext("Transf Cancel", s));
  4698. } else {
  4699. send_text_status(s, ustmtext(" Transf Hangup", s));
  4700. }
  4701. send_start_timer(s);
  4702. if (ast_channel_state(ast) != AST_STATE_UP)
  4703. ast_setstate(ast, AST_STATE_UP);
  4704. return res;
  4705. }
  4706. /*--- unistimsock_read: Read data from UNISTIM socket ---*/
  4707. /* Successful messages is connected to UNISTIM call and forwarded to parsing() */
  4708. static int unistimsock_read(int *id, int fd, short events, void *ignore)
  4709. {
  4710. struct sockaddr_in addr_from = { 0, };
  4711. struct unistimsession *cur = NULL;
  4712. int found = 0;
  4713. int tmp = 0;
  4714. int dw_num_bytes_rcvd;
  4715. unsigned int size_addr_from;
  4716. #ifdef DUMP_PACKET
  4717. int dw_num_bytes_rcvdd;
  4718. #endif
  4719. size_addr_from = sizeof(addr_from);
  4720. dw_num_bytes_rcvd =
  4721. recvfrom(unistimsock, buff, SIZE_PAGE, 0, (struct sockaddr *) &addr_from,
  4722. &size_addr_from);
  4723. if (dw_num_bytes_rcvd == -1) {
  4724. if (errno == EAGAIN) {
  4725. ast_log(LOG_NOTICE, "UNISTIM: Received packet with bad UDP checksum\n");
  4726. } else if (errno != ECONNREFUSED) {
  4727. ast_log(LOG_WARNING, "Recv error %d (%s)\n", errno, strerror(errno));
  4728. }
  4729. return 1;
  4730. }
  4731. /* Looking in the phone list if we already have a registration for him */
  4732. ast_mutex_lock(&sessionlock);
  4733. cur = sessions;
  4734. while (cur) {
  4735. if (cur->sin.sin_addr.s_addr == addr_from.sin_addr.s_addr) {
  4736. found = 1;
  4737. break;
  4738. }
  4739. tmp++;
  4740. cur = cur->next;
  4741. }
  4742. ast_mutex_unlock(&sessionlock);
  4743. #ifdef DUMP_PACKET
  4744. if (unistimdebug)
  4745. ast_verb(0, "\n*** Dump %d bytes from %s - phone_table[%d] ***\n",
  4746. dw_num_bytes_rcvd, ast_inet_ntoa(addr_from.sin_addr), tmp);
  4747. for (dw_num_bytes_rcvdd = 0; dw_num_bytes_rcvdd < dw_num_bytes_rcvd;
  4748. dw_num_bytes_rcvdd++)
  4749. ast_verb(0, "%02hhx ", buff[dw_num_bytes_rcvdd]);
  4750. ast_verb(0, "\n******************************************\n");
  4751. #endif
  4752. if (!found) {
  4753. if (unistimdebug) {
  4754. ast_verb(0, "Received a packet from an unknown source\n");
  4755. }
  4756. parsing(dw_num_bytes_rcvd, buff, NULL, (struct sockaddr_in *) &addr_from);
  4757. } else {
  4758. parsing(dw_num_bytes_rcvd, buff, cur, (struct sockaddr_in *) &addr_from);
  4759. }
  4760. return 1;
  4761. }
  4762. static struct ast_frame *unistim_rtp_read(const struct ast_channel *ast,
  4763. const struct unistim_subchannel *sub)
  4764. {
  4765. /* Retrieve audio/etc from channel. Assumes sub->lock is already held. */
  4766. struct ast_frame *f;
  4767. if (!ast) {
  4768. ast_log(LOG_WARNING, "Channel NULL while reading\n");
  4769. return &ast_null_frame;
  4770. }
  4771. if (!sub->rtp) {
  4772. ast_log(LOG_WARNING, "RTP handle NULL while reading on subchannel %u\n",
  4773. sub->subtype);
  4774. return &ast_null_frame;
  4775. }
  4776. switch (ast_channel_fdno(ast)) {
  4777. case 0:
  4778. f = ast_rtp_instance_read(sub->rtp, 0); /* RTP Audio */
  4779. break;
  4780. case 1:
  4781. f = ast_rtp_instance_read(sub->rtp, 1); /* RTCP Control Channel */
  4782. break;
  4783. default:
  4784. f = &ast_null_frame;
  4785. }
  4786. if (sub->owner) {
  4787. /* We already hold the channel lock */
  4788. if (f->frametype == AST_FRAME_VOICE) {
  4789. if (ast_format_cap_iscompatible_format(ast_channel_nativeformats(sub->owner), f->subclass.format) == AST_FORMAT_CMP_NOT_EQUAL) {
  4790. struct ast_str *cap_buf = ast_str_alloca(AST_FORMAT_CAP_NAMES_LEN);
  4791. struct ast_format_cap *caps;
  4792. ast_debug(1,
  4793. "Oooh, format changed from %s to %s\n",
  4794. ast_format_cap_get_names(ast_channel_nativeformats(sub->owner), &cap_buf),
  4795. ast_format_get_name(f->subclass.format));
  4796. caps = ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_DEFAULT);
  4797. if (caps) {
  4798. ast_format_cap_append(caps, f->subclass.format, 0);
  4799. ast_channel_nativeformats_set(sub->owner, caps);
  4800. ao2_ref(caps, -1);
  4801. }
  4802. ast_set_read_format(sub->owner, ast_channel_readformat(sub->owner));
  4803. ast_set_write_format(sub->owner, ast_channel_writeformat(sub->owner));
  4804. }
  4805. }
  4806. }
  4807. return f;
  4808. }
  4809. static struct ast_frame *unistim_read(struct ast_channel *ast)
  4810. {
  4811. struct ast_frame *fr;
  4812. struct unistim_subchannel *sub = ast_channel_tech_pvt(ast);
  4813. ast_mutex_lock(&sub->lock);
  4814. fr = unistim_rtp_read(ast, sub);
  4815. ast_mutex_unlock(&sub->lock);
  4816. return fr;
  4817. }
  4818. static int unistim_write(struct ast_channel *ast, struct ast_frame *frame)
  4819. {
  4820. struct unistim_subchannel *sub = ast_channel_tech_pvt(ast);
  4821. int res = 0;
  4822. if (frame->frametype != AST_FRAME_VOICE) {
  4823. if (frame->frametype == AST_FRAME_IMAGE) {
  4824. return 0;
  4825. } else {
  4826. ast_log(LOG_WARNING, "Can't send %u type frames with unistim_write\n",
  4827. frame->frametype);
  4828. return 0;
  4829. }
  4830. } else {
  4831. if (ast_format_cap_iscompatible_format(ast_channel_nativeformats(ast), frame->subclass.format) == AST_FORMAT_CMP_NOT_EQUAL) {
  4832. struct ast_str *cap_buf = ast_str_alloca(AST_FORMAT_CAP_NAMES_LEN);
  4833. ast_log(LOG_WARNING,
  4834. "Asked to transmit frame type %s, while native formats is %s (read/write = (%s/%s)\n",
  4835. ast_format_get_name(frame->subclass.format),
  4836. ast_format_cap_get_names(ast_channel_nativeformats(ast), &cap_buf),
  4837. ast_format_get_name(ast_channel_readformat(ast)),
  4838. ast_format_get_name(ast_channel_writeformat(ast)));
  4839. return -1;
  4840. }
  4841. }
  4842. if (sub) {
  4843. ast_mutex_lock(&sub->lock);
  4844. if (sub->rtp) {
  4845. res = ast_rtp_instance_write(sub->rtp, frame);
  4846. }
  4847. ast_mutex_unlock(&sub->lock);
  4848. }
  4849. return res;
  4850. }
  4851. static int unistim_fixup(struct ast_channel *oldchan, struct ast_channel *newchan)
  4852. {
  4853. struct unistim_subchannel *p = ast_channel_tech_pvt(newchan);
  4854. struct unistim_line *l = p->parent;
  4855. ast_mutex_lock(&p->lock);
  4856. ast_debug(1, "New owner for channel USTM/%s@%s-%u is %s\n", l->name,
  4857. l->parent->name, p->subtype, ast_channel_name(newchan));
  4858. if (p->owner != oldchan) {
  4859. ast_log(LOG_WARNING, "old channel wasn't %s (%p) but was %s (%p)\n",
  4860. ast_channel_name(oldchan), oldchan, ast_channel_name(p->owner), p->owner);
  4861. ast_mutex_unlock(&p->lock);
  4862. return -1;
  4863. }
  4864. unistim_set_owner(p, newchan);
  4865. ast_mutex_unlock(&p->lock);
  4866. return 0;
  4867. }
  4868. static char *control2str(int ind)
  4869. {
  4870. switch (ind) {
  4871. case AST_CONTROL_HANGUP:
  4872. return "Other end has hungup";
  4873. case AST_CONTROL_RING:
  4874. return "Local ring";
  4875. case AST_CONTROL_RINGING:
  4876. return "Remote end is ringing";
  4877. case AST_CONTROL_ANSWER:
  4878. return "Remote end has answered";
  4879. case AST_CONTROL_BUSY:
  4880. return "Remote end is busy";
  4881. case AST_CONTROL_TAKEOFFHOOK:
  4882. return "Make it go off hook";
  4883. case AST_CONTROL_OFFHOOK:
  4884. return "Line is off hook";
  4885. case AST_CONTROL_CONGESTION:
  4886. return "Congestion (circuits busy)";
  4887. case AST_CONTROL_FLASH:
  4888. return "Flash hook";
  4889. case AST_CONTROL_WINK:
  4890. return "Wink";
  4891. case AST_CONTROL_OPTION:
  4892. return "Set a low-level option";
  4893. case AST_CONTROL_RADIO_KEY:
  4894. return "Key Radio";
  4895. case AST_CONTROL_RADIO_UNKEY:
  4896. return "Un-Key Radio";
  4897. case AST_CONTROL_CONNECTED_LINE:
  4898. return "Remote end changed";
  4899. case AST_CONTROL_SRCCHANGE:
  4900. return "RTP source updated";
  4901. case AST_CONTROL_SRCUPDATE:
  4902. return "Source of media changed";
  4903. case -1:
  4904. return "Stop tone";
  4905. }
  4906. return "UNKNOWN";
  4907. }
  4908. static void in_band_indication(struct ast_channel *ast, const struct ast_tone_zone *tz,
  4909. const char *indication)
  4910. {
  4911. struct ast_tone_zone_sound *ts = NULL;
  4912. if ((ts = ast_get_indication_tone(tz, indication))) {
  4913. ast_playtones_start(ast, 0, ts->data, 1);
  4914. ts = ast_tone_zone_sound_unref(ts);
  4915. } else {
  4916. ast_log(LOG_WARNING, "Unable to get indication tone for %s\n", indication);
  4917. }
  4918. }
  4919. static int unistim_indicate(struct ast_channel *ast, int ind, const void *data, size_t datalen)
  4920. {
  4921. struct unistim_subchannel *sub;
  4922. struct unistim_line *l;
  4923. struct unistimsession *s;
  4924. if (unistimdebug) {
  4925. ast_verb(3, "Asked to indicate '%s' (%d) condition on channel %s\n",
  4926. control2str(ind), ind, ast_channel_name(ast));
  4927. }
  4928. s = channel_to_session(ast);
  4929. if (!s) {
  4930. return -1;
  4931. }
  4932. sub = ast_channel_tech_pvt(ast);
  4933. l = sub->parent;
  4934. switch (ind) {
  4935. case AST_CONTROL_RINGING:
  4936. if (ast_channel_state(ast) != AST_STATE_UP) {
  4937. send_text(TEXT_LINE2, TEXT_NORMAL, s, ustmtext("Ringing...", s));
  4938. in_band_indication(ast, l->parent->tz, "ring");
  4939. s->device->missed_call = -1;
  4940. break;
  4941. }
  4942. return -1;
  4943. case AST_CONTROL_BUSY:
  4944. if (ast_channel_state(ast) != AST_STATE_UP) {
  4945. sub->alreadygone = 1;
  4946. send_text(TEXT_LINE2, TEXT_NORMAL, s, ustmtext("Busy", s));
  4947. in_band_indication(ast, l->parent->tz, "busy");
  4948. s->device->missed_call = -1;
  4949. break;
  4950. }
  4951. return -1;
  4952. case AST_CONTROL_INCOMPLETE:
  4953. /* Overlapped dialing is not currently supported for UNIStim. Treat an indication
  4954. * of incomplete as congestion
  4955. */
  4956. case AST_CONTROL_CONGESTION:
  4957. if (ast_channel_state(ast) != AST_STATE_UP) {
  4958. sub->alreadygone = 1;
  4959. send_text(TEXT_LINE2, TEXT_NORMAL, s, ustmtext("Congestion", s));
  4960. in_band_indication(ast, l->parent->tz, "congestion");
  4961. s->device->missed_call = -1;
  4962. break;
  4963. }
  4964. return -1;
  4965. case AST_CONTROL_HOLD:
  4966. ast_moh_start(ast, data, NULL);
  4967. break;
  4968. case AST_CONTROL_UNHOLD:
  4969. ast_moh_stop(ast);
  4970. break;
  4971. case AST_CONTROL_PROGRESS:
  4972. case AST_CONTROL_SRCUPDATE:
  4973. case AST_CONTROL_PROCEEDING:
  4974. case AST_CONTROL_UPDATE_RTP_PEER:
  4975. break;
  4976. case -1:
  4977. ast_playtones_stop(ast);
  4978. s->device->missed_call = 0;
  4979. break;
  4980. case AST_CONTROL_CONNECTED_LINE:
  4981. ast_log(LOG_NOTICE, "Connected party is now %s <%s>\n",
  4982. S_COR(ast_channel_connected(ast)->id.name.valid, ast_channel_connected(ast)->id.name.str, ""),
  4983. S_COR(ast_channel_connected(ast)->id.number.valid, ast_channel_connected(ast)->id.number.str, ""));
  4984. if (sub->subtype == SUB_REAL) {
  4985. send_callerid_screen(s, sub);
  4986. }
  4987. break;
  4988. case AST_CONTROL_SRCCHANGE:
  4989. if (sub->rtp) {
  4990. ast_rtp_instance_change_source(sub->rtp);
  4991. }
  4992. break;
  4993. default:
  4994. ast_log(LOG_WARNING, "Don't know how to indicate condition %d\n", ind);
  4995. /* fallthrough */
  4996. case AST_CONTROL_PVT_CAUSE_CODE:
  4997. case AST_CONTROL_MASQUERADE_NOTIFY:
  4998. return -1;
  4999. }
  5000. return 0;
  5001. }
  5002. static struct unistim_subchannel *find_subchannel_by_name(const char *dest)
  5003. {
  5004. struct unistim_line *l;
  5005. struct unistim_device *d;
  5006. struct unistim_subchannel *sub = NULL;
  5007. char line[256];
  5008. char *at;
  5009. char *device;
  5010. ast_copy_string(line, dest, sizeof(line));
  5011. at = strchr(line, '@');
  5012. if (!at) {
  5013. ast_log(LOG_NOTICE, "Device '%s' has no @ (at) sign!\n", dest);
  5014. return NULL;
  5015. }
  5016. *at = '\0';
  5017. at++;
  5018. device = at;
  5019. ast_mutex_lock(&devicelock);
  5020. d = devices;
  5021. at = strchr(device, '/'); /* Extra options ? */
  5022. if (at) {
  5023. *at = '\0';
  5024. }
  5025. while (d) {
  5026. if (!strcasecmp(d->name, device)) {
  5027. if (unistimdebug) {
  5028. ast_verb(0, "Found device: %s\n", d->name);
  5029. }
  5030. /* Found the device */
  5031. AST_LIST_LOCK(&d->lines);
  5032. AST_LIST_TRAVERSE(&d->lines, l, list) {
  5033. /* Search for the right line */
  5034. if (!strcasecmp(l->name, line)) {
  5035. if (unistimdebug) {
  5036. ast_verb(0, "Found line: %s\n", l->name);
  5037. }
  5038. sub = get_sub(d, SUB_REAL);
  5039. if (!sub) {
  5040. sub = unistim_alloc_sub(d, SUB_REAL);
  5041. }
  5042. if (sub->owner) {
  5043. /* Allocate additional channel if asterisk channel already here */
  5044. sub = unistim_alloc_sub(d, SUB_REAL);
  5045. sub->holding = 1;
  5046. }
  5047. sub->ringvolume = -1;
  5048. sub->ringstyle = -1;
  5049. if (at) { /* Other options ? */
  5050. at++; /* Skip slash */
  5051. if (*at == 'r') { /* distinctive ring */
  5052. at++;
  5053. if ((*at < '0') || (*at > '7')) { /* ring style */
  5054. ast_log(LOG_WARNING, "Invalid ring selection (%s)", at);
  5055. } else {
  5056. signed char ring_volume = -1;
  5057. signed char ring_style = *at - '0';
  5058. at++;
  5059. if ((*at >= '0') && (*at <= '3')) { /* ring volume */
  5060. ring_volume = *at - '0';
  5061. }
  5062. if (unistimdebug) {
  5063. ast_verb(0, "Distinctive ring: style #%d volume %d\n",
  5064. ring_style, ring_volume);
  5065. }
  5066. sub->ringvolume = ring_volume;
  5067. sub->ringstyle = ring_style;
  5068. }
  5069. }
  5070. }
  5071. sub->parent = l;
  5072. break;
  5073. }
  5074. }
  5075. AST_LIST_UNLOCK(&d->lines);
  5076. if (sub) {
  5077. ast_mutex_unlock(&devicelock);
  5078. return sub;
  5079. }
  5080. }
  5081. d = d->next;
  5082. }
  5083. /* Device not found */
  5084. ast_mutex_unlock(&devicelock);
  5085. return NULL;
  5086. }
  5087. static int unistim_senddigit_begin(struct ast_channel *ast, char digit)
  5088. {
  5089. struct unistimsession *pte = channel_to_session(ast);
  5090. if (!pte) {
  5091. return -1;
  5092. }
  5093. return send_dtmf_tone(pte, digit);
  5094. }
  5095. static int unistim_senddigit_end(struct ast_channel *ast, char digit, unsigned int duration)
  5096. {
  5097. struct unistimsession *pte = channel_to_session(ast);
  5098. if (!pte) {
  5099. return -1;
  5100. }
  5101. if (unistimdebug) {
  5102. ast_verb(0, "Send Digit off %c (duration %d)\n", digit, duration);
  5103. }
  5104. send_tone(pte, 0, 0);
  5105. return 0;
  5106. }
  5107. /*--- unistim_sendtext: Display a text on the phone screen ---*/
  5108. /* Called from PBX core text message functions */
  5109. static int unistim_sendtext(struct ast_channel *ast, const char *text)
  5110. {
  5111. struct unistimsession *pte = channel_to_session(ast);
  5112. int size;
  5113. char tmp[TEXT_LENGTH_MAX + 1];
  5114. if (unistimdebug) {
  5115. ast_verb(0, "unistim_sendtext called\n");
  5116. }
  5117. if (!text) {
  5118. ast_log(LOG_WARNING, "unistim_sendtext called with a null text\n");
  5119. return -1;
  5120. }
  5121. if (!pte) {
  5122. return -1;
  5123. }
  5124. size = strlen(text);
  5125. if (text[0] == '@') {
  5126. int pos = 0, i = 1, tok = 0, sz = 0;
  5127. char label[11];
  5128. char number[16];
  5129. char icon = '\0';
  5130. char cur = '\0';
  5131. memset(label, 0, 11);
  5132. memset(number, 0, 16);
  5133. while (text[i]) {
  5134. cur = text[i++];
  5135. switch (tok) {
  5136. case 0:
  5137. if ((cur < '0') && (cur > '5')) {
  5138. ast_log(LOG_WARNING,
  5139. "sendtext failed : position must be a number beetween 0 and 5\n");
  5140. return 1;
  5141. }
  5142. pos = cur - '0';
  5143. tok = 1;
  5144. continue;
  5145. case 1:
  5146. if (cur != '@') {
  5147. ast_log(LOG_WARNING, "sendtext failed : invalid position\n");
  5148. return 1;
  5149. }
  5150. tok = 2;
  5151. continue;
  5152. case 2:
  5153. if ((cur < '3') && (cur > '6')) {
  5154. ast_log(LOG_WARNING,
  5155. "sendtext failed : icon must be a number beetween 32 and 63 (first digit invalid)\n");
  5156. return 1;
  5157. }
  5158. icon = (cur - '0') * 10;
  5159. tok = 3;
  5160. continue;
  5161. case 3:
  5162. if ((cur < '0') && (cur > '9')) {
  5163. ast_log(LOG_WARNING,
  5164. "sendtext failed : icon must be a number beetween 32 and 63 (second digit invalid)\n");
  5165. return 1;
  5166. }
  5167. icon += (cur - '0');
  5168. tok = 4;
  5169. continue;
  5170. case 4:
  5171. if (cur != '@') {
  5172. ast_log(LOG_WARNING,
  5173. "sendtext failed : icon must be a number beetween 32 and 63 (too many digits)\n");
  5174. return 1;
  5175. }
  5176. tok = 5;
  5177. continue;
  5178. case 5:
  5179. if (cur == '@') {
  5180. tok = 6;
  5181. sz = 0;
  5182. continue;
  5183. }
  5184. if (sz > 10) {
  5185. continue;
  5186. }
  5187. label[sz] = cur;
  5188. sz++;
  5189. continue;
  5190. case 6:
  5191. if (sz > 15) {
  5192. ast_log(LOG_WARNING,
  5193. "sendtext failed : extension too long = %d (15 car max)\n",
  5194. sz);
  5195. return 1;
  5196. }
  5197. number[sz] = cur;
  5198. sz++;
  5199. continue;
  5200. }
  5201. }
  5202. if (tok != 6) {
  5203. ast_log(LOG_WARNING, "sendtext failed : incomplet command\n");
  5204. return 1;
  5205. }
  5206. if (!pte->device) {
  5207. ast_log(LOG_WARNING, "sendtext failed : no device ?\n");
  5208. return 1;
  5209. }
  5210. strcpy(pte->device->softkeylabel[pos], label);
  5211. strcpy(pte->device->softkeynumber[pos], number);
  5212. pte->device->softkeyicon[pos] = icon;
  5213. send_favorite(pos, icon, pte, label);
  5214. return 0;
  5215. }
  5216. if (size <= TEXT_LENGTH_MAX * 2) {
  5217. if (pte->device->height == 1) {
  5218. send_text(TEXT_LINE0, TEXT_NORMAL, pte, text);
  5219. } else {
  5220. send_text(TEXT_LINE0, TEXT_NORMAL, pte, ustmtext("Message :", pte));
  5221. send_text(TEXT_LINE1, TEXT_NORMAL, pte, text);
  5222. }
  5223. if (size <= TEXT_LENGTH_MAX) {
  5224. send_text(TEXT_LINE2, TEXT_NORMAL, pte, "");
  5225. return 0;
  5226. }
  5227. memcpy(tmp, text + TEXT_LENGTH_MAX, TEXT_LENGTH_MAX);
  5228. tmp[sizeof(tmp) - 1] = '\0';
  5229. send_text(TEXT_LINE2, TEXT_NORMAL, pte, tmp);
  5230. return 0;
  5231. }
  5232. send_text(TEXT_LINE0, TEXT_NORMAL, pte, text);
  5233. memcpy(tmp, text + TEXT_LENGTH_MAX, TEXT_LENGTH_MAX);
  5234. tmp[sizeof(tmp) - 1] = '\0';
  5235. send_text(TEXT_LINE1, TEXT_NORMAL, pte, tmp);
  5236. memcpy(tmp, text + TEXT_LENGTH_MAX * 2, TEXT_LENGTH_MAX);
  5237. tmp[sizeof(tmp) - 1] = '\0';
  5238. send_text(TEXT_LINE2, TEXT_NORMAL, pte, tmp);
  5239. return 0;
  5240. }
  5241. /*--- unistim_send_mwi_to_peer: Send message waiting indication ---*/
  5242. static int unistim_send_mwi_to_peer(struct unistim_line *peer, unsigned int tick)
  5243. {
  5244. int new;
  5245. RAII_VAR(struct stasis_message *, msg, NULL, ao2_cleanup);
  5246. msg = stasis_cache_get(ast_mwi_state_cache(), ast_mwi_state_type(), peer->mailbox);
  5247. if (msg) {
  5248. struct ast_mwi_state *mwi_state = stasis_message_data(msg);
  5249. new = mwi_state->new_msgs;
  5250. } else { /* Fall back on checking the mailbox directly */
  5251. new = ast_app_has_voicemail(peer->mailbox, NULL);
  5252. }
  5253. ast_debug(3, "MWI Status for mailbox %s is %d, lastmsgsent:%d\n",
  5254. peer->mailbox, new, peer->parent->lastmsgssent);
  5255. peer->parent->nextmsgcheck = tick + TIMER_MWI;
  5256. /* Return now if it's the same thing we told them last time */
  5257. if ((peer->parent->session->state != STATE_MAINPAGE) || (new == peer->parent->lastmsgssent)) {
  5258. return 0;
  5259. }
  5260. peer->parent->lastmsgssent = new;
  5261. send_led_update(peer->parent->session, (new > 0)?LED_BAR_ON:LED_BAR_OFF);
  5262. return 0;
  5263. }
  5264. /*--- unistim_new: Initiate a call in the UNISTIM channel */
  5265. /* called from unistim_request (calls from the pbx ) */
  5266. static struct ast_channel *unistim_new(struct unistim_subchannel *sub, int state, const struct ast_assigned_ids *assignedids, const struct ast_channel *requestor)
  5267. {
  5268. struct ast_format_cap *caps;
  5269. struct ast_channel *tmp;
  5270. struct unistim_line *l;
  5271. struct ast_format *tmpfmt;
  5272. if (!sub) {
  5273. ast_log(LOG_WARNING, "subchannel null in unistim_new\n");
  5274. return NULL;
  5275. }
  5276. if (!sub->parent) {
  5277. ast_log(LOG_WARNING, "no line for subchannel %p\n", sub);
  5278. return NULL;
  5279. }
  5280. caps = ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_DEFAULT);
  5281. if (!caps) {
  5282. return NULL;
  5283. }
  5284. l = sub->parent;
  5285. tmp = ast_channel_alloc(1, state, l->cid_num, NULL, l->accountcode, l->exten,
  5286. l->parent->context, assignedids, requestor, l->amaflags, "USTM/%s@%s-%p", l->name, l->parent->name, sub);
  5287. if (unistimdebug) {
  5288. ast_verb(0, "unistim_new sub=%u (%p) chan=%p line=%s\n", sub->subtype, sub, tmp, l->name);
  5289. }
  5290. if (!tmp) {
  5291. ast_log(LOG_WARNING, "Unable to allocate channel structure\n");
  5292. ao2_ref(caps, -1);
  5293. return NULL;
  5294. }
  5295. ast_channel_stage_snapshot(tmp);
  5296. if (ast_format_cap_count(l->cap)) {
  5297. ast_format_cap_append_from_cap(caps, l->cap, AST_MEDIA_TYPE_UNKNOWN);
  5298. } else {
  5299. ast_format_cap_append_from_cap(caps, global_cap, AST_MEDIA_TYPE_UNKNOWN);
  5300. }
  5301. ast_channel_nativeformats_set(tmp, caps);
  5302. ao2_ref(caps, -1);
  5303. tmpfmt = ast_format_cap_get_format(ast_channel_nativeformats(tmp), 0);
  5304. if (unistimdebug) {
  5305. struct ast_str *native_buf = ast_str_alloca(AST_FORMAT_CAP_NAMES_LEN);
  5306. struct ast_str *cap_buf = ast_str_alloca(AST_FORMAT_CAP_NAMES_LEN);
  5307. struct ast_str *global_buf = ast_str_alloca(AST_FORMAT_CAP_NAMES_LEN);
  5308. ast_verb(0, "Best codec = %s from nativeformats %s (line cap=%s global=%s)\n",
  5309. ast_format_get_name(tmpfmt),
  5310. ast_format_cap_get_names(ast_channel_nativeformats(tmp), &native_buf),
  5311. ast_format_cap_get_names(l->cap, &cap_buf),
  5312. ast_format_cap_get_names(global_cap, &global_buf));
  5313. }
  5314. if ((sub->rtp) && (sub->subtype == 0)) {
  5315. if (unistimdebug) {
  5316. ast_verb(0, "New unistim channel with a previous rtp handle ?\n");
  5317. }
  5318. ast_channel_internal_fd_set(tmp, 0, ast_rtp_instance_fd(sub->rtp, 0));
  5319. ast_channel_internal_fd_set(tmp, 1, ast_rtp_instance_fd(sub->rtp, 1));
  5320. }
  5321. if (sub->rtp) {
  5322. ast_jb_configure(tmp, &global_jbconf);
  5323. }
  5324. /* tmp->type = type; */
  5325. ast_setstate(tmp, state);
  5326. if (state == AST_STATE_RING) {
  5327. ast_channel_rings_set(tmp, 1);
  5328. }
  5329. ast_channel_adsicpe_set(tmp, AST_ADSI_UNAVAILABLE);
  5330. ast_channel_set_writeformat(tmp, tmpfmt);
  5331. ast_channel_set_rawwriteformat(tmp, tmpfmt);
  5332. ast_channel_set_readformat(tmp, tmpfmt);
  5333. ast_channel_set_rawreadformat(tmp, tmpfmt);
  5334. ao2_ref(tmpfmt, -1);
  5335. ast_channel_tech_pvt_set(tmp, sub);
  5336. ast_channel_tech_set(tmp, &unistim_tech);
  5337. if (!ast_strlen_zero(l->parent->language)) {
  5338. ast_channel_language_set(tmp, l->parent->language);
  5339. }
  5340. unistim_set_owner(sub, tmp);
  5341. ast_update_use_count();
  5342. ast_channel_callgroup_set(tmp, l->callgroup);
  5343. ast_channel_pickupgroup_set(tmp, l->pickupgroup);
  5344. ast_channel_call_forward_set(tmp, l->parent->call_forward);
  5345. if (!ast_strlen_zero(l->cid_num)) {
  5346. char *name, *loc, *instr;
  5347. instr = ast_strdup(l->cid_num);
  5348. if (instr) {
  5349. ast_callerid_parse(instr, &name, &loc);
  5350. ast_channel_caller(tmp)->id.number.valid = 1;
  5351. ast_free(ast_channel_caller(tmp)->id.number.str);
  5352. ast_channel_caller(tmp)->id.number.str = ast_strdup(loc);
  5353. ast_channel_caller(tmp)->id.name.valid = 1;
  5354. ast_free(ast_channel_caller(tmp)->id.name.str);
  5355. ast_channel_caller(tmp)->id.name.str = ast_strdup(name);
  5356. ast_free(instr);
  5357. }
  5358. }
  5359. ast_channel_priority_set(tmp, 1);
  5360. ast_channel_stage_snapshot_done(tmp);
  5361. ast_channel_unlock(tmp);
  5362. if (state != AST_STATE_DOWN) {
  5363. if (unistimdebug) {
  5364. ast_verb(0, "Starting pbx in unistim_new\n");
  5365. }
  5366. if (ast_pbx_start(tmp)) {
  5367. ast_log(LOG_WARNING, "Unable to start PBX on %s\n", ast_channel_name(tmp));
  5368. ast_hangup(tmp);
  5369. tmp = NULL;
  5370. }
  5371. }
  5372. return tmp;
  5373. }
  5374. static void unistim_set_owner(struct unistim_subchannel *sub, struct ast_channel *chan)
  5375. {
  5376. sub->owner = chan;
  5377. if (sub->rtp) {
  5378. ast_rtp_instance_set_channel_id(sub->rtp, sub->owner ? ast_channel_uniqueid(sub->owner) : "");
  5379. }
  5380. }
  5381. static void *do_monitor(void *data)
  5382. {
  5383. struct unistimsession *cur = NULL;
  5384. unsigned int dw_timeout = 0;
  5385. unsigned int tick;
  5386. int res;
  5387. int reloading;
  5388. /* Add an I/O event to our UDP socket */
  5389. if (unistimsock > -1) {
  5390. ast_io_add(io, unistimsock, unistimsock_read, AST_IO_IN, NULL);
  5391. }
  5392. /* This thread monitors our UDP socket and timers */
  5393. for (;;) {
  5394. /* This loop is executed at least every IDLE_WAITus (1s) or every time a packet is received */
  5395. /* Looking for the smallest time-out value */
  5396. tick = get_tick_count();
  5397. dw_timeout = UINT_MAX;
  5398. ast_mutex_lock(&sessionlock);
  5399. cur = sessions;
  5400. DEBUG_TIMER("checking timeout for session %p with tick = %u\n", cur, tick);
  5401. while (cur) {
  5402. DEBUG_TIMER("checking timeout for session %p timeout = %u\n", cur,
  5403. cur->timeout);
  5404. /* Check if we have miss something */
  5405. if (cur->timeout <= tick) {
  5406. DEBUG_TIMER("Event for session %p\n", cur);
  5407. /* If the queue is empty, send a ping */
  5408. if (cur->last_buf_available == 0) {
  5409. send_ping(cur);
  5410. } else {
  5411. if (send_retransmit(cur)) {
  5412. DEBUG_TIMER("The chained link was modified, restarting...\n");
  5413. cur = sessions;
  5414. dw_timeout = UINT_MAX;
  5415. continue;
  5416. }
  5417. }
  5418. }
  5419. if (dw_timeout > cur->timeout - tick) {
  5420. dw_timeout = cur->timeout - tick;
  5421. }
  5422. /* Checking if the phone is logged on for a new MWI */
  5423. if (cur->device) {
  5424. struct unistim_line *l;
  5425. AST_LIST_LOCK(&cur->device->lines);
  5426. AST_LIST_TRAVERSE(&cur->device->lines, l, list) {
  5427. if ((!ast_strlen_zero(l->mailbox)) && (tick >= l->parent->nextmsgcheck)) {
  5428. DEBUG_TIMER("Checking mailbox for MWI\n");
  5429. unistim_send_mwi_to_peer(l, tick);
  5430. break;
  5431. }
  5432. }
  5433. AST_LIST_UNLOCK(&cur->device->lines);
  5434. if (cur->device->nextdial && tick >= cur->device->nextdial) {
  5435. handle_call_outgoing(cur);
  5436. cur->device->nextdial = 0;
  5437. }
  5438. }
  5439. cur = cur->next;
  5440. }
  5441. ast_mutex_unlock(&sessionlock);
  5442. DEBUG_TIMER("Waiting for %dus\n", dw_timeout);
  5443. res = dw_timeout;
  5444. /* We should not wait more than IDLE_WAIT */
  5445. if ((res < 0) || (res > IDLE_WAIT)) {
  5446. res = IDLE_WAIT;
  5447. }
  5448. /* Wait for UDP messages for a maximum of res us */
  5449. res = ast_io_wait(io, res); /* This function will call unistimsock_read if a packet is received */
  5450. /* Check for a reload request */
  5451. ast_mutex_lock(&unistim_reload_lock);
  5452. reloading = unistim_reloading;
  5453. unistim_reloading = 0;
  5454. ast_mutex_unlock(&unistim_reload_lock);
  5455. if (reloading) {
  5456. ast_verb(1, "Reloading unistim.conf...\n");
  5457. reload_config();
  5458. }
  5459. pthread_testcancel();
  5460. }
  5461. /* Never reached */
  5462. return NULL;
  5463. }
  5464. /*--- restart_monitor: Start the channel monitor thread ---*/
  5465. static int restart_monitor(void)
  5466. {
  5467. pthread_attr_t attr;
  5468. /* If we're supposed to be stopped -- stay stopped */
  5469. if (monitor_thread == AST_PTHREADT_STOP) {
  5470. return 0;
  5471. }
  5472. if (ast_mutex_lock(&monlock)) {
  5473. ast_log(LOG_WARNING, "Unable to lock monitor\n");
  5474. return -1;
  5475. }
  5476. if (monitor_thread == pthread_self()) {
  5477. ast_mutex_unlock(&monlock);
  5478. ast_log(LOG_WARNING, "Cannot kill myself\n");
  5479. return -1;
  5480. }
  5481. if (monitor_thread != AST_PTHREADT_NULL) {
  5482. /* Wake up the thread */
  5483. pthread_kill(monitor_thread, SIGURG);
  5484. } else {
  5485. pthread_attr_init(&attr);
  5486. pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);
  5487. /* Start a new monitor */
  5488. if (ast_pthread_create(&monitor_thread, &attr, do_monitor, NULL) < 0) {
  5489. ast_mutex_unlock(&monlock);
  5490. ast_log(LOG_ERROR, "Unable to start monitor thread.\n");
  5491. return -1;
  5492. }
  5493. }
  5494. ast_mutex_unlock(&monlock);
  5495. return 0;
  5496. }
  5497. /*--- unistim_request: PBX interface function ---*/
  5498. /* UNISTIM calls initiated by the PBX arrive here */
  5499. static struct ast_channel *unistim_request(const char *type, struct ast_format_cap *cap, const struct ast_assigned_ids *assignedids, const struct ast_channel *requestor, const char *dest,
  5500. int *cause)
  5501. {
  5502. struct unistim_subchannel *sub, *sub_ring, *sub_trans;
  5503. struct unistim_device *d;
  5504. struct ast_channel *tmpc = NULL;
  5505. char tmp[256];
  5506. if (!(ast_format_cap_iscompatible(cap, global_cap))) {
  5507. struct ast_str *cap_buf = ast_str_alloca(AST_FORMAT_CAP_NAMES_LEN);
  5508. struct ast_str *global_buf = ast_str_alloca(AST_FORMAT_CAP_NAMES_LEN);
  5509. ast_log(LOG_NOTICE,
  5510. "Asked to get a channel of unsupported format %s while capability is %s\n",
  5511. ast_format_cap_get_names(cap, &cap_buf),
  5512. ast_format_cap_get_names(global_cap, &global_buf));
  5513. return NULL;
  5514. }
  5515. ast_copy_string(tmp, dest, sizeof(tmp));
  5516. if (ast_strlen_zero(tmp)) {
  5517. ast_log(LOG_NOTICE, "Unistim channels require a device\n");
  5518. return NULL;
  5519. }
  5520. sub = find_subchannel_by_name(tmp);
  5521. if (!sub) {
  5522. ast_log(LOG_NOTICE, "No available lines on: %s\n", dest);
  5523. *cause = AST_CAUSE_CONGESTION;
  5524. return NULL;
  5525. }
  5526. d = sub->parent->parent;
  5527. sub_ring = get_sub(d, SUB_RING);
  5528. sub_trans = get_sub(d, SUB_THREEWAY);
  5529. /* Another request already in progress */
  5530. if (!d->session) {
  5531. unistim_unalloc_sub(d, sub);
  5532. *cause = AST_CAUSE_CONGESTION;
  5533. return NULL;
  5534. }
  5535. if (sub_ring || sub_trans) {
  5536. if (unistimdebug) {
  5537. ast_verb(0, "Can't create channel, request already in progress: Busy!\n");
  5538. }
  5539. unistim_unalloc_sub(d, sub);
  5540. *cause = AST_CAUSE_BUSY;
  5541. return NULL;
  5542. }
  5543. if (d->session->state == STATE_DIALPAGE) {
  5544. if (unistimdebug) {
  5545. ast_verb(0, "Can't create channel, user on dialpage: Busy!\n");
  5546. }
  5547. unistim_unalloc_sub(d, sub);
  5548. *cause = AST_CAUSE_BUSY;
  5549. return NULL;
  5550. }
  5551. if (get_avail_softkey(d->session, sub->parent->name) == -1) {
  5552. if (unistimdebug) {
  5553. ast_verb(0, "Can't create channel for line %s, all lines busy\n", sub->parent->name);
  5554. }
  5555. unistim_unalloc_sub(d, sub);
  5556. *cause = AST_CAUSE_BUSY;
  5557. return NULL;
  5558. }
  5559. sub->subtype = SUB_RING;
  5560. sub->softkey = -1;
  5561. ast_format_cap_append_from_cap(sub->parent->cap, cap, AST_MEDIA_TYPE_UNKNOWN);
  5562. tmpc = unistim_new(sub, AST_STATE_DOWN, assignedids, requestor);
  5563. if (!tmpc) {
  5564. ast_log(LOG_WARNING, "Unable to make channel for '%s'\n", tmp);
  5565. }
  5566. if (unistimdebug) {
  5567. ast_verb(0, "unistim_request owner = %p\n", sub->owner);
  5568. }
  5569. restart_monitor();
  5570. /* and finish */
  5571. return tmpc;
  5572. }
  5573. static char *unistim_show_info(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
  5574. {
  5575. struct unistim_device *device = devices;
  5576. struct unistim_line *line;
  5577. struct unistim_subchannel *sub;
  5578. struct unistimsession *s;
  5579. struct ast_str *cap_buf = ast_str_alloca(AST_FORMAT_CAP_NAMES_LEN);
  5580. switch (cmd) {
  5581. case CLI_INIT:
  5582. e->command = "unistim show info";
  5583. e->usage =
  5584. "Usage: unistim show info\n"
  5585. " Dump internal structures.\n\n"
  5586. " device\n"
  5587. " ->line\n"
  5588. " -->sub\n"
  5589. " ==>key\n";
  5590. return NULL;
  5591. case CLI_GENERATE:
  5592. return NULL; /* no completion */
  5593. }
  5594. if (a->argc != e->args) {
  5595. return CLI_SHOWUSAGE;
  5596. }
  5597. ast_cli(a->fd, "Dumping internal structures:\n");
  5598. ast_mutex_lock(&devicelock);
  5599. while (device) {
  5600. int i;
  5601. ast_cli(a->fd, "\nname=%s id=%s ha=%p sess=%p device=%p selected=%d height=%d\n",
  5602. device->name, device->id, device->ha, device->session,
  5603. device, device->selected, device->height);
  5604. AST_LIST_LOCK(&device->lines);
  5605. AST_LIST_TRAVERSE(&device->lines,line,list) {
  5606. ast_cli(a->fd,
  5607. "->name=%s fullname=%s exten=%s callid=%s cap=%s line=%p\n",
  5608. line->name, line->fullname, line->exten, line->cid_num,
  5609. ast_format_cap_get_names(line->cap, &cap_buf), line);
  5610. }
  5611. AST_LIST_UNLOCK(&device->lines);
  5612. AST_LIST_LOCK(&device->subs);
  5613. AST_LIST_TRAVERSE(&device->subs, sub, list) {
  5614. if (!sub) {
  5615. continue;
  5616. }
  5617. ast_cli(a->fd,
  5618. "-->subtype=%s chan=%p rtp=%p line=%p alreadygone=%d softkey=%d\n",
  5619. subtype_tostr(sub->subtype), sub->owner, sub->rtp, sub->parent,
  5620. sub->alreadygone, sub->softkey);
  5621. }
  5622. AST_LIST_UNLOCK(&device->subs);
  5623. for (i = 0; i < FAVNUM; i++) {
  5624. if (!soft_key_visible(device, i)) {
  5625. continue;
  5626. }
  5627. ast_cli(a->fd, "==> %d. dev=%s icon=%#-4x label=%-10s number=%-5s sub=%p line=%p\n",
  5628. i, device->softkeydevice[i], (unsigned)device->softkeyicon[i], device->softkeylabel[i], device->softkeynumber[i],
  5629. device->ssub[i], device->sline[i]);
  5630. }
  5631. device = device->next;
  5632. }
  5633. ast_mutex_unlock(&devicelock);
  5634. ast_cli(a->fd, "\nSessions:\n");
  5635. ast_mutex_lock(&sessionlock);
  5636. s = sessions;
  5637. while (s) {
  5638. ast_cli(a->fd,
  5639. "sin=%s timeout=%d state=%s macaddr=%s device=%p session=%p\n",
  5640. ast_inet_ntoa(s->sin.sin_addr), s->timeout, ptestate_tostr(s->state), s->macaddr,
  5641. s->device, s);
  5642. s = s->next;
  5643. }
  5644. ast_mutex_unlock(&sessionlock);
  5645. return CLI_SUCCESS;
  5646. }
  5647. static char *unistim_show_devices(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
  5648. {
  5649. struct unistim_device *device = devices;
  5650. switch (cmd) {
  5651. case CLI_INIT:
  5652. e->command = "unistim show devices";
  5653. e->usage =
  5654. "Usage: unistim show devices\n"
  5655. " Lists all known Unistim devices.\n";
  5656. return NULL;
  5657. case CLI_GENERATE:
  5658. return NULL; /* no completion */
  5659. }
  5660. if (a->argc != e->args)
  5661. return CLI_SHOWUSAGE;
  5662. ast_cli(a->fd, "%-20.20s %-20.20s %-15.15s %-15.15s %s\n", "Name/username", "MAC", "Host", "Firmware", "Status");
  5663. ast_mutex_lock(&devicelock);
  5664. while (device) {
  5665. ast_cli(a->fd, "%-20.20s %-20.20s %-15.15s %-15.15s %s\n",
  5666. device->name, device->id,
  5667. (!device->session) ? "(Unspecified)" : ast_inet_ntoa(device->session->sin.sin_addr),
  5668. (!device->session) ? "(Unspecified)" : device->session->firmware,
  5669. (!device->session) ? "UNKNOWN" : "OK");
  5670. device = device->next;
  5671. }
  5672. ast_mutex_unlock(&devicelock);
  5673. return CLI_SUCCESS;
  5674. }
  5675. static char *unistim_sp(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
  5676. {
  5677. BUFFSEND;
  5678. struct unistim_subchannel *sub;
  5679. int i, j = 0, len;
  5680. unsigned char c, cc;
  5681. char tmp[256];
  5682. switch (cmd) {
  5683. case CLI_INIT:
  5684. e->command = "unistim send packet";
  5685. e->usage =
  5686. "Usage: unistim send packet USTM/line@name hexa\n"
  5687. " unistim send packet USTM/1000@hans 19040004\n";
  5688. return NULL;
  5689. case CLI_GENERATE:
  5690. return NULL; /* no completion */
  5691. }
  5692. if (a->argc < 5) {
  5693. return CLI_SHOWUSAGE;
  5694. }
  5695. if (strlen(a->argv[3]) < 9) {
  5696. return CLI_SHOWUSAGE;
  5697. }
  5698. len = strlen(a->argv[4]);
  5699. if (len % 2) {
  5700. return CLI_SHOWUSAGE;
  5701. }
  5702. ast_copy_string(tmp, a->argv[3] + 5, sizeof(tmp));
  5703. sub = find_subchannel_by_name(tmp);
  5704. if (!sub) {
  5705. ast_cli(a->fd, "Can't find '%s'\n", tmp);
  5706. return CLI_SUCCESS;
  5707. }
  5708. if (!sub->parent->parent->session) {
  5709. ast_cli(a->fd, "'%s' is not connected\n", tmp);
  5710. return CLI_SUCCESS;
  5711. }
  5712. ast_cli(a->fd, "Sending '%s' to %s (%p)\n", a->argv[4], tmp, sub->parent->parent->session);
  5713. for (i = 0; i < len; i++) {
  5714. c = a->argv[4][i];
  5715. if (c >= 'a') {
  5716. c -= 'a' - 10;
  5717. } else {
  5718. c -= '0';
  5719. }
  5720. i++;
  5721. cc = a->argv[4][i];
  5722. if (cc >= 'a') {
  5723. cc -= 'a' - 10;
  5724. } else {
  5725. cc -= '0';
  5726. }
  5727. tmp[j++] = (c << 4) | cc;
  5728. }
  5729. memcpy(buffsend + SIZE_HEADER, tmp, j);
  5730. send_client(SIZE_HEADER + j, buffsend, sub->parent->parent->session);
  5731. return CLI_SUCCESS;
  5732. }
  5733. static char *unistim_do_debug(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
  5734. {
  5735. switch (cmd) {
  5736. case CLI_INIT:
  5737. e->command = "unistim set debug {on|off}";
  5738. e->usage =
  5739. "Usage: unistim set debug\n"
  5740. " Display debug messages.\n";
  5741. return NULL;
  5742. case CLI_GENERATE:
  5743. return NULL; /* no completion */
  5744. }
  5745. if (a->argc != e->args) {
  5746. return CLI_SHOWUSAGE;
  5747. }
  5748. if (!strcasecmp(a->argv[3], "on")) {
  5749. unistimdebug = 1;
  5750. ast_cli(a->fd, "UNISTIM Debugging Enabled\n");
  5751. } else if (!strcasecmp(a->argv[3], "off")) {
  5752. unistimdebug = 0;
  5753. ast_cli(a->fd, "UNISTIM Debugging Disabled\n");
  5754. } else {
  5755. return CLI_SHOWUSAGE;
  5756. }
  5757. return CLI_SUCCESS;
  5758. }
  5759. /*! \brief --- unistim_reload: Force reload of module from cli ---
  5760. * Runs in the asterisk main thread, so don't do anything useful
  5761. * but setting a flag and waiting for do_monitor to do the job
  5762. * in our thread */
  5763. static char *unistim_reload(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
  5764. {
  5765. switch (cmd) {
  5766. case CLI_INIT:
  5767. e->command = "unistim reload";
  5768. e->usage =
  5769. "Usage: unistim reload\n"
  5770. " Reloads UNISTIM configuration from unistim.conf\n";
  5771. return NULL;
  5772. case CLI_GENERATE:
  5773. return NULL; /* no completion */
  5774. }
  5775. if (e && a && a->argc != e->args) {
  5776. return CLI_SHOWUSAGE;
  5777. }
  5778. reload();
  5779. return CLI_SUCCESS;
  5780. }
  5781. static struct ast_cli_entry unistim_cli[] = {
  5782. AST_CLI_DEFINE(unistim_reload, "Reload UNISTIM configuration"),
  5783. AST_CLI_DEFINE(unistim_show_info, "Show UNISTIM info"),
  5784. AST_CLI_DEFINE(unistim_show_devices, "Show UNISTIM devices"),
  5785. AST_CLI_DEFINE(unistim_sp, "Send packet (for reverse engineering)"),
  5786. AST_CLI_DEFINE(unistim_do_debug, "Toggle UNITSTIM debugging"),
  5787. };
  5788. static void unquote(char *out, const char *src, int maxlen)
  5789. {
  5790. int len = strlen(src);
  5791. if (!len) {
  5792. return;
  5793. }
  5794. if ((len > 1) && src[0] == '\"') {
  5795. /* This is a quoted string */
  5796. src++;
  5797. /* Don't take more than what's there */
  5798. len--;
  5799. if (maxlen > len - 1) {
  5800. maxlen = len - 1;
  5801. }
  5802. memcpy(out, src, maxlen);
  5803. ((char *) out)[maxlen] = '\0';
  5804. } else {
  5805. memcpy(out, src, maxlen);
  5806. }
  5807. return;
  5808. }
  5809. static int parse_bookmark(const char *text, struct unistim_device *d)
  5810. {
  5811. char line[256];
  5812. char *at;
  5813. char *number;
  5814. char *icon;
  5815. int p;
  5816. int len = strlen(text);
  5817. ast_copy_string(line, text, sizeof(line));
  5818. /* Position specified ? */
  5819. if ((len > 2) && (line[1] == '@')) {
  5820. p = line[0];
  5821. if ((p >= '0') && (p <= '5')) {
  5822. p -= '0';
  5823. } else {
  5824. ast_log(LOG_WARNING,
  5825. "Invalid position for bookmark : must be between 0 and 5\n");
  5826. return 0;
  5827. }
  5828. if (d->softkeyicon[p] != 0) {
  5829. ast_log(LOG_WARNING, "Invalid position %d for bookmark : already used:\n", p);
  5830. return 0;
  5831. }
  5832. memmove(line, line + 2, sizeof(line) - 2);
  5833. } else {
  5834. /* No position specified, looking for a free slot */
  5835. for (p = 0; p < FAVNUM; p++) {
  5836. if (!d->softkeyicon[p]) {
  5837. break;
  5838. }
  5839. }
  5840. if (p == FAVNUM) {
  5841. ast_log(LOG_WARNING, "No more free bookmark position\n");
  5842. return 0;
  5843. }
  5844. }
  5845. at = strchr(line, '@');
  5846. if (!at) {
  5847. ast_log(LOG_NOTICE, "Bookmark entry '%s' has no @ (at) sign!\n", text);
  5848. return 0;
  5849. }
  5850. *at = '\0';
  5851. at++;
  5852. number = at;
  5853. at = strchr(at, '@');
  5854. if (ast_strlen_zero(number)) {
  5855. ast_log(LOG_NOTICE, "Bookmark entry '%s' has no number\n", text);
  5856. return 0;
  5857. }
  5858. if (ast_strlen_zero(line)) {
  5859. ast_log(LOG_NOTICE, "Bookmark entry '%s' has no description\n", text);
  5860. return 0;
  5861. }
  5862. at = strchr(number, '@');
  5863. if (!at) {
  5864. d->softkeyicon[p] = FAV_ICON_SHARP; /* default icon */
  5865. } else {
  5866. *at = '\0';
  5867. at++;
  5868. icon = at;
  5869. if (ast_strlen_zero(icon)) {
  5870. ast_log(LOG_NOTICE, "Bookmark entry '%s' has no icon value\n", text);
  5871. return 0;
  5872. }
  5873. if (strncmp(icon, "USTM/", 5)) {
  5874. d->softkeyicon[p] = atoi(icon);
  5875. } else {
  5876. d->softkeyicon[p] = 1;
  5877. ast_copy_string(d->softkeydevice[p], icon + 5, sizeof(d->softkeydevice[p]));
  5878. }
  5879. }
  5880. ast_copy_string(d->softkeylabel[p], line, sizeof(d->softkeylabel[p]));
  5881. ast_copy_string(d->softkeynumber[p], number, sizeof(d->softkeynumber[p]));
  5882. if (unistimdebug) {
  5883. ast_verb(0, "New bookmark at pos %d label='%s' number='%s' icon=%#x\n",
  5884. p, d->softkeylabel[p], d->softkeynumber[p], (unsigned)d->softkeyicon[p]);
  5885. }
  5886. return 1;
  5887. }
  5888. /* Looking for dynamic icons entries in bookmarks */
  5889. static void finish_bookmark(void)
  5890. {
  5891. struct unistim_device *d = devices;
  5892. int i;
  5893. ast_mutex_lock(&devicelock);
  5894. while (d) {
  5895. for (i = 0; i < 6; i++) {
  5896. if (d->softkeyicon[i] == 1) { /* Something for us */
  5897. struct unistim_device *d2 = devices;
  5898. while (d2) {
  5899. if (!strcmp(d->softkeydevice[i], d2->name)) {
  5900. d->sp[i] = d2;
  5901. d->softkeyicon[i] = 0;
  5902. break;
  5903. }
  5904. d2 = d2->next;
  5905. }
  5906. if (d->sp[i] == NULL) {
  5907. ast_log(LOG_NOTICE, "Bookmark entry with device %s not found\n",
  5908. d->softkeydevice[i]);
  5909. }
  5910. }
  5911. }
  5912. d = d->next;
  5913. }
  5914. ast_mutex_unlock(&devicelock);
  5915. }
  5916. static struct unistim_line *find_line_by_number(struct unistim_device *d, const char *val) {
  5917. struct unistim_line *l, *ret = NULL;
  5918. AST_LIST_LOCK(&d->lines);
  5919. AST_LIST_TRAVERSE(&d->lines, l, list) {
  5920. if (!strcmp(l->name, val)) {
  5921. ret = l;
  5922. break;
  5923. }
  5924. }
  5925. AST_LIST_UNLOCK(&d->lines);
  5926. return ret;
  5927. }
  5928. static void delete_device(struct unistim_device *d)
  5929. {
  5930. struct unistim_line *l;
  5931. struct unistim_subchannel *sub;
  5932. struct unistimsession *s;
  5933. if (unistimdebug) {
  5934. ast_verb(0, "Removing device '%s'\n", d->name);
  5935. }
  5936. AST_LIST_LOCK(&d->subs);
  5937. AST_LIST_TRAVERSE_SAFE_BEGIN(&d->subs, sub, list){
  5938. if (sub->subtype == SUB_REAL) {
  5939. if (sub->owner) {
  5940. ast_log(LOG_WARNING,
  5941. "Device '%s' was not deleted : a call is in progress. Try again later.\n",
  5942. d->name);
  5943. AST_LIST_UNLOCK(&d->subs);
  5944. return;
  5945. }
  5946. }
  5947. if (sub->subtype == SUB_THREEWAY) {
  5948. ast_log(LOG_WARNING,
  5949. "Device '%s' with threeway call subchannels allocated, aborting.\n",
  5950. d->name);
  5951. AST_LIST_UNLOCK(&d->subs);
  5952. return;
  5953. }
  5954. AST_LIST_REMOVE_CURRENT(list);
  5955. ast_mutex_destroy(&sub->lock);
  5956. ast_free(sub);
  5957. }
  5958. AST_LIST_TRAVERSE_SAFE_END
  5959. AST_LIST_UNLOCK(&d->subs);
  5960. AST_LIST_LOCK(&d->lines);
  5961. AST_LIST_TRAVERSE_SAFE_BEGIN(&d->lines, l, list){
  5962. AST_LIST_REMOVE_CURRENT(list);
  5963. ast_mutex_destroy(&l->lock);
  5964. unistim_line_destroy(l);
  5965. }
  5966. AST_LIST_TRAVERSE_SAFE_END
  5967. AST_LIST_UNLOCK(&d->lines);
  5968. if (d->session) {
  5969. if (sessions == d->session) {
  5970. sessions = d->session->next;
  5971. } else {
  5972. s = sessions;
  5973. while (s) {
  5974. if (s->next == d->session) {
  5975. s->next = d->session->next;
  5976. break;
  5977. }
  5978. s = s->next;
  5979. }
  5980. }
  5981. ast_mutex_destroy(&d->session->lock);
  5982. ast_free(d->session);
  5983. }
  5984. if (devices == d) {
  5985. devices = d->next;
  5986. } else {
  5987. struct unistim_device *d2 = devices;
  5988. while (d2) {
  5989. if (d2->next == d) {
  5990. d2->next = d->next;
  5991. break;
  5992. }
  5993. d2 = d2->next;
  5994. }
  5995. }
  5996. if (d->tz) {
  5997. d->tz = ast_tone_zone_unref(d->tz);
  5998. }
  5999. ast_mutex_destroy(&d->lock);
  6000. ast_free(d);
  6001. }
  6002. static struct unistim_device *build_device(const char *cat, const struct ast_variable *v)
  6003. {
  6004. struct unistim_device *d;
  6005. struct unistim_line *l = NULL, *lt = NULL;
  6006. int create = 1;
  6007. int nbsoftkey, dateformat, timeformat, callhistory, sharpdial, linecnt;
  6008. char linelabel[AST_MAX_EXTENSION];
  6009. signed char ringvolume, ringstyle, cwvolume, cwstyle;
  6010. /* First, we need to know if we already have this name in our list */
  6011. /* Get a lock for the device chained list */
  6012. ast_mutex_lock(&devicelock);
  6013. d = devices;
  6014. while (d) {
  6015. if (!strcmp(d->name, cat)) {
  6016. /* Yep, we alreay have this one */
  6017. if (unistimsock < 0) {
  6018. /* It's a dupe */
  6019. ast_log(LOG_WARNING, "Duplicate entry found (%s), ignoring.\n", cat);
  6020. ast_mutex_unlock(&devicelock);
  6021. return NULL;
  6022. }
  6023. /* we're reloading right now */
  6024. create = 0;
  6025. break;
  6026. }
  6027. d = d->next;
  6028. }
  6029. ast_mutex_unlock(&devicelock);
  6030. if (!(lt = ast_calloc(1, sizeof(*lt)))) {
  6031. return NULL;
  6032. }
  6033. if (create) {
  6034. if (!(d = ast_calloc(1, sizeof(*d)))) {
  6035. return NULL;
  6036. }
  6037. ast_mutex_init(&d->lock);
  6038. ast_copy_string(d->name, cat, sizeof(d->name));
  6039. ast_copy_string(d->context, DEFAULTCONTEXT, sizeof(d->context));
  6040. d->contrast = -1;
  6041. d->output = OUTPUT_HANDSET;
  6042. d->previous_output = OUTPUT_HANDSET;
  6043. d->volume = VOLUME_LOW;
  6044. d->microphone = MUTE_OFF;
  6045. d->height = DEFAULTHEIGHT;
  6046. d->selected = -1;
  6047. d->interdigit_timer = DEFAULT_INTERDIGIT_TIMER;
  6048. } else {
  6049. /* Delete existing line information */
  6050. AST_LIST_LOCK(&d->lines);
  6051. AST_LIST_TRAVERSE_SAFE_BEGIN(&d->lines, l, list){
  6052. AST_LIST_REMOVE_CURRENT(list);
  6053. unistim_line_destroy(l);
  6054. }
  6055. AST_LIST_TRAVERSE_SAFE_END
  6056. AST_LIST_UNLOCK(&d->lines);
  6057. /* reset bookmarks */
  6058. memset(d->softkeylabel, 0, sizeof(d->softkeylabel));
  6059. memset(d->softkeynumber, 0, sizeof(d->softkeynumber));
  6060. memset(d->softkeyicon, 0, sizeof(d->softkeyicon));
  6061. memset(d->softkeydevice, 0, sizeof(d->softkeydevice));
  6062. memset(d->ssub, 0, sizeof(d->ssub));
  6063. memset(d->sline, 0, sizeof(d->sline));
  6064. memset(d->sp, 0, sizeof(d->sp));
  6065. }
  6066. linelabel[0] = '\0';
  6067. dateformat = 1;
  6068. timeformat = 1;
  6069. ringvolume = 2;
  6070. cwvolume = 1;
  6071. callhistory = 1;
  6072. sharpdial = 0;
  6073. ringstyle = 3;
  6074. cwstyle = 2;
  6075. nbsoftkey = 0;
  6076. linecnt = 0;
  6077. d->dtmfduration = 0;
  6078. while (v) {
  6079. if (!strcasecmp(v->name, "rtp_port")) {
  6080. d->rtp_port = atoi(v->value);
  6081. } else if (!strcasecmp(v->name, "rtp_method")) {
  6082. d->rtp_method = atoi(v->value);
  6083. } else if (!strcasecmp(v->name, "status_method")) {
  6084. d->status_method = atoi(v->value);
  6085. } else if (!strcasecmp(v->name, "device")) {
  6086. ast_copy_string(d->id, v->value, sizeof(d->id));
  6087. } else if (!strcasecmp(v->name, "tn")) {
  6088. ast_copy_string(d->extension_number, v->value, sizeof(d->extension_number));
  6089. } else if (!strcasecmp(v->name, "permit") || !strcasecmp(v->name, "deny")) {
  6090. int acl_error = 0;
  6091. d->ha = ast_append_ha(v->name, v->value, d->ha, &acl_error);
  6092. if (acl_error) {
  6093. ast_log(LOG_ERROR, "Invalid ACL '%s' specified for device '%s' on line %d. Deleting device\n",
  6094. v->value, cat, v->lineno);
  6095. delete_device(d);
  6096. return NULL;
  6097. }
  6098. } else if (!strcasecmp(v->name, "context")) {
  6099. ast_copy_string(d->context, v->value, sizeof(d->context));
  6100. } else if (!strcasecmp(v->name, "maintext0")) {
  6101. unquote(d->maintext0, v->value, sizeof(d->maintext0) - 1);
  6102. } else if (!strcasecmp(v->name, "maintext1")) {
  6103. unquote(d->maintext1, v->value, sizeof(d->maintext1) - 1);
  6104. } else if (!strcasecmp(v->name, "maintext2")) {
  6105. unquote(d->maintext2, v->value, sizeof(d->maintext2) - 1);
  6106. } else if (!strcasecmp(v->name, "titledefault")) {
  6107. unquote(d->titledefault, v->value, sizeof(d->titledefault) - 1);
  6108. } else if (!strcasecmp(v->name, "dateformat")) {
  6109. dateformat = atoi(v->value);
  6110. } else if (!strcasecmp(v->name, "timeformat")) {
  6111. timeformat = atoi(v->value);
  6112. } else if (!strcasecmp(v->name, "contrast")) {
  6113. d->contrast = atoi(v->value);
  6114. if ((d->contrast < 0) || (d->contrast > 15)) {
  6115. ast_log(LOG_WARNING, "contrast must be beetween 0 and 15\n");
  6116. d->contrast = 8;
  6117. }
  6118. } else if (!strcasecmp(v->name, "nat")) {
  6119. d->nat = ast_true(v->value);
  6120. } else if (!strcasecmp(v->name, "hasexp")) {
  6121. d->hasexp = ast_true(v->value);
  6122. } else if (!strcasecmp(v->name, "ringvolume")) {
  6123. ringvolume = atoi(v->value);
  6124. } else if (!strcasecmp(v->name, "ringstyle")) {
  6125. ringstyle = atoi(v->value);
  6126. } else if (!strcasecmp(v->name, "cwvolume")) {
  6127. cwvolume = atoi(v->value);
  6128. } else if (!strcasecmp(v->name, "cwstyle")) {
  6129. cwstyle = atoi(v->value);
  6130. } else if (!strcasecmp(v->name, "callhistory")) {
  6131. callhistory = atoi(v->value);
  6132. } else if (!strcasecmp(v->name, "sharpdial")) {
  6133. sharpdial = ast_true(v->value) ? 1 : 0;
  6134. } else if (!strcasecmp(v->name, "interdigit_timer")) {
  6135. d->interdigit_timer = atoi(v->value);
  6136. } else if (!strcasecmp(v->name, "dtmf_duration")) {
  6137. d->dtmfduration = atoi(v->value);
  6138. if (d->dtmfduration > 150) {
  6139. d->dtmfduration = 150;
  6140. }
  6141. } else if (!strcasecmp(v->name, "callerid")) {
  6142. if (!strcasecmp(v->value, "asreceived")) {
  6143. lt->cid_num[0] = '\0';
  6144. } else {
  6145. ast_copy_string(lt->cid_num, v->value, sizeof(lt->cid_num));
  6146. }
  6147. } else if (!strcasecmp(v->name, "language")) {
  6148. ast_copy_string(d->language, v->value, sizeof(d->language));
  6149. } else if (!strcasecmp(v->name, "country")) {
  6150. ast_copy_string(d->country, v->value, sizeof(d->country));
  6151. } else if (!strcasecmp(v->name, "accountcode")) {
  6152. ast_copy_string(lt->accountcode, v->value, sizeof(lt->accountcode));
  6153. } else if (!strcasecmp(v->name, "amaflags")) {
  6154. int y;
  6155. y = ast_channel_string2amaflag(v->value);
  6156. if (y < 0) {
  6157. ast_log(LOG_WARNING, "Invalid AMA flags: %s at line %d\n", v->value,
  6158. v->lineno);
  6159. } else {
  6160. lt->amaflags = y;
  6161. }
  6162. } else if (!strcasecmp(v->name, "musiconhold")) {
  6163. ast_copy_string(lt->musicclass, v->value, sizeof(lt->musicclass));
  6164. } else if (!strcasecmp(v->name, "callgroup")) {
  6165. lt->callgroup = ast_get_group(v->value);
  6166. } else if (!strcasecmp(v->name, "pickupgroup")) {
  6167. lt->pickupgroup = ast_get_group(v->value);
  6168. } else if (!strcasecmp(v->name, "mailbox")) {
  6169. ast_copy_string(lt->mailbox, v->value, sizeof(lt->mailbox));
  6170. } else if (!strcasecmp(v->name, "parkinglot")) {
  6171. ast_copy_string(lt->parkinglot, v->value, sizeof(lt->parkinglot));
  6172. } else if (!strcasecmp(v->name, "linelabel")) {
  6173. unquote(linelabel, v->value, sizeof(linelabel) - 1);
  6174. } else if (!strcasecmp(v->name, "extension")) {
  6175. if (!strcasecmp(v->value, "none")) {
  6176. d->extension = EXTENSION_NONE;
  6177. } else if (!strcasecmp(v->value, "ask")) {
  6178. d->extension = EXTENSION_ASK;
  6179. } else if (!strcasecmp(v->value, "line")) {
  6180. d->extension = EXTENSION_LINE;
  6181. } else {
  6182. ast_log(LOG_WARNING, "Unknown extension option.\n");
  6183. }
  6184. } else if (!strcasecmp(v->name, "bookmark")) {
  6185. if (nbsoftkey > 5) {
  6186. ast_log(LOG_WARNING,
  6187. "More than 6 softkeys defined. Ignoring new entries.\n");
  6188. } else {
  6189. if (parse_bookmark(v->value, d)) {
  6190. nbsoftkey++;
  6191. }
  6192. }
  6193. } else if (!strcasecmp(v->name, "line")) {
  6194. int len = strlen(linelabel);
  6195. int create_line = 0;
  6196. l = find_line_by_number(d, v->value);
  6197. if (!l) { /* If line still not exists */
  6198. if (!(l = unistim_line_alloc())) {
  6199. ast_free(d);
  6200. ast_free(lt);
  6201. return NULL;
  6202. }
  6203. lt->cap = l->cap;
  6204. memcpy(l, lt, sizeof(*l));
  6205. ast_mutex_init(&l->lock);
  6206. create_line = 1;
  6207. }
  6208. d->to_delete = 0;
  6209. /* Set softkey info for new line*/
  6210. d->sline[nbsoftkey] = l;
  6211. d->softkeyicon[nbsoftkey] = FAV_LINE_ICON;
  6212. if (!len) { /* label is undefined ? */
  6213. ast_copy_string(d->softkeylabel[nbsoftkey], v->value, sizeof(d->softkeylabel[nbsoftkey]));
  6214. } else {
  6215. int softkeylinepos = 0;
  6216. if ((len > 2) && (linelabel[1] == '@')) {
  6217. softkeylinepos = linelabel[0];
  6218. if ((softkeylinepos >= '0') && (softkeylinepos <= '5')) {
  6219. softkeylinepos -= '0';
  6220. d->softkeyicon[nbsoftkey] = FAV_ICON_NONE;
  6221. } else {
  6222. ast_log(LOG_WARNING,
  6223. "Invalid position for linelabel : must be between 0 and 5\n");
  6224. }
  6225. ast_copy_string(d->softkeylabel[softkeylinepos], linelabel + 2,
  6226. sizeof(d->softkeylabel[softkeylinepos]));
  6227. d->softkeyicon[softkeylinepos] = FAV_LINE_ICON;
  6228. } else {
  6229. ast_copy_string(d->softkeylabel[nbsoftkey], linelabel,
  6230. sizeof(d->softkeylabel[nbsoftkey]));
  6231. }
  6232. }
  6233. nbsoftkey++;
  6234. if (create_line) {
  6235. ast_copy_string(l->name, v->value, sizeof(l->name));
  6236. snprintf(l->fullname, sizeof(l->fullname), "USTM/%s@%s", l->name, d->name);
  6237. if (!ast_strlen_zero(l->mailbox)) {
  6238. if (unistimdebug) {
  6239. ast_verb(3, "Setting mailbox '%s' on %s@%s\n", l->mailbox, d->name, l->name);
  6240. }
  6241. }
  6242. ast_format_cap_append_from_cap(l->cap, global_cap, AST_MEDIA_TYPE_UNKNOWN);
  6243. l->parent = d;
  6244. linecnt++;
  6245. AST_LIST_LOCK(&d->lines);
  6246. AST_LIST_INSERT_TAIL(&d->lines, l, list);
  6247. AST_LIST_UNLOCK(&d->lines);
  6248. }
  6249. } else if (!strcasecmp(v->name, "height")) {
  6250. /* Allow the user to lower the expected display lines on the phone
  6251. * For example the Nortel i2001 and i2002 only have one ! */
  6252. d->height = atoi(v->value);
  6253. } else
  6254. ast_log(LOG_WARNING, "Don't know keyword '%s' at line %d\n", v->name,
  6255. v->lineno);
  6256. v = v->next;
  6257. }
  6258. ast_free(lt);
  6259. if (linecnt == 0) {
  6260. ast_log(LOG_ERROR, "An Unistim device must have at least one line!\n");
  6261. ast_free(d);
  6262. return NULL;
  6263. }
  6264. d->ringvolume = ringvolume;
  6265. d->ringstyle = ringstyle;
  6266. d->cwvolume = cwvolume;
  6267. d->cwstyle = cwstyle;
  6268. d->callhistory = callhistory;
  6269. d->sharp_dial = sharpdial;
  6270. d->tz = ast_get_indication_zone(d->country);
  6271. if ((d->tz == NULL) && !ast_strlen_zero(d->country)) {
  6272. ast_log(LOG_WARNING, "Country '%s' was not found in indications.conf\n",
  6273. d->country);
  6274. }
  6275. d->datetimeformat = 48 + (dateformat * 4);
  6276. d->datetimeformat += timeformat;
  6277. if ((autoprovisioning == AUTOPROVISIONING_TN) &&
  6278. (!ast_strlen_zero(d->extension_number))) {
  6279. d->extension = EXTENSION_TN;
  6280. if (!ast_strlen_zero(d->id)) {
  6281. ast_log(LOG_WARNING,
  6282. "tn= and device= can't be used together. Ignoring device= entry\n");
  6283. }
  6284. d->id[0] = 'T'; /* magic : this is a tn entry */
  6285. ast_copy_string((d->id) + 1, d->extension_number, sizeof(d->id) - 1);
  6286. d->extension_number[0] = '\0';
  6287. } else if (ast_strlen_zero(d->id)) {
  6288. if (strcmp(d->name, "template")) {
  6289. ast_log(LOG_ERROR, "You must specify the mac address with device=\n");
  6290. if (d->tz) {
  6291. d->tz = ast_tone_zone_unref(d->tz);
  6292. }
  6293. ast_free(d);
  6294. return NULL;
  6295. } else {
  6296. strcpy(d->id, "000000000000");
  6297. }
  6298. }
  6299. if (!d->rtp_port) {
  6300. d->rtp_port = 10000;
  6301. }
  6302. if (d->contrast == -1) {
  6303. d->contrast = 8;
  6304. }
  6305. if (ast_strlen_zero(d->maintext1)) {
  6306. strcpy(d->maintext1, d->name);
  6307. }
  6308. if (ast_strlen_zero(d->titledefault)) {
  6309. struct ast_tm tm = { 0, };
  6310. struct timeval cur_time = ast_tvnow();
  6311. if ((ast_localtime(&cur_time, &tm, 0)) == 0 || ast_strlen_zero(tm.tm_zone)) {
  6312. ast_log(LOG_WARNING, "Error in ast_localtime()\n");
  6313. ast_copy_string(d->titledefault, "UNISTIM for*", 12);
  6314. } else {
  6315. if (strlen(tm.tm_zone) < 4) {
  6316. strcpy(d->titledefault, "TimeZone ");
  6317. strcat(d->titledefault, tm.tm_zone);
  6318. } else if (strlen(tm.tm_zone) < 9) {
  6319. strcpy(d->titledefault, "TZ ");
  6320. strcat(d->titledefault, tm.tm_zone);
  6321. } else {
  6322. ast_copy_string(d->titledefault, tm.tm_zone, 12);
  6323. }
  6324. }
  6325. }
  6326. /* Update the chained link if it's a new device */
  6327. if (create) {
  6328. ast_mutex_lock(&devicelock);
  6329. d->next = devices;
  6330. devices = d;
  6331. ast_mutex_unlock(&devicelock);
  6332. ast_verb(3, "Added device '%s'\n", d->name);
  6333. } else {
  6334. ast_verb(3, "Device '%s' reloaded\n", d->name);
  6335. }
  6336. return d;
  6337. }
  6338. /*--- reload_config: Re-read unistim.conf config file ---*/
  6339. static int reload_config(void)
  6340. {
  6341. struct ast_config *cfg;
  6342. struct ast_variable *v;
  6343. struct sockaddr_in bindaddr = { 0, };
  6344. char *config = "unistim.conf";
  6345. char *cat;
  6346. struct unistim_device *d;
  6347. const int reuseFlag = 1;
  6348. struct unistimsession *s;
  6349. struct ast_flags config_flags = { 0, };
  6350. cfg = ast_config_load(config, config_flags);
  6351. /* We *must* have a config file otherwise stop immediately */
  6352. if (!cfg) {
  6353. ast_log(LOG_ERROR, "Unable to load config %s\n", config);
  6354. return -1;
  6355. } else if (cfg == CONFIG_STATUS_FILEINVALID) {
  6356. ast_log(LOG_ERROR, "Config file %s is in an invalid format. Aborting.\n", config);
  6357. return -1;
  6358. }
  6359. /* Copy the default jb config over global_jbconf */
  6360. memcpy(&global_jbconf, &default_jbconf, sizeof(struct ast_jb_conf));
  6361. unistim_keepalive = 120;
  6362. unistim_port = 0;
  6363. v = ast_variable_browse(cfg, "general");
  6364. while (v) {
  6365. /* handle jb conf */
  6366. if (!ast_jb_read_conf(&global_jbconf, v->name, v->value)) {
  6367. continue;
  6368. }
  6369. if (!strcasecmp(v->name, "keepalive")) {
  6370. unistim_keepalive = atoi(v->value);
  6371. } else if (!strcasecmp(v->name, "port")) {
  6372. unistim_port = atoi(v->value);
  6373. } else if (!strcasecmp(v->name, "tos")) {
  6374. if (ast_str2tos(v->value, &qos.tos)) {
  6375. ast_log(LOG_WARNING, "Invalid tos value at line %d, refer to QoS documentation\n", v->lineno);
  6376. }
  6377. } else if (!strcasecmp(v->name, "tos_audio")) {
  6378. if (ast_str2tos(v->value, &qos.tos_audio)) {
  6379. ast_log(LOG_WARNING, "Invalid tos_audio value at line %d, refer to QoS documentation\n", v->lineno);
  6380. }
  6381. } else if (!strcasecmp(v->name, "cos")) {
  6382. if (ast_str2cos(v->value, &qos.cos)) {
  6383. ast_log(LOG_WARNING, "Invalid cos value at line %d, refer to QoS documentation\n", v->lineno);
  6384. }
  6385. } else if (!strcasecmp(v->name, "cos_audio")) {
  6386. if (ast_str2cos(v->value, &qos.cos_audio)) {
  6387. ast_log(LOG_WARNING, "Invalid cos_audio value at line %d, refer to QoS documentation\n", v->lineno);
  6388. }
  6389. } else if (!strcasecmp(v->name, "debug")) {
  6390. if (!strcasecmp(v->value, "no")) {
  6391. unistimdebug = 0;
  6392. } else if (!strcasecmp(v->value, "yes")) {
  6393. unistimdebug = 1;
  6394. }
  6395. } else if (!strcasecmp(v->name, "autoprovisioning")) {
  6396. if (!strcasecmp(v->value, "no")) {
  6397. autoprovisioning = AUTOPROVISIONING_NO;
  6398. } else if (!strcasecmp(v->value, "yes")) {
  6399. autoprovisioning = AUTOPROVISIONING_YES;
  6400. } else if (!strcasecmp(v->value, "tn")) {
  6401. autoprovisioning = AUTOPROVISIONING_TN;
  6402. } else {
  6403. ast_log(LOG_WARNING, "Unknown autoprovisioning option.\n");
  6404. }
  6405. } else if (!strcasecmp(v->name, "public_ip")) {
  6406. if (!ast_strlen_zero(v->value)) {
  6407. struct ast_sockaddr addr = { {0,} };
  6408. if (ast_sockaddr_resolve_first_af(&addr, v->value, PARSE_PORT_FORBID, AF_INET)) {
  6409. ast_log(LOG_WARNING, "Invalid address: %s\n", v->value);
  6410. } else {
  6411. ast_sockaddr_to_sin(&addr, &public_ip);
  6412. }
  6413. }
  6414. }
  6415. v = v->next;
  6416. }
  6417. if ((unistim_keepalive < 10) ||
  6418. (unistim_keepalive >
  6419. 255 - (((NB_MAX_RETRANSMIT + 1) * RETRANSMIT_TIMER) / 1000))) {
  6420. ast_log(LOG_ERROR, "keepalive is invalid in %s\n", config);
  6421. ast_config_destroy(cfg);
  6422. return -1;
  6423. }
  6424. packet_send_ping[4] =
  6425. unistim_keepalive + (((NB_MAX_RETRANSMIT + 1) * RETRANSMIT_TIMER) / 1000);
  6426. if ((unistim_port < 1) || (unistim_port > 65535)) {
  6427. ast_log(LOG_ERROR, "port is not set or invalid in %s\n", config);
  6428. ast_config_destroy(cfg);
  6429. return -1;
  6430. }
  6431. unistim_keepalive *= 1000;
  6432. ast_mutex_lock(&devicelock);
  6433. d = devices;
  6434. while (d) {
  6435. if (d->to_delete >= 0) {
  6436. d->to_delete = 1;
  6437. }
  6438. d = d->next;
  6439. }
  6440. ast_mutex_unlock(&devicelock);
  6441. /* load the device sections */
  6442. cat = ast_category_browse(cfg, NULL);
  6443. while (cat) {
  6444. if (strcasecmp(cat, "general")) {
  6445. d = build_device(cat, ast_variable_browse(cfg, cat));
  6446. }
  6447. cat = ast_category_browse(cfg, cat);
  6448. }
  6449. ast_mutex_lock(&devicelock);
  6450. d = devices;
  6451. while (d) {
  6452. if (d->to_delete) {
  6453. delete_device(d);
  6454. d = devices;
  6455. continue;
  6456. }
  6457. d = d->next;
  6458. }
  6459. finish_bookmark();
  6460. ast_mutex_unlock(&devicelock);
  6461. ast_config_destroy(cfg);
  6462. ast_mutex_lock(&sessionlock);
  6463. s = sessions;
  6464. while (s) {
  6465. if (s->device) {
  6466. refresh_all_favorite(s);
  6467. if (ast_strlen_zero(s->device->language)) {
  6468. struct unistim_languages lang;
  6469. lang = options_languages[find_language(s->device->language)];
  6470. send_charset_update(s, lang.encoding);
  6471. }
  6472. }
  6473. s = s->next;
  6474. }
  6475. ast_mutex_unlock(&sessionlock);
  6476. /* We don't recreate a socket when reloading (locks would be necessary). */
  6477. if (unistimsock > -1) {
  6478. return 0;
  6479. }
  6480. bindaddr.sin_addr.s_addr = INADDR_ANY;
  6481. bindaddr.sin_port = htons(unistim_port);
  6482. bindaddr.sin_family = AF_INET;
  6483. unistimsock = socket(AF_INET, SOCK_DGRAM, 0);
  6484. if (unistimsock < 0) {
  6485. ast_log(LOG_WARNING, "Unable to create UNISTIM socket: %s\n", strerror(errno));
  6486. return -1;
  6487. }
  6488. #ifdef HAVE_PKTINFO
  6489. {
  6490. const int pktinfoFlag = 1;
  6491. setsockopt(unistimsock, IPPROTO_IP, IP_PKTINFO, &pktinfoFlag,
  6492. sizeof(pktinfoFlag));
  6493. }
  6494. #else
  6495. if (public_ip.sin_family == 0) {
  6496. ast_log(LOG_WARNING,
  6497. "Your OS does not support IP_PKTINFO, you must set public_ip.\n");
  6498. unistimsock = -1;
  6499. return -1;
  6500. }
  6501. #endif
  6502. setsockopt(unistimsock, SOL_SOCKET, SO_REUSEADDR, (const char *) &reuseFlag,
  6503. sizeof(reuseFlag));
  6504. if (bind(unistimsock, (struct sockaddr *) &bindaddr, sizeof(bindaddr)) < 0) {
  6505. ast_log(LOG_WARNING, "Failed to bind to %s:%d: %s\n",
  6506. ast_inet_ntoa(bindaddr.sin_addr), htons(bindaddr.sin_port),
  6507. strerror(errno));
  6508. close(unistimsock);
  6509. unistimsock = -1;
  6510. } else {
  6511. ast_verb(2, "UNISTIM Listening on %s:%d\n", ast_inet_ntoa(bindaddr.sin_addr), htons(bindaddr.sin_port));
  6512. ast_set_qos(unistimsock, qos.tos, qos.cos, "UNISTIM");
  6513. }
  6514. return 0;
  6515. }
  6516. static enum ast_rtp_glue_result unistim_get_rtp_peer(struct ast_channel *chan, struct ast_rtp_instance **instance)
  6517. {
  6518. struct unistim_subchannel *sub = ast_channel_tech_pvt(chan);
  6519. if (!sub) {
  6520. return AST_RTP_GLUE_RESULT_FORBID;
  6521. }
  6522. if (!sub->rtp) {
  6523. return AST_RTP_GLUE_RESULT_FORBID;
  6524. }
  6525. ao2_ref(sub->rtp, +1);
  6526. *instance = sub->rtp;
  6527. return AST_RTP_GLUE_RESULT_LOCAL;
  6528. }
  6529. static int unistim_set_rtp_peer(struct ast_channel *chan, struct ast_rtp_instance *rtp, struct ast_rtp_instance *vrtp, struct ast_rtp_instance *trtp, const struct ast_format_cap *codecs, int nat_active)
  6530. {
  6531. struct unistim_subchannel *sub = ast_channel_tech_pvt(chan);
  6532. struct sockaddr_in them = { 0, };
  6533. struct sockaddr_in us = { 0, };
  6534. if (!rtp) {
  6535. return 0;
  6536. }
  6537. sub = (struct unistim_subchannel *) ast_channel_tech_pvt(chan);
  6538. if (!sub) {
  6539. ast_log(LOG_ERROR, "No Private Structure, this is bad\n");
  6540. return -1;
  6541. }
  6542. {
  6543. struct ast_sockaddr tmp;
  6544. ast_rtp_instance_get_remote_address(rtp, &tmp);
  6545. ast_sockaddr_to_sin(&tmp, &them);
  6546. ast_rtp_instance_get_local_address(rtp, &tmp);
  6547. ast_sockaddr_to_sin(&tmp, &us);
  6548. }
  6549. /* TODO: Set rtp on phone in case of direct rtp (not implemented) */
  6550. return 0;
  6551. }
  6552. static struct ast_rtp_glue unistim_rtp_glue = {
  6553. .type = channel_type,
  6554. .get_rtp_info = unistim_get_rtp_peer,
  6555. .update_peer = unistim_set_rtp_peer,
  6556. };
  6557. /*--- load_module: PBX load module - initialization ---*/
  6558. int load_module(void)
  6559. {
  6560. int res;
  6561. if (!(global_cap = ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_DEFAULT))) {
  6562. goto buff_failed;
  6563. }
  6564. if (!(unistim_tech.capabilities = ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_DEFAULT))) {
  6565. goto buff_failed;
  6566. }
  6567. ast_format_cap_append(global_cap, ast_format_ulaw, 0);
  6568. ast_format_cap_append(global_cap, ast_format_alaw, 0);
  6569. ast_format_cap_append_from_cap(unistim_tech.capabilities, global_cap, AST_MEDIA_TYPE_AUDIO);
  6570. if (!(buff = ast_malloc(SIZE_PAGE))) {
  6571. goto buff_failed;
  6572. }
  6573. io = io_context_create();
  6574. if (!io) {
  6575. ast_log(LOG_ERROR, "Failed to allocate IO context\n");
  6576. goto io_failed;
  6577. }
  6578. sched = ast_sched_context_create();
  6579. if (!sched) {
  6580. ast_log(LOG_ERROR, "Failed to allocate scheduler context\n");
  6581. goto sched_failed;
  6582. }
  6583. res = reload_config();
  6584. if (res) {
  6585. ao2_ref(unistim_tech.capabilities, -1);
  6586. ao2_ref(global_cap, -1);
  6587. ast_sched_context_destroy(sched);
  6588. io_context_destroy(io);
  6589. return AST_MODULE_LOAD_DECLINE;
  6590. }
  6591. /* Make sure we can register our unistim channel type */
  6592. if (ast_channel_register(&unistim_tech)) {
  6593. ast_log(LOG_ERROR, "Unable to register channel type '%s'\n", channel_type);
  6594. goto chanreg_failed;
  6595. }
  6596. ast_rtp_glue_register(&unistim_rtp_glue);
  6597. ast_cli_register_multiple(unistim_cli, ARRAY_LEN(unistim_cli));
  6598. restart_monitor();
  6599. return AST_MODULE_LOAD_SUCCESS;
  6600. chanreg_failed:
  6601. /*! XXX \todo Leaking anything allocated by reload_config() ... */
  6602. ast_sched_context_destroy(sched);
  6603. sched = NULL;
  6604. sched_failed:
  6605. io_context_destroy(io);
  6606. io = NULL;
  6607. io_failed:
  6608. ast_free(buff);
  6609. buff = NULL;
  6610. buff_failed:
  6611. ao2_cleanup(global_cap);
  6612. global_cap = NULL;
  6613. ao2_cleanup(unistim_tech.capabilities);
  6614. unistim_tech.capabilities = NULL;
  6615. return AST_MODULE_LOAD_DECLINE;
  6616. }
  6617. static int unload_module(void)
  6618. {
  6619. /* First, take us out of the channel loop */
  6620. if (sched) {
  6621. ast_sched_context_destroy(sched);
  6622. }
  6623. ast_cli_unregister_multiple(unistim_cli, ARRAY_LEN(unistim_cli));
  6624. ast_channel_unregister(&unistim_tech);
  6625. ao2_cleanup(unistim_tech.capabilities);
  6626. ast_rtp_glue_unregister(&unistim_rtp_glue);
  6627. ast_mutex_lock(&monlock);
  6628. if (monitor_thread && (monitor_thread != AST_PTHREADT_STOP) && (monitor_thread != AST_PTHREADT_NULL)) {
  6629. pthread_cancel(monitor_thread);
  6630. pthread_kill(monitor_thread, SIGURG);
  6631. pthread_join(monitor_thread, NULL);
  6632. }
  6633. monitor_thread = AST_PTHREADT_STOP;
  6634. ast_mutex_unlock(&monlock);
  6635. if (buff) {
  6636. ast_free(buff);
  6637. }
  6638. if (unistimsock > -1) {
  6639. close(unistimsock);
  6640. }
  6641. ao2_ref(global_cap, -1);
  6642. return 0;
  6643. }
  6644. /*! reload: Part of Asterisk module interface ---*/
  6645. int reload(void)
  6646. {
  6647. if (unistimdebug) {
  6648. ast_verb(0, "reload unistim\n");
  6649. }
  6650. ast_mutex_lock(&unistim_reload_lock);
  6651. if (!unistim_reloading) {
  6652. unistim_reloading = 1;
  6653. }
  6654. ast_mutex_unlock(&unistim_reload_lock);
  6655. restart_monitor();
  6656. return 0;
  6657. }
  6658. AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_DEFAULT, "UNISTIM Protocol (USTM)",
  6659. .support_level = AST_MODULE_SUPPORT_EXTENDED,
  6660. .load = load_module,
  6661. .unload = unload_module,
  6662. .reload = reload,
  6663. );