/* forbid_freq_check.h
 * ===================
 *
 * ver 1.00
 *
 * This is a collection of routines
 * which will be related to checking forbid_freq_table
 * in RCP's (Radar Control Programs)
 * for everyone who want to use...
 * 
 *				by Sessai@NIPR
 */

/* for show&check_forbid_freq...
 *    defined in forbid_freq.c in support.lib */

extern short int forbid_start_freq[];
extern short int forbid_end_freq[];
extern short int num_forbid_freq;

/* ----------------------------------------------------------------
 * forbid_freq table display, check & logging routine... by Sessai
 * ----------------------------------------------------------------
 * void show_forbid_freq(char *errlog);
 *
 *	show and log the forbid freq table loaded in support.lib
 *
 * void check_forbid_freq(char *errlog,short int freq_range,
 *		short int day_start_freq,short int night_start_freq);
 *
 *	check start_freq's with forbid_freq tables...
 *
 * void check_forbid_freq_sub(char *errlog,int sf,short int freq_range);
 *
 *	sub function for check_forbid_freq...
 *
 */

void show_forbid_freq(char *errlog) {

	int i;
	char log_message[80]="";
	char tmpstr[20];
	int k=3; //int k=5;

	sprintf(log_message,
		"FRQ: num_forbid_freq = %d, table entries are...",
			num_forbid_freq);
		log_error(errlog,"Control",log_message);
	for(i=0;i<num_forbid_freq;i++){
		if(i%k==0){
			strcpy(log_message,"FRQ: ");
		}
		sprintf(tmpstr,"%2d:%5d-%5d ",
			i,forbid_start_freq[i],forbid_end_freq[i]);
		strcat(log_message,tmpstr);
		if(i%k==(k-1))	log_error(errlog,"Control",log_message);
	}
		if(num_forbid_freq%k!=0){
				log_error(errlog,"Control",log_message);
		}
}

void check_forbid_freq(
	char *errlog,short int freq_range,
	short int day_start_freq,short int night_start_freq) {

	int f;
	int i=0;
	int status;
	int count=0;
	char log_message[80]="";

	sprintf(log_message,"FRQ: check_forbid_freq...");
		log_error(errlog,"Control",log_message);

	f=day_start_freq;
	    status=check_forbid_freq_sub(errlog,f,freq_range);
		count+=status;
	f=night_start_freq;
	    status=check_forbid_freq_sub(errlog,f,freq_range);
		count+=status;

	if(count==0){
		sprintf(log_message,"FRQ: check_forbid_freq...OK!");
			log_error(errlog,"Control",log_message);
	} else{
		sprintf(log_message,
			"FRQ: check_forbid_freq...%d WARNING(S)!!!");
			log_error(errlog,"Control",log_message);
		sprintf(log_message,
			"FRQ: CHECK FREQ you specified!!!!!!!!!!!!");
			log_error(errlog,"Control",log_message);
	}

}

int check_forbid_freq_sub(char *errlog,int sf,short int freq_range){

	int i;
	int status=0;
	char log_message[80]="";

		if( (sf < 8000) || (20000 < sf+freq_range) ){
			sprintf(log_message,
			    "FRQ: WAR: %d-%d BAD Freq Setting(Out of Range!!!)",
					sf,sf+freq_range);
			log_error(errlog,"Control",log_message);
			status++;
		}
	for(i=0;i<num_forbid_freq;i++){
		if( (sf+freq_range >= forbid_start_freq[i])
		 	&& (forbid_end_freq[i] >= sf) ){
			sprintf(log_message,
			    "FRQ: WAR: %d-%d <=> %d-%d BAD Freq Setting!!!",
				sf,sf+freq_range,
				forbid_start_freq[i],forbid_end_freq[i]);
			log_error(errlog,"Control",log_message);
			status++;
		}
	}
	return status;
}