###############################################################################################################
#
# Test Script : file_delete.py
# Configuration Script: file_delete.xml
# Analysis Script: file_delete_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 Delete
# Requirement: 5.3.7.3
#              In order to delete 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.
#
# 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: 1) Test scripts file_copy_within_proc (5_3_7_4), file_copy_siu_to_epu (5_3_7_5)
#                         and CANNOT be run after this test script has been run.
#                      2) Test script dir_delete (5_3_7_2) can be run only after this script has been run.
#                      3) Test scripts dir_dump (5_3_7_7), sys_status_dump (5_3_7_8) and file_dump (5_3_7_6) must be run after 
#                         this script has been run to verify that the files have been successfully deleted.
#
# Tests on both SIU and EPU0.
# Tests on both banks: EEPROM0, EEPROM1
# Tests deletion of a file that has already been deleted.
# Tests deletion of a file that does not exist.
# Deletes all files that were created using other scripts.
#
###############################################################################################################

# 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 Delete command to delete the empty file
	# Parameters: node id = SIU, Device # = EEPROM0, Directory # = 111, File # = 1, Transaction ID : 273
	
	interface.write_sys(1,'LFS_sendFileDelete 0,2,111,1,273',0)
	time.sleep(1)	
	
	# send the File Delete command to delete the small size file
	interface.write_sys(1,'LFS_sendFileDelete 0,2,111,2,373',0)
	time.sleep(1)	
	
	# send the File Delete command to delete the medium size file
	interface.write_sys(1,'LFS_sendFileDelete 0,2,111,3,473',0)
	time.sleep(1)
	
	# send the File Delete command to delete the big size file
	#interface.write_sys(1,'LFS_sendFileDelete 0,2,111,4,573',0)
	#time.sleep(1)	
	
	# send the File Delete command to delete the small size file that was copied using File Copy Within Processor command
	interface.write_sys(1,'LFS_sendFileDelete 0,2,111,12,73',0)
	time.sleep(1)	
	
	# send the File Delete command to delete the medium size file that was copied using File Copy Within Processor command
	interface.write_sys(1,'LFS_sendFileDelete 0,2,111,13,173',0)
	time.sleep(1)	
	
	# try to delete a file that has already been deleted.
	interface.write_sys(1,'LFS_sendFileDelete 0,2,111,2,673',0)
	time.sleep(1)	
	
	# try to delete a file that does not exist
	interface.write_sys(1,'LFS_sendFileDelete 0,2,111,7,773',0)
	time.sleep(1)							
	
	# send the File Delete command
	# Parameters: node id = SIU, Device # = EEPROM1, Directory # = 111, File # = 1, Transaction ID : 973
	
	interface.write_sys(1,'LFS_sendFileDelete 0,3,111,1,873',0)
	time.sleep(1)	
	
	# send the File Delete command to delete the small size file
	interface.write_sys(1,'LFS_sendFileDelete 0,3,111,2,973',0)
	time.sleep(1)	
	
	# send the File Delete command to delete the medium size file
	interface.write_sys(1,'LFS_sendFileDelete 0,3,111,3,1073',0)
	time.sleep(1)
	
	# send the File Delete command to delete the big size file
	#interface.write_sys(1,'LFS_sendFileDelete 0,3,111,4,1173',0)
	#time.sleep(1)	
	
	# try to delete a file that has already been deleted.
	interface.write_sys(1,'LFS_sendFileDelete 0,3,111,2,1273',0)
	time.sleep(1)	
	
	# try to delete a file that does not exist
	interface.write_sys(1,'LFS_sendFileDelete 0,3,111,7,1373',0)
	time.sleep(1)	
	
	# send the File Delete command to delete the small size file that was copied using File Copy Within Processor command
	interface.write_sys(1,'LFS_sendFileDelete 0,3,111,12,1473',0)
	time.sleep(1)	
	
	# send the File Delete command to delete the medium size file that was copied using File Copy Within Processor command
	interface.write_sys(1,'LFS_sendFileDelete 0,3,111,13,1573',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 Delete command
	# Parameters: node id = EPU0, Device # = EEPROM0, Directory # = 111, File # = 1, Transaction ID : 1673
	
	#interface.write_sys(1,'LFS_sendFileDelete 1,2,111,1,1673',0)
	#time.sleep(1)	
	
	# send the File Delete command to delete the small size file
	#interface.write_sys(1,'LFS_sendFileDelete 1,2,111,2,1773',0)
	#time.sleep(1)	
	
	# send the File Delete command to delete the medium size file
	#interface.write_sys(1,'LFS_sendFileDelete 1,2,111,3,1873',0)
	#time.sleep(1)
	
	# send the File Delete command to delete the big size file
	#interface.write_sys(1,'LFS_sendFileDelete 1,2,111,4,1973',0)
	#time.sleep(1)	
	
	# send the File Delete command
	# Parameters: node id = EPU0, Device # = EEPROM1, Directory # = 111, File # = 1, Transaction ID : 2073
	
	#interface.write_sys(1,'LFS_sendFileDelete 1,3,111,1,2073',0)
	#time.sleep(1)	
	
	# send the File Delete command to delete the small size file
	#interface.write_sys(1,'LFS_sendFileDelete 1,3,111,2,2173',0)
	#time.sleep(1)	
	
	# send the File Delete command to delete the medium size file
	#interface.write_sys(1,'LFS_sendFileDelete 1,3,111,3,2273',0)
	#time.sleep(1)
	
	# send the File Delete command to delete the big size file
	#interface.write_sys(1,'LFS_sendFileDelete 1,3,111,4,2373',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_sendFileDelete command that was sent:
	# 1. Reception of the CmdConfirm Telemetry (APID 720) for the sent LFS_sendFileDelete command (APID 1608 FC 0)
	# 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. 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 deletion of the files 1,2,3,4,12 and 13 from directory 111.
	
	# analyze the results for Pass or Fail criteria
	# interface.exec_analysis(0,'file_delete_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()
