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

📄 gc_basic_call_model.c

📁 dialogic 板卡globalcall 测试代码
💻 C
📖 第 1 页 / 共 5 页
字号:
/***************************************************************************
 *			NAME: void set_calling_num(struct channel *pline, char *phone_num)
 *DESCRIPTION: Sets the calling number for the outbound direction
 *		  INPUT: pline - pointer to entry in port table
 *					phone_num - pointer to the phone number
 *		RETURNS: None.
  ***************************************************************************/
static void set_calling_num(struct channel *pline, char *phone_num)
{
	char		str[MAX_STRING_SIZE];

	if (pline->direction == DIR_OUT) {
		if (gc_SetCallingNum(pline->ldev, phone_num) < 0) {
			sprintf(str, "gc_SetCallingNum(linedev=%ld, phone_num = %s) Failed", pline->ldev, phone_num);
			printandlog(pline->index, GC_APIERR, NULL, str);
			exitdemo(1);
		}
		sprintf(str, "gc_SetCallingNum(linedev=%ld, phone_num = %s) Success\n", pline->ldev, phone_num);
		printandlog(pline->index, MISC, NULL, str);
	}
}


/***************************************************************************
 *			NAME: void intr_hdlr()
 *DESCRIPTION: Handler called when one of the following signals is
 *					received: SIGHUP, SIGINT, SIGQUIT, SIGTERM.
 *					This function stops I/O activity on all channels and
 *					closes all the channels.
 *		  INPUT: None
 *		RETURNS: None.
  ***************************************************************************/
static void intr_hdlr(void)
{
	printf("*******Received User Interrupted Signal*******\n");
	interrupted = YES;
	/* will handle graceful shutdown outside this handler in main's forever loop */
}

/********************************************************************************
 *			NAME: load_config_file()
 *DESCRIPTION: This function reads the configuration file line
 *					by line. Ignores commented lines. For valid lines, 
 *					it fills the devname field of corresponding element 
 *					of port (global array of struct channel) as it is
 *					to be passed as an argument to gc_OpenEx(). It also
 *					fills the direction field in port[index]. 
 * 
 *		  INPUT: None
 *		RETURNS: None - exits if error
 *	  CAUTIONS: Assumes config file is in a "reasonable" format
 *					Not all fields are case insensitive
 *					The parsing in this procedure is simplified for demo purposes,
 *					i.e. error handling is weak to non-existent.
 *					Your application will need to implement parsing that is appropriate 
 *					for your application
 ********************************************************************************/
void load_config_file(void)
{
	char		line[MAX_STRING_SIZE], direction[MAX_STRING_SIZE];
	int		index;
	char		netdevice[MAX_DEVNAME], protname[MAX_DEVNAME], voicedevice[MAX_DEVNAME];
	char		devname[MAX_DEVNAME], destination_num[MAXPHONENUM];
	FILE		*cfg_fp;
	
	/* Open Configuration file in Read mode */
	cfg_fp = fopen(cfgfile, "r");
	
	if (cfg_fp == NULL) {
		printf("Unable to open %s for read\n", cfgfile);
		perror("Reason is: ");
		exit(1);
	}
	
	while (fgets(line, MAX_STRING_SIZE, cfg_fp)) {
		/* strip trailing white space */
		index = strlen(line) - 1;
		while (index && (line[index] <= ' ')) {
			line[index] = '\0';
			index--;
		}

		/* ignore blank lines */
		if (index == 0) {
			continue;
		}
			
		/* if 1st character is not alpha numeric, then treat the line as a comment & ignore it */
		if ((line[0] < 'A') || (line[0] > 'z')) {	/* simplifed non-alpha check */
			continue; /* Ignore the line and read next */

		}
		
		num_devices++; /* Increment Global Variable */

		/* Parse DTI Device, Protocol Name, Direction and Voice Device Name */ 
		sscanf(line, "%s%s%s%s%s", netdevice, protname, direction, voicedevice, destination_num);
		
		if ((netdevice[0] == '\0') || (protname[0] == '\0') || (direction == '\0')) {
			/* Invalid entry in the configuration file */
			fclose (cfg_fp);
			printf("Invalid entry in %s\n", cfgfile);
			printf("Line is: %s\n", line);
			exitdemo(1);
		}
		
		/* Fill devname to be passed as arg in gc_OpenEx() 
		in port array (global) of struct channel */
		
		if (strcmp(netdevice, "NONE") != 0) {
			sprintf(devname, ":L_SS7:N_%s:P_%s", netdevice, protname);
		} else {
			sprintf(devname, ":P_%s", protname);
		}
		/*Fill :V_ only if voice device name is not specified as NONE in cfg file */
		if (tolower(voicedevice[0]) != 'n') {
			strcat(devname, ":V_");
			strcat(devname, voicedevice);
		}
		
		/* Fill Direction in port array (global) of struct channel */
		if (tolower(direction[0]) == 'i') {
			port[num_devices - 1].direction = DIR_IN;
		} else {
			port[num_devices - 1].direction = DIR_OUT;
		}

		/* Fill device index in port array (global) of struct channel */
		port[num_devices - 1].index = num_devices - 1;

		/* Copying all the local variable values to Global Variables */
		strcpy(port[num_devices - 1].devname, devname);
		strcpy(port[num_devices - 1].netdevice, netdevice);
		strcpy(port[num_devices - 1].voicedevice, voicedevice);
		strcpy(port[num_devices - 1].protname, protname);
		strcpy(port[num_devices - 1].destination_num, destination_num);
	} /* End of read all lines loop */
	
	fclose (cfg_fp);
} /* End of Function load_config_file() */

/********************************************************************************
 *		 NAME: printandlog ()
 * DESCRIPTION: prints on the screen and to the log file. 
 *				 Put time stamp to the log file. If the
 *				 message to be logged is specific to a device,
 *				 it is logged to the device specific log file.
 * 
 *		INPUT: index is the device index
 *				 log_type valid values are ERR, MISC, EVENT, GC_APICALL, GC_APIERR and GC_RESULT_INFO
 *				 metaeventp = pointer to metaevent data structure - only used for GC_RESULT_INFO
 *				 msg_str message string to be printed
 *	 RETURNS: NA
 ********************************************************************************/
void printandlog(int index, int log_type, METAEVENT *metaeventp, char *msg_str)
{	
	struct tm			*Time;
	time_t				Timet;
	int					i;

#ifdef _WIN32	/* Defined in Windows */  
	struct _timeb	  Timeb;										/* used to get milliseconds */
#else			/* Defined in Unix	*/
	struct timeb		Timeb;	  
#endif
	 
	char					TimeStr[MAX_STRING_SIZE];			/* will hold the time string when built */
	char					buffer[MAX_STRING_SIZE];			/* will have everything but the time */
	char					timestamp[MAX_STRING_SIZE];		/* holds the formatted time */
	char					str[MAX_STRING_SIZE];
	char					gc_apierr[MAX_STRING_SIZE];		/* iff have GC API error - most of time will be NULL */
	char					gc_result_info[MAX_STRING_SIZE]; /* iff have GC result info request - most of time will be null */
		 
	/* initialize temporary buffers */
	buffer[0] = '\0';
	gc_apierr[0] = gc_result_info[0] = timestamp[0] = '\0';
	 
	/* Put time stamp	 - everything but the milliseconds */
	TimeStr[0] = '\0';
	time(&Timet);
	Time = localtime(&Timet);

#ifdef _WIN32	/* Defined in Windows */  
	_ftime(&Timeb);												/* needed to get the milliseconds */
#else			/* Defined in Unix	 */  
	ftime(&Timeb); 
#endif

	if (log_type == MISC_WITH_NL) {
		strcpy(timestamp, "\n");
	} 

	sprintf(TimeStr, "%02d/%02d %02d:%02d:%02d.%03d ", Time->tm_mon+1, Time->tm_mday, Time->tm_hour, Time->tm_min, Time->tm_sec, Timeb.millitm);
	strcat(timestamp, TimeStr);

	switch(log_type)
	{
		case GC_APICALL:
				strcat(buffer, " [GC_APICALL]: ");
				break;

		case GC_APIERR:
				strcat(buffer, " [GC_APIERR]:	 ");
				format_error_info(gc_apierr);
				break;

		case GC_RESULT_INFO:
				strcat(buffer, " [GC_RESULT_INFO]: ");
				format_result_info(index, metaeventp, gc_result_info);
				break;

		case NON_GC_APIERR:
				strcat(buffer, " [NON_GC_APIERR]: ");
				break;

		case EVENT:
				strcat(buffer, " [EVENT]:		 ");
				break;

		case MISC:
				strcat(buffer, " [MISC]:		 ");
				break;

		case MISC_WITH_NL:
				strcat(buffer, " [MISC]:		 ");
				break;

		case STATE:
				strcat(buffer, " [STATE]:		 ");
				append_call_state_name(buffer, index);
				break;

		default:
			sprintf(str, " [UNEXPECTED LOG TYPE %d]:	", log_type);
			strcat(buffer, str);
			break;
	} /* End of Switch */
	 
	strcat(buffer, msg_str);
	 

	/* now do the actual logging */
	/* 1st to screen */
	printf("%s\n", buffer);

	/* print GC error or GC result information if required */
	if (gc_apierr[0]) {
		printf("%s\n", gc_apierr);
	}
	if (gc_result_info[0]) {
		printf("%s\n", gc_result_info);
	}

	/* then to log file(s) */
	if (index == ALL_DEVICES) {
		for (i = 0; i < num_devices; i++) {
			if (port[i].log_fp) {
				fprintf(port[i].log_fp, "%s ", timestamp);
				fprintf(port[i].log_fp, "%s\n", buffer);
			}
		}
	} else if (port[index].log_fp) {
		fprintf(port[index].log_fp, "%s", timestamp);
		fprintf(port[index].log_fp, "%s\n", buffer);

		/* print GC error or GC result information if required */
		if (gc_apierr[0]) {
			fprintf(port[index].log_fp, "%s\n", gc_apierr);
		}
		if (gc_result_info[0]) {
			fprintf(port[index].log_fp, "%s\n", gc_result_info);
		}
	}
}

/********************************************************************************
 *			NAME: format_error_info()
 *DESCRIPTION: formats error information using gc_ErrorInfo()
 *		  INPUT: buffer - buffer to format into
 *		RETURNS: None
 *	  CAUTIONS: No integrity checking done on dest_buffer overflow
 ********************************************************************************/
static void format_error_info(char *dest_buffer)
{
	GC_INFO			  t_info;	  /* Structure which stores information about GC related errors */ 

	gc_ErrorInfo(&t_info);
			 
	strcpy(dest_buffer, "					 GC ERROR: ");
	strcat(dest_buffer, t_info.gcMsg);
	strcat(dest_buffer, "\n");


	strcat(dest_buffer, "					 CC Name: ");
	strcat(dest_buffer, t_info.ccLibName);
	strcat(dest_buffer, "\n");


	strcat(dest_buffer, "					 CC ERROR: ");
	strcat(dest_buffer, t_info.ccMsg);
	strcat(dest_buffer, "\n");

}

/********************************************************************************
 *			NAME: format_result_info()
 *DESCRIPTION: formats result information using gc_ResultInfo()
 *		  INPUT: index - index into port data structure
 *             metaeventp - pointer to metaevent structure
 *					buffer - buffer to format into
 *		RETURNS: None
 *	  CAUTIONS: No integrity checking done on dest_buffer overflow
 ********************************************************************************/
static void format_result_info(int index, METAEVENT *metaeventp, char *dest_buffer)
{
	char					str[MAX_STRING_SIZE];
	int					rc;
	GC_INFO				t_info;	  /* Structure which stores information about GC related errors */ 

	rc = gc_ResultInfo(metaeventp, &t_info);
	if (rc < 0) {
		sprintf(str, "gc_ResultInfo() call failed");
		printandlog(index, GC_APIERR, NULL, str);
		exitdemo(1);
	}
			 
	strcpy(dest_buffer, "					 GC Result: ");
	strcat(dest_buffer, t_info.gcMsg);
	strcat(dest_buffer, "\n");


	strcat(dest_buffer, "					 CC Name: ");
	strcat(dest_buffer, t_info.ccLibName);
	strcat(dest_buffer, "\n");


	strcat(dest_buffer, "					 CC Result: ");
	strcat(dest_buffer, t_info.ccMsg);
	strcat(dest_buffer, "\n");
}

/********************************************************************************
 *			NAME: append_call_state_name ()
 *DESCRIPTION: Appends the call state to a given buffer for a specified channel index
 *		  INPUT: buffer - buffer to append to
 *					index - index into port st

⌨️ 快捷键说明

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