switch_channel.c 166 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582
  1. /*
  2. * FreeSWITCH Modular Media Switching Software Library / Soft-Switch Application
  3. * Copyright (C) 2005-2020, Anthony Minessale II <anthm@freeswitch.org>
  4. *
  5. * Version: MPL 1.1
  6. *
  7. * The contents of this file are subject to the Mozilla Public License Version
  8. * 1.1 (the "License"); you may not use this file except in compliance with
  9. * the License. You may obtain a copy of the License at
  10. * http://www.mozilla.org/MPL/
  11. *
  12. * Software distributed under the License is distributed on an "AS IS" basis,
  13. * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  14. * for the specific language governing rights and limitations under the
  15. * License.
  16. *
  17. * The Original Code is FreeSWITCH Modular Media Switching Software Library / Soft-Switch Application
  18. *
  19. * The Initial Developer of the Original Code is
  20. * Anthony Minessale II <anthm@freeswitch.org>
  21. * Portions created by the Initial Developer are Copyright (C)
  22. * the Initial Developer. All Rights Reserved.
  23. *
  24. * Contributor(s):
  25. *
  26. * Anthony Minessale II <anthm@freeswitch.org>
  27. * Michael Jerris <mike@jerris.com>
  28. *
  29. *
  30. * switch_channel.c -- Media Channel Interface
  31. *
  32. */
  33. #include <switch.h>
  34. #include <switch_channel.h>
  35. #include <pcre.h>
  36. struct switch_cause_table {
  37. const char *name;
  38. switch_call_cause_t cause;
  39. };
  40. typedef struct switch_device_state_binding_s {
  41. switch_device_state_function_t function;
  42. void *user_data;
  43. struct switch_device_state_binding_s *next;
  44. } switch_device_state_binding_t;
  45. static struct {
  46. switch_memory_pool_t *pool;
  47. switch_hash_t *device_hash;
  48. switch_mutex_t *device_mutex;
  49. switch_device_state_binding_t *device_bindings;
  50. } globals;
  51. static struct switch_cause_table CAUSE_CHART[] = {
  52. {"NONE", SWITCH_CAUSE_NONE},
  53. {"UNALLOCATED_NUMBER", SWITCH_CAUSE_UNALLOCATED_NUMBER},
  54. {"NO_ROUTE_TRANSIT_NET", SWITCH_CAUSE_NO_ROUTE_TRANSIT_NET},
  55. {"NO_ROUTE_DESTINATION", SWITCH_CAUSE_NO_ROUTE_DESTINATION},
  56. {"CHANNEL_UNACCEPTABLE", SWITCH_CAUSE_CHANNEL_UNACCEPTABLE},
  57. {"CALL_AWARDED_DELIVERED", SWITCH_CAUSE_CALL_AWARDED_DELIVERED},
  58. {"NORMAL_CLEARING", SWITCH_CAUSE_NORMAL_CLEARING},
  59. {"USER_BUSY", SWITCH_CAUSE_USER_BUSY},
  60. {"NO_USER_RESPONSE", SWITCH_CAUSE_NO_USER_RESPONSE},
  61. {"NO_ANSWER", SWITCH_CAUSE_NO_ANSWER},
  62. {"SUBSCRIBER_ABSENT", SWITCH_CAUSE_SUBSCRIBER_ABSENT},
  63. {"CALL_REJECTED", SWITCH_CAUSE_CALL_REJECTED},
  64. {"NUMBER_CHANGED", SWITCH_CAUSE_NUMBER_CHANGED},
  65. {"REDIRECTION_TO_NEW_DESTINATION", SWITCH_CAUSE_REDIRECTION_TO_NEW_DESTINATION},
  66. {"EXCHANGE_ROUTING_ERROR", SWITCH_CAUSE_EXCHANGE_ROUTING_ERROR},
  67. {"DESTINATION_OUT_OF_ORDER", SWITCH_CAUSE_DESTINATION_OUT_OF_ORDER},
  68. {"INVALID_NUMBER_FORMAT", SWITCH_CAUSE_INVALID_NUMBER_FORMAT},
  69. {"FACILITY_REJECTED", SWITCH_CAUSE_FACILITY_REJECTED},
  70. {"RESPONSE_TO_STATUS_ENQUIRY", SWITCH_CAUSE_RESPONSE_TO_STATUS_ENQUIRY},
  71. {"NORMAL_UNSPECIFIED", SWITCH_CAUSE_NORMAL_UNSPECIFIED},
  72. {"NORMAL_CIRCUIT_CONGESTION", SWITCH_CAUSE_NORMAL_CIRCUIT_CONGESTION},
  73. {"NETWORK_OUT_OF_ORDER", SWITCH_CAUSE_NETWORK_OUT_OF_ORDER},
  74. {"NORMAL_TEMPORARY_FAILURE", SWITCH_CAUSE_NORMAL_TEMPORARY_FAILURE},
  75. {"SWITCH_CONGESTION", SWITCH_CAUSE_SWITCH_CONGESTION},
  76. {"ACCESS_INFO_DISCARDED", SWITCH_CAUSE_ACCESS_INFO_DISCARDED},
  77. {"REQUESTED_CHAN_UNAVAIL", SWITCH_CAUSE_REQUESTED_CHAN_UNAVAIL},
  78. {"PRE_EMPTED", SWITCH_CAUSE_PRE_EMPTED},
  79. {"FACILITY_NOT_SUBSCRIBED", SWITCH_CAUSE_FACILITY_NOT_SUBSCRIBED},
  80. {"OUTGOING_CALL_BARRED", SWITCH_CAUSE_OUTGOING_CALL_BARRED},
  81. {"INCOMING_CALL_BARRED", SWITCH_CAUSE_INCOMING_CALL_BARRED},
  82. {"BEARERCAPABILITY_NOTAUTH", SWITCH_CAUSE_BEARERCAPABILITY_NOTAUTH},
  83. {"BEARERCAPABILITY_NOTAVAIL", SWITCH_CAUSE_BEARERCAPABILITY_NOTAVAIL},
  84. {"SERVICE_UNAVAILABLE", SWITCH_CAUSE_SERVICE_UNAVAILABLE},
  85. {"BEARERCAPABILITY_NOTIMPL", SWITCH_CAUSE_BEARERCAPABILITY_NOTIMPL},
  86. {"CHAN_NOT_IMPLEMENTED", SWITCH_CAUSE_CHAN_NOT_IMPLEMENTED},
  87. {"FACILITY_NOT_IMPLEMENTED", SWITCH_CAUSE_FACILITY_NOT_IMPLEMENTED},
  88. {"SERVICE_NOT_IMPLEMENTED", SWITCH_CAUSE_SERVICE_NOT_IMPLEMENTED},
  89. {"INVALID_CALL_REFERENCE", SWITCH_CAUSE_INVALID_CALL_REFERENCE},
  90. {"INCOMPATIBLE_DESTINATION", SWITCH_CAUSE_INCOMPATIBLE_DESTINATION},
  91. {"INVALID_MSG_UNSPECIFIED", SWITCH_CAUSE_INVALID_MSG_UNSPECIFIED},
  92. {"MANDATORY_IE_MISSING", SWITCH_CAUSE_MANDATORY_IE_MISSING},
  93. {"MESSAGE_TYPE_NONEXIST", SWITCH_CAUSE_MESSAGE_TYPE_NONEXIST},
  94. {"WRONG_MESSAGE", SWITCH_CAUSE_WRONG_MESSAGE},
  95. {"IE_NONEXIST", SWITCH_CAUSE_IE_NONEXIST},
  96. {"INVALID_IE_CONTENTS", SWITCH_CAUSE_INVALID_IE_CONTENTS},
  97. {"WRONG_CALL_STATE", SWITCH_CAUSE_WRONG_CALL_STATE},
  98. {"RECOVERY_ON_TIMER_EXPIRE", SWITCH_CAUSE_RECOVERY_ON_TIMER_EXPIRE},
  99. {"MANDATORY_IE_LENGTH_ERROR", SWITCH_CAUSE_MANDATORY_IE_LENGTH_ERROR},
  100. {"PROTOCOL_ERROR", SWITCH_CAUSE_PROTOCOL_ERROR},
  101. {"INTERWORKING", SWITCH_CAUSE_INTERWORKING},
  102. {"SUCCESS", SWITCH_CAUSE_SUCCESS},
  103. {"ORIGINATOR_CANCEL", SWITCH_CAUSE_ORIGINATOR_CANCEL},
  104. {"CRASH", SWITCH_CAUSE_CRASH},
  105. {"SYSTEM_SHUTDOWN", SWITCH_CAUSE_SYSTEM_SHUTDOWN},
  106. {"LOSE_RACE", SWITCH_CAUSE_LOSE_RACE},
  107. {"MANAGER_REQUEST", SWITCH_CAUSE_MANAGER_REQUEST},
  108. {"BLIND_TRANSFER", SWITCH_CAUSE_BLIND_TRANSFER},
  109. {"ATTENDED_TRANSFER", SWITCH_CAUSE_ATTENDED_TRANSFER},
  110. {"ALLOTTED_TIMEOUT", SWITCH_CAUSE_ALLOTTED_TIMEOUT},
  111. {"USER_CHALLENGE", SWITCH_CAUSE_USER_CHALLENGE},
  112. {"MEDIA_TIMEOUT", SWITCH_CAUSE_MEDIA_TIMEOUT},
  113. {"PICKED_OFF", SWITCH_CAUSE_PICKED_OFF},
  114. {"USER_NOT_REGISTERED", SWITCH_CAUSE_USER_NOT_REGISTERED},
  115. {"PROGRESS_TIMEOUT", SWITCH_CAUSE_PROGRESS_TIMEOUT},
  116. {"INVALID_GATEWAY", SWITCH_CAUSE_INVALID_GATEWAY},
  117. {"GATEWAY_DOWN", SWITCH_CAUSE_GATEWAY_DOWN},
  118. {"INVALID_URL", SWITCH_CAUSE_INVALID_URL},
  119. {"INVALID_PROFILE", SWITCH_CAUSE_INVALID_PROFILE},
  120. {"NO_PICKUP", SWITCH_CAUSE_NO_PICKUP},
  121. {"SRTP_READ_ERROR", SWITCH_CAUSE_SRTP_READ_ERROR},
  122. {"BOWOUT", SWITCH_CAUSE_BOWOUT},
  123. {"BUSY_EVERYWHERE", SWITCH_CAUSE_BUSY_EVERYWHERE},
  124. {"DECLINE", SWITCH_CAUSE_DECLINE},
  125. {"DOES_NOT_EXIST_ANYWHERE", SWITCH_CAUSE_DOES_NOT_EXIST_ANYWHERE},
  126. {"NOT_ACCEPTABLE", SWITCH_CAUSE_NOT_ACCEPTABLE},
  127. {"UNWANTED", SWITCH_CAUSE_UNWANTED},
  128. {"NO_IDENTITY", SWITCH_CAUSE_NO_IDENTITY},
  129. {"BAD_IDENTITY_INFO", SWITCH_CAUSE_BAD_IDENTITY_INFO},
  130. {"UNSUPPORTED_CERTIFICATE", SWITCH_CAUSE_UNSUPPORTED_CERTIFICATE},
  131. {"INVALID_IDENTITY", SWITCH_CAUSE_INVALID_IDENTITY},
  132. {"STALE_DATE", SWITCH_CAUSE_STALE_DATE},
  133. {"REJECT_ALL", SWITCH_CAUSE_REJECT_ALL},
  134. {NULL, 0}
  135. };
  136. typedef enum {
  137. OCF_HANGUP = (1 << 0)
  138. } opaque_channel_flag_t;
  139. typedef enum {
  140. LP_NEITHER,
  141. LP_ORIGINATOR,
  142. LP_ORIGINATEE
  143. } switch_originator_type_t;
  144. struct switch_channel {
  145. char *name;
  146. switch_call_direction_t direction;
  147. switch_call_direction_t logical_direction;
  148. switch_queue_t *dtmf_queue;
  149. switch_queue_t *dtmf_log_queue;
  150. switch_mutex_t*dtmf_mutex;
  151. switch_mutex_t *flag_mutex;
  152. switch_mutex_t *state_mutex;
  153. switch_mutex_t *thread_mutex;
  154. switch_mutex_t *profile_mutex;
  155. switch_core_session_t *session;
  156. switch_channel_state_t state;
  157. switch_channel_state_t running_state;
  158. switch_channel_callstate_t callstate;
  159. uint32_t flags[CF_FLAG_MAX];
  160. uint32_t caps[CC_FLAG_MAX];
  161. uint8_t state_flags[CF_FLAG_MAX];
  162. uint32_t private_flags;
  163. switch_caller_profile_t *caller_profile;
  164. const switch_state_handler_table_t *state_handlers[SWITCH_MAX_STATE_HANDLERS];
  165. int state_handler_index;
  166. switch_event_t *variables;
  167. switch_event_t *scope_variables;
  168. switch_hash_t *private_hash;
  169. switch_hash_t *app_flag_hash;
  170. switch_call_cause_t hangup_cause;
  171. int vi;
  172. int event_count;
  173. int profile_index;
  174. opaque_channel_flag_t opaque_flags;
  175. switch_originator_type_t last_profile_type;
  176. switch_caller_extension_t *queued_extension;
  177. switch_event_t *app_list;
  178. switch_event_t *api_list;
  179. switch_event_t *var_list;
  180. switch_hold_record_t *hold_record;
  181. switch_device_node_t *device_node;
  182. char *device_id;
  183. switch_event_t *log_tags;
  184. };
  185. static void process_device_hup(switch_channel_t *channel);
  186. static void switch_channel_check_device_state(switch_channel_t *channel, switch_channel_callstate_t callstate);
  187. SWITCH_DECLARE(switch_hold_record_t *) switch_channel_get_hold_record(switch_channel_t *channel)
  188. {
  189. return channel->hold_record;
  190. }
  191. SWITCH_DECLARE(const char *) switch_channel_cause2str(switch_call_cause_t cause)
  192. {
  193. uint8_t x;
  194. const char *str = "UNKNOWN";
  195. for (x = 0; x < (sizeof(CAUSE_CHART) / sizeof(struct switch_cause_table)) - 1; x++) {
  196. if (CAUSE_CHART[x].cause == cause) {
  197. str = CAUSE_CHART[x].name;
  198. break;
  199. }
  200. }
  201. return str;
  202. }
  203. SWITCH_DECLARE(switch_call_cause_t) switch_channel_str2cause(const char *str)
  204. {
  205. uint8_t x;
  206. switch_call_cause_t cause = SWITCH_CAUSE_NORMAL_CLEARING;
  207. if (!zstr(str)) {
  208. if (*str > 47 && *str < 58) {
  209. cause = atoi(str);
  210. } else {
  211. for (x = 0; x < (sizeof(CAUSE_CHART) / sizeof(struct switch_cause_table)) - 1 && CAUSE_CHART[x].name; x++) {
  212. if (!strcasecmp(CAUSE_CHART[x].name, str)) {
  213. cause = CAUSE_CHART[x].cause;
  214. break;
  215. }
  216. }
  217. }
  218. }
  219. return cause;
  220. }
  221. SWITCH_DECLARE(switch_call_cause_t) switch_channel_get_cause(switch_channel_t *channel)
  222. {
  223. return channel->hangup_cause;
  224. }
  225. SWITCH_DECLARE(switch_call_cause_t *) switch_channel_get_cause_ptr(switch_channel_t *channel)
  226. {
  227. return &channel->hangup_cause;
  228. }
  229. struct switch_callstate_table {
  230. const char *name;
  231. switch_channel_callstate_t callstate;
  232. };
  233. static struct switch_callstate_table CALLSTATE_CHART[] = {
  234. {"DOWN", CCS_DOWN},
  235. {"DIALING", CCS_DIALING},
  236. {"RINGING", CCS_RINGING},
  237. {"EARLY", CCS_EARLY},
  238. {"ACTIVE", CCS_ACTIVE},
  239. {"HELD", CCS_HELD},
  240. {"RING_WAIT", CCS_RING_WAIT},
  241. {"HANGUP", CCS_HANGUP},
  242. {"UNHELD", CCS_UNHELD},
  243. {NULL, 0}
  244. };
  245. struct switch_device_state_table {
  246. const char *name;
  247. switch_device_state_t device_state;
  248. };
  249. static struct switch_device_state_table DEVICE_STATE_CHART[] = {
  250. {"DOWN", SDS_DOWN},
  251. {"RINGING", SDS_RINGING},
  252. {"ACTIVE", SDS_ACTIVE},
  253. {"ACTIVE_MULTI", SDS_ACTIVE_MULTI},
  254. {"HELD", SDS_HELD},
  255. {"UNHELD", SDS_UNHELD},
  256. {"HANGUP", SDS_HANGUP},
  257. {NULL, 0}
  258. };
  259. SWITCH_DECLARE(void) switch_channel_perform_set_callstate(switch_channel_t *channel, switch_channel_callstate_t callstate,
  260. const char *file, const char *func, int line)
  261. {
  262. switch_event_t *event;
  263. switch_channel_callstate_t o_callstate = channel->callstate;
  264. if (o_callstate == callstate || o_callstate == CCS_HANGUP) return;
  265. channel->callstate = callstate;
  266. if (channel->device_node) {
  267. channel->device_node->callstate = callstate;
  268. }
  269. switch_log_printf(SWITCH_CHANNEL_ID_LOG, file, func, line, switch_channel_get_uuid(channel), SWITCH_LOG_DEBUG,
  270. "(%s) Callstate Change %s -> %s\n", channel->name,
  271. switch_channel_callstate2str(o_callstate), switch_channel_callstate2str(callstate));
  272. switch_channel_check_device_state(channel, channel->callstate);
  273. if (switch_event_create(&event, SWITCH_EVENT_CHANNEL_CALLSTATE) == SWITCH_STATUS_SUCCESS) {
  274. switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "Original-Channel-Call-State", switch_channel_callstate2str(o_callstate));
  275. switch_event_add_header(event, SWITCH_STACK_BOTTOM, "Channel-Call-State-Number", "%d", callstate);
  276. switch_channel_event_set_data(channel, event);
  277. switch_event_fire(&event);
  278. }
  279. }
  280. SWITCH_DECLARE(switch_channel_callstate_t) switch_channel_get_callstate(switch_channel_t *channel)
  281. {
  282. return channel->callstate;
  283. }
  284. SWITCH_DECLARE(const char *) switch_channel_callstate2str(switch_channel_callstate_t callstate)
  285. {
  286. uint8_t x;
  287. const char *str = "UNKNOWN";
  288. for (x = 0; x < (sizeof(CALLSTATE_CHART) / sizeof(struct switch_cause_table)) - 1; x++) {
  289. if (CALLSTATE_CHART[x].callstate == callstate) {
  290. str = CALLSTATE_CHART[x].name;
  291. break;
  292. }
  293. }
  294. return str;
  295. }
  296. SWITCH_DECLARE(const char *) switch_channel_device_state2str(switch_device_state_t device_state)
  297. {
  298. uint8_t x;
  299. const char *str = "UNKNOWN";
  300. for (x = 0; x < (sizeof(DEVICE_STATE_CHART) / sizeof(struct switch_cause_table)) - 1; x++) {
  301. if (DEVICE_STATE_CHART[x].device_state == device_state) {
  302. str = DEVICE_STATE_CHART[x].name;
  303. break;
  304. }
  305. }
  306. return str;
  307. }
  308. SWITCH_DECLARE(switch_channel_callstate_t) switch_channel_str2callstate(const char *str)
  309. {
  310. uint8_t x;
  311. switch_channel_callstate_t callstate = (switch_channel_callstate_t) SWITCH_CAUSE_NONE;
  312. if (*str > 47 && *str < 58) {
  313. callstate = atoi(str);
  314. } else {
  315. for (x = 0; x < (sizeof(CALLSTATE_CHART) / sizeof(struct switch_callstate_table)) - 1 && CALLSTATE_CHART[x].name; x++) {
  316. if (!strcasecmp(CALLSTATE_CHART[x].name, str)) {
  317. callstate = CALLSTATE_CHART[x].callstate;
  318. break;
  319. }
  320. }
  321. }
  322. return callstate;
  323. }
  324. SWITCH_DECLARE(void) switch_channel_perform_audio_sync(switch_channel_t *channel, const char *file, const char *func, int line)
  325. {
  326. if (switch_channel_media_up(channel)) {
  327. switch_core_session_message_t *msg = NULL;
  328. msg = switch_core_session_alloc(channel->session, sizeof(*msg));
  329. MESSAGE_STAMP_FFL(msg);
  330. msg->message_id = SWITCH_MESSAGE_INDICATE_AUDIO_SYNC;
  331. msg->from = channel->name;
  332. msg->_file = file;
  333. msg->_func = func;
  334. msg->_line = line;
  335. switch_core_session_queue_message(channel->session, msg);
  336. }
  337. }
  338. SWITCH_DECLARE(void) switch_channel_perform_video_sync(switch_channel_t *channel, const char *file, const char *func, int line)
  339. {
  340. if (switch_channel_media_up(channel)) {
  341. switch_core_session_message_t *msg = NULL;
  342. msg = switch_core_session_alloc(channel->session, sizeof(*msg));
  343. MESSAGE_STAMP_FFL(msg);
  344. msg->message_id = SWITCH_MESSAGE_INDICATE_VIDEO_SYNC;
  345. msg->from = channel->name;
  346. msg->_file = file;
  347. msg->_func = func;
  348. msg->_line = line;
  349. switch_core_session_request_video_refresh(channel->session);
  350. switch_core_session_queue_message(channel->session, msg);
  351. }
  352. }
  353. SWITCH_DECLARE(switch_call_cause_t) switch_channel_cause_q850(switch_call_cause_t cause)
  354. {
  355. if (cause <= SWITCH_CAUSE_INTERWORKING) {
  356. return cause;
  357. } else {
  358. return SWITCH_CAUSE_NORMAL_CLEARING;
  359. }
  360. }
  361. SWITCH_DECLARE(switch_call_cause_t) switch_channel_get_cause_q850(switch_channel_t *channel)
  362. {
  363. return switch_channel_cause_q850(channel->hangup_cause);
  364. }
  365. SWITCH_DECLARE(switch_channel_timetable_t *) switch_channel_get_timetable(switch_channel_t *channel)
  366. {
  367. switch_channel_timetable_t *times = NULL;
  368. if (channel->caller_profile) {
  369. switch_mutex_lock(channel->profile_mutex);
  370. times = channel->caller_profile->times;
  371. switch_mutex_unlock(channel->profile_mutex);
  372. }
  373. return times;
  374. }
  375. SWITCH_DECLARE(void) switch_channel_set_direction(switch_channel_t *channel, switch_call_direction_t direction)
  376. {
  377. if (!switch_core_session_in_thread(channel->session)) {
  378. channel->direction = channel->logical_direction = direction;
  379. switch_channel_set_variable(channel, "direction", switch_channel_direction(channel) == SWITCH_CALL_DIRECTION_OUTBOUND ? "outbound" : "inbound");
  380. }
  381. }
  382. SWITCH_DECLARE(switch_call_direction_t) switch_channel_direction(switch_channel_t *channel)
  383. {
  384. return channel->direction;
  385. }
  386. SWITCH_DECLARE(switch_call_direction_t) switch_channel_logical_direction(switch_channel_t *channel)
  387. {
  388. return channel->logical_direction;
  389. }
  390. SWITCH_DECLARE(switch_status_t) switch_channel_alloc(switch_channel_t **channel, switch_call_direction_t direction, switch_memory_pool_t *pool)
  391. {
  392. switch_assert(pool != NULL);
  393. if (((*channel) = switch_core_alloc(pool, sizeof(switch_channel_t))) == 0) {
  394. return SWITCH_STATUS_MEMERR;
  395. }
  396. switch_event_create_plain(&(*channel)->variables, SWITCH_EVENT_CHANNEL_DATA);
  397. switch_core_hash_init(&(*channel)->private_hash);
  398. switch_queue_create(&(*channel)->dtmf_queue, SWITCH_DTMF_LOG_LEN, pool);
  399. switch_queue_create(&(*channel)->dtmf_log_queue, SWITCH_DTMF_LOG_LEN, pool);
  400. switch_mutex_init(&(*channel)->dtmf_mutex, SWITCH_MUTEX_NESTED, pool);
  401. switch_mutex_init(&(*channel)->flag_mutex, SWITCH_MUTEX_NESTED, pool);
  402. switch_mutex_init(&(*channel)->state_mutex, SWITCH_MUTEX_NESTED, pool);
  403. switch_mutex_init(&(*channel)->thread_mutex, SWITCH_MUTEX_NESTED, pool);
  404. switch_mutex_init(&(*channel)->profile_mutex, SWITCH_MUTEX_NESTED, pool);
  405. (*channel)->hangup_cause = SWITCH_CAUSE_NONE;
  406. (*channel)->name = "";
  407. (*channel)->direction = (*channel)->logical_direction = direction;
  408. switch_channel_set_variable(*channel, "direction", switch_channel_direction(*channel) == SWITCH_CALL_DIRECTION_OUTBOUND ? "outbound" : "inbound");
  409. return SWITCH_STATUS_SUCCESS;
  410. }
  411. SWITCH_DECLARE(switch_status_t) switch_channel_dtmf_lock(switch_channel_t *channel)
  412. {
  413. return switch_mutex_lock(channel->dtmf_mutex);
  414. }
  415. SWITCH_DECLARE(switch_status_t) switch_channel_try_dtmf_lock(switch_channel_t *channel)
  416. {
  417. return switch_mutex_trylock(channel->dtmf_mutex);
  418. }
  419. SWITCH_DECLARE(switch_status_t) switch_channel_dtmf_unlock(switch_channel_t *channel)
  420. {
  421. return switch_mutex_unlock(channel->dtmf_mutex);
  422. }
  423. SWITCH_DECLARE(switch_size_t) switch_channel_has_dtmf(switch_channel_t *channel)
  424. {
  425. switch_size_t has;
  426. switch_mutex_lock(channel->dtmf_mutex);
  427. has = switch_queue_size(channel->dtmf_queue);
  428. switch_mutex_unlock(channel->dtmf_mutex);
  429. return has;
  430. }
  431. SWITCH_DECLARE(switch_status_t) switch_channel_queue_dtmf(switch_channel_t *channel, const switch_dtmf_t *dtmf)
  432. {
  433. switch_status_t status;
  434. void *pop;
  435. switch_dtmf_t new_dtmf = { 0 };
  436. switch_bool_t sensitive = switch_true(switch_channel_get_variable_dup(channel, SWITCH_SENSITIVE_DTMF_VARIABLE, SWITCH_FALSE, -1));
  437. switch_assert(dtmf);
  438. switch_mutex_lock(channel->dtmf_mutex);
  439. new_dtmf = *dtmf;
  440. if (sensitive) {
  441. switch_set_flag((&new_dtmf), DTMF_FLAG_SENSITIVE);
  442. }
  443. if ((status = switch_core_session_recv_dtmf(channel->session, dtmf) != SWITCH_STATUS_SUCCESS)) {
  444. goto done;
  445. }
  446. if (is_dtmf(new_dtmf.digit)) {
  447. switch_dtmf_t *dt;
  448. int x = 0;
  449. if (!sensitive) {
  450. switch_log_printf(SWITCH_CHANNEL_CHANNEL_LOG(channel), SWITCH_LOG_INFO, "RECV DTMF %c:%d\n", new_dtmf.digit, new_dtmf.duration);
  451. }
  452. if (new_dtmf.digit != 'w' && new_dtmf.digit != 'W') {
  453. if (new_dtmf.duration > switch_core_max_dtmf_duration(0)) {
  454. switch_log_printf(SWITCH_CHANNEL_CHANNEL_LOG(channel), SWITCH_LOG_DEBUG, "%s EXCESSIVE DTMF DIGIT LEN [%d]\n",
  455. switch_channel_get_name(channel), new_dtmf.duration);
  456. new_dtmf.duration = switch_core_max_dtmf_duration(0);
  457. } else if (new_dtmf.duration < switch_core_min_dtmf_duration(0)) {
  458. switch_log_printf(SWITCH_CHANNEL_CHANNEL_LOG(channel), SWITCH_LOG_DEBUG, "%s SHORT DTMF DIGIT LEN [%d]\n",
  459. switch_channel_get_name(channel), new_dtmf.duration);
  460. new_dtmf.duration = switch_core_min_dtmf_duration(0);
  461. }
  462. }
  463. if (!new_dtmf.duration) {
  464. new_dtmf.duration = switch_core_default_dtmf_duration(0);
  465. }
  466. switch_zmalloc(dt, sizeof(*dt));
  467. *dt = new_dtmf;
  468. while (switch_queue_trypush(channel->dtmf_queue, dt) != SWITCH_STATUS_SUCCESS) {
  469. if (switch_queue_trypop(channel->dtmf_queue, &pop) == SWITCH_STATUS_SUCCESS) {
  470. free(pop);
  471. }
  472. if (++x > 100) {
  473. status = SWITCH_STATUS_FALSE;
  474. free(dt);
  475. goto done;
  476. }
  477. }
  478. }
  479. status = SWITCH_STATUS_SUCCESS;
  480. done:
  481. switch_mutex_unlock(channel->dtmf_mutex);
  482. switch_core_media_break(channel->session, SWITCH_MEDIA_TYPE_AUDIO);
  483. return status;
  484. }
  485. SWITCH_DECLARE(switch_status_t) switch_channel_queue_dtmf_string(switch_channel_t *channel, const char *dtmf_string)
  486. {
  487. char *p;
  488. switch_dtmf_t dtmf = { 0, switch_core_default_dtmf_duration(0), 0, SWITCH_DTMF_APP };
  489. int sent = 0, dur, bad_input = 0;
  490. char *string;
  491. int i, argc;
  492. char *argv[256];
  493. if (zstr(dtmf_string)) {
  494. return SWITCH_STATUS_GENERR;
  495. }
  496. dtmf.flags = DTMF_FLAG_SKIP_PROCESS;
  497. if (*dtmf_string == '~') {
  498. dtmf_string++;
  499. dtmf.flags = 0;
  500. }
  501. string = switch_core_session_strdup(channel->session, dtmf_string);
  502. argc = switch_separate_string(string, '+', argv, (sizeof(argv) / sizeof(argv[0])));
  503. for (i = 0; i < argc; i++) {
  504. dtmf.duration = switch_core_default_dtmf_duration(0);
  505. dur = switch_core_default_dtmf_duration(0) / 8;
  506. if ((p = strchr(argv[i], '@'))) {
  507. *p++ = '\0';
  508. if ((dur = atoi(p)) > (int)switch_core_min_dtmf_duration(0) / 8) {
  509. dtmf.duration = dur * 8;
  510. }
  511. }
  512. for (p = argv[i]; p && *p; p++) {
  513. if (is_dtmf(*p)) {
  514. dtmf.digit = *p;
  515. if (dtmf.duration > switch_core_max_dtmf_duration(0)) {
  516. switch_log_printf(SWITCH_CHANNEL_CHANNEL_LOG(channel), SWITCH_LOG_WARNING, "EXCESSIVE DTMF DIGIT LEN %c %d\n", dtmf.digit, dtmf.duration);
  517. dtmf.duration = switch_core_max_dtmf_duration(0);
  518. } else if (dtmf.duration < switch_core_min_dtmf_duration(0)) {
  519. switch_log_printf(SWITCH_CHANNEL_CHANNEL_LOG(channel), SWITCH_LOG_WARNING, "SHORT DTMF DIGIT LEN %c %d\n", dtmf.digit, dtmf.duration);
  520. dtmf.duration = switch_core_min_dtmf_duration(0);
  521. } else if (!dtmf.duration) {
  522. dtmf.duration = switch_core_default_dtmf_duration(0);
  523. }
  524. if (switch_channel_queue_dtmf(channel, &dtmf) == SWITCH_STATUS_SUCCESS) {
  525. switch_log_printf(SWITCH_CHANNEL_CHANNEL_LOG(channel), SWITCH_LOG_DEBUG, "%s Queue dtmf\ndigit=%c ms=%u samples=%u\n",
  526. switch_channel_get_name(channel), dtmf.digit, dur, dtmf.duration);
  527. sent++;
  528. }
  529. } else {
  530. bad_input++;
  531. }
  532. }
  533. }
  534. if (sent) {
  535. return SWITCH_STATUS_SUCCESS;
  536. }
  537. return bad_input ? SWITCH_STATUS_GENERR : SWITCH_STATUS_FALSE;
  538. }
  539. SWITCH_DECLARE(switch_status_t) switch_channel_dequeue_dtmf(switch_channel_t *channel, switch_dtmf_t *dtmf)
  540. {
  541. switch_event_t *event;
  542. void *pop;
  543. switch_dtmf_t *dt;
  544. switch_status_t status = SWITCH_STATUS_FALSE;
  545. int sensitive = 0;
  546. switch_mutex_lock(channel->dtmf_mutex);
  547. if (switch_queue_trypop(channel->dtmf_queue, &pop) == SWITCH_STATUS_SUCCESS) {
  548. dt = (switch_dtmf_t *) pop;
  549. *dtmf = *dt;
  550. sensitive = switch_test_flag(dtmf, DTMF_FLAG_SENSITIVE);
  551. if (!sensitive && switch_queue_trypush(channel->dtmf_log_queue, dt) != SWITCH_STATUS_SUCCESS) {
  552. free(dt);
  553. }
  554. dt = NULL;
  555. if (dtmf->duration > switch_core_max_dtmf_duration(0)) {
  556. switch_log_printf(SWITCH_CHANNEL_CHANNEL_LOG(channel), SWITCH_LOG_WARNING, "%s EXCESSIVE DTMF DIGIT [%c] LEN [%d]\n",
  557. switch_channel_get_name(channel), sensitive ? 'S' : dtmf->digit, dtmf->duration);
  558. dtmf->duration = switch_core_max_dtmf_duration(0);
  559. } else if (dtmf->duration < switch_core_min_dtmf_duration(0)) {
  560. switch_log_printf(SWITCH_CHANNEL_CHANNEL_LOG(channel), SWITCH_LOG_WARNING, "%s SHORT DTMF DIGIT [%c] LEN [%d]\n",
  561. switch_channel_get_name(channel), sensitive ? 'S' : dtmf->digit, dtmf->duration);
  562. dtmf->duration = switch_core_min_dtmf_duration(0);
  563. } else if (!dtmf->duration) {
  564. dtmf->duration = switch_core_default_dtmf_duration(0);
  565. }
  566. status = SWITCH_STATUS_SUCCESS;
  567. }
  568. switch_mutex_unlock(channel->dtmf_mutex);
  569. if (!sensitive && status == SWITCH_STATUS_SUCCESS && switch_event_create(&event, SWITCH_EVENT_DTMF) == SWITCH_STATUS_SUCCESS) {
  570. const char *dtmf_source_str = NULL;
  571. switch_channel_event_set_data(channel, event);
  572. switch_event_add_header(event, SWITCH_STACK_BOTTOM, "DTMF-Digit", "%c", dtmf->digit);
  573. switch_event_add_header(event, SWITCH_STACK_BOTTOM, "DTMF-Duration", "%u", dtmf->duration);
  574. switch(dtmf->source) {
  575. case SWITCH_DTMF_INBAND_AUDIO: /* From audio */
  576. dtmf_source_str = "INBAND_AUDIO";
  577. break;
  578. case SWITCH_DTMF_RTP: /* From RTP as a telephone event */
  579. dtmf_source_str = "RTP";
  580. break;
  581. case SWITCH_DTMF_ENDPOINT: /* From endpoint signaling */
  582. dtmf_source_str = "ENDPOINT";
  583. break;
  584. case SWITCH_DTMF_APP: /* Injected by application */
  585. dtmf_source_str = "APP";
  586. break;
  587. case SWITCH_DTMF_UNKNOWN: /* Unknown source */
  588. default:
  589. dtmf_source_str = "UNKNOWN";
  590. break;
  591. }
  592. switch_event_add_header(event, SWITCH_STACK_BOTTOM, "DTMF-Source", "%s", dtmf_source_str);
  593. if (switch_channel_test_flag(channel, CF_DIVERT_EVENTS)) {
  594. switch_core_session_queue_event(channel->session, &event);
  595. } else {
  596. switch_event_fire(&event);
  597. }
  598. }
  599. return status;
  600. }
  601. SWITCH_DECLARE(switch_size_t) switch_channel_dequeue_dtmf_string(switch_channel_t *channel, char *dtmf_str, switch_size_t len)
  602. {
  603. switch_size_t x = 0;
  604. switch_dtmf_t dtmf = { 0 };
  605. memset(dtmf_str, 0, len);
  606. while (x < len - 1 && switch_channel_dequeue_dtmf(channel, &dtmf) == SWITCH_STATUS_SUCCESS) {
  607. dtmf_str[x++] = dtmf.digit;
  608. }
  609. return x;
  610. }
  611. SWITCH_DECLARE(void) switch_channel_flush_dtmf(switch_channel_t *channel)
  612. {
  613. void *pop;
  614. switch_mutex_lock(channel->dtmf_mutex);
  615. while (switch_queue_trypop(channel->dtmf_queue, &pop) == SWITCH_STATUS_SUCCESS) {
  616. switch_dtmf_t *dt = (switch_dtmf_t *) pop;
  617. if (channel->state >= CS_HANGUP || switch_queue_trypush(channel->dtmf_log_queue, dt) != SWITCH_STATUS_SUCCESS) {
  618. free(dt);
  619. }
  620. }
  621. switch_mutex_unlock(channel->dtmf_mutex);
  622. }
  623. SWITCH_DECLARE(void) switch_channel_uninit(switch_channel_t *channel)
  624. {
  625. void *pop;
  626. switch_channel_flush_dtmf(channel);
  627. while (switch_queue_trypop(channel->dtmf_log_queue, &pop) == SWITCH_STATUS_SUCCESS) {
  628. switch_safe_free(pop);
  629. }
  630. if (channel->private_hash) {
  631. switch_core_hash_destroy(&channel->private_hash);
  632. }
  633. if (channel->app_flag_hash) {
  634. switch_core_hash_destroy(&channel->app_flag_hash);
  635. }
  636. switch_mutex_lock(channel->profile_mutex);
  637. switch_event_destroy(&channel->variables);
  638. switch_event_destroy(&channel->api_list);
  639. switch_event_destroy(&channel->var_list);
  640. switch_event_destroy(&channel->app_list);
  641. if (channel->log_tags) {
  642. switch_event_destroy(&channel->log_tags);
  643. }
  644. switch_mutex_unlock(channel->profile_mutex);
  645. }
  646. SWITCH_DECLARE(switch_status_t) switch_channel_init(switch_channel_t *channel, switch_core_session_t *session, switch_channel_state_t state,
  647. switch_channel_flag_t flag)
  648. {
  649. switch_assert(channel != NULL);
  650. channel->state = state;
  651. switch_channel_set_flag(channel, flag);
  652. channel->session = session;
  653. channel->running_state = CS_NONE;
  654. return SWITCH_STATUS_SUCCESS;
  655. }
  656. SWITCH_DECLARE(void) switch_channel_perform_presence(switch_channel_t *channel, const char *rpid, const char *status, const char *id,
  657. const char *file, const char *func, int line)
  658. {
  659. switch_event_t *event;
  660. switch_event_types_t type = SWITCH_EVENT_PRESENCE_IN;
  661. const char *call_info = NULL;
  662. char *call_info_state = "active";
  663. if (switch_channel_test_flag(channel, CF_NO_PRESENCE)) {
  664. return;
  665. }
  666. if (!status) {
  667. type = SWITCH_EVENT_PRESENCE_OUT;
  668. status = "idle";
  669. }
  670. if (!id) {
  671. id = switch_channel_get_variable(channel, "presence_id");
  672. }
  673. if (!id) {
  674. return;
  675. }
  676. call_info = switch_channel_get_variable(channel, "presence_call_info");
  677. if (switch_event_create(&event, type) == SWITCH_STATUS_SUCCESS) {
  678. switch_channel_event_set_data(channel, event);
  679. switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "proto", "any");
  680. switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "login", __FILE__);
  681. switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "from", id);
  682. if (type == SWITCH_EVENT_PRESENCE_IN) {
  683. if (!rpid) {
  684. rpid = "unknown";
  685. }
  686. switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "rpid", rpid);
  687. switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "status", status);
  688. }
  689. switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "event_type", "presence");
  690. switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "alt_event_type", "dialog");
  691. if (!strcasecmp(status, "idle") || !switch_channel_up_nosig(channel)) {
  692. call_info_state = "idle";
  693. } else if (!strcasecmp(status, "hold-private")) {
  694. call_info_state = "held-private";
  695. } else if (!strcasecmp(status, "hold")) {
  696. call_info_state = "held";
  697. } else if (!switch_channel_test_flag(channel, CF_ANSWERED)) {
  698. if (channel->direction == SWITCH_CALL_DIRECTION_OUTBOUND) {
  699. call_info_state = "progressing";
  700. } else {
  701. if (switch_channel_test_flag(channel, CF_SLA_INTERCEPT)) {
  702. call_info_state = "idle";
  703. } else {
  704. call_info_state = "alerting";
  705. }
  706. }
  707. }
  708. switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "presence-call-info-state", call_info_state);
  709. if (call_info) {
  710. switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "presence-call-info", call_info);
  711. }
  712. switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "presence-call-direction",
  713. channel->direction == SWITCH_CALL_DIRECTION_OUTBOUND ? "outbound" : "inbound");
  714. switch_event_add_header(event, SWITCH_STACK_BOTTOM, "event_count", "%d", channel->event_count++);
  715. switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "Presence-Calling-File", file);
  716. switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "Presence-Calling-Function", func);
  717. switch_event_add_header(event, SWITCH_STACK_BOTTOM, "Presence-Calling-Line", "%d", line);
  718. if (switch_true(switch_channel_get_variable(channel, "presence_privacy"))) {
  719. switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "Presence-Privacy", "true");
  720. }
  721. switch_event_fire(&event);
  722. }
  723. }
  724. SWITCH_DECLARE(void) switch_channel_mark_hold(switch_channel_t *channel, switch_bool_t on)
  725. {
  726. switch_event_t *event;
  727. if (!!on == !!switch_channel_test_flag(channel, CF_LEG_HOLDING)) {
  728. goto end;
  729. }
  730. if (on) {
  731. switch_channel_set_flag(channel, CF_LEG_HOLDING);
  732. } else {
  733. switch_channel_clear_flag(channel, CF_LEG_HOLDING);
  734. }
  735. if (switch_event_create(&event, on ? SWITCH_EVENT_CHANNEL_HOLD : SWITCH_EVENT_CHANNEL_UNHOLD) == SWITCH_STATUS_SUCCESS) {
  736. switch_channel_event_set_data(channel, event);
  737. switch_event_fire(&event);
  738. }
  739. end:
  740. if (on) {
  741. if (switch_true(switch_channel_get_variable(channel, "flip_record_on_hold"))) {
  742. switch_core_session_t *other_session;
  743. if (switch_core_session_get_partner(channel->session, &other_session) == SWITCH_STATUS_SUCCESS) {
  744. switch_ivr_transfer_recordings(channel->session, other_session);
  745. switch_core_session_rwunlock(other_session);
  746. }
  747. }
  748. }
  749. }
  750. SWITCH_DECLARE(const char *) switch_channel_get_hold_music(switch_channel_t *channel)
  751. {
  752. const char *var;
  753. if (!(var = switch_channel_get_variable(channel, SWITCH_TEMP_HOLD_MUSIC_VARIABLE))) {
  754. var = switch_channel_get_variable(channel, SWITCH_HOLD_MUSIC_VARIABLE);
  755. }
  756. if (!zstr(var)) {
  757. char *expanded = switch_channel_expand_variables(channel, var);
  758. if (expanded != var) {
  759. var = switch_core_session_strdup(channel->session, expanded);
  760. free(expanded);
  761. }
  762. }
  763. return var;
  764. }
  765. SWITCH_DECLARE(const char *) switch_channel_get_hold_music_partner(switch_channel_t *channel)
  766. {
  767. switch_core_session_t *session;
  768. const char *r = NULL;
  769. if (switch_core_session_get_partner(channel->session, &session) == SWITCH_STATUS_SUCCESS) {
  770. r = switch_channel_get_hold_music(switch_core_session_get_channel(session));
  771. switch_core_session_rwunlock(session);
  772. }
  773. return r;
  774. }
  775. SWITCH_DECLARE(void) switch_channel_set_scope_variables(switch_channel_t *channel, switch_event_t **event)
  776. {
  777. switch_mutex_lock(channel->profile_mutex);
  778. if (event && *event) { /* push */
  779. (*event)->next = channel->scope_variables;
  780. channel->scope_variables = *event;
  781. *event = NULL;
  782. } else if (channel->scope_variables) { /* pop */
  783. switch_event_t *top_event = channel->scope_variables;
  784. channel->scope_variables = channel->scope_variables->next;
  785. switch_event_destroy(&top_event);
  786. }
  787. switch_mutex_unlock(channel->profile_mutex);
  788. }
  789. SWITCH_DECLARE(switch_status_t) switch_channel_get_scope_variables(switch_channel_t *channel, switch_event_t **event)
  790. {
  791. switch_status_t status = SWITCH_STATUS_FALSE;
  792. switch_event_t *new_event;
  793. switch_mutex_lock(channel->profile_mutex);
  794. if (channel->scope_variables) {
  795. switch_event_t *ep;
  796. switch_event_header_t *hp;
  797. switch_event_create_plain(&new_event, SWITCH_EVENT_CHANNEL_DATA);
  798. status = SWITCH_STATUS_SUCCESS;
  799. *event = new_event;
  800. for (ep = channel->scope_variables; ep; ep = ep->next) {
  801. for (hp = ep->headers; hp; hp = hp->next) {
  802. if (!switch_event_get_header(new_event, hp->value)) {
  803. switch_event_add_header_string(new_event, SWITCH_STACK_BOTTOM, hp->name, hp->value);
  804. }
  805. }
  806. }
  807. }
  808. switch_mutex_unlock(channel->profile_mutex);
  809. return status;
  810. }
  811. SWITCH_DECLARE(const char *) switch_channel_get_variable_dup(switch_channel_t *channel, const char *varname, switch_bool_t dup, int idx)
  812. {
  813. const char *v = NULL, *r = NULL, *vdup = NULL;
  814. switch_assert(channel != NULL);
  815. switch_mutex_lock(channel->profile_mutex);
  816. if (!zstr(varname)) {
  817. if (channel->scope_variables) {
  818. switch_event_t *ep;
  819. for (ep = channel->scope_variables; ep; ep = ep->next) {
  820. if ((v = switch_event_get_header_idx(ep, varname, idx))) {
  821. break;
  822. }
  823. }
  824. }
  825. if (!v && (!channel->variables || !(v = switch_event_get_header_idx(channel->variables, varname, idx)))) {
  826. switch_caller_profile_t *cp = switch_channel_get_caller_profile(channel);
  827. if (cp) {
  828. if (!strncmp(varname, "aleg_", 5)) {
  829. cp = cp->originator_caller_profile;
  830. varname += 5;
  831. } else if (!strncmp(varname, "bleg_", 5)) {
  832. cp = cp->originatee_caller_profile;
  833. varname += 5;
  834. }
  835. }
  836. if (!cp || !(v = switch_caller_get_field_by_name(cp, varname))) {
  837. if ((vdup = switch_core_get_variable_pdup(varname, switch_core_session_get_pool(channel->session)))) {
  838. v = vdup;
  839. }
  840. }
  841. }
  842. }
  843. if (dup && v != vdup) {
  844. if (v) {
  845. r = switch_core_session_strdup(channel->session, v);
  846. }
  847. } else {
  848. r = v;
  849. }
  850. switch_mutex_unlock(channel->profile_mutex);
  851. return r;
  852. }
  853. SWITCH_DECLARE(const char *) switch_channel_get_variable_strdup(switch_channel_t *channel, const char *varname)
  854. {
  855. const char *value = switch_channel_get_variable_dup(channel, varname, SWITCH_FALSE, -1);
  856. return value ? (const char *)strdup(value) : NULL;
  857. }
  858. SWITCH_DECLARE(switch_status_t) switch_channel_get_variable_buf(switch_channel_t *channel, const char *varname, char *buf, switch_size_t buflen)
  859. {
  860. const char *value = switch_channel_get_variable_dup(channel, varname, SWITCH_FALSE, -1);
  861. if (value && buf && buflen && switch_copy_string(buf, value, buflen)) {
  862. return SWITCH_STATUS_SUCCESS;
  863. }
  864. return SWITCH_STATUS_FALSE;
  865. }
  866. SWITCH_DECLARE(const char *) switch_channel_get_variable_partner(switch_channel_t *channel, const char *varname)
  867. {
  868. const char *uuid;
  869. const char *val = NULL, *r = NULL;
  870. switch_assert(channel != NULL);
  871. if (!zstr(varname)) {
  872. if ((uuid = switch_channel_get_partner_uuid(channel))) {
  873. switch_core_session_t *session;
  874. if ((session = switch_core_session_locate(uuid))) {
  875. switch_channel_t *tchannel = switch_core_session_get_channel(session);
  876. val = switch_channel_get_variable(tchannel, varname);
  877. switch_core_session_rwunlock(session);
  878. }
  879. }
  880. }
  881. if (val)
  882. r = switch_core_session_strdup(channel->session, val);
  883. return r;
  884. }
  885. SWITCH_DECLARE(void) switch_channel_variable_last(switch_channel_t *channel)
  886. {
  887. switch_assert(channel != NULL);
  888. if (!channel->vi) {
  889. return;
  890. }
  891. channel->vi = 0;
  892. switch_mutex_unlock(channel->profile_mutex);
  893. }
  894. SWITCH_DECLARE(switch_event_header_t *) switch_channel_variable_first(switch_channel_t *channel)
  895. {
  896. switch_event_header_t *hi = NULL;
  897. switch_assert(channel != NULL);
  898. switch_mutex_lock(channel->profile_mutex);
  899. if (channel->variables && (hi = channel->variables->headers)) {
  900. channel->vi = 1;
  901. } else {
  902. switch_mutex_unlock(channel->profile_mutex);
  903. }
  904. return hi;
  905. }
  906. SWITCH_DECLARE(switch_status_t) switch_channel_set_private(switch_channel_t *channel, const char *key, const void *private_info)
  907. {
  908. switch_assert(channel != NULL);
  909. switch_core_hash_insert_locked(channel->private_hash, key, private_info, channel->profile_mutex);
  910. return SWITCH_STATUS_SUCCESS;
  911. }
  912. SWITCH_DECLARE(void *) switch_channel_get_private(switch_channel_t *channel, const char *key)
  913. {
  914. void *val;
  915. switch_assert(channel != NULL);
  916. val = switch_core_hash_find_locked(channel->private_hash, key, channel->profile_mutex);
  917. return val;
  918. }
  919. SWITCH_DECLARE(void *) switch_channel_get_private_partner(switch_channel_t *channel, const char *key)
  920. {
  921. const char *uuid;
  922. void *val = NULL;
  923. switch_assert(channel != NULL);
  924. if ((uuid = switch_channel_get_partner_uuid(channel))) {
  925. switch_core_session_t *session;
  926. if ((session = switch_core_session_locate(uuid))) {
  927. val = switch_core_hash_find_locked(channel->private_hash, key, channel->profile_mutex);
  928. switch_core_session_rwunlock(session);
  929. }
  930. }
  931. return val;
  932. }
  933. SWITCH_DECLARE(switch_status_t) switch_channel_set_name(switch_channel_t *channel, const char *name)
  934. {
  935. const char *old = NULL;
  936. switch_assert(channel != NULL);
  937. if (!zstr(channel->name)) {
  938. old = channel->name;
  939. }
  940. channel->name = NULL;
  941. if (name) {
  942. char *uuid = switch_core_session_get_uuid(channel->session);
  943. channel->name = switch_core_session_strdup(channel->session, name);
  944. switch_channel_set_variable(channel, SWITCH_CHANNEL_NAME_VARIABLE, name);
  945. if (old) {
  946. switch_log_printf(SWITCH_CHANNEL_CHANNEL_LOG(channel), SWITCH_LOG_NOTICE, "Rename Channel %s->%s [%s]\n", old, name, uuid);
  947. } else {
  948. switch_log_printf(SWITCH_CHANNEL_CHANNEL_LOG(channel), SWITCH_LOG_NOTICE, "New Channel %s [%s]\n", name, uuid);
  949. }
  950. }
  951. return SWITCH_STATUS_SUCCESS;
  952. }
  953. SWITCH_DECLARE(char *) switch_channel_get_name(switch_channel_t *channel)
  954. {
  955. switch_assert(channel != NULL);
  956. return (!zstr(channel->name)) ? channel->name : "N/A";
  957. }
  958. SWITCH_DECLARE(switch_status_t) switch_channel_set_profile_var(switch_channel_t *channel, const char *name, const char *val)
  959. {
  960. char *v;
  961. switch_status_t status = SWITCH_STATUS_SUCCESS;
  962. switch_mutex_lock(channel->profile_mutex);
  963. if (!strcasecmp(name, "device_id") && !zstr(val)) {
  964. const char *device_id;
  965. if (!(device_id = switch_channel_set_device_id(channel, val))) {
  966. /* one time setting */
  967. switch_mutex_unlock(channel->profile_mutex);
  968. return status;
  969. }
  970. val = device_id;
  971. }
  972. if (!zstr(val)) {
  973. v = switch_core_strdup(channel->caller_profile->pool, val);
  974. } else {
  975. v = SWITCH_BLANK_STRING;
  976. }
  977. if (!strcasecmp(name, "dialplan")) {
  978. channel->caller_profile->dialplan = v;
  979. } else if (!strcasecmp(name, "username")) {
  980. channel->caller_profile->username = v;
  981. } else if (!strcasecmp(name, "caller_id_name")) {
  982. channel->caller_profile->caller_id_name = v;
  983. } else if (!strcasecmp(name, "caller_id_number")) {
  984. channel->caller_profile->caller_id_number = v;
  985. } else if (!strcasecmp(name, "callee_id_name")) {
  986. channel->caller_profile->callee_id_name = v;
  987. } else if (!strcasecmp(name, "callee_id_number")) {
  988. channel->caller_profile->callee_id_number = v;
  989. } else if (val && !strcasecmp(name, "caller_ton")) {
  990. channel->caller_profile->caller_ton = (uint8_t) atoi(v);
  991. } else if (val && !strcasecmp(name, "caller_numplan")) {
  992. channel->caller_profile->caller_numplan = (uint8_t) atoi(v);
  993. } else if (val && !strcasecmp(name, "destination_number_ton")) {
  994. channel->caller_profile->destination_number_ton = (uint8_t) atoi(v);
  995. } else if (val && !strcasecmp(name, "destination_number_numplan")) {
  996. channel->caller_profile->destination_number_numplan = (uint8_t) atoi(v);
  997. } else if (!strcasecmp(name, "ani")) {
  998. channel->caller_profile->ani = v;
  999. } else if (!strcasecmp(name, "aniii")) {
  1000. channel->caller_profile->aniii = v;
  1001. } else if (!strcasecmp(name, "network_addr")) {
  1002. channel->caller_profile->network_addr = v;
  1003. } else if (!strcasecmp(name, "rdnis")) {
  1004. channel->caller_profile->rdnis = v;
  1005. } else if (!strcasecmp(name, "destination_number")) {
  1006. channel->caller_profile->destination_number = v;
  1007. } else if (!strcasecmp(name, "uuid")) {
  1008. channel->caller_profile->uuid = v;
  1009. } else if (!strcasecmp(name, "source")) {
  1010. channel->caller_profile->source = v;
  1011. } else if (!strcasecmp(name, "context")) {
  1012. channel->caller_profile->context = v;
  1013. } else if (!strcasecmp(name, "chan_name")) {
  1014. channel->caller_profile->chan_name = v;
  1015. } else {
  1016. profile_node_t *pn, *n = switch_core_alloc(channel->caller_profile->pool, sizeof(*n));
  1017. int var_found;
  1018. n->var = switch_core_strdup(channel->caller_profile->pool, name);
  1019. n->val = v;
  1020. if (!channel->caller_profile->soft) {
  1021. channel->caller_profile->soft = n;
  1022. } else {
  1023. var_found = 0;
  1024. for(pn = channel->caller_profile->soft; pn ; pn = pn->next) {
  1025. if (!strcasecmp(pn->var,n->var)) {
  1026. pn->val = n->val;
  1027. var_found = 1;
  1028. break;
  1029. }
  1030. if(!pn->next) {
  1031. break;
  1032. }
  1033. }
  1034. if (pn && !pn->next && !var_found) {
  1035. pn->next = n;
  1036. }
  1037. }
  1038. }
  1039. switch_mutex_unlock(channel->profile_mutex);
  1040. return status;
  1041. }
  1042. SWITCH_DECLARE(void) switch_channel_process_export(switch_channel_t *channel, switch_channel_t *peer_channel,
  1043. switch_event_t *var_event, const char *export_varname)
  1044. {
  1045. const char *export_vars = switch_channel_get_variable(channel, export_varname);
  1046. char *cptmp = switch_core_session_strdup(channel->session, export_vars);
  1047. int argc;
  1048. char *argv[256];
  1049. if (zstr(export_vars)) return;
  1050. if (var_event) {
  1051. switch_event_del_header(var_event, export_varname);
  1052. switch_event_add_header_string(var_event, SWITCH_STACK_BOTTOM, export_varname, export_vars);
  1053. }
  1054. if (peer_channel) {
  1055. switch_channel_set_variable(peer_channel, export_varname, export_vars);
  1056. }
  1057. if ((argc = switch_separate_string(cptmp, ',', argv, (sizeof(argv) / sizeof(argv[0]))))) {
  1058. int x;
  1059. for (x = 0; x < argc; x++) {
  1060. const char *vval;
  1061. if ((vval = switch_channel_get_variable(channel, argv[x]))) {
  1062. char *vvar = argv[x];
  1063. if (!strncasecmp(vvar, "nolocal:", 8)) { /* remove this later ? */
  1064. vvar += 8;
  1065. } else if (!strncasecmp(vvar, "_nolocal_", 9)) {
  1066. vvar += 9;
  1067. }
  1068. if (var_event) {
  1069. switch_event_del_header(var_event, vvar);
  1070. switch_event_add_header_string(var_event, SWITCH_STACK_BOTTOM, vvar, vval);
  1071. switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(channel->session), SWITCH_LOG_DEBUG,
  1072. "%s EXPORTING[%s] [%s]=[%s] to event\n",
  1073. switch_channel_get_name(channel),
  1074. export_varname,
  1075. vvar, vval);
  1076. }
  1077. if (peer_channel) {
  1078. switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(channel->session), SWITCH_LOG_DEBUG,
  1079. "%s EXPORTING[%s] [%s]=[%s] to %s\n",
  1080. switch_channel_get_name(channel),
  1081. export_varname,
  1082. vvar, vval, switch_channel_get_name(peer_channel));
  1083. switch_channel_set_variable(peer_channel, vvar, vval);
  1084. }
  1085. }
  1086. }
  1087. }
  1088. }
  1089. SWITCH_DECLARE(switch_status_t) switch_channel_export_variable_var_check(switch_channel_t *channel,
  1090. const char *varname, const char *val,
  1091. const char *export_varname, switch_bool_t var_check)
  1092. {
  1093. char *var_name = NULL;
  1094. const char *exports;
  1095. char *var, *new_exports, *new_exports_d = NULL;
  1096. int local = 1;
  1097. exports = switch_channel_get_variable(channel, export_varname);
  1098. var = switch_core_session_strdup(channel->session, varname);
  1099. if (var) {
  1100. if (!strncasecmp(var, "nolocal:", 8)) { /* remove this later ? */
  1101. var_name = var + 8;
  1102. local = 0;
  1103. } else if (!strncasecmp(var, "_nolocal_", 9)) {
  1104. var_name = var + 9;
  1105. local = 0;
  1106. } else {
  1107. var_name = var;
  1108. }
  1109. }
  1110. switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(channel->session), SWITCH_LOG_DEBUG, "EXPORT (%s) %s[%s]=[%s]\n",
  1111. export_varname, local ? "" : "(REMOTE ONLY) ",
  1112. var_name ? var_name : "", val ? val : "UNDEF");
  1113. switch_channel_set_variable_var_check(channel, var, val, var_check);
  1114. if (var && val) {
  1115. if (exports) {
  1116. new_exports_d = switch_mprintf("%s,%s", exports, var);
  1117. new_exports = new_exports_d;
  1118. } else {
  1119. new_exports = var;
  1120. }
  1121. switch_channel_set_variable(channel, export_varname, new_exports);
  1122. switch_safe_free(new_exports_d);
  1123. }
  1124. return SWITCH_STATUS_SUCCESS;
  1125. }
  1126. SWITCH_DECLARE(switch_status_t) switch_channel_export_variable_printf(switch_channel_t *channel, const char *varname,
  1127. const char *export_varname, const char *fmt, ...)
  1128. {
  1129. switch_status_t status = SWITCH_STATUS_FALSE;
  1130. char *data = NULL;
  1131. va_list ap;
  1132. int ret;
  1133. switch_assert(channel != NULL);
  1134. va_start(ap, fmt);
  1135. ret = switch_vasprintf(&data, fmt, ap);
  1136. va_end(ap);
  1137. if (ret == -1) {
  1138. return SWITCH_STATUS_FALSE;
  1139. }
  1140. status = switch_channel_export_variable(channel, varname, data, export_varname);
  1141. free(data);
  1142. return status;
  1143. }
  1144. SWITCH_DECLARE(uint32_t) switch_channel_del_variable_prefix(switch_channel_t *channel, const char *prefix)
  1145. {
  1146. switch_event_t *event;
  1147. switch_event_header_t *hp;
  1148. uint32_t r = 0;
  1149. switch_channel_get_variables(channel, &event);
  1150. if (event) {
  1151. for (hp = event->headers; hp; hp = hp->next) {
  1152. if (zstr(prefix) || !strncasecmp(hp->name, prefix, strlen(prefix))) {
  1153. switch_channel_set_variable(channel, hp->name, NULL);
  1154. }
  1155. }
  1156. }
  1157. switch_event_destroy(&event);
  1158. return r;
  1159. }
  1160. SWITCH_DECLARE(switch_status_t) switch_channel_transfer_variable_prefix(switch_channel_t *orig_channel, switch_channel_t *new_channel, const char *prefix)
  1161. {
  1162. switch_event_header_t *hi = NULL;
  1163. int x = 0;
  1164. if ((hi = switch_channel_variable_first(orig_channel))) {
  1165. for (; hi; hi = hi->next) {
  1166. char *var = hi->name;
  1167. char *val = hi->value;
  1168. if (zstr(prefix) || !strncasecmp(var, prefix, strlen(prefix))) {
  1169. x++;
  1170. switch_channel_set_variable(new_channel, var, val);
  1171. }
  1172. }
  1173. switch_channel_variable_last(orig_channel);
  1174. }
  1175. return x ? SWITCH_STATUS_SUCCESS : SWITCH_STATUS_FALSE;
  1176. }
  1177. SWITCH_DECLARE(void) switch_channel_set_presence_data_vals(switch_channel_t *channel, const char *presence_data_cols)
  1178. {
  1179. char *cols[128] = { 0 };
  1180. char header_name[128] = "";
  1181. int col_count = 0, i = 0;
  1182. char *data_copy = NULL;
  1183. if (zstr(presence_data_cols)) {
  1184. presence_data_cols = switch_channel_get_variable_dup(channel, "presence_data_cols", SWITCH_FALSE, -1);
  1185. if (zstr(presence_data_cols)) {
  1186. return;
  1187. }
  1188. }
  1189. data_copy = strdup(presence_data_cols);
  1190. col_count = switch_split(data_copy, ':', cols);
  1191. for (i = 0; i < col_count; i++) {
  1192. const char *val = NULL;
  1193. switch_snprintf(header_name, sizeof(header_name), "PD-%s", cols[i]);
  1194. val = switch_channel_get_variable(channel, cols[i]);
  1195. switch_channel_set_profile_var(channel, header_name, val);
  1196. }
  1197. switch_safe_free(data_copy);
  1198. }
  1199. SWITCH_DECLARE(switch_status_t) switch_channel_set_log_tag(switch_channel_t *channel, const char *tagname, const char *tagvalue)
  1200. {
  1201. switch_status_t status = SWITCH_STATUS_FALSE;
  1202. switch_assert(channel != NULL);
  1203. switch_mutex_lock(channel->profile_mutex);
  1204. if (!zstr(tagname)) {
  1205. if (!channel->log_tags) {
  1206. switch_event_create_plain(&channel->log_tags, SWITCH_EVENT_CHANNEL_DATA);
  1207. }
  1208. if (zstr(tagvalue)) {
  1209. switch_event_del_header(channel->log_tags, tagname);
  1210. } else {
  1211. switch_event_add_header_string(channel->log_tags, SWITCH_STACK_BOTTOM, tagname, tagvalue);
  1212. }
  1213. status = SWITCH_STATUS_SUCCESS;
  1214. }
  1215. switch_mutex_unlock(channel->profile_mutex);
  1216. return status;
  1217. }
  1218. SWITCH_DECLARE(switch_status_t) switch_channel_get_log_tags(switch_channel_t *channel, switch_event_t **log_tags)
  1219. {
  1220. switch_status_t status = SWITCH_STATUS_FALSE;
  1221. switch_assert(channel != NULL);
  1222. if (!channel->log_tags) {
  1223. return status;
  1224. }
  1225. switch_mutex_lock(channel->profile_mutex);
  1226. if (channel->log_tags && log_tags) {
  1227. status = switch_event_dup(log_tags, channel->log_tags);
  1228. }
  1229. switch_mutex_unlock(channel->profile_mutex);
  1230. return status;
  1231. }
  1232. SWITCH_DECLARE(switch_status_t) switch_channel_set_variable_var_check(switch_channel_t *channel,
  1233. const char *varname, const char *value, switch_bool_t var_check)
  1234. {
  1235. switch_status_t status = SWITCH_STATUS_FALSE;
  1236. switch_assert(channel != NULL);
  1237. switch_mutex_lock(channel->profile_mutex);
  1238. if (channel->variables && !zstr(varname)) {
  1239. if (zstr(value)) {
  1240. switch_event_del_header(channel->variables, varname);
  1241. } else {
  1242. int ok = 1;
  1243. if (var_check) {
  1244. ok = !switch_string_var_check_const(value);
  1245. }
  1246. if (ok) {
  1247. switch_event_add_header_string(channel->variables, SWITCH_STACK_BOTTOM, varname, value);
  1248. } else {
  1249. switch_log_printf(SWITCH_CHANNEL_CHANNEL_LOG(channel), SWITCH_LOG_CRIT, "Invalid data (${%s} contains a variable)\n", varname);
  1250. }
  1251. }
  1252. status = SWITCH_STATUS_SUCCESS;
  1253. }
  1254. switch_mutex_unlock(channel->profile_mutex);
  1255. return status;
  1256. }
  1257. SWITCH_DECLARE(switch_status_t) switch_channel_set_variable_strip_quotes_var_check(switch_channel_t *channel,
  1258. const char *varname, const char *value, switch_bool_t var_check)
  1259. {
  1260. switch_status_t status = SWITCH_STATUS_FALSE;
  1261. switch_assert(channel != NULL);
  1262. switch_mutex_lock(channel->profile_mutex);
  1263. if (channel->variables && !zstr(varname)) {
  1264. if (zstr(value)) {
  1265. switch_event_del_header(channel->variables, varname);
  1266. } else {
  1267. int ok = 1;
  1268. char *t = (char *)value;
  1269. char *r = (char *)value;
  1270. char *tmp = NULL;
  1271. if (t && *t == '"') {
  1272. t++;
  1273. if (end_of(t) == '"') {
  1274. r = tmp = strdup(t);
  1275. switch_assert(r);
  1276. end_of(r) = '\0';
  1277. }
  1278. }
  1279. if (var_check) {
  1280. ok = !switch_string_var_check_const(r);
  1281. }
  1282. if (ok) {
  1283. switch_event_add_header_string(channel->variables, SWITCH_STACK_BOTTOM, varname, r);
  1284. } else {
  1285. switch_log_printf(SWITCH_CHANNEL_CHANNEL_LOG(channel), SWITCH_LOG_CRIT, "Invalid data (${%s} contains a variable)\n", varname);
  1286. }
  1287. switch_safe_free(tmp);
  1288. }
  1289. status = SWITCH_STATUS_SUCCESS;
  1290. }
  1291. switch_mutex_unlock(channel->profile_mutex);
  1292. return status;
  1293. }
  1294. SWITCH_DECLARE(switch_status_t) switch_channel_add_variable_var_check(switch_channel_t *channel,
  1295. const char *varname, const char *value, switch_bool_t var_check, switch_stack_t stack)
  1296. {
  1297. switch_status_t status = SWITCH_STATUS_FALSE;
  1298. switch_assert(channel != NULL);
  1299. switch_mutex_lock(channel->profile_mutex);
  1300. if (channel->variables && !zstr(varname)) {
  1301. if (zstr(value)) {
  1302. switch_event_del_header(channel->variables, varname);
  1303. } else {
  1304. int ok = 1;
  1305. if (var_check) {
  1306. ok = !switch_string_var_check_const(value);
  1307. }
  1308. if (ok) {
  1309. switch_event_add_header_string(channel->variables, stack, varname, value);
  1310. } else {
  1311. switch_log_printf(SWITCH_CHANNEL_CHANNEL_LOG(channel), SWITCH_LOG_CRIT, "Invalid data (${%s} contains a variable)\n", varname);
  1312. }
  1313. }
  1314. status = SWITCH_STATUS_SUCCESS;
  1315. }
  1316. switch_mutex_unlock(channel->profile_mutex);
  1317. return status;
  1318. }
  1319. switch_status_t switch_event_base_add_header(switch_event_t *event, switch_stack_t stack, const char *header_name, char *data);
  1320. SWITCH_DECLARE(switch_status_t) switch_channel_set_variable_printf(switch_channel_t *channel, const char *varname, const char *fmt, ...)
  1321. {
  1322. int ret = 0;
  1323. char *data;
  1324. va_list ap;
  1325. switch_status_t status = SWITCH_STATUS_FALSE;
  1326. switch_assert(channel != NULL);
  1327. switch_mutex_lock(channel->profile_mutex);
  1328. if (channel->variables && !zstr(varname)) {
  1329. switch_event_del_header(channel->variables, varname);
  1330. va_start(ap, fmt);
  1331. ret = switch_vasprintf(&data, fmt, ap);
  1332. va_end(ap);
  1333. if (ret == -1) {
  1334. switch_mutex_unlock(channel->profile_mutex);
  1335. return SWITCH_STATUS_MEMERR;
  1336. }
  1337. status = switch_channel_set_variable(channel, varname, data);
  1338. free(data);
  1339. }
  1340. switch_mutex_unlock(channel->profile_mutex);
  1341. return status;
  1342. }
  1343. SWITCH_DECLARE(switch_status_t) switch_channel_set_variable_name_printf(switch_channel_t *channel, const char *val, const char *fmt, ...)
  1344. {
  1345. int ret = 0;
  1346. char *varname;
  1347. va_list ap;
  1348. switch_status_t status = SWITCH_STATUS_FALSE;
  1349. switch_assert(channel != NULL);
  1350. switch_mutex_lock(channel->profile_mutex);
  1351. va_start(ap, fmt);
  1352. ret = switch_vasprintf(&varname, fmt, ap);
  1353. va_end(ap);
  1354. if (ret == -1) {
  1355. switch_mutex_unlock(channel->profile_mutex);
  1356. return SWITCH_STATUS_MEMERR;
  1357. }
  1358. status = switch_channel_set_variable(channel, varname, val);
  1359. free(varname);
  1360. switch_mutex_unlock(channel->profile_mutex);
  1361. return status;
  1362. }
  1363. SWITCH_DECLARE(switch_status_t) switch_channel_set_variable_partner_var_check(switch_channel_t *channel,
  1364. const char *varname, const char *value, switch_bool_t var_check)
  1365. {
  1366. const char *uuid;
  1367. switch_assert(channel != NULL);
  1368. if (!zstr(varname)) {
  1369. if ((uuid = switch_channel_get_partner_uuid(channel))) {
  1370. switch_core_session_t *session;
  1371. if ((session = switch_core_session_locate(uuid))) {
  1372. switch_channel_t *tchannel = switch_core_session_get_channel(session);
  1373. switch_channel_set_variable_var_check(tchannel, varname, value, var_check);
  1374. switch_core_session_rwunlock(session);
  1375. }
  1376. return SWITCH_STATUS_SUCCESS;
  1377. }
  1378. }
  1379. return SWITCH_STATUS_FALSE;
  1380. }
  1381. SWITCH_DECLARE(uint32_t) switch_channel_test_flag(switch_channel_t *channel, switch_channel_flag_t flag)
  1382. {
  1383. uint32_t r = 0;
  1384. switch_assert(channel != NULL);
  1385. switch_mutex_lock(channel->flag_mutex);
  1386. r = channel->flags[flag];
  1387. switch_mutex_unlock(channel->flag_mutex);
  1388. return r;
  1389. }
  1390. SWITCH_DECLARE(switch_bool_t) switch_channel_set_flag_partner(switch_channel_t *channel, switch_channel_flag_t flag)
  1391. {
  1392. const char *uuid;
  1393. switch_assert(channel != NULL);
  1394. if ((uuid = switch_channel_get_partner_uuid(channel))) {
  1395. switch_core_session_t *session;
  1396. if ((session = switch_core_session_locate(uuid))) {
  1397. switch_channel_set_flag(switch_core_session_get_channel(session), flag);
  1398. switch_core_session_rwunlock(session);
  1399. return SWITCH_TRUE;
  1400. }
  1401. }
  1402. return SWITCH_FALSE;
  1403. }
  1404. SWITCH_DECLARE(uint32_t) switch_channel_test_flag_partner(switch_channel_t *channel, switch_channel_flag_t flag)
  1405. {
  1406. const char *uuid;
  1407. int r = 0;
  1408. switch_assert(channel != NULL);
  1409. if ((uuid = switch_channel_get_partner_uuid(channel))) {
  1410. switch_core_session_t *session;
  1411. if ((session = switch_core_session_locate(uuid))) {
  1412. r = switch_channel_test_flag(switch_core_session_get_channel(session), flag);
  1413. switch_core_session_rwunlock(session);
  1414. }
  1415. }
  1416. return r;
  1417. }
  1418. SWITCH_DECLARE(switch_bool_t) switch_channel_clear_flag_partner(switch_channel_t *channel, switch_channel_flag_t flag)
  1419. {
  1420. const char *uuid;
  1421. switch_assert(channel != NULL);
  1422. if ((uuid = switch_channel_get_partner_uuid(channel))) {
  1423. switch_core_session_t *session;
  1424. if ((session = switch_core_session_locate(uuid))) {
  1425. switch_channel_clear_flag(switch_core_session_get_channel(session), flag);
  1426. switch_core_session_rwunlock(session);
  1427. return SWITCH_TRUE;
  1428. }
  1429. }
  1430. return SWITCH_FALSE;
  1431. }
  1432. SWITCH_DECLARE(void) switch_channel_wait_for_state(switch_channel_t *channel, switch_channel_t *other_channel, switch_channel_state_t want_state)
  1433. {
  1434. switch_assert(channel);
  1435. for (;;) {
  1436. if ((channel->state < CS_HANGUP && channel->state == channel->running_state && channel->running_state == want_state) ||
  1437. (other_channel && switch_channel_down_nosig(other_channel)) || switch_channel_down(channel)) {
  1438. break;
  1439. }
  1440. switch_cond_next();
  1441. }
  1442. }
  1443. SWITCH_DECLARE(void) switch_channel_wait_for_state_timeout(switch_channel_t *channel, switch_channel_state_t want_state, uint32_t timeout)
  1444. {
  1445. uint32_t count = 0;
  1446. for (;;) {
  1447. if ((channel->state == channel->running_state && channel->running_state == want_state) || channel->state >= CS_HANGUP) {
  1448. break;
  1449. }
  1450. switch_channel_check_signal(channel, SWITCH_TRUE);
  1451. switch_cond_next();
  1452. if (++count >= timeout) {
  1453. break;
  1454. }
  1455. }
  1456. }
  1457. SWITCH_DECLARE(switch_status_t) switch_channel_wait_for_flag(switch_channel_t *channel,
  1458. switch_channel_flag_t want_flag,
  1459. switch_bool_t pres, uint32_t to, switch_channel_t *super_channel)
  1460. {
  1461. if (to) {
  1462. to++;
  1463. }
  1464. for (;;) {
  1465. if (pres) {
  1466. if (switch_channel_test_flag(channel, want_flag)) {
  1467. break;
  1468. }
  1469. } else {
  1470. if (!switch_channel_test_flag(channel, want_flag)) {
  1471. break;
  1472. }
  1473. }
  1474. switch_cond_next();
  1475. if (super_channel && !switch_channel_ready(super_channel)) {
  1476. return SWITCH_STATUS_FALSE;
  1477. }
  1478. if (switch_channel_down(channel)) {
  1479. return SWITCH_STATUS_FALSE;
  1480. }
  1481. if (to && !--to) {
  1482. return SWITCH_STATUS_TIMEOUT;
  1483. }
  1484. }
  1485. return SWITCH_STATUS_SUCCESS;
  1486. }
  1487. SWITCH_DECLARE(switch_status_t) switch_channel_wait_for_app_flag(switch_channel_t *channel,
  1488. uint32_t app_flag,
  1489. const char *key, switch_bool_t pres, uint32_t to)
  1490. {
  1491. int r = 0;
  1492. if (to) {
  1493. to++;
  1494. }
  1495. for (;;) {
  1496. if (pres) {
  1497. if ((r = switch_channel_test_app_flag_key(key, channel, app_flag))) {
  1498. break;
  1499. }
  1500. } else {
  1501. if (!(r = switch_channel_test_app_flag_key(key, channel, app_flag))) {
  1502. break;
  1503. }
  1504. }
  1505. switch_cond_next();
  1506. if (switch_channel_down(channel)) {
  1507. return r;
  1508. }
  1509. if (to && !--to) {
  1510. return r;
  1511. }
  1512. }
  1513. return r;
  1514. }
  1515. SWITCH_DECLARE(void) switch_channel_set_cap_value(switch_channel_t *channel, switch_channel_cap_t cap, uint32_t value)
  1516. {
  1517. switch_assert(channel);
  1518. switch_assert(channel->flag_mutex);
  1519. switch_mutex_lock(channel->flag_mutex);
  1520. channel->caps[cap] = value;
  1521. switch_mutex_unlock(channel->flag_mutex);
  1522. }
  1523. SWITCH_DECLARE(void) switch_channel_clear_cap(switch_channel_t *channel, switch_channel_cap_t cap)
  1524. {
  1525. switch_assert(channel != NULL);
  1526. switch_assert(channel->flag_mutex);
  1527. switch_mutex_lock(channel->flag_mutex);
  1528. channel->caps[cap] = 0;
  1529. switch_mutex_unlock(channel->flag_mutex);
  1530. }
  1531. SWITCH_DECLARE(uint32_t) switch_channel_test_cap(switch_channel_t *channel, switch_channel_cap_t cap)
  1532. {
  1533. switch_assert(channel != NULL);
  1534. return channel->caps[cap] ? 1 : 0;
  1535. }
  1536. SWITCH_DECLARE(uint32_t) switch_channel_test_cap_partner(switch_channel_t *channel, switch_channel_cap_t cap)
  1537. {
  1538. const char *uuid;
  1539. int r = 0;
  1540. switch_assert(channel != NULL);
  1541. if ((uuid = switch_channel_get_partner_uuid(channel))) {
  1542. switch_core_session_t *session;
  1543. if ((session = switch_core_session_locate(uuid))) {
  1544. r = switch_channel_test_cap(switch_core_session_get_channel(session), cap);
  1545. switch_core_session_rwunlock(session);
  1546. }
  1547. }
  1548. return r;
  1549. }
  1550. SWITCH_DECLARE(char *) switch_channel_get_flag_string(switch_channel_t *channel)
  1551. {
  1552. switch_stream_handle_t stream = { 0 };
  1553. char *r;
  1554. int i = 0;
  1555. SWITCH_STANDARD_STREAM(stream);
  1556. switch_mutex_lock(channel->flag_mutex);
  1557. for (i = 0; i < CF_FLAG_MAX; i++) {
  1558. if (channel->flags[i]) {
  1559. stream.write_function(&stream, "%d=%d;", i, channel->flags[i]);
  1560. }
  1561. }
  1562. switch_mutex_unlock(channel->flag_mutex);
  1563. r = (char *) stream.data;
  1564. if (end_of(r) == ';') {
  1565. end_of(r) = '\0';
  1566. }
  1567. return r;
  1568. }
  1569. SWITCH_DECLARE(char *) switch_channel_get_cap_string(switch_channel_t *channel)
  1570. {
  1571. switch_stream_handle_t stream = { 0 };
  1572. char *r;
  1573. int i = 0;
  1574. SWITCH_STANDARD_STREAM(stream);
  1575. switch_mutex_lock(channel->flag_mutex);
  1576. for (i = 0; i < CC_FLAG_MAX; i++) {
  1577. if (channel->caps[i]) {
  1578. stream.write_function(&stream, "%d=%d;", i, channel->caps[i]);
  1579. }
  1580. }
  1581. switch_mutex_unlock(channel->flag_mutex);
  1582. r = (char *) stream.data;
  1583. if (end_of(r) == ';') {
  1584. end_of(r) = '\0';
  1585. }
  1586. return r;
  1587. }
  1588. SWITCH_DECLARE(void) switch_channel_set_flag_value(switch_channel_t *channel, switch_channel_flag_t flag, uint32_t value)
  1589. {
  1590. int HELD = 0;
  1591. int just_set = 0;
  1592. switch_assert(channel);
  1593. switch_assert(channel->flag_mutex);
  1594. switch_mutex_lock(channel->flag_mutex);
  1595. if (flag == CF_LEG_HOLDING && !channel->flags[flag] && channel->flags[CF_ANSWERED]) {
  1596. HELD = 1;
  1597. }
  1598. if (channel->flags[flag] != value) {
  1599. just_set = 1;
  1600. channel->flags[flag] = value;
  1601. }
  1602. switch_mutex_unlock(channel->flag_mutex);
  1603. if (flag == CF_VIDEO_READY && just_set) {
  1604. switch_core_session_request_video_refresh(channel->session);
  1605. }
  1606. if (flag == CF_ORIGINATOR && switch_channel_test_flag(channel, CF_ANSWERED) && switch_channel_up_nosig(channel)) {
  1607. switch_channel_set_callstate(channel, CCS_RING_WAIT);
  1608. }
  1609. if (flag == CF_DIALPLAN) {
  1610. if (channel->direction == SWITCH_CALL_DIRECTION_INBOUND) {
  1611. channel->logical_direction = SWITCH_CALL_DIRECTION_OUTBOUND;
  1612. if (channel->device_node) {
  1613. channel->device_node->direction = SWITCH_CALL_DIRECTION_INBOUND;
  1614. }
  1615. } else {
  1616. channel->logical_direction = SWITCH_CALL_DIRECTION_INBOUND;
  1617. if (channel->device_node) {
  1618. channel->device_node->direction = SWITCH_CALL_DIRECTION_OUTBOUND;
  1619. }
  1620. }
  1621. }
  1622. if (HELD) {
  1623. switch_hold_record_t *hr;
  1624. const char *brto = switch_channel_get_partner_uuid(channel);
  1625. switch_channel_set_callstate(channel, CCS_HELD);
  1626. switch_mutex_lock(channel->profile_mutex);
  1627. channel->caller_profile->times->last_hold = switch_time_now();
  1628. hr = switch_core_session_alloc(channel->session, sizeof(*hr));
  1629. hr->on = switch_time_now();
  1630. if (brto) {
  1631. hr->uuid = switch_core_session_strdup(channel->session, brto);
  1632. }
  1633. if (channel->hold_record) {
  1634. hr->next = channel->hold_record;
  1635. }
  1636. channel->hold_record = hr;
  1637. switch_mutex_unlock(channel->profile_mutex);
  1638. }
  1639. if (flag == CF_OUTBOUND) {
  1640. switch_channel_set_variable(channel, "is_outbound", "true");
  1641. }
  1642. if (flag == CF_RECOVERED) {
  1643. switch_channel_set_variable(channel, "recovered", "true");
  1644. }
  1645. if (flag == CF_VIDEO_ECHO || flag == CF_VIDEO_BLANK || flag == CF_VIDEO_DECODED_READ || flag == CF_VIDEO_PASSIVE) {
  1646. switch_core_session_start_video_thread(channel->session);
  1647. }
  1648. if (flag == CF_VIDEO_DECODED_READ && channel->flags[CF_VIDEO]) {
  1649. switch_core_session_request_video_refresh(channel->session);
  1650. }
  1651. }
  1652. SWITCH_DECLARE(void) switch_channel_set_flag_recursive(switch_channel_t *channel, switch_channel_flag_t flag)
  1653. {
  1654. switch_assert(channel);
  1655. switch_assert(channel->flag_mutex);
  1656. switch_mutex_lock(channel->flag_mutex);
  1657. channel->flags[flag]++;
  1658. switch_mutex_unlock(channel->flag_mutex);
  1659. if (flag == CF_OUTBOUND) {
  1660. switch_channel_set_variable(channel, "is_outbound", "true");
  1661. }
  1662. if (flag == CF_RECOVERED) {
  1663. switch_channel_set_variable(channel, "recovered", "true");
  1664. }
  1665. }
  1666. SWITCH_DECLARE(void) switch_channel_set_private_flag(switch_channel_t *channel, uint32_t flags)
  1667. {
  1668. switch_assert(channel != NULL);
  1669. switch_mutex_lock(channel->flag_mutex);
  1670. channel->private_flags |= flags;
  1671. switch_mutex_unlock(channel->flag_mutex);
  1672. }
  1673. SWITCH_DECLARE(void) switch_channel_clear_private_flag(switch_channel_t *channel, uint32_t flags)
  1674. {
  1675. switch_assert(channel != NULL);
  1676. switch_mutex_lock(channel->flag_mutex);
  1677. channel->private_flags &= ~flags;
  1678. switch_mutex_unlock(channel->flag_mutex);
  1679. }
  1680. SWITCH_DECLARE(int) switch_channel_test_private_flag(switch_channel_t *channel, uint32_t flags)
  1681. {
  1682. switch_assert(channel != NULL);
  1683. return (channel->private_flags & flags);
  1684. }
  1685. SWITCH_DECLARE(void) switch_channel_set_app_flag_key(const char *key, switch_channel_t *channel, uint32_t flags)
  1686. {
  1687. uint32_t *flagp = NULL;
  1688. switch_byte_t new = 0;
  1689. switch_assert(channel != NULL);
  1690. switch_mutex_lock(channel->flag_mutex);
  1691. if (!channel->app_flag_hash) {
  1692. switch_core_hash_init(&channel->app_flag_hash);
  1693. new++;
  1694. }
  1695. if (new || !(flagp = switch_core_hash_find(channel->app_flag_hash, key))) {
  1696. flagp = switch_core_session_alloc(channel->session, sizeof(uint32_t));
  1697. switch_core_hash_insert(channel->app_flag_hash, key, flagp);
  1698. }
  1699. switch_assert(flagp);
  1700. *flagp |= flags;
  1701. switch_mutex_unlock(channel->flag_mutex);
  1702. }
  1703. SWITCH_DECLARE(void) switch_channel_clear_app_flag_key(const char *key, switch_channel_t *channel, uint32_t flags)
  1704. {
  1705. uint32_t *flagp = NULL;
  1706. switch_assert(channel != NULL);
  1707. switch_mutex_lock(channel->flag_mutex);
  1708. if (channel->app_flag_hash && (flagp = switch_core_hash_find(channel->app_flag_hash, key))) {
  1709. if (!flags) {
  1710. *flagp = 0;
  1711. } else {
  1712. *flagp &= ~flags;
  1713. }
  1714. }
  1715. switch_mutex_unlock(channel->flag_mutex);
  1716. }
  1717. SWITCH_DECLARE(int) switch_channel_test_app_flag_key(const char *key, switch_channel_t *channel, uint32_t flags)
  1718. {
  1719. int r = 0;
  1720. uint32_t *flagp = NULL;
  1721. switch_assert(channel != NULL);
  1722. switch_mutex_lock(channel->flag_mutex);
  1723. if (channel->app_flag_hash && (flagp = switch_core_hash_find(channel->app_flag_hash, key))) {
  1724. r = (*flagp & flags);
  1725. }
  1726. switch_mutex_unlock(channel->flag_mutex);
  1727. return r;
  1728. }
  1729. SWITCH_DECLARE(void) switch_channel_set_state_flag(switch_channel_t *channel, switch_channel_flag_t flag)
  1730. {
  1731. switch_assert(channel != NULL);
  1732. switch_mutex_lock(channel->flag_mutex);
  1733. channel->state_flags[0] = 1;
  1734. channel->state_flags[flag] = 1;
  1735. switch_mutex_unlock(channel->flag_mutex);
  1736. }
  1737. SWITCH_DECLARE(void) switch_channel_clear_state_flag(switch_channel_t *channel, switch_channel_flag_t flag)
  1738. {
  1739. switch_assert(channel != NULL);
  1740. switch_mutex_lock(channel->flag_mutex);
  1741. channel->state_flags[flag] = 0;
  1742. switch_mutex_unlock(channel->flag_mutex);
  1743. }
  1744. SWITCH_DECLARE(void) switch_channel_clear_flag(switch_channel_t *channel, switch_channel_flag_t flag)
  1745. {
  1746. int ACTIVE = 0;
  1747. int CLEAR = 0;
  1748. switch_assert(channel != NULL);
  1749. switch_assert(channel->flag_mutex);
  1750. switch_mutex_lock(channel->flag_mutex);
  1751. if (flag == CF_LEG_HOLDING && channel->flags[flag] && channel->flags[CF_ANSWERED]) {
  1752. ACTIVE = 1;
  1753. }
  1754. if (flag == CF_VIDEO_PASSIVE && channel->flags[CF_VIDEO]) {
  1755. channel->flags[CF_VIDEO_READY] = 1;
  1756. if (channel->flags[flag]) {
  1757. CLEAR = 1;
  1758. }
  1759. }
  1760. channel->flags[flag] = 0;
  1761. switch_mutex_unlock(channel->flag_mutex);
  1762. if (flag == CF_DIALPLAN) {
  1763. if (channel->direction == SWITCH_CALL_DIRECTION_OUTBOUND) {
  1764. channel->logical_direction = SWITCH_CALL_DIRECTION_OUTBOUND;
  1765. if (channel->device_node) {
  1766. channel->device_node->direction = SWITCH_CALL_DIRECTION_INBOUND;
  1767. }
  1768. }
  1769. }
  1770. if (ACTIVE) {
  1771. switch_channel_set_callstate(channel, CCS_UNHELD);
  1772. switch_mutex_lock(channel->profile_mutex);
  1773. if (channel->caller_profile->times->last_hold) {
  1774. channel->caller_profile->times->hold_accum += (switch_time_now() - channel->caller_profile->times->last_hold);
  1775. }
  1776. if (channel->hold_record) {
  1777. channel->hold_record->off = switch_time_now();
  1778. }
  1779. if (switch_channel_test_flag(channel, CF_PROXY_MODE) && switch_channel_test_flag(channel, CF_BRIDGED)) {
  1780. switch_channel_set_callstate(channel, CCS_ACTIVE);
  1781. }
  1782. switch_mutex_unlock(channel->profile_mutex);
  1783. }
  1784. if (flag == CF_ORIGINATOR && switch_channel_test_flag(channel, CF_ANSWERED) && switch_channel_up_nosig(channel)) {
  1785. switch_channel_set_callstate(channel, CCS_ACTIVE);
  1786. }
  1787. if (flag == CF_OUTBOUND) {
  1788. switch_channel_set_variable(channel, "is_outbound", NULL);
  1789. }
  1790. if (flag == CF_RECOVERED) {
  1791. switch_channel_set_variable(channel, "recovered", NULL);
  1792. }
  1793. if (flag == CF_VIDEO_PASSIVE && CLEAR) {
  1794. switch_core_session_wake_video_thread(channel->session);
  1795. }
  1796. if (flag == CF_RECOVERING && !channel->hangup_cause && !switch_channel_test_flag(channel, CF_NO_RECOVER)) {
  1797. switch_core_recovery_track(channel->session);
  1798. }
  1799. }
  1800. SWITCH_DECLARE(void) switch_channel_clear_flag_recursive(switch_channel_t *channel, switch_channel_flag_t flag)
  1801. {
  1802. switch_assert(channel != NULL);
  1803. switch_assert(channel->flag_mutex);
  1804. switch_mutex_lock(channel->flag_mutex);
  1805. if (channel->flags[flag]) {
  1806. channel->flags[flag]--;
  1807. }
  1808. switch_mutex_unlock(channel->flag_mutex);
  1809. if (flag == CF_OUTBOUND) {
  1810. switch_channel_set_variable(channel, "is_outbound", NULL);
  1811. }
  1812. }
  1813. SWITCH_DECLARE(switch_channel_state_t) switch_channel_get_state(switch_channel_t *channel)
  1814. {
  1815. switch_channel_state_t state;
  1816. switch_assert(channel != NULL);
  1817. state = channel->state;
  1818. return state;
  1819. }
  1820. SWITCH_DECLARE(switch_channel_state_t) switch_channel_get_running_state(switch_channel_t *channel)
  1821. {
  1822. switch_channel_state_t state;
  1823. switch_assert(channel != NULL);
  1824. state = channel->running_state;
  1825. return state;
  1826. }
  1827. SWITCH_DECLARE(int) switch_channel_state_change_pending(switch_channel_t *channel)
  1828. {
  1829. if (switch_channel_down_nosig(channel) || !switch_core_session_in_thread(channel->session)) {
  1830. return 0;
  1831. }
  1832. return channel->running_state != channel->state;
  1833. }
  1834. SWITCH_DECLARE(int) switch_channel_check_signal(switch_channel_t *channel, switch_bool_t in_thread_only)
  1835. {
  1836. switch_ivr_parse_signal_data(channel->session, SWITCH_FALSE, in_thread_only);
  1837. return 0;
  1838. }
  1839. SWITCH_DECLARE(int) switch_channel_test_ready(switch_channel_t *channel, switch_bool_t check_ready, switch_bool_t check_media)
  1840. {
  1841. int ret = 0;
  1842. switch_assert(channel != NULL);
  1843. switch_channel_check_signal(channel, SWITCH_TRUE);
  1844. if (check_media) {
  1845. ret = ((switch_channel_test_flag(channel, CF_ANSWERED) ||
  1846. switch_channel_test_flag(channel, CF_EARLY_MEDIA)) && !switch_channel_test_flag(channel, CF_PROXY_MODE) &&
  1847. switch_core_session_get_read_codec(channel->session) && switch_core_session_get_write_codec(channel->session));
  1848. if (!ret)
  1849. return ret;
  1850. }
  1851. if (!check_ready)
  1852. return ret;
  1853. ret = 0;
  1854. if (!channel->hangup_cause && channel->state > CS_ROUTING && channel->state < CS_HANGUP && channel->state != CS_RESET &&
  1855. !switch_channel_test_flag(channel, CF_TRANSFER) && !switch_channel_test_flag(channel, CF_NOT_READY) &&
  1856. !switch_channel_state_change_pending(channel)) {
  1857. ret++;
  1858. }
  1859. return ret;
  1860. }
  1861. static const char *state_names[] = {
  1862. "CS_NEW",
  1863. "CS_INIT",
  1864. "CS_ROUTING",
  1865. "CS_SOFT_EXECUTE",
  1866. "CS_EXECUTE",
  1867. "CS_EXCHANGE_MEDIA",
  1868. "CS_PARK",
  1869. "CS_CONSUME_MEDIA",
  1870. "CS_HIBERNATE",
  1871. "CS_RESET",
  1872. "CS_HANGUP",
  1873. "CS_REPORTING",
  1874. "CS_DESTROY",
  1875. "CS_NONE",
  1876. NULL
  1877. };
  1878. SWITCH_DECLARE(const char *) switch_channel_state_name(switch_channel_state_t state)
  1879. {
  1880. return state_names[state];
  1881. }
  1882. SWITCH_DECLARE(switch_channel_state_t) switch_channel_name_state(const char *name)
  1883. {
  1884. uint32_t x = 0;
  1885. for (x = 0; state_names[x]; x++) {
  1886. if (!strcasecmp(state_names[x], name)) {
  1887. return (switch_channel_state_t) x;
  1888. }
  1889. }
  1890. return CS_DESTROY;
  1891. }
  1892. static inline void careful_set(switch_channel_t *channel, switch_channel_state_t *state, switch_channel_state_t val) {
  1893. if (switch_mutex_trylock(channel->thread_mutex) == SWITCH_STATUS_SUCCESS) {
  1894. *state = val;
  1895. switch_mutex_unlock(channel->thread_mutex);
  1896. } else {
  1897. switch_mutex_t *mutex = switch_core_session_get_mutex(channel->session);
  1898. int x = 0;
  1899. for (x = 0; x < 100; x++) {
  1900. if (switch_mutex_trylock(mutex) == SWITCH_STATUS_SUCCESS) {
  1901. *state = val;
  1902. switch_mutex_unlock(mutex);
  1903. break;
  1904. } else {
  1905. switch_cond_next();
  1906. }
  1907. }
  1908. if (x == 100) {
  1909. *state = val;
  1910. }
  1911. }
  1912. }
  1913. SWITCH_DECLARE(switch_channel_state_t) switch_channel_perform_set_running_state(switch_channel_t *channel, switch_channel_state_t state,
  1914. const char *file, const char *func, int line)
  1915. {
  1916. int x;
  1917. switch_mutex_lock(channel->flag_mutex);
  1918. if (channel->state_flags[0]) {
  1919. for (x = 1; x < CF_FLAG_MAX; x++) {
  1920. if (channel->state_flags[x]) {
  1921. channel->flags[x] = 1;
  1922. channel->state_flags[x] = 0;
  1923. }
  1924. }
  1925. channel->state_flags[0] = 0;
  1926. }
  1927. switch_mutex_unlock(channel->flag_mutex);
  1928. switch_channel_clear_flag(channel, CF_TAGGED);
  1929. switch_log_printf(SWITCH_CHANNEL_ID_LOG, file, func, line, switch_channel_get_uuid(channel), SWITCH_LOG_DEBUG, "(%s) Running State Change %s (Cur %d Tot %" SWITCH_SIZE_T_FMT ")\n",
  1930. channel->name, state_names[state], switch_core_session_count(), switch_core_session_id() - 1);
  1931. switch_mutex_lock(channel->state_mutex);
  1932. careful_set(channel, &channel->running_state, state);
  1933. if (state <= CS_DESTROY) {
  1934. switch_event_t *event;
  1935. if (switch_channel_direction(channel) == SWITCH_CALL_DIRECTION_INBOUND) {
  1936. if (state < CS_HANGUP) {
  1937. if (state == CS_ROUTING) {
  1938. switch_channel_set_callstate(channel, CCS_RINGING);
  1939. } else if (switch_channel_test_flag(channel, CF_ANSWERED)) {
  1940. switch_channel_set_callstate(channel, CCS_ACTIVE);
  1941. } else if (switch_channel_test_flag(channel, CF_EARLY_MEDIA)) {
  1942. switch_channel_set_callstate(channel, CCS_EARLY);
  1943. }
  1944. }
  1945. }
  1946. if (switch_event_create(&event, SWITCH_EVENT_CHANNEL_STATE) == SWITCH_STATUS_SUCCESS) {
  1947. switch_channel_event_set_data(channel, event);
  1948. switch_event_fire(&event);
  1949. }
  1950. }
  1951. switch_mutex_unlock(channel->state_mutex);
  1952. return (switch_channel_state_t) SWITCH_STATUS_SUCCESS;
  1953. }
  1954. SWITCH_DECLARE(switch_channel_state_t) switch_channel_perform_set_state(switch_channel_t *channel,
  1955. const char *file, const char *func, int line, switch_channel_state_t state)
  1956. {
  1957. switch_channel_state_t last_state;
  1958. int ok = 0;
  1959. switch_assert(channel != NULL);
  1960. switch_assert(state <= CS_DESTROY);
  1961. switch_mutex_lock(channel->state_mutex);
  1962. last_state = channel->state;
  1963. switch_assert(last_state <= CS_DESTROY);
  1964. if (last_state == state) {
  1965. goto done;
  1966. }
  1967. if (last_state >= CS_HANGUP && state < last_state) {
  1968. goto done;
  1969. }
  1970. /* STUB for more dev
  1971. case CS_INIT:
  1972. switch(state) {
  1973. case CS_NEW:
  1974. case CS_INIT:
  1975. case CS_EXCHANGE_MEDIA:
  1976. case CS_SOFT_EXECUTE:
  1977. case CS_ROUTING:
  1978. case CS_EXECUTE:
  1979. case CS_HANGUP:
  1980. case CS_DESTROY:
  1981. default:
  1982. break;
  1983. }
  1984. break;
  1985. */
  1986. switch (last_state) {
  1987. case CS_NEW:
  1988. case CS_RESET:
  1989. switch (state) {
  1990. default:
  1991. ok++;
  1992. break;
  1993. }
  1994. break;
  1995. case CS_INIT:
  1996. switch (state) {
  1997. case CS_EXCHANGE_MEDIA:
  1998. case CS_SOFT_EXECUTE:
  1999. case CS_ROUTING:
  2000. case CS_EXECUTE:
  2001. case CS_PARK:
  2002. case CS_CONSUME_MEDIA:
  2003. case CS_HIBERNATE:
  2004. case CS_RESET:
  2005. ok++;
  2006. default:
  2007. break;
  2008. }
  2009. break;
  2010. case CS_EXCHANGE_MEDIA:
  2011. switch (state) {
  2012. case CS_SOFT_EXECUTE:
  2013. case CS_ROUTING:
  2014. case CS_EXECUTE:
  2015. case CS_PARK:
  2016. case CS_CONSUME_MEDIA:
  2017. case CS_HIBERNATE:
  2018. case CS_RESET:
  2019. ok++;
  2020. default:
  2021. break;
  2022. }
  2023. break;
  2024. case CS_SOFT_EXECUTE:
  2025. switch (state) {
  2026. case CS_EXCHANGE_MEDIA:
  2027. case CS_ROUTING:
  2028. case CS_EXECUTE:
  2029. case CS_PARK:
  2030. case CS_CONSUME_MEDIA:
  2031. case CS_HIBERNATE:
  2032. case CS_RESET:
  2033. ok++;
  2034. default:
  2035. break;
  2036. }
  2037. break;
  2038. case CS_PARK:
  2039. switch (state) {
  2040. case CS_EXCHANGE_MEDIA:
  2041. case CS_ROUTING:
  2042. case CS_EXECUTE:
  2043. case CS_SOFT_EXECUTE:
  2044. case CS_HIBERNATE:
  2045. case CS_RESET:
  2046. case CS_CONSUME_MEDIA:
  2047. ok++;
  2048. default:
  2049. break;
  2050. }
  2051. break;
  2052. case CS_CONSUME_MEDIA:
  2053. switch (state) {
  2054. case CS_EXCHANGE_MEDIA:
  2055. case CS_ROUTING:
  2056. case CS_EXECUTE:
  2057. case CS_SOFT_EXECUTE:
  2058. case CS_HIBERNATE:
  2059. case CS_RESET:
  2060. case CS_PARK:
  2061. ok++;
  2062. default:
  2063. break;
  2064. }
  2065. break;
  2066. case CS_HIBERNATE:
  2067. switch (state) {
  2068. case CS_EXCHANGE_MEDIA:
  2069. case CS_INIT:
  2070. case CS_ROUTING:
  2071. case CS_EXECUTE:
  2072. case CS_SOFT_EXECUTE:
  2073. case CS_PARK:
  2074. case CS_CONSUME_MEDIA:
  2075. case CS_RESET:
  2076. ok++;
  2077. default:
  2078. break;
  2079. }
  2080. break;
  2081. case CS_ROUTING:
  2082. switch (state) {
  2083. case CS_EXCHANGE_MEDIA:
  2084. case CS_EXECUTE:
  2085. case CS_SOFT_EXECUTE:
  2086. case CS_PARK:
  2087. case CS_CONSUME_MEDIA:
  2088. case CS_HIBERNATE:
  2089. case CS_RESET:
  2090. ok++;
  2091. default:
  2092. break;
  2093. }
  2094. break;
  2095. case CS_EXECUTE:
  2096. switch (state) {
  2097. case CS_EXCHANGE_MEDIA:
  2098. case CS_SOFT_EXECUTE:
  2099. case CS_ROUTING:
  2100. case CS_PARK:
  2101. case CS_CONSUME_MEDIA:
  2102. case CS_HIBERNATE:
  2103. case CS_RESET:
  2104. ok++;
  2105. default:
  2106. break;
  2107. }
  2108. break;
  2109. case CS_HANGUP:
  2110. switch (state) {
  2111. case CS_REPORTING:
  2112. case CS_DESTROY:
  2113. ok++;
  2114. default:
  2115. break;
  2116. }
  2117. break;
  2118. case CS_REPORTING:
  2119. switch (state) {
  2120. case CS_DESTROY:
  2121. ok++;
  2122. default:
  2123. break;
  2124. }
  2125. break;
  2126. default:
  2127. break;
  2128. }
  2129. if (ok) {
  2130. switch_log_printf(SWITCH_CHANNEL_ID_LOG, file, func, line, switch_channel_get_uuid(channel), SWITCH_LOG_DEBUG, "(%s) State Change %s -> %s\n",
  2131. channel->name, state_names[last_state], state_names[state]);
  2132. careful_set(channel, &channel->state, state);
  2133. if (state == CS_HANGUP && !channel->hangup_cause) {
  2134. channel->hangup_cause = SWITCH_CAUSE_NORMAL_CLEARING;
  2135. }
  2136. if (state <= CS_DESTROY) {
  2137. switch_core_session_signal_state_change(channel->session);
  2138. }
  2139. } else {
  2140. switch_log_printf(SWITCH_CHANNEL_ID_LOG, file, func, line, switch_channel_get_uuid(channel), SWITCH_LOG_WARNING,
  2141. "(%s) Invalid State Change %s -> %s\n", channel->name, state_names[last_state], state_names[state]);
  2142. /* we won't tolerate an invalid state change so we can make sure we are as robust as a nice cup of dark coffee! */
  2143. /* not cool lets crash this bad boy and figure out wtf is going on */
  2144. switch_assert(channel->state >= CS_HANGUP);
  2145. }
  2146. done:
  2147. switch_mutex_unlock(channel->state_mutex);
  2148. return channel->state;
  2149. }
  2150. SWITCH_DECLARE(void) switch_channel_state_thread_lock(switch_channel_t *channel)
  2151. {
  2152. switch_mutex_lock(channel->thread_mutex);
  2153. }
  2154. SWITCH_DECLARE(switch_status_t) switch_channel_state_thread_trylock(switch_channel_t *channel)
  2155. {
  2156. return switch_mutex_trylock(channel->thread_mutex);
  2157. }
  2158. SWITCH_DECLARE(void) switch_channel_state_thread_unlock(switch_channel_t *channel)
  2159. {
  2160. switch_mutex_unlock(channel->thread_mutex);
  2161. }
  2162. SWITCH_DECLARE(void) switch_channel_event_set_basic_data(switch_channel_t *channel, switch_event_t *event)
  2163. {
  2164. switch_caller_profile_t *caller_profile, *originator_caller_profile = NULL, *originatee_caller_profile = NULL;
  2165. switch_codec_implementation_t impl = { 0 };
  2166. char state_num[25];
  2167. const char *v;
  2168. switch_mutex_lock(channel->profile_mutex);
  2169. if ((caller_profile = channel->caller_profile)) {
  2170. originator_caller_profile = caller_profile->originator_caller_profile;
  2171. originatee_caller_profile = caller_profile->originatee_caller_profile;
  2172. }
  2173. switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "Channel-State", switch_channel_state_name(channel->running_state));
  2174. switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "Channel-Call-State", switch_channel_callstate2str(channel->callstate));
  2175. switch_snprintf(state_num, sizeof(state_num), "%d", channel->state);
  2176. switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "Channel-State-Number", state_num);
  2177. switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "Channel-Name", switch_channel_get_name(channel));
  2178. switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "Unique-ID", switch_core_session_get_uuid(channel->session));
  2179. switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "Session-External-ID", switch_core_session_get_external_id(channel->session));
  2180. switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "Call-Direction",
  2181. channel->direction == SWITCH_CALL_DIRECTION_OUTBOUND ? "outbound" : "inbound");
  2182. switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "Presence-Call-Direction",
  2183. channel->direction == SWITCH_CALL_DIRECTION_OUTBOUND ? "outbound" : "inbound");
  2184. switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "Channel-HIT-Dialplan",
  2185. switch_channel_direction(channel) == SWITCH_CALL_DIRECTION_INBOUND ||
  2186. switch_channel_test_flag(channel, CF_DIALPLAN) ? "true" : "false");
  2187. if ((v = switch_channel_get_variable_dup(channel, "presence_id", SWITCH_FALSE, -1))) {
  2188. switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "Channel-Presence-ID", v);
  2189. }
  2190. if ((v = switch_channel_get_variable_dup(channel, "presence_data", SWITCH_FALSE, -1))) {
  2191. switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "Channel-Presence-Data", v);
  2192. }
  2193. if ((v = switch_channel_get_variable_dup(channel, "presence_data_cols", SWITCH_FALSE, -1))) {
  2194. switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "Presence-Data-Cols", v);
  2195. switch_event_add_presence_data_cols(channel, event, "PD-");
  2196. }
  2197. if ((v = switch_channel_get_variable_dup(channel, "call_uuid", SWITCH_FALSE, -1))) {
  2198. switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "Channel-Call-UUID", v);
  2199. } else {
  2200. switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "Channel-Call-UUID", switch_core_session_get_uuid(channel->session));
  2201. }
  2202. if (switch_channel_down_nosig(channel)) {
  2203. switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "Answer-State", "hangup");
  2204. } else if (switch_channel_test_flag(channel, CF_ANSWERED)) {
  2205. switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "Answer-State", "answered");
  2206. } else if (switch_channel_test_flag(channel, CF_EARLY_MEDIA)) {
  2207. switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "Answer-State", "early");
  2208. } else {
  2209. switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "Answer-State", "ringing");
  2210. }
  2211. if (channel->hangup_cause) {
  2212. switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "Hangup-Cause", switch_channel_cause2str(channel->hangup_cause));
  2213. }
  2214. switch_core_session_get_read_impl(channel->session, &impl);
  2215. if (impl.iananame) {
  2216. switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "Channel-Read-Codec-Name", impl.iananame);
  2217. switch_event_add_header(event, SWITCH_STACK_BOTTOM, "Channel-Read-Codec-Rate", "%u", impl.actual_samples_per_second);
  2218. switch_event_add_header(event, SWITCH_STACK_BOTTOM, "Channel-Read-Codec-Bit-Rate", "%d", impl.bits_per_second);
  2219. }
  2220. switch_core_session_get_write_impl(channel->session, &impl);
  2221. if (impl.iananame) {
  2222. switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "Channel-Write-Codec-Name", impl.iananame);
  2223. switch_event_add_header(event, SWITCH_STACK_BOTTOM, "Channel-Write-Codec-Rate", "%u", impl.actual_samples_per_second);
  2224. switch_event_add_header(event, SWITCH_STACK_BOTTOM, "Channel-Write-Codec-Bit-Rate", "%d", impl.bits_per_second);
  2225. }
  2226. /* Index Caller's Profile */
  2227. if (caller_profile) {
  2228. switch_caller_profile_event_set_data(caller_profile, "Caller", event);
  2229. }
  2230. /* Index Originator/ee's Profile */
  2231. if (originator_caller_profile && channel->last_profile_type == LP_ORIGINATOR) {
  2232. switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "Other-Type", "originator");
  2233. switch_caller_profile_event_set_data(originator_caller_profile, "Other-Leg", event);
  2234. } else if (originatee_caller_profile && channel->last_profile_type == LP_ORIGINATEE) { /* Index Originatee's Profile */
  2235. switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "Other-Type", "originatee");
  2236. switch_caller_profile_event_set_data(originatee_caller_profile, "Other-Leg", event);
  2237. }
  2238. switch_mutex_unlock(channel->profile_mutex);
  2239. }
  2240. SWITCH_DECLARE(void) switch_channel_event_set_extended_data(switch_channel_t *channel, switch_event_t *event)
  2241. {
  2242. switch_event_header_t *hi;
  2243. int global_verbose_events = -1;
  2244. switch_mutex_lock(channel->profile_mutex);
  2245. switch_core_session_ctl(SCSC_VERBOSE_EVENTS, &global_verbose_events);
  2246. if (global_verbose_events ||
  2247. switch_channel_test_flag(channel, CF_VERBOSE_EVENTS) ||
  2248. switch_event_get_header(event, "presence-data-cols") ||
  2249. event->event_id == SWITCH_EVENT_CHANNEL_CREATE ||
  2250. event->event_id == SWITCH_EVENT_CHANNEL_ORIGINATE ||
  2251. event->event_id == SWITCH_EVENT_CHANNEL_UUID ||
  2252. event->event_id == SWITCH_EVENT_CHANNEL_ANSWER ||
  2253. event->event_id == SWITCH_EVENT_CHANNEL_PARK ||
  2254. event->event_id == SWITCH_EVENT_CHANNEL_UNPARK ||
  2255. event->event_id == SWITCH_EVENT_CHANNEL_BRIDGE ||
  2256. event->event_id == SWITCH_EVENT_CHANNEL_UNBRIDGE ||
  2257. event->event_id == SWITCH_EVENT_CHANNEL_PROGRESS ||
  2258. event->event_id == SWITCH_EVENT_CHANNEL_PROGRESS_MEDIA ||
  2259. event->event_id == SWITCH_EVENT_CHANNEL_HANGUP ||
  2260. event->event_id == SWITCH_EVENT_CHANNEL_HANGUP_COMPLETE ||
  2261. event->event_id == SWITCH_EVENT_REQUEST_PARAMS ||
  2262. event->event_id == SWITCH_EVENT_CHANNEL_DATA ||
  2263. event->event_id == SWITCH_EVENT_CHANNEL_EXECUTE ||
  2264. event->event_id == SWITCH_EVENT_CHANNEL_EXECUTE_COMPLETE ||
  2265. event->event_id == SWITCH_EVENT_CHANNEL_DESTROY ||
  2266. event->event_id == SWITCH_EVENT_SESSION_HEARTBEAT ||
  2267. event->event_id == SWITCH_EVENT_API ||
  2268. event->event_id == SWITCH_EVENT_RECORD_START ||
  2269. event->event_id == SWITCH_EVENT_RECORD_STOP ||
  2270. event->event_id == SWITCH_EVENT_PLAYBACK_START ||
  2271. event->event_id == SWITCH_EVENT_PLAYBACK_STOP ||
  2272. event->event_id == SWITCH_EVENT_CALL_UPDATE ||
  2273. event->event_id == SWITCH_EVENT_MEDIA_BUG_START ||
  2274. event->event_id == SWITCH_EVENT_MEDIA_BUG_STOP ||
  2275. event->event_id == SWITCH_EVENT_CHANNEL_HOLD ||
  2276. event->event_id == SWITCH_EVENT_CHANNEL_UNHOLD ||
  2277. event->event_id == SWITCH_EVENT_TEXT ||
  2278. event->event_id == SWITCH_EVENT_CUSTOM) {
  2279. /* Index Variables */
  2280. if (channel->scope_variables) {
  2281. switch_event_t *ep;
  2282. for (ep = channel->scope_variables; ep; ep = ep->next) {
  2283. for (hi = ep->headers; hi; hi = hi->next) {
  2284. char buf[1024];
  2285. char *vvar = NULL, *vval = NULL;
  2286. vvar = (char *) hi->name;
  2287. vval = (char *) hi->value;
  2288. switch_assert(vvar && vval);
  2289. switch_snprintf(buf, sizeof(buf), "scope_variable_%s", vvar);
  2290. if (!switch_event_get_header(event, buf)) {
  2291. switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, buf, vval);
  2292. }
  2293. }
  2294. }
  2295. }
  2296. if (channel->variables) {
  2297. for (hi = channel->variables->headers; hi; hi = hi->next) {
  2298. char buf[1024];
  2299. char *vvar = NULL, *vval = NULL;
  2300. vvar = (char *) hi->name;
  2301. vval = (char *) hi->value;
  2302. switch_assert(vvar && vval);
  2303. switch_snprintf(buf, sizeof(buf), "variable_%s", vvar);
  2304. switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, buf, vval);
  2305. }
  2306. }
  2307. }
  2308. switch_mutex_unlock(channel->profile_mutex);
  2309. }
  2310. SWITCH_DECLARE(void) switch_channel_event_set_data(switch_channel_t *channel, switch_event_t *event)
  2311. {
  2312. switch_mutex_lock(channel->profile_mutex);
  2313. switch_channel_event_set_basic_data(channel, event);
  2314. switch_channel_event_set_extended_data(channel, event);
  2315. switch_mutex_unlock(channel->profile_mutex);
  2316. }
  2317. SWITCH_DECLARE(void) switch_channel_step_caller_profile(switch_channel_t *channel)
  2318. {
  2319. switch_caller_profile_t *cp;
  2320. switch_mutex_lock(channel->profile_mutex);
  2321. cp = switch_caller_profile_clone(channel->session, channel->caller_profile);
  2322. switch_mutex_unlock(channel->profile_mutex);
  2323. switch_channel_set_caller_profile(channel, cp);
  2324. }
  2325. SWITCH_DECLARE(void) switch_channel_set_caller_profile(switch_channel_t *channel, switch_caller_profile_t *caller_profile)
  2326. {
  2327. char *uuid = NULL;
  2328. switch_assert(channel != NULL);
  2329. switch_assert(channel->session != NULL);
  2330. switch_mutex_lock(channel->profile_mutex);
  2331. switch_assert(caller_profile != NULL);
  2332. caller_profile->direction = channel->direction;
  2333. caller_profile->logical_direction = channel->logical_direction;
  2334. uuid = switch_core_session_get_uuid(channel->session);
  2335. if (!caller_profile->uuid || strcasecmp(caller_profile->uuid, uuid)) {
  2336. caller_profile->uuid = switch_core_session_strdup(channel->session, uuid);
  2337. }
  2338. if (!caller_profile->chan_name || strcasecmp(caller_profile->chan_name, channel->name)) {
  2339. caller_profile->chan_name = switch_core_session_strdup(channel->session, channel->name);
  2340. }
  2341. if (!caller_profile->context) {
  2342. caller_profile->context = switch_core_session_strdup(channel->session, "default");
  2343. }
  2344. if (!caller_profile->times) {
  2345. caller_profile->times = (switch_channel_timetable_t *) switch_core_session_alloc(channel->session, sizeof(*caller_profile->times));
  2346. caller_profile->times->profile_created = switch_micro_time_now();
  2347. }
  2348. if (channel->caller_profile && channel->caller_profile->times) {
  2349. channel->caller_profile->times->transferred = caller_profile->times->profile_created;
  2350. caller_profile->times->answered = channel->caller_profile->times->answered;
  2351. caller_profile->times->progress = channel->caller_profile->times->progress;
  2352. caller_profile->times->progress_media = channel->caller_profile->times->progress_media;
  2353. caller_profile->times->created = channel->caller_profile->times->created;
  2354. caller_profile->times->hungup = channel->caller_profile->times->hungup;
  2355. if (channel->caller_profile->caller_extension) {
  2356. switch_caller_extension_clone(&caller_profile->caller_extension, channel->caller_profile->caller_extension, caller_profile->pool);
  2357. }
  2358. } else {
  2359. caller_profile->times->created = switch_micro_time_now();
  2360. }
  2361. caller_profile->next = channel->caller_profile;
  2362. channel->caller_profile = caller_profile;
  2363. caller_profile->profile_index = switch_core_sprintf(caller_profile->pool, "%d", ++channel->profile_index);
  2364. switch_mutex_unlock(channel->profile_mutex);
  2365. }
  2366. SWITCH_DECLARE(switch_caller_profile_t *) switch_channel_get_caller_profile(switch_channel_t *channel)
  2367. {
  2368. switch_caller_profile_t *profile;
  2369. switch_assert(channel != NULL);
  2370. switch_mutex_lock(channel->profile_mutex);
  2371. if ((profile = channel->caller_profile) && profile->hunt_caller_profile) {
  2372. profile = profile->hunt_caller_profile;
  2373. }
  2374. switch_mutex_unlock(channel->profile_mutex);
  2375. return profile;
  2376. }
  2377. SWITCH_DECLARE(void) switch_channel_set_originator_caller_profile(switch_channel_t *channel, switch_caller_profile_t *caller_profile)
  2378. {
  2379. switch_assert(channel != NULL);
  2380. switch_assert(channel->caller_profile != NULL);
  2381. switch_mutex_lock(channel->profile_mutex);
  2382. if (!caller_profile->times) {
  2383. caller_profile->times = (switch_channel_timetable_t *) switch_core_alloc(caller_profile->pool, sizeof(*caller_profile->times));
  2384. }
  2385. if (channel->caller_profile) {
  2386. caller_profile->next = channel->caller_profile->originator_caller_profile;
  2387. channel->caller_profile->originator_caller_profile = caller_profile;
  2388. channel->last_profile_type = LP_ORIGINATOR;
  2389. }
  2390. switch_assert(channel->caller_profile->originator_caller_profile->next != channel->caller_profile->originator_caller_profile);
  2391. switch_mutex_unlock(channel->profile_mutex);
  2392. }
  2393. SWITCH_DECLARE(void) switch_channel_set_hunt_caller_profile(switch_channel_t *channel, switch_caller_profile_t *caller_profile)
  2394. {
  2395. switch_assert(channel != NULL);
  2396. switch_assert(channel->caller_profile != NULL);
  2397. switch_mutex_lock(channel->profile_mutex);
  2398. channel->caller_profile->hunt_caller_profile = NULL;
  2399. if (channel->caller_profile && caller_profile) {
  2400. caller_profile->direction = channel->direction;
  2401. caller_profile->logical_direction = channel->logical_direction;
  2402. channel->caller_profile->hunt_caller_profile = caller_profile;
  2403. }
  2404. switch_mutex_unlock(channel->profile_mutex);
  2405. }
  2406. SWITCH_DECLARE(void) switch_channel_set_origination_caller_profile(switch_channel_t *channel, switch_caller_profile_t *caller_profile)
  2407. {
  2408. switch_assert(channel != NULL);
  2409. switch_assert(channel->caller_profile != NULL);
  2410. switch_mutex_lock(channel->profile_mutex);
  2411. if (channel->caller_profile) {
  2412. caller_profile->next = channel->caller_profile->origination_caller_profile;
  2413. channel->caller_profile->origination_caller_profile = caller_profile;
  2414. }
  2415. switch_assert(channel->caller_profile->origination_caller_profile->next != channel->caller_profile->origination_caller_profile);
  2416. switch_mutex_unlock(channel->profile_mutex);
  2417. }
  2418. SWITCH_DECLARE(switch_caller_profile_t *) switch_channel_get_origination_caller_profile(switch_channel_t *channel)
  2419. {
  2420. switch_caller_profile_t *profile = NULL;
  2421. switch_assert(channel != NULL);
  2422. switch_mutex_lock(channel->profile_mutex);
  2423. if (channel->caller_profile) {
  2424. profile = channel->caller_profile->origination_caller_profile;
  2425. }
  2426. switch_mutex_unlock(channel->profile_mutex);
  2427. return profile;
  2428. }
  2429. SWITCH_DECLARE(void) switch_channel_set_originatee_caller_profile(switch_channel_t *channel, switch_caller_profile_t *caller_profile)
  2430. {
  2431. switch_assert(channel != NULL);
  2432. switch_assert(channel->caller_profile != NULL);
  2433. switch_mutex_lock(channel->profile_mutex);
  2434. if (channel->caller_profile) {
  2435. caller_profile->next = channel->caller_profile->originatee_caller_profile;
  2436. channel->caller_profile->originatee_caller_profile = caller_profile;
  2437. channel->last_profile_type = LP_ORIGINATEE;
  2438. }
  2439. switch_assert(channel->caller_profile->originatee_caller_profile->next != channel->caller_profile->originatee_caller_profile);
  2440. switch_mutex_unlock(channel->profile_mutex);
  2441. }
  2442. SWITCH_DECLARE(switch_caller_profile_t *) switch_channel_get_originator_caller_profile(switch_channel_t *channel)
  2443. {
  2444. switch_caller_profile_t *profile = NULL;
  2445. switch_assert(channel != NULL);
  2446. switch_mutex_lock(channel->profile_mutex);
  2447. if (channel->caller_profile) {
  2448. profile = channel->caller_profile->originator_caller_profile;
  2449. }
  2450. switch_mutex_unlock(channel->profile_mutex);
  2451. return profile;
  2452. }
  2453. SWITCH_DECLARE(switch_caller_profile_t *) switch_channel_get_originatee_caller_profile(switch_channel_t *channel)
  2454. {
  2455. switch_caller_profile_t *profile = NULL;
  2456. switch_assert(channel != NULL);
  2457. switch_mutex_lock(channel->profile_mutex);
  2458. if (channel->caller_profile) {
  2459. profile = channel->caller_profile->originatee_caller_profile;
  2460. }
  2461. switch_mutex_unlock(channel->profile_mutex);
  2462. return profile;
  2463. }
  2464. SWITCH_DECLARE(char *) switch_channel_get_uuid(switch_channel_t *channel)
  2465. {
  2466. switch_assert(channel != NULL);
  2467. switch_assert(channel->session != NULL);
  2468. return switch_core_session_get_uuid(channel->session);
  2469. }
  2470. SWITCH_DECLARE(int) switch_channel_add_state_handler(switch_channel_t *channel, const switch_state_handler_table_t *state_handler)
  2471. {
  2472. int x, index;
  2473. switch_assert(channel != NULL);
  2474. switch_mutex_lock(channel->state_mutex);
  2475. for (x = 0; x < SWITCH_MAX_STATE_HANDLERS; x++) {
  2476. if (channel->state_handlers[x] == state_handler) {
  2477. index = x;
  2478. goto end;
  2479. }
  2480. }
  2481. index = channel->state_handler_index++;
  2482. if (channel->state_handler_index >= SWITCH_MAX_STATE_HANDLERS) {
  2483. index = -1;
  2484. goto end;
  2485. }
  2486. channel->state_handlers[index] = state_handler;
  2487. end:
  2488. switch_mutex_unlock(channel->state_mutex);
  2489. return index;
  2490. }
  2491. SWITCH_DECLARE(const switch_state_handler_table_t *) switch_channel_get_state_handler(switch_channel_t *channel, int index)
  2492. {
  2493. const switch_state_handler_table_t *h = NULL;
  2494. switch_assert(channel != NULL);
  2495. if (index >= SWITCH_MAX_STATE_HANDLERS || index > channel->state_handler_index) {
  2496. return NULL;
  2497. }
  2498. switch_mutex_lock(channel->state_mutex);
  2499. h = channel->state_handlers[index];
  2500. switch_mutex_unlock(channel->state_mutex);
  2501. return h;
  2502. }
  2503. SWITCH_DECLARE(void) switch_channel_clear_state_handler(switch_channel_t *channel, const switch_state_handler_table_t *state_handler)
  2504. {
  2505. int index, i = channel->state_handler_index;
  2506. const switch_state_handler_table_t *new_handlers[SWITCH_MAX_STATE_HANDLERS] = { 0 };
  2507. switch_assert(channel != NULL);
  2508. switch_mutex_lock(channel->state_mutex);
  2509. channel->state_handler_index = 0;
  2510. if (state_handler) {
  2511. for (index = 0; index < i; index++) {
  2512. if (channel->state_handlers[index] != state_handler) {
  2513. new_handlers[channel->state_handler_index++] = channel->state_handlers[index];
  2514. }
  2515. }
  2516. } else {
  2517. for (index = 0; index < i; index++) {
  2518. if (channel->state_handlers[index] && switch_test_flag(channel->state_handlers[index], SSH_FLAG_STICKY)) {
  2519. new_handlers[channel->state_handler_index++] = channel->state_handlers[index];
  2520. }
  2521. }
  2522. }
  2523. for (index = 0; index < SWITCH_MAX_STATE_HANDLERS; index++) {
  2524. channel->state_handlers[index] = NULL;
  2525. }
  2526. if (channel->state_handler_index > 0) {
  2527. for (index = 0; index < channel->state_handler_index; index++) {
  2528. channel->state_handlers[index] = new_handlers[index];
  2529. }
  2530. }
  2531. switch_mutex_unlock(channel->state_mutex);
  2532. }
  2533. SWITCH_DECLARE(void) switch_channel_restart(switch_channel_t *channel)
  2534. {
  2535. switch_channel_set_state(channel, CS_RESET);
  2536. switch_channel_wait_for_state_timeout(channel, CS_RESET, 5000);
  2537. switch_channel_set_state(channel, CS_EXECUTE);
  2538. }
  2539. /* XXX This is a somewhat simple operation. Were essentially taking the extension that one channel
  2540. was executing and generating a new extension for another channel that starts out where the
  2541. original one left off with an optional forward offset. Since all we are really doing is
  2542. copying a few basic pool-allocated structures from one channel to another there really is
  2543. not much to worry about here in terms of threading since we use read-write locks.
  2544. While the features are nice, they only really are needed in one specific crazy attended
  2545. transfer scenario where one channel was in the middle of calling a particular extension
  2546. when it was rudely cut off by a transfer key press. XXX */
  2547. SWITCH_DECLARE(switch_status_t) switch_channel_caller_extension_masquerade(switch_channel_t *orig_channel, switch_channel_t *new_channel, uint32_t offset)
  2548. {
  2549. switch_caller_profile_t *caller_profile;
  2550. switch_caller_extension_t *extension = NULL, *orig_extension = NULL;
  2551. switch_caller_application_t *ap;
  2552. switch_status_t status = SWITCH_STATUS_FALSE;
  2553. switch_event_header_t *hi = NULL;
  2554. const char *no_copy = switch_channel_get_variable(orig_channel, "attended_transfer_no_copy");
  2555. char *dup;
  2556. int i, argc = 0;
  2557. char *argv[128];
  2558. if (no_copy) {
  2559. dup = switch_core_session_strdup(new_channel->session, no_copy);
  2560. argc = switch_separate_string(dup, ',', argv, (sizeof(argv) / sizeof(argv[0])));
  2561. }
  2562. switch_mutex_lock(orig_channel->profile_mutex);
  2563. switch_mutex_lock(new_channel->profile_mutex);
  2564. caller_profile = switch_caller_profile_clone(new_channel->session, new_channel->caller_profile);
  2565. switch_assert(caller_profile);
  2566. extension = switch_caller_extension_new(new_channel->session, caller_profile->destination_number, caller_profile->destination_number);
  2567. orig_extension = switch_channel_get_caller_extension(orig_channel);
  2568. if (extension && orig_extension) {
  2569. for (ap = orig_extension->current_application; ap && offset > 0; offset--) {
  2570. ap = ap->next;
  2571. }
  2572. for (; ap; ap = ap->next) {
  2573. switch_caller_extension_add_application(new_channel->session, extension, ap->application_name, ap->application_data);
  2574. }
  2575. caller_profile->destination_number = switch_core_strdup(caller_profile->pool, orig_channel->caller_profile->destination_number);
  2576. switch_channel_set_caller_profile(new_channel, caller_profile);
  2577. switch_channel_set_caller_extension(new_channel, extension);
  2578. for (hi = orig_channel->variables->headers; hi; hi = hi->next) {
  2579. int ok = 1;
  2580. for (i = 0; i < argc; i++) {
  2581. if (!strcasecmp(argv[i], hi->name)) {
  2582. ok = 0;
  2583. break;
  2584. }
  2585. }
  2586. if (!ok)
  2587. continue;
  2588. switch_channel_set_variable(new_channel, hi->name, hi->value);
  2589. }
  2590. status = SWITCH_STATUS_SUCCESS;
  2591. }
  2592. switch_mutex_unlock(new_channel->profile_mutex);
  2593. switch_mutex_unlock(orig_channel->profile_mutex);
  2594. return status;
  2595. }
  2596. SWITCH_DECLARE(void) switch_channel_invert_cid(switch_channel_t *channel)
  2597. {
  2598. const char *tname, *tnum;
  2599. switch_caller_profile_t *cp;
  2600. cp = switch_channel_get_caller_profile(channel);
  2601. tname = cp->caller_id_name;
  2602. tnum = cp->caller_id_number;
  2603. #ifdef DEEP_DEBUG_CID
  2604. switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CRIT, "SWAP [%s][%s] [%s][%s]\n", cp->caller_id_name, cp->caller_id_number, cp->callee_id_name, cp->callee_id_number);
  2605. #endif
  2606. cp->caller_id_name = cp->callee_id_name;
  2607. cp->caller_id_number = cp->callee_id_number;
  2608. cp->callee_id_name = tname;
  2609. cp->callee_id_number = tnum;
  2610. if (zstr(cp->caller_id_name)) {
  2611. cp->caller_id_name = "Unknown";
  2612. }
  2613. if (zstr(cp->caller_id_number)) {
  2614. cp->caller_id_number = "Unknown";
  2615. }
  2616. }
  2617. SWITCH_DECLARE(void) switch_channel_flip_cid(switch_channel_t *channel)
  2618. {
  2619. switch_event_t *event;
  2620. const char *tmp = NULL;
  2621. switch_mutex_lock(channel->profile_mutex);
  2622. if (switch_channel_test_flag(channel, CF_RECOVERING) && switch_true(switch_channel_get_variable(channel, "channel_cid_flipped"))) {
  2623. switch_mutex_unlock(channel->profile_mutex);
  2624. return;
  2625. }
  2626. if (channel->caller_profile->callee_id_name) {
  2627. tmp = channel->caller_profile->caller_id_name;
  2628. switch_channel_set_variable(channel, "pre_transfer_caller_id_name", channel->caller_profile->caller_id_name);
  2629. channel->caller_profile->caller_id_name = switch_core_strdup(channel->caller_profile->pool, channel->caller_profile->callee_id_name);
  2630. }
  2631. if (switch_channel_test_flag(channel, CF_BRIDGED)) {
  2632. channel->caller_profile->callee_id_name = SWITCH_BLANK_STRING;
  2633. } else if (tmp) {
  2634. channel->caller_profile->callee_id_name = tmp;
  2635. }
  2636. if (channel->caller_profile->callee_id_number) {
  2637. tmp = channel->caller_profile->caller_id_number;
  2638. switch_channel_set_variable(channel, "pre_transfer_caller_id_number", channel->caller_profile->caller_id_number);
  2639. channel->caller_profile->caller_id_number = switch_core_strdup(channel->caller_profile->pool, channel->caller_profile->callee_id_number);
  2640. }
  2641. if (switch_channel_test_flag(channel, CF_BRIDGED)) {
  2642. channel->caller_profile->callee_id_number = SWITCH_BLANK_STRING;
  2643. } else if (tmp) {
  2644. channel->caller_profile->callee_id_number = tmp;
  2645. }
  2646. switch_channel_set_variable(channel, "channel_cid_flipped", "yes");
  2647. switch_mutex_unlock(channel->profile_mutex);
  2648. if (switch_event_create(&event, SWITCH_EVENT_CALL_UPDATE) == SWITCH_STATUS_SUCCESS) {
  2649. const char *uuid = switch_channel_get_partner_uuid(channel);
  2650. switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "Direction", "RECV");
  2651. if (uuid) {
  2652. switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "Bridged-To", uuid);
  2653. }
  2654. switch_channel_event_set_data(channel, event);
  2655. switch_event_fire(&event);
  2656. }
  2657. switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(channel->session), SWITCH_LOG_INFO, "%s Flipping CID from \"%s\" <%s> to \"%s\" <%s>\n",
  2658. switch_channel_get_name(channel),
  2659. switch_str_nil(switch_channel_get_variable(channel, "pre_transfer_caller_id_name")),
  2660. switch_str_nil(switch_channel_get_variable(channel, "pre_transfer_caller_id_number")),
  2661. channel->caller_profile->caller_id_name,
  2662. channel->caller_profile->caller_id_number
  2663. );
  2664. }
  2665. SWITCH_DECLARE(void) switch_channel_sort_cid(switch_channel_t *channel)
  2666. {
  2667. if (switch_channel_direction(channel) == SWITCH_CALL_DIRECTION_INBOUND && switch_channel_test_flag(channel, CF_BLEG)) {
  2668. switch_channel_flip_cid(channel);
  2669. switch_channel_clear_flag(channel, CF_BLEG);
  2670. } else if (switch_channel_direction(channel) == SWITCH_CALL_DIRECTION_OUTBOUND && !switch_channel_test_flag(channel, CF_DIALPLAN)) {
  2671. switch_channel_set_flag(channel, CF_DIALPLAN);
  2672. switch_channel_flip_cid(channel);
  2673. }
  2674. }
  2675. SWITCH_DECLARE(switch_caller_extension_t *) switch_channel_get_queued_extension(switch_channel_t *channel)
  2676. {
  2677. switch_caller_extension_t *caller_extension;
  2678. switch_mutex_lock(channel->profile_mutex);
  2679. caller_extension = channel->queued_extension;
  2680. channel->queued_extension = NULL;
  2681. switch_mutex_unlock(channel->profile_mutex);
  2682. return caller_extension;
  2683. }
  2684. SWITCH_DECLARE(void) switch_channel_transfer_to_extension(switch_channel_t *channel, switch_caller_extension_t *caller_extension)
  2685. {
  2686. switch_mutex_lock(channel->profile_mutex);
  2687. channel->queued_extension = caller_extension;
  2688. switch_mutex_unlock(channel->profile_mutex);
  2689. switch_channel_set_flag(channel, CF_TRANSFER);
  2690. switch_channel_set_state(channel, CS_ROUTING);
  2691. }
  2692. SWITCH_DECLARE(void) switch_channel_set_caller_extension(switch_channel_t *channel, switch_caller_extension_t *caller_extension)
  2693. {
  2694. switch_assert(channel != NULL);
  2695. switch_channel_sort_cid(channel);
  2696. switch_mutex_lock(channel->profile_mutex);
  2697. caller_extension->next = channel->caller_profile->caller_extension;
  2698. channel->caller_profile->caller_extension = caller_extension;
  2699. switch_mutex_unlock(channel->profile_mutex);
  2700. }
  2701. SWITCH_DECLARE(switch_caller_extension_t *) switch_channel_get_caller_extension(switch_channel_t *channel)
  2702. {
  2703. switch_caller_extension_t *extension = NULL;
  2704. switch_assert(channel != NULL);
  2705. switch_mutex_lock(channel->profile_mutex);
  2706. if (channel->caller_profile) {
  2707. extension = channel->caller_profile->caller_extension;
  2708. }
  2709. switch_mutex_unlock(channel->profile_mutex);
  2710. return extension;
  2711. }
  2712. SWITCH_DECLARE(void) switch_channel_set_bridge_time(switch_channel_t *channel)
  2713. {
  2714. switch_mutex_lock(channel->profile_mutex);
  2715. if (channel->caller_profile && channel->caller_profile->times) {
  2716. channel->caller_profile->times->bridged = switch_micro_time_now();
  2717. }
  2718. switch_mutex_unlock(channel->profile_mutex);
  2719. }
  2720. SWITCH_DECLARE(void) switch_channel_set_hangup_time(switch_channel_t *channel)
  2721. {
  2722. if (channel->caller_profile && channel->caller_profile->times && !channel->caller_profile->times->hungup) {
  2723. switch_mutex_lock(channel->profile_mutex);
  2724. channel->caller_profile->times->hungup = switch_micro_time_now();
  2725. switch_mutex_unlock(channel->profile_mutex);
  2726. }
  2727. }
  2728. SWITCH_DECLARE(switch_channel_state_t) switch_channel_perform_hangup(switch_channel_t *channel,
  2729. const char *file, const char *func, int line, switch_call_cause_t hangup_cause)
  2730. {
  2731. int ok = 0;
  2732. switch_assert(channel != NULL);
  2733. /* one per customer */
  2734. switch_mutex_lock(channel->state_mutex);
  2735. if (!(channel->opaque_flags & OCF_HANGUP)) {
  2736. channel->opaque_flags |= OCF_HANGUP;
  2737. ok = 1;
  2738. }
  2739. switch_mutex_unlock(channel->state_mutex);
  2740. if (switch_channel_test_flag(channel, CF_LEG_HOLDING)) {
  2741. switch_channel_mark_hold(channel, SWITCH_FALSE);
  2742. switch_channel_set_flag(channel, CF_HANGUP_HELD);
  2743. }
  2744. if (!ok) {
  2745. return channel->state;
  2746. }
  2747. switch_channel_clear_flag(channel, CF_BLOCK_STATE);
  2748. if (channel->state < CS_HANGUP) {
  2749. switch_channel_state_t last_state;
  2750. switch_event_t *event;
  2751. const char *var;
  2752. switch_mutex_lock(channel->profile_mutex);
  2753. if (channel->hold_record && !channel->hold_record->off) {
  2754. channel->hold_record->off = switch_time_now();
  2755. }
  2756. switch_mutex_unlock(channel->profile_mutex);
  2757. switch_mutex_lock(channel->state_mutex);
  2758. last_state = channel->state;
  2759. channel->state = CS_HANGUP;
  2760. switch_mutex_unlock(channel->state_mutex);
  2761. channel->hangup_cause = hangup_cause;
  2762. switch_log_printf(SWITCH_CHANNEL_ID_LOG, file, func, line, switch_channel_get_uuid(channel), SWITCH_LOG_NOTICE, "Hangup %s [%s] [%s]\n",
  2763. channel->name, state_names[last_state], switch_channel_cause2str(channel->hangup_cause));
  2764. switch_channel_set_variable_partner(channel, "last_bridge_hangup_cause", switch_channel_cause2str(hangup_cause));
  2765. if ((var = switch_channel_get_variable(channel, SWITCH_PROTO_SPECIFIC_HANGUP_CAUSE_VARIABLE))) {
  2766. switch_channel_set_variable_partner(channel, "last_bridge_" SWITCH_PROTO_SPECIFIC_HANGUP_CAUSE_VARIABLE, var);
  2767. }
  2768. if (switch_channel_test_flag(channel, CF_BRIDGE_ORIGINATOR)) {
  2769. switch_channel_set_variable(channel, "last_bridge_role", "originator");
  2770. } else if (switch_channel_test_flag(channel, CF_BRIDGED)) {
  2771. switch_channel_set_variable(channel, "last_bridge_role", "originatee");
  2772. }
  2773. if (!switch_core_session_running(channel->session) && !switch_core_session_started(channel->session)) {
  2774. switch_core_session_thread_launch(channel->session);
  2775. }
  2776. if (switch_event_create(&event, SWITCH_EVENT_CHANNEL_HANGUP) == SWITCH_STATUS_SUCCESS) {
  2777. switch_channel_event_set_data(channel, event);
  2778. switch_event_fire(&event);
  2779. }
  2780. switch_core_session_kill_channel(channel->session, SWITCH_SIG_KILL);
  2781. switch_core_session_signal_state_change(channel->session);
  2782. switch_core_session_hangup_state(channel->session, SWITCH_FALSE);
  2783. }
  2784. return channel->state;
  2785. }
  2786. static switch_status_t send_ind(switch_channel_t *channel, switch_core_session_message_types_t msg_id, const char *file, const char *func, int line)
  2787. {
  2788. switch_core_session_message_t msg = { 0 };
  2789. msg.message_id = msg_id;
  2790. msg.from = channel->name;
  2791. return switch_core_session_perform_receive_message(channel->session, &msg, file, func, line);
  2792. }
  2793. SWITCH_DECLARE(switch_status_t) switch_channel_perform_acknowledge_call(switch_channel_t *channel,
  2794. const char *file, const char *func, int line)
  2795. {
  2796. send_ind(channel, SWITCH_MESSAGE_INDICATE_ACKNOWLEDGE_CALL, file, func, line);
  2797. return SWITCH_STATUS_SUCCESS;
  2798. }
  2799. SWITCH_DECLARE(switch_status_t) switch_channel_perform_mark_ring_ready_value(switch_channel_t *channel,
  2800. switch_ring_ready_t rv,
  2801. const char *file, const char *func, int line)
  2802. {
  2803. switch_event_t *event;
  2804. if (!switch_channel_test_flag(channel, CF_RING_READY) && !switch_channel_test_flag(channel, CF_ANSWERED)) {
  2805. switch_log_printf(SWITCH_CHANNEL_ID_LOG, file, func, line, switch_channel_get_uuid(channel), SWITCH_LOG_NOTICE, "Ring-Ready %s!\n", channel->name);
  2806. switch_channel_set_flag_value(channel, CF_RING_READY, rv);
  2807. switch_mutex_lock(channel->profile_mutex);
  2808. if (channel->caller_profile && channel->caller_profile->times && !channel->caller_profile->times->progress) {
  2809. channel->caller_profile->times->progress = switch_micro_time_now();
  2810. if (channel->caller_profile->originator_caller_profile) {
  2811. switch_core_session_t *other_session;
  2812. if ((other_session = switch_core_session_locate(channel->caller_profile->originator_caller_profile->uuid))) {
  2813. switch_channel_t *other_channel;
  2814. other_channel = switch_core_session_get_channel(other_session);
  2815. switch_mutex_lock(other_channel->profile_mutex);
  2816. if (other_channel->caller_profile && !other_channel->caller_profile->times->progress) {
  2817. other_channel->caller_profile->times->progress = channel->caller_profile->times->progress;
  2818. }
  2819. switch_mutex_unlock(other_channel->profile_mutex);
  2820. switch_core_session_rwunlock(other_session);
  2821. }
  2822. channel->caller_profile->originator_caller_profile->times->progress = channel->caller_profile->times->progress;
  2823. }
  2824. }
  2825. switch_mutex_unlock(channel->profile_mutex);
  2826. if (switch_event_create(&event, SWITCH_EVENT_CHANNEL_PROGRESS) == SWITCH_STATUS_SUCCESS) {
  2827. switch_channel_event_set_data(channel, event);
  2828. switch_event_fire(&event);
  2829. }
  2830. switch_channel_execute_on(channel, SWITCH_CHANNEL_EXECUTE_ON_RING_VARIABLE);
  2831. switch_channel_api_on(channel, SWITCH_CHANNEL_API_ON_RING_VARIABLE);
  2832. switch_channel_set_callstate(channel, CCS_RINGING);
  2833. send_ind(channel, SWITCH_MESSAGE_RING_EVENT, file, func, line);
  2834. return SWITCH_STATUS_SUCCESS;
  2835. }
  2836. return SWITCH_STATUS_FALSE;
  2837. }
  2838. SWITCH_DECLARE(switch_status_t) switch_channel_perform_mark_pre_answered(switch_channel_t *channel, const char *file, const char *func, int line)
  2839. {
  2840. switch_event_t *event;
  2841. if (!switch_channel_test_flag(channel, CF_EARLY_MEDIA) && !switch_channel_test_flag(channel, CF_ANSWERED)) {
  2842. const char *uuid;
  2843. switch_core_session_t *other_session;
  2844. switch_core_media_check_dtls(channel->session, SWITCH_MEDIA_TYPE_AUDIO);
  2845. switch_log_printf(SWITCH_CHANNEL_ID_LOG, file, func, line, switch_channel_get_uuid(channel), SWITCH_LOG_NOTICE, "Pre-Answer %s!\n", channel->name);
  2846. switch_channel_set_flag(channel, CF_EARLY_MEDIA);
  2847. switch_channel_set_variable(channel, SWITCH_ENDPOINT_DISPOSITION_VARIABLE, "EARLY MEDIA");
  2848. if (switch_true(switch_channel_get_variable(channel, "video_mirror_input"))) {
  2849. switch_channel_set_flag(channel, CF_VIDEO_MIRROR_INPUT);
  2850. }
  2851. if (channel->caller_profile && channel->caller_profile->times) {
  2852. switch_mutex_lock(channel->profile_mutex);
  2853. channel->caller_profile->times->progress_media = switch_micro_time_now();
  2854. if (channel->caller_profile->originator_caller_profile) {
  2855. switch_core_session_t *osession;
  2856. if ((osession = switch_core_session_locate(channel->caller_profile->originator_caller_profile->uuid))) {
  2857. switch_channel_t *other_channel;
  2858. other_channel = switch_core_session_get_channel(osession);
  2859. if (other_channel->caller_profile) {
  2860. other_channel->caller_profile->times->progress_media = channel->caller_profile->times->progress_media;
  2861. }
  2862. switch_core_session_rwunlock(osession);
  2863. }
  2864. channel->caller_profile->originator_caller_profile->times->progress_media = channel->caller_profile->times->progress_media;
  2865. }
  2866. switch_mutex_unlock(channel->profile_mutex);
  2867. }
  2868. if (switch_event_create(&event, SWITCH_EVENT_CHANNEL_PROGRESS_MEDIA) == SWITCH_STATUS_SUCCESS) {
  2869. switch_channel_event_set_data(channel, event);
  2870. switch_event_fire(&event);
  2871. }
  2872. switch_channel_execute_on(channel, SWITCH_CHANNEL_EXECUTE_ON_PRE_ANSWER_VARIABLE);
  2873. switch_channel_execute_on(channel, SWITCH_CHANNEL_EXECUTE_ON_MEDIA_VARIABLE);
  2874. switch_channel_api_on(channel, SWITCH_CHANNEL_API_ON_PRE_ANSWER_VARIABLE);
  2875. switch_channel_api_on(channel, SWITCH_CHANNEL_API_ON_MEDIA_VARIABLE);
  2876. if (switch_true(switch_channel_get_variable(channel, SWITCH_PASSTHRU_PTIME_MISMATCH_VARIABLE))) {
  2877. switch_channel_set_flag(channel, CF_PASSTHRU_PTIME_MISMATCH);
  2878. }
  2879. /* if we're the child of another channel and the other channel is in a blocking read they will never realize we have answered so send
  2880. a SWITCH_SIG_BREAK to interrupt any blocking reads on that channel
  2881. */
  2882. if ((uuid = switch_channel_get_variable(channel, SWITCH_ORIGINATOR_VARIABLE))
  2883. && (other_session = switch_core_session_locate(uuid))) {
  2884. switch_core_session_kill_channel(other_session, SWITCH_SIG_BREAK);
  2885. switch_core_session_rwunlock(other_session);
  2886. }
  2887. switch_channel_set_callstate(channel, CCS_EARLY);
  2888. send_ind(channel, SWITCH_MESSAGE_PROGRESS_EVENT, file, func, line);
  2889. switch_core_media_check_autoadj(channel->session);
  2890. return SWITCH_STATUS_SUCCESS;
  2891. }
  2892. return SWITCH_STATUS_FALSE;
  2893. }
  2894. SWITCH_DECLARE(switch_status_t) switch_channel_perform_pre_answer(switch_channel_t *channel, const char *file, const char *func, int line)
  2895. {
  2896. switch_core_session_message_t msg = { 0 };
  2897. switch_status_t status = SWITCH_STATUS_SUCCESS;
  2898. switch_assert(channel != NULL);
  2899. if (channel->hangup_cause || channel->state >= CS_HANGUP) {
  2900. return SWITCH_STATUS_FALSE;
  2901. }
  2902. if (switch_channel_test_flag(channel, CF_ANSWERED)) {
  2903. return SWITCH_STATUS_SUCCESS;
  2904. }
  2905. if (switch_channel_test_flag(channel, CF_EARLY_MEDIA)) {
  2906. return SWITCH_STATUS_SUCCESS;
  2907. }
  2908. if (switch_channel_direction(channel) == SWITCH_CALL_DIRECTION_INBOUND) {
  2909. msg.message_id = SWITCH_MESSAGE_INDICATE_PROGRESS;
  2910. msg.from = channel->name;
  2911. status = switch_core_session_perform_receive_message(channel->session, &msg, file, func, line);
  2912. }
  2913. if (status == SWITCH_STATUS_SUCCESS) {
  2914. switch_channel_perform_mark_pre_answered(channel, file, func, line);
  2915. switch_channel_audio_sync(channel);
  2916. } else {
  2917. switch_channel_hangup(channel, SWITCH_CAUSE_INCOMPATIBLE_DESTINATION);
  2918. }
  2919. return status;
  2920. }
  2921. SWITCH_DECLARE(switch_status_t) switch_channel_perform_ring_ready_value(switch_channel_t *channel, switch_ring_ready_t rv,
  2922. const char *file, const char *func, int line)
  2923. {
  2924. switch_core_session_message_t msg = { 0 };
  2925. switch_status_t status = SWITCH_STATUS_SUCCESS;
  2926. switch_assert(channel != NULL);
  2927. if (channel->hangup_cause || channel->state >= CS_HANGUP) {
  2928. return SWITCH_STATUS_FALSE;
  2929. }
  2930. if (switch_channel_test_flag(channel, CF_ANSWERED)) {
  2931. return SWITCH_STATUS_SUCCESS;
  2932. }
  2933. if (switch_channel_test_flag(channel, CF_EARLY_MEDIA)) {
  2934. return SWITCH_STATUS_SUCCESS;
  2935. }
  2936. if (switch_channel_direction(channel) == SWITCH_CALL_DIRECTION_INBOUND) {
  2937. msg.message_id = SWITCH_MESSAGE_INDICATE_RINGING;
  2938. msg.from = channel->name;
  2939. msg.numeric_arg = rv;
  2940. status = switch_core_session_perform_receive_message(channel->session, &msg, file, func, line);
  2941. }
  2942. if (status == SWITCH_STATUS_SUCCESS) {
  2943. switch_log_printf(SWITCH_CHANNEL_ID_LOG, file, func, line, switch_channel_get_uuid(channel), SWITCH_LOG_NOTICE, "Ring Ready %s!\n", channel->name);
  2944. switch_channel_perform_mark_ring_ready_value(channel, rv, file, func, line);
  2945. } else {
  2946. switch_channel_hangup(channel, SWITCH_CAUSE_INCOMPATIBLE_DESTINATION);
  2947. }
  2948. return status;
  2949. }
  2950. static void do_api_on(switch_channel_t *channel, const char *variable)
  2951. {
  2952. char *app;
  2953. char *arg = NULL;
  2954. char *expanded = NULL;
  2955. switch_stream_handle_t stream = { 0 };
  2956. app = switch_core_session_strdup(channel->session, variable);
  2957. if ((arg = strchr(app, ' '))) {
  2958. *arg++ = '\0';
  2959. }
  2960. if (zstr(arg)) {
  2961. expanded = arg;
  2962. } else {
  2963. expanded = switch_channel_expand_variables(channel, arg);
  2964. }
  2965. SWITCH_STANDARD_STREAM(stream);
  2966. switch_api_execute(app, expanded, NULL, &stream);
  2967. switch_log_printf(SWITCH_CHANNEL_CHANNEL_LOG(channel), SWITCH_LOG_DEBUG, "%s process %s: %s(%s)\n%s\n",
  2968. channel->name, variable, app, switch_str_nil(expanded), (char *) stream.data);
  2969. if (expanded && expanded != arg) {
  2970. free(expanded);
  2971. }
  2972. free(stream.data);
  2973. }
  2974. SWITCH_DECLARE(switch_status_t) switch_channel_api_on(switch_channel_t *channel, const char *variable_prefix)
  2975. {
  2976. switch_event_header_t *hp;
  2977. switch_event_t *event;
  2978. int x = 0;
  2979. switch_channel_get_variables(channel, &event);
  2980. for (hp = event->headers; hp; hp = hp->next) {
  2981. char *var = hp->name;
  2982. char *val = hp->value;
  2983. if (!strncasecmp(var, variable_prefix, strlen(variable_prefix))) {
  2984. if (hp->idx) {
  2985. int i;
  2986. for (i = 0; i < hp->idx; i++) {
  2987. x++;
  2988. do_api_on(channel, hp->array[i]);
  2989. }
  2990. } else {
  2991. x++;
  2992. do_api_on(channel, val);
  2993. }
  2994. }
  2995. }
  2996. switch_event_destroy(&event);
  2997. return x ? SWITCH_STATUS_SUCCESS : SWITCH_STATUS_FALSE;
  2998. }
  2999. SWITCH_DECLARE(switch_status_t) switch_channel_execute_on_value(switch_channel_t *channel, const char *variable_value)
  3000. {
  3001. switch_status_t status;
  3002. char *arg = NULL;
  3003. char *p;
  3004. int bg = 0;
  3005. char *app;
  3006. char *expanded = NULL;
  3007. app = switch_core_session_strdup(channel->session, variable_value);
  3008. for(p = app; p && *p; p++) {
  3009. if (*p == ' ' || (*p == ':' && (*(p+1) != ':'))) {
  3010. *p++ = '\0';
  3011. arg = p;
  3012. break;
  3013. } else if (*p == ':' && (*(p+1) == ':')) {
  3014. bg++;
  3015. break;
  3016. }
  3017. }
  3018. switch_assert(app != NULL);
  3019. if (!strncasecmp(app, "perl", 4)) {
  3020. bg++;
  3021. }
  3022. if (zstr(arg)) {
  3023. expanded = arg;
  3024. } else {
  3025. expanded = switch_channel_expand_variables(channel, arg);
  3026. }
  3027. if (bg) {
  3028. status = switch_core_session_execute_application_async(channel->session, app, arg);
  3029. } else {
  3030. status = switch_core_session_execute_application(channel->session, app, arg);
  3031. }
  3032. if (expanded && expanded != arg) {
  3033. free(expanded);
  3034. }
  3035. return status;
  3036. }
  3037. SWITCH_DECLARE(switch_status_t) switch_channel_execute_on(switch_channel_t *channel, const char *variable_prefix)
  3038. {
  3039. switch_event_header_t *hp;
  3040. switch_event_t *event, *cevent;
  3041. int x = 0;
  3042. switch_core_get_variables(&event);
  3043. switch_channel_get_variables(channel, &cevent);
  3044. switch_event_merge(event, cevent);
  3045. for (hp = event->headers; hp; hp = hp->next) {
  3046. char *var = hp->name;
  3047. char *val = hp->value;
  3048. if (!strncasecmp(var, variable_prefix, strlen(variable_prefix))) {
  3049. if (hp->idx) {
  3050. int i;
  3051. for (i = 0; i < hp->idx; i++) {
  3052. x++;
  3053. switch_channel_execute_on_value(channel, hp->array[i]);
  3054. }
  3055. } else {
  3056. x++;
  3057. switch_channel_execute_on_value(channel, val);
  3058. }
  3059. }
  3060. }
  3061. switch_event_destroy(&event);
  3062. switch_event_destroy(&cevent);
  3063. return x ? SWITCH_STATUS_SUCCESS : SWITCH_STATUS_FALSE;
  3064. }
  3065. SWITCH_DECLARE(switch_status_t) switch_channel_perform_mark_answered(switch_channel_t *channel, const char *file, const char *func, int line)
  3066. {
  3067. switch_event_t *event;
  3068. const char *uuid;
  3069. switch_core_session_t *other_session;
  3070. const char *var;
  3071. switch_assert(channel != NULL);
  3072. if (channel->hangup_cause || channel->state >= CS_HANGUP) {
  3073. return SWITCH_STATUS_FALSE;
  3074. }
  3075. if (switch_channel_test_flag(channel, CF_ANSWERED)) {
  3076. return SWITCH_STATUS_SUCCESS;
  3077. }
  3078. switch_core_media_check_dtls(channel->session, SWITCH_MEDIA_TYPE_AUDIO);
  3079. if (channel->caller_profile && channel->caller_profile->times) {
  3080. switch_mutex_lock(channel->profile_mutex);
  3081. channel->caller_profile->times->answered = switch_micro_time_now();
  3082. switch_mutex_unlock(channel->profile_mutex);
  3083. }
  3084. switch_channel_set_flag(channel, CF_ANSWERED);
  3085. if (switch_true(switch_channel_get_variable(channel, "video_mirror_input"))) {
  3086. switch_channel_set_flag(channel, CF_VIDEO_MIRROR_INPUT);
  3087. //switch_channel_set_flag(channel, CF_VIDEO_DECODED_READ);
  3088. }
  3089. if (switch_event_create(&event, SWITCH_EVENT_CHANNEL_ANSWER) == SWITCH_STATUS_SUCCESS) {
  3090. switch_channel_event_set_data(channel, event);
  3091. switch_event_fire(&event);
  3092. }
  3093. /* if we're the child of another channel and the other channel is in a blocking read they will never realize we have answered so send
  3094. a SWITCH_SIG_BREAK to interrupt any blocking reads on that channel
  3095. */
  3096. if ((uuid = switch_channel_get_variable(channel, SWITCH_ORIGINATOR_VARIABLE))
  3097. && (other_session = switch_core_session_locate(uuid))) {
  3098. switch_core_session_kill_channel(other_session, SWITCH_SIG_BREAK);
  3099. switch_core_session_rwunlock(other_session);
  3100. }
  3101. if (switch_true(switch_channel_get_variable(channel, SWITCH_PASSTHRU_PTIME_MISMATCH_VARIABLE))) {
  3102. switch_channel_set_flag(channel, CF_PASSTHRU_PTIME_MISMATCH);
  3103. }
  3104. if ((var = switch_channel_get_variable(channel, SWITCH_ENABLE_HEARTBEAT_EVENTS_VARIABLE))) {
  3105. uint32_t seconds = 60;
  3106. int tmp;
  3107. if (switch_is_number(var)) {
  3108. tmp = atoi(var);
  3109. if (tmp > 0) {
  3110. seconds = tmp;
  3111. }
  3112. } else if (!switch_true(var)) {
  3113. seconds = 0;
  3114. }
  3115. if (seconds) {
  3116. switch_core_session_enable_heartbeat(channel->session, seconds);
  3117. }
  3118. }
  3119. switch_channel_set_variable(channel, SWITCH_ENDPOINT_DISPOSITION_VARIABLE, "ANSWER");
  3120. switch_log_printf(SWITCH_CHANNEL_ID_LOG, file, func, line, switch_channel_get_uuid(channel), SWITCH_LOG_NOTICE, "Channel [%s] has been answered\n",
  3121. channel->name);
  3122. if (switch_channel_get_variable(channel, "absolute_codec_string")) {
  3123. /* inherit_codec == true will implicitly clear the absolute_codec_string
  3124. variable if used since it was the reason it was set in the first place and is no longer needed */
  3125. if (switch_true(switch_channel_get_variable(channel, "inherit_codec"))) {
  3126. switch_channel_set_variable(channel, "absolute_codec_string", NULL);
  3127. }
  3128. }
  3129. switch_channel_execute_on(channel, SWITCH_CHANNEL_EXECUTE_ON_ANSWER_VARIABLE);
  3130. if (!switch_channel_test_flag(channel, CF_EARLY_MEDIA)) {
  3131. switch_channel_execute_on(channel, SWITCH_CHANNEL_EXECUTE_ON_MEDIA_VARIABLE);
  3132. switch_channel_api_on(channel, SWITCH_CHANNEL_API_ON_MEDIA_VARIABLE);
  3133. }
  3134. switch_channel_api_on(channel, SWITCH_CHANNEL_API_ON_ANSWER_VARIABLE);
  3135. switch_channel_presence(channel, "unknown", "answered", NULL);
  3136. //switch_channel_audio_sync(channel);
  3137. if (!switch_channel_test_flag(channel, CF_NO_RECOVER)) {
  3138. switch_core_recovery_track(channel->session);
  3139. }
  3140. switch_channel_set_callstate(channel, CCS_ACTIVE);
  3141. send_ind(channel, SWITCH_MESSAGE_ANSWER_EVENT, file, func, line);
  3142. switch_core_media_check_autoadj(channel->session);
  3143. if (switch_channel_test_flag(channel, CF_RTT)) {
  3144. switch_channel_set_flag_partner(channel, CF_RTT);
  3145. }
  3146. return SWITCH_STATUS_SUCCESS;
  3147. }
  3148. SWITCH_DECLARE(switch_status_t) switch_channel_perform_answer(switch_channel_t *channel, const char *file, const char *func, int line)
  3149. {
  3150. switch_core_session_message_t msg = { 0 };
  3151. switch_status_t status = SWITCH_STATUS_SUCCESS;
  3152. switch_assert(channel != NULL);
  3153. if (switch_channel_direction(channel) == SWITCH_CALL_DIRECTION_OUTBOUND) {
  3154. return SWITCH_STATUS_SUCCESS;
  3155. }
  3156. if (channel->hangup_cause || channel->state >= CS_HANGUP) {
  3157. return SWITCH_STATUS_FALSE;
  3158. }
  3159. if (switch_channel_test_flag(channel, CF_ANSWERED)) {
  3160. return SWITCH_STATUS_SUCCESS;
  3161. }
  3162. msg.message_id = SWITCH_MESSAGE_INDICATE_ANSWER;
  3163. msg.from = channel->name;
  3164. status = switch_core_session_perform_receive_message(channel->session, &msg, file, func, line);
  3165. if (status == SWITCH_STATUS_SUCCESS) {
  3166. switch_channel_perform_mark_answered(channel, file, func, line);
  3167. if (!switch_channel_test_flag(channel, CF_EARLY_MEDIA)) {
  3168. switch_channel_audio_sync(channel);
  3169. }
  3170. } else {
  3171. switch_channel_hangup(channel, SWITCH_CAUSE_INCOMPATIBLE_DESTINATION);
  3172. }
  3173. if (switch_core_session_in_thread(channel->session) && !switch_channel_test_flag(channel, CF_PROXY_MODE) &&
  3174. !switch_channel_test_flag(channel, CF_HAS_TEXT)) {
  3175. const char *delay;
  3176. if ((delay = switch_channel_get_variable(channel, "answer_delay"))) {
  3177. uint32_t msec = atoi(delay);
  3178. if (msec) {
  3179. switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(channel->session), SWITCH_LOG_DEBUG, "Answer delay for %u msec\n", msec);
  3180. switch_ivr_sleep(channel->session, msec, SWITCH_TRUE, NULL);
  3181. }
  3182. }
  3183. }
  3184. return status;
  3185. }
  3186. #define resize(l) {\
  3187. char *dp;\
  3188. olen += (len + l + block);\
  3189. cpos = c - data;\
  3190. if ((dp = realloc(data, olen))) {\
  3191. data = dp;\
  3192. c = data + cpos;\
  3193. memset(c, 0, olen - cpos);\
  3194. }} \
  3195. SWITCH_DECLARE(char *) switch_channel_expand_variables_check(switch_channel_t *channel, const char *in, switch_event_t *var_list, switch_event_t *api_list, uint32_t recur)
  3196. {
  3197. char *p, *c = NULL;
  3198. char *data, *indup, *endof_indup;
  3199. size_t sp = 0, len = 0, olen = 0, vtype = 0, br = 0, cpos, block = 128;
  3200. char *cloned_sub_val = NULL, *sub_val = NULL, *expanded_sub_val = NULL;
  3201. char *func_val = NULL, *sb = NULL;
  3202. int nv = 0;
  3203. if (recur > 100) {
  3204. return (char *) in;
  3205. }
  3206. if (zstr(in)) {
  3207. return (char *) in;
  3208. }
  3209. nv = switch_string_var_check_const(in) || switch_string_has_escaped_data(in);
  3210. if (!nv) {
  3211. return (char *) in;
  3212. }
  3213. nv = 0;
  3214. olen = strlen(in) + 1;
  3215. indup = strdup(in);
  3216. endof_indup = end_of_p(indup) + 1;
  3217. if ((data = malloc(olen))) {
  3218. memset(data, 0, olen);
  3219. c = data;
  3220. for (p = indup; p && p < endof_indup && *p; p++) {
  3221. int global = 0;
  3222. vtype = 0;
  3223. if (*p == '\\') {
  3224. if (*(p + 1) == '$') {
  3225. nv = 1;
  3226. p++;
  3227. if (*(p + 1) == '$') {
  3228. p++;
  3229. }
  3230. } else if (*(p + 1) == '\'') {
  3231. p++;
  3232. continue;
  3233. } else if (*(p + 1) == '\\') {
  3234. if (len + 1 >= olen) {
  3235. resize(1);
  3236. }
  3237. *c++ = *p++;
  3238. len++;
  3239. continue;
  3240. }
  3241. }
  3242. if (*p == '$' && !nv) {
  3243. if (*(p + 1) == '$') {
  3244. p++;
  3245. global++;
  3246. }
  3247. if (*(p + 1)) {
  3248. if (*(p + 1) == '{') {
  3249. vtype = global ? 3 : 1;
  3250. } else {
  3251. nv = 1;
  3252. }
  3253. } else {
  3254. nv = 1;
  3255. }
  3256. }
  3257. if (nv) {
  3258. if (len + 1 >= olen) {
  3259. resize(1);
  3260. }
  3261. *c++ = *p;
  3262. len++;
  3263. nv = 0;
  3264. continue;
  3265. }
  3266. if (vtype) {
  3267. char *s = p, *e, *vname, *vval = NULL;
  3268. size_t nlen;
  3269. s++;
  3270. if ((vtype == 1 || vtype == 3) && *s == '{') {
  3271. br = 1;
  3272. s++;
  3273. }
  3274. e = s;
  3275. vname = s;
  3276. while (*e) {
  3277. if (br == 1 && *e == '}') {
  3278. br = 0;
  3279. *e++ = '\0';
  3280. break;
  3281. }
  3282. if (br > 0) {
  3283. if (e != s && *e == '{') {
  3284. br++;
  3285. } else if (br > 1 && *e == '}') {
  3286. br--;
  3287. }
  3288. }
  3289. e++;
  3290. }
  3291. p = e > endof_indup ? endof_indup : e;
  3292. vval = NULL;
  3293. for(sb = vname; sb && *sb; sb++) {
  3294. if (*sb == ' ') {
  3295. vval = sb;
  3296. break;
  3297. } else if (*sb == '(') {
  3298. vval = sb;
  3299. br = 1;
  3300. break;
  3301. }
  3302. }
  3303. if (vval) {
  3304. e = vval - 1;
  3305. *vval++ = '\0';
  3306. while (*e == ' ') {
  3307. *e-- = '\0';
  3308. }
  3309. e = vval;
  3310. while (e && *e) {
  3311. if (*e == '(') {
  3312. br++;
  3313. } else if (br > 1 && *e == ')') {
  3314. br--;
  3315. } else if (br == 1 && *e == ')') {
  3316. *e = '\0';
  3317. break;
  3318. }
  3319. e++;
  3320. }
  3321. vtype = 2;
  3322. }
  3323. if (vtype == 1 || vtype == 3) {
  3324. char *expanded = NULL;
  3325. int offset = 0;
  3326. int ooffset = 0;
  3327. char *ptr;
  3328. int idx = -1;
  3329. if ((expanded = switch_channel_expand_variables_check(channel, (char *) vname, var_list, api_list, recur+1)) == vname) {
  3330. expanded = NULL;
  3331. } else {
  3332. vname = expanded;
  3333. }
  3334. if ((ptr = strchr(vname, ':'))) {
  3335. *ptr++ = '\0';
  3336. offset = atoi(ptr);
  3337. if ((ptr = strchr(ptr, ':'))) {
  3338. ptr++;
  3339. ooffset = atoi(ptr);
  3340. }
  3341. }
  3342. if ((ptr = strchr(vname, '[')) && strchr(ptr, ']')) {
  3343. *ptr++ = '\0';
  3344. idx = atoi(ptr);
  3345. }
  3346. if ((sub_val = (char *) switch_channel_get_variable_dup(channel, vname, SWITCH_TRUE, idx))) {
  3347. if (var_list && !switch_event_check_permission_list(var_list, vname)) {
  3348. sub_val = "<Variable Expansion Permission Denied>";
  3349. }
  3350. if ((expanded_sub_val = switch_channel_expand_variables_check(channel, sub_val, var_list, api_list, recur+1)) == sub_val) {
  3351. expanded_sub_val = NULL;
  3352. } else {
  3353. sub_val = expanded_sub_val;
  3354. }
  3355. if (offset || ooffset) {
  3356. cloned_sub_val = strdup(sub_val);
  3357. switch_assert(cloned_sub_val);
  3358. sub_val = cloned_sub_val;
  3359. }
  3360. if (offset >= 0) {
  3361. if ((size_t) offset > strlen(sub_val)) {
  3362. *sub_val = '\0';
  3363. } else {
  3364. sub_val += offset;
  3365. }
  3366. } else if ((size_t) abs(offset) <= strlen(sub_val)) {
  3367. sub_val = cloned_sub_val + (strlen(cloned_sub_val) + offset);
  3368. }
  3369. if (ooffset > 0 && (size_t) ooffset < strlen(sub_val)) {
  3370. if ((ptr = (char *) sub_val + ooffset)) {
  3371. *ptr = '\0';
  3372. }
  3373. }
  3374. }
  3375. switch_safe_free(expanded);
  3376. } else {
  3377. switch_stream_handle_t stream = { 0 };
  3378. char *expanded = NULL;
  3379. SWITCH_STANDARD_STREAM(stream);
  3380. if (stream.data) {
  3381. char *expanded_vname = NULL;
  3382. if ((expanded_vname = switch_channel_expand_variables_check(channel, (char *) vname, var_list, api_list, recur+1)) == vname) {
  3383. expanded_vname = NULL;
  3384. } else {
  3385. vname = expanded_vname;
  3386. }
  3387. if ((expanded = switch_channel_expand_variables_check(channel, vval, var_list, api_list, recur+1)) == vval) {
  3388. expanded = NULL;
  3389. } else {
  3390. vval = expanded;
  3391. }
  3392. if (!switch_core_test_flag(SCF_API_EXPANSION) || (api_list && !switch_event_check_permission_list(api_list, vname))) {
  3393. func_val = NULL;
  3394. sub_val = "<API Execute Permission Denied>";
  3395. free(stream.data);
  3396. } else {
  3397. if (switch_api_execute(vname, vval, channel->session, &stream) == SWITCH_STATUS_SUCCESS) {
  3398. func_val = stream.data;
  3399. sub_val = func_val;
  3400. } else {
  3401. free(stream.data);
  3402. }
  3403. }
  3404. switch_safe_free(expanded);
  3405. switch_safe_free(expanded_vname);
  3406. } else {
  3407. switch_log_printf(SWITCH_CHANNEL_CHANNEL_LOG(channel), SWITCH_LOG_CRIT, "Memory Error!\n");
  3408. free(data);
  3409. free(indup);
  3410. return (char *) in;
  3411. }
  3412. }
  3413. if ((nlen = sub_val ? strlen(sub_val) : 0)) {
  3414. if (len + nlen >= olen) {
  3415. resize(nlen);
  3416. }
  3417. len += nlen;
  3418. strcat(c, sub_val);
  3419. c += nlen;
  3420. }
  3421. switch_safe_free(func_val);
  3422. switch_safe_free(cloned_sub_val);
  3423. switch_safe_free(expanded_sub_val);
  3424. sub_val = NULL;
  3425. vname = NULL;
  3426. br = 0;
  3427. }
  3428. if (sp) {
  3429. if (len + 1 >= olen) {
  3430. resize(1);
  3431. }
  3432. *c++ = ' ';
  3433. sp = 0;
  3434. len++;
  3435. }
  3436. if (*p == '$') {
  3437. p--;
  3438. } else {
  3439. if (len + 1 >= olen) {
  3440. resize(1);
  3441. }
  3442. *c++ = *p;
  3443. len++;
  3444. }
  3445. }
  3446. }
  3447. free(indup);
  3448. return data;
  3449. }
  3450. SWITCH_DECLARE(char *) switch_channel_build_param_string(switch_channel_t *channel, switch_caller_profile_t *caller_profile, const char *prefix)
  3451. {
  3452. switch_stream_handle_t stream = { 0 };
  3453. switch_size_t encode_len = 1024, new_len = 0;
  3454. char *encode_buf = NULL;
  3455. const char *prof[13] = { 0 }, *prof_names[13] = {
  3456. 0};
  3457. char *e = NULL;
  3458. switch_event_header_t *hi;
  3459. uint32_t x = 0;
  3460. SWITCH_STANDARD_STREAM(stream);
  3461. if (prefix) {
  3462. stream.write_function(&stream, "%s&", prefix);
  3463. }
  3464. encode_buf = malloc(encode_len);
  3465. switch_assert(encode_buf);
  3466. if (!caller_profile) {
  3467. caller_profile = switch_channel_get_caller_profile(channel);
  3468. }
  3469. switch_assert(caller_profile != NULL);
  3470. prof[0] = caller_profile->context;
  3471. prof[1] = caller_profile->destination_number;
  3472. prof[2] = caller_profile->caller_id_name;
  3473. prof[3] = caller_profile->caller_id_number;
  3474. prof[4] = caller_profile->network_addr;
  3475. prof[5] = caller_profile->ani;
  3476. prof[6] = caller_profile->aniii;
  3477. prof[7] = caller_profile->rdnis;
  3478. prof[8] = caller_profile->source;
  3479. prof[9] = caller_profile->chan_name;
  3480. prof[10] = caller_profile->uuid;
  3481. prof[11] = caller_profile->transfer_source;
  3482. prof_names[0] = "context";
  3483. prof_names[1] = "destination_number";
  3484. prof_names[2] = "caller_id_name";
  3485. prof_names[3] = "caller_id_number";
  3486. prof_names[4] = "network_addr";
  3487. prof_names[5] = "ani";
  3488. prof_names[6] = "aniii";
  3489. prof_names[7] = "rdnis";
  3490. prof_names[8] = "source";
  3491. prof_names[9] = "chan_name";
  3492. prof_names[10] = "uuid";
  3493. prof_names[11] = "transfer_source";
  3494. for (x = 0; prof[x]; x++) {
  3495. if (zstr(prof[x])) {
  3496. continue;
  3497. }
  3498. new_len = (strlen(prof[x]) * 3) + 1;
  3499. if (encode_len < new_len) {
  3500. char *tmp;
  3501. encode_len = new_len;
  3502. if (!(tmp = realloc(encode_buf, encode_len))) {
  3503. abort();
  3504. }
  3505. encode_buf = tmp;
  3506. }
  3507. switch_url_encode(prof[x], encode_buf, encode_len);
  3508. stream.write_function(&stream, "%s=%s&", prof_names[x], encode_buf);
  3509. }
  3510. if (channel->caller_profile->soft) {
  3511. profile_node_t *pn;
  3512. for(pn = channel->caller_profile->soft; pn; pn = pn->next) {
  3513. char *var = pn->var;
  3514. char *val = pn->val;
  3515. new_len = (strlen((char *) var) * 3) + 1;
  3516. if (encode_len < new_len) {
  3517. char *tmp;
  3518. encode_len = new_len;
  3519. tmp = realloc(encode_buf, encode_len);
  3520. switch_assert(tmp);
  3521. encode_buf = tmp;
  3522. }
  3523. switch_url_encode((char *) val, encode_buf, encode_len);
  3524. stream.write_function(&stream, "%s=%s&", (char *) var, encode_buf);
  3525. }
  3526. }
  3527. if ((hi = switch_channel_variable_first(channel))) {
  3528. for (; hi; hi = hi->next) {
  3529. char *var = hi->name;
  3530. char *val = hi->value;
  3531. new_len = (strlen((char *) var) * 3) + 1;
  3532. if (encode_len < new_len) {
  3533. char *tmp;
  3534. encode_len = new_len;
  3535. tmp = realloc(encode_buf, encode_len);
  3536. switch_assert(tmp);
  3537. encode_buf = tmp;
  3538. }
  3539. switch_url_encode((char *) val, encode_buf, encode_len);
  3540. stream.write_function(&stream, "%s=%s&", (char *) var, encode_buf);
  3541. }
  3542. switch_channel_variable_last(channel);
  3543. }
  3544. e = (char *) stream.data + (strlen((char *) stream.data) - 1);
  3545. if (e && *e == '&') {
  3546. *e = '\0';
  3547. }
  3548. switch_safe_free(encode_buf);
  3549. return stream.data;
  3550. }
  3551. SWITCH_DECLARE(switch_status_t) switch_channel_pass_callee_id(switch_channel_t *channel, switch_channel_t *other_channel)
  3552. {
  3553. int x = 0;
  3554. switch_assert(channel);
  3555. switch_assert(other_channel);
  3556. switch_mutex_lock(channel->profile_mutex);
  3557. switch_mutex_lock(other_channel->profile_mutex);
  3558. if (!zstr(channel->caller_profile->callee_id_name)) {
  3559. other_channel->caller_profile->callee_id_name = switch_core_strdup(other_channel->caller_profile->pool, channel->caller_profile->callee_id_name);
  3560. x++;
  3561. }
  3562. if (!zstr(channel->caller_profile->callee_id_number)) {
  3563. other_channel->caller_profile->callee_id_number = switch_core_strdup(other_channel->caller_profile->pool, channel->caller_profile->callee_id_number);
  3564. x++;
  3565. }
  3566. switch_mutex_unlock(other_channel->profile_mutex);
  3567. switch_mutex_unlock(channel->profile_mutex);
  3568. return x ? SWITCH_STATUS_SUCCESS : SWITCH_STATUS_FALSE;
  3569. }
  3570. SWITCH_DECLARE(switch_status_t) switch_channel_get_variables(switch_channel_t *channel, switch_event_t **event)
  3571. {
  3572. switch_status_t status;
  3573. switch_mutex_lock(channel->profile_mutex);
  3574. if (channel->variables) {
  3575. status = switch_event_dup(event, channel->variables);
  3576. } else {
  3577. status = switch_event_create(event, SWITCH_EVENT_CHANNEL_DATA);
  3578. }
  3579. switch_mutex_unlock(channel->profile_mutex);
  3580. return status;
  3581. }
  3582. SWITCH_DECLARE(switch_status_t) switch_channel_get_variables_prefix(switch_channel_t *channel, const char *prefix, switch_event_t **event)
  3583. {
  3584. switch_status_t status = SWITCH_STATUS_SUCCESS;
  3585. switch_event_t *vars;
  3586. switch_event_create(&vars, SWITCH_EVENT_CHANNEL_DATA);
  3587. switch_mutex_lock(channel->profile_mutex);
  3588. if (channel->variables) {
  3589. switch_event_header_t *hi;
  3590. for (hi = channel->variables->headers; hi; hi = hi->next) {
  3591. if (!strncmp(hi->name, prefix, strlen(prefix))) {
  3592. switch_event_add_header_string(vars, SWITCH_STACK_BOTTOM, hi->name, hi->value);
  3593. }
  3594. }
  3595. }
  3596. switch_mutex_unlock(channel->profile_mutex);
  3597. *event = vars;
  3598. return status;
  3599. }
  3600. SWITCH_DECLARE(switch_core_session_t *) switch_channel_get_session(switch_channel_t *channel)
  3601. {
  3602. switch_assert(channel);
  3603. return channel->session;
  3604. }
  3605. SWITCH_DECLARE(switch_status_t) switch_channel_set_timestamps(switch_channel_t *channel)
  3606. {
  3607. switch_status_t status = SWITCH_STATUS_SUCCESS;
  3608. const char *cid_buf = NULL;
  3609. switch_caller_profile_t *caller_profile;
  3610. switch_app_log_t *app_log, *ap;
  3611. char *last_app = NULL, *last_arg = NULL;
  3612. char start[80] = "", resurrect[80] = "", answer[80] = "", hold[80],
  3613. bridge[80] = "", progress[80] = "", progress_media[80] = "", end[80] = "", tmp[80] = "",
  3614. profile_start[80] = "";
  3615. int32_t duration = 0, legbillsec = 0, billsec = 0, mduration = 0, billmsec = 0, legbillmsec = 0, progressmsec = 0, progress_mediamsec = 0;
  3616. int32_t answersec = 0, answermsec = 0, waitsec = 0, waitmsec = 0;
  3617. switch_time_t answerusec = 0;
  3618. switch_time_t uduration = 0, legbillusec = 0, billusec = 0, progresssec = 0, progressusec = 0, progress_mediasec = 0, progress_mediausec = 0, waitusec = 0;
  3619. time_t tt_created = 0, tt_answered = 0, tt_resurrected = 0, tt_bridged, tt_last_hold, tt_hold_accum,
  3620. tt_progress = 0, tt_progress_media = 0, tt_hungup = 0, mtt_created = 0, mtt_answered = 0, mtt_bridged = 0,
  3621. mtt_hungup = 0, tt_prof_created, mtt_progress = 0, mtt_progress_media = 0;
  3622. void *pop;
  3623. char dtstr[SWITCH_DTMF_LOG_LEN + 1] = "";
  3624. int x = 0;
  3625. switch_mutex_lock(channel->profile_mutex);
  3626. if (switch_channel_test_flag(channel, CF_TIMESTAMP_SET)) {
  3627. switch_mutex_unlock(channel->profile_mutex);
  3628. return SWITCH_STATUS_FALSE;
  3629. }
  3630. if (!(caller_profile = channel->caller_profile) || !channel->variables) {
  3631. switch_mutex_unlock(channel->profile_mutex);
  3632. return SWITCH_STATUS_FALSE;
  3633. }
  3634. switch_channel_set_flag(channel, CF_TIMESTAMP_SET);
  3635. if ((app_log = switch_core_session_get_app_log(channel->session))) {
  3636. for (ap = app_log; ap && ap->next; ap = ap->next);
  3637. last_app = ap->app;
  3638. last_arg = ap->arg;
  3639. }
  3640. if (!zstr(caller_profile->caller_id_name)) {
  3641. cid_buf = switch_core_session_sprintf(channel->session, "\"%s\" <%s>", caller_profile->caller_id_name,
  3642. switch_str_nil(caller_profile->caller_id_number));
  3643. } else {
  3644. cid_buf = caller_profile->caller_id_number;
  3645. }
  3646. while (x < SWITCH_DTMF_LOG_LEN && switch_queue_trypop(channel->dtmf_log_queue, &pop) == SWITCH_STATUS_SUCCESS) {
  3647. switch_dtmf_t *dt = (switch_dtmf_t *) pop;
  3648. if (dt) {
  3649. dtstr[x++] = dt->digit;
  3650. free(dt);
  3651. dt = NULL;
  3652. }
  3653. }
  3654. if (x) {
  3655. const char *var = switch_channel_get_variable(channel, "digits_dialed_filter");
  3656. char *digit_string = dtstr;
  3657. char *X = NULL;
  3658. switch_regex_t *re = NULL;
  3659. char *substituted = NULL;
  3660. if (!zstr(var)) {
  3661. int proceed = 0;
  3662. int ovector[30];
  3663. if ((proceed = switch_regex_perform(dtstr, var, &re, ovector, sizeof(ovector) / sizeof(ovector[0])))) {
  3664. int len = (strlen(dtstr) + strlen(var) + 10) * proceed;
  3665. int i = 0;
  3666. const char *replace = NULL;
  3667. X = malloc(len);
  3668. for (i = 0; i < proceed; i++) {
  3669. if (pcre_get_substring(dtstr, ovector, proceed, i, &replace) >= 0) {
  3670. if (replace) {
  3671. switch_size_t plen = strlen(replace);
  3672. memset(X, 'X', plen);
  3673. *(X+plen) = '\0';
  3674. switch_safe_free(substituted);
  3675. substituted = switch_string_replace(substituted ? substituted : dtstr, replace, X);
  3676. pcre_free_substring(replace);
  3677. }
  3678. }
  3679. }
  3680. if (!zstr(substituted)) {
  3681. digit_string = substituted;
  3682. }
  3683. }
  3684. }
  3685. switch_channel_set_variable(channel, "digits_dialed", digit_string);
  3686. switch_regex_safe_free(re);
  3687. switch_safe_free(substituted);
  3688. switch_safe_free(X);
  3689. } else {
  3690. switch_channel_set_variable(channel, "digits_dialed", "none");
  3691. }
  3692. if (caller_profile->times) {
  3693. switch_time_exp_t tm;
  3694. switch_size_t retsize;
  3695. const char *fmt = "%Y-%m-%d %T";
  3696. switch_time_exp_lt(&tm, caller_profile->times->created);
  3697. switch_strftime_nocheck(start, &retsize, sizeof(start), fmt, &tm);
  3698. switch_channel_set_variable(channel, "start_stamp", start);
  3699. switch_time_exp_lt(&tm, caller_profile->times->profile_created);
  3700. switch_strftime_nocheck(profile_start, &retsize, sizeof(profile_start), fmt, &tm);
  3701. switch_channel_set_variable(channel, "profile_start_stamp", profile_start);
  3702. if (caller_profile->times->answered) {
  3703. switch_time_exp_lt(&tm, caller_profile->times->answered);
  3704. switch_strftime_nocheck(answer, &retsize, sizeof(answer), fmt, &tm);
  3705. switch_channel_set_variable(channel, "answer_stamp", answer);
  3706. }
  3707. if (caller_profile->times->bridged) {
  3708. switch_time_exp_lt(&tm, caller_profile->times->bridged);
  3709. switch_strftime_nocheck(bridge, &retsize, sizeof(bridge), fmt, &tm);
  3710. switch_channel_set_variable(channel, "bridge_stamp", bridge);
  3711. }
  3712. if (caller_profile->times->last_hold) {
  3713. switch_time_exp_lt(&tm, caller_profile->times->last_hold);
  3714. switch_strftime_nocheck(hold, &retsize, sizeof(hold), fmt, &tm);
  3715. switch_channel_set_variable(channel, "hold_stamp", hold);
  3716. }
  3717. if (caller_profile->times->resurrected) {
  3718. switch_time_exp_lt(&tm, caller_profile->times->resurrected);
  3719. switch_strftime_nocheck(resurrect, &retsize, sizeof(resurrect), fmt, &tm);
  3720. switch_channel_set_variable(channel, "resurrect_stamp", resurrect);
  3721. }
  3722. if (caller_profile->times->progress) {
  3723. switch_time_exp_lt(&tm, caller_profile->times->progress);
  3724. switch_strftime_nocheck(progress, &retsize, sizeof(progress), fmt, &tm);
  3725. switch_channel_set_variable(channel, "progress_stamp", progress);
  3726. }
  3727. if (caller_profile->times->progress_media) {
  3728. switch_time_exp_lt(&tm, caller_profile->times->progress_media);
  3729. switch_strftime_nocheck(progress_media, &retsize, sizeof(progress_media), fmt, &tm);
  3730. switch_channel_set_variable(channel, "progress_media_stamp", progress_media);
  3731. }
  3732. if (channel->hold_record) {
  3733. switch_hold_record_t *hr;
  3734. switch_stream_handle_t stream = { 0 };
  3735. SWITCH_STANDARD_STREAM(stream);
  3736. stream.write_function(&stream, "{", SWITCH_VA_NONE);
  3737. for (hr = channel->hold_record; hr; hr = hr->next) {
  3738. stream.write_function(&stream, "{%"SWITCH_TIME_T_FMT",%"SWITCH_TIME_T_FMT"},", hr->on, hr->off);
  3739. }
  3740. end_of((char *)stream.data) = '}';
  3741. switch_channel_set_variable(channel, "hold_events", (char *)stream.data);
  3742. free(stream.data);
  3743. }
  3744. switch_time_exp_lt(&tm, caller_profile->times->hungup);
  3745. switch_strftime_nocheck(end, &retsize, sizeof(end), fmt, &tm);
  3746. switch_channel_set_variable(channel, "end_stamp", end);
  3747. tt_created = (time_t) (caller_profile->times->created / 1000000);
  3748. mtt_created = (time_t) (caller_profile->times->created / 1000);
  3749. switch_snprintf(tmp, sizeof(tmp), "%" TIME_T_FMT, tt_created);
  3750. switch_channel_set_variable(channel, "start_epoch", tmp);
  3751. switch_snprintf(tmp, sizeof(tmp), "%" SWITCH_TIME_T_FMT, caller_profile->times->created);
  3752. switch_channel_set_variable(channel, "start_uepoch", tmp);
  3753. tt_prof_created = (time_t) (caller_profile->times->profile_created / 1000000);
  3754. switch_snprintf(tmp, sizeof(tmp), "%" TIME_T_FMT, tt_prof_created);
  3755. switch_channel_set_variable(channel, "profile_start_epoch", tmp);
  3756. switch_snprintf(tmp, sizeof(tmp), "%" SWITCH_TIME_T_FMT, caller_profile->times->profile_created);
  3757. switch_channel_set_variable(channel, "profile_start_uepoch", tmp);
  3758. tt_answered = (time_t) (caller_profile->times->answered / 1000000);
  3759. mtt_answered = (time_t) (caller_profile->times->answered / 1000);
  3760. switch_snprintf(tmp, sizeof(tmp), "%" TIME_T_FMT, tt_answered);
  3761. switch_channel_set_variable(channel, "answer_epoch", tmp);
  3762. switch_snprintf(tmp, sizeof(tmp), "%" SWITCH_TIME_T_FMT, caller_profile->times->answered);
  3763. switch_channel_set_variable(channel, "answer_uepoch", tmp);
  3764. tt_bridged = (time_t) (caller_profile->times->bridged / 1000000);
  3765. mtt_bridged = (time_t) (caller_profile->times->bridged / 1000);
  3766. switch_snprintf(tmp, sizeof(tmp), "%" TIME_T_FMT, tt_bridged);
  3767. switch_channel_set_variable(channel, "bridge_epoch", tmp);
  3768. switch_snprintf(tmp, sizeof(tmp), "%" SWITCH_TIME_T_FMT, caller_profile->times->bridged);
  3769. switch_channel_set_variable(channel, "bridge_uepoch", tmp);
  3770. tt_last_hold = (time_t) (caller_profile->times->last_hold / 1000000);
  3771. switch_snprintf(tmp, sizeof(tmp), "%" TIME_T_FMT, tt_last_hold);
  3772. switch_channel_set_variable(channel, "last_hold_epoch", tmp);
  3773. switch_snprintf(tmp, sizeof(tmp), "%" SWITCH_TIME_T_FMT, caller_profile->times->last_hold);
  3774. switch_channel_set_variable(channel, "last_hold_uepoch", tmp);
  3775. tt_hold_accum = (time_t) (caller_profile->times->hold_accum / 1000000);
  3776. switch_snprintf(tmp, sizeof(tmp), "%" TIME_T_FMT, tt_hold_accum);
  3777. switch_channel_set_variable(channel, "hold_accum_seconds", tmp);
  3778. switch_snprintf(tmp, sizeof(tmp), "%" SWITCH_TIME_T_FMT, caller_profile->times->hold_accum);
  3779. switch_channel_set_variable(channel, "hold_accum_usec", tmp);
  3780. switch_snprintf(tmp, sizeof(tmp), "%" SWITCH_TIME_T_FMT, caller_profile->times->hold_accum / 1000);
  3781. switch_channel_set_variable(channel, "hold_accum_ms", tmp);
  3782. tt_resurrected = (time_t) (caller_profile->times->resurrected / 1000000);
  3783. switch_snprintf(tmp, sizeof(tmp), "%" TIME_T_FMT, tt_resurrected);
  3784. switch_channel_set_variable(channel, "resurrect_epoch", tmp);
  3785. switch_snprintf(tmp, sizeof(tmp), "%" SWITCH_TIME_T_FMT, caller_profile->times->resurrected);
  3786. switch_channel_set_variable(channel, "resurrect_uepoch", tmp);
  3787. tt_progress = (time_t) (caller_profile->times->progress / 1000000);
  3788. mtt_progress = (time_t) (caller_profile->times->progress / 1000);
  3789. switch_snprintf(tmp, sizeof(tmp), "%" TIME_T_FMT, tt_progress);
  3790. switch_channel_set_variable(channel, "progress_epoch", tmp);
  3791. switch_snprintf(tmp, sizeof(tmp), "%" SWITCH_TIME_T_FMT, caller_profile->times->progress);
  3792. switch_channel_set_variable(channel, "progress_uepoch", tmp);
  3793. tt_progress_media = (time_t) (caller_profile->times->progress_media / 1000000);
  3794. mtt_progress_media = (time_t) (caller_profile->times->progress_media / 1000);
  3795. switch_snprintf(tmp, sizeof(tmp), "%" TIME_T_FMT, tt_progress_media);
  3796. switch_channel_set_variable(channel, "progress_media_epoch", tmp);
  3797. switch_snprintf(tmp, sizeof(tmp), "%" SWITCH_TIME_T_FMT, caller_profile->times->progress_media);
  3798. switch_channel_set_variable(channel, "progress_media_uepoch", tmp);
  3799. tt_hungup = (time_t) (caller_profile->times->hungup / 1000000);
  3800. mtt_hungup = (time_t) (caller_profile->times->hungup / 1000);
  3801. switch_snprintf(tmp, sizeof(tmp), "%" TIME_T_FMT, tt_hungup);
  3802. switch_channel_set_variable(channel, "end_epoch", tmp);
  3803. switch_snprintf(tmp, sizeof(tmp), "%" SWITCH_TIME_T_FMT, caller_profile->times->hungup);
  3804. switch_channel_set_variable(channel, "end_uepoch", tmp);
  3805. duration = (int32_t) (tt_hungup - tt_created);
  3806. mduration = (int32_t) (mtt_hungup - mtt_created);
  3807. uduration = caller_profile->times->hungup - caller_profile->times->created;
  3808. if (caller_profile->times->bridged > caller_profile->times->created) {
  3809. waitsec = (int32_t) (tt_bridged - tt_created);
  3810. waitmsec = (int32_t) (mtt_bridged - mtt_created);
  3811. waitusec = caller_profile->times->bridged - caller_profile->times->created;
  3812. } else {
  3813. waitsec = 0;
  3814. waitmsec = 0;
  3815. waitusec = 0;
  3816. }
  3817. if (caller_profile->times->answered) {
  3818. billsec = (int32_t) (tt_hungup - tt_answered);
  3819. billmsec = (int32_t) (mtt_hungup - mtt_answered);
  3820. billusec = caller_profile->times->hungup - caller_profile->times->answered;
  3821. legbillsec = (int32_t) (tt_hungup - tt_created);
  3822. legbillmsec = (int32_t) (mtt_hungup - mtt_created);
  3823. legbillusec = caller_profile->times->hungup - caller_profile->times->created;
  3824. answersec = (int32_t) (tt_answered - tt_created);
  3825. answermsec = (int32_t) (mtt_answered - mtt_created);
  3826. answerusec = caller_profile->times->answered - caller_profile->times->created;
  3827. }
  3828. if (caller_profile->times->progress) {
  3829. progresssec = (int32_t) (tt_progress - tt_created);
  3830. progressmsec = (int32_t) (mtt_progress - mtt_created);
  3831. progressusec = caller_profile->times->progress - caller_profile->times->created;
  3832. }
  3833. if (caller_profile->times->progress_media) {
  3834. progress_mediasec = (int32_t) (tt_progress_media - tt_created);
  3835. progress_mediamsec = (int32_t) (mtt_progress_media - mtt_created);
  3836. progress_mediausec = caller_profile->times->progress_media - caller_profile->times->created;
  3837. }
  3838. }
  3839. switch_channel_set_variable(channel, "last_app", last_app);
  3840. switch_channel_set_variable(channel, "last_arg", last_arg);
  3841. switch_channel_set_variable(channel, "caller_id", cid_buf);
  3842. switch_snprintf(tmp, sizeof(tmp), "%d", duration);
  3843. switch_channel_set_variable(channel, "duration", tmp);
  3844. switch_snprintf(tmp, sizeof(tmp), "%d", billsec);
  3845. switch_channel_set_variable(channel, "billsec", tmp);
  3846. switch_snprintf(tmp, sizeof(tmp), "%"SWITCH_TIME_T_FMT, progresssec);
  3847. switch_channel_set_variable(channel, "progresssec", tmp);
  3848. switch_snprintf(tmp, sizeof(tmp), "%d", answersec);
  3849. switch_channel_set_variable(channel, "answersec", tmp);
  3850. switch_snprintf(tmp, sizeof(tmp), "%d", waitsec);
  3851. switch_channel_set_variable(channel, "waitsec", tmp);
  3852. switch_snprintf(tmp, sizeof(tmp), "%"SWITCH_TIME_T_FMT, progress_mediasec);
  3853. switch_channel_set_variable(channel, "progress_mediasec", tmp);
  3854. switch_snprintf(tmp, sizeof(tmp), "%d", legbillsec);
  3855. switch_channel_set_variable(channel, "flow_billsec", tmp);
  3856. switch_snprintf(tmp, sizeof(tmp), "%d", mduration);
  3857. switch_channel_set_variable(channel, "mduration", tmp);
  3858. switch_snprintf(tmp, sizeof(tmp), "%d", billmsec);
  3859. switch_channel_set_variable(channel, "billmsec", tmp);
  3860. switch_snprintf(tmp, sizeof(tmp), "%d", progressmsec);
  3861. switch_channel_set_variable(channel, "progressmsec", tmp);
  3862. switch_snprintf(tmp, sizeof(tmp), "%d", answermsec);
  3863. switch_channel_set_variable(channel, "answermsec", tmp);
  3864. switch_snprintf(tmp, sizeof(tmp), "%d", waitmsec);
  3865. switch_channel_set_variable(channel, "waitmsec", tmp);
  3866. switch_snprintf(tmp, sizeof(tmp), "%d", progress_mediamsec);
  3867. switch_channel_set_variable(channel, "progress_mediamsec", tmp);
  3868. switch_snprintf(tmp, sizeof(tmp), "%d", legbillmsec);
  3869. switch_channel_set_variable(channel, "flow_billmsec", tmp);
  3870. switch_snprintf(tmp, sizeof(tmp), "%" SWITCH_TIME_T_FMT, uduration);
  3871. switch_channel_set_variable(channel, "uduration", tmp);
  3872. switch_snprintf(tmp, sizeof(tmp), "%" SWITCH_TIME_T_FMT, billusec);
  3873. switch_channel_set_variable(channel, "billusec", tmp);
  3874. switch_snprintf(tmp, sizeof(tmp), "%" SWITCH_TIME_T_FMT, progressusec);
  3875. switch_channel_set_variable(channel, "progressusec", tmp);
  3876. switch_snprintf(tmp, sizeof(tmp), "%" SWITCH_TIME_T_FMT, answerusec);
  3877. switch_channel_set_variable(channel, "answerusec", tmp);
  3878. switch_snprintf(tmp, sizeof(tmp), "%" SWITCH_TIME_T_FMT, waitusec);
  3879. switch_channel_set_variable(channel, "waitusec", tmp);
  3880. switch_snprintf(tmp, sizeof(tmp), "%" SWITCH_TIME_T_FMT, progress_mediausec);
  3881. switch_channel_set_variable(channel, "progress_mediausec", tmp);
  3882. switch_snprintf(tmp, sizeof(tmp), "%" SWITCH_TIME_T_FMT, legbillusec);
  3883. switch_channel_set_variable(channel, "flow_billusec", tmp);
  3884. switch_mutex_unlock(channel->profile_mutex);
  3885. return status;
  3886. }
  3887. SWITCH_DECLARE(const char *) switch_channel_get_partner_uuid_copy(switch_channel_t *channel, char *buf, switch_size_t blen)
  3888. {
  3889. const char *uuid = NULL;
  3890. switch_mutex_lock(channel->profile_mutex);
  3891. if (!(uuid = switch_channel_get_variable_dup(channel, SWITCH_SIGNAL_BOND_VARIABLE, SWITCH_TRUE, -1))) {
  3892. uuid = switch_channel_get_variable_dup(channel, SWITCH_ORIGINATE_SIGNAL_BOND_VARIABLE, SWITCH_TRUE, -1);
  3893. }
  3894. if (uuid) {
  3895. strncpy(buf, uuid, blen);
  3896. uuid = (const char *) buf;
  3897. }
  3898. switch_mutex_unlock(channel->profile_mutex);
  3899. return uuid;
  3900. }
  3901. SWITCH_DECLARE(const char *) switch_channel_get_partner_uuid(switch_channel_t *channel)
  3902. {
  3903. const char *uuid = NULL;
  3904. if (!(uuid = switch_channel_get_variable(channel, SWITCH_SIGNAL_BOND_VARIABLE))) {
  3905. uuid = switch_channel_get_variable(channel, SWITCH_ORIGINATE_SIGNAL_BOND_VARIABLE);
  3906. }
  3907. return uuid;
  3908. }
  3909. SWITCH_DECLARE(void) switch_channel_handle_cause(switch_channel_t *channel, switch_call_cause_t cause)
  3910. {
  3911. switch_core_session_t *session = channel->session;
  3912. const char *transfer_on_fail = NULL;
  3913. char *tof_data = NULL;
  3914. char *tof_array[4] = { 0 };
  3915. //int tof_arrayc = 0;
  3916. if (!switch_channel_up_nosig(channel)) {
  3917. return;
  3918. }
  3919. transfer_on_fail = switch_channel_get_variable(channel, "transfer_on_fail");
  3920. tof_data = switch_core_session_strdup(session, transfer_on_fail);
  3921. switch_split(tof_data, ' ', tof_array);
  3922. transfer_on_fail = tof_array[0];
  3923. /*
  3924. if the variable continue_on_fail is set it can be:
  3925. 'true' to continue on all failures.
  3926. 'false' to not continue.
  3927. A list of codes either names or numbers eg "user_busy,normal_temporary_failure,603"
  3928. failure_causes acts as the opposite version
  3929. EXCEPTION... ATTENDED_TRANSFER never is a reason to continue.......
  3930. */
  3931. if (cause != SWITCH_CAUSE_ATTENDED_TRANSFER) {
  3932. const char *continue_on_fail = NULL, *failure_causes = NULL;
  3933. continue_on_fail = switch_channel_get_variable(channel, "continue_on_fail");
  3934. failure_causes = switch_channel_get_variable(channel, "failure_causes");
  3935. if (continue_on_fail || failure_causes) {
  3936. const char *cause_str;
  3937. char cause_num[35] = "";
  3938. cause_str = switch_channel_cause2str(cause);
  3939. switch_snprintf(cause_num, sizeof(cause_num), "%u", cause);
  3940. if (failure_causes) {
  3941. char *lbuf = switch_core_session_strdup(session, failure_causes);
  3942. char *argv[256] = { 0 };
  3943. int argc = switch_separate_string(lbuf, ',', argv, (sizeof(argv) / sizeof(argv[0])));
  3944. int i, x = 0;
  3945. for (i = 0; i < argc; i++) {
  3946. if (!strcasecmp(argv[i], cause_str) || !strcasecmp(argv[i], cause_num)) {
  3947. x++;
  3948. break;
  3949. }
  3950. }
  3951. if (!x) {
  3952. switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_DEBUG,
  3953. "Failure causes [%s]: Cause: %s\n", failure_causes, cause_str);
  3954. return;
  3955. }
  3956. }
  3957. if (continue_on_fail) {
  3958. if (switch_true(continue_on_fail)) {
  3959. return;
  3960. } else {
  3961. char *lbuf = switch_core_session_strdup(session, continue_on_fail);
  3962. char *argv[256] = { 0 };
  3963. int argc = switch_separate_string(lbuf, ',', argv, (sizeof(argv) / sizeof(argv[0])));
  3964. int i;
  3965. for (i = 0; i < argc; i++) {
  3966. if (!strcasecmp(argv[i], cause_str) || !strcasecmp(argv[i], cause_num)) {
  3967. switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_DEBUG,
  3968. "Continue on fail [%s]: Cause: %s\n", continue_on_fail, cause_str);
  3969. return;
  3970. }
  3971. }
  3972. }
  3973. }
  3974. } else {
  3975. /* no answer is *always* a reason to continue */
  3976. if (cause == SWITCH_CAUSE_NO_ANSWER || cause == SWITCH_CAUSE_NO_USER_RESPONSE || cause == SWITCH_CAUSE_ORIGINATOR_CANCEL) {
  3977. return;
  3978. }
  3979. }
  3980. if (transfer_on_fail || failure_causes) {
  3981. const char *cause_str;
  3982. char cause_num[35] = "";
  3983. cause_str = switch_channel_cause2str(cause);
  3984. switch_snprintf(cause_num, sizeof(cause_num), "%u", cause);
  3985. if ((tof_array[1] == NULL ) || (!strcasecmp(tof_array[1], "auto_cause"))){
  3986. tof_array[1] = (char *) cause_str;
  3987. }
  3988. if (failure_causes) {
  3989. char *lbuf = switch_core_session_strdup(session, failure_causes);
  3990. char *argv[256] = { 0 };
  3991. int argc = switch_separate_string(lbuf, ',', argv, (sizeof(argv) / sizeof(argv[0])));
  3992. int i, x = 0;
  3993. for (i = 0; i < argc; i++) {
  3994. if (!strcasecmp(argv[i], cause_str) || !strcasecmp(argv[i], cause_num)) {
  3995. x++;
  3996. break;
  3997. }
  3998. }
  3999. if (!x) {
  4000. switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_DEBUG,
  4001. "Failure causes [%s]: Cause: %s\n", failure_causes, cause_str);
  4002. switch_ivr_session_transfer(session, tof_array[1], tof_array[2], tof_array[3]);
  4003. }
  4004. }
  4005. if (transfer_on_fail) {
  4006. if (switch_true(transfer_on_fail)) {
  4007. return;
  4008. } else {
  4009. char *lbuf = switch_core_session_strdup(session, transfer_on_fail);
  4010. char *argv[256] = { 0 };
  4011. int argc = switch_separate_string(lbuf, ',', argv, (sizeof(argv) / sizeof(argv[0])));
  4012. int i;
  4013. for (i = 0; i < argc; i++) {
  4014. if (!strcasecmp(argv[i], cause_str) || !strcasecmp(argv[i], cause_num)) {
  4015. switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_DEBUG,
  4016. "Transfer on fail [%s]: Cause: %s\n", transfer_on_fail, cause_str);
  4017. switch_ivr_session_transfer(session, tof_array[1], tof_array[2], tof_array[3]);
  4018. }
  4019. }
  4020. }
  4021. }
  4022. }
  4023. }
  4024. if (!switch_channel_test_flag(channel, CF_TRANSFER) && !switch_channel_test_flag(channel, CF_CONFIRM_BLIND_TRANSFER) &&
  4025. switch_channel_get_state(channel) != CS_ROUTING) {
  4026. switch_channel_hangup(channel, cause);
  4027. }
  4028. }
  4029. SWITCH_DECLARE(void) switch_channel_global_init(switch_memory_pool_t *pool)
  4030. {
  4031. memset(&globals, 0, sizeof(globals));
  4032. globals.pool = pool;
  4033. switch_mutex_init(&globals.device_mutex, SWITCH_MUTEX_NESTED, pool);
  4034. switch_core_hash_init(&globals.device_hash);
  4035. }
  4036. SWITCH_DECLARE(void) switch_channel_global_uninit(void)
  4037. {
  4038. switch_core_hash_destroy(&globals.device_hash);
  4039. }
  4040. static void fetch_device_stats(switch_device_record_t *drec)
  4041. {
  4042. switch_device_node_t *np;
  4043. memset(&drec->stats, 0, sizeof(switch_device_stats_t));
  4044. switch_mutex_lock(drec->mutex);
  4045. for(np = drec->uuid_list; np; np = np->next) {
  4046. drec->stats.total++;
  4047. if (np->direction == SWITCH_CALL_DIRECTION_INBOUND) {
  4048. drec->stats.total_in++;
  4049. } else {
  4050. drec->stats.total_out++;
  4051. }
  4052. if (!np->hup_profile) {
  4053. drec->stats.offhook++;
  4054. if (np->direction == SWITCH_CALL_DIRECTION_INBOUND) {
  4055. drec->stats.offhook_in++;
  4056. } else {
  4057. drec->stats.offhook_out++;
  4058. }
  4059. if (np->callstate == CCS_HELD) {
  4060. drec->stats.held++;
  4061. if (np->direction == SWITCH_CALL_DIRECTION_INBOUND) {
  4062. drec->stats.held_in++;
  4063. } else {
  4064. drec->stats.held_out++;
  4065. }
  4066. } else if (np->callstate == CCS_UNHELD) {
  4067. drec->stats.unheld++;
  4068. if (np->direction == SWITCH_CALL_DIRECTION_INBOUND) {
  4069. drec->stats.unheld_in++;
  4070. } else {
  4071. drec->stats.unheld_out++;
  4072. }
  4073. } else {
  4074. if (np->callstate == CCS_EARLY) {
  4075. drec->stats.early++;
  4076. if (np->direction == SWITCH_CALL_DIRECTION_INBOUND) {
  4077. drec->stats.early_in++;
  4078. } else {
  4079. drec->stats.early_out++;
  4080. }
  4081. } else if (np->callstate == CCS_RINGING) {
  4082. drec->stats.ringing++;
  4083. if (np->direction == SWITCH_CALL_DIRECTION_INBOUND) {
  4084. drec->stats.ringing_in++;
  4085. } else {
  4086. drec->stats.ringing_out++;
  4087. }
  4088. } else if (np->callstate == CCS_RING_WAIT) {
  4089. drec->stats.ring_wait++;
  4090. } else if (np->callstate == CCS_HANGUP) {
  4091. drec->stats.hup++;
  4092. if (np->direction == SWITCH_CALL_DIRECTION_INBOUND) {
  4093. drec->stats.hup_in++;
  4094. } else {
  4095. drec->stats.hup_out++;
  4096. }
  4097. } else if (np->callstate != CCS_DOWN) {
  4098. drec->stats.active++;
  4099. if (np->direction == SWITCH_CALL_DIRECTION_INBOUND) {
  4100. drec->stats.active_in++;
  4101. } else {
  4102. drec->stats.active_out++;
  4103. }
  4104. }
  4105. }
  4106. } else {
  4107. drec->stats.hup++;
  4108. if (np->direction == SWITCH_CALL_DIRECTION_INBOUND) {
  4109. drec->stats.hup_in++;
  4110. } else {
  4111. drec->stats.hup_out++;
  4112. }
  4113. }
  4114. }
  4115. switch_mutex_unlock(drec->mutex);
  4116. }
  4117. SWITCH_DECLARE(void) switch_channel_clear_device_record(switch_channel_t *channel)
  4118. {
  4119. switch_memory_pool_t *pool;
  4120. int sanity = 100;
  4121. switch_device_node_t *np;
  4122. switch_event_t *event;
  4123. if (!channel->device_node || !switch_channel_test_flag(channel, CF_FINAL_DEVICE_LEG)) {
  4124. return;
  4125. }
  4126. while(--sanity && channel->device_node->parent->refs) {
  4127. switch_yield(100000);
  4128. }
  4129. switch_log_printf(SWITCH_CHANNEL_CHANNEL_LOG(channel), SWITCH_LOG_DEBUG, "Destroying device cdr %s on device [%s]\n",
  4130. channel->device_node->parent->uuid,
  4131. channel->device_node->parent->device_id);
  4132. if (switch_event_create(&event, SWITCH_EVENT_CALL_DETAIL) == SWITCH_STATUS_SUCCESS) {
  4133. int x = 0;
  4134. char prefix[80] = "";
  4135. switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "Type", "device");
  4136. switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "Device-ID", channel->device_node->parent->device_id);
  4137. switch_mutex_lock(channel->device_node->parent->mutex);
  4138. for(np = channel->device_node->parent->uuid_list; np; np = np->next) {
  4139. switch_snprintf(prefix, sizeof(prefix), "Call-%d", ++x);
  4140. switch_caller_profile_event_set_data(np->hup_profile, prefix, event);
  4141. }
  4142. switch_mutex_unlock(channel->device_node->parent->mutex);
  4143. switch_event_fire(&event);
  4144. }
  4145. switch_mutex_lock(channel->device_node->parent->mutex);
  4146. for(np = channel->device_node->parent->uuid_list; np; np = np->next) {
  4147. if (np->xml_cdr) {
  4148. switch_xml_free(np->xml_cdr);
  4149. }
  4150. if (np->event) {
  4151. switch_event_destroy(&np->event);
  4152. }
  4153. }
  4154. switch_mutex_unlock(channel->device_node->parent->mutex);
  4155. pool = channel->device_node->parent->pool;
  4156. switch_mutex_lock(globals.device_mutex);
  4157. switch_core_destroy_memory_pool(&pool);
  4158. switch_mutex_unlock(globals.device_mutex);
  4159. }
  4160. SWITCH_DECLARE(void) switch_channel_process_device_hangup(switch_channel_t *channel)
  4161. {
  4162. switch_channel_check_device_state(channel, channel->callstate);
  4163. process_device_hup(channel);
  4164. }
  4165. static void process_device_hup(switch_channel_t *channel)
  4166. {
  4167. switch_hold_record_t *hr, *newhr, *last = NULL;
  4168. switch_device_record_t *drec = NULL;
  4169. switch_device_node_t *node;
  4170. if (!channel->device_node) {
  4171. return;
  4172. }
  4173. switch_mutex_lock(globals.device_mutex);
  4174. node = channel->device_node;
  4175. drec = channel->device_node->parent;
  4176. node->hup_profile = switch_caller_profile_dup(drec->pool, channel->caller_profile);
  4177. fetch_device_stats(drec);
  4178. switch_ivr_generate_xml_cdr(channel->session, &node->xml_cdr);
  4179. if (switch_event_create(&node->event, SWITCH_EVENT_CALL_DETAIL) == SWITCH_STATUS_SUCCESS) {
  4180. switch_channel_event_set_extended_data(channel, node->event);
  4181. }
  4182. for (hr = channel->hold_record; hr; hr = hr->next) {
  4183. newhr = switch_core_alloc(drec->pool, sizeof(*newhr));
  4184. newhr->on = hr->on;
  4185. newhr->off = hr->off;
  4186. if (hr->uuid) {
  4187. newhr->uuid = switch_core_strdup(drec->pool, hr->uuid);
  4188. }
  4189. if (!node->hold_record) {
  4190. node->hold_record = newhr;
  4191. } else if (last) {
  4192. last->next = newhr;
  4193. }
  4194. last = newhr;
  4195. }
  4196. if (!drec->stats.offhook) { /* this is final call */
  4197. switch_core_hash_delete(globals.device_hash, drec->device_id);
  4198. switch_log_printf(SWITCH_CHANNEL_CHANNEL_LOG(channel), SWITCH_LOG_DEBUG, "Processing last call from device [%s]\n",
  4199. drec->device_id);
  4200. switch_channel_set_flag(channel, CF_FINAL_DEVICE_LEG);
  4201. } else {
  4202. channel->device_node = NULL;
  4203. }
  4204. drec->refs--;
  4205. switch_mutex_unlock(globals.device_mutex);
  4206. }
  4207. static void switch_channel_check_device_state(switch_channel_t *channel, switch_channel_callstate_t callstate)
  4208. {
  4209. switch_device_record_t *drec = NULL;
  4210. switch_device_state_binding_t *ptr = NULL;
  4211. switch_event_t *event = NULL;
  4212. if (!channel->device_node) {
  4213. return;
  4214. }
  4215. drec = channel->device_node->parent;
  4216. switch_mutex_lock(globals.device_mutex);
  4217. switch_mutex_lock(drec->mutex);
  4218. fetch_device_stats(drec);
  4219. if (drec->state != SDS_HANGUP) {
  4220. if (drec->stats.offhook == 0 || drec->stats.hup == drec->stats.total) {
  4221. drec->state = SDS_HANGUP;
  4222. } else {
  4223. if (drec->stats.active == 0) {
  4224. if ((drec->stats.ringing_out + drec->stats.early_out) > 0 || drec->stats.ring_wait > 0) {
  4225. drec->state = SDS_RINGING;
  4226. } else {
  4227. if (drec->stats.held > 0) {
  4228. drec->state = SDS_HELD;
  4229. } else if (drec->stats.unheld > 0) {
  4230. drec->state = SDS_UNHELD;
  4231. } else {
  4232. drec->state = SDS_DOWN;
  4233. }
  4234. }
  4235. } else if (drec->stats.active == 1) {
  4236. drec->state = SDS_ACTIVE;
  4237. } else {
  4238. drec->state = SDS_ACTIVE_MULTI;
  4239. }
  4240. }
  4241. }
  4242. if ((drec->state == SDS_DOWN && drec->last_state == SDS_DOWN) || (drec->state == SDS_HANGUP && drec->last_state == SDS_HANGUP)) {
  4243. switch_mutex_unlock(drec->mutex);
  4244. switch_mutex_unlock(globals.device_mutex);
  4245. return;
  4246. }
  4247. if (!drec->call_start) {
  4248. drec->call_start = switch_micro_time_now();
  4249. }
  4250. switch(drec->state) {
  4251. case SDS_RINGING:
  4252. if (!drec->ring_start) {
  4253. drec->ring_start = switch_micro_time_now();
  4254. drec->ring_stop = 0;
  4255. }
  4256. break;
  4257. case SDS_ACTIVE:
  4258. case SDS_ACTIVE_MULTI:
  4259. if (!drec->active_start) {
  4260. drec->active_start = switch_micro_time_now();
  4261. drec->active_stop = 0;
  4262. }
  4263. break;
  4264. case SDS_HELD:
  4265. if (!drec->hold_start) {
  4266. drec->hold_start = switch_micro_time_now();
  4267. drec->hold_stop = 0;
  4268. }
  4269. break;
  4270. default:
  4271. break;
  4272. }
  4273. if (callstate != CCS_UNHELD && drec->active_start && drec->state != SDS_ACTIVE && drec->state != SDS_ACTIVE_MULTI) {
  4274. drec->active_stop = switch_micro_time_now();
  4275. }
  4276. if (drec->ring_start && !drec->ring_stop && drec->state != SDS_RINGING) {
  4277. drec->ring_stop = switch_micro_time_now();
  4278. }
  4279. if (drec->hold_start && !drec->hold_stop && drec->state != SDS_HELD) {
  4280. drec->hold_stop = switch_micro_time_now();
  4281. }
  4282. if (switch_event_create(&event, SWITCH_EVENT_DEVICE_STATE) == SWITCH_STATUS_SUCCESS) {
  4283. switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "Device-ID", drec->device_id);
  4284. switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "Last-Device-State", switch_channel_device_state2str(drec->last_state));
  4285. switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "Device-State", switch_channel_device_state2str(drec->state));
  4286. switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "Device-Call-State", switch_channel_callstate2str(callstate));
  4287. switch_event_add_header(event, SWITCH_STACK_BOTTOM, "Device-Total-Legs", "%u", drec->stats.total);
  4288. switch_event_add_header(event, SWITCH_STACK_BOTTOM, "Device-Legs-Offhook", "%u", drec->stats.offhook);
  4289. switch_event_add_header(event, SWITCH_STACK_BOTTOM, "Device-Legs-Ringing", "%u", drec->stats.ringing);
  4290. switch_event_add_header(event, SWITCH_STACK_BOTTOM, "Device-Legs-Early", "%u", drec->stats.early);
  4291. switch_event_add_header(event, SWITCH_STACK_BOTTOM, "Device-Legs-Active", "%u", drec->stats.active);
  4292. switch_event_add_header(event, SWITCH_STACK_BOTTOM, "Device-Legs-Held", "%u", drec->stats.held);
  4293. switch_event_add_header(event, SWITCH_STACK_BOTTOM, "Device-Legs-UnHeld", "%u", drec->stats.unheld);
  4294. switch_event_add_header(event, SWITCH_STACK_BOTTOM, "Device-Legs-Hup", "%u", drec->stats.hup);
  4295. switch_event_add_header(event, SWITCH_STACK_BOTTOM, "Device-Talk-Time-Start-Uepoch", "%"SWITCH_TIME_T_FMT, drec->active_start);
  4296. if (drec->active_stop) {
  4297. switch_event_add_header(event, SWITCH_STACK_BOTTOM, "Device-Talk-Time-Stop-Uepoch", "%"SWITCH_TIME_T_FMT, drec->active_stop);
  4298. switch_event_add_header(event, SWITCH_STACK_BOTTOM, "Device-Talk-Time-Milliseconds", "%u", (uint32_t)(drec->active_stop - drec->active_start) / 1000);
  4299. }
  4300. }
  4301. switch_log_printf(SWITCH_CHANNEL_CHANNEL_LOG(channel), SWITCH_LOG_DEBUG1,
  4302. "%s device: %s\nState: %s Dev State: %s/%s Total:%u Offhook:%u "
  4303. "Ringing:%u Early:%u Active:%u Held:%u Unheld:%u Hungup:%u Dur: %u Ringtime: %u Holdtime: %u %s\n",
  4304. switch_channel_get_name(channel),
  4305. drec->device_id,
  4306. switch_channel_callstate2str(callstate),
  4307. switch_channel_device_state2str(drec->last_state),
  4308. switch_channel_device_state2str(drec->state),
  4309. drec->stats.total,
  4310. drec->stats.offhook,
  4311. drec->stats.ringing,
  4312. drec->stats.early,
  4313. drec->stats.active,
  4314. drec->stats.held,
  4315. drec->stats.unheld,
  4316. drec->stats.hup,
  4317. drec->active_stop ? (uint32_t)(drec->active_stop - drec->active_start) / 1000 : 0,
  4318. drec->ring_stop ? (uint32_t)(drec->ring_stop - drec->ring_start) / 1000 : 0,
  4319. drec->hold_stop ? (uint32_t)(drec->hold_stop - drec->hold_start) / 1000 : 0,
  4320. switch_channel_test_flag(channel, CF_FINAL_DEVICE_LEG) ? "FINAL LEG" : "");
  4321. for (ptr = globals.device_bindings; ptr; ptr = ptr->next) {
  4322. ptr->function(channel->session, callstate, drec);
  4323. }
  4324. drec->last_stats = drec->stats;
  4325. if (drec->active_stop) {
  4326. drec->active_start = drec->active_stop = 0;
  4327. if (drec->state == SDS_ACTIVE || drec->state == SDS_ACTIVE_MULTI) {
  4328. drec->active_start = switch_micro_time_now();
  4329. }
  4330. }
  4331. if (drec->hold_stop) {
  4332. drec->hold_start = drec->hold_stop = 0;
  4333. if (drec->state == SDS_HELD) {
  4334. drec->hold_start = switch_micro_time_now();
  4335. }
  4336. }
  4337. if (drec->ring_stop) {
  4338. drec->ring_start = drec->ring_stop = 0;
  4339. if (drec->state == SDS_RINGING) {
  4340. drec->ring_start = switch_micro_time_now();
  4341. }
  4342. }
  4343. drec->last_call_time = switch_micro_time_now();
  4344. drec->last_state = drec->state;
  4345. switch_mutex_unlock(drec->mutex);
  4346. switch_mutex_unlock(globals.device_mutex);
  4347. if (event) {
  4348. switch_event_fire(&event);
  4349. }
  4350. }
  4351. /* assumed to be called under a lock */
  4352. static void add_uuid(switch_device_record_t *drec, switch_channel_t *channel)
  4353. {
  4354. switch_device_node_t *node;
  4355. switch_assert(drec);
  4356. switch_channel_set_flag(channel, CF_DEVICE_LEG);
  4357. node = switch_core_alloc(drec->pool, sizeof(*node));
  4358. node->uuid = switch_core_strdup(drec->pool, switch_core_session_get_uuid(channel->session));
  4359. node->parent = drec;
  4360. node->callstate = channel->callstate;
  4361. node->direction = channel->logical_direction == SWITCH_CALL_DIRECTION_INBOUND ? SWITCH_CALL_DIRECTION_OUTBOUND : SWITCH_CALL_DIRECTION_INBOUND;
  4362. channel->device_node = node;
  4363. if (!drec->uuid_list) {
  4364. drec->uuid_list = node;
  4365. drec->uuid = node->uuid;
  4366. } else {
  4367. drec->uuid_tail->next = node;
  4368. }
  4369. drec->uuid_tail = node;
  4370. drec->refs++;
  4371. }
  4372. static switch_status_t create_device_record(switch_device_record_t **drecp, const char *device_id)
  4373. {
  4374. switch_device_record_t *drec;
  4375. switch_memory_pool_t *pool;
  4376. switch_assert(drecp);
  4377. switch_core_new_memory_pool(&pool);
  4378. drec = switch_core_alloc(pool, sizeof(*drec));
  4379. drec->pool = pool;
  4380. drec->device_id = switch_core_strdup(drec->pool, device_id);
  4381. switch_mutex_init(&drec->mutex, SWITCH_MUTEX_NESTED, drec->pool);
  4382. *drecp = drec;
  4383. return SWITCH_STATUS_SUCCESS;
  4384. }
  4385. SWITCH_DECLARE(const char *) switch_channel_set_device_id(switch_channel_t *channel, const char *device_id)
  4386. {
  4387. switch_device_record_t *drec;
  4388. if (channel->device_node) {
  4389. return NULL;
  4390. }
  4391. channel->device_id = switch_core_session_strdup(channel->session, device_id);
  4392. switch_mutex_lock(globals.device_mutex);
  4393. if (!(drec = switch_core_hash_find(globals.device_hash, channel->device_id))) {
  4394. create_device_record(&drec, channel->device_id);
  4395. switch_core_hash_insert(globals.device_hash, drec->device_id, drec);
  4396. }
  4397. add_uuid(drec, channel);
  4398. switch_mutex_unlock(globals.device_mutex);
  4399. switch_log_printf(SWITCH_CHANNEL_CHANNEL_LOG(channel), SWITCH_LOG_DEBUG, "Setting DEVICE ID to [%s]\n", device_id);
  4400. switch_channel_check_device_state(channel, channel->callstate);
  4401. return device_id;
  4402. }
  4403. SWITCH_DECLARE(switch_device_record_t *) switch_channel_get_device_record(switch_channel_t *channel)
  4404. {
  4405. if (channel->device_node) {
  4406. switch_mutex_lock(channel->device_node->parent->mutex);
  4407. return channel->device_node->parent;
  4408. }
  4409. return NULL;
  4410. }
  4411. SWITCH_DECLARE(void) switch_channel_release_device_record(switch_device_record_t **drecp)
  4412. {
  4413. if (drecp && *drecp) {
  4414. switch_mutex_unlock((*drecp)->mutex);
  4415. *drecp = NULL;
  4416. }
  4417. }
  4418. SWITCH_DECLARE(switch_status_t) switch_channel_bind_device_state_handler(switch_device_state_function_t function, void *user_data)
  4419. {
  4420. switch_device_state_binding_t *binding = NULL, *ptr = NULL;
  4421. assert(function != NULL);
  4422. if (!(binding = (switch_device_state_binding_t *) switch_core_alloc(globals.pool, sizeof(*binding)))) {
  4423. return SWITCH_STATUS_MEMERR;
  4424. }
  4425. binding->function = function;
  4426. binding->user_data = user_data;
  4427. switch_mutex_lock(globals.device_mutex);
  4428. for (ptr = globals.device_bindings; ptr && ptr->next; ptr = ptr->next);
  4429. if (ptr) {
  4430. ptr->next = binding;
  4431. } else {
  4432. globals.device_bindings = binding;
  4433. }
  4434. switch_mutex_unlock(globals.device_mutex);
  4435. return SWITCH_STATUS_SUCCESS;
  4436. }
  4437. SWITCH_DECLARE(switch_status_t) switch_channel_unbind_device_state_handler(switch_device_state_function_t function)
  4438. {
  4439. switch_device_state_binding_t *ptr, *last = NULL;
  4440. switch_status_t status = SWITCH_STATUS_FALSE;
  4441. switch_mutex_lock(globals.device_mutex);
  4442. for (ptr = globals.device_bindings; ptr; ptr = ptr->next) {
  4443. if (ptr->function == function) {
  4444. status = SWITCH_STATUS_SUCCESS;
  4445. if (last) {
  4446. last->next = ptr->next;
  4447. } else {
  4448. globals.device_bindings = ptr->next;
  4449. last = NULL;
  4450. continue;
  4451. }
  4452. }
  4453. last = ptr;
  4454. }
  4455. switch_mutex_unlock(globals.device_mutex);
  4456. return status;
  4457. }
  4458. SWITCH_DECLARE(switch_status_t) switch_channel_pass_sdp(switch_channel_t *from_channel, switch_channel_t *to_channel, const char *sdp)
  4459. {
  4460. switch_status_t status = SWITCH_STATUS_FALSE;
  4461. char *use_sdp = (char *) sdp;
  4462. char *patched_sdp = NULL;
  4463. if (!switch_channel_get_variable(to_channel, SWITCH_B_SDP_VARIABLE)) {
  4464. const char *var;
  4465. if ((var = switch_channel_get_variable(from_channel, "bypass_media_sdp_filter"))) {
  4466. if ((patched_sdp = switch_core_media_process_sdp_filter(use_sdp, var, from_channel->session))) {
  4467. use_sdp = patched_sdp;
  4468. }
  4469. }
  4470. switch_channel_set_variable(to_channel, SWITCH_B_SDP_VARIABLE, use_sdp);
  4471. }
  4472. switch_safe_free(patched_sdp);
  4473. return status;
  4474. }
  4475. /* For Emacs:
  4476. * Local Variables:
  4477. * mode:c
  4478. * indent-tabs-mode:t
  4479. * tab-width:4
  4480. * c-basic-offset:4
  4481. * End:
  4482. * For VIM:
  4483. * vim:set softtabstop=4 shiftwidth=4 tabstop=4 noet:
  4484. */