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.midp.io.j2me.apdu;
028:
029: /**
030: * The TLP224Message class represents the link level message between a CAD
031: * (Card Acceptance Device) and a Terminal. All messages exchanged between
032: * the CAD and the Terminal are encoded using the TLP224 protocol.
033: * TLP224Messages are used internally to maintain context on the server
034: * side and to send commands to and from the client.
035: */
036: class TLP224Message extends Exception {
037:
038: /**
039: * This field contains the EOT code which is always sent as the last
040: * octet of any TLP224 message.
041: */
042: static final byte EOT = 0x03;
043:
044: /**
045: * This field contains the ACK code which is returned to the sender as
046: * the first octet of a TLP224 response when a message has been
047: * successfully received.
048: */
049: static final byte ACK = 0x60;
050:
051: /**
052: * This field contains the NACK code which is returned to the sender as
053: * the first octet of a TLP224 response when a transmission error occurs.
054: */
055: static final byte NACK = (byte) 0xe0;
056:
057: /**
058: * This field contains the TLP224 command to power up the CAD.
059: */
060: static final byte POWER_UP = 0x6e;
061:
062: /**
063: * This field contains the TLP224 command to send data to the card.
064: */
065: static final byte ISO_INPUT = (byte) 0xda;
066:
067: /**
068: * This field contains the TLP224 command to read data from the card.
069: */
070: static final byte ISO_OUTPUT = (byte) 0xdb;
071:
072: /**
073: * This status code is returned by both the Client and Server CAD's when
074: * a command has been successfully executed.
075: */
076: static final byte STATUS_SUCCESS = 0;
077:
078: /**
079: * This status code is returned by both the Client and Server CAD's when
080: * the first byte of a received message is neither an ACK or NACK.
081: */
082: static final byte STATUS_PROTOCOL_ERROR = 0x09;
083:
084: /**
085: * This status code is returned by both the Client and Server CAD's
086: * when a received message exceeds the length of the internal buffers.
087: */
088: static final byte STATUS_MESSAGE_TOO_LONG = 0x12;
089:
090: /**
091: * This status code is returned by the reader or ServerCad if the card
092: * sends a Procedure Byte which aborts a ISO_IN or ISO_OUT command.
093: */
094: static final byte STATUS_INTERRUPTED_EXCHANGE = (byte) 0xe5;
095:
096: /**
097: * This status code is returned by the reader or ServerCad
098: * if SW1 SW2 are not equal to 0x9000.
099: */
100: static final byte STATUS_CARD_ERROR = (byte) 0xe7;
101:
102: /**
103: * This status code is returned by the reader or ServerCad if the card was
104: * removed between exchanges.
105: */
106: static final byte STATUS_CARD_REMOVED = (byte) 0xf7;
107:
108: /**
109: * This status code is returned by the reader or ServerCad if there is
110: * no card in the reader.
111: */
112: static final byte STATUS_CARD_MISSING = (byte) 0xfb;
113:
114: /**
115: * This field contains the size of the largest possible TLP224 Message.
116: * This message would be <ACK><LEN><255 bytes of
117: * command><LRC>
118: */
119: private static final int MAX_MESSAGE_LEN = 264;
120:
121: /**
122: * Local buffer for this TLP224 message.
123: */
124: private byte[] buf;
125:
126: /**
127: * Current length of data in local buffer.
128: */
129: private int len;
130:
131: /**
132: * Construct a new TLP224Message using the default (MAX_MESSAGE_LEN)
133: * message size.
134: */
135: TLP224Message() {
136: this .buf = new byte[MAX_MESSAGE_LEN];
137: this .len = 0;
138: }
139:
140: /**
141: * Retrieves the contents of this TLP224Message.
142: * @return The non-null byte array containing the data to be sent or
143: * received. Any changes
144: * to this byte array also affect the TLP224Message from which it came.
145: */
146: byte[] getData() {
147: return buf;
148: }
149:
150: /**
151: * Retrieves the length of this TLP224Message.
152: * @return The length in bytes of the internal buffer as previously set
153: * by setLength().
154: */
155: int getLength() {
156: return len;
157: }
158:
159: /**
160: * Set the length of the data in this TLP224Message.
161: * The length must be less than or equal to the length of the internal
162: * buffer.
163: * @param newLen The length to set.
164: * @exception IllegalArgumentException if newLen is greater than the
165: * length of the internal buffer.
166: */
167: void setLength(int newLen) {
168: this .len = newLen;
169: }
170:
171: /**
172: * Compute the TLP224 LRC of this object.
173: * The TLP224 LRC is the exclusive-or of all the bytes in the message.
174: * @param length The number of bytes to compute the LRC over.
175: * @return The computed LRC.
176: */
177: byte computeLRC(int length) {
178: int lrc = 0;
179: for (int i = 0; i < length; i++)
180: lrc ^= buf[i];
181: return (byte) lrc;
182: }
183: }
|