Adafruit GSM library

Dependents:   GSM

Committer:
ptcrews
Date:
Sat Dec 05 07:22:00 2015 +0000
Revision:
0:13b14c71d29e
Adafruit GSM library

Who changed what in which revision?

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