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

Files at this revision

API Documentation at this revision

Comitter:
jengbrecht
Date:
Mon Jan 20 15:33:54 2014 +0000
Parent:
145:6a65bc3e1310
Child:
147:cc1789c65687
Child:
148:df9feef182b4
Child:
150:95093b517872
Commit message:
Moved IPStack.h to Socket folder, should eventually be its own library. Also added overloaded setTransport method to Transport for passing in any IPStack derived object, look at merging this concept to mbed official.

Changed in this revision

Socket/IPStack.h Show annotated file Show diff for this revision Revisions of this file
Socket/Transport.cpp Show annotated file Show diff for this revision Revisions of this file
Socket/Transport.h Show annotated file Show diff for this revision Revisions of this file
cellular/Cellular.cpp Show annotated file Show diff for this revision Revisions of this file
io/IPStack.h Show diff for this revision Revisions of this file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Socket/IPStack.h	Mon Jan 20 15:33:54 2014 +0000
@@ -0,0 +1,143 @@
+/* Universal Socket Modem Interface Library
+* Copyright (c) 2013 Multi-Tech Systems
+*
+* Licensed under the Apache License, Version 2.0 (the "License");
+* you may not use this file except in compliance with the License.
+* You may obtain a copy of the License at
+*
+*     http://www.apache.org/licenses/LICENSE-2.0
+*
+* Unless required by applicable law or agreed to in writing, software
+* distributed under the License is distributed on an "AS IS" BASIS,
+* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+* See the License for the specific language governing permissions and
+* limitations under the License.
+*/
+
+#ifndef IPSTACK_H
+#define IPSTACK_H
+
+#include <string>
+
+namespace mts {
+    
+/** This class is a pure virtual class that should be inherited from when implementing
+* a communications device with an onboard IP stack.  Examples of this would be a Wi-Fi
+* or Cellular radio with a built in IP stack. Typically the IP functionality in these
+* devices is available through an AT or a similiar command interface. The inheriting
+* class should map the device commands and functionality to the pure virtual methods provided
+* here. There should also be at least one or more calls to setup the communication link
+* specific paramters as an init method for example.  This would do things like configure
+* the APN in a cellular radio or set the ssid for a WiFi device, which cannot be accounted
+* for in an abstract class like this one.
+*/
+class IPStack
+{
+public:
+    /// An enumeration for selecting the Socket Mode of TCP or UDP.
+    enum Mode {
+        TCP, UDP
+    };
+
+    /** This method is used to connect the IP layer and below for the interface. Required
+    * configurations and settings should be done in other calls or an init function.
+    *
+    * @returns true if the connection was successfully established, otherwise false.
+    */
+    virtual bool connect() = 0;
+
+    /** This method is used to disconnect the IP layer and below of the interface. This includes
+    * any cleanup required before another connection can be made.
+    */
+    virtual void disconnect() = 0;
+
+    /** This method is used to check if the IP layer of the interface is currently connected.
+    *
+    * @returns true if currently connected, otherwise false.
+    */
+    virtual bool isConnected() = 0;
+
+    /** This method is used to set the local port for the UDP or TCP socket connection.
+    * The connection can be made using the open method.
+    *
+    * @param port the local port of the socket as an int.
+    */
+    virtual bool bind(unsigned int port) = 0;
+
+    /** This method is used to open a socket connection with the given parameters.
+    * This socket connection is established using the devices built in IP stack.
+    * Currently TCP is the only supported mode.
+    *
+    * @param address is the address you want to connect to in the form of xxx.xxx.xxx.xxx
+    * or a URL. If using a URL make sure the device supports DNS and is properly configured
+    * for that mode.
+    * @param port the remote port you want to connect to.
+    * @param mode an enum that specifies whether this socket connection is type TCP or UDP.
+    * Currently only TCP is supported.
+    * @returns true if the connection was successfully opened, otherwise false.
+    */
+    virtual bool open(const std::string& address, unsigned int port, Mode mode) = 0;
+
+    /** This method is used to determine if a socket connection is currently open.
+    *
+    * @returns true if the socket is currently open, otherwise false.
+    */
+    virtual bool isOpen() = 0;
+
+    /** This method is used to close a socket connection that is currently open.
+    *
+    * @returns true if successfully closed, otherwise returns false on an error.
+    */
+    virtual bool close() = 0;
+
+    /** This method is used to read data off of a socket, assuming a valid socket
+    * connection is already open.
+    *
+    * @param data a pointer to the data buffer that will be filled with the read data.
+    * @param max the maximum number of bytes to attempt to read, typically the same as
+    * the size of the passed in data buffer.
+    * @param timeout the amount of time in milliseconds to wait in trying to read the max
+    * number of bytes. If set to -1 the call blocks until it receives the max number of bytes
+    * or encounters and error.
+    * @returns the number of bytes read and stored in the passed in data buffer. Returns
+    * -1 if there was an error in reading.
+    */
+    virtual int read(char* data, int max, int timeout = -1) = 0;
+
+    /** This method is used to write data to a socket, assuming a valid socket
+    * connection is already open.
+    *
+    * @param data a pointer to the data buffer that will be written to the socket.
+    * @param length the size of the data buffer to be written.
+    * @param timeout the amount of time in milliseconds to wait in trying to write the entire
+    * number of bytes. If set to -1 the call blocks until it writes all of the bytes or 
+    * encounters and error.
+    * @returns the number of bytes written to the socket's write buffer. Returns
+    * -1 if there was an error in writing.
+    */
+    virtual int write(const char* data, int length, int timeout = -1) = 0;
+
+
+    /** This method is used to get the number of bytes available to read off the
+    * socket.
+    *
+    * @returns the number of bytes available, 0 if there are no bytes to read.
+    */
+    virtual unsigned int readable() = 0;
+
+    /** This method is used to get the space available to write bytes to the socket.
+    *
+    * @returns the number of bytes that can be written, 0 if unable to write.
+    */
+    virtual unsigned int writeable() = 0;
+
+    /** This method is used to reset the device that provides the communications
+    * capability. Note that you may have to wait some time after reset before the
+    * device can be used.
+    */
+    virtual void reset() = 0;
+};
+
+}
+
+#endif /* IPSTACK_H */
\ No newline at end of file
--- a/Socket/Transport.cpp	Wed Jan 15 19:26:13 2014 +0000
+++ b/Socket/Transport.cpp	Mon Jan 20 15:33:54 2014 +0000
@@ -20,11 +20,23 @@
 
 Transport::TransportType Transport::_type = Transport::NONE;
 
+IPStack* Transport::customType = NULL;
+
 void Transport::setTransport(TransportType type)
 {
+    if (type == CUSTOM) {
+        printf("[ERROR] Transport not set, use other setTransport method for setting custom type.\n\r");
+        return;
+    }
     _type = type;
 }
 
+void Transport::setTransport(IPStack* type)
+{
+    customType = type;
+    _type = CUSTOM;
+}
+
 IPStack* Transport::getInstance()
 {
     switch (_type) {
@@ -32,6 +44,8 @@
             return (IPStack*) Cellular::getInstance();
         case WIFI:
             return (IPStack*) Wifi::getInstance();
+        case CUSTOM:
+            return customType;
         default:
             printf("[ERROR] Transport not set, use setTransport method.\n\r");
             return NULL;
--- a/Socket/Transport.h	Wed Jan 15 19:26:13 2014 +0000
+++ b/Socket/Transport.h	Mon Jan 20 15:33:54 2014 +0000
@@ -35,7 +35,7 @@
 public:
     ///An enumeration that holds the supported Transport Types.
     enum TransportType {
-        CELLULAR, WIFI, NONE
+        CELLULAR, WIFI, NONE, CUSTOM
     };
     
     /** This method allows you to set the transport to be used when creating other 
@@ -45,6 +45,15 @@
     */
     static void setTransport(TransportType type);
     
+    /** This method allows you to set the transport to be used when creatin other
+    * objects from the Socket folder like TCPSocketConnection and UDPSocket. It
+    * differs from the other setTransport method in that it allows for any transport
+    * that derives from IPStack to be used with the native mbed Socket interfaces.
+    *
+    * @param type the type of underlying transport to be used as an IPStack object.
+    */
+    static void setTransport(IPStack* type);
+    
     /** This method is used within the Socket class to get the appropraite transport
     * as an IPStack object.  In general you do not need to call this directly, but
     * simply use the other classes in this folder. 
@@ -55,6 +64,7 @@
     
 private:
     static Transport::TransportType _type; // Member variable that holds the desired transport
+    static IPStack* customType; //Member variable that holds an custom transport type.
 };
 
 #endif /* TRANSPORT_H */
\ No newline at end of file
--- a/cellular/Cellular.cpp	Wed Jan 15 19:26:13 2014 +0000
+++ b/cellular/Cellular.cpp	Mon Jan 20 15:33:54 2014 +0000
@@ -83,12 +83,12 @@
         dtr->write(0);
     }
     instance->io = io;
-    
+
     test();
     // Reset radio to make sure it's in a good state and wait for it to come back
     reset();
     test();
-    
+
     return SUCCESS;
 }
 
@@ -578,7 +578,6 @@
 
 Code Cellular::test()
 {
-    Code code;
     int i = 0;
     while (sendBasicCommand("AT", 1000) != SUCCESS) {
         i++;
@@ -588,7 +587,6 @@
         }
         wait(1);
     }
-
     return SUCCESS;
 }
 
--- a/io/IPStack.h	Wed Jan 15 19:26:13 2014 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,143 +0,0 @@
-/* Universal Socket Modem Interface Library
-* Copyright (c) 2013 Multi-Tech Systems
-*
-* Licensed under the Apache License, Version 2.0 (the "License");
-* you may not use this file except in compliance with the License.
-* You may obtain a copy of the License at
-*
-*     http://www.apache.org/licenses/LICENSE-2.0
-*
-* Unless required by applicable law or agreed to in writing, software
-* distributed under the License is distributed on an "AS IS" BASIS,
-* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-* See the License for the specific language governing permissions and
-* limitations under the License.
-*/
-
-#ifndef IPSTACK_H
-#define IPSTACK_H
-
-#include <string>
-
-namespace mts {
-    
-/** This class is a pure virtual class that should be inherited from when implementing
-* a communications device with an onboard IP stack.  Examples of this would be a Wi-Fi
-* or Cellular radio with a built in IP stack. Typically the IP functionality in these
-* devices is available through an AT or a similiar command interface. The inheriting
-* class should map the device commands and functionality to the pure virtual methods provided
-* here. There should also be at least one or more calls to setup the communication link
-* specific paramters as an init method for example.  This would do things like configure
-* the APN in a cellular radio or set the ssid for a WiFi device, which cannot be accounted
-* for in an abstract class like this one.
-*/
-class IPStack
-{
-public:
-    /// An enumeration for selecting the Socket Mode of TCP or UDP.
-    enum Mode {
-        TCP, UDP
-    };
-
-    /** This method is used to connect the IP layer and below for the interface. Required
-    * configurations and settings should be done in other calls or an init function.
-    *
-    * @returns true if the connection was successfully established, otherwise false.
-    */
-    virtual bool connect() = 0;
-
-    /** This method is used to disconnect the IP layer and below of the interface. This includes
-    * any cleanup required before another connection can be made.
-    */
-    virtual void disconnect() = 0;
-
-    /** This method is used to check if the IP layer of the interface is currently connected.
-    *
-    * @returns true if currently connected, otherwise false.
-    */
-    virtual bool isConnected() = 0;
-
-    /** This method is used to set the local port for the UDP or TCP socket connection.
-    * The connection can be made using the open method.
-    *
-    * @param port the local port of the socket as an int.
-    */
-    virtual bool bind(unsigned int port) = 0;
-
-    /** This method is used to open a socket connection with the given parameters.
-    * This socket connection is established using the devices built in IP stack.
-    * Currently TCP is the only supported mode.
-    *
-    * @param address is the address you want to connect to in the form of xxx.xxx.xxx.xxx
-    * or a URL. If using a URL make sure the device supports DNS and is properly configured
-    * for that mode.
-    * @param port the remote port you want to connect to.
-    * @param mode an enum that specifies whether this socket connection is type TCP or UDP.
-    * Currently only TCP is supported.
-    * @returns true if the connection was successfully opened, otherwise false.
-    */
-    virtual bool open(const std::string& address, unsigned int port, Mode mode) = 0;
-
-    /** This method is used to determine if a socket connection is currently open.
-    *
-    * @returns true if the socket is currently open, otherwise false.
-    */
-    virtual bool isOpen() = 0;
-
-    /** This method is used to close a socket connection that is currently open.
-    *
-    * @returns true if successfully closed, otherwise returns false on an error.
-    */
-    virtual bool close() = 0;
-
-    /** This method is used to read data off of a socket, assuming a valid socket
-    * connection is already open.
-    *
-    * @param data a pointer to the data buffer that will be filled with the read data.
-    * @param max the maximum number of bytes to attempt to read, typically the same as
-    * the size of the passed in data buffer.
-    * @param timeout the amount of time in milliseconds to wait in trying to read the max
-    * number of bytes. If set to -1 the call blocks until it receives the max number of bytes
-    * or encounters and error.
-    * @returns the number of bytes read and stored in the passed in data buffer. Returns
-    * -1 if there was an error in reading.
-    */
-    virtual int read(char* data, int max, int timeout = -1) = 0;
-
-    /** This method is used to write data to a socket, assuming a valid socket
-    * connection is already open.
-    *
-    * @param data a pointer to the data buffer that will be written to the socket.
-    * @param length the size of the data buffer to be written.
-    * @param timeout the amount of time in milliseconds to wait in trying to write the entire
-    * number of bytes. If set to -1 the call blocks until it writes all of the bytes or 
-    * encounters and error.
-    * @returns the number of bytes written to the socket's write buffer. Returns
-    * -1 if there was an error in writing.
-    */
-    virtual int write(const char* data, int length, int timeout = -1) = 0;
-
-
-    /** This method is used to get the number of bytes available to read off the
-    * socket.
-    *
-    * @returns the number of bytes available, 0 if there are no bytes to read.
-    */
-    virtual unsigned int readable() = 0;
-
-    /** This method is used to get the space available to write bytes to the socket.
-    *
-    * @returns the number of bytes that can be written, 0 if unable to write.
-    */
-    virtual unsigned int writeable() = 0;
-
-    /** This method is used to reset the device that provides the communications
-    * capability. Note that you may have to wait some time after reset before the
-    * device can be used.
-    */
-    virtual void reset() = 0;
-};
-
-}
-
-#endif /* IPSTACK_H */
\ No newline at end of file