blob: 7959bfc4c9724f424b27921d8e30692f8cd0d695 [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;
Oliver Smithca866fe2020-02-24 09:56:30 +01009import sim.toolkit.ProactiveResponseHandler;
Oliver Smith4e5e5162020-02-21 08:47:36 +010010import sim.toolkit.ToolkitConstants;
11import sim.toolkit.ToolkitException;
12import sim.toolkit.ToolkitInterface;
13import sim.toolkit.ToolkitRegistry;
14
15public class IMSIPseudo extends Applet implements ToolkitInterface, ToolkitConstants {
16 // DON'T DECLARE USELESS INSTANCE VARIABLES! They get saved to the EEPROM,
17 // which has a limited number of write cycles.
Oliver Smith4e5e5162020-02-21 08:47:36 +010018
Oliver Smithca866fe2020-02-24 09:56:30 +010019 private byte STKServicesMenuId;
Oliver Smith1e5cc462020-02-21 15:39:14 +010020 static byte[] LUCounter = new byte[] { '0', 'x', ' ', 'L', 'U' };
Oliver Smithca866fe2020-02-24 09:56:30 +010021
22 /* Main menu */
Oliver Smith2dcbfab2020-02-21 15:40:21 +010023 static byte[] title = new byte[] { 'I', 'M', 'S', 'I', ' ', 'P', 's', 'e', 'u', 'd', 'o', 'n', 'y', 'm',
24 'i', 'z', 'a', 't', 'i', 'o', 'n'};
Oliver Smithca866fe2020-02-24 09:56:30 +010025 static byte[] showLU = new byte[] {'S', 'h', 'o', 'w', ' ', 'L', 'U', ' ', 'c', 'o', 'u', 'n', 't', 'e', 'r'};
26 static byte[] showIMSI = new byte[] {'S', 'h', 'o', 'w', ' ', 'I', 'M', 'S', 'I'};
27 static byte[] changeIMSI = new byte[] {'C', 'h', 'a', 'n', 'g', 'e', ' ', 'I', 'M', 'S', 'I', ' '};
28 private Object[] itemListMain = {title, showLU, showIMSI, changeIMSI};
29
30 /* Change IMSI menu */
31 static byte[] setDigit1 = new byte[] {'S', 'e', 't', ' ', '1', ' ', 'a', 's', ' ', 'l', 'a', 's', 't', ' ',
32 'd', 'i', 'g', 'i', 't'};
33 static byte[] setDigit2 = new byte[] {'S', 'e', 't', ' ', '2', ' ', 'a', 's', ' ', 'l', 'a', 's', 't', ' ',
34 'd', 'i', 'g', 'i', 't'};
35 private Object[] itemListChangeIMSI = {changeIMSI, setDigit1, setDigit2};
Oliver Smith4e5e5162020-02-21 08:47:36 +010036
37 private IMSIPseudo() {
Oliver Smithca866fe2020-02-24 09:56:30 +010038 /* Register menu and trigger on location updates */
Oliver Smith4e5e5162020-02-21 08:47:36 +010039 ToolkitRegistry reg = ToolkitRegistry.getEntry();
Oliver Smithca866fe2020-02-24 09:56:30 +010040 STKServicesMenuId = reg.initMenuEntry(title, (short)0, (short)title.length, PRO_CMD_SELECT_ITEM, false,
41 (byte)0, (short)0);
Oliver Smithe28705a2020-02-21 10:06:14 +010042 reg.setEvent(EVENT_EVENT_DOWNLOAD_LOCATION_STATUS);
Oliver Smith4e5e5162020-02-21 08:47:36 +010043 }
44
Oliver Smith4e5e5162020-02-21 08:47:36 +010045 public static void install(byte[] bArray, short bOffset, byte bLength) {
46 IMSIPseudo applet = new IMSIPseudo();
47 applet.register();
48 }
49
Oliver Smith4e5e5162020-02-21 08:47:36 +010050 public void process(APDU arg0) throws ISOException {
Oliver Smith4e5e5162020-02-21 08:47:36 +010051 if (selectingApplet())
52 return;
53 }
54
Oliver Smith4e5e5162020-02-21 08:47:36 +010055 public void processToolkit(byte event) throws ToolkitException {
56 EnvelopeHandler envHdlr = EnvelopeHandler.getTheHandler();
57
58 if (event == EVENT_MENU_SELECTION) {
59 byte selectedItemId = envHdlr.getItemIdentifier();
60
Oliver Smithca866fe2020-02-24 09:56:30 +010061 if (selectedItemId == STKServicesMenuId) {
62 showMenu(itemListMain, (byte)4);
63 handleMenuResponseMain();
Oliver Smith4e5e5162020-02-21 08:47:36 +010064 }
65 }
Oliver Smithe28705a2020-02-21 10:06:14 +010066
67 if (event == EVENT_EVENT_DOWNLOAD_LOCATION_STATUS) {
Oliver Smith1e5cc462020-02-21 15:39:14 +010068 LUCounter[0]++;
Oliver Smith234ab542020-02-24 08:25:43 +010069 showMsg(LUCounter);
Oliver Smithe28705a2020-02-21 10:06:14 +010070 }
Oliver Smith4e5e5162020-02-21 08:47:36 +010071 }
72
Oliver Smith234ab542020-02-24 08:25:43 +010073 private void showMsg(byte[] msg) {
Oliver Smith4e5e5162020-02-21 08:47:36 +010074 ProactiveHandler proHdlr = ProactiveHandler.getTheHandler();
Oliver Smith234ab542020-02-24 08:25:43 +010075 proHdlr.initDisplayText((byte)0, DCS_8_BIT_DATA, msg, (short)0, (short)(msg.length));
Oliver Smith4e5e5162020-02-21 08:47:36 +010076 proHdlr.send();
77 return;
78 }
Oliver Smithca866fe2020-02-24 09:56:30 +010079
80 private void showMenu(Object[] itemList, byte itemCount) {
81 ProactiveHandler proHdlr = ProactiveHandler.getTheHandler();
82 proHdlr.init((byte) PRO_CMD_SELECT_ITEM,(byte)0,DEV_ID_ME);
83
84 for (byte i=(byte)0;i<itemCount;i++) {
85 if (i == 0) {
86 /* Title */
87 proHdlr.appendTLV((byte)(TAG_ALPHA_IDENTIFIER | TAG_SET_CR), (byte[])itemList[i],
88 (short)0, (short)((byte[])itemList[i]).length);
89
90 } else {
91 /* Menu entry */
92 proHdlr.appendTLV((byte)(TAG_ITEM | TAG_SET_CR), (byte)i, (byte[])itemList[i], (short)0,
93 (short)((byte[])itemList[i]).length);
94 }
95 }
96 proHdlr.send();
97 }
98
99 private void handleMenuResponseMain() {
100 ProactiveResponseHandler rspHdlr = ProactiveResponseHandler.getTheHandler();
101
102 switch (rspHdlr.getItemIdentifier()) {
103 case 1: /* Show LU counter */
104 showMsg(LUCounter);
105 break;
106 case 2: /* Show IMSI */
107 /* TODO */
108 break;
109 case 3: /* Change IMSI */
110 showMenu(itemListChangeIMSI, (byte)3);
111 handleMenuResponseChangeIMSI();
112 break;
113 }
114 }
115
116 private void handleMenuResponseChangeIMSI() {
117 /* TODO */
118 }
Oliver Smith4e5e5162020-02-21 08:47:36 +0100119}