Project

General

Profile

« Previous | Next » 

Revision 483f5a4f

Added by Neels Hofmeyr about 4 years ago

refactor the menu, the mi2str() implementation, and tests

Instead of a sub-menu for changing the IMSI, just offer a dialog with the
current IMSI pre-filled in an edit field.
"Show IMSI" becomes just "Change IMSI" without changing anything.

Change mi2str() so that it simply returns a byte[] of the right size.

Test new mi2str()

View differences:

sim-applet/src/org/osmocom/IMSIPseudo/IMSIPseudo.java
19 19
	private static final byte[] title = { 'I', 'M', 'S', 'I', ' ', 'P', 's', 'e', 'u', 'd', 'o', 'n', 'y', 'm',
20 20
					   'i', 'z', 'a', 't', 'i', 'o', 'n'};
21 21
	private static final byte[] showLU = {'S', 'h', 'o', 'w', ' ', 'L', 'U', ' ', 'c', 'o', 'u', 'n', 't', 'e', 'r'};
22
	private static final byte[] showIMSI = {'S', 'h', 'o', 'w', ' ', 'I', 'M', 'S', 'I'};
23
	private static final byte[] changeIMSI = {'C', 'h', 'a', 'n', 'g', 'e', ' ', 'I', 'M', 'S', 'I', ' '};
24
	private final Object[] itemListMain = {title, showLU, showIMSI, changeIMSI};
25

  
26
	/* Change IMSI menu */
27
	private static final byte[] enterIMSI = {'E', 'n', 't', 'e', 'r', ' ', 'I', 'M', 'S', 'I' };
28
	private static final byte[] setDigit1 = {'S', 'e', 't', ' ', '1', ' ', 'a', 's', ' ', 'l', 'a', 's', 't', ' ',
29
						  'd', 'i', 'g', 'i', 't'};
30
	private static final byte[] setDigit2 = {'S', 'e', 't', ' ', '2', ' ', 'a', 's', ' ', 'l', 'a', 's', 't', ' ',
31
						  'd', 'i', 'g', 'i', 't'};
32
	private final Object[] itemListChangeIMSI = {changeIMSI, enterIMSI, setDigit1, setDigit2};
22
	private static final byte[] changeIMSI = {'C', 'h', 'a', 'n', 'g', 'e', ' ', 'I', 'M', 'S', 'I'};
23
	private static final byte[] invalidIMSI = {'I', 'n', 'v', 'a', 'l', 'i', 'd', ' ', 'I', 'M', 'S', 'I'};
24
	private static final byte[] noChange = {'N', 'o', ' ', 'c', 'h', 'a', 'n', 'g', 'e'};
25
	private static final byte[] changed = {'I', 'M', 'S', 'I', ' ', 'c', 'h', 'a', 'n', 'g', 'e', 'd', '!'};
26
	private static final byte error[] = {'E', 'R', 'R', 'O', 'R' };
27
	private final Object[] itemListMain = {title, showLU, changeIMSI};
33 28

  
34 29
	private IMSIPseudo() {
35 30
		gsmFile = SIMSystem.getTheSIMView();
......
172 167
		showMsg(msg);
173 168
	}
174 169

  
175

  
176
	private void showIMSI() {
177
		/* 3GPP TS 31.102 4.2.2: IMSI */
178
		byte[] msg = {'C', 'u', 'r', 'r', 'e', 'n', 't', ' ', 'I', 'M', 'S', 'I', ':', ' ',
179
			      ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '};
180

  
181
		try {
182
			byte IMSI[] = readIMSI();
183
			MobileIdentity.mi2str(msg, (byte)14, (byte)16, IMSI, false);
184
			showMsgAndWaitKey(msg);
185
		} catch (SIMViewException e) {
186
			showError(e.getReason());
187
		}
188
	}
189

  
190 170
	private void handleMenuResponseMain() {
191 171
		ProactiveResponseHandler rspHdlr = ProactiveResponseHandler.getTheHandler();
192 172

  
......
194 174
		case 1: /* Show LU counter */
195 175
			showMsg(LUCounter);
196 176
			break;
197
		case 2: /* Show IMSI */
198
			showIMSI();
199
			break;
200
		case 3: /* Change IMSI */
201
			showMenu(itemListChangeIMSI);
202
			handleMenuResponseChangeIMSI();
177
		case 2: /* Change IMSI */
178
			byte prevIMSI_mi[] = readIMSI();
179
			byte prevIMSI_str[] = MobileIdentity.mi2str(prevIMSI_mi);
180
			promptIMSI(prevIMSI_str);
203 181
			break;
204 182
		}
205 183
	}
206 184

  
207
	private void handleMenuResponseChangeIMSI() {
208
		ProactiveResponseHandler rspHdlr = ProactiveResponseHandler.getTheHandler();
209
		switch (rspHdlr.getItemIdentifier()) {
210
		case 1: /* enter IMSI */
211
			promptIMSI();
212
			break;
213
		case 2: /* set last digit to 1 */
214
			promptIMSI();
215
			break;
216
		case 3: /* set last digit to 2 */
217
			promptIMSI();
218
			break;
185
	private void promptIMSI(byte prevIMSI_str[])
186
	{
187
		byte newIMSI_str[] = prevIMSI_str;
188

  
189
		try {
190
			newIMSI_str = prompt(changeIMSI, newIMSI_str, (short)0, (short)15);
191
		} catch (Exception e) {
192
			showError((short)40);
193
			return;
194
		}
195

  
196
		if (newIMSI_str.length < 6 || newIMSI_str.length > 15
197
		    || !Bytes.isDigit(newIMSI_str)) {
198
			showMsg(invalidIMSI);
199
			return;
200
		}
201

  
202
		if (Bytes.equals(newIMSI_str, prevIMSI_str)) {
203
			showMsg(noChange);
204
			return;
219 205
		}
220
	}
221 206

  
222
	private void promptIMSI()
223
	{
224
		byte[] msg = {'N', 'e', 'w', ' ', 'I', 'M', 'S', 'I', '?'};
225
		byte imsi[] = prompt(msg, (short)0, (short)15);
226
		/* The IMSI file should be 9 bytes long, even if the IMSI is shorter */
227 207
		byte mi[];
228 208
		try {
229
			mi = MobileIdentity.str2mi(imsi, MobileIdentity.MI_IMSI, (byte)9);
209
			/* The IMSI file should be 9 bytes long, even if the IMSI is shorter */
210
			mi = MobileIdentity.str2mi(newIMSI_str, MobileIdentity.MI_IMSI, (byte)9);
230 211
			writeIMSI(mi);
212
			showMsg(changed);
231 213
		} catch (Exception e) {
232
			byte err[] = {'E', 'R', 'R' };
233
			showMsgAndWaitKey(err);
214
			showError((short)42);
234 215
		}
235 216
	}
236 217

  
sim-applet/src/org/osmocom/IMSIPseudo/MobileIdentity.java
52 52
		return rc;
53 53
	}
54 54

  
55
	public static byte mi2str(byte dst[], byte dst_ofs, byte dst_len,
56
				  byte mi[], boolean allow_hex)
55
	public static byte[] mi2str(byte mi[])
57 56
	{
58 57
		/* The IMSI byte array by example:
59 58
		 * 08 99 10 07 00 00 10 74 90
......
92 91
		boolean odd_nr_of_digits = ((mi_type & 0x08) != 0);
93 92
		byte start_nibble = 2 + 1; // 2 to skip the bytelen, 1 to skip the mi_type
94 93
		byte end_nibble = (byte)(2 + bytelen * 2 - (odd_nr_of_digits ? 0 : 1));
95
		bcd2str(dst, dst_ofs, dst_len, mi, start_nibble, end_nibble, allow_hex);
96
		return (byte)(end_nibble - start_nibble);
94
		byte str[] = new byte[end_nibble - start_nibble];
95
		bcd2str(str, (byte)0, (byte)str.length, mi, start_nibble, end_nibble, true);
96
		return str;
97 97
	}
98 98

  
99 99
	public static byte char2bcd(byte c)
sim-applet/src/org/osmocom/IMSIPseudo/Test.java
28 28
		return new String(hexdump(data));
29 29
	}
30 30

  
31
	private static final String[] imsis = {
32
		"123456",
33
		"1234567",
34
		"12345678",
35
		"123456789",
36
		"1234567890",
37
		"12345678901",
38
		"123456789012",
39
		"1234567890123",
40
		"12345678901234",
41
		"123456789012345",
42
		"1234567890123456",
43
	};
44

  
45
	private static void test_str2mi2str()
46
	{
47
		for (int i = 0; i < imsis.length; i++) {
48
			byte str[] = imsis[i].getBytes();
49
			byte mi[] = MobileIdentity.str2mi(str, MobileIdentity.MI_IMSI, (byte)9);
50
			byte str_from_mi[] = MobileIdentity.mi2str(mi);
51
			System.out.print("IMSI " + new String(str) + " --> MI " + hexdumpStr(mi) + " --> IMSI "
52
					   + new String(str_from_mi));
53
			if (Bytes.equals(str, str_from_mi))
54
				System.out.println(" (ok)");
55
			else
56
				System.out.println(" ERROR!");
57
		}
58
	}
59

  
31 60
	public static void main(String args[]){
32
		System.out.println(hexdumpStr(MobileIdentity.str2mi("123456".getBytes(), (byte)1, (byte)9)));
33
		System.out.println(hexdumpStr(MobileIdentity.str2mi("1234567".getBytes(), (byte)1, (byte)9)));
34
		System.out.println(hexdumpStr(MobileIdentity.str2mi("12345678".getBytes(), (byte)1, (byte)9)));
35
		System.out.println(hexdumpStr(MobileIdentity.str2mi("123456789".getBytes(), (byte)1, (byte)9)));
36
		System.out.println(hexdumpStr(MobileIdentity.str2mi("1234567890".getBytes(), (byte)1, (byte)9)));
37
		System.out.println(hexdumpStr(MobileIdentity.str2mi("12345678901".getBytes(), (byte)1, (byte)9)));
38
		System.out.println(hexdumpStr(MobileIdentity.str2mi("123456789012".getBytes(), (byte)1, (byte)9)));
39
		System.out.println(hexdumpStr(MobileIdentity.str2mi("1234567890123".getBytes(), (byte)1, (byte)9)));
40
		System.out.println(hexdumpStr(MobileIdentity.str2mi("12345678901234".getBytes(), (byte)1, (byte)9)));
41
		System.out.println(hexdumpStr(MobileIdentity.str2mi("123456789012345".getBytes(), (byte)1, (byte)9)));
42
		System.out.println(hexdumpStr(MobileIdentity.str2mi("1234567890123456".getBytes(), (byte)1, (byte)9)));
61
		test_str2mi2str();
43 62
	}
44 63
}

Also available in: Unified diff

Add picture from clipboard (Maximum size: 48.8 MB)