local fork

Dependencies:   Socket USBHostWANDongle_bleedingedge lwip-sys lwip

Dependents:   Encrypted

Fork of VodafoneUSBModem_bleedingedge by Donatien Garnier

Revision:
79:897a0de9d668
Parent:
75:a6ac8206a58d
--- a/at/ATCommandsInterface.cpp	Tue Jan 29 13:49:23 2013 +0000
+++ b/at/ATCommandsInterface.cpp	Tue Mar 05 14:54:15 2013 +0000
@@ -32,25 +32,34 @@
 #include "ATCommandsInterface.h"
 
 ATCommandsInterface::ATCommandsInterface(IOStream* pStream) :
-   m_pStream(pStream), m_open(false), m_env2AT(), m_AT2Env(), m_processingMtx(),
-   m_processingThread(&ATCommandsInterface::staticCallback, this, (osPriority)AT_THREAD_PRIORITY, 4*192),
-   m_eventsMgmtMtx(), m_eventsProcessingMtx()
+   m_pStream(pStream), // this is the serial interface to the modem
+   m_open(false),      // AT interface is initially in a closed state
+   m_env2AT(),         // send messages from calling functions to AT parsing thread
+   m_AT2Env(),         // send messages from AT parsing thread to calling functions
+   m_processingMtx(),  // mutex for processing thread
+   m_processingThread( // construct processing thread
+      &ATCommandsInterface::staticCallback, // static callback uses this pointer to run process()
+      this,
+      (osPriority)AT_THREAD_PRIORITY,       // normal priority
+      4*192                                 // size of the processing thread
+   ),
+   m_eventsMgmtMtx(),       // mutex for managing events
+   m_eventsProcessingMtx()  // mutex for processing events
 {
-  memset(m_eventsHandlers, 0, MAX_AT_EVENTS_HANDLERS * sizeof(IATEventsHandler*));
-
-  m_processingMtx.lock();
+   // zero the memory for the event handler pointers
+   memset(m_eventsHandlers, 0, MAX_AT_EVENTS_HANDLERS * sizeof(IATEventsHandler*));
+   m_processingMtx.lock();
 }
 
-//Open connection to AT Interface in order to execute command & register/unregister events
-int ATCommandsInterface::open()
-{
-  if( m_open )
-  {
+// open connection to AT Interface in order to execute command & register/unregister events
+int ATCommandsInterface::open() {
+  if( m_open ) {
     WARN("AT interface is already open");
     return OK;
   }
   DBG("Opening AT interface");
-  //Start processing
+  
+  // start processing
   m_processingThread.signal_set(AT_SIG_PROCESSING_START);
 
   m_processingMtx.unlock();
@@ -62,7 +71,7 @@
   return OK;
 }
 
-//Initialize AT link & start events processing
+// initialize AT link & start events processing
 int ATCommandsInterface::init()
 {
   DBG("Sending ATZ E1 V1");
@@ -70,7 +79,8 @@
   //Lock transaction mutex
   m_transactionMtx.lock();
   
-  //Should we flush m_pStream at this point ???
+  // should we flush m_pStream at this point ???
+  // ash - it would do no harm so we should, incase some pending crap arrives
   int err;
   int tries = 5;
   do
@@ -121,6 +131,7 @@
   m_processingThread.signal_set(AT_SIG_PROCESSING_STOP);
   //m_stopSphre.release();
 
+  // ash - don't know why he sends this as it is never used XXX
   int* msg = m_env2AT.alloc(osWaitForever);
   *msg = AT_STOP;
   m_env2AT.put(msg); //Used to unstall the process if needed
@@ -157,7 +168,7 @@
 
   //Lock transaction mutex
   m_transactionMtx.lock();
-  
+  DBG("ATCommandsInterface::execute");
   disableEvents(); //Disable unsollicited result codes
   int ret = executeInternal(command, pProcessor, pResult, timeout);
   enableEvents(); //Re-enable unsollicited result codes whatever the result of the command is
@@ -244,6 +255,7 @@
   m_pStream->abortRead(); //This is thread-safe
 
   //Wait for a result (get result message)
+  DBG("Timeout: %d",timeout);
   evt = m_AT2Env.get(timeout);
 
   if(evt.status != osEventMail)
@@ -264,7 +276,7 @@
       evt = m_AT2Env.get(osWaitForever);
       msgResult = *((int*) evt.value.p);
       m_AT2Env.free((int*)evt.value.p);
-    } while(msgResult != AT_TIMEOUT);  
+    } while(msgResult != AT_TIMEOUT  );  
 
     WARN("Command returned no message");
     return NET_TIMEOUT;
@@ -296,6 +308,7 @@
   //Block on serial read or incoming command
   DBG("Trying to read a new line from stream");
   int ret = m_pStream->waitAvailable(); //This can be aborted
+
   size_t readLen = 0;
   if(ret == OK)
   {
@@ -310,7 +323,7 @@
 
   if( ret == NET_INTERRUPTED ) //It is worth checking readLen as data might have been read even though the read was interrupted
   {
-    DBG("Read was interrupted");
+    DBG("Read was interrupted with %d chars read",readLen);
     return NET_INTERRUPTED; //0 chars were read
   }
   else if(readLen == 0)
@@ -573,7 +586,7 @@
       {
         WARN("Previous command not processed!");
       }
-      DBG("Sending pending command");
+      DBG("Sending pending command %s (%d)",m_transactionCommand,strlen(m_transactionCommand));
       m_pStream->write((uint8_t*)m_transactionCommand, strlen(m_transactionCommand), osWaitForever);
       char cr = CR;
       m_pStream->write((uint8_t*)&cr, 1, osWaitForever); //Carriage return line terminator
@@ -581,6 +594,7 @@
     }
     else //Timeout
     {
+      DBG("Timeout message received");
       //Acknowledge
       int* msg = m_AT2Env.alloc(osWaitForever);
       *msg = AT_TIMEOUT;
@@ -740,6 +754,7 @@
 //This will be called on initialization & after the execution of a command
 void ATCommandsInterface::enableEvents()
 {
+  DBG("Trying to enable events");
   //Advertize this to events handlers
   m_eventsMgmtMtx.lock();
   for(int i = 0; i < MAX_AT_EVENTS_HANDLERS; i++) //Find a free slot
@@ -750,10 +765,13 @@
       //Enable this kind of events
       if(m_eventsHandlers[i]->getEventsEnableCommand() != NULL)
       {
+        DBG("Enabling events for handler %d",i);
         int ret = executeInternal(m_eventsHandlers[i]->getEventsEnableCommand(), this, NULL); //Execute enable command
         if(ret)
         {
-          WARN("Events enabling command failed");
+          WARN("Events enabling command failed: %d",ret);
+        } else {
+           DBG("Enabled events");
         }
       }
     }