bridge_basic.c 116 KB

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