|
| 1 | +/******************************************************************************* |
| 2 | + * Copyright (c) 2014, 2017 IBM Corp. |
| 3 | + * |
| 4 | + * All rights reserved. This program and the accompanying materials |
| 5 | + * are made available under the terms of the Eclipse Public License v1.0 |
| 6 | + * and Eclipse Distribution License v1.0 which accompany this distribution. |
| 7 | + * |
| 8 | + * The Eclipse Public License is available at |
| 9 | + * http://www.eclipse.org/legal/epl-v10.html |
| 10 | + * and the Eclipse Distribution License is available at |
| 11 | + * http://www.eclipse.org/org/documents/edl-v10.php. |
| 12 | + * |
| 13 | + * Contributors: |
| 14 | + * Allan Stockdill-Mander/Ian Craggs - initial API and implementation and/or initial documentation |
| 15 | + * Ian Craggs - documentation and platform specific header |
| 16 | + * Ian Craggs - add setMessageHandler function |
| 17 | + *******************************************************************************/ |
| 18 | + |
| 19 | +#if !defined(__MQTT_CLIENT_C_) |
| 20 | +#define __MQTT_CLIENT_C_ |
| 21 | + |
| 22 | +#if defined(__cplusplus) |
| 23 | + extern "C" { |
| 24 | +#endif |
| 25 | + |
| 26 | +#if defined(WIN32_DLL) || defined(WIN64_DLL) |
| 27 | + #define DLLImport __declspec(dllimport) |
| 28 | + #define DLLExport __declspec(dllexport) |
| 29 | +#elif defined(LINUX_SO) |
| 30 | + #define DLLImport extern |
| 31 | + #define DLLExport __attribute__ ((visibility ("default"))) |
| 32 | +#else |
| 33 | + #define DLLImport |
| 34 | + #define DLLExport |
| 35 | +#endif |
| 36 | + |
| 37 | +#include "MQTTPacket.h" |
| 38 | +#include "stdio.h" |
| 39 | +#include "MQTTFreeRTOS.h" |
| 40 | + |
| 41 | +#if defined(MQTTCLIENT_PLATFORM_HEADER) |
| 42 | +/* The following sequence of macros converts the MQTTCLIENT_PLATFORM_HEADER value |
| 43 | + * into a string constant suitable for use with include. |
| 44 | + */ |
| 45 | +#define xstr(s) str(s) |
| 46 | +#define str(s) #s |
| 47 | +#include xstr(MQTTCLIENT_PLATFORM_HEADER) |
| 48 | +#endif |
| 49 | + |
| 50 | +#define MAX_PACKET_ID 65535 /* according to the MQTT specification - do not change! */ |
| 51 | + |
| 52 | +#if !defined(MAX_MESSAGE_HANDLERS) |
| 53 | +#define MAX_MESSAGE_HANDLERS 5 /* redefinable - how many subscriptions do you want? */ |
| 54 | +#endif |
| 55 | + |
| 56 | +enum QoS { QOS0, QOS1, QOS2, SUBFAIL=0x80 }; |
| 57 | + |
| 58 | +/* all failure return codes must be negative */ |
| 59 | +enum returnCode { BUFFER_OVERFLOW = -2, FAILURE = -1, SUCCESS = 0 }; |
| 60 | + |
| 61 | +/* The Platform specific header must define the Network and Timer structures and functions |
| 62 | + * which operate on them. |
| 63 | + * |
| 64 | +typedef struct Network |
| 65 | +{ |
| 66 | + int (*mqttread)(Network*, unsigned char* read_buffer, int, int); |
| 67 | + int (*mqttwrite)(Network*, unsigned char* send_buffer, int, int); |
| 68 | +} Network;*/ |
| 69 | + |
| 70 | +/* The Timer structure must be defined in the platform specific header, |
| 71 | + * and have the following functions to operate on it. */ |
| 72 | +extern void TimerInit(Timer*); |
| 73 | +extern char TimerIsExpired(Timer*); |
| 74 | +extern void TimerCountdownMS(Timer*, unsigned int); |
| 75 | +extern void TimerCountdown(Timer*, unsigned int); |
| 76 | +extern int TimerLeftMS(Timer*); |
| 77 | + |
| 78 | +typedef struct MQTTMessage |
| 79 | +{ |
| 80 | + enum QoS qos; |
| 81 | + unsigned char retained; |
| 82 | + unsigned char dup; |
| 83 | + unsigned short id; |
| 84 | + void *payload; |
| 85 | + size_t payloadlen; |
| 86 | +} MQTTMessage; |
| 87 | + |
| 88 | +typedef struct MessageData |
| 89 | +{ |
| 90 | + MQTTMessage* message; |
| 91 | + MQTTString* topicName; |
| 92 | +} MessageData; |
| 93 | + |
| 94 | +typedef struct MQTTConnackData |
| 95 | +{ |
| 96 | + unsigned char rc; |
| 97 | + unsigned char sessionPresent; |
| 98 | +} MQTTConnackData; |
| 99 | + |
| 100 | +typedef struct MQTTSubackData |
| 101 | +{ |
| 102 | + enum QoS grantedQoS; |
| 103 | +} MQTTSubackData; |
| 104 | + |
| 105 | +typedef void (*messageHandler)(MessageData*); |
| 106 | + |
| 107 | +typedef struct MQTTClient |
| 108 | +{ |
| 109 | + unsigned int next_packetid, |
| 110 | + command_timeout_ms; |
| 111 | + size_t buf_size, |
| 112 | + readbuf_size; |
| 113 | + unsigned char *buf, |
| 114 | + *readbuf; |
| 115 | + unsigned int keepAliveInterval; |
| 116 | + char ping_outstanding; |
| 117 | + int isconnected; |
| 118 | + int cleansession; |
| 119 | + |
| 120 | + struct MessageHandlers |
| 121 | + { |
| 122 | + const char* topicFilter; |
| 123 | + void (*fp) (MessageData*); |
| 124 | + } messageHandlers[MAX_MESSAGE_HANDLERS]; /* Message handlers are indexed by subscription topic */ |
| 125 | + |
| 126 | + void (*defaultMessageHandler) (MessageData*); |
| 127 | + |
| 128 | + Network* ipstack; |
| 129 | + Timer last_sent, last_received; |
| 130 | +#if defined(MQTT_TASK) |
| 131 | + Mutex mutex; |
| 132 | + Thread thread; |
| 133 | +#endif |
| 134 | +} MQTTClient; |
| 135 | + |
| 136 | +#define DefaultClient {0, 0, 0, 0, NULL, NULL, 0, 0, 0} |
| 137 | + |
| 138 | + |
| 139 | +/** |
| 140 | + * Create an MQTT client object |
| 141 | + * @param client |
| 142 | + * @param network |
| 143 | + * @param command_timeout_ms |
| 144 | + * @param |
| 145 | + */ |
| 146 | +DLLExport void MQTTClientInit(MQTTClient* client, Network* network, unsigned int command_timeout_ms, |
| 147 | + unsigned char* sendbuf, size_t sendbuf_size, unsigned char* readbuf, size_t readbuf_size); |
| 148 | + |
| 149 | +/** MQTT Connect - send an MQTT connect packet down the network and wait for a Connack |
| 150 | + * The nework object must be connected to the network endpoint before calling this |
| 151 | + * @param options - connect options |
| 152 | + * @return success code |
| 153 | + */ |
| 154 | +DLLExport int MQTTConnectWithResults(MQTTClient* client, MQTTPacket_connectData* options, |
| 155 | + MQTTConnackData* data); |
| 156 | + |
| 157 | +/** MQTT Connect - send an MQTT connect packet down the network and wait for a Connack |
| 158 | + * The nework object must be connected to the network endpoint before calling this |
| 159 | + * @param options - connect options |
| 160 | + * @return success code |
| 161 | + */ |
| 162 | +DLLExport int MQTTConnect(MQTTClient* client, MQTTPacket_connectData* options); |
| 163 | + |
| 164 | +/** MQTT Publish - send an MQTT publish packet and wait for all acks to complete for all QoSs |
| 165 | + * @param client - the client object to use |
| 166 | + * @param topic - the topic to publish to |
| 167 | + * @param message - the message to send |
| 168 | + * @return success code |
| 169 | + */ |
| 170 | +DLLExport int MQTTPublish(MQTTClient* client, const char*, MQTTMessage*); |
| 171 | + |
| 172 | +/** MQTT SetMessageHandler - set or remove a per topic message handler |
| 173 | + * @param client - the client object to use |
| 174 | + * @param topicFilter - the topic filter set the message handler for |
| 175 | + * @param messageHandler - pointer to the message handler function or NULL to remove |
| 176 | + * @return success code |
| 177 | + */ |
| 178 | +DLLExport int MQTTSetMessageHandler(MQTTClient* c, const char* topicFilter, messageHandler messageHandler); |
| 179 | + |
| 180 | +/** MQTT Subscribe - send an MQTT subscribe packet and wait for suback before returning. |
| 181 | + * @param client - the client object to use |
| 182 | + * @param topicFilter - the topic filter to subscribe to |
| 183 | + * @param message - the message to send |
| 184 | + * @return success code |
| 185 | + */ |
| 186 | +DLLExport int MQTTSubscribe(MQTTClient* client, const char* topicFilter, enum QoS, messageHandler); |
| 187 | + |
| 188 | +/** MQTT Subscribe - send an MQTT subscribe packet and wait for suback before returning. |
| 189 | + * @param client - the client object to use |
| 190 | + * @param topicFilter - the topic filter to subscribe to |
| 191 | + * @param message - the message to send |
| 192 | + * @param data - suback granted QoS returned |
| 193 | + * @return success code |
| 194 | + */ |
| 195 | +DLLExport int MQTTSubscribeWithResults(MQTTClient* client, const char* topicFilter, enum QoS, messageHandler, MQTTSubackData* data); |
| 196 | + |
| 197 | +/** MQTT Subscribe - send an MQTT unsubscribe packet and wait for unsuback before returning. |
| 198 | + * @param client - the client object to use |
| 199 | + * @param topicFilter - the topic filter to unsubscribe from |
| 200 | + * @return success code |
| 201 | + */ |
| 202 | +DLLExport int MQTTUnsubscribe(MQTTClient* client, const char* topicFilter); |
| 203 | + |
| 204 | +/** MQTT Disconnect - send an MQTT disconnect packet and close the connection |
| 205 | + * @param client - the client object to use |
| 206 | + * @return success code |
| 207 | + */ |
| 208 | +DLLExport int MQTTDisconnect(MQTTClient* client); |
| 209 | + |
| 210 | +/** MQTT Yield - MQTT background |
| 211 | + * @param client - the client object to use |
| 212 | + * @param time - the time, in milliseconds, to yield for |
| 213 | + * @return success code |
| 214 | + */ |
| 215 | +DLLExport int MQTTYield(MQTTClient* client, int time); |
| 216 | + |
| 217 | +/** MQTT isConnected |
| 218 | + * @param client - the client object to use |
| 219 | + * @return truth value indicating whether the client is connected to the server |
| 220 | + */ |
| 221 | +static inline DLLExport int MQTTIsConnected(MQTTClient* client) |
| 222 | +{ |
| 223 | + return client->isconnected; |
| 224 | +} |
| 225 | + |
| 226 | +#if defined(MQTT_TASK) |
| 227 | +/** MQTT start background thread for a client. After this, MQTTYield should not be called. |
| 228 | +* @param client - the client object to use |
| 229 | +* @return success code |
| 230 | +*/ |
| 231 | +DLLExport int MQTTStartTask(MQTTClient* client); |
| 232 | +#endif |
| 233 | + |
| 234 | +#if defined(__cplusplus) |
| 235 | + } |
| 236 | +#endif |
| 237 | + |
| 238 | +#endif |
0 commit comments