//  3GPP TSG-T3 (USIM) ad hoc meeting #6                         Tdoc T3z000003
//  Madrid, Spain, 22 - 23 June, 2000	
//
//
//-----------------------------------------------------------------------------
//
//GEMPLUS/Nicolas Chalvin
//
// a_init_nrm01.java
// version 1.0.0
//
// Package sim.toolkit
// class ProactiveHandler
// method init( )
// test script associated : init_nrm01.doc
//
// the applet is triggered on Envelope Menu Selection
// the result is stored in the file EFtest under the GSM file system
//
//-----------------------------------------------------------------------------



//-----------------------------------------------------------------------------
// PACKAGE DEFINITION
//----------------------------------------------------------------------------
package Output;

//-----------------------------------------------------------------------------
// IMPORTS
//-----------------------------------------------------------------------------
import javacard.framework.*;
import sim.access.*;
import sim.toolkit.*;


public class a_init_nrm01 extends Applet implements ToolkitInterface, ToolkitConstants {

    /** internal data */

    // ------------------------------- Constants ------------------------------
    // Error report constants
    private static final short UNEXPECTED_EXCEPTION = (short) 0x1001;
    private static final short NO_EXCEPTION =         (short) 0x1002;
    private static final short WRONG_COMPARISON =     (short) 0x1003;
    private static final short WRONG_RETURNED_VALUE = (short) 0x1004;
    private static final short NO_ERROR =             (short) 0x9000;
    private static final short INDEX_NO_ERROR = (short) 0;
    private static final short EF_TEST_ID = (short) 0xFFFB;
    private static final short MF_ID = (short) 0x3F00;
    private static final byte  ERR_LENGTH = (byte) 4;


    // ------------------------------- Variables ------------------------------

    private SIMView theGsmApplet;
    private ToolkitRegistry reg;
    private byte[] menu     = {(byte)'A',(byte)'p',(byte)'p',(byte)'l',(byte)'e',(byte)'t',(byte)'1'};

    private byte[] ErrBuffer;


    // ------------------------------- Constructors ---------------------------
    /**
     * Constructor of applet.
     */

    public a_init_nrm01() {

        // get a ToolkitRegistry object...
        reg = ToolkitRegistry.getEntry();
        // ...and initialize it according to the applet characteristics

        reg.initMenuEntry(menu, (short) 0, (short)menu.length, (byte)0x00, false, (byte)0, (byte)0);


        // get a reference to the GSM interface
        theGsmApplet = SIMSystem.getTheSIMView();
        // build byte arrays
        ErrBuffer = new byte[ERR_LENGTH];
    }


    // ------------------------------- Public methods -------------------------
    /**
     * Install method.
     */
    public static void install(byte bArray[], short bOffset, byte bLength) throws ISOException {
        // create and register applet
        a_init_nrm01 a_init_nrm01Applet = new a_init_nrm01();
        a_init_nrm01Applet.register();
    }

    /**
     * Process toolkit events.
     */

    public void processToolkit(byte event) throws ToolkitException {

        short CommandIndex = (short) 0 ;
        ProactiveHandler proHdlr = ProactiveHandler.getTheHandler() ;

        try {

            // Init Command with known values

            CommandIndex = (short) 1 ;
            proHdlr.init((byte)0xFF, (byte)0xFE, (byte)0xFD) ;

            // Length of handler must be 9

            CommandIndex = (short) 2 ;
            if (proHdlr.getLength() != 9) {
                UpdateReport(WRONG_RETURNED_VALUE, CommandIndex) ;
                return ;
            }

        } catch (Exception e) {
            UpdateReport(UNEXPECTED_EXCEPTION, CommandIndex) ;
            return ;
        }

        UpdateReport(NO_ERROR, INDEX_NO_ERROR) ;
    }

    private void UpdateReport(short Reason, short CommandIndex) {
        // Select error file
        theGsmApplet.select( MF_ID);
        theGsmApplet.select( EF_TEST_ID);
        // Format data
        Util.setShort(ErrBuffer, (short) 0, (short) Reason);
        Util.setShort(ErrBuffer, (short) 2, (short) CommandIndex);
        // Update file
        theGsmApplet.updateBinary((short) 0, ErrBuffer, (short) 0, ERR_LENGTH);
    }

    public void process(APDU apdu) throws ISOException{
    }

}