ghrh

Revision:
4:63a2619b423b
Parent:
3:48ee24a4b0f3
Child:
5:ac2342f162fa
--- 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());
-        }
-    }
-}