Jack Hansdampf / mbed-mqtt-GSOE1

Dependents:   ESP8266MQTT

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers MQTTClientMbedOs.cpp Source File

MQTTClientMbedOs.cpp

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 #include "MQTTClientMbedOs.h"
00019 
00020 int MQTTNetworkMbedOs::read(unsigned char *buffer, int len, int timeout)
00021 {
00022     nsapi_size_or_error_t rc = 0;
00023     socket->set_timeout(timeout);
00024     rc = socket->recv(buffer, len);
00025     if (rc == NSAPI_ERROR_WOULD_BLOCK) {
00026         // time out and no data
00027         // MQTTClient.readPacket() requires 0 on time out and no data.
00028         return 0;
00029     }
00030     if (rc == 0) {
00031         // A receive size of 0 indicates that the socket
00032         // was successfully closed so indicate this to MQTTClient
00033         return -1;
00034     }
00035     return rc;
00036 }
00037 
00038 int MQTTNetworkMbedOs::write(unsigned char *buffer, int len, int timeout)
00039 {
00040     nsapi_size_or_error_t rc = 0;
00041     socket->set_timeout(timeout);
00042     rc = socket->send(buffer, len);
00043     if (rc == NSAPI_ERROR_WOULD_BLOCK) {
00044         // time out and no data
00045         // MQTTClient.writePacket() requires 0 on time out and no data.
00046         return 0;
00047     }
00048     if (rc == 0) {
00049         // The socket is closed so indicate this to MQTTClient
00050         return -1;
00051     }
00052     return rc;
00053 }
00054 
00055 int MQTTNetworkMbedOs::connect(const char *hostname, int port)
00056 {
00057     SocketAddress sockAddr(hostname, port);
00058     return socket->connect(sockAddr);
00059 }
00060 
00061 int MQTTNetworkMbedOs::disconnect()
00062 {
00063     return socket->close();
00064 }
00065 
00066 MQTTClient::MQTTClient(TCPSocket *_socket)
00067 {
00068     init(_socket);
00069     mqttNet = new MQTTNetworkMbedOs(socket);
00070     client = new MQTT::Client<MQTTNetworkMbedOs, Countdown, MBED_CONF_MBED_MQTT_MAX_PACKET_SIZE, MBED_CONF_MBED_MQTT_MAX_CONNECTIONS> (*mqttNet);
00071 };
00072 
00073 #if defined(MBEDTLS_SSL_CLI_C) || defined(DOXYGEN_ONLY)
00074 MQTTClient::MQTTClient(TLSSocket *_socket)
00075 {
00076     init(_socket);
00077     mqttNet = new MQTTNetworkMbedOs(socket);
00078     client = new MQTT::Client<MQTTNetworkMbedOs, Countdown, MBED_CONF_MBED_MQTT_MAX_PACKET_SIZE, MBED_CONF_MBED_MQTT_MAX_CONNECTIONS> (*mqttNet);
00079 };
00080 #endif
00081 
00082 MQTTClient::MQTTClient(UDPSocket *_socket)
00083 {
00084     init(_socket);
00085     mqttNet = new MQTTNetworkMbedOs(socket);
00086     clientSN = new MQTTSN::Client<MQTTNetworkMbedOs, Countdown, MBED_CONF_MBED_MQTT_MAX_PACKET_SIZE, MBED_CONF_MBED_MQTT_MAX_CONNECTIONS>(*mqttNet);
00087 };
00088 
00089 #if defined(MBEDTLS_SSL_CLI_C) || defined(DOXYGEN_ONLY)
00090 MQTTClient::MQTTClient(DTLSSocket *_socket)
00091 {
00092     init(_socket);
00093     mqttNet = new MQTTNetworkMbedOs(socket);
00094     clientSN = new MQTTSN::Client<MQTTNetworkMbedOs, Countdown, MBED_CONF_MBED_MQTT_MAX_PACKET_SIZE, MBED_CONF_MBED_MQTT_MAX_CONNECTIONS>(*mqttNet);
00095 };
00096 #endif
00097 
00098 MQTTClient::~MQTTClient()
00099 {
00100     delete mqttNet;
00101     if (client != NULL) delete client;
00102     if (clientSN != NULL) delete clientSN;
00103 }
00104 
00105 nsapi_error_t MQTTClient::connect(MQTTPacket_connectData &options)
00106 {
00107     if (client == NULL) {
00108         return NSAPI_ERROR_NO_CONNECTION;
00109     }
00110     nsapi_error_t ret = client->connect(options);
00111     return ret < 0 ? NSAPI_ERROR_NO_CONNECTION : ret;
00112 }
00113 
00114 nsapi_error_t MQTTClient::connect(MQTTSNPacket_connectData &options)
00115 {
00116     if (clientSN == NULL) {
00117         return NSAPI_ERROR_NO_CONNECTION;
00118     }
00119     nsapi_error_t ret = clientSN->connect(options);
00120     return ret < 0 ? NSAPI_ERROR_NO_CONNECTION : ret;
00121 }
00122 
00123 nsapi_error_t MQTTClient::publish(const char *topicName, MQTT::Message &message)
00124 {
00125     if (client == NULL) {
00126         return NSAPI_ERROR_NO_CONNECTION;
00127     }
00128     nsapi_error_t ret = client->publish(topicName, message);
00129     return ret < 0 ? NSAPI_ERROR_NO_CONNECTION : ret;
00130 }
00131 
00132 nsapi_error_t MQTTClient::publish(MQTTSN_topicid &topicName, MQTTSN::Message &message)
00133 {
00134     if (clientSN == NULL) {
00135         return NSAPI_ERROR_NO_CONNECTION;
00136     }
00137     nsapi_error_t ret = clientSN->publish(topicName, message);
00138     return ret < 0 ? NSAPI_ERROR_NO_CONNECTION : ret;
00139 }
00140 
00141 nsapi_error_t MQTTClient::subscribe(const char *topicFilter, enum MQTT::QoS qos, messageHandler mh)
00142 {
00143     if (client == NULL) {
00144         return NSAPI_ERROR_NO_CONNECTION;
00145     }
00146     nsapi_error_t ret = client->subscribe(topicFilter, qos, mh);
00147     return ret < 0 ? NSAPI_ERROR_NO_CONNECTION : ret;
00148 }
00149 
00150 nsapi_error_t MQTTClient::subscribe(MQTTSN_topicid &topicFilter, enum MQTTSN::QoS qos, messageHandlerSN mh)
00151 {
00152     if (clientSN == NULL) {
00153         return NSAPI_ERROR_NO_CONNECTION;
00154     }
00155     nsapi_error_t ret = clientSN->subscribe(topicFilter, qos, mh);
00156     return ret < 0 ? NSAPI_ERROR_NO_CONNECTION : ret;
00157 }
00158 
00159 nsapi_error_t MQTTClient::unsubscribe(const char *topicFilter)
00160 {
00161     if (client == NULL) {
00162         return NSAPI_ERROR_NO_CONNECTION;
00163     }
00164     nsapi_error_t ret = client->unsubscribe(topicFilter);
00165     return ret < 0 ? NSAPI_ERROR_NO_CONNECTION : ret;
00166 }
00167 
00168 nsapi_error_t MQTTClient::unsubscribe(MQTTSN_topicid &topicFilter)
00169 {
00170     if (clientSN == NULL) {
00171         return NSAPI_ERROR_NO_CONNECTION;
00172     }
00173     nsapi_error_t ret = clientSN->unsubscribe(topicFilter);
00174     return ret < 0 ? NSAPI_ERROR_NO_CONNECTION : ret;
00175 }
00176 
00177 nsapi_error_t MQTTClient::yield(unsigned long timeout_ms)
00178 {
00179     nsapi_error_t ret = NSAPI_ERROR_OK;
00180     if (client != NULL) {
00181         ret = client->yield(timeout_ms);
00182     } else if (clientSN != NULL) {
00183         ret = clientSN->yield(timeout_ms);
00184     } else {
00185         return NSAPI_ERROR_NO_CONNECTION;
00186     }
00187     return ret < 0 ? NSAPI_ERROR_NO_CONNECTION : ret;
00188 }
00189 
00190 nsapi_error_t MQTTClient::disconnect()
00191 {
00192     nsapi_error_t ret = NSAPI_ERROR_OK;
00193     if (client != NULL) {
00194         ret = client->disconnect();
00195     } else if (clientSN != NULL) {
00196         ret = clientSN->disconnect(0);
00197     } else {
00198         return NSAPI_ERROR_NO_CONNECTION;
00199     }
00200     return ret < 0 ? NSAPI_ERROR_NO_CONNECTION : ret;
00201 }
00202 
00203 bool MQTTClient::isConnected()
00204 {
00205     if ((client == NULL && clientSN == NULL) || 
00206             (client != NULL && clientSN != NULL)){
00207         return false;
00208     } else if( client != NULL) {
00209         return client->isConnected();
00210     } else {
00211         return clientSN->isConnected();
00212     }
00213 }
00214 
00215 void MQTTClient::setDefaultMessageHandler(messageHandler mh)
00216 {
00217     if (client != NULL) {
00218         client->setDefaultMessageHandler(mh);
00219     } else if (clientSN != NULL) {
00220         client->setDefaultMessageHandler(mh);
00221     }
00222 }
00223 
00224 nsapi_error_t MQTTClient::setMessageHandler(const char *topicFilter, messageHandler mh)
00225 {
00226     if (clientSN != NULL) {
00227         return NSAPI_ERROR_UNSUPPORTED;
00228     } else if (client == NULL) {
00229         return NSAPI_ERROR_NO_CONNECTION;
00230     } else {
00231         return client->setMessageHandler(topicFilter, mh);
00232     }
00233 }
00234 
00235 void MQTTClient::init(Socket *sock)
00236 {
00237     socket = sock;
00238     client = NULL;
00239     clientSN = NULL;
00240 }