File: command.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 (299 lines) | stat: -rw-r--r-- 10,821 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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
# -*- 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.
"""AT Commands related classes and help functions"""

import re

from twisted.internet import defer

from wader.common.aterrors import ERROR_REGEXP

OK_REGEXP = re.compile("\r\n(?P<resp>OK)\r\n")


def build_cmd_dict(extract=OK_REGEXP, end=OK_REGEXP, error=ERROR_REGEXP):
    """Returns a dictionary ready to be used in `CMD_DICT`"""
    for regexp in [extract, end, error]:
        if isinstance(regexp, basestring):
            regexp = re.compile(regexp)
        if hasattr(regexp, 'search'):
            pass
        else:
            raise ValueError("Don't know what to do with %r" % regexp)

    return dict(extract=extract, end=end, error=error)


def get_cmd_dict_copy():
    """
    Returns a copy of the `CMD_DICT` dictionary

    Use this instead of importing it directly as you may forget to copy() it
    """
    return CMD_DICT.copy()


CMD_DICT = {

    'add_contact': build_cmd_dict(),

    'cancel_ussd': build_cmd_dict(),

    'change_pin': build_cmd_dict(),

    'check_pin': build_cmd_dict(re.compile(r"""
                                         \r\n
                                         \+CPIN:\s
                                         (?P<resp>
                                            READY      |
                                            SIM\sPIN2? |
                                            SIM\sPUK2?
                                         )
                                         \r\n""", re.X)),

    'delete_contact': build_cmd_dict(),

    'delete_sms': build_cmd_dict(),

    'disable_echo': build_cmd_dict(),

    'enable_echo': build_cmd_dict(),

    'enable_radio': build_cmd_dict(),

    'enable_pin': build_cmd_dict(),

    'find_contacts': build_cmd_dict(re.compile(r"""
                            \r\n
                            \+CPBF:\s
                            (?P<id>\d+),
                            "(?P<number>[+0-9a-fA-F]+)",
                            (?P<category>\d+),
                            \"(?P<name>.*)\"
                            """, re.X)),

    'get_apns': build_cmd_dict(re.compile(r"""
                            \r\n
                            \+CGDCONT:\s
                            (?P<index>\d+),
                            "[A-Za-z0-9]*",
                            "(?P<apn>.*)",
                            "(?P<ip>.*)",
                            \d,\d""", re.X)),

    'get_apn_range': build_cmd_dict(re.compile(r"""
                            \r\n
                            \+CGDCONT:\s*\((?P<lo4>\d+)-(?P<hi4>\d+)\),
                            (?P<ip4>(?:"IP")|(?:"00490050"))(?P<ignored>.*)
                            """, re.X)),

    'get_charsets': build_cmd_dict(re.compile('"(?P<lang>.*?)",?')),

    'get_contact': build_cmd_dict(re.compile(r"""
                            \r\n
                            \+CPBR:\s(?P<id>\d+),
                            "(?P<number>[+0-9a-fA-F]+)",
                            (?P<cat>\d+),
                            "(?P<name>.*)"
                            (?P<ignored>,.*)?
                            \r\n""", re.X)),

    'list_contacts': build_cmd_dict(
                        end=re.compile('(\r\n)?\r\n(OK)\r\n'),
                        extract=re.compile(r"""
                            \r\n
                            \+CPBR:\s(?P<id>\d+),
                            "(?P<number>[+0-9a-fA-F]+)",
                            (?P<cat>\d+),
                            "(?P<name>.*)"
                            """, re.X)),

    'get_card_version': build_cmd_dict(re.compile(
                              '\r\n(\+C?GMR:)?(?P<version>.*)\r\n\r\nOK\r\n')),

    'get_card_model': build_cmd_dict(re.compile(
                              '\r\n(?P<model>.*)\r\n\r\nOK\r\n')),

    'get_charset': build_cmd_dict(re.compile(
                              '\r\n\+CSCS:\s"(?P<lang>.*)"\r\n')),

    'get_esn': build_cmd_dict(re.compile('\r\n\+ESN:\s"(?P<esn>.*)"\r\n')),

    'get_manufacturer_name': build_cmd_dict(re.compile(
                              '\r\n(?P<name>.*)\r\n\r\nOK\r\n')),

    'get_imei': build_cmd_dict(re.compile("\r\n(?P<imei>\d+)\r\n")),

    'get_imsi': build_cmd_dict(re.compile('\r\n(?P<imsi>\d+)\r\n')),

    'get_netreg_status': build_cmd_dict(re.compile(r"""
                              \r\n
                              \+CREG:\s
                              (?P<mode>\d),(?P<status>\d+)
                              (,[0-9a-fA-F]*,[0-9a-fA-F]*)?
                              \r\n
                              """, re.X)),

    'get_network_info': build_cmd_dict(re.compile(r"""
                              \r\n
                              \+COPS:\s+
                              (\d,\d,     # or followed by num,num,str,num
                              "(?P<netname>[^"]*)",
                              (?P<status>\d)
                              |(?P<error>\d)
                              )           # end of group
                              \r\n""", re.X)),

    'get_network_names': build_cmd_dict(re.compile(r"""
                              \(
                              (?P<id>\d+),
                              "(?P<lname>[^"]*)",
                              "(?P<sname>[^"]*)",
                              "(?P<netid>\d+)",
                              (?P<type>\d)
                              \),?""", re.X),
                              end=re.compile('\r\n\r\nOK\r\n')),

    'get_signal_quality': build_cmd_dict(re.compile(r"""
                              \r\n
                              \+CSQ:\s(?P<rssi>\d+),(?P<ber>\d+)
                              \r\n""", re.X)),

    'get_sms_format': build_cmd_dict(
                              re.compile('\r\n\+CMGF:\s(?P<format>\d)\r\n')),

    'get_phonebook_size': build_cmd_dict(re.compile(r"""
                              \r\n
                              \+CPBR:\s
                              \(\d\-(?P<size>\d+)\)(?:,\d+)*
                              \r\n""", re.X)),

    'get_pin_status': build_cmd_dict(
                              re.compile('\r\n\+CLCK:\s(?P<status>\d)\r\n')),

    'get_radio_status': build_cmd_dict(
                              re.compile("\r\n\+CFUN:\s?(?P<status>\d)\r\n")),

    'get_roaming_ids': build_cmd_dict(re.compile(r"""
                              \r\n
                              \+CPOL:\s(?P<index>\d+),
                              (?P<type>\d),
                              "(?P<netid>\d+)"
                              (?P<ignored>,.*)?
                              """, re.X)),

    'list_sms': build_cmd_dict(re.compile(r"""
                              \r\n
                              \+CMGL:\s
                              (?P<id>\d+),
                              (?P<where>\d),,\d+
                              \r\n(?P<pdu>\w+)""", re.X)),

    'get_sms': build_cmd_dict(re.compile(r"""
                              \r\n
                              \+CMGR:\s
                              (?P<where>\d),,
                              \d+\r\n
                              (?P<pdu>\w+)
                              \r\n""", re.X)),

    'get_smsc': build_cmd_dict(re.compile(
                              '\r\n\+CSCA:\s"(?P<smsc>.*)",\d+\r\n')),

    'register_with_netid': build_cmd_dict(),

    'reset_settings': build_cmd_dict(),

    'save_sms': build_cmd_dict(re.compile('\r\n\+CMGW:\s(?P<index>\d+)\r\n')),

    'send_at': build_cmd_dict(),

    'send_sms': build_cmd_dict(re.compile(
                              '\r\n\+CMGS:\s(?P<index>\d+)\r\n')),

    'send_sms_from_storage': build_cmd_dict(re.compile(
                              '\r\n\+CMSS:\s(?P<index>\d+)\r\n')),

    'send_pin': build_cmd_dict(),

    'send_puk': build_cmd_dict(),

    'send_ussd': build_cmd_dict(re.compile("""
                              \r\n\+CUSD:\s
                              (?P<index>\d)
                              (?:,"(?P<resp>.*)")?
                              (?:,(?P<code>\d+))?
                              \r\n""", re.X),
                              re.compile('\r\n\+CUSD:\s\d(?:,".*")?(?:,\d+)?\r\n')),

    'set_apn': build_cmd_dict(),

    'set_charset': build_cmd_dict(),

    'set_error_level': build_cmd_dict(),

    'set_netreg_notification': build_cmd_dict(),

    'set_network_info_format': build_cmd_dict(),

    'set_sms_indication': build_cmd_dict(),

    'set_sms_format': build_cmd_dict(),

    'set_smsc': build_cmd_dict(),

    # r'CRSM:\s(?P<sw1>\d+),(?P<sw2>\d+)(?:,"?(?P<response>[0-9A-Fa-f]*)"?)?'
    'sim_access_restricted': build_cmd_dict(re.compile(r"""
                                          \r\n
                                          \+CRSM:\s
                                          (?P<sw1>\d+),
                                          (?P<sw2>\d+)
                                          (?:,"?
                                          (?P<response>[0-9A-Fa-f]*)
                                          "?)?
                                          \r\n""", re.X)),

}


class ATCmd(object):
    """I encapsulate all the data related to an AT command"""

    def __init__(self, cmd, name=None, eol='\r\n', nolog=tuple()):
        self.cmd = cmd
        self.name = name
        self.eol = eol
        self.nolog = nolog
        # Some commands like sending a sms require an special handling this
        # is because we have to wait till we receive a prompt like '\r\n> '
        # if splitcmd is set, the second part will be send 0.1 seconds later
        self.splitcmd = None
        # command's deferred
        self.deferred = defer.Deferred()
        self.timeout = 15    # default timeout
        self.call_id = None  # DelayedCall reference

    def __repr__(self):
        args = (self.name, self.get_cmd(), self.timeout)
        return "<ATCmd name: %s raw: %r timeout: %d>" % args

    def get_cmd(self):
        """Returns the raw AT command plus EOL"""
        cmd = self.cmd + self.eol
        return str(cmd)