###############################################################################################################
#
# Test Script : file_load.py
# Configuration Script: file_load.xml
# Analysis Script: file_load_analysis.py
#
# Script Author : Shantha Condamoor
# Main FSW Package: FILE
# FSW Package Author : Dan Wood
# Test Type: 002b
# Test Procedure: File Management and Memory Upload and Dump Verification
# Test Name: File Loads
# Requirement: 5.3.7.9	File Loads
#                       After entering the boot shell, the FSW processors shall be commandable by the 
#                       SIU to perform file loads.

# system 0 - testbed host
# system 1 - Spacecraft/1553 bus controller
#
# Test Preconditions: 1) This test is run in both the Boot as well as Application Mode. The test procedure defines
#                        how this test should be run in the Boot Mode and Application modes. During the first part of
#                        of the test, FSW on RAD750 SIU (and EPU0) crates must have undergone Primary Boot only.
#                        The test script is run in this mode first and results are recorded and analyzed.
#
#                        Then it is advanced to the Secondary Boot mode by issuing proper commands from the spacecraft
#                        simulator as illustrated in the Test Procedure. In this mode, all Application Modules 
#                        (including LFS and FILE) must have been loaded, and all tasks, including (FILE, ITC) 
#                        must have been initialized and running. The same script is run again in the Application Mode.
#
#                     2) Before this test script can be run, four files created and attached with headers
#                       file1, file2, file3 and file4 must exist in the same directory that the script is run from.
#                       The process for creating these files and attaching the headers is described in the Test Procedure.
#                     3) This test can be run only after dir_create (5_3_7_1) has successfully run and directory 111
#                        has been created and exists
#                     4) Before this test script can be run, test scripts sys_status_dump (5_3_7_8) and
#                        dir_dump (5_3_7_7) must have been run successfully. It must be verified that the 
#                        files 1,2,3 and 4 do not exist in directory 111 prior to running this script by verifying the
#                        reports of the above scripts. 
#           
#
# Test Postconditions: 1) Files 1,2,3 and 4 created during the boot mode must be deleted prior to running this test
#                         again in the application mode. This must be verified by running test scripts sys_status_dump 
#                         (5_3_7_8) and dir_dump (5_3_7_7) and analyzing the reports.
#                      2) The following test scripts depend on the creation and existence of directory 111 and hence
#                         can be run only after the successful completion of this test:
#                         c) file_dump (5_3_7_6)
#                         d) file_copy_within_proc (5_3_7_4)
#                         e) file_copy_siu_to_epu (5_3_7_5)
#                         f) file_delete (5_3_7_3)
#
# Tests on SIU only. File uploads for EPUs must be done thru File Copy from SIU to EPU commands (5_3_7_5)
# Tests on both banks: EEPROM0, EEPROM1
# Tests in the Application and  Boot Modes. same test script run twice per clause (1) in Test Postcondition.
# Tests uploading empty file (file1.f) - file1.f just contains header.
# Tests uploading small size file (file2.f - fits in one telecommand) , medium size file (file3.f - several packets) 
#         and big size files (file4.f - large # of pkts).
# Tests uploading a file that has already been uploaded (i.e., with the same file number into same directory and device)
# Four files (file1.f, file2.f, file3.f, file4.f) are uploaded for each bank (EEPROM0 and EEPROM1) 
# for both the SIU and EPU0
# Files will be uploaded into direcotry 111 for testing scripts as described in clause (2) of Test Postcondition.
#
###############################################################################################################

# 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 20',0)
		
        time.sleep(1)
	
	# send the File Upload command for an empty file
	# Parameters: file: file1.f, dev : EEPROM0, dir: 111, file number: 1, transaction id: 279
	# file2 is a very small file
	
	interface.write_sys(1,'FILE_sendUpl \"/afs/slac.stanford.edu/u/ey/scondam/LTX/scripts/test2b/file1.f\",2,111,1,279',0)
	time.sleep(1)		
	
	# send the File Upload command for small file
	# Parameters: file: file2.f, dev : EEPROM0, dir: 111, file number: 2, transaction id: 379
	# file2 is a very small file
	
	interface.write_sys(1,'FILE_sendUpl \"/afs/slac.stanford.edu/u/ey/scondam/LTX/scripts/test2b/file2.f\",2,111,2,379',0)
	time.sleep(1)	
	
	# send the File Upload command
	# Parameters: file: file3.f, dev : EEPROM0, dir: 111, file number: 3, transaction id: 479
	# file2 is a medium size file
	
	interface.write_sys(1,'FILE_sendUpl \"/afs/slac.stanford.edu/u/ey/scondam/LTX/scripts/test2b/file3.f\",2,111,3,479',0)
	time.sleep(20)	
	
	# send the File Upload command
	# Parameters: file: file4.f, dev : EEPROM0, dir: 111, file number: 4, transaction id: 579
	# file2 is a very big file hence must sleep for a long time
	
	#interface.write_sys(1,'FILE_sendUpl \"/afs/slac.stanford.edu/u/ey/scondam/LTX/scripts/test2b/file4.f\",2,111,4,579',0)
	#time.sleep(1000)  
	
	# try uploading same file number again 
	# Parameters: file: file2.f, dev : EEPROM1, dir: 111, file number: 2, transaction id: 679
	# file2 is a very small file
	
	interface.write_sys(1,'FILE_sendUpl \"/afs/slac.stanford.edu/u/ey/scondam/LTX/scripts/test2b/file2.f\",2,111,2,679',0)
	time.sleep(1)	
	
	# send the File Upload command for an empty file
	# Parameters: file: file1.f, dev : EEPROM1, dir: 111, file number: 1, transaction id: 279
	# file2 is a very small file
	
	interface.write_sys(1,'FILE_sendUpl \"/afs/slac.stanford.edu/u/ey/scondam/LTX/scripts/test2b/file1.f\",3,111,1,879',0)
	time.sleep(1)		
	
	# send the File Upload command for small file
	# Parameters: file: file2.f, dev : EEPROM1, dir: 111, file number: 2, transaction id: 379
	# file2 is a very small file
	
	interface.write_sys(1,'FILE_sendUpl \"/afs/slac.stanford.edu/u/ey/scondam/LTX/scripts/test2b/file2.f\",3,111,2,979',0)
	time.sleep(1)	
	
	# send the File Upload command
	# Parameters: file: file3.f, dev : EEPROM1, dir: 111, file number: 3, transaction id: 479
	# file2 is a medium size file
	
	interface.write_sys(1,'FILE_sendUpl \"/afs/slac.stanford.edu/u/ey/scondam/LTX/scripts/test2b/file3.f\",3,111,3,1079',0)
	time.sleep(20)	
	
	# send the File Upload command
	# Parameters: file: file4.f, dev : EEPROM1, dir: 111, file number: 4, transaction id: 579
	# file2 is a very big file hence must sleep for a long time
	
	#interface.write_sys(1,'FILE_sendUpl \"/afs/slac.stanford.edu/u/ey/scondam/LTX/scripts/test2b/file4.f\",3,111,4,1179',0)
	#time.sleep(1000)  		    
	    

        #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 FILE_sendUpl command that was sent:
	# 1. FILE Upload Start Telecommand is sent (APID 1601 FC 0) 
	# 2. Reception of the CmdConfirm Telemetry (APID 720) for the sent  FILE Upload Start Telecommand (APID 1601/0)
	# 3. FILE Upload Data Telecommand is sent (APID 1601 FC 3)
	# 4. FILE Upload Commit Telecommand is sent (APID 1601 FC 2)
	# 5. Reception of the CmdConfirm Telemetry (APID 720) for the sent  FILE Upload Commit Telecommand (APID 1601/2)
	# 6. Verifies that the Command Status field in this telemetry indicates success of execution (LFS_msg must be "Success") 	
	#    or indicates failure failure for creation of an existing direcotry
	#    (currently this is not the case - it still indicates success).
	# 7. sys_status_dump and dir_dump scripts must be run after this test is run and the results analyzed therein
	#    to verify the successful creation of the files 1,2,3 and 4 in directory 111.	
	
	# analyze the results for Pass or Fail criteria
	# interface.exec_analysis(0,'file_load_analysis.py');				        			     
                
        #close all systems	
		
        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()
