ok
GPRS.h@9:2f5445178940, 2014-05-13 (annotated)
- Committer:
- screamer
- Date:
- Tue May 13 14:32:33 2014 +0000
- Revision:
- 9:2f5445178940
- Parent:
- gprs.h@7:1278e9dde24e
- Parent:
- gprs.h@8:1ee8ba65061a
Merge
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 |
screamer | 8:1ee8ba65061a | 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 | |
screamer | 8:1ee8ba65061a | 65 | /** Check SIM card' Status |
lawliet | 4:63a2619b423b | 66 | * @returns |
screamer | 8:1ee8ba65061a | 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 | |
screamer | 8:1ee8ba65061a | 72 | /** Check signal strength |
lawliet | 4:63a2619b423b | 73 | * @returns |
screamer | 8:1ee8ba65061a | 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 | |
screamer | 8:1ee8ba65061a | 79 | /** Set SMS format and processing mode |
lawliet | 4:63a2619b423b | 80 | * @returns |
screamer | 8:1ee8ba65061a | 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 | |
screamer | 8:1ee8ba65061a | 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 |
screamer | 8:1ee8ba65061a | 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 | |
screamer | 8:1ee8ba65061a | 95 | /** Read SMS by index |
lawliet | 4:63a2619b423b | 96 | * @param *message buffer used to get SMS message |
screamer | 8:1ee8ba65061a | 97 | * @param index which SMS message to read |
lawliet | 4:63a2619b423b | 98 | * @returns |
screamer | 8:1ee8ba65061a | 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 | |
screamer | 8:1ee8ba65061a | 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 |
screamer | 8:1ee8ba65061a | 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 | |
screamer | 8:1ee8ba65061a | 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 | |
screamer | 8:1ee8ba65061a | 117 | /** Call someone |
lawliet | 4:63a2619b423b | 118 | * @param *number the phone number which you want to call |
lawliet | 4:63a2619b423b | 119 | * @returns |
screamer | 8:1ee8ba65061a | 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 | |
screamer | 8:1ee8ba65061a | 125 | /** Auto answer if coming a call |
lawliet | 4:63a2619b423b | 126 | * @returns |
screamer | 8:1ee8ba65061a | 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 | |
screamer | 8:1ee8ba65061a | 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 |
screamer | 8:1ee8ba65061a | 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 | |
screamer | 8:1ee8ba65061a | 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 | */ |
screamer | 8:1ee8ba65061a | 145 | |
lawliet | 5:ac2342f162fa | 146 | int networkInit(char* apn, char* userName = NULL, char* passWord = NULL); |
screamer | 8:1ee8ba65061a | 147 | /** Build TCP connect |
lawliet | 4:63a2619b423b | 148 | * @param *ip ip address which will connect to |
lawliet | 4:63a2619b423b | 149 | * @param *port TCP server' port number |
lawliet | 4:63a2619b423b | 150 | * @returns |
screamer | 8:1ee8ba65061a | 151 | * 0 on success, |
lawliet | 4:63a2619b423b | 152 | * -1 on error |
lawliet | 4:63a2619b423b | 153 | */ |
lawliet | 4:63a2619b423b | 154 | int connectTCP(char *ip, char *port); |
lawliet | 4:63a2619b423b | 155 | |
screamer | 8:1ee8ba65061a | 156 | /** Send data to TCP server |
lawliet | 4:63a2619b423b | 157 | * @param *data data that will be send to TCP server |
lawliet | 4:63a2619b423b | 158 | * @returns |
screamer | 8:1ee8ba65061a | 159 | * 0 on success, |
lawliet | 4:63a2619b423b | 160 | * -1 on error |
lawliet | 4:63a2619b423b | 161 | */ |
lawliet | 4:63a2619b423b | 162 | int sendTCPData(char *data); |
lawliet | 4:63a2619b423b | 163 | |
screamer | 8:1ee8ba65061a | 164 | /** Close TCP connection |
lawliet | 4:63a2619b423b | 165 | * @returns |
screamer | 8:1ee8ba65061a | 166 | * 0 on success, |
lawliet | 4:63a2619b423b | 167 | * -1 on error |
lawliet | 4:63a2619b423b | 168 | */ |
lawliet | 4:63a2619b423b | 169 | int closeTCP(void); |
lawliet | 4:63a2619b423b | 170 | |
screamer | 8:1ee8ba65061a | 171 | /** Close TCP service |
lawliet | 4:63a2619b423b | 172 | * @returns |
screamer | 8:1ee8ba65061a | 173 | * 0 on success, |
lawliet | 4:63a2619b423b | 174 | * -1 on error |
lawliet | 4:63a2619b423b | 175 | */ |
lawliet | 4:63a2619b423b | 176 | int shutTCP(void); |
lawliet | 4:63a2619b423b | 177 | |
lawliet | 6:e508f9ee7922 | 178 | Serial gprsSerial; |
lawliet | 4:63a2619b423b | 179 | //USBSerial pc; |
screamer | 8:1ee8ba65061a | 180 | |
lawliet | 4:63a2619b423b | 181 | private: |
screamer | 8:1ee8ba65061a | 182 | /** Read from GPRS module and save to buffer array |
lawliet | 2:16985da3a446 | 183 | * @param *buffer buffer array to save what read from GPRS module |
lawliet | 2:16985da3a446 | 184 | * @param *count the maximal bytes number read from GPRS module |
lawliet | 2:16985da3a446 | 185 | * @returns |
screamer | 8:1ee8ba65061a | 186 | * 0 on success, |
lawliet | 2:16985da3a446 | 187 | * -1 on error |
lawliet | 2:16985da3a446 | 188 | */ |
lawliet | 1:642a8dbe076c | 189 | int readBuffer(char *buffer,int count); |
lawliet | 2:16985da3a446 | 190 | |
screamer | 8:1ee8ba65061a | 191 | /** Send AT command to GPRS module |
lawliet | 2:16985da3a446 | 192 | * @param *cmd command array which will be send to GPRS module |
lawliet | 2:16985da3a446 | 193 | */ |
lawliet | 1:642a8dbe076c | 194 | void sendCmd(char *cmd); |
lawliet | 2:16985da3a446 | 195 | |
screamer | 8:1ee8ba65061a | 196 | /** Check GPRS module response before timeout |
lawliet | 2:16985da3a446 | 197 | * @param *resp correct response which GPRS module will return |
lawliet | 2:16985da3a446 | 198 | * @param *timeout waiting seconds till timeout |
lawliet | 2:16985da3a446 | 199 | * @returns |
screamer | 8:1ee8ba65061a | 200 | * 0 on success, |
lawliet | 2:16985da3a446 | 201 | * -1 on error |
lawliet | 2:16985da3a446 | 202 | */ |
lawliet | 1:642a8dbe076c | 203 | int waitForResp(char *resp, int timeout); |
lawliet | 2:16985da3a446 | 204 | |
screamer | 8:1ee8ba65061a | 205 | /** Send AT command to GPRS module and wait for correct response |
lawliet | 2:16985da3a446 | 206 | * @param *cmd AT command which will be send to GPRS module |
lawliet | 2:16985da3a446 | 207 | * @param *resp correct response which GPRS module will return |
lawliet | 2:16985da3a446 | 208 | * @param *timeout waiting seconds till timeout |
lawliet | 2:16985da3a446 | 209 | * @returns |
screamer | 8:1ee8ba65061a | 210 | * 0 on success, |
lawliet | 2:16985da3a446 | 211 | * -1 on error |
lawliet | 2:16985da3a446 | 212 | */ |
lawliet | 1:642a8dbe076c | 213 | int sendCmdAndWaitForResp(char *cmd, char *resp, int timeout); |
lawliet | 2:16985da3a446 | 214 | |
lawliet | 1:642a8dbe076c | 215 | Timer timeCnt; |
lawliet | 1:642a8dbe076c | 216 | char *phoneNumber; |
lawliet | 4:63a2619b423b | 217 | char messageBuffer[SMS_MAX_LENGTH]; |
lawliet | 0:a5ae94727346 | 218 | }; |
lawliet | 0:a5ae94727346 | 219 | |
lawliet | 0:a5ae94727346 | 220 | #endif |
lawliet | 4:63a2619b423b | 221 |