bridge_basic.c 106 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279
  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. ast_bridge_channel_update_accountcodes(bridge_channel, swap);
  592. ast_bridge_channel_update_linkedids(bridge_channel, swap);
  593. return 0;
  594. }
  595. static int bridge_basic_push(struct ast_bridge *self, struct ast_bridge_channel *bridge_channel, struct ast_bridge_channel *swap)
  596. {
  597. struct bridge_basic_personality *personality = self->personality;
  598. ast_assert(personality != NULL);
  599. if (personality->details[personality->current].v_table->push(self, bridge_channel, swap)) {
  600. return -1;
  601. }
  602. return ast_bridge_base_v_table.push(self, bridge_channel, swap);
  603. }
  604. static void bridge_basic_pull(struct ast_bridge *self, struct ast_bridge_channel *bridge_channel)
  605. {
  606. struct bridge_basic_personality *personality = self->personality;
  607. ast_assert(personality != NULL);
  608. if (personality->details[personality->current].v_table->pull) {
  609. personality->details[personality->current].v_table->pull(self, bridge_channel);
  610. }
  611. ast_bridge_base_v_table.pull(self, bridge_channel);
  612. }
  613. static void bridge_basic_destroy(struct ast_bridge *self)
  614. {
  615. struct bridge_basic_personality *personality = self->personality;
  616. ao2_cleanup(personality);
  617. ast_bridge_base_v_table.destroy(self);
  618. }
  619. /*!
  620. * \brief Remove appropriate hooks when basic bridge personality changes
  621. *
  622. * Hooks that have the AST_BRIDGE_HOOK_REMOVE_ON_PERSONALITY_CHANGE flag
  623. * set will be removed from all bridge channels in the bridge.
  624. *
  625. * \param bridge Basic bridge undergoing personality change
  626. */
  627. static void remove_hooks_on_personality_change(struct ast_bridge *bridge)
  628. {
  629. struct ast_bridge_channel *iter;
  630. AST_LIST_TRAVERSE(&bridge->channels, iter, entry) {
  631. SCOPED_LOCK(lock, iter, ast_bridge_channel_lock, ast_bridge_channel_unlock);
  632. ast_bridge_features_remove(iter->features, AST_BRIDGE_HOOK_REMOVE_ON_PERSONALITY_CHANGE);
  633. }
  634. }
  635. /*!
  636. * \brief Attended transfer superstates.
  637. *
  638. * An attended transfer's progress is facilitated by a state machine.
  639. * The individual states of the state machine fall into the realm of
  640. * one of two superstates.
  641. */
  642. enum attended_transfer_superstate {
  643. /*!
  644. * \brief Transfer superstate
  645. *
  646. * The attended transfer state machine begins in this superstate. The
  647. * goal of this state is for a transferer channel to facilitate a
  648. * transfer from a transferee to a transfer target.
  649. *
  650. * There are two bridges used in this superstate. The transferee bridge is
  651. * the bridge that the transferer and transferee channels originally
  652. * communicate in, and the target bridge is the bridge where the transfer
  653. * target is being dialed.
  654. *
  655. * The transferer channel is capable of moving between the bridges using
  656. * the DTMF swap sequence.
  657. */
  658. SUPERSTATE_TRANSFER,
  659. /*!
  660. * \brief Recall superstate
  661. *
  662. * The attended transfer state machine moves to this superstate if
  663. * atxferdropcall is set to "no" and the transferer channel hangs up
  664. * during a transfer. The goal in this superstate is to call back either
  665. * the transfer target or transferer and rebridge with the transferee
  666. * channel(s).
  667. *
  668. * In this superstate, there is only a single bridge used, the original
  669. * transferee bridge. Rather than distinguishing between a transferer
  670. * and transfer target, all outbound calls are toward a "recall_target"
  671. * channel.
  672. */
  673. SUPERSTATE_RECALL,
  674. };
  675. /*!
  676. * The states in the attended transfer state machine.
  677. */
  678. enum attended_transfer_state {
  679. /*!
  680. * \brief Calling Target state
  681. *
  682. * This state describes the initial state of a transfer. The transferer
  683. * waits in the transfer target's bridge for the transfer target to answer.
  684. *
  685. * Superstate: Transfer
  686. *
  687. * Preconditions:
  688. * 1) Transfer target is RINGING
  689. * 2) Transferer is in transferee bridge
  690. * 3) Transferee is on hold
  691. *
  692. * Transitions to TRANSFER_CALLING_TARGET:
  693. * 1) This is the initial state for an attended transfer.
  694. * 2) TRANSFER_HESITANT: Transferer presses DTMF swap sequence
  695. *
  696. * State operation:
  697. * The transferer is moved from the transferee bridge into the transfer
  698. * target bridge.
  699. *
  700. * Transitions from TRANSFER_CALLING_TARGET:
  701. * 1) TRANSFER_FAIL: Transferee hangs up.
  702. * 2) TRANSFER_BLOND: Transferer hangs up or presses DTMF swap sequence
  703. * and configured atxferdropcall setting is yes.
  704. * 3) TRANSFER_BLOND_NONFINAL: Transferer hangs up or presses DTMF swap
  705. * sequence and configured atxferdroppcall setting is no.
  706. * 4) TRANSFER_CONSULTING: Transfer target answers the call.
  707. * 5) TRANSFER_REBRIDGE: Transfer target hangs up, call to transfer target
  708. * times out, or transferer presses DTMF abort sequence.
  709. * 6) TRANSFER_THREEWAY: Transferer presses DTMF threeway sequence.
  710. * 7) TRANSFER_HESITANT: Transferer presses DTMF swap sequence.
  711. */
  712. TRANSFER_CALLING_TARGET,
  713. /*!
  714. * \brief Hesitant state
  715. *
  716. * This state only arises if when waiting for the transfer target to
  717. * answer, the transferer presses the DTMF swap sequence. This will
  718. * cause the transferer to be rebridged with the transferee temporarily.
  719. *
  720. * Superstate: Transfer
  721. *
  722. * Preconditions:
  723. * 1) Transfer target is in ringing state
  724. * 2) Transferer is in transfer target bridge
  725. * 3) Transferee is on hold
  726. *
  727. * Transitions to TRANSFER_HESITANT:
  728. * 1) TRANSFER_CALLING_TARGET: Transferer presses DTMF swap sequence.
  729. *
  730. * State operation:
  731. * The transferer is moved from the transfer target bridge into the
  732. * transferee bridge, and the transferee is taken off hold.
  733. *
  734. * Transitions from TRANSFER_HESITANT:
  735. * 1) TRANSFER_FAIL: Transferee hangs up
  736. * 2) TRANSFER_BLOND: Transferer hangs up or presses DTMF swap sequence
  737. * and configured atxferdropcall setting is yes.
  738. * 3) TRANSFER_BLOND_NONFINAL: Transferer hangs up or presses DTMF swap
  739. * sequence and configured atxferdroppcall setting is no.
  740. * 4) TRANSFER_DOUBLECHECKING: Transfer target answers the call
  741. * 5) TRANSFER_RESUME: Transfer target hangs up, call to transfer target
  742. * times out, or transferer presses DTMF abort sequence.
  743. * 6) TRANSFER_THREEWAY: Transferer presses DTMF threeway sequence.
  744. * 7) TRANSFER_CALLING_TARGET: Transferer presses DTMF swap sequence.
  745. */
  746. TRANSFER_HESITANT,
  747. /*!
  748. * \brief Rebridge state
  749. *
  750. * This is a terminal state that indicates that the transferer needs
  751. * to move back to the transferee's bridge. This is a failed attended
  752. * transfer result.
  753. *
  754. * Superstate: Transfer
  755. *
  756. * Preconditions:
  757. * 1) Transferer is in transfer target bridge
  758. * 2) Transferee is on hold
  759. *
  760. * Transitions to TRANSFER_REBRIDGE:
  761. * 1) TRANSFER_CALLING_TARGET: Transfer target hangs up, call to transfer target
  762. * times out, or transferer presses DTMF abort sequence.
  763. * 2) TRANSFER_STATE_CONSULTING: Transfer target hangs up, or transferer presses
  764. * DTMF abort sequence.
  765. *
  766. * State operation:
  767. * The transferer channel is moved from the transfer target bridge to the
  768. * transferee bridge. The transferee is taken off hold. A stasis transfer
  769. * message is published indicating a failed attended transfer.
  770. *
  771. * Transitions from TRANSFER_REBRIDGE:
  772. * None
  773. */
  774. TRANSFER_REBRIDGE,
  775. /*!
  776. * \brief Resume state
  777. *
  778. * This is a terminal state that indicates that the party bridged with the
  779. * transferee is the final party to be bridged with that transferee. This state
  780. * may come about due to a successful recall or due to a failed transfer.
  781. *
  782. * Superstate: Transfer or Recall
  783. *
  784. * Preconditions:
  785. * In Transfer Superstate:
  786. * 1) Transferer is in transferee bridge
  787. * 2) Transferee is not on hold
  788. * In Recall Superstate:
  789. * 1) The recall target is in the transferee bridge
  790. * 2) Transferee is not on hold
  791. *
  792. * Transitions to TRANSFER_RESUME:
  793. * TRANSFER_HESITANT: Transfer target hangs up, call to transfer target times out,
  794. * or transferer presses DTMF abort sequence.
  795. * TRANSFER_DOUBLECHECKING: Transfer target hangs up or transferer presses DTMF
  796. * abort sequence.
  797. * TRANSFER_BLOND_NONFINAL: Recall target answers
  798. * TRANSFER_RECALLING: Recall target answers
  799. * TRANSFER_RETRANSFER: Recall target answers
  800. *
  801. * State operations:
  802. * None
  803. *
  804. * Transitions from TRANSFER_RESUME:
  805. * None
  806. */
  807. TRANSFER_RESUME,
  808. /*!
  809. * \brief Threeway state
  810. *
  811. * This state results when the transferer wishes to have all parties involved
  812. * in a transfer to be in the same bridge together.
  813. *
  814. * Superstate: Transfer
  815. *
  816. * Preconditions:
  817. * 1) Transfer target state is either RINGING or UP
  818. * 2) Transferer is in either bridge
  819. * 3) Transferee is not on hold
  820. *
  821. * Transitions to TRANSFER_THREEWAY:
  822. * 1) TRANSFER_CALLING_TARGET: Transferer presses DTMF threeway sequence.
  823. * 2) TRANSFER_HESITANT: Transferer presses DTMF threeway sequence.
  824. * 3) TRANSFER_CONSULTING: Transferer presses DTMF threeway sequence.
  825. * 4) TRANSFER_DOUBLECHECKING: Transferer presses DTMF threeway sequence.
  826. *
  827. * State operation:
  828. * The transfer target bridge is merged into the transferee bridge.
  829. *
  830. * Transitions from TRANSFER_THREEWAY:
  831. * None.
  832. */
  833. TRANSFER_THREEWAY,
  834. /*!
  835. * \brief Consulting state
  836. *
  837. * This state describes the case where the transferer and transfer target
  838. * are able to converse in the transfer target's bridge prior to completing
  839. * the transfer.
  840. *
  841. * Superstate: Transfer
  842. *
  843. * Preconditions:
  844. * 1) Transfer target is UP
  845. * 2) Transferer is in target bridge
  846. * 3) Transferee is on hold
  847. *
  848. * Transitions to TRANSFER_CONSULTING:
  849. * 1) TRANSFER_CALLING_TARGET: Transfer target answers.
  850. * 2) TRANSFER_DOUBLECHECKING: Transferer presses DTMF swap sequence.
  851. *
  852. * State operations:
  853. * None.
  854. *
  855. * Transitions from TRANSFER_CONSULTING:
  856. * TRANSFER_COMPLETE: Transferer hangs up or transferer presses DTMF complete sequence.
  857. * TRANSFER_REBRIDGE: Transfer target hangs up or transferer presses DTMF abort sequence.
  858. * TRANSFER_THREEWAY: Transferer presses DTMF threeway sequence.
  859. * TRANSFER_DOUBLECHECKING: Transferer presses DTMF swap sequence.
  860. */
  861. TRANSFER_CONSULTING,
  862. /*!
  863. * \brief Double-checking state
  864. *
  865. * This state describes the case where the transferer and transferee are
  866. * able to converse in the transferee's bridge prior to completing the transfer. The
  867. * difference between this and TRANSFER_HESITANT is that the transfer target is
  868. * UP in this case.
  869. *
  870. * Superstate: Transfer
  871. *
  872. * Preconditions:
  873. * 1) Transfer target is UP and on hold
  874. * 2) Transferer is in transferee bridge
  875. * 3) Transferee is off hold
  876. *
  877. * Transitions to TRANSFER_DOUBLECHECKING:
  878. * 1) TRANSFER_HESITANT: Transfer target answers.
  879. * 2) TRANSFER_CONSULTING: Transferer presses DTMF swap sequence.
  880. *
  881. * State operations:
  882. * None.
  883. *
  884. * Transitions from TRANSFER_DOUBLECHECKING:
  885. * 1) TRANSFER_FAIL: Transferee hangs up.
  886. * 2) TRANSFER_COMPLETE: Transferer hangs up or presses DTMF complete sequence.
  887. * 3) TRANSFER_RESUME: Transfer target hangs up or transferer presses DTMF abort sequence.
  888. * 4) TRANSFER_THREEWAY: Transferer presses DTMF threeway sequence.
  889. * 5) TRANSFER_CONSULTING: Transferer presses the DTMF swap sequence.
  890. */
  891. TRANSFER_DOUBLECHECKING,
  892. /*!
  893. * \brief Complete state
  894. *
  895. * This is a terminal state where a transferer has successfully completed an attended
  896. * transfer. This state's goal is to get the transfer target and transferee into
  897. * the same bridge and the transferer off the call.
  898. *
  899. * Superstate: Transfer
  900. *
  901. * Preconditions:
  902. * 1) Transfer target is UP and off hold.
  903. * 2) Transferer is in either bridge.
  904. * 3) Transferee is off hold.
  905. *
  906. * Transitions to TRANSFER_COMPLETE:
  907. * 1) TRANSFER_CONSULTING: transferer hangs up or presses the DTMF complete sequence.
  908. * 2) TRANSFER_DOUBLECHECKING: transferer hangs up or presses the DTMF complete sequence.
  909. *
  910. * State operation:
  911. * The transfer target bridge is merged into the transferee bridge. The transferer
  912. * channel is kicked out of the bridges as part of the merge.
  913. *
  914. * State operations:
  915. * 1) Merge the transfer target bridge into the transferee bridge,
  916. * excluding the transferer channel from the merge.
  917. * 2) Publish a stasis transfer message.
  918. *
  919. * Exit operations:
  920. * This is a terminal state, so there are no exit operations.
  921. */
  922. TRANSFER_COMPLETE,
  923. /*!
  924. * \brief Blond state
  925. *
  926. * This is a terminal state where a transferer has completed an attended transfer prior
  927. * to the transfer target answering. This state is only entered if atxferdropcall
  928. * is set to 'yes'. This is considered to be a successful attended transfer.
  929. *
  930. * Superstate: Transfer
  931. *
  932. * Preconditions:
  933. * 1) Transfer target is RINGING.
  934. * 2) Transferer is in either bridge.
  935. * 3) Transferee is off hold.
  936. *
  937. * Transitions to TRANSFER_BLOND:
  938. * 1) TRANSFER_CALLING_TARGET: Transferer hangs up or presses the DTMF complete sequence.
  939. * atxferdropcall is set to 'yes'.
  940. * 2) TRANSFER_HESITANT: Transferer hangs up or presses the DTMF complete sequence.
  941. * atxferdropcall is set to 'yes'.
  942. *
  943. * State operations:
  944. * The transfer target bridge is merged into the transferee bridge. The transferer
  945. * channel is kicked out of the bridges as part of the merge. A stasis transfer
  946. * publication is sent indicating a successful transfer.
  947. *
  948. * Transitions from TRANSFER_BLOND:
  949. * None
  950. */
  951. TRANSFER_BLOND,
  952. /*!
  953. * \brief Blond non-final state
  954. *
  955. * This state is very similar to the TRANSFER_BLOND state, except that
  956. * this state is entered when atxferdropcall is set to 'no'. This is the
  957. * initial state of the Recall superstate, so state operations mainly involve
  958. * moving to the Recall superstate. This means that the transfer target, that
  959. * is currently ringing is now known as the recall target.
  960. *
  961. * Superstate: Recall
  962. *
  963. * Preconditions:
  964. * 1) Recall target is RINGING.
  965. * 2) Transferee is off hold.
  966. *
  967. * Transitions to TRANSFER_BLOND_NONFINAL:
  968. * 1) TRANSFER_CALLING_TARGET: Transferer hangs up or presses the DTMF complete sequence.
  969. * atxferdropcall is set to 'no'.
  970. * 2) TRANSFER_HESITANT: Transferer hangs up or presses the DTMF complete sequence.
  971. * atxferdropcall is set to 'no'.
  972. *
  973. * State operation:
  974. * The superstate of the attended transfer is changed from Transfer to Recall.
  975. * The transfer target bridge is merged into the transferee bridge. The transferer
  976. * channel is kicked out of the bridges as part of the merge.
  977. *
  978. * Transitions from TRANSFER_BLOND_NONFINAL:
  979. * 1) TRANSFER_FAIL: Transferee hangs up
  980. * 2) TRANSFER_RESUME: Recall target answers
  981. * 3) TRANSFER_RECALLING: Recall target hangs up or time expires.
  982. */
  983. TRANSFER_BLOND_NONFINAL,
  984. /*!
  985. * \brief Recalling state
  986. *
  987. * This state is entered if the recall target from the TRANSFER_BLOND_NONFINAL
  988. * or TRANSFER_RETRANSFER states hangs up or does not answer. The goal of this
  989. * state is to call back the original transferer in an attempt to recover the
  990. * original call.
  991. *
  992. * Superstate: Recall
  993. *
  994. * Preconditions:
  995. * 1) Recall target is down.
  996. * 2) Transferee is off hold.
  997. *
  998. * Transitions to TRANSFER_RECALLING:
  999. * 1) TRANSFER_BLOND_NONFINAL: Recall target hangs up or time expires.
  1000. * 2) TRANSFER_RETRANSFER: Recall target hangs up or time expires.
  1001. * atxferloopdelay is non-zero.
  1002. * 3) TRANSFER_WAIT_TO_RECALL: Time expires.
  1003. *
  1004. * State operation:
  1005. * The original transferer becomes the recall target and is called using the Dialing API.
  1006. * Ringing is indicated to the transferee.
  1007. *
  1008. * Transitions from TRANSFER_RECALLING:
  1009. * 1) TRANSFER_FAIL:
  1010. * a) Transferee hangs up.
  1011. * b) Recall target hangs up or time expires, and number of recall attempts exceeds atxfercallbackretries
  1012. * 2) TRANSFER_WAIT_TO_RETRANSFER: Recall target hangs up or time expires.
  1013. * atxferloopdelay is non-zero.
  1014. * 3) TRANSFER_RETRANSFER: Recall target hangs up or time expires.
  1015. * atxferloopdelay is zero.
  1016. * 4) TRANSFER_RESUME: Recall target answers.
  1017. */
  1018. TRANSFER_RECALLING,
  1019. /*!
  1020. * \brief Wait to Retransfer state
  1021. *
  1022. * This state is used simply to give a bit of breathing room between attempting
  1023. * to call back the original transferer and attempting to call back the original
  1024. * transfer target. The transferee hears music on hold during this state as an
  1025. * auditory clue that no one is currently being dialed.
  1026. *
  1027. * Superstate: Recall
  1028. *
  1029. * Preconditions:
  1030. * 1) Recall target is down.
  1031. * 2) Transferee is off hold.
  1032. *
  1033. * Transitions to TRANSFER_WAIT_TO_RETRANSFER:
  1034. * 1) TRANSFER_RECALLING: Recall target hangs up or time expires.
  1035. * atxferloopdelay is non-zero.
  1036. *
  1037. * State operation:
  1038. * The transferee is placed on hold.
  1039. *
  1040. * Transitions from TRANSFER_WAIT_TO_RETRANSFER:
  1041. * 1) TRANSFER_FAIL: Transferee hangs up.
  1042. * 2) TRANSFER_RETRANSFER: Time expires.
  1043. */
  1044. TRANSFER_WAIT_TO_RETRANSFER,
  1045. /*!
  1046. * \brief Retransfer state
  1047. *
  1048. * This state is used in order to attempt to call back the original
  1049. * transfer target channel from the transfer. The transferee hears
  1050. * ringing during this state as an auditory cue that a party is being
  1051. * dialed.
  1052. *
  1053. * Superstate: Recall
  1054. *
  1055. * Preconditions:
  1056. * 1) Recall target is down.
  1057. * 2) Transferee is off hold.
  1058. *
  1059. * Transitions to TRANSFER_RETRANSFER:
  1060. * 1) TRANSFER_RECALLING: Recall target hangs up or time expires.
  1061. * atxferloopdelay is zero.
  1062. * 2) TRANSFER_WAIT_TO_RETRANSFER: Time expires.
  1063. *
  1064. * State operation:
  1065. * The original transfer target is requested and is set as the recall target.
  1066. * The recall target is called and placed into the transferee bridge.
  1067. *
  1068. * Transitions from TRANSFER_RETRANSFER:
  1069. * 1) TRANSFER_FAIL: Transferee hangs up.
  1070. * 2) TRANSFER_WAIT_TO_RECALL: Recall target hangs up or time expires.
  1071. * atxferloopdelay is non-zero.
  1072. * 3) TRANSFER_RECALLING: Recall target hangs up or time expires.
  1073. * atxferloopdelay is zero.
  1074. */
  1075. TRANSFER_RETRANSFER,
  1076. /*!
  1077. * \brief Wait to recall state
  1078. *
  1079. * This state is used simply to give a bit of breathing room between attempting
  1080. * to call back the original transfer target and attempting to call back the
  1081. * original transferer. The transferee hears music on hold during this state as an
  1082. * auditory clue that no one is currently being dialed.
  1083. *
  1084. * Superstate: Recall
  1085. *
  1086. * Preconditions:
  1087. * 1) Recall target is down.
  1088. * 2) Transferee is off hold.
  1089. *
  1090. * Transitions to TRANSFER_WAIT_TO_RECALL:
  1091. * 1) TRANSFER_RETRANSFER: Recall target hangs up or time expires.
  1092. * atxferloopdelay is non-zero.
  1093. *
  1094. * State operation:
  1095. * Transferee is placed on hold.
  1096. *
  1097. * Transitions from TRANSFER_WAIT_TO_RECALL:
  1098. * 1) TRANSFER_FAIL: Transferee hangs up
  1099. * 2) TRANSFER_RECALLING: Time expires
  1100. */
  1101. TRANSFER_WAIT_TO_RECALL,
  1102. /*!
  1103. * \brief Fail state
  1104. *
  1105. * This state indicates that something occurred during the transfer that
  1106. * makes a graceful completion impossible. The most common stimulus for this
  1107. * state is when the transferee hangs up.
  1108. *
  1109. * Superstate: Transfer and Recall
  1110. *
  1111. * Preconditions:
  1112. * None
  1113. *
  1114. * Transitions to TRANSFER_FAIL:
  1115. * 1) TRANSFER_CALLING_TARGET: Transferee hangs up.
  1116. * 2) TRANSFER_HESITANT: Transferee hangs up.
  1117. * 3) TRANSFER_DOUBLECHECKING: Transferee hangs up.
  1118. * 4) TRANSFER_BLOND_NONFINAL: Transferee hangs up.
  1119. * 5) TRANSFER_RECALLING:
  1120. * a) Transferee hangs up.
  1121. * b) Recall target hangs up or time expires, and number of
  1122. * recall attempts exceeds atxfercallbackretries.
  1123. * 6) TRANSFER_WAIT_TO_RETRANSFER: Transferee hangs up.
  1124. * 7) TRANSFER_RETRANSFER: Transferee hangs up.
  1125. * 8) TRANSFER_WAIT_TO_RECALL: Transferee hangs up.
  1126. *
  1127. * State operation:
  1128. * A transfer stasis publication is made indicating a failed transfer.
  1129. * The transferee bridge is destroyed.
  1130. *
  1131. * Transitions from TRANSFER_FAIL:
  1132. * None.
  1133. */
  1134. TRANSFER_FAIL,
  1135. };
  1136. /*!
  1137. * \brief Stimuli that can cause transfer state changes
  1138. */
  1139. enum attended_transfer_stimulus {
  1140. /*! No stimulus. This literally can never happen. */
  1141. STIMULUS_NONE,
  1142. /*! All of the transferee channels have been hung up. */
  1143. STIMULUS_TRANSFEREE_HANGUP,
  1144. /*! The transferer has hung up. */
  1145. STIMULUS_TRANSFERER_HANGUP,
  1146. /*! The transfer target channel has hung up. */
  1147. STIMULUS_TRANSFER_TARGET_HANGUP,
  1148. /*! The transfer target channel has answered. */
  1149. STIMULUS_TRANSFER_TARGET_ANSWER,
  1150. /*! The recall target channel has hung up. */
  1151. STIMULUS_RECALL_TARGET_HANGUP,
  1152. /*! The recall target channel has answered. */
  1153. STIMULUS_RECALL_TARGET_ANSWER,
  1154. /*! The current state's timer has expired. */
  1155. STIMULUS_TIMEOUT,
  1156. /*! The transferer pressed the abort DTMF sequence. */
  1157. STIMULUS_DTMF_ATXFER_ABORT,
  1158. /*! The transferer pressed the complete DTMF sequence. */
  1159. STIMULUS_DTMF_ATXFER_COMPLETE,
  1160. /*! The transferer pressed the three-way DTMF sequence. */
  1161. STIMULUS_DTMF_ATXFER_THREEWAY,
  1162. /*! The transferer pressed the swap DTMF sequence. */
  1163. STIMULUS_DTMF_ATXFER_SWAP,
  1164. };
  1165. /*!
  1166. * \brief String representations of the various stimuli
  1167. *
  1168. * Used for debugging purposes
  1169. */
  1170. const char *stimulus_strs[] = {
  1171. [STIMULUS_NONE] = "None",
  1172. [STIMULUS_TRANSFEREE_HANGUP] = "Transferee Hangup",
  1173. [STIMULUS_TRANSFERER_HANGUP] = "Transferer Hangup",
  1174. [STIMULUS_TRANSFER_TARGET_HANGUP] = "Transfer Target Hangup",
  1175. [STIMULUS_TRANSFER_TARGET_ANSWER] = "Transfer Target Answer",
  1176. [STIMULUS_RECALL_TARGET_HANGUP] = "Recall Target Hangup",
  1177. [STIMULUS_RECALL_TARGET_ANSWER] = "Recall Target Answer",
  1178. [STIMULUS_TIMEOUT] = "Timeout",
  1179. [STIMULUS_DTMF_ATXFER_ABORT] = "DTMF Abort",
  1180. [STIMULUS_DTMF_ATXFER_COMPLETE] = "DTMF Complete",
  1181. [STIMULUS_DTMF_ATXFER_THREEWAY] = "DTMF Threeway",
  1182. [STIMULUS_DTMF_ATXFER_SWAP] = "DTMF Swap",
  1183. };
  1184. struct stimulus_list {
  1185. enum attended_transfer_stimulus stimulus;
  1186. AST_LIST_ENTRY(stimulus_list) next;
  1187. };
  1188. /*!
  1189. * \brief Collection of data related to an attended transfer attempt
  1190. */
  1191. struct attended_transfer_properties {
  1192. AST_DECLARE_STRING_FIELDS (
  1193. /*! Extension of transfer target */
  1194. AST_STRING_FIELD(exten);
  1195. /*! Context of transfer target */
  1196. AST_STRING_FIELD(context);
  1197. /*! Sound to play on failure */
  1198. AST_STRING_FIELD(failsound);
  1199. /*! Sound to play when transfer completes */
  1200. AST_STRING_FIELD(xfersound);
  1201. /*! The channel technology of the transferer channel */
  1202. AST_STRING_FIELD(transferer_type);
  1203. /*! The transferer channel address */
  1204. AST_STRING_FIELD(transferer_addr);
  1205. );
  1206. /*! Condition used to synchronize when stimuli are reported to the monitor thread */
  1207. ast_cond_t cond;
  1208. /*! The bridge where the transferee resides. This bridge is also the bridge that
  1209. * survives a successful attended transfer.
  1210. */
  1211. struct ast_bridge *transferee_bridge;
  1212. /*! The bridge used to place an outbound call to the transfer target. This
  1213. * bridge is merged with the transferee_bridge on a successful transfer.
  1214. */
  1215. struct ast_bridge *target_bridge;
  1216. /*! The party that performs the attended transfer. */
  1217. struct ast_channel *transferer;
  1218. /*! The local channel dialed to reach the transfer target. */
  1219. struct ast_channel *transfer_target;
  1220. /*! The party that is currently being recalled. Depending on
  1221. * the current state, this may be either the party that originally
  1222. * was the transferer or the original transfer target
  1223. */
  1224. struct ast_channel *recall_target;
  1225. /*! The absolute starting time for running timers */
  1226. struct timeval start;
  1227. AST_LIST_HEAD_NOLOCK(,stimulus_list) stimulus_queue;
  1228. /*! The current state of the attended transfer */
  1229. enum attended_transfer_state state;
  1230. /*! The current superstate of the attended transfer */
  1231. enum attended_transfer_superstate superstate;
  1232. /*! Configured atxferdropcall from features.conf */
  1233. int atxferdropcall;
  1234. /*! Configured atxfercallbackretries from features.conf */
  1235. int atxfercallbackretries;
  1236. /*! Configured atxferloopdelay from features.conf */
  1237. int atxferloopdelay;
  1238. /*! Configured atxfernoanswertimeout from features.conf */
  1239. int atxfernoanswertimeout;
  1240. /*! Count of the number of times that recalls have been attempted */
  1241. int retry_attempts;
  1242. /*! Framehook ID for outbounc call to transfer target or recall target */
  1243. int target_framehook_id;
  1244. /*! Dial structure used when recalling transferer channel */
  1245. struct ast_dial *dial;
  1246. /*! The bridging features the transferer has available */
  1247. struct ast_flags transferer_features;
  1248. };
  1249. static void attended_transfer_properties_destructor(void *obj)
  1250. {
  1251. struct attended_transfer_properties *props = obj;
  1252. ast_debug(1, "Destroy attended transfer properties %p\n", props);
  1253. ao2_cleanup(props->target_bridge);
  1254. ao2_cleanup(props->transferee_bridge);
  1255. /* Use ast_channel_cleanup() instead of ast_channel_unref() for channels since they may be NULL */
  1256. ast_channel_cleanup(props->transferer);
  1257. ast_channel_cleanup(props->transfer_target);
  1258. ast_channel_cleanup(props->recall_target);
  1259. ast_string_field_free_memory(props);
  1260. ast_cond_destroy(&props->cond);
  1261. }
  1262. /*!
  1263. * \internal
  1264. * \brief Determine the transfer context to use.
  1265. * \since 12.0.0
  1266. *
  1267. * \param transferer Channel initiating the transfer.
  1268. * \param context User supplied context if available. May be NULL.
  1269. *
  1270. * \return The context to use for the transfer.
  1271. */
  1272. static const char *get_transfer_context(struct ast_channel *transferer, const char *context)
  1273. {
  1274. if (!ast_strlen_zero(context)) {
  1275. return context;
  1276. }
  1277. context = pbx_builtin_getvar_helper(transferer, "TRANSFER_CONTEXT");
  1278. if (!ast_strlen_zero(context)) {
  1279. return context;
  1280. }
  1281. context = ast_channel_macrocontext(transferer);
  1282. if (!ast_strlen_zero(context)) {
  1283. return context;
  1284. }
  1285. context = ast_channel_context(transferer);
  1286. if (!ast_strlen_zero(context)) {
  1287. return context;
  1288. }
  1289. return "default";
  1290. }
  1291. /*!
  1292. * \brief Allocate and initialize attended transfer properties
  1293. *
  1294. * \param transferer The channel performing the attended transfer
  1295. * \param context Suggestion for what context the transfer target extension can be found in
  1296. *
  1297. * \retval NULL Failure to allocate or initialize
  1298. * \retval non-NULL Newly allocated properties
  1299. */
  1300. static struct attended_transfer_properties *attended_transfer_properties_alloc(
  1301. struct ast_channel *transferer, const char *context)
  1302. {
  1303. struct attended_transfer_properties *props;
  1304. char *tech;
  1305. char *addr;
  1306. char *serial;
  1307. RAII_VAR(struct ast_features_xfer_config *, xfer_cfg, NULL, ao2_cleanup);
  1308. struct ast_flags *transferer_features;
  1309. props = ao2_alloc(sizeof(*props), attended_transfer_properties_destructor);
  1310. if (!props || ast_string_field_init(props, 64)) {
  1311. return NULL;
  1312. }
  1313. ast_cond_init(&props->cond, NULL);
  1314. props->target_framehook_id = -1;
  1315. props->transferer = ast_channel_ref(transferer);
  1316. ast_channel_lock(props->transferer);
  1317. xfer_cfg = ast_get_chan_features_xfer_config(props->transferer);
  1318. if (!xfer_cfg) {
  1319. ast_log(LOG_ERROR, "Unable to get transfer configuration from channel %s\n", ast_channel_name(props->transferer));
  1320. ao2_ref(props, -1);
  1321. return NULL;
  1322. }
  1323. transferer_features = ast_bridge_features_ds_get(props->transferer);
  1324. if (transferer_features) {
  1325. props->transferer_features = *transferer_features;
  1326. }
  1327. props->atxferdropcall = xfer_cfg->atxferdropcall;
  1328. props->atxfercallbackretries = xfer_cfg->atxfercallbackretries;
  1329. props->atxfernoanswertimeout = xfer_cfg->atxfernoanswertimeout;
  1330. props->atxferloopdelay = xfer_cfg->atxferloopdelay;
  1331. ast_string_field_set(props, context, get_transfer_context(transferer, context));
  1332. ast_string_field_set(props, failsound, xfer_cfg->xferfailsound);
  1333. ast_string_field_set(props, xfersound, xfer_cfg->xfersound);
  1334. tech = ast_strdupa(ast_channel_name(props->transferer));
  1335. addr = strchr(tech, '/');
  1336. if (!addr) {
  1337. ast_log(LOG_ERROR, "Transferer channel name does not follow typical channel naming format (tech/address)\n");
  1338. ast_channel_unref(props->transferer);
  1339. return NULL;
  1340. }
  1341. *addr++ = '\0';
  1342. serial = strrchr(addr, '-');
  1343. if (serial) {
  1344. *serial = '\0';
  1345. }
  1346. ast_string_field_set(props, transferer_type, tech);
  1347. ast_string_field_set(props, transferer_addr, addr);
  1348. ast_channel_unlock(props->transferer);
  1349. ast_debug(1, "Allocated attended transfer properties %p for transfer from %s\n",
  1350. props, ast_channel_name(props->transferer));
  1351. return props;
  1352. }
  1353. /*!
  1354. * \brief Free backlog of stimuli in the queue
  1355. */
  1356. static void clear_stimulus_queue(struct attended_transfer_properties *props)
  1357. {
  1358. struct stimulus_list *list;
  1359. SCOPED_AO2LOCK(lock, props);
  1360. while ((list = AST_LIST_REMOVE_HEAD(&props->stimulus_queue, next))) {
  1361. ast_free(list);
  1362. }
  1363. }
  1364. /*!
  1365. * \brief Initiate shutdown of attended transfer properties
  1366. *
  1367. * Calling this indicates that the attended transfer properties are no longer needed
  1368. * because the transfer operation has concluded.
  1369. */
  1370. static void attended_transfer_properties_shutdown(struct attended_transfer_properties *props)
  1371. {
  1372. ast_debug(1, "Shutting down attended transfer %p\n", props);
  1373. if (props->transferee_bridge) {
  1374. bridge_basic_change_personality(props->transferee_bridge,
  1375. BRIDGE_BASIC_PERSONALITY_NORMAL, NULL);
  1376. ast_bridge_merge_inhibit(props->transferee_bridge, -1);
  1377. }
  1378. if (props->target_bridge) {
  1379. ast_bridge_destroy(props->target_bridge, 0);
  1380. props->target_bridge = NULL;
  1381. }
  1382. if (props->transferer) {
  1383. ast_channel_remove_bridge_role(props->transferer, AST_TRANSFERER_ROLE_NAME);
  1384. }
  1385. clear_stimulus_queue(props);
  1386. ao2_cleanup(props);
  1387. }
  1388. static void stimulate_attended_transfer(struct attended_transfer_properties *props,
  1389. enum attended_transfer_stimulus stimulus)
  1390. {
  1391. struct stimulus_list *list;
  1392. list = ast_calloc(1, sizeof(*list));
  1393. if (!list) {
  1394. ast_log(LOG_ERROR, "Unable to push event to attended transfer queue. Expect transfer to fail\n");
  1395. return;
  1396. }
  1397. list->stimulus = stimulus;
  1398. ao2_lock(props);
  1399. AST_LIST_INSERT_TAIL(&props->stimulus_queue, list, next);
  1400. ast_cond_signal(&props->cond);
  1401. ao2_unlock(props);
  1402. }
  1403. /*!
  1404. * \brief Send a stasis publication for a successful attended transfer
  1405. */
  1406. static void publish_transfer_success(struct attended_transfer_properties *props)
  1407. {
  1408. struct ast_bridge_channel_pair transferee = {
  1409. .channel = props->transferer,
  1410. .bridge = props->transferee_bridge,
  1411. };
  1412. struct ast_bridge_channel_pair transfer_target = {
  1413. .channel = props->transferer,
  1414. .bridge = props->target_bridge,
  1415. };
  1416. ast_bridge_publish_attended_transfer_bridge_merge(0, AST_BRIDGE_TRANSFER_SUCCESS,
  1417. &transferee, &transfer_target, props->transferee_bridge);
  1418. }
  1419. /*!
  1420. * \brief Send a stasis publication for an attended transfer that ends in a threeway call
  1421. */
  1422. static void publish_transfer_threeway(struct attended_transfer_properties *props)
  1423. {
  1424. struct ast_bridge_channel_pair transferee = {
  1425. .channel = props->transferer,
  1426. .bridge = props->transferee_bridge,
  1427. };
  1428. struct ast_bridge_channel_pair transfer_target = {
  1429. .channel = props->transferer,
  1430. .bridge = props->target_bridge,
  1431. };
  1432. struct ast_bridge_channel_pair threeway = {
  1433. .channel = props->transferer,
  1434. .bridge = props->transferee_bridge,
  1435. };
  1436. ast_bridge_publish_attended_transfer_threeway(0, AST_BRIDGE_TRANSFER_SUCCESS,
  1437. &transferee, &transfer_target, &threeway);
  1438. }
  1439. /*!
  1440. * \brief Send a stasis publication for a failed attended transfer
  1441. */
  1442. static void publish_transfer_fail(struct attended_transfer_properties *props)
  1443. {
  1444. struct ast_bridge_channel_pair transferee = {
  1445. .channel = props->transferer,
  1446. .bridge = props->transferee_bridge,
  1447. };
  1448. struct ast_bridge_channel_pair transfer_target = {
  1449. .channel = props->transferer,
  1450. .bridge = props->target_bridge,
  1451. };
  1452. ast_bridge_publish_attended_transfer_fail(0, AST_BRIDGE_TRANSFER_FAIL,
  1453. &transferee, &transfer_target);
  1454. }
  1455. /*!
  1456. * \brief Helper method to play a sound on a channel in a bridge
  1457. *
  1458. * \param chan The channel to play the sound to
  1459. * \param sound The sound to play
  1460. */
  1461. static void play_sound(struct ast_channel *chan, const char *sound)
  1462. {
  1463. RAII_VAR(struct ast_bridge_channel *, bridge_channel, NULL, ao2_cleanup);
  1464. ast_channel_lock(chan);
  1465. bridge_channel = ast_channel_get_bridge_channel(chan);
  1466. ast_channel_unlock(chan);
  1467. if (!bridge_channel) {
  1468. return;
  1469. }
  1470. ast_bridge_channel_queue_playfile(bridge_channel, NULL, sound, NULL);
  1471. }
  1472. /*!
  1473. * \brief Helper method to place a channel in a bridge on hold
  1474. */
  1475. static void hold(struct ast_channel *chan)
  1476. {
  1477. RAII_VAR(struct ast_bridge_channel *, bridge_channel, NULL, ao2_cleanup);
  1478. if (chan) {
  1479. ast_channel_lock(chan);
  1480. bridge_channel = ast_channel_get_bridge_channel(chan);
  1481. ast_channel_unlock(chan);
  1482. ast_assert(bridge_channel != NULL);
  1483. ast_bridge_channel_write_hold(bridge_channel, NULL);
  1484. }
  1485. }
  1486. /*!
  1487. * \brief Helper method to take a channel in a bridge off hold
  1488. */
  1489. static void unhold(struct ast_channel *chan)
  1490. {
  1491. RAII_VAR(struct ast_bridge_channel *, bridge_channel, NULL, ao2_cleanup);
  1492. ast_channel_lock(chan);
  1493. bridge_channel = ast_channel_get_bridge_channel(chan);
  1494. ast_channel_unlock(chan);
  1495. ast_assert(bridge_channel != NULL);
  1496. ast_bridge_channel_write_unhold(bridge_channel);
  1497. }
  1498. /*!
  1499. * \brief Helper method to send a ringing indication to a channel in a bridge
  1500. */
  1501. static void ringing(struct ast_channel *chan)
  1502. {
  1503. RAII_VAR(struct ast_bridge_channel *, bridge_channel, NULL, ao2_cleanup);
  1504. ast_channel_lock(chan);
  1505. bridge_channel = ast_channel_get_bridge_channel(chan);
  1506. ast_channel_unlock(chan);
  1507. ast_assert(bridge_channel != NULL);
  1508. ast_bridge_channel_write_control_data(bridge_channel, AST_CONTROL_RINGING, NULL, 0);
  1509. }
  1510. /*!
  1511. * \brief Helper method to send a ringing indication to all channels in a bridge
  1512. */
  1513. static void bridge_ringing(struct ast_bridge *bridge)
  1514. {
  1515. struct ast_frame ringing = {
  1516. .frametype = AST_FRAME_CONTROL,
  1517. .subclass.integer = AST_CONTROL_RINGING,
  1518. };
  1519. ast_bridge_queue_everyone_else(bridge, NULL, &ringing);
  1520. }
  1521. /*!
  1522. * \brief Helper method to send a hold frame to all channels in a bridge
  1523. */
  1524. static void bridge_hold(struct ast_bridge *bridge)
  1525. {
  1526. struct ast_frame hold = {
  1527. .frametype = AST_FRAME_CONTROL,
  1528. .subclass.integer = AST_CONTROL_HOLD,
  1529. };
  1530. ast_bridge_queue_everyone_else(bridge, NULL, &hold);
  1531. }
  1532. /*!
  1533. * \brief Helper method to send an unhold frame to all channels in a bridge
  1534. */
  1535. static void bridge_unhold(struct ast_bridge *bridge)
  1536. {
  1537. struct ast_frame unhold = {
  1538. .frametype = AST_FRAME_CONTROL,
  1539. .subclass.integer = AST_CONTROL_UNHOLD,
  1540. };
  1541. ast_bridge_queue_everyone_else(bridge, NULL, &unhold);
  1542. }
  1543. /*!
  1544. * \brief Wrapper for \ref bridge_do_move
  1545. */
  1546. static int bridge_move(struct ast_bridge *dest, struct ast_bridge *src, struct ast_channel *channel, struct ast_channel *swap)
  1547. {
  1548. int res;
  1549. RAII_VAR(struct ast_bridge_channel *, bridge_channel, NULL, ao2_cleanup);
  1550. ast_bridge_lock_both(src, dest);
  1551. ast_channel_lock(channel);
  1552. bridge_channel = ast_channel_get_bridge_channel(channel);
  1553. ast_channel_unlock(channel);
  1554. ast_assert(bridge_channel != NULL);
  1555. ao2_lock(bridge_channel);
  1556. bridge_channel->swap = swap;
  1557. ao2_unlock(bridge_channel);
  1558. res = bridge_do_move(dest, bridge_channel, 1, 0);
  1559. ast_bridge_unlock(dest);
  1560. ast_bridge_unlock(src);
  1561. return res;
  1562. }
  1563. /*!
  1564. * \brief Wrapper for \ref bridge_do_merge
  1565. */
  1566. static void bridge_merge(struct ast_bridge *dest, struct ast_bridge *src, struct ast_channel **kick_channels, unsigned int num_channels)
  1567. {
  1568. struct ast_bridge_channel **kick_bridge_channels = num_channels ?
  1569. ast_alloca(num_channels * sizeof(*kick_bridge_channels)) : NULL;
  1570. int i;
  1571. int num_bridge_channels = 0;
  1572. ast_bridge_lock_both(dest, src);
  1573. for (i = 0; i < num_channels; ++i) {
  1574. struct ast_bridge_channel *kick_bridge_channel;
  1575. kick_bridge_channel = bridge_find_channel(src, kick_channels[i]);
  1576. if (!kick_bridge_channel) {
  1577. kick_bridge_channel = bridge_find_channel(dest, kick_channels[i]);
  1578. }
  1579. /* It's possible (and fine) for the bridge channel to be NULL at this point if the
  1580. * channel has hung up already. If that happens, we can just remove it from the list
  1581. * of bridge channels to kick from the bridge
  1582. */
  1583. if (!kick_bridge_channel) {
  1584. continue;
  1585. }
  1586. kick_bridge_channels[num_bridge_channels++] = kick_bridge_channel;
  1587. }
  1588. bridge_do_merge(dest, src, kick_bridge_channels, num_bridge_channels, 0);
  1589. ast_bridge_unlock(dest);
  1590. ast_bridge_unlock(src);
  1591. }
  1592. /*!
  1593. * \brief Flags that indicate properties of attended transfer states
  1594. */
  1595. enum attended_transfer_state_flags {
  1596. /*! This state requires that the timer be reset when entering the state */
  1597. TRANSFER_STATE_FLAG_TIMER_RESET = (1 << 0),
  1598. /*! This state's timer uses atxferloopdelay */
  1599. TRANSFER_STATE_FLAG_TIMER_LOOP_DELAY = (1 << 1),
  1600. /*! This state's timer uses atxfernoanswertimeout */
  1601. TRANSFER_STATE_FLAG_ATXFER_NO_ANSWER = (1 << 2),
  1602. /*! This state has a time limit associated with it */
  1603. TRANSFER_STATE_FLAG_TIMED = (TRANSFER_STATE_FLAG_TIMER_RESET |
  1604. TRANSFER_STATE_FLAG_TIMER_LOOP_DELAY | TRANSFER_STATE_FLAG_ATXFER_NO_ANSWER),
  1605. /*! This state does not transition to any other states */
  1606. TRANSFER_STATE_FLAG_TERMINAL = (1 << 3),
  1607. };
  1608. static int calling_target_enter(struct attended_transfer_properties *props);
  1609. static enum attended_transfer_state calling_target_exit(struct attended_transfer_properties *props,
  1610. enum attended_transfer_stimulus stimulus);
  1611. static int hesitant_enter(struct attended_transfer_properties *props);
  1612. static enum attended_transfer_state hesitant_exit(struct attended_transfer_properties *props,
  1613. enum attended_transfer_stimulus stimulus);
  1614. static int rebridge_enter(struct attended_transfer_properties *props);
  1615. static int resume_enter(struct attended_transfer_properties *props);
  1616. static int threeway_enter(struct attended_transfer_properties *props);
  1617. static int consulting_enter(struct attended_transfer_properties *props);
  1618. static enum attended_transfer_state consulting_exit(struct attended_transfer_properties *props,
  1619. enum attended_transfer_stimulus stimulus);
  1620. static int double_checking_enter(struct attended_transfer_properties *props);
  1621. static enum attended_transfer_state double_checking_exit(struct attended_transfer_properties *props,
  1622. enum attended_transfer_stimulus stimulus);
  1623. static int complete_enter(struct attended_transfer_properties *props);
  1624. static int blond_enter(struct attended_transfer_properties *props);
  1625. static int blond_nonfinal_enter(struct attended_transfer_properties *props);
  1626. static enum attended_transfer_state blond_nonfinal_exit(struct attended_transfer_properties *props,
  1627. enum attended_transfer_stimulus stimulus);
  1628. static int recalling_enter(struct attended_transfer_properties *props);
  1629. static enum attended_transfer_state recalling_exit(struct attended_transfer_properties *props,
  1630. enum attended_transfer_stimulus stimulus);
  1631. static int wait_to_retransfer_enter(struct attended_transfer_properties *props);
  1632. static enum attended_transfer_state wait_to_retransfer_exit(struct attended_transfer_properties *props,
  1633. enum attended_transfer_stimulus stimulus);
  1634. static int retransfer_enter(struct attended_transfer_properties *props);
  1635. static enum attended_transfer_state retransfer_exit(struct attended_transfer_properties *props,
  1636. enum attended_transfer_stimulus stimulus);
  1637. static int wait_to_recall_enter(struct attended_transfer_properties *props);
  1638. static enum attended_transfer_state wait_to_recall_exit(struct attended_transfer_properties *props,
  1639. enum attended_transfer_stimulus stimulus);
  1640. static int fail_enter(struct attended_transfer_properties *props);
  1641. /*!
  1642. * \brief Properties of an attended transfer state
  1643. */
  1644. struct attended_transfer_state_properties {
  1645. /*! The name of the state. Used for debugging */
  1646. const char *state_name;
  1647. /*! Function used to enter a state */
  1648. int (*enter)(struct attended_transfer_properties *props);
  1649. /*!
  1650. * Function used to exit a state
  1651. * This is used both to determine what the next state
  1652. * to transition to will be and to perform any cleanup
  1653. * necessary before exiting the current state.
  1654. */
  1655. enum attended_transfer_state (*exit)(struct attended_transfer_properties *props,
  1656. enum attended_transfer_stimulus stimulus);
  1657. /*! Flags associated with this state */
  1658. enum attended_transfer_state_flags flags;
  1659. };
  1660. static const struct attended_transfer_state_properties state_properties[] = {
  1661. [TRANSFER_CALLING_TARGET] = {
  1662. .state_name = "Calling Target",
  1663. .enter = calling_target_enter,
  1664. .exit = calling_target_exit,
  1665. .flags = TRANSFER_STATE_FLAG_ATXFER_NO_ANSWER | TRANSFER_STATE_FLAG_TIMER_RESET,
  1666. },
  1667. [TRANSFER_HESITANT] = {
  1668. .state_name = "Hesitant",
  1669. .enter = hesitant_enter,
  1670. .exit = hesitant_exit,
  1671. .flags = TRANSFER_STATE_FLAG_ATXFER_NO_ANSWER,
  1672. },
  1673. [TRANSFER_REBRIDGE] = {
  1674. .state_name = "Rebridge",
  1675. .enter = rebridge_enter,
  1676. .flags = TRANSFER_STATE_FLAG_TERMINAL,
  1677. },
  1678. [TRANSFER_RESUME] = {
  1679. .state_name = "Resume",
  1680. .enter = resume_enter,
  1681. .flags = TRANSFER_STATE_FLAG_TERMINAL,
  1682. },
  1683. [TRANSFER_THREEWAY] = {
  1684. .state_name = "Threeway",
  1685. .enter = threeway_enter,
  1686. .flags = TRANSFER_STATE_FLAG_TERMINAL,
  1687. },
  1688. [TRANSFER_CONSULTING] = {
  1689. .state_name = "Consulting",
  1690. .enter = consulting_enter,
  1691. .exit = consulting_exit,
  1692. },
  1693. [TRANSFER_DOUBLECHECKING] = {
  1694. .state_name = "Double Checking",
  1695. .enter = double_checking_enter,
  1696. .exit = double_checking_exit,
  1697. },
  1698. [TRANSFER_COMPLETE] = {
  1699. .state_name = "Complete",
  1700. .enter = complete_enter,
  1701. .flags = TRANSFER_STATE_FLAG_TERMINAL,
  1702. },
  1703. [TRANSFER_BLOND] = {
  1704. .state_name = "Blond",
  1705. .enter = blond_enter,
  1706. .flags = TRANSFER_STATE_FLAG_TERMINAL,
  1707. },
  1708. [TRANSFER_BLOND_NONFINAL] = {
  1709. .state_name = "Blond Non-Final",
  1710. .enter = blond_nonfinal_enter,
  1711. .exit = blond_nonfinal_exit,
  1712. .flags = TRANSFER_STATE_FLAG_ATXFER_NO_ANSWER,
  1713. },
  1714. [TRANSFER_RECALLING] = {
  1715. .state_name = "Recalling",
  1716. .enter = recalling_enter,
  1717. .exit = recalling_exit,
  1718. .flags = TRANSFER_STATE_FLAG_ATXFER_NO_ANSWER | TRANSFER_STATE_FLAG_TIMER_RESET,
  1719. },
  1720. [TRANSFER_WAIT_TO_RETRANSFER] = {
  1721. .state_name = "Wait to Retransfer",
  1722. .enter = wait_to_retransfer_enter,
  1723. .exit = wait_to_retransfer_exit,
  1724. .flags = TRANSFER_STATE_FLAG_TIMER_RESET | TRANSFER_STATE_FLAG_TIMER_LOOP_DELAY,
  1725. },
  1726. [TRANSFER_RETRANSFER] = {
  1727. .state_name = "Retransfer",
  1728. .enter = retransfer_enter,
  1729. .exit = retransfer_exit,
  1730. .flags = TRANSFER_STATE_FLAG_ATXFER_NO_ANSWER | TRANSFER_STATE_FLAG_TIMER_RESET,
  1731. },
  1732. [TRANSFER_WAIT_TO_RECALL] = {
  1733. .state_name = "Wait to Recall",
  1734. .enter = wait_to_recall_enter,
  1735. .exit = wait_to_recall_exit,
  1736. .flags = TRANSFER_STATE_FLAG_TIMER_RESET | TRANSFER_STATE_FLAG_TIMER_LOOP_DELAY,
  1737. },
  1738. [TRANSFER_FAIL] = {
  1739. .state_name = "Fail",
  1740. .enter = fail_enter,
  1741. .flags = TRANSFER_STATE_FLAG_TERMINAL,
  1742. },
  1743. };
  1744. static int calling_target_enter(struct attended_transfer_properties *props)
  1745. {
  1746. return bridge_move(props->target_bridge, props->transferee_bridge, props->transferer, NULL);
  1747. }
  1748. static enum attended_transfer_state calling_target_exit(struct attended_transfer_properties *props,
  1749. enum attended_transfer_stimulus stimulus)
  1750. {
  1751. switch (stimulus) {
  1752. case STIMULUS_TRANSFEREE_HANGUP:
  1753. play_sound(props->transferer, props->failsound);
  1754. publish_transfer_fail(props);
  1755. return TRANSFER_FAIL;
  1756. case STIMULUS_DTMF_ATXFER_COMPLETE:
  1757. case STIMULUS_TRANSFERER_HANGUP:
  1758. bridge_unhold(props->transferee_bridge);
  1759. return props->atxferdropcall ? TRANSFER_BLOND : TRANSFER_BLOND_NONFINAL;
  1760. case STIMULUS_TRANSFER_TARGET_ANSWER:
  1761. return TRANSFER_CONSULTING;
  1762. case STIMULUS_TRANSFER_TARGET_HANGUP:
  1763. case STIMULUS_TIMEOUT:
  1764. case STIMULUS_DTMF_ATXFER_ABORT:
  1765. play_sound(props->transferer, props->failsound);
  1766. return TRANSFER_REBRIDGE;
  1767. case STIMULUS_DTMF_ATXFER_THREEWAY:
  1768. bridge_unhold(props->transferee_bridge);
  1769. return TRANSFER_THREEWAY;
  1770. case STIMULUS_DTMF_ATXFER_SWAP:
  1771. return TRANSFER_HESITANT;
  1772. case STIMULUS_NONE:
  1773. case STIMULUS_RECALL_TARGET_ANSWER:
  1774. case STIMULUS_RECALL_TARGET_HANGUP:
  1775. default:
  1776. ast_log(LOG_WARNING, "Unexpected stimulus '%s' received in attended transfer state '%s'\n",
  1777. stimulus_strs[stimulus], state_properties[props->state].state_name);
  1778. return props->state;
  1779. }
  1780. }
  1781. static int hesitant_enter(struct attended_transfer_properties *props)
  1782. {
  1783. if (bridge_move(props->transferee_bridge, props->target_bridge, props->transferer, NULL)) {
  1784. return -1;
  1785. }
  1786. unhold(props->transferer);
  1787. return 0;
  1788. }
  1789. static enum attended_transfer_state hesitant_exit(struct attended_transfer_properties *props,
  1790. enum attended_transfer_stimulus stimulus)
  1791. {
  1792. switch (stimulus) {
  1793. case STIMULUS_TRANSFEREE_HANGUP:
  1794. play_sound(props->transferer, props->failsound);
  1795. publish_transfer_fail(props);
  1796. return TRANSFER_FAIL;
  1797. case STIMULUS_DTMF_ATXFER_COMPLETE:
  1798. case STIMULUS_TRANSFERER_HANGUP:
  1799. return props->atxferdropcall ? TRANSFER_BLOND : TRANSFER_BLOND_NONFINAL;
  1800. case STIMULUS_TRANSFER_TARGET_ANSWER:
  1801. return TRANSFER_DOUBLECHECKING;
  1802. case STIMULUS_TRANSFER_TARGET_HANGUP:
  1803. case STIMULUS_TIMEOUT:
  1804. case STIMULUS_DTMF_ATXFER_ABORT:
  1805. play_sound(props->transferer, props->failsound);
  1806. return TRANSFER_RESUME;
  1807. case STIMULUS_DTMF_ATXFER_THREEWAY:
  1808. return TRANSFER_THREEWAY;
  1809. case STIMULUS_DTMF_ATXFER_SWAP:
  1810. hold(props->transferer);
  1811. return TRANSFER_CALLING_TARGET;
  1812. case STIMULUS_NONE:
  1813. case STIMULUS_RECALL_TARGET_HANGUP:
  1814. case STIMULUS_RECALL_TARGET_ANSWER:
  1815. default:
  1816. ast_log(LOG_WARNING, "Unexpected stimulus '%s' received in attended transfer state '%s'\n",
  1817. stimulus_strs[stimulus], state_properties[props->state].state_name);
  1818. return props->state;
  1819. }
  1820. }
  1821. static int rebridge_enter(struct attended_transfer_properties *props)
  1822. {
  1823. if (bridge_move(props->transferee_bridge, props->target_bridge,
  1824. props->transferer, NULL)) {
  1825. return -1;
  1826. }
  1827. unhold(props->transferer);
  1828. return 0;
  1829. }
  1830. static int resume_enter(struct attended_transfer_properties *props)
  1831. {
  1832. return 0;
  1833. }
  1834. static int threeway_enter(struct attended_transfer_properties *props)
  1835. {
  1836. bridge_merge(props->transferee_bridge, props->target_bridge, NULL, 0);
  1837. play_sound(props->transfer_target, props->xfersound);
  1838. play_sound(props->transferer, props->xfersound);
  1839. publish_transfer_threeway(props);
  1840. return 0;
  1841. }
  1842. static int consulting_enter(struct attended_transfer_properties *props)
  1843. {
  1844. return 0;
  1845. }
  1846. static enum attended_transfer_state consulting_exit(struct attended_transfer_properties *props,
  1847. enum attended_transfer_stimulus stimulus)
  1848. {
  1849. switch (stimulus) {
  1850. case STIMULUS_TRANSFEREE_HANGUP:
  1851. /* This is a one-of-a-kind event. The transferer and transfer target are talking in
  1852. * one bridge, and the transferee has hung up in a separate bridge. In this case, we
  1853. * will change the personality of the transfer target bridge back to normal, and play
  1854. * a sound to the transferer to indicate the transferee is gone.
  1855. */
  1856. bridge_basic_change_personality(props->target_bridge, BRIDGE_BASIC_PERSONALITY_NORMAL, NULL);
  1857. play_sound(props->transferer, props->failsound);
  1858. ast_bridge_merge_inhibit(props->target_bridge, -1);
  1859. /* These next two lines are here to ensure that our reference to the target bridge
  1860. * is cleaned up properly and that the target bridge is not destroyed when the
  1861. * monitor thread exits
  1862. */
  1863. ao2_ref(props->target_bridge, -1);
  1864. props->target_bridge = NULL;
  1865. return TRANSFER_FAIL;
  1866. case STIMULUS_TRANSFERER_HANGUP:
  1867. case STIMULUS_DTMF_ATXFER_COMPLETE:
  1868. /* We know the transferer is in the target_bridge, so take the other bridge off hold */
  1869. bridge_unhold(props->transferee_bridge);
  1870. return TRANSFER_COMPLETE;
  1871. case STIMULUS_TRANSFER_TARGET_HANGUP:
  1872. case STIMULUS_DTMF_ATXFER_ABORT:
  1873. play_sound(props->transferer, props->failsound);
  1874. return TRANSFER_REBRIDGE;
  1875. case STIMULUS_DTMF_ATXFER_THREEWAY:
  1876. bridge_unhold(props->transferee_bridge);
  1877. return TRANSFER_THREEWAY;
  1878. case STIMULUS_DTMF_ATXFER_SWAP:
  1879. hold(props->transferer);
  1880. bridge_move(props->transferee_bridge, props->target_bridge, props->transferer, NULL);
  1881. unhold(props->transferer);
  1882. return TRANSFER_DOUBLECHECKING;
  1883. case STIMULUS_NONE:
  1884. case STIMULUS_TIMEOUT:
  1885. case STIMULUS_TRANSFER_TARGET_ANSWER:
  1886. case STIMULUS_RECALL_TARGET_HANGUP:
  1887. case STIMULUS_RECALL_TARGET_ANSWER:
  1888. default:
  1889. ast_log(LOG_WARNING, "Unexpected stimulus '%s' received in attended transfer state '%s'\n",
  1890. stimulus_strs[stimulus], state_properties[props->state].state_name);
  1891. return props->state;
  1892. }
  1893. }
  1894. static int double_checking_enter(struct attended_transfer_properties *props)
  1895. {
  1896. return 0;
  1897. }
  1898. static enum attended_transfer_state double_checking_exit(struct attended_transfer_properties *props,
  1899. enum attended_transfer_stimulus stimulus)
  1900. {
  1901. switch (stimulus) {
  1902. case STIMULUS_TRANSFEREE_HANGUP:
  1903. play_sound(props->transferer, props->failsound);
  1904. publish_transfer_fail(props);
  1905. return TRANSFER_FAIL;
  1906. case STIMULUS_TRANSFERER_HANGUP:
  1907. case STIMULUS_DTMF_ATXFER_COMPLETE:
  1908. /* We know the transferer is in the transferee, so take the other bridge off hold */
  1909. bridge_unhold(props->target_bridge);
  1910. return TRANSFER_COMPLETE;
  1911. case STIMULUS_TRANSFER_TARGET_HANGUP:
  1912. case STIMULUS_DTMF_ATXFER_ABORT:
  1913. play_sound(props->transferer, props->failsound);
  1914. return TRANSFER_RESUME;
  1915. case STIMULUS_DTMF_ATXFER_THREEWAY:
  1916. bridge_unhold(props->target_bridge);
  1917. return TRANSFER_THREEWAY;
  1918. case STIMULUS_DTMF_ATXFER_SWAP:
  1919. hold(props->transferer);
  1920. bridge_move(props->target_bridge, props->transferee_bridge, props->transferer, NULL);
  1921. unhold(props->transferer);
  1922. return TRANSFER_CONSULTING;
  1923. case STIMULUS_NONE:
  1924. case STIMULUS_TIMEOUT:
  1925. case STIMULUS_TRANSFER_TARGET_ANSWER:
  1926. case STIMULUS_RECALL_TARGET_HANGUP:
  1927. case STIMULUS_RECALL_TARGET_ANSWER:
  1928. default:
  1929. ast_log(LOG_WARNING, "Unexpected stimulus '%s' received in attended transfer state '%s'\n",
  1930. stimulus_strs[stimulus], state_properties[props->state].state_name);
  1931. return props->state;
  1932. }
  1933. }
  1934. static int complete_enter(struct attended_transfer_properties *props)
  1935. {
  1936. bridge_merge(props->transferee_bridge, props->target_bridge, &props->transferer, 1);
  1937. play_sound(props->transfer_target, props->xfersound);
  1938. publish_transfer_success(props);
  1939. return 0;
  1940. }
  1941. static int blond_enter(struct attended_transfer_properties *props)
  1942. {
  1943. bridge_merge(props->transferee_bridge, props->target_bridge, &props->transferer, 1);
  1944. ringing(props->transfer_target);
  1945. publish_transfer_success(props);
  1946. return 0;
  1947. }
  1948. static int blond_nonfinal_enter(struct attended_transfer_properties *props)
  1949. {
  1950. int res;
  1951. props->superstate = SUPERSTATE_RECALL;
  1952. props->recall_target = ast_channel_ref(props->transfer_target);
  1953. res = blond_enter(props);
  1954. props->transfer_target = ast_channel_unref(props->transfer_target);
  1955. return res;
  1956. }
  1957. static enum attended_transfer_state blond_nonfinal_exit(struct attended_transfer_properties *props,
  1958. enum attended_transfer_stimulus stimulus)
  1959. {
  1960. switch (stimulus) {
  1961. case STIMULUS_TRANSFEREE_HANGUP:
  1962. return TRANSFER_FAIL;
  1963. case STIMULUS_RECALL_TARGET_ANSWER:
  1964. return TRANSFER_RESUME;
  1965. case STIMULUS_TIMEOUT:
  1966. ast_softhangup(props->recall_target, AST_SOFTHANGUP_EXPLICIT);
  1967. props->recall_target = ast_channel_unref(props->recall_target);
  1968. case STIMULUS_RECALL_TARGET_HANGUP:
  1969. return TRANSFER_RECALLING;
  1970. case STIMULUS_NONE:
  1971. case STIMULUS_DTMF_ATXFER_ABORT:
  1972. case STIMULUS_DTMF_ATXFER_COMPLETE:
  1973. case STIMULUS_DTMF_ATXFER_THREEWAY:
  1974. case STIMULUS_DTMF_ATXFER_SWAP:
  1975. case STIMULUS_TRANSFERER_HANGUP:
  1976. case STIMULUS_TRANSFER_TARGET_HANGUP:
  1977. case STIMULUS_TRANSFER_TARGET_ANSWER:
  1978. default:
  1979. ast_log(LOG_WARNING, "Unexpected stimulus '%s' received in attended transfer state '%s'\n",
  1980. stimulus_strs[stimulus], state_properties[props->state].state_name);
  1981. return props->state;
  1982. }
  1983. }
  1984. /*!
  1985. * \brief Dial callback when attempting to recall the original transferer channel
  1986. *
  1987. * This is how we can monitor if the recall target has answered or has hung up.
  1988. * If one of the two is detected, then an appropriate stimulus is sent to the
  1989. * attended transfer monitor thread.
  1990. */
  1991. static void recall_callback(struct ast_dial *dial)
  1992. {
  1993. struct attended_transfer_properties *props = ast_dial_get_user_data(dial);
  1994. switch (ast_dial_state(dial)) {
  1995. default:
  1996. case AST_DIAL_RESULT_INVALID:
  1997. case AST_DIAL_RESULT_FAILED:
  1998. case AST_DIAL_RESULT_TIMEOUT:
  1999. case AST_DIAL_RESULT_HANGUP:
  2000. case AST_DIAL_RESULT_UNANSWERED:
  2001. /* Failure cases */
  2002. stimulate_attended_transfer(props, STIMULUS_RECALL_TARGET_HANGUP);
  2003. break;
  2004. case AST_DIAL_RESULT_RINGING:
  2005. case AST_DIAL_RESULT_PROGRESS:
  2006. case AST_DIAL_RESULT_PROCEEDING:
  2007. case AST_DIAL_RESULT_TRYING:
  2008. /* Don't care about these cases */
  2009. break;
  2010. case AST_DIAL_RESULT_ANSWERED:
  2011. /* We struck gold! */
  2012. props->recall_target = ast_dial_answered_steal(dial);
  2013. stimulate_attended_transfer(props, STIMULUS_RECALL_TARGET_ANSWER);
  2014. break;
  2015. }
  2016. }
  2017. static int recalling_enter(struct attended_transfer_properties *props)
  2018. {
  2019. RAII_VAR(struct ast_format_cap *, cap, ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_NOLOCK), ast_format_cap_destroy);
  2020. struct ast_format fmt;
  2021. if (!cap) {
  2022. return -1;
  2023. }
  2024. ast_format_cap_add(cap, ast_format_set(&fmt, AST_FORMAT_SLINEAR, 0));
  2025. /* When we dial the transfer target, since we are communicating
  2026. * with a local channel, we can place the local channel in a bridge
  2027. * and then call out to it. When recalling the transferer, though, we
  2028. * have to use the dialing API because the channel is not local.
  2029. */
  2030. props->dial = ast_dial_create();
  2031. if (!props->dial) {
  2032. return -1;
  2033. }
  2034. if (ast_dial_append(props->dial, props->transferer_type, props->transferer_addr, NULL)) {
  2035. return -1;
  2036. }
  2037. if (ast_dial_prerun(props->dial, NULL, cap)) {
  2038. return -1;
  2039. }
  2040. ast_dial_set_state_callback(props->dial, &recall_callback);
  2041. ao2_ref(props, +1);
  2042. ast_dial_set_user_data(props->dial, props);
  2043. if (ast_dial_run(props->dial, NULL, 1) == AST_DIAL_RESULT_FAILED) {
  2044. ao2_ref(props, -1);
  2045. return -1;
  2046. }
  2047. bridge_ringing(props->transferee_bridge);
  2048. return 0;
  2049. }
  2050. static enum attended_transfer_state recalling_exit(struct attended_transfer_properties *props,
  2051. enum attended_transfer_stimulus stimulus)
  2052. {
  2053. /* No matter what the outcome was, we need to kill off the dial */
  2054. ast_dial_join(props->dial);
  2055. ast_dial_destroy(props->dial);
  2056. props->dial = NULL;
  2057. /* This reference is the one we incremented for the dial state callback (recall_callback) to use */
  2058. ao2_ref(props, -1);
  2059. switch (stimulus) {
  2060. case STIMULUS_TRANSFEREE_HANGUP:
  2061. return TRANSFER_FAIL;
  2062. case STIMULUS_TIMEOUT:
  2063. case STIMULUS_RECALL_TARGET_HANGUP:
  2064. ++props->retry_attempts;
  2065. if (props->retry_attempts >= props->atxfercallbackretries) {
  2066. return TRANSFER_FAIL;
  2067. }
  2068. if (props->atxferloopdelay) {
  2069. return TRANSFER_WAIT_TO_RETRANSFER;
  2070. }
  2071. return TRANSFER_RETRANSFER;
  2072. case STIMULUS_RECALL_TARGET_ANSWER:
  2073. /* Setting this datastore up will allow the transferer to have all of his
  2074. * call features set up automatically when the bridge changes back to a
  2075. * normal personality
  2076. */
  2077. ast_bridge_features_ds_set(props->recall_target, &props->transferer_features);
  2078. ast_channel_ref(props->recall_target);
  2079. if (ast_bridge_impart(props->transferee_bridge, props->recall_target, NULL, NULL,
  2080. AST_BRIDGE_IMPART_CHAN_INDEPENDENT)) {
  2081. ast_hangup(props->recall_target);
  2082. return TRANSFER_FAIL;
  2083. }
  2084. return TRANSFER_RESUME;
  2085. case STIMULUS_NONE:
  2086. case STIMULUS_DTMF_ATXFER_ABORT:
  2087. case STIMULUS_DTMF_ATXFER_COMPLETE:
  2088. case STIMULUS_DTMF_ATXFER_THREEWAY:
  2089. case STIMULUS_DTMF_ATXFER_SWAP:
  2090. case STIMULUS_TRANSFER_TARGET_HANGUP:
  2091. case STIMULUS_TRANSFER_TARGET_ANSWER:
  2092. case STIMULUS_TRANSFERER_HANGUP:
  2093. default:
  2094. ast_log(LOG_WARNING, "Unexpected stimulus '%s' received in attended transfer state '%s'\n",
  2095. stimulus_strs[stimulus], state_properties[props->state].state_name);
  2096. return props->state;
  2097. }
  2098. }
  2099. static int wait_to_retransfer_enter(struct attended_transfer_properties *props)
  2100. {
  2101. bridge_hold(props->transferee_bridge);
  2102. return 0;
  2103. }
  2104. static enum attended_transfer_state wait_to_retransfer_exit(struct attended_transfer_properties *props,
  2105. enum attended_transfer_stimulus stimulus)
  2106. {
  2107. bridge_unhold(props->transferee_bridge);
  2108. switch (stimulus) {
  2109. case STIMULUS_TRANSFEREE_HANGUP:
  2110. return TRANSFER_FAIL;
  2111. case STIMULUS_TIMEOUT:
  2112. return TRANSFER_RETRANSFER;
  2113. case STIMULUS_NONE:
  2114. case STIMULUS_DTMF_ATXFER_ABORT:
  2115. case STIMULUS_DTMF_ATXFER_COMPLETE:
  2116. case STIMULUS_DTMF_ATXFER_THREEWAY:
  2117. case STIMULUS_DTMF_ATXFER_SWAP:
  2118. case STIMULUS_TRANSFER_TARGET_HANGUP:
  2119. case STIMULUS_TRANSFER_TARGET_ANSWER:
  2120. case STIMULUS_TRANSFERER_HANGUP:
  2121. case STIMULUS_RECALL_TARGET_HANGUP:
  2122. case STIMULUS_RECALL_TARGET_ANSWER:
  2123. default:
  2124. ast_log(LOG_WARNING, "Unexpected stimulus '%s' received in attended transfer state '%s'\n",
  2125. stimulus_strs[stimulus], state_properties[props->state].state_name);
  2126. return props->state;
  2127. }
  2128. }
  2129. static int attach_framehook(struct attended_transfer_properties *props, struct ast_channel *channel);
  2130. static int retransfer_enter(struct attended_transfer_properties *props)
  2131. {
  2132. RAII_VAR(struct ast_format_cap *, cap, ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_NOLOCK), ast_format_cap_destroy);
  2133. struct ast_format fmt;
  2134. char destination[AST_MAX_EXTENSION + AST_MAX_CONTEXT + 2];
  2135. int cause;
  2136. if (!cap) {
  2137. return -1;
  2138. }
  2139. snprintf(destination, sizeof(destination), "%s@%s", props->exten, props->context);
  2140. ast_format_cap_add(cap, ast_format_set(&fmt, AST_FORMAT_SLINEAR, 0));
  2141. /* Get a channel that is the destination we wish to call */
  2142. props->recall_target = ast_request("Local", cap, NULL, NULL, destination, &cause);
  2143. if (!props->recall_target) {
  2144. ast_log(LOG_ERROR, "Unable to request outbound channel for recall target\n");
  2145. return -1;
  2146. }
  2147. if (attach_framehook(props, props->recall_target)) {
  2148. ast_log(LOG_ERROR, "Unable to attach framehook to recall target\n");
  2149. ast_hangup(props->recall_target);
  2150. props->recall_target = NULL;
  2151. return -1;
  2152. }
  2153. if (ast_call(props->recall_target, destination, 0)) {
  2154. ast_log(LOG_ERROR, "Unable to place outbound call to recall target\n");
  2155. ast_hangup(props->recall_target);
  2156. props->recall_target = NULL;
  2157. return -1;
  2158. }
  2159. ast_channel_ref(props->recall_target);
  2160. if (ast_bridge_impart(props->transferee_bridge, props->recall_target, NULL, NULL,
  2161. AST_BRIDGE_IMPART_CHAN_INDEPENDENT)) {
  2162. ast_log(LOG_ERROR, "Unable to place recall target into bridge\n");
  2163. ast_hangup(props->recall_target);
  2164. return -1;
  2165. }
  2166. return 0;
  2167. }
  2168. static enum attended_transfer_state retransfer_exit(struct attended_transfer_properties *props,
  2169. enum attended_transfer_stimulus stimulus)
  2170. {
  2171. switch (stimulus) {
  2172. case STIMULUS_TRANSFEREE_HANGUP:
  2173. return TRANSFER_FAIL;
  2174. case STIMULUS_TIMEOUT:
  2175. ast_softhangup(props->recall_target, AST_SOFTHANGUP_EXPLICIT);
  2176. case STIMULUS_RECALL_TARGET_HANGUP:
  2177. props->recall_target = ast_channel_unref(props->recall_target);
  2178. if (props->atxferloopdelay) {
  2179. return TRANSFER_WAIT_TO_RECALL;
  2180. }
  2181. return TRANSFER_RECALLING;
  2182. case STIMULUS_RECALL_TARGET_ANSWER:
  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_recall_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_recall_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_RECALLING;
  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 fail_enter(struct attended_transfer_properties *props)
  2229. {
  2230. if (props->transferee_bridge) {
  2231. ast_bridge_destroy(props->transferee_bridge, 0);
  2232. props->transferee_bridge = NULL;
  2233. }
  2234. return 0;
  2235. }
  2236. /*!
  2237. * \brief DTMF hook when transferer presses abort sequence.
  2238. *
  2239. * Sends a stimulus to the attended transfer monitor thread that the abort sequence has been pressed
  2240. */
  2241. static int atxfer_abort(struct ast_bridge_channel *bridge_channel, void *hook_pvt)
  2242. {
  2243. struct attended_transfer_properties *props = hook_pvt;
  2244. ast_debug(1, "Transferer on attended transfer %p pressed abort sequence\n", props);
  2245. stimulate_attended_transfer(props, STIMULUS_DTMF_ATXFER_ABORT);
  2246. return 0;
  2247. }
  2248. /*!
  2249. * \brief DTMF hook when transferer presses complete sequence.
  2250. *
  2251. * Sends a stimulus to the attended transfer monitor thread that the complete sequence has been pressed
  2252. */
  2253. static int atxfer_complete(struct ast_bridge_channel *bridge_channel, void *hook_pvt)
  2254. {
  2255. struct attended_transfer_properties *props = hook_pvt;
  2256. ast_debug(1, "Transferer on attended transfer %p pressed complete sequence\n", props);
  2257. stimulate_attended_transfer(props, STIMULUS_DTMF_ATXFER_COMPLETE);
  2258. return 0;
  2259. }
  2260. /*!
  2261. * \brief DTMF hook when transferer presses threeway sequence.
  2262. *
  2263. * Sends a stimulus to the attended transfer monitor thread that the threeway sequence has been pressed
  2264. */
  2265. static int atxfer_threeway(struct ast_bridge_channel *bridge_channel, void *hook_pvt)
  2266. {
  2267. struct attended_transfer_properties *props = hook_pvt;
  2268. ast_debug(1, "Transferer on attended transfer %p pressed threeway sequence\n", props);
  2269. stimulate_attended_transfer(props, STIMULUS_DTMF_ATXFER_THREEWAY);
  2270. return 0;
  2271. }
  2272. /*!
  2273. * \brief DTMF hook when transferer presses swap sequence.
  2274. *
  2275. * Sends a stimulus to the attended transfer monitor thread that the swap sequence has been pressed
  2276. */
  2277. static int atxfer_swap(struct ast_bridge_channel *bridge_channel, void *hook_pvt)
  2278. {
  2279. struct attended_transfer_properties *props = hook_pvt;
  2280. ast_debug(1, "Transferer on attended transfer %p pressed swap sequence\n", props);
  2281. stimulate_attended_transfer(props, STIMULUS_DTMF_ATXFER_SWAP);
  2282. return 0;
  2283. }
  2284. /*!
  2285. * \brief Hangup hook for transferer channel.
  2286. *
  2287. * Sends a stimulus to the attended transfer monitor thread that the transferer has hung up.
  2288. */
  2289. static int atxfer_transferer_hangup(struct ast_bridge_channel *bridge_channel, void *hook_pvt)
  2290. {
  2291. struct attended_transfer_properties *props = hook_pvt;
  2292. ast_debug(1, "Transferer on attended transfer %p hung up\n", props);
  2293. stimulate_attended_transfer(props, STIMULUS_TRANSFERER_HANGUP);
  2294. return 0;
  2295. }
  2296. /*!
  2297. * \brief Frame hook for transfer target channel
  2298. *
  2299. * This is used to determine if the transfer target or recall target has answered
  2300. * the outgoing call.
  2301. *
  2302. * When an answer is detected, a stimulus is sent to the attended transfer monitor
  2303. * thread to indicate that the transfer target or recall target has answered.
  2304. *
  2305. * \param chan The channel the framehook is attached to.
  2306. * \param frame The frame being read or written.
  2307. * \param event What is being done with the frame.
  2308. * \param data The attended transfer properties.
  2309. */
  2310. static struct ast_frame *transfer_target_framehook_cb(struct ast_channel *chan,
  2311. struct ast_frame *frame, enum ast_framehook_event event, void *data)
  2312. {
  2313. struct attended_transfer_properties *props = data;
  2314. if (event == AST_FRAMEHOOK_EVENT_READ &&
  2315. frame && frame->frametype == AST_FRAME_CONTROL &&
  2316. frame->subclass.integer == AST_CONTROL_ANSWER) {
  2317. ast_debug(1, "Detected an answer for recall attempt on attended transfer %p\n", props);
  2318. if (props->superstate == SUPERSTATE_TRANSFER) {
  2319. stimulate_attended_transfer(props, STIMULUS_TRANSFER_TARGET_ANSWER);
  2320. } else {
  2321. stimulate_attended_transfer(props, STIMULUS_RECALL_TARGET_ANSWER);
  2322. }
  2323. ast_framehook_detach(chan, props->target_framehook_id);
  2324. props->target_framehook_id = -1;
  2325. }
  2326. return frame;
  2327. }
  2328. static void transfer_target_framehook_destroy_cb(void *data)
  2329. {
  2330. struct attended_transfer_properties *props = data;
  2331. ao2_cleanup(props);
  2332. }
  2333. static int bridge_personality_atxfer_push(struct ast_bridge *self, struct ast_bridge_channel *bridge_channel, struct ast_bridge_channel *swap)
  2334. {
  2335. const char *abort_dtmf;
  2336. const char *complete_dtmf;
  2337. const char *threeway_dtmf;
  2338. const char *swap_dtmf;
  2339. struct bridge_basic_personality *personality = self->personality;
  2340. if (!ast_channel_has_role(bridge_channel->chan, AST_TRANSFERER_ROLE_NAME)) {
  2341. return 0;
  2342. }
  2343. abort_dtmf = ast_channel_get_role_option(bridge_channel->chan, AST_TRANSFERER_ROLE_NAME, "abort");
  2344. complete_dtmf = ast_channel_get_role_option(bridge_channel->chan, AST_TRANSFERER_ROLE_NAME, "complete");
  2345. threeway_dtmf = ast_channel_get_role_option(bridge_channel->chan, AST_TRANSFERER_ROLE_NAME, "threeway");
  2346. swap_dtmf = ast_channel_get_role_option(bridge_channel->chan, AST_TRANSFERER_ROLE_NAME, "swap");
  2347. if (!ast_strlen_zero(abort_dtmf) && ast_bridge_dtmf_hook(bridge_channel->features,
  2348. abort_dtmf, atxfer_abort, personality->details[personality->current].pvt, NULL,
  2349. AST_BRIDGE_HOOK_REMOVE_ON_PERSONALITY_CHANGE | AST_BRIDGE_HOOK_REMOVE_ON_PULL)) {
  2350. return -1;
  2351. }
  2352. if (!ast_strlen_zero(complete_dtmf) && ast_bridge_dtmf_hook(bridge_channel->features,
  2353. complete_dtmf, atxfer_complete, personality->details[personality->current].pvt, NULL,
  2354. AST_BRIDGE_HOOK_REMOVE_ON_PERSONALITY_CHANGE | AST_BRIDGE_HOOK_REMOVE_ON_PULL)) {
  2355. return -1;
  2356. }
  2357. if (!ast_strlen_zero(threeway_dtmf) && ast_bridge_dtmf_hook(bridge_channel->features,
  2358. threeway_dtmf, atxfer_threeway, personality->details[personality->current].pvt, NULL,
  2359. AST_BRIDGE_HOOK_REMOVE_ON_PERSONALITY_CHANGE | AST_BRIDGE_HOOK_REMOVE_ON_PULL)) {
  2360. return -1;
  2361. }
  2362. if (!ast_strlen_zero(swap_dtmf) && ast_bridge_dtmf_hook(bridge_channel->features,
  2363. swap_dtmf, atxfer_swap, personality->details[personality->current].pvt, NULL,
  2364. AST_BRIDGE_HOOK_REMOVE_ON_PERSONALITY_CHANGE | AST_BRIDGE_HOOK_REMOVE_ON_PULL)) {
  2365. return -1;
  2366. }
  2367. if (ast_bridge_hangup_hook(bridge_channel->features, atxfer_transferer_hangup,
  2368. personality->details[personality->current].pvt, NULL,
  2369. AST_BRIDGE_HOOK_REMOVE_ON_PERSONALITY_CHANGE | AST_BRIDGE_HOOK_REMOVE_ON_PULL)) {
  2370. return -1;
  2371. }
  2372. return 0;
  2373. }
  2374. static void transfer_pull(struct ast_bridge *self, struct ast_bridge_channel *bridge_channel, struct attended_transfer_properties *props)
  2375. {
  2376. if (self->num_channels > 1 || bridge_channel->state == BRIDGE_CHANNEL_STATE_WAIT) {
  2377. return;
  2378. }
  2379. if (self->num_channels == 1) {
  2380. RAII_VAR(struct ast_bridge_channel *, transferer_bridge_channel, NULL, ao2_cleanup);
  2381. ast_channel_lock(props->transferer);
  2382. transferer_bridge_channel = ast_channel_get_bridge_channel(props->transferer);
  2383. ast_channel_unlock(props->transferer);
  2384. if (!transferer_bridge_channel) {
  2385. return;
  2386. }
  2387. if (AST_LIST_FIRST(&self->channels) != transferer_bridge_channel) {
  2388. return;
  2389. }
  2390. }
  2391. /* Reaching this point means that either
  2392. * 1) The bridge has no channels in it
  2393. * 2) The bridge has one channel, and it's the transferer
  2394. * In either case, it indicates that the non-transferer parties
  2395. * are no longer in the bridge.
  2396. */
  2397. if (self == props->transferee_bridge) {
  2398. stimulate_attended_transfer(props, STIMULUS_TRANSFEREE_HANGUP);
  2399. } else {
  2400. stimulate_attended_transfer(props, STIMULUS_TRANSFER_TARGET_HANGUP);
  2401. }
  2402. }
  2403. static void recall_pull(struct ast_bridge *self, struct ast_bridge_channel *bridge_channel, struct attended_transfer_properties *props)
  2404. {
  2405. if (self == props->target_bridge) {
  2406. /* Once we're in the recall superstate, we no longer care about this bridge */
  2407. return;
  2408. }
  2409. if (bridge_channel->chan == props->recall_target) {
  2410. stimulate_attended_transfer(props, STIMULUS_RECALL_TARGET_HANGUP);
  2411. return;
  2412. }
  2413. if (self->num_channels == 0) {
  2414. /* Empty bridge means all transferees are gone for sure */
  2415. stimulate_attended_transfer(props, STIMULUS_TRANSFEREE_HANGUP);
  2416. return;
  2417. }
  2418. if (self->num_channels == 1) {
  2419. RAII_VAR(struct ast_bridge_channel *, target_bridge_channel, NULL, ao2_cleanup);
  2420. if (!props->recall_target) {
  2421. /* No recall target means that the pull happened on a transferee. If there's still
  2422. * a channel left in the bridge, we don't need to send a stimulus
  2423. */
  2424. return;
  2425. }
  2426. ast_channel_lock(props->recall_target);
  2427. target_bridge_channel = ast_channel_get_bridge_channel(props->recall_target);
  2428. ast_channel_unlock(props->recall_target);
  2429. if (!target_bridge_channel) {
  2430. return;
  2431. }
  2432. if (AST_LIST_FIRST(&self->channels) == target_bridge_channel) {
  2433. stimulate_attended_transfer(props, STIMULUS_TRANSFEREE_HANGUP);
  2434. }
  2435. }
  2436. }
  2437. static void bridge_personality_atxfer_pull(struct ast_bridge *self, struct ast_bridge_channel *bridge_channel)
  2438. {
  2439. struct bridge_basic_personality *personality = self->personality;
  2440. struct attended_transfer_properties *props = personality->details[personality->current].pvt;
  2441. switch (props->superstate) {
  2442. case SUPERSTATE_TRANSFER:
  2443. transfer_pull(self, bridge_channel, props);
  2444. break;
  2445. case SUPERSTATE_RECALL:
  2446. recall_pull(self, bridge_channel, props);
  2447. break;
  2448. }
  2449. }
  2450. static enum attended_transfer_stimulus wait_for_stimulus(struct attended_transfer_properties *props)
  2451. {
  2452. RAII_VAR(struct stimulus_list *, list, NULL, ast_free_ptr);
  2453. SCOPED_MUTEX(lock, ao2_object_get_lockaddr(props));
  2454. while (!(list = AST_LIST_REMOVE_HEAD(&props->stimulus_queue, next))) {
  2455. if (!(state_properties[props->state].flags & TRANSFER_STATE_FLAG_TIMED)) {
  2456. ast_cond_wait(&props->cond, lock);
  2457. } else {
  2458. struct timeval relative_timeout = { 0, };
  2459. struct timeval absolute_timeout;
  2460. struct timespec timeout_arg;
  2461. if (state_properties[props->state].flags & TRANSFER_STATE_FLAG_TIMER_RESET) {
  2462. props->start = ast_tvnow();
  2463. }
  2464. if (state_properties[props->state].flags & TRANSFER_STATE_FLAG_TIMER_LOOP_DELAY) {
  2465. relative_timeout.tv_sec = props->atxferloopdelay;
  2466. } else {
  2467. /* Implied TRANSFER_STATE_FLAG_TIMER_ATXFER_NO_ANSWER */
  2468. relative_timeout.tv_sec = props->atxfernoanswertimeout;
  2469. }
  2470. absolute_timeout = ast_tvadd(props->start, relative_timeout);
  2471. timeout_arg.tv_sec = absolute_timeout.tv_sec;
  2472. timeout_arg.tv_nsec = absolute_timeout.tv_usec * 1000;
  2473. if (ast_cond_timedwait(&props->cond, lock, &timeout_arg) == ETIMEDOUT) {
  2474. return STIMULUS_TIMEOUT;
  2475. }
  2476. }
  2477. }
  2478. return list->stimulus;
  2479. }
  2480. /*!
  2481. * \brief The main loop for the attended transfer monitor thread.
  2482. *
  2483. * This loop runs continuously until the attended transfer reaches
  2484. * a terminal state. Stimuli for changes in the attended transfer
  2485. * state are handled in this thread so that all factors in an
  2486. * attended transfer can be handled in an orderly fashion.
  2487. *
  2488. * \param data The attended transfer properties
  2489. */
  2490. static void *attended_transfer_monitor_thread(void *data)
  2491. {
  2492. struct attended_transfer_properties *props = data;
  2493. for (;;) {
  2494. enum attended_transfer_stimulus stimulus;
  2495. ast_debug(1, "About to enter state %s for attended transfer %p\n", state_properties[props->state].state_name, props);
  2496. if (state_properties[props->state].enter &&
  2497. state_properties[props->state].enter(props)) {
  2498. ast_log(LOG_ERROR, "State %s enter function returned an error for attended transfer %p\n",
  2499. state_properties[props->state].state_name, props);
  2500. break;
  2501. }
  2502. if (state_properties[props->state].flags & TRANSFER_STATE_FLAG_TERMINAL) {
  2503. ast_debug(1, "State %s is a terminal state. Ending attended transfer %p\n",
  2504. state_properties[props->state].state_name, props);
  2505. break;
  2506. }
  2507. stimulus = wait_for_stimulus(props);
  2508. ast_debug(1, "Received stimulus %s on attended transfer %p\n", stimulus_strs[stimulus], props);
  2509. ast_assert(state_properties[props->state].exit != NULL);
  2510. props->state = state_properties[props->state].exit(props, stimulus);
  2511. ast_debug(1, "Told to enter state %s exit on attended transfer %p\n", state_properties[props->state].state_name, props);
  2512. }
  2513. attended_transfer_properties_shutdown(props);
  2514. return NULL;
  2515. }
  2516. static int attach_framehook(struct attended_transfer_properties *props, struct ast_channel *channel)
  2517. {
  2518. struct ast_framehook_interface target_interface = {
  2519. .version = AST_FRAMEHOOK_INTERFACE_VERSION,
  2520. .event_cb = transfer_target_framehook_cb,
  2521. .destroy_cb = transfer_target_framehook_destroy_cb,
  2522. };
  2523. ao2_ref(props, +1);
  2524. target_interface.data = props;
  2525. props->target_framehook_id = ast_framehook_attach(channel, &target_interface);
  2526. if (props->target_framehook_id == -1) {
  2527. ao2_ref(props, -1);
  2528. return -1;
  2529. }
  2530. return 0;
  2531. }
  2532. static int add_transferer_role(struct ast_channel *chan, struct ast_bridge_features_attended_transfer *attended_transfer)
  2533. {
  2534. const char *atxfer_abort;
  2535. const char *atxfer_threeway;
  2536. const char *atxfer_complete;
  2537. const char *atxfer_swap;
  2538. RAII_VAR(struct ast_features_xfer_config *, xfer_cfg, NULL, ao2_cleanup);
  2539. SCOPED_CHANNELLOCK(lock, chan);
  2540. xfer_cfg = ast_get_chan_features_xfer_config(chan);
  2541. if (!xfer_cfg) {
  2542. return -1;
  2543. }
  2544. if (attended_transfer) {
  2545. atxfer_abort = ast_strdupa(S_OR(attended_transfer->abort, xfer_cfg->atxferabort));
  2546. atxfer_threeway = ast_strdupa(S_OR(attended_transfer->threeway, xfer_cfg->atxferthreeway));
  2547. atxfer_complete = ast_strdupa(S_OR(attended_transfer->complete, xfer_cfg->atxfercomplete));
  2548. atxfer_swap = ast_strdupa(S_OR(attended_transfer->swap, xfer_cfg->atxferswap));
  2549. } else {
  2550. atxfer_abort = ast_strdupa(xfer_cfg->atxferabort);
  2551. atxfer_threeway = ast_strdupa(xfer_cfg->atxferthreeway);
  2552. atxfer_complete = ast_strdupa(xfer_cfg->atxfercomplete);
  2553. atxfer_swap = ast_strdupa(xfer_cfg->atxferswap);
  2554. }
  2555. return ast_channel_add_bridge_role(chan, AST_TRANSFERER_ROLE_NAME) ||
  2556. ast_channel_set_bridge_role_option(chan, AST_TRANSFERER_ROLE_NAME, "abort", atxfer_abort) ||
  2557. ast_channel_set_bridge_role_option(chan, AST_TRANSFERER_ROLE_NAME, "complete", atxfer_complete) ||
  2558. ast_channel_set_bridge_role_option(chan, AST_TRANSFERER_ROLE_NAME, "threeway", atxfer_threeway) ||
  2559. ast_channel_set_bridge_role_option(chan, AST_TRANSFERER_ROLE_NAME, "swap", atxfer_swap);
  2560. }
  2561. /*!
  2562. * \brief Helper function that presents dialtone and grabs extension
  2563. *
  2564. * \retval 0 on success
  2565. * \retval -1 on failure
  2566. */
  2567. static int grab_transfer(struct ast_channel *chan, char *exten, size_t exten_len, const char *context)
  2568. {
  2569. int res;
  2570. int digit_timeout;
  2571. RAII_VAR(struct ast_features_xfer_config *, xfer_cfg, NULL, ao2_cleanup);
  2572. ast_channel_lock(chan);
  2573. xfer_cfg = ast_get_chan_features_xfer_config(chan);
  2574. if (!xfer_cfg) {
  2575. ast_log(LOG_ERROR, "Unable to get transfer configuration\n");
  2576. ast_channel_unlock(chan);
  2577. return -1;
  2578. }
  2579. digit_timeout = xfer_cfg->transferdigittimeout * 1000;
  2580. ast_channel_unlock(chan);
  2581. /* Play the simple "transfer" prompt out and wait */
  2582. res = ast_stream_and_wait(chan, "pbx-transfer", AST_DIGIT_ANY);
  2583. ast_stopstream(chan);
  2584. if (res < 0) {
  2585. /* Hangup or error */
  2586. return -1;
  2587. }
  2588. if (res) {
  2589. /* Store the DTMF digit that interrupted playback of the file. */
  2590. exten[0] = res;
  2591. }
  2592. /* Drop to dialtone so they can enter the extension they want to transfer to */
  2593. res = ast_app_dtget(chan, context, exten, exten_len, exten_len - 1, digit_timeout);
  2594. if (res < 0) {
  2595. /* Hangup or error */
  2596. res = -1;
  2597. } else if (!res) {
  2598. /* 0 for invalid extension dialed. */
  2599. if (ast_strlen_zero(exten)) {
  2600. ast_debug(1, "%s dialed no digits.\n", ast_channel_name(chan));
  2601. } else {
  2602. ast_debug(1, "%s dialed '%s@%s' does not exist.\n",
  2603. ast_channel_name(chan), exten, context);
  2604. }
  2605. ast_stream_and_wait(chan, "pbx-invalid", AST_DIGIT_NONE);
  2606. res = -1;
  2607. } else {
  2608. /* Dialed extension is valid. */
  2609. res = 0;
  2610. }
  2611. return res;
  2612. }
  2613. static void copy_caller_data(struct ast_channel *dest, struct ast_channel *caller)
  2614. {
  2615. ast_channel_lock_both(caller, dest);
  2616. ast_connected_line_copy_from_caller(ast_channel_connected(dest), ast_channel_caller(caller));
  2617. ast_channel_inherit_variables(caller, dest);
  2618. ast_channel_datastore_inherit(caller, dest);
  2619. ast_channel_unlock(dest);
  2620. ast_channel_unlock(caller);
  2621. }
  2622. /*! \brief Helper function that creates an outgoing channel and returns it immediately */
  2623. static struct ast_channel *dial_transfer(struct ast_channel *caller, const char *destination)
  2624. {
  2625. struct ast_channel *chan;
  2626. int cause;
  2627. /* Now we request a local channel to prepare to call the destination */
  2628. chan = ast_request("Local", ast_channel_nativeformats(caller), NULL, caller, destination,
  2629. &cause);
  2630. if (!chan) {
  2631. return NULL;
  2632. }
  2633. /* Who is transferring the call. */
  2634. pbx_builtin_setvar_helper(chan, "TRANSFERERNAME", ast_channel_name(caller));
  2635. ast_bridge_set_transfer_variables(chan, ast_channel_name(caller), 1);
  2636. /* Before we actually dial out let's inherit appropriate information. */
  2637. copy_caller_data(chan, caller);
  2638. return chan;
  2639. }
  2640. /*!
  2641. * \brief Internal built in feature for attended transfers
  2642. *
  2643. * This hook will set up a thread for monitoring the progress of
  2644. * an attended transfer. For more information about attended transfer
  2645. * progress, see documentation on the transfer state machine.
  2646. *
  2647. * \param bridge_channel The channel that pressed the attended transfer DTMF sequence
  2648. * \param hook_pvt Structure with further information about the attended transfer
  2649. */
  2650. static int feature_attended_transfer(struct ast_bridge_channel *bridge_channel, void *hook_pvt)
  2651. {
  2652. struct ast_bridge_features_attended_transfer *attended_transfer = hook_pvt;
  2653. struct attended_transfer_properties *props;
  2654. struct ast_bridge *bridge;
  2655. char destination[AST_MAX_EXTENSION + AST_MAX_CONTEXT + 1];
  2656. char exten[AST_MAX_EXTENSION] = "";
  2657. pthread_t thread;
  2658. /* Inhibit the bridge before we do anything else. */
  2659. bridge = ast_bridge_channel_merge_inhibit(bridge_channel, +1);
  2660. if (strcmp(bridge->v_table->name, "basic")) {
  2661. ast_log(LOG_ERROR, "Attended transfer attempted on unsupported bridge type '%s'.\n",
  2662. bridge->v_table->name);
  2663. ast_bridge_merge_inhibit(bridge, -1);
  2664. ao2_ref(bridge, -1);
  2665. return 0;
  2666. }
  2667. /* Was the bridge inhibited before we inhibited it? */
  2668. if (1 < bridge->inhibit_merge) {
  2669. /*
  2670. * The peer likely initiated attended transfer at the same time
  2671. * and we lost the race.
  2672. */
  2673. ast_verb(3, "Channel %s: Bridge '%s' does not permit merging at this time.\n",
  2674. ast_channel_name(bridge_channel->chan), bridge->uniqueid);
  2675. ast_bridge_merge_inhibit(bridge, -1);
  2676. ao2_ref(bridge, -1);
  2677. return 0;
  2678. }
  2679. props = attended_transfer_properties_alloc(bridge_channel->chan,
  2680. attended_transfer ? attended_transfer->context : NULL);
  2681. if (!props) {
  2682. ast_log(LOG_ERROR, "Unable to allocate control structure for performing attended transfer.\n");
  2683. ast_bridge_merge_inhibit(bridge, -1);
  2684. ao2_ref(bridge, -1);
  2685. return 0;
  2686. }
  2687. props->transferee_bridge = bridge;
  2688. if (add_transferer_role(props->transferer, attended_transfer)) {
  2689. ast_log(LOG_ERROR, "Unable to set transferrer bridge role.\n");
  2690. attended_transfer_properties_shutdown(props);
  2691. return 0;
  2692. }
  2693. ast_bridge_channel_write_hold(bridge_channel, NULL);
  2694. /* Grab the extension to transfer to */
  2695. if (grab_transfer(bridge_channel->chan, exten, sizeof(exten), props->context)) {
  2696. ast_log(LOG_WARNING, "Unable to acquire target extension for attended transfer.\n");
  2697. ast_bridge_channel_write_unhold(bridge_channel);
  2698. attended_transfer_properties_shutdown(props);
  2699. return 0;
  2700. }
  2701. ast_string_field_set(props, exten, exten);
  2702. /* Fill the variable with the extension and context we want to call */
  2703. snprintf(destination, sizeof(destination), "%s@%s", props->exten, props->context);
  2704. ast_debug(1, "Attended transfer to '%s'\n", destination);
  2705. /* Get a channel that is the destination we wish to call */
  2706. props->transfer_target = dial_transfer(bridge_channel->chan, destination);
  2707. if (!props->transfer_target) {
  2708. ast_log(LOG_ERROR, "Unable to request outbound channel for attended transfer target.\n");
  2709. ast_stream_and_wait(props->transferer, props->failsound, AST_DIGIT_NONE);
  2710. ast_bridge_channel_write_unhold(bridge_channel);
  2711. attended_transfer_properties_shutdown(props);
  2712. return 0;
  2713. }
  2714. /* Create a bridge to use to talk to the person we are calling */
  2715. props->target_bridge = ast_bridge_basic_new();
  2716. if (!props->target_bridge) {
  2717. ast_log(LOG_ERROR, "Unable to create bridge for attended transfer target.\n");
  2718. ast_stream_and_wait(props->transferer, props->failsound, AST_DIGIT_NONE);
  2719. ast_bridge_channel_write_unhold(bridge_channel);
  2720. ast_hangup(props->transfer_target);
  2721. props->transfer_target = NULL;
  2722. attended_transfer_properties_shutdown(props);
  2723. return 0;
  2724. }
  2725. ast_bridge_merge_inhibit(props->target_bridge, +1);
  2726. if (attach_framehook(props, props->transfer_target)) {
  2727. ast_log(LOG_ERROR, "Unable to attach framehook to transfer target.\n");
  2728. ast_stream_and_wait(props->transferer, props->failsound, AST_DIGIT_NONE);
  2729. ast_bridge_channel_write_unhold(bridge_channel);
  2730. ast_hangup(props->transfer_target);
  2731. props->transfer_target = NULL;
  2732. attended_transfer_properties_shutdown(props);
  2733. return 0;
  2734. }
  2735. bridge_basic_change_personality(props->target_bridge,
  2736. BRIDGE_BASIC_PERSONALITY_ATXFER, props);
  2737. bridge_basic_change_personality(bridge,
  2738. BRIDGE_BASIC_PERSONALITY_ATXFER, props);
  2739. if (ast_call(props->transfer_target, destination, 0)) {
  2740. ast_log(LOG_ERROR, "Unable to place outbound call to transfer target.\n");
  2741. ast_stream_and_wait(bridge_channel->chan, props->failsound, AST_DIGIT_NONE);
  2742. ast_bridge_channel_write_unhold(bridge_channel);
  2743. ast_hangup(props->transfer_target);
  2744. props->transfer_target = NULL;
  2745. attended_transfer_properties_shutdown(props);
  2746. return 0;
  2747. }
  2748. /* We increase the refcount of the transfer target because ast_bridge_impart() will
  2749. * steal the reference we already have. We need to keep a reference, so the only
  2750. * choice is to give it a bump
  2751. */
  2752. ast_channel_ref(props->transfer_target);
  2753. if (ast_bridge_impart(props->target_bridge, props->transfer_target, NULL, NULL,
  2754. AST_BRIDGE_IMPART_CHAN_INDEPENDENT)) {
  2755. ast_log(LOG_ERROR, "Unable to place transfer target into bridge.\n");
  2756. ast_stream_and_wait(bridge_channel->chan, props->failsound, AST_DIGIT_NONE);
  2757. ast_bridge_channel_write_unhold(bridge_channel);
  2758. ast_hangup(props->transfer_target);
  2759. props->transfer_target = NULL;
  2760. attended_transfer_properties_shutdown(props);
  2761. return 0;
  2762. }
  2763. if (ast_pthread_create_detached(&thread, NULL, attended_transfer_monitor_thread, props)) {
  2764. ast_log(LOG_ERROR, "Unable to create monitoring thread for attended transfer.\n");
  2765. ast_stream_and_wait(bridge_channel->chan, props->failsound, AST_DIGIT_NONE);
  2766. ast_bridge_channel_write_unhold(bridge_channel);
  2767. attended_transfer_properties_shutdown(props);
  2768. return 0;
  2769. }
  2770. /* Once the monitoring thread has been created, it is responsible for destroying all
  2771. * of the necessary components.
  2772. */
  2773. return 0;
  2774. }
  2775. static void blind_transfer_cb(struct ast_channel *new_channel, void *user_data,
  2776. enum ast_transfer_type transfer_type)
  2777. {
  2778. struct ast_channel *transferer_channel = user_data;
  2779. if (transfer_type == AST_BRIDGE_TRANSFER_MULTI_PARTY) {
  2780. copy_caller_data(new_channel, transferer_channel);
  2781. }
  2782. }
  2783. /*! \brief Internal built in feature for blind transfers */
  2784. static int feature_blind_transfer(struct ast_bridge_channel *bridge_channel, void *hook_pvt)
  2785. {
  2786. char exten[AST_MAX_EXTENSION] = "";
  2787. struct ast_bridge_features_blind_transfer *blind_transfer = hook_pvt;
  2788. const char *context;
  2789. char *goto_on_blindxfr;
  2790. ast_bridge_channel_write_hold(bridge_channel, NULL);
  2791. ast_channel_lock(bridge_channel->chan);
  2792. context = ast_strdupa(get_transfer_context(bridge_channel->chan,
  2793. blind_transfer ? blind_transfer->context : NULL));
  2794. goto_on_blindxfr = ast_strdupa(S_OR(pbx_builtin_getvar_helper(bridge_channel->chan,
  2795. "GOTO_ON_BLINDXFR"), ""));
  2796. ast_channel_unlock(bridge_channel->chan);
  2797. /* Grab the extension to transfer to */
  2798. if (grab_transfer(bridge_channel->chan, exten, sizeof(exten), context)) {
  2799. ast_bridge_channel_write_unhold(bridge_channel);
  2800. return 0;
  2801. }
  2802. if (!ast_strlen_zero(goto_on_blindxfr)) {
  2803. ast_debug(1, "After transfer, transferer %s goes to %s\n",
  2804. ast_channel_name(bridge_channel->chan), goto_on_blindxfr);
  2805. ast_bridge_set_after_go_on(bridge_channel->chan, NULL, NULL, 0, goto_on_blindxfr);
  2806. }
  2807. if (ast_bridge_transfer_blind(0, bridge_channel->chan, exten, context, blind_transfer_cb,
  2808. bridge_channel->chan) != AST_BRIDGE_TRANSFER_SUCCESS &&
  2809. !ast_strlen_zero(goto_on_blindxfr)) {
  2810. ast_bridge_discard_after_goto(bridge_channel->chan);
  2811. }
  2812. return 0;
  2813. }
  2814. struct ast_bridge_methods ast_bridge_basic_v_table;
  2815. struct ast_bridge_methods personality_normal_v_table;
  2816. struct ast_bridge_methods personality_atxfer_v_table;
  2817. static void bridge_basic_change_personality(struct ast_bridge *bridge,
  2818. enum bridge_basic_personality_type type, void *user_data)
  2819. {
  2820. struct bridge_basic_personality *personality = bridge->personality;
  2821. SCOPED_LOCK(lock, bridge, ast_bridge_lock, ast_bridge_unlock);
  2822. remove_hooks_on_personality_change(bridge);
  2823. ao2_cleanup(personality->details[personality->current].pvt);
  2824. personality->details[personality->current].pvt = NULL;
  2825. ast_clear_flag(&bridge->feature_flags, AST_FLAGS_ALL);
  2826. personality->current = type;
  2827. if (user_data) {
  2828. ao2_ref(user_data, +1);
  2829. }
  2830. personality->details[personality->current].pvt = user_data;
  2831. ast_set_flag(&bridge->feature_flags, personality->details[personality->current].bridge_flags);
  2832. if (personality->details[personality->current].on_personality_change) {
  2833. personality->details[personality->current].on_personality_change(bridge);
  2834. }
  2835. }
  2836. static void personality_destructor(void *obj)
  2837. {
  2838. struct bridge_basic_personality *personality = obj;
  2839. int i;
  2840. for (i = 0; i < BRIDGE_BASIC_PERSONALITY_END; ++i) {
  2841. ao2_cleanup(personality->details[i].pvt);
  2842. }
  2843. }
  2844. static void on_personality_change_normal(struct ast_bridge *bridge)
  2845. {
  2846. struct ast_bridge_channel *iter;
  2847. AST_LIST_TRAVERSE(&bridge->channels, iter, entry) {
  2848. if (add_normal_hooks(bridge, iter)) {
  2849. ast_log(LOG_WARNING, "Unable to set up bridge hooks for channel %s. Features may not work properly\n",
  2850. ast_channel_name(iter->chan));
  2851. }
  2852. }
  2853. }
  2854. static void init_details(struct personality_details *details,
  2855. enum bridge_basic_personality_type type)
  2856. {
  2857. switch (type) {
  2858. case BRIDGE_BASIC_PERSONALITY_NORMAL:
  2859. details->v_table = &personality_normal_v_table;
  2860. details->bridge_flags = NORMAL_FLAGS;
  2861. details->on_personality_change = on_personality_change_normal;
  2862. break;
  2863. case BRIDGE_BASIC_PERSONALITY_ATXFER:
  2864. details->v_table = &personality_atxfer_v_table;
  2865. details->bridge_flags = TRANSFER_FLAGS;
  2866. break;
  2867. default:
  2868. ast_log(LOG_WARNING, "Asked to initialize unexpected basic bridge personality type.\n");
  2869. break;
  2870. }
  2871. }
  2872. static struct ast_bridge *bridge_basic_personality_alloc(struct ast_bridge *bridge)
  2873. {
  2874. struct bridge_basic_personality *personality;
  2875. int i;
  2876. if (!bridge) {
  2877. return NULL;
  2878. }
  2879. personality = ao2_alloc(sizeof(*personality), personality_destructor);
  2880. if (!personality) {
  2881. ao2_ref(bridge, -1);
  2882. return NULL;
  2883. }
  2884. for (i = 0; i < BRIDGE_BASIC_PERSONALITY_END; ++i) {
  2885. init_details(&personality->details[i], i);
  2886. }
  2887. personality->current = BRIDGE_BASIC_PERSONALITY_NORMAL;
  2888. bridge->personality = personality;
  2889. return bridge;
  2890. }
  2891. struct ast_bridge *ast_bridge_basic_new(void)
  2892. {
  2893. struct ast_bridge *bridge;
  2894. bridge = bridge_alloc(sizeof(struct ast_bridge), &ast_bridge_basic_v_table);
  2895. bridge = bridge_base_init(bridge,
  2896. AST_BRIDGE_CAPABILITY_NATIVE | AST_BRIDGE_CAPABILITY_1TO1MIX
  2897. | AST_BRIDGE_CAPABILITY_MULTIMIX, NORMAL_FLAGS, NULL, NULL, NULL);
  2898. bridge = bridge_basic_personality_alloc(bridge);
  2899. bridge = bridge_register(bridge);
  2900. return bridge;
  2901. }
  2902. void ast_bridge_basic_set_flags(struct ast_bridge *bridge, unsigned int flags)
  2903. {
  2904. SCOPED_LOCK(lock, bridge, ast_bridge_lock, ast_bridge_unlock);
  2905. struct bridge_basic_personality *personality = bridge->personality;
  2906. personality->details[personality->current].bridge_flags |= flags;
  2907. ast_set_flag(&bridge->feature_flags, flags);
  2908. }
  2909. void ast_bridging_init_basic(void)
  2910. {
  2911. /* Setup bridge basic subclass v_table. */
  2912. ast_bridge_basic_v_table = ast_bridge_base_v_table;
  2913. ast_bridge_basic_v_table.name = "basic";
  2914. ast_bridge_basic_v_table.push = bridge_basic_push;
  2915. ast_bridge_basic_v_table.pull = bridge_basic_pull;
  2916. ast_bridge_basic_v_table.destroy = bridge_basic_destroy;
  2917. personality_normal_v_table = ast_bridge_base_v_table;
  2918. personality_normal_v_table.name = "normal";
  2919. personality_normal_v_table.push = bridge_personality_normal_push;
  2920. personality_atxfer_v_table = ast_bridge_base_v_table;
  2921. personality_atxfer_v_table.name = "attended transfer";
  2922. personality_atxfer_v_table.push = bridge_personality_atxfer_push;
  2923. personality_atxfer_v_table.pull = bridge_personality_atxfer_pull;
  2924. ast_bridge_features_register(AST_BRIDGE_BUILTIN_ATTENDEDTRANSFER, feature_attended_transfer, NULL);
  2925. ast_bridge_features_register(AST_BRIDGE_BUILTIN_BLINDTRANSFER, feature_blind_transfer, NULL);
  2926. }