res_fax.c 153 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799
  1. /*
  2. * Asterisk -- An open source telephony toolkit.
  3. *
  4. * Copyright (C) 2008-2009, Digium, Inc.
  5. *
  6. * Dwayne M. Hubbard <dhubbard@digium.com>
  7. * Kevin P. Fleming <kpfleming@digium.com>
  8. * Matthew Nicholson <mnicholson@digium.com>
  9. *
  10. * Initial T.38-gateway code
  11. * 2008, Daniel Ferenci <daniel.ferenci@nethemba.com>
  12. * Created by Nethemba s.r.o. http://www.nethemba.com
  13. * Sponsored by IPEX a.s. http://www.ipex.cz
  14. *
  15. * T.38-gateway integration into asterisk app_fax and rework
  16. * 2008-2011, Gregory Hinton Nietsky <gregory@distrotech.co.za>
  17. * dns Telecom http://www.dnstelecom.co.za
  18. *
  19. * Modified to make T.38-gateway compatible with Asterisk 1.6.2
  20. * 2010, Anton Verevkin <mymail@verevkin.it>
  21. * ViaNetTV http://www.vianettv.com
  22. *
  23. * Modified to make T.38-gateway work
  24. * 2010, Klaus Darilion, IPCom GmbH, www.ipcom.at
  25. *
  26. * See http://www.asterisk.org for more information about
  27. * the Asterisk project. Please do not directly contact
  28. * any of the maintainers of this project for assistance;
  29. * the project provides a web site, mailing lists and IRC
  30. * channels for your use.
  31. *
  32. * This program is free software, distributed under the terms of
  33. * the GNU General Public License Version 2. See the LICENSE file
  34. * at the top of the source tree.
  35. */
  36. /*** MODULEINFO
  37. <conflict>app_fax</conflict>
  38. <support_level>core</support_level>
  39. ***/
  40. /*! \file
  41. *
  42. * \brief Generic FAX Resource for FAX technology resource modules
  43. *
  44. * \author Dwayne M. Hubbard <dhubbard@digium.com>
  45. * \author Kevin P. Fleming <kpfleming@digium.com>
  46. * \author Matthew Nicholson <mnicholson@digium.com>
  47. * \author Gregory H. Nietsky <gregory@distrotech.co.za>
  48. *
  49. * A generic FAX resource module that provides SendFAX and ReceiveFAX applications.
  50. * This module requires FAX technology modules, like res_fax_spandsp, to register with it
  51. * so it can use the technology modules to perform the actual FAX transmissions.
  52. * \ingroup applications
  53. */
  54. /*! \li \ref res_fax.c uses the configuration file \ref res_fax.conf
  55. * \addtogroup configuration_file Configuration Files
  56. */
  57. /*!
  58. * \page res_fax.conf res_fax.conf
  59. * \verbinclude res_fax.conf.sample
  60. */
  61. #include "asterisk.h"
  62. ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
  63. #include "asterisk/io.h"
  64. #include "asterisk/file.h"
  65. #include "asterisk/logger.h"
  66. #include "asterisk/module.h"
  67. #include "asterisk/app.h"
  68. #include "asterisk/lock.h"
  69. #include "asterisk/options.h"
  70. #include "asterisk/strings.h"
  71. #include "asterisk/cli.h"
  72. #include "asterisk/utils.h"
  73. #include "asterisk/config.h"
  74. #include "asterisk/astobj2.h"
  75. #include "asterisk/res_fax.h"
  76. #include "asterisk/file.h"
  77. #include "asterisk/channel.h"
  78. #include "asterisk/pbx.h"
  79. #include "asterisk/dsp.h"
  80. #include "asterisk/indications.h"
  81. #include "asterisk/ast_version.h"
  82. #include "asterisk/translate.h"
  83. #include "asterisk/stasis.h"
  84. #include "asterisk/stasis_channels.h"
  85. #include "asterisk/smoother.h"
  86. #include "asterisk/format_cache.h"
  87. /*** DOCUMENTATION
  88. <application name="ReceiveFAX" language="en_US" module="res_fax">
  89. <synopsis>
  90. Receive a FAX and save as a TIFF/F file.
  91. </synopsis>
  92. <syntax>
  93. <parameter name="filename" required="true" />
  94. <parameter name="options">
  95. <optionlist>
  96. <option name="d">
  97. <para>Enable FAX debugging.</para>
  98. </option>
  99. <option name="f">
  100. <para>Allow audio fallback FAX transfer on T.38 capable channels.</para>
  101. </option>
  102. <option name="F">
  103. <para>Force usage of audio mode on T.38 capable channels.</para>
  104. </option>
  105. <option name="s">
  106. <para>Send progress Manager events (overrides statusevents setting in res_fax.conf).</para>
  107. </option>
  108. </optionlist>
  109. </parameter>
  110. </syntax>
  111. <description>
  112. <para>This application is provided by res_fax, which is a FAX technology agnostic module
  113. that utilizes FAX technology resource modules to complete a FAX transmission.</para>
  114. <para>Session arguments can be set by the FAXOPT function and to check results of the ReceiveFax() application.</para>
  115. </description>
  116. <see-also>
  117. <ref type="function">FAXOPT</ref>
  118. </see-also>
  119. </application>
  120. <application name="SendFAX" language="en_US" module="res_fax">
  121. <synopsis>
  122. Sends a specified TIFF/F file as a FAX.
  123. </synopsis>
  124. <syntax>
  125. <parameter name="filename" required="true" argsep="&amp;">
  126. <argument name="filename2" multiple="true">
  127. <para>TIFF file to send as a FAX.</para>
  128. </argument>
  129. </parameter>
  130. <parameter name="options">
  131. <optionlist>
  132. <option name="d">
  133. <para>Enable FAX debugging.</para>
  134. </option>
  135. <option name="f">
  136. <para>Allow audio fallback FAX transfer on T.38 capable channels.</para>
  137. </option>
  138. <option name="F">
  139. <para>Force usage of audio mode on T.38 capable channels.</para>
  140. </option>
  141. <option name="s">
  142. <para>Send progress Manager events (overrides statusevents setting in res_fax.conf).</para>
  143. </option>
  144. <option name="z">
  145. <para>Initiate a T.38 reinvite on the channel if the remote end does not.</para>
  146. </option>
  147. </optionlist>
  148. </parameter>
  149. </syntax>
  150. <description>
  151. <para>This application is provided by res_fax, which is a FAX technology agnostic module
  152. that utilizes FAX technology resource modules to complete a FAX transmission.</para>
  153. <para>Session arguments can be set by the FAXOPT function and to check results of the SendFax() application.</para>
  154. </description>
  155. <see-also>
  156. <ref type="function">FAXOPT</ref>
  157. </see-also>
  158. </application>
  159. <function name="FAXOPT" language="en_US" module="res_fax">
  160. <synopsis>
  161. Gets/sets various pieces of information about a fax session.
  162. </synopsis>
  163. <syntax>
  164. <parameter name="item" required="true">
  165. <enumlist>
  166. <enum name="ecm">
  167. <para>R/W Error Correction Mode (ECM) enable with 'yes', disable with 'no'.</para>
  168. </enum>
  169. <enum name="error">
  170. <para>R/O FAX transmission error code upon failure.</para>
  171. </enum>
  172. <enum name="filename">
  173. <para>R/O Filename of the first file of the FAX transmission.</para>
  174. </enum>
  175. <enum name="filenames">
  176. <para>R/O Filenames of all of the files in the FAX transmission (comma separated).</para>
  177. </enum>
  178. <enum name="headerinfo">
  179. <para>R/W FAX header information.</para>
  180. </enum>
  181. <enum name="localstationid">
  182. <para>R/W Local Station Identification.</para>
  183. </enum>
  184. <enum name="minrate">
  185. <para>R/W Minimum transfer rate set before transmission.</para>
  186. </enum>
  187. <enum name="maxrate">
  188. <para>R/W Maximum transfer rate set before transmission.</para>
  189. </enum>
  190. <enum name="modem">
  191. <para>R/W Modem type (v17/v27/v29).</para>
  192. </enum>
  193. <enum name="gateway">
  194. <para>R/W T38 fax gateway, with optional fax activity timeout in seconds (yes[,timeout]/no)</para>
  195. </enum>
  196. <enum name="faxdetect">
  197. <para>R/W Enable FAX detect with optional timeout in seconds (yes,t38,cng[,timeout]/no)</para>
  198. </enum>
  199. <enum name="pages">
  200. <para>R/O Number of pages transferred.</para>
  201. </enum>
  202. <enum name="rate">
  203. <para>R/O Negotiated transmission rate.</para>
  204. </enum>
  205. <enum name="remotestationid">
  206. <para>R/O Remote Station Identification after transmission.</para>
  207. </enum>
  208. <enum name="resolution">
  209. <para>R/O Negotiated image resolution after transmission.</para>
  210. </enum>
  211. <enum name="sessionid">
  212. <para>R/O Session ID of the FAX transmission.</para>
  213. </enum>
  214. <enum name="status">
  215. <para>R/O Result Status of the FAX transmission.</para>
  216. </enum>
  217. <enum name="statusstr">
  218. <para>R/O Verbose Result Status of the FAX transmission.</para>
  219. </enum>
  220. <enum name="t38timeout">
  221. <para>R/W The timeout used for T.38 negotiation.</para>
  222. </enum>
  223. </enumlist>
  224. </parameter>
  225. </syntax>
  226. <description>
  227. <para>FAXOPT can be used to override the settings for a FAX session listed in <filename>res_fax.conf</filename>,
  228. it can also be used to retrieve information about a FAX session that has finished eg. pages/status.</para>
  229. </description>
  230. <see-also>
  231. <ref type="application">ReceiveFax</ref>
  232. <ref type="application">SendFax</ref>
  233. </see-also>
  234. </function>
  235. <manager name="FAXSessions" language="en_US">
  236. <synopsis>
  237. Lists active FAX sessions
  238. </synopsis>
  239. <syntax>
  240. <xi:include xpointer="xpointer(/docs/manager[@name='Login']/syntax/parameter[@name='ActionID'])" />
  241. </syntax>
  242. <description>
  243. <para>Will generate a series of FAXSession events with information about each FAXSession. Closes with
  244. a FAXSessionsComplete event which includes a count of the included FAX sessions. This action works in
  245. the same manner as the CLI command 'fax show sessions'</para>
  246. </description>
  247. </manager>
  248. <managerEvent language="en_US" name="FAXSessionsEntry">
  249. <managerEventInstance class="EVENT_FLAG_REPORTING">
  250. <synopsis>A single list item for the FAXSessions AMI command</synopsis>
  251. <syntax>
  252. <parameter name="ActionID" required="false"/>
  253. <parameter name="Channel">
  254. <para>Name of the channel responsible for the FAX session</para>
  255. </parameter>
  256. <parameter name="Technology">
  257. <para>The FAX technology that the FAX session is using</para>
  258. </parameter>
  259. <parameter name="SessionNumber">
  260. <para>The numerical identifier for this particular session</para>
  261. </parameter>
  262. <parameter name="SessionType">
  263. <para>FAX session passthru/relay type</para>
  264. <enumlist>
  265. <enum name="G.711" />
  266. <enum name="T.38" />
  267. </enumlist>
  268. </parameter>
  269. <parameter name="Operation">
  270. <para>FAX session operation type</para>
  271. <enumlist>
  272. <enum name="gateway" />
  273. <enum name="V.21" />
  274. <enum name="send" />
  275. <enum name="receive" />
  276. <enum name="none" />
  277. </enumlist>
  278. </parameter>
  279. <parameter name="State">
  280. <para>Current state of the FAX session</para>
  281. <enumlist>
  282. <enum name="Uninitialized" />
  283. <enum name="Initialized" />
  284. <enum name="Open" />
  285. <enum name="Active" />
  286. <enum name="Complete" />
  287. <enum name="Reserved" />
  288. <enum name="Inactive" />
  289. <enum name="Unknown" />
  290. </enumlist>
  291. </parameter>
  292. <parameter name="Files">
  293. <para>File or list of files associated with this FAX session</para>
  294. </parameter>
  295. </syntax>
  296. </managerEventInstance>
  297. </managerEvent>
  298. <managerEvent language="en_US" name="FAXSessionsComplete">
  299. <managerEventInstance class="EVENT_FLAG_CALL">
  300. <synopsis>Raised when all FAXSession events are completed for a FAXSessions command</synopsis>
  301. <syntax>
  302. <parameter name="ActionID" required="false"/>
  303. <parameter name="Total">
  304. <para>Count of FAXSession events sent in response to FAXSessions action</para>
  305. </parameter>
  306. </syntax>
  307. </managerEventInstance>
  308. </managerEvent>
  309. <manager name="FAXSession" language="en_US">
  310. <synopsis>
  311. Responds with a detailed description of a single FAX session
  312. </synopsis>
  313. <syntax>
  314. <xi:include xpointer="xpointer(/docs/manager[@name='Login']/syntax/parameter[@name='ActionID'])" />
  315. <parameter name="SessionNumber" required="true">
  316. <para>The session ID of the fax the user is interested in.</para>
  317. </parameter>
  318. </syntax>
  319. <description>
  320. <para>Provides details about a specific FAX session. The response will include a common subset of
  321. the output from the CLI command 'fax show session &lt;session_number&gt;' for each technology. If the
  322. FAX technolgy used by this session does not include a handler for FAXSession, then this action
  323. will fail.</para>
  324. </description>
  325. </manager>
  326. <managerEvent language="en_US" name="FAXSession">
  327. <managerEventInstance class="EVENT_FLAG_REPORTING">
  328. <synopsis>Raised in response to FAXSession manager command</synopsis>
  329. <syntax>
  330. <parameter name="ActionID" required="false"/>
  331. <parameter name="SessionNumber">
  332. <para>The numerical identifier for this particular session</para>
  333. </parameter>
  334. <xi:include xpointer="xpointer(/docs/managerEvent[@name='FAXSessionsEntry']/managerEventInstance/syntax/parameter[@name='Operation'])" />
  335. <xi:include xpointer="xpointer(/docs/managerEvent[@name='FAXSessionsEntry']/managerEventInstance/syntax/parameter[@name='State'])" />
  336. <parameter name="ErrorCorrectionMode" required="false">
  337. <para>Whether error correcting mode is enabled for the FAX session. This field is not
  338. included when operation is 'V.21 Detect' or if operation is 'gateway' and state is
  339. 'Uninitialized'
  340. </para>
  341. <enumlist>
  342. <enum name="yes" />
  343. <enum name="no" />
  344. </enumlist>
  345. </parameter>
  346. <parameter name="DataRate" required="false">
  347. <para>Bit rate of the FAX. This field is not included when operation is 'V.21 Detect' or
  348. if operation is 'gateway' and state is 'Uninitialized'.</para>
  349. </parameter>
  350. <parameter name="ImageResolution" required="false">
  351. <para>Resolution of each page of the FAX. Will be in the format of X_RESxY_RES. This field
  352. is not included if the operation is anything other than Receive/Transmit.</para>
  353. </parameter>
  354. <parameter name="PageNumber" required="false">
  355. <para>Current number of pages transferred during this FAX session. May change as the FAX
  356. progresses. This field is not included when operation is 'V.21 Detect' or if operation is
  357. 'gateway' and state is 'Uninitialized'.</para>
  358. </parameter>
  359. <parameter name="FileName" required="false">
  360. <para>Filename of the image being sent/received for this FAX session. This field is not
  361. included if Operation isn't 'send' or 'receive'.</para>
  362. </parameter>
  363. <parameter name="PagesTransmitted" required="false">
  364. <para>Total number of pages sent during this session. This field is not included if
  365. Operation isn't 'send' or 'receive'. Will always be 0 for 'receive'.</para>
  366. </parameter>
  367. <parameter name="PagesReceived" required="false">
  368. <para>Total number of pages received during this session. This field is not included if
  369. Operation is not 'send' or 'receive'. Will be 0 for 'send'.</para>
  370. </parameter>
  371. <parameter name="TotalBadLines" required="false">
  372. <para>Total number of bad lines sent/received during this session. This field is not
  373. included if Operation is not 'send' or 'received'.</para>
  374. </parameter>
  375. </syntax>
  376. </managerEventInstance>
  377. </managerEvent>
  378. <manager name="FAXStats" language="en_US">
  379. <synopsis>
  380. Responds with fax statistics
  381. </synopsis>
  382. <syntax>
  383. <xi:include xpointer="xpointer(/docs/manager[@name='Login']/syntax/parameter[@name='ActionID'])" />
  384. </syntax>
  385. <description>
  386. <para>Provides FAX statistics including the number of active sessions, reserved sessions, completed
  387. sessions, failed sessions, and the number of receive/transmit attempts. This command provides all
  388. of the non-technology specific information provided by the CLI command 'fax show stats'</para>
  389. </description>
  390. </manager>
  391. <managerEvent language="en_US" name="FAXStats">
  392. <managerEventInstance class="EVENT_FLAG_REPORTING">
  393. <synopsis>Raised in response to FAXStats manager command</synopsis>
  394. <syntax>
  395. <parameter name="ActionID" required="false"/>
  396. <parameter name="CurrentSessions" required="true">
  397. <para>Number of active FAX sessions</para>
  398. </parameter>
  399. <parameter name="ReservedSessions" required="true">
  400. <para>Number of reserved FAX sessions</para>
  401. </parameter>
  402. <parameter name="TransmitAttempts" required="true">
  403. <para>Total FAX sessions for which Asterisk is/was the transmitter</para>
  404. </parameter>
  405. <parameter name="ReceiveAttempts" required="true">
  406. <para>Total FAX sessions for which Asterisk is/was the recipient</para>
  407. </parameter>
  408. <parameter name="CompletedFAXes" required="true">
  409. <para>Total FAX sessions which have been completed successfully</para>
  410. </parameter>
  411. <parameter name="FailedFAXes" required="true">
  412. <para>Total FAX sessions which failed to complete successfully</para>
  413. </parameter>
  414. </syntax>
  415. </managerEventInstance>
  416. </managerEvent>
  417. ***/
  418. static const char app_receivefax[] = "ReceiveFAX";
  419. static const char app_sendfax[] = "SendFAX";
  420. struct debug_info_history {
  421. unsigned int consec_frames;
  422. unsigned int consec_ms;
  423. unsigned char silence;
  424. };
  425. struct ast_fax_debug_info {
  426. struct timeval base_tv;
  427. struct debug_info_history c2s, s2c;
  428. struct ast_dsp *dsp;
  429. };
  430. /*! \brief used for gateway framehook */
  431. struct fax_gateway {
  432. /*! \brief FAX Session */
  433. struct ast_fax_session *s;
  434. struct ast_fax_session *peer_v21_session;
  435. struct ast_fax_session *chan_v21_session;
  436. /*! \brief reserved fax session token */
  437. struct ast_fax_tech_token *token;
  438. /*! \brief the start of our timeout counter */
  439. struct timeval timeout_start;
  440. /*! \brief framehook used in gateway mode */
  441. int framehook;
  442. /*! \brief bridged */
  443. int bridged:1;
  444. /*! \brief 1 if a v21 preamble has been detected */
  445. int detected_v21:1;
  446. /*! \brief a flag to track the state of our negotiation */
  447. enum ast_t38_state t38_state;
  448. /*! \brief original audio formats */
  449. struct ast_format *chan_read_format;
  450. struct ast_format *chan_write_format;
  451. struct ast_format *peer_read_format;
  452. struct ast_format *peer_write_format;
  453. };
  454. /*! \brief used for fax detect framehook */
  455. struct fax_detect {
  456. /*! \brief the start of our timeout counter */
  457. struct timeval timeout_start;
  458. /*! \brief DSP Processor */
  459. struct ast_dsp *dsp;
  460. /*! \brief original audio formats */
  461. struct ast_format *orig_format;
  462. /*! \brief fax session details */
  463. struct ast_fax_session_details *details;
  464. /*! \brief mode */
  465. int flags;
  466. };
  467. /*! \brief FAX Detect flags */
  468. #define FAX_DETECT_MODE_CNG (1 << 0)
  469. #define FAX_DETECT_MODE_T38 (1 << 1)
  470. #define FAX_DETECT_MODE_BOTH (FAX_DETECT_MODE_CNG | FAX_DETECT_MODE_T38)
  471. static int fax_logger_level = -1;
  472. /*! \brief maximum buckets for res_fax ao2 containers */
  473. #define FAX_MAXBUCKETS 10
  474. #define RES_FAX_TIMEOUT 10000
  475. #define FAX_GATEWAY_TIMEOUT RES_FAX_TIMEOUT
  476. /*! \brief The faxregistry is used to manage information and statistics for all FAX sessions. */
  477. static struct {
  478. /*! The number of active FAX sessions */
  479. int active_sessions;
  480. /*! The number of reserved FAX sessions */
  481. int reserved_sessions;
  482. /*! active sessions are astobj2 objects */
  483. struct ao2_container *container;
  484. /*! Total number of Tx FAX attempts */
  485. int fax_tx_attempts;
  486. /*! Total number of Rx FAX attempts */
  487. int fax_rx_attempts;
  488. /*! Number of successful FAX transmissions */
  489. int fax_complete;
  490. /*! Number of failed FAX transmissions */
  491. int fax_failures;
  492. /*! the next unique session name */
  493. int nextsessionname;
  494. } faxregistry;
  495. /*! \brief registered FAX technology modules are put into this list */
  496. struct fax_module {
  497. const struct ast_fax_tech *tech;
  498. AST_RWLIST_ENTRY(fax_module) list;
  499. };
  500. static AST_RWLIST_HEAD_STATIC(faxmodules, fax_module);
  501. #define RES_FAX_MINRATE 4800
  502. #define RES_FAX_MAXRATE 14400
  503. #define RES_FAX_STATUSEVENTS 0
  504. #define RES_FAX_MODEM (AST_FAX_MODEM_V17 | AST_FAX_MODEM_V27TER | AST_FAX_MODEM_V29)
  505. #define RES_FAX_T38TIMEOUT 5000
  506. struct fax_options {
  507. enum ast_fax_modems modems;
  508. uint32_t statusevents:1;
  509. uint32_t ecm:1;
  510. unsigned int minrate;
  511. unsigned int maxrate;
  512. unsigned int t38timeout;
  513. };
  514. static struct fax_options general_options;
  515. static const struct fax_options default_options = {
  516. .minrate = RES_FAX_MINRATE,
  517. .maxrate = RES_FAX_MAXRATE,
  518. .statusevents = RES_FAX_STATUSEVENTS,
  519. .modems = RES_FAX_MODEM,
  520. .ecm = AST_FAX_OPTFLAG_TRUE,
  521. .t38timeout = RES_FAX_T38TIMEOUT,
  522. };
  523. AST_RWLOCK_DEFINE_STATIC(options_lock);
  524. static void get_general_options(struct fax_options* options);
  525. static void set_general_options(const struct fax_options* options);
  526. static const char *config = "res_fax.conf";
  527. static int global_fax_debug = 0;
  528. enum {
  529. OPT_CALLEDMODE = (1 << 0),
  530. OPT_CALLERMODE = (1 << 1),
  531. OPT_DEBUG = (1 << 2),
  532. OPT_STATUS = (1 << 3),
  533. OPT_ALLOWAUDIO = (1 << 5),
  534. OPT_REQUEST_T38 = (1 << 6),
  535. OPT_FORCE_AUDIO = (1 << 7),
  536. };
  537. AST_APP_OPTIONS(fax_exec_options, BEGIN_OPTIONS
  538. AST_APP_OPTION('a', OPT_CALLEDMODE),
  539. AST_APP_OPTION('c', OPT_CALLERMODE),
  540. AST_APP_OPTION('d', OPT_DEBUG),
  541. AST_APP_OPTION('f', OPT_ALLOWAUDIO),
  542. AST_APP_OPTION('F', OPT_FORCE_AUDIO),
  543. AST_APP_OPTION('s', OPT_STATUS),
  544. AST_APP_OPTION('z', OPT_REQUEST_T38),
  545. END_OPTIONS);
  546. static void debug_check_frame_for_silence(struct ast_fax_session *s, unsigned int c2s, struct ast_frame *frame)
  547. {
  548. struct debug_info_history *history = c2s ? &s->debug_info->c2s : &s->debug_info->s2c;
  549. int dspsilence;
  550. unsigned int last_consec_frames, last_consec_ms;
  551. unsigned char wassil;
  552. struct timeval diff;
  553. diff = ast_tvsub(ast_tvnow(), s->debug_info->base_tv);
  554. ast_dsp_reset(s->debug_info->dsp);
  555. ast_dsp_silence(s->debug_info->dsp, frame, &dspsilence);
  556. wassil = history->silence;
  557. history->silence = (dspsilence != 0) ? 1 : 0;
  558. if (history->silence != wassil) {
  559. last_consec_frames = history->consec_frames;
  560. last_consec_ms = history->consec_ms;
  561. history->consec_frames = 0;
  562. history->consec_ms = 0;
  563. if ((last_consec_frames != 0)) {
  564. ast_verb(0, "Channel '%s' fax session '%u', [ %.3ld.%.6ld ], %s sent %u frames (%u ms) of %s.\n",
  565. s->channame, s->id, (long) diff.tv_sec, (long int) diff.tv_usec,
  566. (c2s) ? "channel" : "stack", last_consec_frames, last_consec_ms,
  567. (wassil) ? "silence" : "energy");
  568. }
  569. }
  570. history->consec_frames++;
  571. history->consec_ms += (frame->samples / 8);
  572. }
  573. static void destroy_callback(void *data)
  574. {
  575. if (data) {
  576. ao2_ref(data, -1);
  577. }
  578. }
  579. static void fixup_callback(void *data, struct ast_channel *old_chan, struct ast_channel *new_chan);
  580. static const struct ast_datastore_info fax_datastore = {
  581. .type = "res_fax",
  582. .destroy = destroy_callback,
  583. .chan_fixup = fixup_callback,
  584. };
  585. static int fax_gateway_attach(struct ast_channel *chan, struct ast_fax_session_details *details);
  586. static int fax_detect_attach(struct ast_channel *chan, int timeout, int flags);
  587. static struct ast_fax_session_details *find_or_create_details(struct ast_channel *chan);
  588. static struct ast_fax_session *fax_v21_session_new (struct ast_channel *chan);
  589. /*! \brief Copies fax detection and gateway framehooks during masquerades
  590. *
  591. * \note must be called with both old_chan and new_chan locked. Since this
  592. * is only called by do_masquerade, that shouldn't be an issue.
  593. */
  594. static void fixup_callback(void *data, struct ast_channel *old_chan, struct ast_channel *new_chan)
  595. {
  596. struct ast_fax_session_details *old_details = data;
  597. struct ast_datastore *datastore = ast_channel_datastore_find(old_chan, &fax_datastore, NULL);
  598. if (old_details->gateway_id >= 0) {
  599. struct ast_fax_session_details *new_details = find_or_create_details(new_chan);
  600. ast_framehook_detach(old_chan, old_details->gateway_id);
  601. new_details->is_t38_negotiated = old_details->is_t38_negotiated;
  602. fax_gateway_attach(new_chan, new_details);
  603. ao2_cleanup(new_details);
  604. }
  605. if (old_details->faxdetect_id >= 0) {
  606. ast_framehook_detach(old_chan, old_details->faxdetect_id);
  607. fax_detect_attach(new_chan, old_details->faxdetect_timeout, old_details->faxdetect_flags);
  608. }
  609. if (datastore) {
  610. ast_channel_datastore_remove(old_chan, datastore);
  611. ast_datastore_free(datastore);
  612. }
  613. }
  614. /*! \brief returns a reference counted pointer to a fax datastore, if it exists */
  615. static struct ast_fax_session_details *find_details(struct ast_channel *chan)
  616. {
  617. struct ast_fax_session_details *details;
  618. struct ast_datastore *datastore;
  619. ast_channel_lock(chan);
  620. if (!(datastore = ast_channel_datastore_find(chan, &fax_datastore, NULL))) {
  621. ast_channel_unlock(chan);
  622. return NULL;
  623. }
  624. if (!(details = datastore->data)) {
  625. ast_log(LOG_WARNING, "Huh? channel '%s' has a FAX datastore without data!\n", ast_channel_name(chan));
  626. ast_channel_unlock(chan);
  627. return NULL;
  628. }
  629. ao2_ref(details, 1);
  630. ast_channel_unlock(chan);
  631. return details;
  632. }
  633. /*! \brief destroy a FAX session details structure */
  634. static void destroy_session_details(void *details)
  635. {
  636. struct ast_fax_session_details *d = details;
  637. struct ast_fax_document *doc;
  638. while ((doc = AST_LIST_REMOVE_HEAD(&d->documents, next))) {
  639. ast_free(doc);
  640. }
  641. ast_string_field_free_memory(d);
  642. }
  643. /*! \brief create a FAX session details structure */
  644. static struct ast_fax_session_details *session_details_new(void)
  645. {
  646. struct ast_fax_session_details *d;
  647. struct fax_options options;
  648. if (!(d = ao2_alloc(sizeof(*d), destroy_session_details))) {
  649. return NULL;
  650. }
  651. if (ast_string_field_init(d, 512)) {
  652. ao2_ref(d, -1);
  653. return NULL;
  654. }
  655. get_general_options(&options);
  656. AST_LIST_HEAD_INIT_NOLOCK(&d->documents);
  657. /* These options need to be set to the configured default and may be overridden by
  658. * SendFAX, ReceiveFAX, or FAXOPT */
  659. d->option.request_t38 = AST_FAX_OPTFLAG_FALSE;
  660. d->option.send_cng = AST_FAX_OPTFLAG_FALSE;
  661. d->option.send_ced = AST_FAX_OPTFLAG_FALSE;
  662. d->option.ecm = options.ecm;
  663. d->option.statusevents = options.statusevents;
  664. d->modems = options.modems;
  665. d->minrate = options.minrate;
  666. d->maxrate = options.maxrate;
  667. d->t38timeout = options.t38timeout;
  668. d->gateway_id = -1;
  669. d->faxdetect_id = -1;
  670. d->gateway_timeout = 0;
  671. return d;
  672. }
  673. static struct ast_control_t38_parameters our_t38_parameters = {
  674. .version = 0,
  675. .max_ifp = 400,
  676. .rate = AST_T38_RATE_14400,
  677. .rate_management = AST_T38_RATE_MANAGEMENT_TRANSFERRED_TCF,
  678. };
  679. static void t38_parameters_ast_to_fax(struct ast_fax_t38_parameters *dst, const struct ast_control_t38_parameters *src)
  680. {
  681. dst->version = src->version;
  682. dst->max_ifp = src->max_ifp;
  683. dst->rate = src->rate;
  684. dst->rate_management = src->rate_management;
  685. dst->fill_bit_removal = src->fill_bit_removal;
  686. dst->transcoding_mmr = src->transcoding_mmr;
  687. dst->transcoding_jbig = src->transcoding_jbig;
  688. }
  689. static void t38_parameters_fax_to_ast(struct ast_control_t38_parameters *dst, const struct ast_fax_t38_parameters *src)
  690. {
  691. dst->version = src->version;
  692. dst->max_ifp = src->max_ifp;
  693. dst->rate = src->rate;
  694. dst->rate_management = src->rate_management;
  695. dst->fill_bit_removal = src->fill_bit_removal;
  696. dst->transcoding_mmr = src->transcoding_mmr;
  697. dst->transcoding_jbig = src->transcoding_jbig;
  698. }
  699. /*! \brief returns a reference counted details structure from the channel's fax datastore. If the datastore
  700. * does not exist it will be created */
  701. static struct ast_fax_session_details *find_or_create_details(struct ast_channel *chan)
  702. {
  703. struct ast_fax_session_details *details;
  704. struct ast_datastore *datastore;
  705. if ((details = find_details(chan))) {
  706. return details;
  707. }
  708. /* channel does not have one so we must create one */
  709. if (!(details = session_details_new())) {
  710. ast_log(LOG_WARNING, "channel '%s' can't get a FAX details structure for the datastore!\n", ast_channel_name(chan));
  711. return NULL;
  712. }
  713. if (!(datastore = ast_datastore_alloc(&fax_datastore, NULL))) {
  714. ao2_ref(details, -1);
  715. ast_log(LOG_WARNING, "channel '%s' can't get a datastore!\n", ast_channel_name(chan));
  716. return NULL;
  717. }
  718. /* add the datastore to the channel and increment the refcount */
  719. datastore->data = details;
  720. /* initialize default T.38 parameters */
  721. t38_parameters_ast_to_fax(&details->our_t38_parameters, &our_t38_parameters);
  722. t38_parameters_ast_to_fax(&details->their_t38_parameters, &our_t38_parameters);
  723. ao2_ref(details, 1);
  724. ast_channel_lock(chan);
  725. ast_channel_datastore_add(chan, datastore);
  726. ast_channel_unlock(chan);
  727. return details;
  728. }
  729. unsigned int ast_fax_maxrate(void)
  730. {
  731. struct fax_options options;
  732. get_general_options(&options);
  733. return options.maxrate;
  734. }
  735. unsigned int ast_fax_minrate(void)
  736. {
  737. struct fax_options options;
  738. get_general_options(&options);
  739. return options.minrate;
  740. }
  741. static int update_modem_bits(enum ast_fax_modems *bits, const char *value)
  742. {
  743. char *m[5], *tok, *v = (char *)value;
  744. int i = 0, j;
  745. if (!strchr(v, ',')) {
  746. m[i++] = v;
  747. m[i] = NULL;
  748. } else {
  749. tok = strtok(v, ", ");
  750. while (tok && i < ARRAY_LEN(m) - 1) {
  751. m[i++] = tok;
  752. tok = strtok(NULL, ", ");
  753. }
  754. m[i] = NULL;
  755. }
  756. *bits = 0;
  757. for (j = 0; j < i; j++) {
  758. if (!strcasecmp(m[j], "v17")) {
  759. *bits |= AST_FAX_MODEM_V17;
  760. } else if (!strcasecmp(m[j], "v27")) {
  761. *bits |= AST_FAX_MODEM_V27TER;
  762. } else if (!strcasecmp(m[j], "v29")) {
  763. *bits |= AST_FAX_MODEM_V29;
  764. } else if (!strcasecmp(m[j], "v34")) {
  765. *bits |= AST_FAX_MODEM_V34;
  766. } else {
  767. ast_log(LOG_WARNING, "ignoring invalid modem setting: '%s', valid options {v17 | v27 | v29 | v34}\n", m[j]);
  768. }
  769. }
  770. return 0;
  771. }
  772. static char *ast_fax_caps_to_str(enum ast_fax_capabilities caps, char *buf, size_t bufsize)
  773. {
  774. char *out = buf;
  775. size_t size = bufsize;
  776. int first = 1;
  777. if (caps & AST_FAX_TECH_SEND) {
  778. if (!first) {
  779. ast_build_string(&buf, &size, ",");
  780. }
  781. ast_build_string(&buf, &size, "SEND");
  782. first = 0;
  783. }
  784. if (caps & AST_FAX_TECH_RECEIVE) {
  785. if (!first) {
  786. ast_build_string(&buf, &size, ",");
  787. }
  788. ast_build_string(&buf, &size, "RECEIVE");
  789. first = 0;
  790. }
  791. if (caps & AST_FAX_TECH_AUDIO) {
  792. if (!first) {
  793. ast_build_string(&buf, &size, ",");
  794. }
  795. ast_build_string(&buf, &size, "AUDIO");
  796. first = 0;
  797. }
  798. if (caps & AST_FAX_TECH_T38) {
  799. if (!first) {
  800. ast_build_string(&buf, &size, ",");
  801. }
  802. ast_build_string(&buf, &size, "T38");
  803. first = 0;
  804. }
  805. if (caps & AST_FAX_TECH_MULTI_DOC) {
  806. if (!first) {
  807. ast_build_string(&buf, &size, ",");
  808. }
  809. ast_build_string(&buf, &size, "MULTI_DOC");
  810. first = 0;
  811. }
  812. if (caps & AST_FAX_TECH_GATEWAY) {
  813. if (!first) {
  814. ast_build_string(&buf, &size, ",");
  815. }
  816. ast_build_string(&buf, &size, "GATEWAY");
  817. first = 0;
  818. }
  819. if (caps & AST_FAX_TECH_V21_DETECT) {
  820. if (!first) {
  821. ast_build_string(&buf, &size, ",");
  822. }
  823. ast_build_string(&buf, &size, "V21");
  824. first = 0;
  825. }
  826. return out;
  827. }
  828. static int ast_fax_modem_to_str(enum ast_fax_modems bits, char *tbuf, size_t bufsize)
  829. {
  830. int count = 0;
  831. if (bits & AST_FAX_MODEM_V17) {
  832. strcat(tbuf, "V17");
  833. count++;
  834. }
  835. if (bits & AST_FAX_MODEM_V27TER) {
  836. if (count) {
  837. strcat(tbuf, ",");
  838. }
  839. strcat(tbuf, "V27");
  840. count++;
  841. }
  842. if (bits & AST_FAX_MODEM_V29) {
  843. if (count) {
  844. strcat(tbuf, ",");
  845. }
  846. strcat(tbuf, "V29");
  847. count++;
  848. }
  849. if (bits & AST_FAX_MODEM_V34) {
  850. if (count) {
  851. strcat(tbuf, ",");
  852. }
  853. strcat(tbuf, "V34");
  854. count++;
  855. }
  856. return 0;
  857. }
  858. static int check_modem_rate(enum ast_fax_modems modems, unsigned int rate)
  859. {
  860. switch (rate) {
  861. case 2400:
  862. case 4800:
  863. if (!(modems & (AST_FAX_MODEM_V27TER | AST_FAX_MODEM_V34))) {
  864. return 1;
  865. }
  866. break;
  867. case 7200:
  868. case 9600:
  869. if (!(modems & (AST_FAX_MODEM_V17 | AST_FAX_MODEM_V29 | AST_FAX_MODEM_V34))) {
  870. return 1;
  871. }
  872. break;
  873. case 12000:
  874. case 14400:
  875. if (!(modems & (AST_FAX_MODEM_V17 | AST_FAX_MODEM_V34))) {
  876. return 1;
  877. }
  878. break;
  879. case 28800:
  880. case 33600:
  881. if (!(modems & AST_FAX_MODEM_V34)) {
  882. return 1;
  883. }
  884. break;
  885. default:
  886. /* this should never happen */
  887. return 1;
  888. }
  889. return 0;
  890. }
  891. /*! \brief register a FAX technology module */
  892. int ast_fax_tech_register(struct ast_fax_tech *tech)
  893. {
  894. struct fax_module *fax;
  895. if (!(fax = ast_calloc(1, sizeof(*fax)))) {
  896. return -1;
  897. }
  898. fax->tech = tech;
  899. AST_RWLIST_WRLOCK(&faxmodules);
  900. AST_RWLIST_INSERT_TAIL(&faxmodules, fax, list);
  901. AST_RWLIST_UNLOCK(&faxmodules);
  902. ast_module_ref(ast_module_info->self);
  903. ast_verb(3, "Registered handler for '%s' (%s)\n", fax->tech->type, fax->tech->description);
  904. return 0;
  905. }
  906. /*! \brief unregister a FAX technology module */
  907. void ast_fax_tech_unregister(struct ast_fax_tech *tech)
  908. {
  909. struct fax_module *fax;
  910. ast_verb(3, "Unregistering FAX module type '%s'\n", tech->type);
  911. AST_RWLIST_WRLOCK(&faxmodules);
  912. AST_RWLIST_TRAVERSE_SAFE_BEGIN(&faxmodules, fax, list) {
  913. if (fax->tech != tech) {
  914. continue;
  915. }
  916. AST_RWLIST_REMOVE_CURRENT(list);
  917. ast_module_unref(ast_module_info->self);
  918. ast_free(fax);
  919. ast_verb(4, "Unregistered FAX module type '%s'\n", tech->type);
  920. break;
  921. }
  922. AST_RWLIST_TRAVERSE_SAFE_END;
  923. AST_RWLIST_UNLOCK(&faxmodules);
  924. }
  925. /*! \brief convert a ast_fax_state to a string */
  926. const char *ast_fax_state_to_str(enum ast_fax_state state)
  927. {
  928. switch (state) {
  929. case AST_FAX_STATE_UNINITIALIZED:
  930. return "Uninitialized";
  931. case AST_FAX_STATE_INITIALIZED:
  932. return "Initialized";
  933. case AST_FAX_STATE_OPEN:
  934. return "Open";
  935. case AST_FAX_STATE_ACTIVE:
  936. return "Active";
  937. case AST_FAX_STATE_COMPLETE:
  938. return "Complete";
  939. case AST_FAX_STATE_RESERVED:
  940. return "Reserved";
  941. case AST_FAX_STATE_INACTIVE:
  942. return "Inactive";
  943. default:
  944. ast_log(LOG_WARNING, "unhandled FAX state: %u\n", state);
  945. return "Unknown";
  946. }
  947. }
  948. void ast_fax_log(int level, const char *file, const int line, const char *function, const char *msg)
  949. {
  950. if (fax_logger_level != -1) {
  951. ast_log_dynamic_level(fax_logger_level, "%s", msg);
  952. } else {
  953. ast_log(level, file, line, function, "%s", msg);
  954. }
  955. }
  956. /*! \brief convert a rate string to a rate */
  957. static unsigned int fax_rate_str_to_int(const char *ratestr)
  958. {
  959. int rate;
  960. if (sscanf(ratestr, "%d", &rate) != 1) {
  961. ast_log(LOG_ERROR, "failed to sscanf '%s' to rate\n", ratestr);
  962. return 0;
  963. }
  964. switch (rate) {
  965. case 2400:
  966. case 4800:
  967. case 7200:
  968. case 9600:
  969. case 12000:
  970. case 14400:
  971. case 28800:
  972. case 33600:
  973. return rate;
  974. default:
  975. ast_log(LOG_WARNING, "ignoring invalid rate '%s'. Valid options are {2400 | 4800 | 7200 | 9600 | 12000 | 14400 | 28800 | 33600}\n", ratestr);
  976. return 0;
  977. }
  978. }
  979. /*! \brief Release a session token.
  980. * \param s a session returned from fax_session_reserve()
  981. * \param token a token generated from fax_session_reserve()
  982. *
  983. * This function releases the given token and marks the given session as no
  984. * longer reserved. It is safe to call on a session that is not actually
  985. * reserved and with a NULL token. This is so that sessions returned by
  986. * technologies that do not support reserved sessions don't require extra logic
  987. * to handle.
  988. *
  989. * \note This function DOES NOT release the given fax session, only the given
  990. * token.
  991. */
  992. static void fax_session_release(struct ast_fax_session *s, struct ast_fax_tech_token *token)
  993. {
  994. if (token) {
  995. s->tech->release_token(token);
  996. }
  997. if (s->state == AST_FAX_STATE_RESERVED) {
  998. ast_atomic_fetchadd_int(&faxregistry.reserved_sessions, -1);
  999. s->state = AST_FAX_STATE_INACTIVE;
  1000. }
  1001. }
  1002. /*! \brief destroy a FAX session structure */
  1003. static void destroy_session(void *session)
  1004. {
  1005. struct ast_fax_session *s = session;
  1006. if (s->tech) {
  1007. fax_session_release(s, NULL);
  1008. if (s->tech_pvt) {
  1009. s->tech->destroy_session(s);
  1010. }
  1011. ast_module_unref(s->tech->module);
  1012. }
  1013. if (s->details) {
  1014. if (s->details->caps & AST_FAX_TECH_GATEWAY) {
  1015. s->details->caps &= ~AST_FAX_TECH_GATEWAY;
  1016. }
  1017. ao2_ref(s->details, -1);
  1018. }
  1019. if (s->debug_info) {
  1020. ast_dsp_free(s->debug_info->dsp);
  1021. ast_free(s->debug_info);
  1022. }
  1023. if (s->smoother) {
  1024. ast_smoother_free(s->smoother);
  1025. }
  1026. if (s->state != AST_FAX_STATE_INACTIVE) {
  1027. ast_atomic_fetchadd_int(&faxregistry.active_sessions, -1);
  1028. }
  1029. ast_free(s->channame);
  1030. ast_free(s->chan_uniqueid);
  1031. }
  1032. /*! \brief Reserve a fax session.
  1033. * \param details the fax session details
  1034. * \param token a pointer to a place to store a token to be passed to fax_session_new() later
  1035. *
  1036. * This function reserves a fax session for use later. If the selected fax
  1037. * technology does not support reserving sessions a session will still be
  1038. * returned but token will not be set.
  1039. *
  1040. * \note The reference returned by this function does not get consumed by
  1041. * fax_session_new() and must always be dereferenced separately.
  1042. *
  1043. * \return NULL or an uninitialized and possibly reserved session
  1044. */
  1045. static struct ast_fax_session *fax_session_reserve(struct ast_fax_session_details *details, struct ast_fax_tech_token **token)
  1046. {
  1047. struct ast_fax_session *s;
  1048. struct fax_module *faxmod;
  1049. if (!(s = ao2_alloc(sizeof(*s), destroy_session))) {
  1050. return NULL;
  1051. }
  1052. s->state = AST_FAX_STATE_INACTIVE;
  1053. s->details = details;
  1054. ao2_ref(s->details, 1);
  1055. /* locate a FAX technology module that can handle said requirements
  1056. * Note: the requirements have not yet been finalized as T.38
  1057. * negotiation has not yet occured. */
  1058. AST_RWLIST_RDLOCK(&faxmodules);
  1059. AST_RWLIST_TRAVERSE(&faxmodules, faxmod, list) {
  1060. if ((faxmod->tech->caps & details->caps) != details->caps) {
  1061. continue;
  1062. }
  1063. ast_debug(4, "Reserving a FAX session from '%s'.\n", faxmod->tech->description);
  1064. ast_module_ref(faxmod->tech->module);
  1065. s->tech = faxmod->tech;
  1066. break;
  1067. }
  1068. AST_RWLIST_UNLOCK(&faxmodules);
  1069. if (!faxmod) {
  1070. char caps[128] = "";
  1071. ast_log(LOG_ERROR, "Could not locate a FAX technology module with capabilities (%s)\n", ast_fax_caps_to_str(details->caps, caps, sizeof(caps)));
  1072. ao2_ref(s, -1);
  1073. return NULL;
  1074. }
  1075. if (!s->tech->reserve_session) {
  1076. ast_debug(1, "Selected FAX technology module (%s) does not support reserving sessions.\n", s->tech->description);
  1077. return s;
  1078. }
  1079. if (!(*token = s->tech->reserve_session(s))) {
  1080. ao2_ref(s, -1);
  1081. return NULL;
  1082. }
  1083. s->state = AST_FAX_STATE_RESERVED;
  1084. ast_atomic_fetchadd_int(&faxregistry.reserved_sessions, 1);
  1085. return s;
  1086. }
  1087. /*! \brief create a FAX session
  1088. *
  1089. * \param details details for the session
  1090. * \param chan the channel the session will run on
  1091. * \param reserved a reserved session to base this session on (can be NULL)
  1092. * \param token the token for a reserved session (can be NULL)
  1093. *
  1094. * Create a new fax session based on the given details structure.
  1095. *
  1096. * \note The given token is always consumed (by tech->new_session() or by
  1097. * fax_session_release() in the event of a failure). The given reference to a
  1098. * reserved session is never consumed and must be dereferenced separately from
  1099. * the reference returned by this function.
  1100. *
  1101. * \return NULL or a reference to a new fax session
  1102. */
  1103. static struct ast_fax_session *fax_session_new(struct ast_fax_session_details *details, struct ast_channel *chan, struct ast_fax_session *reserved, struct ast_fax_tech_token *token)
  1104. {
  1105. struct ast_fax_session *s = NULL;
  1106. struct fax_module *faxmod;
  1107. if (reserved) {
  1108. s = reserved;
  1109. ao2_ref(reserved, +1);
  1110. ao2_unlink(faxregistry.container, reserved);
  1111. /* NOTE: we don't consume the reference to the reserved
  1112. * session. The session returned from fax_session_new() is a
  1113. * new reference and must be derefed in addition to the
  1114. * reserved session.
  1115. */
  1116. if (s->state == AST_FAX_STATE_RESERVED) {
  1117. ast_atomic_fetchadd_int(&faxregistry.reserved_sessions, -1);
  1118. s->state = AST_FAX_STATE_UNINITIALIZED;
  1119. }
  1120. }
  1121. if (!s && !(s = ao2_alloc(sizeof(*s), destroy_session))) {
  1122. return NULL;
  1123. }
  1124. ast_atomic_fetchadd_int(&faxregistry.active_sessions, 1);
  1125. s->state = AST_FAX_STATE_UNINITIALIZED;
  1126. if (details->option.debug && (details->caps & AST_FAX_TECH_AUDIO)) {
  1127. if (!(s->debug_info = ast_calloc(1, sizeof(*(s->debug_info))))) {
  1128. fax_session_release(s, token);
  1129. ao2_ref(s, -1);
  1130. return NULL;
  1131. }
  1132. if (!(s->debug_info->dsp = ast_dsp_new())) {
  1133. ast_free(s->debug_info);
  1134. s->debug_info = NULL;
  1135. fax_session_release(s, token);
  1136. ao2_ref(s, -1);
  1137. return NULL;
  1138. }
  1139. ast_dsp_set_threshold(s->debug_info->dsp, 128);
  1140. }
  1141. if (!(s->channame = ast_strdup(ast_channel_name(chan)))) {
  1142. fax_session_release(s, token);
  1143. ao2_ref(s, -1);
  1144. return NULL;
  1145. }
  1146. if (!(s->chan_uniqueid = ast_strdup(ast_channel_uniqueid(chan)))) {
  1147. fax_session_release(s, token);
  1148. ao2_ref(s, -1);
  1149. return NULL;
  1150. }
  1151. s->chan = chan;
  1152. if (!s->details) {
  1153. s->details = details;
  1154. ao2_ref(s->details, 1);
  1155. }
  1156. details->id = s->id = ast_atomic_fetchadd_int(&faxregistry.nextsessionname, 1);
  1157. if (!token) {
  1158. /* locate a FAX technology module that can handle said requirements */
  1159. AST_RWLIST_RDLOCK(&faxmodules);
  1160. AST_RWLIST_TRAVERSE(&faxmodules, faxmod, list) {
  1161. if ((faxmod->tech->caps & details->caps) != details->caps) {
  1162. continue;
  1163. }
  1164. ast_debug(4, "Requesting a new FAX session from '%s'.\n", faxmod->tech->description);
  1165. ast_module_ref(faxmod->tech->module);
  1166. if (reserved) {
  1167. /* Balance module ref from reserved session */
  1168. ast_module_unref(reserved->tech->module);
  1169. }
  1170. s->tech = faxmod->tech;
  1171. break;
  1172. }
  1173. AST_RWLIST_UNLOCK(&faxmodules);
  1174. if (!faxmod) {
  1175. char caps[128] = "";
  1176. ast_log(LOG_ERROR, "Could not locate a FAX technology module with capabilities (%s)\n", ast_fax_caps_to_str(details->caps, caps, sizeof(caps)));
  1177. ao2_ref(s, -1);
  1178. return NULL;
  1179. }
  1180. }
  1181. if (!(s->tech_pvt = s->tech->new_session(s, token))) {
  1182. ast_log(LOG_ERROR, "FAX session failed to initialize.\n");
  1183. ao2_ref(s, -1);
  1184. return NULL;
  1185. }
  1186. /* link the session to the session container */
  1187. if (!(ao2_link(faxregistry.container, s))) {
  1188. ast_log(LOG_ERROR, "failed to add FAX session '%u' to container.\n", s->id);
  1189. ao2_ref(s, -1);
  1190. return NULL;
  1191. }
  1192. ast_debug(4, "channel '%s' using FAX session '%u'\n", s->channame, s->id);
  1193. return s;
  1194. }
  1195. /*!
  1196. * \internal
  1197. * \brief Convert the filenames in a fax session into a JSON array
  1198. * \retval NULL on error
  1199. * \retval A \ref ast_json array on success
  1200. */
  1201. static struct ast_json *generate_filenames_json(struct ast_fax_session_details *details)
  1202. {
  1203. RAII_VAR(struct ast_json *, json_array, ast_json_array_create(), ast_json_unref);
  1204. struct ast_fax_document *doc;
  1205. if (!details || !json_array) {
  1206. return NULL;
  1207. }
  1208. /* don't process empty lists */
  1209. if (AST_LIST_EMPTY(&details->documents)) {
  1210. return NULL;
  1211. }
  1212. AST_LIST_TRAVERSE(&details->documents, doc, next) {
  1213. struct ast_json *entry = ast_json_string_create(doc->filename);
  1214. if (!entry) {
  1215. return NULL;
  1216. }
  1217. if (ast_json_array_append(json_array, entry)) {
  1218. return NULL;
  1219. }
  1220. }
  1221. ast_json_ref(json_array);
  1222. return json_array;
  1223. }
  1224. /*!
  1225. * \brief Generate a string of filenames using the given prefix and separator.
  1226. * \param details the fax session details
  1227. * \param prefix the prefix to each filename
  1228. * \param separator the separator between filenames
  1229. *
  1230. * This function generates a string of filenames from the given details
  1231. * structure and using the given prefix and separator.
  1232. *
  1233. * \retval NULL there was an error generating the string
  1234. * \return the string generated string
  1235. */
  1236. static char *generate_filenames_string(struct ast_fax_session_details *details, char *prefix, char *separator)
  1237. {
  1238. char *filenames, *c;
  1239. size_t size = 0;
  1240. int first = 1;
  1241. struct ast_fax_document *doc;
  1242. /* don't process empty lists */
  1243. if (AST_LIST_EMPTY(&details->documents)) {
  1244. return ast_strdup("");
  1245. }
  1246. /* Calculate the total length of all of the file names */
  1247. AST_LIST_TRAVERSE(&details->documents, doc, next) {
  1248. size += strlen(separator) + strlen(prefix) + strlen(doc->filename);
  1249. }
  1250. size += 1; /* add space for the terminating null */
  1251. if (!(filenames = ast_malloc(size))) {
  1252. return NULL;
  1253. }
  1254. c = filenames;
  1255. ast_build_string(&c, &size, "%s%s", prefix, AST_LIST_FIRST(&details->documents)->filename);
  1256. AST_LIST_TRAVERSE(&details->documents, doc, next) {
  1257. if (first) {
  1258. first = 0;
  1259. continue;
  1260. }
  1261. ast_build_string(&c, &size, "%s%s%s", separator, prefix, doc->filename);
  1262. }
  1263. return filenames;
  1264. }
  1265. /*! \brief send a FAX status manager event */
  1266. static int report_fax_status(struct ast_channel *chan, struct ast_fax_session_details *details, const char *status)
  1267. {
  1268. RAII_VAR(struct ast_json *, json_object, NULL, ast_json_unref);
  1269. RAII_VAR(struct stasis_message *, message, NULL, ao2_cleanup);
  1270. struct ast_json *json_filenames = NULL;
  1271. if (!details->option.statusevents) {
  1272. return 0;
  1273. }
  1274. json_filenames = generate_filenames_json(details);
  1275. if (!json_filenames) {
  1276. return -1;
  1277. }
  1278. json_object = ast_json_pack("{s: s, s: s, s: s, s: s, s: o}",
  1279. "type", "status",
  1280. "operation", (details->caps & AST_FAX_TECH_GATEWAY)
  1281. ? "gateway"
  1282. : (details->caps & AST_FAX_TECH_RECEIVE) ? "receive" : "send",
  1283. "status", status,
  1284. "local_station_id", AST_JSON_UTF8_VALIDATE(details->localstationid),
  1285. "filenames", json_filenames);
  1286. if (!json_object) {
  1287. return -1;
  1288. }
  1289. {
  1290. SCOPED_CHANNELLOCK(lock, chan);
  1291. message = ast_channel_blob_create_from_cache(ast_channel_uniqueid(chan), ast_channel_fax_type(), json_object);
  1292. if (!message) {
  1293. return -1;
  1294. }
  1295. stasis_publish(ast_channel_topic(chan), message);
  1296. }
  1297. return 0;
  1298. }
  1299. /*! \brief Set fax related channel variables. */
  1300. static void set_channel_variables(struct ast_channel *chan, struct ast_fax_session_details *details)
  1301. {
  1302. char buf[10];
  1303. pbx_builtin_setvar_helper(chan, "FAXSTATUS", S_OR(details->result, NULL));
  1304. pbx_builtin_setvar_helper(chan, "FAXERROR", S_OR(details->error, NULL));
  1305. pbx_builtin_setvar_helper(chan, "FAXSTATUSSTRING", S_OR(details->resultstr, NULL));
  1306. pbx_builtin_setvar_helper(chan, "REMOTESTATIONID", S_OR(details->remotestationid, NULL));
  1307. pbx_builtin_setvar_helper(chan, "LOCALSTATIONID", S_OR(details->localstationid, NULL));
  1308. pbx_builtin_setvar_helper(chan, "FAXBITRATE", S_OR(details->transfer_rate, NULL));
  1309. pbx_builtin_setvar_helper(chan, "FAXRESOLUTION", S_OR(details->resolution, NULL));
  1310. if (details->is_t38_negotiated) {
  1311. pbx_builtin_setvar_helper(chan, "FAXMODE", "T38");
  1312. } else {
  1313. pbx_builtin_setvar_helper(chan, "FAXMODE", "audio");
  1314. }
  1315. snprintf(buf, sizeof(buf), "%u", details->pages_transferred);
  1316. pbx_builtin_setvar_helper(chan, "FAXPAGES", buf);
  1317. }
  1318. #define GENERIC_FAX_EXEC_SET_VARS(fax, chan, errorstr, reason) \
  1319. do { \
  1320. if (ast_strlen_zero(fax->details->result)) \
  1321. ast_string_field_set(fax->details, result, "FAILED"); \
  1322. if (ast_strlen_zero(fax->details->resultstr)) \
  1323. ast_string_field_set(fax->details, resultstr, reason); \
  1324. if (ast_strlen_zero(fax->details->error)) \
  1325. ast_string_field_set(fax->details, error, errorstr); \
  1326. set_channel_variables(chan, fax->details); \
  1327. } while (0)
  1328. #define GENERIC_FAX_EXEC_ERROR_QUIET(fax, chan, errorstr, reason) \
  1329. do { \
  1330. GENERIC_FAX_EXEC_SET_VARS(fax, chan, errorstr, reason); \
  1331. } while (0)
  1332. #define GENERIC_FAX_EXEC_ERROR(fax, chan, errorstr, reason) \
  1333. do { \
  1334. ast_log(LOG_ERROR, "channel '%s' FAX session '%u' failure, reason: '%s' (%s)\n", ast_channel_name(chan), fax->id, reason, errorstr); \
  1335. GENERIC_FAX_EXEC_ERROR_QUIET(fax, chan, errorstr, reason); \
  1336. } while (0)
  1337. static int set_fax_t38_caps(struct ast_channel *chan, struct ast_fax_session_details *details)
  1338. {
  1339. switch (ast_channel_get_t38_state(chan)) {
  1340. case T38_STATE_UNKNOWN:
  1341. details->caps |= AST_FAX_TECH_T38;
  1342. break;
  1343. case T38_STATE_REJECTED:
  1344. case T38_STATE_UNAVAILABLE:
  1345. details->caps |= AST_FAX_TECH_AUDIO;
  1346. break;
  1347. case T38_STATE_NEGOTIATED:
  1348. /* already in T.38 mode? This should not happen. */
  1349. case T38_STATE_NEGOTIATING: {
  1350. /* the other end already sent us a T.38 reinvite, so we need to prod the channel
  1351. * driver into resending their parameters to us if it supports doing so... if
  1352. * not, we can't proceed, because we can't create a proper reply without them.
  1353. * if it does work, the channel driver will send an AST_CONTROL_T38_PARAMETERS
  1354. * with a request of AST_T38_REQUEST_NEGOTIATE, which will be read by the function
  1355. * that gets called after this one completes
  1356. */
  1357. struct ast_control_t38_parameters parameters = { .request_response = AST_T38_REQUEST_PARMS, };
  1358. if (ast_indicate_data(chan, AST_CONTROL_T38_PARAMETERS, &parameters, sizeof(parameters)) != AST_T38_REQUEST_PARMS) {
  1359. ast_log(LOG_ERROR, "channel '%s' is in an unsupported T.38 negotiation state, cannot continue.\n", ast_channel_name(chan));
  1360. return -1;
  1361. }
  1362. details->caps |= AST_FAX_TECH_T38;
  1363. break;
  1364. }
  1365. default:
  1366. ast_log(LOG_ERROR, "channel '%s' is in an unsupported T.38 negotiation state, cannot continue.\n", ast_channel_name(chan));
  1367. return -1;
  1368. }
  1369. return 0;
  1370. }
  1371. static int disable_t38(struct ast_channel *chan)
  1372. {
  1373. int timeout_ms;
  1374. struct ast_frame *frame = NULL;
  1375. struct ast_control_t38_parameters t38_parameters = { .request_response = AST_T38_REQUEST_TERMINATE, };
  1376. struct timeval start;
  1377. int ms;
  1378. ast_debug(1, "Shutting down T.38 on %s\n", ast_channel_name(chan));
  1379. if (ast_indicate_data(chan, AST_CONTROL_T38_PARAMETERS, &t38_parameters, sizeof(t38_parameters)) != 0) {
  1380. ast_debug(1, "error while disabling T.38 on channel '%s'\n", ast_channel_name(chan));
  1381. return -1;
  1382. }
  1383. /* wait up to five seconds for negotiation to complete */
  1384. timeout_ms = 5000;
  1385. start = ast_tvnow();
  1386. while ((ms = ast_remaining_ms(start, timeout_ms))) {
  1387. ms = ast_waitfor(chan, ms);
  1388. if (ms == 0) {
  1389. break;
  1390. }
  1391. if (ms < 0) {
  1392. ast_debug(1, "error while disabling T.38 on channel '%s'\n", ast_channel_name(chan));
  1393. return -1;
  1394. }
  1395. if (!(frame = ast_read(chan))) {
  1396. return -1;
  1397. }
  1398. if ((frame->frametype == AST_FRAME_CONTROL) &&
  1399. (frame->subclass.integer == AST_CONTROL_T38_PARAMETERS) &&
  1400. (frame->datalen == sizeof(t38_parameters))) {
  1401. struct ast_control_t38_parameters *parameters = frame->data.ptr;
  1402. switch (parameters->request_response) {
  1403. case AST_T38_TERMINATED:
  1404. ast_debug(1, "Shut down T.38 on %s\n", ast_channel_name(chan));
  1405. break;
  1406. case AST_T38_REFUSED:
  1407. ast_log(LOG_WARNING, "channel '%s' refused to disable T.38\n", ast_channel_name(chan));
  1408. ast_frfree(frame);
  1409. return -1;
  1410. default:
  1411. ast_log(LOG_ERROR, "channel '%s' failed to disable T.38\n", ast_channel_name(chan));
  1412. ast_frfree(frame);
  1413. return -1;
  1414. }
  1415. ast_frfree(frame);
  1416. break;
  1417. }
  1418. ast_frfree(frame);
  1419. }
  1420. if (ms == 0) { /* all done, nothing happened */
  1421. ast_debug(1, "channel '%s' timed-out during T.38 shutdown\n", ast_channel_name(chan));
  1422. }
  1423. return 0;
  1424. }
  1425. /*! \brief this is the generic FAX session handling function */
  1426. static int generic_fax_exec(struct ast_channel *chan, struct ast_fax_session_details *details, struct ast_fax_session *reserved, struct ast_fax_tech_token *token)
  1427. {
  1428. int ms;
  1429. int timeout = RES_FAX_TIMEOUT;
  1430. int chancount;
  1431. unsigned int expected_frametype = -1;
  1432. struct ast_frame_subclass expected_framesubclass = { .integer = 0, };
  1433. unsigned int t38negotiated = (ast_channel_get_t38_state(chan) == T38_STATE_NEGOTIATED);
  1434. struct ast_control_t38_parameters t38_parameters;
  1435. const char *tempvar;
  1436. struct ast_fax_session *fax = NULL;
  1437. struct ast_frame *frame = NULL;
  1438. struct ast_channel *c = chan;
  1439. RAII_VAR(struct ast_format *, orig_write_format, NULL, ao2_cleanup);
  1440. RAII_VAR(struct ast_format *, orig_read_format, NULL, ao2_cleanup);
  1441. int remaining_time;
  1442. struct timeval start;
  1443. chancount = 1;
  1444. /* Make sure one or the other is set to avoid race condition */
  1445. if (t38negotiated) {
  1446. details->caps |= AST_FAX_TECH_T38;
  1447. } else {
  1448. details->caps |= AST_FAX_TECH_AUDIO;
  1449. }
  1450. /* create the FAX session */
  1451. if (!(fax = fax_session_new(details, chan, reserved, token))) {
  1452. ast_log(LOG_ERROR, "Can't create a FAX session, FAX attempt failed.\n");
  1453. report_fax_status(chan, details, "No Available Resource");
  1454. return -1;
  1455. }
  1456. ast_channel_lock(chan);
  1457. /* update session details */
  1458. if (ast_strlen_zero(details->headerinfo) && (tempvar = pbx_builtin_getvar_helper(chan, "LOCALHEADERINFO"))) {
  1459. ast_string_field_set(details, headerinfo, tempvar);
  1460. }
  1461. if (ast_strlen_zero(details->localstationid)) {
  1462. tempvar = pbx_builtin_getvar_helper(chan, "LOCALSTATIONID");
  1463. ast_string_field_set(details, localstationid, tempvar ? tempvar : "unknown");
  1464. }
  1465. ast_channel_unlock(chan);
  1466. report_fax_status(chan, details, "Allocating Resources");
  1467. if (details->caps & AST_FAX_TECH_AUDIO) {
  1468. expected_frametype = AST_FRAME_VOICE;
  1469. expected_framesubclass.format = ast_format_slin;
  1470. orig_write_format = ao2_bump(ast_channel_writeformat(chan));
  1471. if (ast_set_write_format(chan, ast_format_slin) < 0) {
  1472. ast_log(LOG_ERROR, "channel '%s' failed to set write format to signed linear'.\n", ast_channel_name(chan));
  1473. ao2_unlink(faxregistry.container, fax);
  1474. ao2_ref(fax, -1);
  1475. return -1;
  1476. }
  1477. orig_read_format = ao2_bump(ast_channel_readformat(chan));
  1478. if (ast_set_read_format(chan, ast_format_slin) < 0) {
  1479. ast_log(LOG_ERROR, "channel '%s' failed to set read format to signed linear.\n", ast_channel_name(chan));
  1480. ao2_unlink(faxregistry.container, fax);
  1481. ao2_ref(fax, -1);
  1482. return -1;
  1483. }
  1484. if (fax->smoother) {
  1485. ast_smoother_free(fax->smoother);
  1486. fax->smoother = NULL;
  1487. }
  1488. if (!(fax->smoother = ast_smoother_new(320))) {
  1489. ast_log(LOG_WARNING, "Channel '%s' FAX session '%u' failed to obtain a smoother.\n", ast_channel_name(chan), fax->id);
  1490. }
  1491. } else {
  1492. expected_frametype = AST_FRAME_MODEM;
  1493. expected_framesubclass.integer = AST_MODEM_T38;
  1494. }
  1495. if (fax->debug_info) {
  1496. fax->debug_info->base_tv = ast_tvnow();
  1497. }
  1498. /* reset our result fields just in case the fax tech driver wants to
  1499. * set custom error messages */
  1500. ast_string_field_set(details, result, "");
  1501. ast_string_field_set(details, resultstr, "");
  1502. ast_string_field_set(details, error, "");
  1503. details->is_t38_negotiated = t38negotiated;
  1504. set_channel_variables(chan, details);
  1505. if (fax->tech->start_session(fax) < 0) {
  1506. GENERIC_FAX_EXEC_ERROR(fax, chan, "INIT_ERROR", "failed to start FAX session");
  1507. }
  1508. report_fax_status(chan, details, "FAX Transmission In Progress");
  1509. ast_debug(5, "channel %s will wait on FAX fd %d\n", ast_channel_name(chan), fax->fd);
  1510. /* handle frames for the session */
  1511. remaining_time = timeout;
  1512. start = ast_tvnow();
  1513. while (remaining_time > 0) {
  1514. struct ast_channel *ready_chan;
  1515. int ofd, exception;
  1516. ms = 1000;
  1517. errno = 0;
  1518. ready_chan = ast_waitfor_nandfds(&c, chancount, &fax->fd, 1, &exception, &ofd, &ms);
  1519. if (ready_chan) {
  1520. if (!(frame = ast_read(chan))) {
  1521. /* the channel is probably gone, so lets stop polling on it and let the
  1522. * FAX session complete before we exit the application. if needed,
  1523. * send the FAX stack silence so the modems can finish their session without
  1524. * any problems */
  1525. ast_debug(1, "Channel '%s' did not return a frame; probably hung up.\n", ast_channel_name(chan));
  1526. GENERIC_FAX_EXEC_SET_VARS(fax, chan, "HANGUP", "remote channel hungup");
  1527. c = NULL;
  1528. chancount = 0;
  1529. remaining_time = ast_remaining_ms(start, timeout);
  1530. fax->tech->cancel_session(fax);
  1531. if (fax->tech->generate_silence) {
  1532. fax->tech->generate_silence(fax);
  1533. }
  1534. continue;
  1535. }
  1536. if ((frame->frametype == AST_FRAME_CONTROL) &&
  1537. (frame->subclass.integer == AST_CONTROL_T38_PARAMETERS) &&
  1538. (frame->datalen == sizeof(t38_parameters))) {
  1539. unsigned int was_t38 = t38negotiated;
  1540. struct ast_control_t38_parameters *parameters = frame->data.ptr;
  1541. switch (parameters->request_response) {
  1542. case AST_T38_REQUEST_NEGOTIATE:
  1543. /* the other end has requested a switch to T.38, so reply that we are willing, if we can
  1544. * do T.38 as well
  1545. */
  1546. t38_parameters_fax_to_ast(&t38_parameters, &details->our_t38_parameters);
  1547. if (details->caps & AST_FAX_TECH_T38) {
  1548. details->is_t38_negotiated = 1;
  1549. t38_parameters.request_response = AST_T38_NEGOTIATED;
  1550. } else {
  1551. t38_parameters.request_response = AST_T38_REFUSED;
  1552. }
  1553. ast_indicate_data(chan, AST_CONTROL_T38_PARAMETERS, &t38_parameters, sizeof(t38_parameters));
  1554. break;
  1555. case AST_T38_NEGOTIATED:
  1556. t38_parameters_ast_to_fax(&details->their_t38_parameters, parameters);
  1557. t38negotiated = 1;
  1558. details->is_t38_negotiated = 1;
  1559. break;
  1560. default:
  1561. break;
  1562. }
  1563. if (t38negotiated && !was_t38) {
  1564. if (fax->tech->switch_to_t38(fax)) {
  1565. GENERIC_FAX_EXEC_ERROR(fax, chan, "UNKNOWN", "T.38 switch failed");
  1566. break;
  1567. }
  1568. details->caps &= ~AST_FAX_TECH_AUDIO;
  1569. expected_frametype = AST_FRAME_MODEM;
  1570. expected_framesubclass.integer = AST_MODEM_T38;
  1571. if (fax->smoother) {
  1572. ast_smoother_free(fax->smoother);
  1573. fax->smoother = NULL;
  1574. }
  1575. report_fax_status(chan, details, "T.38 Negotiated");
  1576. ast_verb(3, "Channel '%s' switched to T.38 FAX session '%u'.\n", ast_channel_name(chan), fax->id);
  1577. }
  1578. } else if ((frame->frametype == expected_frametype) && (expected_framesubclass.integer == frame->subclass.integer) &&
  1579. ((!frame->subclass.format && !expected_framesubclass.format) ||
  1580. (frame->subclass.format && expected_framesubclass.format &&
  1581. (ast_format_cmp(frame->subclass.format, expected_framesubclass.format) != AST_FORMAT_CMP_NOT_EQUAL)))) {
  1582. struct ast_frame *f;
  1583. if (fax->smoother) {
  1584. /* push the frame into a smoother */
  1585. if (ast_smoother_feed(fax->smoother, frame) < 0) {
  1586. GENERIC_FAX_EXEC_ERROR(fax, chan, "UNKNOWN", "Failed to feed the smoother");
  1587. }
  1588. while ((f = ast_smoother_read(fax->smoother)) && (f->data.ptr)) {
  1589. if (fax->debug_info) {
  1590. debug_check_frame_for_silence(fax, 1, f);
  1591. }
  1592. /* write the frame to the FAX stack */
  1593. fax->tech->write(fax, f);
  1594. fax->frames_received++;
  1595. if (f != frame) {
  1596. ast_frfree(f);
  1597. }
  1598. }
  1599. } else {
  1600. /* write the frame to the FAX stack */
  1601. fax->tech->write(fax, frame);
  1602. fax->frames_received++;
  1603. }
  1604. start = ast_tvnow();
  1605. }
  1606. ast_frfree(frame);
  1607. } else if (ofd == fax->fd) {
  1608. /* read a frame from the FAX stack and send it out the channel.
  1609. * the FAX stack will return a NULL if the FAX session has already completed */
  1610. if (!(frame = fax->tech->read(fax))) {
  1611. break;
  1612. }
  1613. if (fax->debug_info && (frame->frametype == AST_FRAME_VOICE)) {
  1614. debug_check_frame_for_silence(fax, 0, frame);
  1615. }
  1616. ast_write(chan, frame);
  1617. fax->frames_sent++;
  1618. ast_frfree(frame);
  1619. start = ast_tvnow();
  1620. } else {
  1621. if (ms && (ofd < 0)) {
  1622. if ((errno == 0) || (errno == EINTR)) {
  1623. remaining_time = ast_remaining_ms(start, timeout);
  1624. if (remaining_time <= 0)
  1625. GENERIC_FAX_EXEC_ERROR(fax, chan, "TIMEOUT", "fax session timed-out");
  1626. continue;
  1627. } else {
  1628. ast_log(LOG_WARNING, "something bad happened while channel '%s' was polling.\n", ast_channel_name(chan));
  1629. GENERIC_FAX_EXEC_ERROR(fax, chan, "UNKNOWN", "error polling data");
  1630. break;
  1631. }
  1632. } else {
  1633. /* nothing happened */
  1634. remaining_time = ast_remaining_ms(start, timeout);
  1635. if (remaining_time <= 0) {
  1636. GENERIC_FAX_EXEC_ERROR(fax, chan, "TIMEOUT", "fax session timed-out");
  1637. break;
  1638. }
  1639. }
  1640. }
  1641. }
  1642. ast_debug(3, "channel '%s' - event loop stopped { timeout: %d, remaining_time: %d }\n", ast_channel_name(chan), timeout, remaining_time);
  1643. set_channel_variables(chan, details);
  1644. ast_atomic_fetchadd_int(&faxregistry.fax_complete, 1);
  1645. if (!strcasecmp(details->result, "FAILED")) {
  1646. ast_atomic_fetchadd_int(&faxregistry.fax_failures, 1);
  1647. }
  1648. if (fax) {
  1649. ao2_unlink(faxregistry.container, fax);
  1650. ao2_ref(fax, -1);
  1651. }
  1652. /* if the channel is still alive, and we changed its read/write formats,
  1653. * restore them now
  1654. */
  1655. if (chancount) {
  1656. if (orig_read_format) {
  1657. ast_set_read_format(chan, orig_read_format);
  1658. }
  1659. if (orig_write_format) {
  1660. ast_set_write_format(chan, orig_write_format);
  1661. }
  1662. }
  1663. /* return the chancount so the calling function can determine if the channel hungup during this FAX session or not */
  1664. return chancount;
  1665. }
  1666. static int receivefax_t38_init(struct ast_channel *chan, struct ast_fax_session_details *details)
  1667. {
  1668. int timeout_ms;
  1669. struct ast_frame *frame = NULL;
  1670. struct ast_control_t38_parameters t38_parameters;
  1671. struct timeval start;
  1672. int ms;
  1673. /* don't send any audio if we've already received a T.38 reinvite */
  1674. if (ast_channel_get_t38_state(chan) != T38_STATE_NEGOTIATING) {
  1675. /* generate 3 seconds of CED */
  1676. if (ast_playtones_start(chan, 1024, "!2100/3000", 1)) {
  1677. ast_log(LOG_ERROR, "error generating CED tone on %s\n", ast_channel_name(chan));
  1678. return -1;
  1679. }
  1680. timeout_ms = 3000;
  1681. start = ast_tvnow();
  1682. while ((ms = ast_remaining_ms(start, timeout_ms))) {
  1683. ms = ast_waitfor(chan, ms);
  1684. if (ms < 0) {
  1685. ast_log(LOG_ERROR, "error while generating CED tone on %s\n", ast_channel_name(chan));
  1686. ast_playtones_stop(chan);
  1687. return -1;
  1688. }
  1689. if (ms == 0) { /* all done, nothing happened */
  1690. break;
  1691. }
  1692. if (!(frame = ast_read(chan))) {
  1693. ast_log(LOG_ERROR, "error reading frame while generating CED tone on %s\n", ast_channel_name(chan));
  1694. ast_playtones_stop(chan);
  1695. return -1;
  1696. }
  1697. if ((frame->frametype == AST_FRAME_CONTROL) &&
  1698. (frame->subclass.integer == AST_CONTROL_T38_PARAMETERS) &&
  1699. (frame->datalen == sizeof(t38_parameters))) {
  1700. struct ast_control_t38_parameters *parameters = frame->data.ptr;
  1701. switch (parameters->request_response) {
  1702. case AST_T38_REQUEST_NEGOTIATE:
  1703. /* the other end has requested a switch to T.38, so reply that we are willing, if we can
  1704. * do T.38 as well
  1705. */
  1706. t38_parameters_fax_to_ast(&t38_parameters, &details->our_t38_parameters);
  1707. t38_parameters.request_response = (details->caps & AST_FAX_TECH_T38) ? AST_T38_NEGOTIATED : AST_T38_REFUSED;
  1708. ast_indicate_data(chan, AST_CONTROL_T38_PARAMETERS, &t38_parameters, sizeof(t38_parameters));
  1709. ast_playtones_stop(chan);
  1710. break;
  1711. case AST_T38_NEGOTIATED:
  1712. ast_debug(1, "Negotiated T.38 for receive on %s\n", ast_channel_name(chan));
  1713. t38_parameters_ast_to_fax(&details->their_t38_parameters, parameters);
  1714. details->caps &= ~AST_FAX_TECH_AUDIO;
  1715. report_fax_status(chan, details, "T.38 Negotiated");
  1716. break;
  1717. default:
  1718. break;
  1719. }
  1720. }
  1721. ast_frfree(frame);
  1722. }
  1723. ast_playtones_stop(chan);
  1724. }
  1725. /* if T.38 was negotiated, we are done initializing */
  1726. if (ast_channel_get_t38_state(chan) == T38_STATE_NEGOTIATED) {
  1727. return 0;
  1728. }
  1729. /* request T.38 */
  1730. ast_debug(1, "Negotiating T.38 for receive on %s\n", ast_channel_name(chan));
  1731. /* wait for negotiation to complete */
  1732. timeout_ms = details->t38timeout;
  1733. /* set parameters based on the session's parameters */
  1734. t38_parameters_fax_to_ast(&t38_parameters, &details->our_t38_parameters);
  1735. t38_parameters.request_response = AST_T38_REQUEST_NEGOTIATE;
  1736. if ((ast_indicate_data(chan, AST_CONTROL_T38_PARAMETERS, &t38_parameters, sizeof(t38_parameters)) != 0)) {
  1737. return -1;
  1738. }
  1739. start = ast_tvnow();
  1740. while ((ms = ast_remaining_ms(start, timeout_ms))) {
  1741. int break_loop = 0;
  1742. ms = ast_waitfor(chan, ms);
  1743. if (ms < 0) {
  1744. ast_log(LOG_WARNING, "error on '%s' while waiting for T.38 negotiation.\n", ast_channel_name(chan));
  1745. return -1;
  1746. }
  1747. if (ms == 0) { /* all done, nothing happened */
  1748. ast_log(LOG_WARNING, "channel '%s' timed-out during the T.38 negotiation.\n", ast_channel_name(chan));
  1749. details->caps &= ~AST_FAX_TECH_T38;
  1750. break;
  1751. }
  1752. if (!(frame = ast_read(chan))) {
  1753. ast_log(LOG_WARNING, "error on '%s' while waiting for T.38 negotiation.\n", ast_channel_name(chan));
  1754. return -1;
  1755. }
  1756. if ((frame->frametype == AST_FRAME_CONTROL) &&
  1757. (frame->subclass.integer == AST_CONTROL_T38_PARAMETERS) &&
  1758. (frame->datalen == sizeof(t38_parameters))) {
  1759. struct ast_control_t38_parameters *parameters = frame->data.ptr;
  1760. switch (parameters->request_response) {
  1761. case AST_T38_REQUEST_NEGOTIATE:
  1762. t38_parameters_fax_to_ast(&t38_parameters, &details->our_t38_parameters);
  1763. t38_parameters.request_response = AST_T38_NEGOTIATED;
  1764. ast_indicate_data(chan, AST_CONTROL_T38_PARAMETERS, &t38_parameters, sizeof(t38_parameters));
  1765. break;
  1766. case AST_T38_NEGOTIATED:
  1767. ast_debug(1, "Negotiated T.38 for receive on %s\n", ast_channel_name(chan));
  1768. t38_parameters_ast_to_fax(&details->their_t38_parameters, parameters);
  1769. details->caps &= ~AST_FAX_TECH_AUDIO;
  1770. report_fax_status(chan, details, "T.38 Negotiated");
  1771. break_loop = 1;
  1772. break;
  1773. case AST_T38_REFUSED:
  1774. ast_log(LOG_WARNING, "channel '%s' refused to negotiate T.38\n", ast_channel_name(chan));
  1775. details->caps &= ~AST_FAX_TECH_T38;
  1776. break_loop = 1;
  1777. break;
  1778. default:
  1779. ast_log(LOG_ERROR, "channel '%s' failed to negotiate T.38\n", ast_channel_name(chan));
  1780. details->caps &= ~AST_FAX_TECH_T38;
  1781. break_loop = 1;
  1782. break;
  1783. }
  1784. }
  1785. ast_frfree(frame);
  1786. if (break_loop) {
  1787. break;
  1788. }
  1789. }
  1790. /* if T.38 was negotiated, we are done initializing */
  1791. if (ast_channel_get_t38_state(chan) == T38_STATE_NEGOTIATED) {
  1792. return 0;
  1793. }
  1794. /* if we made it here, then T.38 failed, check the 'f' flag */
  1795. if (details->option.allow_audio != AST_FAX_OPTFLAG_TRUE) {
  1796. ast_log(LOG_WARNING, "Audio FAX not allowed on channel '%s' and T.38 negotiation failed; aborting.\n", ast_channel_name(chan));
  1797. return -1;
  1798. }
  1799. /* ok, audio fallback is allowed */
  1800. details->caps |= AST_FAX_TECH_AUDIO;
  1801. return 0;
  1802. }
  1803. /*! \brief Report on the final state of a receive fax operation
  1804. * \note This will lock the \ref ast_channel
  1805. */
  1806. static int report_receive_fax_status(struct ast_channel *chan, const char *filename)
  1807. {
  1808. RAII_VAR(struct ast_json *, json_object, NULL, ast_json_unref);
  1809. RAII_VAR(struct stasis_message *, message, NULL, ao2_cleanup);
  1810. RAII_VAR(struct ast_json *, json_array, ast_json_array_create(), ast_json_unref);
  1811. struct ast_json *json_filename = ast_json_string_create(filename);
  1812. if (!json_array || !json_filename) {
  1813. ast_json_unref(json_filename);
  1814. return -1;
  1815. }
  1816. ast_json_array_append(json_array, json_filename);
  1817. {
  1818. const char *remote_station_id;
  1819. const char *local_station_id;
  1820. const char *fax_pages;
  1821. const char *fax_resolution;
  1822. const char *fax_bitrate;
  1823. SCOPED_CHANNELLOCK(lock, chan);
  1824. remote_station_id = S_OR(pbx_builtin_getvar_helper(chan, "REMOTESTATIONID"), "");
  1825. if (!ast_strlen_zero(remote_station_id)) {
  1826. remote_station_id = ast_strdupa(remote_station_id);
  1827. }
  1828. local_station_id = S_OR(pbx_builtin_getvar_helper(chan, "LOCALSTATIONID"), "");
  1829. if (!ast_strlen_zero(local_station_id)) {
  1830. local_station_id = ast_strdupa(local_station_id);
  1831. }
  1832. fax_pages = S_OR(pbx_builtin_getvar_helper(chan, "FAXPAGES"), "");
  1833. if (!ast_strlen_zero(fax_pages)) {
  1834. fax_pages = ast_strdupa(fax_pages);
  1835. }
  1836. fax_resolution = S_OR(pbx_builtin_getvar_helper(chan, "FAXRESOLUTION"), "");
  1837. if (!ast_strlen_zero(fax_resolution)) {
  1838. fax_resolution = ast_strdupa(fax_resolution);
  1839. }
  1840. fax_bitrate = S_OR(pbx_builtin_getvar_helper(chan, "FAXBITRATE"), "");
  1841. if (!ast_strlen_zero(fax_bitrate)) {
  1842. fax_bitrate = ast_strdupa(fax_bitrate);
  1843. }
  1844. json_object = ast_json_pack("{s: s, s: s, s: s, s: s, s: s, s: s, s: o}",
  1845. "type", "receive",
  1846. "remote_station_id", S_OR(remote_station_id, ""),
  1847. "local_station_id", S_OR(local_station_id, ""),
  1848. "fax_pages", S_OR(fax_pages, ""),
  1849. "fax_resolution", S_OR(fax_resolution, ""),
  1850. "fax_bitrate", S_OR(fax_bitrate, ""),
  1851. "filenames", ast_json_ref(json_array));
  1852. if (!json_object) {
  1853. return -1;
  1854. }
  1855. message = ast_channel_blob_create_from_cache(ast_channel_uniqueid(chan), ast_channel_fax_type(), json_object);
  1856. if (!message) {
  1857. return -1;
  1858. }
  1859. stasis_publish(ast_channel_topic(chan), message);
  1860. }
  1861. return 0;
  1862. }
  1863. /*! \brief initiate a receive FAX session */
  1864. static int receivefax_exec(struct ast_channel *chan, const char *data)
  1865. {
  1866. char *parse, modems[128] = "";
  1867. int channel_alive;
  1868. RAII_VAR(struct ast_fax_session *, s, NULL, ao2_cleanup);
  1869. RAII_VAR(struct ast_fax_session_details *, details, NULL, ao2_cleanup);
  1870. struct ast_fax_tech_token *token = NULL;
  1871. struct ast_fax_document *doc;
  1872. AST_DECLARE_APP_ARGS(args,
  1873. AST_APP_ARG(filename);
  1874. AST_APP_ARG(options);
  1875. );
  1876. struct ast_flags opts = { 0, };
  1877. enum ast_t38_state t38state;
  1878. /* initialize output channel variables */
  1879. pbx_builtin_setvar_helper(chan, "FAXSTATUS", "FAILED");
  1880. pbx_builtin_setvar_helper(chan, "REMOTESTATIONID", NULL);
  1881. pbx_builtin_setvar_helper(chan, "FAXPAGES", "0");
  1882. pbx_builtin_setvar_helper(chan, "FAXBITRATE", NULL);
  1883. pbx_builtin_setvar_helper(chan, "FAXRESOLUTION", NULL);
  1884. pbx_builtin_setvar_helper(chan, "FAXMODE", NULL);
  1885. /* Get a FAX session details structure from the channel's FAX datastore and create one if
  1886. * it does not already exist. */
  1887. if (!(details = find_or_create_details(chan))) {
  1888. pbx_builtin_setvar_helper(chan, "FAXERROR", "MEMORY_ERROR");
  1889. pbx_builtin_setvar_helper(chan, "FAXSTATUSSTRING", "error allocating memory");
  1890. ast_log(LOG_ERROR, "System cannot provide memory for session requirements.\n");
  1891. return -1;
  1892. }
  1893. ast_string_field_set(details, result, "FAILED");
  1894. ast_string_field_set(details, resultstr, "error starting fax session");
  1895. ast_string_field_set(details, error, "INIT_ERROR");
  1896. set_channel_variables(chan, details);
  1897. if (details->gateway_id > 0) {
  1898. ast_string_field_set(details, resultstr, "can't receive a fax on a channel with a T.38 gateway");
  1899. set_channel_variables(chan, details);
  1900. ast_log(LOG_ERROR, "executing ReceiveFAX on a channel with a T.38 Gateway is not supported\n");
  1901. return -1;
  1902. }
  1903. if (details->maxrate < details->minrate) {
  1904. ast_string_field_set(details, error, "INVALID_ARGUMENTS");
  1905. ast_string_field_set(details, resultstr, "maxrate is less than minrate");
  1906. set_channel_variables(chan, details);
  1907. ast_log(LOG_ERROR, "maxrate %u is less than minrate %u\n", details->maxrate, details->minrate);
  1908. return -1;
  1909. }
  1910. if (check_modem_rate(details->modems, details->minrate)) {
  1911. ast_fax_modem_to_str(details->modems, modems, sizeof(modems));
  1912. ast_log(LOG_ERROR, "'modems' setting '%s' is incompatible with 'minrate' setting %u\n", modems, details->minrate);
  1913. ast_string_field_set(details, error, "INVALID_ARGUMENTS");
  1914. ast_string_field_set(details, resultstr, "incompatible 'modems' and 'minrate' settings");
  1915. set_channel_variables(chan, details);
  1916. return -1;
  1917. }
  1918. if (check_modem_rate(details->modems, details->maxrate)) {
  1919. ast_fax_modem_to_str(details->modems, modems, sizeof(modems));
  1920. ast_log(LOG_ERROR, "'modems' setting '%s' is incompatible with 'maxrate' setting %u\n", modems, details->maxrate);
  1921. ast_string_field_set(details, error, "INVALID_ARGUMENTS");
  1922. ast_string_field_set(details, resultstr, "incompatible 'modems' and 'maxrate' settings");
  1923. set_channel_variables(chan, details);
  1924. return -1;
  1925. }
  1926. if (ast_strlen_zero(data)) {
  1927. ast_string_field_set(details, error, "INVALID_ARGUMENTS");
  1928. ast_string_field_set(details, resultstr, "invalid arguments");
  1929. set_channel_variables(chan, details);
  1930. ast_log(LOG_WARNING, "%s requires an argument (filename[,options])\n", app_receivefax);
  1931. return -1;
  1932. }
  1933. parse = ast_strdupa(data);
  1934. AST_STANDARD_APP_ARGS(args, parse);
  1935. if (!ast_strlen_zero(args.options) &&
  1936. ast_app_parse_options(fax_exec_options, &opts, NULL, args.options)) {
  1937. ast_string_field_set(details, error, "INVALID_ARGUMENTS");
  1938. ast_string_field_set(details, resultstr, "invalid arguments");
  1939. set_channel_variables(chan, details);
  1940. return -1;
  1941. }
  1942. if (ast_strlen_zero(args.filename)) {
  1943. ast_string_field_set(details, error, "INVALID_ARGUMENTS");
  1944. ast_string_field_set(details, resultstr, "invalid arguments");
  1945. set_channel_variables(chan, details);
  1946. ast_log(LOG_WARNING, "%s requires an argument (filename[,options])\n", app_receivefax);
  1947. return -1;
  1948. }
  1949. /* check for unsupported FAX application options */
  1950. if (ast_test_flag(&opts, OPT_CALLERMODE) || ast_test_flag(&opts, OPT_CALLEDMODE)) {
  1951. ast_string_field_set(details, error, "INVALID_ARGUMENTS");
  1952. ast_string_field_set(details, resultstr, "invalid arguments");
  1953. set_channel_variables(chan, details);
  1954. ast_log(LOG_WARNING, "%s does not support polling\n", app_receivefax);
  1955. return -1;
  1956. }
  1957. ast_atomic_fetchadd_int(&faxregistry.fax_rx_attempts, 1);
  1958. pbx_builtin_setvar_helper(chan, "FAXERROR", "Channel Problems");
  1959. pbx_builtin_setvar_helper(chan, "FAXSTATUSSTRING", "Error before FAX transmission started.");
  1960. if (!(doc = ast_calloc(1, sizeof(*doc) + strlen(args.filename) + 1))) {
  1961. ast_string_field_set(details, error, "MEMORY_ERROR");
  1962. ast_string_field_set(details, resultstr, "error allocating memory");
  1963. set_channel_variables(chan, details);
  1964. ast_log(LOG_ERROR, "System cannot provide memory for session requirements.\n");
  1965. return -1;
  1966. }
  1967. strcpy(doc->filename, args.filename);
  1968. AST_LIST_INSERT_TAIL(&details->documents, doc, next);
  1969. ast_verb(3, "Channel '%s' receiving FAX '%s'\n", ast_channel_name(chan), args.filename);
  1970. details->caps = AST_FAX_TECH_RECEIVE;
  1971. details->option.send_ced = AST_FAX_OPTFLAG_TRUE;
  1972. /* check for debug */
  1973. if (ast_test_flag(&opts, OPT_DEBUG) || global_fax_debug) {
  1974. details->option.debug = AST_FAX_OPTFLAG_TRUE;
  1975. }
  1976. /* check for request for status events */
  1977. if (ast_test_flag(&opts, OPT_STATUS)) {
  1978. details->option.statusevents = AST_FAX_OPTFLAG_TRUE;
  1979. }
  1980. t38state = ast_channel_get_t38_state(chan);
  1981. if ((t38state == T38_STATE_UNAVAILABLE) || (t38state == T38_STATE_REJECTED) ||
  1982. ast_test_flag(&opts, OPT_ALLOWAUDIO) ||
  1983. ast_test_flag(&opts, OPT_FORCE_AUDIO)) {
  1984. details->option.allow_audio = AST_FAX_OPTFLAG_TRUE;
  1985. }
  1986. if (!(s = fax_session_reserve(details, &token))) {
  1987. ast_string_field_set(details, resultstr, "error reserving fax session");
  1988. set_channel_variables(chan, details);
  1989. ast_log(LOG_ERROR, "Unable to reserve FAX session.\n");
  1990. return -1;
  1991. }
  1992. /* make sure the channel is up */
  1993. if (ast_channel_state(chan) != AST_STATE_UP) {
  1994. if (ast_answer(chan)) {
  1995. ast_string_field_set(details, resultstr, "error answering channel");
  1996. set_channel_variables(chan, details);
  1997. ast_log(LOG_WARNING, "Channel '%s' failed answer attempt.\n", ast_channel_name(chan));
  1998. fax_session_release(s, token);
  1999. return -1;
  2000. }
  2001. }
  2002. if (!ast_test_flag(&opts, OPT_FORCE_AUDIO)) {
  2003. if (set_fax_t38_caps(chan, details)) {
  2004. ast_string_field_set(details, error, "T38_NEG_ERROR");
  2005. ast_string_field_set(details, resultstr, "error negotiating T.38");
  2006. set_channel_variables(chan, details);
  2007. fax_session_release(s, token);
  2008. return -1;
  2009. }
  2010. } else {
  2011. details->caps |= AST_FAX_TECH_AUDIO;
  2012. }
  2013. if (!ast_test_flag(&opts, OPT_FORCE_AUDIO) && (details->caps & AST_FAX_TECH_T38)) {
  2014. if (receivefax_t38_init(chan, details)) {
  2015. ast_string_field_set(details, error, "T38_NEG_ERROR");
  2016. ast_string_field_set(details, resultstr, "error negotiating T.38");
  2017. set_channel_variables(chan, details);
  2018. fax_session_release(s, token);
  2019. ast_log(LOG_ERROR, "error initializing channel '%s' in T.38 mode\n", ast_channel_name(chan));
  2020. return -1;
  2021. }
  2022. }
  2023. if ((channel_alive = generic_fax_exec(chan, details, s, token)) < 0) {
  2024. ast_atomic_fetchadd_int(&faxregistry.fax_failures, 1);
  2025. }
  2026. if (ast_channel_get_t38_state(chan) == T38_STATE_NEGOTIATED) {
  2027. if (disable_t38(chan)) {
  2028. ast_debug(1, "error disabling T.38 mode on %s\n", ast_channel_name(chan));
  2029. }
  2030. }
  2031. if (report_receive_fax_status(chan, args.filename)) {
  2032. ast_log(AST_LOG_ERROR, "Error publishing ReceiveFax status message\n");
  2033. }
  2034. /* If the channel hungup return -1; otherwise, return 0 to continue in the dialplan */
  2035. return (!channel_alive) ? -1 : 0;
  2036. }
  2037. static int sendfax_t38_init(struct ast_channel *chan, struct ast_fax_session_details *details)
  2038. {
  2039. int timeout_ms;
  2040. struct ast_frame *frame = NULL;
  2041. struct ast_control_t38_parameters t38_parameters;
  2042. struct timeval start;
  2043. int ms;
  2044. /* send CNG tone while listening for the receiver to initiate a switch
  2045. * to T.38 mode; if they do, stop sending the CNG tone and proceed with
  2046. * the switch.
  2047. *
  2048. * 10500 is enough time for 3 CNG tones
  2049. */
  2050. timeout_ms = 10500;
  2051. /* don't send any audio if we've already received a T.38 reinvite */
  2052. if (ast_channel_get_t38_state(chan) != T38_STATE_NEGOTIATING) {
  2053. if (ast_playtones_start(chan, 1024, "!1100/500,!0/3000,!1100/500,!0/3000,!1100/500,!0/3000", 1)) {
  2054. ast_log(LOG_ERROR, "error generating CNG tone on %s\n", ast_channel_name(chan));
  2055. return -1;
  2056. }
  2057. }
  2058. start = ast_tvnow();
  2059. while ((ms = ast_remaining_ms(start, timeout_ms))) {
  2060. int break_loop = 0;
  2061. ms = ast_waitfor(chan, ms);
  2062. if (ms < 0) {
  2063. ast_log(LOG_ERROR, "error while generating CNG tone on %s\n", ast_channel_name(chan));
  2064. ast_playtones_stop(chan);
  2065. return -1;
  2066. }
  2067. if (ms == 0) { /* all done, nothing happened */
  2068. break;
  2069. }
  2070. if (!(frame = ast_read(chan))) {
  2071. ast_log(LOG_ERROR, "error reading frame while generating CNG tone on %s\n", ast_channel_name(chan));
  2072. ast_playtones_stop(chan);
  2073. return -1;
  2074. }
  2075. if ((frame->frametype == AST_FRAME_CONTROL) &&
  2076. (frame->subclass.integer == AST_CONTROL_T38_PARAMETERS) &&
  2077. (frame->datalen == sizeof(t38_parameters))) {
  2078. struct ast_control_t38_parameters *parameters = frame->data.ptr;
  2079. switch (parameters->request_response) {
  2080. case AST_T38_REQUEST_NEGOTIATE:
  2081. /* the other end has requested a switch to T.38, so reply that we are willing, if we can
  2082. * do T.38 as well
  2083. */
  2084. t38_parameters_fax_to_ast(&t38_parameters, &details->our_t38_parameters);
  2085. t38_parameters.request_response = (details->caps & AST_FAX_TECH_T38) ? AST_T38_NEGOTIATED : AST_T38_REFUSED;
  2086. ast_indicate_data(chan, AST_CONTROL_T38_PARAMETERS, &t38_parameters, sizeof(t38_parameters));
  2087. ast_playtones_stop(chan);
  2088. break;
  2089. case AST_T38_NEGOTIATED:
  2090. ast_debug(1, "Negotiated T.38 for send on %s\n", ast_channel_name(chan));
  2091. t38_parameters_ast_to_fax(&details->their_t38_parameters, parameters);
  2092. details->caps &= ~AST_FAX_TECH_AUDIO;
  2093. report_fax_status(chan, details, "T.38 Negotiated");
  2094. break_loop = 1;
  2095. break;
  2096. default:
  2097. break;
  2098. }
  2099. }
  2100. ast_frfree(frame);
  2101. if (break_loop) {
  2102. break;
  2103. }
  2104. }
  2105. ast_playtones_stop(chan);
  2106. if (ast_channel_get_t38_state(chan) == T38_STATE_NEGOTIATED) {
  2107. return 0;
  2108. }
  2109. /* T.38 negotiation did not happen, initiate a switch if requested */
  2110. if (details->option.request_t38 == AST_FAX_OPTFLAG_TRUE) {
  2111. ast_debug(1, "Negotiating T.38 for send on %s\n", ast_channel_name(chan));
  2112. /* wait up to five seconds for negotiation to complete */
  2113. timeout_ms = 5000;
  2114. /* set parameters based on the session's parameters */
  2115. t38_parameters_fax_to_ast(&t38_parameters, &details->our_t38_parameters);
  2116. t38_parameters.request_response = AST_T38_REQUEST_NEGOTIATE;
  2117. if ((ast_indicate_data(chan, AST_CONTROL_T38_PARAMETERS, &t38_parameters, sizeof(t38_parameters)) != 0)) {
  2118. return -1;
  2119. }
  2120. start = ast_tvnow();
  2121. while ((ms = ast_remaining_ms(start, timeout_ms))) {
  2122. int break_loop = 0;
  2123. ms = ast_waitfor(chan, ms);
  2124. if (ms < 0) {
  2125. ast_log(LOG_WARNING, "error on '%s' while waiting for T.38 negotiation.\n", ast_channel_name(chan));
  2126. return -1;
  2127. }
  2128. if (ms == 0) { /* all done, nothing happened */
  2129. ast_log(LOG_WARNING, "channel '%s' timed-out during the T.38 negotiation.\n", ast_channel_name(chan));
  2130. details->caps &= ~AST_FAX_TECH_T38;
  2131. break;
  2132. }
  2133. if (!(frame = ast_read(chan))) {
  2134. ast_log(LOG_WARNING, "error on '%s' while waiting for T.38 negotiation.\n", ast_channel_name(chan));
  2135. return -1;
  2136. }
  2137. if ((frame->frametype == AST_FRAME_CONTROL) &&
  2138. (frame->subclass.integer == AST_CONTROL_T38_PARAMETERS) &&
  2139. (frame->datalen == sizeof(t38_parameters))) {
  2140. struct ast_control_t38_parameters *parameters = frame->data.ptr;
  2141. switch (parameters->request_response) {
  2142. case AST_T38_REQUEST_NEGOTIATE:
  2143. t38_parameters_fax_to_ast(&t38_parameters, &details->our_t38_parameters);
  2144. t38_parameters.request_response = AST_T38_NEGOTIATED;
  2145. ast_indicate_data(chan, AST_CONTROL_T38_PARAMETERS, &t38_parameters, sizeof(t38_parameters));
  2146. break;
  2147. case AST_T38_NEGOTIATED:
  2148. ast_debug(1, "Negotiated T.38 for receive on %s\n", ast_channel_name(chan));
  2149. t38_parameters_ast_to_fax(&details->their_t38_parameters, parameters);
  2150. details->caps &= ~AST_FAX_TECH_AUDIO;
  2151. report_fax_status(chan, details, "T.38 Negotiated");
  2152. break_loop = 1;
  2153. break;
  2154. case AST_T38_REFUSED:
  2155. ast_log(LOG_WARNING, "channel '%s' refused to negotiate T.38\n", ast_channel_name(chan));
  2156. details->caps &= ~AST_FAX_TECH_T38;
  2157. break_loop = 1;
  2158. break;
  2159. default:
  2160. ast_log(LOG_ERROR, "channel '%s' failed to negotiate T.38\n", ast_channel_name(chan));
  2161. details->caps &= ~AST_FAX_TECH_T38;
  2162. break_loop = 1;
  2163. break;
  2164. }
  2165. }
  2166. ast_frfree(frame);
  2167. if (break_loop) {
  2168. break;
  2169. }
  2170. }
  2171. /* if T.38 was negotiated, we are done initializing */
  2172. if (ast_channel_get_t38_state(chan) == T38_STATE_NEGOTIATED) {
  2173. return 0;
  2174. }
  2175. /* send one more CNG tone to get audio going again for some
  2176. * carriers if we are going to fall back to audio mode */
  2177. if (details->option.allow_audio == AST_FAX_OPTFLAG_TRUE) {
  2178. if (ast_playtones_start(chan, 1024, "!1100/500,!0/3000", 1)) {
  2179. ast_log(LOG_ERROR, "error generating second CNG tone on %s\n", ast_channel_name(chan));
  2180. return -1;
  2181. }
  2182. timeout_ms = 3500;
  2183. start = ast_tvnow();
  2184. while ((ms = ast_remaining_ms(start, timeout_ms))) {
  2185. int break_loop = 0;
  2186. ms = ast_waitfor(chan, ms);
  2187. if (ms < 0) {
  2188. ast_log(LOG_ERROR, "error while generating second CNG tone on %s\n", ast_channel_name(chan));
  2189. ast_playtones_stop(chan);
  2190. return -1;
  2191. }
  2192. if (ms == 0) { /* all done, nothing happened */
  2193. break;
  2194. }
  2195. if (!(frame = ast_read(chan))) {
  2196. ast_log(LOG_ERROR, "error reading frame while generating second CNG tone on %s\n", ast_channel_name(chan));
  2197. ast_playtones_stop(chan);
  2198. return -1;
  2199. }
  2200. if ((frame->frametype == AST_FRAME_CONTROL) &&
  2201. (frame->subclass.integer == AST_CONTROL_T38_PARAMETERS) &&
  2202. (frame->datalen == sizeof(t38_parameters))) {
  2203. struct ast_control_t38_parameters *parameters = frame->data.ptr;
  2204. switch (parameters->request_response) {
  2205. case AST_T38_REQUEST_NEGOTIATE:
  2206. /* the other end has requested a switch to T.38, so reply that we are willing, if we can
  2207. * do T.38 as well
  2208. */
  2209. t38_parameters_fax_to_ast(&t38_parameters, &details->our_t38_parameters);
  2210. t38_parameters.request_response = (details->caps & AST_FAX_TECH_T38) ? AST_T38_NEGOTIATED : AST_T38_REFUSED;
  2211. ast_indicate_data(chan, AST_CONTROL_T38_PARAMETERS, &t38_parameters, sizeof(t38_parameters));
  2212. ast_playtones_stop(chan);
  2213. break;
  2214. case AST_T38_NEGOTIATED:
  2215. ast_debug(1, "Negotiated T.38 for send on %s\n", ast_channel_name(chan));
  2216. t38_parameters_ast_to_fax(&details->their_t38_parameters, parameters);
  2217. details->caps &= ~AST_FAX_TECH_AUDIO;
  2218. report_fax_status(chan, details, "T.38 Negotiated");
  2219. break_loop = 1;
  2220. break;
  2221. default:
  2222. break;
  2223. }
  2224. }
  2225. ast_frfree(frame);
  2226. if (break_loop) {
  2227. break;
  2228. }
  2229. }
  2230. ast_playtones_stop(chan);
  2231. /* if T.38 was negotiated, we are done initializing */
  2232. if (ast_channel_get_t38_state(chan) == T38_STATE_NEGOTIATED) {
  2233. return 0;
  2234. }
  2235. }
  2236. }
  2237. /* if we made it here, then T.38 failed, check the 'f' flag */
  2238. if (details->option.allow_audio == AST_FAX_OPTFLAG_FALSE) {
  2239. ast_log(LOG_WARNING, "Audio FAX not allowed on channel '%s' and T.38 negotiation failed; aborting.\n", ast_channel_name(chan));
  2240. return -1;
  2241. }
  2242. /* ok, audio fallback is allowed */
  2243. details->caps |= AST_FAX_TECH_AUDIO;
  2244. return 0;
  2245. }
  2246. /*!
  2247. * \brief Report on the status of a completed fax send attempt
  2248. * \note This will lock the \ref ast_channel
  2249. */
  2250. static int report_send_fax_status(struct ast_channel *chan, struct ast_fax_session_details *details)
  2251. {
  2252. RAII_VAR(struct ast_json *, json_obj, NULL, ast_json_unref);
  2253. RAII_VAR(struct stasis_message *, message, NULL, ao2_cleanup);
  2254. struct ast_json *json_filenames;
  2255. json_filenames = generate_filenames_json(details);
  2256. if (!json_filenames) {
  2257. return -1;
  2258. }
  2259. {
  2260. const char *remote_station_id;
  2261. const char *local_station_id;
  2262. const char *fax_pages;
  2263. const char *fax_resolution;
  2264. const char *fax_bitrate;
  2265. SCOPED_CHANNELLOCK(lock, chan);
  2266. remote_station_id = S_OR(pbx_builtin_getvar_helper(chan, "REMOTESTATIONID"), "");
  2267. if (!ast_strlen_zero(remote_station_id)) {
  2268. remote_station_id = ast_strdupa(remote_station_id);
  2269. }
  2270. local_station_id = S_OR(pbx_builtin_getvar_helper(chan, "LOCALSTATIONID"), "");
  2271. if (!ast_strlen_zero(local_station_id)) {
  2272. local_station_id = ast_strdupa(local_station_id);
  2273. }
  2274. fax_pages = S_OR(pbx_builtin_getvar_helper(chan, "FAXPAGES"), "");
  2275. if (!ast_strlen_zero(fax_pages)) {
  2276. fax_pages = ast_strdupa(fax_pages);
  2277. }
  2278. fax_resolution = S_OR(pbx_builtin_getvar_helper(chan, "FAXRESOLUTION"), "");
  2279. if (!ast_strlen_zero(fax_resolution)) {
  2280. fax_resolution = ast_strdupa(fax_resolution);
  2281. }
  2282. fax_bitrate = S_OR(pbx_builtin_getvar_helper(chan, "FAXBITRATE"), "");
  2283. if (!ast_strlen_zero(fax_bitrate)) {
  2284. fax_bitrate = ast_strdupa(fax_bitrate);
  2285. }
  2286. json_obj = ast_json_pack("{s: s, s: s, s: s, s: s, s: s, s: s, s: o}",
  2287. "type", "send",
  2288. "remote_station_id", S_OR(remote_station_id, ""),
  2289. "local_station_id", S_OR(local_station_id, ""),
  2290. "fax_pages", S_OR(fax_pages, ""),
  2291. "fax_resolution", S_OR(fax_resolution, ""),
  2292. "fax_bitrate", S_OR(fax_bitrate, ""),
  2293. "filenames", json_filenames);
  2294. if (!json_obj) {
  2295. return -1;
  2296. }
  2297. message = ast_channel_blob_create_from_cache(ast_channel_uniqueid(chan), ast_channel_fax_type(), json_obj);
  2298. if (!message) {
  2299. return -1;
  2300. }
  2301. stasis_publish(ast_channel_topic(chan), message);
  2302. }
  2303. return 0;
  2304. }
  2305. /*! \brief initiate a send FAX session */
  2306. static int sendfax_exec(struct ast_channel *chan, const char *data)
  2307. {
  2308. char *parse, *filenames, *c, modems[128] = "";
  2309. int channel_alive, file_count;
  2310. RAII_VAR(struct ast_fax_session_details *, details, NULL, ao2_cleanup);
  2311. RAII_VAR(struct ast_fax_session *, s, NULL, ao2_cleanup);
  2312. struct ast_fax_tech_token *token = NULL;
  2313. struct ast_fax_document *doc;
  2314. AST_DECLARE_APP_ARGS(args,
  2315. AST_APP_ARG(filenames);
  2316. AST_APP_ARG(options);
  2317. );
  2318. struct ast_flags opts = { 0, };
  2319. enum ast_t38_state t38state;
  2320. /* initialize output channel variables */
  2321. pbx_builtin_setvar_helper(chan, "FAXSTATUS", "FAILED");
  2322. pbx_builtin_setvar_helper(chan, "REMOTESTATIONID", NULL);
  2323. pbx_builtin_setvar_helper(chan, "FAXPAGES", "0");
  2324. pbx_builtin_setvar_helper(chan, "FAXBITRATE", NULL);
  2325. pbx_builtin_setvar_helper(chan, "FAXRESOLUTION", NULL);
  2326. pbx_builtin_setvar_helper(chan, "FAXMODE", NULL);
  2327. /* Get a requirement structure and set it. This structure is used
  2328. * to tell the FAX technology module about the higher level FAX session */
  2329. if (!(details = find_or_create_details(chan))) {
  2330. pbx_builtin_setvar_helper(chan, "FAXERROR", "MEMORY_ERROR");
  2331. pbx_builtin_setvar_helper(chan, "FAXSTATUSSTRING", "error allocating memory");
  2332. ast_log(LOG_ERROR, "System cannot provide memory for session requirements.\n");
  2333. return -1;
  2334. }
  2335. ast_string_field_set(details, result, "FAILED");
  2336. ast_string_field_set(details, resultstr, "error starting fax session");
  2337. ast_string_field_set(details, error, "INIT_ERROR");
  2338. set_channel_variables(chan, details);
  2339. if (details->gateway_id > 0) {
  2340. ast_string_field_set(details, resultstr, "can't send a fax on a channel with a T.38 gateway");
  2341. set_channel_variables(chan, details);
  2342. ast_log(LOG_ERROR, "executing SendFAX on a channel with a T.38 Gateway is not supported\n");
  2343. return -1;
  2344. }
  2345. if (details->maxrate < details->minrate) {
  2346. ast_string_field_set(details, error, "INVALID_ARGUMENTS");
  2347. ast_string_field_set(details, resultstr, "maxrate is less than minrate");
  2348. set_channel_variables(chan, details);
  2349. ast_log(LOG_ERROR, "maxrate %u is less than minrate %u\n", details->maxrate, details->minrate);
  2350. return -1;
  2351. }
  2352. if (check_modem_rate(details->modems, details->minrate)) {
  2353. ast_fax_modem_to_str(details->modems, modems, sizeof(modems));
  2354. ast_log(LOG_ERROR, "'modems' setting '%s' is incompatible with 'minrate' setting %u\n", modems, details->minrate);
  2355. ast_string_field_set(details, error, "INVALID_ARGUMENTS");
  2356. ast_string_field_set(details, resultstr, "incompatible 'modems' and 'minrate' settings");
  2357. set_channel_variables(chan, details);
  2358. return -1;
  2359. }
  2360. if (check_modem_rate(details->modems, details->maxrate)) {
  2361. ast_fax_modem_to_str(details->modems, modems, sizeof(modems));
  2362. ast_log(LOG_ERROR, "'modems' setting '%s' is incompatible with 'maxrate' setting %u\n", modems, details->maxrate);
  2363. ast_string_field_set(details, error, "INVALID_ARGUMENTS");
  2364. ast_string_field_set(details, resultstr, "incompatible 'modems' and 'maxrate' settings");
  2365. set_channel_variables(chan, details);
  2366. return -1;
  2367. }
  2368. if (ast_strlen_zero(data)) {
  2369. ast_string_field_set(details, error, "INVALID_ARGUMENTS");
  2370. ast_string_field_set(details, resultstr, "invalid arguments");
  2371. set_channel_variables(chan, details);
  2372. ast_log(LOG_WARNING, "%s requires an argument (filename[&filename[&filename]][,options])\n", app_sendfax);
  2373. return -1;
  2374. }
  2375. parse = ast_strdupa(data);
  2376. AST_STANDARD_APP_ARGS(args, parse);
  2377. if (!ast_strlen_zero(args.options) &&
  2378. ast_app_parse_options(fax_exec_options, &opts, NULL, args.options)) {
  2379. ast_string_field_set(details, error, "INVALID_ARGUMENTS");
  2380. ast_string_field_set(details, resultstr, "invalid arguments");
  2381. set_channel_variables(chan, details);
  2382. return -1;
  2383. }
  2384. if (ast_strlen_zero(args.filenames)) {
  2385. ast_string_field_set(details, error, "INVALID_ARGUMENTS");
  2386. ast_string_field_set(details, resultstr, "invalid arguments");
  2387. set_channel_variables(chan, details);
  2388. ast_log(LOG_WARNING, "%s requires an argument (filename[&filename[&filename]],options])\n", app_sendfax);
  2389. return -1;
  2390. }
  2391. /* check for unsupported FAX application options */
  2392. if (ast_test_flag(&opts, OPT_CALLERMODE) || ast_test_flag(&opts, OPT_CALLEDMODE)) {
  2393. ast_string_field_set(details, error, "INVALID_ARGUMENTS");
  2394. ast_string_field_set(details, resultstr, "invalid arguments");
  2395. set_channel_variables(chan, details);
  2396. ast_log(LOG_WARNING, "%s does not support polling\n", app_sendfax);
  2397. return -1;
  2398. }
  2399. ast_atomic_fetchadd_int(&faxregistry.fax_tx_attempts, 1);
  2400. file_count = 0;
  2401. filenames = args.filenames;
  2402. while ((c = strsep(&filenames, "&"))) {
  2403. if (access(c, (F_OK | R_OK)) < 0) {
  2404. ast_string_field_set(details, error, "FILE_ERROR");
  2405. ast_string_field_set(details, resultstr, "error reading file");
  2406. set_channel_variables(chan, details);
  2407. ast_log(LOG_ERROR, "access failure. Verify '%s' exists and check permissions.\n", args.filenames);
  2408. return -1;
  2409. }
  2410. if (!(doc = ast_calloc(1, sizeof(*doc) + strlen(c) + 1))) {
  2411. ast_string_field_set(details, error, "MEMORY_ERROR");
  2412. ast_string_field_set(details, resultstr, "error allocating memory");
  2413. set_channel_variables(chan, details);
  2414. ast_log(LOG_ERROR, "System cannot provide memory for session requirements.\n");
  2415. return -1;
  2416. }
  2417. strcpy(doc->filename, c);
  2418. AST_LIST_INSERT_TAIL(&details->documents, doc, next);
  2419. file_count++;
  2420. }
  2421. ast_verb(3, "Channel '%s' sending FAX:\n", ast_channel_name(chan));
  2422. AST_LIST_TRAVERSE(&details->documents, doc, next) {
  2423. ast_verb(3, " %s\n", doc->filename);
  2424. }
  2425. details->caps = AST_FAX_TECH_SEND;
  2426. if (file_count > 1) {
  2427. details->caps |= AST_FAX_TECH_MULTI_DOC;
  2428. }
  2429. /* check for debug */
  2430. if (ast_test_flag(&opts, OPT_DEBUG) || global_fax_debug) {
  2431. details->option.debug = AST_FAX_OPTFLAG_TRUE;
  2432. }
  2433. /* check for request for status events */
  2434. if (ast_test_flag(&opts, OPT_STATUS)) {
  2435. details->option.statusevents = AST_FAX_OPTFLAG_TRUE;
  2436. }
  2437. t38state = ast_channel_get_t38_state(chan);
  2438. if ((t38state == T38_STATE_UNAVAILABLE) || (t38state == T38_STATE_REJECTED) ||
  2439. ast_test_flag(&opts, OPT_ALLOWAUDIO) ||
  2440. ast_test_flag(&opts, OPT_FORCE_AUDIO)) {
  2441. details->option.allow_audio = AST_FAX_OPTFLAG_TRUE;
  2442. }
  2443. if (ast_test_flag(&opts, OPT_REQUEST_T38)) {
  2444. details->option.request_t38 = AST_FAX_OPTFLAG_TRUE;
  2445. }
  2446. if (!(s = fax_session_reserve(details, &token))) {
  2447. ast_string_field_set(details, resultstr, "error reserving fax session");
  2448. set_channel_variables(chan, details);
  2449. ast_log(LOG_ERROR, "Unable to reserve FAX session.\n");
  2450. return -1;
  2451. }
  2452. /* make sure the channel is up */
  2453. if (ast_channel_state(chan) != AST_STATE_UP) {
  2454. if (ast_answer(chan)) {
  2455. ast_string_field_set(details, resultstr, "error answering channel");
  2456. set_channel_variables(chan, details);
  2457. ast_log(LOG_WARNING, "Channel '%s' failed answer attempt.\n", ast_channel_name(chan));
  2458. fax_session_release(s, token);
  2459. return -1;
  2460. }
  2461. }
  2462. if (!ast_test_flag(&opts, OPT_FORCE_AUDIO)) {
  2463. if (set_fax_t38_caps(chan, details)) {
  2464. ast_string_field_set(details, error, "T38_NEG_ERROR");
  2465. ast_string_field_set(details, resultstr, "error negotiating T.38");
  2466. set_channel_variables(chan, details);
  2467. fax_session_release(s, token);
  2468. return -1;
  2469. }
  2470. } else {
  2471. details->caps |= AST_FAX_TECH_AUDIO;
  2472. }
  2473. if (!ast_test_flag(&opts, OPT_FORCE_AUDIO) && (details->caps & AST_FAX_TECH_T38)) {
  2474. if (sendfax_t38_init(chan, details)) {
  2475. ast_string_field_set(details, error, "T38_NEG_ERROR");
  2476. ast_string_field_set(details, resultstr, "error negotiating T.38");
  2477. set_channel_variables(chan, details);
  2478. fax_session_release(s, token);
  2479. ast_log(LOG_ERROR, "error initializing channel '%s' in T.38 mode\n", ast_channel_name(chan));
  2480. return -1;
  2481. }
  2482. } else {
  2483. details->option.send_cng = 1;
  2484. }
  2485. if ((channel_alive = generic_fax_exec(chan, details, s, token)) < 0) {
  2486. ast_atomic_fetchadd_int(&faxregistry.fax_failures, 1);
  2487. }
  2488. if (ast_channel_get_t38_state(chan) == T38_STATE_NEGOTIATED) {
  2489. if (disable_t38(chan)) {
  2490. ast_debug(1, "error disabling T.38 mode on %s\n", ast_channel_name(chan));
  2491. }
  2492. }
  2493. if (!(filenames = generate_filenames_string(details, "FileName: ", "\r\n"))) {
  2494. ast_log(LOG_ERROR, "Error generating SendFAX manager event\n");
  2495. return (!channel_alive) ? -1 : 0;
  2496. }
  2497. /* send out the AMI completion event */
  2498. if (report_send_fax_status(chan, details)) {
  2499. ast_log(AST_LOG_ERROR, "Error publishing SendFAX status message\n");
  2500. }
  2501. /* If the channel hungup return -1; otherwise, return 0 to continue in the dialplan */
  2502. return (!channel_alive) ? -1 : 0;
  2503. }
  2504. /*! \brief destroy the v21 detection parts of a fax gateway session */
  2505. static void destroy_v21_sessions(struct fax_gateway *gateway)
  2506. {
  2507. if (gateway->chan_v21_session) {
  2508. ao2_unlink(faxregistry.container, gateway->chan_v21_session);
  2509. ao2_ref(gateway->chan_v21_session, -1);
  2510. gateway->chan_v21_session = NULL;
  2511. }
  2512. if (gateway->peer_v21_session) {
  2513. ao2_unlink(faxregistry.container, gateway->peer_v21_session);
  2514. ao2_ref(gateway->peer_v21_session, -1);
  2515. gateway->peer_v21_session = NULL;
  2516. }
  2517. }
  2518. /*! \brief destroy a FAX gateway session structure */
  2519. static void destroy_gateway(void *data)
  2520. {
  2521. struct fax_gateway *gateway = data;
  2522. destroy_v21_sessions(gateway);
  2523. if (gateway->s) {
  2524. fax_session_release(gateway->s, gateway->token);
  2525. gateway->token = NULL;
  2526. ao2_unlink(faxregistry.container, gateway->s);
  2527. ao2_ref(gateway->s, -1);
  2528. gateway->s = NULL;
  2529. }
  2530. ao2_cleanup(gateway->chan_read_format);
  2531. ao2_cleanup(gateway->chan_write_format);
  2532. ao2_cleanup(gateway->peer_read_format);
  2533. ao2_cleanup(gateway->peer_write_format);
  2534. }
  2535. static struct ast_fax_session *fax_v21_session_new (struct ast_channel *chan) {
  2536. struct ast_fax_session_details *v21_details;
  2537. struct ast_fax_session *v21_session;
  2538. if (!chan || !(v21_details = session_details_new())) {
  2539. return NULL;
  2540. }
  2541. v21_details->caps = AST_FAX_TECH_V21_DETECT;
  2542. v21_session = fax_session_new(v21_details, chan, NULL, NULL);
  2543. ao2_ref(v21_details, -1);
  2544. return v21_session;
  2545. }
  2546. /*! \brief Create a new fax gateway object.
  2547. * \param chan the channel the gateway object will be attached to
  2548. * \param details the fax session details
  2549. * \return NULL or a fax gateway object
  2550. */
  2551. static struct fax_gateway *fax_gateway_new(struct ast_channel *chan, struct ast_fax_session_details *details)
  2552. {
  2553. struct fax_gateway *gateway = ao2_alloc(sizeof(*gateway), destroy_gateway);
  2554. if (!gateway) {
  2555. return NULL;
  2556. }
  2557. if (!(gateway->chan_v21_session = fax_v21_session_new(chan))) {
  2558. ast_log(LOG_ERROR, "Can't create V21 session on chan %s for T.38 gateway session\n", ast_channel_name(chan));
  2559. ao2_ref(gateway, -1);
  2560. return NULL;
  2561. }
  2562. gateway->framehook = -1;
  2563. details->caps = AST_FAX_TECH_GATEWAY;
  2564. if (details->gateway_timeout && !(gateway->s = fax_session_reserve(details, &gateway->token))) {
  2565. details->caps &= ~AST_FAX_TECH_GATEWAY;
  2566. ast_log(LOG_ERROR, "Can't reserve a FAX session, gateway attempt failed.\n");
  2567. ao2_ref(gateway, -1);
  2568. return NULL;
  2569. }
  2570. return gateway;
  2571. }
  2572. /*!
  2573. * \brief Create a fax session and start T.30<->T.38 gateway mode
  2574. *
  2575. * \param gateway a fax gateway object
  2576. * \param details fax session details
  2577. * \param chan active channel
  2578. *
  2579. * \pre chan is locked on entry
  2580. *
  2581. * \return 0 on error 1 on success
  2582. */
  2583. static int fax_gateway_start(struct fax_gateway *gateway, struct ast_fax_session_details *details, struct ast_channel *chan)
  2584. {
  2585. struct ast_fax_session *s;
  2586. int start_res;
  2587. /* if the fax gateway is already started then do nothing */
  2588. if (gateway->s && gateway->s->state != AST_FAX_STATE_RESERVED) {
  2589. return 0;
  2590. }
  2591. /* create the FAX session */
  2592. if (!(s = fax_session_new(details, chan, gateway->s, gateway->token))) {
  2593. gateway->token = NULL;
  2594. ast_string_field_set(details, result, "FAILED");
  2595. ast_string_field_set(details, resultstr, "error starting gateway session");
  2596. ast_string_field_set(details, error, "INIT_ERROR");
  2597. details->is_t38_negotiated = 0;
  2598. set_channel_variables(chan, details);
  2599. report_fax_status(chan, details, "No Available Resource");
  2600. ast_log(LOG_ERROR, "Can't create a FAX session, gateway attempt failed.\n");
  2601. return -1;
  2602. }
  2603. /* release the reference for the reserved session and replace it with
  2604. * the real session */
  2605. if (gateway->s) {
  2606. ao2_ref(gateway->s, -1);
  2607. }
  2608. gateway->s = s;
  2609. gateway->token = NULL;
  2610. ast_channel_unlock(chan);
  2611. start_res = gateway->s->tech->start_session(gateway->s);
  2612. ast_channel_lock(chan);
  2613. if (start_res < 0) {
  2614. ast_string_field_set(details, result, "FAILED");
  2615. ast_string_field_set(details, resultstr, "error starting gateway session");
  2616. ast_string_field_set(details, error, "INIT_ERROR");
  2617. details->is_t38_negotiated = 0;
  2618. set_channel_variables(chan, details);
  2619. return -1;
  2620. }
  2621. gateway->timeout_start.tv_sec = 0;
  2622. gateway->timeout_start.tv_usec = 0;
  2623. report_fax_status(chan, details, "FAX Transmission In Progress");
  2624. return 0;
  2625. }
  2626. /*! \pre chan is locked on entry */
  2627. static struct ast_frame *fax_gateway_request_t38(struct fax_gateway *gateway, struct ast_channel *chan, struct ast_frame *f)
  2628. {
  2629. struct ast_frame *fp;
  2630. struct ast_control_t38_parameters t38_parameters = {
  2631. .request_response = AST_T38_REQUEST_NEGOTIATE,
  2632. };
  2633. struct ast_frame control_frame = {
  2634. .src = "res_fax",
  2635. .frametype = AST_FRAME_CONTROL,
  2636. .datalen = sizeof(t38_parameters),
  2637. .subclass.integer = AST_CONTROL_T38_PARAMETERS,
  2638. .data.ptr = &t38_parameters,
  2639. };
  2640. struct ast_fax_session_details *details = find_details(chan);
  2641. if (!details) {
  2642. ast_log(LOG_ERROR, "no FAX session details found on chan %s for T.38 gateway session, odd\n", ast_channel_name(chan));
  2643. ast_framehook_detach(chan, gateway->framehook);
  2644. return f;
  2645. }
  2646. t38_parameters_fax_to_ast(&t38_parameters, &details->our_t38_parameters);
  2647. ao2_ref(details, -1);
  2648. if (!(fp = ast_frisolate(&control_frame))) {
  2649. ast_log(LOG_ERROR, "error generating T.38 request control frame on chan %s for T.38 gateway session\n", ast_channel_name(chan));
  2650. return f;
  2651. }
  2652. gateway->t38_state = T38_STATE_NEGOTIATING;
  2653. gateway->timeout_start = ast_tvnow();
  2654. details->is_t38_negotiated = 0;
  2655. details->gateway_timeout = FAX_GATEWAY_TIMEOUT;
  2656. ast_debug(1, "requesting T.38 for gateway session for %s\n", ast_channel_name(chan));
  2657. return fp;
  2658. }
  2659. /*! \pre chan is locked on entry */
  2660. static struct ast_frame *fax_gateway_detect_v21(struct fax_gateway *gateway, struct ast_channel *chan, struct ast_channel *peer, struct ast_channel *active, struct ast_frame *f)
  2661. {
  2662. struct ast_channel *other = (active == chan) ? peer : chan;
  2663. struct ast_fax_session *active_v21_session = (active == chan) ? gateway->chan_v21_session : gateway->peer_v21_session;
  2664. if (!active_v21_session || gateway->detected_v21) {
  2665. return f;
  2666. }
  2667. if (active_v21_session->tech->write(active_v21_session, f) == 0 &&
  2668. active_v21_session->details->option.v21_detected) {
  2669. gateway->detected_v21 = 1;
  2670. }
  2671. if (gateway->detected_v21) {
  2672. enum ast_t38_state state_other;
  2673. destroy_v21_sessions(gateway);
  2674. ast_channel_unlock(chan);
  2675. state_other = ast_channel_get_t38_state(other);
  2676. ast_channel_lock(chan);
  2677. if (state_other == T38_STATE_UNKNOWN) {
  2678. ast_debug(1, "detected v21 preamble from %s\n", ast_channel_name(active));
  2679. return fax_gateway_request_t38(gateway, chan, f);
  2680. } else {
  2681. ast_debug(1, "detected v21 preamble on %s, but %s does not support T.38 for T.38 gateway session\n", ast_channel_name(active), ast_channel_name(other));
  2682. }
  2683. }
  2684. return f;
  2685. }
  2686. /*! \pre chan is locked on entry */
  2687. static void fax_gateway_indicate_t38(struct ast_channel *chan, struct ast_channel *active, struct ast_control_t38_parameters *control_params)
  2688. {
  2689. if (active == chan) {
  2690. ast_channel_unlock(chan);
  2691. ast_indicate_data(chan, AST_CONTROL_T38_PARAMETERS, control_params, sizeof(*control_params));
  2692. ast_channel_lock(chan);
  2693. } else {
  2694. ast_queue_control_data(chan, AST_CONTROL_T38_PARAMETERS, control_params, sizeof(*control_params));
  2695. }
  2696. }
  2697. /*!
  2698. * \brief T38 Gateway Negotiate t38 parameters
  2699. *
  2700. * \param gateway gateway object
  2701. * \param chan channel running the gateway
  2702. * \param peer channel im bridged too
  2703. * \param active channel the frame originated on
  2704. * \param f the control frame to process
  2705. *
  2706. * \pre chan is locked on entry
  2707. *
  2708. * \return processed control frame or null frame
  2709. */
  2710. static struct ast_frame *fax_gateway_detect_t38(struct fax_gateway *gateway, struct ast_channel *chan, struct ast_channel *peer, struct ast_channel *active, struct ast_frame *f)
  2711. {
  2712. struct ast_control_t38_parameters *control_params = f->data.ptr;
  2713. struct ast_channel *other = (active == chan) ? peer : chan;
  2714. struct ast_fax_session_details *details;
  2715. enum ast_t38_state state_other;
  2716. if (f->datalen != sizeof(struct ast_control_t38_parameters)) {
  2717. /* invalaid AST_CONTROL_T38_PARAMETERS frame, we can't
  2718. * do anything with it, pass it on */
  2719. return f;
  2720. }
  2721. /* ignore frames from ourselves */
  2722. if ((gateway->t38_state == T38_STATE_NEGOTIATED && control_params->request_response == AST_T38_NEGOTIATED)
  2723. || (gateway->t38_state == T38_STATE_REJECTED && control_params->request_response == AST_T38_REFUSED)
  2724. || (gateway->t38_state == T38_STATE_NEGOTIATING && control_params->request_response == AST_T38_REQUEST_TERMINATE)) {
  2725. return f;
  2726. }
  2727. if (!(details = find_details(chan))) {
  2728. ast_log(LOG_ERROR, "no FAX session details found on chan %s for T.38 gateway session, odd\n", ast_channel_name(chan));
  2729. ast_framehook_detach(chan, gateway->framehook);
  2730. return f;
  2731. }
  2732. if (control_params->request_response == AST_T38_REQUEST_NEGOTIATE) {
  2733. ast_channel_unlock(chan);
  2734. state_other = ast_channel_get_t38_state(other);
  2735. ast_channel_lock(chan);
  2736. if (state_other == T38_STATE_UNKNOWN) {
  2737. /* we detected a request to negotiate T.38 and the
  2738. * other channel appears to support T.38, we'll pass
  2739. * the request through and only step in if the other
  2740. * channel rejects the request */
  2741. ast_debug(1, "%s is attempting to negotiate T.38 with %s, we'll see what happens\n", ast_channel_name(active), ast_channel_name(other));
  2742. t38_parameters_ast_to_fax(&details->their_t38_parameters, control_params);
  2743. gateway->t38_state = T38_STATE_UNKNOWN;
  2744. gateway->timeout_start = ast_tvnow();
  2745. details->is_t38_negotiated = 0;
  2746. details->gateway_timeout = FAX_GATEWAY_TIMEOUT;
  2747. ao2_ref(details, -1);
  2748. return f;
  2749. } else if (state_other == T38_STATE_UNAVAILABLE || state_other == T38_STATE_REJECTED) {
  2750. /* the other channel does not support T.38, we need to
  2751. * step in here */
  2752. ast_debug(1, "%s is attempting to negotiate T.38 but %s does not support it\n", ast_channel_name(active), ast_channel_name(other));
  2753. ast_debug(1, "starting T.38 gateway for T.38 channel %s and G.711 channel %s\n", ast_channel_name(active), ast_channel_name(other));
  2754. t38_parameters_ast_to_fax(&details->their_t38_parameters, control_params);
  2755. t38_parameters_fax_to_ast(control_params, &details->our_t38_parameters);
  2756. if (fax_gateway_start(gateway, details, chan)) {
  2757. ast_log(LOG_ERROR, "error starting T.38 gateway for T.38 channel %s and G.711 channel %s\n", ast_channel_name(active), ast_channel_name(other));
  2758. gateway->t38_state = T38_STATE_REJECTED;
  2759. details->is_t38_negotiated = 0;
  2760. control_params->request_response = AST_T38_REFUSED;
  2761. ast_framehook_detach(chan, details->gateway_id);
  2762. details->gateway_id = -1;
  2763. } else {
  2764. gateway->t38_state = T38_STATE_NEGOTIATED;
  2765. details->is_t38_negotiated = chan == active;
  2766. control_params->request_response = AST_T38_NEGOTIATED;
  2767. report_fax_status(chan, details, "T.38 Negotiated");
  2768. }
  2769. fax_gateway_indicate_t38(chan, active, control_params);
  2770. ao2_ref(details, -1);
  2771. return &ast_null_frame;
  2772. } else if (gateway->t38_state == T38_STATE_NEGOTIATING) {
  2773. /* we got a request to negotiate T.38 after we already
  2774. * sent one to the other party based on v21 preamble
  2775. * detection. We'll just pretend we passed this request
  2776. * through in the first place. */
  2777. t38_parameters_ast_to_fax(&details->their_t38_parameters, control_params);
  2778. gateway->t38_state = T38_STATE_UNKNOWN;
  2779. gateway->timeout_start = ast_tvnow();
  2780. details->is_t38_negotiated = 0;
  2781. details->gateway_timeout = FAX_GATEWAY_TIMEOUT;
  2782. ast_debug(1, "%s is attempting to negotiate T.38 after we already sent a negotiation request based on v21 preamble detection\n", ast_channel_name(active));
  2783. ao2_ref(details, -1);
  2784. return &ast_null_frame;
  2785. } else if (gateway->t38_state == T38_STATE_NEGOTIATED) {
  2786. /* we got a request to negotiate T.38 after we already
  2787. * sent one to the other party based on v21 preamble
  2788. * detection and received a response. We need to
  2789. * respond to this and shut down the gateway. */
  2790. t38_parameters_fax_to_ast(control_params, &details->their_t38_parameters);
  2791. ast_framehook_detach(chan, details->gateway_id);
  2792. details->gateway_id = -1;
  2793. control_params->request_response = AST_T38_NEGOTIATED;
  2794. fax_gateway_indicate_t38(chan, active, control_params);
  2795. ast_string_field_set(details, result, "SUCCESS");
  2796. ast_string_field_set(details, resultstr, "no gateway necessary");
  2797. ast_string_field_set(details, error, "NATIVE_T38");
  2798. details->is_t38_negotiated = 1;
  2799. set_channel_variables(chan, details);
  2800. ast_debug(1, "%s is attempting to negotiate T.38 after we already negotiated T.38 with %s, disabling the gateway\n", ast_channel_name(active), ast_channel_name(other));
  2801. ao2_ref(details, -1);
  2802. return &ast_null_frame;
  2803. } else {
  2804. ast_log(LOG_WARNING, "%s is attempting to negotiate T.38 while %s is in an unsupported state\n", ast_channel_name(active), ast_channel_name(other));
  2805. ao2_ref(details, -1);
  2806. return f;
  2807. }
  2808. } else if (gateway->t38_state == T38_STATE_NEGOTIATING
  2809. && control_params->request_response == AST_T38_REFUSED) {
  2810. ast_debug(1, "unable to negotiate T.38 on %s for fax gateway\n", ast_channel_name(active));
  2811. details->is_t38_negotiated = 0;
  2812. /* our request to negotiate T.38 was refused, if the other
  2813. * channel supports T.38, they might still reinvite and save
  2814. * the day. Otherwise disable the gateway. */
  2815. ast_channel_unlock(chan);
  2816. state_other = ast_channel_get_t38_state(other);
  2817. ast_channel_lock(chan);
  2818. if (state_other == T38_STATE_UNKNOWN) {
  2819. gateway->t38_state = T38_STATE_UNAVAILABLE;
  2820. } else {
  2821. ast_framehook_detach(chan, details->gateway_id);
  2822. details->gateway_id = -1;
  2823. ast_string_field_set(details, result, "FAILED");
  2824. ast_string_field_set(details, resultstr, "unable to negotiate T.38");
  2825. ast_string_field_set(details, error, "T38_NEG_ERROR");
  2826. set_channel_variables(chan, details);
  2827. }
  2828. ao2_ref(details, -1);
  2829. return &ast_null_frame;
  2830. } else if (gateway->t38_state == T38_STATE_NEGOTIATING
  2831. && control_params->request_response == AST_T38_NEGOTIATED) {
  2832. ast_debug(1, "starting T.38 gateway for T.38 channel %s and G.711 channel %s\n", ast_channel_name(active), ast_channel_name(other));
  2833. t38_parameters_ast_to_fax(&details->their_t38_parameters, control_params);
  2834. if (fax_gateway_start(gateway, details, chan)) {
  2835. ast_log(LOG_ERROR, "error starting T.38 gateway for T.38 channel %s and G.711 channel %s\n", ast_channel_name(active), ast_channel_name(other));
  2836. gateway->t38_state = T38_STATE_NEGOTIATING;
  2837. details->is_t38_negotiated = 0;
  2838. control_params->request_response = AST_T38_REQUEST_TERMINATE;
  2839. fax_gateway_indicate_t38(chan, active, control_params);
  2840. } else {
  2841. gateway->t38_state = T38_STATE_NEGOTIATED;
  2842. details->is_t38_negotiated = chan == active;
  2843. report_fax_status(chan, details, "T.38 Negotiated");
  2844. }
  2845. ao2_ref(details, -1);
  2846. return &ast_null_frame;
  2847. } else if (control_params->request_response == AST_T38_REFUSED) {
  2848. /* the other channel refused the request to negotiate T.38,
  2849. * we'll step in here and pretend the request was accepted */
  2850. ast_debug(1, "%s attempted to negotiate T.38 but %s refused the request\n", ast_channel_name(other), ast_channel_name(active));
  2851. ast_debug(1, "starting T.38 gateway for T.38 channel %s and G.711 channel %s\n", ast_channel_name(other), ast_channel_name(active));
  2852. t38_parameters_fax_to_ast(control_params, &details->our_t38_parameters);
  2853. if (fax_gateway_start(gateway, details, chan)) {
  2854. ast_log(LOG_ERROR, "error starting T.38 gateway for T.38 channel %s and G.711 channel %s\n", ast_channel_name(other), ast_channel_name(active));
  2855. gateway->t38_state = T38_STATE_REJECTED;
  2856. details->is_t38_negotiated = 0;
  2857. control_params->request_response = AST_T38_REFUSED;
  2858. ast_framehook_detach(chan, details->gateway_id);
  2859. details->gateway_id = -1;
  2860. } else {
  2861. gateway->t38_state = T38_STATE_NEGOTIATED;
  2862. details->is_t38_negotiated = chan == other;
  2863. control_params->request_response = AST_T38_NEGOTIATED;
  2864. }
  2865. ao2_ref(details, -1);
  2866. return f;
  2867. } else if (control_params->request_response == AST_T38_REQUEST_TERMINATE) {
  2868. /* the channel wishes to end our short relationship, we shall
  2869. * oblige */
  2870. ast_debug(1, "T.38 channel %s is requesting a shutdown of T.38, disabling the gateway\n", ast_channel_name(active));
  2871. ast_framehook_detach(chan, details->gateway_id);
  2872. details->gateway_id = -1;
  2873. gateway->t38_state = T38_STATE_REJECTED;
  2874. details->is_t38_negotiated = 0;
  2875. control_params->request_response = AST_T38_TERMINATED;
  2876. fax_gateway_indicate_t38(chan, active, control_params);
  2877. ao2_ref(details, -1);
  2878. return &ast_null_frame;
  2879. } else if (control_params->request_response == AST_T38_NEGOTIATED) {
  2880. ast_debug(1, "T.38 successfully negotiated between %s and %s, no gateway necessary\n", ast_channel_name(active), ast_channel_name(other));
  2881. ast_framehook_detach(chan, details->gateway_id);
  2882. details->gateway_id = -1;
  2883. ast_string_field_set(details, result, "SUCCESS");
  2884. ast_string_field_set(details, resultstr, "no gateway necessary");
  2885. ast_string_field_set(details, error, "NATIVE_T38");
  2886. details->is_t38_negotiated = 1;
  2887. set_channel_variables(chan, details);
  2888. ao2_ref(details, -1);
  2889. return f;
  2890. } else if (control_params->request_response == AST_T38_TERMINATED) {
  2891. ast_debug(1, "T.38 disabled on channel %s\n", ast_channel_name(active));
  2892. ast_framehook_detach(chan, details->gateway_id);
  2893. details->gateway_id = -1;
  2894. ao2_ref(details, -1);
  2895. return &ast_null_frame;
  2896. }
  2897. ao2_ref(details, -1);
  2898. return f;
  2899. }
  2900. /*! \brief Destroy the gateway data structure when the framehook is detached
  2901. * \param data framehook data (gateway data)*/
  2902. static void fax_gateway_framehook_destroy(void *data)
  2903. {
  2904. struct fax_gateway *gateway = data;
  2905. if (gateway->s) {
  2906. switch (gateway->s->state) {
  2907. case AST_FAX_STATE_INITIALIZED:
  2908. case AST_FAX_STATE_OPEN:
  2909. case AST_FAX_STATE_ACTIVE:
  2910. case AST_FAX_STATE_COMPLETE:
  2911. if (gateway->s->tech->cancel_session) {
  2912. gateway->s->tech->cancel_session(gateway->s);
  2913. }
  2914. /* fall through */
  2915. default:
  2916. break;
  2917. }
  2918. }
  2919. ao2_ref(gateway, -1);
  2920. }
  2921. /*!
  2922. * \brief T.30<->T.38 gateway framehook.
  2923. *
  2924. * Intercept packets on bridged channels and determine if a T.38 gateway is
  2925. * required. If a gateway is required, start a gateway and handle T.38
  2926. * negotiation if necessary.
  2927. *
  2928. * \param chan channel running the gateway
  2929. * \param f frame to handle may be NULL
  2930. * \param event framehook event
  2931. * \param data framehook data (struct fax_gateway *)
  2932. *
  2933. * \pre chan is locked on entry
  2934. *
  2935. * \return processed frame or NULL when f is NULL or a null frame
  2936. */
  2937. static struct ast_frame *fax_gateway_framehook(struct ast_channel *chan, struct ast_frame *f, enum ast_framehook_event event, void *data)
  2938. {
  2939. struct fax_gateway *gateway = data;
  2940. struct ast_channel *active;
  2941. RAII_VAR(struct ast_fax_session_details *, details, NULL, ao2_cleanup);
  2942. RAII_VAR(struct ast_channel *, peer, NULL, ao2_cleanup);
  2943. RAII_VAR(struct ast_channel *, chan_ref, chan, ao2_cleanup);
  2944. /* Ref bump channel for when we have to unlock it */
  2945. ao2_ref(chan_ref, 1);
  2946. if (gateway->s) {
  2947. details = gateway->s->details;
  2948. ao2_ref(details, 1);
  2949. } else {
  2950. if (!(details = find_details(chan))) {
  2951. ast_log(LOG_ERROR, "no FAX session details found on chan %s for T.38 gateway session, odd\n", ast_channel_name(chan));
  2952. ast_framehook_detach(chan, gateway->framehook);
  2953. return f;
  2954. }
  2955. }
  2956. /* restore audio formats when we are detached */
  2957. if (event == AST_FRAMEHOOK_EVENT_DETACHED) {
  2958. set_channel_variables(chan, details);
  2959. if (gateway->bridged) {
  2960. ast_set_read_format(chan, gateway->chan_read_format);
  2961. ast_set_write_format(chan, gateway->chan_write_format);
  2962. ast_channel_unlock(chan);
  2963. peer = ast_channel_bridge_peer(chan);
  2964. if (peer) {
  2965. ast_set_read_format(peer, gateway->peer_read_format);
  2966. ast_set_write_format(peer, gateway->peer_write_format);
  2967. ast_channel_make_compatible(chan, peer);
  2968. }
  2969. ast_channel_lock(chan);
  2970. }
  2971. return NULL;
  2972. }
  2973. if (!f || (event == AST_FRAMEHOOK_EVENT_ATTACHED)) {
  2974. return NULL;
  2975. };
  2976. /* this frame was generated by the fax gateway, pass it on */
  2977. if (ast_test_flag(f, AST_FAX_FRFLAG_GATEWAY)) {
  2978. return f;
  2979. }
  2980. /* If we aren't bridged or we don't have a peer, don't do anything */
  2981. ast_channel_unlock(chan);
  2982. peer = ast_channel_bridge_peer(chan);
  2983. ast_channel_lock(chan);
  2984. if (!peer) {
  2985. return f;
  2986. }
  2987. if (!gateway->bridged) {
  2988. enum ast_t38_state state_chan;
  2989. enum ast_t38_state state_peer;
  2990. ast_channel_unlock(chan);
  2991. state_chan = ast_channel_get_t38_state(chan);
  2992. state_peer = ast_channel_get_t38_state(peer);
  2993. ast_channel_lock(chan);
  2994. /* don't start a gateway if neither channel can handle T.38 */
  2995. if (state_chan == T38_STATE_UNAVAILABLE && state_peer == T38_STATE_UNAVAILABLE) {
  2996. ast_debug(1, "not starting gateway for %s and %s; neither channel supports T.38\n", ast_channel_name(chan), ast_channel_name(peer));
  2997. ast_framehook_detach(chan, gateway->framehook);
  2998. details->gateway_id = -1;
  2999. ast_string_field_set(details, result, "FAILED");
  3000. ast_string_field_set(details, resultstr, "neither channel supports T.38");
  3001. ast_string_field_set(details, error, "T38_NEG_ERROR");
  3002. details->is_t38_negotiated = 0;
  3003. set_channel_variables(chan, details);
  3004. return f;
  3005. }
  3006. if (details->gateway_timeout) {
  3007. gateway->timeout_start = ast_tvnow();
  3008. }
  3009. ast_channel_unlock(chan);
  3010. ast_channel_lock_both(chan, peer);
  3011. /* we are bridged, change r/w formats to SLIN for v21 preamble
  3012. * detection and T.30 */
  3013. ao2_replace(gateway->chan_read_format, ast_channel_readformat(chan));
  3014. ao2_replace(gateway->chan_write_format, ast_channel_writeformat(chan));
  3015. ao2_replace(gateway->peer_read_format, ast_channel_readformat(peer));
  3016. ao2_replace(gateway->peer_write_format, ast_channel_writeformat(peer));
  3017. ast_set_read_format(chan, ast_format_slin);
  3018. ast_set_write_format(chan, ast_format_slin);
  3019. ast_set_read_format(peer, ast_format_slin);
  3020. ast_set_write_format(peer, ast_format_slin);
  3021. ast_channel_unlock(peer);
  3022. gateway->bridged = 1;
  3023. if (!(gateway->peer_v21_session = fax_v21_session_new(peer))) {
  3024. ast_log(LOG_ERROR, "Can't create V21 session on chan %s for T.38 gateway session\n", ast_channel_name(peer));
  3025. ast_framehook_detach(chan, gateway->framehook);
  3026. return f;
  3027. }
  3028. }
  3029. if (gateway->bridged && !ast_tvzero(gateway->timeout_start)) {
  3030. if (ast_tvdiff_ms(ast_tvnow(), gateway->timeout_start) > details->gateway_timeout) {
  3031. ast_debug(1, "no fax activity between %s and %s after %d ms, disabling gateway\n", ast_channel_name(chan), ast_channel_name(peer), details->gateway_timeout);
  3032. ast_framehook_detach(chan, gateway->framehook);
  3033. details->gateway_id = -1;
  3034. ast_string_field_set(details, result, "FAILED");
  3035. ast_string_field_build(details, resultstr, "no fax activity after %d ms", details->gateway_timeout);
  3036. ast_string_field_set(details, error, "TIMEOUT");
  3037. details->is_t38_negotiated = 0;
  3038. set_channel_variables(chan, details);
  3039. return f;
  3040. }
  3041. }
  3042. /* only handle VOICE, MODEM, and CONTROL frames*/
  3043. switch (f->frametype) {
  3044. case AST_FRAME_VOICE:
  3045. if ((ast_format_cmp(f->subclass.format, ast_format_slin) != AST_FORMAT_CMP_EQUAL) &&
  3046. (ast_format_cmp(f->subclass.format, ast_format_alaw) != AST_FORMAT_CMP_EQUAL) &&
  3047. (ast_format_cmp(f->subclass.format, ast_format_ulaw) != AST_FORMAT_CMP_EQUAL)) {
  3048. return f;
  3049. }
  3050. break;
  3051. case AST_FRAME_MODEM:
  3052. if (f->subclass.integer == AST_MODEM_T38) {
  3053. break;
  3054. }
  3055. return f;
  3056. case AST_FRAME_CONTROL:
  3057. if (f->subclass.integer == AST_CONTROL_T38_PARAMETERS) {
  3058. break;
  3059. }
  3060. return f;
  3061. default:
  3062. return f;
  3063. }
  3064. /* detect the active channel */
  3065. switch (event) {
  3066. case AST_FRAMEHOOK_EVENT_WRITE:
  3067. active = peer;
  3068. break;
  3069. case AST_FRAMEHOOK_EVENT_READ:
  3070. active = chan;
  3071. break;
  3072. default:
  3073. ast_log(LOG_WARNING, "unhandled framehook event %u\n", event);
  3074. return f;
  3075. }
  3076. /* handle control frames */
  3077. if (f->frametype == AST_FRAME_CONTROL && f->subclass.integer == AST_CONTROL_T38_PARAMETERS) {
  3078. return fax_gateway_detect_t38(gateway, chan, peer, active, f);
  3079. }
  3080. if (!gateway->detected_v21 && gateway->t38_state == T38_STATE_UNAVAILABLE && f->frametype == AST_FRAME_VOICE) {
  3081. /* not in gateway mode and have not detected v21 yet, listen
  3082. * for v21 */
  3083. return fax_gateway_detect_v21(gateway, chan, peer, active, f);
  3084. }
  3085. /* in gateway mode, gateway some packets */
  3086. if (gateway->t38_state == T38_STATE_NEGOTIATED) {
  3087. struct ast_trans_pvt *readtrans;
  3088. /* framehooks are called in __ast_read() before frame format
  3089. * translation is done, so we need to translate here */
  3090. if ((f->frametype == AST_FRAME_VOICE) && (ast_format_cmp(f->subclass.format, ast_format_slin) != AST_FORMAT_CMP_EQUAL)
  3091. && (readtrans = ast_channel_readtrans(active))) {
  3092. if ((f = ast_translate(readtrans, f, 1)) == NULL) {
  3093. f = &ast_null_frame;
  3094. return f;
  3095. }
  3096. /* XXX we ignore the return value here, perhaps we should
  3097. * disable the gateway if a write fails. I am not sure how a
  3098. * write would fail, or even if a failure would be fatal so for
  3099. * now we'll just ignore the return value. */
  3100. gateway->s->tech->write(gateway->s, f);
  3101. ast_frfree(f);
  3102. } else {
  3103. gateway->s->tech->write(gateway->s, f);
  3104. }
  3105. f = &ast_null_frame;
  3106. return f;
  3107. }
  3108. /* force silence on the line if T.38 negotiation might be taking place */
  3109. if (gateway->t38_state != T38_STATE_UNAVAILABLE && gateway->t38_state != T38_STATE_REJECTED) {
  3110. if (f->frametype == AST_FRAME_VOICE &&
  3111. (ast_format_cmp(f->subclass.format, ast_format_slin) == AST_FORMAT_CMP_EQUAL)) {
  3112. short silence_buf[f->samples];
  3113. struct ast_frame silence_frame = {
  3114. .frametype = AST_FRAME_VOICE,
  3115. .subclass.format = ast_format_slin,
  3116. .data.ptr = silence_buf,
  3117. .samples = f->samples,
  3118. .datalen = sizeof(silence_buf),
  3119. };
  3120. memset(silence_buf, 0, sizeof(silence_buf));
  3121. return ast_frisolate(&silence_frame);
  3122. } else {
  3123. return &ast_null_frame;
  3124. }
  3125. }
  3126. return f;
  3127. }
  3128. /*! \brief Attach a gateway framehook object to a channel.
  3129. * \param chan the channel to attach to
  3130. * \param details fax session details
  3131. * \return the framehook id of the attached framehook or -1 on error
  3132. * \retval -1 error
  3133. */
  3134. static int fax_gateway_attach(struct ast_channel *chan, struct ast_fax_session_details *details)
  3135. {
  3136. struct fax_gateway *gateway;
  3137. struct ast_framehook_interface fr_hook = {
  3138. .version = AST_FRAMEHOOK_INTERFACE_VERSION,
  3139. .event_cb = fax_gateway_framehook,
  3140. .destroy_cb = fax_gateway_framehook_destroy,
  3141. .disable_inheritance = 1, /* Masquerade inheritance is handled through the datastore fixup */
  3142. };
  3143. if (global_fax_debug) {
  3144. details->option.debug = AST_FAX_OPTFLAG_TRUE;
  3145. }
  3146. ast_string_field_set(details, result, "SUCCESS");
  3147. ast_string_field_set(details, resultstr, "gateway operation started successfully");
  3148. ast_string_field_set(details, error, "NO_ERROR");
  3149. set_channel_variables(chan, details);
  3150. /* set up the frame hook*/
  3151. gateway = fax_gateway_new(chan, details);
  3152. if (!gateway) {
  3153. ast_string_field_set(details, result, "FAILED");
  3154. ast_string_field_set(details, resultstr, "error initializing gateway session");
  3155. ast_string_field_set(details, error, "INIT_ERROR");
  3156. details->is_t38_negotiated = 0;
  3157. set_channel_variables(chan, details);
  3158. report_fax_status(chan, details, "No Available Resource");
  3159. return -1;
  3160. }
  3161. fr_hook.data = gateway;
  3162. ast_channel_lock(chan);
  3163. gateway->framehook = ast_framehook_attach(chan, &fr_hook);
  3164. ast_channel_unlock(chan);
  3165. if (gateway->framehook < 0) {
  3166. ao2_ref(gateway, -1);
  3167. ast_string_field_set(details, result, "FAILED");
  3168. ast_string_field_set(details, resultstr, "error attaching gateway to channel");
  3169. ast_string_field_set(details, error, "INIT_ERROR");
  3170. details->is_t38_negotiated = 0;
  3171. set_channel_variables(chan, details);
  3172. return -1;
  3173. }
  3174. return gateway->framehook;
  3175. }
  3176. /*! \brief destroy a FAX detect structure */
  3177. static void destroy_faxdetect(void *data)
  3178. {
  3179. struct fax_detect *faxdetect = data;
  3180. if (faxdetect->dsp) {
  3181. ast_dsp_free(faxdetect->dsp);
  3182. faxdetect->dsp = NULL;
  3183. }
  3184. ao2_cleanup(faxdetect->details);
  3185. ao2_cleanup(faxdetect->orig_format);
  3186. }
  3187. /*! \brief Create a new fax detect object.
  3188. * \param chan the channel attaching to
  3189. * \param timeout in ms to remove framehook in this time if not zero
  3190. * \param flags required options
  3191. * \return NULL or a fax gateway object
  3192. */
  3193. static struct fax_detect *fax_detect_new(struct ast_channel *chan, int timeout, int flags)
  3194. {
  3195. struct fax_detect *faxdetect = ao2_alloc(sizeof(*faxdetect), destroy_faxdetect);
  3196. if (!faxdetect) {
  3197. return NULL;
  3198. }
  3199. faxdetect->flags = flags;
  3200. if (timeout) {
  3201. faxdetect->timeout_start = ast_tvnow();
  3202. } else {
  3203. faxdetect->timeout_start.tv_sec = 0;
  3204. faxdetect->timeout_start.tv_usec = 0;
  3205. }
  3206. if (faxdetect->flags & FAX_DETECT_MODE_CNG) {
  3207. faxdetect->dsp = ast_dsp_new();
  3208. if (!faxdetect->dsp) {
  3209. ao2_ref(faxdetect, -1);
  3210. return NULL;
  3211. }
  3212. ast_dsp_set_features(faxdetect->dsp, DSP_FEATURE_FAX_DETECT);
  3213. ast_dsp_set_faxmode(faxdetect->dsp, DSP_FAXMODE_DETECT_CNG | DSP_FAXMODE_DETECT_SQUELCH);
  3214. } else {
  3215. faxdetect->dsp = NULL;
  3216. }
  3217. return faxdetect;
  3218. }
  3219. /*! \brief Deref the faxdetect data structure when the faxdetect framehook is detached
  3220. * \param data framehook data (faxdetect data)*/
  3221. static void fax_detect_framehook_destroy(void *data)
  3222. {
  3223. struct fax_detect *faxdetect = data;
  3224. ao2_ref(faxdetect, -1);
  3225. }
  3226. /*! \brief Fax Detect Framehook
  3227. *
  3228. * Listen for fax tones in audio path and enable jumping to a extension when detected.
  3229. *
  3230. * \param chan channel
  3231. * \param f frame to handle may be NULL
  3232. * \param event framehook event
  3233. * \param data framehook data (struct fax_detect *)
  3234. *
  3235. * \return processed frame or NULL when f is NULL or a null frame
  3236. */
  3237. static struct ast_frame *fax_detect_framehook(struct ast_channel *chan, struct ast_frame *f, enum ast_framehook_event event, void *data)
  3238. {
  3239. struct fax_detect *faxdetect = data;
  3240. struct ast_fax_session_details *details;
  3241. struct ast_control_t38_parameters *control_params;
  3242. RAII_VAR(struct ast_channel *, peer, NULL, ao2_cleanup);
  3243. RAII_VAR(struct ast_channel *, chan_ref, chan, ao2_cleanup);
  3244. int result = 0;
  3245. /* Ref bump the channel for when we have to unlock it */
  3246. ao2_ref(chan, 1);
  3247. details = faxdetect->details;
  3248. switch (event) {
  3249. case AST_FRAMEHOOK_EVENT_ATTACHED:
  3250. /* Setup format for DSP on ATTACH*/
  3251. ao2_replace(faxdetect->orig_format, ast_channel_readformat(chan));
  3252. if ((ast_format_cmp(ast_channel_readformat(chan), ast_format_slin) != AST_FORMAT_CMP_EQUAL) &&
  3253. (ast_format_cmp(ast_channel_readformat(chan), ast_format_alaw) != AST_FORMAT_CMP_EQUAL) &&
  3254. (ast_format_cmp(ast_channel_readformat(chan), ast_format_ulaw) != AST_FORMAT_CMP_EQUAL)) {
  3255. if (ast_set_read_format(chan, ast_format_slin)) {
  3256. ast_framehook_detach(chan, details->faxdetect_id);
  3257. details->faxdetect_id = -1;
  3258. return f;
  3259. }
  3260. }
  3261. return NULL;
  3262. case AST_FRAMEHOOK_EVENT_DETACHED:
  3263. /* restore audio formats when we are detached */
  3264. ast_set_read_format(chan, faxdetect->orig_format);
  3265. ast_channel_unlock(chan);
  3266. peer = ast_channel_bridge_peer(chan);
  3267. if (peer) {
  3268. ast_channel_make_compatible(chan, peer);
  3269. }
  3270. ast_channel_lock(chan);
  3271. return NULL;
  3272. case AST_FRAMEHOOK_EVENT_READ:
  3273. if (f) {
  3274. break;
  3275. }
  3276. default:
  3277. return f;
  3278. };
  3279. if (details->faxdetect_id < 0) {
  3280. return f;
  3281. }
  3282. if (!ast_tvzero(faxdetect->timeout_start)
  3283. && ast_tvdiff_ms(ast_tvnow(), faxdetect->timeout_start) > details->faxdetect_timeout) {
  3284. ast_debug(1, "FAXOPT(faxdetect) timeout on %s\n", ast_channel_name(chan));
  3285. ast_framehook_detach(chan, details->faxdetect_id);
  3286. details->faxdetect_id = -1;
  3287. return f;
  3288. }
  3289. /* only handle VOICE and CONTROL frames*/
  3290. switch (f->frametype) {
  3291. case AST_FRAME_VOICE:
  3292. /* we have no DSP this means we not detecting CNG */
  3293. if (!faxdetect->dsp) {
  3294. return f;
  3295. }
  3296. /* We can only process some formats*/
  3297. if ((ast_format_cmp(f->subclass.format, ast_format_slin) != AST_FORMAT_CMP_EQUAL) &&
  3298. (ast_format_cmp(f->subclass.format, ast_format_alaw) != AST_FORMAT_CMP_EQUAL) &&
  3299. (ast_format_cmp(f->subclass.format, ast_format_ulaw) != AST_FORMAT_CMP_EQUAL)) {
  3300. return f;
  3301. }
  3302. break;
  3303. case AST_FRAME_CONTROL:
  3304. if ((f->subclass.integer == AST_CONTROL_T38_PARAMETERS) &&
  3305. (faxdetect->flags & FAX_DETECT_MODE_T38)) {
  3306. break;
  3307. }
  3308. return f;
  3309. default:
  3310. return f;
  3311. }
  3312. if (f->frametype == AST_FRAME_VOICE) {
  3313. f = ast_dsp_process(chan, faxdetect->dsp, f);
  3314. if (f->frametype == AST_FRAME_DTMF) {
  3315. result = f->subclass.integer;
  3316. }
  3317. } else if ((f->frametype == AST_FRAME_CONTROL) && (f->datalen == sizeof(struct ast_control_t38_parameters))) {
  3318. control_params = f->data.ptr;
  3319. switch (control_params->request_response) {
  3320. case AST_T38_NEGOTIATED:
  3321. case AST_T38_REQUEST_NEGOTIATE:
  3322. result = 't';
  3323. break;
  3324. default:
  3325. break;
  3326. }
  3327. }
  3328. if (result) {
  3329. const char *target_context;
  3330. switch (result) {
  3331. case 'f':
  3332. case 't':
  3333. target_context = S_OR(ast_channel_macrocontext(chan), ast_channel_context(chan));
  3334. ast_channel_unlock(chan);
  3335. ast_frfree(f);
  3336. f = &ast_null_frame;
  3337. if (ast_exists_extension(chan, target_context, "fax", 1,
  3338. S_COR(ast_channel_caller(chan)->id.number.valid, ast_channel_caller(chan)->id.number.str, NULL))) {
  3339. ast_verb(2, "Redirecting '%s' to fax extension due to %s detection\n",
  3340. ast_channel_name(chan), (result == 'f') ? "CNG" : "T38");
  3341. pbx_builtin_setvar_helper(chan, "FAXEXTEN", ast_channel_exten(chan));
  3342. if (ast_async_goto(chan, target_context, "fax", 1)) {
  3343. ast_log(LOG_NOTICE, "Failed to async goto '%s' into fax of '%s'\n", ast_channel_name(chan), target_context);
  3344. }
  3345. } else {
  3346. ast_log(LOG_NOTICE, "FAX %s detected but no fax extension in context (%s)\n",
  3347. (result == 'f') ? "CNG" : "T38", target_context);
  3348. }
  3349. ast_channel_lock(chan);
  3350. ast_framehook_detach(chan, details->faxdetect_id);
  3351. details->faxdetect_id = -1;
  3352. break;
  3353. default:
  3354. break;
  3355. }
  3356. }
  3357. return f;
  3358. }
  3359. /*! \brief Attach a faxdetect framehook object to a channel.
  3360. * \param chan the channel to attach to
  3361. * \param timeout in ms to remove framehook in this time if not zero
  3362. * \return the faxdetect structure or NULL on error
  3363. * \param flags required options
  3364. * \retval -1 error
  3365. */
  3366. static int fax_detect_attach(struct ast_channel *chan, int timeout, int flags)
  3367. {
  3368. struct fax_detect *faxdetect;
  3369. struct ast_fax_session_details *details;
  3370. struct ast_framehook_interface fr_hook = {
  3371. .version = AST_FRAMEHOOK_INTERFACE_VERSION,
  3372. .event_cb = fax_detect_framehook,
  3373. .destroy_cb = fax_detect_framehook_destroy,
  3374. };
  3375. if (!(details = find_or_create_details(chan))) {
  3376. ast_log(LOG_ERROR, "System cannot provide memory for session requirements.\n");
  3377. return -1;
  3378. }
  3379. /* set up the frame hook*/
  3380. faxdetect = fax_detect_new(chan, timeout, flags);
  3381. if (!faxdetect) {
  3382. ao2_ref(details, -1);
  3383. return -1;
  3384. }
  3385. fr_hook.data = faxdetect;
  3386. faxdetect->details = details;
  3387. ast_channel_lock(chan);
  3388. details->faxdetect_id = ast_framehook_attach(chan, &fr_hook);
  3389. details->faxdetect_timeout = timeout;
  3390. details->faxdetect_flags = flags;
  3391. ast_channel_unlock(chan);
  3392. if (details->faxdetect_id < 0) {
  3393. ao2_ref(faxdetect, -1);
  3394. }
  3395. return details->faxdetect_id;
  3396. }
  3397. /*! \brief hash callback for ao2 */
  3398. static int session_hash_cb(const void *obj, const int flags)
  3399. {
  3400. const struct ast_fax_session *s = obj;
  3401. return s->id;
  3402. }
  3403. /*! \brief compare callback for ao2 */
  3404. static int session_cmp_cb(void *obj, void *arg, int flags)
  3405. {
  3406. struct ast_fax_session *lhs = obj, *rhs = arg;
  3407. return (lhs->id == rhs->id) ? CMP_MATCH | CMP_STOP : 0;
  3408. }
  3409. /*! \brief fax session tab completion */
  3410. static char *fax_session_tab_complete(struct ast_cli_args *a)
  3411. {
  3412. int tklen;
  3413. int wordnum = 0;
  3414. char *name = NULL;
  3415. struct ao2_iterator i;
  3416. struct ast_fax_session *s;
  3417. char tbuf[5];
  3418. if (a->pos != 3) {
  3419. return NULL;
  3420. }
  3421. tklen = strlen(a->word);
  3422. i = ao2_iterator_init(faxregistry.container, 0);
  3423. while ((s = ao2_iterator_next(&i))) {
  3424. snprintf(tbuf, sizeof(tbuf), "%u", s->id);
  3425. if (!strncasecmp(a->word, tbuf, tklen) && ++wordnum > a->n) {
  3426. name = ast_strdup(tbuf);
  3427. ao2_ref(s, -1);
  3428. break;
  3429. }
  3430. ao2_ref(s, -1);
  3431. }
  3432. ao2_iterator_destroy(&i);
  3433. return name;
  3434. }
  3435. static char *cli_fax_show_version(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
  3436. {
  3437. struct fax_module *fax;
  3438. switch(cmd) {
  3439. case CLI_INIT:
  3440. e->command = "fax show version";
  3441. e->usage =
  3442. "Usage: fax show version\n"
  3443. " Show versions of FAX For Asterisk components.\n";
  3444. return NULL;
  3445. case CLI_GENERATE:
  3446. return NULL;
  3447. }
  3448. if (a->argc != 3) {
  3449. return CLI_SHOWUSAGE;
  3450. }
  3451. ast_cli(a->fd, "FAX For Asterisk Components:\n");
  3452. ast_cli(a->fd, "\tApplications: %s\n", ast_get_version());
  3453. AST_RWLIST_RDLOCK(&faxmodules);
  3454. AST_RWLIST_TRAVERSE(&faxmodules, fax, list) {
  3455. ast_cli(a->fd, "\t%s: %s\n", fax->tech->description, fax->tech->version);
  3456. }
  3457. AST_RWLIST_UNLOCK(&faxmodules);
  3458. ast_cli(a->fd, "\n");
  3459. return CLI_SUCCESS;
  3460. }
  3461. /*! \brief enable FAX debugging */
  3462. static char *cli_fax_set_debug(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
  3463. {
  3464. int flag;
  3465. const char *what;
  3466. switch (cmd) {
  3467. case CLI_INIT:
  3468. e->command = "fax set debug {on|off}";
  3469. e->usage =
  3470. "Usage: fax set debug { on | off }\n"
  3471. " Enable/Disable FAX debugging on new FAX sessions. The basic FAX debugging will result in\n"
  3472. " additional events sent to manager sessions with 'call' class permissions. When\n"
  3473. " verbosity is greater than '5' events will be displayed to the console and audio versus\n"
  3474. " energy analysis will be performed and displayed to the console.\n";
  3475. return NULL;
  3476. case CLI_GENERATE:
  3477. return NULL;
  3478. }
  3479. what = a->argv[e->args-1]; /* guaranteed to exist */
  3480. if (!strcasecmp(what, "on")) {
  3481. flag = 1;
  3482. } else if (!strcasecmp(what, "off")) {
  3483. flag = 0;
  3484. } else {
  3485. return CLI_SHOWUSAGE;
  3486. }
  3487. global_fax_debug = flag;
  3488. ast_cli(a->fd, "\n\nFAX Debug %s\n\n", (flag) ? "Enabled" : "Disabled");
  3489. return CLI_SUCCESS;
  3490. }
  3491. /*! \brief display registered FAX capabilities */
  3492. static char *cli_fax_show_capabilities(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
  3493. {
  3494. struct fax_module *fax;
  3495. unsigned int num_modules = 0;
  3496. switch (cmd) {
  3497. case CLI_INIT:
  3498. e->command = "fax show capabilities";
  3499. e->usage =
  3500. "Usage: fax show capabilities\n"
  3501. " Shows the capabilities of the registered FAX technology modules\n";
  3502. return NULL;
  3503. case CLI_GENERATE:
  3504. return NULL;
  3505. }
  3506. ast_cli(a->fd, "\n\nRegistered FAX Technology Modules:\n\n");
  3507. AST_RWLIST_RDLOCK(&faxmodules);
  3508. AST_RWLIST_TRAVERSE(&faxmodules, fax, list) {
  3509. ast_cli(a->fd, "%-15s : %s\n%-15s : %s\n%-15s : ", "Type", fax->tech->type, "Description", fax->tech->description, "Capabilities");
  3510. fax->tech->cli_show_capabilities(a->fd);
  3511. num_modules++;
  3512. }
  3513. AST_RWLIST_UNLOCK(&faxmodules);
  3514. ast_cli(a->fd, "%u registered modules\n\n", num_modules);
  3515. return CLI_SUCCESS;
  3516. }
  3517. /*! \brief display global defaults and settings */
  3518. static char *cli_fax_show_settings(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
  3519. {
  3520. struct fax_module *fax;
  3521. char modems[128] = "";
  3522. struct fax_options options;
  3523. switch (cmd) {
  3524. case CLI_INIT:
  3525. e->command = "fax show settings";
  3526. e->usage =
  3527. "Usage: fax show settings\n"
  3528. " Show the global settings and defaults of both the FAX core and technology modules\n";
  3529. return NULL;
  3530. case CLI_GENERATE:
  3531. return NULL;
  3532. }
  3533. get_general_options(&options);
  3534. ast_cli(a->fd, "FAX For Asterisk Settings:\n");
  3535. ast_cli(a->fd, "\tECM: %s\n", options.ecm ? "Enabled" : "Disabled");
  3536. ast_cli(a->fd, "\tStatus Events: %s\n", options.statusevents ? "On" : "Off");
  3537. ast_cli(a->fd, "\tMinimum Bit Rate: %u\n", options.minrate);
  3538. ast_cli(a->fd, "\tMaximum Bit Rate: %u\n", options.maxrate);
  3539. ast_fax_modem_to_str(options.modems, modems, sizeof(modems));
  3540. ast_cli(a->fd, "\tModem Modulations Allowed: %s\n", modems);
  3541. ast_cli(a->fd, "\tT.38 Negotiation Timeout: %u\n", options.t38timeout);
  3542. ast_cli(a->fd, "\n\nFAX Technology Modules:\n\n");
  3543. AST_RWLIST_RDLOCK(&faxmodules);
  3544. AST_RWLIST_TRAVERSE(&faxmodules, fax, list) {
  3545. ast_cli(a->fd, "%s (%s) Settings:\n", fax->tech->type, fax->tech->description);
  3546. fax->tech->cli_show_settings(a->fd);
  3547. }
  3548. AST_RWLIST_UNLOCK(&faxmodules);
  3549. return CLI_SUCCESS;
  3550. }
  3551. /*! \brief display details of a specified fax session */
  3552. static char *cli_fax_show_session(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
  3553. {
  3554. struct ast_fax_session *s, tmp;
  3555. switch (cmd) {
  3556. case CLI_INIT:
  3557. e->command = "fax show session";
  3558. e->usage =
  3559. "Usage: fax show session <session number>\n"
  3560. " Shows status of the named FAX session\n";
  3561. return NULL;
  3562. case CLI_GENERATE:
  3563. return fax_session_tab_complete(a);
  3564. }
  3565. if (a->argc != 4) {
  3566. return CLI_SHOWUSAGE;
  3567. }
  3568. if (sscanf(a->argv[3], "%u", &tmp.id) != 1) {
  3569. ast_log(LOG_ERROR, "invalid session id: '%s'\n", a->argv[3]);
  3570. return RESULT_SUCCESS;
  3571. }
  3572. ast_cli(a->fd, "\nFAX Session Details:\n--------------------\n\n");
  3573. s = ao2_find(faxregistry.container, &tmp, OBJ_POINTER);
  3574. if (s) {
  3575. s->tech->cli_show_session(s, a->fd);
  3576. ao2_ref(s, -1);
  3577. }
  3578. ast_cli(a->fd, "\n\n");
  3579. return CLI_SUCCESS;
  3580. }
  3581. static int manager_fax_session(struct mansession *s, const struct message *m)
  3582. {
  3583. const char *action_id = astman_get_header(m, "ActionID");
  3584. const char *session_number = astman_get_header(m, "SessionNumber");
  3585. char id_text[256] = "";
  3586. struct ast_fax_session *session;
  3587. struct ast_fax_session find_session;
  3588. if (sscanf(session_number, "%30u", &find_session.id) != 1) {
  3589. astman_send_error(s, m, "Invalid session ID");
  3590. return 0;
  3591. }
  3592. session = ao2_find(faxregistry.container, &find_session, OBJ_POINTER);
  3593. if (!session) {
  3594. astman_send_error(s, m, "Session not found");
  3595. return 0;
  3596. }
  3597. if (!session->tech->manager_fax_session) {
  3598. astman_send_error(s, m, "Fax technology doesn't provide a handler for FAXSession");
  3599. ao2_ref(session, -1);
  3600. return 0;
  3601. }
  3602. if (!ast_strlen_zero(action_id)) {
  3603. snprintf(id_text, sizeof(id_text), "ActionID: %s\r\n", action_id);
  3604. }
  3605. astman_send_ack(s, m, "FAXSession event will follow");
  3606. session->tech->manager_fax_session(s, id_text, session);
  3607. ao2_ref(session, -1);
  3608. return 0;
  3609. }
  3610. /*! \brief display fax stats */
  3611. static char *cli_fax_show_stats(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
  3612. {
  3613. struct fax_module *fax;
  3614. switch (cmd) {
  3615. case CLI_INIT:
  3616. e->command = "fax show stats";
  3617. e->usage =
  3618. "Usage: fax show stats\n"
  3619. " Shows a statistical summary of FAX transmissions\n";
  3620. return NULL;
  3621. case CLI_GENERATE:
  3622. return NULL;
  3623. }
  3624. ast_cli(a->fd, "\nFAX Statistics:\n---------------\n\n");
  3625. ast_cli(a->fd, "%-20.20s : %d\n", "Current Sessions", faxregistry.active_sessions);
  3626. ast_cli(a->fd, "%-20.20s : %d\n", "Reserved Sessions", faxregistry.reserved_sessions);
  3627. ast_cli(a->fd, "%-20.20s : %d\n", "Transmit Attempts", faxregistry.fax_tx_attempts);
  3628. ast_cli(a->fd, "%-20.20s : %d\n", "Receive Attempts", faxregistry.fax_rx_attempts);
  3629. ast_cli(a->fd, "%-20.20s : %d\n", "Completed FAXes", faxregistry.fax_complete);
  3630. ast_cli(a->fd, "%-20.20s : %d\n", "Failed FAXes", faxregistry.fax_failures);
  3631. AST_RWLIST_RDLOCK(&faxmodules);
  3632. AST_RWLIST_TRAVERSE(&faxmodules, fax, list) {
  3633. fax->tech->cli_show_stats(a->fd);
  3634. }
  3635. AST_RWLIST_UNLOCK(&faxmodules);
  3636. ast_cli(a->fd, "\n\n");
  3637. return CLI_SUCCESS;
  3638. }
  3639. static int manager_fax_stats(struct mansession *s, const struct message *m)
  3640. {
  3641. const char *action_id = astman_get_header(m, "ActionID");
  3642. char id_text[256] = "";
  3643. astman_send_ack(s, m, "FAXStats event will follow");
  3644. if (!ast_strlen_zero(action_id)) {
  3645. snprintf(id_text, sizeof(id_text), "ActionID: %s\r\n", action_id);
  3646. }
  3647. astman_append(s, "Event: FAXStats\r\n"
  3648. "%s"
  3649. "CurrentSessions: %d\r\n"
  3650. "ReservedSessions: %d\r\n"
  3651. "TransmitAttempts: %d\r\n"
  3652. "ReceiveAttempts: %d\r\n"
  3653. "CompletedFAXes: %d\r\n"
  3654. "FailedFAXes: %d\r\n"
  3655. "\r\n",
  3656. id_text,
  3657. faxregistry.active_sessions, faxregistry.reserved_sessions,
  3658. faxregistry.fax_tx_attempts, faxregistry.fax_rx_attempts,
  3659. faxregistry.fax_complete, faxregistry.fax_failures);
  3660. return 0;
  3661. }
  3662. static const char *fax_session_type(struct ast_fax_session *s)
  3663. {
  3664. if (s->details->caps & AST_FAX_TECH_AUDIO) {
  3665. return "G.711";
  3666. }
  3667. if (s->details->caps & AST_FAX_TECH_T38) {
  3668. return "T.38";
  3669. }
  3670. return "none";
  3671. }
  3672. const char *ast_fax_session_operation_str(struct ast_fax_session *s)
  3673. {
  3674. if (s->details->caps & AST_FAX_TECH_GATEWAY) {
  3675. return "gateway";
  3676. }
  3677. if (s->details->caps & AST_FAX_TECH_SEND) {
  3678. return "send";
  3679. }
  3680. if (s->details->caps & AST_FAX_TECH_RECEIVE) {
  3681. return "receive";
  3682. }
  3683. if (s->details->caps & AST_FAX_TECH_V21_DETECT) {
  3684. return "V.21";
  3685. }
  3686. return "none";
  3687. }
  3688. /*! \brief display fax sessions */
  3689. static char *cli_fax_show_sessions(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
  3690. {
  3691. struct ast_fax_session *s;
  3692. struct ao2_iterator i;
  3693. int session_count;
  3694. char *filenames;
  3695. switch (cmd) {
  3696. case CLI_INIT:
  3697. e->command = "fax show sessions";
  3698. e->usage =
  3699. "Usage: fax show sessions\n"
  3700. " Shows the current FAX sessions\n";
  3701. return NULL;
  3702. case CLI_GENERATE:
  3703. return NULL;
  3704. }
  3705. ast_cli(a->fd, "\nCurrent FAX Sessions:\n\n");
  3706. ast_cli(a->fd, "%-20.20s %-10.10s %-10.10s %-5.5s %-10.10s %-15.15s %-30.30s\n",
  3707. "Channel", "Tech", "FAXID", "Type", "Operation", "State", "File(s)");
  3708. i = ao2_iterator_init(faxregistry.container, 0);
  3709. while ((s = ao2_iterator_next(&i))) {
  3710. ao2_lock(s);
  3711. filenames = generate_filenames_string(s->details, "", ", ");
  3712. ast_cli(a->fd, "%-20.20s %-10.10s %-10u %-5.5s %-10.10s %-15.15s %-30s\n",
  3713. s->channame, s->tech->type, s->id,
  3714. fax_session_type(s),
  3715. ast_fax_session_operation_str(s),
  3716. ast_fax_state_to_str(s->state), S_OR(filenames, ""));
  3717. ast_free(filenames);
  3718. ao2_unlock(s);
  3719. ao2_ref(s, -1);
  3720. }
  3721. ao2_iterator_destroy(&i);
  3722. session_count = ao2_container_count(faxregistry.container);
  3723. ast_cli(a->fd, "\n%d FAX sessions\n\n", session_count);
  3724. return CLI_SUCCESS;
  3725. }
  3726. static int manager_fax_sessions_entry(struct mansession *s,
  3727. struct ast_fax_session *session, const char *id_text)
  3728. {
  3729. char *filenames;
  3730. ao2_lock(session);
  3731. filenames = generate_filenames_string(session->details, "", ",");
  3732. if (!filenames) {
  3733. ast_log(LOG_ERROR, "Error generating Files string");
  3734. ao2_unlock(session);
  3735. return -1;
  3736. }
  3737. astman_append(s, "Event: FAXSessionsEntry\r\n"
  3738. "%s" /* ActionID if present */
  3739. "Channel: %s\r\n" /* Channel name */
  3740. "Technology: %s\r\n" /* Fax session technology */
  3741. "SessionNumber: %u\r\n" /* Session ID */
  3742. "SessionType: %s\r\n" /* G711 or T38 */
  3743. "Operation: %s\r\n"
  3744. "State: %s\r\n"
  3745. "Files: %s\r\n"
  3746. "\r\n",
  3747. id_text, session->channame, session->tech->type, session->id,
  3748. fax_session_type(session), ast_fax_session_operation_str(session),
  3749. ast_fax_state_to_str(session->state), S_OR(filenames, ""));
  3750. ast_free(filenames);
  3751. ao2_unlock(session);
  3752. return 0;
  3753. }
  3754. static int manager_fax_sessions(struct mansession *s, const struct message *m)
  3755. {
  3756. const char *action_id = astman_get_header(m, "ActionID");
  3757. char id_text[256];
  3758. struct ast_fax_session *session;
  3759. struct ao2_iterator iter;
  3760. int session_count = 0;
  3761. id_text[0] = '\0';
  3762. if (!ast_strlen_zero(action_id)) {
  3763. snprintf(id_text, sizeof(id_text), "ActionID: %s\r\n", action_id);
  3764. }
  3765. astman_send_listack(s, m, "FAXSessionsEntry event list will follow", "Start");
  3766. iter = ao2_iterator_init(faxregistry.container, 0);
  3767. while ((session = ao2_iterator_next(&iter))) {
  3768. if (!manager_fax_sessions_entry(s, session, id_text)) {
  3769. session_count++;
  3770. }
  3771. ao2_ref(session, -1);
  3772. }
  3773. ao2_iterator_destroy(&iter);
  3774. astman_send_list_complete_start(s, m, "FAXSessionsComplete", session_count);
  3775. astman_append(s, "Total: %d\r\n", session_count);
  3776. astman_send_list_complete_end(s);
  3777. return 0;
  3778. }
  3779. static struct ast_cli_entry fax_cli[] = {
  3780. AST_CLI_DEFINE(cli_fax_show_version, "Show versions of FAX For Asterisk components"),
  3781. AST_CLI_DEFINE(cli_fax_set_debug, "Enable/Disable FAX debugging on new FAX sessions"),
  3782. AST_CLI_DEFINE(cli_fax_show_capabilities, "Show the capabilities of the registered FAX technology modules"),
  3783. AST_CLI_DEFINE(cli_fax_show_settings, "Show the global settings and defaults of both the FAX core and technology modules"),
  3784. AST_CLI_DEFINE(cli_fax_show_session, "Show the status of the named FAX sessions"),
  3785. AST_CLI_DEFINE(cli_fax_show_sessions, "Show the current FAX sessions"),
  3786. AST_CLI_DEFINE(cli_fax_show_stats, "Summarize FAX session history"),
  3787. };
  3788. static void set_general_options(const struct fax_options *options)
  3789. {
  3790. ast_rwlock_wrlock(&options_lock);
  3791. general_options = *options;
  3792. ast_rwlock_unlock(&options_lock);
  3793. }
  3794. static void get_general_options(struct fax_options *options)
  3795. {
  3796. ast_rwlock_rdlock(&options_lock);
  3797. *options = general_options;
  3798. ast_rwlock_unlock(&options_lock);
  3799. }
  3800. static int set_t38timeout(const char *value, unsigned int *t38timeout)
  3801. {
  3802. unsigned int timeout;
  3803. if (sscanf(value, "%u", &timeout) != 1) {
  3804. ast_log(LOG_ERROR, "Unable to get timeout from '%s'\n", value);
  3805. return -1;
  3806. } else if (timeout) {
  3807. *t38timeout = timeout;
  3808. } else {
  3809. ast_log(LOG_ERROR, "T.38 negotiation timeout must be non-zero\n");
  3810. return -1;
  3811. }
  3812. return 0;
  3813. }
  3814. /*! \brief configure res_fax */
  3815. static int set_config(int reload)
  3816. {
  3817. struct ast_config *cfg;
  3818. struct ast_variable *v;
  3819. struct ast_flags config_flags = { reload ? CONFIG_FLAG_FILEUNCHANGED : 0 };
  3820. char modems[128] = "";
  3821. struct fax_options options;
  3822. int res = 0;
  3823. options = default_options;
  3824. /* When we're not reloading, we have to be certain to set the general options
  3825. * to the defaults in case config loading goes wrong at some point. On a reload,
  3826. * the general options need to stay the same as what they were prior to the
  3827. * reload rather than being reset to the defaults.
  3828. */
  3829. if (!reload) {
  3830. set_general_options(&options);
  3831. }
  3832. /* read configuration */
  3833. if (!(cfg = ast_config_load2(config, "res_fax", config_flags))) {
  3834. ast_log(LOG_NOTICE, "Configuration file '%s' not found, %s options.\n",
  3835. config, reload ? "not changing" : "using default");
  3836. return 0;
  3837. }
  3838. if (cfg == CONFIG_STATUS_FILEINVALID) {
  3839. ast_log(LOG_NOTICE, "Configuration file '%s' is invalid, %s options.\n",
  3840. config, reload ? "not changing" : "using default");
  3841. return 0;
  3842. }
  3843. if (cfg == CONFIG_STATUS_FILEUNCHANGED) {
  3844. return 0;
  3845. }
  3846. if (reload) {
  3847. options = default_options;
  3848. }
  3849. /* create configuration */
  3850. for (v = ast_variable_browse(cfg, "general"); v; v = v->next) {
  3851. int rate;
  3852. if (!strcasecmp(v->name, "minrate")) {
  3853. ast_debug(3, "reading minrate '%s' from configuration file\n", v->value);
  3854. if ((rate = fax_rate_str_to_int(v->value)) == 0) {
  3855. res = -1;
  3856. goto end;
  3857. }
  3858. options.minrate = rate;
  3859. } else if (!strcasecmp(v->name, "maxrate")) {
  3860. ast_debug(3, "reading maxrate '%s' from configuration file\n", v->value);
  3861. if ((rate = fax_rate_str_to_int(v->value)) == 0) {
  3862. res = -1;
  3863. goto end;
  3864. }
  3865. options.maxrate = rate;
  3866. } else if (!strcasecmp(v->name, "statusevents")) {
  3867. ast_debug(3, "reading statusevents '%s' from configuration file\n", v->value);
  3868. options.statusevents = ast_true(v->value);
  3869. } else if (!strcasecmp(v->name, "ecm")) {
  3870. ast_debug(3, "reading ecm '%s' from configuration file\n", v->value);
  3871. options.ecm = ast_true(v->value);
  3872. } else if ((!strcasecmp(v->name, "modem")) || (!strcasecmp(v->name, "modems"))) {
  3873. options.modems = 0;
  3874. update_modem_bits(&options.modems, v->value);
  3875. } else if (!strcasecmp(v->name, "t38timeout")) {
  3876. if (set_t38timeout(v->value, &options.t38timeout)) {
  3877. res = -1;
  3878. goto end;
  3879. }
  3880. }
  3881. }
  3882. if (options.maxrate < options.minrate) {
  3883. ast_log(LOG_ERROR, "maxrate %u is less than minrate %u\n", options.maxrate, options.minrate);
  3884. res = -1;
  3885. goto end;
  3886. }
  3887. if (check_modem_rate(options.modems, options.minrate)) {
  3888. ast_fax_modem_to_str(options.modems, modems, sizeof(modems));
  3889. ast_log(LOG_ERROR, "'modems' setting '%s' is incompatible with 'minrate' setting %u\n", modems, options.minrate);
  3890. res = -1;
  3891. goto end;
  3892. }
  3893. if (check_modem_rate(options.modems, options.maxrate)) {
  3894. ast_fax_modem_to_str(options.modems, modems, sizeof(modems));
  3895. ast_log(LOG_ERROR, "'modems' setting '%s' is incompatible with 'maxrate' setting %u\n", modems, options.maxrate);
  3896. res = -1;
  3897. goto end;
  3898. }
  3899. set_general_options(&options);
  3900. end:
  3901. ast_config_destroy(cfg);
  3902. return res;
  3903. }
  3904. /*! \brief FAXOPT read function returns the contents of a FAX option */
  3905. static int acf_faxopt_read(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t len)
  3906. {
  3907. struct ast_fax_session_details *details = find_details(chan);
  3908. int res = 0;
  3909. char *filenames;
  3910. if (!details) {
  3911. ast_log(LOG_ERROR, "channel '%s' can't read FAXOPT(%s) because it has never been written.\n", ast_channel_name(chan), data);
  3912. return -1;
  3913. }
  3914. if (!strcasecmp(data, "ecm")) {
  3915. ast_copy_string(buf, details->option.ecm ? "yes" : "no", len);
  3916. } else if (!strcasecmp(data, "t38gateway") || !strcasecmp(data, "gateway") ||
  3917. !strcasecmp(data, "t38_gateway") || !strcasecmp(data, "faxgateway")) {
  3918. ast_copy_string(buf, details->gateway_id != -1 ? "yes" : "no", len);
  3919. } else if (!strcasecmp(data, "faxdetect")) {
  3920. ast_copy_string(buf, details->faxdetect_id != -1 ? "yes" : "no", len);
  3921. } else if (!strcasecmp(data, "error")) {
  3922. ast_copy_string(buf, details->error, len);
  3923. } else if (!strcasecmp(data, "filename")) {
  3924. if (AST_LIST_EMPTY(&details->documents)) {
  3925. ast_log(LOG_ERROR, "channel '%s' can't read FAXOPT(%s) because it has never been written.\n", ast_channel_name(chan), data);
  3926. res = -1;
  3927. } else {
  3928. ast_copy_string(buf, AST_LIST_FIRST(&details->documents)->filename, len);
  3929. }
  3930. } else if (!strcasecmp(data, "filenames")) {
  3931. if (AST_LIST_EMPTY(&details->documents)) {
  3932. ast_log(LOG_ERROR, "channel '%s' can't read FAXOPT(%s) because it has never been written.\n", ast_channel_name(chan), data);
  3933. res = -1;
  3934. } else if ((filenames = generate_filenames_string(details, "", ","))) {
  3935. ast_copy_string(buf, filenames, len);
  3936. ast_free(filenames);
  3937. } else {
  3938. ast_log(LOG_ERROR, "channel '%s' can't read FAXOPT(%s), there was an error generating the filenames list.\n", ast_channel_name(chan), data);
  3939. res = -1;
  3940. }
  3941. } else if (!strcasecmp(data, "headerinfo")) {
  3942. ast_copy_string(buf, details->headerinfo, len);
  3943. } else if (!strcasecmp(data, "localstationid")) {
  3944. ast_copy_string(buf, details->localstationid, len);
  3945. } else if (!strcasecmp(data, "maxrate")) {
  3946. snprintf(buf, len, "%u", details->maxrate);
  3947. } else if (!strcasecmp(data, "minrate")) {
  3948. snprintf(buf, len, "%u", details->minrate);
  3949. } else if (!strcasecmp(data, "pages")) {
  3950. snprintf(buf, len, "%u", details->pages_transferred);
  3951. } else if (!strcasecmp(data, "rate")) {
  3952. ast_copy_string(buf, details->transfer_rate, len);
  3953. } else if (!strcasecmp(data, "remotestationid")) {
  3954. ast_copy_string(buf, details->remotestationid, len);
  3955. } else if (!strcasecmp(data, "resolution")) {
  3956. ast_copy_string(buf, details->resolution, len);
  3957. } else if (!strcasecmp(data, "sessionid")) {
  3958. snprintf(buf, len, "%u", details->id);
  3959. } else if (!strcasecmp(data, "status")) {
  3960. ast_copy_string(buf, details->result, len);
  3961. } else if (!strcasecmp(data, "statusstr")) {
  3962. ast_copy_string(buf, details->resultstr, len);
  3963. } else if ((!strcasecmp(data, "modem")) || (!strcasecmp(data, "modems"))) {
  3964. ast_fax_modem_to_str(details->modems, buf, len);
  3965. } else if (!strcasecmp(data, "t38timeout")) {
  3966. snprintf(buf, len, "%u", details->t38timeout);
  3967. } else {
  3968. ast_log(LOG_WARNING, "channel '%s' can't read FAXOPT(%s) because it is unhandled!\n", ast_channel_name(chan), data);
  3969. res = -1;
  3970. }
  3971. ao2_ref(details, -1);
  3972. return res;
  3973. }
  3974. /*! \brief FAXOPT write function modifies the contents of a FAX option */
  3975. static int acf_faxopt_write(struct ast_channel *chan, const char *cmd, char *data, const char *value)
  3976. {
  3977. int res = 0;
  3978. struct ast_fax_session_details *details;
  3979. if (!(details = find_or_create_details(chan))) {
  3980. ast_log(LOG_WARNING, "channel '%s' can't set FAXOPT(%s) to '%s' because it failed to create a datastore.\n", ast_channel_name(chan), data, value);
  3981. return -1;
  3982. }
  3983. ast_debug(3, "channel '%s' setting FAXOPT(%s) to '%s'\n", ast_channel_name(chan), data, value);
  3984. if (!strcasecmp(data, "ecm")) {
  3985. const char *val = ast_skip_blanks(value);
  3986. if (ast_true(val)) {
  3987. details->option.ecm = AST_FAX_OPTFLAG_TRUE;
  3988. } else if (ast_false(val)) {
  3989. details->option.ecm = AST_FAX_OPTFLAG_FALSE;
  3990. } else {
  3991. ast_log(LOG_WARNING, "Unsupported value '%s' passed to FAXOPT(ecm).\n", value);
  3992. }
  3993. } else if (!strcasecmp(data, "t38gateway") || !strcasecmp(data, "gateway") ||
  3994. !strcasecmp(data, "t38_gateway") || !strcasecmp(data, "faxgateway")) {
  3995. const char *val = ast_skip_blanks(value);
  3996. char *timeout = strchr(val, ',');
  3997. if (timeout) {
  3998. *timeout++ = '\0';
  3999. }
  4000. if (ast_true(val)) {
  4001. if (details->gateway_id < 0) {
  4002. details->gateway_timeout = 0;
  4003. if (timeout) {
  4004. unsigned int gwtimeout;
  4005. if (sscanf(timeout, "%30u", &gwtimeout) == 1) {
  4006. details->gateway_timeout = gwtimeout * 1000;
  4007. } else {
  4008. ast_log(LOG_WARNING, "Unsupported timeout '%s' passed to FAXOPT(%s).\n", timeout, data);
  4009. }
  4010. }
  4011. details->gateway_id = fax_gateway_attach(chan, details);
  4012. if (details->gateway_id < 0) {
  4013. ast_log(LOG_ERROR, "Error attaching T.38 gateway to channel %s.\n", ast_channel_name(chan));
  4014. res = -1;
  4015. } else {
  4016. ast_debug(1, "Attached T.38 gateway to channel %s.\n", ast_channel_name(chan));
  4017. }
  4018. } else {
  4019. ast_log(LOG_WARNING, "Attempt to attach a T.38 gateway on channel (%s) with gateway already running.\n", ast_channel_name(chan));
  4020. }
  4021. } else if (ast_false(val)) {
  4022. ast_channel_lock(chan);
  4023. ast_framehook_detach(chan, details->gateway_id);
  4024. ast_channel_unlock(chan);
  4025. details->gateway_id = -1;
  4026. } else {
  4027. ast_log(LOG_WARNING, "Unsupported value '%s' passed to FAXOPT(%s).\n", value, data);
  4028. }
  4029. } else if (!strcasecmp(data, "faxdetect")) {
  4030. const char *val = ast_skip_blanks(value);
  4031. char *timeout = strchr(val, ',');
  4032. unsigned int fdtimeout = 0;
  4033. int flags;
  4034. int faxdetect;
  4035. if (timeout) {
  4036. *timeout++ = '\0';
  4037. }
  4038. if (ast_true(val) || !strcasecmp(val, "t38") || !strcasecmp(val, "cng")) {
  4039. if (details->faxdetect_id < 0) {
  4040. if (timeout) {
  4041. if (sscanf(timeout, "%30u", &fdtimeout) == 1) {
  4042. fdtimeout *= 1000;
  4043. } else {
  4044. ast_log(LOG_WARNING, "Unsupported timeout '%s' passed to FAXOPT(%s).\n",
  4045. timeout, data);
  4046. }
  4047. }
  4048. if (!strcasecmp(val, "t38")) {
  4049. flags = FAX_DETECT_MODE_T38;
  4050. } else if (!strcasecmp(val, "cng")) {
  4051. flags = FAX_DETECT_MODE_CNG;
  4052. } else {
  4053. flags = FAX_DETECT_MODE_BOTH;
  4054. }
  4055. faxdetect = fax_detect_attach(chan, fdtimeout, flags);
  4056. if (faxdetect < 0) {
  4057. ast_log(LOG_ERROR, "Error attaching FAX detect to channel %s.\n", ast_channel_name(chan));
  4058. res = -1;
  4059. } else {
  4060. ast_debug(1, "Attached FAX detect to channel %s.\n", ast_channel_name(chan));
  4061. }
  4062. } else {
  4063. ast_log(LOG_WARNING, "Attempt to attach a FAX detect on channel (%s) with FAX detect already running.\n", ast_channel_name(chan));
  4064. }
  4065. } else if (ast_false(val)) {
  4066. ast_channel_lock(chan);
  4067. ast_framehook_detach(chan, details->faxdetect_id);
  4068. ast_channel_unlock(chan);
  4069. details->faxdetect_id = -1;
  4070. } else {
  4071. ast_log(LOG_WARNING, "Unsupported value '%s' passed to FAXOPT(%s).\n", value, data);
  4072. }
  4073. } else if (!strcasecmp(data, "headerinfo")) {
  4074. ast_string_field_set(details, headerinfo, value);
  4075. } else if (!strcasecmp(data, "localstationid")) {
  4076. ast_string_field_set(details, localstationid, value);
  4077. } else if (!strcasecmp(data, "maxrate")) {
  4078. details->maxrate = fax_rate_str_to_int(value);
  4079. if (!details->maxrate) {
  4080. details->maxrate = ast_fax_maxrate();
  4081. }
  4082. } else if (!strcasecmp(data, "minrate")) {
  4083. details->minrate = fax_rate_str_to_int(value);
  4084. if (!details->minrate) {
  4085. details->minrate = ast_fax_minrate();
  4086. }
  4087. } else if (!strcasecmp(data, "t38timeout")) {
  4088. if (set_t38timeout(value, &details->t38timeout)) {
  4089. res = -1;
  4090. }
  4091. } else if ((!strcasecmp(data, "modem")) || (!strcasecmp(data, "modems"))) {
  4092. update_modem_bits(&details->modems, value);
  4093. } else {
  4094. ast_log(LOG_WARNING, "channel '%s' set FAXOPT(%s) to '%s' is unhandled!\n", ast_channel_name(chan), data, value);
  4095. res = -1;
  4096. }
  4097. ao2_ref(details, -1);
  4098. return res;
  4099. }
  4100. /*! \brief FAXOPT dialplan function */
  4101. struct ast_custom_function acf_faxopt = {
  4102. .name = "FAXOPT",
  4103. .read = acf_faxopt_read,
  4104. .write = acf_faxopt_write,
  4105. };
  4106. /*! \brief unload res_fax */
  4107. static int unload_module(void)
  4108. {
  4109. ast_cli_unregister_multiple(fax_cli, ARRAY_LEN(fax_cli));
  4110. if (ast_custom_function_unregister(&acf_faxopt) < 0) {
  4111. ast_log(LOG_WARNING, "failed to unregister function '%s'\n", acf_faxopt.name);
  4112. }
  4113. if (ast_unregister_application(app_sendfax) < 0) {
  4114. ast_log(LOG_WARNING, "failed to unregister '%s'\n", app_sendfax);
  4115. }
  4116. if (ast_unregister_application(app_receivefax) < 0) {
  4117. ast_log(LOG_WARNING, "failed to unregister '%s'\n", app_receivefax);
  4118. }
  4119. ast_manager_unregister("FAXSessions");
  4120. ast_manager_unregister("FAXSession");
  4121. ast_manager_unregister("FAXStats");
  4122. if (fax_logger_level != -1) {
  4123. ast_logger_unregister_level("FAX");
  4124. }
  4125. ao2_ref(faxregistry.container, -1);
  4126. return 0;
  4127. }
  4128. /*!
  4129. * \brief Load the module
  4130. *
  4131. * Module loading including tests for configuration or dependencies.
  4132. * This function can return AST_MODULE_LOAD_FAILURE, AST_MODULE_LOAD_DECLINE,
  4133. * or AST_MODULE_LOAD_SUCCESS. If a dependency or environment variable fails
  4134. * tests return AST_MODULE_LOAD_FAILURE. If the module can not load the
  4135. * configuration file or other non-critical problem return
  4136. * AST_MODULE_LOAD_DECLINE. On success return AST_MODULE_LOAD_SUCCESS.
  4137. */
  4138. static int load_module(void)
  4139. {
  4140. int res;
  4141. /* initialize the registry */
  4142. faxregistry.active_sessions = 0;
  4143. faxregistry.reserved_sessions = 0;
  4144. faxregistry.container = ao2_container_alloc_hash(AO2_ALLOC_OPT_LOCK_MUTEX, 0,
  4145. FAX_MAXBUCKETS, session_hash_cb, NULL, session_cmp_cb);
  4146. if (!faxregistry.container) {
  4147. return AST_MODULE_LOAD_DECLINE;
  4148. }
  4149. if (set_config(0) < 0) {
  4150. ast_log(LOG_ERROR, "failed to load configuration file '%s'\n", config);
  4151. ao2_ref(faxregistry.container, -1);
  4152. return AST_MODULE_LOAD_DECLINE;
  4153. }
  4154. /* register CLI operations and applications */
  4155. if (ast_register_application_xml(app_sendfax, sendfax_exec) < 0) {
  4156. ast_log(LOG_WARNING, "failed to register '%s'.\n", app_sendfax);
  4157. ao2_ref(faxregistry.container, -1);
  4158. return AST_MODULE_LOAD_DECLINE;
  4159. }
  4160. if (ast_register_application_xml(app_receivefax, receivefax_exec) < 0) {
  4161. ast_log(LOG_WARNING, "failed to register '%s'.\n", app_receivefax);
  4162. ast_unregister_application(app_sendfax);
  4163. ao2_ref(faxregistry.container, -1);
  4164. return AST_MODULE_LOAD_DECLINE;
  4165. }
  4166. if (ast_manager_register_xml("FAXSessions", EVENT_FLAG_CALL, manager_fax_sessions)) {
  4167. ast_log(LOG_WARNING, "failed to register 'FAXSessions' AMI command.\n");
  4168. ast_unregister_application(app_receivefax);
  4169. ast_unregister_application(app_sendfax);
  4170. ao2_ref(faxregistry.container, -1);
  4171. return AST_MODULE_LOAD_DECLINE;
  4172. }
  4173. if (ast_manager_register_xml("FAXSession", EVENT_FLAG_CALL, manager_fax_session)) {
  4174. ast_log(LOG_WARNING, "failed to register 'FAXSession' AMI command.\n");
  4175. ast_manager_unregister("FAXSession");
  4176. ast_unregister_application(app_receivefax);
  4177. ast_unregister_application(app_sendfax);
  4178. ao2_ref(faxregistry.container, -1);
  4179. return AST_MODULE_LOAD_DECLINE;
  4180. }
  4181. if (ast_manager_register_xml("FAXStats", EVENT_FLAG_REPORTING, manager_fax_stats)) {
  4182. ast_log(LOG_WARNING, "failed to register 'FAXStats' AMI command.\n");
  4183. ast_manager_unregister("FAXSession");
  4184. ast_manager_unregister("FAXSessions");
  4185. ast_unregister_application(app_receivefax);
  4186. ast_unregister_application(app_sendfax);
  4187. ao2_ref(faxregistry.container, -1);
  4188. return AST_MODULE_LOAD_DECLINE;
  4189. }
  4190. ast_cli_register_multiple(fax_cli, ARRAY_LEN(fax_cli));
  4191. res = ast_custom_function_register(&acf_faxopt);
  4192. fax_logger_level = ast_logger_register_level("FAX");
  4193. return res;
  4194. }
  4195. static int reload_module(void)
  4196. {
  4197. set_config(1);
  4198. return 0;
  4199. }
  4200. AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_GLOBAL_SYMBOLS | AST_MODFLAG_LOAD_ORDER, "Generic FAX Applications",
  4201. .support_level = AST_MODULE_SUPPORT_CORE,
  4202. .load = load_module,
  4203. .unload = unload_module,
  4204. .reload = reload_module,
  4205. .load_pri = AST_MODPRI_APP_DEPEND,
  4206. );