<?php
//
// Created on: 22 Feb 2001
// Usage     : bsstable.php?language="eng|gb"&bssid=0
//             [&modified_data="Save is Completed]
// Function  : Connect to a new BSS and update OMCR_BSSTABLE
//
//
// MySQL table: OMCR_BSSTABLE
// newbss.php --> bsstable.php --> modifyBssParam.php
//
?>
<?php
include("nocache.inc");

//
// if nothing has been modified, reload the original page
//
if ($modified_data == '')
{
    header("Location: " .
        "newbss.php?language=$language&bssid=$bssid" .
        "&objectid=$objectid&tablename=$tablename" .
        "&status='Save not needed'"
    );
    return;
}
if (!$ip && !$bssid)
{
    header("Location: " .
        "newbss.php?language=$language&bssid=$bssid" .
        "&objectid=$objectid&tablename=$tablename" .
        "&status='Too few parametes'"
    );
    return;
}

//
// Find the available row in OMCR_COMMAND to insert nmi commands
// Get 10 available and insert 1 nmi commands
// 1. newbss <bssid> <ip addr>
//
include("login_parm.inc");
$db = mysql_connect($hostname, $username, $password);
mysql_select_db($dbname,$db);

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

$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()
    //
    header("Location: " .
        "newbss.php?language=$language&bssid=$bssid" .
        "&objectid=$objectid&tablename=$tablename" .
        "&status='Command table is full: Wait and send the command again'"
    );
    return;
}

//
// 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='$name',remark='$remark' " .
             "WHERE bssid=$bssid";
//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)";
        //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);

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

//
// Wait for response: timeout after 500ms x 10 = 5 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";
    //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[],$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 !!!
            header("Location: " .
                "newbss.php?language=$language&bssid=$bssid" .
                "&objectid=$objectid" .
                "&tablename=$tablename" .
                "&status=$result[0]: $response[0]"
            );
        }
        else
        {
            // Success !!!
            header("Location: " .
                "modifyBssParam.php?language=$language&bssid=$bssid" .
                "&objectid=$objectid" .
                "&tablename=$tablename" .
                "&mode=newbss" .
                "&status=Creation command is sent"
            );
        }
        return;
    }
    $i++;
}

//
// Server has no response
// OMCR_COMMAND.status did not change to zero before timeout
//
header("Location: " .
    "newbss.php?language=$language&bssid=$bssid" .
    "&objectid=$objectid&tablename=$tablename" .
    "&status=No response"
);
?>
