Temp fork

Dependents:   Seeed_GPRS_Shield

Fork of GPRSInterface by wei zou

Committer:
screamer
Date:
Fri Jul 25 10:42:42 2014 +0000
Revision:
9:5afc5b4e2df8
Parent:
1:7298a7950f65
Fix documentation

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
lawliet 0:8ccbd963e74d 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
screamer 9:5afc5b4e2df8 38 /** The GPRS class
screamer 9:5afc5b4e2df8 39 *
screamer 9:5afc5b4e2df8 40 */
lawliet 0:8ccbd963e74d 41 class GPRS: public Modem
lawliet 0:8ccbd963e74d 42 {
lawliet 0:8ccbd963e74d 43
lawliet 0:8ccbd963e74d 44 public:
lawliet 0:8ccbd963e74d 45 /** Constructor
lawliet 0:8ccbd963e74d 46 * @param tx mbed pin to use for tx line of Serial interface
lawliet 0:8ccbd963e74d 47 * @param rx mbed pin to use for rx line of Serial interface
lawliet 0:8ccbd963e74d 48 * @param baudRate serial communicate baud rate
lawliet 0:8ccbd963e74d 49 * @param apn name of the gateway for GPRS to connect to the network
lawliet 0:8ccbd963e74d 50 * @param userName apn's username, usually is NULL
lawliet 0:8ccbd963e74d 51 * @param passWord apn's password, usually is NULL
lawliet 0:8ccbd963e74d 52 */
lawliet 0:8ccbd963e74d 53 GPRS(PinName tx, PinName rx, int baudRate, const char* apn, const char* userName = NULL, const char *passWord = NULL);
lawliet 0:8ccbd963e74d 54
screamer 9:5afc5b4e2df8 55 /** Get instance of GPRS class
lawliet 1:7298a7950f65 56 */
lawliet 1:7298a7950f65 57 static GPRS* getInstance() {
lawliet 1:7298a7950f65 58 return inst;
lawliet 1:7298a7950f65 59 };
lawliet 1:7298a7950f65 60
lawliet 0:8ccbd963e74d 61 /** Connect the GPRS module to the network.
lawliet 0:8ccbd963e74d 62 * @return true if connected, false otherwise
lawliet 0:8ccbd963e74d 63 */
lawliet 0:8ccbd963e74d 64 bool join(void);
lawliet 0:8ccbd963e74d 65
lawliet 0:8ccbd963e74d 66 /** Disconnect the GPRS module from the network
lawliet 0:8ccbd963e74d 67 * @returns true if successful
lawliet 0:8ccbd963e74d 68 */
lawliet 0:8ccbd963e74d 69 bool disconnect(void);
lawliet 1:7298a7950f65 70
lawliet 1:7298a7950f65 71 /** Close a tcp connection
lawliet 1:7298a7950f65 72 * @returns true if successful
lawliet 1:7298a7950f65 73 */
lawliet 1:7298a7950f65 74 bool close(int socket);
lawliet 0:8ccbd963e74d 75
lawliet 0:8ccbd963e74d 76 /** Open a tcp/udp connection with the specified host on the specified port
lawliet 0:8ccbd963e74d 77 * @param socket an endpoint of an inter-process communication flow of GPRS module,for SIM900 module, it is in [0,6]
lawliet 0:8ccbd963e74d 78 * @param ptl protocol for socket, TCP/UDP can be choosen
lawliet 0:8ccbd963e74d 79 * @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 80 * @param port port
lawliet 0:8ccbd963e74d 81 * @param timeout wait seconds till connected
lawliet 0:8ccbd963e74d 82 * @returns true if successful
lawliet 0:8ccbd963e74d 83 */
lawliet 0:8ccbd963e74d 84 bool connect(int socket, Protocol ptl, const char * host, int port, int timeout = DEFAULT_TIMEOUT);
lawliet 0:8ccbd963e74d 85
lawliet 0:8ccbd963e74d 86 /** Set the protocol (UDP or TCP)
lawliet 0:8ccbd963e74d 87 * @param socket socket
lawliet 0:8ccbd963e74d 88 * @param p protocol
lawliet 0:8ccbd963e74d 89 * @returns true if successful
lawliet 0:8ccbd963e74d 90 */
lawliet 0:8ccbd963e74d 91 bool setProtocol(int socket, Protocol p);
lawliet 0:8ccbd963e74d 92
lawliet 0:8ccbd963e74d 93 /** Reset the GPRS module
lawliet 0:8ccbd963e74d 94 */
lawliet 0:8ccbd963e74d 95 void reset();
lawliet 0:8ccbd963e74d 96
screamer 9:5afc5b4e2df8 97 /** Check if GPRS module is readable or not
lawliet 0:8ccbd963e74d 98 * @returns true if readable
lawliet 0:8ccbd963e74d 99 */
lawliet 0:8ccbd963e74d 100 bool readable(void);
lawliet 0:8ccbd963e74d 101
screamer 9:5afc5b4e2df8 102 /** Wait a few time to check if GPRS module is readable or not
lawliet 0:8ccbd963e74d 103 * @param socket socket
lawliet 0:8ccbd963e74d 104 * @param wait_time time of waiting
lawliet 0:8ccbd963e74d 105 */
lawliet 0:8ccbd963e74d 106 int wait_readable(int socket, int wait_time);
lawliet 0:8ccbd963e74d 107
screamer 9:5afc5b4e2df8 108 /** Wait a few time to check if GPRS module is writeable or not
lawliet 0:8ccbd963e74d 109 * @param socket socket
lawliet 0:8ccbd963e74d 110 * @param wait_time time of waiting
lawliet 0:8ccbd963e74d 111 */
lawliet 0:8ccbd963e74d 112 int wait_writeable(int socket, int req_size);
lawliet 0:8ccbd963e74d 113
lawliet 0:8ccbd963e74d 114 /** Check if a tcp link is active
lawliet 0:8ccbd963e74d 115 * @returns true if successful
lawliet 0:8ccbd963e74d 116 */
lawliet 0:8ccbd963e74d 117 bool is_connected(int socket);
lawliet 0:8ccbd963e74d 118
screamer 9:5afc5b4e2df8 119 /** Send data to socket
lawliet 0:8ccbd963e74d 120 * @param socket socket
lawliet 0:8ccbd963e74d 121 * @param str string to be sent
lawliet 0:8ccbd963e74d 122 * @param len string length
lawliet 0:8ccbd963e74d 123 * @returns return bytes that actually been send
lawliet 0:8ccbd963e74d 124 */
lawliet 0:8ccbd963e74d 125 int send(int socket, const char * str, int len);
lawliet 0:8ccbd963e74d 126
screamer 9:5afc5b4e2df8 127 /** Read data from socket
lawliet 0:8ccbd963e74d 128 * @param socket socket
lawliet 0:8ccbd963e74d 129 * @param buf buffer that will store the data read from socket
lawliet 0:8ccbd963e74d 130 * @param len string length need to read from socket
lawliet 0:8ccbd963e74d 131 * @returns bytes that actually read
lawliet 0:8ccbd963e74d 132 */
lawliet 0:8ccbd963e74d 133 int recv(int socket, char* buf, int len);
lawliet 0:8ccbd963e74d 134
screamer 9:5afc5b4e2df8 135 /** Convert the host to ip
lawliet 0:8ccbd963e74d 136 * @param host host ip string, ex. 10.11.12.13
lawliet 0:8ccbd963e74d 137 * @param ip long int ip address, ex. 0x11223344
lawliet 0:8ccbd963e74d 138 * @returns true if successful
lawliet 0:8ccbd963e74d 139 */
lawliet 0:8ccbd963e74d 140 bool gethostbyname(const char* host, uint32_t* ip);
lawliet 0:8ccbd963e74d 141
lawliet 0:8ccbd963e74d 142 int new_socket();
lawliet 0:8ccbd963e74d 143 uint16_t new_port();
lawliet 0:8ccbd963e74d 144 uint32_t _ip;
lawliet 0:8ccbd963e74d 145
lawliet 0:8ccbd963e74d 146 protected:
lawliet 0:8ccbd963e74d 147
lawliet 0:8ccbd963e74d 148 bool preInit();
lawliet 0:8ccbd963e74d 149 bool checkSIMStatus(void);
lawliet 0:8ccbd963e74d 150 uint32_t str_to_ip(const char* str);
lawliet 0:8ccbd963e74d 151 static GPRS* inst;
lawliet 0:8ccbd963e74d 152 int socketID;
lawliet 0:8ccbd963e74d 153 const char* _apn;
lawliet 0:8ccbd963e74d 154 const char* _userName;
lawliet 0:8ccbd963e74d 155 const char* _passWord;
lawliet 0:8ccbd963e74d 156 };
lawliet 0:8ccbd963e74d 157
lawliet 0:8ccbd963e74d 158 #endif