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