lipengwei
2020-05-27 ca75cc09395171ca363b040f815099fc734b3f9d
commit | author | age
2370e0 1 //
H 2 // MQTTMessage.h
3 // MQTTClient.framework
4 //
5 // Copyright © 2013-2016, Christoph Krey
6 //
7 // based on
8 //
9 // Copyright (c) 2011, 2013, 2lemetry LLC
10 //
11 // All rights reserved. This program and the accompanying materials
12 // are made available under the terms of the Eclipse Public License v1.0
13 // which accompanies this distribution, and is available at
14 // http://www.eclipse.org/legal/epl-v10.html
15 //
16 // Contributors:
17 //    Kyle Roche - initial API and implementation and/or initial documentation
18 //
19
20 #import <Foundation/Foundation.h>
21 /**
22  Enumeration of MQTT Quality of Service levels
23  */
24 typedef NS_ENUM(UInt8, MQTTQosLevel) {
25     MQTTQosLevelAtMostOnce = 0,
26     MQTTQosLevelAtLeastOnce = 1,
27     MQTTQosLevelExactlyOnce = 2
28 };
29
30 /**
31  Enumeration of MQTT protocol version
32  */
33 typedef NS_ENUM(UInt8, MQTTProtocolVersion) {
34     MQTTProtocolVersion31 = 3,
35     MQTTProtocolVersion311 = 4
36 };
37
38 typedef NS_ENUM(UInt8, MQTTCommandType) {
39     MQTT_None = 0,
40     MQTTConnect = 1,
41     MQTTConnack = 2,
42     MQTTPublish = 3,
43     MQTTPuback = 4,
44     MQTTPubrec = 5,
45     MQTTPubrel = 6,
46     MQTTPubcomp = 7,
47     MQTTSubscribe = 8,
48     MQTTSuback = 9,
49     MQTTUnsubscribe = 10,
50     MQTTUnsuback = 11,
51     MQTTPingreq = 12,
52     MQTTPingresp = 13,
53     MQTTDisconnect = 14
54 };
55
56 @interface MQTTMessage : NSObject
57 @property (nonatomic) MQTTCommandType type;
58 @property (nonatomic) MQTTQosLevel qos;
59 @property (nonatomic) BOOL retainFlag;
60 @property (nonatomic) BOOL dupFlag;
61 @property (nonatomic) UInt16 mid;
62 @property (strong, nonatomic) NSData * data;
63
64 /**
65  Enumeration of MQTT Connect return codes
66  */
67
68 typedef NS_ENUM(NSUInteger, MQTTConnectReturnCode) {
69     MQTTConnectAccepted = 0,
70     MQTTConnectRefusedUnacceptableProtocolVersion,
71     MQTTConnectRefusedIdentiferRejected,
72     MQTTConnectRefusedServerUnavailable,
73     MQTTConnectRefusedBadUserNameOrPassword,
74     MQTTConnectRefusedNotAuthorized
75 };
76
77 // factory methods
78 + (MQTTMessage *)connectMessageWithClientId:(NSString*)clientId
79                                    userName:(NSString*)userName
80                                    password:(NSString*)password
81                                   keepAlive:(NSInteger)keeplive
82                                cleanSession:(BOOL)cleanSessionFlag
83                                        will:(BOOL)will
84                                   willTopic:(NSString*)willTopic
85                                     willMsg:(NSData*)willData
86                                     willQoS:(MQTTQosLevel)willQoS
87                                  willRetain:(BOOL)willRetainFlag
88                               protocolLevel:(UInt8)protocolLevel;
89
90 + (MQTTMessage *)pingreqMessage;
91 + (MQTTMessage *)disconnectMessage;
92 + (MQTTMessage *)subscribeMessageWithMessageId:(UInt16)msgId
93                                         topics:(NSDictionary *)topics;
94 + (MQTTMessage *)unsubscribeMessageWithMessageId:(UInt16)msgId
95                                           topics:(NSArray *)topics;
96 + (MQTTMessage *)publishMessageWithData:(NSData*)payload
97                                 onTopic:(NSString*)topic
98                                     qos:(MQTTQosLevel)qosLevel
99                                   msgId:(UInt16)msgId
100                              retainFlag:(BOOL)retain
101                                 dupFlag:(BOOL)dup;
102 + (MQTTMessage *)pubackMessageWithMessageId:(UInt16)msgId;
103 + (MQTTMessage *)pubrecMessageWithMessageId:(UInt16)msgId;
104 + (MQTTMessage *)pubrelMessageWithMessageId:(UInt16)msgId;
105 + (MQTTMessage *)pubcompMessageWithMessageId:(UInt16)msgId;
106 + (MQTTMessage *)messageFromData:(NSData *)data;
107
108 // instance methods
109 - (instancetype)initWithType:(MQTTCommandType)type;
110 - (instancetype)initWithType:(MQTTCommandType)type
111                         data:(NSData *)data;
112 - (instancetype)initWithType:(MQTTCommandType)type
113                          qos:(MQTTQosLevel)qos
114                         data:(NSData *)data;
115 - (instancetype)initWithType:(MQTTCommandType)type
116                          qos:(MQTTQosLevel)qos
117                   retainFlag:(BOOL)retainFlag
118                      dupFlag:(BOOL)dupFlag
119                         data:(NSData *)data;
120
121 - (NSData *)wireFormat;
122
123
124 @end
125
126 @interface NSMutableData (MQTT)
127 - (void)appendByte:(UInt8)byte;
128 - (void)appendUInt16BigEndian:(UInt16)val;
129 - (void)appendMQTTString:(NSString*)s;
130
131 @end