###############################################################################################################
#
# Test Script : dir_delete.py
# Configuration Script: dir_delete.xml
# Analysis Script: dir_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: Directory Delete
# Requirement: 5.3.7.2
#              In order to delete a directory, the FSW shall receive as input, from the spacecraft via the CTDB,
#              a command that includes 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 Directories 111 and 112 MAY have been created. 
#                     5) This test must be run at least twice as described in the Test Procedure - when directory 111 still contains files
#                        and when all the files in 111 have been deleted.
#
# Test Postconditions: 1) Test scripts dir_dump (5_3_7_7) and sys_status_dump (5_3_7_8) must be run after 
#                         this script has been run to verify that the directories have been successfully deleted.
#
# Tests on both SIU and EPU0.
# Tests on both banks: EEPROM0, EEPROM1
# Tests deletion of a directory that still contains some files
# Tests deletion of an empty directory.
# Tests deletion of a non-existent directory.
#
###############################################################################################################

# 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 Directory Delete Telecommand. Directory 111 still contains some files.
	# Parameters: latUnit = SIU, dev # = EEPROM0, directory # = 111, transaction ID = 372	
	
	interface.write_sys(1,'LFS_sendDirDelete 0,2,111,372',0)
	time.sleep(1)		
	
	# try to delete an empty directory
	interface.write_sys(1,'LFS_sendDirDelete 0,2,112,572',0)
	time.sleep(1)	
	
	# try to delete an non-existent directory
	interface.write_sys(1,'LFS_sendDirDelete 0,2,113,672',0)
	time.sleep(1)	
	
	# send the Directory Delete Telecommand. Directory 111 still contains some files. 
	# Parameters: latUnit = SIU, dev # = EEPROM1, directory # = 111, transaction ID = 472	
	
	interface.write_sys(1,'LFS_sendDirDelete 0,3,111,472',0)
	time.sleep(1)	
	
	# send the Directory Delete Telecommand. Directory 112 
	# Parameters: latUnit = SIU, dev # = EEPROM1, directory # = 111, transaction ID = 72	
	
	interface.write_sys(1,'LFS_sendDirDelete 0,3,112,72',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 Directory Delete Telecommand.
	# Parameters: latUnit = EPU0, dev # = EEPROM0, directory # = 111, transaction ID = 772
	
	#interface.write_sys(1,'LFS_sendDirDelete 1,2,111,772',0)
	#time.sleep(1)		
	
	# send the Directory Delete Telecommand.
	# Parameters: latUnit = EPU0, dev # = EEPROM1, directory # = 111, transaction ID = 872
	
	#interface.write_sys(1,'LFS_sendDirDelete 1,3,111,872',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_sendDirDelete command that was sent:
	# 1. Reception of the CmdConfirm Telemetry (APID 720) for the sent LFS_sendDirDelete command (APID 1608 FC 3)
	# 2. Verfies that the Command Status field in this telemetry indicates success of execution (LFS_msg must be "Success")
	#    if a dir exists and failure (LFS_msg must be "File open err") if the dir does not exist. 
	# 3. Verfies that deletion of a directory that contains some files returns a Command Status code of failure
	#    (this is not the case currently)
	# 4. Verifies that deletion of an empty directory succeeds.
	# 5. Verifies that deletion of a non-existent direcotry returns a Command Status code of failure
	# 6. sys_status_dump and dir_dump scripts must be run after this test is run and the 
	#    results analyzed therein to verify the successful deletion of the directories 111 and 112.
	
	# analyze the results for Pass or Fail criteria
	# interface.exec_analysis(0,'dir_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()
