<?php
// Created on: 25 Dec 2001
// Usage: This is an included file for the PHP script which need to talk to OMCR_COMMAND table
// Return value: 0 if OK, -1 if error
// 
// Function: Send an nmi command to bsscomm via MySQL OMCR_COMMAND table
//
?>
<?php

/*
 * Some parameters are passed by reference
 */
function send_nmicommand($db,$bssid,$language,$nmicommand,&$resultcode,&$responsecode,&$response,$debug)
{
	$ok = 0;

	/* do it for ten times to search for a valid entry in OMCR_COMMAND */
	for ($i=0; $i<10 && $ok==0; $i++)
	{

		$sqlstring = "LOCK TABLES OMCR_COMMAND WRITE";
		$res = mysql_query($sqlstring, $db) or
			die("Invalid query: $sqlstring\n" . mysqli_error($pubConn));
		/* Get the largest sequence number */
		$sqlstring = "SELECT MAX(seqNum) from OMCR_COMMAND";
		//echo "$sqlstring<BR>\n";
		$res = mysql_query($sqlstring, $db) or
			die("Invalid query: $sqlstring\n" . mysqli_error($pubConn));
		list($max_seq_num) = mysqli_fetch_row($res);
		$max_seq_num++;
		mysql_free_result($res);

		/* Send Command to OMCR_COMMAND */
		$sqlstring = "UPDATE OMCR_COMMAND " .
			"SET bssid=$bssid, nmicommand='$nmicommand',seqNum=$max_seq_num,status=255,resultcode=-1 " .
			"WHERE status=0 AND seqNum<$max_seq_num LIMIT 1";
		//echo "$sqlstring<BR>\n";
		$res = mysql_query($sqlstring, $db) or
			die("Invalid query: $sqlstring\n" . mysqli_error($pubConn));
		if (mysql_affected_rows() == 1)
			$ok=1;
		$sqlstring = "UNLOCK TABLES";
		$res = mysql_query($sqlstring, $db) or
			die("Invalid query: $sqlstring\n" . mysqli_error($pubConn));
                if ($ok == 0)
			usleep(200000); // 200 msec
	}

	if ($ok == 0)
		return -1;	

	$timewait=0;
	$interval=500000;   // in micro-seconds (0.5 sec)

    	$timeout = 8;
	while ($timewait < 1000000 * $timeout)
	{
		$resultcode=$responsecode=$response=$cmd=null;
		usleep($interval);
		$timewait += $interval;
		$sqlstring = "SELECT resultcode, responsecode,response,nmicommand FROM OMCR_COMMAND " .
			"WHERE status=0 AND seqNum=$max_seq_num";
		//echo "$sqlstring<BR>\n";
		$res = mysql_query($sqlstring, $db) or
			die("Invalid query:$sqlstring\n" . mysqli_error($pubConn));
		while ( $mydata = mysqli_fetch_row($res) )
		{
			list($resultcode[],$responsecode[],$response[],$cmd[]) = $mydata;
		}
		$no_of_rows = mysqli_num_rows($res);
		mysql_free_result($res);

		if ($no_of_rows >= 1)
		{
			/* multiple entry due to duplicate seqNum is found
			 * Even deplicate seqNum, we can still wait for the result
			 */
			for ($i=0; $i<$no_of_rows; $i++)
			{
				if ($cmd[$i] == $nmicommand)
				{
					if ($i != 0)
					{
						$resultcode[0] = $resultcode[$i];
						$responsecode[0] = $responsecode[$i];
						$response[0] = $response[$i];
					}
					if ($resultcode[0] != 0)
					{
						if ($debug) echo "resultcode=$resultcode[0]<BR>";
						$sqlstring = "SELECT ${language}StatInfo FROM OMCR_STATCODE" .
							" WHERE statcode=$resultcode[0]";
						//echo "$sqlstring<BR>\n";
						$res = mysql_query($sqlstring, $db) or
							die("Invalid query:$sqlstring\n" . mysqli_error($pubConn));
						if ( $row = mysqli_fetch_row($res) )
							echo "<font color=red>" . $row[0] . "</font><BR>\n";;
						mysql_free_result($res);
					}
					return 0;	/* Result found */
				}
			}
		}
	}

        echo "Timeout without getting result from BSS OMC<BR>";
	return -1;	/* No result found */
}

?>
