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

Committer:
yihui
Date:
Fri Apr 03 09:51:13 2015 +0000
Revision:
14:3c6454f033ac
Parent:
13:379ce1d51b88
bugfix

Who changed what in which revision?

UserRevisionLine numberNew contents of line
lawliet 0:8ccbd963e74d 1 /*
lawliet 0:8ccbd963e74d 2 GPRS.h
lawliet 0:8ccbd963e74d 3 2014 Copyright (c) Seeed Technology Inc. All right reserved.
lawliet 0:8ccbd963e74d 4
lawliet 0:8ccbd963e74d 5 Author:lawliet zou(lawliet.zou@gmail.com)
lawliet 0:8ccbd963e74d 6 2014-2-24
lawliet 0:8ccbd963e74d 7
lawliet 0:8ccbd963e74d 8 This library is free software; you can redistribute it and/or
lawliet 0:8ccbd963e74d 9 modify it under the terms of the GNU Lesser General Public
lawliet 0:8ccbd963e74d 10 License as published by the Free Software Foundation; either
lawliet 0:8ccbd963e74d 11 version 2.1 of the License, or (at your option) any later version.
lawliet 0:8ccbd963e74d 12
lawliet 0:8ccbd963e74d 13 This library is distributed in the hope that it will be useful,
lawliet 0:8ccbd963e74d 14 but WITHOUT ANY WARRANTY; without even the implied warranty of
lawliet 0:8ccbd963e74d 15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
lawliet 0:8ccbd963e74d 16 Lesser General Public License for more details.
lawliet 0:8ccbd963e74d 17
lawliet 0:8ccbd963e74d 18 You should have received a copy of the GNU Lesser General Public
lawliet 0:8ccbd963e74d 19 License along with this library; if not, write to the Free Software
lawliet 0:8ccbd963e74d 20 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
lawliet 0:8ccbd963e74d 21 */
lawliet 0:8ccbd963e74d 22
lawliet 0:8ccbd963e74d 23 #ifndef __GPRS_H__
lawliet 0:8ccbd963e74d 24 #define __GPRS_H__
lawliet 0:8ccbd963e74d 25
lawliet 0:8ccbd963e74d 26 #include "mbed.h"
lawliet 0:8ccbd963e74d 27 #include "modem.h"
lawliet 0:8ccbd963e74d 28
lawliet 0:8ccbd963e74d 29 #define DEFAULT_WAIT_RESP_TIMEOUT 500
Yihui Xiong 13:379ce1d51b88 30 #define MAX_SOCK_NUM 7 // (0~6)
lawliet 0:8ccbd963e74d 31
lawliet 0:8ccbd963e74d 32 enum Protocol {
lawliet 0:8ccbd963e74d 33 CLOSED = 0,
lawliet 0:8ccbd963e74d 34 TCP = 1,
lawliet 0:8ccbd963e74d 35 UDP = 2,
lawliet 0:8ccbd963e74d 36 };
lawliet 0:8ccbd963e74d 37
lawliet 0:8ccbd963e74d 38 class GPRS: public Modem
lawliet 0:8ccbd963e74d 39 {
lawliet 0:8ccbd963e74d 40
lawliet 0:8ccbd963e74d 41 public:
lawliet 0:8ccbd963e74d 42 /** Constructor
lawliet 0:8ccbd963e74d 43 * @param tx mbed pin to use for tx line of Serial interface
lawliet 0:8ccbd963e74d 44 * @param rx mbed pin to use for rx line of Serial interface
lawliet 0:8ccbd963e74d 45 * @param baudRate serial communicate baud rate
lawliet 0:8ccbd963e74d 46 * @param apn name of the gateway for GPRS to connect to the network
lawliet 0:8ccbd963e74d 47 * @param userName apn's username, usually is NULL
lawliet 0:8ccbd963e74d 48 * @param passWord apn's password, usually is NULL
lawliet 0:8ccbd963e74d 49 */
Yihui Xiong 13:379ce1d51b88 50 GPRS(PinName tx, PinName rx, const char* apn, const char* userName = NULL, const char *passWord = NULL);
lawliet 0:8ccbd963e74d 51
lawliet 1:7298a7950f65 52 /** get instance of GPRS class
lawliet 1:7298a7950f65 53 */
lawliet 1:7298a7950f65 54 static GPRS* getInstance() {
lawliet 1:7298a7950f65 55 return inst;
lawliet 1:7298a7950f65 56 };
lawliet 1:7298a7950f65 57
lawliet 0:8ccbd963e74d 58 /** Connect the GPRS module to the network.
lawliet 0:8ccbd963e74d 59 * @return true if connected, false otherwise
lawliet 0:8ccbd963e74d 60 */
lawliet 0:8ccbd963e74d 61 bool join(void);
lawliet 0:8ccbd963e74d 62
lawliet 0:8ccbd963e74d 63 /** Disconnect the GPRS module from the network
lawliet 0:8ccbd963e74d 64 * @returns true if successful
lawliet 0:8ccbd963e74d 65 */
lawliet 0:8ccbd963e74d 66 bool disconnect(void);
lawliet 1:7298a7950f65 67
lawliet 1:7298a7950f65 68 /** Close a tcp connection
lawliet 1:7298a7950f65 69 * @returns true if successful
lawliet 1:7298a7950f65 70 */
lawliet 1:7298a7950f65 71 bool close(int socket);
lawliet 0:8ccbd963e74d 72
lawliet 0:8ccbd963e74d 73 /** Open a tcp/udp connection with the specified host on the specified port
lawliet 0:8ccbd963e74d 74 * @param socket an endpoint of an inter-process communication flow of GPRS module,for SIM900 module, it is in [0,6]
lawliet 0:8ccbd963e74d 75 * @param ptl protocol for socket, TCP/UDP can be choosen
lawliet 0:8ccbd963e74d 76 * @param host host (can be either an ip address or a name. If a name is provided, a dns request will be established)
lawliet 0:8ccbd963e74d 77 * @param port port
Yihui Xiong 13:379ce1d51b88 78 * @param timeout wait time (ms)
lawliet 0:8ccbd963e74d 79 * @returns true if successful
lawliet 0:8ccbd963e74d 80 */
Yihui Xiong 13:379ce1d51b88 81 bool connect(int socket, Protocol ptl, const char * host, int port, int timeout = DEFAULT_TIMEOUT_MS);
lawliet 0:8ccbd963e74d 82
lawliet 0:8ccbd963e74d 83 /** Set the protocol (UDP or TCP)
lawliet 0:8ccbd963e74d 84 * @param socket socket
lawliet 0:8ccbd963e74d 85 * @param p protocol
lawliet 0:8ccbd963e74d 86 * @returns true if successful
lawliet 0:8ccbd963e74d 87 */
lawliet 0:8ccbd963e74d 88 bool setProtocol(int socket, Protocol p);
lawliet 0:8ccbd963e74d 89
lawliet 0:8ccbd963e74d 90 /** Reset the GPRS module
lawliet 0:8ccbd963e74d 91 */
lawliet 0:8ccbd963e74d 92 void reset();
lawliet 0:8ccbd963e74d 93
lawliet 0:8ccbd963e74d 94 /** wait a few time to check if GPRS module is readable or not
lawliet 0:8ccbd963e74d 95 * @param socket socket
lawliet 0:8ccbd963e74d 96 * @param wait_time time of waiting
lawliet 0:8ccbd963e74d 97 */
lawliet 0:8ccbd963e74d 98 int wait_readable(int socket, int wait_time);
lawliet 0:8ccbd963e74d 99
lawliet 0:8ccbd963e74d 100 /** wait a few time to check if GPRS module is writeable or not
lawliet 0:8ccbd963e74d 101 * @param socket socket
lawliet 0:8ccbd963e74d 102 * @param wait_time time of waiting
lawliet 0:8ccbd963e74d 103 */
lawliet 0:8ccbd963e74d 104 int wait_writeable(int socket, int req_size);
lawliet 0:8ccbd963e74d 105
lawliet 0:8ccbd963e74d 106 /** Check if a tcp link is active
lawliet 0:8ccbd963e74d 107 * @returns true if successful
lawliet 0:8ccbd963e74d 108 */
lawliet 0:8ccbd963e74d 109 bool is_connected(int socket);
lawliet 0:8ccbd963e74d 110
lawliet 0:8ccbd963e74d 111 /** send data to socket
lawliet 0:8ccbd963e74d 112 * @param socket socket
lawliet 0:8ccbd963e74d 113 * @param str string to be sent
lawliet 0:8ccbd963e74d 114 * @param len string length
lawliet 0:8ccbd963e74d 115 * @returns return bytes that actually been send
lawliet 0:8ccbd963e74d 116 */
Yihui Xiong 13:379ce1d51b88 117 int sock_send(int socket, const char * str, int len);
lawliet 0:8ccbd963e74d 118
lawliet 0:8ccbd963e74d 119 /** read data from socket
lawliet 0:8ccbd963e74d 120 * @param socket socket
lawliet 0:8ccbd963e74d 121 * @param buf buffer that will store the data read from socket
lawliet 0:8ccbd963e74d 122 * @param len string length need to read from socket
lawliet 0:8ccbd963e74d 123 * @returns bytes that actually read
lawliet 0:8ccbd963e74d 124 */
Yihui Xiong 13:379ce1d51b88 125 int sock_recv(int socket, char* buf, int len);
lawliet 0:8ccbd963e74d 126
lawliet 0:8ccbd963e74d 127 /** convert the host to ip
lawliet 0:8ccbd963e74d 128 * @param host host ip string, ex. 10.11.12.13
lawliet 0:8ccbd963e74d 129 * @param ip long int ip address, ex. 0x11223344
lawliet 0:8ccbd963e74d 130 * @returns true if successful
lawliet 0:8ccbd963e74d 131 */
lawliet 0:8ccbd963e74d 132 bool gethostbyname(const char* host, uint32_t* ip);
lawliet 0:8ccbd963e74d 133
lawliet 0:8ccbd963e74d 134 int new_socket();
lawliet 0:8ccbd963e74d 135 uint16_t new_port();
Yihui Xiong 13:379ce1d51b88 136 uint32_t _ip;
Yihui Xiong 13:379ce1d51b88 137
lawliet 0:8ccbd963e74d 138
lawliet 0:8ccbd963e74d 139 protected:
lawliet 0:8ccbd963e74d 140 static GPRS* inst;
lawliet 0:8ccbd963e74d 141 int socketID;
lawliet 0:8ccbd963e74d 142 const char* _apn;
lawliet 0:8ccbd963e74d 143 const char* _userName;
Yihui Xiong 13:379ce1d51b88 144 const char* _passWord;
Yihui Xiong 13:379ce1d51b88 145 bool connected;
Yihui Xiong 13:379ce1d51b88 146 uint8_t recv_buf[32]; // for
Yihui Xiong 13:379ce1d51b88 147 uint8_t recv_bytes;
lawliet 0:8ccbd963e74d 148 };
lawliet 0:8ccbd963e74d 149
lawliet 0:8ccbd963e74d 150 #endif