<?php
/***************************************************************
File Name: sgList.php
File Relation: sgConf.php
File Include:  wxc2_omc/inc/header.inc
Function: Configure the sigling gateway parameter
Global Variable: 
Global function: 
Author: JianHui Zheng
Date: 2008-06-16

----- Modify -----
Description: 
***************************************************************/
	require("../../inc/header.inc");
?>

<script LANGUAGE="JavaScript">  
	function openSet(objIdStr,instanceNo){
		URL="./sgConf.php?instanceNo="+instanceNo+"&objIdStr="+objIdStr;
		var setWindow = open(URL, 'newWin', 'toolbar=no,directories=no,menubar=no,scrollbars=yes,resizable=yes,status=yes,width=400,height=350');
		setWindow.focus();
	}   
	
	function delFirm(instanceNo)
	{
		var delValue,x,y;
		x = document.body.scrollLeft;
		y = document.body.scrollTop;
	    
		if(confirm('Do you want to delete the selected?')!=true){
			window.location.href='<?php echo $PHP_SELF;?>'+"?scroll_x="+x+"&scroll_y="+y;
		}else{
			window.location.href='<?php echo $PHP_SELF;?>?&delete=1&delValue='+instanceNo+"&scroll_x="+x+"&scroll_y="+y;
		}
	}
</script>

<body leftmargin="15" rightmargin="10" onload="javascript:adjust();" onresize="javascript:adjust();" style="overflow:hidden;overflow-x:hidden;overflow-y:hidden;">

<?php
adjust_head();
adjust_title_head();

	//Global variable
	$sysId='222_99_99';
	$objIdStr="2.9";
	$nGetComm	= 0;
	$nSetComm	= 3;
	$maxTimeOut = 30;
	$nProcFail = 0;//failure
	$nWaitProc = 1;//wait
	$nInProc   = 2;
	$nOutProc  = 3;
	/*	
	SG attributes
	[unit]Enable[u]00.0-00.7[u]select
	[v]0[opt]Disable
	[v]1[opt]Enable
	[unit]Server[u]01.0-01.7[u]select
	[v]0[opt]Client
	[v]1[opt]Server
	[unit]ACK[u]02.0-02.0[u]select
	[v]0[opt]no ACK
	[v]1[opt]ACK
	[unit]SG Type[u]02.1-02.7[u]select
	[v]0[opt]M2UA
	[v]1[opt]IUA
	[v]2[opt]M3UA
	[unit]IP[u]03.0-06.7[u]input
	[v]keepHex-high-0
	*/
	$sgTableConfArr=array(
		array('SG Flag','00.0-00.7'),
		array('Application Mode','01.0-01.7'),
		array('Ack Option','02.0-02.0'),
		array('Signaling Type','02.1-02.7'),
		array('Remote IP','03.0-06.7')
	);

	$statusLabel=array(0=>"Disable",1=>"Enable");
	$serverLabel=array(0=>"Client",1=>"Server");
	$ackLabel=array(0=>"Allow",1=>"Forbid");
	$typeLabel=array(0=>"M2UA",1=>"IUA",2=>"M3UA");

	//Get the default value
	$OBJ_222="OBJ_222";
	$defSql = " SELECT initValue FROM paramConf WHERE keyId > 0 AND level_1 = 2 AND level_2 = 9 AND paramType=2";
	$defRes = @mysqli_query($pubConn,$defSql);
	$defRow = @mysqli_fetch_array($defRes);
	$defaultValue = $defRow[initValue];
	//echo "<br>defaultValue=$defaultValue<br>";
	
	//set and get parameter
	if(isset($setSys) || isset($getSys))
	{
		echo "<br><br><br><br><br>";
		if(isset($setSys))
		{
			$moveSql="UPDATE OBJ_222.param_99 SET readValue = initValue WHERE level_1='2' AND level_2='9' ";
		  if($DEBUG) echo "<BR>moveSql=$moveSql";
		  mysqli_query($pubConn,$moveSql);
		  echo mysqli_error($pubConn);
			$commIdStr=sysCommSend($sysId,$nSetComm,$objIdStr);
			$showType="Set";
		}
		else
		{
			$commIdStr=sysCommSend($sysId,$nGetComm,$objIdStr);
			$showType="Get";
		}
		$time = 0;
		while(1)
		{
			if($time == 0)
				echo "<br>$showType signaling gateway";
			if($time > $maxTimeOut)
			{
				echo "<br><font size=\"2\" color=\"#FF0000\">Timeout</font>";
				break;
			}
			echo "<img border=\"0\" src=\"../../images/arrow.gif\" width=\"10\" height=\"10\">";
			flush();	
			$sysId = "222_99_99";
			$chRes=ChComm($sysId,"0");
			//echo "<br>chRes=$chRes";
			if($chRes == $nOutProc || $chRes == $nProcFail)
			{
				if($chRes == $nProcFail)
					echo "<br><font size=\"2\" color=\"#FF0000\">$remark</font><br>";
				else if($remark == 'response timeout')
					echo "<br><font size=\"2\" color=\"#FF0000\">$remark</font><br>";
				else
					echo "<br><font size=\"2\" color=\"#0000FF\">$remark</font><br>";
				break;
			}
			sleep(1);
			$time++;
		}
		$moveSql = "UPDATE OBJ_222.param_99 SET initValue = readValue WHERE level_1='2' AND level_2='9' ";
		if($DEBUG) echo "<BR>moveSql=$moveSql";
		mysqli_query($pubConn,$moveSql);
		echo mysqli_error($pubConn);
	}
	//save parameter
	else if(isset($saveSys))
	{
		$updSql="UPDATE OBJ_222.param_99 SET initValue='02' WHERE level_1='3' AND level_2='7' ";
		$updRes = @mysqli_query($pubConn,$updSql);
		echo mysqli_error($pubConn);
		$commIdStr=sysCommSend($sysId,$nSetComm,'3.7');
		$time=0;
		while(1)
		{
			if($time == 0)
				echo "<br>Save parameter to system";
			if($time > $maxTimeOut)
			{
				echo "<br><font size=\"2\" color=\"#FF0000\">Timeout</font>";
				break;
			}
			echo "<img border=\"0\" src=\"../../images/arrow.gif\" width=\"10\" height=\"10\">";
			flush();	
			$sysId = "222_99_99";
			$chRes=ChComm($sysId,"0");
			//echo "<br>chRes=$chRes";
			if($chRes == $nOutProc || $chRes == $nProcFail){
				if($chRes == $nProcFail)
					echo "<br><font size=\"2\" color=\"#FF0000\">$remark</font><br>";
				elseif($remark == 'response timeout')
					echo "<br><font size=\"2\" color=\"#FF0000\">$remark</font><br>";
				else
						echo "<br><font size=\"2\" color=\"#0000FF\">$remark</font><br>";
				break;
			}
	
			sleep(1);
			$time++;
		}
	}
	//reset parameter
	else if(isset($resetSys))
	{
		$updSql="UPDATE OBJ_222.param_99 SET initValue = '01' WHERE  level_1 = '3' AND level_2 = '7' ";
		$updRes = @mysqli_query($pubConn,$updSql);
		echo mysqli_error($pubConn);
		$commIdStr=sysCommSend($sysId,$nSetComm,'3.7');
		if($DEBUG) echo "<BR>commIdStr=$commIdStr";
		if($DEBUG) echo "<BR>commIdStr=$commIdStr";
		$time=0;
		while(1)
		{
			//echo "<br>i=$i";
			if($time == 0)
				echo "<br>Reset system";
			if($time > $maxTimeOut){
				echo "<br><font size=\"2\" color=\"#FF0000\">Timeout</font>";
				break;
			}
			echo "<img border=\"0\" src=\"../../images/arrow.gif\" width=\"10\" height=\"10\">";
			flush();	
			$sysId = "222_99_99";
			$chRes=ChComm($sysId,"0");
			//echo "<br>chRes=$chRes";
			if($chRes == $nOutProc || $chRes == $nProcFail){
				if($chRes == $nProcFail)
					echo "<br><font size=\"2\" color=\"#FF0000\">$remark</font><br>";
				elseif($remark == 'response timeout')
					echo "<br><font size=\"2\" color=\"#FF0000\">$remark</font><br>";
				else
					echo "<br><font size=\"2\" color=\"#0000FF\">$remark</font><br>";
				break;
			}
			sleep(1);
			$time++;
		}
	}
	
	//delete
	if(1==$delete)
	{
		$delSql = "UPDATE OBJ_222.param_99 SET initValue = '$defaultValue' 
		            WHERE level_1='2' AND level_2='9' AND instanceNo='$delValue'";
		//echo "<br>delSql=$delSql";
		$delRes = @mysqli_query($pubConn,$delSql);
		echo mysqli_error($pubConn);
	}
	
	if($setSys || $getSys || $saveSys ||$resetSys){
		echo "<br>";
		echo "<p align=\"left\"><a href=\"$PHP_SELF?setFlag=$setFlag\" target=\"contents\"><img align=absBottom border=0 src=\"../../images/left.gif\" width=\"14\" height=\"14\">$strBack</a>";
		exit();
	}
	
	//Get the parameter
	$getSql="SELECT initValue,instanceNo  FROM param_99 WHERE level_1=2 AND level_2=9 ";
	$result	=mysqli_query($pubConn,$getSql);
	echo mysqli_error($pubConn);
	$sgCount=0;

	while($rows	= mysqli_fetch_array($result)){
		$instanceNo=$rows[instanceNo];
		$value=$rows[0];
		if(strcasecmp($value,$defaultValue)==0)
			continue;
		
		$sgInstance[$sgCount]=$instanceNo+0;
		$sgStatus[$sgCount]=getBitValue($value,$sgTableConfArr[0][1])+0;
		$sgServer[$sgCount]=getBitValue($value,$sgTableConfArr[1][1])+0;
		$sgACK[$sgCount]=getBitValue($value,$sgTableConfArr[2][1])+0;
		$sgType[$sgCount]=bindec(getBitValue($value,$sgTableConfArr[3][1]))+0;
		$sgIP[$sgCount]=ipHex2Dec(getBitValue($value,$sgTableConfArr[4][1]));
		
		//echo "value=$value,status:{$sgStatus[$sgCount]},server:{$sgServer[$sgCount]},ack:{$sgACK[$sgCount]},type:{$sgType[$sgCount]},IP:{$sgIP[$sgCount]}<br>";
		$sgCount++;
	}
	
	//show the page
?>
<?php
	$helpId="2.1.0";
	echo "<table id=\"table_up\" border=\"0\" width=\"100%\">";
	echo "<tr>";
	echo "<td width=\"50%\">";
	echo "$strConfiguration > $strNetwork > SS7 > Signaling Gateway";
	echo "</td>";
	echo "<td width=\"50%\" align=\"right\" title=\"Help\">";
	showHelp($helpId);
	echo "</td>";
	echo "</tr>";
	echo "<tr>";
	echo "<td align=\"left\">";
	
	echo "<form>";
	echo "<b>System:</b>&nbsp;&nbsp;";
	echo "<input type=\"submit\" name=\"getSys\" value=\"$strGet\" class=\"button\">";
	echo "<input type=\"submit\" name=\"setSys\" value=\"$strSet\" class=\"button\">";
	
	echo "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
	echo "<b>Command:</b>&nbsp;&nbsp;";
	// style=\"left: 1; top: 2; width: 60; height: 25; position: relative\"
	echo "<input type=\"submit\" name=\"saveSys\" value=\"$strSave\" class=\"button\">";
	echo "<input type=\"submit\" name=\"resetSys\" value=\"$strReset\" class=\"button\">";
	echo "<input type=\"hidden\" name=\"objIdStr\" value=\"2.9\">";
	
	echo "</td>";
	echo "<td>&nbsp;";
	echo "</td>";
	echo "</table>";

	

adjust_title_tail();
adjust_content_head();	
	echo "<br><table border=\"1\" width=\"100%\" cellpadding=\"1\" cellspacing=\"0\" bordercolor=\"#666666\" bordercolordark=\"#FFFFFF\">";
	echo "<tr bgcolor=\"#E6E6E6\">";
	echo "<td>No.</td>";
	echo "<td>SG Flag</td>";
	echo "<td>Application Mode</td>";
	echo "<td>Ack Option</td>";
	echo "<td>Signaling Type</td>";
	echo "<td>Remote IP</td>";
	echo "<td>Delete</td>";
	echo "</tr>";
	
	for($index=0;$index<$sgCount;$index++)
	{
		if($sgInstance[$index] == $setInstanceNo && $objIdStr==$setObjIdStr)
			echo "<tr bgcolor=#CCFFCC>";
		else
			echo "<tr>";
		echo "<td><a href=\"javascript:openSet('2.9','{$sgInstance[$index]}')\">";
		echo "{$sgInstance[$index]}</a>&nbsp;</td>";
		echo "<td>{$statusLabel[$sgStatus[$index]]}&nbsp;</td>";
		echo "<td>{$serverLabel[$sgServer[$index]]}&nbsp;</td>";
		echo "<td>{$ackLabel[$sgACK[$index]]}&nbsp;</td>";
		echo "<td>{$typeLabel[$sgType[$index]]}&nbsp;</td>";
		echo "<td>{$sgIP[$index]}&nbsp;</td>";
		echo "<td><input type=\"checkbox\" name=\"C$instanceNo\" value=\"ON\" onClick=\"javascript:delFirm({$sgInstance[$index]})\" ></td>";
		echo "</tr>";
	}
	echo "<tr>";
	echo "<td><a href=\"javascript:openSet('$objIdStr','new')\">";
	echo "New</a></td>";
	echo "<td>--</td>";
	echo "<td>--</td>";
	echo "<td>--</td>";
	echo "<td>--</td>";
	echo "<td>--</td>";
	echo "<td>--</td>";
	echo "</tr>";
	echo "</table>";
	echo "</form>";
	
	//function define
	function ChComm($sysId,$chkType)
	{     
		global $omc_db;
		global $remark,$nProcFail,$nWaitProc,$nInProc,$nOutProc;
	
		$commTable = "parameterComm";
		
		$sysIdArr = explode('_',$sysId);
		$sysTypeNo	= $sysIdArr[0];
		$sysNo		= $sysIdArr[1];
		$subSysNo	= $sysIdArr[2];
		
		if($chkType==0)
			$selComm = " SELECT commState,remark FROM $commTable WHERE sysTypeNo = '$sysTypeNo' ";
		else
			$selComm = " SELECT commState,remark FROM $commTable WHERE sysTypeNo = '$sysTypeNo' AND sysNo = '$sysNo' ";
		
		$selRes  = @mysqli_query($pubConn,$selComm);
		$selRow  = @mysqli_fetch_array($selRes);
		echo mysqli_error($pubConn);
		$outProcCount=0;
		do
		{
			$temCommState = $selRow[commState];
			if(isset($selRow[remark])){
				$remark = $selRow[remark];
				if($DEBUG)echo "<br>remark=$remark";
			}
			if($DEBUG)echo "<br>temCommState=$temCommState";
			if($temCommState == $nWaitProc){
				return($nWaitProc);
			}elseif($temCommState == $nInProc){
				return($nInProc);
			}elseif($temCommState == $nOutProc){
				$commState = $nOutProc;
				$outProcCount++;
			}else{
				$commState = $nProcFail;
			}
		}while($selRow  = @mysqli_fetch_array($selRes));
		if($outProcCount > 0){
			return($nOutProc);
		}else{
			return($nProcFail);
		}
	}
	
	
	function ipHex2Dec($ipHex)
	{
		$ipDec="";
		for($i=0;$i<4;$i++)
		{
			$temp=substr($ipHex,$i*2,2);
			$temp=hexdec($temp);
			$ipDec.=$temp.".";
		}
		$ipDec=substr($ipDec,0,strlen($ipDec)-1);
		return $ipDec;
	}
	

	function ipDec2Hex($ipDec)
	{
		$ipHex="";
		$unit=explode(".",$ipDec);
		for($i=0;$i<4;$i++)
		{
			$temp=dechex($unit[$i]);
			$temp=omc_keep_length($temp,"0","2","1");
			$ipHex.=$temp;
		}
		return strtoupper($ipHex);
	}
adjust_content_tail();
?>
</body>
</html>
