From cc5cf6cc0d288a9493e8832dfbfc65b663073fa8 Mon Sep 17 00:00:00 2001 From: NeilBrown Date: Tue, 1 Jan 2013 07:53:26 +1100 Subject: [PATCH] gsmd: log data usage --- gsm/gsmd.py | 49 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) diff --git a/gsm/gsmd.py b/gsm/gsmd.py index b55b9f7..2af2c94 100644 --- a/gsm/gsmd.py +++ b/gsm/gsmd.py @@ -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): -- 2.39.5