###############################################################################################################
#
# Test Script : dir_create.py
# Configuration Script: dir_create.xml
# Analysis Script: dir_create_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 Create 
# Requirement: 5.3.7.1
#              In order to create 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 and EPU1 crates 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 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 
#                        directories 111, 112 and 113 do not exist prior to running this script by verifying the
#                        reports of the above scripts. 
#
# Test Postconditions: 1) Test scripts sys_status_dump (5_3_7_8) and dir_dump (5_3_7_7) must be run after this
#                         test to verify the successful creation of the Directories 111 and 112.
#                      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:
#                         a) file_load(5_3_7_9)
#                         b) file_load_cancel (5_3_7_10)
#                         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)
#                         g) directory_delete (5_3_7_2)
#
# Tests on both SIU and EPU0.
# Tests on both banks: EEPROM0, EEPROM1
# Tests creation of a directory that already exists.
# Four Directories (111) are created one for each bank (EEPROM0 and EEPROM1) for both the SIU and EPU0
# Files can be uploaded into 111 for testing scripts as mentioned in clause (2) of Test Postconitions.
# One Directory (112) is created and will remain empty for all tests in this test procedure.
# One Directory (113) will never be created and remain as non-existent for future tests.
#
###############################################################################################################

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 Create Telecommand for SIU Bank 0.
	# Parameters: latUnit = SIU, dev # = EEPROM0, directory # = 111, transaction ID = 371
	
	interface.write_sys(1,'LFS_sendDirCreate 0,2,111,371',0)
	time.sleep(1)	
	
	# create a directory that will remain empty for all other tests in this procedure.
	interface.write_sys(1,'LFS_sendDirCreate 0,2,112,471',0)
	time.sleep(1)			
	
	# try to create a directory that already exists
	interface.write_sys(1,'LFS_sendDirCreate 0,2,111,571',0)
	time.sleep(1)		
	
	# send the Directory Create Telecommand for SIU Bank 1.
	# Parameters: latUnit = SIU, dev # = EEPROM1, directory # = 111, transaction ID = 671
	
	interface.write_sys(1,'LFS_sendDirCreate 0,3,111,671',0)
	time.sleep(1)	
	
	# create a directory that will remain empty for all other tests in this procedure.
	interface.write_sys(1,'LFS_sendDirCreate 0,3,112,771',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 Create Telecommand for EPU0 Bank 0.
	# Parameters: latUnit = EPU0, dev # = EEPROM0, directory # = 111, transaction ID = 871
	
	#interface.write_sys(1,'LFS_sendDirCreate 1,2,111,871',0)
	#time.sleep(1)	
	
	# send the Directory Create Telecommand for EPU0 Bank 1.
	# Parameters: latUnit = EPU0, dev # = EEPROM1, directory # = 111, transaction ID = 971

	#interface.write_sys(1,'LFS_sendDirCreate 1,3,111,971',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_sendDirCreate command that was sent:
	# 1. Reception of the CmdConfirm Telemetry (APID 720) for the sent LFS_sendDirCreate command (APID 1608 FC 2)
	# 2. Verfies that the Command Status field in this telemetry indicates success of execution (LFS_msg must be "Success") 
	# 3. Verifies that the Command Status field in this telemetry indicates failure for creation of an existing direcotry
	#    (currently this is not the case - it still indicates success).
	# 4. 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 directories 111 and 112.
	
	# analyze the results for Pass or Fail criteria
	# interface.exec_analysis(0,'dir_create_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()
