offer some API for GPRS use, such as call / sms / tcp connect

Dependents:   ARCH_GPRS_Test ARCH_GPRS_Xively GPRS_Shield_Test Seeed_Arch_GPRS_V2_HelloWorld

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers gprs.cpp Source File

gprs.cpp

00001 /*
00002   gprs.cpp
00003   2013 Copyright (c) Seeed Technology Inc.  All right reserved.
00004 
00005   Author:lawliet.zou@gmail.com
00006   2013-11-14
00007 
00008   This library is free software; you can redistribute it and/or
00009   modify it under the terms of the GNU Lesser General Public
00010   License as published by the Free Software Foundation; either
00011   version 2.1 of the License, or (at your option) any later version.
00012 
00013   This library is distributed in the hope that it will be useful,
00014   but WITHOUT ANY WARRANTY; without even the implied warranty of
00015   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00016   Lesser General Public License for more details.
00017 
00018   You should have received a copy of the GNU Lesser General Public
00019   License along with this library; if not, write to the Free Software
00020   Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
00021 */
00022 #include "gprs.h"
00023 
00024 int GPRS::readBuffer(char *buffer,int count)
00025 {
00026     int i = 0;
00027     timeCnt.start();  // start timer
00028     while(1) {
00029         while (gprsSerial.readable()) {
00030             char c = gprsSerial.getc();
00031             if (c == '\r' || c == '\n') c = '$';
00032             buffer[i++] = c;
00033             if(i > count)break;
00034         }
00035         if(i > count)break;
00036         if(timeCnt.read() > DEFAULT_TIMEOUT) {
00037             timeCnt.stop();
00038             timeCnt.reset();
00039             break;
00040         }
00041     }
00042     wait(0.5);
00043     while(gprsSerial.readable()) {  // display the other thing..
00044         char c = gprsSerial.getc();
00045     }
00046     return 0;
00047 }
00048 
00049 void cleanBuffer(char *buffer, int count)
00050 {
00051     for(int i=0; i < count; i++) {
00052         buffer[i] = '\0';
00053     }
00054 }
00055 
00056 void GPRS::sendCmd(char *cmd)
00057 {
00058     gprsSerial.puts(cmd);
00059 }
00060 
00061 int GPRS::waitForResp(char *resp, int timeout)
00062 {
00063     int len = strlen(resp);
00064     int sum=0;
00065     timeCnt.start();
00066 
00067     while(1) {
00068         if(gprsSerial.readable()) {
00069             char c = gprsSerial.getc();
00070             sum = (c==resp[sum]) ? sum+1 : 0;
00071             if(sum == len)break;
00072         }
00073         if(timeCnt.read() > timeout) {  // time out
00074             timeCnt.stop();
00075             timeCnt.reset();
00076             return -1;
00077         }
00078     }
00079     timeCnt.stop();                 // stop timer
00080     timeCnt.reset();                    // clear timer
00081     while(gprsSerial.readable()) {      // display the other thing..
00082         char c = gprsSerial.getc();
00083     }
00084 
00085     return 0;
00086 }
00087 
00088 int GPRS::sendCmdAndWaitForResp(char *cmd, char *resp, int timeout)
00089 {
00090     sendCmd(cmd);
00091     return waitForResp(resp,timeout);
00092 }
00093 
00094 int GPRS::powerCheck(void)
00095 {
00096     return sendCmdAndWaitForResp("AT\r\n", "OK", 2);    
00097 }
00098 
00099 int GPRS::init(void)
00100 {
00101     for(int i = 0; i < 3; i++){
00102         sendCmdAndWaitForResp("AT\r\n", "OK", DEFAULT_TIMEOUT);
00103         wait(0.5);
00104     }
00105     if(0 != checkSIMStatus()) {
00106         return -1;
00107     }
00108     if(checkSignalStrength()<1) {
00109         return -1;
00110     }
00111     if(0 != settingSMS()) {
00112         return -1;
00113     }
00114     return 0;
00115 }
00116 
00117 int GPRS::checkSIMStatus(void)
00118 {
00119     char gprsBuffer[30];
00120     int count = 0;
00121     cleanBuffer(gprsBuffer,30);
00122     while(count < 3) {
00123         sendCmd("AT+CPIN?\r\n");
00124         readBuffer(gprsBuffer,30);
00125         if((NULL != strstr(gprsBuffer,"+CPIN: READY"))) {
00126             break;
00127         }
00128         count++;
00129         wait(1);
00130     }
00131 
00132     if(count == 3) {
00133         return -1;
00134     }
00135     return 0;
00136 }
00137 
00138 int GPRS::checkSignalStrength(void)
00139 {
00140     char gprsBuffer[100];
00141     int index,count = 0;
00142     cleanBuffer(gprsBuffer,100);
00143     while(count < 3) {
00144         sendCmd("AT+CSQ\r\n");
00145         readBuffer(gprsBuffer,25);
00146         if(sscanf(gprsBuffer, "AT+CSQ$$$$+CSQ: %d", &index)>0) {
00147             break;
00148         }
00149         count++;
00150         wait(1);
00151     }
00152     if(count == 3) {
00153         return -1;
00154     }
00155     return index;
00156 }
00157 
00158 int GPRS::settingSMS(void)
00159 {
00160     if(0 != sendCmdAndWaitForResp("AT+CNMI=2,2\r\n", "OK", DEFAULT_TIMEOUT)) {
00161         return -1;
00162     }
00163     if(0 != sendCmdAndWaitForResp("AT+CMGF=1\r\n", "OK", DEFAULT_TIMEOUT)) {
00164         return -1;
00165     }
00166     return 0;
00167 }
00168 
00169 int GPRS::sendSMS(char *number, char *data)
00170 {
00171     char cmd[64];
00172     while(gprsSerial.readable()) {
00173         char c = gprsSerial.getc();
00174     }
00175     snprintf(cmd, sizeof(cmd),"AT+CMGS=\"%s\"\r\n",number);
00176     if(0 != sendCmdAndWaitForResp(cmd,">",DEFAULT_TIMEOUT)) {
00177         return -1;
00178     }
00179     wait(1);
00180     gprsSerial.puts(data);
00181     gprsSerial.putc((char)0x1a);
00182     return 0;
00183 }
00184 
00185 int GPRS::readSMS(char *message, int index)
00186 {
00187     int i = 0;
00188     char gprsBuffer[100];
00189     char *p,*s;
00190     gprsSerial.printf("AT+CMGR=%d\r\n",index);
00191     cleanBuffer(gprsBuffer,100);
00192     readBuffer(gprsBuffer,100);
00193     if(NULL == ( s = strstr(gprsBuffer,"+CMGR"))) {
00194         return -1;
00195     }
00196     if(NULL != ( s = strstr(gprsBuffer,"+32"))) {
00197         p = s + 6;
00198         while((*p != '$')&&(i < SMS_MAX_LENGTH-1)) {
00199             message[i++] = *(p++);
00200         }
00201         message[i] = '\0';
00202     }
00203     return 0;
00204 }
00205 
00206 int GPRS::deleteSMS(int index)
00207 {
00208     char cmd[32];
00209     snprintf(cmd,sizeof(cmd),"AT+CMGD=%d\r\n",index);
00210     sendCmd(cmd);
00211     return 0;
00212 }
00213 
00214 int GPRS::getSMS(char* message)
00215 {
00216     if(NULL != messageBuffer) {
00217         strncpy(message,messageBuffer,SMS_MAX_LENGTH);
00218     }
00219     return 0;
00220 }
00221 
00222 int GPRS::callUp(char *number)
00223 {
00224     if(0 != sendCmdAndWaitForResp("AT+COLP=1\r\n","OK",5)) {
00225         return -1;
00226     }
00227     wait(1);
00228     gprsSerial.printf("\r\nATD%s;\r\n",NULL==number?phoneNumber:number);
00229     return 0;
00230 }
00231 
00232 int GPRS::answer(void)
00233 {
00234     gprsSerial.printf("ATA\r\n");
00235     return 0;
00236 }
00237 
00238 int GPRS::loopHandle(void)
00239 {
00240     char gprsBuffer[100];
00241     int i;
00242     char *s = NULL;
00243     while(gprsSerial.readable()) {
00244         char c = gprsSerial.getc();
00245     }
00246     wait(0.5);
00247 START:
00248     cleanBuffer(gprsBuffer,100);
00249     i = 0;
00250     while(1) {
00251         if(gprsSerial.readable()) {
00252             timeCnt.start();  // start timer
00253             while(1) {
00254                 while (gprsSerial.readable()) {
00255                     char c = gprsSerial.getc();
00256                     if (c == '\r' || c == '\n') c = '$';
00257                     gprsBuffer[i] = c;
00258                     i++;
00259                     if(i > 100) {
00260                         i = 0;
00261                         break;
00262                     }
00263                 }
00264                 if(timeCnt.read() > 2) {          // time out
00265                     timeCnt.stop();
00266                     timeCnt.reset();
00267                     break;
00268                 }
00269             }
00270             break;
00271         }
00272     }
00273     if(NULL != strstr(gprsBuffer,"RING")) {
00274         return MESSAGE_RING;
00275     } else if(NULL != (s = strstr(gprsBuffer,"+CMT"))) { //SMS: $$+CMTI: "SM",24$$
00276         if(NULL != (s = strstr(gprsBuffer,"+32"))) {
00277             s += 6;
00278             int i = 0;
00279             cleanBuffer(messageBuffer,SMS_MAX_LENGTH);
00280             while((*s != '$')&&(i < SMS_MAX_LENGTH-1)) {
00281                 messageBuffer[i++] = *(s++);
00282             }
00283             messageBuffer[i] = '\0';
00284             return MESSAGE_SMS;
00285         } else {
00286             goto START;
00287         }
00288     } else {
00289         goto START;
00290     }
00291 }
00292 
00293 int GPRS::networkInit(char* apn, char* userName, char* passWord)
00294 {
00295     char cstt[64];
00296     snprintf(cstt,sizeof(cstt),"AT+CSTT=\"%s\",\"%s\",\"%s\"\r\n",apn,userName,passWord);
00297     if(0 != sendCmdAndWaitForResp(cstt, "OK", DEFAULT_TIMEOUT)) {
00298         return -1;
00299     }
00300     return 0;
00301 }
00302 
00303 int GPRS::connectTCP(char *ip, char *port)
00304 {
00305     char cipstart[64];
00306 #if 0
00307     if(0 != sendCmdAndWaitForResp("AT+CSTT=\"CMNET\",\"\",\"\"\r\n", "OK", 5)) {
00308         return -1;
00309     }
00310 #endif
00311     sprintf(cipstart, "AT+CIPSTART=\"TCP\",\"%s\",\"%s\"\r\n", ip, port);
00312     if(0 != sendCmdAndWaitForResp(cipstart, "OK", DEFAULT_TIMEOUT)) {
00313         return -1;
00314     }
00315     return 0;
00316 }
00317 int GPRS::sendTCPData(char *data)
00318 {
00319     char cmd[64];
00320     int len = strlen(data);
00321     snprintf(cmd,sizeof(cmd),"AT+CIPSEND=%d\r\n",len);
00322     if(0 != sendCmdAndWaitForResp(cmd,">",DEFAULT_TIMEOUT)) {
00323         return -1;
00324     }
00325     if(0 != sendCmdAndWaitForResp(data,"OK",DEFAULT_TIMEOUT)) {
00326         return -1;
00327     }
00328     return 0;
00329 }
00330 
00331 int GPRS::closeTCP(void)
00332 {
00333     sendCmd("AT+CIPCLOSE\r\n");
00334     return 0;
00335 }
00336 
00337 int GPRS::shutTCP(void)
00338 {
00339     sendCmd("AT+CIPSHUT\r\n");
00340     return 0;
00341 }