Simple detection for LE910-NA1 modules
Fork of MTS-Cellular by
Cellular/Cellular.h
- Committer:
- Mike Fiore
- Date:
- 2014-05-22
- Revision:
- 11:4e428f689069
- Parent:
- 10:c188cc05aed5
- Child:
- 19:f6261f1c3dd4
File content as of revision 11:4e428f689069:
#ifndef CELLULAR_H #define CELLULAR_H #include <string> #include <vector> #include "IPStack.h" #include "MTSBufferedIO.h" #include "CellUtils.h" namespace mts { /** This is a class for communicating with a Cellular radio device. * This class supports three main types of cellular radio interactions including: * configuration and status processing, SMS processing, and TCP/UDP Socket * data connections. This class also inherits from IPStack providing a common set of commands * for communication devices that support IP protocols. It is also integrated with the standard * mbed Sockets package and can therefore be used seamlessly with clients and services built * on top of this interface already within the mbed library. * * For all examples below please change Pin Names to match your hardware configuration. * It also assumes the use of RTS/CTS hardware handshaking using GPIOs. To disable this * you will need to change settings on the radio module and and use the MTSSerial class * instead of MTSSerialFlowControl. * * The following set of example code demonstrates how to send and receive configuration and * status AT commands with the radio, create a data connection and test it: * @code * #include "mbed.h" * #include "mtech.h" * * using namespace mts; * * main() { * //Setup serial interface to radio * MTSSerialFlowControl* serial = new MTSSerialFlowControl(YOUR_TXD, YOUR_RXD, YOUR_RTS, YOUR_CTS); * serial->baud(YOUR_BAUD); * * //Setup Cellular class - Select your radio type * Cellular* cellular = new UniversalIP(); * //Cellular cellular = new TelitIP(); * cellular->init(serial); * * //Run status and configuration commands * printf("\n\r////Start Status and Configuration Commands////\n\r"); * printf("Command Test: %s\n\r", getCodeNames(cellular->test()).c_str()); * wait(1); * printf("Signal Strength: %d\n\r", cellular->getSignalStrength()); * wait(1); * printf("Registration State: %s\n\r", Cellular::getRegistrationNames(cellular->getRegistration()).c_str()); * wait(1); * printf("Send Basic Command (AT): %s\n\r", getCodeNames(cellular->sendBasicCommand("AT", 1000)).c_str()); * wait(1); * printf("Send Command (AT+CSQ): %s\n\r", cellular->sendCommand("AT+CSQ", 1000).c_str()); * wait(1); * * //Start Test * printf("\n\r////Start Network Connectivity Test////\n\r"); * printf("Set APN: %s\n\r", getCodeNames(cellular->setApn("wap.cingular")).c_str()); //Use APN from service provider!!! * * //Setup a data connection * printf("Attempting to Connect, this may take some time...\n\r"); * while (!cellular->connect()) { * printf("Failed to connect... Trying again.\n\r"); * wait(1); * } * printf("Connected to the Network!\n\r"); * * //Try pinging default server "8.8.8.8" * printf("Ping Valid: %s\n\r", cellular->ping() ? "true" : "false"); * * //Perform HTTP transfer over TCP socket connection * printf("Open Connection: %s\n\r", cellular->open("echo.200please.com", 80, IPStack::TCP) ? "Success" : "Failure"); * printf("Performing HTTP GET on echo.200please.com\n\r"); * string httpGET = "GET / HTTP/1.1\r\nHost: echo.200please.com\r\nConnection: keep-alive\r\n\r\n"; * printf("Wrote %d of %d bytes\n\r", cellular->write(httpGET.data(), httpGET.size(), 2000), httpGET.size()); * char buffer[256]; * buffer[255] = '\0'; * printf("Read %d bytes\n\r", cellular->read(buffer, 255, 2000)); * printf("HTTP Response:\n\r %s", buffer); * wait(1); * printf("Close Connection: %s\n\r", cellular->close() ? "Success" : "Failure"); * * //Disconnect from network * printf("Disconnecting...\n\r"); * cellular->disconnect(); * printf("Is Connected: %s\n\r", cellular->isConnected() ? "True" : "False"); * * printf("End Program\n\r"); * } * @endcode * * The following set of example code demonstrates how process SMS messages: * @code * #include "mbed.h" * #include "mtech.h" * * using namespace mts; * * main() { * //Setup serial interface to radio * MTSSerialFlowControl* serial = new MTSSerialFlowControl(YOUR_TXD, YOUR_RXD, YOUR_RTS, YOUR_CTS); * serial->baud(YOUR_BAUD); * * //Setup Cellular class * //Setup Cellular class - Select your radio type * Cellular* cellular = new UniversalIP(); * //Cellular cellular = new TelitIP(); * cellular->init(serial); * * //Start test * printf("AT Test: %s\n\r", getCodeNames(cellular->test()).c_str()); * * //Waiting for network registration * printf("Checking Network Registration, this may take some time...\n\r"); * while (cellular->getRegistration() != Cellular::REGISTERED) { * printf("Still waiting... Checking again.\n\r"); * wait(1); * } * printf("Connected to the Network!\n\r"); * * //Send SMS Message * Code code; * std::string sMsg("Hello from Multi-Tech MBED!"); * std::string sPhoneNum("16128675309"); //Put your phone number here or leave Jenny's... * * printf("Sending message [%s] to [%s]\r\n", sMsg.c_str(), sPhoneNum.c_str()); * code = cellular->sendSMS(sPhoneNum, sMsg); * * if(code != SUCCESS) { * printf("Error during SMS send [%s]\r\n", getCodeNames(code).c_str()); * } else { * printf("Success!\r\n"); * } * * //Try and receive SMS messages * //To determine your radio's phone number send yourself an SMS and check the received # * printf("Checking Received Messages\r\n"); * while (true) { * std::vector<Cellular::Sms> vSms = cellular->getReceivedSms(); * printf("\r\n"); * for(unsigned int i = 0; i < vSms.size(); i++) { * printf("[%d][%s][%s][%s]\r\n", i, vSms[i].timestamp.c_str(), * vSms[i].phoneNumber.c_str(), vSms[i].message.c_str()); * } * wait(10); * } * } * @endcode */ class Cellular : public IPStack { public: // Class ping paramter constants static const unsigned int PINGDELAY = 3; //Time to wait on each ping for a response before timimg out (seconds) static const unsigned int PINGNUM = 4; //Number of pings to try on ping command /// Enumeration for different cellular radio types. enum Radio { NA, MTSMC_H5, MTSMC_EV3, MTSMC_G3, MTSMC_C2, MTSMC_H5_IP, MTSMC_EV3_IP, MTSMC_C2_IP }; /// An enumeration of radio registration states with a cell tower. enum Registration { NOT_REGISTERED, REGISTERED, SEARCHING, DENIED, UNKNOWN, ROAMING }; /** This structure contains the data for an SMS message. */ struct Sms { /// Message Phone Number std::string phoneNumber; /// Message Body std::string message; /// Message Timestamp std::string timestamp; }; /** This method initializes the object with the underlying radio * interface to use. Note that this function MUST be called before * any other calls will function correctly on a Cellular object. Also * note that MTSBufferedIO is abstract, so you must use one of * its inherited classes like MTSSerial, MTSSerialFlowControl or write a class * similar to MTSSerialFlowControl which maps the MTSBufferedIO API * to your favorite serial library. * * @param io the io interface that is attached to the cellular radio. * @param DCD this is the dcd signal from the radio. If attached the * the pin must be passed in here for this class to operate correctly. * The default is not connected. * @param DTR this is the dtr signal from the radio. If attached the * the pin must be passed in here for this class to operate correctly. * The default is not connected. * @returns true if the init was successful, otherwise false. */ virtual bool init(MTSBufferedIO* io); /** * */ bool configureSignals(unsigned int DCD = NC, unsigned int DTR = NC, unsigned int RESET = NC); /** A method for testing command access to the radio. This method sends the * command "AT" to the radio, which is a standard radio test to see if you * have command access to the radio. The function returns when it receives * the expected response from the radio. * * @returns the standard AT Code enumeration. */ virtual Code test(); /** A method for getting the signal strength of the radio. This method allows you to * get a value that maps to signal strength in dBm. Here 0-1 is Poor, 2-9 is Marginal, * 10-14 is Ok, 15-19 is Good, and 20+ is Excellent. If you get a result of 99 the * signal strength is not known or not detectable. * * @returns an integer representing the signal strength. */ virtual int getSignalStrength(); /** This method is used to check the registration state of the radio with the cell tower. * If not appropriatley registered with the tower you cannot make a cellular connection. * * @returns the registration state as an enumeration type. */ virtual Registration getRegistration(); /** This method is used to set the radios APN if using a SIM card. Note that the APN * must be set correctly before you can make a data connection. The APN for your SIM * can be obtained by contacting your cellular service provider. * * @param the APN as a string. * @returns the standard AT Code enumeration. */ virtual Code setApn(const std::string& apn); /** This method is used to set the DNS which enables the use of URLs instead * of IP addresses when making a socket connection. * * @param the DNS server address as a string in form xxx.xxx.xxx.xxx. * @returns the standard AT Code enumeration. */ virtual Code setDns(const std::string& primary, const std::string& secondary = "0.0.0.0"); /** This method is used to send an SMS message. Note that you cannot send an * SMS message and have a data connection open at the same time. * * @param phoneNumber the phone number to send the message to as a string. * @param message the text message to be sent. * @returns the standard AT Code enumeration. */ virtual Code sendSMS(const std::string& phoneNumber, const std::string& message); /** This method is used to send an SMS message. Note that you cannot send an * SMS message and have a data connection open at the same time. * * @param sms an Sms struct that contains all SMS transaction information. * @returns the standard AT Code enumeration. */ virtual Code sendSMS(const Sms& sms); /** This method retrieves all of the SMS messages currently available for * this phone number. * * @returns a vector of existing SMS messages each as an Sms struct. */ virtual std::vector<Cellular::Sms> getReceivedSms(); /** This method can be used to remove/delete all received SMS messages * even if they have never been retrieved or read. * * @returns the standard AT Code enumeration. */ virtual Code deleteAllReceivedSms(); /** This method can be used to remove/delete all received SMS messages * that have been retrieved by the user through the getReceivedSms method. * Messages that have not been retrieved yet will be unaffected. * * @returns the standard AT Code enumeration. */ virtual Code deleteOnlyReceivedReadSms(); //Cellular Radio Specific /** A method for sending a generic AT command to the radio. Note that you cannot * send commands and have a data connection at the same time. * * @param command the command to send to the radio without the escape character. * @param timeoutMillis the time in millis to wait for a response before returning. * @param esc escape character to add at the end of the command, defaults to * carriage return (CR). Does not append any character if esc == 0. * @returns all data received from the radio after the command as a string. */ virtual std::string sendCommand(const std::string& command, unsigned int timeoutMillis, char esc = CR); /** A method for sending a basic AT command to the radio. A basic AT command is * one that simply has a response of either OK or ERROR without any other information. * Note that you cannot send commands and have a data connection at the same time. * * @param command the command to send to the radio without the escape character. * @param timeoutMillis the time in millis to wait for a response before returning. * @param esc escape character to add at the end of the command, defaults to * carriage return (CR). * @returns the standard Code enumeration. */ virtual Code sendBasicCommand(const std::string& command, unsigned int timeoutMillis, char esc = CR); /** A static method for getting a string representation for the Registration * enumeration. * * @param code a Registration enumeration. * @returns the enumeration name as a string. */ static std::string getRegistrationNames(Registration registration); /** A static method for getting a string representation for the Radio * enumeration. * * @param type a Radio enumeration. * @returns the enumeration name as a string. */ static std::string getRadioNames(Radio radio); protected: MTSBufferedIO* io; //IO interface obect that the radio is accessed through. bool echoMode; //Specifies if the echo mode is currently enabled. bool pppConnected; //Specifies if a PPP session is currently connected. std::string apn; //A string that holds the APN for the radio. Radio type; //The type of radio being used Mode mode; //The current socket Mode. bool socketOpened; //Specifies if a Socket is presently opened. bool socketCloseable; //Specifies is a Socket can be closed. unsigned int local_port; //Holds the local port for socket connections. std::string local_address; //Holds the local address for socket connections. unsigned int host_port; //Holds the remote port for socket connections. std::string host_address; //Holds the remote address for socket connections. DigitalIn* dcd; //Maps to the radio's dcd signal DigitalOut* dtr; //Maps to the radio's dtr signal DigitalOut* resetLine; //Maps to the radio's reset signal }; } #endif /* CELLULAR_H */