ccss.c 151 KB

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