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