res_fax_spandsp.c 43 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269
  1. /*
  2. * Asterisk -- An open source telephony toolkit.
  3. *
  4. * Copyright (C) 2009-2010, Digium, Inc.
  5. *
  6. * Matthew Nicholson <mnicholson@digium.com>
  7. *
  8. * Initial T.38-gateway code
  9. * 2008, Daniel Ferenci <daniel.ferenci@nethemba.com>
  10. * Created by Nethemba s.r.o. http://www.nethemba.com
  11. * Sponsored by IPEX a.s. http://www.ipex.cz
  12. *
  13. * T.38-gateway integration into asterisk app_fax and rework
  14. * 2008, Gregory Hinton Nietsky <gregory@dnstelecom.co.za>
  15. * dns Telecom http://www.dnstelecom.co.za
  16. *
  17. * Modified to make T.38-gateway compatible with Asterisk 1.6.2
  18. * 2010, Anton Verevkin <mymail@verevkin.it>
  19. * ViaNetTV http://www.vianettv.com
  20. *
  21. * Modified to make T.38-gateway work
  22. * 2010, Klaus Darilion, IPCom GmbH, www.ipcom.at
  23. *
  24. * See http://www.asterisk.org for more information about
  25. * the Asterisk project. Please do not directly contact
  26. * any of the maintainers of this project for assistance;
  27. * the project provides a web site, mailing lists and IRC
  28. * channels for your use.
  29. *
  30. * This program is free software, distributed under the terms of
  31. * the GNU General Public License Version 2. See the LICENSE file
  32. * at the top of the source tree.
  33. */
  34. /*! \file
  35. *
  36. * \brief Spandsp T.38 and G.711 FAX Resource
  37. *
  38. * \author Matthew Nicholson <mnicholson@digium.com>
  39. * \author Gregory H. Nietsky <gregory@distrotech.co.za>
  40. *
  41. * This module registers the Spandsp FAX technology with the res_fax module.
  42. */
  43. /*** MODULEINFO
  44. <depend>spandsp</depend>
  45. <depend>res_fax</depend>
  46. <support_level>extended</support_level>
  47. ***/
  48. #include "asterisk.h"
  49. ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
  50. #define SPANDSP_EXPOSE_INTERNAL_STRUCTURES
  51. #include <spandsp.h>
  52. #include <spandsp/version.h>
  53. #include "asterisk/logger.h"
  54. #include "asterisk/module.h"
  55. #include "asterisk/strings.h"
  56. #include "asterisk/cli.h"
  57. #include "asterisk/utils.h"
  58. #include "asterisk/timing.h"
  59. #include "asterisk/astobj2.h"
  60. #include "asterisk/res_fax.h"
  61. #include "asterisk/channel.h"
  62. #include "asterisk/format_cache.h"
  63. #define SPANDSP_FAX_SAMPLES 160
  64. #define SPANDSP_FAX_TIMER_RATE 8000 / SPANDSP_FAX_SAMPLES /* 50 ticks per second, 20ms, 160 samples per second */
  65. #define SPANDSP_ENGAGE_UDPTL_NAT_RETRY 3
  66. static void *spandsp_fax_new(struct ast_fax_session *s, struct ast_fax_tech_token *token);
  67. static void spandsp_fax_destroy(struct ast_fax_session *s);
  68. static struct ast_frame *spandsp_fax_read(struct ast_fax_session *s);
  69. static int spandsp_fax_write(struct ast_fax_session *s, const struct ast_frame *f);
  70. static int spandsp_fax_start(struct ast_fax_session *s);
  71. static int spandsp_fax_cancel(struct ast_fax_session *s);
  72. static int spandsp_fax_switch_to_t38(struct ast_fax_session *s);
  73. static int spandsp_fax_gateway_start(struct ast_fax_session *s);
  74. static int spandsp_fax_gateway_process(struct ast_fax_session *s, const struct ast_frame *f);
  75. static void spandsp_fax_gateway_cleanup(struct ast_fax_session *s);
  76. static int spandsp_v21_detect(struct ast_fax_session *s, const struct ast_frame *f);
  77. static void spandsp_v21_cleanup(struct ast_fax_session *s);
  78. static void spandsp_v21_tone(void *data, int code, int level, int delay);
  79. static char *spandsp_fax_cli_show_capabilities(int fd);
  80. static char *spandsp_fax_cli_show_session(struct ast_fax_session *s, int fd);
  81. static void spandsp_manager_fax_session(struct mansession *s,
  82. const char *id_text, struct ast_fax_session *session);
  83. static char *spandsp_fax_cli_show_stats(int fd);
  84. static char *spandsp_fax_cli_show_settings(int fd);
  85. static struct ast_fax_tech spandsp_fax_tech = {
  86. .type = "Spandsp",
  87. .description = "Spandsp FAX Driver",
  88. #if SPANDSP_RELEASE_DATE >= 20090220
  89. /* spandsp 0.0.6 */
  90. .version = SPANDSP_RELEASE_DATETIME_STRING,
  91. #else
  92. /* spandsp 0.0.5
  93. * TODO: maybe we should determine the version better way
  94. */
  95. .version = "pre-20090220",
  96. #endif
  97. .caps = AST_FAX_TECH_AUDIO | AST_FAX_TECH_T38 | AST_FAX_TECH_SEND
  98. | AST_FAX_TECH_RECEIVE | AST_FAX_TECH_GATEWAY
  99. | AST_FAX_TECH_V21_DETECT,
  100. .new_session = spandsp_fax_new,
  101. .destroy_session = spandsp_fax_destroy,
  102. .read = spandsp_fax_read,
  103. .write = spandsp_fax_write,
  104. .start_session = spandsp_fax_start,
  105. .cancel_session = spandsp_fax_cancel,
  106. .switch_to_t38 = spandsp_fax_switch_to_t38,
  107. .cli_show_capabilities = spandsp_fax_cli_show_capabilities,
  108. .cli_show_session = spandsp_fax_cli_show_session,
  109. .manager_fax_session = spandsp_manager_fax_session,
  110. .cli_show_stats = spandsp_fax_cli_show_stats,
  111. .cli_show_settings = spandsp_fax_cli_show_settings,
  112. };
  113. struct spandsp_fax_stats {
  114. int success;
  115. int nofax;
  116. int neg_failed;
  117. int failed_to_train;
  118. int rx_protocol_error;
  119. int tx_protocol_error;
  120. int protocol_error;
  121. int retries_exceeded;
  122. int file_error;
  123. int mem_error;
  124. int call_dropped;
  125. int unknown_error;
  126. int switched;
  127. };
  128. static struct {
  129. ast_mutex_t lock;
  130. struct spandsp_fax_stats g711;
  131. struct spandsp_fax_stats t38;
  132. } spandsp_global_stats;
  133. struct spandsp_pvt {
  134. unsigned int ist38:1;
  135. unsigned int isdone:1;
  136. enum ast_t38_state ast_t38_state;
  137. fax_state_t fax_state;
  138. t38_terminal_state_t t38_state;
  139. t30_state_t *t30_state;
  140. t38_core_state_t *t38_core_state;
  141. struct spandsp_fax_stats *stats;
  142. struct spandsp_fax_gw_stats *t38stats;
  143. t38_gateway_state_t t38_gw_state;
  144. struct ast_timer *timer;
  145. AST_LIST_HEAD(frame_queue, ast_frame) read_frames;
  146. int v21_detected;
  147. modem_connect_tones_rx_state_t *tone_state;
  148. };
  149. static int spandsp_v21_new(struct spandsp_pvt *p);
  150. static void session_destroy(struct spandsp_pvt *p);
  151. static int t38_tx_packet_handler(t38_core_state_t *t38_core_state, void *data, const uint8_t *buf, int len, int count);
  152. static void t30_phase_e_handler(t30_state_t *t30_state, void *data, int completion_code);
  153. static void spandsp_log(int level, const char *msg);
  154. static int update_stats(struct spandsp_pvt *p, int completion_code);
  155. static int spandsp_modems(struct ast_fax_session_details *details);
  156. static void set_logging(logging_state_t *state, struct ast_fax_session_details *details);
  157. static void set_local_info(t30_state_t *t30_state, struct ast_fax_session_details *details);
  158. static void set_file(t30_state_t *t30_state, struct ast_fax_session_details *details);
  159. static void set_ecm(t30_state_t *t30_state, struct ast_fax_session_details *details);
  160. static void session_destroy(struct spandsp_pvt *p)
  161. {
  162. struct ast_frame *f;
  163. t30_state_t *t30_to_terminate;
  164. if (p->t30_state) {
  165. t30_to_terminate = p->t30_state;
  166. } else if (p->ist38) {
  167. #if SPANDSP_RELEASE_DATE >= 20080725
  168. t30_to_terminate = &p->t38_state.t30;
  169. #else
  170. t30_to_terminate = &p->t38_state.t30_state;
  171. #endif
  172. } else {
  173. #if SPANDSP_RELEASE_DATE >= 20080725
  174. t30_to_terminate = &p->fax_state.t30;
  175. #else
  176. t30_to_terminate = &p->fax_state.t30_state;
  177. #endif
  178. }
  179. t30_terminate(t30_to_terminate);
  180. p->isdone = 1;
  181. ast_timer_close(p->timer);
  182. p->timer = NULL;
  183. fax_release(&p->fax_state);
  184. t38_terminal_release(&p->t38_state);
  185. while ((f = AST_LIST_REMOVE_HEAD(&p->read_frames, frame_list))) {
  186. ast_frfree(f);
  187. }
  188. }
  189. /*! \brief
  190. *
  191. */
  192. static int t38_tx_packet_handler(t38_core_state_t *t38_core_state, void *data, const uint8_t *buf, int len, int count)
  193. {
  194. int res = -1;
  195. struct ast_fax_session *s = data;
  196. struct spandsp_pvt *p = s->tech_pvt;
  197. struct ast_frame fax_frame = {
  198. .frametype = AST_FRAME_MODEM,
  199. .subclass.integer = AST_MODEM_T38,
  200. .src = "res_fax_spandsp_t38",
  201. };
  202. struct ast_frame *f = &fax_frame;
  203. /* TODO: Asterisk does not provide means of resending the same packet multiple
  204. times so count is ignored at the moment */
  205. AST_FRAME_SET_BUFFER(f, buf, 0, len);
  206. if (!(f = ast_frisolate(f))) {
  207. return res;
  208. }
  209. if (s->details->caps & AST_FAX_TECH_GATEWAY) {
  210. ast_set_flag(f, AST_FAX_FRFLAG_GATEWAY);
  211. if (p->ast_t38_state == T38_STATE_NEGOTIATED) {
  212. res = ast_write(s->chan, f);
  213. } else {
  214. res = ast_queue_frame(s->chan, f);
  215. }
  216. ast_frfree(f);
  217. } else {
  218. /* no need to lock, this all runs in the same thread */
  219. AST_LIST_INSERT_TAIL(&p->read_frames, f, frame_list);
  220. res = 0;
  221. }
  222. return res;
  223. }
  224. static int update_stats(struct spandsp_pvt *p, int completion_code)
  225. {
  226. switch (completion_code) {
  227. case T30_ERR_OK:
  228. ast_atomic_fetchadd_int(&p->stats->success, 1);
  229. break;
  230. /* Link problems */
  231. case T30_ERR_CEDTONE: /*! The CED tone exceeded 5s */
  232. case T30_ERR_T0_EXPIRED: /*! Timed out waiting for initial communication */
  233. case T30_ERR_T1_EXPIRED: /*! Timed out waiting for the first message */
  234. case T30_ERR_T3_EXPIRED: /*! Timed out waiting for procedural interrupt */
  235. case T30_ERR_HDLC_CARRIER: /*! The HDLC carrier did not stop in a timely manner */
  236. case T30_ERR_CANNOT_TRAIN: /*! Failed to train with any of the compatible modems */
  237. ast_atomic_fetchadd_int(&p->stats->failed_to_train, 1);
  238. break;
  239. case T30_ERR_OPER_INT_FAIL: /*! Operator intervention failed */
  240. case T30_ERR_INCOMPATIBLE: /*! Far end is not compatible */
  241. case T30_ERR_RX_INCAPABLE: /*! Far end is not able to receive */
  242. case T30_ERR_TX_INCAPABLE: /*! Far end is not able to transmit */
  243. case T30_ERR_NORESSUPPORT: /*! Far end cannot receive at the resolution of the image */
  244. case T30_ERR_NOSIZESUPPORT: /*! Far end cannot receive at the size of image */
  245. ast_atomic_fetchadd_int(&p->stats->neg_failed, 1);
  246. break;
  247. case T30_ERR_UNEXPECTED: /*! Unexpected message received */
  248. ast_atomic_fetchadd_int(&p->stats->protocol_error, 1);
  249. break;
  250. /* Phase E status values returned to a transmitter */
  251. case T30_ERR_TX_BADDCS: /*! Received bad response to DCS or training */
  252. case T30_ERR_TX_BADPG: /*! Received a DCN from remote after sending a page */
  253. case T30_ERR_TX_ECMPHD: /*! Invalid ECM response received from receiver */
  254. case T30_ERR_TX_GOTDCN: /*! Received a DCN while waiting for a DIS */
  255. case T30_ERR_TX_INVALRSP: /*! Invalid response after sending a page */
  256. case T30_ERR_TX_NODIS: /*! Received other than DIS while waiting for DIS */
  257. case T30_ERR_TX_PHBDEAD: /*! Received no response to DCS, training or TCF */
  258. case T30_ERR_TX_PHDDEAD: /*! No response after sending a page */
  259. case T30_ERR_TX_T5EXP: /*! Timed out waiting for receiver ready (ECM mode) */
  260. ast_atomic_fetchadd_int(&p->stats->tx_protocol_error, 1);
  261. break;
  262. /* Phase E status values returned to a receiver */
  263. case T30_ERR_RX_ECMPHD: /*! Invalid ECM response received from transmitter */
  264. case T30_ERR_RX_GOTDCS: /*! DCS received while waiting for DTC */
  265. case T30_ERR_RX_INVALCMD: /*! Unexpected command after page received */
  266. case T30_ERR_RX_NOCARRIER: /*! Carrier lost during fax receive */
  267. case T30_ERR_RX_NOEOL: /*! Timed out while waiting for EOL (end Of line) */
  268. ast_atomic_fetchadd_int(&p->stats->rx_protocol_error, 1);
  269. break;
  270. case T30_ERR_RX_NOFAX: /*! Timed out while waiting for first line */
  271. ast_atomic_fetchadd_int(&p->stats->nofax, 1);
  272. break;
  273. case T30_ERR_RX_T2EXPDCN: /*! Timer T2 expired while waiting for DCN */
  274. case T30_ERR_RX_T2EXPD: /*! Timer T2 expired while waiting for phase D */
  275. case T30_ERR_RX_T2EXPFAX: /*! Timer T2 expired while waiting for fax page */
  276. case T30_ERR_RX_T2EXPMPS: /*! Timer T2 expired while waiting for next fax page */
  277. case T30_ERR_RX_T2EXPRR: /*! Timer T2 expired while waiting for RR command */
  278. case T30_ERR_RX_T2EXP: /*! Timer T2 expired while waiting for NSS, DCS or MCF */
  279. case T30_ERR_RX_DCNWHY: /*! Unexpected DCN while waiting for DCS or DIS */
  280. case T30_ERR_RX_DCNDATA: /*! Unexpected DCN while waiting for image data */
  281. case T30_ERR_RX_DCNFAX: /*! Unexpected DCN while waiting for EOM, EOP or MPS */
  282. case T30_ERR_RX_DCNPHD: /*! Unexpected DCN after EOM or MPS sequence */
  283. case T30_ERR_RX_DCNRRD: /*! Unexpected DCN after RR/RNR sequence */
  284. case T30_ERR_RX_DCNNORTN: /*! Unexpected DCN after requested retransmission */
  285. ast_atomic_fetchadd_int(&p->stats->rx_protocol_error, 1);
  286. break;
  287. /* TIFF file problems */
  288. case T30_ERR_FILEERROR: /*! TIFF/F file cannot be opened */
  289. case T30_ERR_NOPAGE: /*! TIFF/F page not found */
  290. case T30_ERR_BADTIFF: /*! TIFF/F format is not compatible */
  291. case T30_ERR_BADPAGE: /*! TIFF/F page number tag missing */
  292. case T30_ERR_BADTAG: /*! Incorrect values for TIFF/F tags */
  293. case T30_ERR_BADTIFFHDR: /*! Bad TIFF/F header - incorrect values in fields */
  294. ast_atomic_fetchadd_int(&p->stats->file_error, 1);
  295. break;
  296. case T30_ERR_NOMEM: /*! Cannot allocate memory for more pages */
  297. ast_atomic_fetchadd_int(&p->stats->mem_error, 1);
  298. break;
  299. /* General problems */
  300. case T30_ERR_RETRYDCN: /*! Disconnected after permitted retries */
  301. ast_atomic_fetchadd_int(&p->stats->retries_exceeded, 1);
  302. break;
  303. case T30_ERR_CALLDROPPED: /*! The call dropped prematurely */
  304. ast_atomic_fetchadd_int(&p->stats->call_dropped, 1);
  305. break;
  306. /* Feature negotiation issues */
  307. case T30_ERR_NOPOLL: /*! Poll not accepted */
  308. case T30_ERR_IDENT_UNACCEPTABLE: /*! Far end's ident is not acceptable */
  309. case T30_ERR_SUB_UNACCEPTABLE: /*! Far end's sub-address is not acceptable */
  310. case T30_ERR_SEP_UNACCEPTABLE: /*! Far end's selective polling address is not acceptable */
  311. case T30_ERR_PSA_UNACCEPTABLE: /*! Far end's polled sub-address is not acceptable */
  312. case T30_ERR_SID_UNACCEPTABLE: /*! Far end's sender identification is not acceptable */
  313. case T30_ERR_PWD_UNACCEPTABLE: /*! Far end's password is not acceptable */
  314. case T30_ERR_TSA_UNACCEPTABLE: /*! Far end's transmitting subscriber internet address is not acceptable */
  315. case T30_ERR_IRA_UNACCEPTABLE: /*! Far end's internet routing address is not acceptable */
  316. case T30_ERR_CIA_UNACCEPTABLE: /*! Far end's calling subscriber internet address is not acceptable */
  317. case T30_ERR_ISP_UNACCEPTABLE: /*! Far end's internet selective polling address is not acceptable */
  318. case T30_ERR_CSA_UNACCEPTABLE: /*! Far end's called subscriber internet address is not acceptable */
  319. ast_atomic_fetchadd_int(&p->stats->neg_failed, 1);
  320. break;
  321. default:
  322. ast_atomic_fetchadd_int(&p->stats->unknown_error, 1);
  323. ast_log(LOG_WARNING, "unknown FAX session result '%d' (%s)\n", completion_code, t30_completion_code_to_str(completion_code));
  324. return -1;
  325. }
  326. return 0;
  327. }
  328. /*! \brief Phase E handler callback.
  329. * \param t30_state the span t30 state
  330. * \param data this will be the ast_fax_session
  331. * \param completion_code the result of the fax session
  332. *
  333. * This function pulls stats from the spandsp stack and stores them for res_fax
  334. * to use later.
  335. */
  336. static void t30_phase_e_handler(t30_state_t *t30_state, void *data, int completion_code)
  337. {
  338. struct ast_fax_session *s = data;
  339. struct spandsp_pvt *p = s->tech_pvt;
  340. char headerinfo[T30_MAX_PAGE_HEADER_INFO + 1];
  341. const char *c;
  342. t30_stats_t stats;
  343. ast_debug(5, "FAX session '%u' entering phase E\n", s->id);
  344. p->isdone = 1;
  345. update_stats(p, completion_code);
  346. t30_get_transfer_statistics(t30_state, &stats);
  347. if (completion_code == T30_ERR_OK) {
  348. ast_string_field_set(s->details, result, "SUCCESS");
  349. } else {
  350. ast_string_field_set(s->details, result, "FAILED");
  351. ast_string_field_set(s->details, error, t30_completion_code_to_str(completion_code));
  352. }
  353. ast_string_field_set(s->details, resultstr, t30_completion_code_to_str(completion_code));
  354. ast_debug(5, "FAX session '%u' completed with result: %s (%s)\n", s->id, s->details->result, s->details->resultstr);
  355. if ((c = t30_get_tx_ident(t30_state))) {
  356. ast_string_field_set(s->details, localstationid, c);
  357. }
  358. if ((c = t30_get_rx_ident(t30_state))) {
  359. ast_string_field_set(s->details, remotestationid, c);
  360. }
  361. #if SPANDSP_RELEASE_DATE >= 20090220
  362. s->details->pages_transferred = (s->details->caps & AST_FAX_TECH_RECEIVE) ? stats.pages_rx : stats.pages_tx;
  363. #else
  364. s->details->pages_transferred = stats.pages_transferred;
  365. #endif
  366. ast_string_field_build(s->details, transfer_rate, "%d", stats.bit_rate);
  367. ast_string_field_build(s->details, resolution, "%dx%d", stats.x_resolution, stats.y_resolution);
  368. t30_get_tx_page_header_info(t30_state, headerinfo);
  369. ast_string_field_set(s->details, headerinfo, headerinfo);
  370. }
  371. /*! \brief Send spandsp log messages to asterisk.
  372. * \param level the spandsp logging level
  373. * \param msg the log message
  374. *
  375. * \note This function is a callback function called by spandsp.
  376. */
  377. static void spandsp_log(int level, const char *msg)
  378. {
  379. if (level == SPAN_LOG_ERROR) {
  380. ast_log(LOG_ERROR, "%s", msg);
  381. } else if (level == SPAN_LOG_WARNING) {
  382. ast_log(LOG_WARNING, "%s", msg);
  383. } else {
  384. ast_fax_log(LOG_DEBUG, msg);
  385. }
  386. }
  387. static void set_logging(logging_state_t *state, struct ast_fax_session_details *details)
  388. {
  389. int level = SPAN_LOG_WARNING;
  390. if (details->option.debug) {
  391. level = SPAN_LOG_DEBUG_3;
  392. }
  393. span_log_set_message_handler(state, spandsp_log);
  394. span_log_set_level(state, SPAN_LOG_SHOW_SEVERITY | SPAN_LOG_SHOW_PROTOCOL | level);
  395. }
  396. static void set_local_info(t30_state_t *t30_state, struct ast_fax_session_details *details)
  397. {
  398. if (!ast_strlen_zero(details->localstationid)) {
  399. t30_set_tx_ident(t30_state, details->localstationid);
  400. }
  401. if (!ast_strlen_zero(details->headerinfo)) {
  402. t30_set_tx_page_header_info(t30_state, details->headerinfo);
  403. }
  404. }
  405. static void set_file(t30_state_t *t30_state, struct ast_fax_session_details *details)
  406. {
  407. if (details->caps & AST_FAX_TECH_RECEIVE) {
  408. t30_set_rx_file(t30_state, AST_LIST_FIRST(&details->documents)->filename, -1);
  409. } else {
  410. /* if not AST_FAX_TECH_RECEIVE, assume AST_FAX_TECH_SEND, this
  411. * should be safe because we ensure either RECEIVE or SEND is
  412. * indicated in spandsp_fax_new() */
  413. t30_set_tx_file(t30_state, AST_LIST_FIRST(&details->documents)->filename, -1, -1);
  414. }
  415. }
  416. static void set_ecm(t30_state_t *t30_state, struct ast_fax_session_details *details)
  417. {
  418. t30_set_ecm_capability(t30_state, details->option.ecm);
  419. t30_set_supported_compressions(t30_state, T30_SUPPORT_T4_1D_COMPRESSION | T30_SUPPORT_T4_2D_COMPRESSION | T30_SUPPORT_T6_COMPRESSION);
  420. }
  421. static int spandsp_v21_new(struct spandsp_pvt *p)
  422. {
  423. /* XXX Here we use MODEM_CONNECT_TONES_FAX_CED_OR_PREAMBLE even though
  424. * we don't care about CED tones. Using MODEM_CONNECT_TONES_PREAMBLE
  425. * doesn't seem to work right all the time.
  426. */
  427. p->tone_state = modem_connect_tones_rx_init(NULL, MODEM_CONNECT_TONES_FAX_CED_OR_PREAMBLE, spandsp_v21_tone, p);
  428. if (!p->tone_state) {
  429. return -1;
  430. }
  431. return 0;
  432. }
  433. static int spandsp_modems(struct ast_fax_session_details *details)
  434. {
  435. int modems = 0;
  436. if (AST_FAX_MODEM_V17 & details->modems) {
  437. modems |= T30_SUPPORT_V17;
  438. }
  439. if (AST_FAX_MODEM_V27TER & details->modems) {
  440. modems |= T30_SUPPORT_V27TER;
  441. }
  442. if (AST_FAX_MODEM_V29 & details->modems) {
  443. modems |= T30_SUPPORT_V29;
  444. }
  445. if (AST_FAX_MODEM_V34 & details->modems) {
  446. #if defined(T30_SUPPORT_V34)
  447. modems |= T30_SUPPORT_V34;
  448. #elif defined(T30_SUPPORT_V34HDX)
  449. modems |= T30_SUPPORT_V34HDX;
  450. #else
  451. ast_log(LOG_WARNING, "v34 not supported in this version of spandsp\n");
  452. #endif
  453. }
  454. return modems;
  455. }
  456. /*! \brief create an instance of the spandsp tech_pvt for a fax session */
  457. static void *spandsp_fax_new(struct ast_fax_session *s, struct ast_fax_tech_token *token)
  458. {
  459. struct spandsp_pvt *p;
  460. int caller_mode;
  461. if ((!(p = ast_calloc(1, sizeof(*p))))) {
  462. ast_log(LOG_ERROR, "Cannot initialize the spandsp private FAX technology structure.\n");
  463. goto e_return;
  464. }
  465. if (s->details->caps & AST_FAX_TECH_V21_DETECT) {
  466. if (spandsp_v21_new(p)) {
  467. ast_log(LOG_ERROR, "Cannot initialize the spandsp private v21 technology structure.\n");
  468. goto e_return;
  469. }
  470. s->state = AST_FAX_STATE_ACTIVE;
  471. return p;
  472. }
  473. if (s->details->caps & AST_FAX_TECH_GATEWAY) {
  474. s->state = AST_FAX_STATE_INITIALIZED;
  475. return p;
  476. }
  477. AST_LIST_HEAD_INIT(&p->read_frames);
  478. if (s->details->caps & AST_FAX_TECH_RECEIVE) {
  479. caller_mode = 0;
  480. } else if (s->details->caps & AST_FAX_TECH_SEND) {
  481. caller_mode = 1;
  482. } else {
  483. ast_log(LOG_ERROR, "Are we sending or receiving? The FAX requirements (capabilities: 0x%X) were not properly set.\n", s->details->caps);
  484. goto e_free;
  485. }
  486. if (!(p->timer = ast_timer_open())) {
  487. ast_log(LOG_ERROR, "Channel '%s' FAX session '%u' failed to create timing source.\n", s->channame, s->id);
  488. goto e_free;
  489. }
  490. s->fd = ast_timer_fd(p->timer);
  491. p->stats = &spandsp_global_stats.g711;
  492. if (s->details->caps & (AST_FAX_TECH_T38 | AST_FAX_TECH_AUDIO)) {
  493. if ((s->details->caps & AST_FAX_TECH_AUDIO) == 0) {
  494. /* audio mode was not requested, start in T.38 mode */
  495. p->ist38 = 1;
  496. p->stats = &spandsp_global_stats.t38;
  497. }
  498. /* init t38 stuff */
  499. t38_terminal_init(&p->t38_state, caller_mode, t38_tx_packet_handler, s);
  500. set_logging(&p->t38_state.logging, s->details);
  501. /* init audio stuff */
  502. fax_init(&p->fax_state, caller_mode);
  503. set_logging(&p->fax_state.logging, s->details);
  504. }
  505. s->state = AST_FAX_STATE_INITIALIZED;
  506. return p;
  507. e_free:
  508. ast_free(p);
  509. e_return:
  510. return NULL;
  511. }
  512. static void spandsp_v21_cleanup(struct ast_fax_session *s)
  513. {
  514. struct spandsp_pvt *p = s->tech_pvt;
  515. modem_connect_tones_rx_free(p->tone_state);
  516. }
  517. /*! \brief Destroy a spandsp fax session.
  518. */
  519. static void spandsp_fax_destroy(struct ast_fax_session *s)
  520. {
  521. struct spandsp_pvt *p = s->tech_pvt;
  522. if (s->details->caps & AST_FAX_TECH_GATEWAY) {
  523. spandsp_fax_gateway_cleanup(s);
  524. } else if (s->details->caps & AST_FAX_TECH_V21_DETECT) {
  525. spandsp_v21_cleanup(s);
  526. } else {
  527. session_destroy(p);
  528. }
  529. ast_free(p);
  530. s->tech_pvt = NULL;
  531. s->fd = -1;
  532. }
  533. /*! \brief Read a frame from the spandsp fax stack.
  534. */
  535. static struct ast_frame *spandsp_fax_read(struct ast_fax_session *s)
  536. {
  537. struct spandsp_pvt *p = s->tech_pvt;
  538. uint8_t buffer[AST_FRIENDLY_OFFSET + SPANDSP_FAX_SAMPLES * sizeof(uint16_t)];
  539. int16_t *buf = (int16_t *) (buffer + AST_FRIENDLY_OFFSET);
  540. int samples;
  541. struct ast_frame fax_frame = {
  542. .frametype = AST_FRAME_VOICE,
  543. .src = "res_fax_spandsp_g711",
  544. .subclass.format = ast_format_slin,
  545. };
  546. struct ast_frame *f = &fax_frame;
  547. if (ast_timer_ack(p->timer, 1) < 0) {
  548. ast_log(LOG_ERROR, "Failed to acknowledge timer for FAX session '%u'\n", s->id);
  549. return NULL;
  550. }
  551. /* XXX do we need to lock here? */
  552. if (p->isdone) {
  553. s->state = AST_FAX_STATE_COMPLETE;
  554. ast_debug(5, "FAX session '%u' is complete.\n", s->id);
  555. return NULL;
  556. }
  557. if (p->ist38) {
  558. t38_terminal_send_timeout(&p->t38_state, SPANDSP_FAX_SAMPLES);
  559. if ((f = AST_LIST_REMOVE_HEAD(&p->read_frames, frame_list))) {
  560. return f;
  561. }
  562. } else {
  563. if ((samples = fax_tx(&p->fax_state, buf, SPANDSP_FAX_SAMPLES)) > 0) {
  564. f->samples = samples;
  565. AST_FRAME_SET_BUFFER(f, buffer, AST_FRIENDLY_OFFSET, samples * sizeof(int16_t));
  566. return ast_frisolate(f);
  567. }
  568. }
  569. return &ast_null_frame;
  570. }
  571. static void spandsp_v21_tone(void *data, int code, int level, int delay)
  572. {
  573. struct spandsp_pvt *p = data;
  574. if (code == MODEM_CONNECT_TONES_FAX_PREAMBLE) {
  575. p->v21_detected = 1;
  576. }
  577. }
  578. static int spandsp_v21_detect(struct ast_fax_session *s, const struct ast_frame *f)
  579. {
  580. struct spandsp_pvt *p = s->tech_pvt;
  581. int16_t *slndata;
  582. g711_state_t *decoder;
  583. if (p->v21_detected) {
  584. return 0;
  585. }
  586. /*invalid frame*/
  587. if (!f->data.ptr || !f->datalen) {
  588. return -1;
  589. }
  590. ast_debug(5, "frame={ datalen=%d, samples=%d, mallocd=%d, src=%s, flags=%u, ts=%ld, len=%ld, seqno=%d, data.ptr=%p, subclass.format=%s }\n", f->datalen, f->samples, f->mallocd, f->src, f->flags, f->ts, f->len, f->seqno, f->data.ptr, ast_format_get_name(f->subclass.format));
  591. /* slinear frame can be passed to spandsp */
  592. if (ast_format_cmp(f->subclass.format, ast_format_slin) == AST_FORMAT_CMP_EQUAL) {
  593. modem_connect_tones_rx(p->tone_state, f->data.ptr, f->samples);
  594. /* alaw/ulaw frame must be converted to slinear before passing to spandsp */
  595. } else if (ast_format_cmp(f->subclass.format, ast_format_alaw) == AST_FORMAT_CMP_EQUAL ||
  596. ast_format_cmp(f->subclass.format, ast_format_ulaw) == AST_FORMAT_CMP_EQUAL) {
  597. if (!(slndata = ast_malloc(sizeof(*slndata) * f->samples))) {
  598. return -1;
  599. }
  600. decoder = g711_init(NULL, (ast_format_cmp(f->subclass.format, ast_format_alaw) == AST_FORMAT_CMP_EQUAL ? G711_ALAW : G711_ULAW));
  601. g711_decode(decoder, slndata, f->data.ptr, f->samples);
  602. ast_debug(5, "spandsp transcoding frame from %s to slinear for v21 detection\n", ast_format_get_name(f->subclass.format));
  603. modem_connect_tones_rx(p->tone_state, slndata, f->samples);
  604. g711_release(decoder);
  605. #if SPANDSP_RELEASE_DATE >= 20090220
  606. g711_free(decoder);
  607. #endif
  608. ast_free(slndata);
  609. /* frame in other formats cannot be passed to spandsp, it could cause segfault */
  610. } else {
  611. ast_log(LOG_WARNING, "Frame format %s not supported, v.21 detection skipped\n", ast_format_get_name(f->subclass.format));
  612. return -1;
  613. }
  614. if (p->v21_detected) {
  615. s->details->option.v21_detected = 1;
  616. ast_debug(5, "v.21 detected\n");
  617. }
  618. return 0;
  619. }
  620. /*! \brief Write a frame to the spandsp fax stack.
  621. * \param s a fax session
  622. * \param f the frame to write
  623. *
  624. * \note res_fax does not currently use the return value of this function.
  625. * Also the fax_rx() function never fails.
  626. *
  627. * \retval 0 success
  628. * \retval -1 failure
  629. */
  630. static int spandsp_fax_write(struct ast_fax_session *s, const struct ast_frame *f)
  631. {
  632. struct spandsp_pvt *p = s->tech_pvt;
  633. if (s->details->caps & AST_FAX_TECH_V21_DETECT) {
  634. return spandsp_v21_detect(s, f);
  635. }
  636. if (s->details->caps & AST_FAX_TECH_GATEWAY) {
  637. return spandsp_fax_gateway_process(s, f);
  638. }
  639. /* XXX do we need to lock here? */
  640. if (s->state == AST_FAX_STATE_COMPLETE) {
  641. ast_log(LOG_WARNING, "FAX session '%u' is in the '%s' state.\n", s->id, ast_fax_state_to_str(s->state));
  642. return -1;
  643. }
  644. if (p->ist38) {
  645. return t38_core_rx_ifp_packet(p->t38_core_state, f->data.ptr, f->datalen, f->seqno);
  646. } else {
  647. return fax_rx(&p->fax_state, f->data.ptr, f->samples);
  648. }
  649. }
  650. /*! \brief generate T.30 packets sent to the T.30 leg of gateway
  651. * \param chan T.30 channel
  652. * \param data fax session structure
  653. * \param len not used
  654. * \param samples no of samples generated
  655. * \return -1 on failure or 0 on sucess*/
  656. static int spandsp_fax_gw_t30_gen(struct ast_channel *chan, void *data, int len, int samples)
  657. {
  658. int res = -1;
  659. struct ast_fax_session *s = data;
  660. struct spandsp_pvt *p = s->tech_pvt;
  661. uint8_t buffer[AST_FRIENDLY_OFFSET + samples * sizeof(uint16_t)];
  662. struct ast_frame *f;
  663. struct ast_frame t30_frame = {
  664. .frametype = AST_FRAME_VOICE,
  665. .subclass.format = ast_format_slin,
  666. .src = "res_fax_spandsp_g711",
  667. .samples = samples,
  668. .flags = AST_FAX_FRFLAG_GATEWAY,
  669. };
  670. AST_FRAME_SET_BUFFER(&t30_frame, buffer, AST_FRIENDLY_OFFSET, t30_frame.samples * sizeof(int16_t));
  671. if (!(f = ast_frisolate(&t30_frame))) {
  672. return p->isdone ? -1 : res;
  673. }
  674. /* generate a T.30 packet */
  675. if ((f->samples = t38_gateway_tx(&p->t38_gw_state, f->data.ptr, f->samples))) {
  676. f->datalen = f->samples * sizeof(int16_t);
  677. res = ast_write(chan, f);
  678. }
  679. ast_frfree(f);
  680. return p->isdone ? -1 : res;
  681. }
  682. /*! \brief simple routine to allocate data to generator
  683. * \param chan channel
  684. * \param params generator data
  685. * \return data to use in generator call*/
  686. static void *spandsp_fax_gw_gen_alloc(struct ast_channel *chan, void *params)
  687. {
  688. ao2_ref(params, +1);
  689. return params;
  690. }
  691. static void spandsp_fax_gw_gen_release(struct ast_channel *chan, void *data)
  692. {
  693. ao2_ref(data, -1);
  694. }
  695. /*! \brief activate a spandsp gateway based on the information in the given fax session
  696. * \param s fax session
  697. * \return -1 on error 0 on sucess*/
  698. static int spandsp_fax_gateway_start(struct ast_fax_session *s)
  699. {
  700. struct spandsp_pvt *p = s->tech_pvt;
  701. struct ast_fax_t38_parameters *t38_param;
  702. int i;
  703. RAII_VAR(struct ast_channel *, peer, NULL, ao2_cleanup);
  704. static struct ast_generator t30_gen = {
  705. .alloc = spandsp_fax_gw_gen_alloc,
  706. .release = spandsp_fax_gw_gen_release,
  707. .generate = spandsp_fax_gw_t30_gen,
  708. };
  709. #if SPANDSP_RELEASE_DATE >= 20081012
  710. /* for spandsp shaphots 0.0.6 and higher */
  711. p->t38_core_state=&p->t38_gw_state.t38x.t38;
  712. #else
  713. /* for spandsp release 0.0.5 */
  714. p->t38_core_state=&p->t38_gw_state.t38;
  715. #endif
  716. if (!t38_gateway_init(&p->t38_gw_state, t38_tx_packet_handler, s)) {
  717. return -1;
  718. }
  719. p->ist38 = 1;
  720. p->ast_t38_state = ast_channel_get_t38_state(s->chan);
  721. peer = ast_channel_bridge_peer(s->chan);
  722. if (!peer) {
  723. return -1;
  724. }
  725. /* we can be in T38_STATE_NEGOTIATING or T38_STATE_NEGOTIATED when the
  726. * gateway is started. We treat both states the same. */
  727. if (p->ast_t38_state == T38_STATE_NEGOTIATING) {
  728. p->ast_t38_state = T38_STATE_NEGOTIATED;
  729. }
  730. ast_activate_generator(p->ast_t38_state == T38_STATE_NEGOTIATED ? peer : s->chan, &t30_gen , s);
  731. set_logging(&p->t38_gw_state.logging, s->details);
  732. set_logging(&p->t38_core_state->logging, s->details);
  733. t38_param = (p->ast_t38_state == T38_STATE_NEGOTIATED) ? &s->details->our_t38_parameters : &s->details->their_t38_parameters;
  734. t38_set_t38_version(p->t38_core_state, t38_param->version);
  735. t38_gateway_set_ecm_capability(&p->t38_gw_state, s->details->option.ecm);
  736. t38_set_max_datagram_size(p->t38_core_state, t38_param->max_ifp);
  737. t38_set_fill_bit_removal(p->t38_core_state, t38_param->fill_bit_removal);
  738. t38_set_mmr_transcoding(p->t38_core_state, t38_param->transcoding_mmr);
  739. t38_set_jbig_transcoding(p->t38_core_state, t38_param->transcoding_jbig);
  740. t38_set_data_rate_management_method(p->t38_core_state,
  741. (t38_param->rate_management == AST_T38_RATE_MANAGEMENT_TRANSFERRED_TCF)? 1 : 2);
  742. t38_gateway_set_transmit_on_idle(&p->t38_gw_state, TRUE);
  743. t38_set_sequence_number_handling(p->t38_core_state, TRUE);
  744. t38_gateway_set_supported_modems(&p->t38_gw_state, spandsp_modems(s->details));
  745. /* engage udptl nat on other side of T38 line
  746. * (Asterisk changes media ports thus we send a few packets to reinitialize
  747. * pinholes in NATs and FWs
  748. */
  749. for (i=0; i < SPANDSP_ENGAGE_UDPTL_NAT_RETRY; i++) {
  750. #if SPANDSP_RELEASE_DATE >= 20091228
  751. t38_core_send_indicator(&p->t38_gw_state.t38x.t38, T38_IND_NO_SIGNAL);
  752. #elif SPANDSP_RELEASE_DATE >= 20081012
  753. t38_core_send_indicator(&p->t38_gw_state.t38x.t38, T38_IND_NO_SIGNAL, p->t38_gw_state.t38x.t38.indicator_tx_count);
  754. #else
  755. t38_core_send_indicator(&p->t38_gw_state.t38, T38_IND_NO_SIGNAL, p->t38_gw_state.t38.indicator_tx_count);
  756. #endif
  757. }
  758. s->state = AST_FAX_STATE_ACTIVE;
  759. return 0;
  760. }
  761. /*! \brief process a frame from the bridge
  762. * \param s fax session
  763. * \param f frame to process
  764. * \return 1 on sucess 0 on incorect packet*/
  765. static int spandsp_fax_gateway_process(struct ast_fax_session *s, const struct ast_frame *f)
  766. {
  767. struct spandsp_pvt *p = s->tech_pvt;
  768. /*invalid frame*/
  769. if (!f->data.ptr || !f->datalen) {
  770. return -1;
  771. }
  772. /* Process a IFP packet */
  773. if ((f->frametype == AST_FRAME_MODEM) && (f->subclass.integer == AST_MODEM_T38)) {
  774. return t38_core_rx_ifp_packet(p->t38_core_state, f->data.ptr, f->datalen, f->seqno);
  775. } else if ((f->frametype == AST_FRAME_VOICE) &&
  776. (ast_format_cmp(f->subclass.format, ast_format_slin) == AST_FORMAT_CMP_EQUAL)) {
  777. return t38_gateway_rx(&p->t38_gw_state, f->data.ptr, f->samples);
  778. }
  779. return -1;
  780. }
  781. /*! \brief gather data and clean up after gateway ends
  782. * \param s fax session*/
  783. static void spandsp_fax_gateway_cleanup(struct ast_fax_session *s)
  784. {
  785. struct spandsp_pvt *p = s->tech_pvt;
  786. t38_stats_t t38_stats;
  787. t38_gateway_get_transfer_statistics(&p->t38_gw_state, &t38_stats);
  788. s->details->option.ecm = t38_stats.error_correcting_mode ? AST_FAX_OPTFLAG_TRUE : AST_FAX_OPTFLAG_FALSE;
  789. s->details->pages_transferred = t38_stats.pages_transferred;
  790. ast_string_field_build(s->details, transfer_rate, "%d", t38_stats.bit_rate);
  791. }
  792. /*! \brief */
  793. static int spandsp_fax_start(struct ast_fax_session *s)
  794. {
  795. struct spandsp_pvt *p = s->tech_pvt;
  796. s->state = AST_FAX_STATE_OPEN;
  797. if (s->details->caps & AST_FAX_TECH_GATEWAY) {
  798. return spandsp_fax_gateway_start(s);
  799. }
  800. if (p->ist38) {
  801. #if SPANDSP_RELEASE_DATE >= 20080725
  802. /* for spandsp shaphots 0.0.6 and higher */
  803. p->t30_state = &p->t38_state.t30;
  804. p->t38_core_state = &p->t38_state.t38_fe.t38;
  805. #else
  806. /* for spandsp releases 0.0.5 */
  807. p->t30_state = &p->t38_state.t30_state;
  808. p->t38_core_state = &p->t38_state.t38;
  809. #endif
  810. } else {
  811. #if SPANDSP_RELEASE_DATE >= 20080725
  812. /* for spandsp shaphots 0.0.6 and higher */
  813. p->t30_state = &p->fax_state.t30;
  814. #else
  815. /* for spandsp release 0.0.5 */
  816. p->t30_state = &p->fax_state.t30_state;
  817. #endif
  818. }
  819. set_logging(&p->t30_state->logging, s->details);
  820. /* set some parameters */
  821. set_local_info(p->t30_state, s->details);
  822. set_file(p->t30_state, s->details);
  823. set_ecm(p->t30_state, s->details);
  824. t30_set_supported_modems(p->t30_state, spandsp_modems(s->details));
  825. /* perhaps set_transmit_on_idle() should be called */
  826. t30_set_phase_e_handler(p->t30_state, t30_phase_e_handler, s);
  827. /* set T.38 parameters */
  828. if (p->ist38) {
  829. set_logging(&p->t38_core_state->logging, s->details);
  830. t38_set_max_datagram_size(p->t38_core_state, s->details->their_t38_parameters.max_ifp);
  831. if (s->details->their_t38_parameters.fill_bit_removal) {
  832. t38_set_fill_bit_removal(p->t38_core_state, TRUE);
  833. }
  834. if (s->details->their_t38_parameters.transcoding_mmr) {
  835. t38_set_mmr_transcoding(p->t38_core_state, TRUE);
  836. }
  837. if (s->details->their_t38_parameters.transcoding_jbig) {
  838. t38_set_jbig_transcoding(p->t38_core_state, TRUE);
  839. }
  840. } else {
  841. /* have the fax stack generate silence if it has no data to send */
  842. fax_set_transmit_on_idle(&p->fax_state, 1);
  843. }
  844. /* start the timer */
  845. if (ast_timer_set_rate(p->timer, SPANDSP_FAX_TIMER_RATE)) {
  846. ast_log(LOG_ERROR, "FAX session '%u' error setting rate on timing source.\n", s->id);
  847. return -1;
  848. }
  849. s->state = AST_FAX_STATE_ACTIVE;
  850. return 0;
  851. }
  852. /*! \brief */
  853. static int spandsp_fax_cancel(struct ast_fax_session *s)
  854. {
  855. struct spandsp_pvt *p = s->tech_pvt;
  856. if (s->details->caps & AST_FAX_TECH_GATEWAY) {
  857. p->isdone = 1;
  858. return 0;
  859. }
  860. t30_terminate(p->t30_state);
  861. p->isdone = 1;
  862. return 0;
  863. }
  864. /*! \brief */
  865. static int spandsp_fax_switch_to_t38(struct ast_fax_session *s)
  866. {
  867. struct spandsp_pvt *p = s->tech_pvt;
  868. /* prevent the phase E handler from running, this is not a real termination */
  869. t30_set_phase_e_handler(p->t30_state, NULL, NULL);
  870. t30_terminate(p->t30_state);
  871. s->details->option.switch_to_t38 = 1;
  872. ast_atomic_fetchadd_int(&p->stats->switched, 1);
  873. p->ist38 = 1;
  874. p->stats = &spandsp_global_stats.t38;
  875. spandsp_fax_start(s);
  876. return 0;
  877. }
  878. /*! \brief */
  879. static char *spandsp_fax_cli_show_capabilities(int fd)
  880. {
  881. ast_cli(fd, "SEND RECEIVE T.38 G.711 GATEWAY\n\n");
  882. return CLI_SUCCESS;
  883. }
  884. /*! \brief */
  885. static char *spandsp_fax_cli_show_session(struct ast_fax_session *s, int fd)
  886. {
  887. ao2_lock(s);
  888. if (s->details->caps & AST_FAX_TECH_GATEWAY) {
  889. struct spandsp_pvt *p = s->tech_pvt;
  890. ast_cli(fd, "%-22s : %u\n", "session", s->id);
  891. ast_cli(fd, "%-22s : %s\n", "operation", "Gateway");
  892. ast_cli(fd, "%-22s : %s\n", "state", ast_fax_state_to_str(s->state));
  893. if (s->state != AST_FAX_STATE_UNINITIALIZED) {
  894. t38_stats_t stats;
  895. t38_gateway_get_transfer_statistics(&p->t38_gw_state, &stats);
  896. ast_cli(fd, "%-22s : %s\n", "ECM Mode", stats.error_correcting_mode ? "Yes" : "No");
  897. ast_cli(fd, "%-22s : %d\n", "Data Rate", stats.bit_rate);
  898. ast_cli(fd, "%-22s : %d\n", "Page Number", stats.pages_transferred + 1);
  899. }
  900. } else if (s->details->caps & AST_FAX_TECH_V21_DETECT) {
  901. ast_cli(fd, "%-22s : %u\n", "session", s->id);
  902. ast_cli(fd, "%-22s : %s\n", "operation", "V.21 Detect");
  903. ast_cli(fd, "%-22s : %s\n", "state", ast_fax_state_to_str(s->state));
  904. } else {
  905. struct spandsp_pvt *p = s->tech_pvt;
  906. ast_cli(fd, "%-22s : %u\n", "session", s->id);
  907. ast_cli(fd, "%-22s : %s\n", "operation", (s->details->caps & AST_FAX_TECH_RECEIVE) ? "Receive" : "Transmit");
  908. ast_cli(fd, "%-22s : %s\n", "state", ast_fax_state_to_str(s->state));
  909. if (s->state != AST_FAX_STATE_UNINITIALIZED) {
  910. t30_stats_t stats;
  911. t30_get_transfer_statistics(p->t30_state, &stats);
  912. ast_cli(fd, "%-22s : %s\n", "Last Status", t30_completion_code_to_str(stats.current_status));
  913. ast_cli(fd, "%-22s : %s\n", "ECM Mode", stats.error_correcting_mode ? "Yes" : "No");
  914. ast_cli(fd, "%-22s : %d\n", "Data Rate", stats.bit_rate);
  915. ast_cli(fd, "%-22s : %dx%d\n", "Image Resolution", stats.x_resolution, stats.y_resolution);
  916. #if SPANDSP_RELEASE_DATE >= 20090220
  917. ast_cli(fd, "%-22s : %d\n", "Page Number", ((s->details->caps & AST_FAX_TECH_RECEIVE) ? stats.pages_rx : stats.pages_tx) + 1);
  918. #else
  919. ast_cli(fd, "%-22s : %d\n", "Page Number", stats.pages_transferred + 1);
  920. #endif
  921. ast_cli(fd, "%-22s : %s\n", "File Name", s->details->caps & AST_FAX_TECH_RECEIVE ? p->t30_state->rx_file : p->t30_state->tx_file);
  922. ast_cli(fd, "\nData Statistics:\n");
  923. #if SPANDSP_RELEASE_DATE >= 20090220
  924. ast_cli(fd, "%-22s : %d\n", "Tx Pages", stats.pages_tx);
  925. ast_cli(fd, "%-22s : %d\n", "Rx Pages", stats.pages_rx);
  926. #else
  927. ast_cli(fd, "%-22s : %d\n", "Tx Pages", (s->details->caps & AST_FAX_TECH_SEND) ? stats.pages_transferred : 0);
  928. ast_cli(fd, "%-22s : %d\n", "Rx Pages", (s->details->caps & AST_FAX_TECH_RECEIVE) ? stats.pages_transferred : 0);
  929. #endif
  930. ast_cli(fd, "%-22s : %d\n", "Longest Bad Line Run", stats.longest_bad_row_run);
  931. ast_cli(fd, "%-22s : %d\n", "Total Bad Lines", stats.bad_rows);
  932. }
  933. }
  934. ao2_unlock(s);
  935. ast_cli(fd, "\n\n");
  936. return CLI_SUCCESS;
  937. }
  938. static void spandsp_manager_fax_session(struct mansession *s,
  939. const char *id_text, struct ast_fax_session *session)
  940. {
  941. struct ast_str *message_string;
  942. struct spandsp_pvt *span_pvt = session->tech_pvt;
  943. int res;
  944. message_string = ast_str_create(128);
  945. if (!message_string) {
  946. return;
  947. }
  948. ao2_lock(session);
  949. res = ast_str_append(&message_string, 0, "SessionNumber: %u\r\n", session->id);
  950. res |= ast_str_append(&message_string, 0, "Operation: %s\r\n", ast_fax_session_operation_str(session));
  951. res |= ast_str_append(&message_string, 0, "State: %s\r\n", ast_fax_state_to_str(session->state));
  952. if (session->details->caps & AST_FAX_TECH_GATEWAY) {
  953. t38_stats_t stats;
  954. if (session->state == AST_FAX_STATE_UNINITIALIZED) {
  955. goto skip_cap_additions;
  956. }
  957. t38_gateway_get_transfer_statistics(&span_pvt->t38_gw_state, &stats);
  958. res |= ast_str_append(&message_string, 0, "ErrorCorrectionMode: %s\r\n",
  959. stats.error_correcting_mode ? "yes" : "no");
  960. res |= ast_str_append(&message_string, 0, "DataRate: %d\r\n",
  961. stats.bit_rate);
  962. res |= ast_str_append(&message_string, 0, "PageNumber: %d\r\n",
  963. stats.pages_transferred + 1);
  964. } else if (!(session->details->caps & AST_FAX_TECH_V21_DETECT)) { /* caps is SEND/RECEIVE */
  965. t30_stats_t stats;
  966. if (session->state == AST_FAX_STATE_UNINITIALIZED) {
  967. goto skip_cap_additions;
  968. }
  969. t30_get_transfer_statistics(span_pvt->t30_state, &stats);
  970. res |= ast_str_append(&message_string, 0, "ErrorCorrectionMode: %s\r\n",
  971. stats.error_correcting_mode ? "Yes" : "No");
  972. res |= ast_str_append(&message_string, 0, "DataRate: %d\r\n",
  973. stats.bit_rate);
  974. res |= ast_str_append(&message_string, 0, "ImageResolution: %dx%d\r\n",
  975. stats.x_resolution, stats.y_resolution);
  976. #if SPANDSP_RELEASE_DATE >= 20090220
  977. res |= ast_str_append(&message_string, 0, "PageNumber: %d\r\n",
  978. ((session->details->caps & AST_FAX_TECH_RECEIVE) ? stats.pages_rx : stats.pages_tx) + 1);
  979. #else
  980. res |= ast_str_append(&message_string, 0, "PageNumber: %d\r\n",
  981. stats.pages_transferred + 1);
  982. #endif
  983. res |= ast_str_append(&message_string, 0, "FileName: %s\r\n",
  984. session->details->caps & AST_FAX_TECH_RECEIVE ? span_pvt->t30_state->rx_file :
  985. span_pvt->t30_state->tx_file);
  986. #if SPANDSP_RELEASE_DATE >= 20090220
  987. res |= ast_str_append(&message_string, 0, "PagesTransmitted: %d\r\n",
  988. stats.pages_tx);
  989. res |= ast_str_append(&message_string, 0, "PagesReceived: %d\r\n",
  990. stats.pages_rx);
  991. #else
  992. res |= ast_str_append(&message_string, 0, "PagesTransmitted: %d\r\n",
  993. (session->details->caps & AST_FAX_TECH_SEND) ? stats.pages_transferred : 0);
  994. res |= ast_str_append(&message_string, 0, "PagesReceived: %d\r\n",
  995. (session->details->caps & AST_FAX_TECH_RECEIVE) ? stats.pages_transferred : 0);
  996. #endif
  997. res |= ast_str_append(&message_string, 0, "TotalBadLines: %d\r\n",
  998. stats.bad_rows);
  999. }
  1000. skip_cap_additions:
  1001. ao2_unlock(session);
  1002. if (res < 0) {
  1003. /* One or more of the ast_str_append attempts failed, cancel the message */
  1004. ast_free(message_string);
  1005. return;
  1006. }
  1007. astman_append(s, "Event: FAXSession\r\n"
  1008. "%s"
  1009. "%s"
  1010. "\r\n",
  1011. id_text,
  1012. ast_str_buffer(message_string));
  1013. ast_free(message_string);
  1014. }
  1015. /*! \brief */
  1016. static char *spandsp_fax_cli_show_stats(int fd)
  1017. {
  1018. ast_mutex_lock(&spandsp_global_stats.lock);
  1019. ast_cli(fd, "\n%-20.20s\n", "Spandsp G.711");
  1020. ast_cli(fd, "%-20.20s : %d\n", "Success", spandsp_global_stats.g711.success);
  1021. ast_cli(fd, "%-20.20s : %d\n", "Switched to T.38", spandsp_global_stats.g711.switched);
  1022. ast_cli(fd, "%-20.20s : %d\n", "Call Dropped", spandsp_global_stats.g711.call_dropped);
  1023. ast_cli(fd, "%-20.20s : %d\n", "No FAX", spandsp_global_stats.g711.nofax);
  1024. ast_cli(fd, "%-20.20s : %d\n", "Negotiation Failed", spandsp_global_stats.g711.neg_failed);
  1025. ast_cli(fd, "%-20.20s : %d\n", "Train Failure", spandsp_global_stats.g711.failed_to_train);
  1026. ast_cli(fd, "%-20.20s : %d\n", "Retries Exceeded", spandsp_global_stats.g711.retries_exceeded);
  1027. ast_cli(fd, "%-20.20s : %d\n", "Protocol Error", spandsp_global_stats.g711.protocol_error);
  1028. ast_cli(fd, "%-20.20s : %d\n", "TX Protocol Error", spandsp_global_stats.g711.tx_protocol_error);
  1029. ast_cli(fd, "%-20.20s : %d\n", "RX Protocol Error", spandsp_global_stats.g711.rx_protocol_error);
  1030. ast_cli(fd, "%-20.20s : %d\n", "File Error", spandsp_global_stats.g711.file_error);
  1031. ast_cli(fd, "%-20.20s : %d\n", "Memory Error", spandsp_global_stats.g711.mem_error);
  1032. ast_cli(fd, "%-20.20s : %d\n", "Unknown Error", spandsp_global_stats.g711.unknown_error);
  1033. ast_cli(fd, "\n%-20.20s\n", "Spandsp T.38");
  1034. ast_cli(fd, "%-20.20s : %d\n", "Success", spandsp_global_stats.t38.success);
  1035. ast_cli(fd, "%-20.20s : %d\n", "Call Dropped", spandsp_global_stats.t38.call_dropped);
  1036. ast_cli(fd, "%-20.20s : %d\n", "No FAX", spandsp_global_stats.t38.nofax);
  1037. ast_cli(fd, "%-20.20s : %d\n", "Negotiation Failed", spandsp_global_stats.t38.neg_failed);
  1038. ast_cli(fd, "%-20.20s : %d\n", "Train Failure", spandsp_global_stats.t38.failed_to_train);
  1039. ast_cli(fd, "%-20.20s : %d\n", "Retries Exceeded", spandsp_global_stats.t38.retries_exceeded);
  1040. ast_cli(fd, "%-20.20s : %d\n", "Protocol Error", spandsp_global_stats.t38.protocol_error);
  1041. ast_cli(fd, "%-20.20s : %d\n", "TX Protocol Error", spandsp_global_stats.t38.tx_protocol_error);
  1042. ast_cli(fd, "%-20.20s : %d\n", "RX Protocol Error", spandsp_global_stats.t38.rx_protocol_error);
  1043. ast_cli(fd, "%-20.20s : %d\n", "File Error", spandsp_global_stats.t38.file_error);
  1044. ast_cli(fd, "%-20.20s : %d\n", "Memory Error", spandsp_global_stats.t38.mem_error);
  1045. ast_cli(fd, "%-20.20s : %d\n", "Unknown Error", spandsp_global_stats.t38.unknown_error);
  1046. ast_mutex_unlock(&spandsp_global_stats.lock);
  1047. return CLI_SUCCESS;
  1048. }
  1049. /*! \brief Show res_fax_spandsp settings */
  1050. static char *spandsp_fax_cli_show_settings(int fd)
  1051. {
  1052. /* no settings at the moment */
  1053. return CLI_SUCCESS;
  1054. }
  1055. /*! \brief unload res_fax_spandsp */
  1056. static int unload_module(void)
  1057. {
  1058. ast_fax_tech_unregister(&spandsp_fax_tech);
  1059. ast_mutex_destroy(&spandsp_global_stats.lock);
  1060. return AST_MODULE_LOAD_SUCCESS;
  1061. }
  1062. /*! \brief load res_fax_spandsp */
  1063. static int load_module(void)
  1064. {
  1065. ast_mutex_init(&spandsp_global_stats.lock);
  1066. spandsp_fax_tech.module = ast_module_info->self;
  1067. if (ast_fax_tech_register(&spandsp_fax_tech) < 0) {
  1068. ast_log(LOG_ERROR, "failed to register FAX technology\n");
  1069. return AST_MODULE_LOAD_DECLINE;
  1070. }
  1071. /* prevent logging to stderr */
  1072. span_set_message_handler(NULL);
  1073. return AST_MODULE_LOAD_SUCCESS;
  1074. }
  1075. AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_DEFAULT, "Spandsp G.711 and T.38 FAX Technologies",
  1076. .support_level = AST_MODULE_SUPPORT_EXTENDED,
  1077. .load = load_module,
  1078. .unload = unload_module,
  1079. );