bridge_basic.c 119 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668
  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. struct ao2_container *applicationmap;
  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. ao2_callback_data(applicationmap, 0, setup_dynamic_feature, features, &res);
  548. ao2_ref(applicationmap, -1);
  549. }
  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 when transfer completes */
  1203. AST_STRING_FIELD(xfersound);
  1204. /*! The channel technology of the transferer channel */
  1205. AST_STRING_FIELD(transferer_type);
  1206. /*! The transferer channel address */
  1207. AST_STRING_FIELD(transferer_addr);
  1208. );
  1209. /*! Condition used to synchronize when stimuli are reported to the monitor thread */
  1210. ast_cond_t cond;
  1211. /*! The bridge where the transferee resides. This bridge is also the bridge that
  1212. * survives a successful attended transfer.
  1213. */
  1214. struct ast_bridge *transferee_bridge;
  1215. /*! The bridge used to place an outbound call to the transfer target. This
  1216. * bridge is merged with the transferee_bridge on a successful transfer.
  1217. */
  1218. struct ast_bridge *target_bridge;
  1219. /*! The party that performs the attended transfer. */
  1220. struct ast_channel *transferer;
  1221. /*! The local channel dialed to reach the transfer target. */
  1222. struct ast_channel *transfer_target;
  1223. /*! The party that is currently being recalled. Depending on
  1224. * the current state, this may be either the party that originally
  1225. * was the transferer or the original transfer target. This is
  1226. * set with reference when entering the BLOND_NONFINAL, RECALLING,
  1227. * and RETRANSFER states, and the reference released on state exit
  1228. * if continuing with recall or retransfer to avoid leak.
  1229. */
  1230. struct ast_channel *recall_target;
  1231. /*! The absolute starting time for running timers */
  1232. struct timeval start;
  1233. AST_LIST_HEAD_NOLOCK(,stimulus_list) stimulus_queue;
  1234. /*! The current state of the attended transfer */
  1235. enum attended_transfer_state state;
  1236. /*! The current superstate of the attended transfer */
  1237. enum attended_transfer_superstate superstate;
  1238. /*! Configured atxferdropcall from features.conf */
  1239. int atxferdropcall;
  1240. /*! Configured atxfercallbackretries from features.conf */
  1241. int atxfercallbackretries;
  1242. /*! Configured atxferloopdelay from features.conf */
  1243. int atxferloopdelay;
  1244. /*! Configured atxfernoanswertimeout from features.conf */
  1245. int atxfernoanswertimeout;
  1246. /*! Count of the number of times that recalls have been attempted */
  1247. int retry_attempts;
  1248. /*! Framehook ID for outbounc call to transfer target or recall target */
  1249. int target_framehook_id;
  1250. /*! Dial structure used when recalling transferer channel */
  1251. struct ast_dial *dial;
  1252. /*! The bridging features the transferer has available */
  1253. struct ast_flags transferer_features;
  1254. /*! Saved transferer connected line data for recalling the transferer. */
  1255. struct ast_party_connected_line original_transferer_colp;
  1256. };
  1257. static void attended_transfer_properties_destructor(void *obj)
  1258. {
  1259. struct attended_transfer_properties *props = obj;
  1260. ast_debug(1, "Destroy attended transfer properties %p\n", props);
  1261. ao2_cleanup(props->target_bridge);
  1262. ao2_cleanup(props->transferee_bridge);
  1263. /* Use ast_channel_cleanup() instead of ast_channel_unref() for channels since they may be NULL */
  1264. ast_channel_cleanup(props->transferer);
  1265. ast_channel_cleanup(props->transfer_target);
  1266. ast_channel_cleanup(props->recall_target);
  1267. ast_party_connected_line_free(&props->original_transferer_colp);
  1268. ast_string_field_free_memory(props);
  1269. ast_cond_destroy(&props->cond);
  1270. }
  1271. /*!
  1272. * \internal
  1273. * \brief Determine the transfer context to use.
  1274. * \since 12.0.0
  1275. *
  1276. * \param transferer Channel initiating the transfer.
  1277. * \param context User supplied context if available. May be NULL.
  1278. *
  1279. * \return The context to use for the transfer.
  1280. */
  1281. static const char *get_transfer_context(struct ast_channel *transferer, const char *context)
  1282. {
  1283. if (!ast_strlen_zero(context)) {
  1284. return context;
  1285. }
  1286. context = pbx_builtin_getvar_helper(transferer, "TRANSFER_CONTEXT");
  1287. if (!ast_strlen_zero(context)) {
  1288. return context;
  1289. }
  1290. context = ast_channel_macrocontext(transferer);
  1291. if (!ast_strlen_zero(context)) {
  1292. return context;
  1293. }
  1294. context = ast_channel_context(transferer);
  1295. if (!ast_strlen_zero(context)) {
  1296. return context;
  1297. }
  1298. return "default";
  1299. }
  1300. /*!
  1301. * \brief Allocate and initialize attended transfer properties
  1302. *
  1303. * \param transferer The channel performing the attended transfer
  1304. * \param context Suggestion for what context the transfer target extension can be found in
  1305. *
  1306. * \retval NULL Failure to allocate or initialize
  1307. * \retval non-NULL Newly allocated properties
  1308. */
  1309. static struct attended_transfer_properties *attended_transfer_properties_alloc(
  1310. struct ast_channel *transferer, const char *context)
  1311. {
  1312. struct attended_transfer_properties *props;
  1313. char *tech;
  1314. char *addr;
  1315. char *serial;
  1316. struct ast_features_xfer_config *xfer_cfg;
  1317. struct ast_flags *transferer_features;
  1318. props = ao2_alloc(sizeof(*props), attended_transfer_properties_destructor);
  1319. if (!props) {
  1320. ast_log(LOG_ERROR, "Unable to create props - channel %s, context %s\n",
  1321. ast_channel_name(transferer), context);
  1322. return NULL;
  1323. }
  1324. ast_cond_init(&props->cond, NULL);
  1325. if (ast_string_field_init(props, 64)) {
  1326. ast_log(LOG_ERROR, "Unable to initialize prop fields - channel %s, context %s\n",
  1327. ast_channel_name(transferer), context);
  1328. ao2_ref(props, -1);
  1329. return NULL;
  1330. }
  1331. props->target_framehook_id = -1;
  1332. props->transferer = ast_channel_ref(transferer);
  1333. ast_channel_lock(props->transferer);
  1334. xfer_cfg = ast_get_chan_features_xfer_config(props->transferer);
  1335. if (!xfer_cfg) {
  1336. ast_log(LOG_ERROR, "Unable to get transfer configuration from channel %s\n", ast_channel_name(props->transferer));
  1337. ast_channel_unlock(props->transferer);
  1338. ao2_ref(props, -1);
  1339. return NULL;
  1340. }
  1341. transferer_features = ast_bridge_features_ds_get(props->transferer);
  1342. if (transferer_features) {
  1343. props->transferer_features = *transferer_features;
  1344. }
  1345. props->atxferdropcall = xfer_cfg->atxferdropcall;
  1346. props->atxfercallbackretries = xfer_cfg->atxfercallbackretries;
  1347. props->atxfernoanswertimeout = xfer_cfg->atxfernoanswertimeout;
  1348. props->atxferloopdelay = xfer_cfg->atxferloopdelay;
  1349. ast_string_field_set(props, context, get_transfer_context(transferer, context));
  1350. ast_string_field_set(props, xfersound, xfer_cfg->xfersound);
  1351. ao2_ref(xfer_cfg, -1);
  1352. /*
  1353. * Save the transferee's party information for any recall calls.
  1354. * This is the only piece of information needed that gets overwritten
  1355. * on the transferer channel by the inital call to the transfer target.
  1356. */
  1357. ast_party_connected_line_copy(&props->original_transferer_colp,
  1358. ast_channel_connected(props->transferer));
  1359. tech = ast_strdupa(ast_channel_name(props->transferer));
  1360. addr = strchr(tech, '/');
  1361. if (!addr) {
  1362. ast_log(LOG_ERROR, "Transferer channel name does not follow typical channel naming format (tech/address)\n");
  1363. ast_channel_unlock(props->transferer);
  1364. ao2_ref(props, -1);
  1365. return NULL;
  1366. }
  1367. *addr++ = '\0';
  1368. serial = strrchr(addr, '-');
  1369. if (serial) {
  1370. *serial = '\0';
  1371. }
  1372. ast_string_field_set(props, transferer_type, tech);
  1373. ast_string_field_set(props, transferer_addr, addr);
  1374. ast_channel_unlock(props->transferer);
  1375. ast_debug(1, "Allocated attended transfer properties %p for transfer from %s\n",
  1376. props, ast_channel_name(props->transferer));
  1377. return props;
  1378. }
  1379. /*!
  1380. * \brief Free backlog of stimuli in the queue
  1381. */
  1382. static void clear_stimulus_queue(struct attended_transfer_properties *props)
  1383. {
  1384. struct stimulus_list *list;
  1385. SCOPED_AO2LOCK(lock, props);
  1386. while ((list = AST_LIST_REMOVE_HEAD(&props->stimulus_queue, next))) {
  1387. ast_free(list);
  1388. }
  1389. }
  1390. /*!
  1391. * \brief Initiate shutdown of attended transfer properties
  1392. *
  1393. * Calling this indicates that the attended transfer properties are no longer needed
  1394. * because the transfer operation has concluded.
  1395. */
  1396. static void attended_transfer_properties_shutdown(struct attended_transfer_properties *props)
  1397. {
  1398. ast_debug(1, "Shutting down attended transfer %p\n", props);
  1399. if (props->transferee_bridge) {
  1400. bridge_basic_change_personality(props->transferee_bridge,
  1401. BRIDGE_BASIC_PERSONALITY_NORMAL, NULL);
  1402. ast_bridge_merge_inhibit(props->transferee_bridge, -1);
  1403. }
  1404. if (props->target_bridge) {
  1405. ast_bridge_destroy(props->target_bridge, 0);
  1406. props->target_bridge = NULL;
  1407. }
  1408. if (props->transferer) {
  1409. ast_channel_remove_bridge_role(props->transferer, AST_TRANSFERER_ROLE_NAME);
  1410. }
  1411. clear_stimulus_queue(props);
  1412. ao2_cleanup(props);
  1413. }
  1414. static void stimulate_attended_transfer(struct attended_transfer_properties *props,
  1415. enum attended_transfer_stimulus stimulus)
  1416. {
  1417. struct stimulus_list *list;
  1418. list = ast_calloc(1, sizeof(*list));
  1419. if (!list) {
  1420. ast_log(LOG_ERROR, "Unable to push event to attended transfer queue. Expect transfer to fail\n");
  1421. return;
  1422. }
  1423. list->stimulus = stimulus;
  1424. ao2_lock(props);
  1425. AST_LIST_INSERT_TAIL(&props->stimulus_queue, list, next);
  1426. ast_cond_signal(&props->cond);
  1427. ao2_unlock(props);
  1428. }
  1429. static void remove_attended_transfer_stimulus(struct attended_transfer_properties *props,
  1430. enum attended_transfer_stimulus stimulus)
  1431. {
  1432. struct stimulus_list *list;
  1433. ao2_lock(props);
  1434. AST_LIST_TRAVERSE_SAFE_BEGIN(&props->stimulus_queue, list, next) {
  1435. if (list->stimulus == stimulus) {
  1436. AST_LIST_REMOVE_CURRENT(next);
  1437. ast_free(list);
  1438. break;
  1439. }
  1440. }
  1441. AST_LIST_TRAVERSE_SAFE_END;
  1442. ao2_unlock(props);
  1443. }
  1444. /*!
  1445. * \brief Get a desired transfer party for a bridge the transferer is not in.
  1446. *
  1447. * \param bridge The bridge to get the party from. May be NULL.
  1448. * \param[out] party The lone channel in the bridge. Will be set NULL if bridge is NULL or multiple parties are present.
  1449. */
  1450. static void get_transfer_party_non_transferer_bridge(struct ast_bridge *bridge,
  1451. struct ast_channel **party)
  1452. {
  1453. if (bridge && bridge->num_channels == 1) {
  1454. *party = ast_channel_ref(AST_LIST_FIRST(&bridge->channels)->chan);
  1455. } else {
  1456. *party = NULL;
  1457. }
  1458. }
  1459. /*!
  1460. * \brief Get the transferee and transfer target when the transferer is in a bridge with
  1461. * one of the desired parties.
  1462. *
  1463. * \param transferer_bridge The bridge the transferer is in
  1464. * \param other_bridge The bridge the transferer is not in. May be NULL.
  1465. * \param transferer The transferer party
  1466. * \param[out] transferer_peer The party that is in the bridge with the transferer
  1467. * \param[out] other_party The party that is in the other_bridge
  1468. */
  1469. static void get_transfer_parties_transferer_bridge(struct ast_bridge *transferer_bridge,
  1470. struct ast_bridge *other_bridge, struct ast_channel *transferer,
  1471. struct ast_channel **transferer_peer, struct ast_channel **other_party)
  1472. {
  1473. *transferer_peer = ast_bridge_peer(transferer_bridge, transferer);
  1474. get_transfer_party_non_transferer_bridge(other_bridge, other_party);
  1475. }
  1476. /*!
  1477. * \brief determine transferee and transfer target for an attended transfer
  1478. *
  1479. * In builtin attended transfers, there is a single transferer channel that jumps between
  1480. * the two bridges involved. At the time the attended transfer occurs, the transferer could
  1481. * be in either bridge, so determining the parties is a bit more complex than normal.
  1482. *
  1483. * The method used here is to determine which of the two bridges the transferer is in, and
  1484. * grabbing the peer from that bridge. The other bridge, if it only has a single channel in it,
  1485. * has the other desired channel.
  1486. *
  1487. * \param transferer The channel performing the transfer
  1488. * \param transferee_bridge The bridge that the transferee is in
  1489. * \param target_bridge The bridge that the transfer target is in
  1490. * \param[out] transferee The transferee channel
  1491. * \param[out] transfer_target The transfer target channel
  1492. */
  1493. static void get_transfer_parties(struct ast_channel *transferer, struct ast_bridge *transferee_bridge,
  1494. struct ast_bridge *target_bridge, struct ast_channel **transferee,
  1495. struct ast_channel **transfer_target)
  1496. {
  1497. struct ast_bridge *transferer_bridge;
  1498. ast_channel_lock(transferer);
  1499. transferer_bridge = ast_channel_get_bridge(transferer);
  1500. ast_channel_unlock(transferer);
  1501. if (transferer_bridge == transferee_bridge) {
  1502. get_transfer_parties_transferer_bridge(transferee_bridge, target_bridge,
  1503. transferer, transferee, transfer_target);
  1504. } else if (transferer_bridge == target_bridge) {
  1505. get_transfer_parties_transferer_bridge(target_bridge, transferee_bridge,
  1506. transferer, transfer_target, transferee);
  1507. } else {
  1508. get_transfer_party_non_transferer_bridge(transferee_bridge, transferee);
  1509. get_transfer_party_non_transferer_bridge(target_bridge, transfer_target);
  1510. }
  1511. ao2_cleanup(transferer_bridge);
  1512. }
  1513. /*!
  1514. * \brief Send a stasis publication for a successful attended transfer
  1515. */
  1516. static void publish_transfer_success(struct attended_transfer_properties *props,
  1517. struct ast_channel *transferee_channel, struct ast_channel *target_channel)
  1518. {
  1519. struct ast_attended_transfer_message *transfer_msg;
  1520. transfer_msg = ast_attended_transfer_message_create(0, props->transferer,
  1521. props->transferee_bridge, props->transferer, props->target_bridge,
  1522. transferee_channel, target_channel);
  1523. if (!transfer_msg) {
  1524. ast_log(LOG_ERROR, "Unable to publish successful attended transfer from %s\n",
  1525. ast_channel_name(props->transferer));
  1526. return;
  1527. }
  1528. ast_attended_transfer_message_add_merge(transfer_msg, props->transferee_bridge);
  1529. ast_bridge_publish_attended_transfer(transfer_msg);
  1530. ao2_cleanup(transfer_msg);
  1531. }
  1532. /*!
  1533. * \brief Send a stasis publication for an attended transfer that ends in a threeway call
  1534. */
  1535. static void publish_transfer_threeway(struct attended_transfer_properties *props,
  1536. struct ast_channel *transferee_channel, struct ast_channel *target_channel)
  1537. {
  1538. struct ast_attended_transfer_message *transfer_msg;
  1539. transfer_msg = ast_attended_transfer_message_create(0, props->transferer,
  1540. props->transferee_bridge, props->transferer, props->target_bridge,
  1541. transferee_channel, target_channel);
  1542. if (!transfer_msg) {
  1543. ast_log(LOG_ERROR, "Unable to publish successful three-way transfer from %s\n",
  1544. ast_channel_name(props->transferer));
  1545. return;
  1546. }
  1547. ast_attended_transfer_message_add_threeway(transfer_msg, props->transferer,
  1548. props->transferee_bridge);
  1549. ast_bridge_publish_attended_transfer(transfer_msg);
  1550. ao2_cleanup(transfer_msg);
  1551. }
  1552. /*!
  1553. * \brief Send a stasis publication for a failed attended transfer
  1554. */
  1555. static void publish_transfer_fail(struct attended_transfer_properties *props)
  1556. {
  1557. struct ast_attended_transfer_message *transfer_msg;
  1558. transfer_msg = ast_attended_transfer_message_create(0, props->transferer,
  1559. props->transferee_bridge, props->transferer, props->target_bridge,
  1560. NULL, NULL);
  1561. if (!transfer_msg) {
  1562. ast_log(LOG_ERROR, "Unable to publish failed transfer from %s\n",
  1563. ast_channel_name(props->transferer));
  1564. return;
  1565. }
  1566. transfer_msg->result = AST_BRIDGE_TRANSFER_FAIL;
  1567. ast_bridge_publish_attended_transfer(transfer_msg);
  1568. ao2_cleanup(transfer_msg);
  1569. }
  1570. /*!
  1571. * \brief Helper method to play a sound on a channel in a bridge
  1572. *
  1573. * \param chan The channel to play the sound to
  1574. * \param sound The sound to play
  1575. */
  1576. static void play_sound(struct ast_channel *chan, const char *sound)
  1577. {
  1578. struct ast_bridge_channel *bridge_channel;
  1579. ast_channel_lock(chan);
  1580. bridge_channel = ast_channel_get_bridge_channel(chan);
  1581. ast_channel_unlock(chan);
  1582. if (bridge_channel) {
  1583. ast_bridge_channel_queue_playfile(bridge_channel, NULL, sound, NULL);
  1584. ao2_ref(bridge_channel, -1);
  1585. }
  1586. }
  1587. /*!
  1588. * \brief Helper method to play a fail sound on a channel in a bridge
  1589. *
  1590. * \param chan The channel to play the fail sound to
  1591. */
  1592. static void play_failsound(struct ast_channel *chan)
  1593. {
  1594. char *sound;
  1595. ast_channel_lock(chan);
  1596. sound = ast_get_chan_features_xferfailsound(chan);
  1597. ast_channel_unlock(chan);
  1598. if (sound) {
  1599. play_sound(chan, sound);
  1600. ast_free(sound);
  1601. }
  1602. }
  1603. /*!
  1604. * \brief Helper method to stream a fail sound on a channel
  1605. *
  1606. * \param chan The channel to stream the fail sound to
  1607. */
  1608. static void stream_failsound(struct ast_channel *chan)
  1609. {
  1610. char *sound;
  1611. ast_channel_lock(chan);
  1612. sound = ast_get_chan_features_xferfailsound(chan);
  1613. ast_channel_unlock(chan);
  1614. if (sound) {
  1615. ast_stream_and_wait(chan, sound, AST_DIGIT_NONE);
  1616. ast_free(sound);
  1617. }
  1618. }
  1619. /*!
  1620. * \brief Helper method to place a channel in a bridge on hold
  1621. */
  1622. static void hold(struct ast_channel *chan)
  1623. {
  1624. struct ast_bridge_channel *bridge_channel;
  1625. if (!chan) {
  1626. return;
  1627. }
  1628. ast_channel_lock(chan);
  1629. bridge_channel = ast_channel_get_bridge_channel(chan);
  1630. ast_channel_unlock(chan);
  1631. if (bridge_channel) {
  1632. ast_bridge_channel_write_hold(bridge_channel, NULL);
  1633. ao2_ref(bridge_channel, -1);
  1634. }
  1635. }
  1636. /*!
  1637. * \brief Helper method to take a channel in a bridge off hold
  1638. */
  1639. static void unhold(struct ast_channel *chan)
  1640. {
  1641. struct ast_bridge_channel *bridge_channel;
  1642. if (!chan) {
  1643. return;
  1644. }
  1645. ast_channel_lock(chan);
  1646. bridge_channel = ast_channel_get_bridge_channel(chan);
  1647. ast_channel_unlock(chan);
  1648. if (bridge_channel) {
  1649. ast_bridge_channel_write_unhold(bridge_channel);
  1650. ao2_ref(bridge_channel, -1);
  1651. }
  1652. }
  1653. /*!
  1654. * \brief Helper method to send a ringing indication to a channel in a bridge
  1655. */
  1656. static void ringing(struct ast_channel *chan)
  1657. {
  1658. struct ast_bridge_channel *bridge_channel;
  1659. ast_channel_lock(chan);
  1660. bridge_channel = ast_channel_get_bridge_channel(chan);
  1661. ast_channel_unlock(chan);
  1662. if (bridge_channel) {
  1663. ast_bridge_channel_write_control_data(bridge_channel, AST_CONTROL_RINGING, NULL, 0);
  1664. ao2_ref(bridge_channel, -1);
  1665. }
  1666. }
  1667. /*!
  1668. * \brief Helper method to send a ringing indication to all channels in a bridge
  1669. */
  1670. static void bridge_ringing(struct ast_bridge *bridge)
  1671. {
  1672. struct ast_frame ringing = {
  1673. .frametype = AST_FRAME_CONTROL,
  1674. .subclass.integer = AST_CONTROL_RINGING,
  1675. };
  1676. ast_bridge_queue_everyone_else(bridge, NULL, &ringing);
  1677. }
  1678. /*!
  1679. * \brief Helper method to send a hold frame to all channels in a bridge
  1680. */
  1681. static void bridge_hold(struct ast_bridge *bridge)
  1682. {
  1683. struct ast_frame hold = {
  1684. .frametype = AST_FRAME_CONTROL,
  1685. .subclass.integer = AST_CONTROL_HOLD,
  1686. };
  1687. ast_bridge_queue_everyone_else(bridge, NULL, &hold);
  1688. }
  1689. /*!
  1690. * \brief Helper method to send an unhold frame to all channels in a bridge
  1691. */
  1692. static void bridge_unhold(struct ast_bridge *bridge)
  1693. {
  1694. struct ast_frame unhold = {
  1695. .frametype = AST_FRAME_CONTROL,
  1696. .subclass.integer = AST_CONTROL_UNHOLD,
  1697. };
  1698. ast_bridge_queue_everyone_else(bridge, NULL, &unhold);
  1699. }
  1700. /*!
  1701. * \brief Wrapper for \ref bridge_do_move
  1702. */
  1703. static void bridge_move(struct ast_bridge *dest, struct ast_bridge *src, struct ast_channel *channel, struct ast_channel *swap)
  1704. {
  1705. struct ast_bridge_channel *bridge_channel;
  1706. ast_bridge_lock_both(src, dest);
  1707. ast_channel_lock(channel);
  1708. bridge_channel = ast_channel_get_bridge_channel(channel);
  1709. ast_channel_unlock(channel);
  1710. if (bridge_channel) {
  1711. ao2_lock(bridge_channel);
  1712. bridge_channel->swap = swap;
  1713. ao2_unlock(bridge_channel);
  1714. bridge_do_move(dest, bridge_channel, 1, 0);
  1715. }
  1716. ast_bridge_unlock(dest);
  1717. ast_bridge_unlock(src);
  1718. ao2_cleanup(bridge_channel);
  1719. }
  1720. /*!
  1721. * \brief Wrapper for \ref bridge_do_merge
  1722. */
  1723. static void bridge_merge(struct ast_bridge *dest, struct ast_bridge *src, struct ast_channel **kick_channels, unsigned int num_channels)
  1724. {
  1725. struct ast_bridge_channel **kick_bridge_channels = num_channels ?
  1726. ast_alloca(num_channels * sizeof(*kick_bridge_channels)) : NULL;
  1727. int i;
  1728. int num_bridge_channels = 0;
  1729. ast_bridge_lock_both(dest, src);
  1730. for (i = 0; i < num_channels; ++i) {
  1731. struct ast_bridge_channel *kick_bridge_channel;
  1732. kick_bridge_channel = bridge_find_channel(src, kick_channels[i]);
  1733. if (!kick_bridge_channel) {
  1734. kick_bridge_channel = bridge_find_channel(dest, kick_channels[i]);
  1735. }
  1736. /* It's possible (and fine) for the bridge channel to be NULL at this point if the
  1737. * channel has hung up already. If that happens, we can just remove it from the list
  1738. * of bridge channels to kick from the bridge
  1739. */
  1740. if (!kick_bridge_channel) {
  1741. continue;
  1742. }
  1743. kick_bridge_channels[num_bridge_channels++] = kick_bridge_channel;
  1744. }
  1745. bridge_do_merge(dest, src, kick_bridge_channels, num_bridge_channels, 0);
  1746. ast_bridge_unlock(dest);
  1747. ast_bridge_unlock(src);
  1748. }
  1749. /*!
  1750. * \brief Flags that indicate properties of attended transfer states
  1751. */
  1752. enum attended_transfer_state_flags {
  1753. /*! This state requires that the timer be reset when entering the state */
  1754. TRANSFER_STATE_FLAG_TIMER_RESET = (1 << 0),
  1755. /*! This state's timer uses atxferloopdelay */
  1756. TRANSFER_STATE_FLAG_TIMER_LOOP_DELAY = (1 << 1),
  1757. /*! This state's timer uses atxfernoanswertimeout */
  1758. TRANSFER_STATE_FLAG_ATXFER_NO_ANSWER = (1 << 2),
  1759. /*! This state has a time limit associated with it */
  1760. TRANSFER_STATE_FLAG_TIMED = (TRANSFER_STATE_FLAG_TIMER_RESET |
  1761. TRANSFER_STATE_FLAG_TIMER_LOOP_DELAY | TRANSFER_STATE_FLAG_ATXFER_NO_ANSWER),
  1762. /*! This state does not transition to any other states */
  1763. TRANSFER_STATE_FLAG_TERMINAL = (1 << 3),
  1764. };
  1765. static int calling_target_enter(struct attended_transfer_properties *props);
  1766. static enum attended_transfer_state calling_target_exit(struct attended_transfer_properties *props,
  1767. enum attended_transfer_stimulus stimulus);
  1768. static int hesitant_enter(struct attended_transfer_properties *props);
  1769. static enum attended_transfer_state hesitant_exit(struct attended_transfer_properties *props,
  1770. enum attended_transfer_stimulus stimulus);
  1771. static int rebridge_enter(struct attended_transfer_properties *props);
  1772. static int resume_enter(struct attended_transfer_properties *props);
  1773. static int threeway_enter(struct attended_transfer_properties *props);
  1774. static int consulting_enter(struct attended_transfer_properties *props);
  1775. static enum attended_transfer_state consulting_exit(struct attended_transfer_properties *props,
  1776. enum attended_transfer_stimulus stimulus);
  1777. static int double_checking_enter(struct attended_transfer_properties *props);
  1778. static enum attended_transfer_state double_checking_exit(struct attended_transfer_properties *props,
  1779. enum attended_transfer_stimulus stimulus);
  1780. static int complete_enter(struct attended_transfer_properties *props);
  1781. static int blond_enter(struct attended_transfer_properties *props);
  1782. static int blond_nonfinal_enter(struct attended_transfer_properties *props);
  1783. static enum attended_transfer_state blond_nonfinal_exit(struct attended_transfer_properties *props,
  1784. enum attended_transfer_stimulus stimulus);
  1785. static int recalling_enter(struct attended_transfer_properties *props);
  1786. static enum attended_transfer_state recalling_exit(struct attended_transfer_properties *props,
  1787. enum attended_transfer_stimulus stimulus);
  1788. static int wait_to_retransfer_enter(struct attended_transfer_properties *props);
  1789. static enum attended_transfer_state wait_to_retransfer_exit(struct attended_transfer_properties *props,
  1790. enum attended_transfer_stimulus stimulus);
  1791. static int retransfer_enter(struct attended_transfer_properties *props);
  1792. static enum attended_transfer_state retransfer_exit(struct attended_transfer_properties *props,
  1793. enum attended_transfer_stimulus stimulus);
  1794. static int wait_to_recall_enter(struct attended_transfer_properties *props);
  1795. static enum attended_transfer_state wait_to_recall_exit(struct attended_transfer_properties *props,
  1796. enum attended_transfer_stimulus stimulus);
  1797. static int fail_enter(struct attended_transfer_properties *props);
  1798. /*!
  1799. * \brief Properties of an attended transfer state
  1800. */
  1801. struct attended_transfer_state_properties {
  1802. /*! The name of the state. Used for debugging */
  1803. const char *state_name;
  1804. /*! Function used to enter a state */
  1805. int (*enter)(struct attended_transfer_properties *props);
  1806. /*!
  1807. * Function used to exit a state
  1808. * This is used both to determine what the next state
  1809. * to transition to will be and to perform any cleanup
  1810. * necessary before exiting the current state.
  1811. */
  1812. enum attended_transfer_state (*exit)(struct attended_transfer_properties *props,
  1813. enum attended_transfer_stimulus stimulus);
  1814. /*! Flags associated with this state */
  1815. enum attended_transfer_state_flags flags;
  1816. };
  1817. static const struct attended_transfer_state_properties state_properties[] = {
  1818. [TRANSFER_CALLING_TARGET] = {
  1819. .state_name = "Calling Target",
  1820. .enter = calling_target_enter,
  1821. .exit = calling_target_exit,
  1822. .flags = TRANSFER_STATE_FLAG_ATXFER_NO_ANSWER | TRANSFER_STATE_FLAG_TIMER_RESET,
  1823. },
  1824. [TRANSFER_HESITANT] = {
  1825. .state_name = "Hesitant",
  1826. .enter = hesitant_enter,
  1827. .exit = hesitant_exit,
  1828. .flags = TRANSFER_STATE_FLAG_ATXFER_NO_ANSWER,
  1829. },
  1830. [TRANSFER_REBRIDGE] = {
  1831. .state_name = "Rebridge",
  1832. .enter = rebridge_enter,
  1833. .flags = TRANSFER_STATE_FLAG_TERMINAL,
  1834. },
  1835. [TRANSFER_RESUME] = {
  1836. .state_name = "Resume",
  1837. .enter = resume_enter,
  1838. .flags = TRANSFER_STATE_FLAG_TERMINAL,
  1839. },
  1840. [TRANSFER_THREEWAY] = {
  1841. .state_name = "Threeway",
  1842. .enter = threeway_enter,
  1843. .flags = TRANSFER_STATE_FLAG_TERMINAL,
  1844. },
  1845. [TRANSFER_CONSULTING] = {
  1846. .state_name = "Consulting",
  1847. .enter = consulting_enter,
  1848. .exit = consulting_exit,
  1849. },
  1850. [TRANSFER_DOUBLECHECKING] = {
  1851. .state_name = "Double Checking",
  1852. .enter = double_checking_enter,
  1853. .exit = double_checking_exit,
  1854. },
  1855. [TRANSFER_COMPLETE] = {
  1856. .state_name = "Complete",
  1857. .enter = complete_enter,
  1858. .flags = TRANSFER_STATE_FLAG_TERMINAL,
  1859. },
  1860. [TRANSFER_BLOND] = {
  1861. .state_name = "Blond",
  1862. .enter = blond_enter,
  1863. .flags = TRANSFER_STATE_FLAG_TERMINAL,
  1864. },
  1865. [TRANSFER_BLOND_NONFINAL] = {
  1866. .state_name = "Blond Non-Final",
  1867. .enter = blond_nonfinal_enter,
  1868. .exit = blond_nonfinal_exit,
  1869. .flags = TRANSFER_STATE_FLAG_ATXFER_NO_ANSWER,
  1870. },
  1871. [TRANSFER_RECALLING] = {
  1872. .state_name = "Recalling",
  1873. .enter = recalling_enter,
  1874. .exit = recalling_exit,
  1875. .flags = TRANSFER_STATE_FLAG_ATXFER_NO_ANSWER | TRANSFER_STATE_FLAG_TIMER_RESET,
  1876. },
  1877. [TRANSFER_WAIT_TO_RETRANSFER] = {
  1878. .state_name = "Wait to Retransfer",
  1879. .enter = wait_to_retransfer_enter,
  1880. .exit = wait_to_retransfer_exit,
  1881. .flags = TRANSFER_STATE_FLAG_TIMER_RESET | TRANSFER_STATE_FLAG_TIMER_LOOP_DELAY,
  1882. },
  1883. [TRANSFER_RETRANSFER] = {
  1884. .state_name = "Retransfer",
  1885. .enter = retransfer_enter,
  1886. .exit = retransfer_exit,
  1887. .flags = TRANSFER_STATE_FLAG_ATXFER_NO_ANSWER | TRANSFER_STATE_FLAG_TIMER_RESET,
  1888. },
  1889. [TRANSFER_WAIT_TO_RECALL] = {
  1890. .state_name = "Wait to Recall",
  1891. .enter = wait_to_recall_enter,
  1892. .exit = wait_to_recall_exit,
  1893. .flags = TRANSFER_STATE_FLAG_TIMER_RESET | TRANSFER_STATE_FLAG_TIMER_LOOP_DELAY,
  1894. },
  1895. [TRANSFER_FAIL] = {
  1896. .state_name = "Fail",
  1897. .enter = fail_enter,
  1898. .flags = TRANSFER_STATE_FLAG_TERMINAL,
  1899. },
  1900. };
  1901. static int calling_target_enter(struct attended_transfer_properties *props)
  1902. {
  1903. bridge_move(props->target_bridge, props->transferee_bridge, props->transferer, NULL);
  1904. return 0;
  1905. }
  1906. static enum attended_transfer_state calling_target_exit(struct attended_transfer_properties *props,
  1907. enum attended_transfer_stimulus stimulus)
  1908. {
  1909. switch (stimulus) {
  1910. case STIMULUS_TRANSFEREE_HANGUP:
  1911. play_failsound(props->transferer);
  1912. publish_transfer_fail(props);
  1913. return TRANSFER_FAIL;
  1914. case STIMULUS_DTMF_ATXFER_COMPLETE:
  1915. case STIMULUS_TRANSFERER_HANGUP:
  1916. bridge_unhold(props->transferee_bridge);
  1917. return props->atxferdropcall ? TRANSFER_BLOND : TRANSFER_BLOND_NONFINAL;
  1918. case STIMULUS_TRANSFER_TARGET_ANSWER:
  1919. return TRANSFER_CONSULTING;
  1920. case STIMULUS_TRANSFER_TARGET_HANGUP:
  1921. case STIMULUS_TIMEOUT:
  1922. case STIMULUS_DTMF_ATXFER_ABORT:
  1923. play_failsound(props->transferer);
  1924. return TRANSFER_REBRIDGE;
  1925. case STIMULUS_DTMF_ATXFER_THREEWAY:
  1926. bridge_unhold(props->transferee_bridge);
  1927. return TRANSFER_THREEWAY;
  1928. case STIMULUS_DTMF_ATXFER_SWAP:
  1929. return TRANSFER_HESITANT;
  1930. case STIMULUS_NONE:
  1931. case STIMULUS_RECALL_TARGET_ANSWER:
  1932. case STIMULUS_RECALL_TARGET_HANGUP:
  1933. default:
  1934. ast_log(LOG_WARNING, "Unexpected stimulus '%s' received in attended transfer state '%s'\n",
  1935. stimulus_strs[stimulus], state_properties[props->state].state_name);
  1936. return props->state;
  1937. }
  1938. }
  1939. static int hesitant_enter(struct attended_transfer_properties *props)
  1940. {
  1941. bridge_move(props->transferee_bridge, props->target_bridge, props->transferer, NULL);
  1942. unhold(props->transferer);
  1943. return 0;
  1944. }
  1945. static enum attended_transfer_state hesitant_exit(struct attended_transfer_properties *props,
  1946. enum attended_transfer_stimulus stimulus)
  1947. {
  1948. switch (stimulus) {
  1949. case STIMULUS_TRANSFEREE_HANGUP:
  1950. play_failsound(props->transferer);
  1951. publish_transfer_fail(props);
  1952. return TRANSFER_FAIL;
  1953. case STIMULUS_DTMF_ATXFER_COMPLETE:
  1954. case STIMULUS_TRANSFERER_HANGUP:
  1955. return props->atxferdropcall ? TRANSFER_BLOND : TRANSFER_BLOND_NONFINAL;
  1956. case STIMULUS_TRANSFER_TARGET_ANSWER:
  1957. return TRANSFER_DOUBLECHECKING;
  1958. case STIMULUS_TRANSFER_TARGET_HANGUP:
  1959. case STIMULUS_TIMEOUT:
  1960. case STIMULUS_DTMF_ATXFER_ABORT:
  1961. play_failsound(props->transferer);
  1962. return TRANSFER_RESUME;
  1963. case STIMULUS_DTMF_ATXFER_THREEWAY:
  1964. return TRANSFER_THREEWAY;
  1965. case STIMULUS_DTMF_ATXFER_SWAP:
  1966. hold(props->transferer);
  1967. return TRANSFER_CALLING_TARGET;
  1968. case STIMULUS_NONE:
  1969. case STIMULUS_RECALL_TARGET_HANGUP:
  1970. case STIMULUS_RECALL_TARGET_ANSWER:
  1971. default:
  1972. ast_log(LOG_WARNING, "Unexpected stimulus '%s' received in attended transfer state '%s'\n",
  1973. stimulus_strs[stimulus], state_properties[props->state].state_name);
  1974. return props->state;
  1975. }
  1976. }
  1977. static int rebridge_enter(struct attended_transfer_properties *props)
  1978. {
  1979. bridge_move(props->transferee_bridge, props->target_bridge, props->transferer, NULL);
  1980. unhold(props->transferer);
  1981. return 0;
  1982. }
  1983. static int resume_enter(struct attended_transfer_properties *props)
  1984. {
  1985. return 0;
  1986. }
  1987. static int threeway_enter(struct attended_transfer_properties *props)
  1988. {
  1989. struct ast_channel *transferee_channel;
  1990. struct ast_channel *target_channel;
  1991. get_transfer_parties(props->transferer, props->transferee_bridge, props->target_bridge,
  1992. &transferee_channel, &target_channel);
  1993. bridge_merge(props->transferee_bridge, props->target_bridge, NULL, 0);
  1994. play_sound(props->transfer_target, props->xfersound);
  1995. play_sound(props->transferer, props->xfersound);
  1996. publish_transfer_threeway(props, transferee_channel, target_channel);
  1997. ast_channel_cleanup(transferee_channel);
  1998. ast_channel_cleanup(target_channel);
  1999. return 0;
  2000. }
  2001. static int consulting_enter(struct attended_transfer_properties *props)
  2002. {
  2003. return 0;
  2004. }
  2005. static enum attended_transfer_state consulting_exit(struct attended_transfer_properties *props,
  2006. enum attended_transfer_stimulus stimulus)
  2007. {
  2008. switch (stimulus) {
  2009. case STIMULUS_TRANSFEREE_HANGUP:
  2010. /* This is a one-of-a-kind event. The transferer and transfer target are talking in
  2011. * one bridge, and the transferee has hung up in a separate bridge. In this case, we
  2012. * will change the personality of the transfer target bridge back to normal, and play
  2013. * a sound to the transferer to indicate the transferee is gone.
  2014. */
  2015. bridge_basic_change_personality(props->target_bridge, BRIDGE_BASIC_PERSONALITY_NORMAL, NULL);
  2016. play_failsound(props->transferer);
  2017. ast_bridge_merge_inhibit(props->target_bridge, -1);
  2018. /* These next two lines are here to ensure that our reference to the target bridge
  2019. * is cleaned up properly and that the target bridge is not destroyed when the
  2020. * monitor thread exits
  2021. */
  2022. ao2_ref(props->target_bridge, -1);
  2023. props->target_bridge = NULL;
  2024. return TRANSFER_FAIL;
  2025. case STIMULUS_TRANSFERER_HANGUP:
  2026. case STIMULUS_DTMF_ATXFER_COMPLETE:
  2027. /* We know the transferer is in the target_bridge, so take the other bridge off hold */
  2028. bridge_unhold(props->transferee_bridge);
  2029. return TRANSFER_COMPLETE;
  2030. case STIMULUS_TRANSFER_TARGET_HANGUP:
  2031. return TRANSFER_REBRIDGE;
  2032. case STIMULUS_DTMF_ATXFER_ABORT:
  2033. play_failsound(props->transferer);
  2034. return TRANSFER_REBRIDGE;
  2035. case STIMULUS_DTMF_ATXFER_THREEWAY:
  2036. bridge_unhold(props->transferee_bridge);
  2037. return TRANSFER_THREEWAY;
  2038. case STIMULUS_DTMF_ATXFER_SWAP:
  2039. hold(props->transferer);
  2040. bridge_move(props->transferee_bridge, props->target_bridge, props->transferer, NULL);
  2041. unhold(props->transferer);
  2042. return TRANSFER_DOUBLECHECKING;
  2043. case STIMULUS_NONE:
  2044. case STIMULUS_TIMEOUT:
  2045. case STIMULUS_TRANSFER_TARGET_ANSWER:
  2046. case STIMULUS_RECALL_TARGET_HANGUP:
  2047. case STIMULUS_RECALL_TARGET_ANSWER:
  2048. default:
  2049. ast_log(LOG_WARNING, "Unexpected stimulus '%s' received in attended transfer state '%s'\n",
  2050. stimulus_strs[stimulus], state_properties[props->state].state_name);
  2051. return props->state;
  2052. }
  2053. }
  2054. static int double_checking_enter(struct attended_transfer_properties *props)
  2055. {
  2056. return 0;
  2057. }
  2058. static enum attended_transfer_state double_checking_exit(struct attended_transfer_properties *props,
  2059. enum attended_transfer_stimulus stimulus)
  2060. {
  2061. switch (stimulus) {
  2062. case STIMULUS_TRANSFEREE_HANGUP:
  2063. play_failsound(props->transferer);
  2064. publish_transfer_fail(props);
  2065. return TRANSFER_FAIL;
  2066. case STIMULUS_TRANSFERER_HANGUP:
  2067. case STIMULUS_DTMF_ATXFER_COMPLETE:
  2068. /* We know the transferer is in the transferee, so take the other bridge off hold */
  2069. bridge_unhold(props->target_bridge);
  2070. return TRANSFER_COMPLETE;
  2071. case STIMULUS_TRANSFER_TARGET_HANGUP:
  2072. case STIMULUS_DTMF_ATXFER_ABORT:
  2073. play_failsound(props->transferer);
  2074. return TRANSFER_RESUME;
  2075. case STIMULUS_DTMF_ATXFER_THREEWAY:
  2076. bridge_unhold(props->target_bridge);
  2077. return TRANSFER_THREEWAY;
  2078. case STIMULUS_DTMF_ATXFER_SWAP:
  2079. hold(props->transferer);
  2080. bridge_move(props->target_bridge, props->transferee_bridge, props->transferer, NULL);
  2081. unhold(props->transferer);
  2082. return TRANSFER_CONSULTING;
  2083. case STIMULUS_NONE:
  2084. case STIMULUS_TIMEOUT:
  2085. case STIMULUS_TRANSFER_TARGET_ANSWER:
  2086. case STIMULUS_RECALL_TARGET_HANGUP:
  2087. case STIMULUS_RECALL_TARGET_ANSWER:
  2088. default:
  2089. ast_log(LOG_WARNING, "Unexpected stimulus '%s' received in attended transfer state '%s'\n",
  2090. stimulus_strs[stimulus], state_properties[props->state].state_name);
  2091. return props->state;
  2092. }
  2093. }
  2094. static int complete_enter(struct attended_transfer_properties *props)
  2095. {
  2096. struct ast_channel *transferee_channel;
  2097. struct ast_channel *target_channel;
  2098. get_transfer_parties(props->transferer, props->transferee_bridge, props->target_bridge,
  2099. &transferee_channel, &target_channel);
  2100. bridge_merge(props->transferee_bridge, props->target_bridge, &props->transferer, 1);
  2101. play_sound(props->transfer_target, props->xfersound);
  2102. publish_transfer_success(props, transferee_channel, target_channel);
  2103. ast_channel_cleanup(transferee_channel);
  2104. ast_channel_cleanup(target_channel);
  2105. return 0;
  2106. }
  2107. static int blond_enter(struct attended_transfer_properties *props)
  2108. {
  2109. struct ast_channel *transferee_channel;
  2110. struct ast_channel *target_channel;
  2111. get_transfer_parties(props->transferer, props->transferee_bridge, props->target_bridge,
  2112. &transferee_channel, &target_channel);
  2113. bridge_merge(props->transferee_bridge, props->target_bridge, &props->transferer, 1);
  2114. ringing(props->transfer_target);
  2115. publish_transfer_success(props, transferee_channel, target_channel);
  2116. ast_channel_cleanup(transferee_channel);
  2117. ast_channel_cleanup(target_channel);
  2118. return 0;
  2119. }
  2120. static int blond_nonfinal_enter(struct attended_transfer_properties *props)
  2121. {
  2122. int res;
  2123. props->superstate = SUPERSTATE_RECALL;
  2124. /* move the transfer target to the recall target along with its reference */
  2125. props->recall_target = ast_channel_ref(props->transfer_target);
  2126. res = blond_enter(props);
  2127. props->transfer_target = ast_channel_unref(props->transfer_target);
  2128. return res;
  2129. }
  2130. static enum attended_transfer_state blond_nonfinal_exit(struct attended_transfer_properties *props,
  2131. enum attended_transfer_stimulus stimulus)
  2132. {
  2133. switch (stimulus) {
  2134. case STIMULUS_TRANSFEREE_HANGUP:
  2135. return TRANSFER_FAIL;
  2136. case STIMULUS_RECALL_TARGET_ANSWER:
  2137. return TRANSFER_RESUME;
  2138. case STIMULUS_TIMEOUT:
  2139. ast_softhangup(props->recall_target, AST_SOFTHANGUP_EXPLICIT);
  2140. /* It is possible before we hung them up that they queued up a recall target answer
  2141. * so we remove it if present as it should not exist.
  2142. */
  2143. remove_attended_transfer_stimulus(props, STIMULUS_RECALL_TARGET_ANSWER);
  2144. case STIMULUS_RECALL_TARGET_HANGUP:
  2145. props->recall_target = ast_channel_unref(props->recall_target);
  2146. return TRANSFER_RECALLING;
  2147. case STIMULUS_NONE:
  2148. case STIMULUS_DTMF_ATXFER_ABORT:
  2149. case STIMULUS_DTMF_ATXFER_COMPLETE:
  2150. case STIMULUS_DTMF_ATXFER_THREEWAY:
  2151. case STIMULUS_DTMF_ATXFER_SWAP:
  2152. case STIMULUS_TRANSFERER_HANGUP:
  2153. case STIMULUS_TRANSFER_TARGET_HANGUP:
  2154. case STIMULUS_TRANSFER_TARGET_ANSWER:
  2155. default:
  2156. ast_log(LOG_WARNING, "Unexpected stimulus '%s' received in attended transfer state '%s'\n",
  2157. stimulus_strs[stimulus], state_properties[props->state].state_name);
  2158. return props->state;
  2159. }
  2160. }
  2161. /*!
  2162. * \brief Dial callback when attempting to recall the original transferer channel
  2163. *
  2164. * This is how we can monitor if the recall target has answered or has hung up.
  2165. * If one of the two is detected, then an appropriate stimulus is sent to the
  2166. * attended transfer monitor thread.
  2167. */
  2168. static void recall_callback(struct ast_dial *dial)
  2169. {
  2170. struct attended_transfer_properties *props = ast_dial_get_user_data(dial);
  2171. switch (ast_dial_state(dial)) {
  2172. default:
  2173. case AST_DIAL_RESULT_INVALID:
  2174. case AST_DIAL_RESULT_FAILED:
  2175. case AST_DIAL_RESULT_TIMEOUT:
  2176. case AST_DIAL_RESULT_HANGUP:
  2177. case AST_DIAL_RESULT_UNANSWERED:
  2178. /* Failure cases */
  2179. stimulate_attended_transfer(props, STIMULUS_RECALL_TARGET_HANGUP);
  2180. break;
  2181. case AST_DIAL_RESULT_RINGING:
  2182. case AST_DIAL_RESULT_PROGRESS:
  2183. case AST_DIAL_RESULT_PROCEEDING:
  2184. case AST_DIAL_RESULT_TRYING:
  2185. /* Don't care about these cases */
  2186. break;
  2187. case AST_DIAL_RESULT_ANSWERED:
  2188. /* We struck gold! */
  2189. props->recall_target = ast_dial_answered_steal(dial);
  2190. stimulate_attended_transfer(props, STIMULUS_RECALL_TARGET_ANSWER);
  2191. break;
  2192. }
  2193. }
  2194. /*!
  2195. * \internal
  2196. * \brief Setup common things to transferrer and transfer_target recall channels.
  2197. *
  2198. * \param recall Channel for recalling a party.
  2199. * \param transferer Channel supplying recall information.
  2200. *
  2201. * \details
  2202. * Setup callid, variables, datastores, accountcode, and peeraccount.
  2203. *
  2204. * \pre Both channels are locked on entry.
  2205. *
  2206. * \pre COLP and CLID on the recall channel are setup by the caller but not
  2207. * explicitly published yet.
  2208. *
  2209. * \return Nothing
  2210. */
  2211. static void common_recall_channel_setup(struct ast_channel *recall, struct ast_channel *transferer)
  2212. {
  2213. struct ast_callid *callid;
  2214. callid = ast_read_threadstorage_callid();
  2215. if (callid) {
  2216. ast_channel_callid_set(recall, callid);
  2217. ast_callid_unref(callid);
  2218. }
  2219. ast_channel_inherit_variables(transferer, recall);
  2220. ast_channel_datastore_inherit(transferer, recall);
  2221. /*
  2222. * Stage a snapshot to ensure that a snapshot is always done
  2223. * on the recall channel so earler COLP and CLID setup will
  2224. * get published.
  2225. */
  2226. ast_channel_stage_snapshot(recall);
  2227. ast_channel_req_accountcodes(recall, transferer, AST_CHANNEL_REQUESTOR_REPLACEMENT);
  2228. ast_channel_stage_snapshot_done(recall);
  2229. }
  2230. static int recalling_enter(struct attended_transfer_properties *props)
  2231. {
  2232. RAII_VAR(struct ast_format_cap *, cap, ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_DEFAULT), ao2_cleanup);
  2233. struct ast_channel *recall;
  2234. if (!cap) {
  2235. return -1;
  2236. }
  2237. ast_format_cap_append(cap, ast_format_slin, 0);
  2238. /* When we dial the transfer target, since we are communicating
  2239. * with a local channel, we can place the local channel in a bridge
  2240. * and then call out to it. When recalling the transferer, though, we
  2241. * have to use the dialing API because the channel is not local.
  2242. */
  2243. props->dial = ast_dial_create();
  2244. if (!props->dial) {
  2245. return -1;
  2246. }
  2247. if (ast_dial_append(props->dial, props->transferer_type, props->transferer_addr, NULL)) {
  2248. return -1;
  2249. }
  2250. if (ast_dial_prerun(props->dial, NULL, cap)) {
  2251. return -1;
  2252. }
  2253. /*
  2254. * Setup callid, variables, datastores, accountcode, peeraccount,
  2255. * COLP, and CLID on the recalled transferrer.
  2256. */
  2257. recall = ast_dial_get_channel(props->dial, 0);
  2258. if (!recall) {
  2259. return -1;
  2260. }
  2261. ast_channel_lock_both(recall, props->transferer);
  2262. ast_party_caller_copy(ast_channel_caller(recall),
  2263. ast_channel_caller(props->transferer));
  2264. ast_party_connected_line_copy(ast_channel_connected(recall),
  2265. &props->original_transferer_colp);
  2266. common_recall_channel_setup(recall, props->transferer);
  2267. ast_channel_unlock(recall);
  2268. ast_channel_unlock(props->transferer);
  2269. ast_dial_set_state_callback(props->dial, recall_callback);
  2270. ao2_ref(props, +1);
  2271. ast_dial_set_user_data(props->dial, props);
  2272. if (ast_dial_run(props->dial, NULL, 1) == AST_DIAL_RESULT_FAILED) {
  2273. ao2_ref(props, -1);
  2274. return -1;
  2275. }
  2276. bridge_ringing(props->transferee_bridge);
  2277. return 0;
  2278. }
  2279. static enum attended_transfer_state recalling_exit(struct attended_transfer_properties *props,
  2280. enum attended_transfer_stimulus stimulus)
  2281. {
  2282. /* No matter what the outcome was, we need to kill off the dial */
  2283. ast_dial_join(props->dial);
  2284. ast_dial_destroy(props->dial);
  2285. props->dial = NULL;
  2286. /* This reference is the one we incremented for the dial state callback (recall_callback) to use */
  2287. ao2_ref(props, -1);
  2288. switch (stimulus) {
  2289. case STIMULUS_TRANSFEREE_HANGUP:
  2290. return TRANSFER_FAIL;
  2291. case STIMULUS_TIMEOUT:
  2292. case STIMULUS_RECALL_TARGET_HANGUP:
  2293. ++props->retry_attempts;
  2294. if (props->retry_attempts >= props->atxfercallbackretries) {
  2295. return TRANSFER_FAIL;
  2296. }
  2297. if (props->atxferloopdelay) {
  2298. return TRANSFER_WAIT_TO_RETRANSFER;
  2299. }
  2300. return TRANSFER_RETRANSFER;
  2301. case STIMULUS_RECALL_TARGET_ANSWER:
  2302. /* Setting this datastore up will allow the transferer to have all of his
  2303. * call features set up automatically when the bridge changes back to a
  2304. * normal personality
  2305. */
  2306. ast_bridge_features_ds_set(props->recall_target, &props->transferer_features);
  2307. ast_channel_ref(props->recall_target);
  2308. if (ast_bridge_impart(props->transferee_bridge, props->recall_target, NULL, NULL,
  2309. AST_BRIDGE_IMPART_CHAN_INDEPENDENT)) {
  2310. ast_hangup(props->recall_target);
  2311. ast_channel_unref(props->recall_target);
  2312. return TRANSFER_FAIL;
  2313. }
  2314. return TRANSFER_RESUME;
  2315. case STIMULUS_NONE:
  2316. case STIMULUS_DTMF_ATXFER_ABORT:
  2317. case STIMULUS_DTMF_ATXFER_COMPLETE:
  2318. case STIMULUS_DTMF_ATXFER_THREEWAY:
  2319. case STIMULUS_DTMF_ATXFER_SWAP:
  2320. case STIMULUS_TRANSFER_TARGET_HANGUP:
  2321. case STIMULUS_TRANSFER_TARGET_ANSWER:
  2322. case STIMULUS_TRANSFERER_HANGUP:
  2323. default:
  2324. ast_log(LOG_WARNING, "Unexpected stimulus '%s' received in attended transfer state '%s'\n",
  2325. stimulus_strs[stimulus], state_properties[props->state].state_name);
  2326. return props->state;
  2327. }
  2328. }
  2329. static int wait_to_retransfer_enter(struct attended_transfer_properties *props)
  2330. {
  2331. bridge_hold(props->transferee_bridge);
  2332. return 0;
  2333. }
  2334. static enum attended_transfer_state wait_to_retransfer_exit(struct attended_transfer_properties *props,
  2335. enum attended_transfer_stimulus stimulus)
  2336. {
  2337. bridge_unhold(props->transferee_bridge);
  2338. switch (stimulus) {
  2339. case STIMULUS_TRANSFEREE_HANGUP:
  2340. return TRANSFER_FAIL;
  2341. case STIMULUS_TIMEOUT:
  2342. return TRANSFER_RETRANSFER;
  2343. case STIMULUS_NONE:
  2344. case STIMULUS_DTMF_ATXFER_ABORT:
  2345. case STIMULUS_DTMF_ATXFER_COMPLETE:
  2346. case STIMULUS_DTMF_ATXFER_THREEWAY:
  2347. case STIMULUS_DTMF_ATXFER_SWAP:
  2348. case STIMULUS_TRANSFER_TARGET_HANGUP:
  2349. case STIMULUS_TRANSFER_TARGET_ANSWER:
  2350. case STIMULUS_TRANSFERER_HANGUP:
  2351. case STIMULUS_RECALL_TARGET_HANGUP:
  2352. case STIMULUS_RECALL_TARGET_ANSWER:
  2353. default:
  2354. ast_log(LOG_WARNING, "Unexpected stimulus '%s' received in attended transfer state '%s'\n",
  2355. stimulus_strs[stimulus], state_properties[props->state].state_name);
  2356. return props->state;
  2357. }
  2358. }
  2359. static int attach_framehook(struct attended_transfer_properties *props, struct ast_channel *channel);
  2360. static int retransfer_enter(struct attended_transfer_properties *props)
  2361. {
  2362. RAII_VAR(struct ast_format_cap *, cap, ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_DEFAULT), ao2_cleanup);
  2363. char destination[AST_MAX_EXTENSION + AST_MAX_CONTEXT + 2];
  2364. int cause;
  2365. if (!cap) {
  2366. return -1;
  2367. }
  2368. snprintf(destination, sizeof(destination), "%s@%s", props->exten, props->context);
  2369. ast_format_cap_append(cap, ast_format_slin, 0);
  2370. /* Get a channel that is the destination we wish to call */
  2371. props->recall_target = ast_request("Local", cap, NULL, NULL, destination, &cause);
  2372. if (!props->recall_target) {
  2373. ast_log(LOG_ERROR, "Unable to request outbound channel for recall target\n");
  2374. return -1;
  2375. }
  2376. if (attach_framehook(props, props->recall_target)) {
  2377. ast_log(LOG_ERROR, "Unable to attach framehook to recall target\n");
  2378. ast_hangup(props->recall_target);
  2379. props->recall_target = NULL;
  2380. return -1;
  2381. }
  2382. /*
  2383. * Setup callid, variables, datastores, accountcode, peeraccount,
  2384. * and COLP on the recalled transfer target.
  2385. */
  2386. ast_channel_lock_both(props->recall_target, props->transferer);
  2387. ast_party_connected_line_copy(ast_channel_connected(props->recall_target),
  2388. &props->original_transferer_colp);
  2389. ast_party_id_reset(&ast_channel_connected(props->recall_target)->priv);
  2390. common_recall_channel_setup(props->recall_target, props->recall_target);
  2391. ast_channel_unlock(props->recall_target);
  2392. ast_channel_unlock(props->transferer);
  2393. if (ast_call(props->recall_target, destination, 0)) {
  2394. ast_log(LOG_ERROR, "Unable to place outbound call to recall target\n");
  2395. ast_hangup(props->recall_target);
  2396. props->recall_target = NULL;
  2397. return -1;
  2398. }
  2399. ast_channel_ref(props->recall_target);
  2400. if (ast_bridge_impart(props->transferee_bridge, props->recall_target, NULL, NULL,
  2401. AST_BRIDGE_IMPART_CHAN_INDEPENDENT)) {
  2402. ast_log(LOG_ERROR, "Unable to place recall target into bridge\n");
  2403. ast_hangup(props->recall_target);
  2404. ast_channel_unref(props->recall_target);
  2405. return -1;
  2406. }
  2407. return 0;
  2408. }
  2409. static enum attended_transfer_state retransfer_exit(struct attended_transfer_properties *props,
  2410. enum attended_transfer_stimulus stimulus)
  2411. {
  2412. switch (stimulus) {
  2413. case STIMULUS_TRANSFEREE_HANGUP:
  2414. return TRANSFER_FAIL;
  2415. case STIMULUS_TIMEOUT:
  2416. ast_softhangup(props->recall_target, AST_SOFTHANGUP_EXPLICIT);
  2417. case STIMULUS_RECALL_TARGET_HANGUP:
  2418. props->recall_target = ast_channel_unref(props->recall_target);
  2419. if (props->atxferloopdelay) {
  2420. return TRANSFER_WAIT_TO_RECALL;
  2421. }
  2422. return TRANSFER_RECALLING;
  2423. case STIMULUS_RECALL_TARGET_ANSWER:
  2424. return TRANSFER_RESUME;
  2425. case STIMULUS_NONE:
  2426. case STIMULUS_DTMF_ATXFER_ABORT:
  2427. case STIMULUS_DTMF_ATXFER_COMPLETE:
  2428. case STIMULUS_DTMF_ATXFER_THREEWAY:
  2429. case STIMULUS_DTMF_ATXFER_SWAP:
  2430. case STIMULUS_TRANSFER_TARGET_HANGUP:
  2431. case STIMULUS_TRANSFER_TARGET_ANSWER:
  2432. case STIMULUS_TRANSFERER_HANGUP:
  2433. default:
  2434. ast_log(LOG_WARNING, "Unexpected stimulus '%s' received in attended transfer state '%s'\n",
  2435. stimulus_strs[stimulus], state_properties[props->state].state_name);
  2436. return props->state;
  2437. }
  2438. }
  2439. static int wait_to_recall_enter(struct attended_transfer_properties *props)
  2440. {
  2441. bridge_hold(props->transferee_bridge);
  2442. return 0;
  2443. }
  2444. static enum attended_transfer_state wait_to_recall_exit(struct attended_transfer_properties *props,
  2445. enum attended_transfer_stimulus stimulus)
  2446. {
  2447. bridge_unhold(props->transferee_bridge);
  2448. switch (stimulus) {
  2449. case STIMULUS_TRANSFEREE_HANGUP:
  2450. return TRANSFER_FAIL;
  2451. case STIMULUS_TIMEOUT:
  2452. return TRANSFER_RECALLING;
  2453. case STIMULUS_NONE:
  2454. case STIMULUS_DTMF_ATXFER_ABORT:
  2455. case STIMULUS_DTMF_ATXFER_COMPLETE:
  2456. case STIMULUS_DTMF_ATXFER_THREEWAY:
  2457. case STIMULUS_DTMF_ATXFER_SWAP:
  2458. case STIMULUS_TRANSFER_TARGET_HANGUP:
  2459. case STIMULUS_TRANSFER_TARGET_ANSWER:
  2460. case STIMULUS_TRANSFERER_HANGUP:
  2461. case STIMULUS_RECALL_TARGET_HANGUP:
  2462. case STIMULUS_RECALL_TARGET_ANSWER:
  2463. default:
  2464. ast_log(LOG_WARNING, "Unexpected stimulus '%s' received in attended transfer state '%s'\n",
  2465. stimulus_strs[stimulus], state_properties[props->state].state_name);
  2466. return props->state;
  2467. }
  2468. }
  2469. static int fail_enter(struct attended_transfer_properties *props)
  2470. {
  2471. if (props->transferee_bridge) {
  2472. ast_bridge_destroy(props->transferee_bridge, 0);
  2473. props->transferee_bridge = NULL;
  2474. }
  2475. return 0;
  2476. }
  2477. /*!
  2478. * \brief DTMF hook when transferer presses abort sequence.
  2479. *
  2480. * Sends a stimulus to the attended transfer monitor thread that the abort sequence has been pressed
  2481. */
  2482. static int atxfer_abort(struct ast_bridge_channel *bridge_channel, void *hook_pvt)
  2483. {
  2484. struct attended_transfer_properties *props = hook_pvt;
  2485. ast_debug(1, "Transferer on attended transfer %p pressed abort sequence\n", props);
  2486. stimulate_attended_transfer(props, STIMULUS_DTMF_ATXFER_ABORT);
  2487. return 0;
  2488. }
  2489. /*!
  2490. * \brief DTMF hook when transferer presses complete sequence.
  2491. *
  2492. * Sends a stimulus to the attended transfer monitor thread that the complete sequence has been pressed
  2493. */
  2494. static int atxfer_complete(struct ast_bridge_channel *bridge_channel, void *hook_pvt)
  2495. {
  2496. struct attended_transfer_properties *props = hook_pvt;
  2497. ast_debug(1, "Transferer on attended transfer %p pressed complete sequence\n", props);
  2498. stimulate_attended_transfer(props, STIMULUS_DTMF_ATXFER_COMPLETE);
  2499. return 0;
  2500. }
  2501. /*!
  2502. * \brief DTMF hook when transferer presses threeway sequence.
  2503. *
  2504. * Sends a stimulus to the attended transfer monitor thread that the threeway sequence has been pressed
  2505. */
  2506. static int atxfer_threeway(struct ast_bridge_channel *bridge_channel, void *hook_pvt)
  2507. {
  2508. struct attended_transfer_properties *props = hook_pvt;
  2509. ast_debug(1, "Transferer on attended transfer %p pressed threeway sequence\n", props);
  2510. stimulate_attended_transfer(props, STIMULUS_DTMF_ATXFER_THREEWAY);
  2511. return 0;
  2512. }
  2513. /*!
  2514. * \brief DTMF hook when transferer presses swap sequence.
  2515. *
  2516. * Sends a stimulus to the attended transfer monitor thread that the swap sequence has been pressed
  2517. */
  2518. static int atxfer_swap(struct ast_bridge_channel *bridge_channel, void *hook_pvt)
  2519. {
  2520. struct attended_transfer_properties *props = hook_pvt;
  2521. ast_debug(1, "Transferer on attended transfer %p pressed swap sequence\n", props);
  2522. stimulate_attended_transfer(props, STIMULUS_DTMF_ATXFER_SWAP);
  2523. return 0;
  2524. }
  2525. /*!
  2526. * \brief Hangup hook for transferer channel.
  2527. *
  2528. * Sends a stimulus to the attended transfer monitor thread that the transferer has hung up.
  2529. */
  2530. static int atxfer_transferer_hangup(struct ast_bridge_channel *bridge_channel, void *hook_pvt)
  2531. {
  2532. struct attended_transfer_properties *props = hook_pvt;
  2533. ast_debug(1, "Transferer on attended transfer %p hung up\n", props);
  2534. stimulate_attended_transfer(props, STIMULUS_TRANSFERER_HANGUP);
  2535. return 0;
  2536. }
  2537. /*!
  2538. * \brief Frame hook for transfer target channel
  2539. *
  2540. * This is used to determine if the transfer target or recall target has answered
  2541. * the outgoing call.
  2542. *
  2543. * When an answer is detected, a stimulus is sent to the attended transfer monitor
  2544. * thread to indicate that the transfer target or recall target has answered.
  2545. *
  2546. * \param chan The channel the framehook is attached to.
  2547. * \param frame The frame being read or written.
  2548. * \param event What is being done with the frame.
  2549. * \param data The attended transfer properties.
  2550. */
  2551. static struct ast_frame *transfer_target_framehook_cb(struct ast_channel *chan,
  2552. struct ast_frame *frame, enum ast_framehook_event event, void *data)
  2553. {
  2554. struct attended_transfer_properties *props = data;
  2555. if (event == AST_FRAMEHOOK_EVENT_READ &&
  2556. frame && frame->frametype == AST_FRAME_CONTROL &&
  2557. frame->subclass.integer == AST_CONTROL_ANSWER &&
  2558. !ast_check_hangup(chan)) {
  2559. ast_debug(1, "Detected an answer for recall attempt on attended transfer %p\n", props);
  2560. if (props->superstate == SUPERSTATE_TRANSFER) {
  2561. stimulate_attended_transfer(props, STIMULUS_TRANSFER_TARGET_ANSWER);
  2562. } else {
  2563. stimulate_attended_transfer(props, STIMULUS_RECALL_TARGET_ANSWER);
  2564. }
  2565. ast_framehook_detach(chan, props->target_framehook_id);
  2566. props->target_framehook_id = -1;
  2567. }
  2568. return frame;
  2569. }
  2570. /*! \brief Callback function which informs upstream if we are consuming a frame of a specific type */
  2571. static int transfer_target_framehook_consume(void *data, enum ast_frame_type type)
  2572. {
  2573. return (type == AST_FRAME_CONTROL ? 1 : 0);
  2574. }
  2575. static void transfer_target_framehook_destroy_cb(void *data)
  2576. {
  2577. struct attended_transfer_properties *props = data;
  2578. ao2_cleanup(props);
  2579. }
  2580. static int bridge_personality_atxfer_push(struct ast_bridge *self, struct ast_bridge_channel *bridge_channel, struct ast_bridge_channel *swap)
  2581. {
  2582. const char *abort_dtmf;
  2583. const char *complete_dtmf;
  2584. const char *threeway_dtmf;
  2585. const char *swap_dtmf;
  2586. struct bridge_basic_personality *personality = self->personality;
  2587. if (!ast_channel_has_role(bridge_channel->chan, AST_TRANSFERER_ROLE_NAME)) {
  2588. return 0;
  2589. }
  2590. abort_dtmf = ast_channel_get_role_option(bridge_channel->chan, AST_TRANSFERER_ROLE_NAME, "abort");
  2591. complete_dtmf = ast_channel_get_role_option(bridge_channel->chan, AST_TRANSFERER_ROLE_NAME, "complete");
  2592. threeway_dtmf = ast_channel_get_role_option(bridge_channel->chan, AST_TRANSFERER_ROLE_NAME, "threeway");
  2593. swap_dtmf = ast_channel_get_role_option(bridge_channel->chan, AST_TRANSFERER_ROLE_NAME, "swap");
  2594. if (!ast_strlen_zero(abort_dtmf) && ast_bridge_dtmf_hook(bridge_channel->features,
  2595. abort_dtmf, atxfer_abort, personality->details[personality->current].pvt, NULL,
  2596. AST_BRIDGE_HOOK_REMOVE_ON_PERSONALITY_CHANGE | AST_BRIDGE_HOOK_REMOVE_ON_PULL)) {
  2597. return -1;
  2598. }
  2599. if (!ast_strlen_zero(complete_dtmf) && ast_bridge_dtmf_hook(bridge_channel->features,
  2600. complete_dtmf, atxfer_complete, personality->details[personality->current].pvt, NULL,
  2601. AST_BRIDGE_HOOK_REMOVE_ON_PERSONALITY_CHANGE | AST_BRIDGE_HOOK_REMOVE_ON_PULL)) {
  2602. return -1;
  2603. }
  2604. if (!ast_strlen_zero(threeway_dtmf) && ast_bridge_dtmf_hook(bridge_channel->features,
  2605. threeway_dtmf, atxfer_threeway, personality->details[personality->current].pvt, NULL,
  2606. AST_BRIDGE_HOOK_REMOVE_ON_PERSONALITY_CHANGE | AST_BRIDGE_HOOK_REMOVE_ON_PULL)) {
  2607. return -1;
  2608. }
  2609. if (!ast_strlen_zero(swap_dtmf) && ast_bridge_dtmf_hook(bridge_channel->features,
  2610. swap_dtmf, atxfer_swap, personality->details[personality->current].pvt, NULL,
  2611. AST_BRIDGE_HOOK_REMOVE_ON_PERSONALITY_CHANGE | AST_BRIDGE_HOOK_REMOVE_ON_PULL)) {
  2612. return -1;
  2613. }
  2614. if (ast_bridge_hangup_hook(bridge_channel->features, atxfer_transferer_hangup,
  2615. personality->details[personality->current].pvt, NULL,
  2616. AST_BRIDGE_HOOK_REMOVE_ON_PERSONALITY_CHANGE | AST_BRIDGE_HOOK_REMOVE_ON_PULL)) {
  2617. return -1;
  2618. }
  2619. return 0;
  2620. }
  2621. static void transfer_pull(struct ast_bridge *self, struct ast_bridge_channel *bridge_channel, struct attended_transfer_properties *props)
  2622. {
  2623. if (self->num_channels > 1 || bridge_channel->state == BRIDGE_CHANNEL_STATE_WAIT) {
  2624. return;
  2625. }
  2626. if (self->num_channels == 1) {
  2627. struct ast_bridge_channel *transferer_bridge_channel;
  2628. int not_transferer;
  2629. ast_channel_lock(props->transferer);
  2630. transferer_bridge_channel = ast_channel_get_bridge_channel(props->transferer);
  2631. ast_channel_unlock(props->transferer);
  2632. if (!transferer_bridge_channel) {
  2633. return;
  2634. }
  2635. not_transferer = AST_LIST_FIRST(&self->channels) != transferer_bridge_channel;
  2636. ao2_ref(transferer_bridge_channel, -1);
  2637. if (not_transferer) {
  2638. return;
  2639. }
  2640. }
  2641. /* Reaching this point means that either
  2642. * 1) The bridge has no channels in it
  2643. * 2) The bridge has one channel, and it's the transferer
  2644. * In either case, it indicates that the non-transferer parties
  2645. * are no longer in the bridge.
  2646. */
  2647. if (self == props->transferee_bridge) {
  2648. stimulate_attended_transfer(props, STIMULUS_TRANSFEREE_HANGUP);
  2649. } else {
  2650. stimulate_attended_transfer(props, STIMULUS_TRANSFER_TARGET_HANGUP);
  2651. }
  2652. }
  2653. static void recall_pull(struct ast_bridge *self, struct ast_bridge_channel *bridge_channel, struct attended_transfer_properties *props)
  2654. {
  2655. if (self == props->target_bridge) {
  2656. /* Once we're in the recall superstate, we no longer care about this bridge */
  2657. return;
  2658. }
  2659. if (bridge_channel->chan == props->recall_target) {
  2660. stimulate_attended_transfer(props, STIMULUS_RECALL_TARGET_HANGUP);
  2661. return;
  2662. }
  2663. if (self->num_channels == 0) {
  2664. /* Empty bridge means all transferees are gone for sure */
  2665. stimulate_attended_transfer(props, STIMULUS_TRANSFEREE_HANGUP);
  2666. return;
  2667. }
  2668. if (self->num_channels == 1) {
  2669. struct ast_bridge_channel *target_bridge_channel;
  2670. if (!props->recall_target) {
  2671. /* No recall target means that the pull happened on a transferee. If there's still
  2672. * a channel left in the bridge, we don't need to send a stimulus
  2673. */
  2674. return;
  2675. }
  2676. ast_channel_lock(props->recall_target);
  2677. target_bridge_channel = ast_channel_get_bridge_channel(props->recall_target);
  2678. ast_channel_unlock(props->recall_target);
  2679. if (target_bridge_channel) {
  2680. if (AST_LIST_FIRST(&self->channels) == target_bridge_channel) {
  2681. stimulate_attended_transfer(props, STIMULUS_TRANSFEREE_HANGUP);
  2682. }
  2683. ao2_ref(target_bridge_channel, -1);
  2684. }
  2685. }
  2686. }
  2687. static void bridge_personality_atxfer_pull(struct ast_bridge *self, struct ast_bridge_channel *bridge_channel)
  2688. {
  2689. struct bridge_basic_personality *personality = self->personality;
  2690. struct attended_transfer_properties *props = personality->details[personality->current].pvt;
  2691. switch (props->superstate) {
  2692. case SUPERSTATE_TRANSFER:
  2693. transfer_pull(self, bridge_channel, props);
  2694. break;
  2695. case SUPERSTATE_RECALL:
  2696. recall_pull(self, bridge_channel, props);
  2697. break;
  2698. }
  2699. }
  2700. static enum attended_transfer_stimulus wait_for_stimulus(struct attended_transfer_properties *props)
  2701. {
  2702. enum attended_transfer_stimulus stimulus;
  2703. struct stimulus_list *list;
  2704. SCOPED_MUTEX(lock, ao2_object_get_lockaddr(props));
  2705. while (!(list = AST_LIST_REMOVE_HEAD(&props->stimulus_queue, next))) {
  2706. if (!(state_properties[props->state].flags & TRANSFER_STATE_FLAG_TIMED)) {
  2707. ast_cond_wait(&props->cond, lock);
  2708. } else {
  2709. struct timeval relative_timeout = { 0, };
  2710. struct timeval absolute_timeout;
  2711. struct timespec timeout_arg;
  2712. if (state_properties[props->state].flags & TRANSFER_STATE_FLAG_TIMER_RESET) {
  2713. props->start = ast_tvnow();
  2714. }
  2715. if (state_properties[props->state].flags & TRANSFER_STATE_FLAG_TIMER_LOOP_DELAY) {
  2716. relative_timeout.tv_sec = props->atxferloopdelay;
  2717. } else {
  2718. /* Implied TRANSFER_STATE_FLAG_TIMER_ATXFER_NO_ANSWER */
  2719. relative_timeout.tv_sec = props->atxfernoanswertimeout;
  2720. }
  2721. absolute_timeout = ast_tvadd(props->start, relative_timeout);
  2722. timeout_arg.tv_sec = absolute_timeout.tv_sec;
  2723. timeout_arg.tv_nsec = absolute_timeout.tv_usec * 1000;
  2724. if (ast_cond_timedwait(&props->cond, lock, &timeout_arg) == ETIMEDOUT) {
  2725. return STIMULUS_TIMEOUT;
  2726. }
  2727. }
  2728. }
  2729. stimulus = list->stimulus;
  2730. ast_free(list);
  2731. return stimulus;
  2732. }
  2733. /*!
  2734. * \brief The main loop for the attended transfer monitor thread.
  2735. *
  2736. * This loop runs continuously until the attended transfer reaches
  2737. * a terminal state. Stimuli for changes in the attended transfer
  2738. * state are handled in this thread so that all factors in an
  2739. * attended transfer can be handled in an orderly fashion.
  2740. *
  2741. * \param data The attended transfer properties
  2742. */
  2743. static void *attended_transfer_monitor_thread(void *data)
  2744. {
  2745. struct attended_transfer_properties *props = data;
  2746. struct ast_callid *callid;
  2747. /*
  2748. * Set thread callid to the transferer's callid because we
  2749. * are doing all this on that channel's behalf.
  2750. */
  2751. ast_channel_lock(props->transferer);
  2752. callid = ast_channel_callid(props->transferer);
  2753. ast_channel_unlock(props->transferer);
  2754. if (callid) {
  2755. ast_callid_threadassoc_add(callid);
  2756. }
  2757. for (;;) {
  2758. enum attended_transfer_stimulus stimulus;
  2759. ast_debug(1, "About to enter state %s for attended transfer %p\n", state_properties[props->state].state_name, props);
  2760. if (state_properties[props->state].enter &&
  2761. state_properties[props->state].enter(props)) {
  2762. ast_log(LOG_ERROR, "State %s enter function returned an error for attended transfer %p\n",
  2763. state_properties[props->state].state_name, props);
  2764. break;
  2765. }
  2766. if (state_properties[props->state].flags & TRANSFER_STATE_FLAG_TERMINAL) {
  2767. ast_debug(1, "State %s is a terminal state. Ending attended transfer %p\n",
  2768. state_properties[props->state].state_name, props);
  2769. break;
  2770. }
  2771. stimulus = wait_for_stimulus(props);
  2772. ast_debug(1, "Received stimulus %s on attended transfer %p\n", stimulus_strs[stimulus], props);
  2773. ast_assert(state_properties[props->state].exit != NULL);
  2774. props->state = state_properties[props->state].exit(props, stimulus);
  2775. ast_debug(1, "Told to enter state %s exit on attended transfer %p\n", state_properties[props->state].state_name, props);
  2776. }
  2777. attended_transfer_properties_shutdown(props);
  2778. if (callid) {
  2779. ast_callid_unref(callid);
  2780. ast_callid_threadassoc_remove();
  2781. }
  2782. return NULL;
  2783. }
  2784. static int attach_framehook(struct attended_transfer_properties *props, struct ast_channel *channel)
  2785. {
  2786. struct ast_framehook_interface target_interface = {
  2787. .version = AST_FRAMEHOOK_INTERFACE_VERSION,
  2788. .event_cb = transfer_target_framehook_cb,
  2789. .destroy_cb = transfer_target_framehook_destroy_cb,
  2790. .consume_cb = transfer_target_framehook_consume,
  2791. .disable_inheritance = 1,
  2792. };
  2793. ao2_ref(props, +1);
  2794. target_interface.data = props;
  2795. ast_channel_lock(channel);
  2796. props->target_framehook_id = ast_framehook_attach(channel, &target_interface);
  2797. ast_channel_unlock(channel);
  2798. if (props->target_framehook_id == -1) {
  2799. ao2_ref(props, -1);
  2800. return -1;
  2801. }
  2802. return 0;
  2803. }
  2804. static int add_transferer_role(struct ast_channel *chan, struct ast_bridge_features_attended_transfer *attended_transfer)
  2805. {
  2806. const char *atxfer_abort;
  2807. const char *atxfer_threeway;
  2808. const char *atxfer_complete;
  2809. const char *atxfer_swap;
  2810. struct ast_features_xfer_config *xfer_cfg;
  2811. SCOPED_CHANNELLOCK(lock, chan);
  2812. xfer_cfg = ast_get_chan_features_xfer_config(chan);
  2813. if (!xfer_cfg) {
  2814. return -1;
  2815. }
  2816. if (attended_transfer) {
  2817. atxfer_abort = ast_strdupa(S_OR(attended_transfer->abort, xfer_cfg->atxferabort));
  2818. atxfer_threeway = ast_strdupa(S_OR(attended_transfer->threeway, xfer_cfg->atxferthreeway));
  2819. atxfer_complete = ast_strdupa(S_OR(attended_transfer->complete, xfer_cfg->atxfercomplete));
  2820. atxfer_swap = ast_strdupa(S_OR(attended_transfer->swap, xfer_cfg->atxferswap));
  2821. } else {
  2822. atxfer_abort = ast_strdupa(xfer_cfg->atxferabort);
  2823. atxfer_threeway = ast_strdupa(xfer_cfg->atxferthreeway);
  2824. atxfer_complete = ast_strdupa(xfer_cfg->atxfercomplete);
  2825. atxfer_swap = ast_strdupa(xfer_cfg->atxferswap);
  2826. }
  2827. ao2_ref(xfer_cfg, -1);
  2828. return ast_channel_add_bridge_role(chan, AST_TRANSFERER_ROLE_NAME) ||
  2829. ast_channel_set_bridge_role_option(chan, AST_TRANSFERER_ROLE_NAME, "abort", atxfer_abort) ||
  2830. ast_channel_set_bridge_role_option(chan, AST_TRANSFERER_ROLE_NAME, "complete", atxfer_complete) ||
  2831. ast_channel_set_bridge_role_option(chan, AST_TRANSFERER_ROLE_NAME, "threeway", atxfer_threeway) ||
  2832. ast_channel_set_bridge_role_option(chan, AST_TRANSFERER_ROLE_NAME, "swap", atxfer_swap);
  2833. }
  2834. /*!
  2835. * \brief Helper function that presents dialtone and grabs extension
  2836. *
  2837. * \retval 0 on success
  2838. * \retval -1 on failure
  2839. */
  2840. static int grab_transfer(struct ast_channel *chan, char *exten, size_t exten_len, const char *context)
  2841. {
  2842. int res;
  2843. int digit_timeout;
  2844. int attempts = 0;
  2845. int max_attempts;
  2846. struct ast_features_xfer_config *xfer_cfg;
  2847. char *retry_sound;
  2848. char *invalid_sound;
  2849. ast_channel_lock(chan);
  2850. xfer_cfg = ast_get_chan_features_xfer_config(chan);
  2851. if (!xfer_cfg) {
  2852. ast_log(LOG_ERROR, "Channel %s: Unable to get transfer configuration\n",
  2853. ast_channel_name(chan));
  2854. ast_channel_unlock(chan);
  2855. return -1;
  2856. }
  2857. digit_timeout = xfer_cfg->transferdigittimeout * 1000;
  2858. max_attempts = xfer_cfg->transferdialattempts;
  2859. retry_sound = ast_strdupa(xfer_cfg->transferretrysound);
  2860. invalid_sound = ast_strdupa(xfer_cfg->transferinvalidsound);
  2861. ao2_ref(xfer_cfg, -1);
  2862. ast_channel_unlock(chan);
  2863. /* Play the simple "transfer" prompt out and wait */
  2864. res = ast_stream_and_wait(chan, "pbx-transfer", AST_DIGIT_ANY);
  2865. ast_stopstream(chan);
  2866. if (res < 0) {
  2867. /* Hangup or error */
  2868. return -1;
  2869. }
  2870. if (res) {
  2871. /* Store the DTMF digit that interrupted playback of the file. */
  2872. exten[0] = res;
  2873. }
  2874. /* Drop to dialtone so they can enter the extension they want to transfer to */
  2875. do {
  2876. ++attempts;
  2877. ast_test_suite_event_notify("TRANSFER_BEGIN_DIAL",
  2878. "Channel: %s\r\n"
  2879. "Attempt: %d",
  2880. ast_channel_name(chan), attempts);
  2881. res = ast_app_dtget(chan, context, exten, exten_len, exten_len - 1, digit_timeout);
  2882. ast_test_suite_event_notify("TRANSFER_DIALLED",
  2883. "Channel: %s\r\n"
  2884. "Attempt: %d\r\n"
  2885. "Dialled: %s\r\n"
  2886. "Result: %s",
  2887. ast_channel_name(chan), attempts, exten, res > 0 ? "Success" : "Failure");
  2888. if (res < 0) {
  2889. /* Hangup or error */
  2890. res = -1;
  2891. } else if (!res) {
  2892. /* 0 for invalid extension dialed. */
  2893. if (ast_strlen_zero(exten)) {
  2894. ast_verb(3, "Channel %s: Dialed no digits.\n", ast_channel_name(chan));
  2895. } else {
  2896. ast_verb(3, "Channel %s: Dialed '%s@%s' does not exist.\n",
  2897. ast_channel_name(chan), exten, context);
  2898. }
  2899. if (attempts < max_attempts) {
  2900. ast_stream_and_wait(chan, retry_sound, AST_DIGIT_NONE);
  2901. } else {
  2902. ast_stream_and_wait(chan, invalid_sound, AST_DIGIT_NONE);
  2903. }
  2904. memset(exten, 0, exten_len);
  2905. res = 1;
  2906. } else {
  2907. /* Dialed extension is valid. */
  2908. res = 0;
  2909. }
  2910. } while (res > 0 && attempts < max_attempts);
  2911. ast_test_suite_event_notify("TRANSFER_DIAL_FINAL",
  2912. "Channel: %s\r\n"
  2913. "Result: %s",
  2914. ast_channel_name(chan), res == 0 ? "Success" : "Failure");
  2915. return res ? -1 : 0;
  2916. }
  2917. static void copy_caller_data(struct ast_channel *dest, struct ast_channel *caller)
  2918. {
  2919. ast_channel_lock_both(caller, dest);
  2920. ast_connected_line_copy_from_caller(ast_channel_connected(dest), ast_channel_caller(caller));
  2921. ast_channel_inherit_variables(caller, dest);
  2922. ast_channel_datastore_inherit(caller, dest);
  2923. ast_channel_unlock(dest);
  2924. ast_channel_unlock(caller);
  2925. }
  2926. /*! \brief Helper function that creates an outgoing channel and returns it immediately */
  2927. static struct ast_channel *dial_transfer(struct ast_channel *caller, const char *destination)
  2928. {
  2929. struct ast_channel *chan;
  2930. int cause;
  2931. /* Now we request a local channel to prepare to call the destination */
  2932. chan = ast_request("Local", ast_channel_nativeformats(caller), NULL, caller, destination,
  2933. &cause);
  2934. if (!chan) {
  2935. return NULL;
  2936. }
  2937. ast_channel_lock_both(chan, caller);
  2938. ast_channel_req_accountcodes(chan, caller, AST_CHANNEL_REQUESTOR_BRIDGE_PEER);
  2939. /* Who is transferring the call. */
  2940. pbx_builtin_setvar_helper(chan, "TRANSFERERNAME", ast_channel_name(caller));
  2941. ast_bridge_set_transfer_variables(chan, ast_channel_name(caller), 1);
  2942. ast_channel_unlock(chan);
  2943. ast_channel_unlock(caller);
  2944. /* Before we actually dial out let's inherit appropriate information. */
  2945. copy_caller_data(chan, caller);
  2946. return chan;
  2947. }
  2948. /*!
  2949. * \brief Internal built in feature for attended transfers
  2950. *
  2951. * This hook will set up a thread for monitoring the progress of
  2952. * an attended transfer. For more information about attended transfer
  2953. * progress, see documentation on the transfer state machine.
  2954. *
  2955. * \param bridge_channel The channel that pressed the attended transfer DTMF sequence
  2956. * \param hook_pvt Structure with further information about the attended transfer
  2957. */
  2958. static int feature_attended_transfer(struct ast_bridge_channel *bridge_channel, void *hook_pvt)
  2959. {
  2960. struct ast_bridge_features_attended_transfer *attended_transfer = hook_pvt;
  2961. struct attended_transfer_properties *props;
  2962. struct ast_bridge *bridge;
  2963. char destination[AST_MAX_EXTENSION + AST_MAX_CONTEXT + 1];
  2964. char exten[AST_MAX_EXTENSION] = "";
  2965. pthread_t thread;
  2966. /* Inhibit the bridge before we do anything else. */
  2967. bridge = ast_bridge_channel_merge_inhibit(bridge_channel, +1);
  2968. ast_verb(3, "Channel %s: Started DTMF attended transfer.\n",
  2969. ast_channel_name(bridge_channel->chan));
  2970. if (strcmp(bridge->v_table->name, "basic")) {
  2971. ast_log(LOG_ERROR, "Channel %s: Attended transfer attempted on unsupported bridge type '%s'.\n",
  2972. ast_channel_name(bridge_channel->chan), bridge->v_table->name);
  2973. ast_bridge_merge_inhibit(bridge, -1);
  2974. ao2_ref(bridge, -1);
  2975. return 0;
  2976. }
  2977. /* Was the bridge inhibited before we inhibited it? */
  2978. if (1 < bridge->inhibit_merge) {
  2979. /*
  2980. * The peer likely initiated attended transfer at the same time
  2981. * and we lost the race.
  2982. */
  2983. ast_verb(3, "Channel %s: Bridge '%s' does not permit merging at this time.\n",
  2984. ast_channel_name(bridge_channel->chan), bridge->uniqueid);
  2985. ast_bridge_merge_inhibit(bridge, -1);
  2986. ao2_ref(bridge, -1);
  2987. return 0;
  2988. }
  2989. props = attended_transfer_properties_alloc(bridge_channel->chan,
  2990. attended_transfer ? attended_transfer->context : NULL);
  2991. if (!props) {
  2992. ast_log(LOG_ERROR, "Channel %s: Unable to allocate control structure for performing attended transfer.\n",
  2993. ast_channel_name(bridge_channel->chan));
  2994. ast_bridge_merge_inhibit(bridge, -1);
  2995. ao2_ref(bridge, -1);
  2996. return 0;
  2997. }
  2998. props->transferee_bridge = bridge;
  2999. if (add_transferer_role(props->transferer, attended_transfer)) {
  3000. ast_log(LOG_ERROR, "Channel %s: Unable to set transferrer bridge role.\n",
  3001. ast_channel_name(bridge_channel->chan));
  3002. attended_transfer_properties_shutdown(props);
  3003. return 0;
  3004. }
  3005. ast_bridge_channel_write_hold(bridge_channel, NULL);
  3006. /* Grab the extension to transfer to */
  3007. if (grab_transfer(bridge_channel->chan, exten, sizeof(exten), props->context)) {
  3008. /*
  3009. * XXX The warning here really should be removed. While the
  3010. * message is accurate, this is a normal exit for when the user
  3011. * fails to specify a valid transfer target. e.g., The user
  3012. * hungup, didn't dial any digits, or dialed an invalid
  3013. * extension.
  3014. */
  3015. ast_log(LOG_WARNING, "Channel %s: Unable to acquire target extension for attended transfer.\n",
  3016. ast_channel_name(bridge_channel->chan));
  3017. ast_bridge_channel_write_unhold(bridge_channel);
  3018. attended_transfer_properties_shutdown(props);
  3019. return 0;
  3020. }
  3021. ast_string_field_set(props, exten, exten);
  3022. /* Fill the variable with the extension and context we want to call */
  3023. snprintf(destination, sizeof(destination), "%s@%s", props->exten, props->context);
  3024. ast_debug(1, "Channel %s: Attended transfer target '%s'\n",
  3025. ast_channel_name(bridge_channel->chan), destination);
  3026. /* Get a channel that is the destination we wish to call */
  3027. props->transfer_target = dial_transfer(bridge_channel->chan, destination);
  3028. if (!props->transfer_target) {
  3029. ast_log(LOG_ERROR, "Channel %s: Unable to request outbound channel for attended transfer target.\n",
  3030. ast_channel_name(bridge_channel->chan));
  3031. stream_failsound(props->transferer);
  3032. ast_bridge_channel_write_unhold(bridge_channel);
  3033. attended_transfer_properties_shutdown(props);
  3034. return 0;
  3035. }
  3036. /* Create a bridge to use to talk to the person we are calling */
  3037. props->target_bridge = ast_bridge_basic_new();
  3038. if (!props->target_bridge) {
  3039. ast_log(LOG_ERROR, "Channel %s: Unable to create bridge for attended transfer target.\n",
  3040. ast_channel_name(bridge_channel->chan));
  3041. stream_failsound(props->transferer);
  3042. ast_bridge_channel_write_unhold(bridge_channel);
  3043. ast_hangup(props->transfer_target);
  3044. props->transfer_target = NULL;
  3045. attended_transfer_properties_shutdown(props);
  3046. return 0;
  3047. }
  3048. ast_bridge_merge_inhibit(props->target_bridge, +1);
  3049. if (attach_framehook(props, props->transfer_target)) {
  3050. ast_log(LOG_ERROR, "Channel %s: Unable to attach framehook to transfer target.\n",
  3051. ast_channel_name(bridge_channel->chan));
  3052. stream_failsound(props->transferer);
  3053. ast_bridge_channel_write_unhold(bridge_channel);
  3054. ast_hangup(props->transfer_target);
  3055. props->transfer_target = NULL;
  3056. attended_transfer_properties_shutdown(props);
  3057. return 0;
  3058. }
  3059. bridge_basic_change_personality(props->target_bridge,
  3060. BRIDGE_BASIC_PERSONALITY_ATXFER, props);
  3061. bridge_basic_change_personality(bridge,
  3062. BRIDGE_BASIC_PERSONALITY_ATXFER, props);
  3063. if (ast_call(props->transfer_target, destination, 0)) {
  3064. ast_log(LOG_ERROR, "Channel %s: Unable to place outbound call to transfer target.\n",
  3065. ast_channel_name(bridge_channel->chan));
  3066. stream_failsound(props->transferer);
  3067. ast_bridge_channel_write_unhold(bridge_channel);
  3068. ast_hangup(props->transfer_target);
  3069. props->transfer_target = NULL;
  3070. attended_transfer_properties_shutdown(props);
  3071. return 0;
  3072. }
  3073. /* We increase the refcount of the transfer target because ast_bridge_impart() will
  3074. * steal the reference we already have. We need to keep a reference, so the only
  3075. * choice is to give it a bump
  3076. */
  3077. ast_channel_ref(props->transfer_target);
  3078. if (ast_bridge_impart(props->target_bridge, props->transfer_target, NULL, NULL,
  3079. AST_BRIDGE_IMPART_CHAN_INDEPENDENT)) {
  3080. ast_log(LOG_ERROR, "Channel %s: Unable to place transfer target into bridge.\n",
  3081. ast_channel_name(bridge_channel->chan));
  3082. stream_failsound(props->transferer);
  3083. ast_bridge_channel_write_unhold(bridge_channel);
  3084. ast_hangup(props->transfer_target);
  3085. props->transfer_target = NULL;
  3086. attended_transfer_properties_shutdown(props);
  3087. return 0;
  3088. }
  3089. if (ast_pthread_create_detached(&thread, NULL, attended_transfer_monitor_thread, props)) {
  3090. ast_log(LOG_ERROR, "Channel %s: Unable to create monitoring thread for attended transfer.\n",
  3091. ast_channel_name(bridge_channel->chan));
  3092. stream_failsound(props->transferer);
  3093. ast_bridge_channel_write_unhold(bridge_channel);
  3094. attended_transfer_properties_shutdown(props);
  3095. return 0;
  3096. }
  3097. /* Once the monitoring thread has been created, it is responsible for destroying all
  3098. * of the necessary components.
  3099. */
  3100. return 0;
  3101. }
  3102. static void blind_transfer_cb(struct ast_channel *new_channel, struct transfer_channel_data *user_data_wrapper,
  3103. enum ast_transfer_type transfer_type)
  3104. {
  3105. struct ast_channel *transferer_channel = user_data_wrapper->data;
  3106. if (transfer_type == AST_BRIDGE_TRANSFER_MULTI_PARTY) {
  3107. copy_caller_data(new_channel, transferer_channel);
  3108. }
  3109. }
  3110. /*! \brief Internal built in feature for blind transfers */
  3111. static int feature_blind_transfer(struct ast_bridge_channel *bridge_channel, void *hook_pvt)
  3112. {
  3113. char xfer_exten[AST_MAX_EXTENSION] = "";
  3114. struct ast_bridge_features_blind_transfer *blind_transfer = hook_pvt;
  3115. const char *xfer_context;
  3116. char *goto_on_blindxfr;
  3117. ast_verb(3, "Channel %s: Started DTMF blind transfer.\n",
  3118. ast_channel_name(bridge_channel->chan));
  3119. ast_bridge_channel_write_hold(bridge_channel, NULL);
  3120. ast_channel_lock(bridge_channel->chan);
  3121. xfer_context = ast_strdupa(get_transfer_context(bridge_channel->chan,
  3122. blind_transfer ? blind_transfer->context : NULL));
  3123. goto_on_blindxfr = ast_strdupa(S_OR(pbx_builtin_getvar_helper(bridge_channel->chan,
  3124. "GOTO_ON_BLINDXFR"), ""));
  3125. ast_channel_unlock(bridge_channel->chan);
  3126. /* Grab the extension to transfer to */
  3127. if (grab_transfer(bridge_channel->chan, xfer_exten, sizeof(xfer_exten), xfer_context)) {
  3128. ast_bridge_channel_write_unhold(bridge_channel);
  3129. return 0;
  3130. }
  3131. ast_debug(1, "Channel %s: Blind transfer target '%s@%s'\n",
  3132. ast_channel_name(bridge_channel->chan), xfer_exten, xfer_context);
  3133. if (!ast_strlen_zero(goto_on_blindxfr)) {
  3134. const char *chan_context;
  3135. const char *chan_exten;
  3136. int chan_priority;
  3137. ast_debug(1, "Channel %s: After transfer, transferrer goes to %s\n",
  3138. ast_channel_name(bridge_channel->chan), goto_on_blindxfr);
  3139. ast_channel_lock(bridge_channel->chan);
  3140. chan_context = ast_strdupa(ast_channel_context(bridge_channel->chan));
  3141. chan_exten = ast_strdupa(ast_channel_exten(bridge_channel->chan));
  3142. chan_priority = ast_channel_priority(bridge_channel->chan);
  3143. ast_channel_unlock(bridge_channel->chan);
  3144. ast_bridge_set_after_go_on(bridge_channel->chan,
  3145. chan_context, chan_exten, chan_priority, goto_on_blindxfr);
  3146. }
  3147. if (ast_bridge_transfer_blind(0, bridge_channel->chan, xfer_exten, xfer_context,
  3148. blind_transfer_cb, bridge_channel->chan) != AST_BRIDGE_TRANSFER_SUCCESS
  3149. && !ast_strlen_zero(goto_on_blindxfr)) {
  3150. ast_bridge_discard_after_goto(bridge_channel->chan);
  3151. }
  3152. return 0;
  3153. }
  3154. struct ast_bridge_methods ast_bridge_basic_v_table;
  3155. struct ast_bridge_methods personality_normal_v_table;
  3156. struct ast_bridge_methods personality_atxfer_v_table;
  3157. static void bridge_basic_change_personality(struct ast_bridge *bridge,
  3158. enum bridge_basic_personality_type type, void *user_data)
  3159. {
  3160. struct bridge_basic_personality *personality = bridge->personality;
  3161. SCOPED_LOCK(lock, bridge, ast_bridge_lock, ast_bridge_unlock);
  3162. remove_hooks_on_personality_change(bridge);
  3163. ao2_cleanup(personality->details[personality->current].pvt);
  3164. personality->details[personality->current].pvt = NULL;
  3165. ast_clear_flag(&bridge->feature_flags, AST_FLAGS_ALL);
  3166. personality->current = type;
  3167. if (user_data) {
  3168. ao2_ref(user_data, +1);
  3169. }
  3170. personality->details[personality->current].pvt = user_data;
  3171. ast_set_flag(&bridge->feature_flags, personality->details[personality->current].bridge_flags);
  3172. if (personality->details[personality->current].on_personality_change) {
  3173. personality->details[personality->current].on_personality_change(bridge);
  3174. }
  3175. }
  3176. static void personality_destructor(void *obj)
  3177. {
  3178. struct bridge_basic_personality *personality = obj;
  3179. int i;
  3180. for (i = 0; i < BRIDGE_BASIC_PERSONALITY_END; ++i) {
  3181. ao2_cleanup(personality->details[i].pvt);
  3182. }
  3183. }
  3184. static void on_personality_change_normal(struct ast_bridge *bridge)
  3185. {
  3186. struct ast_bridge_channel *iter;
  3187. AST_LIST_TRAVERSE(&bridge->channels, iter, entry) {
  3188. if (add_normal_hooks(bridge, iter)) {
  3189. ast_log(LOG_WARNING, "Unable to set up bridge hooks for channel %s. Features may not work properly\n",
  3190. ast_channel_name(iter->chan));
  3191. }
  3192. }
  3193. }
  3194. static void init_details(struct personality_details *details,
  3195. enum bridge_basic_personality_type type)
  3196. {
  3197. switch (type) {
  3198. case BRIDGE_BASIC_PERSONALITY_NORMAL:
  3199. details->v_table = &personality_normal_v_table;
  3200. details->bridge_flags = NORMAL_FLAGS;
  3201. details->on_personality_change = on_personality_change_normal;
  3202. break;
  3203. case BRIDGE_BASIC_PERSONALITY_ATXFER:
  3204. details->v_table = &personality_atxfer_v_table;
  3205. details->bridge_flags = TRANSFER_FLAGS;
  3206. break;
  3207. default:
  3208. ast_log(LOG_WARNING, "Asked to initialize unexpected basic bridge personality type.\n");
  3209. break;
  3210. }
  3211. }
  3212. static struct ast_bridge *bridge_basic_personality_alloc(struct ast_bridge *bridge)
  3213. {
  3214. struct bridge_basic_personality *personality;
  3215. int i;
  3216. if (!bridge) {
  3217. return NULL;
  3218. }
  3219. personality = ao2_alloc(sizeof(*personality), personality_destructor);
  3220. if (!personality) {
  3221. ao2_ref(bridge, -1);
  3222. return NULL;
  3223. }
  3224. for (i = 0; i < BRIDGE_BASIC_PERSONALITY_END; ++i) {
  3225. init_details(&personality->details[i], i);
  3226. }
  3227. personality->current = BRIDGE_BASIC_PERSONALITY_NORMAL;
  3228. bridge->personality = personality;
  3229. return bridge;
  3230. }
  3231. struct ast_bridge *ast_bridge_basic_new(void)
  3232. {
  3233. struct ast_bridge *bridge;
  3234. bridge = bridge_alloc(sizeof(struct ast_bridge), &ast_bridge_basic_v_table);
  3235. bridge = bridge_base_init(bridge,
  3236. AST_BRIDGE_CAPABILITY_NATIVE | AST_BRIDGE_CAPABILITY_1TO1MIX
  3237. | AST_BRIDGE_CAPABILITY_MULTIMIX, NORMAL_FLAGS, NULL, NULL, NULL);
  3238. bridge = bridge_basic_personality_alloc(bridge);
  3239. bridge = bridge_register(bridge);
  3240. return bridge;
  3241. }
  3242. void ast_bridge_basic_set_flags(struct ast_bridge *bridge, unsigned int flags)
  3243. {
  3244. SCOPED_LOCK(lock, bridge, ast_bridge_lock, ast_bridge_unlock);
  3245. struct bridge_basic_personality *personality = bridge->personality;
  3246. personality->details[personality->current].bridge_flags |= flags;
  3247. ast_set_flag(&bridge->feature_flags, flags);
  3248. }
  3249. void ast_bridging_init_basic(void)
  3250. {
  3251. /* Setup bridge basic subclass v_table. */
  3252. ast_bridge_basic_v_table = ast_bridge_base_v_table;
  3253. ast_bridge_basic_v_table.name = "basic";
  3254. ast_bridge_basic_v_table.push = bridge_basic_push;
  3255. ast_bridge_basic_v_table.pull = bridge_basic_pull;
  3256. ast_bridge_basic_v_table.destroy = bridge_basic_destroy;
  3257. /*
  3258. * Personality vtables don't have the same rules as
  3259. * normal bridge vtables. These vtable functions are
  3260. * used as alterations to the ast_bridge_basic_v_table
  3261. * method functionality and are checked for NULL before
  3262. * calling.
  3263. */
  3264. personality_normal_v_table.name = "normal";
  3265. personality_normal_v_table.push = bridge_personality_normal_push;
  3266. personality_atxfer_v_table.name = "attended transfer";
  3267. personality_atxfer_v_table.push = bridge_personality_atxfer_push;
  3268. personality_atxfer_v_table.pull = bridge_personality_atxfer_pull;
  3269. ast_bridge_features_register(AST_BRIDGE_BUILTIN_ATTENDEDTRANSFER, feature_attended_transfer, NULL);
  3270. ast_bridge_features_register(AST_BRIDGE_BUILTIN_BLINDTRANSFER, feature_blind_transfer, NULL);
  3271. }