The Cayenne MQTT mbed Library provides functions to easily connect to the Cayenne IoT project builder.

Dependents:   Cayenne-ESP8266Interface Cayenne-WIZnet_Library Cayenne-WIZnetInterface Cayenne-X-NUCLEO-IDW01M1 ... more

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers MQTTNetworkIDW01M1.h Source File

MQTTNetworkIDW01M1.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 _MQTTNETWORKIDW01M1_h
00019 #define _MQTTNETWORKIDW01M1_h
00020 
00021 #include "mbed.h"
00022 #include "TCPSocket.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), _connected(false) {
00037         _socket.open(&_interface);
00038     }
00039       
00040     /**
00041     * Connect the network interface.
00042     * @param[in] hostname Host for connection
00043     * @param[in] port Port for connection
00044     * @param[in] timeout_ms Timeout for the read operation, in milliseconds
00045     * @return 0 on success, -1 on failure.
00046     */
00047     int connect(char* hostname, int port, int timeout_ms = 1000) {
00048         //_socket.set_blocking(false);
00049         _socket.set_timeout(timeout_ms);
00050         int ret = _socket.connect(hostname, port);
00051         _connected = (ret == 0);
00052         return ret;
00053     }
00054     
00055     /**
00056     * Disconnect the network interface.
00057     * @return true if disconnect was successful, false otherwise
00058     */
00059     bool disconnect()
00060     {  
00061         _connected = false;
00062         return _socket.close();
00063     }
00064 
00065     /**
00066     * Check if connected.
00067     * @return true if connected, false otherwise
00068     */
00069     bool connected()
00070     {
00071         return _connected;
00072     }
00073     
00074     /**
00075     * Read data from the network.
00076     * @param[out] buffer Buffer that receives the data
00077     * @param[in] len Buffer length
00078     * @param[in] timeout_ms Timeout for the read operation, in milliseconds
00079     * @return Number of bytes read, or a negative value if there was an error
00080     */
00081     int read(unsigned char* buffer, int len, int timeout_ms) {
00082         _socket.set_timeout(timeout_ms);
00083         return _socket.recv((char*)buffer, len);
00084     }
00085     
00086     /**
00087     * Write data to the network.
00088     * @param[in] buffer Buffer that contains data to write
00089     * @param[in] len Number of bytes to write
00090     * @param[in] timeout_ms Timeout for the write operation, in milliseconds
00091     * @return Number of bytes written, or a negative value if there was an error
00092     */
00093     int write(unsigned char* buffer, int len, int timeout_ms) {
00094         _socket.set_timeout(timeout_ms);
00095         return _socket.send((char*)buffer, len);
00096     }   
00097 
00098 private:
00099     Interface& _interface;
00100     TCPSocket _socket;
00101     bool _connected;
00102 };
00103 
00104 #endif