###############################################################################################################
#
# Test Script : sys_status_dump.py
# Configuration Script: sys_status_dump.xml
# Analysis Script: sys_status_dump_analysis.py
#
# Script Author : Shantha Condamoor
# Main FSW Package: LFS
# FSW Package Author : Dan Wood
# Test Type: 002b
# Test Procedure: File Management and Memory Upload and Dump Verification
# Test Name: File System Status Dump 
# Requirement: 5.3.7.8
#              5.3.7.8	File System Status Dump	
#              5.3.7.8.1 File System Status Dump Command
#                        The FSW shall receive as input, from the spacecraft via the CTDB, 
#                        a command to dump the file system status for a specific unit.
#              5.3.7.8.2 File System Status Dump Data 
#                        In response to receiving a File System Status Dump command, 
#                        the FSW shall transmit the requested data to the spacecraft, 
#                        including: total block size, blocks used, and blocks free for the 
#                        originally requested unit.
#
# system 0 - testbed host
# system 1 - Spacecraft/1553 bus controller
#
# Test Preconditions: 1) This script CANNOT be run in the BOOT mode as LFS commands are currently unsupported in this mode.
#                     2) Before this test script can be run, FSW on RAD750 SIU crate  must have undergone Primary
#                        and Secondary Boots, all Application Modules (including LFS and FILE) must have been loaded,
#                        all tasks, (including FILE, ITC) must have been initialized and be running.
#                     3) Before this test script can be run, FSW on RAD750 EPU0 and EPU1 crates must have
#                        undergone Primary and Secondary Boots, all Application Modules (including LFS and FILE)
#                        must have been loaded and all tasks (including FILE and ITC) must have been initialized
#                        and be running
#
#
# Test Postconditions: This test must typically be run several times as part of the File Management Test Procedure:
#                      1. Before any directories are created
#                      2. After direcotries 111 and 112 are created
#                      3. After Directory 111 contains the uploaded files 1,2,3 and 4
#                      4. After Files are deleted from Directory 111.
#                      5. After Directory 111 and 112 are deleted.
#
#
# Tests on SIU, EPU0 and EPU1.
# Tests on both EEPROM0 and EEPROM1
# 
###############################################################################################################

# system 0 - testbed host
# system 1 - Spacecraft/1553 bus controller

from ltx_scriptinterface import *
import time

def main():
    
    #define interface - REQUIRED
    interface = None
    
    #always a good idea to enclose body in try/except block as LTX will throw exceptions
    try:
        #initialize the script interface -- REQUIRED
        interface = LTX_ScriptInterface()
        
        #start the host shell
        interface.start_sys(0)
        
        #start the spacecraft (SC) shell
        interface.start_sys(1)
        
        #initialize the systems
        interface.exec_ltx_cmds(0,'init')
        interface.exec_ltx_cmds(1,'init')
        
        #start the SC initialization
        interface.write_sys(1,'SCP_init 3',0)
        time.sleep(1)   
		             
        #enable Diagnostic dumps
        interface.write_sys(1,'SCP_setDiagnostic 10',0)
		
        time.sleep(1)
	
	# enable ITC Cmd Responses from nid=SIU, for the LAT FILE Master task (2)
	# for normal (not broadcast) commands from SC, when the task is functioning
	# purely as an executing task (0) 
	# Parameters: node id = SIU, Task ID = FILE, class = normal, action = not forwarding task,  new
	
	interface.write_sys(1,'ITC_sendCmdResponse 0,2,0,0,0',0)
	time.sleep(1)

	# send the File System Status Dump command for EEPROM0
	# Parameters: latUnit: SIU, dev #: EEPROM0, Transaction ID; 378
	
	interface.write_sys(1,'LFS_sendSysStatus 0,2,378',0)
	time.sleep(1)	
	
	# send the File System Status Dump command for EEPROM1
	# Parameters: latUnit: SIU, dev #: EEPROM1, Transaction ID; 478
	
	interface.write_sys(1,'LFS_sendSysStatus 0,3,478',0)
	time.sleep(1)	
	
	# enable ITC Cmd Responses from nid=EPU0, for the LAT FILE Master task (2)
	# for normal (not broadcast) commands from SC, when the task is functioning
	# as a forwarding task (1) 
	# Parameters: node id = EPU0, Task ID = FILE, class = normal, action = forward,  new
	
	#interface.write_sys(1,'ITC_sendCmdResponse 1,2,0,1,0',0)
	#time.sleep(1)	
	
	# send the File System Status Dump command for EEPROM0
	# Parameters: latUnit: EPU0, dev #: EEPROM0, Transaction ID; 578
	
	#interface.write_sys(1,'LFS_sendSysStatus 1,2,578',0)
	#time.sleep(1)	
	
	# send the File System Status Dump command for EEPROM1
	# Parameters: latUnit: EPU0, dev #: EEPROM1, Transaction ID; 678
	
	#interface.write_sys(1,'LFS_sendSysStatus 1,3,678',0)
	#time.sleep(1)	
	
	# enable ITC Cmd Responses from nid=EPU1, for the LAT FILE Master task (2)
	# for normal (not broadcast) commands from SC, when the task is functioning
	# as a forwarding task (1) 
	# Parameters: node id = EPU1, Task ID = FILE, class = normal, action = forward,  new
	
	#interface.write_sys(1,'ITC_sendCmdResponse 2,2,0,1,0',0)
	#time.sleep(1)			
	
	# send the File System Status Dump command for EEPROM0
	# Parameters: latUnit: EPU0, dev #: EEPROM0, Transaction ID; 778
	
	#interface.write_sys(1,'LFS_sendSysStatus 1,2,778',0)
	#time.sleep(1)	
	
	# send the File System Status Dump command for EEPROM1
	# Parameters: latUnit: EPU0, dev #: EEPROM1, Transaction ID; 878
	
	#interface.write_sys(1,'LFS_sendSysStatus 1,3,878',0)
	#time.sleep(1)		
			
        #disable diagnostic packet dumps
        interface.write_sys(1,'SCP_setDiagnostic 0',0)
	time.sleep(1)		
	
	#
	# Analyze the results for Pass / Fail criteria
	#
	# The analysis script verifies the following for each of the LFS_sendSysStatus command that was sent:
	# 1. Reception of the CmdConfirm Telemetry (APID 720) for the sent LFS_sendSysStatus command (APID 1608 FC 6)
	# 2. Verfies that the Command Status field in this telemetry indicates success of execution (LFS_msg must be "Success") 
	# 3. Reception of File System Status Report Telemetry (APID 794)
	# 4. Verfies LATSUNIT value (in the DUMPSTFLAGS) matches with the latunit parameter
	# 5. Verifies that LFSXID value (in the DUMPSTFLAGS) matches with the Transaction ID parameter
	# 6. Verifies that FILEDEV value (in FILESTID) matches with the  dev #
	# 7. Verifies that FILEDIR value (in FILESTID) is 0 (for System information)
	# 8. Verifies that FILENUM value (in FILESTID) is 0 (for System information)
	# 9. Verifies that SYSBLKTOTAL value (total block size) contains a valid value (range between 0 - 4000 blocks?)
	# 10. Verifies that SYSBLKSIZE value (blocks used)contains a valid value (must be less than SYSBLKTOTAL)
	# 11. Verifies that SYSBLKFREE value (blocks free) contains a valid value (must be less than SYSBLKTOTAL)
	
	# interface.exec_analysis(0,'sys_status_dump_analysis.py');
			
	
        interface.exec_ltx_cmds(1,'exit')
        interface.exec_ltx_cmds(0,'exit')
        
        interface.stop_sys('all')
        
        #exit the test
        interface.close(0,0)
            
    except Exception,e:
        #if a thrown exception is severe, you might want to handle it
        #if interface is active, try to stop systems and close interface -- OPTIONAL but recommended
        print 'Unhandled exception in main script',e
        if interface != None:
            #forcibly kill all active systems
            interface.kill_all()        
        
if __name__ == "__main__":
    main()
