chan_skinny.c 275 KB

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