From 8ec754ba530c4455089a7c23ad3a5eb326a196a9 Mon Sep 17 00:00:00 2001 From: NeilBrown Date: Fri, 25 Jan 2013 10:39:52 +1100 Subject: [PATCH] gsmd: improve CFUN handling, and split 'init' state. Sometimes it can take a little while for CFUN to be effective. So if it tells us the state is stil '0', we don't want to immediately try the rest of the init process. So split the init state into two - one which sets CFUN, and one which is entered once CFUN says it is OK. Also, if CFUN says it isn't OK in some way, trigger a reset, but not too often. Signed-off-by: NeilBrown --- gsm/gsmd.py | 33 ++++++++++++++++++++++++++++----- gsm/notes | 9 +++++++++ 2 files changed, 37 insertions(+), 5 deletions(-) diff --git a/gsm/gsmd.py b/gsm/gsmd.py index 70c1ab9..e1980d9 100644 --- a/gsm/gsmd.py +++ b/gsm/gsmd.py @@ -431,6 +431,22 @@ def call_status(channel, line, m): # channel.set_state('on-call') pass +def check_cfun(channel, line, m): + # response to +CFUN? + # If '1', then advance from init1 to init2 + # else if not 0, possibly do a reset + if not m: + return + if m.groups()[0] == '1': + if channel.gstate == 'init1': + channel.set_state('init2') + return + if m.groups()[0] != '0': + if channel.last_reset + 100 < time.time(): + channel.last_reset = time.time() + channel.set_state('reset') + return + def data_handle(channel, line, m): # Response to _OWANDATA - should contain IP address etc if not m: @@ -558,12 +574,11 @@ control['reset'] = [ PowerAction('reopen'), #PowerAction('off'), AtAction(at='E0', timeout=30000), - ChangeStateAction('init'), + ChangeStateAction('init1'), ] # For suspend, we want power on, but no wakups for status or cellid control['suspend'] = [ - AtAction(check='+CFUN?', ok='\+CFUN: [14]', at='+CFUN=1', timeout=10000), AtAction(check='+CPAS', ok='\+CPAS: (\d)', handle = call_status), CheckSMS(), ChangeStateAction(None), # allow async state change @@ -576,7 +591,6 @@ control['suspend'] = [ ] control['resume'] = [ BlockSuspendAction(True), - AtAction(check='+CFUN?', ok='\+CFUN: [14]', at='+CFUN=1', timeout=10000), AtAction(check='+CPAS', ok='\+CPAS: (\d)', handle = call_status), AtAction(at='+CNMI=1,1,2,0,0', critical=False), AtAction(at='_OSQI=1', critical=False), @@ -591,7 +605,10 @@ control['listenerr'] = [ AtAction(at='V1E0'), AtAction(at='+CMEE=2;+CRC=1') ] -control['init'] = [ + +# init1 checks phone status and once we are online +# we switch to init2, then idle +control['init1'] = [ BlockSuspendAction(True), PowerAction('on'), AtAction(at='V1E0'), @@ -605,6 +622,10 @@ control['init'] = [ # record=('carrier', '\\1'), timeout=10000), AtAction(check='+COPS?', ok='\+COPS: \d+,\d+,"([^"]*)"', at='+COPS=0', record=('carrier', '\\1'), timeout=10000), + AtAction(check='+CFUN?', ok='\+CFUN: (\d)', at='+CFUN=1', timeout=10000, handle=check_cfun, repeat=5000), +] + +control['init2'] = [ # text format for various messages such SMS AtAction(check='+CMGF?', ok='\+CMGF: 0', at='+CMGF=0'), # get location status updates @@ -655,6 +676,7 @@ control['idle'] = [ RouteVoice(False), CheckSMS(), BlockSuspendAction(False), + AtAction(check='+CFUN?', ok='\+CFUN: (\d)', timeout=10000, handle=check_cfun, repeat=30000), AtAction(check='+COPS?', ok='\+COPS: \d+,\d+,"([^"]*)"', at='+COPS', record=('carrier', '\\1'), timeout=10000), #AtAction(check='+COPS?', ok='\+COPS: \d+,\d+,"([^"]*)"', at='+COPS=0', @@ -795,6 +817,7 @@ class GsmD(AtChannel): self.next_data_call = 0 self.gstate = None self.nextstate = [] + self.last_reset = time.time() record('carrier','') record('cell','') @@ -893,7 +916,7 @@ class GsmD(AtChannel): if self.suspend_handle.suspended: self.set_state('suspend') else: - self.set_state('init') + self.set_state('init1') else: if not self.flightmode: self.flightmode = True diff --git a/gsm/notes b/gsm/notes index 47b4b0e..c3abe72 100644 --- a/gsm/notes +++ b/gsm/notes @@ -295,3 +295,12 @@ Data sequencing: ADD CLCC monitoring to get out of 'idle' into 'on-call' ? + + +How to record data usage? +Need a log file with: + data/time SIM bytes-received bytes-transmitted + +that updates every 10 minutes. +Also have state file with counts at last log so increments can +be monitored. -- 2.39.5