chan_skinny.c 246 KB

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