bridge_basic.c 112 KB

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