Commit 2be574ef authored by Andrey Filippov's avatar Andrey Filippov

Updating to match camogm behavior

parent 4a35837c
......@@ -2,7 +2,7 @@
/*!***************************************************************************
*! FILE NAME : logger_launcher.php
*! DESCRIPTION: launches the event logger (IMU/GPS/External trigger/Other sensor) a dies
*! Copyright (C) 2012 Elphel, Inc
*! Copyright (C) 2012-2023 Elphel, Inc
*! -----------------------------------------------------------------------------**
*!
*! This program is free software: you can redistribute it and/or modify
......@@ -37,23 +37,59 @@
*!
*!
*/
define('SUFFIX_LEN', 5); // file suffix length
define('LOGGER_CONF', '/etc/elphel393/imu_logger.xml');
define('BIN_NAME', 'log_imu');
define('DEFAULT_MNT', '/mnt/sda1');
define('DEFAULT_DIR', 'test00');
define('DEFAULT_FILE', 'log_file');
define('DEFAULT_FLEN', 5000000); // 5 M records, 64 bytes each (total 320MB)
define('LOGGER_MNT', 'logger_mnt');
define('LOGGER_DIR', 'logger_dir');
define('LOGGER_FILE', 'logger_file');
define('LOGGER_FLEN', 'logger_flen');
include 'include/show_source_include.php';
set_include_path ( get_include_path () . PATH_SEPARATOR . '/www/pages/include' );
include 'show_source_include.php';
include "elphel_functions_include.php"; // includes curl functions
//default parameters
$cmd = "start";
$file = "/mnt/sda1/imu_log.log";
$index = 1;
$n = 5000000;
$mount_point = "/mnt/sda1";
//$cmd = "start";
//$file = "/mnt/sda1/imu_log.log";
//$index = 1;
//$n = 5000000;
//$mount_point = "/mnt/sda1";
$force_dev = false;
$xml = new SimpleXMLElement("<?xml version='1.0' standalone='yes'?><Document/>");
if (file_exists(LOGGER_CONF)) {
$config_xml = simplexml_load_file(LOGGER_CONF);
$logger_config = loggerConfigFromXML($config_xml);
/*
echo "<pre>";
var_dump($logger_config);
echo "</pre>";
exit (0);
*/
} else {
$xml->addChild('error', 'no config');
$xml->addChild('description', 'Configuarion file '.LOGGER_CONF.' does not exist. It is created by /usr/bin/start_ims.php');
send_response($xml);
exit(1);
}
if (!isset($logger_config[LOGGER_MNT])) $logger_config[LOGGER_MNT] = DEFAULT_MNT;
if (!isset($logger_config[LOGGER_DIR])) $logger_config[LOGGER_DIR] = DEFAULT_DIR;
if (!isset($logger_config[LOGGER_FILE])) $logger_config[LOGGER_FILE] = DEFAULT_FILE;
if (!isset($logger_config[LOGGER_FLEN])) $logger_config[LOGGER_FLEN] = DEFAULT_FLEN;
$xml = "<Document>\n";
if (isset($_GET[LOGGER_MNT])) $logger_config[LOGGER_MNT] = $_GET[LOGGER_MNT];
if (isset($_GET[LOGGER_DIR])) $logger_config[LOGGER_DIR] = $_GET[LOGGER_DIR];
if (isset($_GET[LOGGER_FILE])) $logger_config[LOGGER_FILE] = $_GET[LOGGER_FILE];
if (isset($_GET[LOGGER_FLEN])) $logger_config[LOGGER_FLEN] = $_GET[LOGGER_FLEN];
//get parameters
if (isset($_GET['cmd'])) $cmd = $_GET['cmd'];
/*
if (isset($_GET['file'])) $file = $_GET['file'];
if (isset($_GET['index'])) $index = $_GET['index'];
if (isset($_GET['n'])) $n = $_GET['n'];
......@@ -63,54 +99,153 @@ if (isset($_GET['dev'])) {
$dev = $_GET['dev'];
$force_dev = true;
}
*/
if ($cmd=="start") {
if (!is_dir($mount_point)) mkdir($mount_point);
//detect devices
//$dev = "/dev/hda1";
if (!$force_dev) {
$sda1 = exec("cat /proc/diskstats | grep 'sda1'");
if (strlen($sda1)>0) $dev = "/dev/sda1";
else {
$xml .= "\t<error>CF cards not found</error>\n";
switch ($cmd) {
case 'readconf':
$xml = loggerConfigToXML($logger_config);
break;
case 'writeconf':
$xml = loggerConfigToXML($logger_config);
$conf_file = fopen ( LOGGER_CONF, "w" );
fwrite ( $conf_file,$xml->asXML () );
fclose ( $conf_file );
break;
case 'stop':
$proc = getPIDByName('log_imu');
$was_running = count($proc) > 0;
$xml->addChild('prev_state', $was_running ? 'RUNNING' : 'STOPPED');
if ($was_running){
exec("killall -1 ".BIN_NAME);
}
// unmount
// exec("umount $mount_point");
exec("sync");
add_state_to_xml($xml);
break;
case 'start':
$proc = getPIDByName('log_imu');
$was_running = count($proc) > 0;
$xml->addChild('prev_state', $was_running ? 'RUNNING' : 'STOPPED');
if ($was_running) {
$xml->addChild('error', 'was running');
add_state_to_xml($xml);
send_response($xml);
break;
}
$dir_path = $logger_config[LOGGER_MNT];
if ($logger_config[LOGGER_DIR]) {
$dir_path .= '/'.$logger_config[LOGGER_DIR];
}
$pref_path = $dir_path.'/'.$logger_config[LOGGER_FILE];
$xml->addChild('dir_path', $dir_path);
$xml->addChild('pref_path', $pref_path);
$indx = get_next_index($dir_path, $logger_config[LOGGER_FILE]);
if ($indx == -1){ // create directory recursive if it does not exist
$xml->addChild('mkdir', $dir_path);
if (! mkdir($dir_path, 0777, true)){
$xml->addChild('error', 'mkdir error');
send_response($xml);
exit(0);
}
$indx = get_next_index($dir_path, $logger_config[LOGGER_FILE]);
}
if ($indx == -1) {
$xml->addChild('error', 'write dir does not exist');
break;
}
if ($indx == -2) {
$xml->addChild('error', 'write dir is not a directory');
break;
}
$exe = '/usr/bin/'.BIN_NAME;
$flen = $logger_config[LOGGER_FLEN];
exec("$exe $pref_path $indx $flen >/dev/null 2>&1 &");
add_state_to_xml($xml);
break;
exec("mount $dev $mount_point");
exec("/usr/bin/log_imu $file $index $n >/dev/null 2>&1 &");
$xml .= "\t<result>ok</result>\n";
case 'state':
add_state_to_xml($xml);
break;
case 'next_index':
$dir_path = $logger_config[LOGGER_MNT];
if ($logger_config[LOGGER_DIR]) {
$dir_path .= '/'.$logger_config[LOGGER_DIR];
}
$xml->addChild('dir_path', $dir_path);
$xml->addChild('pref_path',$dir_path.'/'.$logger_config[LOGGER_FILE]);
$indx = get_next_index($dir_path, $logger_config[LOGGER_FILE]);
switch ($indx) {
case -1:
$xml->addChild('error', 'write dir does not exist');
break;
case -2:
$xml->addChild('error', 'write dir is not a directory');
break;
default:
$xml->addChild('next_index', $indx);
}
break;
}
send_response($xml);
exit(0);
if ($cmd=="stop") {
exec("killall -1 log_imu");
//unmount
//exec("umount $mount_point");
exec("sync");
$xml .= "\t<result>ok</result>\n";
function add_state_to_xml(&$xml){
$proc = getPIDByName('log_imu');
if (count($proc)) {
$xml->addChild('state', 'RUNNING');
if (count($proc[0]['argv']) > 1){
$xml->addChild('outfile', $proc[0]['argv'][1]);
if (count($proc[0]['argv']) > 2){
$xml->addChild('start_index', $proc[0]['argv'][2]);
if (count($proc[0]['argv']) > 3){
$xml->addChild('num_records', $proc[0]['argv'][3]);
if (count($proc[0]['argv']) > 4){
$xml->addChild('rewind_by', $proc[0]['argv'][4]);
}
}
}
}
} else {
$xml->addChild('state', 'STOPPED');
}
}
$xml .= "</Document>";
send_response($xml);
function get_next_index($dir_path, $logger_file) {
if (!file_exists($dir_path)) {
return -1;
} else if (!is_dir($dir_path)) {
return -2;
}
$indx = -1;
$dirlist = scandir($dir_path, SCANDIR_SORT_ASCENDING);
$prefix_len = strlen($logger_file);
$entry_len = $prefix_len+1+SUFFIX_LEN;
foreach ($dirlist as $logfile) {
if (strlen($logfile) == $entry_len) {
if (strncmp($logger_file, $logfile, $prefix_len) === 0){
$i = intval(substr($logfile,-SUFFIX_LEN));
if ($i > $indx){
$indx=$i;
}
}
}
}
return $indx+1;
}
function send_response($xml){
$rslt=$xml->asXML();
header("Content-Type: text/xml");
header("Content-Length: ".strlen($xml)."\n");
header("Content-Length: ".strlen($rslt)."\n");
header("Pragma: no-cache\n");
printf("%s", $xml);
printf($rslt);
flush();
}
function _help(){
echo "<pre>\n";
echo "Usage example: 'http://192.168.0.9/logger_launcher.php?file=/mnt/sda1/test.log&index=1&n=1000000&dev=/dev/sda1', where\n";
echo "Usage example (OBSOLETE): 'http://192.168.0.9/logger_launcher.php?file=/mnt/sda1/test.log&index=1&n=1000000&dev=/dev/sda1', where\n";
echo "'file'- log name (includes absolute path), '/usr/html/CF/' is the 'dev's mount point\n";
echo "'index'- index added to the log name\n";
echo "'n'- max number of records in a single log file\n";
......@@ -120,5 +255,74 @@ function _help(){
function _usage(){
_help();
}
function loggerConfigFromXML($confXML) {
$conf = array ();
// var_dump($confXML);
foreach ( $confXML as $item ) {
// var_dump($item);
$key = $item->getName ();
switch ($key) {
case 'imu_registers' :
$conf [$key] = array ();
foreach ( $item as $reg ) {
$rname = substr ( $reg->getName (), 2 );
$conf [$key] [$rname + 0] = (( string ) $reg) + 0;
}
break;
case 'nmea' :
$conf [$key] = array ();
foreach ( $item as $reg ) {
$rname = substr ( $reg->getName (), 2 );
$conf [$key] [$rname + 0] = array (
( string ) $reg->sentence,
( string ) $reg->format
);
}
break;
case 'imu_period' :
$conf [$key] = ( string ) $item;
if ($conf [$key] != 'auto')
$conf [$key] += 0;
break;
case 'message' :
case 'logger_mnt' :
case 'logger_dir' :
case 'logger_file' :
$conf [$key] = ( string ) $item;
break;
default :
// $conf[$key]=(int) $item;
$conf [$key] = (( string ) $item) + 0;
}
}
return $conf;
}
function loggerConfigToXML($conf) {
$logger_xml = new SimpleXMLElement ( "<?xml version='1.0' standalone='yes'?><Logger_configuration/>" );
foreach ( $conf as $key => $value ) {
switch ($key) {
case 'imu_registers' :
$subtree = $logger_xml->addChild ( $key );
for($i = 0; $i < count ( $value ); $i ++) {
$subtree->addChild ( 'R_' . sprintf ( '%02d', $i ), sprintf ( "0x%02x", $value [$i] ) );
}
break;
case 'nmea' :
$subtree = $logger_xml->addChild ( $key );
for($i = 0; $i < count ( $value ); $i ++) {
$sentence = $subtree->addChild ( 'S_' . sprintf ( '%01d', $i ) );
$sentence->addChild ( 'sentence', $value [$i] [0] );
$sentence->addChild ( 'format', $value [$i] [1] );
}
break;
default :
$logger_xml->addChild ( $key, $value );
}
}
return $logger_xml;
}
?>
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment