3G Shield library, port for mbed. (from http://3gsa.org/ ) see: https://developer.mbed.org/users/phsfan/notebook/phsshield/

Dependents:   PHSShield_test PHSShield_connectTCP PHSShield_Twitter PHSShield_httpGET

Committer:
phsfan
Date:
Tue Mar 10 01:12:17 2015 +0000
Revision:
0:ca471c239d5f
1st port for mbed

Who changed what in which revision?

UserRevisionLine numberNew contents of line
phsfan 0:ca471c239d5f 1 //***********************************************************
phsfan 0:ca471c239d5f 2 // a3gs.h -- IEM 3G Sheild for Arduino Control Library
phsfan 0:ca471c239d5f 3 //
phsfan 0:ca471c239d5f 4 // History:
phsfan 0:ca471c239d5f 5 // R1.0 2012/10/06 1st Release for IEM 3G Shiled(Ver1.0)
phsfan 0:ca471c239d5f 6 // R1.1 2012/10/14 2nd Release(Operate by few memories)
phsfan 0:ca471c239d5f 7 // (modify sendSMS(),onSMSReceived(),httpGET(),httpPOST(),tweet(),connectTCP())
phsfan 0:ca471c239d5f 8 // R1.2 2012/10/28 Bug fix (httpGET(), httpPOST(), tweet(), availableSMS() and redSMS())
phsfan 0:ca471c239d5f 9 // R1.3 2013/01/01 Support "https" in httpGET() and httpPost()
phsfan 0:ca471c239d5f 10 // R1.4 2013/04/16 Bug fix (discardUntil() has never return)
phsfan 0:ca471c239d5f 11 // R2.0 2013/07/10 Change interface of TCP/IP and discardUntil(), some bugs fix
phsfan 0:ca471c239d5f 12 // a3gsMAX_TWEET_LENGTH set as 60 (bytes)
phsfan 0:ca471c239d5f 13 // R2.1 2013/08/17 Buf fix (discardUntil() has never return)
phsfan 0:ca471c239d5f 14 // R2.2 2013/10/27 Bug fix (read(res, reslength))
phsfan 0:ca471c239d5f 15 // R2.3 2014/07/06 Support binary data in read() and add new version read() function, bug fix write()
phsfan 0:ca471c239d5f 16 // R3.0 2014/08/30 Support for gw3g R2.0 and add follow functions:
phsfan 0:ca471c239d5f 17 // setAirplaneMode(), put(), get()
phsfan 0:ca471c239d5f 18 //
phsfan 0:ca471c239d5f 19 // Author:
phsfan 0:ca471c239d5f 20 // 3G Shield Alliance and Atushi Daikoku
phsfan 0:ca471c239d5f 21 //
phsfan 0:ca471c239d5f 22 // Notes:
phsfan 0:ca471c239d5f 23 // Lower compatible with Arduino GSM/GPRS Shield library.
phsfan 0:ca471c239d5f 24 // Notices as bellow:
phsfan 0:ca471c239d5f 25 // - Use SoftwareSerial library (RxD is D4, TxD is D5).
phsfan 0:ca471c239d5f 26 // - Use Interrupt 0(D2) for SMS arrival notification.
phsfan 0:ca471c239d5f 27 // - Use D6 and D7 are used for Power control.
phsfan 0:ca471c239d5f 28 // If you want to change UART baudrate then define "a3gsBAUDRATE"
phsfan 0:ca471c239d5f 29 // symbol before #include "a3gs.h" statement.
phsfan 0:ca471c239d5f 30 //***********************************************************
phsfan 0:ca471c239d5f 31 /* Modified by 2015 phsfan
phsfan 0:ca471c239d5f 32 * for ABIT PHS Shield on mbed
phsfan 0:ca471c239d5f 33 */
phsfan 0:ca471c239d5f 34
phsfan 0:ca471c239d5f 35 #ifndef _A3GS_H_
phsfan 0:ca471c239d5f 36 #define _A3GS_H_ 1
phsfan 0:ca471c239d5f 37
phsfan 0:ca471c239d5f 38 #include "mbed.h"
phsfan 0:ca471c239d5f 39
phsfan 0:ca471c239d5f 40 /*
phsfan 0:ca471c239d5f 41 Define constants
phsfan 0:ca471c239d5f 42 */
phsfan 0:ca471c239d5f 43 // for compatibility of GSM.h
phsfan 0:ca471c239d5f 44 #define ctrlz 26 // Ascii character for ctr+z. End of a SMS.
phsfan 0:ca471c239d5f 45 #define cr 13 // Ascii character for carriage return.
phsfan 0:ca471c239d5f 46 #define lf 10 // Ascii character for line feed.
phsfan 0:ca471c239d5f 47
phsfan 0:ca471c239d5f 48 // Basic constants
phsfan 0:ca471c239d5f 49 #define a3gsBAUDRATE 4800 // Default UART baudrate if "a3gsBAUDRATE" is undefined
phsfan 0:ca471c239d5f 50 #define a3gsDATE_SIZE 11 // date space size ("YYYY/MM/DD\0" format, used in getTime(), included '\0')
phsfan 0:ca471c239d5f 51 #define a3gsTIME_SIZE 9 // time space size ("HH:MM:SS\0" format(hour is 24h-way), used in getTime(), included '\0')
phsfan 0:ca471c239d5f 52 #define a3gsCS_ASCII 0 // SMS Text Char Code: ASCII (used in sendSMS())
phsfan 0:ca471c239d5f 53 #define a3gsCS_UNICODE 1 // SMS Text Char Code: UNICODE(Little Endian) (used in sendSMS())
phsfan 0:ca471c239d5f 54 #define a3gsIMEI_SIZE 16 // imei space size ("99..9\0" format, used in getIMEI(), included '\0')
phsfan 0:ca471c239d5f 55 #define a3gsDEFAULT_PORT 0 // Default port number(for httpGET() and httpPOST())
phsfan 0:ca471c239d5f 56
phsfan 0:ca471c239d5f 57 // Return values in general
phsfan 0:ca471c239d5f 58 #define a3gsSUCCESS 0
phsfan 0:ca471c239d5f 59 #define a3gsERROR (-1)
phsfan 0:ca471c239d5f 60
phsfan 0:ca471c239d5f 61 // Maximum lengths
phsfan 0:ca471c239d5f 62 #define a3gsMAX_VERSION_LENGTH 5 // Maximum bytes of Version number(used in getVersion())
phsfan 0:ca471c239d5f 63 #define a3gsMAX_SMS_LENGTH 100 // Maximum bytes of SMS message(used in sendSMS())
phsfan 0:ca471c239d5f 64 #define a3gsMAX_MSN_LENGTH 11 // Maximum bytes of Phone Number in Japan(used in readSMS())
phsfan 0:ca471c239d5f 65 #define a3gsMAX_PROFILE_NUMBER 16 // Profile number is 1..a3gsMAX_PROFILE_NUMBER
phsfan 0:ca471c239d5f 66 #define a3gsMAX_HOST_LENGTH 128 // Maximum length of host name
phsfan 0:ca471c239d5f 67 #define a3gsMAX_DATA_LENGTH 1024 // Maximum length of data(at read/write)
phsfan 0:ca471c239d5f 68 #define a3gsMAX_STORAGE_NUMBER 30 // Maximum number of storages
phsfan 0:ca471c239d5f 69 #define a3gsMAX_STORAGE_LENGTH 1023 // Maximum storages size(bytes)
phsfan 0:ca471c239d5f 70
phsfan 0:ca471c239d5f 71 #define a3gsMAX_URL_LENGTH 256 // Maximum length of URL(used in httpGET() and httpPOST())
phsfan 0:ca471c239d5f 72 #define a3gsMAX_RESULT_LENGTH 1024 // Maximum length of result(used in httpGET() and httpPOST())
phsfan 0:ca471c239d5f 73 #define a3gsMAX_HEADER_LENGTH 512 // Maximum length of header(used in httpPOST())
phsfan 0:ca471c239d5f 74 #define a3gsMAX_BODY_LENGTH 1024 // Maximum length of header(used in httpPOST())
phsfan 0:ca471c239d5f 75 #define a3gsMAX_TWEET_LENGTH 140 // Maximum length of Tweet message(used in tweet())
phsfan 0:ca471c239d5f 76
phsfan 0:ca471c239d5f 77 // Return values of getService()
phsfan 0:ca471c239d5f 78 #define a3gsSRV_NO 0 // Out of service
phsfan 0:ca471c239d5f 79 #define a3gsSRV_PS 1 // Data(packet) only
phsfan 0:ca471c239d5f 80 #define a3gsSRV_CS 2 // Voice only
phsfan 0:ca471c239d5f 81 #define a3gsSRV_BOTH 3 // Data and voice both
phsfan 0:ca471c239d5f 82
phsfan 0:ca471c239d5f 83 // Method of positioning by getPosition()
phsfan 0:ca471c239d5f 84 #define a3gsMPBASED 0 // GPS + AGPS
phsfan 0:ca471c239d5f 85 #define a3gsMPASSISTED 1 // AGPS
phsfan 0:ca471c239d5f 86 #define a3gsMPSTANDALONE 2 // GPS only
phsfan 0:ca471c239d5f 87
phsfan 0:ca471c239d5f 88 /*
phsfan 0:ca471c239d5f 89 Declare class
phsfan 0:ca471c239d5f 90 */
phsfan 0:ca471c239d5f 91 class A3GS
phsfan 0:ca471c239d5f 92 {
phsfan 0:ca471c239d5f 93 public:
phsfan 0:ca471c239d5f 94 enum A3GS_st_e { ERROR, IDLE, READY, TCPCONNECTEDCLIENT };
phsfan 0:ca471c239d5f 95
phsfan 0:ca471c239d5f 96 A3GS(PinName tx, PinName rx, PinName intin, PinName power, PinName reg, int baud = a3gsBAUDRATE);
phsfan 0:ca471c239d5f 97
phsfan 0:ca471c239d5f 98 // compatible methods with Arduino GSM/GPRS Shield library
phsfan 0:ca471c239d5f 99 int getStatus() { return _status; };
phsfan 0:ca471c239d5f 100 int begin(char* pin = 0);
phsfan 0:ca471c239d5f 101 int begin(char* pin, uint32_t baudrate);
phsfan 0:ca471c239d5f 102 int end(void);
phsfan 0:ca471c239d5f 103 int restart(char* pin = 0);
phsfan 0:ca471c239d5f 104 int start(char* pin = 0);
phsfan 0:ca471c239d5f 105 int shutdown(void);
phsfan 0:ca471c239d5f 106 //-- This library do not use "pin" parameter, so ignore it.
phsfan 0:ca471c239d5f 107 int getIMEI(char* imei);
phsfan 0:ca471c239d5f 108 int sendSMS(const char* to, const char* msg, int encode = a3gsCS_ASCII);
phsfan 0:ca471c239d5f 109 bool availableSMS(void);
phsfan 0:ca471c239d5f 110 int readSMS(char* msg, int msglength, char* number, int nlength);
phsfan 0:ca471c239d5f 111 int connectTCP(const char* server, int port);
phsfan 0:ca471c239d5f 112 int disconnectTCP();
phsfan 0:ca471c239d5f 113 int write(uint8_t c);
phsfan 0:ca471c239d5f 114 //--@R3.0 support binary data
phsfan 0:ca471c239d5f 115 int write(const char* str);
phsfan 0:ca471c239d5f 116 //--@R3.0 this function doesn't support binary data
phsfan 0:ca471c239d5f 117 int write(const uint8_t* buffer, size_t sz);
phsfan 0:ca471c239d5f 118 //--@R3.0 support binary data
phsfan 0:ca471c239d5f 119 int read(char* result, int resultlength); //@R2.3 - Leave for compatibility
phsfan 0:ca471c239d5f 120 //--@R3.0 change non-blocking mode and this function doesn't support binary data
phsfan 0:ca471c239d5f 121 int read(void); //@R2.0 Change
phsfan 0:ca471c239d5f 122 //--@R3.0 support binary data and change non-blocking mode
phsfan 0:ca471c239d5f 123 int read(uint8_t* buffer, size_t sz); //@R2.3 Add
phsfan 0:ca471c239d5f 124 //--@R3.0 support binary data and change non-blocking mode
phsfan 0:ca471c239d5f 125 //-- This library do not support tcp/ip server functions.
phsfan 0:ca471c239d5f 126 int httpGET(const char* server, uint16_t port, const char* path, char* result, int resultlength, bool ssled = false, const char* header = NULL);
phsfan 0:ca471c239d5f 127 //--@R3.0 add optional paramer "header"
phsfan 0:ca471c239d5f 128 int httpPOST(const char* server, uint16_t port, const char* path, const char* header, const char* body, char* result, int* resultlength, bool ssled = false);
phsfan 0:ca471c239d5f 129 //-- httpPOST() is not compatible parameters with Arduino GSM/GPRS Shield library
phsfan 0:ca471c239d5f 130 int tweet(const char* token, const char* msg);
phsfan 0:ca471c239d5f 131 //-- tweet() sends a message to twitter with the text "msg"and the token "token".
phsfan 0:ca471c239d5f 132 //-- Get the token from http://arduino-tweet.appspot.com/
phsfan 0:ca471c239d5f 133
phsfan 0:ca471c239d5f 134 //-- Extended methods of IEM 3G Shield --//
phsfan 0:ca471c239d5f 135 int onSMSReceived(void (*handler)(void));
phsfan 0:ca471c239d5f 136 int getLocation(int method, char* latitude, char* longitude);
phsfan 0:ca471c239d5f 137 int getServices(int& status);
phsfan 0:ca471c239d5f 138 int getRSSI(int& rssi);
phsfan 0:ca471c239d5f 139 int getTime(char* date, char* time);
phsfan 0:ca471c239d5f 140 int getTime2(uint32_t& seconds);
phsfan 0:ca471c239d5f 141 int getVersion(char *version);
phsfan 0:ca471c239d5f 142 int setDefaultProfile(int profileNum);
phsfan 0:ca471c239d5f 143 int getDefaultProfile(int* profileNum);
phsfan 0:ca471c239d5f 144 int setBaudrate(uint32_t baudrate);
phsfan 0:ca471c239d5f 145 int setLED(bool sw);
phsfan 0:ca471c239d5f 146 //--@R3.0 add three functions:
phsfan 0:ca471c239d5f 147 int setAirplaneMode(bool sw);
phsfan 0:ca471c239d5f 148 int put(int storageNum, uint8_t *buffer, size_t sz);
phsfan 0:ca471c239d5f 149 int get(int storageNum, uint8_t *buffer, size_t sz);
phsfan 0:ca471c239d5f 150 int updateProfile(const uint8_t *encryptedProfile, int sz);
phsfan 0:ca471c239d5f 151 int encryptString(const char *password, const char *s);
phsfan 0:ca471c239d5f 152
phsfan 0:ca471c239d5f 153 private:
phsfan 0:ca471c239d5f 154 int _status; // for Compatible with GSM.h
phsfan 0:ca471c239d5f 155 void sendCommand(const char* cmd);
phsfan 0:ca471c239d5f 156 void sendData(const char* data);
phsfan 0:ca471c239d5f 157 void discardUntil(const char match);
phsfan 0:ca471c239d5f 158 int getResult(char *buf, int *len, uint32_t timeout);
phsfan 0:ca471c239d5f 159 void handleINT0(void);
phsfan 0:ca471c239d5f 160
phsfan 0:ca471c239d5f 161 RawSerial iemSerial;
phsfan 0:ca471c239d5f 162 InterruptIn _intin;
phsfan 0:ca471c239d5f 163 DigitalOut _reg, _power;
phsfan 0:ca471c239d5f 164 };
phsfan 0:ca471c239d5f 165
phsfan 0:ca471c239d5f 166 #endif // _A3GS_H_
phsfan 0:ca471c239d5f 167