A library for talking to Multi-Tech's Cellular SocketModem Devices.

Dependents:   M2X_dev axeda_wrapper_dev MTS_M2x_Example1 MTS_Cellular_Connect_Example ... more

Committer:
jengbrecht
Date:
Tue Dec 31 16:34:22 2013 +0000
Revision:
106:358972176b89
Parent:
103:da58d27c15d7
Child:
108:554585370b4a
In Wifi added the ability to get and set the Device's IP as static or DHCP.  Also moved the DHCP setting to the init function for default setting. Added some more documentation.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
jengbrecht 69:f3e696bbb0d5 1 #ifndef WIFI_H
jengbrecht 69:f3e696bbb0d5 2 #define WIFI_H
jengbrecht 69:f3e696bbb0d5 3
jengbrecht 69:f3e696bbb0d5 4 #include "IPStack.h"
jengbrecht 69:f3e696bbb0d5 5 #include "MTSBufferedIO.h"
jengbrecht 69:f3e696bbb0d5 6 #include "mbed.h"
jengbrecht 69:f3e696bbb0d5 7 #include <string>
jengbrecht 69:f3e696bbb0d5 8
jengbrecht 69:f3e696bbb0d5 9 using namespace mts;
jengbrecht 69:f3e696bbb0d5 10
jengbrecht 69:f3e696bbb0d5 11 class Wifi : public IPStack
jengbrecht 69:f3e696bbb0d5 12 {
jengbrecht 69:f3e696bbb0d5 13 public:
jengbrecht 103:da58d27c15d7 14 ///An enumeration for all the supported WiFi security types.
jengbrecht 69:f3e696bbb0d5 15 enum SecurityType {
jengbrecht 69:f3e696bbb0d5 16 NONE, WEP64, WEP128, WPA, WPA2
jengbrecht 69:f3e696bbb0d5 17 };
jengbrecht 69:f3e696bbb0d5 18
jengbrecht 103:da58d27c15d7 19 /** Destructs a Wifi object and frees all related resources.
jengbrecht 103:da58d27c15d7 20 */
jengbrecht 69:f3e696bbb0d5 21 ~Wifi();
jengbrecht 69:f3e696bbb0d5 22
jengbrecht 103:da58d27c15d7 23 /** This static function is used to create or get a reference to a
jengbrecht 103:da58d27c15d7 24 * Wifi object. Wifi uses the singleton pattern, which means
jengbrecht 103:da58d27c15d7 25 * that you can only have one existing at a time. The first time you
jengbrecht 103:da58d27c15d7 26 * call getInstance this method creates a new uninitialized Wifi
jengbrecht 103:da58d27c15d7 27 * object and returns it. All future calls to this method will return
jengbrecht 103:da58d27c15d7 28 * a reference to the instance created during the first call. Note that
jengbrecht 103:da58d27c15d7 29 * you must call init on the returned instance before mnaking any other
jengbrecht 103:da58d27c15d7 30 * calls. If using this class's bindings to any of the Socket package
jengbrecht 103:da58d27c15d7 31 * classes like TCPSocketConnection, you must call this method and the
jengbrecht 103:da58d27c15d7 32 * init method on the returned object first, before even creating the
jengbrecht 103:da58d27c15d7 33 * other objects.
jengbrecht 103:da58d27c15d7 34 *
jengbrecht 103:da58d27c15d7 35 * @returns a reference to the single Wifi obect that has been created.
jengbrecht 103:da58d27c15d7 36 */
jengbrecht 69:f3e696bbb0d5 37 static Wifi* getInstance();
jengbrecht 103:da58d27c15d7 38
jengbrecht 103:da58d27c15d7 39 /** This method initializes the object with the underlying Wifi module
jengbrecht 103:da58d27c15d7 40 * interface to use. Note that this function MUST be called before
jengbrecht 103:da58d27c15d7 41 * any other calls will function correctly on a Wifi object. Also
jengbrecht 103:da58d27c15d7 42 * note that MTSBufferedIO is abstract, so you must use one of
jengbrecht 103:da58d27c15d7 43 * its inherited classes like MTSSerial or MTSSerialFlowControl.
jengbrecht 103:da58d27c15d7 44 *
jengbrecht 103:da58d27c15d7 45 * @param io the buffered io interface that is attached to the wifi
jengbrecht 103:da58d27c15d7 46 * radio module.
jengbrecht 103:da58d27c15d7 47 * @returns true if the init was successful, otherwise false.
jengbrecht 103:da58d27c15d7 48 */
jengbrecht 69:f3e696bbb0d5 49 bool init(MTSBufferedIO* io);
jengbrecht 69:f3e696bbb0d5 50
jengbrecht 103:da58d27c15d7 51 /** This method establishes a network connection on the Wif radio module.
jengbrecht 103:da58d27c15d7 52 * Note that before calling you NEED to first set the network information
jengbrecht 103:da58d27c15d7 53 * including WiFi SSID and optional security key using the setNetwork
jengbrecht 103:da58d27c15d7 54 * method.
jengbrecht 103:da58d27c15d7 55 *
jengbrecht 103:da58d27c15d7 56 * @returns true if the connection was successfully established, otherwise
jengbrecht 103:da58d27c15d7 57 * false on an error.
jengbrecht 103:da58d27c15d7 58 */
jengbrecht 69:f3e696bbb0d5 59 virtual bool connect();
jengbrecht 103:da58d27c15d7 60
jengbrecht 103:da58d27c15d7 61 /** This method is used to stop a previously established Wifi network connection.
jengbrecht 103:da58d27c15d7 62 */
jengbrecht 69:f3e696bbb0d5 63 virtual void disconnect();
jengbrecht 103:da58d27c15d7 64
jengbrecht 103:da58d27c15d7 65 /** This method is used to check if the radio currently has a Wifi network
jengbrecht 103:da58d27c15d7 66 * connection established.
jengbrecht 103:da58d27c15d7 67 *
jengbrecht 103:da58d27c15d7 68 * @returns true if a network connection exists, otherwise false.
jengbrecht 103:da58d27c15d7 69 */
jengbrecht 69:f3e696bbb0d5 70 virtual bool isConnected();
jengbrecht 69:f3e696bbb0d5 71
jengbrecht 69:f3e696bbb0d5 72 // TCP and UDP Socket related commands
jengbrecht 69:f3e696bbb0d5 73 // For behavior of the following methods refer to IPStack.h documentation
jengbrecht 69:f3e696bbb0d5 74 virtual bool bind(unsigned int port);
jengbrecht 69:f3e696bbb0d5 75 virtual bool open(const std::string& address, unsigned int port, Mode mode);
jengbrecht 69:f3e696bbb0d5 76 virtual bool isOpen();
jengbrecht 69:f3e696bbb0d5 77 virtual bool close();
jengbrecht 69:f3e696bbb0d5 78 virtual int read(char* data, int max, int timeout = -1);
jengbrecht 69:f3e696bbb0d5 79 virtual int write(const char* data, int length, int timeout = -1);
jengbrecht 69:f3e696bbb0d5 80 virtual unsigned int readable();
jengbrecht 69:f3e696bbb0d5 81 virtual unsigned int writeable();
jengbrecht 69:f3e696bbb0d5 82
jengbrecht 69:f3e696bbb0d5 83 virtual void reset();
jengbrecht 69:f3e696bbb0d5 84
jengbrecht 106:358972176b89 85 /** A method for sending a generic text command to the radio. Note that you cannot
jengbrecht 106:358972176b89 86 * send commands and have a socket connection at the same time, unless you first
jengbrecht 106:358972176b89 87 * switch to command mode.
jengbrecht 106:358972176b89 88 *
jengbrecht 106:358972176b89 89 * @param command the command to send to the WiFi module without the escape character.
jengbrecht 106:358972176b89 90 * @param timeoutMillis the time in millis to wait for a response before returning.
jengbrecht 106:358972176b89 91 * @param response the text string to look for and to return immediately after finding.
jengbrecht 106:358972176b89 92 * The default is to look for no specific response.
jengbrecht 106:358972176b89 93 * @param esc escape character to add at the end of the command, defaults to
jengbrecht 106:358972176b89 94 * carriage return (CR). Does not append any character if esc == 0.
jengbrecht 106:358972176b89 95 * @returns all data received from the radio after the command as a string.
jengbrecht 106:358972176b89 96 */
jengbrecht 93:aa7a48e65974 97 std::string sendCommand(std::string command, int timeoutMillis, std::string response = "", char esc = CR);
jengbrecht 106:358972176b89 98
jengbrecht 106:358972176b89 99 /** A method for sending a basic command to the radio. A basic text command is
jengbrecht 106:358972176b89 100 * one that simply has a response of either AOK or ERR without any other information.
jengbrecht 106:358972176b89 101 * Note that you cannot send commands and have a tcp connection at the same time
jengbrecht 106:358972176b89 102 * unless you first switch to command mode.
jengbrecht 106:358972176b89 103 *
jengbrecht 106:358972176b89 104 * @param command the command to send to the WiFi module without the escape character.
jengbrecht 106:358972176b89 105 * @param timeoutMillis the time in millis to wait for a response before returning.
jengbrecht 106:358972176b89 106 * @param esc escape character to add at the end of the command, defaults to
jengbrecht 106:358972176b89 107 * carriage return (CR).
jengbrecht 106:358972176b89 108 * @returns the standard Code enumeration.
jengbrecht 106:358972176b89 109 */
sgodinez 73:bb5bbca971ae 110 Code sendBasicCommand(std::string command, int timeoutMillis, char esc = CR);
jengbrecht 69:f3e696bbb0d5 111
jengbrecht 103:da58d27c15d7 112 /** This method is used to set the network information details. This method must be
jengbrecht 103:da58d27c15d7 113 * called before connect, which establishes the WiFi network connection.
jengbrecht 103:da58d27c15d7 114 *
jengbrecht 103:da58d27c15d7 115 * @param ssid the SSID for the network you want to attached to.
jengbrecht 103:da58d27c15d7 116 * @param type the type of security used on the network. The default is NONE.
jengbrecht 103:da58d27c15d7 117 * @param key the security key for the network. The default is no key.
jengbrecht 103:da58d27c15d7 118 */
jengbrecht 103:da58d27c15d7 119 Code setNetwork(const std::string& ssid, SecurityType type = NONE, const std::string& key = "");
jengbrecht 106:358972176b89 120
jengbrecht 106:358972176b89 121 /** This method is used to set the IP address or puts the module in DHCP mode.
jengbrecht 106:358972176b89 122 *
jengbrecht 106:358972176b89 123 * @param address the IP address you want to use in the form of xxx.xxx.xxx.xxx or DHCP
jengbrecht 106:358972176b89 124 * if you want to use DHCP. The default is DHCP.
jengbrecht 106:358972176b89 125 * @returns the standard Code enumeration.
jengbrecht 106:358972176b89 126 */
jengbrecht 106:358972176b89 127 Code setDeviceIP(std::string address = "DHCP");
jengbrecht 106:358972176b89 128
jengbrecht 106:358972176b89 129 /** This method is used to get the IP address of the device, which can be
jengbrecht 106:358972176b89 130 * set either statically or via DHCP after connecting to a network.
jengbrecht 106:358972176b89 131 *
jengbrecht 106:358972176b89 132 * @returns the devices IP address.
jengbrecht 106:358972176b89 133 */
jengbrecht 106:358972176b89 134 std::string getDeviceIP();
jengbrecht 103:da58d27c15d7 135
jengbrecht 103:da58d27c15d7 136 /** This method is used to set the DNS which enables the use of URLs instead
jengbrecht 103:da58d27c15d7 137 * of IP addresses when making a socket connection.
jengbrecht 103:da58d27c15d7 138 *
jengbrecht 103:da58d27c15d7 139 * @param the DNS server address as a string in form xxx.xxx.xxx.xxx.
jengbrecht 103:da58d27c15d7 140 * @returns the standard AT Code enumeration.
jengbrecht 103:da58d27c15d7 141 */
jengbrecht 94:1baa587e89ae 142 Code setDNS(const std::string& dnsName);
jengbrecht 103:da58d27c15d7 143
jengbrecht 103:da58d27c15d7 144 /** A method for getting the signal strength of the Wifi module. This method allows
jengbrecht 103:da58d27c15d7 145 * you to get the signal strength in dBm. If you get a result of 99 the signal strength
jengbrecht 103:da58d27c15d7 146 * is not known or there was an error in reading it. Note that you cannot read the signal
jengbrecht 103:da58d27c15d7 147 * strength unless you are already attached to a Wifi network.
jengbrecht 103:da58d27c15d7 148 *
jengbrecht 103:da58d27c15d7 149 * @returns an integer representing the signal strength in dBm.
jengbrecht 103:da58d27c15d7 150 */
jengbrecht 69:f3e696bbb0d5 151 int getSignalStrength();
jengbrecht 103:da58d27c15d7 152
jengbrecht 103:da58d27c15d7 153 /** This method is used test network connectivity by pinging a server.
jengbrecht 103:da58d27c15d7 154 *
jengbrecht 103:da58d27c15d7 155 * @param address the address of the server in format xxx.xxx.xxx.xxx.
jengbrecht 103:da58d27c15d7 156 * @returns true if the ping was successful, otherwise false.
jengbrecht 103:da58d27c15d7 157 */
jengbrecht 95:4fdf968b5b37 158 bool ping(const std::string& address = "8.8.8.8");
jengbrecht 103:da58d27c15d7 159
jengbrecht 103:da58d27c15d7 160 /** This method is used to set whether the device is in command mode or data mode.
jengbrecht 103:da58d27c15d7 161 * In command mode you are able to send configuration and status commands while
jengbrecht 103:da58d27c15d7 162 * data mode is used for sending data when you have an open socket connection.
jengbrecht 103:da58d27c15d7 163 * Note that for all other methods in this class the change is handled automatically.
jengbrecht 103:da58d27c15d7 164 * Only use this methodif you want to send your own commands that are not already
jengbrecht 103:da58d27c15d7 165 * supported and need to make sure that you are in command mode.
jengbrecht 103:da58d27c15d7 166 *
jengbrecht 103:da58d27c15d7 167 * @param on if true sets to command mode, otherwise to data mode.
jengbrecht 103:da58d27c15d7 168 * @returns true if the change was successful, otherwise false.
jengbrecht 103:da58d27c15d7 169 */
jengbrecht 79:f356009dbc12 170 bool setCmdMode(bool on);
jengbrecht 69:f3e696bbb0d5 171
jengbrecht 69:f3e696bbb0d5 172 private:
jengbrecht 69:f3e696bbb0d5 173 static Wifi* instance; //Static pointer to the single Cellular object.
jengbrecht 69:f3e696bbb0d5 174
jengbrecht 69:f3e696bbb0d5 175 MTSBufferedIO* io; //IO interface obect that the radio is accessed through.
jengbrecht 69:f3e696bbb0d5 176
jengbrecht 69:f3e696bbb0d5 177 bool wifiConnected; //Specifies if a Wifi network session is currently connected.
jengbrecht 69:f3e696bbb0d5 178 std::string _ssid; //A string that holds the SSID for the Wifi module.
jengbrecht 69:f3e696bbb0d5 179
jengbrecht 69:f3e696bbb0d5 180 Mode mode; //The current socket Mode.
jengbrecht 69:f3e696bbb0d5 181 bool socketOpened; //Specifies if a Socket is presently opened.
jengbrecht 69:f3e696bbb0d5 182 bool socketCloseable; //Specifies is a Socket can be closed.
jengbrecht 69:f3e696bbb0d5 183 unsigned int local_port; //Holds the local port for socket connections.
jengbrecht 69:f3e696bbb0d5 184 std::string local_address; //Holds the local address for socket connections.
jengbrecht 69:f3e696bbb0d5 185 unsigned int host_port; //Holds the remote port for socket connections.
jengbrecht 69:f3e696bbb0d5 186 std::string host_address; //Holds the remote address for socket connections.
jengbrecht 103:da58d27c15d7 187 bool cmdOn; //Determines whether the device is in command mode or not
jengbrecht 69:f3e696bbb0d5 188
jengbrecht 69:f3e696bbb0d5 189 Wifi(); //Private constructor, use the getInstance() method.
jengbrecht 69:f3e696bbb0d5 190 Wifi(MTSBufferedIO* io); //Private constructor, use the getInstance() method.
jengbrecht 69:f3e696bbb0d5 191 };
jengbrecht 69:f3e696bbb0d5 192
jengbrecht 69:f3e696bbb0d5 193 #endif /* WIFI_H */