Update to work with latest mBed

Dependencies:   mbed-dev

Fork of FONA_CellPhone by Dream Team

Committer:
George windoge T
Date:
Thu Mar 10 01:53:00 2016 -0500
Revision:
2:aaef20ac9044
Parent:
0:851248a56628
Child:
9:06eca688f2a3
GT: commented some of the code

Who changed what in which revision?

UserRevisionLine numberNew contents of line
gtzintzarov3 0:851248a56628 1 /***************************************************
gtzintzarov3 0:851248a56628 2 This is a library for our Adafruit FONA Cellular Module
gtzintzarov3 0:851248a56628 3
gtzintzarov3 0:851248a56628 4 Designed specifically to work with the Adafruit FONA
gtzintzarov3 0:851248a56628 5 ----> http://www.adafruit.com/products/1946
gtzintzarov3 0:851248a56628 6 ----> http://www.adafruit.com/products/1963
gtzintzarov3 0:851248a56628 7
gtzintzarov3 0:851248a56628 8 These displays use TTL Serial to communicate, 2 pins are required to
gtzintzarov3 0:851248a56628 9 interface
gtzintzarov3 0:851248a56628 10 Adafruit invests time and resources providing this open source code,
gtzintzarov3 0:851248a56628 11 please support Adafruit and open-source hardware by purchasing
gtzintzarov3 0:851248a56628 12 products from Adafruit!
gtzintzarov3 0:851248a56628 13
gtzintzarov3 0:851248a56628 14 Written by Limor Fried/Ladyada for Adafruit Industries.
gtzintzarov3 0:851248a56628 15 BSD license, all text above must be included in any redistribution
gtzintzarov3 0:851248a56628 16 ****************************************************/
gtzintzarov3 0:851248a56628 17
gtzintzarov3 0:851248a56628 18 /*
gtzintzarov3 0:851248a56628 19 * Modified by Marc PLOUHINEC 27/06/2015 for use in mbed
gtzintzarov3 0:851248a56628 20 */
gtzintzarov3 0:851248a56628 21
gtzintzarov3 0:851248a56628 22 #ifndef ADAFRUIT_FONA_H
gtzintzarov3 0:851248a56628 23 #define ADAFRUIT_FONA_H
gtzintzarov3 0:851248a56628 24
gtzintzarov3 0:851248a56628 25 #include "mbed.h"
gtzintzarov3 0:851248a56628 26
gtzintzarov3 0:851248a56628 27 //#define ADAFRUIT_FONA_DEBUG
gtzintzarov3 0:851248a56628 28
gtzintzarov3 0:851248a56628 29 #define FONA_HEADSETAUDIO 0
gtzintzarov3 0:851248a56628 30 #define FONA_EXTAUDIO 1
gtzintzarov3 0:851248a56628 31
gtzintzarov3 0:851248a56628 32 #define FONA_STTONE_DIALTONE 1
gtzintzarov3 0:851248a56628 33 #define FONA_STTONE_BUSY 2
gtzintzarov3 0:851248a56628 34 #define FONA_STTONE_CONGESTION 3
gtzintzarov3 0:851248a56628 35 #define FONA_STTONE_PATHACK 4
gtzintzarov3 0:851248a56628 36 #define FONA_STTONE_DROPPED 5
gtzintzarov3 0:851248a56628 37 #define FONA_STTONE_ERROR 6
gtzintzarov3 0:851248a56628 38 #define FONA_STTONE_CALLWAIT 7
gtzintzarov3 0:851248a56628 39 #define FONA_STTONE_RINGING 8
gtzintzarov3 0:851248a56628 40 #define FONA_STTONE_BEEP 16
gtzintzarov3 0:851248a56628 41 #define FONA_STTONE_POSTONE 17
gtzintzarov3 0:851248a56628 42 #define FONA_STTONE_ERRTONE 18
gtzintzarov3 0:851248a56628 43 #define FONA_STTONE_INDIANDIALTONE 19
gtzintzarov3 0:851248a56628 44 #define FONA_STTONE_USADIALTONE 20
gtzintzarov3 0:851248a56628 45
George windoge T 2:aaef20ac9044 46 #define FONA_DEFAULT_TIMEOUT_MS 500 //timeout between send AT and reply from FONA
gtzintzarov3 0:851248a56628 47
gtzintzarov3 0:851248a56628 48 #define FONA_HTTP_GET 0
gtzintzarov3 0:851248a56628 49 #define FONA_HTTP_POST 1
gtzintzarov3 0:851248a56628 50 #define FONA_HTTP_HEAD 2
gtzintzarov3 0:851248a56628 51
gtzintzarov3 0:851248a56628 52 #define RX_BUFFER_SIZE 255
gtzintzarov3 0:851248a56628 53
gtzintzarov3 0:851248a56628 54 class Adafruit_FONA : public Stream {
gtzintzarov3 0:851248a56628 55 public:
gtzintzarov3 0:851248a56628 56 /**
gtzintzarov3 0:851248a56628 57 * Listener for FONA events.
gtzintzarov3 0:851248a56628 58 */
gtzintzarov3 0:851248a56628 59 class EventListener {
gtzintzarov3 0:851248a56628 60 public:
gtzintzarov3 0:851248a56628 61 /**
gtzintzarov3 0:851248a56628 62 * Method called when somebody call the FONA.
gtzintzarov3 0:851248a56628 63 */
gtzintzarov3 0:851248a56628 64 virtual void onRing() = 0;
gtzintzarov3 0:851248a56628 65
gtzintzarov3 0:851248a56628 66 /**
gtzintzarov3 0:851248a56628 67 * Method called when the calling person stop his call.
gtzintzarov3 0:851248a56628 68 */
gtzintzarov3 0:851248a56628 69 virtual void onNoCarrier() = 0;
gtzintzarov3 0:851248a56628 70 };
gtzintzarov3 0:851248a56628 71
gtzintzarov3 0:851248a56628 72 public:
gtzintzarov3 0:851248a56628 73 Adafruit_FONA(PinName tx, PinName rx, PinName rst, PinName ringIndicator) :
gtzintzarov3 0:851248a56628 74 _rstpin(rst, false), _ringIndicatorInterruptIn(ringIndicator),
gtzintzarov3 0:851248a56628 75 apn("FONAnet"), apnusername(NULL), apnpassword(NULL), httpsredirect(false), useragent("FONA"),
gtzintzarov3 0:851248a56628 76 _incomingCall(false), eventListener(NULL), mySerial(tx, rx), rxBufferInIndex(0), rxBufferOutIndex(0),
gtzintzarov3 0:851248a56628 77 currentReceivedLineSize(0) {}
gtzintzarov3 0:851248a56628 78 bool begin(int baudrate);
gtzintzarov3 0:851248a56628 79 void setEventListener(EventListener *eventListener);
gtzintzarov3 0:851248a56628 80
gtzintzarov3 0:851248a56628 81 // Stream
gtzintzarov3 0:851248a56628 82 virtual int _putc(int value);
gtzintzarov3 0:851248a56628 83 virtual int _getc();
gtzintzarov3 0:851248a56628 84 int readable();
gtzintzarov3 0:851248a56628 85
gtzintzarov3 0:851248a56628 86 // RTC
gtzintzarov3 0:851248a56628 87 bool enableRTC(uint8_t i); // i = 0 <=> disable, i = 1 <=> enable
gtzintzarov3 0:851248a56628 88
gtzintzarov3 0:851248a56628 89 // Battery and ADC
gtzintzarov3 0:851248a56628 90 bool getADCVoltage(uint16_t *v);
gtzintzarov3 0:851248a56628 91 bool getBattPercent(uint16_t *p);
gtzintzarov3 0:851248a56628 92 bool getBattVoltage(uint16_t *v);
gtzintzarov3 0:851248a56628 93
gtzintzarov3 0:851248a56628 94 // SIM query
gtzintzarov3 0:851248a56628 95 bool unlockSIM(char *pin);
gtzintzarov3 0:851248a56628 96 uint8_t getSIMCCID(char *ccid);
gtzintzarov3 0:851248a56628 97 uint8_t getNetworkStatus(void);
gtzintzarov3 0:851248a56628 98 uint8_t getRSSI(void);
gtzintzarov3 0:851248a56628 99
gtzintzarov3 0:851248a56628 100 // IMEI
gtzintzarov3 0:851248a56628 101 uint8_t getIMEI(char *imei);
gtzintzarov3 0:851248a56628 102
gtzintzarov3 0:851248a56628 103 // set Audio output
gtzintzarov3 0:851248a56628 104 bool setAudio(uint8_t a);
gtzintzarov3 0:851248a56628 105 bool setVolume(uint8_t i);
gtzintzarov3 0:851248a56628 106 uint8_t getVolume(void);
gtzintzarov3 0:851248a56628 107 bool playToolkitTone(uint8_t t, uint16_t len);
gtzintzarov3 0:851248a56628 108 bool setMicVolume(uint8_t a, uint8_t level);
gtzintzarov3 0:851248a56628 109 bool playDTMF(char tone);
gtzintzarov3 0:851248a56628 110
gtzintzarov3 0:851248a56628 111 // FM radio functions
gtzintzarov3 0:851248a56628 112 bool tuneFMradio(uint16_t station);
gtzintzarov3 0:851248a56628 113 bool FMradio(bool onoff, uint8_t a = FONA_HEADSETAUDIO);
gtzintzarov3 0:851248a56628 114 bool setFMVolume(uint8_t i);
gtzintzarov3 0:851248a56628 115 int8_t getFMVolume();
gtzintzarov3 0:851248a56628 116 int8_t getFMSignalLevel(uint16_t station);
gtzintzarov3 0:851248a56628 117
gtzintzarov3 0:851248a56628 118 // SMS handling
gtzintzarov3 0:851248a56628 119 bool setSMSInterrupt(uint8_t i);
gtzintzarov3 0:851248a56628 120 uint8_t getSMSInterrupt(void);
gtzintzarov3 0:851248a56628 121 int8_t getNumSMS(void);
gtzintzarov3 0:851248a56628 122 bool readSMS(uint8_t i, char *smsbuff, uint16_t max, uint16_t *readsize);
gtzintzarov3 0:851248a56628 123 bool sendSMS(char *smsaddr, char *smsmsg);
gtzintzarov3 0:851248a56628 124 bool deleteSMS(uint8_t i);
gtzintzarov3 0:851248a56628 125 bool getSMSSender(uint8_t i, char *sender, int senderlen);
gtzintzarov3 0:851248a56628 126
gtzintzarov3 0:851248a56628 127 // Time
gtzintzarov3 0:851248a56628 128 bool enableNetworkTimeSync(bool onoff);
gtzintzarov3 0:851248a56628 129 bool enableNTPTimeSync(bool onoff, const char* ntpserver=0);
gtzintzarov3 0:851248a56628 130 bool getTime(char* buff, uint16_t maxlen);
gtzintzarov3 0:851248a56628 131
gtzintzarov3 0:851248a56628 132 // GPRS handling
gtzintzarov3 0:851248a56628 133 bool enableGPRS(bool onoff);
gtzintzarov3 0:851248a56628 134 uint8_t GPRSstate(void);
gtzintzarov3 0:851248a56628 135 bool getGSMLoc(uint16_t *replycode, char *buff, uint16_t maxlen);
gtzintzarov3 0:851248a56628 136 bool getGSMLoc(float *lat, float *lon);
gtzintzarov3 0:851248a56628 137 void setGPRSNetworkSettings(const char* apn, const char* username=0, const char* password=0);
gtzintzarov3 0:851248a56628 138
gtzintzarov3 0:851248a56628 139 // GPS handling
gtzintzarov3 0:851248a56628 140 bool enableGPS(bool onoff);
gtzintzarov3 0:851248a56628 141 int8_t GPSstatus(void);
gtzintzarov3 0:851248a56628 142 uint8_t getGPS(uint8_t arg, char *buffer, uint8_t maxbuff);
gtzintzarov3 0:851248a56628 143 bool getGPS(float *lat, float *lon, float *speed_kph=0, float *heading=0, float *altitude=0);
gtzintzarov3 0:851248a56628 144 bool enableGPSNMEA(uint8_t nmea);
gtzintzarov3 0:851248a56628 145
gtzintzarov3 0:851248a56628 146 // TCP raw connections
gtzintzarov3 0:851248a56628 147 bool TCPconnect(char *server, uint16_t port);
gtzintzarov3 0:851248a56628 148 bool TCPclose(void);
gtzintzarov3 0:851248a56628 149 bool TCPconnected(void);
gtzintzarov3 0:851248a56628 150 bool TCPsend(char *packet, uint8_t len);
gtzintzarov3 0:851248a56628 151 uint16_t TCPavailable(void);
gtzintzarov3 0:851248a56628 152 uint16_t TCPread(uint8_t *buff, uint8_t len);
gtzintzarov3 0:851248a56628 153
gtzintzarov3 0:851248a56628 154 // HTTP low level interface (maps directly to SIM800 commands).
gtzintzarov3 0:851248a56628 155 bool HTTP_init();
gtzintzarov3 0:851248a56628 156 bool HTTP_term();
gtzintzarov3 0:851248a56628 157 void HTTP_para_start(const char* parameter, bool quoted = true);
gtzintzarov3 0:851248a56628 158 bool HTTP_para_end(bool quoted = true);
gtzintzarov3 0:851248a56628 159 bool HTTP_para(const char* parameter, const char *value);
gtzintzarov3 0:851248a56628 160 bool HTTP_para(const char* parameter, int32_t value);
gtzintzarov3 0:851248a56628 161 bool HTTP_data(uint32_t size, uint32_t maxTime=10000);
gtzintzarov3 0:851248a56628 162 bool HTTP_action(uint8_t method, uint16_t *status, uint16_t *datalen, int32_t timeout = 10000);
gtzintzarov3 0:851248a56628 163 bool HTTP_readall(uint16_t *datalen);
gtzintzarov3 0:851248a56628 164 bool HTTP_ssl(bool onoff);
gtzintzarov3 0:851248a56628 165
gtzintzarov3 0:851248a56628 166 // HTTP high level interface (easier to use, less flexible).
gtzintzarov3 0:851248a56628 167 bool HTTP_GET_start(char *url, uint16_t *status, uint16_t *datalen);
gtzintzarov3 0:851248a56628 168 void HTTP_GET_end(void);
gtzintzarov3 0:851248a56628 169 bool HTTP_POST_start(char *url, const char* contenttype, const uint8_t *postdata, uint16_t postdatalen, uint16_t *status, uint16_t *datalen);
gtzintzarov3 0:851248a56628 170 void HTTP_POST_end(void);
gtzintzarov3 0:851248a56628 171 void setUserAgent(const char* useragent);
gtzintzarov3 0:851248a56628 172
gtzintzarov3 0:851248a56628 173 // HTTPS
gtzintzarov3 0:851248a56628 174 void setHTTPSRedirect(bool onoff);
gtzintzarov3 0:851248a56628 175
gtzintzarov3 0:851248a56628 176 // PWM (buzzer)
gtzintzarov3 0:851248a56628 177 bool setPWM(uint16_t period, uint8_t duty = 50);
gtzintzarov3 0:851248a56628 178
gtzintzarov3 0:851248a56628 179 // Phone calls
gtzintzarov3 0:851248a56628 180 bool callPhone(char *phonenum);
gtzintzarov3 0:851248a56628 181 bool hangUp(void);
gtzintzarov3 0:851248a56628 182 bool pickUp(void);
gtzintzarov3 0:851248a56628 183 bool callerIdNotification(bool enable);
gtzintzarov3 0:851248a56628 184 bool incomingCallNumber(char* phonenum);
gtzintzarov3 0:851248a56628 185
gtzintzarov3 0:851248a56628 186 // Helper functions to verify responses.
gtzintzarov3 0:851248a56628 187 bool expectReply(const char* reply, uint16_t timeout = 10000);
gtzintzarov3 0:851248a56628 188
gtzintzarov3 0:851248a56628 189 private:
gtzintzarov3 0:851248a56628 190 DigitalOut _rstpin;
gtzintzarov3 0:851248a56628 191 InterruptIn _ringIndicatorInterruptIn;
gtzintzarov3 0:851248a56628 192
George windoge T 2:aaef20ac9044 193 char replybuffer[255]; // the output of getreply(), readline() is the function that changes the replybuffer
gtzintzarov3 0:851248a56628 194 char* apn;
gtzintzarov3 0:851248a56628 195 char* apnusername;
gtzintzarov3 0:851248a56628 196 char* apnpassword;
gtzintzarov3 0:851248a56628 197 bool httpsredirect;
gtzintzarov3 0:851248a56628 198 char* useragent;
gtzintzarov3 0:851248a56628 199
gtzintzarov3 0:851248a56628 200 volatile bool _incomingCall;
gtzintzarov3 0:851248a56628 201 EventListener *eventListener;
gtzintzarov3 0:851248a56628 202 Serial mySerial;
gtzintzarov3 0:851248a56628 203
gtzintzarov3 0:851248a56628 204 // Circular buffer used to receive serial data from an interruption
gtzintzarov3 0:851248a56628 205 int rxBuffer[RX_BUFFER_SIZE + 1];
gtzintzarov3 0:851248a56628 206 volatile int rxBufferInIndex; // Index where new data is added to the buffer
gtzintzarov3 0:851248a56628 207 volatile int rxBufferOutIndex; // Index where data is removed from the buffer
gtzintzarov3 0:851248a56628 208 char currentReceivedLine[RX_BUFFER_SIZE]; // Array containing the current received line
gtzintzarov3 0:851248a56628 209 int currentReceivedLineSize;
gtzintzarov3 0:851248a56628 210
gtzintzarov3 0:851248a56628 211 inline bool isRxBufferFull() {
gtzintzarov3 0:851248a56628 212 return ((rxBufferInIndex + 1) % RX_BUFFER_SIZE) == rxBufferOutIndex;
gtzintzarov3 0:851248a56628 213 }
gtzintzarov3 0:851248a56628 214
gtzintzarov3 0:851248a56628 215 inline bool isRxBufferEmpty() {
gtzintzarov3 0:851248a56628 216 return rxBufferInIndex == rxBufferOutIndex;
gtzintzarov3 0:851248a56628 217 }
gtzintzarov3 0:851248a56628 218
gtzintzarov3 0:851248a56628 219 inline void incrementRxBufferInIndex() {
gtzintzarov3 0:851248a56628 220 rxBufferInIndex = (rxBufferInIndex + 1) % RX_BUFFER_SIZE;
gtzintzarov3 0:851248a56628 221 }
gtzintzarov3 0:851248a56628 222
gtzintzarov3 0:851248a56628 223 inline void incrementRxBufferOutIndex() {
gtzintzarov3 0:851248a56628 224 rxBufferOutIndex = (rxBufferOutIndex + 1) % RX_BUFFER_SIZE;
gtzintzarov3 0:851248a56628 225 }
gtzintzarov3 0:851248a56628 226
gtzintzarov3 0:851248a56628 227 /**
gtzintzarov3 0:851248a56628 228 * Method called when Serial data is received (interrupt routine).
gtzintzarov3 0:851248a56628 229 */
gtzintzarov3 0:851248a56628 230 void onSerialDataReceived();
gtzintzarov3 0:851248a56628 231
gtzintzarov3 0:851248a56628 232 // HTTP helpers
gtzintzarov3 0:851248a56628 233 bool HTTP_setup(char *url);
gtzintzarov3 0:851248a56628 234
gtzintzarov3 0:851248a56628 235 void flushInput();
gtzintzarov3 0:851248a56628 236 uint16_t readRaw(uint16_t b);
gtzintzarov3 0:851248a56628 237 uint8_t readline(uint16_t timeout = FONA_DEFAULT_TIMEOUT_MS, bool multiline = false);
gtzintzarov3 0:851248a56628 238 uint8_t getReply(const char* send, uint16_t timeout = FONA_DEFAULT_TIMEOUT_MS);
gtzintzarov3 0:851248a56628 239 uint8_t getReply(const char* prefix, char *suffix, uint16_t timeout = FONA_DEFAULT_TIMEOUT_MS);
gtzintzarov3 0:851248a56628 240 uint8_t getReply(const char* prefix, int32_t suffix, uint16_t timeout = FONA_DEFAULT_TIMEOUT_MS);
gtzintzarov3 0:851248a56628 241 uint8_t getReply(const char* prefix, int32_t suffix1, int32_t suffix2, uint16_t timeout); // Don't set default value or else function call is ambiguous.
gtzintzarov3 0:851248a56628 242 uint8_t getReplyQuoted(const char* prefix, const char* suffix, uint16_t timeout = FONA_DEFAULT_TIMEOUT_MS);
gtzintzarov3 0:851248a56628 243
gtzintzarov3 0:851248a56628 244 bool sendCheckReply(const char* send, const char* reply, uint16_t timeout = FONA_DEFAULT_TIMEOUT_MS);
gtzintzarov3 0:851248a56628 245 bool sendCheckReply(const char* prefix, char *suffix, const char* reply, uint16_t timeout = FONA_DEFAULT_TIMEOUT_MS);
gtzintzarov3 0:851248a56628 246 bool sendCheckReply(const char* prefix, int32_t suffix, const char* reply, uint16_t timeout = FONA_DEFAULT_TIMEOUT_MS);
gtzintzarov3 0:851248a56628 247 bool sendCheckReply(const char* prefix, int32_t suffix, int32_t suffix2, const char* reply, uint16_t timeout = FONA_DEFAULT_TIMEOUT_MS);
gtzintzarov3 0:851248a56628 248 bool sendCheckReplyQuoted(const char* prefix, const char* suffix, const char* reply, uint16_t timeout = FONA_DEFAULT_TIMEOUT_MS);
gtzintzarov3 0:851248a56628 249
gtzintzarov3 0:851248a56628 250 bool parseReply(const char* toreply, uint16_t *v, char divider = ',', uint8_t index=0);
gtzintzarov3 0:851248a56628 251 bool parseReply(const char* toreply, char *v, char divider = ',', uint8_t index=0);
gtzintzarov3 0:851248a56628 252 bool parseReplyQuoted(const char* toreply, char* v, int maxlen, char divider, uint8_t index);
gtzintzarov3 0:851248a56628 253
gtzintzarov3 0:851248a56628 254 bool sendParseReply(const char* tosend, const char* toreply, uint16_t *v, char divider = ',', uint8_t index = 0);
gtzintzarov3 0:851248a56628 255
gtzintzarov3 0:851248a56628 256 void onIncomingCall();
gtzintzarov3 0:851248a56628 257 };
gtzintzarov3 0:851248a56628 258
gtzintzarov3 0:851248a56628 259 #endif