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