<?php
//
// Created on: 22 Feb 2001
// Usage     : bsstable.php?language="eng|gb"
//                          &local_sys_id=0
//                          &remote_sys_id=0    <-- This is bssid
//                          &start_ip=0         <-- This is BSS IP
//                          &remark=0
//                          &local_E1=0
//                          [&ReturnURL=abc.php]
//
// Function  : Connect to a new BSS and update OMCR_BSSTABLE
//
//
// MySQL table: OMCR_BSSTABLE
// ../switch_keep/device_list/device_list.php -->
//     bsstable.php --> device_list.php --> modifyBssParam.php
//
// Return:
// device_list.php?save=1
//                         &local_sys_id=0
//                         &remote_sys_id=x    <-- This is bssid
//                         &start_ip=x         <-- This is BSS IP
//                         &remark=x
//                         &local_E1=x
// OR
// device_list.php?save=-1
//                         &local_sys_id=0
//                         &remote_sys_id=x    <-- This is bssid
//                         &start_ip=x         <-- This is BSS IP
//                         &remark=x
//                         &local_E1=x
//                         &err_info=Wrong IP|Wrong BSSID
//
?>
<?php
include("nocache.inc");
$DEBUG = 0;

$bssIdArr=explode('_',$sysId); 
$bssid = $bssIdArr[1] -0; // convert to integer


if($DEBUG)
{
    echo "Incomplete information<BR>";
    echo "mode=$mode<BR>";
    echo "sysId=$sysId<BR>";
    echo "ip=$ip<BR>";
	echo "bssid = $bssid<BR>";
}

$baseUrl="../configuration/systemList/systemConf.php?bssId=$bssid&mode=add&sysId=$sysId&ip=$ip&sysAlias=$remark";

//check in url
if(strlen($sysId) < 5 || $bssIdArr[1] == '' || $mode !='add'){
	$errorInfo = "This url's value error!";
	if($DEBUG) echo "<br>errorInfo=$errorInfo";
	$errorInfo=rawurlencode($errorInfo);
	$returnUrl=$baseUrl.'&errorInfo='.$errorInfo;
	if($DEBUG) echo "<br>returnUrl=$returnUrl";
	if(!$DEBUG) echo " <meta http-equiv=\"refresh\" content=\"0; url=$returnUrl\"> ";
	exit();
}

include("login_parm.inc");
$db = mysql_connect($hostname, $username, $password);
mysql_select_db($dbname,$db);

//check ip and bssid if have be created
$sqlstring = "SELECT bssid,ip FROM OMCR_BSSTABLE WHERE ip!='' AND bssid ='$bssid'";
if($DEBUG) echo "$sqlstring<BR>";
$resultSet = @mysql_query($sqlstring,$db);
$resultRowNum = @mysqli_num_rows($resultSet);
if($resultRowNum > 0){
	$resultRows=mysqli_fetch_array($resultSet);
	$bssId=$resultRows[bssid];
	$ip=$resultRows[ip];
	$sysId='500_'. $bssid.'_0';
	$returnUrl=$baseUrl;
	if($DEBUG) echo "<br>returnUrl=$returnUrl";
	if(!$DEBUG) echo " <meta http-equiv=\"refresh\" content=\"0; url=$returnUrl\"> ";
	exit();
}

//
// Find the available row in OMCR_COMMAND to insert nmi commands
// Get 10 available and insert 1 nmi commands
// 1. newbss <bssid> <ip addr>
//

$parm_name = split(",",$modified_data);
$nmicmd="newbss $bssid $ip";

/* Old MySql */
$sqlstring = "SELECT row_no FROM OMCR_COMMAND WHERE status=0 " .
             "ORDER BY seqNum ASC LIMIT 10";
//echo "$sqlstring<BR>";
$res = mysql_query($sqlstring ,$db) or
          die("Invalid query:$sqlstring\n" . mysqli_error($pubConn));
while (list($row_no[]) = mysqli_fetch_row($res));
$no_of_rows = mysqli_num_rows($res);
mysql_free_result($res);

if ( $no_of_rows <= 0 || ($no_of_rows > 10) )
{
    //
    // No available row in OMCR_COMMAND
    // Send error to MySQL()
    //
    $errorInfo = "Command table is full: Wait and send the command again";
	if($DEBUG) echo "<br>errorInfo=$errorInfo";
	$errorInfo=rawurlencode($errorInfo);
	$returnUrl=$baseUrl.'&errorInfo='.$errorInfo;
	if($DEBUG) echo "<br>returnUrl=$returnUrl";
	if(!$DEBUG) echo " <meta http-equiv=\"refresh\" content=\"0; url=$returnUrl\"> ";
	exit();
}

//
// Save "Name and remark" in OMCR_BSSTABLE
// A non-null IP field in OMCR_BSSTABLE means the entry is valid
//
$sqlstring = "UPDATE OMCR_BSSTABLE SET name='BSS$bssid', ip='$ip'," .
             "remark='$remark' WHERE bssid=$bssid";
if($DEBUG) echo "$sqlstring<BR>";
$res = mysql_query($sqlstring ,$db) or
          die("Invalid query:$sqlstring\n" . mysqli_error($pubConn));

//
// Insert 1 commands in OMCR_COMMAND out of 10 possible available rows
//
$i=0;
do
{
    if ($row_no[$i] != '')
    {
        $sqlstring = "REPLACE INTO OMCR_COMMAND VALUES(" .
                     $row_no[$i] . ",-1,'$nmicmd',null,255,-1,null,null,null)";
        if($DEBUG) echo "$sqlstring<BR>";
        $res       = mysql_query($sqlstring ,$db) or
                     die("Invalid query:$sqlstring\n" . mysqli_error($pubConn));
    }
    $i++;
} while ($i<10 && mysql_affected_rows() != 2); //replace=delete+insert (2 task)

$cmd1_row_no = $row_no[$i-1];

//
// Wait for response: timeout after 500ms x 20 = 10 secs
//
$i=0;
while ($i<20)
{
    usleep(500000);
    $sqlstring = "SELECT result,response from OMCR_COMMAND WHERE status=0 AND " .
                 "row_no=$cmd1_row_no ORDER BY seqNum ASC";
    if($DEBUG) echo "$sqlstring<BR>";
    $res       = mysql_query($sqlstring ,$db) or
                 die("Invalid query:$sqlstring\n" . mysqli_error($pubConn));
    while ( $mydata = mysqli_fetch_row($res) )
        list($result[],$response[]) = $mydata;
        //list($a[],$b[],$c[],$d[],$e[],$f[],$result[],$response[]) = $mydata;
    $no_of_rows = mysqli_num_rows($res);
    mysql_free_result($res);

    if ($no_of_rows == 1)
    {
        if ($result[0] != 'ok')
        {
            // Create/Delete failed !!!
			$errorInfo = "$result[0]: $response[0]";
			if($DEBUG) echo "<br>errorInfo=$errorInfo";
			$errorInfo=rawurlencode($errorInfo);
			$returnUrl=$baseUrl.'&errorInfo='.$errorInfo;
			if($DEBUG) echo "<br>returnUrl=$returnUrl";
			if(!$DEBUG) echo " <meta http-equiv=\"refresh\" content=\"0; url=$returnUrl\"> ";
			exit();
        }else{
            // Success !!!
			$returnUrl=$baseUrl;
			echo " <meta http-equiv=\"refresh\" content=\"0; url=$returnUrl\"> ";
			exit();
        }
        return;
    }
    $i++;
}

//
// Server has no response
// OMCR_COMMAND.status did not change to zero before timeout
//
$errorInfo = "No response";
if($DEBUG) echo "<br>errorInfo=$errorInfo";
$errorInfo=rawurlencode($errorInfo);
$returnUrl=$baseUrl.'&errorInfo='.$errorInfo;
if($DEBUG) echo "<br>returnUrl=$returnUrl";
if(!$DEBUG) echo " <meta http-equiv=\"refresh\" content=\"0; url=$returnUrl\"> ";
exit();
?>
