rhr
gprs.h@7:1278e9dde24e, 2014-04-28 (annotated)
- Committer:
- lawliet
- Date:
- Mon Apr 28 12:32:12 2014 +0000
- Revision:
- 7:1278e9dde24e
- Parent:
- 6:e508f9ee7922
update loopHandle function
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
lawliet | 0:a5ae94727346 | 1 | /* |
lawliet | 3:48ee24a4b0f3 | 2 | gprs.h |
lawliet | 0:a5ae94727346 | 3 | 2013 Copyright (c) Seeed Technology Inc. All right reserved. |
lawliet | 0:a5ae94727346 | 4 | |
lawliet | 0:a5ae94727346 | 5 | Author:lawliet.zou@gmail.com |
lawliet | 0:a5ae94727346 | 6 | 2013-11-14 |
lawliet | 0:a5ae94727346 | 7 | |
lawliet | 0:a5ae94727346 | 8 | This library is free software; you can redistribute it and/or |
lawliet | 0:a5ae94727346 | 9 | modify it under the terms of the GNU Lesser General Public |
lawliet | 0:a5ae94727346 | 10 | License as published by the Free Software Foundation; either |
lawliet | 0:a5ae94727346 | 11 | version 2.1 of the License, or (at your option) any later version. |
lawliet | 0:a5ae94727346 | 12 | |
lawliet | 0:a5ae94727346 | 13 | This library is distributed in the hope that it will be useful, |
lawliet | 0:a5ae94727346 | 14 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
lawliet | 0:a5ae94727346 | 15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
lawliet | 0:a5ae94727346 | 16 | Lesser General Public License for more details. |
lawliet | 0:a5ae94727346 | 17 | |
lawliet | 0:a5ae94727346 | 18 | You should have received a copy of the GNU Lesser General Public |
lawliet | 0:a5ae94727346 | 19 | License along with this library; if not, write to the Free Software |
lawliet | 0:a5ae94727346 | 20 | Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
lawliet | 0:a5ae94727346 | 21 | */ |
lawliet | 0:a5ae94727346 | 22 | |
lawliet | 0:a5ae94727346 | 23 | #ifndef __GPRS_H__ |
lawliet | 0:a5ae94727346 | 24 | #define __GPRS_H__ |
lawliet | 0:a5ae94727346 | 25 | |
lawliet | 3:48ee24a4b0f3 | 26 | #include <stdio.h> |
lawliet | 0:a5ae94727346 | 27 | #include "mbed.h" |
lawliet | 3:48ee24a4b0f3 | 28 | |
lawliet | 7:1278e9dde24e | 29 | #define DEFAULT_TIMEOUT 5 |
lawliet | 7:1278e9dde24e | 30 | #define SMS_MAX_LENGTH 16 |
lawliet | 7:1278e9dde24e | 31 | |
lawliet | 4:63a2619b423b | 32 | |
lawliet | 4:63a2619b423b | 33 | enum GPRS_MESSAGE { |
lawliet | 4:63a2619b423b | 34 | MESSAGE_RING = 0, |
lawliet | 4:63a2619b423b | 35 | MESSAGE_SMS = 1, |
lawliet | 4:63a2619b423b | 36 | MESSAGE_ERROR |
lawliet | 4:63a2619b423b | 37 | }; |
lawliet | 4:63a2619b423b | 38 | |
lawliet | 3:48ee24a4b0f3 | 39 | |
lawliet | 2:16985da3a446 | 40 | /** GPRS class. |
lawliet | 2:16985da3a446 | 41 | * Used for mobile communication. attention that GPRS module communicate with MCU in serial protocol |
lawliet | 2:16985da3a446 | 42 | */ |
lawliet | 2:16985da3a446 | 43 | class GPRS |
lawliet | 1:642a8dbe076c | 44 | { |
lawliet | 1:642a8dbe076c | 45 | public: |
lawliet | 2:16985da3a446 | 46 | /** Create GPRS instance |
lawliet | 2:16985da3a446 | 47 | * @param tx uart transmit pin to communicate with GPRS module |
lawliet | 2:16985da3a446 | 48 | * @param rx uart receive pin to communicate with GPRS module |
lawliet | 2:16985da3a446 | 49 | * @param baudRate baud rate of uart communication |
lawliet | 2:16985da3a446 | 50 | * @param number default phone number during mobile communication |
lawliet | 2:16985da3a446 | 51 | */ |
lawliet | 2:16985da3a446 | 52 | GPRS(PinName tx, PinName rx, int baudRate,char *number) : gprsSerial(tx, rx) { |
lawliet | 7:1278e9dde24e | 53 | //gprsSerial.baud(baudRate); |
lawliet | 2:16985da3a446 | 54 | phoneNumber = number; |
lawliet | 1:642a8dbe076c | 55 | }; |
lawliet | 7:1278e9dde24e | 56 | |
lawliet | 7:1278e9dde24e | 57 | int powerCheck(void); |
lawliet | 2:16985da3a446 | 58 | /** init GPRS module including SIM card check & signal strength & network check |
lawliet | 2:16985da3a446 | 59 | * @returns |
lawliet | 2:16985da3a446 | 60 | * 0 on success |
lawliet | 2:16985da3a446 | 61 | * -1 on error |
lawliet | 2:16985da3a446 | 62 | */ |
lawliet | 1:642a8dbe076c | 63 | int init(void); |
lawliet | 2:16985da3a446 | 64 | |
lawliet | 4:63a2619b423b | 65 | /** check SIM card' Status |
lawliet | 4:63a2619b423b | 66 | * @returns |
lawliet | 4:63a2619b423b | 67 | * 0 on success |
lawliet | 4:63a2619b423b | 68 | * -1 on error |
lawliet | 4:63a2619b423b | 69 | */ |
lawliet | 4:63a2619b423b | 70 | int checkSIMStatus(void); |
lawliet | 4:63a2619b423b | 71 | |
lawliet | 4:63a2619b423b | 72 | /** check signal strength |
lawliet | 4:63a2619b423b | 73 | * @returns |
lawliet | 4:63a2619b423b | 74 | * signal strength in number(ex 3,4,5,6,7,8...) on success |
lawliet | 4:63a2619b423b | 75 | * -1 on error |
lawliet | 4:63a2619b423b | 76 | */ |
lawliet | 4:63a2619b423b | 77 | int checkSignalStrength(void); |
lawliet | 4:63a2619b423b | 78 | |
lawliet | 4:63a2619b423b | 79 | /** set SMS format and processing mode |
lawliet | 4:63a2619b423b | 80 | * @returns |
lawliet | 4:63a2619b423b | 81 | * 0 on success |
lawliet | 4:63a2619b423b | 82 | * -1 on error |
lawliet | 4:63a2619b423b | 83 | */ |
lawliet | 4:63a2619b423b | 84 | int settingSMS(void); |
lawliet | 4:63a2619b423b | 85 | |
lawliet | 4:63a2619b423b | 86 | /** send text SMS |
lawliet | 4:63a2619b423b | 87 | * @param *number phone number which SMS will be send to |
lawliet | 4:63a2619b423b | 88 | * @param *data message that will be send to |
lawliet | 4:63a2619b423b | 89 | * @returns |
lawliet | 4:63a2619b423b | 90 | * 0 on success |
lawliet | 4:63a2619b423b | 91 | * -1 on error |
lawliet | 4:63a2619b423b | 92 | */ |
lawliet | 4:63a2619b423b | 93 | int sendSMS(char *number, char *data); |
lawliet | 4:63a2619b423b | 94 | |
lawliet | 4:63a2619b423b | 95 | /** read SMS by index |
lawliet | 4:63a2619b423b | 96 | * @param *message buffer used to get SMS message |
lawliet | 4:63a2619b423b | 97 | * @param index which SMS message to read |
lawliet | 4:63a2619b423b | 98 | * @returns |
lawliet | 4:63a2619b423b | 99 | * 0 on success |
lawliet | 4:63a2619b423b | 100 | * -1 on error |
lawliet | 4:63a2619b423b | 101 | */ |
lawliet | 4:63a2619b423b | 102 | int readSMS(char *message, int index); |
lawliet | 4:63a2619b423b | 103 | |
lawliet | 4:63a2619b423b | 104 | /** delete SMS message on SIM card |
lawliet | 4:63a2619b423b | 105 | * @param *index the index number which SMS message will be delete |
lawliet | 4:63a2619b423b | 106 | * @returns |
lawliet | 4:63a2619b423b | 107 | * 0 on success |
lawliet | 4:63a2619b423b | 108 | * -1 on error |
lawliet | 4:63a2619b423b | 109 | */ |
lawliet | 4:63a2619b423b | 110 | int deleteSMS(int index); |
lawliet | 4:63a2619b423b | 111 | |
lawliet | 4:63a2619b423b | 112 | /** read SMS when coming a message,it will be store in messageBuffer. |
lawliet | 4:63a2619b423b | 113 | * @param message buffer used to get SMS message |
lawliet | 4:63a2619b423b | 114 | */ |
lawliet | 4:63a2619b423b | 115 | int getSMS(char* message); |
lawliet | 4:63a2619b423b | 116 | |
lawliet | 4:63a2619b423b | 117 | /** call someone |
lawliet | 4:63a2619b423b | 118 | * @param *number the phone number which you want to call |
lawliet | 4:63a2619b423b | 119 | * @returns |
lawliet | 4:63a2619b423b | 120 | * 0 on success |
lawliet | 4:63a2619b423b | 121 | * -1 on error |
lawliet | 4:63a2619b423b | 122 | */ |
lawliet | 4:63a2619b423b | 123 | int callUp(char *number); |
lawliet | 4:63a2619b423b | 124 | |
lawliet | 4:63a2619b423b | 125 | /** auto answer if coming a call |
lawliet | 4:63a2619b423b | 126 | * @returns |
lawliet | 4:63a2619b423b | 127 | * 0 on success |
lawliet | 4:63a2619b423b | 128 | * -1 on error |
lawliet | 4:63a2619b423b | 129 | */ |
lawliet | 4:63a2619b423b | 130 | int answer(void); |
lawliet | 4:63a2619b423b | 131 | |
lawliet | 4:63a2619b423b | 132 | /** a loop to wait for some event. if a call comes in, it will auto answer it and if a SMS message comes in, it will read the message |
lawliet | 4:63a2619b423b | 133 | * @param *check whether to check phone number when get event |
lawliet | 4:63a2619b423b | 134 | * @returns |
lawliet | 4:63a2619b423b | 135 | * 0 on success |
lawliet | 4:63a2619b423b | 136 | * -1 on error |
lawliet | 4:63a2619b423b | 137 | */ |
lawliet | 4:63a2619b423b | 138 | int loopHandle(void); |
lawliet | 4:63a2619b423b | 139 | |
lawliet | 5:ac2342f162fa | 140 | /** gprs network init |
lawliet | 5:ac2342f162fa | 141 | * @param *apn Access Point Name to connect network |
lawliet | 5:ac2342f162fa | 142 | * @param *userName general is empty |
lawliet | 5:ac2342f162fa | 143 | * @param *passWord general is empty |
lawliet | 5:ac2342f162fa | 144 | */ |
lawliet | 5:ac2342f162fa | 145 | int networkInit(char* apn, char* userName = NULL, char* passWord = NULL); |
lawliet | 4:63a2619b423b | 146 | /** build TCP connect |
lawliet | 4:63a2619b423b | 147 | * @param *ip ip address which will connect to |
lawliet | 4:63a2619b423b | 148 | * @param *port TCP server' port number |
lawliet | 4:63a2619b423b | 149 | * @returns |
lawliet | 4:63a2619b423b | 150 | * 0 on success |
lawliet | 4:63a2619b423b | 151 | * -1 on error |
lawliet | 4:63a2619b423b | 152 | */ |
lawliet | 4:63a2619b423b | 153 | int connectTCP(char *ip, char *port); |
lawliet | 4:63a2619b423b | 154 | |
lawliet | 4:63a2619b423b | 155 | /** send data to TCP server |
lawliet | 4:63a2619b423b | 156 | * @param *data data that will be send to TCP server |
lawliet | 4:63a2619b423b | 157 | * @returns |
lawliet | 4:63a2619b423b | 158 | * 0 on success |
lawliet | 4:63a2619b423b | 159 | * -1 on error |
lawliet | 4:63a2619b423b | 160 | */ |
lawliet | 4:63a2619b423b | 161 | int sendTCPData(char *data); |
lawliet | 4:63a2619b423b | 162 | |
lawliet | 4:63a2619b423b | 163 | /** close TCP connection |
lawliet | 4:63a2619b423b | 164 | * @returns |
lawliet | 4:63a2619b423b | 165 | * 0 on success |
lawliet | 4:63a2619b423b | 166 | * -1 on error |
lawliet | 4:63a2619b423b | 167 | */ |
lawliet | 4:63a2619b423b | 168 | int closeTCP(void); |
lawliet | 4:63a2619b423b | 169 | |
lawliet | 4:63a2619b423b | 170 | /** close TCP service |
lawliet | 4:63a2619b423b | 171 | * @returns |
lawliet | 4:63a2619b423b | 172 | * 0 on success |
lawliet | 4:63a2619b423b | 173 | * -1 on error |
lawliet | 4:63a2619b423b | 174 | */ |
lawliet | 4:63a2619b423b | 175 | int shutTCP(void); |
lawliet | 4:63a2619b423b | 176 | |
lawliet | 6:e508f9ee7922 | 177 | Serial gprsSerial; |
lawliet | 4:63a2619b423b | 178 | //USBSerial pc; |
lawliet | 4:63a2619b423b | 179 | private: |
lawliet | 2:16985da3a446 | 180 | /** read from GPRS module and save to buffer array |
lawliet | 2:16985da3a446 | 181 | * @param *buffer buffer array to save what read from GPRS module |
lawliet | 2:16985da3a446 | 182 | * @param *count the maximal bytes number read from GPRS module |
lawliet | 2:16985da3a446 | 183 | * @returns |
lawliet | 2:16985da3a446 | 184 | * 0 on success |
lawliet | 2:16985da3a446 | 185 | * -1 on error |
lawliet | 2:16985da3a446 | 186 | */ |
lawliet | 1:642a8dbe076c | 187 | int readBuffer(char *buffer,int count); |
lawliet | 2:16985da3a446 | 188 | |
lawliet | 2:16985da3a446 | 189 | /** send AT command to GPRS module |
lawliet | 2:16985da3a446 | 190 | * @param *cmd command array which will be send to GPRS module |
lawliet | 2:16985da3a446 | 191 | */ |
lawliet | 1:642a8dbe076c | 192 | void sendCmd(char *cmd); |
lawliet | 2:16985da3a446 | 193 | |
lawliet | 2:16985da3a446 | 194 | /** check GPRS module response before timeout |
lawliet | 2:16985da3a446 | 195 | * @param *resp correct response which GPRS module will return |
lawliet | 2:16985da3a446 | 196 | * @param *timeout waiting seconds till timeout |
lawliet | 2:16985da3a446 | 197 | * @returns |
lawliet | 2:16985da3a446 | 198 | * 0 on success |
lawliet | 2:16985da3a446 | 199 | * -1 on error |
lawliet | 2:16985da3a446 | 200 | */ |
lawliet | 1:642a8dbe076c | 201 | int waitForResp(char *resp, int timeout); |
lawliet | 2:16985da3a446 | 202 | |
lawliet | 2:16985da3a446 | 203 | /** send AT command to GPRS module and wait for correct response |
lawliet | 2:16985da3a446 | 204 | * @param *cmd AT command which will be send to GPRS module |
lawliet | 2:16985da3a446 | 205 | * @param *resp correct response which GPRS module will return |
lawliet | 2:16985da3a446 | 206 | * @param *timeout waiting seconds till timeout |
lawliet | 2:16985da3a446 | 207 | * @returns |
lawliet | 2:16985da3a446 | 208 | * 0 on success |
lawliet | 2:16985da3a446 | 209 | * -1 on error |
lawliet | 2:16985da3a446 | 210 | */ |
lawliet | 1:642a8dbe076c | 211 | int sendCmdAndWaitForResp(char *cmd, char *resp, int timeout); |
lawliet | 2:16985da3a446 | 212 | |
lawliet | 1:642a8dbe076c | 213 | Timer timeCnt; |
lawliet | 1:642a8dbe076c | 214 | char *phoneNumber; |
lawliet | 4:63a2619b423b | 215 | char messageBuffer[SMS_MAX_LENGTH]; |
lawliet | 0:a5ae94727346 | 216 | }; |
lawliet | 0:a5ae94727346 | 217 | |
lawliet | 0:a5ae94727346 | 218 | #endif |
lawliet | 4:63a2619b423b | 219 |