chan_skinny.c 237 KB

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