⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 libvoice.c

📁 VoiFax is a program that manage voice/data/fax modem in the same manner of vgetty and mgetty.VoiFax
💻 C
📖 第 1 页 / 共 2 页
字号:
			libevent_process_event("LV_DTMF #");	
			break;
		case 'a':
			libevent_process_event("LV_TONE FAX ANSWER");	
			break;
		case 'b':
			libevent_process_event("LV_TONE BUSY");
			break;
		case 'c':
			libevent_process_event("LV_TONE FAX CALLER");
			break;
		case 'd':
			libevent_process_event("LV_TONE DIALTONE");
			break;
		case 'e':
			libevent_process_event("LV_TONE DATA CALLER");
			break;
		case 'f':
			libevent_process_event("LV_TONE DATA ANSWER");
			break;
		case 'h':
			libevent_process_event("LV_ONHOOK");
			break;
		case 'q':
			libevent_process_event("LV_QUIET");
			break;
		case 's':
			libevent_process_event("LV_SILENCE");
			break;
		case 't':
			libevent_process_event("LV_OFFHOOK");
			break;
		case 'R':
			libvoice_ring_handler(NULL);
			break;
		default:
			if ((param[0] >= '0' && param[0] <= '9') ||		/* DTMF from 0 to 9 */
				(param[0] >= 'A' && param[0] <= 'D'))			/* DTMF from A to D */
			{
				strcpy(message, "LV_DTMF  ");
				message[8] = param[0];
				libevent_process_event(message);
			}

	}
	return true;
}
/*int libvoice_vcon_handler(char *param)
{
	return true;
}*/
/*int libvoice_connect_handler(char *param)
{
	return true;
}*/
int libvoice_ring_handler(char *param)
{
	/* solo per sicurezza e per evitare il warning in fase di compilazione */
	param = param;
	TRC(TRC_INFO, "libvoice_ring_handler", "Ring!!!");
	if (libvoice_lastring > time(NULL) + 5)
	{
		libvoice_rings = atoi(libconfig_getvalue(libvoice_device, "rings", "1"));
		return true;
	}
	else
	{
		if (libvoice_rings > 0) libvoice_rings--;
		libvoice_lastring = time(NULL);
		if (libvoice_rings == 0)
			libevent_process_event("LV_RINGING");
	}
	return true;
}

int libvoice_play(char *filename)
{
char module[] = "libvoice_play";
FILE *fd;
int readed;
char buffer[CHUNKSIZE];
int tot, written, ret, bytetoread;
char trc[200];
fd_set set;
struct timeval tv;
	
	if (libvoice_status != ST_VOICE)
		libvoice_sendcmd("VoiceMode");

	fd = fopen(filename, "r");
	if (fd==NULL)
	{
		TRC(TRC_ERR, module, "file not found");
		TRC(TRC_ERR, module, filename);
		return false;
	}

	/* Invia il comando di play al modem */
	/*modem_chat(&modem, voicecmd.StartPlay[0], "CONNECT");  */
	if (libvoice_sendcmd("StartPlay"))
	{
		libvoice_status = ST_VOICE_PLAYING;

		
		
		while (!feof(fd) && libvoice_status == ST_VOICE_PLAYING)
		{
			readed = fread(buffer, 1, CHUNKSIZE, fd);

			tot = 0;

			do
			{
				memset(&tv, 0, sizeof(tv));
				tv.tv_usec = 500000;
				FD_ZERO(&set);
				FD_SET(modem.w_fd, &set);

				ret = select(modem.w_fd + 1, NULL, &set, NULL, &tv);
				if (ret > 0)
				{	
					written = write(modem.w_fd, &buffer[tot], readed - tot);
					tot += written;
				}
			} while (tot < readed || ret <= 0 || written == -1);
			
			if (written == -1)
			{
				sprintf(trc, "Write response: %d, readed: %d", written, readed);
				TRC(TRC_ERR, module, trc);
				return false;
			}
			
			/* necessario per la gestione degli eventi */
			fcntl(modem.r_fd, FIONREAD, &bytetoread);
			if (bytetoread > 0)
			{
				readed = modem_receiveresp(&modem, buffer, CHUNKSIZE, false);

				/* se viene ricevuto OK, il modem ha terminato di effettuare il play in modo inaspettato!!! */
				if (memory_search(buffer, readed, "OK", 2) != -1)
					libvoice_status = ST_VOICE;
			}
		}

		libvoice_stop();
	}
	else
	{
		TRC(TRC_ERR, module, "error sending play command to modem");
		return false;
	}

	fclose(fd);
	return true;
}

int libvoice_record(char *filename)
{
char module[] = "libvoice_record";
FILE *fd;
char buffer[CHUNKSIZE];

	
	if (libvoice_status != ST_VOICE)
		libvoice_sendcmd("VoiceMode");

	fd = fopen(filename, "w");
	if (fd==NULL)
	{
		TRC(TRC_ERR, module, "error creating file");
		TRC(TRC_ERR, module, filename);
		return false;
	}

	/* Invia il comando di record al modem */
	/*modem_chat(&modem, voicecmd.StartPlay[0], "CONNECT");  */
	if (libvoice_sendcmd("StartRecord"))
	{
		libvoice_status = ST_VOICE_RECORDING;

		while (libvoice_status == ST_VOICE_RECORDING)
		{
			modem_receiveresp(&modem, buffer, CHUNKSIZE, false);
			fwrite(buffer, 1, CHUNKSIZE, fd);
		}
	}
	else
	{
		TRC(TRC_ERR, module, "error sending recording command to modem");
		return false;
	}

	fclose(fd);
	return true;
}

int libvoice_stop()
{
int ret;
char buffer[100];

	usleep(100000);
	ret = true;
	switch(libvoice_status)
	{
		case ST_VOICE_PLAYING:
			ret &= libvoice_sendcmd("StopPlay");
			break;

		case ST_VOICE_RECORDING:
			ret &= libvoice_sendcmd("StopRecord");
			break;

		default:
			libvoice_sendcmd("StopPlay");
			libvoice_sendcmd("StopRecord");
			ret = true;
			break;
	}

	if (ret == false && strcmp(libvoice_device, "ttySim") == 0)
	{
		while (strcmp(buffer, "OK") != 0)
			modem_receiveresp(&modem, buffer, 100, true);
	}
	
	libvoice_status = ST_VOICE;
	/*libevent_process_event("LV_READY");*/
	
	return ret;
}

void libvoice_processevent(void)
{
char buffer[100];
	modem_receiveresp(&modem, buffer, 100, false);
}

void libvoice_answer(void)
{
	libvoice_sendcmd("Answer");
}

void libvoice_hangup(void)
{
	libvoice_sendcmd("Hangup");
}

void libvoice_selectdialupline(void)
{
	libvoice_sendcmd("SelectDialupLine");
}

void libvoice_reset(void)
{
	libvoice_sendcmd("Reset");
}

int libvoice_setdevice(int device)
{
	switch (device)
	{
		case SD_NODEVICE:
			break;
			
		case SD_DIALUPLINE:
			return libvoice_sendcmd("SelectDialupLine");
			break;
			
		case SD_INTERNAL_MICROPHONE:
			return libvoice_sendcmd("SelectInternalMicrophone");
			break;
			
		case EXTERNAL_MICROPHONE:
			return libvoice_sendcmd("SelectExternalMicrophone");
			break;
			
		case INTERNAL_SPEAKER:
			return libvoice_sendcmd("SelectInternalSpeaker");
			break;
			
		case LOCAL_HANDSET:
			return libvoice_sendcmd("SelectLocalHandset");
			break;
			
		case EXTERNAL_SPEAKER:
			return libvoice_sendcmd("SelectExternalSpeaker");
			break;
			
		case DIALUP_WITH_INT_SPEAKER:
			return libvoice_sendcmd("SelectDialupWithIntSpeaker");
			break;
			
		case DIALUP_WITH_EXT_SPEAKER:
			return libvoice_sendcmd("SelectDialupWithExtSpeaker");
			break;
			
		case DIALUP_WITH_INTERNAL_MIC_AND_SPEAKER:
			return libvoice_sendcmd("SelectDialupWithInternalMicAndSpeaker");
			break;
			
		case DIALUP_WITH_EXTERNAL_MIC_AND_SPEAKER:
			return libvoice_sendcmd("SelectDialupWithExternalMicAndSpeaker");
			break;
			
		case DIALUP_WITH_LOCAL_HANDSET:
			return libvoice_sendcmd("SelectDialupWithLocalHandset");
			break;
	}
	return false;
}

int libvoice_beep(int freq, int length)
{
	int argc;
	char args[2][MAXCMDSIZE];
	char *argv[2];

	argc = 2;

	argv[0] = &args[0][0]; argv[1] = &args[1][0];
	
	sprintf(argv[0], "%d", freq);
	sprintf(argv[1], "%d", length);
	
	return libvoice_sendcmdwithargs("Beep", argc, argv);
}

int libvoice_senddtmf(char digit, int length)
{
	int argc;
	char args[2][MAXCMDSIZE];
	char *argv[2];

	argc = 2;

	argv[0] = &args[0][0]; argv[1] = &args[1][0];
	
	sprintf(argv[0], "%c", digit);
	sprintf(argv[1], "%d", length);
	
	return libvoice_sendcmdwithargs("SendDTMF", argc, argv);
}

int libvoice_dial(char *number)
{
	int argc;
	char args[MAXCMDSIZE];
	char *argv[1];

	argc = 1;

	argv[0] = &args[0];
	
	sprintf(argv[0], "%s", number);

	return libvoice_sendcmdwithargs("Dial", argc, argv);
}

char *libvoice_getty(void)
{
	return libvoice_device;
}

void libvoice_shutdown(void)
{
	libvoice_sendcmd("DataMode");
	libvoice_sendcmd("Reset");
	modem_close(&modem);
}

int libvoice_wait(int seconds)
{
	time_t start;
	start = time(NULL);
	
	while (time(NULL) - start < seconds)
		libvoice_processevent();

	return true;
}

int libvoice_quote(char *cmd, char *rsp)
{
	return modem_chat(&modem, cmd, rsp);
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -