001: /*
002: *
003: *
004: * Copyright 1990-2007 Sun Microsystems, Inc. All Rights Reserved.
005: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER
006: *
007: * This program is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU General Public License version
009: * 2 only, as published by the Free Software Foundation.
010: *
011: * This program is distributed in the hope that it will be useful, but
012: * WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * General Public License version 2 for more details (a copy is
015: * included at /legal/license.txt).
016: *
017: * You should have received a copy of the GNU General Public License
018: * version 2 along with this work; if not, write to the Free Software
019: * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA
021: *
022: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
023: * Clara, CA 95054 or visit www.sun.com if you need additional
024: * information or have any questions.
025: */
026:
027: package com.sun.satsa.gsmapplet;
028:
029: import sim.toolkit.*;
030: import javacard.framework.*;
031:
032: /**
033: * This applets is suppose to simulate the working of the main GSM applet in
034: * (U)SIM that controls access to all other applets. No other applet is
035: * selected directly but is invoked by this applet on basis of events.
036: */
037: public class GSMApplet extends Applet implements ToolkitConstants {
038: /**
039: * Instruction byte for an Envelope. Only Envelopes are supported at
040: * this time
041: */
042: private static final byte ENVELOPE_INS = (byte) 0xc2;
043:
044: /** Instruction byte value for GET RESPONSE APDU. */
045: private static final byte GET_RESPONSE_INS = (byte) 0xc0;
046:
047: /**
048: * A field to hold the instance of SATAccessor which is used by the
049: * toolkit classes to set and get the APDU buffer.
050: */
051: AccessSATImpl SATAccessor;
052:
053: /**
054: * Constructor.
055: */
056: private GSMApplet() {
057: register();
058: SATAccessor = new AccessSATImpl();
059: AID aid = JCSystem.getAID();
060: ViewHandler.SATAccessor = (AccessSAT) JCSystem
061: .getAppletShareableInterfaceObject(aid, (byte) 0);
062: }
063:
064: /**
065: * Applet's install method. Creates an instance of GSMApplet
066: * @param bArray parameters array
067: * @param bOffset offset in the parameters array
068: * @param bLength length of parameters
069: */
070: public static void install(byte[] bArray, short bOffset,
071: byte bLength) {
072: new GSMApplet();
073: }
074:
075: /**
076: * This method is called by JCRE before it tries to uninstall this applet.
077: */
078: public void uninstall() {
079: ViewHandler.SATAccessor = null;
080: }
081:
082: /**
083: * Returns shareable interface object to SIM Framework.
084: *
085: * @param clientAID - aid of SIM Framework
086: * @param parameter
087: * @return The current applet instance
088: */
089: public Shareable getShareableInterfaceObject(AID clientAID,
090: byte parameter) {
091: return (Shareable) SATAccessor;
092: }
093:
094: /**
095: * Process method that processes all the APDUs received from the terminal.
096: * @param apdu APDU that has been received from the terminal
097: */
098: public void process(APDU apdu) {
099: byte[] buffer = apdu.getBuffer();
100:
101: // clear the channel information
102: buffer[ISO7816.OFFSET_CLA] = (byte) (buffer[ISO7816.OFFSET_CLA] & (byte) 0xFC);
103:
104: // check SELECT APDU command
105: if ((buffer[ISO7816.OFFSET_CLA] == 0)
106: && (buffer[ISO7816.OFFSET_INS] == (byte) (0xA4)))
107: return;
108:
109: switch (buffer[ISO7816.OFFSET_INS]) {
110: case ENVELOPE_INS:
111: short incomingData = apdu.setIncomingAndReceive();
112: SATAccessor.resetBuffers();
113: SATAccessor.setAPDUBuffer(buffer,
114: (short) (incomingData + 5));
115:
116: for (byte i = 0; i < AccessSATImpl.MAX_LISTENERS; i++) {
117: ToolkitInterface ti = SATAccessor.tiList[i];
118: if (ti == null) {
119: continue;
120: }
121: ViewHandler.currentTI = ti;
122: ti.processToolkit(EVENT_UNFORMATTED_SMS_PP_ENV);
123: if (EnvelopeResponseHandler.status != 0) {
124: short dataLength = SATAccessor.getOutDataLength();
125: short st = (short) ((EnvelopeResponseHandler.status << 8) | dataLength);
126: ISOException.throwIt(st);
127: }
128: }
129: break;
130: case GET_RESPONSE_INS:
131: SATAccessor.setAPDUBuffer(buffer, (short) 5);
132: short dataLength = SATAccessor.getOutDataLength();
133: apdu.setOutgoing();
134: apdu.setOutgoingLength(dataLength);
135: SATAccessor.setOutgoingAPDU();
136: Util.arrayCopy(SATAccessor.getAPDUBuffer(), (short) 0,
137: buffer, (short) 0, dataLength);
138: apdu.sendBytes((short) 0, dataLength);
139: break;
140: default:
141: ISOException.throwIt(ISO7816.SW_INS_NOT_SUPPORTED);
142: }
143: }
144: }
|