Version working with mbed-os

Dependents:  

Fork of Adafruit_FONA_Library by Jesse van Rhijn

Committer:
zathorix
Date:
Wed May 24 09:19:56 2017 +0000
Revision:
8:aacbc647caee
Parent:
7:cb4c50c42edb
Changed some print statements

Who changed what in which revision?

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