Skip to contentSkip to conversation

phonesim: Time out +CSIM when no SIM present.

PATCH New Andrzej Zaborowski 1 participant 1 message Apr 29, 2010

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 phonesim: Time out +CSIM when no SIM present.

Send an ERROR after some time from receiving a AT+CSIM when card is not inserted. Implement cancelling pending commands.

---
 src/phonesim.cpp |   25 ++++++++++++++++++++++++-
 src/phonesim.h   |   10 ++++++++++
 2 files changed, 34 insertions(+), 1 deletions(-)
src/phonesim.cpp +24 -1
index d5d7d87..65f7ac7 100644
@@ -545,6 +545,7 @@ SimRules::SimRules( int fd, QObject *p,  const QString& filename, HardwareManipu
     currentChannel = 1;
     incomingUsed = 0;
     lineUsed = 0;
+    pending = NULL;
     defaultToolkitApp = toolkitApp = new DemoSimApplication( this, this );
     connect( _callManager, SIGNAL(controlEvent(QSimControlEvent)),
              toolkitApp, SLOT(controlEvent(QSimControlEvent)) );
@@ -839,6 +840,9 @@ void SimRules::tryReadCommand()
 
 void SimRules::destruct()
 {
+    if ( pending )
+        delete pending;
+
     if ( toolkitApp != defaultToolkitApp )
         delete toolkitApp;
     delete defaultToolkitApp;
@@ -915,6 +919,19 @@ SimState *SimRules::state( const QString& name ) const
     return 0;
 }
 
+class TimeoutCommand : public SimRules::PendingCommand, protected QObject {
+    QString response;
+    virtual void timerEvent(QTimerEvent *evt) {
+        rules.respond( response );
+        delete this;
+    }
+public:
+    TimeoutCommand( SimRules &r, QString rsp, int secs = 20 ) :
+            SimRules::PendingCommand(r), QObject(), response(rsp) {
+        startTimer( secs * 1000 );
+    }
+};
+
 bool SimRules::simCsimOk( const QByteArray& payload )
 {
     unsigned char sw1 = 0x90;
@@ -955,8 +972,11 @@ bool SimRules::simCommand( const QString& cmd )
     if ( !cmd.startsWith( "AT+CSIM=" ) )
         return false;
 
-    if ( getMachine() && !getMachine()->getSimPresent() )
+    /* If no card inserted, just time out */
+    if ( getMachine() && !getMachine()->getSimPresent() ) {
+        new TimeoutCommand( *this, "ERROR" );
         return true;
+    }
 
     // Extract the binary payload of the AT+CSIM command.
     int comma = cmd.indexOf( QChar(',') );
@@ -1067,6 +1087,9 @@ bool SimRules::simCommand( const QString& cmd )
 
 void SimRules::command( const QString& cmd )
 {
+    if ( pending )
+        delete pending;
+
     if(getMachine())
         getMachine()->handleToData(cmd);
 
src/phonesim.h +10 -0
index cb7a5e1..6e35147 100644
@@ -284,6 +284,14 @@ public:
     SimApplication *simApplication() const { return toolkitApp; }
     void setSimApplication( SimApplication *app );
 
+    class PendingCommand {
+    public:
+        PendingCommand( SimRules &r ) : rules(r) { r.pending = this; }
+        virtual ~PendingCommand() { rules.pending = NULL; }
+    protected:
+        SimRules &rules;
+    };
+
 signals:
     void returnQueryVariable( const QString&, const QString & );
     void returnQueryState( const QString& );
@@ -358,6 +366,8 @@ private:
     CallManager *_callManager;
 
     bool simCsimOk( const QByteArray& payload );
+
+    PendingCommand *pending;
 };
 
 
Signature · 1 line
1.6.1