Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
MQTTClientMbedOs.h
00001 /* 00002 * Copyright (c) 2019, ARM Limited, All Rights Reserved 00003 * SPDX-License-Identifier: Apache-2.0 00004 * 00005 * Licensed under the Apache License, Version 2.0 (the "License"); you may 00006 * not use this file except in compliance with the License. 00007 * You may obtain a copy of the License at 00008 * 00009 * http://www.apache.org/licenses/LICENSE-2.0 00010 * 00011 * Unless required by applicable law or agreed to in writing, software 00012 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 00013 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 00014 * See the License for the specific language governing permissions and 00015 * limitations under the License. 00016 */ 00017 00018 #ifndef MQTT_CLIENT_MBED_OS_H 00019 #define MQTT_CLIENT_MBED_OS_H 00020 00021 #define MQTTCLIENT_QOS2 1 00022 00023 #include <TCPSocket.h> 00024 #include <TLSSocket.h> 00025 #include <DTLSSocket.h> 00026 #include <UDPSocket.h> 00027 #include "unity/unity.h" 00028 00029 #include "FP.h" 00030 #include <MQTTPacket.h> 00031 #include <MQTTClient.h> 00032 #include <MQTTSNPacket.h> 00033 #include <MQTTSNClient.h> 00034 #include <MQTTmbed.h> // Countdown 00035 00036 /** 00037 * @brief Implementation of the Network class template parameter of MQTTClient. 00038 */ 00039 class MQTTNetworkMbedOs { 00040 public: 00041 /** 00042 * @brief Construct the network implementation. 00043 * 00044 * If TCPSocket or TLSSocket are provided, the MQTT protocol will be used. 00045 * If UDPSocket or DTLSSocket are provided, the MQTT-SN protocol will be used. 00046 * 00047 * @param _socket socket to be used for MQTT communication. 00048 */ 00049 MQTTNetworkMbedOs(Socket *_socket) : socket(_socket) {} 00050 00051 /** 00052 * @brief Read data from the socket. 00053 * 00054 * @param buffer buffer to store the data 00055 * @param len expected amount of bytes 00056 * @param timeout timeout for the operation 00057 */ 00058 int read(unsigned char *buffer, int len, int timeout); 00059 00060 /** 00061 * @brief Write data to the socket. 00062 * 00063 * @param buffer buffer that contains data to be written 00064 * @param len amount of bytes to write 00065 * @param timeout timeout for the operation 00066 */ 00067 int write(unsigned char *buffer, int len, int timeout); 00068 00069 /** 00070 * @brief Connect the socket to the hostname at a given port. 00071 * 00072 * The socket must have the network interface set up and connected before. 00073 * This connect() is different from MQTTClient::connect, which performs the 00074 * connection to a broker, over the connected socket. 00075 * 00076 * @param hostname Hostname to connect to. This can be a string containing 00077 * IP address like "192.168.52.10" or domain address, like "mqtt.flespi.io" 00078 * @param port port number to be used for connection 00079 */ 00080 int connect(const char *hostname, int port); 00081 00082 /** 00083 * @brief Disconnect from the hostname. 00084 */ 00085 int disconnect(); 00086 00087 private: 00088 Socket *socket; 00089 }; 00090 00091 /** 00092 * @brief MQTT client mbed-os wrapper class 00093 * 00094 * This class wraps around the paho library templated MQTT(-SN)Client. 00095 * It depends on the type of socket provided whether MQTT or MQTT-SN will be used. 00096 * MQTTNetworkMbedOs will be used as a Network for the paho MQTTClient. 00097 */ 00098 class MQTTClient { 00099 public: 00100 /** MQTT message handler */ 00101 typedef void (*messageHandler)(MQTT::MessageData &); 00102 /** MQTT-SN message handler */ 00103 typedef void (*messageHandlerSN)(MQTTSN::MessageData &); 00104 00105 /** 00106 * @brief Constructor for the TCPSocket-based communication. 00107 * MQTT protocol will be used. 00108 * 00109 * @param _socket socket to be used for communication 00110 */ 00111 MQTTClient(TCPSocket *_socket); 00112 #if defined(MBEDTLS_SSL_CLI_C) || defined(DOXYGEN_ONLY) 00113 /** 00114 * @brief Constructor for the TLSSocket-based communication. 00115 * MQTT protocol will be used over a secure socket. 00116 * 00117 * @param _socket socket to be used for communication 00118 */ 00119 MQTTClient(TLSSocket *_socket); 00120 #endif 00121 /** 00122 * @brief Constructor for the UDPSocket-based communication. 00123 * MQTT-SN protocol will be used. 00124 * 00125 * @param _socket socket to be used for communication 00126 */ 00127 MQTTClient(UDPSocket *_socket); 00128 #if defined(MBEDTLS_SSL_CLI_C) || defined(DOXYGEN_ONLY) 00129 /** 00130 * @brief Constructor for the DTLSSocket-based communication. 00131 * MQTT-SN protocol will be used over a secure socket. 00132 * 00133 * @param _socket socket to be used for communication 00134 */ 00135 MQTTClient(DTLSSocket *_socket); 00136 #endif 00137 00138 /** 00139 * @brief Destructor for the TCPSocket-based communication. 00140 */ 00141 ~MQTTClient(); 00142 00143 /** 00144 * @brief Connect to the MQTT broker 00145 * 00146 * This is different from connecting to the server, done in MQTTNetworkMbedOs::connect() 00147 * and also different from connecting the network interface, that the socket is using. 00148 * 00149 * @param options options to be used for the connection. 00150 * @retval NSAPI_ERROR_OK on success, error code on failure 00151 */ 00152 nsapi_error_t connect(MQTTPacket_connectData &options); 00153 /** 00154 * @brief Connect to the MQTT broker via an MQTT-SN gateway 00155 * 00156 * This is different from connecting to the server, done in MQTTNetworkMbedOs::connect() 00157 * and also different from connecting the network interface, that the socket is using. 00158 * 00159 * @param options options to be used for the connection. 00160 * @retval NSAPI_ERROR_OK on success, error code on failure 00161 */ 00162 nsapi_error_t connect(MQTTSNPacket_connectData &options); 00163 00164 /** 00165 * @brief Publish message to a topic. 00166 * @param topicName string with a topic name 00167 * @param message message to be published 00168 * @retval NSAPI_ERROR_OK on success, error code on failure 00169 */ 00170 nsapi_error_t publish(const char *topicName, MQTT::Message &message); 00171 /** 00172 * @brief Publish message to an MQTT-SN topic. 00173 * @param topicName MQTTSN_topicid structure specifying the topic. 00174 * @param message message to be published 00175 * @retval NSAPI_ERROR_OK on success, error code on failure 00176 */ 00177 nsapi_error_t publish(MQTTSN_topicid &topicName, MQTTSN::Message &message); 00178 00179 /** 00180 * @brief Subscribe to a topic. 00181 * @param topicFilter string with a topic filter 00182 * @param qos level of qos to be received 00183 * @param mh message handler to be called upon message reception 00184 * @retval NSAPI_ERROR_OK on success, error code on failure 00185 */ 00186 nsapi_error_t subscribe(const char *topicFilter, enum MQTT::QoS qos, messageHandler mh); 00187 /** 00188 * @brief Subscribe to an MQTT-SN topic. 00189 * @param topicFilter string with a topic filter 00190 * @param qos level of qos to be received 00191 * @param mh message handler to be called upon message reception 00192 * @retval NSAPI_ERROR_OK on success, error code on failure 00193 */ 00194 nsapi_error_t subscribe(MQTTSN_topicid &topicFilter, enum MQTTSN::QoS qos, messageHandlerSN mh); 00195 00196 /** 00197 * @brief Unsubscribe from a topic. 00198 * @param topicFilter string with a topic filter 00199 * @retval NSAPI_ERROR_OK on success, error code on failure 00200 */ 00201 nsapi_error_t unsubscribe(const char *topicFilter); 00202 /** 00203 * @brief Unsubscribe from an MQTT-SN topic. 00204 * @param topicFilter string with a topic filter 00205 * @retval NSAPI_ERROR_OK on success, error code on failure 00206 */ 00207 nsapi_error_t unsubscribe(MQTTSN_topicid &topicFilter); 00208 00209 /** 00210 * @brief Yield current thread execution and handle other events 00211 * @param timeout_ms ms to yield for 00212 * @retval NSAPI_ERROR_OK on success, error code on failure 00213 */ 00214 nsapi_error_t yield(unsigned long timeout_ms = 1000L) ; 00215 00216 /** 00217 * @brief Disconnect from a broker, that the client has been connected to. 00218 * @retval NSAPI_ERROR_OK on success, error code on failure 00219 */ 00220 nsapi_error_t disconnect(); 00221 00222 /** 00223 * @brief Check whether client is connected to a broker. 00224 * @retval true if the client is connected, false otherwise 00225 */ 00226 bool isConnected(); 00227 00228 /** Set the default message handling callback - used for any message which does not match a subscription message handler 00229 * @param mh - pointer to the callback function. Set to 0 to remove. 00230 */ 00231 void setDefaultMessageHandler(messageHandler mh); 00232 00233 /** Set a message handling callback. This can be used outside of the the subscribe method. 00234 * @param topicFilter - a topic pattern which can include wildcards 00235 * @param mh - pointer to the callback function. If 0, removes the callback if any 00236 */ 00237 nsapi_error_t setMessageHandler(const char *topicFilter, messageHandler mh); 00238 00239 private: 00240 /** 00241 * @brief Helper function to initialize member variables. 00242 */ 00243 void init(Socket *sock); 00244 00245 Socket *socket; 00246 MQTTNetworkMbedOs *mqttNet; 00247 NetworkInterface *net; 00248 00249 MQTT::Client<MQTTNetworkMbedOs, Countdown, MBED_CONF_MBED_MQTT_MAX_PACKET_SIZE, MBED_CONF_MBED_MQTT_MAX_CONNECTIONS> *client; 00250 MQTTSN::Client<MQTTNetworkMbedOs, Countdown, MBED_CONF_MBED_MQTT_MAX_PACKET_SIZE, MBED_CONF_MBED_MQTT_MAX_CONNECTIONS> *clientSN; 00251 }; 00252 00253 #endif // MQTT_CLIENT_MBED_OS_H
Generated on Wed Jul 13 2022 10:46:02 by
1.7.2