Habinthesky / GSM
Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers GSM.cpp Source File

GSM.cpp

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