blob: 467d5ee2358f978814cb078a935fbc19db631b8a [file] [log] [blame]
Vadim Yanitskiy1381ff12018-10-27 01:48:15 +07001# -*- coding: utf-8 -*-
2
Vadim Yanitskiy1381ff12018-10-27 01:48:15 +07003# Copyright (C) 2018 Vadim Yanitskiy <axilirator@gmail.com>
4#
5# This program is free software: you can redistribute it and/or modify
6# it under the terms of the GNU General Public License as published by
7# the Free Software Foundation, either version 2 of the License, or
8# (at your option) any later version.
9#
10# This program is distributed in the hope that it will be useful,
11# but WITHOUT ANY WARRANTY; without even the implied warranty of
12# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13# GNU General Public License for more details.
14#
15# You should have received a copy of the GNU General Public License
16# along with this program. If not, see <http://www.gnu.org/licenses/>.
17#
18
Vadim Yanitskiy1381ff12018-10-27 01:48:15 +070019import select
20import struct
21import socket
22import os
23
24from pySim.transport import LinkBase
25from pySim.exceptions import *
26from pySim.utils import h2b, b2h
27
28class L1CTLMessage(object):
29
30 # Every (encoded) L1CTL message has the following structure:
31 # - msg_length (2 bytes, net order)
32 # - l1ctl_hdr (packed structure)
33 # - msg_type
34 # - flags
35 # - padding (2 spare bytes)
36 # - ... payload ...
37
38 def __init__(self, msg_type, flags = 0x00):
39 # Init L1CTL message header
40 self.data = struct.pack("BBxx", msg_type, flags)
41
42 def gen_msg(self):
43 return struct.pack("!H", len(self.data)) + self.data
44
45class L1CTLMessageReset(L1CTLMessage):
46
47 # L1CTL message types
48 L1CTL_RESET_REQ = 0x0d
49 L1CTL_RESET_IND = 0x07
50 L1CTL_RESET_CONF = 0x0e
51
52 # Reset types
53 L1CTL_RES_T_BOOT = 0x00
54 L1CTL_RES_T_FULL = 0x01
55 L1CTL_RES_T_SCHED = 0x02
56
57 def __init__(self, type = L1CTL_RES_T_FULL):
58 super(L1CTLMessageReset, self).__init__(self.L1CTL_RESET_REQ)
59 self.data += struct.pack("Bxxx", type)
60
61class L1CTLMessageSIM(L1CTLMessage):
62
63 # SIM related message types
64 L1CTL_SIM_REQ = 0x16
65 L1CTL_SIM_CONF = 0x17
66
67 def __init__(self, pdu):
68 super(L1CTLMessageSIM, self).__init__(self.L1CTL_SIM_REQ)
69 self.data += pdu
70
71class CalypsoSimLink(LinkBase):
Harald Welteee3501f2021-04-02 13:00:18 +020072 """Transport Link for Calypso based phones."""
Vadim Yanitskiy1381ff12018-10-27 01:48:15 +070073
Harald Welteee3501f2021-04-02 13:00:18 +020074 def __init__(self, sock_path:str = "/tmp/osmocom_l2"):
Vadim Yanitskiy1381ff12018-10-27 01:48:15 +070075 # Make sure that a given socket path exists
76 if not os.path.exists(sock_path):
77 raise ReaderError("There is no such ('%s') UNIX socket" % sock_path)
78
79 print("Connecting to osmocon at '%s'..." % sock_path)
80
81 # Establish a client connection
82 self.sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
83 self.sock.connect(sock_path)
84
85 def __del__(self):
86 self.sock.close()
87
88 def wait_for_rsp(self, exp_len = 128):
89 # Wait for incoming data (timeout is 3 seconds)
90 s, _, _ = select.select([self.sock], [], [], 3.0)
91 if not s:
92 raise ReaderError("Timeout waiting for card response")
93
94 # Receive expected amount of bytes from osmocon
95 rsp = self.sock.recv(exp_len)
96 return rsp
97
98 def reset_card(self):
99 # Request FULL reset
100 req_msg = L1CTLMessageReset()
101 self.sock.send(req_msg.gen_msg())
102
103 # Wait for confirmation
104 rsp = self.wait_for_rsp()
105 rsp_msg = struct.unpack_from("!HB", rsp)
106 if rsp_msg[1] != L1CTLMessageReset.L1CTL_RESET_CONF:
107 raise ReaderError("Failed to reset Calypso PHY")
108
109 def connect(self):
110 self.reset_card()
111
112 def disconnect(self):
113 pass # Nothing to do really ...
114
115 def wait_for_card(self, timeout = None, newcardonly = False):
116 pass # Nothing to do really ...
117
118 def send_apdu_raw(self, pdu):
Vadim Yanitskiy1381ff12018-10-27 01:48:15 +0700119
120 # Request FULL reset
121 req_msg = L1CTLMessageSIM(h2b(pdu))
122 self.sock.send(req_msg.gen_msg())
123
124 # Read message length first
125 rsp = self.wait_for_rsp(struct.calcsize("!H"))
126 msg_len = struct.unpack_from("!H", rsp)[0]
127 if msg_len < struct.calcsize("BBxx"):
128 raise ReaderError("Missing L1CTL header for L1CTL_SIM_CONF")
129
130 # Read the whole message then
131 rsp = self.sock.recv(msg_len)
132
133 # Verify L1CTL header
134 hdr = struct.unpack_from("BBxx", rsp)
135 if hdr[0] != L1CTLMessageSIM.L1CTL_SIM_CONF:
136 raise ReaderError("Unexpected L1CTL message received")
137
138 # Verify the payload length
139 offset = struct.calcsize("BBxx")
140 if len(rsp) <= offset:
141 raise ProtocolError("Empty response from SIM?!?")
142
143 # Omit L1CTL header
144 rsp = rsp[offset:]
145
146 # Unpack data and SW
147 data = rsp[:-2]
148 sw = rsp[-2:]
149
150 return b2h(data), b2h(sw)