###############################################################################################################
#
# Test Script : file_load_app_mode.py
# Configuration Script: file_load_app_mode.xml
# Analysis Script: file_load_app_mode_analysis.py
#
# Author : Shantha Condamoor
# 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) Before this test script can be run, FSW on RAD750 SIU crate  must have undergone Primary 
#                        and Secondary boots. All modules must have been loaded and running. 
#                     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.
#
# Test Postconditions: Test scripts pertaining to requirements 5_3_7_2 thru 5_3_7_10 can be run only after 
#                      this test script has been run.
#
# 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 Mode only. file_load_boot_mode.py script tests file uploads in the Boot Mode.
# 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 pertaining to requirements 5_3_7_2 thru  5_3_7_10
#
###############################################################################################################

# 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 or Fail criteria
	# interface.exec_analysis(0,'file_load_app_mode.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()
