bridge_basic.c 120 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708
  1. /*
  2. * Asterisk -- An open source telephony toolkit.
  3. *
  4. * Copyright (C) 2013 Digium, Inc.
  5. *
  6. * Richard Mudgett <rmudgett@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. /*!
  19. * \file
  20. * \brief Basic bridge class. It is a subclass of struct ast_bridge.
  21. *
  22. * \author Richard Mudgett <rmudgett@digium.com>
  23. *
  24. * See Also:
  25. * \arg \ref AstCREDITS
  26. */
  27. #include "asterisk.h"
  28. #include "asterisk/channel.h"
  29. #include "asterisk/utils.h"
  30. #include "asterisk/linkedlists.h"
  31. #include "asterisk/bridge.h"
  32. #include "asterisk/bridge_internal.h"
  33. #include "asterisk/bridge_basic.h"
  34. #include "asterisk/bridge_after.h"
  35. #include "asterisk/astobj2.h"
  36. #include "asterisk/features_config.h"
  37. #include "asterisk/pbx.h"
  38. #include "asterisk/file.h"
  39. #include "asterisk/app.h"
  40. #include "asterisk/dial.h"
  41. #include "asterisk/stasis_bridges.h"
  42. #include "asterisk/stasis_channels.h"
  43. #include "asterisk/features.h"
  44. #include "asterisk/format_cache.h"
  45. #include "asterisk/test.h"
  46. #define NORMAL_FLAGS (AST_BRIDGE_FLAG_DISSOLVE_HANGUP | AST_BRIDGE_FLAG_DISSOLVE_EMPTY \
  47. | AST_BRIDGE_FLAG_SMART)
  48. #define TRANSFER_FLAGS AST_BRIDGE_FLAG_SMART
  49. struct attended_transfer_properties;
  50. enum bridge_basic_personality_type {
  51. /*! Index for "normal" basic bridge personality */
  52. BRIDGE_BASIC_PERSONALITY_NORMAL,
  53. /*! Index for attended transfer basic bridge personality */
  54. BRIDGE_BASIC_PERSONALITY_ATXFER,
  55. /*! Indicates end of enum. Must always remain the last element */
  56. BRIDGE_BASIC_PERSONALITY_END,
  57. };
  58. /*!
  59. * \brief Change basic bridge personality
  60. *
  61. * Changing personalities allows for the bridge to remain in use but have
  62. * properties such as its v_table and its flags change.
  63. *
  64. * \param bridge The bridge
  65. * \param type The personality to change the bridge to
  66. * \param user_data Private data to attach to the personality.
  67. */
  68. static void bridge_basic_change_personality(struct ast_bridge *bridge,
  69. enum bridge_basic_personality_type type, void *user_data);
  70. /* ------------------------------------------------------------------- */
  71. static const struct ast_datastore_info dtmf_features_info = {
  72. .type = "bridge-dtmf-features",
  73. .destroy = ast_free_ptr,
  74. };
  75. /*!
  76. * \internal
  77. * \since 12.0.0
  78. * \brief read a feature code character and set it on for the give feature_flags struct
  79. *
  80. * \param feature_flags flags being modifed
  81. * \param feature feature code provided - should be an uppercase letter
  82. *
  83. * \retval 0 if the feature was set successfully
  84. * \retval -1 failure because the requested feature code isn't handled by this function
  85. */
  86. static int set_feature_flag_from_char(struct ast_flags *feature_flags, char feature)
  87. {
  88. switch (feature) {
  89. case 'T':
  90. ast_set_flag(feature_flags, AST_FEATURE_REDIRECT);
  91. return 0;
  92. case 'K':
  93. ast_set_flag(feature_flags, AST_FEATURE_PARKCALL);
  94. return 0;
  95. case 'H':
  96. ast_set_flag(feature_flags, AST_FEATURE_DISCONNECT);
  97. return 0;
  98. case 'W':
  99. ast_set_flag(feature_flags, AST_FEATURE_AUTOMON);
  100. return 0;
  101. case 'X':
  102. ast_set_flag(feature_flags, AST_FEATURE_AUTOMIXMON);
  103. return 0;
  104. default:
  105. return -1;
  106. }
  107. }
  108. /*!
  109. * \internal
  110. * \since 12.0.0
  111. * \brief Write a features string to a string buffer based on the feature flags provided
  112. *
  113. * \param feature_flags pointer to the feature flags to write from.
  114. * \param buffer pointer to a string buffer to write the features
  115. * \param buffer_size size of the buffer provided (should be able to fit all feature codes)
  116. *
  117. * \retval 0 on successful write
  118. * \retval -1 failure due to running out of buffer space
  119. */
  120. static int dtmf_features_flags_to_string(struct ast_flags *feature_flags, char *buffer, size_t buffer_size)
  121. {
  122. size_t buffer_expended = 0;
  123. unsigned int cur_feature;
  124. static const struct {
  125. char letter;
  126. unsigned int flag;
  127. } associations[] = {
  128. { 'T', AST_FEATURE_REDIRECT },
  129. { 'K', AST_FEATURE_PARKCALL },
  130. { 'H', AST_FEATURE_DISCONNECT },
  131. { 'W', AST_FEATURE_AUTOMON },
  132. { 'X', AST_FEATURE_AUTOMIXMON },
  133. };
  134. for (cur_feature = 0; cur_feature < ARRAY_LEN(associations); cur_feature++) {
  135. if (ast_test_flag(feature_flags, associations[cur_feature].flag)) {
  136. if (buffer_expended == buffer_size - 1) {
  137. buffer[buffer_expended] = '\0';
  138. return -1;
  139. }
  140. buffer[buffer_expended++] = associations[cur_feature].letter;
  141. }
  142. }
  143. buffer[buffer_expended] = '\0';
  144. return 0;
  145. }
  146. static int build_dtmf_features(struct ast_flags *flags, const char *features)
  147. {
  148. const char *feature;
  149. char missing_features[strlen(features) + 1];
  150. size_t number_of_missing_features = 0;
  151. for (feature = features; *feature; feature++) {
  152. if (!isupper(*feature)) {
  153. ast_log(LOG_ERROR, "Features string '%s' rejected because it contains non-uppercase feature.\n", features);
  154. return -1;
  155. }
  156. if (set_feature_flag_from_char(flags, *feature)) {
  157. missing_features[number_of_missing_features++] = *feature;
  158. }
  159. }
  160. missing_features[number_of_missing_features] = '\0';
  161. if (number_of_missing_features) {
  162. ast_log(LOG_WARNING, "Features '%s' from features string '%s' can not be applied.\n", missing_features, features);
  163. }
  164. return 0;
  165. }
  166. int ast_bridge_features_ds_set_string(struct ast_channel *chan, const char *features)
  167. {
  168. struct ast_flags flags = {0};
  169. if (build_dtmf_features(&flags, features)) {
  170. return -1;
  171. }
  172. ast_channel_lock(chan);
  173. if (ast_bridge_features_ds_set(chan, &flags)) {
  174. ast_channel_unlock(chan);
  175. ast_log(LOG_ERROR, "Failed to apply features datastore for '%s' to channel '%s'\n", features, ast_channel_name(chan));
  176. return -1;
  177. }
  178. ast_channel_unlock(chan);
  179. return 0;
  180. }
  181. int ast_bridge_features_ds_get_string(struct ast_channel *chan, char *buffer, size_t buf_size)
  182. {
  183. struct ast_flags *channel_flags;
  184. struct ast_flags held_copy;
  185. ast_channel_lock(chan);
  186. if (!(channel_flags = ast_bridge_features_ds_get(chan))) {
  187. ast_channel_unlock(chan);
  188. return -1;
  189. }
  190. held_copy = *channel_flags;
  191. ast_channel_unlock(chan);
  192. return dtmf_features_flags_to_string(&held_copy, buffer, buf_size);
  193. }
  194. static int bridge_features_ds_set_full(struct ast_channel *chan, struct ast_flags *flags, int replace)
  195. {
  196. struct ast_datastore *datastore;
  197. struct ast_flags *ds_flags;
  198. datastore = ast_channel_datastore_find(chan, &dtmf_features_info, NULL);
  199. if (datastore) {
  200. ds_flags = datastore->data;
  201. if (replace) {
  202. *ds_flags = *flags;
  203. } else {
  204. flags->flags = flags->flags | ds_flags->flags;
  205. *ds_flags = *flags;
  206. }
  207. return 0;
  208. }
  209. datastore = ast_datastore_alloc(&dtmf_features_info, NULL);
  210. if (!datastore) {
  211. return -1;
  212. }
  213. ds_flags = ast_malloc(sizeof(*ds_flags));
  214. if (!ds_flags) {
  215. ast_datastore_free(datastore);
  216. return -1;
  217. }
  218. *ds_flags = *flags;
  219. datastore->data = ds_flags;
  220. ast_channel_datastore_add(chan, datastore);
  221. return 0;
  222. }
  223. int ast_bridge_features_ds_set(struct ast_channel *chan, struct ast_flags *flags)
  224. {
  225. return bridge_features_ds_set_full(chan, flags, 1);
  226. }
  227. int ast_bridge_features_ds_append(struct ast_channel *chan, struct ast_flags *flags)
  228. {
  229. return bridge_features_ds_set_full(chan, flags, 0);
  230. }
  231. struct ast_flags *ast_bridge_features_ds_get(struct ast_channel *chan)
  232. {
  233. struct ast_datastore *datastore;
  234. datastore = ast_channel_datastore_find(chan, &dtmf_features_info, NULL);
  235. if (!datastore) {
  236. return NULL;
  237. }
  238. return datastore->data;
  239. }
  240. /*!
  241. * \internal
  242. * \brief Determine if we should dissolve the bridge from a hangup.
  243. * \since 12.0.0
  244. *
  245. * \param bridge_channel Channel executing the feature
  246. * \param hook_pvt Private data passed in when the hook was created
  247. *
  248. * \retval 0 Keep the callback hook.
  249. * \retval -1 Remove the callback hook.
  250. */
  251. static int basic_hangup_hook(struct ast_bridge_channel *bridge_channel, void *hook_pvt)
  252. {
  253. int bridge_count = 0;
  254. struct ast_bridge_channel *iter;
  255. ast_bridge_channel_lock_bridge(bridge_channel);
  256. AST_LIST_TRAVERSE(&bridge_channel->bridge->channels, iter, entry) {
  257. if (iter != bridge_channel && iter->state == BRIDGE_CHANNEL_STATE_WAIT) {
  258. ++bridge_count;
  259. }
  260. }
  261. if (2 <= bridge_count) {
  262. /* Just allow this channel to leave the multi-party bridge. */
  263. ast_bridge_channel_leave_bridge(bridge_channel,
  264. BRIDGE_CHANNEL_STATE_END_NO_DISSOLVE, 0);
  265. }
  266. ast_bridge_unlock(bridge_channel->bridge);
  267. return 0;
  268. }
  269. /*!
  270. * \brief Details for specific basic bridge personalities
  271. */
  272. struct personality_details {
  273. /*! The v_table to use for this personality */
  274. struct ast_bridge_methods *v_table;
  275. /*! Flags to set on this type of bridge */
  276. unsigned int bridge_flags;
  277. /*! User data for this personality. If used, must be an ao2 object */
  278. void *pvt;
  279. /*! Callback to be called when changing to the personality */
  280. void (*on_personality_change)(struct ast_bridge *bridge);
  281. };
  282. /*!
  283. * \brief structure that organizes different personalities for basic bridges.
  284. */
  285. struct bridge_basic_personality {
  286. /*! The current bridge personality in use */
  287. enum bridge_basic_personality_type current;
  288. /*! Array of details for the types of bridge personalities supported */
  289. struct personality_details details[BRIDGE_BASIC_PERSONALITY_END];
  290. };
  291. /*!
  292. * \internal
  293. * \brief Get the extension for a given builtin feature.
  294. *
  295. * \param chan Get the feature extension for this channel.
  296. * \param feature_name features.conf name of feature.
  297. * \param buf Where to put the extension.
  298. * \param len Length of the given extension buffer.
  299. *
  300. * \retval 0 success
  301. * \retval non-zero failiure
  302. */
  303. static int builtin_feature_get_exten(struct ast_channel *chan, const char *feature_name, char *buf, size_t len)
  304. {
  305. SCOPED_CHANNELLOCK(lock, chan);
  306. return ast_get_builtin_feature(chan, feature_name, buf, len);
  307. }
  308. /*!
  309. * \internal
  310. * \brief Helper to add a builtin DTMF feature hook to the features struct.
  311. * \since 12.0.0
  312. *
  313. * \param features Bridge features to setup.
  314. * \param chan Get features from this channel.
  315. * \param flags Feature flags on the channel.
  316. * \param feature_flag Feature flag to test.
  317. * \param feature_name features.conf name of feature.
  318. * \param feature_bridge Bridge feature enum to get hook callback.
  319. *
  320. * \retval 0 on success.
  321. * \retval -1 on error.
  322. */
  323. static int builtin_features_helper(struct ast_bridge_features *features, struct ast_channel *chan,
  324. struct ast_flags *flags, unsigned int feature_flag, const char *feature_name, enum ast_bridge_builtin_feature feature_bridge)
  325. {
  326. char dtmf[AST_FEATURE_MAX_LEN];
  327. int res;
  328. res = 0;
  329. if (ast_test_flag(flags, feature_flag)
  330. && !builtin_feature_get_exten(chan, feature_name, dtmf, sizeof(dtmf))
  331. && !ast_strlen_zero(dtmf)) {
  332. res = ast_bridge_features_enable(features, feature_bridge, dtmf, NULL, NULL,
  333. AST_BRIDGE_HOOK_REMOVE_ON_PULL | AST_BRIDGE_HOOK_REMOVE_ON_PERSONALITY_CHANGE);
  334. if (res) {
  335. ast_log(LOG_ERROR, "Channel %s: Requested DTMF feature %s not available.\n",
  336. ast_channel_name(chan), feature_name);
  337. }
  338. }
  339. return res;
  340. }
  341. /*!
  342. * \internal
  343. * \brief Setup bridge builtin features.
  344. * \since 12.0.0
  345. *
  346. * \param features Bridge features to setup.
  347. * \param chan Get features from this channel.
  348. *
  349. * \retval 0 on success.
  350. * \retval -1 on error.
  351. */
  352. static int setup_bridge_features_builtin(struct ast_bridge_features *features, struct ast_channel *chan)
  353. {
  354. struct ast_flags *flags;
  355. int res;
  356. ast_channel_lock(chan);
  357. flags = ast_bridge_features_ds_get(chan);
  358. ast_channel_unlock(chan);
  359. if (!flags) {
  360. return 0;
  361. }
  362. res = 0;
  363. res |= builtin_features_helper(features, chan, flags, AST_FEATURE_REDIRECT, "blindxfer", AST_BRIDGE_BUILTIN_BLINDTRANSFER);
  364. res |= builtin_features_helper(features, chan, flags, AST_FEATURE_REDIRECT, "atxfer", AST_BRIDGE_BUILTIN_ATTENDEDTRANSFER);
  365. res |= builtin_features_helper(features, chan, flags, AST_FEATURE_DISCONNECT, "disconnect", AST_BRIDGE_BUILTIN_HANGUP);
  366. res |= builtin_features_helper(features, chan, flags, AST_FEATURE_PARKCALL, "parkcall", AST_BRIDGE_BUILTIN_PARKCALL);
  367. res |= builtin_features_helper(features, chan, flags, AST_FEATURE_AUTOMIXMON, "automixmon", AST_BRIDGE_BUILTIN_AUTOMIXMON);
  368. return res ? -1 : 0;
  369. }
  370. struct dynamic_dtmf_hook_run {
  371. /*! Offset into app_name[] where the channel name that activated the hook starts. */
  372. int activated_offset;
  373. /*! Offset into app_name[] where the dynamic feature name starts. */
  374. int feature_offset;
  375. /*! Offset into app_name[] where the MOH class name starts. (zero if no MOH) */
  376. int moh_offset;
  377. /*! Offset into app_name[] where the application argument string starts. (zero if no arguments) */
  378. int app_args_offset;
  379. /*! Application name to run. */
  380. char app_name[0];
  381. };
  382. static void dynamic_dtmf_hook_callback(struct ast_bridge_channel *bridge_channel,
  383. const void *payload, size_t payload_size)
  384. {
  385. struct ast_channel *chan = bridge_channel->chan;
  386. const struct dynamic_dtmf_hook_run *run_data = payload;
  387. pbx_builtin_setvar_helper(chan, "DYNAMIC_FEATURENAME",
  388. &run_data->app_name[run_data->feature_offset]);
  389. pbx_builtin_setvar_helper(chan, "DYNAMIC_WHO_ACTIVATED",
  390. &run_data->app_name[run_data->activated_offset]);
  391. ast_bridge_channel_run_app(bridge_channel, run_data->app_name,
  392. run_data->app_args_offset ? &run_data->app_name[run_data->app_args_offset] : NULL,
  393. run_data->moh_offset ? &run_data->app_name[run_data->moh_offset] : NULL);
  394. }
  395. struct dynamic_dtmf_hook_data {
  396. /*! Which side of bridge to run app (AST_FEATURE_FLAG_ONSELF/AST_FEATURE_FLAG_ONPEER) */
  397. unsigned int flags;
  398. /*! Offset into app_name[] where the dynamic feature name starts. */
  399. int feature_offset;
  400. /*! Offset into app_name[] where the MOH class name starts. (zero if no MOH) */
  401. int moh_offset;
  402. /*! Offset into app_name[] where the application argument string starts. (zero if no arguments) */
  403. int app_args_offset;
  404. /*! Application name to run. */
  405. char app_name[0];
  406. };
  407. /*!
  408. * \internal
  409. * \brief Activated dynamic DTMF feature hook.
  410. * \since 12.0.0
  411. *
  412. * \param bridge_channel Channel executing the feature
  413. * \param hook_pvt Private data passed in when the hook was created
  414. *
  415. * \retval 0 Keep the callback hook.
  416. * \retval -1 Remove the callback hook.
  417. */
  418. static int dynamic_dtmf_hook_trip(struct ast_bridge_channel *bridge_channel, void *hook_pvt)
  419. {
  420. struct dynamic_dtmf_hook_data *pvt = hook_pvt;
  421. struct dynamic_dtmf_hook_run *run_data;
  422. const char *activated_name;
  423. size_t len_name;
  424. size_t len_args;
  425. size_t len_moh;
  426. size_t len_feature;
  427. size_t len_activated;
  428. size_t len_data;
  429. /* Determine lengths of things. */
  430. len_name = strlen(pvt->app_name) + 1;
  431. len_args = pvt->app_args_offset ? strlen(&pvt->app_name[pvt->app_args_offset]) + 1 : 0;
  432. len_moh = pvt->moh_offset ? strlen(&pvt->app_name[pvt->moh_offset]) + 1 : 0;
  433. len_feature = strlen(&pvt->app_name[pvt->feature_offset]) + 1;
  434. ast_channel_lock(bridge_channel->chan);
  435. activated_name = ast_strdupa(ast_channel_name(bridge_channel->chan));
  436. ast_channel_unlock(bridge_channel->chan);
  437. len_activated = strlen(activated_name) + 1;
  438. len_data = sizeof(*run_data) + len_name + len_args + len_moh + len_feature + len_activated;
  439. /* Fill in dynamic feature run hook data. */
  440. run_data = ast_alloca(len_data);
  441. run_data->app_args_offset = len_args ? len_name : 0;
  442. run_data->moh_offset = len_moh ? len_name + len_args : 0;
  443. run_data->feature_offset = len_name + len_args + len_moh;
  444. run_data->activated_offset = len_name + len_args + len_moh + len_feature;
  445. strcpy(run_data->app_name, pvt->app_name);/* Safe */
  446. if (len_args) {
  447. strcpy(&run_data->app_name[run_data->app_args_offset],
  448. &pvt->app_name[pvt->app_args_offset]);/* Safe */
  449. }
  450. if (len_moh) {
  451. strcpy(&run_data->app_name[run_data->moh_offset],
  452. &pvt->app_name[pvt->moh_offset]);/* Safe */
  453. }
  454. strcpy(&run_data->app_name[run_data->feature_offset],
  455. &pvt->app_name[pvt->feature_offset]);/* Safe */
  456. strcpy(&run_data->app_name[run_data->activated_offset], activated_name);/* Safe */
  457. if (ast_test_flag(pvt, AST_FEATURE_FLAG_ONPEER)) {
  458. ast_bridge_channel_write_callback(bridge_channel,
  459. AST_BRIDGE_CHANNEL_CB_OPTION_MEDIA,
  460. dynamic_dtmf_hook_callback, run_data, len_data);
  461. } else {
  462. dynamic_dtmf_hook_callback(bridge_channel, run_data, len_data);
  463. }
  464. return 0;
  465. }
  466. /*!
  467. * \internal
  468. * \brief Add a dynamic DTMF feature hook to the bridge features.
  469. * \since 12.0.0
  470. *
  471. * \param features Bridge features to setup.
  472. * \param flags Which side of bridge to run app (AST_FEATURE_FLAG_ONSELF/AST_FEATURE_FLAG_ONPEER).
  473. * \param dtmf DTMF trigger sequence.
  474. * \param feature_name Name of the dynamic feature.
  475. * \param app_name Dialplan application name to run.
  476. * \param app_args Dialplan application arguments. (Empty or NULL if no arguments)
  477. * \param moh_class MOH class to play to peer. (Empty or NULL if no MOH played)
  478. *
  479. * \retval 0 on success.
  480. * \retval -1 on error.
  481. */
  482. static int dynamic_dtmf_hook_add(struct ast_bridge_features *features, unsigned int flags, const char *dtmf, const char *feature_name, const char *app_name, const char *app_args, const char *moh_class)
  483. {
  484. struct dynamic_dtmf_hook_data *hook_data;
  485. size_t len_name = strlen(app_name) + 1;
  486. size_t len_args = ast_strlen_zero(app_args) ? 0 : strlen(app_args) + 1;
  487. size_t len_moh = ast_strlen_zero(moh_class) ? 0 : strlen(moh_class) + 1;
  488. size_t len_feature = strlen(feature_name) + 1;
  489. size_t len_data = sizeof(*hook_data) + len_name + len_args + len_moh + len_feature;
  490. int res;
  491. /* Fill in application run hook data. */
  492. hook_data = ast_malloc(len_data);
  493. if (!hook_data) {
  494. return -1;
  495. }
  496. hook_data->flags = flags;
  497. hook_data->app_args_offset = len_args ? len_name : 0;
  498. hook_data->moh_offset = len_moh ? len_name + len_args : 0;
  499. hook_data->feature_offset = len_name + len_args + len_moh;
  500. strcpy(hook_data->app_name, app_name);/* Safe */
  501. if (len_args) {
  502. strcpy(&hook_data->app_name[hook_data->app_args_offset], app_args);/* Safe */
  503. }
  504. if (len_moh) {
  505. strcpy(&hook_data->app_name[hook_data->moh_offset], moh_class);/* Safe */
  506. }
  507. strcpy(&hook_data->app_name[hook_data->feature_offset], feature_name);/* Safe */
  508. res = ast_bridge_dtmf_hook(features, dtmf, dynamic_dtmf_hook_trip, hook_data,
  509. ast_free_ptr,
  510. AST_BRIDGE_HOOK_REMOVE_ON_PULL | AST_BRIDGE_HOOK_REMOVE_ON_PERSONALITY_CHANGE);
  511. if (res) {
  512. ast_free(hook_data);
  513. }
  514. return res;
  515. }
  516. static int setup_dynamic_feature(void *obj, void *arg, void *data, int flags)
  517. {
  518. struct ast_applicationmap_item *item = obj;
  519. struct ast_bridge_features *features = arg;
  520. int *res = data;
  521. *res |= dynamic_dtmf_hook_add(features,
  522. item->activate_on_self ? AST_FEATURE_FLAG_ONSELF : AST_FEATURE_FLAG_ONPEER,
  523. item->dtmf, item->name, item->app, item->app_data, item->moh_class);
  524. return 0;
  525. }
  526. /*!
  527. * \internal
  528. * \brief Setup bridge dynamic features.
  529. * \since 12.0.0
  530. *
  531. * \param features Bridge features to setup.
  532. * \param chan Get features from this channel.
  533. *
  534. * \retval 0 on success.
  535. * \retval -1 on error.
  536. */
  537. static int setup_bridge_features_dynamic(struct ast_bridge_features *features, struct ast_channel *chan)
  538. {
  539. struct ao2_container *applicationmap;
  540. int res = 0;
  541. ast_channel_lock(chan);
  542. applicationmap = ast_get_chan_applicationmap(chan);
  543. ast_channel_unlock(chan);
  544. if (applicationmap) {
  545. ao2_callback_data(applicationmap, 0, setup_dynamic_feature, features, &res);
  546. ao2_ref(applicationmap, -1);
  547. }
  548. return res;
  549. }
  550. /*!
  551. * \internal
  552. * \brief Setup DTMF feature hooks using the channel features datastore property.
  553. * \since 12.0.0
  554. *
  555. * \param bridge_channel What to setup DTMF features on.
  556. *
  557. * \retval 0 on success.
  558. * \retval -1 on error.
  559. */
  560. static int bridge_basic_setup_features(struct ast_bridge_channel *bridge_channel)
  561. {
  562. int res = 0;
  563. res |= setup_bridge_features_builtin(bridge_channel->features, bridge_channel->chan);
  564. res |= setup_bridge_features_dynamic(bridge_channel->features, bridge_channel->chan);
  565. return res;
  566. }
  567. static int add_normal_hooks(struct ast_bridge *bridge, struct ast_bridge_channel *bridge_channel)
  568. {
  569. return ast_bridge_hangup_hook(bridge_channel->features, basic_hangup_hook,
  570. NULL, NULL, AST_BRIDGE_HOOK_REMOVE_ON_PULL)
  571. || bridge_basic_setup_features(bridge_channel);
  572. }
  573. /*!
  574. * \internal
  575. * \brief ast_bridge basic push method.
  576. * \since 12.0.0
  577. *
  578. * \param self Bridge to operate upon.
  579. * \param bridge_channel Bridge channel to push.
  580. * \param swap Bridge channel to swap places with if not NULL.
  581. *
  582. * \note On entry, self is already locked.
  583. *
  584. * \retval 0 on success
  585. * \retval -1 on failure
  586. */
  587. static int bridge_personality_normal_push(struct ast_bridge *self, struct ast_bridge_channel *bridge_channel, struct ast_bridge_channel *swap)
  588. {
  589. if (add_normal_hooks(self, bridge_channel)) {
  590. return -1;
  591. }
  592. return 0;
  593. }
  594. static int bridge_basic_push(struct ast_bridge *self, struct ast_bridge_channel *bridge_channel, struct ast_bridge_channel *swap)
  595. {
  596. struct bridge_basic_personality *personality = self->personality;
  597. ast_assert(personality != NULL);
  598. if (personality->details[personality->current].v_table->push
  599. && personality->details[personality->current].v_table->push(self, bridge_channel, swap)) {
  600. return -1;
  601. }
  602. ast_bridge_channel_update_linkedids(bridge_channel, swap);
  603. ast_bridge_channel_update_accountcodes(bridge_channel, swap);
  604. return ast_bridge_base_v_table.push(self, bridge_channel, swap);
  605. }
  606. static void bridge_basic_pull(struct ast_bridge *self, struct ast_bridge_channel *bridge_channel)
  607. {
  608. struct bridge_basic_personality *personality = self->personality;
  609. ast_assert(personality != NULL);
  610. if (personality->details[personality->current].v_table->pull) {
  611. personality->details[personality->current].v_table->pull(self, bridge_channel);
  612. }
  613. ast_bridge_channel_update_accountcodes(NULL, bridge_channel);
  614. ast_bridge_base_v_table.pull(self, bridge_channel);
  615. }
  616. static void bridge_basic_destroy(struct ast_bridge *self)
  617. {
  618. struct bridge_basic_personality *personality = self->personality;
  619. ao2_cleanup(personality);
  620. ast_bridge_base_v_table.destroy(self);
  621. }
  622. /*!
  623. * \brief Remove appropriate hooks when basic bridge personality changes
  624. *
  625. * Hooks that have the AST_BRIDGE_HOOK_REMOVE_ON_PERSONALITY_CHANGE flag
  626. * set will be removed from all bridge channels in the bridge.
  627. *
  628. * \param bridge Basic bridge undergoing personality change
  629. */
  630. static void remove_hooks_on_personality_change(struct ast_bridge *bridge)
  631. {
  632. struct ast_bridge_channel *iter;
  633. AST_LIST_TRAVERSE(&bridge->channels, iter, entry) {
  634. SCOPED_LOCK(lock, iter, ast_bridge_channel_lock, ast_bridge_channel_unlock);
  635. ast_bridge_features_remove(iter->features, AST_BRIDGE_HOOK_REMOVE_ON_PERSONALITY_CHANGE);
  636. }
  637. }
  638. /*!
  639. * \brief Attended transfer superstates.
  640. *
  641. * An attended transfer's progress is facilitated by a state machine.
  642. * The individual states of the state machine fall into the realm of
  643. * one of two superstates.
  644. */
  645. enum attended_transfer_superstate {
  646. /*!
  647. * \brief Transfer superstate
  648. *
  649. * The attended transfer state machine begins in this superstate. The
  650. * goal of this state is for a transferer channel to facilitate a
  651. * transfer from a transferee to a transfer target.
  652. *
  653. * There are two bridges used in this superstate. The transferee bridge is
  654. * the bridge that the transferer and transferee channels originally
  655. * communicate in, and the target bridge is the bridge where the transfer
  656. * target is being dialed.
  657. *
  658. * The transferer channel is capable of moving between the bridges using
  659. * the DTMF swap sequence.
  660. */
  661. SUPERSTATE_TRANSFER,
  662. /*!
  663. * \brief Recall superstate
  664. *
  665. * The attended transfer state machine moves to this superstate if
  666. * atxferdropcall is set to "no" and the transferer channel hangs up
  667. * during a transfer. The goal in this superstate is to call back either
  668. * the transfer target or transferer and rebridge with the transferee
  669. * channel(s).
  670. *
  671. * In this superstate, there is only a single bridge used, the original
  672. * transferee bridge. Rather than distinguishing between a transferer
  673. * and transfer target, all outbound calls are toward a "recall_target"
  674. * channel.
  675. */
  676. SUPERSTATE_RECALL,
  677. };
  678. /*!
  679. * The states in the attended transfer state machine.
  680. */
  681. enum attended_transfer_state {
  682. /*!
  683. * \brief Calling Target state
  684. *
  685. * This state describes the initial state of a transfer. The transferer
  686. * waits in the transfer target's bridge for the transfer target to answer.
  687. *
  688. * Superstate: Transfer
  689. *
  690. * Preconditions:
  691. * 1) Transfer target is RINGING
  692. * 2) Transferer is in transferee bridge
  693. * 3) Transferee is on hold
  694. *
  695. * Transitions to TRANSFER_CALLING_TARGET:
  696. * 1) This is the initial state for an attended transfer.
  697. * 2) TRANSFER_HESITANT: Transferer presses DTMF swap sequence
  698. *
  699. * State operation:
  700. * The transferer is moved from the transferee bridge into the transfer
  701. * target bridge.
  702. *
  703. * Transitions from TRANSFER_CALLING_TARGET:
  704. * 1) TRANSFER_FAIL: Transferee hangs up.
  705. * 2) TRANSFER_BLOND: Transferer hangs up or presses DTMF swap sequence
  706. * and configured atxferdropcall setting is yes.
  707. * 3) TRANSFER_BLOND_NONFINAL: Transferer hangs up or presses DTMF swap
  708. * sequence and configured atxferdropcall setting is no.
  709. * 4) TRANSFER_CONSULTING: Transfer target answers the call.
  710. * 5) TRANSFER_REBRIDGE: Transfer target hangs up, call to transfer target
  711. * times out, or transferer presses DTMF abort sequence.
  712. * 6) TRANSFER_THREEWAY: Transferer presses DTMF threeway sequence.
  713. * 7) TRANSFER_HESITANT: Transferer presses DTMF swap sequence.
  714. */
  715. TRANSFER_CALLING_TARGET,
  716. /*!
  717. * \brief Hesitant state
  718. *
  719. * This state only arises if when waiting for the transfer target to
  720. * answer, the transferer presses the DTMF swap sequence. This will
  721. * cause the transferer to be rebridged with the transferee temporarily.
  722. *
  723. * Superstate: Transfer
  724. *
  725. * Preconditions:
  726. * 1) Transfer target is in ringing state
  727. * 2) Transferer is in transfer target bridge
  728. * 3) Transferee is on hold
  729. *
  730. * Transitions to TRANSFER_HESITANT:
  731. * 1) TRANSFER_CALLING_TARGET: Transferer presses DTMF swap sequence.
  732. *
  733. * State operation:
  734. * The transferer is moved from the transfer target bridge into the
  735. * transferee bridge, and the transferee is taken off hold.
  736. *
  737. * Transitions from TRANSFER_HESITANT:
  738. * 1) TRANSFER_FAIL: Transferee hangs up
  739. * 2) TRANSFER_BLOND: Transferer hangs up or presses DTMF swap sequence
  740. * and configured atxferdropcall setting is yes.
  741. * 3) TRANSFER_BLOND_NONFINAL: Transferer hangs up or presses DTMF swap
  742. * sequence and configured atxferdropcall setting is no.
  743. * 4) TRANSFER_DOUBLECHECKING: Transfer target answers the call
  744. * 5) TRANSFER_RESUME: Transfer target hangs up, call to transfer target
  745. * times out, or transferer presses DTMF abort sequence.
  746. * 6) TRANSFER_THREEWAY: Transferer presses DTMF threeway sequence.
  747. * 7) TRANSFER_CALLING_TARGET: Transferer presses DTMF swap sequence.
  748. */
  749. TRANSFER_HESITANT,
  750. /*!
  751. * \brief Rebridge state
  752. *
  753. * This is a terminal state that indicates that the transferer needs
  754. * to move back to the transferee's bridge. This is a failed attended
  755. * transfer result.
  756. *
  757. * Superstate: Transfer
  758. *
  759. * Preconditions:
  760. * 1) Transferer is in transfer target bridge
  761. * 2) Transferee is on hold
  762. *
  763. * Transitions to TRANSFER_REBRIDGE:
  764. * 1) TRANSFER_CALLING_TARGET: Transfer target hangs up, call to transfer target
  765. * times out, or transferer presses DTMF abort sequence.
  766. * 2) TRANSFER_STATE_CONSULTING: Transfer target hangs up, or transferer presses
  767. * DTMF abort sequence.
  768. *
  769. * State operation:
  770. * The transferer channel is moved from the transfer target bridge to the
  771. * transferee bridge. The transferee is taken off hold. A stasis transfer
  772. * message is published indicating a failed attended transfer.
  773. *
  774. * Transitions from TRANSFER_REBRIDGE:
  775. * None
  776. */
  777. TRANSFER_REBRIDGE,
  778. /*!
  779. * \brief Resume state
  780. *
  781. * This is a terminal state that indicates that the party bridged with the
  782. * transferee is the final party to be bridged with that transferee. This state
  783. * may come about due to a successful recall or due to a failed transfer.
  784. *
  785. * Superstate: Transfer or Recall
  786. *
  787. * Preconditions:
  788. * In Transfer Superstate:
  789. * 1) Transferer is in transferee bridge
  790. * 2) Transferee is not on hold
  791. * In Recall Superstate:
  792. * 1) The recall target is in the transferee bridge
  793. * 2) Transferee is not on hold
  794. *
  795. * Transitions to TRANSFER_RESUME:
  796. * TRANSFER_HESITANT: Transfer target hangs up, call to transfer target times out,
  797. * or transferer presses DTMF abort sequence.
  798. * TRANSFER_DOUBLECHECKING: Transfer target hangs up or transferer presses DTMF
  799. * abort sequence.
  800. * TRANSFER_BLOND_NONFINAL: Recall target answers
  801. * TRANSFER_RECALLING: Recall target answers
  802. * TRANSFER_RETRANSFER: Recall target answers
  803. *
  804. * State operations:
  805. * None
  806. *
  807. * Transitions from TRANSFER_RESUME:
  808. * None
  809. */
  810. TRANSFER_RESUME,
  811. /*!
  812. * \brief Threeway state
  813. *
  814. * This state results when the transferer wishes to have all parties involved
  815. * in a transfer to be in the same bridge together.
  816. *
  817. * Superstate: Transfer
  818. *
  819. * Preconditions:
  820. * 1) Transfer target state is either RINGING or UP
  821. * 2) Transferer is in either bridge
  822. * 3) Transferee is not on hold
  823. *
  824. * Transitions to TRANSFER_THREEWAY:
  825. * 1) TRANSFER_CALLING_TARGET: Transferer presses DTMF threeway sequence.
  826. * 2) TRANSFER_HESITANT: Transferer presses DTMF threeway sequence.
  827. * 3) TRANSFER_CONSULTING: Transferer presses DTMF threeway sequence.
  828. * 4) TRANSFER_DOUBLECHECKING: Transferer presses DTMF threeway sequence.
  829. *
  830. * State operation:
  831. * The transfer target bridge is merged into the transferee bridge.
  832. *
  833. * Transitions from TRANSFER_THREEWAY:
  834. * None.
  835. */
  836. TRANSFER_THREEWAY,
  837. /*!
  838. * \brief Consulting state
  839. *
  840. * This state describes the case where the transferer and transfer target
  841. * are able to converse in the transfer target's bridge prior to completing
  842. * the transfer.
  843. *
  844. * Superstate: Transfer
  845. *
  846. * Preconditions:
  847. * 1) Transfer target is UP
  848. * 2) Transferer is in target bridge
  849. * 3) Transferee is on hold
  850. *
  851. * Transitions to TRANSFER_CONSULTING:
  852. * 1) TRANSFER_CALLING_TARGET: Transfer target answers.
  853. * 2) TRANSFER_DOUBLECHECKING: Transferer presses DTMF swap sequence.
  854. *
  855. * State operations:
  856. * None.
  857. *
  858. * Transitions from TRANSFER_CONSULTING:
  859. * TRANSFER_COMPLETE: Transferer hangs up or transferer presses DTMF complete sequence.
  860. * TRANSFER_REBRIDGE: Transfer target hangs up or transferer presses DTMF abort sequence.
  861. * TRANSFER_THREEWAY: Transferer presses DTMF threeway sequence.
  862. * TRANSFER_DOUBLECHECKING: Transferer presses DTMF swap sequence.
  863. */
  864. TRANSFER_CONSULTING,
  865. /*!
  866. * \brief Double-checking state
  867. *
  868. * This state describes the case where the transferer and transferee are
  869. * able to converse in the transferee's bridge prior to completing the transfer. The
  870. * difference between this and TRANSFER_HESITANT is that the transfer target is
  871. * UP in this case.
  872. *
  873. * Superstate: Transfer
  874. *
  875. * Preconditions:
  876. * 1) Transfer target is UP and on hold
  877. * 2) Transferer is in transferee bridge
  878. * 3) Transferee is off hold
  879. *
  880. * Transitions to TRANSFER_DOUBLECHECKING:
  881. * 1) TRANSFER_HESITANT: Transfer target answers.
  882. * 2) TRANSFER_CONSULTING: Transferer presses DTMF swap sequence.
  883. *
  884. * State operations:
  885. * None.
  886. *
  887. * Transitions from TRANSFER_DOUBLECHECKING:
  888. * 1) TRANSFER_FAIL: Transferee hangs up.
  889. * 2) TRANSFER_COMPLETE: Transferer hangs up or presses DTMF complete sequence.
  890. * 3) TRANSFER_RESUME: Transfer target hangs up or transferer presses DTMF abort sequence.
  891. * 4) TRANSFER_THREEWAY: Transferer presses DTMF threeway sequence.
  892. * 5) TRANSFER_CONSULTING: Transferer presses the DTMF swap sequence.
  893. */
  894. TRANSFER_DOUBLECHECKING,
  895. /*!
  896. * \brief Complete state
  897. *
  898. * This is a terminal state where a transferer has successfully completed an attended
  899. * transfer. This state's goal is to get the transfer target and transferee into
  900. * the same bridge and the transferer off the call.
  901. *
  902. * Superstate: Transfer
  903. *
  904. * Preconditions:
  905. * 1) Transfer target is UP and off hold.
  906. * 2) Transferer is in either bridge.
  907. * 3) Transferee is off hold.
  908. *
  909. * Transitions to TRANSFER_COMPLETE:
  910. * 1) TRANSFER_CONSULTING: transferer hangs up or presses the DTMF complete sequence.
  911. * 2) TRANSFER_DOUBLECHECKING: transferer hangs up or presses the DTMF complete sequence.
  912. *
  913. * State operation:
  914. * The transfer target bridge is merged into the transferee bridge. The transferer
  915. * channel is kicked out of the bridges as part of the merge.
  916. *
  917. * State operations:
  918. * 1) Merge the transfer target bridge into the transferee bridge,
  919. * excluding the transferer channel from the merge.
  920. * 2) Publish a stasis transfer message.
  921. *
  922. * Exit operations:
  923. * This is a terminal state, so there are no exit operations.
  924. */
  925. TRANSFER_COMPLETE,
  926. /*!
  927. * \brief Blond state
  928. *
  929. * This is a terminal state where a transferer has completed an attended transfer prior
  930. * to the transfer target answering. This state is only entered if atxferdropcall
  931. * is set to 'yes'. This is considered to be a successful attended transfer.
  932. *
  933. * Superstate: Transfer
  934. *
  935. * Preconditions:
  936. * 1) Transfer target is RINGING.
  937. * 2) Transferer is in either bridge.
  938. * 3) Transferee is off hold.
  939. *
  940. * Transitions to TRANSFER_BLOND:
  941. * 1) TRANSFER_CALLING_TARGET: Transferer hangs up or presses the DTMF complete sequence.
  942. * atxferdropcall is set to 'yes'.
  943. * 2) TRANSFER_HESITANT: Transferer hangs up or presses the DTMF complete sequence.
  944. * atxferdropcall is set to 'yes'.
  945. *
  946. * State operations:
  947. * The transfer target bridge is merged into the transferee bridge. The transferer
  948. * channel is kicked out of the bridges as part of the merge. A stasis transfer
  949. * publication is sent indicating a successful transfer.
  950. *
  951. * Transitions from TRANSFER_BLOND:
  952. * None
  953. */
  954. TRANSFER_BLOND,
  955. /*!
  956. * \brief Blond non-final state
  957. *
  958. * This state is very similar to the TRANSFER_BLOND state, except that
  959. * this state is entered when atxferdropcall is set to 'no'. This is the
  960. * initial state of the Recall superstate, so state operations mainly involve
  961. * moving to the Recall superstate. This means that the transfer target, that
  962. * is currently ringing is now known as the recall target.
  963. *
  964. * Superstate: Recall
  965. *
  966. * Preconditions:
  967. * 1) Recall target is RINGING.
  968. * 2) Transferee is off hold.
  969. *
  970. * Transitions to TRANSFER_BLOND_NONFINAL:
  971. * 1) TRANSFER_CALLING_TARGET: Transferer hangs up or presses the DTMF complete sequence.
  972. * atxferdropcall is set to 'no'.
  973. * 2) TRANSFER_HESITANT: Transferer hangs up or presses the DTMF complete sequence.
  974. * atxferdropcall is set to 'no'.
  975. *
  976. * State operation:
  977. * The superstate of the attended transfer is changed from Transfer to Recall.
  978. * The transfer target bridge is merged into the transferee bridge. The transferer
  979. * channel is kicked out of the bridges as part of the merge.
  980. *
  981. * Transitions from TRANSFER_BLOND_NONFINAL:
  982. * 1) TRANSFER_FAIL: Transferee hangs up
  983. * 2) TRANSFER_RESUME: Recall target answers
  984. * 3) TRANSFER_RECALLING: Recall target hangs up or time expires.
  985. */
  986. TRANSFER_BLOND_NONFINAL,
  987. /*!
  988. * \brief Recalling state
  989. *
  990. * This state is entered if the recall target from the TRANSFER_BLOND_NONFINAL
  991. * or TRANSFER_RETRANSFER states hangs up or does not answer. The goal of this
  992. * state is to call back the original transferer in an attempt to recover the
  993. * original call.
  994. *
  995. * Superstate: Recall
  996. *
  997. * Preconditions:
  998. * 1) Recall target is down.
  999. * 2) Transferee is off hold.
  1000. *
  1001. * Transitions to TRANSFER_RECALLING:
  1002. * 1) TRANSFER_BLOND_NONFINAL: Recall target hangs up or time expires.
  1003. * 2) TRANSFER_RETRANSFER: Recall target hangs up or time expires.
  1004. * atxferloopdelay is non-zero.
  1005. * 3) TRANSFER_WAIT_TO_RECALL: Time expires.
  1006. *
  1007. * State operation:
  1008. * The original transferer becomes the recall target and is called using the Dialing API.
  1009. * Ringing is indicated to the transferee.
  1010. *
  1011. * Transitions from TRANSFER_RECALLING:
  1012. * 1) TRANSFER_FAIL:
  1013. * a) Transferee hangs up.
  1014. * b) Recall target hangs up or time expires, and number of recall attempts exceeds atxfercallbackretries
  1015. * 2) TRANSFER_WAIT_TO_RETRANSFER: Recall target hangs up or time expires.
  1016. * atxferloopdelay is non-zero.
  1017. * 3) TRANSFER_RETRANSFER: Recall target hangs up or time expires.
  1018. * atxferloopdelay is zero.
  1019. * 4) TRANSFER_RESUME: Recall target answers.
  1020. */
  1021. TRANSFER_RECALLING,
  1022. /*!
  1023. * \brief Wait to Retransfer state
  1024. *
  1025. * This state is used simply to give a bit of breathing room between attempting
  1026. * to call back the original transferer and attempting to call back the original
  1027. * transfer target. The transferee hears music on hold during this state as an
  1028. * auditory clue that no one is currently being dialed.
  1029. *
  1030. * Superstate: Recall
  1031. *
  1032. * Preconditions:
  1033. * 1) Recall target is down.
  1034. * 2) Transferee is off hold.
  1035. *
  1036. * Transitions to TRANSFER_WAIT_TO_RETRANSFER:
  1037. * 1) TRANSFER_RECALLING: Recall target hangs up or time expires.
  1038. * atxferloopdelay is non-zero.
  1039. *
  1040. * State operation:
  1041. * The transferee is placed on hold.
  1042. *
  1043. * Transitions from TRANSFER_WAIT_TO_RETRANSFER:
  1044. * 1) TRANSFER_FAIL: Transferee hangs up.
  1045. * 2) TRANSFER_RETRANSFER: Time expires.
  1046. */
  1047. TRANSFER_WAIT_TO_RETRANSFER,
  1048. /*!
  1049. * \brief Retransfer state
  1050. *
  1051. * This state is used in order to attempt to call back the original
  1052. * transfer target channel from the transfer. The transferee hears
  1053. * ringing during this state as an auditory cue that a party is being
  1054. * dialed.
  1055. *
  1056. * Superstate: Recall
  1057. *
  1058. * Preconditions:
  1059. * 1) Recall target is down.
  1060. * 2) Transferee is off hold.
  1061. *
  1062. * Transitions to TRANSFER_RETRANSFER:
  1063. * 1) TRANSFER_RECALLING: Recall target hangs up or time expires.
  1064. * atxferloopdelay is zero.
  1065. * 2) TRANSFER_WAIT_TO_RETRANSFER: Time expires.
  1066. *
  1067. * State operation:
  1068. * The original transfer target is requested and is set as the recall target.
  1069. * The recall target is called and placed into the transferee bridge.
  1070. *
  1071. * Transitions from TRANSFER_RETRANSFER:
  1072. * 1) TRANSFER_FAIL: Transferee hangs up.
  1073. * 2) TRANSFER_WAIT_TO_RECALL: Recall target hangs up or time expires.
  1074. * atxferloopdelay is non-zero.
  1075. * 3) TRANSFER_RECALLING: Recall target hangs up or time expires.
  1076. * atxferloopdelay is zero.
  1077. */
  1078. TRANSFER_RETRANSFER,
  1079. /*!
  1080. * \brief Wait to recall state
  1081. *
  1082. * This state is used simply to give a bit of breathing room between attempting
  1083. * to call back the original transfer target and attempting to call back the
  1084. * original transferer. The transferee hears music on hold during this state as an
  1085. * auditory clue that no one is currently being dialed.
  1086. *
  1087. * Superstate: Recall
  1088. *
  1089. * Preconditions:
  1090. * 1) Recall target is down.
  1091. * 2) Transferee is off hold.
  1092. *
  1093. * Transitions to TRANSFER_WAIT_TO_RECALL:
  1094. * 1) TRANSFER_RETRANSFER: Recall target hangs up or time expires.
  1095. * atxferloopdelay is non-zero.
  1096. *
  1097. * State operation:
  1098. * Transferee is placed on hold.
  1099. *
  1100. * Transitions from TRANSFER_WAIT_TO_RECALL:
  1101. * 1) TRANSFER_FAIL: Transferee hangs up
  1102. * 2) TRANSFER_RECALLING: Time expires
  1103. */
  1104. TRANSFER_WAIT_TO_RECALL,
  1105. /*!
  1106. * \brief Fail state
  1107. *
  1108. * This state indicates that something occurred during the transfer that
  1109. * makes a graceful completion impossible. The most common stimulus for this
  1110. * state is when the transferee hangs up.
  1111. *
  1112. * Superstate: Transfer and Recall
  1113. *
  1114. * Preconditions:
  1115. * None
  1116. *
  1117. * Transitions to TRANSFER_FAIL:
  1118. * 1) TRANSFER_CALLING_TARGET: Transferee hangs up.
  1119. * 2) TRANSFER_HESITANT: Transferee hangs up.
  1120. * 3) TRANSFER_DOUBLECHECKING: Transferee hangs up.
  1121. * 4) TRANSFER_BLOND_NONFINAL: Transferee hangs up.
  1122. * 5) TRANSFER_RECALLING:
  1123. * a) Transferee hangs up.
  1124. * b) Recall target hangs up or time expires, and number of
  1125. * recall attempts exceeds atxfercallbackretries.
  1126. * 6) TRANSFER_WAIT_TO_RETRANSFER: Transferee hangs up.
  1127. * 7) TRANSFER_RETRANSFER: Transferee hangs up.
  1128. * 8) TRANSFER_WAIT_TO_RECALL: Transferee hangs up.
  1129. *
  1130. * State operation:
  1131. * A transfer stasis publication is made indicating a failed transfer.
  1132. * The transferee bridge is destroyed.
  1133. *
  1134. * Transitions from TRANSFER_FAIL:
  1135. * None.
  1136. */
  1137. TRANSFER_FAIL,
  1138. };
  1139. /*!
  1140. * \brief Stimuli that can cause transfer state changes
  1141. */
  1142. enum attended_transfer_stimulus {
  1143. /*! No stimulus. This literally can never happen. */
  1144. STIMULUS_NONE,
  1145. /*! All of the transferee channels have been hung up. */
  1146. STIMULUS_TRANSFEREE_HANGUP,
  1147. /*! The transferer has hung up. */
  1148. STIMULUS_TRANSFERER_HANGUP,
  1149. /*! The transfer target channel has hung up. */
  1150. STIMULUS_TRANSFER_TARGET_HANGUP,
  1151. /*! The transfer target channel has answered. */
  1152. STIMULUS_TRANSFER_TARGET_ANSWER,
  1153. /*! The recall target channel has hung up. */
  1154. STIMULUS_RECALL_TARGET_HANGUP,
  1155. /*! The recall target channel has answered. */
  1156. STIMULUS_RECALL_TARGET_ANSWER,
  1157. /*! The current state's timer has expired. */
  1158. STIMULUS_TIMEOUT,
  1159. /*! The transferer pressed the abort DTMF sequence. */
  1160. STIMULUS_DTMF_ATXFER_ABORT,
  1161. /*! The transferer pressed the complete DTMF sequence. */
  1162. STIMULUS_DTMF_ATXFER_COMPLETE,
  1163. /*! The transferer pressed the three-way DTMF sequence. */
  1164. STIMULUS_DTMF_ATXFER_THREEWAY,
  1165. /*! The transferer pressed the swap DTMF sequence. */
  1166. STIMULUS_DTMF_ATXFER_SWAP,
  1167. };
  1168. /*!
  1169. * \brief String representations of the various stimuli
  1170. *
  1171. * Used for debugging purposes
  1172. */
  1173. const char *stimulus_strs[] = {
  1174. [STIMULUS_NONE] = "None",
  1175. [STIMULUS_TRANSFEREE_HANGUP] = "Transferee Hangup",
  1176. [STIMULUS_TRANSFERER_HANGUP] = "Transferer Hangup",
  1177. [STIMULUS_TRANSFER_TARGET_HANGUP] = "Transfer Target Hangup",
  1178. [STIMULUS_TRANSFER_TARGET_ANSWER] = "Transfer Target Answer",
  1179. [STIMULUS_RECALL_TARGET_HANGUP] = "Recall Target Hangup",
  1180. [STIMULUS_RECALL_TARGET_ANSWER] = "Recall Target Answer",
  1181. [STIMULUS_TIMEOUT] = "Timeout",
  1182. [STIMULUS_DTMF_ATXFER_ABORT] = "DTMF Abort",
  1183. [STIMULUS_DTMF_ATXFER_COMPLETE] = "DTMF Complete",
  1184. [STIMULUS_DTMF_ATXFER_THREEWAY] = "DTMF Threeway",
  1185. [STIMULUS_DTMF_ATXFER_SWAP] = "DTMF Swap",
  1186. };
  1187. struct stimulus_list {
  1188. enum attended_transfer_stimulus stimulus;
  1189. AST_LIST_ENTRY(stimulus_list) next;
  1190. };
  1191. /*!
  1192. * \brief Collection of data related to an attended transfer attempt
  1193. */
  1194. struct attended_transfer_properties {
  1195. AST_DECLARE_STRING_FIELDS (
  1196. /*! Extension of transfer target */
  1197. AST_STRING_FIELD(exten);
  1198. /*! Context of transfer target */
  1199. AST_STRING_FIELD(context);
  1200. /*! Sound to play when transfer completes */
  1201. AST_STRING_FIELD(xfersound);
  1202. /*! The channel technology of the transferer channel */
  1203. AST_STRING_FIELD(transferer_type);
  1204. /*! The transferer channel address */
  1205. AST_STRING_FIELD(transferer_addr);
  1206. );
  1207. /*! Condition used to synchronize when stimuli are reported to the monitor thread */
  1208. ast_cond_t cond;
  1209. /*! The bridge where the transferee resides. This bridge is also the bridge that
  1210. * survives a successful attended transfer.
  1211. */
  1212. struct ast_bridge *transferee_bridge;
  1213. /*! The bridge used to place an outbound call to the transfer target. This
  1214. * bridge is merged with the transferee_bridge on a successful transfer.
  1215. */
  1216. struct ast_bridge *target_bridge;
  1217. /*! The party that performs the attended transfer. */
  1218. struct ast_channel *transferer;
  1219. /*! The local channel dialed to reach the transfer target. */
  1220. struct ast_channel *transfer_target;
  1221. /*! The party that is currently being recalled. Depending on
  1222. * the current state, this may be either the party that originally
  1223. * was the transferer or the original transfer target. This is
  1224. * set with reference when entering the BLOND_NONFINAL, RECALLING,
  1225. * and RETRANSFER states, and the reference released on state exit
  1226. * if continuing with recall or retransfer to avoid leak.
  1227. */
  1228. struct ast_channel *recall_target;
  1229. /*! The absolute starting time for running timers */
  1230. struct timeval start;
  1231. AST_LIST_HEAD_NOLOCK(,stimulus_list) stimulus_queue;
  1232. /*! The current state of the attended transfer */
  1233. enum attended_transfer_state state;
  1234. /*! The current superstate of the attended transfer */
  1235. enum attended_transfer_superstate superstate;
  1236. /*! Configured atxferdropcall from features.conf */
  1237. int atxferdropcall;
  1238. /*! Configured atxfercallbackretries from features.conf */
  1239. int atxfercallbackretries;
  1240. /*! Configured atxferloopdelay from features.conf */
  1241. int atxferloopdelay;
  1242. /*! Configured atxfernoanswertimeout from features.conf */
  1243. int atxfernoanswertimeout;
  1244. /*! Count of the number of times that recalls have been attempted */
  1245. int retry_attempts;
  1246. /*! Framehook ID for outbounc call to transfer target or recall target */
  1247. int target_framehook_id;
  1248. /*! Dial structure used when recalling transferer channel */
  1249. struct ast_dial *dial;
  1250. /*! The bridging features the transferer has available */
  1251. struct ast_flags transferer_features;
  1252. /*! Saved transferer connected line data for recalling the transferer. */
  1253. struct ast_party_connected_line original_transferer_colp;
  1254. };
  1255. static void attended_transfer_properties_destructor(void *obj)
  1256. {
  1257. struct attended_transfer_properties *props = obj;
  1258. ast_debug(1, "Destroy attended transfer properties %p\n", props);
  1259. ao2_cleanup(props->target_bridge);
  1260. ao2_cleanup(props->transferee_bridge);
  1261. /* Use ast_channel_cleanup() instead of ast_channel_unref() for channels since they may be NULL */
  1262. ast_channel_cleanup(props->transferer);
  1263. ast_channel_cleanup(props->transfer_target);
  1264. ast_channel_cleanup(props->recall_target);
  1265. ast_party_connected_line_free(&props->original_transferer_colp);
  1266. ast_string_field_free_memory(props);
  1267. ast_cond_destroy(&props->cond);
  1268. }
  1269. /*!
  1270. * \internal
  1271. * \brief Determine the transfer context to use.
  1272. * \since 12.0.0
  1273. *
  1274. * \param transferer Channel initiating the transfer.
  1275. * \param context User supplied context if available. May be NULL.
  1276. *
  1277. * \return The context to use for the transfer.
  1278. */
  1279. static const char *get_transfer_context(struct ast_channel *transferer, const char *context)
  1280. {
  1281. if (!ast_strlen_zero(context)) {
  1282. return context;
  1283. }
  1284. context = pbx_builtin_getvar_helper(transferer, "TRANSFER_CONTEXT");
  1285. if (!ast_strlen_zero(context)) {
  1286. return context;
  1287. }
  1288. context = ast_channel_context(transferer);
  1289. if (!ast_strlen_zero(context)) {
  1290. return context;
  1291. }
  1292. return "default";
  1293. }
  1294. /*!
  1295. * \internal
  1296. * \brief Determine the transfer extension to use.
  1297. *
  1298. * \param transferer Channel initiating the transfer.
  1299. * \param exten User supplied extension if available. May be NULL.
  1300. *
  1301. * \return The extension to use for the transfer.
  1302. */
  1303. static const char *get_transfer_exten(struct ast_channel *transferer, const char *exten)
  1304. {
  1305. if (!ast_strlen_zero(exten)) {
  1306. return exten;
  1307. }
  1308. exten = pbx_builtin_getvar_helper(transferer, "TRANSFER_EXTEN");
  1309. if (!ast_strlen_zero(exten)) {
  1310. return exten;
  1311. }
  1312. return ""; /* empty default, to get transfer extension from user now */
  1313. }
  1314. /*!
  1315. * \brief Allocate and initialize attended transfer properties
  1316. *
  1317. * \param transferer The channel performing the attended transfer
  1318. * \param context Suggestion for what context the transfer target extension can be found in
  1319. *
  1320. * \retval NULL Failure to allocate or initialize
  1321. * \retval non-NULL Newly allocated properties
  1322. */
  1323. static struct attended_transfer_properties *attended_transfer_properties_alloc(
  1324. struct ast_channel *transferer, const char *context)
  1325. {
  1326. struct attended_transfer_properties *props;
  1327. char *tech;
  1328. char *addr;
  1329. char *serial;
  1330. struct ast_features_xfer_config *xfer_cfg;
  1331. struct ast_flags *transferer_features;
  1332. props = ao2_alloc(sizeof(*props), attended_transfer_properties_destructor);
  1333. if (!props) {
  1334. ast_log(LOG_ERROR, "Unable to create props - channel %s, context %s\n",
  1335. ast_channel_name(transferer), context);
  1336. return NULL;
  1337. }
  1338. ast_cond_init(&props->cond, NULL);
  1339. if (ast_string_field_init(props, 64)) {
  1340. ast_log(LOG_ERROR, "Unable to initialize prop fields - channel %s, context %s\n",
  1341. ast_channel_name(transferer), context);
  1342. ao2_ref(props, -1);
  1343. return NULL;
  1344. }
  1345. props->target_framehook_id = -1;
  1346. props->transferer = ast_channel_ref(transferer);
  1347. ast_channel_lock(props->transferer);
  1348. xfer_cfg = ast_get_chan_features_xfer_config(props->transferer);
  1349. if (!xfer_cfg) {
  1350. ast_log(LOG_ERROR, "Unable to get transfer configuration from channel %s\n", ast_channel_name(props->transferer));
  1351. ast_channel_unlock(props->transferer);
  1352. ao2_ref(props, -1);
  1353. return NULL;
  1354. }
  1355. transferer_features = ast_bridge_features_ds_get(props->transferer);
  1356. if (transferer_features) {
  1357. props->transferer_features = *transferer_features;
  1358. }
  1359. props->atxferdropcall = xfer_cfg->atxferdropcall;
  1360. props->atxfercallbackretries = xfer_cfg->atxfercallbackretries;
  1361. props->atxfernoanswertimeout = xfer_cfg->atxfernoanswertimeout;
  1362. props->atxferloopdelay = xfer_cfg->atxferloopdelay;
  1363. ast_string_field_set(props, context, get_transfer_context(transferer, context));
  1364. ast_string_field_set(props, xfersound, xfer_cfg->xfersound);
  1365. ao2_ref(xfer_cfg, -1);
  1366. /*
  1367. * Save the transferee's party information for any recall calls.
  1368. * This is the only piece of information needed that gets overwritten
  1369. * on the transferer channel by the inital call to the transfer target.
  1370. */
  1371. ast_party_connected_line_copy(&props->original_transferer_colp,
  1372. ast_channel_connected(props->transferer));
  1373. tech = ast_strdupa(ast_channel_name(props->transferer));
  1374. addr = strchr(tech, '/');
  1375. if (!addr) {
  1376. ast_log(LOG_ERROR, "Transferer channel name does not follow typical channel naming format (tech/address)\n");
  1377. ast_channel_unlock(props->transferer);
  1378. ao2_ref(props, -1);
  1379. return NULL;
  1380. }
  1381. *addr++ = '\0';
  1382. serial = strrchr(addr, '-');
  1383. if (serial) {
  1384. *serial = '\0';
  1385. }
  1386. ast_string_field_set(props, transferer_type, tech);
  1387. ast_string_field_set(props, transferer_addr, addr);
  1388. ast_channel_unlock(props->transferer);
  1389. ast_debug(1, "Allocated attended transfer properties %p for transfer from %s\n",
  1390. props, ast_channel_name(props->transferer));
  1391. return props;
  1392. }
  1393. /*!
  1394. * \brief Free backlog of stimuli in the queue
  1395. */
  1396. static void clear_stimulus_queue(struct attended_transfer_properties *props)
  1397. {
  1398. struct stimulus_list *list;
  1399. SCOPED_AO2LOCK(lock, props);
  1400. while ((list = AST_LIST_REMOVE_HEAD(&props->stimulus_queue, next))) {
  1401. ast_free(list);
  1402. }
  1403. }
  1404. /*!
  1405. * \brief Initiate shutdown of attended transfer properties
  1406. *
  1407. * Calling this indicates that the attended transfer properties are no longer needed
  1408. * because the transfer operation has concluded.
  1409. */
  1410. static void attended_transfer_properties_shutdown(struct attended_transfer_properties *props)
  1411. {
  1412. ast_debug(1, "Shutting down attended transfer %p\n", props);
  1413. if (props->transferee_bridge) {
  1414. bridge_basic_change_personality(props->transferee_bridge,
  1415. BRIDGE_BASIC_PERSONALITY_NORMAL, NULL);
  1416. ast_bridge_merge_inhibit(props->transferee_bridge, -1);
  1417. }
  1418. if (props->target_bridge) {
  1419. ast_bridge_destroy(props->target_bridge, 0);
  1420. props->target_bridge = NULL;
  1421. }
  1422. if (props->transferer) {
  1423. ast_channel_remove_bridge_role(props->transferer, AST_TRANSFERER_ROLE_NAME);
  1424. }
  1425. clear_stimulus_queue(props);
  1426. ao2_cleanup(props);
  1427. }
  1428. static void stimulate_attended_transfer(struct attended_transfer_properties *props,
  1429. enum attended_transfer_stimulus stimulus)
  1430. {
  1431. struct stimulus_list *list;
  1432. list = ast_calloc(1, sizeof(*list));
  1433. if (!list) {
  1434. ast_log(LOG_ERROR, "Unable to push event to attended transfer queue. Expect transfer to fail\n");
  1435. return;
  1436. }
  1437. list->stimulus = stimulus;
  1438. ao2_lock(props);
  1439. AST_LIST_INSERT_TAIL(&props->stimulus_queue, list, next);
  1440. ast_cond_signal(&props->cond);
  1441. ao2_unlock(props);
  1442. }
  1443. static void remove_attended_transfer_stimulus(struct attended_transfer_properties *props,
  1444. enum attended_transfer_stimulus stimulus)
  1445. {
  1446. struct stimulus_list *list;
  1447. ao2_lock(props);
  1448. AST_LIST_TRAVERSE_SAFE_BEGIN(&props->stimulus_queue, list, next) {
  1449. if (list->stimulus == stimulus) {
  1450. AST_LIST_REMOVE_CURRENT(next);
  1451. ast_free(list);
  1452. break;
  1453. }
  1454. }
  1455. AST_LIST_TRAVERSE_SAFE_END;
  1456. ao2_unlock(props);
  1457. }
  1458. /*!
  1459. * \brief Get a desired transfer party for a bridge the transferer is not in.
  1460. *
  1461. * \param bridge The bridge to get the party from. May be NULL.
  1462. * \param[out] party The lone channel in the bridge. Will be set NULL if bridge is NULL or multiple parties are present.
  1463. */
  1464. static void get_transfer_party_non_transferer_bridge(struct ast_bridge *bridge,
  1465. struct ast_channel **party)
  1466. {
  1467. if (bridge && bridge->num_channels == 1) {
  1468. *party = ast_channel_ref(AST_LIST_FIRST(&bridge->channels)->chan);
  1469. } else {
  1470. *party = NULL;
  1471. }
  1472. }
  1473. /*!
  1474. * \brief Get the transferee and transfer target when the transferer is in a bridge with
  1475. * one of the desired parties.
  1476. *
  1477. * \param transferer_bridge The bridge the transferer is in
  1478. * \param other_bridge The bridge the transferer is not in. May be NULL.
  1479. * \param transferer The transferer party
  1480. * \param[out] transferer_peer The party that is in the bridge with the transferer
  1481. * \param[out] other_party The party that is in the other_bridge
  1482. */
  1483. static void get_transfer_parties_transferer_bridge(struct ast_bridge *transferer_bridge,
  1484. struct ast_bridge *other_bridge, struct ast_channel *transferer,
  1485. struct ast_channel **transferer_peer, struct ast_channel **other_party)
  1486. {
  1487. *transferer_peer = ast_bridge_peer(transferer_bridge, transferer);
  1488. get_transfer_party_non_transferer_bridge(other_bridge, other_party);
  1489. }
  1490. /*!
  1491. * \brief determine transferee and transfer target for an attended transfer
  1492. *
  1493. * In builtin attended transfers, there is a single transferer channel that jumps between
  1494. * the two bridges involved. At the time the attended transfer occurs, the transferer could
  1495. * be in either bridge, so determining the parties is a bit more complex than normal.
  1496. *
  1497. * The method used here is to determine which of the two bridges the transferer is in, and
  1498. * grabbing the peer from that bridge. The other bridge, if it only has a single channel in it,
  1499. * has the other desired channel.
  1500. *
  1501. * \param transferer The channel performing the transfer
  1502. * \param transferee_bridge The bridge that the transferee is in
  1503. * \param target_bridge The bridge that the transfer target is in
  1504. * \param[out] transferee The transferee channel
  1505. * \param[out] transfer_target The transfer target channel
  1506. */
  1507. static void get_transfer_parties(struct ast_channel *transferer, struct ast_bridge *transferee_bridge,
  1508. struct ast_bridge *target_bridge, struct ast_channel **transferee,
  1509. struct ast_channel **transfer_target)
  1510. {
  1511. struct ast_bridge *transferer_bridge;
  1512. ast_channel_lock(transferer);
  1513. transferer_bridge = ast_channel_get_bridge(transferer);
  1514. ast_channel_unlock(transferer);
  1515. if (transferer_bridge == transferee_bridge) {
  1516. get_transfer_parties_transferer_bridge(transferee_bridge, target_bridge,
  1517. transferer, transferee, transfer_target);
  1518. } else if (transferer_bridge == target_bridge) {
  1519. get_transfer_parties_transferer_bridge(target_bridge, transferee_bridge,
  1520. transferer, transfer_target, transferee);
  1521. } else {
  1522. get_transfer_party_non_transferer_bridge(transferee_bridge, transferee);
  1523. get_transfer_party_non_transferer_bridge(target_bridge, transfer_target);
  1524. }
  1525. ao2_cleanup(transferer_bridge);
  1526. }
  1527. /*!
  1528. * \brief Send a stasis publication for a successful attended transfer
  1529. */
  1530. static void publish_transfer_success(struct attended_transfer_properties *props,
  1531. struct ast_channel *transferee_channel, struct ast_channel *target_channel)
  1532. {
  1533. struct ast_attended_transfer_message *transfer_msg;
  1534. transfer_msg = ast_attended_transfer_message_create(0, props->transferer,
  1535. props->transferee_bridge, props->transferer, props->target_bridge,
  1536. transferee_channel, target_channel);
  1537. if (!transfer_msg) {
  1538. ast_log(LOG_ERROR, "Unable to publish successful attended transfer from %s\n",
  1539. ast_channel_name(props->transferer));
  1540. return;
  1541. }
  1542. ast_attended_transfer_message_add_merge(transfer_msg, props->transferee_bridge);
  1543. ast_bridge_publish_attended_transfer(transfer_msg);
  1544. ao2_cleanup(transfer_msg);
  1545. }
  1546. /*!
  1547. * \brief Send a stasis publication for an attended transfer that ends in a threeway call
  1548. */
  1549. static void publish_transfer_threeway(struct attended_transfer_properties *props,
  1550. struct ast_channel *transferee_channel, struct ast_channel *target_channel)
  1551. {
  1552. struct ast_attended_transfer_message *transfer_msg;
  1553. transfer_msg = ast_attended_transfer_message_create(0, props->transferer,
  1554. props->transferee_bridge, props->transferer, props->target_bridge,
  1555. transferee_channel, target_channel);
  1556. if (!transfer_msg) {
  1557. ast_log(LOG_ERROR, "Unable to publish successful three-way transfer from %s\n",
  1558. ast_channel_name(props->transferer));
  1559. return;
  1560. }
  1561. ast_attended_transfer_message_add_threeway(transfer_msg, props->transferer,
  1562. props->transferee_bridge);
  1563. ast_bridge_publish_attended_transfer(transfer_msg);
  1564. ao2_cleanup(transfer_msg);
  1565. }
  1566. /*!
  1567. * \brief Send a stasis publication for a failed attended transfer
  1568. */
  1569. static void publish_transfer_fail(struct attended_transfer_properties *props)
  1570. {
  1571. struct ast_attended_transfer_message *transfer_msg;
  1572. transfer_msg = ast_attended_transfer_message_create(0, props->transferer,
  1573. props->transferee_bridge, props->transferer, props->target_bridge,
  1574. NULL, NULL);
  1575. if (!transfer_msg) {
  1576. ast_log(LOG_ERROR, "Unable to publish failed transfer from %s\n",
  1577. ast_channel_name(props->transferer));
  1578. return;
  1579. }
  1580. transfer_msg->result = AST_BRIDGE_TRANSFER_FAIL;
  1581. ast_bridge_publish_attended_transfer(transfer_msg);
  1582. ao2_cleanup(transfer_msg);
  1583. }
  1584. /*!
  1585. * \brief Helper method to play a sound on a channel in a bridge
  1586. *
  1587. * \param chan The channel to play the sound to
  1588. * \param sound The sound to play
  1589. */
  1590. static void play_sound(struct ast_channel *chan, const char *sound)
  1591. {
  1592. struct ast_bridge_channel *bridge_channel;
  1593. ast_channel_lock(chan);
  1594. bridge_channel = ast_channel_get_bridge_channel(chan);
  1595. ast_channel_unlock(chan);
  1596. if (bridge_channel) {
  1597. ast_bridge_channel_queue_playfile(bridge_channel, NULL, sound, NULL);
  1598. ao2_ref(bridge_channel, -1);
  1599. }
  1600. }
  1601. /*!
  1602. * \brief Helper method to play a fail sound on a channel in a bridge
  1603. *
  1604. * \param chan The channel to play the fail sound to
  1605. */
  1606. static void play_failsound(struct ast_channel *chan)
  1607. {
  1608. char *sound;
  1609. ast_channel_lock(chan);
  1610. sound = ast_get_chan_features_xferfailsound(chan);
  1611. ast_channel_unlock(chan);
  1612. if (sound) {
  1613. play_sound(chan, sound);
  1614. ast_free(sound);
  1615. }
  1616. }
  1617. /*!
  1618. * \brief Helper method to stream a fail sound on a channel
  1619. *
  1620. * \param chan The channel to stream the fail sound to
  1621. */
  1622. static void stream_failsound(struct ast_channel *chan)
  1623. {
  1624. char *sound;
  1625. ast_channel_lock(chan);
  1626. sound = ast_get_chan_features_xferfailsound(chan);
  1627. ast_channel_unlock(chan);
  1628. if (sound) {
  1629. ast_stream_and_wait(chan, sound, AST_DIGIT_NONE);
  1630. ast_free(sound);
  1631. }
  1632. }
  1633. /*!
  1634. * \brief Helper method to place a channel in a bridge on hold
  1635. */
  1636. static void hold(struct ast_channel *chan)
  1637. {
  1638. struct ast_bridge_channel *bridge_channel;
  1639. if (!chan) {
  1640. return;
  1641. }
  1642. ast_channel_lock(chan);
  1643. bridge_channel = ast_channel_get_bridge_channel(chan);
  1644. ast_channel_unlock(chan);
  1645. if (bridge_channel) {
  1646. ast_bridge_channel_write_hold(bridge_channel, NULL);
  1647. ao2_ref(bridge_channel, -1);
  1648. }
  1649. }
  1650. /*!
  1651. * \brief Helper method to take a channel in a bridge off hold
  1652. */
  1653. static void unhold(struct ast_channel *chan)
  1654. {
  1655. struct ast_bridge_channel *bridge_channel;
  1656. if (!chan) {
  1657. return;
  1658. }
  1659. ast_channel_lock(chan);
  1660. bridge_channel = ast_channel_get_bridge_channel(chan);
  1661. ast_channel_unlock(chan);
  1662. if (bridge_channel) {
  1663. ast_bridge_channel_write_unhold(bridge_channel);
  1664. ao2_ref(bridge_channel, -1);
  1665. }
  1666. }
  1667. /*!
  1668. * \brief Helper method to send a ringing indication to a channel in a bridge
  1669. */
  1670. static void ringing(struct ast_channel *chan)
  1671. {
  1672. struct ast_bridge_channel *bridge_channel;
  1673. ast_channel_lock(chan);
  1674. bridge_channel = ast_channel_get_bridge_channel(chan);
  1675. ast_channel_unlock(chan);
  1676. if (bridge_channel) {
  1677. ast_bridge_channel_write_control_data(bridge_channel, AST_CONTROL_RINGING, NULL, 0);
  1678. ao2_ref(bridge_channel, -1);
  1679. }
  1680. }
  1681. /*!
  1682. * \brief Helper method to send a ringing indication to all channels in a bridge
  1683. */
  1684. static void bridge_ringing(struct ast_bridge *bridge)
  1685. {
  1686. struct ast_frame ringing = {
  1687. .frametype = AST_FRAME_CONTROL,
  1688. .subclass.integer = AST_CONTROL_RINGING,
  1689. };
  1690. ast_bridge_lock(bridge);
  1691. ast_bridge_queue_everyone_else(bridge, NULL, &ringing);
  1692. ast_bridge_unlock(bridge);
  1693. }
  1694. /*!
  1695. * \brief Helper method to send a hold frame to all channels in a bridge
  1696. */
  1697. static void bridge_hold(struct ast_bridge *bridge)
  1698. {
  1699. struct ast_frame hold = {
  1700. .frametype = AST_FRAME_CONTROL,
  1701. .subclass.integer = AST_CONTROL_HOLD,
  1702. };
  1703. ast_bridge_lock(bridge);
  1704. ast_bridge_queue_everyone_else(bridge, NULL, &hold);
  1705. ast_bridge_unlock(bridge);
  1706. }
  1707. /*!
  1708. * \brief Helper method to send an unhold frame to all channels in a bridge
  1709. */
  1710. static void bridge_unhold(struct ast_bridge *bridge)
  1711. {
  1712. struct ast_frame unhold = {
  1713. .frametype = AST_FRAME_CONTROL,
  1714. .subclass.integer = AST_CONTROL_UNHOLD,
  1715. };
  1716. ast_bridge_lock(bridge);
  1717. ast_bridge_queue_everyone_else(bridge, NULL, &unhold);
  1718. ast_bridge_unlock(bridge);
  1719. }
  1720. /*!
  1721. * \brief Wrapper for \ref bridge_do_move
  1722. */
  1723. static void bridge_move(struct ast_bridge *dest, struct ast_bridge *src, struct ast_channel *channel, struct ast_channel *swap)
  1724. {
  1725. struct ast_bridge_channel *bridge_channel;
  1726. ast_bridge_lock_both(src, dest);
  1727. ast_channel_lock(channel);
  1728. bridge_channel = ast_channel_get_bridge_channel(channel);
  1729. ast_channel_unlock(channel);
  1730. if (bridge_channel) {
  1731. ao2_lock(bridge_channel);
  1732. bridge_channel->swap = swap;
  1733. ao2_unlock(bridge_channel);
  1734. bridge_do_move(dest, bridge_channel, 1, 0);
  1735. }
  1736. ast_bridge_unlock(dest);
  1737. ast_bridge_unlock(src);
  1738. ao2_cleanup(bridge_channel);
  1739. }
  1740. /*!
  1741. * \brief Wrapper for \ref bridge_do_merge
  1742. */
  1743. static void bridge_merge(struct ast_bridge *dest, struct ast_bridge *src, struct ast_channel **kick_channels, unsigned int num_channels)
  1744. {
  1745. struct ast_bridge_channel **kick_bridge_channels = num_channels ?
  1746. ast_alloca(num_channels * sizeof(*kick_bridge_channels)) : NULL;
  1747. int i;
  1748. int num_bridge_channels = 0;
  1749. ast_bridge_lock_both(dest, src);
  1750. for (i = 0; i < num_channels; ++i) {
  1751. struct ast_bridge_channel *kick_bridge_channel;
  1752. kick_bridge_channel = bridge_find_channel(src, kick_channels[i]);
  1753. if (!kick_bridge_channel) {
  1754. kick_bridge_channel = bridge_find_channel(dest, kick_channels[i]);
  1755. }
  1756. /* It's possible (and fine) for the bridge channel to be NULL at this point if the
  1757. * channel has hung up already. If that happens, we can just remove it from the list
  1758. * of bridge channels to kick from the bridge
  1759. */
  1760. if (!kick_bridge_channel) {
  1761. continue;
  1762. }
  1763. kick_bridge_channels[num_bridge_channels++] = kick_bridge_channel;
  1764. }
  1765. bridge_do_merge(dest, src, kick_bridge_channels, num_bridge_channels, 0);
  1766. ast_bridge_unlock(dest);
  1767. ast_bridge_unlock(src);
  1768. }
  1769. /*!
  1770. * \brief Flags that indicate properties of attended transfer states
  1771. */
  1772. enum attended_transfer_state_flags {
  1773. /*! This state requires that the timer be reset when entering the state */
  1774. TRANSFER_STATE_FLAG_TIMER_RESET = (1 << 0),
  1775. /*! This state's timer uses atxferloopdelay */
  1776. TRANSFER_STATE_FLAG_TIMER_LOOP_DELAY = (1 << 1),
  1777. /*! This state's timer uses atxfernoanswertimeout */
  1778. TRANSFER_STATE_FLAG_ATXFER_NO_ANSWER = (1 << 2),
  1779. /*! This state has a time limit associated with it */
  1780. TRANSFER_STATE_FLAG_TIMED = (TRANSFER_STATE_FLAG_TIMER_RESET |
  1781. TRANSFER_STATE_FLAG_TIMER_LOOP_DELAY | TRANSFER_STATE_FLAG_ATXFER_NO_ANSWER),
  1782. /*! This state does not transition to any other states */
  1783. TRANSFER_STATE_FLAG_TERMINAL = (1 << 3),
  1784. };
  1785. static int calling_target_enter(struct attended_transfer_properties *props);
  1786. static enum attended_transfer_state calling_target_exit(struct attended_transfer_properties *props,
  1787. enum attended_transfer_stimulus stimulus);
  1788. static int hesitant_enter(struct attended_transfer_properties *props);
  1789. static enum attended_transfer_state hesitant_exit(struct attended_transfer_properties *props,
  1790. enum attended_transfer_stimulus stimulus);
  1791. static int rebridge_enter(struct attended_transfer_properties *props);
  1792. static int resume_enter(struct attended_transfer_properties *props);
  1793. static int threeway_enter(struct attended_transfer_properties *props);
  1794. static int consulting_enter(struct attended_transfer_properties *props);
  1795. static enum attended_transfer_state consulting_exit(struct attended_transfer_properties *props,
  1796. enum attended_transfer_stimulus stimulus);
  1797. static int double_checking_enter(struct attended_transfer_properties *props);
  1798. static enum attended_transfer_state double_checking_exit(struct attended_transfer_properties *props,
  1799. enum attended_transfer_stimulus stimulus);
  1800. static int complete_enter(struct attended_transfer_properties *props);
  1801. static int blond_enter(struct attended_transfer_properties *props);
  1802. static int blond_nonfinal_enter(struct attended_transfer_properties *props);
  1803. static enum attended_transfer_state blond_nonfinal_exit(struct attended_transfer_properties *props,
  1804. enum attended_transfer_stimulus stimulus);
  1805. static int recalling_enter(struct attended_transfer_properties *props);
  1806. static enum attended_transfer_state recalling_exit(struct attended_transfer_properties *props,
  1807. enum attended_transfer_stimulus stimulus);
  1808. static int wait_to_retransfer_enter(struct attended_transfer_properties *props);
  1809. static enum attended_transfer_state wait_to_retransfer_exit(struct attended_transfer_properties *props,
  1810. enum attended_transfer_stimulus stimulus);
  1811. static int retransfer_enter(struct attended_transfer_properties *props);
  1812. static enum attended_transfer_state retransfer_exit(struct attended_transfer_properties *props,
  1813. enum attended_transfer_stimulus stimulus);
  1814. static int wait_to_recall_enter(struct attended_transfer_properties *props);
  1815. static enum attended_transfer_state wait_to_recall_exit(struct attended_transfer_properties *props,
  1816. enum attended_transfer_stimulus stimulus);
  1817. static int fail_enter(struct attended_transfer_properties *props);
  1818. /*!
  1819. * \brief Properties of an attended transfer state
  1820. */
  1821. struct attended_transfer_state_properties {
  1822. /*! The name of the state. Used for debugging */
  1823. const char *state_name;
  1824. /*! Function used to enter a state */
  1825. int (*enter)(struct attended_transfer_properties *props);
  1826. /*!
  1827. * Function used to exit a state
  1828. * This is used both to determine what the next state
  1829. * to transition to will be and to perform any cleanup
  1830. * necessary before exiting the current state.
  1831. */
  1832. enum attended_transfer_state (*exit)(struct attended_transfer_properties *props,
  1833. enum attended_transfer_stimulus stimulus);
  1834. /*! Flags associated with this state */
  1835. enum attended_transfer_state_flags flags;
  1836. };
  1837. static const struct attended_transfer_state_properties state_properties[] = {
  1838. [TRANSFER_CALLING_TARGET] = {
  1839. .state_name = "Calling Target",
  1840. .enter = calling_target_enter,
  1841. .exit = calling_target_exit,
  1842. .flags = TRANSFER_STATE_FLAG_ATXFER_NO_ANSWER | TRANSFER_STATE_FLAG_TIMER_RESET,
  1843. },
  1844. [TRANSFER_HESITANT] = {
  1845. .state_name = "Hesitant",
  1846. .enter = hesitant_enter,
  1847. .exit = hesitant_exit,
  1848. .flags = TRANSFER_STATE_FLAG_ATXFER_NO_ANSWER,
  1849. },
  1850. [TRANSFER_REBRIDGE] = {
  1851. .state_name = "Rebridge",
  1852. .enter = rebridge_enter,
  1853. .flags = TRANSFER_STATE_FLAG_TERMINAL,
  1854. },
  1855. [TRANSFER_RESUME] = {
  1856. .state_name = "Resume",
  1857. .enter = resume_enter,
  1858. .flags = TRANSFER_STATE_FLAG_TERMINAL,
  1859. },
  1860. [TRANSFER_THREEWAY] = {
  1861. .state_name = "Threeway",
  1862. .enter = threeway_enter,
  1863. .flags = TRANSFER_STATE_FLAG_TERMINAL,
  1864. },
  1865. [TRANSFER_CONSULTING] = {
  1866. .state_name = "Consulting",
  1867. .enter = consulting_enter,
  1868. .exit = consulting_exit,
  1869. },
  1870. [TRANSFER_DOUBLECHECKING] = {
  1871. .state_name = "Double Checking",
  1872. .enter = double_checking_enter,
  1873. .exit = double_checking_exit,
  1874. },
  1875. [TRANSFER_COMPLETE] = {
  1876. .state_name = "Complete",
  1877. .enter = complete_enter,
  1878. .flags = TRANSFER_STATE_FLAG_TERMINAL,
  1879. },
  1880. [TRANSFER_BLOND] = {
  1881. .state_name = "Blond",
  1882. .enter = blond_enter,
  1883. .flags = TRANSFER_STATE_FLAG_TERMINAL,
  1884. },
  1885. [TRANSFER_BLOND_NONFINAL] = {
  1886. .state_name = "Blond Non-Final",
  1887. .enter = blond_nonfinal_enter,
  1888. .exit = blond_nonfinal_exit,
  1889. .flags = TRANSFER_STATE_FLAG_ATXFER_NO_ANSWER,
  1890. },
  1891. [TRANSFER_RECALLING] = {
  1892. .state_name = "Recalling",
  1893. .enter = recalling_enter,
  1894. .exit = recalling_exit,
  1895. .flags = TRANSFER_STATE_FLAG_ATXFER_NO_ANSWER | TRANSFER_STATE_FLAG_TIMER_RESET,
  1896. },
  1897. [TRANSFER_WAIT_TO_RETRANSFER] = {
  1898. .state_name = "Wait to Retransfer",
  1899. .enter = wait_to_retransfer_enter,
  1900. .exit = wait_to_retransfer_exit,
  1901. .flags = TRANSFER_STATE_FLAG_TIMER_RESET | TRANSFER_STATE_FLAG_TIMER_LOOP_DELAY,
  1902. },
  1903. [TRANSFER_RETRANSFER] = {
  1904. .state_name = "Retransfer",
  1905. .enter = retransfer_enter,
  1906. .exit = retransfer_exit,
  1907. .flags = TRANSFER_STATE_FLAG_ATXFER_NO_ANSWER | TRANSFER_STATE_FLAG_TIMER_RESET,
  1908. },
  1909. [TRANSFER_WAIT_TO_RECALL] = {
  1910. .state_name = "Wait to Recall",
  1911. .enter = wait_to_recall_enter,
  1912. .exit = wait_to_recall_exit,
  1913. .flags = TRANSFER_STATE_FLAG_TIMER_RESET | TRANSFER_STATE_FLAG_TIMER_LOOP_DELAY,
  1914. },
  1915. [TRANSFER_FAIL] = {
  1916. .state_name = "Fail",
  1917. .enter = fail_enter,
  1918. .flags = TRANSFER_STATE_FLAG_TERMINAL,
  1919. },
  1920. };
  1921. static int calling_target_enter(struct attended_transfer_properties *props)
  1922. {
  1923. bridge_move(props->target_bridge, props->transferee_bridge, props->transferer, NULL);
  1924. return 0;
  1925. }
  1926. static enum attended_transfer_state calling_target_exit(struct attended_transfer_properties *props,
  1927. enum attended_transfer_stimulus stimulus)
  1928. {
  1929. switch (stimulus) {
  1930. case STIMULUS_TRANSFEREE_HANGUP:
  1931. play_failsound(props->transferer);
  1932. publish_transfer_fail(props);
  1933. return TRANSFER_FAIL;
  1934. case STIMULUS_DTMF_ATXFER_COMPLETE:
  1935. case STIMULUS_TRANSFERER_HANGUP:
  1936. bridge_unhold(props->transferee_bridge);
  1937. return props->atxferdropcall ? TRANSFER_BLOND : TRANSFER_BLOND_NONFINAL;
  1938. case STIMULUS_TRANSFER_TARGET_ANSWER:
  1939. return TRANSFER_CONSULTING;
  1940. case STIMULUS_TRANSFER_TARGET_HANGUP:
  1941. case STIMULUS_TIMEOUT:
  1942. case STIMULUS_DTMF_ATXFER_ABORT:
  1943. play_failsound(props->transferer);
  1944. return TRANSFER_REBRIDGE;
  1945. case STIMULUS_DTMF_ATXFER_THREEWAY:
  1946. bridge_unhold(props->transferee_bridge);
  1947. return TRANSFER_THREEWAY;
  1948. case STIMULUS_DTMF_ATXFER_SWAP:
  1949. return TRANSFER_HESITANT;
  1950. case STIMULUS_NONE:
  1951. case STIMULUS_RECALL_TARGET_ANSWER:
  1952. case STIMULUS_RECALL_TARGET_HANGUP:
  1953. default:
  1954. ast_log(LOG_WARNING, "Unexpected stimulus '%s' received in attended transfer state '%s'\n",
  1955. stimulus_strs[stimulus], state_properties[props->state].state_name);
  1956. return props->state;
  1957. }
  1958. }
  1959. static int hesitant_enter(struct attended_transfer_properties *props)
  1960. {
  1961. bridge_move(props->transferee_bridge, props->target_bridge, props->transferer, NULL);
  1962. unhold(props->transferer);
  1963. return 0;
  1964. }
  1965. static enum attended_transfer_state hesitant_exit(struct attended_transfer_properties *props,
  1966. enum attended_transfer_stimulus stimulus)
  1967. {
  1968. switch (stimulus) {
  1969. case STIMULUS_TRANSFEREE_HANGUP:
  1970. play_failsound(props->transferer);
  1971. publish_transfer_fail(props);
  1972. return TRANSFER_FAIL;
  1973. case STIMULUS_DTMF_ATXFER_COMPLETE:
  1974. case STIMULUS_TRANSFERER_HANGUP:
  1975. return props->atxferdropcall ? TRANSFER_BLOND : TRANSFER_BLOND_NONFINAL;
  1976. case STIMULUS_TRANSFER_TARGET_ANSWER:
  1977. return TRANSFER_DOUBLECHECKING;
  1978. case STIMULUS_TRANSFER_TARGET_HANGUP:
  1979. case STIMULUS_TIMEOUT:
  1980. case STIMULUS_DTMF_ATXFER_ABORT:
  1981. play_failsound(props->transferer);
  1982. return TRANSFER_RESUME;
  1983. case STIMULUS_DTMF_ATXFER_THREEWAY:
  1984. return TRANSFER_THREEWAY;
  1985. case STIMULUS_DTMF_ATXFER_SWAP:
  1986. hold(props->transferer);
  1987. return TRANSFER_CALLING_TARGET;
  1988. case STIMULUS_NONE:
  1989. case STIMULUS_RECALL_TARGET_HANGUP:
  1990. case STIMULUS_RECALL_TARGET_ANSWER:
  1991. default:
  1992. ast_log(LOG_WARNING, "Unexpected stimulus '%s' received in attended transfer state '%s'\n",
  1993. stimulus_strs[stimulus], state_properties[props->state].state_name);
  1994. return props->state;
  1995. }
  1996. }
  1997. static int rebridge_enter(struct attended_transfer_properties *props)
  1998. {
  1999. bridge_move(props->transferee_bridge, props->target_bridge, props->transferer, NULL);
  2000. unhold(props->transferer);
  2001. return 0;
  2002. }
  2003. static int resume_enter(struct attended_transfer_properties *props)
  2004. {
  2005. return 0;
  2006. }
  2007. static int threeway_enter(struct attended_transfer_properties *props)
  2008. {
  2009. struct ast_channel *transferee_channel;
  2010. struct ast_channel *target_channel;
  2011. get_transfer_parties(props->transferer, props->transferee_bridge, props->target_bridge,
  2012. &transferee_channel, &target_channel);
  2013. bridge_merge(props->transferee_bridge, props->target_bridge, NULL, 0);
  2014. play_sound(props->transfer_target, props->xfersound);
  2015. play_sound(props->transferer, props->xfersound);
  2016. publish_transfer_threeway(props, transferee_channel, target_channel);
  2017. ast_channel_cleanup(transferee_channel);
  2018. ast_channel_cleanup(target_channel);
  2019. return 0;
  2020. }
  2021. static int consulting_enter(struct attended_transfer_properties *props)
  2022. {
  2023. return 0;
  2024. }
  2025. static enum attended_transfer_state consulting_exit(struct attended_transfer_properties *props,
  2026. enum attended_transfer_stimulus stimulus)
  2027. {
  2028. switch (stimulus) {
  2029. case STIMULUS_TRANSFEREE_HANGUP:
  2030. /* This is a one-of-a-kind event. The transferer and transfer target are talking in
  2031. * one bridge, and the transferee has hung up in a separate bridge. In this case, we
  2032. * will change the personality of the transfer target bridge back to normal, and play
  2033. * a sound to the transferer to indicate the transferee is gone.
  2034. */
  2035. bridge_basic_change_personality(props->target_bridge, BRIDGE_BASIC_PERSONALITY_NORMAL, NULL);
  2036. play_failsound(props->transferer);
  2037. ast_bridge_merge_inhibit(props->target_bridge, -1);
  2038. /* These next two lines are here to ensure that our reference to the target bridge
  2039. * is cleaned up properly and that the target bridge is not destroyed when the
  2040. * monitor thread exits
  2041. */
  2042. ao2_ref(props->target_bridge, -1);
  2043. props->target_bridge = NULL;
  2044. return TRANSFER_FAIL;
  2045. case STIMULUS_TRANSFERER_HANGUP:
  2046. case STIMULUS_DTMF_ATXFER_COMPLETE:
  2047. /* We know the transferer is in the target_bridge, so take the other bridge off hold */
  2048. bridge_unhold(props->transferee_bridge);
  2049. return TRANSFER_COMPLETE;
  2050. case STIMULUS_TRANSFER_TARGET_HANGUP:
  2051. return TRANSFER_REBRIDGE;
  2052. case STIMULUS_DTMF_ATXFER_ABORT:
  2053. play_failsound(props->transferer);
  2054. return TRANSFER_REBRIDGE;
  2055. case STIMULUS_DTMF_ATXFER_THREEWAY:
  2056. bridge_unhold(props->transferee_bridge);
  2057. return TRANSFER_THREEWAY;
  2058. case STIMULUS_DTMF_ATXFER_SWAP:
  2059. hold(props->transferer);
  2060. bridge_move(props->transferee_bridge, props->target_bridge, props->transferer, NULL);
  2061. unhold(props->transferer);
  2062. return TRANSFER_DOUBLECHECKING;
  2063. case STIMULUS_NONE:
  2064. case STIMULUS_TIMEOUT:
  2065. case STIMULUS_TRANSFER_TARGET_ANSWER:
  2066. case STIMULUS_RECALL_TARGET_HANGUP:
  2067. case STIMULUS_RECALL_TARGET_ANSWER:
  2068. default:
  2069. ast_log(LOG_WARNING, "Unexpected stimulus '%s' received in attended transfer state '%s'\n",
  2070. stimulus_strs[stimulus], state_properties[props->state].state_name);
  2071. return props->state;
  2072. }
  2073. }
  2074. static int double_checking_enter(struct attended_transfer_properties *props)
  2075. {
  2076. return 0;
  2077. }
  2078. static enum attended_transfer_state double_checking_exit(struct attended_transfer_properties *props,
  2079. enum attended_transfer_stimulus stimulus)
  2080. {
  2081. switch (stimulus) {
  2082. case STIMULUS_TRANSFEREE_HANGUP:
  2083. play_failsound(props->transferer);
  2084. publish_transfer_fail(props);
  2085. return TRANSFER_FAIL;
  2086. case STIMULUS_TRANSFERER_HANGUP:
  2087. case STIMULUS_DTMF_ATXFER_COMPLETE:
  2088. /* We know the transferer is in the transferee, so take the other bridge off hold */
  2089. bridge_unhold(props->target_bridge);
  2090. return TRANSFER_COMPLETE;
  2091. case STIMULUS_TRANSFER_TARGET_HANGUP:
  2092. case STIMULUS_DTMF_ATXFER_ABORT:
  2093. play_failsound(props->transferer);
  2094. return TRANSFER_RESUME;
  2095. case STIMULUS_DTMF_ATXFER_THREEWAY:
  2096. bridge_unhold(props->target_bridge);
  2097. return TRANSFER_THREEWAY;
  2098. case STIMULUS_DTMF_ATXFER_SWAP:
  2099. hold(props->transferer);
  2100. bridge_move(props->target_bridge, props->transferee_bridge, props->transferer, NULL);
  2101. unhold(props->transferer);
  2102. return TRANSFER_CONSULTING;
  2103. case STIMULUS_NONE:
  2104. case STIMULUS_TIMEOUT:
  2105. case STIMULUS_TRANSFER_TARGET_ANSWER:
  2106. case STIMULUS_RECALL_TARGET_HANGUP:
  2107. case STIMULUS_RECALL_TARGET_ANSWER:
  2108. default:
  2109. ast_log(LOG_WARNING, "Unexpected stimulus '%s' received in attended transfer state '%s'\n",
  2110. stimulus_strs[stimulus], state_properties[props->state].state_name);
  2111. return props->state;
  2112. }
  2113. }
  2114. static int complete_enter(struct attended_transfer_properties *props)
  2115. {
  2116. struct ast_channel *transferee_channel;
  2117. struct ast_channel *target_channel;
  2118. get_transfer_parties(props->transferer, props->transferee_bridge, props->target_bridge,
  2119. &transferee_channel, &target_channel);
  2120. bridge_merge(props->transferee_bridge, props->target_bridge, &props->transferer, 1);
  2121. play_sound(props->transfer_target, props->xfersound);
  2122. publish_transfer_success(props, transferee_channel, target_channel);
  2123. ast_channel_cleanup(transferee_channel);
  2124. ast_channel_cleanup(target_channel);
  2125. return 0;
  2126. }
  2127. static int blond_enter(struct attended_transfer_properties *props)
  2128. {
  2129. struct ast_channel *transferee_channel;
  2130. struct ast_channel *target_channel;
  2131. get_transfer_parties(props->transferer, props->transferee_bridge, props->target_bridge,
  2132. &transferee_channel, &target_channel);
  2133. bridge_merge(props->transferee_bridge, props->target_bridge, &props->transferer, 1);
  2134. ringing(props->transfer_target);
  2135. publish_transfer_success(props, transferee_channel, target_channel);
  2136. ast_channel_cleanup(transferee_channel);
  2137. ast_channel_cleanup(target_channel);
  2138. return 0;
  2139. }
  2140. static int blond_nonfinal_enter(struct attended_transfer_properties *props)
  2141. {
  2142. int res;
  2143. props->superstate = SUPERSTATE_RECALL;
  2144. /* move the transfer target to the recall target along with its reference */
  2145. props->recall_target = ast_channel_ref(props->transfer_target);
  2146. res = blond_enter(props);
  2147. props->transfer_target = ast_channel_unref(props->transfer_target);
  2148. return res;
  2149. }
  2150. static enum attended_transfer_state blond_nonfinal_exit(struct attended_transfer_properties *props,
  2151. enum attended_transfer_stimulus stimulus)
  2152. {
  2153. switch (stimulus) {
  2154. case STIMULUS_TRANSFEREE_HANGUP:
  2155. return TRANSFER_FAIL;
  2156. case STIMULUS_RECALL_TARGET_ANSWER:
  2157. return TRANSFER_RESUME;
  2158. case STIMULUS_TIMEOUT:
  2159. ast_softhangup(props->recall_target, AST_SOFTHANGUP_EXPLICIT);
  2160. /* It is possible before we hung them up that they queued up a recall target answer
  2161. * so we remove it if present as it should not exist.
  2162. */
  2163. remove_attended_transfer_stimulus(props, STIMULUS_RECALL_TARGET_ANSWER);
  2164. case STIMULUS_RECALL_TARGET_HANGUP:
  2165. props->recall_target = ast_channel_unref(props->recall_target);
  2166. return TRANSFER_RECALLING;
  2167. case STIMULUS_NONE:
  2168. case STIMULUS_DTMF_ATXFER_ABORT:
  2169. case STIMULUS_DTMF_ATXFER_COMPLETE:
  2170. case STIMULUS_DTMF_ATXFER_THREEWAY:
  2171. case STIMULUS_DTMF_ATXFER_SWAP:
  2172. case STIMULUS_TRANSFERER_HANGUP:
  2173. case STIMULUS_TRANSFER_TARGET_HANGUP:
  2174. case STIMULUS_TRANSFER_TARGET_ANSWER:
  2175. default:
  2176. ast_log(LOG_WARNING, "Unexpected stimulus '%s' received in attended transfer state '%s'\n",
  2177. stimulus_strs[stimulus], state_properties[props->state].state_name);
  2178. return props->state;
  2179. }
  2180. }
  2181. /*!
  2182. * \brief Dial callback when attempting to recall the original transferer channel
  2183. *
  2184. * This is how we can monitor if the recall target has answered or has hung up.
  2185. * If one of the two is detected, then an appropriate stimulus is sent to the
  2186. * attended transfer monitor thread.
  2187. */
  2188. static void recall_callback(struct ast_dial *dial)
  2189. {
  2190. struct attended_transfer_properties *props = ast_dial_get_user_data(dial);
  2191. switch (ast_dial_state(dial)) {
  2192. default:
  2193. case AST_DIAL_RESULT_INVALID:
  2194. case AST_DIAL_RESULT_FAILED:
  2195. case AST_DIAL_RESULT_TIMEOUT:
  2196. case AST_DIAL_RESULT_HANGUP:
  2197. case AST_DIAL_RESULT_UNANSWERED:
  2198. /* Failure cases */
  2199. stimulate_attended_transfer(props, STIMULUS_RECALL_TARGET_HANGUP);
  2200. break;
  2201. case AST_DIAL_RESULT_RINGING:
  2202. case AST_DIAL_RESULT_PROGRESS:
  2203. case AST_DIAL_RESULT_PROCEEDING:
  2204. case AST_DIAL_RESULT_TRYING:
  2205. /* Don't care about these cases */
  2206. break;
  2207. case AST_DIAL_RESULT_ANSWERED:
  2208. /* We struck gold! */
  2209. props->recall_target = ast_dial_answered_steal(dial);
  2210. stimulate_attended_transfer(props, STIMULUS_RECALL_TARGET_ANSWER);
  2211. break;
  2212. }
  2213. }
  2214. /*!
  2215. * \internal
  2216. * \brief Setup common things to transferrer and transfer_target recall channels.
  2217. *
  2218. * \param recall Channel for recalling a party.
  2219. * \param transferer Channel supplying recall information.
  2220. *
  2221. * \details
  2222. * Setup callid, variables, datastores, accountcode, and peeraccount.
  2223. *
  2224. * \pre Both channels are locked on entry.
  2225. *
  2226. * \pre COLP and CLID on the recall channel are setup by the caller but not
  2227. * explicitly published yet.
  2228. */
  2229. static void common_recall_channel_setup(struct ast_channel *recall, struct ast_channel *transferer)
  2230. {
  2231. ast_callid callid;
  2232. callid = ast_read_threadstorage_callid();
  2233. if (callid) {
  2234. ast_channel_callid_set(recall, callid);
  2235. }
  2236. ast_channel_inherit_variables(transferer, recall);
  2237. ast_channel_datastore_inherit(transferer, recall);
  2238. /*
  2239. * Stage a snapshot to ensure that a snapshot is always done
  2240. * on the recall channel so earlier COLP and CLID setup will
  2241. * get published.
  2242. */
  2243. ast_channel_stage_snapshot(recall);
  2244. ast_channel_req_accountcodes(recall, transferer, AST_CHANNEL_REQUESTOR_REPLACEMENT);
  2245. ast_channel_stage_snapshot_done(recall);
  2246. }
  2247. static int recalling_enter(struct attended_transfer_properties *props)
  2248. {
  2249. RAII_VAR(struct ast_format_cap *, cap, ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_DEFAULT), ao2_cleanup);
  2250. struct ast_channel *recall;
  2251. if (!cap) {
  2252. return -1;
  2253. }
  2254. ast_format_cap_append(cap, ast_format_slin, 0);
  2255. /* When we dial the transfer target, since we are communicating
  2256. * with a local channel, we can place the local channel in a bridge
  2257. * and then call out to it. When recalling the transferer, though, we
  2258. * have to use the dialing API because the channel is not local.
  2259. */
  2260. props->dial = ast_dial_create();
  2261. if (!props->dial) {
  2262. return -1;
  2263. }
  2264. if (ast_dial_append(props->dial, props->transferer_type, props->transferer_addr, NULL)) {
  2265. return -1;
  2266. }
  2267. if (ast_dial_prerun(props->dial, NULL, cap)) {
  2268. return -1;
  2269. }
  2270. /*
  2271. * Setup callid, variables, datastores, accountcode, peeraccount,
  2272. * COLP, and CLID on the recalled transferrer.
  2273. */
  2274. recall = ast_dial_get_channel(props->dial, 0);
  2275. if (!recall) {
  2276. return -1;
  2277. }
  2278. ast_channel_lock_both(recall, props->transferer);
  2279. ast_party_caller_copy(ast_channel_caller(recall),
  2280. ast_channel_caller(props->transferer));
  2281. ast_party_connected_line_copy(ast_channel_connected(recall),
  2282. &props->original_transferer_colp);
  2283. common_recall_channel_setup(recall, props->transferer);
  2284. ast_channel_unlock(recall);
  2285. ast_channel_unlock(props->transferer);
  2286. ast_dial_set_state_callback(props->dial, recall_callback);
  2287. ao2_ref(props, +1);
  2288. ast_dial_set_user_data(props->dial, props);
  2289. if (ast_dial_run(props->dial, NULL, 1) == AST_DIAL_RESULT_FAILED) {
  2290. ao2_ref(props, -1);
  2291. return -1;
  2292. }
  2293. bridge_ringing(props->transferee_bridge);
  2294. return 0;
  2295. }
  2296. static enum attended_transfer_state recalling_exit(struct attended_transfer_properties *props,
  2297. enum attended_transfer_stimulus stimulus)
  2298. {
  2299. /* No matter what the outcome was, we need to kill off the dial */
  2300. ast_dial_join(props->dial);
  2301. ast_dial_destroy(props->dial);
  2302. props->dial = NULL;
  2303. /* This reference is the one we incremented for the dial state callback (recall_callback) to use */
  2304. ao2_ref(props, -1);
  2305. switch (stimulus) {
  2306. case STIMULUS_TRANSFEREE_HANGUP:
  2307. return TRANSFER_FAIL;
  2308. case STIMULUS_TIMEOUT:
  2309. case STIMULUS_RECALL_TARGET_HANGUP:
  2310. ++props->retry_attempts;
  2311. if (props->retry_attempts >= props->atxfercallbackretries) {
  2312. return TRANSFER_FAIL;
  2313. }
  2314. if (props->atxferloopdelay) {
  2315. return TRANSFER_WAIT_TO_RETRANSFER;
  2316. }
  2317. return TRANSFER_RETRANSFER;
  2318. case STIMULUS_RECALL_TARGET_ANSWER:
  2319. /* Setting this datastore up will allow the transferer to have all of his
  2320. * call features set up automatically when the bridge changes back to a
  2321. * normal personality
  2322. */
  2323. ast_bridge_features_ds_set(props->recall_target, &props->transferer_features);
  2324. ast_channel_ref(props->recall_target);
  2325. if (ast_bridge_impart(props->transferee_bridge, props->recall_target, NULL, NULL,
  2326. AST_BRIDGE_IMPART_CHAN_INDEPENDENT)) {
  2327. ast_hangup(props->recall_target);
  2328. ast_channel_unref(props->recall_target);
  2329. return TRANSFER_FAIL;
  2330. }
  2331. return TRANSFER_RESUME;
  2332. case STIMULUS_NONE:
  2333. case STIMULUS_DTMF_ATXFER_ABORT:
  2334. case STIMULUS_DTMF_ATXFER_COMPLETE:
  2335. case STIMULUS_DTMF_ATXFER_THREEWAY:
  2336. case STIMULUS_DTMF_ATXFER_SWAP:
  2337. case STIMULUS_TRANSFER_TARGET_HANGUP:
  2338. case STIMULUS_TRANSFER_TARGET_ANSWER:
  2339. case STIMULUS_TRANSFERER_HANGUP:
  2340. default:
  2341. ast_log(LOG_WARNING, "Unexpected stimulus '%s' received in attended transfer state '%s'\n",
  2342. stimulus_strs[stimulus], state_properties[props->state].state_name);
  2343. return props->state;
  2344. }
  2345. }
  2346. static int wait_to_retransfer_enter(struct attended_transfer_properties *props)
  2347. {
  2348. bridge_hold(props->transferee_bridge);
  2349. return 0;
  2350. }
  2351. static enum attended_transfer_state wait_to_retransfer_exit(struct attended_transfer_properties *props,
  2352. enum attended_transfer_stimulus stimulus)
  2353. {
  2354. bridge_unhold(props->transferee_bridge);
  2355. switch (stimulus) {
  2356. case STIMULUS_TRANSFEREE_HANGUP:
  2357. return TRANSFER_FAIL;
  2358. case STIMULUS_TIMEOUT:
  2359. return TRANSFER_RETRANSFER;
  2360. case STIMULUS_NONE:
  2361. case STIMULUS_DTMF_ATXFER_ABORT:
  2362. case STIMULUS_DTMF_ATXFER_COMPLETE:
  2363. case STIMULUS_DTMF_ATXFER_THREEWAY:
  2364. case STIMULUS_DTMF_ATXFER_SWAP:
  2365. case STIMULUS_TRANSFER_TARGET_HANGUP:
  2366. case STIMULUS_TRANSFER_TARGET_ANSWER:
  2367. case STIMULUS_TRANSFERER_HANGUP:
  2368. case STIMULUS_RECALL_TARGET_HANGUP:
  2369. case STIMULUS_RECALL_TARGET_ANSWER:
  2370. default:
  2371. ast_log(LOG_WARNING, "Unexpected stimulus '%s' received in attended transfer state '%s'\n",
  2372. stimulus_strs[stimulus], state_properties[props->state].state_name);
  2373. return props->state;
  2374. }
  2375. }
  2376. static int attach_framehook(struct attended_transfer_properties *props, struct ast_channel *channel);
  2377. static int retransfer_enter(struct attended_transfer_properties *props)
  2378. {
  2379. RAII_VAR(struct ast_format_cap *, cap, ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_DEFAULT), ao2_cleanup);
  2380. char destination[AST_MAX_EXTENSION + AST_MAX_CONTEXT + 2];
  2381. int cause;
  2382. if (!cap) {
  2383. return -1;
  2384. }
  2385. snprintf(destination, sizeof(destination), "%s@%s", props->exten, props->context);
  2386. ast_format_cap_append(cap, ast_format_slin, 0);
  2387. /* Get a channel that is the destination we wish to call */
  2388. props->recall_target = ast_request("Local", cap, NULL, NULL, destination, &cause);
  2389. if (!props->recall_target) {
  2390. ast_log(LOG_ERROR, "Unable to request outbound channel for recall target\n");
  2391. return -1;
  2392. }
  2393. if (attach_framehook(props, props->recall_target)) {
  2394. ast_log(LOG_ERROR, "Unable to attach framehook to recall target\n");
  2395. ast_hangup(props->recall_target);
  2396. props->recall_target = NULL;
  2397. return -1;
  2398. }
  2399. /*
  2400. * Setup callid, variables, datastores, accountcode, peeraccount,
  2401. * and COLP on the recalled transfer target.
  2402. */
  2403. ast_channel_lock_both(props->recall_target, props->transferer);
  2404. ast_party_connected_line_copy(ast_channel_connected(props->recall_target),
  2405. &props->original_transferer_colp);
  2406. ast_party_id_reset(&ast_channel_connected(props->recall_target)->priv);
  2407. common_recall_channel_setup(props->recall_target, props->transferer);
  2408. ast_channel_unlock(props->recall_target);
  2409. ast_channel_unlock(props->transferer);
  2410. if (ast_call(props->recall_target, destination, 0)) {
  2411. ast_log(LOG_ERROR, "Unable to place outbound call to recall target\n");
  2412. ast_hangup(props->recall_target);
  2413. props->recall_target = NULL;
  2414. return -1;
  2415. }
  2416. ast_channel_ref(props->recall_target);
  2417. if (ast_bridge_impart(props->transferee_bridge, props->recall_target, NULL, NULL,
  2418. AST_BRIDGE_IMPART_CHAN_INDEPENDENT)) {
  2419. ast_log(LOG_ERROR, "Unable to place recall target into bridge\n");
  2420. ast_hangup(props->recall_target);
  2421. ast_channel_unref(props->recall_target);
  2422. return -1;
  2423. }
  2424. return 0;
  2425. }
  2426. static enum attended_transfer_state retransfer_exit(struct attended_transfer_properties *props,
  2427. enum attended_transfer_stimulus stimulus)
  2428. {
  2429. switch (stimulus) {
  2430. case STIMULUS_TRANSFEREE_HANGUP:
  2431. return TRANSFER_FAIL;
  2432. case STIMULUS_TIMEOUT:
  2433. ast_softhangup(props->recall_target, AST_SOFTHANGUP_EXPLICIT);
  2434. case STIMULUS_RECALL_TARGET_HANGUP:
  2435. props->recall_target = ast_channel_unref(props->recall_target);
  2436. if (props->atxferloopdelay) {
  2437. return TRANSFER_WAIT_TO_RECALL;
  2438. }
  2439. return TRANSFER_RECALLING;
  2440. case STIMULUS_RECALL_TARGET_ANSWER:
  2441. return TRANSFER_RESUME;
  2442. case STIMULUS_NONE:
  2443. case STIMULUS_DTMF_ATXFER_ABORT:
  2444. case STIMULUS_DTMF_ATXFER_COMPLETE:
  2445. case STIMULUS_DTMF_ATXFER_THREEWAY:
  2446. case STIMULUS_DTMF_ATXFER_SWAP:
  2447. case STIMULUS_TRANSFER_TARGET_HANGUP:
  2448. case STIMULUS_TRANSFER_TARGET_ANSWER:
  2449. case STIMULUS_TRANSFERER_HANGUP:
  2450. default:
  2451. ast_log(LOG_WARNING, "Unexpected stimulus '%s' received in attended transfer state '%s'\n",
  2452. stimulus_strs[stimulus], state_properties[props->state].state_name);
  2453. return props->state;
  2454. }
  2455. }
  2456. static int wait_to_recall_enter(struct attended_transfer_properties *props)
  2457. {
  2458. bridge_hold(props->transferee_bridge);
  2459. return 0;
  2460. }
  2461. static enum attended_transfer_state wait_to_recall_exit(struct attended_transfer_properties *props,
  2462. enum attended_transfer_stimulus stimulus)
  2463. {
  2464. bridge_unhold(props->transferee_bridge);
  2465. switch (stimulus) {
  2466. case STIMULUS_TRANSFEREE_HANGUP:
  2467. return TRANSFER_FAIL;
  2468. case STIMULUS_TIMEOUT:
  2469. return TRANSFER_RECALLING;
  2470. case STIMULUS_NONE:
  2471. case STIMULUS_DTMF_ATXFER_ABORT:
  2472. case STIMULUS_DTMF_ATXFER_COMPLETE:
  2473. case STIMULUS_DTMF_ATXFER_THREEWAY:
  2474. case STIMULUS_DTMF_ATXFER_SWAP:
  2475. case STIMULUS_TRANSFER_TARGET_HANGUP:
  2476. case STIMULUS_TRANSFER_TARGET_ANSWER:
  2477. case STIMULUS_TRANSFERER_HANGUP:
  2478. case STIMULUS_RECALL_TARGET_HANGUP:
  2479. case STIMULUS_RECALL_TARGET_ANSWER:
  2480. default:
  2481. ast_log(LOG_WARNING, "Unexpected stimulus '%s' received in attended transfer state '%s'\n",
  2482. stimulus_strs[stimulus], state_properties[props->state].state_name);
  2483. return props->state;
  2484. }
  2485. }
  2486. static int fail_enter(struct attended_transfer_properties *props)
  2487. {
  2488. if (props->transferee_bridge) {
  2489. ast_bridge_destroy(props->transferee_bridge, 0);
  2490. props->transferee_bridge = NULL;
  2491. }
  2492. return 0;
  2493. }
  2494. /*!
  2495. * \brief DTMF hook when transferer presses abort sequence.
  2496. *
  2497. * Sends a stimulus to the attended transfer monitor thread that the abort sequence has been pressed
  2498. */
  2499. static int atxfer_abort(struct ast_bridge_channel *bridge_channel, void *hook_pvt)
  2500. {
  2501. struct attended_transfer_properties *props = hook_pvt;
  2502. ast_debug(1, "Transferer on attended transfer %p pressed abort sequence\n", props);
  2503. stimulate_attended_transfer(props, STIMULUS_DTMF_ATXFER_ABORT);
  2504. return 0;
  2505. }
  2506. /*!
  2507. * \brief DTMF hook when transferer presses complete sequence.
  2508. *
  2509. * Sends a stimulus to the attended transfer monitor thread that the complete sequence has been pressed
  2510. */
  2511. static int atxfer_complete(struct ast_bridge_channel *bridge_channel, void *hook_pvt)
  2512. {
  2513. struct attended_transfer_properties *props = hook_pvt;
  2514. ast_debug(1, "Transferer on attended transfer %p pressed complete sequence\n", props);
  2515. stimulate_attended_transfer(props, STIMULUS_DTMF_ATXFER_COMPLETE);
  2516. return 0;
  2517. }
  2518. /*!
  2519. * \brief DTMF hook when transferer presses threeway sequence.
  2520. *
  2521. * Sends a stimulus to the attended transfer monitor thread that the threeway sequence has been pressed
  2522. */
  2523. static int atxfer_threeway(struct ast_bridge_channel *bridge_channel, void *hook_pvt)
  2524. {
  2525. struct attended_transfer_properties *props = hook_pvt;
  2526. ast_debug(1, "Transferer on attended transfer %p pressed threeway sequence\n", props);
  2527. stimulate_attended_transfer(props, STIMULUS_DTMF_ATXFER_THREEWAY);
  2528. return 0;
  2529. }
  2530. /*!
  2531. * \brief DTMF hook when transferer presses swap sequence.
  2532. *
  2533. * Sends a stimulus to the attended transfer monitor thread that the swap sequence has been pressed
  2534. */
  2535. static int atxfer_swap(struct ast_bridge_channel *bridge_channel, void *hook_pvt)
  2536. {
  2537. struct attended_transfer_properties *props = hook_pvt;
  2538. ast_debug(1, "Transferer on attended transfer %p pressed swap sequence\n", props);
  2539. stimulate_attended_transfer(props, STIMULUS_DTMF_ATXFER_SWAP);
  2540. return 0;
  2541. }
  2542. /*!
  2543. * \brief Hangup hook for transferer channel.
  2544. *
  2545. * Sends a stimulus to the attended transfer monitor thread that the transferer has hung up.
  2546. */
  2547. static int atxfer_transferer_hangup(struct ast_bridge_channel *bridge_channel, void *hook_pvt)
  2548. {
  2549. struct attended_transfer_properties *props = hook_pvt;
  2550. ast_debug(1, "Transferer on attended transfer %p hung up\n", props);
  2551. stimulate_attended_transfer(props, STIMULUS_TRANSFERER_HANGUP);
  2552. return 0;
  2553. }
  2554. /*!
  2555. * \brief Frame hook for transfer target channel
  2556. *
  2557. * This is used to determine if the transfer target or recall target has answered
  2558. * the outgoing call.
  2559. *
  2560. * When an answer is detected, a stimulus is sent to the attended transfer monitor
  2561. * thread to indicate that the transfer target or recall target has answered.
  2562. *
  2563. * \param chan The channel the framehook is attached to.
  2564. * \param frame The frame being read or written.
  2565. * \param event What is being done with the frame.
  2566. * \param data The attended transfer properties.
  2567. */
  2568. static struct ast_frame *transfer_target_framehook_cb(struct ast_channel *chan,
  2569. struct ast_frame *frame, enum ast_framehook_event event, void *data)
  2570. {
  2571. struct attended_transfer_properties *props = data;
  2572. if (event == AST_FRAMEHOOK_EVENT_READ &&
  2573. frame && frame->frametype == AST_FRAME_CONTROL &&
  2574. frame->subclass.integer == AST_CONTROL_ANSWER &&
  2575. !ast_check_hangup(chan)) {
  2576. ast_debug(1, "Detected an answer for recall attempt on attended transfer %p\n", props);
  2577. if (props->superstate == SUPERSTATE_TRANSFER) {
  2578. stimulate_attended_transfer(props, STIMULUS_TRANSFER_TARGET_ANSWER);
  2579. } else {
  2580. stimulate_attended_transfer(props, STIMULUS_RECALL_TARGET_ANSWER);
  2581. }
  2582. ast_framehook_detach(chan, props->target_framehook_id);
  2583. props->target_framehook_id = -1;
  2584. }
  2585. return frame;
  2586. }
  2587. /*! \brief Callback function which informs upstream if we are consuming a frame of a specific type */
  2588. static int transfer_target_framehook_consume(void *data, enum ast_frame_type type)
  2589. {
  2590. return (type == AST_FRAME_CONTROL ? 1 : 0);
  2591. }
  2592. static void transfer_target_framehook_destroy_cb(void *data)
  2593. {
  2594. struct attended_transfer_properties *props = data;
  2595. ao2_cleanup(props);
  2596. }
  2597. static int bridge_personality_atxfer_push(struct ast_bridge *self, struct ast_bridge_channel *bridge_channel, struct ast_bridge_channel *swap)
  2598. {
  2599. const char *abort_dtmf;
  2600. const char *complete_dtmf;
  2601. const char *threeway_dtmf;
  2602. const char *swap_dtmf;
  2603. struct bridge_basic_personality *personality = self->personality;
  2604. if (!ast_channel_has_role(bridge_channel->chan, AST_TRANSFERER_ROLE_NAME)) {
  2605. return 0;
  2606. }
  2607. abort_dtmf = ast_channel_get_role_option(bridge_channel->chan, AST_TRANSFERER_ROLE_NAME, "abort");
  2608. complete_dtmf = ast_channel_get_role_option(bridge_channel->chan, AST_TRANSFERER_ROLE_NAME, "complete");
  2609. threeway_dtmf = ast_channel_get_role_option(bridge_channel->chan, AST_TRANSFERER_ROLE_NAME, "threeway");
  2610. swap_dtmf = ast_channel_get_role_option(bridge_channel->chan, AST_TRANSFERER_ROLE_NAME, "swap");
  2611. if (!ast_strlen_zero(abort_dtmf) && ast_bridge_dtmf_hook(bridge_channel->features,
  2612. abort_dtmf, atxfer_abort, personality->details[personality->current].pvt, NULL,
  2613. AST_BRIDGE_HOOK_REMOVE_ON_PERSONALITY_CHANGE | AST_BRIDGE_HOOK_REMOVE_ON_PULL)) {
  2614. return -1;
  2615. }
  2616. if (!ast_strlen_zero(complete_dtmf) && ast_bridge_dtmf_hook(bridge_channel->features,
  2617. complete_dtmf, atxfer_complete, personality->details[personality->current].pvt, NULL,
  2618. AST_BRIDGE_HOOK_REMOVE_ON_PERSONALITY_CHANGE | AST_BRIDGE_HOOK_REMOVE_ON_PULL)) {
  2619. return -1;
  2620. }
  2621. if (!ast_strlen_zero(threeway_dtmf) && ast_bridge_dtmf_hook(bridge_channel->features,
  2622. threeway_dtmf, atxfer_threeway, personality->details[personality->current].pvt, NULL,
  2623. AST_BRIDGE_HOOK_REMOVE_ON_PERSONALITY_CHANGE | AST_BRIDGE_HOOK_REMOVE_ON_PULL)) {
  2624. return -1;
  2625. }
  2626. if (!ast_strlen_zero(swap_dtmf) && ast_bridge_dtmf_hook(bridge_channel->features,
  2627. swap_dtmf, atxfer_swap, personality->details[personality->current].pvt, NULL,
  2628. AST_BRIDGE_HOOK_REMOVE_ON_PERSONALITY_CHANGE | AST_BRIDGE_HOOK_REMOVE_ON_PULL)) {
  2629. return -1;
  2630. }
  2631. if (ast_bridge_hangup_hook(bridge_channel->features, atxfer_transferer_hangup,
  2632. personality->details[personality->current].pvt, NULL,
  2633. AST_BRIDGE_HOOK_REMOVE_ON_PERSONALITY_CHANGE | AST_BRIDGE_HOOK_REMOVE_ON_PULL)) {
  2634. return -1;
  2635. }
  2636. return 0;
  2637. }
  2638. static void transfer_pull(struct ast_bridge *self, struct ast_bridge_channel *bridge_channel, struct attended_transfer_properties *props)
  2639. {
  2640. if (self->num_channels > 1 || bridge_channel->state == BRIDGE_CHANNEL_STATE_WAIT) {
  2641. return;
  2642. }
  2643. if (self->num_channels == 1) {
  2644. struct ast_bridge_channel *transferer_bridge_channel;
  2645. int not_transferer;
  2646. ast_channel_lock(props->transferer);
  2647. transferer_bridge_channel = ast_channel_get_bridge_channel(props->transferer);
  2648. ast_channel_unlock(props->transferer);
  2649. if (!transferer_bridge_channel) {
  2650. return;
  2651. }
  2652. not_transferer = AST_LIST_FIRST(&self->channels) != transferer_bridge_channel;
  2653. ao2_ref(transferer_bridge_channel, -1);
  2654. if (not_transferer) {
  2655. return;
  2656. }
  2657. }
  2658. /* Reaching this point means that either
  2659. * 1) The bridge has no channels in it
  2660. * 2) The bridge has one channel, and it's the transferer
  2661. * In either case, it indicates that the non-transferer parties
  2662. * are no longer in the bridge.
  2663. */
  2664. if (self == props->transferee_bridge) {
  2665. stimulate_attended_transfer(props, STIMULUS_TRANSFEREE_HANGUP);
  2666. } else {
  2667. stimulate_attended_transfer(props, STIMULUS_TRANSFER_TARGET_HANGUP);
  2668. }
  2669. }
  2670. static void recall_pull(struct ast_bridge *self, struct ast_bridge_channel *bridge_channel, struct attended_transfer_properties *props)
  2671. {
  2672. if (self == props->target_bridge) {
  2673. /* Once we're in the recall superstate, we no longer care about this bridge */
  2674. return;
  2675. }
  2676. if (bridge_channel->chan == props->recall_target) {
  2677. stimulate_attended_transfer(props, STIMULUS_RECALL_TARGET_HANGUP);
  2678. return;
  2679. }
  2680. if (self->num_channels == 0) {
  2681. /* Empty bridge means all transferees are gone for sure */
  2682. stimulate_attended_transfer(props, STIMULUS_TRANSFEREE_HANGUP);
  2683. return;
  2684. }
  2685. if (self->num_channels == 1) {
  2686. struct ast_bridge_channel *target_bridge_channel;
  2687. if (!props->recall_target) {
  2688. /* No recall target means that the pull happened on a transferee. If there's still
  2689. * a channel left in the bridge, we don't need to send a stimulus
  2690. */
  2691. return;
  2692. }
  2693. ast_channel_lock(props->recall_target);
  2694. target_bridge_channel = ast_channel_get_bridge_channel(props->recall_target);
  2695. ast_channel_unlock(props->recall_target);
  2696. if (target_bridge_channel) {
  2697. if (AST_LIST_FIRST(&self->channels) == target_bridge_channel) {
  2698. stimulate_attended_transfer(props, STIMULUS_TRANSFEREE_HANGUP);
  2699. }
  2700. ao2_ref(target_bridge_channel, -1);
  2701. }
  2702. }
  2703. }
  2704. static void bridge_personality_atxfer_pull(struct ast_bridge *self, struct ast_bridge_channel *bridge_channel)
  2705. {
  2706. struct bridge_basic_personality *personality = self->personality;
  2707. struct attended_transfer_properties *props = personality->details[personality->current].pvt;
  2708. switch (props->superstate) {
  2709. case SUPERSTATE_TRANSFER:
  2710. transfer_pull(self, bridge_channel, props);
  2711. break;
  2712. case SUPERSTATE_RECALL:
  2713. recall_pull(self, bridge_channel, props);
  2714. break;
  2715. }
  2716. }
  2717. static enum attended_transfer_stimulus wait_for_stimulus(struct attended_transfer_properties *props)
  2718. {
  2719. enum attended_transfer_stimulus stimulus;
  2720. struct stimulus_list *list;
  2721. SCOPED_MUTEX(lock, ao2_object_get_lockaddr(props));
  2722. while (!(list = AST_LIST_REMOVE_HEAD(&props->stimulus_queue, next))) {
  2723. if (!(state_properties[props->state].flags & TRANSFER_STATE_FLAG_TIMED)) {
  2724. ast_cond_wait(&props->cond, lock);
  2725. } else {
  2726. struct timeval relative_timeout = { 0, };
  2727. struct timeval absolute_timeout;
  2728. struct timespec timeout_arg;
  2729. if (state_properties[props->state].flags & TRANSFER_STATE_FLAG_TIMER_RESET) {
  2730. props->start = ast_tvnow();
  2731. }
  2732. if (state_properties[props->state].flags & TRANSFER_STATE_FLAG_TIMER_LOOP_DELAY) {
  2733. relative_timeout.tv_sec = props->atxferloopdelay;
  2734. } else {
  2735. /* Implied TRANSFER_STATE_FLAG_TIMER_ATXFER_NO_ANSWER */
  2736. relative_timeout.tv_sec = props->atxfernoanswertimeout;
  2737. }
  2738. absolute_timeout = ast_tvadd(props->start, relative_timeout);
  2739. timeout_arg.tv_sec = absolute_timeout.tv_sec;
  2740. timeout_arg.tv_nsec = absolute_timeout.tv_usec * 1000;
  2741. if (ast_cond_timedwait(&props->cond, lock, &timeout_arg) == ETIMEDOUT) {
  2742. return STIMULUS_TIMEOUT;
  2743. }
  2744. }
  2745. }
  2746. stimulus = list->stimulus;
  2747. ast_free(list);
  2748. return stimulus;
  2749. }
  2750. /*!
  2751. * \brief The main loop for the attended transfer monitor thread.
  2752. *
  2753. * This loop runs continuously until the attended transfer reaches
  2754. * a terminal state. Stimuli for changes in the attended transfer
  2755. * state are handled in this thread so that all factors in an
  2756. * attended transfer can be handled in an orderly fashion.
  2757. *
  2758. * \param data The attended transfer properties
  2759. */
  2760. static void *attended_transfer_monitor_thread(void *data)
  2761. {
  2762. struct attended_transfer_properties *props = data;
  2763. ast_callid callid;
  2764. /*
  2765. * Set thread callid to the transferer's callid because we
  2766. * are doing all this on that channel's behalf.
  2767. */
  2768. ast_channel_lock(props->transferer);
  2769. callid = ast_channel_callid(props->transferer);
  2770. ast_channel_unlock(props->transferer);
  2771. if (callid) {
  2772. ast_callid_threadassoc_add(callid);
  2773. }
  2774. for (;;) {
  2775. enum attended_transfer_stimulus stimulus;
  2776. ast_debug(1, "About to enter state %s for attended transfer %p\n", state_properties[props->state].state_name, props);
  2777. if (state_properties[props->state].enter &&
  2778. state_properties[props->state].enter(props)) {
  2779. ast_log(LOG_ERROR, "State %s enter function returned an error for attended transfer %p\n",
  2780. state_properties[props->state].state_name, props);
  2781. break;
  2782. }
  2783. if (state_properties[props->state].flags & TRANSFER_STATE_FLAG_TERMINAL) {
  2784. ast_debug(1, "State %s is a terminal state. Ending attended transfer %p\n",
  2785. state_properties[props->state].state_name, props);
  2786. break;
  2787. }
  2788. stimulus = wait_for_stimulus(props);
  2789. ast_debug(1, "Received stimulus %s on attended transfer %p\n", stimulus_strs[stimulus], props);
  2790. ast_assert(state_properties[props->state].exit != NULL);
  2791. props->state = state_properties[props->state].exit(props, stimulus);
  2792. ast_debug(1, "Told to enter state %s exit on attended transfer %p\n", state_properties[props->state].state_name, props);
  2793. }
  2794. attended_transfer_properties_shutdown(props);
  2795. if (callid) {
  2796. ast_callid_threadassoc_remove();
  2797. }
  2798. return NULL;
  2799. }
  2800. static int attach_framehook(struct attended_transfer_properties *props, struct ast_channel *channel)
  2801. {
  2802. struct ast_framehook_interface target_interface = {
  2803. .version = AST_FRAMEHOOK_INTERFACE_VERSION,
  2804. .event_cb = transfer_target_framehook_cb,
  2805. .destroy_cb = transfer_target_framehook_destroy_cb,
  2806. .consume_cb = transfer_target_framehook_consume,
  2807. .disable_inheritance = 1,
  2808. };
  2809. ao2_ref(props, +1);
  2810. target_interface.data = props;
  2811. ast_channel_lock(channel);
  2812. props->target_framehook_id = ast_framehook_attach(channel, &target_interface);
  2813. ast_channel_unlock(channel);
  2814. if (props->target_framehook_id == -1) {
  2815. ao2_ref(props, -1);
  2816. return -1;
  2817. }
  2818. return 0;
  2819. }
  2820. static int add_transferer_role(struct ast_channel *chan, struct ast_bridge_features_attended_transfer *attended_transfer)
  2821. {
  2822. const char *atxfer_abort;
  2823. const char *atxfer_threeway;
  2824. const char *atxfer_complete;
  2825. const char *atxfer_swap;
  2826. struct ast_features_xfer_config *xfer_cfg;
  2827. SCOPED_CHANNELLOCK(lock, chan);
  2828. xfer_cfg = ast_get_chan_features_xfer_config(chan);
  2829. if (!xfer_cfg) {
  2830. return -1;
  2831. }
  2832. if (attended_transfer) {
  2833. atxfer_abort = ast_strdupa(S_OR(attended_transfer->abort, xfer_cfg->atxferabort));
  2834. atxfer_threeway = ast_strdupa(S_OR(attended_transfer->threeway, xfer_cfg->atxferthreeway));
  2835. atxfer_complete = ast_strdupa(S_OR(attended_transfer->complete, xfer_cfg->atxfercomplete));
  2836. atxfer_swap = ast_strdupa(S_OR(attended_transfer->swap, xfer_cfg->atxferswap));
  2837. } else {
  2838. atxfer_abort = ast_strdupa(xfer_cfg->atxferabort);
  2839. atxfer_threeway = ast_strdupa(xfer_cfg->atxferthreeway);
  2840. atxfer_complete = ast_strdupa(xfer_cfg->atxfercomplete);
  2841. atxfer_swap = ast_strdupa(xfer_cfg->atxferswap);
  2842. }
  2843. ao2_ref(xfer_cfg, -1);
  2844. return ast_channel_add_bridge_role(chan, AST_TRANSFERER_ROLE_NAME) ||
  2845. ast_channel_set_bridge_role_option(chan, AST_TRANSFERER_ROLE_NAME, "abort", atxfer_abort) ||
  2846. ast_channel_set_bridge_role_option(chan, AST_TRANSFERER_ROLE_NAME, "complete", atxfer_complete) ||
  2847. ast_channel_set_bridge_role_option(chan, AST_TRANSFERER_ROLE_NAME, "threeway", atxfer_threeway) ||
  2848. ast_channel_set_bridge_role_option(chan, AST_TRANSFERER_ROLE_NAME, "swap", atxfer_swap);
  2849. }
  2850. /*!
  2851. * \brief Helper function that presents dialtone and grabs extension
  2852. *
  2853. * \retval 0 on success
  2854. * \retval -1 on failure
  2855. */
  2856. static int grab_transfer(struct ast_channel *chan, char *exten, size_t exten_len, const char *context)
  2857. {
  2858. int res;
  2859. int digit_timeout;
  2860. int attempts = 0;
  2861. int max_attempts;
  2862. struct ast_features_xfer_config *xfer_cfg;
  2863. char *announce_sound, *retry_sound, *invalid_sound;
  2864. const char *extenoverride;
  2865. ast_channel_lock(chan);
  2866. extenoverride = get_transfer_exten(chan, NULL);
  2867. if (!ast_strlen_zero(extenoverride)) {
  2868. int extenres = ast_exists_extension(chan, context, extenoverride, 1,
  2869. S_COR(ast_channel_caller(chan)->id.number.valid, ast_channel_caller(chan)->id.number.str, NULL)) ? 1 : 0;
  2870. if (extenres) {
  2871. ast_copy_string(exten, extenoverride, exten_len);
  2872. ast_channel_unlock(chan);
  2873. ast_verb(3, "Transfering call to '%s@%s'", exten, context);
  2874. return 0;
  2875. }
  2876. ast_log(LOG_WARNING, "Override extension '%s' does not exist in context '%s'\n", extenoverride, context);
  2877. /* since we didn't get a valid extension from the channel, fall back and grab it from the user as usual now */
  2878. }
  2879. xfer_cfg = ast_get_chan_features_xfer_config(chan);
  2880. if (!xfer_cfg) {
  2881. ast_log(LOG_ERROR, "Channel %s: Unable to get transfer configuration\n",
  2882. ast_channel_name(chan));
  2883. ast_channel_unlock(chan);
  2884. return -1;
  2885. }
  2886. digit_timeout = xfer_cfg->transferdigittimeout * 1000;
  2887. max_attempts = xfer_cfg->transferdialattempts;
  2888. announce_sound = ast_strdupa(xfer_cfg->transferannouncesound);
  2889. retry_sound = ast_strdupa(xfer_cfg->transferretrysound);
  2890. invalid_sound = ast_strdupa(xfer_cfg->transferinvalidsound);
  2891. ao2_ref(xfer_cfg, -1);
  2892. ast_channel_unlock(chan);
  2893. /* Play the simple "transfer" prompt out and wait */
  2894. if (!ast_strlen_zero(announce_sound)) {
  2895. res = ast_stream_and_wait(chan, announce_sound, AST_DIGIT_ANY);
  2896. ast_stopstream(chan);
  2897. if (res < 0) {
  2898. /* Hangup or error */
  2899. return -1;
  2900. }
  2901. if (res) {
  2902. /* Store the DTMF digit that interrupted playback of the file. */
  2903. exten[0] = res;
  2904. }
  2905. }
  2906. /* Drop to dialtone so they can enter the extension they want to transfer to */
  2907. do {
  2908. ++attempts;
  2909. ast_test_suite_event_notify("TRANSFER_BEGIN_DIAL",
  2910. "Channel: %s\r\n"
  2911. "Attempt: %d",
  2912. ast_channel_name(chan), attempts);
  2913. res = ast_app_dtget(chan, context, exten, exten_len, exten_len - 1, digit_timeout);
  2914. ast_test_suite_event_notify("TRANSFER_DIALLED",
  2915. "Channel: %s\r\n"
  2916. "Attempt: %d\r\n"
  2917. "Dialled: %s\r\n"
  2918. "Result: %s",
  2919. ast_channel_name(chan), attempts, exten, res > 0 ? "Success" : "Failure");
  2920. if (res < 0) {
  2921. /* Hangup or error */
  2922. res = -1;
  2923. } else if (!res) {
  2924. /* 0 for invalid extension dialed. */
  2925. if (ast_strlen_zero(exten)) {
  2926. ast_verb(3, "Channel %s: Dialed no digits.\n", ast_channel_name(chan));
  2927. } else {
  2928. ast_verb(3, "Channel %s: Dialed '%s@%s' does not exist.\n",
  2929. ast_channel_name(chan), exten, context);
  2930. }
  2931. if (attempts < max_attempts) {
  2932. ast_stream_and_wait(chan, retry_sound, AST_DIGIT_NONE);
  2933. } else {
  2934. ast_stream_and_wait(chan, invalid_sound, AST_DIGIT_NONE);
  2935. }
  2936. memset(exten, 0, exten_len);
  2937. res = 1;
  2938. } else {
  2939. /* Dialed extension is valid. */
  2940. res = 0;
  2941. }
  2942. } while (res > 0 && attempts < max_attempts);
  2943. ast_test_suite_event_notify("TRANSFER_DIAL_FINAL",
  2944. "Channel: %s\r\n"
  2945. "Result: %s",
  2946. ast_channel_name(chan), res == 0 ? "Success" : "Failure");
  2947. return res ? -1 : 0;
  2948. }
  2949. static void copy_caller_data(struct ast_channel *dest, struct ast_channel *caller)
  2950. {
  2951. ast_channel_lock_both(caller, dest);
  2952. ast_connected_line_copy_from_caller(ast_channel_connected(dest), ast_channel_caller(caller));
  2953. ast_channel_inherit_variables(caller, dest);
  2954. ast_channel_datastore_inherit(caller, dest);
  2955. ast_channel_unlock(dest);
  2956. ast_channel_unlock(caller);
  2957. }
  2958. /*! \brief Helper function that creates an outgoing channel and returns it immediately */
  2959. static struct ast_channel *dial_transfer(struct ast_channel *caller, const char *destination)
  2960. {
  2961. struct ast_channel *chan;
  2962. int cause;
  2963. struct ast_format_cap *caps;
  2964. ast_channel_lock(caller);
  2965. caps = ao2_bump(ast_channel_nativeformats(caller));
  2966. ast_channel_unlock(caller);
  2967. /* Now we request a local channel to prepare to call the destination */
  2968. chan = ast_request("Local", caps, NULL, caller, destination, &cause);
  2969. ao2_cleanup(caps);
  2970. if (!chan) {
  2971. return NULL;
  2972. }
  2973. ast_channel_lock_both(chan, caller);
  2974. ast_channel_req_accountcodes(chan, caller, AST_CHANNEL_REQUESTOR_BRIDGE_PEER);
  2975. /* Who is transferring the call. */
  2976. pbx_builtin_setvar_helper(chan, "TRANSFERERNAME", ast_channel_name(caller));
  2977. ast_bridge_set_transfer_variables(chan, ast_channel_name(caller), 1);
  2978. ast_channel_unlock(chan);
  2979. ast_channel_unlock(caller);
  2980. /* Before we actually dial out let's inherit appropriate information. */
  2981. copy_caller_data(chan, caller);
  2982. return chan;
  2983. }
  2984. /*!
  2985. * \brief Internal built in feature for attended transfers
  2986. *
  2987. * This hook will set up a thread for monitoring the progress of
  2988. * an attended transfer. For more information about attended transfer
  2989. * progress, see documentation on the transfer state machine.
  2990. *
  2991. * \param bridge_channel The channel that pressed the attended transfer DTMF sequence
  2992. * \param hook_pvt Structure with further information about the attended transfer
  2993. */
  2994. static int feature_attended_transfer(struct ast_bridge_channel *bridge_channel, void *hook_pvt)
  2995. {
  2996. struct ast_bridge_features_attended_transfer *attended_transfer = hook_pvt;
  2997. struct attended_transfer_properties *props;
  2998. struct ast_bridge *bridge;
  2999. char destination[AST_MAX_EXTENSION + AST_MAX_CONTEXT + 1];
  3000. char exten[AST_MAX_EXTENSION] = "";
  3001. pthread_t thread;
  3002. /* Inhibit the bridge before we do anything else. */
  3003. bridge = ast_bridge_channel_merge_inhibit(bridge_channel, +1);
  3004. ast_verb(3, "Channel %s: Started DTMF attended transfer.\n",
  3005. ast_channel_name(bridge_channel->chan));
  3006. if (strcmp(bridge->v_table->name, "basic")) {
  3007. ast_log(LOG_ERROR, "Channel %s: Attended transfer attempted on unsupported bridge type '%s'.\n",
  3008. ast_channel_name(bridge_channel->chan), bridge->v_table->name);
  3009. ast_bridge_merge_inhibit(bridge, -1);
  3010. ao2_ref(bridge, -1);
  3011. return 0;
  3012. }
  3013. /* Was the bridge inhibited before we inhibited it? */
  3014. if (1 < bridge->inhibit_merge) {
  3015. /*
  3016. * The peer likely initiated attended transfer at the same time
  3017. * and we lost the race.
  3018. */
  3019. ast_verb(3, "Channel %s: Bridge '%s' does not permit merging at this time.\n",
  3020. ast_channel_name(bridge_channel->chan), bridge->uniqueid);
  3021. ast_bridge_merge_inhibit(bridge, -1);
  3022. ao2_ref(bridge, -1);
  3023. return 0;
  3024. }
  3025. props = attended_transfer_properties_alloc(bridge_channel->chan,
  3026. attended_transfer ? attended_transfer->context : NULL);
  3027. if (!props) {
  3028. ast_log(LOG_ERROR, "Channel %s: Unable to allocate control structure for performing attended transfer.\n",
  3029. ast_channel_name(bridge_channel->chan));
  3030. ast_bridge_merge_inhibit(bridge, -1);
  3031. ao2_ref(bridge, -1);
  3032. return 0;
  3033. }
  3034. props->transferee_bridge = bridge;
  3035. if (add_transferer_role(props->transferer, attended_transfer)) {
  3036. ast_log(LOG_ERROR, "Channel %s: Unable to set transferrer bridge role.\n",
  3037. ast_channel_name(bridge_channel->chan));
  3038. attended_transfer_properties_shutdown(props);
  3039. return 0;
  3040. }
  3041. ast_bridge_channel_write_hold(bridge_channel, NULL);
  3042. /* Grab the extension to transfer to */
  3043. if (grab_transfer(bridge_channel->chan, exten, sizeof(exten), props->context)) {
  3044. /*
  3045. * This is a normal exit for when the user fails
  3046. * to specify a valid transfer target. e.g., The user
  3047. * hungup, didn't dial any digits, or dialed an invalid
  3048. * extension.
  3049. */
  3050. ast_verb(3, "Channel %s: Unable to acquire target extension for attended transfer.\n",
  3051. ast_channel_name(bridge_channel->chan));
  3052. ast_bridge_channel_write_unhold(bridge_channel);
  3053. attended_transfer_properties_shutdown(props);
  3054. return 0;
  3055. }
  3056. ast_string_field_set(props, exten, exten);
  3057. /* Fill the variable with the extension and context we want to call */
  3058. snprintf(destination, sizeof(destination), "%s@%s", props->exten, props->context);
  3059. ast_debug(1, "Channel %s: Attended transfer target '%s'\n",
  3060. ast_channel_name(bridge_channel->chan), destination);
  3061. /* Get a channel that is the destination we wish to call */
  3062. props->transfer_target = dial_transfer(bridge_channel->chan, destination);
  3063. if (!props->transfer_target) {
  3064. ast_log(LOG_ERROR, "Channel %s: Unable to request outbound channel for attended transfer target.\n",
  3065. ast_channel_name(bridge_channel->chan));
  3066. stream_failsound(props->transferer);
  3067. ast_bridge_channel_write_unhold(bridge_channel);
  3068. attended_transfer_properties_shutdown(props);
  3069. return 0;
  3070. }
  3071. /* Create a bridge to use to talk to the person we are calling */
  3072. props->target_bridge = ast_bridge_basic_new();
  3073. if (!props->target_bridge) {
  3074. ast_log(LOG_ERROR, "Channel %s: Unable to create bridge for attended transfer target.\n",
  3075. ast_channel_name(bridge_channel->chan));
  3076. stream_failsound(props->transferer);
  3077. ast_bridge_channel_write_unhold(bridge_channel);
  3078. ast_hangup(props->transfer_target);
  3079. props->transfer_target = NULL;
  3080. attended_transfer_properties_shutdown(props);
  3081. return 0;
  3082. }
  3083. ast_bridge_merge_inhibit(props->target_bridge, +1);
  3084. if (attach_framehook(props, props->transfer_target)) {
  3085. ast_log(LOG_ERROR, "Channel %s: Unable to attach framehook to transfer target.\n",
  3086. ast_channel_name(bridge_channel->chan));
  3087. stream_failsound(props->transferer);
  3088. ast_bridge_channel_write_unhold(bridge_channel);
  3089. ast_hangup(props->transfer_target);
  3090. props->transfer_target = NULL;
  3091. attended_transfer_properties_shutdown(props);
  3092. return 0;
  3093. }
  3094. bridge_basic_change_personality(props->target_bridge,
  3095. BRIDGE_BASIC_PERSONALITY_ATXFER, props);
  3096. bridge_basic_change_personality(bridge,
  3097. BRIDGE_BASIC_PERSONALITY_ATXFER, props);
  3098. if (ast_call(props->transfer_target, destination, 0)) {
  3099. ast_log(LOG_ERROR, "Channel %s: Unable to place outbound call to transfer target.\n",
  3100. ast_channel_name(bridge_channel->chan));
  3101. stream_failsound(props->transferer);
  3102. ast_bridge_channel_write_unhold(bridge_channel);
  3103. ast_hangup(props->transfer_target);
  3104. props->transfer_target = NULL;
  3105. attended_transfer_properties_shutdown(props);
  3106. return 0;
  3107. }
  3108. /* We increase the refcount of the transfer target because ast_bridge_impart() will
  3109. * steal the reference we already have. We need to keep a reference, so the only
  3110. * choice is to give it a bump
  3111. */
  3112. ast_channel_ref(props->transfer_target);
  3113. if (ast_bridge_impart(props->target_bridge, props->transfer_target, NULL, NULL,
  3114. AST_BRIDGE_IMPART_CHAN_INDEPENDENT)) {
  3115. ast_log(LOG_ERROR, "Channel %s: Unable to place transfer target into bridge.\n",
  3116. ast_channel_name(bridge_channel->chan));
  3117. stream_failsound(props->transferer);
  3118. ast_bridge_channel_write_unhold(bridge_channel);
  3119. ast_hangup(props->transfer_target);
  3120. props->transfer_target = NULL;
  3121. attended_transfer_properties_shutdown(props);
  3122. return 0;
  3123. }
  3124. if (ast_pthread_create_detached(&thread, NULL, attended_transfer_monitor_thread, props)) {
  3125. ast_log(LOG_ERROR, "Channel %s: Unable to create monitoring thread for attended transfer.\n",
  3126. ast_channel_name(bridge_channel->chan));
  3127. stream_failsound(props->transferer);
  3128. ast_bridge_channel_write_unhold(bridge_channel);
  3129. attended_transfer_properties_shutdown(props);
  3130. return 0;
  3131. }
  3132. /* Once the monitoring thread has been created, it is responsible for destroying all
  3133. * of the necessary components.
  3134. */
  3135. return 0;
  3136. }
  3137. static void blind_transfer_cb(struct ast_channel *new_channel, struct transfer_channel_data *user_data_wrapper,
  3138. enum ast_transfer_type transfer_type)
  3139. {
  3140. struct ast_channel *transferer_channel = user_data_wrapper->data;
  3141. if (transfer_type == AST_BRIDGE_TRANSFER_MULTI_PARTY) {
  3142. copy_caller_data(new_channel, transferer_channel);
  3143. }
  3144. }
  3145. /*! \brief Internal built in feature for blind transfers */
  3146. static int feature_blind_transfer(struct ast_bridge_channel *bridge_channel, void *hook_pvt)
  3147. {
  3148. char xfer_exten[AST_MAX_EXTENSION] = "";
  3149. struct ast_bridge_features_blind_transfer *blind_transfer = hook_pvt;
  3150. const char *xfer_context;
  3151. char *goto_on_blindxfer;
  3152. ast_verb(3, "Channel %s: Started DTMF blind transfer.\n",
  3153. ast_channel_name(bridge_channel->chan));
  3154. ast_bridge_channel_write_hold(bridge_channel, NULL);
  3155. ast_channel_lock(bridge_channel->chan);
  3156. xfer_context = ast_strdupa(get_transfer_context(bridge_channel->chan,
  3157. blind_transfer ? blind_transfer->context : NULL));
  3158. goto_on_blindxfer = ast_strdupa(S_OR(pbx_builtin_getvar_helper(bridge_channel->chan,
  3159. "GOTO_ON_BLINDXFR"), ""));
  3160. ast_channel_unlock(bridge_channel->chan);
  3161. /* Grab the extension to transfer to */
  3162. if (grab_transfer(bridge_channel->chan, xfer_exten, sizeof(xfer_exten), xfer_context)) {
  3163. ast_bridge_channel_write_unhold(bridge_channel);
  3164. return 0;
  3165. }
  3166. ast_debug(1, "Channel %s: Blind transfer target '%s@%s'\n",
  3167. ast_channel_name(bridge_channel->chan), xfer_exten, xfer_context);
  3168. if (!ast_strlen_zero(goto_on_blindxfer)) {
  3169. const char *chan_context;
  3170. const char *chan_exten;
  3171. int chan_priority;
  3172. ast_debug(1, "Channel %s: After transfer, transferrer goes to %s\n",
  3173. ast_channel_name(bridge_channel->chan), goto_on_blindxfer);
  3174. ast_channel_lock(bridge_channel->chan);
  3175. chan_context = ast_strdupa(ast_channel_context(bridge_channel->chan));
  3176. chan_exten = ast_strdupa(ast_channel_exten(bridge_channel->chan));
  3177. chan_priority = ast_channel_priority(bridge_channel->chan);
  3178. ast_channel_unlock(bridge_channel->chan);
  3179. ast_bridge_set_after_go_on(bridge_channel->chan,
  3180. chan_context, chan_exten, chan_priority, goto_on_blindxfer);
  3181. }
  3182. if (ast_bridge_transfer_blind(0, bridge_channel->chan, xfer_exten, xfer_context,
  3183. blind_transfer_cb, bridge_channel->chan) != AST_BRIDGE_TRANSFER_SUCCESS
  3184. && !ast_strlen_zero(goto_on_blindxfer)) {
  3185. ast_bridge_discard_after_goto(bridge_channel->chan);
  3186. }
  3187. return 0;
  3188. }
  3189. struct ast_bridge_methods ast_bridge_basic_v_table;
  3190. struct ast_bridge_methods personality_normal_v_table;
  3191. struct ast_bridge_methods personality_atxfer_v_table;
  3192. static void bridge_basic_change_personality(struct ast_bridge *bridge,
  3193. enum bridge_basic_personality_type type, void *user_data)
  3194. {
  3195. struct bridge_basic_personality *personality = bridge->personality;
  3196. SCOPED_LOCK(lock, bridge, ast_bridge_lock, ast_bridge_unlock);
  3197. remove_hooks_on_personality_change(bridge);
  3198. ao2_cleanup(personality->details[personality->current].pvt);
  3199. personality->details[personality->current].pvt = NULL;
  3200. ast_clear_flag(&bridge->feature_flags, AST_FLAGS_ALL);
  3201. personality->current = type;
  3202. if (user_data) {
  3203. ao2_ref(user_data, +1);
  3204. }
  3205. personality->details[personality->current].pvt = user_data;
  3206. ast_set_flag(&bridge->feature_flags, personality->details[personality->current].bridge_flags);
  3207. if (personality->details[personality->current].on_personality_change) {
  3208. personality->details[personality->current].on_personality_change(bridge);
  3209. }
  3210. }
  3211. static void personality_destructor(void *obj)
  3212. {
  3213. struct bridge_basic_personality *personality = obj;
  3214. int i;
  3215. for (i = 0; i < BRIDGE_BASIC_PERSONALITY_END; ++i) {
  3216. ao2_cleanup(personality->details[i].pvt);
  3217. }
  3218. }
  3219. static void on_personality_change_normal(struct ast_bridge *bridge)
  3220. {
  3221. struct ast_bridge_channel *iter;
  3222. AST_LIST_TRAVERSE(&bridge->channels, iter, entry) {
  3223. if (add_normal_hooks(bridge, iter)) {
  3224. ast_log(LOG_WARNING, "Unable to set up bridge hooks for channel %s. Features may not work properly\n",
  3225. ast_channel_name(iter->chan));
  3226. }
  3227. }
  3228. }
  3229. static void init_details(struct personality_details *details,
  3230. enum bridge_basic_personality_type type)
  3231. {
  3232. switch (type) {
  3233. case BRIDGE_BASIC_PERSONALITY_NORMAL:
  3234. details->v_table = &personality_normal_v_table;
  3235. details->bridge_flags = NORMAL_FLAGS;
  3236. details->on_personality_change = on_personality_change_normal;
  3237. break;
  3238. case BRIDGE_BASIC_PERSONALITY_ATXFER:
  3239. details->v_table = &personality_atxfer_v_table;
  3240. details->bridge_flags = TRANSFER_FLAGS;
  3241. break;
  3242. default:
  3243. ast_log(LOG_WARNING, "Asked to initialize unexpected basic bridge personality type.\n");
  3244. break;
  3245. }
  3246. }
  3247. static struct ast_bridge *bridge_basic_personality_alloc(struct ast_bridge *bridge)
  3248. {
  3249. struct bridge_basic_personality *personality;
  3250. int i;
  3251. if (!bridge) {
  3252. return NULL;
  3253. }
  3254. personality = ao2_alloc(sizeof(*personality), personality_destructor);
  3255. if (!personality) {
  3256. ao2_ref(bridge, -1);
  3257. return NULL;
  3258. }
  3259. for (i = 0; i < BRIDGE_BASIC_PERSONALITY_END; ++i) {
  3260. init_details(&personality->details[i], i);
  3261. }
  3262. personality->current = BRIDGE_BASIC_PERSONALITY_NORMAL;
  3263. bridge->personality = personality;
  3264. return bridge;
  3265. }
  3266. struct ast_bridge *ast_bridge_basic_new(void)
  3267. {
  3268. struct ast_bridge *bridge;
  3269. bridge = bridge_alloc(sizeof(struct ast_bridge), &ast_bridge_basic_v_table);
  3270. bridge = bridge_base_init(bridge,
  3271. AST_BRIDGE_CAPABILITY_NATIVE | AST_BRIDGE_CAPABILITY_1TO1MIX
  3272. | AST_BRIDGE_CAPABILITY_MULTIMIX, NORMAL_FLAGS, NULL, NULL, NULL);
  3273. bridge = bridge_basic_personality_alloc(bridge);
  3274. bridge = bridge_register(bridge);
  3275. return bridge;
  3276. }
  3277. void ast_bridge_basic_set_flags(struct ast_bridge *bridge, unsigned int flags)
  3278. {
  3279. SCOPED_LOCK(lock, bridge, ast_bridge_lock, ast_bridge_unlock);
  3280. struct bridge_basic_personality *personality = bridge->personality;
  3281. personality->details[personality->current].bridge_flags |= flags;
  3282. ast_set_flag(&bridge->feature_flags, flags);
  3283. }
  3284. void ast_bridging_init_basic(void)
  3285. {
  3286. /* Setup bridge basic subclass v_table. */
  3287. ast_bridge_basic_v_table = ast_bridge_base_v_table;
  3288. ast_bridge_basic_v_table.name = "basic";
  3289. ast_bridge_basic_v_table.push = bridge_basic_push;
  3290. ast_bridge_basic_v_table.pull = bridge_basic_pull;
  3291. ast_bridge_basic_v_table.destroy = bridge_basic_destroy;
  3292. /*
  3293. * Personality vtables don't have the same rules as
  3294. * normal bridge vtables. These vtable functions are
  3295. * used as alterations to the ast_bridge_basic_v_table
  3296. * method functionality and are checked for NULL before
  3297. * calling.
  3298. */
  3299. personality_normal_v_table.name = "normal";
  3300. personality_normal_v_table.push = bridge_personality_normal_push;
  3301. personality_atxfer_v_table.name = "attended transfer";
  3302. personality_atxfer_v_table.push = bridge_personality_atxfer_push;
  3303. personality_atxfer_v_table.pull = bridge_personality_atxfer_pull;
  3304. ast_bridge_features_register(AST_BRIDGE_BUILTIN_ATTENDEDTRANSFER, feature_attended_transfer, NULL);
  3305. ast_bridge_features_register(AST_BRIDGE_BUILTIN_BLINDTRANSFER, feature_blind_transfer, NULL);
  3306. }