aboutsummaryrefslogtreecommitdiff
path: root/decode/wmc.py
blob: 4d185b8c4b27944fd9ee940eabee840fed0fcf53 (plain)
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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
#!/usr/bin/python
# -*- Mode: python; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
#
# 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:
#
# Copyright (C) 2011 Red Hat, Inc.
#

import binascii
import struct
import defs

def complete(data, direction):
    if direction == defs.TO_MODEM:
        if data[len(data) - 2:] == "0d":
            return True
    elif direction == defs.TO_HOST:
        if data[len(data) - 6:] == "30307e":
            return True
    else:
        raise ValueError("No data direction")
    return False


def unpack(data, direction):
    # unpack the data
    if direction == defs.TO_MODEM:
        if data[:14] == "41542a574d433d":
            # remove the AT*WMC= bits, and the newline and CRC at the end
            data = data[14:]
            if data[len(data) - 2:] == "0d":
                data = data[:len(data) - 6]
    elif direction == defs.TO_HOST:
        if data[len(data) - 2:] == "7e":
            # remove HDLC terminator and CRC
            data = data[:len(data) - 6]
    else:
        raise ValueError("No data direction")

    data = binascii.unhexlify(data)

    # PPP-unescape it
    escape = False
    new_data = ""
    for i in data:
        if ord(i) == 0x7D:
            escape = True
        elif escape == True:
            new_data += chr(ord(i) ^ 0x20)
            escape = False
        else:
            new_data += i

    return new_data

def show_data(data, prefix):
    line = ""
    for i in data:
        line += " %02x" % ord(i)
    print prefix + "  Data:  %s" % line

def show_device_info(data, prefix, direction):
    if direction != defs.TO_HOST:
        return

    fmt = "<"
    fmt += "27s"  # unknown1
    fmt += "64s"  # manf
    fmt += "64s"  # model
    fmt += "64s"  # fwrev
    fmt += "64s"  # hwrev
    fmt += "64s"  # unknown2
    fmt += "64s"  # unknown3
    fmt += "10s"  # min
    fmt += "12s"  # unknown4
    fmt += "H"    # home_sid
    fmt += "6s"   # unknown5
    fmt += "H"    # eri_ver?
    fmt += "3s"   # unknown6
    fmt += "64s"  # unknown7
    fmt += "s"    # unknown8
    fmt += "14s"  # meid
    fmt += "6s"   # unknown9
    fmt += "16s"  # imei
    fmt += "6s"   # unknown10
    fmt += "16s"  # unknown11
    fmt += "20s"  # iccid
    fmt += "6s"   # unknown12

    expected = struct.calcsize(fmt)
    if len(data) >= expected:
        (u1, manf, model, fwrev, hwrev, u2, u3, cdmamin, u4, homesid, u5, eriver, \
            u6, u7, u8, meid, u9, imei, u10, u11, iccid, u12) = struct.unpack(fmt, data[:expected])
        print prefix + "  Manf:     %s" % manf
        print prefix + "  Model:    %s" % model
        print prefix + "  FW Rev:   %s" % fwrev
        print prefix + "  HW Rev:   %s" % hwrev
        print prefix + "  MIN:      %s" % cdmamin
        print prefix + "  Home SID: %d" % homesid
        print prefix + "  ERI Ver:  %d" % eriver
        print prefix + "  MEID:     %s" % meid
        print prefix + "  IMEI:     %s" % imei
        print prefix + "  U11 :     %s" % u11
        print prefix + "  ICCID:    %s" % iccid
    else:
        raise ValueError("Unexpected Info command response len (got %d expected %d)" % (len(data), expected))

    fmt3 = "<"
    fmt3 += "16s"  # MCC
    fmt3 += "16s"  # MNC
    fmt3 += "4s"   # unknown11
    fmt3 += "4s"   # unknown12
    fmt3 += "4s"   # unknown13
    expected3 = struct.calcsize(fmt3)
    if len(data) >= expected + expected3:
        (mcc, mnc, u11, u12, u13) = struct.unpack(fmt3, data[expected:])
        print prefix + "  MCC:      %s" % mcc
        print prefix + "  MNC:      %s" % mnc


def state_to_string(state):
    states = { 0: "unknown",
               1: "idle",
               2: "connecting",
               3: "authenticating",
               4: "connected",
               5: "dormant",
               6: "updating NAM",
               7: "updating PRL",
               8: "disconnecting",
               9: "error",
              10: "updating UICC",
              11: "updating PLMN" }
    try:
        return states[state]
    except KeyError:
        return "unknown"

def show_connection_info(data, prefix, direction):
    if direction != defs.TO_HOST:
        return

    fmt = "<"
    fmt += "I"   # rx_bytes
    fmt += "I"   # tx_bytes
    fmt += "8s"  # unknown1
    fmt += "B"   # state
    fmt += "3s"  # unknown2

    expected = struct.calcsize(fmt)
    if len(data) >= expected:
        (rxb, txb, u1, state, u2) = struct.unpack(fmt, data[:expected])
        print prefix + "  RX Bytes: %d" % rxb
        print prefix + "  TX Bytes: %d" % txb
        print prefix + "  State:    %d (%s)" % (state, state_to_string (state))
    else:
        raise ValueError("Unexpected Connection Info command response len (got %d expected %d)" % (len(data), expected))

    fmt3 = "<"
    fmt3 += "4s"  # unknown3
    fmt3 += "16s" # ip4_address
    fmt3 += "8s"  # netmask?
    fmt3 += "40s" # ip6_address
    expected3 = struct.calcsize(fmt3)
    if len(data) >= expected + expected3:
        (u3, ip4addr, netmask, ip6addr) = struct.unpack(fmt3, data[expected:])
        print prefix + "  IP4 Addr: %s" % ip4addr
        print prefix + "  IP6 Addr: %s" % ip6addr

def get_signal(item):
    if item == 0x7D:
        return (item * -1, "(NO SIGNAL)")
    else:
        return (item * -1, "")

def service_to_string(service):
    services = { 0: "none",
                 1: "AMPS",
                 2: "IS95-A",
                 3: "IS95-B",
                 4: "GSM",
                 5: "GPRS",
                 6: "1xRTT",
                 7: "EVDO r0",
                 8: "UMTS",
                 9: "EVDO rA",
                10: "EDGE",
                11: "HSDPA",
                12: "HSUPA",
                13: "HSPA",
                14: "LTE",
                15: "EVDO rA eHRPD" }
    try:
        return services[service]
    except KeyError:
        return "unknown"

def show_network_info(data, prefix, direction):
    if direction != defs.TO_HOST:
        return

    fmt = "<"
    fmt += "B"   # unknown1
    fmt += "3s"  # unknown2
    fmt += "B"   # service
    fmt += "B"   # unknown3
    fmt += "H"   # year
    fmt += "B"   # month
    fmt += "B"   # zero
    fmt += "B"   # day
    fmt += "B"   # zero
    fmt += "B"   # hours
    fmt += "B"   # zero
    fmt += "B"   # minutes
    fmt += "B"   # zero
    fmt += "B"   # seconds
    fmt += "H"   # counter1
    fmt += "H"   # counter2
    fmt += "3s"  # unknown4
    fmt += "B"   # 2g_dbm
    fmt += "3s"  # unknown5
    fmt += "16s" # cdma_opname
    fmt += "18s" # unknown6
    fmt += "B"   # 3g_dbm
    fmt += "3s"  # unknown7
    fmt += "B"   # unknown8
    fmt += "3s"  # unknown9
    fmt += "B"   # unknown10
    fmt += "8s"  # 3gpp_opname
    fmt += "4s"  # unknown11
    fmt += "I"   # unknown12
    fmt += "I"   # unknown13
    fmt += "44s" # unknown14
    fmt += "I"   # mcc/mnc

    expected = struct.calcsize(fmt)
    if len(data) >= expected:
        (u1, u2, service, u3, year, month, z1, day, z2, hours, z3, minutes, z4, \
         seconds, counter1, counter2, u4, two_g_dbm, u5, \
         cdma_opname, u6, three_g_dbm, u7, u8, u9, u10, tgpp_opname, u11, u12, \
         u13, u14, mccmnc) = struct.unpack(fmt, data[:expected])
        print prefix + "  Time:     %04d/%02d/%02d %02d:%02d:%02d" % (year, month, day, hours, minutes, seconds)
        print prefix + "  Counter1: %s" % counter1
        print prefix + "  Counter2: %s" % counter2
        print prefix + "  Service:  %d (%s)" % (service, service_to_string (service))
        print prefix + "  2G dBm:   %d dBm %s" % get_signal(two_g_dbm)
        print prefix + "  3G dBm:   %d dBm %s" % get_signal(three_g_dbm)
        print prefix + "  CDMA Op:  %s" % cdma_opname
        print prefix + "  3GPP Op:  %s" % tgpp_opname

        # handle 2-digit MNC
        if mccmnc < 100000:
           mccmnc *= 10;

        mcc = mccmnc / 1000
        mnc = mccmnc - (mcc * 1000)
        if mcc > 100:
            print prefix + "  MCC/MNC:  %u-%u" % (mcc, mnc)

    else:
        raise ValueError("Unexpected Network Info command response len (got %d expected %d)" % (len(data), expected))

    fmt3 = "<"
    fmt3 += "B"   # lte_dbm
    fmt3 += "3s"  # unknown15
    fmt3 += "4s"  # unknown16
    expected3 = struct.calcsize(fmt3)
    if len(data) >= expected + expected3:
        (lte_dbm, u17, u18) = struct.unpack(fmt3, data[expected:])
        print prefix + "  LTE dBm:  %d dBm %s" % get_signal(lte_dbm)


def show_init(data, prefix, direction):
    if len(data) == 0:
        # PC5740/old format
        return

    if direction == defs.TO_HOST:
        show_data(data, prefix)
        return

    fmt = "<"
    fmt += "H"  # year
    fmt += "B"  # month
    fmt += "B"  # zero
    fmt += "B"  # day
    fmt += "B"  # zero
    fmt += "B"  # hours
    fmt += "B"  # zero
    fmt += "B"  # minutes
    fmt += "B"  # zero
    fmt += "B"  # seconds
    expected = struct.calcsize(fmt)
    if len(data) >= expected:
        (year, month, z1, day, z2, hours, z3, minutes, z4, seconds) = struct.unpack(fmt, data[:expected])
        print prefix + "  Time:  %04d/%02d/%02d %02d:%02d:%02d" % (year, month, day, hours, minutes, seconds)
    else:
        raise ValueError ("Unexpected Init command length (got %d expected %d)" % (len(data), expected))

def show_bearer_info(data, prefix, direction):
    pass

def mode_to_string(mode):
    if mode == 0x00:
        return "CDMA/EVDO"
    elif mode == 0x01:
        return "CDMA only"
    elif mode == 0x02:
        return "EVDO only"
    elif mode == 0x0A:
        return "GSM/UMTS"
    elif mode == 0x0B:
        return "GSM/GPRS/EDGE only"
    elif mode == 0x0C:
        return "UMTS/HSPA only"
    elif mode == 0x14:
        return "Auto"
    return "unknown"

def show_get_global_mode(data, prefix, direction):
    if direction != defs.TO_HOST:
        return

    fmt = "<"
    fmt += "B"   # unknown1
    fmt += "B"   # mode
    fmt += "B"   # unknown2
    fmt += "B"   # unknown3

    expected = struct.calcsize(fmt)
    if len(data) != expected:
        raise ValueError("Unexpected GET_GLOBAL_MODE command response len (got %d expected %d)" % (len(data), expected))
    (u1, mode, u2, u3) = struct.unpack(fmt, data)

    print prefix + "  Mode:   0x%X (%s)" % (mode, mode_to_string(mode))

def show_set_global_mode(data, prefix, direction):
    if direction != defs.TO_MODEM:
        return;

    fmt = "<"
    fmt += "B"   # unknown1
    fmt += "B"   # mode
    fmt += "B"   # unknown2
    fmt += "B"   # unknown3

    expected = struct.calcsize(fmt)
    if len(data) != expected:
        raise ValueError("Unexpected SET_GLOBAL_MODE command response len (got %d expected %d)" % (len(data), expected))
    (u1, mode, u2, u3) = struct.unpack(fmt, data)

    print prefix + "  Mode:   0x%X (%s)" % (mode, mode_to_string(mode))


cmds = { 0x03: ("GET_GLOBAL_MODE", show_get_global_mode),
         0x04: ("SET_GLOBAL_MODE", show_set_global_mode),
         0x06: ("DEVICE_INFO", show_device_info),
         0x0A: ("CONNECTION_INFO", show_connection_info),
         0x0B: ("NETWORK_INFO", show_network_info),
         0x0D: ("INIT", show_init),
         0x4D: ("EPS_BEARER_INFO", show_bearer_info)
       }

def show(data, prefix, direction):
    if ord(data[:1]) != 0xC8:
        return

    data = data[1:]  # skip 0xC8 header
    cmdno = ord(data[:1])
    try:
        cmdinfo = cmds[cmdno]
    except KeyError:
        return
    data = data[1:]  # skip cmdno

    print prefix + "WMC Packet:"
    print prefix + "  Cmd:    0x%02x (%s)" % (cmdno, cmdinfo[0])
    cmdinfo[1](data, prefix, direction)
    print ""

def get_funcs():
    return (complete, unpack, show)