USE YHTANG AWS ACCOUNT TO TEST MQTT
Dependents: NuMaker-mbed-AWS-IoT-example
MQTTClient.h@40:9623a2c9c8ac, 2014-08-01 (annotated)
- Committer:
- icraggs
- Date:
- Fri Aug 01 23:44:12 2014 +0000
- Revision:
- 40:9623a2c9c8ac
- Parent:
- 37:e3d64f9b986c
- Parent:
- 34:e18a166198df
- Child:
- 41:b7e86fa6dbb8
Merging
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
icraggs | 6:4d312a49200b | 1 | /******************************************************************************* |
icraggs | 6:4d312a49200b | 2 | * Copyright (c) 2014 IBM Corp. |
sam_grove | 0:fe461e4d7afe | 3 | * |
icraggs | 6:4d312a49200b | 4 | * All rights reserved. This program and the accompanying materials |
icraggs | 6:4d312a49200b | 5 | * are made available under the terms of the Eclipse Public License v1.0 |
icraggs | 6:4d312a49200b | 6 | * and Eclipse Distribution License v1.0 which accompany this distribution. |
sam_grove | 0:fe461e4d7afe | 7 | * |
icraggs | 6:4d312a49200b | 8 | * The Eclipse Public License is available at |
icraggs | 6:4d312a49200b | 9 | * http://www.eclipse.org/legal/epl-v10.html |
icraggs | 6:4d312a49200b | 10 | * and the Eclipse Distribution License is available at |
icraggs | 6:4d312a49200b | 11 | * http://www.eclipse.org/org/documents/edl-v10.php. |
sam_grove | 0:fe461e4d7afe | 12 | * |
icraggs | 6:4d312a49200b | 13 | * Contributors: |
icraggs | 6:4d312a49200b | 14 | * Ian Craggs - initial API and implementation and/or initial documentation |
icraggs | 6:4d312a49200b | 15 | *******************************************************************************/ |
icraggs | 21:e918525e529d | 16 | |
icraggs | 21:e918525e529d | 17 | /* |
icraggs | 21:e918525e529d | 18 | |
icraggs | 21:e918525e529d | 19 | TODO: |
icraggs | 21:e918525e529d | 20 | |
icraggs | 26:2658bb87c53d | 21 | ensure publish packets are retried on reconnect |
icraggs | 23:05fc7de97d4a | 22 | |
icraggs | 21:e918525e529d | 23 | */ |
sam_grove | 0:fe461e4d7afe | 24 | |
icraggs | 2:dcfdd2abfe71 | 25 | #if !defined(MQTTCLIENT_H) |
icraggs | 2:dcfdd2abfe71 | 26 | #define MQTTCLIENT_H |
icraggs | 2:dcfdd2abfe71 | 27 | |
icraggs | 34:e18a166198df | 28 | #include "FP.h" |
icraggs | 3:dbff6b768d28 | 29 | #include "MQTTPacket.h" |
icraggs | 6:4d312a49200b | 30 | #include "stdio.h" |
icraggs | 2:dcfdd2abfe71 | 31 | |
icraggs | 3:dbff6b768d28 | 32 | namespace MQTT |
icraggs | 3:dbff6b768d28 | 33 | { |
icraggs | 3:dbff6b768d28 | 34 | |
icraggs | 2:dcfdd2abfe71 | 35 | |
icraggs | 2:dcfdd2abfe71 | 36 | enum QoS { QOS0, QOS1, QOS2 }; |
sam_grove | 0:fe461e4d7afe | 37 | |
icraggs | 31:a51dd239b78e | 38 | // all failure return codes must be negative |
icraggs | 23:05fc7de97d4a | 39 | enum returnCode { BUFFER_OVERFLOW = -2, FAILURE = -1, SUCCESS = 0 }; |
icraggs | 23:05fc7de97d4a | 40 | |
icraggs | 2:dcfdd2abfe71 | 41 | |
icraggs | 3:dbff6b768d28 | 42 | struct Message |
icraggs | 2:dcfdd2abfe71 | 43 | { |
icraggs | 2:dcfdd2abfe71 | 44 | enum QoS qos; |
icraggs | 2:dcfdd2abfe71 | 45 | bool retained; |
icraggs | 2:dcfdd2abfe71 | 46 | bool dup; |
Ian Craggs |
12:cc7f2d62a393 | 47 | unsigned short id; |
icraggs | 2:dcfdd2abfe71 | 48 | void *payload; |
icraggs | 2:dcfdd2abfe71 | 49 | size_t payloadlen; |
sam_grove | 0:fe461e4d7afe | 50 | }; |
sam_grove | 0:fe461e4d7afe | 51 | |
icraggs | 4:4ef00243708e | 52 | |
icraggs | 20:cad3d54d7ecf | 53 | struct MessageData |
icraggs | 20:cad3d54d7ecf | 54 | { |
icraggs | 31:a51dd239b78e | 55 | MessageData(MQTTString &aTopicName, struct Message &aMessage) : message(aMessage), topicName(aTopicName) |
icraggs | 37:e3d64f9b986c | 56 | { } |
icraggs | 31:a51dd239b78e | 57 | |
icraggs | 31:a51dd239b78e | 58 | struct Message &message; |
icraggs | 31:a51dd239b78e | 59 | MQTTString &topicName; |
icraggs | 20:cad3d54d7ecf | 60 | }; |
icraggs | 20:cad3d54d7ecf | 61 | |
icraggs | 20:cad3d54d7ecf | 62 | |
icraggs | 9:01b8cc7d94cc | 63 | class PacketId |
icraggs | 9:01b8cc7d94cc | 64 | { |
icraggs | 9:01b8cc7d94cc | 65 | public: |
icraggs | 23:05fc7de97d4a | 66 | PacketId() |
icraggs | 23:05fc7de97d4a | 67 | { |
icraggs | 23:05fc7de97d4a | 68 | next = 0; |
icraggs | 23:05fc7de97d4a | 69 | } |
icraggs | 9:01b8cc7d94cc | 70 | |
icraggs | 23:05fc7de97d4a | 71 | int getNext() |
icraggs | 23:05fc7de97d4a | 72 | { |
icraggs | 23:05fc7de97d4a | 73 | return next = (next == MAX_PACKET_ID) ? 1 : ++next; |
icraggs | 23:05fc7de97d4a | 74 | } |
Ian Craggs |
12:cc7f2d62a393 | 75 | |
icraggs | 9:01b8cc7d94cc | 76 | private: |
icraggs | 9:01b8cc7d94cc | 77 | static const int MAX_PACKET_ID = 65535; |
icraggs | 9:01b8cc7d94cc | 78 | int next; |
icraggs | 9:01b8cc7d94cc | 79 | }; |
icraggs | 31:a51dd239b78e | 80 | |
icraggs | 31:a51dd239b78e | 81 | |
icraggs | 31:a51dd239b78e | 82 | class QoS2 |
icraggs | 31:a51dd239b78e | 83 | { |
icraggs | 31:a51dd239b78e | 84 | public: |
icraggs | 31:a51dd239b78e | 85 | |
icraggs | 31:a51dd239b78e | 86 | |
icraggs | 31:a51dd239b78e | 87 | private: |
icraggs | 31:a51dd239b78e | 88 | |
icraggs | 31:a51dd239b78e | 89 | |
icraggs | 31:a51dd239b78e | 90 | }; |
icraggs | 2:dcfdd2abfe71 | 91 | |
icraggs | 16:91c2f9a144d4 | 92 | |
icraggs | 21:e918525e529d | 93 | /** |
icraggs | 21:e918525e529d | 94 | * @class Client |
icraggs | 22:aadb79d29330 | 95 | * @brief blocking, non-threaded MQTT client API |
icraggs | 23:05fc7de97d4a | 96 | * |
icraggs | 23:05fc7de97d4a | 97 | * This version of the API blocks on all method calls, until they are complete. This means that only one |
icraggs | 23:05fc7de97d4a | 98 | * MQTT request can be in process at any one time. |
icraggs | 21:e918525e529d | 99 | * @param Network a network class which supports send, receive |
icraggs | 21:e918525e529d | 100 | * @param Timer a timer class with the methods: |
icraggs | 21:e918525e529d | 101 | */ |
icraggs | 31:a51dd239b78e | 102 | template<class Network, class Timer, int MAX_MQTT_PACKET_SIZE = 100, int MAX_MESSAGE_HANDLERS = 5> |
icraggs | 31:a51dd239b78e | 103 | class Client |
icraggs | 2:dcfdd2abfe71 | 104 | { |
icraggs | 2:dcfdd2abfe71 | 105 | |
icraggs | 22:aadb79d29330 | 106 | public: |
icraggs | 26:2658bb87c53d | 107 | |
icraggs | 31:a51dd239b78e | 108 | typedef void (*messageHandler)(MessageData&); |
icraggs | 23:05fc7de97d4a | 109 | |
icraggs | 23:05fc7de97d4a | 110 | /** Construct the client |
icraggs | 23:05fc7de97d4a | 111 | * @param network - pointer to an instance of the Network class - must be connected to the endpoint |
icraggs | 23:05fc7de97d4a | 112 | * before calling MQTT connect |
icraggs | 23:05fc7de97d4a | 113 | * @param limits an instance of the Limit class - to alter limits as required |
icraggs | 23:05fc7de97d4a | 114 | */ |
icraggs | 23:05fc7de97d4a | 115 | Client(Network& network, unsigned int command_timeout_ms = 30000); |
icraggs | 20:cad3d54d7ecf | 116 | |
icraggs | 20:cad3d54d7ecf | 117 | /** Set the default message handling callback - used for any message which does not match a subscription message handler |
icraggs | 20:cad3d54d7ecf | 118 | * @param mh - pointer to the callback function |
icraggs | 20:cad3d54d7ecf | 119 | */ |
icraggs | 20:cad3d54d7ecf | 120 | void setDefaultMessageHandler(messageHandler mh) |
icraggs | 20:cad3d54d7ecf | 121 | { |
icraggs | 20:cad3d54d7ecf | 122 | defaultMessageHandler.attach(mh); |
icraggs | 20:cad3d54d7ecf | 123 | } |
icraggs | 2:dcfdd2abfe71 | 124 | |
icraggs | 20:cad3d54d7ecf | 125 | /** MQTT Connect - send an MQTT connect packet down the network and wait for a Connack |
icraggs | 20:cad3d54d7ecf | 126 | * The nework object must be connected to the network endpoint before calling this |
icraggs | 20:cad3d54d7ecf | 127 | * @param options - connect options |
icraggs | 20:cad3d54d7ecf | 128 | * @return success code - |
icraggs | 20:cad3d54d7ecf | 129 | */ |
icraggs | 20:cad3d54d7ecf | 130 | int connect(MQTTPacket_connectData* options = 0); |
icraggs | 20:cad3d54d7ecf | 131 | |
icraggs | 20:cad3d54d7ecf | 132 | /** MQTT Publish - send an MQTT publish packet and wait for all acks to complete for all QoSs |
icraggs | 20:cad3d54d7ecf | 133 | * @param topic - the topic to publish to |
icraggs | 20:cad3d54d7ecf | 134 | * @param message - the message to send |
icraggs | 20:cad3d54d7ecf | 135 | * @return success code - |
icraggs | 20:cad3d54d7ecf | 136 | */ |
icraggs | 20:cad3d54d7ecf | 137 | int publish(const char* topicName, Message* message); |
icraggs | 20:cad3d54d7ecf | 138 | |
icraggs | 20:cad3d54d7ecf | 139 | /** MQTT Subscribe - send an MQTT subscribe packet and wait for the suback |
icraggs | 20:cad3d54d7ecf | 140 | * @param topicFilter - a topic pattern which can include wildcards |
icraggs | 20:cad3d54d7ecf | 141 | * @param qos - the MQTT QoS to subscribe at |
icraggs | 20:cad3d54d7ecf | 142 | * @param mh - the callback function to be invoked when a message is received for this subscription |
icraggs | 20:cad3d54d7ecf | 143 | * @return success code - |
icraggs | 20:cad3d54d7ecf | 144 | */ |
icraggs | 20:cad3d54d7ecf | 145 | int subscribe(const char* topicFilter, enum QoS qos, messageHandler mh); |
icraggs | 9:01b8cc7d94cc | 146 | |
icraggs | 20:cad3d54d7ecf | 147 | /** MQTT Unsubscribe - send an MQTT unsubscribe packet and wait for the unsuback |
icraggs | 20:cad3d54d7ecf | 148 | * @param topicFilter - a topic pattern which can include wildcards |
icraggs | 20:cad3d54d7ecf | 149 | * @return success code - |
icraggs | 20:cad3d54d7ecf | 150 | */ |
icraggs | 20:cad3d54d7ecf | 151 | int unsubscribe(const char* topicFilter); |
icraggs | 20:cad3d54d7ecf | 152 | |
icraggs | 31:a51dd239b78e | 153 | /** MQTT Disconnect - send an MQTT disconnect packet, and clean up any state |
icraggs | 20:cad3d54d7ecf | 154 | * @return success code - |
icraggs | 20:cad3d54d7ecf | 155 | */ |
icraggs | 20:cad3d54d7ecf | 156 | int disconnect(); |
icraggs | 8:c46930bd6c82 | 157 | |
icraggs | 20:cad3d54d7ecf | 158 | /** A call to this API must be made within the keepAlive interval to keep the MQTT connection alive |
icraggs | 20:cad3d54d7ecf | 159 | * yield can be called if no other MQTT operation is needed. This will also allow messages to be |
icraggs | 20:cad3d54d7ecf | 160 | * received. |
icraggs | 23:05fc7de97d4a | 161 | * @param timeout_ms the time to wait, in milliseconds |
icraggs | 26:2658bb87c53d | 162 | * @return success code - on failure, this means the client has disconnected |
icraggs | 20:cad3d54d7ecf | 163 | */ |
icraggs | 26:2658bb87c53d | 164 | int yield(int timeout_ms = 1000); |
icraggs | 20:cad3d54d7ecf | 165 | |
icraggs | 20:cad3d54d7ecf | 166 | private: |
icraggs | 20:cad3d54d7ecf | 167 | |
icraggs | 20:cad3d54d7ecf | 168 | int cycle(Timer& timer); |
icraggs | 20:cad3d54d7ecf | 169 | int waitfor(int packet_type, Timer& timer); |
icraggs | 20:cad3d54d7ecf | 170 | int keepalive(); |
icraggs | 2:dcfdd2abfe71 | 171 | |
icraggs | 3:dbff6b768d28 | 172 | int decodePacket(int* value, int timeout); |
icraggs | 20:cad3d54d7ecf | 173 | int readPacket(Timer& timer); |
icraggs | 20:cad3d54d7ecf | 174 | int sendPacket(int length, Timer& timer); |
icraggs | 26:2658bb87c53d | 175 | int deliverMessage(MQTTString& topicName, Message& message); |
icraggs | 26:2658bb87c53d | 176 | bool isTopicMatched(char* topicFilter, MQTTString& topicName); |
icraggs | 3:dbff6b768d28 | 177 | |
icraggs | 23:05fc7de97d4a | 178 | Network& ipstack; |
icraggs | 23:05fc7de97d4a | 179 | unsigned int command_timeout_ms; |
icraggs | 16:91c2f9a144d4 | 180 | |
icraggs | 36:2f1ada427e56 | 181 | unsigned char buf[MAX_MQTT_PACKET_SIZE]; |
icraggs | 36:2f1ada427e56 | 182 | unsigned char readbuf[MAX_MQTT_PACKET_SIZE]; |
icraggs | 15:64a57183aa03 | 183 | |
icraggs | 20:cad3d54d7ecf | 184 | Timer ping_timer; |
icraggs | 15:64a57183aa03 | 185 | unsigned int keepAliveInterval; |
icraggs | 20:cad3d54d7ecf | 186 | bool ping_outstanding; |
icraggs | 4:4ef00243708e | 187 | |
icraggs | 9:01b8cc7d94cc | 188 | PacketId packetid; |
icraggs | 9:01b8cc7d94cc | 189 | |
icraggs | 16:91c2f9a144d4 | 190 | struct MessageHandlers |
icraggs | 15:64a57183aa03 | 191 | { |
icraggs | 26:2658bb87c53d | 192 | const char* topicFilter; |
icraggs | 31:a51dd239b78e | 193 | FP<void, MessageData&> fp; |
icraggs | 23:05fc7de97d4a | 194 | } messageHandlers[MAX_MESSAGE_HANDLERS]; // Message handlers are indexed by subscription topic |
icraggs | 15:64a57183aa03 | 195 | |
icraggs | 31:a51dd239b78e | 196 | FP<void, MessageData&> defaultMessageHandler; |
icraggs | 28:8b2abe9bd814 | 197 | |
icraggs | 26:2658bb87c53d | 198 | bool isconnected; |
icraggs | 31:a51dd239b78e | 199 | |
icraggs | 31:a51dd239b78e | 200 | #if 0 |
icraggs | 31:a51dd239b78e | 201 | struct |
icraggs | 31:a51dd239b78e | 202 | { |
icraggs | 31:a51dd239b78e | 203 | bool used; |
icraggs | 31:a51dd239b78e | 204 | int id; |
icraggs | 31:a51dd239b78e | 205 | } QoS2messages[MAX_QOS2_MESSAGES]; |
icraggs | 31:a51dd239b78e | 206 | |
icraggs | 31:a51dd239b78e | 207 | #endif |
icraggs | 28:8b2abe9bd814 | 208 | |
sam_grove | 0:fe461e4d7afe | 209 | }; |
sam_grove | 0:fe461e4d7afe | 210 | |
icraggs | 15:64a57183aa03 | 211 | } |
icraggs | 15:64a57183aa03 | 212 | |
icraggs | 15:64a57183aa03 | 213 | |
icraggs | 23:05fc7de97d4a | 214 | template<class Network, class Timer, int a, int MAX_MESSAGE_HANDLERS> |
icraggs | 23:05fc7de97d4a | 215 | MQTT::Client<Network, Timer, a, MAX_MESSAGE_HANDLERS>::Client(Network& network, unsigned int command_timeout_ms) : ipstack(network), packetid() |
icraggs | 15:64a57183aa03 | 216 | { |
icraggs | 23:05fc7de97d4a | 217 | ping_timer = Timer(); |
icraggs | 23:05fc7de97d4a | 218 | ping_outstanding = 0; |
icraggs | 23:05fc7de97d4a | 219 | for (int i = 0; i < MAX_MESSAGE_HANDLERS; ++i) |
icraggs | 26:2658bb87c53d | 220 | messageHandlers[i].topicFilter = 0; |
icraggs | 26:2658bb87c53d | 221 | this->command_timeout_ms = command_timeout_ms; |
icraggs | 26:2658bb87c53d | 222 | isconnected = false; |
icraggs | 11:db15da110a37 | 223 | } |
icraggs | 11:db15da110a37 | 224 | |
icraggs | 11:db15da110a37 | 225 | |
icraggs | 23:05fc7de97d4a | 226 | template<class Network, class Timer, int a, int b> |
icraggs | 23:05fc7de97d4a | 227 | int MQTT::Client<Network, Timer, a, b>::sendPacket(int length, Timer& timer) |
icraggs | 8:c46930bd6c82 | 228 | { |
icraggs | 23:05fc7de97d4a | 229 | int rc = FAILURE, |
icraggs | 23:05fc7de97d4a | 230 | sent = 0; |
icraggs | 8:c46930bd6c82 | 231 | |
icraggs | 23:05fc7de97d4a | 232 | while (sent < length && !timer.expired()) |
icraggs | 23:05fc7de97d4a | 233 | { |
icraggs | 23:05fc7de97d4a | 234 | rc = ipstack.write(&buf[sent], length, timer.left_ms()); |
icraggs | 26:2658bb87c53d | 235 | if (rc < 0) // there was an error writing the data |
icraggs | 26:2658bb87c53d | 236 | break; |
icraggs | 26:2658bb87c53d | 237 | sent += rc; |
icraggs | 23:05fc7de97d4a | 238 | } |
icraggs | 20:cad3d54d7ecf | 239 | if (sent == length) |
icraggs | 23:05fc7de97d4a | 240 | { |
icraggs | 20:cad3d54d7ecf | 241 | ping_timer.countdown(this->keepAliveInterval); // record the fact that we have successfully sent the packet |
icraggs | 23:05fc7de97d4a | 242 | rc = SUCCESS; |
icraggs | 23:05fc7de97d4a | 243 | } |
icraggs | 23:05fc7de97d4a | 244 | else |
icraggs | 23:05fc7de97d4a | 245 | rc = FAILURE; |
icraggs | 23:05fc7de97d4a | 246 | return rc; |
icraggs | 8:c46930bd6c82 | 247 | } |
icraggs | 8:c46930bd6c82 | 248 | |
icraggs | 8:c46930bd6c82 | 249 | |
icraggs | 26:2658bb87c53d | 250 | template<class Network, class Timer, int a, int b> |
icraggs | 26:2658bb87c53d | 251 | int MQTT::Client<Network, Timer, a, b>::decodePacket(int* value, int timeout) |
icraggs | 8:c46930bd6c82 | 252 | { |
icraggs | 36:2f1ada427e56 | 253 | unsigned char c; |
icraggs | 8:c46930bd6c82 | 254 | int multiplier = 1; |
icraggs | 8:c46930bd6c82 | 255 | int len = 0; |
icraggs | 20:cad3d54d7ecf | 256 | const int MAX_NO_OF_REMAINING_LENGTH_BYTES = 4; |
icraggs | 8:c46930bd6c82 | 257 | |
icraggs | 8:c46930bd6c82 | 258 | *value = 0; |
icraggs | 8:c46930bd6c82 | 259 | do |
icraggs | 8:c46930bd6c82 | 260 | { |
icraggs | 8:c46930bd6c82 | 261 | int rc = MQTTPACKET_READ_ERROR; |
icraggs | 8:c46930bd6c82 | 262 | |
icraggs | 8:c46930bd6c82 | 263 | if (++len > MAX_NO_OF_REMAINING_LENGTH_BYTES) |
icraggs | 8:c46930bd6c82 | 264 | { |
icraggs | 8:c46930bd6c82 | 265 | rc = MQTTPACKET_READ_ERROR; /* bad data */ |
icraggs | 8:c46930bd6c82 | 266 | goto exit; |
icraggs | 8:c46930bd6c82 | 267 | } |
icraggs | 23:05fc7de97d4a | 268 | rc = ipstack.read(&c, 1, timeout); |
icraggs | 8:c46930bd6c82 | 269 | if (rc != 1) |
icraggs | 8:c46930bd6c82 | 270 | goto exit; |
icraggs | 8:c46930bd6c82 | 271 | *value += (c & 127) * multiplier; |
icraggs | 8:c46930bd6c82 | 272 | multiplier *= 128; |
icraggs | 8:c46930bd6c82 | 273 | } while ((c & 128) != 0); |
icraggs | 8:c46930bd6c82 | 274 | exit: |
icraggs | 8:c46930bd6c82 | 275 | return len; |
icraggs | 8:c46930bd6c82 | 276 | } |
icraggs | 8:c46930bd6c82 | 277 | |
icraggs | 8:c46930bd6c82 | 278 | |
icraggs | 8:c46930bd6c82 | 279 | /** |
icraggs | 8:c46930bd6c82 | 280 | * If any read fails in this method, then we should disconnect from the network, as on reconnect |
icraggs | 8:c46930bd6c82 | 281 | * the packets can be retried. |
icraggs | 8:c46930bd6c82 | 282 | * @param timeout the max time to wait for the packet read to complete, in milliseconds |
icraggs | 8:c46930bd6c82 | 283 | * @return the MQTT packet type, or -1 if none |
icraggs | 8:c46930bd6c82 | 284 | */ |
icraggs | 23:05fc7de97d4a | 285 | template<class Network, class Timer, int a, int b> |
icraggs | 23:05fc7de97d4a | 286 | int MQTT::Client<Network, Timer, a, b>::readPacket(Timer& timer) |
icraggs | 8:c46930bd6c82 | 287 | { |
icraggs | 26:2658bb87c53d | 288 | int rc = FAILURE; |
icraggs | 8:c46930bd6c82 | 289 | MQTTHeader header = {0}; |
icraggs | 8:c46930bd6c82 | 290 | int len = 0; |
icraggs | 8:c46930bd6c82 | 291 | int rem_len = 0; |
icraggs | 8:c46930bd6c82 | 292 | |
icraggs | 8:c46930bd6c82 | 293 | /* 1. read the header byte. This has the packet type in it */ |
icraggs | 23:05fc7de97d4a | 294 | if (ipstack.read(readbuf, 1, timer.left_ms()) != 1) |
icraggs | 8:c46930bd6c82 | 295 | goto exit; |
icraggs | 8:c46930bd6c82 | 296 | |
icraggs | 8:c46930bd6c82 | 297 | len = 1; |
icraggs | 8:c46930bd6c82 | 298 | /* 2. read the remaining length. This is variable in itself */ |
icraggs | 20:cad3d54d7ecf | 299 | decodePacket(&rem_len, timer.left_ms()); |
icraggs | 8:c46930bd6c82 | 300 | len += MQTTPacket_encode(readbuf + 1, rem_len); /* put the original remaining length back into the buffer */ |
icraggs | 8:c46930bd6c82 | 301 | |
icraggs | 8:c46930bd6c82 | 302 | /* 3. read the rest of the buffer using a callback to supply the rest of the data */ |
icraggs | 23:05fc7de97d4a | 303 | if (ipstack.read(readbuf + len, rem_len, timer.left_ms()) != rem_len) |
icraggs | 8:c46930bd6c82 | 304 | goto exit; |
icraggs | 8:c46930bd6c82 | 305 | |
icraggs | 8:c46930bd6c82 | 306 | header.byte = readbuf[0]; |
icraggs | 8:c46930bd6c82 | 307 | rc = header.bits.type; |
icraggs | 8:c46930bd6c82 | 308 | exit: |
icraggs | 8:c46930bd6c82 | 309 | return rc; |
icraggs | 3:dbff6b768d28 | 310 | } |
icraggs | 3:dbff6b768d28 | 311 | |
icraggs | 8:c46930bd6c82 | 312 | |
icraggs | 26:2658bb87c53d | 313 | // assume topic filter and name is in correct format |
icraggs | 26:2658bb87c53d | 314 | // # can only be at end |
icraggs | 26:2658bb87c53d | 315 | // + and # can only be next to separator |
icraggs | 26:2658bb87c53d | 316 | template<class Network, class Timer, int a, int b> |
icraggs | 26:2658bb87c53d | 317 | bool MQTT::Client<Network, Timer, a, b>::isTopicMatched(char* topicFilter, MQTTString& topicName) |
icraggs | 26:2658bb87c53d | 318 | { |
icraggs | 26:2658bb87c53d | 319 | char* curf = topicFilter; |
icraggs | 26:2658bb87c53d | 320 | char* curn = topicName.lenstring.data; |
icraggs | 26:2658bb87c53d | 321 | char* curn_end = curn + topicName.lenstring.len; |
icraggs | 26:2658bb87c53d | 322 | |
icraggs | 26:2658bb87c53d | 323 | while (*curf && curn < curn_end) |
icraggs | 26:2658bb87c53d | 324 | { |
icraggs | 26:2658bb87c53d | 325 | if (*curn == '/' && *curf != '/') |
icraggs | 26:2658bb87c53d | 326 | break; |
icraggs | 26:2658bb87c53d | 327 | if (*curf != '+' && *curf != '#' && *curf != *curn) |
icraggs | 26:2658bb87c53d | 328 | break; |
icraggs | 26:2658bb87c53d | 329 | if (*curf == '+') |
icraggs | 26:2658bb87c53d | 330 | { // skip until we meet the next separator, or end of string |
icraggs | 26:2658bb87c53d | 331 | char* nextpos = curn + 1; |
icraggs | 26:2658bb87c53d | 332 | while (nextpos < curn_end && *nextpos != '/') |
icraggs | 26:2658bb87c53d | 333 | nextpos = ++curn + 1; |
icraggs | 26:2658bb87c53d | 334 | } |
icraggs | 26:2658bb87c53d | 335 | else if (*curf == '#') |
icraggs | 26:2658bb87c53d | 336 | curn = curn_end - 1; // skip until end of string |
icraggs | 26:2658bb87c53d | 337 | curf++; |
icraggs | 26:2658bb87c53d | 338 | curn++; |
icraggs | 26:2658bb87c53d | 339 | }; |
icraggs | 26:2658bb87c53d | 340 | |
icraggs | 26:2658bb87c53d | 341 | return (curn == curn_end) && (*curf == '\0'); |
icraggs | 26:2658bb87c53d | 342 | } |
icraggs | 26:2658bb87c53d | 343 | |
icraggs | 26:2658bb87c53d | 344 | |
icraggs | 26:2658bb87c53d | 345 | |
icraggs | 23:05fc7de97d4a | 346 | template<class Network, class Timer, int a, int MAX_MESSAGE_HANDLERS> |
icraggs | 26:2658bb87c53d | 347 | int MQTT::Client<Network, Timer, a, MAX_MESSAGE_HANDLERS>::deliverMessage(MQTTString& topicName, Message& message) |
icraggs | 15:64a57183aa03 | 348 | { |
icraggs | 26:2658bb87c53d | 349 | int rc = FAILURE; |
icraggs | 20:cad3d54d7ecf | 350 | |
icraggs | 20:cad3d54d7ecf | 351 | // we have to find the right message handler - indexed by topic |
icraggs | 23:05fc7de97d4a | 352 | for (int i = 0; i < MAX_MESSAGE_HANDLERS; ++i) |
icraggs | 20:cad3d54d7ecf | 353 | { |
icraggs | 26:2658bb87c53d | 354 | if (messageHandlers[i].topicFilter != 0 && (MQTTPacket_equals(&topicName, (char*)messageHandlers[i].topicFilter) || |
icraggs | 26:2658bb87c53d | 355 | isTopicMatched((char*)messageHandlers[i].topicFilter, topicName))) |
icraggs | 20:cad3d54d7ecf | 356 | { |
icraggs | 26:2658bb87c53d | 357 | if (messageHandlers[i].fp.attached()) |
icraggs | 26:2658bb87c53d | 358 | { |
icraggs | 31:a51dd239b78e | 359 | MessageData md(topicName, message); |
icraggs | 31:a51dd239b78e | 360 | messageHandlers[i].fp(md); |
icraggs | 26:2658bb87c53d | 361 | rc = SUCCESS; |
icraggs | 26:2658bb87c53d | 362 | } |
icraggs | 20:cad3d54d7ecf | 363 | } |
icraggs | 20:cad3d54d7ecf | 364 | } |
icraggs | 26:2658bb87c53d | 365 | |
icraggs | 26:2658bb87c53d | 366 | if (rc == FAILURE && defaultMessageHandler.attached()) |
icraggs | 26:2658bb87c53d | 367 | { |
icraggs | 31:a51dd239b78e | 368 | MessageData md(topicName, message); |
icraggs | 31:a51dd239b78e | 369 | defaultMessageHandler(md); |
icraggs | 26:2658bb87c53d | 370 | rc = SUCCESS; |
icraggs | 26:2658bb87c53d | 371 | } |
icraggs | 20:cad3d54d7ecf | 372 | |
icraggs | 20:cad3d54d7ecf | 373 | return rc; |
icraggs | 15:64a57183aa03 | 374 | } |
icraggs | 15:64a57183aa03 | 375 | |
icraggs | 15:64a57183aa03 | 376 | |
icraggs | 20:cad3d54d7ecf | 377 | |
icraggs | 23:05fc7de97d4a | 378 | template<class Network, class Timer, int a, int b> |
icraggs | 26:2658bb87c53d | 379 | int MQTT::Client<Network, Timer, a, b>::yield(int timeout_ms) |
icraggs | 20:cad3d54d7ecf | 380 | { |
icraggs | 26:2658bb87c53d | 381 | int rc = SUCCESS; |
icraggs | 20:cad3d54d7ecf | 382 | Timer timer = Timer(); |
icraggs | 20:cad3d54d7ecf | 383 | |
icraggs | 23:05fc7de97d4a | 384 | timer.countdown_ms(timeout_ms); |
icraggs | 20:cad3d54d7ecf | 385 | while (!timer.expired()) |
icraggs | 26:2658bb87c53d | 386 | { |
icraggs | 26:2658bb87c53d | 387 | if (cycle(timer) == FAILURE) |
icraggs | 26:2658bb87c53d | 388 | { |
icraggs | 26:2658bb87c53d | 389 | rc = FAILURE; |
icraggs | 26:2658bb87c53d | 390 | break; |
icraggs | 26:2658bb87c53d | 391 | } |
icraggs | 26:2658bb87c53d | 392 | } |
icraggs | 26:2658bb87c53d | 393 | |
icraggs | 26:2658bb87c53d | 394 | return rc; |
icraggs | 20:cad3d54d7ecf | 395 | } |
icraggs | 20:cad3d54d7ecf | 396 | |
icraggs | 20:cad3d54d7ecf | 397 | |
icraggs | 23:05fc7de97d4a | 398 | template<class Network, class Timer, int MAX_MQTT_PACKET_SIZE, int b> |
icraggs | 23:05fc7de97d4a | 399 | int MQTT::Client<Network, Timer, MAX_MQTT_PACKET_SIZE, b>::cycle(Timer& timer) |
icraggs | 8:c46930bd6c82 | 400 | { |
icraggs | 8:c46930bd6c82 | 401 | /* get one piece of work off the wire and one pass through */ |
icraggs | 20:cad3d54d7ecf | 402 | |
Ian Craggs |
12:cc7f2d62a393 | 403 | // read the socket, see what work is due |
icraggs | 36:2f1ada427e56 | 404 | unsigned short packet_type = readPacket(timer); |
icraggs | 15:64a57183aa03 | 405 | |
icraggs | 26:2658bb87c53d | 406 | int len = 0, |
icraggs | 26:2658bb87c53d | 407 | rc = SUCCESS; |
icraggs | 28:8b2abe9bd814 | 408 | |
icraggs | 8:c46930bd6c82 | 409 | switch (packet_type) |
icraggs | 8:c46930bd6c82 | 410 | { |
icraggs | 15:64a57183aa03 | 411 | case CONNACK: |
icraggs | 8:c46930bd6c82 | 412 | case PUBACK: |
icraggs | 8:c46930bd6c82 | 413 | case SUBACK: |
icraggs | 8:c46930bd6c82 | 414 | break; |
icraggs | 15:64a57183aa03 | 415 | case PUBLISH: |
icraggs | 20:cad3d54d7ecf | 416 | MQTTString topicName; |
icraggs | 20:cad3d54d7ecf | 417 | Message msg; |
icraggs | 36:2f1ada427e56 | 418 | if (MQTTDeserialize_publish((unsigned char*)&msg.dup, (int*)&msg.qos, (unsigned char*)&msg.retained, (unsigned short*)&msg.id, &topicName, |
icraggs | 36:2f1ada427e56 | 419 | (unsigned char**)&msg.payload, (int*)&msg.payloadlen, readbuf, MAX_MQTT_PACKET_SIZE) != 1) |
icraggs | 26:2658bb87c53d | 420 | goto exit; |
icraggs | 31:a51dd239b78e | 421 | // if (msg.qos != QOS2) |
icraggs | 31:a51dd239b78e | 422 | deliverMessage(topicName, msg); |
icraggs | 31:a51dd239b78e | 423 | #if 0 |
icraggs | 31:a51dd239b78e | 424 | else if (isQoS2msgidFree(msg.id)) |
icraggs | 31:a51dd239b78e | 425 | { |
icraggs | 31:a51dd239b78e | 426 | UseQoS2msgid(msg.id); |
icraggs | 31:a51dd239b78e | 427 | deliverMessage(topicName, msg); |
icraggs | 31:a51dd239b78e | 428 | } |
icraggs | 31:a51dd239b78e | 429 | #endif |
icraggs | 20:cad3d54d7ecf | 430 | if (msg.qos != QOS0) |
icraggs | 20:cad3d54d7ecf | 431 | { |
icraggs | 20:cad3d54d7ecf | 432 | if (msg.qos == QOS1) |
icraggs | 23:05fc7de97d4a | 433 | len = MQTTSerialize_ack(buf, MAX_MQTT_PACKET_SIZE, PUBACK, 0, msg.id); |
icraggs | 20:cad3d54d7ecf | 434 | else if (msg.qos == QOS2) |
icraggs | 23:05fc7de97d4a | 435 | len = MQTTSerialize_ack(buf, MAX_MQTT_PACKET_SIZE, PUBREC, 0, msg.id); |
icraggs | 26:2658bb87c53d | 436 | if (len <= 0) |
icraggs | 26:2658bb87c53d | 437 | rc = FAILURE; |
icraggs | 26:2658bb87c53d | 438 | else |
icraggs | 26:2658bb87c53d | 439 | rc = sendPacket(len, timer); |
icraggs | 26:2658bb87c53d | 440 | if (rc == FAILURE) |
icraggs | 20:cad3d54d7ecf | 441 | goto exit; // there was a problem |
icraggs | 20:cad3d54d7ecf | 442 | } |
Ian Craggs |
12:cc7f2d62a393 | 443 | break; |
icraggs | 15:64a57183aa03 | 444 | case PUBREC: |
icraggs | 36:2f1ada427e56 | 445 | unsigned short mypacketid; |
icraggs | 36:2f1ada427e56 | 446 | unsigned char dup, type; |
icraggs | 26:2658bb87c53d | 447 | if (MQTTDeserialize_ack(&type, &dup, &mypacketid, readbuf, MAX_MQTT_PACKET_SIZE) != 1) |
icraggs | 26:2658bb87c53d | 448 | rc = FAILURE; |
icraggs | 26:2658bb87c53d | 449 | else if ((len = MQTTSerialize_ack(buf, MAX_MQTT_PACKET_SIZE, PUBREL, 0, mypacketid)) <= 0) |
icraggs | 26:2658bb87c53d | 450 | rc = FAILURE; |
icraggs | 26:2658bb87c53d | 451 | else if ((rc = sendPacket(len, timer)) != SUCCESS) // send the PUBREL packet |
icraggs | 26:2658bb87c53d | 452 | rc = FAILURE; // there was a problem |
icraggs | 26:2658bb87c53d | 453 | if (rc == FAILURE) |
icraggs | 20:cad3d54d7ecf | 454 | goto exit; // there was a problem |
icraggs | 8:c46930bd6c82 | 455 | break; |
icraggs | 8:c46930bd6c82 | 456 | case PUBCOMP: |
icraggs | 8:c46930bd6c82 | 457 | break; |
icraggs | 15:64a57183aa03 | 458 | case PINGRESP: |
icraggs | 20:cad3d54d7ecf | 459 | ping_outstanding = false; |
icraggs | 8:c46930bd6c82 | 460 | break; |
icraggs | 15:64a57183aa03 | 461 | } |
icraggs | 20:cad3d54d7ecf | 462 | keepalive(); |
Ian Craggs |
12:cc7f2d62a393 | 463 | exit: |
icraggs | 26:2658bb87c53d | 464 | if (rc == SUCCESS) |
icraggs | 26:2658bb87c53d | 465 | rc = packet_type; |
icraggs | 26:2658bb87c53d | 466 | return rc; |
icraggs | 15:64a57183aa03 | 467 | } |
icraggs | 15:64a57183aa03 | 468 | |
icraggs | 15:64a57183aa03 | 469 | |
icraggs | 23:05fc7de97d4a | 470 | template<class Network, class Timer, int MAX_MQTT_PACKET_SIZE, int b> |
icraggs | 23:05fc7de97d4a | 471 | int MQTT::Client<Network, Timer, MAX_MQTT_PACKET_SIZE, b>::keepalive() |
icraggs | 15:64a57183aa03 | 472 | { |
icraggs | 26:2658bb87c53d | 473 | int rc = FAILURE; |
icraggs | 15:64a57183aa03 | 474 | |
icraggs | 20:cad3d54d7ecf | 475 | if (keepAliveInterval == 0) |
icraggs | 26:2658bb87c53d | 476 | { |
icraggs | 26:2658bb87c53d | 477 | rc = SUCCESS; |
icraggs | 20:cad3d54d7ecf | 478 | goto exit; |
icraggs | 26:2658bb87c53d | 479 | } |
icraggs | 15:64a57183aa03 | 480 | |
icraggs | 20:cad3d54d7ecf | 481 | if (ping_timer.expired()) |
icraggs | 20:cad3d54d7ecf | 482 | { |
icraggs | 26:2658bb87c53d | 483 | if (!ping_outstanding) |
icraggs | 20:cad3d54d7ecf | 484 | { |
icraggs | 20:cad3d54d7ecf | 485 | Timer timer = Timer(1000); |
icraggs | 23:05fc7de97d4a | 486 | int len = MQTTSerialize_pingreq(buf, MAX_MQTT_PACKET_SIZE); |
icraggs | 26:2658bb87c53d | 487 | if (len > 0 && (rc = sendPacket(len, timer)) == SUCCESS) // send the ping packet |
icraggs | 20:cad3d54d7ecf | 488 | ping_outstanding = true; |
icraggs | 20:cad3d54d7ecf | 489 | } |
icraggs | 20:cad3d54d7ecf | 490 | } |
icraggs | 15:64a57183aa03 | 491 | |
icraggs | 15:64a57183aa03 | 492 | exit: |
icraggs | 20:cad3d54d7ecf | 493 | return rc; |
icraggs | 8:c46930bd6c82 | 494 | } |
icraggs | 8:c46930bd6c82 | 495 | |
icraggs | 8:c46930bd6c82 | 496 | |
icraggs | 16:91c2f9a144d4 | 497 | // only used in single-threaded mode where one command at a time is in process |
icraggs | 23:05fc7de97d4a | 498 | template<class Network, class Timer, int a, int b> |
icraggs | 23:05fc7de97d4a | 499 | int MQTT::Client<Network, Timer, a, b>::waitfor(int packet_type, Timer& timer) |
icraggs | 15:64a57183aa03 | 500 | { |
icraggs | 26:2658bb87c53d | 501 | int rc = FAILURE; |
icraggs | 20:cad3d54d7ecf | 502 | |
icraggs | 20:cad3d54d7ecf | 503 | do |
icraggs | 16:91c2f9a144d4 | 504 | { |
icraggs | 20:cad3d54d7ecf | 505 | if (timer.expired()) |
icraggs | 20:cad3d54d7ecf | 506 | break; // we timed out |
icraggs | 20:cad3d54d7ecf | 507 | } |
icraggs | 20:cad3d54d7ecf | 508 | while ((rc = cycle(timer)) != packet_type); |
icraggs | 20:cad3d54d7ecf | 509 | |
icraggs | 20:cad3d54d7ecf | 510 | return rc; |
icraggs | 16:91c2f9a144d4 | 511 | } |
icraggs | 16:91c2f9a144d4 | 512 | |
icraggs | 16:91c2f9a144d4 | 513 | |
icraggs | 23:05fc7de97d4a | 514 | template<class Network, class Timer, int MAX_MQTT_PACKET_SIZE, int b> |
icraggs | 23:05fc7de97d4a | 515 | int MQTT::Client<Network, Timer, MAX_MQTT_PACKET_SIZE, b>::connect(MQTTPacket_connectData* options) |
icraggs | 16:91c2f9a144d4 | 516 | { |
icraggs | 23:05fc7de97d4a | 517 | Timer connect_timer = Timer(command_timeout_ms); |
icraggs | 26:2658bb87c53d | 518 | int rc = FAILURE; |
icraggs | 31:a51dd239b78e | 519 | MQTTPacket_connectData default_options = MQTTPacket_connectData_initializer; |
icraggs | 31:a51dd239b78e | 520 | int len = 0; |
icraggs | 31:a51dd239b78e | 521 | |
icraggs | 31:a51dd239b78e | 522 | if (isconnected) // don't send connect packet again if we are already connected |
icraggs | 31:a51dd239b78e | 523 | goto exit; |
icraggs | 8:c46930bd6c82 | 524 | |
icraggs | 8:c46930bd6c82 | 525 | if (options == 0) |
Ian Craggs |
12:cc7f2d62a393 | 526 | options = &default_options; // set default options if none were supplied |
icraggs | 8:c46930bd6c82 | 527 | |
icraggs | 15:64a57183aa03 | 528 | this->keepAliveInterval = options->keepAliveInterval; |
icraggs | 20:cad3d54d7ecf | 529 | ping_timer.countdown(this->keepAliveInterval); |
icraggs | 31:a51dd239b78e | 530 | if ((len = MQTTSerialize_connect(buf, MAX_MQTT_PACKET_SIZE, options)) <= 0) |
icraggs | 26:2658bb87c53d | 531 | goto exit; |
icraggs | 26:2658bb87c53d | 532 | if ((rc = sendPacket(len, connect_timer)) != SUCCESS) // send the connect packet |
icraggs | 20:cad3d54d7ecf | 533 | goto exit; // there was a problem |
icraggs | 8:c46930bd6c82 | 534 | |
icraggs | 20:cad3d54d7ecf | 535 | // this will be a blocking call, wait for the connack |
icraggs | 20:cad3d54d7ecf | 536 | if (waitfor(CONNACK, connect_timer) == CONNACK) |
icraggs | 15:64a57183aa03 | 537 | { |
icraggs | 36:2f1ada427e56 | 538 | unsigned char connack_rc = 255; |
icraggs | 37:e3d64f9b986c | 539 | bool sessionPresent = false; |
icraggs | 37:e3d64f9b986c | 540 | if (MQTTDeserialize_connack((unsigned char*)&sessionPresent, &connack_rc, readbuf, MAX_MQTT_PACKET_SIZE) == 1) |
icraggs | 20:cad3d54d7ecf | 541 | rc = connack_rc; |
icraggs | 26:2658bb87c53d | 542 | else |
icraggs | 26:2658bb87c53d | 543 | rc = FAILURE; |
icraggs | 8:c46930bd6c82 | 544 | } |
icraggs | 26:2658bb87c53d | 545 | else |
icraggs | 26:2658bb87c53d | 546 | rc = FAILURE; |
icraggs | 15:64a57183aa03 | 547 | |
icraggs | 15:64a57183aa03 | 548 | exit: |
icraggs | 26:2658bb87c53d | 549 | if (rc == SUCCESS) |
icraggs | 26:2658bb87c53d | 550 | isconnected = true; |
icraggs | 8:c46930bd6c82 | 551 | return rc; |
icraggs | 8:c46930bd6c82 | 552 | } |
icraggs | 8:c46930bd6c82 | 553 | |
icraggs | 8:c46930bd6c82 | 554 | |
icraggs | 23:05fc7de97d4a | 555 | template<class Network, class Timer, int MAX_MQTT_PACKET_SIZE, int MAX_MESSAGE_HANDLERS> |
icraggs | 23:05fc7de97d4a | 556 | int MQTT::Client<Network, Timer, MAX_MQTT_PACKET_SIZE, MAX_MESSAGE_HANDLERS>::subscribe(const char* topicFilter, enum QoS qos, messageHandler messageHandler) |
icraggs | 20:cad3d54d7ecf | 557 | { |
icraggs | 31:a51dd239b78e | 558 | int rc = FAILURE; |
icraggs | 23:05fc7de97d4a | 559 | Timer timer = Timer(command_timeout_ms); |
icraggs | 26:2658bb87c53d | 560 | int len = 0; |
icraggs | 31:a51dd239b78e | 561 | MQTTString topic = {(char*)topicFilter, 0, 0}; |
icraggs | 20:cad3d54d7ecf | 562 | |
icraggs | 26:2658bb87c53d | 563 | if (!isconnected) |
icraggs | 20:cad3d54d7ecf | 564 | goto exit; |
icraggs | 26:2658bb87c53d | 565 | |
icraggs | 26:2658bb87c53d | 566 | len = MQTTSerialize_subscribe(buf, MAX_MQTT_PACKET_SIZE, 0, packetid.getNext(), 1, &topic, (int*)&qos); |
icraggs | 26:2658bb87c53d | 567 | if (len <= 0) |
icraggs | 26:2658bb87c53d | 568 | goto exit; |
icraggs | 23:05fc7de97d4a | 569 | if ((rc = sendPacket(len, timer)) != SUCCESS) // send the subscribe packet |
icraggs | 26:2658bb87c53d | 570 | goto exit; // there was a problem |
icraggs | 8:c46930bd6c82 | 571 | |
icraggs | 20:cad3d54d7ecf | 572 | if (waitfor(SUBACK, timer) == SUBACK) // wait for suback |
icraggs | 8:c46930bd6c82 | 573 | { |
icraggs | 36:2f1ada427e56 | 574 | int count = 0, grantedQoS = -1; |
icraggs | 36:2f1ada427e56 | 575 | unsigned short mypacketid; |
icraggs | 23:05fc7de97d4a | 576 | if (MQTTDeserialize_suback(&mypacketid, 1, &count, &grantedQoS, readbuf, MAX_MQTT_PACKET_SIZE) == 1) |
icraggs | 20:cad3d54d7ecf | 577 | rc = grantedQoS; // 0, 1, 2 or 0x80 |
icraggs | 20:cad3d54d7ecf | 578 | if (rc != 0x80) |
icraggs | 8:c46930bd6c82 | 579 | { |
icraggs | 23:05fc7de97d4a | 580 | for (int i = 0; i < MAX_MESSAGE_HANDLERS; ++i) |
icraggs | 16:91c2f9a144d4 | 581 | { |
icraggs | 26:2658bb87c53d | 582 | if (messageHandlers[i].topicFilter == 0) |
icraggs | 20:cad3d54d7ecf | 583 | { |
icraggs | 26:2658bb87c53d | 584 | messageHandlers[i].topicFilter = topicFilter; |
icraggs | 20:cad3d54d7ecf | 585 | messageHandlers[i].fp.attach(messageHandler); |
icraggs | 20:cad3d54d7ecf | 586 | rc = 0; |
icraggs | 20:cad3d54d7ecf | 587 | break; |
icraggs | 20:cad3d54d7ecf | 588 | } |
icraggs | 16:91c2f9a144d4 | 589 | } |
icraggs | 8:c46930bd6c82 | 590 | } |
icraggs | 8:c46930bd6c82 | 591 | } |
icraggs | 26:2658bb87c53d | 592 | else |
icraggs | 26:2658bb87c53d | 593 | rc = FAILURE; |
icraggs | 26:2658bb87c53d | 594 | |
icraggs | 15:64a57183aa03 | 595 | exit: |
Ian Craggs |
12:cc7f2d62a393 | 596 | return rc; |
icraggs | 15:64a57183aa03 | 597 | } |
icraggs | 15:64a57183aa03 | 598 | |
icraggs | 15:64a57183aa03 | 599 | |
icraggs | 23:05fc7de97d4a | 600 | template<class Network, class Timer, int MAX_MQTT_PACKET_SIZE, int MAX_MESSAGE_HANDLERS> |
icraggs | 23:05fc7de97d4a | 601 | int MQTT::Client<Network, Timer, MAX_MQTT_PACKET_SIZE, MAX_MESSAGE_HANDLERS>::unsubscribe(const char* topicFilter) |
icraggs | 20:cad3d54d7ecf | 602 | { |
icraggs | 26:2658bb87c53d | 603 | int rc = FAILURE; |
icraggs | 31:a51dd239b78e | 604 | Timer timer = Timer(command_timeout_ms); |
Ian Craggs |
12:cc7f2d62a393 | 605 | MQTTString topic = {(char*)topicFilter, 0, 0}; |
icraggs | 31:a51dd239b78e | 606 | int len = 0; |
icraggs | 8:c46930bd6c82 | 607 | |
icraggs | 31:a51dd239b78e | 608 | if (!isconnected) |
icraggs | 31:a51dd239b78e | 609 | goto exit; |
icraggs | 31:a51dd239b78e | 610 | |
icraggs | 31:a51dd239b78e | 611 | if ((len = MQTTSerialize_unsubscribe(buf, MAX_MQTT_PACKET_SIZE, 0, packetid.getNext(), 1, &topic)) <= 0) |
icraggs | 20:cad3d54d7ecf | 612 | goto exit; |
icraggs | 23:05fc7de97d4a | 613 | if ((rc = sendPacket(len, timer)) != SUCCESS) // send the subscribe packet |
icraggs | 20:cad3d54d7ecf | 614 | goto exit; // there was a problem |
Ian Craggs |
12:cc7f2d62a393 | 615 | |
icraggs | 20:cad3d54d7ecf | 616 | if (waitfor(UNSUBACK, timer) == UNSUBACK) |
Ian Craggs |
12:cc7f2d62a393 | 617 | { |
icraggs | 36:2f1ada427e56 | 618 | unsigned short mypacketid; // should be the same as the packetid above |
icraggs | 23:05fc7de97d4a | 619 | if (MQTTDeserialize_unsuback(&mypacketid, readbuf, MAX_MQTT_PACKET_SIZE) == 1) |
icraggs | 20:cad3d54d7ecf | 620 | rc = 0; |
Ian Craggs |
12:cc7f2d62a393 | 621 | } |
icraggs | 26:2658bb87c53d | 622 | else |
icraggs | 26:2658bb87c53d | 623 | rc = FAILURE; |
icraggs | 15:64a57183aa03 | 624 | |
icraggs | 15:64a57183aa03 | 625 | exit: |
Ian Craggs |
12:cc7f2d62a393 | 626 | return rc; |
icraggs | 15:64a57183aa03 | 627 | } |
icraggs | 15:64a57183aa03 | 628 | |
icraggs | 15:64a57183aa03 | 629 | |
icraggs | 15:64a57183aa03 | 630 | |
icraggs | 23:05fc7de97d4a | 631 | template<class Network, class Timer, int MAX_MQTT_PACKET_SIZE, int b> |
icraggs | 23:05fc7de97d4a | 632 | int MQTT::Client<Network, Timer, MAX_MQTT_PACKET_SIZE, b>::publish(const char* topicName, Message* message) |
icraggs | 20:cad3d54d7ecf | 633 | { |
icraggs | 26:2658bb87c53d | 634 | int rc = FAILURE; |
icraggs | 31:a51dd239b78e | 635 | Timer timer = Timer(command_timeout_ms); |
icraggs | 31:a51dd239b78e | 636 | MQTTString topicString = {(char*)topicName, 0, 0}; |
icraggs | 31:a51dd239b78e | 637 | int len = 0; |
icraggs | 20:cad3d54d7ecf | 638 | |
icraggs | 31:a51dd239b78e | 639 | if (!isconnected) |
icraggs | 31:a51dd239b78e | 640 | goto exit; |
icraggs | 15:64a57183aa03 | 641 | |
icraggs | 20:cad3d54d7ecf | 642 | if (message->qos == QOS1 || message->qos == QOS2) |
icraggs | 20:cad3d54d7ecf | 643 | message->id = packetid.getNext(); |
icraggs | 15:64a57183aa03 | 644 | |
icraggs | 31:a51dd239b78e | 645 | len = MQTTSerialize_publish(buf, MAX_MQTT_PACKET_SIZE, 0, message->qos, message->retained, message->id, |
icraggs | 36:2f1ada427e56 | 646 | topicString, (unsigned char*)message->payload, message->payloadlen); |
icraggs | 26:2658bb87c53d | 647 | if (len <= 0) |
icraggs | 26:2658bb87c53d | 648 | goto exit; |
icraggs | 26:2658bb87c53d | 649 | if ((rc = sendPacket(len, timer)) != SUCCESS) // send the subscribe packet |
icraggs | 20:cad3d54d7ecf | 650 | goto exit; // there was a problem |
Ian Craggs |
12:cc7f2d62a393 | 651 | |
icraggs | 20:cad3d54d7ecf | 652 | if (message->qos == QOS1) |
Ian Craggs |
12:cc7f2d62a393 | 653 | { |
icraggs | 20:cad3d54d7ecf | 654 | if (waitfor(PUBACK, timer) == PUBACK) |
icraggs | 20:cad3d54d7ecf | 655 | { |
icraggs | 36:2f1ada427e56 | 656 | unsigned short mypacketid; |
icraggs | 36:2f1ada427e56 | 657 | unsigned char dup, type; |
icraggs | 26:2658bb87c53d | 658 | if (MQTTDeserialize_ack(&type, &dup, &mypacketid, readbuf, MAX_MQTT_PACKET_SIZE) != 1) |
icraggs | 26:2658bb87c53d | 659 | rc = FAILURE; |
icraggs | 20:cad3d54d7ecf | 660 | } |
icraggs | 26:2658bb87c53d | 661 | else |
icraggs | 26:2658bb87c53d | 662 | rc = FAILURE; |
Ian Craggs |
12:cc7f2d62a393 | 663 | } |
icraggs | 20:cad3d54d7ecf | 664 | else if (message->qos == QOS2) |
Ian Craggs |
12:cc7f2d62a393 | 665 | { |
icraggs | 20:cad3d54d7ecf | 666 | if (waitfor(PUBCOMP, timer) == PUBCOMP) |
icraggs | 20:cad3d54d7ecf | 667 | { |
icraggs | 36:2f1ada427e56 | 668 | unsigned short mypacketid; |
icraggs | 36:2f1ada427e56 | 669 | unsigned char dup, type; |
icraggs | 26:2658bb87c53d | 670 | if (MQTTDeserialize_ack(&type, &dup, &mypacketid, readbuf, MAX_MQTT_PACKET_SIZE) != 1) |
icraggs | 26:2658bb87c53d | 671 | rc = FAILURE; |
icraggs | 20:cad3d54d7ecf | 672 | } |
icraggs | 26:2658bb87c53d | 673 | else |
icraggs | 26:2658bb87c53d | 674 | rc = FAILURE; |
Ian Craggs |
12:cc7f2d62a393 | 675 | } |
icraggs | 15:64a57183aa03 | 676 | |
icraggs | 15:64a57183aa03 | 677 | exit: |
icraggs | 8:c46930bd6c82 | 678 | return rc; |
icraggs | 8:c46930bd6c82 | 679 | } |
icraggs | 8:c46930bd6c82 | 680 | |
icraggs | 8:c46930bd6c82 | 681 | |
icraggs | 23:05fc7de97d4a | 682 | template<class Network, class Timer, int MAX_MQTT_PACKET_SIZE, int b> |
icraggs | 23:05fc7de97d4a | 683 | int MQTT::Client<Network, Timer, MAX_MQTT_PACKET_SIZE, b>::disconnect() |
icraggs | 20:cad3d54d7ecf | 684 | { |
icraggs | 26:2658bb87c53d | 685 | int rc = FAILURE; |
icraggs | 23:05fc7de97d4a | 686 | Timer timer = Timer(command_timeout_ms); // we might wait for incomplete incoming publishes to complete |
icraggs | 23:05fc7de97d4a | 687 | int len = MQTTSerialize_disconnect(buf, MAX_MQTT_PACKET_SIZE); |
icraggs | 26:2658bb87c53d | 688 | if (len > 0) |
icraggs | 26:2658bb87c53d | 689 | rc = sendPacket(len, timer); // send the disconnect packet |
icraggs | 31:a51dd239b78e | 690 | |
icraggs | 31:a51dd239b78e | 691 | isconnected = false; |
icraggs | 23:05fc7de97d4a | 692 | return rc; |
icraggs | 20:cad3d54d7ecf | 693 | } |
icraggs | 20:cad3d54d7ecf | 694 | |
icraggs | 20:cad3d54d7ecf | 695 | |
icraggs | 20:cad3d54d7ecf | 696 | #endif |