###############################################################################################################
#
# Test Script : file_copy_within_proc.py
# Configuration Script: file_copy_within_proc.xml
# Analysis Script: file_copy_within_proc_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 Copy within a Processor
# Requirement: 5.3.7.4
#              In order to copy a file within a processor, the FSW shall receive as input, from the spacecraft 
#              via the CTDB, a command that includes source file identifier, source directory identifier, 
#              source device identifier, unit identifier, destination file identifier, destination directory 
#              identifier, and destination device identifier.
#
# 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, test script dir_create (5_3_7_1) 
#                        must have been run successfully and Directories 111 must have been created
#                     4) Before this test script can be run, test scripts sys_status_dump (5_3_7_8),
#                        dir_dump (5_3_7_7) and file_dump (5_3_7_6) must have been run successfully. 
#                        It must be verified that the files 12 and 13 do not exist in directory 111 
#                        prior to running this script by verifying the reports of the above scripts. 
#
# Test Postconditions: 1) After this test is run, test scripts sys_status_dump (5_3_7_8),
#                        dir_dump (5_3_7_7) and file_dump (5_3_7_6) must be run successfully 
#                        to verify that the files 12 and 13 exist in directory 111.
#
# Tests on SIU only
# Tests on both banks: EEPROM0, EEPROM1
# Tests copying to a file that already exists.
# Tests copying from a source file that does not exist.
# Tests copying files from one bank to another.
# Tests on files 2 (file2.f) and 3 (file3.f)
###############################################################################################################

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

	# send the File Copy command Telecommand (small file)
	# Parameters: latUnit = SIU, src dev # = EEPROM0, src directory # = 111, src file # = 2
	#             dest dev # = EEPROM0, dst directory # = 111, dst file # = 12, transaction ID = 274
	
	interface.write_sys(1,'LFS_sendFileCopy 0,2,111,2,2,111,12,274',0)
	time.sleep(1)	
	
	# send the File Copy command Telecommand (medium file)
	# Parameters: latUnit = SIU, src dev # = EEPROM0, src directory # = 111, src file # = 3
	#             dest dev # = EEPROM0, dst directory # = 111, dst file # = 13, transaction ID = 374
	
	interface.write_sys(1,'LFS_sendFileCopy 0,2,111,3,2,111,13,374',0)
	time.sleep(1)	
	
	# try copying to a file that already exists
	# Parameters: latUnit = SIU, src dev # = EEPROM0, src directory # = 111, src file # = 2
	#             dest dev # = EEPROM0, dst directory # = 111, dst file # = 12, transaction ID = 474
	
	interface.write_sys(1,'LFS_sendFileCopy 0,2,111,2,2,111,12,474',0)
	time.sleep(1)	
	
	# try copying from a file that does not exist
	# Parameters: latUnit = SIU, src dev # = EEPROM0, src directory # = 111, src file # = 7
	#             dest dev # = EEPROM0, dst directory # = 111, dst file # = 17, transaction ID = 574
	
	interface.write_sys(1,'LFS_sendFileCopy 0,2,111,7,2,111,17,574',0)
	time.sleep(1)				
	
	# send the File Copy command Telecommand (small file)
	# Parameters: latUnit = SIU, src dev # = EEPROM0, src directory # = 111, src file # = 2
	#             dest dev # = EEPROM1, dst directory # = 111, dst file # = 12, transaction ID = 674
	
	interface.write_sys(1,'LFS_sendFileCopy 0,2,111,2,3,111,12,674',0)
	time.sleep(1)	
	
	# send the File Copy command Telecommand (small file)
	# Parameters: latUnit = SIU, src dev # = EEPROM0, src directory # = 111, src file # = 3
	#             dest dev # = EEPROM1, dst directory # = 111, dst file # = 13, transaction ID = 774
	
	interface.write_sys(1,'LFS_sendFileCopy 0,2,111,3,3,111,13,774',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_sendFileCopy (APID 1608 FC 1) command that was sent:
	# 1. Reception of the CmdConfirm Telemetry (APID 720) for the sent  FILE Upload Start Telecommand (APID 1601/0)
	# 2. Verifies that the Command Status field in this telemetry indicates success of execution (LFS_msg must be "Success") 	
	#    or indicates failure failure for copying from an unknown source file (file open err) or copying into an existing file.
	#    (currently the later is not the case - it still indicates success).
	# 3. sys_status_dump, dir_dump and file_dump scripts must be run after this test is run and the 
	#    results analyzed therein to verify the successful copying of the local files 12 and 13 in directory 111.
			
	# analyze the results for Pass or Fail criteria
	# interface.exec_analysis(0,'file_copy_within_proc_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()
