fax_tester.c 67 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909
  1. /*
  2. * SpanDSP - a series of DSP components for telephony
  3. *
  4. * fax_tester.c
  5. *
  6. * Written by Steve Underwood <steveu@coppice.org>
  7. *
  8. * Copyright (C) 2008 Steve Underwood
  9. *
  10. * All rights reserved.
  11. *
  12. * This program is free software; you can redistribute it and/or modify
  13. * it under the terms of the GNU General Public License version 2, as
  14. * published by the Free Software Foundation.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU General Public License
  22. * along with this program; if not, write to the Free Software
  23. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  24. */
  25. /*! \file */
  26. #if defined(HAVE_CONFIG_H)
  27. #include "config.h"
  28. #endif
  29. #include <inttypes.h>
  30. #include <stdlib.h>
  31. #include <stdio.h>
  32. #include <string.h>
  33. #include <ctype.h>
  34. #if defined(HAVE_TGMATH_H)
  35. #include <tgmath.h>
  36. #endif
  37. #if defined(HAVE_MATH_H)
  38. #include <math.h>
  39. #endif
  40. #include "floating_fudge.h"
  41. #include <assert.h>
  42. #include <fcntl.h>
  43. #include <time.h>
  44. #include <unistd.h>
  45. #if defined(HAVE_LIBXML_XMLMEMORY_H)
  46. #include <libxml/xmlmemory.h>
  47. #endif
  48. #if defined(HAVE_LIBXML_PARSER_H)
  49. #include <libxml/parser.h>
  50. #endif
  51. #if defined(HAVE_LIBXML_XINCLUDE_H)
  52. #include <libxml/xinclude.h>
  53. #endif
  54. #define SPANDSP_EXPOSE_INTERNAL_STRUCTURES
  55. #include "spandsp.h"
  56. #include "fax_utils.h"
  57. #include "fax_tester.h"
  58. #define HDLC_FRAMING_OK_THRESHOLD 5
  59. extern const char *output_tiff_file_name;
  60. struct xml_node_parms_s
  61. {
  62. xmlChar *dir;
  63. xmlChar *type;
  64. xmlChar *modem;
  65. xmlChar *value;
  66. xmlChar *tag;
  67. xmlChar *bad_rows;
  68. xmlChar *crc_error;
  69. xmlChar *pattern;
  70. xmlChar *timein;
  71. xmlChar *timeout;
  72. xmlChar *min_bits;
  73. xmlChar *frame_size;
  74. xmlChar *block;
  75. xmlChar *compression;
  76. };
  77. struct
  78. {
  79. const char *tag;
  80. int code;
  81. } t30_status[] =
  82. {
  83. {"OK", T30_ERR_OK},
  84. {"CEDTONE", T30_ERR_CEDTONE},
  85. {"T0_EXPIRED", T30_ERR_T0_EXPIRED},
  86. {"T1_EXPIRED", T30_ERR_T1_EXPIRED},
  87. {"T3_EXPIRED", T30_ERR_T3_EXPIRED},
  88. {"HDLC_CARRIER", T30_ERR_HDLC_CARRIER},
  89. {"CANNOT_TRAIN", T30_ERR_CANNOT_TRAIN},
  90. {"OPER_INT_FAIL", T30_ERR_OPER_INT_FAIL},
  91. {"INCOMPATIBLE", T30_ERR_INCOMPATIBLE},
  92. {"RX_INCAPABLE", T30_ERR_RX_INCAPABLE},
  93. {"TX_INCAPABLE", T30_ERR_TX_INCAPABLE},
  94. {"NORESSUPPORT", T30_ERR_NORESSUPPORT},
  95. {"NOSIZESUPPORT", T30_ERR_NOSIZESUPPORT},
  96. {"UNEXPECTED", T30_ERR_UNEXPECTED},
  97. {"TX_BADDCS", T30_ERR_TX_BADDCS},
  98. {"TX_BADPG", T30_ERR_TX_BADPG},
  99. {"TX_ECMPHD", T30_ERR_TX_ECMPHD},
  100. {"TX_GOTDCN", T30_ERR_TX_GOTDCN},
  101. {"TX_INVALRSP", T30_ERR_TX_INVALRSP},
  102. {"TX_NODIS", T30_ERR_TX_NODIS},
  103. {"TX_PHBDEAD", T30_ERR_TX_PHBDEAD},
  104. {"TX_PHDDEAD", T30_ERR_TX_PHDDEAD},
  105. {"TX_T5EXP", T30_ERR_TX_T5EXP},
  106. {"RX_ECMPHD", T30_ERR_RX_ECMPHD},
  107. {"RX_GOTDCS", T30_ERR_RX_GOTDCS},
  108. {"RX_INVALCMD", T30_ERR_RX_INVALCMD},
  109. {"RX_NOCARRIER", T30_ERR_RX_NOCARRIER},
  110. {"RX_NOEOL", T30_ERR_RX_NOEOL},
  111. {"RX_NOFAX", T30_ERR_RX_NOFAX},
  112. {"RX_T2EXPDCN", T30_ERR_RX_T2EXPDCN},
  113. {"RX_T2EXPD", T30_ERR_RX_T2EXPD},
  114. {"RX_T2EXPFAX", T30_ERR_RX_T2EXPFAX},
  115. {"RX_T2EXPMPS", T30_ERR_RX_T2EXPMPS},
  116. {"RX_T2EXPRR", T30_ERR_RX_T2EXPRR},
  117. {"RX_T2EXP", T30_ERR_RX_T2EXP},
  118. {"RX_DCNWHY", T30_ERR_RX_DCNWHY},
  119. {"RX_DCNDATA", T30_ERR_RX_DCNDATA},
  120. {"RX_DCNFAX", T30_ERR_RX_DCNFAX},
  121. {"RX_DCNPHD", T30_ERR_RX_DCNPHD},
  122. {"RX_DCNRRD", T30_ERR_RX_DCNRRD},
  123. {"RX_DCNNORTN", T30_ERR_RX_DCNNORTN},
  124. {"FILEERROR", T30_ERR_FILEERROR},
  125. {"NOPAGE", T30_ERR_NOPAGE},
  126. {"BADTIFF", T30_ERR_BADTIFF},
  127. {"BADPAGE", T30_ERR_BADPAGE},
  128. {"BADTAG", T30_ERR_BADTAG},
  129. {"BADTIFFHDR", T30_ERR_BADTIFFHDR},
  130. {"NOMEM", T30_ERR_NOMEM},
  131. {"RETRYDCN", T30_ERR_RETRYDCN},
  132. {"CALLDROPPED", T30_ERR_CALLDROPPED},
  133. {"NOPOLL", T30_ERR_NOPOLL},
  134. {"IDENT_UNACCEPTABLE", T30_ERR_IDENT_UNACCEPTABLE},
  135. {"SUB_UNACCEPTABLE", T30_ERR_SUB_UNACCEPTABLE},
  136. {"SEP_UNACCEPTABLE", T30_ERR_SEP_UNACCEPTABLE},
  137. {"PSA_UNACCEPTABLE", T30_ERR_PSA_UNACCEPTABLE},
  138. {"SID_UNACCEPTABLE", T30_ERR_SID_UNACCEPTABLE},
  139. {"PWD_UNACCEPTABLE", T30_ERR_PWD_UNACCEPTABLE},
  140. {"TSA_UNACCEPTABLE", T30_ERR_TSA_UNACCEPTABLE},
  141. {"IRA_UNACCEPTABLE", T30_ERR_IRA_UNACCEPTABLE},
  142. {"CIA_UNACCEPTABLE", T30_ERR_CIA_UNACCEPTABLE},
  143. {"ISP_UNACCEPTABLE", T30_ERR_ISP_UNACCEPTABLE},
  144. {"CSA_UNACCEPTABLE", T30_ERR_CSA_UNACCEPTABLE},
  145. {NULL, -1}
  146. };
  147. static void timer_update(faxtester_state_t *s, int len)
  148. {
  149. s->timer += len;
  150. if (s->timer > s->timeout)
  151. {
  152. s->timeout = 0x7FFFFFFFFFFFFFFFLL;
  153. span_log(&s->logging, SPAN_LOG_FLOW, "FAX tester step timed out\n");
  154. printf("Test failed\n");
  155. exit(2);
  156. }
  157. }
  158. /*- End of function --------------------------------------------------------*/
  159. static void front_end_step_complete(faxtester_state_t *s)
  160. {
  161. while (faxtester_next_step(s) == 0)
  162. ;
  163. /*endwhile*/
  164. }
  165. /*- End of function --------------------------------------------------------*/
  166. static int faxtester_phase_b_handler(void *user_data, int result)
  167. {
  168. int ch;
  169. int status;
  170. faxtester_state_t *s;
  171. const char *u;
  172. s = (faxtester_state_t *) user_data;
  173. ch = s->far_tag;
  174. status = T30_ERR_OK;
  175. if ((u = t30_get_rx_ident(s->far_t30)))
  176. {
  177. printf("%c: Phase B: remote ident '%s'\n", ch, u);
  178. if (s->expected_rx_info.ident[0] && strcmp(s->expected_rx_info.ident, u))
  179. {
  180. printf("%c: Phase B: remote ident incorrect! - expected '%s'\n", ch, s->expected_rx_info.ident);
  181. status = T30_ERR_IDENT_UNACCEPTABLE;
  182. }
  183. }
  184. else
  185. {
  186. if (s->expected_rx_info.ident[0])
  187. {
  188. printf("%c: Phase B: remote ident missing!\n", ch);
  189. status = T30_ERR_IDENT_UNACCEPTABLE;
  190. }
  191. }
  192. if ((u = t30_get_rx_sub_address(s->far_t30)))
  193. {
  194. printf("%c: Phase B: remote sub-address '%s'\n", ch, u);
  195. if (s->expected_rx_info.sub_address[0] && strcmp(s->expected_rx_info.sub_address, u))
  196. {
  197. printf("%c: Phase B: remote sub-address incorrect! - expected '%s'\n", ch, s->expected_rx_info.sub_address);
  198. status = T30_ERR_SUB_UNACCEPTABLE;
  199. }
  200. }
  201. else
  202. {
  203. if (s->expected_rx_info.sub_address[0])
  204. {
  205. printf("%c: Phase B: remote sub-address missing!\n", ch);
  206. status = T30_ERR_SUB_UNACCEPTABLE;
  207. }
  208. }
  209. if ((u = t30_get_rx_polled_sub_address(s->far_t30)))
  210. {
  211. printf("%c: Phase B: remote polled sub-address '%s'\n", ch, u);
  212. if (s->expected_rx_info.polled_sub_address[0] && strcmp(s->expected_rx_info.polled_sub_address, u))
  213. {
  214. printf("%c: Phase B: remote polled sub-address incorrect! - expected '%s'\n", ch, s->expected_rx_info.polled_sub_address);
  215. status = T30_ERR_PSA_UNACCEPTABLE;
  216. }
  217. }
  218. else
  219. {
  220. if (s->expected_rx_info.polled_sub_address[0])
  221. {
  222. printf("%c: Phase B: remote polled sub-address missing!\n", ch);
  223. status = T30_ERR_PSA_UNACCEPTABLE;
  224. }
  225. }
  226. if ((u = t30_get_rx_selective_polling_address(s->far_t30)))
  227. {
  228. printf("%c: Phase B: remote selective polling address '%s'\n", ch, u);
  229. if (s->expected_rx_info.selective_polling_address[0] && strcmp(s->expected_rx_info.selective_polling_address, u))
  230. {
  231. printf("%c: Phase B: remote selective polling address incorrect! - expected '%s'\n", ch, s->expected_rx_info.selective_polling_address);
  232. status = T30_ERR_SEP_UNACCEPTABLE;
  233. }
  234. }
  235. else
  236. {
  237. if (s->expected_rx_info.selective_polling_address[0])
  238. {
  239. printf("%c: Phase B: remote selective polling address missing!\n", ch);
  240. status = T30_ERR_SEP_UNACCEPTABLE;
  241. }
  242. }
  243. if ((u = t30_get_rx_sender_ident(s->far_t30)))
  244. {
  245. printf("%c: Phase B: remote sender ident '%s'\n", ch, u);
  246. if (s->expected_rx_info.sender_ident[0] && strcmp(s->expected_rx_info.sender_ident, u))
  247. {
  248. printf("%c: Phase B: remote sender ident incorrect! - expected '%s'\n", ch, s->expected_rx_info.sender_ident);
  249. status = T30_ERR_SID_UNACCEPTABLE;
  250. }
  251. }
  252. else
  253. {
  254. if (s->expected_rx_info.sender_ident[0])
  255. {
  256. printf("%c: Phase B: remote sender ident missing!\n", ch);
  257. status = T30_ERR_SID_UNACCEPTABLE;
  258. }
  259. }
  260. if ((u = t30_get_rx_password(s->far_t30)))
  261. {
  262. printf("%c: Phase B: remote password '%s'\n", ch, u);
  263. if (s->expected_rx_info.password[0] && strcmp(s->expected_rx_info.password, u))
  264. {
  265. printf("%c: Phase B: remote password incorrect! - expected '%s'\n", ch, s->expected_rx_info.password);
  266. status = T30_ERR_PWD_UNACCEPTABLE;
  267. }
  268. }
  269. else
  270. {
  271. if (s->expected_rx_info.password[0])
  272. {
  273. printf("%c: Phase B: remote password missing!\n", ch);
  274. status = T30_ERR_PWD_UNACCEPTABLE;
  275. }
  276. }
  277. printf("%c: Phase B handler on channel %c - (0x%X) %s\n", ch, ch, result, t30_frametype(result));
  278. return status;
  279. }
  280. /*- End of function --------------------------------------------------------*/
  281. static int faxtester_phase_d_handler(void *user_data, int result)
  282. {
  283. int i;
  284. int ch;
  285. faxtester_state_t *s;
  286. char tag[20];
  287. s = (faxtester_state_t *) user_data;
  288. ch = s->far_tag;
  289. i = 0;
  290. snprintf(tag, sizeof(tag), "%c: Phase D", ch);
  291. printf("%c: Phase D handler on channel %c - (0x%X) %s\n", ch, ch, result, t30_frametype(result));
  292. fax_log_page_transfer_statistics(s->far_t30, tag);
  293. fax_log_tx_parameters(s->far_t30, tag);
  294. fax_log_rx_parameters(s->far_t30, tag);
  295. if (s->use_receiver_not_ready)
  296. t30_set_receiver_not_ready(s->far_t30, 3);
  297. if (s->test_local_interrupt)
  298. {
  299. if (i == 0)
  300. {
  301. printf("%c: Initiating interrupt request\n", ch);
  302. t30_local_interrupt_request(s->far_t30, true);
  303. }
  304. else
  305. {
  306. switch (result)
  307. {
  308. case T30_PIP:
  309. case T30_PRI_MPS:
  310. case T30_PRI_EOM:
  311. case T30_PRI_EOP:
  312. printf("%c: Accepting interrupt request\n", ch);
  313. t30_local_interrupt_request(s->far_t30, true);
  314. break;
  315. case T30_PIN:
  316. break;
  317. }
  318. }
  319. }
  320. return T30_ERR_OK;
  321. }
  322. /*- End of function --------------------------------------------------------*/
  323. static void faxtester_phase_e_handler(void *user_data, int result)
  324. {
  325. int ch;
  326. faxtester_state_t *s;
  327. char tag[20];
  328. s = (faxtester_state_t *) user_data;
  329. ch = s->far_tag;
  330. snprintf(tag, sizeof(tag), "%c: Phase E", ch);
  331. printf("%c: Phase E handler on channel %c - (%d) %s\n", ch, ch, result, t30_completion_code_to_str(result));
  332. fax_log_final_transfer_statistics(s->far_t30, tag);
  333. fax_log_tx_parameters(s->far_t30, tag);
  334. fax_log_rx_parameters(s->far_t30, tag);
  335. }
  336. /*- End of function --------------------------------------------------------*/
  337. static void t30_real_time_frame_handler(void *user_data,
  338. bool incoming,
  339. const uint8_t *msg,
  340. int len)
  341. {
  342. if (msg == NULL)
  343. {
  344. }
  345. else
  346. {
  347. fprintf(stderr,
  348. "T.30: Real time frame handler - %s, %s, length = %d\n",
  349. (incoming) ? "line->T.30" : "T.30->line",
  350. t30_frametype(msg[2]),
  351. len);
  352. }
  353. }
  354. /*- End of function --------------------------------------------------------*/
  355. static int faxtester_document_handler(void *user_data, int event)
  356. {
  357. int ch;
  358. faxtester_state_t *s;
  359. t30_state_t *t;
  360. s = (faxtester_state_t *) user_data;
  361. ch = s->far_tag;
  362. t = s->far_t30;
  363. fprintf(stderr, "%c: Document handler on channel %c - event %d\n", ch, ch, event);
  364. if (s->next_tx_file[0])
  365. {
  366. t30_set_tx_file(t, s->next_tx_file, -1, -1);
  367. s->next_tx_file[0] = '\0';
  368. return true;
  369. }
  370. return false;
  371. }
  372. /*- End of function --------------------------------------------------------*/
  373. static void faxtester_real_time_frame_handler(faxtester_state_t *s,
  374. int direction,
  375. const uint8_t *msg,
  376. int len)
  377. {
  378. if (msg == NULL)
  379. {
  380. while (faxtester_next_step(s) == 0)
  381. ;
  382. /*endwhile*/
  383. }
  384. else
  385. {
  386. fprintf(stderr,
  387. "TST: Real time frame handler - %s, %s, length = %d\n",
  388. (direction) ? "line->tester" : "tester->line",
  389. t30_frametype(msg[2]),
  390. len);
  391. if (direction && msg[1] == s->awaited[1])
  392. {
  393. if ((s->awaited_len >= 0 && len != abs(s->awaited_len))
  394. ||
  395. (s->awaited_len < 0 && len < abs(s->awaited_len))
  396. ||
  397. memcmp(msg, s->awaited, abs(s->awaited_len)) != 0)
  398. {
  399. span_log_buf(&s->logging, SPAN_LOG_FLOW, "Expected", s->awaited, abs(s->awaited_len));
  400. span_log_buf(&s->logging, SPAN_LOG_FLOW, "Received", msg, len);
  401. printf("Test failed\n");
  402. exit(2);
  403. }
  404. }
  405. if (msg[1] == s->awaited[1])
  406. {
  407. while (faxtester_next_step(s) == 0)
  408. ;
  409. /*endwhile*/
  410. }
  411. }
  412. }
  413. /*- End of function --------------------------------------------------------*/
  414. void faxtester_send_hdlc_flags(faxtester_state_t *s, int flags)
  415. {
  416. hdlc_tx_flags(&s->modems.hdlc_tx, flags);
  417. }
  418. /*- End of function --------------------------------------------------------*/
  419. void faxtester_send_hdlc_msg(faxtester_state_t *s, const uint8_t *msg, int len, int crc_ok)
  420. {
  421. hdlc_tx_frame(&s->modems.hdlc_tx, msg, len);
  422. if (!crc_ok)
  423. hdlc_tx_corrupt_frame(&s->modems.hdlc_tx);
  424. }
  425. /*- End of function --------------------------------------------------------*/
  426. static void hdlc_underflow_handler(void *user_data)
  427. {
  428. faxtester_state_t *s;
  429. uint8_t buf[400];
  430. s = (faxtester_state_t *) user_data;
  431. if (s->image_buffer)
  432. {
  433. /* We are sending an ECM image */
  434. if (s->image_ptr < s->image_len)
  435. {
  436. buf[0] = 0xFF;
  437. buf[1] = 0x03;
  438. buf[2] = 0x06;
  439. buf[3] = s->image_ptr/s->ecm_frame_size;
  440. memcpy(&buf[4], &s->image_buffer[s->image_ptr], s->ecm_frame_size);
  441. hdlc_tx_frame(&s->modems.hdlc_tx, buf, 4 + s->ecm_frame_size);
  442. if (s->corrupt_crc >= 0 && s->corrupt_crc == s->image_ptr/s->ecm_frame_size)
  443. hdlc_tx_corrupt_frame(&s->modems.hdlc_tx);
  444. s->image_ptr += s->ecm_frame_size;
  445. return;
  446. }
  447. /* The actual image is over. We are sending the final RCP frames. */
  448. if (s->image_bit_ptr > 2)
  449. {
  450. s->image_bit_ptr--;
  451. buf[0] = 0xFF;
  452. buf[1] = 0x03;
  453. buf[2] = 0x86;
  454. hdlc_tx_frame(&s->modems.hdlc_tx, buf, 3);
  455. return;
  456. }
  457. /* All done. */
  458. s->image_buffer = NULL;
  459. }
  460. front_end_step_complete(s);
  461. }
  462. /*- End of function --------------------------------------------------------*/
  463. static void modem_tx_status(void *user_data, int status)
  464. {
  465. faxtester_state_t *s;
  466. s = (faxtester_state_t *) user_data;
  467. printf("Tx status is %s (%d)\n", signal_status_to_str(status), status);
  468. switch (status)
  469. {
  470. case SIG_STATUS_SHUTDOWN_COMPLETE:
  471. front_end_step_complete(s);
  472. break;
  473. }
  474. }
  475. /*- End of function --------------------------------------------------------*/
  476. static void tone_detected(void *user_data, int tone, int level, int delay)
  477. {
  478. faxtester_state_t *s;
  479. s = (faxtester_state_t *) user_data;
  480. span_log(&s->logging,
  481. SPAN_LOG_FLOW,
  482. "%s (%d) declared (%ddBm0)\n",
  483. modem_connect_tone_to_str(tone),
  484. tone,
  485. level);
  486. if (tone != MODEM_CONNECT_TONES_NONE)
  487. {
  488. s->tone_on_time = s->timer;
  489. }
  490. else
  491. {
  492. span_log(&s->logging,
  493. SPAN_LOG_FLOW,
  494. "Tone was on for %fs\n",
  495. (float) (s->timer - s->tone_on_time)/SAMPLE_RATE + 0.55);
  496. }
  497. s->tone_state = tone;
  498. if (tone == MODEM_CONNECT_TONES_NONE)
  499. front_end_step_complete(s);
  500. }
  501. /*- End of function --------------------------------------------------------*/
  502. static int non_ecm_get_bit(void *user_data)
  503. {
  504. faxtester_state_t *s;
  505. int bit;
  506. s = (faxtester_state_t *) user_data;
  507. if (s->image_bit_ptr == 0)
  508. {
  509. if (s->image_ptr >= s->image_len)
  510. {
  511. s->image_buffer = NULL;
  512. return SIG_STATUS_END_OF_DATA;
  513. }
  514. s->image_bit_ptr = 8;
  515. s->image_ptr++;
  516. }
  517. s->image_bit_ptr--;
  518. bit = (s->image_buffer[s->image_ptr] >> (7 - s->image_bit_ptr)) & 0x01;
  519. //printf("Rx bit - %d\n", bit);
  520. return bit;
  521. }
  522. /*- End of function --------------------------------------------------------*/
  523. static void faxtester_set_ecm_image_buffer(faxtester_state_t *s, int block, int frame_size, int crc_hit)
  524. {
  525. s->image_ptr = 256*frame_size*block;
  526. if (s->image_len > s->image_ptr + 256*frame_size)
  527. s->image_len = s->image_ptr + 256*frame_size;
  528. s->ecm_frame_size = frame_size;
  529. s->image_bit_ptr = 8;
  530. s->corrupt_crc = crc_hit;
  531. s->image_buffer = s->image;
  532. /* Send the first frame */
  533. hdlc_underflow_handler(s);
  534. }
  535. /*- End of function --------------------------------------------------------*/
  536. static void non_ecm_rx_status(void *user_data, int status)
  537. {
  538. faxtester_state_t *s;
  539. s = (faxtester_state_t *) user_data;
  540. span_log(&s->logging, SPAN_LOG_FLOW, "Non-ECM carrier status is %s (%d)\n", signal_status_to_str(status), status);
  541. switch (status)
  542. {
  543. case SIG_STATUS_TRAINING_FAILED:
  544. s->modems.rx_trained = false;
  545. break;
  546. case SIG_STATUS_TRAINING_SUCCEEDED:
  547. /* The modem is now trained */
  548. s->modems.rx_trained = true;
  549. break;
  550. case SIG_STATUS_CARRIER_UP:
  551. s->modems.rx_signal_present = true;
  552. break;
  553. case SIG_STATUS_CARRIER_DOWN:
  554. if (s->modems.rx_trained)
  555. faxtester_real_time_frame_handler(s, true, NULL, 0);
  556. s->modems.rx_signal_present = false;
  557. s->modems.rx_trained = false;
  558. break;
  559. }
  560. }
  561. /*- End of function --------------------------------------------------------*/
  562. static void non_ecm_put_bit(void *user_data, int bit)
  563. {
  564. if (bit < 0)
  565. {
  566. non_ecm_rx_status(user_data, bit);
  567. return;
  568. }
  569. }
  570. /*- End of function --------------------------------------------------------*/
  571. static void hdlc_rx_status(void *user_data, int status)
  572. {
  573. faxtester_state_t *s;
  574. s = (faxtester_state_t *) user_data;
  575. span_log(&s->logging, SPAN_LOG_FLOW, "HDLC carrier status is %s (%d)\n", signal_status_to_str(status), status);
  576. switch (status)
  577. {
  578. case SIG_STATUS_TRAINING_FAILED:
  579. s->modems.rx_trained = false;
  580. break;
  581. case SIG_STATUS_TRAINING_SUCCEEDED:
  582. /* The modem is now trained */
  583. s->modems.rx_trained = true;
  584. break;
  585. case SIG_STATUS_CARRIER_UP:
  586. s->modems.rx_signal_present = true;
  587. break;
  588. case SIG_STATUS_CARRIER_DOWN:
  589. s->modems.rx_signal_present = false;
  590. s->modems.rx_trained = false;
  591. break;
  592. }
  593. }
  594. /*- End of function --------------------------------------------------------*/
  595. static void hdlc_accept(void *user_data, const uint8_t *msg, int len, int ok)
  596. {
  597. faxtester_state_t *s;
  598. if (len < 0)
  599. {
  600. hdlc_rx_status(user_data, len);
  601. return;
  602. }
  603. s = (faxtester_state_t *) user_data;
  604. faxtester_real_time_frame_handler(s, true, msg, len);
  605. }
  606. /*- End of function --------------------------------------------------------*/
  607. int faxtester_rx(faxtester_state_t *s, int16_t *amp, int len)
  608. {
  609. int i;
  610. for (i = 0; i < len; i++)
  611. amp[i] = dc_restore(&s->modems.dc_restore, amp[i]);
  612. if (s->modems.rx_handler)
  613. s->modems.rx_handler(s->modems.rx_user_data, amp, len);
  614. timer_update(s, len);
  615. if (s->wait_for_silence)
  616. {
  617. if (!s->modems.rx_signal_present)
  618. {
  619. s->wait_for_silence = false;
  620. front_end_step_complete(s);
  621. }
  622. }
  623. return 0;
  624. }
  625. /*- End of function --------------------------------------------------------*/
  626. int faxtester_tx(faxtester_state_t *s, int16_t *amp, int max_len)
  627. {
  628. int len;
  629. len = 0;
  630. if (s->transmit)
  631. {
  632. while ((len += s->modems.tx_handler(s->modems.tx_user_data, amp + len, max_len - len)) < max_len)
  633. {
  634. /* Allow for a change of tx handler within a block */
  635. front_end_step_complete(s);
  636. if (!s->transmit)
  637. {
  638. if (s->modems.transmit_on_idle)
  639. {
  640. /* Pad to the requested length with silence */
  641. memset(amp + len, 0, (max_len - len)*sizeof(int16_t));
  642. len = max_len;
  643. }
  644. break;
  645. }
  646. }
  647. }
  648. else
  649. {
  650. if (s->modems.transmit_on_idle)
  651. {
  652. /* Pad to the requested length with silence */
  653. memset(amp, 0, max_len*sizeof(int16_t));
  654. len = max_len;
  655. }
  656. }
  657. return len;
  658. }
  659. /*- End of function --------------------------------------------------------*/
  660. void faxtest_set_rx_silence(faxtester_state_t *s)
  661. {
  662. s->wait_for_silence = true;
  663. }
  664. /*- End of function --------------------------------------------------------*/
  665. void faxtester_set_rx_type(void *user_data, int type, int bit_rate, int short_train, int use_hdlc)
  666. {
  667. faxtester_state_t *s;
  668. fax_modems_state_t *t;
  669. s = (faxtester_state_t *) user_data;
  670. t = &s->modems;
  671. span_log(&s->logging, SPAN_LOG_FLOW, "Set rx type %d\n", type);
  672. if (s->current_rx_type == type)
  673. return;
  674. s->current_rx_type = type;
  675. if (use_hdlc)
  676. hdlc_rx_init(&t->hdlc_rx, false, false, HDLC_FRAMING_OK_THRESHOLD, hdlc_accept, s);
  677. switch (type)
  678. {
  679. case T30_MODEM_CED:
  680. fax_modems_start_slow_modem(t, FAX_MODEM_CED_TONE_RX);
  681. s->tone_state = MODEM_CONNECT_TONES_NONE;
  682. break;
  683. case T30_MODEM_CNG:
  684. fax_modems_start_slow_modem(t, FAX_MODEM_CNG_TONE_RX);
  685. s->tone_state = MODEM_CONNECT_TONES_NONE;
  686. break;
  687. case T30_MODEM_V21:
  688. if (s->flush_handler)
  689. s->flush_handler(s, s->flush_user_data, 3);
  690. fax_modems_start_slow_modem(t, FAX_MODEM_V21_RX);
  691. break;
  692. case T30_MODEM_V27TER:
  693. fax_modems_start_fast_modem(t, FAX_MODEM_V27TER_RX, bit_rate, short_train, use_hdlc);
  694. break;
  695. case T30_MODEM_V29:
  696. fax_modems_start_fast_modem(t, FAX_MODEM_V29_RX, bit_rate, short_train, use_hdlc);
  697. break;
  698. case T30_MODEM_V17:
  699. fax_modems_start_fast_modem(t, FAX_MODEM_V17_RX, bit_rate, short_train, use_hdlc);
  700. break;
  701. case T30_MODEM_DONE:
  702. span_log(&s->logging, SPAN_LOG_FLOW, "FAX exchange complete\n");
  703. default:
  704. fax_modems_set_rx_handler(t, (span_rx_handler_t) &span_dummy_rx, s, NULL, s);
  705. break;
  706. }
  707. }
  708. /*- End of function --------------------------------------------------------*/
  709. void faxtester_set_tx_type(void *user_data, int type, int bit_rate, int short_train, int use_hdlc)
  710. {
  711. faxtester_state_t *s;
  712. get_bit_func_t get_bit_func;
  713. void *get_bit_user_data;
  714. fax_modems_state_t *t;
  715. int tone;
  716. s = (faxtester_state_t *) user_data;
  717. t = &s->modems;
  718. span_log(&s->logging, SPAN_LOG_FLOW, "Set tx type %d\n", type);
  719. if (use_hdlc)
  720. {
  721. get_bit_func = (get_bit_func_t) hdlc_tx_get_bit;
  722. get_bit_user_data = (void *) &t->hdlc_tx;
  723. }
  724. else
  725. {
  726. get_bit_func = non_ecm_get_bit;
  727. get_bit_user_data = (void *) s;
  728. }
  729. if (type == s->current_tx_type)
  730. {
  731. if (type == T30_MODEM_PAUSE)
  732. silence_gen_alter(&t->silence_gen, ms_to_samples(short_train));
  733. return;
  734. }
  735. switch (type)
  736. {
  737. case T30_MODEM_PAUSE:
  738. silence_gen_alter(&t->silence_gen, ms_to_samples(short_train));
  739. fax_modems_set_tx_handler(t, (span_tx_handler_t) &silence_gen, &t->silence_gen);
  740. s->transmit = true;
  741. break;
  742. case T30_MODEM_CED:
  743. case T30_MODEM_CNG:
  744. tone = (type == T30_MODEM_CED) ? FAX_MODEM_CED_TONE_TX : FAX_MODEM_CNG_TONE_TX;
  745. fax_modems_start_slow_modem(t, tone);
  746. s->transmit = true;
  747. break;
  748. case T30_MODEM_V21:
  749. fax_modems_start_slow_modem(t, FAX_MODEM_V21_TX);
  750. fsk_tx_set_modem_status_handler(&t->v21_tx, modem_tx_status, (void *) s);
  751. s->transmit = true;
  752. break;
  753. case T30_MODEM_V27TER:
  754. fax_modems_set_get_bit(t, get_bit_func, get_bit_user_data);
  755. fax_modems_start_fast_modem(t, FAX_MODEM_V27TER_TX, bit_rate, short_train, use_hdlc);
  756. v27ter_tx_set_modem_status_handler(&t->fast_modems.v27ter_tx, modem_tx_status, (void *) s);
  757. /* For any fast modem, set 200ms of preamble flags */
  758. hdlc_tx_flags(&t->hdlc_tx, bit_rate/(8*5));
  759. s->transmit = true;
  760. break;
  761. case T30_MODEM_V29:
  762. fax_modems_set_get_bit(t, get_bit_func, get_bit_user_data);
  763. fax_modems_start_fast_modem(t, FAX_MODEM_V29_TX, bit_rate, short_train, use_hdlc);
  764. v29_tx_set_modem_status_handler(&t->fast_modems.v29_tx, modem_tx_status, (void *) s);
  765. /* For any fast modem, set 200ms of preamble flags */
  766. hdlc_tx_flags(&t->hdlc_tx, bit_rate/(8*5));
  767. s->transmit = true;
  768. break;
  769. case T30_MODEM_V17:
  770. fax_modems_set_get_bit(t, get_bit_func, get_bit_user_data);
  771. fax_modems_start_fast_modem(t, FAX_MODEM_V17_TX, bit_rate, short_train, use_hdlc);
  772. v17_tx_set_modem_status_handler(&t->fast_modems.v17_tx, modem_tx_status, (void *) s);
  773. /* For any fast modem, set 200ms of preamble flags */
  774. hdlc_tx_flags(&t->hdlc_tx, bit_rate/(8*5));
  775. s->transmit = true;
  776. break;
  777. case T30_MODEM_DONE:
  778. span_log(&s->logging, SPAN_LOG_FLOW, "FAX exchange complete\n");
  779. /* Fall through */
  780. default:
  781. silence_gen_alter(&t->silence_gen, 0);
  782. fax_modems_set_tx_handler(t, (span_tx_handler_t) &silence_gen, &t->silence_gen);
  783. s->transmit = false;
  784. break;
  785. }
  786. s->current_tx_type = type;
  787. }
  788. /*- End of function --------------------------------------------------------*/
  789. void faxtester_set_timeout(faxtester_state_t *s, int timeout)
  790. {
  791. if (timeout >= 0)
  792. s->timeout = s->timer + timeout*SAMPLE_RATE/1000;
  793. else
  794. s->timeout = 0x7FFFFFFFFFFFFFFFLL;
  795. }
  796. /*- End of function --------------------------------------------------------*/
  797. void faxtester_set_transmit_on_idle(faxtester_state_t *s, int transmit_on_idle)
  798. {
  799. s->modems.transmit_on_idle = transmit_on_idle;
  800. }
  801. /*- End of function --------------------------------------------------------*/
  802. void faxtester_set_tep_mode(faxtester_state_t *s, int use_tep)
  803. {
  804. fax_modems_set_tep_mode(&s->modems, use_tep);
  805. }
  806. /*- End of function --------------------------------------------------------*/
  807. static void corrupt_image(faxtester_state_t *s, const char *bad_rows)
  808. {
  809. int i;
  810. int j;
  811. int k;
  812. uint32_t bits;
  813. uint32_t bitsx;
  814. int list[1000];
  815. int x;
  816. int row;
  817. const char *t;
  818. /* Form the list of rows to be hit */
  819. x = 0;
  820. t = bad_rows;
  821. while (*t)
  822. {
  823. while (isspace((int) *t))
  824. t++;
  825. if (sscanf(t, "%d", &list[x]) < 1)
  826. break;
  827. x++;
  828. while (isdigit((int) *t))
  829. t++;
  830. if (*t == ',')
  831. t++;
  832. }
  833. /* Go through the image, and corrupt the first bit of every listed row */
  834. bits = 0x7FF;
  835. bitsx = 0x7FF;
  836. row = 0;
  837. for (i = 0; i < s->image_len; i++)
  838. {
  839. bits ^= (s->image[i] << 11);
  840. bitsx ^= (s->image[i] << 11);
  841. for (j = 0; j < 8; j++)
  842. {
  843. if ((bits & 0xFFF) == 0x800)
  844. {
  845. /* We are at an EOL. Is this row in the list of rows to be corrupted? */
  846. row++;
  847. for (k = 0; k < x; k++)
  848. {
  849. if (list[k] == row)
  850. {
  851. /* Corrupt this row. TSB85 says to hit the first bit after the EOL */
  852. bitsx ^= 0x1000;
  853. }
  854. }
  855. }
  856. bits >>= 1;
  857. bitsx >>= 1;
  858. }
  859. s->image[i] = (bitsx >> 3) & 0xFF;
  860. }
  861. span_log(&s->logging, SPAN_LOG_FLOW, "%d rows found. %d corrupted\n", row, x);
  862. }
  863. /*- End of function --------------------------------------------------------*/
  864. static int string_to_msg(uint8_t msg[], uint8_t mask[], const char buf[])
  865. {
  866. int i;
  867. int x;
  868. const char *t;
  869. msg[0] = 0;
  870. mask[0] = 0xFF;
  871. i = 0;
  872. t = (char *) buf;
  873. while (*t)
  874. {
  875. /* Skip white space */
  876. while (isspace((int) *t))
  877. t++;
  878. /* If we find ... we allow arbitrary additional info beyond this point in the message */
  879. if (t[0] == '.' && t[1] == '.' && t[2] == '.')
  880. {
  881. return -i;
  882. }
  883. else if (isxdigit((int) *t))
  884. {
  885. for ( ; isxdigit((int) *t); t++)
  886. {
  887. x = *t;
  888. if (x >= 'a')
  889. x -= 0x20;
  890. if (x >= 'A')
  891. x -= ('A' - 10);
  892. else
  893. x -= '0';
  894. msg[i] = (msg[i] << 4) | x;
  895. }
  896. mask[i] = 0xFF;
  897. if (*t == '/')
  898. {
  899. /* There is a mask following the byte */
  900. mask[i] = 0;
  901. for (t++; isxdigit((int) *t); t++)
  902. {
  903. x = *t;
  904. if (x >= 'a')
  905. x -= 0x20;
  906. if (x >= 'A')
  907. x -= ('A' - 10);
  908. else
  909. x -= '0';
  910. mask[i] = (mask[i] << 4) | x;
  911. }
  912. }
  913. if (*t && !isspace((int) *t))
  914. {
  915. /* Bad string */
  916. return 0;
  917. }
  918. i++;
  919. }
  920. }
  921. return i;
  922. }
  923. /*- End of function --------------------------------------------------------*/
  924. void faxtester_set_flush_handler(faxtester_state_t *s, faxtester_flush_handler_t handler, void *user_data)
  925. {
  926. s->flush_handler = handler;
  927. s->flush_user_data = user_data;
  928. }
  929. /*- End of function --------------------------------------------------------*/
  930. static void fax_prepare(faxtester_state_t *s)
  931. {
  932. if (s->far_fax)
  933. {
  934. fax_set_transmit_on_idle(s->far_fax, true);
  935. fax_set_tep_mode(s->far_fax, true);
  936. }
  937. #if 0
  938. t30_set_tx_ident(s->far_t30, "1234567890");
  939. t30_set_tx_sub_address(s->far_t30, "Sub-address");
  940. t30_set_tx_sender_ident(s->far_t30, "Sender ID");
  941. t30_set_tx_password(s->far_t30, "Password");
  942. t30_set_tx_polled_sub_address(s->far_t30, "Polled sub-address");
  943. t30_set_tx_selective_polling_address(s->far_t30, "Sel polling address");
  944. #endif
  945. t30_set_tx_nsf(s->far_t30, (const uint8_t *) "\x50\x00\x00\x00Spandsp NSF\x00", 16);
  946. //t30_set_tx_nss(s->far_t30, (const uint8_t *) "\x50\x00\x00\x00Spandsp NSS\x00", 16);
  947. t30_set_tx_nsc(s->far_t30, (const uint8_t *) "\x50\x00\x00\x00Spandsp NSC\x00", 16);
  948. t30_set_ecm_capability(s->far_t30, true);
  949. t30_set_supported_t30_features(s->far_t30,
  950. T30_SUPPORT_IDENTIFICATION
  951. | T30_SUPPORT_SELECTIVE_POLLING
  952. | T30_SUPPORT_SUB_ADDRESSING);
  953. t30_set_supported_image_sizes(s->far_t30,
  954. T4_SUPPORT_WIDTH_215MM
  955. | T4_SUPPORT_WIDTH_255MM
  956. | T4_SUPPORT_WIDTH_303MM
  957. | T4_SUPPORT_LENGTH_US_LETTER
  958. | T4_SUPPORT_LENGTH_US_LEGAL
  959. | T4_SUPPORT_LENGTH_UNLIMITED);
  960. t30_set_supported_bilevel_resolutions(s->far_t30,
  961. T4_RESOLUTION_R8_STANDARD
  962. | T4_RESOLUTION_R8_FINE
  963. | T4_RESOLUTION_R8_SUPERFINE
  964. | T4_RESOLUTION_R16_SUPERFINE
  965. | T4_RESOLUTION_100_100
  966. | T4_RESOLUTION_200_100
  967. | T4_RESOLUTION_200_200
  968. | T4_RESOLUTION_200_400
  969. | T4_RESOLUTION_300_300
  970. | T4_RESOLUTION_300_600
  971. | T4_RESOLUTION_400_400
  972. | T4_RESOLUTION_400_800
  973. | T4_RESOLUTION_600_600
  974. | T4_RESOLUTION_600_1200
  975. | T4_RESOLUTION_1200_1200);
  976. t30_set_supported_colour_resolutions(s->far_t30, 0);
  977. t30_set_supported_modems(s->far_t30, T30_SUPPORT_V27TER | T30_SUPPORT_V29 | T30_SUPPORT_V17);
  978. t30_set_supported_compressions(s->far_t30, T4_COMPRESSION_T4_1D | T4_COMPRESSION_T4_2D | T4_COMPRESSION_T6);
  979. t30_set_phase_b_handler(s->far_t30, faxtester_phase_b_handler, (void *) s);
  980. t30_set_phase_d_handler(s->far_t30, faxtester_phase_d_handler, (void *) s);
  981. t30_set_phase_e_handler(s->far_t30, faxtester_phase_e_handler, (void *) s);
  982. t30_set_real_time_frame_handler(s->far_t30, t30_real_time_frame_handler, (void *) s);
  983. t30_set_document_handler(s->far_t30, faxtester_document_handler, (void *) s);
  984. }
  985. /*- End of function --------------------------------------------------------*/
  986. static void get_node_parms(struct xml_node_parms_s *parms, xmlNodePtr node)
  987. {
  988. parms->dir = xmlGetProp(node, (const xmlChar *) "dir");
  989. parms->type = xmlGetProp(node, (const xmlChar *) "type");
  990. parms->modem = xmlGetProp(node, (const xmlChar *) "modem");
  991. parms->value = xmlGetProp(node, (const xmlChar *) "value");
  992. parms->tag = xmlGetProp(node, (const xmlChar *) "tag");
  993. parms->bad_rows = xmlGetProp(node, (const xmlChar *) "bad_rows");
  994. parms->crc_error = xmlGetProp(node, (const xmlChar *) "crc_error");
  995. parms->pattern = xmlGetProp(node, (const xmlChar *) "pattern");
  996. parms->timein = xmlGetProp(node, (const xmlChar *) "timein");
  997. parms->timeout = xmlGetProp(node, (const xmlChar *) "timeout");
  998. parms->min_bits = xmlGetProp(node, (const xmlChar *) "min_bits");
  999. parms->frame_size = xmlGetProp(node, (const xmlChar *) "frame_size");
  1000. parms->block = xmlGetProp(node, (const xmlChar *) "block");
  1001. parms->compression = xmlGetProp(node, (const xmlChar *) "compression");
  1002. }
  1003. /*- End of function --------------------------------------------------------*/
  1004. static void free_node_parms(struct xml_node_parms_s *parms)
  1005. {
  1006. if (parms->dir)
  1007. xmlFree(parms->dir);
  1008. if (parms->type)
  1009. xmlFree(parms->type);
  1010. if (parms->modem)
  1011. xmlFree(parms->modem);
  1012. if (parms->value)
  1013. xmlFree(parms->value);
  1014. if (parms->tag)
  1015. xmlFree(parms->tag);
  1016. if (parms->bad_rows)
  1017. xmlFree(parms->bad_rows);
  1018. if (parms->crc_error)
  1019. xmlFree(parms->crc_error);
  1020. if (parms->pattern)
  1021. xmlFree(parms->pattern);
  1022. if (parms->timein)
  1023. xmlFree(parms->timein);
  1024. if (parms->timeout)
  1025. xmlFree(parms->timeout);
  1026. if (parms->min_bits)
  1027. xmlFree(parms->min_bits);
  1028. if (parms->frame_size)
  1029. xmlFree(parms->frame_size);
  1030. if (parms->block)
  1031. xmlFree(parms->block);
  1032. if (parms->compression)
  1033. xmlFree(parms->compression);
  1034. }
  1035. /*- End of function --------------------------------------------------------*/
  1036. SPAN_DECLARE(int) faxtester_next_step(faxtester_state_t *s)
  1037. {
  1038. int delay;
  1039. int flags;
  1040. struct xml_node_parms_s parms;
  1041. uint8_t buf[1000];
  1042. uint8_t mask[1000];
  1043. char path[1024];
  1044. int i;
  1045. int j;
  1046. int hdlc;
  1047. int short_train;
  1048. int min_row_bits;
  1049. int ecm_frame_size;
  1050. int ecm_block;
  1051. int compression_type;
  1052. xmlChar *min;
  1053. xmlChar *max;
  1054. t4_tx_state_t t4_tx_state;
  1055. t30_stats_t t30_stats;
  1056. s->test_for_call_clear = false;
  1057. if (s->cur == NULL)
  1058. {
  1059. if (!s->final_delayed)
  1060. {
  1061. /* Add a bit of waiting at the end, to ensure everything gets flushed through,
  1062. any timers can expire, etc. */
  1063. faxtester_set_timeout(s, -1);
  1064. faxtester_set_rx_type(s, T30_MODEM_NONE, 0, false, false);
  1065. faxtester_set_tx_type(s, T30_MODEM_PAUSE, 0, 120000, false);
  1066. s->final_delayed = true;
  1067. return 1;
  1068. }
  1069. /* Finished */
  1070. printf("Test passed\n");
  1071. exit(0);
  1072. }
  1073. for (;;)
  1074. {
  1075. if (s->cur == NULL)
  1076. {
  1077. if (s->repeat_parent == NULL)
  1078. {
  1079. /* Finished */
  1080. printf("Test passed\n");
  1081. exit(0);
  1082. }
  1083. if (++s->repeat_count > s->repeat_max)
  1084. {
  1085. /* Finished */
  1086. printf("Too many repeats\n");
  1087. printf("Test failed\n");
  1088. exit(0);
  1089. }
  1090. if (s->repeat_count < s->repeat_min)
  1091. {
  1092. s->cur = s->repeat_start;
  1093. }
  1094. else
  1095. {
  1096. s->cur = s->repeat_parent->next;
  1097. s->repeat_parent = NULL;
  1098. }
  1099. }
  1100. if (xmlStrcmp(s->cur->name, (const xmlChar *) "step") == 0)
  1101. {
  1102. break;
  1103. }
  1104. if (s->repeat_parent == NULL && xmlStrcmp(s->cur->name, (const xmlChar *) "repeat") == 0)
  1105. {
  1106. min = xmlGetProp(s->cur, (const xmlChar *) "min");
  1107. max = xmlGetProp(s->cur, (const xmlChar *) "max");
  1108. s->repeat_min = min ? atoi((const char *) min) : 0;
  1109. s->repeat_max = max ? atoi((const char *) max) : INT_MAX;
  1110. s->repeat_count = 0;
  1111. if (min)
  1112. xmlFree(min);
  1113. if (max)
  1114. xmlFree(max);
  1115. if (s->repeat_min > 0)
  1116. {
  1117. s->repeat_parent = s->cur;
  1118. s->repeat_start =
  1119. s->cur = s->cur->xmlChildrenNode;
  1120. continue;
  1121. }
  1122. }
  1123. s->cur = s->cur->next;
  1124. }
  1125. get_node_parms(&parms, s->cur);
  1126. s->cur = s->cur->next;
  1127. span_log(&s->logging,
  1128. SPAN_LOG_FLOW,
  1129. "Dir - %s, type - %s, modem - %s, value - %s, timein - %s, timeout - %s, tag - %s\n",
  1130. (parms.dir) ? (const char *) parms.dir : " ",
  1131. (parms.type) ? (const char *) parms.type : "",
  1132. (parms.modem) ? (const char *) parms.modem : "",
  1133. (parms.value) ? (const char *) parms.value : "",
  1134. (parms.timein) ? (const char *) parms.timein : "",
  1135. (parms.timeout) ? (const char *) parms.timeout : "",
  1136. (parms.tag) ? (const char *) parms.tag : "");
  1137. if (parms.type == NULL)
  1138. {
  1139. free_node_parms(&parms);
  1140. return 1;
  1141. }
  1142. s->timein_x = (parms.timein) ? atoi((const char *) parms.timein) : -1;
  1143. s->timeout_x = (parms.timeout) ? atoi((const char *) parms.timeout) : -1;
  1144. if (parms.dir && strcasecmp((const char *) parms.dir, "R") == 0)
  1145. {
  1146. /* Receive always has a timeout applied. */
  1147. if (s->timeout_x < 0)
  1148. s->timeout_x = 7000;
  1149. faxtester_set_timeout(s, s->timeout_x);
  1150. if (parms.modem)
  1151. {
  1152. hdlc = (strcasecmp((const char *) parms.type, "PREAMBLE") == 0);
  1153. short_train = (strcasecmp((const char *) parms.type, "TCF") != 0);
  1154. faxtester_set_tx_type(s, T30_MODEM_NONE, 0, false, false);
  1155. if (strcasecmp((const char *) parms.modem, "V.21") == 0)
  1156. {
  1157. faxtester_set_rx_type(s, T30_MODEM_V21, 300, false, true);
  1158. }
  1159. else if (strcasecmp((const char *) parms.modem, "V.17/14400") == 0)
  1160. {
  1161. faxtester_set_rx_type(s, T30_MODEM_V17, 14400, short_train, hdlc);
  1162. }
  1163. else if (strcasecmp((const char *) parms.modem, "V.17/12000") == 0)
  1164. {
  1165. faxtester_set_rx_type(s, T30_MODEM_V17, 12000, short_train, hdlc);
  1166. }
  1167. else if (strcasecmp((const char *) parms.modem, "V.17/9600") == 0)
  1168. {
  1169. faxtester_set_rx_type(s, T30_MODEM_V17, 9600, short_train, hdlc);
  1170. }
  1171. else if (strcasecmp((const char *) parms.modem, "V.17/7200") == 0)
  1172. {
  1173. faxtester_set_rx_type(s, T30_MODEM_V17, 7200, short_train, hdlc);
  1174. }
  1175. else if (strcasecmp((const char *) parms.modem, "V.29/9600") == 0)
  1176. {
  1177. faxtester_set_rx_type(s, T30_MODEM_V29, 9600, false, hdlc);
  1178. }
  1179. else if (strcasecmp((const char *) parms.modem, "V.29/7200") == 0)
  1180. {
  1181. faxtester_set_rx_type(s, T30_MODEM_V29, 7200, false, hdlc);
  1182. }
  1183. else if (strcasecmp((const char *) parms.modem, "V.27ter/4800") == 0)
  1184. {
  1185. faxtester_set_rx_type(s, T30_MODEM_V27TER, 4800, false, hdlc);
  1186. }
  1187. else if (strcasecmp((const char *) parms.modem, "V.27ter/2400") == 0)
  1188. {
  1189. faxtester_set_rx_type(s, T30_MODEM_V27TER, 2400, false, hdlc);
  1190. }
  1191. else
  1192. {
  1193. span_log(&s->logging, SPAN_LOG_FLOW, "Unrecognised modem\n");
  1194. }
  1195. }
  1196. if (strcasecmp((const char *) parms.type, "SET") == 0)
  1197. {
  1198. if (strcasecmp((const char *) parms.tag, "IDENT") == 0)
  1199. strcpy(s->expected_rx_info.ident, (const char *) parms.value);
  1200. else if (strcasecmp((const char *) parms.tag, "SUB") == 0)
  1201. strcpy(s->expected_rx_info.sub_address, (const char *) parms.value);
  1202. else if (strcasecmp((const char *) parms.tag, "SEP") == 0)
  1203. strcpy(s->expected_rx_info.selective_polling_address, (const char *) parms.value);
  1204. else if (strcasecmp((const char *) parms.tag, "PSA") == 0)
  1205. strcpy(s->expected_rx_info.polled_sub_address, (const char *) parms.value);
  1206. else if (strcasecmp((const char *) parms.tag, "SID") == 0)
  1207. strcpy(s->expected_rx_info.sender_ident, (const char *) parms.value);
  1208. else if (strcasecmp((const char *) parms.tag, "PWD") == 0)
  1209. strcpy(s->expected_rx_info.password, (const char *) parms.value);
  1210. free_node_parms(&parms);
  1211. return 0;
  1212. }
  1213. else if (strcasecmp((const char *) parms.type, "CNG") == 0)
  1214. {
  1215. /* Look for CNG */
  1216. faxtester_set_rx_type(s, T30_MODEM_CNG, 0, false, false);
  1217. faxtester_set_tx_type(s, T30_MODEM_NONE, 0, false, false);
  1218. }
  1219. else if (strcasecmp((const char *) parms.type, "CED") == 0)
  1220. {
  1221. /* Look for CED */
  1222. faxtester_set_rx_type(s, T30_MODEM_CED, 0, false, false);
  1223. faxtester_set_tx_type(s, T30_MODEM_NONE, 0, false, false);
  1224. }
  1225. else if (strcasecmp((const char *) parms.type, "HDLC") == 0)
  1226. {
  1227. i = string_to_msg(buf, mask, (const char *) parms.value);
  1228. bit_reverse(s->awaited, buf, abs(i));
  1229. s->awaited_len = i;
  1230. }
  1231. else if (strcasecmp((const char *) parms.type, "TCF") == 0)
  1232. {
  1233. }
  1234. else if (strcasecmp((const char *) parms.type, "MSG") == 0)
  1235. {
  1236. }
  1237. else if (strcasecmp((const char *) parms.type, "PP") == 0)
  1238. {
  1239. }
  1240. else if (strcasecmp((const char *) parms.type, "SILENCE") == 0)
  1241. {
  1242. faxtest_set_rx_silence(s);
  1243. }
  1244. else if (strcasecmp((const char *) parms.type, "CLEAR") == 0)
  1245. {
  1246. span_log(&s->logging, SPAN_LOG_FLOW, "Far end should drop the call\n");
  1247. faxtester_set_rx_type(s, T30_MODEM_NONE, 0, false, false);
  1248. faxtester_set_tx_type(s, T30_MODEM_PAUSE, 0, s->timeout_x, false);
  1249. s->test_for_call_clear = true;
  1250. s->call_clear_timer = 0;
  1251. }
  1252. else
  1253. {
  1254. span_log(&s->logging, SPAN_LOG_FLOW, "Unrecognised type '%s'\n", (const char *) parms.type);
  1255. free_node_parms(&parms);
  1256. return 0;
  1257. }
  1258. }
  1259. else
  1260. {
  1261. faxtester_set_timeout(s, s->timeout_x);
  1262. if (parms.modem)
  1263. {
  1264. hdlc = (strcasecmp((const char *) parms.type, "PREAMBLE") == 0);
  1265. short_train = (strcasecmp((const char *) parms.type, "TCF") != 0);
  1266. faxtester_set_rx_type(s, T30_MODEM_NONE, 0, false, false);
  1267. if (strcasecmp((const char *) parms.modem, "V.21") == 0)
  1268. {
  1269. faxtester_set_tx_type(s, T30_MODEM_V21, 300, false, true);
  1270. }
  1271. else if (strcasecmp((const char *) parms.modem, "V.17/14400") == 0)
  1272. {
  1273. faxtester_set_tx_type(s, T30_MODEM_V17, 14400, short_train, hdlc);
  1274. }
  1275. else if (strcasecmp((const char *) parms.modem, "V.17/12000") == 0)
  1276. {
  1277. faxtester_set_tx_type(s, T30_MODEM_V17, 12000, short_train, hdlc);
  1278. }
  1279. else if (strcasecmp((const char *) parms.modem, "V.17/9600") == 0)
  1280. {
  1281. faxtester_set_tx_type(s, T30_MODEM_V17, 9600, short_train, hdlc);
  1282. }
  1283. else if (strcasecmp((const char *) parms.modem, "V.17/7200") == 0)
  1284. {
  1285. faxtester_set_tx_type(s, T30_MODEM_V17, 7200, short_train, hdlc);
  1286. }
  1287. else if (strcasecmp((const char *) parms.modem, "V.29/9600") == 0)
  1288. {
  1289. faxtester_set_tx_type(s, T30_MODEM_V29, 9600, false, hdlc);
  1290. }
  1291. else if (strcasecmp((const char *) parms.modem, "V.29/7200") == 0)
  1292. {
  1293. faxtester_set_tx_type(s, T30_MODEM_V29, 7200, false, hdlc);
  1294. }
  1295. else if (strcasecmp((const char *) parms.modem, "V.27ter/4800") == 0)
  1296. {
  1297. faxtester_set_tx_type(s, T30_MODEM_V27TER, 4800, false, hdlc);
  1298. }
  1299. else if (strcasecmp((const char *) parms.modem, "V.27ter/2400") == 0)
  1300. {
  1301. faxtester_set_tx_type(s, T30_MODEM_V27TER, 2400, false, hdlc);
  1302. }
  1303. else
  1304. {
  1305. span_log(&s->logging, SPAN_LOG_FLOW, "Unrecognised modem\n");
  1306. }
  1307. }
  1308. if (strcasecmp((const char *) parms.type, "SET") == 0)
  1309. {
  1310. if (strcasecmp((const char *) parms.tag, "IDENT") == 0)
  1311. t30_set_tx_ident(s->far_t30, (const char *) parms.value);
  1312. else if (strcasecmp((const char *) parms.tag, "SUB") == 0)
  1313. t30_set_tx_sub_address(s->far_t30, (const char *) parms.value);
  1314. else if (strcasecmp((const char *) parms.tag, "SEP") == 0)
  1315. t30_set_tx_selective_polling_address(s->far_t30, (const char *) parms.value);
  1316. else if (strcasecmp((const char *) parms.tag, "PSA") == 0)
  1317. t30_set_tx_polled_sub_address(s->far_t30, (const char *) parms.value);
  1318. else if (strcasecmp((const char *) parms.tag, "SID") == 0)
  1319. t30_set_tx_sender_ident(s->far_t30, (const char *) parms.value);
  1320. else if (strcasecmp((const char *) parms.tag, "PWD") == 0)
  1321. t30_set_tx_password(s->far_t30, (const char *) parms.value);
  1322. else if (strcasecmp((const char *) parms.tag, "RXFILE") == 0)
  1323. {
  1324. if (parms.value)
  1325. t30_set_rx_file(s->far_t30, (const char *) parms.value, -1);
  1326. else
  1327. t30_set_rx_file(s->far_t30, output_tiff_file_name, -1);
  1328. }
  1329. else if (strcasecmp((const char *) parms.tag, "TXFILE") == 0)
  1330. {
  1331. sprintf(s->next_tx_file, "%s/%s", s->image_path, (const char *) parms.value);
  1332. printf("Push '%s'\n", s->next_tx_file);
  1333. }
  1334. free_node_parms(&parms);
  1335. return 0;
  1336. }
  1337. else if (strcasecmp((const char *) parms.type, "CALL") == 0)
  1338. {
  1339. if (s->far_fax)
  1340. fax_restart(s->far_fax, false);
  1341. else
  1342. t38_terminal_restart(s->far_t38, false);
  1343. fax_prepare(s);
  1344. s->next_tx_file[0] = '\0';
  1345. t30_set_rx_file(s->far_t30, output_tiff_file_name, -1);
  1346. /* Avoid libtiff 3.8.2 and earlier bug on complex 2D lines. */
  1347. t30_set_supported_output_compressions(s->far_t30, T4_COMPRESSION_T4_1D);
  1348. if (parms.value)
  1349. {
  1350. sprintf(path, "%s/%s", s->image_path, (const char *) parms.value);
  1351. t30_set_tx_file(s->far_t30, path, -1, -1);
  1352. }
  1353. free_node_parms(&parms);
  1354. return 0;
  1355. }
  1356. else if (strcasecmp((const char *) parms.type, "ANSWER") == 0)
  1357. {
  1358. if (s->far_fax)
  1359. fax_restart(s->far_fax, true);
  1360. else
  1361. t38_terminal_restart(s->far_t38, true);
  1362. fax_prepare(s);
  1363. s->next_tx_file[0] = '\0';
  1364. /* Avoid libtiff 3.8.2 and earlier bug on complex 2D lines. */
  1365. t30_set_supported_output_compressions(s->far_t30, T4_COMPRESSION_T4_1D);
  1366. if (parms.value)
  1367. {
  1368. sprintf(path, "%s/%s", s->image_path, (const char *) parms.value);
  1369. t30_set_tx_file(s->far_t30, path, -1, -1);
  1370. }
  1371. free_node_parms(&parms);
  1372. return 0;
  1373. }
  1374. else if (strcasecmp((const char *) parms.type, "CNG") == 0)
  1375. {
  1376. faxtester_set_rx_type(s, T30_MODEM_NONE, 0, false, false);
  1377. faxtester_set_tx_type(s, T30_MODEM_CNG, 0, false, false);
  1378. }
  1379. else if (strcasecmp((const char *) parms.type, "CED") == 0)
  1380. {
  1381. faxtester_set_rx_type(s, T30_MODEM_NONE, 0, false, false);
  1382. faxtester_set_tx_type(s, T30_MODEM_CED, 0, false, false);
  1383. }
  1384. else if (strcasecmp((const char *) parms.type, "WAIT") == 0)
  1385. {
  1386. delay = (parms.value) ? atoi((const char *) parms.value) : 1;
  1387. faxtester_set_rx_type(s, T30_MODEM_NONE, 0, false, false);
  1388. faxtester_set_tx_type(s, T30_MODEM_PAUSE, 0, delay, false);
  1389. }
  1390. else if (strcasecmp((const char *) parms.type, "PREAMBLE") == 0)
  1391. {
  1392. flags = (parms.value) ? atoi((const char *) parms.value) : 37;
  1393. faxtester_send_hdlc_flags(s, flags);
  1394. }
  1395. else if (strcasecmp((const char *) parms.type, "POSTAMBLE") == 0)
  1396. {
  1397. flags = (parms.value) ? atoi((const char *) parms.value) : 5;
  1398. faxtester_send_hdlc_flags(s, flags);
  1399. }
  1400. else if (strcasecmp((const char *) parms.type, "HDLC") == 0)
  1401. {
  1402. i = string_to_msg(buf, mask, (const char *) parms.value);
  1403. bit_reverse(buf, buf, abs(i));
  1404. if (parms.crc_error && strcasecmp((const char *) parms.crc_error, "0") == 0)
  1405. faxtester_send_hdlc_msg(s, buf, abs(i), false);
  1406. else
  1407. faxtester_send_hdlc_msg(s, buf, abs(i), true);
  1408. }
  1409. else if (strcasecmp((const char *) parms.type, "TCF") == 0)
  1410. {
  1411. i = (parms.value) ? atoi((const char *) parms.value) : 450;
  1412. if (parms.pattern)
  1413. {
  1414. /* TODO: implement proper patterns */
  1415. j = atoi((const char *) parms.pattern);
  1416. memset(s->image, 0x55, j);
  1417. if (i > j)
  1418. memset(s->image + j, 0, i - j);
  1419. }
  1420. else
  1421. {
  1422. memset(s->image, 0, i);
  1423. }
  1424. s->image_ptr = 0;
  1425. s->image_bit_ptr = 8;
  1426. s->image_buffer = s->image;
  1427. s->image_len = i;
  1428. }
  1429. else if (strcasecmp((const char *) parms.type, "MSG") == 0)
  1430. {
  1431. /* A non-ECM page */
  1432. min_row_bits = (parms.min_bits) ? atoi((const char *) parms.min_bits) : 0;
  1433. sprintf(path, "%s/%s", s->image_path, (const char *) parms.value);
  1434. if (t4_tx_init(&t4_tx_state, path, -1, -1) == NULL)
  1435. {
  1436. span_log(&s->logging, SPAN_LOG_FLOW, "Failed to init T.4 send\n");
  1437. printf("Test failed\n");
  1438. exit(2);
  1439. }
  1440. t4_tx_set_header_info(&t4_tx_state, NULL);
  1441. compression_type = T4_COMPRESSION_T4_1D;
  1442. if (parms.compression)
  1443. {
  1444. if (strcasecmp((const char *) parms.compression, "T.4 1D") == 0)
  1445. compression_type = T4_COMPRESSION_T4_1D;
  1446. else if (strcasecmp((const char *) parms.compression, "T.4 2D") == 0)
  1447. compression_type = T4_COMPRESSION_T4_2D;
  1448. else if (strcasecmp((const char *) parms.compression, "T.6") == 0)
  1449. compression_type = T4_COMPRESSION_T6;
  1450. else if (strcasecmp((const char *) parms.compression, "T.85") == 0)
  1451. compression_type = T4_COMPRESSION_T85;
  1452. }
  1453. if (t4_tx_set_tx_image_format(&t4_tx_state,
  1454. compression_type,
  1455. T4_SUPPORT_WIDTH_215MM
  1456. | T4_SUPPORT_LENGTH_US_LETTER
  1457. | T4_SUPPORT_LENGTH_US_LEGAL
  1458. | T4_SUPPORT_LENGTH_UNLIMITED,
  1459. T4_RESOLUTION_R8_STANDARD
  1460. | T4_RESOLUTION_R8_FINE
  1461. | T4_RESOLUTION_R8_SUPERFINE
  1462. | T4_RESOLUTION_R16_SUPERFINE
  1463. | T4_RESOLUTION_200_100
  1464. | T4_RESOLUTION_200_200
  1465. | T4_RESOLUTION_200_400
  1466. | T4_RESOLUTION_300_300
  1467. | T4_RESOLUTION_300_600
  1468. | T4_RESOLUTION_400_400
  1469. | T4_RESOLUTION_400_800
  1470. | T4_RESOLUTION_600_600
  1471. | T4_RESOLUTION_600_1200
  1472. | T4_RESOLUTION_1200_1200,
  1473. T4_RESOLUTION_100_100
  1474. | T4_RESOLUTION_200_200
  1475. | T4_RESOLUTION_300_300
  1476. | T4_RESOLUTION_400_400
  1477. | T4_RESOLUTION_600_600
  1478. | T4_RESOLUTION_1200_1200) < 0)
  1479. {
  1480. span_log(&s->logging, SPAN_LOG_FLOW, "Failed to set T.4 compression\n");
  1481. printf("Test failed\n");
  1482. exit(2);
  1483. }
  1484. t4_tx_set_min_bits_per_row(&t4_tx_state, min_row_bits);
  1485. if (t4_tx_start_page(&t4_tx_state))
  1486. {
  1487. span_log(&s->logging, SPAN_LOG_FLOW, "Failed to start T.4 send\n");
  1488. printf("Test failed\n");
  1489. exit(2);
  1490. }
  1491. s->image_len = t4_tx_get(&t4_tx_state, s->image, sizeof(s->image));
  1492. if (parms.bad_rows)
  1493. {
  1494. span_log(&s->logging, SPAN_LOG_FLOW, "We need to corrupt the image\n");
  1495. corrupt_image(s, (const char *) parms.bad_rows);
  1496. }
  1497. t4_tx_release(&t4_tx_state);
  1498. span_log(&s->logging, SPAN_LOG_FLOW, "Non-ECM image is %d bytes (min row bits %d)\n", s->image_len, min_row_bits);
  1499. s->image_ptr = 0;
  1500. s->image_bit_ptr = 8;
  1501. s->image_buffer = s->image;
  1502. }
  1503. else if (strcasecmp((const char *) parms.type, "PP") == 0)
  1504. {
  1505. min_row_bits = (parms.min_bits) ? atoi((const char *) parms.min_bits) : 0;
  1506. ecm_block = (parms.block) ? atoi((const char *) parms.block) : 0;
  1507. ecm_frame_size = (parms.frame_size) ? atoi((const char *) parms.frame_size) : 64;
  1508. i = (parms.crc_error) ? atoi((const char *) parms.crc_error) : -1;
  1509. sprintf(path, "%s/%s", s->image_path, (const char *) parms.value);
  1510. if (t4_tx_init(&t4_tx_state, path, -1, -1) == NULL)
  1511. {
  1512. span_log(&s->logging, SPAN_LOG_FLOW, "Failed to init T.4 send\n");
  1513. printf("Test failed\n");
  1514. exit(2);
  1515. }
  1516. t4_tx_set_header_info(&t4_tx_state, NULL);
  1517. compression_type = T4_COMPRESSION_T4_1D;
  1518. if (parms.compression)
  1519. {
  1520. if (strcasecmp((const char *) parms.compression, "T.4 1D") == 0)
  1521. compression_type = T4_COMPRESSION_T4_1D;
  1522. else if (strcasecmp((const char *) parms.compression, "T.4 2D") == 0)
  1523. compression_type = T4_COMPRESSION_T4_2D;
  1524. else if (strcasecmp((const char *) parms.compression, "T.6") == 0)
  1525. compression_type = T4_COMPRESSION_T6;
  1526. else if (strcasecmp((const char *) parms.compression, "T.85") == 0)
  1527. compression_type = T4_COMPRESSION_T85;
  1528. }
  1529. if (t4_tx_set_tx_image_format(&t4_tx_state,
  1530. compression_type,
  1531. T4_SUPPORT_WIDTH_215MM
  1532. | T4_SUPPORT_LENGTH_US_LETTER
  1533. | T4_SUPPORT_LENGTH_US_LEGAL
  1534. | T4_SUPPORT_LENGTH_UNLIMITED,
  1535. T4_RESOLUTION_R8_STANDARD
  1536. | T4_RESOLUTION_R8_FINE
  1537. | T4_RESOLUTION_R8_SUPERFINE
  1538. | T4_RESOLUTION_R16_SUPERFINE
  1539. | T4_RESOLUTION_200_100
  1540. | T4_RESOLUTION_200_200
  1541. | T4_RESOLUTION_200_400
  1542. | T4_RESOLUTION_300_300
  1543. | T4_RESOLUTION_300_600
  1544. | T4_RESOLUTION_400_400
  1545. | T4_RESOLUTION_400_800
  1546. | T4_RESOLUTION_600_600
  1547. | T4_RESOLUTION_600_1200
  1548. | T4_RESOLUTION_1200_1200,
  1549. T4_RESOLUTION_100_100
  1550. | T4_RESOLUTION_200_200
  1551. | T4_RESOLUTION_300_300
  1552. | T4_RESOLUTION_400_400
  1553. | T4_RESOLUTION_600_600
  1554. | T4_RESOLUTION_1200_1200) < 0)
  1555. {
  1556. span_log(&s->logging, SPAN_LOG_FLOW, "Failed to set T.4 compression\n");
  1557. printf("Test failed\n");
  1558. exit(2);
  1559. }
  1560. t4_tx_set_min_bits_per_row(&t4_tx_state, min_row_bits);
  1561. if (t4_tx_start_page(&t4_tx_state))
  1562. {
  1563. span_log(&s->logging, SPAN_LOG_FLOW, "Failed to start T.4 send\n");
  1564. printf("Test failed\n");
  1565. exit(2);
  1566. }
  1567. /*endif*/
  1568. s->image_len = t4_tx_get(&t4_tx_state, s->image, sizeof(s->image));
  1569. if (parms.bad_rows)
  1570. {
  1571. span_log(&s->logging, SPAN_LOG_FLOW, "We need to corrupt the image\n");
  1572. corrupt_image(s, (const char *) parms.bad_rows);
  1573. }
  1574. /*endif*/
  1575. t4_tx_release(&t4_tx_state);
  1576. span_log(&s->logging, SPAN_LOG_FLOW, "ECM image is %d bytes (min row bits %d)\n", s->image_len, min_row_bits);
  1577. faxtester_set_ecm_image_buffer(s, ecm_block, ecm_frame_size, i);
  1578. }
  1579. else if (strcasecmp((const char *) parms.type, "CLEAR") == 0)
  1580. {
  1581. span_log(&s->logging, SPAN_LOG_FLOW, "Time to drop the call\n");
  1582. faxtester_set_rx_type(s, T30_MODEM_NONE, 0, false, false);
  1583. faxtester_set_tx_type(s, T30_MODEM_PAUSE, 0, s->timeout_x, false);
  1584. t30_terminate(s->far_t30);
  1585. free_node_parms(&parms);
  1586. return 0;
  1587. }
  1588. else if (strcasecmp((const char *) parms.type, "STATUS") == 0)
  1589. {
  1590. if (parms.value)
  1591. {
  1592. for (i = 0; t30_status[i].code >= 0; i++)
  1593. {
  1594. if (strcmp(t30_status[i].tag, (const char *) parms.value) == 0)
  1595. break;
  1596. }
  1597. if (t30_status[i].code >= 0)
  1598. delay = t30_status[i].code;
  1599. else
  1600. delay = atoi((const char *) parms.value);
  1601. t30_get_transfer_statistics(s->far_t30, &t30_stats);
  1602. if (delay == t30_stats.current_status)
  1603. span_log(&s->logging, SPAN_LOG_FLOW, "Expected status (%s) found\n", t30_status[i].tag);
  1604. else
  1605. span_log(&s->logging, SPAN_LOG_FLOW, "Expected status %s, but found %s (%d)\n", t30_status[i].tag, t30_status[t30_stats.current_status].tag, t30_stats.current_status);
  1606. if (delay != t30_stats.current_status)
  1607. {
  1608. printf("Test failed\n");
  1609. exit(2);
  1610. }
  1611. }
  1612. free_node_parms(&parms);
  1613. return 0;
  1614. }
  1615. else
  1616. {
  1617. span_log(&s->logging, SPAN_LOG_FLOW, "Unrecognised type '%s'\n", (const char *) parms.type);
  1618. free_node_parms(&parms);
  1619. return 0;
  1620. }
  1621. /*endif*/
  1622. }
  1623. /*endif*/
  1624. free_node_parms(&parms);
  1625. return 1;
  1626. }
  1627. /*- End of function --------------------------------------------------------*/
  1628. static int parse_config(faxtester_state_t *s, xmlNodePtr cur)
  1629. {
  1630. xmlChar *x;
  1631. xmlChar *y;
  1632. while (cur)
  1633. {
  1634. if (xmlStrcmp(cur->name, (const xmlChar *) "path") == 0)
  1635. {
  1636. x = NULL;
  1637. y = NULL;
  1638. if ((x = xmlGetProp(cur, (const xmlChar *) "type"))
  1639. &&
  1640. (y = xmlGetProp(cur, (const xmlChar *) "value")))
  1641. {
  1642. if (strcasecmp((const char *) x, "IMAGE") == 0)
  1643. {
  1644. span_log(&s->logging, SPAN_LOG_FLOW, "Found '%s' '%s'\n", (char *) x, (char *) y);
  1645. strcpy(s->image_path, (const char *) y);
  1646. }
  1647. /*endif*/
  1648. }
  1649. /*endif*/
  1650. if (x)
  1651. xmlFree(x);
  1652. /*endif*/
  1653. if (y)
  1654. xmlFree(y);
  1655. /*endif*/
  1656. }
  1657. /*endif*/
  1658. cur = cur->next;
  1659. }
  1660. /*endwhile*/
  1661. return -1;
  1662. }
  1663. /*- End of function --------------------------------------------------------*/
  1664. static int parse_test_group(faxtester_state_t *s, xmlNodePtr cur, const char *test)
  1665. {
  1666. xmlChar *x;
  1667. while (cur)
  1668. {
  1669. if (xmlStrcmp(cur->name, (const xmlChar *) "test") == 0)
  1670. {
  1671. if ((x = xmlGetProp(cur, (const xmlChar *) "name")))
  1672. {
  1673. if (xmlStrcmp(x, (const xmlChar *) test) == 0)
  1674. {
  1675. span_log(&s->logging, SPAN_LOG_FLOW, "Found '%s'\n", (char *) x);
  1676. s->cur = cur->xmlChildrenNode;
  1677. xmlFree(x);
  1678. return 0;
  1679. }
  1680. /*endif*/
  1681. xmlFree(x);
  1682. }
  1683. /*endif*/
  1684. }
  1685. /*endif*/
  1686. cur = cur->next;
  1687. }
  1688. /*endwhile*/
  1689. return -1;
  1690. }
  1691. /*- End of function --------------------------------------------------------*/
  1692. static int get_test_set(faxtester_state_t *s, const char *test_file, const char *test)
  1693. {
  1694. xmlParserCtxtPtr ctxt;
  1695. xmlNodePtr cur;
  1696. if ((ctxt = xmlNewParserCtxt()) == NULL)
  1697. {
  1698. fprintf(stderr, "Failed to allocate XML parser context\n");
  1699. return -1;
  1700. }
  1701. /* parse the file, activating the DTD validation option */
  1702. if ((s->doc = xmlCtxtReadFile(ctxt, test_file, NULL, XML_PARSE_XINCLUDE | XML_PARSE_DTDVALID)) == NULL)
  1703. {
  1704. fprintf(stderr, "Failed to read the XML document\n");
  1705. return -1;
  1706. }
  1707. if (ctxt->valid == 0)
  1708. {
  1709. fprintf(stderr, "Failed to validate the XML document\n");
  1710. xmlFreeDoc(s->doc);
  1711. s->doc = NULL;
  1712. xmlFreeParserCtxt(ctxt);
  1713. return -1;
  1714. }
  1715. xmlFreeParserCtxt(ctxt);
  1716. /* Check the document is of the right kind */
  1717. if ((cur = xmlDocGetRootElement(s->doc)) == NULL)
  1718. {
  1719. xmlFreeDoc(s->doc);
  1720. s->doc = NULL;
  1721. fprintf(stderr, "Empty document\n");
  1722. return -1;
  1723. }
  1724. /*endif*/
  1725. if (xmlStrcmp(cur->name, (const xmlChar *) "fax-tests"))
  1726. {
  1727. xmlFreeDoc(s->doc);
  1728. s->doc = NULL;
  1729. fprintf(stderr, "Document of the wrong type, root node != fax-tests\n");
  1730. return -1;
  1731. }
  1732. /*endif*/
  1733. cur = cur->xmlChildrenNode;
  1734. while (cur && xmlIsBlankNode(cur))
  1735. cur = cur->next;
  1736. /*endwhile*/
  1737. if (cur == NULL)
  1738. {
  1739. fprintf(stderr, "XML test not found\n");
  1740. return -1;
  1741. }
  1742. /*endif*/
  1743. xmlCleanupParser();
  1744. while (cur)
  1745. {
  1746. if (xmlStrcmp(cur->name, (const xmlChar *) "config") == 0)
  1747. parse_config(s, cur->xmlChildrenNode);
  1748. /*endif*/
  1749. if (xmlStrcmp(cur->name, (const xmlChar *) "test-group") == 0)
  1750. {
  1751. if (parse_test_group(s, cur->xmlChildrenNode, test) == 0)
  1752. return 0;
  1753. /*endif*/
  1754. }
  1755. /*endif*/
  1756. cur = cur->next;
  1757. }
  1758. /*endwhile*/
  1759. fprintf(stderr, "XML test not found\n");
  1760. return -1;
  1761. }
  1762. /*- End of function --------------------------------------------------------*/
  1763. SPAN_DECLARE(logging_state_t *) faxtester_get_logging_state(faxtester_state_t *s)
  1764. {
  1765. return &s->logging;
  1766. }
  1767. /*- End of function --------------------------------------------------------*/
  1768. faxtester_state_t *faxtester_init(faxtester_state_t *s, const char *test_file, const char *test)
  1769. {
  1770. if (s == NULL)
  1771. {
  1772. if ((s = (faxtester_state_t *) malloc(sizeof(*s))) == NULL)
  1773. return NULL;
  1774. }
  1775. /*endif*/
  1776. memset(s, 0, sizeof(*s));
  1777. span_log_init(&s->logging, SPAN_LOG_NONE, NULL);
  1778. span_log_set_protocol(&s->logging, "TST");
  1779. fax_modems_init(&s->modems,
  1780. false,
  1781. hdlc_accept,
  1782. hdlc_underflow_handler,
  1783. non_ecm_put_bit,
  1784. t38_non_ecm_buffer_get_bit,
  1785. tone_detected,
  1786. s);
  1787. fax_modems_set_tep_mode(&s->modems, false);
  1788. fax_modems_set_rx_active(&s->modems, true);
  1789. faxtester_set_timeout(s, -1);
  1790. s->timein_x = -1;
  1791. s->timeout_x = -1;
  1792. faxtester_set_tx_type(s, T30_MODEM_NONE, 0, false, false);
  1793. strcpy(s->image_path, ".");
  1794. s->next_tx_file[0] = '\0';
  1795. if (get_test_set(s, test_file, test) < 0)
  1796. {
  1797. /* TODO: free the state, if it was allocated. */
  1798. return NULL;
  1799. }
  1800. /*endif*/
  1801. memset(&s->expected_rx_info, 0, sizeof(s->expected_rx_info));
  1802. return s;
  1803. }
  1804. /*- End of function --------------------------------------------------------*/
  1805. int faxtester_release(faxtester_state_t *s)
  1806. {
  1807. if (s->doc)
  1808. {
  1809. xmlFreeDoc(s->doc);
  1810. s->doc = NULL;
  1811. }
  1812. return 0;
  1813. }
  1814. /*- End of function --------------------------------------------------------*/
  1815. int faxtester_free(faxtester_state_t *s)
  1816. {
  1817. faxtester_release(s);
  1818. free(s);
  1819. return 0;
  1820. }
  1821. /*- End of function --------------------------------------------------------*/
  1822. /*- End of file ------------------------------------------------------------*/