chan_skinny.c 237 KB

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