testfax.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650
  1. /*
  2. * SpanDSP - a series of DSP components for telephony
  3. *
  4. * testfax.c
  5. *
  6. * Written by Steve Underwood <steveu@coppice.org>
  7. *
  8. * Copyright (C) 2003 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. #if defined(HAVE_CONFIG_H)
  26. #include <config.h>
  27. #endif
  28. #include <fcntl.h>
  29. #include <stdio.h>
  30. #include <string.h>
  31. #include <errno.h>
  32. #include <sys/ioctl.h>
  33. #include <stdlib.h>
  34. #include <inttypes.h>
  35. #include <sys/signal.h>
  36. #include <sys/select.h>
  37. #include <sys/wait.h>
  38. #include <sys/resource.h>
  39. #include <sys/time.h>
  40. #if defined(HAVE_LIBUNICALL)
  41. #include <linux/zaptel.h>
  42. #include <pthread.h>
  43. #include <sndfile.h>
  44. #include <tiffio.h>
  45. #include "unicall.h"
  46. //#include "../libmfcr2/libmfcr2.h"
  47. //#include "../libpri/libpri.h"
  48. //#include "../libpri/libfx.h"
  49. #include "spandsp.h"
  50. int caller_mode = FALSE;
  51. static SNDFILE *rxhandle;
  52. static SNDFILE *txhandle;
  53. typedef struct
  54. {
  55. pthread_t thread;
  56. int chan;
  57. int sig_fd;
  58. int fd;
  59. uc_call_t *call;
  60. uc_crn_t crn;
  61. int xxx;
  62. int cause;
  63. uc_t *uc;
  64. dtmf_rx_state_t dtmf_state;
  65. char dtmf[101];
  66. int dtmf_ptr;
  67. char *tag;
  68. char originating_number[32];
  69. char destination_number[32];
  70. t30_state_t fax;
  71. } chan_stuff_t;
  72. chan_stuff_t chan_stuff[30];
  73. tone_gen_descriptor_t tone_desc;
  74. tone_gen_state_t gen;
  75. pthread_mutex_t mutex;
  76. void channel_read_fax_channel(uc_t *uc, int chan, void *user_data, uint8_t *buf, int len);
  77. int channel_write_fax_channel(uc_t *uc, int chan, void *user_data, uint8_t *buf, int max_len);
  78. int channel_error(uc_t *uc, int chan, void *user_data, int cause);
  79. int signaling_error(uc_t *uc, void *user_data, int cause);
  80. void channel_read_fax_channel(uc_t *uc, int chan, void *user_data, uint8_t *buf, int len)
  81. {
  82. int i;
  83. int xlen;
  84. char *s;
  85. int outframes;
  86. #if 0
  87. outframes = sf_writef_short(rxhandle,
  88. AF_DEFAULT_TRACK,
  89. buf,
  90. len >> 1);
  91. if (outframes != len)
  92. {
  93. printf("Failed to write %d samples\n", len);
  94. exit(2);
  95. }
  96. #endif
  97. dtmf_rx(&chan_stuff[chan].dtmf_state, (int16_t *) buf, len);
  98. xlen = dtmf_rx_get(&chan_stuff[chan].dtmf_state,
  99. chan_stuff[chan].dtmf + chan_stuff[chan].dtmf_ptr,
  100. 100 - chan_stuff[chan].dtmf_ptr);
  101. if (xlen > 0)
  102. {
  103. s = chan_stuff[chan].dtmf + chan_stuff[chan].dtmf_ptr;
  104. while (*s)
  105. {
  106. if (*s == '#')
  107. {
  108. uc_set_channel_read_callback(uc, 0, NULL, 0);
  109. uc_set_channel_write_callback(uc, 0, NULL, 0);
  110. if (uc_call_control(uc, UC_OP_DROPCALL, chan_stuff[chan].crn, (void *) UC_CAUSE_NORMAL_CLEARING))
  111. printf ("A Drop Call failed\n");
  112. /*endif*/
  113. break;
  114. }
  115. /*endif*/
  116. s++;
  117. }
  118. /*endwhile*/
  119. printf("Got '%s'\n", chan_stuff[chan].dtmf);
  120. chan_stuff[chan].dtmf_ptr += xlen;
  121. }
  122. /*endif*/
  123. t30_rx(&(chan_stuff[chan].fax), (int16_t *) buf, len);
  124. }
  125. /*- End of function --------------------------------------------------------*/
  126. int channel_write_fax_channel(uc_t *uc, int chan, void *user_data, uint8_t *buf, int max_len)
  127. {
  128. int len;
  129. len = t30_tx(&(chan_stuff[chan].fax), (int16_t *) buf, max_len >> 1);
  130. sf_writef_short(txhandle, AF_DEFAULT_TRACK, buf, len);
  131. if (len > 0)
  132. len <<= 1;
  133. return len;
  134. }
  135. /*- End of function --------------------------------------------------------*/
  136. int channel_error(uc_t *uc, int chan, void *user_data, int cause)
  137. {
  138. printf("Error %d\n", cause);
  139. return 0;
  140. }
  141. /*- End of function --------------------------------------------------------*/
  142. int signaling_error(uc_t *uc, void *user_data, int cause)
  143. {
  144. printf("Error %d\n", cause);
  145. return 0;
  146. }
  147. /*- End of function --------------------------------------------------------*/
  148. static void initiate_call(uc_t *uc, int chan, uc_event_t *e)
  149. {
  150. uc_makecall_t makecall;
  151. uc_callparms_t *callparms;
  152. int ret;
  153. printf ("Initiating call\n");
  154. pthread_mutex_lock(&mutex);
  155. if ((callparms = uc_new_callparms(NULL)) == NULL)
  156. return;
  157. /*endif*/
  158. pthread_mutex_unlock(&mutex);
  159. uc_callparm_originating_number(callparms, chan_stuff[chan].originating_number);
  160. uc_callparm_destination_number(callparms, chan_stuff[chan].destination_number);
  161. makecall.callparms = callparms;
  162. makecall.crn = 0;
  163. if (ret = uc_call_control(uc, UC_OP_MAKECALL, 0, (void *) &makecall) != UC_RET_OK)
  164. fprintf(stderr, "Make Call failed - %d\n", ret);
  165. /*endif*/
  166. chan_stuff[chan].crn = makecall.crn;
  167. free(callparms);
  168. }
  169. /*- End of function --------------------------------------------------------*/
  170. static void phase_b_handler(t30_state_t *s, void *user_data, int msg)
  171. {
  172. chan_stuff_t *t;
  173. t = (chan_stuff_t *) user_data;
  174. printf("Phase B - %d\n", msg);
  175. }
  176. /*- End of function --------------------------------------------------------*/
  177. static void phase_d_handler(t30_state_t *s, void *user_data, int msg)
  178. {
  179. chan_stuff_t *t;
  180. t = (chan_stuff_t *) user_data;
  181. printf("Phase D - %d\n", msg);
  182. }
  183. /*- End of function --------------------------------------------------------*/
  184. static void phase_e_handler(t30_state_t *s, void *user_data, int result)
  185. {
  186. chan_stuff_t *t;
  187. printf("Phase E - %d\n", result);
  188. t = (chan_stuff_t *) user_data;
  189. if (uc_call_control(t->uc, UC_OP_DROPCALL, t->crn, (void *) UC_CAUSE_NORMAL_CLEARING))
  190. fprintf(stderr, "Phase E Drop Call failed\n");
  191. /*endif*/
  192. }
  193. /*- End of function --------------------------------------------------------*/
  194. static void flush_handler(t30_state_t *s, void *user_data, int which)
  195. {
  196. chan_stuff_t *t;
  197. printf("Flush\n");
  198. t = (chan_stuff_t *) user_data;
  199. }
  200. /*- End of function --------------------------------------------------------*/
  201. static void handle_uc_event(uc_t *uc, void *user_data, uc_event_t *e)
  202. {
  203. int chan;
  204. chan = (int) user_data;
  205. printf ("-- %s (%d)\n", uc_event2str(e->e), chan);
  206. switch (e->e)
  207. {
  208. case UC_EVENT_DEVICEFAIL:
  209. break;
  210. case UC_EVENT_PROTOCOLFAIL:
  211. printf("-- Protocol failure on channel %d, cause %d\n", e->gen.channel, e->gen.data);
  212. break;
  213. case UC_EVENT_SIGCHANSTATUS:
  214. printf("-- Signalling channel status - %s\n", e->sigchanstatus.ok ? "Up" : "Down");
  215. break;
  216. case UC_EVENT_ALARM:
  217. printf("-- Alarm - 0x%X 0x%X\n", e->alarm.raised, e->alarm.cleared);
  218. break;
  219. case UC_EVENT_FARBLOCKED:
  220. printf("-- Channel far end blocked! :-(\n");
  221. chan_stuff[chan].xxx &= ~1;
  222. break;
  223. case UC_EVENT_FARUNBLOCKED:
  224. printf("-- Channel far end unblocked! :-)\n");
  225. chan_stuff[chan].xxx |= 1;
  226. if (chan_stuff[chan].xxx == 3)
  227. {
  228. if (caller_mode)
  229. initiate_call(uc, chan, e);
  230. /*endif*/
  231. }
  232. /*endif*/
  233. break;
  234. case UC_EVENT_LOCALBLOCKED:
  235. printf("-- Channel local end blocked! :-(\n");
  236. chan_stuff[chan].xxx &= ~2;
  237. break;
  238. case UC_EVENT_LOCALUNBLOCKED:
  239. printf("-- Channel local end unblocked! :-)\n");
  240. chan_stuff[chan].xxx |= 2;
  241. if (chan_stuff[chan].xxx == 3)
  242. {
  243. if (caller_mode)
  244. initiate_call(uc, chan, e);
  245. /*endif*/
  246. }
  247. /*endif*/
  248. break;
  249. case UC_EVENT_DIALING:
  250. printf("-- Dialing on channel %d\n", e->gen.channel);
  251. break;
  252. case UC_EVENT_PROCEEDING:
  253. printf("-- Proceeding on channel %d\n", e->gen.channel);
  254. break;
  255. case UC_EVENT_ACCEPTED:
  256. printf("-- Accepted on channel %d\n", e->gen.channel);
  257. if (uc_call_control(uc, UC_OP_ANSWERCALL, e->gen.crn, (void *) -1))
  258. fprintf(stderr, "Answer Call failed\n");
  259. /*endif*/
  260. break;
  261. case UC_EVENT_DETECTED:
  262. printf("-- Detected on channel %d\n", e->gen.channel);
  263. break;
  264. case UC_EVENT_MOREDIGITS:
  265. printf("-- More digits on channel %d, CRN %d (ANI: %s, DNIS: %s)\n", e->offered.channel, e->offered.crn, e->offered.parms.originating_number, e->offered.parms.destination_number);
  266. break;
  267. case UC_EVENT_ALERTING:
  268. printf("-- Alerting on channel %d\n", e->gen.channel);
  269. /* This is just a notification of call progress. We need take no action at this point. */
  270. break;
  271. case UC_EVENT_FARDISCONNECTED:
  272. printf("-- Far end disconnected on channel %d\n", e->fardisconnected.channel);
  273. /* Kill any outstanding audio processing */
  274. uc_set_channel_read_callback(uc, 0, NULL, 0);
  275. uc_set_channel_write_callback(uc, 0, NULL, 0);
  276. if (uc_call_control(uc, UC_OP_DROPCALL, e->fardisconnected.crn, (void *) UC_CAUSE_NORMAL_CLEARING))
  277. fprintf(stderr, "C Drop Call failed\n");
  278. /*endif*/
  279. break;
  280. case UC_EVENT_DROPCALL:
  281. printf("-- Drop call on channel %d\n", e->gen.channel);
  282. if (uc_call_control(uc, UC_OP_RELEASECALL, e->gen.crn, NULL))
  283. fprintf(stderr, "uc_ReleaseCall failed\n");
  284. /*endif*/
  285. break;
  286. case UC_EVENT_RELEASECALL:
  287. printf("-- Released on channel %d\n", e->gen.channel);
  288. if (caller_mode)
  289. initiate_call(uc, chan, e);
  290. /*endif*/
  291. break;
  292. case UC_EVENT_OFFERED:
  293. printf("-- Offered on channel %d, CRN %d (ANI: %s, DNIS: %s)\n", e->offered.channel, e->offered.crn, e->offered.parms.originating_number, e->offered.parms.destination_number);
  294. if (!caller_mode)
  295. {
  296. switch (chan_stuff[chan].cause)
  297. {
  298. case 0:
  299. if (uc_call_control(uc, UC_OP_ACCEPTCALL, e->offered.crn, (void *) -1))
  300. fprintf(stderr, "uc_AcceptCall failed\n");
  301. /*endif*/
  302. chan_stuff[chan].crn = e->offered.crn;
  303. break;
  304. case 1:
  305. if (uc_call_control(uc, UC_OP_ANSWERCALL, e->offered.crn, (void *) -1))
  306. fprintf(stderr, "uc_AnswerCall failed\n");
  307. /*endif*/
  308. chan_stuff[chan].crn = e->offered.crn;
  309. break;
  310. case 2:
  311. if (uc_call_control(uc, UC_OP_DROPCALL, e->offered.crn, (void *) UC_CAUSE_USER_BUSY))
  312. fprintf(stderr, "E Drop Call failed\n");
  313. /*endif*/
  314. break;
  315. case 3:
  316. if (uc_call_control(uc, UC_OP_DROPCALL, e->offered.crn, (void *) UC_CAUSE_UNASSIGNED_NUMBER))
  317. fprintf(stderr, "F Drop Call failed\n");
  318. /*endif*/
  319. break;
  320. case 4:
  321. if (uc_call_control(uc, UC_OP_DROPCALL, e->offered.crn, (void *) UC_CAUSE_NETWORK_CONGESTION))
  322. fprintf(stderr, "G Drop Call failed\n");
  323. /*endif*/
  324. break;
  325. case 5:
  326. if (uc_call_control(uc, UC_OP_DROPCALL, e->offered.crn, (void *) UC_CAUSE_DEST_OUT_OF_ORDER))
  327. fprintf(stderr, "H Drop Call failed\n");
  328. /*endif*/
  329. break;
  330. }
  331. /*endswitch*/
  332. if (++chan_stuff[chan].cause > 5)
  333. chan_stuff[chan].cause = 0;
  334. /*endif*/
  335. }
  336. /*endif*/
  337. break;
  338. case UC_EVENT_ANSWERED:
  339. printf("-- Answered on channel %d\n", e->gen.channel);
  340. uc_set_channel_read_callback(uc, 0, channel_read_fax_channel, (void *) chan);
  341. printf("XXX read callback set\n");
  342. uc_set_channel_write_callback(uc, 0, channel_write_fax_channel, (void *) chan);
  343. printf("XXX write callback set\n");
  344. t30_init(&(chan_stuff[chan].fax), FALSE, uc);
  345. t30_set_local_ident(&(chan_stuff[chan].fax), "12345678");
  346. t30_set_tx_file(&(chan_stuff[chan].fax), "tx.tif");
  347. //t30_set_rx_file(&(chan_stuff[chan].fax), "rx.tif");
  348. t30_set_phase_b_handler(&(chan_stuff[chan].fax), phase_b_handler, &(chan_stuff[chan]));
  349. t30_set_phase_d_handler(&(chan_stuff[chan].fax), phase_d_handler, &(chan_stuff[chan]));
  350. t30_set_phase_e_handler(&(chan_stuff[chan].fax), phase_e_handler, &(chan_stuff[chan]));
  351. t30_set_flush_handler(&(chan_stuff[chan].fax), flush_handler, &(chan_stuff[chan]));
  352. printf("XXX FAX inited\n");
  353. dtmf_rx_init(&chan_stuff[chan].dtmf_state, NULL, NULL);
  354. printf("XXX DTMF inited\n");
  355. break;
  356. case UC_EVENT_CONNECTED:
  357. printf("-- Connected on channel %d\n", e->gen.channel);
  358. uc_set_channel_read_callback(uc, 0, channel_read_fax_channel, (void *) chan);
  359. printf("XXX read callback set\n");
  360. uc_set_channel_write_callback(uc, 0, channel_write_fax_channel, (void *) chan);
  361. printf("XXX write callback set\n");
  362. t30_init(&(chan_stuff[chan].fax), TRUE, uc);
  363. t30_set_local_ident(&(chan_stuff[chan].fax), "87654321");
  364. t30_set_tx_file(&(chan_stuff[chan].fax), "tx.tif");
  365. //t30_set_rx_file(&(chan_stuff[chan].fax), "rx.tif");
  366. t30_set_phase_b_handler(&(chan_stuff[chan].fax), phase_b_handler, &(chan_stuff[chan]));
  367. t30_set_phase_d_handler(&(chan_stuff[chan].fax), phase_d_handler, &(chan_stuff[chan]));
  368. t30_set_phase_e_handler(&(chan_stuff[chan].fax), phase_e_handler, &(chan_stuff[chan]));
  369. printf("XXX FAX inited\n");
  370. #if 0
  371. if (uc_call_control(uc, UC_OP_DROPCALL, e->offered.crn, (void *) UC_CAUSE_NORMAL_CLEARING))
  372. printf ("I Drop Call failed\n");
  373. /*endif*/
  374. #endif
  375. break;
  376. default:
  377. fprintf(stderr, "--!! Unknown signaling event %d\n", e->e);
  378. break;
  379. }
  380. /*endswitch*/
  381. }
  382. /*- End of function --------------------------------------------------------*/
  383. static void *run_uc(void *arg)
  384. {
  385. uc_t *uc;
  386. uc_event_t *e;
  387. struct timeval tv = {0,0};
  388. struct timeval *next;
  389. fd_set rfds;
  390. fd_set wfds;
  391. fd_set efds;
  392. int res;
  393. int dfd;
  394. int chan;
  395. chan = *((int *) arg);
  396. dfd = chan_stuff[chan].fd;
  397. if (chan < 4)
  398. uc = uc_new(dfd, dfd, "fx", "ls,us", UC_MODE_CO, 1);
  399. else
  400. uc = uc_new(dfd, dfd, "fx", "ls", UC_MODE_CPE, 1);
  401. //uc = uc_new(dfd, dfd, "mfcr2", "cn", UC_MODE_CPE, 1);
  402. //uc = uc_new(dfd, dfd, "pri", "ctr4", UC_MODE_CPE, 1);
  403. if (uc == NULL)
  404. {
  405. fprintf(stderr, "Unable to create instance\n");
  406. return NULL;
  407. }
  408. /*endif*/
  409. uc_set_api_codec(uc, 0, UC_CODEC_LINEAR16);
  410. chan_stuff[chan].uc = uc;
  411. uc_set_signaling_callback(uc, handle_uc_event, (void *) chan);
  412. uc_set_signaling_error_callback(uc, signaling_error, (void *) chan);
  413. uc_set_channel_error_callback(uc, 0, channel_error, (void *) chan);
  414. uc_set_logging(uc, 0x7FFFFFFF, 0, chan_stuff[chan].tag);
  415. uc_call_control(uc, UC_OP_UNBLOCK, 0, (void *) -1);
  416. for (;;)
  417. {
  418. FD_ZERO(&rfds);
  419. FD_ZERO(&wfds);
  420. FD_ZERO(&efds);
  421. FD_SET(dfd, &rfds);
  422. FD_SET(dfd, &wfds);
  423. FD_SET(dfd, &efds);
  424. if ((next = uc_schedule_next(uc)))
  425. {
  426. gettimeofday(&tv, NULL);
  427. tv.tv_sec = next->tv_sec - tv.tv_sec;
  428. tv.tv_usec = next->tv_usec - tv.tv_usec;
  429. if (tv.tv_usec < 0)
  430. {
  431. tv.tv_usec += 1000000;
  432. tv.tv_sec -= 1;
  433. }
  434. /*endif*/
  435. if (tv.tv_sec < 0)
  436. {
  437. tv.tv_sec = 0;
  438. tv.tv_usec = 0;
  439. }
  440. /*endif*/
  441. }
  442. /*endif*/
  443. res = select(dfd + 1, &rfds, NULL, &efds, next ? &tv : NULL);
  444. e = NULL;
  445. if (res == 0)
  446. {
  447. uc_schedule_run(uc);
  448. }
  449. else if (res > 0)
  450. {
  451. e = uc_check_event(uc);
  452. }
  453. else if (errno != EINTR)
  454. {
  455. fprintf(stderr, "Error (%d) on select: %s\n", errno, strerror(errno));
  456. }
  457. /*endif*/
  458. if (e)
  459. {
  460. printf("Non-callback signaling event\n");
  461. handle_uc_event(uc, (void *) chan, e);
  462. }
  463. /*endif*/
  464. }
  465. /*endfor*/
  466. return NULL;
  467. }
  468. /*- End of function --------------------------------------------------------*/
  469. int main(int argc, char *argv[])
  470. {
  471. pthread_attr_t attr;
  472. struct zt_bufferinfo b;
  473. struct zt_gains g;
  474. int chan;
  475. int chanx;
  476. char dev_name[20];
  477. AFfilesetup filesetup;
  478. int j;
  479. filesetup = afNewFileSetup();
  480. if (filesetup == AF_NULL_FILESETUP)
  481. {
  482. fprintf(stderr, " Failed to create file setup\n");
  483. exit(2);
  484. }
  485. afInitSampleFormat(filesetup, AF_DEFAULT_TRACK, AF_SAMPFMT_TWOSCOMP, 16);
  486. afInitRate(filesetup, AF_DEFAULT_TRACK, (float) SAMPLE_RATE);
  487. afInitFileFormat(filesetup, AF_FILE_WAVE);
  488. afInitChannels(filesetup, AF_DEFAULT_TRACK, 1);
  489. rxhandle = afOpenFile("rxfax.wav", "w", filesetup);
  490. if (rxhandle == NULL)
  491. {
  492. fprintf(stderr, " Failed to open fax audio file\n");
  493. exit(2);
  494. }
  495. txhandle = afOpenFile("txfax.wav", "w", filesetup);
  496. if (txhandle == NULL)
  497. {
  498. fprintf(stderr, " Failed to open fax audio file\n");
  499. exit(2);
  500. }
  501. if (argc < 1)
  502. {
  503. fprintf(stderr, "Usage: testcall [call]\n");
  504. exit(1);
  505. }
  506. /*endif*/
  507. uc_start();
  508. pthread_attr_init(&attr);
  509. pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
  510. pthread_mutex_init(&mutex, NULL);
  511. for (chan = 0; chan < 5/*30*/; chan++)
  512. {
  513. chan_stuff[chan].sig_fd = open("/dev/zap/channel", O_RDWR | O_NONBLOCK);
  514. if (chan_stuff[chan].sig_fd < 0)
  515. {
  516. fprintf(stderr, "Failed to open channel: %s\n", strerror(errno));
  517. exit(1);
  518. }
  519. /*endif*/
  520. chan_stuff[chan].fd = chan_stuff[chan].sig_fd;
  521. /* Allow for the missing channel at TS16 */
  522. chanx = chan + 1 + (chan + 15)%30;
  523. chanx = chan + 125;
  524. if (ioctl(chan_stuff[chan].fd, ZT_SPECIFY, &chanx))
  525. {
  526. fprintf(stderr, "Failed to specify channel %d: %s\n", chanx, strerror(errno));
  527. exit(1);
  528. }
  529. /*endif*/
  530. if (ioctl(chan_stuff[chan].fd, ZT_GET_BUFINFO, &b))
  531. {
  532. fprintf(stderr, "Unable to get buffer info on channel %d: %s\n", chanx, strerror(errno));
  533. exit(1);
  534. }
  535. /*endif*/
  536. printf ("%d %d %d %d %d %d\n",
  537. b.rxbufpolicy,
  538. b.txbufpolicy,
  539. b.numbufs,
  540. b.bufsize,
  541. b.readbufs,
  542. b.writebufs);
  543. b.rxbufpolicy = ZT_POLICY_IMMEDIATE;
  544. b.txbufpolicy = ZT_POLICY_IMMEDIATE;
  545. b.numbufs = 4;
  546. b.bufsize = 160;
  547. if (ioctl(chan_stuff[chan].fd, ZT_SET_BUFINFO, &b))
  548. {
  549. fprintf(stderr, "Unable to set buffer info on channel %d: %s\n", chanx, strerror(errno));
  550. exit(1);
  551. }
  552. /*endif*/
  553. if (ioctl(chan_stuff[chan].fd, ZT_GET_BUFINFO, &b))
  554. {
  555. fprintf(stderr, "Unable to get buffer info on channel %d: %s\n", chanx, strerror(errno));
  556. exit(1);
  557. }
  558. /*endif*/
  559. for (j = 0; j < 256; j++)
  560. {
  561. g.rxgain[j] = j;
  562. g.txgain[j] = j;
  563. }
  564. /*endif*/
  565. ioctl(chan_stuff[chan].fd, ZT_SETGAINS, &g);
  566. printf("%d %d %d %d %d %d\n",
  567. b.rxbufpolicy,
  568. b.txbufpolicy,
  569. b.numbufs,
  570. b.bufsize,
  571. b.readbufs,
  572. b.writebufs);
  573. if (argc > 1)
  574. caller_mode = TRUE;
  575. /*endif*/
  576. chan_stuff[chan].chan = chan;
  577. sprintf(dev_name, "Chan %2d:", chanx);
  578. chan_stuff[chan].tag = strdup(dev_name);
  579. sprintf(chan_stuff[chan].originating_number, "%d", 987654321 + chan);
  580. sprintf(chan_stuff[chan].destination_number, "%d", 1234 + chan);
  581. printf("Thread for channel %d\n", chan);
  582. if (pthread_create(&chan_stuff[chan].thread, &attr, run_uc, &chan_stuff[chan].chan))
  583. exit(2);
  584. /*endif*/
  585. }
  586. /*endfor*/
  587. for (;;)
  588. {
  589. sleep(5);
  590. printf("Main thread\n");
  591. }
  592. /*endfor*/
  593. return 0;
  594. }
  595. /*- End of function --------------------------------------------------------*/
  596. #else
  597. int main(int argc, char *argv[])
  598. {
  599. printf("This program was not built with Unicall available\n");
  600. }
  601. /*- End of function --------------------------------------------------------*/
  602. #endif
  603. /*- End of file ------------------------------------------------------------*/