Library for the Adafruit FONA. This is a port of the original Arduino library available at: https://github.com/adafruit/Adafruit_FONA_Library .

Dependents:   Adafruit_FONA_Library_FONAtest

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers Adafruit_FONA.h Source File

Adafruit_FONA.h

00001 /***************************************************
00002   This is a library for our Adafruit FONA Cellular Module
00003 
00004   Designed specifically to work with the Adafruit FONA
00005   ----> http://www.adafruit.com/products/1946
00006   ----> http://www.adafruit.com/products/1963
00007 
00008   These displays use TTL Serial to communicate, 2 pins are required to
00009   interface
00010   Adafruit invests time and resources providing this open source code,
00011   please support Adafruit and open-source hardware by purchasing
00012   products from Adafruit!
00013 
00014   Written by Limor Fried/Ladyada for Adafruit Industries.
00015   BSD license, all text above must be included in any redistribution
00016  ****************************************************/
00017  
00018  /*
00019   *  Modified by Marc PLOUHINEC 27/06/2015 for use in mbed
00020   */
00021  
00022 #ifndef ADAFRUIT_FONA_H
00023 #define ADAFRUIT_FONA_H
00024 
00025 #include "mbed.h"
00026 
00027 //#define ADAFRUIT_FONA_DEBUG
00028 
00029 #define FONA_HEADSETAUDIO 0
00030 #define FONA_EXTAUDIO 1
00031 
00032 #define FONA_STTONE_DIALTONE 1
00033 #define FONA_STTONE_BUSY 2
00034 #define FONA_STTONE_CONGESTION 3
00035 #define FONA_STTONE_PATHACK 4
00036 #define FONA_STTONE_DROPPED 5
00037 #define FONA_STTONE_ERROR 6
00038 #define FONA_STTONE_CALLWAIT 7
00039 #define FONA_STTONE_RINGING 8
00040 #define FONA_STTONE_BEEP 16
00041 #define FONA_STTONE_POSTONE 17
00042 #define FONA_STTONE_ERRTONE 18
00043 #define FONA_STTONE_INDIANDIALTONE 19
00044 #define FONA_STTONE_USADIALTONE 20
00045 
00046 #define FONA_DEFAULT_TIMEOUT_MS 500
00047 
00048 #define FONA_HTTP_GET   0
00049 #define FONA_HTTP_POST  1
00050 #define FONA_HTTP_HEAD  2 
00051 
00052 #define RX_BUFFER_SIZE  255
00053 
00054 class Adafruit_FONA : public Stream {
00055     public:
00056         /**
00057          * Listener for FONA events.
00058          */
00059         class EventListener {
00060             public:
00061                 /**
00062                  * Method called when somebody call the FONA.
00063                  */
00064                 virtual void onRing() = 0;
00065                 
00066                 /**
00067                  * Method called when the calling person stop his call.
00068                  */
00069                 virtual void onNoCarrier() = 0;
00070         };
00071     
00072     public:
00073         Adafruit_FONA(PinName tx, PinName rx, PinName rst, PinName ringIndicator) :
00074             _rstpin(rst, false), _ringIndicatorInterruptIn(ringIndicator),
00075             apn("FONAnet"), apnusername(NULL), apnpassword(NULL), httpsredirect(false), useragent("FONA"),
00076             _incomingCall(false), eventListener(NULL), mySerial(tx, rx), rxBufferInIndex(0), rxBufferOutIndex(0), 
00077             currentReceivedLineSize(0) {}
00078         bool begin(int baudrate);
00079         void setEventListener(EventListener *eventListener);
00080         
00081         // Stream
00082         virtual int _putc(int value);
00083         virtual int _getc();
00084         int readable();
00085         
00086         // RTC
00087         bool enableRTC(uint8_t i); // i = 0 <=> disable, i = 1 <=> enable
00088         
00089         // Battery and ADC
00090         bool getADCVoltage(uint16_t *v);
00091         bool getBattPercent(uint16_t *p);
00092         bool getBattVoltage(uint16_t *v);
00093         
00094         // SIM query
00095         bool unlockSIM(char *pin);
00096         uint8_t getSIMCCID(char *ccid);
00097         uint8_t getNetworkStatus(void);
00098         uint8_t getRSSI(void);
00099         
00100         // IMEI
00101         uint8_t getIMEI(char *imei);
00102         
00103         // set Audio output
00104         bool setAudio(uint8_t a);
00105         bool setVolume(uint8_t i);
00106         uint8_t getVolume(void);
00107         bool playToolkitTone(uint8_t t, uint16_t len);
00108         bool setMicVolume(uint8_t a, uint8_t level);
00109         bool playDTMF(char tone);
00110         
00111         // FM radio functions
00112         bool tuneFMradio(uint16_t station);
00113         bool FMradio(bool onoff, uint8_t a = FONA_HEADSETAUDIO);
00114         bool setFMVolume(uint8_t i);
00115         int8_t getFMVolume();
00116         int8_t getFMSignalLevel(uint16_t station);
00117         
00118         // SMS handling
00119         bool setSMSInterrupt(uint8_t i);
00120         uint8_t getSMSInterrupt(void);
00121         int8_t getNumSMS(void);
00122         bool readSMS(uint8_t i, char *smsbuff, uint16_t max, uint16_t *readsize);
00123         bool sendSMS(char *smsaddr, char *smsmsg);
00124         bool deleteSMS(uint8_t i);
00125         bool getSMSSender(uint8_t i, char *sender, int senderlen);
00126         
00127         // Time
00128         bool enableNetworkTimeSync(bool onoff);
00129         bool enableNTPTimeSync(bool onoff, const char* ntpserver=0);
00130         bool getTime(char* buff, uint16_t maxlen);
00131         
00132         // GPRS handling
00133         bool enableGPRS(bool onoff);
00134         uint8_t GPRSstate(void);
00135         bool getGSMLoc(uint16_t *replycode, char *buff, uint16_t maxlen);
00136         bool getGSMLoc(float *lat, float *lon);
00137         void setGPRSNetworkSettings(const char* apn, const char* username=0, const char* password=0);
00138 
00139         // GPS handling
00140         bool enableGPS(bool onoff);
00141         int8_t GPSstatus(void);
00142         uint8_t getGPS(uint8_t arg, char *buffer, uint8_t maxbuff);
00143         bool getGPS(float *lat, float *lon, float *speed_kph=0, float *heading=0, float *altitude=0);
00144         bool enableGPSNMEA(uint8_t nmea);
00145         
00146         // TCP raw connections
00147         bool TCPconnect(char *server, uint16_t port);
00148         bool TCPclose(void);
00149         bool TCPconnected(void);
00150         bool TCPsend(char *packet, uint8_t len);
00151         uint16_t TCPavailable(void);
00152         uint16_t TCPread(uint8_t *buff, uint8_t len);
00153         
00154         // HTTP low level interface (maps directly to SIM800 commands).
00155         bool HTTP_init();
00156         bool HTTP_term();
00157         void HTTP_para_start(const char* parameter, bool quoted = true);
00158         bool HTTP_para_end(bool quoted = true);
00159         bool HTTP_para(const char* parameter, const char *value);
00160         bool HTTP_para(const char* parameter, int32_t value);
00161         bool HTTP_data(uint32_t size, uint32_t maxTime=10000);
00162         bool HTTP_action(uint8_t method, uint16_t *status, uint16_t *datalen, int32_t timeout = 10000);
00163         bool HTTP_readall(uint16_t *datalen);
00164         bool HTTP_ssl(bool onoff);
00165         
00166         // HTTP high level interface (easier to use, less flexible).
00167         bool HTTP_GET_start(char *url, uint16_t *status, uint16_t *datalen);
00168         void HTTP_GET_end(void);
00169         bool HTTP_POST_start(char *url, const char* contenttype, const uint8_t *postdata, uint16_t postdatalen, uint16_t *status, uint16_t *datalen);
00170         void HTTP_POST_end(void);
00171         void setUserAgent(const char* useragent);
00172         
00173         // HTTPS
00174         void setHTTPSRedirect(bool onoff);
00175         
00176         // PWM (buzzer)
00177         bool setPWM(uint16_t period, uint8_t duty = 50);
00178         
00179         // Phone calls
00180         bool callPhone(char *phonenum);
00181         bool hangUp(void);
00182         bool pickUp(void);
00183         bool callerIdNotification(bool enable);
00184         bool incomingCallNumber(char* phonenum);
00185         
00186         // Helper functions to verify responses.
00187         bool expectReply(const char* reply, uint16_t timeout = 10000);
00188         
00189     private:
00190         DigitalOut _rstpin;
00191         InterruptIn _ringIndicatorInterruptIn;
00192         
00193         char replybuffer[255];
00194         char* apn;
00195         char* apnusername;
00196         char* apnpassword;
00197         bool httpsredirect;
00198         char* useragent;
00199         
00200         volatile bool _incomingCall;
00201         EventListener *eventListener;
00202         Serial mySerial;
00203         
00204         // Circular buffer used to receive serial data from an interruption
00205         int rxBuffer[RX_BUFFER_SIZE + 1];
00206         volatile int rxBufferInIndex; // Index where new data is added to the buffer
00207         volatile int rxBufferOutIndex; // Index where data is removed from the buffer
00208         char currentReceivedLine[RX_BUFFER_SIZE]; // Array containing the current received line
00209         int currentReceivedLineSize;
00210         
00211         inline bool isRxBufferFull() {
00212             return ((rxBufferInIndex + 1) % RX_BUFFER_SIZE) == rxBufferOutIndex;
00213         }
00214         
00215         inline bool isRxBufferEmpty() {
00216             return rxBufferInIndex == rxBufferOutIndex;
00217         }
00218         
00219         inline void incrementRxBufferInIndex() {
00220             rxBufferInIndex = (rxBufferInIndex + 1) % RX_BUFFER_SIZE;
00221         }
00222         
00223         inline void incrementRxBufferOutIndex() {
00224             rxBufferOutIndex = (rxBufferOutIndex + 1) % RX_BUFFER_SIZE;
00225         }
00226         
00227         /**
00228          * Method called when Serial data is received (interrupt routine).
00229          */
00230         void onSerialDataReceived();
00231         
00232         // HTTP helpers
00233         bool HTTP_setup(char *url);
00234         
00235         void flushInput();
00236         uint16_t readRaw(uint16_t b);
00237         uint8_t readline(uint16_t timeout = FONA_DEFAULT_TIMEOUT_MS, bool multiline = false);
00238         uint8_t getReply(const char* send, uint16_t timeout = FONA_DEFAULT_TIMEOUT_MS);
00239         uint8_t getReply(const char* prefix, char *suffix, uint16_t timeout = FONA_DEFAULT_TIMEOUT_MS);
00240         uint8_t getReply(const char* prefix, int32_t suffix, uint16_t timeout = FONA_DEFAULT_TIMEOUT_MS);
00241         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.
00242         uint8_t getReplyQuoted(const char* prefix, const char* suffix, uint16_t timeout = FONA_DEFAULT_TIMEOUT_MS);
00243         
00244         bool sendCheckReply(const char* send, const char* reply, uint16_t timeout = FONA_DEFAULT_TIMEOUT_MS);
00245         bool sendCheckReply(const char* prefix, char *suffix, const char* reply, uint16_t timeout = FONA_DEFAULT_TIMEOUT_MS);
00246         bool sendCheckReply(const char* prefix, int32_t suffix, const char* reply, uint16_t timeout = FONA_DEFAULT_TIMEOUT_MS);
00247         bool sendCheckReply(const char* prefix, int32_t suffix, int32_t suffix2, const char* reply, uint16_t timeout = FONA_DEFAULT_TIMEOUT_MS);
00248         bool sendCheckReplyQuoted(const char* prefix, const char* suffix, const char* reply, uint16_t timeout = FONA_DEFAULT_TIMEOUT_MS);
00249         
00250         bool parseReply(const char* toreply, uint16_t *v, char divider  = ',', uint8_t index=0);
00251         bool parseReply(const char* toreply, char *v, char divider  = ',', uint8_t index=0);
00252         bool parseReplyQuoted(const char* toreply, char* v, int maxlen, char divider, uint8_t index);
00253         
00254         bool sendParseReply(const char* tosend, const char* toreply, uint16_t *v, char divider = ',', uint8_t index = 0);
00255         
00256         void onIncomingCall();
00257 };
00258 
00259 #endif