res_fax.c 90 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927
  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. *
  9. * See http://www.asterisk.org for more information about
  10. * the Asterisk project. Please do not directly contact
  11. * any of the maintainers of this project for assistance;
  12. * the project provides a web site, mailing lists and IRC
  13. * channels for your use.
  14. *
  15. * This program is free software, distributed under the terms of
  16. * the GNU General Public License Version 2. See the LICENSE file
  17. * at the top of the source tree.
  18. */
  19. /*** MODULEINFO
  20. <support_level>core</support_level>
  21. <conflict>app_fax</conflict>
  22. ***/
  23. /*! \file
  24. *
  25. * \brief Generic FAX Resource for FAX technology resource modules
  26. *
  27. * \author Dwayne M. Hubbard <dhubbard@digium.com>
  28. * \author Kevin P. Fleming <kpfleming@digium.com>
  29. *
  30. * A generic FAX resource module that provides SendFAX and ReceiveFAX applications.
  31. * This module requires FAX technology modules, like res_fax_spandsp, to register with it
  32. * so it can use the technology modules to perform the actual FAX transmissions.
  33. * \ingroup applications
  34. */
  35. #include "asterisk.h"
  36. ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
  37. #include "asterisk/io.h"
  38. #include "asterisk/file.h"
  39. #include "asterisk/logger.h"
  40. #include "asterisk/module.h"
  41. #include "asterisk/app.h"
  42. #include "asterisk/lock.h"
  43. #include "asterisk/options.h"
  44. #include "asterisk/strings.h"
  45. #include "asterisk/cli.h"
  46. #include "asterisk/utils.h"
  47. #include "asterisk/config.h"
  48. #include "asterisk/astobj2.h"
  49. #include "asterisk/res_fax.h"
  50. #include "asterisk/file.h"
  51. #include "asterisk/channel.h"
  52. #include "asterisk/pbx.h"
  53. #include "asterisk/manager.h"
  54. #include "asterisk/dsp.h"
  55. #include "asterisk/indications.h"
  56. #include "asterisk/ast_version.h"
  57. /*** DOCUMENTATION
  58. <application name="ReceiveFAX" language="en_US" module="res_fax">
  59. <synopsis>
  60. Receive a FAX and save as a TIFF/F file.
  61. </synopsis>
  62. <syntax>
  63. <parameter name="filename" required="true" />
  64. <parameter name="options">
  65. <optionlist>
  66. <option name="d">
  67. <para>Enable FAX debugging.</para>
  68. </option>
  69. <option name="f">
  70. <para>Allow audio fallback FAX transfer on T.38 capable channels.</para>
  71. </option>
  72. <option name="s">
  73. <para>Send progress Manager events (overrides statusevents setting in res_fax.conf).</para>
  74. </option>
  75. </optionlist>
  76. </parameter>
  77. </syntax>
  78. <description>
  79. <para>This application is provided by res_fax, which is a FAX technology agnostic module
  80. that utilizes FAX technology resource modules to complete a FAX transmission.</para>
  81. <para>Session arguments can be set by the FAXOPT function and to check results of the ReceiveFax() application.</para>
  82. </description>
  83. <see-also>
  84. <ref type="function">FAXOPT</ref>
  85. </see-also>
  86. </application>
  87. <application name="SendFAX" language="en_US" module="res_fax">
  88. <synopsis>
  89. Sends a specified TIFF/F file as a FAX.
  90. </synopsis>
  91. <syntax>
  92. <parameter name="filename" required="true" argsep="&amp;">
  93. <argument name="filename2" multiple="true">
  94. <para>TIFF file to send as a FAX.</para>
  95. </argument>
  96. </parameter>
  97. <parameter name="options">
  98. <optionlist>
  99. <option name="d">
  100. <para>Enable FAX debugging.</para>
  101. </option>
  102. <option name="f">
  103. <para>Allow audio fallback FAX transfer 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. <option name="z">
  109. <para>Initiate a T.38 reinvite on the channel if the remote end does not.</para>
  110. </option>
  111. </optionlist>
  112. </parameter>
  113. </syntax>
  114. <description>
  115. <para>This application is provided by res_fax, which is a FAX technology agnostic module
  116. that utilizes FAX technology resource modules to complete a FAX transmission.</para>
  117. <para>Session arguments can be set by the FAXOPT function and to check results of the SendFax() application.</para>
  118. </description>
  119. <see-also>
  120. <ref type="function">FAXOPT</ref>
  121. </see-also>
  122. </application>
  123. <function name="FAXOPT" language="en_US" module="res_fax">
  124. <synopsis>
  125. Gets/sets various pieces of information about a fax session.
  126. </synopsis>
  127. <syntax>
  128. <parameter name="item" required="true">
  129. <enumlist>
  130. <enum name="ecm">
  131. <para>R/W Error Correction Mode (ECM) enable with 'yes', disable with 'no'.</para>
  132. </enum>
  133. <enum name="error">
  134. <para>R/O FAX transmission error code upon failure.</para>
  135. </enum>
  136. <enum name="filename">
  137. <para>R/O Filename of the first file of the FAX transmission.</para>
  138. </enum>
  139. <enum name="filenames">
  140. <para>R/O Filenames of all of the files in the FAX transmission (comma separated).</para>
  141. </enum>
  142. <enum name="headerinfo">
  143. <para>R/W FAX header information.</para>
  144. </enum>
  145. <enum name="localstationid">
  146. <para>R/W Local Station Identification.</para>
  147. </enum>
  148. <enum name="minrate">
  149. <para>R/W Minimum transfer rate set before transmission.</para>
  150. </enum>
  151. <enum name="maxrate">
  152. <para>R/W Maximum transfer rate set before transmission.</para>
  153. </enum>
  154. <enum name="modem">
  155. <para>R/W Modem type (v17/v27/v29).</para>
  156. </enum>
  157. <enum name="pages">
  158. <para>R/O Number of pages transferred.</para>
  159. </enum>
  160. <enum name="rate">
  161. <para>R/O Negotiated transmission rate.</para>
  162. </enum>
  163. <enum name="remotestationid">
  164. <para>R/O Remote Station Identification after transmission.</para>
  165. </enum>
  166. <enum name="resolution">
  167. <para>R/O Negotiated image resolution after transmission.</para>
  168. </enum>
  169. <enum name="sessionid">
  170. <para>R/O Session ID of the FAX transmission.</para>
  171. </enum>
  172. <enum name="status">
  173. <para>R/O Result Status of the FAX transmission.</para>
  174. </enum>
  175. <enum name="statusstr">
  176. <para>R/O Verbose Result Status of the FAX transmission.</para>
  177. </enum>
  178. </enumlist>
  179. </parameter>
  180. </syntax>
  181. <description>
  182. <para>FAXOPT can be used to override the settings for a FAX session listed in <filename>res_fax.conf</filename>,
  183. it can also be used to retreive information about a FAX session that has finished eg. pages/status.</para>
  184. </description>
  185. <see-also>
  186. <ref type="application">ReceiveFax</ref>
  187. <ref type="application">SendFax</ref>
  188. </see-also>
  189. </function>
  190. ***/
  191. static const char app_receivefax[] = "ReceiveFAX";
  192. static const char app_sendfax[] = "SendFAX";
  193. struct debug_info_history {
  194. unsigned int consec_frames;
  195. unsigned int consec_ms;
  196. unsigned char silence;
  197. };
  198. struct ast_fax_debug_info {
  199. struct timeval base_tv;
  200. struct debug_info_history c2s, s2c;
  201. struct ast_dsp *dsp;
  202. };
  203. static int fax_logger_level = -1;
  204. /*! \brief maximum buckets for res_fax ao2 containers */
  205. #define FAX_MAXBUCKETS 10
  206. #define RES_FAX_TIMEOUT 10000
  207. /*! \brief The faxregistry is used to manage information and statistics for all FAX sessions. */
  208. static struct {
  209. /*! The number of active FAX sessions */
  210. int active_sessions;
  211. /*! The number of reserved FAX sessions */
  212. int reserved_sessions;
  213. /*! active sessions are astobj2 objects */
  214. struct ao2_container *container;
  215. /*! Total number of Tx FAX attempts */
  216. int fax_tx_attempts;
  217. /*! Total number of Rx FAX attempts */
  218. int fax_rx_attempts;
  219. /*! Number of successful FAX transmissions */
  220. int fax_complete;
  221. /*! Number of failed FAX transmissions */
  222. int fax_failures;
  223. /*! the next unique session name */
  224. int nextsessionname;
  225. } faxregistry;
  226. /*! \brief registered FAX technology modules are put into this list */
  227. struct fax_module {
  228. const struct ast_fax_tech *tech;
  229. AST_RWLIST_ENTRY(fax_module) list;
  230. };
  231. static AST_RWLIST_HEAD_STATIC(faxmodules, fax_module);
  232. #define RES_FAX_MINRATE 2400
  233. #define RES_FAX_MAXRATE 14400
  234. #define RES_FAX_STATUSEVENTS 0
  235. #define RES_FAX_MODEM (AST_FAX_MODEM_V17 | AST_FAX_MODEM_V27 | AST_FAX_MODEM_V29)
  236. struct fax_options {
  237. enum ast_fax_modems modems;
  238. uint32_t statusevents:1;
  239. uint32_t ecm:1;
  240. unsigned int minrate;
  241. unsigned int maxrate;
  242. };
  243. static struct fax_options general_options;
  244. static const struct fax_options default_options = {
  245. .minrate = RES_FAX_MINRATE,
  246. .maxrate = RES_FAX_MAXRATE,
  247. .statusevents = RES_FAX_STATUSEVENTS,
  248. .modems = RES_FAX_MODEM,
  249. .ecm = AST_FAX_OPTFLAG_TRUE,
  250. };
  251. AST_RWLOCK_DEFINE_STATIC(options_lock);
  252. static void get_general_options(struct fax_options* options);
  253. static void set_general_options(const struct fax_options* options);
  254. static const char *config = "res_fax.conf";
  255. static int global_fax_debug = 0;
  256. enum {
  257. OPT_CALLEDMODE = (1 << 0),
  258. OPT_CALLERMODE = (1 << 1),
  259. OPT_DEBUG = (1 << 2),
  260. OPT_STATUS = (1 << 3),
  261. OPT_ALLOWAUDIO = (1 << 5),
  262. OPT_REQUEST_T38 = (1 << 6),
  263. };
  264. AST_APP_OPTIONS(fax_exec_options, BEGIN_OPTIONS
  265. AST_APP_OPTION('a', OPT_CALLEDMODE),
  266. AST_APP_OPTION('c', OPT_CALLERMODE),
  267. AST_APP_OPTION('d', OPT_DEBUG),
  268. AST_APP_OPTION('f', OPT_ALLOWAUDIO),
  269. AST_APP_OPTION('s', OPT_STATUS),
  270. AST_APP_OPTION('z', OPT_REQUEST_T38),
  271. END_OPTIONS);
  272. struct manager_event_info {
  273. char context[AST_MAX_CONTEXT];
  274. char exten[AST_MAX_EXTENSION];
  275. char cid[128];
  276. };
  277. static void debug_check_frame_for_silence(struct ast_fax_session *s, unsigned int c2s, struct ast_frame *frame)
  278. {
  279. struct debug_info_history *history = c2s ? &s->debug_info->c2s : &s->debug_info->s2c;
  280. int dspsilence;
  281. unsigned int last_consec_frames, last_consec_ms;
  282. unsigned char wassil;
  283. struct timeval diff;
  284. diff = ast_tvsub(ast_tvnow(), s->debug_info->base_tv);
  285. ast_dsp_reset(s->debug_info->dsp);
  286. ast_dsp_silence(s->debug_info->dsp, frame, &dspsilence);
  287. wassil = history->silence;
  288. history->silence = (dspsilence != 0) ? 1 : 0;
  289. if (history->silence != wassil) {
  290. last_consec_frames = history->consec_frames;
  291. last_consec_ms = history->consec_ms;
  292. history->consec_frames = 0;
  293. history->consec_ms = 0;
  294. if ((last_consec_frames != 0)) {
  295. ast_verb(6, "Channel '%s' fax session '%d', [ %.3ld.%.6ld ], %s sent %d frames (%d ms) of %s.\n",
  296. s->channame, s->id, (long) diff.tv_sec, (long int) diff.tv_usec,
  297. (c2s) ? "channel" : "stack", last_consec_frames, last_consec_ms,
  298. (wassil) ? "silence" : "energy");
  299. }
  300. }
  301. history->consec_frames++;
  302. history->consec_ms += (frame->samples / 8);
  303. }
  304. static void destroy_callback(void *data)
  305. {
  306. if (data) {
  307. ao2_ref(data, -1);
  308. }
  309. }
  310. static const struct ast_datastore_info fax_datastore = {
  311. .type = "res_fax",
  312. .destroy = destroy_callback,
  313. };
  314. /*! \brief returns a reference counted pointer to a fax datastore, if it exists */
  315. static struct ast_fax_session_details *find_details(struct ast_channel *chan)
  316. {
  317. struct ast_fax_session_details *details;
  318. struct ast_datastore *datastore;
  319. ast_channel_lock(chan);
  320. if (!(datastore = ast_channel_datastore_find(chan, &fax_datastore, NULL))) {
  321. ast_channel_unlock(chan);
  322. return NULL;
  323. }
  324. if (!(details = datastore->data)) {
  325. ast_log(LOG_WARNING, "Huh? channel '%s' has a FAX datastore without data!\n", chan->name);
  326. ast_channel_unlock(chan);
  327. return NULL;
  328. }
  329. ao2_ref(details, 1);
  330. ast_channel_unlock(chan);
  331. return details;
  332. }
  333. /*! \brief destroy a FAX session details structure */
  334. static void destroy_session_details(void *details)
  335. {
  336. struct ast_fax_session_details *d = details;
  337. struct ast_fax_document *doc;
  338. while ((doc = AST_LIST_REMOVE_HEAD(&d->documents, next))) {
  339. ast_free(doc);
  340. }
  341. ast_string_field_free_memory(d);
  342. }
  343. /*! \brief create a FAX session details structure */
  344. static struct ast_fax_session_details *session_details_new(void)
  345. {
  346. struct ast_fax_session_details *d;
  347. struct fax_options options;
  348. if (!(d = ao2_alloc(sizeof(*d), destroy_session_details))) {
  349. return NULL;
  350. }
  351. if (ast_string_field_init(d, 512)) {
  352. ao2_ref(d, -1);
  353. return NULL;
  354. }
  355. get_general_options(&options);
  356. AST_LIST_HEAD_INIT_NOLOCK(&d->documents);
  357. /* These options need to be set to the configured default and may be overridden by
  358. * SendFAX, ReceiveFAX, or FAXOPT */
  359. d->option.request_t38 = AST_FAX_OPTFLAG_FALSE;
  360. d->option.send_cng = AST_FAX_OPTFLAG_FALSE;
  361. d->option.send_ced = AST_FAX_OPTFLAG_FALSE;
  362. d->option.ecm = options.ecm;
  363. d->option.statusevents = options.statusevents;
  364. d->modems = options.modems;
  365. d->minrate = options.minrate;
  366. d->maxrate = options.maxrate;
  367. return d;
  368. }
  369. /*! \brief returns a reference counted details structure from the channel's fax datastore. If the datastore
  370. * does not exist it will be created */
  371. static struct ast_fax_session_details *find_or_create_details(struct ast_channel *chan)
  372. {
  373. struct ast_fax_session_details *details;
  374. struct ast_datastore *datastore;
  375. if ((details = find_details(chan))) {
  376. return details;
  377. }
  378. /* channel does not have one so we must create one */
  379. if (!(details = session_details_new())) {
  380. ast_log(LOG_WARNING, "channel '%s' can't get a FAX details structure for the datastore!\n", chan->name);
  381. return NULL;
  382. }
  383. if (!(datastore = ast_datastore_alloc(&fax_datastore, NULL))) {
  384. ao2_ref(details, -1);
  385. ast_log(LOG_WARNING, "channel '%s' can't get a datastore!\n", chan->name);
  386. return NULL;
  387. }
  388. /* add the datastore to the channel and increment the refcount */
  389. datastore->data = details;
  390. ao2_ref(details, 1);
  391. ast_channel_lock(chan);
  392. ast_channel_datastore_add(chan, datastore);
  393. ast_channel_unlock(chan);
  394. return details;
  395. }
  396. unsigned int ast_fax_maxrate(void)
  397. {
  398. struct fax_options options;
  399. get_general_options(&options);
  400. return options.maxrate;
  401. }
  402. unsigned int ast_fax_minrate(void)
  403. {
  404. struct fax_options options;
  405. get_general_options(&options);
  406. return options.minrate;
  407. }
  408. static int update_modem_bits(enum ast_fax_modems *bits, const char *value)
  409. {
  410. char *m[5], *tok, *v = (char *)value;
  411. int i = 0, j;
  412. if (!(tok = strchr(v, ','))) {
  413. m[i++] = v;
  414. m[i] = NULL;
  415. } else {
  416. tok = strtok(v, ", ");
  417. while (tok && (i < 5)) {
  418. m[i++] = tok;
  419. tok = strtok(NULL, ", ");
  420. }
  421. m[i] = NULL;
  422. }
  423. *bits = 0;
  424. for (j = 0; j < i; j++) {
  425. if (!strcasecmp(m[j], "v17")) {
  426. *bits |= AST_FAX_MODEM_V17;
  427. } else if (!strcasecmp(m[j], "v27")) {
  428. *bits |= AST_FAX_MODEM_V27;
  429. } else if (!strcasecmp(m[j], "v29")) {
  430. *bits |= AST_FAX_MODEM_V29;
  431. } else if (!strcasecmp(m[j], "v34")) {
  432. *bits |= AST_FAX_MODEM_V34;
  433. } else {
  434. ast_log(LOG_WARNING, "ignoring invalid modem setting: '%s', valid options {v17 | v27 | v29 | v34}\n", m[j]);
  435. }
  436. }
  437. return 0;
  438. }
  439. static char *ast_fax_caps_to_str(enum ast_fax_capabilities caps, char *buf, size_t bufsize)
  440. {
  441. char *out = buf;
  442. size_t size = bufsize;
  443. int first = 1;
  444. if (caps & AST_FAX_TECH_SEND) {
  445. if (!first) {
  446. ast_build_string(&buf, &size, ",");
  447. }
  448. ast_build_string(&buf, &size, "SEND");
  449. first = 0;
  450. }
  451. if (caps & AST_FAX_TECH_RECEIVE) {
  452. if (!first) {
  453. ast_build_string(&buf, &size, ",");
  454. }
  455. ast_build_string(&buf, &size, "RECEIVE");
  456. first = 0;
  457. }
  458. if (caps & AST_FAX_TECH_AUDIO) {
  459. if (!first) {
  460. ast_build_string(&buf, &size, ",");
  461. }
  462. ast_build_string(&buf, &size, "AUDIO");
  463. first = 0;
  464. }
  465. if (caps & AST_FAX_TECH_T38) {
  466. if (!first) {
  467. ast_build_string(&buf, &size, ",");
  468. }
  469. ast_build_string(&buf, &size, "T38");
  470. first = 0;
  471. }
  472. if (caps & AST_FAX_TECH_MULTI_DOC) {
  473. if (!first) {
  474. ast_build_string(&buf, &size, ",");
  475. }
  476. ast_build_string(&buf, &size, "MULTI_DOC");
  477. first = 0;
  478. }
  479. return out;
  480. }
  481. static int ast_fax_modem_to_str(enum ast_fax_modems bits, char *tbuf, size_t bufsize)
  482. {
  483. int count = 0;
  484. if (bits & AST_FAX_MODEM_V17) {
  485. strcat(tbuf, "V17");
  486. count++;
  487. }
  488. if (bits & AST_FAX_MODEM_V27) {
  489. if (count) {
  490. strcat(tbuf, ",");
  491. }
  492. strcat(tbuf, "V27");
  493. count++;
  494. }
  495. if (bits & AST_FAX_MODEM_V29) {
  496. if (count) {
  497. strcat(tbuf, ",");
  498. }
  499. strcat(tbuf, "V29");
  500. count++;
  501. }
  502. if (bits & AST_FAX_MODEM_V34) {
  503. if (count) {
  504. strcat(tbuf, ",");
  505. }
  506. strcat(tbuf, "V34");
  507. count++;
  508. }
  509. return 0;
  510. }
  511. static int check_modem_rate(enum ast_fax_modems modems, unsigned int rate)
  512. {
  513. switch (rate) {
  514. case 2400:
  515. if (!(modems & (AST_FAX_MODEM_V27 | AST_FAX_MODEM_V34))) {
  516. return 1;
  517. }
  518. break;
  519. case 4800:
  520. if (!(modems & (AST_FAX_MODEM_V27 | AST_FAX_MODEM_V34))) {
  521. return 1;
  522. }
  523. break;
  524. case 7200:
  525. case 9600:
  526. if (!(modems & (AST_FAX_MODEM_V17 | AST_FAX_MODEM_V29 | AST_FAX_MODEM_V34))) {
  527. return 1;
  528. }
  529. break;
  530. case 12000:
  531. case 14400:
  532. if (!(modems & (AST_FAX_MODEM_V17 | AST_FAX_MODEM_V34))) {
  533. return 1;
  534. }
  535. break;
  536. case 28800:
  537. case 33600:
  538. if (!(modems & AST_FAX_MODEM_V34)) {
  539. return 1;
  540. }
  541. break;
  542. default:
  543. /* this should never happen */
  544. return 1;
  545. }
  546. return 0;
  547. }
  548. /*! \brief register a FAX technology module */
  549. int ast_fax_tech_register(struct ast_fax_tech *tech)
  550. {
  551. struct fax_module *fax;
  552. if (!(fax = ast_calloc(1, sizeof(*fax)))) {
  553. return -1;
  554. }
  555. fax->tech = tech;
  556. AST_RWLIST_WRLOCK(&faxmodules);
  557. AST_RWLIST_INSERT_TAIL(&faxmodules, fax, list);
  558. AST_RWLIST_UNLOCK(&faxmodules);
  559. ast_module_ref(ast_module_info->self);
  560. ast_verb(3, "Registered handler for '%s' (%s)\n", fax->tech->type, fax->tech->description);
  561. return 0;
  562. }
  563. /*! \brief unregister a FAX technology module */
  564. void ast_fax_tech_unregister(struct ast_fax_tech *tech)
  565. {
  566. struct fax_module *fax;
  567. ast_verb(3, "Unregistering FAX module type '%s'\n", tech->type);
  568. AST_RWLIST_WRLOCK(&faxmodules);
  569. AST_RWLIST_TRAVERSE_SAFE_BEGIN(&faxmodules, fax, list) {
  570. if (fax->tech != tech) {
  571. continue;
  572. }
  573. AST_RWLIST_REMOVE_CURRENT(list);
  574. ast_module_unref(ast_module_info->self);
  575. ast_free(fax);
  576. ast_verb(4, "Unregistered FAX module type '%s'\n", tech->type);
  577. break;
  578. }
  579. AST_RWLIST_TRAVERSE_SAFE_END;
  580. AST_RWLIST_UNLOCK(&faxmodules);
  581. }
  582. /*! \brief convert a ast_fax_state to a string */
  583. const char *ast_fax_state_to_str(enum ast_fax_state state)
  584. {
  585. switch (state) {
  586. case AST_FAX_STATE_UNINITIALIZED:
  587. return "Uninitialized";
  588. case AST_FAX_STATE_INITIALIZED:
  589. return "Initialized";
  590. case AST_FAX_STATE_OPEN:
  591. return "Open";
  592. case AST_FAX_STATE_ACTIVE:
  593. return "Active";
  594. case AST_FAX_STATE_COMPLETE:
  595. return "Complete";
  596. case AST_FAX_STATE_RESERVED:
  597. return "Reserved";
  598. case AST_FAX_STATE_INACTIVE:
  599. return "Inactive";
  600. default:
  601. ast_log(LOG_WARNING, "unhandled FAX state: %d\n", state);
  602. return "Unknown";
  603. }
  604. }
  605. void ast_fax_log(int level, const char *file, const int line, const char *function, const char *msg)
  606. {
  607. if (fax_logger_level != -1) {
  608. ast_log_dynamic_level(fax_logger_level, "%s", msg);
  609. } else {
  610. ast_log(level, file, line, function, "%s", msg);
  611. }
  612. }
  613. /*! \brief convert a rate string to a rate */
  614. static unsigned int fax_rate_str_to_int(const char *ratestr)
  615. {
  616. int rate;
  617. if (sscanf(ratestr, "%d", &rate) != 1) {
  618. ast_log(LOG_ERROR, "failed to sscanf '%s' to rate\n", ratestr);
  619. return 0;
  620. }
  621. switch (rate) {
  622. case 2400:
  623. case 4800:
  624. case 7200:
  625. case 9600:
  626. case 12000:
  627. case 14400:
  628. case 28800:
  629. case 33600:
  630. return rate;
  631. default:
  632. ast_log(LOG_WARNING, "ignoring invalid rate '%s'. Valid options are {2400 | 4800 | 7200 | 9600 | 12000 | 14400 | 28800 | 33600}\n", ratestr);
  633. return 0;
  634. }
  635. }
  636. static void fax_session_release(struct ast_fax_session *s, struct ast_fax_tech_token *token)
  637. {
  638. if (token) {
  639. s->tech->release_token(token);
  640. }
  641. if (s->state == AST_FAX_STATE_RESERVED) {
  642. ast_atomic_fetchadd_int(&faxregistry.reserved_sessions, -1);
  643. s->state = AST_FAX_STATE_INACTIVE;
  644. }
  645. }
  646. /*! \brief destroy a FAX session structure */
  647. static void destroy_session(void *session)
  648. {
  649. struct ast_fax_session *s = session;
  650. if (s->tech) {
  651. fax_session_release(s, NULL);
  652. if (s->tech_pvt) {
  653. s->tech->destroy_session(s);
  654. }
  655. ast_module_unref(s->tech->module);
  656. }
  657. if (s->details) {
  658. ao2_ref(s->details, -1);
  659. }
  660. if (s->debug_info) {
  661. ast_dsp_free(s->debug_info->dsp);
  662. ast_free(s->debug_info);
  663. }
  664. if (s->smoother) {
  665. ast_smoother_free(s->smoother);
  666. }
  667. if (s->state != AST_FAX_STATE_INACTIVE) {
  668. ast_atomic_fetchadd_int(&faxregistry.active_sessions, -1);
  669. }
  670. ast_free(s->channame);
  671. ast_free(s->chan_uniqueid);
  672. }
  673. static struct ast_fax_session *fax_session_reserve(struct ast_fax_session_details *details, struct ast_fax_tech_token **token)
  674. {
  675. struct ast_fax_session *s;
  676. struct fax_module *faxmod;
  677. char caps[128] = "";
  678. if (!(s = ao2_alloc(sizeof(*s), destroy_session))) {
  679. return NULL;
  680. }
  681. s->state = AST_FAX_STATE_INACTIVE;
  682. /* locate a FAX technology module that can handle said requirements
  683. * Note: the requirements have not yet been finalized as T.38
  684. * negotiation has not yet occured. */
  685. AST_RWLIST_RDLOCK(&faxmodules);
  686. AST_RWLIST_TRAVERSE(&faxmodules, faxmod, list) {
  687. if ((faxmod->tech->caps & details->caps) != details->caps) {
  688. continue;
  689. }
  690. ast_debug(4, "Reserving a FAX session from '%s'.\n", faxmod->tech->description);
  691. ast_module_ref(faxmod->tech->module);
  692. s->tech = faxmod->tech;
  693. break;
  694. }
  695. AST_RWLIST_UNLOCK(&faxmodules);
  696. if (!faxmod) {
  697. 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)));
  698. ao2_ref(s, -1);
  699. return NULL;
  700. }
  701. if (!s->tech->reserve_session) {
  702. ast_debug(1, "Selected FAX technology module (%s) does not support reserving sessions.\n", s->tech->description);
  703. return s;
  704. }
  705. if (!(*token = s->tech->reserve_session(s))) {
  706. ao2_ref(s, -1);
  707. return NULL;
  708. }
  709. s->state = AST_FAX_STATE_RESERVED;
  710. ast_atomic_fetchadd_int(&faxregistry.reserved_sessions, 1);
  711. return s;
  712. }
  713. /*! \brief create a FAX session */
  714. 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)
  715. {
  716. struct ast_fax_session *s = NULL;
  717. struct fax_module *faxmod;
  718. char caps[128] = "";
  719. if (reserved) {
  720. s = reserved;
  721. ao2_ref(reserved, +1);
  722. if (s->state == AST_FAX_STATE_RESERVED) {
  723. ast_atomic_fetchadd_int(&faxregistry.reserved_sessions, -1);
  724. s->state = AST_FAX_STATE_UNINITIALIZED;
  725. }
  726. }
  727. if (!s && !(s = ao2_alloc(sizeof(*s), destroy_session))) {
  728. return NULL;
  729. }
  730. ast_atomic_fetchadd_int(&faxregistry.active_sessions, 1);
  731. s->state = AST_FAX_STATE_UNINITIALIZED;
  732. if (details->option.debug && (details->caps & AST_FAX_TECH_AUDIO)) {
  733. if (!(s->debug_info = ast_calloc(1, sizeof(*(s->debug_info))))) {
  734. fax_session_release(s, token);
  735. ao2_ref(s, -1);
  736. return NULL;
  737. }
  738. if (!(s->debug_info->dsp = ast_dsp_new())) {
  739. ast_free(s->debug_info);
  740. s->debug_info = NULL;
  741. fax_session_release(s, token);
  742. ao2_ref(s, -1);
  743. return NULL;
  744. }
  745. ast_dsp_set_threshold(s->debug_info->dsp, 128);
  746. }
  747. if (!(s->channame = ast_strdup(chan->name))) {
  748. fax_session_release(s, token);
  749. ao2_ref(s, -1);
  750. return NULL;
  751. }
  752. if (!(s->chan_uniqueid = ast_strdup(chan->uniqueid))) {
  753. fax_session_release(s, token);
  754. ao2_ref(s, -1);
  755. return NULL;
  756. }
  757. s->chan = chan;
  758. s->details = details;
  759. ao2_ref(s->details, 1);
  760. details->id = s->id = ast_atomic_fetchadd_int(&faxregistry.nextsessionname, 1);
  761. if (!token) {
  762. /* locate a FAX technology module that can handle said requirements */
  763. AST_RWLIST_RDLOCK(&faxmodules);
  764. AST_RWLIST_TRAVERSE(&faxmodules, faxmod, list) {
  765. if ((faxmod->tech->caps & details->caps) != details->caps) {
  766. continue;
  767. }
  768. ast_debug(4, "Requesting a new FAX session from '%s'.\n", faxmod->tech->description);
  769. ast_module_ref(faxmod->tech->module);
  770. s->tech = faxmod->tech;
  771. break;
  772. }
  773. AST_RWLIST_UNLOCK(&faxmodules);
  774. if (!faxmod) {
  775. 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)));
  776. ao2_ref(s, -1);
  777. return NULL;
  778. }
  779. }
  780. if (!(s->tech_pvt = s->tech->new_session(s, token))) {
  781. ast_log(LOG_ERROR, "FAX session failed to initialize.\n");
  782. ao2_ref(s, -1);
  783. return NULL;
  784. }
  785. /* link the session to the session container */
  786. if (!(ao2_link(faxregistry.container, s))) {
  787. ast_log(LOG_ERROR, "failed to add FAX session '%d' to container.\n", s->id);
  788. ao2_ref(s, -1);
  789. return NULL;
  790. }
  791. ast_debug(4, "channel '%s' using FAX session '%d'\n", s->channame, s->id);
  792. return s;
  793. }
  794. static void get_manager_event_info(struct ast_channel *chan, struct manager_event_info *info)
  795. {
  796. pbx_substitute_variables_helper(chan, "${CONTEXT}", info->context, sizeof(info->context));
  797. pbx_substitute_variables_helper(chan, "${EXTEN}", info->exten, sizeof(info->exten));
  798. pbx_substitute_variables_helper(chan, "${CALLERID(num)}", info->cid, sizeof(info->cid));
  799. }
  800. /* \brief Generate a string of filenames using the given prefix and separator.
  801. * \param details the fax session details
  802. * \param prefix the prefix to each filename
  803. * \param separator the separator between filenames
  804. *
  805. * This function generates a string of filenames from the given details
  806. * structure and using the given prefix and separator.
  807. *
  808. * \retval NULL there was an error generating the string
  809. * \return the string generated string
  810. */
  811. static char *generate_filenames_string(struct ast_fax_session_details *details, char *prefix, char *separator)
  812. {
  813. char *filenames, *c;
  814. size_t size = 0;
  815. int first = 1;
  816. struct ast_fax_document *doc;
  817. /* don't process empty lists */
  818. if (AST_LIST_EMPTY(&details->documents)) {
  819. return NULL;
  820. }
  821. /* Calculate the total length of all of the file names */
  822. AST_LIST_TRAVERSE(&details->documents, doc, next) {
  823. size += strlen(separator) + strlen(prefix) + strlen(doc->filename);
  824. }
  825. size += 1; /* add space for the terminating null */
  826. if (!(filenames = ast_malloc(size))) {
  827. return NULL;
  828. }
  829. c = filenames;
  830. ast_build_string(&c, &size, "%s%s", prefix, AST_LIST_FIRST(&details->documents)->filename);
  831. AST_LIST_TRAVERSE(&details->documents, doc, next) {
  832. if (first) {
  833. first = 0;
  834. continue;
  835. }
  836. ast_build_string(&c, &size, "%s%s%s", separator, prefix, doc->filename);
  837. }
  838. return filenames;
  839. }
  840. /*! \brief send a FAX status manager event */
  841. static int report_fax_status(struct ast_channel *chan, struct ast_fax_session_details *details, const char *status)
  842. {
  843. char *filenames = generate_filenames_string(details, "FileName: ", "\r\n");
  844. if (!filenames) {
  845. return 1;
  846. }
  847. ast_channel_lock(chan);
  848. if (details->option.statusevents) {
  849. struct manager_event_info info;
  850. get_manager_event_info(chan, &info);
  851. manager_event(EVENT_FLAG_CALL,
  852. (details->caps & AST_FAX_TECH_RECEIVE) ? "ReceiveFAXStatus" : "SendFAXStatus",
  853. "Status: %s\r\n"
  854. "Channel: %s\r\n"
  855. "Context: %s\r\n"
  856. "Exten: %s\r\n"
  857. "CallerID: %s\r\n"
  858. "LocalStationID: %s\r\n"
  859. "%s\r\n",
  860. status,
  861. chan->name,
  862. info.context,
  863. info.exten,
  864. info.cid,
  865. details->localstationid,
  866. filenames);
  867. }
  868. ast_channel_unlock(chan);
  869. ast_free(filenames);
  870. return 0;
  871. }
  872. /*! \brief Set fax related channel variables. */
  873. static void set_channel_variables(struct ast_channel *chan, struct ast_fax_session_details *details)
  874. {
  875. char buf[10];
  876. pbx_builtin_setvar_helper(chan, "FAXSTATUS", S_OR(details->result, NULL));
  877. pbx_builtin_setvar_helper(chan, "FAXERROR", S_OR(details->error, NULL));
  878. pbx_builtin_setvar_helper(chan, "FAXSTATUSSTRING", S_OR(details->resultstr, NULL));
  879. pbx_builtin_setvar_helper(chan, "REMOTESTATIONID", S_OR(details->remotestationid, NULL));
  880. pbx_builtin_setvar_helper(chan, "LOCALSTATIONID", S_OR(details->localstationid, NULL));
  881. pbx_builtin_setvar_helper(chan, "FAXBITRATE", S_OR(details->transfer_rate, NULL));
  882. pbx_builtin_setvar_helper(chan, "FAXRESOLUTION", S_OR(details->resolution, NULL));
  883. snprintf(buf, sizeof(buf), "%d", details->pages_transferred);
  884. pbx_builtin_setvar_helper(chan, "FAXPAGES", buf);
  885. }
  886. #define GENERIC_FAX_EXEC_SET_VARS(fax, chan, errorstr, reason) \
  887. do { \
  888. if (ast_strlen_zero(fax->details->result)) \
  889. ast_string_field_set(fax->details, result, "FAILED"); \
  890. if (ast_strlen_zero(fax->details->resultstr)) \
  891. ast_string_field_set(fax->details, resultstr, reason); \
  892. if (ast_strlen_zero(fax->details->error)) \
  893. ast_string_field_set(fax->details, error, errorstr); \
  894. set_channel_variables(chan, fax->details); \
  895. } while (0)
  896. #define GENERIC_FAX_EXEC_ERROR_QUIET(fax, chan, errorstr, reason) \
  897. do { \
  898. GENERIC_FAX_EXEC_SET_VARS(fax, chan, errorstr, reason); \
  899. res = ms = -1; \
  900. } while (0)
  901. #define GENERIC_FAX_EXEC_ERROR(fax, chan, errorstr, reason) \
  902. do { \
  903. ast_log(LOG_ERROR, "channel '%s' FAX session '%d' failure, reason: '%s' (%s)\n", chan->name, fax->id, reason, errorstr); \
  904. GENERIC_FAX_EXEC_ERROR_QUIET(fax, chan, errorstr, reason); \
  905. } while (0)
  906. static void t38_parameters_ast_to_fax(struct ast_fax_t38_parameters *dst, const struct ast_control_t38_parameters *src)
  907. {
  908. dst->version = src->version;
  909. dst->max_ifp = src->max_ifp;
  910. dst->rate = src->rate;
  911. dst->rate_management = src->rate_management;
  912. dst->fill_bit_removal = src->fill_bit_removal;
  913. dst->transcoding_mmr = src->transcoding_mmr;
  914. dst->transcoding_jbig = src->transcoding_jbig;
  915. }
  916. static void t38_parameters_fax_to_ast(struct ast_control_t38_parameters *dst, const struct ast_fax_t38_parameters *src)
  917. {
  918. dst->version = src->version;
  919. dst->max_ifp = src->max_ifp;
  920. dst->rate = src->rate;
  921. dst->rate_management = src->rate_management;
  922. dst->fill_bit_removal = src->fill_bit_removal;
  923. dst->transcoding_mmr = src->transcoding_mmr;
  924. dst->transcoding_jbig = src->transcoding_jbig;
  925. }
  926. static int set_fax_t38_caps(struct ast_channel *chan, struct ast_fax_session_details *details)
  927. {
  928. switch (ast_channel_get_t38_state(chan)) {
  929. case T38_STATE_UNKNOWN:
  930. details->caps |= AST_FAX_TECH_T38;
  931. break;
  932. case T38_STATE_UNAVAILABLE:
  933. details->caps |= AST_FAX_TECH_AUDIO;
  934. break;
  935. case T38_STATE_NEGOTIATING: {
  936. /* the other end already sent us a T.38 reinvite, so we need to prod the channel
  937. * driver into resending their parameters to us if it supports doing so... if
  938. * not, we can't proceed, because we can't create a proper reply without them.
  939. * if it does work, the channel driver will send an AST_CONTROL_T38_PARAMETERS
  940. * with a request of AST_T38_REQUEST_NEGOTIATE, which will be read by the function
  941. * that gets called after this one completes
  942. */
  943. struct ast_control_t38_parameters parameters = { .request_response = AST_T38_REQUEST_PARMS, };
  944. if (ast_indicate_data(chan, AST_CONTROL_T38_PARAMETERS, &parameters, sizeof(parameters)) != AST_T38_REQUEST_PARMS) {
  945. ast_log(LOG_ERROR, "channel '%s' is in an unsupported T.38 negotiation state, cannot continue.\n", chan->name);
  946. return -1;
  947. }
  948. details->caps |= AST_FAX_TECH_T38;
  949. break;
  950. }
  951. default:
  952. ast_log(LOG_ERROR, "channel '%s' is in an unsupported T.38 negotiation state, cannot continue.\n", chan->name);
  953. return -1;
  954. }
  955. return 0;
  956. }
  957. static int disable_t38(struct ast_channel *chan)
  958. {
  959. int ms;
  960. struct ast_frame *frame = NULL;
  961. struct ast_control_t38_parameters t38_parameters = { .request_response = AST_T38_REQUEST_TERMINATE, };
  962. ast_debug(1, "Shutting down T.38 on %s\n", chan->name);
  963. if (ast_indicate_data(chan, AST_CONTROL_T38_PARAMETERS, &t38_parameters, sizeof(t38_parameters)) != 0) {
  964. ast_debug(1, "error while disabling T.38 on channel '%s'\n", chan->name);
  965. return -1;
  966. }
  967. /* wait up to five seconds for negotiation to complete */
  968. ms = 5000;
  969. while (ms > 0) {
  970. ms = ast_waitfor(chan, ms);
  971. if (ms < 0) {
  972. ast_debug(1, "error while disabling T.38 on channel '%s'\n", chan->name);
  973. return -1;
  974. }
  975. if (ms == 0) { /* all done, nothing happened */
  976. ast_debug(1, "channel '%s' timed-out during T.38 shutdown\n", chan->name);
  977. break;
  978. }
  979. if (!(frame = ast_read(chan))) {
  980. return -1;
  981. }
  982. if ((frame->frametype == AST_FRAME_CONTROL) &&
  983. (frame->subclass.integer == AST_CONTROL_T38_PARAMETERS) &&
  984. (frame->datalen == sizeof(t38_parameters))) {
  985. struct ast_control_t38_parameters *parameters = frame->data.ptr;
  986. switch (parameters->request_response) {
  987. case AST_T38_TERMINATED:
  988. ast_debug(1, "Shut down T.38 on %s\n", chan->name);
  989. break;
  990. case AST_T38_REFUSED:
  991. ast_log(LOG_WARNING, "channel '%s' refused to disable T.38\n", chan->name);
  992. ast_frfree(frame);
  993. return -1;
  994. default:
  995. ast_log(LOG_ERROR, "channel '%s' failed to disable T.38\n", chan->name);
  996. ast_frfree(frame);
  997. return -1;
  998. }
  999. ast_frfree(frame);
  1000. break;
  1001. }
  1002. ast_frfree(frame);
  1003. }
  1004. return 0;
  1005. }
  1006. static struct ast_control_t38_parameters our_t38_parameters = {
  1007. .version = 0,
  1008. .max_ifp = 400,
  1009. .rate = AST_T38_RATE_14400,
  1010. .rate_management = AST_T38_RATE_MANAGEMENT_TRANSFERRED_TCF,
  1011. };
  1012. /*! \brief this is the generic FAX session handling function */
  1013. 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)
  1014. {
  1015. int ms;
  1016. int timeout = RES_FAX_TIMEOUT;
  1017. int res = 0, chancount;
  1018. unsigned int expected_frametype = -1;
  1019. union ast_frame_subclass expected_framesubclass = { .integer = -1 };
  1020. unsigned int t38negotiated = (ast_channel_get_t38_state(chan) == T38_STATE_NEGOTIATED);
  1021. struct ast_control_t38_parameters t38_parameters;
  1022. const char *tempvar;
  1023. struct ast_fax_session *fax = NULL;
  1024. struct ast_frame *frame = NULL;
  1025. struct ast_channel *c = chan;
  1026. unsigned int orig_write_format = 0, orig_read_format = 0;
  1027. chancount = 1;
  1028. /* create the FAX session */
  1029. if (!(fax = fax_session_new(details, chan, reserved, token))) {
  1030. ast_log(LOG_ERROR, "Can't create a FAX session, FAX attempt failed.\n");
  1031. report_fax_status(chan, details, "No Available Resource");
  1032. return -1;
  1033. }
  1034. ast_channel_lock(chan);
  1035. /* update session details */
  1036. if (ast_strlen_zero(details->headerinfo) && (tempvar = pbx_builtin_getvar_helper(chan, "LOCALHEADERINFO"))) {
  1037. ast_string_field_set(details, headerinfo, tempvar);
  1038. }
  1039. if (ast_strlen_zero(details->localstationid)) {
  1040. tempvar = pbx_builtin_getvar_helper(chan, "LOCALSTATIONID");
  1041. ast_string_field_set(details, localstationid, tempvar ? tempvar : "unknown");
  1042. }
  1043. ast_channel_unlock(chan);
  1044. report_fax_status(chan, details, "Allocating Resources");
  1045. if (details->caps & AST_FAX_TECH_AUDIO) {
  1046. expected_frametype = AST_FRAME_VOICE;;
  1047. expected_framesubclass.codec = AST_FORMAT_SLINEAR;
  1048. orig_write_format = chan->writeformat;
  1049. if (ast_set_write_format(chan, AST_FORMAT_SLINEAR) < 0) {
  1050. ast_log(LOG_ERROR, "channel '%s' failed to set write format to signed linear'.\n", chan->name);
  1051. ao2_lock(faxregistry.container);
  1052. ao2_unlink(faxregistry.container, fax);
  1053. ao2_unlock(faxregistry.container);
  1054. ao2_ref(fax, -1);
  1055. ast_channel_unlock(chan);
  1056. return -1;
  1057. }
  1058. orig_read_format = chan->readformat;
  1059. if (ast_set_read_format(chan, AST_FORMAT_SLINEAR) < 0) {
  1060. ast_log(LOG_ERROR, "channel '%s' failed to set read format to signed linear.\n", chan->name);
  1061. ao2_lock(faxregistry.container);
  1062. ao2_unlink(faxregistry.container, fax);
  1063. ao2_unlock(faxregistry.container);
  1064. ao2_ref(fax, -1);
  1065. ast_channel_unlock(chan);
  1066. return -1;
  1067. }
  1068. if (fax->smoother) {
  1069. ast_smoother_free(fax->smoother);
  1070. fax->smoother = NULL;
  1071. }
  1072. if (!(fax->smoother = ast_smoother_new(320))) {
  1073. ast_log(LOG_WARNING, "Channel '%s' FAX session '%d' failed to obtain a smoother.\n", chan->name, fax->id);
  1074. }
  1075. } else {
  1076. expected_frametype = AST_FRAME_MODEM;
  1077. expected_framesubclass.codec = AST_MODEM_T38;
  1078. }
  1079. if (fax->debug_info) {
  1080. fax->debug_info->base_tv = ast_tvnow();
  1081. }
  1082. /* reset our result fields just in case the fax tech driver wants to
  1083. * set custom error messages */
  1084. ast_string_field_set(details, result, "");
  1085. ast_string_field_set(details, resultstr, "");
  1086. ast_string_field_set(details, error, "");
  1087. set_channel_variables(chan, details);
  1088. if (fax->tech->start_session(fax) < 0) {
  1089. GENERIC_FAX_EXEC_ERROR(fax, chan, "INIT_ERROR", "failed to start FAX session");
  1090. }
  1091. report_fax_status(chan, details, "FAX Transmission In Progress");
  1092. ast_debug(5, "channel %s will wait on FAX fd %d\n", chan->name, fax->fd);
  1093. /* handle frames for the session */
  1094. ms = 1000;
  1095. while ((res > -1) && (ms > -1) && (timeout > 0)) {
  1096. struct ast_channel *ready_chan;
  1097. int ofd, exception;
  1098. ms = 1000;
  1099. errno = 0;
  1100. ready_chan = ast_waitfor_nandfds(&c, chancount, &fax->fd, 1, &exception, &ofd, &ms);
  1101. if (ready_chan) {
  1102. if (!(frame = ast_read(chan))) {
  1103. /* the channel is probably gone, so lets stop polling on it and let the
  1104. * FAX session complete before we exit the application. if needed,
  1105. * send the FAX stack silence so the modems can finish their session without
  1106. * any problems */
  1107. ast_debug(1, "Channel '%s' did not return a frame; probably hung up.\n", chan->name);
  1108. GENERIC_FAX_EXEC_SET_VARS(fax, chan, "HANGUP", "remote channel hungup");
  1109. c = NULL;
  1110. chancount = 0;
  1111. timeout -= (1000 - ms);
  1112. fax->tech->cancel_session(fax);
  1113. if (fax->tech->generate_silence) {
  1114. fax->tech->generate_silence(fax);
  1115. }
  1116. continue;
  1117. }
  1118. if ((frame->frametype == AST_FRAME_CONTROL) &&
  1119. (frame->subclass.integer == AST_CONTROL_T38_PARAMETERS) &&
  1120. (frame->datalen == sizeof(t38_parameters))) {
  1121. unsigned int was_t38 = t38negotiated;
  1122. struct ast_control_t38_parameters *parameters = frame->data.ptr;
  1123. switch (parameters->request_response) {
  1124. case AST_T38_REQUEST_NEGOTIATE:
  1125. /* the other end has requested a switch to T.38, so reply that we are willing, if we can
  1126. * do T.38 as well
  1127. */
  1128. t38_parameters_fax_to_ast(&t38_parameters, &details->our_t38_parameters);
  1129. t38_parameters.request_response = (details->caps & AST_FAX_TECH_T38) ? AST_T38_NEGOTIATED : AST_T38_REFUSED;
  1130. ast_indicate_data(chan, AST_CONTROL_T38_PARAMETERS, &t38_parameters, sizeof(t38_parameters));
  1131. break;
  1132. case AST_T38_NEGOTIATED:
  1133. t38_parameters_ast_to_fax(&details->their_t38_parameters, parameters);
  1134. t38negotiated = 1;
  1135. break;
  1136. default:
  1137. break;
  1138. }
  1139. if (t38negotiated && !was_t38) {
  1140. fax->tech->switch_to_t38(fax);
  1141. details->caps &= ~AST_FAX_TECH_AUDIO;
  1142. expected_frametype = AST_FRAME_MODEM;
  1143. expected_framesubclass.codec = AST_MODEM_T38;
  1144. if (fax->smoother) {
  1145. ast_smoother_free(fax->smoother);
  1146. fax->smoother = NULL;
  1147. }
  1148. report_fax_status(chan, details, "T.38 Negotiated");
  1149. ast_verb(3, "Channel '%s' switched to T.38 FAX session '%d'.\n", chan->name, fax->id);
  1150. }
  1151. } else if ((frame->frametype == expected_frametype) &&
  1152. (!memcmp(&frame->subclass, &expected_framesubclass, sizeof(frame->subclass)))) {
  1153. struct ast_frame *f;
  1154. if (fax->smoother) {
  1155. /* push the frame into a smoother */
  1156. if (ast_smoother_feed(fax->smoother, frame) < 0) {
  1157. GENERIC_FAX_EXEC_ERROR(fax, chan, "UNKNOWN", "Failed to feed the smoother");
  1158. }
  1159. while ((f = ast_smoother_read(fax->smoother)) && (f->data.ptr)) {
  1160. if (fax->debug_info) {
  1161. debug_check_frame_for_silence(fax, 1, f);
  1162. }
  1163. /* write the frame to the FAX stack */
  1164. fax->tech->write(fax, f);
  1165. fax->frames_received++;
  1166. if (f != frame) {
  1167. ast_frfree(f);
  1168. }
  1169. }
  1170. } else {
  1171. /* write the frame to the FAX stack */
  1172. fax->tech->write(fax, frame);
  1173. fax->frames_received++;
  1174. }
  1175. timeout = RES_FAX_TIMEOUT;
  1176. }
  1177. ast_frfree(frame);
  1178. } else if (ofd == fax->fd) {
  1179. /* read a frame from the FAX stack and send it out the channel.
  1180. * the FAX stack will return a NULL if the FAX session has already completed */
  1181. if (!(frame = fax->tech->read(fax))) {
  1182. break;
  1183. }
  1184. if (fax->debug_info && (frame->frametype == AST_FRAME_VOICE)) {
  1185. debug_check_frame_for_silence(fax, 0, frame);
  1186. }
  1187. ast_write(chan, frame);
  1188. fax->frames_sent++;
  1189. ast_frfree(frame);
  1190. timeout = RES_FAX_TIMEOUT;
  1191. } else {
  1192. if (ms && (ofd < 0)) {
  1193. if ((errno == 0) || (errno == EINTR)) {
  1194. timeout -= (1000 - ms);
  1195. if (timeout <= 0)
  1196. GENERIC_FAX_EXEC_ERROR(fax, chan, "TIMEOUT", "fax session timed-out");
  1197. continue;
  1198. } else {
  1199. ast_log(LOG_WARNING, "something bad happened while channel '%s' was polling.\n", chan->name);
  1200. GENERIC_FAX_EXEC_ERROR(fax, chan, "UNKNOWN", "error polling data");
  1201. res = ms;
  1202. break;
  1203. }
  1204. } else {
  1205. /* nothing happened */
  1206. if (timeout > 0) {
  1207. timeout -= 1000;
  1208. if (timeout <= 0)
  1209. GENERIC_FAX_EXEC_ERROR(fax, chan, "TIMEOUT", "fax session timed-out");
  1210. continue;
  1211. } else {
  1212. ast_log(LOG_WARNING, "channel '%s' timed-out during the FAX transmission.\n", chan->name);
  1213. GENERIC_FAX_EXEC_ERROR(fax, chan, "TIMEOUT", "fax session timed-out");
  1214. break;
  1215. }
  1216. }
  1217. }
  1218. }
  1219. ast_debug(3, "channel '%s' - event loop stopped { timeout: %d, ms: %d, res: %d }\n", chan->name, timeout, ms, res);
  1220. set_channel_variables(chan, details);
  1221. if (!strcasecmp(details->result, "FAILED")) {
  1222. ast_atomic_fetchadd_int(&faxregistry.fax_failures, 1);
  1223. } else {
  1224. ast_atomic_fetchadd_int(&faxregistry.fax_complete, 1);
  1225. }
  1226. if (fax) {
  1227. ao2_lock(faxregistry.container);
  1228. ao2_unlink(faxregistry.container, fax);
  1229. ao2_unlock(faxregistry.container);
  1230. ao2_ref(fax, -1);
  1231. }
  1232. /* if the channel is still alive, and we changed its read/write formats,
  1233. * restore them now
  1234. */
  1235. if (chancount) {
  1236. if (orig_read_format) {
  1237. ast_set_read_format(chan, orig_read_format);
  1238. }
  1239. if (orig_write_format) {
  1240. ast_set_write_format(chan, orig_write_format);
  1241. }
  1242. }
  1243. /* return the chancount so the calling function can determine if the channel hungup during this FAX session or not */
  1244. return chancount;
  1245. }
  1246. static int receivefax_t38_init(struct ast_channel *chan, struct ast_fax_session_details *details)
  1247. {
  1248. int ms;
  1249. struct ast_frame *frame = NULL;
  1250. struct ast_control_t38_parameters t38_parameters;
  1251. t38_parameters_ast_to_fax(&details->our_t38_parameters, &our_t38_parameters);
  1252. /* don't send any audio if we've already received a T.38 reinvite */
  1253. if (ast_channel_get_t38_state(chan) != T38_STATE_NEGOTIATING) {
  1254. /* generate 3 seconds of CED */
  1255. if (ast_playtones_start(chan, 1024, "!2100/3000", 1)) {
  1256. ast_log(LOG_ERROR, "error generating CED tone on %s\n", chan->name);
  1257. return -1;
  1258. }
  1259. ms = 3000;
  1260. while (ms > 0) {
  1261. ms = ast_waitfor(chan, ms);
  1262. if (ms < 0) {
  1263. ast_log(LOG_ERROR, "error while generating CED tone on %s\n", chan->name);
  1264. ast_playtones_stop(chan);
  1265. return -1;
  1266. }
  1267. if (ms == 0) { /* all done, nothing happened */
  1268. break;
  1269. }
  1270. if (!(frame = ast_read(chan))) {
  1271. ast_log(LOG_ERROR, "error reading frame while generating CED tone on %s\n", chan->name);
  1272. ast_playtones_stop(chan);
  1273. return -1;
  1274. }
  1275. if ((frame->frametype == AST_FRAME_CONTROL) &&
  1276. (frame->subclass.integer == AST_CONTROL_T38_PARAMETERS) &&
  1277. (frame->datalen == sizeof(t38_parameters))) {
  1278. struct ast_control_t38_parameters *parameters = frame->data.ptr;
  1279. switch (parameters->request_response) {
  1280. case AST_T38_REQUEST_NEGOTIATE:
  1281. /* the other end has requested a switch to T.38, so reply that we are willing, if we can
  1282. * do T.38 as well
  1283. */
  1284. t38_parameters_fax_to_ast(&t38_parameters, &details->our_t38_parameters);
  1285. t38_parameters.request_response = (details->caps & AST_FAX_TECH_T38) ? AST_T38_NEGOTIATED : AST_T38_REFUSED;
  1286. ast_indicate_data(chan, AST_CONTROL_T38_PARAMETERS, &t38_parameters, sizeof(t38_parameters));
  1287. ast_playtones_stop(chan);
  1288. break;
  1289. case AST_T38_NEGOTIATED:
  1290. ast_debug(1, "Negotiated T.38 for receive on %s\n", chan->name);
  1291. t38_parameters_ast_to_fax(&details->their_t38_parameters, parameters);
  1292. details->caps &= ~AST_FAX_TECH_AUDIO;
  1293. report_fax_status(chan, details, "T.38 Negotiated");
  1294. break;
  1295. default:
  1296. break;
  1297. }
  1298. }
  1299. ast_frfree(frame);
  1300. }
  1301. ast_playtones_stop(chan);
  1302. }
  1303. /* if T.38 was negotiated, we are done initializing */
  1304. if (ast_channel_get_t38_state(chan) == T38_STATE_NEGOTIATED) {
  1305. return 0;
  1306. }
  1307. /* request T.38 */
  1308. ast_debug(1, "Negotiating T.38 for receive on %s\n", chan->name);
  1309. /* wait up to five seconds for negotiation to complete */
  1310. ms = 5000;
  1311. /* set parameters based on the session's parameters */
  1312. t38_parameters_fax_to_ast(&t38_parameters, &details->our_t38_parameters);
  1313. t38_parameters.request_response = AST_T38_REQUEST_NEGOTIATE;
  1314. if ((ast_indicate_data(chan, AST_CONTROL_T38_PARAMETERS, &t38_parameters, sizeof(t38_parameters)) != 0)) {
  1315. return -1;
  1316. }
  1317. while (ms > 0) {
  1318. ms = ast_waitfor(chan, ms);
  1319. if (ms < 0) {
  1320. ast_log(LOG_WARNING, "error on '%s' while waiting for T.38 negotiation.\n", chan->name);
  1321. return -1;
  1322. }
  1323. if (ms == 0) { /* all done, nothing happened */
  1324. ast_log(LOG_WARNING, "channel '%s' timed-out during the T.38 negotiation.\n", chan->name);
  1325. details->caps &= ~AST_FAX_TECH_T38;
  1326. break;
  1327. }
  1328. if (!(frame = ast_read(chan))) {
  1329. ast_log(LOG_WARNING, "error on '%s' while waiting for T.38 negotiation.\n", chan->name);
  1330. return -1;
  1331. }
  1332. if ((frame->frametype == AST_FRAME_CONTROL) &&
  1333. (frame->subclass.integer == AST_CONTROL_T38_PARAMETERS) &&
  1334. (frame->datalen == sizeof(t38_parameters))) {
  1335. struct ast_control_t38_parameters *parameters = frame->data.ptr;
  1336. switch (parameters->request_response) {
  1337. case AST_T38_REQUEST_NEGOTIATE:
  1338. t38_parameters_fax_to_ast(&t38_parameters, &details->our_t38_parameters);
  1339. t38_parameters.request_response = AST_T38_NEGOTIATED;
  1340. ast_indicate_data(chan, AST_CONTROL_T38_PARAMETERS, &t38_parameters, sizeof(t38_parameters));
  1341. break;
  1342. case AST_T38_NEGOTIATED:
  1343. ast_debug(1, "Negotiated T.38 for receive on %s\n", chan->name);
  1344. t38_parameters_ast_to_fax(&details->their_t38_parameters, parameters);
  1345. details->caps &= ~AST_FAX_TECH_AUDIO;
  1346. report_fax_status(chan, details, "T.38 Negotiated");
  1347. ms = 0;
  1348. break;
  1349. case AST_T38_REFUSED:
  1350. ast_log(LOG_WARNING, "channel '%s' refused to negotiate T.38\n", chan->name);
  1351. details->caps &= ~AST_FAX_TECH_T38;
  1352. ms = 0;
  1353. break;
  1354. default:
  1355. ast_log(LOG_ERROR, "channel '%s' failed to negotiate T.38\n", chan->name);
  1356. details->caps &= ~AST_FAX_TECH_T38;
  1357. ms = 0;
  1358. break;
  1359. }
  1360. }
  1361. ast_frfree(frame);
  1362. }
  1363. /* if T.38 was negotiated, we are done initializing */
  1364. if (ast_channel_get_t38_state(chan) == T38_STATE_NEGOTIATED) {
  1365. return 0;
  1366. }
  1367. /* if we made it here, then T.38 failed, check the 'f' flag */
  1368. if (details->option.allow_audio != AST_FAX_OPTFLAG_TRUE) {
  1369. ast_log(LOG_WARNING, "Audio FAX not allowed on channel '%s' and T.38 negotiation failed; aborting.\n", chan->name);
  1370. return -1;
  1371. }
  1372. /* ok, audio fallback is allowed */
  1373. details->caps |= AST_FAX_TECH_AUDIO;
  1374. return 0;
  1375. }
  1376. /*! \brief initiate a receive FAX session */
  1377. static int receivefax_exec(struct ast_channel *chan, const char *data)
  1378. {
  1379. char *parse, modems[128] = "";
  1380. int channel_alive;
  1381. struct ast_fax_session_details *details;
  1382. struct ast_fax_session *s;
  1383. struct ast_fax_tech_token *token = NULL;
  1384. struct ast_fax_document *doc;
  1385. AST_DECLARE_APP_ARGS(args,
  1386. AST_APP_ARG(filename);
  1387. AST_APP_ARG(options);
  1388. );
  1389. struct ast_flags opts = { 0, };
  1390. struct manager_event_info info;
  1391. /* initialize output channel variables */
  1392. pbx_builtin_setvar_helper(chan, "FAXSTATUS", "FAILED");
  1393. pbx_builtin_setvar_helper(chan, "REMOTESTATIONID", NULL);
  1394. pbx_builtin_setvar_helper(chan, "FAXPAGES", "0");
  1395. pbx_builtin_setvar_helper(chan, "FAXBITRATE", NULL);
  1396. pbx_builtin_setvar_helper(chan, "FAXRESOLUTION", NULL);
  1397. /* if we ran receivefax then we attempted to receive a fax, even if we
  1398. * never start a fax session */
  1399. ast_atomic_fetchadd_int(&faxregistry.fax_rx_attempts, 1);
  1400. /* Get a FAX session details structure from the channel's FAX datastore and create one if
  1401. * it does not already exist. */
  1402. if (!(details = find_or_create_details(chan))) {
  1403. ast_atomic_fetchadd_int(&faxregistry.fax_failures, 1);
  1404. pbx_builtin_setvar_helper(chan, "FAXERROR", "MEMORY_ERROR");
  1405. pbx_builtin_setvar_helper(chan, "FAXSTATUSSTRING", "error allocating memory");
  1406. ast_log(LOG_ERROR, "System cannot provide memory for session requirements.\n");
  1407. return -1;
  1408. }
  1409. ast_string_field_set(details, result, "FAILED");
  1410. ast_string_field_set(details, resultstr, "error starting fax session");
  1411. ast_string_field_set(details, error, "INIT_ERROR");
  1412. set_channel_variables(chan, details);
  1413. if (details->maxrate < details->minrate) {
  1414. ast_atomic_fetchadd_int(&faxregistry.fax_failures, 1);
  1415. ast_string_field_set(details, error, "INVALID_ARGUMENTS");
  1416. ast_string_field_set(details, resultstr, "maxrate is less than minrate");
  1417. set_channel_variables(chan, details);
  1418. ast_log(LOG_ERROR, "maxrate %d is less than minrate %d\n", details->maxrate, details->minrate);
  1419. ao2_ref(details, -1);
  1420. return -1;
  1421. }
  1422. if (check_modem_rate(details->modems, details->minrate)) {
  1423. ast_atomic_fetchadd_int(&faxregistry.fax_failures, 1);
  1424. ast_fax_modem_to_str(details->modems, modems, sizeof(modems));
  1425. ast_log(LOG_ERROR, "'modems' setting '%s' is incompatible with 'minrate' setting %d\n", modems, details->minrate);
  1426. ast_string_field_set(details, error, "INVALID_ARGUMENTS");
  1427. ast_string_field_set(details, resultstr, "incompatible 'modems' and 'minrate' settings");
  1428. set_channel_variables(chan, details);
  1429. ao2_ref(details, -1);
  1430. return -1;
  1431. }
  1432. if (check_modem_rate(details->modems, details->maxrate)) {
  1433. ast_atomic_fetchadd_int(&faxregistry.fax_failures, 1);
  1434. ast_fax_modem_to_str(details->modems, modems, sizeof(modems));
  1435. ast_log(LOG_ERROR, "'modems' setting '%s' is incompatible with 'maxrate' setting %d\n", modems, details->maxrate);
  1436. ast_string_field_set(details, error, "INVALID_ARGUMENTS");
  1437. ast_string_field_set(details, resultstr, "incompatible 'modems' and 'maxrate' settings");
  1438. set_channel_variables(chan, details);
  1439. ao2_ref(details, -1);
  1440. return -1;
  1441. }
  1442. if (ast_strlen_zero(data)) {
  1443. ast_atomic_fetchadd_int(&faxregistry.fax_failures, 1);
  1444. ast_string_field_set(details, error, "INVALID_ARGUMENTS");
  1445. ast_string_field_set(details, resultstr, "invalid arguments");
  1446. set_channel_variables(chan, details);
  1447. ast_log(LOG_WARNING, "%s requires an argument (filename[,options])\n", app_receivefax);
  1448. ao2_ref(details, -1);
  1449. return -1;
  1450. }
  1451. parse = ast_strdupa(data);
  1452. AST_STANDARD_APP_ARGS(args, parse);
  1453. if (!ast_strlen_zero(args.options) &&
  1454. ast_app_parse_options(fax_exec_options, &opts, NULL, args.options)) {
  1455. ast_atomic_fetchadd_int(&faxregistry.fax_failures, 1);
  1456. ast_string_field_set(details, error, "INVALID_ARGUMENTS");
  1457. ast_string_field_set(details, resultstr, "invalid arguments");
  1458. set_channel_variables(chan, details);
  1459. ao2_ref(details, -1);
  1460. return -1;
  1461. }
  1462. if (ast_strlen_zero(args.filename)) {
  1463. ast_atomic_fetchadd_int(&faxregistry.fax_failures, 1);
  1464. ast_string_field_set(details, error, "INVALID_ARGUMENTS");
  1465. ast_string_field_set(details, resultstr, "invalid arguments");
  1466. set_channel_variables(chan, details);
  1467. ast_log(LOG_WARNING, "%s requires an argument (filename[,options])\n", app_receivefax);
  1468. ao2_ref(details, -1);
  1469. return -1;
  1470. }
  1471. /* check for unsupported FAX application options */
  1472. if (ast_test_flag(&opts, OPT_CALLERMODE) || ast_test_flag(&opts, OPT_CALLEDMODE)) {
  1473. ast_atomic_fetchadd_int(&faxregistry.fax_failures, 1);
  1474. ast_string_field_set(details, error, "INVALID_ARGUMENTS");
  1475. ast_string_field_set(details, resultstr, "invalid arguments");
  1476. set_channel_variables(chan, details);
  1477. ast_log(LOG_WARNING, "%s does not support polling\n", app_receivefax);
  1478. ao2_ref(details, -1);
  1479. return -1;
  1480. }
  1481. pbx_builtin_setvar_helper(chan, "FAXERROR", "Channel Problems");
  1482. pbx_builtin_setvar_helper(chan, "FAXSTATUSSTRING", "Error before FAX transmission started.");
  1483. if (!(doc = ast_calloc(1, sizeof(*doc) + strlen(args.filename) + 1))) {
  1484. ast_atomic_fetchadd_int(&faxregistry.fax_failures, 1);
  1485. ast_string_field_set(details, error, "MEMORY_ERROR");
  1486. ast_string_field_set(details, resultstr, "error allocating memory");
  1487. set_channel_variables(chan, details);
  1488. ast_log(LOG_ERROR, "System cannot provide memory for session requirements.\n");
  1489. ao2_ref(details, -1);
  1490. return -1;
  1491. }
  1492. strcpy(doc->filename, args.filename);
  1493. AST_LIST_INSERT_TAIL(&details->documents, doc, next);
  1494. ast_verb(3, "Channel '%s' receiving FAX '%s'\n", chan->name, args.filename);
  1495. details->caps = AST_FAX_TECH_RECEIVE;
  1496. /* check for debug */
  1497. if (ast_test_flag(&opts, OPT_DEBUG) || global_fax_debug) {
  1498. details->option.debug = AST_FAX_OPTFLAG_TRUE;
  1499. }
  1500. /* check for request for status events */
  1501. if (ast_test_flag(&opts, OPT_STATUS)) {
  1502. details->option.statusevents = AST_FAX_OPTFLAG_TRUE;
  1503. }
  1504. if ((ast_channel_get_t38_state(chan) == T38_STATE_UNAVAILABLE) ||
  1505. ast_test_flag(&opts, OPT_ALLOWAUDIO)) {
  1506. details->option.allow_audio = AST_FAX_OPTFLAG_TRUE;
  1507. }
  1508. if (!(s = fax_session_reserve(details, &token))) {
  1509. ast_atomic_fetchadd_int(&faxregistry.fax_failures, 1);
  1510. ast_string_field_set(details, resultstr, "error reserving fax session");
  1511. set_channel_variables(chan, details);
  1512. ast_log(LOG_ERROR, "Unable to reserve FAX session.\n");
  1513. ao2_ref(details, -1);
  1514. return -1;
  1515. }
  1516. /* make sure the channel is up */
  1517. if (chan->_state != AST_STATE_UP) {
  1518. if (ast_answer(chan)) {
  1519. ast_atomic_fetchadd_int(&faxregistry.fax_failures, 1);
  1520. ast_string_field_set(details, resultstr, "error answering channel");
  1521. set_channel_variables(chan, details);
  1522. ast_log(LOG_WARNING, "Channel '%s' failed answer attempt.\n", chan->name);
  1523. fax_session_release(s, token);
  1524. ao2_ref(s, -1);
  1525. ao2_ref(details, -1);
  1526. return -1;
  1527. }
  1528. }
  1529. if (set_fax_t38_caps(chan, details)) {
  1530. ast_atomic_fetchadd_int(&faxregistry.fax_failures, 1);
  1531. ast_string_field_set(details, error, "T38_NEG_ERROR");
  1532. ast_string_field_set(details, resultstr, "error negotiating T.38");
  1533. set_channel_variables(chan, details);
  1534. fax_session_release(s, token);
  1535. ao2_ref(s, -1);
  1536. ao2_ref(details, -1);
  1537. return -1;
  1538. }
  1539. if (details->caps & AST_FAX_TECH_T38) {
  1540. if (receivefax_t38_init(chan, details)) {
  1541. ast_atomic_fetchadd_int(&faxregistry.fax_failures, 1);
  1542. ast_string_field_set(details, error, "T38_NEG_ERROR");
  1543. ast_string_field_set(details, resultstr, "error negotiating T.38");
  1544. set_channel_variables(chan, details);
  1545. fax_session_release(s, token);
  1546. ao2_ref(s, -1);
  1547. ao2_ref(details, -1);
  1548. ast_log(LOG_ERROR, "error initializing channel '%s' in T.38 mode\n", chan->name);
  1549. return -1;
  1550. }
  1551. } else {
  1552. details->option.send_ced = 1;
  1553. }
  1554. if ((channel_alive = generic_fax_exec(chan, details, s, token)) < 0) {
  1555. ast_atomic_fetchadd_int(&faxregistry.fax_failures, 1);
  1556. }
  1557. if (ast_channel_get_t38_state(chan) == T38_STATE_NEGOTIATED) {
  1558. if (disable_t38(chan)) {
  1559. ast_debug(1, "error disabling T.38 mode on %s\n", chan->name);
  1560. }
  1561. }
  1562. /* send out the AMI completion event */
  1563. ast_channel_lock(chan);
  1564. get_manager_event_info(chan, &info);
  1565. manager_event(EVENT_FLAG_CALL,
  1566. "ReceiveFAX",
  1567. "Channel: %s\r\n"
  1568. "Context: %s\r\n"
  1569. "Exten: %s\r\n"
  1570. "CallerID: %s\r\n"
  1571. "RemoteStationID: %s\r\n"
  1572. "LocalStationID: %s\r\n"
  1573. "PagesTransferred: %s\r\n"
  1574. "Resolution: %s\r\n"
  1575. "TransferRate: %s\r\n"
  1576. "FileName: %s\r\n",
  1577. chan->name,
  1578. info.context,
  1579. info.exten,
  1580. info.cid,
  1581. S_OR(pbx_builtin_getvar_helper(chan, "REMOTESTATIONID"), ""),
  1582. S_OR(pbx_builtin_getvar_helper(chan, "LOCALSTATIONID"), ""),
  1583. S_OR(pbx_builtin_getvar_helper(chan, "FAXPAGES"), ""),
  1584. S_OR(pbx_builtin_getvar_helper(chan, "FAXRESOLUTION"), ""),
  1585. S_OR(pbx_builtin_getvar_helper(chan, "FAXBITRATE"), ""),
  1586. args.filename);
  1587. ast_channel_unlock(chan);
  1588. ao2_ref(s, -1);
  1589. ao2_ref(details, -1);
  1590. /* If the channel hungup return -1; otherwise, return 0 to continue in the dialplan */
  1591. return (!channel_alive) ? -1 : 0;
  1592. }
  1593. static int sendfax_t38_init(struct ast_channel *chan, struct ast_fax_session_details *details)
  1594. {
  1595. int ms;
  1596. struct ast_frame *frame = NULL;
  1597. struct ast_control_t38_parameters t38_parameters;
  1598. t38_parameters_ast_to_fax(&details->our_t38_parameters, &our_t38_parameters);
  1599. /* send CNG tone while listening for the receiver to initiate a switch
  1600. * to T.38 mode; if they do, stop sending the CNG tone and proceed with
  1601. * the switch.
  1602. *
  1603. * 10500 is enough time for 3 CNG tones
  1604. */
  1605. ms = 10500;
  1606. /* don't send any audio if we've already received a T.38 reinvite */
  1607. if (ast_channel_get_t38_state(chan) != T38_STATE_NEGOTIATING) {
  1608. if (ast_playtones_start(chan, 1024, "!1100/500,!0/3000,!1100/500,!0/3000,!1100/500,!0/3000", 1)) {
  1609. ast_log(LOG_ERROR, "error generating CNG tone on %s\n", chan->name);
  1610. return -1;
  1611. }
  1612. }
  1613. while (ms > 0) {
  1614. ms = ast_waitfor(chan, ms);
  1615. if (ms < 0) {
  1616. ast_log(LOG_ERROR, "error while generating CNG tone on %s\n", chan->name);
  1617. ast_playtones_stop(chan);
  1618. return -1;
  1619. }
  1620. if (ms == 0) { /* all done, nothing happened */
  1621. break;
  1622. }
  1623. if (!(frame = ast_read(chan))) {
  1624. ast_log(LOG_ERROR, "error reading frame while generating CNG tone on %s\n", chan->name);
  1625. ast_playtones_stop(chan);
  1626. return -1;
  1627. }
  1628. if ((frame->frametype == AST_FRAME_CONTROL) &&
  1629. (frame->subclass.integer == AST_CONTROL_T38_PARAMETERS) &&
  1630. (frame->datalen == sizeof(t38_parameters))) {
  1631. struct ast_control_t38_parameters *parameters = frame->data.ptr;
  1632. switch (parameters->request_response) {
  1633. case AST_T38_REQUEST_NEGOTIATE:
  1634. /* the other end has requested a switch to T.38, so reply that we are willing, if we can
  1635. * do T.38 as well
  1636. */
  1637. t38_parameters_fax_to_ast(&t38_parameters, &details->our_t38_parameters);
  1638. t38_parameters.request_response = (details->caps & AST_FAX_TECH_T38) ? AST_T38_NEGOTIATED : AST_T38_REFUSED;
  1639. ast_indicate_data(chan, AST_CONTROL_T38_PARAMETERS, &t38_parameters, sizeof(t38_parameters));
  1640. ast_playtones_stop(chan);
  1641. break;
  1642. case AST_T38_NEGOTIATED:
  1643. ast_debug(1, "Negotiated T.38 for send on %s\n", chan->name);
  1644. t38_parameters_ast_to_fax(&details->their_t38_parameters, parameters);
  1645. details->caps &= ~AST_FAX_TECH_AUDIO;
  1646. report_fax_status(chan, details, "T.38 Negotiated");
  1647. ms = 0;
  1648. break;
  1649. default:
  1650. break;
  1651. }
  1652. }
  1653. ast_frfree(frame);
  1654. }
  1655. ast_playtones_stop(chan);
  1656. if (ast_channel_get_t38_state(chan) == T38_STATE_NEGOTIATED) {
  1657. return 0;
  1658. }
  1659. /* T.38 negotiation did not happen, initiate a switch if requested */
  1660. if (details->option.request_t38 == AST_FAX_OPTFLAG_TRUE) {
  1661. ast_debug(1, "Negotiating T.38 for send on %s\n", chan->name);
  1662. /* wait up to five seconds for negotiation to complete */
  1663. ms = 5000;
  1664. /* set parameters based on the session's parameters */
  1665. t38_parameters_fax_to_ast(&t38_parameters, &details->our_t38_parameters);
  1666. t38_parameters.request_response = AST_T38_REQUEST_NEGOTIATE;
  1667. if ((ast_indicate_data(chan, AST_CONTROL_T38_PARAMETERS, &t38_parameters, sizeof(t38_parameters)) != 0)) {
  1668. return -1;
  1669. }
  1670. while (ms > 0) {
  1671. ms = ast_waitfor(chan, ms);
  1672. if (ms < 0) {
  1673. ast_log(LOG_WARNING, "error on '%s' while waiting for T.38 negotiation.\n", chan->name);
  1674. return -1;
  1675. }
  1676. if (ms == 0) { /* all done, nothing happened */
  1677. ast_log(LOG_WARNING, "channel '%s' timed-out during the T.38 negotiation.\n", chan->name);
  1678. details->caps &= ~AST_FAX_TECH_T38;
  1679. break;
  1680. }
  1681. if (!(frame = ast_read(chan))) {
  1682. ast_log(LOG_WARNING, "error on '%s' while waiting for T.38 negotiation.\n", chan->name);
  1683. return -1;
  1684. }
  1685. if ((frame->frametype == AST_FRAME_CONTROL) &&
  1686. (frame->subclass.integer == AST_CONTROL_T38_PARAMETERS) &&
  1687. (frame->datalen == sizeof(t38_parameters))) {
  1688. struct ast_control_t38_parameters *parameters = frame->data.ptr;
  1689. switch (parameters->request_response) {
  1690. case AST_T38_REQUEST_NEGOTIATE:
  1691. t38_parameters_fax_to_ast(&t38_parameters, &details->our_t38_parameters);
  1692. t38_parameters.request_response = AST_T38_NEGOTIATED;
  1693. ast_indicate_data(chan, AST_CONTROL_T38_PARAMETERS, &t38_parameters, sizeof(t38_parameters));
  1694. break;
  1695. case AST_T38_NEGOTIATED:
  1696. ast_debug(1, "Negotiated T.38 for receive on %s\n", chan->name);
  1697. t38_parameters_ast_to_fax(&details->their_t38_parameters, parameters);
  1698. details->caps &= ~AST_FAX_TECH_AUDIO;
  1699. report_fax_status(chan, details, "T.38 Negotiated");
  1700. ms = 0;
  1701. break;
  1702. case AST_T38_REFUSED:
  1703. ast_log(LOG_WARNING, "channel '%s' refused to negotiate T.38\n", chan->name);
  1704. details->caps &= ~AST_FAX_TECH_T38;
  1705. ms = 0;
  1706. break;
  1707. default:
  1708. ast_log(LOG_ERROR, "channel '%s' failed to negotiate T.38\n", chan->name);
  1709. details->caps &= ~AST_FAX_TECH_T38;
  1710. ms = 0;
  1711. break;
  1712. }
  1713. }
  1714. ast_frfree(frame);
  1715. }
  1716. /* if T.38 was negotiated, we are done initializing */
  1717. if (ast_channel_get_t38_state(chan) == T38_STATE_NEGOTIATED) {
  1718. return 0;
  1719. }
  1720. /* send one more CNG tone to get audio going again for some
  1721. * carriers if we are going to fall back to audio mode */
  1722. if (details->option.allow_audio == AST_FAX_OPTFLAG_TRUE) {
  1723. if (ast_playtones_start(chan, 1024, "!1100/500,!0/3000", 1)) {
  1724. ast_log(LOG_ERROR, "error generating second CNG tone on %s\n", chan->name);
  1725. return -1;
  1726. }
  1727. ms = 3500;
  1728. while (ms > 0) {
  1729. ms = ast_waitfor(chan, ms);
  1730. if (ms < 0) {
  1731. ast_log(LOG_ERROR, "error while generating second CNG tone on %s\n", chan->name);
  1732. ast_playtones_stop(chan);
  1733. return -1;
  1734. }
  1735. if (ms == 0) { /* all done, nothing happened */
  1736. break;
  1737. }
  1738. if (!(frame = ast_read(chan))) {
  1739. ast_log(LOG_ERROR, "error reading frame while generating second CNG tone on %s\n", chan->name);
  1740. ast_playtones_stop(chan);
  1741. return -1;
  1742. }
  1743. if ((frame->frametype == AST_FRAME_CONTROL) &&
  1744. (frame->subclass.integer == AST_CONTROL_T38_PARAMETERS) &&
  1745. (frame->datalen == sizeof(t38_parameters))) {
  1746. struct ast_control_t38_parameters *parameters = frame->data.ptr;
  1747. switch (parameters->request_response) {
  1748. case AST_T38_REQUEST_NEGOTIATE:
  1749. /* the other end has requested a switch to T.38, so reply that we are willing, if we can
  1750. * do T.38 as well
  1751. */
  1752. t38_parameters_fax_to_ast(&t38_parameters, &details->our_t38_parameters);
  1753. t38_parameters.request_response = (details->caps & AST_FAX_TECH_T38) ? AST_T38_NEGOTIATED : AST_T38_REFUSED;
  1754. ast_indicate_data(chan, AST_CONTROL_T38_PARAMETERS, &t38_parameters, sizeof(t38_parameters));
  1755. ast_playtones_stop(chan);
  1756. break;
  1757. case AST_T38_NEGOTIATED:
  1758. ast_debug(1, "Negotiated T.38 for send on %s\n", chan->name);
  1759. t38_parameters_ast_to_fax(&details->their_t38_parameters, parameters);
  1760. details->caps &= ~AST_FAX_TECH_AUDIO;
  1761. report_fax_status(chan, details, "T.38 Negotiated");
  1762. ms = 0;
  1763. break;
  1764. default:
  1765. break;
  1766. }
  1767. }
  1768. ast_frfree(frame);
  1769. }
  1770. ast_playtones_stop(chan);
  1771. /* if T.38 was negotiated, we are done initializing */
  1772. if (ast_channel_get_t38_state(chan) == T38_STATE_NEGOTIATED) {
  1773. return 0;
  1774. }
  1775. }
  1776. }
  1777. /* if we made it here, then T.38 failed, check the 'f' flag */
  1778. if (details->option.allow_audio == AST_FAX_OPTFLAG_FALSE) {
  1779. ast_log(LOG_WARNING, "Audio FAX not allowed on channel '%s' and T.38 negotiation failed; aborting.\n", chan->name);
  1780. return -1;
  1781. }
  1782. /* ok, audio fallback is allowed */
  1783. details->caps |= AST_FAX_TECH_AUDIO;
  1784. return 0;
  1785. }
  1786. /*! \brief initiate a send FAX session */
  1787. static int sendfax_exec(struct ast_channel *chan, const char *data)
  1788. {
  1789. char *parse, *filenames, *c, modems[128] = "";
  1790. int channel_alive, file_count;
  1791. struct ast_fax_session_details *details;
  1792. struct ast_fax_session *s;
  1793. struct ast_fax_tech_token *token = NULL;
  1794. struct ast_fax_document *doc;
  1795. AST_DECLARE_APP_ARGS(args,
  1796. AST_APP_ARG(filenames);
  1797. AST_APP_ARG(options);
  1798. );
  1799. struct ast_flags opts = { 0, };
  1800. struct manager_event_info info;
  1801. /* initialize output channel variables */
  1802. pbx_builtin_setvar_helper(chan, "FAXSTATUS", "FAILED");
  1803. pbx_builtin_setvar_helper(chan, "REMOTESTATIONID", NULL);
  1804. pbx_builtin_setvar_helper(chan, "FAXPAGES", "0");
  1805. pbx_builtin_setvar_helper(chan, "FAXBITRATE", NULL);
  1806. pbx_builtin_setvar_helper(chan, "FAXRESOLUTION", NULL);
  1807. /* if we ran sendfax then we attempted to send a fax, even if we never
  1808. * start a fax session */
  1809. ast_atomic_fetchadd_int(&faxregistry.fax_tx_attempts, 1);
  1810. /* Get a requirement structure and set it. This structure is used
  1811. * to tell the FAX technology module about the higher level FAX session */
  1812. if (!(details = find_or_create_details(chan))) {
  1813. ast_atomic_fetchadd_int(&faxregistry.fax_failures, 1);
  1814. pbx_builtin_setvar_helper(chan, "FAXERROR", "MEMORY_ERROR");
  1815. pbx_builtin_setvar_helper(chan, "FAXSTATUSSTRING", "error allocating memory");
  1816. ast_log(LOG_ERROR, "System cannot provide memory for session requirements.\n");
  1817. return -1;
  1818. }
  1819. ast_string_field_set(details, result, "FAILED");
  1820. ast_string_field_set(details, resultstr, "error starting fax session");
  1821. ast_string_field_set(details, error, "INIT_ERROR");
  1822. set_channel_variables(chan, details);
  1823. if (details->maxrate < details->minrate) {
  1824. ast_atomic_fetchadd_int(&faxregistry.fax_failures, 1);
  1825. ast_string_field_set(details, error, "INVALID_ARGUMENTS");
  1826. ast_string_field_set(details, resultstr, "maxrate is less than minrate");
  1827. set_channel_variables(chan, details);
  1828. ast_log(LOG_ERROR, "maxrate %d is less than minrate %d\n", details->maxrate, details->minrate);
  1829. ao2_ref(details, -1);
  1830. return -1;
  1831. }
  1832. if (check_modem_rate(details->modems, details->minrate)) {
  1833. ast_atomic_fetchadd_int(&faxregistry.fax_failures, 1);
  1834. ast_fax_modem_to_str(details->modems, modems, sizeof(modems));
  1835. ast_log(LOG_ERROR, "'modems' setting '%s' is incompatible with 'minrate' setting %d\n", modems, details->minrate);
  1836. ast_string_field_set(details, error, "INVALID_ARGUMENTS");
  1837. ast_string_field_set(details, resultstr, "incompatible 'modems' and 'minrate' settings");
  1838. set_channel_variables(chan, details);
  1839. ao2_ref(details, -1);
  1840. return -1;
  1841. }
  1842. if (check_modem_rate(details->modems, details->maxrate)) {
  1843. ast_atomic_fetchadd_int(&faxregistry.fax_failures, 1);
  1844. ast_fax_modem_to_str(details->modems, modems, sizeof(modems));
  1845. ast_log(LOG_ERROR, "'modems' setting '%s' is incompatible with 'maxrate' setting %d\n", modems, details->maxrate);
  1846. ast_string_field_set(details, error, "INVALID_ARGUMENTS");
  1847. ast_string_field_set(details, resultstr, "incompatible 'modems' and 'maxrate' settings");
  1848. set_channel_variables(chan, details);
  1849. ao2_ref(details, -1);
  1850. return -1;
  1851. }
  1852. if (ast_strlen_zero(data)) {
  1853. ast_atomic_fetchadd_int(&faxregistry.fax_failures, 1);
  1854. ast_string_field_set(details, error, "INVALID_ARGUMENTS");
  1855. ast_string_field_set(details, resultstr, "invalid arguments");
  1856. set_channel_variables(chan, details);
  1857. ast_log(LOG_WARNING, "%s requires an argument (filename[&filename[&filename]][,options])\n", app_sendfax);
  1858. ao2_ref(details, -1);
  1859. return -1;
  1860. }
  1861. parse = ast_strdupa(data);
  1862. AST_STANDARD_APP_ARGS(args, parse);
  1863. if (!ast_strlen_zero(args.options) &&
  1864. ast_app_parse_options(fax_exec_options, &opts, NULL, args.options)) {
  1865. ast_atomic_fetchadd_int(&faxregistry.fax_failures, 1);
  1866. ast_string_field_set(details, error, "INVALID_ARGUMENTS");
  1867. ast_string_field_set(details, resultstr, "invalid arguments");
  1868. set_channel_variables(chan, details);
  1869. ao2_ref(details, -1);
  1870. return -1;
  1871. }
  1872. if (ast_strlen_zero(args.filenames)) {
  1873. ast_atomic_fetchadd_int(&faxregistry.fax_failures, 1);
  1874. ast_string_field_set(details, error, "INVALID_ARGUMENTS");
  1875. ast_string_field_set(details, resultstr, "invalid arguments");
  1876. set_channel_variables(chan, details);
  1877. ast_log(LOG_WARNING, "%s requires an argument (filename[&filename[&filename]],options])\n", app_sendfax);
  1878. ao2_ref(details, -1);
  1879. return -1;
  1880. }
  1881. /* check for unsupported FAX application options */
  1882. if (ast_test_flag(&opts, OPT_CALLERMODE) || ast_test_flag(&opts, OPT_CALLEDMODE)) {
  1883. ast_atomic_fetchadd_int(&faxregistry.fax_failures, 1);
  1884. ast_string_field_set(details, error, "INVALID_ARGUMENTS");
  1885. ast_string_field_set(details, resultstr, "invalid arguments");
  1886. set_channel_variables(chan, details);
  1887. ast_log(LOG_WARNING, "%s does not support polling\n", app_sendfax);
  1888. ao2_ref(details, -1);
  1889. return -1;
  1890. }
  1891. file_count = 0;
  1892. filenames = args.filenames;
  1893. while ((c = strsep(&filenames, "&"))) {
  1894. if (access(c, (F_OK | R_OK)) < 0) {
  1895. ast_atomic_fetchadd_int(&faxregistry.fax_failures, 1);
  1896. ast_string_field_set(details, error, "FILE_ERROR");
  1897. ast_string_field_set(details, resultstr, "error reading file");
  1898. set_channel_variables(chan, details);
  1899. ast_log(LOG_ERROR, "access failure. Verify '%s' exists and check permissions.\n", args.filenames);
  1900. ao2_ref(details, -1);
  1901. return -1;
  1902. }
  1903. if (!(doc = ast_calloc(1, sizeof(*doc) + strlen(c) + 1))) {
  1904. ast_atomic_fetchadd_int(&faxregistry.fax_failures, 1);
  1905. ast_string_field_set(details, error, "MEMORY_ERROR");
  1906. ast_string_field_set(details, resultstr, "error allocating memory");
  1907. set_channel_variables(chan, details);
  1908. ast_log(LOG_ERROR, "System cannot provide memory for session requirements.\n");
  1909. ao2_ref(details, -1);
  1910. return -1;
  1911. }
  1912. strcpy(doc->filename, c);
  1913. AST_LIST_INSERT_TAIL(&details->documents, doc, next);
  1914. file_count++;
  1915. }
  1916. if (file_count > 1) {
  1917. details->caps |= AST_FAX_TECH_MULTI_DOC;
  1918. }
  1919. ast_verb(3, "Channel '%s' sending FAX:\n", chan->name);
  1920. AST_LIST_TRAVERSE(&details->documents, doc, next) {
  1921. ast_verb(3, " %s\n", doc->filename);
  1922. }
  1923. details->caps = AST_FAX_TECH_SEND;
  1924. /* check for debug */
  1925. if (ast_test_flag(&opts, OPT_DEBUG) || global_fax_debug) {
  1926. details->option.debug = AST_FAX_OPTFLAG_TRUE;
  1927. }
  1928. /* check for request for status events */
  1929. if (ast_test_flag(&opts, OPT_STATUS)) {
  1930. details->option.statusevents = AST_FAX_OPTFLAG_TRUE;
  1931. }
  1932. if ((ast_channel_get_t38_state(chan) == T38_STATE_UNAVAILABLE) ||
  1933. ast_test_flag(&opts, OPT_ALLOWAUDIO)) {
  1934. details->option.allow_audio = AST_FAX_OPTFLAG_TRUE;
  1935. }
  1936. if (ast_test_flag(&opts, OPT_REQUEST_T38)) {
  1937. details->option.request_t38 = AST_FAX_OPTFLAG_TRUE;
  1938. }
  1939. if (!(s = fax_session_reserve(details, &token))) {
  1940. ast_atomic_fetchadd_int(&faxregistry.fax_failures, 1);
  1941. ast_string_field_set(details, resultstr, "error reserving fax session");
  1942. set_channel_variables(chan, details);
  1943. ast_log(LOG_ERROR, "Unable to reserve FAX session.\n");
  1944. ao2_ref(details, -1);
  1945. return -1;
  1946. }
  1947. /* make sure the channel is up */
  1948. if (chan->_state != AST_STATE_UP) {
  1949. if (ast_answer(chan)) {
  1950. ast_atomic_fetchadd_int(&faxregistry.fax_failures, 1);
  1951. ast_string_field_set(details, resultstr, "error answering channel");
  1952. set_channel_variables(chan, details);
  1953. ast_log(LOG_WARNING, "Channel '%s' failed answer attempt.\n", chan->name);
  1954. fax_session_release(s, token);
  1955. ao2_ref(s, -1);
  1956. ao2_ref(details, -1);
  1957. return -1;
  1958. }
  1959. }
  1960. if (set_fax_t38_caps(chan, details)) {
  1961. ast_atomic_fetchadd_int(&faxregistry.fax_failures, 1);
  1962. ast_string_field_set(details, error, "T38_NEG_ERROR");
  1963. ast_string_field_set(details, resultstr, "error negotiating T.38");
  1964. set_channel_variables(chan, details);
  1965. fax_session_release(s, token);
  1966. ao2_ref(s, -1);
  1967. ao2_ref(details, -1);
  1968. return -1;
  1969. }
  1970. if (details->caps & AST_FAX_TECH_T38) {
  1971. if (sendfax_t38_init(chan, details)) {
  1972. ast_atomic_fetchadd_int(&faxregistry.fax_failures, 1);
  1973. ast_string_field_set(details, error, "T38_NEG_ERROR");
  1974. ast_string_field_set(details, resultstr, "error negotiating T.38");
  1975. set_channel_variables(chan, details);
  1976. fax_session_release(s, token);
  1977. ao2_ref(s, -1);
  1978. ao2_ref(details, -1);
  1979. ast_log(LOG_ERROR, "error initializing channel '%s' in T.38 mode\n", chan->name);
  1980. return -1;
  1981. }
  1982. } else {
  1983. details->option.send_cng = 1;
  1984. }
  1985. if ((channel_alive = generic_fax_exec(chan, details, s, token)) < 0) {
  1986. ast_atomic_fetchadd_int(&faxregistry.fax_failures, 1);
  1987. }
  1988. if (ast_channel_get_t38_state(chan) == T38_STATE_NEGOTIATED) {
  1989. if (disable_t38(chan)) {
  1990. ast_debug(1, "error disabling T.38 mode on %s\n", chan->name);
  1991. }
  1992. }
  1993. if (!(filenames = generate_filenames_string(details, "FileName: ", "\r\n"))) {
  1994. ast_log(LOG_ERROR, "Error generating SendFAX manager event\n");
  1995. ao2_ref(s, -1);
  1996. ao2_ref(details, -1);
  1997. return (!channel_alive) ? -1 : 0;
  1998. }
  1999. /* send out the AMI completion event */
  2000. ast_channel_lock(chan);
  2001. get_manager_event_info(chan, &info);
  2002. manager_event(EVENT_FLAG_CALL,
  2003. "SendFAX",
  2004. "Channel: %s\r\n"
  2005. "Context: %s\r\n"
  2006. "Exten: %s\r\n"
  2007. "CallerID: %s\r\n"
  2008. "RemoteStationID: %s\r\n"
  2009. "LocalStationID: %s\r\n"
  2010. "PagesTransferred: %s\r\n"
  2011. "Resolution: %s\r\n"
  2012. "TransferRate: %s\r\n"
  2013. "%s\r\n",
  2014. chan->name,
  2015. info.context,
  2016. info.exten,
  2017. info.cid,
  2018. S_OR(pbx_builtin_getvar_helper(chan, "REMOTESTATIONID"), ""),
  2019. S_OR(pbx_builtin_getvar_helper(chan, "LOCALSTATIONID"), ""),
  2020. S_OR(pbx_builtin_getvar_helper(chan, "FAXPAGES"), ""),
  2021. S_OR(pbx_builtin_getvar_helper(chan, "FAXRESOLUTION"), ""),
  2022. S_OR(pbx_builtin_getvar_helper(chan, "FAXBITRATE"), ""),
  2023. filenames);
  2024. ast_channel_unlock(chan);
  2025. ast_free(filenames);
  2026. ao2_ref(s, -1);
  2027. ao2_ref(details, -1);
  2028. /* If the channel hungup return -1; otherwise, return 0 to continue in the dialplan */
  2029. return (!channel_alive) ? -1 : 0;
  2030. }
  2031. /*! \brief hash callback for ao2 */
  2032. static int session_hash_cb(const void *obj, const int flags)
  2033. {
  2034. const struct ast_fax_session *s = obj;
  2035. return s->id;
  2036. }
  2037. /*! \brief compare callback for ao2 */
  2038. static int session_cmp_cb(void *obj, void *arg, int flags)
  2039. {
  2040. struct ast_fax_session *lhs = obj, *rhs = arg;
  2041. return (lhs->id == rhs->id) ? CMP_MATCH | CMP_STOP : 0;
  2042. }
  2043. /*! \brief fax session tab completion */
  2044. static char *fax_session_tab_complete(struct ast_cli_args *a)
  2045. {
  2046. int tklen;
  2047. int wordnum = 0;
  2048. char *name = NULL;
  2049. struct ao2_iterator i;
  2050. struct ast_fax_session *s;
  2051. char tbuf[5];
  2052. if (a->pos != 3) {
  2053. return NULL;
  2054. }
  2055. tklen = strlen(a->word);
  2056. i = ao2_iterator_init(faxregistry.container, 0);
  2057. while ((s = ao2_iterator_next(&i))) {
  2058. snprintf(tbuf, sizeof(tbuf), "%d", s->id);
  2059. if (!strncasecmp(a->word, tbuf, tklen) && ++wordnum > a->n) {
  2060. name = ast_strdup(tbuf);
  2061. ao2_ref(s, -1);
  2062. break;
  2063. }
  2064. ao2_ref(s, -1);
  2065. }
  2066. ao2_iterator_destroy(&i);
  2067. return name;
  2068. }
  2069. static char *cli_fax_show_version(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
  2070. {
  2071. struct fax_module *fax;
  2072. switch(cmd) {
  2073. case CLI_INIT:
  2074. e->command = "fax show version";
  2075. e->usage =
  2076. "Usage: fax show version\n"
  2077. " Show versions of FAX For Asterisk components.\n";
  2078. return NULL;
  2079. case CLI_GENERATE:
  2080. return NULL;
  2081. }
  2082. if (a->argc != 3) {
  2083. return CLI_SHOWUSAGE;
  2084. }
  2085. ast_cli(a->fd, "FAX For Asterisk Components:\n");
  2086. ast_cli(a->fd, "\tApplications: %s\n", ast_get_version());
  2087. AST_RWLIST_RDLOCK(&faxmodules);
  2088. AST_RWLIST_TRAVERSE(&faxmodules, fax, list) {
  2089. ast_cli(a->fd, "\t%s: %s\n", fax->tech->description, fax->tech->version);
  2090. }
  2091. AST_RWLIST_UNLOCK(&faxmodules);
  2092. ast_cli(a->fd, "\n");
  2093. return CLI_SUCCESS;
  2094. }
  2095. /*! \brief enable FAX debugging */
  2096. static char *cli_fax_set_debug(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
  2097. {
  2098. int flag;
  2099. const char *what;
  2100. switch (cmd) {
  2101. case CLI_INIT:
  2102. e->command = "fax set debug {on|off}";
  2103. e->usage =
  2104. "Usage: fax set debug { on | off }\n"
  2105. " Enable/Disable FAX debugging on new FAX sessions. The basic FAX debugging will result in\n"
  2106. " additional events sent to manager sessions with 'call' class permissions. When\n"
  2107. " verbosity is greater than '5' events will be displayed to the console and audio versus\n"
  2108. " energy analysis will be performed and displayed to the console.\n";
  2109. return NULL;
  2110. case CLI_GENERATE:
  2111. return NULL;
  2112. }
  2113. what = a->argv[e->args-1]; /* guaranteed to exist */
  2114. if (!strcasecmp(what, "on")) {
  2115. flag = 1;
  2116. } else if (!strcasecmp(what, "off")) {
  2117. flag = 0;
  2118. } else {
  2119. return CLI_SHOWUSAGE;
  2120. }
  2121. global_fax_debug = flag;
  2122. ast_cli(a->fd, "\n\nFAX Debug %s\n\n", (flag) ? "Enabled" : "Disabled");
  2123. return CLI_SUCCESS;
  2124. }
  2125. /*! \brief display registered FAX capabilities */
  2126. static char *cli_fax_show_capabilities(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
  2127. {
  2128. struct fax_module *fax;
  2129. unsigned int num_modules = 0;
  2130. switch (cmd) {
  2131. case CLI_INIT:
  2132. e->command = "fax show capabilities";
  2133. e->usage =
  2134. "Usage: fax show capabilities\n"
  2135. " Shows the capabilities of the registered FAX technology modules\n";
  2136. return NULL;
  2137. case CLI_GENERATE:
  2138. return NULL;
  2139. }
  2140. ast_cli(a->fd, "\n\nRegistered FAX Technology Modules:\n\n");
  2141. AST_RWLIST_RDLOCK(&faxmodules);
  2142. AST_RWLIST_TRAVERSE(&faxmodules, fax, list) {
  2143. ast_cli(a->fd, "%-15s : %s\n%-15s : %s\n%-15s : ", "Type", fax->tech->type, "Description", fax->tech->description, "Capabilities");
  2144. fax->tech->cli_show_capabilities(a->fd);
  2145. num_modules++;
  2146. }
  2147. AST_RWLIST_UNLOCK(&faxmodules);
  2148. ast_cli(a->fd, "%d registered modules\n\n", num_modules);
  2149. return CLI_SUCCESS;
  2150. }
  2151. /*! \brief display global defaults and settings */
  2152. static char *cli_fax_show_settings(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
  2153. {
  2154. struct fax_module *fax;
  2155. char modems[128] = "";
  2156. struct fax_options options;
  2157. switch (cmd) {
  2158. case CLI_INIT:
  2159. e->command = "fax show settings";
  2160. e->usage =
  2161. "Usage: fax show settings\n"
  2162. " Show the global settings and defaults of both the FAX core and technology modules\n";
  2163. return NULL;
  2164. case CLI_GENERATE:
  2165. return NULL;
  2166. }
  2167. get_general_options(&options);
  2168. ast_cli(a->fd, "FAX For Asterisk Settings:\n");
  2169. ast_cli(a->fd, "\tECM: %s\n", options.ecm ? "Enabled" : "Disabled");
  2170. ast_cli(a->fd, "\tStatus Events: %s\n", options.statusevents ? "On" : "Off");
  2171. ast_cli(a->fd, "\tMinimum Bit Rate: %d\n", options.minrate);
  2172. ast_cli(a->fd, "\tMaximum Bit Rate: %d\n", options.maxrate);
  2173. ast_fax_modem_to_str(options.modems, modems, sizeof(modems));
  2174. ast_cli(a->fd, "\tModem Modulations Allowed: %s\n", modems);
  2175. ast_cli(a->fd, "\n\nFAX Technology Modules:\n\n");
  2176. AST_RWLIST_RDLOCK(&faxmodules);
  2177. AST_RWLIST_TRAVERSE(&faxmodules, fax, list) {
  2178. ast_cli(a->fd, "%s (%s) Settings:\n", fax->tech->type, fax->tech->description);
  2179. fax->tech->cli_show_settings(a->fd);
  2180. }
  2181. AST_RWLIST_UNLOCK(&faxmodules);
  2182. return CLI_SUCCESS;
  2183. }
  2184. /*! \brief display details of a specified fax session */
  2185. static char *cli_fax_show_session(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
  2186. {
  2187. struct ast_fax_session *s, tmp;
  2188. switch (cmd) {
  2189. case CLI_INIT:
  2190. e->command = "fax show session";
  2191. e->usage =
  2192. "Usage: fax show session <session number>\n"
  2193. " Shows status of the named FAX session\n";
  2194. return NULL;
  2195. case CLI_GENERATE:
  2196. return fax_session_tab_complete(a);
  2197. }
  2198. if (a->argc != 4) {
  2199. return CLI_SHOWUSAGE;
  2200. }
  2201. if (sscanf(a->argv[3], "%d", &tmp.id) != 1) {
  2202. ast_log(LOG_ERROR, "invalid session id: '%s'\n", a->argv[3]);
  2203. return RESULT_SUCCESS;
  2204. }
  2205. ast_cli(a->fd, "\nFAX Session Details:\n--------------------\n\n");
  2206. s = ao2_find(faxregistry.container, &tmp, OBJ_POINTER);
  2207. if (s) {
  2208. s->tech->cli_show_session(s, a->fd);
  2209. ao2_ref(s, -1);
  2210. }
  2211. ast_cli(a->fd, "\n\n");
  2212. return CLI_SUCCESS;
  2213. }
  2214. /*! \brief display fax stats */
  2215. static char *cli_fax_show_stats(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
  2216. {
  2217. struct fax_module *fax;
  2218. switch (cmd) {
  2219. case CLI_INIT:
  2220. e->command = "fax show stats";
  2221. e->usage =
  2222. "Usage: fax show stats\n"
  2223. " Shows a statistical summary of FAX transmissions\n";
  2224. return NULL;
  2225. case CLI_GENERATE:
  2226. return NULL;
  2227. }
  2228. ast_cli(a->fd, "\nFAX Statistics:\n---------------\n\n");
  2229. ast_cli(a->fd, "%-20.20s : %d\n", "Current Sessions", faxregistry.active_sessions);
  2230. ast_cli(a->fd, "%-20.20s : %d\n", "Reserved Sessions", faxregistry.reserved_sessions);
  2231. ast_cli(a->fd, "%-20.20s : %d\n", "Transmit Attempts", faxregistry.fax_tx_attempts);
  2232. ast_cli(a->fd, "%-20.20s : %d\n", "Receive Attempts", faxregistry.fax_rx_attempts);
  2233. ast_cli(a->fd, "%-20.20s : %d\n", "Completed FAXes", faxregistry.fax_complete);
  2234. ast_cli(a->fd, "%-20.20s : %d\n", "Failed FAXes", faxregistry.fax_failures);
  2235. AST_RWLIST_RDLOCK(&faxmodules);
  2236. AST_RWLIST_TRAVERSE(&faxmodules, fax, list) {
  2237. fax->tech->cli_show_stats(a->fd);
  2238. }
  2239. AST_RWLIST_UNLOCK(&faxmodules);
  2240. ast_cli(a->fd, "\n\n");
  2241. return CLI_SUCCESS;
  2242. }
  2243. /*! \brief display fax sessions */
  2244. static char *cli_fax_show_sessions(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
  2245. {
  2246. struct ast_fax_session *s;
  2247. struct ao2_iterator i;
  2248. int session_count;
  2249. char *filenames;
  2250. switch (cmd) {
  2251. case CLI_INIT:
  2252. e->command = "fax show sessions";
  2253. e->usage =
  2254. "Usage: fax show sessions\n"
  2255. " Shows the current FAX sessions\n";
  2256. return NULL;
  2257. case CLI_GENERATE:
  2258. return NULL;
  2259. }
  2260. ast_cli(a->fd, "\nCurrent FAX Sessions:\n\n");
  2261. ast_cli(a->fd, "%-20.20s %-10.10s %-10.10s %-5.5s %-10.10s %-15.15s %-30.30s\n",
  2262. "Channel", "Tech", "FAXID", "Type", "Operation", "State", "File(s)");
  2263. i = ao2_iterator_init(faxregistry.container, 0);
  2264. while ((s = ao2_iterator_next(&i))) {
  2265. ao2_lock(s);
  2266. if (!(filenames = generate_filenames_string(s->details, "", ", "))) {
  2267. ast_log(LOG_ERROR, "error printing filenames for 'fax show sessions' command");
  2268. ao2_unlock(s);
  2269. ao2_ref(s, -1);
  2270. ao2_iterator_destroy(&i);
  2271. return CLI_FAILURE;
  2272. }
  2273. ast_cli(a->fd, "%-20.20s %-10.10s %-10d %-5.5s %-10.10s %-15.15s %-30s\n",
  2274. s->channame, s->tech->type, s->id,
  2275. (s->details->caps & AST_FAX_TECH_AUDIO) ? "G.711" : "T.38",
  2276. (s->details->caps & AST_FAX_TECH_SEND) ? "send" : "receive",
  2277. ast_fax_state_to_str(s->state), filenames);
  2278. ast_free(filenames);
  2279. ao2_unlock(s);
  2280. ao2_ref(s, -1);
  2281. }
  2282. ao2_iterator_destroy(&i);
  2283. session_count = ao2_container_count(faxregistry.container);
  2284. ast_cli(a->fd, "\n%d FAX sessions\n\n", session_count);
  2285. return CLI_SUCCESS;
  2286. }
  2287. static struct ast_cli_entry fax_cli[] = {
  2288. AST_CLI_DEFINE(cli_fax_show_version, "Show versions of FAX For Asterisk components"),
  2289. AST_CLI_DEFINE(cli_fax_set_debug, "Enable/Disable FAX debugging on new FAX sessions"),
  2290. AST_CLI_DEFINE(cli_fax_show_capabilities, "Show the capabilities of the registered FAX technology modules"),
  2291. AST_CLI_DEFINE(cli_fax_show_settings, "Show the global settings and defaults of both the FAX core and technology modules"),
  2292. AST_CLI_DEFINE(cli_fax_show_session, "Show the status of the named FAX sessions"),
  2293. AST_CLI_DEFINE(cli_fax_show_sessions, "Show the current FAX sessions"),
  2294. AST_CLI_DEFINE(cli_fax_show_stats, "Summarize FAX session history"),
  2295. };
  2296. static void set_general_options(const struct fax_options *options)
  2297. {
  2298. ast_rwlock_wrlock(&options_lock);
  2299. general_options = *options;
  2300. ast_rwlock_unlock(&options_lock);
  2301. }
  2302. static void get_general_options(struct fax_options *options)
  2303. {
  2304. ast_rwlock_rdlock(&options_lock);
  2305. *options = general_options;
  2306. ast_rwlock_unlock(&options_lock);
  2307. }
  2308. /*! \brief configure res_fax */
  2309. static int set_config(int reload)
  2310. {
  2311. struct ast_config *cfg;
  2312. struct ast_variable *v;
  2313. struct ast_flags config_flags = { reload ? CONFIG_FLAG_FILEUNCHANGED : 0 };
  2314. char modems[128] = "";
  2315. struct fax_options options;
  2316. int res = 0;
  2317. options = default_options;
  2318. /* When we're not reloading, we have to be certain to set the general options
  2319. * to the defaults in case config loading goes wrong at some point. On a reload,
  2320. * the general options need to stay the same as what they were prior to the
  2321. * reload rather than being reset to the defaults.
  2322. */
  2323. if (!reload) {
  2324. set_general_options(&options);
  2325. }
  2326. /* read configuration */
  2327. if (!(cfg = ast_config_load2(config, "res_fax", config_flags))) {
  2328. ast_log(LOG_NOTICE, "Configuration file '%s' not found, %s options.\n",
  2329. config, reload ? "not changing" : "using default");
  2330. return 0;
  2331. }
  2332. if (cfg == CONFIG_STATUS_FILEINVALID) {
  2333. ast_log(LOG_NOTICE, "Configuration file '%s' is invalid, %s options.\n",
  2334. config, reload ? "not changing" : "using default");
  2335. return 0;
  2336. }
  2337. if (cfg == CONFIG_STATUS_FILEUNCHANGED) {
  2338. return 0;
  2339. }
  2340. if (reload) {
  2341. options = default_options;
  2342. }
  2343. /* create configuration */
  2344. for (v = ast_variable_browse(cfg, "general"); v; v = v->next) {
  2345. int rate;
  2346. if (!strcasecmp(v->name, "minrate")) {
  2347. ast_debug(3, "reading minrate '%s' from configuration file\n", v->value);
  2348. if ((rate = fax_rate_str_to_int(v->value)) == 0) {
  2349. res = -1;
  2350. goto end;
  2351. }
  2352. options.minrate = rate;
  2353. } else if (!strcasecmp(v->name, "maxrate")) {
  2354. ast_debug(3, "reading maxrate '%s' from configuration file\n", v->value);
  2355. if ((rate = fax_rate_str_to_int(v->value)) == 0) {
  2356. res = -1;
  2357. goto end;
  2358. }
  2359. options.maxrate = rate;
  2360. } else if (!strcasecmp(v->name, "statusevents")) {
  2361. ast_debug(3, "reading statusevents '%s' from configuration file\n", v->value);
  2362. options.statusevents = ast_true(v->value);
  2363. } else if (!strcasecmp(v->name, "ecm")) {
  2364. ast_debug(3, "reading ecm '%s' from configuration file\n", v->value);
  2365. options.ecm = ast_true(v->value);
  2366. } else if ((!strcasecmp(v->name, "modem")) || (!strcasecmp(v->name, "modems"))) {
  2367. options.modems = 0;
  2368. update_modem_bits(&options.modems, v->value);
  2369. }
  2370. }
  2371. if (options.maxrate < options.minrate) {
  2372. ast_log(LOG_ERROR, "maxrate %d is less than minrate %d\n", options.maxrate, options.minrate);
  2373. res = -1;
  2374. goto end;
  2375. }
  2376. if (check_modem_rate(options.modems, options.minrate)) {
  2377. ast_fax_modem_to_str(options.modems, modems, sizeof(modems));
  2378. ast_log(LOG_ERROR, "'modems' setting '%s' is incompatible with 'minrate' setting %d\n", modems, options.minrate);
  2379. res = -1;
  2380. goto end;
  2381. }
  2382. if (check_modem_rate(options.modems, options.maxrate)) {
  2383. ast_fax_modem_to_str(options.modems, modems, sizeof(modems));
  2384. ast_log(LOG_ERROR, "'modems' setting '%s' is incompatible with 'maxrate' setting %d\n", modems, options.maxrate);
  2385. res = -1;
  2386. goto end;
  2387. }
  2388. set_general_options(&options);
  2389. end:
  2390. ast_config_destroy(cfg);
  2391. return res;
  2392. }
  2393. /*! \brief FAXOPT read function returns the contents of a FAX option */
  2394. static int acf_faxopt_read(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t len)
  2395. {
  2396. struct ast_fax_session_details *details = find_details(chan);
  2397. int res = 0;
  2398. char *filenames;
  2399. if (!details) {
  2400. ast_log(LOG_ERROR, "channel '%s' can't read FAXOPT(%s) because it has never been written.\n", chan->name, data);
  2401. return -1;
  2402. }
  2403. if (!strcasecmp(data, "ecm")) {
  2404. ast_copy_string(buf, details->option.ecm ? "yes" : "no", len);
  2405. } else if (!strcasecmp(data, "error")) {
  2406. ast_copy_string(buf, details->error, len);
  2407. } else if (!strcasecmp(data, "filename")) {
  2408. if (AST_LIST_EMPTY(&details->documents)) {
  2409. ast_log(LOG_ERROR, "channel '%s' can't read FAXOPT(%s) because it has never been written.\n", chan->name, data);
  2410. res = -1;
  2411. } else {
  2412. ast_copy_string(buf, AST_LIST_FIRST(&details->documents)->filename, len);
  2413. }
  2414. } else if (!strcasecmp(data, "filenames")) {
  2415. if (AST_LIST_EMPTY(&details->documents)) {
  2416. ast_log(LOG_ERROR, "channel '%s' can't read FAXOPT(%s) because it has never been written.\n", chan->name, data);
  2417. res = -1;
  2418. } else if ((filenames = generate_filenames_string(details, "", ","))) {
  2419. ast_copy_string(buf, filenames, len);
  2420. ast_free(filenames);
  2421. } else {
  2422. ast_log(LOG_ERROR, "channel '%s' can't read FAXOPT(%s), there was an error generating the filenames list.\n", chan->name, data);
  2423. res = -1;
  2424. }
  2425. } else if (!strcasecmp(data, "headerinfo")) {
  2426. ast_copy_string(buf, details->headerinfo, len);
  2427. } else if (!strcasecmp(data, "localstationid")) {
  2428. ast_copy_string(buf, details->localstationid, len);
  2429. } else if (!strcasecmp(data, "maxrate")) {
  2430. snprintf(buf, len, "%d", details->maxrate);
  2431. } else if (!strcasecmp(data, "minrate")) {
  2432. snprintf(buf, len, "%d", details->minrate);
  2433. } else if (!strcasecmp(data, "pages")) {
  2434. snprintf(buf, len, "%d", details->pages_transferred);
  2435. } else if (!strcasecmp(data, "rate")) {
  2436. ast_copy_string(buf, details->transfer_rate, len);
  2437. } else if (!strcasecmp(data, "remotestationid")) {
  2438. ast_copy_string(buf, details->remotestationid, len);
  2439. } else if (!strcasecmp(data, "resolution")) {
  2440. ast_copy_string(buf, details->resolution, len);
  2441. } else if (!strcasecmp(data, "sessionid")) {
  2442. snprintf(buf, len, "%d", details->id);
  2443. } else if (!strcasecmp(data, "status")) {
  2444. ast_copy_string(buf, details->result, len);
  2445. } else if (!strcasecmp(data, "statusstr")) {
  2446. ast_copy_string(buf, details->resultstr, len);
  2447. } else if ((!strcasecmp(data, "modem")) || (!strcasecmp(data, "modems"))) {
  2448. ast_fax_modem_to_str(details->modems, buf, len);
  2449. } else {
  2450. ast_log(LOG_WARNING, "channel '%s' can't read FAXOPT(%s) because it is unhandled!\n", chan->name, data);
  2451. res = -1;
  2452. }
  2453. ao2_ref(details, -1);
  2454. return res;
  2455. }
  2456. /*! \brief FAXOPT write function modifies the contents of a FAX option */
  2457. static int acf_faxopt_write(struct ast_channel *chan, const char *cmd, char *data, const char *value)
  2458. {
  2459. int res = 0;
  2460. struct ast_fax_session_details *details;
  2461. if (!(details = find_or_create_details(chan))) {
  2462. ast_log(LOG_WARNING, "channel '%s' can't set FAXOPT(%s) to '%s' because it failed to create a datastore.\n", chan->name, data, value);
  2463. return -1;
  2464. }
  2465. ast_debug(3, "channel '%s' setting FAXOPT(%s) to '%s'\n", chan->name, data, value);
  2466. if (!strcasecmp(data, "ecm")) {
  2467. const char *val = ast_skip_blanks(value);
  2468. if (ast_true(val)) {
  2469. details->option.ecm = AST_FAX_OPTFLAG_TRUE;
  2470. } else if (ast_false(val)) {
  2471. details->option.ecm = AST_FAX_OPTFLAG_FALSE;
  2472. } else {
  2473. ast_log(LOG_WARNING, "Unsupported value '%s' passed to FAXOPT(ecm).\n", value);
  2474. }
  2475. } else if (!strcasecmp(data, "headerinfo")) {
  2476. ast_string_field_set(details, headerinfo, value);
  2477. } else if (!strcasecmp(data, "localstationid")) {
  2478. ast_string_field_set(details, localstationid, value);
  2479. } else if (!strcasecmp(data, "maxrate")) {
  2480. details->maxrate = fax_rate_str_to_int(value);
  2481. if (!details->maxrate) {
  2482. details->maxrate = ast_fax_maxrate();
  2483. }
  2484. } else if (!strcasecmp(data, "minrate")) {
  2485. details->minrate = fax_rate_str_to_int(value);
  2486. if (!details->minrate) {
  2487. details->minrate = ast_fax_minrate();
  2488. }
  2489. } else if ((!strcasecmp(data, "modem")) || (!strcasecmp(data, "modems"))) {
  2490. update_modem_bits(&details->modems, value);
  2491. } else {
  2492. ast_log(LOG_WARNING, "channel '%s' set FAXOPT(%s) to '%s' is unhandled!\n", chan->name, data, value);
  2493. res = -1;
  2494. }
  2495. ao2_ref(details, -1);
  2496. return res;
  2497. }
  2498. /*! \brief FAXOPT dialplan function */
  2499. struct ast_custom_function acf_faxopt = {
  2500. .name = "FAXOPT",
  2501. .read = acf_faxopt_read,
  2502. .write = acf_faxopt_write,
  2503. };
  2504. /*! \brief unload res_fax */
  2505. static int unload_module(void)
  2506. {
  2507. ast_cli_unregister_multiple(fax_cli, ARRAY_LEN(fax_cli));
  2508. if (ast_custom_function_unregister(&acf_faxopt) < 0) {
  2509. ast_log(LOG_WARNING, "failed to unregister function '%s'\n", acf_faxopt.name);
  2510. }
  2511. if (ast_unregister_application(app_sendfax) < 0) {
  2512. ast_log(LOG_WARNING, "failed to unregister '%s'\n", app_sendfax);
  2513. }
  2514. if (ast_unregister_application(app_receivefax) < 0) {
  2515. ast_log(LOG_WARNING, "failed to unregister '%s'\n", app_receivefax);
  2516. }
  2517. if (fax_logger_level != -1) {
  2518. ast_logger_unregister_level("FAX");
  2519. }
  2520. ao2_ref(faxregistry.container, -1);
  2521. return 0;
  2522. }
  2523. /*! \brief load res_fax */
  2524. static int load_module(void)
  2525. {
  2526. int res;
  2527. /* initialize the registry */
  2528. faxregistry.active_sessions = 0;
  2529. faxregistry.reserved_sessions = 0;
  2530. if (!(faxregistry.container = ao2_container_alloc(FAX_MAXBUCKETS, session_hash_cb, session_cmp_cb))) {
  2531. return AST_MODULE_LOAD_DECLINE;
  2532. }
  2533. if (set_config(0) < 0) {
  2534. ast_log(LOG_ERROR, "failed to load configuration file '%s'\n", config);
  2535. ao2_ref(faxregistry.container, -1);
  2536. return AST_MODULE_LOAD_DECLINE;
  2537. }
  2538. /* register CLI operations and applications */
  2539. if (ast_register_application_xml(app_sendfax, sendfax_exec) < 0) {
  2540. ast_log(LOG_WARNING, "failed to register '%s'.\n", app_sendfax);
  2541. ao2_ref(faxregistry.container, -1);
  2542. return AST_MODULE_LOAD_DECLINE;
  2543. }
  2544. if (ast_register_application_xml(app_receivefax, receivefax_exec) < 0) {
  2545. ast_log(LOG_WARNING, "failed to register '%s'.\n", app_receivefax);
  2546. ast_unregister_application(app_sendfax);
  2547. ao2_ref(faxregistry.container, -1);
  2548. return AST_MODULE_LOAD_DECLINE;
  2549. }
  2550. ast_cli_register_multiple(fax_cli, ARRAY_LEN(fax_cli));
  2551. res = ast_custom_function_register(&acf_faxopt);
  2552. fax_logger_level = ast_logger_register_level("FAX");
  2553. return res;
  2554. }
  2555. static int reload_module(void)
  2556. {
  2557. set_config(1);
  2558. return 0;
  2559. }
  2560. AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_GLOBAL_SYMBOLS | AST_MODFLAG_LOAD_ORDER, "Generic FAX Applications",
  2561. .load = load_module,
  2562. .unload = unload_module,
  2563. .reload = reload_module,
  2564. .load_pri = AST_MODPRI_APP_DEPEND,
  2565. );