a library to use GPRS like ethernet or wifi, which makes it possible to connect to the internet with your GPRS module

Dependencies:   BufferedSerial

Dependents:   ThinkSpeak_Test roam_v1 roam_v2 finalv3

Fork of GPRSInterface by wei zou

Files at this revision

API Documentation at this revision

Comitter:
yihui
Date:
Fri Apr 03 09:51:13 2015 +0000
Parent:
13:379ce1d51b88
Commit message:
bugfix

Changed in this revision

GPRS/GPRS.cpp Show annotated file Show diff for this revision Revisions of this file
diff -r 379ce1d51b88 -r 3c6454f033ac GPRS/GPRS.cpp
--- a/GPRS/GPRS.cpp	Fri Apr 03 15:44:04 2015 +0800
+++ b/GPRS/GPRS.cpp	Fri Apr 03 09:51:13 2015 +0000
@@ -1,26 +1,26 @@
-/*
-  GPRS.cpp
-  2014 Copyright (c) Seeed Technology Inc.  All right reserved.
-
-  Author:lawliet zou(lawliet.zou@gmail.com)
-  2014-2-24
-
-  This library is free software; you can redistribute it and/or
-  modify it under the terms of the GNU Lesser General Public
-  License as published by the Free Software Foundation; either
-  version 2.1 of the License, or (at your option) any later version.
-
-  This library is distributed in the hope that it will be useful,
-  but WITHOUT ANY WARRANTY; without even the implied warranty of
-  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
-  Lesser General Public License for more details.
-
-  You should have received a copy of the GNU Lesser General Public
-  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 "mbed.h"
+/*
+  GPRS.cpp
+  2014 Copyright (c) Seeed Technology Inc.  All right reserved.
+
+  Author:lawliet zou(lawliet.zou@gmail.com)
+  2014-2-24
+
+  This library is free software; you can redistribute it and/or
+  modify it under the terms of the GNU Lesser General Public
+  License as published by the Free Software Foundation; either
+  version 2.1 of the License, or (at your option) any later version.
+
+  This library is distributed in the hope that it will be useful,
+  but WITHOUT ANY WARRANTY; without even the implied warranty of
+  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+  Lesser General Public License for more details.
+
+  You should have received a copy of the GNU Lesser General Public
+  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 "mbed.h"
 #include "GPRS.h"
 
 #define DEBUG
@@ -32,38 +32,38 @@
 #else
 #define LOG(args...)
 #endif
-
-
-GPRS* GPRS::inst;
-
-GPRS::GPRS(PinName tx, PinName rx, const char* apn, const char* userName, const char* passWord) : Modem(tx,rx)
-{
-    inst = this;
-    _apn = apn;
-    _userName = userName;
-    _passWord = passWord;
+
+
+GPRS* GPRS::inst;
+
+GPRS::GPRS(PinName tx, PinName rx, const char* apn, const char* userName, const char* passWord) : Modem(tx,rx)
+{
+    inst = this;
+    _apn = apn;
+    _userName = userName;
+    _passWord = passWord;
     socketID = -1;
     
     connected = false;
-    recv_bytes = 0;
-}
-
-
-
-bool GPRS::join()
-{
+    recv_bytes = 0;
+}
+
+
+
+bool GPRS::join()
+{
     char ip_addr_buf[32];
-    
-    //Select multiple connection
+    
+    //Select multiple connection
     command("AT+CIPMUX=1\r\n");
-    
-    // Set APN
+    
+    // Set APN
     command("AT+CSTT=\"%s\",\"%s\",\"%s\"\r\n",_apn,_userName,_passWord);
-
-    // Brings up wireless connection
-    command("AT+CIICR\r\n");
-
-    // Get local IP address
+
+    // Brings up wireless connection
+    command("AT+CIICR\r\n");
+
+    // Get local IP address
     printf("AT+CIFSR\r\n");
     
     readline(ip_addr_buf, sizeof(ip_addr_buf));   // read echo
@@ -79,32 +79,32 @@
         return false;
     }
     
-    _ip = (a << 24) + (b << 16) + (c << 8) + d;
-
-    return true;
-}
-
-bool GPRS::setProtocol(int socket, Protocol p)
-{
-    if (socket < 0 || socket > MAX_SOCK_NUM-1) {
-        return false;
-    }
-    //ToDo: setProtocol
-    return true;
-}
-
-bool GPRS::connect(int socket, Protocol ptl, const char * host, int port, int timeout)
+    _ip = (a << 24) + (b << 16) + (c << 8) + d;
+
+    return true;
+}
+
+bool GPRS::setProtocol(int socket, Protocol p)
 {
-    const char *protocol;
-    if (socket < 0 || socket > MAX_SOCK_NUM-1) {
-        return false;
-    }
-    if(ptl == TCP) {
-        protocol = "TCP";
-    } else if(ptl == UDP) {
-        protocol = "UDP";
-    } else {
-        return false;
+    if (socket < 0 || socket > MAX_SOCK_NUM-1) {
+        return false;
+    }
+    //ToDo: setProtocol
+    return true;
+}
+
+bool GPRS::connect(int socket, Protocol ptl, const char * host, int port, int timeout)
+{
+    const char *protocol;
+    if (socket < 0 || socket > MAX_SOCK_NUM-1) {
+        return false;
+    }
+    if(ptl == TCP) {
+        protocol = "TCP";
+    } else if(ptl == UDP) {
+        protocol = "UDP";
+    } else {
+        return false;
     }
     
     command("AT+CIPSTART=%d,\"%s\",\"%s\",%d\r\n", socket, protocol, host, port);
@@ -121,47 +121,67 @@
     } else {
         LOG("failed to connect (r:%s)\r\n", response);
         return false;
-    }
-}
-
-bool GPRS::gethostbyname(const char* host, uint32_t* ip)
+    }
+}
+
+bool GPRS::gethostbyname(const char* host, uint32_t* ip)
 {
-    int a, b, c, d;
+    int a, b, c, d;
     if (sscanf(host, "%d.%d.%d.%d", &a, &b, &c, &d) == 4) {
         *ip = (a << 24) + (b << 16) + (c << 8) + d;
         
         return true;
     }
-    
-    return false;
-}
-
-bool GPRS::disconnect()
-{
+    
+    return false;
+}
+
+bool GPRS::disconnect()
+{
     puts("AT+CIPSHUT\r\n");
-    connected = false;
-    return true;
-}
-
-bool GPRS::is_connected(int socket)
-{
-    return connected;
-}
-
-void GPRS::reset()
-{
-
-}
-
-bool GPRS::close(int socket)
-{
-    if (socket < 0 || socket > (MAX_SOCK_NUM - 1)) {
-        return false;
+    connected = false;
+    return true;
+}
+
+bool GPRS::is_connected(int socket)
+{
+    static uint32_t last = 0;
+    if (((uint32_t)us_ticker_read() - last) > 60000000) {
+        last = us_ticker_read();
+        
+        flush();
+        printf("AT+CIPSTATUS=%d\r\n", socket);
+        
+        connected = false;
+        
+        char response[80];
+        readline(response, sizeof(response));   // echo, ignore
+        if (readline(response, sizeof(response)) > 0) {
+            if (strstr(response, "CONNECTED")) {
+                connected = true;
+            }
+            
+            readline(response, sizeof(response)); // ok
+        }
     }
-    
-    printf("AT+CIPCLOSE=%d\r\n", socket);
-    connected = false;
-    return true;
+    
+    return connected;
+}
+
+void GPRS::reset()
+{
+
+}
+
+bool GPRS::close(int socket)
+{
+    if (socket < 0 || socket > (MAX_SOCK_NUM - 1)) {
+        return false;
+    }
+    
+    printf("AT+CIPCLOSE=%d\r\n", socket);
+    connected = false;
+    return true;
 }
 
 int GPRS::sock_send(int socket, const char * data, int len)
@@ -247,9 +267,7 @@
     int sock;
     int bytes = 0;
     if (sscanf(response, "+RECEIVE,%d,%d:", &sock, &bytes) != 2) {
-        LOG("socket is closed, r(%s)\r\n", response);
-        
-        connected = false;
+        LOG("unknow:%s\r\n", response);
         return -1;
     }
     
@@ -268,4 +286,4 @@
 {
     socketID = 0; //we only support one socket.
     return socketID; 
-}
+}