chan_unistim.c 171 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757
  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__)
  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__ */
  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/event.h"
  60. #include "asterisk/rtp_engine.h"
  61. #include "asterisk/netsock.h"
  62. #include "asterisk/acl.h"
  63. #include "asterisk/callerid.h"
  64. #include "asterisk/cli.h"
  65. #include "asterisk/app.h"
  66. #include "asterisk/musiconhold.h"
  67. #include "asterisk/causes.h"
  68. #include "asterisk/indications.h"
  69. /*! Beware, G729 and G723 are not supported by asterisk, except with the proper licence */
  70. #define CAPABILITY AST_FORMAT_ALAW | AST_FORMAT_ULAW /* | AST_FORMAT_G729A | AST_FORMAT_G723_1 */
  71. #define DEFAULTCONTEXT "default"
  72. #define DEFAULTCALLERID "Unknown"
  73. #define DEFAULTCALLERNAME " "
  74. #define DEFAULTHEIGHT 3
  75. #define USTM_LOG_DIR "unistimHistory"
  76. /*! Size of the transmit buffer */
  77. #define MAX_BUF_SIZE 64
  78. /*! Number of slots for the transmit queue */
  79. #define MAX_BUF_NUMBER 50
  80. /*! Try x times before removing the phone */
  81. #define NB_MAX_RETRANSMIT 8
  82. /*! Nb of milliseconds waited when no events are scheduled */
  83. #define IDLE_WAIT 1000
  84. /*! Wait x milliseconds before resending a packet */
  85. #define RETRANSMIT_TIMER 2000
  86. /*! How often the mailbox is checked for new messages */
  87. #define TIMER_MWI 10000
  88. /*! Not used */
  89. #define DEFAULT_CODEC 0x00
  90. #define SIZE_PAGE 4096
  91. #define DEVICE_NAME_LEN 16
  92. #define AST_CONFIG_MAX_PATH 255
  93. #define MAX_ENTRY_LOG 30
  94. #define SUB_REAL 0
  95. #define SUB_THREEWAY 1
  96. #define MAX_SUBS 2
  97. enum autoprovision {
  98. AUTOPROVISIONING_NO = 0,
  99. AUTOPROVISIONING_YES,
  100. AUTOPROVISIONING_DB,
  101. AUTOPROVISIONING_TN
  102. };
  103. enum autoprov_extn {
  104. /*! Do not create an extension into the default dialplan */
  105. EXTENSION_NONE = 0,
  106. /*! Prompt user for an extension number and register it */
  107. EXTENSION_ASK,
  108. /*! Register an extension with the line=> value */
  109. EXTENSION_LINE,
  110. /*! Used with AUTOPROVISIONING_TN */
  111. EXTENSION_TN
  112. };
  113. #define OUTPUT_HANDSET 0xC0
  114. #define OUTPUT_HEADPHONE 0xC1
  115. #define OUTPUT_SPEAKER 0xC2
  116. #define VOLUME_LOW 0x01
  117. #define VOLUME_LOW_SPEAKER 0x03
  118. #define VOLUME_NORMAL 0x02
  119. #define VOLUME_INSANELY_LOUD 0x07
  120. #define MUTE_OFF 0x00
  121. #define MUTE_ON 0xFF
  122. #define MUTE_ON_DISCRET 0xCE
  123. #define SIZE_HEADER 6
  124. #define SIZE_MAC_ADDR 17
  125. #define TEXT_LENGTH_MAX 24
  126. #define TEXT_LINE0 0x00
  127. #define TEXT_LINE1 0x20
  128. #define TEXT_LINE2 0x40
  129. #define TEXT_NORMAL 0x05
  130. #define TEXT_INVERSE 0x25
  131. #define STATUS_LENGTH_MAX 28
  132. #define FAV_ICON_NONE 0x00
  133. #define FAV_ICON_ONHOOK_BLACK 0x20
  134. #define FAV_ICON_ONHOOK_WHITE 0x21
  135. #define FAV_ICON_SPEAKER_ONHOOK_BLACK 0x22
  136. #define FAV_ICON_SPEAKER_ONHOOK_WHITE 0x23
  137. #define FAV_ICON_OFFHOOK_BLACK 0x24
  138. #define FAV_ICON_OFFHOOK_WHITE 0x25
  139. #define FAV_ICON_ONHOLD_BLACK 0x26
  140. #define FAV_ICON_ONHOLD_WHITE 0x27
  141. #define FAV_ICON_SPEAKER_OFFHOOK_BLACK 0x28
  142. #define FAV_ICON_SPEAKER_OFFHOOK_WHITE 0x29
  143. #define FAV_ICON_PHONE_BLACK 0x2A
  144. #define FAV_ICON_PHONE_WHITE 0x2B
  145. #define FAV_ICON_SPEAKER_ONHOLD_BLACK 0x2C
  146. #define FAV_ICON_SPEAKER_ONHOLD_WHITE 0x2D
  147. #define FAV_ICON_HEADPHONES 0x2E
  148. #define FAV_ICON_HEADPHONES_ONHOLD 0x2F
  149. #define FAV_ICON_HOME 0x30
  150. #define FAV_ICON_CITY 0x31
  151. #define FAV_ICON_SHARP 0x32
  152. #define FAV_ICON_PAGER 0x33
  153. #define FAV_ICON_CALL_CENTER 0x34
  154. #define FAV_ICON_FAX 0x35
  155. #define FAV_ICON_MAILBOX 0x36
  156. #define FAV_ICON_REFLECT 0x37
  157. #define FAV_ICON_COMPUTER 0x38
  158. #define FAV_ICON_FORWARD 0x39
  159. #define FAV_ICON_LOCKED 0x3A
  160. #define FAV_ICON_TRASH 0x3B
  161. #define FAV_ICON_INBOX 0x3C
  162. #define FAV_ICON_OUTBOX 0x3D
  163. #define FAV_ICON_MEETING 0x3E
  164. #define FAV_ICON_BOX 0x3F
  165. #define FAV_BLINK_FAST 0x20
  166. #define FAV_BLINK_SLOW 0x40
  167. #define FAV_MAX_LENGTH 0x0A
  168. static void dummy(char *unused, ...)
  169. {
  170. return;
  171. }
  172. /*! \brief Global jitterbuffer configuration - by default, jb is disabled
  173. * \note Values shown here match the defaults shown in unistim.conf.sample */
  174. static struct ast_jb_conf default_jbconf =
  175. {
  176. .flags = 0,
  177. .max_size = 200,
  178. .resync_threshold = 1000,
  179. .impl = "fixed",
  180. .target_extra = 40,
  181. };
  182. static struct ast_jb_conf global_jbconf;
  183. /* #define DUMP_PACKET 1 */
  184. /* #define DEBUG_TIMER ast_verbose */
  185. #define DEBUG_TIMER dummy
  186. /*! Enable verbose output. can also be set with the CLI */
  187. static int unistimdebug = 0;
  188. static int unistim_port;
  189. static enum autoprovision autoprovisioning = AUTOPROVISIONING_NO;
  190. static int unistim_keepalive;
  191. static int unistimsock = -1;
  192. static struct {
  193. unsigned int tos;
  194. unsigned int tos_audio;
  195. unsigned int cos;
  196. unsigned int cos_audio;
  197. } qos = { 0, 0, 0, 0 };
  198. static struct io_context *io;
  199. static struct sched_context *sched;
  200. static struct sockaddr_in public_ip = { 0, };
  201. /*! give the IP address for the last packet received */
  202. static struct sockaddr_in address_from;
  203. /*! size of the sockaddr_in (in WSARecvFrom) */
  204. static unsigned int size_addr_from = sizeof(address_from);
  205. /*! Receive buffer address */
  206. static unsigned char *buff;
  207. static int unistim_reloading = 0;
  208. AST_MUTEX_DEFINE_STATIC(unistim_reload_lock);
  209. AST_MUTEX_DEFINE_STATIC(usecnt_lock);
  210. static int usecnt = 0;
  211. /* extern char ast_config_AST_LOG_DIR[AST_CONFIG_MAX_PATH]; */
  212. /*! This is the thread for the monitor which checks for input on the channels
  213. * which are not currently in use. */
  214. static pthread_t monitor_thread = AST_PTHREADT_NULL;
  215. /*! Protect the monitoring thread, so only one process can kill or start it, and not
  216. * when it's doing something critical. */
  217. AST_MUTEX_DEFINE_STATIC(monlock);
  218. /*! Protect the session list */
  219. AST_MUTEX_DEFINE_STATIC(sessionlock);
  220. /*! Protect the device list */
  221. AST_MUTEX_DEFINE_STATIC(devicelock);
  222. enum phone_state {
  223. STATE_INIT,
  224. STATE_AUTHDENY,
  225. STATE_MAINPAGE,
  226. STATE_EXTENSION,
  227. STATE_DIALPAGE,
  228. STATE_RINGING,
  229. STATE_CALL,
  230. STATE_SELECTCODEC,
  231. STATE_CLEANING,
  232. STATE_HISTORY
  233. };
  234. enum handset_state {
  235. STATE_ONHOOK,
  236. STATE_OFFHOOK,
  237. };
  238. enum phone_key {
  239. KEY_0 = 0x40,
  240. KEY_1 = 0x41,
  241. KEY_2 = 0x42,
  242. KEY_3 = 0x43,
  243. KEY_4 = 0x44,
  244. KEY_5 = 0x45,
  245. KEY_6 = 0x46,
  246. KEY_7 = 0x47,
  247. KEY_8 = 0x48,
  248. KEY_9 = 0x49,
  249. KEY_STAR = 0x4a,
  250. KEY_SHARP = 0x4b,
  251. KEY_UP = 0x4c,
  252. KEY_DOWN = 0x4d,
  253. KEY_RIGHT = 0x4e,
  254. KEY_LEFT = 0x4f,
  255. KEY_QUIT = 0x50,
  256. KEY_COPY = 0x51,
  257. KEY_FUNC1 = 0x54,
  258. KEY_FUNC2 = 0x55,
  259. KEY_FUNC3 = 0x56,
  260. KEY_FUNC4 = 0x57,
  261. KEY_ONHOLD = 0x5b,
  262. KEY_HANGUP = 0x5c,
  263. KEY_MUTE = 0x5d,
  264. KEY_HEADPHN = 0x5e,
  265. KEY_LOUDSPK = 0x5f,
  266. KEY_FAV0 = 0x60,
  267. KEY_FAV1 = 0x61,
  268. KEY_FAV2 = 0x62,
  269. KEY_FAV3 = 0x63,
  270. KEY_FAV4 = 0x64,
  271. KEY_FAV5 = 0x65,
  272. KEY_COMPUTR = 0x7b,
  273. KEY_CONF = 0x7c,
  274. KEY_SNDHIST = 0x7d,
  275. KEY_RCVHIST = 0x7e,
  276. KEY_INDEX = 0x7f
  277. };
  278. struct tone_zone_unistim {
  279. char country[3];
  280. int freq1;
  281. int freq2;
  282. };
  283. static const struct tone_zone_unistim frequency[] = {
  284. {"us", 350, 440},
  285. {"fr", 440, 0},
  286. {"au", 413, 438},
  287. {"nl", 425, 0},
  288. {"uk", 350, 440},
  289. {"fi", 425, 0},
  290. {"es", 425, 0},
  291. {"jp", 400, 0},
  292. {"no", 425, 0},
  293. {"at", 420, 0},
  294. {"nz", 400, 0},
  295. {"tw", 350, 440},
  296. {"cl", 400, 0},
  297. {"se", 425, 0},
  298. {"be", 425, 0},
  299. {"sg", 425, 0},
  300. {"il", 414, 0},
  301. {"br", 425, 0},
  302. {"hu", 425, 0},
  303. {"lt", 425, 0},
  304. {"pl", 425, 0},
  305. {"za", 400, 0},
  306. {"pt", 425, 0},
  307. {"ee", 425, 0},
  308. {"mx", 425, 0},
  309. {"in", 400, 0},
  310. {"de", 425, 0},
  311. {"ch", 425, 0},
  312. {"dk", 425, 0},
  313. {"cn", 450, 0},
  314. {"--", 0, 0}
  315. };
  316. struct wsabuf {
  317. u_long len;
  318. unsigned char *buf;
  319. };
  320. struct systemtime {
  321. unsigned short w_year;
  322. unsigned short w_month;
  323. unsigned short w_day_of_week;
  324. unsigned short w_day;
  325. unsigned short w_hour;
  326. unsigned short w_minute;
  327. unsigned short w_second;
  328. unsigned short w_milliseconds;
  329. };
  330. struct unistim_subchannel {
  331. ast_mutex_t lock;
  332. /*! SUBS_REAL or SUBS_THREEWAY */
  333. unsigned int subtype;
  334. /*! Asterisk channel used by the subchannel */
  335. struct ast_channel *owner;
  336. /*! Unistim line */
  337. struct unistim_line *parent;
  338. /*! RTP handle */
  339. struct ast_rtp_instance *rtp;
  340. int alreadygone;
  341. char ringvolume;
  342. char ringstyle;
  343. };
  344. /*!
  345. * \todo Convert to stringfields
  346. */
  347. struct unistim_line {
  348. ast_mutex_t lock;
  349. /*! Like 200 */
  350. char name[80];
  351. /*! Like USTM/200\@black */
  352. char fullname[80];
  353. /*! pointer to our current connection, channel... */
  354. struct unistim_subchannel *subs[MAX_SUBS];
  355. /*! Extension where to start */
  356. char exten[AST_MAX_EXTENSION];
  357. /*! Context to start in */
  358. char context[AST_MAX_EXTENSION];
  359. /*! Language for asterisk sounds */
  360. char language[MAX_LANGUAGE];
  361. /*! CallerID Number */
  362. char cid_num[AST_MAX_EXTENSION];
  363. /*! Mailbox for MWI */
  364. char mailbox[AST_MAX_EXTENSION];
  365. /*! Used by MWI */
  366. int lastmsgssent;
  367. /*! Used by MWI */
  368. time_t nextmsgcheck;
  369. /*! MusicOnHold class */
  370. char musicclass[MAX_MUSICCLASS];
  371. /*! Call group */
  372. unsigned int callgroup;
  373. /*! Pickup group */
  374. unsigned int pickupgroup;
  375. /*! Account code (for billing) */
  376. char accountcode[80];
  377. /*! AMA flags (for billing) */
  378. int amaflags;
  379. /*! Codec supported */
  380. format_t capability;
  381. /*! Parkinglot */
  382. char parkinglot[AST_MAX_CONTEXT];
  383. struct unistim_line *next;
  384. struct unistim_device *parent;
  385. };
  386. /*!
  387. * \brief A device containing one or more lines
  388. */
  389. static struct unistim_device {
  390. int receiver_state; /*!< state of the receiver (see ReceiverState) */
  391. int size_phone_number; /*!< size of the phone number */
  392. char phone_number[16]; /*!< the phone number entered by the user */
  393. char redial_number[16]; /*!< the last phone number entered by the user */
  394. int phone_current; /*!< Number of the current phone */
  395. int pos_fav; /*!< Position of the displayed favorites (used for scrolling) */
  396. char id[18]; /*!< mac address of the current phone in ascii */
  397. char name[DEVICE_NAME_LEN]; /*!< name of the device */
  398. int softkeylinepos; /*!< position of the line softkey (default 0) */
  399. char softkeylabel[6][11]; /*!< soft key label */
  400. char softkeynumber[6][16]; /*!< number dialed when the soft key is pressed */
  401. char softkeyicon[6]; /*!< icon number */
  402. char softkeydevice[6][16]; /*!< name of the device monitored */
  403. struct unistim_device *sp[6]; /*!< pointer to the device monitored by this soft key */
  404. int height; /*!< The number of lines the phone can display */
  405. char maintext0[25]; /*!< when the phone is idle, display this string on line 0 */
  406. char maintext1[25]; /*!< when the phone is idle, display this string on line 1 */
  407. char maintext2[25]; /*!< when the phone is idle, display this string on line 2 */
  408. char titledefault[13]; /*!< title (text before date/time) */
  409. char datetimeformat; /*!< format used for displaying time/date */
  410. char contrast; /*!< contrast */
  411. char country[3]; /*!< country used for dial tone frequency */
  412. struct ast_tone_zone *tz; /*!< Tone zone for res_indications (ring, busy, congestion) */
  413. char ringvolume; /*!< Ring volume */
  414. char ringstyle; /*!< Ring melody */
  415. int rtp_port; /*!< RTP port used by the phone */
  416. int rtp_method; /*!< Select the unistim data used to establish a RTP session */
  417. int status_method; /*!< Select the unistim packet used for sending status text */
  418. char codec_number; /*!< The current codec used to make calls */
  419. int missed_call; /*!< Number of call unanswered */
  420. int callhistory; /*!< Allowed to record call history */
  421. char lst_cid[TEXT_LENGTH_MAX]; /*!< Last callerID received */
  422. char lst_cnm[TEXT_LENGTH_MAX]; /*!< Last callername recevied */
  423. char call_forward[AST_MAX_EXTENSION]; /*!< Forward number */
  424. int output; /*!< Handset, headphone or speaker */
  425. int previous_output; /*!< Previous output */
  426. int volume; /*!< Default volume */
  427. int mute; /*!< Mute mode */
  428. int moh; /*!< Music on hold in progress */
  429. int nat; /*!< Used by the obscure ast_rtp_setnat */
  430. enum autoprov_extn extension; /*!< See ifdef EXTENSION for valid values */
  431. char extension_number[11]; /*!< Extension number entered by the user */
  432. char to_delete; /*!< Used in reload */
  433. time_t start_call_timestamp; /*!< timestamp for the length calculation of the call */
  434. struct ast_silence_generator *silence_generator;
  435. struct unistim_line *lines;
  436. struct ast_ha *ha;
  437. struct unistimsession *session;
  438. struct unistim_device *next;
  439. } *devices = NULL;
  440. static struct unistimsession {
  441. ast_mutex_t lock;
  442. struct sockaddr_in sin; /*!< IP address of the phone */
  443. struct sockaddr_in sout; /*!< IP address of server */
  444. int timeout; /*!< time-out in ticks : resend packet if no ack was received before the timeout occured */
  445. unsigned short seq_phone; /*!< sequence number for the next packet (when we receive a request) */
  446. unsigned short seq_server; /*!< sequence number for the next packet (when we send a request) */
  447. unsigned short last_seq_ack; /*!< sequence number of the last ACK received */
  448. unsigned long tick_next_ping; /*!< time for the next ping */
  449. int last_buf_available; /*!< number of a free slot */
  450. int nb_retransmit; /*!< number of retransmition */
  451. int state; /*!< state of the phone (see phone_state) */
  452. int size_buff_entry; /*!< size of the buffer used to enter datas */
  453. char buff_entry[16]; /*!< Buffer for temporary datas */
  454. char macaddr[18]; /*!< mac adress of the phone (not always available) */
  455. struct wsabuf wsabufsend[MAX_BUF_NUMBER]; /*!< Size of each paquet stored in the buffer array & pointer to this buffer */
  456. unsigned char buf[MAX_BUF_NUMBER][MAX_BUF_SIZE]; /*!< Buffer array used to keep the lastest non-acked paquets */
  457. struct unistim_device *device;
  458. struct unistimsession *next;
  459. } *sessions = NULL;
  460. /*!
  461. * \page Unistim datagram formats
  462. *
  463. * Format of datagrams :
  464. * bytes 0 & 1 : ffff for discovery packet, 0000 for everything else
  465. * byte 2 : sequence number (high part)
  466. * byte 3 : sequence number (low part)
  467. * byte 4 : 2 = ask question or send info, 1 = answer or ACK, 0 = retransmit request
  468. * byte 5 : direction, 1 = server to phone, 2 = phone to server arguments
  469. */
  470. static const unsigned char packet_rcv_discovery[] =
  471. { 0xff, 0xff, 0xff, 0xff, 0x02, 0x02, 0xff, 0xff, 0xff, 0xff, 0x9e, 0x03, 0x08 };
  472. static const unsigned char packet_send_discovery_ack[] =
  473. { 0x00, 0x00, /*Initial Seq (2 bytes) */ 0x00, 0x00, 0x00, 0x01 };
  474. static const unsigned char packet_recv_firm_version[] =
  475. { 0x00, 0x00, 0x00, 0x13, 0x9a, 0x0a, 0x02 };
  476. static const unsigned char packet_recv_pressed_key[] =
  477. { 0x00, 0x00, 0x00, 0x13, 0x99, 0x04, 0x00 };
  478. static const unsigned char packet_recv_pick_up[] =
  479. { 0x00, 0x00, 0x00, 0x13, 0x99, 0x03, 0x04 };
  480. static const unsigned char packet_recv_hangup[] =
  481. { 0x00, 0x00, 0x00, 0x13, 0x99, 0x03, 0x03 };
  482. static const unsigned char packet_recv_r2[] = { 0x00, 0x00, 0x00, 0x13, 0x96, 0x03, 0x03 };
  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. static const unsigned char packet_send_CloseAudioStreamRX[] = { 0x16, 0x05, 0x31, 0x00, 0xff };
  520. static const unsigned char packet_send_CloseAudioStreamTX[] = { 0x16, 0x05, 0x31, 0xff, 0x00 };*/
  521. static const unsigned char packet_send_stream_based_tone_on[] =
  522. { 0x16, 0x06, 0x1b, 0x00, 0x00, 0x05 };
  523. static const unsigned char packet_send_stream_based_tone_single_freq[] =
  524. { 0x16, 0x06, 0x1d, 0x00, 0x01, 0xb8 };
  525. static const unsigned char packet_send_stream_based_tone_dial_freq[] =
  526. { 0x16, 0x08, 0x1d, 0x00, 0x01, 0xb8, 0x01, 0x5e };
  527. static const unsigned char packet_send_select_output[] =
  528. { 0x16, 0x06, 0x32, 0xc0, 0x01, 0x00 };
  529. static const unsigned char packet_send_ring[] =
  530. { 0x16, 0x06, 0x32, 0xdf, 0x00, 0xff, 0x16, 0x05, 0x1c, 0x00, 0x00, 0x16,
  531. 0x04, 0x1a, 0x01, 0x16, 0x05, 0x12, 0x13 /* Ring type 10 to 17 */ , 0x18, 0x16, 0x04, 0x18, /* volume 00, 10, 20... */
  532. 0x20, 0x16, 0x04, 0x10, 0x00
  533. };
  534. static const unsigned char packet_send_end_call[] =
  535. { 0x16, 0x06, 0x32, 0xdf, 0x00, 0xff, 0x16, 0x05, 0x31, 0x00, 0x00, 0x19, 0x04, 0x00,
  536. 0x10, 0x19, 0x04, 0x00, 0x18, 0x16, 0x05,
  537. 0x04, 0x00, 0x00, 0x16, 0x04, 0x37, 0x10
  538. };
  539. static const unsigned char packet_send_s9[] =
  540. { 0x16, 0x06, 0x32, 0xdf, 0x00, 0xff, 0x19, 0x04, 0x00, 0x10, 0x16, 0x05, 0x1c, 0x00,
  541. 0x00 };
  542. static const unsigned char packet_send_rtp_packet_size[] =
  543. { 0x16, 0x08, 0x38, 0x00, 0x00, 0xe0, 0x00, 0xa0 };
  544. static const unsigned char packet_send_jitter_buffer_conf[] =
  545. { 0x16, 0x0e, 0x3a, 0x00, /* jitter */ 0x02, /* high water mark */ 0x04, 0x00, 0x00,
  546. /* early packet resync 2 bytes */ 0x3e, 0x80,
  547. 0x00, 0x00, /* late packet resync 2 bytes */ 0x3e, 0x80
  548. };
  549. /* Duration in ms div 2 (0x20 = 64ms, 0x08 = 16ms)
  550. static unsigned char packet_send_StreamBasedToneCad[] =
  551. { 0x16, 0x0a, 0x1e, 0x00, duration on 0x0a, duration off 0x0d, duration on 0x0a, duration off 0x0d, duration on 0x0a, duration off 0x2b }; */
  552. static const unsigned char packet_send_open_audio_stream_rx[] =
  553. { 0x16, 0x1a, 0x30, 0x00, 0xff, /* Codec */ 0x00, 0x00, 0x01, 0x00, 0xb8, 0xb8, 0x0e,
  554. 0x0e, 0x01, /* Port */ 0x14, 0x50, 0x00,
  555. 0x00, /* Port */ 0x14, 0x50, 0x00, 0x00, /* Dest IP */ 0x0a, 0x93, 0x69, 0x05
  556. };
  557. static const unsigned char packet_send_open_audio_stream_tx[] =
  558. { 0x16, 0x1a, 0x30, 0xff, 0x00, 0x00, /* Codec */ 0x00, 0x01, 0x00, 0xb8, 0xb8, 0x0e,
  559. 0x0e, 0x01, /* Local port */ 0x14, 0x50,
  560. 0x00, 0x00, /* Rmt Port */ 0x14, 0x50, 0x00, 0x00, /* Dest IP */ 0x0a, 0x93, 0x69, 0x05
  561. };
  562. static const unsigned char packet_send_open_audio_stream_rx3[] =
  563. { 0x16, 0x1a, 0x30, 0x00, 0xff, /* Codec */ 0x00, 0x00, 0x02, 0x01, 0xb8, 0xb8, 0x06,
  564. 0x06, 0x81, /* RTP Port */ 0x14, 0x50,
  565. /* RTCP Port */ 0x14,
  566. 0x51, /* RTP Port */ 0x14, 0x50, /* RTCP Port */ 0x00, 0x00, /* Dest IP */ 0x0a, 0x93,
  567. 0x69, 0x05
  568. };
  569. static const unsigned char packet_send_open_audio_stream_tx3[] =
  570. { 0x16, 0x1a, 0x30, 0xff, 0x00, 0x00, /* Codec */ 0x00, 0x02, 0x01, 0xb8, 0xb8, 0x06,
  571. 0x06, 0x81, /* RTP Local port */ 0x14, 0x50,
  572. /* RTCP Port */ 0x00, 0x00, /* RTP Rmt Port */ 0x14, 0x50, /* RTCP Port */ 0x00, 0x00,
  573. /* Dest IP */ 0x0a, 0x93, 0x69, 0x05
  574. };
  575. static const unsigned char packet_send_arrow[] = { 0x17, 0x04, 0x04, 0x00 };
  576. static const unsigned char packet_send_blink_cursor[] = { 0x17, 0x04, 0x10, 0x86 };
  577. static const unsigned char packet_send_date_time2[] = { 0x17, 0x04, 0x17, 0x3d, 0x11, 0x09, 0x02, 0x0a, /*Month */ 0x05, /*Day */
  578. 0x06, /*Hour */ 0x07, /*Minutes */ 0x08, 0x32
  579. };
  580. static const unsigned char packet_send_Contrast[] =
  581. { 0x17, 0x04, 0x24, /*Contrast */ 0x08 };
  582. static const unsigned char packet_send_StartTimer[] =
  583. { 0x17, 0x05, 0x0b, 0x05, 0x00, 0x17, 0x08, 0x16, /* Text */ 0x44, 0x75, 0x72, 0xe9,
  584. 0x65 };
  585. static const unsigned char packet_send_stop_timer[] = { 0x17, 0x05, 0x0b, 0x02, 0x00 };
  586. static const unsigned char packet_send_icon[] = { 0x17, 0x05, 0x14, /*pos */ 0x00, /*icon */ 0x25 }; /* display an icon in front of the text zone */
  587. static const unsigned char packet_send_S7[] = { 0x17, 0x06, 0x0f, 0x30, 0x07, 0x07 };
  588. static const unsigned char packet_send_set_pos_cursor[] =
  589. { 0x17, 0x06, 0x10, 0x81, 0x04, /*pos */ 0x20 };
  590. /*static unsigned char packet_send_MonthLabelsDownload[] =
  591. { 0x17, 0x0a, 0x15, Month (3 char) 0x46, 0x65, 0x62, 0x4d, 0xe4, 0x72, 0x20 }; */
  592. static const unsigned char packet_send_favorite[] =
  593. { 0x17, 0x0f, 0x19, 0x10, /*pos */ 0x01, /*name */ 0x20, 0x20, 0x20, 0x20, 0x20, 0x20,
  594. 0x20, 0x20, 0x20, 0x20, /*end_name */ 0x19,
  595. 0x05, 0x0f, /*pos */ 0x01, /*icone */ 0x00
  596. };
  597. static const unsigned char packet_send_title[] =
  598. { 0x17, 0x10, 0x19, 0x02, /*text */ 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20,
  599. 0x20, 0x20, 0x20, 0x20 /*end_text */ };
  600. static const unsigned char packet_send_text[] =
  601. { 0x17, 0x1e, 0x1b, 0x04, /*pos */ 0x00, /*inverse */ 0x25, /*text */ 0x20, 0x20,
  602. 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20,
  603. 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20,
  604. /*end_text */ 0x17, 0x04, 0x10, 0x87
  605. };
  606. static const unsigned char packet_send_status[] =
  607. { 0x17, 0x20, 0x19, 0x08, /*text */ 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20,
  608. 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20,
  609. 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20 /*end_text */
  610. };
  611. static const unsigned char packet_send_status2[] =
  612. { 0x17, 0x0b, 0x19, /* pos [08|28|48|68] */ 0x00, /* text */ 0x20, 0x20, 0x20, 0x20,
  613. 0x20, 0x20, 0x20 /* end_text */ };
  614. static const unsigned char packet_send_led_update[] = { 0x19, 0x04, 0x00, 0x00 };
  615. static const unsigned char packet_send_query_basic_manager_04[] = { 0x1a, 0x04, 0x01, 0x04 };
  616. static const unsigned char packet_send_query_mac_address[] = { 0x1a, 0x04, 0x01, 0x08 };
  617. static const unsigned char packet_send_query_basic_manager_10[] = { 0x1a, 0x04, 0x01, 0x10 };
  618. static const unsigned char packet_send_S1[] = { 0x1a, 0x07, 0x07, 0x00, 0x00, 0x00, 0x13 };
  619. static unsigned char packet_send_ping[] =
  620. { 0x1e, 0x05, 0x12, 0x00, /*Watchdog timer */ 0x78 };
  621. #define BUFFSEND unsigned char buffsend[64] = { 0x00, 0x00, 0xaa, 0xbb, 0x02, 0x01 }
  622. static const char tdesc[] = "UNISTIM Channel Driver";
  623. static const char channel_type[] = "USTM";
  624. /*! Protos */
  625. static struct ast_channel *unistim_new(struct unistim_subchannel *sub, int state, const char *linkedid);
  626. static int load_module(void);
  627. static int reload(void);
  628. static int unload_module(void);
  629. static int reload_config(void);
  630. static void show_main_page(struct unistimsession *pte);
  631. static struct ast_channel *unistim_request(const char *type, format_t format, const struct ast_channel *requestor,
  632. void *data, int *cause);
  633. static int unistim_call(struct ast_channel *ast, char *dest, int timeout);
  634. static int unistim_hangup(struct ast_channel *ast);
  635. static int unistim_answer(struct ast_channel *ast);
  636. static struct ast_frame *unistim_read(struct ast_channel *ast);
  637. static int unistim_write(struct ast_channel *ast, struct ast_frame *frame);
  638. static int unistim_indicate(struct ast_channel *ast, int ind, const void *data,
  639. size_t datalen);
  640. static int unistim_fixup(struct ast_channel *oldchan, struct ast_channel *newchan);
  641. static int unistim_senddigit_begin(struct ast_channel *ast, char digit);
  642. static int unistim_senddigit_end(struct ast_channel *ast, char digit,
  643. unsigned int duration);
  644. static int unistim_sendtext(struct ast_channel *ast, const char *text);
  645. static int write_entry_history(struct unistimsession *pte, FILE * f, char c,
  646. char *line1);
  647. static void change_callerid(struct unistimsession *pte, int type, char *callerid);
  648. static const struct ast_channel_tech unistim_tech = {
  649. .type = channel_type,
  650. .description = tdesc,
  651. .capabilities = CAPABILITY,
  652. .properties = AST_CHAN_TP_WANTSJITTER | AST_CHAN_TP_CREATESJITTER,
  653. .requester = unistim_request,
  654. .call = unistim_call,
  655. .hangup = unistim_hangup,
  656. .answer = unistim_answer,
  657. .read = unistim_read,
  658. .write = unistim_write,
  659. .indicate = unistim_indicate,
  660. .fixup = unistim_fixup,
  661. .send_digit_begin = unistim_senddigit_begin,
  662. .send_digit_end = unistim_senddigit_end,
  663. .send_text = unistim_sendtext,
  664. .bridge = ast_rtp_instance_bridge,
  665. };
  666. static void display_last_error(const char *sz_msg)
  667. {
  668. time_t cur_time;
  669. time(&cur_time);
  670. /* Display the error message */
  671. ast_log(LOG_WARNING, "%s %s : (%d) %s\n", ctime(&cur_time), sz_msg, errno,
  672. strerror(errno));
  673. }
  674. static unsigned int get_tick_count(void)
  675. {
  676. struct timeval now = ast_tvnow();
  677. return (now.tv_sec * 1000) + (now.tv_usec / 1000);
  678. }
  679. /* Send data to a phone without retransmit nor buffering */
  680. static void send_raw_client(int size, const unsigned char *data, struct sockaddr_in *addr_to,
  681. const struct sockaddr_in *addr_ourip)
  682. {
  683. #ifdef HAVE_PKTINFO
  684. struct iovec msg_iov;
  685. struct msghdr msg;
  686. char buffer[CMSG_SPACE(sizeof(struct in_pktinfo))];
  687. struct cmsghdr *ip_msg = (struct cmsghdr *) buffer;
  688. struct in_pktinfo *pki = (struct in_pktinfo *) CMSG_DATA(ip_msg);
  689. /* cast this to a non-const pointer, since the sendmsg() API
  690. * does not provide read-only and write-only flavors of the
  691. * structures used for its arguments, but in this case we know
  692. * the data will not be modified
  693. */
  694. msg_iov.iov_base = (char *) data;
  695. msg_iov.iov_len = size;
  696. msg.msg_name = addr_to; /* optional address */
  697. msg.msg_namelen = sizeof(struct sockaddr_in); /* size of address */
  698. msg.msg_iov = &msg_iov; /* scatter/gather array */
  699. msg.msg_iovlen = 1; /* # elements in msg_iov */
  700. msg.msg_control = ip_msg; /* ancillary data */
  701. msg.msg_controllen = sizeof(buffer); /* ancillary data buffer len */
  702. msg.msg_flags = 0; /* flags on received message */
  703. ip_msg->cmsg_len = CMSG_LEN(sizeof(*pki));
  704. ip_msg->cmsg_level = IPPROTO_IP;
  705. ip_msg->cmsg_type = IP_PKTINFO;
  706. pki->ipi_ifindex = 0; /* Interface index, 0 = use interface specified in routing table */
  707. pki->ipi_spec_dst.s_addr = addr_ourip->sin_addr.s_addr; /* Local address */
  708. /* pki->ipi_addr = ; Header Destination address - ignored by kernel */
  709. #ifdef DUMP_PACKET
  710. if (unistimdebug) {
  711. int tmp;
  712. char iabuf[INET_ADDRSTRLEN];
  713. char iabuf2[INET_ADDRSTRLEN];
  714. ast_verb(0, "\n**> From %s sending %d bytes to %s ***\n",
  715. ast_inet_ntoa(addr_ourip->sin_addr), (int) size,
  716. ast_inet_ntoa(addr_to->sin_addr));
  717. for (tmp = 0; tmp < size; tmp++)
  718. ast_verb(0, "%.2x ", (unsigned char) data[tmp]);
  719. ast_verb(0, "\n******************************************\n");
  720. }
  721. #endif
  722. if (sendmsg(unistimsock, &msg, 0) == -1)
  723. display_last_error("Error sending datas");
  724. #else
  725. if (sendto(unistimsock, data, size, 0, (struct sockaddr *) addr_to, sizeof(*addr_to))
  726. == -1)
  727. display_last_error("Error sending datas");
  728. #endif
  729. }
  730. static void send_client(int size, const unsigned char *data, struct unistimsession *pte)
  731. {
  732. unsigned int tick;
  733. int buf_pos;
  734. unsigned short seq = ntohs(++pte->seq_server);
  735. ast_mutex_lock(&pte->lock);
  736. buf_pos = pte->last_buf_available;
  737. if (buf_pos >= MAX_BUF_NUMBER) {
  738. ast_log(LOG_WARNING, "Error : send queue overflow\n");
  739. ast_mutex_unlock(&pte->lock);
  740. return;
  741. }
  742. memcpy((void *)data + sizeof(unsigned short), (void *)&seq, sizeof(unsigned short));
  743. pte->wsabufsend[buf_pos].len = size;
  744. memcpy(pte->wsabufsend[buf_pos].buf, data, size);
  745. tick = get_tick_count();
  746. pte->timeout = tick + RETRANSMIT_TIMER;
  747. /*#ifdef DUMP_PACKET */
  748. if (unistimdebug)
  749. ast_verb(6, "Sending datas with seq #0x%.4x Using slot #%d :\n", (unsigned)pte->seq_server, buf_pos);
  750. /*#endif */
  751. send_raw_client(pte->wsabufsend[buf_pos].len, pte->wsabufsend[buf_pos].buf, &(pte->sin),
  752. &(pte->sout));
  753. pte->last_buf_available++;
  754. ast_mutex_unlock(&pte->lock);
  755. }
  756. static void send_ping(struct unistimsession *pte)
  757. {
  758. BUFFSEND;
  759. if (unistimdebug)
  760. ast_verb(6, "Sending ping\n");
  761. pte->tick_next_ping = get_tick_count() + unistim_keepalive;
  762. memcpy(buffsend + SIZE_HEADER, packet_send_ping, sizeof(packet_send_ping));
  763. send_client(SIZE_HEADER + sizeof(packet_send_ping), buffsend, pte);
  764. }
  765. static int get_to_address(int fd, struct sockaddr_in *toAddr)
  766. {
  767. #ifdef HAVE_PKTINFO
  768. int err;
  769. struct msghdr msg;
  770. struct {
  771. struct cmsghdr cm;
  772. int len;
  773. struct in_addr address;
  774. } ip_msg;
  775. /* Zero out the structures before we use them */
  776. /* This sets several key values to NULL */
  777. memset(&msg, 0, sizeof(msg));
  778. memset(&ip_msg, 0, sizeof(ip_msg));
  779. /* Initialize the message structure */
  780. msg.msg_control = &ip_msg;
  781. msg.msg_controllen = sizeof(ip_msg);
  782. /* Get info about the incoming packet */
  783. err = recvmsg(fd, &msg, MSG_PEEK);
  784. if (err == -1)
  785. ast_log(LOG_WARNING, "recvmsg returned an error: %s\n", strerror(errno));
  786. memcpy(&toAddr->sin_addr, &ip_msg.address, sizeof(struct in_addr));
  787. return err;
  788. #else
  789. memcpy(&toAddr, &public_ip, sizeof(&toAddr));
  790. return 0;
  791. #endif
  792. }
  793. /* Allocate memory & initialize structures for a new phone */
  794. /* addr_from : ip address of the phone */
  795. static struct unistimsession *create_client(const struct sockaddr_in *addr_from)
  796. {
  797. int tmp;
  798. struct unistimsession *s;
  799. if (!(s = ast_calloc(1, sizeof(*s))))
  800. return NULL;
  801. memcpy(&s->sin, addr_from, sizeof(struct sockaddr_in));
  802. get_to_address(unistimsock, &s->sout);
  803. s->sout.sin_family = AF_INET;
  804. if (unistimdebug) {
  805. ast_verb(0, "Creating a new entry for the phone from %s received via server ip %s\n",
  806. ast_inet_ntoa(addr_from->sin_addr), ast_inet_ntoa(s->sout.sin_addr));
  807. }
  808. ast_mutex_init(&s->lock);
  809. ast_mutex_lock(&sessionlock);
  810. s->next = sessions;
  811. sessions = s;
  812. s->timeout = get_tick_count() + RETRANSMIT_TIMER;
  813. s->seq_phone = (short) 0x0000;
  814. s->seq_server = (short) 0x0000;
  815. s->last_seq_ack = (short) 0x000;
  816. s->last_buf_available = 0;
  817. s->nb_retransmit = 0;
  818. s->state = STATE_INIT;
  819. s->tick_next_ping = get_tick_count() + unistim_keepalive;
  820. /* Initialize struct wsabuf */
  821. for (tmp = 0; tmp < MAX_BUF_NUMBER; tmp++) {
  822. s->wsabufsend[tmp].buf = s->buf[tmp];
  823. }
  824. ast_mutex_unlock(&sessionlock);
  825. return s;
  826. }
  827. static void send_end_call(struct unistimsession *pte)
  828. {
  829. BUFFSEND;
  830. if (unistimdebug)
  831. ast_verb(0, "Sending end call\n");
  832. memcpy(buffsend + SIZE_HEADER, packet_send_end_call, sizeof(packet_send_end_call));
  833. send_client(SIZE_HEADER + sizeof(packet_send_end_call), buffsend, pte);
  834. }
  835. static void set_ping_timer(struct unistimsession *pte)
  836. {
  837. unsigned int tick = 0; /* XXX what is this for, anyways */
  838. pte->timeout = pte->tick_next_ping;
  839. DEBUG_TIMER("tick = %u next ping at %u tick\n", tick, pte->timeout);
  840. return;
  841. }
  842. /* Checking if our send queue is empty,
  843. * if true, setting up a timer for keepalive */
  844. static void check_send_queue(struct unistimsession *pte)
  845. {
  846. /* Check if our send queue contained only one element */
  847. if (pte->last_buf_available == 1) {
  848. if (unistimdebug)
  849. ast_verb(6, "Our single packet was ACKed.\n");
  850. pte->last_buf_available--;
  851. set_ping_timer(pte);
  852. return;
  853. }
  854. /* Check if this ACK catch up our latest packet */
  855. else if (pte->last_seq_ack + 1 == pte->seq_server + 1) {
  856. if (unistimdebug)
  857. ast_verb(6, "Our send queue is completely ACKed.\n");
  858. pte->last_buf_available = 0; /* Purge the send queue */
  859. set_ping_timer(pte);
  860. return;
  861. }
  862. if (unistimdebug)
  863. ast_verb(6, "We still have packets in our send queue\n");
  864. return;
  865. }
  866. static void send_start_timer(struct unistimsession *pte)
  867. {
  868. BUFFSEND;
  869. if (unistimdebug)
  870. ast_verb(0, "Sending start timer\n");
  871. memcpy(buffsend + SIZE_HEADER, packet_send_StartTimer, sizeof(packet_send_StartTimer));
  872. send_client(SIZE_HEADER + sizeof(packet_send_StartTimer), buffsend, pte);
  873. }
  874. static void send_stop_timer(struct unistimsession *pte)
  875. {
  876. BUFFSEND;
  877. if (unistimdebug)
  878. ast_verb(0, "Sending stop timer\n");
  879. memcpy(buffsend + SIZE_HEADER, packet_send_stop_timer, sizeof(packet_send_stop_timer));
  880. send_client(SIZE_HEADER + sizeof(packet_send_stop_timer), buffsend, pte);
  881. }
  882. static void Sendicon(unsigned char pos, unsigned char status, struct unistimsession *pte)
  883. {
  884. BUFFSEND;
  885. if (unistimdebug)
  886. ast_verb(0, "Sending icon pos %d with status 0x%.2x\n", pos, (unsigned)status);
  887. memcpy(buffsend + SIZE_HEADER, packet_send_icon, sizeof(packet_send_icon));
  888. buffsend[9] = pos;
  889. buffsend[10] = status;
  890. send_client(SIZE_HEADER + sizeof(packet_send_icon), buffsend, pte);
  891. }
  892. static void send_tone(struct unistimsession *pte, uint16_t tone1, uint16_t tone2)
  893. {
  894. BUFFSEND;
  895. if (!tone1) {
  896. if (unistimdebug)
  897. ast_verb(0, "Sending Stream Based Tone Off\n");
  898. memcpy(buffsend + SIZE_HEADER, packet_send_stream_based_tone_off,
  899. sizeof(packet_send_stream_based_tone_off));
  900. send_client(SIZE_HEADER + sizeof(packet_send_stream_based_tone_off), buffsend, pte);
  901. return;
  902. }
  903. /* Since most of the world use a continuous tone, it's useless
  904. if (unistimdebug)
  905. ast_verb(0, "Sending Stream Based Tone Cadence Download\n");
  906. memcpy (buffsend + SIZE_HEADER, packet_send_StreamBasedToneCad, sizeof (packet_send_StreamBasedToneCad));
  907. send_client (SIZE_HEADER + sizeof (packet_send_StreamBasedToneCad), buffsend, pte); */
  908. if (unistimdebug)
  909. ast_verb(0, "Sending Stream Based Tone Frequency Component List Download %d %d\n", tone1, tone2);
  910. tone1 *= 8;
  911. if (!tone2) {
  912. memcpy(buffsend + SIZE_HEADER, packet_send_stream_based_tone_single_freq,
  913. sizeof(packet_send_stream_based_tone_single_freq));
  914. buffsend[10] = (tone1 & 0xff00) >> 8;
  915. buffsend[11] = (tone1 & 0x00ff);
  916. send_client(SIZE_HEADER + sizeof(packet_send_stream_based_tone_single_freq), buffsend,
  917. pte);
  918. } else {
  919. tone2 *= 8;
  920. memcpy(buffsend + SIZE_HEADER, packet_send_stream_based_tone_dial_freq,
  921. sizeof(packet_send_stream_based_tone_dial_freq));
  922. buffsend[10] = (tone1 & 0xff00) >> 8;
  923. buffsend[11] = (tone1 & 0x00ff);
  924. buffsend[12] = (tone2 & 0xff00) >> 8;
  925. buffsend[13] = (tone2 & 0x00ff);
  926. send_client(SIZE_HEADER + sizeof(packet_send_stream_based_tone_dial_freq), buffsend,
  927. pte);
  928. }
  929. if (unistimdebug)
  930. ast_verb(0, "Sending Stream Based Tone On\n");
  931. memcpy(buffsend + SIZE_HEADER, packet_send_stream_based_tone_on,
  932. sizeof(packet_send_stream_based_tone_on));
  933. send_client(SIZE_HEADER + sizeof(packet_send_stream_based_tone_on), buffsend, pte);
  934. }
  935. /* Positions for favorites
  936. |--------------------|
  937. | 5 2 |
  938. | 4 1 |
  939. | 3 0 |
  940. */
  941. /* status (icons) : 00 = nothing, 2x/3x = see parser.h, 4x/5x = blink fast, 6x/7x = blink slow */
  942. static void
  943. send_favorite(unsigned char pos, unsigned char status, struct unistimsession *pte,
  944. const char *text)
  945. {
  946. BUFFSEND;
  947. int i;
  948. if (unistimdebug)
  949. ast_verb(0, "Sending favorite pos %d with status 0x%.2x\n", pos, (unsigned)status);
  950. memcpy(buffsend + SIZE_HEADER, packet_send_favorite, sizeof(packet_send_favorite));
  951. buffsend[10] = pos;
  952. buffsend[24] = pos;
  953. buffsend[25] = status;
  954. i = strlen(text);
  955. if (i > FAV_MAX_LENGTH)
  956. i = FAV_MAX_LENGTH;
  957. memcpy(buffsend + FAV_MAX_LENGTH + 1, text, i);
  958. send_client(SIZE_HEADER + sizeof(packet_send_favorite), buffsend, pte);
  959. }
  960. static void refresh_all_favorite(struct unistimsession *pte)
  961. {
  962. int i = 0;
  963. if (unistimdebug)
  964. ast_verb(0, "Refreshing all favorite\n");
  965. for (i = 0; i < 6; i++) {
  966. if ((pte->device->softkeyicon[i] <= FAV_ICON_HEADPHONES_ONHOLD) &&
  967. (pte->device->softkeylinepos != i))
  968. send_favorite((unsigned char) i, pte->device->softkeyicon[i] + 1, pte,
  969. pte->device->softkeylabel[i]);
  970. else
  971. send_favorite((unsigned char) i, pte->device->softkeyicon[i], pte,
  972. pte->device->softkeylabel[i]);
  973. }
  974. }
  975. /* Change the status for this phone (pte) and update for each phones where pte is bookmarked
  976. * use FAV_ICON_*_BLACK constant in status parameters */
  977. static void change_favorite_icon(struct unistimsession *pte, unsigned char status)
  978. {
  979. struct unistim_device *d = devices;
  980. int i;
  981. /* Update the current phone */
  982. if (pte->state != STATE_CLEANING)
  983. send_favorite(pte->device->softkeylinepos, status, pte,
  984. pte->device->softkeylabel[pte->device->softkeylinepos]);
  985. /* Notify other phones if we're in their bookmark */
  986. while (d) {
  987. for (i = 0; i < 6; i++) {
  988. if (d->sp[i] == pte->device) { /* It's us ? */
  989. if (d->softkeyicon[i] != status) { /* Avoid resending the same icon */
  990. d->softkeyicon[i] = status;
  991. if (d->session)
  992. send_favorite(i, status + 1, d->session, d->softkeylabel[i]);
  993. }
  994. }
  995. }
  996. d = d->next;
  997. }
  998. }
  999. static int RegisterExtension(const struct unistimsession *pte)
  1000. {
  1001. if (unistimdebug)
  1002. ast_verb(0, "Trying to register extension '%s' into context '%s' to %s\n",
  1003. pte->device->extension_number, pte->device->lines->context,
  1004. pte->device->lines->fullname);
  1005. return ast_add_extension(pte->device->lines->context, 0,
  1006. pte->device->extension_number, 1, NULL, NULL, "Dial",
  1007. pte->device->lines->fullname, 0, "Unistim");
  1008. }
  1009. static int UnregisterExtension(const struct unistimsession *pte)
  1010. {
  1011. if (unistimdebug)
  1012. ast_verb(0, "Trying to unregister extension '%s' context '%s'\n",
  1013. pte->device->extension_number, pte->device->lines->context);
  1014. return ast_context_remove_extension(pte->device->lines->context,
  1015. pte->device->extension_number, 1, "Unistim");
  1016. }
  1017. /* Free memory allocated for a phone */
  1018. static void close_client(struct unistimsession *s)
  1019. {
  1020. struct unistim_subchannel *sub;
  1021. struct unistimsession *cur, *prev = NULL;
  1022. ast_mutex_lock(&sessionlock);
  1023. cur = sessions;
  1024. /* Looking for the session in the linked chain */
  1025. while (cur) {
  1026. if (cur == s)
  1027. break;
  1028. prev = cur;
  1029. cur = cur->next;
  1030. }
  1031. if (cur) { /* Session found ? */
  1032. if (cur->device) { /* This session was registered ? */
  1033. s->state = STATE_CLEANING;
  1034. if (unistimdebug)
  1035. ast_verb(0, "close_client session %p device %p lines %p sub %p\n",
  1036. s, s->device, s->device->lines,
  1037. s->device->lines->subs[SUB_REAL]);
  1038. change_favorite_icon(s, FAV_ICON_NONE);
  1039. sub = s->device->lines->subs[SUB_REAL];
  1040. if (sub) {
  1041. if (sub->owner) { /* Call in progress ? */
  1042. if (unistimdebug)
  1043. ast_verb(0, "Aborting call\n");
  1044. ast_queue_hangup_with_cause(sub->owner, AST_CAUSE_NETWORK_OUT_OF_ORDER);
  1045. }
  1046. } else
  1047. ast_log(LOG_WARNING, "Freeing a client with no subchannel !\n");
  1048. if (!ast_strlen_zero(s->device->extension_number))
  1049. UnregisterExtension(s);
  1050. cur->device->session = NULL;
  1051. } else {
  1052. if (unistimdebug)
  1053. ast_verb(0, "Freeing an unregistered client\n");
  1054. }
  1055. if (prev)
  1056. prev->next = cur->next;
  1057. else
  1058. sessions = cur->next;
  1059. ast_mutex_destroy(&s->lock);
  1060. ast_free(s);
  1061. } else
  1062. ast_log(LOG_WARNING, "Trying to delete non-existent session %p?\n", s);
  1063. ast_mutex_unlock(&sessionlock);
  1064. return;
  1065. }
  1066. /* Return 1 if the session chained link was modified */
  1067. static int send_retransmit(struct unistimsession *pte)
  1068. {
  1069. int i;
  1070. ast_mutex_lock(&pte->lock);
  1071. if (++pte->nb_retransmit >= NB_MAX_RETRANSMIT) {
  1072. if (unistimdebug)
  1073. ast_verb(0, "Too many retransmit - freeing client\n");
  1074. ast_mutex_unlock(&pte->lock);
  1075. close_client(pte);
  1076. return 1;
  1077. }
  1078. pte->timeout = get_tick_count() + RETRANSMIT_TIMER;
  1079. for (i = pte->last_buf_available - (pte->seq_server - pte->last_seq_ack);
  1080. i < pte->last_buf_available; i++) {
  1081. if (i < 0) {
  1082. ast_log(LOG_WARNING,
  1083. "Asked to retransmit an ACKed slot ! last_buf_available=%d, seq_server = #0x%.4x last_seq_ack = #0x%.4x\n",
  1084. pte->last_buf_available, (unsigned)pte->seq_server, (unsigned)pte->last_seq_ack);
  1085. continue;
  1086. }
  1087. if (unistimdebug) {
  1088. unsigned short *sbuf = (unsigned short *) pte->wsabufsend[i].buf;
  1089. unsigned short seq;
  1090. seq = ntohs(sbuf[1]);
  1091. ast_verb(0, "Retransmit slot #%d (seq=#0x%.4x), last ack was #0x%.4x\n", i,
  1092. (unsigned)seq, (unsigned)pte->last_seq_ack);
  1093. }
  1094. send_raw_client(pte->wsabufsend[i].len, pte->wsabufsend[i].buf, &pte->sin,
  1095. &pte->sout);
  1096. }
  1097. ast_mutex_unlock(&pte->lock);
  1098. return 0;
  1099. }
  1100. /* inverse : TEXT_INVERSE : yes, TEXT_NORMAL : no */
  1101. static void
  1102. send_text(unsigned char pos, unsigned char inverse, struct unistimsession *pte,
  1103. const char *text)
  1104. {
  1105. int i;
  1106. BUFFSEND;
  1107. if (unistimdebug)
  1108. ast_verb(0, "Sending text at pos %d, inverse flag %d\n", pos, inverse);
  1109. memcpy(buffsend + SIZE_HEADER, packet_send_text, sizeof(packet_send_text));
  1110. buffsend[10] = pos;
  1111. buffsend[11] = inverse;
  1112. i = strlen(text);
  1113. if (i > TEXT_LENGTH_MAX)
  1114. i = TEXT_LENGTH_MAX;
  1115. memcpy(buffsend + 12, text, i);
  1116. send_client(SIZE_HEADER + sizeof(packet_send_text), buffsend, pte);
  1117. }
  1118. static void send_text_status(struct unistimsession *pte, const char *text)
  1119. {
  1120. BUFFSEND;
  1121. int i;
  1122. if (unistimdebug)
  1123. ast_verb(0, "Sending status text\n");
  1124. if (pte->device) {
  1125. if (pte->device->status_method == 1) { /* For new firmware and i2050 soft phone */
  1126. int n = strlen(text);
  1127. /* Must send individual button separately */
  1128. int j;
  1129. for (i = 0, j = 0; i < 4; i++, j += 7) {
  1130. int pos = 0x08 + (i * 0x20);
  1131. memcpy(buffsend + SIZE_HEADER, packet_send_status2,
  1132. sizeof(packet_send_status2));
  1133. buffsend[9] = pos;
  1134. memcpy(buffsend + 10, (j < n) ? (text + j) : " ", 7);
  1135. send_client(SIZE_HEADER + sizeof(packet_send_status2), buffsend, pte);
  1136. }
  1137. return;
  1138. }
  1139. }
  1140. memcpy(buffsend + SIZE_HEADER, packet_send_status, sizeof(packet_send_status));
  1141. i = strlen(text);
  1142. if (i > STATUS_LENGTH_MAX)
  1143. i = STATUS_LENGTH_MAX;
  1144. memcpy(buffsend + 10, text, i);
  1145. send_client(SIZE_HEADER + sizeof(packet_send_status), buffsend, pte);
  1146. }
  1147. /* led values in hexa : 0 = bar off, 1 = bar on, 2 = bar 1s on/1s off, 3 = bar 2.5s on/0.5s off
  1148. * 4 = bar 0.6s on/0.3s off, 5 = bar 0.5s on/0.5s off, 6 = bar 2s on/0.5s off
  1149. * 7 = bar off, 8 = speaker off, 9 = speaker on, 10 = headphone off, 11 = headphone on
  1150. * 18 = mute off, 19 mute on */
  1151. static void send_led_update(struct unistimsession *pte, unsigned char led)
  1152. {
  1153. BUFFSEND;
  1154. if (unistimdebug)
  1155. ast_verb(0, "Sending led_update (%x)\n", (unsigned)led);
  1156. memcpy(buffsend + SIZE_HEADER, packet_send_led_update, sizeof(packet_send_led_update));
  1157. buffsend[9] = led;
  1158. send_client(SIZE_HEADER + sizeof(packet_send_led_update), buffsend, pte);
  1159. }
  1160. /* output = OUTPUT_HANDSET, OUTPUT_HEADPHONE or OUTPUT_SPEAKER
  1161. * volume = VOLUME_LOW, VOLUME_NORMAL, VOLUME_INSANELY_LOUD
  1162. * mute = MUTE_OFF, MUTE_ON */
  1163. static void
  1164. send_select_output(struct unistimsession *pte, unsigned char output, unsigned char volume,
  1165. unsigned char mute)
  1166. {
  1167. BUFFSEND;
  1168. if (unistimdebug)
  1169. ast_verb(0, "Sending select output packet output=%x volume=%x mute=%x\n", (unsigned)output,
  1170. (unsigned)volume, (unsigned)mute);
  1171. memcpy(buffsend + SIZE_HEADER, packet_send_select_output,
  1172. sizeof(packet_send_select_output));
  1173. buffsend[9] = output;
  1174. if (output == OUTPUT_SPEAKER)
  1175. volume = VOLUME_LOW_SPEAKER;
  1176. else
  1177. volume = VOLUME_LOW;
  1178. buffsend[10] = volume;
  1179. if (mute == MUTE_ON_DISCRET)
  1180. buffsend[11] = MUTE_ON;
  1181. else
  1182. buffsend[11] = mute;
  1183. send_client(SIZE_HEADER + sizeof(packet_send_select_output), buffsend, pte);
  1184. if (mute == MUTE_OFF)
  1185. send_led_update(pte, 0x18);
  1186. else if (mute == MUTE_ON)
  1187. send_led_update(pte, 0x19);
  1188. pte->device->mute = mute;
  1189. if (output == OUTPUT_HANDSET) {
  1190. if (mute == MUTE_ON)
  1191. change_favorite_icon(pte, FAV_ICON_ONHOLD_BLACK);
  1192. else
  1193. change_favorite_icon(pte, FAV_ICON_OFFHOOK_BLACK);
  1194. send_led_update(pte, 0x08);
  1195. send_led_update(pte, 0x10);
  1196. } else if (output == OUTPUT_HEADPHONE) {
  1197. if (mute == MUTE_ON)
  1198. change_favorite_icon(pte, FAV_ICON_HEADPHONES_ONHOLD);
  1199. else
  1200. change_favorite_icon(pte, FAV_ICON_HEADPHONES);
  1201. send_led_update(pte, 0x08);
  1202. send_led_update(pte, 0x11);
  1203. } else if (output == OUTPUT_SPEAKER) {
  1204. send_led_update(pte, 0x10);
  1205. send_led_update(pte, 0x09);
  1206. if (pte->device->receiver_state == STATE_OFFHOOK) {
  1207. if (mute == MUTE_ON)
  1208. change_favorite_icon(pte, FAV_ICON_SPEAKER_ONHOLD_BLACK);
  1209. else
  1210. change_favorite_icon(pte, FAV_ICON_SPEAKER_ONHOOK_BLACK);
  1211. } else {
  1212. if (mute == MUTE_ON)
  1213. change_favorite_icon(pte, FAV_ICON_SPEAKER_ONHOLD_BLACK);
  1214. else
  1215. change_favorite_icon(pte, FAV_ICON_SPEAKER_OFFHOOK_BLACK);
  1216. }
  1217. } else
  1218. ast_log(LOG_WARNING, "Invalid output (%d)\n", output);
  1219. if (output != pte->device->output)
  1220. pte->device->previous_output = pte->device->output;
  1221. pte->device->output = output;
  1222. }
  1223. static void send_ring(struct unistimsession *pte, char volume, char style)
  1224. {
  1225. BUFFSEND;
  1226. if (unistimdebug)
  1227. ast_verb(0, "Sending ring packet\n");
  1228. memcpy(buffsend + SIZE_HEADER, packet_send_ring, sizeof(packet_send_ring));
  1229. buffsend[24] = style + 0x10;
  1230. buffsend[29] = volume * 0x10;
  1231. send_client(SIZE_HEADER + sizeof(packet_send_ring), buffsend, pte);
  1232. }
  1233. static void send_no_ring(struct unistimsession *pte)
  1234. {
  1235. BUFFSEND;
  1236. if (unistimdebug)
  1237. ast_verb(0, "Sending no ring packet\n");
  1238. memcpy(buffsend + SIZE_HEADER, packet_send_no_ring, sizeof(packet_send_no_ring));
  1239. send_client(SIZE_HEADER + sizeof(packet_send_no_ring), buffsend, pte);
  1240. }
  1241. static void send_texttitle(struct unistimsession *pte, const char *text)
  1242. {
  1243. BUFFSEND;
  1244. int i;
  1245. if (unistimdebug)
  1246. ast_verb(0, "Sending title text\n");
  1247. memcpy(buffsend + SIZE_HEADER, packet_send_title, sizeof(packet_send_title));
  1248. i = strlen(text);
  1249. if (i > 12)
  1250. i = 12;
  1251. memcpy(buffsend + 10, text, i);
  1252. send_client(SIZE_HEADER + sizeof(packet_send_title), buffsend, pte);
  1253. }
  1254. static void send_date_time(struct unistimsession *pte)
  1255. {
  1256. BUFFSEND;
  1257. struct timeval now = ast_tvnow();
  1258. struct ast_tm atm = { 0, };
  1259. if (unistimdebug)
  1260. ast_verb(0, "Sending Time & Date\n");
  1261. memcpy(buffsend + SIZE_HEADER, packet_send_date_time, sizeof(packet_send_date_time));
  1262. ast_localtime(&now, &atm, NULL);
  1263. buffsend[10] = (unsigned char) atm.tm_mon + 1;
  1264. buffsend[11] = (unsigned char) atm.tm_mday;
  1265. buffsend[12] = (unsigned char) atm.tm_hour;
  1266. buffsend[13] = (unsigned char) atm.tm_min;
  1267. send_client(SIZE_HEADER + sizeof(packet_send_date_time), buffsend, pte);
  1268. }
  1269. static void send_date_time2(struct unistimsession *pte)
  1270. {
  1271. BUFFSEND;
  1272. struct timeval now = ast_tvnow();
  1273. struct ast_tm atm = { 0, };
  1274. if (unistimdebug)
  1275. ast_verb(0, "Sending Time & Date #2\n");
  1276. memcpy(buffsend + SIZE_HEADER, packet_send_date_time2, sizeof(packet_send_date_time2));
  1277. ast_localtime(&now, &atm, NULL);
  1278. if (pte->device)
  1279. buffsend[9] = pte->device->datetimeformat;
  1280. else
  1281. buffsend[9] = 61;
  1282. buffsend[14] = (unsigned char) atm.tm_mon + 1;
  1283. buffsend[15] = (unsigned char) atm.tm_mday;
  1284. buffsend[16] = (unsigned char) atm.tm_hour;
  1285. buffsend[17] = (unsigned char) atm.tm_min;
  1286. send_client(SIZE_HEADER + sizeof(packet_send_date_time2), buffsend, pte);
  1287. }
  1288. static void send_date_time3(struct unistimsession *pte)
  1289. {
  1290. BUFFSEND;
  1291. struct timeval now = ast_tvnow();
  1292. struct ast_tm atm = { 0, };
  1293. if (unistimdebug)
  1294. ast_verb(0, "Sending Time & Date #3\n");
  1295. memcpy(buffsend + SIZE_HEADER, packet_send_date_time3, sizeof(packet_send_date_time3));
  1296. ast_localtime(&now, &atm, NULL);
  1297. buffsend[10] = (unsigned char) atm.tm_mon + 1;
  1298. buffsend[11] = (unsigned char) atm.tm_mday;
  1299. buffsend[12] = (unsigned char) atm.tm_hour;
  1300. buffsend[13] = (unsigned char) atm.tm_min;
  1301. send_client(SIZE_HEADER + sizeof(packet_send_date_time3), buffsend, pte);
  1302. }
  1303. static void send_blink_cursor(struct unistimsession *pte)
  1304. {
  1305. BUFFSEND;
  1306. if (unistimdebug)
  1307. ast_verb(0, "Sending set blink\n");
  1308. memcpy(buffsend + SIZE_HEADER, packet_send_blink_cursor, sizeof(packet_send_blink_cursor));
  1309. send_client(SIZE_HEADER + sizeof(packet_send_blink_cursor), buffsend, pte);
  1310. return;
  1311. }
  1312. /* pos : 0xab (a=0/2/4 = line ; b = row) */
  1313. static void send_cursor_pos(struct unistimsession *pte, unsigned char pos)
  1314. {
  1315. BUFFSEND;
  1316. if (unistimdebug)
  1317. ast_verb(0, "Sending set cursor position\n");
  1318. memcpy(buffsend + SIZE_HEADER, packet_send_set_pos_cursor,
  1319. sizeof(packet_send_set_pos_cursor));
  1320. buffsend[11] = pos;
  1321. send_client(SIZE_HEADER + sizeof(packet_send_set_pos_cursor), buffsend, pte);
  1322. return;
  1323. }
  1324. static void rcv_resume_connection_with_server(struct unistimsession *pte)
  1325. {
  1326. BUFFSEND;
  1327. if (unistimdebug) {
  1328. ast_verb(0, "ResumeConnectionWithServer received\n");
  1329. ast_verb(0, "Sending packet_send_query_mac_address\n");
  1330. }
  1331. memcpy(buffsend + SIZE_HEADER, packet_send_query_mac_address,
  1332. sizeof(packet_send_query_mac_address));
  1333. send_client(SIZE_HEADER + sizeof(packet_send_query_mac_address), buffsend, pte);
  1334. return;
  1335. }
  1336. static int unistim_register(struct unistimsession *s)
  1337. {
  1338. struct unistim_device *d;
  1339. ast_mutex_lock(&devicelock);
  1340. d = devices;
  1341. while (d) {
  1342. if (!strcasecmp(s->macaddr, d->id)) {
  1343. /* XXX Deal with IP authentication */
  1344. s->device = d;
  1345. d->session = s;
  1346. d->codec_number = DEFAULT_CODEC;
  1347. d->pos_fav = 0;
  1348. d->missed_call = 0;
  1349. d->receiver_state = STATE_ONHOOK;
  1350. break;
  1351. }
  1352. d = d->next;
  1353. }
  1354. ast_mutex_unlock(&devicelock);
  1355. if (!d)
  1356. return 0;
  1357. return 1;
  1358. }
  1359. static int alloc_sub(struct unistim_line *l, int x)
  1360. {
  1361. struct unistim_subchannel *sub;
  1362. if (!(sub = ast_calloc(1, sizeof(*sub))))
  1363. return 0;
  1364. if (unistimdebug)
  1365. ast_verb(3, "Allocating UNISTIM subchannel #%d on %s@%s ptr=%p\n", x, l->name, l->parent->name, sub);
  1366. sub->parent = l;
  1367. sub->subtype = x;
  1368. l->subs[x] = sub;
  1369. ast_mutex_init(&sub->lock);
  1370. return 1;
  1371. }
  1372. static int unalloc_sub(struct unistim_line *p, int x)
  1373. {
  1374. if (!x) {
  1375. ast_log(LOG_WARNING, "Trying to unalloc the real channel %s@%s?!?\n", p->name,
  1376. p->parent->name);
  1377. return -1;
  1378. }
  1379. if (unistimdebug)
  1380. ast_debug(1, "Released sub %d of channel %s@%s\n", x, p->name,
  1381. p->parent->name);
  1382. ast_mutex_destroy(&p->lock);
  1383. ast_free(p->subs[x]);
  1384. p->subs[x] = 0;
  1385. return 0;
  1386. }
  1387. static void rcv_mac_addr(struct unistimsession *pte, const unsigned char *buf)
  1388. {
  1389. BUFFSEND;
  1390. int tmp, i = 0;
  1391. char addrmac[19];
  1392. int res = 0;
  1393. for (tmp = 15; tmp < 15 + SIZE_HEADER; tmp++) {
  1394. sprintf(&addrmac[i], "%.2x", (unsigned) buf[tmp]);
  1395. i += 2;
  1396. }
  1397. if (unistimdebug) {
  1398. ast_verb(0, "Mac Address received : %s\n", addrmac);
  1399. }
  1400. strcpy(pte->macaddr, addrmac);
  1401. res = unistim_register(pte);
  1402. if (!res) {
  1403. switch (autoprovisioning) {
  1404. case AUTOPROVISIONING_NO:
  1405. ast_log(LOG_WARNING, "No entry found for this phone : %s\n", addrmac);
  1406. pte->state = STATE_AUTHDENY;
  1407. break;
  1408. case AUTOPROVISIONING_YES:
  1409. {
  1410. struct unistim_device *d, *newd;
  1411. struct unistim_line *newl;
  1412. if (unistimdebug)
  1413. ast_verb(0, "New phone, autoprovisioning on\n");
  1414. /* First : locate the [template] section */
  1415. ast_mutex_lock(&devicelock);
  1416. d = devices;
  1417. while (d) {
  1418. if (!strcasecmp(d->name, "template")) {
  1419. /* Found, cloning this entry */
  1420. if (!(newd = ast_malloc(sizeof(*newd)))) {
  1421. ast_mutex_unlock(&devicelock);
  1422. return;
  1423. }
  1424. memcpy(newd, d, sizeof(*newd));
  1425. if (!(newl = ast_malloc(sizeof(*newl)))) {
  1426. ast_free(newd);
  1427. ast_mutex_unlock(&devicelock);
  1428. return;
  1429. }
  1430. memcpy(newl, d->lines, sizeof(*newl));
  1431. if (!alloc_sub(newl, SUB_REAL)) {
  1432. ast_free(newd);
  1433. ast_free(newl);
  1434. ast_mutex_unlock(&devicelock);
  1435. return;
  1436. }
  1437. /* Ok, now updating some fields */
  1438. ast_copy_string(newd->id, addrmac, sizeof(newd->id));
  1439. ast_copy_string(newd->name, addrmac, sizeof(newd->name));
  1440. if (newd->extension == EXTENSION_NONE)
  1441. newd->extension = EXTENSION_ASK;
  1442. newd->lines = newl;
  1443. newd->receiver_state = STATE_ONHOOK;
  1444. newd->session = pte;
  1445. newd->to_delete = -1;
  1446. pte->device = newd;
  1447. newd->next = NULL;
  1448. newl->parent = newd;
  1449. strcpy(newl->name, d->lines->name);
  1450. snprintf(d->lines->name, sizeof(d->lines->name), "%d",
  1451. atoi(d->lines->name) + 1);
  1452. snprintf(newl->fullname, sizeof(newl->fullname), "USTM/%s@%s",
  1453. newl->name, newd->name);
  1454. /* Go to the end of the linked chain */
  1455. while (d->next) {
  1456. d = d->next;
  1457. }
  1458. d->next = newd;
  1459. d = newd;
  1460. break;
  1461. }
  1462. d = d->next;
  1463. }
  1464. ast_mutex_unlock(&devicelock);
  1465. if (!d) {
  1466. ast_log(LOG_WARNING, "No entry [template] found in unistim.conf\n");
  1467. pte->state = STATE_AUTHDENY;
  1468. }
  1469. }
  1470. break;
  1471. case AUTOPROVISIONING_TN:
  1472. pte->state = STATE_AUTHDENY;
  1473. break;
  1474. case AUTOPROVISIONING_DB:
  1475. ast_log(LOG_WARNING,
  1476. "Autoprovisioning with database is not yet functional\n");
  1477. break;
  1478. default:
  1479. ast_log(LOG_WARNING, "Internal error : unknown autoprovisioning value = %u\n",
  1480. autoprovisioning);
  1481. }
  1482. }
  1483. if (pte->state != STATE_AUTHDENY) {
  1484. ast_verb(3, "Device '%s' successfuly registered\n", pte->device->name);
  1485. switch (pte->device->extension) {
  1486. case EXTENSION_NONE:
  1487. pte->state = STATE_MAINPAGE;
  1488. break;
  1489. case EXTENSION_ASK:
  1490. /* Checking if we already have an extension number */
  1491. if (ast_strlen_zero(pte->device->extension_number))
  1492. pte->state = STATE_EXTENSION;
  1493. else {
  1494. /* Yes, because of a phone reboot. We don't ask again for the TN */
  1495. if (RegisterExtension(pte))
  1496. pte->state = STATE_EXTENSION;
  1497. else
  1498. pte->state = STATE_MAINPAGE;
  1499. }
  1500. break;
  1501. case EXTENSION_LINE:
  1502. ast_copy_string(pte->device->extension_number, pte->device->lines->name,
  1503. sizeof(pte->device->extension_number));
  1504. if (RegisterExtension(pte))
  1505. pte->state = STATE_EXTENSION;
  1506. else
  1507. pte->state = STATE_MAINPAGE;
  1508. break;
  1509. case EXTENSION_TN:
  1510. /* If we are here, it's because of a phone reboot */
  1511. pte->state = STATE_MAINPAGE;
  1512. break;
  1513. default:
  1514. ast_log(LOG_WARNING, "Internal error, extension value unknown : %u\n",
  1515. pte->device->extension);
  1516. pte->state = STATE_AUTHDENY;
  1517. break;
  1518. }
  1519. }
  1520. if (pte->state == STATE_EXTENSION) {
  1521. if (pte->device->extension != EXTENSION_TN)
  1522. pte->device->extension = EXTENSION_ASK;
  1523. pte->device->extension_number[0] = '\0';
  1524. }
  1525. if (unistimdebug)
  1526. ast_verb(0, "\nSending S1\n");
  1527. memcpy(buffsend + SIZE_HEADER, packet_send_S1, sizeof(packet_send_S1));
  1528. send_client(SIZE_HEADER + sizeof(packet_send_S1), buffsend, pte);
  1529. if (unistimdebug)
  1530. ast_verb(0, "Sending query_basic_manager_04\n");
  1531. memcpy(buffsend + SIZE_HEADER, packet_send_query_basic_manager_04,
  1532. sizeof(packet_send_query_basic_manager_04));
  1533. send_client(SIZE_HEADER + sizeof(packet_send_query_basic_manager_04), buffsend, pte);
  1534. if (unistimdebug)
  1535. ast_verb(0, "Sending query_basic_manager_10\n");
  1536. memcpy(buffsend + SIZE_HEADER, packet_send_query_basic_manager_10,
  1537. sizeof(packet_send_query_basic_manager_10));
  1538. send_client(SIZE_HEADER + sizeof(packet_send_query_basic_manager_10), buffsend, pte);
  1539. send_date_time(pte);
  1540. return;
  1541. }
  1542. static int write_entry_history(struct unistimsession *pte, FILE * f, char c, char *line1)
  1543. {
  1544. if (fwrite(&c, 1, 1, f) != 1) {
  1545. display_last_error("Unable to write history log header.");
  1546. return -1;
  1547. }
  1548. if (fwrite(line1, TEXT_LENGTH_MAX, 1, f) != 1) {
  1549. display_last_error("Unable to write history entry - date.");
  1550. return -1;
  1551. }
  1552. if (fwrite(pte->device->lst_cid, TEXT_LENGTH_MAX, 1, f) != 1) {
  1553. display_last_error("Unable to write history entry - callerid.");
  1554. return -1;
  1555. }
  1556. if (fwrite(pte->device->lst_cnm, TEXT_LENGTH_MAX, 1, f) != 1) {
  1557. display_last_error("Unable to write history entry - callername.");
  1558. return -1;
  1559. }
  1560. return 0;
  1561. }
  1562. static int write_history(struct unistimsession *pte, char way, char ismissed)
  1563. {
  1564. char tmp[AST_CONFIG_MAX_PATH], tmp2[AST_CONFIG_MAX_PATH];
  1565. char line1[TEXT_LENGTH_MAX + 1];
  1566. char count = 0, *histbuf;
  1567. int size;
  1568. FILE *f, *f2;
  1569. struct timeval now = ast_tvnow();
  1570. struct ast_tm atm = { 0, };
  1571. if (!pte->device)
  1572. return -1;
  1573. if (!pte->device->callhistory)
  1574. return 0;
  1575. if (strchr(pte->device->name, '/') || (pte->device->name[0] == '.')) {
  1576. ast_log(LOG_WARNING, "Account code '%s' insecure for writing file\n",
  1577. pte->device->name);
  1578. return -1;
  1579. }
  1580. snprintf(tmp, sizeof(tmp), "%s/%s", ast_config_AST_LOG_DIR, USTM_LOG_DIR);
  1581. if (ast_mkdir(tmp, 0770)) {
  1582. if (errno != EEXIST) {
  1583. display_last_error("Unable to create directory for history");
  1584. return -1;
  1585. }
  1586. }
  1587. ast_localtime(&now, &atm, NULL);
  1588. if (ismissed) {
  1589. if (way == 'i')
  1590. strcpy(tmp2, "Miss");
  1591. else
  1592. strcpy(tmp2, "Fail");
  1593. } else
  1594. strcpy(tmp2, "Answ");
  1595. snprintf(line1, sizeof(line1), "%04d/%02d/%02d %02d:%02d:%02d %s",
  1596. atm.tm_year + 1900, atm.tm_mon + 1, atm.tm_mday, atm.tm_hour,
  1597. atm.tm_min, atm.tm_sec, tmp2);
  1598. snprintf(tmp, sizeof(tmp), "%s/%s/%s-%c.csv", ast_config_AST_LOG_DIR,
  1599. USTM_LOG_DIR, pte->device->name, way);
  1600. if ((f = fopen(tmp, "r"))) {
  1601. struct stat bufstat;
  1602. if (stat(tmp, &bufstat)) {
  1603. display_last_error("Unable to stat history log.");
  1604. fclose(f);
  1605. return -1;
  1606. }
  1607. size = 1 + (MAX_ENTRY_LOG * TEXT_LENGTH_MAX * 3);
  1608. if (bufstat.st_size != size) {
  1609. ast_log(LOG_WARNING,
  1610. "History file %s has an incorrect size (%d instead of %d). It will be replaced by a new one.",
  1611. tmp, (int) bufstat.st_size, size);
  1612. fclose(f);
  1613. f = NULL;
  1614. count = 1;
  1615. }
  1616. }
  1617. /* If we can't open the log file, we create a brand new one */
  1618. if (!f) {
  1619. char c = 1;
  1620. int i;
  1621. if ((errno != ENOENT) && (count == 0)) {
  1622. display_last_error("Unable to open history log.");
  1623. return -1;
  1624. }
  1625. f = fopen(tmp, "w");
  1626. if (!f) {
  1627. display_last_error("Unable to create history log.");
  1628. return -1;
  1629. }
  1630. if (write_entry_history(pte, f, c, line1)) {
  1631. fclose(f);
  1632. return -1;
  1633. }
  1634. memset(line1, ' ', TEXT_LENGTH_MAX);
  1635. for (i = 3; i < MAX_ENTRY_LOG * 3; i++) {
  1636. if (fwrite(line1, TEXT_LENGTH_MAX, 1, f) != 1) {
  1637. display_last_error("Unable to write history entry - stuffing.");
  1638. fclose(f);
  1639. return -1;
  1640. }
  1641. }
  1642. if (fclose(f))
  1643. display_last_error("Unable to close history - creation.");
  1644. return 0;
  1645. }
  1646. /* We can open the log file, we create a temporary one, we add our entry and copy the rest */
  1647. if (fread(&count, 1, 1, f) != 1) {
  1648. display_last_error("Unable to read history header.");
  1649. fclose(f);
  1650. return -1;
  1651. }
  1652. if (count > MAX_ENTRY_LOG) {
  1653. ast_log(LOG_WARNING, "Invalid count in history header of %s (%d max %d)\n", tmp,
  1654. count, MAX_ENTRY_LOG);
  1655. fclose(f);
  1656. return -1;
  1657. }
  1658. snprintf(tmp2, sizeof(tmp2), "%s/%s/%s-%c.csv.tmp", ast_config_AST_LOG_DIR,
  1659. USTM_LOG_DIR, pte->device->name, way);
  1660. if (!(f2 = fopen(tmp2, "w"))) {
  1661. display_last_error("Unable to create temporary history log.");
  1662. fclose(f);
  1663. return -1;
  1664. }
  1665. if (++count > MAX_ENTRY_LOG)
  1666. count = MAX_ENTRY_LOG;
  1667. if (write_entry_history(pte, f2, count, line1)) {
  1668. fclose(f);
  1669. fclose(f2);
  1670. return -1;
  1671. }
  1672. size = (MAX_ENTRY_LOG - 1) * TEXT_LENGTH_MAX * 3;
  1673. if (!(histbuf = ast_malloc(size))) {
  1674. fclose(f);
  1675. fclose(f2);
  1676. return -1;
  1677. }
  1678. if (fread(histbuf, size, 1, f) != 1) {
  1679. ast_free(histbuf);
  1680. fclose(f);
  1681. fclose(f2);
  1682. display_last_error("Unable to read previous history entries.");
  1683. return -1;
  1684. }
  1685. if (fwrite(histbuf, size, 1, f2) != 1) {
  1686. ast_free(histbuf);
  1687. fclose(f);
  1688. fclose(f2);
  1689. display_last_error("Unable to write previous history entries.");
  1690. return -1;
  1691. }
  1692. ast_free(histbuf);
  1693. if (fclose(f))
  1694. display_last_error("Unable to close history log.");
  1695. if (fclose(f2))
  1696. display_last_error("Unable to close temporary history log.");
  1697. if (unlink(tmp))
  1698. display_last_error("Unable to remove old history log.");
  1699. if (rename(tmp2, tmp))
  1700. display_last_error("Unable to rename new history log.");
  1701. return 0;
  1702. }
  1703. static void cancel_dial(struct unistimsession *pte)
  1704. {
  1705. send_no_ring(pte);
  1706. pte->device->missed_call++;
  1707. write_history(pte, 'i', 1);
  1708. show_main_page(pte);
  1709. return;
  1710. }
  1711. static void swap_subs(struct unistim_line *p, int a, int b)
  1712. {
  1713. /* struct ast_channel *towner; */
  1714. struct ast_rtp_instance *rtp;
  1715. int fds;
  1716. if (unistimdebug)
  1717. ast_verb(0, "Swapping %d and %d\n", a, b);
  1718. if ((!p->subs[a]->owner) || (!p->subs[b]->owner)) {
  1719. ast_log(LOG_WARNING,
  1720. "Attempted to swap subchannels with a null owner : sub #%d=%p sub #%d=%p\n",
  1721. a, p->subs[a]->owner, b, p->subs[b]->owner);
  1722. return;
  1723. }
  1724. rtp = p->subs[a]->rtp;
  1725. p->subs[a]->rtp = p->subs[b]->rtp;
  1726. p->subs[b]->rtp = rtp;
  1727. fds = p->subs[a]->owner->fds[0];
  1728. p->subs[a]->owner->fds[0] = p->subs[b]->owner->fds[0];
  1729. p->subs[b]->owner->fds[0] = fds;
  1730. fds = p->subs[a]->owner->fds[1];
  1731. p->subs[a]->owner->fds[1] = p->subs[b]->owner->fds[1];
  1732. p->subs[b]->owner->fds[1] = fds;
  1733. }
  1734. static int attempt_transfer(struct unistim_subchannel *p1, struct unistim_subchannel *p2)
  1735. {
  1736. int res = 0;
  1737. struct ast_channel
  1738. *chana = NULL, *chanb = NULL, *bridgea = NULL, *bridgeb = NULL, *peera =
  1739. NULL, *peerb = NULL, *peerc = NULL;
  1740. if (!p1->owner || !p2->owner) {
  1741. ast_log(LOG_WARNING, "Transfer attempted without dual ownership?\n");
  1742. return -1;
  1743. }
  1744. chana = p1->owner;
  1745. chanb = p2->owner;
  1746. bridgea = ast_bridged_channel(chana);
  1747. bridgeb = ast_bridged_channel(chanb);
  1748. if (bridgea) {
  1749. peera = chana;
  1750. peerb = chanb;
  1751. peerc = bridgea;
  1752. } else if (bridgeb) {
  1753. peera = chanb;
  1754. peerb = chana;
  1755. peerc = bridgeb;
  1756. }
  1757. if (peera && peerb && peerc && (peerb != peerc)) {
  1758. /*ast_quiet_chan(peera);
  1759. ast_quiet_chan(peerb);
  1760. ast_quiet_chan(peerc);
  1761. ast_quiet_chan(peerd); */
  1762. if (ast_channel_masquerade(peerb, peerc)) {
  1763. ast_log(LOG_WARNING, "Failed to masquerade %s into %s\n", peerb->name,
  1764. peerc->name);
  1765. res = -1;
  1766. }
  1767. return res;
  1768. } else {
  1769. ast_log(LOG_NOTICE,
  1770. "Transfer attempted with no appropriate bridged calls to transfer\n");
  1771. if (chana)
  1772. ast_softhangup_nolock(chana, AST_SOFTHANGUP_DEV);
  1773. if (chanb)
  1774. ast_softhangup_nolock(chanb, AST_SOFTHANGUP_DEV);
  1775. return -1;
  1776. }
  1777. return 0;
  1778. }
  1779. void change_callerid(struct unistimsession *pte, int type, char *callerid)
  1780. {
  1781. char *data;
  1782. int size;
  1783. if (type)
  1784. data = pte->device->lst_cnm;
  1785. else
  1786. data = pte->device->lst_cid;
  1787. /* This is very nearly strncpy(), except that the remaining buffer
  1788. * is padded with ' ', instead of '\0' */
  1789. memset(data, ' ', TEXT_LENGTH_MAX);
  1790. size = strlen(callerid);
  1791. if (size > TEXT_LENGTH_MAX)
  1792. size = TEXT_LENGTH_MAX;
  1793. memcpy(data, callerid, size);
  1794. }
  1795. static void close_call(struct unistimsession *pte)
  1796. {
  1797. struct unistim_subchannel *sub;
  1798. struct unistim_line *l = pte->device->lines;
  1799. sub = pte->device->lines->subs[SUB_REAL];
  1800. send_stop_timer(pte);
  1801. if (sub->owner) {
  1802. sub->alreadygone = 1;
  1803. if (l->subs[SUB_THREEWAY]) {
  1804. l->subs[SUB_THREEWAY]->alreadygone = 1;
  1805. if (attempt_transfer(sub, l->subs[SUB_THREEWAY]) < 0)
  1806. ast_verb(0, "attempt_transfer failed.\n");
  1807. } else
  1808. ast_queue_hangup(sub->owner);
  1809. } else {
  1810. if (l->subs[SUB_THREEWAY]) {
  1811. if (l->subs[SUB_THREEWAY]->owner)
  1812. ast_queue_hangup_with_cause(l->subs[SUB_THREEWAY]->owner, AST_CAUSE_NORMAL_CLEARING);
  1813. else
  1814. ast_log(LOG_WARNING, "threeway sub without owner\n");
  1815. } else
  1816. ast_verb(0, "USTM(%s@%s-%u) channel already destroyed\n", sub->parent->name,
  1817. sub->parent->parent->name, sub->subtype);
  1818. }
  1819. change_callerid(pte, 0, pte->device->redial_number);
  1820. change_callerid(pte, 1, "");
  1821. write_history(pte, 'o', pte->device->missed_call);
  1822. pte->device->missed_call = 0;
  1823. show_main_page(pte);
  1824. return;
  1825. }
  1826. static void IgnoreCall(struct unistimsession *pte)
  1827. {
  1828. send_no_ring(pte);
  1829. return;
  1830. }
  1831. static void *unistim_ss(void *data)
  1832. {
  1833. struct ast_channel *chan = data;
  1834. struct unistim_subchannel *sub = chan->tech_pvt;
  1835. struct unistim_line *l = sub->parent;
  1836. struct unistimsession *s = l->parent->session;
  1837. int res;
  1838. ast_verb(3, "Starting switch on '%s@%s-%u' to %s\n", l->name, l->parent->name, sub->subtype, s->device->phone_number);
  1839. ast_copy_string(chan->exten, s->device->phone_number, sizeof(chan->exten));
  1840. ast_copy_string(s->device->redial_number, s->device->phone_number,
  1841. sizeof(s->device->redial_number));
  1842. ast_setstate(chan, AST_STATE_RING);
  1843. res = ast_pbx_run(chan);
  1844. if (res) {
  1845. ast_log(LOG_WARNING, "PBX exited non-zero\n");
  1846. send_tone(s, 1000, 0);;
  1847. }
  1848. return NULL;
  1849. }
  1850. static void start_rtp(struct unistim_subchannel *sub)
  1851. {
  1852. BUFFSEND;
  1853. struct sockaddr_in us = { 0, };
  1854. struct sockaddr_in public = { 0, };
  1855. struct sockaddr_in sin = { 0, };
  1856. format_t codec;
  1857. struct sockaddr_in sout = { 0, };
  1858. struct ast_sockaddr us_tmp;
  1859. struct ast_sockaddr sin_tmp;
  1860. struct ast_sockaddr sout_tmp;
  1861. /* Sanity checks */
  1862. if (!sub) {
  1863. ast_log(LOG_WARNING, "start_rtp with a null subchannel !\n");
  1864. return;
  1865. }
  1866. if (!sub->parent) {
  1867. ast_log(LOG_WARNING, "start_rtp with a null line !\n");
  1868. return;
  1869. }
  1870. if (!sub->parent->parent) {
  1871. ast_log(LOG_WARNING, "start_rtp with a null device !\n");
  1872. return;
  1873. }
  1874. if (!sub->parent->parent->session) {
  1875. ast_log(LOG_WARNING, "start_rtp with a null session !\n");
  1876. return;
  1877. }
  1878. sout = sub->parent->parent->session->sout;
  1879. ast_mutex_lock(&sub->lock);
  1880. /* Allocate the RTP */
  1881. if (unistimdebug)
  1882. ast_verb(0, "Starting RTP. Bind on %s\n", ast_inet_ntoa(sout.sin_addr));
  1883. ast_sockaddr_from_sin(&sout_tmp, &sout);
  1884. sub->rtp = ast_rtp_instance_new("asterisk", sched, &sout_tmp, NULL);
  1885. if (!sub->rtp) {
  1886. ast_log(LOG_WARNING, "Unable to create RTP session: %s binaddr=%s\n",
  1887. strerror(errno), ast_inet_ntoa(sout.sin_addr));
  1888. ast_mutex_unlock(&sub->lock);
  1889. return;
  1890. }
  1891. ast_rtp_instance_set_prop(sub->rtp, AST_RTP_PROPERTY_RTCP, 1);
  1892. if (sub->owner) {
  1893. sub->owner->fds[0] = ast_rtp_instance_fd(sub->rtp, 0);
  1894. sub->owner->fds[1] = ast_rtp_instance_fd(sub->rtp, 1);
  1895. }
  1896. ast_rtp_instance_set_qos(sub->rtp, qos.tos_audio, qos.cos_audio, "UNISTIM RTP");
  1897. ast_rtp_instance_set_prop(sub->rtp, AST_RTP_PROPERTY_NAT, sub->parent->parent->nat);
  1898. /* Create the RTP connection */
  1899. ast_rtp_instance_get_local_address(sub->rtp, &us_tmp);
  1900. ast_sockaddr_to_sin(&us_tmp, &us);
  1901. sin.sin_family = AF_INET;
  1902. /* Setting up RTP for our side */
  1903. memcpy(&sin.sin_addr, &sub->parent->parent->session->sin.sin_addr,
  1904. sizeof(sin.sin_addr));
  1905. sin.sin_port = htons(sub->parent->parent->rtp_port);
  1906. ast_sockaddr_from_sin(&sin_tmp, &sin);
  1907. ast_rtp_instance_set_remote_address(sub->rtp, &sin_tmp);
  1908. if (!(sub->owner->nativeformats & sub->owner->readformat)) {
  1909. format_t fmt;
  1910. char tmp[256];
  1911. fmt = ast_best_codec(sub->owner->nativeformats);
  1912. ast_log(LOG_WARNING,
  1913. "Our read/writeformat has been changed to something incompatible: %s, using %s best codec from %s\n",
  1914. ast_getformatname(sub->owner->readformat),
  1915. ast_getformatname(fmt),
  1916. ast_getformatname_multiple(tmp, sizeof(tmp), sub->owner->nativeformats));
  1917. sub->owner->readformat = fmt;
  1918. sub->owner->writeformat = fmt;
  1919. }
  1920. codec = ast_rtp_codecs_payload_code(ast_rtp_instance_get_codecs(sub->rtp), 1, sub->owner->readformat);
  1921. /* Setting up RTP of the phone */
  1922. if (public_ip.sin_family == 0) /* NAT IP override ? */
  1923. memcpy(&public, &us, sizeof(public)); /* No defined, using IP from recvmsg */
  1924. else
  1925. memcpy(&public, &public_ip, sizeof(public)); /* override */
  1926. if (unistimdebug) {
  1927. ast_verb(0, "RTP started : Our IP/port is : %s:%hd with codec %s\n",
  1928. ast_inet_ntoa(us.sin_addr),
  1929. htons(us.sin_port), ast_getformatname(sub->owner->readformat));
  1930. ast_verb(0, "Starting phone RTP stack. Our public IP is %s\n",
  1931. ast_inet_ntoa(public.sin_addr));
  1932. }
  1933. if ((sub->owner->readformat == AST_FORMAT_ULAW) ||
  1934. (sub->owner->readformat == AST_FORMAT_ALAW)) {
  1935. if (unistimdebug)
  1936. ast_verb(0, "Sending packet_send_rtp_packet_size for codec %s\n", ast_getformatname(codec));
  1937. memcpy(buffsend + SIZE_HEADER, packet_send_rtp_packet_size,
  1938. sizeof(packet_send_rtp_packet_size));
  1939. buffsend[10] = (int) codec & 0xffffffffLL;
  1940. send_client(SIZE_HEADER + sizeof(packet_send_rtp_packet_size), buffsend,
  1941. sub->parent->parent->session);
  1942. }
  1943. if (unistimdebug)
  1944. ast_verb(0, "Sending Jitter Buffer Parameters Configuration\n");
  1945. memcpy(buffsend + SIZE_HEADER, packet_send_jitter_buffer_conf,
  1946. sizeof(packet_send_jitter_buffer_conf));
  1947. send_client(SIZE_HEADER + sizeof(packet_send_jitter_buffer_conf), buffsend,
  1948. sub->parent->parent->session);
  1949. if (sub->parent->parent->rtp_method != 0) {
  1950. uint16_t rtcpsin_port = htons(us.sin_port) + 1; /* RTCP port is RTP + 1 */
  1951. if (unistimdebug)
  1952. ast_verb(0, "Sending OpenAudioStreamTX using method #%d\n",
  1953. sub->parent->parent->rtp_method);
  1954. if (sub->parent->parent->rtp_method == 3)
  1955. memcpy(buffsend + SIZE_HEADER, packet_send_open_audio_stream_tx3,
  1956. sizeof(packet_send_open_audio_stream_tx3));
  1957. else
  1958. memcpy(buffsend + SIZE_HEADER, packet_send_open_audio_stream_tx,
  1959. sizeof(packet_send_open_audio_stream_tx));
  1960. if (sub->parent->parent->rtp_method != 2) {
  1961. memcpy(buffsend + 28, &public.sin_addr, sizeof(public.sin_addr));
  1962. buffsend[20] = (htons(sin.sin_port) & 0xff00) >> 8;
  1963. buffsend[21] = (htons(sin.sin_port) & 0x00ff);
  1964. buffsend[23] = (rtcpsin_port & 0x00ff);
  1965. buffsend[22] = (rtcpsin_port & 0xff00) >> 8;
  1966. buffsend[25] = (us.sin_port & 0xff00) >> 8;
  1967. buffsend[24] = (us.sin_port & 0x00ff);
  1968. buffsend[27] = (rtcpsin_port & 0x00ff);
  1969. buffsend[26] = (rtcpsin_port & 0xff00) >> 8;
  1970. } else {
  1971. memcpy(buffsend + 23, &public.sin_addr, sizeof(public.sin_addr));
  1972. buffsend[15] = (htons(sin.sin_port) & 0xff00) >> 8;
  1973. buffsend[16] = (htons(sin.sin_port) & 0x00ff);
  1974. buffsend[20] = (us.sin_port & 0xff00) >> 8;
  1975. buffsend[19] = (us.sin_port & 0x00ff);
  1976. }
  1977. buffsend[11] = codec; /* rx */
  1978. buffsend[12] = codec; /* tx */
  1979. send_client(SIZE_HEADER + sizeof(packet_send_open_audio_stream_tx), buffsend,
  1980. sub->parent->parent->session);
  1981. if (unistimdebug)
  1982. ast_verb(0, "Sending OpenAudioStreamRX\n");
  1983. if (sub->parent->parent->rtp_method == 3)
  1984. memcpy(buffsend + SIZE_HEADER, packet_send_open_audio_stream_rx3,
  1985. sizeof(packet_send_open_audio_stream_rx3));
  1986. else
  1987. memcpy(buffsend + SIZE_HEADER, packet_send_open_audio_stream_rx,
  1988. sizeof(packet_send_open_audio_stream_rx));
  1989. if (sub->parent->parent->rtp_method != 2) {
  1990. memcpy(buffsend + 28, &public.sin_addr, sizeof(public.sin_addr));
  1991. buffsend[20] = (htons(sin.sin_port) & 0xff00) >> 8;
  1992. buffsend[21] = (htons(sin.sin_port) & 0x00ff);
  1993. buffsend[23] = (rtcpsin_port & 0x00ff);
  1994. buffsend[22] = (rtcpsin_port & 0xff00) >> 8;
  1995. buffsend[25] = (us.sin_port & 0xff00) >> 8;
  1996. buffsend[24] = (us.sin_port & 0x00ff);
  1997. buffsend[27] = (rtcpsin_port & 0x00ff);
  1998. buffsend[26] = (rtcpsin_port & 0xff00) >> 8;
  1999. } else {
  2000. memcpy(buffsend + 23, &public.sin_addr, sizeof(public.sin_addr));
  2001. buffsend[15] = (htons(sin.sin_port) & 0xff00) >> 8;
  2002. buffsend[16] = (htons(sin.sin_port) & 0x00ff);
  2003. buffsend[20] = (us.sin_port & 0xff00) >> 8;
  2004. buffsend[19] = (us.sin_port & 0x00ff);
  2005. }
  2006. buffsend[11] = codec; /* rx */
  2007. buffsend[12] = codec; /* tx */
  2008. send_client(SIZE_HEADER + sizeof(packet_send_open_audio_stream_rx), buffsend,
  2009. sub->parent->parent->session);
  2010. } else {
  2011. uint16_t rtcpsin_port = htons(us.sin_port) + 1; /* RTCP port is RTP + 1 */
  2012. if (unistimdebug)
  2013. ast_verb(0, "Sending packet_send_call default method\n");
  2014. memcpy(buffsend + SIZE_HEADER, packet_send_call, sizeof(packet_send_call));
  2015. memcpy(buffsend + 53, &public.sin_addr, sizeof(public.sin_addr));
  2016. /* Destination port when sending RTP */
  2017. buffsend[49] = (us.sin_port & 0x00ff);
  2018. buffsend[50] = (us.sin_port & 0xff00) >> 8;
  2019. /* Destination port when sending RTCP */
  2020. buffsend[52] = (rtcpsin_port & 0x00ff);
  2021. buffsend[51] = (rtcpsin_port & 0xff00) >> 8;
  2022. /* Codec */
  2023. buffsend[40] = codec;
  2024. buffsend[41] = codec;
  2025. if (sub->owner->readformat == AST_FORMAT_ULAW)
  2026. buffsend[42] = 1; /* 1 = 20ms (160 bytes), 2 = 40ms (320 bytes) */
  2027. else if (sub->owner->readformat == AST_FORMAT_ALAW)
  2028. buffsend[42] = 1; /* 1 = 20ms (160 bytes), 2 = 40ms (320 bytes) */
  2029. else if (sub->owner->readformat == AST_FORMAT_G723_1)
  2030. buffsend[42] = 2; /* 1 = 30ms (24 bytes), 2 = 60 ms (48 bytes) */
  2031. else if (sub->owner->readformat == AST_FORMAT_G729A)
  2032. buffsend[42] = 2; /* 1 = 10ms (10 bytes), 2 = 20ms (20 bytes) */
  2033. else
  2034. ast_log(LOG_WARNING, "Unsupported codec %s!\n",
  2035. ast_getformatname(sub->owner->readformat));
  2036. /* Source port for transmit RTP and Destination port for receiving RTP */
  2037. buffsend[45] = (htons(sin.sin_port) & 0xff00) >> 8;
  2038. buffsend[46] = (htons(sin.sin_port) & 0x00ff);
  2039. buffsend[47] = (rtcpsin_port & 0xff00) >> 8;
  2040. buffsend[48] = (rtcpsin_port & 0x00ff);
  2041. send_client(SIZE_HEADER + sizeof(packet_send_call), buffsend,
  2042. sub->parent->parent->session);
  2043. }
  2044. ast_mutex_unlock(&sub->lock);
  2045. }
  2046. static void SendDialTone(struct unistimsession *pte)
  2047. {
  2048. int i;
  2049. /* No country defined ? Using US tone */
  2050. if (ast_strlen_zero(pte->device->country)) {
  2051. if (unistimdebug)
  2052. ast_verb(0, "No country defined, using US tone\n");
  2053. send_tone(pte, 350, 440);
  2054. return;
  2055. }
  2056. if (strlen(pte->device->country) != 2) {
  2057. if (unistimdebug)
  2058. ast_verb(0, "Country code != 2 char, using US tone\n");
  2059. send_tone(pte, 350, 440);
  2060. return;
  2061. }
  2062. i = 0;
  2063. while (frequency[i].freq1) {
  2064. if ((frequency[i].country[0] == pte->device->country[0]) &&
  2065. (frequency[i].country[1] == pte->device->country[1])) {
  2066. if (unistimdebug)
  2067. ast_verb(0, "Country code found (%s), freq1=%d freq2=%d\n",
  2068. frequency[i].country, frequency[i].freq1, frequency[i].freq2);
  2069. send_tone(pte, frequency[i].freq1, frequency[i].freq2);
  2070. }
  2071. i++;
  2072. }
  2073. }
  2074. static void handle_dial_page(struct unistimsession *pte)
  2075. {
  2076. pte->state = STATE_DIALPAGE;
  2077. if (pte->device->call_forward[0] == -1) {
  2078. send_text(TEXT_LINE0, TEXT_NORMAL, pte, "");
  2079. send_text(TEXT_LINE1, TEXT_NORMAL, pte, "Enter forward");
  2080. send_text_status(pte, "ForwardCancel BackSpcErase");
  2081. if (pte->device->call_forward[1] != 0) {
  2082. char tmp[TEXT_LENGTH_MAX + 1];
  2083. ast_copy_string(pte->device->phone_number, pte->device->call_forward + 1,
  2084. sizeof(pte->device->phone_number));
  2085. pte->device->size_phone_number = strlen(pte->device->phone_number);
  2086. if (pte->device->size_phone_number > 15)
  2087. pte->device->size_phone_number = 15;
  2088. strcpy(tmp, "Number : ...............");
  2089. memcpy(tmp + 9, pte->device->phone_number, pte->device->size_phone_number);
  2090. if (pte->device->height == 1) {
  2091. send_text(TEXT_LINE0, TEXT_NORMAL, pte, tmp);
  2092. send_blink_cursor(pte);
  2093. send_cursor_pos(pte,
  2094. (unsigned char) (TEXT_LINE0 + 0x09 +
  2095. pte->device->size_phone_number));
  2096. } else {
  2097. send_text(TEXT_LINE2, TEXT_NORMAL, pte, tmp);
  2098. send_blink_cursor(pte);
  2099. send_cursor_pos(pte,
  2100. (unsigned char) (TEXT_LINE2 + 0x09 +
  2101. pte->device->size_phone_number));
  2102. }
  2103. send_led_update(pte, 0);
  2104. return;
  2105. }
  2106. } else {
  2107. if ((pte->device->output == OUTPUT_HANDSET) &&
  2108. (pte->device->receiver_state == STATE_ONHOOK))
  2109. send_select_output(pte, OUTPUT_SPEAKER, pte->device->volume, MUTE_OFF);
  2110. else
  2111. send_select_output(pte, pte->device->output, pte->device->volume, MUTE_OFF);
  2112. SendDialTone(pte);
  2113. if (pte->device->height > 1) {
  2114. send_text(TEXT_LINE0, TEXT_NORMAL, pte, "Enter the number to dial");
  2115. send_text(TEXT_LINE1, TEXT_NORMAL, pte, "and press Call");
  2116. }
  2117. send_text_status(pte, "Call Redial BackSpcErase");
  2118. }
  2119. if (pte->device->height == 1) {
  2120. send_text(TEXT_LINE0, TEXT_NORMAL, pte, "Number : ...............");
  2121. send_blink_cursor(pte);
  2122. send_cursor_pos(pte, TEXT_LINE0 + 0x09);
  2123. } else {
  2124. send_text(TEXT_LINE2, TEXT_NORMAL, pte, "Number : ...............");
  2125. send_blink_cursor(pte);
  2126. send_cursor_pos(pte, TEXT_LINE2 + 0x09);
  2127. }
  2128. pte->device->size_phone_number = 0;
  2129. pte->device->phone_number[0] = 0;
  2130. change_favorite_icon(pte, FAV_ICON_PHONE_BLACK);
  2131. Sendicon(TEXT_LINE0, FAV_ICON_NONE, pte);
  2132. pte->device->missed_call = 0;
  2133. send_led_update(pte, 0);
  2134. return;
  2135. }
  2136. /* Step 1 : Music On Hold for peer, Dialing screen for us */
  2137. static void TransferCallStep1(struct unistimsession *pte)
  2138. {
  2139. struct unistim_subchannel *sub;
  2140. struct unistim_line *p = pte->device->lines;
  2141. sub = p->subs[SUB_REAL];
  2142. if (!sub->owner) {
  2143. ast_log(LOG_WARNING, "Unable to find subchannel for music on hold\n");
  2144. return;
  2145. }
  2146. if (p->subs[SUB_THREEWAY]) {
  2147. if (unistimdebug)
  2148. ast_verb(0, "Transfer canceled, hangup our threeway channel\n");
  2149. if (p->subs[SUB_THREEWAY]->owner)
  2150. ast_queue_hangup_with_cause(p->subs[SUB_THREEWAY]->owner, AST_CAUSE_NORMAL_CLEARING);
  2151. else
  2152. ast_log(LOG_WARNING, "Canceling a threeway channel without owner\n");
  2153. return;
  2154. }
  2155. /* Start music on hold if appropriate */
  2156. if (pte->device->moh)
  2157. ast_log(LOG_WARNING, "Transfer with peer already listening music on hold\n");
  2158. else {
  2159. if (ast_bridged_channel(p->subs[SUB_REAL]->owner)) {
  2160. ast_moh_start(ast_bridged_channel(p->subs[SUB_REAL]->owner),
  2161. pte->device->lines->musicclass, NULL);
  2162. pte->device->moh = 1;
  2163. } else {
  2164. ast_log(LOG_WARNING, "Unable to find peer subchannel for music on hold\n");
  2165. return;
  2166. }
  2167. }
  2168. /* Silence our channel */
  2169. if (!pte->device->silence_generator) {
  2170. pte->device->silence_generator =
  2171. ast_channel_start_silence_generator(p->subs[SUB_REAL]->owner);
  2172. if (pte->device->silence_generator == NULL)
  2173. ast_log(LOG_WARNING, "Unable to start a silence generator.\n");
  2174. else if (unistimdebug)
  2175. ast_verb(0, "Starting silence generator\n");
  2176. }
  2177. handle_dial_page(pte);
  2178. }
  2179. /* From phone to PBX */
  2180. static void HandleCallOutgoing(struct unistimsession *s)
  2181. {
  2182. struct ast_channel *c;
  2183. struct unistim_subchannel *sub;
  2184. pthread_t t;
  2185. s->state = STATE_CALL;
  2186. sub = s->device->lines->subs[SUB_REAL];
  2187. if (!sub) {
  2188. ast_log(LOG_NOTICE, "No available lines on: %s\n", s->device->name);
  2189. return;
  2190. }
  2191. if (!sub->owner) { /* A call is already in progress ? */
  2192. c = unistim_new(sub, AST_STATE_DOWN, NULL); /* No, starting a new one */
  2193. if (c) {
  2194. /* Need to start RTP before calling ast_pbx_run */
  2195. if (!sub->rtp)
  2196. start_rtp(sub);
  2197. send_select_output(s, s->device->output, s->device->volume, MUTE_OFF);
  2198. if (s->device->height == 1) {
  2199. send_text(TEXT_LINE0, TEXT_NORMAL, s, s->device->phone_number);
  2200. } else {
  2201. send_text(TEXT_LINE0, TEXT_NORMAL, s, "Calling :");
  2202. send_text(TEXT_LINE1, TEXT_NORMAL, s, s->device->phone_number);
  2203. send_text(TEXT_LINE2, TEXT_NORMAL, s, "Dialing...");
  2204. }
  2205. send_text_status(s, "Hangup");
  2206. /* start switch */
  2207. if (ast_pthread_create(&t, NULL, unistim_ss, c)) {
  2208. display_last_error("Unable to create switch thread");
  2209. ast_queue_hangup_with_cause(c, AST_CAUSE_SWITCH_CONGESTION);
  2210. }
  2211. } else
  2212. ast_log(LOG_WARNING, "Unable to create channel for %s@%s\n",
  2213. sub->parent->name, s->device->name);
  2214. } else { /* We already have a call, so we switch in a threeway call */
  2215. if (s->device->moh) {
  2216. struct unistim_subchannel *subchannel;
  2217. struct unistim_line *p = s->device->lines;
  2218. subchannel = p->subs[SUB_REAL];
  2219. if (!subchannel->owner) {
  2220. ast_log(LOG_WARNING, "Unable to find subchannel for music on hold\n");
  2221. return;
  2222. }
  2223. if (p->subs[SUB_THREEWAY]) {
  2224. ast_log(LOG_WARNING,
  2225. "Can't transfer while an another transfer is taking place\n");
  2226. return;
  2227. }
  2228. if (!alloc_sub(p, SUB_THREEWAY)) {
  2229. ast_log(LOG_WARNING, "Unable to allocate three-way subchannel\n");
  2230. return;
  2231. }
  2232. /* Stop the silence generator */
  2233. if (s->device->silence_generator) {
  2234. if (unistimdebug)
  2235. ast_verb(0, "Stopping silence generator\n");
  2236. ast_channel_stop_silence_generator(subchannel->owner,
  2237. s->device->silence_generator);
  2238. s->device->silence_generator = NULL;
  2239. }
  2240. send_tone(s, 0, 0);
  2241. /* Make new channel */
  2242. c = unistim_new(p->subs[SUB_THREEWAY], AST_STATE_DOWN, NULL);
  2243. if (!c) {
  2244. ast_log(LOG_WARNING, "Cannot allocate new structure on channel %p\n", p);
  2245. return;
  2246. }
  2247. /* Swap things around between the three-way and real call */
  2248. swap_subs(p, SUB_THREEWAY, SUB_REAL);
  2249. send_select_output(s, s->device->output, s->device->volume, MUTE_OFF);
  2250. if (s->device->height == 1) {
  2251. send_text(TEXT_LINE0, TEXT_NORMAL, s, s->device->phone_number);
  2252. } else {
  2253. send_text(TEXT_LINE0, TEXT_NORMAL, s, "Calling (pre-transfer)");
  2254. send_text(TEXT_LINE1, TEXT_NORMAL, s, s->device->phone_number);
  2255. send_text(TEXT_LINE2, TEXT_NORMAL, s, "Dialing...");
  2256. }
  2257. send_text_status(s, "TransfrCancel");
  2258. if (ast_pthread_create(&t, NULL, unistim_ss, p->subs[SUB_THREEWAY]->owner)) {
  2259. ast_log(LOG_WARNING, "Unable to start simple switch on channel %p\n", p);
  2260. ast_hangup(c);
  2261. return;
  2262. }
  2263. if (unistimdebug)
  2264. ast_verb(0, "Started three way call on channel %p (%s) subchan %u\n",
  2265. p->subs[SUB_THREEWAY]->owner, p->subs[SUB_THREEWAY]->owner->name,
  2266. p->subs[SUB_THREEWAY]->subtype);
  2267. } else
  2268. ast_debug(1, "Current sub [%s] already has owner\n", sub->owner->name);
  2269. }
  2270. return;
  2271. }
  2272. /* From PBX to phone */
  2273. static void HandleCallIncoming(struct unistimsession *s)
  2274. {
  2275. struct unistim_subchannel *sub;
  2276. s->state = STATE_CALL;
  2277. s->device->missed_call = 0;
  2278. send_no_ring(s);
  2279. sub = s->device->lines->subs[SUB_REAL];
  2280. if (!sub) {
  2281. ast_log(LOG_NOTICE, "No available lines on: %s\n", s->device->name);
  2282. return;
  2283. } else if (unistimdebug)
  2284. ast_verb(0, "Handle Call Incoming for %s@%s\n", sub->parent->name,
  2285. s->device->name);
  2286. start_rtp(sub);
  2287. if (!sub->rtp)
  2288. ast_log(LOG_WARNING, "Unable to create channel for %s@%s\n", sub->parent->name,
  2289. s->device->name);
  2290. ast_queue_control(sub->owner, AST_CONTROL_ANSWER);
  2291. send_text(TEXT_LINE2, TEXT_NORMAL, s, "is on-line");
  2292. send_text_status(s, "Hangup Transf");
  2293. send_start_timer(s);
  2294. if ((s->device->output == OUTPUT_HANDSET) &&
  2295. (s->device->receiver_state == STATE_ONHOOK))
  2296. send_select_output(s, OUTPUT_SPEAKER, s->device->volume, MUTE_OFF);
  2297. else
  2298. send_select_output(s, s->device->output, s->device->volume, MUTE_OFF);
  2299. s->device->start_call_timestamp = time(0);
  2300. write_history(s, 'i', 0);
  2301. return;
  2302. }
  2303. static int unistim_do_senddigit(struct unistimsession *pte, char digit)
  2304. {
  2305. struct ast_frame f = { .frametype = AST_FRAME_DTMF, .subclass.integer = digit, .src = "unistim" };
  2306. struct unistim_subchannel *sub;
  2307. sub = pte->device->lines->subs[SUB_REAL];
  2308. if (!sub->owner || sub->alreadygone) {
  2309. ast_log(LOG_WARNING, "Unable to find subchannel in dtmf senddigit\n");
  2310. return -1;
  2311. }
  2312. /* Send DTMF indication _before_ playing sounds */
  2313. ast_queue_frame(sub->owner, &f);
  2314. if (unistimdebug)
  2315. ast_verb(0, "Send Digit %c\n", digit);
  2316. switch (digit) {
  2317. case '0':
  2318. send_tone(pte, 941, 1336);
  2319. break;
  2320. case '1':
  2321. send_tone(pte, 697, 1209);
  2322. break;
  2323. case '2':
  2324. send_tone(pte, 697, 1336);
  2325. break;
  2326. case '3':
  2327. send_tone(pte, 697, 1477);
  2328. break;
  2329. case '4':
  2330. send_tone(pte, 770, 1209);
  2331. break;
  2332. case '5':
  2333. send_tone(pte, 770, 1336);
  2334. break;
  2335. case '6':
  2336. send_tone(pte, 770, 1477);
  2337. break;
  2338. case '7':
  2339. send_tone(pte, 852, 1209);
  2340. break;
  2341. case '8':
  2342. send_tone(pte, 852, 1336);
  2343. break;
  2344. case '9':
  2345. send_tone(pte, 852, 1477);
  2346. break;
  2347. case 'A':
  2348. send_tone(pte, 697, 1633);
  2349. break;
  2350. case 'B':
  2351. send_tone(pte, 770, 1633);
  2352. break;
  2353. case 'C':
  2354. send_tone(pte, 852, 1633);
  2355. break;
  2356. case 'D':
  2357. send_tone(pte, 941, 1633);
  2358. break;
  2359. case '*':
  2360. send_tone(pte, 941, 1209);
  2361. break;
  2362. case '#':
  2363. send_tone(pte, 941, 1477);
  2364. break;
  2365. default:
  2366. send_tone(pte, 500, 2000);
  2367. }
  2368. usleep(150000); /* XXX Less than perfect, blocking an important thread is not a good idea */
  2369. send_tone(pte, 0, 0);
  2370. return 0;
  2371. }
  2372. static void key_call(struct unistimsession *pte, char keycode)
  2373. {
  2374. if ((keycode >= KEY_0) && (keycode <= KEY_SHARP)) {
  2375. if (keycode == KEY_SHARP)
  2376. keycode = '#';
  2377. else if (keycode == KEY_STAR)
  2378. keycode = '*';
  2379. else
  2380. keycode -= 0x10;
  2381. unistim_do_senddigit(pte, keycode);
  2382. return;
  2383. }
  2384. switch (keycode) {
  2385. case KEY_HANGUP:
  2386. case KEY_FUNC1:
  2387. close_call(pte);
  2388. break;
  2389. case KEY_FUNC2:
  2390. TransferCallStep1(pte);
  2391. break;
  2392. case KEY_HEADPHN:
  2393. if (pte->device->output == OUTPUT_HEADPHONE)
  2394. send_select_output(pte, OUTPUT_HANDSET, pte->device->volume, MUTE_OFF);
  2395. else
  2396. send_select_output(pte, OUTPUT_HEADPHONE, pte->device->volume, MUTE_OFF);
  2397. break;
  2398. case KEY_LOUDSPK:
  2399. if (pte->device->output != OUTPUT_SPEAKER)
  2400. send_select_output(pte, OUTPUT_SPEAKER, pte->device->volume, MUTE_OFF);
  2401. else
  2402. send_select_output(pte, pte->device->previous_output, pte->device->volume,
  2403. MUTE_OFF);
  2404. break;
  2405. case KEY_MUTE:
  2406. if (!pte->device->moh) {
  2407. if (pte->device->mute == MUTE_ON)
  2408. send_select_output(pte, pte->device->output, pte->device->volume, MUTE_OFF);
  2409. else
  2410. send_select_output(pte, pte->device->output, pte->device->volume, MUTE_ON);
  2411. break;
  2412. }
  2413. case KEY_ONHOLD:
  2414. {
  2415. struct unistim_subchannel *sub;
  2416. struct ast_channel *bridgepeer = NULL;
  2417. sub = pte->device->lines->subs[SUB_REAL];
  2418. if (!sub->owner) {
  2419. ast_log(LOG_WARNING, "Unable to find subchannel for music on hold\n");
  2420. return;
  2421. }
  2422. if ((bridgepeer = ast_bridged_channel(sub->owner))) {
  2423. if (pte->device->moh) {
  2424. ast_moh_stop(bridgepeer);
  2425. pte->device->moh = 0;
  2426. send_select_output(pte, pte->device->output, pte->device->volume,
  2427. MUTE_OFF);
  2428. } else {
  2429. ast_moh_start(bridgepeer, pte->device->lines->musicclass, NULL);
  2430. pte->device->moh = 1;
  2431. send_select_output(pte, pte->device->output, pte->device->volume,
  2432. MUTE_ON);
  2433. }
  2434. } else
  2435. ast_log(LOG_WARNING,
  2436. "Unable to find peer subchannel for music on hold\n");
  2437. break;
  2438. }
  2439. }
  2440. return;
  2441. }
  2442. static void key_ringing(struct unistimsession *pte, char keycode)
  2443. {
  2444. if (keycode == KEY_FAV0 + pte->device->softkeylinepos) {
  2445. HandleCallIncoming(pte);
  2446. return;
  2447. }
  2448. switch (keycode) {
  2449. case KEY_HANGUP:
  2450. case KEY_FUNC4:
  2451. IgnoreCall(pte);
  2452. break;
  2453. case KEY_FUNC1:
  2454. HandleCallIncoming(pte);
  2455. break;
  2456. }
  2457. return;
  2458. }
  2459. static void Keyfavorite(struct unistimsession *pte, char keycode)
  2460. {
  2461. int fav;
  2462. if ((keycode < KEY_FAV1) && (keycode > KEY_FAV5)) {
  2463. ast_log(LOG_WARNING, "It's not a favorite key\n");
  2464. return;
  2465. }
  2466. if (keycode == KEY_FAV0)
  2467. return;
  2468. fav = keycode - KEY_FAV0;
  2469. if (pte->device->softkeyicon[fav] == 0)
  2470. return;
  2471. ast_copy_string(pte->device->phone_number, pte->device->softkeynumber[fav],
  2472. sizeof(pte->device->phone_number));
  2473. HandleCallOutgoing(pte);
  2474. return;
  2475. }
  2476. static void key_dial_page(struct unistimsession *pte, char keycode)
  2477. {
  2478. if (keycode == KEY_FUNC3) {
  2479. if (pte->device->size_phone_number <= 1)
  2480. keycode = KEY_FUNC4;
  2481. else {
  2482. pte->device->size_phone_number -= 2;
  2483. keycode = pte->device->phone_number[pte->device->size_phone_number] + 0x10;
  2484. }
  2485. }
  2486. if ((keycode >= KEY_0) && (keycode <= KEY_SHARP)) {
  2487. char tmpbuf[] = "Number : ...............";
  2488. unsigned int i = 0; /* changed to unsigned due to weird gcc 4.8.1 compiler complaint */
  2489. if (pte->device->size_phone_number >= 15)
  2490. return;
  2491. if (pte->device->size_phone_number == 0)
  2492. send_tone(pte, 0, 0);
  2493. while (i < pte->device->size_phone_number) {
  2494. tmpbuf[i + 9] = pte->device->phone_number[i];
  2495. i++;
  2496. }
  2497. if (keycode == KEY_SHARP)
  2498. keycode = '#';
  2499. else if (keycode == KEY_STAR)
  2500. keycode = '*';
  2501. else
  2502. keycode -= 0x10;
  2503. tmpbuf[i + 9] = keycode;
  2504. pte->device->phone_number[i] = keycode;
  2505. pte->device->size_phone_number++;
  2506. pte->device->phone_number[i + 1] = 0;
  2507. if (pte->device->height == 1) {
  2508. send_text(TEXT_LINE0, TEXT_NORMAL, pte, tmpbuf);
  2509. } else {
  2510. send_text(TEXT_LINE2, TEXT_NORMAL, pte, tmpbuf);
  2511. }
  2512. send_blink_cursor(pte);
  2513. send_cursor_pos(pte, (unsigned char) (TEXT_LINE2 + 0x0a + i));
  2514. return;
  2515. }
  2516. if (keycode == KEY_FUNC4) {
  2517. pte->device->size_phone_number = 0;
  2518. if (pte->device->height == 1) {
  2519. send_text(TEXT_LINE0, TEXT_NORMAL, pte, "Number : ...............");
  2520. send_blink_cursor(pte);
  2521. send_cursor_pos(pte, TEXT_LINE0 + 0x09);
  2522. } else {
  2523. send_text(TEXT_LINE2, TEXT_NORMAL, pte, "Number : ...............");
  2524. send_blink_cursor(pte);
  2525. send_cursor_pos(pte, TEXT_LINE2 + 0x09);
  2526. }
  2527. return;
  2528. }
  2529. if (pte->device->call_forward[0] == -1) {
  2530. if (keycode == KEY_FUNC1) {
  2531. ast_copy_string(pte->device->call_forward, pte->device->phone_number,
  2532. sizeof(pte->device->call_forward));
  2533. show_main_page(pte);
  2534. } else if ((keycode == KEY_FUNC2) || (keycode == KEY_HANGUP)) {
  2535. pte->device->call_forward[0] = '\0';
  2536. show_main_page(pte);
  2537. }
  2538. return;
  2539. }
  2540. switch (keycode) {
  2541. case KEY_FUNC2:
  2542. if (ast_strlen_zero(pte->device->redial_number))
  2543. break;
  2544. ast_copy_string(pte->device->phone_number, pte->device->redial_number,
  2545. sizeof(pte->device->phone_number));
  2546. case KEY_FUNC1:
  2547. HandleCallOutgoing(pte);
  2548. break;
  2549. case KEY_HANGUP:
  2550. if (pte->device->lines->subs[SUB_REAL]->owner) {
  2551. /* Stop the silence generator */
  2552. if (pte->device->silence_generator) {
  2553. if (unistimdebug)
  2554. ast_verb(0, "Stopping silence generator\n");
  2555. ast_channel_stop_silence_generator(pte->device->lines->subs[SUB_REAL]->
  2556. owner, pte->device->silence_generator);
  2557. pte->device->silence_generator = NULL;
  2558. }
  2559. send_tone(pte, 0, 0);
  2560. ast_moh_stop(ast_bridged_channel(pte->device->lines->subs[SUB_REAL]->owner));
  2561. pte->device->moh = 0;
  2562. pte->state = STATE_CALL;
  2563. if (pte->device->height == 1) {
  2564. send_text(TEXT_LINE0, TEXT_NORMAL, pte, "Dial Cancel,back to priv. call.");
  2565. } else {
  2566. send_text(TEXT_LINE0, TEXT_NORMAL, pte, "Dialing canceled,");
  2567. send_text(TEXT_LINE1, TEXT_NORMAL, pte, "switching back to");
  2568. send_text(TEXT_LINE2, TEXT_NORMAL, pte, "previous call.");
  2569. }
  2570. send_text_status(pte, "Hangup Transf");
  2571. } else
  2572. show_main_page(pte);
  2573. break;
  2574. case KEY_FAV1:
  2575. case KEY_FAV2:
  2576. case KEY_FAV3:
  2577. case KEY_FAV4:
  2578. case KEY_FAV5:
  2579. Keyfavorite(pte, keycode);
  2580. break;
  2581. case KEY_LOUDSPK:
  2582. if (pte->device->output == OUTPUT_SPEAKER) {
  2583. if (pte->device->receiver_state == STATE_OFFHOOK)
  2584. send_select_output(pte, pte->device->previous_output, pte->device->volume,
  2585. MUTE_OFF);
  2586. else
  2587. show_main_page(pte);
  2588. } else
  2589. send_select_output(pte, OUTPUT_SPEAKER, pte->device->volume, MUTE_OFF);
  2590. break;
  2591. case KEY_HEADPHN:
  2592. if (pte->device->output == OUTPUT_HEADPHONE) {
  2593. if (pte->device->receiver_state == STATE_OFFHOOK)
  2594. send_select_output(pte, OUTPUT_HANDSET, pte->device->volume, MUTE_OFF);
  2595. else
  2596. show_main_page(pte);
  2597. } else
  2598. send_select_output(pte, OUTPUT_HEADPHONE, pte->device->volume, MUTE_OFF);
  2599. break;
  2600. }
  2601. return;
  2602. }
  2603. #define SELECTCODEC_START_ENTRY_POS 15
  2604. #define SELECTCODEC_MAX_LENGTH 2
  2605. #define SELECTCODEC_MSG "Codec number : .."
  2606. static void HandleSelectCodec(struct unistimsession *pte)
  2607. {
  2608. char buf[30], buf2[5];
  2609. pte->state = STATE_SELECTCODEC;
  2610. strcpy(buf, "Using codec ");
  2611. sprintf(buf2, "%d", pte->device->codec_number);
  2612. strcat(buf, buf2);
  2613. strcat(buf, " (G711u=0,");
  2614. send_text(TEXT_LINE0, TEXT_NORMAL, pte, buf);
  2615. send_text(TEXT_LINE1, TEXT_NORMAL, pte, "G723=4,G711a=8,G729A=18)");
  2616. send_text(TEXT_LINE2, TEXT_INVERSE, pte, SELECTCODEC_MSG);
  2617. send_blink_cursor(pte);
  2618. send_cursor_pos(pte, TEXT_LINE2 + SELECTCODEC_START_ENTRY_POS);
  2619. pte->size_buff_entry = 0;
  2620. send_text_status(pte, "Select BackSpcErase Cancel");
  2621. return;
  2622. }
  2623. static void key_select_codec(struct unistimsession *pte, char keycode)
  2624. {
  2625. if (keycode == KEY_FUNC2) {
  2626. if (pte->size_buff_entry <= 1)
  2627. keycode = KEY_FUNC3;
  2628. else {
  2629. pte->size_buff_entry -= 2;
  2630. keycode = pte->buff_entry[pte->size_buff_entry] + 0x10;
  2631. }
  2632. }
  2633. if ((keycode >= KEY_0) && (keycode <= KEY_9)) {
  2634. char tmpbuf[] = SELECTCODEC_MSG;
  2635. int i = 0;
  2636. if (pte->size_buff_entry >= SELECTCODEC_MAX_LENGTH)
  2637. return;
  2638. while (i < pte->size_buff_entry) {
  2639. tmpbuf[i + SELECTCODEC_START_ENTRY_POS] = pte->buff_entry[i];
  2640. i++;
  2641. }
  2642. tmpbuf[i + SELECTCODEC_START_ENTRY_POS] = keycode - 0x10;
  2643. pte->buff_entry[i] = keycode - 0x10;
  2644. pte->size_buff_entry++;
  2645. send_text(TEXT_LINE2, TEXT_INVERSE, pte, tmpbuf);
  2646. send_blink_cursor(pte);
  2647. send_cursor_pos(pte,
  2648. (unsigned char) (TEXT_LINE2 + SELECTCODEC_START_ENTRY_POS + 1 + i));
  2649. return;
  2650. }
  2651. switch (keycode) {
  2652. case KEY_FUNC1:
  2653. if (pte->size_buff_entry == 1)
  2654. pte->device->codec_number = pte->buff_entry[0] - 48;
  2655. else if (pte->size_buff_entry == 2)
  2656. pte->device->codec_number =
  2657. ((pte->buff_entry[0] - 48) * 10) + (pte->buff_entry[1] - 48);
  2658. show_main_page(pte);
  2659. break;
  2660. case KEY_FUNC3:
  2661. pte->size_buff_entry = 0;
  2662. send_text(TEXT_LINE2, TEXT_INVERSE, pte, SELECTCODEC_MSG);
  2663. send_blink_cursor(pte);
  2664. send_cursor_pos(pte, TEXT_LINE2 + SELECTCODEC_START_ENTRY_POS);
  2665. break;
  2666. case KEY_HANGUP:
  2667. case KEY_FUNC4:
  2668. show_main_page(pte);
  2669. break;
  2670. }
  2671. return;
  2672. }
  2673. #define SELECTEXTENSION_START_ENTRY_POS 0
  2674. #define SELECTEXTENSION_MAX_LENGTH 10
  2675. #define SELECTEXTENSION_MSG ".........."
  2676. static void ShowExtensionPage(struct unistimsession *pte)
  2677. {
  2678. pte->state = STATE_EXTENSION;
  2679. send_text(TEXT_LINE0, TEXT_NORMAL, pte, "Please enter a Terminal");
  2680. send_text(TEXT_LINE1, TEXT_NORMAL, pte, "Number (TN) :");
  2681. send_text(TEXT_LINE2, TEXT_NORMAL, pte, SELECTEXTENSION_MSG);
  2682. send_blink_cursor(pte);
  2683. send_cursor_pos(pte, TEXT_LINE2 + SELECTEXTENSION_START_ENTRY_POS);
  2684. send_text_status(pte, "Enter BackSpcErase");
  2685. pte->size_buff_entry = 0;
  2686. return;
  2687. }
  2688. static void key_select_extension(struct unistimsession *pte, char keycode)
  2689. {
  2690. if (keycode == KEY_FUNC2) {
  2691. if (pte->size_buff_entry <= 1)
  2692. keycode = KEY_FUNC3;
  2693. else {
  2694. pte->size_buff_entry -= 2;
  2695. keycode = pte->buff_entry[pte->size_buff_entry] + 0x10;
  2696. }
  2697. }
  2698. if ((keycode >= KEY_0) && (keycode <= KEY_9)) {
  2699. char tmpbuf[] = SELECTEXTENSION_MSG;
  2700. int i = 0;
  2701. if (pte->size_buff_entry >= SELECTEXTENSION_MAX_LENGTH)
  2702. return;
  2703. while (i < pte->size_buff_entry) {
  2704. tmpbuf[i + SELECTEXTENSION_START_ENTRY_POS] = pte->buff_entry[i];
  2705. i++;
  2706. }
  2707. tmpbuf[i + SELECTEXTENSION_START_ENTRY_POS] = keycode - 0x10;
  2708. pte->buff_entry[i] = keycode - 0x10;
  2709. pte->size_buff_entry++;
  2710. send_text(TEXT_LINE2, TEXT_NORMAL, pte, tmpbuf);
  2711. send_blink_cursor(pte);
  2712. send_cursor_pos(pte,
  2713. (unsigned char) (TEXT_LINE2 + SELECTEXTENSION_START_ENTRY_POS + 1 +
  2714. i));
  2715. return;
  2716. }
  2717. switch (keycode) {
  2718. case KEY_FUNC1:
  2719. if (pte->size_buff_entry < 1)
  2720. return;
  2721. if (autoprovisioning == AUTOPROVISIONING_TN) {
  2722. struct unistim_device *d;
  2723. /* First step : looking for this TN in our device list */
  2724. ast_mutex_lock(&devicelock);
  2725. d = devices;
  2726. pte->buff_entry[pte->size_buff_entry] = '\0';
  2727. while (d) {
  2728. if (d->id[0] == 'T') { /* It's a TN device ? */
  2729. /* It's the TN we're looking for ? */
  2730. if (!strcmp((d->id) + 1, pte->buff_entry)) {
  2731. pte->device = d;
  2732. d->session = pte;
  2733. d->codec_number = DEFAULT_CODEC;
  2734. d->pos_fav = 0;
  2735. d->missed_call = 0;
  2736. d->receiver_state = STATE_ONHOOK;
  2737. strcpy(d->id, pte->macaddr);
  2738. pte->device->extension_number[0] = 'T';
  2739. pte->device->extension = EXTENSION_TN;
  2740. ast_copy_string((pte->device->extension_number) + 1,
  2741. pte->buff_entry, pte->size_buff_entry + 1);
  2742. ast_mutex_unlock(&devicelock);
  2743. show_main_page(pte);
  2744. refresh_all_favorite(pte);
  2745. return;
  2746. }
  2747. }
  2748. d = d->next;
  2749. }
  2750. ast_mutex_unlock(&devicelock);
  2751. send_text(TEXT_LINE0, TEXT_NORMAL, pte, "Invalid Terminal Number.");
  2752. send_text(TEXT_LINE1, TEXT_NORMAL, pte, "Please try again :");
  2753. send_cursor_pos(pte,
  2754. (unsigned char) (TEXT_LINE2 + SELECTEXTENSION_START_ENTRY_POS +
  2755. pte->size_buff_entry));
  2756. send_blink_cursor(pte);
  2757. } else {
  2758. ast_copy_string(pte->device->extension_number, pte->buff_entry,
  2759. pte->size_buff_entry + 1);
  2760. if (RegisterExtension(pte)) {
  2761. send_text(TEXT_LINE0, TEXT_NORMAL, pte, "Invalid extension.");
  2762. send_text(TEXT_LINE1, TEXT_NORMAL, pte, "Please try again :");
  2763. send_cursor_pos(pte,
  2764. (unsigned char) (TEXT_LINE2 +
  2765. SELECTEXTENSION_START_ENTRY_POS +
  2766. pte->size_buff_entry));
  2767. send_blink_cursor(pte);
  2768. } else
  2769. show_main_page(pte);
  2770. }
  2771. break;
  2772. case KEY_FUNC3:
  2773. pte->size_buff_entry = 0;
  2774. send_text(TEXT_LINE2, TEXT_NORMAL, pte, SELECTEXTENSION_MSG);
  2775. send_blink_cursor(pte);
  2776. send_cursor_pos(pte, TEXT_LINE2 + SELECTEXTENSION_START_ENTRY_POS);
  2777. break;
  2778. }
  2779. return;
  2780. }
  2781. static int ReformatNumber(char *number)
  2782. {
  2783. int pos = 0, i = 0, size = strlen(number);
  2784. for (; i < size; i++) {
  2785. if ((number[i] >= '0') && (number[i] <= '9')) {
  2786. if (i == pos) {
  2787. pos++;
  2788. continue;
  2789. }
  2790. number[pos] = number[i];
  2791. pos++;
  2792. }
  2793. }
  2794. number[pos] = 0;
  2795. return pos;
  2796. }
  2797. static void show_entry_history(struct unistimsession *pte, FILE ** f)
  2798. {
  2799. char line[TEXT_LENGTH_MAX + 1], status[STATUS_LENGTH_MAX + 1], func1[10], func2[10],
  2800. func3[10];
  2801. if (fread(line, TEXT_LENGTH_MAX, 1, *f) != 1) {
  2802. display_last_error("Can't read history date entry");
  2803. fclose(*f);
  2804. return;
  2805. }
  2806. line[sizeof(line) - 1] = '\0';
  2807. send_text(TEXT_LINE0, TEXT_NORMAL, pte, line);
  2808. if (fread(line, TEXT_LENGTH_MAX, 1, *f) != 1) {
  2809. display_last_error("Can't read callerid entry");
  2810. fclose(*f);
  2811. return;
  2812. }
  2813. line[sizeof(line) - 1] = '\0';
  2814. ast_copy_string(pte->device->lst_cid, line, sizeof(pte->device->lst_cid));
  2815. send_text(TEXT_LINE1, TEXT_NORMAL, pte, line);
  2816. if (fread(line, TEXT_LENGTH_MAX, 1, *f) != 1) {
  2817. display_last_error("Can't read callername entry");
  2818. fclose(*f);
  2819. return;
  2820. }
  2821. line[sizeof(line) - 1] = '\0';
  2822. send_text(TEXT_LINE2, TEXT_NORMAL, pte, line);
  2823. fclose(*f);
  2824. snprintf(line, sizeof(line), "Call %03d/%03d", pte->buff_entry[2],
  2825. pte->buff_entry[1]);
  2826. send_texttitle(pte, line);
  2827. if (pte->buff_entry[2] == 1)
  2828. strcpy(func1, " ");
  2829. else
  2830. strcpy(func1, "Prvious");
  2831. if (pte->buff_entry[2] >= pte->buff_entry[1])
  2832. strcpy(func2, " ");
  2833. else
  2834. strcpy(func2, "Next ");
  2835. if (ReformatNumber(pte->device->lst_cid))
  2836. strcpy(func3, "Redial ");
  2837. else
  2838. strcpy(func3, " ");
  2839. snprintf(status, sizeof(status), "%s%s%sCancel", func1, func2, func3);
  2840. send_text_status(pte, status);
  2841. }
  2842. static char OpenHistory(struct unistimsession *pte, char way, FILE ** f)
  2843. {
  2844. char tmp[AST_CONFIG_MAX_PATH];
  2845. char count;
  2846. snprintf(tmp, sizeof(tmp), "%s/%s/%s-%c.csv", ast_config_AST_LOG_DIR,
  2847. USTM_LOG_DIR, pte->device->name, way);
  2848. *f = fopen(tmp, "r");
  2849. if (!*f) {
  2850. display_last_error("Unable to open history file");
  2851. return 0;
  2852. }
  2853. if (fread(&count, 1, 1, *f) != 1) {
  2854. display_last_error("Unable to read history header - display.");
  2855. fclose(*f);
  2856. return 0;
  2857. }
  2858. if (count > MAX_ENTRY_LOG) {
  2859. ast_log(LOG_WARNING, "Invalid count in history header of %s (%d max %d)\n", tmp,
  2860. count, MAX_ENTRY_LOG);
  2861. fclose(*f);
  2862. return 0;
  2863. }
  2864. return count;
  2865. }
  2866. static void show_history(struct unistimsession *pte, char way)
  2867. {
  2868. FILE *f;
  2869. char count;
  2870. if (!pte->device)
  2871. return;
  2872. if (!pte->device->callhistory)
  2873. return;
  2874. count = OpenHistory(pte, way, &f);
  2875. if (!count)
  2876. return;
  2877. pte->buff_entry[0] = way;
  2878. pte->buff_entry[1] = count;
  2879. pte->buff_entry[2] = 1;
  2880. show_entry_history(pte, &f);
  2881. pte->state = STATE_HISTORY;
  2882. }
  2883. static void show_main_page(struct unistimsession *pte)
  2884. {
  2885. char tmpbuf[TEXT_LENGTH_MAX + 1];
  2886. if ((pte->device->extension == EXTENSION_ASK) &&
  2887. (ast_strlen_zero(pte->device->extension_number))) {
  2888. ShowExtensionPage(pte);
  2889. return;
  2890. }
  2891. pte->state = STATE_MAINPAGE;
  2892. send_tone(pte, 0, 0);
  2893. send_select_output(pte, pte->device->output, pte->device->volume, MUTE_ON_DISCRET);
  2894. pte->device->lines->lastmsgssent = 0;
  2895. send_favorite(pte->device->softkeylinepos, FAV_ICON_ONHOOK_BLACK, pte,
  2896. pte->device->softkeylabel[pte->device->softkeylinepos]);
  2897. if (!ast_strlen_zero(pte->device->call_forward)) {
  2898. if (pte->device->height == 1) {
  2899. send_text(TEXT_LINE0, TEXT_NORMAL, pte, "Forwarding ON");
  2900. } else {
  2901. send_text(TEXT_LINE0, TEXT_NORMAL, pte, "Call forwarded to :");
  2902. send_text(TEXT_LINE1, TEXT_NORMAL, pte, pte->device->call_forward);
  2903. }
  2904. Sendicon(TEXT_LINE0, FAV_ICON_REFLECT + FAV_BLINK_SLOW, pte);
  2905. send_text_status(pte, "Dial Redial NoForwd");
  2906. } else {
  2907. if ((pte->device->extension == EXTENSION_ASK) ||
  2908. (pte->device->extension == EXTENSION_TN))
  2909. send_text_status(pte, "Dial Redial ForwardUnregis");
  2910. else
  2911. send_text_status(pte, "Dial Redial Forward");
  2912. send_text(TEXT_LINE1, TEXT_NORMAL, pte, pte->device->maintext1);
  2913. if (pte->device->missed_call == 0)
  2914. send_text(TEXT_LINE0, TEXT_NORMAL, pte, pte->device->maintext0);
  2915. else {
  2916. sprintf(tmpbuf, "%d unanswered call(s)", pte->device->missed_call);
  2917. send_text(TEXT_LINE0, TEXT_NORMAL, pte, tmpbuf);
  2918. Sendicon(TEXT_LINE0, FAV_ICON_CALL_CENTER + FAV_BLINK_SLOW, pte);
  2919. }
  2920. }
  2921. if (ast_strlen_zero(pte->device->maintext2)) {
  2922. strcpy(tmpbuf, "IP : ");
  2923. strcat(tmpbuf, ast_inet_ntoa(pte->sin.sin_addr));
  2924. send_text(TEXT_LINE2, TEXT_NORMAL, pte, tmpbuf);
  2925. } else
  2926. send_text(TEXT_LINE2, TEXT_NORMAL, pte, pte->device->maintext2);
  2927. send_texttitle(pte, pte->device->titledefault);
  2928. change_favorite_icon(pte, FAV_ICON_ONHOOK_BLACK);
  2929. }
  2930. static void key_main_page(struct unistimsession *pte, char keycode)
  2931. {
  2932. if (pte->device->missed_call) {
  2933. Sendicon(TEXT_LINE0, FAV_ICON_NONE, pte);
  2934. pte->device->missed_call = 0;
  2935. }
  2936. if ((keycode >= KEY_0) && (keycode <= KEY_SHARP)) {
  2937. handle_dial_page(pte);
  2938. key_dial_page(pte, keycode);
  2939. return;
  2940. }
  2941. switch (keycode) {
  2942. case KEY_FUNC1:
  2943. handle_dial_page(pte);
  2944. break;
  2945. case KEY_FUNC2:
  2946. if (ast_strlen_zero(pte->device->redial_number))
  2947. break;
  2948. if ((pte->device->output == OUTPUT_HANDSET) &&
  2949. (pte->device->receiver_state == STATE_ONHOOK))
  2950. send_select_output(pte, OUTPUT_SPEAKER, pte->device->volume, MUTE_OFF);
  2951. else
  2952. send_select_output(pte, pte->device->output, pte->device->volume, MUTE_OFF);
  2953. ast_copy_string(pte->device->phone_number, pte->device->redial_number,
  2954. sizeof(pte->device->phone_number));
  2955. HandleCallOutgoing(pte);
  2956. break;
  2957. case KEY_FUNC3:
  2958. if (!ast_strlen_zero(pte->device->call_forward)) {
  2959. /* Cancel call forwarding */
  2960. memmove(pte->device->call_forward + 1, pte->device->call_forward,
  2961. sizeof(pte->device->call_forward) - 1);
  2962. pte->device->call_forward[0] = '\0';
  2963. Sendicon(TEXT_LINE0, FAV_ICON_NONE, pte);
  2964. pte->device->output = OUTPUT_HANDSET; /* Seems to be reseted somewhere */
  2965. show_main_page(pte);
  2966. break;
  2967. }
  2968. pte->device->call_forward[0] = -1;
  2969. handle_dial_page(pte);
  2970. break;
  2971. case KEY_FUNC4:
  2972. if (pte->device->extension == EXTENSION_ASK) {
  2973. UnregisterExtension(pte);
  2974. pte->device->extension_number[0] = '\0';
  2975. ShowExtensionPage(pte);
  2976. } else if (pte->device->extension == EXTENSION_TN) {
  2977. ast_mutex_lock(&devicelock);
  2978. strcpy(pte->device->id, pte->device->extension_number);
  2979. pte->buff_entry[0] = '\0';
  2980. pte->size_buff_entry = 0;
  2981. pte->device->session = NULL;
  2982. pte->device = NULL;
  2983. ast_mutex_unlock(&devicelock);
  2984. ShowExtensionPage(pte);
  2985. }
  2986. break;
  2987. case KEY_FAV0:
  2988. handle_dial_page(pte);
  2989. break;
  2990. case KEY_FAV1:
  2991. case KEY_FAV2:
  2992. case KEY_FAV3:
  2993. case KEY_FAV4:
  2994. case KEY_FAV5:
  2995. if ((pte->device->output == OUTPUT_HANDSET) &&
  2996. (pte->device->receiver_state == STATE_ONHOOK))
  2997. send_select_output(pte, OUTPUT_SPEAKER, pte->device->volume, MUTE_OFF);
  2998. else
  2999. send_select_output(pte, pte->device->output, pte->device->volume, MUTE_OFF);
  3000. Keyfavorite(pte, keycode);
  3001. break;
  3002. case KEY_CONF:
  3003. HandleSelectCodec(pte);
  3004. break;
  3005. case KEY_LOUDSPK:
  3006. send_select_output(pte, OUTPUT_SPEAKER, pte->device->volume, MUTE_OFF);
  3007. handle_dial_page(pte);
  3008. break;
  3009. case KEY_HEADPHN:
  3010. send_select_output(pte, OUTPUT_HEADPHONE, pte->device->volume, MUTE_OFF);
  3011. handle_dial_page(pte);
  3012. break;
  3013. case KEY_SNDHIST:
  3014. show_history(pte, 'o');
  3015. break;
  3016. case KEY_RCVHIST:
  3017. show_history(pte, 'i');
  3018. break;
  3019. }
  3020. return;
  3021. }
  3022. static void key_history(struct unistimsession *pte, char keycode)
  3023. {
  3024. FILE *f;
  3025. char count;
  3026. long offset;
  3027. switch (keycode) {
  3028. case KEY_UP:
  3029. case KEY_LEFT:
  3030. case KEY_FUNC1:
  3031. if (pte->buff_entry[2] <= 1)
  3032. return;
  3033. pte->buff_entry[2]--;
  3034. count = OpenHistory(pte, pte->buff_entry[0], &f);
  3035. if (!count)
  3036. return;
  3037. offset = ((pte->buff_entry[2] - 1) * TEXT_LENGTH_MAX * 3);
  3038. if (fseek(f, offset, SEEK_CUR)) {
  3039. display_last_error("Unable to seek history entry.");
  3040. fclose(f);
  3041. return;
  3042. }
  3043. show_entry_history(pte, &f);
  3044. break;
  3045. case KEY_DOWN:
  3046. case KEY_RIGHT:
  3047. case KEY_FUNC2:
  3048. if (pte->buff_entry[2] >= pte->buff_entry[1])
  3049. return;
  3050. pte->buff_entry[2]++;
  3051. count = OpenHistory(pte, pte->buff_entry[0], &f);
  3052. if (!count)
  3053. return;
  3054. offset = ((pte->buff_entry[2] - 1) * TEXT_LENGTH_MAX * 3);
  3055. if (fseek(f, offset, SEEK_CUR)) {
  3056. display_last_error("Unable to seek history entry.");
  3057. fclose(f);
  3058. return;
  3059. }
  3060. show_entry_history(pte, &f);
  3061. break;
  3062. case KEY_FUNC3:
  3063. if (!ReformatNumber(pte->device->lst_cid))
  3064. break;
  3065. ast_copy_string(pte->device->redial_number, pte->device->lst_cid,
  3066. sizeof(pte->device->redial_number));
  3067. key_main_page(pte, KEY_FUNC2);
  3068. break;
  3069. case KEY_FUNC4:
  3070. case KEY_HANGUP:
  3071. show_main_page(pte);
  3072. break;
  3073. case KEY_SNDHIST:
  3074. if (pte->buff_entry[0] == 'i')
  3075. show_history(pte, 'o');
  3076. else
  3077. show_main_page(pte);
  3078. break;
  3079. case KEY_RCVHIST:
  3080. if (pte->buff_entry[0] == 'i')
  3081. show_main_page(pte);
  3082. else
  3083. show_history(pte, 'i');
  3084. break;
  3085. }
  3086. return;
  3087. }
  3088. static void init_phone_step2(struct unistimsession *pte)
  3089. {
  3090. BUFFSEND;
  3091. if (unistimdebug)
  3092. ast_verb(0, "Sending S4\n");
  3093. memcpy(buffsend + SIZE_HEADER, packet_send_s4, sizeof(packet_send_s4));
  3094. send_client(SIZE_HEADER + sizeof(packet_send_s4), buffsend, pte);
  3095. send_date_time2(pte);
  3096. send_date_time3(pte);
  3097. if (unistimdebug)
  3098. ast_verb(0, "Sending S7\n");
  3099. memcpy(buffsend + SIZE_HEADER, packet_send_S7, sizeof(packet_send_S7));
  3100. send_client(SIZE_HEADER + sizeof(packet_send_S7), buffsend, pte);
  3101. if (unistimdebug)
  3102. ast_verb(0, "Sending Contrast\n");
  3103. memcpy(buffsend + SIZE_HEADER, packet_send_Contrast, sizeof(packet_send_Contrast));
  3104. if (pte->device != NULL)
  3105. buffsend[9] = pte->device->contrast;
  3106. send_client(SIZE_HEADER + sizeof(packet_send_Contrast), buffsend, pte);
  3107. if (unistimdebug)
  3108. ast_verb(0, "Sending S9\n");
  3109. memcpy(buffsend + SIZE_HEADER, packet_send_s9, sizeof(packet_send_s9));
  3110. send_client(SIZE_HEADER + sizeof(packet_send_s9), buffsend, pte);
  3111. send_no_ring(pte);
  3112. if (unistimdebug)
  3113. ast_verb(0, "Sending S7\n");
  3114. memcpy(buffsend + SIZE_HEADER, packet_send_S7, sizeof(packet_send_S7));
  3115. send_client(SIZE_HEADER + sizeof(packet_send_S7), buffsend, pte);
  3116. send_led_update(pte, 0);
  3117. send_ping(pte);
  3118. if (pte->state < STATE_MAINPAGE) {
  3119. if (autoprovisioning == AUTOPROVISIONING_TN) {
  3120. ShowExtensionPage(pte);
  3121. return;
  3122. } else {
  3123. int i;
  3124. char tmp[30];
  3125. for (i = 1; i < 6; i++)
  3126. send_favorite(i, 0, pte, "");
  3127. send_text(TEXT_LINE0, TEXT_NORMAL, pte, "Sorry, this phone is not");
  3128. send_text(TEXT_LINE1, TEXT_NORMAL, pte, "registered in unistim.cfg");
  3129. strcpy(tmp, "MAC = ");
  3130. strcat(tmp, pte->macaddr);
  3131. send_text(TEXT_LINE2, TEXT_NORMAL, pte, tmp);
  3132. send_text_status(pte, "");
  3133. send_texttitle(pte, "UNISTIM for*");
  3134. return;
  3135. }
  3136. }
  3137. show_main_page(pte);
  3138. refresh_all_favorite(pte);
  3139. if (unistimdebug)
  3140. ast_verb(0, "Sending arrow\n");
  3141. memcpy(buffsend + SIZE_HEADER, packet_send_arrow, sizeof(packet_send_arrow));
  3142. send_client(SIZE_HEADER + sizeof(packet_send_arrow), buffsend, pte);
  3143. return;
  3144. }
  3145. static void process_request(int size, unsigned char *buf, struct unistimsession *pte)
  3146. {
  3147. char tmpbuf[255];
  3148. if (memcmp
  3149. (buf + SIZE_HEADER, packet_recv_resume_connection_with_server,
  3150. sizeof(packet_recv_resume_connection_with_server)) == 0) {
  3151. rcv_resume_connection_with_server(pte);
  3152. return;
  3153. }
  3154. if (memcmp(buf + SIZE_HEADER, packet_recv_firm_version, sizeof(packet_recv_firm_version)) ==
  3155. 0) {
  3156. buf[size] = 0;
  3157. if (unistimdebug)
  3158. ast_verb(0, "Got the firmware version : '%s'\n", buf + 13);
  3159. init_phone_step2(pte);
  3160. return;
  3161. }
  3162. if (memcmp(buf + SIZE_HEADER, packet_recv_mac_addr, sizeof(packet_recv_mac_addr)) == 0) {
  3163. rcv_mac_addr(pte, buf);
  3164. return;
  3165. }
  3166. if (memcmp(buf + SIZE_HEADER, packet_recv_r2, sizeof(packet_recv_r2)) == 0) {
  3167. if (unistimdebug)
  3168. ast_verb(0, "R2 received\n");
  3169. return;
  3170. }
  3171. if (pte->state < STATE_MAINPAGE) {
  3172. if (unistimdebug)
  3173. ast_verb(0, "Request not authorized in this state\n");
  3174. return;
  3175. }
  3176. if (!memcmp(buf + SIZE_HEADER, packet_recv_pressed_key, sizeof(packet_recv_pressed_key))) {
  3177. char keycode = buf[13];
  3178. if (unistimdebug)
  3179. ast_verb(0, "Key pressed : keycode = 0x%.2x - current state : %d\n", (unsigned)keycode,
  3180. pte->state);
  3181. switch (pte->state) {
  3182. case STATE_INIT:
  3183. if (unistimdebug)
  3184. ast_verb(0, "No keys allowed in the init state\n");
  3185. break;
  3186. case STATE_AUTHDENY:
  3187. if (unistimdebug)
  3188. ast_verb(0, "No keys allowed in authdeny state\n");
  3189. break;
  3190. case STATE_MAINPAGE:
  3191. key_main_page(pte, keycode);
  3192. break;
  3193. case STATE_DIALPAGE:
  3194. key_dial_page(pte, keycode);
  3195. break;
  3196. case STATE_RINGING:
  3197. key_ringing(pte, keycode);
  3198. break;
  3199. case STATE_CALL:
  3200. key_call(pte, keycode);
  3201. break;
  3202. case STATE_EXTENSION:
  3203. key_select_extension(pte, keycode);
  3204. break;
  3205. case STATE_SELECTCODEC:
  3206. key_select_codec(pte, keycode);
  3207. break;
  3208. case STATE_HISTORY:
  3209. key_history(pte, keycode);
  3210. break;
  3211. default:
  3212. ast_log(LOG_WARNING, "Key : Unknown state\n");
  3213. }
  3214. return;
  3215. }
  3216. if (memcmp(buf + SIZE_HEADER, packet_recv_pick_up, sizeof(packet_recv_pick_up)) == 0) {
  3217. if (unistimdebug)
  3218. ast_verb(0, "Handset off hook\n");
  3219. if (!pte->device) /* We are not yet registered (asking for a TN in AUTOPROVISIONING_TN) */
  3220. return;
  3221. pte->device->receiver_state = STATE_OFFHOOK;
  3222. if (pte->device->output == OUTPUT_HEADPHONE)
  3223. send_select_output(pte, OUTPUT_HEADPHONE, pte->device->volume, MUTE_OFF);
  3224. else
  3225. send_select_output(pte, OUTPUT_HANDSET, pte->device->volume, MUTE_OFF);
  3226. if (pte->state == STATE_RINGING)
  3227. HandleCallIncoming(pte);
  3228. else if ((pte->state == STATE_DIALPAGE) || (pte->state == STATE_CALL))
  3229. send_select_output(pte, OUTPUT_HANDSET, pte->device->volume, MUTE_OFF);
  3230. else if (pte->state == STATE_EXTENSION) /* We must have a TN before calling */
  3231. return;
  3232. else {
  3233. send_select_output(pte, OUTPUT_HANDSET, pte->device->volume, MUTE_OFF);
  3234. handle_dial_page(pte);
  3235. }
  3236. return;
  3237. }
  3238. if (memcmp(buf + SIZE_HEADER, packet_recv_hangup, sizeof(packet_recv_hangup)) == 0) {
  3239. if (unistimdebug)
  3240. ast_verb(0, "Handset on hook\n");
  3241. if (!pte->device)
  3242. return;
  3243. pte->device->receiver_state = STATE_ONHOOK;
  3244. if (pte->state == STATE_CALL)
  3245. close_call(pte);
  3246. else if (pte->device->lines->subs[SUB_REAL]->owner)
  3247. close_call(pte);
  3248. else if (pte->state == STATE_EXTENSION)
  3249. return;
  3250. else
  3251. show_main_page(pte);
  3252. return;
  3253. }
  3254. strcpy(tmpbuf, ast_inet_ntoa(pte->sin.sin_addr));
  3255. strcat(tmpbuf, " Unknown request packet\n");
  3256. if (unistimdebug)
  3257. ast_debug(1, "%s", tmpbuf);
  3258. return;
  3259. }
  3260. static void parsing(int size, unsigned char *buf, struct unistimsession *pte,
  3261. struct sockaddr_in *addr_from)
  3262. {
  3263. unsigned short *sbuf = (unsigned short *) buf;
  3264. unsigned short seq;
  3265. char tmpbuf[255];
  3266. strcpy(tmpbuf, ast_inet_ntoa(addr_from->sin_addr));
  3267. if (size < 10) {
  3268. if (size == 0) {
  3269. ast_log(LOG_WARNING, "%s Read error\n", tmpbuf);
  3270. } else {
  3271. ast_log(LOG_NOTICE, "%s Packet too short - ignoring\n", tmpbuf);
  3272. }
  3273. return;
  3274. }
  3275. if (sbuf[0] == 0xffff) { /* Starting with 0xffff ? *//* Yes, discovery packet ? */
  3276. if (size != sizeof(packet_rcv_discovery)) {
  3277. ast_log(LOG_NOTICE, "%s Invalid size of a discovery packet\n", tmpbuf);
  3278. } else {
  3279. if (memcmp(buf, packet_rcv_discovery, sizeof(packet_rcv_discovery)) == 0) {
  3280. if (unistimdebug)
  3281. ast_verb(0, "Discovery packet received - Sending Discovery ACK\n");
  3282. if (pte) { /* A session was already active for this IP ? */
  3283. if (pte->state == STATE_INIT) { /* Yes, but it's a dupe */
  3284. if (unistimdebug)
  3285. ast_verb(1, "Duplicated Discovery packet\n");
  3286. send_raw_client(sizeof(packet_send_discovery_ack),
  3287. packet_send_discovery_ack, addr_from, &pte->sout);
  3288. pte->seq_phone = (short) 0x0000; /* reset sequence number */
  3289. } else { /* No, probably a reboot, phone side */
  3290. close_client(pte); /* Cleanup the previous session */
  3291. if (create_client(addr_from))
  3292. send_raw_client(sizeof(packet_send_discovery_ack),
  3293. packet_send_discovery_ack, addr_from, &pte->sout);
  3294. }
  3295. } else {
  3296. /* Creating new entry in our phone list */
  3297. if ((pte = create_client(addr_from)))
  3298. send_raw_client(sizeof(packet_send_discovery_ack),
  3299. packet_send_discovery_ack, addr_from, &pte->sout);
  3300. }
  3301. return;
  3302. }
  3303. ast_log(LOG_NOTICE, "%s Invalid discovery packet\n", tmpbuf);
  3304. }
  3305. return;
  3306. }
  3307. if (!pte) {
  3308. if (unistimdebug)
  3309. ast_verb(0, "%s Not a discovery packet from an unknown source : ignoring\n",
  3310. tmpbuf);
  3311. return;
  3312. }
  3313. if (sbuf[0] != 0) { /* Starting with something else than 0x0000 ? */
  3314. ast_log(LOG_NOTICE, "Unknown packet received - ignoring\n");
  3315. return;
  3316. }
  3317. if (buf[5] != 2) {
  3318. ast_log(LOG_NOTICE, "%s Wrong direction : got 0x%.2x expected 0x02\n", tmpbuf,
  3319. (unsigned)buf[5]);
  3320. return;
  3321. }
  3322. seq = ntohs(sbuf[1]);
  3323. if (buf[4] == 1) {
  3324. ast_mutex_lock(&pte->lock);
  3325. if (unistimdebug)
  3326. ast_verb(6, "ACK received for packet #0x%.4x\n", (unsigned)seq);
  3327. pte->nb_retransmit = 0;
  3328. if ((pte->last_seq_ack) + 1 == seq) {
  3329. pte->last_seq_ack++;
  3330. check_send_queue(pte);
  3331. ast_mutex_unlock(&pte->lock);
  3332. return;
  3333. }
  3334. if (pte->last_seq_ack > seq) {
  3335. if (pte->last_seq_ack == 0xffff) {
  3336. ast_verb(0, "ACK at 0xffff, restarting counter.\n");
  3337. pte->last_seq_ack = 0;
  3338. } else
  3339. ast_log(LOG_NOTICE,
  3340. "%s Warning : ACK received for an already ACKed packet : #0x%.4x we are at #0x%.4x\n",
  3341. tmpbuf, (unsigned)seq, (unsigned)pte->last_seq_ack);
  3342. ast_mutex_unlock(&pte->lock);
  3343. return;
  3344. }
  3345. if (pte->seq_server < seq) {
  3346. ast_log(LOG_NOTICE,
  3347. "%s Error : ACK received for a non-existent packet : #0x%.4x\n",
  3348. tmpbuf, (unsigned)pte->seq_server);
  3349. ast_mutex_unlock(&pte->lock);
  3350. return;
  3351. }
  3352. if (unistimdebug)
  3353. ast_verb(0, "%s ACK gap : Received ACK #0x%.4x, previous was #0x%.4x\n",
  3354. tmpbuf, (unsigned)seq, (unsigned)pte->last_seq_ack);
  3355. pte->last_seq_ack = seq;
  3356. check_send_queue(pte);
  3357. ast_mutex_unlock(&pte->lock);
  3358. return;
  3359. }
  3360. if (buf[4] == 2) {
  3361. if (unistimdebug)
  3362. ast_verb(0, "Request received\n");
  3363. if (pte->seq_phone == seq) {
  3364. /* Send ACK */
  3365. buf[4] = 1;
  3366. buf[5] = 1;
  3367. send_raw_client(SIZE_HEADER, buf, addr_from, &pte->sout);
  3368. pte->seq_phone++;
  3369. process_request(size, buf, pte);
  3370. return;
  3371. }
  3372. if (pte->seq_phone > seq) {
  3373. ast_log(LOG_NOTICE,
  3374. "%s Warning : received a retransmitted packet : #0x%.4x (we are at #0x%.4x)\n",
  3375. tmpbuf, (unsigned)seq, (unsigned)pte->seq_phone);
  3376. /* BUG ? pte->device->seq_phone = seq; */
  3377. /* Send ACK */
  3378. buf[4] = 1;
  3379. buf[5] = 1;
  3380. send_raw_client(SIZE_HEADER, buf, addr_from, &pte->sout);
  3381. return;
  3382. }
  3383. ast_log(LOG_NOTICE,
  3384. "%s Warning : we lost a packet : received #0x%.4x (we are at #0x%.4x)\n",
  3385. tmpbuf, (unsigned)seq, (unsigned)pte->seq_phone);
  3386. return;
  3387. }
  3388. if (buf[4] == 0) {
  3389. ast_log(LOG_NOTICE, "%s Retransmit request for packet #0x%.4x\n", tmpbuf, (unsigned)seq);
  3390. if (pte->last_seq_ack > seq) {
  3391. ast_log(LOG_NOTICE,
  3392. "%s Error : received a request for an already ACKed packet : #0x%.4x\n",
  3393. tmpbuf, (unsigned)pte->last_seq_ack);
  3394. return;
  3395. }
  3396. if (pte->seq_server < seq) {
  3397. ast_log(LOG_NOTICE,
  3398. "%s Error : received a request for a non-existent packet : #0x%.4x\n",
  3399. tmpbuf, (unsigned)pte->seq_server);
  3400. return;
  3401. }
  3402. send_retransmit(pte);
  3403. return;
  3404. }
  3405. ast_log(LOG_NOTICE, "%s Unknown request : got 0x%.2x expected 0x00,0x01 or 0x02\n",
  3406. tmpbuf, (unsigned)buf[4]);
  3407. return;
  3408. }
  3409. static struct unistimsession *channel_to_session(struct ast_channel *ast)
  3410. {
  3411. struct unistim_subchannel *sub;
  3412. if (!ast) {
  3413. ast_log(LOG_WARNING, "Unistim callback function called with a null channel\n");
  3414. return NULL;
  3415. }
  3416. if (!ast->tech_pvt) {
  3417. ast_log(LOG_WARNING, "Unistim callback function called without a tech_pvt\n");
  3418. return NULL;
  3419. }
  3420. sub = ast->tech_pvt;
  3421. if (!sub->parent) {
  3422. ast_log(LOG_WARNING, "Unistim callback function called without a line\n");
  3423. return NULL;
  3424. }
  3425. if (!sub->parent->parent) {
  3426. ast_log(LOG_WARNING, "Unistim callback function called without a device\n");
  3427. return NULL;
  3428. }
  3429. if (!sub->parent->parent->session) {
  3430. ast_log(LOG_WARNING, "Unistim callback function called without a session\n");
  3431. return NULL;
  3432. }
  3433. return sub->parent->parent->session;
  3434. }
  3435. /*--- unistim_call: Initiate UNISTIM call from PBX ---*/
  3436. /* used from the dial() application */
  3437. static int unistim_call(struct ast_channel *ast, char *dest, int timeout)
  3438. {
  3439. int res = 0;
  3440. struct unistim_subchannel *sub;
  3441. struct unistimsession *session;
  3442. session = channel_to_session(ast);
  3443. if (!session) {
  3444. ast_log(LOG_ERROR, "Device not registered, cannot call %s\n", dest);
  3445. return -1;
  3446. }
  3447. sub = ast->tech_pvt;
  3448. if ((ast->_state != AST_STATE_DOWN) && (ast->_state != AST_STATE_RESERVED)) {
  3449. ast_log(LOG_WARNING, "unistim_call called on %s, neither down nor reserved\n",
  3450. ast->name);
  3451. return -1;
  3452. }
  3453. if (unistimdebug)
  3454. ast_verb(3, "unistim_call(%s)\n", ast->name);
  3455. session->state = STATE_RINGING;
  3456. Sendicon(TEXT_LINE0, FAV_ICON_NONE, session);
  3457. if (sub->owner) {
  3458. if (sub->owner->connected.id.number.valid
  3459. && sub->owner->connected.id.number.str) {
  3460. if (session->device->height == 1) {
  3461. send_text(TEXT_LINE0, TEXT_NORMAL, session, sub->owner->connected.id.number.str);
  3462. } else {
  3463. send_text(TEXT_LINE1, TEXT_NORMAL, session, sub->owner->connected.id.number.str);
  3464. }
  3465. change_callerid(session, 0, sub->owner->connected.id.number.str);
  3466. } else {
  3467. if (session->device->height == 1) {
  3468. send_text(TEXT_LINE0, TEXT_NORMAL, session, DEFAULTCALLERID);
  3469. } else {
  3470. send_text(TEXT_LINE1, TEXT_NORMAL, session, DEFAULTCALLERID);
  3471. }
  3472. change_callerid(session, 0, DEFAULTCALLERID);
  3473. }
  3474. if (sub->owner->connected.id.name.valid
  3475. && sub->owner->connected.id.name.str) {
  3476. send_text(TEXT_LINE0, TEXT_NORMAL, session, sub->owner->connected.id.name.str);
  3477. change_callerid(session, 1, sub->owner->connected.id.name.str);
  3478. } else {
  3479. send_text(TEXT_LINE0, TEXT_NORMAL, session, DEFAULTCALLERNAME);
  3480. change_callerid(session, 1, DEFAULTCALLERNAME);
  3481. }
  3482. }
  3483. send_text(TEXT_LINE2, TEXT_NORMAL, session, "is calling you.");
  3484. send_text_status(session, "Accept Ignore");
  3485. if (sub->ringstyle == -1)
  3486. send_ring(session, session->device->ringvolume, session->device->ringstyle);
  3487. else {
  3488. if (sub->ringvolume == -1)
  3489. send_ring(session, session->device->ringvolume, sub->ringstyle);
  3490. else
  3491. send_ring(session, sub->ringvolume, sub->ringstyle);
  3492. }
  3493. change_favorite_icon(session, FAV_ICON_SPEAKER_ONHOOK_BLACK + FAV_BLINK_FAST);
  3494. ast_setstate(ast, AST_STATE_RINGING);
  3495. ast_queue_control(ast, AST_CONTROL_RINGING);
  3496. return res;
  3497. }
  3498. /*--- unistim_hangup: Hangup UNISTIM call */
  3499. static int unistim_hangup(struct ast_channel *ast)
  3500. {
  3501. struct unistim_subchannel *sub;
  3502. struct unistim_line *l;
  3503. struct unistimsession *s;
  3504. s = channel_to_session(ast);
  3505. sub = ast->tech_pvt;
  3506. if (!s) {
  3507. ast_debug(1, "Asked to hangup channel not connected\n");
  3508. ast_mutex_lock(&sub->lock);
  3509. sub->owner = NULL;
  3510. ast->tech_pvt = NULL;
  3511. sub->alreadygone = 0;
  3512. ast_mutex_unlock(&sub->lock);
  3513. if (sub->rtp) {
  3514. if (unistimdebug)
  3515. ast_verb(0, "Destroying RTP session\n");
  3516. ast_rtp_instance_destroy(sub->rtp);
  3517. sub->rtp = NULL;
  3518. }
  3519. return 0;
  3520. }
  3521. l = sub->parent;
  3522. if (unistimdebug)
  3523. ast_verb(0, "unistim_hangup(%s) on %s@%s\n", ast->name, l->name, l->parent->name);
  3524. if ((l->subs[SUB_THREEWAY]) && (sub->subtype == SUB_REAL)) {
  3525. if (unistimdebug)
  3526. ast_verb(0, "Real call disconnected while talking to threeway\n");
  3527. sub->owner = NULL;
  3528. ast->tech_pvt = NULL;
  3529. return 0;
  3530. }
  3531. if ((l->subs[SUB_REAL]->owner) && (sub->subtype == SUB_THREEWAY) &&
  3532. (sub->alreadygone == 0)) {
  3533. if (unistimdebug)
  3534. ast_verb(0, "threeway call disconnected, switching to real call\n");
  3535. send_text(TEXT_LINE0, TEXT_NORMAL, s, "Three way call canceled,");
  3536. send_text(TEXT_LINE1, TEXT_NORMAL, s, "switching back to");
  3537. send_text(TEXT_LINE2, TEXT_NORMAL, s, "previous call.");
  3538. send_text_status(s, "Hangup Transf");
  3539. ast_moh_stop(ast_bridged_channel(l->subs[SUB_REAL]->owner));
  3540. swap_subs(l, SUB_THREEWAY, SUB_REAL);
  3541. l->parent->moh = 0;
  3542. ast_mutex_lock(&sub->lock);
  3543. sub->owner = NULL;
  3544. ast->tech_pvt = NULL;
  3545. ast_mutex_unlock(&sub->lock);
  3546. unalloc_sub(l, SUB_THREEWAY);
  3547. return 0;
  3548. }
  3549. ast_mutex_lock(&sub->lock);
  3550. sub->owner = NULL;
  3551. ast->tech_pvt = NULL;
  3552. sub->alreadygone = 0;
  3553. ast_mutex_unlock(&sub->lock);
  3554. if (!s) {
  3555. if (unistimdebug)
  3556. ast_verb(0, "Asked to hangup channel not connected (no session)\n");
  3557. if (sub->rtp) {
  3558. if (unistimdebug)
  3559. ast_verb(0, "Destroying RTP session\n");
  3560. ast_rtp_instance_destroy(sub->rtp);
  3561. sub->rtp = NULL;
  3562. }
  3563. return 0;
  3564. }
  3565. if (sub->subtype == SUB_REAL) {
  3566. /* Stop the silence generator */
  3567. if (s->device->silence_generator) {
  3568. if (unistimdebug)
  3569. ast_verb(0, "Stopping silence generator\n");
  3570. if (sub->owner)
  3571. ast_channel_stop_silence_generator(sub->owner,
  3572. s->device->silence_generator);
  3573. else
  3574. ast_log(LOG_WARNING,
  3575. "Trying to stop silence generator on a null channel !\n");
  3576. s->device->silence_generator = NULL;
  3577. }
  3578. }
  3579. l->parent->moh = 0;
  3580. send_no_ring(s);
  3581. send_end_call(s);
  3582. if (sub->rtp) {
  3583. if (unistimdebug)
  3584. ast_verb(0, "Destroying RTP session\n");
  3585. ast_rtp_instance_destroy(sub->rtp);
  3586. sub->rtp = NULL;
  3587. } else if (unistimdebug)
  3588. ast_verb(0, "No RTP session to destroy\n");
  3589. if (l->subs[SUB_THREEWAY]) {
  3590. if (unistimdebug)
  3591. ast_verb(0, "Cleaning other subchannels\n");
  3592. unalloc_sub(l, SUB_THREEWAY);
  3593. }
  3594. if (s->state == STATE_RINGING)
  3595. cancel_dial(s);
  3596. else if (s->state == STATE_CALL)
  3597. close_call(s);
  3598. return 0;
  3599. }
  3600. /*--- unistim_answer: Answer UNISTIM call */
  3601. static int unistim_answer(struct ast_channel *ast)
  3602. {
  3603. int res = 0;
  3604. struct unistim_subchannel *sub;
  3605. struct unistim_line *l;
  3606. struct unistimsession *s;
  3607. s = channel_to_session(ast);
  3608. if (!s) {
  3609. ast_log(LOG_WARNING, "unistim_answer on a disconnected device ?\n");
  3610. return -1;
  3611. }
  3612. sub = ast->tech_pvt;
  3613. l = sub->parent;
  3614. if ((!sub->rtp) && (!l->subs[SUB_THREEWAY]))
  3615. start_rtp(sub);
  3616. if (unistimdebug)
  3617. ast_verb(0, "unistim_answer(%s) on %s@%s-%u\n", ast->name, l->name,
  3618. l->parent->name, sub->subtype);
  3619. send_text(TEXT_LINE2, TEXT_NORMAL, l->parent->session, "is now on-line");
  3620. if (l->subs[SUB_THREEWAY])
  3621. send_text_status(l->parent->session, "Transf Cancel");
  3622. else
  3623. send_text_status(l->parent->session, "Hangup Transf");
  3624. send_start_timer(l->parent->session);
  3625. if (ast->_state != AST_STATE_UP)
  3626. ast_setstate(ast, AST_STATE_UP);
  3627. return res;
  3628. }
  3629. /*--- unistimsock_read: Read data from UNISTIM socket ---*/
  3630. /* Successful messages is connected to UNISTIM call and forwarded to parsing() */
  3631. static int unistimsock_read(int *id, int fd, short events, void *ignore)
  3632. {
  3633. struct sockaddr_in addr_from = { 0, };
  3634. struct unistimsession *cur = NULL;
  3635. int found = 0;
  3636. int tmp = 0;
  3637. int dw_num_bytes_rcvd;
  3638. #ifdef DUMP_PACKET
  3639. int dw_num_bytes_rcvdd;
  3640. char iabuf[INET_ADDRSTRLEN];
  3641. #endif
  3642. dw_num_bytes_rcvd =
  3643. recvfrom(unistimsock, buff, SIZE_PAGE, 0, (struct sockaddr *) &addr_from,
  3644. &size_addr_from);
  3645. if (dw_num_bytes_rcvd == -1) {
  3646. if (errno == EAGAIN)
  3647. ast_log(LOG_NOTICE, "UNISTIM: Received packet with bad UDP checksum\n");
  3648. else if (errno != ECONNREFUSED)
  3649. ast_log(LOG_WARNING, "Recv error %d (%s)\n", errno, strerror(errno));
  3650. return 1;
  3651. }
  3652. /* Looking in the phone list if we already have a registration for him */
  3653. ast_mutex_lock(&sessionlock);
  3654. cur = sessions;
  3655. while (cur) {
  3656. if (cur->sin.sin_addr.s_addr == addr_from.sin_addr.s_addr) {
  3657. found = 1;
  3658. break;
  3659. }
  3660. tmp++;
  3661. cur = cur->next;
  3662. }
  3663. ast_mutex_unlock(&sessionlock);
  3664. #ifdef DUMP_PACKET
  3665. if (unistimdebug)
  3666. ast_verb(0, "\n*** Dump %d bytes from %s - phone_table[%d] ***\n",
  3667. dw_num_bytes_rcvd, ast_inet_ntoa(addr_from.sin_addr), tmp);
  3668. for (dw_num_bytes_rcvdd = 0; dw_num_bytes_rcvdd < dw_num_bytes_rcvd;
  3669. dw_num_bytes_rcvdd++)
  3670. ast_verb(0, "%.2x ", (unsigned char) buff[dw_num_bytes_rcvdd]);
  3671. ast_verb(0, "\n******************************************\n");
  3672. #endif
  3673. if (!found) {
  3674. if (unistimdebug)
  3675. ast_verb(0, "Received a packet from an unknown source\n");
  3676. parsing(dw_num_bytes_rcvd, buff, NULL, (struct sockaddr_in *) &addr_from);
  3677. } else
  3678. parsing(dw_num_bytes_rcvd, buff, cur, (struct sockaddr_in *) &addr_from);
  3679. return 1;
  3680. }
  3681. static struct ast_frame *unistim_rtp_read(const struct ast_channel *ast,
  3682. const struct unistim_subchannel *sub)
  3683. {
  3684. /* Retrieve audio/etc from channel. Assumes sub->lock is already held. */
  3685. struct ast_frame *f;
  3686. if (!ast) {
  3687. ast_log(LOG_WARNING, "Channel NULL while reading\n");
  3688. return &ast_null_frame;
  3689. }
  3690. if (!sub->rtp) {
  3691. ast_log(LOG_WARNING, "RTP handle NULL while reading on subchannel %u\n",
  3692. sub->subtype);
  3693. return &ast_null_frame;
  3694. }
  3695. switch (ast->fdno) {
  3696. case 0:
  3697. f = ast_rtp_instance_read(sub->rtp, 0); /* RTP Audio */
  3698. break;
  3699. case 1:
  3700. f = ast_rtp_instance_read(sub->rtp, 1); /* RTCP Control Channel */
  3701. break;
  3702. default:
  3703. f = &ast_null_frame;
  3704. }
  3705. if (sub->owner) {
  3706. /* We already hold the channel lock */
  3707. if (f->frametype == AST_FRAME_VOICE) {
  3708. if (f->subclass.codec != sub->owner->nativeformats) {
  3709. ast_debug(1,
  3710. "Oooh, format changed from %s to %s\n",
  3711. ast_getformatname(sub->owner->nativeformats),
  3712. ast_getformatname(f->subclass.codec));
  3713. sub->owner->nativeformats = f->subclass.codec;
  3714. ast_set_read_format(sub->owner, sub->owner->readformat);
  3715. ast_set_write_format(sub->owner, sub->owner->writeformat);
  3716. }
  3717. }
  3718. }
  3719. return f;
  3720. }
  3721. static struct ast_frame *unistim_read(struct ast_channel *ast)
  3722. {
  3723. struct ast_frame *fr;
  3724. struct unistim_subchannel *sub = ast->tech_pvt;
  3725. ast_mutex_lock(&sub->lock);
  3726. fr = unistim_rtp_read(ast, sub);
  3727. ast_mutex_unlock(&sub->lock);
  3728. return fr;
  3729. }
  3730. static int unistim_write(struct ast_channel *ast, struct ast_frame *frame)
  3731. {
  3732. struct unistim_subchannel *sub = ast->tech_pvt;
  3733. int res = 0;
  3734. if (frame->frametype != AST_FRAME_VOICE) {
  3735. if (frame->frametype == AST_FRAME_IMAGE)
  3736. return 0;
  3737. else {
  3738. ast_log(LOG_WARNING, "Can't send %u type frames with unistim_write\n",
  3739. frame->frametype);
  3740. return 0;
  3741. }
  3742. } else {
  3743. if (!(frame->subclass.codec & ast->nativeformats)) {
  3744. char tmp[256];
  3745. ast_log(LOG_WARNING,
  3746. "Asked to transmit frame type %s, while native formats is %s (read/write = (%s/%s)\n",
  3747. ast_getformatname(frame->subclass.codec),
  3748. ast_getformatname_multiple(tmp, sizeof(tmp), ast->nativeformats),
  3749. ast_getformatname(ast->readformat),
  3750. ast_getformatname(ast->writeformat));
  3751. return -1;
  3752. }
  3753. }
  3754. if (sub) {
  3755. ast_mutex_lock(&sub->lock);
  3756. if (sub->rtp) {
  3757. res = ast_rtp_instance_write(sub->rtp, frame);
  3758. }
  3759. ast_mutex_unlock(&sub->lock);
  3760. }
  3761. return res;
  3762. }
  3763. static int unistim_fixup(struct ast_channel *oldchan, struct ast_channel *newchan)
  3764. {
  3765. struct unistim_subchannel *p = newchan->tech_pvt;
  3766. struct unistim_line *l = p->parent;
  3767. ast_mutex_lock(&p->lock);
  3768. ast_debug(1, "New owner for channel USTM/%s@%s-%u is %s\n", l->name,
  3769. l->parent->name, p->subtype, newchan->name);
  3770. if (p->owner != oldchan) {
  3771. ast_log(LOG_WARNING, "old channel wasn't %s (%p) but was %s (%p)\n",
  3772. oldchan->name, oldchan, p->owner->name, p->owner);
  3773. ast_mutex_unlock(&p->lock);
  3774. return -1;
  3775. }
  3776. p->owner = newchan;
  3777. ast_mutex_unlock(&p->lock);
  3778. return 0;
  3779. }
  3780. static char *control2str(int ind)
  3781. {
  3782. switch (ind) {
  3783. case AST_CONTROL_HANGUP:
  3784. return "Other end has hungup";
  3785. case AST_CONTROL_RING:
  3786. return "Local ring";
  3787. case AST_CONTROL_RINGING:
  3788. return "Remote end is ringing";
  3789. case AST_CONTROL_ANSWER:
  3790. return "Remote end has answered";
  3791. case AST_CONTROL_BUSY:
  3792. return "Remote end is busy";
  3793. case AST_CONTROL_TAKEOFFHOOK:
  3794. return "Make it go off hook";
  3795. case AST_CONTROL_OFFHOOK:
  3796. return "Line is off hook";
  3797. case AST_CONTROL_CONGESTION:
  3798. return "Congestion (circuits busy)";
  3799. case AST_CONTROL_FLASH:
  3800. return "Flash hook";
  3801. case AST_CONTROL_WINK:
  3802. return "Wink";
  3803. case AST_CONTROL_OPTION:
  3804. return "Set a low-level option";
  3805. case AST_CONTROL_RADIO_KEY:
  3806. return "Key Radio";
  3807. case AST_CONTROL_RADIO_UNKEY:
  3808. return "Un-Key Radio";
  3809. case -1:
  3810. return "Stop tone";
  3811. }
  3812. return "UNKNOWN";
  3813. }
  3814. static void in_band_indication(struct ast_channel *ast, const struct ast_tone_zone *tz,
  3815. const char *indication)
  3816. {
  3817. struct ast_tone_zone_sound *ts = NULL;
  3818. if ((ts = ast_get_indication_tone(tz, indication))) {
  3819. ast_playtones_start(ast, 0, ts->data, 1);
  3820. ts = ast_tone_zone_sound_unref(ts);
  3821. } else {
  3822. ast_log(LOG_WARNING, "Unable to get indication tone for %s\n", indication);
  3823. }
  3824. }
  3825. static int unistim_indicate(struct ast_channel *ast, int ind, const void *data,
  3826. size_t datalen)
  3827. {
  3828. struct unistim_subchannel *sub;
  3829. struct unistim_line *l;
  3830. struct unistimsession *s;
  3831. if (unistimdebug) {
  3832. ast_verb(3, "Asked to indicate '%s' condition on channel %s\n",
  3833. control2str(ind), ast->name);
  3834. }
  3835. s = channel_to_session(ast);
  3836. if (!s)
  3837. return -1;
  3838. sub = ast->tech_pvt;
  3839. l = sub->parent;
  3840. switch (ind) {
  3841. case AST_CONTROL_RINGING:
  3842. if (ast->_state != AST_STATE_UP) {
  3843. send_text(TEXT_LINE2, TEXT_NORMAL, s, "Ringing...");
  3844. in_band_indication(ast, l->parent->tz, "ring");
  3845. s->device->missed_call = -1;
  3846. break;
  3847. }
  3848. return -1;
  3849. case AST_CONTROL_BUSY:
  3850. if (ast->_state != AST_STATE_UP) {
  3851. sub->alreadygone = 1;
  3852. send_text(TEXT_LINE2, TEXT_NORMAL, s, "Busy");
  3853. in_band_indication(ast, l->parent->tz, "busy");
  3854. s->device->missed_call = -1;
  3855. break;
  3856. }
  3857. return -1;
  3858. case AST_CONTROL_INCOMPLETE:
  3859. /* Overlapped dialing is not currently supported for UNIStim. Treat an indication
  3860. * of incomplete as congestion
  3861. */
  3862. case AST_CONTROL_CONGESTION:
  3863. if (ast->_state != AST_STATE_UP) {
  3864. sub->alreadygone = 1;
  3865. send_text(TEXT_LINE2, TEXT_NORMAL, s, "Congestion");
  3866. in_band_indication(ast, l->parent->tz, "congestion");
  3867. s->device->missed_call = -1;
  3868. break;
  3869. }
  3870. return -1;
  3871. case AST_CONTROL_HOLD:
  3872. ast_moh_start(ast, data, NULL);
  3873. break;
  3874. case AST_CONTROL_UNHOLD:
  3875. ast_moh_stop(ast);
  3876. break;
  3877. case AST_CONTROL_PROGRESS:
  3878. case AST_CONTROL_SRCUPDATE:
  3879. break;
  3880. case -1:
  3881. ast_playtones_stop(ast);
  3882. s->device->missed_call = 0;
  3883. break;
  3884. case AST_CONTROL_PROCEEDING:
  3885. case AST_CONTROL_UPDATE_RTP_PEER:
  3886. break;
  3887. default:
  3888. ast_log(LOG_WARNING, "Don't know how to indicate condition %d\n", ind);
  3889. return -1;
  3890. }
  3891. return 0;
  3892. }
  3893. static struct unistim_subchannel *find_subchannel_by_name(const char *dest)
  3894. {
  3895. struct unistim_line *l;
  3896. struct unistim_device *d;
  3897. char line[256];
  3898. char *at;
  3899. char *device;
  3900. ast_copy_string(line, dest, sizeof(line));
  3901. at = strchr(line, '@');
  3902. if (!at) {
  3903. ast_log(LOG_NOTICE, "Device '%s' has no @ (at) sign!\n", dest);
  3904. return NULL;
  3905. }
  3906. *at = '\0';
  3907. at++;
  3908. device = at;
  3909. ast_mutex_lock(&devicelock);
  3910. d = devices;
  3911. at = strchr(device, '/'); /* Extra options ? */
  3912. if (at)
  3913. *at = '\0';
  3914. while (d) {
  3915. if (!strcasecmp(d->name, device)) {
  3916. if (unistimdebug)
  3917. ast_verb(0, "Found device: %s\n", d->name);
  3918. /* Found the device */
  3919. l = d->lines;
  3920. while (l) {
  3921. /* Search for the right line */
  3922. if (!strcasecmp(l->name, line)) {
  3923. l->subs[SUB_REAL]->ringvolume = -1;
  3924. l->subs[SUB_REAL]->ringstyle = -1;
  3925. if (at) { /* Other options ? */
  3926. at++; /* Skip slash */
  3927. if (*at == 'r') { /* distinctive ring */
  3928. at++;
  3929. if ((*at < '0') || (*at > '7')) /* ring style */
  3930. ast_log(LOG_WARNING, "Invalid ring selection (%s)", at);
  3931. else {
  3932. char ring_volume = -1;
  3933. char ring_style = *at - '0';
  3934. at++;
  3935. if ((*at >= '0') && (*at <= '3')) /* ring volume */
  3936. ring_volume = *at - '0';
  3937. if (unistimdebug)
  3938. ast_verb(0, "Distinctive ring : style #%d volume %d\n",
  3939. ring_style, ring_volume);
  3940. l->subs[SUB_REAL]->ringvolume = ring_volume;
  3941. l->subs[SUB_REAL]->ringstyle = ring_style;
  3942. }
  3943. }
  3944. }
  3945. ast_mutex_unlock(&devicelock);
  3946. return l->subs[SUB_REAL];
  3947. }
  3948. l = l->next;
  3949. }
  3950. }
  3951. d = d->next;
  3952. }
  3953. /* Device not found */
  3954. ast_mutex_unlock(&devicelock);
  3955. return NULL;
  3956. }
  3957. static int unistim_senddigit_begin(struct ast_channel *ast, char digit)
  3958. {
  3959. struct unistimsession *pte = channel_to_session(ast);
  3960. if (!pte)
  3961. return -1;
  3962. return unistim_do_senddigit(pte, digit);
  3963. }
  3964. static int unistim_senddigit_end(struct ast_channel *ast, char digit, unsigned int duration)
  3965. {
  3966. struct unistimsession *pte = channel_to_session(ast);
  3967. struct ast_frame f = { 0, };
  3968. struct unistim_subchannel *sub;
  3969. sub = pte->device->lines->subs[SUB_REAL];
  3970. if (!sub->owner || sub->alreadygone) {
  3971. ast_log(LOG_WARNING, "Unable to find subchannel in dtmf senddigit_end\n");
  3972. return -1;
  3973. }
  3974. if (unistimdebug)
  3975. ast_verb(0, "Send Digit off %c\n", digit);
  3976. if (!pte)
  3977. return -1;
  3978. send_tone(pte, 0, 0);
  3979. f.frametype = AST_FRAME_DTMF;
  3980. f.subclass.integer = digit;
  3981. f.src = "unistim";
  3982. ast_queue_frame(sub->owner, &f);
  3983. return 0;
  3984. }
  3985. /*--- unistim_sendtext: Display a text on the phone screen ---*/
  3986. /* Called from PBX core text message functions */
  3987. static int unistim_sendtext(struct ast_channel *ast, const char *text)
  3988. {
  3989. struct unistimsession *pte = channel_to_session(ast);
  3990. int size;
  3991. char tmp[TEXT_LENGTH_MAX + 1];
  3992. if (unistimdebug)
  3993. ast_verb(0, "unistim_sendtext called\n");
  3994. if (!text) {
  3995. ast_log(LOG_WARNING, "unistim_sendtext called with a null text\n");
  3996. return 1;
  3997. }
  3998. size = strlen(text);
  3999. if (text[0] == '@') {
  4000. int pos = 0, i = 1, tok = 0, sz = 0;
  4001. char label[11];
  4002. char number[16];
  4003. char icon = '\0';
  4004. char cur = '\0';
  4005. memset(label, 0, 11);
  4006. memset(number, 0, 16);
  4007. while (text[i]) {
  4008. cur = text[i++];
  4009. switch (tok) {
  4010. case 0:
  4011. if ((cur < '0') && (cur > '5')) {
  4012. ast_log(LOG_WARNING,
  4013. "sendtext failed : position must be a number beetween 0 and 5\n");
  4014. return 1;
  4015. }
  4016. pos = cur - '0';
  4017. tok = 1;
  4018. continue;
  4019. case 1:
  4020. if (cur != '@') {
  4021. ast_log(LOG_WARNING, "sendtext failed : invalid position\n");
  4022. return 1;
  4023. }
  4024. tok = 2;
  4025. continue;
  4026. case 2:
  4027. if ((cur < '3') && (cur > '6')) {
  4028. ast_log(LOG_WARNING,
  4029. "sendtext failed : icon must be a number beetween 32 and 63 (first digit invalid)\n");
  4030. return 1;
  4031. }
  4032. icon = (cur - '0') * 10;
  4033. tok = 3;
  4034. continue;
  4035. case 3:
  4036. if ((cur < '0') && (cur > '9')) {
  4037. ast_log(LOG_WARNING,
  4038. "sendtext failed : icon must be a number beetween 32 and 63 (second digit invalid)\n");
  4039. return 1;
  4040. }
  4041. icon += (cur - '0');
  4042. tok = 4;
  4043. continue;
  4044. case 4:
  4045. if (cur != '@') {
  4046. ast_log(LOG_WARNING,
  4047. "sendtext failed : icon must be a number beetween 32 and 63 (too many digits)\n");
  4048. return 1;
  4049. }
  4050. tok = 5;
  4051. continue;
  4052. case 5:
  4053. if (cur == '@') {
  4054. tok = 6;
  4055. sz = 0;
  4056. continue;
  4057. }
  4058. if (sz > 10)
  4059. continue;
  4060. label[sz] = cur;
  4061. sz++;
  4062. continue;
  4063. case 6:
  4064. if (sz > 15) {
  4065. ast_log(LOG_WARNING,
  4066. "sendtext failed : extension too long = %d (15 car max)\n",
  4067. sz);
  4068. return 1;
  4069. }
  4070. number[sz] = cur;
  4071. sz++;
  4072. continue;
  4073. }
  4074. }
  4075. if (tok != 6) {
  4076. ast_log(LOG_WARNING, "sendtext failed : incomplet command\n");
  4077. return 1;
  4078. }
  4079. if (!pte->device) {
  4080. ast_log(LOG_WARNING, "sendtext failed : no device ?\n");
  4081. return 1;
  4082. }
  4083. strcpy(pte->device->softkeylabel[pos], label);
  4084. strcpy(pte->device->softkeynumber[pos], number);
  4085. pte->device->softkeyicon[pos] = icon;
  4086. send_favorite(pos, icon, pte, label);
  4087. return 0;
  4088. }
  4089. if (size <= TEXT_LENGTH_MAX * 2) {
  4090. if (pte->device->height == 1) {
  4091. send_text(TEXT_LINE0, TEXT_NORMAL, pte, text);
  4092. } else {
  4093. send_text(TEXT_LINE0, TEXT_NORMAL, pte, "Message :");
  4094. send_text(TEXT_LINE1, TEXT_NORMAL, pte, text);
  4095. }
  4096. if (size <= TEXT_LENGTH_MAX) {
  4097. send_text(TEXT_LINE2, TEXT_NORMAL, pte, "");
  4098. return 0;
  4099. }
  4100. memcpy(tmp, text + TEXT_LENGTH_MAX, TEXT_LENGTH_MAX);
  4101. tmp[sizeof(tmp) - 1] = '\0';
  4102. send_text(TEXT_LINE2, TEXT_NORMAL, pte, tmp);
  4103. return 0;
  4104. }
  4105. send_text(TEXT_LINE0, TEXT_NORMAL, pte, text);
  4106. memcpy(tmp, text + TEXT_LENGTH_MAX, TEXT_LENGTH_MAX);
  4107. tmp[sizeof(tmp) - 1] = '\0';
  4108. send_text(TEXT_LINE1, TEXT_NORMAL, pte, tmp);
  4109. memcpy(tmp, text + TEXT_LENGTH_MAX * 2, TEXT_LENGTH_MAX);
  4110. tmp[sizeof(tmp) - 1] = '\0';
  4111. send_text(TEXT_LINE2, TEXT_NORMAL, pte, tmp);
  4112. return 0;
  4113. }
  4114. /*--- unistim_send_mwi_to_peer: Send message waiting indication ---*/
  4115. static int unistim_send_mwi_to_peer(struct unistimsession *s, unsigned int tick)
  4116. {
  4117. struct ast_event *event;
  4118. int new;
  4119. char *mailbox, *context;
  4120. struct unistim_line *peer = s->device->lines;
  4121. context = mailbox = ast_strdupa(peer->mailbox);
  4122. strsep(&context, "@");
  4123. if (ast_strlen_zero(context))
  4124. context = "default";
  4125. event = ast_event_get_cached(AST_EVENT_MWI,
  4126. AST_EVENT_IE_MAILBOX, AST_EVENT_IE_PLTYPE_STR, mailbox,
  4127. AST_EVENT_IE_CONTEXT, AST_EVENT_IE_PLTYPE_STR, context,
  4128. AST_EVENT_IE_END);
  4129. if (event) {
  4130. new = ast_event_get_ie_uint(event, AST_EVENT_IE_NEWMSGS);
  4131. ast_event_destroy(event);
  4132. } else { /* Fall back on checking the mailbox directly */
  4133. new = ast_app_has_voicemail(peer->mailbox, "INBOX");
  4134. }
  4135. peer->nextmsgcheck = tick + TIMER_MWI;
  4136. /* Return now if it's the same thing we told them last time */
  4137. if (new == peer->lastmsgssent) {
  4138. return 0;
  4139. }
  4140. peer->lastmsgssent = new;
  4141. if (new == 0) {
  4142. send_led_update(s, 0);
  4143. } else {
  4144. send_led_update(s, 1);
  4145. }
  4146. return 0;
  4147. }
  4148. /*--- unistim_new: Initiate a call in the UNISTIM channel */
  4149. /* called from unistim_request (calls from the pbx ) */
  4150. static struct ast_channel *unistim_new(struct unistim_subchannel *sub, int state, const char *linkedid)
  4151. {
  4152. struct ast_channel *tmp;
  4153. struct unistim_line *l;
  4154. int fmt;
  4155. if (!sub) {
  4156. ast_log(LOG_WARNING, "subchannel null in unistim_new\n");
  4157. return NULL;
  4158. }
  4159. if (!sub->parent) {
  4160. ast_log(LOG_WARNING, "no line for subchannel %p\n", sub);
  4161. return NULL;
  4162. }
  4163. l = sub->parent;
  4164. tmp = ast_channel_alloc(1, state, l->cid_num, NULL, l->accountcode, l->exten,
  4165. l->context, linkedid, l->amaflags, "%s@%s-%u", l->name, l->parent->name, sub->subtype);
  4166. if (unistimdebug)
  4167. ast_verb(0, "unistim_new sub=%u (%p) chan=%p\n", sub->subtype, sub, tmp);
  4168. if (!tmp) {
  4169. ast_log(LOG_WARNING, "Unable to allocate channel structure\n");
  4170. return NULL;
  4171. }
  4172. tmp->nativeformats = l->capability;
  4173. if (!tmp->nativeformats)
  4174. tmp->nativeformats = CAPABILITY;
  4175. fmt = ast_best_codec(tmp->nativeformats);
  4176. if (unistimdebug) {
  4177. char tmp1[256], tmp2[256], tmp3[256];
  4178. ast_verb(0, "Best codec = %s from nativeformats %s (line cap=%s global=%s)\n",
  4179. ast_getformatname(fmt),
  4180. ast_getformatname_multiple(tmp1, sizeof(tmp1), tmp->nativeformats),
  4181. ast_getformatname_multiple(tmp2, sizeof(tmp2), l->capability),
  4182. ast_getformatname_multiple(tmp3, sizeof(tmp3), CAPABILITY));
  4183. }
  4184. if ((sub->rtp) && (sub->subtype == 0)) {
  4185. if (unistimdebug)
  4186. ast_verb(0, "New unistim channel with a previous rtp handle ?\n");
  4187. tmp->fds[0] = ast_rtp_instance_fd(sub->rtp, 0);
  4188. tmp->fds[1] = ast_rtp_instance_fd(sub->rtp, 1);
  4189. }
  4190. if (sub->rtp)
  4191. ast_jb_configure(tmp, &global_jbconf);
  4192. /* tmp->type = type; */
  4193. ast_setstate(tmp, state);
  4194. if (state == AST_STATE_RING)
  4195. tmp->rings = 1;
  4196. tmp->adsicpe = AST_ADSI_UNAVAILABLE;
  4197. tmp->writeformat = fmt;
  4198. tmp->rawwriteformat = fmt;
  4199. tmp->readformat = fmt;
  4200. tmp->rawreadformat = fmt;
  4201. tmp->tech_pvt = sub;
  4202. tmp->tech = &unistim_tech;
  4203. if (!ast_strlen_zero(l->language))
  4204. ast_string_field_set(tmp, language, l->language);
  4205. sub->owner = tmp;
  4206. ast_mutex_lock(&usecnt_lock);
  4207. usecnt++;
  4208. ast_mutex_unlock(&usecnt_lock);
  4209. ast_update_use_count();
  4210. tmp->callgroup = l->callgroup;
  4211. tmp->pickupgroup = l->pickupgroup;
  4212. ast_string_field_set(tmp, call_forward, l->parent->call_forward);
  4213. if (!ast_strlen_zero(l->cid_num)) {
  4214. char *name, *loc, *instr;
  4215. instr = ast_strdup(l->cid_num);
  4216. if (instr) {
  4217. ast_callerid_parse(instr, &name, &loc);
  4218. tmp->caller.id.number.valid = 1;
  4219. ast_free(tmp->caller.id.number.str);
  4220. tmp->caller.id.number.str = ast_strdup(loc);
  4221. tmp->caller.id.name.valid = 1;
  4222. ast_free(tmp->caller.id.name.str);
  4223. tmp->caller.id.name.str = ast_strdup(name);
  4224. ast_free(instr);
  4225. }
  4226. }
  4227. tmp->priority = 1;
  4228. if (state != AST_STATE_DOWN) {
  4229. if (unistimdebug)
  4230. ast_verb(0, "Starting pbx in unistim_new\n");
  4231. if (ast_pbx_start(tmp)) {
  4232. ast_log(LOG_WARNING, "Unable to start PBX on %s\n", tmp->name);
  4233. ast_hangup(tmp);
  4234. tmp = NULL;
  4235. }
  4236. }
  4237. return tmp;
  4238. }
  4239. static void *do_monitor(void *data)
  4240. {
  4241. struct unistimsession *cur = NULL;
  4242. unsigned int dw_timeout = 0;
  4243. unsigned int tick;
  4244. int res;
  4245. int reloading;
  4246. /* Add an I/O event to our UDP socket */
  4247. if (unistimsock > -1)
  4248. ast_io_add(io, unistimsock, unistimsock_read, AST_IO_IN, NULL);
  4249. /* This thread monitors our UDP socket and timers */
  4250. for (;;) {
  4251. /* This loop is executed at least every IDLE_WAITus (1s) or every time a packet is received */
  4252. /* Looking for the smallest time-out value */
  4253. tick = get_tick_count();
  4254. dw_timeout = UINT_MAX;
  4255. ast_mutex_lock(&sessionlock);
  4256. cur = sessions;
  4257. DEBUG_TIMER("checking timeout for session %p with tick = %u\n", cur, tick);
  4258. while (cur) {
  4259. DEBUG_TIMER("checking timeout for session %p timeout = %u\n", cur,
  4260. cur->timeout);
  4261. /* Check if we have miss something */
  4262. if (cur->timeout <= tick) {
  4263. DEBUG_TIMER("Event for session %p\n", cur);
  4264. /* If the queue is empty, send a ping */
  4265. if (cur->last_buf_available == 0)
  4266. send_ping(cur);
  4267. else {
  4268. if (send_retransmit(cur)) {
  4269. DEBUG_TIMER("The chained link was modified, restarting...\n");
  4270. cur = sessions;
  4271. dw_timeout = UINT_MAX;
  4272. continue;
  4273. }
  4274. }
  4275. }
  4276. if (dw_timeout > cur->timeout - tick)
  4277. dw_timeout = cur->timeout - tick;
  4278. /* Checking if the phone is logged on for a new MWI */
  4279. if (cur->device) {
  4280. if ((!ast_strlen_zero(cur->device->lines->mailbox)) &&
  4281. ((tick >= cur->device->lines->nextmsgcheck))) {
  4282. DEBUG_TIMER("Checking mailbox for MWI\n");
  4283. unistim_send_mwi_to_peer(cur, tick);
  4284. break;
  4285. }
  4286. }
  4287. cur = cur->next;
  4288. }
  4289. ast_mutex_unlock(&sessionlock);
  4290. DEBUG_TIMER("Waiting for %dus\n", dw_timeout);
  4291. res = dw_timeout;
  4292. /* We should not wait more than IDLE_WAIT */
  4293. if ((res < 0) || (res > IDLE_WAIT))
  4294. res = IDLE_WAIT;
  4295. /* Wait for UDP messages for a maximum of res us */
  4296. res = ast_io_wait(io, res); /* This function will call unistimsock_read if a packet is received */
  4297. /* Check for a reload request */
  4298. ast_mutex_lock(&unistim_reload_lock);
  4299. reloading = unistim_reloading;
  4300. unistim_reloading = 0;
  4301. ast_mutex_unlock(&unistim_reload_lock);
  4302. if (reloading) {
  4303. ast_verb(1, "Reloading unistim.conf...\n");
  4304. reload_config();
  4305. }
  4306. pthread_testcancel();
  4307. }
  4308. /* Never reached */
  4309. return NULL;
  4310. }
  4311. /*--- restart_monitor: Start the channel monitor thread ---*/
  4312. static int restart_monitor(void)
  4313. {
  4314. pthread_attr_t attr;
  4315. /* If we're supposed to be stopped -- stay stopped */
  4316. if (monitor_thread == AST_PTHREADT_STOP)
  4317. return 0;
  4318. if (ast_mutex_lock(&monlock)) {
  4319. ast_log(LOG_WARNING, "Unable to lock monitor\n");
  4320. return -1;
  4321. }
  4322. if (monitor_thread == pthread_self()) {
  4323. ast_mutex_unlock(&monlock);
  4324. ast_log(LOG_WARNING, "Cannot kill myself\n");
  4325. return -1;
  4326. }
  4327. if (monitor_thread != AST_PTHREADT_NULL) {
  4328. /* Wake up the thread */
  4329. pthread_kill(monitor_thread, SIGURG);
  4330. } else {
  4331. pthread_attr_init(&attr);
  4332. pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);
  4333. /* Start a new monitor */
  4334. if (ast_pthread_create(&monitor_thread, &attr, do_monitor, NULL) < 0) {
  4335. ast_mutex_unlock(&monlock);
  4336. ast_log(LOG_ERROR, "Unable to start monitor thread.\n");
  4337. return -1;
  4338. }
  4339. }
  4340. ast_mutex_unlock(&monlock);
  4341. return 0;
  4342. }
  4343. /*--- unistim_request: PBX interface function ---*/
  4344. /* UNISTIM calls initiated by the PBX arrive here */
  4345. static struct ast_channel *unistim_request(const char *type, format_t format, const struct ast_channel *requestor, void *data,
  4346. int *cause)
  4347. {
  4348. format_t oldformat;
  4349. struct unistim_subchannel *sub;
  4350. struct ast_channel *tmpc = NULL;
  4351. char tmp[256];
  4352. char *dest = data;
  4353. oldformat = format;
  4354. format &= CAPABILITY;
  4355. ast_log(LOG_NOTICE,
  4356. "Asked to get a channel of format %s while capability is %s result : %s\n",
  4357. ast_getformatname(oldformat),
  4358. ast_getformatname_multiple(tmp, sizeof(tmp), CAPABILITY),
  4359. ast_getformatname(format));
  4360. if (!format) {
  4361. ast_log(LOG_NOTICE,
  4362. "Asked to get a channel of unsupported format %s while capability is %s\n",
  4363. ast_getformatname(oldformat), ast_getformatname_multiple(tmp, sizeof(tmp), CAPABILITY));
  4364. return NULL;
  4365. }
  4366. ast_copy_string(tmp, dest, sizeof(tmp));
  4367. if (ast_strlen_zero(tmp)) {
  4368. ast_log(LOG_NOTICE, "Unistim channels require a device\n");
  4369. return NULL;
  4370. }
  4371. sub = find_subchannel_by_name(tmp);
  4372. if (!sub) {
  4373. ast_log(LOG_NOTICE, "No available lines on: %s\n", dest);
  4374. *cause = AST_CAUSE_CONGESTION;
  4375. return NULL;
  4376. }
  4377. ast_verb(3, "unistim_request(%s)\n", tmp);
  4378. /* Busy ? */
  4379. if (sub->owner) {
  4380. if (unistimdebug)
  4381. ast_verb(0, "Can't create channel : Busy !\n");
  4382. *cause = AST_CAUSE_BUSY;
  4383. return NULL;
  4384. }
  4385. sub->parent->capability = format;
  4386. tmpc = unistim_new(sub, AST_STATE_DOWN, requestor ? requestor->linkedid : NULL);
  4387. if (!tmpc)
  4388. ast_log(LOG_WARNING, "Unable to make channel for '%s'\n", tmp);
  4389. if (unistimdebug)
  4390. ast_verb(0, "unistim_request owner = %p\n", sub->owner);
  4391. restart_monitor();
  4392. /* and finish */
  4393. return tmpc;
  4394. }
  4395. static char *unistim_info(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
  4396. {
  4397. struct unistim_device *device = devices;
  4398. struct unistim_line *line;
  4399. struct unistim_subchannel *sub;
  4400. struct unistimsession *s;
  4401. int i;
  4402. struct ast_channel *tmp;
  4403. switch (cmd) {
  4404. case CLI_INIT:
  4405. e->command = "unistim show info";
  4406. e->usage =
  4407. "Usage: unistim show info\n"
  4408. " Dump internal structures.\n";
  4409. return NULL;
  4410. case CLI_GENERATE:
  4411. return NULL; /* no completion */
  4412. }
  4413. if (a->argc != e->args)
  4414. return CLI_SHOWUSAGE;
  4415. ast_cli(a->fd, "Dumping internal structures :\ndevice\n->line\n-->sub\n");
  4416. while (device) {
  4417. ast_cli(a->fd, "\nname=%s id=%s line=%p ha=%p sess=%p device=%p\n",
  4418. device->name, device->id, device->lines, device->ha, device->session,
  4419. device);
  4420. line = device->lines;
  4421. while (line) {
  4422. ast_cli(a->fd,
  4423. "->name=%s fullname=%s exten=%s callid=%s cap=%" PRId64 " device=%p line=%p\n",
  4424. line->name, line->fullname, line->exten, line->cid_num,
  4425. line->capability, line->parent, line);
  4426. for (i = 0; i < MAX_SUBS; i++) {
  4427. sub = line->subs[i];
  4428. if (!sub)
  4429. continue;
  4430. if (!sub->owner)
  4431. tmp = (void *) -42;
  4432. else
  4433. tmp = sub->owner->_bridge;
  4434. if (sub->subtype != i)
  4435. ast_cli(a->fd, "Warning ! subchannel->subs[%d] have a subtype=%u\n", i,
  4436. sub->subtype);
  4437. ast_cli(a->fd,
  4438. "-->subtype=%u chan=%p rtp=%p bridge=%p line=%p alreadygone=%d\n",
  4439. sub->subtype, sub->owner, sub->rtp, tmp, sub->parent,
  4440. sub->alreadygone);
  4441. }
  4442. line = line->next;
  4443. }
  4444. device = device->next;
  4445. }
  4446. ast_cli(a->fd, "\nSessions:\n");
  4447. ast_mutex_lock(&sessionlock);
  4448. s = sessions;
  4449. while (s) {
  4450. ast_cli(a->fd,
  4451. "sin=%s timeout=%d state=%d macaddr=%s device=%p session=%p\n",
  4452. ast_inet_ntoa(s->sin.sin_addr), s->timeout, s->state, s->macaddr,
  4453. s->device, s);
  4454. s = s->next;
  4455. }
  4456. ast_mutex_unlock(&sessionlock);
  4457. return CLI_SUCCESS;
  4458. }
  4459. static char *unistim_sp(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
  4460. {
  4461. BUFFSEND;
  4462. struct unistim_subchannel *sub;
  4463. int i, j = 0, len;
  4464. unsigned char c, cc;
  4465. char tmp[256];
  4466. switch (cmd) {
  4467. case CLI_INIT:
  4468. e->command = "unistim send packet";
  4469. e->usage =
  4470. "Usage: unistim send packet USTM/line@name hexa\n"
  4471. " unistim send packet USTM/1000@hans 19040004\n";
  4472. return NULL;
  4473. case CLI_GENERATE:
  4474. return NULL; /* no completion */
  4475. }
  4476. if (a->argc < 5)
  4477. return CLI_SHOWUSAGE;
  4478. if (strlen(a->argv[3]) < 9)
  4479. return CLI_SHOWUSAGE;
  4480. len = strlen(a->argv[4]);
  4481. if (len % 2)
  4482. return CLI_SHOWUSAGE;
  4483. ast_copy_string(tmp, a->argv[3] + 5, sizeof(tmp));
  4484. sub = find_subchannel_by_name(tmp);
  4485. if (!sub) {
  4486. ast_cli(a->fd, "Can't find '%s'\n", tmp);
  4487. return CLI_SUCCESS;
  4488. }
  4489. if (!sub->parent->parent->session) {
  4490. ast_cli(a->fd, "'%s' is not connected\n", tmp);
  4491. return CLI_SUCCESS;
  4492. }
  4493. ast_cli(a->fd, "Sending '%s' to %s (%p)\n", a->argv[4], tmp, sub->parent->parent->session);
  4494. for (i = 0; i < len; i++) {
  4495. c = a->argv[4][i];
  4496. if (c >= 'a')
  4497. c -= 'a' - 10;
  4498. else
  4499. c -= '0';
  4500. i++;
  4501. cc = a->argv[4][i];
  4502. if (cc >= 'a')
  4503. cc -= 'a' - 10;
  4504. else
  4505. cc -= '0';
  4506. tmp[j++] = (c << 4) | cc;
  4507. }
  4508. memcpy(buffsend + SIZE_HEADER, tmp, j);
  4509. send_client(SIZE_HEADER + j, buffsend, sub->parent->parent->session);
  4510. return CLI_SUCCESS;
  4511. }
  4512. static char *unistim_do_debug(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
  4513. {
  4514. switch (cmd) {
  4515. case CLI_INIT:
  4516. e->command = "unistim set debug {on|off}";
  4517. e->usage =
  4518. "Usage: unistim set debug\n"
  4519. " Display debug messages.\n";
  4520. return NULL;
  4521. case CLI_GENERATE:
  4522. return NULL; /* no completion */
  4523. }
  4524. if (a->argc != e->args)
  4525. return CLI_SHOWUSAGE;
  4526. if (!strcasecmp(a->argv[3], "on")) {
  4527. unistimdebug = 1;
  4528. ast_cli(a->fd, "UNISTIM Debugging Enabled\n");
  4529. } else if (!strcasecmp(a->argv[3], "off")) {
  4530. unistimdebug = 0;
  4531. ast_cli(a->fd, "UNISTIM Debugging Disabled\n");
  4532. } else
  4533. return CLI_SHOWUSAGE;
  4534. return CLI_SUCCESS;
  4535. }
  4536. /*! \brief --- unistim_reload: Force reload of module from cli ---
  4537. * Runs in the asterisk main thread, so don't do anything useful
  4538. * but setting a flag and waiting for do_monitor to do the job
  4539. * in our thread */
  4540. static char *unistim_reload(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
  4541. {
  4542. switch (cmd) {
  4543. case CLI_INIT:
  4544. e->command = "unistim reload";
  4545. e->usage =
  4546. "Usage: unistim reload\n"
  4547. " Reloads UNISTIM configuration from unistim.conf\n";
  4548. return NULL;
  4549. case CLI_GENERATE:
  4550. return NULL; /* no completion */
  4551. }
  4552. if (e && a && a->argc != e->args)
  4553. return CLI_SHOWUSAGE;
  4554. if (unistimdebug)
  4555. ast_verb(0, "reload unistim\n");
  4556. ast_mutex_lock(&unistim_reload_lock);
  4557. if (!unistim_reloading)
  4558. unistim_reloading = 1;
  4559. ast_mutex_unlock(&unistim_reload_lock);
  4560. restart_monitor();
  4561. return CLI_SUCCESS;
  4562. }
  4563. static struct ast_cli_entry unistim_cli[] = {
  4564. AST_CLI_DEFINE(unistim_reload, "Reload UNISTIM configuration"),
  4565. AST_CLI_DEFINE(unistim_info, "Show UNISTIM info"),
  4566. AST_CLI_DEFINE(unistim_sp, "Send packet (for reverse engineering)"),
  4567. AST_CLI_DEFINE(unistim_do_debug, "Toggle UNITSTIM debugging"),
  4568. };
  4569. static void unquote(char *out, const char *src, int maxlen)
  4570. {
  4571. int len = strlen(src);
  4572. if (!len)
  4573. return;
  4574. if ((len > 1) && src[0] == '\"') {
  4575. /* This is a quoted string */
  4576. src++;
  4577. /* Don't take more than what's there */
  4578. len--;
  4579. if (maxlen > len - 1)
  4580. maxlen = len - 1;
  4581. memcpy(out, src, maxlen);
  4582. ((char *) out)[maxlen] = '\0';
  4583. } else
  4584. memcpy(out, src, maxlen);
  4585. return;
  4586. }
  4587. static int ParseBookmark(const char *text, struct unistim_device *d)
  4588. {
  4589. char line[256];
  4590. char *at;
  4591. char *number;
  4592. char *icon;
  4593. int p;
  4594. int len = strlen(text);
  4595. ast_copy_string(line, text, sizeof(line));
  4596. /* Position specified ? */
  4597. if ((len > 2) && (line[1] == '@')) {
  4598. p = line[0];
  4599. if ((p >= '0') && (p <= '5'))
  4600. p -= '0';
  4601. else {
  4602. ast_log(LOG_WARNING,
  4603. "Invalid position for bookmark : must be between 0 and 5\n");
  4604. return 0;
  4605. }
  4606. if (d->softkeyicon[p] != 0) {
  4607. ast_log(LOG_WARNING, "Invalid position %d for bookmark : already used\n:", p);
  4608. return 0;
  4609. }
  4610. memmove(line, line + 2, sizeof(line) - 2);
  4611. } else {
  4612. /* No position specified, looking for a free slot */
  4613. for (p = 0; p <= 5; p++) {
  4614. if (!d->softkeyicon[p])
  4615. break;
  4616. }
  4617. if (p > 5) {
  4618. ast_log(LOG_WARNING, "No more free bookmark position\n");
  4619. return 0;
  4620. }
  4621. }
  4622. at = strchr(line, '@');
  4623. if (!at) {
  4624. ast_log(LOG_NOTICE, "Bookmark entry '%s' has no @ (at) sign!\n", text);
  4625. return 0;
  4626. }
  4627. *at = '\0';
  4628. at++;
  4629. number = at;
  4630. at = strchr(at, '@');
  4631. if (ast_strlen_zero(number)) {
  4632. ast_log(LOG_NOTICE, "Bookmark entry '%s' has no number\n", text);
  4633. return 0;
  4634. }
  4635. if (ast_strlen_zero(line)) {
  4636. ast_log(LOG_NOTICE, "Bookmark entry '%s' has no description\n", text);
  4637. return 0;
  4638. }
  4639. at = strchr(number, '@');
  4640. if (!at)
  4641. d->softkeyicon[p] = FAV_ICON_SHARP; /* default icon */
  4642. else {
  4643. *at = '\0';
  4644. at++;
  4645. icon = at;
  4646. if (ast_strlen_zero(icon)) {
  4647. ast_log(LOG_NOTICE, "Bookmark entry '%s' has no icon value\n", text);
  4648. return 0;
  4649. }
  4650. if (strncmp(icon, "USTM/", 5))
  4651. d->softkeyicon[p] = atoi(icon);
  4652. else {
  4653. d->softkeyicon[p] = 1;
  4654. ast_copy_string(d->softkeydevice[p], icon + 5, sizeof(d->softkeydevice[p]));
  4655. }
  4656. }
  4657. ast_copy_string(d->softkeylabel[p], line, sizeof(d->softkeylabel[p]));
  4658. ast_copy_string(d->softkeynumber[p], number, sizeof(d->softkeynumber[p]));
  4659. if (unistimdebug)
  4660. ast_verb(0, "New bookmark at pos %d label='%s' number='%s' icon=%x\n",
  4661. p, d->softkeylabel[p], d->softkeynumber[p], (unsigned)d->softkeyicon[p]);
  4662. return 1;
  4663. }
  4664. /* Looking for dynamic icons entries in bookmarks */
  4665. static void finish_bookmark(void)
  4666. {
  4667. struct unistim_device *d = devices;
  4668. int i;
  4669. while (d) {
  4670. for (i = 0; i < 6; i++) {
  4671. if (d->softkeyicon[i] == 1) { /* Something for us */
  4672. struct unistim_device *d2 = devices;
  4673. while (d2) {
  4674. if (!strcmp(d->softkeydevice[i], d2->name)) {
  4675. d->sp[i] = d2;
  4676. d->softkeyicon[i] = 0;
  4677. break;
  4678. }
  4679. d2 = d2->next;
  4680. }
  4681. if (d->sp[i] == NULL)
  4682. ast_log(LOG_NOTICE, "Bookmark entry with device %s not found\n",
  4683. d->softkeydevice[i]);
  4684. }
  4685. }
  4686. d = d->next;
  4687. }
  4688. }
  4689. static struct unistim_device *build_device(const char *cat, const struct ast_variable *v)
  4690. {
  4691. struct unistim_device *d;
  4692. struct unistim_line *l = NULL;
  4693. int create = 1;
  4694. int nbsoftkey, dateformat, timeformat, callhistory;
  4695. char linelabel[AST_MAX_EXTENSION];
  4696. char context[AST_MAX_EXTENSION];
  4697. char ringvolume, ringstyle;
  4698. /* First, we need to know if we already have this name in our list */
  4699. /* Get a lock for the device chained list */
  4700. ast_mutex_lock(&devicelock);
  4701. d = devices;
  4702. while (d) {
  4703. if (!strcmp(d->name, cat)) {
  4704. /* Yep, we alreay have this one */
  4705. if (unistimsock < 0) {
  4706. /* It's a dupe */
  4707. ast_log(LOG_WARNING, "Duplicate entry found (%s), ignoring.\n", cat);
  4708. ast_mutex_unlock(&devicelock);
  4709. return NULL;
  4710. }
  4711. /* we're reloading right now */
  4712. create = 0;
  4713. l = d->lines;
  4714. break;
  4715. }
  4716. d = d->next;
  4717. }
  4718. ast_mutex_unlock(&devicelock);
  4719. if (create) {
  4720. if (!(d = ast_calloc(1, sizeof(*d))))
  4721. return NULL;
  4722. if (!(l = ast_calloc(1, sizeof(*l)))) {
  4723. ast_free(d);
  4724. return NULL;
  4725. }
  4726. ast_copy_string(d->name, cat, sizeof(d->name));
  4727. d->contrast = -1;
  4728. d->output = OUTPUT_HANDSET;
  4729. d->previous_output = OUTPUT_HANDSET;
  4730. d->volume = VOLUME_LOW;
  4731. d->mute = MUTE_OFF;
  4732. d->height = DEFAULTHEIGHT;
  4733. }
  4734. ast_copy_string(context, DEFAULTCONTEXT, sizeof(context));
  4735. linelabel[0] = '\0';
  4736. dateformat = 1;
  4737. timeformat = 1;
  4738. ringvolume = 2;
  4739. callhistory = 1;
  4740. ringstyle = 3;
  4741. nbsoftkey = 0;
  4742. while (v) {
  4743. if (!strcasecmp(v->name, "rtp_port"))
  4744. d->rtp_port = atoi(v->value);
  4745. else if (!strcasecmp(v->name, "rtp_method"))
  4746. d->rtp_method = atoi(v->value);
  4747. else if (!strcasecmp(v->name, "status_method"))
  4748. d->status_method = atoi(v->value);
  4749. else if (!strcasecmp(v->name, "device"))
  4750. ast_copy_string(d->id, v->value, sizeof(d->id));
  4751. else if (!strcasecmp(v->name, "tn"))
  4752. ast_copy_string(d->extension_number, v->value, sizeof(d->extension_number));
  4753. else if (!strcasecmp(v->name, "permit") || !strcasecmp(v->name, "deny"))
  4754. d->ha = ast_append_ha(v->name, v->value, d->ha, NULL);
  4755. else if (!strcasecmp(v->name, "context"))
  4756. ast_copy_string(context, v->value, sizeof(context));
  4757. else if (!strcasecmp(v->name, "maintext0"))
  4758. unquote(d->maintext0, v->value, sizeof(d->maintext0) - 1);
  4759. else if (!strcasecmp(v->name, "maintext1"))
  4760. unquote(d->maintext1, v->value, sizeof(d->maintext1) - 1);
  4761. else if (!strcasecmp(v->name, "maintext2"))
  4762. unquote(d->maintext2, v->value, sizeof(d->maintext2) - 1);
  4763. else if (!strcasecmp(v->name, "titledefault"))
  4764. unquote(d->titledefault, v->value, sizeof(d->titledefault) - 1);
  4765. else if (!strcasecmp(v->name, "dateformat"))
  4766. dateformat = atoi(v->value);
  4767. else if (!strcasecmp(v->name, "timeformat"))
  4768. timeformat = atoi(v->value);
  4769. else if (!strcasecmp(v->name, "contrast")) {
  4770. d->contrast = atoi(v->value);
  4771. if ((d->contrast < 0) || (d->contrast > 15)) {
  4772. ast_log(LOG_WARNING, "contrast must be beetween 0 and 15\n");
  4773. d->contrast = 8;
  4774. }
  4775. } else if (!strcasecmp(v->name, "nat"))
  4776. d->nat = ast_true(v->value);
  4777. else if (!strcasecmp(v->name, "ringvolume"))
  4778. ringvolume = atoi(v->value);
  4779. else if (!strcasecmp(v->name, "ringstyle"))
  4780. ringstyle = atoi(v->value);
  4781. else if (!strcasecmp(v->name, "callhistory"))
  4782. callhistory = atoi(v->value);
  4783. else if (!strcasecmp(v->name, "callerid")) {
  4784. if (!strcasecmp(v->value, "asreceived"))
  4785. l->cid_num[0] = '\0';
  4786. else
  4787. ast_copy_string(l->cid_num, v->value, sizeof(l->cid_num));
  4788. } else if (!strcasecmp(v->name, "language"))
  4789. ast_copy_string(l->language, v->value, sizeof(l->language));
  4790. else if (!strcasecmp(v->name, "country"))
  4791. ast_copy_string(d->country, v->value, sizeof(d->country));
  4792. else if (!strcasecmp(v->name, "accountcode"))
  4793. ast_copy_string(l->accountcode, v->value, sizeof(l->accountcode));
  4794. else if (!strcasecmp(v->name, "amaflags")) {
  4795. int y;
  4796. y = ast_cdr_amaflags2int(v->value);
  4797. if (y < 0)
  4798. ast_log(LOG_WARNING, "Invalid AMA flags: %s at line %d\n", v->value,
  4799. v->lineno);
  4800. else
  4801. l->amaflags = y;
  4802. } else if (!strcasecmp(v->name, "musiconhold"))
  4803. ast_copy_string(l->musicclass, v->value, sizeof(l->musicclass));
  4804. else if (!strcasecmp(v->name, "callgroup"))
  4805. l->callgroup = ast_get_group(v->value);
  4806. else if (!strcasecmp(v->name, "pickupgroup"))
  4807. l->pickupgroup = ast_get_group(v->value);
  4808. else if (!strcasecmp(v->name, "mailbox"))
  4809. ast_copy_string(l->mailbox, v->value, sizeof(l->mailbox));
  4810. else if (!strcasecmp(v->name, "parkinglot"))
  4811. ast_copy_string(l->parkinglot, v->value, sizeof(l->parkinglot));
  4812. else if (!strcasecmp(v->name, "linelabel"))
  4813. unquote(linelabel, v->value, sizeof(linelabel) - 1);
  4814. else if (!strcasecmp(v->name, "extension")) {
  4815. if (!strcasecmp(v->value, "none"))
  4816. d->extension = EXTENSION_NONE;
  4817. else if (!strcasecmp(v->value, "ask"))
  4818. d->extension = EXTENSION_ASK;
  4819. else if (!strcasecmp(v->value, "line"))
  4820. d->extension = EXTENSION_LINE;
  4821. else
  4822. ast_log(LOG_WARNING, "Unknown extension option.\n");
  4823. } else if (!strcasecmp(v->name, "bookmark")) {
  4824. if (nbsoftkey > 5)
  4825. ast_log(LOG_WARNING,
  4826. "More than 6 softkeys defined. Ignoring new entries.\n");
  4827. else {
  4828. if (ParseBookmark(v->value, d))
  4829. nbsoftkey++;
  4830. }
  4831. } else if (!strcasecmp(v->name, "line")) {
  4832. int len = strlen(linelabel);
  4833. if (nbsoftkey) {
  4834. ast_log(LOG_WARNING,
  4835. "You must use bookmark AFTER line=>. Only one line is supported in this version\n");
  4836. if (create) {
  4837. ast_free(d);
  4838. ast_free(l);
  4839. }
  4840. return NULL;
  4841. }
  4842. if (create) {
  4843. ast_mutex_init(&l->lock);
  4844. } else {
  4845. d->to_delete = 0;
  4846. /* reset bookmarks */
  4847. memset(d->softkeylabel, 0, sizeof(d->softkeylabel));
  4848. memset(d->softkeynumber, 0, sizeof(d->softkeynumber));
  4849. memset(d->softkeyicon, 0, sizeof(d->softkeyicon));
  4850. memset(d->softkeydevice, 0, sizeof(d->softkeydevice));
  4851. memset(d->sp, 0, sizeof(d->sp));
  4852. }
  4853. ast_copy_string(l->name, v->value, sizeof(l->name));
  4854. snprintf(l->fullname, sizeof(l->fullname), "USTM/%s@%s", l->name, d->name);
  4855. d->softkeyicon[0] = FAV_ICON_ONHOOK_BLACK;
  4856. if (!len) /* label is undefined ? */
  4857. ast_copy_string(d->softkeylabel[0], v->value, sizeof(d->softkeylabel[0]));
  4858. else {
  4859. if ((len > 2) && (linelabel[1] == '@')) {
  4860. d->softkeylinepos = linelabel[0];
  4861. if ((d->softkeylinepos >= '0') && (d->softkeylinepos <= '5')) {
  4862. d->softkeylinepos -= '0';
  4863. d->softkeyicon[0] = 0;
  4864. } else {
  4865. ast_log(LOG_WARNING,
  4866. "Invalid position for linelabel : must be between 0 and 5\n");
  4867. d->softkeylinepos = 0;
  4868. }
  4869. ast_copy_string(d->softkeylabel[d->softkeylinepos], linelabel + 2,
  4870. sizeof(d->softkeylabel[d->softkeylinepos]));
  4871. d->softkeyicon[d->softkeylinepos] = FAV_ICON_ONHOOK_BLACK;
  4872. } else
  4873. ast_copy_string(d->softkeylabel[0], linelabel,
  4874. sizeof(d->softkeylabel[0]));
  4875. }
  4876. nbsoftkey++;
  4877. ast_copy_string(l->context, context, sizeof(l->context));
  4878. if (!ast_strlen_zero(l->mailbox)) {
  4879. if (unistimdebug)
  4880. ast_verb(3, "Setting mailbox '%s' on %s@%s\n", l->mailbox, d->name, l->name);
  4881. }
  4882. l->capability = CAPABILITY;
  4883. l->parent = d;
  4884. if (create) {
  4885. if (!alloc_sub(l, SUB_REAL)) {
  4886. ast_mutex_destroy(&l->lock);
  4887. ast_free(l);
  4888. ast_free(d);
  4889. return NULL;
  4890. }
  4891. l->next = d->lines;
  4892. d->lines = l;
  4893. }
  4894. } else if (!strcasecmp(v->name, "height")) {
  4895. /* Allow the user to lower the expected display lines on the phone
  4896. * For example the Nortal I2001 and I2002 only have one ! */
  4897. d->height = atoi(v->value);
  4898. } else
  4899. ast_log(LOG_WARNING, "Don't know keyword '%s' at line %d\n", v->name,
  4900. v->lineno);
  4901. v = v->next;
  4902. }
  4903. d->ringvolume = ringvolume;
  4904. d->ringstyle = ringstyle;
  4905. d->callhistory = callhistory;
  4906. d->tz = ast_get_indication_zone(d->country);
  4907. if ((d->tz == NULL) && !ast_strlen_zero(d->country))
  4908. ast_log(LOG_WARNING, "Country '%s' was not found in indications.conf\n",
  4909. d->country);
  4910. d->datetimeformat = 56 + (dateformat * 4);
  4911. d->datetimeformat += timeformat;
  4912. if (!d->lines) {
  4913. ast_log(LOG_ERROR, "An Unistim device must have at least one line!\n");
  4914. ast_mutex_destroy(&l->lock);
  4915. ast_free(l);
  4916. if (d->tz) {
  4917. d->tz = ast_tone_zone_unref(d->tz);
  4918. }
  4919. ast_free(d);
  4920. return NULL;
  4921. }
  4922. if ((autoprovisioning == AUTOPROVISIONING_TN) &&
  4923. (!ast_strlen_zero(d->extension_number))) {
  4924. d->extension = EXTENSION_TN;
  4925. if (!ast_strlen_zero(d->id))
  4926. ast_log(LOG_WARNING,
  4927. "tn= and device= can't be used together. Ignoring device= entry\n");
  4928. d->id[0] = 'T'; /* magic : this is a tn entry */
  4929. ast_copy_string((d->id) + 1, d->extension_number, sizeof(d->id) - 1);
  4930. d->extension_number[0] = '\0';
  4931. } else if (ast_strlen_zero(d->id)) {
  4932. if (strcmp(d->name, "template")) {
  4933. ast_log(LOG_ERROR, "You must specify the mac address with device=\n");
  4934. ast_mutex_destroy(&l->lock);
  4935. ast_free(l);
  4936. if (d->tz) {
  4937. d->tz = ast_tone_zone_unref(d->tz);
  4938. }
  4939. ast_free(d);
  4940. return NULL;
  4941. } else
  4942. strcpy(d->id, "000000000000");
  4943. }
  4944. if (!d->rtp_port)
  4945. d->rtp_port = 10000;
  4946. if (d->contrast == -1)
  4947. d->contrast = 8;
  4948. if (ast_strlen_zero(d->maintext0))
  4949. strcpy(d->maintext0, "Welcome");
  4950. if (ast_strlen_zero(d->maintext1))
  4951. strcpy(d->maintext1, d->name);
  4952. if (ast_strlen_zero(d->titledefault)) {
  4953. struct ast_tm tm = { 0, };
  4954. struct timeval cur_time = ast_tvnow();
  4955. if ((ast_localtime(&cur_time, &tm, 0)) == 0 || ast_strlen_zero(tm.tm_zone)) {
  4956. display_last_error("Error in ast_localtime()");
  4957. ast_copy_string(d->titledefault, "UNISTIM for*", 12);
  4958. } else {
  4959. if (strlen(tm.tm_zone) < 4) {
  4960. strcpy(d->titledefault, "TimeZone ");
  4961. strcat(d->titledefault, tm.tm_zone);
  4962. } else if (strlen(tm.tm_zone) < 9) {
  4963. strcpy(d->titledefault, "TZ ");
  4964. strcat(d->titledefault, tm.tm_zone);
  4965. } else
  4966. ast_copy_string(d->titledefault, tm.tm_zone, 12);
  4967. }
  4968. }
  4969. /* Update the chained link if it's a new device */
  4970. if (create) {
  4971. ast_mutex_lock(&devicelock);
  4972. d->next = devices;
  4973. devices = d;
  4974. ast_mutex_unlock(&devicelock);
  4975. ast_verb(3, "Added device '%s'\n", d->name);
  4976. } else {
  4977. ast_verb(3, "Device '%s' reloaded\n", d->name);
  4978. }
  4979. return d;
  4980. }
  4981. /*--- reload_config: Re-read unistim.conf config file ---*/
  4982. static int reload_config(void)
  4983. {
  4984. struct ast_config *cfg;
  4985. struct ast_variable *v;
  4986. struct ast_hostent ahp;
  4987. struct hostent *hp;
  4988. struct sockaddr_in bindaddr = { 0, };
  4989. char *config = "unistim.conf";
  4990. char *cat;
  4991. struct unistim_device *d;
  4992. const int reuseFlag = 1;
  4993. struct unistimsession *s;
  4994. struct ast_flags config_flags = { 0, };
  4995. cfg = ast_config_load(config, config_flags);
  4996. /* We *must* have a config file otherwise stop immediately */
  4997. if (!cfg) {
  4998. ast_log(LOG_ERROR, "Unable to load config %s\n", config);
  4999. return -1;
  5000. } else if (cfg == CONFIG_STATUS_FILEINVALID) {
  5001. ast_log(LOG_ERROR, "Config file %s is in an invalid format. Aborting.\n", config);
  5002. return -1;
  5003. }
  5004. /* Copy the default jb config over global_jbconf */
  5005. memcpy(&global_jbconf, &default_jbconf, sizeof(struct ast_jb_conf));
  5006. unistim_keepalive = 120;
  5007. unistim_port = 0;
  5008. v = ast_variable_browse(cfg, "general");
  5009. while (v) {
  5010. /* handle jb conf */
  5011. if (!ast_jb_read_conf(&global_jbconf, v->name, v->value))
  5012. continue;
  5013. if (!strcasecmp(v->name, "keepalive"))
  5014. unistim_keepalive = atoi(v->value);
  5015. else if (!strcasecmp(v->name, "port"))
  5016. unistim_port = atoi(v->value);
  5017. else if (!strcasecmp(v->name, "tos")) {
  5018. if (ast_str2tos(v->value, &qos.tos))
  5019. ast_log(LOG_WARNING, "Invalid tos value at line %d, refer to QoS documentation\n", v->lineno);
  5020. } else if (!strcasecmp(v->name, "tos_audio")) {
  5021. if (ast_str2tos(v->value, &qos.tos_audio))
  5022. ast_log(LOG_WARNING, "Invalid tos_audio value at line %d, refer to QoS documentation\n", v->lineno);
  5023. } else if (!strcasecmp(v->name, "cos")) {
  5024. if (ast_str2cos(v->value, &qos.cos))
  5025. ast_log(LOG_WARNING, "Invalid cos value at line %d, refer to QoS documentation\n", v->lineno);
  5026. } else if (!strcasecmp(v->name, "cos_audio")) {
  5027. if (ast_str2cos(v->value, &qos.cos_audio))
  5028. ast_log(LOG_WARNING, "Invalid cos_audio value at line %d, refer to QoS documentation\n", v->lineno);
  5029. } else if (!strcasecmp(v->name, "autoprovisioning")) {
  5030. if (!strcasecmp(v->value, "no"))
  5031. autoprovisioning = AUTOPROVISIONING_NO;
  5032. else if (!strcasecmp(v->value, "yes"))
  5033. autoprovisioning = AUTOPROVISIONING_YES;
  5034. else if (!strcasecmp(v->value, "db"))
  5035. autoprovisioning = AUTOPROVISIONING_DB;
  5036. else if (!strcasecmp(v->value, "tn"))
  5037. autoprovisioning = AUTOPROVISIONING_TN;
  5038. else
  5039. ast_log(LOG_WARNING, "Unknown autoprovisioning option.\n");
  5040. } else if (!strcasecmp(v->name, "public_ip")) {
  5041. if (!ast_strlen_zero(v->value)) {
  5042. if (!(hp = ast_gethostbyname(v->value, &ahp)))
  5043. ast_log(LOG_WARNING, "Invalid address: %s\n", v->value);
  5044. else {
  5045. memcpy(&public_ip.sin_addr, hp->h_addr, sizeof(public_ip.sin_addr));
  5046. public_ip.sin_family = AF_INET;
  5047. }
  5048. }
  5049. }
  5050. v = v->next;
  5051. }
  5052. if ((unistim_keepalive < 10) ||
  5053. (unistim_keepalive >
  5054. 255 - (((NB_MAX_RETRANSMIT + 1) * RETRANSMIT_TIMER) / 1000))) {
  5055. ast_log(LOG_ERROR, "keepalive is invalid in %s\n", config);
  5056. ast_config_destroy(cfg);
  5057. return -1;
  5058. }
  5059. packet_send_ping[4] =
  5060. unistim_keepalive + (((NB_MAX_RETRANSMIT + 1) * RETRANSMIT_TIMER) / 1000);
  5061. if ((unistim_port < 1) || (unistim_port > 65535)) {
  5062. ast_log(LOG_ERROR, "port is not set or invalid in %s\n", config);
  5063. ast_config_destroy(cfg);
  5064. return -1;
  5065. }
  5066. unistim_keepalive *= 1000;
  5067. ast_mutex_lock(&devicelock);
  5068. d = devices;
  5069. while (d) {
  5070. if (d->to_delete >= 0)
  5071. d->to_delete = 1;
  5072. d = d->next;
  5073. }
  5074. ast_mutex_unlock(&devicelock);
  5075. /* load the device sections */
  5076. cat = ast_category_browse(cfg, NULL);
  5077. while (cat) {
  5078. if (strcasecmp(cat, "general")) {
  5079. d = build_device(cat, ast_variable_browse(cfg, cat));
  5080. }
  5081. cat = ast_category_browse(cfg, cat);
  5082. }
  5083. ast_mutex_lock(&devicelock);
  5084. d = devices;
  5085. while (d) {
  5086. if (d->to_delete) {
  5087. int i;
  5088. if (unistimdebug)
  5089. ast_verb(0, "Removing device '%s'\n", d->name);
  5090. if (!d->lines) {
  5091. ast_log(LOG_ERROR, "Device '%s' without a line !, aborting\n", d->name);
  5092. ast_config_destroy(cfg);
  5093. return 0;
  5094. }
  5095. if (!d->lines->subs[0]) {
  5096. ast_log(LOG_ERROR, "Device '%s' without a subchannel !, aborting\n",
  5097. d->name);
  5098. ast_config_destroy(cfg);
  5099. return 0;
  5100. }
  5101. if (d->lines->subs[0]->owner) {
  5102. ast_log(LOG_WARNING,
  5103. "Device '%s' was not deleted : a call is in progress. Try again later.\n",
  5104. d->name);
  5105. d = d->next;
  5106. continue;
  5107. }
  5108. ast_mutex_destroy(&d->lines->subs[0]->lock);
  5109. ast_free(d->lines->subs[0]);
  5110. for (i = 1; i < MAX_SUBS; i++) {
  5111. if (d->lines->subs[i]) {
  5112. ast_log(LOG_WARNING,
  5113. "Device '%s' with threeway call subchannels allocated, aborting.\n",
  5114. d->name);
  5115. break;
  5116. }
  5117. }
  5118. if (i < MAX_SUBS) {
  5119. d = d->next;
  5120. continue;
  5121. }
  5122. ast_mutex_destroy(&d->lines->lock);
  5123. ast_free(d->lines);
  5124. if (d->session) {
  5125. if (sessions == d->session)
  5126. sessions = d->session->next;
  5127. else {
  5128. s = sessions;
  5129. while (s) {
  5130. if (s->next == d->session) {
  5131. s->next = d->session->next;
  5132. break;
  5133. }
  5134. s = s->next;
  5135. }
  5136. }
  5137. ast_mutex_destroy(&d->session->lock);
  5138. ast_free(d->session);
  5139. }
  5140. if (devices == d)
  5141. devices = d->next;
  5142. else {
  5143. struct unistim_device *d2 = devices;
  5144. while (d2) {
  5145. if (d2->next == d) {
  5146. d2->next = d->next;
  5147. break;
  5148. }
  5149. d2 = d2->next;
  5150. }
  5151. }
  5152. if (d->tz) {
  5153. d->tz = ast_tone_zone_unref(d->tz);
  5154. }
  5155. ast_free(d);
  5156. d = devices;
  5157. continue;
  5158. }
  5159. d = d->next;
  5160. }
  5161. finish_bookmark();
  5162. ast_mutex_unlock(&devicelock);
  5163. ast_config_destroy(cfg);
  5164. ast_mutex_lock(&sessionlock);
  5165. s = sessions;
  5166. while (s) {
  5167. if (s->device)
  5168. refresh_all_favorite(s);
  5169. s = s->next;
  5170. }
  5171. ast_mutex_unlock(&sessionlock);
  5172. /* We don't recreate a socket when reloading (locks would be necessary). */
  5173. if (unistimsock > -1)
  5174. return 0;
  5175. bindaddr.sin_addr.s_addr = INADDR_ANY;
  5176. bindaddr.sin_port = htons(unistim_port);
  5177. bindaddr.sin_family = AF_INET;
  5178. unistimsock = socket(AF_INET, SOCK_DGRAM, 0);
  5179. if (unistimsock < 0) {
  5180. ast_log(LOG_WARNING, "Unable to create UNISTIM socket: %s\n", strerror(errno));
  5181. return -1;
  5182. }
  5183. #ifdef HAVE_PKTINFO
  5184. {
  5185. const int pktinfoFlag = 1;
  5186. setsockopt(unistimsock, IPPROTO_IP, IP_PKTINFO, &pktinfoFlag,
  5187. sizeof(pktinfoFlag));
  5188. }
  5189. #else
  5190. if (public_ip.sin_family == 0) {
  5191. ast_log(LOG_WARNING,
  5192. "Your OS does not support IP_PKTINFO, you must set public_ip.\n");
  5193. unistimsock = -1;
  5194. return -1;
  5195. }
  5196. #endif
  5197. setsockopt(unistimsock, SOL_SOCKET, SO_REUSEADDR, (const char *) &reuseFlag,
  5198. sizeof(reuseFlag));
  5199. if (bind(unistimsock, (struct sockaddr *) &bindaddr, sizeof(bindaddr)) < 0) {
  5200. ast_log(LOG_WARNING, "Failed to bind to %s:%d: %s\n",
  5201. ast_inet_ntoa(bindaddr.sin_addr), htons(bindaddr.sin_port),
  5202. strerror(errno));
  5203. close(unistimsock);
  5204. unistimsock = -1;
  5205. } else {
  5206. ast_verb(2, "UNISTIM Listening on %s:%d\n", ast_inet_ntoa(bindaddr.sin_addr), htons(bindaddr.sin_port));
  5207. ast_netsock_set_qos(unistimsock, qos.tos, qos.cos, "UNISTIM");
  5208. }
  5209. return 0;
  5210. }
  5211. static enum ast_rtp_glue_result unistim_get_rtp_peer(struct ast_channel *chan, struct ast_rtp_instance **instance)
  5212. {
  5213. struct unistim_subchannel *sub = chan->tech_pvt;
  5214. if (!sub) {
  5215. return AST_RTP_GLUE_RESULT_FORBID;
  5216. }
  5217. if (!sub->rtp) {
  5218. return AST_RTP_GLUE_RESULT_FORBID;
  5219. }
  5220. ao2_ref(sub->rtp, +1);
  5221. *instance = sub->rtp;
  5222. return AST_RTP_GLUE_RESULT_LOCAL;
  5223. }
  5224. 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, format_t codecs, int nat_active)
  5225. {
  5226. struct unistim_subchannel *sub;
  5227. struct sockaddr_in them = { 0, };
  5228. struct sockaddr_in us = { 0, };
  5229. if (!rtp) {
  5230. return 0;
  5231. }
  5232. sub = chan->tech_pvt;
  5233. if (!sub) {
  5234. ast_log(LOG_ERROR, "No Private Structure, this is bad\n");
  5235. return -1;
  5236. }
  5237. {
  5238. struct ast_sockaddr tmp;
  5239. ast_rtp_instance_get_remote_address(rtp, &tmp);
  5240. ast_sockaddr_to_sin(&tmp, &them);
  5241. ast_rtp_instance_get_local_address(rtp, &tmp);
  5242. ast_sockaddr_to_sin(&tmp, &us);
  5243. }
  5244. /* TODO: Set rtp on phone in case of direct rtp (not implemented) */
  5245. return 0;
  5246. }
  5247. static struct ast_rtp_glue unistim_rtp_glue = {
  5248. .type = channel_type,
  5249. .get_rtp_info = unistim_get_rtp_peer,
  5250. .update_peer = unistim_set_rtp_peer,
  5251. };
  5252. /*--- load_module: PBX load module - initialization ---*/
  5253. int load_module(void)
  5254. {
  5255. int res;
  5256. if (!(buff = ast_malloc(SIZE_PAGE)))
  5257. goto buff_failed;
  5258. io = io_context_create();
  5259. if (!io) {
  5260. ast_log(LOG_ERROR, "Failed to allocate IO context\n");
  5261. goto io_failed;
  5262. }
  5263. sched = sched_context_create();
  5264. if (!sched) {
  5265. ast_log(LOG_ERROR, "Failed to allocate scheduler context\n");
  5266. goto sched_failed;
  5267. }
  5268. res = reload_config();
  5269. if (res)
  5270. return AST_MODULE_LOAD_DECLINE;
  5271. /* Make sure we can register our unistim channel type */
  5272. if (ast_channel_register(&unistim_tech)) {
  5273. ast_log(LOG_ERROR, "Unable to register channel type '%s'\n", channel_type);
  5274. goto chanreg_failed;
  5275. }
  5276. ast_rtp_glue_register(&unistim_rtp_glue);
  5277. ast_cli_register_multiple(unistim_cli, ARRAY_LEN(unistim_cli));
  5278. restart_monitor();
  5279. return AST_MODULE_LOAD_SUCCESS;
  5280. chanreg_failed:
  5281. /*! XXX \todo Leaking anything allocated by reload_config() ... */
  5282. sched_context_destroy(sched);
  5283. sched = NULL;
  5284. sched_failed:
  5285. io_context_destroy(io);
  5286. io = NULL;
  5287. io_failed:
  5288. ast_free(buff);
  5289. buff = NULL;
  5290. buff_failed:
  5291. return AST_MODULE_LOAD_FAILURE;
  5292. }
  5293. static int unload_module(void)
  5294. {
  5295. /* First, take us out of the channel loop */
  5296. if (sched)
  5297. sched_context_destroy(sched);
  5298. ast_cli_unregister_multiple(unistim_cli, ARRAY_LEN(unistim_cli));
  5299. ast_channel_unregister(&unistim_tech);
  5300. ast_rtp_glue_unregister(&unistim_rtp_glue);
  5301. ast_mutex_lock(&monlock);
  5302. if (monitor_thread && (monitor_thread != AST_PTHREADT_STOP) && (monitor_thread != AST_PTHREADT_NULL)) {
  5303. pthread_cancel(monitor_thread);
  5304. pthread_kill(monitor_thread, SIGURG);
  5305. pthread_join(monitor_thread, NULL);
  5306. }
  5307. monitor_thread = AST_PTHREADT_STOP;
  5308. ast_mutex_unlock(&monlock);
  5309. if (buff)
  5310. ast_free(buff);
  5311. if (unistimsock > -1)
  5312. close(unistimsock);
  5313. return 0;
  5314. }
  5315. /*! reload: Part of Asterisk module interface ---*/
  5316. int reload(void)
  5317. {
  5318. unistim_reload(NULL, 0, NULL);
  5319. return 0;
  5320. }
  5321. AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_DEFAULT, "UNISTIM Protocol (USTM)",
  5322. .load = load_module,
  5323. .unload = unload_module,
  5324. .reload = reload,
  5325. );