rhr
GSM.cpp@13:d05bf1e0237e, 2020-10-05 (annotated)
- Committer:
- Michelbylette
- Date:
- Mon Oct 05 08:32:08 2020 +0000
- Revision:
- 13:d05bf1e0237e
- Parent:
- 11:c93524a12e92
gg;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
lawliet | 0:a5ae94727346 | 1 | /* |
screamer | 10:24671d8aa0c9 | 2 | GSM.cpp |
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 | */ |
screamer | 10:24671d8aa0c9 | 22 | #include "GSM.h" |
lawliet | 0:a5ae94727346 | 23 | |
Michelbylette | 11:c93524a12e92 | 24 | void cleanBuffer(char *buffer, int count) |
Michelbylette | 11:c93524a12e92 | 25 | { |
Michelbylette | 11:c93524a12e92 | 26 | for(int i=0; i < count; i++) { |
Michelbylette | 11:c93524a12e92 | 27 | buffer[i] = '\0'; |
Michelbylette | 11:c93524a12e92 | 28 | } |
Michelbylette | 11:c93524a12e92 | 29 | } |
Michelbylette | 11:c93524a12e92 | 30 | |
Michelbylette | 11:c93524a12e92 | 31 | int GSM::unlock(char *PIN) |
Michelbylette | 11:c93524a12e92 | 32 | { |
Michelbylette | 11:c93524a12e92 | 33 | |
Michelbylette | 11:c93524a12e92 | 34 | char cmd[32]; |
Michelbylette | 11:c93524a12e92 | 35 | char gprsBuffer[30]; |
Michelbylette | 11:c93524a12e92 | 36 | int count = 0; |
Michelbylette | 11:c93524a12e92 | 37 | cleanBuffer(gprsBuffer,30); |
Michelbylette | 11:c93524a12e92 | 38 | snprintf(cmd, sizeof(cmd),"AT+CPIN=%s\r\n", PIN); |
Michelbylette | 11:c93524a12e92 | 39 | wait(1); |
Michelbylette | 11:c93524a12e92 | 40 | sendCmdAndWaitForResp(cmd, "OK"); |
Michelbylette | 11:c93524a12e92 | 41 | while(count < 3) { |
Michelbylette | 11:c93524a12e92 | 42 | //wait(1); |
Michelbylette | 11:c93524a12e92 | 43 | sendCmd("AT+CPIN?\r\n"); |
Michelbylette | 11:c93524a12e92 | 44 | readBuffer(gprsBuffer,30); |
Michelbylette | 11:c93524a12e92 | 45 | //wait(1); |
Michelbylette | 11:c93524a12e92 | 46 | if((NULL != strstr(gprsBuffer,"+CPIN: READY"))) { |
Michelbylette | 11:c93524a12e92 | 47 | return 0; |
Michelbylette | 11:c93524a12e92 | 48 | } |
Michelbylette | 11:c93524a12e92 | 49 | count++; |
Michelbylette | 11:c93524a12e92 | 50 | wait(1); |
Michelbylette | 11:c93524a12e92 | 51 | } |
Michelbylette | 11:c93524a12e92 | 52 | return -1; // too many tries |
Michelbylette | 11:c93524a12e92 | 53 | } |
Michelbylette | 11:c93524a12e92 | 54 | |
Michelbylette | 11:c93524a12e92 | 55 | int GSM::powerOn(void) |
Michelbylette | 11:c93524a12e92 | 56 | { |
Michelbylette | 11:c93524a12e92 | 57 | int count = 0; |
Michelbylette | 11:c93524a12e92 | 58 | while (0 != powerCheck()) { |
Michelbylette | 11:c93524a12e92 | 59 | PWK = 1; |
Michelbylette | 11:c93524a12e92 | 60 | count ++; |
Michelbylette | 11:c93524a12e92 | 61 | wait(0.5); |
Michelbylette | 11:c93524a12e92 | 62 | if (count > 3) return -1; |
Michelbylette | 11:c93524a12e92 | 63 | } |
Michelbylette | 11:c93524a12e92 | 64 | PWK = 0; |
Michelbylette | 11:c93524a12e92 | 65 | return 0; |
Michelbylette | 11:c93524a12e92 | 66 | } |
Michelbylette | 11:c93524a12e92 | 67 | |
Michelbylette | 11:c93524a12e92 | 68 | int GSM::powerOff(void) |
Michelbylette | 11:c93524a12e92 | 69 | { |
Michelbylette | 11:c93524a12e92 | 70 | while(0 == powerCheck()) { |
Michelbylette | 11:c93524a12e92 | 71 | sendCmdAndWaitForResp("AT+QPOWD=1\r", "OK"); |
Michelbylette | 11:c93524a12e92 | 72 | wait(0.1); |
Michelbylette | 11:c93524a12e92 | 73 | } |
Michelbylette | 11:c93524a12e92 | 74 | return 0; |
Michelbylette | 11:c93524a12e92 | 75 | } |
Michelbylette | 11:c93524a12e92 | 76 | |
Michelbylette | 11:c93524a12e92 | 77 | |
Michelbylette | 11:c93524a12e92 | 78 | |
screamer | 10:24671d8aa0c9 | 79 | int GSM::readBuffer(char *buffer,int count) |
lawliet | 0:a5ae94727346 | 80 | { |
lawliet | 1:642a8dbe076c | 81 | int i = 0; |
lawliet | 1:642a8dbe076c | 82 | timeCnt.start(); // start timer |
lawliet | 1:642a8dbe076c | 83 | while(1) { |
lawliet | 1:642a8dbe076c | 84 | while (gprsSerial.readable()) { |
lawliet | 1:642a8dbe076c | 85 | char c = gprsSerial.getc(); |
lawliet | 1:642a8dbe076c | 86 | if (c == '\r' || c == '\n') c = '$'; |
lawliet | 1:642a8dbe076c | 87 | buffer[i++] = c; |
lawliet | 1:642a8dbe076c | 88 | if(i > count)break; |
lawliet | 1:642a8dbe076c | 89 | } |
lawliet | 1:642a8dbe076c | 90 | if(i > count)break; |
lawliet | 3:48ee24a4b0f3 | 91 | if(timeCnt.read() > DEFAULT_TIMEOUT) { |
lawliet | 1:642a8dbe076c | 92 | timeCnt.stop(); |
lawliet | 1:642a8dbe076c | 93 | timeCnt.reset(); |
lawliet | 1:642a8dbe076c | 94 | break; |
lawliet | 1:642a8dbe076c | 95 | } |
lawliet | 1:642a8dbe076c | 96 | } |
lawliet | 1:642a8dbe076c | 97 | wait(0.5); |
Michelbylette | 11:c93524a12e92 | 98 | while(gprsSerial.readable()) { // display the other thing.. |
lawliet | 1:642a8dbe076c | 99 | char c = gprsSerial.getc(); |
lawliet | 1:642a8dbe076c | 100 | } |
lawliet | 1:642a8dbe076c | 101 | return 0; |
lawliet | 0:a5ae94727346 | 102 | } |
lawliet | 0:a5ae94727346 | 103 | |
screamer | 10:24671d8aa0c9 | 104 | void GSM::sendCmd(char *cmd) |
lawliet | 0:a5ae94727346 | 105 | { |
lawliet | 1:642a8dbe076c | 106 | gprsSerial.puts(cmd); |
lawliet | 0:a5ae94727346 | 107 | } |
lawliet | 0:a5ae94727346 | 108 | |
screamer | 10:24671d8aa0c9 | 109 | int GSM::waitForResp(char *resp, int timeout) |
lawliet | 0:a5ae94727346 | 110 | { |
lawliet | 1:642a8dbe076c | 111 | int len = strlen(resp); |
lawliet | 1:642a8dbe076c | 112 | int sum=0; |
lawliet | 1:642a8dbe076c | 113 | timeCnt.start(); |
lawliet | 1:642a8dbe076c | 114 | |
lawliet | 1:642a8dbe076c | 115 | while(1) { |
lawliet | 1:642a8dbe076c | 116 | if(gprsSerial.readable()) { |
lawliet | 1:642a8dbe076c | 117 | char c = gprsSerial.getc(); |
lawliet | 1:642a8dbe076c | 118 | sum = (c==resp[sum]) ? sum+1 : 0; |
lawliet | 1:642a8dbe076c | 119 | if(sum == len)break; |
lawliet | 1:642a8dbe076c | 120 | } |
lawliet | 1:642a8dbe076c | 121 | if(timeCnt.read() > timeout) { // time out |
lawliet | 1:642a8dbe076c | 122 | timeCnt.stop(); |
lawliet | 1:642a8dbe076c | 123 | timeCnt.reset(); |
lawliet | 1:642a8dbe076c | 124 | return -1; |
lawliet | 1:642a8dbe076c | 125 | } |
lawliet | 1:642a8dbe076c | 126 | } |
Michelbylette | 11:c93524a12e92 | 127 | timeCnt.stop(); // stop timer |
lawliet | 1:642a8dbe076c | 128 | timeCnt.reset(); // clear timer |
lawliet | 1:642a8dbe076c | 129 | while(gprsSerial.readable()) { // display the other thing.. |
lawliet | 1:642a8dbe076c | 130 | char c = gprsSerial.getc(); |
lawliet | 1:642a8dbe076c | 131 | } |
lawliet | 1:642a8dbe076c | 132 | |
lawliet | 1:642a8dbe076c | 133 | return 0; |
lawliet | 1:642a8dbe076c | 134 | } |
lawliet | 0:a5ae94727346 | 135 | |
screamer | 10:24671d8aa0c9 | 136 | int GSM::sendCmdAndWaitForResp(char *cmd, char *resp, int timeout) |
lawliet | 0:a5ae94727346 | 137 | { |
lawliet | 0:a5ae94727346 | 138 | sendCmd(cmd); |
lawliet | 1:642a8dbe076c | 139 | return waitForResp(resp,timeout); |
lawliet | 0:a5ae94727346 | 140 | } |
lawliet | 0:a5ae94727346 | 141 | |
screamer | 10:24671d8aa0c9 | 142 | int GSM::powerCheck(void) |
lawliet | 7:1278e9dde24e | 143 | { |
Michelbylette | 11:c93524a12e92 | 144 | return sendCmdAndWaitForResp("AT\r\n", "OK", 2); |
lawliet | 7:1278e9dde24e | 145 | } |
lawliet | 7:1278e9dde24e | 146 | |
screamer | 10:24671d8aa0c9 | 147 | int GSM::init(void) |
lawliet | 4:63a2619b423b | 148 | { |
lawliet | 7:1278e9dde24e | 149 | for(int i = 0; i < 3; i++){ |
Michelbylette | 11:c93524a12e92 | 150 | sendCmdAndWaitForResp("AT\r\n", "OK"); |
lawliet | 7:1278e9dde24e | 151 | wait(0.5); |
lawliet | 7:1278e9dde24e | 152 | } |
lawliet | 4:63a2619b423b | 153 | if(0 != checkSIMStatus()) { |
lawliet | 4:63a2619b423b | 154 | return -1; |
lawliet | 4:63a2619b423b | 155 | } |
lawliet | 4:63a2619b423b | 156 | if(checkSignalStrength()<1) { |
Michelbylette | 11:c93524a12e92 | 157 | return -2; |
lawliet | 4:63a2619b423b | 158 | } |
lawliet | 4:63a2619b423b | 159 | if(0 != settingSMS()) { |
Michelbylette | 11:c93524a12e92 | 160 | return -3; |
lawliet | 4:63a2619b423b | 161 | } |
lawliet | 4:63a2619b423b | 162 | return 0; |
lawliet | 4:63a2619b423b | 163 | } |
lawliet | 4:63a2619b423b | 164 | |
screamer | 10:24671d8aa0c9 | 165 | int GSM::checkSIMStatus(void) |
lawliet | 0:a5ae94727346 | 166 | { |
lawliet | 1:642a8dbe076c | 167 | char gprsBuffer[30]; |
lawliet | 1:642a8dbe076c | 168 | int count = 0; |
lawliet | 1:642a8dbe076c | 169 | cleanBuffer(gprsBuffer,30); |
Michelbylette | 11:c93524a12e92 | 170 | |
lawliet | 1:642a8dbe076c | 171 | while(count < 3) { |
lawliet | 1:642a8dbe076c | 172 | sendCmd("AT+CPIN?\r\n"); |
lawliet | 1:642a8dbe076c | 173 | readBuffer(gprsBuffer,30); |
lawliet | 1:642a8dbe076c | 174 | if((NULL != strstr(gprsBuffer,"+CPIN: READY"))) { |
lawliet | 1:642a8dbe076c | 175 | break; |
lawliet | 1:642a8dbe076c | 176 | } |
lawliet | 1:642a8dbe076c | 177 | count++; |
lawliet | 1:642a8dbe076c | 178 | wait(1); |
lawliet | 1:642a8dbe076c | 179 | } |
lawliet | 0:a5ae94727346 | 180 | |
lawliet | 1:642a8dbe076c | 181 | if(count == 3) { |
lawliet | 1:642a8dbe076c | 182 | return -1; |
lawliet | 1:642a8dbe076c | 183 | } |
lawliet | 1:642a8dbe076c | 184 | return 0; |
lawliet | 0:a5ae94727346 | 185 | } |
lawliet | 0:a5ae94727346 | 186 | |
screamer | 10:24671d8aa0c9 | 187 | int GSM::checkSignalStrength(void) |
lawliet | 0:a5ae94727346 | 188 | { |
lawliet | 1:642a8dbe076c | 189 | char gprsBuffer[100]; |
lawliet | 1:642a8dbe076c | 190 | int index,count = 0; |
lawliet | 1:642a8dbe076c | 191 | cleanBuffer(gprsBuffer,100); |
lawliet | 1:642a8dbe076c | 192 | while(count < 3) { |
lawliet | 1:642a8dbe076c | 193 | sendCmd("AT+CSQ\r\n"); |
lawliet | 1:642a8dbe076c | 194 | readBuffer(gprsBuffer,25); |
lawliet | 1:642a8dbe076c | 195 | if(sscanf(gprsBuffer, "AT+CSQ$$$$+CSQ: %d", &index)>0) { |
lawliet | 1:642a8dbe076c | 196 | break; |
lawliet | 1:642a8dbe076c | 197 | } |
lawliet | 1:642a8dbe076c | 198 | count++; |
lawliet | 1:642a8dbe076c | 199 | wait(1); |
lawliet | 1:642a8dbe076c | 200 | } |
Michelbylette | 11:c93524a12e92 | 201 | if(count == 10) { |
lawliet | 1:642a8dbe076c | 202 | return -1; |
lawliet | 1:642a8dbe076c | 203 | } |
lawliet | 1:642a8dbe076c | 204 | return index; |
lawliet | 0:a5ae94727346 | 205 | } |
lawliet | 0:a5ae94727346 | 206 | |
screamer | 10:24671d8aa0c9 | 207 | int GSM::settingSMS(void) |
lawliet | 4:63a2619b423b | 208 | { |
Michelbylette | 11:c93524a12e92 | 209 | if(0 != sendCmdAndWaitForResp("AT+CMGF=1\r\n", "OK")) { |
lawliet | 4:63a2619b423b | 210 | return -1; |
lawliet | 4:63a2619b423b | 211 | } |
Michelbylette | 11:c93524a12e92 | 212 | if(0 != sendCmdAndWaitForResp("AT+CNMI=2,2\r\n", "OK")) { //AT+CNMI=2,2 |
Michelbylette | 11:c93524a12e92 | 213 | |
Michelbylette | 11:c93524a12e92 | 214 | return -2; |
lawliet | 4:63a2619b423b | 215 | } |
lawliet | 4:63a2619b423b | 216 | return 0; |
lawliet | 4:63a2619b423b | 217 | } |
lawliet | 4:63a2619b423b | 218 | |
screamer | 10:24671d8aa0c9 | 219 | int GSM::sendSMS(char *number, char *data) |
lawliet | 0:a5ae94727346 | 220 | { |
lawliet | 1:642a8dbe076c | 221 | char cmd[64]; |
lawliet | 4:63a2619b423b | 222 | while(gprsSerial.readable()) { |
lawliet | 4:63a2619b423b | 223 | char c = gprsSerial.getc(); |
lawliet | 1:642a8dbe076c | 224 | } |
Michelbylette | 11:c93524a12e92 | 225 | snprintf(cmd, sizeof(cmd), "AT+CMGS=\"%s\"\r\n", number); |
Michelbylette | 11:c93524a12e92 | 226 | if(0 != sendCmdAndWaitForResp(cmd, ">")) { |
lawliet | 1:642a8dbe076c | 227 | return -1; |
lawliet | 1:642a8dbe076c | 228 | } |
lawliet | 1:642a8dbe076c | 229 | wait(1); |
lawliet | 4:63a2619b423b | 230 | gprsSerial.puts(data); |
lawliet | 4:63a2619b423b | 231 | gprsSerial.putc((char)0x1a); |
lawliet | 1:642a8dbe076c | 232 | return 0; |
lawliet | 0:a5ae94727346 | 233 | } |
lawliet | 0:a5ae94727346 | 234 | |
screamer | 10:24671d8aa0c9 | 235 | int GSM::readSMS(char *message, int index) |
lawliet | 0:a5ae94727346 | 236 | { |
lawliet | 4:63a2619b423b | 237 | int i = 0; |
lawliet | 1:642a8dbe076c | 238 | char gprsBuffer[100]; |
lawliet | 1:642a8dbe076c | 239 | char *p,*s; |
lawliet | 4:63a2619b423b | 240 | gprsSerial.printf("AT+CMGR=%d\r\n",index); |
lawliet | 1:642a8dbe076c | 241 | cleanBuffer(gprsBuffer,100); |
lawliet | 1:642a8dbe076c | 242 | readBuffer(gprsBuffer,100); |
lawliet | 4:63a2619b423b | 243 | if(NULL == ( s = strstr(gprsBuffer,"+CMGR"))) { |
lawliet | 1:642a8dbe076c | 244 | return -1; |
lawliet | 1:642a8dbe076c | 245 | } |
lawliet | 4:63a2619b423b | 246 | if(NULL != ( s = strstr(gprsBuffer,"+32"))) { |
lawliet | 4:63a2619b423b | 247 | p = s + 6; |
lawliet | 4:63a2619b423b | 248 | while((*p != '$')&&(i < SMS_MAX_LENGTH-1)) { |
lawliet | 4:63a2619b423b | 249 | message[i++] = *(p++); |
lawliet | 1:642a8dbe076c | 250 | } |
lawliet | 4:63a2619b423b | 251 | message[i] = '\0'; |
lawliet | 1:642a8dbe076c | 252 | } |
lawliet | 1:642a8dbe076c | 253 | return 0; |
lawliet | 0:a5ae94727346 | 254 | } |
lawliet | 0:a5ae94727346 | 255 | |
screamer | 10:24671d8aa0c9 | 256 | int GSM::deleteSMS(int index) |
lawliet | 0:a5ae94727346 | 257 | { |
lawliet | 4:63a2619b423b | 258 | char cmd[32]; |
lawliet | 1:642a8dbe076c | 259 | snprintf(cmd,sizeof(cmd),"AT+CMGD=%d\r\n",index); |
lawliet | 1:642a8dbe076c | 260 | sendCmd(cmd); |
lawliet | 1:642a8dbe076c | 261 | return 0; |
lawliet | 0:a5ae94727346 | 262 | } |
lawliet | 0:a5ae94727346 | 263 | |
screamer | 10:24671d8aa0c9 | 264 | int GSM::getSMS(char* message) |
lawliet | 4:63a2619b423b | 265 | { |
lawliet | 4:63a2619b423b | 266 | if(NULL != messageBuffer) { |
lawliet | 4:63a2619b423b | 267 | strncpy(message,messageBuffer,SMS_MAX_LENGTH); |
lawliet | 4:63a2619b423b | 268 | } |
lawliet | 4:63a2619b423b | 269 | return 0; |
lawliet | 4:63a2619b423b | 270 | } |
lawliet | 4:63a2619b423b | 271 | |
screamer | 10:24671d8aa0c9 | 272 | int GSM::callUp(char *number) |
lawliet | 0:a5ae94727346 | 273 | { |
lawliet | 1:642a8dbe076c | 274 | if(0 != sendCmdAndWaitForResp("AT+COLP=1\r\n","OK",5)) { |
lawliet | 1:642a8dbe076c | 275 | return -1; |
lawliet | 1:642a8dbe076c | 276 | } |
lawliet | 1:642a8dbe076c | 277 | wait(1); |
lawliet | 1:642a8dbe076c | 278 | gprsSerial.printf("\r\nATD%s;\r\n",NULL==number?phoneNumber:number); |
lawliet | 1:642a8dbe076c | 279 | return 0; |
lawliet | 0:a5ae94727346 | 280 | } |
lawliet | 0:a5ae94727346 | 281 | |
screamer | 10:24671d8aa0c9 | 282 | int GSM::answer(void) |
lawliet | 0:a5ae94727346 | 283 | { |
lawliet | 1:642a8dbe076c | 284 | gprsSerial.printf("ATA\r\n"); |
lawliet | 1:642a8dbe076c | 285 | return 0; |
lawliet | 0:a5ae94727346 | 286 | } |
lawliet | 0:a5ae94727346 | 287 | |
screamer | 10:24671d8aa0c9 | 288 | int GSM::loopHandle(void) |
lawliet | 0:a5ae94727346 | 289 | { |
lawliet | 1:642a8dbe076c | 290 | char gprsBuffer[100]; |
lawliet | 7:1278e9dde24e | 291 | int i; |
lawliet | 4:63a2619b423b | 292 | char *s = NULL; |
lawliet | 4:63a2619b423b | 293 | while(gprsSerial.readable()) { |
lawliet | 4:63a2619b423b | 294 | char c = gprsSerial.getc(); |
lawliet | 4:63a2619b423b | 295 | } |
lawliet | 7:1278e9dde24e | 296 | wait(0.5); |
lawliet | 4:63a2619b423b | 297 | START: |
lawliet | 7:1278e9dde24e | 298 | cleanBuffer(gprsBuffer,100); |
lawliet | 7:1278e9dde24e | 299 | i = 0; |
lawliet | 1:642a8dbe076c | 300 | while(1) { |
lawliet | 1:642a8dbe076c | 301 | if(gprsSerial.readable()) { |
lawliet | 7:1278e9dde24e | 302 | timeCnt.start(); // start timer |
lawliet | 7:1278e9dde24e | 303 | while(1) { |
lawliet | 7:1278e9dde24e | 304 | while (gprsSerial.readable()) { |
lawliet | 7:1278e9dde24e | 305 | char c = gprsSerial.getc(); |
lawliet | 7:1278e9dde24e | 306 | if (c == '\r' || c == '\n') c = '$'; |
lawliet | 7:1278e9dde24e | 307 | gprsBuffer[i] = c; |
lawliet | 7:1278e9dde24e | 308 | i++; |
lawliet | 7:1278e9dde24e | 309 | if(i > 100) { |
lawliet | 7:1278e9dde24e | 310 | i = 0; |
lawliet | 7:1278e9dde24e | 311 | break; |
lawliet | 7:1278e9dde24e | 312 | } |
lawliet | 7:1278e9dde24e | 313 | } |
lawliet | 7:1278e9dde24e | 314 | if(timeCnt.read() > 2) { // time out |
lawliet | 7:1278e9dde24e | 315 | timeCnt.stop(); |
lawliet | 7:1278e9dde24e | 316 | timeCnt.reset(); |
lawliet | 7:1278e9dde24e | 317 | break; |
lawliet | 7:1278e9dde24e | 318 | } |
lawliet | 7:1278e9dde24e | 319 | } |
lawliet | 1:642a8dbe076c | 320 | break; |
lawliet | 1:642a8dbe076c | 321 | } |
lawliet | 1:642a8dbe076c | 322 | } |
lawliet | 1:642a8dbe076c | 323 | if(NULL != strstr(gprsBuffer,"RING")) { |
lawliet | 4:63a2619b423b | 324 | return MESSAGE_RING; |
lawliet | 4:63a2619b423b | 325 | } else if(NULL != (s = strstr(gprsBuffer,"+CMT"))) { //SMS: $$+CMTI: "SM",24$$ |
lawliet | 4:63a2619b423b | 326 | if(NULL != (s = strstr(gprsBuffer,"+32"))) { |
lawliet | 4:63a2619b423b | 327 | s += 6; |
lawliet | 4:63a2619b423b | 328 | int i = 0; |
lawliet | 4:63a2619b423b | 329 | cleanBuffer(messageBuffer,SMS_MAX_LENGTH); |
lawliet | 4:63a2619b423b | 330 | while((*s != '$')&&(i < SMS_MAX_LENGTH-1)) { |
lawliet | 4:63a2619b423b | 331 | messageBuffer[i++] = *(s++); |
lawliet | 4:63a2619b423b | 332 | } |
lawliet | 4:63a2619b423b | 333 | messageBuffer[i] = '\0'; |
lawliet | 4:63a2619b423b | 334 | return MESSAGE_SMS; |
lawliet | 4:63a2619b423b | 335 | } else { |
lawliet | 4:63a2619b423b | 336 | goto START; |
lawliet | 1:642a8dbe076c | 337 | } |
lawliet | 4:63a2619b423b | 338 | } else { |
lawliet | 4:63a2619b423b | 339 | goto START; |
lawliet | 1:642a8dbe076c | 340 | } |
lawliet | 0:a5ae94727346 | 341 | } |
lawliet | 0:a5ae94727346 | 342 | |
screamer | 10:24671d8aa0c9 | 343 | int GSM::networkInit(char* apn, char* userName, char* passWord) |
lawliet | 5:ac2342f162fa | 344 | { |
lawliet | 5:ac2342f162fa | 345 | char cstt[64]; |
lawliet | 5:ac2342f162fa | 346 | snprintf(cstt,sizeof(cstt),"AT+CSTT=\"%s\",\"%s\",\"%s\"\r\n",apn,userName,passWord); |
Michelbylette | 11:c93524a12e92 | 347 | if(0 != sendCmdAndWaitForResp(cstt, "OK")) { |
lawliet | 5:ac2342f162fa | 348 | return -1; |
lawliet | 5:ac2342f162fa | 349 | } |
lawliet | 5:ac2342f162fa | 350 | return 0; |
lawliet | 5:ac2342f162fa | 351 | } |
lawliet | 0:a5ae94727346 | 352 | |
screamer | 10:24671d8aa0c9 | 353 | int GSM::connectTCP(char *ip, char *port) |
lawliet | 0:a5ae94727346 | 354 | { |
lawliet | 5:ac2342f162fa | 355 | char cipstart[64]; |
lawliet | 5:ac2342f162fa | 356 | #if 0 |
lawliet | 3:48ee24a4b0f3 | 357 | if(0 != sendCmdAndWaitForResp("AT+CSTT=\"CMNET\",\"\",\"\"\r\n", "OK", 5)) { |
lawliet | 1:642a8dbe076c | 358 | return -1; |
lawliet | 1:642a8dbe076c | 359 | } |
lawliet | 5:ac2342f162fa | 360 | #endif |
lawliet | 1:642a8dbe076c | 361 | sprintf(cipstart, "AT+CIPSTART=\"TCP\",\"%s\",\"%s\"\r\n", ip, port); |
Michelbylette | 11:c93524a12e92 | 362 | if(0 != sendCmdAndWaitForResp(cipstart, "OK")) { |
lawliet | 1:642a8dbe076c | 363 | return -1; |
lawliet | 1:642a8dbe076c | 364 | } |
lawliet | 1:642a8dbe076c | 365 | return 0; |
lawliet | 0:a5ae94727346 | 366 | } |
screamer | 10:24671d8aa0c9 | 367 | int GSM::sendTCPData(char *data) |
lawliet | 0:a5ae94727346 | 368 | { |
lawliet | 1:642a8dbe076c | 369 | char cmd[64]; |
lawliet | 1:642a8dbe076c | 370 | int len = strlen(data); |
lawliet | 1:642a8dbe076c | 371 | snprintf(cmd,sizeof(cmd),"AT+CIPSEND=%d\r\n",len); |
Michelbylette | 11:c93524a12e92 | 372 | if(0 != sendCmdAndWaitForResp(cmd,">")) { |
lawliet | 1:642a8dbe076c | 373 | return -1; |
lawliet | 1:642a8dbe076c | 374 | } |
Michelbylette | 11:c93524a12e92 | 375 | if(0 != sendCmdAndWaitForResp(data,"OK")) { |
lawliet | 1:642a8dbe076c | 376 | return -1; |
lawliet | 1:642a8dbe076c | 377 | } |
lawliet | 1:642a8dbe076c | 378 | return 0; |
lawliet | 0:a5ae94727346 | 379 | } |
lawliet | 0:a5ae94727346 | 380 | |
screamer | 10:24671d8aa0c9 | 381 | int GSM::closeTCP(void) |
lawliet | 0:a5ae94727346 | 382 | { |
lawliet | 1:642a8dbe076c | 383 | sendCmd("AT+CIPCLOSE\r\n"); |
lawliet | 1:642a8dbe076c | 384 | return 0; |
lawliet | 0:a5ae94727346 | 385 | } |
lawliet | 0:a5ae94727346 | 386 | |
screamer | 10:24671d8aa0c9 | 387 | int GSM::shutTCP(void) |
lawliet | 0:a5ae94727346 | 388 | { |
lawliet | 1:642a8dbe076c | 389 | sendCmd("AT+CIPSHUT\r\n"); |
lawliet | 1:642a8dbe076c | 390 | return 0; |
lawliet | 0:a5ae94727346 | 391 | } |