chan_skinny.c 276 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835
  1. /*
  2. * Asterisk -- An open source telephony toolkit.
  3. *
  4. * Copyright (C) 1999 - 2005, Digium, Inc.
  5. *
  6. * chan_skinny was developed by Jeremy McNamara & Florian Overkamp
  7. * chan_skinny was heavily modified/fixed by North Antara
  8. *
  9. * See http://www.asterisk.org for more information about
  10. * the Asterisk project. Please do not directly contact
  11. * any of the maintainers of this project for assistance;
  12. * the project provides a web site, mailing lists and IRC
  13. * channels for your use.
  14. *
  15. * This program is free software, distributed under the terms of
  16. * the GNU General Public License Version 2. See the LICENSE file
  17. * at the top of the source tree.
  18. */
  19. /*! \file
  20. *
  21. * \brief Implementation of the Skinny protocol
  22. *
  23. * \author Jeremy McNamara & Florian Overkamp & North Antara
  24. * \ingroup channel_drivers
  25. */
  26. /*! \li \ref chan_skinny.c uses the configuration file \ref skinny.conf
  27. * \addtogroup configuration_file
  28. */
  29. /*! \page skinny.conf skinny.conf
  30. * \verbinclude skinny.conf.sample
  31. */
  32. /*** MODULEINFO
  33. <support_level>extended</support_level>
  34. ***/
  35. #include "asterisk.h"
  36. ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
  37. #include <sys/socket.h>
  38. #include <netinet/in.h>
  39. #include <netinet/tcp.h>
  40. #include <sys/ioctl.h>
  41. #include <net/if.h>
  42. #include <fcntl.h>
  43. #include <netdb.h>
  44. #include <arpa/inet.h>
  45. #include <sys/signal.h>
  46. #include <signal.h>
  47. #include <ctype.h>
  48. #include "asterisk/lock.h"
  49. #include "asterisk/channel.h"
  50. #include "asterisk/config.h"
  51. #include "asterisk/module.h"
  52. #include "asterisk/pbx.h"
  53. #include "asterisk/sched.h"
  54. #include "asterisk/io.h"
  55. #include "asterisk/rtp_engine.h"
  56. #include "asterisk/netsock2.h"
  57. #include "asterisk/acl.h"
  58. #include "asterisk/callerid.h"
  59. #include "asterisk/cli.h"
  60. #include "asterisk/manager.h"
  61. #include "asterisk/say.h"
  62. #include "asterisk/astdb.h"
  63. #include "asterisk/causes.h"
  64. #include "asterisk/pickup.h"
  65. #include "asterisk/app.h"
  66. #include "asterisk/musiconhold.h"
  67. #include "asterisk/utils.h"
  68. #include "asterisk/dsp.h"
  69. #include "asterisk/stringfields.h"
  70. #include "asterisk/abstract_jb.h"
  71. #include "asterisk/threadstorage.h"
  72. #include "asterisk/devicestate.h"
  73. #include "asterisk/indications.h"
  74. #include "asterisk/linkedlists.h"
  75. #include "asterisk/stasis_endpoints.h"
  76. #include "asterisk/bridge.h"
  77. #include "asterisk/parking.h"
  78. #include "asterisk/stasis_channels.h"
  79. #include "asterisk/format_cache.h"
  80. /*** DOCUMENTATION
  81. <manager name="SKINNYdevices" language="en_US">
  82. <synopsis>
  83. List SKINNY devices (text format).
  84. </synopsis>
  85. <syntax>
  86. <xi:include xpointer="xpointer(/docs/manager[@name='Login']/syntax/parameter[@name='ActionID'])" />
  87. </syntax>
  88. <description>
  89. <para>Lists Skinny devices in text format with details on current status.
  90. Devicelist will follow as separate events, followed by a final event called
  91. DevicelistComplete.</para>
  92. </description>
  93. </manager>
  94. <manager name="SKINNYshowdevice" language="en_US">
  95. <synopsis>
  96. Show SKINNY device (text format).
  97. </synopsis>
  98. <syntax>
  99. <xi:include xpointer="xpointer(/docs/manager[@name='Login']/syntax/parameter[@name='ActionID'])" />
  100. <parameter name="Device" required="true">
  101. <para>The device name you want to check.</para>
  102. </parameter>
  103. </syntax>
  104. <description>
  105. <para>Show one SKINNY device with details on current status.</para>
  106. </description>
  107. </manager>
  108. <manager name="SKINNYlines" language="en_US">
  109. <synopsis>
  110. List SKINNY lines (text format).
  111. </synopsis>
  112. <syntax>
  113. <xi:include xpointer="xpointer(/docs/manager[@name='Login']/syntax/parameter[@name='ActionID'])" />
  114. </syntax>
  115. <description>
  116. <para>Lists Skinny lines in text format with details on current status.
  117. Linelist will follow as separate events, followed by a final event called
  118. LinelistComplete.</para>
  119. </description>
  120. </manager>
  121. <manager name="SKINNYshowline" language="en_US">
  122. <synopsis>
  123. Show SKINNY line (text format).
  124. </synopsis>
  125. <syntax>
  126. <xi:include xpointer="xpointer(/docs/manager[@name='Login']/syntax/parameter[@name='ActionID'])" />
  127. <parameter name="Line" required="true">
  128. <para>The line name you want to check.</para>
  129. </parameter>
  130. </syntax>
  131. <description>
  132. <para>Show one SKINNY line with details on current status.</para>
  133. </description>
  134. </manager>
  135. ***/
  136. /* Skinny debugging only available if asterisk configured with --enable-dev-mode */
  137. #ifdef AST_DEVMODE
  138. static int skinnydebug = 0;
  139. char dbgcli_buf[256];
  140. #define DEBUG_GENERAL (1 << 1)
  141. #define DEBUG_SUB (1 << 2)
  142. #define DEBUG_PACKET (1 << 3)
  143. #define DEBUG_AUDIO (1 << 4)
  144. #define DEBUG_LOCK (1 << 5)
  145. #define DEBUG_TEMPLATE (1 << 6)
  146. #define DEBUG_THREAD (1 << 7)
  147. #define DEBUG_HINT (1 << 8)
  148. #define DEBUG_KEEPALIVE (1 << 9)
  149. #define SKINNY_DEBUG(type, verb_level, text, ...) \
  150. do{ \
  151. if (skinnydebug & (type)) { \
  152. ast_verb(verb_level, "[%d] " text, ast_get_tid(), ##__VA_ARGS__); \
  153. } \
  154. }while(0)
  155. #else
  156. #define SKINNY_DEBUG(type, verb_level, text, ...)
  157. #endif
  158. /*************************************
  159. * Skinny/Asterisk Protocol Settings *
  160. *************************************/
  161. static const char tdesc[] = "Skinny Client Control Protocol (Skinny)";
  162. static const char config[] = "skinny.conf";
  163. static struct ast_format_cap *default_cap;
  164. enum skinny_codecs {
  165. SKINNY_CODEC_ALAW = 2,
  166. SKINNY_CODEC_ULAW = 4,
  167. SKINNY_CODEC_G722 = 6,
  168. SKINNY_CODEC_G723_1 = 9,
  169. SKINNY_CODEC_G729A = 12,
  170. SKINNY_CODEC_G726_32 = 82, /* XXX Which packing order does this translate to? */
  171. SKINNY_CODEC_H261 = 100,
  172. SKINNY_CODEC_H263 = 101
  173. };
  174. #define DEFAULT_SKINNY_PORT 2000
  175. #define DEFAULT_SKINNY_BACKLOG 2
  176. #define SKINNY_MAX_PACKET 2000
  177. #define DEFAULT_AUTH_TIMEOUT 30
  178. #define DEFAULT_AUTH_LIMIT 50
  179. static struct {
  180. unsigned int tos;
  181. unsigned int tos_audio;
  182. unsigned int tos_video;
  183. unsigned int cos;
  184. unsigned int cos_audio;
  185. unsigned int cos_video;
  186. } qos = { 0, 0, 0, 0, 0, 0 };
  187. static int keep_alive = 120;
  188. static int auth_timeout = DEFAULT_AUTH_TIMEOUT;
  189. static int auth_limit = DEFAULT_AUTH_LIMIT;
  190. static int unauth_sessions = 0;
  191. static char immed_dialchar;
  192. static char vmexten[AST_MAX_EXTENSION]; /* Voicemail pilot number */
  193. static char used_context[AST_MAX_EXTENSION]; /* placeholder to check if context are already used in regcontext */
  194. static char regcontext[AST_MAX_CONTEXT]; /* Context for auto-extension */
  195. static char date_format[6] = "D-M-Y";
  196. static char version_id[16] = "P002F202";
  197. #if __BYTE_ORDER == __LITTLE_ENDIAN
  198. #define letohl(x) (x)
  199. #define letohs(x) (x)
  200. #define htolel(x) (x)
  201. #define htoles(x) (x)
  202. #else
  203. #if defined(HAVE_BYTESWAP_H)
  204. #include <byteswap.h>
  205. #define letohl(x) bswap_32(x)
  206. #define letohs(x) bswap_16(x)
  207. #define htolel(x) bswap_32(x)
  208. #define htoles(x) bswap_16(x)
  209. #elif defined(HAVE_SYS_ENDIAN_SWAP16)
  210. #include <sys/endian.h>
  211. #define letohl(x) __swap32(x)
  212. #define letohs(x) __swap16(x)
  213. #define htolel(x) __swap32(x)
  214. #define htoles(x) __swap16(x)
  215. #elif defined(HAVE_SYS_ENDIAN_BSWAP16)
  216. #include <sys/endian.h>
  217. #define letohl(x) bswap32(x)
  218. #define letohs(x) bswap16(x)
  219. #define htolel(x) bswap32(x)
  220. #define htoles(x) bswap16(x)
  221. #else
  222. #define __bswap_16(x) \
  223. ((((x) & 0xff00) >> 8) | \
  224. (((x) & 0x00ff) << 8))
  225. #define __bswap_32(x) \
  226. ((((x) & 0xff000000) >> 24) | \
  227. (((x) & 0x00ff0000) >> 8) | \
  228. (((x) & 0x0000ff00) << 8) | \
  229. (((x) & 0x000000ff) << 24))
  230. #define letohl(x) __bswap_32(x)
  231. #define letohs(x) __bswap_16(x)
  232. #define htolel(x) __bswap_32(x)
  233. #define htoles(x) __bswap_16(x)
  234. #endif
  235. #endif
  236. /*! Global jitterbuffer configuration - by default, jb is disabled
  237. * \note Values shown here match the defaults shown in skinny.conf.sample */
  238. static struct ast_jb_conf default_jbconf =
  239. {
  240. .flags = 0,
  241. .max_size = 200,
  242. .resync_threshold = 1000,
  243. .impl = "fixed",
  244. .target_extra = 40,
  245. };
  246. static struct ast_jb_conf global_jbconf;
  247. #ifdef AST_DEVMODE
  248. AST_THREADSTORAGE(message2str_threadbuf);
  249. #define MESSAGE2STR_BUFSIZE 35
  250. #endif
  251. AST_THREADSTORAGE(device2str_threadbuf);
  252. #define DEVICE2STR_BUFSIZE 15
  253. AST_THREADSTORAGE(control2str_threadbuf);
  254. #define CONTROL2STR_BUFSIZE 100
  255. AST_THREADSTORAGE(substate2str_threadbuf);
  256. #define SUBSTATE2STR_BUFSIZE 15
  257. AST_THREADSTORAGE(callstate2str_threadbuf);
  258. #define CALLSTATE2STR_BUFSIZE 15
  259. /*********************
  260. * Protocol Messages *
  261. *********************/
  262. /* message types */
  263. #define KEEP_ALIVE_MESSAGE 0x0000
  264. /* no additional struct */
  265. #define REGISTER_MESSAGE 0x0001
  266. struct register_message {
  267. char name[16];
  268. uint32_t userId;
  269. uint32_t instance;
  270. uint32_t ip;
  271. uint32_t type;
  272. uint32_t maxStreams;
  273. uint32_t space;
  274. uint8_t protocolVersion;
  275. /*! \brief space2 is used for newer version of skinny */
  276. char space2[3];
  277. };
  278. #define IP_PORT_MESSAGE 0x0002
  279. #define KEYPAD_BUTTON_MESSAGE 0x0003
  280. struct keypad_button_message {
  281. uint32_t button;
  282. uint32_t lineInstance;
  283. uint32_t callReference;
  284. };
  285. #define ENBLOC_CALL_MESSAGE 0x0004
  286. struct enbloc_call_message {
  287. char calledParty[24];
  288. };
  289. #define STIMULUS_MESSAGE 0x0005
  290. struct stimulus_message {
  291. uint32_t stimulus;
  292. uint32_t stimulusInstance;
  293. uint32_t callreference;
  294. };
  295. #define OFFHOOK_MESSAGE 0x0006
  296. struct offhook_message {
  297. uint32_t instance;
  298. uint32_t reference;
  299. };
  300. #define ONHOOK_MESSAGE 0x0007
  301. struct onhook_message {
  302. uint32_t instance;
  303. uint32_t reference;
  304. };
  305. #define CAPABILITIES_RES_MESSAGE 0x0010
  306. struct station_capabilities {
  307. uint32_t codec; /* skinny codec, not ast codec */
  308. uint32_t frames;
  309. union {
  310. char res[8];
  311. uint32_t rate;
  312. } payloads;
  313. };
  314. #define SKINNY_MAX_CAPABILITIES 18
  315. struct capabilities_res_message {
  316. uint32_t count;
  317. struct station_capabilities caps[SKINNY_MAX_CAPABILITIES];
  318. };
  319. #define SPEED_DIAL_STAT_REQ_MESSAGE 0x000A
  320. struct speed_dial_stat_req_message {
  321. uint32_t speedDialNumber;
  322. };
  323. #define LINE_STATE_REQ_MESSAGE 0x000B
  324. struct line_state_req_message {
  325. uint32_t lineNumber;
  326. };
  327. #define TIME_DATE_REQ_MESSAGE 0x000D
  328. #define BUTTON_TEMPLATE_REQ_MESSAGE 0x000E
  329. #define VERSION_REQ_MESSAGE 0x000F
  330. #define SERVER_REQUEST_MESSAGE 0x0012
  331. #define ALARM_MESSAGE 0x0020
  332. struct alarm_message {
  333. uint32_t alarmSeverity;
  334. char displayMessage[80];
  335. uint32_t alarmParam1;
  336. uint32_t alarmParam2;
  337. };
  338. #define OPEN_RECEIVE_CHANNEL_ACK_MESSAGE 0x0022
  339. struct open_receive_channel_ack_message_ip4 {
  340. uint32_t status;
  341. uint32_t ipAddr;
  342. uint32_t port;
  343. uint32_t callReference;
  344. };
  345. struct open_receive_channel_ack_message_ip6 {
  346. uint32_t status;
  347. uint32_t space;
  348. char ipAddr[16];
  349. uint32_t port;
  350. uint32_t callReference;
  351. };
  352. #define SOFT_KEY_SET_REQ_MESSAGE 0x0025
  353. #define SOFT_KEY_EVENT_MESSAGE 0x0026
  354. struct soft_key_event_message {
  355. uint32_t softKeyEvent;
  356. uint32_t instance;
  357. uint32_t callreference;
  358. };
  359. #define UNREGISTER_MESSAGE 0x0027
  360. #define SOFT_KEY_TEMPLATE_REQ_MESSAGE 0x0028
  361. #define HEADSET_STATUS_MESSAGE 0x002B
  362. #define REGISTER_AVAILABLE_LINES_MESSAGE 0x002D
  363. #define SERVICEURL_STATREQ_MESSAGE 0x0033
  364. struct serviceurl_statreq_message {
  365. uint32_t instance;
  366. };
  367. #define REGISTER_ACK_MESSAGE 0x0081
  368. struct register_ack_message {
  369. uint32_t keepAlive;
  370. char dateTemplate[6];
  371. char res[2];
  372. uint32_t secondaryKeepAlive;
  373. char res2[4];
  374. };
  375. #define START_TONE_MESSAGE 0x0082
  376. struct start_tone_message {
  377. uint32_t tone;
  378. uint32_t space;
  379. uint32_t instance;
  380. uint32_t reference;
  381. };
  382. #define STOP_TONE_MESSAGE 0x0083
  383. struct stop_tone_message {
  384. uint32_t instance;
  385. uint32_t reference;
  386. uint32_t space;
  387. };
  388. #define SET_RINGER_MESSAGE 0x0085
  389. struct set_ringer_message {
  390. uint32_t ringerMode;
  391. uint32_t unknown1; /* See notes in transmit_ringer_mode */
  392. uint32_t unknown2;
  393. uint32_t space[2];
  394. };
  395. #define SET_LAMP_MESSAGE 0x0086
  396. struct set_lamp_message {
  397. uint32_t stimulus;
  398. uint32_t stimulusInstance;
  399. uint32_t deviceStimulus;
  400. };
  401. #define SET_SPEAKER_MESSAGE 0x0088
  402. struct set_speaker_message {
  403. uint32_t mode;
  404. };
  405. /* XXX When do we need to use this? */
  406. #define SET_MICROPHONE_MESSAGE 0x0089
  407. struct set_microphone_message {
  408. uint32_t mode;
  409. };
  410. #define START_MEDIA_TRANSMISSION_MESSAGE 0x008A
  411. struct media_qualifier {
  412. uint32_t precedence;
  413. uint32_t vad;
  414. uint32_t packets;
  415. uint32_t bitRate;
  416. };
  417. struct start_media_transmission_message_ip4 {
  418. uint32_t conferenceId;
  419. uint32_t passThruPartyId;
  420. uint32_t remoteIp;
  421. uint32_t remotePort;
  422. uint32_t packetSize;
  423. uint32_t payloadType;
  424. struct media_qualifier qualifier;
  425. uint32_t space[19];
  426. };
  427. struct start_media_transmission_message_ip6 {
  428. uint32_t conferenceId;
  429. uint32_t passThruPartyId;
  430. uint32_t space;
  431. char remoteIp[16];
  432. uint32_t remotePort;
  433. uint32_t packetSize;
  434. uint32_t payloadType;
  435. struct media_qualifier qualifier;
  436. /*! \brief space2 is used for newer version of skinny */
  437. uint32_t space2[19];
  438. };
  439. #define STOP_MEDIA_TRANSMISSION_MESSAGE 0x008B
  440. struct stop_media_transmission_message {
  441. uint32_t conferenceId;
  442. uint32_t passThruPartyId;
  443. uint32_t space[3];
  444. };
  445. #define CALL_INFO_MESSAGE 0x008F
  446. struct call_info_message {
  447. char callingPartyName[40];
  448. char callingParty[24];
  449. char calledPartyName[40];
  450. char calledParty[24];
  451. uint32_t instance;
  452. uint32_t reference;
  453. uint32_t type;
  454. char originalCalledPartyName[40];
  455. char originalCalledParty[24];
  456. char lastRedirectingPartyName[40];
  457. char lastRedirectingParty[24];
  458. uint32_t originalCalledPartyRedirectReason;
  459. uint32_t lastRedirectingReason;
  460. char callingPartyVoiceMailbox[24];
  461. char calledPartyVoiceMailbox[24];
  462. char originalCalledPartyVoiceMailbox[24];
  463. char lastRedirectingVoiceMailbox[24];
  464. uint32_t space[3];
  465. };
  466. #define FORWARD_STAT_MESSAGE 0x0090
  467. struct forward_stat_message {
  468. uint32_t activeforward;
  469. uint32_t lineNumber;
  470. uint32_t fwdall;
  471. char fwdallnum[24];
  472. uint32_t fwdbusy;
  473. char fwdbusynum[24];
  474. uint32_t fwdnoanswer;
  475. char fwdnoanswernum[24];
  476. };
  477. #define SPEED_DIAL_STAT_RES_MESSAGE 0x0091
  478. struct speed_dial_stat_res_message {
  479. uint32_t speedDialNumber;
  480. char speedDialDirNumber[24];
  481. char speedDialDisplayName[40];
  482. };
  483. #define LINE_STAT_RES_MESSAGE 0x0092
  484. struct line_stat_res_message {
  485. uint32_t lineNumber;
  486. char lineDirNumber[24];
  487. char lineDisplayName[24];
  488. uint32_t space[15];
  489. };
  490. #define DEFINETIMEDATE_MESSAGE 0x0094
  491. struct definetimedate_message {
  492. uint32_t year; /* since 1900 */
  493. uint32_t month;
  494. uint32_t dayofweek; /* monday = 1 */
  495. uint32_t day;
  496. uint32_t hour;
  497. uint32_t minute;
  498. uint32_t seconds;
  499. uint32_t milliseconds;
  500. uint32_t timestamp;
  501. };
  502. #define BUTTON_TEMPLATE_RES_MESSAGE 0x0097
  503. struct button_definition {
  504. uint8_t instanceNumber;
  505. uint8_t buttonDefinition;
  506. };
  507. struct button_definition_template {
  508. uint8_t buttonDefinition;
  509. /* for now, anything between 0xB0 and 0xCF is custom */
  510. /*int custom;*/
  511. };
  512. #define STIMULUS_REDIAL 0x01
  513. #define STIMULUS_SPEEDDIAL 0x02
  514. #define STIMULUS_HOLD 0x03
  515. #define STIMULUS_TRANSFER 0x04
  516. #define STIMULUS_FORWARDALL 0x05
  517. #define STIMULUS_FORWARDBUSY 0x06
  518. #define STIMULUS_FORWARDNOANSWER 0x07
  519. #define STIMULUS_DISPLAY 0x08
  520. #define STIMULUS_LINE 0x09
  521. #define STIMULUS_VOICEMAIL 0x0F
  522. #define STIMULUS_AUTOANSWER 0x11
  523. #define STIMULUS_SERVICEURL 0x14
  524. #define STIMULUS_DND 0x3F
  525. #define STIMULUS_CONFERENCE 0x7D
  526. #define STIMULUS_CALLPARK 0x7E
  527. #define STIMULUS_CALLPICKUP 0x7F
  528. #define STIMULUS_NONE 0xFF
  529. /* Button types */
  530. #define BT_REDIAL STIMULUS_REDIAL
  531. #define BT_SPEEDDIAL STIMULUS_SPEEDDIAL
  532. #define BT_HOLD STIMULUS_HOLD
  533. #define BT_TRANSFER STIMULUS_TRANSFER
  534. #define BT_FORWARDALL STIMULUS_FORWARDALL
  535. #define BT_FORWARDBUSY STIMULUS_FORWARDBUSY
  536. #define BT_FORWARDNOANSWER STIMULUS_FORWARDNOANSWER
  537. #define BT_DISPLAY STIMULUS_DISPLAY
  538. #define BT_LINE STIMULUS_LINE
  539. #define BT_VOICEMAIL STIMULUS_VOICEMAIL
  540. #define BT_AUTOANSWER STIMULUS_AUTOANSWER
  541. #define BT_SERVICEURL STIMULUS_SERVICEURL
  542. #define BT_DND STIMULUS_DND
  543. #define BT_CONFERENCE STIMULUS_CONFERENCE
  544. #define BT_CALLPARK STIMULUS_CALLPARK
  545. #define BT_CALLPICKUP STIMULUS_CALLPICKUP
  546. #define BT_NONE 0x00
  547. /* Custom button types - add our own between 0xB0 and 0xCF.
  548. This may need to be revised in the future,
  549. if stimuluses are ever added in this range. */
  550. #define BT_CUST_LINESPEEDDIAL 0xB0 /* line or speeddial with/without hint */
  551. #define BT_CUST_LINE 0xB1 /* line or speeddial with hint only */
  552. struct button_template_res_message {
  553. uint32_t buttonOffset;
  554. uint32_t buttonCount;
  555. uint32_t totalButtonCount;
  556. struct button_definition definition[42];
  557. };
  558. #define VERSION_RES_MESSAGE 0x0098
  559. struct version_res_message {
  560. char version[16];
  561. };
  562. #define DISPLAYTEXT_MESSAGE 0x0099
  563. struct displaytext_message {
  564. char text[40];
  565. };
  566. #define CLEAR_NOTIFY_MESSAGE 0x0115
  567. #define CLEAR_DISPLAY_MESSAGE 0x009A
  568. struct clear_display_message {
  569. uint32_t space;
  570. };
  571. #define CAPABILITIES_REQ_MESSAGE 0x009B
  572. #define REGISTER_REJ_MESSAGE 0x009D
  573. struct register_rej_message {
  574. char errMsg[33];
  575. };
  576. #define SERVER_RES_MESSAGE 0x009E
  577. struct server_identifier {
  578. char serverName[48];
  579. };
  580. struct server_res_message {
  581. struct server_identifier server[5];
  582. uint32_t serverListenPort[5];
  583. uint32_t serverIpAddr[5];
  584. };
  585. #define RESET_MESSAGE 0x009F
  586. struct reset_message {
  587. uint32_t resetType;
  588. };
  589. #define KEEP_ALIVE_ACK_MESSAGE 0x0100
  590. #define OPEN_RECEIVE_CHANNEL_MESSAGE 0x0105
  591. struct open_receive_channel_message {
  592. uint32_t conferenceId;
  593. uint32_t partyId;
  594. uint32_t packets;
  595. uint32_t capability;
  596. uint32_t echo;
  597. uint32_t bitrate;
  598. uint32_t space[36];
  599. };
  600. #define CLOSE_RECEIVE_CHANNEL_MESSAGE 0x0106
  601. struct close_receive_channel_message {
  602. uint32_t conferenceId;
  603. uint32_t partyId;
  604. uint32_t space[2];
  605. };
  606. #define SOFT_KEY_TEMPLATE_RES_MESSAGE 0x0108
  607. struct soft_key_template_definition {
  608. char softKeyLabel[16];
  609. uint32_t softKeyEvent;
  610. };
  611. #define BKSP_REQ_MESSAGE 0x0119
  612. struct bksp_req_message {
  613. uint32_t instance;
  614. uint32_t callreference;
  615. };
  616. #define KEYDEF_ONHOOK 0
  617. #define KEYDEF_CONNECTED 1
  618. #define KEYDEF_ONHOLD 2
  619. #define KEYDEF_RINGIN 3
  620. #define KEYDEF_OFFHOOK 4
  621. #define KEYDEF_CONNWITHTRANS 5
  622. #define KEYDEF_DADFD 6 /* Digits After Dialing First Digit */
  623. #define KEYDEF_CONNWITHCONF 7
  624. #define KEYDEF_RINGOUT 8
  625. #define KEYDEF_OFFHOOKWITHFEAT 9
  626. #define KEYDEF_UNKNOWN 10
  627. #define KEYDEF_SLAHOLD 11
  628. #define KEYDEF_SLACONNECTEDNOTACTIVE 12
  629. #define KEYDEF_RINGOUTWITHTRANS 13
  630. #define SOFTKEY_NONE 0x00
  631. #define SOFTKEY_REDIAL 0x01
  632. #define SOFTKEY_NEWCALL 0x02
  633. #define SOFTKEY_HOLD 0x03
  634. #define SOFTKEY_TRNSFER 0x04
  635. #define SOFTKEY_CFWDALL 0x05
  636. #define SOFTKEY_CFWDBUSY 0x06
  637. #define SOFTKEY_CFWDNOANSWER 0x07
  638. #define SOFTKEY_BKSPC 0x08
  639. #define SOFTKEY_ENDCALL 0x09
  640. #define SOFTKEY_RESUME 0x0A
  641. #define SOFTKEY_ANSWER 0x0B
  642. #define SOFTKEY_INFO 0x0C
  643. #define SOFTKEY_CONFRN 0x0D
  644. #define SOFTKEY_PARK 0x0E
  645. #define SOFTKEY_JOIN 0x0F
  646. #define SOFTKEY_MEETME 0x10
  647. #define SOFTKEY_PICKUP 0x11
  648. #define SOFTKEY_GPICKUP 0x12
  649. #define SOFTKEY_DND 0x13
  650. #define SOFTKEY_IDIVERT 0x14
  651. #define SOFTKEY_FORCEDIAL 0x15
  652. #define KEYMASK_ALL 0xFFFFFFFF
  653. #define KEYMASK_NONE (1 << 0)
  654. #define KEYMASK_REDIAL (1 << 1)
  655. #define KEYMASK_NEWCALL (1 << 2)
  656. #define KEYMASK_HOLD (1 << 3)
  657. #define KEYMASK_TRNSFER (1 << 4)
  658. #define KEYMASK_CFWDALL (1 << 5)
  659. #define KEYMASK_CFWDBUSY (1 << 6)
  660. #define KEYMASK_CFWDNOANSWER (1 << 7)
  661. #define KEYMASK_BKSPC (1 << 8)
  662. #define KEYMASK_ENDCALL (1 << 9)
  663. #define KEYMASK_RESUME (1 << 10)
  664. #define KEYMASK_ANSWER (1 << 11)
  665. #define KEYMASK_INFO (1 << 12)
  666. #define KEYMASK_CONFRN (1 << 13)
  667. #define KEYMASK_PARK (1 << 14)
  668. #define KEYMASK_JOIN (1 << 15)
  669. #define KEYMASK_MEETME (1 << 16)
  670. #define KEYMASK_PICKUP (1 << 17)
  671. #define KEYMASK_GPICKUP (1 << 18)
  672. #define KEYMASK_DND (1 << 29)
  673. #define KEYMASK_IDIVERT (1 << 20)
  674. #define KEYMASK_FORCEDIAL (1 << 21)
  675. /* Localized message "codes" (in octal)
  676. Below is en_US (taken from a 7970) */
  677. /* "\200\000" ??? */
  678. #define OCTAL_REDIAL "\200\001" /* Redial */
  679. #define OCTAL_NEWCALL "\200\002" /* New Call */
  680. #define OCTAL_HOLD "\200\003" /* Hold */
  681. #define OCTAL_TRANSFER "\200\004" /* Transfer */
  682. #define OCTAL_CFWDALL "\200\005" /* CFwdALL */
  683. #define OCTAL_CFWDBUSY "\200\006" /* CFwdBusy */
  684. #define OCTAL_CFWDNOAN "\200\007" /* CFwdNoAnswer */
  685. #define OCTAL_BKSPC "\200\010" /* << */
  686. #define OCTAL_ENDCALL "\200\011" /* EndCall */
  687. #define OCTAL_RESUME "\200\012" /* Resume */
  688. #define OCTAL_ANSWER "\200\013" /* Answer */
  689. #define OCTAL_INFO "\200\014" /* Info */
  690. #define OCTAL_CONFRN "\200\015" /* Confrn */
  691. #define OCTAL_PARK "\200\016" /* Park */
  692. #define OCTAL_JOIN "\200\017" /* Join */
  693. #define OCTAL_MEETME "\200\020" /* MeetMe */
  694. #define OCTAL_PICKUP "\200\021" /* PickUp */
  695. #define OCTAL_GPICKUP "\200\022" /* GPickUp */
  696. #define OCTAL_CUROPTS "\200\023" /* Your current options */
  697. #define OCTAL_OFFHOOK "\200\024" /* Off Hook */
  698. #define OCTAL_ONHOOK "\200\025" /* On Hook */
  699. #define OCTAL_RINGOUT "\200\026" /* Ring out */
  700. #define OCTAL_FROM "\200\027" /* From */
  701. #define OCTAL_CONNECTED "\200\030" /* Connected */
  702. #define OCTAL_BUSY "\200\031" /* Busy */
  703. #define OCTAL_LINEINUSE "\200\032" /* Line In Use */
  704. #define OCTAL_CALLWAITING "\200\033" /* Call Waiting */
  705. #define OCTAL_CALLXFER "\200\034" /* Call Transfer */
  706. #define OCTAL_CALLPARK "\200\035" /* Call Park */
  707. #define OCTAL_CALLPROCEED "\200\036" /* Call Proceed */
  708. #define OCTAL_INUSEREMOTE "\200\037" /* In Use Remote */
  709. #define OCTAL_ENTRNUM "\200\040" /* Enter number */
  710. #define OCTAL_PARKAT "\200\041" /* Call park At */
  711. #define OCTAL_PRIMONLY "\200\042" /* Primary Only */
  712. #define OCTAL_TMPFAIL "\200\043" /* Temp Fail */
  713. #define OCTAL_HAVEVMAIL "\200\044" /* You Have VoiceMail */
  714. #define OCTAL_FWDEDTO "\200\045" /* Forwarded to */
  715. #define OCTAL_CANTCOMPCNF "\200\046" /* Can Not Complete Conference */
  716. #define OCTAL_NOCONFBRDG "\200\047" /* No Conference Bridge */
  717. #define OCTAL_NOPRIMARYCTL "\200\050" /* Can Not Hold Primary Control */
  718. #define OCTAL_INVALCONFPART "\200\051" /* Invalid Conference Participant */
  719. #define OCTAL_INCONFALREADY "\200\052" /* In Conference Already */
  720. #define OCTAL_NOPARTINFO "\200\053" /* No Participant Info */
  721. #define OCTAL_MAXPARTEXCEED "\200\054" /* Exceed Maximum Parties */
  722. #define OCTAL_KEYNOTACTIVE "\200\055" /* Key Is Not Active */
  723. #define OCTAL_ERRNOLIC "\200\056" /* Error No License */
  724. #define OCTAL_ERRDBCFG "\200\057" /* Error DBConfig */
  725. #define OCTAL_ERRDB "\200\060" /* Error Database */
  726. #define OCTAL_ERRPASSLMT "\200\061" /* Error Pass Limit */
  727. #define OCTAL_ERRUNK "\200\062" /* Error Unknown */
  728. #define OCTAL_ERRMISMATCH "\200\063" /* Error Mismatch */
  729. #define OCTAL_CONFERENCE "\200\064" /* Conference */
  730. #define OCTAL_PARKNO "\200\065" /* Park Number */
  731. #define OCTAL_PRIVATE "\200\066" /* Private */
  732. #define OCTAL_INSUFBANDW "\200\067" /* Not Enough Bandwidth */
  733. #define OCTAL_UNKNUM "\200\070" /* Unknown Number */
  734. #define OCTAL_RMLSTC "\200\071" /* RmLstC */
  735. #define OCTAL_VOICEMAIL "\200\072" /* Voicemail */
  736. #define OCTAL_IMMDIV "\200\073" /* ImmDiv */
  737. #define OCTAL_INTRCPT "\200\074" /* Intrcpt */
  738. #define OCTAL_SETWTCH "\200\075" /* SetWtch */
  739. #define OCTAL_TRNSFVM "\200\076" /* TrnsfVM */
  740. #define OCTAL_DND "\200\077" /* DND */
  741. #define OCTAL_DIVALL "\200\100" /* DivAll */
  742. #define OCTAL_CALLBACK "\200\101" /* CallBack */
  743. #define OCTAL_NETCNGREROUT "\200\102" /* Network congestion,rerouting */
  744. #define OCTAL_BARGE "\200\103" /* Barge */
  745. #define OCTAL_BARGEFAIL "\200\104" /* Failed to setup Barge */
  746. #define OCTAL_BARGEEXIST "\200\105" /* Another Barge exists */
  747. #define OCTAL_INCOMPATDEV "\200\106" /* Incompatible device type */
  748. #define OCTAL_PARKNONUM "\200\107" /* No Park Number Available */
  749. #define OCTAL_PARKREVERSION "\200\110" /* CallPark Reversion */
  750. #define OCTAL_SRVNOTACTIVE "\200\111" /* Service is not Active */
  751. #define OCTAL_HITRAFFIC "\200\112" /* High Traffic Try Again Later */
  752. #define OCTAL_QRT "\200\113" /* QRT */
  753. #define OCTAL_MCID "\200\114" /* MCID */
  754. #define OCTAL_DIRTRFR "\200\115" /* DirTrfr */
  755. #define OCTAL_SELECT "\200\116" /* Select */
  756. #define OCTAL_CONFLIST "\200\117" /* ConfList */
  757. #define OCTAL_IDIVERT "\200\120" /* iDivert */
  758. #define OCTAL_CBARGE "\200\121" /* cBarge */
  759. #define OCTAL_CANTCOMPLXFER "\200\122" /* Can Not Complete Transfer */
  760. #define OCTAL_CANTJOINCALLS "\200\123" /* Can Not Join Calls */
  761. #define OCTAL_MCIDSUCCESS "\200\124" /* Mcid Successful */
  762. #define OCTAL_NUMNOTCFG "\200\125" /* Number Not Configured */
  763. #define OCTAL_SECERROR "\200\126" /* Security Error */
  764. #define OCTAL_VIDBANDWNA "\200\127" /* Video Bandwidth Unavailable */
  765. #define OCTAL_VIDMODE "\200\130" /* VidMode */
  766. #define OCTAL_CALLDURTIMEOUT "\200\131" /* Max Call Duration Timeout */
  767. #define OCTAL_HOLDDURTIMEOUT "\200\132" /* Max Hold Duration Timeout */
  768. #define OCTAL_OPICKUP "\200\133" /* OPickUp */
  769. /* "\200\134" ??? */
  770. /* "\200\135" ??? */
  771. /* "\200\136" ??? */
  772. /* "\200\137" ??? */
  773. /* "\200\140" ??? */
  774. #define OCTAL_EXTXFERRESTRICT "\200\141" /* External Transfer Restricted */
  775. /* "\200\142" ??? */
  776. /* "\200\143" ??? */
  777. /* "\200\144" ??? */
  778. #define OCTAL_MACADD "\200\145" /* Mac Address */
  779. #define OCTAL_HOST "\200\146" /* Host Name */
  780. #define OCTAL_DOMAIN "\200\147" /* Domain Name */
  781. #define OCTAL_IPADD "\200\150" /* IP Address */
  782. #define OCTAL_SUBMASK "\200\151" /* Subnet Mask */
  783. #define OCTAL_TFTP1 "\200\152" /* TFTP Server 1 */
  784. #define OCTAL_ROUTER1 "\200\153" /* Default Router 1 */
  785. #define OCTAL_ROUTER2 "\200\154" /* Default Router 2 */
  786. #define OCTAL_ROUTER3 "\200\155" /* Default Router 3 */
  787. #define OCTAL_ROUTER4 "\200\156" /* Default Router 4 */
  788. #define OCTAL_ROUTER5 "\200\157" /* Default Router 5 */
  789. #define OCTAL_DNS1 "\200\160" /* DNS Server 1 */
  790. #define OCTAL_DNS2 "\200\161" /* DNS Server 2 */
  791. #define OCTAL_DNS3 "\200\162" /* DNS Server 3 */
  792. #define OCTAL_DNS4 "\200\163" /* DNS Server 4 */
  793. #define OCTAL_DNS5 "\200\164" /* DNS Server 5 */
  794. #define OCTAL_VLANOPID "\200\165" /* Operational VLAN Id */
  795. #define OCTAL_VLANADID "\200\166" /* Admin. VLAN Id */
  796. #define OCTAL_CM1 "\200\167" /* CallManager 1 */
  797. #define OCTAL_CM2 "\200\170" /* CallManager 2 */
  798. #define OCTAL_CM3 "\200\171" /* CallManager 3 */
  799. #define OCTAL_CM4 "\200\172" /* CallManager 4 */
  800. #define OCTAL_CM5 "\200\173" /* CallManager 5 */
  801. #define OCTAL_URLINFO "\200\174" /* Information URL */
  802. #define OCTAL_URLDIRS "\200\175" /* Directories URL */
  803. #define OCTAL_URLMSGS "\200\176" /* Messages URL */
  804. #define OCTAL_URLSRVS "\200\177" /* Services URL */
  805. static struct soft_key_template_definition soft_key_template_default[] = {
  806. { OCTAL_REDIAL, SOFTKEY_REDIAL },
  807. { OCTAL_NEWCALL, SOFTKEY_NEWCALL },
  808. { OCTAL_HOLD, SOFTKEY_HOLD },
  809. { OCTAL_TRANSFER, SOFTKEY_TRNSFER },
  810. { OCTAL_CFWDALL, SOFTKEY_CFWDALL },
  811. { OCTAL_CFWDBUSY, SOFTKEY_CFWDBUSY },
  812. { OCTAL_CFWDNOAN, SOFTKEY_CFWDNOANSWER },
  813. { OCTAL_BKSPC, SOFTKEY_BKSPC },
  814. { OCTAL_ENDCALL, SOFTKEY_ENDCALL },
  815. { OCTAL_RESUME, SOFTKEY_RESUME },
  816. { OCTAL_ANSWER, SOFTKEY_ANSWER },
  817. { OCTAL_INFO, SOFTKEY_INFO },
  818. { OCTAL_CONFRN, SOFTKEY_CONFRN },
  819. { OCTAL_PARK, SOFTKEY_PARK },
  820. { OCTAL_JOIN, SOFTKEY_JOIN },
  821. { OCTAL_MEETME, SOFTKEY_MEETME },
  822. { OCTAL_PICKUP, SOFTKEY_PICKUP },
  823. { OCTAL_GPICKUP, SOFTKEY_GPICKUP },
  824. { OCTAL_DND, SOFTKEY_DND },
  825. { OCTAL_IDIVERT, SOFTKEY_IDIVERT },
  826. { "Dial", SOFTKEY_FORCEDIAL},
  827. };
  828. struct soft_key_definitions {
  829. const uint8_t mode;
  830. const uint8_t *defaults;
  831. const int count;
  832. };
  833. static const uint8_t soft_key_default_onhook[] = {
  834. SOFTKEY_REDIAL,
  835. SOFTKEY_NEWCALL,
  836. SOFTKEY_CFWDALL,
  837. SOFTKEY_CFWDBUSY,
  838. SOFTKEY_CFWDNOANSWER,
  839. SOFTKEY_DND,
  840. SOFTKEY_GPICKUP,
  841. /*SOFTKEY_CONFRN,*/
  842. };
  843. static const uint8_t soft_key_default_connected[] = {
  844. SOFTKEY_HOLD,
  845. SOFTKEY_ENDCALL,
  846. SOFTKEY_TRNSFER,
  847. SOFTKEY_PARK,
  848. SOFTKEY_CFWDALL,
  849. SOFTKEY_CFWDBUSY,
  850. SOFTKEY_CFWDNOANSWER,
  851. };
  852. static const uint8_t soft_key_default_onhold[] = {
  853. SOFTKEY_RESUME,
  854. SOFTKEY_NEWCALL,
  855. SOFTKEY_ENDCALL,
  856. SOFTKEY_TRNSFER,
  857. };
  858. static const uint8_t soft_key_default_ringin[] = {
  859. SOFTKEY_ANSWER,
  860. SOFTKEY_ENDCALL,
  861. SOFTKEY_TRNSFER,
  862. };
  863. static const uint8_t soft_key_default_offhook[] = {
  864. SOFTKEY_REDIAL,
  865. SOFTKEY_ENDCALL,
  866. SOFTKEY_TRNSFER,
  867. SOFTKEY_CFWDALL,
  868. SOFTKEY_CFWDBUSY,
  869. SOFTKEY_CFWDNOANSWER,
  870. SOFTKEY_GPICKUP,
  871. };
  872. static const uint8_t soft_key_default_connwithtrans[] = {
  873. SOFTKEY_HOLD,
  874. SOFTKEY_ENDCALL,
  875. SOFTKEY_TRNSFER,
  876. SOFTKEY_PARK,
  877. SOFTKEY_CFWDALL,
  878. SOFTKEY_CFWDBUSY,
  879. SOFTKEY_CFWDNOANSWER,
  880. };
  881. static const uint8_t soft_key_default_dadfd[] = {
  882. SOFTKEY_BKSPC,
  883. SOFTKEY_ENDCALL,
  884. SOFTKEY_TRNSFER,
  885. SOFTKEY_FORCEDIAL,
  886. };
  887. static const uint8_t soft_key_default_connwithconf[] = {
  888. SOFTKEY_NONE,
  889. };
  890. static const uint8_t soft_key_default_ringout[] = {
  891. SOFTKEY_NONE,
  892. SOFTKEY_ENDCALL,
  893. };
  894. static const uint8_t soft_key_default_ringoutwithtransfer[] = {
  895. SOFTKEY_NONE,
  896. SOFTKEY_ENDCALL,
  897. SOFTKEY_TRNSFER,
  898. };
  899. static const uint8_t soft_key_default_offhookwithfeat[] = {
  900. SOFTKEY_REDIAL,
  901. SOFTKEY_ENDCALL,
  902. SOFTKEY_TRNSFER,
  903. };
  904. static const uint8_t soft_key_default_unknown[] = {
  905. SOFTKEY_NONE,
  906. };
  907. static const uint8_t soft_key_default_SLAhold[] = {
  908. SOFTKEY_REDIAL,
  909. SOFTKEY_NEWCALL,
  910. SOFTKEY_RESUME,
  911. };
  912. static const uint8_t soft_key_default_SLAconnectednotactive[] = {
  913. SOFTKEY_REDIAL,
  914. SOFTKEY_NEWCALL,
  915. SOFTKEY_JOIN,
  916. };
  917. static const struct soft_key_definitions soft_key_default_definitions[] = {
  918. {KEYDEF_ONHOOK, soft_key_default_onhook, sizeof(soft_key_default_onhook) / sizeof(uint8_t)},
  919. {KEYDEF_CONNECTED, soft_key_default_connected, sizeof(soft_key_default_connected) / sizeof(uint8_t)},
  920. {KEYDEF_ONHOLD, soft_key_default_onhold, sizeof(soft_key_default_onhold) / sizeof(uint8_t)},
  921. {KEYDEF_RINGIN, soft_key_default_ringin, sizeof(soft_key_default_ringin) / sizeof(uint8_t)},
  922. {KEYDEF_OFFHOOK, soft_key_default_offhook, sizeof(soft_key_default_offhook) / sizeof(uint8_t)},
  923. {KEYDEF_CONNWITHTRANS, soft_key_default_connwithtrans, sizeof(soft_key_default_connwithtrans) / sizeof(uint8_t)},
  924. {KEYDEF_DADFD, soft_key_default_dadfd, sizeof(soft_key_default_dadfd) / sizeof(uint8_t)},
  925. {KEYDEF_CONNWITHCONF, soft_key_default_connwithconf, sizeof(soft_key_default_connwithconf) / sizeof(uint8_t)},
  926. {KEYDEF_RINGOUT, soft_key_default_ringout, sizeof(soft_key_default_ringout) / sizeof(uint8_t)},
  927. {KEYDEF_RINGOUTWITHTRANS, soft_key_default_ringoutwithtransfer, sizeof(soft_key_default_ringoutwithtransfer) / sizeof(uint8_t)},
  928. {KEYDEF_OFFHOOKWITHFEAT, soft_key_default_offhookwithfeat, sizeof(soft_key_default_offhookwithfeat) / sizeof(uint8_t)},
  929. {KEYDEF_UNKNOWN, soft_key_default_unknown, sizeof(soft_key_default_unknown) / sizeof(uint8_t)},
  930. {KEYDEF_SLAHOLD, soft_key_default_SLAhold, sizeof(soft_key_default_SLAhold) / sizeof(uint8_t)},
  931. {KEYDEF_SLACONNECTEDNOTACTIVE, soft_key_default_SLAconnectednotactive, sizeof(soft_key_default_SLAconnectednotactive) / sizeof(uint8_t)}
  932. };
  933. struct soft_key_template_res_message {
  934. uint32_t softKeyOffset;
  935. uint32_t softKeyCount;
  936. uint32_t totalSoftKeyCount;
  937. struct soft_key_template_definition softKeyTemplateDefinition[32];
  938. };
  939. #define SOFT_KEY_SET_RES_MESSAGE 0x0109
  940. struct soft_key_set_definition {
  941. uint8_t softKeyTemplateIndex[16];
  942. uint16_t softKeyInfoIndex[16];
  943. };
  944. struct soft_key_set_res_message {
  945. uint32_t softKeySetOffset;
  946. uint32_t softKeySetCount;
  947. uint32_t totalSoftKeySetCount;
  948. struct soft_key_set_definition softKeySetDefinition[16];
  949. uint32_t res;
  950. };
  951. #define SELECT_SOFT_KEYS_MESSAGE 0x0110
  952. struct select_soft_keys_message {
  953. uint32_t instance;
  954. uint32_t reference;
  955. uint32_t softKeySetIndex;
  956. uint32_t validKeyMask;
  957. };
  958. #define CALL_STATE_MESSAGE 0x0111
  959. struct call_state_message {
  960. uint32_t callState;
  961. uint32_t lineInstance;
  962. uint32_t callReference;
  963. uint32_t space[3];
  964. };
  965. #define DISPLAY_PROMPT_STATUS_MESSAGE 0x0112
  966. struct display_prompt_status_message {
  967. uint32_t messageTimeout;
  968. char promptMessage[32];
  969. uint32_t lineInstance;
  970. uint32_t callReference;
  971. uint32_t space[3];
  972. };
  973. #define CLEAR_PROMPT_MESSAGE 0x0113
  974. struct clear_prompt_message {
  975. uint32_t lineInstance;
  976. uint32_t callReference;
  977. };
  978. #define DISPLAY_NOTIFY_MESSAGE 0x0114
  979. struct display_notify_message {
  980. uint32_t displayTimeout;
  981. char displayMessage[100];
  982. };
  983. #define ACTIVATE_CALL_PLANE_MESSAGE 0x0116
  984. struct activate_call_plane_message {
  985. uint32_t lineInstance;
  986. };
  987. #define DIALED_NUMBER_MESSAGE 0x011D
  988. struct dialed_number_message {
  989. char dialedNumber[24];
  990. uint32_t lineInstance;
  991. uint32_t callReference;
  992. };
  993. #define MAX_SERVICEURL 256
  994. #define SERVICEURL_STAT_MESSAGE 0x012F
  995. struct serviceurl_stat_message {
  996. uint32_t instance;
  997. char url[MAX_SERVICEURL];
  998. char displayName[40];
  999. };
  1000. #define MAXCALLINFOSTR 256
  1001. #define MAXDISPLAYNOTIFYSTR 32
  1002. #define DISPLAY_PRINOTIFY_MESSAGE 0x0120
  1003. struct display_prinotify_message {
  1004. uint32_t timeout;
  1005. uint32_t priority;
  1006. char text[MAXDISPLAYNOTIFYSTR];
  1007. };
  1008. #define CLEAR_PRINOTIFY_MESSAGE 0x0121
  1009. struct clear_prinotify_message {
  1010. uint32_t priority;
  1011. };
  1012. #define CALL_INFO_MESSAGE_VARIABLE 0x014A
  1013. struct call_info_message_variable {
  1014. uint32_t instance;
  1015. uint32_t callreference;
  1016. uint32_t calldirection;
  1017. uint32_t unknown1;
  1018. uint32_t unknown2;
  1019. uint32_t unknown3;
  1020. uint32_t unknown4;
  1021. uint32_t unknown5;
  1022. char calldetails[MAXCALLINFOSTR];
  1023. };
  1024. #define DISPLAY_PRINOTIFY_MESSAGE_VARIABLE 0x0144
  1025. struct display_prinotify_message_variable {
  1026. uint32_t timeout;
  1027. uint32_t priority;
  1028. char text[MAXDISPLAYNOTIFYSTR];
  1029. };
  1030. #define DISPLAY_PROMPT_STATUS_MESSAGE_VARIABLE 0x0145
  1031. struct display_prompt_status_message_variable {
  1032. uint32_t unknown;
  1033. uint32_t lineInstance;
  1034. uint32_t callReference;
  1035. char promptMessage[MAXCALLINFOSTR];
  1036. };
  1037. union skinny_data {
  1038. struct alarm_message alarm;
  1039. struct speed_dial_stat_req_message speeddialreq;
  1040. struct register_message reg;
  1041. struct register_ack_message regack;
  1042. struct register_rej_message regrej;
  1043. struct capabilities_res_message caps;
  1044. struct version_res_message version;
  1045. struct button_template_res_message buttontemplate;
  1046. struct displaytext_message displaytext;
  1047. struct clear_display_message cleardisplay;
  1048. struct display_prompt_status_message displaypromptstatus;
  1049. struct clear_prompt_message clearpromptstatus;
  1050. struct definetimedate_message definetimedate;
  1051. struct start_tone_message starttone;
  1052. struct stop_tone_message stoptone;
  1053. struct speed_dial_stat_res_message speeddial;
  1054. struct line_state_req_message line;
  1055. struct line_stat_res_message linestat;
  1056. struct soft_key_set_res_message softkeysets;
  1057. struct soft_key_template_res_message softkeytemplate;
  1058. struct server_res_message serverres;
  1059. struct reset_message reset;
  1060. struct set_lamp_message setlamp;
  1061. struct set_ringer_message setringer;
  1062. struct call_state_message callstate;
  1063. struct keypad_button_message keypad;
  1064. struct select_soft_keys_message selectsoftkey;
  1065. struct activate_call_plane_message activatecallplane;
  1066. struct stimulus_message stimulus;
  1067. struct offhook_message offhook;
  1068. struct onhook_message onhook;
  1069. struct set_speaker_message setspeaker;
  1070. struct set_microphone_message setmicrophone;
  1071. struct call_info_message callinfo;
  1072. struct start_media_transmission_message_ip4 startmedia_ip4;
  1073. struct start_media_transmission_message_ip6 startmedia_ip6;
  1074. struct stop_media_transmission_message stopmedia;
  1075. struct open_receive_channel_message openreceivechannel;
  1076. struct open_receive_channel_ack_message_ip4 openreceivechannelack_ip4;
  1077. struct open_receive_channel_ack_message_ip6 openreceivechannelack_ip6;
  1078. struct close_receive_channel_message closereceivechannel;
  1079. struct display_notify_message displaynotify;
  1080. struct dialed_number_message dialednumber;
  1081. struct soft_key_event_message softkeyeventmessage;
  1082. struct enbloc_call_message enbloccallmessage;
  1083. struct forward_stat_message forwardstat;
  1084. struct bksp_req_message bkspmessage;
  1085. struct call_info_message_variable callinfomessagevariable;
  1086. struct display_prompt_status_message_variable displaypromptstatusvar;
  1087. struct serviceurl_stat_message serviceurlmessage;
  1088. struct clear_prinotify_message clearprinotify;
  1089. struct display_prinotify_message displayprinotify;
  1090. struct display_prinotify_message_variable displayprinotifyvar;
  1091. };
  1092. /* packet composition */
  1093. struct skinny_req {
  1094. uint32_t len;
  1095. uint32_t res;
  1096. uint32_t e;
  1097. union skinny_data data;
  1098. };
  1099. /* XXX This is the combined size of the variables above. (len, res, e)
  1100. If more are added, this MUST change.
  1101. (sizeof(skinny_req) - sizeof(skinny_data)) DOES NOT WORK on all systems (amd64?). */
  1102. static int skinny_header_size = 12;
  1103. /*****************************
  1104. * Asterisk specific globals *
  1105. *****************************/
  1106. static int skinnyreload = 0;
  1107. /* a hostname, portnumber, socket and such is usefull for VoIP protocols */
  1108. static struct sockaddr_in bindaddr;
  1109. static char ourhost[256];
  1110. static int ourport;
  1111. static struct in_addr __ourip;
  1112. static struct ast_hostent ahp;
  1113. static struct hostent *hp;
  1114. static int skinnysock = -1;
  1115. static pthread_t accept_t;
  1116. static int callnums = 1;
  1117. #define SKINNY_DEVICE_UNKNOWN -1
  1118. #define SKINNY_DEVICE_NONE 0
  1119. #define SKINNY_DEVICE_30SPPLUS 1
  1120. #define SKINNY_DEVICE_12SPPLUS 2
  1121. #define SKINNY_DEVICE_12SP 3
  1122. #define SKINNY_DEVICE_12 4
  1123. #define SKINNY_DEVICE_30VIP 5
  1124. #define SKINNY_DEVICE_7910 6
  1125. #define SKINNY_DEVICE_7960 7
  1126. #define SKINNY_DEVICE_7940 8
  1127. #define SKINNY_DEVICE_7935 9
  1128. #define SKINNY_DEVICE_ATA186 12 /* Cisco ATA-186 */
  1129. #define SKINNY_DEVICE_7941 115
  1130. #define SKINNY_DEVICE_7971 119
  1131. #define SKINNY_DEVICE_7914 124 /* Expansion module */
  1132. #define SKINNY_DEVICE_7985 302
  1133. #define SKINNY_DEVICE_7911 307
  1134. #define SKINNY_DEVICE_7961GE 308
  1135. #define SKINNY_DEVICE_7941GE 309
  1136. #define SKINNY_DEVICE_7931 348
  1137. #define SKINNY_DEVICE_7921 365
  1138. #define SKINNY_DEVICE_7906 369
  1139. #define SKINNY_DEVICE_7962 404 /* Not found */
  1140. #define SKINNY_DEVICE_7937 431
  1141. #define SKINNY_DEVICE_7942 434
  1142. #define SKINNY_DEVICE_7945 435
  1143. #define SKINNY_DEVICE_7965 436
  1144. #define SKINNY_DEVICE_7975 437
  1145. #define SKINNY_DEVICE_7905 20000
  1146. #define SKINNY_DEVICE_7920 30002
  1147. #define SKINNY_DEVICE_7970 30006
  1148. #define SKINNY_DEVICE_7912 30007
  1149. #define SKINNY_DEVICE_7902 30008
  1150. #define SKINNY_DEVICE_CIPC 30016 /* Cisco IP Communicator */
  1151. #define SKINNY_DEVICE_7961 30018
  1152. #define SKINNY_DEVICE_7936 30019
  1153. #define SKINNY_DEVICE_SCCPGATEWAY_AN 30027 /* Analog gateway */
  1154. #define SKINNY_DEVICE_SCCPGATEWAY_BRI 30028 /* BRI gateway */
  1155. #define SKINNY_SPEAKERON 1
  1156. #define SKINNY_SPEAKEROFF 2
  1157. #define SKINNY_MICON 1
  1158. #define SKINNY_MICOFF 2
  1159. #define SKINNY_OFFHOOK 1
  1160. #define SKINNY_ONHOOK 2
  1161. #define SKINNY_RINGOUT 3
  1162. #define SKINNY_RINGIN 4
  1163. #define SKINNY_CONNECTED 5
  1164. #define SKINNY_BUSY 6
  1165. #define SKINNY_CONGESTION 7
  1166. #define SKINNY_HOLD 8
  1167. #define SKINNY_CALLWAIT 9
  1168. #define SKINNY_TRANSFER 10
  1169. #define SKINNY_PARK 11
  1170. #define SKINNY_PROGRESS 12
  1171. #define SKINNY_CALLREMOTEMULTILINE 13
  1172. #define SKINNY_INVALID 14
  1173. #define SKINNY_INCOMING 1
  1174. #define SKINNY_OUTGOING 2
  1175. #define SKINNY_SILENCE 0x00 /* Note sure this is part of the protocol, remove? */
  1176. #define SKINNY_DIALTONE 0x21
  1177. #define SKINNY_BUSYTONE 0x23
  1178. #define SKINNY_ALERT 0x24
  1179. #define SKINNY_REORDER 0x25
  1180. #define SKINNY_CALLWAITTONE 0x2D
  1181. #define SKINNY_ZIPZIP 0x31
  1182. #define SKINNY_ZIP 0x32
  1183. #define SKINNY_BEEPBONK 0x33
  1184. #define SKINNY_BARGIN 0x43
  1185. #define SKINNY_NOTONE 0x7F
  1186. #define SKINNY_LAMP_OFF 1
  1187. #define SKINNY_LAMP_ON 2
  1188. #define SKINNY_LAMP_WINK 3
  1189. #define SKINNY_LAMP_FLASH 4
  1190. #define SKINNY_LAMP_BLINK 5
  1191. #define SKINNY_RING_OFF 1
  1192. #define SKINNY_RING_INSIDE 2
  1193. #define SKINNY_RING_OUTSIDE 3
  1194. #define SKINNY_RING_FEATURE 4
  1195. #define SKINNY_CFWD_ALL (1 << 0)
  1196. #define SKINNY_CFWD_BUSY (1 << 1)
  1197. #define SKINNY_CFWD_NOANSWER (1 << 2)
  1198. /* Skinny rtp stream modes. Do we really need this? */
  1199. #define SKINNY_CX_SENDONLY 0
  1200. #define SKINNY_CX_RECVONLY 1
  1201. #define SKINNY_CX_SENDRECV 2
  1202. #define SKINNY_CX_CONF 3
  1203. #define SKINNY_CX_CONFERENCE 3
  1204. #define SKINNY_CX_MUTE 4
  1205. #define SKINNY_CX_INACTIVE 4
  1206. #if 0
  1207. static const char * const skinny_cxmodes[] = {
  1208. "sendonly",
  1209. "recvonly",
  1210. "sendrecv",
  1211. "confrnce",
  1212. "inactive"
  1213. };
  1214. #endif
  1215. /* driver scheduler */
  1216. static struct ast_sched_context *sched = NULL;
  1217. /* Protect the network socket */
  1218. AST_MUTEX_DEFINE_STATIC(netlock);
  1219. /* Wait up to 16 seconds for first digit */
  1220. static int firstdigittimeout = 16000;
  1221. /* How long to wait for following digits */
  1222. static int gendigittimeout = 8000;
  1223. /* How long to wait for an extra digit, if there is an ambiguous match */
  1224. static int matchdigittimeout = 3000;
  1225. /*!
  1226. * To apease the stupid compiler option on ast_sched_del()
  1227. * since we don't care about the return value.
  1228. */
  1229. static int not_used;
  1230. #define SUBSTATE_UNSET 0
  1231. #define SUBSTATE_OFFHOOK 1
  1232. #define SUBSTATE_ONHOOK 2
  1233. #define SUBSTATE_RINGOUT 3
  1234. #define SUBSTATE_RINGIN 4
  1235. #define SUBSTATE_CONNECTED 5
  1236. #define SUBSTATE_BUSY 6
  1237. #define SUBSTATE_CONGESTION 7
  1238. #define SUBSTATE_HOLD 8
  1239. #define SUBSTATE_CALLWAIT 9
  1240. #define SUBSTATE_PROGRESS 12
  1241. #define SUBSTATE_DIALING 101
  1242. #define DIALTYPE_NORMAL 1<<0
  1243. #define DIALTYPE_CFWD 1<<1
  1244. #define DIALTYPE_XFER 1<<2
  1245. struct skinny_subchannel {
  1246. struct ast_channel *owner;
  1247. struct ast_rtp_instance *rtp;
  1248. struct ast_rtp_instance *vrtp;
  1249. unsigned int callid;
  1250. char exten[AST_MAX_EXTENSION];
  1251. /* time_t lastouttime; */ /* Unused */
  1252. int progress;
  1253. int ringing;
  1254. /* int lastout; */ /* Unused */
  1255. int cxmode;
  1256. int nat;
  1257. int calldirection;
  1258. int blindxfer;
  1259. int xferor;
  1260. int substate;
  1261. int aa_sched;
  1262. int aa_beep;
  1263. int aa_mute;
  1264. int dialer_sched;
  1265. int cfwd_sched;
  1266. int dialType;
  1267. int getforward;
  1268. char *origtonum;
  1269. char *origtoname;
  1270. AST_LIST_ENTRY(skinny_subchannel) list;
  1271. struct skinny_subchannel *related;
  1272. struct skinny_line *line;
  1273. struct skinny_subline *subline;
  1274. };
  1275. #define SKINNY_LINE_OPTIONS \
  1276. char name[80]; \
  1277. char label[24]; \
  1278. char accountcode[AST_MAX_ACCOUNT_CODE]; \
  1279. char exten[AST_MAX_EXTENSION]; \
  1280. char context[AST_MAX_CONTEXT]; \
  1281. char language[MAX_LANGUAGE]; \
  1282. char cid_num[AST_MAX_EXTENSION]; \
  1283. char cid_name[AST_MAX_EXTENSION]; \
  1284. char lastcallerid[AST_MAX_EXTENSION]; \
  1285. int cfwdtype; \
  1286. char call_forward_all[AST_MAX_EXTENSION]; \
  1287. char call_forward_busy[AST_MAX_EXTENSION]; \
  1288. char call_forward_noanswer[AST_MAX_EXTENSION]; \
  1289. char mailbox[AST_MAX_MAILBOX_UNIQUEID]; \
  1290. char vmexten[AST_MAX_EXTENSION]; \
  1291. char regexten[AST_MAX_EXTENSION]; \
  1292. char regcontext[AST_MAX_CONTEXT]; \
  1293. char parkinglot[AST_MAX_CONTEXT]; \
  1294. char mohinterpret[MAX_MUSICCLASS]; \
  1295. char mohsuggest[MAX_MUSICCLASS]; \
  1296. char lastnumberdialed[AST_MAX_EXTENSION]; \
  1297. char dialoutexten[AST_MAX_EXTENSION]; \
  1298. char dialoutcontext[AST_MAX_CONTEXT]; \
  1299. ast_group_t callgroup; \
  1300. ast_group_t pickupgroup; \
  1301. struct ast_namedgroups *named_callgroups; \
  1302. struct ast_namedgroups *named_pickupgroups; \
  1303. int callwaiting; \
  1304. int transfer; \
  1305. int threewaycalling; \
  1306. int mwiblink; \
  1307. int cancallforward; \
  1308. int callfwdtimeout; \
  1309. int dnd; \
  1310. int hidecallerid; \
  1311. int amaflags; \
  1312. int instance; \
  1313. int group; \
  1314. int nonCodecCapability; \
  1315. int immediate; \
  1316. int nat; \
  1317. int directmedia; \
  1318. int prune;
  1319. struct skinny_line {
  1320. SKINNY_LINE_OPTIONS
  1321. ast_mutex_t lock;
  1322. struct skinny_container *container;
  1323. struct stasis_subscription *mwi_event_sub; /* Event based MWI */
  1324. struct skinny_subchannel *activesub;
  1325. AST_LIST_HEAD(, skinny_subchannel) sub;
  1326. AST_LIST_HEAD(, skinny_subline) sublines;
  1327. AST_LIST_ENTRY(skinny_line) list;
  1328. AST_LIST_ENTRY(skinny_line) all;
  1329. struct skinny_device *device;
  1330. struct ast_format_cap *cap;
  1331. struct ast_format_cap *confcap;
  1332. struct ast_variable *chanvars; /*!< Channel variables to set for inbound call */
  1333. int newmsgs;
  1334. };
  1335. static struct skinny_line_options{
  1336. SKINNY_LINE_OPTIONS
  1337. } default_line_struct = {
  1338. .callwaiting = 1,
  1339. .transfer = 1,
  1340. .mwiblink = 0,
  1341. .dnd = 0,
  1342. .hidecallerid = 0,
  1343. .amaflags = 0,
  1344. .instance = 0,
  1345. .directmedia = 0,
  1346. .nat = 0,
  1347. .callfwdtimeout = 20000,
  1348. .prune = 0,
  1349. };
  1350. static struct skinny_line_options *default_line = &default_line_struct;
  1351. static AST_LIST_HEAD_STATIC(lines, skinny_line);
  1352. struct skinny_subline {
  1353. struct skinny_container *container;
  1354. struct skinny_line *line;
  1355. struct skinny_subchannel *sub;
  1356. AST_LIST_ENTRY(skinny_subline) list;
  1357. char name[80];
  1358. char context[AST_MAX_CONTEXT];
  1359. char exten[AST_MAX_EXTENSION];
  1360. char stname[AST_MAX_EXTENSION];
  1361. char lnname[AST_MAX_EXTENSION];
  1362. char ourName[40];
  1363. char ourNum[24];
  1364. char theirName[40];
  1365. char theirNum[24];
  1366. int calldirection;
  1367. int substate;
  1368. int extenstate;
  1369. unsigned int callid;
  1370. };
  1371. struct skinny_speeddial {
  1372. ast_mutex_t lock;
  1373. struct skinny_container *container;
  1374. char label[42];
  1375. char context[AST_MAX_CONTEXT];
  1376. char exten[AST_MAX_EXTENSION];
  1377. int instance;
  1378. int stateid;
  1379. int laststate;
  1380. int isHint;
  1381. AST_LIST_ENTRY(skinny_speeddial) list;
  1382. struct skinny_device *parent;
  1383. };
  1384. struct skinny_serviceurl {
  1385. int instance;
  1386. char url[MAX_SERVICEURL];
  1387. char displayName[40];
  1388. AST_LIST_ENTRY(skinny_serviceurl) list;
  1389. struct skinny_device *device;
  1390. };
  1391. #define SKINNY_DEVICECONTAINER 1
  1392. #define SKINNY_LINECONTAINER 2
  1393. #define SKINNY_SUBLINECONTAINER 3
  1394. #define SKINNY_SDCONTAINER 4
  1395. struct skinny_container {
  1396. int type;
  1397. void *data;
  1398. };
  1399. struct skinny_addon {
  1400. ast_mutex_t lock;
  1401. char type[10];
  1402. AST_LIST_ENTRY(skinny_addon) list;
  1403. struct skinny_device *parent;
  1404. };
  1405. #define SKINNY_DEVICE_OPTIONS \
  1406. char name[80]; \
  1407. char id[16]; \
  1408. char version_id[16]; \
  1409. char vmexten[AST_MAX_EXTENSION]; \
  1410. int type; \
  1411. int protocolversion; \
  1412. int hookstate; \
  1413. int lastlineinstance; \
  1414. int lastcallreference; \
  1415. int earlyrtp; \
  1416. int transfer; \
  1417. int callwaiting; \
  1418. int mwiblink; \
  1419. int dnd; \
  1420. int prune;
  1421. struct skinny_device {
  1422. SKINNY_DEVICE_OPTIONS
  1423. struct type *first;
  1424. struct type *last;
  1425. ast_mutex_t lock;
  1426. struct sockaddr_in addr;
  1427. struct in_addr ourip;
  1428. struct ast_ha *ha;
  1429. struct skinnysession *session;
  1430. struct skinny_line *activeline;
  1431. struct ast_format_cap *cap;
  1432. struct ast_format_cap *confcap;
  1433. struct ast_endpoint *endpoint;
  1434. AST_LIST_HEAD(, skinny_line) lines;
  1435. AST_LIST_HEAD(, skinny_speeddial) speeddials;
  1436. AST_LIST_HEAD(, skinny_serviceurl) serviceurls;
  1437. AST_LIST_HEAD(, skinny_addon) addons;
  1438. AST_LIST_ENTRY(skinny_device) list;
  1439. };
  1440. static struct skinny_device_options {
  1441. SKINNY_DEVICE_OPTIONS
  1442. } default_device_struct = {
  1443. .transfer = 1,
  1444. .earlyrtp = 1,
  1445. .callwaiting = 1,
  1446. .mwiblink = 0,
  1447. .dnd = 0,
  1448. .prune = 0,
  1449. .hookstate = SKINNY_ONHOOK,
  1450. };
  1451. static struct skinny_device_options *default_device = &default_device_struct;
  1452. static AST_LIST_HEAD_STATIC(devices, skinny_device);
  1453. struct skinnysession {
  1454. pthread_t t;
  1455. ast_mutex_t lock;
  1456. struct timeval start;
  1457. struct sockaddr_in sin;
  1458. int fd;
  1459. char outbuf[SKINNY_MAX_PACKET];
  1460. struct skinny_device *device;
  1461. AST_LIST_ENTRY(skinnysession) list;
  1462. int lockstate; /* Only for use in the skinny_session thread */
  1463. int auth_timeout_sched;
  1464. int keepalive_timeout_sched;
  1465. struct timeval last_keepalive;
  1466. int keepalive_count;
  1467. };
  1468. static struct ast_channel *skinny_request(const char *type, struct ast_format_cap *cap, const struct ast_assigned_ids *assignedids, const struct ast_channel *requestor, const char *dest, int *cause);
  1469. static AST_LIST_HEAD_STATIC(sessions, skinnysession);
  1470. static int skinny_devicestate(const char *data);
  1471. static int skinny_call(struct ast_channel *ast, const char *dest, int timeout);
  1472. static int skinny_hangup(struct ast_channel *ast);
  1473. static int skinny_answer(struct ast_channel *ast);
  1474. static struct ast_frame *skinny_read(struct ast_channel *ast);
  1475. static int skinny_write(struct ast_channel *ast, struct ast_frame *frame);
  1476. static int skinny_indicate(struct ast_channel *ast, int ind, const void *data, size_t datalen);
  1477. static int skinny_fixup(struct ast_channel *oldchan, struct ast_channel *newchan);
  1478. static int skinny_senddigit_begin(struct ast_channel *ast, char digit);
  1479. static int skinny_senddigit_end(struct ast_channel *ast, char digit, unsigned int duration);
  1480. static void mwi_event_cb(void *userdata, struct stasis_subscription *sub, struct stasis_message *msg);
  1481. static int skinny_dialer_cb(const void *data);
  1482. static int skinny_reload(void);
  1483. static void skinny_set_owner(struct skinny_subchannel* sub, struct ast_channel* chan);
  1484. static void setsubstate(struct skinny_subchannel *sub, int state);
  1485. static void dumpsub(struct skinny_subchannel *sub, int forcehangup);
  1486. static void activatesub(struct skinny_subchannel *sub, int state);
  1487. static void dialandactivatesub(struct skinny_subchannel *sub, char exten[AST_MAX_EXTENSION]);
  1488. static int skinny_nokeepalive_cb(const void *data);
  1489. static void transmit_definetimedate(struct skinny_device *d);
  1490. static struct ast_channel_tech skinny_tech = {
  1491. .type = "Skinny",
  1492. .description = tdesc,
  1493. .properties = AST_CHAN_TP_WANTSJITTER | AST_CHAN_TP_CREATESJITTER,
  1494. .requester = skinny_request,
  1495. .devicestate = skinny_devicestate,
  1496. .call = skinny_call,
  1497. .hangup = skinny_hangup,
  1498. .answer = skinny_answer,
  1499. .read = skinny_read,
  1500. .write = skinny_write,
  1501. .indicate = skinny_indicate,
  1502. .fixup = skinny_fixup,
  1503. .send_digit_begin = skinny_senddigit_begin,
  1504. .send_digit_end = skinny_senddigit_end,
  1505. };
  1506. static int skinny_extensionstate_cb(char *context, char *id, struct ast_state_cb_info *info, void *data);
  1507. static struct skinny_line *skinny_line_alloc(void)
  1508. {
  1509. struct skinny_line *l;
  1510. if (!(l = ast_calloc(1, sizeof(*l)))) {
  1511. return NULL;
  1512. }
  1513. l->cap = ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_DEFAULT);
  1514. l->confcap = ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_DEFAULT);
  1515. if (!l->cap || !l->confcap) {
  1516. ao2_cleanup(l->cap);
  1517. ao2_cleanup(l->confcap);
  1518. ast_free(l);
  1519. return NULL;
  1520. }
  1521. return l;
  1522. }
  1523. static struct skinny_line *skinny_line_destroy(struct skinny_line *l)
  1524. {
  1525. ao2_ref(l->cap, -1);
  1526. ao2_ref(l->confcap, -1);
  1527. l->named_callgroups = ast_unref_namedgroups(l->named_callgroups);
  1528. l->named_pickupgroups = ast_unref_namedgroups(l->named_pickupgroups);
  1529. ast_free(l->container);
  1530. ast_free(l);
  1531. return NULL;
  1532. }
  1533. static struct skinny_device *skinny_device_alloc(const char *dname)
  1534. {
  1535. struct skinny_device *d;
  1536. if (!(d = ast_calloc(1, sizeof(*d)))) {
  1537. return NULL;
  1538. }
  1539. d->cap = ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_DEFAULT);
  1540. d->confcap = ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_DEFAULT);
  1541. d->endpoint = ast_endpoint_create("Skinny", dname);
  1542. if (!d->cap || !d->confcap || !d->endpoint) {
  1543. ao2_cleanup(d->cap);
  1544. ao2_cleanup(d->confcap);
  1545. ast_free(d);
  1546. return NULL;
  1547. }
  1548. ast_copy_string(d->name, dname, sizeof(d->name));
  1549. return d;
  1550. }
  1551. static struct skinny_device *skinny_device_destroy(struct skinny_device *d)
  1552. {
  1553. ao2_ref(d->cap, -1);
  1554. ao2_ref(d->confcap, -1);
  1555. ast_endpoint_shutdown(d->endpoint);
  1556. ast_free(d);
  1557. return NULL;
  1558. }
  1559. static void *get_button_template(struct skinnysession *s, struct button_definition_template *btn)
  1560. {
  1561. struct skinny_device *d = s->device;
  1562. struct skinny_addon *a;
  1563. int i;
  1564. switch (d->type) {
  1565. case SKINNY_DEVICE_30SPPLUS:
  1566. case SKINNY_DEVICE_30VIP:
  1567. /* 13 rows, 2 columns */
  1568. for (i = 0; i < 4; i++)
  1569. (btn++)->buttonDefinition = BT_CUST_LINE;
  1570. (btn++)->buttonDefinition = BT_REDIAL;
  1571. (btn++)->buttonDefinition = BT_VOICEMAIL;
  1572. (btn++)->buttonDefinition = BT_CALLPARK;
  1573. (btn++)->buttonDefinition = BT_FORWARDALL;
  1574. (btn++)->buttonDefinition = BT_CONFERENCE;
  1575. for (i = 0; i < 4; i++)
  1576. (btn++)->buttonDefinition = BT_NONE;
  1577. for (i = 0; i < 13; i++)
  1578. (btn++)->buttonDefinition = BT_SPEEDDIAL;
  1579. break;
  1580. case SKINNY_DEVICE_12SPPLUS:
  1581. case SKINNY_DEVICE_12SP:
  1582. case SKINNY_DEVICE_12:
  1583. /* 6 rows, 2 columns */
  1584. for (i = 0; i < 2; i++)
  1585. (btn++)->buttonDefinition = BT_CUST_LINE;
  1586. for (i = 0; i < 4; i++)
  1587. (btn++)->buttonDefinition = BT_SPEEDDIAL;
  1588. (btn++)->buttonDefinition = BT_HOLD;
  1589. (btn++)->buttonDefinition = BT_REDIAL;
  1590. (btn++)->buttonDefinition = BT_TRANSFER;
  1591. (btn++)->buttonDefinition = BT_FORWARDALL;
  1592. (btn++)->buttonDefinition = BT_CALLPARK;
  1593. (btn++)->buttonDefinition = BT_VOICEMAIL;
  1594. break;
  1595. case SKINNY_DEVICE_7910:
  1596. (btn++)->buttonDefinition = BT_LINE;
  1597. (btn++)->buttonDefinition = BT_HOLD;
  1598. (btn++)->buttonDefinition = BT_TRANSFER;
  1599. (btn++)->buttonDefinition = BT_DISPLAY;
  1600. (btn++)->buttonDefinition = BT_VOICEMAIL;
  1601. (btn++)->buttonDefinition = BT_CONFERENCE;
  1602. (btn++)->buttonDefinition = BT_FORWARDALL;
  1603. for (i = 0; i < 2; i++)
  1604. (btn++)->buttonDefinition = BT_SPEEDDIAL;
  1605. (btn++)->buttonDefinition = BT_REDIAL;
  1606. break;
  1607. case SKINNY_DEVICE_7960:
  1608. case SKINNY_DEVICE_7961:
  1609. case SKINNY_DEVICE_7961GE:
  1610. case SKINNY_DEVICE_7962:
  1611. case SKINNY_DEVICE_7965:
  1612. for (i = 0; i < 6; i++)
  1613. (btn++)->buttonDefinition = BT_CUST_LINESPEEDDIAL;
  1614. break;
  1615. case SKINNY_DEVICE_7940:
  1616. case SKINNY_DEVICE_7941:
  1617. case SKINNY_DEVICE_7941GE:
  1618. case SKINNY_DEVICE_7942:
  1619. case SKINNY_DEVICE_7945:
  1620. for (i = 0; i < 2; i++)
  1621. (btn++)->buttonDefinition = BT_CUST_LINESPEEDDIAL;
  1622. break;
  1623. case SKINNY_DEVICE_7935:
  1624. case SKINNY_DEVICE_7936:
  1625. for (i = 0; i < 2; i++)
  1626. (btn++)->buttonDefinition = BT_LINE;
  1627. break;
  1628. case SKINNY_DEVICE_ATA186:
  1629. (btn++)->buttonDefinition = BT_LINE;
  1630. break;
  1631. case SKINNY_DEVICE_7970:
  1632. case SKINNY_DEVICE_7971:
  1633. case SKINNY_DEVICE_7975:
  1634. case SKINNY_DEVICE_CIPC:
  1635. for (i = 0; i < 8; i++)
  1636. (btn++)->buttonDefinition = BT_CUST_LINESPEEDDIAL;
  1637. break;
  1638. case SKINNY_DEVICE_7985:
  1639. /* XXX I have no idea what the buttons look like on these. */
  1640. ast_log(LOG_WARNING, "Unsupported device type '%d (7985)' found.\n", d->type);
  1641. break;
  1642. case SKINNY_DEVICE_7912:
  1643. case SKINNY_DEVICE_7911:
  1644. case SKINNY_DEVICE_7905:
  1645. (btn++)->buttonDefinition = BT_LINE;
  1646. (btn++)->buttonDefinition = BT_HOLD;
  1647. break;
  1648. case SKINNY_DEVICE_7920:
  1649. /* XXX I don't know if this is right. */
  1650. for (i = 0; i < 4; i++)
  1651. (btn++)->buttonDefinition = BT_CUST_LINESPEEDDIAL;
  1652. break;
  1653. case SKINNY_DEVICE_7921:
  1654. for (i = 0; i < 6; i++)
  1655. (btn++)->buttonDefinition = BT_CUST_LINESPEEDDIAL;
  1656. break;
  1657. case SKINNY_DEVICE_7902:
  1658. ast_log(LOG_WARNING, "Unsupported device type '%d (7902)' found.\n", d->type);
  1659. break;
  1660. case SKINNY_DEVICE_7906:
  1661. ast_log(LOG_WARNING, "Unsupported device type '%d (7906)' found.\n", d->type);
  1662. break;
  1663. case SKINNY_DEVICE_7931:
  1664. ast_log(LOG_WARNING, "Unsupported device type '%d (7931)' found.\n", d->type);
  1665. break;
  1666. case SKINNY_DEVICE_7937:
  1667. ast_log(LOG_WARNING, "Unsupported device type '%d (7937)' found.\n", d->type);
  1668. break;
  1669. case SKINNY_DEVICE_7914:
  1670. ast_log(LOG_WARNING, "Unsupported device type '%d (7914)' found. Expansion module registered by itself?\n", d->type);
  1671. break;
  1672. case SKINNY_DEVICE_SCCPGATEWAY_AN:
  1673. case SKINNY_DEVICE_SCCPGATEWAY_BRI:
  1674. ast_log(LOG_WARNING, "Unsupported device type '%d (SCCP gateway)' found.\n", d->type);
  1675. break;
  1676. default:
  1677. ast_log(LOG_WARNING, "Unknown device type '%d' found.\n", d->type);
  1678. break;
  1679. }
  1680. AST_LIST_LOCK(&d->addons);
  1681. AST_LIST_TRAVERSE(&d->addons, a, list) {
  1682. if (!strcasecmp(a->type, "7914")) {
  1683. for (i = 0; i < 14; i++)
  1684. (btn++)->buttonDefinition = BT_CUST_LINESPEEDDIAL;
  1685. } else {
  1686. ast_log(LOG_WARNING, "Unknown addon type '%s' found. Skipping.\n", a->type);
  1687. }
  1688. }
  1689. AST_LIST_UNLOCK(&d->addons);
  1690. return btn;
  1691. }
  1692. static struct skinny_req *req_alloc(size_t size, int response_message)
  1693. {
  1694. struct skinny_req *req;
  1695. if (!(req = ast_calloc(1, skinny_header_size + size + 4)))
  1696. return NULL;
  1697. req->len = htolel(size+4);
  1698. req->e = htolel(response_message);
  1699. return req;
  1700. }
  1701. static struct skinny_line *find_line_by_instance(struct skinny_device *d, int instance)
  1702. {
  1703. struct skinny_line *l;
  1704. /*Dialing from on hook or on a 7920 uses instance 0 in requests
  1705. but we need to start looking at instance 1 */
  1706. if (!instance)
  1707. instance = 1;
  1708. AST_LIST_TRAVERSE(&d->lines, l, list){
  1709. if (l->instance == instance)
  1710. break;
  1711. }
  1712. if (!l) {
  1713. ast_log(LOG_WARNING, "Could not find line with instance '%d' on device '%s'\n", instance, d->name);
  1714. }
  1715. return l;
  1716. }
  1717. static struct skinny_line *find_line_by_name(const char *dest)
  1718. {
  1719. struct skinny_line *l;
  1720. struct skinny_line *tmpl = NULL;
  1721. struct skinny_device *d;
  1722. char line[256];
  1723. char *at;
  1724. char *device;
  1725. int checkdevice = 0;
  1726. ast_copy_string(line, dest, sizeof(line));
  1727. at = strchr(line, '@');
  1728. if (at)
  1729. *at++ = '\0';
  1730. device = at;
  1731. if (!ast_strlen_zero(device))
  1732. checkdevice = 1;
  1733. AST_LIST_LOCK(&devices);
  1734. AST_LIST_TRAVERSE(&devices, d, list){
  1735. if (checkdevice && tmpl)
  1736. break;
  1737. else if (!checkdevice) {
  1738. /* This is a match, since we're checking for line on every device. */
  1739. } else if (!strcasecmp(d->name, device)) {
  1740. } else
  1741. continue;
  1742. /* Found the device (or we don't care which device) */
  1743. AST_LIST_TRAVERSE(&d->lines, l, list){
  1744. /* Search for the right line */
  1745. if (!strcasecmp(l->name, line)) {
  1746. if (tmpl) {
  1747. ast_log(LOG_WARNING, "Ambiguous line name: %s\n", line);
  1748. AST_LIST_UNLOCK(&devices);
  1749. return NULL;
  1750. } else
  1751. tmpl = l;
  1752. }
  1753. }
  1754. }
  1755. AST_LIST_UNLOCK(&devices);
  1756. return tmpl;
  1757. }
  1758. static struct skinny_subline *find_subline_by_name(const char *dest)
  1759. {
  1760. struct skinny_line *l;
  1761. struct skinny_subline *subline;
  1762. struct skinny_subline *tmpsubline = NULL;
  1763. struct skinny_device *d;
  1764. AST_LIST_LOCK(&devices);
  1765. AST_LIST_TRAVERSE(&devices, d, list){
  1766. AST_LIST_TRAVERSE(&d->lines, l, list){
  1767. AST_LIST_TRAVERSE(&l->sublines, subline, list){
  1768. if (!strcasecmp(subline->name, dest)) {
  1769. if (tmpsubline) {
  1770. ast_verb(2, "Ambiguous subline name: %s\n", dest);
  1771. AST_LIST_UNLOCK(&devices);
  1772. return NULL;
  1773. } else
  1774. tmpsubline = subline;
  1775. }
  1776. }
  1777. }
  1778. }
  1779. AST_LIST_UNLOCK(&devices);
  1780. return tmpsubline;
  1781. }
  1782. static struct skinny_subline *find_subline_by_callid(struct skinny_device *d, int callid)
  1783. {
  1784. struct skinny_subline *subline;
  1785. struct skinny_line *l;
  1786. AST_LIST_TRAVERSE(&d->lines, l, list){
  1787. AST_LIST_TRAVERSE(&l->sublines, subline, list){
  1788. if (subline->callid == callid) {
  1789. return subline;
  1790. }
  1791. }
  1792. }
  1793. return NULL;
  1794. }
  1795. /*!
  1796. * implement the setvar config line
  1797. */
  1798. static struct ast_variable *add_var(const char *buf, struct ast_variable *list)
  1799. {
  1800. struct ast_variable *tmpvar = NULL;
  1801. char *varname = ast_strdupa(buf), *varval = NULL;
  1802. if ((varval = strchr(varname,'='))) {
  1803. *varval++ = '\0';
  1804. if ((tmpvar = ast_variable_new(varname, varval, ""))) {
  1805. tmpvar->next = list;
  1806. list = tmpvar;
  1807. }
  1808. }
  1809. return list;
  1810. }
  1811. static void skinny_locksub(struct skinny_subchannel *sub)
  1812. {
  1813. if (sub && sub->owner) {
  1814. ast_channel_lock(sub->owner);
  1815. }
  1816. }
  1817. static void skinny_unlocksub(struct skinny_subchannel *sub)
  1818. {
  1819. if (sub && sub->owner) {
  1820. ast_channel_unlock(sub->owner);
  1821. }
  1822. }
  1823. static int skinny_sched_del(int sched_id, struct skinny_subchannel *sub)
  1824. {
  1825. SKINNY_DEBUG(DEBUG_SUB, 3, "Sub %u - Deleting SCHED %d\n",
  1826. sub->callid, sched_id);
  1827. return ast_sched_del(sched, sched_id);
  1828. }
  1829. static int skinny_sched_add(int when, ast_sched_cb callback, struct skinny_subchannel *sub)
  1830. {
  1831. int ret;
  1832. ret = ast_sched_add(sched, when, callback, sub);
  1833. SKINNY_DEBUG(DEBUG_SUB, 3, "Sub %u - Added SCHED %d\n",
  1834. sub->callid, ret);
  1835. return ret;
  1836. }
  1837. /* It's quicker/easier to find the subchannel when we know the instance number too */
  1838. static struct skinny_subchannel *find_subchannel_by_instance_reference(struct skinny_device *d, int instance, int reference)
  1839. {
  1840. struct skinny_line *l = find_line_by_instance(d, instance);
  1841. struct skinny_subchannel *sub;
  1842. if (!l) {
  1843. return NULL;
  1844. }
  1845. /* 7920 phones set call reference to 0, so use the first
  1846. sub-channel on the list.
  1847. This MIGHT need more love to be right */
  1848. if (!reference)
  1849. sub = AST_LIST_FIRST(&l->sub);
  1850. else {
  1851. AST_LIST_TRAVERSE(&l->sub, sub, list) {
  1852. if (sub->callid == reference)
  1853. break;
  1854. }
  1855. }
  1856. if (!sub) {
  1857. ast_log(LOG_WARNING, "Could not find subchannel with reference '%d' on '%s'\n", reference, d->name);
  1858. }
  1859. return sub;
  1860. }
  1861. /* Find the subchannel when we only have the callid - this shouldn't happen often */
  1862. static struct skinny_subchannel *find_subchannel_by_reference(struct skinny_device *d, int reference)
  1863. {
  1864. struct skinny_line *l;
  1865. struct skinny_subchannel *sub = NULL;
  1866. AST_LIST_TRAVERSE(&d->lines, l, list){
  1867. AST_LIST_TRAVERSE(&l->sub, sub, list){
  1868. if (sub->callid == reference)
  1869. break;
  1870. }
  1871. if (sub)
  1872. break;
  1873. }
  1874. if (!l) {
  1875. ast_log(LOG_WARNING, "Could not find any lines that contained a subchannel with reference '%d' on device '%s'\n", reference, d->name);
  1876. } else {
  1877. if (!sub) {
  1878. ast_log(LOG_WARNING, "Could not find subchannel with reference '%d' on '%s@%s'\n", reference, l->name, d->name);
  1879. }
  1880. }
  1881. return sub;
  1882. }
  1883. static struct skinny_speeddial *find_speeddial_by_instance(struct skinny_device *d, int instance, int isHint)
  1884. {
  1885. struct skinny_speeddial *sd;
  1886. AST_LIST_TRAVERSE(&d->speeddials, sd, list) {
  1887. if (sd->isHint == isHint && sd->instance == instance)
  1888. break;
  1889. }
  1890. if (!sd) {
  1891. ast_log(LOG_WARNING, "Could not find speeddial with instance '%d' on device '%s'\n", instance, d->name);
  1892. }
  1893. return sd;
  1894. }
  1895. static struct ast_format *codec_skinny2ast(enum skinny_codecs skinnycodec)
  1896. {
  1897. switch (skinnycodec) {
  1898. case SKINNY_CODEC_ALAW:
  1899. return ast_format_alaw;
  1900. case SKINNY_CODEC_ULAW:
  1901. return ast_format_ulaw;
  1902. case SKINNY_CODEC_G722:
  1903. return ast_format_g722;
  1904. case SKINNY_CODEC_G723_1:
  1905. return ast_format_g723;
  1906. case SKINNY_CODEC_G729A:
  1907. return ast_format_g729;
  1908. case SKINNY_CODEC_G726_32:
  1909. return ast_format_g726; /* XXX Is this right? */
  1910. case SKINNY_CODEC_H261:
  1911. return ast_format_h261;
  1912. case SKINNY_CODEC_H263:
  1913. return ast_format_h263;
  1914. default:
  1915. return ast_format_none;
  1916. }
  1917. }
  1918. static int codec_ast2skinny(const struct ast_format *astcodec)
  1919. {
  1920. if (ast_format_cmp(astcodec, ast_format_alaw) == AST_FORMAT_CMP_EQUAL) {
  1921. return SKINNY_CODEC_ALAW;
  1922. } else if (ast_format_cmp(astcodec, ast_format_ulaw) == AST_FORMAT_CMP_EQUAL) {
  1923. return SKINNY_CODEC_ULAW;
  1924. } else if (ast_format_cmp(astcodec, ast_format_g722) == AST_FORMAT_CMP_EQUAL) {
  1925. return SKINNY_CODEC_G722;
  1926. } else if (ast_format_cmp(astcodec, ast_format_g723) == AST_FORMAT_CMP_EQUAL) {
  1927. return SKINNY_CODEC_G723_1;
  1928. } else if (ast_format_cmp(astcodec, ast_format_g729) == AST_FORMAT_CMP_EQUAL) {
  1929. return SKINNY_CODEC_G729A;
  1930. } else if (ast_format_cmp(astcodec, ast_format_g726) == AST_FORMAT_CMP_EQUAL) {
  1931. return SKINNY_CODEC_G726_32;
  1932. } else if (ast_format_cmp(astcodec, ast_format_h261) == AST_FORMAT_CMP_EQUAL) {
  1933. return SKINNY_CODEC_H261;
  1934. } else if (ast_format_cmp(astcodec, ast_format_h263) == AST_FORMAT_CMP_EQUAL) {
  1935. return SKINNY_CODEC_H263;
  1936. } else {
  1937. return 0;
  1938. }
  1939. }
  1940. static int set_callforwards(struct skinny_line *l, const char *cfwd, int cfwdtype)
  1941. {
  1942. if (!l)
  1943. return 0;
  1944. if (!ast_strlen_zero(cfwd)) {
  1945. if (cfwdtype & SKINNY_CFWD_ALL) {
  1946. l->cfwdtype |= SKINNY_CFWD_ALL;
  1947. ast_copy_string(l->call_forward_all, cfwd, sizeof(l->call_forward_all));
  1948. }
  1949. if (cfwdtype & SKINNY_CFWD_BUSY) {
  1950. l->cfwdtype |= SKINNY_CFWD_BUSY;
  1951. ast_copy_string(l->call_forward_busy, cfwd, sizeof(l->call_forward_busy));
  1952. }
  1953. if (cfwdtype & SKINNY_CFWD_NOANSWER) {
  1954. l->cfwdtype |= SKINNY_CFWD_NOANSWER;
  1955. ast_copy_string(l->call_forward_noanswer, cfwd, sizeof(l->call_forward_noanswer));
  1956. }
  1957. } else {
  1958. if (cfwdtype & SKINNY_CFWD_ALL) {
  1959. l->cfwdtype &= ~SKINNY_CFWD_ALL;
  1960. memset(l->call_forward_all, 0, sizeof(l->call_forward_all));
  1961. }
  1962. if (cfwdtype & SKINNY_CFWD_BUSY) {
  1963. l->cfwdtype &= ~SKINNY_CFWD_BUSY;
  1964. memset(l->call_forward_busy, 0, sizeof(l->call_forward_busy));
  1965. }
  1966. if (cfwdtype & SKINNY_CFWD_NOANSWER) {
  1967. l->cfwdtype &= ~SKINNY_CFWD_NOANSWER;
  1968. memset(l->call_forward_noanswer, 0, sizeof(l->call_forward_noanswer));
  1969. }
  1970. }
  1971. return l->cfwdtype;
  1972. }
  1973. static void cleanup_stale_contexts(char *new, char *old)
  1974. {
  1975. char *oldcontext, *newcontext, *stalecontext, *stringp, newlist[AST_MAX_CONTEXT];
  1976. while ((oldcontext = strsep(&old, "&"))) {
  1977. stalecontext = NULL;
  1978. ast_copy_string(newlist, new, sizeof(newlist));
  1979. stringp = newlist;
  1980. while ((newcontext = strsep(&stringp, "&"))) {
  1981. if (strcmp(newcontext, oldcontext) == 0) {
  1982. /* This is not the context you're looking for */
  1983. stalecontext = NULL;
  1984. break;
  1985. } else if (strcmp(newcontext, oldcontext)) {
  1986. stalecontext = oldcontext;
  1987. }
  1988. }
  1989. ast_context_destroy_by_name(stalecontext, "Skinny");
  1990. }
  1991. }
  1992. static void register_exten(struct skinny_line *l)
  1993. {
  1994. char multi[256];
  1995. char *stringp, *ext, *context;
  1996. if (ast_strlen_zero(regcontext))
  1997. return;
  1998. ast_copy_string(multi, S_OR(l->regexten, l->name), sizeof(multi));
  1999. stringp = multi;
  2000. while ((ext = strsep(&stringp, "&"))) {
  2001. if ((context = strchr(ext, '@'))) {
  2002. *context++ = '\0'; /* split ext@context */
  2003. if (!ast_context_find(context)) {
  2004. ast_log(LOG_WARNING, "Context %s must exist in regcontext= in skinny.conf!\n", context);
  2005. continue;
  2006. }
  2007. } else {
  2008. context = regcontext;
  2009. }
  2010. ast_add_extension(context, 1, ext, 1, NULL, NULL, "Noop",
  2011. ast_strdup(l->name), ast_free_ptr, "Skinny");
  2012. }
  2013. }
  2014. static void unregister_exten(struct skinny_line *l)
  2015. {
  2016. char multi[256];
  2017. char *stringp, *ext, *context;
  2018. if (ast_strlen_zero(regcontext))
  2019. return;
  2020. ast_copy_string(multi, S_OR(l->regexten, l->name), sizeof(multi));
  2021. stringp = multi;
  2022. while ((ext = strsep(&stringp, "&"))) {
  2023. if ((context = strchr(ext, '@'))) {
  2024. *context++ = '\0'; /* split ext@context */
  2025. if (!ast_context_find(context)) {
  2026. ast_log(LOG_WARNING, "Context %s must exist in regcontext= in skinny.conf!\n", context);
  2027. continue;
  2028. }
  2029. } else {
  2030. context = regcontext;
  2031. }
  2032. ast_context_remove_extension(context, ext, 1, NULL);
  2033. }
  2034. }
  2035. static int skinny_register(struct skinny_req *req, struct skinnysession *s)
  2036. {
  2037. struct skinny_device *d;
  2038. struct skinny_line *l;
  2039. struct skinny_subline *subline;
  2040. struct skinny_speeddial *sd;
  2041. struct sockaddr_in sin;
  2042. socklen_t slen;
  2043. int instance;
  2044. int res = -1;
  2045. if (-1 < s->auth_timeout_sched) {
  2046. not_used = ast_sched_del(sched, s->auth_timeout_sched);
  2047. s->auth_timeout_sched = -1;
  2048. }
  2049. AST_LIST_LOCK(&devices);
  2050. AST_LIST_TRAVERSE(&devices, d, list){
  2051. struct ast_sockaddr addr;
  2052. ast_sockaddr_from_sin(&addr, &s->sin);
  2053. if (!strcasecmp(req->data.reg.name, d->id)
  2054. && ast_apply_ha(d->ha, &addr)) {
  2055. RAII_VAR(struct ast_json *, blob, NULL, ast_json_unref);
  2056. if (d->session) {
  2057. ast_log(LOG_WARNING, "Device already registered.\n");
  2058. transmit_definetimedate(d);
  2059. res = 0;
  2060. break;
  2061. }
  2062. s->device = d;
  2063. d->type = letohl(req->data.reg.type);
  2064. d->protocolversion = letohl(req->data.reg.protocolVersion);
  2065. if (ast_strlen_zero(d->version_id)) {
  2066. ast_copy_string(d->version_id, version_id, sizeof(d->version_id));
  2067. }
  2068. d->session = s;
  2069. slen = sizeof(sin);
  2070. if (getsockname(s->fd, (struct sockaddr *)&sin, &slen)) {
  2071. ast_log(LOG_WARNING, "Cannot get socket name\n");
  2072. sin.sin_addr = __ourip;
  2073. }
  2074. d->ourip = sin.sin_addr;
  2075. AST_LIST_TRAVERSE(&d->speeddials, sd, list) {
  2076. sd->stateid = ast_extension_state_add(sd->context, sd->exten, skinny_extensionstate_cb, sd->container);
  2077. }
  2078. instance = 0;
  2079. AST_LIST_TRAVERSE(&d->lines, l, list) {
  2080. instance++;
  2081. }
  2082. AST_LIST_TRAVERSE(&d->lines, l, list) {
  2083. ast_format_cap_get_compatible(l->confcap, d->cap, l->cap);
  2084. /* l->capability = d->capability; */
  2085. l->instance = instance;
  2086. l->newmsgs = ast_app_has_voicemail(l->mailbox, NULL);
  2087. set_callforwards(l, NULL, SKINNY_CFWD_ALL|SKINNY_CFWD_BUSY|SKINNY_CFWD_NOANSWER);
  2088. register_exten(l);
  2089. /* initialize MWI on line and device */
  2090. mwi_event_cb(l, NULL, NULL);
  2091. AST_LIST_TRAVERSE(&l->sublines, subline, list) {
  2092. ast_extension_state_add(subline->context, subline->exten, skinny_extensionstate_cb, subline->container);
  2093. }
  2094. ast_devstate_changed(AST_DEVICE_NOT_INUSE, AST_DEVSTATE_CACHABLE, "Skinny/%s", l->name);
  2095. --instance;
  2096. }
  2097. ast_endpoint_set_state(d->endpoint, AST_ENDPOINT_ONLINE);
  2098. blob = ast_json_pack("{s: s}", "peer_status", "Registered");
  2099. ast_endpoint_blob_publish(d->endpoint, ast_endpoint_state_type(), blob);
  2100. res = 1;
  2101. break;
  2102. }
  2103. }
  2104. AST_LIST_UNLOCK(&devices);
  2105. return res;
  2106. }
  2107. static void end_session(struct skinnysession *s)
  2108. {
  2109. pthread_cancel(s->t);
  2110. }
  2111. #ifdef AST_DEVMODE
  2112. static char *callstate2str(int ind)
  2113. {
  2114. char *tmp;
  2115. switch (ind) {
  2116. case SKINNY_OFFHOOK:
  2117. return "SKINNY_OFFHOOK";
  2118. case SKINNY_ONHOOK:
  2119. return "SKINNY_ONHOOK";
  2120. case SKINNY_RINGOUT:
  2121. return "SKINNY_RINGOUT";
  2122. case SKINNY_RINGIN:
  2123. return "SKINNY_RINGIN";
  2124. case SKINNY_CONNECTED:
  2125. return "SKINNY_CONNECTED";
  2126. case SKINNY_BUSY:
  2127. return "SKINNY_BUSY";
  2128. case SKINNY_CONGESTION:
  2129. return "SKINNY_CONGESTION";
  2130. case SKINNY_PROGRESS:
  2131. return "SKINNY_PROGRESS";
  2132. case SKINNY_HOLD:
  2133. return "SKINNY_HOLD";
  2134. case SKINNY_CALLWAIT:
  2135. return "SKINNY_CALLWAIT";
  2136. default:
  2137. if (!(tmp = ast_threadstorage_get(&callstate2str_threadbuf, CALLSTATE2STR_BUFSIZE)))
  2138. return "Unknown";
  2139. snprintf(tmp, CALLSTATE2STR_BUFSIZE, "UNKNOWN-%d", ind);
  2140. return tmp;
  2141. }
  2142. }
  2143. #endif
  2144. static int transmit_response_bysession(struct skinnysession *s, struct skinny_req *req)
  2145. {
  2146. int res = 0;
  2147. unsigned long len;
  2148. if (!s) {
  2149. ast_log(LOG_WARNING, "Asked to transmit to a non-existent session!\n");
  2150. return -1;
  2151. }
  2152. ast_mutex_lock(&s->lock);
  2153. /* Don't optimize out assigning letohl() to len. It is necessary to guarantee that the comparison will always catch invalid values.
  2154. * letohl() may or may not return a signed value depending upon which definition is used. */
  2155. len = letohl(req->len);
  2156. if (SKINNY_MAX_PACKET < len) {
  2157. ast_log(LOG_WARNING, "transmit_response: the length of the request (%u) is out of bounds (%d)\n", letohl(req->len), SKINNY_MAX_PACKET);
  2158. ast_mutex_unlock(&s->lock);
  2159. return -1;
  2160. }
  2161. memset(s->outbuf, 0, sizeof(s->outbuf));
  2162. memcpy(s->outbuf, req, skinny_header_size);
  2163. memcpy(s->outbuf+skinny_header_size, &req->data, letohl(req->len));
  2164. res = write(s->fd, s->outbuf, letohl(req->len)+8);
  2165. if (res != letohl(req->len)+8) {
  2166. ast_log(LOG_WARNING, "Transmit: write only sent %d out of %u bytes: %s\n", res, letohl(req->len)+8, strerror(errno));
  2167. if (res == -1) {
  2168. ast_log(LOG_WARNING, "Transmit: Skinny Client was lost, unregistering\n");
  2169. end_session(s);
  2170. }
  2171. }
  2172. ast_free(req);
  2173. ast_mutex_unlock(&s->lock);
  2174. return 1;
  2175. }
  2176. static void transmit_response(struct skinny_device *d, struct skinny_req *req)
  2177. {
  2178. transmit_response_bysession(d->session, req);
  2179. }
  2180. static void transmit_registerrej(struct skinnysession *s)
  2181. {
  2182. struct skinny_req *req;
  2183. char name[16];
  2184. if (!(req = req_alloc(sizeof(struct register_rej_message), REGISTER_REJ_MESSAGE)))
  2185. return;
  2186. memcpy(&name, req->data.reg.name, sizeof(name));
  2187. snprintf(req->data.regrej.errMsg, sizeof(req->data.regrej.errMsg), "No Authority: %s", name);
  2188. SKINNY_DEBUG(DEBUG_PACKET, 3, "Transmitting REGISTER_REJ_MESSAGE to UNKNOWN_DEVICE\n");
  2189. transmit_response_bysession(s, req);
  2190. }
  2191. static void transmit_speaker_mode(struct skinny_device *d, int mode)
  2192. {
  2193. struct skinny_req *req;
  2194. if (!(req = req_alloc(sizeof(struct set_speaker_message), SET_SPEAKER_MESSAGE)))
  2195. return;
  2196. req->data.setspeaker.mode = htolel(mode);
  2197. SKINNY_DEBUG(DEBUG_PACKET, 3, "Transmitting SET_SPEAKER_MESSAGE to %s, mode %d\n", d->name, mode);
  2198. transmit_response(d, req);
  2199. }
  2200. static void transmit_microphone_mode(struct skinny_device *d, int mode)
  2201. {
  2202. struct skinny_req *req;
  2203. if (!(req = req_alloc(sizeof(struct set_microphone_message), SET_MICROPHONE_MESSAGE)))
  2204. return;
  2205. req->data.setmicrophone.mode = htolel(mode);
  2206. SKINNY_DEBUG(DEBUG_PACKET, 3, "Transmitting SET_MICROPHONE_MESSAGE to %s, mode %d\n", d->name, mode);
  2207. transmit_response(d, req);
  2208. }
  2209. //static void transmit_callinfo(struct skinny_subchannel *sub)
  2210. static void transmit_callinfo(struct skinny_device *d, int instance, int callid,
  2211. char *fromname, char *fromnum, char *toname, char *tonum, int calldirection, char *origtonum, char *origtoname)
  2212. {
  2213. struct skinny_req *req;
  2214. if (!(req = req_alloc(sizeof(struct call_info_message), CALL_INFO_MESSAGE)))
  2215. return;
  2216. ast_copy_string(req->data.callinfo.callingPartyName, fromname, sizeof(req->data.callinfo.callingPartyName));
  2217. ast_copy_string(req->data.callinfo.callingParty, fromnum, sizeof(req->data.callinfo.callingParty));
  2218. ast_copy_string(req->data.callinfo.calledPartyName, toname, sizeof(req->data.callinfo.calledPartyName));
  2219. ast_copy_string(req->data.callinfo.calledParty, tonum, sizeof(req->data.callinfo.calledParty));
  2220. if (origtoname) {
  2221. ast_copy_string(req->data.callinfo.originalCalledPartyName, origtoname, sizeof(req->data.callinfo.originalCalledPartyName));
  2222. }
  2223. if (origtonum) {
  2224. ast_copy_string(req->data.callinfo.originalCalledParty, origtonum, sizeof(req->data.callinfo.originalCalledParty));
  2225. }
  2226. req->data.callinfo.instance = htolel(instance);
  2227. req->data.callinfo.reference = htolel(callid);
  2228. req->data.callinfo.type = htolel(calldirection);
  2229. SKINNY_DEBUG(DEBUG_PACKET, 3, "Transmitting CALL_INFO_MESSAGE to %s, to %s(%s) from %s(%s), origto %s(%s) (dir=%d) on %s(%d)\n",
  2230. d->name, toname, tonum, fromname, fromnum, origtoname, origtonum, calldirection, d->name, instance);
  2231. transmit_response(d, req);
  2232. }
  2233. static void transmit_callinfo_variable(struct skinny_device *d, int instance, int callreference,
  2234. char *fromname, char *fromnum, char *toname, char *tonum, int calldirection, char *origtonum, char *origtoname)
  2235. {
  2236. struct skinny_req *req;
  2237. char *strptr;
  2238. char *thestrings[13];
  2239. int i;
  2240. int callinfostrleft = MAXCALLINFOSTR;
  2241. if (!(req = req_alloc(sizeof(struct call_info_message_variable), CALL_INFO_MESSAGE_VARIABLE)))
  2242. return;
  2243. req->data.callinfomessagevariable.instance = htolel(instance);
  2244. req->data.callinfomessagevariable.callreference = htolel(callreference);
  2245. req->data.callinfomessagevariable.calldirection = htolel(calldirection);
  2246. req->data.callinfomessagevariable.unknown1 = htolel(0x00);
  2247. req->data.callinfomessagevariable.unknown2 = htolel(0x00);
  2248. req->data.callinfomessagevariable.unknown3 = htolel(0x00);
  2249. req->data.callinfomessagevariable.unknown4 = htolel(0x00);
  2250. req->data.callinfomessagevariable.unknown5 = htolel(0x00);
  2251. thestrings[0] = fromnum;
  2252. thestrings[1] = ""; /* Appears to be origfrom */
  2253. if (calldirection == SKINNY_OUTGOING) {
  2254. thestrings[2] = tonum;
  2255. thestrings[3] = origtonum;
  2256. } else {
  2257. thestrings[2] = "";
  2258. thestrings[3] = "";
  2259. }
  2260. thestrings[4] = "";
  2261. thestrings[5] = "";
  2262. thestrings[6] = "";
  2263. thestrings[7] = "";
  2264. thestrings[8] = "";
  2265. thestrings[9] = fromname;
  2266. thestrings[10] = toname;
  2267. thestrings[11] = origtoname;
  2268. thestrings[12] = "";
  2269. strptr = req->data.callinfomessagevariable.calldetails;
  2270. for(i = 0; i < 13; i++) {
  2271. if (thestrings[i]) {
  2272. ast_copy_string(strptr, thestrings[i], callinfostrleft);
  2273. strptr += strlen(thestrings[i]) + 1;
  2274. callinfostrleft -= strlen(thestrings[i]) + 1;
  2275. } else {
  2276. ast_copy_string(strptr, "", callinfostrleft);
  2277. strptr++;
  2278. callinfostrleft--;
  2279. }
  2280. }
  2281. req->len = req->len - (callinfostrleft & ~0x3);
  2282. SKINNY_DEBUG(DEBUG_PACKET, 3, "Transmitting CALL_INFO_MESSAGE_VARIABLE to %s, to %s(%s) from %s(%s), origto %s(%s) (dir=%d) on %s(%d)\n",
  2283. d->name, toname, tonum, fromname, fromnum, origtoname, origtonum, calldirection, d->name, instance);
  2284. transmit_response(d, req);
  2285. }
  2286. static void send_callinfo(struct skinny_subchannel *sub)
  2287. {
  2288. struct ast_channel *ast;
  2289. struct skinny_device *d;
  2290. struct skinny_line *l;
  2291. struct ast_party_id connected_id;
  2292. char *fromname;
  2293. char *fromnum;
  2294. char *toname;
  2295. char *tonum;
  2296. if (!sub || !sub->owner || !sub->line || !sub->line->device) {
  2297. return;
  2298. }
  2299. ast = sub->owner;
  2300. l = sub->line;
  2301. d = l->device;
  2302. connected_id = ast_channel_connected_effective_id(ast);
  2303. if (sub->calldirection == SKINNY_INCOMING) {
  2304. if ((ast_party_id_presentation(&connected_id) & AST_PRES_RESTRICTION) == AST_PRES_ALLOWED) {
  2305. fromname = S_COR(connected_id.name.valid, connected_id.name.str, "");
  2306. fromnum = S_COR(connected_id.number.valid, connected_id.number.str, "");
  2307. } else {
  2308. fromname = "";
  2309. fromnum = "";
  2310. }
  2311. toname = S_COR(ast_channel_caller(ast)->id.name.valid, ast_channel_caller(ast)->id.name.str, "");
  2312. tonum = S_COR(ast_channel_caller(ast)->id.number.valid, ast_channel_caller(ast)->id.number.str, "");
  2313. } else if (sub->calldirection == SKINNY_OUTGOING) {
  2314. fromname = S_COR(ast_channel_caller(ast)->id.name.valid, ast_channel_caller(ast)->id.name.str, "");
  2315. fromnum = S_COR(ast_channel_caller(ast)->id.number.valid, ast_channel_caller(ast)->id.number.str, "");
  2316. toname = S_COR(ast_channel_connected(ast)->id.name.valid, ast_channel_connected(ast)->id.name.str, "");
  2317. tonum = S_COR(ast_channel_connected(ast)->id.number.valid, ast_channel_connected(ast)->id.number.str, l->lastnumberdialed);
  2318. } else {
  2319. ast_verb(1, "Error sending Callinfo to %s(%d) - No call direction in sub\n", d->name, l->instance);
  2320. return;
  2321. }
  2322. if (d->protocolversion < 17) {
  2323. transmit_callinfo(d, l->instance, sub->callid, fromname, fromnum, toname, tonum, sub->calldirection, sub->origtonum, sub->origtoname);
  2324. } else {
  2325. transmit_callinfo_variable(d, l->instance, sub->callid, fromname, fromnum, toname, tonum, sub->calldirection, sub->origtonum, sub->origtoname);
  2326. }
  2327. }
  2328. static void push_callinfo(struct skinny_subline *subline, struct skinny_subchannel *sub)
  2329. {
  2330. struct ast_channel *ast;
  2331. struct skinny_device *d;
  2332. struct skinny_line *l;
  2333. struct ast_party_id connected_id;
  2334. char *fromname;
  2335. char *fromnum;
  2336. char *toname;
  2337. char *tonum;
  2338. if (!sub || !sub->owner || !sub->line || !sub->line->device) {
  2339. return;
  2340. }
  2341. ast = sub->owner;
  2342. l = sub->line;
  2343. d = l->device;
  2344. connected_id = ast_channel_connected_effective_id(ast);
  2345. if (sub->calldirection == SKINNY_INCOMING) {
  2346. if((ast_party_id_presentation(&connected_id) & AST_PRES_RESTRICTION) == AST_PRES_ALLOWED) {
  2347. fromname = S_COR(connected_id.name.valid, connected_id.name.str, "");
  2348. fromnum = S_COR(connected_id.number.valid, connected_id.number.str, "");
  2349. } else {
  2350. fromname = "";
  2351. fromnum = "";
  2352. }
  2353. toname = S_COR(ast_channel_caller(ast)->id.name.valid, ast_channel_caller(ast)->id.name.str, "");
  2354. tonum = S_COR(ast_channel_caller(ast)->id.number.valid, ast_channel_caller(ast)->id.number.str, "");
  2355. } else if (sub->calldirection == SKINNY_OUTGOING) {
  2356. fromname = S_COR(ast_channel_caller(ast)->id.name.valid, ast_channel_caller(ast)->id.name.str, "");
  2357. fromnum = S_COR(ast_channel_caller(ast)->id.number.valid, ast_channel_caller(ast)->id.number.str, "");
  2358. toname = S_COR(ast_channel_connected(ast)->id.name.valid, ast_channel_connected(ast)->id.name.str, "");
  2359. tonum = S_COR(ast_channel_connected(ast)->id.number.valid, ast_channel_connected(ast)->id.number.str, l->lastnumberdialed);
  2360. } else {
  2361. ast_verb(1, "Error sending Callinfo to %s(%d) - No call direction in sub\n", d->name, l->instance);
  2362. return;
  2363. }
  2364. if (d->protocolversion < 17) {
  2365. transmit_callinfo(subline->line->device, subline->line->instance, subline->callid, fromname, fromnum, toname, tonum, sub->calldirection, sub->origtonum, sub->origtoname);
  2366. } else {
  2367. transmit_callinfo_variable(subline->line->device, subline->line->instance, subline->callid, fromname, fromnum, toname, tonum, sub->calldirection, sub->origtonum, sub->origtoname);
  2368. }
  2369. }
  2370. static void transmit_connect(struct skinny_device *d, struct skinny_subchannel *sub)
  2371. {
  2372. struct skinny_req *req;
  2373. struct skinny_line *l = sub->line;
  2374. struct ast_format *tmpfmt;
  2375. unsigned int framing;
  2376. if (!(req = req_alloc(sizeof(struct open_receive_channel_message), OPEN_RECEIVE_CHANNEL_MESSAGE)))
  2377. return;
  2378. tmpfmt = ast_format_cap_get_format(l->cap, 0);
  2379. framing = ast_format_cap_get_format_framing(l->cap, tmpfmt);
  2380. req->data.openreceivechannel.conferenceId = htolel(sub->callid);
  2381. req->data.openreceivechannel.partyId = htolel(sub->callid);
  2382. req->data.openreceivechannel.packets = htolel(framing);
  2383. req->data.openreceivechannel.capability = htolel(codec_ast2skinny(tmpfmt));
  2384. req->data.openreceivechannel.echo = htolel(0);
  2385. req->data.openreceivechannel.bitrate = htolel(0);
  2386. SKINNY_DEBUG(DEBUG_PACKET, 3, "Transmitting OPEN_RECEIVE_CHANNEL_MESSAGE to %s, confid %u, partyid %u, ms %u, fmt %d, echo %d, brate %d\n",
  2387. d->name, sub->callid, sub->callid, framing, codec_ast2skinny(tmpfmt), 0, 0);
  2388. ao2_ref(tmpfmt, -1);
  2389. transmit_response(d, req);
  2390. }
  2391. static void transmit_start_tone(struct skinny_device *d, int tone, int instance, int reference)
  2392. {
  2393. struct skinny_req *req;
  2394. if (!(req = req_alloc(sizeof(struct start_tone_message), START_TONE_MESSAGE)))
  2395. return;
  2396. req->data.starttone.tone = htolel(tone);
  2397. req->data.starttone.instance = htolel(instance);
  2398. req->data.starttone.reference = htolel(reference);
  2399. SKINNY_DEBUG(DEBUG_PACKET, 3, "Transmitting START_TONE_MESSAGE to %s, tone %d, inst %d, ref %d\n",
  2400. d->name, tone, instance, reference);
  2401. transmit_response(d, req);
  2402. }
  2403. static void transmit_stop_tone(struct skinny_device *d, int instance, int reference)
  2404. {
  2405. struct skinny_req *req;
  2406. if (!(req = req_alloc(sizeof(struct stop_tone_message), STOP_TONE_MESSAGE)))
  2407. return;
  2408. req->data.stoptone.instance = htolel(instance);
  2409. req->data.stoptone.reference = htolel(reference);
  2410. SKINNY_DEBUG(DEBUG_PACKET, 3, "Transmitting STOP_TONE_MESSAGE to %s, inst %d, ref %d\n",
  2411. d->name, instance, reference);
  2412. transmit_response(d, req);
  2413. }
  2414. static int keyset_translatebitmask(int keyset, int intmask)
  2415. {
  2416. int extmask = 0;
  2417. int x, y;
  2418. const struct soft_key_definitions *softkeymode = soft_key_default_definitions;
  2419. for(x = 0; x < ARRAY_LEN(soft_key_default_definitions); x++) {
  2420. if (softkeymode[x].mode == keyset) {
  2421. const uint8_t *defaults = softkeymode[x].defaults;
  2422. for (y = 0; y < softkeymode[x].count; y++) {
  2423. if (intmask & (1 << (defaults[y]))) {
  2424. extmask |= (1 << ((y)));
  2425. }
  2426. }
  2427. break;
  2428. }
  2429. }
  2430. return extmask;
  2431. }
  2432. static void transmit_selectsoftkeys(struct skinny_device *d, int instance, int callid, int softkey, int mask)
  2433. {
  2434. struct skinny_req *req;
  2435. int newmask;
  2436. if (!(req = req_alloc(sizeof(struct select_soft_keys_message), SELECT_SOFT_KEYS_MESSAGE)))
  2437. return;
  2438. newmask = keyset_translatebitmask(softkey, mask);
  2439. req->data.selectsoftkey.instance = htolel(instance);
  2440. req->data.selectsoftkey.reference = htolel(callid);
  2441. req->data.selectsoftkey.softKeySetIndex = htolel(softkey);
  2442. req->data.selectsoftkey.validKeyMask = htolel(newmask);
  2443. SKINNY_DEBUG(DEBUG_PACKET, 3, "Transmitting SELECT_SOFT_KEYS_MESSAGE to %s, inst %d, callid %d, softkey %d, mask 0x%08x\n",
  2444. d->name, instance, callid, softkey, (unsigned)newmask);
  2445. transmit_response(d, req);
  2446. }
  2447. static void transmit_lamp_indication(struct skinny_device *d, int stimulus, int instance, int indication)
  2448. {
  2449. struct skinny_req *req;
  2450. if (!(req = req_alloc(sizeof(struct set_lamp_message), SET_LAMP_MESSAGE)))
  2451. return;
  2452. req->data.setlamp.stimulus = htolel(stimulus);
  2453. req->data.setlamp.stimulusInstance = htolel(instance);
  2454. req->data.setlamp.deviceStimulus = htolel(indication);
  2455. SKINNY_DEBUG(DEBUG_PACKET, 3, "Transmitting SET_LAMP_MESSAGE to %s, stim %d, inst %d, ind %d\n",
  2456. d->name, stimulus, instance, indication);
  2457. transmit_response(d, req);
  2458. }
  2459. static void transmit_ringer_mode(struct skinny_device *d, int mode)
  2460. {
  2461. struct skinny_req *req;
  2462. if (!(req = req_alloc(sizeof(struct set_ringer_message), SET_RINGER_MESSAGE)))
  2463. return;
  2464. req->data.setringer.ringerMode = htolel(mode);
  2465. /* XXX okay, I don't quite know what this is, but here's what happens (on a 7960).
  2466. Note: The phone will always show as ringing on the display.
  2467. 1: phone will audibly ring over and over
  2468. 2: phone will audibly ring only once
  2469. any other value, will NOT cause the phone to audibly ring
  2470. */
  2471. req->data.setringer.unknown1 = htolel(1);
  2472. /* XXX the value here doesn't seem to change anything. Must be higher than 0.
  2473. Perhaps a packet capture can shed some light on this. */
  2474. req->data.setringer.unknown2 = htolel(1);
  2475. SKINNY_DEBUG(DEBUG_PACKET, 3, "Transmitting SET_RINGER_MESSAGE to %s, mode %d, unk1 1, unk2 1\n",
  2476. d->name, mode);
  2477. transmit_response(d, req);
  2478. }
  2479. static void transmit_clear_display_message(struct skinny_device *d, int instance, int reference)
  2480. {
  2481. struct skinny_req *req;
  2482. if (!(req = req_alloc(sizeof(struct clear_display_message), CLEAR_DISPLAY_MESSAGE)))
  2483. return;
  2484. //what do we want hear CLEAR_DISPLAY_MESSAGE or CLEAR_PROMPT_STATUS???
  2485. //if we are clearing the display, it appears there is no instance and refernece info (size 0)
  2486. //req->data.clearpromptstatus.lineInstance = instance;
  2487. //req->data.clearpromptstatus.callReference = reference;
  2488. SKINNY_DEBUG(DEBUG_PACKET, 3, "Transmitting CLEAR_DISPLAY_MESSAGE to %s\n", d->name);
  2489. transmit_response(d, req);
  2490. }
  2491. /* This function is not currently used, but will be (wedhorn)*/
  2492. /* static void transmit_display_message(struct skinny_device *d, const char *text, int instance, int reference)
  2493. {
  2494. struct skinny_req *req;
  2495. if (text == 0) {
  2496. ast_verb(1, "Bug, Asked to display empty message\n");
  2497. return;
  2498. }
  2499. if (!(req = req_alloc(sizeof(struct displaytext_message), DISPLAYTEXT_MESSAGE)))
  2500. return;
  2501. ast_copy_string(req->data.displaytext.text, text, sizeof(req->data.displaytext.text));
  2502. SKINNY_DEBUG(DEBUG_PACKET, 3, "Transmitting DISPLAYTEXT_MESSAGE to %s, text %s\n", d->name, text);
  2503. transmit_response(d, req);
  2504. } */
  2505. static void transmit_displaynotify(struct skinny_device *d, const char *text, int t)
  2506. {
  2507. struct skinny_req *req;
  2508. if (!(req = req_alloc(sizeof(struct display_notify_message), DISPLAY_NOTIFY_MESSAGE)))
  2509. return;
  2510. ast_copy_string(req->data.displaynotify.displayMessage, text, sizeof(req->data.displaynotify.displayMessage));
  2511. req->data.displaynotify.displayTimeout = htolel(t);
  2512. SKINNY_DEBUG(DEBUG_PACKET, 3, "Transmitting DISPLAY_NOTIFY_MESSAGE to %s, text %s\n", d->name, text);
  2513. transmit_response(d, req);
  2514. }
  2515. static void transmit_clearprinotify(struct skinny_device *d, int priority)
  2516. {
  2517. struct skinny_req *req;
  2518. if (!(req = req_alloc(sizeof(struct clear_prinotify_message), CLEAR_PRINOTIFY_MESSAGE)))
  2519. return;
  2520. req->data.clearprinotify.priority = htolel(priority);
  2521. SKINNY_DEBUG(DEBUG_PACKET, 3, "Transmitting CLEAR_PRINOTIFY_MESSAGE to %s, priority %d\n", d->name, priority);
  2522. transmit_response(d, req);
  2523. }
  2524. static void _transmit_displayprinotify(struct skinny_device *d, const char *text, const char *extratext, int timeout, int priority)
  2525. {
  2526. struct skinny_req *req;
  2527. if (!(req = req_alloc(sizeof(struct display_prinotify_message), DISPLAY_PRINOTIFY_MESSAGE)))
  2528. return;
  2529. req->data.displayprinotify.timeout = htolel(timeout);
  2530. req->data.displayprinotify.priority = htolel(priority);
  2531. if ((char)*text == '\200') {
  2532. int octalstrlen = strlen(text);
  2533. ast_copy_string(req->data.displayprinotify.text, text, sizeof(req->data.displayprinotify.text));
  2534. ast_copy_string(req->data.displayprinotify.text+octalstrlen, extratext, sizeof(req->data.displayprinotify.text)-octalstrlen);
  2535. SKINNY_DEBUG(DEBUG_PACKET, 3, "Transmitting DISPLAY_PRINOTIFY_MESSAGE to %s, '\\%03o\\%03o', '%s', timeout=%d, priority=%d\n",
  2536. d->name, (unsigned)*text, (unsigned)*(text+1), extratext, timeout, priority);
  2537. } else {
  2538. ast_copy_string(req->data.displayprinotify.text, text, sizeof(req->data.displayprinotify.text));
  2539. SKINNY_DEBUG(DEBUG_PACKET, 3, "Transmitting DISPLAY_PRINOTIFY_MESSAGE to %s, '%s', timeout=%d, priority=%d\n",
  2540. d->name, text, timeout, priority);
  2541. }
  2542. transmit_response(d, req);
  2543. }
  2544. static void _transmit_displayprinotifyvar(struct skinny_device *d, const char *text, const char *extratext, int timeout, int priority)
  2545. {
  2546. struct skinny_req *req;
  2547. int packetlen;
  2548. if (!(req = req_alloc(sizeof(struct display_prinotify_message_variable), DISPLAY_PRINOTIFY_MESSAGE_VARIABLE)))
  2549. return;
  2550. req->data.displayprinotifyvar.timeout = htolel(timeout);
  2551. req->data.displayprinotifyvar.priority = htolel(priority);
  2552. if ((char)*text == '\200') {
  2553. int octalstrlen = strlen(text);
  2554. ast_copy_string(req->data.displayprinotifyvar.text, text, sizeof(req->data.displayprinotifyvar.text));
  2555. ast_copy_string(req->data.displayprinotifyvar.text+octalstrlen, extratext, sizeof(req->data.displayprinotifyvar.text)-octalstrlen);
  2556. packetlen = req->len - MAXDISPLAYNOTIFYSTR + strlen(text) + strlen(extratext);
  2557. SKINNY_DEBUG(DEBUG_PACKET, 3, "Transmitting DISPLAY_PRINOTIFY_MESSAGE_VARIABLE to %s, '\\%03o\\%03o', '%s', timeout=%d, priority=%d\n",
  2558. d->name, (unsigned)*text, (unsigned)*(text+1), extratext, timeout, priority);
  2559. } else {
  2560. ast_copy_string(req->data.displayprinotifyvar.text, text, sizeof(req->data.displayprinotifyvar.text));
  2561. packetlen = req->len - MAXDISPLAYNOTIFYSTR + strlen(text);
  2562. SKINNY_DEBUG(DEBUG_PACKET, 3, "Transmitting DISPLAY_PRINOTIFY_MESSAGE_VARIABLE to %s, '%s', timeout=%d, priority=%d\n",
  2563. d->name, text, timeout, priority);
  2564. }
  2565. req->len = (packetlen & ~0x3) + 4;
  2566. transmit_response(d, req);
  2567. }
  2568. static void send_displayprinotify(struct skinny_device *d, const char *text, const char *extratext, int timeout, int priority)
  2569. {
  2570. if (d->protocolversion < 17) {
  2571. _transmit_displayprinotify(d, text, extratext, timeout, priority);
  2572. } else {
  2573. _transmit_displayprinotifyvar(d, text, extratext, timeout, priority);
  2574. }
  2575. }
  2576. static void transmit_displaypromptstatus(struct skinny_device *d, const char *text, const char *extratext, int t, int instance, int callid)
  2577. {
  2578. struct skinny_req *req;
  2579. if (!(req = req_alloc(sizeof(struct display_prompt_status_message), DISPLAY_PROMPT_STATUS_MESSAGE)))
  2580. return;
  2581. req->data.displaypromptstatus.messageTimeout = htolel(t);
  2582. req->data.displaypromptstatus.lineInstance = htolel(instance);
  2583. req->data.displaypromptstatus.callReference = htolel(callid);
  2584. if ((char)*text == '\200') {
  2585. int octalstrlen = strlen(text);
  2586. ast_copy_string(req->data.displaypromptstatus.promptMessage, text, sizeof(req->data.displaypromptstatusvar.promptMessage));
  2587. ast_copy_string(req->data.displaypromptstatus.promptMessage+octalstrlen, extratext, sizeof(req->data.displaypromptstatus.promptMessage)-octalstrlen);
  2588. SKINNY_DEBUG(DEBUG_PACKET, 3, "Transmitting DISPLAY_PROMPT_STATUS_MESSAGE to %s, '\\%03o\\%03o', '%s'\n",
  2589. d->name, (unsigned)*text, (unsigned)*(text+1), extratext);
  2590. } else {
  2591. ast_copy_string(req->data.displaypromptstatus.promptMessage, text, sizeof(req->data.displaypromptstatus.promptMessage));
  2592. SKINNY_DEBUG(DEBUG_PACKET, 3, "Transmitting DISPLAY_PROMPT_STATUS_MESSAGE to %s, '%s'\n",
  2593. d->name, text);
  2594. }
  2595. transmit_response(d, req);
  2596. }
  2597. static void transmit_displaypromptstatusvar(struct skinny_device *d, const char *text, const char *extratext, int t, int instance, int callid)
  2598. {
  2599. struct skinny_req *req;
  2600. int packetlen;
  2601. if (!(req = req_alloc(sizeof(struct display_prompt_status_message_variable), DISPLAY_PROMPT_STATUS_MESSAGE_VARIABLE)))
  2602. return;
  2603. req->data.displaypromptstatusvar.lineInstance = htolel(instance);
  2604. req->data.displaypromptstatusvar.callReference = htolel(callid);
  2605. if ((char)*text == '\200') {
  2606. int octalstrlen = strlen(text);
  2607. ast_copy_string(req->data.displaypromptstatusvar.promptMessage, text, sizeof(req->data.displaypromptstatusvar.promptMessage));
  2608. ast_copy_string(req->data.displaypromptstatusvar.promptMessage+octalstrlen, extratext, sizeof(req->data.displaypromptstatusvar.promptMessage)-octalstrlen);
  2609. packetlen = req->len - MAXCALLINFOSTR + strlen(text) + strlen(extratext);
  2610. SKINNY_DEBUG(DEBUG_PACKET, 3, "Transmitting DISPLAY_PROMPT_STATUS_MESSAGE_VARIABLE to %s, '\\%03o\\%03o', '%s'\n",
  2611. d->name, (unsigned)*text, (unsigned)*(text+1), extratext);
  2612. } else {
  2613. ast_copy_string(req->data.displaypromptstatusvar.promptMessage, text, sizeof(req->data.displaypromptstatus.promptMessage));
  2614. packetlen = req->len - MAXCALLINFOSTR + strlen(text);
  2615. SKINNY_DEBUG(DEBUG_PACKET, 3, "Transmitting DISPLAY_PROMPT_STATUS_MESSAGE_VARIABLE to %s, '%s'\n",
  2616. d->name, text);
  2617. }
  2618. req->len = (packetlen & ~0x3) + 4;
  2619. transmit_response(d, req);
  2620. }
  2621. static void send_displaypromptstatus(struct skinny_device *d, const char *text, const char *extratext, int t, int instance, int callid)
  2622. {
  2623. if (d->protocolversion < 17) {
  2624. transmit_displaypromptstatus(d, text, extratext, t, instance, callid);
  2625. } else {
  2626. transmit_displaypromptstatusvar(d, text, extratext, t, instance, callid);
  2627. }
  2628. }
  2629. static void transmit_clearpromptmessage(struct skinny_device *d, int instance, int callid)
  2630. {
  2631. struct skinny_req *req;
  2632. if (!(req = req_alloc(sizeof(struct clear_prompt_message), CLEAR_PROMPT_MESSAGE)))
  2633. return;
  2634. req->data.clearpromptstatus.lineInstance = htolel(instance);
  2635. req->data.clearpromptstatus.callReference = htolel(callid);
  2636. SKINNY_DEBUG(DEBUG_PACKET, 3, "Transmitting CLEAR_PROMPT_MESSAGE to %s, inst %d, callid %d\n",
  2637. d->name, instance, callid);
  2638. transmit_response(d, req);
  2639. }
  2640. static void transmit_dialednumber(struct skinny_device *d, const char *text, int instance, int callid)
  2641. {
  2642. struct skinny_req *req;
  2643. if (!(req = req_alloc(sizeof(struct dialed_number_message), DIALED_NUMBER_MESSAGE)))
  2644. return;
  2645. ast_copy_string(req->data.dialednumber.dialedNumber, text, sizeof(req->data.dialednumber.dialedNumber));
  2646. req->data.dialednumber.lineInstance = htolel(instance);
  2647. req->data.dialednumber.callReference = htolel(callid);
  2648. SKINNY_DEBUG(DEBUG_PACKET, 3, "Transmitting DIALED_NUMBER_MESSAGE to %s, num %s, inst %d, callid %d\n",
  2649. d->name, text, instance, callid);
  2650. transmit_response(d, req);
  2651. }
  2652. static void transmit_closereceivechannel(struct skinny_device *d, struct skinny_subchannel *sub)
  2653. {
  2654. struct skinny_req *req;
  2655. if (!(req = req_alloc(sizeof(struct close_receive_channel_message), CLOSE_RECEIVE_CHANNEL_MESSAGE)))
  2656. return;
  2657. req->data.closereceivechannel.conferenceId = htolel(0);
  2658. req->data.closereceivechannel.partyId = htolel(sub->callid);
  2659. SKINNY_DEBUG(DEBUG_PACKET, 3, "Transmitting CLOSE_RECEIVE_CHANNEL_MESSAGE to %s, confid %d, callid %u\n",
  2660. d->name, 0, sub->callid);
  2661. transmit_response(d, req);
  2662. }
  2663. static void transmit_stopmediatransmission(struct skinny_device *d, struct skinny_subchannel *sub)
  2664. {
  2665. struct skinny_req *req;
  2666. if (!(req = req_alloc(sizeof(struct stop_media_transmission_message), STOP_MEDIA_TRANSMISSION_MESSAGE)))
  2667. return;
  2668. req->data.stopmedia.conferenceId = htolel(0);
  2669. req->data.stopmedia.passThruPartyId = htolel(sub->callid);
  2670. SKINNY_DEBUG(DEBUG_PACKET, 3, "Transmitting STOP_MEDIA_TRANSMISSION_MESSAGE to %s, confid %d, passthrupartyid %u\n",
  2671. d->name, 0, sub->callid);
  2672. transmit_response(d, req);
  2673. }
  2674. static void transmit_startmediatransmission(struct skinny_device *d, struct skinny_subchannel *sub, struct sockaddr_in dest,
  2675. struct ast_format *format, unsigned int framing)
  2676. {
  2677. struct skinny_req *req;
  2678. if (d->protocolversion < 17) {
  2679. if (!(req = req_alloc(sizeof(struct start_media_transmission_message_ip4), START_MEDIA_TRANSMISSION_MESSAGE)))
  2680. return;
  2681. req->data.startmedia_ip4.conferenceId = htolel(sub->callid);
  2682. req->data.startmedia_ip4.passThruPartyId = htolel(sub->callid);
  2683. req->data.startmedia_ip4.remoteIp = dest.sin_addr.s_addr;
  2684. req->data.startmedia_ip4.remotePort = htolel(ntohs(dest.sin_port));
  2685. req->data.startmedia_ip4.packetSize = htolel(framing);
  2686. req->data.startmedia_ip4.payloadType = htolel(codec_ast2skinny(format));
  2687. req->data.startmedia_ip4.qualifier.precedence = htolel(127);
  2688. req->data.startmedia_ip4.qualifier.vad = htolel(0);
  2689. req->data.startmedia_ip4.qualifier.packets = htolel(0);
  2690. req->data.startmedia_ip4.qualifier.bitRate = htolel(0);
  2691. } else {
  2692. if (!(req = req_alloc(sizeof(struct start_media_transmission_message_ip6), START_MEDIA_TRANSMISSION_MESSAGE)))
  2693. return;
  2694. req->data.startmedia_ip6.conferenceId = htolel(sub->callid);
  2695. req->data.startmedia_ip6.passThruPartyId = htolel(sub->callid);
  2696. memcpy(req->data.startmedia_ip6.remoteIp, &dest.sin_addr.s_addr, sizeof(dest.sin_addr.s_addr));
  2697. req->data.startmedia_ip6.remotePort = htolel(ntohs(dest.sin_port));
  2698. req->data.startmedia_ip6.packetSize = htolel(framing);
  2699. req->data.startmedia_ip6.payloadType = htolel(codec_ast2skinny(format));
  2700. req->data.startmedia_ip6.qualifier.precedence = htolel(127);
  2701. req->data.startmedia_ip6.qualifier.vad = htolel(0);
  2702. req->data.startmedia_ip6.qualifier.packets = htolel(0);
  2703. req->data.startmedia_ip6.qualifier.bitRate = htolel(0);
  2704. }
  2705. SKINNY_DEBUG(DEBUG_PACKET, 3, "Transmitting START_MEDIA_TRANSMISSION_MESSAGE to %s, callid %u, passthrupartyid %u, ip %s:%d, ms %u, fmt %d, prec 127\n",
  2706. d->name, sub->callid, sub->callid, ast_inet_ntoa(dest.sin_addr), dest.sin_port, framing, codec_ast2skinny(format));
  2707. transmit_response(d, req);
  2708. }
  2709. static void transmit_activatecallplane(struct skinny_device *d, struct skinny_line *l)
  2710. {
  2711. struct skinny_req *req;
  2712. if (!(req = req_alloc(sizeof(struct activate_call_plane_message), ACTIVATE_CALL_PLANE_MESSAGE)))
  2713. return;
  2714. req->data.activatecallplane.lineInstance = htolel(l->instance);
  2715. SKINNY_DEBUG(DEBUG_PACKET, 3, "Transmitting ACTIVATE_CALL_PLANE_MESSAGE to %s, inst %d\n",
  2716. d->name, l->instance);
  2717. transmit_response(d, req);
  2718. }
  2719. static void transmit_callstate(struct skinny_device *d, int buttonInstance, unsigned callid, int state)
  2720. {
  2721. struct skinny_req *req;
  2722. if (!(req = req_alloc(sizeof(struct call_state_message), CALL_STATE_MESSAGE)))
  2723. return;
  2724. req->data.callstate.callState = htolel(state);
  2725. req->data.callstate.lineInstance = htolel(buttonInstance);
  2726. req->data.callstate.callReference = htolel(callid);
  2727. SKINNY_DEBUG(DEBUG_PACKET, 3, "Transmitting CALL_STATE_MESSAGE to %s, state %s, inst %d, callid %u\n",
  2728. d->name, callstate2str(state), buttonInstance, callid);
  2729. transmit_response(d, req);
  2730. }
  2731. static void transmit_cfwdstate(struct skinny_device *d, struct skinny_line *l)
  2732. {
  2733. struct skinny_req *req;
  2734. int anyon = 0;
  2735. if (!(req = req_alloc(sizeof(struct forward_stat_message), FORWARD_STAT_MESSAGE)))
  2736. return;
  2737. if (l->cfwdtype & SKINNY_CFWD_ALL) {
  2738. if (!ast_strlen_zero(l->call_forward_all)) {
  2739. ast_copy_string(req->data.forwardstat.fwdallnum, l->call_forward_all, sizeof(req->data.forwardstat.fwdallnum));
  2740. req->data.forwardstat.fwdall = htolel(1);
  2741. anyon++;
  2742. } else {
  2743. req->data.forwardstat.fwdall = htolel(0);
  2744. }
  2745. }
  2746. if (l->cfwdtype & SKINNY_CFWD_BUSY) {
  2747. if (!ast_strlen_zero(l->call_forward_busy)) {
  2748. ast_copy_string(req->data.forwardstat.fwdbusynum, l->call_forward_busy, sizeof(req->data.forwardstat.fwdbusynum));
  2749. req->data.forwardstat.fwdbusy = htolel(1);
  2750. anyon++;
  2751. } else {
  2752. req->data.forwardstat.fwdbusy = htolel(0);
  2753. }
  2754. }
  2755. if (l->cfwdtype & SKINNY_CFWD_NOANSWER) {
  2756. if (!ast_strlen_zero(l->call_forward_noanswer)) {
  2757. ast_copy_string(req->data.forwardstat.fwdnoanswernum, l->call_forward_noanswer, sizeof(req->data.forwardstat.fwdnoanswernum));
  2758. req->data.forwardstat.fwdnoanswer = htolel(1);
  2759. anyon++;
  2760. } else {
  2761. req->data.forwardstat.fwdnoanswer = htolel(0);
  2762. }
  2763. }
  2764. req->data.forwardstat.lineNumber = htolel(l->instance);
  2765. if (anyon)
  2766. req->data.forwardstat.activeforward = htolel(7);
  2767. else
  2768. req->data.forwardstat.activeforward = htolel(0);
  2769. SKINNY_DEBUG(DEBUG_PACKET, 3, "Transmitting FORWARD_STAT_MESSAGE to %s, inst %d, all %s, busy %s, noans %s, acitve %d\n",
  2770. d->name, l->instance, l->call_forward_all, l->call_forward_busy, l->call_forward_noanswer, anyon ? 7 : 0);
  2771. transmit_response(d, req);
  2772. }
  2773. static void transmit_speeddialstatres(struct skinny_device *d, struct skinny_speeddial *sd)
  2774. {
  2775. struct skinny_req *req;
  2776. if (!(req = req_alloc(sizeof(struct speed_dial_stat_res_message), SPEED_DIAL_STAT_RES_MESSAGE)))
  2777. return;
  2778. req->data.speeddialreq.speedDialNumber = htolel(sd->instance);
  2779. ast_copy_string(req->data.speeddial.speedDialDirNumber, sd->exten, sizeof(req->data.speeddial.speedDialDirNumber));
  2780. ast_copy_string(req->data.speeddial.speedDialDisplayName, sd->label, sizeof(req->data.speeddial.speedDialDisplayName));
  2781. SKINNY_DEBUG(DEBUG_PACKET, 3, "Transmitting SPEED_DIAL_STAT_RES_MESSAGE to %s, inst %d, dir %s, display %s\n",
  2782. d->name, sd->instance, sd->exten, sd->label);
  2783. transmit_response(d, req);
  2784. }
  2785. //static void transmit_linestatres(struct skinny_device *d, struct skinny_line *l)
  2786. static void transmit_linestatres(struct skinny_device *d, int instance)
  2787. {
  2788. struct skinny_req *req;
  2789. struct skinny_line *l;
  2790. struct skinny_speeddial *sd;
  2791. if (!(req = req_alloc(sizeof(struct line_stat_res_message), LINE_STAT_RES_MESSAGE)))
  2792. return;
  2793. if ((l = find_line_by_instance(d, instance))) {
  2794. req->data.linestat.lineNumber = letohl(l->instance);
  2795. memcpy(req->data.linestat.lineDirNumber, l->name, sizeof(req->data.linestat.lineDirNumber));
  2796. memcpy(req->data.linestat.lineDisplayName, l->label, sizeof(req->data.linestat.lineDisplayName));
  2797. } else if ((sd = find_speeddial_by_instance(d, instance, 1))) {
  2798. req->data.linestat.lineNumber = letohl(sd->instance);
  2799. memcpy(req->data.linestat.lineDirNumber, sd->label, sizeof(req->data.linestat.lineDirNumber));
  2800. memcpy(req->data.linestat.lineDisplayName, sd->label, sizeof(req->data.linestat.lineDisplayName));
  2801. }
  2802. SKINNY_DEBUG(DEBUG_PACKET, 3, "Transmitting LINE_STAT_RES_MESSAGE to %s, inst %d, num %s, label %s\n",
  2803. d->name, l->instance, req->data.linestat.lineDirNumber, req->data.linestat.lineDisplayName);
  2804. transmit_response(d, req);
  2805. }
  2806. static void transmit_definetimedate(struct skinny_device *d)
  2807. {
  2808. struct skinny_req *req;
  2809. struct timeval now = ast_tvnow();
  2810. struct ast_tm cmtime;
  2811. if (!(req = req_alloc(sizeof(struct definetimedate_message), DEFINETIMEDATE_MESSAGE)))
  2812. return;
  2813. ast_localtime(&now, &cmtime, NULL);
  2814. req->data.definetimedate.year = htolel(cmtime.tm_year+1900);
  2815. req->data.definetimedate.month = htolel(cmtime.tm_mon+1);
  2816. req->data.definetimedate.dayofweek = htolel(cmtime.tm_wday);
  2817. req->data.definetimedate.day = htolel(cmtime.tm_mday);
  2818. req->data.definetimedate.hour = htolel(cmtime.tm_hour);
  2819. req->data.definetimedate.minute = htolel(cmtime.tm_min);
  2820. req->data.definetimedate.seconds = htolel(cmtime.tm_sec);
  2821. req->data.definetimedate.milliseconds = htolel(cmtime.tm_usec / 1000);
  2822. req->data.definetimedate.timestamp = htolel(now.tv_sec);
  2823. SKINNY_DEBUG(DEBUG_PACKET, 3, "Transmitting DEFINETIMEDATE_MESSAGE to %s, date %u %u %u dow %u time %u:%u:%u.%u\n",
  2824. d->name, req->data.definetimedate.year, req->data.definetimedate.month, req->data.definetimedate.day, req->data.definetimedate.dayofweek,
  2825. req->data.definetimedate.hour, req->data.definetimedate.minute, req->data.definetimedate.seconds, req->data.definetimedate.milliseconds);
  2826. transmit_response(d, req);
  2827. }
  2828. static void transmit_versionres(struct skinny_device *d)
  2829. {
  2830. struct skinny_req *req;
  2831. if (!(req = req_alloc(sizeof(struct version_res_message), VERSION_RES_MESSAGE)))
  2832. return;
  2833. ast_copy_string(req->data.version.version, d->version_id, sizeof(req->data.version.version));
  2834. SKINNY_DEBUG(DEBUG_PACKET, 3, "Transmitting VERSION_RES_MESSAGE to %s, version %s\n", d->name, d->version_id);
  2835. transmit_response(d, req);
  2836. }
  2837. static void transmit_serverres(struct skinny_device *d)
  2838. {
  2839. struct skinny_req *req;
  2840. if (!(req = req_alloc(sizeof(struct server_res_message), SERVER_RES_MESSAGE)))
  2841. return;
  2842. memcpy(req->data.serverres.server[0].serverName, ourhost,
  2843. sizeof(req->data.serverres.server[0].serverName));
  2844. req->data.serverres.serverListenPort[0] = htolel(ourport);
  2845. req->data.serverres.serverIpAddr[0] = htolel(d->ourip.s_addr);
  2846. SKINNY_DEBUG(DEBUG_PACKET, 3, "Transmitting SERVER_RES_MESSAGE to %s, srvname %s %s:%d\n",
  2847. d->name, ourhost, ast_inet_ntoa(d->ourip), ourport);
  2848. transmit_response(d, req);
  2849. }
  2850. static void transmit_softkeysetres(struct skinny_device *d)
  2851. {
  2852. struct skinny_req *req;
  2853. int i;
  2854. int x;
  2855. int y;
  2856. int keydefcount;
  2857. const struct soft_key_definitions *softkeymode = soft_key_default_definitions;
  2858. if (!(req = req_alloc(sizeof(struct soft_key_set_res_message), SOFT_KEY_SET_RES_MESSAGE)))
  2859. return;
  2860. SKINNY_DEBUG(DEBUG_TEMPLATE, 3, "Creating Softkey Template\n");
  2861. keydefcount = ARRAY_LEN(soft_key_default_definitions);
  2862. req->data.softkeysets.softKeySetOffset = htolel(0);
  2863. req->data.softkeysets.softKeySetCount = htolel(keydefcount);
  2864. req->data.softkeysets.totalSoftKeySetCount = htolel(keydefcount);
  2865. for (x = 0; x < keydefcount; x++) {
  2866. const uint8_t *defaults = softkeymode->defaults;
  2867. /* XXX I wanted to get the size of the array dynamically, but that wasn't wanting to work.
  2868. This will have to do for now. */
  2869. for (y = 0; y < softkeymode->count; y++) {
  2870. for (i = 0; i < (sizeof(soft_key_template_default) / sizeof(struct soft_key_template_definition)); i++) {
  2871. if (defaults[y] == i+1) {
  2872. req->data.softkeysets.softKeySetDefinition[softkeymode->mode].softKeyTemplateIndex[y] = (i+1);
  2873. req->data.softkeysets.softKeySetDefinition[softkeymode->mode].softKeyInfoIndex[y] = htoles(i+301);
  2874. SKINNY_DEBUG(DEBUG_TEMPLATE, 4, "softKeySetDefinition : softKeyTemplateIndex: %d softKeyInfoIndex: %d\n",
  2875. i+1, i+301);
  2876. }
  2877. }
  2878. }
  2879. softkeymode++;
  2880. }
  2881. SKINNY_DEBUG(DEBUG_PACKET | DEBUG_TEMPLATE, 3, "Transmitting SOFT_KEY_SET_RES_MESSAGE to %s, template data\n",
  2882. d->name);
  2883. transmit_response(d, req);
  2884. }
  2885. static void transmit_softkeytemplateres(struct skinny_device *d)
  2886. {
  2887. struct skinny_req *req;
  2888. if (!(req = req_alloc(sizeof(struct soft_key_template_res_message), SOFT_KEY_TEMPLATE_RES_MESSAGE)))
  2889. return;
  2890. req->data.softkeytemplate.softKeyOffset = htolel(0);
  2891. req->data.softkeytemplate.softKeyCount = htolel(sizeof(soft_key_template_default) / sizeof(struct soft_key_template_definition));
  2892. req->data.softkeytemplate.totalSoftKeyCount = htolel(sizeof(soft_key_template_default) / sizeof(struct soft_key_template_definition));
  2893. memcpy(req->data.softkeytemplate.softKeyTemplateDefinition,
  2894. soft_key_template_default,
  2895. sizeof(soft_key_template_default));
  2896. SKINNY_DEBUG(DEBUG_PACKET, 3, "Transmitting SOFT_KEY_TEMPLATE_RES_MESSAGE to %s, offset 0, keycnt %u, totalkeycnt %u, template data\n",
  2897. d->name, req->data.softkeytemplate.softKeyCount, req->data.softkeytemplate.totalSoftKeyCount);
  2898. transmit_response(d, req);
  2899. }
  2900. static void transmit_reset(struct skinny_device *d, int fullrestart)
  2901. {
  2902. struct skinny_req *req;
  2903. if (!(req = req_alloc(sizeof(struct reset_message), RESET_MESSAGE)))
  2904. return;
  2905. if (fullrestart)
  2906. req->data.reset.resetType = 2;
  2907. else
  2908. req->data.reset.resetType = 1;
  2909. SKINNY_DEBUG(DEBUG_PACKET, 3, "Transmitting RESET_MESSAGE to %s, type %s\n",
  2910. d->name, (fullrestart) ? "Restarting" : "Resetting");
  2911. transmit_response(d, req);
  2912. }
  2913. static void transmit_keepaliveack(struct skinnysession *s)
  2914. {
  2915. struct skinny_req *req;
  2916. if (!(req = req_alloc(0, KEEP_ALIVE_ACK_MESSAGE)))
  2917. return;
  2918. #ifdef AST_DEVMODE
  2919. {
  2920. struct skinny_device *d = s->device;
  2921. SKINNY_DEBUG(DEBUG_PACKET, 3, "Transmitting KEEP_ALIVE_ACK_MESSAGE to %s\n", (d ? d->name : "unregistered"));
  2922. }
  2923. #endif
  2924. transmit_response_bysession(s, req);
  2925. }
  2926. static void transmit_registerack(struct skinny_device *d)
  2927. {
  2928. struct skinny_req *req;
  2929. if (!(req = req_alloc(sizeof(struct register_ack_message), REGISTER_ACK_MESSAGE)))
  2930. return;
  2931. req->data.regack.res[0] = '0';
  2932. req->data.regack.res[1] = '\0';
  2933. req->data.regack.keepAlive = htolel(keep_alive);
  2934. memcpy(req->data.regack.dateTemplate, date_format, sizeof(req->data.regack.dateTemplate));
  2935. req->data.regack.res2[0] = '0';
  2936. req->data.regack.res2[1] = '\0';
  2937. req->data.regack.secondaryKeepAlive = htolel(keep_alive);
  2938. #ifdef AST_DEVMODE
  2939. {
  2940. short res = req->data.regack.res[0] << 8 | req->data.regack.res[1];
  2941. unsigned int res2 = req->data.regack.res2[0] << 24 | req->data.regack.res2[1] << 16 | req->data.regack.res2[2] << 8 | req->data.regack.res2[3];
  2942. SKINNY_DEBUG(DEBUG_PACKET, 3, "Transmitting REGISTER_ACK_MESSAGE to %s, keepalive %d, datetemplate %s, seckeepalive %d, res 0x%04x, res2 0x%08x\n",
  2943. d->name, keep_alive, date_format, keep_alive, (unsigned)res, res2);
  2944. }
  2945. #endif
  2946. transmit_response(d, req);
  2947. }
  2948. static void transmit_capabilitiesreq(struct skinny_device *d)
  2949. {
  2950. struct skinny_req *req;
  2951. if (!(req = req_alloc(0, CAPABILITIES_REQ_MESSAGE)))
  2952. return;
  2953. SKINNY_DEBUG(DEBUG_PACKET, 3, "Transmitting CAPABILITIES_REQ_MESSAGE to %s\n", d->name);
  2954. transmit_response(d, req);
  2955. }
  2956. static void transmit_backspace(struct skinny_device *d, int instance, unsigned callid)
  2957. {
  2958. struct skinny_req *req;
  2959. if (!(req = req_alloc(sizeof(struct bksp_req_message), BKSP_REQ_MESSAGE)))
  2960. return;
  2961. req->data.bkspmessage.instance = htolel(instance);
  2962. req->data.bkspmessage.callreference = htolel(callid);
  2963. SKINNY_DEBUG(DEBUG_PACKET, 3, "Transmitting BKSP_REQ_MESSAGE to %s, inst %d, callid %u \n",
  2964. d->name, instance, callid);
  2965. transmit_response(d, req);
  2966. }
  2967. static void transmit_serviceurlstat(struct skinny_device *d, int instance)
  2968. {
  2969. struct skinny_req *req;
  2970. struct skinny_serviceurl *surl;
  2971. if (!(req = req_alloc(sizeof(struct serviceurl_stat_message), SERVICEURL_STAT_MESSAGE)))
  2972. return;
  2973. AST_LIST_TRAVERSE(&d->serviceurls, surl, list) {
  2974. if (surl->instance == instance) {
  2975. break;
  2976. }
  2977. }
  2978. if (surl) {
  2979. memcpy(req->data.serviceurlmessage.displayName, surl->displayName, sizeof(req->data.serviceurlmessage.displayName));
  2980. memcpy(req->data.serviceurlmessage.url, surl->url, sizeof(req->data.serviceurlmessage.url));
  2981. }
  2982. req->data.serviceurlmessage.instance = htolel(instance);
  2983. SKINNY_DEBUG(DEBUG_PACKET, 3, "Transmitting SERVICEURL_STAT_MESSAGE to %s, inst %d\n",
  2984. d->name, instance);
  2985. transmit_response(d, req);
  2986. }
  2987. static int skinny_extensionstate_cb(char *context, char *exten, struct ast_state_cb_info *info, void *data)
  2988. {
  2989. struct skinny_container *container = data;
  2990. struct skinny_device *d = NULL;
  2991. char hint[AST_MAX_EXTENSION];
  2992. int state = info->exten_state;
  2993. /* only interested in device state here */
  2994. if (info->reason != AST_HINT_UPDATE_DEVICE) {
  2995. return 0;
  2996. }
  2997. if (container->type == SKINNY_SDCONTAINER) {
  2998. struct skinny_speeddial *sd = container->data;
  2999. d = sd->parent;
  3000. SKINNY_DEBUG(DEBUG_HINT, 3, "Got hint %s on speeddial %s\n", ast_extension_state2str(state), sd->label);
  3001. if (ast_get_hint(hint, sizeof(hint), NULL, 0, NULL, sd->context, sd->exten)) {
  3002. /* If they are not registered, we will override notification and show no availability */
  3003. if (ast_device_state(hint) == AST_DEVICE_UNAVAILABLE) {
  3004. transmit_lamp_indication(d, STIMULUS_LINE, sd->instance, SKINNY_LAMP_FLASH);
  3005. transmit_callstate(d, sd->instance, 0, SKINNY_ONHOOK);
  3006. return 0;
  3007. }
  3008. switch (state) {
  3009. case AST_EXTENSION_DEACTIVATED: /* Retry after a while */
  3010. case AST_EXTENSION_REMOVED: /* Extension is gone */
  3011. SKINNY_DEBUG(DEBUG_HINT, 3, "Extension state: Watcher for hint %s %s. Notify Device %s\n",
  3012. exten, state == AST_EXTENSION_DEACTIVATED ? "deactivated" : "removed", d->name);
  3013. sd->stateid = -1;
  3014. transmit_lamp_indication(d, STIMULUS_LINE, sd->instance, SKINNY_LAMP_OFF);
  3015. transmit_callstate(d, sd->instance, 0, SKINNY_ONHOOK);
  3016. break;
  3017. case AST_EXTENSION_RINGING:
  3018. case AST_EXTENSION_UNAVAILABLE:
  3019. transmit_lamp_indication(d, STIMULUS_LINE, sd->instance, SKINNY_LAMP_BLINK);
  3020. transmit_callstate(d, sd->instance, 0, SKINNY_RINGIN);
  3021. break;
  3022. case AST_EXTENSION_BUSY: /* callstate = SKINNY_BUSY wasn't wanting to work - I'll settle for this */
  3023. case AST_EXTENSION_INUSE:
  3024. transmit_lamp_indication(d, STIMULUS_LINE, sd->instance, SKINNY_LAMP_ON);
  3025. transmit_callstate(d, sd->instance, 0, SKINNY_CALLREMOTEMULTILINE);
  3026. break;
  3027. case AST_EXTENSION_ONHOLD:
  3028. transmit_lamp_indication(d, STIMULUS_LINE, sd->instance, SKINNY_LAMP_WINK);
  3029. transmit_callstate(d, sd->instance, 0, SKINNY_HOLD);
  3030. break;
  3031. case AST_EXTENSION_NOT_INUSE:
  3032. default:
  3033. transmit_lamp_indication(d, STIMULUS_LINE, sd->instance, SKINNY_LAMP_OFF);
  3034. transmit_callstate(d, sd->instance, 0, SKINNY_ONHOOK);
  3035. break;
  3036. }
  3037. }
  3038. sd->laststate = state;
  3039. } else if (container->type == SKINNY_SUBLINECONTAINER) {
  3040. struct skinny_subline *subline = container->data;
  3041. struct skinny_line *l = subline->line;
  3042. d = l->device;
  3043. SKINNY_DEBUG(DEBUG_HINT, 3, "Got hint %s on subline %s (%s@%s)\n", ast_extension_state2str(state), subline->name, exten, context);
  3044. subline->extenstate = state;
  3045. if (subline->callid == 0) {
  3046. return 0;
  3047. }
  3048. switch (state) {
  3049. case AST_EXTENSION_RINGING: /* Handled by normal ringin */
  3050. break;
  3051. case AST_EXTENSION_INUSE:
  3052. if (subline->sub && (subline->sub->substate == SKINNY_CONNECTED)) { /* Device has a real call */
  3053. transmit_callstate(d, l->instance, subline->callid, SKINNY_CONNECTED);
  3054. transmit_selectsoftkeys(d, l->instance, subline->callid, KEYDEF_CONNECTED, KEYMASK_ALL);
  3055. send_displaypromptstatus(d, OCTAL_CONNECTED, "", 0, l->instance, subline->callid);
  3056. } else { /* Some other device has active call */
  3057. transmit_callstate(d, l->instance, subline->callid, SKINNY_CALLREMOTEMULTILINE);
  3058. transmit_selectsoftkeys(d, l->instance, subline->callid, KEYDEF_SLACONNECTEDNOTACTIVE, KEYMASK_ALL);
  3059. send_displaypromptstatus(d, "In Use", "", 0, l->instance, subline->callid);
  3060. }
  3061. transmit_lamp_indication(d, STIMULUS_LINE, l->instance, SKINNY_LAMP_ON);
  3062. transmit_ringer_mode(d, SKINNY_RING_OFF);
  3063. transmit_activatecallplane(d, l);
  3064. break;
  3065. case AST_EXTENSION_ONHOLD:
  3066. transmit_callstate(d, l->instance, subline->callid, SKINNY_HOLD);
  3067. transmit_selectsoftkeys(d, l->instance, subline->callid, KEYDEF_SLAHOLD, KEYMASK_ALL);
  3068. send_displaypromptstatus(d, "Hold", "", 0, l->instance, subline->callid);
  3069. transmit_lamp_indication(d, STIMULUS_LINE, l->instance, SKINNY_LAMP_BLINK);
  3070. transmit_activatecallplane(d, l);
  3071. break;
  3072. case AST_EXTENSION_NOT_INUSE:
  3073. transmit_callstate(d, l->instance, subline->callid, SKINNY_ONHOOK);
  3074. transmit_selectsoftkeys(d, l->instance, subline->callid, KEYDEF_ONHOOK, KEYMASK_ALL);
  3075. transmit_clearpromptmessage(d, l->instance, subline->callid);
  3076. transmit_lamp_indication(d, STIMULUS_LINE, l->instance, SKINNY_LAMP_OFF);
  3077. transmit_activatecallplane(d, l);
  3078. subline->callid = 0;
  3079. break;
  3080. default:
  3081. ast_log(LOG_WARNING, "AST_EXTENSION_STATE %s not configured\n", ast_extension_state2str(state));
  3082. }
  3083. } else {
  3084. ast_log(LOG_WARNING, "Invalid data supplied to skinny_extensionstate_cb\n");
  3085. }
  3086. return 0;
  3087. }
  3088. static void update_connectedline(struct skinny_subchannel *sub, const void *data, size_t datalen)
  3089. {
  3090. struct ast_channel *c = sub->owner;
  3091. struct skinny_line *l = sub->line;
  3092. struct skinny_device *d = l->device;
  3093. if (!d->session) {
  3094. return;
  3095. }
  3096. if (sub->calldirection == SKINNY_OUTGOING && !sub->origtonum) {
  3097. /* Do not set origtonum before here or origtoname won't be set */
  3098. sub->origtonum = ast_strdup(sub->exten);
  3099. if (ast_channel_connected(c)->id.name.valid) {
  3100. sub->origtoname = ast_strdup(ast_channel_connected(c)->id.name.str);
  3101. }
  3102. }
  3103. if (!ast_channel_caller(c)->id.number.valid
  3104. || ast_strlen_zero(ast_channel_caller(c)->id.number.str)
  3105. || !ast_channel_connected(c)->id.number.valid
  3106. || ast_strlen_zero(ast_channel_connected(c)->id.number.str))
  3107. return;
  3108. SKINNY_DEBUG(DEBUG_SUB, 3, "Sub %u - Updating\n", sub->callid);
  3109. send_callinfo(sub);
  3110. }
  3111. static void mwi_event_cb(void *userdata, struct stasis_subscription *sub, struct stasis_message *msg)
  3112. {
  3113. struct skinny_line *l = userdata;
  3114. struct skinny_device *d = l->device;
  3115. struct skinny_line *l2;
  3116. int dev_msgs = 0;
  3117. if (!d || !d->session) {
  3118. return;
  3119. }
  3120. if (msg && ast_mwi_state_type() == stasis_message_type(msg)) {
  3121. struct ast_mwi_state *mwi_state = stasis_message_data(msg);
  3122. l->newmsgs = mwi_state->new_msgs;
  3123. }
  3124. if (l->newmsgs) {
  3125. transmit_lamp_indication(d, STIMULUS_VOICEMAIL, l->instance, l->mwiblink?SKINNY_LAMP_BLINK:SKINNY_LAMP_ON);
  3126. } else {
  3127. transmit_lamp_indication(d, STIMULUS_VOICEMAIL, l->instance, SKINNY_LAMP_OFF);
  3128. }
  3129. /* find out wether the device lamp should be on or off */
  3130. AST_LIST_TRAVERSE(&d->lines, l2, list) {
  3131. if (l2->newmsgs) {
  3132. dev_msgs++;
  3133. }
  3134. }
  3135. if (dev_msgs) {
  3136. transmit_lamp_indication(d, STIMULUS_VOICEMAIL, 0, d->mwiblink?SKINNY_LAMP_BLINK:SKINNY_LAMP_ON);
  3137. } else {
  3138. transmit_lamp_indication(d, STIMULUS_VOICEMAIL, 0, SKINNY_LAMP_OFF);
  3139. }
  3140. ast_verb(3, "Skinny mwi_event_cb found %d new messages\n", l->newmsgs);
  3141. }
  3142. /* I do not believe skinny can deal with video.
  3143. Anyone know differently? */
  3144. /* Yes, it can. Currently 7985 and Cisco VT Advantage do video. */
  3145. static enum ast_rtp_glue_result skinny_get_vrtp_peer(struct ast_channel *c, struct ast_rtp_instance **instance)
  3146. {
  3147. struct skinny_subchannel *sub = NULL;
  3148. if (!(sub = ast_channel_tech_pvt(c)) || !(sub->vrtp))
  3149. return AST_RTP_GLUE_RESULT_FORBID;
  3150. ao2_ref(sub->vrtp, +1);
  3151. *instance = sub->vrtp;
  3152. return AST_RTP_GLUE_RESULT_REMOTE;
  3153. }
  3154. static enum ast_rtp_glue_result skinny_get_rtp_peer(struct ast_channel *c, struct ast_rtp_instance **instance)
  3155. {
  3156. struct skinny_subchannel *sub = NULL;
  3157. struct skinny_line *l;
  3158. enum ast_rtp_glue_result res = AST_RTP_GLUE_RESULT_REMOTE;
  3159. SKINNY_DEBUG(DEBUG_AUDIO, 4, "skinny_get_rtp_peer() Channel = %s\n", ast_channel_name(c));
  3160. if (!(sub = ast_channel_tech_pvt(c)))
  3161. return AST_RTP_GLUE_RESULT_FORBID;
  3162. skinny_locksub(sub);
  3163. if (!(sub->rtp)){
  3164. skinny_unlocksub(sub);
  3165. return AST_RTP_GLUE_RESULT_FORBID;
  3166. }
  3167. ao2_ref(sub->rtp, +1);
  3168. *instance = sub->rtp;
  3169. l = sub->line;
  3170. if (!l->directmedia || l->nat){
  3171. res = AST_RTP_GLUE_RESULT_LOCAL;
  3172. SKINNY_DEBUG(DEBUG_AUDIO, 4, "skinny_get_rtp_peer() Using AST_RTP_GLUE_RESULT_LOCAL \n");
  3173. }
  3174. skinny_unlocksub(sub);
  3175. return res;
  3176. }
  3177. static int skinny_set_rtp_peer(struct ast_channel *c, struct ast_rtp_instance *rtp, struct ast_rtp_instance *vrtp, struct ast_rtp_instance *trtp, const struct ast_format_cap *codecs, int nat_active)
  3178. {
  3179. struct skinny_subchannel *sub;
  3180. struct skinny_line *l;
  3181. struct skinny_device *d;
  3182. struct sockaddr_in us = { 0, };
  3183. struct sockaddr_in them = { 0, };
  3184. struct ast_sockaddr them_tmp;
  3185. struct ast_sockaddr us_tmp;
  3186. sub = ast_channel_tech_pvt(c);
  3187. if (ast_channel_state(c) != AST_STATE_UP)
  3188. return 0;
  3189. if (!sub) {
  3190. return -1;
  3191. }
  3192. l = sub->line;
  3193. d = l->device;
  3194. if (rtp){
  3195. struct ast_format *tmpfmt;
  3196. unsigned int framing;
  3197. ast_rtp_instance_get_remote_address(rtp, &them_tmp);
  3198. ast_sockaddr_to_sin(&them_tmp, &them);
  3199. /* Shutdown any early-media or previous media on re-invite */
  3200. transmit_stopmediatransmission(d, sub);
  3201. SKINNY_DEBUG(DEBUG_AUDIO, 4, "Peerip = %s:%d\n", ast_inet_ntoa(them.sin_addr), ntohs(them.sin_port));
  3202. tmpfmt = ast_format_cap_get_format(l->cap, 0);
  3203. framing = ast_format_cap_get_format_framing(l->cap, tmpfmt);
  3204. SKINNY_DEBUG(DEBUG_AUDIO, 4, "Setting payloadType to '%s' (%u ms)\n", ast_format_get_name(tmpfmt), framing);
  3205. if (!(l->directmedia) || (l->nat)){
  3206. ast_rtp_instance_get_local_address(rtp, &us_tmp);
  3207. ast_sockaddr_to_sin(&us_tmp, &us);
  3208. us.sin_addr.s_addr = us.sin_addr.s_addr ? us.sin_addr.s_addr : d->ourip.s_addr;
  3209. transmit_startmediatransmission(d, sub, us, tmpfmt, framing);
  3210. } else {
  3211. transmit_startmediatransmission(d, sub, them, tmpfmt, framing);
  3212. }
  3213. ao2_ref(tmpfmt, -1);
  3214. return 0;
  3215. }
  3216. /* Need a return here to break the bridge */
  3217. return 0;
  3218. }
  3219. static struct ast_rtp_glue skinny_rtp_glue = {
  3220. .type = "Skinny",
  3221. .get_rtp_info = skinny_get_rtp_peer,
  3222. .get_vrtp_info = skinny_get_vrtp_peer,
  3223. .update_peer = skinny_set_rtp_peer,
  3224. };
  3225. #ifdef AST_DEVMODE
  3226. static char *skinny_debugs(void)
  3227. {
  3228. char *ptr;
  3229. int posn = 0;
  3230. ptr = dbgcli_buf;
  3231. ptr[0] = '\0';
  3232. if (skinnydebug & DEBUG_GENERAL) {
  3233. strcpy(ptr, "general "); /* SAFE */
  3234. posn += 8;
  3235. ptr += 8;
  3236. }
  3237. if (skinnydebug & DEBUG_SUB) {
  3238. strcpy(ptr, "sub "); /* SAFE */
  3239. posn += 4;
  3240. ptr += 4;
  3241. }
  3242. if (skinnydebug & DEBUG_AUDIO) {
  3243. strcpy(ptr, "audio "); /* SAFE */
  3244. posn += 6;
  3245. ptr += 6;
  3246. }
  3247. if (skinnydebug & DEBUG_PACKET) {
  3248. strcpy(ptr, "packet "); /* SAFE */
  3249. posn += 7;
  3250. ptr += 7;
  3251. }
  3252. if (skinnydebug & DEBUG_LOCK) {
  3253. strcpy(ptr, "lock "); /* SAFE */
  3254. posn += 5;
  3255. ptr += 5;
  3256. }
  3257. if (skinnydebug & DEBUG_TEMPLATE) {
  3258. strcpy(ptr, "template "); /* SAFE */
  3259. posn += 9;
  3260. ptr += 9;
  3261. }
  3262. if (skinnydebug & DEBUG_THREAD) {
  3263. strcpy(ptr, "thread "); /* SAFE */
  3264. posn += 7;
  3265. ptr += 7;
  3266. }
  3267. if (skinnydebug & DEBUG_HINT) {
  3268. strcpy(ptr, "hint "); /* SAFE */
  3269. posn += 5;
  3270. ptr += 5;
  3271. }
  3272. if (skinnydebug & DEBUG_KEEPALIVE) {
  3273. strcpy(ptr, "keepalive "); /* SAFE */
  3274. posn += 10;
  3275. ptr += 10;
  3276. }
  3277. if (posn > 0) {
  3278. strncpy(--ptr, "\0", 1);
  3279. }
  3280. return dbgcli_buf;
  3281. }
  3282. static char *complete_skinny_debug(const char *line, const char *word, int pos, int state)
  3283. {
  3284. const char *debugOpts[]={ "all","audio","hint","keepalive","lock","off","packet","show","sub","template","thread",NULL };
  3285. char *wordptr = (char *)word;
  3286. char buf[32];
  3287. char *bufptr = buf;
  3288. int buflen = sizeof(buf);
  3289. int wordlen;
  3290. int which = 0;
  3291. int i = 0;
  3292. if (*word == '+' || *word == '-' || *word == '!') {
  3293. *bufptr = *word;
  3294. wordptr++;
  3295. bufptr++;
  3296. buflen--;
  3297. }
  3298. wordlen = strlen(wordptr);
  3299. while (debugOpts[i]) {
  3300. if (!strncasecmp(wordptr, debugOpts[i], wordlen) && ++which > state) {
  3301. ast_copy_string(bufptr, debugOpts[i], buflen);
  3302. return ast_strdup(buf);
  3303. }
  3304. i++;
  3305. }
  3306. return NULL;
  3307. }
  3308. static char *handle_skinny_set_debug(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
  3309. {
  3310. int i;
  3311. int result = 0;
  3312. const char *arg;
  3313. int bitmask;
  3314. int negate;
  3315. switch (cmd) {
  3316. case CLI_INIT:
  3317. e->command = "skinny debug";
  3318. e->usage =
  3319. "Usage: skinny debug {audio|hint|keepalive|lock|off|packet|show|sub|template|thread}\n"
  3320. " Enables/Disables various Skinny debugging messages\n";
  3321. return NULL;
  3322. case CLI_GENERATE:
  3323. return complete_skinny_debug(a->line, a->word, a->pos, a->n);
  3324. }
  3325. if (a->argc < 3)
  3326. return CLI_SHOWUSAGE;
  3327. if (a->argc == 3 && !strncasecmp(a->argv[e->args-1], "show", 4)) {
  3328. ast_cli(a->fd, "Skinny Debugging - %s\n", skinny_debugs());
  3329. return CLI_SUCCESS;
  3330. }
  3331. for(i = e->args; i < a->argc; i++) {
  3332. result++;
  3333. arg = a->argv[i];
  3334. if (!strncasecmp(arg, "off", 3)) {
  3335. skinnydebug = 0;
  3336. continue;
  3337. }
  3338. if (!strncasecmp(arg, "all", 3)) {
  3339. skinnydebug = DEBUG_GENERAL|DEBUG_SUB|DEBUG_PACKET|DEBUG_AUDIO|DEBUG_LOCK|DEBUG_TEMPLATE|DEBUG_THREAD|DEBUG_HINT|DEBUG_KEEPALIVE;
  3340. continue;
  3341. }
  3342. if (!strncasecmp(arg, "-", 1) || !strncasecmp(arg, "!", 1)) {
  3343. negate = 1;
  3344. arg++;
  3345. } else if (!strncasecmp(arg, "+", 1)) {
  3346. negate = 0;
  3347. arg++;
  3348. } else {
  3349. negate = 0;
  3350. }
  3351. if (!strncasecmp(arg, "general", 7)) {
  3352. bitmask = DEBUG_GENERAL;
  3353. } else if (!strncasecmp(arg, "sub", 3)) {
  3354. bitmask = DEBUG_SUB;
  3355. } else if (!strncasecmp(arg, "packet", 6)) {
  3356. bitmask = DEBUG_PACKET;
  3357. } else if (!strncasecmp(arg, "audio", 5)) {
  3358. bitmask = DEBUG_AUDIO;
  3359. } else if (!strncasecmp(arg, "lock", 4)) {
  3360. bitmask = DEBUG_LOCK;
  3361. } else if (!strncasecmp(arg, "template", 8)) {
  3362. bitmask = DEBUG_TEMPLATE;
  3363. } else if (!strncasecmp(arg, "thread", 6)) {
  3364. bitmask = DEBUG_THREAD;
  3365. } else if (!strncasecmp(arg, "hint", 4)) {
  3366. bitmask = DEBUG_HINT;
  3367. } else if (!strncasecmp(arg, "keepalive", 9)) {
  3368. bitmask = DEBUG_KEEPALIVE;
  3369. } else {
  3370. ast_cli(a->fd, "Skinny Debugging - option '%s' unknown\n", a->argv[i]);
  3371. result--;
  3372. continue;
  3373. }
  3374. if (negate) {
  3375. skinnydebug &= ~bitmask;
  3376. } else {
  3377. skinnydebug |= bitmask;
  3378. }
  3379. }
  3380. if (result) {
  3381. ast_cli(a->fd, "Skinny Debugging - %s\n", skinnydebug ? skinny_debugs() : "off");
  3382. return CLI_SUCCESS;
  3383. } else {
  3384. return CLI_SHOWUSAGE;
  3385. }
  3386. }
  3387. #endif
  3388. static char *handle_skinny_reload(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
  3389. {
  3390. switch (cmd) {
  3391. case CLI_INIT:
  3392. e->command = "skinny reload";
  3393. e->usage =
  3394. "Usage: skinny reload\n"
  3395. " Reloads the chan_skinny configuration\n";
  3396. return NULL;
  3397. case CLI_GENERATE:
  3398. return NULL;
  3399. }
  3400. if (a->argc != e->args)
  3401. return CLI_SHOWUSAGE;
  3402. skinny_reload();
  3403. return CLI_SUCCESS;
  3404. }
  3405. static char *complete_skinny_devices(const char *word, int state)
  3406. {
  3407. struct skinny_device *d;
  3408. int wordlen = strlen(word), which = 0;
  3409. AST_LIST_TRAVERSE(&devices, d, list) {
  3410. if (!strncasecmp(word, d->name, wordlen) && ++which > state) {
  3411. return ast_strdup(d->name);
  3412. }
  3413. }
  3414. return NULL;
  3415. }
  3416. static char *complete_skinny_show_device(const char *line, const char *word, int pos, int state)
  3417. {
  3418. return (pos == 3 ? complete_skinny_devices(word, state) : NULL);
  3419. }
  3420. static char *complete_skinny_reset(const char *line, const char *word, int pos, int state)
  3421. {
  3422. if (pos == 2) {
  3423. static const char * const completions[] = { "all", NULL };
  3424. char *ret = ast_cli_complete(word, completions, state);
  3425. if (!ret) {
  3426. ret = complete_skinny_devices(word, state - 1);
  3427. }
  3428. return ret;
  3429. } else if (pos == 3) {
  3430. static const char * const completions[] = { "restart", NULL };
  3431. return ast_cli_complete(word, completions, state);
  3432. }
  3433. return NULL;
  3434. }
  3435. static char *complete_skinny_show_line(const char *line, const char *word, int pos, int state)
  3436. {
  3437. if (pos == 3) {
  3438. struct skinny_device *d;
  3439. struct skinny_line *l;
  3440. int wordlen = strlen(word), which = 0;
  3441. AST_LIST_TRAVERSE(&devices, d, list) {
  3442. AST_LIST_TRAVERSE(&d->lines, l, list) {
  3443. if (!strncasecmp(word, l->name, wordlen) && ++which > state) {
  3444. return ast_strdup(l->name);
  3445. }
  3446. }
  3447. }
  3448. } else if (pos == 4) {
  3449. static const char * const completions[] = { "on", NULL };
  3450. return ast_cli_complete(word, completions, state);
  3451. } else if (pos == 5) {
  3452. return complete_skinny_devices(word, state);
  3453. }
  3454. return NULL;
  3455. }
  3456. static char *handle_skinny_reset(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
  3457. {
  3458. struct skinny_device *d;
  3459. switch (cmd) {
  3460. case CLI_INIT:
  3461. e->command = "skinny reset";
  3462. e->usage =
  3463. "Usage: skinny reset <DeviceId|DeviceName|all> [restart]\n"
  3464. " Causes a Skinny device to reset itself, optionally with a full restart\n";
  3465. return NULL;
  3466. case CLI_GENERATE:
  3467. return complete_skinny_reset(a->line, a->word, a->pos, a->n);
  3468. }
  3469. if (a->argc < 3 || a->argc > 4)
  3470. return CLI_SHOWUSAGE;
  3471. AST_LIST_LOCK(&devices);
  3472. AST_LIST_TRAVERSE(&devices, d, list) {
  3473. int resetonly = 1;
  3474. if (!strcasecmp(a->argv[2], d->id) || !strcasecmp(a->argv[2], d->name) || !strcasecmp(a->argv[2], "all")) {
  3475. if (!(d->session))
  3476. continue;
  3477. if (a->argc == 4 && !strcasecmp(a->argv[3], "restart"))
  3478. resetonly = 0;
  3479. transmit_reset(d, resetonly);
  3480. }
  3481. }
  3482. AST_LIST_UNLOCK(&devices);
  3483. return CLI_SUCCESS;
  3484. }
  3485. static char *device2str(int type)
  3486. {
  3487. char *tmp;
  3488. switch (type) {
  3489. case SKINNY_DEVICE_NONE:
  3490. return "No Device";
  3491. case SKINNY_DEVICE_30SPPLUS:
  3492. return "30SP Plus";
  3493. case SKINNY_DEVICE_12SPPLUS:
  3494. return "12SP Plus";
  3495. case SKINNY_DEVICE_12SP:
  3496. return "12SP";
  3497. case SKINNY_DEVICE_12:
  3498. return "12";
  3499. case SKINNY_DEVICE_30VIP:
  3500. return "30VIP";
  3501. case SKINNY_DEVICE_7910:
  3502. return "7910";
  3503. case SKINNY_DEVICE_7960:
  3504. return "7960";
  3505. case SKINNY_DEVICE_7940:
  3506. return "7940";
  3507. case SKINNY_DEVICE_7935:
  3508. return "7935";
  3509. case SKINNY_DEVICE_ATA186:
  3510. return "ATA186";
  3511. case SKINNY_DEVICE_7941:
  3512. return "7941";
  3513. case SKINNY_DEVICE_7971:
  3514. return "7971";
  3515. case SKINNY_DEVICE_7914:
  3516. return "7914";
  3517. case SKINNY_DEVICE_7985:
  3518. return "7985";
  3519. case SKINNY_DEVICE_7911:
  3520. return "7911";
  3521. case SKINNY_DEVICE_7961GE:
  3522. return "7961GE";
  3523. case SKINNY_DEVICE_7941GE:
  3524. return "7941GE";
  3525. case SKINNY_DEVICE_7931:
  3526. return "7931";
  3527. case SKINNY_DEVICE_7921:
  3528. return "7921";
  3529. case SKINNY_DEVICE_7906:
  3530. return "7906";
  3531. case SKINNY_DEVICE_7962:
  3532. return "7962";
  3533. case SKINNY_DEVICE_7937:
  3534. return "7937";
  3535. case SKINNY_DEVICE_7942:
  3536. return "7942";
  3537. case SKINNY_DEVICE_7945:
  3538. return "7945";
  3539. case SKINNY_DEVICE_7965:
  3540. return "7965";
  3541. case SKINNY_DEVICE_7975:
  3542. return "7975";
  3543. case SKINNY_DEVICE_7905:
  3544. return "7905";
  3545. case SKINNY_DEVICE_7920:
  3546. return "7920";
  3547. case SKINNY_DEVICE_7970:
  3548. return "7970";
  3549. case SKINNY_DEVICE_7912:
  3550. return "7912";
  3551. case SKINNY_DEVICE_7902:
  3552. return "7902";
  3553. case SKINNY_DEVICE_CIPC:
  3554. return "IP Communicator";
  3555. case SKINNY_DEVICE_7961:
  3556. return "7961";
  3557. case SKINNY_DEVICE_7936:
  3558. return "7936";
  3559. case SKINNY_DEVICE_SCCPGATEWAY_AN:
  3560. return "SCCPGATEWAY_AN";
  3561. case SKINNY_DEVICE_SCCPGATEWAY_BRI:
  3562. return "SCCPGATEWAY_BRI";
  3563. case SKINNY_DEVICE_UNKNOWN:
  3564. return "Unknown";
  3565. default:
  3566. if (!(tmp = ast_threadstorage_get(&device2str_threadbuf, DEVICE2STR_BUFSIZE)))
  3567. return "Unknown";
  3568. snprintf(tmp, DEVICE2STR_BUFSIZE, "UNKNOWN-%d", type);
  3569. return tmp;
  3570. }
  3571. }
  3572. static char *_skinny_show_devices(int fd, int *total, struct mansession *s, const struct message *m, int argc, const char * const *argv)
  3573. {
  3574. struct skinny_device *d;
  3575. struct skinny_line *l;
  3576. const char *id;
  3577. char idtext[256] = "";
  3578. int total_devices = 0;
  3579. if (s) { /* Manager - get ActionID */
  3580. id = astman_get_header(m, "ActionID");
  3581. if (!ast_strlen_zero(id))
  3582. snprintf(idtext, sizeof(idtext), "ActionID: %s\r\n", id);
  3583. }
  3584. switch (argc) {
  3585. case 3:
  3586. break;
  3587. default:
  3588. return CLI_SHOWUSAGE;
  3589. }
  3590. if (!s) {
  3591. ast_cli(fd, "Name DeviceId IP Type R NL\n");
  3592. ast_cli(fd, "-------------------- ---------------- --------------- --------------- - --\n");
  3593. }
  3594. AST_LIST_LOCK(&devices);
  3595. AST_LIST_TRAVERSE(&devices, d, list) {
  3596. int numlines = 0;
  3597. total_devices++;
  3598. AST_LIST_TRAVERSE(&d->lines, l, list) {
  3599. numlines++;
  3600. }
  3601. if (!s) {
  3602. ast_cli(fd, "%-20s %-16s %-15s %-15s %c %2d\n",
  3603. d->name,
  3604. d->id,
  3605. d->session?ast_inet_ntoa(d->session->sin.sin_addr):"",
  3606. device2str(d->type),
  3607. d->session?'Y':'N',
  3608. numlines);
  3609. } else {
  3610. astman_append(s,
  3611. "Event: DeviceEntry\r\n%s"
  3612. "Channeltype: SKINNY\r\n"
  3613. "ObjectName: %s\r\n"
  3614. "ChannelObjectType: device\r\n"
  3615. "DeviceId: %s\r\n"
  3616. "IPaddress: %s\r\n"
  3617. "Type: %s\r\n"
  3618. "Devicestatus: %s\r\n"
  3619. "NumberOfLines: %d\r\n",
  3620. idtext,
  3621. d->name,
  3622. d->id,
  3623. d->session?ast_inet_ntoa(d->session->sin.sin_addr):"-none-",
  3624. device2str(d->type),
  3625. d->session?"registered":"unregistered",
  3626. numlines);
  3627. }
  3628. }
  3629. AST_LIST_UNLOCK(&devices);
  3630. if (total)
  3631. *total = total_devices;
  3632. return CLI_SUCCESS;
  3633. }
  3634. /*! \brief Show SKINNY devices in the manager API */
  3635. /* Inspired from chan_sip */
  3636. static int manager_skinny_show_devices(struct mansession *s, const struct message *m)
  3637. {
  3638. const char *a[] = {"skinny", "show", "devices"};
  3639. int total = 0;
  3640. astman_send_listack(s, m, "Device status list will follow", "start");
  3641. /* List the devices in separate manager events */
  3642. _skinny_show_devices(-1, &total, s, m, 3, a);
  3643. /* Send final confirmation */
  3644. astman_send_list_complete_start(s, m, "DevicelistComplete", total);
  3645. astman_send_list_complete_end(s);
  3646. return 0;
  3647. }
  3648. static char *handle_skinny_show_devices(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
  3649. {
  3650. switch (cmd) {
  3651. case CLI_INIT:
  3652. e->command = "skinny show devices";
  3653. e->usage =
  3654. "Usage: skinny show devices\n"
  3655. " Lists all devices known to the Skinny subsystem.\n";
  3656. return NULL;
  3657. case CLI_GENERATE:
  3658. return NULL;
  3659. }
  3660. return _skinny_show_devices(a->fd, NULL, NULL, NULL, a->argc, a->argv);
  3661. }
  3662. static char *_skinny_show_device(int type, int fd, struct mansession *s, const struct message *m, int argc, const char * const *argv)
  3663. {
  3664. struct skinny_device *d;
  3665. struct skinny_line *l;
  3666. struct skinny_speeddial *sd;
  3667. struct skinny_addon *sa;
  3668. struct skinny_serviceurl *surl;
  3669. struct ast_str *codec_buf = ast_str_alloca(AST_FORMAT_CAP_NAMES_LEN);
  3670. if (argc < 4) {
  3671. return CLI_SHOWUSAGE;
  3672. }
  3673. AST_LIST_LOCK(&devices);
  3674. AST_LIST_TRAVERSE(&devices, d, list) {
  3675. if (!strcasecmp(argv[3], d->id) || !strcasecmp(argv[3], d->name)) {
  3676. int numlines = 0, numaddons = 0, numspeeddials = 0, numserviceurls = 0;
  3677. AST_LIST_TRAVERSE(&d->lines, l, list){
  3678. numlines++;
  3679. }
  3680. AST_LIST_TRAVERSE(&d->addons, sa, list) {
  3681. numaddons++;
  3682. }
  3683. AST_LIST_TRAVERSE(&d->speeddials, sd, list) {
  3684. numspeeddials++;
  3685. }
  3686. AST_LIST_TRAVERSE(&d->serviceurls, surl, list) {
  3687. numserviceurls++;
  3688. }
  3689. if (type == 0) { /* CLI */
  3690. ast_cli(fd, "Name: %s\n", d->name);
  3691. ast_cli(fd, "Id: %s\n", d->id);
  3692. ast_cli(fd, "version: %s\n", S_OR(d->version_id, "Unknown"));
  3693. ast_cli(fd, "Ip address: %s\n", (d->session ? ast_inet_ntoa(d->session->sin.sin_addr) : "Unknown"));
  3694. ast_cli(fd, "Port: %d\n", (d->session ? ntohs(d->session->sin.sin_port) : 0));
  3695. ast_cli(fd, "Device Type: %s\n", device2str(d->type));
  3696. ast_cli(fd, "Conf Codecs: %s\n", ast_format_cap_get_names(d->confcap, &codec_buf));
  3697. ast_cli(fd, "Neg Codecs: %s\n", ast_format_cap_get_names(d->cap, &codec_buf));
  3698. ast_cli(fd, "Registered: %s\n", (d->session ? "Yes" : "No"));
  3699. ast_cli(fd, "Lines: %d\n", numlines);
  3700. AST_LIST_TRAVERSE(&d->lines, l, list) {
  3701. ast_cli(fd, " %s (%s)\n", l->name, l->label);
  3702. }
  3703. ast_cli(fd, "Addons: %d\n", numaddons);
  3704. AST_LIST_TRAVERSE(&d->addons, sa, list) {
  3705. ast_cli(fd, " %s\n", sa->type);
  3706. }
  3707. ast_cli(fd, "Speeddials: %d\n", numspeeddials);
  3708. AST_LIST_TRAVERSE(&d->speeddials, sd, list) {
  3709. ast_cli(fd, " %s (%s) ishint: %d\n", sd->exten, sd->label, sd->isHint);
  3710. }
  3711. ast_cli(fd, "ServiceURLs: %d\n", numserviceurls);
  3712. AST_LIST_TRAVERSE(&d->serviceurls, surl, list) {
  3713. ast_cli(fd, " %s (%s)\n", surl->displayName, surl->url);
  3714. }
  3715. } else { /* manager */
  3716. astman_append(s, "Channeltype: SKINNY\r\n");
  3717. astman_append(s, "ObjectName: %s\r\n", d->name);
  3718. astman_append(s, "ChannelObjectType: device\r\n");
  3719. astman_append(s, "Id: %s\r\n", d->id);
  3720. astman_append(s, "version: %s\r\n", S_OR(d->version_id, "Unknown"));
  3721. astman_append(s, "Ipaddress: %s\r\n", (d->session ? ast_inet_ntoa(d->session->sin.sin_addr) : "Unknown"));
  3722. astman_append(s, "Port: %d\r\n", (d->session ? ntohs(d->session->sin.sin_port) : 0));
  3723. astman_append(s, "DeviceType: %s\r\n", device2str(d->type));
  3724. astman_append(s, "Codecs: %s\r\n", ast_format_cap_get_names(d->confcap, &codec_buf));
  3725. astman_append(s, "CodecOrder: %s\r\n", ast_format_cap_get_names(d->cap, &codec_buf));
  3726. astman_append(s, "Devicestatus: %s\r\n", (d->session?"registered":"unregistered"));
  3727. astman_append(s, "NumberOfLines: %d\r\n", numlines);
  3728. AST_LIST_TRAVERSE(&d->lines, l, list) {
  3729. astman_append(s, "Line: %s (%s)\r\n", l->name, l->label);
  3730. }
  3731. astman_append(s, "NumberOfAddons: %d\r\n", numaddons);
  3732. AST_LIST_TRAVERSE(&d->addons, sa, list) {
  3733. astman_append(s, "Addon: %s\r\n", sa->type);
  3734. }
  3735. astman_append(s, "NumberOfSpeeddials: %d\r\n", numspeeddials);
  3736. AST_LIST_TRAVERSE(&d->speeddials, sd, list) {
  3737. astman_append(s, "Speeddial: %s (%s) ishint: %d\r\n", sd->exten, sd->label, sd->isHint);
  3738. }
  3739. astman_append(s, "ServiceURLs: %d\r\n", numserviceurls);
  3740. AST_LIST_TRAVERSE(&d->serviceurls, surl, list) {
  3741. astman_append(s, " %s (%s)\r\n", surl->displayName, surl->url);
  3742. }
  3743. }
  3744. }
  3745. }
  3746. AST_LIST_UNLOCK(&devices);
  3747. return CLI_SUCCESS;
  3748. }
  3749. static int manager_skinny_show_device(struct mansession *s, const struct message *m)
  3750. {
  3751. const char *a[4];
  3752. const char *device;
  3753. device = astman_get_header(m, "Device");
  3754. if (ast_strlen_zero(device)) {
  3755. astman_send_error(s, m, "Device: <name> missing.");
  3756. return 0;
  3757. }
  3758. a[0] = "skinny";
  3759. a[1] = "show";
  3760. a[2] = "device";
  3761. a[3] = device;
  3762. _skinny_show_device(1, -1, s, m, 4, a);
  3763. astman_append(s, "\r\n\r\n" );
  3764. return 0;
  3765. }
  3766. /*! \brief Show device information */
  3767. static char *handle_skinny_show_device(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
  3768. {
  3769. switch (cmd) {
  3770. case CLI_INIT:
  3771. e->command = "skinny show device";
  3772. e->usage =
  3773. "Usage: skinny show device <DeviceId|DeviceName>\n"
  3774. " Lists all deviceinformation of a specific device known to the Skinny subsystem.\n";
  3775. return NULL;
  3776. case CLI_GENERATE:
  3777. return complete_skinny_show_device(a->line, a->word, a->pos, a->n);
  3778. }
  3779. return _skinny_show_device(0, a->fd, NULL, NULL, a->argc, a->argv);
  3780. }
  3781. static char *_skinny_show_lines(int fd, int *total, struct mansession *s, const struct message *m, int argc, const char * const *argv)
  3782. {
  3783. struct skinny_line *l;
  3784. struct skinny_subchannel *sub;
  3785. int total_lines = 0;
  3786. int verbose = 0;
  3787. const char *id;
  3788. char idtext[256] = "";
  3789. if (s) { /* Manager - get ActionID */
  3790. id = astman_get_header(m, "ActionID");
  3791. if (!ast_strlen_zero(id))
  3792. snprintf(idtext, sizeof(idtext), "ActionID: %s\r\n", id);
  3793. }
  3794. switch (argc) {
  3795. case 4:
  3796. verbose = 1;
  3797. break;
  3798. case 3:
  3799. verbose = 0;
  3800. break;
  3801. default:
  3802. return CLI_SHOWUSAGE;
  3803. }
  3804. if (!s) {
  3805. ast_cli(fd, "Name Device Name Instance Label \n");
  3806. ast_cli(fd, "-------------------- -------------------- -------- --------------------\n");
  3807. }
  3808. AST_LIST_LOCK(&lines);
  3809. AST_LIST_TRAVERSE(&lines, l, all) {
  3810. total_lines++;
  3811. if (!s) {
  3812. ast_cli(fd, "%-20s %-20s %8d %-20s\n",
  3813. l->name,
  3814. (l->device ? l->device->name : "Not connected"),
  3815. l->instance,
  3816. l->label);
  3817. if (verbose) {
  3818. AST_LIST_TRAVERSE(&l->sub, sub, list) {
  3819. RAII_VAR(struct ast_channel *, bridged, ast_channel_bridge_peer(sub->owner), ao2_cleanup);
  3820. ast_cli(fd, " %s> %s to %s\n",
  3821. (sub == l->activesub?"Active ":"Inactive"),
  3822. ast_channel_name(sub->owner),
  3823. bridged ? ast_channel_name(bridged) : ""
  3824. );
  3825. }
  3826. }
  3827. } else {
  3828. astman_append(s,
  3829. "Event: LineEntry\r\n%s"
  3830. "Channeltype: SKINNY\r\n"
  3831. "ObjectName: %s\r\n"
  3832. "ChannelObjectType: line\r\n"
  3833. "Device: %s\r\n"
  3834. "Instance: %d\r\n"
  3835. "Label: %s\r\n",
  3836. idtext,
  3837. l->name,
  3838. (l->device ? l->device->name : "None"),
  3839. l->instance,
  3840. l->label);
  3841. }
  3842. }
  3843. AST_LIST_UNLOCK(&lines);
  3844. if (total) {
  3845. *total = total_lines;
  3846. }
  3847. return CLI_SUCCESS;
  3848. }
  3849. /*! \brief Show Skinny lines in the manager API */
  3850. /* Inspired from chan_sip */
  3851. static int manager_skinny_show_lines(struct mansession *s, const struct message *m)
  3852. {
  3853. const char *a[] = {"skinny", "show", "lines"};
  3854. int total = 0;
  3855. astman_send_listack(s, m, "Line status list will follow", "start");
  3856. /* List the lines in separate manager events */
  3857. _skinny_show_lines(-1, &total, s, m, 3, a);
  3858. /* Send final confirmation */
  3859. astman_send_list_complete_start(s, m, "LinelistComplete", total);
  3860. astman_send_list_complete_end(s);
  3861. return 0;
  3862. }
  3863. static char *handle_skinny_show_lines(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
  3864. {
  3865. switch (cmd) {
  3866. case CLI_INIT:
  3867. e->command = "skinny show lines [verbose]";
  3868. e->usage =
  3869. "Usage: skinny show lines\n"
  3870. " Lists all lines known to the Skinny subsystem.\n"
  3871. " If 'verbose' is specified, the output includes\n"
  3872. " information about subs for each line.\n";
  3873. return NULL;
  3874. case CLI_GENERATE:
  3875. return NULL;
  3876. }
  3877. if (a->argc == e->args) {
  3878. if (strcasecmp(a->argv[e->args-1], "verbose")) {
  3879. return CLI_SHOWUSAGE;
  3880. }
  3881. } else if (a->argc != e->args - 1) {
  3882. return CLI_SHOWUSAGE;
  3883. }
  3884. return _skinny_show_lines(a->fd, NULL, NULL, NULL, a->argc, a->argv);
  3885. }
  3886. static char *_skinny_show_line(int type, int fd, struct mansession *s, const struct message *m, int argc, const char * const *argv)
  3887. {
  3888. struct skinny_device *d;
  3889. struct skinny_line *l;
  3890. struct skinny_subline *subline;
  3891. struct ast_str *codec_buf = ast_str_alloca(AST_FORMAT_CAP_NAMES_LEN);
  3892. char group_buf[256];
  3893. char cbuf[256];
  3894. switch (argc) {
  3895. case 4:
  3896. break;
  3897. case 6:
  3898. break;
  3899. default:
  3900. return CLI_SHOWUSAGE;
  3901. }
  3902. AST_LIST_LOCK(&devices);
  3903. /* Show all lines matching the one supplied */
  3904. AST_LIST_TRAVERSE(&devices, d, list) {
  3905. if (argc == 6 && (strcasecmp(argv[5], d->id) && strcasecmp(argv[5], d->name))) {
  3906. continue;
  3907. }
  3908. AST_LIST_TRAVERSE(&d->lines, l, list) {
  3909. struct ast_str *tmp_str = ast_str_alloca(512);
  3910. if (strcasecmp(argv[3], l->name)) {
  3911. continue;
  3912. }
  3913. if (type == 0) { /* CLI */
  3914. ast_cli(fd, "Line: %s\n", l->name);
  3915. ast_cli(fd, "On Device: %s\n", d->name);
  3916. ast_cli(fd, "Line Label: %s\n", l->label);
  3917. ast_cli(fd, "Extension: %s\n", S_OR(l->exten, "<not set>"));
  3918. ast_cli(fd, "Context: %s\n", l->context);
  3919. ast_cli(fd, "CallGroup: %s\n", ast_print_group(group_buf, sizeof(group_buf), l->callgroup));
  3920. ast_cli(fd, "PickupGroup: %s\n", ast_print_group(group_buf, sizeof(group_buf), l->pickupgroup));
  3921. ast_cli(fd, "NamedCallGroup: %s\n", ast_print_namedgroups(&tmp_str, l->named_callgroups));
  3922. ast_str_reset(tmp_str);
  3923. ast_cli(fd, "NamedPickupGroup: %s\n", ast_print_namedgroups(&tmp_str, l->named_pickupgroups));
  3924. ast_str_reset(tmp_str);
  3925. ast_cli(fd, "Language: %s\n", S_OR(l->language, "<not set>"));
  3926. ast_cli(fd, "Accountcode: %s\n", S_OR(l->accountcode, "<not set>"));
  3927. ast_cli(fd, "AmaFlag: %s\n", ast_channel_amaflags2string(l->amaflags));
  3928. ast_cli(fd, "CallerId Number: %s\n", S_OR(l->cid_num, "<not set>"));
  3929. ast_cli(fd, "CallerId Name: %s\n", S_OR(l->cid_name, "<not set>"));
  3930. ast_cli(fd, "Hide CallerId: %s\n", (l->hidecallerid ? "Yes" : "No"));
  3931. ast_cli(fd, "CFwdAll: %s\n", S_COR((l->cfwdtype & SKINNY_CFWD_ALL), l->call_forward_all, "<not set>"));
  3932. ast_cli(fd, "CFwdBusy: %s\n", S_COR((l->cfwdtype & SKINNY_CFWD_BUSY), l->call_forward_busy, "<not set>"));
  3933. ast_cli(fd, "CFwdNoAnswer: %s\n", S_COR((l->cfwdtype & SKINNY_CFWD_NOANSWER), l->call_forward_noanswer, "<not set>"));
  3934. ast_cli(fd, "CFwdTimeout: %dms\n", l->callfwdtimeout);
  3935. ast_cli(fd, "VoicemailBox: %s\n", S_OR(l->mailbox, "<not set>"));
  3936. ast_cli(fd, "VoicemailNumber: %s\n", S_OR(l->vmexten, "<not set>"));
  3937. ast_cli(fd, "MWIblink: %d\n", l->mwiblink);
  3938. ast_cli(fd, "Regextension: %s\n", S_OR(l->regexten, "<not set>"));
  3939. ast_cli(fd, "Regcontext: %s\n", S_OR(l->regcontext, "<not set>"));
  3940. ast_cli(fd, "MoHInterpret: %s\n", S_OR(l->mohinterpret, "<not set>"));
  3941. ast_cli(fd, "MoHSuggest: %s\n", S_OR(l->mohsuggest, "<not set>"));
  3942. ast_cli(fd, "Last dialed nr: %s\n", S_OR(l->lastnumberdialed, "<no calls made yet>"));
  3943. ast_cli(fd, "Last CallerID: %s\n", S_OR(l->lastcallerid, "<not set>"));
  3944. ast_cli(fd, "Transfer enabled: %s\n", (l->transfer ? "Yes" : "No"));
  3945. ast_cli(fd, "Callwaiting: %s\n", (l->callwaiting ? "Yes" : "No"));
  3946. ast_cli(fd, "3Way Calling: %s\n", (l->threewaycalling ? "Yes" : "No"));
  3947. ast_cli(fd, "Can forward: %s\n", (l->cancallforward ? "Yes" : "No"));
  3948. ast_cli(fd, "Do Not Disturb: %s\n", (l->dnd ? "Yes" : "No"));
  3949. ast_cli(fd, "NAT: %s\n", (l->nat ? "Yes" : "No"));
  3950. ast_cli(fd, "immediate: %s\n", (l->immediate ? "Yes" : "No"));
  3951. ast_cli(fd, "Group: %d\n", l->group);
  3952. ast_cli(fd, "Parkinglot: %s\n", S_OR(l->parkinglot, "<not set>"));
  3953. ast_cli(fd, "Conf Codecs: %s\n", ast_format_cap_get_names(l->confcap, &codec_buf));
  3954. ast_cli(fd, "Neg Codecs: %s\n", ast_format_cap_get_names(l->cap, &codec_buf));
  3955. if (AST_LIST_FIRST(&l->sublines)) {
  3956. ast_cli(fd, "Sublines:\n");
  3957. AST_LIST_TRAVERSE(&l->sublines, subline, list) {
  3958. ast_cli(fd, " %s, %s@%s\n", subline->name, subline->exten, subline->context);
  3959. }
  3960. }
  3961. ast_cli(fd, "\n");
  3962. } else { /* manager */
  3963. astman_append(s, "Channeltype: SKINNY\r\n");
  3964. astman_append(s, "ObjectName: %s\r\n", l->name);
  3965. astman_append(s, "ChannelObjectType: line\r\n");
  3966. astman_append(s, "Device: %s\r\n", d->name);
  3967. astman_append(s, "LineLabel: %s\r\n", l->label);
  3968. astman_append(s, "Extension: %s\r\n", S_OR(l->exten, "<not set>"));
  3969. astman_append(s, "Context: %s\r\n", l->context);
  3970. astman_append(s, "CallGroup: %s\r\n", ast_print_group(group_buf, sizeof(group_buf), l->callgroup));
  3971. astman_append(s, "PickupGroup: %s\r\n", ast_print_group(group_buf, sizeof(group_buf), l->pickupgroup));
  3972. astman_append(s, "NamedCallGroup: %s\r\n", ast_print_namedgroups(&tmp_str, l->named_callgroups));
  3973. ast_str_reset(tmp_str);
  3974. astman_append(s, "NamedPickupGroup: %s\r\n", ast_print_namedgroups(&tmp_str, l->named_pickupgroups));
  3975. ast_str_reset(tmp_str);
  3976. astman_append(s, "Language: %s\r\n", S_OR(l->language, "<not set>"));
  3977. astman_append(s, "Accountcode: %s\r\n", S_OR(l->accountcode, "<not set>"));
  3978. astman_append(s, "AMAflags: %s\r\n", ast_channel_amaflags2string(l->amaflags));
  3979. astman_append(s, "Callerid: %s\r\n", ast_callerid_merge(cbuf, sizeof(cbuf), l->cid_name, l->cid_num, ""));
  3980. astman_append(s, "HideCallerId: %s\r\n", (l->hidecallerid ? "Yes" : "No"));
  3981. astman_append(s, "CFwdAll: %s\r\n", S_COR((l->cfwdtype & SKINNY_CFWD_ALL), l->call_forward_all, "<not set>"));
  3982. astman_append(s, "CFwdBusy: %s\r\n", S_COR((l->cfwdtype & SKINNY_CFWD_BUSY), l->call_forward_busy, "<not set>"));
  3983. astman_append(s, "CFwdNoAnswer: %s\r\n", S_COR((l->cfwdtype & SKINNY_CFWD_NOANSWER), l->call_forward_noanswer, "<not set>"));
  3984. astman_append(s, "VoicemailBox: %s\r\n", S_OR(l->mailbox, "<not set>"));
  3985. astman_append(s, "VoicemailNumber: %s\r\n", S_OR(l->vmexten, "<not set>"));
  3986. astman_append(s, "MWIblink: %d\r\n", l->mwiblink);
  3987. astman_append(s, "RegExtension: %s\r\n", S_OR(l->regexten, "<not set>"));
  3988. astman_append(s, "Regcontext: %s\r\n", S_OR(l->regcontext, "<not set>"));
  3989. astman_append(s, "MoHInterpret: %s\r\n", S_OR(l->mohinterpret, "<not set>"));
  3990. astman_append(s, "MoHSuggest: %s\r\n", S_OR(l->mohsuggest, "<not set>"));
  3991. astman_append(s, "LastDialedNr: %s\r\n", S_OR(l->lastnumberdialed, "<no calls made yet>"));
  3992. astman_append(s, "LastCallerID: %s\r\n", S_OR(l->lastcallerid, "<not set>"));
  3993. astman_append(s, "Transfer: %s\r\n", (l->transfer ? "Yes" : "No"));
  3994. astman_append(s, "Callwaiting: %s\r\n", (l->callwaiting ? "Yes" : "No"));
  3995. astman_append(s, "3WayCalling: %s\r\n", (l->threewaycalling ? "Yes" : "No"));
  3996. astman_append(s, "CanForward: %s\r\n", (l->cancallforward ? "Yes" : "No"));
  3997. astman_append(s, "DoNotDisturb: %s\r\n", (l->dnd ? "Yes" : "No"));
  3998. astman_append(s, "NAT: %s\r\n", (l->nat ? "Yes" : "No"));
  3999. astman_append(s, "immediate: %s\r\n", (l->immediate ? "Yes" : "No"));
  4000. astman_append(s, "Group: %d\r\n", l->group);
  4001. astman_append(s, "Parkinglot: %s\r\n", S_OR(l->parkinglot, "<not set>"));
  4002. astman_append(s, "Codecs: %s\r\n", ast_format_cap_get_names(l->confcap, &codec_buf));
  4003. astman_append(s, "\r\n");
  4004. }
  4005. }
  4006. }
  4007. AST_LIST_UNLOCK(&devices);
  4008. return CLI_SUCCESS;
  4009. }
  4010. static int manager_skinny_show_line(struct mansession *s, const struct message *m)
  4011. {
  4012. const char *a[4];
  4013. const char *line;
  4014. line = astman_get_header(m, "Line");
  4015. if (ast_strlen_zero(line)) {
  4016. astman_send_error(s, m, "Line: <name> missing.");
  4017. return 0;
  4018. }
  4019. a[0] = "skinny";
  4020. a[1] = "show";
  4021. a[2] = "line";
  4022. a[3] = line;
  4023. _skinny_show_line(1, -1, s, m, 4, a);
  4024. astman_append(s, "\r\n\r\n" );
  4025. return 0;
  4026. }
  4027. /*! \brief List line information. */
  4028. static char *handle_skinny_show_line(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
  4029. {
  4030. switch (cmd) {
  4031. case CLI_INIT:
  4032. e->command = "skinny show line";
  4033. e->usage =
  4034. "Usage: skinny show line <Line> [on <DeviceID|DeviceName>]\n"
  4035. " List all lineinformation of a specific line known to the Skinny subsystem.\n";
  4036. return NULL;
  4037. case CLI_GENERATE:
  4038. return complete_skinny_show_line(a->line, a->word, a->pos, a->n);
  4039. }
  4040. return _skinny_show_line(0, a->fd, NULL, NULL, a->argc, a->argv);
  4041. }
  4042. /*! \brief List global settings for the Skinny subsystem. */
  4043. static char *handle_skinny_show_settings(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
  4044. {
  4045. char immed_str[2] = {immed_dialchar, '\0'};
  4046. switch (cmd) {
  4047. case CLI_INIT:
  4048. e->command = "skinny show settings";
  4049. e->usage =
  4050. "Usage: skinny show settings\n"
  4051. " Lists all global configuration settings of the Skinny subsystem.\n";
  4052. return NULL;
  4053. case CLI_GENERATE:
  4054. return NULL;
  4055. }
  4056. if (a->argc != 3)
  4057. return CLI_SHOWUSAGE;
  4058. ast_cli(a->fd, "\nGlobal Settings:\n");
  4059. ast_cli(a->fd, " Skinny Port: %d\n", ntohs(bindaddr.sin_port));
  4060. ast_cli(a->fd, " Bindaddress: %s\n", ast_inet_ntoa(bindaddr.sin_addr));
  4061. ast_cli(a->fd, " KeepAlive: %d\n", keep_alive);
  4062. ast_cli(a->fd, " Date Format: %s\n", date_format);
  4063. ast_cli(a->fd, " Voice Mail Extension: %s\n", S_OR(vmexten, "(not set)"));
  4064. ast_cli(a->fd, " Reg. context: %s\n", S_OR(regcontext, "(not set)"));
  4065. ast_cli(a->fd, " Immed. Dial Key: %s\n", S_OR(immed_str, "(not set)"));
  4066. ast_cli(a->fd, " Jitterbuffer enabled: %s\n", AST_CLI_YESNO(ast_test_flag(&global_jbconf, AST_JB_ENABLED)));
  4067. if (ast_test_flag(&global_jbconf, AST_JB_ENABLED)) {
  4068. ast_cli(a->fd, " Jitterbuffer forced: %s\n", AST_CLI_YESNO(ast_test_flag(&global_jbconf, AST_JB_FORCED)));
  4069. ast_cli(a->fd, " Jitterbuffer max size: %ld\n", global_jbconf.max_size);
  4070. ast_cli(a->fd, " Jitterbuffer resync: %ld\n", global_jbconf.resync_threshold);
  4071. ast_cli(a->fd, " Jitterbuffer impl: %s\n", global_jbconf.impl);
  4072. if (!strcasecmp(global_jbconf.impl, "adaptive")) {
  4073. ast_cli(a->fd, " Jitterbuffer tgt extra: %ld\n", global_jbconf.target_extra);
  4074. }
  4075. ast_cli(a->fd, " Jitterbuffer log: %s\n", AST_CLI_YESNO(ast_test_flag(&global_jbconf, AST_JB_LOG)));
  4076. }
  4077. return CLI_SUCCESS;
  4078. }
  4079. static char *_skinny_message_set(int type, int fd, struct mansession *s, const struct message *m, int argc, const char * const *argv)
  4080. {
  4081. struct skinny_device *d;
  4082. char text_buf[32];
  4083. if (argc < 7) {
  4084. return CLI_SHOWUSAGE;
  4085. }
  4086. AST_LIST_LOCK(&devices);
  4087. AST_LIST_TRAVERSE(&devices, d, list) {
  4088. if (!strcasecmp(argv[3], d->name)) {
  4089. int i;
  4090. char *strp = text_buf;
  4091. int charleft = sizeof(text_buf);
  4092. int priority = atoi(argv[4]);
  4093. int timeout = atoi(argv[5]);
  4094. ast_copy_string(strp, argv[6], charleft);
  4095. charleft -= strlen(strp);
  4096. strp += strlen(strp);
  4097. for(i=7; i<argc; i++) {
  4098. ast_copy_string(strp++, " ", charleft--);
  4099. ast_copy_string(strp, argv[i], charleft);
  4100. charleft -= strlen(strp);
  4101. strp += strlen(strp);
  4102. }
  4103. send_displayprinotify(d, text_buf, "", timeout, priority);
  4104. }
  4105. }
  4106. AST_LIST_UNLOCK(&devices);
  4107. return CLI_SUCCESS;
  4108. }
  4109. /*! \brief Handle sending messages to devices. */
  4110. static char *handle_skinny_message_set(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
  4111. {
  4112. switch (cmd) {
  4113. case CLI_INIT:
  4114. e->command = "skinny message set";
  4115. e->usage =
  4116. "Usage: skinny message set <device> <priority> <timeout> <message>\n"
  4117. " Set the current priority level message on a device.\n";
  4118. return NULL;
  4119. case CLI_GENERATE:
  4120. return complete_skinny_show_device(a->line, a->word, a->pos, a->n);
  4121. }
  4122. return _skinny_message_set(0, a->fd, NULL, NULL, a->argc, a->argv);
  4123. }
  4124. static char *_skinny_message_clear(int type, int fd, struct mansession *s, const struct message *m, int argc, const char * const *argv)
  4125. {
  4126. struct skinny_device *d;
  4127. if (argc != 5) {
  4128. return CLI_SHOWUSAGE;
  4129. }
  4130. AST_LIST_LOCK(&devices);
  4131. AST_LIST_TRAVERSE(&devices, d, list) {
  4132. if (!strcasecmp(argv[3], d->name)) {
  4133. int priority = atoi(argv[4]);
  4134. transmit_clearprinotify(d, priority);
  4135. }
  4136. }
  4137. AST_LIST_UNLOCK(&devices);
  4138. return CLI_SUCCESS;
  4139. }
  4140. /*! \brief Handle clearing messages to devices. */
  4141. static char *handle_skinny_message_clear(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
  4142. {
  4143. switch (cmd) {
  4144. case CLI_INIT:
  4145. e->command = "skinny message clear";
  4146. e->usage =
  4147. "Usage: skinny message clear <device> <priority>\n"
  4148. " Clear the current priority level message on device.\n";
  4149. return NULL;
  4150. case CLI_GENERATE:
  4151. return complete_skinny_show_device(a->line, a->word, a->pos, a->n);
  4152. }
  4153. return _skinny_message_clear(0, a->fd, NULL, NULL, a->argc, a->argv);
  4154. }
  4155. static struct ast_cli_entry cli_skinny[] = {
  4156. AST_CLI_DEFINE(handle_skinny_show_devices, "List defined Skinny devices"),
  4157. AST_CLI_DEFINE(handle_skinny_show_device, "List Skinny device information"),
  4158. AST_CLI_DEFINE(handle_skinny_show_lines, "List defined Skinny lines per device"),
  4159. AST_CLI_DEFINE(handle_skinny_show_line, "List Skinny line information"),
  4160. AST_CLI_DEFINE(handle_skinny_show_settings, "List global Skinny settings"),
  4161. #ifdef AST_DEVMODE
  4162. AST_CLI_DEFINE(handle_skinny_set_debug, "Enable/Disable Skinny debugging"),
  4163. #endif
  4164. AST_CLI_DEFINE(handle_skinny_reset, "Reset Skinny device(s)"),
  4165. AST_CLI_DEFINE(handle_skinny_reload, "Reload Skinny config"),
  4166. AST_CLI_DEFINE(handle_skinny_message_set, "Send message to devices"),
  4167. AST_CLI_DEFINE(handle_skinny_message_clear, "Clear message to devices"),
  4168. };
  4169. static void start_rtp(struct skinny_subchannel *sub)
  4170. {
  4171. struct skinny_line *l = sub->line;
  4172. struct skinny_device *d = l->device;
  4173. #if 0
  4174. int hasvideo = 0;
  4175. #endif
  4176. struct ast_sockaddr bindaddr_tmp;
  4177. skinny_locksub(sub);
  4178. SKINNY_DEBUG(DEBUG_AUDIO, 3, "Sub %u - Starting RTP\n", sub->callid);
  4179. ast_sockaddr_from_sin(&bindaddr_tmp, &bindaddr);
  4180. sub->rtp = ast_rtp_instance_new("asterisk", sched, &bindaddr_tmp, NULL);
  4181. #if 0
  4182. if (hasvideo)
  4183. sub->vrtp = ast_rtp_instance_new("asterisk", sched, &bindaddr_tmp, NULL);
  4184. #endif
  4185. if (sub->rtp) {
  4186. ast_rtp_instance_set_prop(sub->rtp, AST_RTP_PROPERTY_RTCP, 1);
  4187. }
  4188. if (sub->vrtp) {
  4189. ast_rtp_instance_set_prop(sub->vrtp, AST_RTP_PROPERTY_RTCP, 1);
  4190. }
  4191. if (sub->rtp && sub->owner) {
  4192. ast_rtp_instance_set_channel_id(sub->rtp, ast_channel_uniqueid(sub->owner));
  4193. ast_channel_set_fd(sub->owner, 0, ast_rtp_instance_fd(sub->rtp, 0));
  4194. ast_channel_set_fd(sub->owner, 1, ast_rtp_instance_fd(sub->rtp, 1));
  4195. }
  4196. #if 0
  4197. if (hasvideo && sub->vrtp && sub->owner) {
  4198. ast_rtp_instance_set_channel_id(sub->vrtp, ast_channel_uniqueid(sub->owner));
  4199. ast_channel_set_fd(sub->owner, 2, ast_rtp_instance_fd(sub->vrtp, 0));
  4200. ast_channel_set_fd(sub->owner, 3, ast_rtp_instance_fd(sub->vrtp, 1));
  4201. }
  4202. #endif
  4203. if (sub->rtp) {
  4204. ast_rtp_instance_set_qos(sub->rtp, qos.tos_audio, qos.cos_audio, "Skinny RTP");
  4205. ast_rtp_instance_set_prop(sub->rtp, AST_RTP_PROPERTY_NAT, l->nat);
  4206. /* Set frame packetization */
  4207. ast_rtp_codecs_set_framing(ast_rtp_instance_get_codecs(sub->rtp),
  4208. ast_format_cap_get_framing(l->cap));
  4209. }
  4210. if (sub->vrtp) {
  4211. ast_rtp_instance_set_qos(sub->vrtp, qos.tos_video, qos.cos_video, "Skinny VRTP");
  4212. ast_rtp_instance_set_prop(sub->vrtp, AST_RTP_PROPERTY_NAT, l->nat);
  4213. }
  4214. /* Create the RTP connection */
  4215. transmit_connect(d, sub);
  4216. skinny_unlocksub(sub);
  4217. }
  4218. static void destroy_rtp(struct skinny_subchannel *sub)
  4219. {
  4220. if (sub->rtp) {
  4221. SKINNY_DEBUG(DEBUG_AUDIO, 3, "Sub %u - Destroying RTP\n", sub->callid);
  4222. ast_rtp_instance_stop(sub->rtp);
  4223. ast_rtp_instance_destroy(sub->rtp);
  4224. sub->rtp = NULL;
  4225. }
  4226. if (sub->vrtp) {
  4227. SKINNY_DEBUG(DEBUG_AUDIO, 3, "Sub %u - Destroying VRTP\n", sub->callid);
  4228. ast_rtp_instance_stop(sub->vrtp);
  4229. ast_rtp_instance_destroy(sub->vrtp);
  4230. sub->vrtp = NULL;
  4231. }
  4232. }
  4233. static void *skinny_newcall(void *data)
  4234. {
  4235. struct ast_channel *c = data;
  4236. struct skinny_subchannel *sub = ast_channel_tech_pvt(c);
  4237. struct skinny_line *l = sub->line;
  4238. struct skinny_device *d = l->device;
  4239. int res = 0;
  4240. ast_channel_lock(c);
  4241. ast_set_callerid(c,
  4242. l->hidecallerid ? "" : l->cid_num,
  4243. l->hidecallerid ? "" : l->cid_name,
  4244. ast_channel_caller(c)->ani.number.valid ? NULL : l->cid_num);
  4245. #if 1 /* XXX This code is probably not necessary */
  4246. ast_party_number_free(&ast_channel_connected(c)->id.number);
  4247. ast_party_number_init(&ast_channel_connected(c)->id.number);
  4248. ast_channel_connected(c)->id.number.valid = 1;
  4249. ast_channel_connected(c)->id.number.str = ast_strdup(ast_channel_exten(c));
  4250. ast_party_name_free(&ast_channel_connected(c)->id.name);
  4251. ast_party_name_init(&ast_channel_connected(c)->id.name);
  4252. #endif
  4253. ast_setstate(c, AST_STATE_RING);
  4254. ast_channel_unlock(c);
  4255. if (!sub->rtp) {
  4256. start_rtp(sub);
  4257. }
  4258. ast_verb(3, "Sub %u - Calling %s@%s\n", sub->callid, ast_channel_exten(c), ast_channel_context(c));
  4259. res = ast_pbx_run(c);
  4260. if (res) {
  4261. ast_log(LOG_WARNING, "PBX exited non-zero\n");
  4262. transmit_start_tone(d, SKINNY_REORDER, l->instance, sub->callid);
  4263. }
  4264. return NULL;
  4265. }
  4266. static void skinny_dialer(struct skinny_subchannel *sub, int timedout)
  4267. {
  4268. struct ast_channel *c = sub->owner;
  4269. struct skinny_line *l = sub->line;
  4270. struct skinny_device *d = l->device;
  4271. if (timedout || !ast_matchmore_extension(c, ast_channel_context(c), sub->exten, 1, l->cid_num)) {
  4272. SKINNY_DEBUG(DEBUG_SUB, 3, "Sub %u - Force dialing '%s' because of %s\n", sub->callid, sub->exten, (timedout ? "timeout" : "exactmatch"));
  4273. if (ast_exists_extension(c, ast_channel_context(c), sub->exten, 1, l->cid_num)) {
  4274. if (sub->substate == SUBSTATE_OFFHOOK) {
  4275. dialandactivatesub(sub, sub->exten);
  4276. }
  4277. } else {
  4278. if (d->hookstate == SKINNY_OFFHOOK) {
  4279. // FIXME: redundant because below will onhook before the sound plays, but it correct to send it.
  4280. transmit_start_tone(d, SKINNY_REORDER, l->instance, sub->callid);
  4281. }
  4282. dumpsub(sub, 0);
  4283. }
  4284. } else {
  4285. SKINNY_DEBUG(DEBUG_SUB, 3, "Sub %u - Wait for more digits\n", sub->callid);
  4286. if (ast_exists_extension(c, ast_channel_context(c), sub->exten, 1, l->cid_num)) {
  4287. transmit_selectsoftkeys(d, l->instance, sub->callid, KEYDEF_DADFD, KEYMASK_ALL);
  4288. sub->dialer_sched = skinny_sched_add(matchdigittimeout, skinny_dialer_cb, sub);
  4289. } else {
  4290. sub->dialer_sched = skinny_sched_add(gendigittimeout, skinny_dialer_cb, sub);
  4291. }
  4292. }
  4293. }
  4294. static int skinny_dialer_cb(const void *data)
  4295. {
  4296. struct skinny_subchannel *sub = (struct skinny_subchannel *)data;
  4297. SKINNY_DEBUG(DEBUG_SUB, 3, "Sub %u - Dialer called from SCHED %d\n", sub->callid, sub->dialer_sched);
  4298. sub->dialer_sched = -1;
  4299. skinny_dialer(sub, 1);
  4300. return 0;
  4301. }
  4302. static int skinny_autoanswer_cb(const void *data)
  4303. {
  4304. struct skinny_subchannel *sub = (struct skinny_subchannel *)data;
  4305. skinny_locksub(sub);
  4306. sub->aa_sched = -1;
  4307. setsubstate(sub, SKINNY_CONNECTED);
  4308. skinny_unlocksub(sub);
  4309. return 0;
  4310. }
  4311. static int skinny_cfwd_cb(const void *data)
  4312. {
  4313. struct skinny_subchannel *sub = (struct skinny_subchannel *)data;
  4314. struct skinny_line *l = sub->line;
  4315. sub->cfwd_sched = -1;
  4316. SKINNY_DEBUG(DEBUG_SUB, 3, "Sub %u - CFWDNOANS to %s.\n", sub->callid, l->call_forward_noanswer);
  4317. ast_channel_call_forward_set(sub->owner, l->call_forward_noanswer);
  4318. ast_queue_control(sub->owner, AST_CONTROL_REDIRECTING);
  4319. return 0;
  4320. }
  4321. static int skinny_call(struct ast_channel *ast, const char *dest, int timeout)
  4322. {
  4323. int res = 0;
  4324. struct skinny_subchannel *sub = ast_channel_tech_pvt(ast);
  4325. struct skinny_line *l = sub->line;
  4326. struct skinny_device *d = l->device;
  4327. struct ast_var_t *current;
  4328. int doautoanswer = 0;
  4329. if (!d || !d->session) {
  4330. ast_log(LOG_WARNING, "Device not registered, cannot call %s\n", dest);
  4331. return -1;
  4332. }
  4333. if ((ast_channel_state(ast) != AST_STATE_DOWN) && (ast_channel_state(ast) != AST_STATE_RESERVED)) {
  4334. ast_log(LOG_WARNING, "skinny_call called on %s, neither down nor reserved\n", ast_channel_name(ast));
  4335. return -1;
  4336. }
  4337. SKINNY_DEBUG(DEBUG_SUB, 3, "Skinny Call (%s) - Sub %u\n",
  4338. ast_channel_name(ast), sub->callid);
  4339. if (l->dnd) {
  4340. ast_queue_control(ast, AST_CONTROL_BUSY);
  4341. return -1;
  4342. }
  4343. if (AST_LIST_NEXT(sub,list) && !l->callwaiting) {
  4344. ast_queue_control(ast, AST_CONTROL_BUSY);
  4345. return -1;
  4346. }
  4347. skinny_locksub(sub);
  4348. AST_LIST_TRAVERSE(ast_channel_varshead(ast), current, entries) {
  4349. if (!(strcmp(ast_var_name(current), "SKINNY_AUTOANSWER"))) {
  4350. if (d->hookstate == SKINNY_ONHOOK && sub->aa_sched < 0) {
  4351. char buf[24];
  4352. int aatime;
  4353. char *stringp = buf, *curstr;
  4354. ast_copy_string(buf, ast_var_value(current), sizeof(buf));
  4355. curstr = strsep(&stringp, ":");
  4356. aatime = atoi(curstr);
  4357. while ((curstr = strsep(&stringp, ":"))) {
  4358. if (!(strcasecmp(curstr,"BEEP"))) {
  4359. sub->aa_beep = 1;
  4360. } else if (!(strcasecmp(curstr,"MUTE"))) {
  4361. sub->aa_mute = 1;
  4362. }
  4363. }
  4364. SKINNY_DEBUG(DEBUG_SUB, 3, "Sub %u - setting autoanswer time=%dms %s%s\n",
  4365. sub->callid, aatime, sub->aa_beep ? "BEEP " : "", sub->aa_mute ? "MUTE" : "");
  4366. if (aatime) {
  4367. //sub->aa_sched = ast_sched_add(sched, aatime, skinny_autoanswer_cb, sub);
  4368. sub->aa_sched = skinny_sched_add(aatime, skinny_autoanswer_cb, sub);
  4369. } else {
  4370. doautoanswer = 1;
  4371. }
  4372. }
  4373. }
  4374. }
  4375. setsubstate(sub, SUBSTATE_RINGIN);
  4376. if (doautoanswer) {
  4377. setsubstate(sub, SUBSTATE_CONNECTED);
  4378. }
  4379. skinny_unlocksub(sub);
  4380. return res;
  4381. }
  4382. static int skinny_hangup(struct ast_channel *ast)
  4383. {
  4384. struct skinny_subchannel *sub = ast_channel_tech_pvt(ast);
  4385. if (!sub) {
  4386. ast_debug(1, "Asked to hangup channel not connected\n");
  4387. return 0;
  4388. }
  4389. dumpsub(sub, 1);
  4390. SKINNY_DEBUG(DEBUG_SUB, 3, "Sub %u - Destroying\n", sub->callid);
  4391. skinny_set_owner(sub, NULL);
  4392. ast_channel_tech_pvt_set(ast, NULL);
  4393. destroy_rtp(sub);
  4394. ast_free(sub->origtonum);
  4395. ast_free(sub->origtoname);
  4396. ast_free(sub);
  4397. ast_module_unref(ast_module_info->self);
  4398. return 0;
  4399. }
  4400. static int skinny_answer(struct ast_channel *ast)
  4401. {
  4402. int res = 0;
  4403. struct skinny_subchannel *sub = ast_channel_tech_pvt(ast);
  4404. sub->cxmode = SKINNY_CX_SENDRECV;
  4405. SKINNY_DEBUG(DEBUG_SUB, 3, "skinny_answer(%s) on %s@%s-%u\n",
  4406. ast_channel_name(ast), sub->line->name, sub->line->device->name, sub->callid);
  4407. setsubstate(sub, SUBSTATE_CONNECTED);
  4408. return res;
  4409. }
  4410. /* Retrieve audio/etc from channel. Assumes sub->lock is already held. */
  4411. static struct ast_frame *skinny_rtp_read(struct skinny_subchannel *sub)
  4412. {
  4413. struct ast_channel *ast = sub->owner;
  4414. struct ast_frame *f;
  4415. if (!sub->rtp) {
  4416. /* We have no RTP allocated for this channel */
  4417. return &ast_null_frame;
  4418. }
  4419. switch(ast_channel_fdno(ast)) {
  4420. case 0:
  4421. f = ast_rtp_instance_read(sub->rtp, 0); /* RTP Audio */
  4422. break;
  4423. case 1:
  4424. f = ast_rtp_instance_read(sub->rtp, 1); /* RTCP Control Channel */
  4425. break;
  4426. case 2:
  4427. f = ast_rtp_instance_read(sub->vrtp, 0); /* RTP Video */
  4428. break;
  4429. case 3:
  4430. f = ast_rtp_instance_read(sub->vrtp, 1); /* RTCP Control Channel for video */
  4431. break;
  4432. #if 0
  4433. case 5:
  4434. /* Not yet supported */
  4435. f = ast_udptl_read(sub->udptl); /* UDPTL for T.38 */
  4436. break;
  4437. #endif
  4438. default:
  4439. f = &ast_null_frame;
  4440. }
  4441. if (ast) {
  4442. /* We already hold the channel lock */
  4443. if (f->frametype == AST_FRAME_VOICE) {
  4444. if (ast_format_cap_iscompatible_format(ast_channel_nativeformats(ast), f->subclass.format) == AST_FORMAT_CMP_NOT_EQUAL) {
  4445. struct ast_format_cap *caps;
  4446. ast_debug(1, "Oooh, format changed to %s\n", ast_format_get_name(f->subclass.format));
  4447. caps = ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_DEFAULT);
  4448. if (caps) {
  4449. ast_format_cap_append(caps, f->subclass.format, 0);
  4450. ast_channel_nativeformats_set(ast, caps);
  4451. ao2_ref(caps, -1);
  4452. }
  4453. ast_set_read_format(ast, ast_channel_readformat(ast));
  4454. ast_set_write_format(ast, ast_channel_writeformat(ast));
  4455. }
  4456. }
  4457. }
  4458. return f;
  4459. }
  4460. static struct ast_frame *skinny_read(struct ast_channel *ast)
  4461. {
  4462. struct ast_frame *fr;
  4463. struct skinny_subchannel *sub = ast_channel_tech_pvt(ast);
  4464. skinny_locksub(sub);
  4465. fr = skinny_rtp_read(sub);
  4466. skinny_unlocksub(sub);
  4467. return fr;
  4468. }
  4469. static int skinny_write(struct ast_channel *ast, struct ast_frame *frame)
  4470. {
  4471. struct skinny_subchannel *sub = ast_channel_tech_pvt(ast);
  4472. int res = 0;
  4473. if (frame->frametype != AST_FRAME_VOICE) {
  4474. if (frame->frametype == AST_FRAME_IMAGE) {
  4475. return 0;
  4476. } else {
  4477. ast_log(LOG_WARNING, "Can't send %u type frames with skinny_write\n", frame->frametype);
  4478. return 0;
  4479. }
  4480. } else {
  4481. if (ast_format_cap_iscompatible_format(ast_channel_nativeformats(ast), frame->subclass.format) == AST_FORMAT_CMP_NOT_EQUAL) {
  4482. struct ast_str *codec_buf = ast_str_alloca(AST_FORMAT_CAP_NAMES_LEN);
  4483. ast_log(LOG_WARNING, "Asked to transmit frame type %s, while native formats is %s (read/write = %s/%s)\n",
  4484. ast_format_get_name(frame->subclass.format),
  4485. ast_format_cap_get_names(ast_channel_nativeformats(ast), &codec_buf),
  4486. ast_format_get_name(ast_channel_readformat(ast)),
  4487. ast_format_get_name(ast_channel_writeformat(ast)));
  4488. return -1;
  4489. }
  4490. }
  4491. if (sub) {
  4492. skinny_locksub(sub);
  4493. if (sub->rtp) {
  4494. res = ast_rtp_instance_write(sub->rtp, frame);
  4495. }
  4496. skinny_unlocksub(sub);
  4497. }
  4498. return res;
  4499. }
  4500. static int skinny_fixup(struct ast_channel *oldchan, struct ast_channel *newchan)
  4501. {
  4502. struct skinny_subchannel *sub = ast_channel_tech_pvt(newchan);
  4503. ast_log(LOG_NOTICE, "skinny_fixup(%s, %s)\n", ast_channel_name(oldchan), ast_channel_name(newchan));
  4504. if (sub->owner != oldchan) {
  4505. ast_log(LOG_WARNING, "old channel wasn't %p but was %p\n", oldchan, sub->owner);
  4506. return -1;
  4507. }
  4508. skinny_set_owner(sub, newchan);
  4509. return 0;
  4510. }
  4511. static int skinny_senddigit_begin(struct ast_channel *ast, char digit)
  4512. {
  4513. return -1; /* Start inband indications */
  4514. }
  4515. static int skinny_senddigit_end(struct ast_channel *ast, char digit, unsigned int duration)
  4516. {
  4517. #if 0
  4518. struct skinny_subchannel *sub = ast->tech_pvt;
  4519. struct skinny_line *l = sub->line;
  4520. struct skinny_device *d = l->device;
  4521. int tmp;
  4522. /* not right */
  4523. sprintf(tmp, "%d", digit);
  4524. //transmit_tone(d, digit, l->instance, sub->callid);
  4525. #endif
  4526. return -1; /* Stop inband indications */
  4527. }
  4528. static int get_devicestate(struct skinny_line *l)
  4529. {
  4530. struct skinny_subchannel *sub;
  4531. int res = AST_DEVICE_UNKNOWN;
  4532. if (!l)
  4533. res = AST_DEVICE_INVALID;
  4534. else if (!l->device || !l->device->session)
  4535. res = AST_DEVICE_UNAVAILABLE;
  4536. else if (l->dnd)
  4537. res = AST_DEVICE_BUSY;
  4538. else {
  4539. if (l->device->hookstate == SKINNY_ONHOOK) {
  4540. res = AST_DEVICE_NOT_INUSE;
  4541. } else {
  4542. res = AST_DEVICE_INUSE;
  4543. }
  4544. AST_LIST_TRAVERSE(&l->sub, sub, list) {
  4545. if (sub->substate == SUBSTATE_HOLD) {
  4546. res = AST_DEVICE_ONHOLD;
  4547. break;
  4548. }
  4549. }
  4550. }
  4551. return res;
  4552. }
  4553. static char *control2str(int ind) {
  4554. char *tmp;
  4555. switch (ind) {
  4556. case AST_CONTROL_HANGUP:
  4557. return "Other end has hungup";
  4558. case AST_CONTROL_RING:
  4559. return "Local ring";
  4560. case AST_CONTROL_RINGING:
  4561. return "Remote end is ringing";
  4562. case AST_CONTROL_ANSWER:
  4563. return "Remote end has answered";
  4564. case AST_CONTROL_BUSY:
  4565. return "Remote end is busy";
  4566. case AST_CONTROL_TAKEOFFHOOK:
  4567. return "Make it go off hook";
  4568. case AST_CONTROL_OFFHOOK:
  4569. return "Line is off hook";
  4570. case AST_CONTROL_CONGESTION:
  4571. return "Congestion (circuits busy)";
  4572. case AST_CONTROL_FLASH:
  4573. return "Flash hook";
  4574. case AST_CONTROL_WINK:
  4575. return "Wink";
  4576. case AST_CONTROL_OPTION:
  4577. return "Set a low-level option";
  4578. case AST_CONTROL_RADIO_KEY:
  4579. return "Key Radio";
  4580. case AST_CONTROL_RADIO_UNKEY:
  4581. return "Un-Key Radio";
  4582. case AST_CONTROL_PROGRESS:
  4583. return "Remote end is making Progress";
  4584. case AST_CONTROL_PROCEEDING:
  4585. return "Remote end is proceeding";
  4586. case AST_CONTROL_HOLD:
  4587. return "Hold";
  4588. case AST_CONTROL_UNHOLD:
  4589. return "Unhold";
  4590. case AST_CONTROL_VIDUPDATE:
  4591. return "VidUpdate";
  4592. case AST_CONTROL_SRCUPDATE:
  4593. return "Media Source Update";
  4594. case AST_CONTROL_TRANSFER:
  4595. return "Transfer";
  4596. case AST_CONTROL_CONNECTED_LINE:
  4597. return "Connected Line";
  4598. case AST_CONTROL_REDIRECTING:
  4599. return "Redirecting";
  4600. case AST_CONTROL_T38_PARAMETERS:
  4601. return "T38_Parameters";
  4602. case AST_CONTROL_CC:
  4603. return "CC Not Possible";
  4604. case AST_CONTROL_SRCCHANGE:
  4605. return "Media Source Change";
  4606. case AST_CONTROL_INCOMPLETE:
  4607. return "Incomplete";
  4608. case -1:
  4609. return "Stop tone";
  4610. default:
  4611. if (!(tmp = ast_threadstorage_get(&control2str_threadbuf, CONTROL2STR_BUFSIZE)))
  4612. return "Unknown";
  4613. snprintf(tmp, CONTROL2STR_BUFSIZE, "UNKNOWN-%d", ind);
  4614. return tmp;
  4615. }
  4616. }
  4617. static void skinny_transfer_attended(struct skinny_subchannel *sub)
  4618. {
  4619. struct skinny_subchannel *xferee;
  4620. struct skinny_subchannel *xferor;
  4621. enum ast_transfer_result res;
  4622. if (sub->xferor) {
  4623. xferor = sub;
  4624. xferee = sub->related;
  4625. } else {
  4626. xferor = sub;
  4627. xferee = sub->related;
  4628. }
  4629. ast_queue_control(xferee->owner, AST_CONTROL_UNHOLD);
  4630. if (ast_channel_state(xferor->owner) == AST_STATE_RINGING) {
  4631. ast_queue_control(xferee->owner, AST_CONTROL_RINGING);
  4632. }
  4633. res = ast_bridge_transfer_attended(xferee->owner, xferor->owner);
  4634. if (res != AST_BRIDGE_TRANSFER_SUCCESS) {
  4635. SKINNY_DEBUG(DEBUG_SUB, 3, "Sub %u failed to transfer %u to '%s'@'%s' - %u\n",
  4636. xferor->callid, xferee->callid, xferor->exten, xferor->line->context, res);
  4637. send_displayprinotify(xferor->line->device, "Transfer failed", NULL, 10, 5);
  4638. ast_queue_control(xferee->owner, AST_CONTROL_HOLD);
  4639. }
  4640. }
  4641. static void skinny_transfer_blind(struct skinny_subchannel *sub)
  4642. {
  4643. struct skinny_subchannel *xferee = sub->related;
  4644. enum ast_transfer_result res;
  4645. sub->related = NULL;
  4646. xferee->related = NULL;
  4647. ast_queue_control(xferee->owner, AST_CONTROL_UNHOLD);
  4648. res = ast_bridge_transfer_blind(1, xferee->owner, sub->exten, sub->line->context, NULL, NULL);
  4649. if (res != AST_BRIDGE_TRANSFER_SUCCESS) {
  4650. SKINNY_DEBUG(DEBUG_SUB, 3, "Sub %u failed to blind transfer %u to '%s'@'%s' - %u\n",
  4651. sub->callid, xferee->callid, sub->exten, sub->line->context, res);
  4652. send_displayprinotify(sub->line->device, "Transfer failed", NULL, 10, 5);
  4653. ast_queue_control(xferee->owner, AST_CONTROL_HOLD);
  4654. }
  4655. dumpsub(sub, 1);
  4656. }
  4657. static int skinny_indicate(struct ast_channel *ast, int ind, const void *data, size_t datalen)
  4658. {
  4659. struct skinny_subchannel *sub = ast_channel_tech_pvt(ast);
  4660. struct skinny_line *l = sub->line;
  4661. struct skinny_device *d = l->device;
  4662. struct skinnysession *s = d->session;
  4663. if (!s) {
  4664. ast_log(LOG_NOTICE, "Asked to indicate '%s' condition on channel %s, but session does not exist.\n", control2str(ind), ast_channel_name(ast));
  4665. return -1;
  4666. }
  4667. SKINNY_DEBUG(DEBUG_SUB, 3, "Asked to indicate '%s' condition on channel %s (Sub %u)\n",
  4668. control2str(ind), ast_channel_name(ast), sub->callid);
  4669. switch(ind) {
  4670. case AST_CONTROL_RINGING:
  4671. setsubstate(sub, SUBSTATE_RINGOUT);
  4672. return (d->earlyrtp ? -1 : 0); /* Tell asterisk to provide inband signalling if rtp started */
  4673. case AST_CONTROL_BUSY:
  4674. setsubstate(sub, SUBSTATE_BUSY);
  4675. return (d->earlyrtp ? -1 : 0); /* Tell asterisk to provide inband signalling if rtp started */
  4676. case AST_CONTROL_INCOMPLETE:
  4677. /* Support for incomplete not supported for chan_skinny; treat as congestion */
  4678. case AST_CONTROL_CONGESTION:
  4679. setsubstate(sub, SUBSTATE_CONGESTION);
  4680. return (d->earlyrtp ? -1 : 0); /* Tell asterisk to provide inband signalling if rtp started */
  4681. case AST_CONTROL_PROGRESS:
  4682. setsubstate(sub, SUBSTATE_PROGRESS);
  4683. return (d->earlyrtp ? -1 : 0); /* Tell asterisk to provide inband signalling if rtp started */
  4684. case -1: /* STOP_TONE */
  4685. transmit_stop_tone(d, l->instance, sub->callid);
  4686. break;
  4687. case AST_CONTROL_HOLD:
  4688. ast_moh_start(ast, data, l->mohinterpret);
  4689. break;
  4690. case AST_CONTROL_UNHOLD:
  4691. ast_moh_stop(ast);
  4692. break;
  4693. case AST_CONTROL_PROCEEDING:
  4694. break;
  4695. case AST_CONTROL_SRCUPDATE:
  4696. if (sub->rtp) {
  4697. ast_rtp_instance_update_source(sub->rtp);
  4698. }
  4699. break;
  4700. case AST_CONTROL_SRCCHANGE:
  4701. if (sub->rtp) {
  4702. ast_rtp_instance_change_source(sub->rtp);
  4703. }
  4704. break;
  4705. case AST_CONTROL_CONNECTED_LINE:
  4706. update_connectedline(sub, data, datalen);
  4707. break;
  4708. default:
  4709. ast_log(LOG_WARNING, "Don't know how to indicate condition %d\n", ind);
  4710. /* fallthrough */
  4711. case AST_CONTROL_PVT_CAUSE_CODE:
  4712. case AST_CONTROL_MASQUERADE_NOTIFY:
  4713. return -1; /* Tell asterisk to provide inband signalling */
  4714. }
  4715. return 0;
  4716. }
  4717. static void skinny_set_owner(struct skinny_subchannel* sub, struct ast_channel* chan)
  4718. {
  4719. sub->owner = chan;
  4720. if (sub->rtp) {
  4721. ast_rtp_instance_set_channel_id(sub->rtp, sub->owner ? ast_channel_uniqueid(sub->owner) : "");
  4722. }
  4723. if (sub->vrtp) {
  4724. ast_rtp_instance_set_channel_id(sub->vrtp, sub->owner ? ast_channel_uniqueid(sub->owner) : "");
  4725. }
  4726. }
  4727. static struct ast_channel *skinny_new(struct skinny_line *l, struct skinny_subline *subline, int state, const struct ast_assigned_ids *assignedids, const struct ast_channel *requestor, int direction)
  4728. {
  4729. struct ast_channel *tmp;
  4730. struct skinny_subchannel *sub;
  4731. struct skinny_device *d = l->device;
  4732. struct ast_variable *v = NULL;
  4733. struct ast_format *tmpfmt;
  4734. struct ast_format_cap *caps;
  4735. #ifdef AST_DEVMODE
  4736. struct ast_str *codec_buf = ast_str_alloca(AST_FORMAT_CAP_NAMES_LEN);
  4737. #endif
  4738. if (!l->device || !l->device->session) {
  4739. ast_log(LOG_WARNING, "Device for line %s is not registered.\n", l->name);
  4740. return NULL;
  4741. }
  4742. caps = ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_DEFAULT);
  4743. if (!caps) {
  4744. return NULL;
  4745. }
  4746. tmp = ast_channel_alloc(1, state, l->cid_num, l->cid_name, l->accountcode, l->exten, l->context, assignedids, requestor, l->amaflags, "Skinny/%s@%s-%d", l->name, d->name, callnums);
  4747. if (!tmp) {
  4748. ast_log(LOG_WARNING, "Unable to allocate channel structure\n");
  4749. ao2_ref(caps, -1);
  4750. return NULL;
  4751. } else {
  4752. sub = ast_calloc(1, sizeof(*sub));
  4753. if (!sub) {
  4754. ast_log(LOG_WARNING, "Unable to allocate Skinny subchannel\n");
  4755. ast_channel_unlock(tmp);
  4756. ast_channel_unref(tmp);
  4757. ao2_ref(caps, -1);
  4758. return NULL;
  4759. } else {
  4760. skinny_set_owner(sub, tmp);
  4761. sub->callid = callnums++;
  4762. d->lastlineinstance = l->instance;
  4763. d->lastcallreference = sub->callid;
  4764. sub->cxmode = SKINNY_CX_INACTIVE;
  4765. sub->nat = l->nat;
  4766. sub->line = l;
  4767. sub->blindxfer = 0;
  4768. sub->xferor = 0;
  4769. sub->related = NULL;
  4770. sub->calldirection = direction;
  4771. sub->aa_sched = -1;
  4772. sub->dialer_sched = -1;
  4773. sub->cfwd_sched = -1;
  4774. sub->dialType = DIALTYPE_NORMAL;
  4775. sub->getforward = 0;
  4776. if (subline) {
  4777. sub->subline = subline;
  4778. subline->sub = sub;
  4779. } else {
  4780. sub->subline = NULL;
  4781. }
  4782. AST_LIST_INSERT_HEAD(&l->sub, sub, list);
  4783. //l->activesub = sub;
  4784. }
  4785. ast_channel_stage_snapshot(tmp);
  4786. ast_channel_tech_set(tmp, &skinny_tech);
  4787. ast_channel_tech_pvt_set(tmp, sub);
  4788. if (!ast_format_cap_count(l->cap)) {
  4789. ast_format_cap_append_from_cap(caps, l->cap, AST_MEDIA_TYPE_UNKNOWN);
  4790. } else {
  4791. ast_format_cap_append_from_cap(caps, default_cap, AST_MEDIA_TYPE_UNKNOWN);
  4792. }
  4793. ast_channel_nativeformats_set(tmp, caps);
  4794. ao2_ref(caps, -1);
  4795. tmpfmt = ast_format_cap_get_format(ast_channel_nativeformats(tmp), 0);
  4796. SKINNY_DEBUG(DEBUG_SUB, 3, "skinny_new: tmp->nativeformats=%s fmt=%s\n",
  4797. ast_format_cap_get_names(ast_channel_nativeformats(tmp), &codec_buf),
  4798. ast_format_get_name(tmpfmt));
  4799. if (sub->rtp) {
  4800. ast_channel_set_fd(tmp, 0, ast_rtp_instance_fd(sub->rtp, 0));
  4801. }
  4802. if (state == AST_STATE_RING) {
  4803. ast_channel_rings_set(tmp, 1);
  4804. }
  4805. ast_channel_set_writeformat(tmp, tmpfmt);
  4806. ast_channel_set_rawwriteformat(tmp, tmpfmt);
  4807. ast_channel_set_readformat(tmp, tmpfmt);
  4808. ast_channel_set_rawreadformat(tmp, tmpfmt);
  4809. ao2_ref(tmpfmt, -1);
  4810. if (!ast_strlen_zero(l->language))
  4811. ast_channel_language_set(tmp, l->language);
  4812. if (!ast_strlen_zero(l->accountcode))
  4813. ast_channel_accountcode_set(tmp, l->accountcode);
  4814. if (!ast_strlen_zero(l->parkinglot))
  4815. ast_channel_parkinglot_set(tmp, l->parkinglot);
  4816. if (l->amaflags)
  4817. ast_channel_amaflags_set(tmp, l->amaflags);
  4818. ast_module_ref(ast_module_info->self);
  4819. ast_channel_callgroup_set(tmp, l->callgroup);
  4820. ast_channel_pickupgroup_set(tmp, l->pickupgroup);
  4821. ast_channel_named_callgroups_set(tmp, l->named_callgroups);
  4822. ast_channel_named_pickupgroups_set(tmp, l->named_pickupgroups);
  4823. if (l->cfwdtype & SKINNY_CFWD_ALL) {
  4824. SKINNY_DEBUG(DEBUG_SUB, 3, "Sub %u - CFWDALL to %s.\n", sub->callid, l->call_forward_all);
  4825. ast_channel_call_forward_set(tmp, l->call_forward_all);
  4826. } else if ((l->cfwdtype & SKINNY_CFWD_BUSY) && (get_devicestate(l) != AST_DEVICE_NOT_INUSE)) {
  4827. SKINNY_DEBUG(DEBUG_SUB, 3, "Sub %u - CFWDBUSY to %s.\n", sub->callid, l->call_forward_busy);
  4828. ast_channel_call_forward_set(tmp, l->call_forward_busy);
  4829. } else if (l->cfwdtype & SKINNY_CFWD_NOANSWER) {
  4830. SKINNY_DEBUG(DEBUG_SUB, 3, "Sub %u - CFWDNOANS Scheduling for %d seconds.\n", sub->callid, l->callfwdtimeout/1000);
  4831. sub->cfwd_sched = skinny_sched_add(l->callfwdtimeout, skinny_cfwd_cb, sub);
  4832. }
  4833. if (subline) {
  4834. ast_channel_context_set(tmp, subline->context);
  4835. } else {
  4836. ast_channel_context_set(tmp, l->context);
  4837. }
  4838. ast_channel_exten_set(tmp, l->exten);
  4839. /* Don't use ast_set_callerid() here because it will
  4840. * generate a needless NewCallerID event */
  4841. if (!ast_strlen_zero(l->cid_num)) {
  4842. ast_channel_caller(tmp)->ani.number.valid = 1;
  4843. ast_channel_caller(tmp)->ani.number.str = ast_strdup(l->cid_num);
  4844. }
  4845. ast_channel_priority_set(tmp, 1);
  4846. ast_channel_adsicpe_set(tmp, AST_ADSI_UNAVAILABLE);
  4847. if (sub->rtp)
  4848. ast_jb_configure(tmp, &global_jbconf);
  4849. /* Set channel variables for this call from configuration */
  4850. for (v = l->chanvars ; v ; v = v->next)
  4851. pbx_builtin_setvar_helper(tmp, v->name, v->value);
  4852. ast_channel_stage_snapshot_done(tmp);
  4853. ast_channel_unlock(tmp);
  4854. if (state != AST_STATE_DOWN) {
  4855. if (ast_pbx_start(tmp)) {
  4856. ast_log(LOG_WARNING, "Unable to start PBX on %s\n", ast_channel_name(tmp));
  4857. ast_hangup(tmp);
  4858. tmp = NULL;
  4859. }
  4860. }
  4861. }
  4862. return tmp;
  4863. }
  4864. static char *substate2str(int ind) {
  4865. char *tmp;
  4866. switch (ind) {
  4867. case SUBSTATE_UNSET:
  4868. return "SUBSTATE_UNSET";
  4869. case SUBSTATE_OFFHOOK:
  4870. return "SUBSTATE_OFFHOOK";
  4871. case SUBSTATE_ONHOOK:
  4872. return "SUBSTATE_ONHOOK";
  4873. case SUBSTATE_RINGOUT:
  4874. return "SUBSTATE_RINGOUT";
  4875. case SUBSTATE_RINGIN:
  4876. return "SUBSTATE_RINGIN";
  4877. case SUBSTATE_CONNECTED:
  4878. return "SUBSTATE_CONNECTED";
  4879. case SUBSTATE_BUSY:
  4880. return "SUBSTATE_BUSY";
  4881. case SUBSTATE_CONGESTION:
  4882. return "SUBSTATE_CONGESTION";
  4883. case SUBSTATE_PROGRESS:
  4884. return "SUBSTATE_PROGRESS";
  4885. case SUBSTATE_HOLD:
  4886. return "SUBSTATE_HOLD";
  4887. case SUBSTATE_CALLWAIT:
  4888. return "SUBSTATE_CALLWAIT";
  4889. case SUBSTATE_DIALING:
  4890. return "SUBSTATE_DIALING";
  4891. default:
  4892. if (!(tmp = ast_threadstorage_get(&substate2str_threadbuf, SUBSTATE2STR_BUFSIZE)))
  4893. return "Unknown";
  4894. snprintf(tmp, SUBSTATE2STR_BUFSIZE, "UNKNOWN-%d", ind);
  4895. return tmp;
  4896. }
  4897. }
  4898. static void setsubstate(struct skinny_subchannel *sub, int state)
  4899. {
  4900. struct skinny_line *l = sub->line;
  4901. struct skinny_subline *subline = sub->subline;
  4902. struct skinny_device *d = l->device;
  4903. struct ast_channel *c = sub->owner;
  4904. struct ast_party_id connected_id;
  4905. pthread_t t;
  4906. int actualstate = state;
  4907. char *fromnum;
  4908. if (sub->substate == SUBSTATE_ONHOOK) {
  4909. return;
  4910. }
  4911. skinny_locksub(sub);
  4912. if (-1 < sub->dialer_sched) {
  4913. skinny_sched_del(sub->dialer_sched, sub);
  4914. sub->dialer_sched = -1;
  4915. }
  4916. if (state != SUBSTATE_RINGIN && -1 < sub->aa_sched) {
  4917. skinny_sched_del(sub->aa_sched, sub);
  4918. sub->aa_sched = -1;
  4919. sub->aa_beep = 0;
  4920. sub->aa_mute = 0;
  4921. }
  4922. if (sub->cfwd_sched > -1) {
  4923. if (state == SUBSTATE_CONNECTED) {
  4924. if (skinny_sched_del(sub->cfwd_sched, sub)) {
  4925. SKINNY_DEBUG(DEBUG_SUB, 3, "Sub %u - trying to change state from %s to %s, but already forwarded because no answer.\n",
  4926. sub->callid, substate2str(sub->substate), substate2str(actualstate));
  4927. skinny_unlocksub(sub);
  4928. return;
  4929. }
  4930. sub->cfwd_sched = -1;
  4931. } else if (state == SUBSTATE_ONHOOK) {
  4932. skinny_sched_del(sub->cfwd_sched, sub);
  4933. sub->cfwd_sched = -1;
  4934. }
  4935. }
  4936. if ((state == SUBSTATE_RINGIN) && ((d->hookstate == SKINNY_OFFHOOK) || (AST_LIST_NEXT(AST_LIST_FIRST(&l->sub), list)))) {
  4937. actualstate = SUBSTATE_CALLWAIT;
  4938. }
  4939. if (sub->substate == SUBSTATE_RINGIN && state != SUBSTATE_RINGIN) {
  4940. transmit_clearprinotify(d, 5);
  4941. }
  4942. if ((state == SUBSTATE_CONNECTED) && (!subline) && (AST_LIST_FIRST(&l->sublines))) {
  4943. const char *slastation;
  4944. struct skinny_subline *tmpsubline;
  4945. slastation = pbx_builtin_getvar_helper(c, "SLASTATION");
  4946. ast_verb(3, "Connecting %s to subline\n", slastation);
  4947. if (slastation) {
  4948. AST_LIST_TRAVERSE(&l->sublines, tmpsubline, list) {
  4949. if (!strcasecmp(tmpsubline->stname, slastation)) {
  4950. subline = tmpsubline;
  4951. break;
  4952. }
  4953. }
  4954. if (subline) {
  4955. struct skinny_line *tmpline;
  4956. subline->sub = sub;
  4957. sub->subline = subline;
  4958. subline->callid = sub->callid;
  4959. send_callinfo(sub);
  4960. AST_LIST_TRAVERSE(&lines, tmpline, all) {
  4961. AST_LIST_TRAVERSE(&tmpline->sublines, tmpsubline, list) {
  4962. if (!(subline == tmpsubline)) {
  4963. if (!strcasecmp(subline->lnname, tmpsubline->lnname)) {
  4964. struct ast_state_cb_info info = {
  4965. .exten_state = tmpsubline->extenstate,
  4966. };
  4967. tmpsubline->callid = callnums++;
  4968. transmit_callstate(tmpsubline->line->device, tmpsubline->line->instance, tmpsubline->callid, SKINNY_OFFHOOK);
  4969. push_callinfo(tmpsubline, sub);
  4970. skinny_extensionstate_cb(NULL, NULL, &info, tmpsubline->container);
  4971. }
  4972. }
  4973. }
  4974. }
  4975. }
  4976. }
  4977. }
  4978. if (subline) { /* Different handling for subs under a subline, indications come through hints */
  4979. switch (actualstate) {
  4980. case SUBSTATE_ONHOOK:
  4981. AST_LIST_REMOVE(&l->sub, sub, list);
  4982. if (sub->related) {
  4983. sub->related->related = NULL;
  4984. }
  4985. if (sub == l->activesub) {
  4986. l->activesub = NULL;
  4987. transmit_closereceivechannel(d, sub);
  4988. transmit_stopmediatransmission(d, sub);
  4989. }
  4990. if (subline->callid) {
  4991. transmit_stop_tone(d, l->instance, sub->callid);
  4992. transmit_callstate(d, l->instance, subline->callid, SKINNY_CALLREMOTEMULTILINE);
  4993. transmit_selectsoftkeys(d, l->instance, subline->callid, KEYDEF_SLACONNECTEDNOTACTIVE, KEYMASK_ALL);
  4994. send_displaypromptstatus(d, "In Use", "", 0, l->instance, subline->callid);
  4995. }
  4996. sub->cxmode = SKINNY_CX_RECVONLY;
  4997. sub->substate = SUBSTATE_ONHOOK;
  4998. sub->substate = SUBSTATE_ONHOOK;
  4999. if (sub->owner) {
  5000. ast_queue_hangup(sub->owner);
  5001. }
  5002. break;
  5003. case SUBSTATE_CONNECTED:
  5004. transmit_activatecallplane(d, l);
  5005. transmit_stop_tone(d, l->instance, sub->callid);
  5006. transmit_selectsoftkeys(d, l->instance, subline->callid, KEYDEF_CONNECTED, KEYMASK_ALL);
  5007. transmit_callstate(d, l->instance, subline->callid, SKINNY_CONNECTED);
  5008. if (!sub->rtp) {
  5009. start_rtp(sub);
  5010. }
  5011. if (sub->substate == SUBSTATE_RINGIN || sub->substate == SUBSTATE_CALLWAIT) {
  5012. ast_queue_control(sub->owner, AST_CONTROL_ANSWER);
  5013. }
  5014. if (sub->substate == SUBSTATE_DIALING || sub->substate == SUBSTATE_RINGOUT) {
  5015. transmit_dialednumber(d, sub->exten, l->instance, sub->callid);
  5016. }
  5017. if (ast_channel_state(sub->owner) != AST_STATE_UP) {
  5018. ast_setstate(sub->owner, AST_STATE_UP);
  5019. }
  5020. sub->substate = SUBSTATE_CONNECTED;
  5021. l->activesub = sub;
  5022. break;
  5023. case SUBSTATE_HOLD:
  5024. if (sub->substate != SUBSTATE_CONNECTED) {
  5025. ast_log(LOG_WARNING, "Cannot set substate to SUBSTATE_HOLD from %s (on call-%u)\n", substate2str(sub->substate), sub->callid);
  5026. return;
  5027. }
  5028. transmit_activatecallplane(d, l);
  5029. transmit_closereceivechannel(d, sub);
  5030. transmit_stopmediatransmission(d, sub);
  5031. transmit_callstate(d, l->instance, subline->callid, SKINNY_CALLREMOTEMULTILINE);
  5032. transmit_selectsoftkeys(d, l->instance, subline->callid, KEYDEF_SLACONNECTEDNOTACTIVE, KEYMASK_ALL);
  5033. send_displaypromptstatus(d, "In Use", "", 0, l->instance, subline->callid);
  5034. sub->substate = SUBSTATE_HOLD;
  5035. ast_queue_hold(sub->owner, l->mohsuggest);
  5036. break;
  5037. default:
  5038. ast_log(LOG_WARNING, "Substate handling under subline for state %d not implemented on Sub-%u\n", state, sub->callid);
  5039. }
  5040. skinny_unlocksub(sub);
  5041. return;
  5042. }
  5043. if ((d->hookstate == SKINNY_ONHOOK) && ((actualstate == SUBSTATE_OFFHOOK) || (actualstate == SUBSTATE_DIALING)
  5044. || (actualstate == SUBSTATE_RINGOUT) || (actualstate == SUBSTATE_CONNECTED) || (actualstate == SUBSTATE_BUSY)
  5045. || (actualstate == SUBSTATE_CONGESTION) || (actualstate == SUBSTATE_PROGRESS))) {
  5046. d->hookstate = SKINNY_OFFHOOK;
  5047. transmit_speaker_mode(d, SKINNY_SPEAKERON);
  5048. }
  5049. SKINNY_DEBUG(DEBUG_SUB, 3, "Sub %u - change state from %s to %s\n",
  5050. sub->callid, substate2str(sub->substate), substate2str(actualstate));
  5051. if (actualstate == sub->substate) {
  5052. skinny_unlocksub(sub);
  5053. return;
  5054. }
  5055. switch (actualstate) {
  5056. case SUBSTATE_OFFHOOK:
  5057. ast_verb(1, "Call-id: %u\n", sub->callid);
  5058. l->activesub = sub;
  5059. transmit_callstate(d, l->instance, sub->callid, SKINNY_OFFHOOK);
  5060. transmit_activatecallplane(d, l);
  5061. transmit_clear_display_message(d, l->instance, sub->callid);
  5062. transmit_start_tone(d, SKINNY_DIALTONE, l->instance, sub->callid);
  5063. transmit_selectsoftkeys(d, l->instance, sub->callid, KEYDEF_OFFHOOK, KEYMASK_ALL);
  5064. send_displaypromptstatus(d, OCTAL_ENTRNUM, "", 0, l->instance, sub->callid);
  5065. sub->substate = SUBSTATE_OFFHOOK;
  5066. sub->dialer_sched = skinny_sched_add(firstdigittimeout, skinny_dialer_cb, sub);
  5067. break;
  5068. case SUBSTATE_ONHOOK:
  5069. AST_LIST_REMOVE(&l->sub, sub, list);
  5070. if (sub->related) {
  5071. sub->related->related = NULL;
  5072. }
  5073. if ((sub->substate == SUBSTATE_RINGIN || sub->substate == SUBSTATE_CALLWAIT) && ast_channel_hangupcause(sub->owner) == AST_CAUSE_ANSWERED_ELSEWHERE) {
  5074. transmit_callstate(d, l->instance, sub->callid, SKINNY_CONNECTED);
  5075. }
  5076. if (sub == l->activesub) {
  5077. l->activesub = NULL;
  5078. transmit_closereceivechannel(d, sub);
  5079. transmit_stopmediatransmission(d, sub);
  5080. transmit_stop_tone(d, l->instance, sub->callid);
  5081. transmit_callstate(d, l->instance, sub->callid, SKINNY_ONHOOK);
  5082. transmit_clearpromptmessage(d, l->instance, sub->callid);
  5083. transmit_ringer_mode(d, SKINNY_RING_OFF);
  5084. transmit_definetimedate(d);
  5085. transmit_lamp_indication(d, STIMULUS_LINE, l->instance, SKINNY_LAMP_OFF);
  5086. } else {
  5087. transmit_stop_tone(d, l->instance, sub->callid);
  5088. transmit_callstate(d, l->instance, sub->callid, SKINNY_ONHOOK);
  5089. transmit_clearpromptmessage(d, l->instance, sub->callid);
  5090. }
  5091. sub->cxmode = SKINNY_CX_RECVONLY;
  5092. if (sub->owner) {
  5093. if (sub->substate == SUBSTATE_OFFHOOK) {
  5094. sub->substate = SUBSTATE_ONHOOK;
  5095. skinny_unlocksub(sub);
  5096. ast_hangup(sub->owner);
  5097. return;
  5098. } else {
  5099. sub->substate = SUBSTATE_ONHOOK;
  5100. ast_queue_hangup(sub->owner);
  5101. }
  5102. } else {
  5103. sub->substate = SUBSTATE_ONHOOK;
  5104. }
  5105. break;
  5106. case SUBSTATE_DIALING:
  5107. if (ast_strlen_zero(sub->exten) || !ast_exists_extension(c, ast_channel_context(c), sub->exten, 1, l->cid_num)) {
  5108. ast_log(LOG_WARNING, "Exten (%s)@(%s) does not exist, unable to set substate DIALING on sub %u\n", sub->exten, ast_channel_context(c), sub->callid);
  5109. break;
  5110. }
  5111. if (d->hookstate == SKINNY_ONHOOK) {
  5112. d->hookstate = SKINNY_OFFHOOK;
  5113. transmit_speaker_mode(d, SKINNY_SPEAKERON);
  5114. transmit_activatecallplane(d, l);
  5115. }
  5116. if (!sub->subline) {
  5117. transmit_callstate(d, l->instance, sub->callid, SKINNY_OFFHOOK);
  5118. transmit_stop_tone(d, l->instance, sub->callid);
  5119. transmit_clear_display_message(d, l->instance, sub->callid);
  5120. transmit_selectsoftkeys(d, l->instance, sub->callid, KEYDEF_RINGOUT, KEYMASK_ALL);
  5121. send_displaypromptstatus(d, "Dialing", "", 0, l->instance, sub->callid);
  5122. }
  5123. if (AST_LIST_FIRST(&l->sublines)) {
  5124. if (subline) {
  5125. ast_channel_exten_set(c, subline->exten);
  5126. ast_channel_context_set(c, "sla_stations");
  5127. } else {
  5128. pbx_builtin_setvar_helper(c, "_DESTEXTEN", sub->exten);
  5129. pbx_builtin_setvar_helper(c, "_DESTCONTEXT", ast_channel_context(c));
  5130. ast_channel_exten_set(c, l->dialoutexten);
  5131. ast_channel_context_set(c, l->dialoutcontext);
  5132. ast_copy_string(l->lastnumberdialed, sub->exten, sizeof(l->lastnumberdialed));
  5133. }
  5134. } else {
  5135. ast_channel_exten_set(c, sub->exten);
  5136. ast_copy_string(l->lastnumberdialed, sub->exten, sizeof(l->lastnumberdialed));
  5137. }
  5138. sub->substate = SUBSTATE_DIALING;
  5139. if (ast_pthread_create(&t, NULL, skinny_newcall, c)) {
  5140. ast_log(LOG_WARNING, "Unable to create new call thread: %s\n", strerror(errno));
  5141. ast_hangup(c);
  5142. }
  5143. break;
  5144. case SUBSTATE_RINGOUT:
  5145. if (!(sub->substate == SUBSTATE_DIALING || sub->substate == SUBSTATE_PROGRESS)) {
  5146. ast_log(LOG_WARNING, "Cannot set substate to SUBSTATE_RINGOUT from %s (on call-%u)\n", substate2str(sub->substate), sub->callid);
  5147. break;
  5148. }
  5149. if (sub->substate != SUBSTATE_PROGRESS) {
  5150. transmit_callstate(d, l->instance, sub->callid, SKINNY_PROGRESS);
  5151. }
  5152. if (!d->earlyrtp) {
  5153. transmit_start_tone(d, SKINNY_ALERT, l->instance, sub->callid);
  5154. }
  5155. transmit_callstate(d, l->instance, sub->callid, SKINNY_RINGOUT);
  5156. if (sub->related) {
  5157. transmit_selectsoftkeys(d, l->instance, sub->callid, KEYDEF_RINGOUTWITHTRANS, KEYMASK_ALL);
  5158. } else {
  5159. transmit_selectsoftkeys(d, l->instance, sub->callid, KEYDEF_RINGOUT, KEYMASK_ALL);
  5160. }
  5161. transmit_dialednumber(d, sub->exten, l->instance, sub->callid);
  5162. send_displaypromptstatus(d, OCTAL_RINGOUT, "", 0, l->instance, sub->callid);
  5163. send_callinfo(sub);
  5164. sub->substate = SUBSTATE_RINGOUT;
  5165. break;
  5166. case SUBSTATE_RINGIN:
  5167. connected_id = ast_channel_connected_effective_id(c);
  5168. if ((ast_party_id_presentation(&connected_id) & AST_PRES_RESTRICTION) == AST_PRES_ALLOWED) {
  5169. fromnum = S_COR(connected_id.number.valid, connected_id.number.str, "Unknown");
  5170. } else {
  5171. fromnum = "Unknown";
  5172. }
  5173. transmit_callstate(d, l->instance, sub->callid, SKINNY_RINGIN);
  5174. transmit_selectsoftkeys(d, l->instance, sub->callid, KEYDEF_RINGIN, KEYMASK_ALL);
  5175. send_displaypromptstatus(d, OCTAL_FROM, fromnum, 0, l->instance, sub->callid);
  5176. send_displayprinotify(d, OCTAL_FROM, fromnum, 10, 5);
  5177. send_callinfo(sub);
  5178. transmit_lamp_indication(d, STIMULUS_LINE, l->instance, SKINNY_LAMP_BLINK);
  5179. transmit_ringer_mode(d, SKINNY_RING_INSIDE);
  5180. transmit_activatecallplane(d, l);
  5181. if (d->hookstate == SKINNY_ONHOOK) {
  5182. l->activesub = sub;
  5183. }
  5184. if (sub->substate != SUBSTATE_RINGIN || sub->substate != SUBSTATE_CALLWAIT) {
  5185. ast_setstate(c, AST_STATE_RINGING);
  5186. ast_queue_control(c, AST_CONTROL_RINGING);
  5187. }
  5188. sub->substate = SUBSTATE_RINGIN;
  5189. break;
  5190. case SUBSTATE_CALLWAIT:
  5191. transmit_callstate(d, l->instance, sub->callid, SKINNY_RINGIN);
  5192. transmit_callstate(d, l->instance, sub->callid, SKINNY_CALLWAIT);
  5193. transmit_selectsoftkeys(d, l->instance, sub->callid, KEYDEF_RINGIN, KEYMASK_ALL);
  5194. send_displaypromptstatus(d, OCTAL_CALLWAITING, "", 0, l->instance, sub->callid);
  5195. send_callinfo(sub);
  5196. transmit_lamp_indication(d, STIMULUS_LINE, l->instance, SKINNY_LAMP_BLINK);
  5197. transmit_start_tone(d, SKINNY_CALLWAITTONE, l->instance, sub->callid);
  5198. ast_setstate(c, AST_STATE_RINGING);
  5199. ast_queue_control(c, AST_CONTROL_RINGING);
  5200. sub->substate = SUBSTATE_CALLWAIT;
  5201. break;
  5202. case SUBSTATE_CONNECTED:
  5203. if (sub->substate == SUBSTATE_RINGIN) {
  5204. transmit_callstate(d, l->instance, sub->callid, SKINNY_OFFHOOK);
  5205. }
  5206. if (sub->substate == SUBSTATE_HOLD) {
  5207. ast_queue_unhold(sub->owner);
  5208. transmit_connect(d, sub);
  5209. }
  5210. transmit_ringer_mode(d, SKINNY_RING_OFF);
  5211. transmit_activatecallplane(d, l);
  5212. transmit_stop_tone(d, l->instance, sub->callid);
  5213. send_callinfo(sub);
  5214. transmit_callstate(d, l->instance, sub->callid, SKINNY_CONNECTED);
  5215. send_displaypromptstatus(d, OCTAL_CONNECTED, "", 0, l->instance, sub->callid);
  5216. transmit_selectsoftkeys(d, l->instance, sub->callid, KEYDEF_CONNECTED, KEYMASK_ALL);
  5217. if (!sub->rtp) {
  5218. start_rtp(sub);
  5219. }
  5220. if (sub->aa_beep) {
  5221. transmit_start_tone(d, SKINNY_ZIP, l->instance, sub->callid);
  5222. }
  5223. if (sub->aa_mute) {
  5224. transmit_microphone_mode(d, SKINNY_MICOFF);
  5225. }
  5226. if (sub->substate == SUBSTATE_RINGIN || sub->substate == SUBSTATE_CALLWAIT) {
  5227. ast_queue_control(sub->owner, AST_CONTROL_ANSWER);
  5228. }
  5229. if (sub->substate == SUBSTATE_DIALING || sub->substate == SUBSTATE_RINGOUT) {
  5230. transmit_dialednumber(d, sub->exten, l->instance, sub->callid);
  5231. }
  5232. if (ast_channel_state(sub->owner) != AST_STATE_UP) {
  5233. ast_setstate(sub->owner, AST_STATE_UP);
  5234. }
  5235. sub->substate = SUBSTATE_CONNECTED;
  5236. l->activesub = sub;
  5237. break;
  5238. case SUBSTATE_BUSY:
  5239. if (!(sub->substate == SUBSTATE_DIALING || sub->substate == SUBSTATE_PROGRESS || sub->substate == SUBSTATE_RINGOUT)) {
  5240. ast_log(LOG_WARNING, "Cannot set substate to SUBSTATE_BUSY from %s (on call-%u)\n", substate2str(sub->substate), sub->callid);
  5241. break;
  5242. }
  5243. if (!d->earlyrtp) {
  5244. transmit_start_tone(d, SKINNY_BUSYTONE, l->instance, sub->callid);
  5245. }
  5246. send_callinfo(sub);
  5247. transmit_callstate(d, l->instance, sub->callid, SKINNY_BUSY);
  5248. send_displaypromptstatus(d, OCTAL_BUSY, "", 0, l->instance, sub->callid);
  5249. sub->substate = SUBSTATE_BUSY;
  5250. break;
  5251. case SUBSTATE_CONGESTION:
  5252. if (!(sub->substate == SUBSTATE_DIALING || sub->substate == SUBSTATE_PROGRESS || sub->substate == SUBSTATE_RINGOUT)) {
  5253. ast_log(LOG_WARNING, "Cannot set substate to SUBSTATE_CONGESTION from %s (on call-%u)\n", substate2str(sub->substate), sub->callid);
  5254. break;
  5255. }
  5256. if (!d->earlyrtp) {
  5257. transmit_start_tone(d, SKINNY_REORDER, l->instance, sub->callid);
  5258. }
  5259. send_callinfo(sub);
  5260. transmit_callstate(d, l->instance, sub->callid, SKINNY_CONGESTION);
  5261. send_displaypromptstatus(d, "Congestion", "", 0, l->instance, sub->callid);
  5262. sub->substate = SUBSTATE_CONGESTION;
  5263. break;
  5264. case SUBSTATE_PROGRESS:
  5265. if (sub->substate != SUBSTATE_DIALING) {
  5266. ast_log(LOG_WARNING, "Cannot set substate to SUBSTATE_PROGRESS from %s (on call-%u)\n", substate2str(sub->substate), sub->callid);
  5267. break;
  5268. }
  5269. if (!d->earlyrtp) {
  5270. transmit_start_tone(d, SKINNY_ALERT, l->instance, sub->callid);
  5271. }
  5272. send_callinfo(sub);
  5273. transmit_callstate(d, l->instance, sub->callid, SKINNY_PROGRESS);
  5274. send_displaypromptstatus(d, "Call Progress", "", 0, l->instance, sub->callid);
  5275. sub->substate = SUBSTATE_PROGRESS;
  5276. break;
  5277. case SUBSTATE_HOLD:
  5278. if (sub->substate != SUBSTATE_CONNECTED) {
  5279. ast_log(LOG_WARNING, "Cannot set substate to SUBSTATE_HOLD from %s (on call-%u)\n", substate2str(sub->substate), sub->callid);
  5280. break;
  5281. }
  5282. ast_queue_hold(sub->owner, l->mohsuggest);
  5283. transmit_activatecallplane(d, l);
  5284. transmit_closereceivechannel(d, sub);
  5285. transmit_stopmediatransmission(d, sub);
  5286. transmit_callstate(d, l->instance, sub->callid, SKINNY_HOLD);
  5287. transmit_lamp_indication(d, STIMULUS_LINE, l->instance, SKINNY_LAMP_WINK);
  5288. transmit_selectsoftkeys(d, l->instance, sub->callid, KEYDEF_ONHOLD, KEYMASK_ALL);
  5289. sub->substate = SUBSTATE_HOLD;
  5290. break;
  5291. default:
  5292. ast_log(LOG_WARNING, "Was asked to change to nonexistant substate %d on Sub-%u\n", state, sub->callid);
  5293. }
  5294. skinny_unlocksub(sub);
  5295. }
  5296. static void dumpsub(struct skinny_subchannel *sub, int forcehangup)
  5297. {
  5298. struct skinny_line *l = sub->line;
  5299. struct skinny_device *d = l->device;
  5300. struct skinny_subchannel *activate_sub = NULL;
  5301. struct skinny_subchannel *tsub;
  5302. SKINNY_DEBUG(DEBUG_SUB, 3, "Sub %u - Dumping\n", sub->callid);
  5303. if (!forcehangup && sub->substate == SUBSTATE_HOLD) {
  5304. l->activesub = NULL;
  5305. return;
  5306. }
  5307. if (sub == l->activesub) {
  5308. d->hookstate = SKINNY_ONHOOK;
  5309. transmit_speaker_mode(d, SKINNY_SPEAKEROFF);
  5310. if (sub->related) {
  5311. activate_sub = sub->related;
  5312. setsubstate(sub, SUBSTATE_ONHOOK);
  5313. l->activesub = activate_sub;
  5314. if (l->activesub->substate != SUBSTATE_HOLD) {
  5315. ast_log(LOG_WARNING, "Sub-%u was related but not at SUBSTATE_HOLD\n", sub->callid);
  5316. return;
  5317. }
  5318. setsubstate(l->activesub, SUBSTATE_HOLD);
  5319. } else {
  5320. setsubstate(sub, SUBSTATE_ONHOOK);
  5321. AST_LIST_TRAVERSE(&l->sub, tsub, list) {
  5322. if (tsub->substate == SUBSTATE_CALLWAIT) {
  5323. activate_sub = tsub;
  5324. }
  5325. }
  5326. if (activate_sub) {
  5327. setsubstate(activate_sub, SUBSTATE_RINGIN);
  5328. return;
  5329. }
  5330. AST_LIST_TRAVERSE(&l->sub, tsub, list) {
  5331. if (tsub->substate == SUBSTATE_HOLD) {
  5332. activate_sub = tsub;
  5333. }
  5334. }
  5335. if (activate_sub) {
  5336. setsubstate(activate_sub, SUBSTATE_HOLD);
  5337. return;
  5338. }
  5339. }
  5340. } else {
  5341. setsubstate(sub, SUBSTATE_ONHOOK);
  5342. }
  5343. }
  5344. static void activatesub(struct skinny_subchannel *sub, int state)
  5345. {
  5346. struct skinny_line *l = sub->line;
  5347. SKINNY_DEBUG(DEBUG_SUB, 3, "Sub %u - Activating, and deactivating sub %u\n",
  5348. sub->callid, l->activesub ? l->activesub->callid : 0);
  5349. if (sub == l->activesub) {
  5350. setsubstate(sub, state);
  5351. } else {
  5352. if (l->activesub) {
  5353. if (l->activesub->substate == SUBSTATE_RINGIN) {
  5354. setsubstate(l->activesub, SUBSTATE_CALLWAIT);
  5355. } else if (l->activesub->substate != SUBSTATE_HOLD) {
  5356. setsubstate(l->activesub, SUBSTATE_ONHOOK);
  5357. }
  5358. }
  5359. l->activesub = sub;
  5360. setsubstate(sub, state);
  5361. }
  5362. }
  5363. static void dialandactivatesub(struct skinny_subchannel *sub, char exten[AST_MAX_EXTENSION])
  5364. {
  5365. struct skinny_line *l = sub->line;
  5366. struct skinny_device *d = l->device;
  5367. if (sub->dialType == DIALTYPE_NORMAL) {
  5368. SKINNY_DEBUG(DEBUG_SUB, 3, "Sub %u - Dial %s and Activate\n", sub->callid, exten);
  5369. ast_copy_string(sub->exten, exten, sizeof(sub->exten));
  5370. activatesub(sub, SUBSTATE_DIALING);
  5371. } else if (sub->dialType == DIALTYPE_CFWD) {
  5372. SKINNY_DEBUG(DEBUG_SUB, 3, "Sub %u - Set callforward(%d) to %s\n", sub->callid, sub->getforward, exten);
  5373. set_callforwards(l, sub->exten, sub->getforward);
  5374. dumpsub(sub, 1);
  5375. transmit_cfwdstate(d, l);
  5376. transmit_displaynotify(d, "CFwd enabled", 10);
  5377. } else if (sub->dialType == DIALTYPE_XFER) {
  5378. ast_copy_string(sub->exten, exten, sizeof(sub->exten));
  5379. skinny_transfer_blind(sub);
  5380. }
  5381. }
  5382. static int handle_hold_button(struct skinny_subchannel *sub)
  5383. {
  5384. if (!sub)
  5385. return -1;
  5386. if (sub->related) {
  5387. setsubstate(sub, SUBSTATE_HOLD);
  5388. activatesub(sub->related, SUBSTATE_CONNECTED);
  5389. } else {
  5390. if (sub->substate == SUBSTATE_HOLD) {
  5391. activatesub(sub, SUBSTATE_CONNECTED);
  5392. } else {
  5393. setsubstate(sub, SUBSTATE_HOLD);
  5394. }
  5395. }
  5396. return 1;
  5397. }
  5398. static int handle_transfer_button(struct skinny_subchannel *sub)
  5399. {
  5400. struct skinny_line *l;
  5401. struct skinny_device *d;
  5402. struct skinny_subchannel *newsub;
  5403. struct ast_channel *c;
  5404. if (!sub) {
  5405. ast_verbose("Transfer: No subchannel to transfer\n");
  5406. return -1;
  5407. }
  5408. l = sub->line;
  5409. d = l->device;
  5410. if (!d->session) {
  5411. ast_log(LOG_WARNING, "Device for line %s is not registered.\n", l->name);
  5412. return -1;
  5413. }
  5414. if (!sub->related) {
  5415. /* Another sub has not been created so this must be first XFER press */
  5416. if (!(sub->substate == SUBSTATE_HOLD)) {
  5417. setsubstate(sub, SUBSTATE_HOLD);
  5418. }
  5419. c = skinny_new(l, NULL, AST_STATE_DOWN, NULL, NULL, SKINNY_OUTGOING);
  5420. if (c) {
  5421. newsub = ast_channel_tech_pvt(c);
  5422. /* point the sub and newsub at each other so we know they are related */
  5423. newsub->related = sub;
  5424. sub->related = newsub;
  5425. newsub->xferor = 1;
  5426. setsubstate(newsub, SUBSTATE_OFFHOOK);
  5427. } else {
  5428. ast_log(LOG_WARNING, "Unable to create channel for %s@%s\n", l->name, d->name);
  5429. }
  5430. } else {
  5431. /* We already have a related sub so we can either complete XFER or go into BLINDXFER (or cancel BLINDXFER */
  5432. if (sub->substate == SUBSTATE_OFFHOOK) {
  5433. if (sub->dialType == DIALTYPE_XFER) {
  5434. sub->dialType = DIALTYPE_NORMAL;
  5435. } else {
  5436. sub->dialType = DIALTYPE_XFER;
  5437. }
  5438. } else {
  5439. skinny_transfer_attended(sub);
  5440. }
  5441. }
  5442. return 0;
  5443. }
  5444. static void handle_callforward_button(struct skinny_line *l, struct skinny_subchannel *sub, int cfwdtype)
  5445. {
  5446. struct skinny_device *d = l->device;
  5447. struct ast_channel *c;
  5448. if (!d->session) {
  5449. ast_log(LOG_WARNING, "Device for line %s is not registered.\n", l->name);
  5450. return;
  5451. }
  5452. if (!sub && (l->cfwdtype & cfwdtype)) {
  5453. set_callforwards(l, NULL, cfwdtype);
  5454. if (sub) {
  5455. dumpsub(sub, 1);
  5456. }
  5457. transmit_cfwdstate(d, l);
  5458. transmit_displaynotify(d, "CFwd disabled", 10);
  5459. } else {
  5460. if (!sub || !sub->owner) {
  5461. if (!(c = skinny_new(l, NULL, AST_STATE_DOWN, NULL, NULL, SKINNY_OUTGOING))) {
  5462. ast_log(LOG_WARNING, "Unable to create channel for %s@%s\n", l->name, d->name);
  5463. return;
  5464. }
  5465. sub = ast_channel_tech_pvt(c);
  5466. l->activesub = sub;
  5467. setsubstate(sub, SUBSTATE_OFFHOOK);
  5468. }
  5469. sub->getforward |= cfwdtype;
  5470. sub->dialType = DIALTYPE_CFWD;
  5471. }
  5472. }
  5473. static int handle_ip_port_message(struct skinny_req *req, struct skinnysession *s)
  5474. {
  5475. /* no response necessary */
  5476. return 1;
  5477. }
  5478. static void handle_keepalive_message(struct skinny_req *req, struct skinnysession *s)
  5479. {
  5480. not_used = ast_sched_del(sched, s->keepalive_timeout_sched);
  5481. #ifdef AST_DEVMODE
  5482. {
  5483. long keepalive_diff;
  5484. keepalive_diff = (long) ast_tvdiff_ms(ast_tvnow(), ast_tvadd(s->last_keepalive, ast_tv(keep_alive, 0)));
  5485. SKINNY_DEBUG(DEBUG_PACKET|DEBUG_KEEPALIVE, 3,
  5486. "Keep_alive %d on %s, %.3fs %s\n",
  5487. ++s->keepalive_count,
  5488. (s->device ? s->device->name : "unregistered"),
  5489. (float) labs(keepalive_diff) / 1000,
  5490. (keepalive_diff > 0 ? "late" : "early"));
  5491. }
  5492. #endif
  5493. s->keepalive_timeout_sched = ast_sched_add(sched, keep_alive*3000, skinny_nokeepalive_cb, s);
  5494. s->last_keepalive = ast_tvnow();
  5495. transmit_keepaliveack(s);
  5496. }
  5497. static int handle_keypad_button_message(struct skinny_req *req, struct skinnysession *s)
  5498. {
  5499. struct skinny_subchannel *sub = NULL;
  5500. struct skinny_line *l;
  5501. struct skinny_device *d = s->device;
  5502. struct ast_frame f = { 0, };
  5503. char dgt;
  5504. int digit;
  5505. int lineInstance;
  5506. int callReference;
  5507. size_t len;
  5508. digit = letohl(req->data.keypad.button);
  5509. lineInstance = letohl(req->data.keypad.lineInstance);
  5510. callReference = letohl(req->data.keypad.callReference);
  5511. if (lineInstance && callReference) {
  5512. sub = find_subchannel_by_instance_reference(d, lineInstance, callReference);
  5513. } else {
  5514. sub = d->activeline->activesub;
  5515. }
  5516. if (!sub)
  5517. return 0;
  5518. l = sub->line;
  5519. if (digit == 14) {
  5520. dgt = '*';
  5521. } else if (digit == 15) {
  5522. dgt = '#';
  5523. } else if (digit >= 0 && digit <= 9) {
  5524. dgt = '0' + digit;
  5525. } else {
  5526. /* digit=10-13 (A,B,C,D ?), or
  5527. * digit is bad value
  5528. *
  5529. * probably should not end up here, but set
  5530. * value for backward compatibility, and log
  5531. * a warning.
  5532. */
  5533. dgt = '0' + digit;
  5534. ast_log(LOG_WARNING, "Unsupported digit %d\n", digit);
  5535. }
  5536. if ((sub->owner && ast_channel_state(sub->owner) < AST_STATE_UP)) {
  5537. if (-1 < sub->dialer_sched && !skinny_sched_del(sub->dialer_sched, sub)) {
  5538. SKINNY_DEBUG(DEBUG_SUB, 3, "Sub %u - Got a digit and not timed out, so try dialing\n", sub->callid);
  5539. sub->dialer_sched = -1;
  5540. len = strlen(sub->exten);
  5541. if (len == 0) {
  5542. transmit_stop_tone(d, l->instance, sub->callid);
  5543. transmit_selectsoftkeys(d, l->instance, sub->callid, KEYDEF_DADFD, KEYMASK_ALL&~KEYMASK_FORCEDIAL);
  5544. }
  5545. if (len < sizeof(sub->exten) - 1 && dgt != immed_dialchar) {
  5546. sub->exten[len] = dgt;
  5547. sub->exten[len + 1] = '\0';
  5548. }
  5549. if (len == sizeof(sub->exten) - 1 || dgt == immed_dialchar) {
  5550. skinny_dialer(sub, 1);
  5551. } else {
  5552. skinny_dialer(sub, 0);
  5553. }
  5554. } else {
  5555. SKINNY_DEBUG(DEBUG_SUB, 3, "Sub %u Got a digit already timedout, ignore\n", sub->callid);
  5556. /* Timed out so the call is being progressed elsewhere, to late for digits */
  5557. return 0;
  5558. }
  5559. } else {
  5560. SKINNY_DEBUG(DEBUG_SUB, 3, "Sub %u - Got a digit and sending as DTMF\n", sub->callid);
  5561. f.subclass.integer = dgt;
  5562. f.src = "skinny";
  5563. if (sub->owner) {
  5564. if (ast_channel_state(sub->owner) == 0) {
  5565. f.frametype = AST_FRAME_DTMF_BEGIN;
  5566. ast_queue_frame(sub->owner, &f);
  5567. }
  5568. /* XXX MUST queue this frame to all lines in threeway call if threeway call is active */
  5569. f.frametype = AST_FRAME_DTMF_END;
  5570. ast_queue_frame(sub->owner, &f);
  5571. /* XXX This seriously needs to be fixed */
  5572. if (AST_LIST_NEXT(sub, list) && AST_LIST_NEXT(sub, list)->owner) {
  5573. if (ast_channel_state(sub->owner) == 0) {
  5574. f.frametype = AST_FRAME_DTMF_BEGIN;
  5575. ast_queue_frame(AST_LIST_NEXT(sub, list)->owner, &f);
  5576. }
  5577. f.frametype = AST_FRAME_DTMF_END;
  5578. ast_queue_frame(AST_LIST_NEXT(sub, list)->owner, &f);
  5579. }
  5580. } else {
  5581. ast_log(LOG_WARNING, "Got digit on %s, but not associated with channel\n", l->name);
  5582. }
  5583. }
  5584. return 1;
  5585. }
  5586. static int handle_stimulus_message(struct skinny_req *req, struct skinnysession *s)
  5587. {
  5588. struct skinny_device *d = s->device;
  5589. struct skinny_line *l;
  5590. struct skinny_subchannel *sub;
  5591. /*struct skinny_speeddial *sd;*/
  5592. struct ast_channel *c;
  5593. int event;
  5594. int instance;
  5595. #ifdef AST_DEVMODE
  5596. int callreference;
  5597. /* This is only used in AST_DEVMODE, as an argument to SKINNY_DEBUG */
  5598. callreference = letohl(req->data.stimulus.callreference);
  5599. #endif
  5600. event = letohl(req->data.stimulus.stimulus);
  5601. instance = letohl(req->data.stimulus.stimulusInstance);
  5602. /* Note that this call should be using the passed in instance and callreference */
  5603. sub = find_subchannel_by_instance_reference(d, d->lastlineinstance, d->lastcallreference);
  5604. if (!sub) {
  5605. l = find_line_by_instance(d, d->lastlineinstance);
  5606. if (!l) {
  5607. return 0;
  5608. }
  5609. sub = l->activesub;
  5610. } else {
  5611. l = sub->line;
  5612. }
  5613. switch(event) {
  5614. case STIMULUS_REDIAL:
  5615. SKINNY_DEBUG(DEBUG_PACKET, 3, "Received STIMULUS_REDIAL from %s, inst %d, callref %d\n",
  5616. d->name, instance, callreference);
  5617. if (ast_strlen_zero(l->lastnumberdialed)) {
  5618. ast_log(LOG_WARNING, "Attempted redial, but no previously dialed number found. Ignoring button.\n");
  5619. break;
  5620. }
  5621. c = skinny_new(l, NULL, AST_STATE_DOWN, NULL, NULL, SKINNY_OUTGOING);
  5622. if (!c) {
  5623. ast_log(LOG_WARNING, "Unable to create channel for %s@%s\n", l->name, d->name);
  5624. } else {
  5625. sub = ast_channel_tech_pvt(c);
  5626. l = sub->line;
  5627. dialandactivatesub(sub, l->lastnumberdialed);
  5628. }
  5629. break;
  5630. case STIMULUS_SPEEDDIAL:
  5631. {
  5632. struct skinny_speeddial *sd;
  5633. SKINNY_DEBUG(DEBUG_PACKET, 3, "Received STIMULUS_SPEEDDIAL from %s, inst %d, callref %d\n",
  5634. d->name, instance, callreference);
  5635. if (!(sd = find_speeddial_by_instance(d, instance, 0))) {
  5636. return 0;
  5637. }
  5638. if (!sub || !sub->owner)
  5639. c = skinny_new(l, NULL, AST_STATE_DOWN, NULL, NULL, SKINNY_OUTGOING);
  5640. else
  5641. c = sub->owner;
  5642. if (!c) {
  5643. ast_log(LOG_WARNING, "Unable to create channel for %s@%s\n", l->name, d->name);
  5644. } else {
  5645. sub = ast_channel_tech_pvt(c);
  5646. dialandactivatesub(sub, sd->exten);
  5647. }
  5648. }
  5649. break;
  5650. case STIMULUS_HOLD:
  5651. SKINNY_DEBUG(DEBUG_PACKET, 3, "Received STIMULUS_HOLD from %s, inst %d, callref %d\n",
  5652. d->name, instance, callreference);
  5653. handle_hold_button(sub);
  5654. break;
  5655. case STIMULUS_TRANSFER:
  5656. SKINNY_DEBUG(DEBUG_PACKET, 3, "Received STIMULUS_TRANSFER from %s, inst %d, callref %d\n",
  5657. d->name, instance, callreference);
  5658. if (l->transfer)
  5659. handle_transfer_button(sub);
  5660. else
  5661. transmit_displaynotify(d, "Transfer disabled", 10);
  5662. break;
  5663. case STIMULUS_CONFERENCE:
  5664. SKINNY_DEBUG(DEBUG_PACKET, 3, "Received STIMULUS_CONFERENCE from %s, inst %d, callref %d\n",
  5665. d->name, instance, callreference);
  5666. /* XXX determine the best way to pull off a conference. Meetme? */
  5667. break;
  5668. case STIMULUS_VOICEMAIL:
  5669. SKINNY_DEBUG(DEBUG_PACKET, 3, "Received STIMULUS_VOICEMAIL from %s, inst %d, callref %d\n",
  5670. d->name, instance, callreference);
  5671. if (!sub || !sub->owner) {
  5672. c = skinny_new(l, NULL, AST_STATE_DOWN, NULL, NULL, SKINNY_OUTGOING);
  5673. } else {
  5674. c = sub->owner;
  5675. }
  5676. if (!c) {
  5677. ast_log(LOG_WARNING, "Unable to create channel for %s@%s\n", l->name, d->name);
  5678. break;
  5679. }
  5680. sub = ast_channel_tech_pvt(c);
  5681. if (sub->substate == SUBSTATE_UNSET || sub->substate == SUBSTATE_OFFHOOK){
  5682. dialandactivatesub(sub, l->vmexten);
  5683. }
  5684. break;
  5685. case STIMULUS_CALLPARK:
  5686. {
  5687. char extout[AST_MAX_EXTENSION];
  5688. RAII_VAR(struct ast_bridge_channel *, bridge_channel, NULL, ao2_cleanup);
  5689. SKINNY_DEBUG(DEBUG_PACKET, 3, "Received STIMULUS_CALLPARK from %s, inst %d, callref %d\n",
  5690. d->name, instance, callreference);
  5691. if (!ast_parking_provider_registered()) {
  5692. transmit_displaynotify(d, "Call Park not available", 10);
  5693. break;
  5694. }
  5695. if ((sub && sub->owner) && (ast_channel_state(sub->owner) == AST_STATE_UP)) {
  5696. c = sub->owner;
  5697. ast_channel_lock(c);
  5698. bridge_channel = ast_channel_get_bridge_channel(c);
  5699. ast_channel_unlock(c);
  5700. if (!bridge_channel) {
  5701. transmit_displaynotify(d, "Call Park failed", 10);
  5702. break;
  5703. }
  5704. if (!ast_parking_park_call(bridge_channel, extout, sizeof(extout))) {
  5705. static const char msg_prefix[] = "Call Parked at: ";
  5706. char message[sizeof(msg_prefix) + sizeof(extout)];
  5707. snprintf(message, sizeof(message), "%s%s", msg_prefix, extout);
  5708. transmit_displaynotify(d, message, 10);
  5709. break;
  5710. }
  5711. transmit_displaynotify(d, "Call Park failed", 10);
  5712. } else {
  5713. transmit_displaynotify(d, "Call Park not available", 10);
  5714. }
  5715. break;
  5716. }
  5717. case STIMULUS_DND:
  5718. SKINNY_DEBUG(DEBUG_PACKET, 3, "Received STIMULUS_DND from %s, inst %d, callref %d\n",
  5719. d->name, instance, callreference);
  5720. /* Do not disturb */
  5721. if (l->dnd != 0){
  5722. ast_verb(3, "Disabling DND on %s@%s\n", l->name, d->name);
  5723. l->dnd = 0;
  5724. transmit_lamp_indication(d, STIMULUS_DND, 1, SKINNY_LAMP_ON);
  5725. transmit_displaynotify(d, "DnD disabled", 10);
  5726. } else {
  5727. ast_verb(3, "Enabling DND on %s@%s\n", l->name, d->name);
  5728. l->dnd = 1;
  5729. transmit_lamp_indication(d, STIMULUS_DND, 1, SKINNY_LAMP_OFF);
  5730. transmit_displaynotify(d, "DnD enabled", 10);
  5731. }
  5732. break;
  5733. case STIMULUS_FORWARDALL:
  5734. SKINNY_DEBUG(DEBUG_PACKET, 3, "Received STIMULUS_FORWARDALL from %s, inst %d, callref %d\n",
  5735. d->name, instance, callreference);
  5736. handle_callforward_button(l, sub, SKINNY_CFWD_ALL);
  5737. break;
  5738. case STIMULUS_FORWARDBUSY:
  5739. SKINNY_DEBUG(DEBUG_PACKET, 3, "Received STIMULUS_FORWARDBUSY from %s, inst %d, callref %d\n",
  5740. d->name, instance, callreference);
  5741. handle_callforward_button(l, sub, SKINNY_CFWD_BUSY);
  5742. break;
  5743. case STIMULUS_FORWARDNOANSWER:
  5744. SKINNY_DEBUG(DEBUG_PACKET, 3, "Received STIMULUS_FORWARDNOANSWER from %s, inst %d, callref %d\n",
  5745. d->name, instance, callreference);
  5746. handle_callforward_button(l, sub, SKINNY_CFWD_NOANSWER);
  5747. break;
  5748. case STIMULUS_DISPLAY:
  5749. /* Not sure what this is */
  5750. SKINNY_DEBUG(DEBUG_PACKET, 3, "Received STIMULUS_DISPLAY from %s, inst %d, callref %d\n",
  5751. d->name, instance, callreference);
  5752. break;
  5753. case STIMULUS_LINE:
  5754. SKINNY_DEBUG(DEBUG_PACKET, 3, "Received STIMULUS_LINE from %s, inst %d, callref %d\n",
  5755. d->name, instance, callreference);
  5756. l = find_line_by_instance(d, instance);
  5757. if (!l) {
  5758. return 0;
  5759. }
  5760. d->activeline = l;
  5761. /* turn the speaker on */
  5762. transmit_speaker_mode(d, SKINNY_SPEAKERON);
  5763. transmit_ringer_mode(d, SKINNY_RING_OFF);
  5764. transmit_lamp_indication(d, STIMULUS_LINE, l->instance, SKINNY_LAMP_ON);
  5765. d->hookstate = SKINNY_OFFHOOK;
  5766. if (sub && sub->calldirection == SKINNY_INCOMING) {
  5767. setsubstate(sub, SUBSTATE_CONNECTED);
  5768. } else {
  5769. if (sub && sub->owner) {
  5770. ast_debug(1, "Current subchannel [%s] already has owner\n", ast_channel_name(sub->owner));
  5771. } else {
  5772. c = skinny_new(l, NULL, AST_STATE_DOWN, NULL, NULL, SKINNY_OUTGOING);
  5773. if (c) {
  5774. setsubstate(ast_channel_tech_pvt(c), SUBSTATE_OFFHOOK);
  5775. } else {
  5776. ast_log(LOG_WARNING, "Unable to create channel for %s@%s\n", l->name, d->name);
  5777. }
  5778. }
  5779. }
  5780. break;
  5781. default:
  5782. SKINNY_DEBUG(DEBUG_PACKET, 3, "Received UNKNOWN_STIMULUS(%d) from %s, inst %d, callref %d\n",
  5783. event, d->name, instance, callreference);
  5784. break;
  5785. }
  5786. ast_devstate_changed(AST_DEVICE_UNKNOWN, AST_DEVSTATE_CACHABLE, "Skinny/%s", l->name);
  5787. return 1;
  5788. }
  5789. static int handle_offhook_message(struct skinny_req *req, struct skinnysession *s)
  5790. {
  5791. struct skinny_device *d = s->device;
  5792. struct skinny_line *l = NULL;
  5793. struct skinny_subchannel *sub = NULL;
  5794. struct ast_channel *c;
  5795. int instance;
  5796. int reference;
  5797. instance = letohl(req->data.offhook.instance);
  5798. reference = letohl(req->data.offhook.reference);
  5799. if (d->hookstate == SKINNY_OFFHOOK) {
  5800. ast_verb(3, "Got offhook message when device (%s) already offhook\n", d->name);
  5801. return 0;
  5802. }
  5803. if (reference) {
  5804. sub = find_subchannel_by_instance_reference(d, instance, reference);
  5805. if (sub) {
  5806. l = sub->line;
  5807. }
  5808. }
  5809. if (!sub) {
  5810. if (instance) {
  5811. l = find_line_by_instance(d, instance);
  5812. } else {
  5813. l = d->activeline;
  5814. }
  5815. sub = l->activesub;
  5816. }
  5817. transmit_ringer_mode(d, SKINNY_RING_OFF);
  5818. d->hookstate = SKINNY_OFFHOOK;
  5819. ast_devstate_changed(AST_DEVICE_INUSE, AST_DEVSTATE_CACHABLE, "Skinny/%s", l->name);
  5820. if (sub && sub->substate == SUBSTATE_HOLD) {
  5821. return 1;
  5822. }
  5823. transmit_lamp_indication(d, STIMULUS_LINE, l->instance, SKINNY_LAMP_ON);
  5824. if (sub && sub->calldirection == SKINNY_INCOMING) {
  5825. setsubstate(sub, SUBSTATE_CONNECTED);
  5826. } else {
  5827. /* Not ideal, but let's send updated time at onhook and offhook, as it clears the display */
  5828. transmit_definetimedate(d);
  5829. if (sub && sub->owner) {
  5830. ast_debug(1, "Current sub [%s] already has owner\n", ast_channel_name(sub->owner));
  5831. } else {
  5832. c = skinny_new(l, NULL, AST_STATE_DOWN, NULL, NULL, SKINNY_OUTGOING);
  5833. if (c) {
  5834. setsubstate(ast_channel_tech_pvt(c), SUBSTATE_OFFHOOK);
  5835. } else {
  5836. ast_log(LOG_WARNING, "Unable to create channel for %s@%s\n", l->name, d->name);
  5837. }
  5838. }
  5839. }
  5840. return 1;
  5841. }
  5842. static int handle_onhook_message(struct skinny_req *req, struct skinnysession *s)
  5843. {
  5844. struct skinny_device *d = s->device;
  5845. struct skinny_line *l;
  5846. struct skinny_subchannel *sub;
  5847. int instance;
  5848. int reference;
  5849. instance = letohl(req->data.onhook.instance);
  5850. reference = letohl(req->data.onhook.reference);
  5851. if (instance && reference) {
  5852. sub = find_subchannel_by_instance_reference(d, instance, reference);
  5853. if (!sub) {
  5854. return 0;
  5855. }
  5856. l = sub->line;
  5857. } else {
  5858. l = d->activeline;
  5859. sub = l->activesub;
  5860. if (!sub) {
  5861. return 0;
  5862. }
  5863. }
  5864. if (d->hookstate == SKINNY_ONHOOK) {
  5865. /* Something else already put us back on hook */
  5866. /* Not ideal, but let's send updated time anyway, as it clears the display */
  5867. transmit_definetimedate(d);
  5868. return 0;
  5869. }
  5870. if (l->transfer && sub->xferor && ast_channel_state(sub->owner) >= AST_STATE_RING) {
  5871. /* We're allowed to transfer, we have two active calls and
  5872. we made at least one of the calls. Let's try and transfer */
  5873. handle_transfer_button(sub);
  5874. return 0;
  5875. }
  5876. ast_devstate_changed(AST_DEVICE_NOT_INUSE, AST_DEVSTATE_CACHABLE, "Skinny/%s", l->name);
  5877. dumpsub(sub, 0);
  5878. d->hookstate = SKINNY_ONHOOK;
  5879. /* Not ideal, but let's send updated time at onhook and offhook, as it clears the display */
  5880. transmit_definetimedate(d);
  5881. return 1;
  5882. }
  5883. static int handle_capabilities_res_message(struct skinny_req *req, struct skinnysession *s)
  5884. {
  5885. struct skinny_device *d = s->device;
  5886. struct skinny_line *l;
  5887. uint32_t count = 0;
  5888. struct ast_format_cap *codecs = ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_DEFAULT);
  5889. int i;
  5890. #ifdef AST_DEVMODE
  5891. struct ast_str *codec_buf = ast_str_alloca(AST_FORMAT_CAP_NAMES_LEN);
  5892. #endif
  5893. if (!codecs) {
  5894. return 0;
  5895. }
  5896. count = letohl(req->data.caps.count);
  5897. if (count > SKINNY_MAX_CAPABILITIES) {
  5898. count = SKINNY_MAX_CAPABILITIES;
  5899. ast_log(LOG_WARNING, "Received more capabilities than we can handle (%d). Ignoring the rest.\n", SKINNY_MAX_CAPABILITIES);
  5900. }
  5901. for (i = 0; i < count; i++) {
  5902. struct ast_format *acodec;
  5903. int scodec = 0;
  5904. scodec = letohl(req->data.caps.caps[i].codec);
  5905. acodec = codec_skinny2ast(scodec);
  5906. SKINNY_DEBUG(DEBUG_AUDIO, 4, "Adding codec capability %s (%d)\n", ast_format_get_name(acodec), scodec);
  5907. ast_format_cap_append(codecs, acodec, 0);
  5908. }
  5909. ast_format_cap_get_compatible(d->confcap, codecs, d->cap);
  5910. SKINNY_DEBUG(DEBUG_AUDIO, 4, "Device capability set to '%s'\n", ast_format_cap_get_names(d->cap, &codec_buf));
  5911. AST_LIST_TRAVERSE(&d->lines, l, list) {
  5912. ast_mutex_lock(&l->lock);
  5913. ast_format_cap_get_compatible(l->confcap, d->cap, l->cap);
  5914. ast_mutex_unlock(&l->lock);
  5915. }
  5916. ao2_ref(codecs, -1);
  5917. return 1;
  5918. }
  5919. static int handle_button_template_req_message(struct skinny_req *req, struct skinnysession *s)
  5920. {
  5921. struct skinny_device *d = s->device;
  5922. struct skinny_line *l;
  5923. int i;
  5924. struct skinny_speeddial *sd;
  5925. struct skinny_serviceurl *surl;
  5926. struct button_definition_template btn[42];
  5927. int lineInstance = 1;
  5928. int speeddialInstance = 1;
  5929. int serviceurlInstance = 1;
  5930. int buttonCount = 0;
  5931. if (!(req = req_alloc(sizeof(struct button_template_res_message), BUTTON_TEMPLATE_RES_MESSAGE)))
  5932. return -1;
  5933. SKINNY_DEBUG(DEBUG_TEMPLATE, 3, "Creating Button Template\n");
  5934. memset(&btn, 0, sizeof(btn));
  5935. get_button_template(s, btn);
  5936. for (i=0; i<42; i++) {
  5937. int btnSet = 0;
  5938. switch (btn[i].buttonDefinition) {
  5939. case BT_CUST_LINE:
  5940. /* assume failure */
  5941. req->data.buttontemplate.definition[i].buttonDefinition = BT_NONE;
  5942. req->data.buttontemplate.definition[i].instanceNumber = 0;
  5943. AST_LIST_TRAVERSE(&d->lines, l, list) {
  5944. if (l->instance == lineInstance) {
  5945. SKINNY_DEBUG(DEBUG_TEMPLATE, 4, "Adding button: %d, %d\n", BT_LINE, lineInstance);
  5946. req->data.buttontemplate.definition[i].buttonDefinition = BT_LINE;
  5947. req->data.buttontemplate.definition[i].instanceNumber = lineInstance;
  5948. lineInstance++;
  5949. buttonCount++;
  5950. btnSet = 1;
  5951. break;
  5952. }
  5953. }
  5954. if (!btnSet) {
  5955. AST_LIST_TRAVERSE(&d->speeddials, sd, list) {
  5956. if (sd->isHint && sd->instance == lineInstance) {
  5957. SKINNY_DEBUG(DEBUG_TEMPLATE, 4, "Adding button: %d, %d\n", BT_LINE, lineInstance);
  5958. req->data.buttontemplate.definition[i].buttonDefinition = BT_LINE;
  5959. req->data.buttontemplate.definition[i].instanceNumber = lineInstance;
  5960. lineInstance++;
  5961. buttonCount++;
  5962. btnSet = 1;
  5963. break;
  5964. }
  5965. }
  5966. }
  5967. break;
  5968. case BT_CUST_LINESPEEDDIAL:
  5969. /* assume failure */
  5970. req->data.buttontemplate.definition[i].buttonDefinition = BT_NONE;
  5971. req->data.buttontemplate.definition[i].instanceNumber = 0;
  5972. AST_LIST_TRAVERSE(&d->lines, l, list) {
  5973. if (l->instance == lineInstance) {
  5974. SKINNY_DEBUG(DEBUG_TEMPLATE, 4, "Adding button: %d, %d\n", BT_LINE, lineInstance);
  5975. req->data.buttontemplate.definition[i].buttonDefinition = BT_LINE;
  5976. req->data.buttontemplate.definition[i].instanceNumber = lineInstance;
  5977. lineInstance++;
  5978. buttonCount++;
  5979. btnSet = 1;
  5980. break;
  5981. }
  5982. }
  5983. if (!btnSet) {
  5984. AST_LIST_TRAVERSE(&d->speeddials, sd, list) {
  5985. if (sd->isHint && sd->instance == lineInstance) {
  5986. SKINNY_DEBUG(DEBUG_TEMPLATE, 4, "Adding button: %d, %d\n", BT_LINE, lineInstance);
  5987. req->data.buttontemplate.definition[i].buttonDefinition = BT_LINE;
  5988. req->data.buttontemplate.definition[i].instanceNumber = lineInstance;
  5989. lineInstance++;
  5990. buttonCount++;
  5991. btnSet = 1;
  5992. break;
  5993. } else if (!sd->isHint && sd->instance == speeddialInstance) {
  5994. SKINNY_DEBUG(DEBUG_TEMPLATE, 4, "Adding button: %d, %d\n", BT_SPEEDDIAL, speeddialInstance);
  5995. req->data.buttontemplate.definition[i].buttonDefinition = BT_SPEEDDIAL;
  5996. req->data.buttontemplate.definition[i].instanceNumber = speeddialInstance;
  5997. speeddialInstance++;
  5998. buttonCount++;
  5999. btnSet = 1;
  6000. break;
  6001. }
  6002. }
  6003. }
  6004. if (!btnSet) {
  6005. AST_LIST_TRAVERSE(&d->serviceurls, surl, list) {
  6006. if (surl->instance == serviceurlInstance) {
  6007. SKINNY_DEBUG(DEBUG_TEMPLATE, 4, "Adding button: %d, %d\n", BT_SERVICEURL, serviceurlInstance);
  6008. req->data.buttontemplate.definition[i].buttonDefinition = BT_SERVICEURL;
  6009. req->data.buttontemplate.definition[i].instanceNumber = serviceurlInstance;
  6010. serviceurlInstance++;
  6011. buttonCount++;
  6012. btnSet = 1;
  6013. break;
  6014. }
  6015. }
  6016. }
  6017. break;
  6018. case BT_LINE:
  6019. req->data.buttontemplate.definition[i].buttonDefinition = htolel(BT_NONE);
  6020. req->data.buttontemplate.definition[i].instanceNumber = htolel(0);
  6021. AST_LIST_TRAVERSE(&d->lines, l, list) {
  6022. if (l->instance == lineInstance) {
  6023. SKINNY_DEBUG(DEBUG_TEMPLATE, 4, "Adding button: %d, %d\n", BT_LINE, lineInstance);
  6024. req->data.buttontemplate.definition[i].buttonDefinition = BT_LINE;
  6025. req->data.buttontemplate.definition[i].instanceNumber = lineInstance;
  6026. lineInstance++;
  6027. buttonCount++;
  6028. btnSet = 1;
  6029. break;
  6030. }
  6031. }
  6032. break;
  6033. case BT_SPEEDDIAL:
  6034. req->data.buttontemplate.definition[i].buttonDefinition = BT_NONE;
  6035. req->data.buttontemplate.definition[i].instanceNumber = 0;
  6036. AST_LIST_TRAVERSE(&d->speeddials, sd, list) {
  6037. if (!sd->isHint && sd->instance == speeddialInstance) {
  6038. SKINNY_DEBUG(DEBUG_TEMPLATE, 4, "Adding button: %d, %d\n", BT_SPEEDDIAL, speeddialInstance);
  6039. req->data.buttontemplate.definition[i].buttonDefinition = BT_SPEEDDIAL;
  6040. req->data.buttontemplate.definition[i].instanceNumber = speeddialInstance - 1;
  6041. speeddialInstance++;
  6042. buttonCount++;
  6043. btnSet = 1;
  6044. break;
  6045. }
  6046. }
  6047. break;
  6048. case BT_NONE:
  6049. break;
  6050. default:
  6051. SKINNY_DEBUG(DEBUG_TEMPLATE, 4, "Adding button: %d, %d\n", btn[i].buttonDefinition, 0);
  6052. req->data.buttontemplate.definition[i].buttonDefinition = htolel(btn[i].buttonDefinition);
  6053. req->data.buttontemplate.definition[i].instanceNumber = 0;
  6054. buttonCount++;
  6055. btnSet = 1;
  6056. break;
  6057. }
  6058. }
  6059. req->data.buttontemplate.buttonOffset = 0;
  6060. req->data.buttontemplate.buttonCount = htolel(buttonCount);
  6061. req->data.buttontemplate.totalButtonCount = htolel(buttonCount);
  6062. SKINNY_DEBUG(DEBUG_PACKET | DEBUG_TEMPLATE, 3, "Transmitting BUTTON_TEMPLATE_RES_MESSAGE to %s, type %d\n",
  6063. d->name, d->type);
  6064. transmit_response(d, req);
  6065. return 1;
  6066. }
  6067. static int handle_open_receive_channel_ack_message(struct skinny_req *req, struct skinnysession *s)
  6068. {
  6069. struct skinny_device *d = s->device;
  6070. struct skinny_line *l;
  6071. struct skinny_subchannel *sub;
  6072. struct sockaddr_in sin = { 0, };
  6073. struct sockaddr_in us = { 0, };
  6074. struct ast_sockaddr sin_tmp;
  6075. struct ast_sockaddr us_tmp;
  6076. struct ast_format *tmpfmt;
  6077. uint32_t addr;
  6078. int port;
  6079. int status;
  6080. int callid;
  6081. unsigned int framing;
  6082. status = (d->protocolversion<17) ? letohl(req->data.openreceivechannelack_ip4.status) : letohl(req->data.openreceivechannelack_ip6.status);
  6083. if (status) {
  6084. SKINNY_DEBUG(DEBUG_PACKET, 3, "Received OPEN_RECEIVE_CHANNEL_ACK_MESSAGE from %s, status %d\n",
  6085. d->name, status);
  6086. ast_log(LOG_ERROR, "Open Receive Channel Failure\n");
  6087. return 0;
  6088. }
  6089. if (d->protocolversion<17) {
  6090. addr = req->data.openreceivechannelack_ip4.ipAddr;
  6091. port = letohl(req->data.openreceivechannelack_ip4.port);
  6092. callid = letohl(req->data.openreceivechannelack_ip4.callReference);
  6093. } else {
  6094. memcpy(&addr, &req->data.openreceivechannelack_ip6.ipAddr, sizeof(addr));
  6095. port = letohl(req->data.openreceivechannelack_ip6.port);
  6096. callid = letohl(req->data.openreceivechannelack_ip6.callReference);
  6097. }
  6098. sin.sin_family = AF_INET;
  6099. sin.sin_addr.s_addr = addr;
  6100. sin.sin_port = htons(port);
  6101. SKINNY_DEBUG(DEBUG_PACKET, 3, "Received OPEN_RECEIVE_CHANNEL_ACK_MESSAGE from %s, status %d, callid %d, ip %s:%d\n",
  6102. d->name, status, callid, ast_inet_ntoa(sin.sin_addr), port);
  6103. sub = find_subchannel_by_reference(d, callid);
  6104. if (!sub) {
  6105. ast_log(LOG_ERROR, "Open Receive Channel Failure - can't find sub for %d\n", callid);
  6106. return 0;
  6107. }
  6108. l = sub->line;
  6109. if (sub->rtp) {
  6110. ast_sockaddr_from_sin(&sin_tmp, &sin);
  6111. ast_rtp_instance_set_remote_address(sub->rtp, &sin_tmp);
  6112. ast_rtp_instance_get_local_address(sub->rtp, &us_tmp);
  6113. ast_sockaddr_to_sin(&us_tmp, &us);
  6114. us.sin_addr.s_addr = us.sin_addr.s_addr ? us.sin_addr.s_addr : d->ourip.s_addr;
  6115. } else {
  6116. ast_log(LOG_ERROR, "No RTP structure, this is very bad\n");
  6117. return 0;
  6118. }
  6119. SKINNY_DEBUG(DEBUG_PACKET, 4, "device ipaddr = %s:%d\n", ast_inet_ntoa(sin.sin_addr), ntohs(sin.sin_port));
  6120. SKINNY_DEBUG(DEBUG_PACKET, 4, "asterisk ipaddr = %s:%d\n", ast_inet_ntoa(us.sin_addr), ntohs(us.sin_port));
  6121. tmpfmt = ast_format_cap_get_format(l->cap, 0);
  6122. framing = ast_format_cap_get_format_framing(l->cap, tmpfmt);
  6123. SKINNY_DEBUG(DEBUG_PACKET, 4, "Setting payloadType to '%s' (%u ms)\n", ast_format_get_name(tmpfmt), framing);
  6124. transmit_startmediatransmission(d, sub, us, tmpfmt, framing);
  6125. ao2_ref(tmpfmt, -1);
  6126. return 1;
  6127. }
  6128. static int handle_enbloc_call_message(struct skinny_req *req, struct skinnysession *s)
  6129. {
  6130. struct skinny_device *d = s->device;
  6131. struct skinny_line *l;
  6132. struct skinny_subchannel *sub = NULL;
  6133. struct ast_channel *c;
  6134. sub = find_subchannel_by_instance_reference(d, d->lastlineinstance, d->lastcallreference);
  6135. if (!sub) {
  6136. l = find_line_by_instance(d, d->lastlineinstance);
  6137. if (!l) {
  6138. return 0;
  6139. }
  6140. } else {
  6141. l = sub->line;
  6142. }
  6143. c = skinny_new(l, NULL, AST_STATE_DOWN, NULL, NULL, SKINNY_OUTGOING);
  6144. if(!c) {
  6145. ast_log(LOG_WARNING, "Unable to create channel for %s@%s\n", l->name, d->name);
  6146. } else {
  6147. d->hookstate = SKINNY_OFFHOOK;
  6148. sub = ast_channel_tech_pvt(c);
  6149. dialandactivatesub(sub, req->data.enbloccallmessage.calledParty);
  6150. }
  6151. return 1;
  6152. }
  6153. static int handle_soft_key_event_message(struct skinny_req *req, struct skinnysession *s)
  6154. {
  6155. struct skinny_device *d = s->device;
  6156. struct skinny_line *l;
  6157. struct skinny_subchannel *sub = NULL;
  6158. struct ast_channel *c;
  6159. int event;
  6160. int instance;
  6161. int callreference;
  6162. event = letohl(req->data.softkeyeventmessage.softKeyEvent);
  6163. instance = letohl(req->data.softkeyeventmessage.instance);
  6164. callreference = letohl(req->data.softkeyeventmessage.callreference);
  6165. if (instance) {
  6166. l = find_line_by_instance(d, instance);
  6167. if (callreference) {
  6168. sub = find_subchannel_by_instance_reference(d, instance, callreference);
  6169. } else {
  6170. sub = find_subchannel_by_instance_reference(d, instance, d->lastcallreference);
  6171. }
  6172. } else {
  6173. l = find_line_by_instance(d, d->lastlineinstance);
  6174. }
  6175. if (!l) {
  6176. ast_log(LOG_WARNING, "Received Softkey Event: %d(%d/%d) but can't find line\n", event, instance, callreference);
  6177. return 0;
  6178. }
  6179. ast_devstate_changed(AST_DEVICE_INUSE, AST_DEVSTATE_CACHABLE, "Skinny/%s", l->name);
  6180. switch(event) {
  6181. case SOFTKEY_NONE:
  6182. SKINNY_DEBUG(DEBUG_PACKET, 3, "Received SOFTKEY_NONE from %s, inst %d, callref %d\n",
  6183. d->name, instance, callreference);
  6184. break;
  6185. case SOFTKEY_REDIAL:
  6186. SKINNY_DEBUG(DEBUG_PACKET, 3, "Received SOFTKEY_REDIAL from %s, inst %d, callref %d\n",
  6187. d->name, instance, callreference);
  6188. if (ast_strlen_zero(l->lastnumberdialed)) {
  6189. ast_log(LOG_WARNING, "Attempted redial, but no previously dialed number found. Ignoring button.\n");
  6190. break;
  6191. }
  6192. if (!sub || !sub->owner) {
  6193. c = skinny_new(l, NULL, AST_STATE_DOWN, NULL, NULL, SKINNY_OUTGOING);
  6194. } else {
  6195. c = sub->owner;
  6196. }
  6197. if (!c) {
  6198. ast_log(LOG_WARNING, "Unable to create channel for %s@%s\n", l->name, d->name);
  6199. } else {
  6200. sub = ast_channel_tech_pvt(c);
  6201. dialandactivatesub(sub, l->lastnumberdialed);
  6202. }
  6203. break;
  6204. case SOFTKEY_NEWCALL: /* Actually the DIAL softkey */
  6205. SKINNY_DEBUG(DEBUG_PACKET, 3, "Received SOFTKEY_NEWCALL from %s, inst %d, callref %d\n",
  6206. d->name, instance, callreference);
  6207. /* New Call ALWAYS gets a new sub-channel */
  6208. c = skinny_new(l, NULL, AST_STATE_DOWN, NULL, NULL, SKINNY_OUTGOING);
  6209. sub = ast_channel_tech_pvt(c);
  6210. if (!c) {
  6211. ast_log(LOG_WARNING, "Unable to create channel for %s@%s\n", l->name, d->name);
  6212. } else {
  6213. activatesub(sub, SUBSTATE_OFFHOOK);
  6214. }
  6215. break;
  6216. case SOFTKEY_HOLD:
  6217. SKINNY_DEBUG(DEBUG_PACKET, 3, "Received SOFTKEY_HOLD from %s, inst %d, callref %d\n",
  6218. d->name, instance, callreference);
  6219. if (sub) {
  6220. setsubstate(sub, SUBSTATE_HOLD);
  6221. } else { /* No sub, maybe an SLA call */
  6222. struct skinny_subline *subline;
  6223. if ((subline = find_subline_by_callid(d, callreference))) {
  6224. setsubstate(subline->sub, SUBSTATE_HOLD);
  6225. }
  6226. }
  6227. break;
  6228. case SOFTKEY_TRNSFER:
  6229. SKINNY_DEBUG(DEBUG_PACKET, 3, "Received SOFTKEY_TRNSFER from %s, inst %d, callref %d\n",
  6230. d->name, instance, callreference);
  6231. if (l->transfer)
  6232. handle_transfer_button(sub);
  6233. else
  6234. transmit_displaynotify(d, "Transfer disabled", 10);
  6235. break;
  6236. case SOFTKEY_DND:
  6237. SKINNY_DEBUG(DEBUG_PACKET, 3, "Received SOFTKEY_DND from %s, inst %d, callref %d\n",
  6238. d->name, instance, callreference);
  6239. /* Do not disturb */
  6240. if (l->dnd != 0){
  6241. ast_verb(3, "Disabling DND on %s@%s\n", l->name, d->name);
  6242. l->dnd = 0;
  6243. transmit_lamp_indication(d, STIMULUS_DND, 1, SKINNY_LAMP_ON);
  6244. transmit_displaynotify(d, "DnD disabled", 10);
  6245. } else {
  6246. ast_verb(3, "Enabling DND on %s@%s\n", l->name, d->name);
  6247. l->dnd = 1;
  6248. transmit_lamp_indication(d, STIMULUS_DND, 1, SKINNY_LAMP_OFF);
  6249. transmit_displaynotify(d, "DnD enabled", 10);
  6250. }
  6251. break;
  6252. case SOFTKEY_CFWDALL:
  6253. SKINNY_DEBUG(DEBUG_PACKET, 3, "Received SOFTKEY_CFWDALL from %s, inst %d, callref %d\n",
  6254. d->name, instance, callreference);
  6255. handle_callforward_button(l, sub, SKINNY_CFWD_ALL);
  6256. break;
  6257. case SOFTKEY_CFWDBUSY:
  6258. SKINNY_DEBUG(DEBUG_PACKET, 3, "Received SOFTKEY_CFWDBUSY from %s, inst %d, callref %d\n",
  6259. d->name, instance, callreference);
  6260. handle_callforward_button(l, sub, SKINNY_CFWD_BUSY);
  6261. break;
  6262. case SOFTKEY_CFWDNOANSWER:
  6263. SKINNY_DEBUG(DEBUG_PACKET, 3, "Received SOFTKEY_CFWDNOANSWER from %s, inst %d, callref %d\n",
  6264. d->name, instance, callreference);
  6265. handle_callforward_button(l, sub, SKINNY_CFWD_NOANSWER);
  6266. break;
  6267. case SOFTKEY_BKSPC:
  6268. SKINNY_DEBUG(DEBUG_PACKET, 3, "Received SOFTKEY_BKSPC from %s, inst %d, callref %d\n",
  6269. d->name, instance, callreference);
  6270. if (-1 < sub->dialer_sched && !skinny_sched_del(sub->dialer_sched, sub)) {
  6271. size_t len;
  6272. sub->dialer_sched = -1;
  6273. len = strlen(sub->exten);
  6274. if (len > 0) {
  6275. sub->exten[len-1] = '\0';
  6276. if (len == 1) {
  6277. transmit_start_tone(d, SKINNY_DIALTONE, l->instance, sub->callid);
  6278. transmit_selectsoftkeys(d, l->instance, sub->callid, KEYDEF_OFFHOOK, KEYMASK_ALL);
  6279. }
  6280. transmit_backspace(d, l->instance, sub->callid);
  6281. }
  6282. sub->dialer_sched = skinny_sched_add(gendigittimeout, skinny_dialer_cb, sub);
  6283. }
  6284. break;
  6285. case SOFTKEY_ENDCALL:
  6286. SKINNY_DEBUG(DEBUG_PACKET, 3, "Received SOFTKEY_ENDCALL from %s, inst %d, callref %d\n",
  6287. d->name, instance, callreference);
  6288. ast_devstate_changed(AST_DEVICE_NOT_INUSE, AST_DEVSTATE_CACHABLE, "Skinny/%s", l->name);
  6289. if (sub) {
  6290. dumpsub(sub, 1);
  6291. } else { /* No sub, maybe an SLA call */
  6292. struct skinny_subline *subline;
  6293. if ((subline = find_subline_by_callid(d, callreference))) {
  6294. dumpsub(subline->sub, 1);
  6295. }
  6296. }
  6297. d->hookstate = SKINNY_ONHOOK;
  6298. /* Not ideal, but let's send updated time at onhook and offhook, as it clears the display */
  6299. transmit_definetimedate(d);
  6300. break;
  6301. case SOFTKEY_RESUME:
  6302. SKINNY_DEBUG(DEBUG_PACKET, 3, "Received SOFTKEY_RESUME from %s, inst %d, callref %d\n",
  6303. d->name, instance, callreference);
  6304. if (sub) {
  6305. activatesub(sub, SUBSTATE_CONNECTED);
  6306. } else { /* No sub, maybe an inactive SLA call */
  6307. struct skinny_subline *subline;
  6308. subline = find_subline_by_callid(d, callreference);
  6309. c = skinny_new(l, subline, AST_STATE_DOWN, NULL, NULL, SKINNY_OUTGOING);
  6310. if (!c) {
  6311. ast_log(LOG_WARNING, "Unable to create channel for %s@%s\n", l->name, d->name);
  6312. } else {
  6313. sub = ast_channel_tech_pvt(c);
  6314. dialandactivatesub(sub, subline->exten);
  6315. }
  6316. }
  6317. break;
  6318. case SOFTKEY_ANSWER:
  6319. SKINNY_DEBUG(DEBUG_PACKET, 3, "Received SOFTKEY_ANSWER from %s, inst %d, callref %d\n",
  6320. d->name, instance, callreference);
  6321. transmit_ringer_mode(d, SKINNY_RING_OFF);
  6322. transmit_lamp_indication(d, STIMULUS_LINE, l->instance, SKINNY_LAMP_ON);
  6323. if (d->hookstate == SKINNY_ONHOOK) {
  6324. transmit_speaker_mode(d, SKINNY_SPEAKERON);
  6325. d->hookstate = SKINNY_OFFHOOK;
  6326. }
  6327. if (sub && sub->calldirection == SKINNY_INCOMING) {
  6328. activatesub(sub, SUBSTATE_CONNECTED);
  6329. }
  6330. break;
  6331. case SOFTKEY_INFO:
  6332. SKINNY_DEBUG(DEBUG_PACKET, 3, "Received SOFTKEY_INFO from %s, inst %d, callref %d\n",
  6333. d->name, instance, callreference);
  6334. break;
  6335. case SOFTKEY_CONFRN:
  6336. SKINNY_DEBUG(DEBUG_PACKET, 3, "Received SOFTKEY_CONFRN from %s, inst %d, callref %d\n",
  6337. d->name, instance, callreference);
  6338. /* XXX determine the best way to pull off a conference. Meetme? */
  6339. break;
  6340. case SOFTKEY_PARK:
  6341. {
  6342. char extout[AST_MAX_EXTENSION];
  6343. RAII_VAR(struct ast_bridge_channel *, bridge_channel, NULL, ao2_cleanup);
  6344. SKINNY_DEBUG(DEBUG_PACKET, 3, "Received SOFTKEY_PARK from %s, inst %d, callref %d\n",
  6345. d->name, instance, callreference);
  6346. if (!ast_parking_provider_registered()) {
  6347. transmit_displaynotify(d, "Call Park not available", 10);
  6348. break;
  6349. }
  6350. if ((sub && sub->owner) && (ast_channel_state(sub->owner) == AST_STATE_UP)) {
  6351. c = sub->owner;
  6352. ast_channel_lock(c);
  6353. bridge_channel = ast_channel_get_bridge_channel(c);
  6354. ast_channel_unlock(c);
  6355. if (!bridge_channel) {
  6356. transmit_displaynotify(d, "Call Park failed", 10);
  6357. break;
  6358. }
  6359. if (!ast_parking_park_call(bridge_channel, extout, sizeof(extout))) {
  6360. static const char msg_prefix[] = "Call Parked at: ";
  6361. char message[sizeof(msg_prefix) + sizeof(extout)];
  6362. snprintf(message, sizeof(message), "%s%s", msg_prefix, extout);
  6363. transmit_displaynotify(d, message, 10);
  6364. break;
  6365. }
  6366. transmit_displaynotify(d, "Call Park failed", 10);
  6367. } else {
  6368. transmit_displaynotify(d, "Call Park not available", 10);
  6369. }
  6370. break;
  6371. }
  6372. case SOFTKEY_JOIN:
  6373. SKINNY_DEBUG(DEBUG_PACKET, 3, "Received SOFTKEY_JOIN from %s, inst %d, callref %d\n",
  6374. d->name, instance, callreference);
  6375. /* this is SLA territory, should not get here unless there is a meetme at subline */
  6376. {
  6377. struct skinny_subline *subline;
  6378. subline = find_subline_by_callid(d, callreference);
  6379. c = skinny_new(l, subline, AST_STATE_DOWN, NULL, NULL, SKINNY_OUTGOING);
  6380. if (!c) {
  6381. ast_log(LOG_WARNING, "Unable to create channel for %s@%s\n", l->name, d->name);
  6382. } else {
  6383. sub = ast_channel_tech_pvt(c);
  6384. dialandactivatesub(sub, subline->exten);
  6385. }
  6386. }
  6387. break;
  6388. case SOFTKEY_MEETME:
  6389. /* XXX How is this different from CONFRN? */
  6390. SKINNY_DEBUG(DEBUG_PACKET, 3, "Received SOFTKEY_MEETME from %s, inst %d, callref %d\n",
  6391. d->name, instance, callreference);
  6392. break;
  6393. case SOFTKEY_PICKUP:
  6394. SKINNY_DEBUG(DEBUG_PACKET, 3, "Received SOFTKEY_PICKUP from %s, inst %d, callref %d\n",
  6395. d->name, instance, callreference);
  6396. break;
  6397. case SOFTKEY_GPICKUP:
  6398. SKINNY_DEBUG(DEBUG_PACKET, 3, "Received SOFTKEY_GPICKUP from %s, inst %d, callref %d\n",
  6399. d->name, instance, callreference);
  6400. if (!sub || !sub->owner) {
  6401. c = skinny_new(l, NULL, AST_STATE_DOWN, NULL, NULL, SKINNY_INCOMING);
  6402. } else {
  6403. c = sub->owner;
  6404. }
  6405. if (!c) {
  6406. ast_log(LOG_WARNING, "Unable to create channel for %s@%s\n", l->name, d->name);
  6407. } else {
  6408. ast_channel_ref(c);
  6409. sub = ast_channel_tech_pvt(c);
  6410. ast_pickup_call(c);
  6411. if (sub->owner == c) {
  6412. ast_channel_unref(c);
  6413. dumpsub(sub, 1);
  6414. } else {
  6415. ast_hangup(c);
  6416. setsubstate(sub, SUBSTATE_CONNECTED);
  6417. ast_channel_unref(c);
  6418. }
  6419. }
  6420. break;
  6421. case SOFTKEY_FORCEDIAL:
  6422. SKINNY_DEBUG(DEBUG_PACKET, 3, "Received SOFTKEY_FORCEDIAL from %s, inst %d, callref %d\n",
  6423. d->name, instance, callreference);
  6424. skinny_dialer(sub, 1);
  6425. break;
  6426. default:
  6427. SKINNY_DEBUG(DEBUG_PACKET, 3, "Received SOFTKEY_UNKNOWN(%d) from %s, inst %d, callref %d\n",
  6428. event, d->name, instance, callreference);
  6429. break;
  6430. }
  6431. return 1;
  6432. }
  6433. static int handle_message(struct skinny_req *req, struct skinnysession *s)
  6434. {
  6435. int res = 0;
  6436. struct skinny_speeddial *sd;
  6437. struct skinny_device *d = s->device;
  6438. if (!d && !(letohl(req->e) == REGISTER_MESSAGE || letohl(req->e) == ALARM_MESSAGE || letohl(req->e) == KEEP_ALIVE_MESSAGE)) {
  6439. ast_log(LOG_WARNING, "Client sent message #%u without first registering.\n", req->e);
  6440. return 0;
  6441. }
  6442. switch(letohl(req->e)) {
  6443. case KEEP_ALIVE_MESSAGE:
  6444. SKINNY_DEBUG(DEBUG_PACKET, 3, "Received KEEP_ALIVE_MESSAGE from %s\n", (d ? d->name : "unregistered"));
  6445. handle_keepalive_message(req, s);
  6446. break;
  6447. case REGISTER_MESSAGE:
  6448. SKINNY_DEBUG(DEBUG_PACKET, 3, "Received REGISTER_MESSAGE from %s, name %s, type %u, protovers %d\n",
  6449. d->name, req->data.reg.name, letohl(req->data.reg.type), letohl(req->data.reg.protocolVersion));
  6450. res = skinny_register(req, s);
  6451. if (!res) {
  6452. sleep(2);
  6453. res = skinny_register(req, s);
  6454. }
  6455. if (res != 1) {
  6456. transmit_registerrej(s);
  6457. return -1;
  6458. }
  6459. ast_atomic_fetchadd_int(&unauth_sessions, -1);
  6460. ast_verb(3, "Device '%s' successfully registered (protoVers %d)\n", s->device->name, s->device->protocolversion);
  6461. transmit_registerack(s->device);
  6462. transmit_capabilitiesreq(s->device);
  6463. res = 0;
  6464. break;
  6465. case IP_PORT_MESSAGE:
  6466. SKINNY_DEBUG(DEBUG_PACKET, 3, "Received IP_PORT_MESSAGE from %s\n", d->name);
  6467. res = handle_ip_port_message(req, s);
  6468. break;
  6469. case KEYPAD_BUTTON_MESSAGE:
  6470. SKINNY_DEBUG(DEBUG_PACKET, 3, "Received KEYPAD_BUTTON_MESSAGE from %s, digit %u, inst %u, callref %u\n",
  6471. d->name, letohl(req->data.keypad.button), letohl(req->data.keypad.lineInstance), letohl(req->data.keypad.callReference));
  6472. res = handle_keypad_button_message(req, s);
  6473. break;
  6474. case ENBLOC_CALL_MESSAGE:
  6475. SKINNY_DEBUG(DEBUG_PACKET, 3, "Received ENBLOC_CALL_MESSAGE from %s, calledParty %s\n",
  6476. d->name, req->data.enbloccallmessage.calledParty);
  6477. res = handle_enbloc_call_message(req, s);
  6478. break;
  6479. case STIMULUS_MESSAGE:
  6480. /* SKINNY_PACKETDEBUG handled in handle_stimulus_message */
  6481. res = handle_stimulus_message(req, s);
  6482. break;
  6483. case OFFHOOK_MESSAGE:
  6484. SKINNY_DEBUG(DEBUG_PACKET, 3, "Received OFFHOOK_MESSAGE from %s, inst %u, ref %u\n",
  6485. d->name, letohl(req->data.offhook.instance), letohl(req->data.offhook.reference));
  6486. res = handle_offhook_message(req, s);
  6487. break;
  6488. case ONHOOK_MESSAGE:
  6489. SKINNY_DEBUG(DEBUG_PACKET, 3, "Received ONHOOK_MESSAGE from %s, inst %u, ref %u\n",
  6490. d->name, letohl(req->data.offhook.instance), letohl(req->data.offhook.reference));
  6491. res = handle_onhook_message(req, s);
  6492. break;
  6493. case CAPABILITIES_RES_MESSAGE:
  6494. SKINNY_DEBUG(DEBUG_PACKET | DEBUG_AUDIO, 3, "Received CAPABILITIES_RES_MESSAGE from %s, count %u, codec data\n",
  6495. d->name, letohl(req->data.caps.count));
  6496. res = handle_capabilities_res_message(req, s);
  6497. break;
  6498. case SPEED_DIAL_STAT_REQ_MESSAGE:
  6499. SKINNY_DEBUG(DEBUG_PACKET, 3, "Received SPEED_DIAL_STAT_REQ_MESSAGE from %s, sdNum %u\n",
  6500. d->name, letohl(req->data.speeddialreq.speedDialNumber));
  6501. if ( (sd = find_speeddial_by_instance(s->device, letohl(req->data.speeddialreq.speedDialNumber), 0)) ) {
  6502. transmit_speeddialstatres(d, sd);
  6503. }
  6504. break;
  6505. case LINE_STATE_REQ_MESSAGE:
  6506. SKINNY_DEBUG(DEBUG_PACKET, 3, "Received LINE_STATE_REQ_MESSAGE from %s, lineNum %u\n",
  6507. d->name, letohl(req->data.line.lineNumber));
  6508. transmit_linestatres(d, letohl(req->data.line.lineNumber));
  6509. break;
  6510. case TIME_DATE_REQ_MESSAGE:
  6511. SKINNY_DEBUG(DEBUG_PACKET, 3, "Received TIME_DATE_REQ_MESSAGE from %s\n", d->name);
  6512. transmit_definetimedate(d);
  6513. break;
  6514. case BUTTON_TEMPLATE_REQ_MESSAGE:
  6515. SKINNY_DEBUG(DEBUG_PACKET, 3, "Received BUTTON_TEMPLATE_REQ_MESSAGE from %s\n", d->name);
  6516. res = handle_button_template_req_message(req, s);
  6517. break;
  6518. case VERSION_REQ_MESSAGE:
  6519. SKINNY_DEBUG(DEBUG_PACKET, 3, "Received VERSION_REQ_MESSAGE from %s\n", d->name);
  6520. transmit_versionres(d);
  6521. break;
  6522. case SERVER_REQUEST_MESSAGE:
  6523. SKINNY_DEBUG(DEBUG_PACKET, 3, "Received SERVER_REQUEST_MESSAGE from %s\n", d->name);
  6524. transmit_serverres(d);
  6525. break;
  6526. case ALARM_MESSAGE:
  6527. /* no response necessary */
  6528. SKINNY_DEBUG(DEBUG_PACKET, 3, "Received ALARM_MESSAGE from %s, alarm %s\n",
  6529. d->name, req->data.alarm.displayMessage);
  6530. break;
  6531. case OPEN_RECEIVE_CHANNEL_ACK_MESSAGE:
  6532. /* SKINNY_PACKETDEBUG handled in handle_open_receive_channel_ack_message */
  6533. res = handle_open_receive_channel_ack_message(req, s);
  6534. break;
  6535. case SOFT_KEY_SET_REQ_MESSAGE:
  6536. SKINNY_DEBUG(DEBUG_PACKET, 3, "Received SOFT_KEY_SET_REQ_MESSAGE from %s\n", d->name);
  6537. transmit_softkeysetres(d);
  6538. transmit_selectsoftkeys(d, 0, 0, KEYDEF_ONHOOK, KEYMASK_ALL);
  6539. break;
  6540. case SOFT_KEY_EVENT_MESSAGE:
  6541. /* SKINNY_PACKETDEBUG handled in handle_soft_key_event_message */
  6542. res = handle_soft_key_event_message(req, s);
  6543. break;
  6544. case UNREGISTER_MESSAGE:
  6545. SKINNY_DEBUG(DEBUG_PACKET, 3, "Received UNREGISTER_MESSAGE from %s\n", d->name);
  6546. ast_log(LOG_NOTICE, "Received UNREGISTER_MESSAGE from %s\n", d->name);
  6547. end_session(s);
  6548. break;
  6549. case SOFT_KEY_TEMPLATE_REQ_MESSAGE:
  6550. SKINNY_DEBUG(DEBUG_PACKET, 3, "Received SOFT_KEY_TEMPLATE_REQ_MESSAGE from %s\n", d->name);
  6551. transmit_softkeytemplateres(d);
  6552. break;
  6553. case HEADSET_STATUS_MESSAGE:
  6554. /* XXX umm...okay? Why do I care? */
  6555. SKINNY_DEBUG(DEBUG_PACKET, 3, "Received HEADSET_STATUS_MESSAGE from %s\n", d->name);
  6556. break;
  6557. case REGISTER_AVAILABLE_LINES_MESSAGE:
  6558. /* XXX I have no clue what this is for, but my phone was sending it, so... */
  6559. SKINNY_DEBUG(DEBUG_PACKET, 3, "Received REGISTER_AVAILABLE_LINES_MESSAGE from %s\n", d->name);
  6560. break;
  6561. case SERVICEURL_STATREQ_MESSAGE:
  6562. SKINNY_DEBUG(DEBUG_PACKET, 3, "SERVICEURL_STATREQ_MESSAGE from %s\n", d->name);
  6563. transmit_serviceurlstat(d, letohl(req->data.serviceurlmessage.instance));
  6564. break;
  6565. default:
  6566. SKINNY_DEBUG(DEBUG_PACKET, 3, "Received UNKNOWN_MESSAGE(%x) from %s\n", (unsigned)letohl(req->e), d->name);
  6567. break;
  6568. }
  6569. return res;
  6570. }
  6571. static void destroy_session(struct skinnysession *s)
  6572. {
  6573. ast_mutex_lock(&s->lock);
  6574. if (s->fd > -1) {
  6575. close(s->fd);
  6576. }
  6577. if (s->device) {
  6578. s->device->session = NULL;
  6579. } else {
  6580. ast_atomic_fetchadd_int(&unauth_sessions, -1);
  6581. }
  6582. ast_mutex_unlock(&s->lock);
  6583. ast_mutex_destroy(&s->lock);
  6584. if (s->t != AST_PTHREADT_NULL) {
  6585. pthread_detach(s->t);
  6586. }
  6587. ast_free(s);
  6588. }
  6589. static int skinny_noauth_cb(const void *data)
  6590. {
  6591. struct skinnysession *s = (struct skinnysession *)data;
  6592. ast_log(LOG_WARNING, "Skinny Client failed to authenticate in %d seconds (SCHED %d)\n", auth_timeout, s->auth_timeout_sched);
  6593. s->auth_timeout_sched = -1;
  6594. end_session(s);
  6595. return 0;
  6596. }
  6597. static int skinny_nokeepalive_cb(const void *data)
  6598. {
  6599. struct skinnysession *s = (struct skinnysession *)data;
  6600. ast_log(LOG_WARNING, "Skinny Client failed to send keepalive in last %d seconds (SCHED %d)\n", keep_alive*3, s->keepalive_timeout_sched);
  6601. s->keepalive_timeout_sched = -1;
  6602. end_session(s);
  6603. return 0;
  6604. }
  6605. static void skinny_session_cleanup(void *data)
  6606. {
  6607. struct skinnysession *s = (struct skinnysession *)data;
  6608. struct skinny_device *d = s->device;
  6609. struct skinny_line *l;
  6610. struct skinny_speeddial *sd;
  6611. ast_log(LOG_NOTICE, "Ending Skinny session from %s at %s\n", d ? d->name : "unknown", ast_inet_ntoa(s->sin.sin_addr));
  6612. if (s->lockstate) {
  6613. ast_mutex_unlock(&s->lock);
  6614. }
  6615. if (-1 < s->auth_timeout_sched) {
  6616. not_used = ast_sched_del(sched, s->auth_timeout_sched);
  6617. s->auth_timeout_sched = -1;
  6618. }
  6619. if (-1 < s->keepalive_timeout_sched) {
  6620. not_used = ast_sched_del(sched, s->keepalive_timeout_sched);
  6621. s->keepalive_timeout_sched = -1;
  6622. }
  6623. if (d) {
  6624. RAII_VAR(struct ast_json *, blob, NULL, ast_json_unref);
  6625. d->session = NULL;
  6626. AST_LIST_TRAVERSE(&d->speeddials, sd, list) {
  6627. if (sd->stateid > -1)
  6628. ast_extension_state_del(sd->stateid, NULL);
  6629. }
  6630. AST_LIST_TRAVERSE(&d->lines, l, list) {
  6631. if (l->device != d) {
  6632. continue;
  6633. }
  6634. ast_format_cap_remove_by_type(l->cap, AST_MEDIA_TYPE_UNKNOWN);
  6635. ast_format_cap_update_by_allow_disallow(l->cap, "all", 0);
  6636. l->instance = 0;
  6637. unregister_exten(l);
  6638. ast_devstate_changed(AST_DEVICE_UNAVAILABLE, AST_DEVSTATE_CACHABLE, "Skinny/%s", l->name);
  6639. }
  6640. ast_endpoint_set_state(d->endpoint, AST_ENDPOINT_OFFLINE);
  6641. blob = ast_json_pack("{s: s}", "peer_status", "Unregistered");
  6642. ast_endpoint_blob_publish(d->endpoint, ast_endpoint_state_type(), blob);
  6643. }
  6644. AST_LIST_LOCK(&sessions);
  6645. AST_LIST_REMOVE(&sessions, s, list);
  6646. AST_LIST_UNLOCK(&sessions);
  6647. destroy_session(s);
  6648. }
  6649. #define PACKET_TIMEOUT 10000
  6650. static void *skinny_session(void *data)
  6651. {
  6652. int res;
  6653. int bytesread;
  6654. struct skinny_req *req = NULL;
  6655. struct skinnysession *s = data;
  6656. int dlen = 0;
  6657. int eventmessage = 0;
  6658. struct pollfd fds[1];
  6659. ast_log(LOG_NOTICE, "Starting Skinny session from %s\n", ast_inet_ntoa(s->sin.sin_addr));
  6660. pthread_cleanup_push(skinny_session_cleanup, s);
  6661. s->start = ast_tvnow();
  6662. s->last_keepalive = ast_tvnow();
  6663. s->keepalive_count = 0;
  6664. s->lockstate = 0;
  6665. AST_LIST_LOCK(&sessions);
  6666. AST_LIST_INSERT_HEAD(&sessions, s, list);
  6667. AST_LIST_UNLOCK(&sessions);
  6668. s->auth_timeout_sched = ast_sched_add(sched, auth_timeout*1000, skinny_noauth_cb, s);
  6669. s->keepalive_timeout_sched = ast_sched_add(sched, keep_alive*3000, skinny_nokeepalive_cb, s);
  6670. for (;;) {
  6671. fds[0].fd = s->fd;
  6672. fds[0].events = POLLIN;
  6673. fds[0].revents = 0;
  6674. res = ast_poll(fds, 1, -1); /* block */
  6675. if (res < 0) {
  6676. if (errno != EINTR) {
  6677. ast_log(LOG_WARNING, "Select returned error: %s\n", strerror(errno));
  6678. ast_verb(3, "Ending Skinny session from %s (bad input)\n", ast_inet_ntoa(s->sin.sin_addr));
  6679. break;
  6680. }
  6681. }
  6682. if (!fds[0].revents) {
  6683. continue;
  6684. }
  6685. ast_debug(1, "Reading header\n");
  6686. if (!(req = ast_calloc(1, SKINNY_MAX_PACKET))) {
  6687. ast_log(LOG_WARNING, "Unable to allocated memorey for skinny_req.\n");
  6688. break;
  6689. }
  6690. ast_mutex_lock(&s->lock);
  6691. s->lockstate = 1;
  6692. if ((res = read(s->fd, req, skinny_header_size)) != skinny_header_size) {
  6693. if (res < 0) {
  6694. ast_log(LOG_WARNING, "Header read() returned error: %s\n", strerror(errno));
  6695. } else {
  6696. ast_log(LOG_WARNING, "Unable to read header. Only found %d bytes.\n", res);
  6697. }
  6698. break;
  6699. }
  6700. eventmessage = letohl(req->e);
  6701. if (eventmessage < 0) {
  6702. ast_log(LOG_ERROR, "Event Message is NULL from socket %d, This is bad\n", s->fd);
  6703. break;
  6704. }
  6705. dlen = letohl(req->len) - 4;
  6706. if (dlen < 0) {
  6707. ast_log(LOG_WARNING, "Skinny Client sent invalid data.\n");
  6708. break;
  6709. }
  6710. if (dlen > (SKINNY_MAX_PACKET - skinny_header_size)) {
  6711. ast_log(LOG_WARNING, "Skinny packet too large (%d bytes), max length(%d bytes)\n", dlen+8, SKINNY_MAX_PACKET);
  6712. break;
  6713. }
  6714. ast_debug(1, "Read header: Message ID: 0x%04x, %d bytes in packet\n", eventmessage, dlen);
  6715. bytesread = 0;
  6716. while (bytesread < dlen) {
  6717. ast_debug(1, "Waiting %dms for %d bytes of %d\n", PACKET_TIMEOUT, dlen - bytesread, dlen);
  6718. fds[0].revents = 0;
  6719. res = ast_poll(fds, 1, PACKET_TIMEOUT);
  6720. if (res <= 0) {
  6721. if (res == 0) {
  6722. ast_debug(1, "Poll timed out waiting for %d bytes\n", dlen - bytesread);
  6723. } else {
  6724. ast_log(LOG_WARNING, "Poll failed waiting for %d bytes: %s\n",
  6725. dlen - bytesread, strerror(errno));
  6726. }
  6727. ast_verb(3, "Ending Skinny session from %s (bad input)\n", ast_inet_ntoa(s->sin.sin_addr));
  6728. res = -1;
  6729. break;
  6730. }
  6731. if (!fds[0].revents) {
  6732. continue;
  6733. }
  6734. res = read(s->fd, ((char*)&req->data)+bytesread, dlen-bytesread);
  6735. if (res < 0) {
  6736. ast_log(LOG_WARNING, "Data read() returned error: %s\n", strerror(errno));
  6737. break;
  6738. }
  6739. bytesread += res;
  6740. ast_debug(1, "Read %d bytes. %d of %d now read\n", res, bytesread, dlen);
  6741. }
  6742. s->lockstate = 0;
  6743. ast_mutex_unlock(&s->lock);
  6744. if (res < 0) {
  6745. break;
  6746. }
  6747. pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, NULL);
  6748. res = handle_message(req, s);
  6749. pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL);
  6750. if (req) {
  6751. ast_free(req);
  6752. req = NULL;
  6753. }
  6754. }
  6755. ast_log(LOG_NOTICE, "Skinny Session returned: %s\n", strerror(errno));
  6756. if (req) {
  6757. ast_free(req);
  6758. }
  6759. pthread_cleanup_pop(1);
  6760. return 0;
  6761. }
  6762. static void *accept_thread(void *ignore)
  6763. {
  6764. int as;
  6765. struct sockaddr_in sin;
  6766. socklen_t sinlen;
  6767. struct skinnysession *s;
  6768. int arg = 1;
  6769. for (;;) {
  6770. sinlen = sizeof(sin);
  6771. as = accept(skinnysock, (struct sockaddr *)&sin, &sinlen);
  6772. if (as < 0) {
  6773. ast_log(LOG_NOTICE, "Accept returned -1: %s\n", strerror(errno));
  6774. continue;
  6775. }
  6776. if (ast_atomic_fetchadd_int(&unauth_sessions, +1) >= auth_limit) {
  6777. close(as);
  6778. ast_atomic_fetchadd_int(&unauth_sessions, -1);
  6779. continue;
  6780. }
  6781. if (setsockopt(as, IPPROTO_TCP, TCP_NODELAY, (char *) &arg, sizeof(arg)) < 0) {
  6782. ast_log(LOG_WARNING, "Failed to set TCP_NODELAY on Skinny TCP connection: %s\n", strerror(errno));
  6783. }
  6784. if (!(s = ast_calloc(1, sizeof(struct skinnysession)))) {
  6785. close(as);
  6786. ast_atomic_fetchadd_int(&unauth_sessions, -1);
  6787. continue;
  6788. }
  6789. ast_mutex_init(&s->lock);
  6790. memcpy(&s->sin, &sin, sizeof(sin));
  6791. s->fd = as;
  6792. s->auth_timeout_sched = -1;
  6793. s->keepalive_timeout_sched = -1;
  6794. if (ast_pthread_create(&s->t, NULL, skinny_session, s)) {
  6795. s->t = AST_PTHREADT_NULL;
  6796. destroy_session(s);
  6797. }
  6798. }
  6799. SKINNY_DEBUG(DEBUG_THREAD, 3, "Killing accept thread\n");
  6800. close(as);
  6801. return 0;
  6802. }
  6803. static int skinny_devicestate(const char *data)
  6804. {
  6805. struct skinny_line *l;
  6806. char *tmp;
  6807. tmp = ast_strdupa(data);
  6808. l = find_line_by_name(tmp);
  6809. return get_devicestate(l);
  6810. }
  6811. static struct ast_channel *skinny_request(const char *type, struct ast_format_cap *cap, const struct ast_assigned_ids *assignedids, const struct ast_channel *requestor, const char *dest, int *cause)
  6812. {
  6813. struct skinny_line *l;
  6814. struct skinny_subline *subline = NULL;
  6815. struct ast_channel *tmpc = NULL;
  6816. char tmp[256];
  6817. if (!(ast_format_cap_has_type(cap, AST_MEDIA_TYPE_AUDIO))) {
  6818. struct ast_str *codec_buf = ast_str_alloca(AST_FORMAT_CAP_NAMES_LEN);
  6819. ast_log(LOG_NOTICE, "Asked to get a channel of unsupported format '%s'\n", ast_format_cap_get_names(cap, &codec_buf));
  6820. return NULL;
  6821. }
  6822. ast_copy_string(tmp, dest, sizeof(tmp));
  6823. if (ast_strlen_zero(tmp)) {
  6824. ast_log(LOG_NOTICE, "Skinny channels require a device\n");
  6825. return NULL;
  6826. }
  6827. l = find_line_by_name(tmp);
  6828. if (!l) {
  6829. subline = find_subline_by_name(tmp);
  6830. if (!subline) {
  6831. ast_log(LOG_NOTICE, "No available lines on: %s\n", dest);
  6832. return NULL;
  6833. }
  6834. l = subline->line;
  6835. }
  6836. ast_verb(3, "skinny_request(%s)\n", tmp);
  6837. tmpc = skinny_new(l, subline, AST_STATE_DOWN, assignedids, requestor, SKINNY_INCOMING);
  6838. if (!tmpc) {
  6839. ast_log(LOG_WARNING, "Unable to make channel for '%s'\n", tmp);
  6840. } else if (subline) {
  6841. struct skinny_subchannel *sub = ast_channel_tech_pvt(tmpc);
  6842. subline->sub = sub;
  6843. subline->calldirection = SKINNY_INCOMING;
  6844. subline->substate = SUBSTATE_UNSET;
  6845. subline->callid = sub->callid;
  6846. sub->subline = subline;
  6847. }
  6848. return tmpc;
  6849. }
  6850. #define TYPE_GENERAL 1
  6851. #define TYPE_DEF_DEVICE 2
  6852. #define TYPE_DEF_LINE 4
  6853. #define TYPE_DEVICE 8
  6854. #define TYPE_LINE 16
  6855. #define CLINE_OPTS ((struct skinny_line_options *)item)
  6856. #define CLINE ((struct skinny_line *)item)
  6857. #define CDEV_OPTS ((struct skinny_device_options *)item)
  6858. #define CDEV ((struct skinny_device *)item)
  6859. static void config_parse_variables(int type, void *item, struct ast_variable *vptr)
  6860. {
  6861. struct ast_variable *v;
  6862. int lineInstance = 1;
  6863. int speeddialInstance = 1;
  6864. int serviceUrlInstance = 1;
  6865. while(vptr) {
  6866. v = vptr;
  6867. vptr = vptr->next;
  6868. if (type & (TYPE_GENERAL)) {
  6869. char newcontexts[AST_MAX_CONTEXT];
  6870. char oldcontexts[AST_MAX_CONTEXT];
  6871. char *stringp, *context, *oldregcontext;
  6872. if (!ast_jb_read_conf(&global_jbconf, v->name, v->value)) {
  6873. v = v->next;
  6874. continue;
  6875. }
  6876. if (!strcasecmp(v->name, "bindaddr")) {
  6877. if (!(hp = ast_gethostbyname(v->value, &ahp))) {
  6878. ast_log(LOG_WARNING, "Invalid address: %s\n", v->value);
  6879. } else {
  6880. memcpy(&bindaddr.sin_addr, hp->h_addr, sizeof(bindaddr.sin_addr));
  6881. }
  6882. continue;
  6883. } else if (!strcasecmp(v->name, "keepalive")) {
  6884. keep_alive = atoi(v->value);
  6885. continue;
  6886. } else if (!strcasecmp(v->name, "authtimeout")) {
  6887. int timeout = atoi(v->value);
  6888. if (timeout < 1) {
  6889. ast_log(LOG_WARNING, "Invalid authtimeout value '%s', using default value\n", v->value);
  6890. auth_timeout = DEFAULT_AUTH_TIMEOUT;
  6891. } else {
  6892. auth_timeout = timeout;
  6893. }
  6894. continue;
  6895. } else if (!strcasecmp(v->name, "authlimit")) {
  6896. int limit = atoi(v->value);
  6897. if (limit < 1) {
  6898. ast_log(LOG_WARNING, "Invalid authlimit value '%s', using default value\n", v->value);
  6899. auth_limit = DEFAULT_AUTH_LIMIT;
  6900. } else {
  6901. auth_limit = limit;
  6902. }
  6903. continue;
  6904. } else if (!strcasecmp(v->name, "regcontext")) {
  6905. ast_copy_string(newcontexts, v->value, sizeof(newcontexts));
  6906. stringp = newcontexts;
  6907. /* Initialize copy of current global_regcontext for later use in removing stale contexts */
  6908. ast_copy_string(oldcontexts, regcontext, sizeof(oldcontexts));
  6909. oldregcontext = oldcontexts;
  6910. /* Let's remove any contexts that are no longer defined in regcontext */
  6911. cleanup_stale_contexts(stringp, oldregcontext);
  6912. /* Create contexts if they don't exist already */
  6913. while ((context = strsep(&stringp, "&"))) {
  6914. ast_copy_string(used_context, context, sizeof(used_context));
  6915. ast_context_find_or_create(NULL, NULL, context, "Skinny");
  6916. }
  6917. ast_copy_string(regcontext, v->value, sizeof(regcontext));
  6918. continue;
  6919. } else if (!strcasecmp(v->name, "vmexten")) {
  6920. ast_copy_string(vmexten, v->value, sizeof(vmexten));
  6921. continue;
  6922. } else if (!strcasecmp(v->name, "immeddialkey")) {
  6923. if (!strcmp(v->value,"#")) {
  6924. immed_dialchar = '#';
  6925. } else if (!strcmp(v->value,"*")) {
  6926. immed_dialchar = '*';
  6927. } else {
  6928. ast_log(LOG_WARNING, "Invalid immeddialkey '%s' at line %d, only # or * accepted. Immeddial key disabled\n", v->value, v->lineno);
  6929. }
  6930. continue;
  6931. } else if (!strcasecmp(v->name, "dateformat")) {
  6932. memcpy(date_format, v->value, sizeof(date_format));
  6933. continue;
  6934. } else if (!strcasecmp(v->name, "tos")) {
  6935. if (ast_str2tos(v->value, &qos.tos))
  6936. ast_log(LOG_WARNING, "Invalid tos value at line %d, refer to QoS documentation\n", v->lineno);
  6937. continue;
  6938. } else if (!strcasecmp(v->name, "tos_audio")) {
  6939. if (ast_str2tos(v->value, &qos.tos_audio))
  6940. ast_log(LOG_WARNING, "Invalid tos_audio value at line %d, refer to QoS documentation\n", v->lineno);
  6941. continue;
  6942. } else if (!strcasecmp(v->name, "tos_video")) {
  6943. if (ast_str2tos(v->value, &qos.tos_video))
  6944. ast_log(LOG_WARNING, "Invalid tos_video value at line %d, refer to QoS documentation\n", v->lineno);
  6945. continue;
  6946. } else if (!strcasecmp(v->name, "cos")) {
  6947. if (ast_str2cos(v->value, &qos.cos))
  6948. ast_log(LOG_WARNING, "Invalid cos value at line %d, refer to QoS documentation\n", v->lineno);
  6949. continue;
  6950. } else if (!strcasecmp(v->name, "cos_audio")) {
  6951. if (ast_str2cos(v->value, &qos.cos_audio))
  6952. ast_log(LOG_WARNING, "Invalid cos_audio value at line %d, refer to QoS documentation\n", v->lineno);
  6953. continue;
  6954. } else if (!strcasecmp(v->name, "cos_video")) {
  6955. if (ast_str2cos(v->value, &qos.cos_video))
  6956. ast_log(LOG_WARNING, "Invalid cos_video value at line %d, refer to QoS documentation\n", v->lineno);
  6957. continue;
  6958. } else if (!strcasecmp(v->name, "bindport")) {
  6959. if (sscanf(v->value, "%5d", &ourport) == 1) {
  6960. bindaddr.sin_port = htons(ourport);
  6961. } else {
  6962. ast_log(LOG_WARNING, "Invalid bindport '%s' at line %d of %s\n", v->value, v->lineno, config);
  6963. }
  6964. continue;
  6965. } else if (!strcasecmp(v->name, "allow")) {
  6966. ast_format_cap_update_by_allow_disallow(default_cap, v->value, 1);
  6967. continue;
  6968. } else if (!strcasecmp(v->name, "disallow")) {
  6969. ast_format_cap_update_by_allow_disallow(default_cap, v->value, 0);
  6970. continue;
  6971. }
  6972. }
  6973. if (!strcasecmp(v->name, "transfer")) {
  6974. if (type & (TYPE_DEF_DEVICE | TYPE_DEVICE)) {
  6975. CDEV_OPTS->transfer = ast_true(v->value);
  6976. continue;
  6977. } else if (type & (TYPE_DEF_LINE | TYPE_LINE)) {
  6978. CLINE_OPTS->transfer = ast_true(v->value);
  6979. continue;
  6980. }
  6981. } else if (!strcasecmp(v->name, "callwaiting")) {
  6982. if (type & (TYPE_DEF_DEVICE | TYPE_DEVICE)) {
  6983. CDEV_OPTS->callwaiting = ast_true(v->value);
  6984. continue;
  6985. } else if (type & (TYPE_DEF_LINE | TYPE_LINE)) {
  6986. CLINE_OPTS->callwaiting = ast_true(v->value);
  6987. continue;
  6988. }
  6989. } else if (!strcasecmp(v->name, "directmedia") || !strcasecmp(v->name, "canreinvite")) {
  6990. if (type & (TYPE_DEF_LINE | TYPE_LINE)) {
  6991. CLINE_OPTS->directmedia = ast_true(v->value);
  6992. continue;
  6993. }
  6994. } else if (!strcasecmp(v->name, "nat")) {
  6995. if (type & (TYPE_DEF_LINE | TYPE_LINE)) {
  6996. CLINE_OPTS->nat = ast_true(v->value);
  6997. continue;
  6998. }
  6999. } else if (!strcasecmp(v->name, "context")) {
  7000. if (type & (TYPE_DEF_LINE | TYPE_LINE)) {
  7001. ast_copy_string(CLINE_OPTS->context, v->value, sizeof(CLINE_OPTS->context));
  7002. continue;
  7003. }
  7004. }else if (!strcasecmp(v->name, "vmexten")) {
  7005. if (type & (TYPE_DEF_DEVICE | TYPE_DEVICE)) {
  7006. ast_copy_string(CDEV_OPTS->vmexten, v->value, sizeof(CDEV_OPTS->vmexten));
  7007. continue;
  7008. } else if (type & (TYPE_DEF_LINE | TYPE_LINE)) {
  7009. ast_copy_string(CLINE_OPTS->vmexten, v->value, sizeof(CLINE_OPTS->vmexten));
  7010. continue;
  7011. }
  7012. } else if (!strcasecmp(v->name, "mwiblink")) {
  7013. if (type & (TYPE_DEF_DEVICE | TYPE_DEVICE)) {
  7014. CDEV_OPTS->mwiblink = ast_true(v->value);
  7015. continue;
  7016. } else if (type & (TYPE_DEF_LINE | TYPE_LINE)) {
  7017. CLINE_OPTS->mwiblink = ast_true(v->value);
  7018. continue;
  7019. }
  7020. } else if (!strcasecmp(v->name, "linelabel")) {
  7021. if (type & (TYPE_DEF_LINE | TYPE_LINE)) {
  7022. ast_copy_string(CLINE_OPTS->label, v->value, sizeof(CLINE_OPTS->label));
  7023. continue;
  7024. }
  7025. } else if (!strcasecmp(v->name, "callerid")) {
  7026. if (type & (TYPE_DEF_LINE | TYPE_LINE)) {
  7027. if (!strcasecmp(v->value, "asreceived")) {
  7028. CLINE_OPTS->cid_num[0] = '\0';
  7029. CLINE_OPTS->cid_name[0] = '\0';
  7030. } else {
  7031. ast_callerid_split(v->value, CLINE_OPTS->cid_name, sizeof(CLINE_OPTS->cid_name), CLINE_OPTS->cid_num, sizeof(CLINE_OPTS->cid_num));
  7032. }
  7033. continue;
  7034. }
  7035. } else if (!strcasecmp(v->name, "amaflags")) {
  7036. if (type & (TYPE_DEF_LINE | TYPE_LINE)) {
  7037. int tempamaflags = ast_channel_string2amaflag(v->value);
  7038. if (tempamaflags < 0) {
  7039. ast_log(LOG_WARNING, "Invalid AMA flags: %s at line %d\n", v->value, v->lineno);
  7040. } else {
  7041. CLINE_OPTS->amaflags = tempamaflags;
  7042. }
  7043. continue;
  7044. }
  7045. } else if (!strcasecmp(v->name, "regexten")) {
  7046. if (type & (TYPE_DEF_LINE | TYPE_LINE)) {
  7047. ast_copy_string(CLINE_OPTS->regexten, v->value, sizeof(CLINE_OPTS->regexten));
  7048. continue;
  7049. }
  7050. } else if (!strcasecmp(v->name, "language")) {
  7051. if (type & (TYPE_DEF_LINE | TYPE_LINE)) {
  7052. ast_copy_string(CLINE_OPTS->language, v->value, sizeof(CLINE_OPTS->language));
  7053. continue;
  7054. }
  7055. } else if (!strcasecmp(v->name, "accountcode")) {
  7056. if (type & (TYPE_DEF_LINE | TYPE_LINE)) {
  7057. ast_copy_string(CLINE_OPTS->accountcode, v->value, sizeof(CLINE_OPTS->accountcode));
  7058. continue;
  7059. }
  7060. } else if (!strcasecmp(v->name, "mohinterpret") || !strcasecmp(v->name, "musiconhold")) {
  7061. if (type & (TYPE_DEF_LINE | TYPE_LINE)) {
  7062. ast_copy_string(CLINE_OPTS->mohinterpret, v->value, sizeof(CLINE_OPTS->mohinterpret));
  7063. continue;
  7064. }
  7065. } else if (!strcasecmp(v->name, "mohsuggest")) {
  7066. if (type & (TYPE_DEF_LINE | TYPE_LINE)) {
  7067. ast_copy_string(CLINE_OPTS->mohsuggest, v->value, sizeof(CLINE_OPTS->mohsuggest));
  7068. continue;
  7069. }
  7070. } else if (!strcasecmp(v->name, "callgroup")) {
  7071. if (type & (TYPE_DEF_LINE | TYPE_LINE)) {
  7072. CLINE_OPTS->callgroup = ast_get_group(v->value);
  7073. continue;
  7074. }
  7075. } else if (!strcasecmp(v->name, "pickupgroup")) {
  7076. if (type & (TYPE_DEF_LINE | TYPE_LINE)) {
  7077. CLINE_OPTS->pickupgroup = ast_get_group(v->value);
  7078. continue;
  7079. }
  7080. } else if (!strcasecmp(v->name, "namedcallgroup")) {
  7081. if (type & (TYPE_DEF_LINE | TYPE_LINE)) {
  7082. CLINE_OPTS->named_callgroups = ast_get_namedgroups(v->value);
  7083. continue;
  7084. }
  7085. } else if (!strcasecmp(v->name, "namedpickupgroup")) {
  7086. if (type & (TYPE_DEF_LINE | TYPE_LINE)) {
  7087. CLINE_OPTS->named_pickupgroups = ast_get_namedgroups(v->value);
  7088. continue;
  7089. }
  7090. } else if (!strcasecmp(v->name, "immediate")) {
  7091. if (type & (TYPE_DEF_DEVICE | TYPE_DEVICE | TYPE_DEF_LINE | TYPE_LINE)) {
  7092. CLINE_OPTS->immediate = ast_true(v->value);
  7093. continue;
  7094. }
  7095. } else if (!strcasecmp(v->name, "cancallforward")) {
  7096. if (type & (TYPE_DEF_LINE | TYPE_LINE)) {
  7097. CLINE_OPTS->cancallforward = ast_true(v->value);
  7098. continue;
  7099. }
  7100. } else if (!strcasecmp(v->name, "callfwdtimeout")) {
  7101. if (type & (TYPE_DEF_LINE | TYPE_LINE)) {
  7102. CLINE_OPTS->callfwdtimeout = atoi(v->value);
  7103. continue;
  7104. }
  7105. } else if (!strcasecmp(v->name, "mailbox")) {
  7106. if (type & (TYPE_DEF_LINE | TYPE_LINE)) {
  7107. ast_copy_string(CLINE_OPTS->mailbox, v->value, sizeof(CLINE_OPTS->mailbox));
  7108. continue;
  7109. }
  7110. } else if ( !strcasecmp(v->name, "parkinglot")) {
  7111. if (type & (TYPE_DEF_LINE | TYPE_LINE)) {
  7112. ast_copy_string(CLINE_OPTS->parkinglot, v->value, sizeof(CLINE_OPTS->parkinglot));
  7113. continue;
  7114. }
  7115. } else if (!strcasecmp(v->name, "hasvoicemail")) {
  7116. if (type & (TYPE_LINE)) {
  7117. if (ast_true(v->value) && ast_strlen_zero(CLINE->mailbox)) {
  7118. /*
  7119. * hasvoicemail is a users.conf legacy voicemail enable method.
  7120. * hasvoicemail is only going to work for app_voicemail mailboxes.
  7121. */
  7122. if (strchr(CLINE->name, '@')) {
  7123. ast_copy_string(CLINE->mailbox, CLINE->name, sizeof(CLINE->mailbox));
  7124. } else {
  7125. snprintf(CLINE->mailbox, sizeof(CLINE->mailbox), "%s@default",
  7126. CLINE->name);
  7127. }
  7128. }
  7129. continue;
  7130. }
  7131. } else if (!strcasecmp(v->name, "threewaycalling")) {
  7132. if (type & (TYPE_DEF_LINE | TYPE_LINE)) {
  7133. CLINE_OPTS->threewaycalling = ast_true(v->value);
  7134. continue;
  7135. }
  7136. } else if (!strcasecmp(v->name, "setvar")) {
  7137. if (type & (TYPE_LINE)) {
  7138. CLINE->chanvars = add_var(v->value, CLINE->chanvars);
  7139. continue;
  7140. }
  7141. } else if (!strcasecmp(v->name, "earlyrtp")) {
  7142. if (type & (TYPE_DEF_DEVICE | TYPE_DEVICE)) {
  7143. CDEV_OPTS->earlyrtp = ast_true(v->value);
  7144. continue;
  7145. }
  7146. } else if (!strcasecmp(v->name, "host")) {
  7147. if (type & (TYPE_DEVICE)) {
  7148. struct ast_sockaddr CDEV_addr_tmp;
  7149. CDEV_addr_tmp.ss.ss_family = AF_INET;
  7150. if (ast_get_ip(&CDEV_addr_tmp, v->value)) {
  7151. ast_log(LOG_WARNING, "Bad IP '%s' at line %d.\n", v->value, v->lineno);
  7152. }
  7153. ast_sockaddr_to_sin(&CDEV_addr_tmp,
  7154. &CDEV->addr);
  7155. continue;
  7156. }
  7157. } else if (!strcasecmp(v->name, "port")) {
  7158. if (type & (TYPE_DEF_DEVICE)) {
  7159. CDEV->addr.sin_port = htons(atoi(v->value));
  7160. continue;
  7161. }
  7162. } else if (!strcasecmp(v->name, "device")) {
  7163. if (type & (TYPE_DEVICE)) {
  7164. ast_copy_string(CDEV_OPTS->id, v->value, sizeof(CDEV_OPTS->id));
  7165. continue;
  7166. }
  7167. } else if (!strcasecmp(v->name, "permit") || !strcasecmp(v->name, "deny")) {
  7168. if (type & (TYPE_DEVICE)) {
  7169. CDEV->ha = ast_append_ha(v->name, v->value, CDEV->ha, NULL);
  7170. continue;
  7171. }
  7172. } else if (!strcasecmp(v->name, "allow")) {
  7173. if (type & (TYPE_DEF_DEVICE | TYPE_DEVICE)) {
  7174. ast_format_cap_update_by_allow_disallow(CDEV->confcap, v->value, 1);
  7175. continue;
  7176. }
  7177. if (type & (TYPE_DEF_LINE | TYPE_LINE)) {
  7178. ast_format_cap_update_by_allow_disallow(CLINE->confcap, v->value, 1);
  7179. continue;
  7180. }
  7181. } else if (!strcasecmp(v->name, "disallow")) {
  7182. if (type & (TYPE_DEF_DEVICE | TYPE_DEVICE)) {
  7183. ast_format_cap_update_by_allow_disallow(CDEV->confcap, v->value, 0);
  7184. continue;
  7185. }
  7186. if (type & (TYPE_DEF_LINE | TYPE_LINE)) {
  7187. ast_format_cap_update_by_allow_disallow(CLINE->confcap, v->value, 0);
  7188. continue;
  7189. }
  7190. } else if (!strcasecmp(v->name, "version")) {
  7191. if (type & (TYPE_DEF_DEVICE | TYPE_DEVICE)) {
  7192. ast_copy_string(CDEV_OPTS->version_id, v->value, sizeof(CDEV_OPTS->version_id));
  7193. continue;
  7194. }
  7195. } else if (!strcasecmp(v->name, "line")) {
  7196. if (type & (TYPE_DEVICE)) {
  7197. struct skinny_line *l;
  7198. AST_LIST_TRAVERSE(&lines, l, all) {
  7199. if (!strcasecmp(v->value, l->name) && !l->prune) {
  7200. /* FIXME: temp solution about line conflicts */
  7201. struct skinny_device *d;
  7202. struct skinny_line *l2;
  7203. int lineinuse = 0;
  7204. AST_LIST_TRAVERSE(&devices, d, list) {
  7205. AST_LIST_TRAVERSE(&d->lines, l2, list) {
  7206. if (l2 == l && strcasecmp(d->id, CDEV->id)) {
  7207. ast_log(LOG_WARNING, "Line %s already used by %s. Not connecting to %s.\n", l->name, d->name, CDEV->name);
  7208. lineinuse++;
  7209. }
  7210. }
  7211. }
  7212. if (!lineinuse) {
  7213. if (!AST_LIST_FIRST(&CDEV->lines)) {
  7214. CDEV->activeline = l;
  7215. }
  7216. lineInstance++;
  7217. AST_LIST_INSERT_HEAD(&CDEV->lines, l, list);
  7218. l->device = CDEV;
  7219. }
  7220. break;
  7221. }
  7222. }
  7223. continue;
  7224. }
  7225. } else if (!strcasecmp(v->name, "subline")) {
  7226. if (type & (TYPE_LINE)) {
  7227. struct skinny_subline *subline;
  7228. struct skinny_container *container;
  7229. char buf[256];
  7230. char *stringp = buf, *exten, *stname, *context;
  7231. if (!(subline = ast_calloc(1, sizeof(*subline)))) {
  7232. ast_log(LOG_WARNING, "Unable to allocate memory for subline %s. Ignoring subline.\n", v->value);
  7233. continue;
  7234. }
  7235. if (!(container = ast_calloc(1, sizeof(*container)))) {
  7236. ast_log(LOG_WARNING, "Unable to allocate memory for subline %s container. Ignoring subline.\n", v->value);
  7237. ast_free(subline);
  7238. continue;
  7239. }
  7240. ast_copy_string(buf, v->value, sizeof(buf));
  7241. exten = strsep(&stringp, "@");
  7242. ast_copy_string(subline->exten, ast_strip(exten), sizeof(subline->exten));
  7243. stname = strsep(&exten, "_");
  7244. ast_copy_string(subline->stname, ast_strip(stname), sizeof(subline->stname));
  7245. ast_copy_string(subline->lnname, ast_strip(exten), sizeof(subline->lnname));
  7246. context = strsep(&stringp, ",");
  7247. ast_copy_string(subline->name, ast_strip(stringp), sizeof(subline->name));
  7248. ast_copy_string(subline->context, ast_strip(context), sizeof(subline->context));
  7249. subline->line = CLINE;
  7250. subline->sub = NULL;
  7251. container->type = SKINNY_SUBLINECONTAINER;
  7252. container->data = subline;
  7253. subline->container = container;
  7254. AST_LIST_INSERT_HEAD(&CLINE->sublines, subline, list);
  7255. continue;
  7256. }
  7257. } else if (!strcasecmp(v->name, "dialoutcontext")) {
  7258. if (type & (TYPE_LINE)) {
  7259. ast_copy_string(CLINE_OPTS->dialoutcontext, v->value, sizeof(CLINE_OPTS->dialoutcontext));
  7260. continue;
  7261. }
  7262. } else if (!strcasecmp(v->name, "dialoutexten")) {
  7263. if (type & (TYPE_LINE)) {
  7264. ast_copy_string(CLINE_OPTS->dialoutexten, v->value, sizeof(CLINE_OPTS->dialoutexten));
  7265. continue;
  7266. }
  7267. } else if (!strcasecmp(v->name, "speeddial")) {
  7268. if (type & (TYPE_DEVICE)) {
  7269. struct skinny_speeddial *sd;
  7270. struct skinny_container *container;
  7271. char buf[256];
  7272. char *stringp = buf, *exten, *context, *label;
  7273. if (!(sd = ast_calloc(1, sizeof(*sd)))) {
  7274. ast_log(LOG_WARNING, "Unable to allocate memory for speeddial %s. Ignoring speeddial.\n", v->name);
  7275. continue;
  7276. }
  7277. if (!(container = ast_calloc(1, sizeof(*container)))) {
  7278. ast_log(LOG_WARNING, "Unable to allocate memory for speeddial %s container. Ignoring speeddial.\n", v->name);
  7279. ast_free(sd);
  7280. continue;
  7281. }
  7282. ast_copy_string(buf, v->value, sizeof(buf));
  7283. exten = strsep(&stringp, ",");
  7284. if ((context = strchr(exten, '@'))) {
  7285. *context++ = '\0';
  7286. }
  7287. label = stringp;
  7288. ast_mutex_init(&sd->lock);
  7289. ast_copy_string(sd->exten, exten, sizeof(sd->exten));
  7290. if (!ast_strlen_zero(context)) {
  7291. sd->isHint = 1;
  7292. sd->instance = lineInstance++;
  7293. ast_copy_string(sd->context, context, sizeof(sd->context));
  7294. } else {
  7295. sd->isHint = 0;
  7296. sd->instance = speeddialInstance++;
  7297. sd->context[0] = '\0';
  7298. }
  7299. ast_copy_string(sd->label, S_OR(label, exten), sizeof(sd->label));
  7300. sd->parent = CDEV;
  7301. container->type = SKINNY_SDCONTAINER;
  7302. container->data = sd;
  7303. sd->container = container;
  7304. AST_LIST_INSERT_HEAD(&CDEV->speeddials, sd, list);
  7305. continue;
  7306. }
  7307. } else if (!strcasecmp(v->name, "serviceurl")) {
  7308. if (type & (TYPE_DEVICE)) {
  7309. struct skinny_serviceurl *surl;
  7310. char buf[256];
  7311. char *stringp = buf, *serviceUrl, *displayName;
  7312. if (!(surl = ast_calloc(1, sizeof(*surl)))) {
  7313. ast_log(LOG_WARNING, "Unable to allocate memory for serviceurl %s. Ignoring service URL.\n", v->name);
  7314. continue;
  7315. }
  7316. ast_copy_string(buf, v->value, sizeof(buf));
  7317. displayName = strsep(&stringp, ",");
  7318. if (stringp) {
  7319. serviceUrl = stringp;
  7320. ast_copy_string(surl->url, ast_strip(serviceUrl), sizeof(surl->url));
  7321. ast_copy_string(surl->displayName, displayName, sizeof(surl->displayName));
  7322. surl->instance = serviceUrlInstance++;
  7323. surl->device = CDEV;
  7324. AST_LIST_INSERT_HEAD(&CDEV->serviceurls, surl, list);
  7325. } else {
  7326. ast_free(surl);
  7327. ast_log(LOG_WARNING, "Badly formed option for service URL in %s. Ignoring service URL.\n", v->name);
  7328. }
  7329. continue;
  7330. }
  7331. } else if (!strcasecmp(v->name, "addon")) {
  7332. if (type & (TYPE_DEVICE)) {
  7333. struct skinny_addon *a;
  7334. if (!(a = ast_calloc(1, sizeof(*a)))) {
  7335. ast_log(LOG_WARNING, "Unable to allocate memory for addon %s. Ignoring addon.\n", v->name);
  7336. continue;
  7337. } else {
  7338. ast_mutex_init(&a->lock);
  7339. ast_copy_string(a->type, v->value, sizeof(a->type));
  7340. AST_LIST_INSERT_HEAD(&CDEV->addons, a, list);
  7341. }
  7342. continue;
  7343. }
  7344. } else {
  7345. ast_log(LOG_WARNING, "Don't know keyword '%s' at line %d\n", v->name, v->lineno);
  7346. continue;
  7347. }
  7348. ast_log(LOG_WARNING, "Invalid category used: %s at line %d\n", v->name, v->lineno);
  7349. }
  7350. }
  7351. static struct skinny_line *config_line(const char *lname, struct ast_variable *v)
  7352. {
  7353. struct skinny_line *l, *temp;
  7354. int update = 0;
  7355. struct skinny_container *container;
  7356. ast_log(LOG_NOTICE, "Configuring skinny line %s.\n", lname);
  7357. /* We find the old line and remove it just before the new
  7358. line is created */
  7359. AST_LIST_LOCK(&lines);
  7360. AST_LIST_TRAVERSE(&lines, temp, all) {
  7361. if (!strcasecmp(lname, temp->name) && temp->prune) {
  7362. update = 1;
  7363. break;
  7364. }
  7365. }
  7366. if (!(l = skinny_line_alloc())) {
  7367. ast_verb(1, "Unable to allocate memory for line %s.\n", lname);
  7368. AST_LIST_UNLOCK(&lines);
  7369. return NULL;
  7370. }
  7371. if (!(container = ast_calloc(1, sizeof(*container)))) {
  7372. ast_log(LOG_WARNING, "Unable to allocate memory for line %s container.\n", lname);
  7373. skinny_line_destroy(l);
  7374. AST_LIST_UNLOCK(&lines);
  7375. return NULL;
  7376. }
  7377. container->type = SKINNY_LINECONTAINER;
  7378. container->data = l;
  7379. l->container = container;
  7380. memcpy(l, default_line, sizeof(*default_line));
  7381. ast_mutex_init(&l->lock);
  7382. ast_copy_string(l->name, lname, sizeof(l->name));
  7383. ast_format_cap_append_from_cap(l->confcap, default_cap, AST_MEDIA_TYPE_UNKNOWN);
  7384. AST_LIST_INSERT_TAIL(&lines, l, all);
  7385. ast_mutex_lock(&l->lock);
  7386. AST_LIST_UNLOCK(&lines);
  7387. config_parse_variables(TYPE_LINE, l, v);
  7388. if (!ast_strlen_zero(l->mailbox)) {
  7389. struct stasis_topic *mailbox_specific_topic;
  7390. ast_verb(3, "Setting mailbox '%s' on line %s\n", l->mailbox, l->name);
  7391. mailbox_specific_topic = ast_mwi_topic(l->mailbox);
  7392. if (mailbox_specific_topic) {
  7393. l->mwi_event_sub = stasis_subscribe_pool(mailbox_specific_topic, mwi_event_cb, l);
  7394. stasis_subscription_accept_message_type(l->mwi_event_sub, ast_mwi_state_type());
  7395. stasis_subscription_set_filter(l->mwi_event_sub, STASIS_SUBSCRIPTION_FILTER_SELECTIVE);
  7396. }
  7397. }
  7398. if (!ast_strlen_zero(vmexten) && ast_strlen_zero(l->vmexten)) {
  7399. ast_copy_string(l->vmexten, vmexten, sizeof(l->vmexten));
  7400. }
  7401. ast_mutex_unlock(&l->lock);
  7402. /* We do not want to unlink or free the line yet, it needs
  7403. to be available to detect a device reconfig when we load the
  7404. devices. Old lines will be pruned after the reload completes */
  7405. ast_verb(3, "%s config for line '%s'\n", update ? "Updated" : (skinnyreload ? "Reloaded" : "Created"), l->name);
  7406. return l;
  7407. }
  7408. static struct skinny_device *config_device(const char *dname, struct ast_variable *v)
  7409. {
  7410. struct skinny_device *d, *temp;
  7411. struct skinny_line *l, *ltemp;
  7412. struct skinny_subchannel *sub;
  7413. int update = 0;
  7414. ast_log(LOG_NOTICE, "Configuring skinny device %s.\n", dname);
  7415. AST_LIST_LOCK(&devices);
  7416. AST_LIST_TRAVERSE(&devices, temp, list) {
  7417. if (!strcasecmp(dname, temp->name) && temp->prune) {
  7418. update = 1;
  7419. break;
  7420. }
  7421. }
  7422. if (!(d = skinny_device_alloc(dname))) {
  7423. ast_verb(1, "Unable to allocate memory for device %s.\n", dname);
  7424. AST_LIST_UNLOCK(&devices);
  7425. return NULL;
  7426. }
  7427. memcpy(d, default_device, sizeof(*default_device));
  7428. ast_mutex_init(&d->lock);
  7429. ast_copy_string(d->name, dname, sizeof(d->name));
  7430. ast_format_cap_append_from_cap(d->confcap, default_cap, AST_MEDIA_TYPE_UNKNOWN);
  7431. AST_LIST_INSERT_TAIL(&devices, d, list);
  7432. ast_mutex_lock(&d->lock);
  7433. AST_LIST_UNLOCK(&devices);
  7434. config_parse_variables(TYPE_DEVICE, d, v);
  7435. if (!AST_LIST_FIRST(&d->lines)) {
  7436. ast_log(LOG_ERROR, "A Skinny device must have at least one line!\n");
  7437. ast_mutex_unlock(&d->lock);
  7438. return NULL;
  7439. }
  7440. if (/*d->addr.sin_addr.s_addr && */!ntohs(d->addr.sin_port)) {
  7441. d->addr.sin_port = htons(DEFAULT_SKINNY_PORT);
  7442. }
  7443. if (skinnyreload){
  7444. AST_LIST_LOCK(&devices);
  7445. AST_LIST_TRAVERSE(&devices, temp, list) {
  7446. if (strcasecmp(d->id, temp->id) || !temp->prune || !temp->session) {
  7447. continue;
  7448. }
  7449. ast_mutex_lock(&d->lock);
  7450. d->session = temp->session;
  7451. d->session->device = d;
  7452. d->hookstate = temp->hookstate;
  7453. AST_LIST_LOCK(&d->lines);
  7454. AST_LIST_TRAVERSE(&d->lines, l, list){
  7455. AST_LIST_LOCK(&temp->lines);
  7456. AST_LIST_TRAVERSE(&temp->lines, ltemp, list) {
  7457. if (strcasecmp(l->name, ltemp->name)) {
  7458. continue;
  7459. }
  7460. ast_mutex_lock(&ltemp->lock);
  7461. l->instance = ltemp->instance;
  7462. if (l == temp->activeline) {
  7463. d->activeline = l;
  7464. }
  7465. if (!AST_LIST_EMPTY(&ltemp->sub)) {
  7466. ast_mutex_lock(&l->lock);
  7467. l->sub = ltemp->sub;
  7468. l->activesub = ltemp->activesub;
  7469. AST_LIST_TRAVERSE(&l->sub, sub, list) {
  7470. sub->line = l;
  7471. }
  7472. ast_mutex_unlock(&l->lock);
  7473. }
  7474. ast_mutex_unlock(&ltemp->lock);
  7475. }
  7476. AST_LIST_UNLOCK(&temp->lines);
  7477. }
  7478. AST_LIST_UNLOCK(&d->lines);
  7479. ast_mutex_unlock(&d->lock);
  7480. }
  7481. AST_LIST_UNLOCK(&devices);
  7482. }
  7483. ast_mutex_unlock(&d->lock);
  7484. ast_verb(3, "%s config for device '%s'\n", update ? "Updated" : (skinnyreload ? "Reloaded" : "Created"), d->name);
  7485. return d;
  7486. }
  7487. static int config_load(void)
  7488. {
  7489. int on = 1;
  7490. struct ast_config *cfg;
  7491. char *cat;
  7492. int oldport = ntohs(bindaddr.sin_port);
  7493. struct ast_flags config_flags = { 0 };
  7494. ast_log(LOG_NOTICE, "Configuring skinny from %s\n", config);
  7495. if (gethostname(ourhost, sizeof(ourhost))) {
  7496. ast_log(LOG_WARNING, "Unable to get hostname, Skinny disabled.\n");
  7497. return 0;
  7498. }
  7499. cfg = ast_config_load(config, config_flags);
  7500. /* We *must* have a config file otherwise stop immediately */
  7501. if (!cfg || cfg == CONFIG_STATUS_FILEINVALID) {
  7502. ast_log(LOG_NOTICE, "Unable to load config %s, Skinny disabled.\n", config);
  7503. return -1;
  7504. }
  7505. memset(&bindaddr, 0, sizeof(bindaddr));
  7506. immed_dialchar = '\0';
  7507. memset(&vmexten, '\0', sizeof(vmexten));
  7508. /* Copy the default jb config over global_jbconf */
  7509. memcpy(&global_jbconf, &default_jbconf, sizeof(struct ast_jb_conf));
  7510. /* load the general section */
  7511. cat = ast_category_browse(cfg, "general");
  7512. config_parse_variables(TYPE_GENERAL, NULL, ast_variable_browse(cfg, "general"));
  7513. if (ntohl(bindaddr.sin_addr.s_addr)) {
  7514. __ourip = bindaddr.sin_addr;
  7515. } else {
  7516. hp = ast_gethostbyname(ourhost, &ahp);
  7517. if (!hp) {
  7518. ast_log(LOG_WARNING, "Unable to get our IP address, Skinny disabled\n");
  7519. ast_config_destroy(cfg);
  7520. return 0;
  7521. }
  7522. memcpy(&__ourip, hp->h_addr, sizeof(__ourip));
  7523. }
  7524. if (!ntohs(bindaddr.sin_port)) {
  7525. bindaddr.sin_port = htons(DEFAULT_SKINNY_PORT);
  7526. }
  7527. bindaddr.sin_family = AF_INET;
  7528. /* load the lines sections */
  7529. config_parse_variables(TYPE_DEF_LINE, default_line, ast_variable_browse(cfg, "lines"));
  7530. cat = ast_category_browse(cfg, "lines");
  7531. while (cat && strcasecmp(cat, "general") && strcasecmp(cat, "devices")) {
  7532. config_line(cat, ast_variable_browse(cfg, cat));
  7533. cat = ast_category_browse(cfg, cat);
  7534. }
  7535. /* load the devices sections */
  7536. config_parse_variables(TYPE_DEF_DEVICE, default_device, ast_variable_browse(cfg, "devices"));
  7537. cat = ast_category_browse(cfg, "devices");
  7538. while (cat && strcasecmp(cat, "general") && strcasecmp(cat, "lines")) {
  7539. config_device(cat, ast_variable_browse(cfg, cat));
  7540. cat = ast_category_browse(cfg, cat);
  7541. }
  7542. ast_mutex_lock(&netlock);
  7543. if ((skinnysock > -1) && (ntohs(bindaddr.sin_port) != oldport)) {
  7544. close(skinnysock);
  7545. skinnysock = -1;
  7546. }
  7547. if (skinnysock < 0) {
  7548. skinnysock = socket(AF_INET, SOCK_STREAM, 0);
  7549. if(setsockopt(skinnysock, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)) == -1) {
  7550. ast_log(LOG_ERROR, "Set Socket Options failed: errno %d, %s\n", errno, strerror(errno));
  7551. ast_config_destroy(cfg);
  7552. ast_mutex_unlock(&netlock);
  7553. return 0;
  7554. }
  7555. if (skinnysock < 0) {
  7556. ast_log(LOG_WARNING, "Unable to create Skinny socket: %s\n", strerror(errno));
  7557. } else {
  7558. if (bind(skinnysock, (struct sockaddr *)&bindaddr, sizeof(bindaddr)) < 0) {
  7559. ast_log(LOG_WARNING, "Failed to bind to %s:%d: %s\n",
  7560. ast_inet_ntoa(bindaddr.sin_addr), ntohs(bindaddr.sin_port),
  7561. strerror(errno));
  7562. close(skinnysock);
  7563. skinnysock = -1;
  7564. ast_config_destroy(cfg);
  7565. ast_mutex_unlock(&netlock);
  7566. return 0;
  7567. }
  7568. if (listen(skinnysock, DEFAULT_SKINNY_BACKLOG)) {
  7569. ast_log(LOG_WARNING, "Failed to start listening to %s:%d: %s\n",
  7570. ast_inet_ntoa(bindaddr.sin_addr), ntohs(bindaddr.sin_port),
  7571. strerror(errno));
  7572. close(skinnysock);
  7573. skinnysock = -1;
  7574. ast_config_destroy(cfg);
  7575. ast_mutex_unlock(&netlock);
  7576. return 0;
  7577. }
  7578. ast_verb(2, "Skinny listening on %s:%d\n",
  7579. ast_inet_ntoa(bindaddr.sin_addr), ntohs(bindaddr.sin_port));
  7580. ast_set_qos(skinnysock, qos.tos, qos.cos, "Skinny");
  7581. ast_pthread_create_background(&accept_t, NULL, accept_thread, NULL);
  7582. }
  7583. }
  7584. ast_mutex_unlock(&netlock);
  7585. ast_config_destroy(cfg);
  7586. return 1;
  7587. }
  7588. static void delete_devices(void)
  7589. {
  7590. struct skinny_device *d;
  7591. struct skinny_line *l;
  7592. struct skinny_speeddial *sd;
  7593. struct skinny_addon *a;
  7594. struct skinny_serviceurl *surl;
  7595. AST_LIST_LOCK(&devices);
  7596. AST_LIST_LOCK(&lines);
  7597. /* Delete all devices */
  7598. while ((d = AST_LIST_REMOVE_HEAD(&devices, list))) {
  7599. /* Delete all lines for this device */
  7600. while ((l = AST_LIST_REMOVE_HEAD(&d->lines, list))) {
  7601. AST_LIST_REMOVE(&lines, l, all);
  7602. AST_LIST_REMOVE(&d->lines, l, list);
  7603. l = skinny_line_destroy(l);
  7604. }
  7605. /* Delete all speeddials for this device */
  7606. while ((sd = AST_LIST_REMOVE_HEAD(&d->speeddials, list))) {
  7607. free(sd->container);
  7608. free(sd);
  7609. }
  7610. /* Delete all serviceurls for this device */
  7611. while ((surl = AST_LIST_REMOVE_HEAD(&d->serviceurls, list))) {
  7612. free(surl);
  7613. }
  7614. /* Delete all addons for this device */
  7615. while ((a = AST_LIST_REMOVE_HEAD(&d->addons, list))) {
  7616. free(a);
  7617. }
  7618. d = skinny_device_destroy(d);
  7619. }
  7620. AST_LIST_UNLOCK(&lines);
  7621. AST_LIST_UNLOCK(&devices);
  7622. }
  7623. int skinny_reload(void)
  7624. {
  7625. struct skinny_device *d;
  7626. struct skinny_line *l;
  7627. struct skinny_speeddial *sd;
  7628. struct skinny_addon *a;
  7629. if (skinnyreload) {
  7630. ast_verb(3, "Chan_skinny is already reloading.\n");
  7631. return 0;
  7632. }
  7633. skinnyreload = 1;
  7634. /* Mark all devices and lines as candidates to be pruned */
  7635. AST_LIST_LOCK(&devices);
  7636. AST_LIST_TRAVERSE(&devices, d, list) {
  7637. d->prune = 1;
  7638. }
  7639. AST_LIST_UNLOCK(&devices);
  7640. AST_LIST_LOCK(&lines);
  7641. AST_LIST_TRAVERSE(&lines, l, all) {
  7642. l->prune = 1;
  7643. }
  7644. AST_LIST_UNLOCK(&lines);
  7645. config_load();
  7646. /* Remove any devices that no longer exist in the config */
  7647. AST_LIST_LOCK(&devices);
  7648. AST_LIST_TRAVERSE_SAFE_BEGIN(&devices, d, list) {
  7649. if (!d->prune) {
  7650. continue;
  7651. }
  7652. ast_verb(3, "Removing device '%s'\n", d->name);
  7653. /* Delete all lines for this device.
  7654. We do not want to free the line here, that
  7655. will happen below. */
  7656. while ((l = AST_LIST_REMOVE_HEAD(&d->lines, list))) {
  7657. if (l->mwi_event_sub) {
  7658. l->mwi_event_sub = stasis_unsubscribe(l->mwi_event_sub);
  7659. }
  7660. }
  7661. /* Delete all speeddials for this device */
  7662. while ((sd = AST_LIST_REMOVE_HEAD(&d->speeddials, list))) {
  7663. free(sd);
  7664. }
  7665. /* Delete all addons for this device */
  7666. while ((a = AST_LIST_REMOVE_HEAD(&d->addons, list))) {
  7667. free(a);
  7668. }
  7669. AST_LIST_REMOVE_CURRENT(list);
  7670. d = skinny_device_destroy(d);
  7671. }
  7672. AST_LIST_TRAVERSE_SAFE_END;
  7673. AST_LIST_UNLOCK(&devices);
  7674. AST_LIST_LOCK(&lines);
  7675. AST_LIST_TRAVERSE_SAFE_BEGIN(&lines, l, all) {
  7676. if (l->prune) {
  7677. AST_LIST_REMOVE_CURRENT(all);
  7678. l = skinny_line_destroy(l);
  7679. }
  7680. }
  7681. AST_LIST_TRAVERSE_SAFE_END;
  7682. AST_LIST_UNLOCK(&lines);
  7683. AST_LIST_TRAVERSE(&devices, d, list) {
  7684. /* Do a soft reset to re-register the devices after
  7685. cleaning up the removed devices and lines */
  7686. if (d->session) {
  7687. ast_verb(3, "Restarting device '%s'\n", d->name);
  7688. transmit_reset(d, 1);
  7689. }
  7690. }
  7691. skinnyreload = 0;
  7692. return 0;
  7693. }
  7694. /*!
  7695. * \brief Load the module
  7696. *
  7697. * Module loading including tests for configuration or dependencies.
  7698. * This function can return AST_MODULE_LOAD_FAILURE, AST_MODULE_LOAD_DECLINE,
  7699. * or AST_MODULE_LOAD_SUCCESS. If a dependency or environment variable fails
  7700. * tests return AST_MODULE_LOAD_FAILURE. If the module can not load the
  7701. * configuration file or other non-critical problem return
  7702. * AST_MODULE_LOAD_DECLINE. On success return AST_MODULE_LOAD_SUCCESS.
  7703. */
  7704. static int load_module(void)
  7705. {
  7706. int res = 0;
  7707. if (!(default_cap = ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_DEFAULT))) {
  7708. return AST_MODULE_LOAD_DECLINE;
  7709. }
  7710. if (!(skinny_tech.capabilities = ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_DEFAULT))) {
  7711. ao2_ref(default_cap, -1);
  7712. return AST_MODULE_LOAD_DECLINE;
  7713. }
  7714. ast_format_cap_append_by_type(skinny_tech.capabilities, AST_MEDIA_TYPE_AUDIO);
  7715. ast_format_cap_append(default_cap, ast_format_ulaw, 0);
  7716. ast_format_cap_append(default_cap, ast_format_alaw, 0);
  7717. for (; res < ARRAY_LEN(soft_key_template_default); res++) {
  7718. soft_key_template_default[res].softKeyEvent = htolel(soft_key_template_default[res].softKeyEvent);
  7719. }
  7720. /* load and parse config */
  7721. res = config_load();
  7722. if (res == -1) {
  7723. ao2_ref(skinny_tech.capabilities, -1);
  7724. ao2_ref(default_cap, -1);
  7725. return AST_MODULE_LOAD_DECLINE;
  7726. }
  7727. sched = ast_sched_context_create();
  7728. if (!sched) {
  7729. ao2_ref(skinny_tech.capabilities, -1);
  7730. ao2_ref(default_cap, -1);
  7731. ast_log(LOG_WARNING, "Unable to create schedule context\n");
  7732. return AST_MODULE_LOAD_DECLINE;
  7733. }
  7734. /* Make sure we can register our skinny channel type */
  7735. if (ast_channel_register(&skinny_tech)) {
  7736. ao2_ref(default_cap, -1);
  7737. ao2_ref(skinny_tech.capabilities, -1);
  7738. ast_log(LOG_ERROR, "Unable to register channel class 'Skinny'\n");
  7739. return AST_MODULE_LOAD_DECLINE;
  7740. }
  7741. ast_rtp_glue_register(&skinny_rtp_glue);
  7742. ast_cli_register_multiple(cli_skinny, ARRAY_LEN(cli_skinny));
  7743. ast_manager_register_xml("SKINNYdevices", EVENT_FLAG_SYSTEM | EVENT_FLAG_REPORTING, manager_skinny_show_devices);
  7744. ast_manager_register_xml("SKINNYshowdevice", EVENT_FLAG_SYSTEM | EVENT_FLAG_REPORTING, manager_skinny_show_device);
  7745. ast_manager_register_xml("SKINNYlines", EVENT_FLAG_SYSTEM | EVENT_FLAG_REPORTING, manager_skinny_show_lines);
  7746. ast_manager_register_xml("SKINNYshowline", EVENT_FLAG_SYSTEM | EVENT_FLAG_REPORTING, manager_skinny_show_line);
  7747. if (ast_sched_start_thread(sched)) {
  7748. ast_sched_context_destroy(sched);
  7749. sched = NULL;
  7750. ast_channel_unregister(&skinny_tech);
  7751. ao2_ref(default_cap, -1);
  7752. ao2_ref(skinny_tech.capabilities, -1);
  7753. return AST_MODULE_LOAD_DECLINE;
  7754. }
  7755. return AST_MODULE_LOAD_SUCCESS;
  7756. }
  7757. static int unload_module(void)
  7758. {
  7759. struct skinnysession *s;
  7760. struct skinny_device *d;
  7761. struct skinny_line *l;
  7762. struct skinny_subchannel *sub;
  7763. pthread_t tempthread;
  7764. ast_rtp_glue_unregister(&skinny_rtp_glue);
  7765. ast_channel_unregister(&skinny_tech);
  7766. ao2_cleanup(skinny_tech.capabilities);
  7767. ast_cli_unregister_multiple(cli_skinny, ARRAY_LEN(cli_skinny));
  7768. ast_manager_unregister("SKINNYdevices");
  7769. ast_manager_unregister("SKINNYshowdevice");
  7770. ast_manager_unregister("SKINNYlines");
  7771. ast_manager_unregister("SKINNYshowline");
  7772. ast_mutex_lock(&netlock);
  7773. if (accept_t && (accept_t != AST_PTHREADT_STOP)) {
  7774. pthread_cancel(accept_t);
  7775. pthread_kill(accept_t, SIGURG);
  7776. pthread_join(accept_t, NULL);
  7777. }
  7778. accept_t = AST_PTHREADT_STOP;
  7779. ast_mutex_unlock(&netlock);
  7780. AST_LIST_LOCK(&sessions);
  7781. /* Destroy all the interfaces and free their memory */
  7782. while((s = AST_LIST_REMOVE_HEAD(&sessions, list))) {
  7783. RAII_VAR(struct ast_json *, blob, NULL, ast_json_unref);
  7784. AST_LIST_UNLOCK(&sessions);
  7785. d = s->device;
  7786. AST_LIST_TRAVERSE(&d->lines, l, list){
  7787. ast_mutex_lock(&l->lock);
  7788. AST_LIST_TRAVERSE(&l->sub, sub, list) {
  7789. skinny_locksub(sub);
  7790. if (sub->owner) {
  7791. ast_softhangup(sub->owner, AST_SOFTHANGUP_APPUNLOAD);
  7792. }
  7793. skinny_unlocksub(sub);
  7794. }
  7795. if (l->mwi_event_sub) {
  7796. l->mwi_event_sub = stasis_unsubscribe_and_join(l->mwi_event_sub);
  7797. }
  7798. ast_mutex_unlock(&l->lock);
  7799. unregister_exten(l);
  7800. }
  7801. ast_endpoint_set_state(d->endpoint, AST_ENDPOINT_OFFLINE);
  7802. blob = ast_json_pack("{s: s}", "peer_status", "Unregistered");
  7803. ast_endpoint_blob_publish(d->endpoint, ast_endpoint_state_type(), blob);
  7804. tempthread = s->t;
  7805. pthread_cancel(tempthread);
  7806. pthread_join(tempthread, NULL);
  7807. AST_LIST_LOCK(&sessions);
  7808. }
  7809. AST_LIST_UNLOCK(&sessions);
  7810. delete_devices();
  7811. close(skinnysock);
  7812. if (sched) {
  7813. ast_sched_context_destroy(sched);
  7814. }
  7815. ast_context_destroy_by_name(used_context, "Skinny");
  7816. ao2_ref(default_cap, -1);
  7817. return 0;
  7818. }
  7819. static int reload(void)
  7820. {
  7821. skinny_reload();
  7822. return 0;
  7823. }
  7824. AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER, "Skinny Client Control Protocol (Skinny)",
  7825. .support_level = AST_MODULE_SUPPORT_EXTENDED,
  7826. .load = load_module,
  7827. .unload = unload_module,
  7828. .reload = reload,
  7829. .load_pri = AST_MODPRI_CHANNEL_DRIVER,
  7830. );