chan_skinny.c 274 KB

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