###############################################################################################################
#
# Test Script : mem_pool_status_dump.py
# Configuration Script: mem_pool_status_dump.xml
# Analysis Script: mem_pool_status_dump_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 Pool Status Dump
# Requirement: 5.3.7.11
#              The FSW shall receive as input, from the spacecraft via the CTDB, a command to return 
#              information on the memory pool for a specified unit and memory pool.
#
# system 0 - testbed host
# system 1 - Spacecraft/1553 bus controller
#
# Test Preconditions: 1) 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 (and EPU0) crates must have undergone Primary Boot only.
#                        The test script is run in this mode first and results are recorded and analyzed.
#
#                        Then it 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 LCM - the task to which the MEM task attaches) must have been loaded, 
#                        and all tasks, including (LCM, MEM) must have been initialized and running. 
#                        The same script is run again in the Application Mode.
#
# Test Postconditions: None
#
# Tests on both SIU and EPU0.
# Tests for both System and Application Memory Pool Dumps
###############################################################################################################

# 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 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 = not forward,  new
	
	interface.write_sys(1,'ITC_sendCmdResponse 0,1,0,0,0',0)
	time.sleep(1)

	# send the MEM_sendPoolDump (unit, tran, pool) command for System Pool for SIU
	# Parameters: Unit - LAT unit to receive the command (0 = SIU, 1 = EPU0 ..)
	#             tran - Transaction ID to be echoed in the response Telemetry
	#             pool - Pool ID ( 0 = system, 1 = application)
	
	interface.write_sys(1,'MEM_sendPoolDump 0,3711,0',0)
	time.sleep(1)
	
	# send the MEM_sendPoolDump (unit, tran, pool) command for Application Pool for SIU	
	interface.write_sys(1,'MEM_sendPoolDump 0,3701,1',0)
	time.sleep(1)	
	
	# send the MEM_sendPoolDump (unit, tran, pool) command for System Pool for EPU0	
	#interface.write_sys(1,'MEM_sendPoolDump 1,3702,0',0)
	#time.sleep(1)
	
	# send the MEM_sendPoolDump (unit, tran, pool) command for Application Pool for EPU0	
	#interface.write_sys(1,'MEM_sendPoolDump 1,3703,1',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 MEM_sendPoolDump command that was sent:
	# 1. MEM_sendPoolDump Telecommand is sent (APID 1604 FC 7) 
	# 2. Reception of the CmdConfirm Telemetry (APID 720) for the sent  MEM_sendPoolDump Telecommand (APID 1604/7)	
	#    Verify that the command status field in this telemetry indicates success
	# 3. Reception of the Memory Pool Statistics Dump Telemetry (APID 785)
	# 4. Verify that TLATUNIT in TIBF field contains the correct Lat Unit ID
	# 5. Verify that TTRANID in TIBF field contains the correct Transaction ID
	# 6. Verify that TPOOLID contains the correct Pool ID
	# 7. Verify that TPOOLFREEBYTES contains a valid value (?).
	# 8. Verify that TPOOLFREEBLOCKS contains a valid value (?)
	# 9. Verify that TPOOLMAXBLKBYTES contains a valid value (?)
	# 10. Verify that TPOOLALLOCBYTES contains a valid value(?)
	# 11. Verify that TPOOLALLOCBLOCKS contains a valid value (?)
	#
	# analyze the results for Pass or Fail criteria
	# interface.exec_analysis(0,'mem_pool_status_dump_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()
