ccss.c 153 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695
  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. RAII_VAR(struct ast_json *, blob, NULL, ast_json_unref);
  931. RAII_VAR(struct ast_json_payload *, payload, NULL, ao2_cleanup);
  932. RAII_VAR(struct stasis_message *, message, NULL, ao2_cleanup);
  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. if (!(payload = ast_json_payload_create(blob))) {
  945. return -1;
  946. }
  947. if (!(message = stasis_message_create(message_type, payload))) {
  948. return -1;
  949. }
  950. stasis_publish(ast_system_topic(), message);
  951. return 0;
  952. }
  953. static void cc_publish_available(int core_id, const char *callee, const char *service)
  954. {
  955. RAII_VAR(struct ast_json *, extras, NULL, ast_json_unref);
  956. extras = ast_json_pack("{s: s, s: s}",
  957. "callee", callee,
  958. "service", service);
  959. cc_publish(ast_cc_available_type(), core_id, extras);
  960. }
  961. static void cc_publish_offertimerstart(int core_id, const char *caller, unsigned int expires)
  962. {
  963. RAII_VAR(struct ast_json *, extras, NULL, ast_json_unref);
  964. extras = ast_json_pack("{s: s, s: i}",
  965. "caller", caller,
  966. "expires", expires);
  967. cc_publish(ast_cc_offertimerstart_type(), core_id, extras);
  968. }
  969. static void cc_publish_requested(int core_id, const char *caller, const char *callee)
  970. {
  971. RAII_VAR(struct ast_json *, extras, NULL, ast_json_unref);
  972. extras = ast_json_pack("{s: s, s: s}",
  973. "caller", caller,
  974. "callee", callee);
  975. cc_publish(ast_cc_requested_type(), core_id, extras);
  976. }
  977. static void cc_publish_requestacknowledged(int core_id, const char *caller)
  978. {
  979. RAII_VAR(struct ast_json *, extras, NULL, ast_json_unref);
  980. extras = ast_json_pack("{s: s}",
  981. "caller", caller);
  982. cc_publish(ast_cc_requestacknowledged_type(), core_id, extras);
  983. }
  984. static void cc_publish_callerstopmonitoring(int core_id, const char *caller)
  985. {
  986. RAII_VAR(struct ast_json *, extras, NULL, ast_json_unref);
  987. extras = ast_json_pack("{s: s}",
  988. "caller", caller);
  989. cc_publish(ast_cc_callerstopmonitoring_type(), core_id, extras);
  990. }
  991. static void cc_publish_callerstartmonitoring(int core_id, const char *caller)
  992. {
  993. RAII_VAR(struct ast_json *, extras, NULL, ast_json_unref);
  994. extras = ast_json_pack("{s: s}",
  995. "caller", caller);
  996. cc_publish(ast_cc_callerstartmonitoring_type(), core_id, extras);
  997. }
  998. static void cc_publish_callerrecalling(int core_id, const char *caller)
  999. {
  1000. RAII_VAR(struct ast_json *, extras, NULL, ast_json_unref);
  1001. extras = ast_json_pack("{s: s}",
  1002. "caller", caller);
  1003. cc_publish(ast_cc_callerrecalling_type(), core_id, extras);
  1004. }
  1005. static void cc_publish_recallcomplete(int core_id, const char *caller)
  1006. {
  1007. RAII_VAR(struct ast_json *, extras, NULL, ast_json_unref);
  1008. extras = ast_json_pack("{s: s}",
  1009. "caller", caller);
  1010. cc_publish(ast_cc_recallcomplete_type(), core_id, extras);
  1011. }
  1012. static void cc_publish_failure(int core_id, const char *caller, const char *reason)
  1013. {
  1014. RAII_VAR(struct ast_json *, extras, NULL, ast_json_unref);
  1015. extras = ast_json_pack("{s: s, s: s}",
  1016. "caller", caller,
  1017. "reason", reason);
  1018. cc_publish(ast_cc_failure_type(), core_id, extras);
  1019. }
  1020. static void cc_publish_monitorfailed(int core_id, const char *callee)
  1021. {
  1022. RAII_VAR(struct ast_json *, extras, NULL, ast_json_unref);
  1023. extras = ast_json_pack("{s: s}",
  1024. "callee", callee);
  1025. cc_publish(ast_cc_monitorfailed_type(), core_id, extras);
  1026. }
  1027. struct cc_monitor_backend {
  1028. AST_LIST_ENTRY(cc_monitor_backend) next;
  1029. const struct ast_cc_monitor_callbacks *callbacks;
  1030. };
  1031. AST_RWLIST_HEAD_STATIC(cc_monitor_backends, cc_monitor_backend);
  1032. int ast_cc_monitor_register(const struct ast_cc_monitor_callbacks *callbacks)
  1033. {
  1034. struct cc_monitor_backend *backend = ast_calloc(1, sizeof(*backend));
  1035. if (!backend) {
  1036. return -1;
  1037. }
  1038. backend->callbacks = callbacks;
  1039. AST_RWLIST_WRLOCK(&cc_monitor_backends);
  1040. AST_RWLIST_INSERT_TAIL(&cc_monitor_backends, backend, next);
  1041. AST_RWLIST_UNLOCK(&cc_monitor_backends);
  1042. return 0;
  1043. }
  1044. static const struct ast_cc_monitor_callbacks *find_monitor_callbacks(const char * const type)
  1045. {
  1046. struct cc_monitor_backend *backend;
  1047. const struct ast_cc_monitor_callbacks *callbacks = NULL;
  1048. AST_RWLIST_RDLOCK(&cc_monitor_backends);
  1049. AST_RWLIST_TRAVERSE(&cc_monitor_backends, backend, next) {
  1050. if (!strcmp(backend->callbacks->type, type)) {
  1051. ast_log_dynamic_level(cc_logger_level, "Returning monitor backend %s\n", backend->callbacks->type);
  1052. callbacks = backend->callbacks;
  1053. break;
  1054. }
  1055. }
  1056. AST_RWLIST_UNLOCK(&cc_monitor_backends);
  1057. return callbacks;
  1058. }
  1059. void ast_cc_monitor_unregister(const struct ast_cc_monitor_callbacks *callbacks)
  1060. {
  1061. struct cc_monitor_backend *backend;
  1062. AST_RWLIST_WRLOCK(&cc_monitor_backends);
  1063. AST_RWLIST_TRAVERSE_SAFE_BEGIN(&cc_monitor_backends, backend, next) {
  1064. if (backend->callbacks == callbacks) {
  1065. AST_RWLIST_REMOVE_CURRENT(next);
  1066. ast_free(backend);
  1067. break;
  1068. }
  1069. }
  1070. AST_RWLIST_TRAVERSE_SAFE_END;
  1071. AST_RWLIST_UNLOCK(&cc_monitor_backends);
  1072. }
  1073. struct cc_agent_backend {
  1074. AST_LIST_ENTRY(cc_agent_backend) next;
  1075. const struct ast_cc_agent_callbacks *callbacks;
  1076. };
  1077. AST_RWLIST_HEAD_STATIC(cc_agent_backends, cc_agent_backend);
  1078. int ast_cc_agent_register(const struct ast_cc_agent_callbacks *callbacks)
  1079. {
  1080. struct cc_agent_backend *backend = ast_calloc(1, sizeof(*backend));
  1081. if (!backend) {
  1082. return -1;
  1083. }
  1084. backend->callbacks = callbacks;
  1085. AST_RWLIST_WRLOCK(&cc_agent_backends);
  1086. AST_RWLIST_INSERT_TAIL(&cc_agent_backends, backend, next);
  1087. AST_RWLIST_UNLOCK(&cc_agent_backends);
  1088. return 0;
  1089. }
  1090. void ast_cc_agent_unregister(const struct ast_cc_agent_callbacks *callbacks)
  1091. {
  1092. struct cc_agent_backend *backend;
  1093. AST_RWLIST_WRLOCK(&cc_agent_backends);
  1094. AST_RWLIST_TRAVERSE_SAFE_BEGIN(&cc_agent_backends, backend, next) {
  1095. if (backend->callbacks == callbacks) {
  1096. AST_RWLIST_REMOVE_CURRENT(next);
  1097. ast_free(backend);
  1098. break;
  1099. }
  1100. }
  1101. AST_RWLIST_TRAVERSE_SAFE_END;
  1102. AST_RWLIST_UNLOCK(&cc_agent_backends);
  1103. }
  1104. static const struct ast_cc_agent_callbacks *find_agent_callbacks(struct ast_channel *chan)
  1105. {
  1106. struct cc_agent_backend *backend;
  1107. const struct ast_cc_agent_callbacks *callbacks = NULL;
  1108. struct ast_cc_config_params *cc_params;
  1109. char type[32];
  1110. cc_params = ast_channel_get_cc_config_params(chan);
  1111. if (!cc_params) {
  1112. return NULL;
  1113. }
  1114. switch (ast_get_cc_agent_policy(cc_params)) {
  1115. case AST_CC_AGENT_GENERIC:
  1116. ast_copy_string(type, "generic", sizeof(type));
  1117. break;
  1118. case AST_CC_AGENT_NATIVE:
  1119. ast_channel_get_cc_agent_type(chan, type, sizeof(type));
  1120. break;
  1121. default:
  1122. ast_log_dynamic_level(cc_logger_level, "Not returning agent callbacks since this channel is configured not to have a CC agent\n");
  1123. return NULL;
  1124. }
  1125. AST_RWLIST_RDLOCK(&cc_agent_backends);
  1126. AST_RWLIST_TRAVERSE(&cc_agent_backends, backend, next) {
  1127. if (!strcmp(backend->callbacks->type, type)) {
  1128. ast_log_dynamic_level(cc_logger_level, "Returning agent backend %s\n", backend->callbacks->type);
  1129. callbacks = backend->callbacks;
  1130. break;
  1131. }
  1132. }
  1133. AST_RWLIST_UNLOCK(&cc_agent_backends);
  1134. return callbacks;
  1135. }
  1136. /*!
  1137. * \internal
  1138. * \brief Determine if the given device state is considered available by generic CCSS.
  1139. * \since 1.8
  1140. *
  1141. * \param state Device state to test.
  1142. *
  1143. * \return TRUE if the given device state is considered available by generic CCSS.
  1144. */
  1145. static int cc_generic_is_device_available(enum ast_device_state state)
  1146. {
  1147. return state == AST_DEVICE_NOT_INUSE || state == AST_DEVICE_UNKNOWN;
  1148. }
  1149. static int cc_generic_monitor_request_cc(struct ast_cc_monitor *monitor, int *available_timer_id);
  1150. static int cc_generic_monitor_suspend(struct ast_cc_monitor *monitor);
  1151. static int cc_generic_monitor_unsuspend(struct ast_cc_monitor *monitor);
  1152. static int cc_generic_monitor_cancel_available_timer(struct ast_cc_monitor *monitor, int *sched_id);
  1153. static void cc_generic_monitor_destructor(void *private_data);
  1154. static struct ast_cc_monitor_callbacks generic_monitor_cbs = {
  1155. .type = "generic",
  1156. .request_cc = cc_generic_monitor_request_cc,
  1157. .suspend = cc_generic_monitor_suspend,
  1158. .unsuspend = cc_generic_monitor_unsuspend,
  1159. .cancel_available_timer = cc_generic_monitor_cancel_available_timer,
  1160. .destructor = cc_generic_monitor_destructor,
  1161. };
  1162. struct ao2_container *generic_monitors;
  1163. struct generic_monitor_instance {
  1164. int core_id;
  1165. int is_suspended;
  1166. int monitoring;
  1167. AST_LIST_ENTRY(generic_monitor_instance) next;
  1168. };
  1169. struct generic_monitor_instance_list {
  1170. const char *device_name;
  1171. enum ast_device_state current_state;
  1172. /* If there are multiple instances monitoring the
  1173. * same device and one should fail, we need to know
  1174. * whether to signal that the device can be recalled.
  1175. * The problem is that the device state is not enough
  1176. * to check. If a caller has requested CCNR, then the
  1177. * fact that the device is available does not indicate
  1178. * that the device is ready to be recalled. Instead, as
  1179. * soon as one instance of the monitor becomes available
  1180. * for a recall, we mark the entire list as being fit
  1181. * for recall. If a CCNR request comes in, then we will
  1182. * have to mark the list as unfit for recall since this
  1183. * is a clear indicator that the person at the monitored
  1184. * device has gone away and is actuall not fit to be
  1185. * recalled
  1186. */
  1187. int fit_for_recall;
  1188. struct stasis_subscription *sub;
  1189. AST_LIST_HEAD_NOLOCK(, generic_monitor_instance) list;
  1190. };
  1191. /*!
  1192. * \brief private data for generic device monitor
  1193. */
  1194. struct generic_monitor_pvt {
  1195. /*!
  1196. * We need the device name during destruction so we
  1197. * can find the appropriate item to destroy.
  1198. */
  1199. const char *device_name;
  1200. /*!
  1201. * We need the core ID for similar reasons. Once we
  1202. * find the appropriate item in our ao2_container, we
  1203. * need to remove the appropriate cc_monitor from the
  1204. * list of monitors.
  1205. */
  1206. int core_id;
  1207. };
  1208. static int generic_monitor_hash_fn(const void *obj, const int flags)
  1209. {
  1210. const struct generic_monitor_instance_list *generic_list = obj;
  1211. return ast_str_hash(generic_list->device_name);
  1212. }
  1213. static int generic_monitor_cmp_fn(void *obj, void *arg, int flags)
  1214. {
  1215. const struct generic_monitor_instance_list *generic_list1 = obj;
  1216. const struct generic_monitor_instance_list *generic_list2 = arg;
  1217. return !strcmp(generic_list1->device_name, generic_list2->device_name) ? CMP_MATCH | CMP_STOP : 0;
  1218. }
  1219. static struct generic_monitor_instance_list *find_generic_monitor_instance_list(const char * const device_name)
  1220. {
  1221. struct generic_monitor_instance_list finder = {0};
  1222. char *uppertech = ast_strdupa(device_name);
  1223. ast_tech_to_upper(uppertech);
  1224. finder.device_name = uppertech;
  1225. return ao2_t_find(generic_monitors, &finder, OBJ_POINTER, "Finding generic monitor instance list");
  1226. }
  1227. static void generic_monitor_instance_list_destructor(void *obj)
  1228. {
  1229. struct generic_monitor_instance_list *generic_list = obj;
  1230. struct generic_monitor_instance *generic_instance;
  1231. generic_list->sub = stasis_unsubscribe(generic_list->sub);
  1232. while ((generic_instance = AST_LIST_REMOVE_HEAD(&generic_list->list, next))) {
  1233. ast_free(generic_instance);
  1234. }
  1235. ast_free((char *)generic_list->device_name);
  1236. }
  1237. static void generic_monitor_devstate_cb(void *userdata, struct stasis_subscription *sub, struct stasis_message *msg);
  1238. static struct generic_monitor_instance_list *create_new_generic_list(struct ast_cc_monitor *monitor)
  1239. {
  1240. struct generic_monitor_instance_list *generic_list = ao2_t_alloc(sizeof(*generic_list),
  1241. generic_monitor_instance_list_destructor, "allocate generic monitor instance list");
  1242. char * device_name;
  1243. struct stasis_topic *device_specific_topic;
  1244. if (!generic_list) {
  1245. return NULL;
  1246. }
  1247. if (!(device_name = ast_strdup(monitor->interface->device_name))) {
  1248. cc_unref(generic_list, "Failed to strdup the monitor's device name");
  1249. return NULL;
  1250. }
  1251. ast_tech_to_upper(device_name);
  1252. generic_list->device_name = device_name;
  1253. device_specific_topic = ast_device_state_topic(device_name);
  1254. if (!device_specific_topic) {
  1255. return NULL;
  1256. }
  1257. if (!(generic_list->sub = stasis_subscribe(device_specific_topic, generic_monitor_devstate_cb, NULL))) {
  1258. cc_unref(generic_list, "Failed to subscribe to device state");
  1259. return NULL;
  1260. }
  1261. generic_list->current_state = ast_device_state(monitor->interface->device_name);
  1262. ao2_t_link(generic_monitors, generic_list, "linking new generic monitor instance list");
  1263. return generic_list;
  1264. }
  1265. static int generic_monitor_devstate_tp_cb(void *data)
  1266. {
  1267. RAII_VAR(struct ast_device_state_message *, dev_state, data, ao2_cleanup);
  1268. enum ast_device_state new_state = dev_state->state;
  1269. enum ast_device_state previous_state;
  1270. struct generic_monitor_instance_list *generic_list;
  1271. struct generic_monitor_instance *generic_instance;
  1272. if (!(generic_list = find_generic_monitor_instance_list(dev_state->device))) {
  1273. /* The most likely cause for this is that we destroyed the monitor in the
  1274. * time between subscribing to its device state and the time this executes.
  1275. * Not really a big deal.
  1276. */
  1277. return 0;
  1278. }
  1279. if (generic_list->current_state == new_state) {
  1280. /* The device state hasn't actually changed, so we don't really care */
  1281. cc_unref(generic_list, "Kill reference of generic list in devstate taskprocessor callback");
  1282. return 0;
  1283. }
  1284. previous_state = generic_list->current_state;
  1285. generic_list->current_state = new_state;
  1286. if (cc_generic_is_device_available(new_state) &&
  1287. (previous_state == AST_DEVICE_INUSE || previous_state == AST_DEVICE_UNAVAILABLE ||
  1288. previous_state == AST_DEVICE_BUSY)) {
  1289. AST_LIST_TRAVERSE(&generic_list->list, generic_instance, next) {
  1290. if (!generic_instance->is_suspended && generic_instance->monitoring) {
  1291. generic_instance->monitoring = 0;
  1292. generic_list->fit_for_recall = 1;
  1293. ast_cc_monitor_callee_available(generic_instance->core_id, "Generic monitored party has become available");
  1294. break;
  1295. }
  1296. }
  1297. }
  1298. cc_unref(generic_list, "Kill reference of generic list in devstate taskprocessor callback");
  1299. return 0;
  1300. }
  1301. static void generic_monitor_devstate_cb(void *userdata, struct stasis_subscription *sub, struct stasis_message *msg)
  1302. {
  1303. /* Wow, it's cool that we've picked up on a state change, but we really want
  1304. * the actual work to be done in the core's taskprocessor execution thread
  1305. * so that all monitor operations can be serialized. Locks?! We don't need
  1306. * no steenkin' locks!
  1307. */
  1308. struct ast_device_state_message *dev_state;
  1309. if (ast_device_state_message_type() != stasis_message_type(msg)) {
  1310. return;
  1311. }
  1312. dev_state = stasis_message_data(msg);
  1313. if (dev_state->eid) {
  1314. /* ignore non-aggregate states */
  1315. return;
  1316. }
  1317. ao2_t_ref(dev_state, +1, "Bumping dev_state ref for cc_core_taskprocessor");
  1318. if (ast_taskprocessor_push(cc_core_taskprocessor, generic_monitor_devstate_tp_cb, dev_state)) {
  1319. ao2_cleanup(dev_state);
  1320. return;
  1321. }
  1322. }
  1323. int ast_cc_available_timer_expire(const void *data)
  1324. {
  1325. struct ast_cc_monitor *monitor = (struct ast_cc_monitor *) data;
  1326. int res;
  1327. monitor->available_timer_id = -1;
  1328. res = ast_cc_monitor_failed(monitor->core_id, monitor->interface->device_name, "Available timer expired for monitor");
  1329. cc_unref(monitor, "Unref reference from scheduler\n");
  1330. return res;
  1331. }
  1332. static int cc_generic_monitor_request_cc(struct ast_cc_monitor *monitor, int *available_timer_id)
  1333. {
  1334. struct generic_monitor_instance_list *generic_list;
  1335. struct generic_monitor_instance *generic_instance;
  1336. struct generic_monitor_pvt *gen_mon_pvt;
  1337. enum ast_cc_service_type service = monitor->service_offered;
  1338. int when;
  1339. /* First things first. Native channel drivers will have their private data allocated
  1340. * at the time that they tell the core that they can offer CC. Generic is quite a bit
  1341. * different, and we wait until this point to allocate our private data.
  1342. */
  1343. if (!(gen_mon_pvt = ast_calloc(1, sizeof(*gen_mon_pvt)))) {
  1344. return -1;
  1345. }
  1346. if (!(gen_mon_pvt->device_name = ast_strdup(monitor->interface->device_name))) {
  1347. ast_free(gen_mon_pvt);
  1348. return -1;
  1349. }
  1350. gen_mon_pvt->core_id = monitor->core_id;
  1351. monitor->private_data = gen_mon_pvt;
  1352. if (!(generic_list = find_generic_monitor_instance_list(monitor->interface->device_name))) {
  1353. if (!(generic_list = create_new_generic_list(monitor))) {
  1354. return -1;
  1355. }
  1356. }
  1357. if (!(generic_instance = ast_calloc(1, sizeof(*generic_instance)))) {
  1358. /* The generic monitor destructor will take care of the appropriate
  1359. * deallocations
  1360. */
  1361. cc_unref(generic_list, "Generic monitor instance failed to allocate");
  1362. return -1;
  1363. }
  1364. generic_instance->core_id = monitor->core_id;
  1365. generic_instance->monitoring = 1;
  1366. AST_LIST_INSERT_TAIL(&generic_list->list, generic_instance, next);
  1367. when = service == AST_CC_CCBS ? ast_get_ccbs_available_timer(monitor->interface->config_params) :
  1368. ast_get_ccnr_available_timer(monitor->interface->config_params);
  1369. *available_timer_id = ast_sched_add(cc_sched_context, when * 1000,
  1370. ast_cc_available_timer_expire, cc_ref(monitor, "Give the scheduler a monitor reference"));
  1371. if (*available_timer_id == -1) {
  1372. cc_unref(monitor, "Failed to schedule available timer. (monitor)");
  1373. cc_unref(generic_list, "Failed to schedule available timer. (generic_list)");
  1374. return -1;
  1375. }
  1376. /* If the new instance was created as CCNR, then that means this device is not currently
  1377. * fit for recall even if it previously was.
  1378. */
  1379. if (service == AST_CC_CCNR || service == AST_CC_CCNL) {
  1380. generic_list->fit_for_recall = 0;
  1381. }
  1382. ast_cc_monitor_request_acked(monitor->core_id, "Generic monitor for %s subscribed to device state.",
  1383. monitor->interface->device_name);
  1384. cc_unref(generic_list, "Finished with monitor instance reference in request cc callback");
  1385. return 0;
  1386. }
  1387. static int cc_generic_monitor_suspend(struct ast_cc_monitor *monitor)
  1388. {
  1389. struct generic_monitor_instance_list *generic_list;
  1390. struct generic_monitor_instance *generic_instance;
  1391. enum ast_device_state state = ast_device_state(monitor->interface->device_name);
  1392. if (!(generic_list = find_generic_monitor_instance_list(monitor->interface->device_name))) {
  1393. return -1;
  1394. }
  1395. /* First we need to mark this particular monitor as being suspended. */
  1396. AST_LIST_TRAVERSE(&generic_list->list, generic_instance, next) {
  1397. if (generic_instance->core_id == monitor->core_id) {
  1398. generic_instance->is_suspended = 1;
  1399. break;
  1400. }
  1401. }
  1402. /* If the device being suspended is currently in use, then we don't need to
  1403. * take any further actions
  1404. */
  1405. if (!cc_generic_is_device_available(state)) {
  1406. cc_unref(generic_list, "Device is in use. Nothing to do. Unref generic list.");
  1407. return 0;
  1408. }
  1409. /* If the device is not in use, though, then it may be possible to report the
  1410. * device's availability using a different monitor which is monitoring the
  1411. * same device
  1412. */
  1413. AST_LIST_TRAVERSE(&generic_list->list, generic_instance, next) {
  1414. if (!generic_instance->is_suspended) {
  1415. ast_cc_monitor_callee_available(generic_instance->core_id, "Generic monitored party has become available");
  1416. break;
  1417. }
  1418. }
  1419. cc_unref(generic_list, "Done with generic list in suspend callback");
  1420. return 0;
  1421. }
  1422. static int cc_generic_monitor_unsuspend(struct ast_cc_monitor *monitor)
  1423. {
  1424. struct generic_monitor_instance *generic_instance;
  1425. struct generic_monitor_instance_list *generic_list = find_generic_monitor_instance_list(monitor->interface->device_name);
  1426. enum ast_device_state state = ast_device_state(monitor->interface->device_name);
  1427. if (!generic_list) {
  1428. return -1;
  1429. }
  1430. /* If the device is currently available, we can immediately announce
  1431. * its availability
  1432. */
  1433. if (cc_generic_is_device_available(state)) {
  1434. ast_cc_monitor_callee_available(monitor->core_id, "Generic monitored party has become available");
  1435. }
  1436. /* In addition, we need to mark this generic_monitor_instance as not being suspended anymore */
  1437. AST_LIST_TRAVERSE(&generic_list->list, generic_instance, next) {
  1438. if (generic_instance->core_id == monitor->core_id) {
  1439. generic_instance->is_suspended = 0;
  1440. generic_instance->monitoring = 1;
  1441. break;
  1442. }
  1443. }
  1444. cc_unref(generic_list, "Done with generic list in cc_generic_monitor_unsuspend");
  1445. return 0;
  1446. }
  1447. static int cc_generic_monitor_cancel_available_timer(struct ast_cc_monitor *monitor, int *sched_id)
  1448. {
  1449. ast_assert(sched_id != NULL);
  1450. if (*sched_id == -1) {
  1451. return 0;
  1452. }
  1453. ast_log_dynamic_level(cc_logger_level, "Core %d: Canceling generic monitor available timer for monitor %s\n",
  1454. monitor->core_id, monitor->interface->device_name);
  1455. if (!ast_sched_del(cc_sched_context, *sched_id)) {
  1456. cc_unref(monitor, "Remove scheduler's reference to the monitor");
  1457. }
  1458. *sched_id = -1;
  1459. return 0;
  1460. }
  1461. static void cc_generic_monitor_destructor(void *private_data)
  1462. {
  1463. struct generic_monitor_pvt *gen_mon_pvt = private_data;
  1464. struct generic_monitor_instance_list *generic_list;
  1465. struct generic_monitor_instance *generic_instance;
  1466. if (!private_data) {
  1467. /* If the private data is NULL, that means that the monitor hasn't even
  1468. * been created yet, but that the destructor was called. While this sort
  1469. * of behavior is useful for native monitors, with a generic one, there is
  1470. * nothing in particular to do.
  1471. */
  1472. return;
  1473. }
  1474. ast_log_dynamic_level(cc_logger_level, "Core %d: Destroying generic monitor %s\n",
  1475. gen_mon_pvt->core_id, gen_mon_pvt->device_name);
  1476. if (!(generic_list = find_generic_monitor_instance_list(gen_mon_pvt->device_name))) {
  1477. /* If there's no generic list, that means that the monitor is being destroyed
  1478. * before we actually got to request CC. Not a biggie. Same in the situation
  1479. * below if the list traversal should complete without finding an entry.
  1480. */
  1481. ast_free((char *)gen_mon_pvt->device_name);
  1482. ast_free(gen_mon_pvt);
  1483. return;
  1484. }
  1485. AST_LIST_TRAVERSE_SAFE_BEGIN(&generic_list->list, generic_instance, next) {
  1486. if (generic_instance->core_id == gen_mon_pvt->core_id) {
  1487. AST_LIST_REMOVE_CURRENT(next);
  1488. ast_free(generic_instance);
  1489. break;
  1490. }
  1491. }
  1492. AST_LIST_TRAVERSE_SAFE_END;
  1493. if (AST_LIST_EMPTY(&generic_list->list)) {
  1494. /* No more monitors with this device name exist. Time to unlink this
  1495. * list from the container
  1496. */
  1497. ao2_t_unlink(generic_monitors, generic_list, "Generic list is empty. Unlink it from the container");
  1498. } else {
  1499. /* There are still instances for this particular device. The situation
  1500. * may be that we were attempting a CC recall and a failure occurred, perhaps
  1501. * on the agent side. If a failure happens here and the device being monitored
  1502. * is available, then we need to signal on the first unsuspended instance that
  1503. * the device is available for recall.
  1504. */
  1505. /* First things first. We don't even want to consider this action if
  1506. * the device in question isn't available right now.
  1507. */
  1508. if (generic_list->fit_for_recall
  1509. && cc_generic_is_device_available(generic_list->current_state)) {
  1510. AST_LIST_TRAVERSE(&generic_list->list, generic_instance, next) {
  1511. if (!generic_instance->is_suspended && generic_instance->monitoring) {
  1512. ast_cc_monitor_callee_available(generic_instance->core_id, "Signaling generic monitor "
  1513. "availability due to other instance's failure.");
  1514. break;
  1515. }
  1516. }
  1517. }
  1518. }
  1519. cc_unref(generic_list, "Done with generic list in generic monitor destructor");
  1520. ast_free((char *)gen_mon_pvt->device_name);
  1521. ast_free(gen_mon_pvt);
  1522. }
  1523. static void cc_interface_destroy(void *data)
  1524. {
  1525. struct ast_cc_interface *interface = data;
  1526. ast_log_dynamic_level(cc_logger_level, "Destroying cc interface %s\n", interface->device_name);
  1527. ast_cc_config_params_destroy(interface->config_params);
  1528. }
  1529. /*!
  1530. * \brief Data regarding an extension monitor's child's dialstrings
  1531. *
  1532. * \details
  1533. * In developing CCSS, we had most aspects of its operation finished,
  1534. * but there was one looming problem that we had failed to get right.
  1535. * In our design document, we stated that when a CC recall occurs, all
  1536. * endpoints that had been dialed originally would be called back.
  1537. * Unfortunately, our implementation only allowed for devices which had
  1538. * active monitors to inhabit the CC_INTERFACES channel variable, thus
  1539. * making the automated recall only call monitored devices.
  1540. *
  1541. * Devices that were not CC-capable, or devices which failed CC at some
  1542. * point during the process would not make it into the CC_INTERFACES
  1543. * channel variable. This struct is meant as a remedy for the problem.
  1544. */
  1545. struct extension_child_dialstring {
  1546. /*!
  1547. * \brief the original dialstring used to call a particular device
  1548. *
  1549. * \details
  1550. * When someone dials a particular endpoint, the dialstring used in
  1551. * the dialplan is copied into this buffer. What's important here is
  1552. * that this is the ORIGINAL dialstring, not the dialstring saved on
  1553. * a device monitor. The dialstring on a device monitor is what should
  1554. * be used when recalling that device. The two dialstrings may not be
  1555. * the same.
  1556. *
  1557. * By keeping a copy of the original dialstring used, we can fall back
  1558. * to using it if the device either does not ever offer CC or if the
  1559. * device at some point fails for some reason, such as a timer expiration.
  1560. */
  1561. char original_dialstring[AST_CHANNEL_NAME];
  1562. /*!
  1563. * \brief The name of the device being dialed
  1564. *
  1565. * \details
  1566. * This serves mainly as a key when searching for a particular dialstring.
  1567. * For instance, let's say that we have called device SIP/400\@somepeer. This
  1568. * device offers call completion, but then due to some unforeseen circumstance,
  1569. * this device backs out and makes CC unavailable. When that happens, we need
  1570. * to find the dialstring that corresponds to that device, and we use the
  1571. * stored device name as a way to find it.
  1572. *
  1573. * \note There is one particular case where the device name stored here
  1574. * will be empty. This is the case where we fail to request a channel, but we
  1575. * still can make use of generic call completion. In such a case, since we never
  1576. * were able to request the channel, we can't find what its device name is. In
  1577. * this case, however, it is not important because the dialstring is guaranteed
  1578. * to be the same both here and in the device monitor.
  1579. */
  1580. char device_name[AST_CHANNEL_NAME];
  1581. /*!
  1582. * \brief Is this structure valid for use in CC_INTERFACES?
  1583. *
  1584. * \details
  1585. * When this structure is first created, all information stored here is planned
  1586. * to be used, so we set the is_valid flag. However, if a device offers call
  1587. * completion, it will potentially have its own dialstring to use for the recall,
  1588. * so we find this structure and clear the is_valid flag. By clearing the is_valid
  1589. * flag, we won't try to populate the CC_INTERFACES variable with the dialstring
  1590. * stored in this struct. Now, if later, the device which had offered CC should fail,
  1591. * perhaps due to a timer expiration, then we need to re-set the is_valid flag. This
  1592. * way, we still will end up placing a call to the device again, and the dialstring
  1593. * used will be the same as was originally used.
  1594. */
  1595. int is_valid;
  1596. AST_LIST_ENTRY(extension_child_dialstring) next;
  1597. };
  1598. /*!
  1599. * \brief Private data for an extension monitor
  1600. */
  1601. struct extension_monitor_pvt {
  1602. AST_LIST_HEAD_NOLOCK(, extension_child_dialstring) child_dialstrings;
  1603. };
  1604. static void cc_extension_monitor_destructor(void *private_data)
  1605. {
  1606. struct extension_monitor_pvt *extension_pvt = private_data;
  1607. struct extension_child_dialstring *child_dialstring;
  1608. /* This shouldn't be possible, but I'm paranoid */
  1609. if (!extension_pvt) {
  1610. return;
  1611. }
  1612. while ((child_dialstring = AST_LIST_REMOVE_HEAD(&extension_pvt->child_dialstrings, next))) {
  1613. ast_free(child_dialstring);
  1614. }
  1615. ast_free(extension_pvt);
  1616. }
  1617. static void cc_monitor_destroy(void *data)
  1618. {
  1619. struct ast_cc_monitor *monitor = data;
  1620. /* During the monitor creation process, it is possible for this
  1621. * function to be called prior to when callbacks are assigned
  1622. * to the monitor. Also, extension monitors do not have callbacks
  1623. * assigned to them, so we wouldn't want to segfault when we try
  1624. * to destroy one of them.
  1625. */
  1626. ast_log_dynamic_level(cc_logger_level, "Core %d: Calling destructor for monitor %s\n",
  1627. monitor->core_id, monitor->interface->device_name);
  1628. if (monitor->interface->monitor_class == AST_CC_EXTENSION_MONITOR) {
  1629. cc_extension_monitor_destructor(monitor->private_data);
  1630. }
  1631. if (monitor->callbacks) {
  1632. monitor->callbacks->destructor(monitor->private_data);
  1633. }
  1634. cc_unref(monitor->interface, "Unreffing tree's reference to interface");
  1635. ast_free(monitor->dialstring);
  1636. }
  1637. static void cc_interface_tree_destroy(void *data)
  1638. {
  1639. struct cc_monitor_tree *cc_interface_tree = data;
  1640. struct ast_cc_monitor *monitor;
  1641. while ((monitor = AST_LIST_REMOVE_HEAD(cc_interface_tree, next))) {
  1642. if (monitor->callbacks) {
  1643. monitor->callbacks->cancel_available_timer(monitor, &monitor->available_timer_id);
  1644. }
  1645. cc_unref(monitor, "Destroying all monitors");
  1646. }
  1647. AST_LIST_HEAD_DESTROY(cc_interface_tree);
  1648. }
  1649. /*!
  1650. * This counter is used for assigning unique ids
  1651. * to CC-enabled dialed interfaces.
  1652. */
  1653. static int dialed_cc_interface_counter;
  1654. /*!
  1655. * \internal
  1656. * \brief data stored in CC datastore
  1657. *
  1658. * The datastore creates a list of interfaces that were
  1659. * dialed, including both extensions and devices. In addition
  1660. * to the intrinsic data of the tree, some extra information
  1661. * is needed for use by app_dial.
  1662. */
  1663. struct dialed_cc_interfaces {
  1664. /*!
  1665. * This value serves a dual-purpose. When dial starts, if the
  1666. * dialed_cc_interfaces datastore currently exists on the calling
  1667. * channel, then the dial_parent_id will serve as a means of
  1668. * letting the new extension cc_monitor we create know
  1669. * who his parent is. This value will be the extension
  1670. * cc_monitor that dialed the local channel that resulted
  1671. * in the new Dial app being called.
  1672. *
  1673. * In addition, once an extension cc_monitor is created,
  1674. * the dial_parent_id will be changed to the id of that newly
  1675. * created interface. This way, device interfaces created from
  1676. * receiving AST_CONTROL_CC frames can use this field to determine
  1677. * who their parent extension interface should be.
  1678. */
  1679. unsigned int dial_parent_id;
  1680. /*!
  1681. * Identifier for the potential CC request that may be made
  1682. * based on this call. Even though an instance of the core may
  1683. * not be made (since the caller may not request CC), we allocate
  1684. * a new core_id at the beginning of the call so that recipient
  1685. * channel drivers can have the information handy just in case
  1686. * the caller does end up requesting CC.
  1687. */
  1688. int core_id;
  1689. /*!
  1690. * When a new Dial application is started, and the datastore
  1691. * already exists on the channel, we can determine if we
  1692. * should be adding any new interface information to tree.
  1693. */
  1694. char ignore;
  1695. /*!
  1696. * When it comes time to offer CC to the caller, we only want to offer
  1697. * it to the original incoming channel. For nested Dials and outbound
  1698. * channels, it is incorrect to attempt such a thing. This flag indicates
  1699. * if the channel to which this datastore is attached may be legally
  1700. * offered CC when the call is finished.
  1701. */
  1702. char is_original_caller;
  1703. /*!
  1704. * Reference-counted "tree" of interfaces.
  1705. */
  1706. struct cc_monitor_tree *interface_tree;
  1707. };
  1708. /*!
  1709. * \internal
  1710. * \brief Destructor function for cc_interfaces datastore
  1711. *
  1712. * This function will free the actual datastore and drop
  1713. * the refcount for the monitor tree by one. In cases
  1714. * where CC can actually be used, this unref will not
  1715. * result in the destruction of the monitor tree, because
  1716. * the CC core will still have a reference.
  1717. *
  1718. * \param data The dialed_cc_interfaces struct to destroy
  1719. */
  1720. static void dialed_cc_interfaces_destroy(void *data)
  1721. {
  1722. struct dialed_cc_interfaces *cc_interfaces = data;
  1723. cc_unref(cc_interfaces->interface_tree, "Unref dial's ref to monitor tree");
  1724. ast_free(cc_interfaces);
  1725. }
  1726. /*!
  1727. * \internal
  1728. * \brief Duplicate callback for cc_interfaces datastore
  1729. *
  1730. * Integers are copied by value, but the monitor tree
  1731. * is done via a shallow copy and a bump of the refcount.
  1732. * This way, sub-Dials will be appending interfaces onto
  1733. * the same list as this call to Dial.
  1734. *
  1735. * \param data The old dialed_cc_interfaces we want to copy
  1736. * \retval NULL Could not allocate memory for new dialed_cc_interfaces
  1737. * \retval non-NULL The new copy of the dialed_cc_interfaces
  1738. */
  1739. static void *dialed_cc_interfaces_duplicate(void *data)
  1740. {
  1741. struct dialed_cc_interfaces *old_cc_interfaces = data;
  1742. struct dialed_cc_interfaces *new_cc_interfaces = ast_calloc(1, sizeof(*new_cc_interfaces));
  1743. if (!new_cc_interfaces) {
  1744. return NULL;
  1745. }
  1746. new_cc_interfaces->ignore = old_cc_interfaces->ignore;
  1747. new_cc_interfaces->dial_parent_id = old_cc_interfaces->dial_parent_id;
  1748. new_cc_interfaces->is_original_caller = 0;
  1749. cc_ref(old_cc_interfaces->interface_tree, "New ref due to duplication of monitor tree");
  1750. new_cc_interfaces->core_id = old_cc_interfaces->core_id;
  1751. new_cc_interfaces->interface_tree = old_cc_interfaces->interface_tree;
  1752. return new_cc_interfaces;
  1753. }
  1754. /*!
  1755. * \internal
  1756. * \brief information regarding the dialed_cc_interfaces datastore
  1757. *
  1758. * The dialed_cc_interfaces datastore is responsible for keeping track
  1759. * of what CC-enabled interfaces have been dialed by the caller. For
  1760. * more information regarding the actual structure of the tree, see
  1761. * the documentation provided in include/asterisk/ccss.h
  1762. */
  1763. static const struct ast_datastore_info dialed_cc_interfaces_info = {
  1764. .type = "Dial CC Interfaces",
  1765. .duplicate = dialed_cc_interfaces_duplicate,
  1766. .destroy = dialed_cc_interfaces_destroy,
  1767. };
  1768. static struct extension_monitor_pvt *extension_monitor_pvt_init(void)
  1769. {
  1770. struct extension_monitor_pvt *ext_pvt = ast_calloc(1, sizeof(*ext_pvt));
  1771. if (!ext_pvt) {
  1772. return NULL;
  1773. }
  1774. AST_LIST_HEAD_INIT_NOLOCK(&ext_pvt->child_dialstrings);
  1775. return ext_pvt;
  1776. }
  1777. void ast_cc_extension_monitor_add_dialstring(struct ast_channel *incoming, const char * const dialstring, const char * const device_name)
  1778. {
  1779. struct ast_datastore *cc_datastore;
  1780. struct dialed_cc_interfaces *cc_interfaces;
  1781. struct ast_cc_monitor *monitor;
  1782. struct extension_monitor_pvt *extension_pvt;
  1783. struct extension_child_dialstring *child_dialstring;
  1784. struct cc_monitor_tree *interface_tree;
  1785. int id;
  1786. ast_channel_lock(incoming);
  1787. if (!(cc_datastore = ast_channel_datastore_find(incoming, &dialed_cc_interfaces_info, NULL))) {
  1788. ast_channel_unlock(incoming);
  1789. return;
  1790. }
  1791. cc_interfaces = cc_datastore->data;
  1792. interface_tree = cc_interfaces->interface_tree;
  1793. id = cc_interfaces->dial_parent_id;
  1794. ast_channel_unlock(incoming);
  1795. AST_LIST_LOCK(interface_tree);
  1796. AST_LIST_TRAVERSE(interface_tree, monitor, next) {
  1797. if (monitor->id == id) {
  1798. break;
  1799. }
  1800. }
  1801. if (!monitor) {
  1802. AST_LIST_UNLOCK(interface_tree);
  1803. return;
  1804. }
  1805. extension_pvt = monitor->private_data;
  1806. if (!(child_dialstring = ast_calloc(1, sizeof(*child_dialstring)))) {
  1807. AST_LIST_UNLOCK(interface_tree);
  1808. return;
  1809. }
  1810. ast_copy_string(child_dialstring->original_dialstring, dialstring, sizeof(child_dialstring->original_dialstring));
  1811. ast_copy_string(child_dialstring->device_name, device_name, sizeof(child_dialstring->device_name));
  1812. child_dialstring->is_valid = 1;
  1813. AST_LIST_INSERT_TAIL(&extension_pvt->child_dialstrings, child_dialstring, next);
  1814. AST_LIST_UNLOCK(interface_tree);
  1815. }
  1816. 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)
  1817. {
  1818. struct ast_cc_monitor *monitor_iter;
  1819. struct extension_monitor_pvt *extension_pvt;
  1820. struct extension_child_dialstring *child_dialstring;
  1821. AST_LIST_TRAVERSE(core_instance->monitors, monitor_iter, next) {
  1822. if (monitor_iter->id == parent_id) {
  1823. break;
  1824. }
  1825. }
  1826. if (!monitor_iter) {
  1827. return;
  1828. }
  1829. extension_pvt = monitor_iter->private_data;
  1830. AST_LIST_TRAVERSE(&extension_pvt->child_dialstrings, child_dialstring, next) {
  1831. if (!strcmp(child_dialstring->device_name, device_name)) {
  1832. child_dialstring->is_valid = is_valid;
  1833. break;
  1834. }
  1835. }
  1836. }
  1837. /*!
  1838. * \internal
  1839. * \brief Allocate and initialize an "extension" interface for CC purposes
  1840. *
  1841. * When app_dial starts, this function is called in order to set up the
  1842. * information about the extension in which this Dial is occurring. Any
  1843. * devices dialed will have this particular cc_monitor as a parent.
  1844. *
  1845. * \param exten Extension from which Dial is occurring
  1846. * \param context Context to which exten belongs
  1847. * \param parent_id What should we set the parent_id of this interface to?
  1848. * \retval NULL Memory allocation failure
  1849. * \retval non-NULL The newly-created cc_monitor for the extension
  1850. */
  1851. static struct ast_cc_monitor *cc_extension_monitor_init(const char * const exten, const char * const context, const unsigned int parent_id)
  1852. {
  1853. struct ast_str *str = ast_str_alloca(2 * AST_MAX_EXTENSION);
  1854. struct ast_cc_interface *cc_interface;
  1855. struct ast_cc_monitor *monitor;
  1856. ast_str_set(&str, 0, "%s@%s", exten, context);
  1857. if (!(cc_interface = ao2_t_alloc(sizeof(*cc_interface) + ast_str_strlen(str), cc_interface_destroy,
  1858. "Allocating new ast_cc_interface"))) {
  1859. return NULL;
  1860. }
  1861. if (!(monitor = ao2_t_alloc(sizeof(*monitor), cc_monitor_destroy, "Allocating new ast_cc_monitor"))) {
  1862. cc_unref(cc_interface, "failed to allocate the monitor, so unref the interface");
  1863. return NULL;
  1864. }
  1865. if (!(monitor->private_data = extension_monitor_pvt_init())) {
  1866. cc_unref(monitor, "Failed to initialize extension monitor private data. uref monitor");
  1867. cc_unref(cc_interface, "Failed to initialize extension monitor private data. unref cc_interface");
  1868. }
  1869. monitor->id = ast_atomic_fetchadd_int(&dialed_cc_interface_counter, +1);
  1870. monitor->parent_id = parent_id;
  1871. cc_interface->monitor_type = "extension";
  1872. cc_interface->monitor_class = AST_CC_EXTENSION_MONITOR;
  1873. strcpy(cc_interface->device_name, ast_str_buffer(str));
  1874. monitor->interface = cc_interface;
  1875. 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);
  1876. return monitor;
  1877. }
  1878. /*!
  1879. * \internal
  1880. * \brief allocate dialed_cc_interfaces datastore and initialize fields
  1881. *
  1882. * This function is called when Situation 1 occurs in ast_cc_call_init.
  1883. * See that function for more information on what Situation 1 is.
  1884. *
  1885. * In this particular case, we have to do a lot of memory allocation in order
  1886. * to create the datastore, the data for the datastore, the tree of interfaces
  1887. * that we'll be adding to, and the initial extension interface for this Dial
  1888. * attempt.
  1889. *
  1890. * \param chan The channel onto which the datastore should be added.
  1891. * \retval -1 An error occurred
  1892. * \retval 0 Success
  1893. */
  1894. static int cc_interfaces_datastore_init(struct ast_channel *chan) {
  1895. struct dialed_cc_interfaces *interfaces;
  1896. struct ast_cc_monitor *monitor;
  1897. struct ast_datastore *dial_cc_datastore;
  1898. /*XXX This may be a bit controversial. In an attempt to not allocate
  1899. * extra resources, I make sure that a future request will be within
  1900. * limits. The problem here is that it is reasonable to think that
  1901. * even if we're not within the limits at this point, we may be by
  1902. * the time the requestor will have made his request. This may be
  1903. * deleted at some point.
  1904. */
  1905. if (!ast_cc_request_is_within_limits()) {
  1906. return 0;
  1907. }
  1908. if (!(interfaces = ast_calloc(1, sizeof(*interfaces)))) {
  1909. return -1;
  1910. }
  1911. 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))) {
  1912. ast_free(interfaces);
  1913. return -1;
  1914. }
  1915. if (!(dial_cc_datastore = ast_datastore_alloc(&dialed_cc_interfaces_info, NULL))) {
  1916. cc_unref(monitor, "Could not allocate the dialed interfaces datastore. Unreffing monitor");
  1917. ast_free(interfaces);
  1918. return -1;
  1919. }
  1920. if (!(interfaces->interface_tree = ao2_t_alloc(sizeof(*interfaces->interface_tree), cc_interface_tree_destroy,
  1921. "Allocate monitor tree"))) {
  1922. ast_datastore_free(dial_cc_datastore);
  1923. cc_unref(monitor, "Could not allocate monitor tree on dialed interfaces datastore. Unreffing monitor");
  1924. ast_free(interfaces);
  1925. return -1;
  1926. }
  1927. /* Finally, all that allocation is done... */
  1928. AST_LIST_HEAD_INIT(interfaces->interface_tree);
  1929. AST_LIST_INSERT_TAIL(interfaces->interface_tree, monitor, next);
  1930. cc_ref(monitor, "List's reference to extension monitor");
  1931. dial_cc_datastore->data = interfaces;
  1932. dial_cc_datastore->inheritance = DATASTORE_INHERIT_FOREVER;
  1933. interfaces->dial_parent_id = monitor->id;
  1934. interfaces->core_id = monitor->core_id = ast_atomic_fetchadd_int(&core_id_counter, +1);
  1935. interfaces->is_original_caller = 1;
  1936. ast_channel_lock(chan);
  1937. ast_channel_datastore_add(chan, dial_cc_datastore);
  1938. ast_channel_unlock(chan);
  1939. cc_unref(monitor, "Unreffing allocation's reference");
  1940. return 0;
  1941. }
  1942. /*!
  1943. * \internal
  1944. * \brief Call a monitor's destructor before the monitor has been allocated
  1945. * \since 1.8
  1946. *
  1947. * \param monitor_type The type of monitor callbacks to use when calling the destructor
  1948. * \param private_data Data allocated by a channel driver that must be freed
  1949. *
  1950. * \details
  1951. * I'll admit, this is a bit evil.
  1952. *
  1953. * When a channel driver determines that it can offer a call completion service to
  1954. * a caller, it is very likely that the channel driver will need to allocate some
  1955. * data so that when the time comes to request CC, the channel driver will have the
  1956. * necessary data at hand.
  1957. *
  1958. * The problem is that there are many places where failures may occur before the monitor
  1959. * has been properly allocated and had its callbacks assigned to it. If one of these
  1960. * failures should occur, then we still need to let the channel driver know that it
  1961. * must destroy the data that it allocated.
  1962. *
  1963. * \return Nothing
  1964. */
  1965. static void call_destructor_with_no_monitor(const char * const monitor_type, void *private_data)
  1966. {
  1967. const struct ast_cc_monitor_callbacks *monitor_callbacks = find_monitor_callbacks(monitor_type);
  1968. if (!monitor_callbacks) {
  1969. return;
  1970. }
  1971. monitor_callbacks->destructor(private_data);
  1972. }
  1973. /*!
  1974. * \internal
  1975. * \brief Allocate and intitialize a device cc_monitor
  1976. *
  1977. * For all intents and purposes, this is the same as
  1978. * cc_extension_monitor_init, except that there is only
  1979. * a single parameter used for naming the interface.
  1980. *
  1981. * This function is called when handling AST_CONTROL_CC frames.
  1982. * The device has reported that CC is possible, so we add it
  1983. * to the interface_tree.
  1984. *
  1985. * Note that it is not necessarily erroneous to add the same
  1986. * device to the tree twice. If the same device is called by
  1987. * two different extension during the same call, then
  1988. * that is a legitimate situation. Of course, I'm pretty sure
  1989. * the dialed_interfaces global datastore will not allow that
  1990. * to happen anyway.
  1991. *
  1992. * \param device_name The name of the device being added to the tree
  1993. * \param dialstring The dialstring used to dial the device being added
  1994. * \param parent_id The parent of this new tree node.
  1995. * \retval NULL Memory allocation failure
  1996. * \retval non-NULL The new ast_cc_interface created.
  1997. */
  1998. 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)
  1999. {
  2000. struct ast_cc_interface *cc_interface;
  2001. struct ast_cc_monitor *monitor;
  2002. size_t device_name_len = strlen(device_name);
  2003. int parent_id = cc_data->parent_interface_id;
  2004. if (!(cc_interface = ao2_t_alloc(sizeof(*cc_interface) + device_name_len, cc_interface_destroy,
  2005. "Allocating new ast_cc_interface"))) {
  2006. return NULL;
  2007. }
  2008. if (!(cc_interface->config_params = ast_cc_config_params_init())) {
  2009. cc_unref(cc_interface, "Failed to allocate config params, unref interface");
  2010. return NULL;
  2011. }
  2012. if (!(monitor = ao2_t_alloc(sizeof(*monitor), cc_monitor_destroy, "Allocating new ast_cc_monitor"))) {
  2013. cc_unref(cc_interface, "Failed to allocate monitor, unref interface");
  2014. return NULL;
  2015. }
  2016. if (!(monitor->dialstring = ast_strdup(dialstring))) {
  2017. cc_unref(monitor, "Failed to copy dialable name. Unref monitor");
  2018. cc_unref(cc_interface, "Failed to copy dialable name");
  2019. return NULL;
  2020. }
  2021. if (!(monitor->callbacks = find_monitor_callbacks(cc_data->monitor_type))) {
  2022. cc_unref(monitor, "Failed to find monitor callbacks. Unref monitor");
  2023. cc_unref(cc_interface, "Failed to find monitor callbacks");
  2024. return NULL;
  2025. }
  2026. strcpy(cc_interface->device_name, device_name);
  2027. monitor->id = ast_atomic_fetchadd_int(&dialed_cc_interface_counter, +1);
  2028. monitor->parent_id = parent_id;
  2029. monitor->core_id = core_id;
  2030. monitor->service_offered = cc_data->service;
  2031. monitor->private_data = cc_data->private_data;
  2032. cc_interface->monitor_type = cc_data->monitor_type;
  2033. cc_interface->monitor_class = AST_CC_DEVICE_MONITOR;
  2034. monitor->interface = cc_interface;
  2035. monitor->available_timer_id = -1;
  2036. ast_cc_copy_config_params(cc_interface->config_params, &cc_data->config_params);
  2037. ast_log_dynamic_level(cc_logger_level, "Core %d: Created a device cc interface for '%s' with id %u and parent %u\n",
  2038. monitor->core_id, cc_interface->device_name, monitor->id, monitor->parent_id);
  2039. return monitor;
  2040. }
  2041. /*!
  2042. * \details
  2043. * Unless we are ignoring CC for some reason, we will always
  2044. * call this function when we read an AST_CONTROL_CC frame
  2045. * from an outbound channel.
  2046. *
  2047. * This function will call cc_device_monitor_init to
  2048. * create the new cc_monitor for the device from which
  2049. * we read the frame. In addition, the new device will be added
  2050. * to the monitor tree on the dialed_cc_interfaces datastore
  2051. * on the inbound channel.
  2052. *
  2053. * If this is the first AST_CONTROL_CC frame that we have handled
  2054. * for this call, then we will also initialize the CC core for
  2055. * this call.
  2056. */
  2057. void ast_handle_cc_control_frame(struct ast_channel *inbound, struct ast_channel *outbound, void *frame_data)
  2058. {
  2059. char *device_name;
  2060. char *dialstring;
  2061. struct ast_cc_monitor *monitor;
  2062. struct ast_datastore *cc_datastore;
  2063. struct dialed_cc_interfaces *cc_interfaces;
  2064. struct cc_control_payload *cc_data = frame_data;
  2065. struct cc_core_instance *core_instance;
  2066. device_name = cc_data->device_name;
  2067. dialstring = cc_data->dialstring;
  2068. ast_channel_lock(inbound);
  2069. if (!(cc_datastore = ast_channel_datastore_find(inbound, &dialed_cc_interfaces_info, NULL))) {
  2070. ast_log(LOG_WARNING, "Unable to retrieve CC datastore while processing CC frame from '%s'. CC services will be unavailable.\n", device_name);
  2071. ast_channel_unlock(inbound);
  2072. call_destructor_with_no_monitor(cc_data->monitor_type, cc_data->private_data);
  2073. return;
  2074. }
  2075. cc_interfaces = cc_datastore->data;
  2076. if (cc_interfaces->ignore) {
  2077. ast_channel_unlock(inbound);
  2078. call_destructor_with_no_monitor(cc_data->monitor_type, cc_data->private_data);
  2079. return;
  2080. }
  2081. if (!cc_interfaces->is_original_caller) {
  2082. /* If the is_original_caller is not set on the *inbound* channel, then
  2083. * it must be a local channel. As such, we do not want to create a core instance
  2084. * or an agent for the local channel. Instead, we want to pass this along to the
  2085. * other side of the local channel so that the original caller can benefit.
  2086. */
  2087. ast_channel_unlock(inbound);
  2088. ast_indicate_data(inbound, AST_CONTROL_CC, cc_data, sizeof(*cc_data));
  2089. return;
  2090. }
  2091. core_instance = find_cc_core_instance(cc_interfaces->core_id);
  2092. if (!core_instance) {
  2093. core_instance = cc_core_init_instance(inbound, cc_interfaces->interface_tree,
  2094. cc_interfaces->core_id, cc_data);
  2095. if (!core_instance) {
  2096. cc_interfaces->ignore = 1;
  2097. ast_channel_unlock(inbound);
  2098. call_destructor_with_no_monitor(cc_data->monitor_type, cc_data->private_data);
  2099. return;
  2100. }
  2101. }
  2102. ast_channel_unlock(inbound);
  2103. /* Yeah this kind of sucks, but luckily most people
  2104. * aren't dialing thousands of interfaces on every call
  2105. *
  2106. * This traversal helps us to not create duplicate monitors in
  2107. * case a device queues multiple CC control frames.
  2108. */
  2109. AST_LIST_LOCK(cc_interfaces->interface_tree);
  2110. AST_LIST_TRAVERSE(cc_interfaces->interface_tree, monitor, next) {
  2111. if (!strcmp(monitor->interface->device_name, device_name)) {
  2112. ast_log_dynamic_level(cc_logger_level, "Core %d: Device %s sent us multiple CC control frames. Ignoring those beyond the first.\n",
  2113. core_instance->core_id, device_name);
  2114. AST_LIST_UNLOCK(cc_interfaces->interface_tree);
  2115. cc_unref(core_instance, "Returning early from ast_handle_cc_control_frame. Unref core_instance");
  2116. call_destructor_with_no_monitor(cc_data->monitor_type, cc_data->private_data);
  2117. return;
  2118. }
  2119. }
  2120. AST_LIST_UNLOCK(cc_interfaces->interface_tree);
  2121. if (!(monitor = cc_device_monitor_init(device_name, dialstring, cc_data, core_instance->core_id))) {
  2122. ast_log(LOG_WARNING, "Unable to create CC device interface for '%s'. CC services will be unavailable on this interface.\n", device_name);
  2123. cc_unref(core_instance, "Returning early from ast_handle_cc_control_frame. Unref core_instance");
  2124. call_destructor_with_no_monitor(cc_data->monitor_type, cc_data->private_data);
  2125. return;
  2126. }
  2127. AST_LIST_LOCK(cc_interfaces->interface_tree);
  2128. cc_ref(monitor, "monitor tree's reference to the monitor");
  2129. AST_LIST_INSERT_TAIL(cc_interfaces->interface_tree, monitor, next);
  2130. AST_LIST_UNLOCK(cc_interfaces->interface_tree);
  2131. cc_extension_monitor_change_is_valid(core_instance, monitor->parent_id, monitor->interface->device_name, 0);
  2132. cc_publish_available(cc_interfaces->core_id, device_name, cc_service_to_string(cc_data->service));
  2133. cc_unref(core_instance, "Done with core_instance after handling CC control frame");
  2134. cc_unref(monitor, "Unref reference from allocating monitor");
  2135. }
  2136. int ast_cc_call_init(struct ast_channel *chan, int *ignore_cc)
  2137. {
  2138. /* There are three situations to deal with here:
  2139. *
  2140. * 1. The channel does not have a dialed_cc_interfaces datastore on
  2141. * it. This means that this is the first time that Dial has
  2142. * been called. We need to create/initialize the datastore.
  2143. *
  2144. * 2. The channel does have a cc_interface datastore on it and
  2145. * the "ignore" indicator is 0. This means that a Local channel
  2146. * was called by a "parent" dial. We can check the datastore's
  2147. * parent field to see who the root of this particular dial tree
  2148. * is.
  2149. *
  2150. * 3. The channel does have a cc_interface datastore on it and
  2151. * the "ignore" indicator is 1. This means that a second Dial call
  2152. * is being made from an extension. In this case, we do not
  2153. * want to make any additions/modifications to the datastore. We
  2154. * will instead set a flag to indicate that CCSS is completely
  2155. * disabled for this Dial attempt.
  2156. */
  2157. struct ast_datastore *cc_interfaces_datastore;
  2158. struct dialed_cc_interfaces *interfaces;
  2159. struct ast_cc_monitor *monitor;
  2160. struct ast_cc_config_params *cc_params;
  2161. ast_channel_lock(chan);
  2162. cc_params = ast_channel_get_cc_config_params(chan);
  2163. if (!cc_params) {
  2164. ast_channel_unlock(chan);
  2165. return -1;
  2166. }
  2167. if (ast_get_cc_agent_policy(cc_params) == AST_CC_AGENT_NEVER) {
  2168. /* We can't offer CC to this caller anyway, so don't bother with CC on this call
  2169. */
  2170. *ignore_cc = 1;
  2171. ast_channel_unlock(chan);
  2172. ast_log_dynamic_level(cc_logger_level, "Agent policy for %s is 'never'. CC not possible\n", ast_channel_name(chan));
  2173. return 0;
  2174. }
  2175. if (!(cc_interfaces_datastore = ast_channel_datastore_find(chan, &dialed_cc_interfaces_info, NULL))) {
  2176. /* Situation 1 has occurred */
  2177. ast_channel_unlock(chan);
  2178. return cc_interfaces_datastore_init(chan);
  2179. }
  2180. interfaces = cc_interfaces_datastore->data;
  2181. ast_channel_unlock(chan);
  2182. if (interfaces->ignore) {
  2183. /* Situation 3 has occurred */
  2184. *ignore_cc = 1;
  2185. ast_log_dynamic_level(cc_logger_level, "Datastore is present with ignore flag set. Ignoring CC offers on this call\n");
  2186. return 0;
  2187. }
  2188. /* Situation 2 has occurred */
  2189. if (!(monitor = cc_extension_monitor_init(S_OR(ast_channel_macroexten(chan), ast_channel_exten(chan)),
  2190. S_OR(ast_channel_macrocontext(chan), ast_channel_context(chan)), interfaces->dial_parent_id))) {
  2191. return -1;
  2192. }
  2193. monitor->core_id = interfaces->core_id;
  2194. AST_LIST_LOCK(interfaces->interface_tree);
  2195. cc_ref(monitor, "monitor tree's reference to the monitor");
  2196. AST_LIST_INSERT_TAIL(interfaces->interface_tree, monitor, next);
  2197. AST_LIST_UNLOCK(interfaces->interface_tree);
  2198. interfaces->dial_parent_id = monitor->id;
  2199. cc_unref(monitor, "Unref monitor's allocation reference");
  2200. return 0;
  2201. }
  2202. int ast_cc_request_is_within_limits(void)
  2203. {
  2204. return cc_request_count < global_cc_max_requests;
  2205. }
  2206. int ast_cc_get_current_core_id(struct ast_channel *chan)
  2207. {
  2208. struct ast_datastore *datastore;
  2209. struct dialed_cc_interfaces *cc_interfaces;
  2210. int core_id_return;
  2211. ast_channel_lock(chan);
  2212. if (!(datastore = ast_channel_datastore_find(chan, &dialed_cc_interfaces_info, NULL))) {
  2213. ast_channel_unlock(chan);
  2214. return -1;
  2215. }
  2216. cc_interfaces = datastore->data;
  2217. core_id_return = cc_interfaces->ignore ? -1 : cc_interfaces->core_id;
  2218. ast_channel_unlock(chan);
  2219. return core_id_return;
  2220. }
  2221. static long count_agents(const char * const caller, const int core_id_exception)
  2222. {
  2223. struct count_agents_cb_data data = {.core_id_exception = core_id_exception,};
  2224. ao2_t_callback_data(cc_core_instances, OBJ_NODATA, count_agents_cb, (char *)caller, &data, "Counting agents");
  2225. ast_log_dynamic_level(cc_logger_level, "Counted %d agents\n", data.count);
  2226. return data.count;
  2227. }
  2228. static void kill_duplicate_offers(char *caller)
  2229. {
  2230. unsigned long match_flags = MATCH_NO_REQUEST;
  2231. struct ao2_iterator *dups_iter;
  2232. /*
  2233. * Must remove the ref that was in cc_core_instances outside of
  2234. * the container lock to prevent deadlock.
  2235. */
  2236. dups_iter = ao2_t_callback_data(cc_core_instances, OBJ_MULTIPLE | OBJ_UNLINK,
  2237. match_agent, caller, &match_flags, "Killing duplicate offers");
  2238. if (dups_iter) {
  2239. /* Now actually unref any duplicate offers by simply destroying the iterator. */
  2240. ao2_iterator_destroy(dups_iter);
  2241. }
  2242. }
  2243. static void check_callback_sanity(const struct ast_cc_agent_callbacks *callbacks)
  2244. {
  2245. ast_assert(callbacks->init != NULL);
  2246. ast_assert(callbacks->start_offer_timer != NULL);
  2247. ast_assert(callbacks->stop_offer_timer != NULL);
  2248. ast_assert(callbacks->respond != NULL);
  2249. ast_assert(callbacks->status_request != NULL);
  2250. ast_assert(callbacks->start_monitoring != NULL);
  2251. ast_assert(callbacks->callee_available != NULL);
  2252. ast_assert(callbacks->destructor != NULL);
  2253. }
  2254. static void agent_destroy(void *data)
  2255. {
  2256. struct ast_cc_agent *agent = data;
  2257. if (agent->callbacks) {
  2258. agent->callbacks->destructor(agent);
  2259. }
  2260. ast_cc_config_params_destroy(agent->cc_params);
  2261. }
  2262. static struct ast_cc_agent *cc_agent_init(struct ast_channel *caller_chan,
  2263. const char * const caller_name, const int core_id,
  2264. struct cc_monitor_tree *interface_tree)
  2265. {
  2266. struct ast_cc_agent *agent;
  2267. struct ast_cc_config_params *cc_params;
  2268. if (!(agent = ao2_t_alloc(sizeof(*agent) + strlen(caller_name), agent_destroy,
  2269. "Allocating new ast_cc_agent"))) {
  2270. return NULL;
  2271. }
  2272. agent->core_id = core_id;
  2273. strcpy(agent->device_name, caller_name);
  2274. cc_params = ast_channel_get_cc_config_params(caller_chan);
  2275. if (!cc_params) {
  2276. cc_unref(agent, "Could not get channel config params.");
  2277. return NULL;
  2278. }
  2279. if (!(agent->cc_params = ast_cc_config_params_init())) {
  2280. cc_unref(agent, "Could not init agent config params.");
  2281. return NULL;
  2282. }
  2283. ast_cc_copy_config_params(agent->cc_params, cc_params);
  2284. if (!(agent->callbacks = find_agent_callbacks(caller_chan))) {
  2285. cc_unref(agent, "Could not find agent callbacks.");
  2286. return NULL;
  2287. }
  2288. check_callback_sanity(agent->callbacks);
  2289. if (agent->callbacks->init(agent, caller_chan)) {
  2290. cc_unref(agent, "Agent init callback failed.");
  2291. return NULL;
  2292. }
  2293. ast_log_dynamic_level(cc_logger_level, "Core %u: Created an agent for caller %s\n",
  2294. agent->core_id, agent->device_name);
  2295. return agent;
  2296. }
  2297. /* Generic agent callbacks */
  2298. static int cc_generic_agent_init(struct ast_cc_agent *agent, struct ast_channel *chan);
  2299. static int cc_generic_agent_start_offer_timer(struct ast_cc_agent *agent);
  2300. static int cc_generic_agent_stop_offer_timer(struct ast_cc_agent *agent);
  2301. static void cc_generic_agent_respond(struct ast_cc_agent *agent, enum ast_cc_agent_response_reason reason);
  2302. static int cc_generic_agent_status_request(struct ast_cc_agent *agent);
  2303. static int cc_generic_agent_stop_ringing(struct ast_cc_agent *agent);
  2304. static int cc_generic_agent_start_monitoring(struct ast_cc_agent *agent);
  2305. static int cc_generic_agent_recall(struct ast_cc_agent *agent);
  2306. static void cc_generic_agent_destructor(struct ast_cc_agent *agent);
  2307. static struct ast_cc_agent_callbacks generic_agent_callbacks = {
  2308. .type = "generic",
  2309. .init = cc_generic_agent_init,
  2310. .start_offer_timer = cc_generic_agent_start_offer_timer,
  2311. .stop_offer_timer = cc_generic_agent_stop_offer_timer,
  2312. .respond = cc_generic_agent_respond,
  2313. .status_request = cc_generic_agent_status_request,
  2314. .stop_ringing = cc_generic_agent_stop_ringing,
  2315. .start_monitoring = cc_generic_agent_start_monitoring,
  2316. .callee_available = cc_generic_agent_recall,
  2317. .destructor = cc_generic_agent_destructor,
  2318. };
  2319. struct cc_generic_agent_pvt {
  2320. /*!
  2321. * Subscription to device state
  2322. *
  2323. * Used in the CC_CALLER_BUSY state. The
  2324. * generic agent will subscribe to the
  2325. * device state of the caller in order to
  2326. * determine when we may move on
  2327. */
  2328. struct stasis_subscription *sub;
  2329. /*!
  2330. * Scheduler id of offer timer.
  2331. */
  2332. int offer_timer_id;
  2333. /*!
  2334. * Caller ID number
  2335. *
  2336. * When we re-call the caller, we need
  2337. * to provide this information to
  2338. * ast_request_and_dial so that the
  2339. * information will be present in the
  2340. * call to the callee
  2341. */
  2342. char cid_num[AST_CHANNEL_NAME];
  2343. /*!
  2344. * Caller ID name
  2345. *
  2346. * See the description of cid_num.
  2347. * The same applies here, except this
  2348. * is the caller's name.
  2349. */
  2350. char cid_name[AST_CHANNEL_NAME];
  2351. /*!
  2352. * Extension dialed
  2353. *
  2354. * The original extension dialed. This is used
  2355. * so that when performing a recall, we can
  2356. * call the proper extension.
  2357. */
  2358. char exten[AST_CHANNEL_NAME];
  2359. /*!
  2360. * Context dialed
  2361. *
  2362. * The original context dialed. This is used
  2363. * so that when performaing a recall, we can
  2364. * call into the proper context
  2365. */
  2366. char context[AST_CHANNEL_NAME];
  2367. };
  2368. static int cc_generic_agent_init(struct ast_cc_agent *agent, struct ast_channel *chan)
  2369. {
  2370. struct cc_generic_agent_pvt *generic_pvt = ast_calloc(1, sizeof(*generic_pvt));
  2371. if (!generic_pvt) {
  2372. return -1;
  2373. }
  2374. generic_pvt->offer_timer_id = -1;
  2375. if (ast_channel_caller(chan)->id.number.valid && ast_channel_caller(chan)->id.number.str) {
  2376. ast_copy_string(generic_pvt->cid_num, ast_channel_caller(chan)->id.number.str, sizeof(generic_pvt->cid_num));
  2377. }
  2378. if (ast_channel_caller(chan)->id.name.valid && ast_channel_caller(chan)->id.name.str) {
  2379. ast_copy_string(generic_pvt->cid_name, ast_channel_caller(chan)->id.name.str, sizeof(generic_pvt->cid_name));
  2380. }
  2381. ast_copy_string(generic_pvt->exten, S_OR(ast_channel_macroexten(chan), ast_channel_exten(chan)), sizeof(generic_pvt->exten));
  2382. ast_copy_string(generic_pvt->context, S_OR(ast_channel_macrocontext(chan), ast_channel_context(chan)), sizeof(generic_pvt->context));
  2383. agent->private_data = generic_pvt;
  2384. ast_set_flag(agent, AST_CC_AGENT_SKIP_OFFER);
  2385. return 0;
  2386. }
  2387. static int offer_timer_expire(const void *data)
  2388. {
  2389. struct ast_cc_agent *agent = (struct ast_cc_agent *) data;
  2390. struct cc_generic_agent_pvt *agent_pvt = agent->private_data;
  2391. ast_log_dynamic_level(cc_logger_level, "Core %u: Queuing change request because offer timer has expired.\n",
  2392. agent->core_id);
  2393. agent_pvt->offer_timer_id = -1;
  2394. ast_cc_failed(agent->core_id, "Generic agent %s offer timer expired", agent->device_name);
  2395. cc_unref(agent, "Remove scheduler's reference to the agent");
  2396. return 0;
  2397. }
  2398. static int cc_generic_agent_start_offer_timer(struct ast_cc_agent *agent)
  2399. {
  2400. int when;
  2401. int sched_id;
  2402. struct cc_generic_agent_pvt *generic_pvt = agent->private_data;
  2403. ast_assert(cc_sched_context != NULL);
  2404. ast_assert(agent->cc_params != NULL);
  2405. when = ast_get_cc_offer_timer(agent->cc_params) * 1000;
  2406. ast_log_dynamic_level(cc_logger_level, "Core %u: About to schedule offer timer expiration for %d ms\n",
  2407. agent->core_id, when);
  2408. if ((sched_id = ast_sched_add(cc_sched_context, when, offer_timer_expire, cc_ref(agent, "Give scheduler an agent ref"))) == -1) {
  2409. return -1;
  2410. }
  2411. generic_pvt->offer_timer_id = sched_id;
  2412. return 0;
  2413. }
  2414. static int cc_generic_agent_stop_offer_timer(struct ast_cc_agent *agent)
  2415. {
  2416. struct cc_generic_agent_pvt *generic_pvt = agent->private_data;
  2417. if (generic_pvt->offer_timer_id != -1) {
  2418. if (!ast_sched_del(cc_sched_context, generic_pvt->offer_timer_id)) {
  2419. cc_unref(agent, "Remove scheduler's reference to the agent");
  2420. }
  2421. generic_pvt->offer_timer_id = -1;
  2422. }
  2423. return 0;
  2424. }
  2425. static void cc_generic_agent_respond(struct ast_cc_agent *agent, enum ast_cc_agent_response_reason reason)
  2426. {
  2427. /* The generic agent doesn't have to do anything special to
  2428. * acknowledge a CC request. Just return.
  2429. */
  2430. return;
  2431. }
  2432. static int cc_generic_agent_status_request(struct ast_cc_agent *agent)
  2433. {
  2434. ast_cc_agent_status_response(agent->core_id, ast_device_state(agent->device_name));
  2435. return 0;
  2436. }
  2437. static int cc_generic_agent_stop_ringing(struct ast_cc_agent *agent)
  2438. {
  2439. struct ast_channel *recall_chan = ast_channel_get_by_name_prefix(agent->device_name, strlen(agent->device_name));
  2440. if (!recall_chan) {
  2441. return 0;
  2442. }
  2443. ast_softhangup(recall_chan, AST_SOFTHANGUP_EXPLICIT);
  2444. return 0;
  2445. }
  2446. static void generic_agent_devstate_cb(void *userdata, struct stasis_subscription *sub, struct stasis_message *msg)
  2447. {
  2448. struct ast_cc_agent *agent = userdata;
  2449. enum ast_device_state new_state;
  2450. struct ast_device_state_message *dev_state;
  2451. struct cc_generic_agent_pvt *generic_pvt = agent->private_data;
  2452. if (stasis_subscription_final_message(sub, msg)) {
  2453. cc_unref(agent, "Done holding ref for subscription");
  2454. return;
  2455. } else if (ast_device_state_message_type() != stasis_message_type(msg)) {
  2456. return;
  2457. }
  2458. dev_state = stasis_message_data(msg);
  2459. if (dev_state->eid) {
  2460. /* ignore non-aggregate states */
  2461. return;
  2462. }
  2463. new_state = dev_state->state;
  2464. if (!cc_generic_is_device_available(new_state)) {
  2465. /* Not interested in this new state of the device. It is still busy. */
  2466. return;
  2467. }
  2468. generic_pvt->sub = stasis_unsubscribe(sub);
  2469. ast_cc_agent_caller_available(agent->core_id, "%s is no longer busy", agent->device_name);
  2470. }
  2471. static int cc_generic_agent_start_monitoring(struct ast_cc_agent *agent)
  2472. {
  2473. struct cc_generic_agent_pvt *generic_pvt = agent->private_data;
  2474. struct ast_str *str = ast_str_alloca(128);
  2475. struct stasis_topic *device_specific_topic;
  2476. ast_assert(generic_pvt->sub == NULL);
  2477. ast_str_set(&str, 0, "Agent monitoring %s device state since it is busy\n",
  2478. agent->device_name);
  2479. device_specific_topic = ast_device_state_topic(agent->device_name);
  2480. if (!device_specific_topic) {
  2481. return -1;
  2482. }
  2483. if (!(generic_pvt->sub = stasis_subscribe(device_specific_topic, generic_agent_devstate_cb, agent))) {
  2484. return -1;
  2485. }
  2486. cc_ref(agent, "Ref agent for subscription");
  2487. return 0;
  2488. }
  2489. static void *generic_recall(void *data)
  2490. {
  2491. struct ast_cc_agent *agent = data;
  2492. struct cc_generic_agent_pvt *generic_pvt = agent->private_data;
  2493. const char *interface = S_OR(ast_get_cc_agent_dialstring(agent->cc_params), ast_strdupa(agent->device_name));
  2494. const char *tech;
  2495. char *target;
  2496. int reason;
  2497. struct ast_channel *chan;
  2498. const char *callback_macro = ast_get_cc_callback_macro(agent->cc_params);
  2499. const char *callback_sub = ast_get_cc_callback_sub(agent->cc_params);
  2500. unsigned int recall_timer = ast_get_cc_recall_timer(agent->cc_params) * 1000;
  2501. struct ast_format_cap *tmp_cap = ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_DEFAULT);
  2502. if (!tmp_cap) {
  2503. return NULL;
  2504. }
  2505. tech = interface;
  2506. if ((target = strchr(interface, '/'))) {
  2507. *target++ = '\0';
  2508. }
  2509. ast_format_cap_append(tmp_cap, ast_format_slin, 0);
  2510. if (!(chan = ast_request_and_dial(tech, tmp_cap, NULL, NULL, target, recall_timer, &reason, generic_pvt->cid_num, generic_pvt->cid_name))) {
  2511. /* Hmm, no channel. Sucks for you, bud.
  2512. */
  2513. ast_log_dynamic_level(cc_logger_level, "Core %u: Failed to call back %s for reason %d\n",
  2514. agent->core_id, agent->device_name, reason);
  2515. ast_cc_failed(agent->core_id, "Failed to call back device %s/%s", tech, target);
  2516. ao2_ref(tmp_cap, -1);
  2517. return NULL;
  2518. }
  2519. ao2_ref(tmp_cap, -1);
  2520. /* We have a channel. It's time now to set up the datastore of recalled CC interfaces.
  2521. * This will be a common task for all recall functions. If it were possible, I'd have
  2522. * the core do it automatically, but alas I cannot. Instead, I will provide a public
  2523. * function to do so.
  2524. */
  2525. ast_setup_cc_recall_datastore(chan, agent->core_id);
  2526. ast_cc_agent_set_interfaces_chanvar(chan);
  2527. ast_channel_exten_set(chan, generic_pvt->exten);
  2528. ast_channel_context_set(chan, generic_pvt->context);
  2529. ast_channel_priority_set(chan, 1);
  2530. pbx_builtin_setvar_helper(chan, "CC_EXTEN", generic_pvt->exten);
  2531. pbx_builtin_setvar_helper(chan, "CC_CONTEXT", generic_pvt->context);
  2532. if (!ast_strlen_zero(callback_macro)) {
  2533. ast_log_dynamic_level(cc_logger_level, "Core %u: There's a callback macro configured for agent %s\n",
  2534. agent->core_id, agent->device_name);
  2535. if (ast_app_exec_macro(NULL, chan, callback_macro)) {
  2536. ast_cc_failed(agent->core_id, "Callback macro to %s failed. Maybe a hangup?", agent->device_name);
  2537. ast_hangup(chan);
  2538. return NULL;
  2539. }
  2540. }
  2541. if (!ast_strlen_zero(callback_sub)) {
  2542. ast_log_dynamic_level(cc_logger_level, "Core %u: There's a callback subroutine configured for agent %s\n",
  2543. agent->core_id, agent->device_name);
  2544. if (ast_app_exec_sub(NULL, chan, callback_sub, 0)) {
  2545. ast_cc_failed(agent->core_id, "Callback subroutine to %s failed. Maybe a hangup?", agent->device_name);
  2546. ast_hangup(chan);
  2547. return NULL;
  2548. }
  2549. }
  2550. if (ast_pbx_start(chan)) {
  2551. ast_cc_failed(agent->core_id, "PBX failed to start for %s.", agent->device_name);
  2552. ast_hangup(chan);
  2553. return NULL;
  2554. }
  2555. ast_cc_agent_recalling(agent->core_id, "Generic agent %s is recalling",
  2556. agent->device_name);
  2557. return NULL;
  2558. }
  2559. static int cc_generic_agent_recall(struct ast_cc_agent *agent)
  2560. {
  2561. pthread_t clotho;
  2562. enum ast_device_state current_state = ast_device_state(agent->device_name);
  2563. if (!cc_generic_is_device_available(current_state)) {
  2564. /* We can't try to contact the device right now because he's not available
  2565. * Let the core know he's busy.
  2566. */
  2567. ast_cc_agent_caller_busy(agent->core_id, "Generic agent caller %s is busy", agent->device_name);
  2568. return 0;
  2569. }
  2570. ast_pthread_create_detached_background(&clotho, NULL, generic_recall, agent);
  2571. return 0;
  2572. }
  2573. static void cc_generic_agent_destructor(struct ast_cc_agent *agent)
  2574. {
  2575. struct cc_generic_agent_pvt *agent_pvt = agent->private_data;
  2576. if (!agent_pvt) {
  2577. /* The agent constructor probably failed. */
  2578. return;
  2579. }
  2580. cc_generic_agent_stop_offer_timer(agent);
  2581. if (agent_pvt->sub) {
  2582. agent_pvt->sub = stasis_unsubscribe(agent_pvt->sub);
  2583. }
  2584. ast_free(agent_pvt);
  2585. }
  2586. static void cc_core_instance_destructor(void *data)
  2587. {
  2588. struct cc_core_instance *core_instance = data;
  2589. ast_log_dynamic_level(cc_logger_level, "Core %d: Destroying core instance\n", core_instance->core_id);
  2590. if (core_instance->agent) {
  2591. cc_unref(core_instance->agent, "Core instance is done with the agent now");
  2592. }
  2593. if (core_instance->monitors) {
  2594. core_instance->monitors = cc_unref(core_instance->monitors, "Core instance is done with interface list");
  2595. }
  2596. }
  2597. static struct cc_core_instance *cc_core_init_instance(struct ast_channel *caller_chan,
  2598. struct cc_monitor_tree *called_tree, const int core_id, struct cc_control_payload *cc_data)
  2599. {
  2600. char caller[AST_CHANNEL_NAME];
  2601. struct cc_core_instance *core_instance;
  2602. struct ast_cc_config_params *cc_params;
  2603. long agent_count;
  2604. int recall_core_id;
  2605. ast_channel_get_device_name(caller_chan, caller, sizeof(caller));
  2606. cc_params = ast_channel_get_cc_config_params(caller_chan);
  2607. if (!cc_params) {
  2608. ast_log_dynamic_level(cc_logger_level, "Could not get CC parameters for %s\n",
  2609. caller);
  2610. return NULL;
  2611. }
  2612. /* First, we need to kill off other pending CC offers from caller. If the caller is going
  2613. * to request a CC service, it may only be for the latest call he made.
  2614. */
  2615. if (ast_get_cc_agent_policy(cc_params) == AST_CC_AGENT_GENERIC) {
  2616. kill_duplicate_offers(caller);
  2617. }
  2618. ast_cc_is_recall(caller_chan, &recall_core_id, NULL);
  2619. agent_count = count_agents(caller, recall_core_id);
  2620. if (agent_count >= ast_get_cc_max_agents(cc_params)) {
  2621. ast_log_dynamic_level(cc_logger_level, "Caller %s already has the maximum number of agents configured\n", caller);
  2622. return NULL;
  2623. }
  2624. /* Generic agents can only have a single outstanding CC request per caller. */
  2625. if (agent_count > 0 && ast_get_cc_agent_policy(cc_params) == AST_CC_AGENT_GENERIC) {
  2626. ast_log_dynamic_level(cc_logger_level, "Generic agents can only have a single outstanding request\n");
  2627. return NULL;
  2628. }
  2629. /* Next, we need to create the core instance for this call */
  2630. if (!(core_instance = ao2_t_alloc(sizeof(*core_instance), cc_core_instance_destructor, "Creating core instance for CC"))) {
  2631. return NULL;
  2632. }
  2633. core_instance->core_id = core_id;
  2634. if (!(core_instance->agent = cc_agent_init(caller_chan, caller, core_instance->core_id, called_tree))) {
  2635. cc_unref(core_instance, "Couldn't allocate agent, unref core_instance");
  2636. return NULL;
  2637. }
  2638. core_instance->monitors = cc_ref(called_tree, "Core instance getting ref to monitor tree");
  2639. ao2_t_link(cc_core_instances, core_instance, "Link core instance into container");
  2640. return core_instance;
  2641. }
  2642. struct cc_state_change_args {
  2643. struct cc_core_instance *core_instance;/*!< Holds reference to core instance. */
  2644. enum cc_state state;
  2645. int core_id;
  2646. char debug[1];
  2647. };
  2648. static int is_state_change_valid(enum cc_state current_state, const enum cc_state new_state, struct ast_cc_agent *agent)
  2649. {
  2650. int is_valid = 0;
  2651. switch (new_state) {
  2652. case CC_AVAILABLE:
  2653. ast_log_dynamic_level(cc_logger_level, "Core %u: Asked to change to state %u? That should never happen.\n",
  2654. agent->core_id, new_state);
  2655. break;
  2656. case CC_CALLER_OFFERED:
  2657. if (current_state == CC_AVAILABLE) {
  2658. is_valid = 1;
  2659. }
  2660. break;
  2661. case CC_CALLER_REQUESTED:
  2662. if (current_state == CC_CALLER_OFFERED ||
  2663. (current_state == CC_AVAILABLE && ast_test_flag(agent, AST_CC_AGENT_SKIP_OFFER))) {
  2664. is_valid = 1;
  2665. }
  2666. break;
  2667. case CC_ACTIVE:
  2668. if (current_state == CC_CALLER_REQUESTED || current_state == CC_CALLER_BUSY) {
  2669. is_valid = 1;
  2670. }
  2671. break;
  2672. case CC_CALLEE_READY:
  2673. if (current_state == CC_ACTIVE) {
  2674. is_valid = 1;
  2675. }
  2676. break;
  2677. case CC_CALLER_BUSY:
  2678. if (current_state == CC_CALLEE_READY) {
  2679. is_valid = 1;
  2680. }
  2681. break;
  2682. case CC_RECALLING:
  2683. if (current_state == CC_CALLEE_READY) {
  2684. is_valid = 1;
  2685. }
  2686. break;
  2687. case CC_COMPLETE:
  2688. if (current_state == CC_RECALLING) {
  2689. is_valid = 1;
  2690. }
  2691. break;
  2692. case CC_FAILED:
  2693. is_valid = 1;
  2694. break;
  2695. default:
  2696. ast_log_dynamic_level(cc_logger_level, "Core %u: Asked to change to unknown state %u\n",
  2697. agent->core_id, new_state);
  2698. break;
  2699. }
  2700. return is_valid;
  2701. }
  2702. static int cc_available(struct cc_core_instance *core_instance, struct cc_state_change_args *args, enum cc_state previous_state)
  2703. {
  2704. /* This should never happen... */
  2705. ast_log(LOG_WARNING, "Someone requested to change to CC_AVAILABLE? Ignoring.\n");
  2706. return -1;
  2707. }
  2708. static int cc_caller_offered(struct cc_core_instance *core_instance, struct cc_state_change_args *args, enum cc_state previous_state)
  2709. {
  2710. if (core_instance->agent->callbacks->start_offer_timer(core_instance->agent)) {
  2711. ast_cc_failed(core_instance->core_id, "Failed to start the offer timer for %s\n",
  2712. core_instance->agent->device_name);
  2713. return -1;
  2714. }
  2715. cc_publish_offertimerstart(core_instance->core_id, core_instance->agent->device_name, core_instance->agent->cc_params->cc_offer_timer);
  2716. ast_log_dynamic_level(cc_logger_level, "Core %d: Started the offer timer for the agent %s!\n",
  2717. core_instance->core_id, core_instance->agent->device_name);
  2718. return 0;
  2719. }
  2720. /*!
  2721. * \brief check if the core instance has any device monitors
  2722. *
  2723. * In any case where we end up removing a device monitor from the
  2724. * list of device monitors, it is important to see what the state
  2725. * of the list is afterwards. If we find that we only have extension
  2726. * monitors left, then no devices are actually being monitored.
  2727. * In such a case, we need to declare that CC has failed for this
  2728. * call. This function helps those cases to determine if they should
  2729. * declare failure.
  2730. *
  2731. * \param core_instance The core instance we are checking for the existence
  2732. * of device monitors
  2733. * \retval 0 No device monitors exist on this core_instance
  2734. * \retval 1 There is still at least 1 device monitor remaining
  2735. */
  2736. static int has_device_monitors(struct cc_core_instance *core_instance)
  2737. {
  2738. struct ast_cc_monitor *iter;
  2739. int res = 0;
  2740. AST_LIST_TRAVERSE(core_instance->monitors, iter, next) {
  2741. if (iter->interface->monitor_class == AST_CC_DEVICE_MONITOR) {
  2742. res = 1;
  2743. break;
  2744. }
  2745. }
  2746. return res;
  2747. }
  2748. static void request_cc(struct cc_core_instance *core_instance)
  2749. {
  2750. struct ast_cc_monitor *monitor_iter;
  2751. AST_LIST_LOCK(core_instance->monitors);
  2752. AST_LIST_TRAVERSE_SAFE_BEGIN(core_instance->monitors, monitor_iter, next) {
  2753. if (monitor_iter->interface->monitor_class == AST_CC_DEVICE_MONITOR) {
  2754. if (monitor_iter->callbacks->request_cc(monitor_iter, &monitor_iter->available_timer_id)) {
  2755. AST_LIST_REMOVE_CURRENT(next);
  2756. cc_extension_monitor_change_is_valid(core_instance, monitor_iter->parent_id,
  2757. monitor_iter->interface->device_name, 1);
  2758. cc_unref(monitor_iter, "request_cc failed. Unref list's reference to monitor");
  2759. } else {
  2760. cc_publish_requested(core_instance->core_id, core_instance->agent->device_name, monitor_iter->interface->device_name);
  2761. }
  2762. }
  2763. }
  2764. AST_LIST_TRAVERSE_SAFE_END;
  2765. if (!has_device_monitors(core_instance)) {
  2766. ast_cc_failed(core_instance->core_id, "All device monitors failed to request CC");
  2767. }
  2768. AST_LIST_UNLOCK(core_instance->monitors);
  2769. }
  2770. static int cc_caller_requested(struct cc_core_instance *core_instance, struct cc_state_change_args *args, enum cc_state previous_state)
  2771. {
  2772. if (!ast_cc_request_is_within_limits()) {
  2773. ast_log(LOG_WARNING, "Cannot request CC since there is no more room for requests\n");
  2774. core_instance->agent->callbacks->respond(core_instance->agent,
  2775. AST_CC_AGENT_RESPONSE_FAILURE_TOO_MANY);
  2776. ast_cc_failed(core_instance->core_id, "Too many requests in the system");
  2777. return -1;
  2778. }
  2779. core_instance->agent->callbacks->stop_offer_timer(core_instance->agent);
  2780. request_cc(core_instance);
  2781. return 0;
  2782. }
  2783. static void unsuspend(struct cc_core_instance *core_instance)
  2784. {
  2785. struct ast_cc_monitor *monitor_iter;
  2786. AST_LIST_LOCK(core_instance->monitors);
  2787. AST_LIST_TRAVERSE_SAFE_BEGIN(core_instance->monitors, monitor_iter, next) {
  2788. if (monitor_iter->interface->monitor_class == AST_CC_DEVICE_MONITOR) {
  2789. if (monitor_iter->callbacks->unsuspend(monitor_iter)) {
  2790. AST_LIST_REMOVE_CURRENT(next);
  2791. cc_extension_monitor_change_is_valid(core_instance, monitor_iter->parent_id,
  2792. monitor_iter->interface->device_name, 1);
  2793. cc_unref(monitor_iter, "unsuspend failed. Unref list's reference to monitor");
  2794. }
  2795. }
  2796. }
  2797. AST_LIST_TRAVERSE_SAFE_END;
  2798. if (!has_device_monitors(core_instance)) {
  2799. ast_cc_failed(core_instance->core_id, "All device monitors failed to unsuspend CC");
  2800. }
  2801. AST_LIST_UNLOCK(core_instance->monitors);
  2802. }
  2803. static int cc_active(struct cc_core_instance *core_instance, struct cc_state_change_args *args, enum cc_state previous_state)
  2804. {
  2805. /* Either
  2806. * 1. Callee accepted CC request, call agent's ack callback.
  2807. * 2. Caller became available, call agent's stop_monitoring callback and
  2808. * call monitor's unsuspend callback.
  2809. */
  2810. if (previous_state == CC_CALLER_REQUESTED) {
  2811. core_instance->agent->callbacks->respond(core_instance->agent,
  2812. AST_CC_AGENT_RESPONSE_SUCCESS);
  2813. cc_publish_requestacknowledged(core_instance->core_id, core_instance->agent->device_name);
  2814. } else if (previous_state == CC_CALLER_BUSY) {
  2815. cc_publish_callerstopmonitoring(core_instance->core_id, core_instance->agent->device_name);
  2816. unsuspend(core_instance);
  2817. }
  2818. /* Not possible for previous_state to be anything else due to the is_state_change_valid check at the beginning */
  2819. return 0;
  2820. }
  2821. static int cc_callee_ready(struct cc_core_instance *core_instance, struct cc_state_change_args *args, enum cc_state previous_state)
  2822. {
  2823. core_instance->agent->callbacks->callee_available(core_instance->agent);
  2824. return 0;
  2825. }
  2826. static void suspend(struct cc_core_instance *core_instance)
  2827. {
  2828. struct ast_cc_monitor *monitor_iter;
  2829. AST_LIST_LOCK(core_instance->monitors);
  2830. AST_LIST_TRAVERSE_SAFE_BEGIN(core_instance->monitors, monitor_iter, next) {
  2831. if (monitor_iter->interface->monitor_class == AST_CC_DEVICE_MONITOR) {
  2832. if (monitor_iter->callbacks->suspend(monitor_iter)) {
  2833. AST_LIST_REMOVE_CURRENT(next);
  2834. cc_extension_monitor_change_is_valid(core_instance, monitor_iter->parent_id,
  2835. monitor_iter->interface->device_name, 1);
  2836. cc_unref(monitor_iter, "suspend failed. Unref list's reference to monitor");
  2837. }
  2838. }
  2839. }
  2840. AST_LIST_TRAVERSE_SAFE_END;
  2841. if (!has_device_monitors(core_instance)) {
  2842. ast_cc_failed(core_instance->core_id, "All device monitors failed to suspend CC");
  2843. }
  2844. AST_LIST_UNLOCK(core_instance->monitors);
  2845. }
  2846. static int cc_caller_busy(struct cc_core_instance *core_instance, struct cc_state_change_args *args, enum cc_state previous_state)
  2847. {
  2848. /* Callee was available, but caller was busy, call agent's begin_monitoring callback
  2849. * and call monitor's suspend callback.
  2850. */
  2851. suspend(core_instance);
  2852. core_instance->agent->callbacks->start_monitoring(core_instance->agent);
  2853. cc_publish_callerstartmonitoring(core_instance->core_id, core_instance->agent->device_name);
  2854. return 0;
  2855. }
  2856. static void cancel_available_timer(struct cc_core_instance *core_instance)
  2857. {
  2858. struct ast_cc_monitor *monitor_iter;
  2859. AST_LIST_LOCK(core_instance->monitors);
  2860. AST_LIST_TRAVERSE_SAFE_BEGIN(core_instance->monitors, monitor_iter, next) {
  2861. if (monitor_iter->interface->monitor_class == AST_CC_DEVICE_MONITOR) {
  2862. if (monitor_iter->callbacks->cancel_available_timer(monitor_iter, &monitor_iter->available_timer_id)) {
  2863. AST_LIST_REMOVE_CURRENT(next);
  2864. cc_extension_monitor_change_is_valid(core_instance, monitor_iter->parent_id,
  2865. monitor_iter->interface->device_name, 1);
  2866. cc_unref(monitor_iter, "cancel_available_timer failed. Unref list's reference to monitor");
  2867. }
  2868. }
  2869. }
  2870. AST_LIST_TRAVERSE_SAFE_END;
  2871. if (!has_device_monitors(core_instance)) {
  2872. ast_cc_failed(core_instance->core_id, "All device monitors failed to cancel their available timers");
  2873. }
  2874. AST_LIST_UNLOCK(core_instance->monitors);
  2875. }
  2876. static int cc_recalling(struct cc_core_instance *core_instance, struct cc_state_change_args *args, enum cc_state previous_state)
  2877. {
  2878. /* Both caller and callee are available, call agent's recall callback
  2879. */
  2880. cancel_available_timer(core_instance);
  2881. cc_publish_callerrecalling(core_instance->core_id, core_instance->agent->device_name);
  2882. return 0;
  2883. }
  2884. static int cc_complete(struct cc_core_instance *core_instance, struct cc_state_change_args *args, enum cc_state previous_state)
  2885. {
  2886. /* Recall has made progress, call agent and monitor destructor functions
  2887. */
  2888. cc_publish_recallcomplete(core_instance->core_id, core_instance->agent->device_name);
  2889. ao2_t_unlink(cc_core_instances, core_instance, "Unlink core instance since CC recall has completed");
  2890. return 0;
  2891. }
  2892. static int cc_failed(struct cc_core_instance *core_instance, struct cc_state_change_args *args, enum cc_state previous_state)
  2893. {
  2894. cc_publish_failure(core_instance->core_id, core_instance->agent->device_name, args->debug);
  2895. ao2_t_unlink(cc_core_instances, core_instance, "Unlink core instance since CC failed");
  2896. return 0;
  2897. }
  2898. static int (* const state_change_funcs [])(struct cc_core_instance *, struct cc_state_change_args *, enum cc_state previous_state) = {
  2899. [CC_AVAILABLE] = cc_available,
  2900. [CC_CALLER_OFFERED] = cc_caller_offered,
  2901. [CC_CALLER_REQUESTED] = cc_caller_requested,
  2902. [CC_ACTIVE] = cc_active,
  2903. [CC_CALLEE_READY] = cc_callee_ready,
  2904. [CC_CALLER_BUSY] = cc_caller_busy,
  2905. [CC_RECALLING] = cc_recalling,
  2906. [CC_COMPLETE] = cc_complete,
  2907. [CC_FAILED] = cc_failed,
  2908. };
  2909. static int cc_do_state_change(void *datap)
  2910. {
  2911. struct cc_state_change_args *args = datap;
  2912. struct cc_core_instance *core_instance;
  2913. enum cc_state previous_state;
  2914. int res;
  2915. ast_log_dynamic_level(cc_logger_level, "Core %d: State change to %u requested. Reason: %s\n",
  2916. args->core_id, args->state, args->debug);
  2917. core_instance = args->core_instance;
  2918. if (!is_state_change_valid(core_instance->current_state, args->state, core_instance->agent)) {
  2919. ast_log_dynamic_level(cc_logger_level, "Core %d: Invalid state change requested. Cannot go from %s to %s\n",
  2920. args->core_id, cc_state_to_string(core_instance->current_state), cc_state_to_string(args->state));
  2921. if (args->state == CC_CALLER_REQUESTED) {
  2922. /*
  2923. * For out-of-order requests, we need to let the requester know that
  2924. * we can't handle the request now.
  2925. */
  2926. core_instance->agent->callbacks->respond(core_instance->agent,
  2927. AST_CC_AGENT_RESPONSE_FAILURE_INVALID);
  2928. }
  2929. ast_free(args);
  2930. cc_unref(core_instance, "Unref core instance from when it was found earlier");
  2931. return -1;
  2932. }
  2933. /* We can change to the new state now. */
  2934. previous_state = core_instance->current_state;
  2935. core_instance->current_state = args->state;
  2936. res = state_change_funcs[core_instance->current_state](core_instance, args, previous_state);
  2937. /* If state change successful then notify any device state watchers of the change */
  2938. if (!res && !strcmp(core_instance->agent->callbacks->type, "generic")) {
  2939. ccss_notify_device_state_change(core_instance->agent->device_name, core_instance->current_state);
  2940. }
  2941. ast_free(args);
  2942. cc_unref(core_instance, "Unref since state change has completed"); /* From ao2_find */
  2943. return res;
  2944. }
  2945. static int cc_request_state_change(enum cc_state state, const int core_id, const char *debug, va_list ap)
  2946. {
  2947. int res;
  2948. int debuglen;
  2949. char dummy[1];
  2950. va_list aq;
  2951. struct cc_core_instance *core_instance;
  2952. struct cc_state_change_args *args;
  2953. /* This initial call to vsnprintf is simply to find what the
  2954. * size of the string needs to be
  2955. */
  2956. va_copy(aq, ap);
  2957. /* We add 1 to the result since vsnprintf's return does not
  2958. * include the terminating null byte
  2959. */
  2960. debuglen = vsnprintf(dummy, sizeof(dummy), debug, aq) + 1;
  2961. va_end(aq);
  2962. if (!(args = ast_calloc(1, sizeof(*args) + debuglen))) {
  2963. return -1;
  2964. }
  2965. core_instance = find_cc_core_instance(core_id);
  2966. if (!core_instance) {
  2967. ast_log_dynamic_level(cc_logger_level, "Core %d: Unable to find core instance.\n",
  2968. core_id);
  2969. ast_free(args);
  2970. return -1;
  2971. }
  2972. args->core_instance = core_instance;
  2973. args->state = state;
  2974. args->core_id = core_id;
  2975. vsnprintf(args->debug, debuglen, debug, ap);
  2976. res = ast_taskprocessor_push(cc_core_taskprocessor, cc_do_state_change, args);
  2977. if (res) {
  2978. cc_unref(core_instance, "Unref core instance. ast_taskprocessor_push failed");
  2979. ast_free(args);
  2980. }
  2981. return res;
  2982. }
  2983. struct cc_recall_ds_data {
  2984. int core_id;
  2985. char ignore;
  2986. char nested;
  2987. struct cc_monitor_tree *interface_tree;
  2988. };
  2989. static void *cc_recall_ds_duplicate(void *data)
  2990. {
  2991. struct cc_recall_ds_data *old_data = data;
  2992. struct cc_recall_ds_data *new_data = ast_calloc(1, sizeof(*new_data));
  2993. if (!new_data) {
  2994. return NULL;
  2995. }
  2996. new_data->interface_tree = cc_ref(old_data->interface_tree, "Bump refcount of monitor tree for recall datastore duplicate");
  2997. new_data->core_id = old_data->core_id;
  2998. new_data->nested = 1;
  2999. return new_data;
  3000. }
  3001. static void cc_recall_ds_destroy(void *data)
  3002. {
  3003. struct cc_recall_ds_data *recall_data = data;
  3004. recall_data->interface_tree = cc_unref(recall_data->interface_tree, "Unref recall monitor tree");
  3005. ast_free(recall_data);
  3006. }
  3007. static const struct ast_datastore_info recall_ds_info = {
  3008. .type = "cc_recall",
  3009. .duplicate = cc_recall_ds_duplicate,
  3010. .destroy = cc_recall_ds_destroy,
  3011. };
  3012. int ast_setup_cc_recall_datastore(struct ast_channel *chan, const int core_id)
  3013. {
  3014. struct ast_datastore *recall_datastore = ast_datastore_alloc(&recall_ds_info, NULL);
  3015. struct cc_recall_ds_data *recall_data;
  3016. struct cc_core_instance *core_instance;
  3017. if (!recall_datastore) {
  3018. return -1;
  3019. }
  3020. if (!(recall_data = ast_calloc(1, sizeof(*recall_data)))) {
  3021. ast_datastore_free(recall_datastore);
  3022. return -1;
  3023. }
  3024. if (!(core_instance = find_cc_core_instance(core_id))) {
  3025. ast_free(recall_data);
  3026. ast_datastore_free(recall_datastore);
  3027. return -1;
  3028. }
  3029. recall_data->interface_tree = cc_ref(core_instance->monitors,
  3030. "Bump refcount for monitor tree for recall datastore");
  3031. recall_data->core_id = core_id;
  3032. recall_datastore->data = recall_data;
  3033. recall_datastore->inheritance = DATASTORE_INHERIT_FOREVER;
  3034. ast_channel_lock(chan);
  3035. ast_channel_datastore_add(chan, recall_datastore);
  3036. ast_channel_unlock(chan);
  3037. cc_unref(core_instance, "Recall datastore set up. No need for core_instance ref");
  3038. return 0;
  3039. }
  3040. int ast_cc_is_recall(struct ast_channel *chan, int *core_id, const char * const monitor_type)
  3041. {
  3042. struct ast_datastore *recall_datastore;
  3043. struct cc_recall_ds_data *recall_data;
  3044. struct cc_monitor_tree *interface_tree;
  3045. char device_name[AST_CHANNEL_NAME];
  3046. struct ast_cc_monitor *device_monitor;
  3047. int core_id_candidate;
  3048. ast_assert(core_id != NULL);
  3049. *core_id = -1;
  3050. ast_channel_lock(chan);
  3051. if (!(recall_datastore = ast_channel_datastore_find(chan, &recall_ds_info, NULL))) {
  3052. /* Obviously not a recall if the datastore isn't present */
  3053. ast_channel_unlock(chan);
  3054. return 0;
  3055. }
  3056. recall_data = recall_datastore->data;
  3057. if (recall_data->ignore) {
  3058. /* Though this is a recall, the call to this particular interface is not part of the
  3059. * recall either because this is a call forward or because this is not the first
  3060. * invocation of Dial during this call
  3061. */
  3062. ast_channel_unlock(chan);
  3063. return 0;
  3064. }
  3065. if (!recall_data->nested) {
  3066. /* If the nested flag is not set, then this means that
  3067. * the channel passed to this function is the caller making
  3068. * the recall. This means that we shouldn't look through
  3069. * the monitor tree for the channel because it shouldn't be
  3070. * there. However, this is a recall though, so return true.
  3071. */
  3072. *core_id = recall_data->core_id;
  3073. ast_channel_unlock(chan);
  3074. return 1;
  3075. }
  3076. if (ast_strlen_zero(monitor_type)) {
  3077. /* If someone passed a NULL or empty monitor type, then it is clear
  3078. * the channel they passed in was an incoming channel, and so searching
  3079. * the list of dialed interfaces is not going to be helpful. Just return
  3080. * false immediately.
  3081. */
  3082. ast_channel_unlock(chan);
  3083. return 0;
  3084. }
  3085. interface_tree = recall_data->interface_tree;
  3086. ast_channel_get_device_name(chan, device_name, sizeof(device_name));
  3087. /* We grab the value of the recall_data->core_id so that we
  3088. * can unlock the channel before we start looking through the
  3089. * interface list. That way we don't have to worry about a possible
  3090. * clash between the channel lock and the monitor tree lock.
  3091. */
  3092. core_id_candidate = recall_data->core_id;
  3093. ast_channel_unlock(chan);
  3094. /*
  3095. * Now we need to find out if the channel device name
  3096. * is in the list of interfaces in the called tree.
  3097. */
  3098. AST_LIST_LOCK(interface_tree);
  3099. AST_LIST_TRAVERSE(interface_tree, device_monitor, next) {
  3100. if (!strcmp(device_monitor->interface->device_name, device_name) &&
  3101. !strcmp(device_monitor->interface->monitor_type, monitor_type)) {
  3102. /* BOOM! Device is in the tree! We have a winner! */
  3103. *core_id = core_id_candidate;
  3104. AST_LIST_UNLOCK(interface_tree);
  3105. return 1;
  3106. }
  3107. }
  3108. AST_LIST_UNLOCK(interface_tree);
  3109. return 0;
  3110. }
  3111. struct ast_cc_monitor *ast_cc_get_monitor_by_recall_core_id(const int core_id, const char * const device_name)
  3112. {
  3113. struct cc_core_instance *core_instance = find_cc_core_instance(core_id);
  3114. struct ast_cc_monitor *monitor_iter;
  3115. if (!core_instance) {
  3116. return NULL;
  3117. }
  3118. AST_LIST_LOCK(core_instance->monitors);
  3119. AST_LIST_TRAVERSE(core_instance->monitors, monitor_iter, next) {
  3120. if (!strcmp(monitor_iter->interface->device_name, device_name)) {
  3121. /* Found a monitor. */
  3122. cc_ref(monitor_iter, "Hand the requester of the monitor a reference");
  3123. break;
  3124. }
  3125. }
  3126. AST_LIST_UNLOCK(core_instance->monitors);
  3127. cc_unref(core_instance, "Done with core instance ref in ast_cc_get_monitor_by_recall_core_id");
  3128. return monitor_iter;
  3129. }
  3130. /*!
  3131. * \internal
  3132. * \brief uniquely append a dialstring to our CC_INTERFACES chanvar string.
  3133. *
  3134. * We will only append a string if it has not already appeared in our channel
  3135. * variable earlier. We ensure that we don't erroneously match substrings by
  3136. * adding an ampersand to the end of our potential dialstring and searching for
  3137. * it plus the ampersand in our variable.
  3138. *
  3139. * It's important to note that once we have built the full CC_INTERFACES string,
  3140. * there will be an extra ampersand at the end which must be stripped off by
  3141. * the caller of this function.
  3142. *
  3143. * \param str An ast_str holding what we will add to CC_INTERFACES
  3144. * \param dialstring A new dialstring to add
  3145. * \retval void
  3146. */
  3147. static void cc_unique_append(struct ast_str **str, const char *dialstring)
  3148. {
  3149. char dialstring_search[AST_CHANNEL_NAME];
  3150. if (ast_strlen_zero(dialstring)) {
  3151. /* No dialstring to append. */
  3152. return;
  3153. }
  3154. snprintf(dialstring_search, sizeof(dialstring_search), "%s%c", dialstring, '&');
  3155. if (strstr(ast_str_buffer(*str), dialstring_search)) {
  3156. return;
  3157. }
  3158. ast_str_append(str, 0, "%s", dialstring_search);
  3159. }
  3160. /*!
  3161. * \internal
  3162. * \brief Build the CC_INTERFACES channel variable
  3163. *
  3164. * The method used is to traverse the child dialstrings in the
  3165. * passed-in extension monitor, adding any that have the is_valid
  3166. * flag set. Then, traverse the monitors, finding all children
  3167. * of the starting extension monitor and adding their dialstrings
  3168. * as well.
  3169. *
  3170. * \param starting_point The extension monitor that is the parent to all
  3171. * monitors whose dialstrings should be added to CC_INTERFACES
  3172. * \param str Where we will store CC_INTERFACES
  3173. * \retval void
  3174. */
  3175. static void build_cc_interfaces_chanvar(struct ast_cc_monitor *starting_point, struct ast_str **str)
  3176. {
  3177. struct extension_monitor_pvt *extension_pvt;
  3178. struct extension_child_dialstring *child_dialstring;
  3179. struct ast_cc_monitor *monitor_iter = starting_point;
  3180. int top_level_id = starting_point->id;
  3181. size_t length;
  3182. /* Init to an empty string. */
  3183. ast_str_truncate(*str, 0);
  3184. /* First we need to take all of the is_valid child_dialstrings from
  3185. * the extension monitor we found and add them to the CC_INTERFACES
  3186. * chanvar
  3187. */
  3188. extension_pvt = starting_point->private_data;
  3189. AST_LIST_TRAVERSE(&extension_pvt->child_dialstrings, child_dialstring, next) {
  3190. if (child_dialstring->is_valid) {
  3191. cc_unique_append(str, child_dialstring->original_dialstring);
  3192. }
  3193. }
  3194. /* And now we get the dialstrings from each of the device monitors */
  3195. while ((monitor_iter = AST_LIST_NEXT(monitor_iter, next))) {
  3196. if (monitor_iter->parent_id == top_level_id) {
  3197. cc_unique_append(str, monitor_iter->dialstring);
  3198. }
  3199. }
  3200. /* str will have an extra '&' tacked onto the end of it, so we need
  3201. * to get rid of that.
  3202. */
  3203. length = ast_str_strlen(*str);
  3204. if (length) {
  3205. ast_str_truncate(*str, length - 1);
  3206. }
  3207. if (length <= 1) {
  3208. /* Nothing to recall? This should not happen. */
  3209. ast_log(LOG_ERROR, "CC_INTERFACES is empty. starting device_name:'%s'\n",
  3210. starting_point->interface->device_name);
  3211. }
  3212. }
  3213. int ast_cc_agent_set_interfaces_chanvar(struct ast_channel *chan)
  3214. {
  3215. struct ast_datastore *recall_datastore;
  3216. struct cc_monitor_tree *interface_tree;
  3217. struct ast_cc_monitor *monitor;
  3218. struct cc_recall_ds_data *recall_data;
  3219. struct ast_str *str = ast_str_create(64);
  3220. int core_id;
  3221. if (!str) {
  3222. return -1;
  3223. }
  3224. ast_channel_lock(chan);
  3225. if (!(recall_datastore = ast_channel_datastore_find(chan, &recall_ds_info, NULL))) {
  3226. ast_channel_unlock(chan);
  3227. ast_free(str);
  3228. return -1;
  3229. }
  3230. recall_data = recall_datastore->data;
  3231. interface_tree = recall_data->interface_tree;
  3232. core_id = recall_data->core_id;
  3233. ast_channel_unlock(chan);
  3234. AST_LIST_LOCK(interface_tree);
  3235. monitor = AST_LIST_FIRST(interface_tree);
  3236. build_cc_interfaces_chanvar(monitor, &str);
  3237. AST_LIST_UNLOCK(interface_tree);
  3238. pbx_builtin_setvar_helper(chan, "CC_INTERFACES", ast_str_buffer(str));
  3239. ast_log_dynamic_level(cc_logger_level, "Core %d: CC_INTERFACES set to %s\n",
  3240. core_id, ast_str_buffer(str));
  3241. ast_free(str);
  3242. return 0;
  3243. }
  3244. int ast_set_cc_interfaces_chanvar(struct ast_channel *chan, const char * const extension)
  3245. {
  3246. struct ast_datastore *recall_datastore;
  3247. struct cc_monitor_tree *interface_tree;
  3248. struct ast_cc_monitor *monitor_iter;
  3249. struct cc_recall_ds_data *recall_data;
  3250. struct ast_str *str = ast_str_create(64);
  3251. int core_id;
  3252. if (!str) {
  3253. return -1;
  3254. }
  3255. ast_channel_lock(chan);
  3256. if (!(recall_datastore = ast_channel_datastore_find(chan, &recall_ds_info, NULL))) {
  3257. ast_channel_unlock(chan);
  3258. ast_free(str);
  3259. return -1;
  3260. }
  3261. recall_data = recall_datastore->data;
  3262. interface_tree = recall_data->interface_tree;
  3263. core_id = recall_data->core_id;
  3264. ast_channel_unlock(chan);
  3265. AST_LIST_LOCK(interface_tree);
  3266. AST_LIST_TRAVERSE(interface_tree, monitor_iter, next) {
  3267. if (!strcmp(monitor_iter->interface->device_name, extension)) {
  3268. break;
  3269. }
  3270. }
  3271. if (!monitor_iter) {
  3272. /* We couldn't find this extension. This may be because
  3273. * we have been directed into an unexpected extension because
  3274. * the admin has changed a CC_INTERFACES variable at some point.
  3275. */
  3276. AST_LIST_UNLOCK(interface_tree);
  3277. ast_free(str);
  3278. return -1;
  3279. }
  3280. build_cc_interfaces_chanvar(monitor_iter, &str);
  3281. AST_LIST_UNLOCK(interface_tree);
  3282. pbx_builtin_setvar_helper(chan, "CC_INTERFACES", ast_str_buffer(str));
  3283. ast_log_dynamic_level(cc_logger_level, "Core %d: CC_INTERFACES set to %s\n",
  3284. core_id, ast_str_buffer(str));
  3285. ast_free(str);
  3286. return 0;
  3287. }
  3288. void ast_ignore_cc(struct ast_channel *chan)
  3289. {
  3290. struct ast_datastore *cc_datastore;
  3291. struct ast_datastore *cc_recall_datastore;
  3292. struct dialed_cc_interfaces *cc_interfaces;
  3293. struct cc_recall_ds_data *recall_cc_data;
  3294. ast_channel_lock(chan);
  3295. if ((cc_datastore = ast_channel_datastore_find(chan, &dialed_cc_interfaces_info, NULL))) {
  3296. cc_interfaces = cc_datastore->data;
  3297. cc_interfaces->ignore = 1;
  3298. }
  3299. if ((cc_recall_datastore = ast_channel_datastore_find(chan, &recall_ds_info, NULL))) {
  3300. recall_cc_data = cc_recall_datastore->data;
  3301. recall_cc_data->ignore = 1;
  3302. }
  3303. ast_channel_unlock(chan);
  3304. }
  3305. static __attribute__((format(printf, 2, 3))) int cc_offer(const int core_id, const char * const debug, ...)
  3306. {
  3307. va_list ap;
  3308. int res;
  3309. va_start(ap, debug);
  3310. res = cc_request_state_change(CC_CALLER_OFFERED, core_id, debug, ap);
  3311. va_end(ap);
  3312. return res;
  3313. }
  3314. int ast_cc_offer(struct ast_channel *caller_chan)
  3315. {
  3316. int core_id;
  3317. int res = -1;
  3318. struct ast_datastore *datastore;
  3319. struct dialed_cc_interfaces *cc_interfaces;
  3320. char cc_is_offerable;
  3321. ast_channel_lock(caller_chan);
  3322. if (!(datastore = ast_channel_datastore_find(caller_chan, &dialed_cc_interfaces_info, NULL))) {
  3323. ast_channel_unlock(caller_chan);
  3324. return res;
  3325. }
  3326. cc_interfaces = datastore->data;
  3327. cc_is_offerable = cc_interfaces->is_original_caller;
  3328. core_id = cc_interfaces->core_id;
  3329. ast_channel_unlock(caller_chan);
  3330. if (cc_is_offerable) {
  3331. res = cc_offer(core_id, "CC offered to caller %s", ast_channel_name(caller_chan));
  3332. }
  3333. return res;
  3334. }
  3335. int ast_cc_agent_accept_request(int core_id, const char * const debug, ...)
  3336. {
  3337. va_list ap;
  3338. int res;
  3339. va_start(ap, debug);
  3340. res = cc_request_state_change(CC_CALLER_REQUESTED, core_id, debug, ap);
  3341. va_end(ap);
  3342. return res;
  3343. }
  3344. int ast_cc_monitor_request_acked(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_ACTIVE, core_id, debug, ap);
  3350. va_end(ap);
  3351. return res;
  3352. }
  3353. int ast_cc_monitor_callee_available(const 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_CALLEE_READY, core_id, debug, ap);
  3359. va_end(ap);
  3360. return res;
  3361. }
  3362. int ast_cc_agent_caller_busy(int core_id, const char * debug, ...)
  3363. {
  3364. va_list ap;
  3365. int res;
  3366. va_start(ap, debug);
  3367. res = cc_request_state_change(CC_CALLER_BUSY, core_id, debug, ap);
  3368. va_end(ap);
  3369. return res;
  3370. }
  3371. int ast_cc_agent_caller_available(int core_id, const char * const debug, ...)
  3372. {
  3373. va_list ap;
  3374. int res;
  3375. va_start(ap, debug);
  3376. res = cc_request_state_change(CC_ACTIVE, core_id, debug, ap);
  3377. va_end(ap);
  3378. return res;
  3379. }
  3380. int ast_cc_agent_recalling(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_RECALLING, core_id, debug, ap);
  3386. va_end(ap);
  3387. return res;
  3388. }
  3389. int ast_cc_completed(struct ast_channel *chan, const char * const debug, ...)
  3390. {
  3391. struct ast_datastore *recall_datastore;
  3392. struct cc_recall_ds_data *recall_data;
  3393. int core_id;
  3394. va_list ap;
  3395. int res;
  3396. ast_channel_lock(chan);
  3397. if (!(recall_datastore = ast_channel_datastore_find(chan, &recall_ds_info, NULL))) {
  3398. /* Silly! Why did you call this function if there's no recall DS? */
  3399. ast_channel_unlock(chan);
  3400. return -1;
  3401. }
  3402. recall_data = recall_datastore->data;
  3403. if (recall_data->nested || recall_data->ignore) {
  3404. /* If this is being called from a nested Dial, it is too
  3405. * early to determine if the recall has actually completed.
  3406. * The outermost dial is the only one with the authority to
  3407. * declare the recall to be complete.
  3408. *
  3409. * Similarly, if this function has been called when the
  3410. * recall has progressed beyond the first dial, this is not
  3411. * a legitimate time to declare the recall to be done. In fact,
  3412. * that should have been done already.
  3413. */
  3414. ast_channel_unlock(chan);
  3415. return -1;
  3416. }
  3417. core_id = recall_data->core_id;
  3418. ast_channel_unlock(chan);
  3419. va_start(ap, debug);
  3420. res = cc_request_state_change(CC_COMPLETE, core_id, debug, ap);
  3421. va_end(ap);
  3422. return res;
  3423. }
  3424. int ast_cc_failed(int core_id, const char * const debug, ...)
  3425. {
  3426. va_list ap;
  3427. int res;
  3428. va_start(ap, debug);
  3429. res = cc_request_state_change(CC_FAILED, core_id, debug, ap);
  3430. va_end(ap);
  3431. return res;
  3432. }
  3433. struct ast_cc_monitor_failure_data {
  3434. const char *device_name;
  3435. char *debug;
  3436. int core_id;
  3437. };
  3438. static int cc_monitor_failed(void *data)
  3439. {
  3440. struct ast_cc_monitor_failure_data *failure_data = data;
  3441. struct cc_core_instance *core_instance;
  3442. struct ast_cc_monitor *monitor_iter;
  3443. core_instance = find_cc_core_instance(failure_data->core_id);
  3444. if (!core_instance) {
  3445. /* Core instance no longer exists or invalid core_id. */
  3446. ast_log_dynamic_level(cc_logger_level,
  3447. "Core %d: Could not find core instance for device %s '%s'\n",
  3448. failure_data->core_id, failure_data->device_name, failure_data->debug);
  3449. ast_free((char *) failure_data->device_name);
  3450. ast_free((char *) failure_data->debug);
  3451. ast_free(failure_data);
  3452. return -1;
  3453. }
  3454. AST_LIST_LOCK(core_instance->monitors);
  3455. AST_LIST_TRAVERSE_SAFE_BEGIN(core_instance->monitors, monitor_iter, next) {
  3456. if (monitor_iter->interface->monitor_class == AST_CC_DEVICE_MONITOR) {
  3457. if (!strcmp(monitor_iter->interface->device_name, failure_data->device_name)) {
  3458. AST_LIST_REMOVE_CURRENT(next);
  3459. cc_extension_monitor_change_is_valid(core_instance, monitor_iter->parent_id,
  3460. monitor_iter->interface->device_name, 1);
  3461. monitor_iter->callbacks->cancel_available_timer(monitor_iter, &monitor_iter->available_timer_id);
  3462. cc_publish_monitorfailed(monitor_iter->core_id, monitor_iter->interface->device_name);
  3463. cc_unref(monitor_iter, "Monitor reported failure. Unref list's reference.");
  3464. }
  3465. }
  3466. }
  3467. AST_LIST_TRAVERSE_SAFE_END;
  3468. if (!has_device_monitors(core_instance)) {
  3469. ast_cc_failed(core_instance->core_id, "All monitors have failed\n");
  3470. }
  3471. AST_LIST_UNLOCK(core_instance->monitors);
  3472. cc_unref(core_instance, "Finished with core_instance in cc_monitor_failed\n");
  3473. ast_free((char *) failure_data->device_name);
  3474. ast_free((char *) failure_data->debug);
  3475. ast_free(failure_data);
  3476. return 0;
  3477. }
  3478. int ast_cc_monitor_failed(int core_id, const char *const monitor_name, const char * const debug, ...)
  3479. {
  3480. struct ast_cc_monitor_failure_data *failure_data;
  3481. int res;
  3482. va_list ap;
  3483. if (!(failure_data = ast_calloc(1, sizeof(*failure_data)))) {
  3484. return -1;
  3485. }
  3486. if (!(failure_data->device_name = ast_strdup(monitor_name))) {
  3487. ast_free(failure_data);
  3488. return -1;
  3489. }
  3490. va_start(ap, debug);
  3491. if (ast_vasprintf(&failure_data->debug, debug, ap) == -1) {
  3492. va_end(ap);
  3493. ast_free((char *)failure_data->device_name);
  3494. ast_free(failure_data);
  3495. return -1;
  3496. }
  3497. va_end(ap);
  3498. failure_data->core_id = core_id;
  3499. res = ast_taskprocessor_push(cc_core_taskprocessor, cc_monitor_failed, failure_data);
  3500. if (res) {
  3501. ast_free((char *)failure_data->device_name);
  3502. ast_free((char *)failure_data->debug);
  3503. ast_free(failure_data);
  3504. }
  3505. return res;
  3506. }
  3507. static int cc_status_request(void *data)
  3508. {
  3509. struct cc_core_instance *core_instance= data;
  3510. int res;
  3511. res = core_instance->agent->callbacks->status_request(core_instance->agent);
  3512. cc_unref(core_instance, "Status request finished. Unref core instance");
  3513. return res;
  3514. }
  3515. int ast_cc_monitor_status_request(int core_id)
  3516. {
  3517. int res;
  3518. struct cc_core_instance *core_instance = find_cc_core_instance(core_id);
  3519. if (!core_instance) {
  3520. return -1;
  3521. }
  3522. res = ast_taskprocessor_push(cc_core_taskprocessor, cc_status_request, core_instance);
  3523. if (res) {
  3524. cc_unref(core_instance, "Unref core instance. ast_taskprocessor_push failed");
  3525. }
  3526. return res;
  3527. }
  3528. static int cc_stop_ringing(void *data)
  3529. {
  3530. struct cc_core_instance *core_instance = data;
  3531. int res = 0;
  3532. if (core_instance->agent->callbacks->stop_ringing) {
  3533. res = core_instance->agent->callbacks->stop_ringing(core_instance->agent);
  3534. }
  3535. /* If an agent is being asked to stop ringing, then he needs to be prepared if for
  3536. * whatever reason he needs to be called back again. The proper state to be in to
  3537. * detect such a circumstance is the CC_ACTIVE state.
  3538. *
  3539. * We get to this state using the slightly unintuitive method of calling
  3540. * ast_cc_monitor_request_acked because it gets us to the proper state.
  3541. */
  3542. ast_cc_monitor_request_acked(core_instance->core_id, "Agent %s asked to stop ringing. Be prepared to be recalled again.",
  3543. core_instance->agent->device_name);
  3544. cc_unref(core_instance, "Stop ringing finished. Unref core_instance");
  3545. return res;
  3546. }
  3547. int ast_cc_monitor_stop_ringing(int core_id)
  3548. {
  3549. int res;
  3550. struct cc_core_instance *core_instance = find_cc_core_instance(core_id);
  3551. if (!core_instance) {
  3552. return -1;
  3553. }
  3554. res = ast_taskprocessor_push(cc_core_taskprocessor, cc_stop_ringing, core_instance);
  3555. if (res) {
  3556. cc_unref(core_instance, "Unref core instance. ast_taskprocessor_push failed");
  3557. }
  3558. return res;
  3559. }
  3560. static int cc_party_b_free(void *data)
  3561. {
  3562. struct cc_core_instance *core_instance = data;
  3563. int res = 0;
  3564. if (core_instance->agent->callbacks->party_b_free) {
  3565. res = core_instance->agent->callbacks->party_b_free(core_instance->agent);
  3566. }
  3567. cc_unref(core_instance, "Party B free finished. Unref core_instance");
  3568. return res;
  3569. }
  3570. int ast_cc_monitor_party_b_free(int core_id)
  3571. {
  3572. int res;
  3573. struct cc_core_instance *core_instance = find_cc_core_instance(core_id);
  3574. if (!core_instance) {
  3575. return -1;
  3576. }
  3577. res = ast_taskprocessor_push(cc_core_taskprocessor, cc_party_b_free, core_instance);
  3578. if (res) {
  3579. cc_unref(core_instance, "Unref core instance. ast_taskprocessor_push failed");
  3580. }
  3581. return res;
  3582. }
  3583. struct cc_status_response_args {
  3584. struct cc_core_instance *core_instance;
  3585. enum ast_device_state devstate;
  3586. };
  3587. static int cc_status_response(void *data)
  3588. {
  3589. struct cc_status_response_args *args = data;
  3590. struct cc_core_instance *core_instance = args->core_instance;
  3591. struct ast_cc_monitor *monitor_iter;
  3592. enum ast_device_state devstate = args->devstate;
  3593. ast_free(args);
  3594. AST_LIST_LOCK(core_instance->monitors);
  3595. AST_LIST_TRAVERSE(core_instance->monitors, monitor_iter, next) {
  3596. if (monitor_iter->interface->monitor_class == AST_CC_DEVICE_MONITOR &&
  3597. monitor_iter->callbacks->status_response) {
  3598. monitor_iter->callbacks->status_response(monitor_iter, devstate);
  3599. }
  3600. }
  3601. AST_LIST_UNLOCK(core_instance->monitors);
  3602. cc_unref(core_instance, "Status response finished. Unref core instance");
  3603. return 0;
  3604. }
  3605. int ast_cc_agent_status_response(int core_id, enum ast_device_state devstate)
  3606. {
  3607. struct cc_status_response_args *args;
  3608. struct cc_core_instance *core_instance;
  3609. int res;
  3610. args = ast_calloc(1, sizeof(*args));
  3611. if (!args) {
  3612. return -1;
  3613. }
  3614. core_instance = find_cc_core_instance(core_id);
  3615. if (!core_instance) {
  3616. ast_free(args);
  3617. return -1;
  3618. }
  3619. args->core_instance = core_instance;
  3620. args->devstate = devstate;
  3621. res = ast_taskprocessor_push(cc_core_taskprocessor, cc_status_response, args);
  3622. if (res) {
  3623. cc_unref(core_instance, "Unref core instance. ast_taskprocessor_push failed");
  3624. ast_free(args);
  3625. }
  3626. return res;
  3627. }
  3628. static int cc_build_payload(struct ast_channel *chan, struct ast_cc_config_params *cc_params,
  3629. const char *monitor_type, const char * const device_name, const char * dialstring,
  3630. enum ast_cc_service_type service, void *private_data, struct cc_control_payload *payload)
  3631. {
  3632. struct ast_datastore *datastore;
  3633. struct dialed_cc_interfaces *cc_interfaces;
  3634. int dial_parent_id;
  3635. ast_channel_lock(chan);
  3636. datastore = ast_channel_datastore_find(chan, &dialed_cc_interfaces_info, NULL);
  3637. if (!datastore) {
  3638. ast_channel_unlock(chan);
  3639. return -1;
  3640. }
  3641. cc_interfaces = datastore->data;
  3642. dial_parent_id = cc_interfaces->dial_parent_id;
  3643. ast_channel_unlock(chan);
  3644. payload->monitor_type = monitor_type;
  3645. payload->private_data = private_data;
  3646. payload->service = service;
  3647. ast_cc_copy_config_params(&payload->config_params, cc_params);
  3648. payload->parent_interface_id = dial_parent_id;
  3649. ast_copy_string(payload->device_name, device_name, sizeof(payload->device_name));
  3650. ast_copy_string(payload->dialstring, dialstring, sizeof(payload->dialstring));
  3651. return 0;
  3652. }
  3653. int ast_queue_cc_frame(struct ast_channel *chan, const char *monitor_type,
  3654. const char * const dialstring, enum ast_cc_service_type service, void *private_data)
  3655. {
  3656. struct ast_frame frame = {0,};
  3657. char device_name[AST_CHANNEL_NAME];
  3658. int retval;
  3659. struct ast_cc_config_params *cc_params;
  3660. cc_params = ast_channel_get_cc_config_params(chan);
  3661. if (!cc_params) {
  3662. return -1;
  3663. }
  3664. ast_channel_get_device_name(chan, device_name, sizeof(device_name));
  3665. if (ast_cc_monitor_count(device_name, monitor_type) >= ast_get_cc_max_monitors(cc_params)) {
  3666. ast_log(LOG_NOTICE, "Not queuing a CC frame for device %s since it already has its maximum monitors allocated\n", device_name);
  3667. return -1;
  3668. }
  3669. if (ast_cc_build_frame(chan, cc_params, monitor_type, device_name, dialstring, service, private_data, &frame)) {
  3670. /* Frame building failed. We can't use this. */
  3671. return -1;
  3672. }
  3673. retval = ast_queue_frame(chan, &frame);
  3674. ast_frfree(&frame);
  3675. return retval;
  3676. }
  3677. int ast_cc_build_frame(struct ast_channel *chan, struct ast_cc_config_params *cc_params,
  3678. const char *monitor_type, const char * const device_name,
  3679. const char * const dialstring, enum ast_cc_service_type service, void *private_data,
  3680. struct ast_frame *frame)
  3681. {
  3682. struct cc_control_payload *payload = ast_calloc(1, sizeof(*payload));
  3683. if (!payload) {
  3684. return -1;
  3685. }
  3686. if (cc_build_payload(chan, cc_params, monitor_type, device_name, dialstring, service, private_data, payload)) {
  3687. /* Something screwed up, we can't make a frame with this */
  3688. ast_free(payload);
  3689. return -1;
  3690. }
  3691. frame->frametype = AST_FRAME_CONTROL;
  3692. frame->subclass.integer = AST_CONTROL_CC;
  3693. frame->data.ptr = payload;
  3694. frame->datalen = sizeof(*payload);
  3695. frame->mallocd = AST_MALLOCD_DATA;
  3696. return 0;
  3697. }
  3698. void ast_cc_call_failed(struct ast_channel *incoming, struct ast_channel *outgoing, const char * const dialstring)
  3699. {
  3700. char device_name[AST_CHANNEL_NAME];
  3701. struct cc_control_payload payload;
  3702. struct ast_cc_config_params *cc_params;
  3703. if (ast_channel_hangupcause(outgoing) != AST_CAUSE_BUSY && ast_channel_hangupcause(outgoing) != AST_CAUSE_CONGESTION) {
  3704. /* It doesn't make sense to try to offer CCBS to the caller if the reason for ast_call
  3705. * failing is something other than busy or congestion
  3706. */
  3707. return;
  3708. }
  3709. cc_params = ast_channel_get_cc_config_params(outgoing);
  3710. if (!cc_params) {
  3711. return;
  3712. }
  3713. if (ast_get_cc_monitor_policy(cc_params) != AST_CC_MONITOR_GENERIC) {
  3714. /* This sort of CCBS only works if using generic CC. For native, we would end up sending
  3715. * a CC request for a non-existent call. The far end will reject this every time
  3716. */
  3717. return;
  3718. }
  3719. ast_channel_get_device_name(outgoing, device_name, sizeof(device_name));
  3720. if (cc_build_payload(outgoing, cc_params, AST_CC_GENERIC_MONITOR_TYPE, device_name,
  3721. dialstring, AST_CC_CCBS, NULL, &payload)) {
  3722. /* Something screwed up, we can't make a frame with this */
  3723. return;
  3724. }
  3725. ast_handle_cc_control_frame(incoming, outgoing, &payload);
  3726. }
  3727. void ast_cc_busy_interface(struct ast_channel *inbound, struct ast_cc_config_params *cc_params,
  3728. const char *monitor_type, const char * const device_name, const char * const dialstring, void *private_data)
  3729. {
  3730. struct cc_control_payload payload;
  3731. if (cc_build_payload(inbound, cc_params, monitor_type, device_name, dialstring, AST_CC_CCBS, private_data, &payload)) {
  3732. /* Something screwed up. Don't try to handle this payload */
  3733. call_destructor_with_no_monitor(monitor_type, private_data);
  3734. return;
  3735. }
  3736. ast_handle_cc_control_frame(inbound, NULL, &payload);
  3737. }
  3738. int ast_cc_callback(struct ast_channel *inbound, const char * const tech, const char * const dest, ast_cc_callback_fn callback)
  3739. {
  3740. const struct ast_channel_tech *chantech = ast_get_channel_tech(tech);
  3741. if (chantech && chantech->cc_callback) {
  3742. chantech->cc_callback(inbound, dest, callback);
  3743. }
  3744. return 0;
  3745. }
  3746. static const char *ccreq_app = "CallCompletionRequest";
  3747. static int ccreq_exec(struct ast_channel *chan, const char *data)
  3748. {
  3749. struct cc_core_instance *core_instance;
  3750. char device_name[AST_CHANNEL_NAME];
  3751. unsigned long match_flags;
  3752. int res;
  3753. ast_channel_get_device_name(chan, device_name, sizeof(device_name));
  3754. match_flags = MATCH_NO_REQUEST;
  3755. if (!(core_instance = ao2_t_callback_data(cc_core_instances, 0, match_agent, device_name, &match_flags, "Find core instance for CallCompletionRequest"))) {
  3756. ast_log_dynamic_level(cc_logger_level, "Couldn't find a core instance for caller %s\n", device_name);
  3757. pbx_builtin_setvar_helper(chan, "CC_REQUEST_RESULT", "FAIL");
  3758. pbx_builtin_setvar_helper(chan, "CC_REQUEST_REASON", "NO_CORE_INSTANCE");
  3759. return 0;
  3760. }
  3761. ast_log_dynamic_level(cc_logger_level, "Core %d: Found core_instance for caller %s\n",
  3762. core_instance->core_id, device_name);
  3763. if (strcmp(core_instance->agent->callbacks->type, "generic")) {
  3764. ast_log_dynamic_level(cc_logger_level, "Core %d: CallCompletionRequest is only for generic agent types.\n",
  3765. core_instance->core_id);
  3766. pbx_builtin_setvar_helper(chan, "CC_REQUEST_RESULT", "FAIL");
  3767. pbx_builtin_setvar_helper(chan, "CC_REQUEST_REASON", "NOT_GENERIC");
  3768. cc_unref(core_instance, "Unref core_instance since CallCompletionRequest was called with native agent");
  3769. return 0;
  3770. }
  3771. if (!ast_cc_request_is_within_limits()) {
  3772. ast_log_dynamic_level(cc_logger_level, "Core %d: CallCompletionRequest failed. Too many requests in the system\n",
  3773. core_instance->core_id);
  3774. ast_cc_failed(core_instance->core_id, "Too many CC requests\n");
  3775. pbx_builtin_setvar_helper(chan, "CC_REQUEST_RESULT", "FAIL");
  3776. pbx_builtin_setvar_helper(chan, "CC_REQUEST_REASON", "TOO_MANY_REQUESTS");
  3777. cc_unref(core_instance, "Unref core_instance since too many CC requests");
  3778. return 0;
  3779. }
  3780. 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);
  3781. pbx_builtin_setvar_helper(chan, "CC_REQUEST_RESULT", res ? "FAIL" : "SUCCESS");
  3782. if (res) {
  3783. pbx_builtin_setvar_helper(chan, "CC_REQUEST_REASON", "UNSPECIFIED");
  3784. }
  3785. cc_unref(core_instance, "Done with CallCompletionRequest");
  3786. return 0;
  3787. }
  3788. static const char *cccancel_app = "CallCompletionCancel";
  3789. static int cccancel_exec(struct ast_channel *chan, const char *data)
  3790. {
  3791. struct cc_core_instance *core_instance;
  3792. char device_name[AST_CHANNEL_NAME];
  3793. unsigned long match_flags;
  3794. int res;
  3795. ast_channel_get_device_name(chan, device_name, sizeof(device_name));
  3796. match_flags = MATCH_REQUEST;
  3797. if (!(core_instance = ao2_t_callback_data(cc_core_instances, 0, match_agent, device_name, &match_flags, "Find core instance for CallCompletionCancel"))) {
  3798. ast_log_dynamic_level(cc_logger_level, "Cannot find CC transaction to cancel for caller %s\n", device_name);
  3799. pbx_builtin_setvar_helper(chan, "CC_CANCEL_RESULT", "FAIL");
  3800. pbx_builtin_setvar_helper(chan, "CC_CANCEL_REASON", "NO_CORE_INSTANCE");
  3801. return 0;
  3802. }
  3803. if (strcmp(core_instance->agent->callbacks->type, "generic")) {
  3804. ast_log(LOG_WARNING, "CallCompletionCancel may only be used for calles with a generic agent\n");
  3805. cc_unref(core_instance, "Unref core instance found during CallCompletionCancel");
  3806. pbx_builtin_setvar_helper(chan, "CC_CANCEL_RESULT", "FAIL");
  3807. pbx_builtin_setvar_helper(chan, "CC_CANCEL_REASON", "NOT_GENERIC");
  3808. return 0;
  3809. }
  3810. res = ast_cc_failed(core_instance->core_id, "Call completion request Cancelled for core ID %d by caller %s",
  3811. core_instance->core_id, device_name);
  3812. cc_unref(core_instance, "Unref core instance found during CallCompletionCancel");
  3813. pbx_builtin_setvar_helper(chan, "CC_CANCEL_RESULT", res ? "FAIL" : "SUCCESS");
  3814. if (res) {
  3815. pbx_builtin_setvar_helper(chan, "CC_CANCEL_REASON", "UNSPECIFIED");
  3816. }
  3817. return 0;
  3818. }
  3819. struct count_monitors_cb_data {
  3820. const char *device_name;
  3821. const char *monitor_type;
  3822. int count;
  3823. };
  3824. static int count_monitors_cb(void *obj, void *arg, int flags)
  3825. {
  3826. struct cc_core_instance *core_instance = obj;
  3827. struct count_monitors_cb_data *cb_data = arg;
  3828. const char *device_name = cb_data->device_name;
  3829. const char *monitor_type = cb_data->monitor_type;
  3830. struct ast_cc_monitor *monitor_iter;
  3831. AST_LIST_LOCK(core_instance->monitors);
  3832. AST_LIST_TRAVERSE(core_instance->monitors, monitor_iter, next) {
  3833. if (!strcmp(monitor_iter->interface->device_name, device_name) &&
  3834. !strcmp(monitor_iter->interface->monitor_type, monitor_type)) {
  3835. cb_data->count++;
  3836. break;
  3837. }
  3838. }
  3839. AST_LIST_UNLOCK(core_instance->monitors);
  3840. return 0;
  3841. }
  3842. int ast_cc_monitor_count(const char * const name, const char * const type)
  3843. {
  3844. struct count_monitors_cb_data data = {.device_name = name, .monitor_type = type,};
  3845. ao2_t_callback(cc_core_instances, OBJ_NODATA, count_monitors_cb, &data, "Counting agents");
  3846. ast_log_dynamic_level(cc_logger_level, "Counted %d monitors\n", data.count);
  3847. return data.count;
  3848. }
  3849. static void initialize_cc_max_requests(void)
  3850. {
  3851. struct ast_config *cc_config;
  3852. const char *cc_max_requests_str;
  3853. struct ast_flags config_flags = {0,};
  3854. char *endptr;
  3855. cc_config = ast_config_load2("ccss.conf", "ccss", config_flags);
  3856. if (!cc_config || cc_config == CONFIG_STATUS_FILEINVALID) {
  3857. ast_log(LOG_WARNING, "Could not find valid ccss.conf file. Using cc_max_requests default\n");
  3858. global_cc_max_requests = GLOBAL_CC_MAX_REQUESTS_DEFAULT;
  3859. return;
  3860. }
  3861. if (!(cc_max_requests_str = ast_variable_retrieve(cc_config, "general", "cc_max_requests"))) {
  3862. ast_config_destroy(cc_config);
  3863. global_cc_max_requests = GLOBAL_CC_MAX_REQUESTS_DEFAULT;
  3864. return;
  3865. }
  3866. global_cc_max_requests = strtol(cc_max_requests_str, &endptr, 10);
  3867. if (!ast_strlen_zero(endptr)) {
  3868. ast_log(LOG_WARNING, "Invalid input given for cc_max_requests. Using default\n");
  3869. global_cc_max_requests = GLOBAL_CC_MAX_REQUESTS_DEFAULT;
  3870. }
  3871. ast_config_destroy(cc_config);
  3872. return;
  3873. }
  3874. /*!
  3875. * \internal
  3876. * \brief helper function to parse and configure each devstate map
  3877. */
  3878. static void initialize_cc_devstate_map_helper(struct ast_config *cc_config, enum cc_state state, const char *cc_setting)
  3879. {
  3880. const char *cc_devstate_str;
  3881. enum ast_device_state this_devstate;
  3882. if ((cc_devstate_str = ast_variable_retrieve(cc_config, "general", cc_setting))) {
  3883. this_devstate = ast_devstate_val(cc_devstate_str);
  3884. if (this_devstate != AST_DEVICE_UNKNOWN) {
  3885. cc_state_to_devstate_map[state] = this_devstate;
  3886. }
  3887. }
  3888. }
  3889. /*!
  3890. * \internal
  3891. * \brief initializes cc_state_to_devstate_map from ccss.conf
  3892. *
  3893. * \details
  3894. * The cc_state_to_devstate_map[] is already initialized with all the
  3895. * default values. This will update that structure with any changes
  3896. * from the ccss.conf file. The configuration parameters in ccss.conf
  3897. * should use any valid device state form that is recognized by
  3898. * ast_devstate_val() function.
  3899. */
  3900. static void initialize_cc_devstate_map(void)
  3901. {
  3902. struct ast_config *cc_config;
  3903. struct ast_flags config_flags = { 0, };
  3904. cc_config = ast_config_load2("ccss.conf", "ccss", config_flags);
  3905. if (!cc_config || cc_config == CONFIG_STATUS_FILEINVALID) {
  3906. ast_log(LOG_WARNING,
  3907. "Could not find valid ccss.conf file. Using cc_[state]_devstate defaults\n");
  3908. return;
  3909. }
  3910. initialize_cc_devstate_map_helper(cc_config, CC_AVAILABLE, "cc_available_devstate");
  3911. initialize_cc_devstate_map_helper(cc_config, CC_CALLER_OFFERED, "cc_caller_offered_devstate");
  3912. initialize_cc_devstate_map_helper(cc_config, CC_CALLER_REQUESTED, "cc_caller_requested_devstate");
  3913. initialize_cc_devstate_map_helper(cc_config, CC_ACTIVE, "cc_active_devstate");
  3914. initialize_cc_devstate_map_helper(cc_config, CC_CALLEE_READY, "cc_callee_ready_devstate");
  3915. initialize_cc_devstate_map_helper(cc_config, CC_CALLER_BUSY, "cc_caller_busy_devstate");
  3916. initialize_cc_devstate_map_helper(cc_config, CC_RECALLING, "cc_recalling_devstate");
  3917. initialize_cc_devstate_map_helper(cc_config, CC_COMPLETE, "cc_complete_devstate");
  3918. initialize_cc_devstate_map_helper(cc_config, CC_FAILED, "cc_failed_devstate");
  3919. ast_config_destroy(cc_config);
  3920. }
  3921. static void cc_cli_print_monitor_stats(struct ast_cc_monitor *monitor, int fd, int parent_id)
  3922. {
  3923. struct ast_cc_monitor *child_monitor_iter = monitor;
  3924. if (!monitor) {
  3925. return;
  3926. }
  3927. ast_cli(fd, "\t\t|-->%s", monitor->interface->device_name);
  3928. if (monitor->interface->monitor_class == AST_CC_DEVICE_MONITOR) {
  3929. ast_cli(fd, "(%s)", cc_service_to_string(monitor->service_offered));
  3930. }
  3931. ast_cli(fd, "\n");
  3932. while ((child_monitor_iter = AST_LIST_NEXT(child_monitor_iter, next))) {
  3933. if (child_monitor_iter->parent_id == monitor->id) {
  3934. cc_cli_print_monitor_stats(child_monitor_iter, fd, child_monitor_iter->id);
  3935. }
  3936. }
  3937. }
  3938. static int print_stats_cb(void *obj, void *arg, int flags)
  3939. {
  3940. int *cli_fd = arg;
  3941. struct cc_core_instance *core_instance = obj;
  3942. ast_cli(*cli_fd, "%d\t\t%s\t\t%s\n", core_instance->core_id, core_instance->agent->device_name,
  3943. cc_state_to_string(core_instance->current_state));
  3944. AST_LIST_LOCK(core_instance->monitors);
  3945. cc_cli_print_monitor_stats(AST_LIST_FIRST(core_instance->monitors), *cli_fd, 0);
  3946. AST_LIST_UNLOCK(core_instance->monitors);
  3947. return 0;
  3948. }
  3949. static int cc_cli_output_status(void *data)
  3950. {
  3951. int *cli_fd = data;
  3952. int count = ao2_container_count(cc_core_instances);
  3953. if (!count) {
  3954. ast_cli(*cli_fd, "There are currently no active call completion transactions\n");
  3955. } else {
  3956. ast_cli(*cli_fd, "%d Call completion transactions\n", count);
  3957. ast_cli(*cli_fd, "Core ID\t\tCaller\t\t\t\tStatus\n");
  3958. ast_cli(*cli_fd, "----------------------------------------------------------------------------\n");
  3959. ao2_t_callback(cc_core_instances, OBJ_NODATA, print_stats_cb, cli_fd, "Printing stats to CLI");
  3960. }
  3961. ast_free(cli_fd);
  3962. return 0;
  3963. }
  3964. static char *handle_cc_status(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
  3965. {
  3966. int *cli_fd;
  3967. switch (cmd) {
  3968. case CLI_INIT:
  3969. e->command = "cc report status";
  3970. e->usage =
  3971. "Usage: cc report status\n"
  3972. " Report the current status of any ongoing CC transactions\n";
  3973. return NULL;
  3974. case CLI_GENERATE:
  3975. return NULL;
  3976. }
  3977. if (a->argc != 3) {
  3978. return CLI_SHOWUSAGE;
  3979. }
  3980. cli_fd = ast_malloc(sizeof(*cli_fd));
  3981. if (!cli_fd) {
  3982. return CLI_FAILURE;
  3983. }
  3984. *cli_fd = a->fd;
  3985. if (ast_taskprocessor_push(cc_core_taskprocessor, cc_cli_output_status, cli_fd)) {
  3986. ast_free(cli_fd);
  3987. return CLI_FAILURE;
  3988. }
  3989. return CLI_SUCCESS;
  3990. }
  3991. static int kill_cores(void *obj, void *arg, int flags)
  3992. {
  3993. int *core_id = arg;
  3994. struct cc_core_instance *core_instance = obj;
  3995. if (!core_id || (core_instance->core_id == *core_id)) {
  3996. ast_cc_failed(core_instance->core_id, "CC transaction canceled administratively\n");
  3997. }
  3998. return 0;
  3999. }
  4000. static char *complete_core_id(const char *line, const char *word, int pos, int state)
  4001. {
  4002. int which = 0;
  4003. int wordlen = strlen(word);
  4004. char *ret = NULL;
  4005. struct ao2_iterator core_iter = ao2_iterator_init(cc_core_instances, 0);
  4006. struct cc_core_instance *core_instance;
  4007. for (; (core_instance = ao2_t_iterator_next(&core_iter, "Next core instance"));
  4008. cc_unref(core_instance, "CLI tab completion iteration")) {
  4009. char core_id_str[20];
  4010. snprintf(core_id_str, sizeof(core_id_str), "%d", core_instance->core_id);
  4011. if (!strncmp(word, core_id_str, wordlen) && ++which > state) {
  4012. ret = ast_strdup(core_id_str);
  4013. cc_unref(core_instance, "Found a matching core ID for CLI tab-completion");
  4014. break;
  4015. }
  4016. }
  4017. ao2_iterator_destroy(&core_iter);
  4018. return ret;
  4019. }
  4020. static char *handle_cc_kill(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
  4021. {
  4022. static const char * const option[] = { "core", "all", NULL };
  4023. switch (cmd) {
  4024. case CLI_INIT:
  4025. e->command = "cc cancel";
  4026. e->usage =
  4027. "Usage: cc cancel can be used in two ways.\n"
  4028. " 1. 'cc cancel core [core ID]' will cancel the CC transaction with\n"
  4029. " core ID equal to the specified core ID.\n"
  4030. " 2. 'cc cancel all' will cancel all active CC transactions.\n";
  4031. return NULL;
  4032. case CLI_GENERATE:
  4033. if (a->pos == 2) {
  4034. return ast_cli_complete(a->word, option, a->n);
  4035. }
  4036. if (a->pos == 3) {
  4037. return complete_core_id(a->line, a->word, a->pos, a->n);
  4038. }
  4039. return NULL;
  4040. }
  4041. if (a->argc == 4) {
  4042. int core_id;
  4043. char *endptr;
  4044. if (strcasecmp(a->argv[2], "core")) {
  4045. return CLI_SHOWUSAGE;
  4046. }
  4047. core_id = strtol(a->argv[3], &endptr, 10);
  4048. if ((errno != 0 && core_id == 0) || (endptr == a->argv[3])) {
  4049. return CLI_SHOWUSAGE;
  4050. }
  4051. ao2_t_callback(cc_core_instances, OBJ_NODATA, kill_cores, &core_id, "CLI Killing Core Id");
  4052. } else if (a->argc == 3) {
  4053. if (strcasecmp(a->argv[2], "all")) {
  4054. return CLI_SHOWUSAGE;
  4055. }
  4056. ao2_t_callback(cc_core_instances, OBJ_NODATA, kill_cores, NULL, "CLI Killing all CC cores");
  4057. } else {
  4058. return CLI_SHOWUSAGE;
  4059. }
  4060. return CLI_SUCCESS;
  4061. }
  4062. static struct ast_cli_entry cc_cli[] = {
  4063. AST_CLI_DEFINE(handle_cc_status, "Reports CC stats"),
  4064. AST_CLI_DEFINE(handle_cc_kill, "Kill a CC transaction"),
  4065. };
  4066. static void cc_shutdown(void)
  4067. {
  4068. ast_devstate_prov_del("ccss");
  4069. ast_cc_agent_unregister(&generic_agent_callbacks);
  4070. ast_cc_monitor_unregister(&generic_monitor_cbs);
  4071. ast_unregister_application(cccancel_app);
  4072. ast_unregister_application(ccreq_app);
  4073. ast_logger_unregister_level(CC_LOGGER_LEVEL_NAME);
  4074. ast_cli_unregister_multiple(cc_cli, ARRAY_LEN(cc_cli));
  4075. if (cc_sched_context) {
  4076. ast_sched_context_destroy(cc_sched_context);
  4077. cc_sched_context = NULL;
  4078. }
  4079. if (cc_core_taskprocessor) {
  4080. cc_core_taskprocessor = ast_taskprocessor_unreference(cc_core_taskprocessor);
  4081. }
  4082. /* Note that core instances must be destroyed prior to the generic_monitors */
  4083. if (cc_core_instances) {
  4084. ao2_t_ref(cc_core_instances, -1, "Unref cc_core_instances container in cc_shutdown");
  4085. cc_core_instances = NULL;
  4086. }
  4087. if (generic_monitors) {
  4088. ao2_t_ref(generic_monitors, -1, "Unref generic_monitor container in cc_shutdown");
  4089. generic_monitors = NULL;
  4090. }
  4091. }
  4092. int ast_cc_init(void)
  4093. {
  4094. int res;
  4095. if (!(cc_core_instances = ao2_t_container_alloc(CC_CORE_INSTANCES_BUCKETS,
  4096. cc_core_instance_hash_fn, cc_core_instance_cmp_fn,
  4097. "Create core instance container"))) {
  4098. return -1;
  4099. }
  4100. if (!(generic_monitors = ao2_t_container_alloc(CC_CORE_INSTANCES_BUCKETS,
  4101. generic_monitor_hash_fn, generic_monitor_cmp_fn,
  4102. "Create generic monitor container"))) {
  4103. return -1;
  4104. }
  4105. if (!(cc_core_taskprocessor = ast_taskprocessor_get("CCSS core", TPS_REF_DEFAULT))) {
  4106. return -1;
  4107. }
  4108. if (!(cc_sched_context = ast_sched_context_create())) {
  4109. return -1;
  4110. }
  4111. if (ast_sched_start_thread(cc_sched_context)) {
  4112. return -1;
  4113. }
  4114. res = ast_register_application2(ccreq_app, ccreq_exec, NULL, NULL, NULL);
  4115. res |= ast_register_application2(cccancel_app, cccancel_exec, NULL, NULL, NULL);
  4116. res |= ast_cc_monitor_register(&generic_monitor_cbs);
  4117. res |= ast_cc_agent_register(&generic_agent_callbacks);
  4118. ast_cli_register_multiple(cc_cli, ARRAY_LEN(cc_cli));
  4119. cc_logger_level = ast_logger_register_level(CC_LOGGER_LEVEL_NAME);
  4120. dialed_cc_interface_counter = 1;
  4121. initialize_cc_max_requests();
  4122. /* Read the map and register the device state callback for generic agents */
  4123. initialize_cc_devstate_map();
  4124. res |= ast_devstate_prov_add("ccss", ccss_device_state);
  4125. ast_register_cleanup(cc_shutdown);
  4126. return res;
  4127. }