Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Fork of GPRSInterface by
GPRS/GPRS.h@10:8c55dfcc9a7c, 2015-09-25 (annotated)
- Committer:
- noahmilam
- Date:
- Fri Sep 25 04:10:56 2015 +0000
- Revision:
- 10:8c55dfcc9a7c
- Parent:
- 9:5afc5b4e2df8
ff
Who changed what in which revision?
User | Revision | Line number | New 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); |
noahmilam | 10:8c55dfcc9a7c | 65 | // added by Noah Milam |
noahmilam | 10:8c55dfcc9a7c | 66 | void start_server(); |
noahmilam | 10:8c55dfcc9a7c | 67 | void send_SMS(char*); |
noahmilam | 10:8c55dfcc9a7c | 68 | char* read_SMS(); |
noahmilam | 10:8c55dfcc9a7c | 69 | void listen_server(); |
noahmilam | 10:8c55dfcc9a7c | 70 | |
noahmilam | 10:8c55dfcc9a7c | 71 | // end of what Noah Milam added |
lawliet | 0:8ccbd963e74d | 72 | /** Disconnect the GPRS module from the network |
lawliet | 0:8ccbd963e74d | 73 | * @returns true if successful |
lawliet | 0:8ccbd963e74d | 74 | */ |
lawliet | 0:8ccbd963e74d | 75 | bool disconnect(void); |
lawliet | 1:7298a7950f65 | 76 | |
lawliet | 1:7298a7950f65 | 77 | /** Close a tcp connection |
lawliet | 1:7298a7950f65 | 78 | * @returns true if successful |
lawliet | 1:7298a7950f65 | 79 | */ |
lawliet | 1:7298a7950f65 | 80 | bool close(int socket); |
lawliet | 0:8ccbd963e74d | 81 | |
lawliet | 0:8ccbd963e74d | 82 | /** Open a tcp/udp connection with the specified host on the specified port |
lawliet | 0:8ccbd963e74d | 83 | * @param socket an endpoint of an inter-process communication flow of GPRS module,for SIM900 module, it is in [0,6] |
lawliet | 0:8ccbd963e74d | 84 | * @param ptl protocol for socket, TCP/UDP can be choosen |
lawliet | 0:8ccbd963e74d | 85 | * @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 | 86 | * @param port port |
lawliet | 0:8ccbd963e74d | 87 | * @param timeout wait seconds till connected |
lawliet | 0:8ccbd963e74d | 88 | * @returns true if successful |
lawliet | 0:8ccbd963e74d | 89 | */ |
lawliet | 0:8ccbd963e74d | 90 | bool connect(int socket, Protocol ptl, const char * host, int port, int timeout = DEFAULT_TIMEOUT); |
lawliet | 0:8ccbd963e74d | 91 | |
lawliet | 0:8ccbd963e74d | 92 | /** Set the protocol (UDP or TCP) |
lawliet | 0:8ccbd963e74d | 93 | * @param socket socket |
lawliet | 0:8ccbd963e74d | 94 | * @param p protocol |
lawliet | 0:8ccbd963e74d | 95 | * @returns true if successful |
lawliet | 0:8ccbd963e74d | 96 | */ |
lawliet | 0:8ccbd963e74d | 97 | bool setProtocol(int socket, Protocol p); |
lawliet | 0:8ccbd963e74d | 98 | |
lawliet | 0:8ccbd963e74d | 99 | /** Reset the GPRS module |
lawliet | 0:8ccbd963e74d | 100 | */ |
lawliet | 0:8ccbd963e74d | 101 | void reset(); |
lawliet | 0:8ccbd963e74d | 102 | |
screamer | 9:5afc5b4e2df8 | 103 | /** Check if GPRS module is readable or not |
lawliet | 0:8ccbd963e74d | 104 | * @returns true if readable |
lawliet | 0:8ccbd963e74d | 105 | */ |
lawliet | 0:8ccbd963e74d | 106 | bool readable(void); |
lawliet | 0:8ccbd963e74d | 107 | |
screamer | 9:5afc5b4e2df8 | 108 | /** Wait a few time to check if GPRS module is readable 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_readable(int socket, int wait_time); |
lawliet | 0:8ccbd963e74d | 113 | |
screamer | 9:5afc5b4e2df8 | 114 | /** Wait a few time to check if GPRS module is writeable or not |
lawliet | 0:8ccbd963e74d | 115 | * @param socket socket |
lawliet | 0:8ccbd963e74d | 116 | * @param wait_time time of waiting |
lawliet | 0:8ccbd963e74d | 117 | */ |
lawliet | 0:8ccbd963e74d | 118 | int wait_writeable(int socket, int req_size); |
lawliet | 0:8ccbd963e74d | 119 | |
lawliet | 0:8ccbd963e74d | 120 | /** Check if a tcp link is active |
lawliet | 0:8ccbd963e74d | 121 | * @returns true if successful |
lawliet | 0:8ccbd963e74d | 122 | */ |
lawliet | 0:8ccbd963e74d | 123 | bool is_connected(int socket); |
lawliet | 0:8ccbd963e74d | 124 | |
screamer | 9:5afc5b4e2df8 | 125 | /** Send data to socket |
lawliet | 0:8ccbd963e74d | 126 | * @param socket socket |
lawliet | 0:8ccbd963e74d | 127 | * @param str string to be sent |
lawliet | 0:8ccbd963e74d | 128 | * @param len string length |
lawliet | 0:8ccbd963e74d | 129 | * @returns return bytes that actually been send |
lawliet | 0:8ccbd963e74d | 130 | */ |
lawliet | 0:8ccbd963e74d | 131 | int send(int socket, const char * str, int len); |
lawliet | 0:8ccbd963e74d | 132 | |
screamer | 9:5afc5b4e2df8 | 133 | /** Read data from socket |
lawliet | 0:8ccbd963e74d | 134 | * @param socket socket |
lawliet | 0:8ccbd963e74d | 135 | * @param buf buffer that will store the data read from socket |
lawliet | 0:8ccbd963e74d | 136 | * @param len string length need to read from socket |
lawliet | 0:8ccbd963e74d | 137 | * @returns bytes that actually read |
lawliet | 0:8ccbd963e74d | 138 | */ |
lawliet | 0:8ccbd963e74d | 139 | int recv(int socket, char* buf, int len); |
lawliet | 0:8ccbd963e74d | 140 | |
screamer | 9:5afc5b4e2df8 | 141 | /** Convert the host to ip |
lawliet | 0:8ccbd963e74d | 142 | * @param host host ip string, ex. 10.11.12.13 |
lawliet | 0:8ccbd963e74d | 143 | * @param ip long int ip address, ex. 0x11223344 |
lawliet | 0:8ccbd963e74d | 144 | * @returns true if successful |
lawliet | 0:8ccbd963e74d | 145 | */ |
lawliet | 0:8ccbd963e74d | 146 | bool gethostbyname(const char* host, uint32_t* ip); |
lawliet | 0:8ccbd963e74d | 147 | |
lawliet | 0:8ccbd963e74d | 148 | int new_socket(); |
lawliet | 0:8ccbd963e74d | 149 | uint16_t new_port(); |
lawliet | 0:8ccbd963e74d | 150 | uint32_t _ip; |
noahmilam | 10:8c55dfcc9a7c | 151 | bool preInit(); |
lawliet | 0:8ccbd963e74d | 152 | protected: |
lawliet | 0:8ccbd963e74d | 153 | |
noahmilam | 10:8c55dfcc9a7c | 154 | |
lawliet | 0:8ccbd963e74d | 155 | bool checkSIMStatus(void); |
lawliet | 0:8ccbd963e74d | 156 | uint32_t str_to_ip(const char* str); |
lawliet | 0:8ccbd963e74d | 157 | static GPRS* inst; |
lawliet | 0:8ccbd963e74d | 158 | int socketID; |
lawliet | 0:8ccbd963e74d | 159 | const char* _apn; |
lawliet | 0:8ccbd963e74d | 160 | const char* _userName; |
lawliet | 0:8ccbd963e74d | 161 | const char* _passWord; |
lawliet | 0:8ccbd963e74d | 162 | }; |
lawliet | 0:8ccbd963e74d | 163 | |
lawliet | 0:8ccbd963e74d | 164 | #endif |