liubin | 281ac46 | 2023-07-19 14:22:54 +0800 | [diff] [blame] | 1 | /*******************************************************************************
|
| 2 | * Copyright (c) 2014 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 | *******************************************************************************/
|
| 16 |
|
| 17 | #ifndef __MQTT_CLIENT_C_
|
| 18 | #define __MQTT_CLIENT_C_
|
| 19 |
|
| 20 | #include "MQTTPacket.h"
|
| 21 | #include "stdio.h"
|
| 22 | #include "MQTTLinux.h"
|
| 23 |
|
| 24 | #define MAX_PACKET_ID 65535
|
| 25 | #define MAX_MESSAGE_HANDLERS 5
|
| 26 | #define MAX_FAIL_ALLOWED 2
|
| 27 |
|
| 28 | enum QoS { QOS0, QOS1, QOS2 };
|
| 29 |
|
| 30 | // all failure return codes must be negative
|
| 31 | enum returnCode {DISCONNECTED = -3, BUFFER_OVERFLOW = -2, FAILURE = -1, SUCCESS = 0 };
|
| 32 |
|
| 33 | void NewTimer(Timer*);
|
| 34 |
|
| 35 | typedef struct MQTTMessage MQTTMessage;
|
| 36 |
|
| 37 | typedef struct MessageData MessageData;
|
| 38 |
|
| 39 | struct MQTTMessage
|
| 40 | {
|
| 41 | enum QoS qos;
|
| 42 | char retained;
|
| 43 | char dup;
|
| 44 | unsigned short id;
|
| 45 | void *payload;
|
| 46 | size_t payloadlen;
|
| 47 | };
|
| 48 |
|
| 49 | struct MessageData
|
| 50 | {
|
| 51 | MQTTMessage* message;
|
| 52 | MQTTString* topicName;
|
| 53 | };
|
| 54 |
|
| 55 | typedef struct {
|
| 56 | char clientId[255];
|
| 57 | char deviceToken[255];
|
| 58 | } regnwl_info_t;
|
| 59 |
|
| 60 | typedef void (*messageHandler)(MessageData*);
|
| 61 |
|
| 62 | typedef struct Client Client;
|
| 63 |
|
| 64 | int MQTTConnect (Client*, MQTTPacket_connectData*);
|
| 65 | int MQTTPublish (Client*, const char*, MQTTMessage*);
|
| 66 | int MQTTSubscribe (Client*, const char*, enum QoS, messageHandler);
|
| 67 | int MQTTUnsubscribe (Client*, const char*);
|
| 68 | int MQTTDisconnect (Client*,Network*);
|
| 69 | int MQTTYield (Client*, int);
|
| 70 |
|
| 71 | void setDefaultMessageHandler(Client*, messageHandler);
|
| 72 |
|
| 73 | void MQTTClient(Client*, Network*, unsigned int, unsigned char*, size_t, unsigned char*, size_t);
|
| 74 |
|
| 75 | struct Client {
|
| 76 | unsigned int next_packetid;
|
| 77 | unsigned int command_timeout_ms;
|
| 78 | size_t buf_size, readbuf_size;
|
| 79 | unsigned char *buf;
|
| 80 | unsigned char *readbuf;
|
| 81 | unsigned int keepAliveInterval;
|
| 82 | char ping_outstanding;
|
| 83 | int fail_count;
|
| 84 | int isconnected;
|
| 85 |
|
| 86 | struct MessageHandlers
|
| 87 | {
|
| 88 | const char* topicFilter;
|
| 89 | void (*fp) (MessageData*);
|
| 90 | } messageHandlers[MAX_MESSAGE_HANDLERS]; // Message handlers are indexed by subscription topic
|
| 91 |
|
| 92 | void (*defaultMessageHandler) (MessageData*);
|
| 93 |
|
| 94 | Network* ipstack;
|
| 95 | Timer ping_timer;
|
| 96 | };
|
| 97 |
|
| 98 | #define DefaultClient {0, 0, 0, 0, NULL, NULL, 0, 0, 0}
|
| 99 |
|
| 100 | #endif
|