Dependencies:   FRDM_MMA8451Q KL46Z-USBHost MAG3110 SocketModem TSI mbed FATFileSystem

Fork of AxedaGo-Freescal_FRDM-KL46Z by Axeda Corp

Committer:
AxedaCorp
Date:
Tue Jul 01 21:31:54 2014 +0000
Revision:
0:65004368569c
Made initial

Who changed what in which revision?

UserRevisionLine numberNew contents of line
AxedaCorp 0:65004368569c 1 /* Universal Socket Modem Interface Library
AxedaCorp 0:65004368569c 2 * Copyright (c) 2013 Multi-Tech Systems
AxedaCorp 0:65004368569c 3 *
AxedaCorp 0:65004368569c 4 * Licensed under the Apache License, Version 2.0 (the "License");
AxedaCorp 0:65004368569c 5 * you may not use this file except in compliance with the License.
AxedaCorp 0:65004368569c 6 * You may obtain a copy of the License at
AxedaCorp 0:65004368569c 7 *
AxedaCorp 0:65004368569c 8 * http://www.apache.org/licenses/LICENSE-2.0
AxedaCorp 0:65004368569c 9 *
AxedaCorp 0:65004368569c 10 * Unless required by applicable law or agreed to in writing, software
AxedaCorp 0:65004368569c 11 * distributed under the License is distributed on an "AS IS" BASIS,
AxedaCorp 0:65004368569c 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
AxedaCorp 0:65004368569c 13 * See the License for the specific language governing permissions and
AxedaCorp 0:65004368569c 14 * limitations under the License.
AxedaCorp 0:65004368569c 15 */
AxedaCorp 0:65004368569c 16
AxedaCorp 0:65004368569c 17 #ifndef WIFI_H
AxedaCorp 0:65004368569c 18 #define WIFI_H
AxedaCorp 0:65004368569c 19
AxedaCorp 0:65004368569c 20 #include "IPStack.h"
AxedaCorp 0:65004368569c 21 #include "MTSBufferedIO.h"
AxedaCorp 0:65004368569c 22 #include "mbed.h"
AxedaCorp 0:65004368569c 23 #include <string>
AxedaCorp 0:65004368569c 24 #include <vector>
AxedaCorp 0:65004368569c 25
AxedaCorp 0:65004368569c 26 using namespace mts;
AxedaCorp 0:65004368569c 27
AxedaCorp 0:65004368569c 28 /** This is a class for communicating with a Roving Networks RN-171 Wifi module. This
AxedaCorp 0:65004368569c 29 * module comes in a variety of form factors including the Multi-Tech SocketShield.
AxedaCorp 0:65004368569c 30 * This class supports two main types of WiFi module interactions including:
AxedaCorp 0:65004368569c 31 * configuration and status command processing and TCP Socket
AxedaCorp 0:65004368569c 32 * data connections. It should be noted that while a data connection is open the module
AxedaCorp 0:65004368569c 33 * must be put in command mode before commands can be sent. This is handled within the class
AxedaCorp 0:65004368569c 34 * automatically for all native commands. This class also inherits from IPStack
AxedaCorp 0:65004368569c 35 * providing a common set of commands for communication devices that have an onboard
AxedaCorp 0:65004368569c 36 * IP Stack. It is also integrated with the standard mbed Sockets package and can therefore
AxedaCorp 0:65004368569c 37 * be used seamlessly with clients and services built on top of this interface already within
AxedaCorp 0:65004368569c 38 * the mbed library.
AxedaCorp 0:65004368569c 39 *
AxedaCorp 0:65004368569c 40 * All of the following examples use the Pin Names for the Freedom KL46Z board coupled with
AxedaCorp 0:65004368569c 41 * the SocketModem Shield Arduino compatible board. Please chage Pin Names accordingly to
AxedaCorp 0:65004368569c 42 * match your hardware configuration. The default baud rate for the WiFi module is 9600 bps.
AxedaCorp 0:65004368569c 43 *
AxedaCorp 0:65004368569c 44 * The following example shows how to connect to a WiFi netork and perform a basic ping test:
AxedaCorp 0:65004368569c 45 * @code
AxedaCorp 0:65004368569c 46 * #include "mbed.h"
AxedaCorp 0:65004368569c 47 * #include "MTSSerial.h"
AxedaCorp 0:65004368569c 48 * #include "Wifi.h"
AxedaCorp 0:65004368569c 49 * using namespace mts;
AxedaCorp 0:65004368569c 50 *
AxedaCorp 0:65004368569c 51 * int main()
AxedaCorp 0:65004368569c 52 * {
AxedaCorp 0:65004368569c 53 * std::string ssid = "Your SSID goes here";
AxedaCorp 0:65004368569c 54 * std::string securityKey = "Your secuirty key goes here";
AxedaCorp 0:65004368569c 55 * Wifi::SecurityType securityType = Wifi::WPA2;
AxedaCorp 0:65004368569c 56 *
AxedaCorp 0:65004368569c 57 * //Wait for wifi module to boot up
AxedaCorp 0:65004368569c 58 * for (int i = 10; i >= 0; i = i - 2) {
AxedaCorp 0:65004368569c 59 * wait(2);
AxedaCorp 0:65004368569c 60 * printf("Waiting %d seconds...\n\r", i);
AxedaCorp 0:65004368569c 61 * }
AxedaCorp 0:65004368569c 62 *
AxedaCorp 0:65004368569c 63 * //Setup serial interface to WiFi module
AxedaCorp 0:65004368569c 64 * MTSSerial* serial = new MTSSerial(PTD3, PTD2, 256, 256);
AxedaCorp 0:65004368569c 65 * serial->baud(9600);
AxedaCorp 0:65004368569c 66 *
AxedaCorp 0:65004368569c 67 * //Setup Wifi class
AxedaCorp 0:65004368569c 68 * Wifi* wifi = Wifi::getInstance();
AxedaCorp 0:65004368569c 69 * printf("Init: %s\n\r", wifi->init(serial) ? "SUCCESS" : "FAILURE");
AxedaCorp 0:65004368569c 70 *
AxedaCorp 0:65004368569c 71 * //Setup and check connection
AxedaCorp 0:65004368569c 72 * printf("Set Network: %s\n\r", getCodeNames(wifi->setNetwork(ssid, securityType, securityKey)).c_str());
AxedaCorp 0:65004368569c 73 * printf("Set DHCP: %s\n\r", getCodeNames(wifi->setDeviceIP("DHCP")).c_str());
AxedaCorp 0:65004368569c 74 * printf("Connect: %s\n\r", wifi->connect() ? "Success" : "Failure");
AxedaCorp 0:65004368569c 75 * printf("Is Connected: %s\n\r", wifi->isConnected() ? "True" : "False");
AxedaCorp 0:65004368569c 76 * printf("Ping Server: %s\n\r", wifi->ping("8.8.8.8") ? "Success" : "Failed");
AxedaCorp 0:65004368569c 77 *
AxedaCorp 0:65004368569c 78 * //Disconnect from network
AxedaCorp 0:65004368569c 79 * printf("Disconnecting...\n\r");
AxedaCorp 0:65004368569c 80 * wifi->disconnect();
AxedaCorp 0:65004368569c 81 * printf("Is Connected: %s\n\r", wifi->isConnected() ? "True" : "False");
AxedaCorp 0:65004368569c 82 *
AxedaCorp 0:65004368569c 83 * printf("End Program\n\r");
AxedaCorp 0:65004368569c 84 * }
AxedaCorp 0:65004368569c 85 * @endcode
AxedaCorp 0:65004368569c 86 */
AxedaCorp 0:65004368569c 87 class Wifi : public IPStack
AxedaCorp 0:65004368569c 88 {
AxedaCorp 0:65004368569c 89 public:
AxedaCorp 0:65004368569c 90 ///An enumeration for all the supported WiFi security types.
AxedaCorp 0:65004368569c 91 enum SecurityType {
AxedaCorp 0:65004368569c 92 NONE, WEP64, WEP128, WPA, WPA2
AxedaCorp 0:65004368569c 93 };
AxedaCorp 0:65004368569c 94
AxedaCorp 0:65004368569c 95 /** Destructs a Wifi object and frees all related resources.
AxedaCorp 0:65004368569c 96 */
AxedaCorp 0:65004368569c 97 ~Wifi();
AxedaCorp 0:65004368569c 98
AxedaCorp 0:65004368569c 99 /** This static function is used to create or get a reference to a
AxedaCorp 0:65004368569c 100 * Wifi object. Wifi uses the singleton pattern, which means
AxedaCorp 0:65004368569c 101 * that you can only have one existing at a time. The first time you
AxedaCorp 0:65004368569c 102 * call getInstance this method creates a new uninitialized Wifi
AxedaCorp 0:65004368569c 103 * object and returns it. All future calls to this method will return
AxedaCorp 0:65004368569c 104 * a reference to the instance created during the first call. Note that
AxedaCorp 0:65004368569c 105 * you must call init on the returned instance before mnaking any other
AxedaCorp 0:65004368569c 106 * calls. If using this class's bindings to any of the Socket package
AxedaCorp 0:65004368569c 107 * classes like TCPSocketConnection, you must call this method and the
AxedaCorp 0:65004368569c 108 * init method on the returned object first, before even creating the
AxedaCorp 0:65004368569c 109 * other objects.
AxedaCorp 0:65004368569c 110 *
AxedaCorp 0:65004368569c 111 * @returns a reference to the single Wifi obect that has been created.
AxedaCorp 0:65004368569c 112 */
AxedaCorp 0:65004368569c 113 static Wifi* getInstance();
AxedaCorp 0:65004368569c 114
AxedaCorp 0:65004368569c 115 /** This method initializes the object with the underlying Wifi module
AxedaCorp 0:65004368569c 116 * interface to use. Note that this function MUST be called before
AxedaCorp 0:65004368569c 117 * any other calls will function correctly on a Wifi object. Also
AxedaCorp 0:65004368569c 118 * note that MTSBufferedIO is abstract, so you must use one of
AxedaCorp 0:65004368569c 119 * its inherited classes like MTSSerial or MTSSerialFlowControl.
AxedaCorp 0:65004368569c 120 *
AxedaCorp 0:65004368569c 121 * @param io the buffered io interface that is attached to the wifi
AxedaCorp 0:65004368569c 122 * radio module.
AxedaCorp 0:65004368569c 123 * @returns true if the init was successful, otherwise false.
AxedaCorp 0:65004368569c 124 */
AxedaCorp 0:65004368569c 125 bool init(MTSBufferedIO* io);
AxedaCorp 0:65004368569c 126
AxedaCorp 0:65004368569c 127 /** This method establishes a network connection on the Wif radio module.
AxedaCorp 0:65004368569c 128 * Note that before calling you NEED to first set the network information
AxedaCorp 0:65004368569c 129 * including WiFi SSID and optional security key using the setNetwork
AxedaCorp 0:65004368569c 130 * method.
AxedaCorp 0:65004368569c 131 *
AxedaCorp 0:65004368569c 132 * @returns true if the connection was successfully established, otherwise
AxedaCorp 0:65004368569c 133 * false on an error.
AxedaCorp 0:65004368569c 134 */
AxedaCorp 0:65004368569c 135 virtual bool connect();
AxedaCorp 0:65004368569c 136
AxedaCorp 0:65004368569c 137 /** This method is used to stop a previously established Wifi network connection.
AxedaCorp 0:65004368569c 138 */
AxedaCorp 0:65004368569c 139 virtual void disconnect();
AxedaCorp 0:65004368569c 140
AxedaCorp 0:65004368569c 141 /** This method is used to check if the radio currently has a Wifi network
AxedaCorp 0:65004368569c 142 * connection established.
AxedaCorp 0:65004368569c 143 *
AxedaCorp 0:65004368569c 144 * @returns true if a network connection exists, otherwise false.
AxedaCorp 0:65004368569c 145 */
AxedaCorp 0:65004368569c 146 virtual bool isConnected();
AxedaCorp 0:65004368569c 147
AxedaCorp 0:65004368569c 148 // TCP and UDP Socket related commands
AxedaCorp 0:65004368569c 149 // For behavior of the following methods refer to IPStack.h documentation
AxedaCorp 0:65004368569c 150 virtual bool bind(unsigned int port);
AxedaCorp 0:65004368569c 151 virtual bool open(const std::string& address, unsigned int port, Mode mode);
AxedaCorp 0:65004368569c 152 virtual bool isOpen();
AxedaCorp 0:65004368569c 153 virtual bool close();
AxedaCorp 0:65004368569c 154 virtual int read(char* data, int max, int timeout = -1);
AxedaCorp 0:65004368569c 155 virtual int write(const char* data, int length, int timeout = -1);
AxedaCorp 0:65004368569c 156 virtual unsigned int readable();
AxedaCorp 0:65004368569c 157 virtual unsigned int writeable();
AxedaCorp 0:65004368569c 158
AxedaCorp 0:65004368569c 159 /** This method performs a soft reboot of the device by issuing the
AxedaCorp 0:65004368569c 160 * reboot command. If the module is not able to respond to commands
AxedaCorp 0:65004368569c 161 * this will not work. This method also waits 10 seconds for the
AxedaCorp 0:65004368569c 162 * module to perform the reset and return.
AxedaCorp 0:65004368569c 163 */
AxedaCorp 0:65004368569c 164 virtual void reset();
AxedaCorp 0:65004368569c 165
AxedaCorp 0:65004368569c 166 /** A method for sending a generic text command to the radio. Note that you cannot
AxedaCorp 0:65004368569c 167 * send commands and have a socket connection at the same time, unless you first
AxedaCorp 0:65004368569c 168 * switch to command mode.
AxedaCorp 0:65004368569c 169 *
AxedaCorp 0:65004368569c 170 * @param command the command to send to the WiFi module without the escape character.
AxedaCorp 0:65004368569c 171 * @param timeoutMillis the time in millis to wait for a response before returning.
AxedaCorp 0:65004368569c 172 * @param response the text string to look for and to return immediately after finding.
AxedaCorp 0:65004368569c 173 * The default is to look for no specific response.
AxedaCorp 0:65004368569c 174 * @param esc escape character to add at the end of the command, defaults to
AxedaCorp 0:65004368569c 175 * carriage return (CR). Does not append any character if esc == 0.
AxedaCorp 0:65004368569c 176 * @returns all data received from the radio after the command as a string.
AxedaCorp 0:65004368569c 177 */
AxedaCorp 0:65004368569c 178 std::string sendCommand(std::string command, int timeoutMillis, std::string response = "", char esc = CR);
AxedaCorp 0:65004368569c 179
AxedaCorp 0:65004368569c 180 /** A method for sending a basic command to the radio. A basic text command is
AxedaCorp 0:65004368569c 181 * one that simply has a response of either AOK or ERR without any other information.
AxedaCorp 0:65004368569c 182 * Note that you cannot send commands and have a tcp connection at the same time
AxedaCorp 0:65004368569c 183 * unless you first switch to command mode.
AxedaCorp 0:65004368569c 184 *
AxedaCorp 0:65004368569c 185 * @param command the command to send to the WiFi module without the escape character.
AxedaCorp 0:65004368569c 186 * @param timeoutMillis the time in millis to wait for a response before returning.
AxedaCorp 0:65004368569c 187 * @param esc escape character to add at the end of the command, defaults to
AxedaCorp 0:65004368569c 188 * carriage return (CR).
AxedaCorp 0:65004368569c 189 * @returns the standard Code enumeration.
AxedaCorp 0:65004368569c 190 */
AxedaCorp 0:65004368569c 191 Code sendBasicCommand(std::string command, int timeoutMillis, char esc = CR);
AxedaCorp 0:65004368569c 192
AxedaCorp 0:65004368569c 193 /** This method is used to set the network information details. This method must be
AxedaCorp 0:65004368569c 194 * called before connect, which establishes the WiFi network connection.
AxedaCorp 0:65004368569c 195 *
AxedaCorp 0:65004368569c 196 * @param ssid the SSID for the network you want to attached to.
AxedaCorp 0:65004368569c 197 * @param type the type of security used on the network. The default is NONE.
AxedaCorp 0:65004368569c 198 * @param key the security key for the network. The default is no key.
AxedaCorp 0:65004368569c 199 */
AxedaCorp 0:65004368569c 200 Code setNetwork(const std::string& ssid, SecurityType type = NONE, const std::string& key = "");
AxedaCorp 0:65004368569c 201
AxedaCorp 0:65004368569c 202 /** This method is used to set the IP address or puts the module in DHCP mode.
AxedaCorp 0:65004368569c 203 *
AxedaCorp 0:65004368569c 204 * @param address the IP address you want to use in the form of xxx.xxx.xxx.xxx or DHCP
AxedaCorp 0:65004368569c 205 * if you want to use DHCP. The default is DHCP.
AxedaCorp 0:65004368569c 206 * @returns the standard Code enumeration.
AxedaCorp 0:65004368569c 207 */
AxedaCorp 0:65004368569c 208 Code setDeviceIP(std::string address = "DHCP");
AxedaCorp 0:65004368569c 209
AxedaCorp 0:65004368569c 210 /** This method is used to get the IP address of the device, which can be
AxedaCorp 0:65004368569c 211 * set either statically or via DHCP after connecting to a network.
AxedaCorp 0:65004368569c 212 *
AxedaCorp 0:65004368569c 213 * @returns the devices IP address.
AxedaCorp 0:65004368569c 214 */
AxedaCorp 0:65004368569c 215 std::string getDeviceIP();
AxedaCorp 0:65004368569c 216
AxedaCorp 0:65004368569c 217 /** This method is used to set the DNS which enables the use of URLs instead
AxedaCorp 0:65004368569c 218 * of IP addresses when making a socket connection.
AxedaCorp 0:65004368569c 219 *
AxedaCorp 0:65004368569c 220 * @param the DNS server address as a string in form xxx.xxx.xxx.xxx.
AxedaCorp 0:65004368569c 221 * @returns the standard AT Code enumeration.
AxedaCorp 0:65004368569c 222 */
AxedaCorp 0:65004368569c 223 Code setDNS(const std::string& dnsName);
AxedaCorp 0:65004368569c 224
AxedaCorp 0:65004368569c 225 /** A method for getting the signal strength of the Wifi module. This method allows
AxedaCorp 0:65004368569c 226 * you to get the signal strength in dBm. If you get a result of 99 the signal strength
AxedaCorp 0:65004368569c 227 * is not known or there was an error in reading it. Note that you cannot read the signal
AxedaCorp 0:65004368569c 228 * strength unless you are already attached to a Wifi network.
AxedaCorp 0:65004368569c 229 *
AxedaCorp 0:65004368569c 230 * @returns an integer representing the signal strength in dBm.
AxedaCorp 0:65004368569c 231 */
AxedaCorp 0:65004368569c 232 int getSignalStrength();
AxedaCorp 0:65004368569c 233
AxedaCorp 0:65004368569c 234 /** This method is used test network connectivity by pinging a server.
AxedaCorp 0:65004368569c 235 *
AxedaCorp 0:65004368569c 236 * @param address the address of the server in format xxx.xxx.xxx.xxx.
AxedaCorp 0:65004368569c 237 * @returns true if the ping was successful, otherwise false.
AxedaCorp 0:65004368569c 238 */
AxedaCorp 0:65004368569c 239 bool ping(const std::string& address = "8.8.8.8");
AxedaCorp 0:65004368569c 240
AxedaCorp 0:65004368569c 241 /** This method is used to set whether the device is in command mode or data mode.
AxedaCorp 0:65004368569c 242 * In command mode you are able to send configuration and status commands while
AxedaCorp 0:65004368569c 243 * data mode is used for sending data when you have an open socket connection.
AxedaCorp 0:65004368569c 244 * Note that for all other methods in this class the change is handled automatically.
AxedaCorp 0:65004368569c 245 * Only use this methodif you want to send your own commands that are not already
AxedaCorp 0:65004368569c 246 * supported and need to make sure that you are in command mode.
AxedaCorp 0:65004368569c 247 *
AxedaCorp 0:65004368569c 248 * @param on if true sets to command mode, otherwise to data mode.
AxedaCorp 0:65004368569c 249 * @returns true if the change was successful, otherwise false.
AxedaCorp 0:65004368569c 250 */
AxedaCorp 0:65004368569c 251 bool setCmdMode(bool on);
AxedaCorp 0:65004368569c 252
AxedaCorp 0:65004368569c 253 private:
AxedaCorp 0:65004368569c 254 static Wifi* instance; //Static pointer to the single Cellular object.
AxedaCorp 0:65004368569c 255
AxedaCorp 0:65004368569c 256 MTSBufferedIO* io; //IO interface obect that the radio is accessed through.
AxedaCorp 0:65004368569c 257
AxedaCorp 0:65004368569c 258 bool wifiConnected; //Specifies if a Wifi network session is currently connected.
AxedaCorp 0:65004368569c 259 std::string _ssid; //A string that holds the SSID for the Wifi module.
AxedaCorp 0:65004368569c 260
AxedaCorp 0:65004368569c 261 Mode mode; //The current socket Mode.
AxedaCorp 0:65004368569c 262 bool socketOpened; //Specifies if a Socket is presently opened.
AxedaCorp 0:65004368569c 263 bool socketCloseable; //Specifies is a Socket can be closed.
AxedaCorp 0:65004368569c 264 unsigned int local_port; //Holds the local port for socket connections.
AxedaCorp 0:65004368569c 265 std::string local_address; //Holds the local address for socket connections.
AxedaCorp 0:65004368569c 266 unsigned int host_port; //Holds the remote port for socket connections.
AxedaCorp 0:65004368569c 267 std::string host_address; //Holds the remote address for socket connections.
AxedaCorp 0:65004368569c 268 bool cmdOn; //Determines whether the device is in command mode or not
AxedaCorp 0:65004368569c 269
AxedaCorp 0:65004368569c 270 Wifi(); //Private constructor, use the getInstance() method.
AxedaCorp 0:65004368569c 271 Wifi(MTSBufferedIO* io); //Private constructor, use the getInstance() method.
AxedaCorp 0:65004368569c 272 bool sortInterfaceMode(void); // module gets in wierd state without IO reset
AxedaCorp 0:65004368569c 273 std::string getHostByName(std::string url); // Gets the IP address for a URL
AxedaCorp 0:65004368569c 274 };
AxedaCorp 0:65004368569c 275
AxedaCorp 0:65004368569c 276 #endif /* WIFI_H */