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 17:30:45 2013 +0000
Revision:
108:554585370b4a
Parent:
106:358972176b89
Child:
120:3051dd49fa3a
Made setDeviceIP more robust in Wifi

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