Dependencies: FRDM_MMA8451Q KL46Z-USBHost MAG3110 SocketModem TSI mbed FATFileSystem
Fork of AxedaGo-Freescal_FRDM-KL46Z revert by
SocketModem/io/IPStack.h@0:65004368569c, 2014-07-01 (annotated)
- Committer:
- AxedaCorp
- Date:
- Tue Jul 01 21:31:54 2014 +0000
- Revision:
- 0:65004368569c
Made initial
Who changed what in which revision?
User | Revision | Line number | New 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 IPSTACK_H |
AxedaCorp | 0:65004368569c | 18 | #define IPSTACK_H |
AxedaCorp | 0:65004368569c | 19 | |
AxedaCorp | 0:65004368569c | 20 | #include <string> |
AxedaCorp | 0:65004368569c | 21 | |
AxedaCorp | 0:65004368569c | 22 | namespace mts { |
AxedaCorp | 0:65004368569c | 23 | |
AxedaCorp | 0:65004368569c | 24 | /** This class is a pure virtual class that should be inherited from when implementing |
AxedaCorp | 0:65004368569c | 25 | * a communications device with an onboard IP stack. Examples of this would be a Wi-Fi |
AxedaCorp | 0:65004368569c | 26 | * or Cellular radio with a built in IP stack. Typically the IP functionality in these |
AxedaCorp | 0:65004368569c | 27 | * devices is available through an AT or a similiar command interface. The inheriting |
AxedaCorp | 0:65004368569c | 28 | * class should map the device commands and functionality to the pure virtual methods provided |
AxedaCorp | 0:65004368569c | 29 | * here. There should also be at least one or more calls to setup the communication link |
AxedaCorp | 0:65004368569c | 30 | * specific paramters as an init method for example. This would do things like configure |
AxedaCorp | 0:65004368569c | 31 | * the APN in a cellular radio or set the ssid for a WiFi device, which cannot be accounted |
AxedaCorp | 0:65004368569c | 32 | * for in an abstract class like this one. |
AxedaCorp | 0:65004368569c | 33 | */ |
AxedaCorp | 0:65004368569c | 34 | class IPStack |
AxedaCorp | 0:65004368569c | 35 | { |
AxedaCorp | 0:65004368569c | 36 | public: |
AxedaCorp | 0:65004368569c | 37 | /// An enumeration for selecting the Socket Mode of TCP or UDP. |
AxedaCorp | 0:65004368569c | 38 | enum Mode { |
AxedaCorp | 0:65004368569c | 39 | TCP, UDP |
AxedaCorp | 0:65004368569c | 40 | }; |
AxedaCorp | 0:65004368569c | 41 | |
AxedaCorp | 0:65004368569c | 42 | /** This method is used to connect the IP layer and below for the interface. Required |
AxedaCorp | 0:65004368569c | 43 | * configurations and settings should be done in other calls or an init function. |
AxedaCorp | 0:65004368569c | 44 | * |
AxedaCorp | 0:65004368569c | 45 | * @returns true if the connection was successfully established, otherwise false. |
AxedaCorp | 0:65004368569c | 46 | */ |
AxedaCorp | 0:65004368569c | 47 | virtual bool connect() = 0; |
AxedaCorp | 0:65004368569c | 48 | |
AxedaCorp | 0:65004368569c | 49 | /** This method is used to disconnect the IP layer and below of the interface. This includes |
AxedaCorp | 0:65004368569c | 50 | * any cleanup required before another connection can be made. |
AxedaCorp | 0:65004368569c | 51 | */ |
AxedaCorp | 0:65004368569c | 52 | virtual void disconnect() = 0; |
AxedaCorp | 0:65004368569c | 53 | |
AxedaCorp | 0:65004368569c | 54 | /** This method is used to check if the IP layer of the interface is currently connected. |
AxedaCorp | 0:65004368569c | 55 | * |
AxedaCorp | 0:65004368569c | 56 | * @returns true if currently connected, otherwise false. |
AxedaCorp | 0:65004368569c | 57 | */ |
AxedaCorp | 0:65004368569c | 58 | virtual bool isConnected() = 0; |
AxedaCorp | 0:65004368569c | 59 | |
AxedaCorp | 0:65004368569c | 60 | /** This method is used to set the local port for the UDP or TCP socket connection. |
AxedaCorp | 0:65004368569c | 61 | * The connection can be made using the open method. |
AxedaCorp | 0:65004368569c | 62 | * |
AxedaCorp | 0:65004368569c | 63 | * @param port the local port of the socket as an int. |
AxedaCorp | 0:65004368569c | 64 | */ |
AxedaCorp | 0:65004368569c | 65 | virtual bool bind(unsigned int port) = 0; |
AxedaCorp | 0:65004368569c | 66 | |
AxedaCorp | 0:65004368569c | 67 | /** This method is used to open a socket connection with the given parameters. |
AxedaCorp | 0:65004368569c | 68 | * This socket connection is established using the devices built in IP stack. |
AxedaCorp | 0:65004368569c | 69 | * Currently TCP is the only supported mode. |
AxedaCorp | 0:65004368569c | 70 | * |
AxedaCorp | 0:65004368569c | 71 | * @param address is the address you want to connect to in the form of xxx.xxx.xxx.xxx |
AxedaCorp | 0:65004368569c | 72 | * or a URL. If using a URL make sure the device supports DNS and is properly configured |
AxedaCorp | 0:65004368569c | 73 | * for that mode. |
AxedaCorp | 0:65004368569c | 74 | * @param port the remote port you want to connect to. |
AxedaCorp | 0:65004368569c | 75 | * @param mode an enum that specifies whether this socket connection is type TCP or UDP. |
AxedaCorp | 0:65004368569c | 76 | * Currently only TCP is supported. |
AxedaCorp | 0:65004368569c | 77 | * @returns true if the connection was successfully opened, otherwise false. |
AxedaCorp | 0:65004368569c | 78 | */ |
AxedaCorp | 0:65004368569c | 79 | virtual bool open(const std::string& address, unsigned int port, Mode mode) = 0; |
AxedaCorp | 0:65004368569c | 80 | |
AxedaCorp | 0:65004368569c | 81 | /** This method is used to determine if a socket connection is currently open. |
AxedaCorp | 0:65004368569c | 82 | * |
AxedaCorp | 0:65004368569c | 83 | * @returns true if the socket is currently open, otherwise false. |
AxedaCorp | 0:65004368569c | 84 | */ |
AxedaCorp | 0:65004368569c | 85 | virtual bool isOpen() = 0; |
AxedaCorp | 0:65004368569c | 86 | |
AxedaCorp | 0:65004368569c | 87 | /** This method is used to close a socket connection that is currently open. |
AxedaCorp | 0:65004368569c | 88 | * |
AxedaCorp | 0:65004368569c | 89 | * @returns true if successfully closed, otherwise returns false on an error. |
AxedaCorp | 0:65004368569c | 90 | */ |
AxedaCorp | 0:65004368569c | 91 | virtual bool close() = 0; |
AxedaCorp | 0:65004368569c | 92 | |
AxedaCorp | 0:65004368569c | 93 | /** This method is used to read data off of a socket, assuming a valid socket |
AxedaCorp | 0:65004368569c | 94 | * connection is already open. |
AxedaCorp | 0:65004368569c | 95 | * |
AxedaCorp | 0:65004368569c | 96 | * @param data a pointer to the data buffer that will be filled with the read data. |
AxedaCorp | 0:65004368569c | 97 | * @param max the maximum number of bytes to attempt to read, typically the same as |
AxedaCorp | 0:65004368569c | 98 | * the size of the passed in data buffer. |
AxedaCorp | 0:65004368569c | 99 | * @param timeout the amount of time in milliseconds to wait in trying to read the max |
AxedaCorp | 0:65004368569c | 100 | * number of bytes. If set to -1 the call blocks until it receives the max number of bytes |
AxedaCorp | 0:65004368569c | 101 | * or encounters and error. |
AxedaCorp | 0:65004368569c | 102 | * @returns the number of bytes read and stored in the passed in data buffer. Returns |
AxedaCorp | 0:65004368569c | 103 | * -1 if there was an error in reading. |
AxedaCorp | 0:65004368569c | 104 | */ |
AxedaCorp | 0:65004368569c | 105 | virtual int read(char* data, int max, int timeout = -1) = 0; |
AxedaCorp | 0:65004368569c | 106 | |
AxedaCorp | 0:65004368569c | 107 | /** This method is used to write data to a socket, assuming a valid socket |
AxedaCorp | 0:65004368569c | 108 | * connection is already open. |
AxedaCorp | 0:65004368569c | 109 | * |
AxedaCorp | 0:65004368569c | 110 | * @param data a pointer to the data buffer that will be written to the socket. |
AxedaCorp | 0:65004368569c | 111 | * @param length the size of the data buffer to be written. |
AxedaCorp | 0:65004368569c | 112 | * @param timeout the amount of time in milliseconds to wait in trying to write the entire |
AxedaCorp | 0:65004368569c | 113 | * number of bytes. If set to -1 the call blocks until it writes all of the bytes or |
AxedaCorp | 0:65004368569c | 114 | * encounters and error. |
AxedaCorp | 0:65004368569c | 115 | * @returns the number of bytes written to the socket's write buffer. Returns |
AxedaCorp | 0:65004368569c | 116 | * -1 if there was an error in writing. |
AxedaCorp | 0:65004368569c | 117 | */ |
AxedaCorp | 0:65004368569c | 118 | virtual int write(const char* data, int length, int timeout = -1) = 0; |
AxedaCorp | 0:65004368569c | 119 | |
AxedaCorp | 0:65004368569c | 120 | |
AxedaCorp | 0:65004368569c | 121 | /** This method is used to get the number of bytes available to read off the |
AxedaCorp | 0:65004368569c | 122 | * socket. |
AxedaCorp | 0:65004368569c | 123 | * |
AxedaCorp | 0:65004368569c | 124 | * @returns the number of bytes available, 0 if there are no bytes to read. |
AxedaCorp | 0:65004368569c | 125 | */ |
AxedaCorp | 0:65004368569c | 126 | virtual unsigned int readable() = 0; |
AxedaCorp | 0:65004368569c | 127 | |
AxedaCorp | 0:65004368569c | 128 | /** This method is used to get the space available to write bytes to the socket. |
AxedaCorp | 0:65004368569c | 129 | * |
AxedaCorp | 0:65004368569c | 130 | * @returns the number of bytes that can be written, 0 if unable to write. |
AxedaCorp | 0:65004368569c | 131 | */ |
AxedaCorp | 0:65004368569c | 132 | virtual unsigned int writeable() = 0; |
AxedaCorp | 0:65004368569c | 133 | |
AxedaCorp | 0:65004368569c | 134 | /** This method is used to reset the device that provides the communications |
AxedaCorp | 0:65004368569c | 135 | * capability. Note that you may have to wait some time after reset before the |
AxedaCorp | 0:65004368569c | 136 | * device can be used. |
AxedaCorp | 0:65004368569c | 137 | */ |
AxedaCorp | 0:65004368569c | 138 | virtual void reset() = 0; |
AxedaCorp | 0:65004368569c | 139 | }; |
AxedaCorp | 0:65004368569c | 140 | |
AxedaCorp | 0:65004368569c | 141 | } |
AxedaCorp | 0:65004368569c | 142 | |
AxedaCorp | 0:65004368569c | 143 | #endif /* IPSTACK_H */ |