Exemplo n.º 1
0
static int on_hangup(struct sangoma_pri *spri, sangoma_pri_event_t event_type, pri_event *event)
{
    //pri_hangup(spri->pri, event->hangup.call, event->hangup.cause);
    printf("-- Hanging up channel %d\n", event->hangup.channel);
    if(pidmap[event->hangup.channel-1].pid) {
        pri_hangup(spri->pri, event->hangup.call, 16);
        pri_destroycall(spri->pri, event->hangup.call);
        kill(pidmap[event->hangup.channel-1].pid, SIGINT);
        pidmap[event->hangup.channel-1].pid = 0;
    }
    return 0;
}
static void event2(struct pri *pri, pri_event *e)
{
	/* CPE */
	switch(e->gen.e) {
	case PRI_EVENT_RING:
		printf("PRI 2: %s (%d)\n", pri_event2str(e->gen.e), e->gen.e);
		pri_proceeding(pri, e->ring.call, e->ring.channel, 0);
		pri_acknowledge(pri, e->ring.call, e->ring.channel, 0);
		break;
	case PRI_EVENT_ANSWER:
		printf("PRI 2: %s (%d)\n", pri_event2str(e->gen.e), e->gen.e);
		pri_hangup(pri, e->answer.call, PRI_CAUSE_NORMAL_UNSPECIFIED);
		break;
	case PRI_EVENT_HANGUP:
		printf("PRI 2: %s (%d)\n", pri_event2str(e->gen.e), e->gen.e);
		pri_hangup(pri, e->hangup.call, e->hangup.cause);
		break;
	case PRI_EVENT_DCHAN_UP:
	default:
		printf("PRI 2: %s (%d)\n", pri_event2str(e->gen.e), e->gen.e);
	}
}
Exemplo n.º 3
0
static void launch_channel(struct sangoma_pri *spri, int channo)
{
	pid_t pid;
	int fd = 0, file = 0, inlen = 0, outlen = 0;
	unsigned char inframe[MAX_BYTES], outframe[MAX_BYTES];
	fd_set readfds;
	int mtu_mru=BYTES / 2;
	int err;
	zap_channel_t *chan;
	zap_codec_t codec = ZAP_CODEC_SLIN;
	unsigned ms = 20;
	unsigned int lead = 50;
	int ifd = -1;
	zap_tone_type_t tt = ZAP_TONE_DTMF;
	char dtmf[] = "1234567890";
	int loops = 0;

	pid = fork();
	
	if (pid) {
		pidmap[channo-1].pid = pid;
		printf("-- Launching process %d to handle channel %d\n", pid, channo);
		return;
	}

	signal(SIGINT, handle_SIGINT);

	//ifd = open("/nfs/sounds/ptest.raw", O_WRONLY|O_CREAT|O_TRUNC, 777);
	
	memset(inframe, 0, MAX_BYTES);
	memset(outframe, 0, MAX_BYTES);
		
	if (zap_channel_open(spri->span, channo, &chan) != ZAP_SUCCESS) {
		printf("DEBUG cant open fd!\n");
	}
	


#if 1
	if (zap_channel_command(chan, ZAP_COMMAND_SET_CODEC, &codec) != ZAP_SUCCESS) {
		printf("Critical Error: Failed to set driver codec!\n");
		zap_channel_close(&chan);
		exit(-1);
	}
#endif
	
#if 1
	if (zap_channel_command(chan, ZAP_COMMAND_ENABLE_DTMF_DETECT, &tt) != ZAP_SUCCESS) {
		printf("Critical Error: Failed to set dtmf detect!\n");
		zap_channel_close(&chan);
		exit(-1);
	}
	zap_channel_set_event_callback(chan, my_zap_event_handler);
#endif


	if (zap_channel_command(chan, ZAP_COMMAND_SET_INTERVAL, &ms) != ZAP_SUCCESS) {
		printf("Critical Error: Failed to set codec interval!\n");
		zap_channel_close(&chan);
		exit(-1);
	}
		
	file = open("sound.raw", O_RDONLY);
	if (file < 0){
		printf("Critical Error: Failed to open sound file!\n");
		zap_channel_close(&chan);
		exit(-1);
	}


	while(ready) {
		zap_wait_flag_t flags = ZAP_READ;
		zap_size_t len;
		loops++;

		if (lead) {
			lead--;
		}

		if (!lead && loops == 300) {
#if 1
			if (zap_channel_command(chan, ZAP_COMMAND_SEND_DTMF, dtmf) != ZAP_SUCCESS) {
				printf("Critical Error: Failed to send dtmf\n");
				zap_channel_close(&chan);
				exit(-1);
			}
#endif

		}

		if (zap_channel_wait(chan, &flags, 2000) != ZAP_SUCCESS) {
			printf("wait FAIL! [%s]\n", chan->last_error);
			break;
		}
	
		if (flags & ZAP_READ) {
			len = MAX_BYTES;
			if (zap_channel_read(chan, inframe, &len) == ZAP_SUCCESS) {
				//printf("READ: %d\n", len);
				//write(ifd, inframe, len);
				if(!lead && (outlen = read(file, outframe, len)) <= 0) {
					break;
				}

			} else {
				printf("READ FAIL! %d [%s]\n", len, chan->last_error);
				break;
			}
			if (lead) {
				continue;
			} 
			zap_channel_write(chan, outframe, sizeof(outframe), &len);
		} else {
			printf("BREAK");
			break;
		}
	}

	printf("loop done\n");

	//sangoma_get_full_cfg(fd, &tdm_api);
	close(file);
	//close(ifd);

	pri_hangup(spri->pri, channo, 16);
	if (zap_channel_close(&chan) != ZAP_SUCCESS) {
		printf("Critical Error: Failed to close channel [%s]\n", chan->last_error);
	}

	printf("Call Handler: Process Finished\n");
	exit(0);
}