blob: a0e3c1801d5d11ab4936f3bd64bbea64f502c1ba [file] [log] [blame]
Oliver Smith4e5e5162020-02-21 08:47:36 +01001package org.osmocom.IMSIPseudo;
2
3import javacard.framework.APDU;
4import javacard.framework.Applet;
5import javacard.framework.ISOException;
6
7import sim.toolkit.EnvelopeHandler;
8import sim.toolkit.ProactiveHandler;
9import sim.toolkit.ToolkitConstants;
10import sim.toolkit.ToolkitException;
11import sim.toolkit.ToolkitInterface;
12import sim.toolkit.ToolkitRegistry;
13
14public class IMSIPseudo extends Applet implements ToolkitInterface, ToolkitConstants {
15 // DON'T DECLARE USELESS INSTANCE VARIABLES! They get saved to the EEPROM,
16 // which has a limited number of write cycles.
17 private byte helloMenuItem;
18
Oliver Smith1e5cc462020-02-21 15:39:14 +010019 static byte[] LUCounter = new byte[] { '0', 'x', ' ', 'L', 'U' };
Oliver Smith2dcbfab2020-02-21 15:40:21 +010020 static byte[] title = new byte[] { 'I', 'M', 'S', 'I', ' ', 'P', 's', 'e', 'u', 'd', 'o', 'n', 'y', 'm',
21 'i', 'z', 'a', 't', 'i', 'o', 'n'};
Oliver Smith4e5e5162020-02-21 08:47:36 +010022
23 private IMSIPseudo() {
24 // This is the interface to the STK applet registry (which is separate
25 // from the JavaCard applet registry!)
26 ToolkitRegistry reg = ToolkitRegistry.getEntry();
27
28 // Define the applet Menu Entry
Oliver Smith2dcbfab2020-02-21 15:40:21 +010029 helloMenuItem = reg.initMenuEntry(title, (short)0, (short)title.length,
Oliver Smith4e5e5162020-02-21 08:47:36 +010030 PRO_CMD_SELECT_ITEM, false, (byte)0, (short)0);
Oliver Smithe28705a2020-02-21 10:06:14 +010031 reg.setEvent(EVENT_EVENT_DOWNLOAD_LOCATION_STATUS);
Oliver Smith4e5e5162020-02-21 08:47:36 +010032 }
33
34 // This method is called by the card when the applet is installed. You must
35 // instantiate your applet and register it here.
36 public static void install(byte[] bArray, short bOffset, byte bLength) {
37 IMSIPseudo applet = new IMSIPseudo();
Oliver Smithe28705a2020-02-21 10:06:14 +010038
Oliver Smith4e5e5162020-02-21 08:47:36 +010039 applet.register();
40 }
41
42 // This processes APDUs sent directly to the applet. For STK applets, this
43 // interface isn't really used.
44 public void process(APDU arg0) throws ISOException {
45 // ignore the applet select command dispached to the process
46 if (selectingApplet())
47 return;
48 }
49
50 // This processes STK events.
51 public void processToolkit(byte event) throws ToolkitException {
52 EnvelopeHandler envHdlr = EnvelopeHandler.getTheHandler();
53
54 if (event == EVENT_MENU_SELECTION) {
55 byte selectedItemId = envHdlr.getItemIdentifier();
56
57 if (selectedItemId == helloMenuItem) {
Oliver Smith234ab542020-02-24 08:25:43 +010058 showMsg(LUCounter);
Oliver Smith4e5e5162020-02-21 08:47:36 +010059 }
60 }
Oliver Smithe28705a2020-02-21 10:06:14 +010061
62 if (event == EVENT_EVENT_DOWNLOAD_LOCATION_STATUS) {
Oliver Smith1e5cc462020-02-21 15:39:14 +010063 LUCounter[0]++;
Oliver Smith234ab542020-02-24 08:25:43 +010064 showMsg(LUCounter);
Oliver Smithe28705a2020-02-21 10:06:14 +010065 }
Oliver Smith4e5e5162020-02-21 08:47:36 +010066 }
67
Oliver Smith234ab542020-02-24 08:25:43 +010068 private void showMsg(byte[] msg) {
Oliver Smith4e5e5162020-02-21 08:47:36 +010069 ProactiveHandler proHdlr = ProactiveHandler.getTheHandler();
Oliver Smith234ab542020-02-24 08:25:43 +010070 proHdlr.initDisplayText((byte)0, DCS_8_BIT_DATA, msg, (short)0, (short)(msg.length));
Oliver Smith4e5e5162020-02-21 08:47:36 +010071 proHdlr.send();
72 return;
73 }
74}