2
0

testadsi.c 21 KB

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