예제 #1
0
static void *channel_run(zap_thread_t *me, void *obj)
{
	zap_channel_t *zchan = obj;
	int fd = -1;
	short buf[160];

	zap_mutex_lock(mutex);
	T++;
	zap_mutex_unlock(mutex);

	zap_set_state_locked_wait(zchan, ZAP_CHANNEL_STATE_UP);

	if ((fd = open("test.raw", O_RDONLY, 0)) < 0) {
		goto end;
	}

	while(R == 1 && THREADS[zchan->span_id][zchan->chan_id] == 1) {
		ssize_t bytes = read(fd, buf, sizeof(buf));
		size_t bbytes;

		if (bytes <= 0) {
			break;
		}

		bbytes = (size_t) bytes;

		zio_slin2alaw(buf, sizeof(buf), &bbytes);

		if (zap_channel_write(zchan, buf, sizeof(buf), &bbytes) != ZAP_SUCCESS) {
			break;
		}
	}

	close(fd);

 end:

	zap_set_state_locked_wait(zchan, ZAP_CHANNEL_STATE_HANGUP);

	THREADS[zchan->span_id][zchan->chan_id] = 0;

	zap_mutex_lock(mutex);
	T = 0;
	zap_mutex_unlock(mutex);
	
	return NULL;
}
예제 #2
0
static int __pri_sangoma_write(struct pri *pri, void *buf, int buflen)
{
	struct sangoma_pri *spri = (struct sangoma_pri *) pri->userdata;
	int res;
	zap_size_t len = buflen -2;
	char bb[4096] = "";

	if (zap_channel_write(spri->zdchan, buf, buflen, &len) != ZAP_SUCCESS) {
		printf("D-WRITE FAIL! [%s]\n", spri->zdchan->last_error);
        return 0;
	}
	
	//print_bits(buf, (int)buflen-2, bb, sizeof(bb), 1, 0);
	//zap_log(ZAP_LOG_DEBUG, "WRITE %d\n%s\n%s\n\n", (int)buflen-2, LINE, bb);

	return (int) buflen;
}
예제 #3
0
int lpwrap_init_pri(struct lpwrap_pri *spri, zap_span_t *span, zap_channel_t *dchan, int swtype, int node, int debug)
{
	int ret = -1;

	memset(spri, 0, sizeof(struct lpwrap_pri));
	
	spri->dchan = dchan;
	spri->span = span;

	if ((spri->pri = pri_new_cb(spri->dchan->sockfd, node, swtype, __pri_lpwrap_read, __pri_lpwrap_write, spri))){
		unsigned char buf[4] = { 0 };
		size_t buflen = sizeof(buf), len = 0;
		pri_set_debug(spri->pri, debug);
		ret = 0;
		zap_set_flag(spri, LPWRAP_PRI_READY);
		zap_channel_write(spri->dchan, buf, buflen, &len);
	} else {
		fprintf(stderr, "Unable to create PRI\n");
	}

	return ret;
}
예제 #4
0
static int __pri_lpwrap_write(struct pri *pri, void *buf, int buflen)
{
	struct lpwrap_pri *spri = (struct lpwrap_pri *) pri_get_userdata(pri);
	zap_size_t len = buflen -2;

	if (zap_channel_write(spri->dchan, buf, buflen, &len) != ZAP_SUCCESS) {
		zap_log(ZAP_LOG_CRIT, "span %d D-WRITE FAIL! [%s]\n", spri->span->span_id, spri->dchan->last_error);
		zap_clear_flag(spri, LPWRAP_PRI_READY);
		return -1;
	}

#ifdef IODEBUG
	{
		char bb[2048] = { 0 };

		print_hex_bytes(buf, buflen - 2, bb, sizeof(bb));
		zap_log(ZAP_LOG_DEBUG, "WRITE %d\n", (int)buflen-2);
	}
#endif

	return (int) buflen;
}
예제 #5
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);
}