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

Fork of Cayenne-MQTT-mbed by myDevicesIoT

Committer:
pferland
Date:
Tue Apr 25 15:32:32 2017 +0000
Revision:
24:686d9ed1d192
Parent:
11:532406b58e8f
Small changes for compatibility with MTSAS socket library

Who changed what in which revision?

UserRevisionLine numberNew contents of line
jburhenn 0:09ef59d2d0f7 1 /*
jburhenn 0:09ef59d2d0f7 2 The MIT License(MIT)
jburhenn 0:09ef59d2d0f7 3
jburhenn 0:09ef59d2d0f7 4 Cayenne MQTT Client Library
jburhenn 0:09ef59d2d0f7 5 Copyright (c) 2016 myDevices
jburhenn 0:09ef59d2d0f7 6
jburhenn 0:09ef59d2d0f7 7 Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
jburhenn 0:09ef59d2d0f7 8 documentation files(the "Software"), to deal in the Software without restriction, including without limitation
jburhenn 0:09ef59d2d0f7 9 the rights to use, copy, modify, merge, publish, distribute, sublicense, and / or sell copies of the Software,
jburhenn 0:09ef59d2d0f7 10 and to permit persons to whom the Software is furnished to do so, subject to the following conditions :
jburhenn 0:09ef59d2d0f7 11 The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
jburhenn 0:09ef59d2d0f7 12 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
jburhenn 0:09ef59d2d0f7 13 WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE AUTHORS OR
jburhenn 0:09ef59d2d0f7 14 COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
jburhenn 0:09ef59d2d0f7 15 ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
jburhenn 0:09ef59d2d0f7 16 */
jburhenn 0:09ef59d2d0f7 17
jburhenn 0:09ef59d2d0f7 18 #ifndef _MQTTNETWORKIDW01M1_h
jburhenn 0:09ef59d2d0f7 19 #define _MQTTNETWORKIDW01M1_h
jburhenn 0:09ef59d2d0f7 20
jburhenn 0:09ef59d2d0f7 21 #include "mbed.h"
jburhenn 0:09ef59d2d0f7 22 #include "TCPSocket.h"
jburhenn 0:09ef59d2d0f7 23
jburhenn 0:09ef59d2d0f7 24 /**
jburhenn 0:09ef59d2d0f7 25 * Networking class for use with MQTTClient.
jburhenn 0:09ef59d2d0f7 26 * @param Interface A network interface class with the methods: init, connect and disconnect.
jburhenn 0:09ef59d2d0f7 27 */
jburhenn 0:09ef59d2d0f7 28 template<class Interface>
jburhenn 0:09ef59d2d0f7 29 class MQTTNetwork
jburhenn 0:09ef59d2d0f7 30 {
jburhenn 0:09ef59d2d0f7 31 public:
jburhenn 0:09ef59d2d0f7 32 /**
jburhenn 0:09ef59d2d0f7 33 * Construct the network interface.
jburhenn 0:09ef59d2d0f7 34 * @param[in] interface The network interface to use
jburhenn 0:09ef59d2d0f7 35 */
jburhenn 0:09ef59d2d0f7 36 MQTTNetwork(Interface& interface) : _interface(interface), _connected(false) {
jburhenn 0:09ef59d2d0f7 37 _socket.open(&_interface);
jburhenn 0:09ef59d2d0f7 38 }
jburhenn 0:09ef59d2d0f7 39
jburhenn 0:09ef59d2d0f7 40 /**
jburhenn 0:09ef59d2d0f7 41 * Connect the network interface.
jburhenn 0:09ef59d2d0f7 42 * @param[in] hostname Host for connection
jburhenn 0:09ef59d2d0f7 43 * @param[in] port Port for connection
jburhenn 0:09ef59d2d0f7 44 * @param[in] timeout_ms Timeout for the read operation, in milliseconds
jburhenn 0:09ef59d2d0f7 45 * @return 0 on success, -1 on failure.
jburhenn 0:09ef59d2d0f7 46 */
jburhenn 0:09ef59d2d0f7 47 int connect(char* hostname, int port, int timeout_ms = 1000) {
jburhenn 0:09ef59d2d0f7 48 //_socket.set_blocking(false);
jburhenn 0:09ef59d2d0f7 49 _socket.set_timeout(timeout_ms);
jburhenn 0:09ef59d2d0f7 50 int ret = _socket.connect(hostname, port);
jburhenn 0:09ef59d2d0f7 51 _connected = (ret == 0);
jburhenn 0:09ef59d2d0f7 52 return ret;
jburhenn 0:09ef59d2d0f7 53 }
jburhenn 0:09ef59d2d0f7 54
jburhenn 0:09ef59d2d0f7 55 /**
jburhenn 0:09ef59d2d0f7 56 * Disconnect the network interface.
jburhenn 0:09ef59d2d0f7 57 * @return true if disconnect was successful, false otherwise
jburhenn 0:09ef59d2d0f7 58 */
jburhenn 0:09ef59d2d0f7 59 bool disconnect()
jburhenn 0:09ef59d2d0f7 60 {
jburhenn 0:09ef59d2d0f7 61 _connected = false;
jburhenn 0:09ef59d2d0f7 62 return _socket.close();
jburhenn 0:09ef59d2d0f7 63 }
jburhenn 0:09ef59d2d0f7 64
jburhenn 0:09ef59d2d0f7 65 /**
jburhenn 0:09ef59d2d0f7 66 * Check if connected.
jburhenn 0:09ef59d2d0f7 67 * @return true if connected, false otherwise
jburhenn 0:09ef59d2d0f7 68 */
jburhenn 0:09ef59d2d0f7 69 bool connected()
jburhenn 0:09ef59d2d0f7 70 {
jburhenn 0:09ef59d2d0f7 71 return _connected;
jburhenn 0:09ef59d2d0f7 72 }
jburhenn 0:09ef59d2d0f7 73
jburhenn 0:09ef59d2d0f7 74 /**
jburhenn 0:09ef59d2d0f7 75 * Read data from the network.
jburhenn 0:09ef59d2d0f7 76 * @param[out] buffer Buffer that receives the data
jburhenn 0:09ef59d2d0f7 77 * @param[in] len Buffer length
jburhenn 0:09ef59d2d0f7 78 * @param[in] timeout_ms Timeout for the read operation, in milliseconds
jburhenn 11:532406b58e8f 79 * @return Number of bytes read, or a negative value if there was an error
jburhenn 0:09ef59d2d0f7 80 */
jburhenn 0:09ef59d2d0f7 81 int read(unsigned char* buffer, int len, int timeout_ms) {
jburhenn 0:09ef59d2d0f7 82 _socket.set_timeout(timeout_ms);
jburhenn 0:09ef59d2d0f7 83 return _socket.recv((char*)buffer, len);
jburhenn 0:09ef59d2d0f7 84 }
jburhenn 0:09ef59d2d0f7 85
jburhenn 0:09ef59d2d0f7 86 /**
jburhenn 0:09ef59d2d0f7 87 * Write data to the network.
jburhenn 0:09ef59d2d0f7 88 * @param[in] buffer Buffer that contains data to write
jburhenn 0:09ef59d2d0f7 89 * @param[in] len Number of bytes to write
jburhenn 0:09ef59d2d0f7 90 * @param[in] timeout_ms Timeout for the write operation, in milliseconds
jburhenn 11:532406b58e8f 91 * @return Number of bytes written, or a negative value if there was an error
jburhenn 0:09ef59d2d0f7 92 */
jburhenn 0:09ef59d2d0f7 93 int write(unsigned char* buffer, int len, int timeout_ms) {
jburhenn 0:09ef59d2d0f7 94 _socket.set_timeout(timeout_ms);
jburhenn 0:09ef59d2d0f7 95 return _socket.send((char*)buffer, len);
jburhenn 0:09ef59d2d0f7 96 }
jburhenn 0:09ef59d2d0f7 97
jburhenn 0:09ef59d2d0f7 98 private:
jburhenn 0:09ef59d2d0f7 99 Interface& _interface;
jburhenn 0:09ef59d2d0f7 100 TCPSocket _socket;
jburhenn 0:09ef59d2d0f7 101 bool _connected;
jburhenn 0:09ef59d2d0f7 102 };
jburhenn 0:09ef59d2d0f7 103
jburhenn 0:09ef59d2d0f7 104 #endif