Peter Ferland / Cayenne-MQTT-mbed-M1

Dependents:   5_Dragonfly_Cayenne_Sprint_IKS01A1

Fork of Cayenne-MQTT-mbed-MTSAS by Peter Ferland

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers MQTTNetwork.h Source File

MQTTNetwork.h

00001 /*
00002 The MIT License(MIT)
00003 
00004 Cayenne MQTT Client Library
00005 Copyright (c) 2016 myDevices
00006 
00007 Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
00008 documentation files(the "Software"), to deal in the Software without restriction, including without limitation
00009 the rights to use, copy, modify, merge, publish, distribute, sublicense, and / or sell copies of the Software,
00010 and to permit persons to whom the Software is furnished to do so, subject to the following conditions :
00011 The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
00012 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
00013 WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE AUTHORS OR
00014 COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
00015 ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
00016 */
00017 
00018 #ifndef _MQTTNETWORK_h
00019 #define _MQTTNETWORK_h
00020 
00021 #include "mbed.h"
00022 #include "TCPSocketConnection.h"
00023 
00024 /**
00025 * Networking class for use with MQTTClient.
00026 * @param Interface A network interface class with the methods: init, connect and disconnect.
00027 */
00028 template<class Interface>
00029 class MQTTNetwork
00030 {
00031 public:
00032     /**
00033     * Construct the network interface.
00034     * @param[in] interface The network interface to use
00035     */
00036     MQTTNetwork(Interface& interface) : _interface(interface) {
00037     }
00038       
00039     /**
00040     * Connect the network interface.
00041     * @param[in] hostname Host for connection
00042     * @param[in] port Port for connection
00043     * @param[in] timeout_ms Timeout for the read operation, in milliseconds
00044     * @return 0 on success, -1 on failure.
00045     */
00046     int connect(char* hostname, int port, int timeout_ms = 1000) {
00047         _interface.connect();
00048         _socket.set_blocking(false, timeout_ms);
00049         return _socket.connect(hostname, port);
00050     }
00051     
00052     /**
00053     * Disconnect the network interface.
00054     * @return true if disconnect was successful, false otherwise
00055     */
00056     bool disconnect()
00057     {
00058         _socket.close();
00059         //return _interface.disconnect();
00060         return true;
00061     }
00062 
00063     /**
00064     * Check if connected.
00065     * @return true if connected, false otherwise
00066     */
00067     bool connected()
00068     {
00069         return _socket.is_connected();
00070     }
00071     
00072     /**
00073     * Read data from the network.
00074     * @param[out] buffer Buffer that receives the data
00075     * @param[in] len Buffer length
00076     * @param[in] timeout_ms Timeout for the read operation, in milliseconds
00077     * @return Number of bytes read, or a negative value if there was an error
00078     */
00079     int read(unsigned char* buffer, int len, int timeout_ms) {
00080         _socket.set_blocking(false, timeout_ms);
00081         return _socket.receive((char*)buffer, len);
00082     }
00083     
00084     /**
00085     * Write data to the network.
00086     * @param[in] buffer Buffer that contains data to write
00087     * @param[in] len Number of bytes to write
00088     * @param[in] timeout_ms Timeout for the write operation, in milliseconds
00089     * @return Number of bytes written, or a negative value if there was an error
00090     */
00091     int write(unsigned char* buffer, int len, int timeout_ms) {
00092         _socket.set_blocking(false, timeout_ms);  
00093         return _socket.send((char*)buffer, len);
00094     }   
00095 
00096 private:
00097     Interface& _interface;
00098     TCPSocketConnection _socket;      
00099 };
00100 
00101 #endif