📄 jibcardterminal.java
字号:
/*--------------------------------------------------------------------------- * Copyright (C) 1999,2000 Dallas Semiconductor Corporation, All Rights Reserved. * * Permission is hereby granted, free of charge, to any person obtaining a * copy of this software and associated documentation files (the "Software"), * to deal in the Software without restriction, including without limitation * the rights to use, copy, modify, merge, publish, distribute, sublicense, * and/or sell copies of the Software, and to permit persons to whom the * Software is furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included * in all copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. * IN NO EVENT SHALL DALLAS SEMICONDUCTOR BE LIABLE FOR ANY CLAIM, DAMAGES * OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR * OTHER DEALINGS IN THE SOFTWARE. * * Except as contained in this notice, the name of Dallas Semiconductor * shall not be used except as stated in the Dallas Semiconductor * Branding Policy. *--------------------------------------------------------------------------- */package com.dalsemi.onewire.jib.terminal;import java.util.Properties;import java.util.Vector;import java.util.Enumeration;import java.util.Hashtable;import opencard.core.terminal.ResponseAPDU;import opencard.core.terminal.CommandAPDU;import opencard.core.terminal.*;import opencard.core.event.*;import com.dalsemi.onewire.adapter.DSPortAdapter;import java.util.StringTokenizer;import com.dalsemi.onewire.*;import com.dalsemi.onewire.container.OneWireContainer16;/** * The <code>JiBCardTerminal</code> class represents a physical card terminal * for a Java Powered <u>i</u>Button. It polls for Java <u>i</u>Button * insertions and removals. * * @author K * */public class JiBCardTerminal extends CardTerminal implements Pollable{ //THIS IS A HACK FOR LOUSY OPENCARD PROBLEMS!!! See below, search for "LOUSY" private static boolean beenHereBefore = false; private boolean doDebug = true; //private iButtonDriver driver = null; private DSPortAdapter driver = null; private static final byte[] JIB_FAMILY_CODES = { (byte)0x16, (byte)0x96 }; private String address; private Hashtable hash; private Object[] ATRArray; private static int MAX_IBUTTONS = 10; private OneWireContainer16[] javabuttons = new OneWireContainer16[MAX_IBUTTONS]; private long[] buttonaddresses = new long[MAX_IBUTTONS]; private boolean[] added = new boolean[MAX_IBUTTONS]; private boolean[] removed = new boolean[MAX_IBUTTONS]; private boolean firstPoll = true; /** * Instantiate a JiBCardTerminal object. This should be handled invisibly by * the OpenCard internals. * * @param name - The user friendly name of the terminal. * @param type - the port type. * @param address - The address/name of the port (i.e. COM1 or /dev/ttya). */ public JiBCardTerminal(String name, String type, String address) { super(name, type, address); this.address = address; // create a hash table to hold up to 10 iButtons by default hash = new Hashtable(MAX_IBUTTONS); ATRArray = new Object[MAX_IBUTTONS]; } /** * Gets the ROM ID of the currently inserted Java Powered <u>i</u>Button. * Returns an <code>int</code> array for legacy reasons. * * @return the unique serial number of the Java Powered <u>i</u>Button. * * @see #getiButtonAddress(int) */ public int[] getiButtonId(int slot_id) { byte[] addr = getiButtonAddress(slot_id); int[] ret = new int[addr.length]; for (int i=0;i<addr.length;i++) ret[i] = addr[i] & 0x0ff; return ret; } /** * Gets the ROM ID of the currently inserted Java Powered <u>i</u>Button. * * @return the unique serial number of the Java Powered <u>i</u>Button. */ public byte[] getiButtonAddress(int slot_id) { OneWireContainer16 owc16 = javabuttons[slot_id]; byte[] id = new byte[8]; byte[] rom = owc16.getAddress(); System.arraycopy(rom,0,id,0,8); return id; } /** * Gets the <code>com.dalsemi.onewire.container.OneWireContainer</code> * of the currently inserted Java Powered <u>i</u>Button. * * @return container object for communication with Java Powered iButton */ public OneWireContainer16 getContainer(int slot_id) { return javabuttons[slot_id]; } /** * Gets the ROM ID of the adapter. * @return the unique serial number of the adapter. */ public byte[] getAdapterAddress() { try { return com.dalsemi.onewire.utils.Address.toByteArray(driver.getAdapterAddress()); } catch(Exception e) { return new byte[0]; } } /** * Gets the ROM ID of the adapter. * Returns an <code>int</code> array for legacy reasons. * * @return the unique serial number of the adapter. */ public int[] getAdapterId() { byte[] addr = getAdapterAddress(); int[] ret = new int[addr.length]; for (int i=0;i<addr.length;i++) ret[i] = addr[i] & 0x0ff; return ret; } /** * Open the terminal for communication. This begins the polling of the One-Wire bus for * Java Powered <u>i</u>Buttons. */ public void open() throws CardTerminalException { if (driver == null) { try { com.dalsemi.onewire.debug.Debug.debug("Address is "+address+", type is "+type); /* we need to map adapter names here... * ONEWIRE_PORT_TYPE_LEGACY_SERIAL --> {DS9097U} * ONEWIRE_PORT_TYPE_SERIAL --> DS9097U * ONEWIRE_PORT_TYPE_LEGACY_PARALLEL --> {DS1410E} * * also make sure we suppor the legacy ones... * iBUTTON_PORT_TYPE_LEGACY_SERIAL --> {DS9097U} * iBUTTON_PORT_TYPE_SERIAL --> DS9097U * iBUTTON_PORT_TYPE_LEGACY_PARALLEL --> {DS1410E} * */ String mytype = null; if (type.equals("ONEWIRE_PORT_TYPE_LEGACY_SERIAL")) mytype = "{DS9097U}"; else if (type.equals("ONEWIRE_PORT_TYPE_SERIAL")) mytype = "DS9097U"; else if (type.equals("ONEWIRE_PORT_TYPE_LEGACY_PARALLEL")) mytype = "{DS1410E}"; //someday, there will be Java USB support, so go ahead and call it LEGACY now else if (type.equals("ONEWIRE_PORT_TYPE_LEGACY_USB")) mytype = "{DS1490}"; //add more here for the USB adapter...etc //check for legacy types... if (type.equals("iBUTTON_PORT_TYPE_LEGACY_SERIAL")) mytype = "{DS9097U}"; else if (type.equals("iBUTTON_PORT_TYPE_SERIAL")) mytype = "DS9097U"; else if (type.equals("iBUTTON_PORT_TYPE_LEGACY_PARALLEL")) mytype = "{DS1410E}"; if (mytype==null) mytype = type; com.dalsemi.onewire.debug.Debug.debug("Address is "+address+", MY type is "+mytype); driver = com.dalsemi.onewire.OneWireAccessProvider.getAdapter(mytype, address); addSlots(MAX_IBUTTONS); com.dalsemi.onewire.debug.Debug.debug("We are now OPEN"); // Add ourselves to the registry CardTerminalRegistry.getRegistry().addPollable((Pollable) this); /* THIS IS A HACK FOR LOUSY OPENCARD PROBLEMS!!! What happens is the event generator thread is interrupted, so when some later thread somes along and adds itself to the pollable list, EventGenerator thinks it's OK because its internal Thread object (played by the part of 't' below) is not null, even though its not alive anymore, so we don't poll! This is not a problem in normal environments where you have a process that you stop and start over, this is a problem in iB-IDE because we don't run your program in a new process, but rather a new thread group, then to simulate program termination we kill your thread group, which kills the polling thread. */ if (!beenHereBefore) { beenHereBefore = true; } else { //start a new polling thread... Thread t = new Thread(opencard.core.event.EventGenerator.getGenerator()); t.setDaemon(true); t.start(); } /* This is another hack! OpenCard starts polling before the app has registered, so if a button is sitting on the 1-Wire when an app comes up, it is never inserted! This way, we ignore the first poll, giving the app some time to start, and then come up */ firstPoll = true; for (int i=0;i<javabuttons.length;i++) { javabuttons[i] = null; buttonaddresses[i] = 0; } hash.clear(); } catch (OneWireException e) { System.out.println("Couldn't get a port adapter for " + type + " on " + address); System.out.println("Exception was "+e.toString()); } } } /** * Closes the terminal and stops polling the 1-Wire bus for Java Powered <u>i</u>Buttons. */ public void close() { // hack to make it work in browsers. if (driver != null) { try { CardTerminalRegistry.getRegistry().removePollable((Pollable) this); com.dalsemi.onewire.debug.Debug.debug("Calling CLOSE!"); driver.freePort(); driver = null; } catch (Exception e) { System.out.println("Exception in close: "+e); driver = null; } } } /** * Currently there are no internal features supported. This method returns <code>null</code>. * * @return null */ protected Properties internalFeatures(Properties features) { return null; } /** * Determines if there is a Java Powered <u>i</u>Button on the given slot. * * @param int the id of the slot. */ public boolean isCardPresent(int slot_id) throws CardTerminalException { com.dalsemi.onewire.debug.Debug.debug("is card present: "+slot_id); // driver must be installed
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -