Implementation of 3G USB Modem Huawei E372

Dependents:   PYRN

Revision:
2:61ac95f0af72
Parent:
0:67daedd6f74f
--- a/ATCommandsInterface.cpp	Fri Feb 20 17:15:55 2015 +0000
+++ b/ATCommandsInterface.cpp	Tue Apr 14 13:27:07 2015 +0000
@@ -17,7 +17,12 @@
  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  */
 
-#include "dbg.h"
+#define __DEBUG__ 0
+#ifndef __MODULE__
+#define __MODULE__ "ATCommandsInterface.cpp"
+#endif
+#include "MyDebug.h"
+
 #include <cstdio>
 //#include <cstring> //For memset, strstr...
 
@@ -38,10 +43,10 @@
 {
   if( m_open )
   {
-    USB_WARN("AT interface is already open");
+    WARN("AT interface is already open");
     return OK;
   }
-  USB_DBG("Opening AT interface");
+  DBG("Opening AT interface");
   //Start processing
   m_processingThread.signal_set(AT_SIG_PROCESSING_START);
 
@@ -60,34 +65,34 @@
   }
   m_eventsMtx.unlock();
 
-  USB_DBG("AT interface opened");
+  DBG("AT interface opened");
 
   return OK;
 }
 
 //Initialize AT link
-int ATCommandsInterface::init()
+int ATCommandsInterface::init(const char *atInitSeq)
 {
-  USB_DBG("Sending ATZ E1 V1");
+  DBG("Sending %s",atInitSeq);
   //Should we flush m_pStream at this point ???
   int err;
   int tries = 5;
   do
   {
-    err = executeSimple("ATZ E1 V1", NULL, 3000); //Enable echo and verbosity
+    err = executeSimple(atInitSeq, NULL, 3000); //Enable echo and verbosity
     if(err && tries)
     {
-      USB_WARN("No response, trying again");
+      WARN("No response, trying again");
       Thread::wait(1000); //Give dongle time to recover
     }
   } while(err && tries--);
   if( err )
   {
-    USB_ERR("Sending ATZ E1 V1 returned with err code %d", err);
+    ERR("Sending %s returned with err code %d", atInitSeq, err);
     return err;
   }
 
-  USB_DBG("AT interface initialized");
+  DBG("AT interface initialized");
 
   return OK;
 }
@@ -97,11 +102,11 @@
 {
   if( !m_open )
   {
-    USB_WARN("AT interface is already closed");
+    WARN("AT interface is already closed");
     return OK;
   }
 
-  USB_DBG("Closing AT interface");
+  DBG("Closing AT interface");
 
   //Stop processing
   m_processingThread.signal_set(AT_SIG_PROCESSING_STOP);
@@ -127,7 +132,7 @@
   }
   m_eventsMtx.unlock();
 
-  USB_DBG("AT interface closed");
+  DBG("AT interface closed");
   return OK;
 }
 
@@ -143,10 +148,10 @@
 
 int ATCommandsInterface::execute(const char* command, IATCommandsProcessor* pProcessor, ATResult* pResult, uint32_t timeout/*=1000*/)
 {
-  USB_DBG("Executing command %s", command);
+  DBG("Executing command %s", command);
   if(!m_open)
   {
-    USB_WARN("Interface is not open!");
+    WARN("Interface is not open!");
     return NET_INVALID;
   }
 
@@ -159,7 +164,7 @@
   if(evt.status == osEventMail)
   {
     m_AT2Env.free((int*)evt.value.p);
-    USB_WARN("Previous result discarded");
+    WARN("Previous result discarded");
   }
 
   //Send params to the process routine
@@ -175,14 +180,14 @@
 
   Thread::wait(100); //FIXME find stg else
 
-  USB_DBG("Sending command ready signal to AT thread & aborting current blocking read operation");
+  DBG("Sending command ready signal to AT thread & aborting current blocking read operation");
 
   //Produce command ready signal
   int* msg = m_env2AT.alloc(osWaitForever);
   *msg = AT_CMD_READY;
   m_env2AT.put(msg);
 
-  USB_DBG("Trying to enter abortRead()");
+  DBG("Trying to enter abortRead()");
   //Unlock process routine (abort read)
   m_pStream->abortRead(); //This is thread-safe
 
@@ -196,15 +201,15 @@
     *msg = AT_TIMEOUT;
     m_env2AT.put(msg);
 
-    USB_DBG("Trying to enter abortRead()");
+    DBG("Trying to enter abortRead()");
     //Unlock process routine (abort read)
     m_pStream->abortRead(); //This is thread-safe
 
-    USB_WARN("Command returned no message");
+    WARN("Command returned no message");
     m_transactionMtx.unlock();
     return NET_TIMEOUT;
   }
-  USB_DBG("Command returned with message %d", *msg);
+  DBG("Command returned with message %d", *msg);
 
   m_AT2Env.free((int*)evt.value.p);
 
@@ -216,10 +221,10 @@
   int ret = ATResultToReturnCode(m_transactionResult);
   if(ret != OK)
   {
-    USB_WARN("Command returned AT result %d with code %d", m_transactionResult.result, m_transactionResult.code);
+    WARN("Command returned AT result %d with code %d", m_transactionResult.result, m_transactionResult.code);
   }
 
-  USB_DBG("Command returned successfully");
+  DBG("Command returned successfully");
 
   //Unlock transaction mutex
   m_transactionMtx.unlock();
@@ -265,7 +270,7 @@
   static bool lineDetected = false;
 
   //Block on serial read or incoming command
-  USB_DBG("Trying to read a new line from stream");
+  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)
@@ -276,33 +281,33 @@
   {
     m_inputPos+=readLen;
     m_inputBuf[m_inputPos] = '\0'; //Add null terminating character to ease the use of str* functions
-    USB_DBG("In buffer: [%s]", m_inputBuf);
+    DBG("In buffer: [%s]", m_inputBuf);
   }
 
   if( ret == NET_INTERRUPTED ) //It is worth checking readLen as data might have been read even though the read was interrupted
   {
-    USB_DBG("Read was interrupted");
+    DBG("Read was interrupted");
     return NET_INTERRUPTED; //0 chars were read
   }
   else if(readLen == 0)
   {
-    USB_DBG("Nothing read");
+    DBG("Nothing read");
     return OK; //0 chars were read
   }
 
-  USB_DBG("Trying to process incoming line");
+  DBG("Trying to process incoming line");
   bool lineProcessed = false;
 
   do
   {
     lineProcessed = false; //Reset flag
 
-    USB_DBG("New iteration");
+    DBG("New iteration");
 
     //Look for a new line
     if(!lineDetected)
     {
-      USB_DBG("No line detected yet");
+      DBG("No line detected yet");
       //Try to look for a starting CRLF
       char* crPtr = strchr(m_inputBuf, CR);
       /*
@@ -318,7 +323,7 @@
       */
       if(crPtr != NULL)
       {
-        USB_DBG("CR char found");
+        DBG("CR char found");
 
 #if 0
         //Discard all preceding characters (can do nothing if m_inputBuf == crPtr)
@@ -338,24 +343,24 @@
               //At this point we can check whether this is the end of a preceding line or the beginning of a new one
               if(m_inputBuf[2] != CR)
               {
-                USB_DBG("Beginning of new line found");
+                DBG("Beginning of new line found");
                 //Beginning of a line
                 lineDetected = true; //Move to next state-machine step
               }
               else
               {
                 //End of an unprocessed line
-                USB_WARN("End of unprocessed line");
+                WARN("End of unprocessed line");
               }
               //In both cases discard CRLF
-              USB_DBG("Discarding CRLF");
+              DBG("Discarding CRLF");
               memmove(m_inputBuf, m_inputBuf + 2, (m_inputPos + 1) - 2); //Move null-terminating char as well
               m_inputPos = m_inputPos - 2; //Adjust m_inputPos
             }
             else
             {
               //This is completely unexpected, discard the CR char to try to recover good state
-              USB_WARN("Unexpected %c char (%02d code) found after CR char", m_inputBuf[1]);
+              WARN("Unexpected %c char (%02d code) found after CR char", m_inputBuf[1]);
               memmove(m_inputBuf, m_inputBuf + 1, (m_inputPos + 1) - 1); //Move null-terminating char as well
               m_inputPos = m_inputPos - 1; //Adjust m_inputPos
             }
@@ -366,7 +371,7 @@
         {
           int crPos = crPtr - m_inputBuf;
           int lfOff = 0; //Offset for LF if present
-          USB_DBG("New line found (possible echo of command)");
+          DBG("New line found (possible echo of command)");
           //This is the end of line
           //Replace m_inputBuf[crPos] with null-terminating char
           m_inputBuf[crPos] = '\0';
@@ -394,14 +399,14 @@
             memmove(m_inputBuf, m_inputBuf + crPos + lfOff + 1, (m_inputPos + 1) - (crPos + lfOff + 1)); //Move null-terminating char as well
             m_inputPos = m_inputPos - (crPos + lfOff + 1); //Adjust m_inputPos
           }
-          USB_DBG("One line was successfully processed");
+          DBG("One line was successfully processed");
           lineProcessed = true; //Line was processed with success
           lineDetected = false; //Search now for a new line
         }
       }
       else if(m_inputBuf[0] == LF) //If there is a remaining LF char from the previous line, discard it
       {
-        USB_DBG("Discarding single LF char");
+        DBG("Discarding single LF char");
         memmove(m_inputBuf, m_inputBuf + 1, (m_inputPos + 1) - 1); //Move null-terminating char as well
         m_inputPos = m_inputPos - 1; //Adjust m_inputPos
       }
@@ -410,7 +415,7 @@
     //Look for the end of line
     if(lineDetected)
     {
-      USB_DBG("Looking for end of line");
+      DBG("Looking for end of line");
       //Try to look for a terminating CRLF
       char* crPtr = strchr(m_inputBuf, CR);
       /*
@@ -428,7 +433,7 @@
 
       if(crPtr != NULL)
       {
-        USB_DBG("CR char found");
+        DBG("CR char found");
         int crPos = crPtr - m_inputBuf;
         //To determine the sequence we need at least 2 chars
         if(m_inputPos - crPos >= 2)
@@ -436,7 +441,7 @@
           //Look for a LF char next to the CR char
           if(m_inputBuf[crPos + 1] == LF)
           {
-            USB_DBG("End of new line found");
+            DBG("End of new line found");
             //This is the end of line
             //Replace m_inputBuf[crPos] with null-terminating char
             m_inputBuf[crPos] = '\0';
@@ -458,13 +463,13 @@
               m_inputPos = m_inputPos - (crPos + 2); //Adjust m_inputPos
             }
 
-            USB_DBG("One line was successfully processed");
+            DBG("One line was successfully processed");
             lineProcessed = true; //Line was processed with success
           }
           else
           {
             //This is completely unexpected, discard all chars till the CR char to try to recover good state
-            USB_WARN("Unexpected %c char (%02d code) found in incoming line", m_inputBuf[crPos + 1]);
+            WARN("Unexpected %c char (%02d code) found in incoming line", m_inputBuf[crPos + 1]);
             memmove(m_inputBuf, m_inputBuf + crPos + 1, (m_inputPos + 1) - (crPos + 1)); //Move null-terminating char as well
             m_inputPos = m_inputPos - (crPos + 1); //Adjust m_inputPos
           }
@@ -473,7 +478,7 @@
       }
       else if(greaterThanPtr != NULL)
       {
-        USB_DBG("> char found");
+        DBG("> char found");
         int gdPos = greaterThanPtr - m_inputBuf;
         //To determine the sequence we need at least 2 chars
         if(m_inputPos - gdPos >= 2)
@@ -499,13 +504,13 @@
               return ret;
             }
 
-            USB_DBG("One line was successfully processed");
+            DBG("One line was successfully processed");
             lineProcessed = true; //Line was processed with success
           }
           else
           {
             //This is completely unexpected, discard all chars till the GD char to try to recover good state
-            USB_WARN("Unexpected %c char (%02d code) found in incoming line", m_inputBuf[gdPos + 1]);
+            WARN("Unexpected %c char (%02d code) found in incoming line", m_inputBuf[gdPos + 1]);
             memmove(m_inputBuf, m_inputBuf + gdPos + 1, (m_inputPos + 1) - (gdPos + 1)); //Move null-terminating char as well
             m_inputPos = m_inputPos - (gdPos + 1); //Adjust m_inputPos
           }
@@ -521,12 +526,12 @@
     //Discard everything
     m_inputPos = 0;
     m_inputBuf[0] = '\0'; //Always have a null-terminating char at start of buffer
-    USB_WARN("Incoming buffer is too short to process incoming line");
+    WARN("Incoming buffer is too short to process incoming line");
     //Look for a new line
     lineDetected = false;
   }
 
-  USB_DBG("Processed every full incoming lines");
+  DBG("Processed every full incoming lines");
 
   return OK;
 }
@@ -534,7 +539,7 @@
 int ATCommandsInterface::trySendCommand()
 {
   osEvent evt = m_env2AT.get(0);
-  USB_DBG("status = %d, msg = %d", evt.status, evt.value.p);
+  DBG("status = %d, msg = %d", evt.status, evt.value.p);
   if(evt.status == osEventMail)
   {
     int* msg = (int*) evt.value.p;
@@ -542,9 +547,9 @@
     {
       if(m_transactionState != IDLE)
       {
-        USB_WARN("Previous command not processed!");
+        WARN("Previous command not processed!");
       }
-      USB_DBG("Sending pending command");
+      DBG("Sending pending command");
       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
@@ -561,14 +566,14 @@
 
 int ATCommandsInterface::processReadLine()
 {
-  USB_DBG("Processing read line [%s]", m_inputBuf);
+  DBG("Processing read line [%s]", m_inputBuf);
   //The line is stored in m_inputBuf
   if(m_transactionState == COMMAND_SENT)
   {
     //If the command has been sent, checks echo to see if it has been received properly
     if( strcmp(m_transactionCommand, m_inputBuf) == 0 )
     {
-      USB_DBG("Command echo received");
+      DBG("Command echo received");
       //If so, it means that the following lines will only be solicited results
       m_transactionState = READING_RESULT;
       return OK;
@@ -613,7 +618,7 @@
     //The following lines can either be a command response or a result code (OK / ERROR / CONNECT / +CME ERROR: %s / +CMS ERROR: %s)
     if(strcmp("OK", m_inputBuf) == 0)
     {
-      USB_DBG("OK result received");
+      DBG("OK result received");
       m_transactionResult.code = 0;
       m_transactionResult.result = ATResult::AT_OK;
       m_transactionState = IDLE;
@@ -624,7 +629,7 @@
     }
     else if(strcmp("ERROR", m_inputBuf) == 0)
     {
-      USB_DBG("ERROR result received");
+      DBG("ERROR result received");
       m_transactionResult.code = 0;
       m_transactionResult.result = ATResult::AT_ERROR;
       m_transactionState = IDLE;
@@ -635,7 +640,7 @@
     }
     else if(strncmp("CONNECT", m_inputBuf, 7 /*=strlen("CONNECT")*/) == 0) //Result can be "CONNECT" or "CONNECT %d", indicating baudrate
     {
-      USB_DBG("CONNECT result received");
+      DBG("CONNECT result received");
       m_transactionResult.code = 0;
       m_transactionResult.result = ATResult::AT_CONNECT;
       m_transactionState = IDLE;
@@ -646,7 +651,7 @@
     }
     else if(strcmp("COMMAND NOT SUPPORT", m_inputBuf) == 0) //Huawei-specific, not normalized
     {
-      USB_DBG("COMMAND NOT SUPPORT result received");
+      DBG("COMMAND NOT SUPPORT result received");
       m_transactionResult.code = 0;
       m_transactionResult.result = ATResult::AT_ERROR;
       m_transactionState = IDLE;
@@ -658,7 +663,7 @@
     else if(strstr(m_inputBuf, "+CME ERROR:") == m_inputBuf) //Mobile Equipment Error
     {
       std::sscanf(m_inputBuf + 12 /* =strlen("+CME ERROR: ") */, "%d", &m_transactionResult.code);
-      USB_DBG("+CME ERROR: %d result received", m_transactionResult.code);
+      DBG("+CME ERROR: %d result received", m_transactionResult.code);
       m_transactionResult.result = ATResult::AT_CME_ERROR;
       m_transactionState = IDLE;
       int* msg = m_AT2Env.alloc(osWaitForever);
@@ -669,7 +674,7 @@
     else if(strstr(m_inputBuf, "+CMS ERROR:") == m_inputBuf) //SIM Error
     {
       std::sscanf(m_inputBuf + 13 /* =strlen("+CME ERROR: ") */, "%d", &m_transactionResult.code);
-      USB_DBG("+CMS ERROR: %d result received", m_transactionResult.code);
+      DBG("+CMS ERROR: %d result received", m_transactionResult.code);
       m_transactionResult.result = ATResult::AT_CMS_ERROR;
       m_transactionState = IDLE;
       int* msg = m_AT2Env.alloc(osWaitForever);
@@ -679,7 +684,7 @@
     }
     else
     {
-      USB_DBG("Unprocessed result received: '%s'", m_inputBuf);
+      DBG("Unprocessed result received: '%s'", m_inputBuf);
       //Must call transaction processor to complete line processing
       int ret = m_pTransactionProcessor->onNewATResponseLine(this, m_inputBuf); //Here sendData can be called
       return ret;
@@ -691,12 +696,12 @@
 
 int ATCommandsInterface::processEntryPrompt()
 {
-  USB_DBG("Calling prompt handler");
+  DBG("Calling prompt handler");
   int ret = m_pTransactionProcessor->onNewEntryPrompt(this); //Here sendData can be called
 
   if( ret != NET_MOREINFO ) //A new prompt is expected
   {
-    USB_DBG("Sending break character");
+    DBG("Sending break character");
     //Send CTRL+Z (break sequence) to exit prompt
     char seq[2] = {BRK, 0x00};
     sendData(seq);
@@ -710,11 +715,11 @@
 {
   //m_inputBuf is cleared at this point (and MUST therefore be empty)
   int dataLen = strlen(data);
-  USB_DBG("Sending raw string of length %d", dataLen);
+  DBG("Sending raw string of length %d", dataLen);
   int ret = m_pStream->write((uint8_t*)data, dataLen, osWaitForever);
   if(ret)
   {
-    USB_WARN("Could not write to stream (returned %d)", ret);
+    WARN("Could not write to stream (returned %d)", ret);
     return ret;
   }
 
@@ -726,7 +731,7 @@
     int ret = m_pStream->read((uint8_t*)m_inputBuf, &readLen, MIN(dataLen - dataPos, AT_INPUT_BUF_SIZE - 1), osWaitForever); //Make sure we do not read more than needed otherwise it could break the parser
     if(ret)
     {
-      USB_WARN("Could not read from stream (returned %d)", ret);
+      WARN("Could not read from stream (returned %d)", ret);
       m_inputPos = 0; //Reset input buffer state
       m_inputBuf[0] = '\0'; //Always have a null-terminating char at start of buffer
       return ret;
@@ -735,7 +740,7 @@
     if( memcmp(m_inputBuf, data + dataPos, readLen) != 0 )
     {
       //Echo does not match output
-      USB_WARN("Echo does not match output");
+      WARN("Echo does not match output");
       m_inputPos = 0; //Reset input buffer state
       m_inputBuf[0] = '\0'; //Always have a null-terminating char at start of buffer
       return NET_DIFF;
@@ -746,7 +751,7 @@
 
   } while(dataPos < dataLen);
 
-  USB_DBG("String sent successfully");
+  DBG("String sent successfully");
 
   m_inputPos = 0; //Reset input buffer state
   m_inputBuf[0] = '\0'; //Always have a null-terminating char at start of buffer
@@ -783,14 +788,14 @@
 
 void ATCommandsInterface::process() //Processing thread
 {
-  USB_DBG("AT Thread started");
+  DBG("AT Thread started");
   while(true)
   {
-    USB_DBG("AT Processing on hold");
+    DBG("AT Processing on hold");
     m_processingThread.signal_wait(AT_SIG_PROCESSING_START); //Block until the process is started
 
     m_processingMtx.lock();
-    USB_DBG("AT Processing started");
+    DBG("AT Processing started");
     //First of all discard buffer
     int ret;
     size_t readLen;
@@ -801,13 +806,13 @@
     m_inputPos = 0; //Clear input buffer
     do
     {
-      USB_DBG("Trying to read a new line");
+      DBG("Trying to read a new line");
       tryReadLine();
-      USB_DBG("Trying to send a pending command");
+      DBG("Trying to send a pending command");
       trySendCommand();
     } while( m_processingThread.signal_wait(AT_SIG_PROCESSING_STOP, 0).status != osEventSignal ); //Loop until the process is interrupted
     m_processingMtx.unlock();
-    USB_DBG("AT Processing stopped");
+    DBG("AT Processing stopped");
   }
 }