ghrh

Committer:
screamer
Date:
Tue May 13 14:28:31 2014 +0000
Revision:
8:1ee8ba65061a
Parent:
gprs.cpp@5:ac2342f162fa
Child:
9:2f5445178940
Fix the source/header files to match the case name of the class. Tune the documentation

Who changed what in which revision?

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