Library for MQTT

Committer:
pavleradojkovic
Date:
Mon Jun 20 16:24:43 2022 +0000
Revision:
0:ba7e439238ab
Inital commit

Who changed what in which revision?

UserRevisionLine numberNew 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 #include "MQTTClientMbedOs.h"
pavleradojkovic 0:ba7e439238ab 19 #include "MQTTNetworkUtil.h"
pavleradojkovic 0:ba7e439238ab 20
pavleradojkovic 0:ba7e439238ab 21 int MQTTNetworkMbedOs::read(unsigned char *buffer, int len, int timeout)
pavleradojkovic 0:ba7e439238ab 22 {
pavleradojkovic 0:ba7e439238ab 23 return accumulate_mqtt_read(socket, buffer, len, timeout);
pavleradojkovic 0:ba7e439238ab 24 }
pavleradojkovic 0:ba7e439238ab 25
pavleradojkovic 0:ba7e439238ab 26 int MQTTNetworkMbedOs::write(unsigned char *buffer, int len, int timeout)
pavleradojkovic 0:ba7e439238ab 27 {
pavleradojkovic 0:ba7e439238ab 28 return mqtt_write(socket, buffer, len, timeout);
pavleradojkovic 0:ba7e439238ab 29 }
pavleradojkovic 0:ba7e439238ab 30
pavleradojkovic 0:ba7e439238ab 31 int MQTTNetworkMbedOs::connect(const char *hostname, int port)
pavleradojkovic 0:ba7e439238ab 32 {
pavleradojkovic 0:ba7e439238ab 33 SocketAddress sockAddr(hostname, port);
pavleradojkovic 0:ba7e439238ab 34 return socket->connect(sockAddr);
pavleradojkovic 0:ba7e439238ab 35 }
pavleradojkovic 0:ba7e439238ab 36
pavleradojkovic 0:ba7e439238ab 37 int MQTTNetworkMbedOs::disconnect()
pavleradojkovic 0:ba7e439238ab 38 {
pavleradojkovic 0:ba7e439238ab 39 return socket->close();
pavleradojkovic 0:ba7e439238ab 40 }
pavleradojkovic 0:ba7e439238ab 41
pavleradojkovic 0:ba7e439238ab 42 MQTTClient::MQTTClient(TCPSocket *_socket)
pavleradojkovic 0:ba7e439238ab 43 {
pavleradojkovic 0:ba7e439238ab 44 init(_socket);
pavleradojkovic 0:ba7e439238ab 45 mqttNet = new MQTTNetworkMbedOs(socket);
pavleradojkovic 0:ba7e439238ab 46 client = new MQTT::Client<MQTTNetworkMbedOs, Countdown, MBED_CONF_MBED_MQTT_MAX_PACKET_SIZE, MBED_CONF_MBED_MQTT_MAX_CONNECTIONS>(*mqttNet);
pavleradojkovic 0:ba7e439238ab 47 };
pavleradojkovic 0:ba7e439238ab 48
pavleradojkovic 0:ba7e439238ab 49 #if defined(MBEDTLS_SSL_CLI_C) || defined(DOXYGEN_ONLY)
pavleradojkovic 0:ba7e439238ab 50 MQTTClient::MQTTClient(TLSSocket *_socket)
pavleradojkovic 0:ba7e439238ab 51 {
pavleradojkovic 0:ba7e439238ab 52 init(_socket);
pavleradojkovic 0:ba7e439238ab 53 mqttNet = new MQTTNetworkMbedOs(socket);
pavleradojkovic 0:ba7e439238ab 54 client = new MQTT::Client<MQTTNetworkMbedOs, Countdown, MBED_CONF_MBED_MQTT_MAX_PACKET_SIZE, MBED_CONF_MBED_MQTT_MAX_CONNECTIONS>(*mqttNet);
pavleradojkovic 0:ba7e439238ab 55 };
pavleradojkovic 0:ba7e439238ab 56 #endif
pavleradojkovic 0:ba7e439238ab 57
pavleradojkovic 0:ba7e439238ab 58 MQTTClient::MQTTClient(UDPSocket *_socket)
pavleradojkovic 0:ba7e439238ab 59 {
pavleradojkovic 0:ba7e439238ab 60 init(_socket);
pavleradojkovic 0:ba7e439238ab 61 mqttNet = new MQTTNetworkMbedOs(socket);
pavleradojkovic 0:ba7e439238ab 62 clientSN = new MQTTSN::Client<MQTTNetworkMbedOs, Countdown, MBED_CONF_MBED_MQTT_MAX_PACKET_SIZE, MBED_CONF_MBED_MQTT_MAX_CONNECTIONS>(*mqttNet);
pavleradojkovic 0:ba7e439238ab 63 };
pavleradojkovic 0:ba7e439238ab 64
pavleradojkovic 0:ba7e439238ab 65 #if defined(MBEDTLS_SSL_CLI_C) || defined(DOXYGEN_ONLY)
pavleradojkovic 0:ba7e439238ab 66 MQTTClient::MQTTClient(DTLSSocket *_socket)
pavleradojkovic 0:ba7e439238ab 67 {
pavleradojkovic 0:ba7e439238ab 68 init(_socket);
pavleradojkovic 0:ba7e439238ab 69 mqttNet = new MQTTNetworkMbedOs(socket);
pavleradojkovic 0:ba7e439238ab 70 clientSN = new MQTTSN::Client<MQTTNetworkMbedOs, Countdown, MBED_CONF_MBED_MQTT_MAX_PACKET_SIZE, MBED_CONF_MBED_MQTT_MAX_CONNECTIONS>(*mqttNet);
pavleradojkovic 0:ba7e439238ab 71 };
pavleradojkovic 0:ba7e439238ab 72 #endif
pavleradojkovic 0:ba7e439238ab 73
pavleradojkovic 0:ba7e439238ab 74 MQTTClient::~MQTTClient()
pavleradojkovic 0:ba7e439238ab 75 {
pavleradojkovic 0:ba7e439238ab 76 delete mqttNet;
pavleradojkovic 0:ba7e439238ab 77 if (client != NULL) delete client;
pavleradojkovic 0:ba7e439238ab 78 if (clientSN != NULL) delete clientSN;
pavleradojkovic 0:ba7e439238ab 79 }
pavleradojkovic 0:ba7e439238ab 80
pavleradojkovic 0:ba7e439238ab 81 nsapi_error_t MQTTClient::connect(MQTTPacket_connectData &options)
pavleradojkovic 0:ba7e439238ab 82 {
pavleradojkovic 0:ba7e439238ab 83 if (client == NULL) {
pavleradojkovic 0:ba7e439238ab 84 return NSAPI_ERROR_NO_CONNECTION;
pavleradojkovic 0:ba7e439238ab 85 }
pavleradojkovic 0:ba7e439238ab 86 nsapi_error_t ret = client->connect(options);
pavleradojkovic 0:ba7e439238ab 87 return ret < 0 ? NSAPI_ERROR_NO_CONNECTION : ret;
pavleradojkovic 0:ba7e439238ab 88 }
pavleradojkovic 0:ba7e439238ab 89
pavleradojkovic 0:ba7e439238ab 90 nsapi_error_t MQTTClient::connect(MQTTSNPacket_connectData &options)
pavleradojkovic 0:ba7e439238ab 91 {
pavleradojkovic 0:ba7e439238ab 92 if (clientSN == NULL) {
pavleradojkovic 0:ba7e439238ab 93 return NSAPI_ERROR_NO_CONNECTION;
pavleradojkovic 0:ba7e439238ab 94 }
pavleradojkovic 0:ba7e439238ab 95 nsapi_error_t ret = clientSN->connect(options);
pavleradojkovic 0:ba7e439238ab 96 return ret < 0 ? NSAPI_ERROR_NO_CONNECTION : ret;
pavleradojkovic 0:ba7e439238ab 97 }
pavleradojkovic 0:ba7e439238ab 98
pavleradojkovic 0:ba7e439238ab 99 nsapi_error_t MQTTClient::publish(const char *topicName, MQTT::Message &message)
pavleradojkovic 0:ba7e439238ab 100 {
pavleradojkovic 0:ba7e439238ab 101 if (client == NULL) {
pavleradojkovic 0:ba7e439238ab 102 return NSAPI_ERROR_NO_CONNECTION;
pavleradojkovic 0:ba7e439238ab 103 }
pavleradojkovic 0:ba7e439238ab 104 nsapi_error_t ret = client->publish(topicName, message);
pavleradojkovic 0:ba7e439238ab 105 return ret < 0 ? NSAPI_ERROR_NO_CONNECTION : ret;
pavleradojkovic 0:ba7e439238ab 106 }
pavleradojkovic 0:ba7e439238ab 107
pavleradojkovic 0:ba7e439238ab 108 nsapi_error_t MQTTClient::publish(MQTTSN_topicid &topicName, MQTTSN::Message &message)
pavleradojkovic 0:ba7e439238ab 109 {
pavleradojkovic 0:ba7e439238ab 110 if (clientSN == NULL) {
pavleradojkovic 0:ba7e439238ab 111 return NSAPI_ERROR_NO_CONNECTION;
pavleradojkovic 0:ba7e439238ab 112 }
pavleradojkovic 0:ba7e439238ab 113 nsapi_error_t ret = clientSN->publish(topicName, message);
pavleradojkovic 0:ba7e439238ab 114 return ret < 0 ? NSAPI_ERROR_NO_CONNECTION : ret;
pavleradojkovic 0:ba7e439238ab 115 }
pavleradojkovic 0:ba7e439238ab 116
pavleradojkovic 0:ba7e439238ab 117 nsapi_error_t MQTTClient::subscribe(const char *topicFilter, enum MQTT::QoS qos, messageHandler mh)
pavleradojkovic 0:ba7e439238ab 118 {
pavleradojkovic 0:ba7e439238ab 119 if (client == NULL) {
pavleradojkovic 0:ba7e439238ab 120 return NSAPI_ERROR_NO_CONNECTION;
pavleradojkovic 0:ba7e439238ab 121 }
pavleradojkovic 0:ba7e439238ab 122 nsapi_error_t ret = client->subscribe(topicFilter, qos, mh);
pavleradojkovic 0:ba7e439238ab 123 return ret < 0 ? NSAPI_ERROR_NO_CONNECTION : ret;
pavleradojkovic 0:ba7e439238ab 124 }
pavleradojkovic 0:ba7e439238ab 125
pavleradojkovic 0:ba7e439238ab 126 nsapi_error_t MQTTClient::subscribe(MQTTSN_topicid &topicFilter, enum MQTTSN::QoS qos, messageHandlerSN mh)
pavleradojkovic 0:ba7e439238ab 127 {
pavleradojkovic 0:ba7e439238ab 128 if (clientSN == NULL) {
pavleradojkovic 0:ba7e439238ab 129 return NSAPI_ERROR_NO_CONNECTION;
pavleradojkovic 0:ba7e439238ab 130 }
pavleradojkovic 0:ba7e439238ab 131 nsapi_error_t ret = clientSN->subscribe(topicFilter, qos, mh);
pavleradojkovic 0:ba7e439238ab 132 return ret < 0 ? NSAPI_ERROR_NO_CONNECTION : ret;
pavleradojkovic 0:ba7e439238ab 133 }
pavleradojkovic 0:ba7e439238ab 134
pavleradojkovic 0:ba7e439238ab 135 nsapi_error_t MQTTClient::unsubscribe(const char *topicFilter)
pavleradojkovic 0:ba7e439238ab 136 {
pavleradojkovic 0:ba7e439238ab 137 if (client == NULL) {
pavleradojkovic 0:ba7e439238ab 138 return NSAPI_ERROR_NO_CONNECTION;
pavleradojkovic 0:ba7e439238ab 139 }
pavleradojkovic 0:ba7e439238ab 140 nsapi_error_t ret = client->unsubscribe(topicFilter);
pavleradojkovic 0:ba7e439238ab 141 return ret < 0 ? NSAPI_ERROR_NO_CONNECTION : ret;
pavleradojkovic 0:ba7e439238ab 142 }
pavleradojkovic 0:ba7e439238ab 143
pavleradojkovic 0:ba7e439238ab 144 nsapi_error_t MQTTClient::unsubscribe(MQTTSN_topicid &topicFilter)
pavleradojkovic 0:ba7e439238ab 145 {
pavleradojkovic 0:ba7e439238ab 146 if (clientSN == NULL) {
pavleradojkovic 0:ba7e439238ab 147 return NSAPI_ERROR_NO_CONNECTION;
pavleradojkovic 0:ba7e439238ab 148 }
pavleradojkovic 0:ba7e439238ab 149 nsapi_error_t ret = clientSN->unsubscribe(topicFilter);
pavleradojkovic 0:ba7e439238ab 150 return ret < 0 ? NSAPI_ERROR_NO_CONNECTION : ret;
pavleradojkovic 0:ba7e439238ab 151 }
pavleradojkovic 0:ba7e439238ab 152
pavleradojkovic 0:ba7e439238ab 153 nsapi_error_t MQTTClient::yield(unsigned long timeout_ms)
pavleradojkovic 0:ba7e439238ab 154 {
pavleradojkovic 0:ba7e439238ab 155 nsapi_error_t ret = NSAPI_ERROR_OK;
pavleradojkovic 0:ba7e439238ab 156 if (client != NULL) {
pavleradojkovic 0:ba7e439238ab 157 ret = client->yield(timeout_ms);
pavleradojkovic 0:ba7e439238ab 158 } else if (clientSN != NULL) {
pavleradojkovic 0:ba7e439238ab 159 ret = clientSN->yield(timeout_ms);
pavleradojkovic 0:ba7e439238ab 160 } else {
pavleradojkovic 0:ba7e439238ab 161 return NSAPI_ERROR_NO_CONNECTION;
pavleradojkovic 0:ba7e439238ab 162 }
pavleradojkovic 0:ba7e439238ab 163 return ret < 0 ? NSAPI_ERROR_NO_CONNECTION : ret;
pavleradojkovic 0:ba7e439238ab 164 }
pavleradojkovic 0:ba7e439238ab 165
pavleradojkovic 0:ba7e439238ab 166 nsapi_error_t MQTTClient::disconnect()
pavleradojkovic 0:ba7e439238ab 167 {
pavleradojkovic 0:ba7e439238ab 168 nsapi_error_t ret = NSAPI_ERROR_OK;
pavleradojkovic 0:ba7e439238ab 169 if (client != NULL) {
pavleradojkovic 0:ba7e439238ab 170 ret = client->disconnect();
pavleradojkovic 0:ba7e439238ab 171 } else if (clientSN != NULL) {
pavleradojkovic 0:ba7e439238ab 172 ret = clientSN->disconnect(0);
pavleradojkovic 0:ba7e439238ab 173 } else {
pavleradojkovic 0:ba7e439238ab 174 return NSAPI_ERROR_NO_CONNECTION;
pavleradojkovic 0:ba7e439238ab 175 }
pavleradojkovic 0:ba7e439238ab 176 return ret < 0 ? NSAPI_ERROR_NO_CONNECTION : ret;
pavleradojkovic 0:ba7e439238ab 177 }
pavleradojkovic 0:ba7e439238ab 178
pavleradojkovic 0:ba7e439238ab 179 bool MQTTClient::isConnected()
pavleradojkovic 0:ba7e439238ab 180 {
pavleradojkovic 0:ba7e439238ab 181 if ((client == NULL && clientSN == NULL) ||
pavleradojkovic 0:ba7e439238ab 182 (client != NULL && clientSN != NULL)){
pavleradojkovic 0:ba7e439238ab 183 return false;
pavleradojkovic 0:ba7e439238ab 184 } else if( client != NULL) {
pavleradojkovic 0:ba7e439238ab 185 return client->isConnected();
pavleradojkovic 0:ba7e439238ab 186 } else {
pavleradojkovic 0:ba7e439238ab 187 return clientSN->isConnected();
pavleradojkovic 0:ba7e439238ab 188 }
pavleradojkovic 0:ba7e439238ab 189 }
pavleradojkovic 0:ba7e439238ab 190
pavleradojkovic 0:ba7e439238ab 191 void MQTTClient::setDefaultMessageHandler(messageHandler mh)
pavleradojkovic 0:ba7e439238ab 192 {
pavleradojkovic 0:ba7e439238ab 193 if (client != NULL) {
pavleradojkovic 0:ba7e439238ab 194 client->setDefaultMessageHandler(mh);
pavleradojkovic 0:ba7e439238ab 195 } else if (clientSN != NULL) {
pavleradojkovic 0:ba7e439238ab 196 client->setDefaultMessageHandler(mh);
pavleradojkovic 0:ba7e439238ab 197 }
pavleradojkovic 0:ba7e439238ab 198 }
pavleradojkovic 0:ba7e439238ab 199
pavleradojkovic 0:ba7e439238ab 200 nsapi_error_t MQTTClient::setMessageHandler(const char *topicFilter, messageHandler mh)
pavleradojkovic 0:ba7e439238ab 201 {
pavleradojkovic 0:ba7e439238ab 202 if (clientSN != NULL) {
pavleradojkovic 0:ba7e439238ab 203 return NSAPI_ERROR_UNSUPPORTED;
pavleradojkovic 0:ba7e439238ab 204 } else if (client == NULL) {
pavleradojkovic 0:ba7e439238ab 205 return NSAPI_ERROR_NO_CONNECTION;
pavleradojkovic 0:ba7e439238ab 206 } else {
pavleradojkovic 0:ba7e439238ab 207 return client->setMessageHandler(topicFilter, mh);
pavleradojkovic 0:ba7e439238ab 208 }
pavleradojkovic 0:ba7e439238ab 209 }
pavleradojkovic 0:ba7e439238ab 210
pavleradojkovic 0:ba7e439238ab 211 void MQTTClient::init(Socket *sock)
pavleradojkovic 0:ba7e439238ab 212 {
pavleradojkovic 0:ba7e439238ab 213 socket = sock;
pavleradojkovic 0:ba7e439238ab 214 client = NULL;
pavleradojkovic 0:ba7e439238ab 215 clientSN = NULL;
pavleradojkovic 0:ba7e439238ab 216 }