aboutsummaryrefslogtreecommitdiff
path: root/test/sms-get.py
blob: 981fb5d6ffdef4d8ac52d043435a63e721bdec68 (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
#!/usr/bin/python
#
# 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) 2009 Novell, Inc.
# Copyright (C) 2009 - 2012 Red Hat, Inc.
#

# An example on how to read SMS messages using ModemManager

import sys
import dbus
import os

MM_DBUS_SERVICE='org.freedesktop.ModemManager'
MM_DBUS_PATH='/org/freedesktop/ModemManager'
MM_DBUS_INTERFACE_MODEM='org.freedesktop.ModemManager.Modem'
MM_DBUS_INTERFACE_MODEM_SMS='org.freedesktop.ModemManager.Modem.Gsm.SMS'

arglen = len(sys.argv)
if arglen != 2 and arglen != 3:
    print "Usage: %s <modem path> [message #]" % sys.argv[0]
    sys.exit(1)

msgnum = None
if len(sys.argv) == 3:
    msgnum = int(sys.argv[2])

objpath = sys.argv[1]
if objpath[:1] != '/':
    objpath = "/org/freedesktop/ModemManager/Modems/" + str(objpath)

# Create the modem properties proxy
bus = dbus.SystemBus()
proxy = bus.get_object(MM_DBUS_SERVICE, objpath)
modem = dbus.Interface(proxy, dbus_interface="org.freedesktop.DBus.Properties")

# Make sure the modem is enabled first
if modem.Get(MM_DBUS_INTERFACE_MODEM, "Enabled") == False:
    print "Modem is not enabled"
    sys.exit(1)

# Create the SMS interface proxy
sms = dbus.Interface(proxy, dbus_interface=MM_DBUS_INTERFACE_MODEM_SMS)

msgs = sms.List()
i = 0
for m in msgs:
    print "-------------------------------------------------------------------"
    smsc = ""
    try:
        smsc = m["smsc"]
    except KeyError:
        pass

    print "%d: From: %s  Time: %s  SMSC: %s" % (m["index"], m["number"], m["timestamp"], smsc)
    if len(m["text"]):
        print "   %s\n" % m["text"]
    elif len(m["data"]):
        print "   Coding: %d" % m["data-coding-scheme"]
        z = 1
        s = ""
        for c in m["data"]:
            s += "%02X " % c
            if not z % 16:
                print "   %s" % s
                s = ""
            z += 1
        if len(s):
            print "   %s" % s
    i += 1