Esempio n. 1
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);
}
Esempio n. 2
0
int main(int argc, char *argv[])
{
	zap_global_set_default_logger(ZAP_LOG_LEVEL_DEBUG);
	zap_channel_t *chan;
	unsigned ms = 20;
	zap_codec_t codec = ZAP_CODEC_SLIN;
	unsigned runs = 1;


	if (zap_global_init() != ZAP_SUCCESS) {
		fprintf(stderr, "Error loading OpenZAP\n");
		exit(-1);
	}

	printf("OpenZAP loaded\n");

 top:
	//if (zap_channel_open_any("wanpipe", 0, ZAP_TOP_DOWN, &chan) == ZAP_SUCCESS) {
	if (zap_channel_open(1, 1, &chan) == ZAP_SUCCESS) {
		int x = 0;
		printf("opened channel %d:%d\n", chan->span_id, chan->chan_id);

#if 1
		if (zap_channel_command(chan, ZAP_COMMAND_SET_INTERVAL, &ms) == ZAP_SUCCESS) {
			ms = 0;
			zap_channel_command(chan, ZAP_COMMAND_GET_INTERVAL, &ms);
			printf("interval set to %u\n", ms);
		} else {
			printf("set interval failed [%s]\n", chan->last_error);
		}
#endif
		if (zap_channel_command(chan, ZAP_COMMAND_SET_CODEC, &codec) == ZAP_SUCCESS) {
			codec = 1;
			zap_channel_command(chan, ZAP_COMMAND_GET_CODEC, &codec);
			printf("codec set to %u\n", codec);
		} else {
			printf("set codec failed [%s]\n", chan->last_error);
		}

		for(x = 0; x < 25; x++) {
			unsigned char buf[2048];
			zap_size_t len = sizeof(buf);
			zap_wait_flag_t flags = ZAP_READ;

			if (zap_channel_wait(chan, &flags, -1) == ZAP_FAIL) {
				printf("wait FAIL! %u [%s]\n", (unsigned)len, chan->last_error);
			}
			if (flags & ZAP_READ) {
				if (zap_channel_read(chan, buf, &len) == ZAP_SUCCESS) {
					printf("READ: %u\n", (unsigned)len); 
				} else {
					printf("READ FAIL! %u [%s]\n", (unsigned)len, chan->last_error);
					break;
				}
			} else {
				printf("wait fail [%s]\n", chan->last_error);
			}
		}
		zap_channel_close(&chan);
	} else {
		printf("open fail [%s]\n", chan->last_error);
	}

	if(--runs) {
		goto top;
	}

	zap_global_destroy();
	return 0;
}