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.h Source File

GPRS.h

00001 /*
00002   GPRS.h
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 #ifndef __GPRS_H__
00024 #define __GPRS_H__
00025 
00026 #include "mbed.h"
00027 #include "modem.h"
00028 
00029 #define DEFAULT_WAIT_RESP_TIMEOUT 500
00030 #define MAX_SOCK_NUM 7                  // (0~6)
00031 
00032 enum Protocol {
00033     CLOSED = 0,
00034     TCP    = 1,
00035     UDP    = 2,
00036 };
00037 
00038 class GPRS: public Modem
00039 {
00040 
00041 public:
00042     /** Constructor
00043      *  @param tx mbed pin to use for tx line of Serial interface
00044      *  @param rx mbed pin to use for rx line of Serial interface
00045      *  @param baudRate serial communicate baud rate
00046      *  @param apn name of the gateway for GPRS to connect to the network
00047      *  @param userName apn's username, usually is NULL
00048      *  @param passWord apn's password, usually is NULL
00049      */
00050     GPRS(PinName tx, PinName rx, const char* apn, const char* userName = NULL, const char *passWord = NULL);
00051 
00052     /** get instance of GPRS class
00053      */
00054     static GPRS* getInstance() {
00055         return inst;
00056     };
00057 
00058     /** Connect the GPRS module to the network.
00059      *  @return true if connected, false otherwise
00060      */
00061     bool join(void);
00062 
00063     /** Disconnect the GPRS module from the network
00064      *  @returns true if successful
00065      */
00066     bool disconnect(void);
00067     
00068     /** Close a tcp connection
00069      *  @returns true if successful
00070      */
00071     bool close(int socket);
00072 
00073     /** Open a tcp/udp connection with the specified host on the specified port
00074      *  @param socket an endpoint of an inter-process communication flow of GPRS module,for SIM900 module, it is in [0,6]
00075      *  @param ptl protocol for socket, TCP/UDP can be choosen
00076      *  @param host host (can be either an ip address or a name. If a name is provided, a dns request will be established)
00077      *  @param port port
00078      *  @param timeout wait time (ms)
00079      *  @returns true if successful
00080      */
00081     bool connect(int socket, Protocol ptl, const char * host, int port, int timeout = DEFAULT_TIMEOUT_MS);
00082 
00083     /** Set the protocol (UDP or TCP)
00084      *  @param socket socket
00085      *  @param p protocol
00086      *  @returns true if successful
00087      */
00088     bool setProtocol(int socket, Protocol p);
00089 
00090     /** Reset the GPRS module
00091      */
00092     void reset();
00093 
00094     /** wait a few time to check if GPRS module is readable or not
00095      *  @param socket socket
00096      *  @param wait_time time of waiting
00097      */
00098     int wait_readable(int socket, int wait_time);
00099 
00100     /** wait a few time to check if GPRS module is writeable or not
00101      *  @param socket socket
00102      *  @param wait_time time of waiting
00103      */
00104     int wait_writeable(int socket, int req_size);
00105 
00106     /** Check if a tcp link is active
00107      *  @returns true if successful
00108      */
00109     bool is_connected(int socket);
00110 
00111     /** send data to socket
00112      *  @param socket socket
00113      *  @param str string to be sent
00114      *  @param len string length
00115      *  @returns return bytes that actually been send
00116      */
00117     int sock_send(int socket, const char * str, int len);
00118 
00119     /** read data from socket
00120      *  @param socket socket
00121      *  @param buf buffer that will store the data read from socket
00122      *  @param len string length need to read from socket
00123      *  @returns bytes that actually read
00124      */
00125     int sock_recv(int socket, char* buf, int len);
00126 
00127     /** convert the host to ip
00128      *  @param host host ip string, ex. 10.11.12.13
00129      *  @param ip long int ip address, ex. 0x11223344
00130      *  @returns true if successful
00131      */
00132     bool gethostbyname(const char* host, uint32_t* ip);
00133 
00134     int new_socket();
00135     uint16_t new_port();
00136     uint32_t _ip;
00137     
00138 
00139 protected:
00140     static GPRS* inst;
00141     int socketID;
00142     const char* _apn;
00143     const char* _userName;
00144     const char* _passWord;
00145     bool connected;
00146     uint8_t recv_buf[32];       // for 
00147     uint8_t recv_bytes;
00148 };
00149 
00150 #endif