File: sim.py

package info (click to toggle)
wader 0.5.12-2
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 2,672 kB
  • ctags: 5,053
  • sloc: python: 17,191; makefile: 142; sh: 131
file content (160 lines) | stat: -rw-r--r-- 4,895 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
# -*- coding: utf-8 -*-
# Copyright (C) 2006-2008  Vodafone España, S.A.
# Copyright (C) 2008-2009  Warp Networks, S.L.
# Author:  Pablo Martí
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
"""SIM startup module"""

from twisted.python import log
from twisted.internet import defer, reactor

import wader.common.aterrors as E
from wader.common.encoding import from_ucs2

RETRY_ATTEMPTS = 3
RETRY_TIMEOUT = 3


# SIM files:
# 3GPP TS 31.102 V10.21.0 (2011-064)
EF_AD = 0x6fad  # 28589
EF_SPN = 0x6f46  # 28486
# ETSI TS 102 221 V8.2.0 (2009-06)
EF_ICCID = 0x2fe2  # 12285

# SIM Commands:
# ETSI TS 102 221 V8.2.0 (2009-06):  Commands
# 3GPP TS 27.007 V10.4.0 (2011-06):  Restricted SIM access +CRSM
COM_READ_BINARY = 0xb0  # 176
COM_READ_RECORD = 0xb2  # 178
COM_GET_RESPONSE = 0xc0  # 192
COM_UPDATE_BINARY = 0xd6  # 214
COM_UPDATE_RECORD = 0xdc  # 220
COM_STATUS = 0xf2  # 242
COM_RETRIEVE_DATA = 0xcb  # 203
COM_SET_DATA = 0xdb  # 219

# Successfull status bytes
# ETSI TS 102 221 V8.2.0 (2009-06)
SW_OK = [0x90, 0x91, 0x92]  # Response APDU structure.


class SIMBaseClass(object):
    """
    I take care of initing the SIM

    The actual details of initing the SIM vary from mobile to datacard, so
    I am the one to subclass in case your device needs a special startup
    """

    def __init__(self, sconn):
        super(SIMBaseClass, self).__init__()
        self.sconn = sconn
        self.size = None
        self.charset = 'IRA'
        self.num_of_failures = 0

    def set_size(self, size):
        log.msg("Setting size to %d" % size)
        self.size = size

    def set_charset(self, charset):
        try:
            self.charset = from_ucs2(charset)
        except (UnicodeDecodeError, TypeError):
            self.charset = charset
        return charset

    def setup_sms(self):
        # Notification when a SMS arrives...
        self.sconn.set_sms_indication(2, 1, 0, 1, 0)
        # set PDU mode
        self.sconn.set_sms_format(0)

    def initialize(self, set_encoding=True):
        """
        Initializes the SIM card

        This method sets up encoding, SMS format and notifications
        in the SIM. It returns a deferred with the SIM size.
        """
        # set up extended error reporting
        self.sconn.set_error_level(1)

        if set_encoding:
            self._setup_encoding()

        self.setup_sms()

        deferred = defer.Deferred()

        def get_size(auxdef):
            d = self.sconn.get_phonebook_size()

            def phonebook_size_cb(resp):
                self.set_size(resp)
                auxdef.callback(self.size)

            def phonebook_size_eb(failure):
                failure.trap(E.General, E.SimBusy, E.SimFailure)
                self.num_of_failures += 1
                if self.num_of_failures > RETRY_ATTEMPTS:
                    # fail gracefully for now, we'll try again
                    # the next time a contact operation is required
                    auxdef.callback(None)
                    return

                reactor.callLater(RETRY_TIMEOUT, get_size, auxdef)

            d.addCallback(phonebook_size_cb)
            d.addErrback(phonebook_size_eb)

            return auxdef

        return get_size(deferred)

    def _set_charset(self, charset):
        """
        Checks whether is necessary the change and memorizes the used charset
        """

        def process_charset(reply):
            """
            Only set the new charset if is different from current encoding
            """
            if reply != charset:
                return self.sconn.set_charset(charset)

            # we already have the wanted charset
            self.charset = reply
            return defer.succeed(True)

        d = self.sconn.get_charset()
        d.addCallback(process_charset)
        return d

    def _process_charsets(self, charsets):
        for charset in ["UCS2", "IRA", "GSM"]:
            if charset in charsets:
                return self._set_charset(charset)

        msg = "Couldn't find an appropriated charset in %s"
        raise E.CharsetError(msg % charsets)

    def _setup_encoding(self):
        d = self.sconn.get_charsets()
        d.addCallback(self._process_charsets)
        return d