基本信息
源码名称:hce framework
源码大小:3.00M
文件格式:.zip
开发语言:Java
更新时间:2015-09-16
   友情提示:(无需注册或充值,赞助后即可获取资源下载链接)

     嘿,亲!知识可是无价之宝呢,但咱这精心整理的资料也耗费了不少心血呀。小小地破费一下,绝对物超所值哦!如有下载和支付问题,请联系我们QQ(微信同号):813200300

本次赞助数额为: 2 元 
   源码介绍
An android host based card emulation framework for Cyanogenmod 10

package org.kevinvalk.hce.framework;

import java.nio.ByteBuffer;
import java.util.HashMap;
import java.util.Map;

import org.kevinvalk.hce.framework.apdu.Apdu;
import org.kevinvalk.hce.framework.apdu.CommandApdu;
import org.kevinvalk.hce.framework.apdu.ResponseApdu;

public class HceFramework
{
	private Map<ByteBuffer, Applet> applets_;
	private Applet activeApplet_;
	private volatile AppletThread appletThread_;
	
	public HceFramework()
	{
		applets_ = new HashMap<ByteBuffer, Applet>();
		activeApplet_ = null;
		appletThread_ = new AppletThread();
	}
	
	/**
	 * Registers an applet to the framework
	 * 
	 * @param Applet applet
	 * @return boolean
	 */
	public boolean register(Applet applet)
	{
		// If it already contains this AID then just return true
		if (applets_.containsKey(ByteBuffer.wrap(applet.getAid())))
			return true;
		return (applets_.put(ByteBuffer.wrap(applet.getAid()), applet) == null);
	}
	
	/**
	 * Handles a new terminal
	 * 
	 * @param TagWrapper tag
	 * @return boolean
	 */
	public boolean handleTag(TagWrapper tag)
	{
		try
		{
			// Get the first APDU from the tag
			Apdu apdu = AppletThread.getApdu(tag); 

			// Keep trying
			do
			{
				CommandApdu commandApdu = new CommandApdu(apdu);
				
				// SELECT
				if (commandApdu.cla == Iso7816.CLA_ISO7816 && commandApdu.ins == Iso7816.INS_SELECT)
				{
					
					// TODO: Process P1 and P2 and handle that
					
					// We have an applet
					if (applets_.containsKey(ByteBuffer.wrap(commandApdu.getData())))
					{					
						// If we have an active applet deselect it
						if (activeApplet_ != null)
							activeApplet_.deselect();
											
						// Set the applet to active and select it
						activeApplet_ = applets_.get(ByteBuffer.wrap(commandApdu.getData()));
						activeApplet_.select();
						
						// Send an OK and start the applet
						Apdu response = AppletThread.sendApdu(tag, new ResponseApdu(Iso7816.SW_NO_ERROR));
						
						// Stop current applet thread and wait just a bit
						appletThread_.stop();
						Thread.sleep(250);
						
						// Set the applet to the active runnable
						appletThread_.setApplet(activeApplet_, tag);
						appletThread_.setApdu(response);
						
						// Run it
						Thread thread= new Thread(appletThread_);
						thread.setName("JavaCard");
						thread.start();
						
						// Stop trying
						return true;
					}
					else
					{
						// Something went wrong
						apdu = AppletThread.sendApdu(tag, new ResponseApdu(Iso7816.SW_APPLET_SELECT_FAILED));
						continue;
					}
				}
				
				// This is as defined in the specifications
				// If we have an active applet let them process this commandApdu
				if (activeApplet_ != null)
					apdu = AppletThread.sendApdu(tag, activeApplet_.process(commandApdu));
				else
					apdu = AppletThread.sendApdu(tag, new ResponseApdu(Iso7816.SW_INS_NOT_SUPPORTED));
			}
			while(true);
		}
		catch(Exception e)
		{
			return false;
		}
	}
}