#!/usr/bin/php-cgi
<?php
header('Access-Control-Allow-Origin: *');

/****************************************************************************88

REDPORT GPS NMEA FUNCTION

AUTHOR: JOSE MARI REYES

HISTORY:

10-01-2017   VERSION 1.0

MANUAL:

			FIELDS:
			========
			enabled			on / off (bool)
			listen			integer
			baud			integer
			udp				integer
			tcp				integer
			usb				on / off (bool)
			sat				on / off (bool)
			usbwrite		on / off (bool)
			
			
			SET A FIELD (enabled)
			
			http://10.1.5.1/cgi-bin/nmea?enabled=on
			
			
			GET NMEA VALUES
			
			http://10.1.5.1/cgi-bin/nmea?read
			
				RETURN:
				
				listen='10101'
				baud='4800'
				udp='11101'
				tcp='11102'
				usb='off'
				sat='off'
				usbwrite='off'
				enabled='off'
				
				*the return is not ordered


			TO APPLY CHANGES
			
			http://10.1.5.1/cgi-bin/nmea?start	

********************************************************************************/

echo "<pre>";

	if (isset($_REQUEST['enabled']))
	{
	 	$v = $_REQUEST['enabled'];
		if (strlen($v) < 1) { echo -1; exit; }
		nmea("enabled", $v);
	}

	if (isset($_REQUEST['listen']))
	{
	 	$v = $_REQUEST['listen'];
		if (strlen($v) < 1) { echo -1; exit; }
		nmea("listen", $v);
	}

	if (isset($_REQUEST['baud']))
	{
	 	$v = $_REQUEST['baud'];
		if (strlen($v) < 1) { echo -1; exit; }
		nmea("baud", $v);
	}
	
	if (isset($_REQUEST['udp']))
	{
	 	$v = $_REQUEST['udp'];
		if (strlen($v) < 1) { echo -1; exit; }
		nmea("udp", $v);
	}
	
	if (isset($_REQUEST['tcp']))
	{
	 	$v = $_REQUEST['tcp'];
		if (strlen($v) < 1) { echo -1; exit; }
		nmea("tcp", $v);
	}
	
	if (isset($_REQUEST['usb']))
	{
	 	$v = $_REQUEST['usb'];
		if (strlen($v) < 1) { echo -1; exit; }
		nmea("usb", $v);
	}
	
	if (isset($_REQUEST['sat']))
	{
	 	$v = $_REQUEST['sat'];
		nmea("sat", $v);
	}
	
	if (isset($_REQUEST['usbwrite']))
	{
	 	$v = $_REQUEST['usvbwrite'];
		if (strlen($v) < 1) { echo -1; exit; }  
		nmea("usbwrite", $v);
	}
	
	if (isset($_REQUEST['read']))
	{
		echo get_nmea();
	}
	

	if (isset($_REQUEST['start']))
	{
		nmea_restart();
	}





function nmea_restart()
{
	$cmd = "/etc/init.d/gpsmon restart";
	exec($cmd);
}


function get_nmea()
{

/*$v = "nmea.@nmea[0]=nmea
nmea.@nmea[0].enabled='off'
nmea.@nmea[0].listen='10101'
nmea.@nmea[0].baud='4800'
nmea.@nmea[0].udp='11101'
nmea.@nmea[0].tcp='11102'
nmea.@nmea[0].usb='off'
nmea.@nmea[0].sat='off'
nmea.@nmea[0].usbwrite='off'";*/

	$cmd = "uci show nmea";
	$v = shell_exec($cmd);
	
	$v = str_replace("nmea.@nmea[0].", "", $v);
	$v = str_replace("nmea.@nmea[0]=nmea", "", $v);

	return $v;
	
}


function nmea($field, $v)
{
	$cmd = "uci set nmea.@nmea[0].$field='$v'";
	exec($cmd);
	exec("uci commit nmea");
}





?> 
