Library for MQTT
mbed-mqtt-master/src/MQTTClientMbedOs.h@0:ba7e439238ab, 2022-06-20 (annotated)
- Committer:
- pavleradojkovic
- Date:
- Mon Jun 20 16:24:43 2022 +0000
- Revision:
- 0:ba7e439238ab
Inital commit
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
pavleradojkovic | 0:ba7e439238ab | 1 | /* |
pavleradojkovic | 0:ba7e439238ab | 2 | * Copyright (c) 2019, ARM Limited, All Rights Reserved |
pavleradojkovic | 0:ba7e439238ab | 3 | * SPDX-License-Identifier: Apache-2.0 |
pavleradojkovic | 0:ba7e439238ab | 4 | * |
pavleradojkovic | 0:ba7e439238ab | 5 | * Licensed under the Apache License, Version 2.0 (the "License"); you may |
pavleradojkovic | 0:ba7e439238ab | 6 | * not use this file except in compliance with the License. |
pavleradojkovic | 0:ba7e439238ab | 7 | * You may obtain a copy of the License at |
pavleradojkovic | 0:ba7e439238ab | 8 | * |
pavleradojkovic | 0:ba7e439238ab | 9 | * http://www.apache.org/licenses/LICENSE-2.0 |
pavleradojkovic | 0:ba7e439238ab | 10 | * |
pavleradojkovic | 0:ba7e439238ab | 11 | * Unless required by applicable law or agreed to in writing, software |
pavleradojkovic | 0:ba7e439238ab | 12 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
pavleradojkovic | 0:ba7e439238ab | 13 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
pavleradojkovic | 0:ba7e439238ab | 14 | * See the License for the specific language governing permissions and |
pavleradojkovic | 0:ba7e439238ab | 15 | * limitations under the License. |
pavleradojkovic | 0:ba7e439238ab | 16 | */ |
pavleradojkovic | 0:ba7e439238ab | 17 | |
pavleradojkovic | 0:ba7e439238ab | 18 | #ifndef MQTT_CLIENT_MBED_OS_H |
pavleradojkovic | 0:ba7e439238ab | 19 | #define MQTT_CLIENT_MBED_OS_H |
pavleradojkovic | 0:ba7e439238ab | 20 | |
pavleradojkovic | 0:ba7e439238ab | 21 | #include <TCPSocket.h> |
pavleradojkovic | 0:ba7e439238ab | 22 | #include <TLSSocket.h> |
pavleradojkovic | 0:ba7e439238ab | 23 | #include <DTLSSocket.h> |
pavleradojkovic | 0:ba7e439238ab | 24 | #include <UDPSocket.h> |
pavleradojkovic | 0:ba7e439238ab | 25 | #include "unity/unity.h" |
pavleradojkovic | 0:ba7e439238ab | 26 | |
pavleradojkovic | 0:ba7e439238ab | 27 | #include "FP.h" |
pavleradojkovic | 0:ba7e439238ab | 28 | #include <MQTTPacket.h> |
pavleradojkovic | 0:ba7e439238ab | 29 | #include <MQTTClient.h> |
pavleradojkovic | 0:ba7e439238ab | 30 | #include <MQTTSNPacket.h> |
pavleradojkovic | 0:ba7e439238ab | 31 | #include <MQTTSNClient.h> |
pavleradojkovic | 0:ba7e439238ab | 32 | #include <MQTTmbed.h> // Countdown |
pavleradojkovic | 0:ba7e439238ab | 33 | |
pavleradojkovic | 0:ba7e439238ab | 34 | /** |
pavleradojkovic | 0:ba7e439238ab | 35 | * @brief Implementation of the Network class template parameter of MQTTClient. |
pavleradojkovic | 0:ba7e439238ab | 36 | */ |
pavleradojkovic | 0:ba7e439238ab | 37 | class MQTTNetworkMbedOs { |
pavleradojkovic | 0:ba7e439238ab | 38 | public: |
pavleradojkovic | 0:ba7e439238ab | 39 | /** |
pavleradojkovic | 0:ba7e439238ab | 40 | * @brief Construct the network implementation. |
pavleradojkovic | 0:ba7e439238ab | 41 | * |
pavleradojkovic | 0:ba7e439238ab | 42 | * If TCPSocket or TLSSocket are provided, the MQTT protocol will be used. |
pavleradojkovic | 0:ba7e439238ab | 43 | * If UDPSocket or DTLSSocket are provided, the MQTT-SN protocol will be used. |
pavleradojkovic | 0:ba7e439238ab | 44 | * |
pavleradojkovic | 0:ba7e439238ab | 45 | * @param _socket socket to be used for MQTT communication. |
pavleradojkovic | 0:ba7e439238ab | 46 | */ |
pavleradojkovic | 0:ba7e439238ab | 47 | MQTTNetworkMbedOs(Socket *_socket) : socket(_socket) {} |
pavleradojkovic | 0:ba7e439238ab | 48 | |
pavleradojkovic | 0:ba7e439238ab | 49 | /** |
pavleradojkovic | 0:ba7e439238ab | 50 | * @brief Read data from the socket. |
pavleradojkovic | 0:ba7e439238ab | 51 | * |
pavleradojkovic | 0:ba7e439238ab | 52 | * @param buffer buffer to store the data |
pavleradojkovic | 0:ba7e439238ab | 53 | * @param len expected amount of bytes |
pavleradojkovic | 0:ba7e439238ab | 54 | * @param timeout timeout for the operation |
pavleradojkovic | 0:ba7e439238ab | 55 | */ |
pavleradojkovic | 0:ba7e439238ab | 56 | int read(unsigned char *buffer, int len, int timeout); |
pavleradojkovic | 0:ba7e439238ab | 57 | |
pavleradojkovic | 0:ba7e439238ab | 58 | /** |
pavleradojkovic | 0:ba7e439238ab | 59 | * @brief Write data to the socket. |
pavleradojkovic | 0:ba7e439238ab | 60 | * |
pavleradojkovic | 0:ba7e439238ab | 61 | * @param buffer buffer that contains data to be written |
pavleradojkovic | 0:ba7e439238ab | 62 | * @param len amount of bytes to write |
pavleradojkovic | 0:ba7e439238ab | 63 | * @param timeout timeout for the operation |
pavleradojkovic | 0:ba7e439238ab | 64 | */ |
pavleradojkovic | 0:ba7e439238ab | 65 | int write(unsigned char *buffer, int len, int timeout); |
pavleradojkovic | 0:ba7e439238ab | 66 | |
pavleradojkovic | 0:ba7e439238ab | 67 | /** |
pavleradojkovic | 0:ba7e439238ab | 68 | * @brief Connect the socket to the hostname at a given port. |
pavleradojkovic | 0:ba7e439238ab | 69 | * |
pavleradojkovic | 0:ba7e439238ab | 70 | * The socket must have the network interface set up and connected before. |
pavleradojkovic | 0:ba7e439238ab | 71 | * This connect() is different from MQTTClient::connect, which performs the |
pavleradojkovic | 0:ba7e439238ab | 72 | * connection to a broker, over the connected socket. |
pavleradojkovic | 0:ba7e439238ab | 73 | * |
pavleradojkovic | 0:ba7e439238ab | 74 | * @param hostname Hostname to connect to. This can be a string containing |
pavleradojkovic | 0:ba7e439238ab | 75 | * IP address like "192.168.52.10" or domain address, like "mqtt.flespi.io" |
pavleradojkovic | 0:ba7e439238ab | 76 | * @param port port number to be used for connection |
pavleradojkovic | 0:ba7e439238ab | 77 | */ |
pavleradojkovic | 0:ba7e439238ab | 78 | int connect(const char *hostname, int port); |
pavleradojkovic | 0:ba7e439238ab | 79 | |
pavleradojkovic | 0:ba7e439238ab | 80 | /** |
pavleradojkovic | 0:ba7e439238ab | 81 | * @brief Disconnect from the hostname. |
pavleradojkovic | 0:ba7e439238ab | 82 | */ |
pavleradojkovic | 0:ba7e439238ab | 83 | int disconnect(); |
pavleradojkovic | 0:ba7e439238ab | 84 | |
pavleradojkovic | 0:ba7e439238ab | 85 | private: |
pavleradojkovic | 0:ba7e439238ab | 86 | Socket *socket; |
pavleradojkovic | 0:ba7e439238ab | 87 | }; |
pavleradojkovic | 0:ba7e439238ab | 88 | |
pavleradojkovic | 0:ba7e439238ab | 89 | /** |
pavleradojkovic | 0:ba7e439238ab | 90 | * @brief MQTT client mbed-os wrapper class |
pavleradojkovic | 0:ba7e439238ab | 91 | * |
pavleradojkovic | 0:ba7e439238ab | 92 | * This class wraps around the paho library templated MQTT(-SN)Client. |
pavleradojkovic | 0:ba7e439238ab | 93 | * It depends on the type of socket provided whether MQTT or MQTT-SN will be used. |
pavleradojkovic | 0:ba7e439238ab | 94 | * MQTTNetworkMbedOs will be used as a Network for the paho MQTTClient. |
pavleradojkovic | 0:ba7e439238ab | 95 | */ |
pavleradojkovic | 0:ba7e439238ab | 96 | class MQTTClient { |
pavleradojkovic | 0:ba7e439238ab | 97 | public: |
pavleradojkovic | 0:ba7e439238ab | 98 | /** MQTT message handler */ |
pavleradojkovic | 0:ba7e439238ab | 99 | typedef void (*messageHandler)(MQTT::MessageData &); |
pavleradojkovic | 0:ba7e439238ab | 100 | /** MQTT-SN message handler */ |
pavleradojkovic | 0:ba7e439238ab | 101 | typedef void (*messageHandlerSN)(MQTTSN::MessageData &); |
pavleradojkovic | 0:ba7e439238ab | 102 | |
pavleradojkovic | 0:ba7e439238ab | 103 | /** |
pavleradojkovic | 0:ba7e439238ab | 104 | * @brief Constructor for the TCPSocket-based communication. |
pavleradojkovic | 0:ba7e439238ab | 105 | * MQTT protocol will be used. |
pavleradojkovic | 0:ba7e439238ab | 106 | * |
pavleradojkovic | 0:ba7e439238ab | 107 | * @param _socket socket to be used for communication |
pavleradojkovic | 0:ba7e439238ab | 108 | */ |
pavleradojkovic | 0:ba7e439238ab | 109 | MQTTClient(TCPSocket *_socket); |
pavleradojkovic | 0:ba7e439238ab | 110 | #if defined(MBEDTLS_SSL_CLI_C) || defined(DOXYGEN_ONLY) |
pavleradojkovic | 0:ba7e439238ab | 111 | /** |
pavleradojkovic | 0:ba7e439238ab | 112 | * @brief Constructor for the TLSSocket-based communication. |
pavleradojkovic | 0:ba7e439238ab | 113 | * MQTT protocol will be used over a secure socket. |
pavleradojkovic | 0:ba7e439238ab | 114 | * |
pavleradojkovic | 0:ba7e439238ab | 115 | * @param _socket socket to be used for communication |
pavleradojkovic | 0:ba7e439238ab | 116 | */ |
pavleradojkovic | 0:ba7e439238ab | 117 | MQTTClient(TLSSocket *_socket); |
pavleradojkovic | 0:ba7e439238ab | 118 | #endif |
pavleradojkovic | 0:ba7e439238ab | 119 | /** |
pavleradojkovic | 0:ba7e439238ab | 120 | * @brief Constructor for the UDPSocket-based communication. |
pavleradojkovic | 0:ba7e439238ab | 121 | * MQTT-SN protocol will be used. |
pavleradojkovic | 0:ba7e439238ab | 122 | * |
pavleradojkovic | 0:ba7e439238ab | 123 | * @param _socket socket to be used for communication |
pavleradojkovic | 0:ba7e439238ab | 124 | */ |
pavleradojkovic | 0:ba7e439238ab | 125 | MQTTClient(UDPSocket *_socket); |
pavleradojkovic | 0:ba7e439238ab | 126 | #if defined(MBEDTLS_SSL_CLI_C) || defined(DOXYGEN_ONLY) |
pavleradojkovic | 0:ba7e439238ab | 127 | /** |
pavleradojkovic | 0:ba7e439238ab | 128 | * @brief Constructor for the DTLSSocket-based communication. |
pavleradojkovic | 0:ba7e439238ab | 129 | * MQTT-SN protocol will be used over a secure socket. |
pavleradojkovic | 0:ba7e439238ab | 130 | * |
pavleradojkovic | 0:ba7e439238ab | 131 | * @param _socket socket to be used for communication |
pavleradojkovic | 0:ba7e439238ab | 132 | */ |
pavleradojkovic | 0:ba7e439238ab | 133 | MQTTClient(DTLSSocket *_socket); |
pavleradojkovic | 0:ba7e439238ab | 134 | #endif |
pavleradojkovic | 0:ba7e439238ab | 135 | |
pavleradojkovic | 0:ba7e439238ab | 136 | /** |
pavleradojkovic | 0:ba7e439238ab | 137 | * @brief Destructor for the TCPSocket-based communication. |
pavleradojkovic | 0:ba7e439238ab | 138 | */ |
pavleradojkovic | 0:ba7e439238ab | 139 | ~MQTTClient(); |
pavleradojkovic | 0:ba7e439238ab | 140 | |
pavleradojkovic | 0:ba7e439238ab | 141 | /** |
pavleradojkovic | 0:ba7e439238ab | 142 | * @brief Connect to the MQTT broker |
pavleradojkovic | 0:ba7e439238ab | 143 | * |
pavleradojkovic | 0:ba7e439238ab | 144 | * This is different from connecting to the server, done in MQTTNetworkMbedOs::connect() |
pavleradojkovic | 0:ba7e439238ab | 145 | * and also different from connecting the network interface, that the socket is using. |
pavleradojkovic | 0:ba7e439238ab | 146 | * |
pavleradojkovic | 0:ba7e439238ab | 147 | * @param options options to be used for the connection. |
pavleradojkovic | 0:ba7e439238ab | 148 | * @retval NSAPI_ERROR_OK on success, error code on failure |
pavleradojkovic | 0:ba7e439238ab | 149 | */ |
pavleradojkovic | 0:ba7e439238ab | 150 | nsapi_error_t connect(MQTTPacket_connectData &options); |
pavleradojkovic | 0:ba7e439238ab | 151 | /** |
pavleradojkovic | 0:ba7e439238ab | 152 | * @brief Connect to the MQTT broker via an MQTT-SN gateway |
pavleradojkovic | 0:ba7e439238ab | 153 | * |
pavleradojkovic | 0:ba7e439238ab | 154 | * This is different from connecting to the server, done in MQTTNetworkMbedOs::connect() |
pavleradojkovic | 0:ba7e439238ab | 155 | * and also different from connecting the network interface, that the socket is using. |
pavleradojkovic | 0:ba7e439238ab | 156 | * |
pavleradojkovic | 0:ba7e439238ab | 157 | * @param options options to be used for the connection. |
pavleradojkovic | 0:ba7e439238ab | 158 | * @retval NSAPI_ERROR_OK on success, error code on failure |
pavleradojkovic | 0:ba7e439238ab | 159 | */ |
pavleradojkovic | 0:ba7e439238ab | 160 | nsapi_error_t connect(MQTTSNPacket_connectData &options); |
pavleradojkovic | 0:ba7e439238ab | 161 | |
pavleradojkovic | 0:ba7e439238ab | 162 | /** |
pavleradojkovic | 0:ba7e439238ab | 163 | * @brief Publish message to a topic. |
pavleradojkovic | 0:ba7e439238ab | 164 | * @param topicName string with a topic name |
pavleradojkovic | 0:ba7e439238ab | 165 | * @param message message to be published |
pavleradojkovic | 0:ba7e439238ab | 166 | * @retval NSAPI_ERROR_OK on success, error code on failure |
pavleradojkovic | 0:ba7e439238ab | 167 | */ |
pavleradojkovic | 0:ba7e439238ab | 168 | nsapi_error_t publish(const char *topicName, MQTT::Message &message); |
pavleradojkovic | 0:ba7e439238ab | 169 | /** |
pavleradojkovic | 0:ba7e439238ab | 170 | * @brief Publish message to an MQTT-SN topic. |
pavleradojkovic | 0:ba7e439238ab | 171 | * @param topicName MQTTSN_topicid structure specifying the topic. |
pavleradojkovic | 0:ba7e439238ab | 172 | * @param message message to be published |
pavleradojkovic | 0:ba7e439238ab | 173 | * @retval NSAPI_ERROR_OK on success, error code on failure |
pavleradojkovic | 0:ba7e439238ab | 174 | */ |
pavleradojkovic | 0:ba7e439238ab | 175 | nsapi_error_t publish(MQTTSN_topicid &topicName, MQTTSN::Message &message); |
pavleradojkovic | 0:ba7e439238ab | 176 | |
pavleradojkovic | 0:ba7e439238ab | 177 | /** |
pavleradojkovic | 0:ba7e439238ab | 178 | * @brief Subscribe to a topic. |
pavleradojkovic | 0:ba7e439238ab | 179 | * @param topicFilter string with a topic filter |
pavleradojkovic | 0:ba7e439238ab | 180 | * @param qos level of qos to be received |
pavleradojkovic | 0:ba7e439238ab | 181 | * @param mh message handler to be called upon message reception |
pavleradojkovic | 0:ba7e439238ab | 182 | * @retval NSAPI_ERROR_OK on success, error code on failure |
pavleradojkovic | 0:ba7e439238ab | 183 | */ |
pavleradojkovic | 0:ba7e439238ab | 184 | nsapi_error_t subscribe(const char *topicFilter, enum MQTT::QoS qos, messageHandler mh); |
pavleradojkovic | 0:ba7e439238ab | 185 | /** |
pavleradojkovic | 0:ba7e439238ab | 186 | * @brief Subscribe to an MQTT-SN topic. |
pavleradojkovic | 0:ba7e439238ab | 187 | * @param topicFilter string with a topic filter |
pavleradojkovic | 0:ba7e439238ab | 188 | * @param qos level of qos to be received |
pavleradojkovic | 0:ba7e439238ab | 189 | * @param mh message handler to be called upon message reception |
pavleradojkovic | 0:ba7e439238ab | 190 | * @retval NSAPI_ERROR_OK on success, error code on failure |
pavleradojkovic | 0:ba7e439238ab | 191 | */ |
pavleradojkovic | 0:ba7e439238ab | 192 | nsapi_error_t subscribe(MQTTSN_topicid &topicFilter, enum MQTTSN::QoS qos, messageHandlerSN mh); |
pavleradojkovic | 0:ba7e439238ab | 193 | |
pavleradojkovic | 0:ba7e439238ab | 194 | /** |
pavleradojkovic | 0:ba7e439238ab | 195 | * @brief Unsubscribe from a topic. |
pavleradojkovic | 0:ba7e439238ab | 196 | * @param topicFilter string with a topic filter |
pavleradojkovic | 0:ba7e439238ab | 197 | * @retval NSAPI_ERROR_OK on success, error code on failure |
pavleradojkovic | 0:ba7e439238ab | 198 | */ |
pavleradojkovic | 0:ba7e439238ab | 199 | nsapi_error_t unsubscribe(const char *topicFilter); |
pavleradojkovic | 0:ba7e439238ab | 200 | /** |
pavleradojkovic | 0:ba7e439238ab | 201 | * @brief Unsubscribe from an MQTT-SN topic. |
pavleradojkovic | 0:ba7e439238ab | 202 | * @param topicFilter string with a topic filter |
pavleradojkovic | 0:ba7e439238ab | 203 | * @retval NSAPI_ERROR_OK on success, error code on failure |
pavleradojkovic | 0:ba7e439238ab | 204 | */ |
pavleradojkovic | 0:ba7e439238ab | 205 | nsapi_error_t unsubscribe(MQTTSN_topicid &topicFilter); |
pavleradojkovic | 0:ba7e439238ab | 206 | |
pavleradojkovic | 0:ba7e439238ab | 207 | /** |
pavleradojkovic | 0:ba7e439238ab | 208 | * @brief Yield current thread execution and handle other events |
pavleradojkovic | 0:ba7e439238ab | 209 | * @param timeout_ms ms to yield for |
pavleradojkovic | 0:ba7e439238ab | 210 | * @retval NSAPI_ERROR_OK on success, error code on failure |
pavleradojkovic | 0:ba7e439238ab | 211 | */ |
pavleradojkovic | 0:ba7e439238ab | 212 | nsapi_error_t yield(unsigned long timeout_ms = 1000L) ; |
pavleradojkovic | 0:ba7e439238ab | 213 | |
pavleradojkovic | 0:ba7e439238ab | 214 | /** |
pavleradojkovic | 0:ba7e439238ab | 215 | * @brief Disconnect from a broker, that the client has been connected to. |
pavleradojkovic | 0:ba7e439238ab | 216 | * @retval NSAPI_ERROR_OK on success, error code on failure |
pavleradojkovic | 0:ba7e439238ab | 217 | */ |
pavleradojkovic | 0:ba7e439238ab | 218 | nsapi_error_t disconnect(); |
pavleradojkovic | 0:ba7e439238ab | 219 | |
pavleradojkovic | 0:ba7e439238ab | 220 | /** |
pavleradojkovic | 0:ba7e439238ab | 221 | * @brief Check whether client is connected to a broker. |
pavleradojkovic | 0:ba7e439238ab | 222 | * @retval true if the client is connected, false otherwise |
pavleradojkovic | 0:ba7e439238ab | 223 | */ |
pavleradojkovic | 0:ba7e439238ab | 224 | bool isConnected(); |
pavleradojkovic | 0:ba7e439238ab | 225 | |
pavleradojkovic | 0:ba7e439238ab | 226 | /** Set the default message handling callback - used for any message which does not match a subscription message handler |
pavleradojkovic | 0:ba7e439238ab | 227 | * @param mh - pointer to the callback function. Set to 0 to remove. |
pavleradojkovic | 0:ba7e439238ab | 228 | */ |
pavleradojkovic | 0:ba7e439238ab | 229 | void setDefaultMessageHandler(messageHandler mh); |
pavleradojkovic | 0:ba7e439238ab | 230 | |
pavleradojkovic | 0:ba7e439238ab | 231 | /** Set a message handling callback. This can be used outside of the the subscribe method. |
pavleradojkovic | 0:ba7e439238ab | 232 | * @param topicFilter - a topic pattern which can include wildcards |
pavleradojkovic | 0:ba7e439238ab | 233 | * @param mh - pointer to the callback function. If 0, removes the callback if any |
pavleradojkovic | 0:ba7e439238ab | 234 | */ |
pavleradojkovic | 0:ba7e439238ab | 235 | nsapi_error_t setMessageHandler(const char *topicFilter, messageHandler mh); |
pavleradojkovic | 0:ba7e439238ab | 236 | |
pavleradojkovic | 0:ba7e439238ab | 237 | private: |
pavleradojkovic | 0:ba7e439238ab | 238 | /** |
pavleradojkovic | 0:ba7e439238ab | 239 | * @brief Helper function to initialize member variables. |
pavleradojkovic | 0:ba7e439238ab | 240 | */ |
pavleradojkovic | 0:ba7e439238ab | 241 | void init(Socket *sock); |
pavleradojkovic | 0:ba7e439238ab | 242 | |
pavleradojkovic | 0:ba7e439238ab | 243 | Socket *socket; |
pavleradojkovic | 0:ba7e439238ab | 244 | MQTTNetworkMbedOs *mqttNet; |
pavleradojkovic | 0:ba7e439238ab | 245 | NetworkInterface *net; |
pavleradojkovic | 0:ba7e439238ab | 246 | |
pavleradojkovic | 0:ba7e439238ab | 247 | MQTT::Client<MQTTNetworkMbedOs, Countdown, MBED_CONF_MBED_MQTT_MAX_PACKET_SIZE, MBED_CONF_MBED_MQTT_MAX_CONNECTIONS> *client; |
pavleradojkovic | 0:ba7e439238ab | 248 | MQTTSN::Client<MQTTNetworkMbedOs, Countdown, MBED_CONF_MBED_MQTT_MAX_PACKET_SIZE, MBED_CONF_MBED_MQTT_MAX_CONNECTIONS> *clientSN; |
pavleradojkovic | 0:ba7e439238ab | 249 | }; |
pavleradojkovic | 0:ba7e439238ab | 250 | |
pavleradojkovic | 0:ba7e439238ab | 251 | #endif // MQTT_CLIENT_MBED_OS_H |