camogm_interface.php 21.9 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
<?
/*!***************************************************************************
*! FILE NAME  : camogmgui.php
*! DESCRIPTION: command interface for the camogm recorder and camogm GUI
*! Copyright (C) 2008 Elphel, Inc
*! -----------------------------------------------------------------------------**
*!
*!  This program is free software: you can redistribute it and/or modify
*!  it under the terms of the GNU General Public License as published by
*!  the Free Software Foundation, either version 3 of the License, or
*!  (at your option) any later version.
*!
*!  This program is distributed in the hope that it will be useful,
*!  but WITHOUT ANY WARRANTY; without even the implied warranty of
*!  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
*!  GNU General Public License for more details.
*!
*!  You should have received a copy of the GNU General Public License
*!  along with this program.  If not, see <http://www.gnu.org/licenses/>.
*! -----------------------------------------------------------------------------**
*!  $Log: camogm_interface.php,v $
*!  Revision 1.23  2013/05/28 01:49:19  dzhimiev
*!  1. fixed list-command
*!
*!  Revision 1.22  2013/05/26 04:41:32  dzhimiev
*!  1. minor changes to avoid php hanging when not communicating with camogm
*!
*!  Revision 1.21  2012/04/14 01:25:12  dzhimiev
*!  1. added 'list' - shows files at the specified path
*!
*!  Revision 1.20  2011/06/02 12:29:07  oneartplease
*!  added new option to set_start_after_timestamp to allow setting record start time in the future without first having to read current camera time "p5" as parameter will start recording at camera time "p"lus 5 seconds
*!
*!  Revision 1.19  2011/04/04 20:19:36  dzhimiev
*!  1. added frame skipping option
*!
*!  Revision 1.18  2011/01/01 13:04:47  oneartplease
*!  added commands to set frameskipping and secondsskipping (timelapse)
*!
*!  Revision 1.17  2010/08/20 20:16:55  oneartplease
*!  cleaned up setcookie commands
*!
*!  Revision 1.16  2010/08/08 20:19:46  dzhimiev
*!  1. added write/read elphel parameters
*!  2. camogm: debuglev , start_after_timestamp
*!
*!  Revision 1.15  2010/06/24 17:31:55  dzhimiev
*!  1. added camogm process run check (ps | grep camogm)
*!
*!  Revision 1.14  2010/01/19 10:07:20  oneartplease
*!  added unmounting command
*!
*!  Revision 1.12  2009/10/13 12:42:31  oneartplease
*!  moved save_gp=1 command
*!
*!  Revision 1.11  2009/06/24 15:20:04  oneartplease
*!  file renaming works with absolute paths now as preparation for mounting multiple devices
*!
*!  Revision 1.5  2009/03/26 12:20:40  oneartplease
*!  audio and geotagging updates, both are not fully working yet though
*!
*!  Revision 1.9  2009/01/14 08:26:19  oneartplease
*!  minor updates
*!
*!
*!  Revision 1.0  2008/09/21 Sebastian Pichelhofer
*!  start and stop commands for ajax execution
*!
*/
$cmd = $_GET['cmd'];
$debug = $_GET['debug'];
$debuglev = $_GET['debuglev'];
73
$cmd_pipe = "/var/state/camogm_cmd";
74
$cmd_state = "/var/state/camogm.state";
75
$cmd_port = "3456";
76
$default_state = "/home/root/camogm.disk";
77
$start_str = "camogm -n " . $cmd_pipe . " -p " . $cmd_port;
78 79 80 81 82 83 84 85 86 87 88 89 90

if ($cmd == "run_camogm")
{
	$camogm_running = false;
	exec('ps | grep "camogm"', $arr); 
	function low_daemon($v)
	{
		return (substr($v, -1) != ']');
	}
	
	$p = (array_filter($arr, "low_daemon"));
	$check = implode("<br />",$p);
	
91 92
	$state_file = get_state_path();
	$start_str = $start_str . " -s " . $state_file;
93
	
94
	if (strstr($check, $start_str))
95 96 97
		$camogm_running = true;
	else
		$camogm_running = false;
98
		
99
	if(!$camogm_running) {
100 101
		exec("rm " . $cmd_pipe);
		exec("rm " . $cmd_state);
102

103
		if (!$debug) exec($start_str.' > /dev/null 2>&1 &'); // "> /dev/null 2>&1 &" makes sure it is really really run as a background job that does not wait for input
104
		else         exec($start_str.$debug.' > dev/null 2>&1 &');
105 106

		for($i=0;$i<5;$i++) {
107 108 109
			if (file_exists($cmd_pipe))
				break;
			sleep(1);
110 111 112 113 114
		}

		if ($debug) {
		    $fcmd = fopen($cmd_pipe, "w");
		    fprintf($fcmd,"debuglev=$debuglev");
115 116 117 118 119 120 121 122 123 124 125 126
		    fclose($fcmd);
		}
		
		// set fast recording mode if there is at least one suitable partition or revert to legacy 'mov' mode
		$partitions = get_raw_dev();
		if (!empty($partitions)) {
			reset($partitions);
			$cmd_str = 'format=jpeg;' . 'rawdev_path=' . key($partitions) . ';';
			write_cmd_pipe($cmd_str);
		} else {
			$cmd_str = 'format=mov;save_gp=1;';
			write_cmd_pipe($cmd_str);
127 128 129 130 131
		}
	}
}
else if ($cmd == "status")
{
132 133 134 135 136 137 138
	// is camogm running
	$camogm_running = "not running";
	
	exec('ps | grep "camogm"', $arr);
	function low_daemon($v)
	{
		return (substr($v, -1) != ']');
139
	}
140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164
	
	$p = (array_filter($arr, "low_daemon"));
	$check = implode("<br />",$p);
	
	if (strstr($check, $start_str))
		$camogm_running = "on";
	else
		$camogm_running = "off";
		
	if ($camogm_running=="on"){
		$pipe="/var/state/camogm.state";
		$mode=0777;
		if(!file_exists($pipe)) {
			umask(0);
			posix_mkfifo($pipe,$mode);
		}
		$fcmd=fopen($cmd_pipe,"w");
		fprintf($fcmd, "xstatus=%s\n",$pipe);
		fclose($fcmd);
	
		$status=file_get_contents($pipe);
	}else{
		$status = "<?xml version='1.0'?><camogm_state>\n<state>".$camogm_running."</state>\n</camogm_state>";
	}
		
165 166 167
	header("Content-Type: text/xml");
	header("Content-Length: ".strlen($status)."\n");
	header("Pragma: no-cache\n");
168
	printf("%s", $status);
169 170 171 172 173 174 175 176 177 178 179 180 181 182 183
}
else if ($cmd == "run_status")
{	
	// is camogm running
	$camogm_running = "not running";

	exec('ps | grep "camogm"', $arr); 
	function low_daemon($v)
	{
		return (substr($v, -1) != ']');
	}
	
	$p = (array_filter($arr, "low_daemon"));
	$check = implode("<br />",$p);
	
184
	if (strstr($check, $start_str))
185 186 187 188 189 190 191 192 193 194
		$camogm_running = "on";
	else
		$camogm_running = "off";
	

	$status="<?xml version='1.0'?><camogm_state>\n<state>".$camogm_running."</state>\n</camogm_state>";

	header("Content-Type: text/xml");
	header("Content-Length: ".strlen($status)."\n");
	header("Pragma: no-cache\n");
195
	printf("%s", $status);
196 197 198 199 200
}
else if ($cmd=="get_hdd_space"){
	if (isset($_GET['mountpoint']))
		$mountpoint = $_GET['mountpoint'];
	else
201
		$mountpoint = '/mnt/sda1';
202 203 204 205
		
        if (is_dir($mountpoint)) $res = disk_free_space($mountpoint);
        else                     $res = 0;
		
206 207 208
	xml_header();
	echo "<command>".$cmd."</command>";
	echo "<".$cmd.">";
209
	echo $res;
210 211 212 213 214 215 216 217
	echo "</".$cmd.">";
	xml_footer();

}
else if ($cmd=="mount") { // mount media like HDD
	if (isset($_GET['partition']))
		$partition = $_GET['partition'];
	else 
218 219
                $partition = '/dev/sda1';
		//$partition = '/dev/hda1';
220 221 222 223

	if (isset($_GET['mountpoint']))
		$mountpoint = $_GET['mountpoint'];
	else
Oleg Dzhimiev's avatar
Oleg Dzhimiev committed
224
		$mountpoint = '/mnt/sda1';
225 226
	
	exec('mkdir '.$mountpoint);
Oleg Dzhimiev's avatar
Oleg Dzhimiev committed
227
	//exec('mkdir /mnt/sda1');
228
	exec('mount '.$partition." ".$mountpoint);
Oleg Dzhimiev's avatar
Oleg Dzhimiev committed
229
	//exec('mount /dev/hda1 /mnt/sda1');
230 231 232 233 234 235 236
	xml_header();
	echo "<command>".$cmd."</command>";
	echo "<".$cmd.">";
	echo "done";
	echo "</".$cmd.">";
	xml_footer();
}
237
else if (($cmd=="umount") || ($cmd=="unmount")) { // unmount media like HDD
238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255
	$message = "";
	if (isset($_GET['mountpoint'])) {
		$mountpoint = $_GET['mountpoint'];
		exec('umount '.$mountpoint);
		$message = "done";
	} else {
		$message = "missing argument: mountpoint";
	}

	xml_header();
	echo "<command>".$cmd."</command>";
	echo "<".$cmd.">";
	echo $message;
	echo "</".$cmd.">";
	xml_footer();
}
else if ($cmd=="set_quality") {
	$quality = $_GET['quality'];
256
	$sensor_port = $_GET['sensor_port'];
257

258
	$thisFrameNumber=elphel_get_frame($sensor_port);
259
	elphel_set_P_value($sensor_port,ELPHEL_QUALITY,$quality+0,$thisFrameNumber+3);
260 261 262 263 264 265

	//$thisFrameNumber=elphel_get_frame();
	//elphel_wait_frame_abs($thisFrameNumber+3);
	xml_header();
	echo "<command>".$cmd."</command>";
	echo "<".$cmd.">";
266
	echo elphel_get_P_value($sensor_port,ELPHEL_QUALITY);
267 268 269 270 271
	echo "</".$cmd.">";
	xml_footer();
	
}
else if ($cmd=="get_quality") {
272
        $sensor_port = $_GET['sensor_port'];
273 274 275
	xml_header();
	echo "<command>".$cmd."</command>";
	echo "<".$cmd.">";
276
	echo elphel_get_P_value($sensor_port,ELPHEL_QUALITY);
277 278 279 280 281 282
	echo "</".$cmd.">";
	xml_footer();
}
else if ($cmd=="set_parameter") {
	$pname  = $_GET['pname'];
	$pvalue = $_GET['pvalue'];
283
	$sensor_port = $_GET['sensor_port'];
284

285
	//elphel_skip_frames($sensor_port,1);
286
	$thisFrameNumber=elphel_get_frame($sensor_port);
287 288 289 290 291 292
	
	if (isset($_GET['pframe'])) {
		$pframe = intval($_GET['pframe']);
	} else {
		$pframe = 3;
	}
293 294

	$constant=constant("ELPHEL_$pname");
295
	elphel_set_P_value($sensor_port,$constant,$pvalue+0,$thisFrameNumber+$pframe);
296 297 298 299

	xml_header();
	echo "<command>".$cmd."</command>";
	echo "<".$cmd.">";
300
	echo elphel_get_P_value($sensor_port,$constant);
301 302 303 304 305
	echo "</".$cmd.">";
	xml_footer();
}
else if ($cmd=="get_parameter") {
	$pname  = $_GET['pname'];
306
	$sensor_port = $_GET['sensor_port'];
307 308 309 310
	$constant=constant("ELPHEL_$pname");
	xml_header();
	echo "<command>".$cmd."</command>";
	echo "<".$cmd.">";
311
	echo elphel_get_P_value($sensor_port,$constant);
312 313 314 315 316
	echo "</".$cmd.">";
	xml_footer();
}
else if ($cmd=="set_skip") {
	$skipping_mask = $_GET['skip_mask'];
317 318
	//!!!sub!!!
	//exec("fpcf -w 4d $skipping_mask");
319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348
	xml_header();
	echo "<command>".$cmd."</command>";
	echo "<".$cmd.">";
	echo "done";
	echo "</".$cmd.">";
	xml_footer();
}
else if ($cmd=="list") {
	if (isset($_GET['path'])) $path = $_GET['path'];
	else {
	    $message = "the path is not set";
	    break;
	}

	if (is_dir($path)) {
	    $files = scandir($path);
	    foreach ($files as $file){
		if (is_file("$path/$file")) $message .= "<f>$path/$file</f>\n";
	    }
	}else{
	    $message = "directory not found";
	    break;
	}
	xml_header();
	echo "<command>".$cmd."</command>";
	echo "<".$cmd.">";
	echo $message;
	echo "</".$cmd.">";
	xml_footer();
}
349
else if ($cmd=="list_raw_devices"){
350 351 352 353 354 355 356 357 358 359 360 361 362
	$devices = get_raw_dev();

	xml_header();
	echo "<command>".$cmd."</command>";
	echo "<".$cmd.">";
	foreach ($devices as $device => $size) {
			echo "<item>";
			echo "<raw_device>" . $device . "</raw_device>";
			echo "<size>" . round($size / 1048576, 2) . "</size>";
			echo "</item>";
	}
	echo "</".$cmd.">";
	xml_footer();
363 364 365 366 367 368 369 370 371 372
}
else if ($cmd=="list_partitions"){
        $partitions = get_partitions();
        foreach ($partitions as $device=>$size) {
                echo "<item>";
                echo "  <device>" . $device . "</device>";
                echo "  <size>" . round($size / 1048576, 2) . "</size>";
                echo "</item>";
        }
}
373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394
else
{
	$fcmd = fopen($cmd_pipe, "w");
	
	xml_header();
	echo "<command>".$cmd."</command>";
	echo "<".$cmd.">";
	
	switch ($cmd)
	{
		case "start":
			fprintf($fcmd,"start;\n");
			break;
		case "stop":
			fprintf($fcmd,"stop;\n");
			exec('sync');
			break;	
		case "exit":
			fprintf($fcmd,"exit;\n");
			exec('sync');
			break;
		case "file_rename":
Oleg Dzhimiev's avatar
Oleg Dzhimiev committed
395
			// Now requires full path (like "/mnt/sda1/test1.mov") for file_old
396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432
			// and either a full path or just a filename for file_new 
			if ((!isset($_GET['file_old'])) || (!isset($_GET['file_new']))) {
				echo "wrong arguments";
				break;
			}

			$old_name = $_GET['file_old'];
			$new_name = $_GET['file_new'];
			
			if (!strrpos($new_name, "/")) {
				$new_name = substr($old_name, 0, strrpos($old_name, "/")+1).$new_name;
			}

			if ($old_name == $new_name) {
				echo "filenames match";
				break;
			}
			
			if (file_exists($new_name)) {
				echo "file_new already exists";
				break;
			}
			if (!file_exists($old_name)) {
				echo "file_old does not exist";
				break;
			}

			// no errors found, so do the rename			
			if (rename($old_name, $new_name)) 
				echo "done";
			else
				echo "undefined error";

			break;
		case "listdevices":
			exec ("cat /proc/partitions", $arr1);
			exec ("cat /proc/mounts", $arr2);
433 434
			$ret = get_mnt_dev();
			$mnt_dev = $ret["devices"];
435 436
			$j = 0;
			// first two lines are header and empty  line separator, skip them
437 438
			$i = 2;
			while($i < count($arr1)) {
439
				// skip flash and RAM disk partitions
440
				if (!strpos($arr1[$i], "mtdblock") && !strpos($arr1[$i], "ram")) {
441 442 443 444 445
					$temp = $arr1[$i];
					while(strstr($temp, "  ")) {
						$temp = str_replace(chr(9), " ", $temp); 
						$temp = str_replace("  ", " ", $temp);
					}
446 447 448 449 450 451 452
					if (preg_match_all('/ +[a-z]{3,3}[0-9]{1,1}$/', $temp, $available_partitons) > 0) {
						// remove leading spaces
						$partitions[$j] = preg_replace("/^ +/", "", $available_partitons[0][0]);
						$parts = explode(" ", $temp);
						$size[$j] = $parts[3];
						$j++;
					}
453 454 455
				}
				$i++;
			}
456 457
			$j = 0;
			foreach ($partitions as $partition) {
458 459 460 461
				$include = false;
				foreach ($mnt_dev as $dev) {
					if (strpos($dev, $partition))
						$include = true;
462
				}
463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490
				if ($include) {
					echo "<item>";
					echo "<partition>/dev/".$partition."</partition>";
					echo "<size>".round($size[$j]/1024/1024, 2) ." GB</size>";
					$j++;
					$i = 0;
					while($i < count($arr2)) {
						if(strpos($arr2[$i], $partition))
						{
							$parts = explode(" ", $arr2[$i]);
							$mountpoint = $parts[1];
							$filesystem = $parts[2];
						}
						$i++;
					}
					if ($mountpoint != "") {
						echo "<mountpoint>".$mountpoint."</mountpoint>";
						$mountpoint = "";
					} else {
						echo "<mountpoint>none</mountpoint>";
					}
					if ($filesystem != "") {
						echo "<filesystem>".$filesystem."</filesystem>";
						$filesystem = "";
					} else {
						echo "<filesystem>none</filesystem>";					
					}
					echo "</item>";
491
				}
492
			}
493
			break;
494 495 496 497
		case "mkdir":
			$dir_name = $_GET['name'];
			if (isset($dir_name) && (($dir_name != "") || ($dir_name != " ")))
			{
Oleg Dzhimiev's avatar
Oleg Dzhimiev committed
498
				exec('mkdir /mnt/sda1/'.$dir_name);
499 500 501 502 503 504 505 506 507
				echo "done";
				break;
			}
			else
				break;
		case "is_hdd_mounted":
			if (isset($_GET['partition']))
				$partition = $_GET['partition'];
			else 
508 509
                                $partition = '/dev/sda1';
				//$partition = '/dev/hda1';
510 511 512 513 514 515 516 517 518 519

			exec('mount', $arr);

			$mounted = implode("", $arr);
			if (strstr($mounted, $partition))
				echo substr($mounted, strpos($mounted, $partition),22);
			else
				echo "no HDD mounted";
			break;
		case "create_symlink":
Oleg Dzhimiev's avatar
Oleg Dzhimiev committed
520
			//exec('ln -s /mnt/sda1 /mnt/flash/html/hdd');
521 522 523 524 525 526
			
			if (isset($_GET['mountpoint'])) $mountpoint = $_GET['mountpoint'];
			else                            $mountpoint = "/mnt/sda1";
			
			exec("rm /www/pages/hdd");
			exec("ln -sf $mountpoint /www/pages/hdd");
527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546
			break;
// 		case "list":
// 			if (isset($_GET['path'])) $path = $_GET['path'];
// 			else {
// 			    echo "the path is not set";
// 			    break;
// 			}
// 
// 			if (is_dir($path)) {
// 			    $files = scandir($path);
// 			    foreach ($files as $file){
// 				if (is_file("$path/$file")) echo "<f>$path/$file</f>";
// 			    }
// 			}else{
// 			    echo "directory not found";
// 			    break;
// 			}
// 
// 			break;
		case "list_files":
547
			if (!file_exists('/www/pages/hdd')) {
548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572
				echo "no webshare found";
			break;
			}
			$dir = $_GET["dir"];
			
			if (isset($dir) && ($dir != "") && ($dir != "/./") && ($dir != "/"))  // show "one level up" item if we are not in "home" directory
			{
				echo "<file>";
				echo "<type>updir</type>";
				echo "<name>..</name>";
				$file_remove_last_slash = substr($dir, 0, strlen($dir)-1);
				$file_pos_of_second_last_slash = strrpos($file_remove_last_slash, "/");
				$up_file = substr($dir, 0, $file_pos_of_second_last_slash+1);
				while(strpos($up_file, "//")) {
					$up_file = str_replace("//", "/", $up_file); 
				}
				if ($up_file == "")
					echo "<path>/</path>";
				else
					echo "<path>".$up_file."</path>";
				echo "<size>0</size>";
				echo "<date>0</date>";
				echo "</file>";
			}
			
573
			if ($handle = opendir('/www/pages/hdd/'.$dir)) {
574 575 576 577 578 579
				while ($file = readdir($handle))
				{
					if ($file != "." && $file != "..")
					{	
						echo "<file>";
						echo "<type>";
580
						if (is_dir("/www/pages/hdd/".$dir.$file))
581 582 583 584 585 586
							echo "dir";
						else
							echo $extension = substr($file, strrpos($file, '.')+1, strlen($file)); 
						echo "</type>";
						echo "<name>".$file."</name>";
						echo "<path>".substr($dir, 1).$file."</path>";
587
						$size = filesize("/www/pages/hdd/".$dir.$file);
588
						echo "<size>".$size."</size>";
589 590 591 592
                                                if(!ini_get('date.timezone')){
                                                    date_default_timezone_set('GMT');
                                                }
						$date = date ("d M Y H:i:s", filectime("/www/pages/hdd/".$dir.$file));
593 594 595 596 597
						echo "<date>".$date."</date>";
						echo "</file>";
					}
				}
				closedir($handle);
598
			}else{
599
				echo "no webshare found<br>";
600
                        }
601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645
			break;
		case "set_prefix":
			$prefix = $_GET['prefix'];
			fprintf($fcmd, "prefix=%s;\n", $prefix);
			setcookie("directory", $prefix);
			break;
		case "set_debuglev":
			$debuglev = $_GET['debuglev'];
			fprintf($fcmd, "debuglev=%s;\n", $debuglev);
			break;
		case "set_duration":
			$duration = $_GET['duration'];
			fprintf($fcmd, "duration=%s;\n", $duration);
			break;
		case "set_size":
			$size = $_GET['size'];
			fprintf($fcmd, "length=%s;\n", $size);
			break;
		case "set_max_frames":
			$max_frames = $_GET['max_frames'];
			fprintf($fcmd, "max_frames=%s;\n", $max_frames);
			break;
		case "set_frames_per_chunk":
			$frames_per_chunk = $_GET['frames_per_chunk'];
			fprintf($fcmd, "frames_per_chunk=%s;\n", $frames_per_chunk);
			break;
		case "set_start_after_timestamp":
			$start_after_timestamp = $_GET['start_after_timestamp'];
			// Allow setting start timestamp as relative time with "p3" = plus 3 second
			// This allows triggering recording in the future without first reading current camera time
			if (substr($start_after_timestamp, 0, 1) == "p") { 
				$start_after_timestamp = elphel_get_fpga_time() + substr($start_after_timestamp, 1) ;
				//echo "now: ".elphel_get_fpga_time()." relative: ".$start_after_timestamp; //debug
			}
			fprintf($fcmd, "start_after_timestamp=%s;\n", $start_after_timestamp);
			break;
		case "set_frameskip":
			$frameskip_value = $_GET['frameskip'];
			fprintf($fcmd, "frameskip=%s;\n", $frameskip_value);
			break;
		case "set_timelapse":
			$timelapse_value = $_GET['timelapse'];
			fprintf($fcmd, "timelapse=%s;\n", $timelapse_value);
			break;
		case "init_compressor":
646 647
			$sensor_port = $_GET['sensor_port'];
			elphel_compressor_run($sensor_port);
648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672
			break;
		case "check_audio_hardware":
			exec('arecord -l', $arr1);
			$audio_hardware = implode("", $arr1);
			if ($audio_hardware == "")
				echo "no Audio Hardware detected";
			else
			{	
				$message = substr($audio_hardware, strpos($audio_hardware, "],")+2);
				$message = substr($message, 1, strpos($message, "[")-2); 
				echo $message;
			}
			break;
		case "test_audio_playback":
			$soundfile = $_GET['soundfile'];
			exec('aplay '.$soundfile);
			break;
		case "setmov":
			exec('echo "format=mov;" > /var/state/camogm_cmd'); // Set quicktime *.mov as default format after startup.
			exec('echo "save_gp=1;\n" > /var/state/camogm_cmd'); // enable calculation of free/used buffer space
			break;
		case "setjpeg":
			exec('echo "format=jpg;" > /var/state/camogm_cmd'); // Set quicktime *.mov as default format after startup.
			exec('echo "save_gp=1;\n" > /var/state/camogm_cmd'); // enable calculation of free/used buffer space
			break;
Oleg Dzhimiev's avatar
Oleg Dzhimiev committed
673 674 675
                case "setrawdevpath":
                        $rawdev_path = $_GET['path'];
                        exec('echo "rawdev_path='.$rawdev_path.';" > /var/state/camogm_cmd');
676
                        break;
677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696
		case "gettime":
			echo elphel_get_fpga_time();
			break;
	}
	if ($fcmd)
		fclose($fcmd);
	echo "</".$cmd.">";
	xml_footer();
}

function xml_header() {
	header("Content-type: text/xml"); 
	header("Pragma: no-cache\n");
	echo "<?xml version=\"1.0\" encoding=\"iso-8859-1\"?>\n";
	echo "<camogm_interface>\n";
}
function xml_footer() {
	echo "</camogm_interface>\n";
}

697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716
/** Get a list of suitable partitions. The list will contain SATA devices only and
 * will have the following format: "name" => "size_in_blocks".
 */
function get_partitions()
{
	$names = array();
	$regexp = '/([0-9]+) +(sd[a-z0-9]+$)/';
	exec("cat /proc/partitions", $partitions);

	// the first two elements of an array are table header and empty line delimiter, skip them
	for ($i = 2; $i < count($partitions); $i++) {
		// select SATA devices only
		if (preg_match($regexp, $partitions[$i], $name) == 1) {
			$names[$name[2]] = $name[1];
			$j++;
		}
	}
	return $names;
}

717 718
/** Get a list of disk devices which have file system and can be mounted. This function
 *  uses 'blkid' command from busybox.
719
 */ 
720 721
function get_mnt_dev()
{
722
	$partitions = get_partitions();
723 724
	$devices = array();
	$fs_types = array();
725 726 727 728 729 730 731 732 733 734 735 736
	foreach ($partitions as $partition => $size) {
		$res = array();
		$dev = "/dev/" . $partition;
		exec("blkid " . $dev, $res);
		if (!empty($res)) {
			$devices[$i] = preg_replace('/: +.*/', "", $res[0]);
			if (preg_match('/(?<=TYPE=")[a-z0-9]+(?=")/', $res[0], $fs) == 1)
				$fs_types[$i] = $fs[0];
			else
				$fs_types[$i] = "none";
			$i++;
		}
737
	}
738
	
739 740 741 742 743 744 745 746 747 748 749
	return array("devices" => $devices, "types" => $fs_types);
}

/** Get a list of devices whithout file system which can be used for raw disk storage from camogm. */
function get_raw_dev()
{
	$j = 0;
	$ret = get_mnt_dev();
	$devices = $ret["devices"]; 
	$types = $ret["types"];

750
	$names = get_partitions();
751 752 753 754
	
	// filter out partitions with file system 
	$i = 0;
	$raw_devices = array();
Oleg Dzhimiev's avatar
Oleg Dzhimiev committed
755

756
	foreach ($names as $name => $size) {
757 758 759 760 761 762 763
		$found = false;
		foreach ($devices as $device) {
			if (strpos($device, $name) !== false)
				$found = true;
		}
		if ($found === false) {
			// current partition is not found in the blkid list, add it to raw devices
764
			$raw_devices["/dev/" . $name] = $size;
765 766 767 768
			$i++;
		}
	}
	
769 770 771 772 773 774 775 776
	//special case
	if (count($raw_devices)>1) {
            foreach($raw_devices as $k=>$v){
                if (preg_match('/sd[a-z][0-9]/',$k)==0) {
                    unset($raw_devices[$k]);
                }
            }
	}
777 778
	return $raw_devices;
}
779

780 781 782 783 784 785 786 787 788 789 790 791
/** Check if camera was booted from NAND or SD card and modify the path where camogm will save
 * disk write pointer. */
function get_state_path()
{
	global $default_state;
	$prefix = '/tmp/rootfs.ro';
	
	if (file_exists($prefix)) {
		$ret = $prefix . $default_state;
	} else {
		$ret = $default_state;
	}
792

793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809
	return $ret;
}

/** Write command to camogm command pipe */
function write_cmd_pipe($cmd_str)
{
	global $cmd_pipe;
	
	$fcmd = fopen($cmd_pipe, 'w');
	if ($fcmd !== false) {
		fprintf($fcmd, $cmd_str);
		fflush($fcmd);
		fclose($fcmd);
	}
}

?>