File: ogsmd-log-data.py

package info (click to toggle)
fso-frameworkd 0.9.5.9%2Bgit20110512-5
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 4,988 kB
  • ctags: 5,213
  • sloc: python: 23,770; sh: 160; sql: 4; makefile: 2
file content (137 lines) | stat: -rwxr-xr-x 4,596 bytes parent folder | download | duplicates (4)
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
#!/usr/bin/env python
# -*- coding: UTF-8 -*-
"""
Log GSM Data from ogsmd

(C) 2008 Michael 'Mickey' Lauer <mlauer@vanille-media.de>
(C) 2008 Openmoko, Inc.

GPLv2 or later
"""

# constants you may want to change

PIN = "9797"
LOG = "/tmp/gsm-data.log"
FREQUENCE = 2000 # ms
EM_COMMAND = "%EM=2,1"
AUTOANSWER = True

# you may not want to change a lot below here

import sys
import gobject
import dbus, dbus.mainloop.glib

logfile = file( LOG, "w" )
gsm = None

#----------------------------------------------------------------------------#
def error( message ):
#----------------------------------------------------------------------------#
    sys.stderr.write( "%s\n" % message )
    sys.exit( -1 )

#----------------------------------------------------------------------------#
def log( message ):
#----------------------------------------------------------------------------#
    logfile.write( "%s\n" % message )
    logfile.flush()
    print message

#----------------------------------------------------------------------------#
def timeout_handler():
#----------------------------------------------------------------------------#
    if gsm is not None:
        strength = gsm.GetSignalStrength()
        log( "SIGNAL NOW %d" % strength )

        # here you can add more of your special AT commands
        result = gsm.DebugCommand( "AT%s\r\n" % EM_COMMAND )
        log( "EM RESULT %s" % result[0] )

        return True # call me again

#----------------------------------------------------------------------------#
def dbus_signal_handler( data, *args, **kwargs ):
#----------------------------------------------------------------------------#
    signal = "%s.%s" % ( kwargs["interface"], kwargs["member"] )

    if signal == "org.freesmartphone.GSM.Network.Status":
        if "lac" and "cid" in data:
            log( "LAC/CID NOW %s, %s" % ( data["lac"], data["cid"] ) )
    elif signal == "org.freesmartphone.GSM.Network.SignalStrength":
        log( "SIGNAL NOW %d" % data )

    elif signal == "org.freesmartphone.GSM.Call.CallStatus":
        status, properties = args
        if "peer" in properties:
            log( "CALL %s [%s]" % ( status, properties["peer"] ) )
        else:
            log( "CALL %s [unknown]" % status )
        if status == "incoming" and AUTOANSWER:
            log( "AUTOANSWERING CALL" )
            gsm.Activate( data )

#----------------------------------------------------------------------------#
def init_dbus():
#----------------------------------------------------------------------------#
    """initialize dbus"""
    print "trying to get bus...",
    try:
        bus = dbus.SystemBus()
    except Exception, e:
        error( "Can't connect to dbus: %s" % e )
    print "ok"
    return bus

#----------------------------------------------------------------------------#
def init_ogsmd( bus ):
#----------------------------------------------------------------------------#
    """initialize ogsmd"""
    print "trying to get object...",
    try:
        global gsm
        gsm = bus.get_object( "org.freesmartphone.ogsmd", "/org/freesmartphone/GSM/Device" )
    except Exception, e:
        error( "can't connect to org.freesmartphone.ogsmd: %s" % e )

    bus.add_signal_receiver( dbus_signal_handler, None, None, "org.freesmartphone.ogsmd", None,
        sender_keyword = "sender", destination_keyword = "destination",
        interface_keyword = "interface", member_keyword = "member", path_keyword = "path" )
    print "ok"
    print "initializing gsm..."

    # init sequence
    try:
        print "-> setting antenna power..."
        gsm.SetAntennaPower( True ) # this will fail, if your SIM is PIN-protected
    except dbus.DBusException, m:
        authstatus = gsm.GetAuthStatus()
        if authstatus != "READY":
            print "-> card PIN protected, sending PIN..."
            gsm.SendAuthCode( PIN ) # send PIN

    gsm.SetAntennaPower( True ) # this should work now
    print "-> registering to network"
    gsm.Register() # autoregister
    print "gsm init ok, entering mainloop"
    return False # don't call me again

#----------------------------------------------------------------------------#
# program starts here
#----------------------------------------------------------------------------#

dbus.mainloop.glib.DBusGMainLoop( set_as_default=True )
mainloop = gobject.MainLoop()
bus = init_dbus()
gobject.idle_add( init_ogsmd, bus )
gobject.timeout_add( FREQUENCE, timeout_handler )

try:
    mainloop.run()
except KeyboardInterrupt:
    mainloop.quit()
else:
    print "normal exit."
    sys.exit( 0 )