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:
Mon Dec 30 23:55:43 2013 +0000
Revision:
103:da58d27c15d7
Parent:
95:4fdf968b5b37
Child:
106:358972176b89
In Wifi updated the getSignalStrength method to be more robust, added a lot more comments. Also modified the setNetwork param order.

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 93:aa7a48e65974 85 std::string sendCommand(std::string command, int timeoutMillis, std::string response = "", char esc = CR);
sgodinez 73:bb5bbca971ae 86 Code sendBasicCommand(std::string command, int timeoutMillis, char esc = CR);
jengbrecht 69:f3e696bbb0d5 87
jengbrecht 103:da58d27c15d7 88 /** This method is used to set the network information details. This method must be
jengbrecht 103:da58d27c15d7 89 * called before connect, which establishes the WiFi network connection.
jengbrecht 103:da58d27c15d7 90 *
jengbrecht 103:da58d27c15d7 91 * @param ssid the SSID for the network you want to attached to.
jengbrecht 103:da58d27c15d7 92 * @param type the type of security used on the network. The default is NONE.
jengbrecht 103:da58d27c15d7 93 * @param key the security key for the network. The default is no key.
jengbrecht 103:da58d27c15d7 94 */
jengbrecht 103:da58d27c15d7 95 Code setNetwork(const std::string& ssid, SecurityType type = NONE, const std::string& key = "");
jengbrecht 103:da58d27c15d7 96
jengbrecht 103:da58d27c15d7 97 /** This method is used to set the DNS which enables the use of URLs instead
jengbrecht 103:da58d27c15d7 98 * of IP addresses when making a socket connection.
jengbrecht 103:da58d27c15d7 99 *
jengbrecht 103:da58d27c15d7 100 * @param the DNS server address as a string in form xxx.xxx.xxx.xxx.
jengbrecht 103:da58d27c15d7 101 * @returns the standard AT Code enumeration.
jengbrecht 103:da58d27c15d7 102 */
jengbrecht 94:1baa587e89ae 103 Code setDNS(const std::string& dnsName);
jengbrecht 103:da58d27c15d7 104
jengbrecht 103:da58d27c15d7 105 /** A method for getting the signal strength of the Wifi module. This method allows
jengbrecht 103:da58d27c15d7 106 * you to get the signal strength in dBm. If you get a result of 99 the signal strength
jengbrecht 103:da58d27c15d7 107 * is not known or there was an error in reading it. Note that you cannot read the signal
jengbrecht 103:da58d27c15d7 108 * strength unless you are already attached to a Wifi network.
jengbrecht 103:da58d27c15d7 109 *
jengbrecht 103:da58d27c15d7 110 * @returns an integer representing the signal strength in dBm.
jengbrecht 103:da58d27c15d7 111 */
jengbrecht 69:f3e696bbb0d5 112 int getSignalStrength();
jengbrecht 103:da58d27c15d7 113
jengbrecht 103:da58d27c15d7 114 /** This method is used test network connectivity by pinging a server.
jengbrecht 103:da58d27c15d7 115 *
jengbrecht 103:da58d27c15d7 116 * @param address the address of the server in format xxx.xxx.xxx.xxx.
jengbrecht 103:da58d27c15d7 117 * @returns true if the ping was successful, otherwise false.
jengbrecht 103:da58d27c15d7 118 */
jengbrecht 95:4fdf968b5b37 119 bool ping(const std::string& address = "8.8.8.8");
jengbrecht 103:da58d27c15d7 120
jengbrecht 103:da58d27c15d7 121 /** This method is used to set whether the device is in command mode or data mode.
jengbrecht 103:da58d27c15d7 122 * In command mode you are able to send configuration and status commands while
jengbrecht 103:da58d27c15d7 123 * data mode is used for sending data when you have an open socket connection.
jengbrecht 103:da58d27c15d7 124 * Note that for all other methods in this class the change is handled automatically.
jengbrecht 103:da58d27c15d7 125 * Only use this methodif you want to send your own commands that are not already
jengbrecht 103:da58d27c15d7 126 * supported and need to make sure that you are in command mode.
jengbrecht 103:da58d27c15d7 127 *
jengbrecht 103:da58d27c15d7 128 * @param on if true sets to command mode, otherwise to data mode.
jengbrecht 103:da58d27c15d7 129 * @returns true if the change was successful, otherwise false.
jengbrecht 103:da58d27c15d7 130 */
jengbrecht 79:f356009dbc12 131 bool setCmdMode(bool on);
jengbrecht 69:f3e696bbb0d5 132
jengbrecht 69:f3e696bbb0d5 133 private:
jengbrecht 69:f3e696bbb0d5 134 static Wifi* instance; //Static pointer to the single Cellular object.
jengbrecht 69:f3e696bbb0d5 135
jengbrecht 69:f3e696bbb0d5 136 MTSBufferedIO* io; //IO interface obect that the radio is accessed through.
jengbrecht 69:f3e696bbb0d5 137
jengbrecht 69:f3e696bbb0d5 138 bool wifiConnected; //Specifies if a Wifi network session is currently connected.
jengbrecht 69:f3e696bbb0d5 139 std::string _ssid; //A string that holds the SSID for the Wifi module.
jengbrecht 69:f3e696bbb0d5 140
jengbrecht 69:f3e696bbb0d5 141 Mode mode; //The current socket Mode.
jengbrecht 69:f3e696bbb0d5 142 bool socketOpened; //Specifies if a Socket is presently opened.
jengbrecht 69:f3e696bbb0d5 143 bool socketCloseable; //Specifies is a Socket can be closed.
jengbrecht 69:f3e696bbb0d5 144 unsigned int local_port; //Holds the local port for socket connections.
jengbrecht 69:f3e696bbb0d5 145 std::string local_address; //Holds the local address for socket connections.
jengbrecht 69:f3e696bbb0d5 146 unsigned int host_port; //Holds the remote port for socket connections.
jengbrecht 69:f3e696bbb0d5 147 std::string host_address; //Holds the remote address for socket connections.
jengbrecht 103:da58d27c15d7 148 bool cmdOn; //Determines whether the device is in command mode or not
jengbrecht 69:f3e696bbb0d5 149
jengbrecht 69:f3e696bbb0d5 150 Wifi(); //Private constructor, use the getInstance() method.
jengbrecht 69:f3e696bbb0d5 151 Wifi(MTSBufferedIO* io); //Private constructor, use the getInstance() method.
jengbrecht 69:f3e696bbb0d5 152 };
jengbrecht 69:f3e696bbb0d5 153
jengbrecht 69:f3e696bbb0d5 154 #endif /* WIFI_H */