program to test exporting mbed-dev
Fork of FONA_CellPhone by
Diff: Adafruit_FONA.cpp
- Revision:
- 18:d52017f1d087
- Parent:
- 9:06eca688f2a3
--- a/Adafruit_FONA.cpp Wed Mar 30 16:19:51 2016 +0000 +++ b/Adafruit_FONA.cpp Wed Jan 25 12:56:31 2017 +0000 @@ -33,7 +33,7 @@ bool Adafruit_FONA::begin(int baudrate) { mySerial.baud(baudrate); //set the baud rate of the fona serial connection - mySerial.attach(this, &Adafruit_FONA::onSerialDataReceived, Serial::RxIrq); //attached onSerialDataReceived as Serial receive interrupt + mySerial.attach(Callback<void()>(this, &Adafruit_FONA::onSerialDataReceived), Serial::RxIrq); // INIT Reboot process _rstpin = HIGH; @@ -64,7 +64,9 @@ if (! sendCheckReply("ATE0", "OK")) { return false; } - + sendCheckReply("AT+CVHU=0", "OK"); + wait_ms(100); + _incomingCall = false; return true; } @@ -72,6 +74,10 @@ this->eventListener = eventListener; } +bool Adafruit_FONA::setBaudrate(uint16_t baud) { + return sendCheckReply("AT+IPREX=", baud, "OK"); +} + /********* Stream ********************************************/ int Adafruit_FONA::_putc(int value) { @@ -138,7 +144,12 @@ } /********* Real Time Clock ********************************************/ - +bool Adafruit_FONA::readRTC(uint8_t *year, uint8_t *month, uint8_t *date, uint8_t *hr, uint8_t *min, uint8_t *sec) { + uint16_t v; + sendParseReply("AT+CCLK?", "+CCLK: ", &v, '/', 0); + *year = v; + return true; +} bool Adafruit_FONA::enableRTC(uint8_t i) { if (! sendCheckReply("AT+CLTS=", i, "OK")) return false; @@ -342,6 +353,14 @@ return sendCheckReply(sendbuff, "OK"); } +uint8_t Adafruit_FONA::getCallStatus(void) { + uint16_t phoneStatus; + + if (! sendParseReply("AT+CPAS", "+CPAS: ", &phoneStatus)) + return FONA_CALL_FAILED; // 1, since 0 is actually a known, good reply + + return phoneStatus; // 0 ready, 2 unkown, 3 ringing, 4 call in progress +} bool Adafruit_FONA::hangUp(void) { return sendCheckReply("ATH0", "OK"); @@ -357,10 +376,9 @@ #endif _incomingCall = true; } - bool Adafruit_FONA::callerIdNotification(bool enable) { if(enable){ - _ringIndicatorInterruptIn.fall(this, &Adafruit_FONA::onIncomingCall); + _ringIndicatorInterruptIn.fall(Callback<void()>(this, &Adafruit_FONA::onIncomingCall)); return sendCheckReply("AT+CLIP=1", "OK"); } @@ -410,10 +428,13 @@ if (! sendCheckReply("AT+CMGF=1", "OK")) return -1; // ask how many sms are stored - - if (! sendParseReply("AT+CPMS?", "+CPMS: \"SM_P\",", &numsms) ) return -1; - - return numsms; + if (sendParseReply("AT+CPMS?", "\"SM\",", &numsms)) + return numsms; + if (sendParseReply("AT+CPMS?", "\"SM_P\",", &numsms)) + return numsms; + + return -1; + } // Reading SMS's is a bit involved so we don't use helpers that may cause delays or debug @@ -518,6 +539,42 @@ } /********* TIME **********************************************************/ +bool Adafruit_FONA::sendUSSD(char *ussdmsg, char *ussdbuff, uint16_t maxlen, uint16_t *readlen) { + if (! sendCheckReply("AT+CUSD=1", "OK")) return false; + + char sendcmd[30] = "AT+CUSD=1,\""; + strncpy(sendcmd+11, ussdmsg, 30-11-2); // 11 bytes beginning, 2 bytes for close quote + null + sendcmd[strlen(sendcmd)] = '\"'; + + if (! sendCheckReply(sendcmd, "OK")) { + *readlen = 0; + return false; + } else { + readline(10000); // read the +CUSD reply, wait up to 10 seconds!!! + //DEBUG_PRINT("* "); DEBUG_PRINTLN(replybuffer); + char *p = strstr(replybuffer, "+CUSD: "); + if (p == 0) { + *readlen = 0; + return false; + } + p+=7; //+CUSD + // Find " to get start of ussd message. + p = strchr(p, '\"'); + if (p == 0) { + *readlen = 0; + return false; + } + p+=1; //" + // Find " to get end of ussd message. + char *strend = strchr(p, '\"'); + + uint16_t lentocopy = min(maxlen-1, strend - p); + strncpy(ussdbuff, p, lentocopy+1); + ussdbuff[lentocopy] = 0; + *readlen = lentocopy; + } + return true; +} bool Adafruit_FONA::enableNetworkTimeSync(bool onoff) { if (onoff) { @@ -777,52 +834,79 @@ /********* GPRS **********************************************************/ +#define CIPSHUT_DELAY (uint16_t)65000 +#define CGATT_DELAY (uint16_t)10000 +#define SAPBR_DELAY (uint16_t)85000 +#define CIICR_DELAY (uint16_t)85000 bool Adafruit_FONA::enableGPRS(bool onoff) { if (onoff) { // disconnect all sockets - sendCheckReply("AT+CIPSHUT", "SHUT OK", 5000); + sendCheckReply("AT+CIPSHUT", "SHUT OK", CIPSHUT_DELAY); - if (! sendCheckReply("AT+CGATT=1", "OK", 10000)) + if (! sendCheckReply("AT+CGATT=1", "OK", CGATT_DELAY)) return false; // set bearer profile! connection type GPRS - if (! sendCheckReply("AT+SAPBR=3,1,\"CONTYPE\",\"GPRS\"", "OK", 10000)) + if (! sendCheckReply("AT+SAPBR=3,1,\"CONTYPE\",\"GPRS\"", "OK", SAPBR_DELAY)) return false; // set bearer profile access point name if (apn) { // Send command AT+SAPBR=3,1,"APN","<apn value>" where <apn value> is the configured APN value. - if (! sendCheckReplyQuoted("AT+SAPBR=3,1,\"APN\",", apn, "OK", 10000)) + if (! sendCheckReplyQuoted("AT+SAPBR=3,1,\"APN\",", apn, "OK", SAPBR_DELAY)) return false; + flushInput(); + + mySerial.printf("AT+CSTT=\""); + mySerial.printf(apn); + if (apnusername) { + mySerial.printf("\",\""); + mySerial.printf(apnusername); + } + if (apnpassword) { + mySerial.printf("\",\""); + mySerial.printf(apnpassword); + } + mySerial.printf("\""); + mySerial.printf("\r\n"); + + + + if (! expectReply("OK")) return false; + // set username/password if (apnusername) { // Send command AT+SAPBR=3,1,"USER","<user>" where <user> is the configured APN username. - if (! sendCheckReplyQuoted("AT+SAPBR=3,1,\"USER\",", apnusername, "OK", 10000)) + if (! sendCheckReplyQuoted("AT+SAPBR=3,1,\"USER\",", apnusername, "OK", SAPBR_DELAY)) return false; } if (apnpassword) { // Send command AT+SAPBR=3,1,"PWD","<password>" where <password> is the configured APN password. - if (! sendCheckReplyQuoted("AT+SAPBR=3,1,\"PWD\",", apnpassword, "OK", 10000)) + if (! sendCheckReplyQuoted("AT+SAPBR=3,1,\"PWD\",", apnpassword, "OK", SAPBR_DELAY)) return false; } } // open GPRS context - if (! sendCheckReply("AT+SAPBR=1,1", "OK", 10000)) + if (! sendCheckReply("AT+SAPBR=1,1", "OK", SAPBR_DELAY)) return false; + + if (! sendCheckReply("AT+CIICR", "OK", CIICR_DELAY)) + return false; + } else { // disconnect all sockets - if (! sendCheckReply("AT+CIPSHUT", "SHUT OK", 5000)) + if (! sendCheckReply("AT+CIPSHUT", "SHUT OK", CIPSHUT_DELAY)) return false; // close GPRS context - if (! sendCheckReply("AT+SAPBR=0,1", "OK", 10000)) + if (! sendCheckReply("AT+SAPBR=0,1", "OK", SAPBR_DELAY)) return false; - if (! sendCheckReply("AT+CGATT=0", "OK", 10000)) + if (! sendCheckReply("AT+CGATT=0", "OK", CGATT_DELAY)) return false; } return true; @@ -832,7 +916,7 @@ uint16_t state; if (! sendParseReply("AT+CGATT?", "+CGATT: ", &state) ) - return -1; + return 0xFF; return state; } @@ -844,7 +928,7 @@ } bool Adafruit_FONA::getGSMLoc(uint16_t *errorcode, char *buff, uint16_t maxlen) { - getReply("AT+CIPGSMLOC=1,1", (uint16_t)10000); + getReply("AT+CIPGSMLOC=1,1", CIPGSMLOC_DELAY); if (! parseReply("+CIPGSMLOC: ", errorcode)) return false; @@ -890,10 +974,10 @@ flushInput(); // close all old connections - if (! sendCheckReply("AT+CIPSHUT", "SHUT OK", 5000) ) return false; + if (! sendCheckReply("AT+CIPSHUT", "SHUT OK", CIPSHUT_DELAY) ) return false; // single connection at a time - if (! sendCheckReply("AT+CIPMUX=0", "OK") ) return false; + if (! sendCheckReply("AT+CIPMUX=0", "OK",CPIMUX_DELAY) ) return false; // manually read data if (! sendCheckReply("AT+CIPRXGET=1", "OK") ) return false; @@ -914,7 +998,7 @@ } bool Adafruit_FONA::TCPconnected(void) { - if (! sendCheckReply("AT+CIPSTATUS", "OK", 100) ) return false; + if (! sendCheckReply("AT+CIPSTATUS", "OK") ) return false; readline(100); #ifdef ADAFRUIT_FONA_DEBUG printf("\t<--- %s\r\n", replybuffer); @@ -1329,7 +1413,8 @@ bool Adafruit_FONA::sendCheckReply(const char *send, const char *reply, uint16_t timeout) { - getReply(send, timeout); + if (! getReply(send, timeout) ) + return false; return (strcmp(replybuffer, reply) == 0); } @@ -1444,4 +1529,4 @@ readline(); // eat 'OK' return true; -} \ No newline at end of file +}