JavaLoyaltypublic class JavaLoyalty extends Applet implements com.sun.javacard.samples.SampleLibrary.JavaLoyaltyInterfaceMinimalistic loyalty applet.
JavaPurse uses grantPoints method implementing JavaLoyaltyInterface
to grant loyalty points. JavaLoyalty can process only two command APDUs:
READ_BALANCE and RESET_BALANCE. |
Fields Summary |
---|
static final byte | READ_BALANCE | static final byte | RESET_BALANCE | static final byte | CREDIT | static final byte | DEBIT | static final short | TRANSACTION_TYPE_OFFSET | static final short | TRANSACTION_AMOUNT_OFFSET | static final short | SCALE | static final short | BALANCE_MAX | short | balance |
Constructors Summary |
---|
protected JavaLoyalty(byte[] bArray, short bOffset, byte bLength)Performs memory allocations, initializations, and applet registration
balance = (short)0;
/*
* if AID length is not zero register Java Loyalty
* applet with specified AID
*
* NOTE: all the memory allocations should be performed before register()
*/
byte aidLen = bArray[bOffset];
if (aidLen== (byte)0){
register();
} else {
register(bArray, (short)(bOffset+1), aidLen);
}
|
Methods Summary |
---|
public Shareable | getShareableInterfaceObject(AID clientAID, byte parameter)Implements getShareableInterfaceObject method of Applet class.
JavaLoyalty could check here if the clientAID is that of JavaPurse
Checking of the parameter to be agreed upon value provides additional
security, or, if the Shareable Interface Object weren't JavaLoyalty itself
it could return different Shareable Interface Objects for different values
of clientAID and/or parameter.
SeeJava Card Runtime Environment (JCRE) Specification
for details.
if (parameter == (byte)0)
return this;
else
return null;
| public void | grantPoints(byte[] buffer)Implements main interaction with a client. The data is transfered
through APDU buffer which is a global array accessible from any
context. The format of data in the buffer is subset of Transaction
Log record format: 2 bytes of 0, 1 byte of transaction type, 2 bytes
amount of transaction, 4 bytes of CAD ID, 3 bytes of date, and 2 bytes
of time. This sample implementation ignores everything but transaction
type and amount.
short amount = Util.getShort(buffer, TRANSACTION_AMOUNT_OFFSET);
amount = (short) (amount / SCALE);
switch (buffer[TRANSACTION_TYPE_OFFSET]) {
case DEBIT:
balance = (short)(balance + amount); break;
case CREDIT:
balance = (short) (balance - amount); break;
}
if (balance < 0) balance = 0;
if (balance > BALANCE_MAX) balance = BALANCE_MAX;
/*
* For this simple sample applet we don't keep track of transaction locations and
* timestamps
*/
| public static void | install(byte[] bArray, short bOffset, byte bLength)Installs Java Loyalty applet.
new JavaLoyalty(bArray, bOffset, bLength);
| public void | process(APDU apdu)Dispatches APDU commands.
byte[] buffer = apdu.getBuffer();
// We don't do any additional C-APDU header checking, just rely on
// CLA and INS bytes for dispatching.
if (apdu.isISOInterindustryCLA()) {
if (buffer[ISO7816.OFFSET_INS] == (byte)(0xA4)) {
return;
} else {
ISOException.throwIt (ISO7816.SW_CLA_NOT_SUPPORTED);
}
}
switch (buffer[ISO7816.OFFSET_INS]) {
case READ_BALANCE:
processReadBalance(apdu); break;
case RESET_BALANCE:
processResetBalance(); break;
default:
ISOException.throwIt(ISO7816.SW_INS_NOT_SUPPORTED);
}
| void | processReadBalance(APDU apdu)Sends 2 bytes of balance value in R-APDU.
byte[] buffer = apdu.getBuffer();
Util.setShort(buffer, (short)0, balance);
apdu.setOutgoingAndSend((short)0, (short)2);
| void | processResetBalance()Resets Balance.
balance = (short)0;
|
|