###############################################################################################################
#
# Test Script : file_dump.py
# Configuration Script: file_dump.xml
# Analysis Script: file_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 Dump
# Requirements: 5.3.7.6
#               5.3.7.6.1	File Dump Command
#                               In order to dump a file, the FSW shall receive as input, from the spacecraft via 
#                               the CTDB, a command that includes file identifier, directory identifier, 
#                               device identifier, and unit identifier.
#               5.3.7.6.2	File Dump Data 
#                               In response to receiving a File Dump command, the FSW shall transmit the requested 
#                               file data to the spacecraft.
#
# 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. 
#                     5) Before this test script can be run, test script file_load (5_3_7_9) MAY 
#                        have been run successfully and Files 1,2,3 and 4 MAY have been created in Directory 111.
#                     6) Before this test script can be run, test script file_copy_within_proc (5_3_7_4) MAY
#                        have been run successfully and Files 1,2,3 and 4 MAY have been copied as Files 12
#                        and 13 into Directory 111 EEPROM0.
#                     5) Before this test script can be run, test script file_copy_within_proc (5_3_7_4) MAY
#                        have been run successfully and Files 1,2,3 and 4 MAY have been copied as Files 12
#                        and 13 into Directory 111 EEPROM1.
#                     6) Before this test script can be run, test script file_copy_siu_to_epu (5_3_7_5) MAY
#                        have been run successfully and Files 1,2,3 and 4 MAY have been copied to Directory 111 
#                        in EPU0 EEPROM0.
#                     7) Before this test script can be run, test script file_copy_siu_to_epu (5_3_7_5) must
#                        have been run successfully and Files 1,2,3 and 4 must have been copied to Directory 111
#                        in EPU0 EEPROM1.
#
# Test Postconditions: This test must typically be run several times as part of the File Management Test Procedure:
#                      1. Before any directories are created (file dumps will result in error)
#                      2. After direcotries 111 and 112 are created (empty directories will result in doing no dumps)
#                      3. After Directory 111 contains the uploaded files 1,2,3 and 4 (file dumps will occur)
#                      4. After Files are deleted from Directory 111. (empty directories will result in doing no dumps)
#                      5. After Directories 111 and 112 are deleted.(file dumps will result in error)
#
# Tests on both SIU and EPU
# Tests on both banks: EEPROM0, EEPROM1
# Tests in the Application Mode only. 
# Tests dumping an empty file that has only a header, no contents (file 1)
# Tests dumping a very small file (file 2 has few bytes), a medium size file (file 3) and a 
# very large file (file 4 - several mega bytes)
# Tests dumping a file that does not exist
#
###############################################################################################################

# 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 60000',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 forward,  new
	
	interface.write_sys(1,'ITC_sendCmdResponse 0,2,0,0,0',0)
	time.sleep(1)

	# send the File Dump command (empty file)
	# Parameters: latUnit: SIU, dev : EEPROM0 dir: 111 file: 1  transaction Id
	interface.write_sys(1,'LFS_sendFileDump 0,2,111,1,276',0)
	time.sleep(1)		

	# send the File Dump command (small file)
	# Parameters: latUnit: SIU, dev : EEPROM0 dir: 111 file: 2  transaction Id	
	interface.write_sys(1,'LFS_sendFileDump 0,2,111,2,376',0)
	time.sleep(1)	

	# send the File Dump command (medium-size file)
	# Parameters: latUnit: SIU, dev : EEPROM0 dir: 111 file: 3  transaction Id(big file)	
	interface.write_sys(1,'LFS_sendFileDump 0,2,111,3,476',0)
	time.sleep(1)	
	
	# send the File Dump command (big-size file)
	# Parameters: latUnit: SIU, dev : EEPROM0 dir: 111 file: 4  transaction Id	
	#interface.write_sys(1,'LFS_sendFileDump 0,2,111,4,576',0)
	#time.sleep(1)	
	
	# try dumping a file that does not exist
	# Parameters: latUnit: SIU, dev : EEPROM0 dir: 111 file: 5  transaction Id	
	interface.write_sys(1,'LFS_sendFileDump 0,2,111,5,676',0)
	time.sleep(1)	
	
	# send the File Dump command (small file) that was copied using the File Copy with Processor command
	# Parameters: latUnit: SIU, dev : EEPROM0 dir: 111 file: 12  transaction Id	
	interface.write_sys(1,'LFS_sendFileDump 0,2,111,12,76',0)
	time.sleep(1)	
	
	# send the File Dump command (medium size file) that was copied using the File Copy with Processor command
	# Parameters: latUnit: SIU, dev : EEPROM0 dir: 111 file: 13  transaction Id	
	interface.write_sys(1,'LFS_sendFileDump 0,2,111,13,77',0)
	time.sleep(1)				
		
	# send the File Dump command (empty file)
	# Parameters: latUnit: SIU, dev : EEPROM1 dir: 111 file: 1 transaction Id 	
	interface.write_sys(1,'LFS_sendFileDump 0,3,111,1,776',0)
	time.sleep(1)	
	
	# send the File Dump command (small file)
	# Parameters: latUnit: SIU, dev : EEPROM1 dir: 111 file: 2 transaction Id	
	interface.write_sys(1,'LFS_sendFileDump 0,3,111,2,876',0)
	time.sleep(1)	
	
	# send the File Dump command (medium size file)
	# Parameters: latUnit: SIU, dev : EEPROM1 dir: 111 file: 3 transaction Id	
	interface.write_sys(1,'LFS_sendFileDump 0,3,111,3,976',0)
	time.sleep(1)	
	
	# send the File Dump command (big-size file)
	# Parameters: latUnit: SIU, dev : EEPROM0 dir: 111 file: 4  transaction Id	
	#interface.write_sys(1,'LFS_sendFileDump 0,3,111,4,1076',0)
	#time.sleep(1)		

	# try dumping a file that does not exist
	# Parameters: latUnit: SIU, dev : EEPROM0 dir: 111 file: 5  transaction Id	
	interface.write_sys(1,'LFS_sendFileDump 0,3,111,5,1176',0)
	time.sleep(1)	
	
	# send the File Dump command (small file) that was copied using the File Copy with Processor command
	# Parameters: latUnit: SIU, dev : EEPROM1 dir: 111 file: 12  transaction Id	
	interface.write_sys(1,'LFS_sendFileDump 0,3,111,12,176',0)
	time.sleep(1)	
	
	# send the File Dump command (medium size file) that was copied using the File Copy with Processor command
	# Parameters: latUnit: SIU, dev : EEPROM1 dir: 111 file: 13  transaction Id	
	interface.write_sys(1,'LFS_sendFileDump 0,3,111,13,177',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 File Dump command (empty file)
	# Parameters: latUnit: EPU0, dev : EEPROM0 dir: 111 file: 1  transaction Id
	#interface.write_sys(1,'LFS_sendFileDump 1,2,111,1,1276',0)
	#time.sleep(1)		

	# send the File Dump command (small file)
	# Parameters: latUnit: EPU0, dev : EEPROM0 dir: 111 file: 2  transaction Id	
	#interface.write_sys(1,'LFS_sendFileDump 1,2,111,2,1376',0)
	#time.sleep(1)	

	# send the File Dump command (medium-size file)
	# Parameters: latUnit: EPU0, dev : EEPROM0 dir: 111 file: 3  transaction Id(big file)	
	#interface.write_sys(1,'LFS_sendFileDump 1,2,111,3,1476',0)
	#time.sleep(1)	
	
	# send the File Dump command (big-size file)
	# Parameters: latUnit: EPU0, dev : EEPROM0 dir: 111 file: 4  transaction Id	
	# interface.write_sys(1,'LFS_sendFileDump 1,2,111,4,1576',0)
	# time.sleep(1)	
	
	# try dumping a file that does not exist
	# Parameters: latUnit: EPU0, dev : EEPROM0 dir: 111 file: 5  transaction Id	
	#interface.write_sys(1,'LFS_sendFileDump 1,2,111,5,1676',0)
	#time.sleep(1)		
		
	# send the File Dump command (empty file)
	# Parameters: latUnit: EPU0, dev : EEPROM1 dir: 111 file: 1 transaction Id 	
	# interface.write_sys(1,'LFS_sendFileDump 1,3,111,1,1776',0)
	#time.sleep(1)	
	
	# send the File Dump command (small file)
	# Parameters: latUnit: EPU0, dev : EEPROM1 dir: 111 file: 2 transaction Id	
	#interface.write_sys(1,'LFS_sendFileDump 1,3,111,2,1876',0)
	#time.sleep(1)	
	
	# send the File Dump command (medium size file)
	# Parameters: latUnit: EPU0, dev : EEPROM1 dir: 111 file: 3 transaction Id	
	#interface.write_sys(1,'LFS_sendFileDump 1,3,111,3,1976',0)
	#time.sleep(1)	
	
	# send the File Dump command (big-size file)
	# Parameters: latUnit: EPU0, dev : EEPROM0 dir: 111 file: 4  transaction Id	
	#interface.write_sys(1,'LFS_sendFileDump 1,3,111,4,2076',0)
	#time.sleep(1)		

	# try dumping a file that does not exist
	# Parameters: latUnit: EPU0, dev : EEPROM0 dir: 111 file: 5  transaction Id	
	#interface.write_sys(1,'LFS_sendFileDump 1,3,111,5,2176',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_sendFileDump command that was sent:
	# 1. Reception of the CmdConfirm Telemetry (APID 720) for the sent LFS_sendFileDump command (APID 1608 FC 4)
	# 2. Verfies that the Command Status field in this telemetry indicates success of execution (LFS_msg must be "Success")
	#    if a file exists and failure (LFS_msg must be "File open err") if the file does not exist. 
	# 3. Reception of Root File Dump Data CTDB Telemetry (APID 795) for existing files. May contain several 795 packets.
	# 4. Absence of Directory Listing Report Telemetry (APID 792) when a directory DOES NOT exist.
	# 5. Verfies LATSUNIT value (in the DUMPSTFLAGS) matches with the latunit parameter
	# 6. Verifies that LFSXID value (in the DUMPSTFLAGS) matches with the Transaction ID parameter
	# 7. Verifies that FILEDEV value (in FILESTID) matches with the  dev #
	# 8. Verifies that FILEDIR value (in FILESTID) matches with the directory #
	# 9. Verifies that FILENUM value (in FILESTID) matches with the file #
	# 10. Verifies that FILESTOFFSET value contains a valid offset for that file
	# 11. Verifies that FILEDUMPSIZE value contains a valid file size.
	# 12. Verifies that FILEDATA values are valid. This is compared against the source file that was uploaded.
	#	
	
	# analyze the results for Pass or Fail criteria
	# interface.exec_analysis(0,'file_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()
