ccss.c 153 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702
  1. /*
  2. * Asterisk -- An open source telephony toolkit.
  3. *
  4. * Copyright (C) 1999 - 2010, Digium, Inc.
  5. *
  6. * Mark Michelson <mmichelson@digium.com>
  7. *
  8. * See http://www.asterisk.org for more information about
  9. * the Asterisk project. Please do not directly contact
  10. * any of the maintainers of this project for assistance;
  11. * the project provides a web site, mailing lists and IRC
  12. * channels for your use.
  13. *
  14. * This program is free software, distributed under the terms of
  15. * the GNU General Public License Version 2. See the LICENSE file
  16. * at the top of the source tree.
  17. */
  18. /*! \file
  19. * \brief Call Completion Supplementary Services implementation
  20. * \author Mark Michelson <mmichelson@digium.com>
  21. */
  22. /*! \li \ref ccss.c uses the configuration file \ref ccss.conf
  23. * \addtogroup configuration_file Configuration Files
  24. */
  25. /*!
  26. * \page ccss.conf ccss.conf
  27. * \verbinclude ccss.conf.sample
  28. */
  29. /*** MODULEINFO
  30. <support_level>core</support_level>
  31. ***/
  32. #include "asterisk.h"
  33. ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
  34. #include "asterisk/astobj2.h"
  35. #include "asterisk/strings.h"
  36. #include "asterisk/ccss.h"
  37. #include "asterisk/channel.h"
  38. #include "asterisk/pbx.h"
  39. #include "asterisk/utils.h"
  40. #include "asterisk/taskprocessor.h"
  41. #include "asterisk/devicestate.h"
  42. #include "asterisk/module.h"
  43. #include "asterisk/app.h"
  44. #include "asterisk/cli.h"
  45. #include "asterisk/manager.h"
  46. #include "asterisk/causes.h"
  47. #include "asterisk/stasis_system.h"
  48. #include "asterisk/format_cache.h"
  49. /*** DOCUMENTATION
  50. <application name="CallCompletionRequest" language="en_US">
  51. <synopsis>
  52. Request call completion service for previous call
  53. </synopsis>
  54. <syntax />
  55. <description>
  56. <para>Request call completion service for a previously failed
  57. call attempt.</para>
  58. <para>This application sets the following channel variables:</para>
  59. <variablelist>
  60. <variable name="CC_REQUEST_RESULT">
  61. <para>This is the returned status of the request.</para>
  62. <value name="SUCCESS" />
  63. <value name="FAIL" />
  64. </variable>
  65. <variable name="CC_REQUEST_REASON">
  66. <para>This is the reason the request failed.</para>
  67. <value name="NO_CORE_INSTANCE" />
  68. <value name="NOT_GENERIC" />
  69. <value name="TOO_MANY_REQUESTS" />
  70. <value name="UNSPECIFIED" />
  71. </variable>
  72. </variablelist>
  73. </description>
  74. </application>
  75. <application name="CallCompletionCancel" language="en_US">
  76. <synopsis>
  77. Cancel call completion service
  78. </synopsis>
  79. <syntax />
  80. <description>
  81. <para>Cancel a Call Completion Request.</para>
  82. <para>This application sets the following channel variables:</para>
  83. <variablelist>
  84. <variable name="CC_CANCEL_RESULT">
  85. <para>This is the returned status of the cancel.</para>
  86. <value name="SUCCESS" />
  87. <value name="FAIL" />
  88. </variable>
  89. <variable name="CC_CANCEL_REASON">
  90. <para>This is the reason the cancel failed.</para>
  91. <value name="NO_CORE_INSTANCE" />
  92. <value name="NOT_GENERIC" />
  93. <value name="UNSPECIFIED" />
  94. </variable>
  95. </variablelist>
  96. </description>
  97. </application>
  98. ***/
  99. /* These are some file-scoped variables. It would be
  100. * nice to define them closer to their first usage, but since
  101. * they are used in many places throughout the file, defining
  102. * them here at the top is easiest.
  103. */
  104. /*!
  105. * The ast_sched_context used for all generic CC timeouts
  106. */
  107. static struct ast_sched_context *cc_sched_context;
  108. /*!
  109. * Counter used to create core IDs for CC calls. Each new
  110. * core ID is created by atomically adding 1 to the core_id_counter
  111. */
  112. static int core_id_counter;
  113. /*!
  114. * Taskprocessor from which all CC agent and monitor callbacks
  115. * are called.
  116. */
  117. static struct ast_taskprocessor *cc_core_taskprocessor;
  118. /*!
  119. * Name printed on all CC log messages.
  120. */
  121. static const char *CC_LOGGER_LEVEL_NAME = "CC";
  122. /*!
  123. * Logger level registered by the CC core.
  124. */
  125. static int cc_logger_level;
  126. /*!
  127. * Parsed configuration value for cc_max_requests
  128. */
  129. static unsigned int global_cc_max_requests;
  130. /*!
  131. * The current number of CC requests in the system
  132. */
  133. static int cc_request_count;
  134. static inline void *cc_ref(void *obj, const char *debug)
  135. {
  136. ao2_t_ref(obj, +1, debug);
  137. return obj;
  138. }
  139. static inline void *cc_unref(void *obj, const char *debug)
  140. {
  141. ao2_t_ref(obj, -1, debug);
  142. return NULL;
  143. }
  144. /*!
  145. * \since 1.8
  146. * \internal
  147. * \brief A structure for holding the configuration parameters
  148. * relating to CCSS
  149. */
  150. struct ast_cc_config_params {
  151. enum ast_cc_agent_policies cc_agent_policy;
  152. enum ast_cc_monitor_policies cc_monitor_policy;
  153. unsigned int cc_offer_timer;
  154. unsigned int ccnr_available_timer;
  155. unsigned int ccbs_available_timer;
  156. unsigned int cc_recall_timer;
  157. unsigned int cc_max_agents;
  158. unsigned int cc_max_monitors;
  159. char cc_callback_macro[AST_MAX_EXTENSION];
  160. char cc_callback_sub[AST_MAX_EXTENSION];
  161. char cc_agent_dialstring[AST_MAX_EXTENSION];
  162. };
  163. /*!
  164. * \since 1.8
  165. * \brief The states used in the CCSS core state machine
  166. *
  167. * For more information, see doc/CCSS_architecture.pdf
  168. */
  169. enum cc_state {
  170. /*! Entered when it is determined that CCSS may be used for the call */
  171. CC_AVAILABLE,
  172. /*! Entered when a CCSS agent has offered CCSS to a caller */
  173. CC_CALLER_OFFERED,
  174. /*! Entered when a CCSS agent confirms that a caller has
  175. * requested CCSS */
  176. CC_CALLER_REQUESTED,
  177. /*! Entered when a CCSS monitor confirms acknowledgment of an
  178. * outbound CCSS request */
  179. CC_ACTIVE,
  180. /*! Entered when a CCSS monitor alerts the core that the called party
  181. * has become available */
  182. CC_CALLEE_READY,
  183. /*! Entered when a CCSS agent alerts the core that the calling party
  184. * may not be recalled because he is unavailable
  185. */
  186. CC_CALLER_BUSY,
  187. /*! Entered when a CCSS agent alerts the core that the calling party
  188. * is attempting to recall the called party
  189. */
  190. CC_RECALLING,
  191. /*! Entered when an application alerts the core that the calling party's
  192. * recall attempt has had a call progress response indicated
  193. */
  194. CC_COMPLETE,
  195. /*! Entered any time that something goes wrong during the process, thus
  196. * resulting in the failure of the attempted CCSS transaction. Note also
  197. * that cancellations of CC are treated as failures.
  198. */
  199. CC_FAILED,
  200. };
  201. /*!
  202. * \brief The payload for an AST_CONTROL_CC frame
  203. *
  204. * \details
  205. * This contains all the necessary data regarding
  206. * a called device so that the CC core will be able
  207. * to allocate the proper monitoring resources.
  208. */
  209. struct cc_control_payload {
  210. /*!
  211. * \brief The type of monitor to allocate.
  212. *
  213. * \details
  214. * The type of monitor to allocate. This is a string which corresponds
  215. * to a set of monitor callbacks registered. Examples include "generic"
  216. * and "SIP"
  217. *
  218. * \note This really should be an array of characters in case this payload
  219. * is sent accross an IAX2 link. However, this would not make too much sense
  220. * given this type may not be recognized by the other end.
  221. * Protection may be necessary to prevent it from being transmitted.
  222. *
  223. * In addition the following other problems are also possible:
  224. * 1) Endian issues with the integers/enums stored in the config_params.
  225. * 2) Alignment padding issues for the element types.
  226. */
  227. const char *monitor_type;
  228. /*!
  229. * \brief Private data allocated by the callee
  230. *
  231. * \details
  232. * All channel drivers that monitor endpoints will need to allocate
  233. * data that is not usable by the CC core. In most cases, some or all
  234. * of this data is allocated at the time that the channel driver offers
  235. * CC to the caller. There are many opportunities for failures to occur
  236. * between when a channel driver offers CC and when a monitor is actually
  237. * allocated to watch the endpoint. For this reason, the channel driver
  238. * must give the core a pointer to the private data that was allocated so
  239. * that the core can call back into the channel driver to destroy it if
  240. * a failure occurs. If no private data has been allocated at the time that
  241. * CC is offered, then it is perfectly acceptable to pass NULL for this
  242. * field.
  243. */
  244. void *private_data;
  245. /*!
  246. * \brief Service offered by the endpoint
  247. *
  248. * \details
  249. * This indicates the type of call completion service offered by the
  250. * endpoint. This data is not crucial to the machinations of the CC core,
  251. * but it is helpful for debugging purposes.
  252. */
  253. enum ast_cc_service_type service;
  254. /*!
  255. * \brief Configuration parameters used by this endpoint
  256. *
  257. * \details
  258. * Each time an endpoint offers call completion, it must provide its call
  259. * completion configuration parameters. This is because settings may be different
  260. * depending on the circumstances.
  261. */
  262. struct ast_cc_config_params config_params;
  263. /*!
  264. * \brief ID of parent extension
  265. *
  266. * \details
  267. * This is the only datum that the CC core derives on its own and is not
  268. * provided by the offerer of CC. This provides the core with information on
  269. * which extension monitor is the most immediate parent of this device.
  270. */
  271. int parent_interface_id;
  272. /*!
  273. * \brief Name of device to be monitored
  274. *
  275. * \details
  276. * The device name by which this monitored endpoint will be referred in the
  277. * CC core. It is highly recommended that this device name is derived by using
  278. * the function ast_channel_get_device_name.
  279. */
  280. char device_name[AST_CHANNEL_NAME];
  281. /*!
  282. * \brief Recall dialstring
  283. *
  284. * \details
  285. * Certain channel drivers (DAHDI in particular) will require that a special
  286. * dialstring be used to indicate that the outgoing call is to interpreted as
  287. * a CC recall. If the channel driver has such a requirement, then this is
  288. * where that special recall dialstring is placed. If no special dialstring
  289. * is to be used, then the channel driver must provide the original dialstring
  290. * used to call this endpoint.
  291. */
  292. char dialstring[AST_CHANNEL_NAME];
  293. };
  294. /*!
  295. * \brief The "tree" of interfaces that is dialed.
  296. *
  297. * \details
  298. * Though this is a linked list, it is logically treated
  299. * as a tree of monitors. Each monitor has an id and a parent_id
  300. * associated with it. The id is a unique ID for that monitor, and
  301. * the parent_id is the unique ID of the monitor's parent in the
  302. * tree. The tree is structured such that all of a parent's children
  303. * will appear after the parent in the tree. However, it cannot be
  304. * guaranteed exactly where after the parent the children are.
  305. *
  306. * The tree is reference counted since several threads may need
  307. * to use it, and it may last beyond the lifetime of a single
  308. * thread.
  309. */
  310. AST_LIST_HEAD(cc_monitor_tree, ast_cc_monitor);
  311. static const int CC_CORE_INSTANCES_BUCKETS = 17;
  312. static struct ao2_container *cc_core_instances;
  313. struct cc_core_instance {
  314. /*!
  315. * Unique identifier for this instance of the CC core.
  316. */
  317. int core_id;
  318. /*!
  319. * The current state for this instance of the CC core.
  320. */
  321. enum cc_state current_state;
  322. /*!
  323. * The CC agent in use for this call
  324. */
  325. struct ast_cc_agent *agent;
  326. /*!
  327. * Reference to the monitor tree formed during the initial call
  328. */
  329. struct cc_monitor_tree *monitors;
  330. };
  331. /*!
  332. * \internal
  333. * \brief Request that the core change states
  334. * \param state The state to which we wish to change
  335. * \param core_id The unique identifier for this instance of the CCSS core state machine
  336. * \param debug Optional message explaining the reason for the state change
  337. * \param ap varargs list
  338. * \retval 0 State change successfully queued
  339. * \retval -1 Unable to queue state change request
  340. */
  341. static int __attribute__((format(printf, 3, 0))) cc_request_state_change(enum cc_state state, const int core_id, const char *debug, va_list ap);
  342. /*!
  343. * \internal
  344. * \brief create a new instance of the CC core and an agent for the calling channel
  345. *
  346. * This function will check to make sure that the incoming channel
  347. * is allowed to request CC by making sure that the incoming channel
  348. * has not exceeded its maximum number of allowed agents.
  349. *
  350. * Should that check pass, the core instance is created, and then the
  351. * agent for the channel.
  352. *
  353. * \param caller_chan The incoming channel for this particular call
  354. * \param called_tree A reference to the tree of called devices. The agent
  355. * will gain a reference to this tree as well
  356. * \param core_id The core_id that this core_instance will assume
  357. * \retval NULL Failed to create the core instance either due to memory allocation
  358. * errors or due to the agent count for the caller being too high
  359. * \retval non-NULL A reference to the newly created cc_core_instance
  360. */
  361. static struct cc_core_instance *cc_core_init_instance(struct ast_channel *caller_chan,
  362. struct cc_monitor_tree *called_tree, const int core_id, struct cc_control_payload *cc_data);
  363. static const struct {
  364. enum ast_cc_service_type service;
  365. const char *service_string;
  366. } cc_service_to_string_map[] = {
  367. {AST_CC_NONE, "NONE"},
  368. {AST_CC_CCBS, "CCBS"},
  369. {AST_CC_CCNR, "CCNR"},
  370. {AST_CC_CCNL, "CCNL"},
  371. };
  372. static const struct {
  373. enum cc_state state;
  374. const char *state_string;
  375. } cc_state_to_string_map[] = {
  376. {CC_AVAILABLE, "CC is available"},
  377. {CC_CALLER_OFFERED, "CC offered to caller"},
  378. {CC_CALLER_REQUESTED, "CC requested by caller"},
  379. {CC_ACTIVE, "CC accepted by callee"},
  380. {CC_CALLEE_READY, "Callee has become available"},
  381. {CC_CALLER_BUSY, "Callee was ready, but caller is now unavailable"},
  382. {CC_RECALLING, "Caller is attempting to recall"},
  383. {CC_COMPLETE, "Recall complete"},
  384. {CC_FAILED, "CC has failed"},
  385. };
  386. static const char *cc_state_to_string(enum cc_state state)
  387. {
  388. return cc_state_to_string_map[state].state_string;
  389. }
  390. static const char *cc_service_to_string(enum ast_cc_service_type service)
  391. {
  392. return cc_service_to_string_map[service].service_string;
  393. }
  394. static int cc_core_instance_hash_fn(const void *obj, const int flags)
  395. {
  396. const struct cc_core_instance *core_instance = obj;
  397. return core_instance->core_id;
  398. }
  399. static int cc_core_instance_cmp_fn(void *obj, void *arg, int flags)
  400. {
  401. struct cc_core_instance *core_instance1 = obj;
  402. struct cc_core_instance *core_instance2 = arg;
  403. return core_instance1->core_id == core_instance2->core_id ? CMP_MATCH | CMP_STOP : 0;
  404. }
  405. static struct cc_core_instance *find_cc_core_instance(const int core_id)
  406. {
  407. struct cc_core_instance finder = {.core_id = core_id,};
  408. return ao2_t_find(cc_core_instances, &finder, OBJ_POINTER, "Finding a core_instance");
  409. }
  410. struct cc_callback_helper {
  411. ao2_callback_fn *function;
  412. void *args;
  413. const char *type;
  414. };
  415. static int cc_agent_callback_helper(void *obj, void *args, int flags)
  416. {
  417. struct cc_core_instance *core_instance = obj;
  418. struct cc_callback_helper *helper = args;
  419. if (strcmp(core_instance->agent->callbacks->type, helper->type)) {
  420. return 0;
  421. }
  422. return helper->function(core_instance->agent, helper->args, flags);
  423. }
  424. struct ast_cc_agent *ast_cc_agent_callback(int flags, ao2_callback_fn *function, void *args, const char * const type)
  425. {
  426. struct cc_callback_helper helper = {.function = function, .args = args, .type = type};
  427. struct cc_core_instance *core_instance;
  428. if ((core_instance = ao2_t_callback(cc_core_instances, flags, cc_agent_callback_helper, &helper,
  429. "Calling provided agent callback function"))) {
  430. struct ast_cc_agent *agent = cc_ref(core_instance->agent, "An outside entity needs the agent");
  431. cc_unref(core_instance, "agent callback done with the core_instance");
  432. return agent;
  433. }
  434. return NULL;
  435. }
  436. enum match_flags {
  437. /* Only match agents that have not yet
  438. * made a CC request
  439. */
  440. MATCH_NO_REQUEST = (1 << 0),
  441. /* Only match agents that have made
  442. * a CC request
  443. */
  444. MATCH_REQUEST = (1 << 1),
  445. };
  446. /* ao2_callbacks for cc_core_instances */
  447. /*!
  448. * \internal
  449. * \brief find a core instance based on its agent
  450. *
  451. * The match flags tell whether we wish to find core instances
  452. * that have a monitor or core instances that do not. Core instances
  453. * with no monitor are core instances for which a caller has not yet
  454. * requested CC. Core instances with a monitor are ones for which the
  455. * caller has requested CC.
  456. */
  457. static int match_agent(void *obj, void *arg, void *data, int flags)
  458. {
  459. struct cc_core_instance *core_instance = obj;
  460. const char *name = arg;
  461. unsigned long match_flags = *(unsigned long *)data;
  462. int possible_match = 0;
  463. if ((match_flags & MATCH_NO_REQUEST) && core_instance->current_state < CC_CALLER_REQUESTED) {
  464. possible_match = 1;
  465. }
  466. if ((match_flags & MATCH_REQUEST) && core_instance->current_state >= CC_CALLER_REQUESTED) {
  467. possible_match = 1;
  468. }
  469. if (!possible_match) {
  470. return 0;
  471. }
  472. if (!strcmp(core_instance->agent->device_name, name)) {
  473. return CMP_MATCH | CMP_STOP;
  474. }
  475. return 0;
  476. }
  477. struct count_agents_cb_data {
  478. int count;
  479. int core_id_exception;
  480. };
  481. /*!
  482. * \internal
  483. * \brief Count the number of agents a specific interface is using
  484. *
  485. * We're only concerned with the number of agents that have requested
  486. * CC, so we restrict our search to core instances which have a non-NULL
  487. * monitor pointer
  488. */
  489. static int count_agents_cb(void *obj, void *arg, void *data, int flags)
  490. {
  491. struct cc_core_instance *core_instance = obj;
  492. const char *name = arg;
  493. struct count_agents_cb_data *cb_data = data;
  494. if (cb_data->core_id_exception == core_instance->core_id) {
  495. ast_log_dynamic_level(cc_logger_level, "Found agent with core_id %d but not counting it toward total\n", core_instance->core_id);
  496. return 0;
  497. }
  498. if (core_instance->current_state >= CC_CALLER_REQUESTED && !strcmp(core_instance->agent->device_name, name)) {
  499. cb_data->count++;
  500. }
  501. return 0;
  502. }
  503. /* default values mapping from cc_state to ast_dev_state */
  504. #define CC_AVAILABLE_DEVSTATE_DEFAULT AST_DEVICE_NOT_INUSE
  505. #define CC_CALLER_OFFERED_DEVSTATE_DEFAULT AST_DEVICE_NOT_INUSE
  506. #define CC_CALLER_REQUESTED_DEVSTATE_DEFAULT AST_DEVICE_NOT_INUSE
  507. #define CC_ACTIVE_DEVSTATE_DEFAULT AST_DEVICE_INUSE
  508. #define CC_CALLEE_READY_DEVSTATE_DEFAULT AST_DEVICE_RINGING
  509. #define CC_CALLER_BUSY_DEVSTATE_DEFAULT AST_DEVICE_ONHOLD
  510. #define CC_RECALLING_DEVSTATE_DEFAULT AST_DEVICE_RINGING
  511. #define CC_COMPLETE_DEVSTATE_DEFAULT AST_DEVICE_NOT_INUSE
  512. #define CC_FAILED_DEVSTATE_DEFAULT AST_DEVICE_NOT_INUSE
  513. /*!
  514. * \internal
  515. * \brief initialization of defaults for CC_STATE to DEVICE_STATE map
  516. */
  517. static enum ast_device_state cc_state_to_devstate_map[] = {
  518. [CC_AVAILABLE] = CC_AVAILABLE_DEVSTATE_DEFAULT,
  519. [CC_CALLER_OFFERED] = CC_CALLER_OFFERED_DEVSTATE_DEFAULT,
  520. [CC_CALLER_REQUESTED] = CC_CALLER_REQUESTED_DEVSTATE_DEFAULT,
  521. [CC_ACTIVE] = CC_ACTIVE_DEVSTATE_DEFAULT,
  522. [CC_CALLEE_READY] = CC_CALLEE_READY_DEVSTATE_DEFAULT,
  523. [CC_CALLER_BUSY] = CC_CALLER_BUSY_DEVSTATE_DEFAULT,
  524. [CC_RECALLING] = CC_RECALLING_DEVSTATE_DEFAULT,
  525. [CC_COMPLETE] = CC_COMPLETE_DEVSTATE_DEFAULT,
  526. [CC_FAILED] = CC_FAILED_DEVSTATE_DEFAULT,
  527. };
  528. /*!
  529. * \internal
  530. * \brief lookup the ast_device_state mapped to cc_state
  531. *
  532. * \param state
  533. *
  534. * \return the correponding DEVICE STATE from the cc_state_to_devstate_map
  535. * when passed an internal state.
  536. */
  537. static enum ast_device_state cc_state_to_devstate(enum cc_state state)
  538. {
  539. return cc_state_to_devstate_map[state];
  540. }
  541. /*!
  542. * \internal
  543. * \brief Callback for devicestate providers
  544. *
  545. * \details
  546. * Initialize with ast_devstate_prov_add() and returns the corresponding
  547. * DEVICE STATE based on the current CC_STATE state machine if the requested
  548. * device is found and is a generic device. Returns the equivalent of
  549. * CC_FAILED, which defaults to NOT_INUSE, if no device is found. NOT_INUSE would
  550. * indicate that there is no presence of any pending call back.
  551. */
  552. static enum ast_device_state ccss_device_state(const char *device_name)
  553. {
  554. struct cc_core_instance *core_instance;
  555. unsigned long match_flags;
  556. enum ast_device_state cc_current_state;
  557. match_flags = MATCH_NO_REQUEST;
  558. core_instance = ao2_t_callback_data(cc_core_instances, 0, match_agent,
  559. (char *) device_name, &match_flags,
  560. "Find Core Instance for ccss_device_state reqeust.");
  561. if (!core_instance) {
  562. ast_log_dynamic_level(cc_logger_level,
  563. "Couldn't find a core instance for caller %s\n", device_name);
  564. return cc_state_to_devstate(CC_FAILED);
  565. }
  566. ast_log_dynamic_level(cc_logger_level,
  567. "Core %d: Found core_instance for caller %s in state %s\n",
  568. core_instance->core_id, device_name, cc_state_to_string(core_instance->current_state));
  569. if (strcmp(core_instance->agent->callbacks->type, "generic")) {
  570. ast_log_dynamic_level(cc_logger_level,
  571. "Core %d: Device State is only for generic agent types.\n",
  572. core_instance->core_id);
  573. cc_unref(core_instance, "Unref core_instance since ccss_device_state was called with native agent");
  574. return cc_state_to_devstate(CC_FAILED);
  575. }
  576. cc_current_state = cc_state_to_devstate(core_instance->current_state);
  577. cc_unref(core_instance, "Unref core_instance done with ccss_device_state");
  578. return cc_current_state;
  579. }
  580. /*!
  581. * \internal
  582. * \brief Notify Device State Changes from CC STATE MACHINE
  583. *
  584. * \details
  585. * Any time a state is changed, we call this function to notify the DEVICE STATE
  586. * subsystem of the change so that subscribed phones to any corresponding hints that
  587. * are using that state are updated.
  588. */
  589. static void ccss_notify_device_state_change(const char *device, enum cc_state state)
  590. {
  591. enum ast_device_state devstate;
  592. devstate = cc_state_to_devstate(state);
  593. ast_log_dynamic_level(cc_logger_level,
  594. "Notification of CCSS state change to '%s', device state '%s' for device '%s'\n",
  595. cc_state_to_string(state), ast_devstate2str(devstate), device);
  596. ast_devstate_changed(devstate, AST_DEVSTATE_CACHABLE, "ccss:%s", device);
  597. }
  598. #define CC_OFFER_TIMER_DEFAULT 20 /* Seconds */
  599. #define CCNR_AVAILABLE_TIMER_DEFAULT 7200 /* Seconds */
  600. #define CCBS_AVAILABLE_TIMER_DEFAULT 4800 /* Seconds */
  601. #define CC_RECALL_TIMER_DEFAULT 20 /* Seconds */
  602. #define CC_MAX_AGENTS_DEFAULT 5
  603. #define CC_MAX_MONITORS_DEFAULT 5
  604. #define GLOBAL_CC_MAX_REQUESTS_DEFAULT 20
  605. static const struct ast_cc_config_params cc_default_params = {
  606. .cc_agent_policy = AST_CC_AGENT_NEVER,
  607. .cc_monitor_policy = AST_CC_MONITOR_NEVER,
  608. .cc_offer_timer = CC_OFFER_TIMER_DEFAULT,
  609. .ccnr_available_timer = CCNR_AVAILABLE_TIMER_DEFAULT,
  610. .ccbs_available_timer = CCBS_AVAILABLE_TIMER_DEFAULT,
  611. .cc_recall_timer = CC_RECALL_TIMER_DEFAULT,
  612. .cc_max_agents = CC_MAX_AGENTS_DEFAULT,
  613. .cc_max_monitors = CC_MAX_MONITORS_DEFAULT,
  614. .cc_callback_macro = "",
  615. .cc_callback_sub = "",
  616. .cc_agent_dialstring = "",
  617. };
  618. void ast_cc_default_config_params(struct ast_cc_config_params *params)
  619. {
  620. *params = cc_default_params;
  621. }
  622. struct ast_cc_config_params *__ast_cc_config_params_init(const char *file, int line, const char *function)
  623. {
  624. #if defined(__AST_DEBUG_MALLOC)
  625. struct ast_cc_config_params *params = __ast_malloc(sizeof(*params), file, line, function);
  626. #else
  627. struct ast_cc_config_params *params = ast_malloc(sizeof(*params));
  628. #endif
  629. if (!params) {
  630. return NULL;
  631. }
  632. ast_cc_default_config_params(params);
  633. return params;
  634. }
  635. void ast_cc_config_params_destroy(struct ast_cc_config_params *params)
  636. {
  637. ast_free(params);
  638. }
  639. static enum ast_cc_agent_policies str_to_agent_policy(const char * const value)
  640. {
  641. if (!strcasecmp(value, "never")) {
  642. return AST_CC_AGENT_NEVER;
  643. } else if (!strcasecmp(value, "native")) {
  644. return AST_CC_AGENT_NATIVE;
  645. } else if (!strcasecmp(value, "generic")) {
  646. return AST_CC_AGENT_GENERIC;
  647. } else {
  648. ast_log(LOG_WARNING, "%s is an invalid value for cc_agent_policy. Switching to 'never'\n", value);
  649. return AST_CC_AGENT_NEVER;
  650. }
  651. }
  652. static enum ast_cc_monitor_policies str_to_monitor_policy(const char * const value)
  653. {
  654. if (!strcasecmp(value, "never")) {
  655. return AST_CC_MONITOR_NEVER;
  656. } else if (!strcasecmp(value, "native")) {
  657. return AST_CC_MONITOR_NATIVE;
  658. } else if (!strcasecmp(value, "generic")) {
  659. return AST_CC_MONITOR_GENERIC;
  660. } else if (!strcasecmp(value, "always")) {
  661. return AST_CC_MONITOR_ALWAYS;
  662. } else {
  663. ast_log(LOG_WARNING, "%s is an invalid value for cc_monitor_policy. Switching to 'never'\n", value);
  664. return AST_CC_MONITOR_NEVER;
  665. }
  666. }
  667. static const char *agent_policy_to_str(enum ast_cc_agent_policies policy)
  668. {
  669. switch (policy) {
  670. case AST_CC_AGENT_NEVER:
  671. return "never";
  672. case AST_CC_AGENT_NATIVE:
  673. return "native";
  674. case AST_CC_AGENT_GENERIC:
  675. return "generic";
  676. default:
  677. /* This should never happen... */
  678. return "";
  679. }
  680. }
  681. static const char *monitor_policy_to_str(enum ast_cc_monitor_policies policy)
  682. {
  683. switch (policy) {
  684. case AST_CC_MONITOR_NEVER:
  685. return "never";
  686. case AST_CC_MONITOR_NATIVE:
  687. return "native";
  688. case AST_CC_MONITOR_GENERIC:
  689. return "generic";
  690. case AST_CC_MONITOR_ALWAYS:
  691. return "always";
  692. default:
  693. /* This should never happen... */
  694. return "";
  695. }
  696. }
  697. int ast_cc_get_param(struct ast_cc_config_params *params, const char * const name,
  698. char *buf, size_t buf_len)
  699. {
  700. const char *value = NULL;
  701. if (!strcasecmp(name, "cc_callback_macro")) {
  702. value = ast_get_cc_callback_macro(params);
  703. } else if (!strcasecmp(name, "cc_callback_sub")) {
  704. value = ast_get_cc_callback_sub(params);
  705. } else if (!strcasecmp(name, "cc_agent_policy")) {
  706. value = agent_policy_to_str(ast_get_cc_agent_policy(params));
  707. } else if (!strcasecmp(name, "cc_monitor_policy")) {
  708. value = monitor_policy_to_str(ast_get_cc_monitor_policy(params));
  709. } else if (!strcasecmp(name, "cc_agent_dialstring")) {
  710. value = ast_get_cc_agent_dialstring(params);
  711. }
  712. if (value) {
  713. ast_copy_string(buf, value, buf_len);
  714. return 0;
  715. }
  716. /* The rest of these are all ints of some sort and require some
  717. * snprintf-itude
  718. */
  719. if (!strcasecmp(name, "cc_offer_timer")) {
  720. snprintf(buf, buf_len, "%u", ast_get_cc_offer_timer(params));
  721. } else if (!strcasecmp(name, "ccnr_available_timer")) {
  722. snprintf(buf, buf_len, "%u", ast_get_ccnr_available_timer(params));
  723. } else if (!strcasecmp(name, "ccbs_available_timer")) {
  724. snprintf(buf, buf_len, "%u", ast_get_ccbs_available_timer(params));
  725. } else if (!strcasecmp(name, "cc_max_agents")) {
  726. snprintf(buf, buf_len, "%u", ast_get_cc_max_agents(params));
  727. } else if (!strcasecmp(name, "cc_max_monitors")) {
  728. snprintf(buf, buf_len, "%u", ast_get_cc_max_monitors(params));
  729. } else if (!strcasecmp(name, "cc_recall_timer")) {
  730. snprintf(buf, buf_len, "%u", ast_get_cc_recall_timer(params));
  731. } else {
  732. ast_log(LOG_WARNING, "%s is not a valid CC parameter. Ignoring.\n", name);
  733. return -1;
  734. }
  735. return 0;
  736. }
  737. int ast_cc_set_param(struct ast_cc_config_params *params, const char * const name,
  738. const char * const value)
  739. {
  740. unsigned int value_as_uint;
  741. if (!strcasecmp(name, "cc_agent_policy")) {
  742. return ast_set_cc_agent_policy(params, str_to_agent_policy(value));
  743. } else if (!strcasecmp(name, "cc_monitor_policy")) {
  744. return ast_set_cc_monitor_policy(params, str_to_monitor_policy(value));
  745. } else if (!strcasecmp(name, "cc_agent_dialstring")) {
  746. ast_set_cc_agent_dialstring(params, value);
  747. } else if (!strcasecmp(name, "cc_callback_macro")) {
  748. ast_set_cc_callback_macro(params, value);
  749. return 0;
  750. } else if (!strcasecmp(name, "cc_callback_sub")) {
  751. ast_set_cc_callback_sub(params, value);
  752. return 0;
  753. }
  754. if (sscanf(value, "%30u", &value_as_uint) != 1) {
  755. return -1;
  756. }
  757. if (!strcasecmp(name, "cc_offer_timer")) {
  758. ast_set_cc_offer_timer(params, value_as_uint);
  759. } else if (!strcasecmp(name, "ccnr_available_timer")) {
  760. ast_set_ccnr_available_timer(params, value_as_uint);
  761. } else if (!strcasecmp(name, "ccbs_available_timer")) {
  762. ast_set_ccbs_available_timer(params, value_as_uint);
  763. } else if (!strcasecmp(name, "cc_max_agents")) {
  764. ast_set_cc_max_agents(params, value_as_uint);
  765. } else if (!strcasecmp(name, "cc_max_monitors")) {
  766. ast_set_cc_max_monitors(params, value_as_uint);
  767. } else if (!strcasecmp(name, "cc_recall_timer")) {
  768. ast_set_cc_recall_timer(params, value_as_uint);
  769. } else {
  770. ast_log(LOG_WARNING, "%s is not a valid CC parameter. Ignoring.\n", name);
  771. return -1;
  772. }
  773. return 0;
  774. }
  775. int ast_cc_is_config_param(const char * const name)
  776. {
  777. return (!strcasecmp(name, "cc_agent_policy") ||
  778. !strcasecmp(name, "cc_monitor_policy") ||
  779. !strcasecmp(name, "cc_offer_timer") ||
  780. !strcasecmp(name, "ccnr_available_timer") ||
  781. !strcasecmp(name, "ccbs_available_timer") ||
  782. !strcasecmp(name, "cc_max_agents") ||
  783. !strcasecmp(name, "cc_max_monitors") ||
  784. !strcasecmp(name, "cc_callback_macro") ||
  785. !strcasecmp(name, "cc_callback_sub") ||
  786. !strcasecmp(name, "cc_agent_dialstring") ||
  787. !strcasecmp(name, "cc_recall_timer"));
  788. }
  789. void ast_cc_copy_config_params(struct ast_cc_config_params *dest, const struct ast_cc_config_params *src)
  790. {
  791. *dest = *src;
  792. }
  793. enum ast_cc_agent_policies ast_get_cc_agent_policy(struct ast_cc_config_params *config)
  794. {
  795. return config->cc_agent_policy;
  796. }
  797. int ast_set_cc_agent_policy(struct ast_cc_config_params *config, enum ast_cc_agent_policies value)
  798. {
  799. /* Screw C and its weak type checking for making me have to do this
  800. * validation at runtime.
  801. */
  802. if (value < AST_CC_AGENT_NEVER || value > AST_CC_AGENT_GENERIC) {
  803. return -1;
  804. }
  805. config->cc_agent_policy = value;
  806. return 0;
  807. }
  808. enum ast_cc_monitor_policies ast_get_cc_monitor_policy(struct ast_cc_config_params *config)
  809. {
  810. return config->cc_monitor_policy;
  811. }
  812. int ast_set_cc_monitor_policy(struct ast_cc_config_params *config, enum ast_cc_monitor_policies value)
  813. {
  814. /* Screw C and its weak type checking for making me have to do this
  815. * validation at runtime.
  816. */
  817. if (value < AST_CC_MONITOR_NEVER || value > AST_CC_MONITOR_ALWAYS) {
  818. return -1;
  819. }
  820. config->cc_monitor_policy = value;
  821. return 0;
  822. }
  823. unsigned int ast_get_cc_offer_timer(struct ast_cc_config_params *config)
  824. {
  825. return config->cc_offer_timer;
  826. }
  827. void ast_set_cc_offer_timer(struct ast_cc_config_params *config, unsigned int value)
  828. {
  829. /* 0 is an unreasonable value for any timer. Stick with the default */
  830. if (value == 0) {
  831. ast_log(LOG_WARNING, "0 is an invalid value for cc_offer_timer. Retaining value as %u\n", config->cc_offer_timer);
  832. return;
  833. }
  834. config->cc_offer_timer = value;
  835. }
  836. unsigned int ast_get_ccnr_available_timer(struct ast_cc_config_params *config)
  837. {
  838. return config->ccnr_available_timer;
  839. }
  840. void ast_set_ccnr_available_timer(struct ast_cc_config_params *config, unsigned int value)
  841. {
  842. /* 0 is an unreasonable value for any timer. Stick with the default */
  843. if (value == 0) {
  844. ast_log(LOG_WARNING, "0 is an invalid value for ccnr_available_timer. Retaining value as %u\n", config->ccnr_available_timer);
  845. return;
  846. }
  847. config->ccnr_available_timer = value;
  848. }
  849. unsigned int ast_get_cc_recall_timer(struct ast_cc_config_params *config)
  850. {
  851. return config->cc_recall_timer;
  852. }
  853. void ast_set_cc_recall_timer(struct ast_cc_config_params *config, unsigned int value)
  854. {
  855. /* 0 is an unreasonable value for any timer. Stick with the default */
  856. if (value == 0) {
  857. ast_log(LOG_WARNING, "0 is an invalid value for ccnr_available_timer. Retaining value as %u\n", config->cc_recall_timer);
  858. return;
  859. }
  860. config->cc_recall_timer = value;
  861. }
  862. unsigned int ast_get_ccbs_available_timer(struct ast_cc_config_params *config)
  863. {
  864. return config->ccbs_available_timer;
  865. }
  866. void ast_set_ccbs_available_timer(struct ast_cc_config_params *config, unsigned int value)
  867. {
  868. /* 0 is an unreasonable value for any timer. Stick with the default */
  869. if (value == 0) {
  870. ast_log(LOG_WARNING, "0 is an invalid value for ccbs_available_timer. Retaining value as %u\n", config->ccbs_available_timer);
  871. return;
  872. }
  873. config->ccbs_available_timer = value;
  874. }
  875. const char *ast_get_cc_agent_dialstring(struct ast_cc_config_params *config)
  876. {
  877. return config->cc_agent_dialstring;
  878. }
  879. void ast_set_cc_agent_dialstring(struct ast_cc_config_params *config, const char *const value)
  880. {
  881. if (ast_strlen_zero(value)) {
  882. config->cc_agent_dialstring[0] = '\0';
  883. } else {
  884. ast_copy_string(config->cc_agent_dialstring, value, sizeof(config->cc_agent_dialstring));
  885. }
  886. }
  887. unsigned int ast_get_cc_max_agents(struct ast_cc_config_params *config)
  888. {
  889. return config->cc_max_agents;
  890. }
  891. void ast_set_cc_max_agents(struct ast_cc_config_params *config, unsigned int value)
  892. {
  893. config->cc_max_agents = value;
  894. }
  895. unsigned int ast_get_cc_max_monitors(struct ast_cc_config_params *config)
  896. {
  897. return config->cc_max_monitors;
  898. }
  899. void ast_set_cc_max_monitors(struct ast_cc_config_params *config, unsigned int value)
  900. {
  901. config->cc_max_monitors = value;
  902. }
  903. const char *ast_get_cc_callback_macro(struct ast_cc_config_params *config)
  904. {
  905. return config->cc_callback_macro;
  906. }
  907. const char *ast_get_cc_callback_sub(struct ast_cc_config_params *config)
  908. {
  909. return config->cc_callback_sub;
  910. }
  911. void ast_set_cc_callback_macro(struct ast_cc_config_params *config, const char * const value)
  912. {
  913. ast_log(LOG_WARNING, "Usage of cc_callback_macro is deprecated. Please use cc_callback_sub instead.\n");
  914. if (ast_strlen_zero(value)) {
  915. config->cc_callback_macro[0] = '\0';
  916. } else {
  917. ast_copy_string(config->cc_callback_macro, value, sizeof(config->cc_callback_macro));
  918. }
  919. }
  920. void ast_set_cc_callback_sub(struct ast_cc_config_params *config, const char * const value)
  921. {
  922. if (ast_strlen_zero(value)) {
  923. config->cc_callback_sub[0] = '\0';
  924. } else {
  925. ast_copy_string(config->cc_callback_sub, value, sizeof(config->cc_callback_sub));
  926. }
  927. }
  928. static int cc_publish(struct stasis_message_type *message_type, int core_id, struct ast_json *extras)
  929. {
  930. struct ast_json *blob;
  931. struct ast_json_payload *payload;
  932. struct stasis_message *message;
  933. if (!message_type) {
  934. return -1;
  935. }
  936. blob = ast_json_pack("{s: i}",
  937. "core_id", core_id);
  938. if (!blob) {
  939. return -1;
  940. }
  941. if (extras) {
  942. ast_json_object_update(blob, extras);
  943. }
  944. payload = ast_json_payload_create(blob);
  945. ast_json_unref(blob);
  946. if (!payload) {
  947. return -1;
  948. }
  949. message = stasis_message_create(message_type, payload);
  950. ao2_ref(payload, -1);
  951. if (!message) {
  952. return -1;
  953. }
  954. stasis_publish(ast_system_topic(), message);
  955. ao2_ref(message, -1);
  956. return 0;
  957. }
  958. static void cc_publish_available(int core_id, const char *callee, const char *service)
  959. {
  960. struct ast_json *extras;
  961. extras = ast_json_pack("{s: s, s: s}",
  962. "callee", callee,
  963. "service", service);
  964. cc_publish(ast_cc_available_type(), core_id, extras);
  965. ast_json_unref(extras);
  966. }
  967. static void cc_publish_offertimerstart(int core_id, const char *caller, unsigned int expires)
  968. {
  969. struct ast_json *extras;
  970. extras = ast_json_pack("{s: s, s: i}",
  971. "caller", caller,
  972. "expires", expires);
  973. cc_publish(ast_cc_offertimerstart_type(), core_id, extras);
  974. ast_json_unref(extras);
  975. }
  976. static void cc_publish_requested(int core_id, const char *caller, const char *callee)
  977. {
  978. struct ast_json *extras;
  979. extras = ast_json_pack("{s: s, s: s}",
  980. "caller", caller,
  981. "callee", callee);
  982. cc_publish(ast_cc_requested_type(), core_id, extras);
  983. ast_json_unref(extras);
  984. }
  985. static void cc_publish_requestacknowledged(int core_id, const char *caller)
  986. {
  987. struct ast_json *extras;
  988. extras = ast_json_pack("{s: s}",
  989. "caller", caller);
  990. cc_publish(ast_cc_requestacknowledged_type(), core_id, extras);
  991. ast_json_unref(extras);
  992. }
  993. static void cc_publish_callerstopmonitoring(int core_id, const char *caller)
  994. {
  995. struct ast_json *extras;
  996. extras = ast_json_pack("{s: s}",
  997. "caller", caller);
  998. cc_publish(ast_cc_callerstopmonitoring_type(), core_id, extras);
  999. ast_json_unref(extras);
  1000. }
  1001. static void cc_publish_callerstartmonitoring(int core_id, const char *caller)
  1002. {
  1003. struct ast_json *extras;
  1004. extras = ast_json_pack("{s: s}",
  1005. "caller", caller);
  1006. cc_publish(ast_cc_callerstartmonitoring_type(), core_id, extras);
  1007. ast_json_unref(extras);
  1008. }
  1009. static void cc_publish_callerrecalling(int core_id, const char *caller)
  1010. {
  1011. struct ast_json *extras;
  1012. extras = ast_json_pack("{s: s}",
  1013. "caller", caller);
  1014. cc_publish(ast_cc_callerrecalling_type(), core_id, extras);
  1015. ast_json_unref(extras);
  1016. }
  1017. static void cc_publish_recallcomplete(int core_id, const char *caller)
  1018. {
  1019. struct ast_json *extras;
  1020. extras = ast_json_pack("{s: s}",
  1021. "caller", caller);
  1022. cc_publish(ast_cc_recallcomplete_type(), core_id, extras);
  1023. ast_json_unref(extras);
  1024. }
  1025. static void cc_publish_failure(int core_id, const char *caller, const char *reason)
  1026. {
  1027. struct ast_json *extras;
  1028. extras = ast_json_pack("{s: s, s: s}",
  1029. "caller", caller,
  1030. "reason", reason);
  1031. cc_publish(ast_cc_failure_type(), core_id, extras);
  1032. ast_json_unref(extras);
  1033. }
  1034. static void cc_publish_monitorfailed(int core_id, const char *callee)
  1035. {
  1036. struct ast_json *extras;
  1037. extras = ast_json_pack("{s: s}",
  1038. "callee", callee);
  1039. cc_publish(ast_cc_monitorfailed_type(), core_id, extras);
  1040. ast_json_unref(extras);
  1041. }
  1042. struct cc_monitor_backend {
  1043. AST_LIST_ENTRY(cc_monitor_backend) next;
  1044. const struct ast_cc_monitor_callbacks *callbacks;
  1045. };
  1046. AST_RWLIST_HEAD_STATIC(cc_monitor_backends, cc_monitor_backend);
  1047. int ast_cc_monitor_register(const struct ast_cc_monitor_callbacks *callbacks)
  1048. {
  1049. struct cc_monitor_backend *backend = ast_calloc(1, sizeof(*backend));
  1050. if (!backend) {
  1051. return -1;
  1052. }
  1053. backend->callbacks = callbacks;
  1054. AST_RWLIST_WRLOCK(&cc_monitor_backends);
  1055. AST_RWLIST_INSERT_TAIL(&cc_monitor_backends, backend, next);
  1056. AST_RWLIST_UNLOCK(&cc_monitor_backends);
  1057. return 0;
  1058. }
  1059. static const struct ast_cc_monitor_callbacks *find_monitor_callbacks(const char * const type)
  1060. {
  1061. struct cc_monitor_backend *backend;
  1062. const struct ast_cc_monitor_callbacks *callbacks = NULL;
  1063. AST_RWLIST_RDLOCK(&cc_monitor_backends);
  1064. AST_RWLIST_TRAVERSE(&cc_monitor_backends, backend, next) {
  1065. if (!strcmp(backend->callbacks->type, type)) {
  1066. ast_log_dynamic_level(cc_logger_level, "Returning monitor backend %s\n", backend->callbacks->type);
  1067. callbacks = backend->callbacks;
  1068. break;
  1069. }
  1070. }
  1071. AST_RWLIST_UNLOCK(&cc_monitor_backends);
  1072. return callbacks;
  1073. }
  1074. void ast_cc_monitor_unregister(const struct ast_cc_monitor_callbacks *callbacks)
  1075. {
  1076. struct cc_monitor_backend *backend;
  1077. AST_RWLIST_WRLOCK(&cc_monitor_backends);
  1078. AST_RWLIST_TRAVERSE_SAFE_BEGIN(&cc_monitor_backends, backend, next) {
  1079. if (backend->callbacks == callbacks) {
  1080. AST_RWLIST_REMOVE_CURRENT(next);
  1081. ast_free(backend);
  1082. break;
  1083. }
  1084. }
  1085. AST_RWLIST_TRAVERSE_SAFE_END;
  1086. AST_RWLIST_UNLOCK(&cc_monitor_backends);
  1087. }
  1088. struct cc_agent_backend {
  1089. AST_LIST_ENTRY(cc_agent_backend) next;
  1090. const struct ast_cc_agent_callbacks *callbacks;
  1091. };
  1092. AST_RWLIST_HEAD_STATIC(cc_agent_backends, cc_agent_backend);
  1093. int ast_cc_agent_register(const struct ast_cc_agent_callbacks *callbacks)
  1094. {
  1095. struct cc_agent_backend *backend = ast_calloc(1, sizeof(*backend));
  1096. if (!backend) {
  1097. return -1;
  1098. }
  1099. backend->callbacks = callbacks;
  1100. AST_RWLIST_WRLOCK(&cc_agent_backends);
  1101. AST_RWLIST_INSERT_TAIL(&cc_agent_backends, backend, next);
  1102. AST_RWLIST_UNLOCK(&cc_agent_backends);
  1103. return 0;
  1104. }
  1105. void ast_cc_agent_unregister(const struct ast_cc_agent_callbacks *callbacks)
  1106. {
  1107. struct cc_agent_backend *backend;
  1108. AST_RWLIST_WRLOCK(&cc_agent_backends);
  1109. AST_RWLIST_TRAVERSE_SAFE_BEGIN(&cc_agent_backends, backend, next) {
  1110. if (backend->callbacks == callbacks) {
  1111. AST_RWLIST_REMOVE_CURRENT(next);
  1112. ast_free(backend);
  1113. break;
  1114. }
  1115. }
  1116. AST_RWLIST_TRAVERSE_SAFE_END;
  1117. AST_RWLIST_UNLOCK(&cc_agent_backends);
  1118. }
  1119. static const struct ast_cc_agent_callbacks *find_agent_callbacks(struct ast_channel *chan)
  1120. {
  1121. struct cc_agent_backend *backend;
  1122. const struct ast_cc_agent_callbacks *callbacks = NULL;
  1123. struct ast_cc_config_params *cc_params;
  1124. char type[32];
  1125. cc_params = ast_channel_get_cc_config_params(chan);
  1126. if (!cc_params) {
  1127. return NULL;
  1128. }
  1129. switch (ast_get_cc_agent_policy(cc_params)) {
  1130. case AST_CC_AGENT_GENERIC:
  1131. ast_copy_string(type, "generic", sizeof(type));
  1132. break;
  1133. case AST_CC_AGENT_NATIVE:
  1134. ast_channel_get_cc_agent_type(chan, type, sizeof(type));
  1135. break;
  1136. default:
  1137. ast_log_dynamic_level(cc_logger_level, "Not returning agent callbacks since this channel is configured not to have a CC agent\n");
  1138. return NULL;
  1139. }
  1140. AST_RWLIST_RDLOCK(&cc_agent_backends);
  1141. AST_RWLIST_TRAVERSE(&cc_agent_backends, backend, next) {
  1142. if (!strcmp(backend->callbacks->type, type)) {
  1143. ast_log_dynamic_level(cc_logger_level, "Returning agent backend %s\n", backend->callbacks->type);
  1144. callbacks = backend->callbacks;
  1145. break;
  1146. }
  1147. }
  1148. AST_RWLIST_UNLOCK(&cc_agent_backends);
  1149. return callbacks;
  1150. }
  1151. /*!
  1152. * \internal
  1153. * \brief Determine if the given device state is considered available by generic CCSS.
  1154. * \since 1.8
  1155. *
  1156. * \param state Device state to test.
  1157. *
  1158. * \return TRUE if the given device state is considered available by generic CCSS.
  1159. */
  1160. static int cc_generic_is_device_available(enum ast_device_state state)
  1161. {
  1162. return state == AST_DEVICE_NOT_INUSE || state == AST_DEVICE_UNKNOWN;
  1163. }
  1164. static int cc_generic_monitor_request_cc(struct ast_cc_monitor *monitor, int *available_timer_id);
  1165. static int cc_generic_monitor_suspend(struct ast_cc_monitor *monitor);
  1166. static int cc_generic_monitor_unsuspend(struct ast_cc_monitor *monitor);
  1167. static int cc_generic_monitor_cancel_available_timer(struct ast_cc_monitor *monitor, int *sched_id);
  1168. static void cc_generic_monitor_destructor(void *private_data);
  1169. static struct ast_cc_monitor_callbacks generic_monitor_cbs = {
  1170. .type = "generic",
  1171. .request_cc = cc_generic_monitor_request_cc,
  1172. .suspend = cc_generic_monitor_suspend,
  1173. .unsuspend = cc_generic_monitor_unsuspend,
  1174. .cancel_available_timer = cc_generic_monitor_cancel_available_timer,
  1175. .destructor = cc_generic_monitor_destructor,
  1176. };
  1177. struct ao2_container *generic_monitors;
  1178. struct generic_monitor_instance {
  1179. int core_id;
  1180. int is_suspended;
  1181. int monitoring;
  1182. AST_LIST_ENTRY(generic_monitor_instance) next;
  1183. };
  1184. struct generic_monitor_instance_list {
  1185. const char *device_name;
  1186. enum ast_device_state current_state;
  1187. /* If there are multiple instances monitoring the
  1188. * same device and one should fail, we need to know
  1189. * whether to signal that the device can be recalled.
  1190. * The problem is that the device state is not enough
  1191. * to check. If a caller has requested CCNR, then the
  1192. * fact that the device is available does not indicate
  1193. * that the device is ready to be recalled. Instead, as
  1194. * soon as one instance of the monitor becomes available
  1195. * for a recall, we mark the entire list as being fit
  1196. * for recall. If a CCNR request comes in, then we will
  1197. * have to mark the list as unfit for recall since this
  1198. * is a clear indicator that the person at the monitored
  1199. * device has gone away and is actuall not fit to be
  1200. * recalled
  1201. */
  1202. int fit_for_recall;
  1203. struct stasis_subscription *sub;
  1204. AST_LIST_HEAD_NOLOCK(, generic_monitor_instance) list;
  1205. };
  1206. /*!
  1207. * \brief private data for generic device monitor
  1208. */
  1209. struct generic_monitor_pvt {
  1210. /*!
  1211. * We need the device name during destruction so we
  1212. * can find the appropriate item to destroy.
  1213. */
  1214. const char *device_name;
  1215. /*!
  1216. * We need the core ID for similar reasons. Once we
  1217. * find the appropriate item in our ao2_container, we
  1218. * need to remove the appropriate cc_monitor from the
  1219. * list of monitors.
  1220. */
  1221. int core_id;
  1222. };
  1223. AO2_STRING_FIELD_HASH_FN(generic_monitor_instance_list, device_name)
  1224. AO2_STRING_FIELD_CMP_FN(generic_monitor_instance_list, device_name)
  1225. static struct generic_monitor_instance_list *find_generic_monitor_instance_list(const char * const device_name)
  1226. {
  1227. struct generic_monitor_instance_list finder = {0};
  1228. char *uppertech = ast_strdupa(device_name);
  1229. ast_tech_to_upper(uppertech);
  1230. finder.device_name = uppertech;
  1231. return ao2_t_find(generic_monitors, &finder, OBJ_POINTER, "Finding generic monitor instance list");
  1232. }
  1233. static void generic_monitor_instance_list_destructor(void *obj)
  1234. {
  1235. struct generic_monitor_instance_list *generic_list = obj;
  1236. struct generic_monitor_instance *generic_instance;
  1237. generic_list->sub = stasis_unsubscribe(generic_list->sub);
  1238. while ((generic_instance = AST_LIST_REMOVE_HEAD(&generic_list->list, next))) {
  1239. ast_free(generic_instance);
  1240. }
  1241. ast_free((char *)generic_list->device_name);
  1242. }
  1243. static void generic_monitor_devstate_cb(void *userdata, struct stasis_subscription *sub, struct stasis_message *msg);
  1244. static struct generic_monitor_instance_list *create_new_generic_list(struct ast_cc_monitor *monitor)
  1245. {
  1246. struct generic_monitor_instance_list *generic_list = ao2_t_alloc(sizeof(*generic_list),
  1247. generic_monitor_instance_list_destructor, "allocate generic monitor instance list");
  1248. char * device_name;
  1249. struct stasis_topic *device_specific_topic;
  1250. if (!generic_list) {
  1251. return NULL;
  1252. }
  1253. if (!(device_name = ast_strdup(monitor->interface->device_name))) {
  1254. cc_unref(generic_list, "Failed to strdup the monitor's device name");
  1255. return NULL;
  1256. }
  1257. ast_tech_to_upper(device_name);
  1258. generic_list->device_name = device_name;
  1259. device_specific_topic = ast_device_state_topic(device_name);
  1260. if (!device_specific_topic) {
  1261. return NULL;
  1262. }
  1263. if (!(generic_list->sub = stasis_subscribe(device_specific_topic, generic_monitor_devstate_cb, NULL))) {
  1264. cc_unref(generic_list, "Failed to subscribe to device state");
  1265. return NULL;
  1266. }
  1267. stasis_subscription_accept_message_type(generic_list->sub, ast_device_state_message_type());
  1268. stasis_subscription_set_filter(generic_list->sub, STASIS_SUBSCRIPTION_FILTER_SELECTIVE);
  1269. generic_list->current_state = ast_device_state(monitor->interface->device_name);
  1270. ao2_t_link(generic_monitors, generic_list, "linking new generic monitor instance list");
  1271. return generic_list;
  1272. }
  1273. static int generic_monitor_devstate_tp_cb(void *data)
  1274. {
  1275. RAII_VAR(struct ast_device_state_message *, dev_state, data, ao2_cleanup);
  1276. enum ast_device_state new_state = dev_state->state;
  1277. enum ast_device_state previous_state;
  1278. struct generic_monitor_instance_list *generic_list;
  1279. struct generic_monitor_instance *generic_instance;
  1280. if (!(generic_list = find_generic_monitor_instance_list(dev_state->device))) {
  1281. /* The most likely cause for this is that we destroyed the monitor in the
  1282. * time between subscribing to its device state and the time this executes.
  1283. * Not really a big deal.
  1284. */
  1285. return 0;
  1286. }
  1287. if (generic_list->current_state == new_state) {
  1288. /* The device state hasn't actually changed, so we don't really care */
  1289. cc_unref(generic_list, "Kill reference of generic list in devstate taskprocessor callback");
  1290. return 0;
  1291. }
  1292. previous_state = generic_list->current_state;
  1293. generic_list->current_state = new_state;
  1294. if (cc_generic_is_device_available(new_state) &&
  1295. (previous_state == AST_DEVICE_INUSE || previous_state == AST_DEVICE_UNAVAILABLE ||
  1296. previous_state == AST_DEVICE_BUSY)) {
  1297. AST_LIST_TRAVERSE(&generic_list->list, generic_instance, next) {
  1298. if (!generic_instance->is_suspended && generic_instance->monitoring) {
  1299. generic_instance->monitoring = 0;
  1300. generic_list->fit_for_recall = 1;
  1301. ast_cc_monitor_callee_available(generic_instance->core_id, "Generic monitored party has become available");
  1302. break;
  1303. }
  1304. }
  1305. }
  1306. cc_unref(generic_list, "Kill reference of generic list in devstate taskprocessor callback");
  1307. return 0;
  1308. }
  1309. static void generic_monitor_devstate_cb(void *userdata, struct stasis_subscription *sub, struct stasis_message *msg)
  1310. {
  1311. /* Wow, it's cool that we've picked up on a state change, but we really want
  1312. * the actual work to be done in the core's taskprocessor execution thread
  1313. * so that all monitor operations can be serialized. Locks?! We don't need
  1314. * no steenkin' locks!
  1315. */
  1316. struct ast_device_state_message *dev_state;
  1317. if (ast_device_state_message_type() != stasis_message_type(msg)) {
  1318. return;
  1319. }
  1320. dev_state = stasis_message_data(msg);
  1321. if (dev_state->eid) {
  1322. /* ignore non-aggregate states */
  1323. return;
  1324. }
  1325. ao2_t_ref(dev_state, +1, "Bumping dev_state ref for cc_core_taskprocessor");
  1326. if (ast_taskprocessor_push(cc_core_taskprocessor, generic_monitor_devstate_tp_cb, dev_state)) {
  1327. ao2_cleanup(dev_state);
  1328. return;
  1329. }
  1330. }
  1331. int ast_cc_available_timer_expire(const void *data)
  1332. {
  1333. struct ast_cc_monitor *monitor = (struct ast_cc_monitor *) data;
  1334. int res;
  1335. monitor->available_timer_id = -1;
  1336. res = ast_cc_monitor_failed(monitor->core_id, monitor->interface->device_name, "Available timer expired for monitor");
  1337. cc_unref(monitor, "Unref reference from scheduler\n");
  1338. return res;
  1339. }
  1340. static int cc_generic_monitor_request_cc(struct ast_cc_monitor *monitor, int *available_timer_id)
  1341. {
  1342. struct generic_monitor_instance_list *generic_list;
  1343. struct generic_monitor_instance *generic_instance;
  1344. struct generic_monitor_pvt *gen_mon_pvt;
  1345. enum ast_cc_service_type service = monitor->service_offered;
  1346. int when;
  1347. /* First things first. Native channel drivers will have their private data allocated
  1348. * at the time that they tell the core that they can offer CC. Generic is quite a bit
  1349. * different, and we wait until this point to allocate our private data.
  1350. */
  1351. if (!(gen_mon_pvt = ast_calloc(1, sizeof(*gen_mon_pvt)))) {
  1352. return -1;
  1353. }
  1354. if (!(gen_mon_pvt->device_name = ast_strdup(monitor->interface->device_name))) {
  1355. ast_free(gen_mon_pvt);
  1356. return -1;
  1357. }
  1358. gen_mon_pvt->core_id = monitor->core_id;
  1359. monitor->private_data = gen_mon_pvt;
  1360. if (!(generic_list = find_generic_monitor_instance_list(monitor->interface->device_name))) {
  1361. if (!(generic_list = create_new_generic_list(monitor))) {
  1362. return -1;
  1363. }
  1364. }
  1365. if (!(generic_instance = ast_calloc(1, sizeof(*generic_instance)))) {
  1366. /* The generic monitor destructor will take care of the appropriate
  1367. * deallocations
  1368. */
  1369. cc_unref(generic_list, "Generic monitor instance failed to allocate");
  1370. return -1;
  1371. }
  1372. generic_instance->core_id = monitor->core_id;
  1373. generic_instance->monitoring = 1;
  1374. AST_LIST_INSERT_TAIL(&generic_list->list, generic_instance, next);
  1375. when = service == AST_CC_CCBS ? ast_get_ccbs_available_timer(monitor->interface->config_params) :
  1376. ast_get_ccnr_available_timer(monitor->interface->config_params);
  1377. *available_timer_id = ast_sched_add(cc_sched_context, when * 1000,
  1378. ast_cc_available_timer_expire, cc_ref(monitor, "Give the scheduler a monitor reference"));
  1379. if (*available_timer_id == -1) {
  1380. cc_unref(monitor, "Failed to schedule available timer. (monitor)");
  1381. cc_unref(generic_list, "Failed to schedule available timer. (generic_list)");
  1382. return -1;
  1383. }
  1384. /* If the new instance was created as CCNR, then that means this device is not currently
  1385. * fit for recall even if it previously was.
  1386. */
  1387. if (service == AST_CC_CCNR || service == AST_CC_CCNL) {
  1388. generic_list->fit_for_recall = 0;
  1389. }
  1390. ast_cc_monitor_request_acked(monitor->core_id, "Generic monitor for %s subscribed to device state.",
  1391. monitor->interface->device_name);
  1392. cc_unref(generic_list, "Finished with monitor instance reference in request cc callback");
  1393. return 0;
  1394. }
  1395. static int cc_generic_monitor_suspend(struct ast_cc_monitor *monitor)
  1396. {
  1397. struct generic_monitor_instance_list *generic_list;
  1398. struct generic_monitor_instance *generic_instance;
  1399. enum ast_device_state state = ast_device_state(monitor->interface->device_name);
  1400. if (!(generic_list = find_generic_monitor_instance_list(monitor->interface->device_name))) {
  1401. return -1;
  1402. }
  1403. /* First we need to mark this particular monitor as being suspended. */
  1404. AST_LIST_TRAVERSE(&generic_list->list, generic_instance, next) {
  1405. if (generic_instance->core_id == monitor->core_id) {
  1406. generic_instance->is_suspended = 1;
  1407. break;
  1408. }
  1409. }
  1410. /* If the device being suspended is currently in use, then we don't need to
  1411. * take any further actions
  1412. */
  1413. if (!cc_generic_is_device_available(state)) {
  1414. cc_unref(generic_list, "Device is in use. Nothing to do. Unref generic list.");
  1415. return 0;
  1416. }
  1417. /* If the device is not in use, though, then it may be possible to report the
  1418. * device's availability using a different monitor which is monitoring the
  1419. * same device
  1420. */
  1421. AST_LIST_TRAVERSE(&generic_list->list, generic_instance, next) {
  1422. if (!generic_instance->is_suspended) {
  1423. ast_cc_monitor_callee_available(generic_instance->core_id, "Generic monitored party has become available");
  1424. break;
  1425. }
  1426. }
  1427. cc_unref(generic_list, "Done with generic list in suspend callback");
  1428. return 0;
  1429. }
  1430. static int cc_generic_monitor_unsuspend(struct ast_cc_monitor *monitor)
  1431. {
  1432. struct generic_monitor_instance *generic_instance;
  1433. struct generic_monitor_instance_list *generic_list = find_generic_monitor_instance_list(monitor->interface->device_name);
  1434. enum ast_device_state state = ast_device_state(monitor->interface->device_name);
  1435. if (!generic_list) {
  1436. return -1;
  1437. }
  1438. /* If the device is currently available, we can immediately announce
  1439. * its availability
  1440. */
  1441. if (cc_generic_is_device_available(state)) {
  1442. ast_cc_monitor_callee_available(monitor->core_id, "Generic monitored party has become available");
  1443. }
  1444. /* In addition, we need to mark this generic_monitor_instance as not being suspended anymore */
  1445. AST_LIST_TRAVERSE(&generic_list->list, generic_instance, next) {
  1446. if (generic_instance->core_id == monitor->core_id) {
  1447. generic_instance->is_suspended = 0;
  1448. generic_instance->monitoring = 1;
  1449. break;
  1450. }
  1451. }
  1452. cc_unref(generic_list, "Done with generic list in cc_generic_monitor_unsuspend");
  1453. return 0;
  1454. }
  1455. static int cc_generic_monitor_cancel_available_timer(struct ast_cc_monitor *monitor, int *sched_id)
  1456. {
  1457. ast_assert(sched_id != NULL);
  1458. if (*sched_id == -1) {
  1459. return 0;
  1460. }
  1461. ast_log_dynamic_level(cc_logger_level, "Core %d: Canceling generic monitor available timer for monitor %s\n",
  1462. monitor->core_id, monitor->interface->device_name);
  1463. if (!ast_sched_del(cc_sched_context, *sched_id)) {
  1464. cc_unref(monitor, "Remove scheduler's reference to the monitor");
  1465. }
  1466. *sched_id = -1;
  1467. return 0;
  1468. }
  1469. static void cc_generic_monitor_destructor(void *private_data)
  1470. {
  1471. struct generic_monitor_pvt *gen_mon_pvt = private_data;
  1472. struct generic_monitor_instance_list *generic_list;
  1473. struct generic_monitor_instance *generic_instance;
  1474. if (!private_data) {
  1475. /* If the private data is NULL, that means that the monitor hasn't even
  1476. * been created yet, but that the destructor was called. While this sort
  1477. * of behavior is useful for native monitors, with a generic one, there is
  1478. * nothing in particular to do.
  1479. */
  1480. return;
  1481. }
  1482. ast_log_dynamic_level(cc_logger_level, "Core %d: Destroying generic monitor %s\n",
  1483. gen_mon_pvt->core_id, gen_mon_pvt->device_name);
  1484. if (!(generic_list = find_generic_monitor_instance_list(gen_mon_pvt->device_name))) {
  1485. /* If there's no generic list, that means that the monitor is being destroyed
  1486. * before we actually got to request CC. Not a biggie. Same in the situation
  1487. * below if the list traversal should complete without finding an entry.
  1488. */
  1489. ast_free((char *)gen_mon_pvt->device_name);
  1490. ast_free(gen_mon_pvt);
  1491. return;
  1492. }
  1493. AST_LIST_TRAVERSE_SAFE_BEGIN(&generic_list->list, generic_instance, next) {
  1494. if (generic_instance->core_id == gen_mon_pvt->core_id) {
  1495. AST_LIST_REMOVE_CURRENT(next);
  1496. ast_free(generic_instance);
  1497. break;
  1498. }
  1499. }
  1500. AST_LIST_TRAVERSE_SAFE_END;
  1501. if (AST_LIST_EMPTY(&generic_list->list)) {
  1502. /* No more monitors with this device name exist. Time to unlink this
  1503. * list from the container
  1504. */
  1505. ao2_t_unlink(generic_monitors, generic_list, "Generic list is empty. Unlink it from the container");
  1506. } else {
  1507. /* There are still instances for this particular device. The situation
  1508. * may be that we were attempting a CC recall and a failure occurred, perhaps
  1509. * on the agent side. If a failure happens here and the device being monitored
  1510. * is available, then we need to signal on the first unsuspended instance that
  1511. * the device is available for recall.
  1512. */
  1513. /* First things first. We don't even want to consider this action if
  1514. * the device in question isn't available right now.
  1515. */
  1516. if (generic_list->fit_for_recall
  1517. && cc_generic_is_device_available(generic_list->current_state)) {
  1518. AST_LIST_TRAVERSE(&generic_list->list, generic_instance, next) {
  1519. if (!generic_instance->is_suspended && generic_instance->monitoring) {
  1520. ast_cc_monitor_callee_available(generic_instance->core_id, "Signaling generic monitor "
  1521. "availability due to other instance's failure.");
  1522. break;
  1523. }
  1524. }
  1525. }
  1526. }
  1527. cc_unref(generic_list, "Done with generic list in generic monitor destructor");
  1528. ast_free((char *)gen_mon_pvt->device_name);
  1529. ast_free(gen_mon_pvt);
  1530. }
  1531. static void cc_interface_destroy(void *data)
  1532. {
  1533. struct ast_cc_interface *interface = data;
  1534. ast_log_dynamic_level(cc_logger_level, "Destroying cc interface %s\n", interface->device_name);
  1535. ast_cc_config_params_destroy(interface->config_params);
  1536. }
  1537. /*!
  1538. * \brief Data regarding an extension monitor's child's dialstrings
  1539. *
  1540. * \details
  1541. * In developing CCSS, we had most aspects of its operation finished,
  1542. * but there was one looming problem that we had failed to get right.
  1543. * In our design document, we stated that when a CC recall occurs, all
  1544. * endpoints that had been dialed originally would be called back.
  1545. * Unfortunately, our implementation only allowed for devices which had
  1546. * active monitors to inhabit the CC_INTERFACES channel variable, thus
  1547. * making the automated recall only call monitored devices.
  1548. *
  1549. * Devices that were not CC-capable, or devices which failed CC at some
  1550. * point during the process would not make it into the CC_INTERFACES
  1551. * channel variable. This struct is meant as a remedy for the problem.
  1552. */
  1553. struct extension_child_dialstring {
  1554. /*!
  1555. * \brief the original dialstring used to call a particular device
  1556. *
  1557. * \details
  1558. * When someone dials a particular endpoint, the dialstring used in
  1559. * the dialplan is copied into this buffer. What's important here is
  1560. * that this is the ORIGINAL dialstring, not the dialstring saved on
  1561. * a device monitor. The dialstring on a device monitor is what should
  1562. * be used when recalling that device. The two dialstrings may not be
  1563. * the same.
  1564. *
  1565. * By keeping a copy of the original dialstring used, we can fall back
  1566. * to using it if the device either does not ever offer CC or if the
  1567. * device at some point fails for some reason, such as a timer expiration.
  1568. */
  1569. char original_dialstring[AST_CHANNEL_NAME];
  1570. /*!
  1571. * \brief The name of the device being dialed
  1572. *
  1573. * \details
  1574. * This serves mainly as a key when searching for a particular dialstring.
  1575. * For instance, let's say that we have called device SIP/400\@somepeer. This
  1576. * device offers call completion, but then due to some unforeseen circumstance,
  1577. * this device backs out and makes CC unavailable. When that happens, we need
  1578. * to find the dialstring that corresponds to that device, and we use the
  1579. * stored device name as a way to find it.
  1580. *
  1581. * \note There is one particular case where the device name stored here
  1582. * will be empty. This is the case where we fail to request a channel, but we
  1583. * still can make use of generic call completion. In such a case, since we never
  1584. * were able to request the channel, we can't find what its device name is. In
  1585. * this case, however, it is not important because the dialstring is guaranteed
  1586. * to be the same both here and in the device monitor.
  1587. */
  1588. char device_name[AST_CHANNEL_NAME];
  1589. /*!
  1590. * \brief Is this structure valid for use in CC_INTERFACES?
  1591. *
  1592. * \details
  1593. * When this structure is first created, all information stored here is planned
  1594. * to be used, so we set the is_valid flag. However, if a device offers call
  1595. * completion, it will potentially have its own dialstring to use for the recall,
  1596. * so we find this structure and clear the is_valid flag. By clearing the is_valid
  1597. * flag, we won't try to populate the CC_INTERFACES variable with the dialstring
  1598. * stored in this struct. Now, if later, the device which had offered CC should fail,
  1599. * perhaps due to a timer expiration, then we need to re-set the is_valid flag. This
  1600. * way, we still will end up placing a call to the device again, and the dialstring
  1601. * used will be the same as was originally used.
  1602. */
  1603. int is_valid;
  1604. AST_LIST_ENTRY(extension_child_dialstring) next;
  1605. };
  1606. /*!
  1607. * \brief Private data for an extension monitor
  1608. */
  1609. struct extension_monitor_pvt {
  1610. AST_LIST_HEAD_NOLOCK(, extension_child_dialstring) child_dialstrings;
  1611. };
  1612. static void cc_extension_monitor_destructor(void *private_data)
  1613. {
  1614. struct extension_monitor_pvt *extension_pvt = private_data;
  1615. struct extension_child_dialstring *child_dialstring;
  1616. /* This shouldn't be possible, but I'm paranoid */
  1617. if (!extension_pvt) {
  1618. return;
  1619. }
  1620. while ((child_dialstring = AST_LIST_REMOVE_HEAD(&extension_pvt->child_dialstrings, next))) {
  1621. ast_free(child_dialstring);
  1622. }
  1623. ast_free(extension_pvt);
  1624. }
  1625. static void cc_monitor_destroy(void *data)
  1626. {
  1627. struct ast_cc_monitor *monitor = data;
  1628. /* During the monitor creation process, it is possible for this
  1629. * function to be called prior to when callbacks are assigned
  1630. * to the monitor. Also, extension monitors do not have callbacks
  1631. * assigned to them, so we wouldn't want to segfault when we try
  1632. * to destroy one of them.
  1633. */
  1634. ast_log_dynamic_level(cc_logger_level, "Core %d: Calling destructor for monitor %s\n",
  1635. monitor->core_id, monitor->interface->device_name);
  1636. if (monitor->interface->monitor_class == AST_CC_EXTENSION_MONITOR) {
  1637. cc_extension_monitor_destructor(monitor->private_data);
  1638. }
  1639. if (monitor->callbacks) {
  1640. monitor->callbacks->destructor(monitor->private_data);
  1641. }
  1642. cc_unref(monitor->interface, "Unreffing tree's reference to interface");
  1643. ast_free(monitor->dialstring);
  1644. }
  1645. static void cc_interface_tree_destroy(void *data)
  1646. {
  1647. struct cc_monitor_tree *cc_interface_tree = data;
  1648. struct ast_cc_monitor *monitor;
  1649. while ((monitor = AST_LIST_REMOVE_HEAD(cc_interface_tree, next))) {
  1650. if (monitor->callbacks) {
  1651. monitor->callbacks->cancel_available_timer(monitor, &monitor->available_timer_id);
  1652. }
  1653. cc_unref(monitor, "Destroying all monitors");
  1654. }
  1655. AST_LIST_HEAD_DESTROY(cc_interface_tree);
  1656. }
  1657. /*!
  1658. * This counter is used for assigning unique ids
  1659. * to CC-enabled dialed interfaces.
  1660. */
  1661. static int dialed_cc_interface_counter;
  1662. /*!
  1663. * \internal
  1664. * \brief data stored in CC datastore
  1665. *
  1666. * The datastore creates a list of interfaces that were
  1667. * dialed, including both extensions and devices. In addition
  1668. * to the intrinsic data of the tree, some extra information
  1669. * is needed for use by app_dial.
  1670. */
  1671. struct dialed_cc_interfaces {
  1672. /*!
  1673. * This value serves a dual-purpose. When dial starts, if the
  1674. * dialed_cc_interfaces datastore currently exists on the calling
  1675. * channel, then the dial_parent_id will serve as a means of
  1676. * letting the new extension cc_monitor we create know
  1677. * who his parent is. This value will be the extension
  1678. * cc_monitor that dialed the local channel that resulted
  1679. * in the new Dial app being called.
  1680. *
  1681. * In addition, once an extension cc_monitor is created,
  1682. * the dial_parent_id will be changed to the id of that newly
  1683. * created interface. This way, device interfaces created from
  1684. * receiving AST_CONTROL_CC frames can use this field to determine
  1685. * who their parent extension interface should be.
  1686. */
  1687. unsigned int dial_parent_id;
  1688. /*!
  1689. * Identifier for the potential CC request that may be made
  1690. * based on this call. Even though an instance of the core may
  1691. * not be made (since the caller may not request CC), we allocate
  1692. * a new core_id at the beginning of the call so that recipient
  1693. * channel drivers can have the information handy just in case
  1694. * the caller does end up requesting CC.
  1695. */
  1696. int core_id;
  1697. /*!
  1698. * When a new Dial application is started, and the datastore
  1699. * already exists on the channel, we can determine if we
  1700. * should be adding any new interface information to tree.
  1701. */
  1702. char ignore;
  1703. /*!
  1704. * When it comes time to offer CC to the caller, we only want to offer
  1705. * it to the original incoming channel. For nested Dials and outbound
  1706. * channels, it is incorrect to attempt such a thing. This flag indicates
  1707. * if the channel to which this datastore is attached may be legally
  1708. * offered CC when the call is finished.
  1709. */
  1710. char is_original_caller;
  1711. /*!
  1712. * Reference-counted "tree" of interfaces.
  1713. */
  1714. struct cc_monitor_tree *interface_tree;
  1715. };
  1716. /*!
  1717. * \internal
  1718. * \brief Destructor function for cc_interfaces datastore
  1719. *
  1720. * This function will free the actual datastore and drop
  1721. * the refcount for the monitor tree by one. In cases
  1722. * where CC can actually be used, this unref will not
  1723. * result in the destruction of the monitor tree, because
  1724. * the CC core will still have a reference.
  1725. *
  1726. * \param data The dialed_cc_interfaces struct to destroy
  1727. */
  1728. static void dialed_cc_interfaces_destroy(void *data)
  1729. {
  1730. struct dialed_cc_interfaces *cc_interfaces = data;
  1731. cc_unref(cc_interfaces->interface_tree, "Unref dial's ref to monitor tree");
  1732. ast_free(cc_interfaces);
  1733. }
  1734. /*!
  1735. * \internal
  1736. * \brief Duplicate callback for cc_interfaces datastore
  1737. *
  1738. * Integers are copied by value, but the monitor tree
  1739. * is done via a shallow copy and a bump of the refcount.
  1740. * This way, sub-Dials will be appending interfaces onto
  1741. * the same list as this call to Dial.
  1742. *
  1743. * \param data The old dialed_cc_interfaces we want to copy
  1744. * \retval NULL Could not allocate memory for new dialed_cc_interfaces
  1745. * \retval non-NULL The new copy of the dialed_cc_interfaces
  1746. */
  1747. static void *dialed_cc_interfaces_duplicate(void *data)
  1748. {
  1749. struct dialed_cc_interfaces *old_cc_interfaces = data;
  1750. struct dialed_cc_interfaces *new_cc_interfaces = ast_calloc(1, sizeof(*new_cc_interfaces));
  1751. if (!new_cc_interfaces) {
  1752. return NULL;
  1753. }
  1754. new_cc_interfaces->ignore = old_cc_interfaces->ignore;
  1755. new_cc_interfaces->dial_parent_id = old_cc_interfaces->dial_parent_id;
  1756. new_cc_interfaces->is_original_caller = 0;
  1757. cc_ref(old_cc_interfaces->interface_tree, "New ref due to duplication of monitor tree");
  1758. new_cc_interfaces->core_id = old_cc_interfaces->core_id;
  1759. new_cc_interfaces->interface_tree = old_cc_interfaces->interface_tree;
  1760. return new_cc_interfaces;
  1761. }
  1762. /*!
  1763. * \internal
  1764. * \brief information regarding the dialed_cc_interfaces datastore
  1765. *
  1766. * The dialed_cc_interfaces datastore is responsible for keeping track
  1767. * of what CC-enabled interfaces have been dialed by the caller. For
  1768. * more information regarding the actual structure of the tree, see
  1769. * the documentation provided in include/asterisk/ccss.h
  1770. */
  1771. static const struct ast_datastore_info dialed_cc_interfaces_info = {
  1772. .type = "Dial CC Interfaces",
  1773. .duplicate = dialed_cc_interfaces_duplicate,
  1774. .destroy = dialed_cc_interfaces_destroy,
  1775. };
  1776. static struct extension_monitor_pvt *extension_monitor_pvt_init(void)
  1777. {
  1778. struct extension_monitor_pvt *ext_pvt = ast_calloc(1, sizeof(*ext_pvt));
  1779. if (!ext_pvt) {
  1780. return NULL;
  1781. }
  1782. AST_LIST_HEAD_INIT_NOLOCK(&ext_pvt->child_dialstrings);
  1783. return ext_pvt;
  1784. }
  1785. void ast_cc_extension_monitor_add_dialstring(struct ast_channel *incoming, const char * const dialstring, const char * const device_name)
  1786. {
  1787. struct ast_datastore *cc_datastore;
  1788. struct dialed_cc_interfaces *cc_interfaces;
  1789. struct ast_cc_monitor *monitor;
  1790. struct extension_monitor_pvt *extension_pvt;
  1791. struct extension_child_dialstring *child_dialstring;
  1792. struct cc_monitor_tree *interface_tree;
  1793. int id;
  1794. ast_channel_lock(incoming);
  1795. if (!(cc_datastore = ast_channel_datastore_find(incoming, &dialed_cc_interfaces_info, NULL))) {
  1796. ast_channel_unlock(incoming);
  1797. return;
  1798. }
  1799. cc_interfaces = cc_datastore->data;
  1800. interface_tree = cc_interfaces->interface_tree;
  1801. id = cc_interfaces->dial_parent_id;
  1802. ast_channel_unlock(incoming);
  1803. AST_LIST_LOCK(interface_tree);
  1804. AST_LIST_TRAVERSE(interface_tree, monitor, next) {
  1805. if (monitor->id == id) {
  1806. break;
  1807. }
  1808. }
  1809. if (!monitor) {
  1810. AST_LIST_UNLOCK(interface_tree);
  1811. return;
  1812. }
  1813. extension_pvt = monitor->private_data;
  1814. if (!(child_dialstring = ast_calloc(1, sizeof(*child_dialstring)))) {
  1815. AST_LIST_UNLOCK(interface_tree);
  1816. return;
  1817. }
  1818. ast_copy_string(child_dialstring->original_dialstring, dialstring, sizeof(child_dialstring->original_dialstring));
  1819. ast_copy_string(child_dialstring->device_name, device_name, sizeof(child_dialstring->device_name));
  1820. child_dialstring->is_valid = 1;
  1821. AST_LIST_INSERT_TAIL(&extension_pvt->child_dialstrings, child_dialstring, next);
  1822. AST_LIST_UNLOCK(interface_tree);
  1823. }
  1824. static void cc_extension_monitor_change_is_valid(struct cc_core_instance *core_instance, unsigned int parent_id, const char * const device_name, int is_valid)
  1825. {
  1826. struct ast_cc_monitor *monitor_iter;
  1827. struct extension_monitor_pvt *extension_pvt;
  1828. struct extension_child_dialstring *child_dialstring;
  1829. AST_LIST_TRAVERSE(core_instance->monitors, monitor_iter, next) {
  1830. if (monitor_iter->id == parent_id) {
  1831. break;
  1832. }
  1833. }
  1834. if (!monitor_iter) {
  1835. return;
  1836. }
  1837. extension_pvt = monitor_iter->private_data;
  1838. AST_LIST_TRAVERSE(&extension_pvt->child_dialstrings, child_dialstring, next) {
  1839. if (!strcmp(child_dialstring->device_name, device_name)) {
  1840. child_dialstring->is_valid = is_valid;
  1841. break;
  1842. }
  1843. }
  1844. }
  1845. /*!
  1846. * \internal
  1847. * \brief Allocate and initialize an "extension" interface for CC purposes
  1848. *
  1849. * When app_dial starts, this function is called in order to set up the
  1850. * information about the extension in which this Dial is occurring. Any
  1851. * devices dialed will have this particular cc_monitor as a parent.
  1852. *
  1853. * \param exten Extension from which Dial is occurring
  1854. * \param context Context to which exten belongs
  1855. * \param parent_id What should we set the parent_id of this interface to?
  1856. * \retval NULL Memory allocation failure
  1857. * \retval non-NULL The newly-created cc_monitor for the extension
  1858. */
  1859. static struct ast_cc_monitor *cc_extension_monitor_init(const char * const exten, const char * const context, const unsigned int parent_id)
  1860. {
  1861. struct ast_str *str = ast_str_alloca(2 * AST_MAX_EXTENSION);
  1862. struct ast_cc_interface *cc_interface;
  1863. struct ast_cc_monitor *monitor;
  1864. ast_str_set(&str, 0, "%s@%s", exten, context);
  1865. if (!(cc_interface = ao2_t_alloc(sizeof(*cc_interface) + ast_str_strlen(str), cc_interface_destroy,
  1866. "Allocating new ast_cc_interface"))) {
  1867. return NULL;
  1868. }
  1869. if (!(monitor = ao2_t_alloc(sizeof(*monitor), cc_monitor_destroy, "Allocating new ast_cc_monitor"))) {
  1870. cc_unref(cc_interface, "failed to allocate the monitor, so unref the interface");
  1871. return NULL;
  1872. }
  1873. if (!(monitor->private_data = extension_monitor_pvt_init())) {
  1874. cc_unref(monitor, "Failed to initialize extension monitor private data. uref monitor");
  1875. cc_unref(cc_interface, "Failed to initialize extension monitor private data. unref cc_interface");
  1876. }
  1877. monitor->id = ast_atomic_fetchadd_int(&dialed_cc_interface_counter, +1);
  1878. monitor->parent_id = parent_id;
  1879. cc_interface->monitor_type = "extension";
  1880. cc_interface->monitor_class = AST_CC_EXTENSION_MONITOR;
  1881. strcpy(cc_interface->device_name, ast_str_buffer(str));
  1882. monitor->interface = cc_interface;
  1883. ast_log_dynamic_level(cc_logger_level, "Created an extension cc interface for '%s' with id %u and parent %u\n", cc_interface->device_name, monitor->id, monitor->parent_id);
  1884. return monitor;
  1885. }
  1886. /*!
  1887. * \internal
  1888. * \brief allocate dialed_cc_interfaces datastore and initialize fields
  1889. *
  1890. * This function is called when Situation 1 occurs in ast_cc_call_init.
  1891. * See that function for more information on what Situation 1 is.
  1892. *
  1893. * In this particular case, we have to do a lot of memory allocation in order
  1894. * to create the datastore, the data for the datastore, the tree of interfaces
  1895. * that we'll be adding to, and the initial extension interface for this Dial
  1896. * attempt.
  1897. *
  1898. * \param chan The channel onto which the datastore should be added.
  1899. * \retval -1 An error occurred
  1900. * \retval 0 Success
  1901. */
  1902. static int cc_interfaces_datastore_init(struct ast_channel *chan) {
  1903. struct dialed_cc_interfaces *interfaces;
  1904. struct ast_cc_monitor *monitor;
  1905. struct ast_datastore *dial_cc_datastore;
  1906. /*XXX This may be a bit controversial. In an attempt to not allocate
  1907. * extra resources, I make sure that a future request will be within
  1908. * limits. The problem here is that it is reasonable to think that
  1909. * even if we're not within the limits at this point, we may be by
  1910. * the time the requestor will have made his request. This may be
  1911. * deleted at some point.
  1912. */
  1913. if (!ast_cc_request_is_within_limits()) {
  1914. return 0;
  1915. }
  1916. if (!(interfaces = ast_calloc(1, sizeof(*interfaces)))) {
  1917. return -1;
  1918. }
  1919. if (!(monitor = cc_extension_monitor_init(S_OR(ast_channel_macroexten(chan), ast_channel_exten(chan)), S_OR(ast_channel_macrocontext(chan), ast_channel_context(chan)), 0))) {
  1920. ast_free(interfaces);
  1921. return -1;
  1922. }
  1923. if (!(dial_cc_datastore = ast_datastore_alloc(&dialed_cc_interfaces_info, NULL))) {
  1924. cc_unref(monitor, "Could not allocate the dialed interfaces datastore. Unreffing monitor");
  1925. ast_free(interfaces);
  1926. return -1;
  1927. }
  1928. if (!(interfaces->interface_tree = ao2_t_alloc(sizeof(*interfaces->interface_tree), cc_interface_tree_destroy,
  1929. "Allocate monitor tree"))) {
  1930. ast_datastore_free(dial_cc_datastore);
  1931. cc_unref(monitor, "Could not allocate monitor tree on dialed interfaces datastore. Unreffing monitor");
  1932. ast_free(interfaces);
  1933. return -1;
  1934. }
  1935. /* Finally, all that allocation is done... */
  1936. AST_LIST_HEAD_INIT(interfaces->interface_tree);
  1937. AST_LIST_INSERT_TAIL(interfaces->interface_tree, monitor, next);
  1938. cc_ref(monitor, "List's reference to extension monitor");
  1939. dial_cc_datastore->data = interfaces;
  1940. dial_cc_datastore->inheritance = DATASTORE_INHERIT_FOREVER;
  1941. interfaces->dial_parent_id = monitor->id;
  1942. interfaces->core_id = monitor->core_id = ast_atomic_fetchadd_int(&core_id_counter, +1);
  1943. interfaces->is_original_caller = 1;
  1944. ast_channel_lock(chan);
  1945. ast_channel_datastore_add(chan, dial_cc_datastore);
  1946. ast_channel_unlock(chan);
  1947. cc_unref(monitor, "Unreffing allocation's reference");
  1948. return 0;
  1949. }
  1950. /*!
  1951. * \internal
  1952. * \brief Call a monitor's destructor before the monitor has been allocated
  1953. * \since 1.8
  1954. *
  1955. * \param monitor_type The type of monitor callbacks to use when calling the destructor
  1956. * \param private_data Data allocated by a channel driver that must be freed
  1957. *
  1958. * \details
  1959. * I'll admit, this is a bit evil.
  1960. *
  1961. * When a channel driver determines that it can offer a call completion service to
  1962. * a caller, it is very likely that the channel driver will need to allocate some
  1963. * data so that when the time comes to request CC, the channel driver will have the
  1964. * necessary data at hand.
  1965. *
  1966. * The problem is that there are many places where failures may occur before the monitor
  1967. * has been properly allocated and had its callbacks assigned to it. If one of these
  1968. * failures should occur, then we still need to let the channel driver know that it
  1969. * must destroy the data that it allocated.
  1970. *
  1971. * \return Nothing
  1972. */
  1973. static void call_destructor_with_no_monitor(const char * const monitor_type, void *private_data)
  1974. {
  1975. const struct ast_cc_monitor_callbacks *monitor_callbacks = find_monitor_callbacks(monitor_type);
  1976. if (!monitor_callbacks) {
  1977. return;
  1978. }
  1979. monitor_callbacks->destructor(private_data);
  1980. }
  1981. /*!
  1982. * \internal
  1983. * \brief Allocate and intitialize a device cc_monitor
  1984. *
  1985. * For all intents and purposes, this is the same as
  1986. * cc_extension_monitor_init, except that there is only
  1987. * a single parameter used for naming the interface.
  1988. *
  1989. * This function is called when handling AST_CONTROL_CC frames.
  1990. * The device has reported that CC is possible, so we add it
  1991. * to the interface_tree.
  1992. *
  1993. * Note that it is not necessarily erroneous to add the same
  1994. * device to the tree twice. If the same device is called by
  1995. * two different extension during the same call, then
  1996. * that is a legitimate situation.
  1997. *
  1998. * \param device_name The name of the device being added to the tree
  1999. * \param dialstring The dialstring used to dial the device being added
  2000. * \param parent_id The parent of this new tree node.
  2001. * \retval NULL Memory allocation failure
  2002. * \retval non-NULL The new ast_cc_interface created.
  2003. */
  2004. static struct ast_cc_monitor *cc_device_monitor_init(const char * const device_name, const char * const dialstring, const struct cc_control_payload *cc_data, int core_id)
  2005. {
  2006. struct ast_cc_interface *cc_interface;
  2007. struct ast_cc_monitor *monitor;
  2008. size_t device_name_len = strlen(device_name);
  2009. int parent_id = cc_data->parent_interface_id;
  2010. if (!(cc_interface = ao2_t_alloc(sizeof(*cc_interface) + device_name_len, cc_interface_destroy,
  2011. "Allocating new ast_cc_interface"))) {
  2012. return NULL;
  2013. }
  2014. if (!(cc_interface->config_params = ast_cc_config_params_init())) {
  2015. cc_unref(cc_interface, "Failed to allocate config params, unref interface");
  2016. return NULL;
  2017. }
  2018. if (!(monitor = ao2_t_alloc(sizeof(*monitor), cc_monitor_destroy, "Allocating new ast_cc_monitor"))) {
  2019. cc_unref(cc_interface, "Failed to allocate monitor, unref interface");
  2020. return NULL;
  2021. }
  2022. if (!(monitor->dialstring = ast_strdup(dialstring))) {
  2023. cc_unref(monitor, "Failed to copy dialable name. Unref monitor");
  2024. cc_unref(cc_interface, "Failed to copy dialable name");
  2025. return NULL;
  2026. }
  2027. if (!(monitor->callbacks = find_monitor_callbacks(cc_data->monitor_type))) {
  2028. cc_unref(monitor, "Failed to find monitor callbacks. Unref monitor");
  2029. cc_unref(cc_interface, "Failed to find monitor callbacks");
  2030. return NULL;
  2031. }
  2032. strcpy(cc_interface->device_name, device_name);
  2033. monitor->id = ast_atomic_fetchadd_int(&dialed_cc_interface_counter, +1);
  2034. monitor->parent_id = parent_id;
  2035. monitor->core_id = core_id;
  2036. monitor->service_offered = cc_data->service;
  2037. monitor->private_data = cc_data->private_data;
  2038. cc_interface->monitor_type = cc_data->monitor_type;
  2039. cc_interface->monitor_class = AST_CC_DEVICE_MONITOR;
  2040. monitor->interface = cc_interface;
  2041. monitor->available_timer_id = -1;
  2042. ast_cc_copy_config_params(cc_interface->config_params, &cc_data->config_params);
  2043. ast_log_dynamic_level(cc_logger_level, "Core %d: Created a device cc interface for '%s' with id %u and parent %u\n",
  2044. monitor->core_id, cc_interface->device_name, monitor->id, monitor->parent_id);
  2045. return monitor;
  2046. }
  2047. /*!
  2048. * \details
  2049. * Unless we are ignoring CC for some reason, we will always
  2050. * call this function when we read an AST_CONTROL_CC frame
  2051. * from an outbound channel.
  2052. *
  2053. * This function will call cc_device_monitor_init to
  2054. * create the new cc_monitor for the device from which
  2055. * we read the frame. In addition, the new device will be added
  2056. * to the monitor tree on the dialed_cc_interfaces datastore
  2057. * on the inbound channel.
  2058. *
  2059. * If this is the first AST_CONTROL_CC frame that we have handled
  2060. * for this call, then we will also initialize the CC core for
  2061. * this call.
  2062. */
  2063. void ast_handle_cc_control_frame(struct ast_channel *inbound, struct ast_channel *outbound, void *frame_data)
  2064. {
  2065. char *device_name;
  2066. char *dialstring;
  2067. struct ast_cc_monitor *monitor;
  2068. struct ast_datastore *cc_datastore;
  2069. struct dialed_cc_interfaces *cc_interfaces;
  2070. struct cc_control_payload *cc_data = frame_data;
  2071. struct cc_core_instance *core_instance;
  2072. device_name = cc_data->device_name;
  2073. dialstring = cc_data->dialstring;
  2074. ast_channel_lock(inbound);
  2075. if (!(cc_datastore = ast_channel_datastore_find(inbound, &dialed_cc_interfaces_info, NULL))) {
  2076. ast_log(LOG_WARNING, "Unable to retrieve CC datastore while processing CC frame from '%s'. CC services will be unavailable.\n", device_name);
  2077. ast_channel_unlock(inbound);
  2078. call_destructor_with_no_monitor(cc_data->monitor_type, cc_data->private_data);
  2079. return;
  2080. }
  2081. cc_interfaces = cc_datastore->data;
  2082. if (cc_interfaces->ignore) {
  2083. ast_channel_unlock(inbound);
  2084. call_destructor_with_no_monitor(cc_data->monitor_type, cc_data->private_data);
  2085. return;
  2086. }
  2087. if (!cc_interfaces->is_original_caller) {
  2088. /* If the is_original_caller is not set on the *inbound* channel, then
  2089. * it must be a local channel. As such, we do not want to create a core instance
  2090. * or an agent for the local channel. Instead, we want to pass this along to the
  2091. * other side of the local channel so that the original caller can benefit.
  2092. */
  2093. ast_channel_unlock(inbound);
  2094. ast_indicate_data(inbound, AST_CONTROL_CC, cc_data, sizeof(*cc_data));
  2095. return;
  2096. }
  2097. core_instance = find_cc_core_instance(cc_interfaces->core_id);
  2098. if (!core_instance) {
  2099. core_instance = cc_core_init_instance(inbound, cc_interfaces->interface_tree,
  2100. cc_interfaces->core_id, cc_data);
  2101. if (!core_instance) {
  2102. cc_interfaces->ignore = 1;
  2103. ast_channel_unlock(inbound);
  2104. call_destructor_with_no_monitor(cc_data->monitor_type, cc_data->private_data);
  2105. return;
  2106. }
  2107. }
  2108. ast_channel_unlock(inbound);
  2109. /* Yeah this kind of sucks, but luckily most people
  2110. * aren't dialing thousands of interfaces on every call
  2111. *
  2112. * This traversal helps us to not create duplicate monitors in
  2113. * case a device queues multiple CC control frames.
  2114. */
  2115. AST_LIST_LOCK(cc_interfaces->interface_tree);
  2116. AST_LIST_TRAVERSE(cc_interfaces->interface_tree, monitor, next) {
  2117. if (!strcmp(monitor->interface->device_name, device_name)) {
  2118. ast_log_dynamic_level(cc_logger_level, "Core %d: Device %s sent us multiple CC control frames. Ignoring those beyond the first.\n",
  2119. core_instance->core_id, device_name);
  2120. AST_LIST_UNLOCK(cc_interfaces->interface_tree);
  2121. cc_unref(core_instance, "Returning early from ast_handle_cc_control_frame. Unref core_instance");
  2122. call_destructor_with_no_monitor(cc_data->monitor_type, cc_data->private_data);
  2123. return;
  2124. }
  2125. }
  2126. AST_LIST_UNLOCK(cc_interfaces->interface_tree);
  2127. if (!(monitor = cc_device_monitor_init(device_name, dialstring, cc_data, core_instance->core_id))) {
  2128. ast_log(LOG_WARNING, "Unable to create CC device interface for '%s'. CC services will be unavailable on this interface.\n", device_name);
  2129. cc_unref(core_instance, "Returning early from ast_handle_cc_control_frame. Unref core_instance");
  2130. call_destructor_with_no_monitor(cc_data->monitor_type, cc_data->private_data);
  2131. return;
  2132. }
  2133. AST_LIST_LOCK(cc_interfaces->interface_tree);
  2134. cc_ref(monitor, "monitor tree's reference to the monitor");
  2135. AST_LIST_INSERT_TAIL(cc_interfaces->interface_tree, monitor, next);
  2136. AST_LIST_UNLOCK(cc_interfaces->interface_tree);
  2137. cc_extension_monitor_change_is_valid(core_instance, monitor->parent_id, monitor->interface->device_name, 0);
  2138. cc_publish_available(cc_interfaces->core_id, device_name, cc_service_to_string(cc_data->service));
  2139. cc_unref(core_instance, "Done with core_instance after handling CC control frame");
  2140. cc_unref(monitor, "Unref reference from allocating monitor");
  2141. }
  2142. int ast_cc_call_init(struct ast_channel *chan, int *ignore_cc)
  2143. {
  2144. /* There are three situations to deal with here:
  2145. *
  2146. * 1. The channel does not have a dialed_cc_interfaces datastore on
  2147. * it. This means that this is the first time that Dial has
  2148. * been called. We need to create/initialize the datastore.
  2149. *
  2150. * 2. The channel does have a cc_interface datastore on it and
  2151. * the "ignore" indicator is 0. This means that a Local channel
  2152. * was called by a "parent" dial. We can check the datastore's
  2153. * parent field to see who the root of this particular dial tree
  2154. * is.
  2155. *
  2156. * 3. The channel does have a cc_interface datastore on it and
  2157. * the "ignore" indicator is 1. This means that a second Dial call
  2158. * is being made from an extension. In this case, we do not
  2159. * want to make any additions/modifications to the datastore. We
  2160. * will instead set a flag to indicate that CCSS is completely
  2161. * disabled for this Dial attempt.
  2162. */
  2163. struct ast_datastore *cc_interfaces_datastore;
  2164. struct dialed_cc_interfaces *interfaces;
  2165. struct ast_cc_monitor *monitor;
  2166. struct ast_cc_config_params *cc_params;
  2167. ast_channel_lock(chan);
  2168. cc_params = ast_channel_get_cc_config_params(chan);
  2169. if (!cc_params) {
  2170. ast_channel_unlock(chan);
  2171. return -1;
  2172. }
  2173. if (ast_get_cc_agent_policy(cc_params) == AST_CC_AGENT_NEVER) {
  2174. /* We can't offer CC to this caller anyway, so don't bother with CC on this call
  2175. */
  2176. *ignore_cc = 1;
  2177. ast_channel_unlock(chan);
  2178. ast_log_dynamic_level(cc_logger_level, "Agent policy for %s is 'never'. CC not possible\n", ast_channel_name(chan));
  2179. return 0;
  2180. }
  2181. if (!(cc_interfaces_datastore = ast_channel_datastore_find(chan, &dialed_cc_interfaces_info, NULL))) {
  2182. /* Situation 1 has occurred */
  2183. ast_channel_unlock(chan);
  2184. return cc_interfaces_datastore_init(chan);
  2185. }
  2186. interfaces = cc_interfaces_datastore->data;
  2187. ast_channel_unlock(chan);
  2188. if (interfaces->ignore) {
  2189. /* Situation 3 has occurred */
  2190. *ignore_cc = 1;
  2191. ast_log_dynamic_level(cc_logger_level, "Datastore is present with ignore flag set. Ignoring CC offers on this call\n");
  2192. return 0;
  2193. }
  2194. /* Situation 2 has occurred */
  2195. if (!(monitor = cc_extension_monitor_init(S_OR(ast_channel_macroexten(chan), ast_channel_exten(chan)),
  2196. S_OR(ast_channel_macrocontext(chan), ast_channel_context(chan)), interfaces->dial_parent_id))) {
  2197. return -1;
  2198. }
  2199. monitor->core_id = interfaces->core_id;
  2200. AST_LIST_LOCK(interfaces->interface_tree);
  2201. cc_ref(monitor, "monitor tree's reference to the monitor");
  2202. AST_LIST_INSERT_TAIL(interfaces->interface_tree, monitor, next);
  2203. AST_LIST_UNLOCK(interfaces->interface_tree);
  2204. interfaces->dial_parent_id = monitor->id;
  2205. cc_unref(monitor, "Unref monitor's allocation reference");
  2206. return 0;
  2207. }
  2208. int ast_cc_request_is_within_limits(void)
  2209. {
  2210. return cc_request_count < global_cc_max_requests;
  2211. }
  2212. int ast_cc_get_current_core_id(struct ast_channel *chan)
  2213. {
  2214. struct ast_datastore *datastore;
  2215. struct dialed_cc_interfaces *cc_interfaces;
  2216. int core_id_return;
  2217. ast_channel_lock(chan);
  2218. if (!(datastore = ast_channel_datastore_find(chan, &dialed_cc_interfaces_info, NULL))) {
  2219. ast_channel_unlock(chan);
  2220. return -1;
  2221. }
  2222. cc_interfaces = datastore->data;
  2223. core_id_return = cc_interfaces->ignore ? -1 : cc_interfaces->core_id;
  2224. ast_channel_unlock(chan);
  2225. return core_id_return;
  2226. }
  2227. static long count_agents(const char * const caller, const int core_id_exception)
  2228. {
  2229. struct count_agents_cb_data data = {.core_id_exception = core_id_exception,};
  2230. ao2_t_callback_data(cc_core_instances, OBJ_NODATA, count_agents_cb, (char *)caller, &data, "Counting agents");
  2231. ast_log_dynamic_level(cc_logger_level, "Counted %d agents\n", data.count);
  2232. return data.count;
  2233. }
  2234. static void kill_duplicate_offers(char *caller)
  2235. {
  2236. unsigned long match_flags = MATCH_NO_REQUEST;
  2237. struct ao2_iterator *dups_iter;
  2238. /*
  2239. * Must remove the ref that was in cc_core_instances outside of
  2240. * the container lock to prevent deadlock.
  2241. */
  2242. dups_iter = ao2_t_callback_data(cc_core_instances, OBJ_MULTIPLE | OBJ_UNLINK,
  2243. match_agent, caller, &match_flags, "Killing duplicate offers");
  2244. if (dups_iter) {
  2245. /* Now actually unref any duplicate offers by simply destroying the iterator. */
  2246. ao2_iterator_destroy(dups_iter);
  2247. }
  2248. }
  2249. static void check_callback_sanity(const struct ast_cc_agent_callbacks *callbacks)
  2250. {
  2251. ast_assert(callbacks->init != NULL);
  2252. ast_assert(callbacks->start_offer_timer != NULL);
  2253. ast_assert(callbacks->stop_offer_timer != NULL);
  2254. ast_assert(callbacks->respond != NULL);
  2255. ast_assert(callbacks->status_request != NULL);
  2256. ast_assert(callbacks->start_monitoring != NULL);
  2257. ast_assert(callbacks->callee_available != NULL);
  2258. ast_assert(callbacks->destructor != NULL);
  2259. }
  2260. static void agent_destroy(void *data)
  2261. {
  2262. struct ast_cc_agent *agent = data;
  2263. if (agent->callbacks) {
  2264. agent->callbacks->destructor(agent);
  2265. }
  2266. ast_cc_config_params_destroy(agent->cc_params);
  2267. }
  2268. static struct ast_cc_agent *cc_agent_init(struct ast_channel *caller_chan,
  2269. const char * const caller_name, const int core_id,
  2270. struct cc_monitor_tree *interface_tree)
  2271. {
  2272. struct ast_cc_agent *agent;
  2273. struct ast_cc_config_params *cc_params;
  2274. if (!(agent = ao2_t_alloc(sizeof(*agent) + strlen(caller_name), agent_destroy,
  2275. "Allocating new ast_cc_agent"))) {
  2276. return NULL;
  2277. }
  2278. agent->core_id = core_id;
  2279. strcpy(agent->device_name, caller_name);
  2280. cc_params = ast_channel_get_cc_config_params(caller_chan);
  2281. if (!cc_params) {
  2282. cc_unref(agent, "Could not get channel config params.");
  2283. return NULL;
  2284. }
  2285. if (!(agent->cc_params = ast_cc_config_params_init())) {
  2286. cc_unref(agent, "Could not init agent config params.");
  2287. return NULL;
  2288. }
  2289. ast_cc_copy_config_params(agent->cc_params, cc_params);
  2290. if (!(agent->callbacks = find_agent_callbacks(caller_chan))) {
  2291. cc_unref(agent, "Could not find agent callbacks.");
  2292. return NULL;
  2293. }
  2294. check_callback_sanity(agent->callbacks);
  2295. if (agent->callbacks->init(agent, caller_chan)) {
  2296. cc_unref(agent, "Agent init callback failed.");
  2297. return NULL;
  2298. }
  2299. ast_log_dynamic_level(cc_logger_level, "Core %u: Created an agent for caller %s\n",
  2300. agent->core_id, agent->device_name);
  2301. return agent;
  2302. }
  2303. /* Generic agent callbacks */
  2304. static int cc_generic_agent_init(struct ast_cc_agent *agent, struct ast_channel *chan);
  2305. static int cc_generic_agent_start_offer_timer(struct ast_cc_agent *agent);
  2306. static int cc_generic_agent_stop_offer_timer(struct ast_cc_agent *agent);
  2307. static void cc_generic_agent_respond(struct ast_cc_agent *agent, enum ast_cc_agent_response_reason reason);
  2308. static int cc_generic_agent_status_request(struct ast_cc_agent *agent);
  2309. static int cc_generic_agent_stop_ringing(struct ast_cc_agent *agent);
  2310. static int cc_generic_agent_start_monitoring(struct ast_cc_agent *agent);
  2311. static int cc_generic_agent_recall(struct ast_cc_agent *agent);
  2312. static void cc_generic_agent_destructor(struct ast_cc_agent *agent);
  2313. static struct ast_cc_agent_callbacks generic_agent_callbacks = {
  2314. .type = "generic",
  2315. .init = cc_generic_agent_init,
  2316. .start_offer_timer = cc_generic_agent_start_offer_timer,
  2317. .stop_offer_timer = cc_generic_agent_stop_offer_timer,
  2318. .respond = cc_generic_agent_respond,
  2319. .status_request = cc_generic_agent_status_request,
  2320. .stop_ringing = cc_generic_agent_stop_ringing,
  2321. .start_monitoring = cc_generic_agent_start_monitoring,
  2322. .callee_available = cc_generic_agent_recall,
  2323. .destructor = cc_generic_agent_destructor,
  2324. };
  2325. struct cc_generic_agent_pvt {
  2326. /*!
  2327. * Subscription to device state
  2328. *
  2329. * Used in the CC_CALLER_BUSY state. The
  2330. * generic agent will subscribe to the
  2331. * device state of the caller in order to
  2332. * determine when we may move on
  2333. */
  2334. struct stasis_subscription *sub;
  2335. /*!
  2336. * Scheduler id of offer timer.
  2337. */
  2338. int offer_timer_id;
  2339. /*!
  2340. * Caller ID number
  2341. *
  2342. * When we re-call the caller, we need
  2343. * to provide this information to
  2344. * ast_request_and_dial so that the
  2345. * information will be present in the
  2346. * call to the callee
  2347. */
  2348. char cid_num[AST_CHANNEL_NAME];
  2349. /*!
  2350. * Caller ID name
  2351. *
  2352. * See the description of cid_num.
  2353. * The same applies here, except this
  2354. * is the caller's name.
  2355. */
  2356. char cid_name[AST_CHANNEL_NAME];
  2357. /*!
  2358. * Extension dialed
  2359. *
  2360. * The original extension dialed. This is used
  2361. * so that when performing a recall, we can
  2362. * call the proper extension.
  2363. */
  2364. char exten[AST_CHANNEL_NAME];
  2365. /*!
  2366. * Context dialed
  2367. *
  2368. * The original context dialed. This is used
  2369. * so that when performaing a recall, we can
  2370. * call into the proper context
  2371. */
  2372. char context[AST_CHANNEL_NAME];
  2373. };
  2374. static int cc_generic_agent_init(struct ast_cc_agent *agent, struct ast_channel *chan)
  2375. {
  2376. struct cc_generic_agent_pvt *generic_pvt = ast_calloc(1, sizeof(*generic_pvt));
  2377. if (!generic_pvt) {
  2378. return -1;
  2379. }
  2380. generic_pvt->offer_timer_id = -1;
  2381. if (ast_channel_caller(chan)->id.number.valid && ast_channel_caller(chan)->id.number.str) {
  2382. ast_copy_string(generic_pvt->cid_num, ast_channel_caller(chan)->id.number.str, sizeof(generic_pvt->cid_num));
  2383. }
  2384. if (ast_channel_caller(chan)->id.name.valid && ast_channel_caller(chan)->id.name.str) {
  2385. ast_copy_string(generic_pvt->cid_name, ast_channel_caller(chan)->id.name.str, sizeof(generic_pvt->cid_name));
  2386. }
  2387. ast_copy_string(generic_pvt->exten, S_OR(ast_channel_macroexten(chan), ast_channel_exten(chan)), sizeof(generic_pvt->exten));
  2388. ast_copy_string(generic_pvt->context, S_OR(ast_channel_macrocontext(chan), ast_channel_context(chan)), sizeof(generic_pvt->context));
  2389. agent->private_data = generic_pvt;
  2390. ast_set_flag(agent, AST_CC_AGENT_SKIP_OFFER);
  2391. return 0;
  2392. }
  2393. static int offer_timer_expire(const void *data)
  2394. {
  2395. struct ast_cc_agent *agent = (struct ast_cc_agent *) data;
  2396. struct cc_generic_agent_pvt *agent_pvt = agent->private_data;
  2397. ast_log_dynamic_level(cc_logger_level, "Core %u: Queuing change request because offer timer has expired.\n",
  2398. agent->core_id);
  2399. agent_pvt->offer_timer_id = -1;
  2400. ast_cc_failed(agent->core_id, "Generic agent %s offer timer expired", agent->device_name);
  2401. cc_unref(agent, "Remove scheduler's reference to the agent");
  2402. return 0;
  2403. }
  2404. static int cc_generic_agent_start_offer_timer(struct ast_cc_agent *agent)
  2405. {
  2406. int when;
  2407. int sched_id;
  2408. struct cc_generic_agent_pvt *generic_pvt = agent->private_data;
  2409. ast_assert(cc_sched_context != NULL);
  2410. ast_assert(agent->cc_params != NULL);
  2411. when = ast_get_cc_offer_timer(agent->cc_params) * 1000;
  2412. ast_log_dynamic_level(cc_logger_level, "Core %u: About to schedule offer timer expiration for %d ms\n",
  2413. agent->core_id, when);
  2414. if ((sched_id = ast_sched_add(cc_sched_context, when, offer_timer_expire, cc_ref(agent, "Give scheduler an agent ref"))) == -1) {
  2415. return -1;
  2416. }
  2417. generic_pvt->offer_timer_id = sched_id;
  2418. return 0;
  2419. }
  2420. static int cc_generic_agent_stop_offer_timer(struct ast_cc_agent *agent)
  2421. {
  2422. struct cc_generic_agent_pvt *generic_pvt = agent->private_data;
  2423. if (generic_pvt->offer_timer_id != -1) {
  2424. if (!ast_sched_del(cc_sched_context, generic_pvt->offer_timer_id)) {
  2425. cc_unref(agent, "Remove scheduler's reference to the agent");
  2426. }
  2427. generic_pvt->offer_timer_id = -1;
  2428. }
  2429. return 0;
  2430. }
  2431. static void cc_generic_agent_respond(struct ast_cc_agent *agent, enum ast_cc_agent_response_reason reason)
  2432. {
  2433. /* The generic agent doesn't have to do anything special to
  2434. * acknowledge a CC request. Just return.
  2435. */
  2436. return;
  2437. }
  2438. static int cc_generic_agent_status_request(struct ast_cc_agent *agent)
  2439. {
  2440. ast_cc_agent_status_response(agent->core_id, ast_device_state(agent->device_name));
  2441. return 0;
  2442. }
  2443. static int cc_generic_agent_stop_ringing(struct ast_cc_agent *agent)
  2444. {
  2445. struct ast_channel *recall_chan = ast_channel_get_by_name_prefix(agent->device_name, strlen(agent->device_name));
  2446. if (!recall_chan) {
  2447. return 0;
  2448. }
  2449. ast_softhangup(recall_chan, AST_SOFTHANGUP_EXPLICIT);
  2450. return 0;
  2451. }
  2452. static void generic_agent_devstate_cb(void *userdata, struct stasis_subscription *sub, struct stasis_message *msg)
  2453. {
  2454. struct ast_cc_agent *agent = userdata;
  2455. enum ast_device_state new_state;
  2456. struct ast_device_state_message *dev_state;
  2457. struct cc_generic_agent_pvt *generic_pvt = agent->private_data;
  2458. if (stasis_subscription_final_message(sub, msg)) {
  2459. cc_unref(agent, "Done holding ref for subscription");
  2460. return;
  2461. } else if (ast_device_state_message_type() != stasis_message_type(msg)) {
  2462. return;
  2463. }
  2464. dev_state = stasis_message_data(msg);
  2465. if (dev_state->eid) {
  2466. /* ignore non-aggregate states */
  2467. return;
  2468. }
  2469. new_state = dev_state->state;
  2470. if (!cc_generic_is_device_available(new_state)) {
  2471. /* Not interested in this new state of the device. It is still busy. */
  2472. return;
  2473. }
  2474. generic_pvt->sub = stasis_unsubscribe(sub);
  2475. ast_cc_agent_caller_available(agent->core_id, "%s is no longer busy", agent->device_name);
  2476. }
  2477. static int cc_generic_agent_start_monitoring(struct ast_cc_agent *agent)
  2478. {
  2479. struct cc_generic_agent_pvt *generic_pvt = agent->private_data;
  2480. struct ast_str *str = ast_str_alloca(128);
  2481. struct stasis_topic *device_specific_topic;
  2482. ast_assert(generic_pvt->sub == NULL);
  2483. ast_str_set(&str, 0, "Agent monitoring %s device state since it is busy\n",
  2484. agent->device_name);
  2485. device_specific_topic = ast_device_state_topic(agent->device_name);
  2486. if (!device_specific_topic) {
  2487. return -1;
  2488. }
  2489. if (!(generic_pvt->sub = stasis_subscribe(device_specific_topic, generic_agent_devstate_cb, agent))) {
  2490. return -1;
  2491. }
  2492. stasis_subscription_accept_message_type(generic_pvt->sub, ast_device_state_message_type());
  2493. stasis_subscription_accept_message_type(generic_pvt->sub, stasis_subscription_change_type());
  2494. stasis_subscription_set_filter(generic_pvt->sub, STASIS_SUBSCRIPTION_FILTER_SELECTIVE);
  2495. cc_ref(agent, "Ref agent for subscription");
  2496. return 0;
  2497. }
  2498. static void *generic_recall(void *data)
  2499. {
  2500. struct ast_cc_agent *agent = data;
  2501. struct cc_generic_agent_pvt *generic_pvt = agent->private_data;
  2502. const char *interface = S_OR(ast_get_cc_agent_dialstring(agent->cc_params), ast_strdupa(agent->device_name));
  2503. const char *tech;
  2504. char *target;
  2505. int reason;
  2506. struct ast_channel *chan;
  2507. const char *callback_macro = ast_get_cc_callback_macro(agent->cc_params);
  2508. const char *callback_sub = ast_get_cc_callback_sub(agent->cc_params);
  2509. unsigned int recall_timer = ast_get_cc_recall_timer(agent->cc_params) * 1000;
  2510. struct ast_format_cap *tmp_cap = ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_DEFAULT);
  2511. if (!tmp_cap) {
  2512. return NULL;
  2513. }
  2514. tech = interface;
  2515. if ((target = strchr(interface, '/'))) {
  2516. *target++ = '\0';
  2517. }
  2518. ast_format_cap_append(tmp_cap, ast_format_slin, 0);
  2519. if (!(chan = ast_request_and_dial(tech, tmp_cap, NULL, NULL, target, recall_timer, &reason, generic_pvt->cid_num, generic_pvt->cid_name))) {
  2520. /* Hmm, no channel. Sucks for you, bud.
  2521. */
  2522. ast_log_dynamic_level(cc_logger_level, "Core %u: Failed to call back %s for reason %d\n",
  2523. agent->core_id, agent->device_name, reason);
  2524. ast_cc_failed(agent->core_id, "Failed to call back device %s/%s", tech, target);
  2525. ao2_ref(tmp_cap, -1);
  2526. return NULL;
  2527. }
  2528. ao2_ref(tmp_cap, -1);
  2529. /* We have a channel. It's time now to set up the datastore of recalled CC interfaces.
  2530. * This will be a common task for all recall functions. If it were possible, I'd have
  2531. * the core do it automatically, but alas I cannot. Instead, I will provide a public
  2532. * function to do so.
  2533. */
  2534. ast_setup_cc_recall_datastore(chan, agent->core_id);
  2535. ast_cc_agent_set_interfaces_chanvar(chan);
  2536. ast_channel_exten_set(chan, generic_pvt->exten);
  2537. ast_channel_context_set(chan, generic_pvt->context);
  2538. ast_channel_priority_set(chan, 1);
  2539. pbx_builtin_setvar_helper(chan, "CC_EXTEN", generic_pvt->exten);
  2540. pbx_builtin_setvar_helper(chan, "CC_CONTEXT", generic_pvt->context);
  2541. if (!ast_strlen_zero(callback_macro)) {
  2542. ast_log_dynamic_level(cc_logger_level, "Core %u: There's a callback macro configured for agent %s\n",
  2543. agent->core_id, agent->device_name);
  2544. if (ast_app_exec_macro(NULL, chan, callback_macro)) {
  2545. ast_cc_failed(agent->core_id, "Callback macro to %s failed. Maybe a hangup?", agent->device_name);
  2546. ast_hangup(chan);
  2547. return NULL;
  2548. }
  2549. }
  2550. if (!ast_strlen_zero(callback_sub)) {
  2551. ast_log_dynamic_level(cc_logger_level, "Core %u: There's a callback subroutine configured for agent %s\n",
  2552. agent->core_id, agent->device_name);
  2553. if (ast_app_exec_sub(NULL, chan, callback_sub, 0)) {
  2554. ast_cc_failed(agent->core_id, "Callback subroutine to %s failed. Maybe a hangup?", agent->device_name);
  2555. ast_hangup(chan);
  2556. return NULL;
  2557. }
  2558. }
  2559. if (ast_pbx_start(chan)) {
  2560. ast_cc_failed(agent->core_id, "PBX failed to start for %s.", agent->device_name);
  2561. ast_hangup(chan);
  2562. return NULL;
  2563. }
  2564. ast_cc_agent_recalling(agent->core_id, "Generic agent %s is recalling",
  2565. agent->device_name);
  2566. return NULL;
  2567. }
  2568. static int cc_generic_agent_recall(struct ast_cc_agent *agent)
  2569. {
  2570. pthread_t clotho;
  2571. enum ast_device_state current_state = ast_device_state(agent->device_name);
  2572. if (!cc_generic_is_device_available(current_state)) {
  2573. /* We can't try to contact the device right now because he's not available
  2574. * Let the core know he's busy.
  2575. */
  2576. ast_cc_agent_caller_busy(agent->core_id, "Generic agent caller %s is busy", agent->device_name);
  2577. return 0;
  2578. }
  2579. ast_pthread_create_detached_background(&clotho, NULL, generic_recall, agent);
  2580. return 0;
  2581. }
  2582. static void cc_generic_agent_destructor(struct ast_cc_agent *agent)
  2583. {
  2584. struct cc_generic_agent_pvt *agent_pvt = agent->private_data;
  2585. if (!agent_pvt) {
  2586. /* The agent constructor probably failed. */
  2587. return;
  2588. }
  2589. cc_generic_agent_stop_offer_timer(agent);
  2590. if (agent_pvt->sub) {
  2591. agent_pvt->sub = stasis_unsubscribe(agent_pvt->sub);
  2592. }
  2593. ast_free(agent_pvt);
  2594. }
  2595. static void cc_core_instance_destructor(void *data)
  2596. {
  2597. struct cc_core_instance *core_instance = data;
  2598. ast_log_dynamic_level(cc_logger_level, "Core %d: Destroying core instance\n", core_instance->core_id);
  2599. if (core_instance->agent) {
  2600. cc_unref(core_instance->agent, "Core instance is done with the agent now");
  2601. }
  2602. if (core_instance->monitors) {
  2603. core_instance->monitors = cc_unref(core_instance->monitors, "Core instance is done with interface list");
  2604. }
  2605. }
  2606. static struct cc_core_instance *cc_core_init_instance(struct ast_channel *caller_chan,
  2607. struct cc_monitor_tree *called_tree, const int core_id, struct cc_control_payload *cc_data)
  2608. {
  2609. char caller[AST_CHANNEL_NAME];
  2610. struct cc_core_instance *core_instance;
  2611. struct ast_cc_config_params *cc_params;
  2612. long agent_count;
  2613. int recall_core_id;
  2614. ast_channel_get_device_name(caller_chan, caller, sizeof(caller));
  2615. cc_params = ast_channel_get_cc_config_params(caller_chan);
  2616. if (!cc_params) {
  2617. ast_log_dynamic_level(cc_logger_level, "Could not get CC parameters for %s\n",
  2618. caller);
  2619. return NULL;
  2620. }
  2621. /* First, we need to kill off other pending CC offers from caller. If the caller is going
  2622. * to request a CC service, it may only be for the latest call he made.
  2623. */
  2624. if (ast_get_cc_agent_policy(cc_params) == AST_CC_AGENT_GENERIC) {
  2625. kill_duplicate_offers(caller);
  2626. }
  2627. ast_cc_is_recall(caller_chan, &recall_core_id, NULL);
  2628. agent_count = count_agents(caller, recall_core_id);
  2629. if (agent_count >= ast_get_cc_max_agents(cc_params)) {
  2630. ast_log_dynamic_level(cc_logger_level, "Caller %s already has the maximum number of agents configured\n", caller);
  2631. return NULL;
  2632. }
  2633. /* Generic agents can only have a single outstanding CC request per caller. */
  2634. if (agent_count > 0 && ast_get_cc_agent_policy(cc_params) == AST_CC_AGENT_GENERIC) {
  2635. ast_log_dynamic_level(cc_logger_level, "Generic agents can only have a single outstanding request\n");
  2636. return NULL;
  2637. }
  2638. /* Next, we need to create the core instance for this call */
  2639. if (!(core_instance = ao2_t_alloc(sizeof(*core_instance), cc_core_instance_destructor, "Creating core instance for CC"))) {
  2640. return NULL;
  2641. }
  2642. core_instance->core_id = core_id;
  2643. if (!(core_instance->agent = cc_agent_init(caller_chan, caller, core_instance->core_id, called_tree))) {
  2644. cc_unref(core_instance, "Couldn't allocate agent, unref core_instance");
  2645. return NULL;
  2646. }
  2647. core_instance->monitors = cc_ref(called_tree, "Core instance getting ref to monitor tree");
  2648. ao2_t_link(cc_core_instances, core_instance, "Link core instance into container");
  2649. return core_instance;
  2650. }
  2651. struct cc_state_change_args {
  2652. struct cc_core_instance *core_instance;/*!< Holds reference to core instance. */
  2653. enum cc_state state;
  2654. int core_id;
  2655. char debug[1];
  2656. };
  2657. static int is_state_change_valid(enum cc_state current_state, const enum cc_state new_state, struct ast_cc_agent *agent)
  2658. {
  2659. int is_valid = 0;
  2660. switch (new_state) {
  2661. case CC_AVAILABLE:
  2662. ast_log_dynamic_level(cc_logger_level, "Core %u: Asked to change to state %u? That should never happen.\n",
  2663. agent->core_id, new_state);
  2664. break;
  2665. case CC_CALLER_OFFERED:
  2666. if (current_state == CC_AVAILABLE) {
  2667. is_valid = 1;
  2668. }
  2669. break;
  2670. case CC_CALLER_REQUESTED:
  2671. if (current_state == CC_CALLER_OFFERED ||
  2672. (current_state == CC_AVAILABLE && ast_test_flag(agent, AST_CC_AGENT_SKIP_OFFER))) {
  2673. is_valid = 1;
  2674. }
  2675. break;
  2676. case CC_ACTIVE:
  2677. if (current_state == CC_CALLER_REQUESTED || current_state == CC_CALLER_BUSY) {
  2678. is_valid = 1;
  2679. }
  2680. break;
  2681. case CC_CALLEE_READY:
  2682. if (current_state == CC_ACTIVE) {
  2683. is_valid = 1;
  2684. }
  2685. break;
  2686. case CC_CALLER_BUSY:
  2687. if (current_state == CC_CALLEE_READY) {
  2688. is_valid = 1;
  2689. }
  2690. break;
  2691. case CC_RECALLING:
  2692. if (current_state == CC_CALLEE_READY) {
  2693. is_valid = 1;
  2694. }
  2695. break;
  2696. case CC_COMPLETE:
  2697. if (current_state == CC_RECALLING) {
  2698. is_valid = 1;
  2699. }
  2700. break;
  2701. case CC_FAILED:
  2702. is_valid = 1;
  2703. break;
  2704. default:
  2705. ast_log_dynamic_level(cc_logger_level, "Core %u: Asked to change to unknown state %u\n",
  2706. agent->core_id, new_state);
  2707. break;
  2708. }
  2709. return is_valid;
  2710. }
  2711. static int cc_available(struct cc_core_instance *core_instance, struct cc_state_change_args *args, enum cc_state previous_state)
  2712. {
  2713. /* This should never happen... */
  2714. ast_log(LOG_WARNING, "Someone requested to change to CC_AVAILABLE? Ignoring.\n");
  2715. return -1;
  2716. }
  2717. static int cc_caller_offered(struct cc_core_instance *core_instance, struct cc_state_change_args *args, enum cc_state previous_state)
  2718. {
  2719. if (core_instance->agent->callbacks->start_offer_timer(core_instance->agent)) {
  2720. ast_cc_failed(core_instance->core_id, "Failed to start the offer timer for %s\n",
  2721. core_instance->agent->device_name);
  2722. return -1;
  2723. }
  2724. cc_publish_offertimerstart(core_instance->core_id, core_instance->agent->device_name, core_instance->agent->cc_params->cc_offer_timer);
  2725. ast_log_dynamic_level(cc_logger_level, "Core %d: Started the offer timer for the agent %s!\n",
  2726. core_instance->core_id, core_instance->agent->device_name);
  2727. return 0;
  2728. }
  2729. /*!
  2730. * \brief check if the core instance has any device monitors
  2731. *
  2732. * In any case where we end up removing a device monitor from the
  2733. * list of device monitors, it is important to see what the state
  2734. * of the list is afterwards. If we find that we only have extension
  2735. * monitors left, then no devices are actually being monitored.
  2736. * In such a case, we need to declare that CC has failed for this
  2737. * call. This function helps those cases to determine if they should
  2738. * declare failure.
  2739. *
  2740. * \param core_instance The core instance we are checking for the existence
  2741. * of device monitors
  2742. * \retval 0 No device monitors exist on this core_instance
  2743. * \retval 1 There is still at least 1 device monitor remaining
  2744. */
  2745. static int has_device_monitors(struct cc_core_instance *core_instance)
  2746. {
  2747. struct ast_cc_monitor *iter;
  2748. int res = 0;
  2749. AST_LIST_TRAVERSE(core_instance->monitors, iter, next) {
  2750. if (iter->interface->monitor_class == AST_CC_DEVICE_MONITOR) {
  2751. res = 1;
  2752. break;
  2753. }
  2754. }
  2755. return res;
  2756. }
  2757. static void request_cc(struct cc_core_instance *core_instance)
  2758. {
  2759. struct ast_cc_monitor *monitor_iter;
  2760. AST_LIST_LOCK(core_instance->monitors);
  2761. AST_LIST_TRAVERSE_SAFE_BEGIN(core_instance->monitors, monitor_iter, next) {
  2762. if (monitor_iter->interface->monitor_class == AST_CC_DEVICE_MONITOR) {
  2763. if (monitor_iter->callbacks->request_cc(monitor_iter, &monitor_iter->available_timer_id)) {
  2764. AST_LIST_REMOVE_CURRENT(next);
  2765. cc_extension_monitor_change_is_valid(core_instance, monitor_iter->parent_id,
  2766. monitor_iter->interface->device_name, 1);
  2767. cc_unref(monitor_iter, "request_cc failed. Unref list's reference to monitor");
  2768. } else {
  2769. cc_publish_requested(core_instance->core_id, core_instance->agent->device_name, monitor_iter->interface->device_name);
  2770. }
  2771. }
  2772. }
  2773. AST_LIST_TRAVERSE_SAFE_END;
  2774. if (!has_device_monitors(core_instance)) {
  2775. ast_cc_failed(core_instance->core_id, "All device monitors failed to request CC");
  2776. }
  2777. AST_LIST_UNLOCK(core_instance->monitors);
  2778. }
  2779. static int cc_caller_requested(struct cc_core_instance *core_instance, struct cc_state_change_args *args, enum cc_state previous_state)
  2780. {
  2781. if (!ast_cc_request_is_within_limits()) {
  2782. ast_log(LOG_WARNING, "Cannot request CC since there is no more room for requests\n");
  2783. core_instance->agent->callbacks->respond(core_instance->agent,
  2784. AST_CC_AGENT_RESPONSE_FAILURE_TOO_MANY);
  2785. ast_cc_failed(core_instance->core_id, "Too many requests in the system");
  2786. return -1;
  2787. }
  2788. core_instance->agent->callbacks->stop_offer_timer(core_instance->agent);
  2789. request_cc(core_instance);
  2790. return 0;
  2791. }
  2792. static void unsuspend(struct cc_core_instance *core_instance)
  2793. {
  2794. struct ast_cc_monitor *monitor_iter;
  2795. AST_LIST_LOCK(core_instance->monitors);
  2796. AST_LIST_TRAVERSE_SAFE_BEGIN(core_instance->monitors, monitor_iter, next) {
  2797. if (monitor_iter->interface->monitor_class == AST_CC_DEVICE_MONITOR) {
  2798. if (monitor_iter->callbacks->unsuspend(monitor_iter)) {
  2799. AST_LIST_REMOVE_CURRENT(next);
  2800. cc_extension_monitor_change_is_valid(core_instance, monitor_iter->parent_id,
  2801. monitor_iter->interface->device_name, 1);
  2802. cc_unref(monitor_iter, "unsuspend failed. Unref list's reference to monitor");
  2803. }
  2804. }
  2805. }
  2806. AST_LIST_TRAVERSE_SAFE_END;
  2807. if (!has_device_monitors(core_instance)) {
  2808. ast_cc_failed(core_instance->core_id, "All device monitors failed to unsuspend CC");
  2809. }
  2810. AST_LIST_UNLOCK(core_instance->monitors);
  2811. }
  2812. static int cc_active(struct cc_core_instance *core_instance, struct cc_state_change_args *args, enum cc_state previous_state)
  2813. {
  2814. /* Either
  2815. * 1. Callee accepted CC request, call agent's ack callback.
  2816. * 2. Caller became available, call agent's stop_monitoring callback and
  2817. * call monitor's unsuspend callback.
  2818. */
  2819. if (previous_state == CC_CALLER_REQUESTED) {
  2820. core_instance->agent->callbacks->respond(core_instance->agent,
  2821. AST_CC_AGENT_RESPONSE_SUCCESS);
  2822. cc_publish_requestacknowledged(core_instance->core_id, core_instance->agent->device_name);
  2823. } else if (previous_state == CC_CALLER_BUSY) {
  2824. cc_publish_callerstopmonitoring(core_instance->core_id, core_instance->agent->device_name);
  2825. unsuspend(core_instance);
  2826. }
  2827. /* Not possible for previous_state to be anything else due to the is_state_change_valid check at the beginning */
  2828. return 0;
  2829. }
  2830. static int cc_callee_ready(struct cc_core_instance *core_instance, struct cc_state_change_args *args, enum cc_state previous_state)
  2831. {
  2832. core_instance->agent->callbacks->callee_available(core_instance->agent);
  2833. return 0;
  2834. }
  2835. static void suspend(struct cc_core_instance *core_instance)
  2836. {
  2837. struct ast_cc_monitor *monitor_iter;
  2838. AST_LIST_LOCK(core_instance->monitors);
  2839. AST_LIST_TRAVERSE_SAFE_BEGIN(core_instance->monitors, monitor_iter, next) {
  2840. if (monitor_iter->interface->monitor_class == AST_CC_DEVICE_MONITOR) {
  2841. if (monitor_iter->callbacks->suspend(monitor_iter)) {
  2842. AST_LIST_REMOVE_CURRENT(next);
  2843. cc_extension_monitor_change_is_valid(core_instance, monitor_iter->parent_id,
  2844. monitor_iter->interface->device_name, 1);
  2845. cc_unref(monitor_iter, "suspend failed. Unref list's reference to monitor");
  2846. }
  2847. }
  2848. }
  2849. AST_LIST_TRAVERSE_SAFE_END;
  2850. if (!has_device_monitors(core_instance)) {
  2851. ast_cc_failed(core_instance->core_id, "All device monitors failed to suspend CC");
  2852. }
  2853. AST_LIST_UNLOCK(core_instance->monitors);
  2854. }
  2855. static int cc_caller_busy(struct cc_core_instance *core_instance, struct cc_state_change_args *args, enum cc_state previous_state)
  2856. {
  2857. /* Callee was available, but caller was busy, call agent's begin_monitoring callback
  2858. * and call monitor's suspend callback.
  2859. */
  2860. suspend(core_instance);
  2861. core_instance->agent->callbacks->start_monitoring(core_instance->agent);
  2862. cc_publish_callerstartmonitoring(core_instance->core_id, core_instance->agent->device_name);
  2863. return 0;
  2864. }
  2865. static void cancel_available_timer(struct cc_core_instance *core_instance)
  2866. {
  2867. struct ast_cc_monitor *monitor_iter;
  2868. AST_LIST_LOCK(core_instance->monitors);
  2869. AST_LIST_TRAVERSE_SAFE_BEGIN(core_instance->monitors, monitor_iter, next) {
  2870. if (monitor_iter->interface->monitor_class == AST_CC_DEVICE_MONITOR) {
  2871. if (monitor_iter->callbacks->cancel_available_timer(monitor_iter, &monitor_iter->available_timer_id)) {
  2872. AST_LIST_REMOVE_CURRENT(next);
  2873. cc_extension_monitor_change_is_valid(core_instance, monitor_iter->parent_id,
  2874. monitor_iter->interface->device_name, 1);
  2875. cc_unref(monitor_iter, "cancel_available_timer failed. Unref list's reference to monitor");
  2876. }
  2877. }
  2878. }
  2879. AST_LIST_TRAVERSE_SAFE_END;
  2880. if (!has_device_monitors(core_instance)) {
  2881. ast_cc_failed(core_instance->core_id, "All device monitors failed to cancel their available timers");
  2882. }
  2883. AST_LIST_UNLOCK(core_instance->monitors);
  2884. }
  2885. static int cc_recalling(struct cc_core_instance *core_instance, struct cc_state_change_args *args, enum cc_state previous_state)
  2886. {
  2887. /* Both caller and callee are available, call agent's recall callback
  2888. */
  2889. cancel_available_timer(core_instance);
  2890. cc_publish_callerrecalling(core_instance->core_id, core_instance->agent->device_name);
  2891. return 0;
  2892. }
  2893. static int cc_complete(struct cc_core_instance *core_instance, struct cc_state_change_args *args, enum cc_state previous_state)
  2894. {
  2895. /* Recall has made progress, call agent and monitor destructor functions
  2896. */
  2897. cc_publish_recallcomplete(core_instance->core_id, core_instance->agent->device_name);
  2898. ao2_t_unlink(cc_core_instances, core_instance, "Unlink core instance since CC recall has completed");
  2899. return 0;
  2900. }
  2901. static int cc_failed(struct cc_core_instance *core_instance, struct cc_state_change_args *args, enum cc_state previous_state)
  2902. {
  2903. cc_publish_failure(core_instance->core_id, core_instance->agent->device_name, args->debug);
  2904. ao2_t_unlink(cc_core_instances, core_instance, "Unlink core instance since CC failed");
  2905. return 0;
  2906. }
  2907. static int (* const state_change_funcs [])(struct cc_core_instance *, struct cc_state_change_args *, enum cc_state previous_state) = {
  2908. [CC_AVAILABLE] = cc_available,
  2909. [CC_CALLER_OFFERED] = cc_caller_offered,
  2910. [CC_CALLER_REQUESTED] = cc_caller_requested,
  2911. [CC_ACTIVE] = cc_active,
  2912. [CC_CALLEE_READY] = cc_callee_ready,
  2913. [CC_CALLER_BUSY] = cc_caller_busy,
  2914. [CC_RECALLING] = cc_recalling,
  2915. [CC_COMPLETE] = cc_complete,
  2916. [CC_FAILED] = cc_failed,
  2917. };
  2918. static int cc_do_state_change(void *datap)
  2919. {
  2920. struct cc_state_change_args *args = datap;
  2921. struct cc_core_instance *core_instance;
  2922. enum cc_state previous_state;
  2923. int res;
  2924. ast_log_dynamic_level(cc_logger_level, "Core %d: State change to %u requested. Reason: %s\n",
  2925. args->core_id, args->state, args->debug);
  2926. core_instance = args->core_instance;
  2927. if (!is_state_change_valid(core_instance->current_state, args->state, core_instance->agent)) {
  2928. ast_log_dynamic_level(cc_logger_level, "Core %d: Invalid state change requested. Cannot go from %s to %s\n",
  2929. args->core_id, cc_state_to_string(core_instance->current_state), cc_state_to_string(args->state));
  2930. if (args->state == CC_CALLER_REQUESTED) {
  2931. /*
  2932. * For out-of-order requests, we need to let the requester know that
  2933. * we can't handle the request now.
  2934. */
  2935. core_instance->agent->callbacks->respond(core_instance->agent,
  2936. AST_CC_AGENT_RESPONSE_FAILURE_INVALID);
  2937. }
  2938. ast_free(args);
  2939. cc_unref(core_instance, "Unref core instance from when it was found earlier");
  2940. return -1;
  2941. }
  2942. /* We can change to the new state now. */
  2943. previous_state = core_instance->current_state;
  2944. core_instance->current_state = args->state;
  2945. res = state_change_funcs[core_instance->current_state](core_instance, args, previous_state);
  2946. /* If state change successful then notify any device state watchers of the change */
  2947. if (!res && !strcmp(core_instance->agent->callbacks->type, "generic")) {
  2948. ccss_notify_device_state_change(core_instance->agent->device_name, core_instance->current_state);
  2949. }
  2950. ast_free(args);
  2951. cc_unref(core_instance, "Unref since state change has completed"); /* From ao2_find */
  2952. return res;
  2953. }
  2954. static int cc_request_state_change(enum cc_state state, const int core_id, const char *debug, va_list ap)
  2955. {
  2956. int res;
  2957. int debuglen;
  2958. char dummy[1];
  2959. va_list aq;
  2960. struct cc_core_instance *core_instance;
  2961. struct cc_state_change_args *args;
  2962. /* This initial call to vsnprintf is simply to find what the
  2963. * size of the string needs to be
  2964. */
  2965. va_copy(aq, ap);
  2966. /* We add 1 to the result since vsnprintf's return does not
  2967. * include the terminating null byte
  2968. */
  2969. debuglen = vsnprintf(dummy, sizeof(dummy), debug, aq) + 1;
  2970. va_end(aq);
  2971. if (!(args = ast_calloc(1, sizeof(*args) + debuglen))) {
  2972. return -1;
  2973. }
  2974. core_instance = find_cc_core_instance(core_id);
  2975. if (!core_instance) {
  2976. ast_log_dynamic_level(cc_logger_level, "Core %d: Unable to find core instance.\n",
  2977. core_id);
  2978. ast_free(args);
  2979. return -1;
  2980. }
  2981. args->core_instance = core_instance;
  2982. args->state = state;
  2983. args->core_id = core_id;
  2984. vsnprintf(args->debug, debuglen, debug, ap);
  2985. res = ast_taskprocessor_push(cc_core_taskprocessor, cc_do_state_change, args);
  2986. if (res) {
  2987. cc_unref(core_instance, "Unref core instance. ast_taskprocessor_push failed");
  2988. ast_free(args);
  2989. }
  2990. return res;
  2991. }
  2992. struct cc_recall_ds_data {
  2993. int core_id;
  2994. char ignore;
  2995. char nested;
  2996. struct cc_monitor_tree *interface_tree;
  2997. };
  2998. static void *cc_recall_ds_duplicate(void *data)
  2999. {
  3000. struct cc_recall_ds_data *old_data = data;
  3001. struct cc_recall_ds_data *new_data = ast_calloc(1, sizeof(*new_data));
  3002. if (!new_data) {
  3003. return NULL;
  3004. }
  3005. new_data->interface_tree = cc_ref(old_data->interface_tree, "Bump refcount of monitor tree for recall datastore duplicate");
  3006. new_data->core_id = old_data->core_id;
  3007. new_data->nested = 1;
  3008. return new_data;
  3009. }
  3010. static void cc_recall_ds_destroy(void *data)
  3011. {
  3012. struct cc_recall_ds_data *recall_data = data;
  3013. recall_data->interface_tree = cc_unref(recall_data->interface_tree, "Unref recall monitor tree");
  3014. ast_free(recall_data);
  3015. }
  3016. static const struct ast_datastore_info recall_ds_info = {
  3017. .type = "cc_recall",
  3018. .duplicate = cc_recall_ds_duplicate,
  3019. .destroy = cc_recall_ds_destroy,
  3020. };
  3021. int ast_setup_cc_recall_datastore(struct ast_channel *chan, const int core_id)
  3022. {
  3023. struct ast_datastore *recall_datastore = ast_datastore_alloc(&recall_ds_info, NULL);
  3024. struct cc_recall_ds_data *recall_data;
  3025. struct cc_core_instance *core_instance;
  3026. if (!recall_datastore) {
  3027. return -1;
  3028. }
  3029. if (!(recall_data = ast_calloc(1, sizeof(*recall_data)))) {
  3030. ast_datastore_free(recall_datastore);
  3031. return -1;
  3032. }
  3033. if (!(core_instance = find_cc_core_instance(core_id))) {
  3034. ast_free(recall_data);
  3035. ast_datastore_free(recall_datastore);
  3036. return -1;
  3037. }
  3038. recall_data->interface_tree = cc_ref(core_instance->monitors,
  3039. "Bump refcount for monitor tree for recall datastore");
  3040. recall_data->core_id = core_id;
  3041. recall_datastore->data = recall_data;
  3042. recall_datastore->inheritance = DATASTORE_INHERIT_FOREVER;
  3043. ast_channel_lock(chan);
  3044. ast_channel_datastore_add(chan, recall_datastore);
  3045. ast_channel_unlock(chan);
  3046. cc_unref(core_instance, "Recall datastore set up. No need for core_instance ref");
  3047. return 0;
  3048. }
  3049. int ast_cc_is_recall(struct ast_channel *chan, int *core_id, const char * const monitor_type)
  3050. {
  3051. struct ast_datastore *recall_datastore;
  3052. struct cc_recall_ds_data *recall_data;
  3053. struct cc_monitor_tree *interface_tree;
  3054. char device_name[AST_CHANNEL_NAME];
  3055. struct ast_cc_monitor *device_monitor;
  3056. int core_id_candidate;
  3057. ast_assert(core_id != NULL);
  3058. *core_id = -1;
  3059. ast_channel_lock(chan);
  3060. if (!(recall_datastore = ast_channel_datastore_find(chan, &recall_ds_info, NULL))) {
  3061. /* Obviously not a recall if the datastore isn't present */
  3062. ast_channel_unlock(chan);
  3063. return 0;
  3064. }
  3065. recall_data = recall_datastore->data;
  3066. if (recall_data->ignore) {
  3067. /* Though this is a recall, the call to this particular interface is not part of the
  3068. * recall either because this is a call forward or because this is not the first
  3069. * invocation of Dial during this call
  3070. */
  3071. ast_channel_unlock(chan);
  3072. return 0;
  3073. }
  3074. if (!recall_data->nested) {
  3075. /* If the nested flag is not set, then this means that
  3076. * the channel passed to this function is the caller making
  3077. * the recall. This means that we shouldn't look through
  3078. * the monitor tree for the channel because it shouldn't be
  3079. * there. However, this is a recall though, so return true.
  3080. */
  3081. *core_id = recall_data->core_id;
  3082. ast_channel_unlock(chan);
  3083. return 1;
  3084. }
  3085. if (ast_strlen_zero(monitor_type)) {
  3086. /* If someone passed a NULL or empty monitor type, then it is clear
  3087. * the channel they passed in was an incoming channel, and so searching
  3088. * the list of dialed interfaces is not going to be helpful. Just return
  3089. * false immediately.
  3090. */
  3091. ast_channel_unlock(chan);
  3092. return 0;
  3093. }
  3094. interface_tree = recall_data->interface_tree;
  3095. ast_channel_get_device_name(chan, device_name, sizeof(device_name));
  3096. /* We grab the value of the recall_data->core_id so that we
  3097. * can unlock the channel before we start looking through the
  3098. * interface list. That way we don't have to worry about a possible
  3099. * clash between the channel lock and the monitor tree lock.
  3100. */
  3101. core_id_candidate = recall_data->core_id;
  3102. ast_channel_unlock(chan);
  3103. /*
  3104. * Now we need to find out if the channel device name
  3105. * is in the list of interfaces in the called tree.
  3106. */
  3107. AST_LIST_LOCK(interface_tree);
  3108. AST_LIST_TRAVERSE(interface_tree, device_monitor, next) {
  3109. if (!strcmp(device_monitor->interface->device_name, device_name) &&
  3110. !strcmp(device_monitor->interface->monitor_type, monitor_type)) {
  3111. /* BOOM! Device is in the tree! We have a winner! */
  3112. *core_id = core_id_candidate;
  3113. AST_LIST_UNLOCK(interface_tree);
  3114. return 1;
  3115. }
  3116. }
  3117. AST_LIST_UNLOCK(interface_tree);
  3118. return 0;
  3119. }
  3120. struct ast_cc_monitor *ast_cc_get_monitor_by_recall_core_id(const int core_id, const char * const device_name)
  3121. {
  3122. struct cc_core_instance *core_instance = find_cc_core_instance(core_id);
  3123. struct ast_cc_monitor *monitor_iter;
  3124. if (!core_instance) {
  3125. return NULL;
  3126. }
  3127. AST_LIST_LOCK(core_instance->monitors);
  3128. AST_LIST_TRAVERSE(core_instance->monitors, monitor_iter, next) {
  3129. if (!strcmp(monitor_iter->interface->device_name, device_name)) {
  3130. /* Found a monitor. */
  3131. cc_ref(monitor_iter, "Hand the requester of the monitor a reference");
  3132. break;
  3133. }
  3134. }
  3135. AST_LIST_UNLOCK(core_instance->monitors);
  3136. cc_unref(core_instance, "Done with core instance ref in ast_cc_get_monitor_by_recall_core_id");
  3137. return monitor_iter;
  3138. }
  3139. /*!
  3140. * \internal
  3141. * \brief uniquely append a dialstring to our CC_INTERFACES chanvar string.
  3142. *
  3143. * We will only append a string if it has not already appeared in our channel
  3144. * variable earlier. We ensure that we don't erroneously match substrings by
  3145. * adding an ampersand to the end of our potential dialstring and searching for
  3146. * it plus the ampersand in our variable.
  3147. *
  3148. * It's important to note that once we have built the full CC_INTERFACES string,
  3149. * there will be an extra ampersand at the end which must be stripped off by
  3150. * the caller of this function.
  3151. *
  3152. * \param str An ast_str holding what we will add to CC_INTERFACES
  3153. * \param dialstring A new dialstring to add
  3154. * \retval void
  3155. */
  3156. static void cc_unique_append(struct ast_str **str, const char *dialstring)
  3157. {
  3158. char dialstring_search[AST_CHANNEL_NAME + 1];
  3159. if (ast_strlen_zero(dialstring)) {
  3160. /* No dialstring to append. */
  3161. return;
  3162. }
  3163. snprintf(dialstring_search, sizeof(dialstring_search), "%s%c", dialstring, '&');
  3164. if (strstr(ast_str_buffer(*str), dialstring_search)) {
  3165. return;
  3166. }
  3167. ast_str_append(str, 0, "%s", dialstring_search);
  3168. }
  3169. /*!
  3170. * \internal
  3171. * \brief Build the CC_INTERFACES channel variable
  3172. *
  3173. * The method used is to traverse the child dialstrings in the
  3174. * passed-in extension monitor, adding any that have the is_valid
  3175. * flag set. Then, traverse the monitors, finding all children
  3176. * of the starting extension monitor and adding their dialstrings
  3177. * as well.
  3178. *
  3179. * \param starting_point The extension monitor that is the parent to all
  3180. * monitors whose dialstrings should be added to CC_INTERFACES
  3181. * \param str Where we will store CC_INTERFACES
  3182. * \retval void
  3183. */
  3184. static void build_cc_interfaces_chanvar(struct ast_cc_monitor *starting_point, struct ast_str **str)
  3185. {
  3186. struct extension_monitor_pvt *extension_pvt;
  3187. struct extension_child_dialstring *child_dialstring;
  3188. struct ast_cc_monitor *monitor_iter = starting_point;
  3189. int top_level_id = starting_point->id;
  3190. size_t length;
  3191. /* Init to an empty string. */
  3192. ast_str_truncate(*str, 0);
  3193. /* First we need to take all of the is_valid child_dialstrings from
  3194. * the extension monitor we found and add them to the CC_INTERFACES
  3195. * chanvar
  3196. */
  3197. extension_pvt = starting_point->private_data;
  3198. AST_LIST_TRAVERSE(&extension_pvt->child_dialstrings, child_dialstring, next) {
  3199. if (child_dialstring->is_valid) {
  3200. cc_unique_append(str, child_dialstring->original_dialstring);
  3201. }
  3202. }
  3203. /* And now we get the dialstrings from each of the device monitors */
  3204. while ((monitor_iter = AST_LIST_NEXT(monitor_iter, next))) {
  3205. if (monitor_iter->parent_id == top_level_id) {
  3206. cc_unique_append(str, monitor_iter->dialstring);
  3207. }
  3208. }
  3209. /* str will have an extra '&' tacked onto the end of it, so we need
  3210. * to get rid of that.
  3211. */
  3212. length = ast_str_strlen(*str);
  3213. if (length) {
  3214. ast_str_truncate(*str, length - 1);
  3215. }
  3216. if (length <= 1) {
  3217. /* Nothing to recall? This should not happen. */
  3218. ast_log(LOG_ERROR, "CC_INTERFACES is empty. starting device_name:'%s'\n",
  3219. starting_point->interface->device_name);
  3220. }
  3221. }
  3222. int ast_cc_agent_set_interfaces_chanvar(struct ast_channel *chan)
  3223. {
  3224. struct ast_datastore *recall_datastore;
  3225. struct cc_monitor_tree *interface_tree;
  3226. struct ast_cc_monitor *monitor;
  3227. struct cc_recall_ds_data *recall_data;
  3228. struct ast_str *str = ast_str_create(64);
  3229. int core_id;
  3230. if (!str) {
  3231. return -1;
  3232. }
  3233. ast_channel_lock(chan);
  3234. if (!(recall_datastore = ast_channel_datastore_find(chan, &recall_ds_info, NULL))) {
  3235. ast_channel_unlock(chan);
  3236. ast_free(str);
  3237. return -1;
  3238. }
  3239. recall_data = recall_datastore->data;
  3240. interface_tree = recall_data->interface_tree;
  3241. core_id = recall_data->core_id;
  3242. ast_channel_unlock(chan);
  3243. AST_LIST_LOCK(interface_tree);
  3244. monitor = AST_LIST_FIRST(interface_tree);
  3245. build_cc_interfaces_chanvar(monitor, &str);
  3246. AST_LIST_UNLOCK(interface_tree);
  3247. pbx_builtin_setvar_helper(chan, "CC_INTERFACES", ast_str_buffer(str));
  3248. ast_log_dynamic_level(cc_logger_level, "Core %d: CC_INTERFACES set to %s\n",
  3249. core_id, ast_str_buffer(str));
  3250. ast_free(str);
  3251. return 0;
  3252. }
  3253. int ast_set_cc_interfaces_chanvar(struct ast_channel *chan, const char * const extension)
  3254. {
  3255. struct ast_datastore *recall_datastore;
  3256. struct cc_monitor_tree *interface_tree;
  3257. struct ast_cc_monitor *monitor_iter;
  3258. struct cc_recall_ds_data *recall_data;
  3259. struct ast_str *str = ast_str_create(64);
  3260. int core_id;
  3261. if (!str) {
  3262. return -1;
  3263. }
  3264. ast_channel_lock(chan);
  3265. if (!(recall_datastore = ast_channel_datastore_find(chan, &recall_ds_info, NULL))) {
  3266. ast_channel_unlock(chan);
  3267. ast_free(str);
  3268. return -1;
  3269. }
  3270. recall_data = recall_datastore->data;
  3271. interface_tree = recall_data->interface_tree;
  3272. core_id = recall_data->core_id;
  3273. ast_channel_unlock(chan);
  3274. AST_LIST_LOCK(interface_tree);
  3275. AST_LIST_TRAVERSE(interface_tree, monitor_iter, next) {
  3276. if (!strcmp(monitor_iter->interface->device_name, extension)) {
  3277. break;
  3278. }
  3279. }
  3280. if (!monitor_iter) {
  3281. /* We couldn't find this extension. This may be because
  3282. * we have been directed into an unexpected extension because
  3283. * the admin has changed a CC_INTERFACES variable at some point.
  3284. */
  3285. AST_LIST_UNLOCK(interface_tree);
  3286. ast_free(str);
  3287. return -1;
  3288. }
  3289. build_cc_interfaces_chanvar(monitor_iter, &str);
  3290. AST_LIST_UNLOCK(interface_tree);
  3291. pbx_builtin_setvar_helper(chan, "CC_INTERFACES", ast_str_buffer(str));
  3292. ast_log_dynamic_level(cc_logger_level, "Core %d: CC_INTERFACES set to %s\n",
  3293. core_id, ast_str_buffer(str));
  3294. ast_free(str);
  3295. return 0;
  3296. }
  3297. void ast_ignore_cc(struct ast_channel *chan)
  3298. {
  3299. struct ast_datastore *cc_datastore;
  3300. struct ast_datastore *cc_recall_datastore;
  3301. struct dialed_cc_interfaces *cc_interfaces;
  3302. struct cc_recall_ds_data *recall_cc_data;
  3303. ast_channel_lock(chan);
  3304. if ((cc_datastore = ast_channel_datastore_find(chan, &dialed_cc_interfaces_info, NULL))) {
  3305. cc_interfaces = cc_datastore->data;
  3306. cc_interfaces->ignore = 1;
  3307. }
  3308. if ((cc_recall_datastore = ast_channel_datastore_find(chan, &recall_ds_info, NULL))) {
  3309. recall_cc_data = cc_recall_datastore->data;
  3310. recall_cc_data->ignore = 1;
  3311. }
  3312. ast_channel_unlock(chan);
  3313. }
  3314. static __attribute__((format(printf, 2, 3))) int cc_offer(const int core_id, const char * const debug, ...)
  3315. {
  3316. va_list ap;
  3317. int res;
  3318. va_start(ap, debug);
  3319. res = cc_request_state_change(CC_CALLER_OFFERED, core_id, debug, ap);
  3320. va_end(ap);
  3321. return res;
  3322. }
  3323. int ast_cc_offer(struct ast_channel *caller_chan)
  3324. {
  3325. int core_id;
  3326. int res = -1;
  3327. struct ast_datastore *datastore;
  3328. struct dialed_cc_interfaces *cc_interfaces;
  3329. char cc_is_offerable;
  3330. ast_channel_lock(caller_chan);
  3331. if (!(datastore = ast_channel_datastore_find(caller_chan, &dialed_cc_interfaces_info, NULL))) {
  3332. ast_channel_unlock(caller_chan);
  3333. return res;
  3334. }
  3335. cc_interfaces = datastore->data;
  3336. cc_is_offerable = cc_interfaces->is_original_caller;
  3337. core_id = cc_interfaces->core_id;
  3338. ast_channel_unlock(caller_chan);
  3339. if (cc_is_offerable) {
  3340. res = cc_offer(core_id, "CC offered to caller %s", ast_channel_name(caller_chan));
  3341. }
  3342. return res;
  3343. }
  3344. int ast_cc_agent_accept_request(int core_id, const char * const debug, ...)
  3345. {
  3346. va_list ap;
  3347. int res;
  3348. va_start(ap, debug);
  3349. res = cc_request_state_change(CC_CALLER_REQUESTED, core_id, debug, ap);
  3350. va_end(ap);
  3351. return res;
  3352. }
  3353. int ast_cc_monitor_request_acked(int core_id, const char * const debug, ...)
  3354. {
  3355. va_list ap;
  3356. int res;
  3357. va_start(ap, debug);
  3358. res = cc_request_state_change(CC_ACTIVE, core_id, debug, ap);
  3359. va_end(ap);
  3360. return res;
  3361. }
  3362. int ast_cc_monitor_callee_available(const int core_id, const char * const debug, ...)
  3363. {
  3364. va_list ap;
  3365. int res;
  3366. va_start(ap, debug);
  3367. res = cc_request_state_change(CC_CALLEE_READY, core_id, debug, ap);
  3368. va_end(ap);
  3369. return res;
  3370. }
  3371. int ast_cc_agent_caller_busy(int core_id, const char * debug, ...)
  3372. {
  3373. va_list ap;
  3374. int res;
  3375. va_start(ap, debug);
  3376. res = cc_request_state_change(CC_CALLER_BUSY, core_id, debug, ap);
  3377. va_end(ap);
  3378. return res;
  3379. }
  3380. int ast_cc_agent_caller_available(int core_id, const char * const debug, ...)
  3381. {
  3382. va_list ap;
  3383. int res;
  3384. va_start(ap, debug);
  3385. res = cc_request_state_change(CC_ACTIVE, core_id, debug, ap);
  3386. va_end(ap);
  3387. return res;
  3388. }
  3389. int ast_cc_agent_recalling(int core_id, const char * const debug, ...)
  3390. {
  3391. va_list ap;
  3392. int res;
  3393. va_start(ap, debug);
  3394. res = cc_request_state_change(CC_RECALLING, core_id, debug, ap);
  3395. va_end(ap);
  3396. return res;
  3397. }
  3398. int ast_cc_completed(struct ast_channel *chan, const char * const debug, ...)
  3399. {
  3400. struct ast_datastore *recall_datastore;
  3401. struct cc_recall_ds_data *recall_data;
  3402. int core_id;
  3403. va_list ap;
  3404. int res;
  3405. ast_channel_lock(chan);
  3406. if (!(recall_datastore = ast_channel_datastore_find(chan, &recall_ds_info, NULL))) {
  3407. /* Silly! Why did you call this function if there's no recall DS? */
  3408. ast_channel_unlock(chan);
  3409. return -1;
  3410. }
  3411. recall_data = recall_datastore->data;
  3412. if (recall_data->nested || recall_data->ignore) {
  3413. /* If this is being called from a nested Dial, it is too
  3414. * early to determine if the recall has actually completed.
  3415. * The outermost dial is the only one with the authority to
  3416. * declare the recall to be complete.
  3417. *
  3418. * Similarly, if this function has been called when the
  3419. * recall has progressed beyond the first dial, this is not
  3420. * a legitimate time to declare the recall to be done. In fact,
  3421. * that should have been done already.
  3422. */
  3423. ast_channel_unlock(chan);
  3424. return -1;
  3425. }
  3426. core_id = recall_data->core_id;
  3427. ast_channel_unlock(chan);
  3428. va_start(ap, debug);
  3429. res = cc_request_state_change(CC_COMPLETE, core_id, debug, ap);
  3430. va_end(ap);
  3431. return res;
  3432. }
  3433. int ast_cc_failed(int core_id, const char * const debug, ...)
  3434. {
  3435. va_list ap;
  3436. int res;
  3437. va_start(ap, debug);
  3438. res = cc_request_state_change(CC_FAILED, core_id, debug, ap);
  3439. va_end(ap);
  3440. return res;
  3441. }
  3442. struct ast_cc_monitor_failure_data {
  3443. const char *device_name;
  3444. char *debug;
  3445. int core_id;
  3446. };
  3447. static int cc_monitor_failed(void *data)
  3448. {
  3449. struct ast_cc_monitor_failure_data *failure_data = data;
  3450. struct cc_core_instance *core_instance;
  3451. struct ast_cc_monitor *monitor_iter;
  3452. core_instance = find_cc_core_instance(failure_data->core_id);
  3453. if (!core_instance) {
  3454. /* Core instance no longer exists or invalid core_id. */
  3455. ast_log_dynamic_level(cc_logger_level,
  3456. "Core %d: Could not find core instance for device %s '%s'\n",
  3457. failure_data->core_id, failure_data->device_name, failure_data->debug);
  3458. ast_free((char *) failure_data->device_name);
  3459. ast_free((char *) failure_data->debug);
  3460. ast_free(failure_data);
  3461. return -1;
  3462. }
  3463. AST_LIST_LOCK(core_instance->monitors);
  3464. AST_LIST_TRAVERSE_SAFE_BEGIN(core_instance->monitors, monitor_iter, next) {
  3465. if (monitor_iter->interface->monitor_class == AST_CC_DEVICE_MONITOR) {
  3466. if (!strcmp(monitor_iter->interface->device_name, failure_data->device_name)) {
  3467. AST_LIST_REMOVE_CURRENT(next);
  3468. cc_extension_monitor_change_is_valid(core_instance, monitor_iter->parent_id,
  3469. monitor_iter->interface->device_name, 1);
  3470. monitor_iter->callbacks->cancel_available_timer(monitor_iter, &monitor_iter->available_timer_id);
  3471. cc_publish_monitorfailed(monitor_iter->core_id, monitor_iter->interface->device_name);
  3472. cc_unref(monitor_iter, "Monitor reported failure. Unref list's reference.");
  3473. }
  3474. }
  3475. }
  3476. AST_LIST_TRAVERSE_SAFE_END;
  3477. if (!has_device_monitors(core_instance)) {
  3478. ast_cc_failed(core_instance->core_id, "All monitors have failed\n");
  3479. }
  3480. AST_LIST_UNLOCK(core_instance->monitors);
  3481. cc_unref(core_instance, "Finished with core_instance in cc_monitor_failed\n");
  3482. ast_free((char *) failure_data->device_name);
  3483. ast_free((char *) failure_data->debug);
  3484. ast_free(failure_data);
  3485. return 0;
  3486. }
  3487. int ast_cc_monitor_failed(int core_id, const char *const monitor_name, const char * const debug, ...)
  3488. {
  3489. struct ast_cc_monitor_failure_data *failure_data;
  3490. int res;
  3491. va_list ap;
  3492. if (!(failure_data = ast_calloc(1, sizeof(*failure_data)))) {
  3493. return -1;
  3494. }
  3495. if (!(failure_data->device_name = ast_strdup(monitor_name))) {
  3496. ast_free(failure_data);
  3497. return -1;
  3498. }
  3499. va_start(ap, debug);
  3500. if (ast_vasprintf(&failure_data->debug, debug, ap) == -1) {
  3501. va_end(ap);
  3502. ast_free((char *)failure_data->device_name);
  3503. ast_free(failure_data);
  3504. return -1;
  3505. }
  3506. va_end(ap);
  3507. failure_data->core_id = core_id;
  3508. res = ast_taskprocessor_push(cc_core_taskprocessor, cc_monitor_failed, failure_data);
  3509. if (res) {
  3510. ast_free((char *)failure_data->device_name);
  3511. ast_free((char *)failure_data->debug);
  3512. ast_free(failure_data);
  3513. }
  3514. return res;
  3515. }
  3516. static int cc_status_request(void *data)
  3517. {
  3518. struct cc_core_instance *core_instance= data;
  3519. int res;
  3520. res = core_instance->agent->callbacks->status_request(core_instance->agent);
  3521. cc_unref(core_instance, "Status request finished. Unref core instance");
  3522. return res;
  3523. }
  3524. int ast_cc_monitor_status_request(int core_id)
  3525. {
  3526. int res;
  3527. struct cc_core_instance *core_instance = find_cc_core_instance(core_id);
  3528. if (!core_instance) {
  3529. return -1;
  3530. }
  3531. res = ast_taskprocessor_push(cc_core_taskprocessor, cc_status_request, core_instance);
  3532. if (res) {
  3533. cc_unref(core_instance, "Unref core instance. ast_taskprocessor_push failed");
  3534. }
  3535. return res;
  3536. }
  3537. static int cc_stop_ringing(void *data)
  3538. {
  3539. struct cc_core_instance *core_instance = data;
  3540. int res = 0;
  3541. if (core_instance->agent->callbacks->stop_ringing) {
  3542. res = core_instance->agent->callbacks->stop_ringing(core_instance->agent);
  3543. }
  3544. /* If an agent is being asked to stop ringing, then he needs to be prepared if for
  3545. * whatever reason he needs to be called back again. The proper state to be in to
  3546. * detect such a circumstance is the CC_ACTIVE state.
  3547. *
  3548. * We get to this state using the slightly unintuitive method of calling
  3549. * ast_cc_monitor_request_acked because it gets us to the proper state.
  3550. */
  3551. ast_cc_monitor_request_acked(core_instance->core_id, "Agent %s asked to stop ringing. Be prepared to be recalled again.",
  3552. core_instance->agent->device_name);
  3553. cc_unref(core_instance, "Stop ringing finished. Unref core_instance");
  3554. return res;
  3555. }
  3556. int ast_cc_monitor_stop_ringing(int core_id)
  3557. {
  3558. int res;
  3559. struct cc_core_instance *core_instance = find_cc_core_instance(core_id);
  3560. if (!core_instance) {
  3561. return -1;
  3562. }
  3563. res = ast_taskprocessor_push(cc_core_taskprocessor, cc_stop_ringing, core_instance);
  3564. if (res) {
  3565. cc_unref(core_instance, "Unref core instance. ast_taskprocessor_push failed");
  3566. }
  3567. return res;
  3568. }
  3569. static int cc_party_b_free(void *data)
  3570. {
  3571. struct cc_core_instance *core_instance = data;
  3572. int res = 0;
  3573. if (core_instance->agent->callbacks->party_b_free) {
  3574. res = core_instance->agent->callbacks->party_b_free(core_instance->agent);
  3575. }
  3576. cc_unref(core_instance, "Party B free finished. Unref core_instance");
  3577. return res;
  3578. }
  3579. int ast_cc_monitor_party_b_free(int core_id)
  3580. {
  3581. int res;
  3582. struct cc_core_instance *core_instance = find_cc_core_instance(core_id);
  3583. if (!core_instance) {
  3584. return -1;
  3585. }
  3586. res = ast_taskprocessor_push(cc_core_taskprocessor, cc_party_b_free, core_instance);
  3587. if (res) {
  3588. cc_unref(core_instance, "Unref core instance. ast_taskprocessor_push failed");
  3589. }
  3590. return res;
  3591. }
  3592. struct cc_status_response_args {
  3593. struct cc_core_instance *core_instance;
  3594. enum ast_device_state devstate;
  3595. };
  3596. static int cc_status_response(void *data)
  3597. {
  3598. struct cc_status_response_args *args = data;
  3599. struct cc_core_instance *core_instance = args->core_instance;
  3600. struct ast_cc_monitor *monitor_iter;
  3601. enum ast_device_state devstate = args->devstate;
  3602. ast_free(args);
  3603. AST_LIST_LOCK(core_instance->monitors);
  3604. AST_LIST_TRAVERSE(core_instance->monitors, monitor_iter, next) {
  3605. if (monitor_iter->interface->monitor_class == AST_CC_DEVICE_MONITOR &&
  3606. monitor_iter->callbacks->status_response) {
  3607. monitor_iter->callbacks->status_response(monitor_iter, devstate);
  3608. }
  3609. }
  3610. AST_LIST_UNLOCK(core_instance->monitors);
  3611. cc_unref(core_instance, "Status response finished. Unref core instance");
  3612. return 0;
  3613. }
  3614. int ast_cc_agent_status_response(int core_id, enum ast_device_state devstate)
  3615. {
  3616. struct cc_status_response_args *args;
  3617. struct cc_core_instance *core_instance;
  3618. int res;
  3619. args = ast_calloc(1, sizeof(*args));
  3620. if (!args) {
  3621. return -1;
  3622. }
  3623. core_instance = find_cc_core_instance(core_id);
  3624. if (!core_instance) {
  3625. ast_free(args);
  3626. return -1;
  3627. }
  3628. args->core_instance = core_instance;
  3629. args->devstate = devstate;
  3630. res = ast_taskprocessor_push(cc_core_taskprocessor, cc_status_response, args);
  3631. if (res) {
  3632. cc_unref(core_instance, "Unref core instance. ast_taskprocessor_push failed");
  3633. ast_free(args);
  3634. }
  3635. return res;
  3636. }
  3637. static int cc_build_payload(struct ast_channel *chan, struct ast_cc_config_params *cc_params,
  3638. const char *monitor_type, const char * const device_name, const char * dialstring,
  3639. enum ast_cc_service_type service, void *private_data, struct cc_control_payload *payload)
  3640. {
  3641. struct ast_datastore *datastore;
  3642. struct dialed_cc_interfaces *cc_interfaces;
  3643. int dial_parent_id;
  3644. ast_channel_lock(chan);
  3645. datastore = ast_channel_datastore_find(chan, &dialed_cc_interfaces_info, NULL);
  3646. if (!datastore) {
  3647. ast_channel_unlock(chan);
  3648. return -1;
  3649. }
  3650. cc_interfaces = datastore->data;
  3651. dial_parent_id = cc_interfaces->dial_parent_id;
  3652. ast_channel_unlock(chan);
  3653. payload->monitor_type = monitor_type;
  3654. payload->private_data = private_data;
  3655. payload->service = service;
  3656. ast_cc_copy_config_params(&payload->config_params, cc_params);
  3657. payload->parent_interface_id = dial_parent_id;
  3658. ast_copy_string(payload->device_name, device_name, sizeof(payload->device_name));
  3659. ast_copy_string(payload->dialstring, dialstring, sizeof(payload->dialstring));
  3660. return 0;
  3661. }
  3662. int ast_queue_cc_frame(struct ast_channel *chan, const char *monitor_type,
  3663. const char * const dialstring, enum ast_cc_service_type service, void *private_data)
  3664. {
  3665. struct ast_frame frame = {0,};
  3666. char device_name[AST_CHANNEL_NAME];
  3667. int retval;
  3668. struct ast_cc_config_params *cc_params;
  3669. cc_params = ast_channel_get_cc_config_params(chan);
  3670. if (!cc_params) {
  3671. return -1;
  3672. }
  3673. ast_channel_get_device_name(chan, device_name, sizeof(device_name));
  3674. if (ast_cc_monitor_count(device_name, monitor_type) >= ast_get_cc_max_monitors(cc_params)) {
  3675. ast_log(LOG_NOTICE, "Not queuing a CC frame for device %s since it already has its maximum monitors allocated\n", device_name);
  3676. return -1;
  3677. }
  3678. if (ast_cc_build_frame(chan, cc_params, monitor_type, device_name, dialstring, service, private_data, &frame)) {
  3679. /* Frame building failed. We can't use this. */
  3680. return -1;
  3681. }
  3682. retval = ast_queue_frame(chan, &frame);
  3683. ast_frfree(&frame);
  3684. return retval;
  3685. }
  3686. int ast_cc_build_frame(struct ast_channel *chan, struct ast_cc_config_params *cc_params,
  3687. const char *monitor_type, const char * const device_name,
  3688. const char * const dialstring, enum ast_cc_service_type service, void *private_data,
  3689. struct ast_frame *frame)
  3690. {
  3691. struct cc_control_payload *payload = ast_calloc(1, sizeof(*payload));
  3692. if (!payload) {
  3693. return -1;
  3694. }
  3695. if (cc_build_payload(chan, cc_params, monitor_type, device_name, dialstring, service, private_data, payload)) {
  3696. /* Something screwed up, we can't make a frame with this */
  3697. ast_free(payload);
  3698. return -1;
  3699. }
  3700. frame->frametype = AST_FRAME_CONTROL;
  3701. frame->subclass.integer = AST_CONTROL_CC;
  3702. frame->data.ptr = payload;
  3703. frame->datalen = sizeof(*payload);
  3704. frame->mallocd = AST_MALLOCD_DATA;
  3705. return 0;
  3706. }
  3707. void ast_cc_call_failed(struct ast_channel *incoming, struct ast_channel *outgoing, const char * const dialstring)
  3708. {
  3709. char device_name[AST_CHANNEL_NAME];
  3710. struct cc_control_payload payload;
  3711. struct ast_cc_config_params *cc_params;
  3712. if (ast_channel_hangupcause(outgoing) != AST_CAUSE_BUSY && ast_channel_hangupcause(outgoing) != AST_CAUSE_CONGESTION) {
  3713. /* It doesn't make sense to try to offer CCBS to the caller if the reason for ast_call
  3714. * failing is something other than busy or congestion
  3715. */
  3716. return;
  3717. }
  3718. cc_params = ast_channel_get_cc_config_params(outgoing);
  3719. if (!cc_params) {
  3720. return;
  3721. }
  3722. if (ast_get_cc_monitor_policy(cc_params) != AST_CC_MONITOR_GENERIC) {
  3723. /* This sort of CCBS only works if using generic CC. For native, we would end up sending
  3724. * a CC request for a non-existent call. The far end will reject this every time
  3725. */
  3726. return;
  3727. }
  3728. ast_channel_get_device_name(outgoing, device_name, sizeof(device_name));
  3729. if (cc_build_payload(outgoing, cc_params, AST_CC_GENERIC_MONITOR_TYPE, device_name,
  3730. dialstring, AST_CC_CCBS, NULL, &payload)) {
  3731. /* Something screwed up, we can't make a frame with this */
  3732. return;
  3733. }
  3734. ast_handle_cc_control_frame(incoming, outgoing, &payload);
  3735. }
  3736. void ast_cc_busy_interface(struct ast_channel *inbound, struct ast_cc_config_params *cc_params,
  3737. const char *monitor_type, const char * const device_name, const char * const dialstring, void *private_data)
  3738. {
  3739. struct cc_control_payload payload;
  3740. if (cc_build_payload(inbound, cc_params, monitor_type, device_name, dialstring, AST_CC_CCBS, private_data, &payload)) {
  3741. /* Something screwed up. Don't try to handle this payload */
  3742. call_destructor_with_no_monitor(monitor_type, private_data);
  3743. return;
  3744. }
  3745. ast_handle_cc_control_frame(inbound, NULL, &payload);
  3746. }
  3747. int ast_cc_callback(struct ast_channel *inbound, const char * const tech, const char * const dest, ast_cc_callback_fn callback)
  3748. {
  3749. const struct ast_channel_tech *chantech = ast_get_channel_tech(tech);
  3750. if (chantech && chantech->cc_callback) {
  3751. chantech->cc_callback(inbound, dest, callback);
  3752. }
  3753. return 0;
  3754. }
  3755. static const char *ccreq_app = "CallCompletionRequest";
  3756. static int ccreq_exec(struct ast_channel *chan, const char *data)
  3757. {
  3758. struct cc_core_instance *core_instance;
  3759. char device_name[AST_CHANNEL_NAME];
  3760. unsigned long match_flags;
  3761. int res;
  3762. ast_channel_get_device_name(chan, device_name, sizeof(device_name));
  3763. match_flags = MATCH_NO_REQUEST;
  3764. if (!(core_instance = ao2_t_callback_data(cc_core_instances, 0, match_agent, device_name, &match_flags, "Find core instance for CallCompletionRequest"))) {
  3765. ast_log_dynamic_level(cc_logger_level, "Couldn't find a core instance for caller %s\n", device_name);
  3766. pbx_builtin_setvar_helper(chan, "CC_REQUEST_RESULT", "FAIL");
  3767. pbx_builtin_setvar_helper(chan, "CC_REQUEST_REASON", "NO_CORE_INSTANCE");
  3768. return 0;
  3769. }
  3770. ast_log_dynamic_level(cc_logger_level, "Core %d: Found core_instance for caller %s\n",
  3771. core_instance->core_id, device_name);
  3772. if (strcmp(core_instance->agent->callbacks->type, "generic")) {
  3773. ast_log_dynamic_level(cc_logger_level, "Core %d: CallCompletionRequest is only for generic agent types.\n",
  3774. core_instance->core_id);
  3775. pbx_builtin_setvar_helper(chan, "CC_REQUEST_RESULT", "FAIL");
  3776. pbx_builtin_setvar_helper(chan, "CC_REQUEST_REASON", "NOT_GENERIC");
  3777. cc_unref(core_instance, "Unref core_instance since CallCompletionRequest was called with native agent");
  3778. return 0;
  3779. }
  3780. if (!ast_cc_request_is_within_limits()) {
  3781. ast_log_dynamic_level(cc_logger_level, "Core %d: CallCompletionRequest failed. Too many requests in the system\n",
  3782. core_instance->core_id);
  3783. ast_cc_failed(core_instance->core_id, "Too many CC requests\n");
  3784. pbx_builtin_setvar_helper(chan, "CC_REQUEST_RESULT", "FAIL");
  3785. pbx_builtin_setvar_helper(chan, "CC_REQUEST_REASON", "TOO_MANY_REQUESTS");
  3786. cc_unref(core_instance, "Unref core_instance since too many CC requests");
  3787. return 0;
  3788. }
  3789. res = ast_cc_agent_accept_request(core_instance->core_id, "CallCompletionRequest called by caller %s for core_id %d", device_name, core_instance->core_id);
  3790. pbx_builtin_setvar_helper(chan, "CC_REQUEST_RESULT", res ? "FAIL" : "SUCCESS");
  3791. if (res) {
  3792. pbx_builtin_setvar_helper(chan, "CC_REQUEST_REASON", "UNSPECIFIED");
  3793. }
  3794. cc_unref(core_instance, "Done with CallCompletionRequest");
  3795. return 0;
  3796. }
  3797. static const char *cccancel_app = "CallCompletionCancel";
  3798. static int cccancel_exec(struct ast_channel *chan, const char *data)
  3799. {
  3800. struct cc_core_instance *core_instance;
  3801. char device_name[AST_CHANNEL_NAME];
  3802. unsigned long match_flags;
  3803. int res;
  3804. ast_channel_get_device_name(chan, device_name, sizeof(device_name));
  3805. match_flags = MATCH_REQUEST;
  3806. if (!(core_instance = ao2_t_callback_data(cc_core_instances, 0, match_agent, device_name, &match_flags, "Find core instance for CallCompletionCancel"))) {
  3807. ast_log_dynamic_level(cc_logger_level, "Cannot find CC transaction to cancel for caller %s\n", device_name);
  3808. pbx_builtin_setvar_helper(chan, "CC_CANCEL_RESULT", "FAIL");
  3809. pbx_builtin_setvar_helper(chan, "CC_CANCEL_REASON", "NO_CORE_INSTANCE");
  3810. return 0;
  3811. }
  3812. if (strcmp(core_instance->agent->callbacks->type, "generic")) {
  3813. ast_log(LOG_WARNING, "CallCompletionCancel may only be used for calles with a generic agent\n");
  3814. cc_unref(core_instance, "Unref core instance found during CallCompletionCancel");
  3815. pbx_builtin_setvar_helper(chan, "CC_CANCEL_RESULT", "FAIL");
  3816. pbx_builtin_setvar_helper(chan, "CC_CANCEL_REASON", "NOT_GENERIC");
  3817. return 0;
  3818. }
  3819. res = ast_cc_failed(core_instance->core_id, "Call completion request Cancelled for core ID %d by caller %s",
  3820. core_instance->core_id, device_name);
  3821. cc_unref(core_instance, "Unref core instance found during CallCompletionCancel");
  3822. pbx_builtin_setvar_helper(chan, "CC_CANCEL_RESULT", res ? "FAIL" : "SUCCESS");
  3823. if (res) {
  3824. pbx_builtin_setvar_helper(chan, "CC_CANCEL_REASON", "UNSPECIFIED");
  3825. }
  3826. return 0;
  3827. }
  3828. struct count_monitors_cb_data {
  3829. const char *device_name;
  3830. const char *monitor_type;
  3831. int count;
  3832. };
  3833. static int count_monitors_cb(void *obj, void *arg, int flags)
  3834. {
  3835. struct cc_core_instance *core_instance = obj;
  3836. struct count_monitors_cb_data *cb_data = arg;
  3837. const char *device_name = cb_data->device_name;
  3838. const char *monitor_type = cb_data->monitor_type;
  3839. struct ast_cc_monitor *monitor_iter;
  3840. AST_LIST_LOCK(core_instance->monitors);
  3841. AST_LIST_TRAVERSE(core_instance->monitors, monitor_iter, next) {
  3842. if (!strcmp(monitor_iter->interface->device_name, device_name) &&
  3843. !strcmp(monitor_iter->interface->monitor_type, monitor_type)) {
  3844. cb_data->count++;
  3845. break;
  3846. }
  3847. }
  3848. AST_LIST_UNLOCK(core_instance->monitors);
  3849. return 0;
  3850. }
  3851. int ast_cc_monitor_count(const char * const name, const char * const type)
  3852. {
  3853. struct count_monitors_cb_data data = {.device_name = name, .monitor_type = type,};
  3854. ao2_t_callback(cc_core_instances, OBJ_NODATA, count_monitors_cb, &data, "Counting agents");
  3855. ast_log_dynamic_level(cc_logger_level, "Counted %d monitors\n", data.count);
  3856. return data.count;
  3857. }
  3858. static void initialize_cc_max_requests(void)
  3859. {
  3860. struct ast_config *cc_config;
  3861. const char *cc_max_requests_str;
  3862. struct ast_flags config_flags = {0,};
  3863. char *endptr;
  3864. cc_config = ast_config_load2("ccss.conf", "ccss", config_flags);
  3865. if (!cc_config || cc_config == CONFIG_STATUS_FILEINVALID) {
  3866. ast_log(LOG_WARNING, "Could not find valid ccss.conf file. Using cc_max_requests default\n");
  3867. global_cc_max_requests = GLOBAL_CC_MAX_REQUESTS_DEFAULT;
  3868. return;
  3869. }
  3870. if (!(cc_max_requests_str = ast_variable_retrieve(cc_config, "general", "cc_max_requests"))) {
  3871. ast_config_destroy(cc_config);
  3872. global_cc_max_requests = GLOBAL_CC_MAX_REQUESTS_DEFAULT;
  3873. return;
  3874. }
  3875. global_cc_max_requests = strtol(cc_max_requests_str, &endptr, 10);
  3876. if (!ast_strlen_zero(endptr)) {
  3877. ast_log(LOG_WARNING, "Invalid input given for cc_max_requests. Using default\n");
  3878. global_cc_max_requests = GLOBAL_CC_MAX_REQUESTS_DEFAULT;
  3879. }
  3880. ast_config_destroy(cc_config);
  3881. return;
  3882. }
  3883. /*!
  3884. * \internal
  3885. * \brief helper function to parse and configure each devstate map
  3886. */
  3887. static void initialize_cc_devstate_map_helper(struct ast_config *cc_config, enum cc_state state, const char *cc_setting)
  3888. {
  3889. const char *cc_devstate_str;
  3890. enum ast_device_state this_devstate;
  3891. if ((cc_devstate_str = ast_variable_retrieve(cc_config, "general", cc_setting))) {
  3892. this_devstate = ast_devstate_val(cc_devstate_str);
  3893. if (this_devstate != AST_DEVICE_UNKNOWN) {
  3894. cc_state_to_devstate_map[state] = this_devstate;
  3895. }
  3896. }
  3897. }
  3898. /*!
  3899. * \internal
  3900. * \brief initializes cc_state_to_devstate_map from ccss.conf
  3901. *
  3902. * \details
  3903. * The cc_state_to_devstate_map[] is already initialized with all the
  3904. * default values. This will update that structure with any changes
  3905. * from the ccss.conf file. The configuration parameters in ccss.conf
  3906. * should use any valid device state form that is recognized by
  3907. * ast_devstate_val() function.
  3908. */
  3909. static void initialize_cc_devstate_map(void)
  3910. {
  3911. struct ast_config *cc_config;
  3912. struct ast_flags config_flags = { 0, };
  3913. cc_config = ast_config_load2("ccss.conf", "ccss", config_flags);
  3914. if (!cc_config || cc_config == CONFIG_STATUS_FILEINVALID) {
  3915. ast_log(LOG_WARNING,
  3916. "Could not find valid ccss.conf file. Using cc_[state]_devstate defaults\n");
  3917. return;
  3918. }
  3919. initialize_cc_devstate_map_helper(cc_config, CC_AVAILABLE, "cc_available_devstate");
  3920. initialize_cc_devstate_map_helper(cc_config, CC_CALLER_OFFERED, "cc_caller_offered_devstate");
  3921. initialize_cc_devstate_map_helper(cc_config, CC_CALLER_REQUESTED, "cc_caller_requested_devstate");
  3922. initialize_cc_devstate_map_helper(cc_config, CC_ACTIVE, "cc_active_devstate");
  3923. initialize_cc_devstate_map_helper(cc_config, CC_CALLEE_READY, "cc_callee_ready_devstate");
  3924. initialize_cc_devstate_map_helper(cc_config, CC_CALLER_BUSY, "cc_caller_busy_devstate");
  3925. initialize_cc_devstate_map_helper(cc_config, CC_RECALLING, "cc_recalling_devstate");
  3926. initialize_cc_devstate_map_helper(cc_config, CC_COMPLETE, "cc_complete_devstate");
  3927. initialize_cc_devstate_map_helper(cc_config, CC_FAILED, "cc_failed_devstate");
  3928. ast_config_destroy(cc_config);
  3929. }
  3930. static void cc_cli_print_monitor_stats(struct ast_cc_monitor *monitor, int fd, int parent_id)
  3931. {
  3932. struct ast_cc_monitor *child_monitor_iter = monitor;
  3933. if (!monitor) {
  3934. return;
  3935. }
  3936. ast_cli(fd, "\t\t|-->%s", monitor->interface->device_name);
  3937. if (monitor->interface->monitor_class == AST_CC_DEVICE_MONITOR) {
  3938. ast_cli(fd, "(%s)", cc_service_to_string(monitor->service_offered));
  3939. }
  3940. ast_cli(fd, "\n");
  3941. while ((child_monitor_iter = AST_LIST_NEXT(child_monitor_iter, next))) {
  3942. if (child_monitor_iter->parent_id == monitor->id) {
  3943. cc_cli_print_monitor_stats(child_monitor_iter, fd, child_monitor_iter->id);
  3944. }
  3945. }
  3946. }
  3947. static int print_stats_cb(void *obj, void *arg, int flags)
  3948. {
  3949. int *cli_fd = arg;
  3950. struct cc_core_instance *core_instance = obj;
  3951. ast_cli(*cli_fd, "%d\t\t%s\t\t%s\n", core_instance->core_id, core_instance->agent->device_name,
  3952. cc_state_to_string(core_instance->current_state));
  3953. AST_LIST_LOCK(core_instance->monitors);
  3954. cc_cli_print_monitor_stats(AST_LIST_FIRST(core_instance->monitors), *cli_fd, 0);
  3955. AST_LIST_UNLOCK(core_instance->monitors);
  3956. return 0;
  3957. }
  3958. static int cc_cli_output_status(void *data)
  3959. {
  3960. int *cli_fd = data;
  3961. int count = ao2_container_count(cc_core_instances);
  3962. if (!count) {
  3963. ast_cli(*cli_fd, "There are currently no active call completion transactions\n");
  3964. } else {
  3965. ast_cli(*cli_fd, "%d Call completion transactions\n", count);
  3966. ast_cli(*cli_fd, "Core ID\t\tCaller\t\t\t\tStatus\n");
  3967. ast_cli(*cli_fd, "----------------------------------------------------------------------------\n");
  3968. ao2_t_callback(cc_core_instances, OBJ_NODATA, print_stats_cb, cli_fd, "Printing stats to CLI");
  3969. }
  3970. ast_free(cli_fd);
  3971. return 0;
  3972. }
  3973. static char *handle_cc_status(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
  3974. {
  3975. int *cli_fd;
  3976. switch (cmd) {
  3977. case CLI_INIT:
  3978. e->command = "cc report status";
  3979. e->usage =
  3980. "Usage: cc report status\n"
  3981. " Report the current status of any ongoing CC transactions\n";
  3982. return NULL;
  3983. case CLI_GENERATE:
  3984. return NULL;
  3985. }
  3986. if (a->argc != 3) {
  3987. return CLI_SHOWUSAGE;
  3988. }
  3989. cli_fd = ast_malloc(sizeof(*cli_fd));
  3990. if (!cli_fd) {
  3991. return CLI_FAILURE;
  3992. }
  3993. *cli_fd = a->fd;
  3994. if (ast_taskprocessor_push(cc_core_taskprocessor, cc_cli_output_status, cli_fd)) {
  3995. ast_free(cli_fd);
  3996. return CLI_FAILURE;
  3997. }
  3998. return CLI_SUCCESS;
  3999. }
  4000. static int kill_cores(void *obj, void *arg, int flags)
  4001. {
  4002. int *core_id = arg;
  4003. struct cc_core_instance *core_instance = obj;
  4004. if (!core_id || (core_instance->core_id == *core_id)) {
  4005. ast_cc_failed(core_instance->core_id, "CC transaction canceled administratively\n");
  4006. }
  4007. return 0;
  4008. }
  4009. static char *complete_core_id(const char *word)
  4010. {
  4011. int wordlen = strlen(word);
  4012. struct ao2_iterator core_iter = ao2_iterator_init(cc_core_instances, 0);
  4013. struct cc_core_instance *core_instance;
  4014. for (; (core_instance = ao2_t_iterator_next(&core_iter, "Next core instance"));
  4015. cc_unref(core_instance, "CLI tab completion iteration")) {
  4016. char core_id_str[20];
  4017. snprintf(core_id_str, sizeof(core_id_str), "%d", core_instance->core_id);
  4018. if (!strncmp(word, core_id_str, wordlen)) {
  4019. if (ast_cli_completion_add(ast_strdup(core_id_str))) {
  4020. cc_unref(core_instance, "Found a matching core ID for CLI tab-completion");
  4021. break;
  4022. }
  4023. }
  4024. }
  4025. ao2_iterator_destroy(&core_iter);
  4026. return NULL;
  4027. }
  4028. static char *handle_cc_kill(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
  4029. {
  4030. switch (cmd) {
  4031. case CLI_INIT:
  4032. e->command = "cc cancel [core|all]";
  4033. e->usage =
  4034. "Usage: cc cancel can be used in two ways.\n"
  4035. " 1. 'cc cancel core [core ID]' will cancel the CC transaction with\n"
  4036. " core ID equal to the specified core ID.\n"
  4037. " 2. 'cc cancel all' will cancel all active CC transactions.\n";
  4038. return NULL;
  4039. case CLI_GENERATE:
  4040. if (a->pos == 3 && !strcasecmp(a->argv[2], "core")) {
  4041. return complete_core_id(a->word);
  4042. }
  4043. return NULL;
  4044. }
  4045. if (a->argc == 4) {
  4046. int core_id;
  4047. char *endptr;
  4048. if (strcasecmp(a->argv[2], "core")) {
  4049. return CLI_SHOWUSAGE;
  4050. }
  4051. core_id = strtol(a->argv[3], &endptr, 10);
  4052. if ((errno != 0 && core_id == 0) || (endptr == a->argv[3])) {
  4053. return CLI_SHOWUSAGE;
  4054. }
  4055. ao2_t_callback(cc_core_instances, OBJ_NODATA, kill_cores, &core_id, "CLI Killing Core Id");
  4056. } else if (a->argc == 3) {
  4057. if (strcasecmp(a->argv[2], "all")) {
  4058. return CLI_SHOWUSAGE;
  4059. }
  4060. ao2_t_callback(cc_core_instances, OBJ_NODATA, kill_cores, NULL, "CLI Killing all CC cores");
  4061. } else {
  4062. return CLI_SHOWUSAGE;
  4063. }
  4064. return CLI_SUCCESS;
  4065. }
  4066. static struct ast_cli_entry cc_cli[] = {
  4067. AST_CLI_DEFINE(handle_cc_status, "Reports CC stats"),
  4068. AST_CLI_DEFINE(handle_cc_kill, "Kill a CC transaction"),
  4069. };
  4070. static void cc_shutdown(void)
  4071. {
  4072. ast_devstate_prov_del("ccss");
  4073. ast_cc_agent_unregister(&generic_agent_callbacks);
  4074. ast_cc_monitor_unregister(&generic_monitor_cbs);
  4075. ast_unregister_application(cccancel_app);
  4076. ast_unregister_application(ccreq_app);
  4077. ast_logger_unregister_level(CC_LOGGER_LEVEL_NAME);
  4078. ast_cli_unregister_multiple(cc_cli, ARRAY_LEN(cc_cli));
  4079. if (cc_sched_context) {
  4080. ast_sched_context_destroy(cc_sched_context);
  4081. cc_sched_context = NULL;
  4082. }
  4083. if (cc_core_taskprocessor) {
  4084. cc_core_taskprocessor = ast_taskprocessor_unreference(cc_core_taskprocessor);
  4085. }
  4086. /* Note that core instances must be destroyed prior to the generic_monitors */
  4087. if (cc_core_instances) {
  4088. ao2_t_ref(cc_core_instances, -1, "Unref cc_core_instances container in cc_shutdown");
  4089. cc_core_instances = NULL;
  4090. }
  4091. if (generic_monitors) {
  4092. ao2_t_ref(generic_monitors, -1, "Unref generic_monitor container in cc_shutdown");
  4093. generic_monitors = NULL;
  4094. }
  4095. }
  4096. int ast_cc_init(void)
  4097. {
  4098. int res;
  4099. cc_core_instances = ao2_t_container_alloc_hash(AO2_ALLOC_OPT_LOCK_MUTEX, 0,
  4100. CC_CORE_INSTANCES_BUCKETS,
  4101. cc_core_instance_hash_fn, NULL, cc_core_instance_cmp_fn,
  4102. "Create core instance container");
  4103. if (!cc_core_instances) {
  4104. return -1;
  4105. }
  4106. generic_monitors = ao2_t_container_alloc_hash(AO2_ALLOC_OPT_LOCK_MUTEX, 0,
  4107. CC_CORE_INSTANCES_BUCKETS,
  4108. generic_monitor_instance_list_hash_fn, NULL, generic_monitor_instance_list_cmp_fn,
  4109. "Create generic monitor container");
  4110. if (!generic_monitors) {
  4111. return -1;
  4112. }
  4113. if (!(cc_core_taskprocessor = ast_taskprocessor_get("CCSS_core", TPS_REF_DEFAULT))) {
  4114. return -1;
  4115. }
  4116. if (!(cc_sched_context = ast_sched_context_create())) {
  4117. return -1;
  4118. }
  4119. if (ast_sched_start_thread(cc_sched_context)) {
  4120. return -1;
  4121. }
  4122. res = ast_register_application2(ccreq_app, ccreq_exec, NULL, NULL, NULL);
  4123. res |= ast_register_application2(cccancel_app, cccancel_exec, NULL, NULL, NULL);
  4124. res |= ast_cc_monitor_register(&generic_monitor_cbs);
  4125. res |= ast_cc_agent_register(&generic_agent_callbacks);
  4126. ast_cli_register_multiple(cc_cli, ARRAY_LEN(cc_cli));
  4127. cc_logger_level = ast_logger_register_level(CC_LOGGER_LEVEL_NAME);
  4128. dialed_cc_interface_counter = 1;
  4129. initialize_cc_max_requests();
  4130. /* Read the map and register the device state callback for generic agents */
  4131. initialize_cc_devstate_map();
  4132. res |= ast_devstate_prov_add("ccss", ccss_device_state);
  4133. ast_register_cleanup(cc_shutdown);
  4134. return res;
  4135. }