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

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers GPRS.cpp Source File

GPRS.cpp

00001 /*
00002   GPRS.cpp
00003   2014 Copyright (c) Seeed Technology Inc.  All right reserved.
00004 
00005   Author:lawliet zou(lawliet.zou@gmail.com)
00006   2014-2-24
00007 
00008   This library is free software; you can redistribute it and/or
00009   modify it under the terms of the GNU Lesser General Public
00010   License as published by the Free Software Foundation; either
00011   version 2.1 of the License, or (at your option) any later version.
00012 
00013   This library is distributed in the hope that it will be useful,
00014   but WITHOUT ANY WARRANTY; without even the implied warranty of
00015   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00016   Lesser General Public License for more details.
00017 
00018   You should have received a copy of the GNU Lesser General Public
00019   License along with this library; if not, write to the Free Software
00020   Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
00021 */
00022 
00023 #include "mbed.h"
00024 #include "GPRS.h"
00025 
00026 #define DEBUG
00027 
00028 #ifdef DEBUG
00029 #include "USBSerial.h"
00030 extern USBSerial pc;
00031 #define LOG(args...)        pc.printf(args)
00032 #else
00033 #define LOG(args...)
00034 #endif
00035 
00036 
00037 GPRS* GPRS::inst;
00038 
00039 GPRS::GPRS(PinName tx, PinName rx, const char* apn, const char* userName, const char* passWord) : Modem(tx,rx)
00040 {
00041     inst = this;
00042     _apn = apn;
00043     _userName = userName;
00044     _passWord = passWord;
00045     socketID = -1;
00046     
00047     connected = false;
00048     recv_bytes = 0;
00049 }
00050 
00051 
00052 
00053 bool GPRS::join()
00054 {
00055     char ip_addr_buf[32];
00056     
00057     //Select multiple connection
00058     command("AT+CIPMUX=1\r\n");
00059     
00060     // Set APN
00061     command("AT+CSTT=\"%s\",\"%s\",\"%s\"\r\n",_apn,_userName,_passWord);
00062 
00063     // Brings up wireless connection
00064     command("AT+CIICR\r\n");
00065 
00066     // Get local IP address
00067     printf("AT+CIFSR\r\n");
00068     
00069     readline(ip_addr_buf, sizeof(ip_addr_buf));   // read echo
00070 
00071     if (readline(ip_addr_buf, sizeof(ip_addr_buf)) <= 0) {
00072         LOG("failed to join network\r\n");
00073         return false;
00074     }
00075     
00076     int a, b, c, d;
00077     if (sscanf(ip_addr_buf, "%d.%d.%d.%d", &a, &b, &c, &d) != 4) {
00078         LOG("failed to get ip, r(%s)\r\n", ip_addr_buf);
00079         return false;
00080     }
00081     
00082     _ip = (a << 24) + (b << 16) + (c << 8) + d;
00083 
00084     return true;
00085 }
00086 
00087 bool GPRS::setProtocol(int socket, Protocol p)
00088 {
00089     if (socket < 0 || socket > MAX_SOCK_NUM-1) {
00090         return false;
00091     }
00092     //ToDo: setProtocol
00093     return true;
00094 }
00095 
00096 bool GPRS::connect(int socket, Protocol ptl, const char * host, int port, int timeout)
00097 {
00098     const char *protocol;
00099     if (socket < 0 || socket > MAX_SOCK_NUM-1) {
00100         return false;
00101     }
00102     if(ptl == TCP) {
00103         protocol = "TCP";
00104     } else if(ptl == UDP) {
00105         protocol = "UDP";
00106     } else {
00107         return false;
00108     }
00109     
00110     command("AT+CIPSTART=%d,\"%s\",\"%s\",%d\r\n", socket, protocol, host, port);
00111     
00112     char response[64] = {0,};
00113     if (readline(response, sizeof(response)) < 0) {
00114         LOG("wait for connection - timeout\r\n");
00115         return false;
00116     }
00117     
00118     if (strstr(response, "CONNECT OK") || strstr(response, "ALREADY CONNECT")) {
00119         connected = true;
00120         return true;
00121     } else {
00122         LOG("failed to connect (r:%s)\r\n", response);
00123         return false;
00124     }
00125 }
00126 
00127 bool GPRS::gethostbyname(const char* host, uint32_t* ip)
00128 {
00129     int a, b, c, d;
00130     if (sscanf(host, "%d.%d.%d.%d", &a, &b, &c, &d) == 4) {
00131         *ip = (a << 24) + (b << 16) + (c << 8) + d;
00132         
00133         return true;
00134     }
00135     
00136     return false;
00137 }
00138 
00139 bool GPRS::disconnect()
00140 {
00141     puts("AT+CIPSHUT\r\n");
00142     connected = false;
00143     return true;
00144 }
00145 
00146 bool GPRS::is_connected(int socket)
00147 {
00148     static uint32_t last = 0;
00149     if (((uint32_t)us_ticker_read() - last) > 60000000) {
00150         last = us_ticker_read();
00151         
00152         flush();
00153         printf("AT+CIPSTATUS=%d\r\n", socket);
00154         
00155         connected = false;
00156         
00157         char response[80];
00158         readline(response, sizeof(response));   // echo, ignore
00159         if (readline(response, sizeof(response)) > 0) {
00160             if (strstr(response, "CONNECTED")) {
00161                 connected = true;
00162             }
00163             
00164             readline(response, sizeof(response)); // ok
00165         }
00166     }
00167     
00168     return connected;
00169 }
00170 
00171 void GPRS::reset()
00172 {
00173 
00174 }
00175 
00176 bool GPRS::close(int socket)
00177 {
00178     if (socket < 0 || socket > (MAX_SOCK_NUM - 1)) {
00179         return false;
00180     }
00181     
00182     printf("AT+CIPCLOSE=%d\r\n", socket);
00183     connected = false;
00184     return true;
00185 }
00186 
00187 int GPRS::sock_send(int socket, const char * data, int len)
00188 {
00189     if (socket < 0 || socket > MAX_SOCK_NUM-1 || len <= 0) {
00190         return -1;
00191     }
00192 
00193     char response[64];
00194 
00195     flush();
00196     printf("AT+CIPSEND=%d,%d\r\n", socket, len);
00197     readline(response, sizeof(response));       // echo, ignore
00198     if (match("> ")) {
00199         connected = false;
00200         return -1;
00201     }
00202     
00203     write(data, len);
00204     
00205     // read echo data
00206     for (int i = 0; i < len; i++) {
00207         while (!readable()) {
00208         }
00209         char ch = getc();
00210     }
00211     
00212     readline(response, sizeof(response));
00213     
00214     // data received
00215     int sock;
00216     int bytes = 0;
00217     if (sscanf(response, "+RECEIVE,%d,%d:", &sock, &bytes) == 2) {
00218         while (bytes > 0) {
00219             if (readable()) {
00220                 recv_buf[recv_bytes] = getc();
00221                 recv_bytes++;
00222                 bytes--;
00223             }
00224         }
00225         
00226         readline(response, sizeof(response));
00227     }
00228     
00229     if (strstr(response, "SEND OK")) {      // 0, SEND OK
00230         return len;
00231     }
00232     
00233     if (strstr(response, "CLOSED")) {
00234         connected = false;
00235     }
00236     return -1;
00237 }
00238 
00239 int GPRS::sock_recv(int socket, char* buf, int len)
00240 {
00241     if (recv_bytes > 0) {
00242         if (len >= recv_bytes) {
00243             len = recv_bytes;
00244             memcpy(buf, recv_buf, recv_bytes);
00245             recv_bytes = 0;
00246         } else {
00247             memcpy(buf, recv_buf, len);
00248             recv_bytes -= len;
00249             memcpy(recv_buf, recv_buf + len, recv_bytes);
00250         }
00251         
00252         return len;
00253     }
00254     
00255     char response[32];
00256     if (readline(response, sizeof(response)) <= 0) {
00257         return -1;
00258     }
00259     
00260     if (strstr(response, "CLOSED")) {
00261         LOG("socket is closed, r(%s)\r\n", response);
00262         connected = false;
00263         
00264         return -1;
00265     }
00266     
00267     int sock;
00268     int bytes = 0;
00269     if (sscanf(response, "+RECEIVE,%d,%d:", &sock, &bytes) != 2) {
00270         LOG("unknow:%s\r\n", response);
00271         return -1;
00272     }
00273     
00274     int bytes_read = 0;
00275     while (bytes_read < bytes) {
00276         if (readable()) {
00277             buf[bytes_read] = getc();
00278             bytes_read++;
00279         }
00280     }
00281     
00282     return bytes;  
00283 }
00284 
00285 int GPRS::new_socket()
00286 {
00287     socketID = 0; //we only support one socket.
00288     return socketID; 
00289 }