-
Notifications
You must be signed in to change notification settings - Fork 47
/
MQTT_Main_PN.SCL
655 lines (526 loc) · 22.4 KB
/
MQTT_Main_PN.SCL
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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
(* ToDo:
- MQtt Publish received: act as QoS wants
- MQtt Publish receives: QoS 2 handshake
*)
(**************************************************************)
FUNCTION_BLOCK MQTT
TITLE = 'MQTT MAIN Function(PN)'
VERSION : '1.0'
AUTHOR : Maul
CONST
// TCP States
TCP_STATE_UNINITIALIZED := 0;
TCP_STATE_INITIALIZING := 1;
TCP_STATE_INITIALIZED := 2;
TCP_STATE_CONNECTING := 3;
TCP_STATE_CONNECTED := 4;
TCP_STATE_DISCONNECTING := 5;
TCP_STATE_DISCONNECTED := 6;
// MQTT States
MQTT_STATE_DISCONNECT := -6;
MQTT_STATE_DISCONNECTING := -5;
MQTT_STATE_DISCONNECTED := -1;
MQTT_STATE_CONNECTING := 0;
MQTT_STATE_CONNECTED := 1;
// packet reading States
WAIT_FOR_RECEIVED_PACKET := 0;
READ_COMPLETE_PACKET := 1;
// MQTT Message Types
MQTT_CONNECT := 16;
MQTT_CONNACK := 32;
MQTT_PUBLISH := 48;
MQTT_PUBACK := 64;
MQTT_PUBREC := 80;
MQTT_PUBREL := 96;
MQTT_PUBCOMP := 112;
MQTT_SUBSCRIBE := 130;
MQTT_SUBACK := 144;
MQTT_UNSUBSCRIBE := 160;
MQTT_UNSUBACK := 176;
MQTT_PINGREQ := 192;
MQTT_PINGRESP := 208;
MQTT_DISCONNECT := 224;
// Types of Ethernet Adapters
PN := 0;
CP := 1;
// CP AG_CTRL COMMANDS
CN_NOP := 0; // do nothing
CN_STATUS := 1;
CN_RESET := 2;
CN_STATUS_ALL := 3; // get status for ALL connections
CN_RESET_ALL := 4; // reset ALL connections
CN_CLEAR_RESET := 5;
CN_DISCON := 6; // disconnect the connection, no automatic reconnect
CN_STARTCON := 7; // initiate connection
END_CONST
VAR_IN_OUT
callback : BOOL;
callbackPacketType : INT;
END_VAR
VAR_TEMP
i : INT;
intTemp : INT;
TOP_SI: STRUCT
EV_CLASS: BYTE;
EV_NUM: BYTE;
PRIORITY: BYTE;
NUM: BYTE;
TYP2_3: BYTE;
TYP1: BYTE;
ZI1: WORD;
ZI2_3: DWORD;
END_STRUCT;
START_UP_SI: STRUCT
EV_CLASS: BYTE;
EV_NUM: BYTE;
PRIORITY: BYTE;
NUM: BYTE;
TYP2_3: BYTE;
TYP1: BYTE;
ZI1: WORD;
ZI2_3: DWORD;
END_STRUCT;
tmpPacketID : INT;
structTmpPacketId AT tmpPacketID : STRUCT
byteLSB : BYTE;
byteMSB : BYTE;
END_STRUCT;
END_VAR
VAR
// TCP State Control
tcpState : INT := 0; // TCP state: TCP_UNINITIALIZED
// MQTT Protocol State Control
intState, lastState : INT; // State Machine
tcpRecByte : BYTE; // Next incoming TCP byte
myPacketReader : mqttPacketReader; // MQTT PacketReader Instance
msgId : INT;
typeOfPacket : INT;
Runtimer : mqttRuntime;
TCP_Timer : mqttRuntime;
resetTime : TIME;
tl : INT;
paylPos : INT;
paylSize : INT;
//debug
recBufLeft : INT;
// Send TCP State
act_send : BOOL;
snd_busy : BOOL;
last_snd_err : WORD;
// Receive TCP State
last_rec_err : WORD;
// Control TCP State
cntrl_done : BOOL;
cntrl_error : BOOL;
cntrl_status : WORD;
cntrl_lastErrStatus : WORD;
cntrl_res1, cntrl_res2 : DWORD;
netcmd_success : BOOL := false;
netcmd_busy : BOOL := false;
netcmd_error : BOOL := false;
netcmd_status : WORD;
netcmd_datareceived : BOOL := false;
netcmd_datalength : INT;
NET_CON : TCON;
NET_DISCON : TDISCON;
NET_SEND : TSEND;
NET_RCV : TRCV;
BUSY_TIMER : TON;
NET_CONFIG : UDT_NET_CONFIG;
END_VAR
VAR_INPUT
connectionID : INT := 1; // S7 Connection ID for CP Ethernet
ipBlock1 : INT := 0;
ipBlock2 : INT := 0;
ipBlock3 : INT := 0;
ipBlock4 : INT := 0;
ipPort : INT := 0;
ipPort_Byte AT ipPort : STRUCT
byte1 : BYTE;
byte2 : BYTE;
END_STRUCT;
END_VAR
VAR_OUTPUT
pubTopic : STRING;
pubTopicChar AT pubTopic : ARRAY[0..255] OF CHAR;
payload : ARRAY[0..4096] OF BYTE; // Must match mqttGlobals.TCP_MAXRECVSIZE
payloadSize : INT;
END_VAR
BEGIN
(* Check if CPU had a reset, trigger network reinitialization on reset *)
intTemp := RD_SINFO (TOP_SI := TOP_SI, START_UP_SI := START_UP_SI);
IF TOP_SI.EV_NUM <> 3 THEN
tcpState := TCP_STATE_UNINITIALIZED;
mqttData.ethTCPConnected := false;
mqttData._state := MQTT_STATE_DISCONNECTED;
END_IF;
(*
******************************
TCP/IP State Machine
******************************
*)
CASE (tcpState) OF
TCP_STATE_UNINITIALIZED:
mqttData.ethTCPConnected := false;
tcpState := TCP_STATE_INITIALIZING;
;
TCP_STATE_INITIALIZING:
// *** PN CODE ****
// Load IP parameters to NET_CONFIG DB variables for usage in TCON
NET_CONFIG.id := INT_TO_WORD(connectionID);
NET_CONFIG.rem_staddr[1] := INT_TO_BYTE(ipBlock1);
NET_CONFIG.rem_staddr[2] := INT_TO_BYTE(ipBlock2);
NET_CONFIG.rem_staddr[3] := INT_TO_BYTE(ipBlock3);
NET_CONFIG.rem_staddr[4] := INT_TO_BYTE(ipBlock4);
NET_CONFIG.rem_tsap_id[1]:= ipPort_Byte.byte1;
NET_CONFIG.rem_tsap_id[2]:= ipPort_Byte.byte2;
NET_CON(REQ := false // IN: BOOL
,ID := INT_TO_WORD(connectionID) // IN: WORD
,CONNECT := NET_CONFIG // INOUT: ANY
);
NET_SEND(REQ := false // IN: BOOL
,ID := INT_TO_WORD(connectionID) // IN: WORD
,LEN := 0// IN: INT
,DATA := mqttData.tcpSendBuf // INOUT: ANY
);
NET_RCV(EN_R := false // IN: BOOL
,ID := INT_TO_WORD(connectionID) // IN: WORD
,LEN := 0 // IN: INT
,DATA := tcpRecByte // INOUT: ANY
);
NET_DISCON(REQ := false // IN: BOOL
,ID := INT_TO_WORD(connectionID) // IN: WORD
);
// *** PN CODE END ****
tcpState := TCP_STATE_INITIALIZED;
;
TCP_STATE_INITIALIZED:
tcpState := TCP_STATE_CONNECTING;
;
TCP_STATE_CONNECTING:
// *** PN CODE ****
NET_CON(REQ := true // IN: BOOL
,ID := INT_TO_WORD(connectionID) // IN: WORD
,CONNECT := NET_CONFIG // INOUT: ANY
);
netcmd_success := NET_CON.DONE; // OUT: BOOL
netcmd_error := NET_CON.ERROR; // OUT: BOOL
netcmd_status := NET_CON.STATUS; // OUT: WORD
IF netcmd_success AND (netcmd_status = W#16#0) THEN
tcpState := TCP_STATE_CONNECTED;
mqttData.ethTCPConnected := true;
END_IF;
IF netcmd_error THEN
cntrl_lastErrStatus := netcmd_error;
tcpState := TCP_STATE_DISCONNECTED;
END_IF;
// TCP timeout while connecting reset connection state and try again
IF NOT netcmd_success THEN
TCP_Timer(IN := true,RES := false);
IF TIME_TO_DINT(TCP_TIMER.RTIME) > TIME_TO_DINT(mqttGlobals.TCP_TIMEOUT) THEN
tcpState := TCP_STATE_DISCONNECTING;
END_IF;
END_IF;
// *** PN CODE END ****
;
TCP_STATE_CONNECTED:
// *** PN CODE - Check TCP connection state ****
NET_RCV(EN_R := false // IN: BOOL
,ID := INT_TO_WORD(connectionID) // IN: WORD
,LEN := 1 // IN: INT
,DATA := tcpRecByte // INOUT: ANY
);
IF NET_RCV.STATUS <> W#16#7000 THEN
tcpState := TCP_STATE_DISCONNECTING;
END_IF;
;
TCP_STATE_DISCONNECTING:
mqttData.ethTCPConnected := false;
// *** PN CODE ****
NET_DISCON(REQ := true // IN: BOOL
,ID := INT_TO_WORD(connectionID) // IN: WORD
);
netcmd_success := NET_DISCON.DONE; // OUT: BOOL
netcmd_busy := NET_DISCON.BUSY; // OUT: BOOL
netcmd_error := NET_DISCON.ERROR; // OUT: BOOL
netcmd_status := NET_DISCON.STATUS; // OUT: WORD
tcpState := TCP_STATE_DISCONNECTED;
// *** PN CODE END ****
;
TCP_STATE_DISCONNECTED:
mqttData.ethTCPConnected := false;
tcpState := TCP_STATE_UNINITIALIZED;
TCP_Timer(IN := false,RES := true);
;
ELSE:;
END_CASE;
(*
*******************************************
MQTT Timout Handling without TCP Connection
*******************************************
*)
Runtimer(IN := ((mqttData._state = MQTT_STATE_CONNECTING) OR (mqttData._state = MQTT_STATE_CONNECTED)),RES := false);
mqttData.runTime := Runtimer.RTIME;
//Recalculate timeouts when timer is set to T#0
IF TIME_TO_DINT(mqttData.runTime) = 0 AND (TIME_TO_DINT(mqttData.lastInActivity) <> 0 OR TIME_TO_DINT(mqttData.lastOutActivity)<> 0) THEN
Runtimer.run := TIME_TO_DINT(mqttGlobals.MQTT_KEEPALIVE) / 1000;
IF TIME_TO_DINT(mqttData.lastOutActivity) <> 0 THEN
mqttData.lastOutActivity := mqttGlobals.MQTT_KEEPALIVE - DINT_TO_TIME(2073600000 - TIME_TO_DINT(mqttData.lastOutActivity));
END_IF;
IF TIME_TO_DINT(mqttData.lastInActivity) <> 0 THEN
mqttData.lastInActivity := mqttGlobals.MQTT_KEEPALIVE - DINT_TO_TIME(2073600000 - TIME_TO_DINT(mqttData.lastInActivity));
END_IF;
END_IF;
IF (mqttData._state = MQTT_STATE_CONNECTING OR mqttData._state = MQTT_STATE_CONNECTED) AND tcpState <> TCP_STATE_CONNECTED THEN
IF ((mqttData.runTime - mqttData.lastInActivity) > mqttGlobals.MQTT_KEEPALIVE) OR ((mqttData.runTime - mqttData.lastOutActivity) > mqttGlobals.MQTT_KEEPALIVE) THEN
// Connection Timeout!
mqttData._state := MQTT_STATE_DISCONNECTED;
mqttData.reqSend := false;
act_send := false;
snd_busy := false;
END_IF;
END_IF;
(*
******************************
TCP/IP Sending and Receiving
******************************
*)
IF NOT (tcpState = TCP_STATE_CONNECTED) THEN // stop if there's no tcp connection
snd_busy := false;
act_send := false;
RETURN;
END_IF;
(* TCP SENDING *)
IF (mqttData.reqSend AND NOT snd_busy) THEN
mqttData.reqSend := false;
act_send := true;
END_IF;
IF mqttData.reqSend AND snd_busy THEN
(* Just a try to stop the process from deadlocking when TCP connection went down *)
BUSY_TIMER(IN := true, PT := T#500MS);
IF BUSY_TIMER.Q THEN
snd_busy := false; // simple reset busy because something went clearly wrong here ...
BUSY_TIMER(IN := false, PT := T#500MS);
END_IF;
END_IF;
// *** PN CODE ****
NET_SEND(REQ := act_send // IN: BOOL
,ID := INT_TO_WORD(connectionID) // IN: WORD
,LEN := mqttData.sendSize // IN: INT
,DATA := mqttData.tcpSendBuf // INOUT: ANY
);
netcmd_success := NET_SEND.DONE; // OUT: BOOL
netcmd_error := NET_SEND.ERROR; // OUT: BOOL
netcmd_status := NET_SEND.STATUS; // OUT: WORD
// after sending reset send buffer management
mqttData.sendSize := 0;
mqttData.tcp_sendBufferFull := false;
IF netcmd_error AND NOT netcmd_success THEN
last_snd_err := netcmd_status;
snd_busy := false;
IF ((netcmd_status = W#16#80A1) OR (netcmd_status = W#16#80C4)) THEN
mqttData.ethTCPConnected := false;
mqttData._state := MQTT_STATE_DISCONNECTED;
tcpState := TCP_STATE_DISCONNECTING;
END_IF;
END_IF;
// *** PN CODE END ****
IF (act_send) THEN
act_send := false;
snd_busy := true;
END_IF;
IF (netcmd_success AND snd_busy) THEN
snd_busy := false;
END_IF;
(* TCP RECEIVING *)
// *** PN CODE ****
NET_RCV(EN_R := true // IN: BOOL
,ID := INT_TO_WORD(connectionID) // IN: WORD
,LEN := 1 // IN: INT
,DATA := tcpRecByte // INOUT: ANY
);
netcmd_datareceived := NET_RCV.NDR; // OUT: BOOL
netcmd_error := NET_RCV.ERROR; // OUT: BOOL
netcmd_status := NET_RCV.STATUS; // OUT: WORD
netcmd_datalength := NET_RCV.RCVD_LEN; // OUT: INT
IF netcmd_error AND ((netcmd_status = W#16#80A1) OR (netcmd_status = W#16#80C4)) THEN
last_rec_err := netcmd_status;
mqttData._state := MQTT_STATE_DISCONNECTED;
tcpState := TCP_STATE_DISCONNECTING;
END_IF;
// *** PN CODE END ****
IF netcmd_datareceived THEN
IF mqttData.RecBufFull THEN
m33.1 := true;
ELSE
mqttData.tcpRecBuf[mqttData.pRec] := tcpRecByte;
mqttData.pRec := mqttData.pRec + 1;
IF (mqttData.pRec >= mqttGlobals.TCP_RECVBUFFERSIZE) THEN
mqttData.pRec := 0;
END_IF;
IF (mqttData.pRec = mqttData.pRead) THEN
mqttData.RecBufFull := true;
END_IF;
END_IF;
END_IF;
myPacketReader();
//debug
IF (mqttData.pRec >= mqttData.pRead) THEN
recBufLeft := mqttGlobals.TCP_RECVBUFFERSIZE - (mqttData.pRec - mqttData.pRead);
ELSE
recBufLeft := mqttGlobals.TCP_RECVBUFFERSIZE - (mqttData.pRead - mqttData.pRec);
END_IF;
(*
******************************
MQTT State Machine
******************************
*)
CASE (mqttData._state) OF
MQTT_STATE_DISCONNECTED:
lastState := mqttData._state;
// TODO: what is this for?
IF ((mqttData.runTime - t#10s) > resetTime) AND NOT mqttData.ethTCPConnected THEN
resetTime := mqttData.runTime;
END_IF;
;
MQTT_STATE_CONNECTING: // this state is triggered by mqttConnect FC
IF (mqttData._state <> lastState) THEN
intState := WAIT_FOR_RECEIVED_PACKET;
lastState := mqttData._state;
END_IF;
CASE (intState) OF
WAIT_FOR_RECEIVED_PACKET:
IF ((mqttData.runTime - mqttData.lastInActivity) >= mqttGlobals.MQTT_SOCKET_TIMEOUT) THEN
// Timeout!
mqttData._state := MQTT_STATE_DISCONNECTED;
ELSIF mqttAvailable() THEN
myPacketReader.iBegin := true;
intState := READ_COMPLETE_PACKET;
END_IF;
;
READ_COMPLETE_PACKET:
IF (myPacketReader.xDone) THEN
typeOfPacket := BYTE_TO_INT(mqttData.buffer[0] AND B#16#F0);
IF(typeOfPacket = mqttGlobals.MQTTCONNACK) THEN
mqttData.lastInActivity := mqttData.runTime;
mqttData.pingOutstanding := false;
IF BYTE_TO_INT(mqttData.buffer[3]) = mqttGlobals.MQTT_CONN_ACCEPTED THEN
mqttData._state := MQTT_STATE_CONNECTED;
ELSE
// MQTT Connection Error!
mqttData._state := MQTT_STATE_DISCONNECTED;
callbackPacketType := MQTT_CONNACK;
mqttData.mqttErrorCode := BYTE_TO_INT(mqttData.buffer[3]);
callback := true;
END_IF;
// ELSE maybe need equivalent/workaround to client->stop()
END_IF;
END_IF;
;
END_CASE;
;
MQTT_STATE_CONNECTED:
IF (mqttData._state <> lastState) THEN
intState := WAIT_FOR_RECEIVED_PACKET;
lastState := mqttData._state;
END_IF;
// Check if Keepalive timeout occured.
IF ((mqttData.runTime - mqttData.lastInActivity) > mqttGlobals.MQTT_KEEPALIVE) OR ((mqttData.runTime - mqttData.lastOutActivity) > mqttGlobals.MQTT_KEEPALIVE) THEN
IF (mqttData.pingOutstanding) THEN
// Ping Timeout!
mqttData._state := MQTT_STATE_DISCONNECTED;
ELSE
mqttData.buffer[0] := INT_TO_BYTE(mqttGlobals.MQTTPINGREQ);
mqttData.buffer[1] := 0;
mqttSendTCP(offset := 0, size := 2);
mqttData.lastOutActivity := mqttData.runTime;
mqttData.lastInActivity := mqttData.lastOutActivity;
mqttData.pingOutstanding := true;
END_IF;
END_IF;
CASE (intState) OF
WAIT_FOR_RECEIVED_PACKET:
IF mqttAvailable() THEN
myPacketReader.iBegin := true;
msgId := 0;
intState := READ_COMPLETE_PACKET;
END_IF;
;
READ_COMPLETE_PACKET:
IF (myPacketReader.xDone) THEN
// check if received message is > 0 bytes for variable part
IF (myPacketReader.result > 0) THEN
mqttData.lastInActivity := mqttData.runTime;
typeOfPacket := BYTE_TO_INT(mqttData.buffer[0] AND B#16#F0);
CASE (typeOfPacket) OF
MQTT_PUBLISH:
// received a PUBLISH, 3.3 PUBLISH Publish message
// refer to MQTT Docs. section 3.3 PUBLISH Publish message
tl := WORD_TO_INT(SHL(IN := BYTE_TO_WORD(mqttData.buffer[myPacketReader.lengthLength+1]), N := 8)) + BYTE_TO_INT(mqttData.buffer[myPacketReader.lengthLength+2]);
FOR i := 0 TO tl-1 DO
pubTopicChar[i+2] := BYTE_TO_CHAR(mqttData.buffer[myPacketReader.lengthLength+3+i]);
END_FOR;
pubTopicChar[1] := INT_TO_CHAR(tl);
paylPos := myPacketReader.lengthLength+3+tl;
paylSize := myPacketReader.result - paylPos;
FOR i := 0 TO paylSize-1 DO
payload[i] := mqttData.buffer[paylPos+i];
END_FOR;
payloadSize := paylSize;
callbackPacketType := mqttGlobals.MQTTPUBLISH;
callback := true;
// Check if the Publish messag has QoS = 1, if yes then send PUBACK response
IF BYTE_TO_INT(mqttData.buffer[0] AND B#16#01) = 1 THEN
mqttPUBACK(packetIdMSB:=mqttData.buffer[paylPos-2],packetIdLSB:=mqttData.buffer[paylPos-1]);
END_IF;
;
MQTT_PINGRESP:
// received a PINGRESP, part of MQTT 3.1.2.10 Keep Alive
// (refer MQTT Docs. section 3.1.2.10 Keep Alive, 3.12 PINGREQ PING request, 3.13 PINGRESP PING response)
mqttData.pingOutstanding := false;
;
MQTT_PUBACK:
// received a PUBACK package, part of MQTT QoS 1 handshake
// (refer MQTT Docs. section 4.3.2 QoS 1: AT least once delivery)
callbackPacketType := mqttGlobals.MQTTPUBACK;
// get the packet ID of the PubAck package and store it in mqttData.recPublishPacketID
structTmpPacketId.byteMSB := mqttData.buffer[3];
structTmpPacketId.byteLSB := mqttData.buffer[2];
mqttData.recPublishPacketID := tmpPacketID;
callback := true;
;
MQTT_PUBREC:
// received a PUBREC package, part of MQTT QoS 2 handshake
// (refer MQTT Docs. section 4.3.3 QoS 2: Exactly once delivery)
// get the packet ID of the PubAck package and store it in mqttData.recPublishPacketID
structTmpPacketId.byteMSB := mqttData.buffer[3];
structTmpPacketId.byteLSB := mqttData.buffer[2];
// send out a PUBREL packet
mqttData.buffer[0] := INT_TO_BYTE(mqttGlobals.MQTTPUBREL);
mqttData.buffer[0] := mqttData.buffer[0] OR SHL(IN:=INT_TO_BYTE(mqttGlobals.MQTTQOS1),N:=1); // set qos flag to 2
mqttData.buffer[1] := 16#02;
mqttSendTCP(offset := 0, size := 4);
mqttData.lastOutActivity := mqttData.runTime;
;
MQTT_PUBCOMP:
// received a PUBCOMP package, part of MQTT QoS 2 handshake
// (see MQTT Docs. section 4.3.3 QoS 2: Exactly once delivery)
// notify about successfull QoS 2 handshake
callbackPacketType := mqttGlobals.MQTTPUBCOMP;
// get the packet ID of the PubAck package and store it in mqttData.recPublishPacketID
structTmpPacketId.byteMSB := mqttData.buffer[3];
structTmpPacketId.byteLSB := mqttData.buffer[2];
mqttData.recPublishPacketID := tmpPacketID;
callback := true;
;
END_CASE; // typeOfPacket
END_IF; // myPacketReader.result > 0
intState := WAIT_FOR_RECEIVED_PACKET;
END_IF; // myPacketReader.xDone
;
END_CASE; //intState
END_CASE; // mqttData._state
END_FUNCTION_BLOCK
DATA_BLOCK MQTT_DB MQTT
BEGIN
END_DATA_BLOCK