A library for the SIM900 module to enable calling, answering, sending and receiving SMS messages

Dependents:   Seeed_GPRS_Shield_GSM BluetoothNONIN HealthCare_Graduation

Fork of GSM by Components

Files at this revision

API Documentation at this revision

Comitter:
lawliet
Date:
Tue Jan 21 06:44:58 2014 +0000
Parent:
3:48ee24a4b0f3
Child:
5:ac2342f162fa
Commit message:
verison 2.0 (fix several errors)

Changed in this revision

gprs.cpp Show annotated file Show diff for this revision Revisions of this file
gprs.h Show annotated file Show diff for this revision Revisions of this file
--- a/gprs.cpp	Fri Jan 10 05:59:36 2014 +0000
+++ b/gprs.cpp	Tue Jan 21 06:44:58 2014 +0000
@@ -19,24 +19,8 @@
   License along with this library; if not, write to the Free Software
   Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 */
-
-
 #include "gprs.h"
 
-int GPRS::init(void)
-{
-    wait(0.5);
-    sendCmd("AT+CFUN=1\r\n");
-    wait(1);
-    if(0 != checkSIMStatus()) { //check SIM card status
-        return -1;
-    }
-    if(checkSignalStrength()<1) { //check Signal Strength
-        return -1;
-    }
-    return 0;
-}
-
 int GPRS::readBuffer(char *buffer,int count)
 {
     int i = 0;
@@ -101,14 +85,26 @@
     return 0;
 }
 
-
-
 int GPRS::sendCmdAndWaitForResp(char *cmd, char *resp, int timeout)
 {
     sendCmd(cmd);
     return waitForResp(resp,timeout);
 }
 
+int GPRS::init(void)
+{
+    if(0 != checkSIMStatus()) {
+        return -1;
+    }
+    if(checkSignalStrength()<1) {
+        return -1;
+    }
+    if(0 != settingSMS()) {
+        return -1;
+    }
+    return 0;
+}
+
 int GPRS::checkSIMStatus(void)
 {
     char gprsBuffer[30];
@@ -150,67 +146,70 @@
     return index;
 }
 
+int GPRS::settingSMS(void)
+{
+    if(0 != sendCmdAndWaitForResp("AT+CNMI=2,2\r\n", "OK", DEFAULT_TIMEOUT)) {
+        return -1;
+    }
+    if(0 != sendCmdAndWaitForResp("AT+CMGF=1\r\n", "OK", DEFAULT_TIMEOUT)) {
+        return -1;
+    }
+    return 0;
+}
+
 int GPRS::sendSMS(char *number, char *data)
 {
     char cmd[64];
-    if(0 != sendCmdAndWaitForResp("AT+CMGF=1\r\n", "OK", DEFAULT_TIMEOUT)) { // Set message mode to ASCII
-        return -1;
+    while(gprsSerial.readable()) {
+        char c = gprsSerial.getc();
     }
-    wait(0.5);
-    // Set the phone number
-    snprintf(cmd, sizeof(cmd),"AT+CMGS=\"%s\"\r\n", number);
+    snprintf(cmd, sizeof(cmd),"AT+CMGS=\"%s\"\r\n",number);
     if(0 != sendCmdAndWaitForResp(cmd,">",DEFAULT_TIMEOUT)) {
         return -1;
     }
     wait(1);
-    gprsSerial.puts(data);// Send Message
-    wait(0.5);
-    gprsSerial.putc(0x1A);//end mark
+    gprsSerial.puts(data);
+    gprsSerial.putc((char)0x1a);
     return 0;
 }
 
-int GPRS::readSMS(char *buffer, char *message, bool check)
+int GPRS::readSMS(char *message, int index)
 {
-    int index,i = 0;
+    int i = 0;
     char gprsBuffer[100];
     char *p,*s;
-
-    if(sscanf(buffer, "$$+CMTI: \"SM\",%d", &index)>0) {
-        gprsSerial.printf("AT+CMGR=%d\r\n", index);
-    } else {
-        return -1;
-    }
+    gprsSerial.printf("AT+CMGR=%d\r\n",index);
     cleanBuffer(gprsBuffer,100);
     readBuffer(gprsBuffer,100);
-    if(NULL == ( s = strstr(gprsBuffer,"+CMGR: \"REC UNREAD\""))) {
+    if(NULL == ( s = strstr(gprsBuffer,"+CMGR"))) {
         return -1;
     }
-
-    //check phone number
-    if(check) {
-        char number[20];
-        snprintf(number,sizeof(number),"\"+86%s\"",phoneNumber); //for China
-        p = s + 20;
-        if(0 != (strncmp(number,p,14))) {
-            return -1;
+    if(NULL != ( s = strstr(gprsBuffer,"+32"))) {
+        p = s + 6;
+        while((*p != '$')&&(i < SMS_MAX_LENGTH-1)) {
+            message[i++] = *(p++);
         }
+        message[i] = '\0';
     }
-    p = s + 64;
-    while(*p != '$') {
-        message[i++] = *(p++);
-    }
-    message[i] = '\0';
     return 0;
 }
 
 int GPRS::deleteSMS(int index)
 {
-    char cmd[64];
+    char cmd[32];
     snprintf(cmd,sizeof(cmd),"AT+CMGD=%d\r\n",index);
     sendCmd(cmd);
     return 0;
 }
 
+int GPRS::getSMS(char* message)
+{
+    if(NULL != messageBuffer) {
+        strncpy(message,messageBuffer,SMS_MAX_LENGTH);
+    }
+    return 0;
+}
+
 int GPRS::callUp(char *number)
 {
     if(0 != sendCmdAndWaitForResp("AT+COLP=1\r\n","OK",5)) {
@@ -227,18 +226,23 @@
     return 0;
 }
 
-int GPRS::loop(bool check)
+int GPRS::loopHandle(void)
 {
     char gprsBuffer[100];
     int i = 0;
+    char *s = NULL;
     cleanBuffer(gprsBuffer,100);
+    while(gprsSerial.readable()) {
+        char c = gprsSerial.getc();
+    }
+    wait(1);
+START:
     while(1) {
         if(gprsSerial.readable()) {
             break;
         }
         wait(1);
     }
-
     timeCnt.start();  // start timer
     while(1) {
         while (gprsSerial.readable()) {
@@ -257,21 +261,27 @@
             break;
         }
     }
-
     if(NULL != strstr(gprsBuffer,"RING")) {
-        if(0 != answer()) {
+        return MESSAGE_RING;
+    } else if(NULL != (s = strstr(gprsBuffer,"+CMT"))) { //SMS: $$+CMTI: "SM",24$$
+        if(NULL != (s = strstr(gprsBuffer,"+32"))) {
+            s += 6;
+            int i = 0;
+            cleanBuffer(messageBuffer,SMS_MAX_LENGTH);
+            while((*s != '$')&&(i < SMS_MAX_LENGTH-1)) {
+                messageBuffer[i++] = *(s++);
+            }
+            messageBuffer[i] = '\0';
+            return MESSAGE_SMS;
+        } else {
+            goto START;
         }
-    } else if(NULL != strstr(gprsBuffer,"$$+CMTI: \"SM\"")) { //SMS: $$+CMTI: "SM",24$$
-        char message[64];
-        if(0 != readSMS(gprsBuffer, message, check)) {
-        }
+    } else {
+        goto START;
     }
-    return 0;
 }
 
 
-/****************************************GPRS TCP CONNECT************************************/
-
 int GPRS::connectTCP(char *ip, char *port)
 {
     char cipstart[50];
@@ -309,29 +319,3 @@
     sendCmd("AT+CIPSHUT\r\n");
     return 0;
 }
-
-/****************************************GPRS DEBUG******************************************/
-void GPRS::serialDebug(PinName tx, PinName rx)
-{
-    char buffer[64];
-    int count = 0;
-    Serial pc(tx,rx);
-    while(1) {
-        if(gprsSerial.readable()) {
-            while(gprsSerial.readable()) {
-                char c = gprsSerial.getc();
-                buffer[count++] = c;
-                if(count == 64) break;
-            }
-            pc.puts(buffer);
-            for(int i = 0; i < count; i++) {
-                buffer[i] = NULL;
-            }
-            count = 0;
-        }
-
-        if(pc.readable()) {
-            gprsSerial.putc(pc.getc());
-        }
-    }
-}
--- a/gprs.h	Fri Jan 10 05:59:36 2014 +0000
+++ b/gprs.h	Tue Jan 21 06:44:58 2014 +0000
@@ -27,6 +27,14 @@
 #include "mbed.h"
 
 #define DEFAULT_TIMEOUT     5
+#define SMS_MAX_LENGTH      16
+
+enum GPRS_MESSAGE {
+    MESSAGE_RING = 0,
+    MESSAGE_SMS  = 1,
+    MESSAGE_ERROR
+};
+
 
 /** GPRS class.
  *  Used for mobile communication. attention that GPRS module communicate with MCU in serial protocol
@@ -52,6 +60,115 @@
      */
     int init(void);
 
+    /** check SIM card' Status
+     *  @returns
+     *      0 on success
+     *      -1 on error
+     */
+    int checkSIMStatus(void);
+
+    /** check signal strength
+     *  @returns
+     *      signal strength in number(ex 3,4,5,6,7,8...) on success
+     *      -1 on error
+     */
+    int checkSignalStrength(void);
+
+    /** set SMS format and processing mode
+     *  @returns
+     *      0 on success
+     *      -1 on error
+     */
+    int settingSMS(void);
+
+    /** send text SMS
+     *  @param  *number    phone number which SMS will be send to
+     *  @param  *data   message that will be send to
+     *  @returns
+     *      0 on success
+     *      -1 on error
+     */
+    int sendSMS(char *number, char *data);
+
+    /** read SMS by index
+     *  @param  *message   buffer used to get SMS message
+         *  @param  index    which SMS message to read
+     *  @returns
+     *      0 on success
+     *      -1 on error
+     */
+    int readSMS(char *message, int index);
+
+    /** delete SMS message on SIM card
+     *  @param  *index    the index number which SMS message will be delete
+     *  @returns
+     *      0 on success
+     *      -1 on error
+     */
+    int deleteSMS(int index);
+
+    /** read SMS when coming a message,it will be store in messageBuffer.
+     *  @param message  buffer used to get SMS message
+     */
+    int getSMS(char* message);
+
+    /** call someone
+     *  @param  *number    the phone number which you want to call
+     *  @returns
+     *      0 on success
+     *      -1 on error
+     */
+    int callUp(char *number);
+
+    /** auto answer if coming a call
+     *  @returns
+     *      0 on success
+     *      -1 on error
+     */
+    int answer(void);
+
+    /** a loop to wait for some event. if a call comes in, it will auto answer it and if a SMS message comes in, it will read the message
+     *  @param  *check    whether to check phone number when get event
+     *  @returns
+     *      0 on success
+     *      -1 on error
+     */
+    int loopHandle(void);
+
+    /** build TCP connect
+     *  @param  *ip    ip address which will connect to
+     *  @param  *port   TCP server' port number
+     *  @returns
+     *      0 on success
+     *      -1 on error
+     */
+    int connectTCP(char *ip, char *port);
+
+    /** send data to TCP server
+     *  @param  *data    data that will be send to TCP server
+     *  @returns
+     *      0 on success
+     *      -1 on error
+     */
+    int sendTCPData(char *data);
+
+    /** close TCP connection
+     *  @returns
+     *      0 on success
+     *      -1 on error
+     */
+    int closeTCP(void);
+
+    /** close TCP service
+     *  @returns
+     *      0 on success
+     *      -1 on error
+     */
+    int shutTCP(void);
+
+
+    //USBSerial pc;
+private:
     /** read from GPRS module and save to buffer array
      *  @param  *buffer buffer array to save what read from GPRS module
      *  @param  *count  the maximal bytes number read from GPRS module
@@ -85,116 +202,11 @@
      */
     int sendCmdAndWaitForResp(char *cmd, char *resp, int timeout);
 
-    /** check SIM card' Status
-     *  @returns
-     *      0 on success
-     *      -1 on error
-     */
-    int checkSIMStatus(void);
-
-    /** check signal strength
-     *  @returns
-     *      signal strength in number(ex 3,4,5,6,7,8...) on success
-     *      -1 on error
-     */
-    int checkSignalStrength(void);
-
-    /** check network is ok or not
-     *  @returns
-     *      0 on success
-     *      -1 on error
-     */
-    int networkInit(void);
-
-    /** send text SMS
-     *  @param  *number    phone number which SMS will be send to
-     *  @param  *data   message that will be send to
-     *  @returns
-     *      0 on success
-     *      -1 on error
-     */
-    int sendSMS(char *number, char *data);
-
-    /** read SMS if get a SMS
-     *  @param  *buffer    buffer that get from GPRS module(when getting a SMS, GPRS module will return a buffer array)
-     *  @param  *message   buffer used to get SMS message
-     *  @param  check    whether to check phone number(we may only want to read SMS from specified phone number)
-     *  @returns
-     *      0 on success
-     *      -1 on error
-     */
-    int readSMS(char *buffer, char *message, bool check);
-
-    /** delete SMS message on SIM card
-     *  @param  *index    the index number which SMS message will be delete
-     *  @returns
-     *      0 on success
-     *      -1 on error
-     */
-    int deleteSMS(int index);
-
-    /** call someone
-     *  @param  *number    the phone number which you want to call
-     *  @returns
-     *      0 on success
-     *      -1 on error
-     */
-    int callUp(char *number);
-
-    /** auto answer if coming a call
-     *  @returns
-     *      0 on success
-     *      -1 on error
-     */
-    int answer(void);
-
-    /** a loop to wait for some event. if a call comes in, it will auto answer it and if a SMS message comes in, it will read the message
-     *  @param  *check    whether to check phone number when get event
-     *  @returns
-     *      0 on success
-     *      -1 on error
-     */
-    int loop(bool check);
-
-    /** build TCP connect
-     *  @param  *ip    ip address which will connect to
-     *  @param  *port   TCP server' port number
-     *  @returns
-     *      0 on success
-     *      -1 on error
-     */
-    int connectTCP(char *ip, char *port);
-
-    /** send data to TCP server
-     *  @param  *data    data that will be send to TCP server
-     *  @returns
-     *      0 on success
-     *      -1 on error
-     */
-    int sendTCPData(char *data);
-
-    /** close TCP connection
-     *  @returns
-     *      0 on success
-     *      -1 on error
-     */
-    int closeTCP(void);
-
-    /** close TCP service
-     *  @returns
-     *      0 on success
-     *      -1 on error
-     */
-    int shutTCP(void);
-
-    /** used for serial debug, you can specify tx and rx pin and then communicate with GPRS module with common AT commands
-     */
-    void serialDebug(PinName tx, PinName rx);
-
-private:
     Serial gprsSerial;
     Timer timeCnt;
     char *phoneNumber;
+    char messageBuffer[SMS_MAX_LENGTH];
 };
 
 #endif
+