]> git.neil.brown.name Git - plato.git/commitdiff
gsmd: log data usage
authorNeilBrown <neilb@suse.de>
Mon, 31 Dec 2012 20:53:26 +0000 (07:53 +1100)
committerNeilBrown <neilb@suse.de>
Mon, 31 Dec 2012 20:58:37 +0000 (07:58 +1100)
gsm/gsmd.py

index b55b9f71a15cdf395ece1561614a8dade1df895a..2af2c946053c0b424bd48b181b59aed8b16cd87e 100644 (file)
@@ -444,6 +444,7 @@ def data_handle(channel, line, m):
         channel.data_IP = ip
         os.system('/sbin/ifconfig hso0 up %s' % ip)
         record('data', ip)
+    data_log_update()
 
 def data_call(channel, line, m):
     # delayed reponse to _OWANCALL.  Maybe be async, may be
@@ -481,11 +482,57 @@ def data_hungup(channel, failed):
             channel.next_data_call = (time.time() +
                                       time.time() - channel.last_data_call);
             channel.last_data_call = time.time()
+            data_log_reset()
             channel.set_state('data-call')
             return
     if channel.gstate == 'data-call':
         channel.set_state('idle')
 
+# DATA traffic logging - goes to /var/log/gsm-data
+def read_bytes(dev = 'hso0'):
+    f = file('/proc/net/dev')
+    rv = None
+    for l in f:
+        w = l.strip().split()
+        if w[0] == dev + ':':
+            rv = ( int(w[1]), int(w[9]) )
+            break
+    f.close()
+    return rv
+
+last_data_usage = None
+last_data_time = 0
+SIM = None
+def data_log_reset():
+    global last_data_usage, last_data_time
+    last_data_usage = read_bytes()
+    last_data_time = time.time()
+    record('data-last-usage', '%s %s' % last_data_usage)
+
+def data_log_update(force = False):
+    global last_data_usage, last_data_time, SIM
+
+    if not SIM:
+        SIM = recall('sim')
+    if not SIM:
+        return
+    if not last_data_usage:
+        data_log_reset()
+
+    if not force and time.time() - last_data_time < 10*60:
+        return
+
+    data_usage = read_bytes()
+
+    calllog('gsm-data', '%s %d %d' %
+            (SIM,
+             data_usage[0] - last_data_usage[0],
+             data_usage[1] - last_data_usage[1]))
+
+    last_data_usage = data_usage
+    last_data_time = time.time()
+    record('data-last-usage', '%s %s' % last_data_usage)
+
 control = {}
 
 # For flight mode, we turn the power off.
@@ -820,9 +867,11 @@ class GsmD(AtChannel):
             self.args['APN'] = self.data_APN
             if self.data_IP:
                 self.set_state('data-hangup')
+                data_log_update(True)
             elif self.gstate == 'idle' and self.data_APN:
                 self.last_data_call = time.time()
                 self.next_data_call = time.time()
+                data_log_reset()
                 self.set_state('data-call')
 
     def check_flightmode(self, f = None):