Cellular library for MTS Socket Modem Arduino Shield devices from Multi-Tech Systems

Dependents:   mtsas mtsas mtsas mtsas

Files at this revision

API Documentation at this revision

Comitter:
Vanger
Date:
Thu Jul 31 21:03:36 2014 +0000
Parent:
42:adcf25505b1c
Child:
44:bf07f3d85822
Commit message:
Added Working example code to EasyIP.h

Changed in this revision

Cellular/EasyIP.h Show annotated file Show diff for this revision Revisions of this file
Cellular/UIP.cpp Show annotated file Show diff for this revision Revisions of this file
Test/TestSMS.h Show annotated file Show diff for this revision Revisions of this file
--- a/Cellular/EasyIP.h	Wed Jul 30 20:27:09 2014 +0000
+++ b/Cellular/EasyIP.h	Thu Jul 31 21:03:36 2014 +0000
@@ -22,6 +22,287 @@
     * be used seamlessly with clients and services built on top of this interface already within
     * the mbed library.
     * The default baud rate for the cellular radio is 115200 bps.
+    *
+    * @code
+    * #include "mbed.h"
+    * #include "mtsas.h"
+    * #include "TCPSocketConnection.h"
+    *
+    * int main(){
+    *   //Modify to match your apn if you are using an HSPA radio with a SIM card
+    *   const char APN[] = "internet";
+    *
+    *   //Sets the log level to INFO, which is about midway on priority levels
+    *   //Possible levels: FATAL, ERROR, WARNING, INFO, DEBUG, TRACE, NONE
+    *   MTSLog::setLogLevel(MTSLog::TRACE_LEVEL);
+    *
+    *   // STMicro Nucelo F401RE
+    *   // The supported jumper configurations of the MTSAS do not line up with
+    *   // the pin mapping of the Nucleo F401RE. Therefore, the MTSAS serial TX
+    *   // pin (JP8 Pin 2) must be manually jumped to Serial1 RX (Shield pin D2)
+    *   // and the MTSAS serial RX pin (JP9 Pin 2) pin must be manually jumped to
+    *   // Serial1 TX (Shield pin D8).
+    *   // Uncomment the following line to use the STMicro Nuceleo F401RE
+    *   MTSSerialFlowControl* io = new MTSSerialFlowControl(D8, D2, D3, D6);
+    *
+    *   // Freescale KL46Z
+    *   // To configure the pins for the Freescale KL46Z board, use configuration B
+    *   // for the SocketModem.
+    *   // Uncomment the following line to use the Freescale KL46Z board
+    *   // MTSSerialFlowControl* io = new MTSSerialFlowControl(D2, D9, D3, D6);
+    *
+    *   // Freescale KL64F
+    *   // To configure the pins for the Freescale KL46Z board, use configuration A
+    *   // for the SocketModem.
+    *   // Uncomment te following line to use the Freescale KL46F board
+    *   // MTSSerialFlowControl* io = new MTSSerialFlowControl(D1, D0, D3, D6);
+    *
+    *   // Sets the baudrate for communicating with the radio
+    *   io->baud(115200);
+    *
+    *   // Sets up the interfacing with the radio through the MTS library
+    *   Cellular* radio = CellularFactory::create(io);
+    *   radio->configureSignals(D4, D7, RESET);
+    *   Transport::setTransport(radio);
+    *
+    *   // Sets the APN on the device (if necessary)
+    *   for (int i = 0; i < 10; i++) {
+    *       if (i >= 10) {
+    *           logError("Failed to set APN to %s", APN);
+    *       }
+    *       if (radio->setApn(APN) == MTS_SUCCESS) {
+    *           logInfo("Successfully set APN to %s", APN);
+    *           break;
+    *       } else {
+    *           wait(1);
+    *       }
+    *   }
+    *
+    *   //Establish PPP link
+    *   for (int i = 0; i < 10; i++) {
+    *       if (i >= 10) {
+    *       logError("Failed to establish PPP link");
+    *       }
+    *       if (radio->connect() == true) {
+    *           logInfo("Successfully established PPP link");
+    *           break;
+    *       } else {
+    *           wait(1);
+    *       }
+    *   }
+    *
+    *   //Ping google.com (optional)
+    *   for (int i = 0; i < 10; i++) {
+    *       if (i >= 10) {
+    *           logError("Failed to ping www.google.com");
+    *       }
+    *       if (radio->ping("www.google.com") == true) {
+    *           logInfo("Successfully pinged www.google.com");
+    *           break;
+    *       } else {
+    *           wait(1);
+    *       }
+    *   }
+    *
+    *   //Used for packet verification from server's data response
+    *   const char PATTERN_LINE1[] = "abcdefghijklmnopqrstuvwzyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890!@#$%^&*()[]{}|";
+    *   const char PATTERN[] = "abcdefghijklmnopqrstuvwzyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890!@#$%^&*()[]{}|\r\n"
+    *                          "abcdefghijklmnopqrstuvwzyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890!@#$%^&*()[]{}/\r\n"
+    *                          "abcdefghijklmnopqrstuvwzyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890!@#$%^&*()[]{}-\r\n"
+    *                          "abcdefghijklmnopqrstuvwzyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890!@#$%^&*()[]{}\\\r\n"
+    *                          "abcdefghijklmnopqrstuvwzyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890!@#$%^&*()[]{}|\r\n"
+    *                          "abcdefghijklmnopqrstuvwzyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890!@#$%^&*()[]{}/\r\n"
+    *                          "abcdefghijklmnopqrstuvwzyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890!@#$%^&*()[]{}-\r\n"
+    *                          "abcdefghijklmnopqrstuvwzyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890!@#$%^&*()[]{}\\\r\n"
+    *                          "abcdefghijklmnopqrstuvwzyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890!@#$%^&*()[]{}*\r\n";
+    *   
+    *   const char MENU_LINE1[] = "send ascii pattern until keypress";
+    *   const char MENU[] = "1       send ascii pattern until keypress"
+    *                       "2       send ascii pattern (numbered)"
+    *                       "3       send pattern and close socket"
+    *                       "4       send [ETX] and wait for keypress"
+    *                       "5       send [DLE] and wait for keypress"
+    *                       "6       send all hex values (00-FF)"
+    *                       "q       quit"
+    *                       ">:";
+    *   
+    *   const char TCP_TEST_SERVER[] = "204.26.122.5";
+    *   const int TCP_TEST_PORT = 7000;
+    *   
+    *   //Creates TCP socket pointer instance
+    *   TCPSocketConnection* sock = new TCPSocketConnection();
+    *   //Turns off read_blocking and sets socket timeout to 2s
+    *   sock->set_blocking(false, 2);
+    *   
+    *   Timer tmr; //Used for timeouts
+    *   int bytesRead = 0; //Number of bytes read
+    *   const int readSize = 1024; //Size of buffer
+    *   char buffer[readSize] = {0}; //Read buffer
+    *   string result; //Result as a string
+    *   
+    *   //Open TCP socket
+    *   for (int i = 0; i < 5; i++) {
+    *       if (i >= 5) {
+    *           logError("Failed to open socket");
+    *       }
+    *       if (! sock->connect(TCP_TEST_SERVER, TCP_TEST_PORT)) {
+    *           logInfo("Opened TCP server");
+    *           break;
+    *       } else {
+    *           wait(1);
+    *       }
+    *   }
+    *   
+    *   //Waiting for menu from remote server
+    *   logInfo("Receiving Menu");
+    *   tmr.reset();
+    *   tmr.start();
+    *   do {
+    *       bytesRead = sock->receive(buffer, readSize);
+    *       if (bytesRead > 0) {
+    *           result.append(buffer, bytesRead);
+    *       }
+    *       logInfo("Total Bytes Read: %d", result.size());
+    *       if(result.find(MENU_LINE1) != std::string::npos) {
+    *           break;
+    *       }
+    *   } while(tmr.read() <= 40);
+    *   
+    *   wait(5);
+    *
+    *   logInfo("Received: [%d] [%s]", result.size(), result.c_str());
+    *
+    *   //Checking that menu was successfully received
+    *   size_t pos = result.find(MENU_LINE1);
+    *   if(pos != string::npos) {
+    *       logInfo("Found Menu 1st Line");
+    *   } else {
+    *       logError("Failed To Find Menu 1st Line");
+    *       sock->close();
+    *       return 0;
+    *   }
+    *
+    *   result.clear();
+    *
+    *   //Sends a response of '2' back to choose option 2 from the menu
+    *   logInfo("Writing To Socket: 2");
+    *   if(sock->send("2\r\n", 3) == 3) {
+    *       logInfo("Successfully Wrote '2'");
+    *   } else {
+    *       logError("Failed To Write '2'");
+    *       sock->close();
+    *       return 0;
+    *   }
+    *   logInfo("Expecting 'how many ? >:'");
+    *   tmr.reset();
+    *   tmr.start();
+    *   do {
+    *       bytesRead = sock->receive(buffer, readSize);
+    *       if (bytesRead > 0) {
+    *           result.append(buffer, bytesRead);
+    *       }
+    *       logInfo("Total Bytes Read: %d", result.size());
+    *       if(result.find("how many") != std::string::npos) {
+    *           break;
+    *       }
+    *   } while(tmr.read() <= 40);
+    *
+    *   logInfo("Received: [%d] [%s]", result.size(), result.c_str());
+    *
+    *   //Sends 2 to have the server send the pattern twice
+    *   if(result.find("how many") != std::string::npos) {
+    *       logInfo("Successfully Found 'how many'");
+    *       logInfo("Writing To Socket: 2");
+    *       if(sock->send("2\r\n", 3) == 3) {
+    *           logInfo("Successfully wrote '2'");
+    *       } else {
+    *           logError("Failed to write '2'");
+    *           sock->close();
+    *           return 0;
+    *       }
+    *   } else {
+    *       logError("didn't receive 'how many'");
+    *       sock->close();
+    *       return 0;
+    *   }
+    *
+    *   result.clear();
+    *
+    *   //Receives data from request sent to server
+    *   logInfo("Receiving Data");
+    *   tmr.reset();
+    *   tmr.start();
+    *   do {
+    *       bytesRead = sock->receive(buffer, readSize);
+    *       if (bytesRead > 0) {
+    *           result.append(buffer, bytesRead);
+    *       }
+    *       logInfo("Total Bytes Read: %d", result.size());
+    *       if(result.size() >= 1645) {
+    *           break;
+    *       }
+    *   } while(tmr.read() <= 40);
+    *
+    *   logInfo("Received Data: [%d] [%s]", result.size(), result.c_str());
+    *
+    *   //Compares received data with expected data
+    *   pos = result.find(PATTERN_LINE1);
+    *   if(pos != string::npos) {
+    *       int patternSize = sizeof(PATTERN) - 1;
+    *       const char* ptr = &result.data()[pos];
+    *       bool match = true;
+    *       for(int i = 0; i < patternSize; i++) {
+    *           if(PATTERN[i] != ptr[i]) {
+    *               logError("1st Pattern Doesn't Match At [%d]", i);
+    *               logError("Pattern [%02X]  Buffer [%02X]", PATTERN[i], ptr[i]);
+    *               match = false;
+    *               break;
+    *           }
+    *       }
+    *       if(match) {
+    *           logInfo("Found 1st Pattern");
+    *       } else {
+    *           logError("Failed To Find 1st Pattern");
+    *           sock->close();
+    *           return 0;
+    *       }
+    *
+    *       pos = result.find(PATTERN_LINE1, pos + patternSize);
+    *       if(pos != std::string::npos) {
+    *           ptr = &result.data()[pos];
+    *           match = true;
+    *           for(int i = 0; i < patternSize; i++) {
+    *               if(PATTERN[i] != ptr[i]) {
+    *                   logError("2nd Pattern Doesn't Match At [%d]", i);
+    *                   logError("Pattern [%02X]  Buffer [%02X]", PATTERN[i], ptr[i]);
+    *                   match = false;
+    *                   break;
+    *               }
+    *           }
+    *           if(match) {
+    *               logInfo("Found 2nd Pattern");
+    *           } else {
+    *               logError("Failed To Find 2nd Pattern");
+    *               sock->close();
+    *               return 0;
+    *           }
+    *       }
+    *   } else {
+    *       logError("Failed To Find Pattern 1st Line");
+    *       sock->close();
+    *       return 0;
+    *   }
+    *
+    *   //Clears the result, and closes the socket connection.
+    *   result.clear();
+    *   sock->close();
+    *   
+    *   //Disconnect ppp link
+    *   radio->disconnect();
+    *   
+    *   logInfo("End of example code");
+    *   return 0;
+    * }
     */
 class EasyIP : public Cellular
 {
--- a/Cellular/UIP.cpp	Wed Jul 30 20:27:09 2014 +0000
+++ b/Cellular/UIP.cpp	Thu Jul 31 21:03:36 2014 +0000
@@ -185,7 +185,7 @@
 {
     char buffer[256] = {0};
     Code portCode, addressCode;
-
+    
     //1) Check that we do not have a live connection up
     if(socketOpened) {
         //Check that the address, port, and mode match
--- a/Test/TestSMS.h	Wed Jul 30 20:27:09 2014 +0000
+++ b/Test/TestSMS.h	Thu Jul 31 21:03:36 2014 +0000
@@ -22,7 +22,7 @@
 TestSMS::TestSMS() : TestCollection("TestSMS") {}
 
 void TestSMS::run() {
-    const char APN[] = "";
+    const char APN[] = "internet";
     
     string number;
     string response;