-
Notifications
You must be signed in to change notification settings - Fork 93
/
constants.js
291 lines (267 loc) · 7.67 KB
/
constants.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
/* Protocol - protocol constants */
const protocol = module.exports
const { Buffer } = require('buffer')
/* Command code => mnemonic */
protocol.types = {
0: 'reserved',
1: 'connect',
2: 'connack',
3: 'publish',
4: 'puback',
5: 'pubrec',
6: 'pubrel',
7: 'pubcomp',
8: 'subscribe',
9: 'suback',
10: 'unsubscribe',
11: 'unsuback',
12: 'pingreq',
13: 'pingresp',
14: 'disconnect',
15: 'auth'
}
protocol.requiredHeaderFlags = {
1: 0, // 'connect'
2: 0, // 'connack'
4: 0, // 'puback'
5: 0, // 'pubrec'
6: 2, // 'pubrel'
7: 0, // 'pubcomp'
8: 2, // 'subscribe'
9: 0, // 'suback'
10: 2, // 'unsubscribe'
11: 0, // 'unsuback'
12: 0, // 'pingreq'
13: 0, // 'pingresp'
14: 0, // 'disconnect'
15: 0 // 'auth'
}
protocol.requiredHeaderFlagsErrors = {}
for (const k in protocol.requiredHeaderFlags) {
const v = protocol.requiredHeaderFlags[k]
protocol.requiredHeaderFlagsErrors[k] = 'Invalid header flag bits, must be 0x' + v.toString(16) + ' for ' + protocol.types[k] + ' packet'
}
/* Mnemonic => Command code */
protocol.codes = {}
for (const k in protocol.types) {
const v = protocol.types[k]
protocol.codes[v] = k
}
/* Header */
protocol.CMD_SHIFT = 4
protocol.CMD_MASK = 0xF0
protocol.DUP_MASK = 0x08
protocol.QOS_MASK = 0x03
protocol.QOS_SHIFT = 1
protocol.RETAIN_MASK = 0x01
/* Length */
protocol.VARBYTEINT_MASK = 0x7F
protocol.VARBYTEINT_FIN_MASK = 0x80
protocol.VARBYTEINT_MAX = 268435455
/* Connack */
protocol.SESSIONPRESENT_MASK = 0x01
protocol.SESSIONPRESENT_HEADER = Buffer.from([protocol.SESSIONPRESENT_MASK])
protocol.CONNACK_HEADER = Buffer.from([protocol.codes.connack << protocol.CMD_SHIFT])
/* Connect */
protocol.USERNAME_MASK = 0x80
protocol.PASSWORD_MASK = 0x40
protocol.WILL_RETAIN_MASK = 0x20
protocol.WILL_QOS_MASK = 0x18
protocol.WILL_QOS_SHIFT = 3
protocol.WILL_FLAG_MASK = 0x04
protocol.CLEAN_SESSION_MASK = 0x02
protocol.CONNECT_HEADER = Buffer.from([protocol.codes.connect << protocol.CMD_SHIFT])
/* Properties */
protocol.properties = {
sessionExpiryInterval: 17,
willDelayInterval: 24,
receiveMaximum: 33,
maximumPacketSize: 39,
topicAliasMaximum: 34,
requestResponseInformation: 25,
requestProblemInformation: 23,
userProperties: 38,
authenticationMethod: 21,
authenticationData: 22,
payloadFormatIndicator: 1,
messageExpiryInterval: 2,
contentType: 3,
responseTopic: 8,
correlationData: 9,
maximumQoS: 36,
retainAvailable: 37,
assignedClientIdentifier: 18,
reasonString: 31,
wildcardSubscriptionAvailable: 40,
subscriptionIdentifiersAvailable: 41,
sharedSubscriptionAvailable: 42,
serverKeepAlive: 19,
responseInformation: 26,
serverReference: 28,
topicAlias: 35,
subscriptionIdentifier: 11
}
protocol.propertiesCodes = {}
for (const prop in protocol.properties) {
const id = protocol.properties[prop]
protocol.propertiesCodes[id] = prop
}
protocol.propertiesTypes = {
sessionExpiryInterval: 'int32',
willDelayInterval: 'int32',
receiveMaximum: 'int16',
maximumPacketSize: 'int32',
topicAliasMaximum: 'int16',
requestResponseInformation: 'byte',
requestProblemInformation: 'byte',
userProperties: 'pair',
authenticationMethod: 'string',
authenticationData: 'binary',
payloadFormatIndicator: 'byte',
messageExpiryInterval: 'int32',
contentType: 'string',
responseTopic: 'string',
correlationData: 'binary',
maximumQoS: 'int8',
retainAvailable: 'byte',
assignedClientIdentifier: 'string',
reasonString: 'string',
wildcardSubscriptionAvailable: 'byte',
subscriptionIdentifiersAvailable: 'byte',
sharedSubscriptionAvailable: 'byte',
serverKeepAlive: 'int16',
responseInformation: 'string',
serverReference: 'string',
topicAlias: 'int16',
subscriptionIdentifier: 'var'
}
function genHeader (type) {
return [0, 1, 2].map(qos => {
return [0, 1].map(dup => {
return [0, 1].map(retain => {
const buf = Buffer.alloc(1)
buf.writeUInt8(
protocol.codes[type] << protocol.CMD_SHIFT |
(dup ? protocol.DUP_MASK : 0) |
qos << protocol.QOS_SHIFT | retain, 0, true)
return buf
})
})
})
}
/* Publish */
protocol.PUBLISH_HEADER = genHeader('publish')
/* Subscribe */
protocol.SUBSCRIBE_HEADER = genHeader('subscribe')
protocol.SUBSCRIBE_OPTIONS_QOS_MASK = 0x03
protocol.SUBSCRIBE_OPTIONS_NL_MASK = 0x01
protocol.SUBSCRIBE_OPTIONS_NL_SHIFT = 2
protocol.SUBSCRIBE_OPTIONS_RAP_MASK = 0x01
protocol.SUBSCRIBE_OPTIONS_RAP_SHIFT = 3
protocol.SUBSCRIBE_OPTIONS_RH_MASK = 0x03
protocol.SUBSCRIBE_OPTIONS_RH_SHIFT = 4
protocol.SUBSCRIBE_OPTIONS_RH = [0x00, 0x10, 0x20]
protocol.SUBSCRIBE_OPTIONS_NL = 0x04
protocol.SUBSCRIBE_OPTIONS_RAP = 0x08
protocol.SUBSCRIBE_OPTIONS_QOS = [0x00, 0x01, 0x02]
/* Unsubscribe */
protocol.UNSUBSCRIBE_HEADER = genHeader('unsubscribe')
/* Confirmations */
protocol.ACKS = {
unsuback: genHeader('unsuback'),
puback: genHeader('puback'),
pubcomp: genHeader('pubcomp'),
pubrel: genHeader('pubrel'),
pubrec: genHeader('pubrec')
}
protocol.SUBACK_HEADER = Buffer.from([protocol.codes.suback << protocol.CMD_SHIFT])
/* Protocol versions */
protocol.VERSION3 = Buffer.from([3])
protocol.VERSION4 = Buffer.from([4])
protocol.VERSION5 = Buffer.from([5])
protocol.VERSION131 = Buffer.from([131])
protocol.VERSION132 = Buffer.from([132])
/* QoS */
protocol.QOS = [0, 1, 2].map(qos => {
return Buffer.from([qos])
})
/* Empty packets */
protocol.EMPTY = {
pingreq: Buffer.from([protocol.codes.pingreq << 4, 0]),
pingresp: Buffer.from([protocol.codes.pingresp << 4, 0]),
disconnect: Buffer.from([protocol.codes.disconnect << 4, 0])
}
protocol.MQTT5_PUBACK_PUBREC_CODES = {
0x00: 'Success',
0x10: 'No matching subscribers',
0x80: 'Unspecified error',
0x83: 'Implementation specific error',
0x87: 'Not authorized',
0x90: 'Topic Name invalid',
0x91: 'Packet identifier in use',
0x97: 'Quota exceeded',
0x99: 'Payload format invalid'
}
protocol.MQTT5_PUBREL_PUBCOMP_CODES = {
0x00: 'Success',
0x92: 'Packet Identifier not found'
}
protocol.MQTT5_SUBACK_CODES = {
0x00: 'Granted QoS 0',
0x01: 'Granted QoS 1',
0x02: 'Granted QoS 2',
0x80: 'Unspecified error',
0x83: 'Implementation specific error',
0x87: 'Not authorized',
0x8F: 'Topic Filter invalid',
0x91: 'Packet Identifier in use',
0x97: 'Quota exceeded',
0x9E: 'Shared Subscriptions not supported',
0xA1: 'Subscription Identifiers not supported',
0xA2: 'Wildcard Subscriptions not supported'
}
protocol.MQTT5_UNSUBACK_CODES = {
0x00: 'Success',
0x11: 'No subscription existed',
0x80: 'Unspecified error',
0x83: 'Implementation specific error',
0x87: 'Not authorized',
0x8F: 'Topic Filter invalid',
0x91: 'Packet Identifier in use'
}
protocol.MQTT5_DISCONNECT_CODES = {
0x00: 'Normal disconnection',
0x04: 'Disconnect with Will Message',
0x80: 'Unspecified error',
0x81: 'Malformed Packet',
0x82: 'Protocol Error',
0x83: 'Implementation specific error',
0x87: 'Not authorized',
0x89: 'Server busy',
0x8B: 'Server shutting down',
0x8D: 'Keep Alive timeout',
0x8E: 'Session taken over',
0x8F: 'Topic Filter invalid',
0x90: 'Topic Name invalid',
0x93: 'Receive Maximum exceeded',
0x94: 'Topic Alias invalid',
0x95: 'Packet too large',
0x96: 'Message rate too high',
0x97: 'Quota exceeded',
0x98: 'Administrative action',
0x99: 'Payload format invalid',
0x9A: 'Retain not supported',
0x9B: 'QoS not supported',
0x9C: 'Use another server',
0x9D: 'Server moved',
0x9E: 'Shared Subscriptions not supported',
0x9F: 'Connection rate exceeded',
0xA0: 'Maximum connect time',
0xA1: 'Subscription Identifiers not supported',
0xA2: 'Wildcard Subscriptions not supported'
}
protocol.MQTT5_AUTH_CODES = {
0x00: 'Success',
0x18: 'Continue authentication',
0x19: 'Re-authenticate'
}