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
656
657
658
659
660
661
662
663
664
665
666
|
/***************************************************************************/
/* Copyright (c) 2024 Microsoft Corporation */
/* Copyright (c) 2026 Eclipse ThreadX contributors */
/* */
/* This program and the accompanying materials are made available under */
/* the terms of the MIT License which is available at */
/* https://opensource.org/licenses/MIT. */
/* */
/* SPDX-License-Identifier: MIT */
/***************************************************************************/
/* MQTT connect test. This test case validates MQTT client connect without username/password. */
#include "tx_api.h"
#include "nx_api.h"
#include "nxd_mqtt_client.h"
extern void test_control_return(UINT status);
#define DEMO_STACK_SIZE 2048
#define CLIENT_ID "1234"
/* Define the ThreadX and NetX object control blocks... */
static TX_THREAD ntest_0;
static TX_THREAD ntest_1;
static NX_PACKET_POOL pool_0;
static NX_IP ip_0;
static NX_IP ip_1;
static NX_TCP_SOCKET server_socket;
#define NUM_PACKETS 24
#define PACKET_SIZE 1536
#define PACKET_POOL_SIZE (NUM_PACKETS * (PACKET_SIZE + sizeof(NX_PACKET)))
#ifdef NX_SECURE_ENABLE
#include "../web_test/test_device_cert.c"
#include "../web_test/test_ca_cert.c"
/* Declare external cryptosuites. */
extern const NX_SECURE_TLS_CRYPTO nx_crypto_tls_ciphers;
static NX_SECURE_TLS_SESSION tls_server_session;
static NX_SECURE_X509_CERT server_local_certificate;
/* Define crypto metadata buffer. */
static UCHAR client_metadata[5*4096];
static UCHAR server_metadata[5*4096];
/* For remote certificate. */
static NX_SECURE_X509_CERT remote_certificate, remote_issuer, ca_certificate;
static UCHAR remote_cert_buffer[2000];
static UCHAR remote_issuer_buffer[2000];
static UCHAR tls_packet_buffer[2][4096];
#define TEST_LOOP 2
#else
#define TEST_LOOP 1
#endif
static TX_SEMAPHORE semaphore_server_start;
static TX_SEMAPHORE semaphore_client_stop;
/* Define the counters used in the demo application... */
static ULONG error_counter;
/* Define thread prototypes. */
static void ntest_0_entry(ULONG thread_input);
static void ntest_1_entry(ULONG thread_input);
extern void _nx_ram_network_driver(struct NX_IP_DRIVER_STRUCT *driver_req);
extern void SET_ERROR_COUNTER(ULONG *error_counter, CHAR *filename, int line_number);
/* Define what the initial system looks like. */
static NXD_MQTT_CLIENT *client_ptr;
static CHAR *stack_ptr;
#ifdef CTEST
VOID test_application_define(void *first_unused_memory)
#else
void netx_mqtt_client_connect_auth_empty_application_define(void *first_unused_memory)
#endif
{
CHAR *pointer;
UINT status;
/* Setup the working pointer. */
pointer = (CHAR *) first_unused_memory;
error_counter = 0;
/* Create the main thread. */
tx_thread_create(&ntest_0, "thread 0", ntest_0_entry, 0,
pointer, DEMO_STACK_SIZE,
24, 24, TX_NO_TIME_SLICE, TX_DONT_START);
pointer = pointer + DEMO_STACK_SIZE;
/* Create the main thread. */
tx_thread_create(&ntest_1, "thread 1", ntest_1_entry, 0,
pointer, DEMO_STACK_SIZE,
23, 23, TX_NO_TIME_SLICE, TX_AUTO_START);
pointer = pointer + DEMO_STACK_SIZE;
tx_semaphore_create(&semaphore_server_start, "semaphore server start", 0);
tx_semaphore_create(&semaphore_client_stop, "semaphore client stop", 0);
/* Initialize the NetX system. */
nx_system_initialize();
/* Create a packet pool. */
status = nx_packet_pool_create(&pool_0, "NetX Main Packet Pool", PACKET_SIZE, pointer, PACKET_POOL_SIZE);
pointer = pointer + PACKET_POOL_SIZE;
if(status)
SET_ERROR_COUNTER(&error_counter, __FILE__, __LINE__);
/* Create an IP instance. */
status = nx_ip_create(&ip_0, "NetX IP Instance 0", IP_ADDRESS(1, 2, 3, 4), 0xFFFFFF00UL, &pool_0, _nx_ram_network_driver,
pointer, 2048, 1);
pointer = pointer + 2048;
/* Create another IP instance. */
status += nx_ip_create(&ip_1, "NetX IP Instance 1", IP_ADDRESS(1, 2, 3, 5), 0xFFFFFF00UL, &pool_0, _nx_ram_network_driver,
pointer, 2048, 1);
pointer = pointer + 2048;
if(status)
SET_ERROR_COUNTER(&error_counter, __FILE__, __LINE__);
/* Enable ARP and supply ARP cache memory for IP Instance 0. */
status = nx_arp_enable(&ip_0, (void *) pointer, 1024);
pointer = pointer + 1024;
/* Enable ARP and supply ARP cache memory for IP Instance 1. */
status += nx_arp_enable(&ip_1, (void *) pointer, 1024);
pointer = pointer + 1024;
/* Check ARP enable status. */
if(status)
SET_ERROR_COUNTER(&error_counter, __FILE__, __LINE__);
/* Enable TCP processing for both IP instances. */
status = nx_tcp_enable(&ip_0);
status += nx_tcp_enable(&ip_1);
/* Check TCP enable status. */
if(status)
SET_ERROR_COUNTER(&error_counter, __FILE__, __LINE__);
stack_ptr = pointer;
pointer += DEMO_STACK_SIZE;
client_ptr = (NXD_MQTT_CLIENT*)pointer;
}
#ifdef NX_SECURE_ENABLE
/* Define the callback function for tls connection. */
static UINT client_tls_setup(NXD_MQTT_CLIENT* client_ptr, NX_SECURE_TLS_SESSION* tls_session,
NX_SECURE_X509_CERT* certificate, NX_SECURE_X509_CERT* trusted_certificate)
{
UINT status;
/* Create a tls session. */
status = nx_secure_tls_session_create(tls_session,
&nx_crypto_tls_ciphers,
client_metadata,
sizeof(client_metadata));
if (status)
{
return status;
}
nx_secure_tls_session_packet_buffer_set(tls_session, tls_packet_buffer[0], sizeof(tls_packet_buffer[0]));
nx_secure_tls_remote_certificate_allocate(tls_session, &remote_certificate, remote_cert_buffer, sizeof(remote_cert_buffer));
nx_secure_tls_remote_certificate_allocate(tls_session, &remote_issuer, remote_issuer_buffer, sizeof(remote_issuer_buffer));
nx_secure_x509_certificate_initialize(&ca_certificate, test_ca_cert_der, test_ca_cert_der_len,
NX_NULL, 0, NX_NULL, 0, NX_SECURE_X509_KEY_TYPE_NONE);
nx_secure_tls_trusted_certificate_add(tls_session, &ca_certificate);
return(NX_SUCCESS);
}
static UINT server_tls_setup(NX_SECURE_TLS_SESSION *tls_session)
{
UINT status;
status = nx_secure_tls_session_create(tls_session,
&nx_crypto_tls_ciphers,
server_metadata,
sizeof(server_metadata));
if (status)
{
return status;
}
memset(&server_local_certificate, 0, sizeof(server_local_certificate));
nx_secure_x509_certificate_initialize(&server_local_certificate,
test_device_cert_der, test_device_cert_der_len,
NX_NULL, 0, test_device_cert_key_der,
test_device_cert_key_der_len, NX_SECURE_X509_KEY_TYPE_RSA_PKCS1_DER);
nx_secure_tls_local_certificate_add(tls_session, &server_local_certificate);
nx_secure_tls_session_packet_buffer_set(tls_session, tls_packet_buffer[1], sizeof(tls_packet_buffer[1]));
return(NX_SUCCESS);
}
#endif
#define MQTT_CLIENT_THREAD_PRIORITY 2
UINT keepalive_value;
UINT cleansession_value;
#ifdef CTEST
static
#else /* CTEST */
extern
#endif /* CTEST */
UCHAR mqtt_memory[8192];
/* Define the test threads. */
/* This thread sets up MQTT client and makes a connect request without username/password. */
static void ntest_0_entry(ULONG thread_input)
{
UINT status;
NXD_ADDRESS server_address;
UINT i;
/* Print out test information banner. */
printf("NetX Test: MQTT Connect Authentication Empty Test....................");
/* Check for earlier error. */
if(error_counter)
{
printf("ERROR!\n");
test_control_return(1);
}
status = nxd_mqtt_client_create(client_ptr, "my client", CLIENT_ID, strlen(CLIENT_ID), &ip_0, &pool_0,
stack_ptr, DEMO_STACK_SIZE, MQTT_CLIENT_THREAD_PRIORITY, mqtt_memory, sizeof(mqtt_memory));
if(status)
SET_ERROR_COUNTER(&error_counter, __FILE__, __LINE__);
tx_thread_sleep(1);
server_address.nxd_ip_version = 4;
server_address.nxd_ip_address.v4 = IP_ADDRESS(1, 2, 3, 5);
keepalive_value = 0;
cleansession_value = 0;
for (i = 0; i < TEST_LOOP; i++)
{
tx_semaphore_get(&semaphore_server_start, NX_WAIT_FOREVER);
if (i == 0)
{
status = nxd_mqtt_client_connect(client_ptr, &server_address, NXD_MQTT_PORT,
keepalive_value, cleansession_value, NX_IP_PERIODIC_RATE);
#ifndef NXD_MQTT_REQUIRE_TLS
if (status != NXD_MQTT_ERROR_NOT_AUTHORIZED)
SET_ERROR_COUNTER(&error_counter, __FILE__, __LINE__);
status = nxd_mqtt_client_login_set(client_ptr, "", 0, "", 0);
if (status)
SET_ERROR_COUNTER(&error_counter, __FILE__, __LINE__);
status = nxd_mqtt_client_connect(client_ptr, &server_address, NXD_MQTT_PORT,
keepalive_value, cleansession_value, NX_IP_PERIODIC_RATE);
if (status)
SET_ERROR_COUNTER(&error_counter, __FILE__, __LINE__);
status = nxd_mqtt_client_disconnect(client_ptr);
/* Make sure the connection fails but TCP connnection successes. */
status = nxd_mqtt_client_connect(client_ptr, &server_address, NXD_MQTT_PORT,
keepalive_value, cleansession_value, NX_IP_PERIODIC_RATE);
if (status != NXD_MQTT_COMMUNICATION_FAILURE)
SET_ERROR_COUNTER(&error_counter, __FILE__, __LINE__);
#else
if (status != NXD_MQTT_CONNECT_FAILURE)
SET_ERROR_COUNTER(&error_counter, __FILE__, __LINE__);
#endif
}
#ifdef NX_SECURE_ENABLE
else
{
status = nxd_mqtt_client_secure_connect(client_ptr, &server_address, NXD_MQTT_PORT,
client_tls_setup,
keepalive_value, cleansession_value, NX_IP_PERIODIC_RATE);
if (status != NXD_MQTT_ERROR_NOT_AUTHORIZED)
SET_ERROR_COUNTER(&error_counter, __FILE__, __LINE__);
status = nxd_mqtt_client_login_set(client_ptr, "", 0, "", 0);
if (status)
SET_ERROR_COUNTER(&error_counter, __FILE__, __LINE__);
status = nxd_mqtt_client_secure_connect(client_ptr, &server_address, NXD_MQTT_PORT,
client_tls_setup,
keepalive_value, cleansession_value, NX_IP_PERIODIC_RATE);
if (status)
SET_ERROR_COUNTER(&error_counter, __FILE__, __LINE__);
status = nxd_mqtt_client_disconnect(client_ptr);
/* Make sure the connection fails but TCP connnection successes. */
status = nxd_mqtt_client_secure_connect(client_ptr, &server_address, NXD_MQTT_PORT,
client_tls_setup,
keepalive_value, cleansession_value, NX_IP_PERIODIC_RATE);
if (status != NXD_MQTT_COMMUNICATION_FAILURE)
SET_ERROR_COUNTER(&error_counter, __FILE__, __LINE__);
}
#endif
tx_semaphore_put(&semaphore_client_stop);
}
status += nxd_mqtt_client_delete(client_ptr);
/* Determine if the test was successful. */
if(error_counter)
{
printf("ERROR!\n");
test_control_return(1);
}
else
{
printf("SUCCESS!\n");
test_control_return(0);
}
}
static UCHAR content[100];
static UCHAR fixed_header[] = {0x10, 0x00, 0x00, 0x04, 'M', 'Q', 'T', 'T', 0x4, 0x0, 0x0, 0x0};
/* This thread acts as MQTT server, accepting the connection. */
static void ntest_1_entry(ULONG thread_input)
{
UINT status;
ULONG actual_status;
NX_PACKET *packet_ptr;
UCHAR *byte;
UCHAR *buf;
UINT i;
/* Ensure the IP instance has been initialized. */
status = nx_ip_status_check(&ip_1, NX_IP_INITIALIZE_DONE, &actual_status, NX_IP_PERIODIC_RATE);
/* Check for error. */
if(status)
SET_ERROR_COUNTER(&error_counter, __FILE__, __LINE__);
/* Create a socket. */
status = nx_tcp_socket_create(&ip_1, &server_socket, "Server Socket",
NX_IP_NORMAL, NX_FRAGMENT_OKAY, NX_IP_TIME_TO_LIVE, 1000,
NX_NULL, NX_NULL);
/* Check for error. */
if(status)
SET_ERROR_COUNTER(&error_counter, __FILE__, __LINE__);
/* Setup this thread to listen. */
status = nx_tcp_server_socket_listen(&ip_1, NXD_MQTT_PORT, &server_socket, 5, NX_NULL);
/* Check for error. */
if(status)
SET_ERROR_COUNTER(&error_counter, __FILE__, __LINE__);
#ifdef NX_SECURE_ENABLE
/* Session setup. */
server_tls_setup(&tls_server_session);
#endif
tx_thread_resume(&ntest_0);
for (i = 0; i < TEST_LOOP; i++)
{
tx_semaphore_put(&semaphore_server_start);
/* Accept a connection from client socket. */
status = nx_tcp_server_socket_accept(&server_socket, NX_IP_PERIODIC_RATE);
/* Check for error. */
if (status)
{
#ifdef NXD_MQTT_REQUIRE_TLS
if (i == 1)
#endif
SET_ERROR_COUNTER(&error_counter, __FILE__, __LINE__);
}
#ifdef NX_SECURE_ENABLE
if (i == 1)
{
status = nx_secure_tls_session_start(&tls_server_session, &server_socket, NX_WAIT_FOREVER);
if (status)
SET_ERROR_COUNTER(&error_counter, __FILE__, __LINE__);
}
#endif
tx_thread_sleep(1);
if (i == 0)
{
status = nx_tcp_socket_receive(&server_socket, &packet_ptr, 5 * NX_IP_PERIODIC_RATE);
}
#ifdef NX_SECURE_ENABLE
else
{
status = nx_secure_tls_session_receive(&tls_server_session, &packet_ptr, NX_WAIT_FOREVER);
}
#endif
if (status)
{
#ifdef NXD_MQTT_REQUIRE_TLS
if (i == 1)
#endif
SET_ERROR_COUNTER(&error_counter, __FILE__, __LINE__);
}
else
{
/* construct the connect message. */
memcpy(content, fixed_header, sizeof(fixed_header));
/* Append client ID */
content[sizeof(fixed_header)] = (strlen(CLIENT_ID) >> 8) & 0xF;
content[sizeof(fixed_header) + 1] = (strlen(CLIENT_ID) & 0xF);
memcpy(content + sizeof(fixed_header) + 2, CLIENT_ID, strlen(CLIENT_ID));
content[1] = (UCHAR)(sizeof(fixed_header) + strlen(CLIENT_ID));
/* Fill in the connection_flag, keepalive, and cleansession flags. */
content[10] = keepalive_value >> 8;
content[11] = keepalive_value & 0xFF;
if (cleansession_value)
content[9] = content[9] | 2;
/* Validate the MQTT connect request. */
if (memcmp(packet_ptr->nx_packet_prepend_ptr, content, sizeof(fixed_header) + strlen(CLIENT_ID)))
SET_ERROR_COUNTER(&error_counter, __FILE__, __LINE__);
#ifdef NX_SECURE_ENABLE
if (i == 1)
{
nx_packet_release(packet_ptr);
status = nx_secure_tls_packet_allocate(&tls_server_session, &pool_0, &packet_ptr, NX_NO_WAIT);
if (status)
SET_ERROR_COUNTER(&error_counter, __FILE__, __LINE__);
}
#endif
/* Response with error */
byte = packet_ptr->nx_packet_prepend_ptr;
byte[0] = 0x20;
byte[1] = 0x02;
byte[2] = 0;
byte[3] = 5;
packet_ptr->nx_packet_append_ptr = packet_ptr->nx_packet_prepend_ptr + 4;
packet_ptr->nx_packet_length = 4;
if (i == 0)
{
status = nx_tcp_socket_send(&server_socket, packet_ptr, 1);
}
#ifdef NX_SECURE_ENABLE
else
{
status = nx_secure_tls_session_send(&tls_server_session, packet_ptr, NX_WAIT_FOREVER);
tx_thread_sleep(1);
}
#endif
#ifdef NX_SECURE_ENABLE
if (i == 1)
{
/* End session. */
nx_secure_tls_session_end(&tls_server_session, NX_NO_WAIT);
}
#endif
/* Disconnect. */
nx_tcp_socket_disconnect(&server_socket, NX_IP_PERIODIC_RATE);
status = nx_tcp_server_socket_unaccept(&server_socket);
if (status)
SET_ERROR_COUNTER(&error_counter, __FILE__, __LINE__);
/* Prepare to accept another connection. */
status = nx_tcp_server_socket_relisten(&ip_1, NXD_MQTT_PORT, &server_socket);
if ((status != NX_SUCCESS) && (status != NX_CONNECTION_PENDING))
SET_ERROR_COUNTER(&error_counter, __FILE__, __LINE__);
/* Accept a connection from client socket. */
status = nx_tcp_server_socket_accept(&server_socket, NX_IP_PERIODIC_RATE);
/* Check for error. */
if (status)
SET_ERROR_COUNTER(&error_counter, __FILE__, __LINE__);
#ifdef NX_SECURE_ENABLE
if (i == 1)
{
status = nx_secure_tls_session_start(&tls_server_session, &server_socket, NX_WAIT_FOREVER);
if (status)
SET_ERROR_COUNTER(&error_counter, __FILE__, __LINE__);
}
#endif
tx_thread_sleep(1);
if (i == 0)
{
status = nx_tcp_socket_receive(&server_socket, &packet_ptr, 5 * NX_IP_PERIODIC_RATE);
}
#ifdef NX_SECURE_ENABLE
else
{
status = nx_secure_tls_session_receive(&tls_server_session, &packet_ptr, NX_WAIT_FOREVER);
}
#endif
if (status)
SET_ERROR_COUNTER(&error_counter, __FILE__, __LINE__);
/* construct the connect message. */
/* Byte 8 flag: username and password should be set. */
content[9] |= 0xC0;
/* Append client ID */
content[sizeof(fixed_header)] = (strlen(CLIENT_ID) >> 8) & 0xFF;
content[sizeof(fixed_header) + 1] = (strlen(CLIENT_ID) & 0xFF);
memcpy(content + sizeof(fixed_header) + 2, CLIENT_ID, strlen(CLIENT_ID));
buf = content + sizeof(fixed_header) + 2 + strlen(CLIENT_ID);
*buf = 0;
*(buf + 1) = 0;
buf = buf + 2;
*buf = 0;
*(buf + 1) = 0;
content[1] = (UCHAR)(sizeof(fixed_header) + strlen(CLIENT_ID) + 4);
/* Fill in the connection_flag, keepalive, and cleansession flags. */
content[10] = keepalive_value >> 8;
content[11] = keepalive_value & 0xFF;
if (cleansession_value)
content[9] = content[9] | 2;
/* Validate the MQTT connect request. */
if (memcmp(packet_ptr->nx_packet_prepend_ptr, content, content[1] + 2))
SET_ERROR_COUNTER(&error_counter, __FILE__, __LINE__);
#ifdef NX_SECURE_ENABLE
if (i == 1)
{
nx_packet_release(packet_ptr);
status = nx_secure_tls_packet_allocate(&tls_server_session, &pool_0, &packet_ptr, NX_NO_WAIT);
if (status)
SET_ERROR_COUNTER(&error_counter, __FILE__, __LINE__);
}
#endif
/* Response with SUCCESS */
byte = packet_ptr->nx_packet_prepend_ptr;
byte[0] = 0x20;
byte[1] = 0x02;
byte[3] = 0;
byte[4] = 0;
packet_ptr->nx_packet_append_ptr = packet_ptr->nx_packet_prepend_ptr + 4;
packet_ptr->nx_packet_length = 4;
if (i == 0)
{
status = nx_tcp_socket_send(&server_socket, packet_ptr, 1);
}
#ifdef NX_SECURE_ENABLE
else
{
status = nx_secure_tls_session_send(&tls_server_session, packet_ptr, NX_WAIT_FOREVER);
}
#endif
if (status)
SET_ERROR_COUNTER(&error_counter, __FILE__, __LINE__);
tx_thread_sleep(1);
#ifdef NX_SECURE_ENABLE
if (i == 1)
{
/* End session. */
nx_secure_tls_session_end(&tls_server_session, NX_NO_WAIT);
}
#endif
/* Disconnect. */
nx_tcp_socket_disconnect(&server_socket, NX_IP_PERIODIC_RATE);
/* Unaccept the server socket. */
status = nx_tcp_server_socket_unaccept(&server_socket);
/* Check for error. */
if (status)
SET_ERROR_COUNTER(&error_counter, __FILE__, __LINE__);
/* Prepare to accept another connection. */
status = nx_tcp_server_socket_relisten(&ip_1, NXD_MQTT_PORT, &server_socket);
if (status)
SET_ERROR_COUNTER(&error_counter, __FILE__, __LINE__);
/* Accept a connection from client socket. */
status = nx_tcp_server_socket_accept(&server_socket, NX_IP_PERIODIC_RATE);
/* Check for error. */
if (status)
SET_ERROR_COUNTER(&error_counter, __FILE__, __LINE__);
#ifdef NX_SECURE_ENABLE
if (i == 1)
{
status = nx_secure_tls_session_start(&tls_server_session, &server_socket, NX_WAIT_FOREVER);
if (status)
SET_ERROR_COUNTER(&error_counter, __FILE__, __LINE__);
tx_thread_sleep(NX_IP_PERIODIC_RATE);
/* End session. */
nx_secure_tls_session_end(&tls_server_session, NX_NO_WAIT);
}
#endif
tx_thread_sleep(1);
/* Disconnect. */
nx_tcp_socket_disconnect(&server_socket, NX_IP_PERIODIC_RATE);
}
tx_semaphore_get(&semaphore_client_stop, NX_WAIT_FOREVER);
/* Unaccept the server socket. */
status = nx_tcp_server_socket_unaccept(&server_socket);
/* Check for error. */
if (status)
SET_ERROR_COUNTER(&error_counter, __FILE__, __LINE__);
/* Prepare to accept another connection. */
status = nx_tcp_server_socket_relisten(&ip_1, NXD_MQTT_PORT, &server_socket);
if (status)
SET_ERROR_COUNTER(&error_counter, __FILE__, __LINE__);
}
/* Unlisten on the server port. */
status = nx_tcp_server_socket_unlisten(&ip_1, NXD_MQTT_PORT);
/* Check for error. */
if (status)
SET_ERROR_COUNTER(&error_counter, __FILE__, __LINE__);
/* Delete the socket. */
status = nx_tcp_socket_delete(&server_socket);
/* Check for error. */
if(status)
SET_ERROR_COUNTER(&error_counter, __FILE__, __LINE__);
}
|