Created
December 3, 2020 14:53
-
-
Save staybuzz/5a76d15da57e6b1a31e1e6efa0c71192 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package org.toorcamp.HelloSTK; | |
import javacard.framework.APDU; | |
import javacard.framework.Applet; | |
import javacard.framework.ISOException; | |
import sim.toolkit.EnvelopeHandler; | |
import sim.toolkit.ProactiveHandler; | |
import sim.toolkit.ToolkitConstants; | |
import sim.toolkit.ToolkitException; | |
import sim.toolkit.ToolkitInterface; | |
import sim.toolkit.ToolkitRegistry; | |
public class HelloSTK extends Applet implements ToolkitInterface, ToolkitConstants { | |
// DON'T DECLARE USELESS INSTANCE VARIABLES! They get saved to the EEPROM, | |
// which has a limited number of write cycles. | |
private byte helloMenuItem; | |
private byte menuItemPlayTone; | |
private byte menuItemPlayTone2; | |
static byte[] welcomeMsg = new byte[] { 'W', 'e', 'l', 'c', 'o', 'm', 'e', ' ', | |
't', 'o', ' ', 'T', 'o', 'o', 'r', 'C', | |
'a', 'm', 'p', ' ', '2', '0', '1', '2' }; | |
static byte[] msg = new byte[] {'H', 'a', 'i', 's', 'a', 'i', '!'}; | |
static byte[] menuItemText = new byte[] { 'H', 'e', 'l', 'l', 'o', ',', ' ', 'S', 'T', 'K'}; | |
static byte[] menuItemText2 = new byte[] { 'P', 'l', 'a', 'y', 'T', 'o', 'n', 'e', '1' }; | |
static byte[] menuItemText3 = new byte[] { 'P', 'l', 'a', 'y', 'T', 'o', 'n', 'e', '2' }; | |
// variable for storing location data | |
private byte[] locationData = new byte[0x20]; | |
private HelloSTK() { | |
// This is the interface to the STK applet registry (which is separate | |
// from the JavaCard applet registry!) | |
ToolkitRegistry reg = ToolkitRegistry.getEntry(); | |
// Define the applet Menu Entry | |
helloMenuItem = reg.initMenuEntry(menuItemText, (short)0, (short)menuItemText.length, | |
PRO_CMD_SELECT_ITEM, false, (byte)0, (short)0); | |
menuItemPlayTone = reg.initMenuEntry(menuItemText2, (short)0, (short)menuItemText2.length, | |
PRO_CMD_SELECT_ITEM, false, (byte)0, (short)0); | |
menuItemPlayTone2 = reg.initMenuEntry(menuItemText3, (short) 0, (short) menuItemText3.length, | |
PRO_CMD_SELECT_ITEM, false, (byte) 0, (short) 0); | |
// register events | |
reg.setEvent(EVENT_EVENT_DOWNLOAD_LOCATION_STATUS); // this constant is defined in sim.toolkit.ToolkitConstants | |
} | |
// This method is called by the card when the applet is installed. You must | |
// instantiate your applet and register it here. | |
public static void install(byte[] bArray, short bOffset, byte bLength) { | |
HelloSTK applet = new HelloSTK(); | |
applet.register(); | |
} | |
// This processes APDUs sent directly to the applet. For STK applets, this | |
// interface isn't really used. | |
public void process(APDU arg0) throws ISOException { | |
// ignore the applet select command dispached to the process | |
if (selectingApplet()) | |
return; | |
} | |
// This processes STK events. | |
public void processToolkit(byte event) throws ToolkitException { | |
EnvelopeHandler envHdlr = EnvelopeHandler.getTheHandler(); | |
// When a menu item is selected | |
if (event == EVENT_MENU_SELECTION) { | |
byte selectedItemId = envHdlr.getItemIdentifier(); | |
if (selectedItemId == helloMenuItem) { | |
showHello(); | |
} | |
else if (selectedItemId == menuItemPlayTone) { | |
// displayText(msg); | |
playTone((byte)0x01, (byte)0x02); | |
playTone((byte)0x11, (byte)0x01); | |
} | |
else if (selectedItemId == menuItemPlayTone2) { | |
for(short x=0; x<0x16; x++) { playTone((byte)x, (byte)0x02); } | |
playTone((byte)0x01, (byte)0x05); | |
for(short x=0x30; x<0x48; x++) { playTone((byte)x, (byte)0x02); } | |
// playTone((byte)0x11, (byte)0x01); | |
} | |
} | |
} | |
private void showHello() { | |
ProactiveHandler proHdlr = ProactiveHandler.getTheHandler(); | |
proHdlr.initDisplayText((byte)0, DCS_8_BIT_DATA, welcomeMsg, (short)0, | |
(short)(welcomeMsg.length)); | |
proHdlr.send(); | |
return; | |
} | |
// display any ascii text | |
private void displayText(byte[] msg) { | |
ProactiveHandler proHdlr = ProactiveHandler.getTheHandler(); | |
proHdlr.initDisplayText((byte)0, DCS_8_BIT_DATA, msg, (short)0, (short)(msg.length)); | |
proHdlr.send(); | |
return; | |
} | |
private void playTone(byte tone, byte durSecond) { | |
// Proactive commandを扱うハンドラを用意する | |
ProactiveHandler proHdlr = ProactiveHandler.getTheHandler(); | |
// Proactive commandを送るためのTLVを準備する | |
// initではProactive UICC command Tag, Command details, Device identitiesを生成する | |
// 引数:使用するコマンド, Command Qualifier(指定できる値はコマンドにより異なる), コマンドの送信先(送信元はUICC固定) | |
proHdlr.init(PRO_CMD_PLAY_TONE, (byte)0x01, DEV_ID_EARPIECE); | |
// appendTLVはinitで生成したTLVに対して新しくTLVを追加する | |
// 引数の形式は数パターンある。ETSI TS 101 476に付属のAPIリファレンス参照 | |
proHdlr.appendTLV(TAG_DURATION, (byte)0x01, durSecond); // Tag, Value1:Time unit(0x01: second), Value2:Time interval | |
proHdlr.appendTLV(TAG_TONE, tone); // Tag, Value:Type of tone | |
proHdlr.send(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment