eyesis4pi_interface.php 6.99 KB
Newer Older
1
<?php
Oleg Dzhimiev's avatar
Oleg Dzhimiev committed
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
/**
 * @file eyesis4pi_interface.php
 * @brief command interface for the eyesis4pi gui
 * @copyright Copyright (C) 2016 Elphel Inc.
 * @author Oleg Dzhimiev <oleg@elphel.com>
 *
 * @par <b>License</b>:
 *  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/>.
21 22
*/

23
include 'include/elphel_functions_include.php';
Oleg Dzhimiev's avatar
Oleg Dzhimiev committed
24

25 26 27 28 29 30
$cmd = "donothing";
if (isset($_GET['cmd']))
  $cmd = $_GET['cmd'];
else if (isset($argv[1]))
  $cmd = $argv[1];

Oleg Dzhimiev's avatar
Oleg Dzhimiev committed
31
#hardcoded for eyesis4pi
32 33
$symlink = "/www/pages/ssd";
$mountpoint = "/mnt/sda1";
34

35 36
//$camogmdisk = "/home/root/camogm.disk";
$camogmdisk = "/mnt/sda1/camogm.disk";
37 38
$nandbootpath = "/tmp/rootfs.ro";

Oleg Dzhimiev's avatar
Oleg Dzhimiev committed
39
//if (is_dir($nandbootpath)) $camogmdisk = $nandbootpath.$camogmdisk; 
40 41 42 43 44 45 46

$sysfs_lba_path = "/sys/devices/soc0/amba@0/80000000.elphel-ahci/";

$file_lba_start = $sysfs_lba_path."lba_start";
$file_lba_current = $sysfs_lba_path."lba_current";
$file_lba_end = $sysfs_lba_path."lba_end";

47
switch($cmd){
48 49 50 51 52
  case "check_imu":
        if (is_link("/dev/imu")) $res = 1;
        else                     $res = 0;
        echo $res;
        break;
53
  case "camogm_debug":
54 55 56 57 58 59 60 61 62 63 64 65 66
        if (isset($_GET['debuglev'])){
          $debuglev = $_GET['debuglev'];
        }else{
          $debuglev = 3;
        }
        
        if (isset($_GET['debug'])){
          $debug = $_GET['debug'];
        }else{
          $debug = "/tmp/camogm.log";
        }        
        
        exec("echo 'debug=$debug;debuglev=$debuglev' > /var/state/camogm_cmd");
67 68 69 70 71 72 73 74
        echo "$cmd ok";
        break;
  case "camogm_kill":
        exec("killall -9 camogm");
        echo "$cmd ok";
        break;
  case "logs_download":
  
75 76
        ini_set('memory_limit','512M');
  
77 78 79 80 81 82 83
        header("Content-Type: application/octet-stream");
        header("Content-Disposition: attachment; filename=\"{$_SERVER["SERVER_ADDR"]}.logs\"");
  
        echo_file($sysfs_lba_path."io_error");
        echo_file($file_lba_start);
        echo_file($file_lba_current);
        echo_file($file_lba_end);
Oleg Dzhimiev's avatar
Oleg Dzhimiev committed
84
      
Oleg Dzhimiev's avatar
Oleg Dzhimiev committed
85 86 87 88
        echo_file($camogmdisk);

        echo_file("/var/state/camera");
                
Oleg Dzhimiev's avatar
Oleg Dzhimiev committed
89 90
        echo_file("/var/log/init_elphel393.log");
        echo_file("/var/log/x393sata_control.log");
91
        //echo_file("/var/log/x393sata_eyesis4pi.log");
Oleg Dzhimiev's avatar
Oleg Dzhimiev committed
92 93 94
      
        echo_file("/var/state/ssd");
      
Oleg Dzhimiev's avatar
Oleg Dzhimiev committed
95 96 97 98 99 100 101
        echo_file("/var/log/autocampars.log");
        
        echo_file("/var/log/lighttpd.error.log");
        echo_file("/var/log/lighttpd_stderr.log");
        
        echo_file("/var/log/messages");
        echo_file("/var/log/access.log");
102
        
103
        echo_file("/tmp/camogm.log");
104
        
105
        break;
106 107 108 109 110 111 112 113
  case "external_drive":
  	switch_sata_connection("external");
  	echo "$cmd ok";
  	break;
  case "internal_drive":
  	switch_sata_connection("internal");
  	echo "$cmd ok";
  	break;
114
  case "symlink":
Oleg Dzhimiev's avatar
Oleg Dzhimiev committed
115 116
    if (is_link($symlink)) die("already exists");
    die(symlink($mountpoint,$symlink));
117
    break;
Oleg Dzhimiev's avatar
Oleg Dzhimiev committed
118
  case "free_space":
119
    //sda1
Oleg Dzhimiev's avatar
Oleg Dzhimiev committed
120 121 122 123
    if (is_dir($mountpoint)){
      $sda1 = round(disk_free_space($mountpoint)/1024/1024/1024,2);
      $sda1 .= "G";
    }
124
    
125
    
126
    //sda2
127 128 129 130
    $lba_start = 0;
    $lba_current = 0;
    $lba_end = 0;
    
131 132 133
    if (is_file($file_lba_start))   $lba_start   = floatval(trim(file_get_contents($file_lba_start)));
    if (is_file($file_lba_current)) $lba_current = floatval(trim(file_get_contents($file_lba_current)));
    if (is_file($file_lba_end))     $lba_end     = floatval(trim(file_get_contents($file_lba_end)));
134 135
    
    if (($lba_start!=0)&&($lba_current!=0)&&($lba_end!=0)){
136
    	//$size = ((($lba_end>>10)&0x003fffff) - (($lba_current>>10)&0x003fffff))/2/1024;
137 138 139
    	$size = ($lba_end - $lba_current)/2/1024/1024;
    	$sda2 = round($size,2);
    	$sda2 .= "G";
140
    }else{
141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175
    	// camogm.disk not found
    	if (!is_file($camogmdisk)){
    		$devices = get_raw_dev();
    		foreach($devices as $device=>$size){
    			//size in MB
    			if ($device=="/dev/sda2") {
    				$sda2 = round($size/1048576,2);
    				$sda2 .= "G";
    			}
    		}
    	}else{
    		//read camogm.disk file
    		$content = file_get_contents($camogmdisk);
    		$content = trim(preg_replace('/\n|\t{2,}/',"\t",$content));
    		$content_arr = explode("\t",$content);
    		
    		if (count($content_arr)>=8){
    			$device = $content_arr[4];
    			$lba_current = $content_arr[6];
    			$lba_end = $content_arr[7];
    			$size = ($lba_end - $lba_current)/2/1024/1024;
    			$sda2 = round($size,2);
    			$sda2 .= "G";
    		}else{    		
	    		//tmp
	    		$devices = get_raw_dev();
	    		foreach($devices as $device=>$size){
	    			//size in MB
	    			if ($device=="/dev/sda2") {
	    				$sda2 = round($size/1048576,2);
	    				$sda2 .= "G";
	    			}
	    		}
    		}
    	}
176
    }
177 178
    //respond_xml("{$sda1} {$sda2}");
    respond_xml("{$sda1}");
179
    break;
180
    
181
  case "reset_camogm_fastrec":
Oleg Dzhimiev's avatar
Oleg Dzhimiev committed
182
  	// remove file
183
  	if (is_file($camogmdisk)){
Oleg Dzhimiev's avatar
Oleg Dzhimiev committed
184
          unlink($camogmdisk);
185
  	}
Oleg Dzhimiev's avatar
Oleg Dzhimiev committed
186 187
  	// reset pointers
  	exec("echo 'rawdev_path=/dev/sda2' > /var/state/camogm_cmd");
188
  	//file_put_contents($file_lba_current,file_get_contents($file_lba_start));
189
  	print("reset fastrec: ok");
190
    break;
Oleg Dzhimiev's avatar
Oleg Dzhimiev committed
191 192 193 194
  case "refresh_camogm_fastrec":
    exec("echo 'rawdev_path=/dev/sda2' > /var/state/camogm_cmd");
    print("refresh fastrec: ok");
    break;
195
  case "free_space_bkp":
196
    // results are in GB
Oleg Dzhimiev's avatar
Oleg Dzhimiev committed
197
    // /dev/sda2 is not a mountpoint but a device because it does not have a file system
198
    $res = 0;
Oleg Dzhimiev's avatar
Oleg Dzhimiev committed
199 200 201 202 203
    if ($_GET['mountpoint']=="/dev/sda2"){
      //root@elphel393:~# cat /home/root/camogm.disk
      //Device          Start LBA       Current LBA     End LBA
      ///dev/sda2       195334335       545641716       976768065
      if (!is_file($camogmdisk)){
204 205 206 207 208
        $devices = get_raw_dev();
        foreach($devices as $device=>$size){
          //size in MB
          if ($device=="/dev/sda2") $res = round($size/1048576,2);
        }
Oleg Dzhimiev's avatar
Oleg Dzhimiev committed
209
      }else{
210 211
        //read camogm.disk file
        $res = 10;
Oleg Dzhimiev's avatar
Oleg Dzhimiev committed
212
      }
Oleg Dzhimiev's avatar
Oleg Dzhimiev committed
213
    }else{
214
      if (is_dir($mountpoint)) $res = round(disk_free_space($mountpoint)/1024/1024/1024,2);
Oleg Dzhimiev's avatar
Oleg Dzhimiev committed
215
    }
216
    respond_xml($res);
Oleg Dzhimiev's avatar
Oleg Dzhimiev committed
217
    break;
218
  default:
Oleg Dzhimiev's avatar
Oleg Dzhimiev committed
219
    print("nothing has been done");
220
}
221 222 223 224 225 226 227 228 229 230 231

function echo_file($f){
  if (is_file($f)){
    echo "$f:\n";
    echo file_get_contents($f)."\n";
  }else{
    echo "$f: missing\n";
  }
  return 0;
}

232
function switch_sata_connection($mode){
233 234 235 236 237 238 239 240 241

  global $mountpoint;

  if       ($mode=="external"){
    exec("/usr/local/bin/x393sata_eyesis4pi_control.py set_zynq_esata");
  }else if ($mode=="internal"){
    exec("/usr/local/bin/x393sata_eyesis4pi_control.py set_zynq_ssd");
  }

242 243
}

244
?>