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