chan_skinny.c 275 KB

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