Crispin Mukalay / Adafruit_FONA_SIMCOM_Library

Dependents:   GPS_SIMCOM_SIM808

Fork of Adafruit_FONA_Library by Marc Plouhinec

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