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