###############################################################################################################
#
# Test Script : mem_dump_cancel.py
# Configuration Script: mem_dump_cancel.xml
# Analysis Script: mem_dump_cancel_analysis.py
#
# Script Author : Shantha Condamoor
# Main FSW Package: MEM
# FSW Package Author : Don May
# Test Type: 002b
# Test Procedure: File Management and Memory Upload and Dump Verification
# Test Name: Memory Dump Cancel
# Requirement: 5.3.7.12.4
#              The FSW shall process a command to cancel a memory dump.
#
# system 0 - testbed host
# system 1 - Spacecraft/1553 bus controller
#
# Test Preconditions: 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 must have undergone Primary Boot only.
#                     The test script is run in this mode first and results are recorded and analyzed.
#
#                     Then SIU 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 MEM) must have been loaded, and all tasks, including (LCM, ITC) 
#                     must have been initialized and running. The same script is run again in the Application Mode.
#
# Test Postconditions: None
#
# Tests the SIU.
# 
###############################################################################################################

# 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 500',0)
		
        time.sleep(1)
	
	# enable ITC Cmd Responses from nid=SIU, for the LAT LCM Master task (1)
	# for normal (not broadcast) commands from SC, when the task is functioning
	# purely as an executing task (0) 
	# Parameters: node id = SIU, Task ID = LCM, class = normal, action = forward,  new
	
	interface.write_sys(1,'ITC_sendCmdResponse 0,1,0,1,0',0)
	time.sleep(1)
	
	# Set to highest level of verbosity level of MEM command display
	interface.write_sys(1,'MEM_setDisplayLevelCmd 5',0)
	time.sleep(1)			
	
	# send the MEM dump command
	# Parameters: unit,start, count
	#           unit: LAT unit to receive the command (0=SIU, 1 = EPU0 ...)
	#           start: Starting address
	#           count: Number of 32-bit words to dump
	interface.write_sys(1,'MEM_sendDump 0,0,32000',0)
	time.sleep(3)	
	
	# cancel the ongoing dump
	# Parameters: LAT unit to receive the command	
	interface.write_sys(1,'MEM_sendCancel 0',0)
	time.sleep(2)
	
	#interface.write_sys(1,'MEM_sendDump 1,0,32000',0)
	#time.sleep(2)	
	
	# cancel the ongoing dump
	# Parameters: LAT unit to receive the command
	#interface.write_sys(1,'MEM_sendCancel 1',0)
	#time.sleep(2)				

        #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 MEM_sendCancel command that was sent:
	# 1. MEM_sendCancel Telecommand is sent (APID 1604 FC 1) 
	# 2. Reception of the CmdConfirm Telemetry (APID 720) for the sent  MEM_sendCancel Telecommand (APID 1604/1)	
	#    Verify that the command status field in this telemetry indicates success	
	# 3. Verify that the memory dump was stopped before all data was dumped.
	
	# analyze the results for Pass or Fail criteria
	# interface.exec_analysis(0,'mem_dump_cancel_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()
