iot_water_monitor_v2

Dependencies:   easy-connect-v16 Watchdog FP MQTTPacket RecordType-v-16 watersenor_and_temp_code

Committer:
DuyLionTran
Date:
Tue Apr 03 17:03:01 2018 +0000
Revision:
57:898fcb6692cd
Parent:
11:3802c82a5ae9
;   * version 2.9.8  	03-04-2018  Minor changes. Time frame updated to IBM Watson every 60s

Who changed what in which revision?

UserRevisionLine numberNew contents of line
DuyLionTran 11:3802c82a5ae9 1 /*******************************************************************************
DuyLionTran 11:3802c82a5ae9 2 * Copyright (c) 2014, 2017 IBM Corp.
DuyLionTran 11:3802c82a5ae9 3 *
DuyLionTran 11:3802c82a5ae9 4 * All rights reserved. This program and the accompanying materials
DuyLionTran 11:3802c82a5ae9 5 * are made available under the terms of the Eclipse Public License v1.0
DuyLionTran 11:3802c82a5ae9 6 * and Eclipse Distribution License v1.0 which accompany this distribution.
DuyLionTran 11:3802c82a5ae9 7 *
DuyLionTran 11:3802c82a5ae9 8 * The Eclipse Public License is available at
DuyLionTran 11:3802c82a5ae9 9 * http://www.eclipse.org/legal/epl-v10.html
DuyLionTran 11:3802c82a5ae9 10 * and the Eclipse Distribution License is available at
DuyLionTran 11:3802c82a5ae9 11 * http://www.eclipse.org/org/documents/edl-v10.php.
DuyLionTran 11:3802c82a5ae9 12 *
DuyLionTran 11:3802c82a5ae9 13 * Contributors:
DuyLionTran 11:3802c82a5ae9 14 * Ian Craggs - initial API and implementation and/or initial documentation
DuyLionTran 11:3802c82a5ae9 15 * Ian Craggs - fix for bug 458512 - QoS 2 messages
DuyLionTran 11:3802c82a5ae9 16 * Ian Craggs - fix for bug 460389 - send loop uses wrong length
DuyLionTran 11:3802c82a5ae9 17 * Ian Craggs - fix for bug 464169 - clearing subscriptions
DuyLionTran 11:3802c82a5ae9 18 * Ian Craggs - fix for bug 464551 - enums and ints can be different size
DuyLionTran 11:3802c82a5ae9 19 * Mark Sonnentag - fix for bug 475204 - inefficient instantiation of Timer
DuyLionTran 11:3802c82a5ae9 20 * Ian Craggs - fix for bug 475749 - packetid modified twice
DuyLionTran 11:3802c82a5ae9 21 * Ian Craggs - add ability to set message handler separately #6
DuyLionTran 11:3802c82a5ae9 22 *******************************************************************************/
DuyLionTran 11:3802c82a5ae9 23
DuyLionTran 11:3802c82a5ae9 24 #if !defined(MQTTCLIENT_H)
DuyLionTran 11:3802c82a5ae9 25 #define MQTTCLIENT_H
DuyLionTran 11:3802c82a5ae9 26
DuyLionTran 11:3802c82a5ae9 27 #include "FP.h"
DuyLionTran 11:3802c82a5ae9 28 #include "MQTTPacket.h"
DuyLionTran 11:3802c82a5ae9 29 #include <stdio.h>
DuyLionTran 11:3802c82a5ae9 30 #include "MQTTLogging.h"
DuyLionTran 11:3802c82a5ae9 31
DuyLionTran 11:3802c82a5ae9 32 #if !defined(MQTTCLIENT_QOS1)
DuyLionTran 11:3802c82a5ae9 33 #define MQTTCLIENT_QOS1 1
DuyLionTran 11:3802c82a5ae9 34 #endif
DuyLionTran 11:3802c82a5ae9 35 #if !defined(MQTTCLIENT_QOS2)
DuyLionTran 11:3802c82a5ae9 36 #define MQTTCLIENT_QOS2 0
DuyLionTran 11:3802c82a5ae9 37 #endif
DuyLionTran 11:3802c82a5ae9 38
DuyLionTran 11:3802c82a5ae9 39 namespace MQTT
DuyLionTran 11:3802c82a5ae9 40 {
DuyLionTran 11:3802c82a5ae9 41
DuyLionTran 11:3802c82a5ae9 42
DuyLionTran 11:3802c82a5ae9 43 enum QoS { QOS0, QOS1, QOS2 };
DuyLionTran 11:3802c82a5ae9 44
DuyLionTran 11:3802c82a5ae9 45 // all failure return codes must be negative
DuyLionTran 11:3802c82a5ae9 46 enum returnCode { BUFFER_OVERFLOW = -2, FAILURE = -1, SUCCESS = 0 };
DuyLionTran 11:3802c82a5ae9 47
DuyLionTran 11:3802c82a5ae9 48
DuyLionTran 11:3802c82a5ae9 49 struct Message
DuyLionTran 11:3802c82a5ae9 50 {
DuyLionTran 11:3802c82a5ae9 51 enum QoS qos;
DuyLionTran 11:3802c82a5ae9 52 bool retained;
DuyLionTran 11:3802c82a5ae9 53 bool dup;
DuyLionTran 11:3802c82a5ae9 54 unsigned short id;
DuyLionTran 11:3802c82a5ae9 55 void *payload;
DuyLionTran 11:3802c82a5ae9 56 size_t payloadlen;
DuyLionTran 11:3802c82a5ae9 57 };
DuyLionTran 11:3802c82a5ae9 58
DuyLionTran 11:3802c82a5ae9 59
DuyLionTran 11:3802c82a5ae9 60 struct MessageData
DuyLionTran 11:3802c82a5ae9 61 {
DuyLionTran 11:3802c82a5ae9 62 MessageData(MQTTString &aTopicName, struct Message &aMessage) : message(aMessage), topicName(aTopicName)
DuyLionTran 11:3802c82a5ae9 63 { }
DuyLionTran 11:3802c82a5ae9 64
DuyLionTran 11:3802c82a5ae9 65 struct Message &message;
DuyLionTran 11:3802c82a5ae9 66 MQTTString &topicName;
DuyLionTran 11:3802c82a5ae9 67 };
DuyLionTran 11:3802c82a5ae9 68
DuyLionTran 11:3802c82a5ae9 69
DuyLionTran 11:3802c82a5ae9 70 struct connackData
DuyLionTran 11:3802c82a5ae9 71 {
DuyLionTran 11:3802c82a5ae9 72 int rc;
DuyLionTran 11:3802c82a5ae9 73 bool sessionPresent;
DuyLionTran 11:3802c82a5ae9 74 };
DuyLionTran 11:3802c82a5ae9 75
DuyLionTran 11:3802c82a5ae9 76
DuyLionTran 11:3802c82a5ae9 77 struct subackData
DuyLionTran 11:3802c82a5ae9 78 {
DuyLionTran 11:3802c82a5ae9 79 int grantedQoS;
DuyLionTran 11:3802c82a5ae9 80 };
DuyLionTran 11:3802c82a5ae9 81
DuyLionTran 11:3802c82a5ae9 82
DuyLionTran 11:3802c82a5ae9 83 class PacketId
DuyLionTran 11:3802c82a5ae9 84 {
DuyLionTran 11:3802c82a5ae9 85 public:
DuyLionTran 11:3802c82a5ae9 86 PacketId()
DuyLionTran 11:3802c82a5ae9 87 {
DuyLionTran 11:3802c82a5ae9 88 next = 0;
DuyLionTran 11:3802c82a5ae9 89 }
DuyLionTran 11:3802c82a5ae9 90
DuyLionTran 11:3802c82a5ae9 91 int getNext()
DuyLionTran 11:3802c82a5ae9 92 {
DuyLionTran 11:3802c82a5ae9 93 return next = (next == MAX_PACKET_ID) ? 1 : next + 1;
DuyLionTran 11:3802c82a5ae9 94 }
DuyLionTran 11:3802c82a5ae9 95
DuyLionTran 11:3802c82a5ae9 96 private:
DuyLionTran 11:3802c82a5ae9 97 static const int MAX_PACKET_ID = 65535;
DuyLionTran 11:3802c82a5ae9 98 int next;
DuyLionTran 11:3802c82a5ae9 99 };
DuyLionTran 11:3802c82a5ae9 100
DuyLionTran 11:3802c82a5ae9 101
DuyLionTran 11:3802c82a5ae9 102 /**
DuyLionTran 11:3802c82a5ae9 103 * @class Client
DuyLionTran 11:3802c82a5ae9 104 * @brief blocking, non-threaded MQTT client API
DuyLionTran 11:3802c82a5ae9 105 *
DuyLionTran 11:3802c82a5ae9 106 * This version of the API blocks on all method calls, until they are complete. This means that only one
DuyLionTran 11:3802c82a5ae9 107 * MQTT request can be in process at any one time.
DuyLionTran 11:3802c82a5ae9 108 * @param Network a network class which supports send, receive
DuyLionTran 11:3802c82a5ae9 109 * @param Timer a timer class with the methods:
DuyLionTran 11:3802c82a5ae9 110 */
DuyLionTran 11:3802c82a5ae9 111 template<class Network, class Timer, int MAX_MQTT_PACKET_SIZE = 100, int MAX_MESSAGE_HANDLERS = 5>
DuyLionTran 11:3802c82a5ae9 112 class Client
DuyLionTran 11:3802c82a5ae9 113 {
DuyLionTran 11:3802c82a5ae9 114
DuyLionTran 11:3802c82a5ae9 115 public:
DuyLionTran 11:3802c82a5ae9 116
DuyLionTran 11:3802c82a5ae9 117 typedef void (*messageHandler)(MessageData&);
DuyLionTran 11:3802c82a5ae9 118
DuyLionTran 11:3802c82a5ae9 119 /** Construct the client
DuyLionTran 11:3802c82a5ae9 120 * @param network - pointer to an instance of the Network class - must be connected to the endpoint
DuyLionTran 11:3802c82a5ae9 121 * before calling MQTT connect
DuyLionTran 11:3802c82a5ae9 122 * @param limits an instance of the Limit class - to alter limits as required
DuyLionTran 11:3802c82a5ae9 123 */
DuyLionTran 11:3802c82a5ae9 124 Client(Network& network, unsigned int command_timeout_ms = 30000);
DuyLionTran 11:3802c82a5ae9 125
DuyLionTran 11:3802c82a5ae9 126 /** Set the default message handling callback - used for any message which does not match a subscription message handler
DuyLionTran 11:3802c82a5ae9 127 * @param mh - pointer to the callback function. Set to 0 to remove.
DuyLionTran 11:3802c82a5ae9 128 */
DuyLionTran 11:3802c82a5ae9 129 void setDefaultMessageHandler(messageHandler mh)
DuyLionTran 11:3802c82a5ae9 130 {
DuyLionTran 11:3802c82a5ae9 131 if (mh != 0)
DuyLionTran 11:3802c82a5ae9 132 defaultMessageHandler.attach(mh);
DuyLionTran 11:3802c82a5ae9 133 else
DuyLionTran 11:3802c82a5ae9 134 defaultMessageHandler.detach();
DuyLionTran 11:3802c82a5ae9 135 }
DuyLionTran 11:3802c82a5ae9 136
DuyLionTran 11:3802c82a5ae9 137 /** Set a message handling callback. This can be used outside of the the subscribe method.
DuyLionTran 11:3802c82a5ae9 138 * @param topicFilter - a topic pattern which can include wildcards
DuyLionTran 11:3802c82a5ae9 139 * @param mh - pointer to the callback function. If 0, removes the callback if any
DuyLionTran 11:3802c82a5ae9 140 */
DuyLionTran 11:3802c82a5ae9 141 int setMessageHandler(const char* topicFilter, messageHandler mh);
DuyLionTran 11:3802c82a5ae9 142
DuyLionTran 11:3802c82a5ae9 143 /** MQTT Connect - send an MQTT connect packet down the network and wait for a Connack
DuyLionTran 11:3802c82a5ae9 144 * The nework object must be connected to the network endpoint before calling this
DuyLionTran 11:3802c82a5ae9 145 * Default connect options are used
DuyLionTran 11:3802c82a5ae9 146 * @return success code -
DuyLionTran 11:3802c82a5ae9 147 */
DuyLionTran 11:3802c82a5ae9 148 int connect();
DuyLionTran 11:3802c82a5ae9 149
DuyLionTran 11:3802c82a5ae9 150 /** MQTT Connect - send an MQTT connect packet down the network and wait for a Connack
DuyLionTran 11:3802c82a5ae9 151 * The nework object must be connected to the network endpoint before calling this
DuyLionTran 11:3802c82a5ae9 152 * @param options - connect options
DuyLionTran 11:3802c82a5ae9 153 * @return success code -
DuyLionTran 11:3802c82a5ae9 154 */
DuyLionTran 11:3802c82a5ae9 155 int connect(MQTTPacket_connectData& options);
DuyLionTran 11:3802c82a5ae9 156
DuyLionTran 11:3802c82a5ae9 157 /** MQTT Connect - send an MQTT connect packet down the network and wait for a Connack
DuyLionTran 11:3802c82a5ae9 158 * The nework object must be connected to the network endpoint before calling this
DuyLionTran 11:3802c82a5ae9 159 * @param options - connect options
DuyLionTran 11:3802c82a5ae9 160 * @param connackData - connack data to be returned
DuyLionTran 11:3802c82a5ae9 161 * @return success code -
DuyLionTran 11:3802c82a5ae9 162 */
DuyLionTran 11:3802c82a5ae9 163 int connect(MQTTPacket_connectData& options, connackData& data);
DuyLionTran 11:3802c82a5ae9 164
DuyLionTran 11:3802c82a5ae9 165 /** MQTT Publish - send an MQTT publish packet and wait for all acks to complete for all QoSs
DuyLionTran 11:3802c82a5ae9 166 * @param topic - the topic to publish to
DuyLionTran 11:3802c82a5ae9 167 * @param message - the message to send
DuyLionTran 11:3802c82a5ae9 168 * @return success code -
DuyLionTran 11:3802c82a5ae9 169 */
DuyLionTran 11:3802c82a5ae9 170 int publish(const char* topicName, Message& message);
DuyLionTran 11:3802c82a5ae9 171
DuyLionTran 11:3802c82a5ae9 172 /** MQTT Publish - send an MQTT publish packet and wait for all acks to complete for all QoSs
DuyLionTran 11:3802c82a5ae9 173 * @param topic - the topic to publish to
DuyLionTran 11:3802c82a5ae9 174 * @param payload - the data to send
DuyLionTran 11:3802c82a5ae9 175 * @param payloadlen - the length of the data
DuyLionTran 11:3802c82a5ae9 176 * @param qos - the QoS to send the publish at
DuyLionTran 11:3802c82a5ae9 177 * @param retained - whether the message should be retained
DuyLionTran 11:3802c82a5ae9 178 * @return success code -
DuyLionTran 11:3802c82a5ae9 179 */
DuyLionTran 11:3802c82a5ae9 180 int publish(const char* topicName, void* payload, size_t payloadlen, enum QoS qos = QOS0, bool retained = false);
DuyLionTran 11:3802c82a5ae9 181
DuyLionTran 11:3802c82a5ae9 182 /** MQTT Publish - send an MQTT publish packet and wait for all acks to complete for all QoSs
DuyLionTran 11:3802c82a5ae9 183 * @param topic - the topic to publish to
DuyLionTran 11:3802c82a5ae9 184 * @param payload - the data to send
DuyLionTran 11:3802c82a5ae9 185 * @param payloadlen - the length of the data
DuyLionTran 11:3802c82a5ae9 186 * @param id - the packet id used - returned
DuyLionTran 11:3802c82a5ae9 187 * @param qos - the QoS to send the publish at
DuyLionTran 11:3802c82a5ae9 188 * @param retained - whether the message should be retained
DuyLionTran 11:3802c82a5ae9 189 * @return success code -
DuyLionTran 11:3802c82a5ae9 190 */
DuyLionTran 11:3802c82a5ae9 191 int publish(const char* topicName, void* payload, size_t payloadlen, unsigned short& id, enum QoS qos = QOS1, bool retained = false);
DuyLionTran 11:3802c82a5ae9 192
DuyLionTran 11:3802c82a5ae9 193 /** MQTT Subscribe - send an MQTT subscribe packet and wait for the suback
DuyLionTran 11:3802c82a5ae9 194 * @param topicFilter - a topic pattern which can include wildcards
DuyLionTran 11:3802c82a5ae9 195 * @param qos - the MQTT QoS to subscribe at
DuyLionTran 11:3802c82a5ae9 196 * @param mh - the callback function to be invoked when a message is received for this subscription
DuyLionTran 11:3802c82a5ae9 197 * @return success code -
DuyLionTran 11:3802c82a5ae9 198 */
DuyLionTran 11:3802c82a5ae9 199 int subscribe(const char* topicFilter, enum QoS qos, messageHandler mh);
DuyLionTran 11:3802c82a5ae9 200
DuyLionTran 11:3802c82a5ae9 201 /** MQTT Subscribe - send an MQTT subscribe packet and wait for the suback
DuyLionTran 11:3802c82a5ae9 202 * @param topicFilter - a topic pattern which can include wildcards
DuyLionTran 11:3802c82a5ae9 203 * @param qos - the MQTT QoS to subscribe at©
DuyLionTran 11:3802c82a5ae9 204 * @param mh - the callback function to be invoked when a message is received for this subscription
DuyLionTran 11:3802c82a5ae9 205 * @param
DuyLionTran 11:3802c82a5ae9 206 * @return success code -
DuyLionTran 11:3802c82a5ae9 207 */
DuyLionTran 11:3802c82a5ae9 208 int subscribe(const char* topicFilter, enum QoS qos, messageHandler mh, subackData &data);
DuyLionTran 11:3802c82a5ae9 209
DuyLionTran 11:3802c82a5ae9 210 /** MQTT Unsubscribe - send an MQTT unsubscribe packet and wait for the unsuback
DuyLionTran 11:3802c82a5ae9 211 * @param topicFilter - a topic pattern which can include wildcards
DuyLionTran 11:3802c82a5ae9 212 * @return success code -
DuyLionTran 11:3802c82a5ae9 213 */
DuyLionTran 11:3802c82a5ae9 214 int unsubscribe(const char* topicFilter);
DuyLionTran 11:3802c82a5ae9 215
DuyLionTran 11:3802c82a5ae9 216 /** MQTT Disconnect - send an MQTT disconnect packet, and clean up any state
DuyLionTran 11:3802c82a5ae9 217 * @return success code -
DuyLionTran 11:3802c82a5ae9 218 */
DuyLionTran 11:3802c82a5ae9 219 int disconnect();
DuyLionTran 11:3802c82a5ae9 220
DuyLionTran 11:3802c82a5ae9 221 /** A call to this API must be made within the keepAlive interval to keep the MQTT connection alive
DuyLionTran 11:3802c82a5ae9 222 * yield can be called if no other MQTT operation is needed. This will also allow messages to be
DuyLionTran 11:3802c82a5ae9 223 * received.
DuyLionTran 11:3802c82a5ae9 224 * @param timeout_ms the time to wait, in milliseconds
DuyLionTran 11:3802c82a5ae9 225 * @return success code - on failure, this means the client has disconnected
DuyLionTran 11:3802c82a5ae9 226 */
DuyLionTran 11:3802c82a5ae9 227 int yield(unsigned long timeout_ms = 1000L);
DuyLionTran 11:3802c82a5ae9 228
DuyLionTran 11:3802c82a5ae9 229 /** Is the client connected?
DuyLionTran 11:3802c82a5ae9 230 * @return flag - is the client connected or not?
DuyLionTran 11:3802c82a5ae9 231 */
DuyLionTran 11:3802c82a5ae9 232 bool isConnected()
DuyLionTran 11:3802c82a5ae9 233 {
DuyLionTran 11:3802c82a5ae9 234 return isconnected;
DuyLionTran 11:3802c82a5ae9 235 }
DuyLionTran 11:3802c82a5ae9 236
DuyLionTran 11:3802c82a5ae9 237 private:
DuyLionTran 11:3802c82a5ae9 238
DuyLionTran 11:3802c82a5ae9 239 void closeSession();
DuyLionTran 11:3802c82a5ae9 240 void cleanSession();
DuyLionTran 11:3802c82a5ae9 241 int cycle(Timer& timer);
DuyLionTran 11:3802c82a5ae9 242 int waitfor(int packet_type, Timer& timer);
DuyLionTran 11:3802c82a5ae9 243 int keepalive();
DuyLionTran 11:3802c82a5ae9 244 int publish(int len, Timer& timer, enum QoS qos);
DuyLionTran 11:3802c82a5ae9 245
DuyLionTran 11:3802c82a5ae9 246 int decodePacket(int* value, int timeout);
DuyLionTran 11:3802c82a5ae9 247 int readPacket(Timer& timer);
DuyLionTran 11:3802c82a5ae9 248 int sendPacket(int length, Timer& timer);
DuyLionTran 11:3802c82a5ae9 249 int deliverMessage(MQTTString& topicName, Message& message);
DuyLionTran 11:3802c82a5ae9 250 bool isTopicMatched(char* topicFilter, MQTTString& topicName);
DuyLionTran 11:3802c82a5ae9 251
DuyLionTran 11:3802c82a5ae9 252 Network& ipstack;
DuyLionTran 11:3802c82a5ae9 253 unsigned long command_timeout_ms;
DuyLionTran 11:3802c82a5ae9 254
DuyLionTran 11:3802c82a5ae9 255 unsigned char sendbuf[MAX_MQTT_PACKET_SIZE];
DuyLionTran 11:3802c82a5ae9 256 unsigned char readbuf[MAX_MQTT_PACKET_SIZE];
DuyLionTran 11:3802c82a5ae9 257
DuyLionTran 11:3802c82a5ae9 258 Timer last_sent, last_received;
DuyLionTran 11:3802c82a5ae9 259 unsigned int keepAliveInterval;
DuyLionTran 11:3802c82a5ae9 260 bool ping_outstanding;
DuyLionTran 11:3802c82a5ae9 261 bool cleansession;
DuyLionTran 11:3802c82a5ae9 262
DuyLionTran 11:3802c82a5ae9 263 PacketId packetid;
DuyLionTran 11:3802c82a5ae9 264
DuyLionTran 11:3802c82a5ae9 265 struct MessageHandlers
DuyLionTran 11:3802c82a5ae9 266 {
DuyLionTran 11:3802c82a5ae9 267 const char* topicFilter;
DuyLionTran 11:3802c82a5ae9 268 FP<void, MessageData&> fp;
DuyLionTran 11:3802c82a5ae9 269 } messageHandlers[MAX_MESSAGE_HANDLERS]; // Message handlers are indexed by subscription topic
DuyLionTran 11:3802c82a5ae9 270
DuyLionTran 11:3802c82a5ae9 271 FP<void, MessageData&> defaultMessageHandler;
DuyLionTran 11:3802c82a5ae9 272
DuyLionTran 11:3802c82a5ae9 273 bool isconnected;
DuyLionTran 11:3802c82a5ae9 274
DuyLionTran 11:3802c82a5ae9 275 #if MQTTCLIENT_QOS1 || MQTTCLIENT_QOS2
DuyLionTran 11:3802c82a5ae9 276 unsigned char pubbuf[MAX_MQTT_PACKET_SIZE]; // store the last publish for sending on reconnect
DuyLionTran 11:3802c82a5ae9 277 int inflightLen;
DuyLionTran 11:3802c82a5ae9 278 unsigned short inflightMsgid;
DuyLionTran 11:3802c82a5ae9 279 enum QoS inflightQoS;
DuyLionTran 11:3802c82a5ae9 280 #endif
DuyLionTran 11:3802c82a5ae9 281
DuyLionTran 11:3802c82a5ae9 282 #if MQTTCLIENT_QOS2
DuyLionTran 11:3802c82a5ae9 283 bool pubrel;
DuyLionTran 11:3802c82a5ae9 284 #if !defined(MAX_INCOMING_QOS2_MESSAGES)
DuyLionTran 11:3802c82a5ae9 285 #define MAX_INCOMING_QOS2_MESSAGES 10
DuyLionTran 11:3802c82a5ae9 286 #endif
DuyLionTran 11:3802c82a5ae9 287 unsigned short incomingQoS2messages[MAX_INCOMING_QOS2_MESSAGES];
DuyLionTran 11:3802c82a5ae9 288 bool isQoS2msgidFree(unsigned short id);
DuyLionTran 11:3802c82a5ae9 289 bool useQoS2msgid(unsigned short id);
DuyLionTran 11:3802c82a5ae9 290 void freeQoS2msgid(unsigned short id);
DuyLionTran 11:3802c82a5ae9 291 #endif
DuyLionTran 11:3802c82a5ae9 292
DuyLionTran 11:3802c82a5ae9 293 };
DuyLionTran 11:3802c82a5ae9 294
DuyLionTran 11:3802c82a5ae9 295 }
DuyLionTran 11:3802c82a5ae9 296
DuyLionTran 11:3802c82a5ae9 297
DuyLionTran 11:3802c82a5ae9 298 template<class Network, class Timer, int a, int MAX_MESSAGE_HANDLERS>
DuyLionTran 11:3802c82a5ae9 299 void MQTT::Client<Network, Timer, a, MAX_MESSAGE_HANDLERS>::cleanSession()
DuyLionTran 11:3802c82a5ae9 300 {
DuyLionTran 11:3802c82a5ae9 301 for (int i = 0; i < MAX_MESSAGE_HANDLERS; ++i)
DuyLionTran 11:3802c82a5ae9 302 messageHandlers[i].topicFilter = 0;
DuyLionTran 11:3802c82a5ae9 303
DuyLionTran 11:3802c82a5ae9 304 #if MQTTCLIENT_QOS1 || MQTTCLIENT_QOS2
DuyLionTran 11:3802c82a5ae9 305 inflightMsgid = 0;
DuyLionTran 11:3802c82a5ae9 306 inflightQoS = QOS0;
DuyLionTran 11:3802c82a5ae9 307 #endif
DuyLionTran 11:3802c82a5ae9 308
DuyLionTran 11:3802c82a5ae9 309 #if MQTTCLIENT_QOS2
DuyLionTran 11:3802c82a5ae9 310 pubrel = false;
DuyLionTran 11:3802c82a5ae9 311 for (int i = 0; i < MAX_INCOMING_QOS2_MESSAGES; ++i)
DuyLionTran 11:3802c82a5ae9 312 incomingQoS2messages[i] = 0;
DuyLionTran 11:3802c82a5ae9 313 #endif
DuyLionTran 11:3802c82a5ae9 314 }
DuyLionTran 11:3802c82a5ae9 315
DuyLionTran 11:3802c82a5ae9 316
DuyLionTran 11:3802c82a5ae9 317 template<class Network, class Timer, int a, int MAX_MESSAGE_HANDLERS>
DuyLionTran 11:3802c82a5ae9 318 void MQTT::Client<Network, Timer, a, MAX_MESSAGE_HANDLERS>::closeSession()
DuyLionTran 11:3802c82a5ae9 319 {
DuyLionTran 11:3802c82a5ae9 320 ping_outstanding = false;
DuyLionTran 11:3802c82a5ae9 321 isconnected = false;
DuyLionTran 11:3802c82a5ae9 322 if (cleansession)
DuyLionTran 11:3802c82a5ae9 323 cleanSession();
DuyLionTran 11:3802c82a5ae9 324 }
DuyLionTran 11:3802c82a5ae9 325
DuyLionTran 11:3802c82a5ae9 326
DuyLionTran 11:3802c82a5ae9 327 template<class Network, class Timer, int a, int MAX_MESSAGE_HANDLERS>
DuyLionTran 11:3802c82a5ae9 328 MQTT::Client<Network, Timer, a, MAX_MESSAGE_HANDLERS>::Client(Network& network, unsigned int command_timeout_ms) : ipstack(network), packetid()
DuyLionTran 11:3802c82a5ae9 329 {
DuyLionTran 11:3802c82a5ae9 330 this->command_timeout_ms = command_timeout_ms;
DuyLionTran 11:3802c82a5ae9 331 cleansession = true;
DuyLionTran 11:3802c82a5ae9 332 closeSession();
DuyLionTran 11:3802c82a5ae9 333 }
DuyLionTran 11:3802c82a5ae9 334
DuyLionTran 11:3802c82a5ae9 335
DuyLionTran 11:3802c82a5ae9 336 #if MQTTCLIENT_QOS2
DuyLionTran 11:3802c82a5ae9 337 template<class Network, class Timer, int a, int b>
DuyLionTran 11:3802c82a5ae9 338 bool MQTT::Client<Network, Timer, a, b>::isQoS2msgidFree(unsigned short id)
DuyLionTran 11:3802c82a5ae9 339 {
DuyLionTran 11:3802c82a5ae9 340 for (int i = 0; i < MAX_INCOMING_QOS2_MESSAGES; ++i)
DuyLionTran 11:3802c82a5ae9 341 {
DuyLionTran 11:3802c82a5ae9 342 if (incomingQoS2messages[i] == id)
DuyLionTran 11:3802c82a5ae9 343 return false;
DuyLionTran 11:3802c82a5ae9 344 }
DuyLionTran 11:3802c82a5ae9 345 return true;
DuyLionTran 11:3802c82a5ae9 346 }
DuyLionTran 11:3802c82a5ae9 347
DuyLionTran 11:3802c82a5ae9 348
DuyLionTran 11:3802c82a5ae9 349 template<class Network, class Timer, int a, int b>
DuyLionTran 11:3802c82a5ae9 350 bool MQTT::Client<Network, Timer, a, b>::useQoS2msgid(unsigned short id)
DuyLionTran 11:3802c82a5ae9 351 {
DuyLionTran 11:3802c82a5ae9 352 for (int i = 0; i < MAX_INCOMING_QOS2_MESSAGES; ++i)
DuyLionTran 11:3802c82a5ae9 353 {
DuyLionTran 11:3802c82a5ae9 354 if (incomingQoS2messages[i] == 0)
DuyLionTran 11:3802c82a5ae9 355 {
DuyLionTran 11:3802c82a5ae9 356 incomingQoS2messages[i] = id;
DuyLionTran 11:3802c82a5ae9 357 return true;
DuyLionTran 11:3802c82a5ae9 358 }
DuyLionTran 11:3802c82a5ae9 359 }
DuyLionTran 11:3802c82a5ae9 360 return false;
DuyLionTran 11:3802c82a5ae9 361 }
DuyLionTran 11:3802c82a5ae9 362
DuyLionTran 11:3802c82a5ae9 363
DuyLionTran 11:3802c82a5ae9 364 template<class Network, class Timer, int a, int b>
DuyLionTran 11:3802c82a5ae9 365 void MQTT::Client<Network, Timer, a, b>::freeQoS2msgid(unsigned short id)
DuyLionTran 11:3802c82a5ae9 366 {
DuyLionTran 11:3802c82a5ae9 367 for (int i = 0; i < MAX_INCOMING_QOS2_MESSAGES; ++i)
DuyLionTran 11:3802c82a5ae9 368 {
DuyLionTran 11:3802c82a5ae9 369 if (incomingQoS2messages[i] == id)
DuyLionTran 11:3802c82a5ae9 370 {
DuyLionTran 11:3802c82a5ae9 371 incomingQoS2messages[i] = 0;
DuyLionTran 11:3802c82a5ae9 372 return;
DuyLionTran 11:3802c82a5ae9 373 }
DuyLionTran 11:3802c82a5ae9 374 }
DuyLionTran 11:3802c82a5ae9 375 }
DuyLionTran 11:3802c82a5ae9 376 #endif
DuyLionTran 11:3802c82a5ae9 377
DuyLionTran 11:3802c82a5ae9 378
DuyLionTran 11:3802c82a5ae9 379 template<class Network, class Timer, int a, int b>
DuyLionTran 11:3802c82a5ae9 380 int MQTT::Client<Network, Timer, a, b>::sendPacket(int length, Timer& timer)
DuyLionTran 11:3802c82a5ae9 381 {
DuyLionTran 11:3802c82a5ae9 382 int rc = FAILURE,
DuyLionTran 11:3802c82a5ae9 383 sent = 0;
DuyLionTran 11:3802c82a5ae9 384
DuyLionTran 11:3802c82a5ae9 385 while (sent < length)
DuyLionTran 11:3802c82a5ae9 386 {
DuyLionTran 11:3802c82a5ae9 387 rc = ipstack.write(&sendbuf[sent], length - sent, timer.left_ms());
DuyLionTran 11:3802c82a5ae9 388 if (rc < 0) // there was an error writing the data
DuyLionTran 11:3802c82a5ae9 389 break;
DuyLionTran 11:3802c82a5ae9 390 sent += rc;
DuyLionTran 11:3802c82a5ae9 391 if (timer.expired()) // only check expiry after at least one attempt to write
DuyLionTran 11:3802c82a5ae9 392 break;
DuyLionTran 11:3802c82a5ae9 393 }
DuyLionTran 11:3802c82a5ae9 394 if (sent == length)
DuyLionTran 11:3802c82a5ae9 395 {
DuyLionTran 11:3802c82a5ae9 396 if (this->keepAliveInterval > 0)
DuyLionTran 11:3802c82a5ae9 397 last_sent.countdown(this->keepAliveInterval); // record the fact that we have successfully sent the packet
DuyLionTran 11:3802c82a5ae9 398 rc = SUCCESS;
DuyLionTran 11:3802c82a5ae9 399 }
DuyLionTran 11:3802c82a5ae9 400 else
DuyLionTran 11:3802c82a5ae9 401 rc = FAILURE;
DuyLionTran 11:3802c82a5ae9 402
DuyLionTran 11:3802c82a5ae9 403 #if defined(MQTT_DEBUG)
DuyLionTran 11:3802c82a5ae9 404 char printbuf[150];
DuyLionTran 11:3802c82a5ae9 405 DEBUG("Rc %d from sending packet %s\r\n", rc,
DuyLionTran 11:3802c82a5ae9 406 MQTTFormat_toServerString(printbuf, sizeof(printbuf), sendbuf, length));
DuyLionTran 11:3802c82a5ae9 407 #endif
DuyLionTran 11:3802c82a5ae9 408 return rc;
DuyLionTran 11:3802c82a5ae9 409 }
DuyLionTran 11:3802c82a5ae9 410
DuyLionTran 11:3802c82a5ae9 411
DuyLionTran 11:3802c82a5ae9 412 template<class Network, class Timer, int a, int b>
DuyLionTran 11:3802c82a5ae9 413 int MQTT::Client<Network, Timer, a, b>::decodePacket(int* value, int timeout)
DuyLionTran 11:3802c82a5ae9 414 {
DuyLionTran 11:3802c82a5ae9 415 unsigned char c;
DuyLionTran 11:3802c82a5ae9 416 int multiplier = 1;
DuyLionTran 11:3802c82a5ae9 417 int len = 0;
DuyLionTran 11:3802c82a5ae9 418 const int MAX_NO_OF_REMAINING_LENGTH_BYTES = 4;
DuyLionTran 11:3802c82a5ae9 419
DuyLionTran 11:3802c82a5ae9 420 *value = 0;
DuyLionTran 11:3802c82a5ae9 421 do
DuyLionTran 11:3802c82a5ae9 422 {
DuyLionTran 11:3802c82a5ae9 423 int rc = MQTTPACKET_READ_ERROR;
DuyLionTran 11:3802c82a5ae9 424
DuyLionTran 11:3802c82a5ae9 425 if (++len > MAX_NO_OF_REMAINING_LENGTH_BYTES)
DuyLionTran 11:3802c82a5ae9 426 {
DuyLionTran 11:3802c82a5ae9 427 rc = MQTTPACKET_READ_ERROR; /* bad data */
DuyLionTran 11:3802c82a5ae9 428 goto exit;
DuyLionTran 11:3802c82a5ae9 429 }
DuyLionTran 11:3802c82a5ae9 430 rc = ipstack.read(&c, 1, timeout);
DuyLionTran 11:3802c82a5ae9 431 if (rc != 1)
DuyLionTran 11:3802c82a5ae9 432 goto exit;
DuyLionTran 11:3802c82a5ae9 433 *value += (c & 127) * multiplier;
DuyLionTran 11:3802c82a5ae9 434 multiplier *= 128;
DuyLionTran 11:3802c82a5ae9 435 } while ((c & 128) != 0);
DuyLionTran 11:3802c82a5ae9 436 exit:
DuyLionTran 11:3802c82a5ae9 437 return len;
DuyLionTran 11:3802c82a5ae9 438 }
DuyLionTran 11:3802c82a5ae9 439
DuyLionTran 11:3802c82a5ae9 440
DuyLionTran 11:3802c82a5ae9 441 /**
DuyLionTran 11:3802c82a5ae9 442 * If any read fails in this method, then we should disconnect from the network, as on reconnect
DuyLionTran 11:3802c82a5ae9 443 * the packets can be retried.
DuyLionTran 11:3802c82a5ae9 444 * @param timeout the max time to wait for the packet read to complete, in milliseconds
DuyLionTran 11:3802c82a5ae9 445 * @return the MQTT packet type, 0 if none, -1 if error
DuyLionTran 11:3802c82a5ae9 446 */
DuyLionTran 11:3802c82a5ae9 447 template<class Network, class Timer, int MAX_MQTT_PACKET_SIZE, int b>
DuyLionTran 11:3802c82a5ae9 448 int MQTT::Client<Network, Timer, MAX_MQTT_PACKET_SIZE, b>::readPacket(Timer& timer)
DuyLionTran 11:3802c82a5ae9 449 {
DuyLionTran 11:3802c82a5ae9 450 int rc = FAILURE;
DuyLionTran 11:3802c82a5ae9 451 MQTTHeader header = {0};
DuyLionTran 11:3802c82a5ae9 452 int len = 0;
DuyLionTran 11:3802c82a5ae9 453 int rem_len = 0;
DuyLionTran 11:3802c82a5ae9 454
DuyLionTran 11:3802c82a5ae9 455 /* 1. read the header byte. This has the packet type in it */
DuyLionTran 11:3802c82a5ae9 456 rc = ipstack.read(readbuf, 1, timer.left_ms());
DuyLionTran 11:3802c82a5ae9 457 if (rc != 1)
DuyLionTran 11:3802c82a5ae9 458 goto exit;
DuyLionTran 11:3802c82a5ae9 459
DuyLionTran 11:3802c82a5ae9 460 len = 1;
DuyLionTran 11:3802c82a5ae9 461 /* 2. read the remaining length. This is variable in itself */
DuyLionTran 11:3802c82a5ae9 462 decodePacket(&rem_len, timer.left_ms());
DuyLionTran 11:3802c82a5ae9 463 len += MQTTPacket_encode(readbuf + 1, rem_len); /* put the original remaining length into the buffer */
DuyLionTran 11:3802c82a5ae9 464
DuyLionTran 11:3802c82a5ae9 465 if (rem_len > (MAX_MQTT_PACKET_SIZE - len))
DuyLionTran 11:3802c82a5ae9 466 {
DuyLionTran 11:3802c82a5ae9 467 rc = BUFFER_OVERFLOW;
DuyLionTran 11:3802c82a5ae9 468 goto exit;
DuyLionTran 11:3802c82a5ae9 469 }
DuyLionTran 11:3802c82a5ae9 470
DuyLionTran 11:3802c82a5ae9 471 /* 3. read the rest of the buffer using a callback to supply the rest of the data */
DuyLionTran 11:3802c82a5ae9 472 if (rem_len > 0 && (ipstack.read(readbuf + len, rem_len, timer.left_ms()) != rem_len))
DuyLionTran 11:3802c82a5ae9 473 goto exit;
DuyLionTran 11:3802c82a5ae9 474
DuyLionTran 11:3802c82a5ae9 475 header.byte = readbuf[0];
DuyLionTran 11:3802c82a5ae9 476 rc = header.bits.type;
DuyLionTran 11:3802c82a5ae9 477 if (this->keepAliveInterval > 0)
DuyLionTran 11:3802c82a5ae9 478 last_received.countdown(this->keepAliveInterval); // record the fact that we have successfully received a packet
DuyLionTran 11:3802c82a5ae9 479 exit:
DuyLionTran 11:3802c82a5ae9 480
DuyLionTran 11:3802c82a5ae9 481 #if defined(MQTT_DEBUG)
DuyLionTran 11:3802c82a5ae9 482 if (rc >= 0)
DuyLionTran 11:3802c82a5ae9 483 {
DuyLionTran 11:3802c82a5ae9 484 char printbuf[50];
DuyLionTran 11:3802c82a5ae9 485 DEBUG("Rc %d receiving packet %s\r\n", rc,
DuyLionTran 11:3802c82a5ae9 486 MQTTFormat_toClientString(printbuf, sizeof(printbuf), readbuf, len));
DuyLionTran 11:3802c82a5ae9 487 }
DuyLionTran 11:3802c82a5ae9 488 #endif
DuyLionTran 11:3802c82a5ae9 489 return rc;
DuyLionTran 11:3802c82a5ae9 490 }
DuyLionTran 11:3802c82a5ae9 491
DuyLionTran 11:3802c82a5ae9 492
DuyLionTran 11:3802c82a5ae9 493 // assume topic filter and name is in correct format
DuyLionTran 11:3802c82a5ae9 494 // # can only be at end
DuyLionTran 11:3802c82a5ae9 495 // + and # can only be next to separator
DuyLionTran 11:3802c82a5ae9 496 template<class Network, class Timer, int a, int b>
DuyLionTran 11:3802c82a5ae9 497 bool MQTT::Client<Network, Timer, a, b>::isTopicMatched(char* topicFilter, MQTTString& topicName)
DuyLionTran 11:3802c82a5ae9 498 {
DuyLionTran 11:3802c82a5ae9 499 char* curf = topicFilter;
DuyLionTran 11:3802c82a5ae9 500 char* curn = topicName.lenstring.data;
DuyLionTran 11:3802c82a5ae9 501 char* curn_end = curn + topicName.lenstring.len;
DuyLionTran 11:3802c82a5ae9 502
DuyLionTran 11:3802c82a5ae9 503 while (*curf && curn < curn_end)
DuyLionTran 11:3802c82a5ae9 504 {
DuyLionTran 11:3802c82a5ae9 505 if (*curn == '/' && *curf != '/')
DuyLionTran 11:3802c82a5ae9 506 break;
DuyLionTran 11:3802c82a5ae9 507 if (*curf != '+' && *curf != '#' && *curf != *curn)
DuyLionTran 11:3802c82a5ae9 508 break;
DuyLionTran 11:3802c82a5ae9 509 if (*curf == '+')
DuyLionTran 11:3802c82a5ae9 510 { // skip until we meet the next separator, or end of string
DuyLionTran 11:3802c82a5ae9 511 char* nextpos = curn + 1;
DuyLionTran 11:3802c82a5ae9 512 while (nextpos < curn_end && *nextpos != '/')
DuyLionTran 11:3802c82a5ae9 513 nextpos = ++curn + 1;
DuyLionTran 11:3802c82a5ae9 514 }
DuyLionTran 11:3802c82a5ae9 515 else if (*curf == '#')
DuyLionTran 11:3802c82a5ae9 516 curn = curn_end - 1; // skip until end of string
DuyLionTran 11:3802c82a5ae9 517 curf++;
DuyLionTran 11:3802c82a5ae9 518 curn++;
DuyLionTran 11:3802c82a5ae9 519 };
DuyLionTran 11:3802c82a5ae9 520
DuyLionTran 11:3802c82a5ae9 521 return (curn == curn_end) && (*curf == '\0');
DuyLionTran 11:3802c82a5ae9 522 }
DuyLionTran 11:3802c82a5ae9 523
DuyLionTran 11:3802c82a5ae9 524
DuyLionTran 11:3802c82a5ae9 525
DuyLionTran 11:3802c82a5ae9 526 template<class Network, class Timer, int a, int MAX_MESSAGE_HANDLERS>
DuyLionTran 11:3802c82a5ae9 527 int MQTT::Client<Network, Timer, a, MAX_MESSAGE_HANDLERS>::deliverMessage(MQTTString& topicName, Message& message)
DuyLionTran 11:3802c82a5ae9 528 {
DuyLionTran 11:3802c82a5ae9 529 int rc = FAILURE;
DuyLionTran 11:3802c82a5ae9 530
DuyLionTran 11:3802c82a5ae9 531 // we have to find the right message handler - indexed by topic
DuyLionTran 11:3802c82a5ae9 532 for (int i = 0; i < MAX_MESSAGE_HANDLERS; ++i)
DuyLionTran 11:3802c82a5ae9 533 {
DuyLionTran 11:3802c82a5ae9 534 if (messageHandlers[i].topicFilter != 0 && (MQTTPacket_equals(&topicName, (char*)messageHandlers[i].topicFilter) ||
DuyLionTran 11:3802c82a5ae9 535 isTopicMatched((char*)messageHandlers[i].topicFilter, topicName)))
DuyLionTran 11:3802c82a5ae9 536 {
DuyLionTran 11:3802c82a5ae9 537 if (messageHandlers[i].fp.attached())
DuyLionTran 11:3802c82a5ae9 538 {
DuyLionTran 11:3802c82a5ae9 539 MessageData md(topicName, message);
DuyLionTran 11:3802c82a5ae9 540 messageHandlers[i].fp(md);
DuyLionTran 11:3802c82a5ae9 541 rc = SUCCESS;
DuyLionTran 11:3802c82a5ae9 542 }
DuyLionTran 11:3802c82a5ae9 543 }
DuyLionTran 11:3802c82a5ae9 544 }
DuyLionTran 11:3802c82a5ae9 545
DuyLionTran 11:3802c82a5ae9 546 if (rc == FAILURE && defaultMessageHandler.attached())
DuyLionTran 11:3802c82a5ae9 547 {
DuyLionTran 11:3802c82a5ae9 548 MessageData md(topicName, message);
DuyLionTran 11:3802c82a5ae9 549 defaultMessageHandler(md);
DuyLionTran 11:3802c82a5ae9 550 rc = SUCCESS;
DuyLionTran 11:3802c82a5ae9 551 }
DuyLionTran 11:3802c82a5ae9 552
DuyLionTran 11:3802c82a5ae9 553 return rc;
DuyLionTran 11:3802c82a5ae9 554 }
DuyLionTran 11:3802c82a5ae9 555
DuyLionTran 11:3802c82a5ae9 556
DuyLionTran 11:3802c82a5ae9 557
DuyLionTran 11:3802c82a5ae9 558 template<class Network, class Timer, int a, int b>
DuyLionTran 11:3802c82a5ae9 559 int MQTT::Client<Network, Timer, a, b>::yield(unsigned long timeout_ms)
DuyLionTran 11:3802c82a5ae9 560 {
DuyLionTran 11:3802c82a5ae9 561 int rc = SUCCESS;
DuyLionTran 11:3802c82a5ae9 562 Timer timer;
DuyLionTran 11:3802c82a5ae9 563
DuyLionTran 11:3802c82a5ae9 564 timer.countdown_ms(timeout_ms);
DuyLionTran 11:3802c82a5ae9 565 while (!timer.expired())
DuyLionTran 11:3802c82a5ae9 566 {
DuyLionTran 11:3802c82a5ae9 567 if (cycle(timer) < 0)
DuyLionTran 11:3802c82a5ae9 568 {
DuyLionTran 11:3802c82a5ae9 569 rc = FAILURE;
DuyLionTran 11:3802c82a5ae9 570 break;
DuyLionTran 11:3802c82a5ae9 571 }
DuyLionTran 11:3802c82a5ae9 572 }
DuyLionTran 11:3802c82a5ae9 573
DuyLionTran 11:3802c82a5ae9 574 return rc;
DuyLionTran 11:3802c82a5ae9 575 }
DuyLionTran 11:3802c82a5ae9 576
DuyLionTran 11:3802c82a5ae9 577
DuyLionTran 11:3802c82a5ae9 578 template<class Network, class Timer, int MAX_MQTT_PACKET_SIZE, int b>
DuyLionTran 11:3802c82a5ae9 579 int MQTT::Client<Network, Timer, MAX_MQTT_PACKET_SIZE, b>::cycle(Timer& timer)
DuyLionTran 11:3802c82a5ae9 580 {
DuyLionTran 11:3802c82a5ae9 581 // get one piece of work off the wire and one pass through
DuyLionTran 11:3802c82a5ae9 582 int len = 0,
DuyLionTran 11:3802c82a5ae9 583 rc = SUCCESS;
DuyLionTran 11:3802c82a5ae9 584
DuyLionTran 11:3802c82a5ae9 585 int packet_type = readPacket(timer); // read the socket, see what work is due
DuyLionTran 11:3802c82a5ae9 586
DuyLionTran 11:3802c82a5ae9 587 switch (packet_type)
DuyLionTran 11:3802c82a5ae9 588 {
DuyLionTran 11:3802c82a5ae9 589 default:
DuyLionTran 11:3802c82a5ae9 590 // no more data to read, unrecoverable. Or read packet fails due to unexpected network error
DuyLionTran 11:3802c82a5ae9 591 rc = packet_type;
DuyLionTran 11:3802c82a5ae9 592 goto exit;
DuyLionTran 11:3802c82a5ae9 593 case NSAPI_ERROR_WOULD_BLOCK:
DuyLionTran 11:3802c82a5ae9 594 case NSAPI_ERROR_OK: // timed out reading packet
DuyLionTran 11:3802c82a5ae9 595 break;
DuyLionTran 11:3802c82a5ae9 596 case CONNACK:
DuyLionTran 11:3802c82a5ae9 597 case PUBACK:
DuyLionTran 11:3802c82a5ae9 598 case SUBACK:
DuyLionTran 11:3802c82a5ae9 599 break;
DuyLionTran 11:3802c82a5ae9 600 case PUBLISH:
DuyLionTran 11:3802c82a5ae9 601 {
DuyLionTran 11:3802c82a5ae9 602 MQTTString topicName = MQTTString_initializer;
DuyLionTran 11:3802c82a5ae9 603 Message msg;
DuyLionTran 11:3802c82a5ae9 604 int intQoS;
DuyLionTran 11:3802c82a5ae9 605 msg.payloadlen = 0; /* this is a size_t, but deserialize publish sets this as int */
DuyLionTran 11:3802c82a5ae9 606 if (MQTTDeserialize_publish((unsigned char*)&msg.dup, &intQoS, (unsigned char*)&msg.retained, (unsigned short*)&msg.id, &topicName,
DuyLionTran 11:3802c82a5ae9 607 (unsigned char**)&msg.payload, (int*)&msg.payloadlen, readbuf, MAX_MQTT_PACKET_SIZE) != 1)
DuyLionTran 11:3802c82a5ae9 608 goto exit;
DuyLionTran 11:3802c82a5ae9 609 msg.qos = (enum QoS)intQoS;
DuyLionTran 11:3802c82a5ae9 610 #if MQTTCLIENT_QOS2
DuyLionTran 11:3802c82a5ae9 611 if (msg.qos != QOS2)
DuyLionTran 11:3802c82a5ae9 612 #endif
DuyLionTran 11:3802c82a5ae9 613 deliverMessage(topicName, msg);
DuyLionTran 11:3802c82a5ae9 614 #if MQTTCLIENT_QOS2
DuyLionTran 11:3802c82a5ae9 615 else if (isQoS2msgidFree(msg.id))
DuyLionTran 11:3802c82a5ae9 616 {
DuyLionTran 11:3802c82a5ae9 617 if (useQoS2msgid(msg.id))
DuyLionTran 11:3802c82a5ae9 618 deliverMessage(topicName, msg);
DuyLionTran 11:3802c82a5ae9 619 else
DuyLionTran 11:3802c82a5ae9 620 WARN("Maximum number of incoming QoS2 messages exceeded");
DuyLionTran 11:3802c82a5ae9 621 }
DuyLionTran 11:3802c82a5ae9 622 #endif
DuyLionTran 11:3802c82a5ae9 623 #if MQTTCLIENT_QOS1 || MQTTCLIENT_QOS2
DuyLionTran 11:3802c82a5ae9 624 if (msg.qos != QOS0)
DuyLionTran 11:3802c82a5ae9 625 {
DuyLionTran 11:3802c82a5ae9 626 if (msg.qos == QOS1)
DuyLionTran 11:3802c82a5ae9 627 len = MQTTSerialize_ack(sendbuf, MAX_MQTT_PACKET_SIZE, PUBACK, 0, msg.id);
DuyLionTran 11:3802c82a5ae9 628 else if (msg.qos == QOS2)
DuyLionTran 11:3802c82a5ae9 629 len = MQTTSerialize_ack(sendbuf, MAX_MQTT_PACKET_SIZE, PUBREC, 0, msg.id);
DuyLionTran 11:3802c82a5ae9 630 if (len <= 0)
DuyLionTran 11:3802c82a5ae9 631 rc = FAILURE;
DuyLionTran 11:3802c82a5ae9 632 else
DuyLionTran 11:3802c82a5ae9 633 rc = sendPacket(len, timer);
DuyLionTran 11:3802c82a5ae9 634 if (rc == FAILURE)
DuyLionTran 11:3802c82a5ae9 635 goto exit; // there was a problem
DuyLionTran 11:3802c82a5ae9 636 }
DuyLionTran 11:3802c82a5ae9 637 break;
DuyLionTran 11:3802c82a5ae9 638 #endif
DuyLionTran 11:3802c82a5ae9 639 }
DuyLionTran 11:3802c82a5ae9 640 #if MQTTCLIENT_QOS2
DuyLionTran 11:3802c82a5ae9 641 case PUBREC:
DuyLionTran 11:3802c82a5ae9 642 case PUBREL:
DuyLionTran 11:3802c82a5ae9 643 unsigned short mypacketid;
DuyLionTran 11:3802c82a5ae9 644 unsigned char dup, type;
DuyLionTran 11:3802c82a5ae9 645 if (MQTTDeserialize_ack(&type, &dup, &mypacketid, readbuf, MAX_MQTT_PACKET_SIZE) != 1)
DuyLionTran 11:3802c82a5ae9 646 rc = FAILURE;
DuyLionTran 11:3802c82a5ae9 647 else if ((len = MQTTSerialize_ack(sendbuf, MAX_MQTT_PACKET_SIZE,
DuyLionTran 11:3802c82a5ae9 648 (packet_type == PUBREC) ? PUBREL : PUBCOMP, 0, mypacketid)) <= 0)
DuyLionTran 11:3802c82a5ae9 649 rc = FAILURE;
DuyLionTran 11:3802c82a5ae9 650 else if ((rc = sendPacket(len, timer)) != SUCCESS) // send the PUBREL packet
DuyLionTran 11:3802c82a5ae9 651 rc = FAILURE; // there was a problem
DuyLionTran 11:3802c82a5ae9 652 if (rc == FAILURE)
DuyLionTran 11:3802c82a5ae9 653 goto exit; // there was a problem
DuyLionTran 11:3802c82a5ae9 654 if (packet_type == PUBREL)
DuyLionTran 11:3802c82a5ae9 655 freeQoS2msgid(mypacketid);
DuyLionTran 11:3802c82a5ae9 656 break;
DuyLionTran 11:3802c82a5ae9 657
DuyLionTran 11:3802c82a5ae9 658 case PUBCOMP:
DuyLionTran 11:3802c82a5ae9 659 break;
DuyLionTran 11:3802c82a5ae9 660 #endif
DuyLionTran 11:3802c82a5ae9 661 case PINGRESP:
DuyLionTran 11:3802c82a5ae9 662 ping_outstanding = false;
DuyLionTran 11:3802c82a5ae9 663 break;
DuyLionTran 11:3802c82a5ae9 664 }
DuyLionTran 11:3802c82a5ae9 665
DuyLionTran 11:3802c82a5ae9 666 if (keepalive() != SUCCESS)
DuyLionTran 11:3802c82a5ae9 667 //check only keepalive FAILURE status so that previous FAILURE status can be considered as FAULT
DuyLionTran 11:3802c82a5ae9 668 rc = FAILURE;
DuyLionTran 11:3802c82a5ae9 669
DuyLionTran 11:3802c82a5ae9 670 exit:
DuyLionTran 11:3802c82a5ae9 671 if (rc == SUCCESS)
DuyLionTran 11:3802c82a5ae9 672 rc = packet_type;
DuyLionTran 11:3802c82a5ae9 673 else if (isconnected)
DuyLionTran 11:3802c82a5ae9 674 closeSession();
DuyLionTran 11:3802c82a5ae9 675 return rc;
DuyLionTran 11:3802c82a5ae9 676 }
DuyLionTran 11:3802c82a5ae9 677
DuyLionTran 11:3802c82a5ae9 678
DuyLionTran 11:3802c82a5ae9 679 template<class Network, class Timer, int MAX_MQTT_PACKET_SIZE, int b>
DuyLionTran 11:3802c82a5ae9 680 int MQTT::Client<Network, Timer, MAX_MQTT_PACKET_SIZE, b>::keepalive()
DuyLionTran 11:3802c82a5ae9 681 {
DuyLionTran 11:3802c82a5ae9 682 int rc = SUCCESS;
DuyLionTran 11:3802c82a5ae9 683 static Timer ping_sent;
DuyLionTran 11:3802c82a5ae9 684
DuyLionTran 11:3802c82a5ae9 685 if (keepAliveInterval == 0)
DuyLionTran 11:3802c82a5ae9 686 goto exit;
DuyLionTran 11:3802c82a5ae9 687
DuyLionTran 11:3802c82a5ae9 688 if (ping_outstanding)
DuyLionTran 11:3802c82a5ae9 689 {
DuyLionTran 11:3802c82a5ae9 690 if (ping_sent.expired())
DuyLionTran 11:3802c82a5ae9 691 {
DuyLionTran 11:3802c82a5ae9 692 rc = FAILURE; // session failure
DuyLionTran 11:3802c82a5ae9 693 #if defined(MQTT_DEBUG)
DuyLionTran 11:3802c82a5ae9 694 DEBUG("PINGRESP not received in keepalive interval\r\n");
DuyLionTran 11:3802c82a5ae9 695 #endif
DuyLionTran 11:3802c82a5ae9 696 }
DuyLionTran 11:3802c82a5ae9 697 }
DuyLionTran 11:3802c82a5ae9 698 else if (last_sent.expired() || last_received.expired())
DuyLionTran 11:3802c82a5ae9 699 {
DuyLionTran 11:3802c82a5ae9 700 Timer timer(1000);
DuyLionTran 11:3802c82a5ae9 701 int len = MQTTSerialize_pingreq(sendbuf, MAX_MQTT_PACKET_SIZE);
DuyLionTran 11:3802c82a5ae9 702 if (len > 0 && (rc = sendPacket(len, timer)) == SUCCESS) // send the ping packet
DuyLionTran 11:3802c82a5ae9 703 {
DuyLionTran 11:3802c82a5ae9 704 ping_outstanding = true;
DuyLionTran 11:3802c82a5ae9 705 ping_sent.countdown(this->keepAliveInterval);
DuyLionTran 11:3802c82a5ae9 706 }
DuyLionTran 11:3802c82a5ae9 707 }
DuyLionTran 11:3802c82a5ae9 708 exit:
DuyLionTran 11:3802c82a5ae9 709 return rc;
DuyLionTran 11:3802c82a5ae9 710 }
DuyLionTran 11:3802c82a5ae9 711
DuyLionTran 11:3802c82a5ae9 712
DuyLionTran 11:3802c82a5ae9 713 // only used in single-threaded mode where one command at a time is in process
DuyLionTran 11:3802c82a5ae9 714 template<class Network, class Timer, int a, int b>
DuyLionTran 11:3802c82a5ae9 715 int MQTT::Client<Network, Timer, a, b>::waitfor(int packet_type, Timer& timer)
DuyLionTran 11:3802c82a5ae9 716 {
DuyLionTran 11:3802c82a5ae9 717 int rc = FAILURE;
DuyLionTran 11:3802c82a5ae9 718
DuyLionTran 11:3802c82a5ae9 719 do
DuyLionTran 11:3802c82a5ae9 720 {
DuyLionTran 11:3802c82a5ae9 721 if (timer.expired())
DuyLionTran 11:3802c82a5ae9 722 break; // we timed out
DuyLionTran 11:3802c82a5ae9 723 rc = cycle(timer);
DuyLionTran 11:3802c82a5ae9 724 }
DuyLionTran 11:3802c82a5ae9 725 while (rc != packet_type && rc >= 0);
DuyLionTran 11:3802c82a5ae9 726
DuyLionTran 11:3802c82a5ae9 727 return rc;
DuyLionTran 11:3802c82a5ae9 728 }
DuyLionTran 11:3802c82a5ae9 729
DuyLionTran 11:3802c82a5ae9 730
DuyLionTran 11:3802c82a5ae9 731 template<class Network, class Timer, int MAX_MQTT_PACKET_SIZE, int b>
DuyLionTran 11:3802c82a5ae9 732 int MQTT::Client<Network, Timer, MAX_MQTT_PACKET_SIZE, b>::connect(MQTTPacket_connectData& options, connackData& data)
DuyLionTran 11:3802c82a5ae9 733 {
DuyLionTran 11:3802c82a5ae9 734 Timer connect_timer(command_timeout_ms);
DuyLionTran 11:3802c82a5ae9 735 int rc = FAILURE;
DuyLionTran 11:3802c82a5ae9 736 int len = 0;
DuyLionTran 11:3802c82a5ae9 737
DuyLionTran 11:3802c82a5ae9 738 if (isconnected) // don't send connect packet again if we are already connected
DuyLionTran 11:3802c82a5ae9 739 goto exit;
DuyLionTran 11:3802c82a5ae9 740
DuyLionTran 11:3802c82a5ae9 741 this->keepAliveInterval = options.keepAliveInterval;
DuyLionTran 11:3802c82a5ae9 742 this->cleansession = options.cleansession;
DuyLionTran 11:3802c82a5ae9 743 if ((len = MQTTSerialize_connect(sendbuf, MAX_MQTT_PACKET_SIZE, &options)) <= 0)
DuyLionTran 11:3802c82a5ae9 744 goto exit;
DuyLionTran 11:3802c82a5ae9 745 if ((rc = sendPacket(len, connect_timer)) != SUCCESS) // send the connect packet
DuyLionTran 11:3802c82a5ae9 746 goto exit; // there was a problem
DuyLionTran 11:3802c82a5ae9 747
DuyLionTran 11:3802c82a5ae9 748 if (this->keepAliveInterval > 0)
DuyLionTran 11:3802c82a5ae9 749 last_received.countdown(this->keepAliveInterval);
DuyLionTran 11:3802c82a5ae9 750 // this will be a blocking call, wait for the connack
DuyLionTran 11:3802c82a5ae9 751 if (waitfor(CONNACK, connect_timer) == CONNACK)
DuyLionTran 11:3802c82a5ae9 752 {
DuyLionTran 11:3802c82a5ae9 753 data.rc = 0;
DuyLionTran 11:3802c82a5ae9 754 data.sessionPresent = false;
DuyLionTran 11:3802c82a5ae9 755 if (MQTTDeserialize_connack((unsigned char*)&data.sessionPresent,
DuyLionTran 11:3802c82a5ae9 756 (unsigned char*)&data.rc, readbuf, MAX_MQTT_PACKET_SIZE) == 1)
DuyLionTran 11:3802c82a5ae9 757 rc = data.rc;
DuyLionTran 11:3802c82a5ae9 758 else
DuyLionTran 11:3802c82a5ae9 759 rc = FAILURE;
DuyLionTran 11:3802c82a5ae9 760 }
DuyLionTran 11:3802c82a5ae9 761 else
DuyLionTran 11:3802c82a5ae9 762 rc = FAILURE;
DuyLionTran 11:3802c82a5ae9 763
DuyLionTran 11:3802c82a5ae9 764 #if MQTTCLIENT_QOS2
DuyLionTran 11:3802c82a5ae9 765 // resend any inflight publish
DuyLionTran 11:3802c82a5ae9 766 if (inflightMsgid > 0 && inflightQoS == QOS2 && pubrel)
DuyLionTran 11:3802c82a5ae9 767 {
DuyLionTran 11:3802c82a5ae9 768 if ((len = MQTTSerialize_ack(sendbuf, MAX_MQTT_PACKET_SIZE, PUBREL, 0, inflightMsgid)) <= 0)
DuyLionTran 11:3802c82a5ae9 769 rc = FAILURE;
DuyLionTran 11:3802c82a5ae9 770 else
DuyLionTran 11:3802c82a5ae9 771 rc = publish(len, connect_timer, inflightQoS);
DuyLionTran 11:3802c82a5ae9 772 }
DuyLionTran 11:3802c82a5ae9 773 else
DuyLionTran 11:3802c82a5ae9 774 #endif
DuyLionTran 11:3802c82a5ae9 775 #if MQTTCLIENT_QOS1 || MQTTCLIENT_QOS2
DuyLionTran 11:3802c82a5ae9 776 if (inflightMsgid > 0)
DuyLionTran 11:3802c82a5ae9 777 {
DuyLionTran 11:3802c82a5ae9 778 memcpy(sendbuf, pubbuf, MAX_MQTT_PACKET_SIZE);
DuyLionTran 11:3802c82a5ae9 779 rc = publish(inflightLen, connect_timer, inflightQoS);
DuyLionTran 11:3802c82a5ae9 780 }
DuyLionTran 11:3802c82a5ae9 781 #endif
DuyLionTran 11:3802c82a5ae9 782
DuyLionTran 11:3802c82a5ae9 783 exit:
DuyLionTran 11:3802c82a5ae9 784 if (rc == SUCCESS)
DuyLionTran 11:3802c82a5ae9 785 {
DuyLionTran 11:3802c82a5ae9 786 isconnected = true;
DuyLionTran 11:3802c82a5ae9 787 ping_outstanding = false;
DuyLionTran 11:3802c82a5ae9 788 }
DuyLionTran 11:3802c82a5ae9 789 return rc;
DuyLionTran 11:3802c82a5ae9 790 }
DuyLionTran 11:3802c82a5ae9 791
DuyLionTran 11:3802c82a5ae9 792
DuyLionTran 11:3802c82a5ae9 793 template<class Network, class Timer, int MAX_MQTT_PACKET_SIZE, int b>
DuyLionTran 11:3802c82a5ae9 794 int MQTT::Client<Network, Timer, MAX_MQTT_PACKET_SIZE, b>::connect(MQTTPacket_connectData& options)
DuyLionTran 11:3802c82a5ae9 795 {
DuyLionTran 11:3802c82a5ae9 796 connackData data;
DuyLionTran 11:3802c82a5ae9 797 return connect(options, data);
DuyLionTran 11:3802c82a5ae9 798 }
DuyLionTran 11:3802c82a5ae9 799
DuyLionTran 11:3802c82a5ae9 800
DuyLionTran 11:3802c82a5ae9 801 template<class Network, class Timer, int MAX_MQTT_PACKET_SIZE, int b>
DuyLionTran 11:3802c82a5ae9 802 int MQTT::Client<Network, Timer, MAX_MQTT_PACKET_SIZE, b>::connect()
DuyLionTran 11:3802c82a5ae9 803 {
DuyLionTran 11:3802c82a5ae9 804 MQTTPacket_connectData default_options = MQTTPacket_connectData_initializer;
DuyLionTran 11:3802c82a5ae9 805 return connect(default_options);
DuyLionTran 11:3802c82a5ae9 806 }
DuyLionTran 11:3802c82a5ae9 807
DuyLionTran 11:3802c82a5ae9 808
DuyLionTran 11:3802c82a5ae9 809 template<class Network, class Timer, int MAX_MQTT_PACKET_SIZE, int MAX_MESSAGE_HANDLERS>
DuyLionTran 11:3802c82a5ae9 810 int MQTT::Client<Network, Timer, MAX_MQTT_PACKET_SIZE, MAX_MESSAGE_HANDLERS>::setMessageHandler(const char* topicFilter, messageHandler messageHandler)
DuyLionTran 11:3802c82a5ae9 811 {
DuyLionTran 11:3802c82a5ae9 812 int rc = FAILURE;
DuyLionTran 11:3802c82a5ae9 813 int i = -1;
DuyLionTran 11:3802c82a5ae9 814
DuyLionTran 11:3802c82a5ae9 815 // first check for an existing matching slot
DuyLionTran 11:3802c82a5ae9 816 for (i = 0; i < MAX_MESSAGE_HANDLERS; ++i)
DuyLionTran 11:3802c82a5ae9 817 {
DuyLionTran 11:3802c82a5ae9 818 if (messageHandlers[i].topicFilter != 0 && strcmp(messageHandlers[i].topicFilter, topicFilter) == 0)
DuyLionTran 11:3802c82a5ae9 819 {
DuyLionTran 11:3802c82a5ae9 820 if (messageHandler == 0) // remove existing
DuyLionTran 11:3802c82a5ae9 821 {
DuyLionTran 11:3802c82a5ae9 822 messageHandlers[i].topicFilter = 0;
DuyLionTran 11:3802c82a5ae9 823 messageHandlers[i].fp.detach();
DuyLionTran 11:3802c82a5ae9 824 }
DuyLionTran 11:3802c82a5ae9 825 rc = SUCCESS; // return i when adding new subscription
DuyLionTran 11:3802c82a5ae9 826 break;
DuyLionTran 11:3802c82a5ae9 827 }
DuyLionTran 11:3802c82a5ae9 828 }
DuyLionTran 11:3802c82a5ae9 829 // if no existing, look for empty slot (unless we are removing)
DuyLionTran 11:3802c82a5ae9 830 if (messageHandler != 0) {
DuyLionTran 11:3802c82a5ae9 831 if (rc == FAILURE)
DuyLionTran 11:3802c82a5ae9 832 {
DuyLionTran 11:3802c82a5ae9 833 for (i = 0; i < MAX_MESSAGE_HANDLERS; ++i)
DuyLionTran 11:3802c82a5ae9 834 {
DuyLionTran 11:3802c82a5ae9 835 if (messageHandlers[i].topicFilter == 0)
DuyLionTran 11:3802c82a5ae9 836 {
DuyLionTran 11:3802c82a5ae9 837 rc = SUCCESS;
DuyLionTran 11:3802c82a5ae9 838 break;
DuyLionTran 11:3802c82a5ae9 839 }
DuyLionTran 11:3802c82a5ae9 840 }
DuyLionTran 11:3802c82a5ae9 841 }
DuyLionTran 11:3802c82a5ae9 842 if (i < MAX_MESSAGE_HANDLERS)
DuyLionTran 11:3802c82a5ae9 843 {
DuyLionTran 11:3802c82a5ae9 844 messageHandlers[i].topicFilter = topicFilter;
DuyLionTran 11:3802c82a5ae9 845 messageHandlers[i].fp.attach(messageHandler);
DuyLionTran 11:3802c82a5ae9 846 }
DuyLionTran 11:3802c82a5ae9 847 }
DuyLionTran 11:3802c82a5ae9 848 return rc;
DuyLionTran 11:3802c82a5ae9 849 }
DuyLionTran 11:3802c82a5ae9 850
DuyLionTran 11:3802c82a5ae9 851
DuyLionTran 11:3802c82a5ae9 852 template<class Network, class Timer, int MAX_MQTT_PACKET_SIZE, int MAX_MESSAGE_HANDLERS>
DuyLionTran 11:3802c82a5ae9 853 int MQTT::Client<Network, Timer, MAX_MQTT_PACKET_SIZE, MAX_MESSAGE_HANDLERS>::subscribe(const char* topicFilter,
DuyLionTran 11:3802c82a5ae9 854 enum QoS qos, messageHandler messageHandler, subackData& data)
DuyLionTran 11:3802c82a5ae9 855 {
DuyLionTran 11:3802c82a5ae9 856 int rc = FAILURE;
DuyLionTran 11:3802c82a5ae9 857 Timer timer(command_timeout_ms);
DuyLionTran 11:3802c82a5ae9 858 int len = 0;
DuyLionTran 11:3802c82a5ae9 859 MQTTString topic = {(char*)topicFilter, {0, 0}};
DuyLionTran 11:3802c82a5ae9 860
DuyLionTran 11:3802c82a5ae9 861 if (!isconnected)
DuyLionTran 11:3802c82a5ae9 862 goto exit;
DuyLionTran 11:3802c82a5ae9 863
DuyLionTran 11:3802c82a5ae9 864 len = MQTTSerialize_subscribe(sendbuf, MAX_MQTT_PACKET_SIZE, 0, packetid.getNext(), 1, &topic, (int*)&qos);
DuyLionTran 11:3802c82a5ae9 865 if (len <= 0)
DuyLionTran 11:3802c82a5ae9 866 goto exit;
DuyLionTran 11:3802c82a5ae9 867 if ((rc = sendPacket(len, timer)) != SUCCESS) // send the subscribe packet
DuyLionTran 11:3802c82a5ae9 868 goto exit; // there was a problem
DuyLionTran 11:3802c82a5ae9 869
DuyLionTran 11:3802c82a5ae9 870 if (waitfor(SUBACK, timer) == SUBACK) // wait for suback
DuyLionTran 11:3802c82a5ae9 871 {
DuyLionTran 11:3802c82a5ae9 872 int count = 0;
DuyLionTran 11:3802c82a5ae9 873 unsigned short mypacketid;
DuyLionTran 11:3802c82a5ae9 874 data.grantedQoS = 0;
DuyLionTran 11:3802c82a5ae9 875 if (MQTTDeserialize_suback(&mypacketid, 1, &count, &data.grantedQoS, readbuf, MAX_MQTT_PACKET_SIZE) == 1)
DuyLionTran 11:3802c82a5ae9 876 {
DuyLionTran 11:3802c82a5ae9 877 if (data.grantedQoS != 0x80)
DuyLionTran 11:3802c82a5ae9 878 rc = setMessageHandler(topicFilter, messageHandler);
DuyLionTran 11:3802c82a5ae9 879 }
DuyLionTran 11:3802c82a5ae9 880 }
DuyLionTran 11:3802c82a5ae9 881 else
DuyLionTran 11:3802c82a5ae9 882 rc = FAILURE;
DuyLionTran 11:3802c82a5ae9 883
DuyLionTran 11:3802c82a5ae9 884 exit:
DuyLionTran 11:3802c82a5ae9 885 if (rc == FAILURE)
DuyLionTran 11:3802c82a5ae9 886 closeSession();
DuyLionTran 11:3802c82a5ae9 887 return rc;
DuyLionTran 11:3802c82a5ae9 888 }
DuyLionTran 11:3802c82a5ae9 889
DuyLionTran 11:3802c82a5ae9 890
DuyLionTran 11:3802c82a5ae9 891 template<class Network, class Timer, int MAX_MQTT_PACKET_SIZE, int MAX_MESSAGE_HANDLERS>
DuyLionTran 11:3802c82a5ae9 892 int MQTT::Client<Network, Timer, MAX_MQTT_PACKET_SIZE, MAX_MESSAGE_HANDLERS>::subscribe(const char* topicFilter, enum QoS qos, messageHandler messageHandler)
DuyLionTran 11:3802c82a5ae9 893 {
DuyLionTran 11:3802c82a5ae9 894 subackData data;
DuyLionTran 11:3802c82a5ae9 895 return subscribe(topicFilter, qos, messageHandler, data);
DuyLionTran 11:3802c82a5ae9 896 }
DuyLionTran 11:3802c82a5ae9 897
DuyLionTran 11:3802c82a5ae9 898
DuyLionTran 11:3802c82a5ae9 899 template<class Network, class Timer, int MAX_MQTT_PACKET_SIZE, int MAX_MESSAGE_HANDLERS>
DuyLionTran 11:3802c82a5ae9 900 int MQTT::Client<Network, Timer, MAX_MQTT_PACKET_SIZE, MAX_MESSAGE_HANDLERS>::unsubscribe(const char* topicFilter)
DuyLionTran 11:3802c82a5ae9 901 {
DuyLionTran 11:3802c82a5ae9 902 int rc = FAILURE;
DuyLionTran 11:3802c82a5ae9 903 Timer timer(command_timeout_ms);
DuyLionTran 11:3802c82a5ae9 904 MQTTString topic = {(char*)topicFilter, {0, 0}};
DuyLionTran 11:3802c82a5ae9 905 int len = 0;
DuyLionTran 11:3802c82a5ae9 906
DuyLionTran 11:3802c82a5ae9 907 if (!isconnected)
DuyLionTran 11:3802c82a5ae9 908 goto exit;
DuyLionTran 11:3802c82a5ae9 909
DuyLionTran 11:3802c82a5ae9 910 if ((len = MQTTSerialize_unsubscribe(sendbuf, MAX_MQTT_PACKET_SIZE, 0, packetid.getNext(), 1, &topic)) <= 0)
DuyLionTran 11:3802c82a5ae9 911 goto exit;
DuyLionTran 11:3802c82a5ae9 912 if ((rc = sendPacket(len, timer)) != SUCCESS) // send the unsubscribe packet
DuyLionTran 11:3802c82a5ae9 913 goto exit; // there was a problem
DuyLionTran 11:3802c82a5ae9 914
DuyLionTran 11:3802c82a5ae9 915 if (waitfor(UNSUBACK, timer) == UNSUBACK)
DuyLionTran 11:3802c82a5ae9 916 {
DuyLionTran 11:3802c82a5ae9 917 unsigned short mypacketid; // should be the same as the packetid above
DuyLionTran 11:3802c82a5ae9 918 if (MQTTDeserialize_unsuback(&mypacketid, readbuf, MAX_MQTT_PACKET_SIZE) == 1)
DuyLionTran 11:3802c82a5ae9 919 {
DuyLionTran 11:3802c82a5ae9 920 // remove the subscription message handler associated with this topic, if there is one
DuyLionTran 11:3802c82a5ae9 921 setMessageHandler(topicFilter, 0);
DuyLionTran 11:3802c82a5ae9 922 }
DuyLionTran 11:3802c82a5ae9 923 }
DuyLionTran 11:3802c82a5ae9 924 else
DuyLionTran 11:3802c82a5ae9 925 rc = FAILURE;
DuyLionTran 11:3802c82a5ae9 926
DuyLionTran 11:3802c82a5ae9 927 exit:
DuyLionTran 11:3802c82a5ae9 928 if (rc != SUCCESS)
DuyLionTran 11:3802c82a5ae9 929 closeSession();
DuyLionTran 11:3802c82a5ae9 930 return rc;
DuyLionTran 11:3802c82a5ae9 931 }
DuyLionTran 11:3802c82a5ae9 932
DuyLionTran 11:3802c82a5ae9 933
DuyLionTran 11:3802c82a5ae9 934 template<class Network, class Timer, int MAX_MQTT_PACKET_SIZE, int b>
DuyLionTran 11:3802c82a5ae9 935 int MQTT::Client<Network, Timer, MAX_MQTT_PACKET_SIZE, b>::publish(int len, Timer& timer, enum QoS qos)
DuyLionTran 11:3802c82a5ae9 936 {
DuyLionTran 11:3802c82a5ae9 937 int rc;
DuyLionTran 11:3802c82a5ae9 938
DuyLionTran 11:3802c82a5ae9 939 if ((rc = sendPacket(len, timer)) != SUCCESS) // send the publish packet
DuyLionTran 11:3802c82a5ae9 940 goto exit; // there was a problem
DuyLionTran 11:3802c82a5ae9 941
DuyLionTran 11:3802c82a5ae9 942 #if MQTTCLIENT_QOS1
DuyLionTran 11:3802c82a5ae9 943 if (qos == QOS1)
DuyLionTran 11:3802c82a5ae9 944 {
DuyLionTran 11:3802c82a5ae9 945 if (waitfor(PUBACK, timer) == PUBACK)
DuyLionTran 11:3802c82a5ae9 946 {
DuyLionTran 11:3802c82a5ae9 947 unsigned short mypacketid;
DuyLionTran 11:3802c82a5ae9 948 unsigned char dup, type;
DuyLionTran 11:3802c82a5ae9 949 if (MQTTDeserialize_ack(&type, &dup, &mypacketid, readbuf, MAX_MQTT_PACKET_SIZE) != 1)
DuyLionTran 11:3802c82a5ae9 950 rc = FAILURE;
DuyLionTran 11:3802c82a5ae9 951 else if (inflightMsgid == mypacketid)
DuyLionTran 11:3802c82a5ae9 952 inflightMsgid = 0;
DuyLionTran 11:3802c82a5ae9 953 }
DuyLionTran 11:3802c82a5ae9 954 else
DuyLionTran 11:3802c82a5ae9 955 rc = FAILURE;
DuyLionTran 11:3802c82a5ae9 956 }
DuyLionTran 11:3802c82a5ae9 957 #endif
DuyLionTran 11:3802c82a5ae9 958 #if MQTTCLIENT_QOS2
DuyLionTran 11:3802c82a5ae9 959 else if (qos == QOS2)
DuyLionTran 11:3802c82a5ae9 960 {
DuyLionTran 11:3802c82a5ae9 961 if (waitfor(PUBCOMP, timer) == PUBCOMP)
DuyLionTran 11:3802c82a5ae9 962 {
DuyLionTran 11:3802c82a5ae9 963 unsigned short mypacketid;
DuyLionTran 11:3802c82a5ae9 964 unsigned char dup, type;
DuyLionTran 11:3802c82a5ae9 965 if (MQTTDeserialize_ack(&type, &dup, &mypacketid, readbuf, MAX_MQTT_PACKET_SIZE) != 1)
DuyLionTran 11:3802c82a5ae9 966 rc = FAILURE;
DuyLionTran 11:3802c82a5ae9 967 else if (inflightMsgid == mypacketid)
DuyLionTran 11:3802c82a5ae9 968 inflightMsgid = 0;
DuyLionTran 11:3802c82a5ae9 969 }
DuyLionTran 11:3802c82a5ae9 970 else
DuyLionTran 11:3802c82a5ae9 971 rc = FAILURE;
DuyLionTran 11:3802c82a5ae9 972 }
DuyLionTran 11:3802c82a5ae9 973 #endif
DuyLionTran 11:3802c82a5ae9 974
DuyLionTran 11:3802c82a5ae9 975 exit:
DuyLionTran 11:3802c82a5ae9 976 if (rc != SUCCESS)
DuyLionTran 11:3802c82a5ae9 977 closeSession();
DuyLionTran 11:3802c82a5ae9 978 return rc;
DuyLionTran 11:3802c82a5ae9 979 }
DuyLionTran 11:3802c82a5ae9 980
DuyLionTran 11:3802c82a5ae9 981
DuyLionTran 11:3802c82a5ae9 982
DuyLionTran 11:3802c82a5ae9 983 template<class Network, class Timer, int MAX_MQTT_PACKET_SIZE, int b>
DuyLionTran 11:3802c82a5ae9 984 int MQTT::Client<Network, Timer, MAX_MQTT_PACKET_SIZE, b>::publish(const char* topicName, void* payload, size_t payloadlen, unsigned short& id, enum QoS qos, bool retained)
DuyLionTran 11:3802c82a5ae9 985 {
DuyLionTran 11:3802c82a5ae9 986 int rc = FAILURE;
DuyLionTran 11:3802c82a5ae9 987 Timer timer(command_timeout_ms);
DuyLionTran 11:3802c82a5ae9 988 MQTTString topicString = MQTTString_initializer;
DuyLionTran 11:3802c82a5ae9 989 int len = 0;
DuyLionTran 11:3802c82a5ae9 990
DuyLionTran 11:3802c82a5ae9 991 if (!isconnected)
DuyLionTran 11:3802c82a5ae9 992 goto exit;
DuyLionTran 11:3802c82a5ae9 993
DuyLionTran 11:3802c82a5ae9 994 topicString.cstring = (char*)topicName;
DuyLionTran 11:3802c82a5ae9 995
DuyLionTran 11:3802c82a5ae9 996 #if MQTTCLIENT_QOS1 || MQTTCLIENT_QOS2
DuyLionTran 11:3802c82a5ae9 997 if (qos == QOS1 || qos == QOS2)
DuyLionTran 11:3802c82a5ae9 998 id = packetid.getNext();
DuyLionTran 11:3802c82a5ae9 999 #endif
DuyLionTran 11:3802c82a5ae9 1000
DuyLionTran 11:3802c82a5ae9 1001 len = MQTTSerialize_publish(sendbuf, MAX_MQTT_PACKET_SIZE, 0, qos, retained, id,
DuyLionTran 11:3802c82a5ae9 1002 topicString, (unsigned char*)payload, payloadlen);
DuyLionTran 11:3802c82a5ae9 1003 if (len <= 0)
DuyLionTran 11:3802c82a5ae9 1004 goto exit;
DuyLionTran 11:3802c82a5ae9 1005
DuyLionTran 11:3802c82a5ae9 1006 #if MQTTCLIENT_QOS1 || MQTTCLIENT_QOS2
DuyLionTran 11:3802c82a5ae9 1007 if (!cleansession)
DuyLionTran 11:3802c82a5ae9 1008 {
DuyLionTran 11:3802c82a5ae9 1009 memcpy(pubbuf, sendbuf, len);
DuyLionTran 11:3802c82a5ae9 1010 inflightMsgid = id;
DuyLionTran 11:3802c82a5ae9 1011 inflightLen = len;
DuyLionTran 11:3802c82a5ae9 1012 inflightQoS = qos;
DuyLionTran 11:3802c82a5ae9 1013 #if MQTTCLIENT_QOS2
DuyLionTran 11:3802c82a5ae9 1014 pubrel = false;
DuyLionTran 11:3802c82a5ae9 1015 #endif
DuyLionTran 11:3802c82a5ae9 1016 }
DuyLionTran 11:3802c82a5ae9 1017 #endif
DuyLionTran 11:3802c82a5ae9 1018
DuyLionTran 11:3802c82a5ae9 1019 rc = publish(len, timer, qos);
DuyLionTran 11:3802c82a5ae9 1020 exit:
DuyLionTran 11:3802c82a5ae9 1021 return rc;
DuyLionTran 11:3802c82a5ae9 1022 }
DuyLionTran 11:3802c82a5ae9 1023
DuyLionTran 11:3802c82a5ae9 1024
DuyLionTran 11:3802c82a5ae9 1025 template<class Network, class Timer, int MAX_MQTT_PACKET_SIZE, int b>
DuyLionTran 11:3802c82a5ae9 1026 int MQTT::Client<Network, Timer, MAX_MQTT_PACKET_SIZE, b>::publish(const char* topicName, void* payload, size_t payloadlen, enum QoS qos, bool retained)
DuyLionTran 11:3802c82a5ae9 1027 {
DuyLionTran 11:3802c82a5ae9 1028 unsigned short id = 0; // dummy - not used for anything
DuyLionTran 11:3802c82a5ae9 1029 return publish(topicName, payload, payloadlen, id, qos, retained);
DuyLionTran 11:3802c82a5ae9 1030 }
DuyLionTran 11:3802c82a5ae9 1031
DuyLionTran 11:3802c82a5ae9 1032
DuyLionTran 11:3802c82a5ae9 1033 template<class Network, class Timer, int MAX_MQTT_PACKET_SIZE, int b>
DuyLionTran 11:3802c82a5ae9 1034 int MQTT::Client<Network, Timer, MAX_MQTT_PACKET_SIZE, b>::publish(const char* topicName, Message& message)
DuyLionTran 11:3802c82a5ae9 1035 {
DuyLionTran 11:3802c82a5ae9 1036 return publish(topicName, message.payload, message.payloadlen, message.qos, message.retained);
DuyLionTran 11:3802c82a5ae9 1037 }
DuyLionTran 11:3802c82a5ae9 1038
DuyLionTran 11:3802c82a5ae9 1039
DuyLionTran 11:3802c82a5ae9 1040 template<class Network, class Timer, int MAX_MQTT_PACKET_SIZE, int b>
DuyLionTran 11:3802c82a5ae9 1041 int MQTT::Client<Network, Timer, MAX_MQTT_PACKET_SIZE, b>::disconnect()
DuyLionTran 11:3802c82a5ae9 1042 {
DuyLionTran 11:3802c82a5ae9 1043 int rc = FAILURE;
DuyLionTran 11:3802c82a5ae9 1044 Timer timer(command_timeout_ms); // we might wait for incomplete incoming publishes to complete
DuyLionTran 11:3802c82a5ae9 1045 int len = MQTTSerialize_disconnect(sendbuf, MAX_MQTT_PACKET_SIZE);
DuyLionTran 11:3802c82a5ae9 1046 if (len > 0)
DuyLionTran 11:3802c82a5ae9 1047 rc = sendPacket(len, timer); // send the disconnect packet
DuyLionTran 11:3802c82a5ae9 1048 closeSession();
DuyLionTran 11:3802c82a5ae9 1049 return rc;
DuyLionTran 11:3802c82a5ae9 1050 }
DuyLionTran 11:3802c82a5ae9 1051
DuyLionTran 11:3802c82a5ae9 1052 #endif