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