优惠券applet

/**
 * File:PCoupon.java Package:com.pervcom.www.pcoupon Project:PCoupon 
 * Date:2010-8-7 Author:Peter.Feng
 * Copyright (c) 2010 Pervcom, Inc. All Rights Reserved.
 */
package com.pervcom.www.pcoupon;

import javacard.framework.APDU;
import javacard.framework.Applet;
import javacard.framework.ISO7816;
import javacard.framework.ISOException;
import javacard.framework.JCSystem;
import javacard.framework.Util;

/**
 * <pre>
 * Author: Peter.Feng
 * Date&Time: 2010-8-7 下午09:50:24
 * PCoupon 
 * TODO 
 * </pre>
 */
public class PCoupon extends Applet {
	
	final static byte CLA_GET_COUPON = (byte)0x00; 		// 读优惠券CLA
	
	final static byte INS_READ_COUPON = (byte)0x5C; 	// 读优惠券INS
	final static byte INS_WRITE_COUPON = (byte)0x5D; 	// 写优惠券INS
	final static byte INS_ACTIVATE = (byte)0x5E; 		// 激活优惠券INS
	
	final static short SW_WRONG_LC = (short)0x6A91; 	// 错误的LC
	
	final static byte MAX_COUPON = (byte)10; 			// 优惠券最大数量.
	
	final static byte OFFSET_USE_STATUS = 0; // 使用状态,1个字节。
	final static byte OFFSET_MERCHANT_CODE = (byte) OFFSET_USE_STATUS + 1; // 商户代码,8个字节。
	final static byte OFFSET_MERCHANT_NAME = (byte) OFFSET_MERCHANT_CODE + 8; // 商户名称,12个字节。
	final static byte OFFSET_COUPON_CODE = (byte) OFFSET_MERCHANT_NAME + 12; // 优惠券代码,9个字节。
	final static byte OFFSET_COUPON_NAME = (byte) OFFSET_COUPON_CODE + 9; // 优惠券名称,20个字节。
	final static byte OFFSET_COUPON_CONTENT = (byte) OFFSET_COUPON_NAME + 20; // 优惠券详情,190个字节。
	final static byte OFFSET_COUPON_PRICE = (byte) (OFFSET_COUPON_CONTENT + 190); // 优惠券金额,4个字节。
	final static byte OFFSET_VALID_DATE = (byte) OFFSET_COUPON_PRICE + 4; // 优惠券日期,4个字节。
	
	CyclicFile couponFile1 = null; // 优惠券文件1
	CyclicFile couponFile2 = null; // 优惠券文件2
	
	// 增加锁定标记
	boolean locked = false;
	
	public PCoupon()
	{
		// 总共248个字节
		couponFile1 = new CyclicFile((byte)MAX_COUPON, (short)122);
		couponFile2 = new CyclicFile((byte)MAX_COUPON, (short)126);
	}	
	
	public static void install(byte[] bArray, short bOffset, byte bLength) {
		PCoupon pcoupon = new PCoupon();
		pcoupon.register(bArray, (short) (bOffset + 1), bArray[bOffset]);
	}

	public void process(APDU apdu) {
		if (selectingApplet()) {
			return;
		}

		byte[] buf = apdu.getBuffer();
		
		byte ins = buf[ISO7816.OFFSET_INS];
		if(locked)
		{
			if(ins != (byte)0x11)
			{
				ISOException.throwIt(ISO7816.SW_INS_NOT_SUPPORTED);
			}
			else
			{
				locked = false;
			}
			return;
		}
		
		switch (buf[ISO7816.OFFSET_INS]) {
		// 锁定
		case(byte)0x01:
		{
			locked = true;
			return;
		}
		case INS_READ_COUPON:{// 读优惠券
			byte recordNum = buf[ISO7816.OFFSET_P1];
			byte[] coupon1 = null;
			byte[] coupon2 = null;
			if(recordNum==(byte)0xA0)
			{
				for(byte i = 1; i < MAX_COUPON; i++)
				{
					coupon1 = couponFile1.getRecord(i);
					coupon2 = couponFile2.getRecord(i);
					if(coupon1[0]==0x01)
					{
						break;
					}
				}
			}else{
				for(byte i = 1; i < MAX_COUPON; i++)
				{					
					coupon1 = couponFile1.getRecord(i);
					coupon2 = couponFile2.getRecord(i);
					if(coupon1[123]==recordNum)
					{
						break;
					}
				}
			}
			Util.arrayCopy(coupon1, (byte)0, buf, (byte)0, (byte)122);
			Util.arrayCopy(coupon2, (byte)0, buf, (byte)122, (byte)126);
			apdu.setOutgoingAndSend((short)0, (short)248);
			return;
		}	
		case INS_WRITE_COUPON:{// 写优惠券
			
			short lc = apdu.setIncomingAndReceive();
			if(lc!=(short)248)
			{
				ISOException.throwIt(SW_WRONG_LC);
			}
			
			JCSystem.beginTransaction();
			byte[] coupon1 = couponFile1.getNewLogRecord();
			Util.arrayCopy(buf, (byte)5, coupon1, (byte)0, (byte)122);
			couponFile1.updateNewLogRecord();
			JCSystem.commitTransaction();
			
			JCSystem.beginTransaction();
			byte[] coupon2 = couponFile2.getNewLogRecord();
			Util.arrayCopy(buf, (byte)127, coupon2, (byte)0, (byte)126);
			couponFile2.updateNewLogRecord();
			JCSystem.commitTransaction();
			return;
		}
		case INS_ACTIVATE:{// 激活优惠券
			byte recordNum = buf[ISO7816.OFFSET_P1];
			byte[] coupon1 = null;
			byte[] coupon2 = null;			
			for(byte i = 1; i < MAX_COUPON; i++)
			{
				coupon1 = couponFile1.getRecord(i);
				coupon2 = couponFile2.getRecord(i);
				//if(i == recordNum)
				if(coupon2[123] == recordNum)
				{
					coupon1[0] = (byte)0x01;
				}
				else
				{
					coupon1[0] = (byte)0x00;
				}
			}
			return;
		}
		default:
			ISOException.throwIt(ISO7816.SW_INS_NOT_SUPPORTED);
		}
		// 000A表示回车换行
	}
}

 

你可能感兴趣的:(applet)