###############################################################################################################
#
# Test Script : dir_dump.py
# Configuration Script: dir_dump.xml
# Analysis Script: dir_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 Directory Dump 
# Requirement: 5.3.7.7
#              5.3.7.7	File Directory Dump	
#              5.3.7.7.1 File Directory Dump Command
#                        In order to dump a file directory, the FSW shall receive as input,
#                        from the spacecraft via the CTDB, a command that includes directory 
#                        identifier, device identifier, and unit identifier.
#              5.3.7.7.2  File Directory Dump Data	
#                        In response to receiving a File Directory Dump command, the FSW shall
#                        transmit the requested data to the spacecraft, including: 
#                        device identifier, directory identifier, file identifier, 
#                        setting of read-only flag, archive flag setting, most recent update 
#                        time, byte size, and number of blocks for each file and subdirectory
#                        in the requested directory.
#
# 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 crate 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
#                     4) Before this test script can be run, test script dir_create (5_3_7_1) may have been 
#                        run successfully and directory 111 may have been created and may or may not contain files.
#                        Directory 112 may or may not have been created and contain no files. Directory 113 does not exist. 
#
# 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 Directories 111 and 112 are deleted.
#
# Tests on SIU and  EPU0.
# Tests on both banks: EEPROM0, EEPROM1
# Tests dumping of a directory that is empty 112
# Tests dumping of a directory that does not exist 113.
# Tests dumping of the root directory 127 for all devices.
#
###############################################################################################################

# 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 100',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 Directory dump Telecommand for the root directory.
	# Parameters: latUnit = SIU, dev # = EEPROM0, directory # = 127, transaction ID = 277
	interface.write_sys(1,'LFS_sendDirDump 0,2,127,277',0)
	time.sleep(1)		

	# send the Directory dump Telecommand.
	# Parameters: latUnit = SIU, dev # = EEPROM0, directory # = 111, transaction ID = 377
	interface.write_sys(1,'LFS_sendDirDump 0,2,111,377',0)
	time.sleep(5)	
	
	# try dumping an empty directory
	# Parameters: latUnit = SIU, dev # = EEPROM0, directory # = 112, transaction ID = 477
	interface.write_sys(1,'LFS_sendDirDump 0,2,112,477',0)
	time.sleep(1)	
	
	# try dumping from a non-existent directory
	# Parameters: latUnit = SIU, dev # = EEPROM0, directory # = 113, transaction ID = 577
	interface.write_sys(1,'LFS_sendDirDump 0,2,113,577',0)
	time.sleep(1)		
	
	# send the Directory dump Telecommand for the root directory.
	# Parameters: latUnit = SIU, dev # = EEPROM1, directory # = 127, transaction ID = 677
	interface.write_sys(1,'LFS_sendDirDump 0,3,127,677',0)
	time.sleep(1)	
	
	# send the Directory dump Telecommand.
	# Parameters: latUnit = SIU, dev # = EEPROM1, directory # = 111, transaction ID = 777
	interface.write_sys(1,'LFS_sendDirDump 0,3,111,777',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 = not forward,  new
	
	#interface.write_sys(1,'ITC_sendCmdResponse 1,2,0,1,0',0)
	#time.sleep(1)	
	
	# send the Directory dump Telecommand for the root directory.
	# Parameters: latUnit = EPU0, dev # = EEPROM0, directory # = 127, transaction ID = 877
	#interface.write_sys(1,'LFS_sendDirDump 1,2,127,877',0)
	#time.sleep(1)		
	
	# send the Directory dump Telecommand.
	# Parameters: latUnit = EPU0, dev # = EEPROM0, directory # = 111, transaction ID = 977
	#interface.write_sys(1,'LFS_sendDirDump 1,2,111,977',0)
	#time.sleep(1)	
	
	# send the Directory dump Telecommand for the root directory.
	# Parameters: latUnit = EPU0, dev # = EEPROM1, directory # = 127, transaction ID = 1077
	#interface.write_sys(1,'LFS_sendDirDump 1,3,127,1077',0)
	#time.sleep(1)		
	
	# send the Directory dump Telecommand.
	# Parameters: latUnit = EPU0, dev # = EEPROM1, directory # = 111, transaction ID = 1177
	#interface.write_sys(1,'LFS_sendDirDump 1,3,111,1177',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_sendDirDump command that was sent:
	# 1. Reception of the CmdConfirm Telemetry (APID 720) for the sent LFS_sendDirDump command (APID 1608 FC 5) for root dir 127
	# 2. Verfies that the Command Status field in this telemetry indicates success of execution (LFS_msg must be "Success") 
	# 3. Reception of Root Directory Listing Report Telemetry (APID 793) reflecting the current status of the system direcotry.
	#    depending on the Test Procedure step when this script is run. May contain several 793 packets.
	# 4. Absence of Directory Listing Report Telemetry (APID 792) when a directory DOES NOT exist.
	# 5. Reception of Directory Listing Report Telemetry (APID 792) when a directory DOES exist.
	# 6. Verify that the number of files and file numbers in a directory shows correctly when it contains files or is empty.
	# 7. Verfies LATSUNIT value (in the DUMPSTFLAGS) matches with the latunit parameter
	# 8. Verifies that LFSXID value (in the DUMPSTFLAGS) matches with the Transaction ID parameter
	# 9. Verifies that FILESTARCHIVE value (in FILESTFLAGS) shows the correct value (0)
	# 10. Verifies that FILESTDIR value (in FILESTFLAGS) shows the correct value (0)
	# 11. Verifies that FILESTRDONLY value (in FILESTFLAGS) shows the correct value (0)
	# 12. Verifies that FILEDEV value (in FILESTID) matches with the  dev #
	# 13. Verifies that FILEDIR value (in FILESTID) matches with the directory #
	# 14. Verifies that FILENUM value (in FILESTID) contains the correct file number present in that directory
	# 15. Verifies that FILESTTIME value contains a valid Update time for that file
	# 16. Verifies that FILESTSIZE value contains a valid file size.
	# 17. Verifies that FILESTBLOCK value is valid.
	# 18. Verifies that FILEHDR value is valid for that file.
	#	
	
	# analyze the results for Pass or Fail criteria
	# interface.exec_analysis(0,'dir_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()
