annotate simtool/hlread.c @ 96:a5dfab380a90

fc-simtool: iccid high-level read command implemented
author Mychaela Falconia <falcon@freecalypso.org>
date Sun, 24 Jan 2021 22:11:34 +0000
parents
children 597c4e87a1f4
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
96
a5dfab380a90 fc-simtool: iccid high-level read command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
1 /*
a5dfab380a90 fc-simtool: iccid high-level read command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
2 * This module implements some high-level or user-friendly read commands.
a5dfab380a90 fc-simtool: iccid high-level read command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
3 */
a5dfab380a90 fc-simtool: iccid high-level read command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
4
a5dfab380a90 fc-simtool: iccid high-level read command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
5 #include <sys/types.h>
a5dfab380a90 fc-simtool: iccid high-level read command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
6 #include <stdio.h>
a5dfab380a90 fc-simtool: iccid high-level read command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
7 #include <stdlib.h>
a5dfab380a90 fc-simtool: iccid high-level read command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
8 #include <pcsclite.h>
a5dfab380a90 fc-simtool: iccid high-level read command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
9 #include <winscard.h>
a5dfab380a90 fc-simtool: iccid high-level read command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
10 #include "globals.h"
a5dfab380a90 fc-simtool: iccid high-level read command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
11 #include "file_id.h"
a5dfab380a90 fc-simtool: iccid high-level read command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
12
a5dfab380a90 fc-simtool: iccid high-level read command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
13 encode_hex_digit(d)
a5dfab380a90 fc-simtool: iccid high-level read command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
14 unsigned d;
a5dfab380a90 fc-simtool: iccid high-level read command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
15 {
a5dfab380a90 fc-simtool: iccid high-level read command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
16 if (d <= 9)
a5dfab380a90 fc-simtool: iccid high-level read command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
17 return(d + '0');
a5dfab380a90 fc-simtool: iccid high-level read command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
18 else
a5dfab380a90 fc-simtool: iccid high-level read command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
19 return(d - 10 + 'A');
a5dfab380a90 fc-simtool: iccid high-level read command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
20 }
a5dfab380a90 fc-simtool: iccid high-level read command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
21
a5dfab380a90 fc-simtool: iccid high-level read command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
22 decode_reversed_nibbles(bytes, nbytes, dest)
a5dfab380a90 fc-simtool: iccid high-level read command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
23 u_char *bytes;
a5dfab380a90 fc-simtool: iccid high-level read command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
24 unsigned nbytes;
a5dfab380a90 fc-simtool: iccid high-level read command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
25 char *dest;
a5dfab380a90 fc-simtool: iccid high-level read command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
26 {
a5dfab380a90 fc-simtool: iccid high-level read command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
27 u_char *sp;
a5dfab380a90 fc-simtool: iccid high-level read command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
28 char *dp;
a5dfab380a90 fc-simtool: iccid high-level read command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
29 unsigned n, c;
a5dfab380a90 fc-simtool: iccid high-level read command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
30
a5dfab380a90 fc-simtool: iccid high-level read command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
31 sp = bytes;
a5dfab380a90 fc-simtool: iccid high-level read command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
32 dp = dest;
a5dfab380a90 fc-simtool: iccid high-level read command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
33 for (n = 0; n < nbytes; n++) {
a5dfab380a90 fc-simtool: iccid high-level read command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
34 c = *sp & 0xF;
a5dfab380a90 fc-simtool: iccid high-level read command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
35 *dp++ = encode_hex_digit(c);
a5dfab380a90 fc-simtool: iccid high-level read command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
36 c = *sp >> 4;
a5dfab380a90 fc-simtool: iccid high-level read command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
37 *dp++ = encode_hex_digit(c);
a5dfab380a90 fc-simtool: iccid high-level read command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
38 sp++;
a5dfab380a90 fc-simtool: iccid high-level read command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
39 }
a5dfab380a90 fc-simtool: iccid high-level read command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
40 }
a5dfab380a90 fc-simtool: iccid high-level read command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
41
a5dfab380a90 fc-simtool: iccid high-level read command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
42 cmd_iccid()
a5dfab380a90 fc-simtool: iccid high-level read command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
43 {
a5dfab380a90 fc-simtool: iccid high-level read command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
44 int rc;
a5dfab380a90 fc-simtool: iccid high-level read command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
45 char buf[21];
a5dfab380a90 fc-simtool: iccid high-level read command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
46
a5dfab380a90 fc-simtool: iccid high-level read command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
47 rc = select_op(FILEID_MF);
a5dfab380a90 fc-simtool: iccid high-level read command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
48 if (rc < 0)
a5dfab380a90 fc-simtool: iccid high-level read command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
49 return(rc);
a5dfab380a90 fc-simtool: iccid high-level read command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
50 rc = select_op(EF_ICCID);
a5dfab380a90 fc-simtool: iccid high-level read command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
51 if (rc < 0)
a5dfab380a90 fc-simtool: iccid high-level read command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
52 return(rc);
a5dfab380a90 fc-simtool: iccid high-level read command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
53 rc = parse_ef_select_response();
a5dfab380a90 fc-simtool: iccid high-level read command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
54 if (rc < 0)
a5dfab380a90 fc-simtool: iccid high-level read command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
55 return(rc);
a5dfab380a90 fc-simtool: iccid high-level read command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
56 if (curfile_structure != 0x00 || curfile_total_size != 10) {
a5dfab380a90 fc-simtool: iccid high-level read command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
57 fprintf(stderr, "error: expected transparent EF of 10 bytes\n");
a5dfab380a90 fc-simtool: iccid high-level read command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
58 return(-1);
a5dfab380a90 fc-simtool: iccid high-level read command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
59 }
a5dfab380a90 fc-simtool: iccid high-level read command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
60 rc = readbin_op(0, 10);
a5dfab380a90 fc-simtool: iccid high-level read command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
61 if (rc < 0)
a5dfab380a90 fc-simtool: iccid high-level read command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
62 return(rc);
a5dfab380a90 fc-simtool: iccid high-level read command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
63 decode_reversed_nibbles(sim_resp_data, 10, buf);
a5dfab380a90 fc-simtool: iccid high-level read command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
64 buf[20] = '\0';
a5dfab380a90 fc-simtool: iccid high-level read command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
65 printf("%s\n", buf);
a5dfab380a90 fc-simtool: iccid high-level read command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
66 return(0);
a5dfab380a90 fc-simtool: iccid high-level read command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
67 }